c.AllocaCStrs; ssa: AllocaU/ArrayAlloca/Times/AllocaCStrs

This commit is contained in:
xushiwei
2024-07-12 21:40:13 +08:00
parent 2d29d1549a
commit e138951e9e
11 changed files with 152 additions and 71 deletions

View File

@@ -143,6 +143,7 @@ func TestErrBuiltin(t *testing.T) {
test("advance", func(ctx *context) { ctx.advance(nil, nil) })
test("alloca", func(ctx *context) { ctx.alloca(nil, nil) })
test("allocaCStr", func(ctx *context) { ctx.allocaCStr(nil, nil) })
test("allocaCStrs", func(ctx *context) { ctx.allocaCStrs(nil, nil) })
test("string", func(ctx *context) { ctx.string(nil, nil) })
test("stringData", func(ctx *context) { ctx.stringData(nil, nil) })
test("funcAddr", func(ctx *context) { ctx.funcAddr(nil, nil) })

View File

@@ -381,17 +381,19 @@ const (
llgoCstr = llgoInstrBase + 1
llgoAlloca = llgoInstrBase + 2
llgoAllocaCStr = llgoInstrBase + 3
llgoAdvance = llgoInstrBase + 4
llgoIndex = llgoInstrBase + 5
llgoDeferData = llgoInstrBase + 6
llgoAllocaCStrs = llgoInstrBase + 4
llgoAdvance = llgoInstrBase + 5
llgoIndex = llgoInstrBase + 6
llgoStringData = llgoInstrBase + 7
llgoString = llgoInstrBase + 8
llgoFuncAddr = llgoInstrBase + 9
llgoDeferData = llgoInstrBase + 9
llgoSigjmpbuf = llgoInstrBase + 0xa
llgoSigsetjmp = llgoInstrBase + 0xb
llgoSiglongjmp = llgoInstrBase + 0xc
llgoFuncAddr = llgoInstrBase + 0xd
llgoPyList = llgoInstrBase + 0x10
llgoPyStr = llgoInstrBase + 0x11

View File

@@ -28,14 +28,29 @@ import (
// -----------------------------------------------------------------------------
func constStr(v ssa.Value) (ret string, ok bool) {
if c, ok := v.(*ssa.Const); ok {
if v := c.Value; v.Kind() == constant.String {
return constant.StringVal(v), true
}
}
return
}
func constBool(v ssa.Value) (ret bool, ok bool) {
if c, ok := v.(*ssa.Const); ok {
if v := c.Value; v.Kind() == constant.Bool {
return constant.BoolVal(v), true
}
}
return
}
// func pystr(string) *py.Object
func pystr(b llssa.Builder, args []ssa.Value) (ret llssa.Expr) {
if len(args) == 1 {
if c, ok := args[0].(*ssa.Const); ok {
if v := c.Value; v.Kind() == constant.String {
sv := constant.StringVal(v)
return b.PyStr(sv)
}
if sv, ok := constStr(args[0]); ok {
return b.PyStr(sv)
}
}
panic("pystr(<string-literal>): invalid arguments")
@@ -44,11 +59,8 @@ func pystr(b llssa.Builder, args []ssa.Value) (ret llssa.Expr) {
// func cstr(string) *int8
func cstr(b llssa.Builder, args []ssa.Value) (ret llssa.Expr) {
if len(args) == 1 {
if c, ok := args[0].(*ssa.Const); ok {
if v := c.Value; v.Kind() == constant.String {
sv := constant.StringVal(v)
return b.CStr(sv)
}
if sv, ok := constStr(args[0]); ok {
return b.CStr(sv)
}
}
panic("cstr(<string-literal>): invalid arguments")
@@ -87,6 +99,19 @@ func (p *context) allocaCStr(b llssa.Builder, args []ssa.Value) (ret llssa.Expr)
panic("allocaCStr(s string): invalid arguments")
}
// func allocaCStrs(strs []string, endWithNil bool) **int8
func (p *context) allocaCStrs(b llssa.Builder, args []ssa.Value) (ret llssa.Expr) {
if len(args) == 2 {
endWithNil, ok := constBool(args[1])
if !ok {
panic("allocaCStrs(strs, endWithNil): endWithNil should be constant bool")
}
strs := p.compileValue(b, args[0])
return b.AllocaCStrs(strs, endWithNil)
}
panic("allocaCStrs(strs []string, endWithNil bool): invalid arguments")
}
// func string(cstr *int8, n ...int) *int8
func (p *context) string(b llssa.Builder, args []ssa.Value) (ret llssa.Expr) {
if len(args) == 2 {
@@ -185,6 +210,7 @@ var llgoInstrs = map[string]int{
"index": llgoIndex,
"alloca": llgoAlloca,
"allocaCStr": llgoAllocaCStr,
"allocaCStrs": llgoAllocaCStrs,
"string": llgoString,
"stringData": llgoStringData,
"funcAddr": llgoFuncAddr,
@@ -340,6 +366,8 @@ func (p *context) call(b llssa.Builder, act llssa.DoAction, call *ssa.CallCommon
ret = p.alloca(b, args)
case llgoAllocaCStr:
ret = p.allocaCStr(b, args)
case llgoAllocaCStrs:
ret = p.allocaCStrs(b, args)
case llgoString:
ret = p.string(b, args)
case llgoStringData: