| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include "macro.h" |
| 7 |
#include <string.h> |
| 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 |
|
| 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 |
|
| 29 |
struct macro { |
| 30 |
struct location definition; |
| 31 |
char *original_name, |
| 32 |
*parameter_list; |
| 33 |
struct block body; |
| 34 |
}; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
union macro_arg_t { |
| 40 |
struct object result; |
| 41 |
struct symbol *symbol; |
| 42 |
}; |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
static STRUCT_DYNABUF_REF(user_macro_name, NAME_INITIALSIZE); |
| 47 |
static STRUCT_DYNABUF_REF(internal_name, NAME_INITIALSIZE); |
| 48 |
static struct rwnode *macro_forest[256]; |
| 49 |
|
| 50 |
static union macro_arg_t *arg_table = NULL; |
| 51 |
static int argtable_size = HALF_INITIAL_ARG_TABLE_SIZE; |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
static void enlarge_arg_table(void) |
| 58 |
{ |
| 59 |
argtable_size *= 2; |
| 60 |
|
| 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 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
static scope_t get_scope_and_title(void) |
| 71 |
{ |
| 72 |
scope_t macro_scope; |
| 73 |
|
| 74 |
input_read_scope_and_symbol_name(¯o_scope); |
| 75 |
|
| 76 |
|
| 77 |
dynabuf_clear(user_macro_name); |
| 78 |
dynabuf_add_string(user_macro_name, GLOBALDYNABUF_CURRENT); |
| 79 |
dynabuf_append(user_macro_name, '\0'); |
| 80 |
dynabuf_clear(internal_name); |
| 81 |
dynabuf_add_string(internal_name, GLOBALDYNABUF_CURRENT); |
| 82 |
dynabuf_append(internal_name, ARG_SEPARATOR); |
| 83 |
SKIPSPACE(); |
| 84 |
return macro_scope; |
| 85 |
} |
| 86 |
|
| 87 |
|
| 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 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
static int search_for_macro(struct rwnode **result, scope_t scope, int create) |
| 103 |
{ |
| 104 |
dynabuf_append(internal_name, '\0'); |
| 105 |
|
| 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 |
|
| 113 |
|
| 114 |
|
| 115 |
void macro_parse_definition(void) |
| 116 |
{ |
| 117 |
char *formal_parameters; |
| 118 |
struct rwnode *macro_node; |
| 119 |
struct macro *original_macro; |
| 120 |
struct macro *new_macro; |
| 121 |
scope_t macro_scope = get_scope_and_title(); |
| 122 |
|
| 123 |
|
| 124 |
dynabuf_clear(GlobalDynaBuf); |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
if (GotByte != CHAR_SOB) { |
| 138 |
do { |
| 139 |
|
| 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 |
|
| 148 |
input_append_symbol_name_to_global_dynabuf(); |
| 149 |
} while (pipe_comma()); |
| 150 |
|
| 151 |
if (GotByte != CHAR_SOB) |
| 152 |
throw_serious_error(exception_no_left_brace); |
| 153 |
} |
| 154 |
dynabuf_append(GlobalDynaBuf, CHAR_EOS); |
| 155 |
|
| 156 |
|
| 157 |
formal_parameters = dynabuf_get_copy(GlobalDynaBuf); |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
if (search_for_macro(¯o_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 |
|
| 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); |
| 172 |
input_block_getcopy(&new_macro->body); |
| 173 |
macro_node->body = new_macro; |
| 174 |
|
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
void macro_parse_call(void) |
| 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 |
|
| 196 |
if (arg_table == NULL) |
| 197 |
enlarge_arg_table(); |
| 198 |
|
| 199 |
|
| 200 |
|
| 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 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
if (GotByte != CHAR_EOS) { |
| 212 |
do { |
| 213 |
|
| 214 |
if (argtable_size <= arg_count) |
| 215 |
enlarge_arg_table(); |
| 216 |
|
| 217 |
|
| 218 |
if (GotByte == REFERENCE_CHAR) { |
| 219 |
|
| 220 |
dynabuf_append(internal_name, ARGTYPE_REF); |
| 221 |
GetByte(); |
| 222 |
input_read_scope_and_symbol_name(&symbol_scope); |
| 223 |
|
| 224 |
arg_table[arg_count].symbol = symbol_find(symbol_scope); |
| 225 |
} else { |
| 226 |
|
| 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 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
search_for_macro(¯o_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 |
|
| 243 |
actual_macro = macro_node->body; |
| 244 |
|
| 245 |
|
| 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; |
| 250 |
|
| 251 |
|
| 252 |
outer_section = section_now; |
| 253 |
|
| 254 |
|
| 255 |
section_new(&new_section, "Macro", actual_macro->original_name, FALSE); |
| 256 |
section_new_cheap_scope(&new_section); |
| 257 |
|
| 258 |
|
| 259 |
GetByte(); |
| 260 |
if (GotByte != CHAR_EOS) { |
| 261 |
arg_count = 0; |
| 262 |
do { |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
if (GotByte == REFERENCE_CHAR) { |
| 267 |
|
| 268 |
GetByte(); |
| 269 |
input_read_scope_and_symbol_name(&symbol_scope); |
| 270 |
|
| 271 |
if (tree_hard_scan(&symbol_node, symbols_forest, symbol_scope, TRUE) == FALSE) { |
| 272 |
|
| 273 |
if (pass.number == 1) |
| 274 |
throw_error("Macro parameter twice."); |
| 275 |
} |
| 276 |
symbol_node->body = arg_table[arg_count].symbol; |
| 277 |
} else { |
| 278 |
|
| 279 |
input_read_scope_and_symbol_name(&symbol_scope); |
| 280 |
symbol = symbol_find(symbol_scope); |
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
symbol->object = arg_table[arg_count].result; |
| 285 |
} |
| 286 |
++arg_count; |
| 287 |
} while (parser_accept_comma()); |
| 288 |
} |
| 289 |
|
| 290 |
|
| 291 |
break_cont_allowed = parser_allow_break_cont(FALSE); |
| 292 |
return_allowed = parser_allow_return(TRUE); |
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
inputchange_macro2_body(actual_macro->body.body); |
| 297 |
parse_until_eob_or_eof(); |
| 298 |
if (GotByte != CHAR_EOB) |
| 299 |
BUG("IllegalBlockTerminator", GotByte); |
| 300 |
|
| 301 |
if (parser_get_shortcut() == SHORTCUT_RETURN) |
| 302 |
parser_set_shortcut(SHORTCUT_NONE); |
| 303 |
|
| 304 |
|
| 305 |
parser_allow_return(return_allowed); |
| 306 |
parser_allow_break_cont(break_cont_allowed); |
| 307 |
|
| 308 |
|
| 309 |
section_finalize(&new_section); |
| 310 |
|
| 311 |
section_now = outer_section; |
| 312 |
|
| 313 |
|
| 314 |
inputchange_back(&icb); |
| 315 |
|
| 316 |
|
| 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; |
| 323 |
} |