-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasmsed.py
382 lines (287 loc) · 9 KB
/
asmsed.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
import abc
import re
class Instruction(metaclass=abc.ABCMeta):
@classmethod
def from_args(cls, argv):
assert len(argv) == 0
return cls()
class Load(Instruction):
MNEMONIC = "load"
NARGS = 1
TEMPLATE = r"s/^(([^#]*#){{{stack_offset}}})([^#]*)((#[^#]*)*)$/\1\3\4#\3/"
# load $offset
def __init__(self, stack_offset):
super().__init__()
self.stack_offset = stack_offset
@classmethod
def from_args(cls, argv):
return cls(int(argv[0]))
def __repr__(self):
return "<instruction: load {}>".format(self.stack_offset)
def tosed(self):
return self.TEMPLATE.format(
stack_offset=self.stack_offset
)
class Store(Instruction):
MNEMONIC = "store"
NARGS = 1
TEMPLATE = r"s/^(([^#]*#){{{stack_offset}}})([^#]*)((#[^#]*)*)#([^#]*)$/\1\6\4/"
# store $offset
def __init__(self, stack_offset):
super().__init__()
self.stack_offset = stack_offset
@classmethod
def from_args(cls, argv):
return cls(int(argv[0]))
def __repr__(self):
return "<instruction: store {}>".format(self.stack_offset)
def tosed(self):
return self.TEMPLATE.format(
stack_offset=self.stack_offset
)
class Label(Instruction):
EXPRESSION = re.compile(r"^\.([a-z0-9][a-z0-9_]*)$", re.I)
TEMPLATE = ":{label_id}\n"
def __init__(self, label_id):
super().__init__()
self.label_id = label_id
@classmethod
def from_args(cls, argv):
return cls(argv[0])
def __repr__(self):
return "<label {!r}>".format(self.label_id)
def tosed(self):
return self.TEMPLATE.format(
instance=id(self),
label_id=self.label_id,
)
class Jump(Instruction):
MNEMONIC = "jmp"
NARGS = 1
TEMPLATE = r"b{label_id}"
def __init__(self, label_id):
super().__init__()
self.label_id = label_id
@classmethod
def from_args(cls, argv):
return cls(argv[0])
def __repr__(self):
return "<instruction: jmp {!r}>".format(self.label_id)
def tosed(self):
return self.TEMPLATE.format(
instance=id(self),
label_id=self.label_id,
)
class PushConstant(Instruction):
MNEMONIC = "pushc"
NARGS = 1
TEMPLATE = r"s/^(.*)$/\1#{value:b}/"
def __init__(self, value):
super().__init__()
self.value = value
@classmethod
def from_args(cls, argv):
return cls(int(argv[0]))
def __repr__(self):
return "<instruction: pushc {}>".format(self.value)
def tosed(self):
return self.TEMPLATE.format(
instance=id(self),
value=self.value
)
class Dup(Instruction):
MNEMONIC = "dup"
NARGS = 0
TEMPLATE = r"""
s/^((.*)#)?([^#]*)$/\1\3#\3/
"""
def __repr__(self):
return "<instruction: inc>"
def tosed(self):
return self.TEMPLATE.format(
instance=id(self),
)
class Pop(Instruction):
MNEMONIC = "pop"
NARGS = 0
TEMPLATE = r"s/^((.*)#|^)[^#]*$/\2/"
def __repr__(self):
return "<instruction: inc>"
def tosed(self):
return self.TEMPLATE.format(
instance=id(self),
)
class JumpIfZero(Instruction):
MNEMONIC = "jz"
NARGS = 1
TEMPLATE = r"/^((.*)#)?0$/{{{{;{pop};b{{label_id}};}}}};{pop}".format(pop=Pop.TEMPLATE)
def __init__(self, label_id):
super().__init__()
self.label_id = label_id
@classmethod
def from_args(cls, argv):
return cls(argv[0])
def __repr__(self):
return "<instruction: jz {!r}>".format(self.label_id)
def tosed(self):
return self.TEMPLATE.format(
instance=id(self),
label_id=self.label_id,
)
class Inc(Instruction):
MNEMONIC = "inc"
NARGS = 0
TEMPLATE = """\
:_inc{instance}d
s/^((.*)#)?([01]*)1(_*)$/\\1\\3_\\4/;
t_inc{instance}d
s/^((.*)#)?(_*)$/\\10\\3/
s/^((.*)#)?([01]*)0(_*)$/\\1\\31\\4/;
y/_/0/
s/^((.*)#)?0*(1[01]*)$/\\1\\3/;
s/^((.*)#)?$/\\10/;
"""
def __repr__(self):
return "<instruction: inc>"
def tosed(self):
return self.TEMPLATE.format(
instance=id(self),
)
class Add(Instruction):
MNEMONIC = "add"
NARGS = 0
TEMPLATE = """\
s/^(.*)$/\\1#0#/; t_add{instance}
:_add{instance}
s/^(.*)###([01])#([01]*)$/\\1#\\2\\3/; t_add{instance}end
s/^(.*)##([01]+)#([01])#([01]*)$/\\1#0#\\2#\\3#\\4/
s/^(.*)#([01]+)##([01])#([01]*)$/\\1#\\2#0#\\3#\\4/
s/^(.*)#([01]*)0#([01]*)0#0#([01]*)$/\\1#\\2#\\3#0#0\\4/; t_add{instance}
s/^(.*)#([01]*)0#([01]*)0#1#([01]*)$/\\1#\\2#\\3#0#1\\4/; t_add{instance}
s/^(.*)#([01]*)0#([01]*)1#0#([01]*)$/\\1#\\2#\\3#0#1\\4/; t_add{instance}
s/^(.*)#([01]*)1#([01]*)0#0#([01]*)$/\\1#\\2#\\3#0#1\\4/; t_add{instance}
s/^(.*)#([01]*)0#([01]*)1#1#([01]*)$/\\1#\\2#\\3#1#0\\4/; t_add{instance}
s/^(.*)#([01]*)1#([01]*)1#0#([01]*)$/\\1#\\2#\\3#1#0\\4/; t_add{instance}
s/^(.*)#([01]*)1#([01]*)0#1#([01]*)$/\\1#\\2#\\3#1#0\\4/; t_add{instance}
s/^(.*)#([01]*)1#([01]*)1#1#([01]*)$/\\1#\\2#\\3#1#1\\4/; t_add{instance}
s/^(.*)$/invalid state: \\1/; p; q1
:_add{instance}end
s/^((.*)#|^)0*(1[01]*)$/\\1\\3/;
s/^((.*)#|^)$/\\10/;
"""
def __repr__(self):
return "<instruction: add>"
def tosed(self):
return self.TEMPLATE.format(
instance=id(self),
)
class Dec(Instruction):
MNEMONIC = "dec"
NARGS = 0
TEMPLATE = """\
:_dec{instance}d
s/^((.*)#)?([01]*)0(_*)$/\\1\\3_\\4/;
t_dec{instance}d
s/^((.*)#)?(_*)$/\\10/; t_dec{instance}end
s/^((.*)#)?([01]*)1(_*)$/\\1\\30\\4/;
y/_/1/
:_dec{instance}end
s/^((.*)#)?0*(1[01]*)$/\\1\\3/;
s/^((.*)#)?$/\\10/;
"""
def __repr__(self):
return "<instruction: dec>"
def tosed(self):
return self.TEMPLATE.format(
instance=id(self),
)
class Sub(Instruction):
MNEMONIC = "sub"
NARGS = 0
TEMPLATE = """\
s/^(.*)$/\\1#0#/; t_sub{instance}
:_sub{instance}
/^(.*)###1#[01]*$/b_sub{instance}_zero
s/^(.*)###0#([01]*)$/\\1#\\2/; t_sub{instance}_end
/^(.*)##[01]+#[01]#[01]*$/b_sub{instance}_zero
s/^(.*)#([01]+)##([01])#([01]*)$/\\1#\\2#0#\\3#\\4/
s/^(.*)#([01]*)0#([01]*)0#0#([01]*)$/\\1#\\2#\\3#0#0\\4/; t_sub{instance}
s/^(.*)#([01]*)0#([01]*)0#1#([01]*)$/\\1#\\2#\\3#1#1\\4/; t_sub{instance}
s/^(.*)#([01]*)0#([01]*)1#0#([01]*)$/\\1#\\2#\\3#1#1\\4/; t_sub{instance}
s/^(.*)#([01]*)1#([01]*)0#0#([01]*)$/\\1#\\2#\\3#0#1\\4/; t_sub{instance}
s/^(.*)#([01]*)0#([01]*)1#1#([01]*)$/\\1#\\2#\\3#1#0\\4/; t_sub{instance}
s/^(.*)#([01]*)1#([01]*)1#0#([01]*)$/\\1#\\2#\\3#0#0\\4/; t_sub{instance}
s/^(.*)#([01]*)1#([01]*)0#1#([01]*)$/\\1#\\2#\\3#0#0\\4/; t_sub{instance}
s/^(.*)#([01]*)1#([01]*)1#1#([01]*)$/\\1#\\2#\\3#1#1\\4/; t_sub{instance}
:_sub{instance}_zero
s/^(.*)#([01]*)#([01]*)#([01])#([01]*)$/\\1#0/
:_sub{instance}_end
s/^((.*)#|^)?0*(1[01]*)?$/\\1\\3/;
s/^((.*)#|^)?$/\\10/;
"""
def __repr__(self):
return "<instruction: sub>"
def tosed(self):
return self.TEMPLATE.format(
instance=id(self),
)
INSTRUCTION_SET = list(Instruction.__subclasses__())
COMMENT = re.compile(r"^([^;]*);.*$")
def parse_asm(lines):
matchmap = []
for insn in INSTRUCTION_SET:
try:
expr = insn.EXPRESSION
except AttributeError:
expr = re.compile("{}{}".format(
re.escape(insn.MNEMONIC),
r"\s+(\S+)" * insn.NARGS
))
matchmap.append((expr, insn))
instructions = []
for i, line_raw in enumerate(lines, 1):
line = COMMENT.sub(r"\1", line_raw)
line = line.strip()
if not line:
continue
for expr, cls in matchmap:
m = expr.match(line)
if m is None:
continue
instructions.append(cls.from_args(m.groups()))
break
else:
raise ValueError("invalid syntax:{}: {}".format(i, line_raw.rstrip("\n")))
return instructions
if __name__ == "__main__":
import argparse
import sys
parser = argparse.ArgumentParser()
parser.add_argument(
"infile",
nargs="?",
type=argparse.FileType("r"),
default=sys.stdin,
)
parser.add_argument(
"-o", "--output",
dest="outfile",
default=None,
)
args = parser.parse_args()
with args.infile as f:
instructions = parse_asm(f)
buf = []
for insn in instructions:
buf.append(insn.tosed())
if args.outfile is not None:
args.outfile = open(args.outfile, "w")
else:
args.outfile = sys.stdout
with args.outfile as f:
print("#!/bin/sed -rf", file=f)
print(r"s/^(.*)$/#\1/", file=f)
for l in buf:
print(l, file=f)
print(r":_exit", file=f)
print(r"s/^#(.*)$/\1/", file=f)