1
0
mirror of https://gitlab.os-k.eu/os-k-team/kvisc.git synced 2023-08-25 14:05:46 +02:00
kvisc/vm/in/arch_i.py
julianb0 cbd079d2a0
vm
2019-08-14 20:23:05 +02:00

88 lines
2.1 KiB
Python

#!/usr/bin/python3
# The OS/K Team licenses this file to you under the MIT license.
# See the LICENSE file in the project root for more information.
from tempfile import TemporaryFile
def parse_1(fi, fp):
global count
# "instr ri ri" => "instr r r\ninstr r i\ninstr i r..."
# not optimal but will do for now
for _, line in enumerate(fi):
if line[0] == '#' or line[0] == ' ' or line[0] == '\n':
continue
line = line.strip()
if line[:8] == "include ":
_, fn = line.split(' ', 1)
fi2 = open(fn[1:-1])
parse_1(fi2, fp)
continue
fp.write("{}\n".format(line))
def parse_2(fp):
global count
fp.seek(0)
for _, line in enumerate(fp):
tok = line.strip().split(' ')
assert(len(tok) > 0)
deprecated = ''
if tok[0][0] == '!':
assert(len(tok[0]) > 1)
deprecated = '__'
tok[0] = tok[0][1:]
if len(tok) == 1:
name = tok[0]
n = 0
else:
assert(len(tok) == 2)
n = tok[1]
ls.write("{}{}_{}\n".format(deprecated, tok[0], n))
hd.write("#ifdef _NEED_ARCH_I\n")
hd.write('{{ "{}{}", {}, i_{}_{} }},\n'\
.format(deprecated, tok[0], n, tok[0], n))
hd.write("#else\n")
hd.write("#define I_{}_{} {}\n".format(tok[0].upper(), n, count))
hd.write("extern bool i_{}_{}(acc_t *, acc_t *, acc_t *, ulong *, ulong *);\n"
.format(tok[0], n))
hd.write("#endif\n\n")
count = count + 1
fi = open("INSTRS")
hd = open("../ob/arch_i.h", "w")
ls = open("../ob/instrs.lst", "w")
count = 0
hd.write("// Auto-generated by arch_i.py from INSTRS\n\n");
hd.write("#ifdef _NEED_ARCH_I\n")
hd.write("instr_t arch_i[] =\n{\n\n")
hd.write("#endif\n")
fp = TemporaryFile(mode="w+")
parse_1(fi, fp)
parse_2(fp)
hd.write("#ifdef _NEED_ARCH_I\n")
hd.write("};\n")
hd.write("#else\n")
hd.write("#define NINSTRS {}\n\n".format(count))
hd.write("#endif\n")
ls.close()
hd.close()