llgo/ssa: allocaCStr; runtime: String

This commit is contained in:
xushiwei
2024-04-30 18:22:56 +08:00
parent c6cb2931e1
commit ae0906d322
9 changed files with 164 additions and 4 deletions

View File

@@ -275,6 +275,7 @@ func (p *context) checkVArgs(v *ssa.Alloc, t *types.Pointer) bool {
return false
}
// 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 {
@@ -287,6 +288,7 @@ func cstr(b llssa.Builder, args []ssa.Value) (ret llssa.Expr) {
panic("cstr(<string-literal>): invalid arguments")
}
// func alloca(size uintptr) unsafe.Pointer
func (p *context) alloca(b llssa.Builder, args []ssa.Value) (ret llssa.Expr) {
if len(args) == 1 {
n := p.compileValue(b, args[0])
@@ -295,6 +297,15 @@ func (p *context) alloca(b llssa.Builder, args []ssa.Value) (ret llssa.Expr) {
panic("alloca(size uintptr): invalid arguments")
}
// func allocaCStr(s string) *int8
func (p *context) allocaCStr(b llssa.Builder, args []ssa.Value) (ret llssa.Expr) {
if len(args) == 1 {
s := p.compileValue(b, args[0])
return b.AllocaCStr(s)
}
panic("allocaCStr(s string): invalid arguments")
}
func (p *context) compileInstrOrValue(b llssa.Builder, iv instrOrValue, asValue bool) (ret llssa.Expr) {
if asValue {
if v, ok := p.bvals[iv]; ok {
@@ -334,7 +345,9 @@ func (p *context) compileInstrOrValue(b llssa.Builder, iv instrOrValue, asValue
ret = cstr(b, call.Args)
case llgoAlloca:
ret = p.alloca(b, call.Args)
case llgoUnreachable:
case llgoAllocaCStr:
ret = p.allocaCStr(b, call.Args)
case llgoUnreachable: // func unreachable()
b.Unreachable()
default:
panic("todo")