forked from indygreg/llvm
/
module.py
239 lines (186 loc) · 7.27 KB
/
module.py
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#===- module.py - Python Object Bindings ---------------------*- python -*--===#
#
# The LLVM Compiler Infrastructure
#
# This file is distributed under the University of Illinois Open Source
# License. See LICENSE.TXT for details.
#
#===------------------------------------------------------------------------===#
import tempfile
import ctypes
from ctypes import string_at
from ..common import LLVMObject
from ..common import LLVMEnum
from ..common import get_library
from ..common import c_object_p
from llvm.core.value import Value
from .context import Context
from .memorybuffer import MemoryBuffer
import value
lib = get_library()
@lib.c_name("LLVMModuleRef")
class Module(LLVMObject):
"""Represents the top-level structure of an llvm program in an opaque object."""
def __init__(self, name=None, context=None, ptr=None):
"""
Create a new module.
Args:
- name (str): Name of the new module.
- context (:class:`llvm.core.context.Context`): Context to create module in, defaults to the global context.
"""
if ptr is None:
if name is None:
raise ValueError("Module must have name")
if context is None:
context = Context.GetGlobalContext()
if name is None:
raise ValueError("Module must have a name")
ptr = lib.LLVMModuleCreateWithNameInContext(name, context)
LLVMObject.__init__(self, ptr=ptr)
@staticmethod
def from_bitcode_buffer(contents, context=None):
"""
Create a new module by reading bitcode from the specified memorybuffer.
Args:
- path (:class:`llvm.core.memorybuffer.MemoryBuffer`): Path to the file to read bitcode from.
- context (:class:`llvm.core.context.Context`): Context to create module in, defaults to the global context.
"""
if context is None:
context = Context.GetGlobalContext()
ptr = c_object_p()
err = ctypes.c_char_p()
r = lib.LLVMParseBitcodeInContext(context, contents, ctypes.byref(ptr), ctypes.byref(err))
if r:
raise ValueError("Error loading bitcode: %s" % err.value)
m = Module(ptr=ptr)
m.take_ownership(contents)
return m
@staticmethod
def from_bitcode_file(path, context=None):
"""
Create a new module by reading bitcode from a file.
Args:
- path (str): Path to the file to read bitcode from.
- context (:class:`llvm.core.context.Context`): Context to create module in, defaults to the global context.
"""
contents = MemoryBuffer(filename=path)
return Module.from_bitcode_buffer(contents, context)
def __dispose__(self):
lib.LLVMDisposeModule(self)
@property
def target(self):
"""Obtain or set the target triple for this module"""
return string_at(lib.LLVMGetTarget(self))
@target.setter
def target(self, tgt):
lib.LLVMSetTarget(self, tgt)
@property
def data_layout(self):
"""Obtain or set the data layout for this module."""
return string_at(lib.LLVMGetDataLayout(self))
@data_layout.setter
def data_layout(self, tgt):
lib.LLVMSetDataLayout(self, tgt)
datalayout = data_layout ## XXX
@property
def context(self):
"""Return the Context this module was created in.
"""
return Context._from_ptr(lib.LLVMGetModuleContext(self))
def dump(self):
"""Dump the module to standard error.
.. note::
This method does a raw dump to the stderr fd, bypassing
sys.stderr.
"""
lib.LLVMDumpModule(self)
def verify(self):
"""Run sanity check on module.
"""
msg = ctypes.c_char_p()
r = lib.LLVMVerifyModule(self, VerifierFailureAction.ReturnStatus, ctypes.byref(msg))
if r:
raise ValueError(msg.value)
return True
def to_assembly(self):
"""Return the IR for this module as a human readable string"""
tmp = tempfile.NamedTemporaryFile()
err = ctypes.c_char_p()
r = lib.LLVMPrintModuleToFile(self, tmp.name, ctypes.byref(err))
if r:
raise RuntimeError(err.value)
return tmp.read()
def write_bitcode(self, fil):
"""Write the bitcode for this module to specified file.
Args:
fil (object): A filelike object with a `fileno` method.
"""
r = lib.LLVMWriteBitcodeToFD(self, fil.fileno(), 0, 1)
if r != 0:
raise IOError("Error writing to file: %d", r)
def to_bitcode(self):
"""Return the bitcode for this module"""
tmp=tempfile.TemporaryFile()
self.write_bitcode(tmp)
tmp.seek(0)
return tmp.read()
class __function_iterator(object):
def __init__(self, module, reverse=False):
self.module = module
self.reverse = reverse
if self.reverse:
self.function = self.module.last
else:
self.function = self.module.first
def __iter__(self):
return self
def next(self):
if not isinstance(self.function, value.Function):
raise StopIteration("")
result = self.function
if self.reverse:
self.function = self.function.prev
else:
self.function = self.function.next
return result
def __iter__(self):
return Module.__function_iterator(self)
def __reversed__(self):
return Module.__function_iterator(self, reverse=True)
@property
def first(self):
return value.Value._create_from_ptr(lib.LLVMGetFirstFunction(self))
@property
def last(self):
return value.Value._create_from_ptr(lib.LLVMGetLastFunction(self))
def print_module_to_file(self, filename):
out = ctypes.c_char_p(None)
# Result is inverted so 0 means everything was ok.
result = lib.LLVMPrintModuleToFile(self, filename, ctypes.byref(out))
if result:
raise RuntimeError("LLVM Error: %s" % out.value)
def add_function(self, functype, name):
"""
Add a new function to the module.
Args:
- functype (:class:`llvm.core.types.FunctionType`): Type of the new function.
- name (str): Name of the new function.
Returns:
:class:`llvm.core.value.Function` -- the newly created function.
"""
return value.Value._create_from_ptr(lib.LLVMAddFunction(self, name, functype))
def get_function(self, name):
return value.Value._create_from_ptr(lib.LLVMGetNamedFunction(self, name))
def add_global_variable(self, vartype, name):
"""
Add a new global variable to the module.
Args:
- vartype (:class:`llvm.core.types.Type`): Type of the global variable.
- name (str): Name of the global variable
Returns:
:class:`llvm.core.value.GlobalVariable` -- the new global variable.
"""
return value.Value._create_from_ptr(lib.LLVMAddGlobal(self, vartype, name))
@lib.c_enum("LLVMVerifierFailureAction", "LLVM", "Action")
class VerifierFailureAction(LLVMEnum):
pass