提交 e8fa425c 编写于 作者: chai2010's avatar chai2010

默认输出 wa 规范的 wat 文件

上级 78581bbd
......@@ -45,8 +45,7 @@ type StdSize = config.StdSizes
type Machine = target_spec.Machine
const (
// 默认输出的目标类型
Machine_default = target_spec.Machine_Wasm32_wa
Machine_default = Machine_Wasm32_wa // 默认输出的目标类型
Machine_Wasm32_wa = target_spec.Machine_Wasm32_wa // 凹语言定义的 WASM 规范
Machine_Wasm32_wasi = target_spec.Machine_Wasm32_wasi // WASI 定义的 WASM 规范
......
......@@ -14,7 +14,6 @@ import (
"github.com/tetratelabs/wazero"
"github.com/tetratelabs/wazero/api"
"github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1"
"github.com/wa-lang/wa/internal/config"
"github.com/wa-lang/wa/internal/logger"
......@@ -44,11 +43,6 @@ func RunWasm(filename string) (stdoutStderr []byte, err error) {
)
defer r.Close(ctx)
if false {
if _, err := wasi_snapshot_preview1.Instantiate(ctx, r); err != nil {
return nil, err
}
}
_, err = r.NewModuleBuilder("wasi_snapshot_preview1").
ExportFunction("fd_write", func(m api.Module, fd, iov, iov_len, p_nwritten uint32) uint32 {
pos, _ := m.Memory().ReadUint32Le(ctx, iov)
......@@ -57,6 +51,14 @@ func RunWasm(filename string) (stdoutStderr []byte, err error) {
fmt.Print(string(bytes))
return 0
}).
ExportFunction("PrintI32", func(m api.Module, v uint32) {
fmt.Print(v)
return
}).
ExportFunction("PrintRune", func(m api.Module, ch uint32) {
fmt.Printf("%c", rune(ch))
return
}).
Instantiate(ctx, r)
if err != nil {
return nil, err
......
......@@ -43,7 +43,24 @@ const (
_waStart = "_start"
)
const modBaseWat = `
const modBaseWat_wa = `
(import "wasi_snapshot_preview1" "PrintI32" (func $$PrintI32 (param $x i32)))
(import "wasi_snapshot_preview1" "PrintRune" (func $$PrintRune (param $ch i32)))
(memory $memory 1)
(export "memory" (memory $memory))
(export "_start" (func $_start))
(func $_start
;; {{$_start/body/begin}}
;; (call $main.init)
(call $main)
;; {{$_start/body/end}}
)
`
const modBaseWat_wasi = `
(import "wasi_snapshot_preview1" "fd_write"
(func $$FdWrite (param i32 i32 i32 i32) (result i32))
)
......
......@@ -3,6 +3,8 @@
package compiler_wat
import (
"fmt"
"github.com/wa-lang/wa/internal/backends/compiler_wat/wir/wat"
"github.com/wa-lang/wa/internal/backends/target_spec"
"github.com/wa-lang/wa/internal/loader"
......@@ -17,11 +19,19 @@ type Compiler struct {
func New() *Compiler {
p := new(Compiler)
p.module.BaseWat = modBaseWat
return p
}
func (p *Compiler) Compile(prog *loader.Program, target target_spec.Machine) (output string, err error) {
switch target {
case target_spec.Machine_Wasm32_wa, "":
p.module.BaseWat = modBaseWat_wa
case target_spec.Machine_Wasm32_wasi:
p.module.BaseWat = modBaseWat_wasi
default:
return "", fmt.Errorf("compiler_wat.Compiler: unsupport target: %v", target)
}
p.CompilePackage(prog.SSAMainPkg)
return p.module.String(), nil
......
......@@ -6,9 +6,6 @@ package target_spec
type Machine string
const (
// 默认输出的目标类型
Machine_default = Machine_Wasm32_wa
Machine_Wasm32_wa Machine = "wasm32-wa" // 凹语言定义的 WASM 规范
Machine_Wasm32_wasi Machine = "wasm32-wasi" // WASI 定义的 WASM 规范
Machine_llir_64bit Machine = "llir-64bit" // LLVM IR 64 位
......
......@@ -62,13 +62,8 @@ func main() {
cli.ShowAppHelpAndExit(c, 0)
}
var target = target_spec.Machine_Wasm32_wasi
if c.Bool("html") {
target = target_spec.Machine_Wasm32_wa
}
ctx := app.NewApp(build_Options(c))
output, err := ctx.WASM(c.Args().First(), target)
output, err := ctx.WASM(c.Args().First(), target_spec.Machine_Wasm32_wa)
if err != nil {
fmt.Println(err)
os.Exit(1)
......@@ -159,10 +154,6 @@ func main() {
Name: "html",
Usage: "output html",
},
&cli.StringFlag{
Name: "target",
Usage: "set target",
},
},
Action: func(c *cli.Context) error {
if c.NArg() == 0 {
......@@ -170,25 +161,8 @@ func main() {
os.Exit(1)
}
var target target_spec.Machine
if s := c.String("target"); s != "" {
if t, ok := api.ParseMachine(s); ok {
target = t
} else {
fmt.Printf("imvalid target: %q", s)
os.Exit(1)
}
}
if target == "" {
if c.Bool("html") {
target = target_spec.Machine_Wasm32_wa
} else {
target = target_spec.Machine_Wasm32_wasi
}
}
ctx := app.NewApp(build_Options(c))
output, err := ctx.WASM(c.Args().First(), target)
output, err := ctx.WASM(c.Args().First(), target_spec.Machine_Wasm32_wa)
if err != nil {
fmt.Println(err)
os.Exit(1)
......@@ -204,16 +178,20 @@ func main() {
os.Exit(1)
}
stdoutStderr, err := app.RunWasm(outfile)
if err != nil {
if c.Bool("html") {
// todo
} else {
stdoutStderr, err := app.RunWasm(outfile)
if err != nil {
if len(stdoutStderr) > 0 {
fmt.Println(string(stdoutStderr))
}
fmt.Println(err)
os.Exit(1)
}
if len(stdoutStderr) > 0 {
fmt.Println(string(stdoutStderr))
}
fmt.Println(err)
os.Exit(1)
}
if len(stdoutStderr) > 0 {
fmt.Println(string(stdoutStderr))
}
return nil
......@@ -255,11 +233,7 @@ func main() {
}
}
if target == "" {
if c.Bool("html") {
target = target_spec.Machine_Wasm32_wa
} else {
target = target_spec.Machine_Wasm32_wasi
}
target = target_spec.Machine_Wasm32_wa
}
ctx := app.NewApp(build_Options(c))
......
// 版权 @2022 凹语言 作者。保留所有权利。
//go:build wasm
// +build wasm
// 凹语言™ 命令行程序。
package main
import (
"encoding/json"
"github.com/wa-lang/wa/api"
)
func main() {
println("hello wasm")
}
type WASMResut struct {
Result string `json:"result"`
Error string `json:"error"`
}
//export walang_api_BuildCode
func walang_api_BuildCode(code string) string {
var result WASMResut
wat, err := api.BuildFile("hello.wa", code)
if err != nil {
result.Error = err.Error()
}
result.Result = string(wat)
b, _ := json.Marshal(result)
return string(b)
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册