Skip to content

Commit

Permalink
wat 后端连接 host 代码片段
Browse files Browse the repository at this point in the history
  • Loading branch information
chai2010 committed Nov 10, 2023
1 parent 7ce1f65 commit bc775d6
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 3 deletions.
43 changes: 42 additions & 1 deletion internal/backends/compiler_wat/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,45 @@ func (p *Compiler) CompileWsFiles(prog *loader.Program) {
p.module.BaseWat = sb.String()
}

func (p *Compiler) CompileWhostFiles(prog *loader.Program) string {
var sb strings.Builder

sb.WriteString(waroot.GetBaseWhostCode(p.prog.Cfg.WaOS))
sb.WriteString("\n")

var pkgpathList = make([]string, 0, len(prog.Pkgs))
for pkgpath := range prog.Pkgs {
pkgpathList = append(pkgpathList, pkgpath)
}
sort.Strings(pkgpathList)

var lineCommentSep = "// -" + strings.Repeat("-", 60-4) + "\n"

for _, pkgpath := range pkgpathList {
pkg := prog.Pkgs[pkgpath]
if len(pkg.WhostFiles) == 0 {
continue
}

func() {
sb.WriteString(lineCommentSep)
sb.WriteString("// package: " + pkgpath + "\n")
sb.WriteString(lineCommentSep)
sb.WriteString("\n")

for _, sf := range pkg.WhostFiles {
sb.WriteString("// file: " + sf.Name + "\n")
sb.WriteString("\n")

sb.WriteString(strings.TrimSpace(sf.Code))
sb.WriteString("\n")
}
}()
}

return sb.String()
}

func (p *Compiler) CompilePkgType(ssaPkg *ssa.Package) {
var memnames []string
for name := range ssaPkg.Members {
Expand Down Expand Up @@ -442,6 +481,9 @@ func (p *Compiler) funcsForJSBinding() []JSFunc {
}

func (p *Compiler) GenJSBinding(wasmFilename string) string {
var bf bytes.Buffer
bf.WriteString(p.CompileWhostFiles(p.prog))

// 模板
t, err := template.New("js").Parse(js_binding_tmpl)
if err != nil {
Expand All @@ -453,7 +495,6 @@ func (p *Compiler) GenJSBinding(wasmFilename string) string {
Globals: p.globalsForJsBinding(),
Funcs: p.funcsForJSBinding(),
}
var bf bytes.Buffer
err = t.Execute(&bf, data)
if err != nil {
logger.Fatal(err)
Expand Down
6 changes: 4 additions & 2 deletions internal/backends/compiler_wat/wir/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ type fnSigWrap struct {
typeAddr int
}

/**************************************
/*
*************************************
Module:
**************************************/
*************************************
*/
type Module struct {
VOID, BOOL, RUNE, U8, U16, I32, U32, UPTR, I64, U64, INT, UINT, F32, F64, STRING, BYTES ValueType

Expand Down
Empty file added waroot/src/base.wa-host.js
Empty file.
Empty file added waroot/src/base.wa-host.wasi
Empty file.
24 changes: 24 additions & 0 deletions waroot/waroot.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ var baseWsFile_clang string
//go:embed src/base.wat.ws
var baseWsFile_wat string

//go:embed src/base.wa-host.js
var baseWhostFile_js string

//go:embed src/base.wa-host.wasi
var baseWhostFile_wasi string

// 获取汇编基础代码
func GetBaseWsCode(backend string) string {
switch backend {
Expand All @@ -65,6 +71,24 @@ func GetBaseWsCode(backend string) string {
panic("unreachable")
}

// 获取宿主基础代码
func GetBaseWhostCode(waos string) string {
switch waos {
case config.WaOS_chrome:
return baseWhostFile_js
case config.WaOS_js:
return baseWhostFile_js
case config.WaOS_wasi:
return baseWhostFile_wasi
}
for _, s := range config.WaBackend_List {
if s == waos {
return ""
}
}
panic("unreachable")
}

func GetFS() fs.FS {
// embed.FS 均采用 Unix 风格路径
fs, err := fs.Sub(_warootFS, "src")
Expand Down

0 comments on commit bc775d6

Please sign in to comment.