1 # Pep/8 instructions are not reserved as keywords. It is common
2 # that the identifier of an instruction will be used for a label.
3 # For this reason, we cannot create precise instruction nodes with
6 # This module manually creates the expected subclass with an additionnal
9 module rich_instructions
15 redef class AnalysisManager
16 redef fun build_ast
(filename
)
20 #fun enrich_ast( ast : AListing ) do
21 for line
in ast
.n_lines
do
22 if line
isa AInstructionLine then
31 redef class AInstructionLine
32 # TODO move to AnalysisManager as private?
35 var instr
= n_instruction
36 var new_instr
: AInstruction
38 var id
= instr
.n_id
.text
.to_upper
39 if instr
isa ABinaryInstruction then
41 new_instr
= new ABrInstruction.from
(instr
)
42 else if id
== "BRLE" then
43 new_instr
= new ABrleInstruction.from
(instr
)
44 else if id
== "BRLT" then
45 new_instr
= new ABrltInstruction.from
(instr
)
46 else if id
== "BREQ" then
47 new_instr
= new ABreqInstruction.from
(instr
)
48 else if id
== "BRNE" then
49 new_instr
= new ABrneInstruction.from
(instr
)
50 else if id
== "BRGE" then
51 new_instr
= new ABrgeInstruction.from
(instr
)
52 else if id
== "BRGT" then
53 new_instr
= new ABrgtInstruction.from
(instr
)
54 else if id
== "BRV" then
55 new_instr
= new ABrvInstruction.from
(instr
)
56 else if id
== "BRC" then
57 new_instr
= new ABrcInstruction.from
(instr
)
58 else if id
== "CALL" then
59 new_instr
= new ACallInstruction.from
(instr
)
60 else if id
== "NOP" then
61 new_instr
= new ABinaryNopInstruction.from
(instr
)
62 else if id
== "DECI" then
63 new_instr
= new ADeciInstruction.from
(instr
)
64 else if id
== "DECO" then
65 new_instr
= new ADecoInstruction.from
(instr
)
66 else if id
== "STRO" then
67 new_instr
= new AStroInstruction.from
(instr
)
68 else if id
== "CHARI" then
69 new_instr
= new AChariInstruction.from
(instr
)
70 else if id
== "CHARO" then
71 new_instr
= new ACharoInstruction.from
(instr
)
72 else if id
== "ADDSP" then
73 new_instr
= new AAddspInstruction.from
(instr
)
74 else if id
== "SUBSP" then
75 new_instr
= new ASubspInstruction.from
(instr
)
76 else if id
.has_prefix
("ADD") then
77 new_instr
= new AAddInstruction.from
(instr
)
78 else if id
.has_prefix
("SUB") then
79 new_instr
= new ASubInstruction.from
(instr
)
80 else if id
.has_prefix
("AND") then
81 new_instr
= new AAndInstruction.from
(instr
)
82 else if id
.has_prefix
("OR") then
83 new_instr
= new AOrInstruction.from
(instr
)
84 else if id
.has_prefix
("CP") then
85 new_instr
= new ACpInstruction.from
(instr
)
86 else if id
.has_prefix
("LDBYTE") then
87 new_instr
= new ALdbyteInstruction.from
(instr
)
88 else if id
.has_prefix
("LD") then
89 new_instr
= new ALdInstruction.from
(instr
)
90 else if id
.has_prefix
("STBYTE") then
91 new_instr
= new AStbyteInstruction.from
(instr
)
92 else if id
.has_prefix
("ST") then
93 new_instr
= new AStInstruction.from
(instr
)
96 print
"error {instr.location}: invalid instruction {id} with data access"
99 else if instr
isa AUnaryInstruction then
101 new_instr
= new AStopInstruction.from
(instr
)
102 else if id
== "RETTR" then
103 new_instr
= new ARettrInstruction.from
(instr
)
104 else if id
== "MOVSPA" then
105 new_instr
= new AMovspaInstruction.from
(instr
)
106 else if id
== "MOVFLGA" then
107 new_instr
= new AMovflgaInstruction.from
(instr
)
108 else if id
.has_prefix
("NOT") then
109 new_instr
= new ANotInstruction.from
(instr
)
110 else if id
.has_prefix
("NEG") then
111 new_instr
= new ANegInstruction.from
(instr
)
112 else if id
.has_prefix
("ASL") then
113 new_instr
= new AAslInstruction.from
(instr
)
114 else if id
.has_prefix
("ASR") then
115 new_instr
= new AAsrInstruction.from
(instr
)
116 else if id
.has_prefix
("ROL") then
117 new_instr
= new ARolInstruction.from
(instr
)
118 else if id
.has_prefix
("ROR") then
119 new_instr
= new ARorInstruction.from
(instr
)
120 else if id
.has_prefix
("NOP") then
121 new_instr
= new AUnaryNopInstruction.from
(instr
)
122 else if id
.has_prefix
("RET") then
123 new_instr
= new ARetInstruction.from
(instr
)
126 print
"error {instr.location}: invalid instruction {id} without data access"
131 # TODO check, one of those 2 might not be necessary
132 replace_child
( instr
, new_instr
)
133 #n_instruction = new_instr
140 abstract class ARichBinaryInstruction
141 super ABinaryInstruction
143 init from
( src
: ABinaryInstruction ) do
144 _n_operand
= src
.n_operand
146 location
= src
.location
151 abstract class ARichUnaryInstruction
152 super AUnaryInstruction
154 init from
( src
: AUnaryInstruction ) do
156 _location
= src
.location
164 abstract class ABranchInstruction
165 super ARichBinaryInstruction
166 init from
( src
) do super
170 super ABranchInstruction
171 init from
( src
) do super
173 class ABrleInstruction
174 super ABranchInstruction
175 init from
( src
) do super
177 class ABrltInstruction
178 super ABranchInstruction
179 init from
( src
) do super
181 class ABreqInstruction
182 super ABranchInstruction
183 init from
( src
) do super
185 class ABrneInstruction
186 super ABranchInstruction
187 init from
( src
) do super
189 class ABrgeInstruction
190 super ABranchInstruction
191 init from
( src
) do super
193 class ABrgtInstruction
194 super ABranchInstruction
195 init from
( src
) do super
197 class ABrvInstruction
198 super ABranchInstruction
199 init from
( src
) do super
201 class ABrcInstruction
202 super ABranchInstruction
203 init from
( src
) do super
205 class ACallInstruction
206 super ABranchInstruction
207 init from
( src
) do super
211 abstract class ANopInstruction
214 class ABinaryNopInstruction
215 super ANopInstruction
216 super ARichBinaryInstruction
217 init from
( src
) do super
219 class AUnaryNopInstruction
220 super ANopInstruction
221 super ARichUnaryInstruction
222 init from
( src
) do super
226 abstract class AOutputInstruction
227 super ARichBinaryInstruction
228 init from
( src
) do super
230 abstract class AInputInstruction
231 super ARichBinaryInstruction
232 init from
( src
) do super
234 # TODO add category for ADecInstruction?
235 class ADeciInstruction
236 super AInputInstruction
237 init from
( src
) do super
239 class ADecoInstruction
240 super AOutputInstruction
241 init from
( src
) do super
244 class AStroInstruction
245 super AOutputInstruction
246 init from
( src
) do super
249 class AChariInstruction
250 super AInputInstruction
251 init from
( src
) do super
253 class ACharoInstruction
254 super AOutputInstruction
255 init from
( src
) do super
259 abstract class AStackInstruction
260 super ARichBinaryInstruction
261 init from
( src
) do super
263 class AAddspInstruction
264 super AStackInstruction
265 init from
( src
) do super
267 class ASubspInstruction
268 super AStackInstruction
269 init from
( src
) do super
273 class AStopInstruction
274 super ARichUnaryInstruction
275 init from
( src
) do super
278 class ARettrInstruction
279 super ARichUnaryInstruction
280 init from
( src
) do super
283 abstract class AMovInstruction
284 super ARichUnaryInstruction
285 init from
( src
) do super
287 class AMovspaInstruction
288 super AMovInstruction
289 init from
( src
) do super
291 class AMovflgaInstruction
292 super AMovInstruction
293 init from
( src
) do super
297 class ANotInstruction
298 super ARichUnaryInstruction
299 init from
( src
) do super
302 class ANegInstruction
303 super ARichUnaryInstruction
304 init from
( src
) do super
307 abstract class AShiftInstruction
308 super ARichUnaryInstruction
309 init from
( src
) do super
311 class AAslInstruction
312 super AShiftInstruction
313 init from
( src
) do super
315 class AAsrInstruction
316 super AShiftInstruction
317 init from
( src
) do super
319 class ARolInstruction
320 super AShiftInstruction
321 init from
( src
) do super
323 class ARorInstruction
324 super AShiftInstruction
325 init from
( src
) do super
328 class ARetInstruction
329 super ARichUnaryInstruction
330 init from
( src
) do super
334 # TODO find a better name
335 abstract class AArithmeticInstruction
336 super ARichBinaryInstruction
337 init from
( src
) do super
339 class AAddInstruction
340 super AArithmeticInstruction
341 init from
( src
) do super
343 class ASubInstruction
344 super AArithmeticInstruction
345 init from
( src
) do super
347 class AAndInstruction
348 super AArithmeticInstruction
349 init from
( src
) do super
352 super AArithmeticInstruction
353 init from
( src
) do super
357 super ARichBinaryInstruction
358 init from
( src
) do super
362 abstract class ALoadInstruction
363 super ARichBinaryInstruction
364 init from
( src
) do super
367 super ALoadInstruction
368 init from
( src
) do super
370 class ALdbyteInstruction
371 super ALoadInstruction
372 init from
( src
) do super
375 abstract class AStoreInstruction
376 super ARichBinaryInstruction
377 init from
( src
) do super
380 super AStoreInstruction
381 init from
( src
) do super
383 class AStbyteInstruction
384 super AStoreInstruction
385 init from
( src
) do super