Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 53 lines (41 sloc) 1.808 kb
40e545a @tsion Initial commit. Working test.ooc
authored
1 use llvm
6ee57d7 @tsion Updated test.ooc.
authored
2 import llvm/[Core, ExecutionEngine, Target]
40e545a @tsion Initial commit. Working test.ooc
authored
3 import structs/ArrayList
4
71ede82 @tsion Now it JITs and runs!!
authored
5 LLVMLinkInJIT: extern func
6
40e545a @tsion Initial commit. Working test.ooc
authored
7 main: func {
71ede82 @tsion Now it JITs and runs!!
authored
8 LLVMLinkInJIT()
6ee57d7 @tsion Updated test.ooc.
authored
9 Target initializeNative()
71ede82 @tsion Now it JITs and runs!!
authored
10
8a1815f @tsion Renamed stuff. I hate having an LLVM* prefix on everything.
authored
11 // Create an (empty) module.
1d55be1 @tsion Broken execution example...
authored
12 myModule := Module new("my_module")
8a1815f @tsion Renamed stuff. I hate having an LLVM* prefix on everything.
authored
13
939381c @tsion Edited comments.
authored
14 // Get the i32 type
5c7cb66 @tsion Updated test.ooc.
authored
15 i32 := Type int32()
8a1815f @tsion Renamed stuff. I hate having an LLVM* prefix on everything.
authored
16
939381c @tsion Edited comments.
authored
17 // Now we need a function named 'sum' using this type. Functions are not
18 // free-standing; thye need to be contained in a module.
19 // Let's name the function arguments 'a', 'b', and 'c'.
5c7cb66 @tsion Updated test.ooc.
authored
20 sum := myModule addFunction("sum", i32,
5664e50 @tsion Fixed the example program.
authored
21 [i32, i32, i32],
22 ["a", "b", "c"])
8a1815f @tsion Renamed stuff. I hate having an LLVM* prefix on everything.
authored
23
24 // Our function needs a "basic block" -- a set of instructions that
25 // end with a terminator (like return, branch etc.). By convention
5c7cb66 @tsion Updated test.ooc.
authored
26 // the first block is called "entry". Instead of explicitly
27 // creating an "entry" basic block and then getting an instruction
28 // builderyou can just call builder() on a function:
29 builder := sum builder()
8a1815f @tsion Renamed stuff. I hate having an LLVM* prefix on everything.
authored
30
31 // OK, now for the instructions themselves. We'll create an add
32 // instruction that returns the sum as a value, which we'll use
33 // a ret instruction to return.
6ee57d7 @tsion Updated test.ooc.
authored
34 tmp := builder add(sum args[0], sum args[1], "tmp")
35 tmp2 := builder add(tmp, sum args[2], "tmp2")
8a1815f @tsion Renamed stuff. I hate having an LLVM* prefix on everything.
authored
36 builder ret(tmp2)
37
38 // We've completed the definition now! Let's see the LLVM assembly
39 // language representation of what we've created:
1d55be1 @tsion Broken execution example...
authored
40 myModule dump()
41
42 // Now, to try to run the function!
43 provider := ModuleProvider new(myModule)
44 engine := ExecutionEngine new(provider)
45
5c7cb66 @tsion Updated test.ooc.
authored
46 arg1 := GenericValue new(i32, 10 as ULLong, 0)
47 arg2 := GenericValue new(i32, 5 as ULLong, 0)
48 arg3 := GenericValue new(i32, 2 as ULLong, 0)
1d55be1 @tsion Broken execution example...
authored
49
66e18cc @tsion Woo it compiles.
authored
50 result := engine runFunction(sum, 3, [arg1, arg2, arg3] as GenericValue*)
1d55be1 @tsion Broken execution example...
authored
51 result toInt(0) toString() println()
40e545a @tsion Initial commit. Working test.ooc
authored
52 }
Something went wrong with that request. Please try again.