| 1 |
from pypy.interpreter.astcompiler.misc import mangle |
| 2 |
from pypy.interpreter.astcompiler.assemble import Instruction, ops, Block |
| 3 |
from pypy.interpreter.location import _encode_lnotab_pair |
| 4 |
from pypy.interpreter.astcompiler.codegen import compute_reordering, rot_n |
| 5 |
|
| 6 |
|
| 7 |
def test_mangle(): |
| 8 |
assert mangle("foo", "Bar") == "foo" |
| 9 |
assert mangle("__foo__", "Bar") == "__foo__" |
| 10 |
assert mangle("foo.baz", "Bar") == "foo.baz" |
| 11 |
assert mangle("__", "Bar") == "__" |
| 12 |
assert mangle("___", "Bar") == "___" |
| 13 |
assert mangle("____", "Bar") == "____" |
| 14 |
assert mangle("__foo", "Bar") == "_Bar__foo" |
| 15 |
assert mangle("__foo", "_Bar") == "_Bar__foo" |
| 16 |
assert mangle("__foo", "__Bar") == "_Bar__foo" |
| 17 |
assert mangle("__foo", "___") == "__foo" |
| 18 |
assert mangle("___foo", "__Bar") == "_Bar___foo" |
| 19 |
|
| 20 |
def test_instruction_size(): |
| 21 |
assert Instruction(ops.POP_TOP).size() == 2 |
| 22 |
assert Instruction(ops.LOAD_FAST, 23).size() == 2 |
| 23 |
assert Instruction(ops.LOAD_FAST, 0xfff0).size() == 4 |
| 24 |
assert Instruction(ops.LOAD_FAST, 0x10000).size() == 6 |
| 25 |
assert Instruction(ops.LOAD_FAST, 0x1000000).size() == 8 |
| 26 |
|
| 27 |
def test_instruction_encode(): |
| 28 |
c = [] |
| 29 |
Instruction(ops.POP_TOP).encode(c) |
| 30 |
assert c == [chr(ops.POP_TOP), '\x00'] |
| 31 |
|
| 32 |
c = [] |
| 33 |
Instruction(ops.LOAD_FAST, 1).encode(c) |
| 34 |
assert c == [chr(ops.LOAD_FAST), '\x01'] |
| 35 |
|
| 36 |
c = [] |
| 37 |
Instruction(ops.LOAD_FAST, 0x201).encode(c) |
| 38 |
assert c == [chr(ops.EXTENDED_ARG), '\x02', chr(ops.LOAD_FAST), '\x01'] |
| 39 |
|
| 40 |
c = [] |
| 41 |
Instruction(ops.LOAD_FAST, 0x30201).encode(c) |
| 42 |
assert c == [chr(ops.EXTENDED_ARG), '\x03', chr(ops.EXTENDED_ARG), '\x02', chr(ops.LOAD_FAST), '\x01'] |
| 43 |
|
| 44 |
c = [] |
| 45 |
Instruction(ops.LOAD_FAST, 0x5030201).encode(c) |
| 46 |
assert c == [chr(ops.EXTENDED_ARG), '\x05', chr(ops.EXTENDED_ARG), '\x03', chr(ops.EXTENDED_ARG), '\x02', chr(ops.LOAD_FAST), '\x01'] |
| 47 |
|
| 48 |
def test_encode_lnotab_pair(): |
| 49 |
l = [] |
| 50 |
_encode_lnotab_pair(0, 1, l) |
| 51 |
assert l == ["\x00", "\x01"] |
| 52 |
|
| 53 |
l = [] |
| 54 |
_encode_lnotab_pair(4, 1, l) |
| 55 |
assert l == ["\x04", "\x01"] |
| 56 |
|
| 57 |
l = [] |
| 58 |
_encode_lnotab_pair(4, -1, l) |
| 59 |
assert l == ["\x04", "\xff"] |
| 60 |
|
| 61 |
l = [] |
| 62 |
_encode_lnotab_pair(4, 127, l) |
| 63 |
assert l == ["\x04", "\x7f"] |
| 64 |
|
| 65 |
l = [] |
| 66 |
_encode_lnotab_pair(4, 128, l) |
| 67 |
assert l == list("\x04\x7f\x00\x01") |
| 68 |
|
| 69 |
l = [] |
| 70 |
_encode_lnotab_pair(4, -1000, l) |
| 71 |
assert l == list("\x04\x80\x00\x80\x00\x80\x00\x80\x00\x80\x00\x80\x00\x80\x00\x98") |
| 72 |
|
| 73 |
def test_compute_reordering(): |
| 74 |
import itertools |
| 75 |
|
| 76 |
for size in range(2, 9): |
| 77 |
target = range(size) |
| 78 |
for perm in itertools.permutations(range(size)): |
| 79 |
if perm[0] == len(perm) - 1: |
| 80 |
continue |
| 81 |
l = list(perm) |
| 82 |
rots = compute_reordering(l) |
| 83 |
l = list(perm) |
| 84 |
for rot in rots: |
| 85 |
rot_n(l, rot) |
| 86 |
assert l == list(reversed(target)) |
| 87 |
|
| 88 |
def jumpinstr(block, opcode, target): |
| 89 |
jump = Instruction(opcode) |
| 90 |
jump.jump = target |
| 91 |
block.instructions.append(jump) |
| 92 |
return jump |
| 93 |
|
| 94 |
def test_jump_thread_remove_jump_to_empty(): |
| 95 |
b = Block() |
| 96 |
b2 = Block() |
| 97 |
b3 = Block() |
| 98 |
b4 = Block() |
| 99 |
b2.next_block = b3 |
| 100 |
b3.next_block = b4 |
| 101 |
|
| 102 |
jump = jumpinstr(b, ops.JUMP_FORWARD, b2) |
| 103 |
|
| 104 |
b4.instructions.append(Instruction(ops.NOP)) |
| 105 |
|
| 106 |
b.jump_thread() |
| 107 |
assert jump.jump is b4 |
| 108 |
|
| 109 |
def test_jump_thread_jump_to_jump_forward(): |
| 110 |
for opcode in (ops.JUMP_FORWARD, ops.JUMP_ABSOLUTE): |
| 111 |
b = Block() |
| 112 |
b2 = Block() |
| 113 |
b3 = Block() |
| 114 |
|
| 115 |
jump1 = jumpinstr(b, opcode, b2) |
| 116 |
jump2 = jumpinstr(b2, ops.JUMP_FORWARD, b3) |
| 117 |
b3.instructions.append(Instruction(ops.NOP)) |
| 118 |
|
| 119 |
b.jump_thread() |
| 120 |
assert jump1.jump is b3 |
| 121 |
|
| 122 |
def test_dont_jump_thread_on_lineno_differences(): |
| 123 |
for opcode in (ops.JUMP_FORWARD, ops.JUMP_ABSOLUTE): |
| 124 |
b = Block() |
| 125 |
b2 = Block() |
| 126 |
b3 = Block() |
| 127 |
|
| 128 |
jump1 = jumpinstr(b, opcode, b2) |
| 129 |
jump1.position_info = (1, -1, -1, -1) |
| 130 |
jump2 = jumpinstr(b2, ops.RETURN_VALUE, b3) |
| 131 |
jump2.position_info = (2, -1, -1, -1) |
| 132 |
b3.instructions.append(Instruction(ops.NOP)) |
| 133 |
|
| 134 |
b.jump_thread() |
| 135 |
assert jump1.jump is b2 |
| 136 |
|
| 137 |
def test_uncond_jump_despite_lineno_differences(): |
| 138 |
for opcode in (ops.JUMP_FORWARD, ops.JUMP_ABSOLUTE): |
| 139 |
b = Block() |
| 140 |
b2 = Block() |
| 141 |
b3 = Block() |
| 142 |
|
| 143 |
jump1 = jumpinstr(b, opcode, b2) |
| 144 |
jump1.position_info = (1, -1, -1, -1) |
| 145 |
jump2 = jumpinstr(b2, ops.JUMP_FORWARD, b3) |
| 146 |
jump2.position_info = (2, -1, -1, -1) |
| 147 |
instr3 = Instruction(ops.NOP) |
| 148 |
instr3.position_info = (2, -1, -1, -1) |
| 149 |
b3.instructions.append(instr3) |
| 150 |
|
| 151 |
b.jump_thread() |
| 152 |
assert jump1.jump is b3 |
| 153 |
|
| 154 |
def test_jump_thread_jump_to_jump_absolute(): |
| 155 |
for opcode in (ops.JUMP_FORWARD, ops.JUMP_ABSOLUTE): |
| 156 |
b = Block() |
| 157 |
b2 = Block() |
| 158 |
b3 = Block() |
| 159 |
|
| 160 |
jump1 = jumpinstr(b, opcode, b2) |
| 161 |
jump2 = jumpinstr(b2, ops.JUMP_ABSOLUTE, b3) |
| 162 |
b3.instructions.append(Instruction(ops.NOP)) |
| 163 |
|
| 164 |
b.jump_thread() |
| 165 |
assert jump1.jump is b3 |
| 166 |
assert jump1.opcode == ops.JUMP_ABSOLUTE |
| 167 |
|
| 168 |
def test_jump_thread_jump_to_return(): |
| 169 |
for opcode in (ops.JUMP_FORWARD, ops.JUMP_ABSOLUTE): |
| 170 |
b = Block() |
| 171 |
b2 = Block() |
| 172 |
|
| 173 |
jump1 = jumpinstr(b, opcode, b2) |
| 174 |
jump2 = Instruction(ops.RETURN_VALUE) |
| 175 |
b2.instructions.append(jump2) |
| 176 |
|
| 177 |
b.jump_thread() |
| 178 |
assert jump1.jump is None |
| 179 |
assert jump1.opcode == ops.RETURN_VALUE |
| 180 |
|
| 181 |
def test_jump_thread_conditional_jump(): |
| 182 |
for opcode1 in (ops.POP_JUMP_IF_FALSE, ops.POP_JUMP_IF_TRUE, |
| 183 |
ops.JUMP_IF_FALSE_OR_POP, ops.JUMP_IF_TRUE_OR_POP): |
| 184 |
for opcode2 in (ops.JUMP_FORWARD, ops.JUMP_ABSOLUTE): |
| 185 |
b = Block() |
| 186 |
b2 = Block() |
| 187 |
b3 = Block() |
| 188 |
|
| 189 |
jump1 = jumpinstr(b, opcode1, b2) |
| 190 |
jump2 = jumpinstr(b2, opcode2, b3) |
| 191 |
b3.instructions.append(Instruction(ops.NOP)) |
| 192 |
|
| 193 |
b.jump_thread() |
| 194 |
assert jump1.jump is b3 |
| 195 |
assert jump1.opcode == opcode1 |
| 196 |
|
| 197 |
def test_jump_thread_jump_to_jump_to_jump(): |
| 198 |
for opcode1 in (ops.POP_JUMP_IF_FALSE, ops.POP_JUMP_IF_TRUE, |
| 199 |
ops.JUMP_IF_FALSE_OR_POP, ops.JUMP_IF_TRUE_OR_POP, |
| 200 |
ops.JUMP_FORWARD, ops.JUMP_ABSOLUTE): |
| 201 |
b = Block() |
| 202 |
b2 = Block() |
| 203 |
b3 = Block() |
| 204 |
b4 = Block() |
| 205 |
|
| 206 |
jump1 = jumpinstr(b, opcode1, b2) |
| 207 |
jump2 = jumpinstr(b2, ops.JUMP_FORWARD, b3) |
| 208 |
jump3 = jumpinstr(b3, ops.JUMP_FORWARD, b4) |
| 209 |
b4.instructions.append(Instruction(ops.NOP)) |
| 210 |
b.jump_thread() |
| 211 |
assert jump1.jump is b4 |
| 212 |
|
| 213 |
def test_jump_thread_fallthrough(): |
| 214 |
b = Block() |
| 215 |
b.next_block = b2 = Block() |
| 216 |
b2.next_block = b3 = Block() |
| 217 |
|
| 218 |
b.instructions.append(Instruction(ops.NOP)) |
| 219 |
b3.instructions.append(Instruction(ops.NOP)) |
| 220 |
b.jump_thread() |
| 221 |
assert b.next_block is b3 |
| 222 |
|
| 223 |
|
| 224 |
def test_block_exits_function(): |
| 225 |
for opcode in (ops.RETURN_VALUE, ops.RAISE_VARARGS, ops.RERAISE): |
| 226 |
b = Block() |
| 227 |
b.emit_instr(Instruction(opcode)) |
| 228 |
assert b.exits_function |
| 229 |
assert b.cant_add_instructions |