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
152 abstract class ARichUnaryInstruction
153 super AUnaryInstruction
155 init from
( src
: AUnaryInstruction ) do
166 abstract class ABranchInstruction
167 super ARichBinaryInstruction
168 init from
( src
) do super
172 super ABranchInstruction
173 init from
( src
) do super
175 class ABrleInstruction
176 super ABranchInstruction
177 init from
( src
) do super
179 class ABrltInstruction
180 super ABranchInstruction
181 init from
( src
) do super
183 class ABreqInstruction
184 super ABranchInstruction
185 init from
( src
) do super
187 class ABrneInstruction
188 super ABranchInstruction
189 init from
( src
) do super
191 class ABrgeInstruction
192 super ABranchInstruction
193 init from
( src
) do super
195 class ABrgtInstruction
196 super ABranchInstruction
197 init from
( src
) do super
199 class ABrvInstruction
200 super ABranchInstruction
201 init from
( src
) do super
203 class ABrcInstruction
204 super ABranchInstruction
205 init from
( src
) do super
207 class ACallInstruction
208 super ABranchInstruction
209 init from
( src
) do super
213 abstract class ANopInstruction
216 class ABinaryNopInstruction
217 super ANopInstruction
218 super ARichBinaryInstruction
219 init from
( src
) do super
221 class AUnaryNopInstruction
222 super ANopInstruction
223 super ARichUnaryInstruction
224 init from
( src
) do super
228 abstract class AOutputInstruction
229 super ARichBinaryInstruction
230 init from
( src
) do super
232 abstract class AInputInstruction
233 super ARichBinaryInstruction
234 init from
( src
) do super
236 # TODO add category for ADecInstruction?
237 class ADeciInstruction
238 super AInputInstruction
239 init from
( src
) do super
241 class ADecoInstruction
242 super AOutputInstruction
243 init from
( src
) do super
246 class AStroInstruction
247 super AOutputInstruction
248 init from
( src
) do super
251 class AChariInstruction
252 super AInputInstruction
253 init from
( src
) do super
255 class ACharoInstruction
256 super AOutputInstruction
257 init from
( src
) do super
261 abstract class AStackInstruction
262 super ARichBinaryInstruction
263 init from
( src
) do super
265 class AAddspInstruction
266 super AStackInstruction
267 init from
( src
) do super
269 class ASubspInstruction
270 super AStackInstruction
271 init from
( src
) do super
275 class AStopInstruction
276 super ARichUnaryInstruction
277 init from
( src
) do super
280 class ARettrInstruction
281 super ARichUnaryInstruction
282 init from
( src
) do super
285 abstract class AMovInstruction
286 super ARichUnaryInstruction
287 init from
( src
) do super
289 class AMovspaInstruction
290 super AMovInstruction
291 init from
( src
) do super
293 class AMovflgaInstruction
294 super AMovInstruction
295 init from
( src
) do super
299 class ANotInstruction
300 super ARichUnaryInstruction
301 init from
( src
) do super
304 class ANegInstruction
305 super ARichUnaryInstruction
306 init from
( src
) do super
309 abstract class AShiftInstruction
310 super ARichUnaryInstruction
311 init from
( src
) do super
313 class AAslInstruction
314 super AShiftInstruction
315 init from
( src
) do super
317 class AAsrInstruction
318 super AShiftInstruction
319 init from
( src
) do super
321 class ARolInstruction
322 super AShiftInstruction
323 init from
( src
) do super
325 class ARorInstruction
326 super AShiftInstruction
327 init from
( src
) do super
330 class ARetInstruction
331 super ARichUnaryInstruction
332 init from
( src
) do super
336 # TODO find a better name
337 abstract class AArithmeticInstruction
338 super ARichBinaryInstruction
339 init from
( src
) do super
341 class AAddInstruction
342 super AArithmeticInstruction
343 init from
( src
) do super
345 class ASubInstruction
346 super AArithmeticInstruction
347 init from
( src
) do super
349 class AAndInstruction
350 super AArithmeticInstruction
351 init from
( src
) do super
354 super AArithmeticInstruction
355 init from
( src
) do super
359 super ARichBinaryInstruction
360 init from
( src
) do super
364 abstract class ALoadInstruction
365 super ARichBinaryInstruction
366 init from
( src
) do super
369 super ALoadInstruction
370 init from
( src
) do super
372 class ALdbyteInstruction
373 super ALoadInstruction
374 init from
( src
) do super
377 abstract class AStoreInstruction
378 super ARichBinaryInstruction
379 init from
( src
) do super
382 super AStoreInstruction
383 init from
( src
) do super
385 class AStbyteInstruction
386 super AStoreInstruction
387 init from
( src
) do super