Exemple #1
0
// globalVarDecl lowers the given global variable declaration to LLVM IR,
// emitting code to m.
func (m *Module) globalVarDecl(n *ast.VarDecl) {
	// Input:
	//    int x;
	// Output:
	//    @x = global i32 0
	ident := n.Name()
	dbg.Printf("create global variable: %v", n)
	typ := toIrType(n.Type())
	var val value.Value
	switch {
	case n.Val != nil:
		panic("support for global variable initializer not yet implemented")
	case irtypes.IsInt(typ):
		var err error
		val, err = constant.NewInt(typ, "0")
		if err != nil {
			panic(fmt.Sprintf("unable to create integer constant; %v", err))
		}
	default:
		val = constant.NewZeroInitializer(typ)
	}
	global, err := ir.NewGlobalDef(ident.Name, val, false)
	if err != nil {
		panic(fmt.Sprintf("unable to create global variable definition %q", ident))
	}
	m.setIdentValue(ident, global)
	// Emit global variable definition.
	m.emitGlobal(global)
}
Exemple #2
0
func TestIsInt(t *testing.T) {
	golden := []struct {
		want bool
		typ  types.Type
	}{
		{want: false, typ: voidTyp},          // void
		{want: true, typ: i1Typ},             // i1
		{want: true, typ: i8Typ},             // i8
		{want: true, typ: i32Typ},            // i32
		{want: false, typ: f16Typ},           // half
		{want: false, typ: f32Typ},           // float
		{want: false, typ: f64Typ},           // double
		{want: false, typ: f128Typ},          // fp128
		{want: false, typ: f80_x86Typ},       // x86_fp80
		{want: false, typ: f128_ppcTyp},      // ppc_fp128
		{want: false, typ: mmxTyp},           // x86_mmx
		{want: false, typ: labelTyp},         // label
		{want: false, typ: metadataTyp},      // metadata
		{want: false, typ: funcTyp},          // i32 (i32)
		{want: false, typ: i8PtrTyp},         // i8*
		{want: false, typ: f16PtrTyp},        // half*
		{want: false, typ: mmxPtrTyp},        // x86_mmx*
		{want: false, typ: funcPtrTyp},       // i32 (i32)*
		{want: false, typ: i8x1VecTyp},       // <1 x i8>
		{want: false, typ: i32x2VecTyp},      // <2 x i32>
		{want: false, typ: f16x3VecTyp},      // <3 x half>
		{want: false, typ: f32x4VecTyp},      // <4 x float>
		{want: false, typ: f64x5VecTyp},      // <5 x double>
		{want: false, typ: f128x6VecTyp},     // <6 x fp128>
		{want: false, typ: f80_x86x7VecTyp},  // <7 x x86_fp80>
		{want: false, typ: f128_ppcx8VecTyp}, // <8 x ppc_fp128>
		{want: false, typ: i8Ptrx9VecTyp},    // <9 x i8*>
		{want: false, typ: f16Ptrx10VecTyp},  // <10 x half*>
		{want: false, typ: i8x1ArrTyp},       // [1 x i8]
		{want: false, typ: i32x2ArrTyp},      // [2 x i32]
		{want: false, typ: f16x3ArrTyp},      // [3 x half]
		{want: false, typ: f32x4ArrTyp},      // [4 x float]
		{want: false, typ: f64x5ArrTyp},      // [5 x double]
		{want: false, typ: f128x6ArrTyp},     // [6 x fp128]
		{want: false, typ: f80_x86x7ArrTyp},  // [7 x x86_fp80]
		{want: false, typ: f128_ppcx8ArrTyp}, // [8 x ppc_fp128]
		{want: false, typ: i8Ptrx9ArrTyp},    // [9 x i8*]
		{want: false, typ: f16Ptrx10ArrTyp},  // [10 x half*]
		{want: false, typ: structTyp},        // {i1, float, x86_mmx, i32 (i32)*, [1 x i8], <3 x half>}
	}

	for i, g := range golden {
		got := types.IsInt(g.typ)
		if got != g.want {
			t.Errorf("i=%d: expected %v, got %v for type %q", i, g.want, got, g.typ)
		}
	}
}