Debian Codegraph @

src/asm/flow.c

raw symbols SCIP
1 //
2 // ACME - a crossassembler for producing 6502/65c02/65816 code.
3 // Copyright (C) 1998-2006 Marco Baye
4 // Have a look at "acme.c" for further info
5 //
6 // Flow control stuff (loops, conditional assembly etc.)
7 //
8 // Macros, conditional assembly, loops and sourcefile-includes are all based on
9 // parsing blocks of code. When defining macros or using loops or conditional
10 // assembly, the block starts with "{" and ends with "}". In the case of
11 // "!source", the given file is treated like a block - the outermost assembler
12 // function uses the same technique to parse the top level file.
13
14 #include <string.h>
15 #include "acme.h"
16 #include "alu.h"
17 #include "config.h"
18 #include "dynabuf.h"
19 #include "global.h"
20 #include "input.h"
21 #include "label.h"
22 #include "macro.h"
23 #include "mnemo.h"
24 #include "tree.h"
25
26
27 // type definitions
28 enum cond_key_t {
29 ID_UNTIL, // Handles to store instead of
30 ID_WHILE, // the UNTIL and WHILE keywords
31 };
32 typedef struct {
33 enum cond_key_t type; // either ID_UNTIL or ID_WHILE
34 int line; // original line number
35 char* body; // pointer to actual expression
36 } loopcond_t;
37
38
39 // Variables
40
41 // predefined stuff
42 static node_t* condkey_tree = NULL;// tree to hold UNTIL and WHILE
43 static node_t condkeys[] = {
44 PREDEFNODE("until", ID_UNTIL),
45 PREDEFLAST("while", ID_WHILE),
46 // ^^^^ this marks the last element
47 };
48
49
50 // Helper functions for "!for" and "!do"
51
52 // Parse a loop body (could also be used for macro body)
53 static void parse_ram_block(int line_number, char* body) {
54 Input_now->line_number = line_number;// set line number to loop start
55 Input_now->src.ram_ptr = body;// set RAM read pointer to loop
56 // Parse loop body
57 Parse_until_eob_or_eof();
58 if(GotByte != CHAR_EOB)
59 Bug_found("IllegalBlockTerminator", GotByte);
60 }
61
62 // Try to read a condition into DynaBuf and store copy pointer in
63 // given loopcond_t structure.
64 // If no condition given, NULL is written to structure.
65 // Call with GotByte = first interesting character
66 static void store_condition(loopcond_t* condition, char terminator) {
67 void* node_body;
68
69 // write line number
70 condition->line = Input_now->line_number;
71 // Check for empty condition
72 if(GotByte == terminator) {
73 // Write NULL condition, then return
74 condition->body = NULL;
75 return;
76 }
77 // Seems as if there really *is* a condition.
78 // Read UNTIL/WHILE keyword
79 if(Input_read_and_lower_keyword()) {
80 // Search for new tree item
81 if(!Tree_easy_scan(condkey_tree, &node_body, GlobalDynaBuf)) {
82 Throw_error(exception_syntax);
83 condition->body = NULL;
84 return;
85 }
86 condition->type = (enum cond_key_t) node_body;
87 // Write given condition into buffer
88 SKIPSPACE();
89 DYNABUF_CLEAR(GlobalDynaBuf);
90 Input_until_terminator(terminator);
91 DynaBuf_append(GlobalDynaBuf, CHAR_EOS);// ensure terminator
92 condition->body = DynaBuf_get_copy(GlobalDynaBuf);
93 }
94 return;
95 }
96
97 // Check a condition expression
98 static bool check_condition(loopcond_t* condition) {
99 intval_t expression;
100
101 // First, check whether there actually *is* a condition
102 if(condition->body == NULL)
103 return(TRUE); // non-existant conditions are always true
104 // set up input for expression evaluation
105 Input_now->line_number = condition->line;
106 Input_now->src.ram_ptr = condition->body;
107 GetByte(); // proceed with next char
108 expression = ALU_defined_int();
109 if(GotByte)
110 Throw_serious_error(exception_syntax);
111 if(condition->type == ID_UNTIL)
112 return(!expression);
113 return(!!expression);
114 }
115
116 // Looping assembly ("!do"). Has to be re-entrant.
117 static enum eos_t PO_do(void) { // Now GotByte = illegal char
118 loopcond_t condition1,
119 condition2;
120 input_t loop_input,
121 *outer_input;
122 char* loop_body;
123 bool go_on;
124 int loop_start;// line number of loop pseudo opcode
125
126 // Read head condition to buffer
127 SKIPSPACE();
128 store_condition(&condition1, CHAR_SOB);
129 if(GotByte != CHAR_SOB)
130 Throw_serious_error(exception_no_left_brace);
131 // Remember line number of loop body,
132 // then read block and get copy
133 loop_start = Input_now->line_number;
134 loop_body = Input_skip_or_store_block(TRUE); // changes line number!
135 // now GotByte = '}'
136 NEXTANDSKIPSPACE();// Now GotByte = first non-blank char after block
137 // Read tail condition to buffer
138 store_condition(&condition2, CHAR_EOS);
139 // now GotByte = CHAR_EOS
140 // set up new input
141 loop_input = *Input_now;// copy current input structure into new
142 loop_input.source_is_ram = TRUE; // set new byte source
143 // remember old input
144 outer_input = Input_now;
145 // activate new input (not useable yet, as pointer and
146 // line number are not yet set up)
147 Input_now = &loop_input;
148 do {
149 // Check head condition
150 go_on = check_condition(&condition1);
151 if(go_on) {
152 parse_ram_block(loop_start, loop_body);
153 // Check tail condition
154 go_on = check_condition(&condition2);
155 }
156 } while(go_on);
157 // Free memory
158 free(condition1.body);
159 free(loop_body);
160 free(condition2.body);
161 // restore previous input:
162 Input_now = outer_input;
163 GotByte = CHAR_EOS; // CAUTION! Very ugly kluge.
164 // But by switching input, we lost the outer input's GotByte. We know
165 // it was CHAR_EOS. We could just call GetByte() to get real input, but
166 // then the main loop could choke on unexpected bytes. So we pretend
167 // that we got the outer input's GotByte value magically back.
168 return(AT_EOS_ANYWAY);
169 }
170
171 // Looping assembly ("!for"). Has to be re-entrant.
172 static enum eos_t PO_for(void) {// Now GotByte = illegal char
173 input_t loop_input,
174 *outer_input;
175 result_t loop_counter;
176 intval_t maximum;
177 char* loop_body;// pointer to loop's body block
178 label_t* label;
179 zone_t zone;
180 int force_bit,
181 loop_start;// line number of "!for" pseudo opcode
182
183 if(Input_read_zone_and_keyword(&zone) == 0) // skips spaces before
184 return(SKIP_REMAINDER);
185 // Now GotByte = illegal char
186 force_bit = Input_get_force_bit(); // skips spaces after
187 label = Label_find(zone, force_bit);
188 if(Input_accept_comma() == FALSE) {
189 Throw_error(exception_syntax);
190 return(SKIP_REMAINDER);
191 }
192 maximum = ALU_defined_int();
193 if(maximum < 0)
194 Throw_serious_error("Loop count is negative.");
195 if(GotByte != CHAR_SOB)
196 Throw_serious_error(exception_no_left_brace);
197 // remember line number of loop pseudo opcode
198 loop_start = Input_now->line_number;
199 // read loop body into DynaBuf and get copy
200 loop_body = Input_skip_or_store_block(TRUE); // changes line number!
201 // switching input makes us lose GotByte. But we know it's '}' anyway!
202 // set up new input
203 loop_input = *Input_now;// copy current input structure into new
204 loop_input.source_is_ram = TRUE; // set new byte source
205 // remember old input
206 outer_input = Input_now;
207 // activate new input
208 // (not yet useable; pointer and line number are still missing)
209 Input_now = &loop_input;
210 // init counter
211 loop_counter.flags = MVALUE_DEFINED | MVALUE_EXISTS;
212 loop_counter.val.intval = 0;
213 // if count == 0, skip loop
214 if(maximum) {
215 do {
216 loop_counter.val.intval++;// increment counter
217 Label_set_value(label, &loop_counter, TRUE);
218 parse_ram_block(loop_start, loop_body);
219 } while(loop_counter.val.intval < maximum);
220 } else
221 Label_set_value(label, &loop_counter, TRUE);
222 // Free memory
223 free(loop_body);
224 // restore previous input:
225 Input_now = outer_input;
226 // GotByte of OuterInput would be '}' (if it would still exist)
227 GetByte(); // fetch next byte
228 return(ENSURE_EOS);
229 }
230
231 // Helper functions for "!if" and "!ifdef"
232
233 // Parse or skip a block. Returns whether block's '}' terminator was missing.
234 // Afterwards: GotByte = '}'
235 static bool skip_or_parse_block(bool parse) {
236 if(!parse) {
237 Input_skip_or_store_block(FALSE);
238 return(FALSE);
239 }
240 // if block was correctly terminated, return FALSE
241 Parse_until_eob_or_eof();
242 // if block isn't correctly terminated, complain and exit
243 if(GotByte != CHAR_EOB)
244 Throw_serious_error(exception_no_right_brace);
245 return(FALSE);
246 }
247
248 // Parse {block} [else {block}]
249 static void parse_block_else_block(bool parse_first) {
250 // Parse first block.
251 // If it's not correctly terminated, return immediately (because
252 // in that case, there's no use in checking for an "else" part).
253 if(skip_or_parse_block(parse_first))
254 return;
255 // now GotByte = '}'. Check for "else" part.
256 // If end of statement, return immediately.
257 NEXTANDSKIPSPACE();
258 if(GotByte == CHAR_EOS)
259 return;
260 // read keyword and check whether really "else"
261 if(Input_read_and_lower_keyword()) {
262 if(strcmp(GlobalDynaBuf->buffer, "else"))
263 Throw_error(exception_syntax);
264 else {
265 SKIPSPACE();
266 if(GotByte != CHAR_SOB)
267 Throw_serious_error(exception_no_left_brace);
268 skip_or_parse_block(!parse_first);
269 // now GotByte = '}'
270 GetByte();
271 }
272 }
273 Input_ensure_EOS();
274 }
275
276 // Conditional assembly ("!if"). Has to be re-entrant.
277 static enum eos_t PO_if(void) {// Now GotByte = illegal char
278 intval_t cond;
279
280 cond = ALU_defined_int();
281 if(GotByte != CHAR_SOB)
282 Throw_serious_error(exception_no_left_brace);
283 parse_block_else_block(!!cond);
284 return(ENSURE_EOS);
285 }
286
287 // Conditional assembly ("!ifdef"). Has to be re-entrant.
288 static enum eos_t PO_ifdef(void) {// Now GotByte = illegal char
289 node_ra_t* node;
290 label_t* label;
291 zone_t zone;
292 bool defined = FALSE;
293
294 if(Input_read_zone_and_keyword(&zone) == 0) // skips spaces before
295 return(SKIP_REMAINDER);
296 Tree_hard_scan(&node, Label_forest, zone, FALSE);
297 if(node) {
298 label = (label_t*) node->body;
299 // in first pass, count usage
300 if(pass_count == 0)
301 label->usage++;
302 if(label->result.flags & MVALUE_DEFINED)
303 defined = TRUE;
304 }
305 SKIPSPACE();
306 if(GotByte == CHAR_SOB)
307 parse_block_else_block(defined);
308 else {
309 if(defined)
310 return(PARSE_REMAINDER);
311 return(SKIP_REMAINDER);
312 }
313 return(ENSURE_EOS);
314 }
315
316 // Macro definition ("!macro").
317 static enum eos_t PO_macro(void) {// Now GotByte = illegal char
318 // In first pass, parse. In all other passes, skip.
319 if(pass_count == 0)
320 Macro_parse_definition(); // now GotByte = '}'
321 else {
322 // skip until CHAR_SOB ('{') is found.
323 // no need to check for end-of-statement, because such an
324 // error would already have been detected in first pass.
325 // for the same reason, there is no need to check for quotes.
326 while(GotByte != CHAR_SOB)
327 GetByte();
328 Input_skip_or_store_block(FALSE); // now GotByte = '}'
329 }
330 GetByte(); // Proceed with next character
331 return(ENSURE_EOS);
332 }
333
334 // Parse a whole source code file
335 void Parse_source(char *source) {
336 // be verbose
337 if(Process_verbosity > 2)
338 printf("Parsing source file!\n");
339 // set up new input
340 Input_new_file(source);
341 // Parse block and check end reason
342 Parse_until_eob_or_eof();
343 if(GotByte != CHAR_EOF)
344 Throw_error("Found '}' instead of end-of-file.");
345 // close sublevel src
346 //fclose(Input_now->src.fd);
347 }
348
349 // Include source file ("!source" or "!src"). Has to be re-entrant.
350 static enum eos_t PO_source(void) {// Now GotByte = illegal char
351 Throw_error("PO_SOURCE not implemented");
352 return 0;
353 }
354 /*
355 static enum eos_t X_PO_source(void) {// Now GotByte = illegal char
356 FILE* fd;
357 char local_gotbyte;
358 input_t new_input,
359 *outer_input;
360
361 // Enter new nesting level.
362 // Quit program if recursion too deep.
363 if(--source_recursions_left < 0)
364 Throw_serious_error("Too deeply nested. Recursive \"!source\"?");
365 // Read file name. Quit function on error.
366 if(Input_read_filename(TRUE))
367 return(SKIP_REMAINDER);
368 // If file could be opened, parse it. Otherwise, complain.
369 if((fd = fopen(GLOBALDYNABUF_CURRENT, FILE_READBINARY))) {
370 char filename[GlobalDynaBuf->size];
371
372 strcpy(filename, GLOBALDYNABUF_CURRENT);
373 outer_input = Input_now;// remember old input
374 local_gotbyte = GotByte;// CAUTION - ugly kluge
375 Input_now = &new_input;// activate new input
376 Parse_and_close_file(fd, filename);
377 Input_now = outer_input;// restore previous input
378 GotByte = local_gotbyte;// CAUTION - ugly kluge
379 } else
380 Throw_error(exception_cannot_open_input_file);
381 // Leave nesting level
382 source_recursions_left++;
383 return(ENSURE_EOS);
384 }
385 */
386 // pseudo opcode table
387 static node_t pseudo_opcodes[] = {
388 PREDEFNODE("do", PO_do),
389 PREDEFNODE("for", PO_for),
390 PREDEFNODE("if", PO_if),
391 PREDEFNODE("ifdef", PO_ifdef),
392 PREDEFNODE("macro", PO_macro),
393 PREDEFNODE("source", PO_source),
394 PREDEFLAST("src", PO_source),
395 // ^^^^ this marks the last element
396 };
397
398 // register pseudo opcodes and build keyword tree for until/while
399 void Flow_init(void) {
400 Tree_add_table(&condkey_tree, condkeys);
401 Tree_add_table(&pseudo_opcode_tree, pseudo_opcodes);
402 }