summaryrefslogtreecommitdiff
path: root/py
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2015-06-25 14:42:13 +0000
committerDamien George <damien.p.george@gmail.com>2015-06-25 14:42:13 +0000
commit59fba2d6ea31c134c9c0b88dc73cd25b236f167c (patch)
tree58038f6437d92ea5bf7b5395175c8e844e169556 /py
parented570e4b2a0a68e43b191fb0d5b45fb2ec83aca4 (diff)
py: Remove mp_load_const_bytes and instead load precreated bytes object.
Previous to this patch each time a bytes object was referenced a new instance (with the same data) was created. With this patch a single bytes object is created in the compiler and is loaded directly at execute time as a true constant (similar to loading bignum and float objects). This saves on allocating RAM and means that bytes objects can now be used when the memory manager is locked (eg in interrupts). The MP_BC_LOAD_CONST_BYTES bytecode was removed as part of this. Generated bytecode is slightly larger due to storing a pointer to the bytes object instead of the qstr identifier. Code size is reduced by about 60 bytes on Thumb2 architectures.
Diffstat (limited to 'py')
-rw-r--r--py/bc0.h1
-rw-r--r--py/compile.c27
-rw-r--r--py/emit.h4
-rw-r--r--py/emitbc.c8
-rw-r--r--py/emitcpy.c9
-rw-r--r--py/emitnative.c10
-rw-r--r--py/nativeglue.c1
-rw-r--r--py/runtime.c7
-rw-r--r--py/runtime.h4
-rw-r--r--py/runtime0.h1
-rw-r--r--py/showbc.c7
-rw-r--r--py/vm.c6
-rw-r--r--py/vmentrytable.h1
13 files changed, 28 insertions, 58 deletions
diff --git a/py/bc0.h b/py/bc0.h
index 0a2d9092f..c5a301554 100644
--- a/py/bc0.h
+++ b/py/bc0.h
@@ -33,7 +33,6 @@
#define MP_BC_LOAD_CONST_NONE (0x11)
#define MP_BC_LOAD_CONST_TRUE (0x12)
#define MP_BC_LOAD_CONST_SMALL_INT (0x14) // signed var-int
-#define MP_BC_LOAD_CONST_BYTES (0x15) // qstr
#define MP_BC_LOAD_CONST_STRING (0x16) // qstr
#define MP_BC_LOAD_CONST_OBJ (0x17) // ptr
#define MP_BC_LOAD_NULL (0x18)
diff --git a/py/compile.c b/py/compile.c
index 45217cfe9..141cd2db9 100644
--- a/py/compile.c
+++ b/py/compile.c
@@ -1078,7 +1078,7 @@ STATIC void compile_funcdef_param(compiler_t *comp, mp_parse_node_t pn) {
if (comp->have_star) {
comp->num_dict_params += 1;
#if MICROPY_EMIT_CPYTHON
- EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pn_id), false);
+ EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pn_id));
compile_node(comp, pn_equal);
#else
// in Micro Python we put the default dict parameters into a dictionary using the bytecode
@@ -1096,7 +1096,7 @@ STATIC void compile_funcdef_param(compiler_t *comp, mp_parse_node_t pn) {
// compile value then key, then store it to the dict
compile_node(comp, pn_equal);
- EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pn_id), false);
+ EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pn_id));
EMIT(store_map);
#endif
} else {
@@ -1178,7 +1178,7 @@ STATIC qstr compile_classdef_helper(compiler_t *comp, mp_parse_node_struct_t *pn
close_over_variables_etc(comp, cscope, 0, 0);
// get its name
- EMIT_ARG(load_const_str, cscope->simple_name, false);
+ EMIT_ARG(load_const_str, cscope->simple_name);
// nodes[1] has parent classes, if any
// empty parenthesis (eg class C():) gets here as an empty PN_classdef_2 and needs special handling
@@ -1553,7 +1553,7 @@ STATIC void compile_import_from(compiler_t *comp, mp_parse_node_struct_t *pns) {
#if MICROPY_EMIT_CPYTHON
EMIT_ARG(load_const_verbatim_strn, "('*',)", 6);
#else
- EMIT_ARG(load_const_str, MP_QSTR__star_, false);
+ EMIT_ARG(load_const_str, MP_QSTR__star_);
EMIT_ARG(build_tuple, 1);
#endif
@@ -1597,7 +1597,7 @@ STATIC void compile_import_from(compiler_t *comp, mp_parse_node_struct_t *pns) {
assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
- EMIT_ARG(load_const_str, id2, false);
+ EMIT_ARG(load_const_str, id2);
}
EMIT_ARG(build_tuple, n);
#endif
@@ -2531,7 +2531,7 @@ STATIC void compile_trailer_paren_helper(compiler_t *comp, mp_parse_node_t pn_ar
compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "LHS of keyword arg must be an id");
return;
}
- EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pns_arg->nodes[0]), false);
+ EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pns_arg->nodes[0]));
compile_node(comp, pns2->nodes[0]);
n_keyword += 1;
} else {
@@ -2979,8 +2979,17 @@ STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn) {
mp_uint_t arg = MP_PARSE_NODE_LEAF_ARG(pn);
switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
case MP_PARSE_NODE_ID: compile_load_id(comp, arg); break;
- case MP_PARSE_NODE_STRING: EMIT_ARG(load_const_str, arg, false); break;
- case MP_PARSE_NODE_BYTES: EMIT_ARG(load_const_str, arg, true); break;
+ case MP_PARSE_NODE_STRING: EMIT_ARG(load_const_str, arg); break;
+ case MP_PARSE_NODE_BYTES:
+ // only create and load the actual bytes object on the last pass
+ if (comp->pass != MP_PASS_EMIT) {
+ EMIT_ARG(load_const_obj, mp_const_none);
+ } else {
+ mp_uint_t len;
+ const byte *data = qstr_data(arg, &len);
+ EMIT_ARG(load_const_obj, mp_obj_new_bytes(data, len));
+ }
+ break;
case MP_PARSE_NODE_TOKEN: default:
if (arg == MP_TOKEN_NEWLINE) {
// this can occur when file_input lets through a NEWLINE (eg if file starts with a newline)
@@ -3367,7 +3376,7 @@ STATIC void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
compile_load_id(comp, MP_QSTR___name__);
compile_store_id(comp, MP_QSTR___module__);
- EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]), false); // 0 is class name
+ EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // 0 is class name
compile_store_id(comp, MP_QSTR___qualname__);
check_for_doc_string(comp, pns->nodes[2]);
diff --git a/py/emit.h b/py/emit.h
index 35ba08db9..8b980c102 100644
--- a/py/emit.h
+++ b/py/emit.h
@@ -84,7 +84,7 @@ typedef struct _emit_method_table_t {
void (*import_star)(emit_t *emit);
void (*load_const_tok)(emit_t *emit, mp_token_kind_t tok);
void (*load_const_small_int)(emit_t *emit, mp_int_t arg);
- void (*load_const_str)(emit_t *emit, qstr qst, bool bytes);
+ void (*load_const_str)(emit_t *emit, qstr qst);
void (*load_const_obj)(emit_t *emit, void *obj);
void (*load_null)(emit_t *emit);
void (*load_attr)(emit_t *emit, qstr qst);
@@ -212,7 +212,7 @@ void mp_emit_bc_import_from(emit_t *emit, qstr qst);
void mp_emit_bc_import_star(emit_t *emit);
void mp_emit_bc_load_const_tok(emit_t *emit, mp_token_kind_t tok);
void mp_emit_bc_load_const_small_int(emit_t *emit, mp_int_t arg);
-void mp_emit_bc_load_const_str(emit_t *emit, qstr qst, bool bytes);
+void mp_emit_bc_load_const_str(emit_t *emit, qstr qst);
void mp_emit_bc_load_const_obj(emit_t *emit, void *obj);
void mp_emit_bc_load_null(emit_t *emit);
void mp_emit_bc_load_attr(emit_t *emit, qstr qst);
diff --git a/py/emitbc.c b/py/emitbc.c
index f99e70322..fef9054c7 100644
--- a/py/emitbc.c
+++ b/py/emitbc.c
@@ -486,13 +486,9 @@ void mp_emit_bc_load_const_small_int(emit_t *emit, mp_int_t arg) {
}
}
-void mp_emit_bc_load_const_str(emit_t *emit, qstr qst, bool bytes) {
+void mp_emit_bc_load_const_str(emit_t *emit, qstr qst) {
emit_bc_pre(emit, 1);
- if (bytes) {
- emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_CONST_BYTES, qst);
- } else {
- emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_CONST_STRING, qst);
- }
+ emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_CONST_STRING, qst);
}
void mp_emit_bc_load_const_obj(emit_t *emit, void *obj) {
diff --git a/py/emitcpy.c b/py/emitcpy.c
index dc96c529f..3bdec1716 100644
--- a/py/emitcpy.c
+++ b/py/emitcpy.c
@@ -176,7 +176,7 @@ STATIC void emit_cpy_load_const_small_int(emit_t *emit, mp_int_t arg) {
}
}
-STATIC void print_quoted_str(qstr qst, bool bytes) {
+STATIC void print_quoted_str(qstr qst) {
const char *str = qstr_str(qst);
int len = strlen(str);
bool has_single_quote = false;
@@ -188,9 +188,6 @@ STATIC void print_quoted_str(qstr qst, bool bytes) {
has_double_quote = true;
}
}
- if (bytes) {
- printf("b");
- }
int quote_char = '\'';
if (has_single_quote && !has_double_quote) {
quote_char = '"';
@@ -213,11 +210,11 @@ STATIC void print_quoted_str(qstr qst, bool bytes) {
printf("%c", quote_char);
}
-STATIC void emit_cpy_load_const_str(emit_t *emit, qstr qst, bool bytes) {
+STATIC void emit_cpy_load_const_str(emit_t *emit, qstr qst) {
emit_pre(emit, 1, 3);
if (emit->pass == MP_PASS_EMIT) {
printf("LOAD_CONST ");
- print_quoted_str(qst, bytes);
+ print_quoted_str(qst);
printf("\n");
}
}
diff --git a/py/emitnative.c b/py/emitnative.c
index 974b8fc7b..0d3f78da4 100644
--- a/py/emitnative.c
+++ b/py/emitnative.c
@@ -167,7 +167,6 @@
STATIC byte mp_f_n_args[MP_F_NUMBER_OF] = {
[MP_F_CONVERT_OBJ_TO_NATIVE] = 2,
[MP_F_CONVERT_NATIVE_TO_OBJ] = 2,
- [MP_F_LOAD_CONST_BYTES] = 1,
[MP_F_LOAD_NAME] = 1,
[MP_F_LOAD_GLOBAL] = 1,
[MP_F_LOAD_BUILD_CLASS] = 0,
@@ -1295,7 +1294,7 @@ STATIC void emit_native_load_const_small_int(emit_t *emit, mp_int_t arg) {
}
}
-STATIC void emit_native_load_const_str(emit_t *emit, qstr qst, bool bytes) {
+STATIC void emit_native_load_const_str(emit_t *emit, qstr qst) {
emit_native_pre(emit);
// TODO: Eventually we want to be able to work with raw pointers in viper to
// do native array access. For now we just load them as any other object.
@@ -1308,12 +1307,7 @@ STATIC void emit_native_load_const_str(emit_t *emit, qstr qst, bool bytes) {
} else
*/
{
- if (bytes) {
- emit_call_with_imm_arg(emit, MP_F_LOAD_CONST_BYTES, qst, REG_ARG_1);
- emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
- } else {
- emit_post_push_imm(emit, VTYPE_PYOBJ, (mp_uint_t)MP_OBJ_NEW_QSTR(qst));
- }
+ emit_post_push_imm(emit, VTYPE_PYOBJ, (mp_uint_t)MP_OBJ_NEW_QSTR(qst));
}
}
diff --git a/py/nativeglue.c b/py/nativeglue.c
index 4252bb88c..87213710c 100644
--- a/py/nativeglue.c
+++ b/py/nativeglue.c
@@ -92,7 +92,6 @@ void mp_native_raise(mp_obj_t o) {
void *const mp_fun_table[MP_F_NUMBER_OF] = {
mp_convert_obj_to_native,
mp_convert_native_to_obj,
- mp_load_const_bytes,
mp_load_name,
mp_load_global,
mp_load_build_class,
diff --git a/py/runtime.c b/py/runtime.c
index 5fcfa6f90..fb592920b 100644
--- a/py/runtime.c
+++ b/py/runtime.c
@@ -104,13 +104,6 @@ void mp_deinit(void) {
#endif
}
-mp_obj_t mp_load_const_bytes(qstr qst) {
- DEBUG_OP_printf("load b'%s'\n", qstr_str(qst));
- mp_uint_t len;
- const byte *data = qstr_data(qst, &len);
- return mp_obj_new_bytes(data, len);
-}
-
mp_obj_t mp_load_name(qstr qst) {
// logic: search locals, globals, builtins
DEBUG_OP_printf("load name %s\n", qstr_str(qst));
diff --git a/py/runtime.h b/py/runtime.h
index 419b1d059..2282ea02c 100644
--- a/py/runtime.h
+++ b/py/runtime.h
@@ -88,10 +88,6 @@ void mp_delete_global(qstr qst);
mp_obj_t mp_unary_op(mp_uint_t op, mp_obj_t arg);
mp_obj_t mp_binary_op(mp_uint_t op, mp_obj_t lhs, mp_obj_t rhs);
-mp_obj_t mp_load_const_int(qstr qst);
-mp_obj_t mp_load_const_dec(qstr qst);
-mp_obj_t mp_load_const_bytes(qstr qst);
-
mp_obj_t mp_call_function_0(mp_obj_t fun);
mp_obj_t mp_call_function_1(mp_obj_t fun, mp_obj_t arg);
mp_obj_t mp_call_function_2(mp_obj_t fun, mp_obj_t arg1, mp_obj_t arg2);
diff --git a/py/runtime0.h b/py/runtime0.h
index de637fb46..294c17bf6 100644
--- a/py/runtime0.h
+++ b/py/runtime0.h
@@ -109,7 +109,6 @@ typedef enum {
typedef enum {
MP_F_CONVERT_OBJ_TO_NATIVE = 0,
MP_F_CONVERT_NATIVE_TO_OBJ,
- MP_F_LOAD_CONST_BYTES,
MP_F_LOAD_NAME,
MP_F_LOAD_GLOBAL,
MP_F_LOAD_BUILD_CLASS,
diff --git a/py/showbc.c b/py/showbc.c
index e08140e6b..2da8d3feb 100644
--- a/py/showbc.c
+++ b/py/showbc.c
@@ -155,11 +155,6 @@ const byte *mp_bytecode_print_str(const byte *ip) {
break;
}
- case MP_BC_LOAD_CONST_BYTES:
- DECODE_QSTR;
- printf("LOAD_CONST_BYTES %s", qstr_str(qst));
- break;
-
case MP_BC_LOAD_CONST_STRING:
DECODE_QSTR;
printf("LOAD_CONST_STRING '%s'", qstr_str(qst));
@@ -168,7 +163,7 @@ const byte *mp_bytecode_print_str(const byte *ip) {
case MP_BC_LOAD_CONST_OBJ:
DECODE_PTR;
printf("LOAD_CONST_OBJ %p=", (void*)unum);
- mp_obj_print((mp_obj_t)unum, PRINT_REPR);
+ mp_obj_print_helper(&mp_plat_print, (mp_obj_t)unum, PRINT_REPR);
break;
case MP_BC_LOAD_NULL:
diff --git a/py/vm.c b/py/vm.c
index 3423e9e5c..08749af21 100644
--- a/py/vm.c
+++ b/py/vm.c
@@ -205,12 +205,6 @@ dispatch_loop:
DISPATCH();
}
- ENTRY(MP_BC_LOAD_CONST_BYTES): {
- DECODE_QSTR;
- PUSH(mp_load_const_bytes(qst));
- DISPATCH();
- }
-
ENTRY(MP_BC_LOAD_CONST_STRING): {
DECODE_QSTR;
PUSH(MP_OBJ_NEW_QSTR(qst));
diff --git a/py/vmentrytable.h b/py/vmentrytable.h
index 413914b6e..d71a8d4f6 100644
--- a/py/vmentrytable.h
+++ b/py/vmentrytable.h
@@ -35,7 +35,6 @@ static void* entry_table[256] = {
[MP_BC_LOAD_CONST_NONE] = &&entry_MP_BC_LOAD_CONST_NONE,
[MP_BC_LOAD_CONST_TRUE] = &&entry_MP_BC_LOAD_CONST_TRUE,
[MP_BC_LOAD_CONST_SMALL_INT] = &&entry_MP_BC_LOAD_CONST_SMALL_INT,
- [MP_BC_LOAD_CONST_BYTES] = &&entry_MP_BC_LOAD_CONST_BYTES,
[MP_BC_LOAD_CONST_STRING] = &&entry_MP_BC_LOAD_CONST_STRING,
[MP_BC_LOAD_CONST_OBJ] = &&entry_MP_BC_LOAD_CONST_OBJ,
[MP_BC_LOAD_NULL] = &&entry_MP_BC_LOAD_NULL,