-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathllvmUtils.py
More file actions
133 lines (79 loc) · 2.79 KB
/
llvmUtils.py
File metadata and controls
133 lines (79 loc) · 2.79 KB
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
import llvmlite.ir as ir
import llvmlite.binding as llvm
intType = ir.IntType(32)
charType = ir.IntType(8)
doubleType = ir.DoubleType()
voidType = ir.VoidType()
nVectorStructType = ir.LiteralStructType([])
intPointerType = intType.as_pointer()
charPointerType = charType.as_pointer()
doublePointerType = doubleType.as_pointer()
nVectorStructPointerType = nVectorStructType.as_pointer()
def irIntPointerType():
return intPointerType
def irCharPointerType():
return charPointerType
def irDoublePointerType():
return doublePointerType
def irVoidType():
return voidType
def irIntType():
return intType
def irDoubleType():
return doubleType
def irCharType():
return charType
def irNVectorType():
# this one should not be used! we're operating only on pointers here!
return nVectorStructType
def irNVectorPointerType():
return nVectorStructPointerType
def isDouble(arg):
return arg.type == irDoubleType()
def isInt(arg):
return arg.type == irIntType()
def isString(arg):
return arg.type == irCharPointerType()
def isVector(arg):
return arg.type == irNVectorPointerType()
def doubleArrayInitializer(count):
return ir.Constant.literal_array(
[ir.Constant(irDoubleType(), 0.0) for _ in range(count)])
def intArrayInitializer(count):
return ir.Constant.literal_array(
[ir.Constant(irIntType(), 0) for _ in range(count)])
def intArrayLiteral(literal):
return ir.Constant.literal_array(
[ir.Constant(irIntType(), int(c)) for c in literal])
def stringLiteral(literal):
return ir.Constant.literal_array(
[ir.Constant(irCharType(), ord(c)) for c in literal])
def intLiteral(value):
return ir.Constant(irIntType(), int(value))
def doubleLiteral(value):
return ir.Constant(irDoubleType(), float(value))
def namedGlobalStringLiteral(module, literal, varName):
literalArray = stringLiteral(literal)
glo = ir.GlobalVariable(
module, literalArray.type, varName)
glo.global_constant = True
glo.initializer = literalArray
return glo
def namedIntArrayLiteral(module, literal, varName):
literalArray = intArrayLiteral(literal)
glo = ir.GlobalVariable(
module, literalArray.type, varName)
glo.global_constant = True
glo.initializer = literalArray
return glo
def arrayPtr(array):
return array.gep([ir.Constant(irIntType(), 0),
ir.Constant(irIntType(), 0)])
def gepArray(array, index):
return array.gep([ir.Constant(irIntType(), 0),
ir.Constant(irIntType(), int(index))])
def gepArrayBuilder(builder, array, index):
return builder.gep(array, [ir.Constant(irIntType(), 0),
ir.Constant(irIntType(), int(index))])
def intArrType(size):
return ir.ArrayType(irIntType(), size)