Debian Codegraph @

src/macro.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 // Macro stuff
6 #include "macro.h"
7 #include <string.h> // needs strlen() + memcpy()
8 #include "config.h"
9 #include "platform.h"
10 #include "alu.h"
11 #include "dynabuf.h"
12 #include "global.h"
13 #include "input.h"
14 #include "section.h"
15 #include "symbol.h"
16 #include "tree.h"
17
18
19 // Constants
20 #define NAME_INITIALSIZE 128
21 #define ARG_SEPARATOR ' ' // separates macro title from arg types
22 #define ARGTYPE_VALUE 'v'
23 #define ARGTYPE_REF 'r'
24 #define REFERENCE_CHAR '~' // prefix for call-by-reference
25 #define HALF_INITIAL_ARG_TABLE_SIZE 4
26
27
28 // macro struct type definition
29 struct macro {
30 struct location definition; // for "macro twice" error
31 char *original_name, // as section title in error msgs
32 *parameter_list; // parameters (whole line)
33 struct block body; // RAM block containing macro body
34 };
35 // there's no need to make this a struct and add a type component:
36 // when the macro has been found, accessing its parameter_list component
37 // gives us the possibility to find out which args are call-by-value and
38 // which ones are call-by-reference.
39 union macro_arg_t {
40 struct object result; // value and flags (call by value)
41 struct symbol *symbol; // pointer to symbol struct (call by reference)
42 };
43
44
45 // Variables
46 static STRUCT_DYNABUF_REF(user_macro_name, NAME_INITIALSIZE); // original macro title
47 static STRUCT_DYNABUF_REF(internal_name, NAME_INITIALSIZE); // plus param type chars
48 static struct rwnode *macro_forest[256]; // trees (because of 8b hash)
49 // Dynamic argument table
50 static union macro_arg_t *arg_table = NULL;
51 static int argtable_size = HALF_INITIAL_ARG_TABLE_SIZE;
52
53
54 // Functions
55
56 // Enlarge the argument table
57 static void enlarge_arg_table(void)
58 {
59 argtable_size *= 2;
60 //printf("Doubling arg table size to %d.\n", argtable_size);
61 arg_table = realloc(arg_table, argtable_size * sizeof(*arg_table));
62 if (arg_table == NULL)
63 throw_serious_error(exception_no_memory_left);
64 }
65
66 // Read macro scope and title. Title is read to GlobalDynaBuf and then copied
67 // over to internal_name DynaBuf, where ARG_SEPARATOR is added.
68 // In user_macro_name DynaBuf, the original name is kept so a copy can be
69 // linked to the resulting macro struct.
70 static scope_t get_scope_and_title(void)
71 {
72 scope_t macro_scope;
73
74 input_read_scope_and_symbol_name(&macro_scope); // skips spaces before
75 // now GotByte = illegal character after title
76 // copy macro title to private dynabuf and add separator character
77 dynabuf_clear(user_macro_name);
78 dynabuf_add_string(user_macro_name, GLOBALDYNABUF_CURRENT);
79 dynabuf_append(user_macro_name, '\0'); // make sure terminator is part of buffer so get_copy() includes it!
80 dynabuf_clear(internal_name);
81 dynabuf_add_string(internal_name, GLOBALDYNABUF_CURRENT);
82 dynabuf_append(internal_name, ARG_SEPARATOR);
83 SKIPSPACE(); // done here once so it's not necessary at two callers
84 return macro_scope;
85 }
86
87 // Check for comma. If there, append to GlobalDynaBuf.
88 static int pipe_comma(void)
89 {
90 int result;
91
92 result = parser_accept_comma();
93 if (result)
94 DYNABUF_APPEND(GlobalDynaBuf, ',');
95 return result;
96 }
97
98 // This function is called from both macro definition and macro call.
99 // Terminate macro name and copy from internal_name to GlobalDynaBuf
100 // (because that's where tree_hard_scan() looks for the search string).
101 // Then try to find macro and return whether it was created.
102 static int search_for_macro(struct rwnode **result, scope_t scope, int create)
103 {
104 dynabuf_append(internal_name, '\0'); // terminate macro name
105 // now internal_name = macro_title SPC argument_specifiers NUL
106 dynabuf_clear(GlobalDynaBuf);
107 dynabuf_add_string(GlobalDynaBuf, internal_name->buffer);
108 dynabuf_append(GlobalDynaBuf, '\0');
109 return tree_hard_scan(result, macro_forest, scope, create);
110 }
111
112 // This function is only called during the first pass, so there's no need to
113 // check whether to skip the definition or not.
114 // Return with GotByte = '}'
115 void macro_parse_definition(void) // Now GotByte = illegal char after "!macro"
116 {
117 char *formal_parameters;
118 struct rwnode *macro_node;
119 struct macro *original_macro; // for "macro twice" error
120 struct macro *new_macro;
121 scope_t macro_scope = get_scope_and_title();
122
123 // now GotByte = first non-space after title
124 dynabuf_clear(GlobalDynaBuf); // prepare to hold formal parameters
125 // GlobalDynaBuf = "" (will hold formal parameter list)
126 // user_macro_name = MacroTitle NUL
127 // internal_name = MacroTitle ARG_SEPARATOR (grows to signature)
128 // Accept n>=0 comma-separated formal parameters before CHAR_SOB ('{').
129 // Valid argument formats are:
130 // .LOCAL_LABEL_BY_VALUE
131 // ~.LOCAL_LABEL_BY_REFERENCE
132 // @CHEAP_LOCAL_LABEL_BY_VALUE
133 // ~@CHEAP_LOCAL_LABEL_BY_REFERENCE
134 // GLOBAL_LABEL_BY_VALUE global args are very uncommon,
135 // ~GLOBAL_LABEL_BY_REFERENCE but not forbidden
136 // now GotByte = non-space
137 if (GotByte != CHAR_SOB) { // any at all?
138 do {
139 // handle call-by-reference character ('~')
140 if (GotByte != REFERENCE_CHAR) {
141 dynabuf_append(internal_name, ARGTYPE_VALUE);
142 } else {
143 dynabuf_append(internal_name, ARGTYPE_REF);
144 dynabuf_append(GlobalDynaBuf, REFERENCE_CHAR);
145 GetByte();
146 }
147 // handle symbol name (including '.'/'@' prefix)
148 input_append_symbol_name_to_global_dynabuf();
149 } while (pipe_comma());
150 // ensure CHAR_SOB ('{')
151 if (GotByte != CHAR_SOB)
152 throw_serious_error(exception_no_left_brace);
153 }
154 dynabuf_append(GlobalDynaBuf, CHAR_EOS); // terminate param list
155 // now GlobalDynaBuf = comma-separated parameter list without spaces,
156 // but terminated with CHAR_EOS.
157 formal_parameters = dynabuf_get_copy(GlobalDynaBuf);
158 // now GlobalDynaBuf = unused
159 // Reading the macro body would change the line number. To have correct
160 // error messages, we're checking for "macro twice" *now*.
161 // Search for macro. Create if not found.
162 // But if found, complain (macro twice).
163 if (search_for_macro(&macro_node, macro_scope, TRUE) == FALSE) {
164 original_macro = macro_node->body;
165 throw_redef_error("Macro already defined.", &(original_macro->definition), "Initial definition.");
166 }
167 // Create new macro struct and set it up. Finally we'll read the body.
168 new_macro = safe_malloc(sizeof(*new_macro));
169 new_macro->original_name = dynabuf_get_copy(user_macro_name);
170 new_macro->parameter_list = formal_parameters;
171 input_get_location(&new_macro->definition); // includes line number
172 input_block_getcopy(&new_macro->body); // also includes line number (and then changes it)
173 macro_node->body = new_macro; // link macro struct to tree node
174 // and that about sums it up
175 }
176
177 // Parse macro call ("+MACROTITLE"). Has to be re-entrant.
178 // TODO: split this into smaller functions, some of the inner blocks are much too long
179 void macro_parse_call(void) // Now GotByte = first char of macro name
180 {
181 struct symbol *symbol;
182 struct section new_section,
183 *outer_section;
184 struct inputchange_buf icb;
185 struct macro *actual_macro;
186 struct rwnode *macro_node,
187 *symbol_node;
188 scope_t macro_scope,
189 symbol_scope;
190 int arg_count = 0;
191 int outer_msg_sum;
192 boolean break_cont_allowed;
193 boolean return_allowed;
194
195 // make sure arg_table is ready (if not yet initialised, do it now)
196 if (arg_table == NULL)
197 enlarge_arg_table();
198
199 // Enter deeper nesting level
200 // Quit program if recursion too deep.
201 if (--sanity.macro_recursions_left < 0)
202 throw_serious_error("Too deeply nested. Recursive macro calls?");
203 macro_scope = get_scope_and_title();
204 // now GotByte = first non-space after title
205 // internal_name = MacroTitle ARG_SEPARATOR (grows to signature)
206 // Accept n>=0 comma-separated arguments before CHAR_EOS.
207 // Valid argument formats are:
208 // ~SYMBOL call by ref
209 // EXPRESSION call by value (everything that does NOT start with '~')
210 // now GotByte = non-space
211 if (GotByte != CHAR_EOS) { // any at all?
212 do {
213 // if arg table cannot take another element, enlarge
214 if (argtable_size <= arg_count)
215 enlarge_arg_table();
216 // Decide whether call-by-reference or call-by-value
217 // In both cases, GlobalDynaBuf may be used.
218 if (GotByte == REFERENCE_CHAR) {
219 // read call-by-reference arg
220 dynabuf_append(internal_name, ARGTYPE_REF);
221 GetByte(); // eat '~'
222 input_read_scope_and_symbol_name(&symbol_scope);
223 // GotByte = illegal char
224 arg_table[arg_count].symbol = symbol_find(symbol_scope); // CAUTION, object type may be NULL!
225 } else {
226 // read call-by-value arg
227 dynabuf_append(internal_name, ARGTYPE_VALUE);
228 ALU_any_result(&(arg_table[arg_count].result));
229 }
230 ++arg_count;
231 } while (parser_accept_comma());
232 }
233 // now arg_table contains the arguments
234 // now GlobalDynaBuf = unused
235 // check for "unknown macro"
236 // Search for macro. Do not create if not found.
237 search_for_macro(&macro_node, macro_scope, FALSE);
238 if (macro_node == NULL) {
239 throw_error("Macro not defined (or wrong signature).");
240 parser_skip_remainder();
241 } else {
242 // make macro_node point to the macro struct
243 actual_macro = macro_node->body;
244
245 // remember input and set up new one:
246 inputchange_new_ram(&icb);
247 inputchange_macro1_params(&actual_macro->definition, actual_macro->parameter_list);
248
249 outer_msg_sum = pass.counters.warnings + pass.counters.errors; // remember for call stack decision
250
251 // remember old section
252 outer_section = section_now;
253 // start new section (with new scope)
254 // FALSE = title mustn't be freed
255 section_new(&new_section, "Macro", actual_macro->original_name, FALSE);
256 section_new_cheap_scope(&new_section);
257
258 // assign arguments
259 GetByte(); // fetch first byte of parameter list
260 if (GotByte != CHAR_EOS) { // any at all?
261 arg_count = 0;
262 do {
263 // Decide whether call-by-reference
264 // or call-by-value
265 // In both cases, GlobalDynaBuf may be used.
266 if (GotByte == REFERENCE_CHAR) {
267 // assign call-by-reference arg
268 GetByte(); // eat '~'
269 input_read_scope_and_symbol_name(&symbol_scope);
270 // create new tree node and link existing symbol struct from arg list to it
271 if (tree_hard_scan(&symbol_node, symbols_forest, symbol_scope, TRUE) == FALSE) {
272 // we expect it to exist in later passes, but in pass 1 it's an error:
273 if (pass.number == 1)
274 throw_error("Macro parameter twice.");
275 }
276 symbol_node->body = arg_table[arg_count].symbol; // CAUTION, object type may be NULL
277 } else {
278 // assign call-by-value arg
279 input_read_scope_and_symbol_name(&symbol_scope);
280 symbol = symbol_find(symbol_scope);
281 // FIXME - find out if symbol was just created.
282 // Then check for the same error message here as above ("Macro parameter twice.").
283 // TODO - on the other hand, this would rule out globals as args (stupid anyway, but not illegal yet!)
284 symbol->object = arg_table[arg_count].result; // FIXME - this assignment redefines globals/whatever without throwing errors!
285 }
286 ++arg_count;
287 } while (parser_accept_comma());
288 }
289
290 // remember whether break/continue/return are allowed and set new states
291 break_cont_allowed = parser_allow_break_cont(FALSE); // forbid !break/!continue
292 return_allowed = parser_allow_return(TRUE); // allow !return
293
294 // and now, finally, parse the actual macro body
295 // maybe call parse_ram_block(actual_macro->definition.line_number, actual_macro->body)
296 inputchange_macro2_body(actual_macro->body.body);
297 parse_until_eob_or_eof();
298 if (GotByte != CHAR_EOB)
299 BUG("IllegalBlockTerminator", GotByte);
300 // was there a "!return"?
301 if (parser_get_shortcut() == SHORTCUT_RETURN)
302 parser_set_shortcut(SHORTCUT_NONE);
303
304 // restore states of break/continue/return
305 parser_allow_return(return_allowed);
306 parser_allow_break_cont(break_cont_allowed);
307
308 // end section (free title memory, if needed)
309 section_finalize(&new_section);
310 // restore previous section
311 section_now = outer_section;
312
313 // restore outer input
314 inputchange_back(&icb);
315
316 // if needed, dump call stack
317 if (outer_msg_sum != pass.counters.warnings + pass.counters.errors)
318 throw_message(DEBUGLEVEL_INFO, "...called from here.", NULL);
319
320 parser_ensure_EOS();
321 }
322 ++sanity.macro_recursions_left; // leave this nesting level
323 }