@@ -2470,42 +2470,41 @@ func (b *builder) createBinOp(op token.Token, typ, ytyp types.Type, x, y llvm.Va
2470
2470
}
2471
2471
2472
2472
// 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 {
2474
2474
switch typ := expr .Type ().Underlying ().(type ) {
2475
2475
case * types.Basic :
2476
- llvmType := b .getLLVMType (typ )
2476
+ llvmType := c .getLLVMType (typ )
2477
2477
if typ .Info ()& types .IsBoolean != 0 {
2478
- b := constant .BoolVal (expr .Value )
2479
2478
n := uint64 (0 )
2480
- if b {
2479
+ if constant . BoolVal ( expr . Value ) {
2481
2480
n = 1
2482
2481
}
2483
2482
return llvm .ConstInt (llvmType , n , false )
2484
2483
} else if typ .Info ()& types .IsString != 0 {
2485
2484
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 )
2487
2486
var strPtr llvm.Value
2488
2487
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 ))
2492
2491
global .SetLinkage (llvm .InternalLinkage )
2493
2492
global .SetGlobalConstant (true )
2494
2493
global .SetUnnamedAddr (true )
2495
2494
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 })
2498
2497
} else {
2499
- strPtr = llvm .ConstNull (b .i8ptrType )
2498
+ strPtr = llvm .ConstNull (c .i8ptrType )
2500
2499
}
2501
- strObj := llvm .ConstNamedStruct (b .getLLVMRuntimeType ("_string" ), []llvm.Value {strPtr , strLen })
2500
+ strObj := llvm .ConstNamedStruct (c .getLLVMRuntimeType ("_string" ), []llvm.Value {strPtr , strLen })
2502
2501
return strObj
2503
2502
} else if typ .Kind () == types .UnsafePointer {
2504
2503
if ! expr .IsNil () {
2505
2504
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 )
2507
2506
}
2508
- return llvm .ConstNull (b .i8ptrType )
2507
+ return llvm .ConstNull (c .i8ptrType )
2509
2508
} else if typ .Info ()& types .IsUnsigned != 0 {
2510
2509
n , _ := constant .Uint64Val (constant .ToInt (expr .Value ))
2511
2510
return llvm .ConstInt (llvmType , n , false )
@@ -2516,18 +2515,18 @@ func (b *builder) createConst(expr *ssa.Const) llvm.Value {
2516
2515
n , _ := constant .Float64Val (expr .Value )
2517
2516
return llvm .ConstFloat (llvmType , n )
2518
2517
} 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 } )
2524
2523
return cplx
2525
2524
} 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 } )
2531
2530
return cplx
2532
2531
} else {
2533
2532
panic ("unknown constant of basic type: " + expr .String ())
@@ -2536,35 +2535,35 @@ func (b *builder) createConst(expr *ssa.Const) llvm.Value {
2536
2535
if expr .Value != nil {
2537
2536
panic ("expected nil chan constant" )
2538
2537
}
2539
- return llvm .ConstNull (b .getLLVMType (expr .Type ()))
2538
+ return llvm .ConstNull (c .getLLVMType (expr .Type ()))
2540
2539
case * types.Signature :
2541
2540
if expr .Value != nil {
2542
2541
panic ("expected nil signature constant" )
2543
2542
}
2544
- return llvm .ConstNull (b .getLLVMType (expr .Type ()))
2543
+ return llvm .ConstNull (c .getLLVMType (expr .Type ()))
2545
2544
case * types.Interface :
2546
2545
if expr .Value != nil {
2547
2546
panic ("expected nil interface constant" )
2548
2547
}
2549
2548
// Create a generic nil interface with no dynamic type (typecode=0).
2550
2549
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 ),
2553
2552
}
2554
- return llvm .ConstNamedStruct (b .getLLVMRuntimeType ("_interface" ), fields )
2553
+ return llvm .ConstNamedStruct (c .getLLVMRuntimeType ("_interface" ), fields )
2555
2554
case * types.Pointer :
2556
2555
if expr .Value != nil {
2557
2556
panic ("expected nil pointer constant" )
2558
2557
}
2559
- return llvm .ConstPointerNull (b .getLLVMType (typ ))
2558
+ return llvm .ConstPointerNull (c .getLLVMType (typ ))
2560
2559
case * types.Slice :
2561
2560
if expr .Value != nil {
2562
2561
panic ("expected nil slice constant" )
2563
2562
}
2564
- elemType := b .getLLVMType (typ .Elem ())
2563
+ elemType := c .getLLVMType (typ .Elem ())
2565
2564
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 {
2568
2567
llvmPtr , // backing array
2569
2568
llvmLen , // len
2570
2569
llvmLen , // cap
@@ -2575,7 +2574,7 @@ func (b *builder) createConst(expr *ssa.Const) llvm.Value {
2575
2574
// I believe this is not allowed by the Go spec.
2576
2575
panic ("non-nil map constant" )
2577
2576
}
2578
- llvmType := b .getLLVMType (typ )
2577
+ llvmType := c .getLLVMType (typ )
2579
2578
return llvm .ConstNull (llvmType )
2580
2579
default :
2581
2580
panic ("unknown constant: " + expr .String ())
0 commit comments