[go: up one dir, main page]

File: README_LLVM_CBUILDER.md

package info (click to toggle)
llvm-py 0.12.7-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 3,596 kB
  • ctags: 5,378
  • sloc: python: 16,884; cpp: 1,917; makefile: 209; ansic: 177; pascal: 87; sh: 8
file content (144 lines) | stat: -rw-r--r-- 3,598 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
LLVM CBuilder
=============

A few short examples:
(TODO: add more later)

```python
from llvm.core import *
from llvm_cbuilder import *
import llvm_cbuilder.shortnames as C
```

```python
class Square(CDefinition):
    _name_ = 'square'
    _retty_ = C.double
    _argtys_ = [ ('x', C.double) ]

    def body(self, x):
        y = x * x
        self.ret(y)
```

```python
m = Module.new('my_module')
llvm_square = Square()(m)
print(m)
```

```
; ModuleID = 'my_module'

define double @square(double %x) {
decl:
  %0 = fmul double %x, %x
  ret double %0
}
```


```python
class IsPrime(CDefinition):
    _name_ = 'isprime'
    _retty_ = C.int
    _argtys_ = [('x', C.int)]

    def body(self, x):
        false = zero = self.constant(C.int, 0)
        true =  1)

        two = self.constant(C.int, 2)

        with self.ifelse( x <= two ) as ifelse:
            with ifelse.then():
                self.ret(true)

        with self.ifelse( (x % two) == zero ) as ifelse:
            with ifelse.then():
                self.ret(false)

        idx = self.var(C.int, 3, name='idx')

        with self.loop() as loop:
            with loop.condition() as setcond:
                setcond( idx < x )

            with loop.body():
                with self.ifelse( (x % idx ) == zero ) as ifelse:
                    with ifelse.then():
                        self.ret(false)
                idx += two

        self.ret(true)
```



```
define i32 @isprime(i32 %x) {
decl:
  %0 = icmp sle i32 %x, 2
  br i1 %0, label %if.then, label %if.end

if.then:                                          ; preds = %loop.body, %loop.cond, %if.end, %decl
  %merge = phi i32 [ 1, %decl ], [ 0, %if.end ], [ 1, %loop.cond ], [ 0, %loop.body ]
  ret i32 %merge

if.end:                                           ; preds = %decl
  %1 = srem i32 %x, 2
  %2 = icmp eq i32 %1, 0
  br i1 %2, label %if.then, label %if.end4

if.end4:                                          ; preds = %if.end
  br label %loop.cond

loop.cond:                                        ; preds = %if.end7, %if.end4
  %idx.0 = phi i32 [ 3, %if.end4 ], [ %6, %if.end7 ]
  %3 = icmp slt i32 %idx.0, %x
  br i1 %3, label %loop.body, label %if.then

loop.body:                                        ; preds = %loop.cond
  %4 = srem i32 %x, %idx.0
  %5 = icmp eq i32 %4, 0
  br i1 %5, label %if.then, label %if.end7

if.end7:                                          ; preds = %loop.body
  %6 = add i32 %idx.0, 2
  br label %loop.cond
}

; ModuleID = 'my_module'

define i32 @isprime(i32 %x) {
decl:
  %0 = icmp sle i32 %x, 2
  br i1 %0, label %if.then, label %if.end

if.then:                                          ; preds = %loop.body, %loop.cond, %if.end, %decl
  %merge = phi i32 [ 1, %decl ], [ 0, %if.end ], [ 1, %loop.cond ], [ 0, %loop.body ]
  ret i32 %merge

if.end:                                           ; preds = %decl
  %1 = srem i32 %x, 2
  %2 = icmp eq i32 %1, 0
  br i1 %2, label %if.then, label %if.end4

if.end4:                                          ; preds = %if.end
  br label %loop.cond

loop.cond:                                        ; preds = %if.end7, %if.end4
  %idx.0 = phi i32 [ 3, %if.end4 ], [ %6, %if.end7 ]
  %3 = icmp slt i32 %idx.0, %x
  br i1 %3, label %loop.body, label %if.then

loop.body:                                        ; preds = %loop.cond
  %4 = srem i32 %x, %idx.0
  %5 = icmp eq i32 %4, 0
  br i1 %5, label %if.then, label %if.end7

if.end7:                                          ; preds = %loop.body
  %6 = add i32 %idx.0, 2
  br label %loop.cond
}
```