Skip to content

Commit dab1385

Browse files
committed
py: Add load_const_obj to emitter, add LOAD_CONST_OBJ to bytecode.
This allows to directly load a Python object to the Python stack. See issue adafruit#722 for background.
1 parent d710cef commit dab1385

8 files changed

Lines changed: 33 additions & 0 deletions

File tree

py/bc0.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#define MP_BC_LOAD_CONST_DEC (0x16) // qstr
3939
#define MP_BC_LOAD_CONST_BYTES (0x17) // qstr
4040
#define MP_BC_LOAD_CONST_STRING (0x18) // qstr
41+
#define MP_BC_LOAD_CONST_OBJ (0x09) // ptr; TODO renumber to be in order
4142
#define MP_BC_LOAD_NULL (0x19)
4243

4344
#define MP_BC_LOAD_FAST_N (0x1a) // uint

py/emit.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ typedef struct _emit_method_table_t {
8080
void (*load_const_int)(emit_t *emit, qstr qst);
8181
void (*load_const_dec)(emit_t *emit, qstr qst);
8282
void (*load_const_str)(emit_t *emit, qstr qst, bool bytes);
83+
void (*load_const_obj)(emit_t *emit, void *obj);
8384
void (*load_null)(emit_t *emit);
8485
void (*load_fast)(emit_t *emit, qstr qst, mp_uint_t id_flags, mp_uint_t local_num);
8586
void (*load_deref)(emit_t *emit, qstr qst, mp_uint_t local_num);

py/emitbc.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,11 @@ STATIC void emit_bc_load_const_str(emit_t *emit, qstr qst, bool bytes) {
487487
}
488488
}
489489

490+
STATIC void emit_bc_load_const_obj(emit_t *emit, void *obj) {
491+
emit_bc_pre(emit, 1);
492+
emit_write_bytecode_byte_ptr(emit, MP_BC_LOAD_CONST_OBJ, obj);
493+
}
494+
490495
STATIC void emit_bc_load_null(emit_t *emit) {
491496
emit_bc_pre(emit, 1);
492497
emit_write_bytecode_byte(emit, MP_BC_LOAD_NULL);
@@ -917,6 +922,7 @@ const emit_method_table_t emit_bc_method_table = {
917922
emit_bc_load_const_int,
918923
emit_bc_load_const_dec,
919924
emit_bc_load_const_str,
925+
emit_bc_load_const_obj,
920926
emit_bc_load_null,
921927
emit_bc_load_fast,
922928
emit_bc_load_deref,

py/emitcpy.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,15 @@ STATIC void emit_cpy_load_const_str(emit_t *emit, qstr qst, bool bytes) {
231231
}
232232
}
233233

234+
STATIC void emit_cpy_load_const_obj(emit_t *emit, void *obj) {
235+
emit_pre(emit, 1, 3);
236+
if (emit->pass == MP_PASS_EMIT) {
237+
printf("LOAD_CONST ");
238+
mp_obj_print(obj, PRINT_REPR);
239+
printf("\n");
240+
}
241+
}
242+
234243
STATIC void emit_cpy_load_null(emit_t *emit) {
235244
// unused for cpy
236245
assert(0);
@@ -833,6 +842,7 @@ const emit_method_table_t emit_cpython_method_table = {
833842
emit_cpy_load_const_int,
834843
emit_cpy_load_const_dec,
835844
emit_cpy_load_const_str,
845+
emit_cpy_load_const_obj,
836846
emit_cpy_load_null,
837847
emit_cpy_load_fast,
838848
emit_cpy_load_deref,

py/emitnative.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1172,6 +1172,12 @@ STATIC void emit_native_load_const_str(emit_t *emit, qstr qst, bool bytes) {
11721172
}
11731173
}
11741174

1175+
STATIC void emit_native_load_const_obj(emit_t *emit, void *obj) {
1176+
emit_native_pre(emit);
1177+
ASM_MOV_ALIGNED_IMM_TO_REG(emit->as, (mp_uint_t)obj, REG_RET);
1178+
emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1179+
}
1180+
11751181
STATIC void emit_native_load_null(emit_t *emit) {
11761182
emit_native_pre(emit);
11771183
emit_post_push_imm(emit, VTYPE_PYOBJ, 0);
@@ -2274,6 +2280,7 @@ const emit_method_table_t EXPORT_FUN(method_table) = {
22742280
emit_native_load_const_int,
22752281
emit_native_load_const_dec,
22762282
emit_native_load_const_str,
2283+
emit_native_load_const_obj,
22772284
emit_native_load_null,
22782285
emit_native_load_fast,
22792286
emit_native_load_deref,

py/emitpass1.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ const emit_method_table_t emit_pass1_method_table = {
190190
(void*)emit_pass1_dummy,
191191
(void*)emit_pass1_dummy,
192192
(void*)emit_pass1_dummy,
193+
(void*)emit_pass1_dummy,
193194
#if MICROPY_PY_BUILTINS_SET
194195
(void*)emit_pass1_dummy,
195196
(void*)emit_pass1_dummy,

py/vm.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,12 @@ mp_vm_return_kind_t mp_execute_bytecode(mp_code_state *code_state, volatile mp_o
224224
DISPATCH();
225225
}
226226

227+
ENTRY(MP_BC_LOAD_CONST_OBJ): {
228+
DECODE_PTR;
229+
PUSH(ptr);
230+
DISPATCH();
231+
}
232+
227233
ENTRY(MP_BC_LOAD_NULL):
228234
PUSH(MP_OBJ_NULL);
229235
DISPATCH();

py/vmentrytable.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ static void* entry_table[256] = {
4040
[MP_BC_LOAD_CONST_DEC] = &&entry_MP_BC_LOAD_CONST_DEC,
4141
[MP_BC_LOAD_CONST_BYTES] = &&entry_MP_BC_LOAD_CONST_BYTES,
4242
[MP_BC_LOAD_CONST_STRING] = &&entry_MP_BC_LOAD_CONST_STRING,
43+
[MP_BC_LOAD_CONST_OBJ] = &&entry_MP_BC_LOAD_CONST_OBJ,
4344
[MP_BC_LOAD_NULL] = &&entry_MP_BC_LOAD_NULL,
4445
[MP_BC_LOAD_FAST_N] = &&entry_MP_BC_LOAD_FAST_N,
4546
[MP_BC_LOAD_DEREF] = &&entry_MP_BC_LOAD_DEREF,

0 commit comments

Comments
 (0)