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

fix action

上级 0f6a8b97
......@@ -3,3 +3,8 @@
module github.com/wa-lang/wa
go 1.17
require (
github.com/tetratelabs/wazero v1.0.0-pre.1
github.com/wa-lang/wabt-go v1.1.0
)
......@@ -39,8 +39,6 @@ type Option struct {
TargetArch string
TargetOS string
Clang string
WasmLLC string
WasmLD string
}
// 命令行程序对象
......@@ -68,26 +66,6 @@ func NewApp(opt *Option) *App {
p.opt.Clang = "clang"
}
}
if p.opt.WasmLLC == "" {
if runtime.GOOS == "windows" {
p.opt.WasmLLC, _ = exec.LookPath("llc.exe")
} else {
p.opt.WasmLLC, _ = exec.LookPath("llc")
}
if p.opt.WasmLLC == "" {
p.opt.WasmLLC = "llc"
}
}
if p.opt.WasmLD == "" {
if runtime.GOOS == "windows" {
p.opt.WasmLD, _ = exec.LookPath("wasm-ld.exe")
} else {
p.opt.WasmLD, _ = exec.LookPath("wasm-ld")
}
if p.opt.WasmLD == "" {
p.opt.WasmLD = "wasm-ld"
}
}
if p.opt.TargetOS == "" {
p.opt.TargetOS = runtime.GOOS
}
......
// 版权 @2019 凹语言 作者。保留所有权利。
package app
import (
"context"
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"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"
"github.com/wa-lang/wabt-go"
)
var wat2wasmPath string
func RunWasm(filename string) (stdoutStderr []byte, err error) {
dst := filename
if strings.HasSuffix(filename, ".wat") {
dst += ".wasm"
if stdoutStderr, err = RunWat2Wasm(filename, "-o", dst); err != nil {
return stdoutStderr, err
}
}
wasmBytes, err := os.ReadFile(dst)
if err != nil {
return nil, err
}
ctx := context.Background()
r := wazero.NewRuntimeWithConfig(ctx,
wazero.NewRuntimeConfig().WithWasmCore2(),
)
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)
n, _ := m.Memory().ReadUint32Le(ctx, iov+4)
bytes, _ := m.Memory().Read(ctx, pos, n)
fmt.Print(string(bytes))
return 0
}).
Instantiate(ctx, r)
if err != nil {
return nil, err
}
_, err = r.InstantiateModuleFromBinary(ctx, wasmBytes)
if err != nil {
return nil, err
}
return nil, nil
}
func InstallWat2wasm(path string) error {
if path == "" {
path, _ = os.Getwd()
}
var exePath string
if isDir(path) {
if runtime.GOOS == "windows" {
exePath = filepath.Join(path, "wat2wasm.exe")
} else {
exePath = filepath.Join(path, "wat2wasm")
}
} else {
exePath = path
}
if err := os.MkdirAll(filepath.Dir(exePath), 0777); err != nil {
logger.Tracef(&config.EnableTrace_app, "install wat2wasm failed: %+v", err)
}
if err := os.WriteFile(exePath, wabt.LoadWat2Wasm(), 0777); err != nil {
logger.Tracef(&config.EnableTrace_app, "install wat2wasm failed: %+v", err)
return err
}
return nil
}
func RunWat2Wasm(args ...string) (stdoutStderr []byte, err error) {
if wat2wasmPath == "" {
logger.Tracef(&config.EnableTrace_app, "wat2wasm not found")
return nil, errors.New("wat2wasm not found")
}
cmd := exec.Command(wat2wasmPath, args...)
stdoutStderr, err = cmd.CombinedOutput()
return
}
func init() {
var baseName = "wat2wasm"
if runtime.GOOS == "windows" {
baseName += ".exe"
}
// 1. exe 同级目录存在 wat2wasm ?
wat2wasmPath = filepath.Join(curExeDir(), baseName)
if exeExists(wat2wasmPath) {
return
}
// 2. 当前目录存在 wat2wasm ?
cwd, _ := os.Getwd()
wat2wasmPath = filepath.Join(cwd, baseName)
if exeExists(wat2wasmPath) {
return
}
// 3. 本地系统存在 wat2wasm ?
if s, _ := exec.LookPath(baseName); s != "" {
wat2wasmPath = s
return
}
// 4. wat2wasm 安装到同级目录存 ?
if err := os.WriteFile(wat2wasmPath, wabt.LoadWat2Wasm(), 0777); err != nil {
logger.Tracef(&config.EnableTrace_app, "install wat2wasm failed: %+v", err)
return
}
}
// 是否为目录
func isDir(path string) bool {
if fi, _ := os.Lstat(path); fi != nil && fi.IsDir() {
return true
}
return false
}
// exe 文件存在
func exeExists(path string) bool {
fi, err := os.Lstat(path)
if err != nil {
return false
}
if !fi.Mode().IsRegular() {
return false
}
return true
}
// 当前执行程序所在目录
func curExeDir() string {
s, err := os.Executable()
if err != nil {
logger.Panicf("os.Executable() failed: %+v", err)
}
return filepath.Dir(s)
}
......@@ -195,18 +195,20 @@ const modBaseWat = `
;; {{$putchar/body/begin}}
)
;; 打印整数
(func $__print_i32 (param $x i32)
;; {{$print_i32/body/begin}}
(local $div i32)
(local $rem i32)
(func $putchar (param $ch i32)
local.get $ch
call $__print_char
)
(func $$print_char (param $ch i32)
local.get $ch
call $__print_char
)
(func $$print_i32 (param $x i32)
;; if $x == 0 { print '0'; return }
(i32.eq (local.get $x) (i32.const 0))
if
(call $putchar (i32.const 48)) ;; '0'
(call $putchar (i32.const 10)) ;; '\n'
(return)
end
......@@ -217,25 +219,33 @@ const modBaseWat = `
(call $putchar (i32.const 45)) ;; '-'
end
local.get $x
call $__print_i32
)
;; 打印整数
(func $__print_i32 (param $x i32)
;; {{$print_i32/body/begin}}
(local $div i32)
(local $rem i32)
;; if $x == 0 { print '0'; return }
(i32.eq (local.get $x) (i32.const 0))
if
(return)
end
;; print_i32($x / 10)
;; puchar($x%10 + '0')
(local.set $div (i32.div_s (local.get $x) (i32.const 10)))
(local.set $rem (i32.rem_s (local.get $x) (i32.const 10)))
(call $print_i32 (local.get $div))
(call $__print_i32 (local.get $div))
(call $putchar (i32.add (local.get $rem) (i32.const 48))) ;; '0'
;; {{$print_i32/body/end}}
)
(func $putchar (param $ch i32)
local.get $ch
call $__print_char
)
(func $print_i32 (param $ch i32)
local.get $ch
call $__print_i32
)
;; ----------------------------------------------------
;; _start 函数
;; ----------------------------------------------------
......
......@@ -87,6 +87,4 @@ func (p *Compiler) CompilePackage(ssaPkg *ssa.Package) {
for _, v := range fns {
p.module.Funcs = append(p.module.Funcs, newFunctionGenerator(p).genFunction(v))
}
println(p.module.String())
}
......@@ -225,7 +225,7 @@ func (g *functionGenerator) genInstruction(inst ssa.Instruction) []wir.Instructi
func (g *functionGenerator) genValue(v ssa.Value) ([]wir.Instruction, wtypes.ValueType) {
if _, ok := g.locals_map[v]; ok {
logger.Printf("Instruction already exist:%s\n", v)
logger.Printf("Instruction already exist:%v\n", v)
}
switch v := v.(type) {
......
......@@ -31,10 +31,7 @@ func main() {
cliApp.Flags = []cli.Flag{
&cli.StringFlag{Name: "os", Usage: "set target OS", Value: runtime.GOOS},
&cli.StringFlag{Name: "arch", Usage: "set target Arch", Value: runtime.GOARCH},
&cli.StringFlag{Name: "backend", Usage: "set backend code generator"},
&cli.StringFlag{Name: "clang", Usage: "set clang"},
&cli.StringFlag{Name: "wasm-llc", Usage: "set wasm-llc"},
&cli.StringFlag{Name: "wasm-ld", Usage: "set wasm-ld"},
&cli.BoolFlag{Name: "debug", Aliases: []string{"d"}, Usage: "set debug mode"},
&cli.StringFlag{Name: "trace", Aliases: []string{"t"}, Usage: "set trace mode (*|app|compiler|loader)"},
}
......@@ -72,7 +69,7 @@ func main() {
cliApp.Commands = []*cli.Command{
{
Name: "init",
Usage: "init a sketch app",
Usage: "init a sketch wa module",
ArgsUsage: "app-name",
Flags: []cli.Flag{
&cli.StringFlag{
......@@ -280,6 +277,10 @@ func main() {
Usage: "set output file",
Value: "a.out.wat",
},
&cli.BoolFlag{
Name: "run",
Usage: "run wat or wasm",
},
},
Action: func(c *cli.Context) error {
outfile := c.String("output")
......@@ -303,7 +304,30 @@ func main() {
os.Exit(1)
}
} else {
fmt.Println(string(output))
if c.Bool("run") {
outfile = "a.out.wat"
err := os.WriteFile(outfile, []byte(output), 0666)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
} else {
fmt.Println(string(output))
}
}
if c.Bool("run") {
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))
}
}
return nil
......@@ -325,6 +349,26 @@ func main() {
return nil
},
},
{
Hidden: true,
Name: "install-wat2wasm",
Usage: "install-wat2wasm tool",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "dir",
Usage: "set output dir",
Value: "",
},
},
Action: func(c *cli.Context) error {
outdir := c.String("dir")
if err := app.InstallWat2wasm(outdir); err != nil {
fmt.Println(err)
os.Exit(1)
}
return nil
},
},
}
cliApp.Run(os.Args)
......@@ -336,7 +380,5 @@ func build_Options(c *cli.Context) *app.Option {
TargetOS: c.String("os"),
TargetArch: c.String("arch"),
Clang: c.String("clang"),
WasmLLC: c.String("wasm-llc"),
WasmLD: c.String("wasm-ld"),
}
}
# 版权 @2019 凹语言 作者。保留所有权利。
fn main() {
for n := 2; n <= 30; n = n + 1 {
var isPrime int = 1
for i := 2; i*i <= n; i = i + 1 {
if x := n % i; x == 0 {
isPrime = 0
}
}
if isPrime != 0 {
println(n)
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册