Skip to content

Commit 80db30a

Browse files
committed
py/emit: Completely remove set_native_type, arg type is set in compiler.
In viper mode, the type of the argument is now stored in id_info->flags.
1 parent 07caf4f commit 80db30a

File tree

5 files changed

+20
-24
lines changed

5 files changed

+20
-24
lines changed

py/compile.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2853,7 +2853,12 @@ STATIC void compile_scope_func_annotations(compiler_t *comp, mp_parse_node_t pn)
28532853

28542854
if (MP_PARSE_NODE_IS_ID(pn_annotation)) {
28552855
qstr arg_type = MP_PARSE_NODE_LEAF_ARG(pn_annotation);
2856-
EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_ARG, id_info->local_num, arg_type);
2856+
int native_type = mp_native_type_from_qstr(arg_type);
2857+
if (native_type < 0) {
2858+
comp->compile_error = mp_obj_new_exception_msg_varg(&mp_type_ViperTypeError, "unknown type '%q'", arg_type);
2859+
} else {
2860+
id_info->flags |= native_type << ID_FLAG_VIPER_TYPE_POS;
2861+
}
28572862
} else {
28582863
compile_syntax_error(comp, pn_annotation, "parameter annotation must be an identifier");
28592864
}
@@ -2983,9 +2988,8 @@ STATIC void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
29832988
apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_param);
29842989
}
29852990
#if MICROPY_EMIT_NATIVE
2986-
else if (scope->emit_options == MP_EMIT_OPT_VIPER) {
2987-
// compile annotations; only needed on latter compiler passes
2988-
// only needed for viper emitter
2991+
if (comp->pass == MP_PASS_SCOPE && scope->emit_options == MP_EMIT_OPT_VIPER) {
2992+
// compile annotations; only needed for viper emitter
29892993

29902994
// argument annotations
29912995
apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_annotations);

py/emit.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,6 @@ typedef enum {
5151

5252
#define MP_EMIT_BREAK_FROM_FOR (0x8000)
5353

54-
#define MP_EMIT_NATIVE_TYPE_ARG (2)
55-
5654
// Kind for emit_id_ops->local()
5755
#define MP_EMIT_IDOP_LOCAL_FAST (0)
5856
#define MP_EMIT_IDOP_LOCAL_DEREF (1)
@@ -100,7 +98,6 @@ typedef struct _mp_emit_method_table_id_ops_t {
10098
} mp_emit_method_table_id_ops_t;
10199

102100
typedef struct _emit_method_table_t {
103-
void (*set_native_type)(emit_t *emit, mp_uint_t op, mp_uint_t arg1, qstr arg2);
104101
void (*start_pass)(emit_t *emit, pass_kind_t pass, scope_t *scope);
105102
void (*end_pass)(emit_t *emit);
106103
bool (*last_emit_was_return_value)(emit_t *emit);

py/emitbc.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -910,7 +910,6 @@ void mp_emit_bc_end_except_handler(emit_t *emit) {
910910

911911
#if MICROPY_EMIT_NATIVE
912912
const emit_method_table_t emit_bc_method_table = {
913-
NULL, // set_native_type is never called when emitting bytecode
914913
mp_emit_bc_start_pass,
915914
mp_emit_bc_end_pass,
916915
mp_emit_bc_last_emit_was_return_value,

py/emitnative.c

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -235,21 +235,6 @@ void EXPORT_FUN(free)(emit_t *emit) {
235235
m_del_obj(emit_t, emit);
236236
}
237237

238-
STATIC void emit_native_set_native_type(emit_t *emit, mp_uint_t op, mp_uint_t arg1, qstr arg2) {
239-
(void)op;
240-
{
241-
int type = mp_native_type_from_qstr(arg2);
242-
if (type < 0) {
243-
EMIT_NATIVE_VIPER_TYPE_ERROR(emit, "unknown type '%q'", arg2);
244-
return;
245-
}
246-
{
247-
assert(arg1 < emit->local_vtype_alloc);
248-
emit->local_vtype[arg1] = type;
249-
}
250-
}
251-
}
252-
253238
STATIC void emit_pre_pop_reg(emit_t *emit, vtype_kind_t *vtype, int reg_dest);
254239
STATIC void emit_post_push_reg(emit_t *emit, vtype_kind_t vtype, int reg);
255240
STATIC void emit_native_load_fast(emit_t *emit, qstr qst, mp_uint_t local_num);
@@ -283,6 +268,17 @@ STATIC void emit_native_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scop
283268
emit->local_vtype[i] = VTYPE_PYOBJ;
284269
}
285270

271+
// Set viper type for arguments
272+
if (emit->do_viper_types) {
273+
for (int i = 0; i < emit->scope->id_info_len; ++i) {
274+
id_info_t *id = &emit->scope->id_info[i];
275+
if (id->flags & ID_FLAG_IS_PARAM) {
276+
assert(id->local_num < emit->local_vtype_alloc);
277+
emit->local_vtype[id->local_num] = id->flags >> ID_FLAG_VIPER_TYPE_POS;
278+
}
279+
}
280+
}
281+
286282
// local variables begin unbound, and have unknown type
287283
for (mp_uint_t i = num_args; i < emit->local_vtype_alloc; i++) {
288284
emit->local_vtype[i] = VTYPE_UNBOUND;
@@ -2483,7 +2479,6 @@ STATIC void emit_native_end_except_handler(emit_t *emit) {
24832479
}
24842480

24852481
const emit_method_table_t EXPORT_FUN(method_table) = {
2486-
emit_native_set_native_type,
24872482
emit_native_start_pass,
24882483
emit_native_end_pass,
24892484
emit_native_last_emit_was_return_value,

py/scope.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ enum {
4141
ID_FLAG_IS_PARAM = 0x01,
4242
ID_FLAG_IS_STAR_PARAM = 0x02,
4343
ID_FLAG_IS_DBL_STAR_PARAM = 0x04,
44+
ID_FLAG_VIPER_TYPE_POS = 4,
4445
};
4546

4647
typedef struct _id_info_t {

0 commit comments

Comments
 (0)