Commit bbd6780c authored by Kunshan Wang's avatar Kunshan Wang

Python binding generated

parent 80c594ae
......@@ -38,11 +38,16 @@ _primitive_types = {
def type_is_explicit_ptr(ty):
return ty.endswith("*")
r_handle_ty = re.compile(r'^Mu\w*(Value|Node)$')
r_handle_ty = re.compile(r'^Mu\w*Value$')
def type_is_handle(ty):
return r_handle_ty.match(ty) is not None
r_node_ty = re.compile(r'^Mu\w*(Node|Clause)$')
def type_is_node(ty):
return r_node_ty.match(ty) is not None
def type_is_ptr(ty):
return type_is_explicit_ptr(ty) or type_is_handle(ty)
......@@ -54,40 +59,6 @@ def to_basic_type(typedefs, name):
name = typedefs[name]
return name
_no_conversion = {
"MuID", # It's just Int.
"MuTrapHandler", # It is a function pointer. Handle in Scala.
"MuCPtr", # Intended to be raw pointer. Passed directly.
"MuCFP", # ditto
"MuWPID", # Just Int
"MuCommInst", # Only used in new_comminst, and the builder uses opcode directly.
}
_array_converters = {
"char*" : "readCharArray",
"uint64_t*" : "readLongArray",
"MuFlag*" : "readFlagArray",
}
_special_converters = {
"MuBool" : "intToBoolean",
"MuName" : "readCString",
"MuMemOrd" : "toMemoryOrder",
"MuAtomicRMWOptr" : "toAtomicRMWOptr",
"MuBinOptr" : "toBinOptr",
"MuCmpOptr" : "toCmpOptr",
"MuConvOptr" : "toConvOptr",
"MuCallConv" : "toFlag",
"MuDestKind" : "toDestKind",
}
_special_return_converters = {
"MuBool" : "booleanToInt",
"MuName" : "exposeString",
"MuCtx*" : "exposeMuCtx",
"MuVM*" : "exposeMicroVM",
}
_manually_defined_types = [
"MuCFP",
"MuValuesFreer",
......@@ -113,7 +84,7 @@ def generate_ctypes(ast):
_enums = [
("MuTrapHandlerResult", 'MU_'),
("MuDestKind", 'MU_DEST_'),
#("MuDestKind", 'MU_DEST_'), # removed
("MuBinOptr", 'MU_BINOP_'),
("MuCmpOptr", 'MU_CMP_'),
("MuConvOptr", 'MU_CONV_'),
......@@ -143,8 +114,7 @@ def generate_cenums(ast):
comminst_defs = enums_map["MuCommInst"]
lines.append("common_instruction_opcodes = {")
for d in comminst_defs:
dp, dv = d["pragmas"], d["value"]
muname = [pv for pk, pv in (p.split(":") for p in dp) if pk == "muname"][0]
muname, dv = d["muname"], d["value"]
lines.append(" '{}': {},".format(muname, dv))
lines.append("}")
......@@ -192,6 +162,7 @@ _defined_in_python = {k for k,v in _enums} | {
_mu_structs = {
"MuVM*": "MuVM",
"MuCtx*": "MuCtx",
"MuIRBuilder*": "MuIRBuilder",
}
def to_python_ty(cty):
......@@ -247,24 +218,71 @@ _method_blacklist = {
"new_thread_exc",
"dump_keepalives",
},
"MuIRBuilder": {
"gen_sym",
},
}
def need_array_wrapper(params):
return any("array_sz_param" in param for param in params)
def generate_method(typedefs, strname, meth) -> Tuple[str, str]:
name = meth['name']
params = meth['params'][1:]
ret_ty = meth['ret_ty']
pragmas = meth['pragmas']
param_tys = [p["type"] for p in params]
py_param_tys = [to_python_ty(t) for t in param_tys]
py_ret_ty = to_python_ty(ret_ty)
if name in _method_blacklist[strname]:
name = name + "_"
naw = need_array_wrapper(params)
if name in _method_blacklist[strname] or naw:
stub_name = name + "_"
else:
stub_name = name
stub = """('{}', {}, [{}]),""".format(
stub_name, py_ret_ty, ", ".join(py_param_tys))
if naw:
formals = [p for p in params if not p.get("is_sz_param", False)]
formal_names = [p["name"] for p in formals]
actual_names = [p["name"] for p in params]
asgns = []
for f in formals:
fn = f["name"]
fcty = f["type"]
ary_ind = f.get("array_sz_param", None)
is_optional = f.get("is_optional", False)
if ary_ind != None:
assert(fcty.endswith("*"))
fcty_elem = fcty[:-1]
asgns.append("_actual_{}_ty = C{} * len({})".format(fn,
fcty_elem, fn))
asgns.append("_actual_{} = _actual_{}_ty()".format(fn, fn))
asgns.append("for _i,_v in enumerate({}):".format(fn))
if type_is_handle(fcty):
asgns.append(" _actual_{}[_i] = _v.c_mu_value".format(fn))
else:
asgns.append(" _actual_{}[_i] = _v".format(fn))
asgns.append("_actual_{} = len({})".format(ary_ind, fn))
elif is_optional and (type_is_node(fcty) or fcty == "MuID"):
asgns.append("_actual_{} = 0 if {} is None else {}".format(fn, fn, fn))
else:
asgns.append("_actual_{} = {}".format(fn, fn))
wrapper = """\
def {}(self, {}):
{}
return self.{}({})
""".format(name, ", ".join(formal_names), "\n".join(" "*8+ln for ln in asgns),
stub_name, ", ".join("_actual_"+n for n in actual_names))
else:
wrapper = None
return """('{}', {}, [{}]),""".format(
name, py_ret_ty, ", ".join(py_param_tys))
return stub, wrapper
def generate_stubs_for_struct(ast, name) -> str:
st = [s for s in ast["structs"] if s["name"] == name][0]
......@@ -272,13 +290,16 @@ def generate_stubs_for_struct(ast, name) -> str:
typedefs = ast["typedefs"]
results = []
stubs = []
wrappers = []
for meth in methods:
code = generate_method(typedefs, name, meth)
results.append(code)
stub, wrapper = generate_method(typedefs, name, meth)
stubs.append(stub)
if wrapper is not None:
wrappers.append(wrapper)
return "\n".join(results)
return "\n".join(stubs), "\n".join(wrappers)
def main():
with open(muapi_h_path) as f:
......@@ -289,8 +310,9 @@ def main():
c_types = generate_ctypes(ast)
c_enums = generate_cenums(ast)
muvalues = generate_muvalues(ast)
muvm_stubs = generate_stubs_for_struct(ast, "MuVM")
muctx_stubs = generate_stubs_for_struct(ast, "MuCtx")
muvm_stubs, _ = generate_stubs_for_struct(ast, "MuVM")
muctx_stubs, _ = generate_stubs_for_struct(ast, "MuCtx")
muirbuilder_stubs, muirbuilder_wrappers = generate_stubs_for_struct(ast, "MuIRBuilder")
injectable_files["libmu.py"].inject_many({
"CTYPES": c_types,
......@@ -298,6 +320,8 @@ def main():
"MUVALUE": muvalues,
"MuVM": muvm_stubs,
"MuCtx": muctx_stubs,
"MuIRBuilder": muirbuilder_stubs,
"wrp_MuIRBuilder": muirbuilder_wrappers,
})
if __name__=='__main__':
......
......@@ -15,7 +15,8 @@ injectable_files = injecttools.make_injectable_file_set(_refimpl2_root, [
("cStubs.scala", "src/main/scala/uvm/refimpl/nat/cStubs.scala",
["STUBS"]),
("libmu.py", "pythonbinding/libmu.py",
["CTYPES", "CENUMS", "MUVALUE", "MuVM", "MuCtx"]),
["CTYPES", "CENUMS", "MUVALUE", "MuVM", "MuCtx", "MuIRBuilder",
"wrp_MuIRBuilder"]),
("comminsts.scala", "src/main/scala/uvm/comminsts/comminsts.scala",
["IRBUILDER_COMMINSTS"]),
("internals.scala", "src/main/scala/uvm/refimpl/internals.scala",
......
from __future__ import division, absolute_import, print_function, unicode_literals
import sys
import ctypes, ctypes.util
from libmu import *
libc = ctypes.CDLL(ctypes.util.find_library("c"))
libc.write.restype = ctypes.c_ssize_t
libc.write.argtypes = [ctypes.c_int, ctypes.c_void_p, ctypes.c_size_t]
dll = MuRefImpl2StartDLL("../cbinding/libmurefimpl2start.so")
mu = dll.mu_refimpl2_new_ex(
dumpBundle=True
#vmLog="DEBUG"
#vmLog="INFO"
)
with mu.new_context() as ctx:
# Perpare the C function pointers:
puts_addr = ctypes.cast(libc.puts, ctypes.c_void_p).value
print("puts = ", puts_addr, hex(puts_addr))
# Building bundle...
b = ctx.new_ir_builder()
i32 = b.gen_sym("@i32")
i8 = b.gen_sym("@i8")
pi8 = b.gen_sym("@pi8")
puts_sig = b.gen_sym("@puts.sig")
puts_fp = b.gen_sym("@puts.fp")
puts = b.gen_sym("@puts")
b.new_type_int(i32, 32)
b.new_type_int(i8, 8)
b.new_type_uptr(pi8, i8)
b.new_funcsig(puts_sig, [pi8], [i32])
b.new_type_ufuncptr(puts_fp, puts_sig)
b.new_const_int(puts, pi8, puts_addr)
buf = ctypes.create_string_buffer("Hello python!".encode("ascii"))
buf_addr = ctypes.cast(buf, ctypes.c_void_p).value
print("buf_addr = ", buf_addr, hex(buf_addr))
msg = b.gen_sym("@msg")
b.new_const_int(msg, pi8, buf_addr)
mumain_sig = b.gen_sym("@mumain.sig")
mumain = b.gen_sym("@mumain")
fv = b.gen_sym("@mumain.v1")
entry = b.gen_sym("@mumain.v1.entry")
ccall = b.gen_sym("@mumain.v1.entry.ccall")
ccall_r = b.gen_sym("@mumain.v1.entry.ccall_r")
threadexit = b.gen_sym("@mumain.v1.entry.threadedit")
b.new_funcsig(mumain_sig, [], [])
b.new_func(mumain, mumain_sig)
b.new_func_ver(fv, mumain, [entry])
b.new_bb(entry, [], [], 0, [ccall, threadexit])
b.new_ccall(ccall, [ccall_r], MuCallConv.DEFAULT, puts_fp, puts_sig, puts, [msg], None, None)
b.new_comminst(threadexit, [], common_instruction_opcodes["@uvm.thread_exit"],
[], [], [], [], None, None)
b.load()
hmumain = ctx.handle_from_func(mumain)
st = ctx.new_stack(hmumain)
th = ctx.new_thread(st, None, PassValues())
mu.execute()
print("Returned from Mu.")
......@@ -202,7 +202,7 @@ CMuFloatValue = CMuValue
CMuDoubleValue = CMuValue
CMuUPtrValue = CMuValue
CMuUFPValue = CMuValue
CMuStructValue = CMuSeqValue
CMuStructValue = CMuValue
CMuArrayValue = CMuSeqValue
CMuVectorValue = CMuSeqValue
CMuRefValue = CMuGenRefValue
......@@ -212,24 +212,7 @@ CMuFuncRefValue = CMuGenRefValue
CMuThreadRefValue = CMuGenRefValue
CMuStackRefValue = CMuGenRefValue
CMuFCRefValue = CMuGenRefValue
CMuIRNode = CMuGenRefValue
CMuBundleNode = CMuIRNode
CMuChildNode = CMuIRNode
CMuTypeNode = CMuChildNode
CMuFuncSigNode = CMuChildNode
CMuVarNode = CMuChildNode
CMuGlobalVarNode = CMuVarNode
CMuConstNode = CMuGlobalVarNode
CMuGlobalNode = CMuGlobalVarNode
CMuFuncNode = CMuGlobalVarNode
CMuExpFuncNode = CMuGlobalVarNode
CMuLocalVarNode = CMuVarNode
CMuNorParamNode = CMuLocalVarNode
CMuExcParamNode = CMuLocalVarNode
CMuInstResNode = CMuLocalVarNode
CMuFuncVerNode = CMuChildNode
CMuBBNode = CMuChildNode
CMuInstNode = CMuChildNode
CMuIBRefValue = CMuGenRefValue
CMuCString = ctypes.c_char_p
CMuID = ctypes.c_uint32
CMuName = CMuCString
......@@ -239,7 +222,6 @@ CMuArraySize = ctypes.c_uint64
CMuWPID = ctypes.c_uint32
CMuFlag = ctypes.c_uint32
CMuTrapHandlerResult = CMuFlag
CMuDestKind = CMuFlag
CMuBinOptr = CMuFlag
CMuCmpOptr = CMuFlag
CMuConvOptr = CMuFlag
......@@ -247,6 +229,21 @@ CMuMemOrd = CMuFlag
CMuAtomicRMWOptr = CMuFlag
CMuCallConv = CMuFlag
CMuCommInst = CMuFlag
CMuTypeNode = CMuID
CMuFuncSigNode = CMuID
CMuVarNode = CMuID
CMuGlobalVarNode = CMuID
CMuLocalVarNode = CMuID
CMuConstNode = CMuID
CMuFuncNode = CMuID
CMuFuncVerNode = CMuID
CMuBBNode = CMuID
CMuInstNode = CMuID
CMuDestClause = CMuID
CMuExcClause = CMuID
CMuKeepaliveClause = CMuID
CMuCurStackClause = CMuID
CMuNewStackClause = CMuID
## GEN:END:CTYPES
## GEN:BEGIN:CENUMS
......@@ -255,15 +252,6 @@ class MuTrapHandlerResult:
REBIND_PASS_VALUES = 0x01
REBIND_THROW_EXC = 0x02
class MuDestKind:
NORMAL = 0x01
EXCEPT = 0x02
TRUE = 0x03
FALSE = 0x04
DEFAULT = 0x05
DISABLED = 0x06
ENABLED = 0x07
class MuBinOptr:
ADD = 0x01
SUB = 0x02
......@@ -395,97 +383,85 @@ common_instruction_opcodes = {
'@uvm.meta.enable_watchpoint': 0x25e,
'@uvm.meta.disable_watchpoint': 0x25f,
'@uvm.meta.set_trap_handler': 0x260,
'@uvm.irbuilder.new_bundle': 0x300,
'@uvm.irbuilder.load_bundle_from_node': 0x301,
'@uvm.irbuilder.abort_bundle_node': 0x302,
'@uvm.irbuilder.get_node': 0x303,
'@uvm.irbuilder.get_id': 0x304,
'@uvm.irbuilder.set_name': 0x305,
'@uvm.irbuilder.new_type_int': 0x306,
'@uvm.irbuilder.new_type_float': 0x307,
'@uvm.irbuilder.new_type_double': 0x308,
'@uvm.irbuilder.new_type_uptr': 0x309,
'@uvm.irbuilder.set_type_uptr': 0x30a,
'@uvm.irbuilder.new_type_ufuncptr': 0x30b,
'@uvm.irbuilder.set_type_ufuncptr': 0x30c,
'@uvm.irbuilder.new_type_struct': 0x30d,
'@uvm.irbuilder.new_type_hybrid': 0x30e,
'@uvm.irbuilder.new_type_array': 0x30f,
'@uvm.irbuilder.new_type_vector': 0x310,
'@uvm.irbuilder.new_type_void': 0x311,
'@uvm.irbuilder.new_type_ref': 0x312,
'@uvm.irbuilder.set_type_ref': 0x313,
'@uvm.irbuilder.new_type_iref': 0x314,
'@uvm.irbuilder.set_type_iref': 0x315,
'@uvm.irbuilder.new_type_weakref': 0x316,
'@uvm.irbuilder.set_type_weakref': 0x317,
'@uvm.irbuilder.new_type_funcref': 0x318,
'@uvm.irbuilder.set_type_funcref': 0x319,
'@uvm.irbuilder.new_type_tagref64': 0x31a,
'@uvm.irbuilder.new_type_threadref': 0x31b,
'@uvm.irbuilder.new_type_stackref': 0x31c,
'@uvm.irbuilder.new_type_framecursorref': 0x31d,
'@uvm.irbuilder.new_type_irnoderef': 0x31e,
'@uvm.irbuilder.new_funcsig': 0x31f,
'@uvm.irbuilder.new_const_int': 0x320,
'@uvm.irbuilder.new_const_int_ex': 0x321,
'@uvm.irbuilder.new_const_float': 0x322,
'@uvm.irbuilder.new_const_double': 0x323,
'@uvm.irbuilder.new_const_null': 0x324,
'@uvm.irbuilder.new_const_seq': 0x325,
'@uvm.irbuilder.new_const_extern': 0x326,
'@uvm.irbuilder.new_global_cell': 0x327,
'@uvm.irbuilder.new_func': 0x328,
'@uvm.irbuilder.new_func_ver': 0x329,
'@uvm.irbuilder.new_exp_func': 0x32a,
'@uvm.irbuilder.new_bb': 0x32b,
'@uvm.irbuilder.new_nor_param': 0x32c,
'@uvm.irbuilder.new_exc_param': 0x32d,
'@uvm.irbuilder.get_inst_res': 0x32e,
'@uvm.irbuilder.get_num_inst_res': 0x32f,
'@uvm.irbuilder.add_dest': 0x330,
'@uvm.irbuilder.add_keepalives': 0x331,
'@uvm.irbuilder.new_binop': 0x332,
'@uvm.irbuilder.new_cmp': 0x333,
'@uvm.irbuilder.new_conv': 0x334,
'@uvm.irbuilder.new_select': 0x335,
'@uvm.irbuilder.new_branch': 0x336,
'@uvm.irbuilder.new_branch2': 0x337,
'@uvm.irbuilder.new_switch': 0x338,
'@uvm.irbuilder.add_switch_dest': 0x339,
'@uvm.irbuilder.new_call': 0x33a,
'@uvm.irbuilder.new_tailcall': 0x33b,
'@uvm.irbuilder.new_ret': 0x33c,
'@uvm.irbuilder.new_throw': 0x33d,
'@uvm.irbuilder.new_extractvalue': 0x33e,
'@uvm.irbuilder.new_insertvalue': 0x33f,
'@uvm.irbuilder.new_extractelement': 0x340,
'@uvm.irbuilder.new_insertelement': 0x341,
'@uvm.irbuilder.new_shufflevector': 0x342,
'@uvm.irbuilder.new_new': 0x343,
'@uvm.irbuilder.new_newhybrid': 0x344,
'@uvm.irbuilder.new_alloca': 0x345,
'@uvm.irbuilder.new_allocahybrid': 0x346,
'@uvm.irbuilder.new_getiref': 0x347,
'@uvm.irbuilder.new_getfieldiref': 0x348,
'@uvm.irbuilder.new_getelemiref': 0x349,
'@uvm.irbuilder.new_shiftiref': 0x34a,
'@uvm.irbuilder.new_getvarpartiref': 0x34b,
'@uvm.irbuilder.new_load': 0x34c,
'@uvm.irbuilder.new_store': 0x34d,
'@uvm.irbuilder.new_cmpxchg': 0x34e,
'@uvm.irbuilder.new_atomicrmw': 0x34f,
'@uvm.irbuilder.new_fence': 0x350,
'@uvm.irbuilder.new_trap': 0x351,
'@uvm.irbuilder.new_watchpoint': 0x352,
'@uvm.irbuilder.new_wpbranch': 0x353,
'@uvm.irbuilder.new_ccall': 0x354,
'@uvm.irbuilder.new_newthread': 0x355,
'@uvm.irbuilder.new_swapstack_ret': 0x356,
'@uvm.irbuilder.new_swapstack_kill': 0x357,
'@uvm.irbuilder.set_newstack_pass_values': 0x358,
'@uvm.irbuilder.set_newstack_throw_exc': 0x359,
'@uvm.irbuilder.new_comminst': 0x35a,
'@uvm.irbuilder.new_ir_builder': 0x270,
'@uvm.irbuilder.load': 0x300,
'@uvm.irbuilder.abort': 0x301,
'@uvm.irbuilder.gen_sym': 0x302,
'@uvm.irbuilder.new_type_int': 0x303,
'@uvm.irbuilder.new_type_float': 0x304,
'@uvm.irbuilder.new_type_double': 0x305,
'@uvm.irbuilder.new_type_uptr': 0x306,
'@uvm.irbuilder.new_type_ufuncptr': 0x307,
'@uvm.irbuilder.new_type_struct': 0x308,
'@uvm.irbuilder.new_type_hybrid': 0x309,
'@uvm.irbuilder.new_type_array': 0x30a,
'@uvm.irbuilder.new_type_vector': 0x30b,
'@uvm.irbuilder.new_type_void': 0x30c,
'@uvm.irbuilder.new_type_ref': 0x30d,
'@uvm.irbuilder.new_type_iref': 0x30e,
'@uvm.irbuilder.new_type_weakref': 0x30f,
'@uvm.irbuilder.new_type_funcref': 0x310,
'@uvm.irbuilder.new_type_tagref64': 0x311,
'@uvm.irbuilder.new_type_threadref': 0x312,
'@uvm.irbuilder.new_type_stackref': 0x313,
'@uvm.irbuilder.new_type_framecursorref': 0x314,
'@uvm.irbuilder.new_funcsig': 0x315,
'@uvm.irbuilder.new_const_int': 0x316,
'@uvm.irbuilder.new_const_int_ex': 0x317,
'@uvm.irbuilder.new_const_float': 0x318,
'@uvm.irbuilder.new_const_double': 0x319,
'@uvm.irbuilder.new_const_null': 0x31a,
'@uvm.irbuilder.new_const_seq': 0x31b,
'@uvm.irbuilder.new_const_extern': 0x31c,
'@uvm.irbuilder.new_global_cell': 0x31d,
'@uvm.irbuilder.new_func': 0x31e,
'@uvm.irbuilder.new_exp_func': 0x31f,
'@uvm.irbuilder.new_func_ver': 0x320,
'@uvm.irbuilder.new_bb': 0x321,
'@uvm.irbuilder.new_dest_clause': 0x322,
'@uvm.irbuilder.new_exc_clause': 0x323,
'@uvm.irbuilder.new_keepalive_clause': 0x324,
'@uvm.irbuilder.new_csc_ret_with': 0x325,
'@uvm.irbuilder.new_csc_kill_old': 0x326,
'@uvm.irbuilder.new_nsc_pass_values': 0x327,
'@uvm.irbuilder.new_nsc_throw_exc': 0x328,
'@uvm.irbuilder.new_binop': 0x329,
'@uvm.irbuilder.new_cmp': 0x32a,
'@uvm.irbuilder.new_conv': 0x32b,
'@uvm.irbuilder.new_select': 0x32c,
'@uvm.irbuilder.new_branch': 0x32d,
'@uvm.irbuilder.new_branch2': 0x32e,
'@uvm.irbuilder.new_switch': 0x32f,
'@uvm.irbuilder.new_call': 0x330,
'@uvm.irbuilder.new_tailcall': 0x331,
'@uvm.irbuilder.new_ret': 0x332,
'@uvm.irbuilder.new_throw': 0x333,
'@uvm.irbuilder.new_extractvalue': 0x334,
'@uvm.irbuilder.new_insertvalue': 0x335,
'@uvm.irbuilder.new_extractelement': 0x336,
'@uvm.irbuilder.new_insertelement': 0x337,
'@uvm.irbuilder.new_shufflevector': 0x338,
'@uvm.irbuilder.new_new': 0x339,
'@uvm.irbuilder.new_newhybrid': 0x33a,
'@uvm.irbuilder.new_alloca': 0x33b,
'@uvm.irbuilder.new_allocahybrid': 0x33c,
'@uvm.irbuilder.new_getiref': 0x33d,
'@uvm.irbuilder.new_getfieldiref': 0x33e,
'@uvm.irbuilder.new_getelemiref': 0x33f,
'@uvm.irbuilder.new_shiftiref': 0x340,
'@uvm.irbuilder.new_getvarpartiref': 0x341,
'@uvm.irbuilder.new_load': 0x342,
'@uvm.irbuilder.new_store': 0x343,
'@uvm.irbuilder.new_cmpxchg': 0x344,
'@uvm.irbuilder.new_atomicrmw': 0x345,
'@uvm.irbuilder.new_fence': 0x346,
'@uvm.irbuilder.new_trap': 0x347,
'@uvm.irbuilder.new_watchpoint': 0x348,
'@uvm.irbuilder.new_wpbranch': 0x349,
'@uvm.irbuilder.new_ccall': 0x34a,
'@uvm.irbuilder.new_newthread': 0x34b,
'@uvm.irbuilder.new_swapstack': 0x34c,
'@uvm.irbuilder.new_comminst': 0x34d,
}
## GEN:END:CENUMS
......@@ -509,6 +485,11 @@ class CMuCtx(ctypes.Structure):
CPtrMuCtx = ctypes.POINTER(CMuCtx)
class CMuIRBuilder(ctypes.Structure):
pass # Incomplete now. _fields_ will be patched later.
CPtrMuIRBuilder = ctypes.POINTER(CMuIRBuilder)
CMuTrapHandler = ctypes.CFUNCTYPE(
# return value
None, # return void
......@@ -593,7 +574,7 @@ class MuFloatValue (MuValue ): _ctypes_ = CMuFloatValue
class MuDoubleValue (MuValue ): _ctypes_ = CMuDoubleValue
class MuUPtrValue (MuValue ): _ctypes_ = CMuUPtrValue
class MuUFPValue (MuValue ): _ctypes_ = CMuUFPValue
class MuStructValue (MuSeqValue ): _ctypes_ = CMuStructValue
class MuStructValue (MuValue ): _ctypes_ = CMuStructValue
class MuArrayValue (MuSeqValue ): _ctypes_ = CMuArrayValue
class MuVectorValue (MuSeqValue ): _ctypes_ = CMuVectorValue
class MuRefValue (MuGenRefValue ): _ctypes_ = CMuRefValue
......@@ -603,24 +584,7 @@ class MuFuncRefValue (MuGenRefValue ): _ctypes_ = CMuFuncRefValue
class MuThreadRefValue (MuGenRefValue ): _ctypes_ = CMuThreadRefValue
class MuStackRefValue (MuGenRefValue ): _ctypes_ = CMuStackRefValue
class MuFCRefValue (MuGenRefValue ): _ctypes_ = CMuFCRefValue
class MuIRNode (MuGenRefValue ): pass
class MuBundleNode (MuIRNode ): _ctypes_ = CMuBundleNode
class MuChildNode (MuIRNode ): pass
class MuTypeNode (MuChildNode ): _ctypes_ = CMuTypeNode
class MuFuncSigNode (MuChildNode ): _ctypes_ = CMuFuncSigNode
class MuVarNode (MuChildNode ): pass
class MuGlobalVarNode (MuVarNode ): pass
class MuConstNode (MuGlobalVarNode ): _ctypes_ = CMuConstNode
class MuGlobalNode (MuGlobalVarNode ): _ctypes_ = CMuGlobalNode
class MuFuncNode (MuGlobalVarNode ): _ctypes_ = CMuFuncNode
class MuExpFuncNode (MuGlobalVarNode ): _ctypes_ = CMuExpFuncNode
class MuLocalVarNode (MuVarNode ): pass
class MuNorParamNode (MuLocalVarNode ): _ctypes_ = CMuNorParamNode
class MuExcParamNode (MuLocalVarNode ): _ctypes_ = CMuExcParamNode
class MuInstResNode (MuLocalVarNode ): _ctypes_ = CMuInstResNode
class MuFuncVerNode (MuChildNode ): _ctypes_ = CMuFuncVerNode
class MuBBNode (MuChildNode ): _ctypes_ = CMuBBNode
class MuInstNode (MuChildNode ): _ctypes_ = CMuInstNode
class MuIBRefValue (MuGenRefValue ): _ctypes_ = CMuIBRefValue
## GEN:END:MUVALUE
class NullablePointer:
......@@ -1113,6 +1077,308 @@ class MuCtx(_StructOfMethodsWrapper):
self.dump_keepalives_(cursor, cvals)
return [MuValue(cvals[i], self) for i in range(nvals)]
class MuIRBuilder(_StructOfMethodsWrapper):
"""An IR builder.
Each IRBuilder instance builds one Mu IR bundle and loads it into the micro
VM.
"""
_c_struct_type_ = CMuIRBuilder
_ctype_ = ctypes.POINTER(_c_struct_type_)
def __init__(self, struct_ptr, muctx):
super(self.__class__, self).__init__(struct_ptr, muctx)
self.muctx = muctx
self.muvm = muctx.muvm
def gen_sym(self, maybe_name=None):
return self.gen_sym_(maybe_name)
# GEN:BEGIN:wrp_MuIRBuilder
def new_type_struct(self, id, fieldtys):
_actual_id = id
_actual_fieldtys_ty = CMuTypeNode * len(fieldtys)
_actual_fieldtys = _actual_fieldtys_ty()
for _i,_v in enumerate(fieldtys):
_actual_fieldtys[_i] = _v
_actual_nfieldtys = len(fieldtys)
return self.new_type_struct_(_actual_id, _actual_fieldtys, _actual_nfieldtys)
def new_type_hybrid(self, id, fixedtys, varty):
_actual_id = id
_actual_fixedtys_ty = CMuTypeNode * len(fixedtys)
_actual_fixedtys = _actual_fixedtys_ty()
for _i,_v in enumerate(fixedtys):
_actual_fixedtys[_i] = _v
_actual_nfixedtys = len(fixedtys)
_actual_varty = varty
return self.new_type_hybrid_(_actual_id, _actual_fixedtys, _actual_nfixedtys, _actual_varty)
def new_funcsig(self, id, paramtys, rettys):
_actual_id = id
_actual_paramtys_ty = CMuTypeNode * len(paramtys)
_actual_paramtys = _actual_paramtys_ty()
for _i,_v in enumerate(paramtys):
_actual_paramtys[_i] = _v
_actual_nparamtys = len(paramtys)
_actual_rettys_ty = CMuTypeNode * len(rettys)
_actual_rettys = _actual_rettys_ty()
for _i,_v in enumerate(rettys):
_actual_rettys[_i] = _v
_actual_nrettys = len(rettys)
return self.new_funcsig_(_actual_id, _actual_paramtys, _actual_nparamtys, _actual_rettys, _actual_nrettys)
def new_const_int_ex(self, id, ty, values):
_actual_id = id
_actual_ty = ty
_actual_values_ty = Cuint64_t * len(values)
_actual_values = _actual_values_ty()
for _i,_v in enumerate(values):
_actual_values[_i] = _v
_actual_nvalues = len(values)
return self.new_const_int_ex_(_actual_id, _actual_ty, _actual_values, _actual_nvalues)
def new_const_seq(self, id, ty, elems):
_actual_id = id
_actual_ty = ty
_actual_elems_ty = CMuGlobalVarNode * len(elems)
_actual_elems = _actual_elems_ty()
for _i,_v in enumerate(elems):
_actual_elems[_i] = _v
_actual_nelems = len(elems)
return self.new_const_seq_(_actual_id, _actual_ty, _actual_elems, _actual_nelems)
def new_func_ver(self, id, func, bbs):
_actual_id = id
_actual_func = func
_actual_bbs_ty = CMuBBNode * len(bbs)
_actual_bbs = _actual_bbs_ty()
for _i,_v in enumerate(bbs):
_actual_bbs[_i] = _v
_actual_nbbs = len(bbs)
return self.new_func_ver_(_actual_id, _actual_func, _actual_bbs, _actual_nbbs)
def new_bb(self, id, nor_param_ids, nor_param_types, exc_param_id, insts):
_actual_id = id
_actual_nor_param_ids_ty = CMuID * len(nor_param_ids)
_actual_nor_param_ids = _actual_nor_param_ids_ty()
for _i,_v in enumerate(nor_param_ids):
_actual_nor_param_ids[_i] = _v
_actual_n_nor_params = len(nor_param_ids)
_actual_nor_param_types_ty = CMuTypeNode * len(nor_param_types)
_actual_nor_param_types = _actual_nor_param_types_ty()
for _i,_v in enumerate(nor_param_types):
_actual_nor_param_types[_i] = _v
_actual_n_nor_params = len(nor_param_types)
_actual_exc_param_id = 0 if exc_param_id is None else exc_param_id
_actual_insts_ty = CMuInstNode * len(insts)
_actual_insts = _actual_insts_ty()
for _i,_v in enumerate(insts):
_actual_insts[_i] = _v
_actual_ninsts = len(insts)
return self.new_bb_(_actual_id, _actual_nor_param_ids, _actual_nor_param_types, _actual_n_nor_params, _actual_exc_param_id, _actual_insts, _actual_ninsts)
def new_dest_clause(self, id, dest, vars):
_actual_id = id
_actual_dest = dest
_actual_vars_ty = CMuVarNode * len(vars)
_actual_vars = _actual_vars_ty()
for _i,_v in enumerate(vars):
_actual_vars[_i] = _v
_actual_nvars = len(vars)
return self.new_dest_clause_(_actual_id, _actual_dest, _actual_vars, _actual_nvars)
def new_keepalive_clause(self, id, vars):
_actual_id = id
_actual_vars_ty = CMuLocalVarNode * len(vars)
_actual_vars = _actual_vars_ty()
for _i,_v in enumerate(vars):
_actual_vars[_i] = _v
_actual_nvars = len(vars)
return self.new_keepalive_clause_(_actual_id, _actual_vars, _actual_nvars)
def new_csc_ret_with(self, id, rettys):
_actual_id = id
_actual_rettys_ty = CMuTypeNode * len(rettys)
_actual_rettys = _actual_rettys_ty()
for _i,_v in enumerate(rettys):