circuitpython/tests/cmdline/cmd_showbc.py.exp
Damien George 538c3c0a55 py: Change jump opcodes to emit 1-byte jump offset when possible.
This commit introduces changes:

- All jump opcodes are changed to have variable length arguments, of either
  1 or 2 bytes (previously they were fixed at 2 bytes).  In most cases only
  1 byte is needed to encode the short jump offset, saving bytecode size.

- The bytecode emitter now selects 1 byte jump arguments when the jump
  offset is guaranteed to fit in 1 byte.  This is achieved by checking if
  the code size changed during the last pass and, if it did (if it shrank),
  then requesting that the compiler make another pass to get the correct
  offsets of the now-smaller code.  This can continue multiple times until
  the code stabilises.  The code can only ever shrink so this iteration is
  guaranteed to complete.  In most cases no extra passes are needed, the
  original 4 passes are enough to get it right by the 4th pass (because the
  2nd pass computes roughly the correct labels and the 3rd pass computes
  the correct size for the jump argument).

This change to the jump opcode encoding reduces .mpy files and RAM usage
(when bytecode is in RAM) by about 2% on average.

The performance of the VM is not impacted, at least within measurment of
the performance benchmark suite.

Code size is reduced for builds that include a decent amount of frozen
bytecode.  ARM Cortex-M builds without any frozen code increase by about
350 bytes.

Signed-off-by: Damien George <damien@micropython.org>
2022-03-28 15:41:38 +11:00

642 lines
13 KiB
Plaintext

File cmdline/cmd_showbc.py, code block '<module>' (descriptor: \.\+, bytecode @\.\+ 63 bytes)
Raw bytecode (code_info_size=18, bytecode_size=45):
10 20 01 60 20 84 7d 64 60 88 07 64 60 69 20 62
64 20 32 00 16 02 32 01 16 02 81 2a 01 53 33 02
16 02 32 03 16 02 54 32 04 10 03 34 02 16 03 19
03 32 05 16 02 80 10 04 2a 01 1b 05 69 51 63
arg names:
(N_STATE 3)
(N_EXC_STACK 0)
bc=0 line=1
bc=0 line=4
bc=0 line=5
bc=4 line=130
bc=8 line=133
bc=8 line=136
bc=16 line=143
bc=20 line=146
bc=20 line=149
bc=29 line=152
bc=29 line=153
bc=31 line=156
bc=35 line=159
bc=35 line=160
00 MAKE_FUNCTION \.\+
02 STORE_NAME f
04 MAKE_FUNCTION \.\+
06 STORE_NAME f
08 LOAD_CONST_SMALL_INT 1
09 BUILD_TUPLE 1
11 LOAD_NULL
12 MAKE_FUNCTION_DEFARGS \.\+
14 STORE_NAME f
16 MAKE_FUNCTION \.\+
18 STORE_NAME f
20 LOAD_BUILD_CLASS
21 MAKE_FUNCTION \.\+
23 LOAD_CONST_STRING 'Class'
25 CALL_FUNCTION n=2 nkw=0
27 STORE_NAME Class
29 DELETE_NAME Class
31 MAKE_FUNCTION \.\+
33 STORE_NAME f
35 LOAD_CONST_SMALL_INT 0
36 LOAD_CONST_STRING '*'
38 BUILD_TUPLE 1
40 IMPORT_NAME 'sys'
42 IMPORT_STAR
43 LOAD_CONST_NONE
44 RETURN_VALUE
File cmdline/cmd_showbc.py, code block 'f' (descriptor: \.\+, bytecode @\.\+ 45\[68\] bytes)
Raw bytecode (code_info_size=8\[46\], bytecode_size=372):
a8 12 9\[bf\] 03 02 60 60 26 22 24 64 22 26 25 25 24
26 23 63 22 22 25 23 23 2f 6c 25 65 25 25 69 68
26 65 27 6a 62 20 23 62 2a 29 69 24 25 28 67 26
########
\.\+81 63
arg names:
(N_STATE 22)
(N_EXC_STACK 2)
(INIT_CELL 14)
(INIT_CELL 15)
(INIT_CELL 16)
bc=0 line=1
bc=0 line=4
bc=0 line=7
bc=6 line=8
bc=8 line=9
bc=12 line=10
bc=16 line=13
bc=18 line=14
bc=24 line=15
bc=29 line=16
bc=34 line=17
bc=38 line=18
bc=44 line=19
bc=47 line=20
bc=50 line=23
bc=52 line=24
bc=54 line=25
bc=59 line=26
bc=62 line=27
bc=65 line=28
bc=80 line=29
bc=92 line=32
bc=97 line=33
bc=102 line=36
bc=107 line=37
bc=112 line=38
bc=121 line=41
bc=129 line=44
bc=135 line=45
bc=140 line=48
bc=147 line=49
bc=157 line=52
bc=159 line=55
bc=159 line=56
bc=162 line=57
bc=164 line=60
bc=174 line=61
bc=183 line=62
bc=192 line=65
bc=196 line=66
bc=201 line=67
bc=209 line=68
bc=216 line=71
bc=222 line=72
bc=229 line=73
bc=239 line=74
bc=247 line=77
bc=250 line=78
bc=255 line=80
bc=258 line=81
bc=260 line=82
bc=266 line=83
bc=268 line=84
bc=274 line=85
bc=279 line=88
bc=285 line=89
bc=289 line=92
bc=293 line=93
bc=295 line=94
########
bc=303 line=96
bc=310 line=98
bc=313 line=99
bc=315 line=100
bc=317 line=101
########
bc=323 line=103
bc=329 line=106
bc=333 line=107
bc=339 line=110
bc=342 line=111
bc=348 line=114
bc=348 line=117
bc=353 line=118
bc=365 line=121
bc=365 line=122
bc=366 line=123
bc=368 line=126
bc=370 line=127
00 LOAD_CONST_NONE
01 LOAD_CONST_FALSE
02 BINARY_OP 27 __add__
03 LOAD_CONST_TRUE
04 BINARY_OP 27 __add__
05 STORE_FAST 0
06 LOAD_CONST_SMALL_INT 0
07 STORE_FAST 0
08 LOAD_CONST_SMALL_INT 1000
11 STORE_FAST 0
12 LOAD_CONST_SMALL_INT -1000
15 STORE_FAST 0
16 LOAD_CONST_SMALL_INT 1
17 STORE_FAST 0
18 LOAD_CONST_SMALL_INT 1
19 LOAD_CONST_SMALL_INT 2
20 BUILD_TUPLE 2
22 STORE_DEREF 14
24 LOAD_CONST_SMALL_INT 1
25 LOAD_CONST_SMALL_INT 2
26 BUILD_LIST 2
28 STORE_FAST 1
29 LOAD_CONST_SMALL_INT 1
30 LOAD_CONST_SMALL_INT 2
31 BUILD_SET 2
33 STORE_FAST 2
34 BUILD_MAP 0
36 STORE_DEREF 15
38 BUILD_MAP 1
40 LOAD_CONST_SMALL_INT 2
41 LOAD_CONST_SMALL_INT 1
42 STORE_MAP
43 STORE_FAST 3
44 LOAD_CONST_STRING 'a'
46 STORE_FAST 4
47 LOAD_CONST_OBJ \.\+=b'a'
49 STORE_FAST 5
50 LOAD_CONST_SMALL_INT 1
51 STORE_FAST 6
52 LOAD_CONST_SMALL_INT 2
53 STORE_FAST 7
54 LOAD_FAST 0
55 LOAD_DEREF 14
57 BINARY_OP 27 __add__
58 STORE_FAST 8
59 LOAD_FAST 0
60 UNARY_OP 1 __neg__
61 STORE_FAST 9
62 LOAD_FAST 0
63 UNARY_OP 3
64 STORE_FAST 10
65 LOAD_FAST 0
66 LOAD_DEREF 14
68 DUP_TOP
69 ROT_THREE
70 BINARY_OP 2 __eq__
71 JUMP_IF_FALSE_OR_POP 77
73 LOAD_FAST 1
74 BINARY_OP 2 __eq__
75 JUMP 79
77 ROT_TWO
78 POP_TOP
79 STORE_FAST 10
80 LOAD_FAST 0
81 LOAD_DEREF 14
83 BINARY_OP 2 __eq__
84 JUMP_IF_FALSE_OR_POP 90
86 LOAD_DEREF 14
88 LOAD_FAST 1
89 BINARY_OP 2 __eq__
90 UNARY_OP 3
91 STORE_FAST 10
92 LOAD_DEREF 14
94 LOAD_ATTR c
96 STORE_FAST 11
97 LOAD_FAST 11
98 LOAD_DEREF 14
100 STORE_ATTR c
102 LOAD_DEREF 14
104 LOAD_CONST_SMALL_INT 0
105 LOAD_SUBSCR
106 STORE_FAST 12
107 LOAD_FAST 12
108 LOAD_DEREF 14
110 LOAD_CONST_SMALL_INT 0
111 STORE_SUBSCR
112 LOAD_DEREF 14
114 LOAD_CONST_SMALL_INT 0
115 DUP_TOP_TWO
116 LOAD_SUBSCR
117 LOAD_FAST 12
118 BINARY_OP 14 __iadd__
119 ROT_THREE
120 STORE_SUBSCR
121 LOAD_DEREF 14
123 LOAD_CONST_NONE
124 LOAD_CONST_NONE
125 BUILD_SLICE 2
127 LOAD_SUBSCR
128 STORE_FAST 0
129 LOAD_FAST 1
130 UNPACK_SEQUENCE 2
132 STORE_FAST 0
133 STORE_DEREF 14
135 LOAD_FAST 0
136 UNPACK_EX 1
138 STORE_FAST 0
139 STORE_FAST 0
140 LOAD_DEREF 14
142 LOAD_FAST 0
143 ROT_TWO
144 STORE_FAST 0
145 STORE_DEREF 14
147 LOAD_FAST 1
148 LOAD_DEREF 14
150 LOAD_FAST 0
151 ROT_THREE
152 ROT_TWO
153 STORE_FAST 0
154 STORE_DEREF 14
156 STORE_FAST 1
157 DELETE_FAST 0
159 LOAD_FAST 0
160 STORE_GLOBAL gl
162 DELETE_GLOBAL gl
164 LOAD_FAST 14
165 LOAD_FAST 15
166 MAKE_CLOSURE \.\+ 2
169 LOAD_FAST 2
170 GET_ITER
171 CALL_FUNCTION n=1 nkw=0
173 STORE_FAST 0
174 LOAD_FAST 14
175 LOAD_FAST 15
176 MAKE_CLOSURE \.\+ 2
179 LOAD_FAST 2
180 CALL_FUNCTION n=1 nkw=0
182 STORE_FAST 0
183 LOAD_FAST 14
184 LOAD_FAST 15
185 MAKE_CLOSURE \.\+ 2
188 LOAD_FAST 2
189 CALL_FUNCTION n=1 nkw=0
191 STORE_FAST 0
192 LOAD_FAST 0
193 CALL_FUNCTION n=0 nkw=0
195 POP_TOP
196 LOAD_FAST 0
197 LOAD_CONST_SMALL_INT 1
198 CALL_FUNCTION n=1 nkw=0
200 POP_TOP
201 LOAD_FAST 0
202 LOAD_CONST_STRING 'b'
204 LOAD_CONST_SMALL_INT 1
205 CALL_FUNCTION n=0 nkw=1
208 POP_TOP
209 LOAD_FAST 0
210 LOAD_DEREF 14
212 LOAD_NULL
213 CALL_FUNCTION_VAR_KW n=0 nkw=0
215 POP_TOP
216 LOAD_FAST 0
217 LOAD_METHOD b
219 CALL_METHOD n=0 nkw=0
221 POP_TOP
222 LOAD_FAST 0
223 LOAD_METHOD b
225 LOAD_CONST_SMALL_INT 1
226 CALL_METHOD n=1 nkw=0
228 POP_TOP
229 LOAD_FAST 0
230 LOAD_METHOD b
232 LOAD_CONST_STRING 'c'
234 LOAD_CONST_SMALL_INT 1
235 CALL_METHOD n=0 nkw=1
238 POP_TOP
239 LOAD_FAST 0
240 LOAD_METHOD b
242 LOAD_FAST 1
243 LOAD_NULL
244 CALL_METHOD_VAR_KW n=0 nkw=0
246 POP_TOP
247 LOAD_FAST 0
248 POP_JUMP_IF_FALSE 255
250 LOAD_DEREF 16
252 POP_TOP
253 JUMP 258
255 LOAD_GLOBAL y
257 POP_TOP
258 JUMP 263
260 LOAD_DEREF 14
262 POP_TOP
263 LOAD_FAST 0
264 POP_JUMP_IF_TRUE 260
266 JUMP 271
268 LOAD_DEREF 14
270 POP_TOP
271 LOAD_FAST 0
272 POP_JUMP_IF_FALSE 268
274 LOAD_FAST 0
275 JUMP_IF_TRUE_OR_POP 278
277 LOAD_FAST 0
278 STORE_FAST 0
279 LOAD_DEREF 14
281 GET_ITER_STACK
282 FOR_ITER 289
284 STORE_FAST 0
285 LOAD_FAST 1
286 POP_TOP
287 JUMP 282
289 SETUP_FINALLY 310
291 SETUP_EXCEPT 302
293 JUMP 297
295 JUMP 300
297 LOAD_FAST 0
298 POP_JUMP_IF_TRUE 295
300 POP_EXCEPT_JUMP 309
302 POP_TOP
303 LOAD_DEREF 14
305 POP_TOP
306 POP_EXCEPT_JUMP 309
308 END_FINALLY
309 LOAD_CONST_NONE
310 LOAD_FAST 1
311 POP_TOP
312 END_FINALLY
313 JUMP 326
315 SETUP_EXCEPT 322
317 UNWIND_JUMP 329 1
320 POP_EXCEPT_JUMP 326
322 POP_TOP
323 POP_EXCEPT_JUMP 326
325 END_FINALLY
326 LOAD_FAST 0
327 POP_JUMP_IF_TRUE 315
329 LOAD_FAST 0
330 SETUP_WITH 337
332 POP_TOP
333 LOAD_DEREF 14
335 POP_TOP
336 LOAD_CONST_NONE
337 WITH_CLEANUP
338 END_FINALLY
339 LOAD_CONST_SMALL_INT 1
340 STORE_DEREF 16
342 LOAD_FAST_N 16
344 MAKE_CLOSURE \.\+ 1
347 STORE_FAST 13
348 LOAD_CONST_SMALL_INT 0
349 LOAD_CONST_NONE
350 IMPORT_NAME 'a'
352 STORE_FAST 0
353 LOAD_CONST_SMALL_INT 0
354 LOAD_CONST_STRING 'b'
356 BUILD_TUPLE 1
358 IMPORT_NAME 'a'
360 IMPORT_FROM 'b'
362 STORE_DEREF 14
364 POP_TOP
365 RAISE_LAST
366 LOAD_CONST_SMALL_INT 1
367 RAISE_OBJ
368 LOAD_CONST_NONE
369 RETURN_VALUE
370 LOAD_CONST_SMALL_INT 1
371 RETURN_VALUE
File cmdline/cmd_showbc.py, code block 'f' (descriptor: \.\+, bytecode @\.\+ 59 bytes)
Raw bytecode (code_info_size=8, bytecode_size=51):
a8 10 0a 02 80 82 34 38 81 57 c0 57 c1 57 c2 57
c3 57 c4 57 c5 57 c6 57 c7 57 c8 c9 82 57 ca 57
cb 57 cc 57 cd 57 ce 57 cf 57 26 10 57 26 11 57
26 12 26 13 b9 24 13 f2 59 51 63
arg names:
(N_STATE 22)
(N_EXC_STACK 0)
bc=0 line=1
bc=0 line=131
bc=20 line=132
bc=44 line=133
00 LOAD_CONST_SMALL_INT 1
01 DUP_TOP
02 STORE_FAST 0
03 DUP_TOP
04 STORE_FAST 1
05 DUP_TOP
06 STORE_FAST 2
07 DUP_TOP
08 STORE_FAST 3
09 DUP_TOP
10 STORE_FAST 4
11 DUP_TOP
12 STORE_FAST 5
13 DUP_TOP
14 STORE_FAST 6
15 DUP_TOP
16 STORE_FAST 7
17 DUP_TOP
18 STORE_FAST 8
19 STORE_FAST 9
20 LOAD_CONST_SMALL_INT 2
21 DUP_TOP
22 STORE_FAST 10
23 DUP_TOP
24 STORE_FAST 11
25 DUP_TOP
26 STORE_FAST 12
27 DUP_TOP
28 STORE_FAST 13
29 DUP_TOP
30 STORE_FAST 14
31 DUP_TOP
32 STORE_FAST 15
33 DUP_TOP
34 STORE_FAST_N 16
36 DUP_TOP
37 STORE_FAST_N 17
39 DUP_TOP
40 STORE_FAST_N 18
42 STORE_FAST_N 19
44 LOAD_FAST 9
45 LOAD_FAST_N 19
47 BINARY_OP 27 __add__
48 POP_TOP
49 LOAD_CONST_NONE
50 RETURN_VALUE
File cmdline/cmd_showbc.py, code block 'f' (descriptor: \.\+, bytecode @\.\+ 20 bytes)
Raw bytecode (code_info_size=9, bytecode_size=11):
a1 01 0b 02 06 80 88 40 00 82 2a 01 53 b0 21 00
01 c1 51 63
arg names: a
(N_STATE 5)
(N_EXC_STACK 0)
(INIT_CELL 0)
bc=0 line=1
bc=0 line=137
bc=0 line=139
00 LOAD_CONST_SMALL_INT 2
01 BUILD_TUPLE 1
03 LOAD_NULL
04 LOAD_FAST 0
05 MAKE_CLOSURE_DEFARGS \.\+ 1
08 STORE_FAST 1
09 LOAD_CONST_NONE
10 RETURN_VALUE
File cmdline/cmd_showbc.py, code block 'f' (descriptor: \.\+, bytecode @\.\+ 21 bytes)
Raw bytecode (code_info_size=8, bytecode_size=13):
88 40 0a 02 80 8f 23 23 51 67 59 81 67 59 81 5e
51 68 59 51 63
arg names:
(N_STATE 2)
(N_EXC_STACK 0)
bc=0 line=1
bc=0 line=144
bc=3 line=145
bc=6 line=146
00 LOAD_CONST_NONE
01 YIELD_VALUE
02 POP_TOP
03 LOAD_CONST_SMALL_INT 1
04 YIELD_VALUE
05 POP_TOP
06 LOAD_CONST_SMALL_INT 1
07 GET_ITER
08 LOAD_CONST_NONE
09 YIELD_FROM
10 POP_TOP
11 LOAD_CONST_NONE
12 RETURN_VALUE
File cmdline/cmd_showbc.py, code block 'Class' (descriptor: \.\+, bytecode @\.\+ 1\[56\] bytes)
Raw bytecode (code_info_size=\[56\], bytecode_size=10):
00 \.\+ 11 0c 16 0d 10 03 16 0e 51 63
arg names:
(N_STATE 1)
(N_EXC_STACK 0)
bc=0 line=1
########
bc=8 line=150
00 LOAD_NAME __name__
02 STORE_NAME __module__
04 LOAD_CONST_STRING 'Class'
06 STORE_NAME __qualname__
08 LOAD_CONST_NONE
09 RETURN_VALUE
File cmdline/cmd_showbc.py, code block 'f' (descriptor: \.\+, bytecode @\.\+ 18 bytes)
Raw bytecode (code_info_size=6, bytecode_size=12):
19 08 02 0f 80 9c 12 10 12 11 b0 15 02 36 00 59
51 63
arg names: self
(N_STATE 4)
(N_EXC_STACK 0)
bc=0 line=1
bc=0 line=157
00 LOAD_GLOBAL super
02 LOAD_GLOBAL __class__
04 LOAD_FAST 0
05 LOAD_SUPER_METHOD f
07 CALL_METHOD n=0 nkw=0
09 POP_TOP
10 LOAD_CONST_NONE
11 RETURN_VALUE
File cmdline/cmd_showbc.py, code block '<genexpr>' (descriptor: \.\+, bytecode @\.\+ 28 bytes)
Raw bytecode (code_info_size=9, bytecode_size=19):
c3 40 0c 12 04 04 04 80 3b 53 b2 53 53 4b 0b c3
25 01 44 39 25 00 67 59 42 33 51 63
arg names: * * *
(N_STATE 9)
(N_EXC_STACK 0)
bc=0 line=1
bc=0 line=60
00 LOAD_NULL
01 LOAD_FAST 2
02 LOAD_NULL
03 LOAD_NULL
04 FOR_ITER 17
06 STORE_FAST 3
07 LOAD_DEREF 1
09 POP_JUMP_IF_FALSE 4
11 LOAD_DEREF 0
13 YIELD_VALUE
14 POP_TOP
15 JUMP 4
17 LOAD_CONST_NONE
18 RETURN_VALUE
File cmdline/cmd_showbc.py, code block '<listcomp>' (descriptor: \.\+, bytecode @\.\+ 26 bytes)
Raw bytecode (code_info_size=8, bytecode_size=18):
4b 0c 14 04 04 04 80 3c 2b 00 b2 5f 4b 0b c3 25
01 44 39 25 00 2f 14 42 33 63
arg names: * * *
(N_STATE 10)
(N_EXC_STACK 0)
bc=0 line=1
bc=0 line=61
00 BUILD_LIST 0
02 LOAD_FAST 2
03 GET_ITER_STACK
04 FOR_ITER 17
06 STORE_FAST 3
07 LOAD_DEREF 1
09 POP_JUMP_IF_FALSE 4
11 LOAD_DEREF 0
13 STORE_COMP 20
15 JUMP 4
17 RETURN_VALUE
File cmdline/cmd_showbc.py, code block '<dictcomp>' (descriptor: \.\+, bytecode @\.\+ 28 bytes)
Raw bytecode (code_info_size=8, bytecode_size=20):
53 0c 15 04 04 04 80 3d 2c 00 b2 5f 4b 0d c3 25
01 44 39 25 00 25 00 2f 19 42 31 63
arg names: * * *
(N_STATE 11)
(N_EXC_STACK 0)
bc=0 line=1
bc=0 line=62
00 BUILD_MAP 0
02 LOAD_FAST 2
03 GET_ITER_STACK
04 FOR_ITER 19
06 STORE_FAST 3
07 LOAD_DEREF 1
09 POP_JUMP_IF_FALSE 4
11 LOAD_DEREF 0
13 LOAD_DEREF 0
15 STORE_COMP 25
17 JUMP 4
19 RETURN_VALUE
File cmdline/cmd_showbc.py, code block 'closure' (descriptor: \.\+, bytecode @\.\+ 20 bytes)
Raw bytecode (code_info_size=8, bytecode_size=12):
19 0c 16 04 80 6f 25 23 25 00 81 f2 c1 81 27 00
29 00 51 63
arg names: *
(N_STATE 4)
(N_EXC_STACK 0)
bc=0 line=1
bc=0 line=112
bc=5 line=113
bc=8 line=114
00 LOAD_DEREF 0
02 LOAD_CONST_SMALL_INT 1
03 BINARY_OP 27 __add__
04 STORE_FAST 1
05 LOAD_CONST_SMALL_INT 1
06 STORE_DEREF 0
08 DELETE_DEREF 0
10 LOAD_CONST_NONE
11 RETURN_VALUE
File cmdline/cmd_showbc.py, code block 'f' (descriptor: \.\+, bytecode @\.\+ 13 bytes)
Raw bytecode (code_info_size=8, bytecode_size=5):
9a 01 0a 02 04 09 80 8b b1 25 00 f2 63
arg names: * b
(N_STATE 4)
(N_EXC_STACK 0)
bc=0 line=1
bc=0 line=140
00 LOAD_FAST 1
01 LOAD_DEREF 0
03 BINARY_OP 27 __add__
04 RETURN_VALUE
mem: total=\\d\+, current=\\d\+, peak=\\d\+
stack: \\d\+ out of \\d\+
GC: total: \\d\+, used: \\d\+, free: \\d\+
No. of 1-blocks: \\d\+, 2-blocks: \\d\+, max blk sz: \\d\+, max free sz: \\d\+