@@ -89,26 +89,21 @@ import (
89
89
func main () {
90
90
// Create a new LLVM IR module.
91
91
m := ir.NewModule ()
92
- arr := constant.NewCharArrayFromString (" Hello, world!\n\x00 " )
93
- gd := m.NewGlobalDef (" str" , arr )
94
- // link to external function puts
92
+ hello := constant.NewCharArrayFromString (" Hello, world!\n\x00 " )
93
+ str := m.NewGlobalDef (" str" , hello )
94
+ // Add external function declaration of puts.
95
95
puts := m.NewFunc (" puts" , types.I32 , ir.NewParam (" " , types.NewPointer (types.I8 )))
96
96
main := m.NewFunc (" main" , types.I32 )
97
97
entry := main.NewBlock (" " )
98
-
99
- // Perform cast per [1]:
100
- gep := constant.NewGetElementPtr (types.NewArray (15 , types.I8 ),
101
- gd,
102
- constant.NewInt (types.I8 , 0 ),
103
- constant.NewInt (types.I8 , 0 ))
98
+ // Cast *[15]i8 to *i8; c.f.: [1]
99
+ zero := constant.NewInt (types.I64 , 0 )
100
+ gep := constant.NewGetElementPtr (hello.Typ , str, zero, zero)
104
101
entry.NewCall (puts, gep)
105
102
entry.NewRet (constant.NewInt (types.I32 , 0 ))
106
103
fmt.Println (m)
107
104
}
108
-
109
- // See also:
105
+ // [1] See also:
110
106
// https://github.com/anoopsarkar/compilers-class-hw/blob/master/llvm-practice/helloworld.ll
111
-
112
107
```
113
108
114
109
#### Pseudo Random-Number Generator
0 commit comments