annotate tools/binding/llvmsample3.d @ 1273:1ba61de8796b

Committing LLVM binding for D as it currently exists in the SVN repository.
author Frits van Bommel <fvbommel wxs.nl>
date Mon, 27 Apr 2009 22:33:17 +0200
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1273
1ba61de8796b Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
1 // simple example that shows off getting D wrappers from C values.
1ba61de8796b Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
2 module llvmsample3;
1ba61de8796b Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
3
1ba61de8796b Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
4 import llvm.c.Core;
1ba61de8796b Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
5 import llvm.llvm;
1ba61de8796b Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
6
1ba61de8796b Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
7 void main()
1ba61de8796b Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
8 {
1ba61de8796b Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
9 auto m = new Module("sample3");
1ba61de8796b Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
10
1ba61de8796b Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
11 // global int32
1ba61de8796b Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
12 auto gi = m.addGlobal(Type.Int32, "myint");
1ba61de8796b Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
13 gi.initializer = ConstantInt.GetU(Type.Int32, 42);
1ba61de8796b Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
14
1ba61de8796b Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
15 // this is not a cached value, it's recreated dynamically
1ba61de8796b Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
16 auto _i = gi.initializer;
1ba61de8796b Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
17 auto ci = cast(ConstantInt)_i;
1ba61de8796b Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
18 assert(ci !is null);
1ba61de8796b Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
19 ci.dump;
1ba61de8796b Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
20
1ba61de8796b Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
21 // global struct
1ba61de8796b Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
22 auto st = StructType.Get([Type.Double,Type.Double,Type.Double]);
1ba61de8796b Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
23 auto gs = m.addGlobal(st, "mystruct");
1ba61de8796b Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
24 auto elems = new Constant[3];
1ba61de8796b Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
25 foreach(i,ref e; elems)
1ba61de8796b Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
26 e = ConstantReal.Get(Type.Double, i+1);
1ba61de8796b Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
27 gs.initializer = ConstantStruct.Get(elems);
1ba61de8796b Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
28
1ba61de8796b Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
29 // again this is not a cached value.
1ba61de8796b Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
30 auto s = gs.initializer;
1ba61de8796b Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
31 auto cs = cast(ConstantStruct)s;
1ba61de8796b Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
32 assert(cs !is null);
1ba61de8796b Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
33
1ba61de8796b Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
34 cs.dump;
1ba61de8796b Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
35 }