Commit 7d4b6cc8 authored by Damien George's avatar Damien George

py/emitnative: Place const objs for native code in separate const table.

This commit changes native code to handle constant objects like bytecode:
instead of storing the pointers inside the native code they are now stored
in a separate constant table (such pointers include objects like bignum,
bytes, and raw code for nested functions).  This removes the need for the
GC to scan native code for root pointers, and takes a step towards making
native code independent of the runtime (eg so it can be compiled offline by
mpy-cross).

Note that the changes to the struct scope_t did not increase its size: on a
32-bit architecture it is still 48 bytes, and on a 64-bit architecture it
decreased from 80 to 72 bytes.
parent 8a84e08d
......@@ -569,7 +569,7 @@ STATIC void close_over_variables_etc(compiler_t *comp, scope_t *this_scope, int
#if MICROPY_EMIT_NATIVE
// When creating a function/closure it will take a reference to the current globals
comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_REFGLOBALS;
comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_REFGLOBALS | MP_SCOPE_FLAG_HASCONSTS;
#endif
// make closed over variables, if any
......@@ -2665,6 +2665,9 @@ STATIC mp_obj_t get_const_object(mp_parse_node_struct_t *pns) {
}
STATIC void compile_const_object(compiler_t *comp, mp_parse_node_struct_t *pns) {
#if MICROPY_EMIT_NATIVE
comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_HASCONSTS;
#endif
EMIT_ARG(load_const_obj, get_const_object(pns));
}
......@@ -2699,6 +2702,9 @@ STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn) {
} else {
EMIT_ARG(load_const_obj, mp_obj_new_int_from_ll(arg));
}
#if MICROPY_EMIT_NATIVE
comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_HASCONSTS;
#endif
}
#else
EMIT_ARG(load_const_small_int, arg);
......@@ -2717,6 +2723,9 @@ STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn) {
const byte *data = qstr_data(arg, &len);
EMIT_ARG(load_const_obj, mp_obj_new_bytes(data, len));
}
#if MICROPY_EMIT_NATIVE
comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_HASCONSTS;
#endif
break;
case MP_PARSE_NODE_TOKEN: default:
if (arg == MP_TOKEN_NEWLINE) {
......
This diff is collapsed.
......@@ -26,13 +26,15 @@
#ifndef MICROPY_INCLUDED_PY_RUNTIME0_H
#define MICROPY_INCLUDED_PY_RUNTIME0_H
// These must fit in 8 bits; see scope.h
// The first four must fit in 8 bits, see emitbc.c
// The remaining must fit in 16 bits, see scope.h
#define MP_SCOPE_FLAG_VARARGS (0x01)
#define MP_SCOPE_FLAG_VARKEYWORDS (0x02)
#define MP_SCOPE_FLAG_GENERATOR (0x04)
#define MP_SCOPE_FLAG_DEFKWARGS (0x08)
#define MP_SCOPE_FLAG_REFGLOBALS (0x10) // used only if native emitter enabled
#define MP_SCOPE_FLAG_VIPERRET_POS (5) // top 3 bits used for viper return type
#define MP_SCOPE_FLAG_HASCONSTS (0x20) // used only if native emitter enabled
#define MP_SCOPE_FLAG_VIPERRET_POS (6) // 3 bits used for viper return type
// types for native (viper) function signature
#define MP_NATIVE_TYPE_OBJ (0x00)
......
......@@ -72,11 +72,11 @@ typedef struct _scope_t {
struct _scope_t *parent;
struct _scope_t *next;
mp_parse_node_t pn;
mp_raw_code_t *raw_code;
uint16_t source_file; // a qstr
uint16_t simple_name; // a qstr
mp_raw_code_t *raw_code;
uint8_t scope_flags; // see runtime0.h
uint8_t emit_options; // see emitglue.h
uint16_t scope_flags; // see runtime0.h
uint16_t emit_options; // see emitglue.h
uint16_t num_pos_args;
uint16_t num_kwonly_args;
uint16_t num_def_pos_args;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment