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
, stream
)
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
149 _location
= src
.location
153 abstract class ARichUnaryInstruction
154 super AUnaryInstruction
156 init from
( src
: AUnaryInstruction ) do
161 _location
= src
.location
168 abstract class ABranchInstruction
169 super ARichBinaryInstruction
170 init from
( src
) do super
174 super ABranchInstruction
175 init from
( src
) do super
177 class ABrleInstruction
178 super ABranchInstruction
179 init from
( src
) do super
181 class ABrltInstruction
182 super ABranchInstruction
183 init from
( src
) do super
185 class ABreqInstruction
186 super ABranchInstruction
187 init from
( src
) do super
189 class ABrneInstruction
190 super ABranchInstruction
191 init from
( src
) do super
193 class ABrgeInstruction
194 super ABranchInstruction
195 init from
( src
) do super
197 class ABrgtInstruction
198 super ABranchInstruction
199 init from
( src
) do super
201 class ABrvInstruction
202 super ABranchInstruction
203 init from
( src
) do super
205 class ABrcInstruction
206 super ABranchInstruction
207 init from
( src
) do super
209 class ACallInstruction
210 super ABranchInstruction
211 init from
( src
) do super
215 abstract class ANopInstruction
218 class ABinaryNopInstruction
219 super ANopInstruction
220 super ARichBinaryInstruction
221 init from
( src
) do super
223 class AUnaryNopInstruction
224 super ANopInstruction
225 super ARichUnaryInstruction
226 init from
( src
) do super
230 abstract class AOutputInstruction
231 super ARichBinaryInstruction
232 init from
( src
) do super
234 abstract class AInputInstruction
235 super ARichBinaryInstruction
236 init from
( src
) do super
238 # TODO add category for ADecInstruction?
239 class ADeciInstruction
240 super AInputInstruction
241 init from
( src
) do super
243 class ADecoInstruction
244 super AOutputInstruction
245 init from
( src
) do super
248 class AStroInstruction
249 super AOutputInstruction
250 init from
( src
) do super
253 class AChariInstruction
254 super AInputInstruction
255 init from
( src
) do super
257 class ACharoInstruction
258 super AOutputInstruction
259 init from
( src
) do super
263 abstract class AStackInstruction
264 super ARichBinaryInstruction
265 init from
( src
) do super
267 class AAddspInstruction
268 super AStackInstruction
269 init from
( src
) do super
271 class ASubspInstruction
272 super AStackInstruction
273 init from
( src
) do super
277 class AStopInstruction
278 super ARichUnaryInstruction
279 init from
( src
) do super
282 class ARettrInstruction
283 super ARichUnaryInstruction
284 init from
( src
) do super
287 abstract class AMovInstruction
288 super ARichUnaryInstruction
289 init from
( src
) do super
291 class AMovspaInstruction
292 super AMovInstruction
293 init from
( src
) do super
295 class AMovflgaInstruction
296 super AMovInstruction
297 init from
( src
) do super
301 class ANotInstruction
302 super ARichUnaryInstruction
303 init from
( src
) do super
306 class ANegInstruction
307 super ARichUnaryInstruction
308 init from
( src
) do super
311 abstract class AShiftInstruction
312 super ARichUnaryInstruction
313 init from
( src
) do super
315 class AAslInstruction
316 super AShiftInstruction
317 init from
( src
) do super
319 class AAsrInstruction
320 super AShiftInstruction
321 init from
( src
) do super
323 class ARolInstruction
324 super AShiftInstruction
325 init from
( src
) do super
327 class ARorInstruction
328 super AShiftInstruction
329 init from
( src
) do super
332 class ARetInstruction
333 super ARichUnaryInstruction
334 init from
( src
) do super
338 # TODO find a better name
339 abstract class AArithmeticInstruction
340 super ARichBinaryInstruction
341 init from
( src
) do super
343 class AAddInstruction
344 super AArithmeticInstruction
345 init from
( src
) do super
347 class ASubInstruction
348 super AArithmeticInstruction
349 init from
( src
) do super
351 class AAndInstruction
352 super AArithmeticInstruction
353 init from
( src
) do super
356 super AArithmeticInstruction
357 init from
( src
) do super
361 super ARichBinaryInstruction
362 init from
( src
) do super
366 abstract class ALoadInstruction
367 super ARichBinaryInstruction
368 init from
( src
) do super
371 super ALoadInstruction
372 init from
( src
) do super
374 class ALdbyteInstruction
375 super ALoadInstruction
376 init from
( src
) do super
379 abstract class AStoreInstruction
380 super ARichBinaryInstruction
381 init from
( src
) do super
384 super AStoreInstruction
385 init from
( src
) do super
387 class AStbyteInstruction
388 super AStoreInstruction
389 init from
( src
) do super