Skip to content

Commit 6942f80

Browse files
committed
py: Add qstr cfg capability; generate QSTR_NULL and QSTR_ from script.
1 parent e233a55 commit 6942f80

File tree

4 files changed

+26
-10
lines changed

4 files changed

+26
-10
lines changed

py/makeqstrdata.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,25 @@ def compute_hash(qstr):
4040

4141
def do_work(infiles):
4242
# read the qstrs in from the input files
43+
qcfgs = {}
4344
qstrs = {}
4445
for infile in infiles:
4546
with open(infile, 'rt') as f:
4647
for line in f:
48+
line = line.strip()
49+
50+
# is this a config line?
51+
match = re.match(r'^QCFG\((.+), (.+)\)', line)
52+
if match:
53+
value = match.group(2)
54+
if value[0] == '(' and value[-1] == ')':
55+
# strip parenthesis from config value
56+
value = value[1:-1]
57+
qcfgs[match.group(1)] = value
58+
continue
59+
4760
# is this a QSTR line?
48-
match = re.match(r'^Q\((.+)\)$', line.strip())
61+
match = re.match(r'^Q\((.*)\)$', line)
4962
if not match:
5063
continue
5164

@@ -63,11 +76,13 @@ def do_work(infiles):
6376
# process the qstrs, printing out the generated C header file
6477
print('// This file was automatically generated by makeqstrdata.py')
6578
print('')
79+
# add NULL qstr with no hash or data
80+
print('QDEF(MP_QSTR_NULL, (const byte*)"\\x00\\x00\\x00\\x00" "")')
6681
for order, ident, qstr in sorted(qstrs.values(), key=lambda x: x[0]):
6782
qhash = compute_hash(qstr)
6883
qlen = len(qstr)
6984
qdata = qstr.replace('"', '\\"')
70-
print('Q(%s, (const byte*)"\\x%02x\\x%02x\\x%02x\\x%02x" "%s")' % (ident, qhash & 0xff, (qhash >> 8) & 0xff, qlen & 0xff, (qlen >> 8) & 0xff, qdata))
85+
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))
7186

7287
return True
7388

py/qstr.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,9 @@ STATIC const qstr_pool_t const_pool = {
7575
10, // set so that the first dynamically allocated pool is twice this size; must be <= the len (just below)
7676
MP_QSTR_number_of, // corresponds to number of strings in array just below
7777
{
78-
(const byte*) "\0\0\0\0", // invalid/no qstr has empty data
79-
(const byte*) "\x05\x15\0\0", // empty qstr with hash=5381=0x1505
80-
#define Q(id, str) str,
78+
#define QDEF(id, str) str,
8179
#include "genhdr/qstrdefs.generated.h"
82-
#undef Q
80+
#undef QDEF
8381
},
8482
};
8583

py/qstr.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,11 @@
3535
// Note: it would be possible to define MP_QSTR_xxx as qstr_from_str_static("xxx")
3636
// for qstrs that are referenced this way, but you don't want to have them in ROM.
3737

38+
// first entry in enum will be MP_QSTR_NULL=0, which indicates invalid/no qstr
3839
enum {
39-
MP_QSTR_NULL = 0, // indicates invalid/no qstr
40-
MP_QSTR_ = 1, // the empty qstr
41-
#define Q(id, str) MP_QSTR_##id,
40+
#define QDEF(id, str) id,
4241
#include "genhdr/qstrdefs.generated.h"
43-
#undef Q
42+
#undef QDEF
4443
MP_QSTR_number_of,
4544
};
4645

py/qstrdefs.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@
2929
// All the qstr definitions in this file are available as constants.
3030
// That is, they are in ROM and you can reference them simply as MP_QSTR_xxxx.
3131

32+
// qstr configuration passed to makeqstrdata.py of the form QCFG(key, value)
33+
//QCFG(somekey, somevalue)
34+
35+
Q()
3236
Q(*)
3337
Q(__build_class__)
3438
Q(__class__)

0 commit comments

Comments
 (0)