-
Notifications
You must be signed in to change notification settings - Fork 5
/
maspsx.py
169 lines (141 loc) · 5.46 KB
/
maspsx.py
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import argparse
import subprocess
import sys
from typing import List
from maspsx import MaspsxProcessor
def main() -> None:
parser = argparse.ArgumentParser()
parser.add_argument("--aspsx-version", type=str)
parser.add_argument("--run-assembler", action="store_true")
parser.add_argument("--gnu-as-path", default="mips-linux-gnu-as")
parser.add_argument("--dont-force-G0", action="store_true")
parser.add_argument("--expand-div", action="store_true")
parser.add_argument("--macro-inc", action="store_true")
parser.add_argument("--dont-expand-li", action="store_true")
parser.add_argument("--force-stdin", action="store_true")
parser.add_argument("--use-comm-section", action="store_true")
parser.add_argument("--use-comm-for-lcomm", action="store_true")
# decomp.me debugging
parser.add_argument("--print-output", action="store_true")
parser.add_argument("--print-input", action="store_true")
# deprecated
parser.add_argument("--no-macro-inc", action="store_true")
parser.add_argument("--expand-li", action="store_true")
args, as_args = parser.parse_known_args()
if args.no_macro_inc:
sys.stderr.write(
"MASPSX: --no-macro-inc is no longer required and will be removed in a future update\n"
)
if args.expand_li:
sys.stderr.write(
"MASPSX: --expand-li is enabled automatically if --aspsx-version is below 2.56\n"
)
read_from_file = sys.stdin.isatty()
if not read_from_file:
in_lines = sys.stdin.readlines()
if len(in_lines) == 0:
if args.force_stdin:
sys.stderr.write("MASPSX: --force-stdin but no input from stdin!\n")
sys.exit(1)
else:
sys.stderr.write(
"MASPSX: Warning, no input from stdin, will try to read from a file\n"
)
read_from_file = True
if read_from_file:
try:
input_file = as_args.pop()
except IndexError:
sys.stderr.write("MASPSX: Error, no input file found!\n")
sys.exit(1)
with open(input_file, "r", encoding="utf") as f:
in_lines = f.readlines()
if args.print_input:
sys.stderr.write("".join(in_lines))
preamble = [
'.include "macro.inc"' if args.macro_inc else "",
]
sdata_limit = 0
filtered_as_args: List[str] = []
for arg in as_args:
# Can we stop gcc from passing us this flag?
if arg == "-KPIC":
continue
# GNU as does not support -mcpu flag
if arg.startswith("-mcpu="):
arg = arg.replace("-mcpu=", "-mtune=")
elif arg.startswith("-G") and len(arg) > 2:
sdata_limit = int(arg[2:])
filtered_as_args.append(arg)
div_uses_tge = False # use tge instruction instead of break in divide
nop_at_expansion = False # insert nop between v0/at?
nop_mflo_mfhi = True # ensure 2 ops between mfhi/mflo and div/mult
sltu_at = True # sltu uses at?
expand_li = True # turn li into lui/ori
gp_allow_offset = False # use gp for sym+offset?
gp_allow_la = False # use gp for la
addiu_at = False # use addiu when expanding lw to use $at
if args.aspsx_version:
aspsx_version = tuple(int(x) for x in args.aspsx_version.split("."))
if aspsx_version == (2, 8):
div_uses_tge = True
if aspsx_version <= (2, 21):
nop_at_expansion = True
nop_mflo_mfhi = False
addiu_at = True
if aspsx_version > (2, 34):
expand_li = False
if aspsx_version >= (2, 77):
sltu_at = False
gp_allow_offset = True
if aspsx_version >= (2, 81):
gp_allow_la = True
if args.dont_expand_li and expand_li:
expand_li = False
maspsx_processor = MaspsxProcessor(
in_lines,
sdata_limit=sdata_limit,
expand_div=args.expand_div,
expand_li=expand_li,
nop_at_expansion=nop_at_expansion,
nop_mflo_mfhi=nop_mflo_mfhi,
sltu_at=sltu_at,
addiu_at=addiu_at,
div_uses_tge=div_uses_tge,
gp_allow_offset=gp_allow_offset,
gp_allow_la=gp_allow_la,
use_comm_section=args.use_comm_section,
use_comm_for_lcomm=args.use_comm_for_lcomm,
)
try:
out_lines = maspsx_processor.process_lines()
except Exception as err:
sys.stderr.write(f"MASPSX: An exception occurred: {err}\n")
sys.exit(1)
out_text = "\n".join(preamble + out_lines)
# avoid "Warning: end of file not at end of a line; newline inserted"
out_text += "\n"
if args.print_output:
sys.stderr.write(out_text)
if args.run_assembler:
cmd = [
args.gnu_as_path,
"-EL", # TODO: switch from 'mips-linux-gnu-as' to 'mipsel-linux-gnu-as'
*filtered_as_args,
"-", # read from stdin
]
if not args.dont_force_G0:
cmd.insert(-1, "-G0")
with subprocess.Popen(
cmd, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE
) as process:
out_bytes = out_text.encode("utf")
stdout, stderr = process.communicate(input=out_bytes)
if len(stdout):
sys.stdout.write(stdout.decode("utf"))
if len(stderr):
sys.stderr.write(stderr.decode("utf"))
else:
sys.stdout.write(out_text)
if __name__ == "__main__":
main()