Mercurial > projects > ldc
comparison tools/binding/llvmsample1.d @ 1651:cb960b882ca3 default tip
bindings were moved to dsource.org/projects/bindings/
author | Moritz Warning <moritzwarning@web.de> |
---|---|
date | Thu, 20 May 2010 20:05:03 +0200 |
parents | 40bd4a0d4870 |
children |
comparison
equal
deleted
inserted
replaced
1650:40bd4a0d4870 | 1651:cb960b882ca3 |
---|---|
1 // simple hello world sample of D LLVM | |
2 module llvmsample1; | |
3 | |
4 import llvm.llvm; | |
5 | |
6 void main() | |
7 { | |
8 // create module | |
9 auto m = new Module("sample1"); | |
10 scope(exit) m.dispose(); | |
11 | |
12 // declare string | |
13 auto chello = ConstantArray.GetString("Hello World!\n", true); | |
14 auto hello = m.addGlobal(chello.type, "hellostring"); | |
15 hello.initializer = chello; | |
16 hello.linkage = Linkage.Internal; | |
17 hello.globalConstant = true; | |
18 | |
19 // declare printf | |
20 auto printfType = FunctionType.Get(Type.Int32, [ PointerType.Get(Type.Int8) ], true); | |
21 auto llprintf = m.addFunction(printfType, "printf"); | |
22 | |
23 // declare main | |
24 auto mainType = FunctionType.Get(Type.Int32, null); | |
25 auto llmain = m.addFunction(mainType, "main"); | |
26 | |
27 // create builder | |
28 auto b = new Builder; | |
29 scope(exit) b.dispose(); | |
30 | |
31 // create main body block | |
32 auto bb = llmain.appendBasicBlock("entry"); | |
33 b.positionAtEnd(bb); | |
34 | |
35 // call printf | |
36 auto zero = ConstantInt.GetU(Type.Int32, 0); | |
37 auto helloptr = b.buildGEP(hello, [ zero, zero ], "str"); | |
38 helloptr.dump(); | |
39 auto args = [ helloptr ]; | |
40 auto call = b.buildCall(llprintf, args, ""); | |
41 | |
42 // return 0 | |
43 b.buildRet(ConstantInt.GetS(Type.Int32, 0)); | |
44 | |
45 // write bitcode | |
46 m.writeBitcodeToFile("sample1.bc"); | |
47 } |