forked from micropython/micropython
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmd_showbc_const.py
69 lines (56 loc) · 1.47 KB
/
cmd_showbc_const.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# cmdline: -v -v
# Test constant-related bytecode optimisations
# (constant folding, compile-time if/while evaluation, etc.)
from micropython import const
import sys
try:
sys.settrace
# if MICROPY_PY_SYS_SETTRACE is enabled, compile-time const optimizations
# are disabled so the bytecode output is very different
print("SKIP")
raise SystemExit
except AttributeError:
pass
_STR = const("foo")
_EMPTY_TUPLE = const(())
_TRUE = const(True)
_FALSE = const(False)
_SMALLINT = const(33)
_ZERO = const(0)
# Bytecode generated for these if/while statements should contain no JUMP_IF
# and no instances of string 'Eliminated'
if _STR or _EMPTY_TUPLE:
print("Kept")
if _STR and _EMPTY_TUPLE:
print("Eliminated")
if _TRUE:
print("Kept")
if _SMALLINT:
print("Kept")
if _ZERO and _SMALLINT:
print("Eliminated")
if _FALSE:
print("Eliminated")
while _SMALLINT:
print("Kept")
break
while _ZERO:
print("Eliminated")
while _FALSE:
print("Eliminated")
# These values are stored in variables, and therefore bytecode will contain JUMP_IF
a = _EMPTY_TUPLE or _STR
if a == _STR:
print("Kept")
b = _SMALLINT and _STR
if b == _STR:
print("Kept")
# The compiler is also unable to optimise these expressions, even though the arguments are const,
# so these also contain JUMP_IF
if (_EMPTY_TUPLE or _STR) == _STR:
print("Kept")
if (_EMPTY_TUPLE and _STR) == _STR:
print("Not Eliminated")
if (not _STR) == _FALSE:
print("Kept")
assert True