summaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
-rw-r--r--py/makeqstrdata.py17
-rw-r--r--py/mpconfig.h7
-rw-r--r--py/qstr.c32
-rw-r--r--py/qstrdefs.h2
4 files changed, 41 insertions, 17 deletions
diff --git a/py/makeqstrdata.py b/py/makeqstrdata.py
index c8e998ed11..48fd9e09d4 100644
--- a/py/makeqstrdata.py
+++ b/py/makeqstrdata.py
@@ -73,16 +73,27 @@ def do_work(infiles):
# add the qstr to the list, with order number to retain original order in file
qstrs[ident] = (len(qstrs), ident, qstr)
- # process the qstrs, printing out the generated C header file
+ # get config variables
+ cfg_bytes_len = int(qcfgs['BYTES_IN_LEN'])
+ cfg_max_len = 1 << (8 * cfg_bytes_len)
+
+ # print out the starte of the generated C header file
print('// This file was automatically generated by makeqstrdata.py')
print('')
+
# add NULL qstr with no hash or data
- print('QDEF(MP_QSTR_NULL, (const byte*)"\\x00\\x00\\x00\\x00" "")')
+ print('QDEF(MP_QSTR_NULL, (const byte*)"\\x00\\x00%s" "")' % ('\\x00' * cfg_bytes_len))
+
+ # go through each qstr and print it out
for order, ident, qstr in sorted(qstrs.values(), key=lambda x: x[0]):
qhash = compute_hash(qstr)
qlen = len(qstr)
qdata = qstr.replace('"', '\\"')
- print('QDEF(MP_QSTR_%s, (const byte*)"\\x%02x\\x%02x\\x%02x\\x%02x" "%s")' % (ident, qhash & 0xff, (qhash >> 8) & 0xff, qlen & 0xff, (qlen >> 8) & 0xff, qdata))
+ if qlen >= cfg_max_len:
+ print('qstr is too long:', qstr)
+ assert False
+ qlen_str = ('\\x%02x' * cfg_bytes_len) % tuple(qlen.to_bytes(cfg_bytes_len, 'little'))
+ print('QDEF(MP_QSTR_%s, (const byte*)"\\x%02x\\x%02x%s" "%s")' % (ident, qhash & 0xff, (qhash >> 8) & 0xff, qlen_str, qdata))
return True
diff --git a/py/mpconfig.h b/py/mpconfig.h
index dcc116daf2..541f7c75a5 100644
--- a/py/mpconfig.h
+++ b/py/mpconfig.h
@@ -112,6 +112,13 @@
#define MICROPY_MODULE_DICT_SIZE (1)
#endif
+// Number of bytes used to store qstr length
+// Dictates hard limit on maximum Python identifier length, but 1 byte
+// (limit of 255 bytes in an identifier) should be enough for everyone
+#ifndef MICROPY_QSTR_BYTES_IN_LEN
+#define MICROPY_QSTR_BYTES_IN_LEN (1)
+#endif
+
/*****************************************************************************/
/* Micro Python emitters */
diff --git a/py/qstr.c b/py/qstr.c
index bb31a08b39..c244738a5e 100644
--- a/py/qstr.c
+++ b/py/qstr.c
@@ -50,9 +50,17 @@
// - \0 terminated (for now, so they can be printed using printf)
#define Q_GET_HASH(q) ((q)[0] | ((q)[1] << 8))
-#define Q_GET_ALLOC(q) (4 + Q_GET_LENGTH(q) + 1)
-#define Q_GET_LENGTH(q) ((q)[2] | ((q)[3] << 8))
-#define Q_GET_DATA(q) ((q) + 4)
+#define Q_GET_ALLOC(q) (2 + MICROPY_QSTR_BYTES_IN_LEN + Q_GET_LENGTH(q) + 1)
+#define Q_GET_DATA(q) ((q) + 2 + MICROPY_QSTR_BYTES_IN_LEN)
+#if MICROPY_QSTR_BYTES_IN_LEN == 1
+ #define Q_GET_LENGTH(q) ((q)[2])
+ #define Q_SET_LENGTH(q, len) do { (q)[2] = (len); } while (0)
+#elif MICROPY_QSTR_BYTES_IN_LEN == 2
+ #define Q_GET_LENGTH(q) ((q)[2] | ((q)[3] << 8))
+ #define Q_SET_LENGTH(q, len) do { (q)[2] = (len); (q)[3] = (len) >> 8; } while (0)
+#else
+ #error unimplemented qstr length decoding
+#endif
// this must match the equivalent function in makeqstrdata.py
mp_uint_t qstr_compute_hash(const byte *data, mp_uint_t len) {
@@ -143,23 +151,21 @@ qstr qstr_from_strn(const char *str, mp_uint_t len) {
qstr q = qstr_find_strn(str, len);
if (q == 0) {
mp_uint_t hash = qstr_compute_hash((const byte*)str, len);
- byte *q_ptr = m_new(byte, 4 + len + 1);
+ byte *q_ptr = m_new(byte, 2 + MICROPY_QSTR_BYTES_IN_LEN + len + 1);
q_ptr[0] = hash;
q_ptr[1] = hash >> 8;
- q_ptr[2] = len;
- q_ptr[3] = len >> 8;
- memcpy(q_ptr + 4, str, len);
- q_ptr[4 + len] = '\0';
+ Q_SET_LENGTH(q_ptr, len);
+ memcpy(q_ptr + 2 + MICROPY_QSTR_BYTES_IN_LEN, str, len);
+ q_ptr[2 + MICROPY_QSTR_BYTES_IN_LEN + len] = '\0';
q = qstr_add(q_ptr);
}
return q;
}
byte *qstr_build_start(mp_uint_t len, byte **q_ptr) {
- assert(len <= 65535);
- *q_ptr = m_new(byte, 4 + len + 1);
- (*q_ptr)[2] = len;
- (*q_ptr)[3] = len >> 8;
+ assert(len < (1 << (8 * MICROPY_QSTR_BYTES_IN_LEN)));
+ *q_ptr = m_new(byte, 2 + MICROPY_QSTR_BYTES_IN_LEN + len + 1);
+ Q_SET_LENGTH(*q_ptr, len);
return Q_GET_DATA(*q_ptr);
}
@@ -170,7 +176,7 @@ qstr qstr_build_end(byte *q_ptr) {
mp_uint_t hash = qstr_compute_hash(Q_GET_DATA(q_ptr), len);
q_ptr[0] = hash;
q_ptr[1] = hash >> 8;
- q_ptr[4 + len] = '\0';
+ q_ptr[2 + MICROPY_QSTR_BYTES_IN_LEN + len] = '\0';
q = qstr_add(q_ptr);
} else {
m_del(byte, q_ptr, Q_GET_ALLOC(q_ptr));
diff --git a/py/qstrdefs.h b/py/qstrdefs.h
index 514116b65d..71a523c2d6 100644
--- a/py/qstrdefs.h
+++ b/py/qstrdefs.h
@@ -30,7 +30,7 @@
// That is, they are in ROM and you can reference them simply as MP_QSTR_xxxx.
// qstr configuration passed to makeqstrdata.py of the form QCFG(key, value)
-//QCFG(somekey, somevalue)
+QCFG(BYTES_IN_LEN, MICROPY_QSTR_BYTES_IN_LEN)
Q()
Q(*)