To protect your data, the CISO officer has suggested users to enable GitLab 2FA as soon as possible.

Commit 8b345be8 authored by John Zhang's avatar John Zhang
Browse files

wip: towards a working binding.

- added support for other meta instructions
- included flags for COMMINST for meta-circular building other COMMINSTs
- added support for name=None in gen_sym
- added support for converting RPython list to meta C arrays
- updated some tests
parent 2b9cc393
......@@ -260,6 +260,10 @@ class MuCommInst:
META_ENABLE_WATCHPOINT = rffi.cast(MuFlag, 0x25e)
META_DISABLE_WATCHPOINT = rffi.cast(MuFlag, 0x25f)
META_SET_TRAP_HANDLER = rffi.cast(MuFlag, 0x260)
META_CONSTANT_BY_ID = rffi.cast(MuFlag, 0x268)
META_GLOBAL_BY_ID = rffi.cast(MuFlag, 0x269)
META_FUNC_BY_ID = rffi.cast(MuFlag, 0x26a)
META_EXPFUNC_BY_ID = rffi.cast(MuFlag, 0x26b)
IRBUILDER_NEW_IR_BUILDER = rffi.cast(MuFlag, 0x270)
IRBUILDER_LOAD = rffi.cast(MuFlag, 0x300)
IRBUILDER_ABORT = rffi.cast(MuFlag, 0x301)
......
......@@ -491,6 +491,10 @@ class MuCommInst:
META_ENABLE_WATCHPOINT = "MU_CI_UVM_META_ENABLE_WATCHPOINT"
META_DISABLE_WATCHPOINT = "MU_CI_UVM_META_DISABLE_WATCHPOINT"
META_SET_TRAP_HANDLER = "MU_CI_UVM_META_SET_TRAP_HANDLER"
META_CONSTANT_BY_ID = "MU_CI_UVM_META_CONSTANT_BY_ID"
META_GLOBAL_BY_ID = "MU_CI_UVM_META_GLOBAL_BY_ID"
META_FUNC_BY_ID = "MU_CI_UVM_META_FUNC_BY_ID"
META_EXPFUNC_BY_ID = "MU_CI_UVM_META_EXPFUNC_BY_ID"
IRBUILDER_NEW_IR_BUILDER = "MU_CI_UVM_IRBUILDER_NEW_IR_BUILDER"
IRBUILDER_LOAD = "MU_CI_UVM_IRBUILDER_LOAD"
IRBUILDER_ABORT = "MU_CI_UVM_IRBUILDER_ABORT"
......
......@@ -6,13 +6,39 @@ from rpython.rtyper.lltypesystem import rffi, lltype, llmemory, rstr
from rpython.rtyper.lltypesystem.lloperation import llop
from rpython.rlib.objectmodel import specialize, always_inline, ExtRegistryEntry, keepalive_until_here
from rpython.translator.mu import mutype
from rpython.translator.mu.ll2mu import ll2mu_type
IRBuilderRef = lltype.Ptr(lltype.GcOpaqueType('irbuilderref', hints={'mu_render_ptr_as': mutype.MUTA_IRBUILDER_REF}))
MuID = rffi.UINT # MU_INT32
MuFlag = rffi.UINT
MU_NO_ID = rffi.cast(MuID, 0)
class MetaInst:
ID_OF = "META_ID_OF"
NAME_OF = "META_NAME_OF"
LOAD_BUNDLE = "META_LOAD_BUNDLE"
LOAD_HAIL = "META_LOAD_HAIL"
NEW_CURSOR = "META_NEW_CURSOR"
NEXT_FRAME = "META_NEXT_FRAME"
COPY_CURSOR = "META_COPY_CURSOR"
CLOSE_CURSOR = "META_CLOSE_CURSOR"
CUR_FUNC = "META_CUR_FUNC"
CUR_FUNC_VER = "META_CUR_FUNC_VER"
CUR_INST = "META_CUR_INST"
DUMP_KEEPALIVES = "META_DUMP_KEEPALIVES"
POP_FRAMES_TO = "META_POP_FRAMES_TO"
PUSH_FRAME = "META_PUSH_FRAME"
ENABLE_WATCHPOINT = "META_ENABLE_WATCHPOINT"
DISABLE_WATCHPOINT = "META_DISABLE_WATCHPOINT"
SET_TRAP_HANDLER = "META_SET_TRAP_HANDLER"
CONSTANT_BY_ID = "META_CONSTANT_BY_ID"
GLOBAL_BY_ID = "META_GLOBAL_BY_ID"
FUNC_BY_ID = "META_FUNC_BY_ID"
EXPFUNC_BY_ID = "META_EXPFUNC_BY_ID"
class MetaIRBuilderInst:
NEW_IR_BUILDER = "IRBUILDER_NEW_IR_BUILDER"
LOAD = "IRBUILDER_LOAD"
......@@ -96,6 +122,226 @@ class MetaIRBuilderInst:
NEW_SWAPSTACK = "IRBUILDER_NEW_SWAPSTACK"
NEW_COMMINST = "IRBUILDER_NEW_COMMINST"
# --------------------
# Flags
class MuBinOpStatus:
N = rffi.cast(MuFlag, 0x01)
Z = rffi.cast(MuFlag, 0x02)
C = rffi.cast(MuFlag, 0x04)
V = rffi.cast(MuFlag, 0x08)
class MuBinOptr:
ADD = rffi.cast(MuFlag, 0x01)
SUB = rffi.cast(MuFlag, 0x02)
MUL = rffi.cast(MuFlag, 0x03)
SDIV = rffi.cast(MuFlag, 0x04)
SREM = rffi.cast(MuFlag, 0x05)
UDIV = rffi.cast(MuFlag, 0x06)
UREM = rffi.cast(MuFlag, 0x07)
SHL = rffi.cast(MuFlag, 0x08)
LSHR = rffi.cast(MuFlag, 0x09)
ASHR = rffi.cast(MuFlag, 0x0A)
AND = rffi.cast(MuFlag, 0x0B)
OR = rffi.cast(MuFlag, 0x0C)
XOR = rffi.cast(MuFlag, 0x0D)
FADD = rffi.cast(MuFlag, 0xB0)
FSUB = rffi.cast(MuFlag, 0xB1)
FMUL = rffi.cast(MuFlag, 0xB2)
FDIV = rffi.cast(MuFlag, 0xB3)
FREM = rffi.cast(MuFlag, 0xB4)
class MuCmpOptr:
EQ = rffi.cast(MuFlag, 0x20)
NE = rffi.cast(MuFlag, 0x21)
SGE = rffi.cast(MuFlag, 0x22)
SGT = rffi.cast(MuFlag, 0x23)
SLE = rffi.cast(MuFlag, 0x24)
SLT = rffi.cast(MuFlag, 0x25)
UGE = rffi.cast(MuFlag, 0x26)
UGT = rffi.cast(MuFlag, 0x27)
ULE = rffi.cast(MuFlag, 0x28)
ULT = rffi.cast(MuFlag, 0x29)
FFALSE = rffi.cast(MuFlag, 0xC0)
FTRUE = rffi.cast(MuFlag, 0xC1)
FUNO = rffi.cast(MuFlag, 0xC2)
FUEQ = rffi.cast(MuFlag, 0xC3)
FUNE = rffi.cast(MuFlag, 0xC4)
FUGT = rffi.cast(MuFlag, 0xC5)
FUGE = rffi.cast(MuFlag, 0xC6)
FULT = rffi.cast(MuFlag, 0xC7)
FULE = rffi.cast(MuFlag, 0xC8)
FORD = rffi.cast(MuFlag, 0xC9)
FOEQ = rffi.cast(MuFlag, 0xCA)
FONE = rffi.cast(MuFlag, 0xCB)
FOGT = rffi.cast(MuFlag, 0xCC)
FOGE = rffi.cast(MuFlag, 0xCD)
FOLT = rffi.cast(MuFlag, 0xCE)
FOLE = rffi.cast(MuFlag, 0xCF)
class MuConvOptr:
TRUNC = rffi.cast(MuFlag, 0x30)
ZEXT = rffi.cast(MuFlag, 0x31)
SEXT = rffi.cast(MuFlag, 0x32)
FPTRUNC = rffi.cast(MuFlag, 0x33)
FPEXT = rffi.cast(MuFlag, 0x34)
FPTOUI = rffi.cast(MuFlag, 0x35)
FPTOSI = rffi.cast(MuFlag, 0x36)
UITOFP = rffi.cast(MuFlag, 0x37)
SITOFP = rffi.cast(MuFlag, 0x38)
BITCAST = rffi.cast(MuFlag, 0x39)
REFCAST = rffi.cast(MuFlag, 0x3A)
PTRCAST = rffi.cast(MuFlag, 0x3B)
class MuMemOrd:
NOT_ATOMIC = rffi.cast(MuFlag, 0x00)
RELAXED = rffi.cast(MuFlag, 0x01)
CONSUME = rffi.cast(MuFlag, 0x02)
ACQUIRE = rffi.cast(MuFlag, 0x03)
RELEASE = rffi.cast(MuFlag, 0x04)
ACQ_REL = rffi.cast(MuFlag, 0x05)
SEQ_CST = rffi.cast(MuFlag, 0x06)
class MuAtomicRMWOptr:
XCHG = rffi.cast(MuFlag, 0x00)
ADD = rffi.cast(MuFlag, 0x01)
SUB = rffi.cast(MuFlag, 0x02)
AND = rffi.cast(MuFlag, 0x03)
NAND = rffi.cast(MuFlag, 0x04)
OR = rffi.cast(MuFlag, 0x05)
XOR = rffi.cast(MuFlag, 0x06)
MAX = rffi.cast(MuFlag, 0x07)
MIN = rffi.cast(MuFlag, 0x08)
UMAX = rffi.cast(MuFlag, 0x09)
UMIN = rffi.cast(MuFlag, 0x0A)
class MuCallConv:
DEFAULT = rffi.cast(MuFlag, 0x00)
class MuCommInst:
NEW_STACK = rffi.cast(MuFlag, 0x201)
KILL_STACK = rffi.cast(MuFlag, 0x202)
THREAD_EXIT = rffi.cast(MuFlag, 0x203)
CURRENT_STACK = rffi.cast(MuFlag, 0x204)
SET_THREADLOCAL = rffi.cast(MuFlag, 0x205)
GET_THREADLOCAL = rffi.cast(MuFlag, 0x206)
TR64_IS_FP = rffi.cast(MuFlag, 0x211)
TR64_IS_INT = rffi.cast(MuFlag, 0x212)
TR64_IS_REF = rffi.cast(MuFlag, 0x213)
TR64_FROM_FP = rffi.cast(MuFlag, 0x214)
TR64_FROM_INT = rffi.cast(MuFlag, 0x215)
TR64_FROM_REF = rffi.cast(MuFlag, 0x216)
TR64_TO_FP = rffi.cast(MuFlag, 0x217)
TR64_TO_INT = rffi.cast(MuFlag, 0x218)
TR64_TO_REF = rffi.cast(MuFlag, 0x219)
TR64_TO_TAG = rffi.cast(MuFlag, 0x21a)
FUTEX_WAIT = rffi.cast(MuFlag, 0x220)
FUTEX_WAIT_TIMEOUT = rffi.cast(MuFlag, 0x221)
FUTEX_WAKE = rffi.cast(MuFlag, 0x222)
FUTEX_CMP_REQUEUE = rffi.cast(MuFlag, 0x223)
KILL_DEPENDENCY = rffi.cast(MuFlag, 0x230)
NATIVE_PIN = rffi.cast(MuFlag, 0x240)
NATIVE_UNPIN = rffi.cast(MuFlag, 0x241)
NATIVE_GET_ADDR = rffi.cast(MuFlag, 0x242)
NATIVE_EXPOSE = rffi.cast(MuFlag, 0x243)
NATIVE_UNEXPOSE = rffi.cast(MuFlag, 0x244)
NATIVE_GET_COOKIE = rffi.cast(MuFlag, 0x245)
META_ID_OF = rffi.cast(MuFlag, 0x250)
META_NAME_OF = rffi.cast(MuFlag, 0x251)
META_LOAD_BUNDLE = rffi.cast(MuFlag, 0x252)
META_LOAD_HAIL = rffi.cast(MuFlag, 0x253)
META_NEW_CURSOR = rffi.cast(MuFlag, 0x254)
META_NEXT_FRAME = rffi.cast(MuFlag, 0x255)
META_COPY_CURSOR = rffi.cast(MuFlag, 0x256)
META_CLOSE_CURSOR = rffi.cast(MuFlag, 0x257)
META_CUR_FUNC = rffi.cast(MuFlag, 0x258)
META_CUR_FUNC_VER = rffi.cast(MuFlag, 0x259)
META_CUR_INST = rffi.cast(MuFlag, 0x25a)
META_DUMP_KEEPALIVES = rffi.cast(MuFlag, 0x25b)
META_POP_FRAMES_TO = rffi.cast(MuFlag, 0x25c)
META_PUSH_FRAME = rffi.cast(MuFlag, 0x25d)
META_ENABLE_WATCHPOINT = rffi.cast(MuFlag, 0x25e)
META_DISABLE_WATCHPOINT = rffi.cast(MuFlag, 0x25f)
META_SET_TRAP_HANDLER = rffi.cast(MuFlag, 0x260)
META_CONSTANT_BY_ID = rffi.cast(MuFlag, 0x268)
META_GLOBAL_BY_ID = rffi.cast(MuFlag, 0x269)
META_FUNC_BY_ID = rffi.cast(MuFlag, 0x26a)
META_EXPFUNC_BY_ID = rffi.cast(MuFlag, 0x26b)
IRBUILDER_NEW_IR_BUILDER = rffi.cast(MuFlag, 0x270)
IRBUILDER_LOAD = rffi.cast(MuFlag, 0x300)
IRBUILDER_ABORT = rffi.cast(MuFlag, 0x301)
IRBUILDER_GEN_SYM = rffi.cast(MuFlag, 0x302)
IRBUILDER_NEW_TYPE_INT = rffi.cast(MuFlag, 0x303)
IRBUILDER_NEW_TYPE_FLOAT = rffi.cast(MuFlag, 0x304)
IRBUILDER_NEW_TYPE_DOUBLE = rffi.cast(MuFlag, 0x305)
IRBUILDER_NEW_TYPE_UPTR = rffi.cast(MuFlag, 0x306)
IRBUILDER_NEW_TYPE_UFUNCPTR = rffi.cast(MuFlag, 0x307)
IRBUILDER_NEW_TYPE_STRUCT = rffi.cast(MuFlag, 0x308)
IRBUILDER_NEW_TYPE_HYBRID = rffi.cast(MuFlag, 0x309)
IRBUILDER_NEW_TYPE_ARRAY = rffi.cast(MuFlag, 0x30a)
IRBUILDER_NEW_TYPE_VECTOR = rffi.cast(MuFlag, 0x30b)
IRBUILDER_NEW_TYPE_VOID = rffi.cast(MuFlag, 0x30c)
IRBUILDER_NEW_TYPE_REF = rffi.cast(MuFlag, 0x30d)
IRBUILDER_NEW_TYPE_IREF = rffi.cast(MuFlag, 0x30e)
IRBUILDER_NEW_TYPE_WEAKREF = rffi.cast(MuFlag, 0x30f)
IRBUILDER_NEW_TYPE_FUNCREF = rffi.cast(MuFlag, 0x310)
IRBUILDER_NEW_TYPE_TAGREF64 = rffi.cast(MuFlag, 0x311)
IRBUILDER_NEW_TYPE_THREADREF = rffi.cast(MuFlag, 0x312)
IRBUILDER_NEW_TYPE_STACKREF = rffi.cast(MuFlag, 0x313)
IRBUILDER_NEW_TYPE_FRAMECURSORREF = rffi.cast(MuFlag, 0x314)
IRBUILDER_NEW_TYPE_IRBUILDERREF = rffi.cast(MuFlag, 0x315)
IRBUILDER_NEW_FUNCSIG = rffi.cast(MuFlag, 0x316)
IRBUILDER_NEW_CONST_INT = rffi.cast(MuFlag, 0x317)
IRBUILDER_NEW_CONST_INT_EX = rffi.cast(MuFlag, 0x318)
IRBUILDER_NEW_CONST_FLOAT = rffi.cast(MuFlag, 0x319)
IRBUILDER_NEW_CONST_DOUBLE = rffi.cast(MuFlag, 0x31a)
IRBUILDER_NEW_CONST_NULL = rffi.cast(MuFlag, 0x31b)
IRBUILDER_NEW_CONST_SEQ = rffi.cast(MuFlag, 0x31c)
IRBUILDER_NEW_CONST_EXTERN = rffi.cast(MuFlag, 0x31d)
IRBUILDER_NEW_GLOBAL_CELL = rffi.cast(MuFlag, 0x31e)
IRBUILDER_NEW_FUNC = rffi.cast(MuFlag, 0x31f)
IRBUILDER_NEW_EXP_FUNC = rffi.cast(MuFlag, 0x320)
IRBUILDER_NEW_FUNC_VER = rffi.cast(MuFlag, 0x321)
IRBUILDER_NEW_BB = rffi.cast(MuFlag, 0x322)
IRBUILDER_NEW_DEST_CLAUSE = rffi.cast(MuFlag, 0x323)
IRBUILDER_NEW_EXC_CLAUSE = rffi.cast(MuFlag, 0x324)
IRBUILDER_NEW_KEEPALIVE_CLAUSE = rffi.cast(MuFlag, 0x325)
IRBUILDER_NEW_CSC_RET_WITH = rffi.cast(MuFlag, 0x326)
IRBUILDER_NEW_CSC_KILL_OLD = rffi.cast(MuFlag, 0x327)
IRBUILDER_NEW_NSC_PASS_VALUES = rffi.cast(MuFlag, 0x328)
IRBUILDER_NEW_NSC_THROW_EXC = rffi.cast(MuFlag, 0x329)
IRBUILDER_NEW_BINOP = rffi.cast(MuFlag, 0x32a)
IRBUILDER_NEW_BINOP_WITH_STATUS = rffi.cast(MuFlag, 0x32b)
IRBUILDER_NEW_CMP = rffi.cast(MuFlag, 0x32c)
IRBUILDER_NEW_CONV = rffi.cast(MuFlag, 0x32d)
IRBUILDER_NEW_SELECT = rffi.cast(MuFlag, 0x32e)
IRBUILDER_NEW_BRANCH = rffi.cast(MuFlag, 0x32f)
IRBUILDER_NEW_BRANCH2 = rffi.cast(MuFlag, 0x330)
IRBUILDER_NEW_SWITCH = rffi.cast(MuFlag, 0x331)
IRBUILDER_NEW_CALL = rffi.cast(MuFlag, 0x332)
IRBUILDER_NEW_TAILCALL = rffi.cast(MuFlag, 0x333)
IRBUILDER_NEW_RET = rffi.cast(MuFlag, 0x334)
IRBUILDER_NEW_THROW = rffi.cast(MuFlag, 0x335)
IRBUILDER_NEW_EXTRACTVALUE = rffi.cast(MuFlag, 0x336)
IRBUILDER_NEW_INSERTVALUE = rffi.cast(MuFlag, 0x337)
IRBUILDER_NEW_EXTRACTELEMENT = rffi.cast(MuFlag, 0x338)
IRBUILDER_NEW_INSERTELEMENT = rffi.cast(MuFlag, 0x339)
IRBUILDER_NEW_SHUFFLEVECTOR = rffi.cast(MuFlag, 0x33a)
IRBUILDER_NEW_NEW = rffi.cast(MuFlag, 0x33b)
IRBUILDER_NEW_NEWHYBRID = rffi.cast(MuFlag, 0x33c)
IRBUILDER_NEW_ALLOCA = rffi.cast(MuFlag, 0x33d)
IRBUILDER_NEW_ALLOCAHYBRID = rffi.cast(MuFlag, 0x33e)
IRBUILDER_NEW_GETIREF = rffi.cast(MuFlag, 0x33f)
IRBUILDER_NEW_GETFIELDIREF = rffi.cast(MuFlag, 0x340)
IRBUILDER_NEW_GETELEMIREF = rffi.cast(MuFlag, 0x341)
IRBUILDER_NEW_SHIFTIREF = rffi.cast(MuFlag, 0x342)
IRBUILDER_NEW_GETVARPARTIREF = rffi.cast(MuFlag, 0x343)
IRBUILDER_NEW_LOAD = rffi.cast(MuFlag, 0x344)
IRBUILDER_NEW_STORE = rffi.cast(MuFlag, 0x345)
IRBUILDER_NEW_CMPXCHG = rffi.cast(MuFlag, 0x346)
IRBUILDER_NEW_ATOMICRMW = rffi.cast(MuFlag, 0x347)
IRBUILDER_NEW_FENCE = rffi.cast(MuFlag, 0x348)
IRBUILDER_NEW_TRAP = rffi.cast(MuFlag, 0x349)
IRBUILDER_NEW_WATCHPOINT = rffi.cast(MuFlag, 0x34a)
IRBUILDER_NEW_WPBRANCH = rffi.cast(MuFlag, 0x34b)
IRBUILDER_NEW_CCALL = rffi.cast(MuFlag, 0x34c)
IRBUILDER_NEW_NEWTHREAD = rffi.cast(MuFlag, 0x34d)
IRBUILDER_NEW_SWAPSTACK = rffi.cast(MuFlag, 0x34e)
IRBUILDER_NEW_COMMINST = rffi.cast(MuFlag, 0x34f)
EXT_PRINT_STATS = rffi.cast(MuFlag, 0xc001)
EXT_CLEAR_STATS = rffi.cast(MuFlag, 0xc002)
@always_inline
def ir_inst(inst, *args, **kwargs):
......@@ -125,12 +371,41 @@ class Entry(ExtRegistryEntry):
for idx in i_args:
args.append(hop.inputarg(hop.args_r[idx], idx))
i_restype = i_kwds.get('i_restype', None)
restype = hop.args_v[i_restype].value if i_restype else lltype.Void
restype = hop.r_result.lowleveltype # it's perhaps better to do it this way
metainfo = {}
args.append(hop.inputconst(lltype.Void, metainfo))
return hop.genop('mu_comminst', args, resulttype=restype)
i_opname = i_kwds.get('i_opname', None)
opname = hop.args_v[i_opname].value if i_opname else 'mu_comminst'
return hop.genop(opname, args, resulttype=restype)
def meta_id_of(name):
name_irstr = str2metacstr(name)
return ir_inst(MetaInst.ID_OF, name_irstr, restype=MuID)
def meta_name_of(id):
return ir_inst(MetaInst.NAME_OF, id, restype=MuMetaCStr)
@specialize.arg(0)
def meta_const_by_id(TYPE, id):
return ir_inst(MetaInst.CONSTANT_BY_ID, id, restype=TYPE, opname='mu_meta_xxx_by_id') # further processing in ll2mu
@specialize.arg(0)
def meta_global_by_id(T, id):
IREF = _iref_type_of(T)
return ir_inst(MetaInst.GLOBAL_BY_ID, id, restype=IREF, opname='mu_meta_xxx_by_id')
@specialize.arg(0)
def meta_func_by_id(TYPE, id):
return ir_inst(MetaInst.FUNC_BY_ID, id, restype=TYPE, opname='mu_meta_xxx_by_id')
@specialize.arg(0)
def meta_expfunc_by_id(TYPE, id):
UFUNCPTR = _expfunc_type_of(TYPE)
return ir_inst(MetaInst.EXPFUNC_BY_ID, id, restype=UFUNCPTR, opname='mu_meta_xxx_by_id')
class MuIRBuilder:
......@@ -143,7 +418,7 @@ class MuIRBuilder:
def abort(self):
ir_inst(MetaIRBuilderInst.ABORT, self._bldr)
def gen_sym(self, name):
def gen_sym(self, name=None):
_name = str2metacstr(name)
return ir_inst(MetaIRBuilderInst.GEN_SYM, self._bldr, _name, restype=MuID)
......@@ -471,25 +746,38 @@ MuMetaCStr = lltype.Ptr(lltype.GcOpaqueType('MuMetaCStr',
hints={'mu_render_ptr_as': mutype.META_CSTR}))
def str2metacstr(str):
n = len(str)
buf = lltype.malloc(_StrBuf, n + 1)
adr_buf = llmemory.cast_ptr_to_adr(buf) + llmemory.itemoffsetof(_StrBuf, 0)
adr_src = llmemory.cast_ptr_to_adr(str) + (llmemory.offsetof(rstr.STR, 'chars') +
llmemory.itemoffsetof(rstr.STR.chars, 0) +
llmemory.sizeof(rstr.Char) * 0)
llmemory.raw_memcopy(adr_src, adr_buf, llmemory.sizeof(rstr.Char) * n)
buf[n] = '\x00'
keepalive_until_here(buf)
keepalive_until_here(str)
ircstr = llop.mu_meta_barebuf2cstriref(MuMetaCStr, buf) # handled in ll2mu
return ircstr
if str:
n = len(str)
buf = lltype.malloc(_StrBuf, n + 1)
adr_buf = llmemory.cast_ptr_to_adr(buf) + llmemory.itemoffsetof(_StrBuf, 0)
adr_src = llmemory.cast_ptr_to_adr(str) + (llmemory.offsetof(rstr.STR, 'chars') +
llmemory.itemoffsetof(rstr.STR.chars, 0) +
llmemory.sizeof(rstr.Char) * 0)
llmemory.raw_memcopy(adr_src, adr_buf, llmemory.sizeof(rstr.Char) * n)
buf[n] = '\x00'
keepalive_until_here(buf)
keepalive_until_here(str)
ircstr = llop.mu_meta_barebuf2cstriref(MuMetaCStr, buf) # handled in ll2mu
return ircstr
else:
return lltype.nullptr(MuMetaCStr.TO)
@specialize.memo()
def _carriptr_type_of(ELM_T):
from rpython.translator.mu.ll2mu import ll2mu_type
ELM_MuT = ll2mu_type(ELM_T)
return lltype.Ptr(lltype.GcOpaqueType('itemarray', hints={'mu_render_ptr_as': mutype.MuIRef(ELM_MuT)}))
@specialize.memo()
def _iref_type_of(T):
T_MuT = ll2mu_type(T)
return lltype.Ptr(lltype.GcOpaqueType('iref', hints={'mu_render_ptr_as': mutype.MuIRef(T_MuT)}))
@specialize.memo()
def _expfunc_type_of(TYPE):
Sig = ll2mu_type(TYPE).Sig
return lltype.Ptr(lltype.GcOpaqueType('ufuncptr', hints={'mu_render_ptr_as': mutype.MuUFuncPtr(Sig)}))
def lst2metacarr(ELM_T, lst):
IPtrBuf = _carriptr_type_of(ELM_T)
return llop.mu_meta_lst2carr(IPtrBuf, lst) # handled in ll2mu
......@@ -2,18 +2,20 @@ from rpython.rlib.rmu import holstein_meta as rmu
from rpython.translator.interactive import Translation
from rpython.translator.mu.tool.textgraph import print_graph
from rpython.translator.mu.test.test_mutyper import graph_of
from rpython.translator.mu import mutype
from rpython.rtyper.lltypesystem import lltype
from rpython.translator.mu import mutype, ll2mu
from rpython.rtyper.lltypesystem import lltype, rffi
def test_str2metacstr():
t = Translation(rmu.str2metacstr, [str], backend='mu')
t.backendopt(mallocs=True, inline=True)
g = graph_of(rmu.str2metacstr, t)
print_graph(g)
t.mutype()
print_graph(g)
assert [op.opname for op in g.startblock.operations[-3:-1]] == ['mu_getiref', 'mu_getvarpartiref']
assert isinstance(g.startblock.operations[-3].result.concretetype, mutype.MuIRef)
blk = g.startblock.exits[1].target
assert [op.opname for op in blk.operations[-3:-1]] == ['mu_getiref', 'mu_getvarpartiref']
assert isinstance(blk.operations[-3].result.concretetype, mutype.MuIRef)
def test_lst2metaarr():
......@@ -58,58 +60,85 @@ def test_type_translation():
print_graph(g)
def test_compile_calls():
from rpython.translator.mu.tool.textgraph import save_graphs
from rpython.rlib.jit import hint
def main(argv):
"""
Builds the following test bundle.
.typedef @i8 = int<8>
.const @0xff_i8 <@i8> = 0xff
.const @0x0a_i8 <@i8> = 0x0a
.funcsig @sig__i8 = () -> (@i8)
.funcdecl @test_fnc <@fnrsig__i8>
.funcdef @test_fnc VERSION @test_fnc_v1 <@sig__i8> {
@test_fnc_v1.blk0():
@test_fnc_v1.blk0.res = ADD <@i8> @0xff_i8 @0x0a_i8
RET @test_fnc_v1.blk0.res
}
"""
hint(0, rmu_meta=True)
mu = rmu.MuVM()
ctx = mu.new_context()
bldr = ctx.new_ir_builder()
i8 = bldr.gen_sym("@i8")
bldr.new_type_int(i8, 8)
def test_func_by_id():
FUNCPTR = lltype.Ptr(lltype.FuncType([lltype.Signed], lltype.Signed))
def f(id):
return rmu.meta_func_by_id(FUNCPTR, id)
c_0xff_i8 = bldr.gen_sym("@0xff_i8")
bldr.new_const_int(c_0xff_i8, i8, 0xff)
c_0x0a_i8 = bldr.gen_sym("@0x0a_i8")
bldr.new_const_int(c_0x0a_i8, i8, 0x0a)
t = Translation(f, [rmu.MuID], backend='mu')
t.rtype()
h = graph_of(rmu.meta_func_by_id, t)
assert h.startblock.operations[0].opname == 'mu_meta_xxx_by_id'
t.mutype()
g = graph_of(f, t)
print_graph(g)
sig__i8 = bldr.gen_sym("@sig__i8")
bldr.new_funcsig(sig__i8, [], [i8])
op = g.startblock.operations[0]
meta_info = op.args[-1].value
assert 'types' in meta_info
assert meta_info['types'] == [ll2mu.ll2mu_type(FUNCPTR)]
test_fnc = bldr.gen_sym("@test_fnc")
bldr.new_func(test_fnc, sig__i8)
# function body
v1 = bldr.gen_sym("@test_fnc_v1")
blk0 = bldr.gen_sym("@test_fnc_v1.blk0")
res = bldr.gen_sym("@test_fnc_v1.blk0.res")
op_binop = bldr.gen_sym()
bldr.new_binop(op_binop, res, rmu.MuBinOptr.ADD, i8, c_0xff_i8, c_0x0a_i8)
op_ret = bldr.gen_sym()
bldr.new_ret(op_ret, [res])
bldr.new_bb(blk0, [], [], rmu.MU_NO_ID, [op_binop, op_ret])
bldr.new_func_ver(v1, test_fnc, [blk0])
def test_compile_calls():
import py
def build_func(bldr):
"""
Builds the following test bundle.
.const @c_1 <@i64> = 1
.funcsig @sig_i64_i64 = (@i64) -> (i@64)
.func @inc VERSION @inc.v1 <@sig_i64_i64> {
%blk0(<@i64> %a):
%a_suc = ADD <@i64> %a @c_1
RET %a_suc
}
:type bldr: rpython.rlib.rmu.holstein_meta.MuIRBuilder
"""
i64 = rmu.meta_id_of("@i64")
c_1 = bldr.gen_sym("@c_1"); bldr.new_const_int(c_1, i64, 1)
sig_i64_i64 = bldr.gen_sym("@sig_i64_i64"); bldr.new_funcsig(sig_i64_i64, [i64], [i64])
stt = bldr.gen_sym(); bldr.new_type_struct(stt, [i64])
inc = bldr.gen_sym("@inc"); bldr.new_func(inc, sig_i64_i64)
blk0 = bldr.gen_sym("@inc.blk0")
a = bldr.gen_sym("@inc.blk0.a")
a_suc = bldr.gen_sym("@inc.blk0.a_suc")
op_add = bldr.gen_sym(); bldr.new_binop(op_add, a_suc, rmu.MuBinOptr.ADD, i64, a, c_1)
op_ret = bldr.gen_sym(); bldr.new_ret(op_ret, [a_suc])
bldr.new_bb(blk0, [a], [i64], rmu.MU_NO_ID, [op_add, op_ret])
bldr.new_func_ver(bldr.gen_sym(), inc, [blk0])
return inc
FUNCPTR = lltype.Ptr(lltype.FuncType([lltype.Signed], lltype.Signed))
def main(argc, argv):
bldr = rmu.MuIRBuilder()
id_fnc = build_func(bldr)
bldr.load()
hint(0, rmu_meta=False)
a = rffi.cast(lltype.Signed, argc)
inc = rmu.meta_func_by_id(FUNCPTR, id_fnc)
a_inc = inc(a)
print a_inc
return 0
t = Translation(main, None, backend='mu')
t.backendopt(mallocs=True)
t = Translation(main, [rffi.INT, rffi.CCHARPP], backend='mu', impl='holstein', codegen='c')
t.driver.standalone = True # force standalone
t.driver.exe_name = '/tmp/test_compile_calls-mu'
t.driver.disable(['entrypoint_mu'])
t.annotate()
# t.backendopt(mallocs=True)
t.mutype()
t.compile_mu()
img = py.path.local(t.driver.exe_name)
from rpython.rlib.rmu import holstein
from rpython.translator.platform import platform, log as log_platform
save_graphs(t.context.graphs)
runmu = py.path.local(holstein.mu_dir).join('..', 'tools', 'runmu.sh')
flags = ['--vmLog=ERROR', '--losSize=780M', '--sosSize=780M']
args = ['1']
log_platform.execute(' '.join([str(runmu)] + flags + [str(img)] + args))
res = platform.execute(runmu, flags + [str(img)] + args)
assert res.returncode == 0, res.err
assert res.out == '2\n'
......@@ -249,6 +249,9 @@ class MuBundleGen:
self.bdr.new_type_uptr(_id, self._id_of(T.TO))
elif isinstance(T, mutype.MuWeakRef):
self.bdr.new_type_weakref(_id, self._id_of(T.TO))
elif isinstance(T, mutype.MuOpaqueRef):
if T is mutype.MUTA_IRBUILDER_REF:
self.bdr.new_type_irbuilderref(_id)
def gen_consts(self):
self.log.gen_types("declaring %d constants..." % (len(self.db.consts) + len(self.db.extern_fncs)))
......@@ -479,6 +482,9 @@ class MuBundleGen:
flags, types, sigs,
self._ids_of(op.args[1:-1]))
def _genop_mu_meta_callinternal(self, op, op_id):
pass
def _id_of(self, entity):
# look up id of the entity, if non-existent, call gen_sym
_id = self.idmap[entity]
......
......@@ -1607,6 +1607,14 @@ class LL2MuMapper:
ops.extend(_ops)
return ops