// 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) }
// localVarDef lowers the given local variable definition to LLVM IR, emitting // code to f. func (m *Module) localVarDef(f *Function, n *ast.VarDecl) { // Input: // void f() { // int a; // <-- relevant line // } // Output: // %a = alloca i32 ident := n.Name() dbg.Printf("create local variable: %v", n) typ := toIrType(n.Type()) allocaInst, err := instruction.NewAlloca(typ, 1) if err != nil { panic(fmt.Sprintf("unable to create alloca instruction; %v", err)) } // Emit local variable definition. f.emitLocal(ident, allocaInst) if n.Val != nil { panic("support for local variable definition initializer not yet implemented") } }