From 6942f80a8feb521ff4dc2b457fa5cb3d039bee54 Mon Sep 17 00:00:00 2001 From: Damien George Date: Sun, 11 Jan 2015 17:52:45 +0000 Subject: [PATCH] py: Add qstr cfg capability; generate QSTR_NULL and QSTR_ from script. --- py/makeqstrdata.py | 19 +++++++++++++++++-- py/qstr.c | 6 ++---- py/qstr.h | 7 +++---- py/qstrdefs.h | 4 ++++ 4 files changed, 26 insertions(+), 10 deletions(-) diff --git a/py/makeqstrdata.py b/py/makeqstrdata.py index f5ce2778a9..c8e998ed11 100644 --- a/py/makeqstrdata.py +++ b/py/makeqstrdata.py @@ -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 diff --git a/py/qstr.c b/py/qstr.c index 44c69dc230..bb31a08b39 100644 --- a/py/qstr.c +++ b/py/qstr.c @@ -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 }, }; diff --git a/py/qstr.h b/py/qstr.h index d5ec1fed67..776c2ab8f7 100644 --- a/py/qstr.h +++ b/py/qstr.h @@ -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, }; diff --git a/py/qstrdefs.h b/py/qstrdefs.h index 04890a800f..514116b65d 100644 --- a/py/qstrdefs.h +++ b/py/qstrdefs.h @@ -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__)