Mercurial > cpython-lru_cache-weakref
annotate Python/makeopcodetargets.py @ 51853:643d8d4fc8eb
http://bugs.python.org/issue4715
This patch by Antoine Pitrou optimizes the bytecode for conditional branches by
merging the following "POP_TOP" instruction into the conditional jump. For
example, the list comprehension "[x for x in l if not x]" produced the
following bytecode:
1 0 BUILD_LIST 0
3 LOAD_FAST 0 (.0)
>> 6 FOR_ITER 23 (to 32)
9 STORE_FAST 1 (x)
12 LOAD_FAST 1 (x)
15 JUMP_IF_TRUE 10 (to 28)
18 POP_TOP
19 LOAD_FAST 1 (x)
22 LIST_APPEND 2
25 JUMP_ABSOLUTE 6
>> 28 POP_TOP
29 JUMP_ABSOLUTE 6
>> 32 RETURN_VALUE
but after the patch it produces the following bytecode:
1 0 BUILD_LIST 0
3 LOAD_FAST 0 (.0)
>> 6 FOR_ITER 18 (to 27)
9 STORE_FAST 1 (x)
12 LOAD_FAST 1 (x)
15 POP_JUMP_IF_TRUE 6
18 LOAD_FAST 1 (x)
21 LIST_APPEND 2
24 JUMP_ABSOLUTE 6
>> 27 RETURN_VALUE
Notice that not only the code is shorter, but the conditional jump
(POP_JUMP_IF_TRUE) jumps right to the start of the loop instead of going through
the JUMP_ABSOLUTE at the end. "continue" statements are helped
similarly.
Furthermore, the old jump opcodes (JUMP_IF_FALSE, JUMP_IF_TRUE) have been
replaced by two new opcodes:
- JUMP_IF_TRUE_OR_POP, which jumps if true and pops otherwise
- JUMP_IF_FALSE_OR_POP, which jumps if false and pops otherwise
| author | Jeffrey Yasskin <jyasskin@gmail.com> |
|---|---|
| date | Wed, 25 Feb 2009 02:25:04 +0000 |
| parents | df090597afaa |
| children | f197407bdba2 fcfe324663a6 |
| rev | line source |
|---|---|
|
51031
d86e23318b41
Issue #4753: By enabling a configure option named '--with-computed-gotos'
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1 #! /usr/bin/env python |
|
d86e23318b41
Issue #4753: By enabling a configure option named '--with-computed-gotos'
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
2 """Generate C code for the jump table of the threaded code interpreter |
|
d86e23318b41
Issue #4753: By enabling a configure option named '--with-computed-gotos'
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
3 (for compilers supporting computed gotos or "labels-as-values", such as gcc). |
|
d86e23318b41
Issue #4753: By enabling a configure option named '--with-computed-gotos'
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
4 """ |
|
d86e23318b41
Issue #4753: By enabling a configure option named '--with-computed-gotos'
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
5 |
|
51202
df090597afaa
Make Python/makeopcodetargets runnable with Python 2.3. With any luck, this
Mark Dickinson <dickinsm@gmail.com>
parents:
51031
diff
changeset
|
6 # This code should stay compatible with Python 2.3, at least while |
|
df090597afaa
Make Python/makeopcodetargets runnable with Python 2.3. With any luck, this
Mark Dickinson <dickinsm@gmail.com>
parents:
51031
diff
changeset
|
7 # some of the buildbots have Python 2.3 as their system Python. |
|
df090597afaa
Make Python/makeopcodetargets runnable with Python 2.3. With any luck, this
Mark Dickinson <dickinsm@gmail.com>
parents:
51031
diff
changeset
|
8 |
|
51031
d86e23318b41
Issue #4753: By enabling a configure option named '--with-computed-gotos'
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
9 import imp |
|
d86e23318b41
Issue #4753: By enabling a configure option named '--with-computed-gotos'
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
10 import os |
|
d86e23318b41
Issue #4753: By enabling a configure option named '--with-computed-gotos'
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
11 |
|
d86e23318b41
Issue #4753: By enabling a configure option named '--with-computed-gotos'
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
12 |
|
d86e23318b41
Issue #4753: By enabling a configure option named '--with-computed-gotos'
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
13 def find_module(modname): |
|
d86e23318b41
Issue #4753: By enabling a configure option named '--with-computed-gotos'
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
14 """Finds and returns a module in the local dist/checkout. |
|
d86e23318b41
Issue #4753: By enabling a configure option named '--with-computed-gotos'
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
15 """ |
|
d86e23318b41
Issue #4753: By enabling a configure option named '--with-computed-gotos'
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
16 modpath = os.path.join( |
|
d86e23318b41
Issue #4753: By enabling a configure option named '--with-computed-gotos'
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
17 os.path.dirname(os.path.dirname(__file__)), "Lib") |
|
d86e23318b41
Issue #4753: By enabling a configure option named '--with-computed-gotos'
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
18 return imp.load_module(modname, *imp.find_module(modname, [modpath])) |
|
d86e23318b41
Issue #4753: By enabling a configure option named '--with-computed-gotos'
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
19 |
|
d86e23318b41
Issue #4753: By enabling a configure option named '--with-computed-gotos'
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
20 def write_contents(f): |
|
d86e23318b41
Issue #4753: By enabling a configure option named '--with-computed-gotos'
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
21 """Write C code contents to the target file object. |
|
d86e23318b41
Issue #4753: By enabling a configure option named '--with-computed-gotos'
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
22 """ |
|
d86e23318b41
Issue #4753: By enabling a configure option named '--with-computed-gotos'
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
23 opcode = find_module("opcode") |
|
d86e23318b41
Issue #4753: By enabling a configure option named '--with-computed-gotos'
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
24 targets = ['_unknown_opcode'] * 256 |
|
d86e23318b41
Issue #4753: By enabling a configure option named '--with-computed-gotos'
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
25 for opname, op in opcode.opmap.items(): |
|
d86e23318b41
Issue #4753: By enabling a configure option named '--with-computed-gotos'
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
26 if opname == "STOP_CODE": |
|
d86e23318b41
Issue #4753: By enabling a configure option named '--with-computed-gotos'
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
27 # XXX opcode not implemented |
|
d86e23318b41
Issue #4753: By enabling a configure option named '--with-computed-gotos'
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
28 continue |
|
d86e23318b41
Issue #4753: By enabling a configure option named '--with-computed-gotos'
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
29 targets[op] = "TARGET_%s" % opname |
|
d86e23318b41
Issue #4753: By enabling a configure option named '--with-computed-gotos'
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
30 f.write("static void *opcode_targets[256] = {\n") |
|
51202
df090597afaa
Make Python/makeopcodetargets runnable with Python 2.3. With any luck, this
Mark Dickinson <dickinsm@gmail.com>
parents:
51031
diff
changeset
|
31 f.write(",\n".join(["\t&&%s" % s for s in targets])) |
|
51031
d86e23318b41
Issue #4753: By enabling a configure option named '--with-computed-gotos'
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
32 f.write("\n};\n") |
|
d86e23318b41
Issue #4753: By enabling a configure option named '--with-computed-gotos'
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
33 |
|
d86e23318b41
Issue #4753: By enabling a configure option named '--with-computed-gotos'
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
34 |
|
d86e23318b41
Issue #4753: By enabling a configure option named '--with-computed-gotos'
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
35 if __name__ == "__main__": |
|
d86e23318b41
Issue #4753: By enabling a configure option named '--with-computed-gotos'
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
36 import sys |
|
d86e23318b41
Issue #4753: By enabling a configure option named '--with-computed-gotos'
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
37 assert len(sys.argv) < 3, "Too many arguments" |
|
d86e23318b41
Issue #4753: By enabling a configure option named '--with-computed-gotos'
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
38 if len(sys.argv) == 2: |
|
d86e23318b41
Issue #4753: By enabling a configure option named '--with-computed-gotos'
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
39 target = sys.argv[1] |
|
d86e23318b41
Issue #4753: By enabling a configure option named '--with-computed-gotos'
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
40 else: |
|
d86e23318b41
Issue #4753: By enabling a configure option named '--with-computed-gotos'
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
41 target = "Python/opcode_targets.h" |
|
d86e23318b41
Issue #4753: By enabling a configure option named '--with-computed-gotos'
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
42 f = open(target, "w") |
|
d86e23318b41
Issue #4753: By enabling a configure option named '--with-computed-gotos'
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
43 try: |
|
d86e23318b41
Issue #4753: By enabling a configure option named '--with-computed-gotos'
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
44 write_contents(f) |
|
d86e23318b41
Issue #4753: By enabling a configure option named '--with-computed-gotos'
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
45 finally: |
|
d86e23318b41
Issue #4753: By enabling a configure option named '--with-computed-gotos'
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
46 f.close() |
