MCPcopy Create free account
hub / github.com/llir/llvm / Example

Function Example

ir/example_test.go:11–80  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

9)
10
11func Example() {
12 // This example produces LLVM IR code equivalent to the following C code,
13 // which implements a pseudo-random number generator.
14 //
15 // int abs(int x);
16 //
17 // int seed = 0;
18 //
19 // // ref: https://en.wikipedia.org/wiki/Linear_congruential_generator
20 // // a = 0x15A4E35
21 // // c = 1
22 // int rand(void) {
23 // seed = seed*0x15A4E35 + 1;
24 // return abs(seed);
25 // }
26
27 // Create convenience types and constants.
28 i32 := types.I32
29 zero := constant.NewInt(i32, 0)
30 a := constant.NewInt(i32, 0x15A4E35) // multiplier of the PRNG.
31 c := constant.NewInt(i32, 1) // increment of the PRNG.
32
33 // Create a new LLVM IR module.
34 m := ir.NewModule()
35
36 // Create an external function declaration and append it to the module.
37 //
38 // int abs(int x);
39 abs := m.NewFunc("abs", i32, ir.NewParam("x", i32))
40
41 // Create a global variable definition and append it to the module.
42 //
43 // int seed = 0;
44 seed := m.NewGlobalDef("seed", zero)
45
46 // Create a function definition and append it to the module.
47 //
48 // int rand(void) { ... }
49 rand := m.NewFunc("rand", i32)
50
51 // Create an unnamed entry basic block and append it to the `rand` function.
52 entry := rand.NewBlock("")
53
54 // Create instructions and append them to the entry basic block.
55 tmp1 := entry.NewLoad(types.I32, seed)
56 tmp2 := entry.NewMul(tmp1, a)
57 tmp3 := entry.NewAdd(tmp2, c)
58 entry.NewStore(tmp3, seed)
59 tmp4 := entry.NewCall(abs, tmp3)
60 entry.NewRet(tmp4)
61
62 // Print the LLVM IR assembly of the module.
63 fmt.Println(m)
64
65 // Output:
66 //
67 // @seed = global i32 0
68 //

Callers

nothing calls this directly

Calls 12

NewFuncMethod · 0.95
NewGlobalDefMethod · 0.95
NewBlockMethod · 0.95
NewLoadMethod · 0.95
NewMulMethod · 0.95
NewAddMethod · 0.95
NewStoreMethod · 0.95
NewCallMethod · 0.95
NewRetMethod · 0.95
NewIntFunction · 0.92
NewModuleFunction · 0.92
NewParamFunction · 0.92

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…