Debian Codegraph @

src/asm/macro.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 // Macro stuff
7
8 #include <string.h> // needs strlen() + memcpy()
9 #include "config.h"
10 #include "platform.h" // done first in case "inline" is redefined
11 #include "acme.h"
12 #include "alu.h"
13 #include "dynabuf.h"
14 #include "global.h"
15 #include "input.h"
16 #include "label.h"
17 #include "section.h"
18 #include "tree.h"
19 #include "macro.h"
20
21
22 // Constants
23 #define MACRONAME_DYNABUF_INITIALSIZE 128
24 #define ARG_SEPARATOR ' ' // separates macro title from arg types
25 #define ARGTYPE_NUM_VAL 'v'
26 #define ARGTYPE_NUM_REF 'V'
27 //#define ARGTYPE_STR_VAL 's'
28 //#define ARGTYPE_STR_REF 'S'
29 #define REFERENCE_CHAR '~' // prefix for call-by-reference
30 #define HALF_INITIAL_ARG_TABLE_SIZE 4
31 static const char exception_macro_twice[] = "Macro already defined.";
32
33
34 // macro struct type definition
35 struct macro_t {
36 int def_line_number;// line number of definition for error msgs
37 char* def_filename; // file name of definition for error msgs
38 char* original_name; // user-supplied name for error msgs
39 char* parameter_list; // parameters (whole line)
40 char* body; // RAM block containing macro body
41 };
42 // there's no need to make this a struct and add a type component:
43 // when the macro has been found, accessing its parameter_list component
44 // gives us the possibility to find out which args are call-by-value and
45 // which ones are call-by-reference.
46 union macro_arg_t {
47 result_t result; // value and flags (call by value)
48 label_t* label; // pointer to label struct (call by reference)
49 };
50
51
52 // Variables
53 static dynabuf_t* user_macro_name; // original macro title
54 static dynabuf_t* internal_name; // plus param type chars
55 static node_ra_t* macro_forest[256]; // trees (because of 8b hash)
56 // Dynamic argument table
57 static union macro_arg_t* arg_table = NULL;
58 static int argtable_size = HALF_INITIAL_ARG_TABLE_SIZE;
59
60
61 // Functions
62
63 // Enlarge the argument table
64 static void enlarge_arg_table(void) {
65 argtable_size *= 2;
66 arg_table =
67 realloc(arg_table, argtable_size * sizeof(union macro_arg_t));
68 if(arg_table == NULL)
69 Throw_serious_error(exception_no_memory_left);
70 }
71
72 // create dynamic buffers and arg table
73 void Macro_init(void) {
74 user_macro_name = DynaBuf_create(MACRONAME_DYNABUF_INITIALSIZE);
75 internal_name = DynaBuf_create(MACRONAME_DYNABUF_INITIALSIZE);
76 enlarge_arg_table();
77 }
78
79 // Read macro zone and title. Title is read to GlobalDynaBuf and then copied
80 // over to internal_name DynaBuf, where ARG_SEPARATOR is added.
81 // In user_macro_name DynaBuf, the original name is reconstructed (even with
82 // '.' prefix) so a copy can be linked to the resulting macro struct.
83 static zone_t get_zone_and_title(void) {
84 zone_t macro_zone;
85
86 Input_read_zone_and_keyword(&macro_zone); // skips spaces before
87 // now GotByte = illegal character after title
88 // copy macro title to private dynabuf and add separator character
89 DYNABUF_CLEAR(user_macro_name);
90 DYNABUF_CLEAR(internal_name);
91 if(macro_zone != ZONE_GLOBAL)
92 DynaBuf_append(user_macro_name, '.');
93 DynaBuf_add_string(user_macro_name, GLOBALDYNABUF_CURRENT);
94 DynaBuf_add_string(internal_name, GLOBALDYNABUF_CURRENT);
95 DynaBuf_append(user_macro_name, '\0');
96 DynaBuf_append(internal_name, ARG_SEPARATOR);
97 SKIPSPACE();// done here once so it's not necessary at two callers
98 return(macro_zone);
99 }
100
101 // Check for comma. If there, append to GlobalDynaBuf.
102 static inline bool pipe_comma(void) {
103 bool result;
104
105 result = Input_accept_comma();
106 if(result)
107 DYNABUF_APPEND(GlobalDynaBuf, ',');
108 return(result);
109 }
110
111 // Return malloc'd copy of string
112 static char* get_string_copy(const char* original) {
113 size_t size;
114 char* copy;
115
116 size = strlen(original) + 1;
117 copy = safe_malloc(size);
118 memcpy(copy, original, size);
119 return(copy);
120 }
121
122 // This function is called from both macro definition and macro call.
123 // Terminate macro name and copy from internal_name to GlobalDynaBuf
124 // (because that's where Tree_hard_scan() looks for the search string).
125 // Then try to find macro and return whether it was created.
126 static bool search_for_macro(node_ra_t** result, zone_t zone, bool create) {
127 DynaBuf_append(internal_name, '\0'); // terminate macro name
128 // now internal_name = macro_title SPC argument_specifiers NUL
129 DYNABUF_CLEAR(GlobalDynaBuf);
130 DynaBuf_add_string(GlobalDynaBuf, internal_name->buffer);
131 DynaBuf_append(GlobalDynaBuf, '\0');
132 return(Tree_hard_scan(result, macro_forest, zone, create));
133 }
134
135 // This function is called when an already existing macro is re-defined.
136 // It first outputs a warning and then a serious error, stopping assembly.
137 // Showing the first message as a warning guarantees that ACME does not reach
138 // the maximum error limit inbetween.
139 static void report_redefinition(node_ra_t* macro_node) {
140 struct macro_t* original_macro = macro_node->body;
141
142 // show warning with location of current definition
143 Throw_warning(exception_macro_twice);
144 // CAUTION, ugly kluge: fiddle with Input_now and Section_now
145 // data to generate helpful error messages
146 Input_now->original_filename = original_macro->def_filename;
147 Input_now->line_number = original_macro->def_line_number;
148 Section_now->type = "original";
149 Section_now->title = "definition";
150 // show serious error with location of original definition
151 Throw_serious_error(exception_macro_twice);
152 }
153
154 // This function is only called during the first pass, so there's no need to
155 // check whether to skip the definition or not.
156 // Return with GotByte = '}'
157 void Macro_parse_definition(void) {// Now GotByte = illegal char after "!macro"
158 char* formal_parameters;
159 node_ra_t* macro_node;
160 struct macro_t* new_macro;
161 zone_t macro_zone = get_zone_and_title();
162
163 // now GotByte = first non-space after title
164 DYNABUF_CLEAR(GlobalDynaBuf); // prepare to hold formal parameters
165 // GlobalDynaBuf = "" (will hold formal parameter list)
166 // user_macro_name = ['.'] MacroTitle NUL
167 // internal_name = MacroTitle ARG_SEPARATOR (grows to signature)
168 // Accept n>=0 comma-separated formal parameters before CHAR_SOB ('{').
169 // Valid argument formats are:
170 // .LOCAL_LABEL_BY_VALUE
171 // ~.LOCAL_LABEL_BY_REFERENCE
172 // GLOBAL_LABEL_BY_VALUE global args are very uncommon,
173 // ~GLOBAL_LABEL_BY_REFERENCE but not forbidden
174 // now GotByte = non-space
175 if(GotByte != CHAR_SOB) { // any at all?
176 do {
177 // handle call-by-reference character ('~')
178 if(GotByte != REFERENCE_CHAR)
179 DynaBuf_append(internal_name, ARGTYPE_NUM_VAL);
180 else {
181 DynaBuf_append(internal_name, ARGTYPE_NUM_REF);
182 DynaBuf_append(GlobalDynaBuf, REFERENCE_CHAR);
183 GetByte();
184 }
185 // handle prefix for local labels ('.')
186 if(GotByte == '.') {
187 DynaBuf_append(GlobalDynaBuf, '.');
188 GetByte();
189 }
190 // handle label name
191 Input_append_keyword_to_global_dynabuf();
192 } while(pipe_comma());
193 // ensure CHAR_SOB ('{')
194 if(GotByte != CHAR_SOB)
195 Throw_serious_error(exception_no_left_brace);
196 }
197 DynaBuf_append(GlobalDynaBuf, CHAR_EOS); // terminate param list
198 // now GlobalDynaBuf = comma-separated parameter list without spaces,
199 // but terminated with CHAR_EOS.
200 formal_parameters = DynaBuf_get_copy(GlobalDynaBuf);
201 // now GlobalDynaBuf = unused
202 // Reading the macro body would change the line number. To have correct
203 // error messages, we're checking for "macro twice" *now*.
204 // Search for macro. Create if not found.
205 // But if found, complain (macro twice).
206 if(search_for_macro(&macro_node, macro_zone, TRUE) == FALSE)
207 report_redefinition(macro_node);// quits with serious error
208 // Create new macro struct and set it up. Finally we'll read the body.
209 new_macro = safe_malloc(sizeof(struct macro_t));
210 new_macro->def_line_number = Input_now->line_number;
211 new_macro->def_filename = get_string_copy(Input_now->original_filename);
212 new_macro->original_name = get_string_copy(user_macro_name->buffer);
213 new_macro->parameter_list = formal_parameters;
214 new_macro->body = Input_skip_or_store_block(TRUE);// changes LineNumber
215 macro_node->body = new_macro; // link macro struct to tree node
216 // and that about sums it up
217 }
218
219 // Parse macro call ("+MACROTITLE"). Has to be re-entrant.
220 void Macro_parse_call(void) { // Now GotByte = dot or first char of macro name
221 char local_gotbyte;
222 label_t* label;
223 section_t new_section,
224 *outer_section;
225 input_t new_input,
226 *outer_input;
227 struct macro_t* actual_macro;
228 node_ra_t *macro_node,
229 *label_node;
230 zone_t macro_zone,
231 label_zone;
232 int arg_count = 0;
233
234 // Enter deeper nesting level
235 // Quit program if recursion too deep.
236 if(--macro_recursions_left < 0)
237 Throw_serious_error("Too deeply nested. Recursive macro calls?");
238 macro_zone = get_zone_and_title();
239 // now GotByte = first non-space after title
240 // internal_name = MacroTitle ARG_SEPARATOR (grows to signature)
241 // Accept n>=0 comma-separated arguments before CHAR_EOS.
242 // Valid argument formats are:
243 // EXPRESSION (everything that does NOT start with '~'
244 // ~.LOCAL_LABEL_BY_REFERENCE
245 // ~GLOBAL_LABEL_BY_REFERENCE
246 // now GotByte = non-space
247 if(GotByte != CHAR_EOS) { // any at all?
248 do {
249 // if arg table cannot take another element, enlarge
250 if(argtable_size <= arg_count)
251 enlarge_arg_table();
252 // Decide whether call-by-reference or call-by-value
253 // In both cases, GlobalDynaBuf may be used.
254 if(GotByte == REFERENCE_CHAR) {
255 // read call-by-reference arg
256 DynaBuf_append(internal_name, ARGTYPE_NUM_REF);
257 GetByte(); // skip '~' character
258 Input_read_zone_and_keyword(&label_zone);
259 // GotByte = illegal char
260 arg_table[arg_count].label =
261 Label_find(label_zone, 0);
262 } else {
263 // read call-by-value arg
264 DynaBuf_append(internal_name, ARGTYPE_NUM_VAL);
265 ALU_any_result(&(arg_table[arg_count].result));
266 }
267 arg_count++;
268 } while(Input_accept_comma());
269 }
270 // now arg_table contains the arguments
271 // now GlobalDynaBuf = unused
272 // check for "unknown macro"
273 // Search for macro. Do not create if not found.
274 search_for_macro(&macro_node, macro_zone, FALSE);
275 if(macro_node == NULL) {
276 Throw_error("Macro not defined (or wrong signature).");
277 Input_skip_remainder();
278 } else {
279 // make macro_node point to the macro struct
280 actual_macro = macro_node->body;
281 local_gotbyte = GotByte;// CAUTION - ugly kluge
282 // set up new input
283 new_input.original_filename = actual_macro->def_filename;
284 new_input.line_number = actual_macro->def_line_number;
285 new_input.source_is_ram = TRUE;
286 new_input.state = INPUTSTATE_NORMAL; // FIXME - fix others!
287 new_input.src.ram_ptr = actual_macro->parameter_list;
288 // remember old input
289 outer_input = Input_now;
290 // activate new input
291 Input_now = &new_input;
292 // remember old section
293 outer_section = Section_now;
294 // start new section (with new zone)
295 // FALSE = title mustn't be freed
296 Section_new_zone(&new_section, "Macro",
297 actual_macro->original_name, FALSE);
298 GetByte(); // fetch first byte of parameter list
299 // assign arguments
300 if(GotByte != CHAR_EOS) { // any at all?
301 arg_count = 0;
302 do {
303 // Decide whether call-by-reference
304 // or call-by-value
305 // In both cases, GlobalDynaBuf may be used.
306 if(GotByte == REFERENCE_CHAR) {
307 // assign call-by-reference arg
308 GetByte(); // skip '~' character
309 Input_read_zone_and_keyword(&label_zone);
310 if((Tree_hard_scan(&label_node, Label_forest, label_zone, TRUE) == FALSE)
311 && (pass_count == 0))
312 Throw_error("Macro parameter twice.");
313 label_node->body = arg_table[arg_count].label;
314 } else {
315 // assign call-by-value arg
316 Input_read_zone_and_keyword(&label_zone);
317 label = Label_find(label_zone, 0);
318 // FIXME - add a possibility to Label_find to make it possible to find out
319 // whether label was just created. Then check for the same error message here
320 // as above ("Macro parameter twice.").
321 label->result = arg_table[arg_count].result;
322 }
323 arg_count++;
324 } while(Input_accept_comma());
325 }
326 // and now, finally, parse the actual macro body
327 Input_now->state = INPUTSTATE_NORMAL; // FIXME - fix others!
328 // maybe call parse_ram_block(actual_macro->def_line_number, actual_macro->body)
329 Input_now->src.ram_ptr = actual_macro->body;
330 Parse_until_eob_or_eof();
331 if(GotByte != CHAR_EOB)
332 Bug_found("IllegalBlockTerminator", GotByte);
333 // end section (free title memory, if needed)
334 Section_finalize(&new_section);
335 // restore previous section
336 Section_now = outer_section;
337 // restore previous input:
338 Input_now = outer_input;
339 // restore old Gotbyte context
340 GotByte = local_gotbyte;// CAUTION - ugly kluge
341 Input_ensure_EOS();
342 }
343 macro_recursions_left++; // leave this nesting level
344 }