Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
wa-lang
wa
提交
8c0d68aa
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,发现更多精彩内容 >>
提交
8c0d68aa
编写于
11月 06, 2022
作者:
B
Ben Shi
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
llvm: implement translation of global variables.
上级
edb85342
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
90 addition
and
23 deletion
+90
-23
_examples/misc/native_test.wa
_examples/misc/native_test.wa
+61
-19
internal/backends/compiler_llvm/compile.go
internal/backends/compiler_llvm/compile.go
+13
-4
internal/backends/compiler_llvm/compile_function.go
internal/backends/compiler_llvm/compile_function.go
+7
-0
internal/backends/compiler_llvm/utils.go
internal/backends/compiler_llvm/utils.go
+9
-0
未找到文件。
_examples/misc/native_test.wa
浏览文件 @
8c0d68aa
...
...
@@ -33,13 +33,18 @@ fn main {
test_struct1(13, 1, 0)
test_struct1(2, 1, 0)
test_pointer()
test_convert0()
test_convert1()
test_convert2()
test_convert3()
test_global_consts()
test_global_variables_0()
test_global_variables_1()
test_pointer(1)
test_pointer(-1)
bye()
}
...
...
@@ -166,23 +171,6 @@ fn test_struct1(a int, b int, c int) {
}
}
fn test_pointer() {
p := new_int()
println(*p)
set_int(p)
println(*p)
}
fn new_int() *i32 {
var i i32
i = 42
return &i
}
fn set_int(p *i32) {
*p = 13
}
fn convert_i32_to_i16(a i32) i16 {
return i16(a)
}
...
...
@@ -251,3 +239,57 @@ fn test_convert3() {
fn test_print(a i16, b u16, c i8, d u8, e f32) {
println(a, ", ", b, ", ", c, ", ", d, ", ", e/2)
}
const gbegin = 10
const gend = 20
fn test_global_consts() {
for i := gbegin; i < gend; i++ {
println(i, " - ", gbegin, " = ", i - gbegin)
}
}
type ty0 struct {
v0 int
v1 float64
}
var gv0 ty0 = ty0{5555, 3.1415926}
var gv1 [4]int = [4]int{81, 82, 17, 76}
fn test_global_variables_0() {
println("gv0: {", gv0.v0, ", ", gv0.v1, "}")
println("gv1: {", gv1[0], ", ", gv1[1], ", ", gv1[2], ", ", gv1[3], "}")
}
fn test_global_variables_1() {
gv0.v0 = 8888
gv0.v1 = 2.71828
gv1[0] += 2
gv1[1] += 3
gv1[2] += 4
gv1[3] += 5
println("gv0: {", gv0.v0, ", ", gv0.v1, "}")
println("gv1: {", gv1[0], ", ", gv1[1], ", ", gv1[2], ", ", gv1[3], "}")
}
var gint0 int = 100
var gint1 int = 200
fn get_int_addr(a int) *int {
if a > 0 {
return &gint0
} else {
return &gint1
}
}
fn set_int(a *int) {
*a += 10
}
fn test_pointer(a int) {
println(gint0, ", ", gint1)
p := get_int_addr(a)
set_int(p)
println(gint0, ", ", gint1)
}
internal/backends/compiler_llvm/compile.go
浏览文件 @
8c0d68aa
...
...
@@ -71,12 +71,21 @@ func (p *Compiler) compilePackage() error {
}
}
// Emit all global variables.
for
_
,
gv
:=
range
gs
{
p
.
output
.
WriteString
(
"@"
)
p
.
output
.
WriteString
(
getNormalName
(
gv
.
Name
()))
p
.
output
.
WriteString
(
" = global "
)
tys
:=
getTypeStr
(
gv
.
Type
(),
p
.
target
)
p
.
output
.
WriteString
(
tys
[
0
:
(
len
(
tys
)
-
1
)])
p
.
output
.
WriteString
(
" zeroinitializer
\n
"
)
}
if
len
(
gs
)
>
0
{
p
.
output
.
WriteString
(
"
\n
"
)
}
// Generate LLVM-IR for each global function.
for
_
,
v
:=
range
fns
{
// TODO: Support the builtin function 'init'.
if
v
.
Name
()
==
"init"
{
continue
}
if
err
:=
p
.
compileFunction
(
v
);
err
!=
nil
{
return
err
}
...
...
internal/backends/compiler_llvm/compile_function.go
浏览文件 @
8c0d68aa
...
...
@@ -46,6 +46,13 @@ func (p *Compiler) compileFunction(fn *ssa.Function) error {
}
p
.
output
.
WriteString
(
") {
\n
"
)
// Initialize all global variables at the beginning of the main function.
if
fn
.
Name
()
==
"main"
{
p
.
output
.
WriteString
(
"__basic_block_init:
\n
"
)
p
.
output
.
WriteString
(
" call void @init()
\n
"
)
p
.
output
.
WriteString
(
" br label %__basic_block_0
\n\n
"
)
}
// Translate Go SSA intermediate instructions.
for
i
,
b
:=
range
fn
.
Blocks
{
p
.
output
.
WriteString
(
fmt
.
Sprintf
(
"__basic_block_%d:
\n
"
,
i
))
...
...
internal/backends/compiler_llvm/utils.go
浏览文件 @
8c0d68aa
...
...
@@ -262,7 +262,16 @@ func getValueStr(val ssa.Value) string {
case
*
ssa
.
Parameter
:
return
"%"
+
val
.
Name
()
case
*
ssa
.
Global
:
return
"@"
+
getNormalName
(
val
.
Name
())
default
:
return
"%"
+
val
.
Name
()
}
}
func
getNormalName
(
name
string
)
string
{
name
=
strings
.
ReplaceAll
(
name
,
"."
,
"__"
)
name
=
strings
.
ReplaceAll
(
name
,
"$"
,
"___"
)
return
name
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录