Move string type to runtime in separate file

Этот коммит содержится в:
Ayke van Laethem 2018-08-29 20:55:09 +02:00
родитель bf160d096b
коммит 8f7db8661b
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: E97FF5335DFDFDED
3 изменённых файлов: 23 добавлений и 19 удалений

Просмотреть файл

@ -92,10 +92,6 @@ func NewCompiler(pkgName, triple string, dumpSSA bool) (*Compiler, error) {
c.uintptrType = c.targetData.IntPtrType() c.uintptrType = c.targetData.IntPtrType()
c.i8ptrType = llvm.PointerType(llvm.Int8Type(), 0) c.i8ptrType = llvm.PointerType(llvm.Int8Type(), 0)
// Go string: tuple of (len, ptr)
t := c.ctx.StructCreateNamed("string")
t.StructSetBody([]llvm.Type{c.lenType, c.i8ptrType}, false)
allocType := llvm.FunctionType(c.i8ptrType, []llvm.Type{c.uintptrType}, false) allocType := llvm.FunctionType(c.i8ptrType, []llvm.Type{c.uintptrType}, false)
c.allocFunc = llvm.AddFunction(c.mod, "runtime.alloc", allocType) c.allocFunc = llvm.AddFunction(c.mod, "runtime.alloc", allocType)
@ -458,7 +454,7 @@ func (c *Compiler) getLLVMType(goType types.Type) (llvm.Type, error) {
case types.Int64, types.Uint64: case types.Int64, types.Uint64:
return llvm.Int64Type(), nil return llvm.Int64Type(), nil
case types.String: case types.String:
return c.mod.GetTypeByName("string"), nil return c.mod.GetTypeByName("runtime._string"), nil
case types.Uintptr: case types.Uintptr:
return c.uintptrType, nil return c.uintptrType, nil
case types.UnsafePointer: case types.UnsafePointer:
@ -1889,7 +1885,7 @@ func (c *Compiler) parseBinOp(frame *Frame, binop *ssa.BinOp) (llvm.Value, error
return c.builder.CreateICmp(llvm.IntNE, x, y, ""), nil return c.builder.CreateICmp(llvm.IntNE, x, y, ""), nil
} }
} else if typ.Kind() == types.String { } else if typ.Kind() == types.String {
result := c.builder.CreateCall(c.mod.NamedFunction("runtime.stringequal"), []llvm.Value{x, y}, "") result := c.builder.CreateCall(c.mod.NamedFunction("runtime.stringEqual"), []llvm.Value{x, y}, "")
if binop.Op == token.NEQ { if binop.Op == token.NEQ {
result = c.builder.CreateNot(result, "") result = c.builder.CreateNot(result, "")
} }
@ -1958,7 +1954,7 @@ func (c *Compiler) parseConst(expr *ssa.Const) (llvm.Value, error) {
global.SetGlobalConstant(true) global.SetGlobalConstant(true)
zero := llvm.ConstInt(llvm.Int32Type(), 0, false) zero := llvm.ConstInt(llvm.Int32Type(), 0, false)
strPtr := c.builder.CreateInBoundsGEP(global, []llvm.Value{zero, zero}, "") strPtr := c.builder.CreateInBoundsGEP(global, []llvm.Value{zero, zero}, "")
strObj := llvm.ConstNamedStruct(c.mod.GetTypeByName("string"), []llvm.Value{strLen, strPtr}) strObj := llvm.ConstNamedStruct(c.mod.GetTypeByName("runtime._string"), []llvm.Value{strLen, strPtr})
return strObj, nil return strObj, nil
} else if typ.Kind() == types.UnsafePointer { } else if typ.Kind() == types.UnsafePointer {
if !expr.IsNil() { if !expr.IsNil() {

Просмотреть файл

@ -21,18 +21,6 @@ func GOMAXPROCS(n int) int {
return 1 return 1
} }
func stringequal(x, y string) bool {
if len(x) != len(y) {
return false
}
for i := 0; i < len(x); i++ {
if x[i] != y[i] {
return false
}
}
return true
}
// Copy size bytes from src to dst. The memory areas must not overlap. // Copy size bytes from src to dst. The memory areas must not overlap.
func memcpy(dst, src unsafe.Pointer, size uintptr) { func memcpy(dst, src unsafe.Pointer, size uintptr) {
for i := uintptr(0); i < size; i++ { for i := uintptr(0); i < size; i++ {

20
src/runtime/string.go Обычный файл
Просмотреть файл

@ -0,0 +1,20 @@
package runtime
// This file implements functions related to Go strings.
type _string struct {
length lenType
ptr *uint8
}
func stringEqual(x, y string) bool {
if len(x) != len(y) {
return false
}
for i := 0; i < len(x); i++ {
if x[i] != y[i] {
return false
}
}
return true
}