Skip to content

Commit 32444b7

Browse files
committed
py: Don't use anonymous unions, name them instead.
This makes the code (more) compatible with the C99 standard.
1 parent 5c670ac commit 32444b7

3 files changed

Lines changed: 50 additions & 48 deletions

File tree

py/emitglue.c

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,23 @@
4444
#define DEBUG_OP_printf(...) (void)0
4545
#endif
4646

47+
struct _mp_raw_code_t {
48+
mp_raw_code_kind_t kind : 3;
49+
mp_uint_t scope_flags : 7;
50+
mp_uint_t n_pos_args : 11;
51+
mp_uint_t n_kwonly_args : 11;
52+
union {
53+
struct {
54+
byte *code;
55+
mp_uint_t len;
56+
} u_byte;
57+
struct {
58+
void *fun_data;
59+
mp_uint_t type_sig; // for viper, compressed as 2-bit types; ret is MSB, then arg0, arg1, etc
60+
} u_native;
61+
} data;
62+
};
63+
4764
mp_raw_code_t *mp_emit_glue_new_raw_code(void) {
4865
mp_raw_code_t *rc = m_new0(mp_raw_code_t, 1);
4966
rc->kind = MP_CODE_RESERVED;
@@ -55,8 +72,8 @@ void mp_emit_glue_assign_bytecode(mp_raw_code_t *rc, byte *code, mp_uint_t len,
5572
rc->scope_flags = scope_flags;
5673
rc->n_pos_args = n_pos_args;
5774
rc->n_kwonly_args = n_kwonly_args;
58-
rc->u_byte.code = code;
59-
rc->u_byte.len = len;
75+
rc->data.u_byte.code = code;
76+
rc->data.u_byte.len = len;
6077

6178
#ifdef DEBUG_PRINT
6279
DEBUG_printf("assign byte code: code=%p len=" UINT_FMT " n_pos_args=" UINT_FMT " n_kwonly_args=" UINT_FMT " flags=%x\n", code, len, n_pos_args, n_kwonly_args, (uint)scope_flags);
@@ -74,8 +91,8 @@ void mp_emit_glue_assign_native(mp_raw_code_t *rc, mp_raw_code_kind_t kind, void
7491
rc->kind = kind;
7592
rc->scope_flags = 0;
7693
rc->n_pos_args = n_args;
77-
rc->u_native.fun_data = fun_data;
78-
rc->u_native.type_sig = type_sig;
94+
rc->data.u_native.fun_data = fun_data;
95+
rc->data.u_native.type_sig = type_sig;
7996

8097
#ifdef DEBUG_PRINT
8198
DEBUG_printf("assign native: kind=%d fun=%p len=" UINT_FMT " n_args=" UINT_FMT "\n", kind, fun_data, fun_len, n_args);
@@ -113,19 +130,19 @@ mp_obj_t mp_make_function_from_raw_code(mp_raw_code_t *rc, mp_obj_t def_args, mp
113130
switch (rc->kind) {
114131
case MP_CODE_BYTECODE:
115132
no_other_choice:
116-
fun = mp_obj_new_fun_bc(rc->scope_flags, rc->n_pos_args, rc->n_kwonly_args, def_args, def_kw_args, rc->u_byte.code);
133+
fun = mp_obj_new_fun_bc(rc->scope_flags, rc->n_pos_args, rc->n_kwonly_args, def_args, def_kw_args, rc->data.u_byte.code);
117134
break;
118135
#if MICROPY_EMIT_NATIVE
119136
case MP_CODE_NATIVE_PY:
120-
fun = mp_obj_new_fun_native(rc->n_pos_args, rc->u_native.fun_data);
137+
fun = mp_obj_new_fun_native(rc->n_pos_args, rc->data.u_native.fun_data);
121138
break;
122139
case MP_CODE_NATIVE_VIPER:
123-
fun = mp_obj_new_fun_viper(rc->n_pos_args, rc->u_native.fun_data, rc->u_native.type_sig);
140+
fun = mp_obj_new_fun_viper(rc->n_pos_args, rc->data.u_native.fun_data, rc->data.u_native.type_sig);
124141
break;
125142
#endif
126143
#if MICROPY_EMIT_INLINE_THUMB
127144
case MP_CODE_NATIVE_ASM:
128-
fun = mp_obj_new_fun_asm(rc->n_pos_args, rc->u_native.fun_data);
145+
fun = mp_obj_new_fun_asm(rc->n_pos_args, rc->data.u_native.fun_data);
129146
break;
130147
#endif
131148
default:

py/emitglue.h

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,7 @@ typedef enum {
3939
MP_CODE_NATIVE_ASM,
4040
} mp_raw_code_kind_t;
4141

42-
typedef struct _mp_raw_code_t {
43-
mp_raw_code_kind_t kind : 3;
44-
mp_uint_t scope_flags : 7;
45-
mp_uint_t n_pos_args : 11;
46-
mp_uint_t n_kwonly_args : 11;
47-
union {
48-
struct {
49-
byte *code;
50-
mp_uint_t len;
51-
} u_byte;
52-
struct {
53-
void *fun_data;
54-
mp_uint_t type_sig; // for viper, compressed as 2-bit types; ret is MSB, then arg0, arg1, etc
55-
} u_native;
56-
};
57-
} mp_raw_code_t;
42+
typedef struct _mp_raw_code_t mp_raw_code_t;
5843

5944
mp_raw_code_t *mp_emit_glue_new_raw_code(void);
6045

py/emitnative.c

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ typedef struct _stack_info_t {
480480
union {
481481
int u_reg;
482482
mp_int_t u_imm;
483-
};
483+
} data;
484484
} stack_info_t;
485485

486486
struct _emit_t {
@@ -712,7 +712,7 @@ STATIC void adjust_stack(emit_t *emit, mp_int_t stack_size_delta) {
712712
DEBUG_printf(" adjust_stack; stack_size=%d+%d; stack now:", emit->stack_size - stack_size_delta, stack_size_delta);
713713
for (int i = 0; i < emit->stack_size; i++) {
714714
stack_info_t *si = &emit->stack_info[i];
715-
DEBUG_printf(" (v=%d k=%d %d)", si->vtype, si->kind, si->u_reg);
715+
DEBUG_printf(" (v=%d k=%d %d)", si->vtype, si->kind, si->data.u_reg);
716716
}
717717
DEBUG_printf("\n");
718718
#endif
@@ -765,7 +765,7 @@ STATIC void emit_native_pre(emit_t *emit) {
765765
case STACK_REG:
766766
// TODO only push reg if in regs_needed
767767
emit->stack_info[i].kind = STACK_VALUE;
768-
ASM_MOV_REG_TO_LOCAL(emit->as, emit->stack_info[i].u_reg, emit->stack_start + i);
768+
ASM_MOV_REG_TO_LOCAL(emit->as, emit->stack_info[i].data.u_reg, emit->stack_start + i);
769769
break;
770770
771771
case STACK_IMM:
@@ -795,9 +795,9 @@ STATIC void need_reg_single(emit_t *emit, int reg_needed, int skip_stack_pos) {
795795
for (int i = 0; i < emit->stack_size; i++) {
796796
if (i != skip_stack_pos) {
797797
stack_info_t *si = &emit->stack_info[i];
798-
if (si->kind == STACK_REG && si->u_reg == reg_needed) {
798+
if (si->kind == STACK_REG && si->data.u_reg == reg_needed) {
799799
si->kind = STACK_VALUE;
800-
ASM_MOV_REG_TO_LOCAL(emit->as, si->u_reg, emit->stack_start + i);
800+
ASM_MOV_REG_TO_LOCAL(emit->as, si->data.u_reg, emit->stack_start + i);
801801
}
802802
}
803803
}
@@ -808,7 +808,7 @@ STATIC void need_reg_all(emit_t *emit) {
808808
stack_info_t *si = &emit->stack_info[i];
809809
if (si->kind == STACK_REG) {
810810
si->kind = STACK_VALUE;
811-
ASM_MOV_REG_TO_LOCAL(emit->as, si->u_reg, emit->stack_start + i);
811+
ASM_MOV_REG_TO_LOCAL(emit->as, si->data.u_reg, emit->stack_start + i);
812812
}
813813
}
814814
}
@@ -818,17 +818,17 @@ STATIC void need_stack_settled(emit_t *emit) {
818818
for (int i = 0; i < emit->stack_size; i++) {
819819
stack_info_t *si = &emit->stack_info[i];
820820
if (si->kind == STACK_REG) {
821-
DEBUG_printf(" reg(%u) to local(%u)\n", si->u_reg, emit->stack_start + i);
821+
DEBUG_printf(" reg(%u) to local(%u)\n", si->data.u_reg, emit->stack_start + i);
822822
si->kind = STACK_VALUE;
823-
ASM_MOV_REG_TO_LOCAL(emit->as, si->u_reg, emit->stack_start + i);
823+
ASM_MOV_REG_TO_LOCAL(emit->as, si->data.u_reg, emit->stack_start + i);
824824
}
825825
}
826826
for (int i = 0; i < emit->stack_size; i++) {
827827
stack_info_t *si = &emit->stack_info[i];
828828
if (si->kind == STACK_IMM) {
829-
DEBUG_printf(" imm(" INT_FMT ") to local(%u)\n", si->u_imm, emit->stack_start + i);
829+
DEBUG_printf(" imm(" INT_FMT ") to local(%u)\n", si->data.u_imm, emit->stack_start + i);
830830
si->kind = STACK_VALUE;
831-
ASM_MOV_IMM_TO_LOCAL_USING(emit->as, si->u_imm, emit->stack_start + i, REG_TEMP0);
831+
ASM_MOV_IMM_TO_LOCAL_USING(emit->as, si->data.u_imm, emit->stack_start + i, REG_TEMP0);
832832
}
833833
}
834834
}
@@ -844,13 +844,13 @@ STATIC void emit_access_stack(emit_t *emit, int pos, vtype_kind_t *vtype, int re
844844
break;
845845

846846
case STACK_REG:
847-
if (si->u_reg != reg_dest) {
848-
ASM_MOV_REG_REG(emit->as, reg_dest, si->u_reg);
847+
if (si->data.u_reg != reg_dest) {
848+
ASM_MOV_REG_REG(emit->as, reg_dest, si->data.u_reg);
849849
}
850850
break;
851851

852852
case STACK_IMM:
853-
ASM_MOV_IMM_TO_REG(emit->as, si->u_imm, reg_dest);
853+
ASM_MOV_IMM_TO_REG(emit->as, si->data.u_imm, reg_dest);
854854
break;
855855
}
856856
}
@@ -864,7 +864,7 @@ STATIC void emit_fold_stack_top(emit_t *emit, int reg_dest) {
864864
// if folded element was on the stack we need to put it in a register
865865
ASM_MOV_LOCAL_TO_REG(emit->as, emit->stack_start + emit->stack_size - 1, reg_dest);
866866
si->kind = STACK_REG;
867-
si->u_reg = reg_dest;
867+
si->data.u_reg = reg_dest;
868868
}
869869
adjust_stack(emit, -1);
870870
}
@@ -874,9 +874,9 @@ STATIC void emit_fold_stack_top(emit_t *emit, int reg_dest) {
874874
STATIC void emit_pre_pop_reg_flexible(emit_t *emit, vtype_kind_t *vtype, int *reg_dest, int not_r1, int not_r2) {
875875
emit->last_emit_was_return_value = false;
876876
stack_info_t *si = peek_stack(emit, 0);
877-
if (si->kind == STACK_REG && si->u_reg != not_r1 && si->u_reg != not_r2) {
877+
if (si->kind == STACK_REG && si->data.u_reg != not_r1 && si->data.u_reg != not_r2) {
878878
*vtype = si->vtype;
879-
*reg_dest = si->u_reg;
879+
*reg_dest = si->data.u_reg;
880880
need_reg_single(emit, *reg_dest, 1);
881881
} else {
882882
emit_access_stack(emit, 1, vtype, *reg_dest);
@@ -919,15 +919,15 @@ STATIC void emit_post_push_reg(emit_t *emit, vtype_kind_t vtype, int reg) {
919919
stack_info_t *si = &emit->stack_info[emit->stack_size];
920920
si->vtype = vtype;
921921
si->kind = STACK_REG;
922-
si->u_reg = reg;
922+
si->data.u_reg = reg;
923923
adjust_stack(emit, 1);
924924
}
925925

926926
STATIC void emit_post_push_imm(emit_t *emit, vtype_kind_t vtype, mp_int_t imm) {
927927
stack_info_t *si = &emit->stack_info[emit->stack_size];
928928
si->vtype = vtype;
929929
si->kind = STACK_IMM;
930-
si->u_imm = imm;
930+
si->data.u_imm = imm;
931931
adjust_stack(emit, 1);
932932
}
933933

@@ -999,10 +999,10 @@ STATIC void emit_get_stack_pointer_to_reg_for_pop(emit_t *emit, mp_uint_t reg_de
999999
si->kind = STACK_VALUE;
10001000
switch (si->vtype) {
10011001
case VTYPE_PYOBJ:
1002-
ASM_MOV_IMM_TO_LOCAL_USING(emit->as, si->u_imm, emit->stack_start + emit->stack_size - 1 - i, reg_dest);
1002+
ASM_MOV_IMM_TO_LOCAL_USING(emit->as, si->data.u_imm, emit->stack_start + emit->stack_size - 1 - i, reg_dest);
10031003
break;
10041004
case VTYPE_BOOL:
1005-
if (si->u_imm == 0) {
1005+
if (si->data.u_imm == 0) {
10061006
ASM_MOV_IMM_TO_LOCAL_USING(emit->as, (mp_uint_t)mp_const_false, emit->stack_start + emit->stack_size - 1 - i, reg_dest);
10071007
} else {
10081008
ASM_MOV_IMM_TO_LOCAL_USING(emit->as, (mp_uint_t)mp_const_true, emit->stack_start + emit->stack_size - 1 - i, reg_dest);
@@ -1011,7 +1011,7 @@ STATIC void emit_get_stack_pointer_to_reg_for_pop(emit_t *emit, mp_uint_t reg_de
10111011
break;
10121012
case VTYPE_INT:
10131013
case VTYPE_UINT:
1014-
ASM_MOV_IMM_TO_LOCAL_USING(emit->as, (si->u_imm << 1) | 1, emit->stack_start + emit->stack_size - 1 - i, reg_dest);
1014+
ASM_MOV_IMM_TO_LOCAL_USING(emit->as, (si->data.u_imm << 1) | 1, emit->stack_start + emit->stack_size - 1 - i, reg_dest);
10151015
si->vtype = VTYPE_PYOBJ;
10161016
break;
10171017
default:
@@ -1332,7 +1332,7 @@ STATIC void emit_native_load_subscr(emit_t *emit) {
13321332
stack_info_t *top = peek_stack(emit, 0);
13331333
if (top->vtype == VTYPE_INT && top->kind == STACK_IMM) {
13341334
// index is an immediate
1335-
mp_int_t index_value = top->u_imm;
1335+
mp_int_t index_value = top->data.u_imm;
13361336
emit_pre_pop_discard(emit); // discard index
13371337
int reg_base = REG_ARG_1;
13381338
int reg_index = REG_ARG_2;
@@ -1531,7 +1531,7 @@ STATIC void emit_native_store_subscr(emit_t *emit) {
15311531
stack_info_t *top = peek_stack(emit, 0);
15321532
if (top->vtype == VTYPE_INT && top->kind == STACK_IMM) {
15331533
// index is an immediate
1534-
mp_int_t index_value = top->u_imm;
1534+
mp_int_t index_value = top->data.u_imm;
15351535
emit_pre_pop_discard(emit); // discard index
15361536
vtype_kind_t vtype_value;
15371537
int reg_base = REG_ARG_1;
@@ -2189,7 +2189,7 @@ STATIC void emit_native_call_function(emit_t *emit, mp_uint_t n_positional, mp_u
21892189
// casting operator
21902190
assert(n_positional == 1 && n_keyword == 0);
21912191
DEBUG_printf(" cast to %d\n", vtype_fun);
2192-
vtype_kind_t vtype_cast = peek_stack(emit, 1)->u_imm;
2192+
vtype_kind_t vtype_cast = peek_stack(emit, 1)->data.u_imm;
21932193
switch (peek_vtype(emit, 0)) {
21942194
case VTYPE_PYOBJ: {
21952195
vtype_kind_t vtype;

0 commit comments

Comments
 (0)