Skip to content

Commit 55baff4

Browse files
committed
Revamp qstrs: they now include length and hash.
Can now have null bytes in strings. Can define ROM qstrs per port using qstrdefsport.h
1 parent 91d457a commit 55baff4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

82 files changed

+582
-314
lines changed

py/builtin.c

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#include "nlr.h"
99
#include "misc.h"
1010
#include "mpconfig.h"
11-
#include "mpqstr.h"
11+
#include "qstr.h"
1212
#include "obj.h"
1313
#include "runtime0.h"
1414
#include "runtime.h"
@@ -139,8 +139,8 @@ MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_callable_obj, mp_builtin_callable);
139139
static mp_obj_t mp_builtin_chr(mp_obj_t o_in) {
140140
int ord = mp_obj_get_int(o_in);
141141
if (0 <= ord && ord <= 0x10ffff) {
142-
char str[2] = {ord, '\0'};
143-
return mp_obj_new_str(qstr_from_strn_copy(str, 1));
142+
char str[1] = {ord};
143+
return mp_obj_new_str(qstr_from_strn(str, 1));
144144
} else {
145145
nlr_jump(mp_obj_new_exception_msg(MP_QSTR_ValueError, "chr() arg not in range(0x110000)"));
146146
}
@@ -257,11 +257,12 @@ static mp_obj_t mp_builtin_next(mp_obj_t o) {
257257
MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_next_obj, mp_builtin_next);
258258

259259
static mp_obj_t mp_builtin_ord(mp_obj_t o_in) {
260-
const char *str = qstr_str(mp_obj_get_qstr(o_in));
261-
if (strlen(str) == 1) {
260+
uint len;
261+
const byte *str = qstr_data(mp_obj_get_qstr(o_in), &len);
262+
if (len == 1) {
262263
return mp_obj_new_int(str[0]);
263264
} else {
264-
nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_TypeError, "ord() expected a character, but string of length %d found", strlen(str)));
265+
nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_TypeError, "ord() expected a character, but string of length %d found", len));
265266
}
266267
}
267268

@@ -304,7 +305,8 @@ MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_range_obj, 1, 3, mp_builtin_range
304305
static mp_obj_t mp_builtin_repr(mp_obj_t o_in) {
305306
vstr_t *vstr = vstr_new();
306307
mp_obj_print_helper((void (*)(void *env, const char *fmt, ...))vstr_printf, vstr, o_in, PRINT_REPR);
307-
return mp_obj_new_str(qstr_from_str_take(vstr->buf, vstr->alloc));
308+
// TODO don't intern this string
309+
return mp_obj_new_str(qstr_from_strn_take(vstr->buf, vstr->alloc, vstr->len));
308310
}
309311

310312
MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_repr_obj, mp_builtin_repr);
@@ -343,7 +345,8 @@ MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_sorted_obj, 1, mp_builtin_sorted);
343345
static mp_obj_t mp_builtin_str(mp_obj_t o_in) {
344346
vstr_t *vstr = vstr_new();
345347
mp_obj_print_helper((void (*)(void*, const char*, ...))vstr_printf, vstr, o_in, PRINT_STR);
346-
return mp_obj_new_str(qstr_from_str_take(vstr->buf, vstr->alloc));
348+
// TODO don't intern this string
349+
return mp_obj_new_str(qstr_from_strn_take(vstr->buf, vstr->alloc, vstr->len));
347350
}
348351

349352
MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_str_obj, mp_builtin_str);

py/builtineval.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "nlr.h"
99
#include "misc.h"
1010
#include "mpconfig.h"
11+
#include "qstr.h"
1112
#include "lexer.h"
1213
#include "lexerunix.h"
1314
#include "parse.h"
@@ -19,10 +20,11 @@
1920
#include "builtin.h"
2021

2122
static mp_obj_t mp_builtin_eval(mp_obj_t o_in) {
22-
const char *str = qstr_str(mp_obj_get_qstr(o_in));
23+
uint str_len;
24+
const byte *str = qstr_data(mp_obj_get_qstr(o_in), &str_len);
2325

2426
// create the lexer
25-
mp_lexer_t *lex = mp_lexer_new_from_str_len("<string>", str, strlen(str), 0);
27+
mp_lexer_t *lex = mp_lexer_new_from_str_len("<string>", (const char*)str, str_len, 0);
2628

2729
// parse the string
2830
qstr parse_exc_id;

py/builtinimport.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "nlr.h"
99
#include "misc.h"
1010
#include "mpconfig.h"
11+
#include "qstr.h"
1112
#include "lexer.h"
1213
#include "lexerunix.h"
1314
#include "parse.h"

py/builtinmp.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
#include "misc.h"
99
#include "mpconfig.h"
10-
#include "mpqstr.h"
10+
#include "qstr.h"
1111
#include "obj.h"
1212
#include "runtime.h"
1313
#include "builtin.h"
@@ -38,8 +38,8 @@ void mp_module_micropython_init(void) {
3838
rt_store_name(MP_QSTR_micropython, m_mp);
3939

4040
#if MICROPY_MEM_STATS
41-
rt_store_attr(m_mp, qstr_from_str_static("mem_total"), (mp_obj_t)&mp_builtin_mem_total_obj);
42-
rt_store_attr(m_mp, qstr_from_str_static("mem_current"), (mp_obj_t)&mp_builtin_mem_current_obj);
43-
rt_store_attr(m_mp, qstr_from_str_static("mem_peak"), (mp_obj_t)&mp_builtin_mem_peak_obj);
41+
rt_store_attr(m_mp, QSTR_FROM_STR_STATIC("mem_total"), (mp_obj_t)&mp_builtin_mem_total_obj);
42+
rt_store_attr(m_mp, QSTR_FROM_STR_STATIC("mem_current"), (mp_obj_t)&mp_builtin_mem_current_obj);
43+
rt_store_attr(m_mp, QSTR_FROM_STR_STATIC("mem_peak"), (mp_obj_t)&mp_builtin_mem_peak_obj);
4444
#endif
4545
}

py/compile.c

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
#include "misc.h"
99
#include "mpconfig.h"
10-
#include "mpqstr.h"
10+
#include "qstr.h"
1111
#include "lexer.h"
1212
#include "parse.h"
1313
#include "scope.h"
@@ -273,8 +273,8 @@ static bool cpython_c_tuple_is_const(mp_parse_node_t pn) {
273273
}
274274

275275
static void cpython_c_print_quoted_str(vstr_t *vstr, qstr qstr, bool bytes) {
276-
const char *str = qstr_str(qstr);
277-
int len = strlen(str);
276+
uint len;
277+
const byte *str = qstr_data(qstr, &len);
278278
bool has_single_quote = false;
279279
bool has_double_quote = false;
280280
for (int i = 0; i < len; i++) {
@@ -1169,22 +1169,20 @@ void do_import_name(compiler_t *comp, mp_parse_node_t pn, qstr *q1, qstr *q2) {
11691169
int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
11701170
int len = n - 1;
11711171
for (int i = 0; i < n; i++) {
1172-
len += strlen(qstr_str(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i])));
1172+
len += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
11731173
}
1174-
char *str = m_new(char, len + 1);
1175-
char *str_dest = str;
1176-
str[0] = 0;
1174+
byte *q_ptr;
1175+
byte *str_dest = qstr_build_start(len, &q_ptr);
11771176
for (int i = 0; i < n; i++) {
11781177
if (i > 0) {
11791178
*str_dest++ = '.';
11801179
}
1181-
const char *str_src = qstr_str(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
1182-
size_t str_src_len = strlen(str_src);
1180+
uint str_src_len;
1181+
const byte *str_src = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &str_src_len);
11831182
memcpy(str_dest, str_src, str_src_len);
11841183
str_dest += str_src_len;
11851184
}
1186-
*str_dest = '\0';
1187-
*q2 = qstr_from_str_take(str, len + 1);
1185+
*q2 = qstr_build_end(q_ptr);
11881186
EMIT(import_name, *q2);
11891187
if (is_as) {
11901188
for (int i = 1; i < n; i++) {
@@ -1221,7 +1219,7 @@ void compile_import_from(compiler_t *comp, mp_parse_node_struct_t *pns) {
12211219
#if MICROPY_EMIT_CPYTHON
12221220
EMIT(load_const_verbatim_str, "('*',)");
12231221
#else
1224-
EMIT(load_const_str, qstr_from_str_static("*"), false);
1222+
EMIT(load_const_str, QSTR_FROM_STR_STATIC("*"), false);
12251223
EMIT(build_tuple, 1);
12261224
#endif
12271225

@@ -1248,7 +1246,9 @@ void compile_import_from(compiler_t *comp, mp_parse_node_struct_t *pns) {
12481246
vstr_printf(vstr, ", ");
12491247
}
12501248
vstr_printf(vstr, "'");
1251-
vstr_printf(vstr, qstr_str(id2));
1249+
uint len;
1250+
const byte *str = qstr_data(id2, &len);
1251+
vstr_add_strn(vstr, (const char*)str, len);
12521252
vstr_printf(vstr, "'");
12531253
}
12541254
if (n == 1) {
@@ -2128,24 +2128,21 @@ void compile_atom_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
21282128
printf("SyntaxError: cannot mix bytes and nonbytes literals\n");
21292129
return;
21302130
}
2131-
const char *str = qstr_str(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
2132-
n_bytes += strlen(str);
2131+
n_bytes += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
21332132
}
21342133

2135-
// allocate memory for concatenated string/bytes
2136-
char *cat_str = m_new(char, n_bytes + 1);
2137-
21382134
// concatenate string/bytes
2139-
char *s_dest = cat_str;
2135+
byte *q_ptr;
2136+
byte *s_dest = qstr_build_start(n_bytes, &q_ptr);
21402137
for (int i = 0; i < n; i++) {
2141-
const char *s = qstr_str(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
2142-
size_t s_len = strlen(s);
2138+
uint s_len;
2139+
const byte *s = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &s_len);
21432140
memcpy(s_dest, s, s_len);
21442141
s_dest += s_len;
21452142
}
2146-
*s_dest = '\0';
2143+
qstr q = qstr_build_end(q_ptr);
21472144

2148-
EMIT(load_const_str, qstr_from_str_take(cat_str, n_bytes + 1), string_kind == MP_PARSE_NODE_BYTES);
2145+
EMIT(load_const_str, q, string_kind == MP_PARSE_NODE_BYTES);
21492146
}
21502147

21512148
// pns needs to have 2 nodes, first is lhs of comprehension, second is PN_comp_for node
@@ -2767,7 +2764,7 @@ void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
27672764
assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
27682765
mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
27692766

2770-
qstr qstr_arg = qstr_from_str_static(".0");
2767+
qstr qstr_arg = QSTR_FROM_STR_STATIC(".0");
27712768
if (comp->pass == PASS_1) {
27722769
bool added;
27732770
id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qstr_arg, &added);

py/emitbc.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include "misc.h"
99
#include "mpconfig.h"
10+
#include "qstr.h"
1011
#include "lexer.h"
1112
#include "parse.h"
1213
#include "scope.h"

py/emitcommon.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include "misc.h"
88
#include "mpconfig.h"
9+
#include "qstr.h"
910
#include "lexer.h"
1011
#include "parse.h"
1112
#include "scope.h"

py/emitcpy.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include "misc.h"
99
#include "mpconfig.h"
10+
#include "qstr.h"
1011
#include "lexer.h"
1112
#include "parse.h"
1213
#include "scope.h"

py/emitinlinethumb.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include "misc.h"
99
#include "mpconfig.h"
10+
#include "qstr.h"
1011
#include "lexer.h"
1112
#include "parse.h"
1213
#include "scope.h"

py/emitnative.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
#include "misc.h"
2727
#include "mpconfig.h"
28+
#include "qstr.h"
2829
#include "lexer.h"
2930
#include "parse.h"
3031
#include "scope.h"

0 commit comments

Comments
 (0)