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

125 lines
2.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-16 20:09:20 +02:00
fi = open("INSTRS")
hd = open("arch_i.h", "w")
2019-05-19 19:54:29 +02:00
ls = open("instrs.lst", "w")
2019-05-15 21:47:08 +02:00
count = 0
2019-05-29 16:57:22 +02:00
hd.write("// Auto-generated by arch_i.py from INSTRS\n\n");
2019-05-16 19:49:51 +02:00
2019-05-15 21:47:08 +02:00
hd.write("#ifdef _NEED_ARCH_I\n")
hd.write("instr_t arch_i[] =\n{\n\n")
hd.write("#endif\n")
2019-06-02 16:33:28 +02:00
fp = TemporaryFile(mode="w+")
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-06-07 22:23:38 +02:00
def doprnt(i, p1, p2):
2019-06-06 14:57:34 +02:00
for c1 in p1:
for c2 in p2:
fp.write("{} {} {}".format(i, c1, c2).strip())
fp.write('\n')
2019-05-15 21:47:08 +02:00
2019-06-02 16:33:28 +02:00
# "instr ri ri" => "instr r r\ninstr r i\ninstr i r..."
# not optimal but will do for now
2019-05-15 21:47:08 +02:00
for _, line in enumerate(fi):
if line[0] == '#' or line[0] == ' ' or line[0] == '\n':
continue
2019-05-30 12:44:56 +02:00
2019-05-16 16:48:45 +02:00
tok = line.strip().split(' ')
2019-05-30 12:44:56 +02:00
2019-06-02 16:33:28 +02:00
if len(tok) == 0:
continue
i = tok[0].strip()
if len(tok) == 1:
2019-06-07 22:23:38 +02:00
doprnt(i, ' ', ' ')
2019-06-02 16:33:28 +02:00
continue
if len(tok) == 2:
p = tok[1].strip()
2019-06-07 22:23:38 +02:00
doprnt(i, p, ' ')
2019-06-02 16:33:28 +02:00
continue
assert(len(tok) == 3)
p1 = tok[1].strip()
p2 = tok[2].strip()
2019-06-06 14:57:34 +02:00
2019-06-07 22:23:38 +02:00
doprnt(i, p1, p2)
2019-06-02 16:33:28 +02:00
fp.seek(0)
2019-06-06 14:57:34 +02:00
2019-06-02 16:33:28 +02:00
for _, line in enumerate(fp):
tok = line.strip().split(' ')
2019-06-16 11:58:12 +02:00
assert(len(tok) > 0)
deprecated = ''
if tok[0][0] == '!':
assert(len(tok[0]) > 1)
deprecated = '__'
tok[0] = tok[0][1:]
2019-05-15 21:47:08 +02:00
if len(tok) == 1:
2019-05-16 16:48:45 +02:00
name = tok[0]
2019-05-15 21:47:08 +02:00
p1 = "NOPRM"
p2 = "NOPRM"
elif len(tok) == 2:
name = "{}_{}".format(tok[0], tok[1].strip())
2019-05-16 16:48:45 +02:00
p1 = getflag(tok[1])
2019-05-15 21:47:08 +02:00
p2 = "NOPRM"
2019-05-30 12:44:56 +02:00
2019-05-15 21:47:08 +02:00
elif len(tok) == 3:
name = "{}_{}_{}".format(tok[0], tok[1], tok[2].strip())
p1 = getflag(tok[1])
2019-05-16 16:48:45 +02:00
p2 = getflag(tok[2])
2019-05-30 12:44:56 +02:00
2019-05-15 21:47:08 +02:00
else:
2019-06-06 14:57:34 +02:00
name = "__TOK_ERROR__"
p1 = "__TOK_ERROR__"
p2 = "__TOK_ERROR__"
2019-05-15 21:47:08 +02:00
2019-06-16 11:58:12 +02:00
ls.write("{}{}\n".format(deprecated, name))
2019-05-19 19:54:29 +02:00
2019-05-15 21:47:08 +02:00
hd.write("#ifdef _NEED_ARCH_I\n")
2019-06-16 11:58:12 +02:00
hd.write('{{ "{}{}", "{}{}", {}, {}, i_{} }},\n'\
.format(deprecated, tok[0], deprecated, name, p1, p2, tok[0]))
2019-05-16 16:48:45 +02:00
hd.write("#else\n")
hd.write("#define I_{} {}\n".format(name.upper(), count))
2019-06-12 15:47:11 +02:00
hd.write("extern bool i_{}(ctx_t *, acc_t *, acc_t *, ulong *, ulong *);\n"
2019-05-16 16:48:45 +02:00
.format(tok[0]))
2019-05-15 21:47:08 +02:00
hd.write("#endif\n\n")
count = count + 1
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()
fi.close()