-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild_bytecode_table.py
More file actions
83 lines (69 loc) · 2.18 KB
/
build_bytecode_table.py
File metadata and controls
83 lines (69 loc) · 2.18 KB
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
70
71
72
73
74
75
76
77
78
"""Build table mapping integer byte codes to function pointers.
Machine operations, their names, and the number of operands
for each are given in opdefs.txt.
"""
import argparse
import datetime
# Fixed code at beginning of generated file
import sys
import logging
logging.basicConfig()
log = logging.getLogger(__name__)
log.setLevel(logging.DEBUG)
LB = "{"
RB = "}"
PROLOGUE = f"""
/**
* GENERATED CODE, DO NOT EDIT
* Generated {datetime.datetime.now()} by build_bytecode_table.py
*
* Integer encoding of VM operations ---
* Map those integer encodings to function pointers (for executing)
* and to strings (for debugging and assembling).
*/
#include "vm_code_table.h"
op_tbl_entry vm_op_bytecodes[] = {LB}
"""
# Fixed code at end of generated file
CODA = """
{ 0, 0, 0} // SENTRY
};
"""
def cli() -> object:
"""Command line interface"""
parser = argparse.ArgumentParser(prog=__name__,
description="Build bytecode table")
parser.add_argument("infile", type=argparse.FileType("r"),
nargs="?", default=sys.stdin,
help="Textual table of operations")
parser.add_argument("outfile", type=argparse.FileType("w"),
nargs="?", default=sys.stdout,
help="Put C header file here")
args = parser.parse_args()
return args
def main():
log.info("Bytecode table generation")
args = cli()
print(PROLOGUE, file=args.outfile)
next_byte_code = 0;
for line in args.infile:
line = line.strip()
# Strip off comments
parts = line.split("#")
comment = ""
if len(parts) > 1:
comment = "#".join(parts[1:])
line = parts[0].strip()
# Is there anything left?
if len(line) == 0:
continue
parts = line.split(",")
assert len(parts) == 3, f"Couldn't parse {line}"
name, func, inlines = parts
print(f'\t {LB} "{name}", {func}, {inlines} {RB}, //{next_byte_code} {comment}',
file=args.outfile)
next_byte_code += 1
print(CODA, file=args.outfile)
log.info("Finished bytecode table generation")
if __name__ == "__main__":
main()