| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
import sys, re |
| 22 |
from io import StringIO |
| 23 |
|
| 24 |
out = StringIO() |
| 25 |
handler_size_bytes = "NTERP_HANDLER_SIZE" |
| 26 |
handler_size_bits = "NTERP_HANDLER_SIZE_LOG2" |
| 27 |
opcode = "" |
| 28 |
opnum = "" |
| 29 |
|
| 30 |
def write_line(line): |
| 31 |
out.write(line + "\n") |
| 32 |
|
| 33 |
def balign(): |
| 34 |
write_line(" .balign {}".format(handler_size_bytes)) |
| 35 |
|
| 36 |
def write_opcode(num, name, write_method): |
| 37 |
global opnum, opcode |
| 38 |
opnum, opcode = str(num), name |
| 39 |
write_line("/* ------------------------------ */") |
| 40 |
balign() |
| 41 |
write_line(".L_{1}: /* {0:#04x} */".format(num, name)) |
| 42 |
opcode_start() |
| 43 |
opcode_pre() |
| 44 |
write_method() |
| 45 |
opcode_end() |
| 46 |
write_line("") |
| 47 |
opnum, opcode = None, None |
| 48 |
|
| 49 |
slow_paths = {} |
| 50 |
|
| 51 |
|
| 52 |
def add_slow_path(write_fn, *write_args, suffix="_slow_path"): |
| 53 |
name = opcode_name_prefix() + (opcode or "common") + suffix |
| 54 |
global out |
| 55 |
|
| 56 |
old_out = out |
| 57 |
out = StringIO() |
| 58 |
opcode_slow_path_start(name) |
| 59 |
write_fn(*write_args) |
| 60 |
opcode_slow_path_end(name) |
| 61 |
out.seek(0) |
| 62 |
code = out.read() |
| 63 |
if name in slow_paths: |
| 64 |
assert slow_paths[name] == code, "Non-matching redefinition of " + name |
| 65 |
slow_paths[name] = code |
| 66 |
out = old_out |
| 67 |
return name |
| 68 |
|
| 69 |
def generate(output_filename): |
| 70 |
out.seek(0) |
| 71 |
out.truncate() |
| 72 |
write_line("/* DO NOT EDIT: This file was generated by gen-mterp.py. */") |
| 73 |
header() |
| 74 |
entry() |
| 75 |
|
| 76 |
instruction_start() |
| 77 |
opcodes() |
| 78 |
balign() |
| 79 |
instruction_end() |
| 80 |
|
| 81 |
for name, slow_path in sorted(slow_paths.items()): |
| 82 |
out.write(slow_path) |
| 83 |
|
| 84 |
footer() |
| 85 |
|
| 86 |
out.seek(0) |
| 87 |
|
| 88 |
text = re.sub(r"(\n\n)(\n)+", r"\1", out.read()) |
| 89 |
with open(output_filename, 'w') as output_file: |
| 90 |
output_file.write(text) |
| 91 |
|