Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
wa-lang
wa
提交
b8a5d1eb
wa
项目概览
wa-lang
/
wa
11 个月 前同步成功
通知
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,发现更多精彩内容 >>
提交
b8a5d1eb
编写于
7月 27, 2022
作者:
3
3dgen
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
增加ssa.Append指令处理
上级
c70c17c9
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
161 addition
and
61 deletion
+161
-61
internal/backends/compiler_c/cir/cconstant/cconstant.go
internal/backends/compiler_c/cir/cconstant/cconstant.go
+9
-9
internal/backends/compiler_c/cir/ctypes/ctypes.go
internal/backends/compiler_c/cir/ctypes/ctypes.go
+39
-9
internal/backends/compiler_c/cir/util.go
internal/backends/compiler_c/cir/util.go
+1
-1
internal/backends/compiler_c/compile_func.go
internal/backends/compiler_c/compile_func.go
+112
-42
未找到文件。
internal/backends/compiler_c/cir/cconstant/cconstant.go
浏览文件 @
b8a5d1eb
...
...
@@ -206,24 +206,24 @@ func (d *Double) Type() ctypes.Type {
func
(
d
*
Double
)
IsExpr
()
{}
/**************************************
String:
String
Lit
:
**************************************/
type
String
struct
{
//
常数
值
type
String
Lit
struct
{
//
字符串字面
值
x
string
}
//
func
NewString
(
v
string
)
*
String
{
return
&
String
{
x
:
v
}
func
NewString
Lit
(
v
string
)
*
StringLit
{
return
&
String
Lit
{
x
:
v
}
}
func
(
s
*
String
)
CIRString
()
string
{
func
(
s
*
String
Lit
)
CIRString
()
string
{
return
"
\"
"
+
s
.
x
+
"
\"
"
}
func
(
s
*
String
)
Type
()
ctypes
.
Type
{
return
&
ctypes
.
String
Type
{}
func
(
s
*
String
Lit
)
Type
()
ctypes
.
Type
{
return
&
ctypes
.
String
Lit
{}
}
func
(
s
*
String
)
IsExpr
()
{}
func
(
s
*
String
Lit
)
IsExpr
()
{}
internal/backends/compiler_c/cir/ctypes/ctypes.go
浏览文件 @
b8a5d1eb
...
...
@@ -20,7 +20,7 @@ type Type interface {
fmt
.
Stringer
// CIRString 返回该类型的CIR表达
CIRString
()
string
// Name 返回该类型的名字,例如字符串类型的Name()=="string",而其CIRString()=="
std
::string"
// Name 返回该类型的名字,例如字符串类型的Name()=="string",而其CIRString()=="
$wartc
::string"
Name
()
string
// Equal 判断u是否与当前类型相同
Equal
(
u
Type
)
bool
...
...
@@ -387,28 +387,58 @@ func (t *RefType) Equal(u Type) bool {
}
/**************************************
String
Type
:
String
Lit
:
**************************************/
type
String
Type
struct
{
type
String
Lit
struct
{
}
// String 返回该类型的字符串表达
func
(
t
*
String
Type
)
String
()
string
{
func
(
t
*
String
Lit
)
String
()
string
{
return
t
.
CIRString
()
}
// CIRString 返回该类型的CIR语法表达
func
(
t
*
StringType
)
CIRString
()
string
{
return
"std::string"
func
(
t
*
StringLit
)
CIRString
()
string
{
//字符串字面值类型没有CIR表达
logger
.
Fatal
(
"Unreachable"
)
return
""
}
func
(
t
*
StringLit
)
Name
()
string
{
return
"StringLit"
}
// Equal 判断u是否与当前类型相同
func
(
t
*
StringLit
)
Equal
(
u
Type
)
bool
{
if
_
,
ok
:=
u
.
(
*
StringLit
);
ok
{
return
true
}
return
false
}
/**************************************
String:
**************************************/
type
StringVar
struct
{
}
// String 返回该类型的字符串表达
func
(
t
*
StringVar
)
String
()
string
{
return
t
.
CIRString
()
}
// CIRString 返回该类型的CIR语法表达
func
(
t
*
StringVar
)
CIRString
()
string
{
return
"$wartc::String"
}
func
(
t
*
String
Type
)
Name
()
string
{
func
(
t
*
String
Var
)
Name
()
string
{
return
"string"
}
// Equal 判断u是否与当前类型相同
func
(
t
*
String
Type
)
Equal
(
u
Type
)
bool
{
if
_
,
ok
:=
u
.
(
*
String
Type
);
ok
{
func
(
t
*
String
Var
)
Equal
(
u
Type
)
bool
{
if
_
,
ok
:=
u
.
(
*
String
Var
);
ok
{
return
true
}
return
false
...
...
internal/backends/compiler_c/cir/util.go
浏览文件 @
b8a5d1eb
...
...
@@ -44,7 +44,7 @@ func ToCType(from types.Type) ctypes.Type {
return
ctypes
.
Double
case
types
.
String
:
return
&
ctypes
.
String
Type
{}
return
&
ctypes
.
String
Var
{}
default
:
logger
.
Fatalf
(
"Unknown type:%s"
,
t
)
...
...
internal/backends/compiler_c/compile_func.go
浏览文件 @
b8a5d1eb
...
...
@@ -30,6 +30,10 @@ func newFunctionGenerator(p *CompilerC) *functionGenerator {
}
func
(
g
*
functionGenerator
)
getValue
(
i
interface
{})
cir
.
Expr
{
if
i
==
nil
{
return
cir
.
NewRawExpr
(
"$wartc::ZeroValue()"
,
ctypes
.
Void
)
}
for
scope
:=
g
.
compiler
.
curScope
;
scope
!=
nil
;
scope
=
scope
.
Parent
{
for
_
,
v
:=
range
scope
.
Vars
{
if
v
.
AssociatedSSAObj
==
i
{
...
...
@@ -64,16 +68,17 @@ func (g *functionGenerator) getValue(i interface{}) cir.Expr {
val
,
_
:=
constant
.
Float64Val
(
v
.
Value
)
return
cconstant
.
NewDouble
(
val
)
case
types
.
String
:
case
types
.
String
,
types
.
UntypedString
:
val
:=
constant
.
StringVal
(
v
.
Value
)
return
cconstant
.
NewString
(
val
)
return
cconstant
.
NewString
Lit
(
val
)
default
:
logger
.
Fatal
(
"Todo: getValue(), "
,
t
)
}
case
*
types
.
Slice
:
logger
.
Fatal
(
"Todo: getValue(), "
,
t
)
elem_type
:=
cir
.
ToCType
(
t
.
Elem
())
return
cir
.
NewRawExpr
(
"$wartc::Slice<"
+
elem_type
.
CIRString
()
+
">("
+
g
.
getValue
(
v
.
Value
)
.
CIRString
()
+
")"
,
ctypes
.
NewSlice
(
elem_type
))
default
:
logger
.
Fatal
(
"Todo: getValue(), "
,
t
)
...
...
@@ -408,8 +413,7 @@ func (g *functionGenerator) genIndexAddr(inst *ssa.IndexAddr) cir.Expr {
return
cir
.
NewRawExpr
(
x
.
CIRString
()
+
"->At("
+
g
.
getValue
(
inst
.
Index
)
.
CIRString
()
+
")"
,
ctypes
.
NewPointerType
(
t
.
GetElem
()))
case
*
ctypes
.
Slice
:
logger
.
Fatal
(
"a bug here?"
)
return
cir
.
NewRawExpr
(
"{ "
+
x
.
CIRString
()
+
"->At("
+
g
.
getValue
(
inst
.
Index
)
.
CIRString
()
+
"), "
+
x
.
CIRString
()
+
".GetBlock() }"
,
ctypes
.
NewRefType
(
t
.
GetElem
()))
return
cir
.
NewRawExpr
(
"{ "
+
x
.
CIRString
()
+
"->At("
+
g
.
getValue
(
inst
.
Index
)
.
CIRString
()
+
"), "
+
x
.
CIRString
()
+
"->GetBlock() }"
,
ctypes
.
NewRefType
(
t
.
GetElem
()))
default
:
logger
.
Fatalf
(
"Todo: genIndexAddr(), %T %s"
,
x
.
Type
(),
inst
)
...
...
@@ -421,8 +425,7 @@ func (g *functionGenerator) genIndexAddr(inst *ssa.IndexAddr) cir.Expr {
return
cir
.
NewRawExpr
(
"{ "
+
x
.
CIRString
()
+
".GetRaw()->At("
+
g
.
getValue
(
inst
.
Index
)
.
CIRString
()
+
"), "
+
x
.
CIRString
()
+
".GetBlock() }"
,
ctypes
.
NewRefType
(
t
.
GetElem
()))
case
*
ctypes
.
Slice
:
logger
.
Fatal
(
"a bug here?"
)
return
cir
.
NewRawExpr
(
"{ "
+
x
.
CIRString
()
+
".GetRaw()->At("
+
g
.
getValue
(
inst
.
Index
)
.
CIRString
()
+
"), "
+
x
.
CIRString
()
+
".GetBlock() }"
,
ctypes
.
NewRefType
(
t
.
GetElem
()))
return
cir
.
NewRawExpr
(
"{ "
+
x
.
CIRString
()
+
".GetRaw()->At("
+
g
.
getValue
(
inst
.
Index
)
.
CIRString
()
+
"), "
+
x
.
CIRString
()
+
".GetRaw()->GetBlock() }"
,
ctypes
.
NewRefType
(
t
.
GetElem
()))
}
default
:
...
...
@@ -456,6 +459,43 @@ func (g *functionGenerator) genSlice(inst *ssa.Slice) cir.Expr {
s
+=
high
.
CIRString
()
+
")"
return
cir
.
NewRawExpr
(
s
,
ctypes
.
NewSlice
(
t
.
GetElem
()))
case
*
ctypes
.
StringVar
:
var
low
,
high
cir
.
Expr
if
inst
.
Low
!=
nil
{
low
=
g
.
getValue
(
inst
.
Low
)
}
else
{
low
=
cconstant
.
NewInt
(
ctypes
.
Int64
,
0
)
}
if
inst
.
High
!=
nil
{
high
=
g
.
getValue
(
inst
.
High
)
}
else
{
high
=
cir
.
NewRawExpr
(
x
.
CIRString
()
+
".Len()"
,
ctypes
.
Uint64
)
}
s
:=
x
.
CIRString
()
+
".Sub("
s
+=
low
.
CIRString
()
+
", "
s
+=
high
.
CIRString
()
+
")"
return
cir
.
NewRawExpr
(
s
,
&
ctypes
.
StringVar
{})
case
*
ctypes
.
StringLit
:
r
:=
g
.
compiler
.
curScope
.
AddTempVarDecl
(
g
.
genRegister
(),
&
ctypes
.
StringVar
{})
r
.
InitVal
=
x
var
low
,
high
cir
.
Expr
if
inst
.
Low
!=
nil
{
low
=
g
.
getValue
(
inst
.
Low
)
}
else
{
low
=
cconstant
.
NewInt
(
ctypes
.
Int64
,
0
)
}
if
inst
.
High
!=
nil
{
high
=
g
.
getValue
(
inst
.
High
)
}
else
{
high
=
cir
.
NewRawExpr
(
r
.
Var
.
CIRString
()
+
".Len()"
,
ctypes
.
Uint64
)
}
s
:=
r
.
Var
.
CIRString
()
+
".Sub("
s
+=
low
.
CIRString
()
+
", "
s
+=
high
.
CIRString
()
+
")"
return
cir
.
NewRawExpr
(
s
,
&
ctypes
.
StringVar
{})
case
*
ctypes
.
RefType
:
switch
t
:=
t
.
Base
.
(
type
)
{
case
*
ctypes
.
Array
:
...
...
@@ -481,7 +521,21 @@ func (g *functionGenerator) genSlice(inst *ssa.Slice) cir.Expr {
return
cir
.
NewRawExpr
(
s
,
ctypes
.
NewSlice
(
t
.
GetElem
()))
case
*
ctypes
.
Slice
:
logger
.
Fatalf
(
"Todo: genSlice(), %T %s"
,
x
.
Type
(),
inst
)
var
low
,
high
cir
.
Expr
if
inst
.
Low
!=
nil
{
low
=
g
.
getValue
(
inst
.
Low
)
}
else
{
low
=
cconstant
.
NewInt
(
ctypes
.
Int64
,
0
)
}
if
inst
.
High
!=
nil
{
high
=
g
.
getValue
(
inst
.
High
)
}
else
{
high
=
cir
.
NewRawExpr
(
x
.
CIRString
()
+
".GetRaw()->Len()"
,
ctypes
.
Uint64
)
}
s
:=
x
.
CIRString
()
+
".GetRaw()->Sub("
s
+=
low
.
CIRString
()
+
", "
s
+=
high
.
CIRString
()
+
")"
return
cir
.
NewRawExpr
(
s
,
ctypes
.
NewSlice
(
t
.
GetElem
()))
default
:
logger
.
Fatalf
(
"Todo: genSlice(), %T %s"
,
x
.
Type
(),
inst
)
...
...
@@ -507,10 +561,15 @@ func (g *functionGenerator) genBuiltin(call *ssa.CallCommon) cir.Expr {
}
arg
:=
g
.
getValue
(
arg
)
switch
arg
.
Type
()
.
(
type
)
{
case
*
ctypes
.
String
Type
:
arg_type
=
append
(
arg_type
,
&
ctypes
.
String
Type
{})
case
*
ctypes
.
String
Lit
:
arg_type
=
append
(
arg_type
,
&
ctypes
.
String
Lit
{})
f
+=
"%s"
args
=
append
(
args
,
cir
.
NewRawExpr
(
arg
.
CIRString
()
+
".c_str()"
,
&
ctypes
.
StringType
{}))
args
=
append
(
args
,
cir
.
NewRawExpr
(
arg
.
CIRString
(),
&
ctypes
.
StringLit
{}))
case
*
ctypes
.
StringVar
:
arg_type
=
append
(
arg_type
,
&
ctypes
.
StringVar
{})
f
+=
"%s"
args
=
append
(
args
,
cir
.
NewRawExpr
(
arg
.
CIRString
()
+
".GetRaw()"
,
&
ctypes
.
StringVar
{}))
case
*
ctypes
.
BoolType
:
case
*
ctypes
.
IntType
:
...
...
@@ -531,45 +590,56 @@ func (g *functionGenerator) genBuiltin(call *ssa.CallCommon) cir.Expr {
f
+=
"
\\
n"
}
args
[
0
]
=
cconstant
.
NewString
(
f
)
args
[
0
]
=
cconstant
.
NewString
Lit
(
f
)
fn_type
:=
ctypes
.
NewFuncType
(
ctypes
.
Void
,
arg_type
)
fn
:=
cir
.
NewVar
(
"printf"
,
fn_type
)
expr
:=
cir
.
NewCallExpr
(
fn
,
args
)
f
unctio
n
:=
cir
.
NewVar
(
"printf"
,
fn_type
)
expr
:=
cir
.
NewCallExpr
(
f
unctio
n
,
args
)
return
expr
case
"append"
:
var
args
[]
cir
.
Expr
var
args_type
[]
ctypes
.
Type
for
_
,
arg
:=
range
call
.
Args
{
arg
:=
g
.
getValue
(
arg
)
args
=
append
(
args
,
arg
)
args_type
=
append
(
args_type
,
arg
.
Type
())
}
fn_type
:=
ctypes
.
NewFuncType
(
cir
.
ToCType
(
call
.
Value
.
(
*
ssa
.
Builtin
)
.
Type
()
.
(
*
types
.
Signature
)
.
Results
()),
args_type
)
function
:=
cir
.
NewVar
(
"$wartc::Append"
,
fn_type
)
return
cir
.
NewCallExpr
(
function
,
args
)
}
logger
.
Fatal
(
"Todo:"
,
call
.
Value
)
return
nil
}
func
(
g
*
functionGenerator
)
genPrint
(
v
cir
.
Expr
)
cir
.
Expr
{
var
args
[]
cir
.
Expr
var
fn_type
ctypes
.
Type
switch
v
.
Type
()
.
(
type
)
{
case
*
ctypes
.
StringType
:
args
=
append
(
args
,
cconstant
.
NewString
(
"%s"
))
args
=
append
(
args
,
v
)
fn_type
=
ctypes
.
NewFuncType
(
ctypes
.
Void
,
[]
ctypes
.
Type
{
&
ctypes
.
StringType
{},
&
ctypes
.
StringType
{}})
case
*
ctypes
.
BoolType
:
case
*
ctypes
.
IntType
:
args
=
append
(
args
,
cconstant
.
NewString
(
"%d"
))
args
=
append
(
args
,
v
)
fn_type
=
ctypes
.
NewFuncType
(
ctypes
.
Void
,
[]
ctypes
.
Type
{
&
ctypes
.
StringType
{},
ctypes
.
Int64
})
case
*
ctypes
.
FloatType
:
args
=
append
(
args
,
cconstant
.
NewString
(
"%lf"
))
args
=
append
(
args
,
v
)
fn_type
=
ctypes
.
NewFuncType
(
ctypes
.
Void
,
[]
ctypes
.
Type
{
&
ctypes
.
StringType
{},
ctypes
.
Double
})
default
:
logger
.
Fatalf
(
"Todo: print(%s)"
,
v
.
Type
()
.
Name
())
return
nil
}
fn
:=
cir
.
NewVar
(
"printf"
,
fn_type
)
expr
:=
cir
.
NewCallExpr
(
fn
,
args
)
return
expr
}
//
func (g *functionGenerator) genPrint(v cir.Expr) cir.Expr {
//
var args []cir.Expr
//
var fn_type ctypes.Type
//
switch v.Type().(type) {
//
case *ctypes.StringType:
//
args = append(args, cconstant.NewString("%s"))
//
args = append(args, v)
//
fn_type = ctypes.NewFuncType(ctypes.Void, []ctypes.Type{&ctypes.StringType{}, &ctypes.StringType{}})
//
//
case *ctypes.BoolType:
//
case *ctypes.IntType:
//
args = append(args, cconstant.NewString("%d"))
//
args = append(args, v)
//
fn_type = ctypes.NewFuncType(ctypes.Void, []ctypes.Type{&ctypes.StringType{}, ctypes.Int64})
//
//
case *ctypes.FloatType:
//
args = append(args, cconstant.NewString("%lf"))
//
args = append(args, v)
//
fn_type = ctypes.NewFuncType(ctypes.Void, []ctypes.Type{&ctypes.StringType{}, ctypes.Double})
//
//
default:
//
logger.Fatalf("Todo: print(%s)", v.Type().Name())
//
return nil
//
}
//
fn := cir.NewVar("printf", fn_type)
//
expr := cir.NewCallExpr(fn, args)
//
return expr
//
}
func
(
g
*
functionGenerator
)
genPhi
(
inst
*
ssa
.
Phi
)
{
r
:=
&
g
.
compiler
.
curScope
.
AddTempVarDecl
(
g
.
genRegister
(),
cir
.
ToCType
(
inst
.
Type
()))
.
Var
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录