Skip to content

Commit 71019ae

Browse files
committed
py/grammar: Group no-compile grammar rules together to shrink tables.
Grammar rules have 2 variants: ones that are attached to a specific compile function which is called to compile that grammar node, and ones that don't have a compile function and are instead just inspected to see what form they take. In the compiler there is a table of all grammar rules, with each entry having a pointer to the associated compile function. Those rules with no compile function have a null pointer. There are 120 such rules, so that's 120 words of essentially wasted code space. By grouping together the compile vs no-compile rules we can put all the no-compile rules at the end of the list of rules, and then we don't need to store the null pointers. We just have a truncated table and it's guaranteed that when indexing this table we only index the first half, the half with populated pointers. This patch implements such a grouping by having a specific macro for the compile vs no-compile grammar rules (DEF_RULE vs DEF_RULE_NC). It saves around 460 bytes of code on 32-bit archs.
1 parent 7839b8b commit 71019ae

4 files changed

Lines changed: 171 additions & 128 deletions

File tree

py/compile.c

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,21 @@
4141
// TODO need to mangle __attr names
4242

4343
typedef enum {
44+
// define rules with a compile function
4445
#define DEF_RULE(rule, comp, kind, ...) PN_##rule,
46+
#define DEF_RULE_NC(rule, kind, ...)
4547
#include "py/grammar.h"
4648
#undef DEF_RULE
47-
PN_maximum_number_of,
49+
#undef DEF_RULE_NC
4850
PN_string, // special node for non-interned string
4951
PN_bytes, // special node for non-interned bytes
5052
PN_const_object, // special node for a constant, generic Python object
53+
// define rules without a compile function
54+
#define DEF_RULE(rule, comp, kind, ...)
55+
#define DEF_RULE_NC(rule, kind, ...) PN_##rule,
56+
#include "py/grammar.h"
57+
#undef DEF_RULE
58+
#undef DEF_RULE_NC
5159
} pn_kind_t;
5260

5361
#define NEED_METHOD_TABLE MICROPY_EMIT_NATIVE
@@ -2680,14 +2688,14 @@ STATIC void compile_const_object(compiler_t *comp, mp_parse_node_struct_t *pns)
26802688

26812689
typedef void (*compile_function_t)(compiler_t*, mp_parse_node_struct_t*);
26822690
STATIC const compile_function_t compile_function[] = {
2683-
#define nc NULL
2691+
// only define rules with a compile function
26842692
#define c(f) compile_##f
26852693
#define DEF_RULE(rule, comp, kind, ...) comp,
2694+
#define DEF_RULE_NC(rule, kind, ...)
26862695
#include "py/grammar.h"
2687-
#undef nc
26882696
#undef c
26892697
#undef DEF_RULE
2690-
NULL,
2698+
#undef DEF_RULE_NC
26912699
compile_string,
26922700
compile_bytes,
26932701
compile_const_object,
@@ -2743,8 +2751,8 @@ STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn) {
27432751
} else {
27442752
mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
27452753
EMIT_ARG(set_source_line, pns->source_line);
2754+
assert(MP_PARSE_NODE_STRUCT_KIND(pns) <= PN_const_object);
27462755
compile_function_t f = compile_function[MP_PARSE_NODE_STRUCT_KIND(pns)];
2747-
assert(f != NULL);
27482756
f(comp, pns);
27492757
}
27502758
}

py/emitinlinethumb.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,21 @@
3636
#if MICROPY_EMIT_INLINE_THUMB
3737

3838
typedef enum {
39+
// define rules with a compile function
3940
#define DEF_RULE(rule, comp, kind, ...) PN_##rule,
41+
#define DEF_RULE_NC(rule, kind, ...)
4042
#include "py/grammar.h"
4143
#undef DEF_RULE
42-
PN_maximum_number_of,
44+
#undef DEF_RULE_NC
45+
PN_string, // special node for non-interned string
46+
PN_bytes, // special node for non-interned bytes
47+
PN_const_object, // special node for a constant, generic Python object
48+
// define rules without a compile function
49+
#define DEF_RULE(rule, comp, kind, ...)
50+
#define DEF_RULE_NC(rule, kind, ...) PN_##rule,
51+
#include "py/grammar.h"
52+
#undef DEF_RULE
53+
#undef DEF_RULE_NC
4354
} pn_kind_t;
4455

4556
struct _emit_inline_asm_t {

0 commit comments

Comments
 (0)