|
|
@@ -135,7 +135,7 @@
|
|
|
|
|
|
#define EMIT_NATIVE_VIPER_TYPE_ERROR(emit, ...) do { \
|
|
|
*emit->error_slot = mp_obj_new_exception_msg_varg(&mp_type_ViperTypeError, __VA_ARGS__); \
|
|
|
- } while (0)
|
|
|
+} while (0)
|
|
|
|
|
|
typedef enum {
|
|
|
STACK_VALUE,
|
|
|
@@ -163,15 +163,25 @@ typedef enum {
|
|
|
|
|
|
STATIC qstr vtype_to_qstr(vtype_kind_t vtype) {
|
|
|
switch (vtype) {
|
|
|
- case VTYPE_PYOBJ: return MP_QSTR_object;
|
|
|
- case VTYPE_BOOL: return MP_QSTR_bool;
|
|
|
- case VTYPE_INT: return MP_QSTR_int;
|
|
|
- case VTYPE_UINT: return MP_QSTR_uint;
|
|
|
- case VTYPE_PTR: return MP_QSTR_ptr;
|
|
|
- case VTYPE_PTR8: return MP_QSTR_ptr8;
|
|
|
- case VTYPE_PTR16: return MP_QSTR_ptr16;
|
|
|
- case VTYPE_PTR32: return MP_QSTR_ptr32;
|
|
|
- case VTYPE_PTR_NONE: default: return MP_QSTR_None;
|
|
|
+ case VTYPE_PYOBJ:
|
|
|
+ return MP_QSTR_object;
|
|
|
+ case VTYPE_BOOL:
|
|
|
+ return MP_QSTR_bool;
|
|
|
+ case VTYPE_INT:
|
|
|
+ return MP_QSTR_int;
|
|
|
+ case VTYPE_UINT:
|
|
|
+ return MP_QSTR_uint;
|
|
|
+ case VTYPE_PTR:
|
|
|
+ return MP_QSTR_ptr;
|
|
|
+ case VTYPE_PTR8:
|
|
|
+ return MP_QSTR_ptr8;
|
|
|
+ case VTYPE_PTR16:
|
|
|
+ return MP_QSTR_ptr16;
|
|
|
+ case VTYPE_PTR32:
|
|
|
+ return MP_QSTR_ptr32;
|
|
|
+ case VTYPE_PTR_NONE:
|
|
|
+ default:
|
|
|
+ return MP_QSTR_None;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -244,7 +254,7 @@ STATIC void emit_native_global_exc_entry(emit_t *emit);
|
|
|
STATIC void emit_native_global_exc_exit(emit_t *emit);
|
|
|
STATIC void emit_native_load_const_obj(emit_t *emit, mp_obj_t obj);
|
|
|
|
|
|
-emit_t *EXPORT_FUN(new)(mp_obj_t *error_slot, uint *label_slot, mp_uint_t max_num_labels) {
|
|
|
+emit_t *EXPORT_FUN(new)(mp_obj_t * error_slot, uint *label_slot, mp_uint_t max_num_labels) {
|
|
|
emit_t *emit = m_new0(emit_t, 1);
|
|
|
emit->error_slot = error_slot;
|
|
|
emit->label_slot = label_slot;
|
|
|
@@ -257,7 +267,7 @@ emit_t *EXPORT_FUN(new)(mp_obj_t *error_slot, uint *label_slot, mp_uint_t max_nu
|
|
|
return emit;
|
|
|
}
|
|
|
|
|
|
-void EXPORT_FUN(free)(emit_t *emit) {
|
|
|
+void EXPORT_FUN(free)(emit_t * emit) {
|
|
|
mp_asm_base_deinit(&emit->as->base, false);
|
|
|
m_del_obj(ASM_T, emit->as);
|
|
|
m_del(exc_stack_entry_t, emit->exc_stack, emit->exc_stack_alloc);
|
|
|
@@ -733,14 +743,14 @@ STATIC void adjust_stack(emit_t *emit, mp_int_t stack_size_delta) {
|
|
|
if (emit->pass > MP_PASS_SCOPE && emit->stack_size > emit->scope->stack_size) {
|
|
|
emit->scope->stack_size = emit->stack_size;
|
|
|
}
|
|
|
-#ifdef DEBUG_PRINT
|
|
|
+ #ifdef DEBUG_PRINT
|
|
|
DEBUG_printf(" adjust_stack; stack_size=%d+%d; stack now:", emit->stack_size - stack_size_delta, stack_size_delta);
|
|
|
for (int i = 0; i < emit->stack_size; i++) {
|
|
|
stack_info_t *si = &emit->stack_info[i];
|
|
|
DEBUG_printf(" (v=%d k=%d %d)", si->vtype, si->kind, si->data.u_reg);
|
|
|
}
|
|
|
DEBUG_printf("\n");
|
|
|
-#endif
|
|
|
+ #endif
|
|
|
}
|
|
|
|
|
|
STATIC void emit_native_adjust_stack_size(emit_t *emit, mp_int_t delta) {
|
|
|
@@ -831,7 +841,7 @@ STATIC vtype_kind_t load_reg_stack_imm(emit_t *emit, int reg_dest, const stack_i
|
|
|
} else if (si->vtype == VTYPE_PTR_NONE) {
|
|
|
emit_native_mov_reg_const(emit, reg_dest, MP_F_CONST_NONE_OBJ);
|
|
|
} else {
|
|
|
- mp_raise_NotImplementedError("conversion to object");
|
|
|
+ mp_raise_NotImplementedError(MP_ERROR_TEXT("conversion to object"));
|
|
|
}
|
|
|
return VTYPE_PYOBJ;
|
|
|
}
|
|
|
@@ -1132,8 +1142,8 @@ STATIC void emit_native_label_assign(emit_t *emit, mp_uint_t l) {
|
|
|
|
|
|
bool is_finally = false;
|
|
|
if (emit->exc_stack_size > 0) {
|
|
|
- exc_stack_entry_t *e = &emit->exc_stack[emit->exc_stack_size - 1];
|
|
|
- is_finally = e->is_finally && e->label == l;
|
|
|
+ exc_stack_entry_t *e = &emit->exc_stack[emit->exc_stack_size - 1];
|
|
|
+ is_finally = e->is_finally && e->label == l;
|
|
|
}
|
|
|
|
|
|
if (is_finally) {
|
|
|
@@ -1405,7 +1415,7 @@ STATIC void emit_native_load_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
|
|
|
DEBUG_printf("load_fast(%s, " UINT_FMT ")\n", qstr_str(qst), local_num);
|
|
|
vtype_kind_t vtype = emit->local_vtype[local_num];
|
|
|
if (vtype == VTYPE_UNBOUND) {
|
|
|
- EMIT_NATIVE_VIPER_TYPE_ERROR(emit, "local '%q' used before type known", qst);
|
|
|
+ EMIT_NATIVE_VIPER_TYPE_ERROR(emit, MP_ERROR_TEXT("local '%q' used before type known"), qst);
|
|
|
}
|
|
|
emit_native_pre(emit);
|
|
|
if (local_num < REG_LOCAL_NUM && CAN_USE_REGS_FOR_LOCALS(emit)) {
|
|
|
@@ -1580,7 +1590,7 @@ STATIC void emit_native_load_subscr(emit_t *emit) {
|
|
|
}
|
|
|
default:
|
|
|
EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
|
|
|
- "can't load from '%q'", vtype_to_qstr(vtype_base));
|
|
|
+ MP_ERROR_TEXT("can't load from '%q'"), vtype_to_qstr(vtype_base));
|
|
|
}
|
|
|
} else {
|
|
|
// index is not an immediate
|
|
|
@@ -1590,7 +1600,7 @@ STATIC void emit_native_load_subscr(emit_t *emit) {
|
|
|
emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1);
|
|
|
if (vtype_index != VTYPE_INT && vtype_index != VTYPE_UINT) {
|
|
|
EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
|
|
|
- "can't load with '%q' index", vtype_to_qstr(vtype_index));
|
|
|
+ MP_ERROR_TEXT("can't load with '%q' index"), vtype_to_qstr(vtype_index));
|
|
|
}
|
|
|
switch (vtype_base) {
|
|
|
case VTYPE_PTR8: {
|
|
|
@@ -1618,7 +1628,7 @@ STATIC void emit_native_load_subscr(emit_t *emit) {
|
|
|
}
|
|
|
default:
|
|
|
EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
|
|
|
- "can't load from '%q'", vtype_to_qstr(vtype_base));
|
|
|
+ MP_ERROR_TEXT("can't load from '%q'"), vtype_to_qstr(vtype_base));
|
|
|
}
|
|
|
}
|
|
|
emit_post_push_reg(emit, VTYPE_INT, REG_RET);
|
|
|
@@ -1642,7 +1652,7 @@ STATIC void emit_native_store_fast(emit_t *emit, qstr qst, mp_uint_t local_num)
|
|
|
} else if (emit->local_vtype[local_num] != vtype) {
|
|
|
// type of local is not the same as object stored in it
|
|
|
EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
|
|
|
- "local '%q' has type '%q' but source is '%q'",
|
|
|
+ MP_ERROR_TEXT("local '%q' has type '%q' but source is '%q'"),
|
|
|
qst, vtype_to_qstr(emit->local_vtype[local_num]), vtype_to_qstr(vtype));
|
|
|
}
|
|
|
}
|
|
|
@@ -1743,7 +1753,7 @@ STATIC void emit_native_store_subscr(emit_t *emit) {
|
|
|
#endif
|
|
|
if (vtype_value != VTYPE_BOOL && vtype_value != VTYPE_INT && vtype_value != VTYPE_UINT) {
|
|
|
EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
|
|
|
- "can't store '%q'", vtype_to_qstr(vtype_value));
|
|
|
+ MP_ERROR_TEXT("can't store '%q'"), vtype_to_qstr(vtype_value));
|
|
|
}
|
|
|
switch (vtype_base) {
|
|
|
case VTYPE_PTR8: {
|
|
|
@@ -1809,7 +1819,7 @@ STATIC void emit_native_store_subscr(emit_t *emit) {
|
|
|
}
|
|
|
default:
|
|
|
EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
|
|
|
- "can't store to '%q'", vtype_to_qstr(vtype_base));
|
|
|
+ MP_ERROR_TEXT("can't store to '%q'"), vtype_to_qstr(vtype_base));
|
|
|
}
|
|
|
} else {
|
|
|
// index is not an immediate
|
|
|
@@ -1820,7 +1830,7 @@ STATIC void emit_native_store_subscr(emit_t *emit) {
|
|
|
emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1);
|
|
|
if (vtype_index != VTYPE_INT && vtype_index != VTYPE_UINT) {
|
|
|
EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
|
|
|
- "can't store with '%q' index", vtype_to_qstr(vtype_index));
|
|
|
+ MP_ERROR_TEXT("can't store with '%q' index"), vtype_to_qstr(vtype_index));
|
|
|
}
|
|
|
#if N_X86
|
|
|
// special case: x86 needs byte stores to be from lower 4 regs (REG_ARG_3 is EDX)
|
|
|
@@ -1830,7 +1840,7 @@ STATIC void emit_native_store_subscr(emit_t *emit) {
|
|
|
#endif
|
|
|
if (vtype_value != VTYPE_BOOL && vtype_value != VTYPE_INT && vtype_value != VTYPE_UINT) {
|
|
|
EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
|
|
|
- "can't store '%q'", vtype_to_qstr(vtype_value));
|
|
|
+ MP_ERROR_TEXT("can't store '%q'"), vtype_to_qstr(vtype_value));
|
|
|
}
|
|
|
switch (vtype_base) {
|
|
|
case VTYPE_PTR8: {
|
|
|
@@ -1870,7 +1880,7 @@ STATIC void emit_native_store_subscr(emit_t *emit) {
|
|
|
}
|
|
|
default:
|
|
|
EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
|
|
|
- "can't store to '%q'", vtype_to_qstr(vtype_base));
|
|
|
+ MP_ERROR_TEXT("can't store to '%q'"), vtype_to_qstr(vtype_base));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -1992,7 +2002,7 @@ STATIC void emit_native_jump_helper(emit_t *emit, bool cond, mp_uint_t label, bo
|
|
|
}
|
|
|
if (!(vtype == VTYPE_BOOL || vtype == VTYPE_INT || vtype == VTYPE_UINT)) {
|
|
|
EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
|
|
|
- "can't implicitly convert '%q' to 'bool'", vtype_to_qstr(vtype));
|
|
|
+ MP_ERROR_TEXT("can't implicitly convert '%q' to 'bool'"), vtype_to_qstr(vtype));
|
|
|
}
|
|
|
}
|
|
|
// For non-pop need to save the vtype so that emit_native_adjust_stack_size
|
|
|
@@ -2058,7 +2068,7 @@ STATIC void emit_native_unwind_jump(emit_t *emit, mp_uint_t label, mp_uint_t exc
|
|
|
ASM_MOV_REG_PCREL(emit->as, REG_RET, label & ~MP_EMIT_BREAK_FROM_FOR);
|
|
|
ASM_MOV_LOCAL_REG(emit->as, LOCAL_IDX_EXC_HANDLER_UNWIND(emit), REG_RET);
|
|
|
// Cancel any active exception (see also emit_native_pop_except_jump)
|
|
|
- emit_native_mov_reg_const(emit, REG_RET, MP_F_CONST_NONE_OBJ);
|
|
|
+ ASM_MOV_REG_IMM(emit->as, REG_RET, (mp_uint_t)MP_OBJ_NULL);
|
|
|
ASM_MOV_LOCAL_REG(emit->as, LOCAL_IDX_EXC_VAL(emit), REG_RET);
|
|
|
// Jump to the innermost active finally
|
|
|
label = first_finally->label;
|
|
|
@@ -2153,9 +2163,8 @@ STATIC void emit_native_with_cleanup(emit_t *emit, mp_uint_t label) {
|
|
|
|
|
|
ASM_MOV_REG_LOCAL(emit->as, REG_ARG_1, LOCAL_IDX_EXC_VAL(emit)); // get exc
|
|
|
|
|
|
- // Check if exc is None and jump to non-exc handler if it is
|
|
|
- emit_native_mov_reg_const(emit, REG_ARG_2, MP_F_CONST_NONE_OBJ);
|
|
|
- ASM_JUMP_IF_REG_EQ(emit->as, REG_ARG_1, REG_ARG_2, *emit->label_slot + 2);
|
|
|
+ // Check if exc is MP_OBJ_NULL (i.e. zero) and jump to non-exc handler if it is
|
|
|
+ ASM_JUMP_IF_REG_ZERO(emit->as, REG_ARG_1, *emit->label_slot + 2, false);
|
|
|
|
|
|
ASM_LOAD_REG_REG_OFFSET(emit->as, REG_ARG_2, REG_ARG_1, 0); // get type(exc)
|
|
|
emit_post_push_reg(emit, VTYPE_PYOBJ, REG_ARG_2); // push type(exc)
|
|
|
@@ -2175,9 +2184,9 @@ STATIC void emit_native_with_cleanup(emit_t *emit, mp_uint_t label) {
|
|
|
emit_call(emit, MP_F_OBJ_IS_TRUE);
|
|
|
ASM_JUMP_IF_REG_ZERO(emit->as, REG_RET, *emit->label_slot + 1, true);
|
|
|
|
|
|
- // Replace exception with None
|
|
|
+ // Replace exception with MP_OBJ_NULL.
|
|
|
emit_native_label_assign(emit, *emit->label_slot);
|
|
|
- emit_native_mov_reg_const(emit, REG_TEMP0, MP_F_CONST_NONE_OBJ);
|
|
|
+ ASM_MOV_REG_IMM(emit->as, REG_TEMP0, (mp_uint_t)MP_OBJ_NULL);
|
|
|
ASM_MOV_LOCAL_REG(emit->as, LOCAL_IDX_EXC_VAL(emit), REG_TEMP0);
|
|
|
|
|
|
// end of with cleanup nlr_catch block
|
|
|
@@ -2255,7 +2264,7 @@ STATIC void emit_native_for_iter_end(emit_t *emit) {
|
|
|
STATIC void emit_native_pop_except_jump(emit_t *emit, mp_uint_t label, bool within_exc_handler) {
|
|
|
if (within_exc_handler) {
|
|
|
// Cancel any active exception so subsequent handlers don't see it
|
|
|
- emit_native_mov_reg_const(emit, REG_TEMP0, MP_F_CONST_NONE_OBJ);
|
|
|
+ ASM_MOV_REG_IMM(emit->as, REG_TEMP0, (mp_uint_t)MP_OBJ_NULL);
|
|
|
ASM_MOV_LOCAL_REG(emit->as, LOCAL_IDX_EXC_VAL(emit), REG_TEMP0);
|
|
|
} else {
|
|
|
emit_native_leave_exc_stack(emit, false);
|
|
|
@@ -2272,7 +2281,7 @@ STATIC void emit_native_unary_op(emit_t *emit, mp_unary_op_t op) {
|
|
|
} else {
|
|
|
adjust_stack(emit, 1);
|
|
|
EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
|
|
|
- "unary op %q not implemented", mp_unary_op_method_name[op]);
|
|
|
+ MP_ERROR_TEXT("unary op %q not implemented"), mp_unary_op_method_name[op]);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -2280,7 +2289,8 @@ STATIC void emit_native_binary_op(emit_t *emit, mp_binary_op_t op) {
|
|
|
DEBUG_printf("binary_op(" UINT_FMT ")\n", op);
|
|
|
vtype_kind_t vtype_lhs = peek_vtype(emit, 1);
|
|
|
vtype_kind_t vtype_rhs = peek_vtype(emit, 0);
|
|
|
- if (vtype_lhs == VTYPE_INT && vtype_rhs == VTYPE_INT) {
|
|
|
+ if ((vtype_lhs == VTYPE_INT || vtype_lhs == VTYPE_UINT)
|
|
|
+ && (vtype_rhs == VTYPE_INT || vtype_rhs == VTYPE_UINT)) {
|
|
|
// for integers, inplace and normal ops are equivalent, so use just normal ops
|
|
|
if (MP_BINARY_OP_INPLACE_OR <= op && op <= MP_BINARY_OP_INPLACE_POWER) {
|
|
|
op += MP_BINARY_OP_OR - MP_BINARY_OP_INPLACE_OR;
|
|
|
@@ -2297,9 +2307,13 @@ STATIC void emit_native_binary_op(emit_t *emit, mp_binary_op_t op) {
|
|
|
if (op == MP_BINARY_OP_LSHIFT) {
|
|
|
ASM_LSL_REG(emit->as, REG_RET);
|
|
|
} else {
|
|
|
- ASM_ASR_REG(emit->as, REG_RET);
|
|
|
+ if (vtype_lhs == VTYPE_UINT) {
|
|
|
+ ASM_LSR_REG(emit->as, REG_RET);
|
|
|
+ } else {
|
|
|
+ ASM_ASR_REG(emit->as, REG_RET);
|
|
|
+ }
|
|
|
}
|
|
|
- emit_post_push_reg(emit, VTYPE_INT, REG_RET);
|
|
|
+ emit_post_push_reg(emit, vtype_lhs, REG_RET);
|
|
|
return;
|
|
|
}
|
|
|
#endif
|
|
|
@@ -2307,6 +2321,10 @@ STATIC void emit_native_binary_op(emit_t *emit, mp_binary_op_t op) {
|
|
|
// special cases for floor-divide and module because we dispatch to helper functions
|
|
|
if (op == MP_BINARY_OP_FLOOR_DIVIDE || op == MP_BINARY_OP_MODULO) {
|
|
|
emit_pre_pop_reg_reg(emit, &vtype_rhs, REG_ARG_2, &vtype_lhs, REG_ARG_1);
|
|
|
+ if (vtype_lhs != VTYPE_INT) {
|
|
|
+ EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
|
|
|
+ MP_ERROR_TEXT("div/mod not implemented for uint"), mp_binary_op_method_name[op]);
|
|
|
+ }
|
|
|
if (op == MP_BINARY_OP_FLOOR_DIVIDE) {
|
|
|
emit_call(emit, MP_F_SMALL_INT_FLOOR_DIVIDE);
|
|
|
} else {
|
|
|
@@ -2319,33 +2337,41 @@ STATIC void emit_native_binary_op(emit_t *emit, mp_binary_op_t op) {
|
|
|
int reg_rhs = REG_ARG_3;
|
|
|
emit_pre_pop_reg_flexible(emit, &vtype_rhs, ®_rhs, REG_RET, REG_ARG_2);
|
|
|
emit_pre_pop_reg(emit, &vtype_lhs, REG_ARG_2);
|
|
|
+
|
|
|
#if !(N_X64 || N_X86)
|
|
|
- if (op == MP_BINARY_OP_LSHIFT) {
|
|
|
- ASM_LSL_REG_REG(emit->as, REG_ARG_2, reg_rhs);
|
|
|
- emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
|
|
|
- } else if (op == MP_BINARY_OP_RSHIFT) {
|
|
|
- ASM_ASR_REG_REG(emit->as, REG_ARG_2, reg_rhs);
|
|
|
- emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
|
|
|
- } else
|
|
|
+ if (op == MP_BINARY_OP_LSHIFT || op == MP_BINARY_OP_RSHIFT) {
|
|
|
+ if (op == MP_BINARY_OP_LSHIFT) {
|
|
|
+ ASM_LSL_REG_REG(emit->as, REG_ARG_2, reg_rhs);
|
|
|
+ } else {
|
|
|
+ if (vtype_lhs == VTYPE_UINT) {
|
|
|
+ ASM_LSR_REG_REG(emit->as, REG_ARG_2, reg_rhs);
|
|
|
+ } else {
|
|
|
+ ASM_ASR_REG_REG(emit->as, REG_ARG_2, reg_rhs);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ emit_post_push_reg(emit, vtype_lhs, REG_ARG_2);
|
|
|
+ return;
|
|
|
+ }
|
|
|
#endif
|
|
|
+
|
|
|
if (op == MP_BINARY_OP_OR) {
|
|
|
ASM_OR_REG_REG(emit->as, REG_ARG_2, reg_rhs);
|
|
|
- emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
|
|
|
+ emit_post_push_reg(emit, vtype_lhs, REG_ARG_2);
|
|
|
} else if (op == MP_BINARY_OP_XOR) {
|
|
|
ASM_XOR_REG_REG(emit->as, REG_ARG_2, reg_rhs);
|
|
|
- emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
|
|
|
+ emit_post_push_reg(emit, vtype_lhs, REG_ARG_2);
|
|
|
} else if (op == MP_BINARY_OP_AND) {
|
|
|
ASM_AND_REG_REG(emit->as, REG_ARG_2, reg_rhs);
|
|
|
- emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
|
|
|
+ emit_post_push_reg(emit, vtype_lhs, REG_ARG_2);
|
|
|
} else if (op == MP_BINARY_OP_ADD) {
|
|
|
ASM_ADD_REG_REG(emit->as, REG_ARG_2, reg_rhs);
|
|
|
- emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
|
|
|
+ emit_post_push_reg(emit, vtype_lhs, REG_ARG_2);
|
|
|
} else if (op == MP_BINARY_OP_SUBTRACT) {
|
|
|
ASM_SUB_REG_REG(emit->as, REG_ARG_2, reg_rhs);
|
|
|
- emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
|
|
|
+ emit_post_push_reg(emit, vtype_lhs, REG_ARG_2);
|
|
|
} else if (op == MP_BINARY_OP_MULTIPLY) {
|
|
|
ASM_MUL_REG_REG(emit->as, REG_ARG_2, reg_rhs);
|
|
|
- emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
|
|
|
+ emit_post_push_reg(emit, vtype_lhs, REG_ARG_2);
|
|
|
} else if (MP_BINARY_OP_LESS <= op && op <= MP_BINARY_OP_NOT_EQUAL) {
|
|
|
// comparison ops are (in enum order):
|
|
|
// MP_BINARY_OP_LESS
|
|
|
@@ -2354,11 +2380,26 @@ STATIC void emit_native_binary_op(emit_t *emit, mp_binary_op_t op) {
|
|
|
// MP_BINARY_OP_LESS_EQUAL
|
|
|
// MP_BINARY_OP_MORE_EQUAL
|
|
|
// MP_BINARY_OP_NOT_EQUAL
|
|
|
+
|
|
|
+ if (vtype_lhs != vtype_rhs) {
|
|
|
+ EMIT_NATIVE_VIPER_TYPE_ERROR(emit, MP_ERROR_TEXT("comparison of int and uint"));
|
|
|
+ }
|
|
|
+
|
|
|
+ size_t op_idx = op - MP_BINARY_OP_LESS + (vtype_lhs == VTYPE_UINT ? 0 : 6);
|
|
|
+
|
|
|
need_reg_single(emit, REG_RET, 0);
|
|
|
#if N_X64
|
|
|
asm_x64_xor_r64_r64(emit->as, REG_RET, REG_RET);
|
|
|
asm_x64_cmp_r64_with_r64(emit->as, reg_rhs, REG_ARG_2);
|
|
|
- static byte ops[6] = {
|
|
|
+ static byte ops[6 + 6] = {
|
|
|
+ // unsigned
|
|
|
+ ASM_X64_CC_JB,
|
|
|
+ ASM_X64_CC_JA,
|
|
|
+ ASM_X64_CC_JE,
|
|
|
+ ASM_X64_CC_JBE,
|
|
|
+ ASM_X64_CC_JAE,
|
|
|
+ ASM_X64_CC_JNE,
|
|
|
+ // signed
|
|
|
ASM_X64_CC_JL,
|
|
|
ASM_X64_CC_JG,
|
|
|
ASM_X64_CC_JE,
|
|
|
@@ -2366,11 +2407,19 @@ STATIC void emit_native_binary_op(emit_t *emit, mp_binary_op_t op) {
|
|
|
ASM_X64_CC_JGE,
|
|
|
ASM_X64_CC_JNE,
|
|
|
};
|
|
|
- asm_x64_setcc_r8(emit->as, ops[op - MP_BINARY_OP_LESS], REG_RET);
|
|
|
+ asm_x64_setcc_r8(emit->as, ops[op_idx], REG_RET);
|
|
|
#elif N_X86
|
|
|
asm_x86_xor_r32_r32(emit->as, REG_RET, REG_RET);
|
|
|
asm_x86_cmp_r32_with_r32(emit->as, reg_rhs, REG_ARG_2);
|
|
|
- static byte ops[6] = {
|
|
|
+ static byte ops[6 + 6] = {
|
|
|
+ // unsigned
|
|
|
+ ASM_X86_CC_JB,
|
|
|
+ ASM_X86_CC_JA,
|
|
|
+ ASM_X86_CC_JE,
|
|
|
+ ASM_X86_CC_JBE,
|
|
|
+ ASM_X86_CC_JAE,
|
|
|
+ ASM_X86_CC_JNE,
|
|
|
+ // signed
|
|
|
ASM_X86_CC_JL,
|
|
|
ASM_X86_CC_JG,
|
|
|
ASM_X86_CC_JE,
|
|
|
@@ -2378,24 +2427,39 @@ STATIC void emit_native_binary_op(emit_t *emit, mp_binary_op_t op) {
|
|
|
ASM_X86_CC_JGE,
|
|
|
ASM_X86_CC_JNE,
|
|
|
};
|
|
|
- asm_x86_setcc_r8(emit->as, ops[op - MP_BINARY_OP_LESS], REG_RET);
|
|
|
+ asm_x86_setcc_r8(emit->as, ops[op_idx], REG_RET);
|
|
|
#elif N_THUMB
|
|
|
asm_thumb_cmp_rlo_rlo(emit->as, REG_ARG_2, reg_rhs);
|
|
|
- static uint16_t ops[6] = {
|
|
|
- ASM_THUMB_OP_ITE_GE,
|
|
|
- ASM_THUMB_OP_ITE_GT,
|
|
|
+ static uint16_t ops[6 + 6] = {
|
|
|
+ // unsigned
|
|
|
+ ASM_THUMB_OP_ITE_CC,
|
|
|
+ ASM_THUMB_OP_ITE_HI,
|
|
|
ASM_THUMB_OP_ITE_EQ,
|
|
|
+ ASM_THUMB_OP_ITE_LS,
|
|
|
+ ASM_THUMB_OP_ITE_CS,
|
|
|
+ ASM_THUMB_OP_ITE_NE,
|
|
|
+ // signed
|
|
|
+ ASM_THUMB_OP_ITE_LT,
|
|
|
ASM_THUMB_OP_ITE_GT,
|
|
|
- ASM_THUMB_OP_ITE_GE,
|
|
|
ASM_THUMB_OP_ITE_EQ,
|
|
|
+ ASM_THUMB_OP_ITE_LE,
|
|
|
+ ASM_THUMB_OP_ITE_GE,
|
|
|
+ ASM_THUMB_OP_ITE_NE,
|
|
|
};
|
|
|
- static byte ret[6] = { 0, 1, 1, 0, 1, 0, };
|
|
|
- asm_thumb_op16(emit->as, ops[op - MP_BINARY_OP_LESS]);
|
|
|
- asm_thumb_mov_rlo_i8(emit->as, REG_RET, ret[op - MP_BINARY_OP_LESS]);
|
|
|
- asm_thumb_mov_rlo_i8(emit->as, REG_RET, ret[op - MP_BINARY_OP_LESS] ^ 1);
|
|
|
+ asm_thumb_op16(emit->as, ops[op_idx]);
|
|
|
+ asm_thumb_mov_rlo_i8(emit->as, REG_RET, 1);
|
|
|
+ asm_thumb_mov_rlo_i8(emit->as, REG_RET, 0);
|
|
|
#elif N_ARM
|
|
|
asm_arm_cmp_reg_reg(emit->as, REG_ARG_2, reg_rhs);
|
|
|
- static uint ccs[6] = {
|
|
|
+ static uint ccs[6 + 6] = {
|
|
|
+ // unsigned
|
|
|
+ ASM_ARM_CC_CC,
|
|
|
+ ASM_ARM_CC_HI,
|
|
|
+ ASM_ARM_CC_EQ,
|
|
|
+ ASM_ARM_CC_LS,
|
|
|
+ ASM_ARM_CC_CS,
|
|
|
+ ASM_ARM_CC_NE,
|
|
|
+ // signed
|
|
|
ASM_ARM_CC_LT,
|
|
|
ASM_ARM_CC_GT,
|
|
|
ASM_ARM_CC_EQ,
|
|
|
@@ -2403,9 +2467,17 @@ STATIC void emit_native_binary_op(emit_t *emit, mp_binary_op_t op) {
|
|
|
ASM_ARM_CC_GE,
|
|
|
ASM_ARM_CC_NE,
|
|
|
};
|
|
|
- asm_arm_setcc_reg(emit->as, REG_RET, ccs[op - MP_BINARY_OP_LESS]);
|
|
|
+ asm_arm_setcc_reg(emit->as, REG_RET, ccs[op_idx]);
|
|
|
#elif N_XTENSA || N_XTENSAWIN
|
|
|
- static uint8_t ccs[6] = {
|
|
|
+ static uint8_t ccs[6 + 6] = {
|
|
|
+ // unsigned
|
|
|
+ ASM_XTENSA_CC_LTU,
|
|
|
+ 0x80 | ASM_XTENSA_CC_LTU, // for GTU we'll swap args
|
|
|
+ ASM_XTENSA_CC_EQ,
|
|
|
+ 0x80 | ASM_XTENSA_CC_GEU, // for LEU we'll swap args
|
|
|
+ ASM_XTENSA_CC_GEU,
|
|
|
+ ASM_XTENSA_CC_NE,
|
|
|
+ // signed
|
|
|
ASM_XTENSA_CC_LT,
|
|
|
0x80 | ASM_XTENSA_CC_LT, // for GT we'll swap args
|
|
|
ASM_XTENSA_CC_EQ,
|
|
|
@@ -2413,21 +2485,21 @@ STATIC void emit_native_binary_op(emit_t *emit, mp_binary_op_t op) {
|
|
|
ASM_XTENSA_CC_GE,
|
|
|
ASM_XTENSA_CC_NE,
|
|
|
};
|
|
|
- uint8_t cc = ccs[op - MP_BINARY_OP_LESS];
|
|
|
+ uint8_t cc = ccs[op_idx];
|
|
|
if ((cc & 0x80) == 0) {
|
|
|
asm_xtensa_setcc_reg_reg_reg(emit->as, cc, REG_RET, REG_ARG_2, reg_rhs);
|
|
|
} else {
|
|
|
asm_xtensa_setcc_reg_reg_reg(emit->as, cc & ~0x80, REG_RET, reg_rhs, REG_ARG_2);
|
|
|
}
|
|
|
#else
|
|
|
- #error not implemented
|
|
|
+ #error not implemented
|
|
|
#endif
|
|
|
emit_post_push_reg(emit, VTYPE_BOOL, REG_RET);
|
|
|
} else {
|
|
|
// TODO other ops not yet implemented
|
|
|
adjust_stack(emit, 1);
|
|
|
EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
|
|
|
- "binary op %q not implemented", mp_binary_op_method_name[op]);
|
|
|
+ MP_ERROR_TEXT("binary op %q not implemented"), mp_binary_op_method_name[op]);
|
|
|
}
|
|
|
} else if (vtype_lhs == VTYPE_PYOBJ && vtype_rhs == VTYPE_PYOBJ) {
|
|
|
emit_pre_pop_reg_reg(emit, &vtype_rhs, REG_ARG_3, &vtype_lhs, REG_ARG_2);
|
|
|
@@ -2448,7 +2520,7 @@ STATIC void emit_native_binary_op(emit_t *emit, mp_binary_op_t op) {
|
|
|
} else {
|
|
|
adjust_stack(emit, -1);
|
|
|
EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
|
|
|
- "can't do binary op between '%q' and '%q'",
|
|
|
+ MP_ERROR_TEXT("can't do binary op between '%q' and '%q'"),
|
|
|
vtype_to_qstr(vtype_lhs), vtype_to_qstr(vtype_rhs));
|
|
|
}
|
|
|
}
|
|
|
@@ -2626,7 +2698,7 @@ STATIC void emit_native_call_function(emit_t *emit, mp_uint_t n_positional, mp_u
|
|
|
break;
|
|
|
default:
|
|
|
// this can happen when casting a cast: int(int)
|
|
|
- mp_raise_NotImplementedError("casting");
|
|
|
+ mp_raise_NotImplementedError(MP_ERROR_TEXT("casting"));
|
|
|
}
|
|
|
} else {
|
|
|
assert(vtype_fun == VTYPE_PYOBJ);
|
|
|
@@ -2690,7 +2762,7 @@ STATIC void emit_native_return_value(emit_t *emit) {
|
|
|
emit_pre_pop_reg(emit, &vtype, return_vtype == VTYPE_PYOBJ ? REG_PARENT_RET : REG_ARG_1);
|
|
|
if (vtype != return_vtype) {
|
|
|
EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
|
|
|
- "return expected '%q' but got '%q'",
|
|
|
+ MP_ERROR_TEXT("return expected '%q' but got '%q'"),
|
|
|
vtype_to_qstr(return_vtype), vtype_to_qstr(vtype));
|
|
|
}
|
|
|
}
|
|
|
@@ -2719,7 +2791,7 @@ STATIC void emit_native_raise_varargs(emit_t *emit, mp_uint_t n_args) {
|
|
|
vtype_kind_t vtype_exc;
|
|
|
emit_pre_pop_reg(emit, &vtype_exc, REG_ARG_1); // arg1 = object to raise
|
|
|
if (vtype_exc != VTYPE_PYOBJ) {
|
|
|
- EMIT_NATIVE_VIPER_TYPE_ERROR(emit, "must raise an object");
|
|
|
+ EMIT_NATIVE_VIPER_TYPE_ERROR(emit, MP_ERROR_TEXT("must raise an object"));
|
|
|
}
|
|
|
// TODO probably make this 1 call to the runtime (which could even call convert, native_raise(obj, type))
|
|
|
emit_call(emit, MP_F_NATIVE_RAISE);
|
|
|
@@ -2729,7 +2801,7 @@ STATIC void emit_native_yield(emit_t *emit, int kind) {
|
|
|
// Note: 1 (yield) or 3 (yield from) labels are reserved for this function, starting at *emit->label_slot
|
|
|
|
|
|
if (emit->do_viper_types) {
|
|
|
- mp_raise_NotImplementedError("native yield");
|
|
|
+ mp_raise_NotImplementedError(MP_ERROR_TEXT("native yield"));
|
|
|
}
|
|
|
emit->scope->scope_flags |= MP_SCOPE_FLAG_GENERATOR;
|
|
|
|
|
|
@@ -2775,6 +2847,7 @@ STATIC void emit_native_yield(emit_t *emit, int kind) {
|
|
|
// Found active handler, get its PC
|
|
|
ASM_MOV_REG_PCREL(emit->as, REG_RET, e->label);
|
|
|
ASM_MOV_LOCAL_REG(emit->as, LOCAL_IDX_EXC_HANDLER_PC(emit), REG_RET);
|
|
|
+ break;
|
|
|
}
|
|
|
}
|
|
|
}
|