Debian Codegraph @

src/flow.c

raw symbols SCIP
1 // ACME - a crossassembler for producing 6502/65c02/65816/65ce02 code.
2 // Copyright (C) 1998-2025 Marco Baye
3 // Have a look at "acme.c" for further info
4 //
5 // Flow control stuff (loops, conditional assembly etc.)
6 //
7 // Macros, conditional assembly, loops and sourcefile-includes are all based on
8 // parsing blocks of code. When defining macros or using loops or conditional
9 // assembly, the block starts with "{" and ends with "}". In the case of
10 // "!source", the given file is treated like a block - the outermost assembler
11 // function uses the same technique to parse the top level file.
12 //
13 // 24 Nov 2007 Added "!ifndef"
14 #include "flow.h"
15 #include <string.h>
16 #include "alu.h"
17 #include "config.h"
18 #include "dynabuf.h"
19 #include "global.h"
20 #include "input.h"
21 #include "mnemo.h"
22 #include "symbol.h"
23 #include "tree.h"
24
25
26 // helper functions for if/ifdef/ifndef/else/for/do/while
27
28
29 // parse symbol name and return if symbol has defined value (called by ifdef/ifndef)
30 boolean check_ifdef_condition(void)
31 {
32 scope_t scope;
33 struct rwnode *node;
34 struct symbol *symbol;
35
36 // read symbol name
37 if (input_read_scope_and_symbol_name(&scope)) // skips spaces before
38 return FALSE; // there was an error, it has been reported, so return value is more or less meaningless anway
39
40 // look for it
41 tree_hard_scan(&node, symbols_forest, scope, FALSE);
42 if (!node)
43 return FALSE; // not found -> no, not defined
44
45 symbol = (struct symbol *) node->body;
46 symbol->has_been_read = TRUE; // we did not really read the symbol's value, but checking for its existence still counts as "used it"
47 if (symbol->object.type == NULL)
48 BUG("ObjectHasNullType", 0);
49 return symbol->object.type->is_defined(&symbol->object);
50 }
51
52
53 // parse a loop body (TODO - also use for macro body?)
54 static void parse_ram_block(struct block *block)
55 {
56 // set line number to loop start
57 // set RAM read pointer to loop
58 inputchange_set_ram(block->line_number, block->body);
59 // parse block
60 parse_until_eob_or_eof();
61 if (GotByte != CHAR_EOB)
62 BUG("IllegalBlockTerminator", GotByte);
63 }
64
65
66 // function for "!for" with counter variable
67 static void counting_for(struct for_loop *loop)
68 {
69 struct object loop_var;
70 enum shortcut shortcut;
71
72 // init counter
73 loop_var.type = &type_number;
74 loop_var.u.number.ntype = NUMTYPE_INT;
75 loop_var.u.number.flags = 0;
76 loop_var.u.number.val.intval = 0; // SEE BELOW - default value if old algo skips loop entirely
77 loop_var.u.number.addr_refs = loop->u.counter.addr_refs;
78 // CAUTION: next line does not have power to change symbol type, but if
79 // "symbol already defined" error is thrown, the type will still have
80 // been changed. this was done so the code below has a counter var.
81 symbol_set_object(loop->symbol, &loop_var, POWER_CHANGE_VALUE);
82 // TODO: in versions before 0.97, force bit handling was broken
83 // in both "!set" and "!for":
84 // trying to change a force bit correctly raised an error, but
85 // in any case, ALL FORCE BITS WERE CLEARED in symbol. only
86 // cases like !set N=N+1 worked, because the force bit was
87 // taken from result.
88 // maybe support this behaviour via --dialect?
89 if (loop->u.counter.force_bit)
90 symbol_set_force_bit(loop->symbol, loop->u.counter.force_bit);
91 loop_var = loop->symbol->object; // update local copy with force bit
92 loop->symbol->has_been_read = TRUE; // lock force bit
93 loop_var.u.number.val.intval = loop->u.counter.first; // SEE ABOVE - this may be nonzero, but has not yet been copied to user symbol!
94 while (loop->iterations_left) {
95 loop->symbol->object = loop_var; // overwrite whole struct, in case some joker has re-assigned loop counter var
96 parse_ram_block(&loop->block);
97 loop_var.u.number.val.intval += loop->u.counter.increment;
98 loop->iterations_left--;
99
100 // was there a "!break" or "!continue"?
101 shortcut = parser_get_shortcut();
102 if (shortcut == SHORTCUT_BREAK) {
103 parser_set_shortcut(SHORTCUT_NONE);
104 break;
105 } else if (shortcut == SHORTCUT_CONT) {
106 parser_set_shortcut(SHORTCUT_NONE);
107 }
108 }
109 // new algo wants illegal value in loop counter after block:
110 if (loop->algorithm == FORALGO_NEWCOUNT)
111 loop->symbol->object = loop_var; // overwrite whole struct, in case some joker has re-assigned loop counter var
112 }
113
114 // function for "!for" with iterating variable
115 static void iterating_for(struct for_loop *loop)
116 {
117 intval_t index = 0;
118 struct object obj;
119 enum shortcut shortcut;
120
121 while (loop->iterations_left) {
122 loop->u.iter.obj.type->at(&loop->u.iter.obj, &obj, index++);
123 symbol_set_object(loop->symbol, &obj, POWER_CHANGE_VALUE | POWER_CHANGE_OBJTYPE);
124 parse_ram_block(&loop->block);
125 loop->iterations_left--;
126
127 // was there a "!break" or "!continue"?
128 shortcut = parser_get_shortcut();
129 if (shortcut == SHORTCUT_BREAK) {
130 parser_set_shortcut(SHORTCUT_NONE);
131 break;
132 } else if (shortcut == SHORTCUT_CONT) {
133 parser_set_shortcut(SHORTCUT_NONE);
134 }
135 }
136 }
137
138
139 // back end function for "!for" pseudo opcode, called with GotByte = '}'
140 void flow_forloop(struct for_loop *loop)
141 {
142 struct inputchange_buf icb;
143 boolean break_cont_buf;
144
145 // remember input and set up new one:
146 inputchange_new_ram(&icb);
147 // fix line number (not for block, but in case symbol handling throws errors)
148 inputchange_set_ram(loop->block.line_number, NULL);
149 // remember break/cont state and allow:
150 break_cont_buf = parser_allow_break_cont(TRUE);
151
152 switch (loop->algorithm) {
153 case FORALGO_OLDCOUNT:
154 case FORALGO_NEWCOUNT:
155 counting_for(loop);
156 break;
157 case FORALGO_ITERATE:
158 iterating_for(loop);
159 break;
160 default:
161 BUG("IllegalLoopAlgo", loop->algorithm);
162 }
163
164 // restore outer break/cont state:
165 parser_allow_break_cont(break_cont_buf);
166 // restore outer input
167 inputchange_back(&icb);
168 }
169
170
171 // read condition, make copy, link to struct
172 // FIXME - remove!
173 static void copy_condition(struct condition *condition, char terminator)
174 {
175 input_read_statement(terminator);
176 condition->block.body = dynabuf_get_copy(GlobalDynaBuf);
177 }
178
179 // try to read a condition into DynaBuf and store pointer to copy in
180 // given loop_condition structure.
181 // if no condition given, NULL is written to structure.
182 // call with GotByte = first interesting character
183 void flow_store_doloop_condition(struct condition *condition, char terminator)
184 {
185 struct location loc;
186
187 // write line number
188 input_get_location(&loc); // FIXME - get rid of this when changing copy_condition to input_line_getcopy!
189 condition->block.line_number = loc.line_number;
190 // set defaults
191 condition->invert = FALSE;
192 condition->block.body = NULL;
193 // check for empty condition
194 if (GotByte == terminator)
195 return;
196
197 // seems as if there really *is* a condition, so check for until/while
198 if (parser_read_and_lower_keyword()) {
199 if (strcmp(GlobalDynaBuf->buffer, "while") == 0) {
200 //condition.invert = FALSE;
201 } else if (strcmp(GlobalDynaBuf->buffer, "until") == 0) {
202 condition->invert = TRUE;
203 } else {
204 throw_error("Expected WHILE or UNTIL keyword, or an empty loop condition.");
205 return;
206 }
207 // write given condition into buffer
208 copy_condition(condition, terminator);
209 }
210 }
211
212
213 // read a condition into DynaBuf and store pointer to copy in
214 // given loop_condition structure.
215 // call with GotByte = first interesting character
216 void flow_store_while_condition(struct condition *condition)
217 {
218 struct location loc;
219
220 input_get_location(&loc); // FIXME - get rid of this when changing copy_condition to input_line_getcopy!
221 condition->block.line_number = loc.line_number;
222 condition->invert = FALSE;
223 copy_condition(condition, CHAR_SOB);
224 }
225
226
227 // check a condition expression
228 static boolean check_condition(struct condition *condition)
229 {
230 struct number intresult;
231
232 // first, check whether there actually *is* a condition
233 if (condition->block.body == NULL)
234 return TRUE; // non-existing conditions are always true
235
236 // set up input for expression evaluation
237 inputchange_set_ram(condition->block.line_number, condition->block.body); // FIXME - just pass condition->block!)
238
239 GetByte(); // proceed with next char
240 ALU_defined_int(&intresult);
241 if (GotByte)
242 throw_serious_error("Unexpected char when evaluating loop condition."); // FIXME - include that char in the error message!
243 return condition->invert ? !intresult.val.intval : !!intresult.val.intval;
244 }
245
246
247 // back end function for "!do" and "!while" pseudo opcodes
248 void flow_do_while(struct do_while *loop)
249 {
250 struct inputchange_buf icb;
251 boolean break_cont_buf;
252 enum shortcut shortcut;
253
254 // remember input and prepare new one:
255 inputchange_new_ram(&icb);
256 // remember break/cont state and allow:
257 break_cont_buf = parser_allow_break_cont(TRUE);
258
259 for (;;) {
260 // check head condition
261 if (!check_condition(&loop->head_cond))
262 break;
263
264 parse_ram_block(&loop->block);
265
266 // was there a "!break" or "!continue"?
267 shortcut = parser_get_shortcut();
268 if (shortcut == SHORTCUT_BREAK) {
269 parser_set_shortcut(SHORTCUT_NONE);
270 break;
271 } else if (shortcut == SHORTCUT_CONT) {
272 parser_set_shortcut(SHORTCUT_NONE);
273 }
274
275 // check tail condition
276 if (!check_condition(&loop->tail_cond))
277 break;
278 }
279
280 // restore outer break/cont state:
281 parser_allow_break_cont(break_cont_buf);
282 // restore outer input
283 inputchange_back(&icb);
284 }