Skip to content

Commit 1e1779e

Browse files
committed
py: Reluctantly add an extra pass to bytecode compiler.
Bytecode also needs a pass to compute the stack size. This is because the state size of the bytecode function is encoded as a variable uint, so we must know the value of this uint before we encode it (otherwise the size of the generated code changes from one pass to the next). Having an entire pass for this seems wasteful (in time). Alternative is to allocate fixed space for the state size (would need 3-4 bytes to be general, when 1 byte is usually sufficient) which uses a bit of extra RAM per bytecode function, and makes the code less elegant in places where this uint is encoded/decoded. So, for now, opt for an extra pass.
1 parent 2127e9a commit 1e1779e

2 files changed

Lines changed: 136 additions & 4 deletions

File tree

py/compile.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3729,10 +3729,6 @@ mp_obj_t mp_compile(mp_parse_node_t pn, qstr source_file, uint emit_opt, bool is
37293729
#endif
37303730
comp->emit = emit_native;
37313731
EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_ENABLE, s->emit_options == MP_EMIT_OPT_VIPER, 0);
3732-
3733-
// native emitters need an extra pass to compute stack size
3734-
compile_scope(comp, s, MP_PASS_STACK_SIZE);
3735-
37363732
break;
37373733
#endif // MICROPY_EMIT_NATIVE
37383734

@@ -3746,6 +3742,9 @@ mp_obj_t mp_compile(mp_parse_node_t pn, qstr source_file, uint emit_opt, bool is
37463742
}
37473743
#endif // !MICROPY_EMIT_CPYTHON
37483744

3745+
// need a pass to compute stack size
3746+
compile_scope(comp, s, MP_PASS_STACK_SIZE);
3747+
37493748
// second last pass: compute code size
37503749
if (comp->compile_error == MP_OBJ_NULL) {
37513750
compile_scope(comp, s, MP_PASS_CODE_SIZE);

tests/basics/fun_largestate.py

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
# test large function (stack) state
2+
3+
def f():
4+
x0 = 1
5+
x1 = 1
6+
x2 = 1
7+
x3 = 1
8+
x4 = 1
9+
x5 = 1
10+
x6 = 1
11+
x7 = 1
12+
x8 = 1
13+
x9 = 1
14+
x10 = 1
15+
x11 = 1
16+
x12 = 1
17+
x13 = 1
18+
x14 = 1
19+
x15 = 1
20+
x16 = 1
21+
x17 = 1
22+
x18 = 1
23+
x19 = 1
24+
x20 = 1
25+
x21 = 1
26+
x22 = 1
27+
x23 = 1
28+
x24 = 1
29+
x25 = 1
30+
x26 = 1
31+
x27 = 1
32+
x28 = 1
33+
x29 = 1
34+
x30 = 1
35+
x31 = 1
36+
x32 = 1
37+
x33 = 1
38+
x34 = 1
39+
x35 = 1
40+
x36 = 1
41+
x37 = 1
42+
x38 = 1
43+
x39 = 1
44+
x40 = 1
45+
x41 = 1
46+
x42 = 1
47+
x43 = 1
48+
x44 = 1
49+
x45 = 1
50+
x46 = 1
51+
x47 = 1
52+
x48 = 1
53+
x49 = 1
54+
x50 = 1
55+
x51 = 1
56+
x52 = 1
57+
x53 = 1
58+
x54 = 1
59+
x55 = 1
60+
x56 = 1
61+
x57 = 1
62+
x58 = 1
63+
x59 = 1
64+
x60 = 1
65+
x61 = 1
66+
x62 = 1
67+
x63 = 1
68+
x64 = 1
69+
x65 = 1
70+
x66 = 1
71+
x67 = 1
72+
x68 = 1
73+
x69 = 1
74+
x70 = 1
75+
x71 = 1
76+
x72 = 1
77+
x73 = 1
78+
x74 = 1
79+
x75 = 1
80+
x76 = 1
81+
x77 = 1
82+
x78 = 1
83+
x79 = 1
84+
x80 = 1
85+
x81 = 1
86+
x82 = 1
87+
x83 = 1
88+
x84 = 1
89+
x85 = 1
90+
x86 = 1
91+
x87 = 1
92+
x88 = 1
93+
x89 = 1
94+
x90 = 1
95+
x91 = 1
96+
x92 = 1
97+
x93 = 1
98+
x94 = 1
99+
x95 = 1
100+
x96 = 1
101+
x97 = 1
102+
x98 = 1
103+
x99 = 1
104+
x100 = 1
105+
x101 = 1
106+
x102 = 1
107+
x103 = 1
108+
x104 = 1
109+
x105 = 1
110+
x106 = 1
111+
x107 = 1
112+
x108 = 1
113+
x109 = 1
114+
x110 = 1
115+
x111 = 1
116+
x112 = 1
117+
x113 = 1
118+
x114 = 1
119+
x115 = 1
120+
x116 = 1
121+
x117 = 1
122+
x118 = 1
123+
x119 = 1
124+
x120 = 1
125+
x121 = 1
126+
x122 = 1
127+
x123 = 1
128+
x124 = 1
129+
x125 = 1
130+
x126 = 1
131+
132+
def g():
133+
x = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,]

0 commit comments

Comments
 (0)