Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
wa-lang
wa
提交
78581bbd
wa
项目概览
wa-lang
/
wa
12 个月 前同步成功
通知
68
Star
655
Fork
45
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
wa
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
78581bbd
编写于
9月 11, 2022
作者:
chai2010
浏览文件
操作
浏览文件
下载
差异文件
Merge branch 'master' of github.com:wa-lang/wa
上级
dd9bab4f
e1369ee1
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
55 addition
and
77 deletion
+55
-77
internal/backends/compiler_wat/base.wat.go
internal/backends/compiler_wat/base.wat.go
+52
-74
internal/backends/compiler_wat/compile_func.go
internal/backends/compiler_wat/compile_func.go
+3
-3
未找到文件。
internal/backends/compiler_wat/base.wat.go
浏览文件 @
78581bbd
...
...
@@ -17,8 +17,8 @@ const _WASM_PAGE_SIZE = 65536
const
(
// ;; heap 和 stack 状态(__heap_base 只读)
// ;; | 0 <-- stack --> | <-- static-data --> | <-- heap --> |
// (global $
__stack_ptr
(mut i32) (i32.const 1024)) ;; index=0
// (global $
__
heap_base i32 (i32.const 2048)) ;; index=1
// (global $
$stack_prt
(mut i32) (i32.const 1024)) ;; index=0
// (global $
$
heap_base i32 (i32.const 2048)) ;; index=1
__stack_ptr_index
=
0
__heap_base_index
=
1
)
...
...
@@ -26,20 +26,18 @@ const (
// 内置函数名字
const
(
// 栈函数
_waStackPtr
=
"waStackPtr"
_waStackAlloc
=
"waStackAlloc"
_waStackFree
=
"waStackFree"
_waStackPtr
=
"$$StackPtr"
_waStackAlloc
=
"$$StackAlloc"
// 堆管理函数
_waHeapPtr
=
"waHeapPtr"
_waAlloc
=
"waAlloc"
_waRetain
=
"waRetain"
_waFree
=
"waFree"
_waHeapPtr
=
"$$HeapPtr"
_waHeapAlloc
=
"$$HeapAlloc"
_waHeapFree
=
"$$HeapFree"
// 输出函数
_waPrint
Char
=
"putchar
"
_waPrint
String
=
"puts
"
_waPrintInt32
=
"
print_i
32"
_waPrint
String
=
"$$Puts
"
_waPrint
Rune
=
"$$PrintRune
"
_waPrintInt32
=
"
$$PrintI
32"
// 开始函数
_waStart
=
"_start"
...
...
@@ -47,7 +45,7 @@ const (
const
modBaseWat
=
`
(import "wasi_snapshot_preview1" "fd_write"
(func $
fd_w
rite (param i32 i32 i32 i32) (result i32))
(func $
$FdW
rite (param i32 i32 i32 i32) (result i32))
)
(memory $memory 1)
...
...
@@ -57,110 +55,90 @@ const modBaseWat = `
;; (export "main.main" (func $main.main))
;; | 0 <-- stack --> | <-- static-data --> | <-- heap --> |
(global $
__stack_ptr
(mut i32) (i32.const 1024)) ;; index=0
(global $
__
heap_base i32 (i32.const 2048)) ;; index=1
(global $
$stack_prt
(mut i32) (i32.const 1024)) ;; index=0
(global $
$
heap_base i32 (i32.const 2048)) ;; index=1
(func $
wa
StackPtr (result i32)
(global.get $
__stack_ptr
)
(func $
$
StackPtr (result i32)
(global.get $
$stack_prt
)
)
(func $
wa
StackAlloc (param $size i32) (result i32)
;; $
__stack_ptr
-= $size
(global.set $
__stack_ptr (i32.sub (global.get $__stack_ptr
) (local.get $size)))
;; return $
__stack_ptr
(return (global.get $
__stack_ptr
))
(func $
$
StackAlloc (param $size i32) (result i32)
;; $
$stack_prt
-= $size
(global.set $
$stack_prt (i32.sub (global.get $$stack_prt
) (local.get $size)))
;; return $
$stack_prt
(return (global.get $
$stack_prt
))
)
(func $waStackFree (param $size i32)
;; $__stack_ptr += $size
(global.set $__stack_ptr (i32.add (global.get $__stack_ptr) (local.get $size)))
(func $$HeapPtr (result i32)
(global.get $$heap_base)
)
(func $waHeapPtr (result i32)
(global.get $__heap_base)
)
(func $waAlloc (param $size i32) (result i32)
;; {{$waAlloc/body/begin}}
unreachable
;; {{$waAlloc/body/end}}
)
(func $waRetain(param $ptr i32) (result i32)
;; {{$waRetain/body/begin}}
(func $$HeapAlloc (param $size i32) (result i32)
;; {{$$HeapAlloc/body/begin}}
unreachable
;; {{$
waRetain
/body/end}}
;; {{$
$HeapAlloc
/body/end}}
)
(func $
wa
Free (param $ptr i32)
;; {{$
wa
Free/body/begin}}
(func $
$Heap
Free (param $ptr i32)
;; {{$
$Heap
Free/body/begin}}
unreachable
;; {{$
wa
Free/body/end}}
;; {{$
$Heap
Free/body/end}}
)
(func $
p
uts (param $str i32) (param $len i32)
;; {{$
p
uts/body/begin}}
(func $
$P
uts (param $str i32) (param $len i32)
;; {{$
$P
uts/body/begin}}
(local $sp i32)
(local $p_iov i32)
(local $p_nwritten i32)
(local $stdout i32)
(local.set $sp (global.get $
__stack_ptr
))
(local.set $sp (global.get $
$stack_prt
))
(local.set $p_iov (call $
wa
StackAlloc (i32.const 8)))
(local.set $p_iov (call $
$
StackAlloc (i32.const 8)))
(local.set $p_nwritten (call $
wa
StackAlloc (i32.const 4)))
(local.set $p_nwritten (call $
$
StackAlloc (i32.const 4)))
(i32.store offset=0 align=1 (local.get $p_iov) (local.get $str))
(i32.store offset=4 align=1 (local.get $p_iov) (local.get $len))
(local.set $stdout (i32.const 1))
(call $
fd_w
rite
(call $
$FdW
rite
(local.get $stdout)
(local.get $p_iov) (i32.const 1)
(local.get $p_nwritten)
)
(global.set $
__stack_ptr
(local.get $sp))
(global.set $
$stack_prt
(local.get $sp))
drop
;; {{$
p
uts/body/end}}
;; {{$
$P
uts/body/end}}
)
(func $
__print_char
(param $ch i32)
;; {{$
putchar
/body/begin}}
(func $
$PrintRune
(param $ch i32)
;; {{$
$PrintRune
/body/begin}}
(local $sp i32)
(local $p_ch i32)
(local.set $sp (global.get $
__stack_ptr
))
(local.set $sp (global.get $
$stack_prt
))
(local.set $p_ch (call $
wa
StackAlloc (i32.const 4)))
(local.set $p_ch (call $
$
StackAlloc (i32.const 4)))
(i32.store offset=0 align=1 (local.get $p_ch) (local.get $ch))
(call $
p
uts (local.get $p_ch) (i32.const 1))
(call $
$P
uts (local.get $p_ch) (i32.const 1))
(global.set $
__stack_ptr
(local.get $sp))
(global.set $
$stack_prt
(local.get $sp))
;; {{$
putchar
/body/begin}}
;; {{$
$PrintRune
/body/begin}}
)
(func $putchar (param $ch i32)
local.get $ch
call $__print_char
)
(func $$print_rune (param $ch i32)
local.get $ch
call $__print_char
)
(func $$print_i32 (param $x i32)
(func $$PrintI32 (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 $
$PrintRune
(i32.const 48)) ;; '0'
(return)
end
...
...
@@ -168,15 +146,15 @@ const modBaseWat = `
(i32.lt_s (local.get $x) (i32.const 0))
if
(local.set $x (i32.sub (i32.const 0) (local.get $x)))
(call $
putchar
(i32.const 45)) ;; '-'
(call $
$PrintRune
(i32.const 45)) ;; '-'
end
local.get $x
call $
__
print_i32
call $
$$
print_i32
)
(func $
__
print_i32 (param $x i32)
;; {{$print_i32/body/begin}}
(func $
$$
print_i32 (param $x i32)
;; {{$
$$
print_i32/body/begin}}
(local $div i32)
(local $rem i32)
...
...
@@ -191,10 +169,10 @@ const modBaseWat = `
;; 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 $
putchar
(i32.add (local.get $rem) (i32.const 48))) ;; '0'
(call $
$$
print_i32 (local.get $div))
(call $
$PrintRune
(i32.add (local.get $rem) (i32.const 48))) ;; '0'
;; {{$print_i32/body/end}}
;; {{$
$$
print_i32/body/end}}
)
(func $_start
...
...
internal/backends/compiler_wat/compile_func.go
浏览文件 @
78581bbd
...
...
@@ -350,11 +350,11 @@ func (g *functionGenerator) genBuiltin(call *ssa.CallCommon) ([]wat.Inst, wir.Va
switch
arg
.
Type
()
.
(
type
)
{
case
wir
.
I32
:
insts
=
append
(
insts
,
arg
.
EmitGet
()
...
)
insts
=
append
(
insts
,
wat
.
NewInstCall
(
"$
print_i
32"
))
insts
=
append
(
insts
,
wat
.
NewInstCall
(
"$
PrintI
32"
))
case
wir
.
RUNE
:
insts
=
append
(
insts
,
arg
.
EmitGet
()
...
)
insts
=
append
(
insts
,
wat
.
NewInstCall
(
"$
print_r
une"
))
insts
=
append
(
insts
,
wat
.
NewInstCall
(
"$
PrintR
une"
))
default
:
logger
.
Fatalf
(
"Todo: print(%T)"
,
arg
.
Type
())
...
...
@@ -363,7 +363,7 @@ func (g *functionGenerator) genBuiltin(call *ssa.CallCommon) ([]wat.Inst, wir.Va
if
call
.
Value
.
Name
()
==
"println"
{
insts
=
append
(
insts
,
wir
.
NewConst
(
wir
.
I32
{},
strconv
.
Itoa
(
'\n'
))
.
EmitGet
()
...
)
insts
=
append
(
insts
,
wat
.
NewInstCall
(
"$
print_r
une"
))
insts
=
append
(
insts
,
wat
.
NewInstCall
(
"$
PrintR
une"
))
}
return
insts
,
wir
.
Void
{}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录