def _call_mem_eq(i, **kwargs): if (not isinstance(i.src_vars[1], _mem.ConstVar) or not issubclass(i.src_vars[1].var_type, ctypes.Array)): raise TranslationError( i.starts_line, 'first arg to mem_eq must be const ctypes.Array') ret = [] # stack=None because we shouldn't need to allocate anything ret.extend(_lea(i, i.src_vars[2], bi.Reg.R2, stack=None)) false, done = _make_tmp_label(), _make_tmp_label() for off, v in enumerate(i.src_vars[1].val.value): ret.extend([ bi.Mov(bi.Mem(bi.Reg.R2, off, bi.Size.Byte), bi.Reg.R1), bi.JumpIfNotEqual(bi.Imm(v), bi.Reg.R1, false) ]) # If we made it here, it's a match ret.extend(_mov(bi.Imm(1), i.dst_vars[0])) ret.append(bi.Jump(done)) # if we jumped here, it's not a match ret.append(bi.Label(false)) ret.extend(_mov(bi.Imm(0), i.dst_vars[0])) ret.append(bi.Label(done)) return ret
def _binary_subscr_map(i, **kwargs): m, k, dv = i.src_vars[0], i.src_vars[1], i.dst_vars[0] if not isinstance(m, _mem.ConstVar): raise TranslationError(i.starts_line, 'Cannot subscript dynamically select map') found, done = _make_tmp_label(), _make_tmp_label() ret = [] ret.extend(_mov(bi.MapFdImm(m.val.fd), bi.Reg.R1)) ret.extend(_lea(i, k, bi.Reg.R2, **kwargs)) ret.extend([ bi.Call(bi.Imm(funcs.map_lookup_elem.num)), ]) if _is_ptr(dv.var_type): sz = _get_cdata_size(dv.var_type) ret.extend(_mov(bi.Reg.R0, dv)) else: ret.extend([ bi.JumpIfNotEqual(bi.Imm(0), bi.Reg.R0, found), ]) # Move default value dr = _get_var_reg(dv) ret.extend(_mov_const(dv.var_type, m.val.DEFAULT_VALUE, dr, dv.offset)) ret.extend([ bi.Jump(done), bi.Label(found), ]) # Primitives by value, others by reference if issubclass(dv.var_type, _ctypes._SimpleCData): sz = _get_cdata_size(dv.var_type) ret.extend(_mov(bi.Mem(bi.Reg.R0, 0, sz), dv)) else: ret.extend(_mov(bi.Reg.R0, dv)) ret.append(bi.Label(done)) return ret
def _compare_op(i, **kwargs): lhs, rhs = i.src_vars op = i.argval if op == '<': lhs, rhs = rhs, lhs op = '>' elif op == '<=': lhs, rhs = rhs, lhs op = '>=' jmp_type = { '>': bi.JumpIfGreaterThan, '>=': bi.JumpIfGreaterOrEqual, '==': bi.JumpIfEqual, '!=': bi.JumpIfNotEqual, }[op] true, done = _make_tmp_label(), _make_tmp_label() return (_mov(lhs, bi.Reg.R1) + _mov(rhs, bi.Reg.R2) + [jmp_type(bi.Reg.R1, bi.Reg.R2, true)] + _mov(bi.Imm(0), i.dst_vars[0]) + [bi.Jump(done)] + [bi.Label(true)] + _mov(bi.Imm(1), i.dst_vars[0]) + [bi.Label(done)])
def _jump_forward(i, **kwargs): return [bi.Jump('label_{}'.format(i.argval))]