Skip to content

Commit b8698fc

Browse files
committed
unified the bops
1 parent ad97f2a commit b8698fc

File tree

15 files changed

+62
-161
lines changed

15 files changed

+62
-161
lines changed

py/bc0.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@
6666

6767
#define MP_BC_UNARY_OP (0x60) // byte
6868
#define MP_BC_BINARY_OP (0x61) // byte
69-
#define MP_BC_COMPARE_OP (0x62) // byte
7069

7170
#define MP_BC_BUILD_TUPLE (0x70) // uint
7271
#define MP_BC_BUILD_LIST (0x71) // uint

py/compile.c

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1464,9 +1464,9 @@ void compile_for_stmt_optimised_range(compiler_t *comp, mp_parse_node_t pn_var,
14641464
compile_node(comp, pn_var);
14651465
compile_node(comp, pn_end);
14661466
if (MP_PARSE_NODE_LEAF_ARG(pn_step) >= 0) {
1467-
EMIT(compare_op, RT_COMPARE_OP_LESS);
1467+
EMIT(binary_op, RT_COMPARE_OP_LESS);
14681468
} else {
1469-
EMIT(compare_op, RT_COMPARE_OP_MORE);
1469+
EMIT(binary_op, RT_COMPARE_OP_MORE);
14701470
}
14711471
EMIT(pop_jump_if_true, top_label);
14721472

@@ -1610,7 +1610,7 @@ void compile_try_except(compiler_t *comp, mp_parse_node_t pn_body, int n_except,
16101610
}
16111611
EMIT(dup_top);
16121612
compile_node(comp, pns_exception_expr);
1613-
EMIT(compare_op, RT_COMPARE_OP_EXCEPTION_MATCH);
1613+
EMIT(binary_op, RT_COMPARE_OP_EXCEPTION_MATCH);
16141614
EMIT(pop_jump_if_false, end_finally_label);
16151615
}
16161616

@@ -1925,29 +1925,29 @@ void compile_comparison(compiler_t *comp, mp_parse_node_struct_t *pns) {
19251925
EMIT(rot_three);
19261926
}
19271927
if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_LESS)) {
1928-
EMIT(compare_op, RT_COMPARE_OP_LESS);
1928+
EMIT(binary_op, RT_COMPARE_OP_LESS);
19291929
} else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_MORE)) {
1930-
EMIT(compare_op, RT_COMPARE_OP_MORE);
1930+
EMIT(binary_op, RT_COMPARE_OP_MORE);
19311931
} else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_EQUAL)) {
1932-
EMIT(compare_op, RT_COMPARE_OP_EQUAL);
1932+
EMIT(binary_op, RT_COMPARE_OP_EQUAL);
19331933
} else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_LESS_EQUAL)) {
1934-
EMIT(compare_op, RT_COMPARE_OP_LESS_EQUAL);
1934+
EMIT(binary_op, RT_COMPARE_OP_LESS_EQUAL);
19351935
} else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_MORE_EQUAL)) {
1936-
EMIT(compare_op, RT_COMPARE_OP_MORE_EQUAL);
1936+
EMIT(binary_op, RT_COMPARE_OP_MORE_EQUAL);
19371937
} else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_NOT_EQUAL)) {
1938-
EMIT(compare_op, RT_COMPARE_OP_NOT_EQUAL);
1938+
EMIT(binary_op, RT_COMPARE_OP_NOT_EQUAL);
19391939
} else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_KW_IN)) {
1940-
EMIT(compare_op, RT_COMPARE_OP_IN);
1940+
EMIT(binary_op, RT_COMPARE_OP_IN);
19411941
} else if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[i])) {
19421942
mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[i];
19431943
int kind = MP_PARSE_NODE_STRUCT_KIND(pns2);
19441944
if (kind == PN_comp_op_not_in) {
1945-
EMIT(compare_op, RT_COMPARE_OP_NOT_IN);
1945+
EMIT(binary_op, RT_COMPARE_OP_NOT_IN);
19461946
} else if (kind == PN_comp_op_is) {
19471947
if (MP_PARSE_NODE_IS_NULL(pns2->nodes[0])) {
1948-
EMIT(compare_op, RT_COMPARE_OP_IS);
1948+
EMIT(binary_op, RT_COMPARE_OP_IS);
19491949
} else {
1950-
EMIT(compare_op, RT_COMPARE_OP_IS_NOT);
1950+
EMIT(binary_op, RT_COMPARE_OP_IS_NOT);
19511951
}
19521952
} else {
19531953
// shouldn't happen

py/emit.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ typedef struct _emit_method_table_t {
8585
void (*pop_except)(emit_t *emit);
8686
void (*unary_op)(emit_t *emit, rt_unary_op_t op);
8787
void (*binary_op)(emit_t *emit, rt_binary_op_t op);
88-
void (*compare_op)(emit_t *emit, rt_compare_op_t op);
8988
void (*build_tuple)(emit_t *emit, int n_args);
9089
void (*build_list)(emit_t *emit, int n_args);
9190
void (*list_append)(emit_t *emit, int list_stack_index);

py/emitbc.c

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -531,11 +531,6 @@ static void emit_bc_binary_op(emit_t *emit, rt_binary_op_t op) {
531531
emit_write_byte_1_byte(emit, MP_BC_BINARY_OP, op);
532532
}
533533

534-
static void emit_bc_compare_op(emit_t *emit, rt_compare_op_t op) {
535-
emit_pre(emit, -1);
536-
emit_write_byte_1_byte(emit, MP_BC_COMPARE_OP, op);
537-
}
538-
539534
static void emit_bc_build_tuple(emit_t *emit, int n_args) {
540535
assert(n_args >= 0);
541536
emit_pre(emit, 1 - n_args);
@@ -762,7 +757,6 @@ const emit_method_table_t emit_bc_method_table = {
762757
emit_bc_pop_except,
763758
emit_bc_unary_op,
764759
emit_bc_binary_op,
765-
emit_bc_compare_op,
766760
emit_bc_build_tuple,
767761
emit_bc_build_list,
768762
emit_bc_list_append,

py/emitcpy.c

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -579,15 +579,6 @@ static void emit_cpy_binary_op(emit_t *emit, rt_binary_op_t op) {
579579
case RT_BINARY_OP_INPLACE_TRUE_DIVIDE: printf("INPLACE_TRUE_DIVIDE\n"); break;
580580
case RT_BINARY_OP_INPLACE_MODULO: printf("INPLACE_MODULO\n"); break;
581581
case RT_BINARY_OP_INPLACE_POWER: printf("INPLACE_POWER\n"); break;
582-
default: assert(0);
583-
}
584-
}
585-
}
586-
587-
static void emit_cpy_compare_op(emit_t *emit, rt_compare_op_t op) {
588-
emit_pre(emit, -1, 3);
589-
if (emit->pass == PASS_3) {
590-
switch (op) {
591582
case RT_COMPARE_OP_LESS: printf("COMPARE_OP <\n"); break;
592583
case RT_COMPARE_OP_MORE: printf("COMPARE_OP >\n"); break;
593584
case RT_COMPARE_OP_EQUAL: printf("COMPARE_OP ==\n"); break;
@@ -863,7 +854,6 @@ const emit_method_table_t emit_cpython_method_table = {
863854
emit_cpy_pop_except,
864855
emit_cpy_unary_op,
865856
emit_cpy_binary_op,
866-
emit_cpy_compare_op,
867857
emit_cpy_build_tuple,
868858
emit_cpy_build_list,
869859
emit_cpy_list_append,

py/emitnative.c

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,31 +1013,6 @@ static void emit_native_binary_op(emit_t *emit, rt_binary_op_t op) {
10131013
}
10141014
}
10151015

1016-
static void emit_native_compare_op(emit_t *emit, rt_compare_op_t op) {
1017-
vtype_kind_t vtype_lhs, vtype_rhs;
1018-
emit_pre_pop_reg_reg(emit, &vtype_rhs, REG_ARG_3, &vtype_lhs, REG_ARG_2);
1019-
if (vtype_lhs == VTYPE_INT && vtype_rhs == VTYPE_INT) {
1020-
assert(op == RT_COMPARE_OP_LESS);
1021-
#if N_X64
1022-
asm_x64_xor_r64_to_r64(emit->as, REG_RET, REG_RET);
1023-
asm_x64_cmp_r64_with_r64(emit->as, REG_ARG_3, REG_ARG_2);
1024-
asm_x64_setcc_r8(emit->as, JCC_JL, REG_RET);
1025-
#elif N_THUMB
1026-
asm_thumb_cmp_reg_reg(emit->as, REG_ARG_2, REG_ARG_3);
1027-
asm_thumb_ite_ge(emit->as);
1028-
asm_thumb_movs_rlo_i8(emit->as, REG_RET, 0); // if r0 >= r1
1029-
asm_thumb_movs_rlo_i8(emit->as, REG_RET, 1); // if r0 < r1
1030-
#endif
1031-
emit_post_push_reg(emit, VTYPE_BOOL, REG_RET);
1032-
} else if (vtype_lhs == VTYPE_PYOBJ && vtype_rhs == VTYPE_PYOBJ) {
1033-
emit_call_with_imm_arg(emit, RT_F_COMPARE_OP, rt_compare_op, op, REG_ARG_1);
1034-
emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1035-
} else {
1036-
printf("ViperTypeError: can't do comparison between types %d and %d\n", vtype_lhs, vtype_rhs);
1037-
assert(0);
1038-
}
1039-
}
1040-
10411016
static void emit_native_build_tuple(emit_t *emit, int n_args) {
10421017
// for viper: call runtime, with types of args
10431018
// if wrapped in byte_array, or something, allocates memory and fills it
@@ -1297,7 +1272,6 @@ const emit_method_table_t EXPORT_FUN(method_table) = {
12971272
emit_native_pop_except,
12981273
emit_native_unary_op,
12991274
emit_native_binary_op,
1300-
emit_native_compare_op,
13011275
emit_native_build_tuple,
13021276
emit_native_build_list,
13031277
emit_native_list_append,

py/emitpass1.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,5 +187,4 @@ const emit_method_table_t emit_pass1_method_table = {
187187
(void*)emit_pass1_dummy,
188188
(void*)emit_pass1_dummy,
189189
(void*)emit_pass1_dummy,
190-
(void*)emit_pass1_dummy,
191190
};

py/obj.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,6 @@ struct _mp_obj_type_t {
155155
/*
156156
What we might need to add here:
157157
158-
compare_op
159158
store_subscr list dict
160159
161160
len str tuple list map
@@ -241,6 +240,7 @@ extern const mp_obj_type_t none_type;
241240

242241
// bool
243242
extern const mp_obj_type_t bool_type;
243+
#define MP_BOOL(x) (x ? mp_const_true : mp_const_false)
244244

245245
// cell
246246
mp_obj_t mp_obj_cell_get(mp_obj_t self_in);

py/objfloat.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,12 @@ mp_obj_t mp_obj_float_binary_op(int op, mp_float_t lhs_val, mp_obj_t rhs_in) {
9999
*/
100100
case RT_BINARY_OP_TRUE_DIVIDE:
101101
case RT_BINARY_OP_INPLACE_TRUE_DIVIDE: lhs_val /= rhs_val; break;
102+
103+
case RT_COMPARE_OP_LESS: return MP_BOOL(lhs_val < rhs_val);
104+
case RT_COMPARE_OP_MORE: return MP_BOOL(lhs_val > rhs_val);
105+
case RT_COMPARE_OP_LESS_EQUAL: return MP_BOOL(lhs_val <= rhs_val);
106+
case RT_COMPARE_OP_MORE_EQUAL: return MP_BOOL(lhs_val >= rhs_val);
107+
102108
return NULL; // op not supported
103109
}
104110
return mp_obj_new_float(lhs_val);

py/objlist.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,8 @@ static void mp_quicksort(mp_obj_t *head, mp_obj_t *tail, mp_obj_t key_fn, bool r
143143
mp_obj_t *t = tail;
144144
mp_obj_t v = key_fn == NULL ? tail[0] : rt_call_function_1(key_fn, tail[0]); // get pivot using key_fn
145145
for (;;) {
146-
do ++h; while (rt_compare_op(op, key_fn == NULL ? h[0] : rt_call_function_1(key_fn, h[0]), v) == mp_const_true);
147-
do --t; while (h < t && rt_compare_op(op, v, key_fn == NULL ? t[0] : rt_call_function_1(key_fn, t[0])) == mp_const_true);
146+
do ++h; while (rt_binary_op(op, key_fn == NULL ? h[0] : rt_call_function_1(key_fn, h[0]), v) == mp_const_true);
147+
do --t; while (h < t && rt_binary_op(op, v, key_fn == NULL ? t[0] : rt_call_function_1(key_fn, t[0])) == mp_const_true);
148148
if (h >= t) break;
149149
mp_obj_t x = h[0];
150150
h[0] = t[0];

0 commit comments

Comments
 (0)