kvisc/vm/in/arch_i.py

158 lines
3.6 KiB
Python
Raw Normal View History

2019-05-16 19:59:14 +02:00
#!/usr/bin/python3
2019-05-15 21:47:08 +02:00
2019-05-30 12:44:56 +02:00
# The OS/K Team licenses this file to you under the MIT license.
2019-05-15 21:56:42 +02:00
# See the LICENSE file in the project root for more information.
2019-05-15 21:47:08 +02:00
2019-06-02 16:33:28 +02:00
from tempfile import TemporaryFile
2019-05-15 21:47:08 +02:00
def getflag(s):
if s == "r":
return "P_REG"
if s == "i":
return "P_IMM"
if s == "m":
return "P_MEM"
2019-05-30 12:44:56 +02:00
2019-06-06 14:57:34 +02:00
return "__FLAG_ERROR__"
2019-07-01 21:46:36 +02:00
def doprnt(fp, i, p1, p2, p3):
2019-06-06 14:57:34 +02:00
for c1 in p1:
for c2 in p2:
2019-07-01 21:46:36 +02:00
for c3 in p3:
fp.write("{} {} {} {}".format(i, c1, c2, c3).strip())
fp.write('\n')
2019-05-15 21:47:08 +02:00
2019-06-23 12:40:18 +02:00
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()
2019-05-30 12:44:56 +02:00
2019-06-23 12:40:18 +02:00
if line[:8] == "include ":
_, fn = line.split(' ', 1)
fi2 = open(fn[1:-1])
parse_1(fi2, fp)
continue
2019-05-30 12:44:56 +02:00
2019-07-01 21:46:36 +02:00
tok = line.split()
2019-06-02 16:33:28 +02:00
2019-06-23 12:40:18 +02:00
if len(tok) == 0:
continue
2019-06-02 16:33:28 +02:00
2019-07-01 21:46:36 +02:00
i = tok[0]
2019-06-02 16:33:28 +02:00
2019-06-23 12:40:18 +02:00
if len(tok) == 1:
2019-07-01 21:46:36 +02:00
doprnt(fp, i, ' ', ' ', ' ')
2019-06-23 12:40:18 +02:00
continue
2019-06-02 16:33:28 +02:00
2019-06-23 12:40:18 +02:00
if len(tok) == 2:
2019-07-01 21:46:36 +02:00
p = tok[1]
doprnt(fp, i, p, ' ', ' ')
2019-06-23 12:40:18 +02:00
continue
2019-06-06 14:57:34 +02:00
2019-07-01 21:46:36 +02:00
if len(tok) == 3:
p1 = tok[1]
p2 = tok[2]
doprnt(fp, i, p1, p2, ' ')
continue
assert(len(tok) == 4)
p1 = tok[1]
p2 = tok[2]
p3 = tok[3]
2019-06-02 16:33:28 +02:00
2019-07-01 21:46:36 +02:00
doprnt(fp, i, p1, p2, p3)
2019-06-06 14:57:34 +02:00
2019-06-23 12:40:18 +02:00
fi.close()
2019-06-02 16:33:28 +02:00
2019-06-23 12:40:18 +02:00
def parse_2(fp):
global count
fp.seek(0)
2019-06-16 11:58:12 +02:00
2019-06-23 12:40:18 +02:00
for _, line in enumerate(fp):
tok = line.strip().split(' ')
2019-06-16 11:58:12 +02:00
2019-06-23 12:40:18 +02:00
assert(len(tok) > 0)
2019-05-15 21:47:08 +02:00
2019-06-23 12:40:18 +02:00
deprecated = ''
if tok[0][0] == '!':
assert(len(tok[0]) > 1)
deprecated = '__'
tok[0] = tok[0][1:]
2019-05-30 12:44:56 +02:00
2019-06-23 12:40:18 +02:00
if len(tok) == 1:
name = tok[0]
p1 = "NOPRM"
p2 = "NOPRM"
2019-07-01 21:46:36 +02:00
p3 = "NOPRM"
2019-05-30 12:44:56 +02:00
2019-06-23 12:40:18 +02:00
elif len(tok) == 2:
2019-07-01 21:46:36 +02:00
name = "{}_{}".format(tok[0], tok[1])
2019-06-23 12:40:18 +02:00
p1 = getflag(tok[1])
p2 = "NOPRM"
2019-07-01 21:46:36 +02:00
p3 = "NOPRM"
2019-05-15 21:47:08 +02:00
2019-06-23 12:40:18 +02:00
elif len(tok) == 3:
2019-07-01 21:46:36 +02:00
name = "{}_{}_{}".format(tok[0], tok[1], tok[2])
p1 = getflag(tok[1])
p2 = getflag(tok[2])
p3 = "NOPRM"
elif len(tok) == 4:
name = "{}_{}_{}_{}".format(tok[0], tok[1], tok[2], tok[3])
2019-06-23 12:40:18 +02:00
p1 = getflag(tok[1])
p2 = getflag(tok[2])
2019-07-01 21:46:36 +02:00
p3 = getflag(tok[3])
2019-05-19 19:54:29 +02:00
2019-06-23 12:40:18 +02:00
else:
name = "__TOK_ERROR__"
p1 = "__TOK_ERROR__"
p2 = "__TOK_ERROR__"
2019-07-01 21:46:36 +02:00
p3 = "__TOK_ERROR__"
2019-05-15 21:47:08 +02:00
2019-06-23 12:40:18 +02:00
ls.write("{}{}\n".format(deprecated, name))
hd.write("#ifdef _NEED_ARCH_I\n")
2019-07-01 21:46:36 +02:00
hd.write('{{ "{}{}", "{}{}", {}, {}, {}, i_{} }},\n'\
.format(deprecated, tok[0], deprecated, name, p1, p2, p3, tok[0]))
2019-06-23 12:40:18 +02:00
hd.write("#else\n")
hd.write("#define I_{} {}\n".format(name.upper(), count))
2019-07-01 21:46:36 +02:00
hd.write("extern bool i_{}(ctx_t *, acc_t *, acc_t *, acc_t *, ulong *, ulong *, ulong *);\n"
2019-06-23 12:40:18 +02:00
.format(tok[0]))
hd.write("#endif\n\n")
count = count + 1
fi = open("INSTRS")
2019-07-18 22:49:31 +02:00
hd = open("../ob/arch_i.h", "w")
ls = open("../ob/instrs.lst", "w")
2019-06-23 12:40:18 +02:00
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)
2019-05-15 21:47:08 +02:00
hd.write("#ifdef _NEED_ARCH_I\n")
hd.write("};\n")
2019-05-16 16:48:45 +02:00
hd.write("#else\n")
hd.write("#define NINSTRS {}\n\n".format(count))
2019-05-15 21:47:08 +02:00
hd.write("#endif\n")
2019-05-19 19:54:29 +02:00
ls.close()
2019-05-15 21:47:08 +02:00
hd.close()