Merge pull request #85 from xushiwei/q
llgo/ssa: allocaCStr; runtime: String
This commit is contained in:
13
cl/_testrt/allocstr/in.go
Normal file
13
cl/_testrt/allocstr/in.go
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/goplus/llgo/internal/runtime/c"
|
||||||
|
)
|
||||||
|
|
||||||
|
func hello() string {
|
||||||
|
return "Hello world\n"
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
c.Printf(c.AllocaCStr(hello()))
|
||||||
|
}
|
||||||
47
cl/_testrt/allocstr/out.ll
Normal file
47
cl/_testrt/allocstr/out.ll
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
; ModuleID = 'main'
|
||||||
|
source_filename = "main"
|
||||||
|
|
||||||
|
%"github.com/goplus/llgo/internal/runtime.String" = type { ptr, i64 }
|
||||||
|
%"github.com/goplus/llgo/internal/runtime.Slice" = type { ptr, i64, i64 }
|
||||||
|
|
||||||
|
@"main.init$guard" = global ptr null
|
||||||
|
@0 = private unnamed_addr constant [13 x i8] c"Hello world\0A\00", align 1
|
||||||
|
|
||||||
|
define %"github.com/goplus/llgo/internal/runtime.String" @main.hello() {
|
||||||
|
_llgo_0:
|
||||||
|
%0 = call %"github.com/goplus/llgo/internal/runtime.String" @"github.com/goplus/llgo/internal/runtime.NewString"(ptr @0, i64 12)
|
||||||
|
ret %"github.com/goplus/llgo/internal/runtime.String" %0
|
||||||
|
}
|
||||||
|
|
||||||
|
define void @main.init() {
|
||||||
|
_llgo_0:
|
||||||
|
%0 = load i1, ptr @"main.init$guard", align 1
|
||||||
|
br i1 %0, label %_llgo_2, label %_llgo_1
|
||||||
|
|
||||||
|
_llgo_1: ; preds = %_llgo_0
|
||||||
|
store i1 true, ptr @"main.init$guard", align 1
|
||||||
|
br label %_llgo_2
|
||||||
|
|
||||||
|
_llgo_2: ; preds = %_llgo_1, %_llgo_0
|
||||||
|
ret void
|
||||||
|
}
|
||||||
|
|
||||||
|
define void @main() {
|
||||||
|
_llgo_0:
|
||||||
|
call void @main.init()
|
||||||
|
%0 = call %"github.com/goplus/llgo/internal/runtime.String" @main.hello()
|
||||||
|
%1 = call i64 @"github.com/goplus/llgo/internal/runtime.StringLen"(%"github.com/goplus/llgo/internal/runtime.String" %0)
|
||||||
|
%2 = add i64 %1, 1
|
||||||
|
%3 = alloca i8, i64 %2, align 1
|
||||||
|
%4 = call ptr @"github.com/goplus/llgo/internal/runtime.CStrCopy"(ptr %3, %"github.com/goplus/llgo/internal/runtime.String" %0)
|
||||||
|
%5 = call i32 (ptr, ...) @printf(ptr %4)
|
||||||
|
ret void
|
||||||
|
}
|
||||||
|
|
||||||
|
declare %"github.com/goplus/llgo/internal/runtime.String" @"github.com/goplus/llgo/internal/runtime.NewString"(ptr, i64)
|
||||||
|
|
||||||
|
declare i64 @"github.com/goplus/llgo/internal/runtime.StringLen"(%"github.com/goplus/llgo/internal/runtime.Slice")
|
||||||
|
|
||||||
|
declare ptr @"github.com/goplus/llgo/internal/runtime.CStrCopy"(ptr, %"github.com/goplus/llgo/internal/runtime.String")
|
||||||
|
|
||||||
|
declare i32 @printf(ptr, ...)
|
||||||
@@ -275,6 +275,7 @@ func (p *context) checkVArgs(v *ssa.Alloc, t *types.Pointer) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// func cstr(string) *int8
|
||||||
func cstr(b llssa.Builder, args []ssa.Value) (ret llssa.Expr) {
|
func cstr(b llssa.Builder, args []ssa.Value) (ret llssa.Expr) {
|
||||||
if len(args) == 1 {
|
if len(args) == 1 {
|
||||||
if c, ok := args[0].(*ssa.Const); ok {
|
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")
|
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) {
|
func (p *context) alloca(b llssa.Builder, args []ssa.Value) (ret llssa.Expr) {
|
||||||
if len(args) == 1 {
|
if len(args) == 1 {
|
||||||
n := p.compileValue(b, args[0])
|
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")
|
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) {
|
func (p *context) compileInstrOrValue(b llssa.Builder, iv instrOrValue, asValue bool) (ret llssa.Expr) {
|
||||||
if asValue {
|
if asValue {
|
||||||
if v, ok := p.bvals[iv]; ok {
|
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)
|
ret = cstr(b, call.Args)
|
||||||
case llgoAlloca:
|
case llgoAlloca:
|
||||||
ret = p.alloca(b, call.Args)
|
ret = p.alloca(b, call.Args)
|
||||||
case llgoUnreachable:
|
case llgoAllocaCStr:
|
||||||
|
ret = p.allocaCStr(b, call.Args)
|
||||||
|
case llgoUnreachable: // func unreachable()
|
||||||
b.Unreachable()
|
b.Unreachable()
|
||||||
default:
|
default:
|
||||||
panic("todo")
|
panic("todo")
|
||||||
|
|||||||
@@ -28,8 +28,8 @@ func testCompile(t *testing.T, src, expected string) {
|
|||||||
cltest.TestCompileEx(t, src, "foo.go", expected)
|
cltest.TestCompileEx(t, src, "foo.go", expected)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestFromTestcgo(t *testing.T) {
|
func TestFromTestrt(t *testing.T) {
|
||||||
cltest.FromDir(t, "", "./_testcgo", true)
|
cltest.FromDir(t, "", "./_testrt", true)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestFromTestdata(t *testing.T) {
|
func TestFromTestdata(t *testing.T) {
|
||||||
|
|||||||
@@ -178,9 +178,10 @@ const (
|
|||||||
llgoInstr = -1
|
llgoInstr = -1
|
||||||
|
|
||||||
llgoInstrBase = 0x80
|
llgoInstrBase = 0x80
|
||||||
|
llgoUnreachable = llgoInstrBase + 0
|
||||||
llgoCstr = llgoInstrBase + 1
|
llgoCstr = llgoInstrBase + 1
|
||||||
llgoAlloca = llgoInstrBase + 2
|
llgoAlloca = llgoInstrBase + 2
|
||||||
llgoUnreachable = llgoInstrBase + 3
|
llgoAllocaCStr = llgoInstrBase + 3
|
||||||
)
|
)
|
||||||
|
|
||||||
func (p *context) funcName(pkg *types.Package, fn *ssa.Function, ignore bool) (string, int) {
|
func (p *context) funcName(pkg *types.Package, fn *ssa.Function, ignore bool) (string, int) {
|
||||||
@@ -212,6 +213,8 @@ func (p *context) funcOf(fn *ssa.Function) (ret llssa.Function, ftype int) {
|
|||||||
ftype = llgoCstr
|
ftype = llgoCstr
|
||||||
case "alloca":
|
case "alloca":
|
||||||
ftype = llgoAlloca
|
ftype = llgoAlloca
|
||||||
|
case "allocaCStr":
|
||||||
|
ftype = llgoAllocaCStr
|
||||||
case "unreachable":
|
case "unreachable":
|
||||||
ftype = llgoUnreachable
|
ftype = llgoUnreachable
|
||||||
default:
|
default:
|
||||||
|
|||||||
@@ -29,6 +29,9 @@ func Str(string) *int8
|
|||||||
//go:linkname Alloca llgo.alloca
|
//go:linkname Alloca llgo.alloca
|
||||||
func Alloca(size uintptr) unsafe.Pointer
|
func Alloca(size uintptr) unsafe.Pointer
|
||||||
|
|
||||||
|
//go:linkname AllocaCStr llgo.allocaCStr
|
||||||
|
func AllocaCStr(s string) *int8
|
||||||
|
|
||||||
//go:linkname Unreachable llgo.unreachable
|
//go:linkname Unreachable llgo.unreachable
|
||||||
func Unreachable()
|
func Unreachable()
|
||||||
|
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
; ModuleID = 'github.com/goplus/llgo/internal/runtime'
|
; ModuleID = 'github.com/goplus/llgo/internal/runtime'
|
||||||
source_filename = "github.com/goplus/llgo/internal/runtime"
|
source_filename = "github.com/goplus/llgo/internal/runtime"
|
||||||
|
|
||||||
|
%"github.com/goplus/llgo/internal/runtime.String" = type { ptr, i64 }
|
||||||
%"github.com/goplus/llgo/internal/runtime.iface" = type { ptr, ptr }
|
%"github.com/goplus/llgo/internal/runtime.iface" = type { ptr, ptr }
|
||||||
%"github.com/goplus/llgo/internal/runtime.itab" = type { ptr, ptr, i32, [4 x i8], [1 x i64] }
|
%"github.com/goplus/llgo/internal/runtime.itab" = type { ptr, ptr, i32, [4 x i8], [1 x i64] }
|
||||||
%"github.com/goplus/llgo/internal/runtime.String" = type { ptr, i64 }
|
|
||||||
%"github.com/goplus/llgo/internal/runtime.Slice" = type { ptr, i64, i64 }
|
%"github.com/goplus/llgo/internal/runtime.Slice" = type { ptr, i64, i64 }
|
||||||
%"github.com/goplus/llgo/internal/abi.Type" = type { i64, i64, i32, i8, i8, i8, i8, ptr, ptr, i32, i32 }
|
%"github.com/goplus/llgo/internal/abi.Type" = type { i64, i64, i32, i8, i8, i8, i8, ptr, ptr, i32, i32 }
|
||||||
|
|
||||||
@@ -11,6 +11,7 @@ source_filename = "github.com/goplus/llgo/internal/runtime"
|
|||||||
@"github.com/goplus/llgo/internal/runtime.basicTypes" = global ptr null
|
@"github.com/goplus/llgo/internal/runtime.basicTypes" = global ptr null
|
||||||
@"github.com/goplus/llgo/internal/runtime.init$guard" = global ptr null
|
@"github.com/goplus/llgo/internal/runtime.init$guard" = global ptr null
|
||||||
@"github.com/goplus/llgo/internal/runtime.sizeBasicTypes" = global ptr null
|
@"github.com/goplus/llgo/internal/runtime.sizeBasicTypes" = global ptr null
|
||||||
|
@0 = private unnamed_addr constant [21 x i8] c"I2Int: type mismatch\00", align 1
|
||||||
|
|
||||||
define ptr @"github.com/goplus/llgo/internal/runtime.Alloc"(i64 %0) {
|
define ptr @"github.com/goplus/llgo/internal/runtime.Alloc"(i64 %0) {
|
||||||
_llgo_0:
|
_llgo_0:
|
||||||
@@ -25,6 +26,33 @@ _llgo_0:
|
|||||||
ret ptr %2
|
ret ptr %2
|
||||||
}
|
}
|
||||||
|
|
||||||
|
define ptr @"github.com/goplus/llgo/internal/runtime.CStrCopy"(ptr %0, %"github.com/goplus/llgo/internal/runtime.String" %1) {
|
||||||
|
_llgo_0:
|
||||||
|
%2 = alloca %"github.com/goplus/llgo/internal/runtime.String", align 8
|
||||||
|
store %"github.com/goplus/llgo/internal/runtime.String" %1, ptr %2, align 8
|
||||||
|
%3 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.String", ptr %2, i32 0, i32 1
|
||||||
|
%4 = load i64, ptr %3, align 4
|
||||||
|
%5 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.String", ptr %2, i32 0, i32 0
|
||||||
|
%6 = load ptr, ptr %5, align 8
|
||||||
|
%7 = call ptr @memcpy(ptr %0, ptr %6, i64 %4)
|
||||||
|
%8 = getelementptr inbounds i8, ptr %0, i64 %4
|
||||||
|
store i8 0, ptr %8, align 1
|
||||||
|
ret ptr %0
|
||||||
|
}
|
||||||
|
|
||||||
|
define ptr @"github.com/goplus/llgo/internal/runtime.CStrDup"(%"github.com/goplus/llgo/internal/runtime.String" %0) {
|
||||||
|
_llgo_0:
|
||||||
|
%1 = alloca %"github.com/goplus/llgo/internal/runtime.String", align 8
|
||||||
|
store %"github.com/goplus/llgo/internal/runtime.String" %0, ptr %1, align 8
|
||||||
|
%2 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.String", ptr %1, i32 0, i32 1
|
||||||
|
%3 = load i64, ptr %2, align 4
|
||||||
|
%4 = add i64 %3, 1
|
||||||
|
%5 = call ptr @"github.com/goplus/llgo/internal/runtime.Alloc"(i64 %4)
|
||||||
|
%6 = load %"github.com/goplus/llgo/internal/runtime.String", ptr %1, align 8
|
||||||
|
%7 = call ptr @"github.com/goplus/llgo/internal/runtime.CStrCopy"(ptr %5, %"github.com/goplus/llgo/internal/runtime.String" %6)
|
||||||
|
ret ptr %7
|
||||||
|
}
|
||||||
|
|
||||||
define { i64, i1 } @"github.com/goplus/llgo/internal/runtime.CheckI2Int"(%"github.com/goplus/llgo/internal/runtime.iface" %0, ptr %1) {
|
define { i64, i1 } @"github.com/goplus/llgo/internal/runtime.CheckI2Int"(%"github.com/goplus/llgo/internal/runtime.iface" %0, ptr %1) {
|
||||||
_llgo_0:
|
_llgo_0:
|
||||||
%2 = alloca %"github.com/goplus/llgo/internal/runtime.iface", align 8
|
%2 = alloca %"github.com/goplus/llgo/internal/runtime.iface", align 8
|
||||||
@@ -77,7 +105,8 @@ _llgo_1: ; preds = %_llgo_0
|
|||||||
ret i64 %10
|
ret i64 %10
|
||||||
|
|
||||||
_llgo_2: ; preds = %_llgo_0
|
_llgo_2: ; preds = %_llgo_0
|
||||||
%11 = call %"github.com/goplus/llgo/internal/runtime.iface" @"github.com/goplus/llgo/internal/runtime.MakeAnyString"([21 x i8] c"I2Int: type mismatch\00")
|
%11 = call %"github.com/goplus/llgo/internal/runtime.String" @"github.com/goplus/llgo/internal/runtime.NewString"(ptr @0, i64 20)
|
||||||
|
%12 = call %"github.com/goplus/llgo/internal/runtime.iface" @"github.com/goplus/llgo/internal/runtime.MakeAnyString"(%"github.com/goplus/llgo/internal/runtime.String" %11)
|
||||||
unreachable
|
unreachable
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -185,6 +214,17 @@ _llgo_0:
|
|||||||
ret %"github.com/goplus/llgo/internal/runtime.Slice" %7
|
ret %"github.com/goplus/llgo/internal/runtime.Slice" %7
|
||||||
}
|
}
|
||||||
|
|
||||||
|
define %"github.com/goplus/llgo/internal/runtime.String" @"github.com/goplus/llgo/internal/runtime.NewString"(ptr %0, i64 %1) {
|
||||||
|
_llgo_0:
|
||||||
|
%2 = alloca %"github.com/goplus/llgo/internal/runtime.String", align 8
|
||||||
|
%3 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.String", ptr %2, i32 0, i32 0
|
||||||
|
%4 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.String", ptr %2, i32 0, i32 1
|
||||||
|
store ptr %0, ptr %3, align 8
|
||||||
|
store i64 %1, ptr %4, align 4
|
||||||
|
%5 = load %"github.com/goplus/llgo/internal/runtime.String", ptr %2, align 8
|
||||||
|
ret %"github.com/goplus/llgo/internal/runtime.String" %5
|
||||||
|
}
|
||||||
|
|
||||||
define %"github.com/goplus/llgo/internal/runtime.Slice" @"github.com/goplus/llgo/internal/runtime.NilSlice"() {
|
define %"github.com/goplus/llgo/internal/runtime.Slice" @"github.com/goplus/llgo/internal/runtime.NilSlice"() {
|
||||||
_llgo_0:
|
_llgo_0:
|
||||||
%0 = alloca %"github.com/goplus/llgo/internal/runtime.Slice", align 8
|
%0 = alloca %"github.com/goplus/llgo/internal/runtime.Slice", align 8
|
||||||
@@ -216,6 +256,24 @@ _llgo_0:
|
|||||||
ret i64 %3
|
ret i64 %3
|
||||||
}
|
}
|
||||||
|
|
||||||
|
define ptr @"github.com/goplus/llgo/internal/runtime.StringData"(%"github.com/goplus/llgo/internal/runtime.String" %0) {
|
||||||
|
_llgo_0:
|
||||||
|
%1 = alloca %"github.com/goplus/llgo/internal/runtime.String", align 8
|
||||||
|
store %"github.com/goplus/llgo/internal/runtime.String" %0, ptr %1, align 8
|
||||||
|
%2 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.String", ptr %1, i32 0, i32 0
|
||||||
|
%3 = load ptr, ptr %2, align 8
|
||||||
|
ret ptr %3
|
||||||
|
}
|
||||||
|
|
||||||
|
define i64 @"github.com/goplus/llgo/internal/runtime.StringLen"(%"github.com/goplus/llgo/internal/runtime.Slice" %0) {
|
||||||
|
_llgo_0:
|
||||||
|
%1 = alloca %"github.com/goplus/llgo/internal/runtime.Slice", align 8
|
||||||
|
store %"github.com/goplus/llgo/internal/runtime.Slice" %0, ptr %1, align 8
|
||||||
|
%2 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.Slice", ptr %1, i32 0, i32 1
|
||||||
|
%3 = load i64, ptr %2, align 4
|
||||||
|
ret i64 %3
|
||||||
|
}
|
||||||
|
|
||||||
define ptr @"github.com/goplus/llgo/internal/runtime.basicType"(i64 %0) {
|
define ptr @"github.com/goplus/llgo/internal/runtime.basicType"(i64 %0) {
|
||||||
_llgo_0:
|
_llgo_0:
|
||||||
%1 = call ptr @"github.com/goplus/llgo/internal/runtime.Alloc"(i64 16)
|
%1 = call ptr @"github.com/goplus/llgo/internal/runtime.Alloc"(i64 16)
|
||||||
@@ -298,4 +356,6 @@ _llgo_2: ; preds = %_llgo_1, %_llgo_0
|
|||||||
|
|
||||||
declare ptr @malloc(i64)
|
declare ptr @malloc(i64)
|
||||||
|
|
||||||
|
declare ptr @memcpy(ptr, ptr, i64)
|
||||||
|
|
||||||
declare void @"github.com/goplus/llgo/internal/abi.init"()
|
declare void @"github.com/goplus/llgo/internal/abi.init"()
|
||||||
|
|||||||
@@ -18,6 +18,8 @@ package runtime
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"unsafe"
|
"unsafe"
|
||||||
|
|
||||||
|
"github.com/goplus/llgo/internal/runtime/c"
|
||||||
)
|
)
|
||||||
|
|
||||||
// -----------------------------------------------------------------------------
|
// -----------------------------------------------------------------------------
|
||||||
@@ -38,4 +40,35 @@ func EmptyString() String {
|
|||||||
return String{nil, 0}
|
return String{nil, 0}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewString creates a new string.
|
||||||
|
func NewString(data unsafe.Pointer, len int) String {
|
||||||
|
return String{data, len}
|
||||||
|
}
|
||||||
|
|
||||||
|
// StringLen returns the length of a string.
|
||||||
|
func StringLen(s Slice) int {
|
||||||
|
return s.len
|
||||||
|
}
|
||||||
|
|
||||||
|
// StringData returns the data pointer of a string.
|
||||||
|
func StringData(s String) unsafe.Pointer {
|
||||||
|
return s.data
|
||||||
|
}
|
||||||
|
|
||||||
|
// -----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
// CStrCopy copies a Go string to a C string buffer and returns it.
|
||||||
|
func CStrCopy(dest unsafe.Pointer, s String) *int8 {
|
||||||
|
n := s.len
|
||||||
|
c.Memcpy(dest, s.data, uintptr(n))
|
||||||
|
arr := (*[1 << 30]int8)(dest)
|
||||||
|
arr[n] = 0
|
||||||
|
return (*int8)(dest)
|
||||||
|
}
|
||||||
|
|
||||||
|
func CStrDup(s String) *int8 {
|
||||||
|
dest := Alloc(uintptr(s.len + 1))
|
||||||
|
return CStrCopy(dest, s)
|
||||||
|
}
|
||||||
|
|
||||||
// -----------------------------------------------------------------------------
|
// -----------------------------------------------------------------------------
|
||||||
|
|||||||
@@ -22,8 +22,8 @@ import (
|
|||||||
"github.com/goplus/llgo/cl/cltest"
|
"github.com/goplus/llgo/cl/cltest"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestFromTestcgo(t *testing.T) {
|
func TestFromTestrt(t *testing.T) {
|
||||||
cltest.FromDir(t, "", "../cl/_testcgo", true)
|
cltest.FromDir(t, "", "../cl/_testrt", true)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestFromTestdata(t *testing.T) {
|
func TestFromTestdata(t *testing.T) {
|
||||||
|
|||||||
31
ssa/expr.go
31
ssa/expr.go
@@ -77,14 +77,6 @@ func (p Program) Null(t Type) Expr {
|
|||||||
return Expr{llvm.ConstNull(t.ll), t}
|
return Expr{llvm.ConstNull(t.ll), t}
|
||||||
}
|
}
|
||||||
|
|
||||||
// StringVal returns string constant expression.
|
|
||||||
func (p Program) StringVal(v string) Expr {
|
|
||||||
t := p.String()
|
|
||||||
cstr := llvm.ConstString(v, true)
|
|
||||||
// TODO(xsw): cstr => gostring
|
|
||||||
return Expr{cstr, t}
|
|
||||||
}
|
|
||||||
|
|
||||||
// BoolVal returns a boolean constant expression.
|
// BoolVal returns a boolean constant expression.
|
||||||
func (p Program) BoolVal(v bool) Expr {
|
func (p Program) BoolVal(v bool) Expr {
|
||||||
t := p.Bool()
|
t := p.Bool()
|
||||||
@@ -149,7 +141,7 @@ func (b Builder) Const(v constant.Value, typ Type) Expr {
|
|||||||
return prog.FloatVal(v, typ)
|
return prog.FloatVal(v, typ)
|
||||||
}
|
}
|
||||||
case kind == types.String:
|
case kind == types.String:
|
||||||
return prog.StringVal(constant.StringVal(v))
|
return b.Str(constant.StringVal(v))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
panic(fmt.Sprintf("unsupported Const: %v, %v", v, typ.t))
|
panic(fmt.Sprintf("unsupported Const: %v, %v", v, typ.t))
|
||||||
@@ -160,6 +152,15 @@ func (b Builder) CStr(v string) Expr {
|
|||||||
return Expr{llvm.CreateGlobalStringPtr(b.impl, v), b.Prog.CStr()}
|
return Expr{llvm.CreateGlobalStringPtr(b.impl, v), b.Prog.CStr()}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Str returns a Go string constant expression.
|
||||||
|
func (b Builder) Str(v string) (ret Expr) {
|
||||||
|
prog := b.Prog
|
||||||
|
cstr := b.CStr(v)
|
||||||
|
ret = b.InlineCall(b.fn.pkg.rtFunc("NewString"), cstr, prog.Val(len(v)))
|
||||||
|
ret.Type = prog.String()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// -----------------------------------------------------------------------------
|
// -----------------------------------------------------------------------------
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -537,6 +538,18 @@ func (b Builder) ArrayAlloca(telem Type, n Expr) (ret Expr) {
|
|||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
// AllocaCStr allocates space for copy it from a Go string.
|
||||||
|
func (b Builder) AllocaCStr(gostr Expr) (ret Expr) {
|
||||||
|
if debugInstr {
|
||||||
|
log.Printf("AllocaCStr %v\n", gostr.impl)
|
||||||
|
}
|
||||||
|
pkg := b.fn.pkg
|
||||||
|
n := b.InlineCall(pkg.rtFunc("StringLen"), gostr)
|
||||||
|
n1 := b.BinOp(token.ADD, n, b.Prog.Val(1))
|
||||||
|
cstr := b.Alloca(n1)
|
||||||
|
return b.InlineCall(pkg.rtFunc("CStrCopy"), cstr, gostr)
|
||||||
|
}
|
||||||
|
|
||||||
// -----------------------------------------------------------------------------
|
// -----------------------------------------------------------------------------
|
||||||
|
|
||||||
// The ChangeType instruction applies to X a value-preserving type
|
// The ChangeType instruction applies to X a value-preserving type
|
||||||
|
|||||||
Reference in New Issue
Block a user