Skip to content

Commit fd20f63

Browse files
aykevldeadprogram
authored andcommitted
compiler: move createConst to compilerContext
Move it from *builder to *compilerContext, so that it can be called in more places. This is necessary to create a string value (for the file name) in createEmbedGlobal.
1 parent 9dd249a commit fd20f63

File tree

1 file changed

+33
-34
lines changed

1 file changed

+33
-34
lines changed

compiler/compiler.go

Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2470,42 +2470,41 @@ func (b *builder) createBinOp(op token.Token, typ, ytyp types.Type, x, y llvm.Va
24702470
}
24712471

24722472
// createConst creates a LLVM constant value from a Go constant.
2473-
func (b *builder) createConst(expr *ssa.Const) llvm.Value {
2473+
func (c *compilerContext) createConst(expr *ssa.Const) llvm.Value {
24742474
switch typ := expr.Type().Underlying().(type) {
24752475
case *types.Basic:
2476-
llvmType := b.getLLVMType(typ)
2476+
llvmType := c.getLLVMType(typ)
24772477
if typ.Info()&types.IsBoolean != 0 {
2478-
b := constant.BoolVal(expr.Value)
24792478
n := uint64(0)
2480-
if b {
2479+
if constant.BoolVal(expr.Value) {
24812480
n = 1
24822481
}
24832482
return llvm.ConstInt(llvmType, n, false)
24842483
} else if typ.Info()&types.IsString != 0 {
24852484
str := constant.StringVal(expr.Value)
2486-
strLen := llvm.ConstInt(b.uintptrType, uint64(len(str)), false)
2485+
strLen := llvm.ConstInt(c.uintptrType, uint64(len(str)), false)
24872486
var strPtr llvm.Value
24882487
if str != "" {
2489-
objname := b.pkg.Path() + "$string"
2490-
global := llvm.AddGlobal(b.mod, llvm.ArrayType(b.ctx.Int8Type(), len(str)), objname)
2491-
global.SetInitializer(b.ctx.ConstString(str, false))
2488+
objname := c.pkg.Path() + "$string"
2489+
global := llvm.AddGlobal(c.mod, llvm.ArrayType(c.ctx.Int8Type(), len(str)), objname)
2490+
global.SetInitializer(c.ctx.ConstString(str, false))
24922491
global.SetLinkage(llvm.InternalLinkage)
24932492
global.SetGlobalConstant(true)
24942493
global.SetUnnamedAddr(true)
24952494
global.SetAlignment(1)
2496-
zero := llvm.ConstInt(b.ctx.Int32Type(), 0, false)
2497-
strPtr = b.CreateInBoundsGEP(global, []llvm.Value{zero, zero}, "")
2495+
zero := llvm.ConstInt(c.ctx.Int32Type(), 0, false)
2496+
strPtr = llvm.ConstInBoundsGEP(global, []llvm.Value{zero, zero})
24982497
} else {
2499-
strPtr = llvm.ConstNull(b.i8ptrType)
2498+
strPtr = llvm.ConstNull(c.i8ptrType)
25002499
}
2501-
strObj := llvm.ConstNamedStruct(b.getLLVMRuntimeType("_string"), []llvm.Value{strPtr, strLen})
2500+
strObj := llvm.ConstNamedStruct(c.getLLVMRuntimeType("_string"), []llvm.Value{strPtr, strLen})
25022501
return strObj
25032502
} else if typ.Kind() == types.UnsafePointer {
25042503
if !expr.IsNil() {
25052504
value, _ := constant.Uint64Val(constant.ToInt(expr.Value))
2506-
return llvm.ConstIntToPtr(llvm.ConstInt(b.uintptrType, value, false), b.i8ptrType)
2505+
return llvm.ConstIntToPtr(llvm.ConstInt(c.uintptrType, value, false), c.i8ptrType)
25072506
}
2508-
return llvm.ConstNull(b.i8ptrType)
2507+
return llvm.ConstNull(c.i8ptrType)
25092508
} else if typ.Info()&types.IsUnsigned != 0 {
25102509
n, _ := constant.Uint64Val(constant.ToInt(expr.Value))
25112510
return llvm.ConstInt(llvmType, n, false)
@@ -2516,18 +2515,18 @@ func (b *builder) createConst(expr *ssa.Const) llvm.Value {
25162515
n, _ := constant.Float64Val(expr.Value)
25172516
return llvm.ConstFloat(llvmType, n)
25182517
} else if typ.Kind() == types.Complex64 {
2519-
r := b.createConst(ssa.NewConst(constant.Real(expr.Value), types.Typ[types.Float32]))
2520-
i := b.createConst(ssa.NewConst(constant.Imag(expr.Value), types.Typ[types.Float32]))
2521-
cplx := llvm.Undef(b.ctx.StructType([]llvm.Type{b.ctx.FloatType(), b.ctx.FloatType()}, false))
2522-
cplx = b.CreateInsertValue(cplx, r, 0, "")
2523-
cplx = b.CreateInsertValue(cplx, i, 1, "")
2518+
r := c.createConst(ssa.NewConst(constant.Real(expr.Value), types.Typ[types.Float32]))
2519+
i := c.createConst(ssa.NewConst(constant.Imag(expr.Value), types.Typ[types.Float32]))
2520+
cplx := llvm.Undef(c.ctx.StructType([]llvm.Type{c.ctx.FloatType(), c.ctx.FloatType()}, false))
2521+
cplx = llvm.ConstInsertValue(cplx, r, []uint32{0})
2522+
cplx = llvm.ConstInsertValue(cplx, i, []uint32{1})
25242523
return cplx
25252524
} else if typ.Kind() == types.Complex128 {
2526-
r := b.createConst(ssa.NewConst(constant.Real(expr.Value), types.Typ[types.Float64]))
2527-
i := b.createConst(ssa.NewConst(constant.Imag(expr.Value), types.Typ[types.Float64]))
2528-
cplx := llvm.Undef(b.ctx.StructType([]llvm.Type{b.ctx.DoubleType(), b.ctx.DoubleType()}, false))
2529-
cplx = b.CreateInsertValue(cplx, r, 0, "")
2530-
cplx = b.CreateInsertValue(cplx, i, 1, "")
2525+
r := c.createConst(ssa.NewConst(constant.Real(expr.Value), types.Typ[types.Float64]))
2526+
i := c.createConst(ssa.NewConst(constant.Imag(expr.Value), types.Typ[types.Float64]))
2527+
cplx := llvm.Undef(c.ctx.StructType([]llvm.Type{c.ctx.DoubleType(), c.ctx.DoubleType()}, false))
2528+
cplx = llvm.ConstInsertValue(cplx, r, []uint32{0})
2529+
cplx = llvm.ConstInsertValue(cplx, i, []uint32{1})
25312530
return cplx
25322531
} else {
25332532
panic("unknown constant of basic type: " + expr.String())
@@ -2536,35 +2535,35 @@ func (b *builder) createConst(expr *ssa.Const) llvm.Value {
25362535
if expr.Value != nil {
25372536
panic("expected nil chan constant")
25382537
}
2539-
return llvm.ConstNull(b.getLLVMType(expr.Type()))
2538+
return llvm.ConstNull(c.getLLVMType(expr.Type()))
25402539
case *types.Signature:
25412540
if expr.Value != nil {
25422541
panic("expected nil signature constant")
25432542
}
2544-
return llvm.ConstNull(b.getLLVMType(expr.Type()))
2543+
return llvm.ConstNull(c.getLLVMType(expr.Type()))
25452544
case *types.Interface:
25462545
if expr.Value != nil {
25472546
panic("expected nil interface constant")
25482547
}
25492548
// Create a generic nil interface with no dynamic type (typecode=0).
25502549
fields := []llvm.Value{
2551-
llvm.ConstInt(b.uintptrType, 0, false),
2552-
llvm.ConstPointerNull(b.i8ptrType),
2550+
llvm.ConstInt(c.uintptrType, 0, false),
2551+
llvm.ConstPointerNull(c.i8ptrType),
25532552
}
2554-
return llvm.ConstNamedStruct(b.getLLVMRuntimeType("_interface"), fields)
2553+
return llvm.ConstNamedStruct(c.getLLVMRuntimeType("_interface"), fields)
25552554
case *types.Pointer:
25562555
if expr.Value != nil {
25572556
panic("expected nil pointer constant")
25582557
}
2559-
return llvm.ConstPointerNull(b.getLLVMType(typ))
2558+
return llvm.ConstPointerNull(c.getLLVMType(typ))
25602559
case *types.Slice:
25612560
if expr.Value != nil {
25622561
panic("expected nil slice constant")
25632562
}
2564-
elemType := b.getLLVMType(typ.Elem())
2563+
elemType := c.getLLVMType(typ.Elem())
25652564
llvmPtr := llvm.ConstPointerNull(llvm.PointerType(elemType, 0))
2566-
llvmLen := llvm.ConstInt(b.uintptrType, 0, false)
2567-
slice := b.ctx.ConstStruct([]llvm.Value{
2565+
llvmLen := llvm.ConstInt(c.uintptrType, 0, false)
2566+
slice := c.ctx.ConstStruct([]llvm.Value{
25682567
llvmPtr, // backing array
25692568
llvmLen, // len
25702569
llvmLen, // cap
@@ -2575,7 +2574,7 @@ func (b *builder) createConst(expr *ssa.Const) llvm.Value {
25752574
// I believe this is not allowed by the Go spec.
25762575
panic("non-nil map constant")
25772576
}
2578-
llvmType := b.getLLVMType(typ)
2577+
llvmType := c.getLLVMType(typ)
25792578
return llvm.ConstNull(llvmType)
25802579
default:
25812580
panic("unknown constant: " + expr.String())

0 commit comments

Comments
 (0)