Commit 6942f80a authored by Damien George's avatar Damien George
Browse files

py: Add qstr cfg capability; generate QSTR_NULL and QSTR_ from script.

parent e233a55a
......@@ -40,12 +40,25 @@ def compute_hash(qstr):
def do_work(infiles):
# read the qstrs in from the input files
qcfgs = {}
qstrs = {}
for infile in infiles:
with open(infile, 'rt') as f:
for line in f:
line = line.strip()
# is this a config line?
match = re.match(r'^QCFG\((.+), (.+)\)', line)
if match:
value = match.group(2)
if value[0] == '(' and value[-1] == ')':
# strip parenthesis from config value
value = value[1:-1]
qcfgs[match.group(1)] = value
continue
# is this a QSTR line?
match = re.match(r'^Q\((.+)\)$', line.strip())
match = re.match(r'^Q\((.*)\)$', line)
if not match:
continue
......@@ -63,11 +76,13 @@ def do_work(infiles):
# process the qstrs, printing out 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" "")')
for order, ident, qstr in sorted(qstrs.values(), key=lambda x: x[0]):
qhash = compute_hash(qstr)
qlen = len(qstr)
qdata = qstr.replace('"', '\\"')
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))
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))
return True
......
......@@ -75,11 +75,9 @@ STATIC const qstr_pool_t const_pool = {
10, // set so that the first dynamically allocated pool is twice this size; must be <= the len (just below)
MP_QSTR_number_of, // corresponds to number of strings in array just below
{
(const byte*) "\0\0\0\0", // invalid/no qstr has empty data
(const byte*) "\x05\x15\0\0", // empty qstr with hash=5381=0x1505
#define Q(id, str) str,
#define QDEF(id, str) str,
#include "genhdr/qstrdefs.generated.h"
#undef Q
#undef QDEF
},
};
......
......@@ -35,12 +35,11 @@
// Note: it would be possible to define MP_QSTR_xxx as qstr_from_str_static("xxx")
// for qstrs that are referenced this way, but you don't want to have them in ROM.
// first entry in enum will be MP_QSTR_NULL=0, which indicates invalid/no qstr
enum {
MP_QSTR_NULL = 0, // indicates invalid/no qstr
MP_QSTR_ = 1, // the empty qstr
#define Q(id, str) MP_QSTR_##id,
#define QDEF(id, str) id,
#include "genhdr/qstrdefs.generated.h"
#undef Q
#undef QDEF
MP_QSTR_number_of,
};
......
......@@ -29,6 +29,10 @@
// All the qstr definitions in this file are available as constants.
// 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)
Q()
Q(*)
Q(__build_class__)
Q(__class__)
......
Supports Markdown
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