mirror of
https://gitee.com/wa-lang/wa.git
synced 2025-12-06 09:18:53 +08:00
调整参数处理代码
This commit is contained in:
34
.github/workflows/publish.yml
vendored
Normal file
34
.github/workflows/publish.yml
vendored
Normal file
@@ -0,0 +1,34 @@
|
||||
name: Deploy
|
||||
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- master
|
||||
pull_request:
|
||||
branches:
|
||||
- master
|
||||
types:
|
||||
- closed
|
||||
|
||||
jobs:
|
||||
deploy:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v3
|
||||
|
||||
- name: Set up Go
|
||||
uses: actions/setup-go@v2
|
||||
with:
|
||||
go-version: 1.17
|
||||
|
||||
- run: make -C internal/app/wawasm
|
||||
|
||||
- name: Deploy
|
||||
uses: peaceiris/actions-gh-pages@v3
|
||||
with:
|
||||
personal_token: ${{ secrets.WA_DEPLOY }}
|
||||
publish_dir: docs
|
||||
publish_branch: gh-pages
|
||||
user_name: "github-actions[bot]"
|
||||
user_email: "github-actions[bot]@users.noreply.github.com"
|
||||
1
.gitignore
vendored
1
.gitignore
vendored
@@ -14,5 +14,6 @@ wat2wasm.exe
|
||||
*.ll
|
||||
*.s
|
||||
*.out*
|
||||
*.wasm
|
||||
|
||||
_target
|
||||
|
||||
33
api/api.go
33
api/api.go
@@ -7,7 +7,6 @@ import (
|
||||
"io/fs"
|
||||
|
||||
"github.com/wa-lang/wa/internal/backends/compiler_wat"
|
||||
"github.com/wa-lang/wa/internal/backends/target_spec"
|
||||
"github.com/wa-lang/wa/internal/config"
|
||||
"github.com/wa-lang/wa/internal/format"
|
||||
"github.com/wa-lang/wa/internal/loader"
|
||||
@@ -42,28 +41,6 @@ type PkgVFS = config.PkgVFS
|
||||
// 指针和整数大小
|
||||
type StdSize = config.StdSizes
|
||||
|
||||
// 目标机器
|
||||
type Machine = target_spec.Machine
|
||||
|
||||
const (
|
||||
Machine_default = Machine_Wasm32_wa // 默认输出的目标类型
|
||||
|
||||
Machine_Wasm32_wa = target_spec.Machine_Wasm32_wa // 凹语言定义的 WASM 规范
|
||||
Machine_Wasm32_wasi = target_spec.Machine_Wasm32_wasi // WASI 定义的 WASM 规范
|
||||
)
|
||||
|
||||
// 解析字符串类型
|
||||
func ParseMachine(s string) (target Machine, ok bool) {
|
||||
switch t := Machine(s); t {
|
||||
case Machine_Wasm32_wa:
|
||||
return t, true
|
||||
case Machine_Wasm32_wasi:
|
||||
return t, true
|
||||
default:
|
||||
return "", false
|
||||
}
|
||||
}
|
||||
|
||||
// 加载 WaModFile 文件
|
||||
// 如果 vfs 为空则从本地文件系统读取
|
||||
func LoadManifest(vfs fs.FS, appPath string) (p *Manifest, err error) {
|
||||
@@ -89,28 +66,26 @@ func LoadProgramVFS(vfs *config.PkgVFS, cfg *config.Config, pkgPath string) (*Pr
|
||||
}
|
||||
|
||||
// 构建 wat 目标
|
||||
func BuildFile(filename string, src interface{}, target Machine) (wat []byte, err error) {
|
||||
cfg := config.DefaultConfig()
|
||||
func BuildFile(cfg *config.Config, filename string, src interface{}) (wat []byte, err error) {
|
||||
prog, err := LoadProgramFile(cfg, filename, src)
|
||||
if err != nil || prog == nil {
|
||||
logger.Tracef(&config.EnableTrace_api, "LoadProgramVFS failed, err = %v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
watOut, err := compiler_wat.New().Compile(prog, target)
|
||||
watOut, err := compiler_wat.New().Compile(prog)
|
||||
return []byte(watOut), err
|
||||
}
|
||||
|
||||
// 构建 wat 目标
|
||||
func BuildVFS(vfs *config.PkgVFS, appPkg string, target Machine) (wat []byte, err error) {
|
||||
cfg := config.DefaultConfig()
|
||||
func BuildVFS(cfg *config.Config, vfs *config.PkgVFS, appPkg string) (wat []byte, err error) {
|
||||
prog, err := LoadProgramVFS(vfs, cfg, appPkg)
|
||||
if err != nil || prog == nil {
|
||||
logger.Tracef(&config.EnableTrace_api, "LoadProgramVFS failed, err = %v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
watOut, err := compiler_wat.New().Compile(prog, target)
|
||||
watOut, err := compiler_wat.New().Compile(prog)
|
||||
return []byte(watOut), err
|
||||
}
|
||||
|
||||
|
||||
@@ -9,12 +9,13 @@ import (
|
||||
"os"
|
||||
|
||||
"github.com/wa-lang/wa/internal/app/apputil"
|
||||
"github.com/wa-lang/wa/internal/config"
|
||||
)
|
||||
|
||||
// 执行凹代码
|
||||
func RunCode(filename, code string) (stdoutStderr []byte, err error) {
|
||||
func RunCode(cfg *config.Config, filename, code string) (stdoutStderr []byte, err error) {
|
||||
// 编译为 wat 格式
|
||||
wat, err := BuildFile(filename, code, Machine_Wasm32_wa)
|
||||
wat, err := BuildFile(cfg, filename, code)
|
||||
|
||||
// wat 写到临时文件
|
||||
outfile := "a.out.wat"
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"log"
|
||||
|
||||
"github.com/wa-lang/wa/api"
|
||||
"github.com/wa-lang/wa/internal/config"
|
||||
)
|
||||
|
||||
func ExampleRunCode() {
|
||||
@@ -22,7 +23,7 @@ func ExampleRunCode() {
|
||||
}
|
||||
`
|
||||
|
||||
output, err := api.RunCode("hello.wa", code)
|
||||
output, err := api.RunCode(config.DefaultConfig(), "hello.wa", code)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
1
docs/index.html
Normal file
1
docs/index.html
Normal file
@@ -0,0 +1 @@
|
||||
https://wa-lang.org
|
||||
@@ -22,7 +22,6 @@ import (
|
||||
"github.com/wa-lang/wa/internal/backends/compiler_c"
|
||||
"github.com/wa-lang/wa/internal/backends/compiler_llvm"
|
||||
"github.com/wa-lang/wa/internal/backends/compiler_wat"
|
||||
"github.com/wa-lang/wa/internal/backends/target_spec"
|
||||
"github.com/wa-lang/wa/internal/config"
|
||||
"github.com/wa-lang/wa/internal/format"
|
||||
"github.com/wa-lang/wa/internal/loader"
|
||||
@@ -34,15 +33,6 @@ import (
|
||||
"github.com/wa-lang/wa/internal/waroot"
|
||||
)
|
||||
|
||||
// 命令行选项
|
||||
type Option struct {
|
||||
Debug bool
|
||||
TargetArch string
|
||||
TargetOS string
|
||||
Clang string
|
||||
Llc string
|
||||
}
|
||||
|
||||
// 命令行程序对象
|
||||
type App struct {
|
||||
opt Option
|
||||
@@ -367,14 +357,14 @@ func (p *App) LLVM(infile string, outfile string, target string, debug bool) err
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *App) WASM(filename string, target target_spec.Machine) ([]byte, error) {
|
||||
func (p *App) WASM(filename string) ([]byte, error) {
|
||||
cfg := config.DefaultConfig()
|
||||
prog, err := loader.LoadProgram(cfg, filename)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
output, err := compiler_wat.New().Compile(prog, target)
|
||||
output, err := compiler_wat.New().Compile(prog)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
52
internal/app/opt.go
Normal file
52
internal/app/opt.go
Normal file
@@ -0,0 +1,52 @@
|
||||
// 版权 @2019 凹语言 作者。保留所有权利。
|
||||
|
||||
package app
|
||||
|
||||
import "github.com/wa-lang/wa/internal/config"
|
||||
|
||||
// 命令行选项
|
||||
type Option struct {
|
||||
Debug bool
|
||||
TargetArch string
|
||||
TargetOS string
|
||||
Clang string
|
||||
Llc string
|
||||
LD_StackSize int
|
||||
LD_MaxMemory int
|
||||
}
|
||||
|
||||
func (opt *Option) Config() *config.Config {
|
||||
cfg := config.DefaultConfig()
|
||||
|
||||
if opt.Debug {
|
||||
cfg.Debug = true
|
||||
}
|
||||
if opt.TargetArch != "" {
|
||||
cfg.WaArch = opt.TargetArch
|
||||
}
|
||||
if opt.TargetOS != "" {
|
||||
cfg.WaOS = opt.TargetOS
|
||||
}
|
||||
if opt.LD_StackSize != 0 {
|
||||
cfg.LDFlags.StackSize = opt.LD_StackSize
|
||||
}
|
||||
if opt.LD_MaxMemory != 0 {
|
||||
cfg.LDFlags.MaxMemory = opt.LD_MaxMemory
|
||||
}
|
||||
|
||||
switch cfg.WaArch {
|
||||
case "wasm":
|
||||
cfg.WaSizes.MaxAlign = 4
|
||||
cfg.WaSizes.WordSize = 4
|
||||
case "amd64":
|
||||
cfg.WaSizes.MaxAlign = 8
|
||||
cfg.WaSizes.WordSize = 8
|
||||
case "arm64":
|
||||
cfg.WaSizes.MaxAlign = 8
|
||||
cfg.WaSizes.WordSize = 8
|
||||
default:
|
||||
panic("todo")
|
||||
}
|
||||
|
||||
return cfg
|
||||
}
|
||||
7
internal/app/wawasm/Makefile
Normal file
7
internal/app/wawasm/Makefile
Normal file
@@ -0,0 +1,7 @@
|
||||
# 版权 @2022 凹语言 作者。保留所有权利。
|
||||
|
||||
default:
|
||||
GOOS=js GOARCH=wasm go build -o wa.wasm
|
||||
mv wa.wasm ../../../docs
|
||||
|
||||
clean:
|
||||
10
internal/app/wawasm/main_nowasm.go
Normal file
10
internal/app/wawasm/main_nowasm.go
Normal file
@@ -0,0 +1,10 @@
|
||||
// 版权 @2022 凹语言 作者。保留所有权利。
|
||||
|
||||
//go:build !wasm
|
||||
// +build !wasm
|
||||
|
||||
package main
|
||||
|
||||
func main() {
|
||||
println("only for build wa.wasm")
|
||||
}
|
||||
21
internal/app/wawasm/main_wasm.go
Normal file
21
internal/app/wawasm/main_wasm.go
Normal file
@@ -0,0 +1,21 @@
|
||||
// 版权 @2022 凹语言 作者。保留所有权利。
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"syscall/js"
|
||||
|
||||
"github.com/wa-lang/wa/api"
|
||||
)
|
||||
|
||||
func main() {
|
||||
window := js.Global().Get("window")
|
||||
waCode := window.Get("waCode").String()
|
||||
|
||||
wat, err := api.BuildFile("hello.wa", waCode, "wasm32-wa")
|
||||
if err != nil {
|
||||
window.Set("waWat", err.Error())
|
||||
} else {
|
||||
window.Set("waWat", string(wat))
|
||||
}
|
||||
}
|
||||
@@ -3,11 +3,8 @@
|
||||
package compiler_wat
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/wa-lang/wa/internal/backends/compiler_wat/wir"
|
||||
"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"
|
||||
"github.com/wa-lang/wa/internal/ssa"
|
||||
)
|
||||
@@ -26,15 +23,8 @@ func New() *Compiler {
|
||||
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)
|
||||
}
|
||||
func (p *Compiler) Compile(prog *loader.Program) (output string, err error) {
|
||||
p.module.BaseWat = modBaseWat_wa
|
||||
|
||||
for _, pkg := range prog.Pkgs {
|
||||
p.ssaPkg = pkg.SSAPkg
|
||||
|
||||
@@ -1,11 +0,0 @@
|
||||
// 版权 @2019 凹语言 作者。保留所有权利。
|
||||
|
||||
package target_spec
|
||||
|
||||
// 目标机器
|
||||
type Machine string
|
||||
|
||||
const (
|
||||
Machine_Wasm32_wa Machine = "wasm32-wa" // 凹语言定义的 WASM 规范
|
||||
Machine_Wasm32_wasi Machine = "wasm32-wasi" // WASI 定义的 WASM 规范
|
||||
)
|
||||
68
main.go
68
main.go
@@ -17,7 +17,6 @@ import (
|
||||
"github.com/wa-lang/wa/internal/3rdparty/cli"
|
||||
"github.com/wa-lang/wa/internal/app"
|
||||
"github.com/wa-lang/wa/internal/app/apputil"
|
||||
"github.com/wa-lang/wa/internal/backends/target_spec"
|
||||
"github.com/wa-lang/wa/internal/config"
|
||||
)
|
||||
|
||||
@@ -45,14 +44,6 @@ func main() {
|
||||
Aliases: []string{"t"},
|
||||
Usage: "set trace mode (*|app|compiler|loader)",
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "clang",
|
||||
Usage: "set llvm/clang path",
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "llc",
|
||||
Usage: "set llvm/llc path",
|
||||
},
|
||||
}
|
||||
|
||||
cliApp.Before = func(c *cli.Context) error {
|
||||
@@ -72,7 +63,7 @@ func main() {
|
||||
}
|
||||
|
||||
ctx := app.NewApp(build_Options(c))
|
||||
output, err := ctx.WASM(c.Args().First(), target_spec.Machine_Wasm32_wa)
|
||||
output, err := ctx.WASM(c.Args().First())
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
os.Exit(1)
|
||||
@@ -109,7 +100,10 @@ func main() {
|
||||
Name: "debug",
|
||||
Usage: "only for dev/debug",
|
||||
Action: func(c *cli.Context) error {
|
||||
wat, err := api.BuildFile("hello.wa", "fn main() { println(123) }", "")
|
||||
wat, err := api.BuildFile(
|
||||
config.DefaultConfig(),
|
||||
"hello.wa", "fn main() { println(123) }",
|
||||
)
|
||||
if err != nil {
|
||||
if len(wat) != 0 {
|
||||
fmt.Println(string(wat))
|
||||
@@ -171,7 +165,7 @@ func main() {
|
||||
}
|
||||
|
||||
ctx := app.NewApp(build_Options(c))
|
||||
output, err := ctx.WASM(c.Args().First(), target_spec.Machine_Wasm32_wa)
|
||||
output, err := ctx.WASM(c.Args().First())
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
os.Exit(1)
|
||||
@@ -222,7 +216,7 @@ func main() {
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "target",
|
||||
Usage: "set target",
|
||||
Usage: "set target (*wa|wasi|arduino)",
|
||||
},
|
||||
&cli.IntFlag{
|
||||
Name: "ld-stack-size",
|
||||
@@ -241,21 +235,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("invalid target: %q", s)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
if target == "" {
|
||||
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())
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
os.Exit(1)
|
||||
@@ -296,6 +277,14 @@ func main() {
|
||||
Name: "debug",
|
||||
Usage: "dump orginal intermediate representation",
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "clang",
|
||||
Usage: "set llvm/clang path",
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "llc",
|
||||
Usage: "set llvm/llc path",
|
||||
},
|
||||
},
|
||||
Action: func(c *cli.Context) error {
|
||||
outfile := c.String("output")
|
||||
@@ -464,9 +453,26 @@ func main() {
|
||||
}
|
||||
|
||||
func build_Options(c *cli.Context) *app.Option {
|
||||
return &app.Option{
|
||||
Debug: c.Bool("debug"),
|
||||
Clang: c.String("clang"),
|
||||
Llc: c.String("llc"),
|
||||
opt := &app.Option{
|
||||
Debug: c.Bool("debug"),
|
||||
Clang: c.String("clang"),
|
||||
Llc: c.String("llc"),
|
||||
LD_StackSize: c.Int("ld-stack-size"),
|
||||
LD_MaxMemory: c.Int("ld-max-memory"),
|
||||
}
|
||||
switch c.String("target") {
|
||||
case "", "wa":
|
||||
opt.TargetArch = "wasm"
|
||||
opt.TargetOS = "wa"
|
||||
case "wasi":
|
||||
opt.TargetArch = "wasm"
|
||||
opt.TargetOS = "wasi"
|
||||
case "arduino":
|
||||
opt.TargetArch = "wasm"
|
||||
opt.TargetOS = "arduino"
|
||||
default:
|
||||
opt.TargetArch = "wasm"
|
||||
opt.TargetOS = "unknown"
|
||||
}
|
||||
return opt
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user