| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
#include "global.h" |
| 14 |
#include <string.h> |
| 15 |
#include "platform.h" |
| 16 |
#include "acme.h" |
| 17 |
#include "alu.h" |
| 18 |
#include "cpu.h" |
| 19 |
#include "dynabuf.h" |
| 20 |
#include "encoding.h" |
| 21 |
#include "input.h" |
| 22 |
#include "macro.h" |
| 23 |
#include "output.h" |
| 24 |
#include "pseudoopcodes.h" |
| 25 |
#include "section.h" |
| 26 |
#include "symbol.h" |
| 27 |
#include "tree.h" |
| 28 |
#include "typesystem.h" |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
char s_untitled[] = "<untitled>"; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
const char exception_negative_size[] = "Negative size argument."; |
| 37 |
const char exception_no_left_brace [] = "Expected '{' character."; |
| 38 |
const char exception_no_memory_left[] = "Out of memory."; |
| 39 |
const char exception_no_right_brace [] = "Expected '}', found EOF instead."; |
| 40 |
|
| 41 |
|
| 42 |
const char exception_number_out_of_range[] = "Number out of range."; |
| 43 |
const char exception_number_out_of_8b_range[] = "Number does not fit in 8 bits."; |
| 44 |
static const char exception_number_out_of_16b_range[] = "Number does not fit in 16 bits."; |
| 45 |
static const char exception_number_out_of_24b_range[] = "Number does not fit in 24 bits."; |
| 46 |
const char exception_pc_undefined[] = "Program counter undefined."; |
| 47 |
const char exception_symbol_defined[] = "Symbol already defined."; |
| 48 |
const char exception_syntax[] = "Syntax error."; |
| 49 |
|
| 50 |
#define MAXERRORS 10 |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
const char global_byte_flags[256] = { |
| 65 |
0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 66 |
0x00, 0x10, 0x10, 0x00, 0x00, 0x10, 0x00, 0x00, |
| 67 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 68 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 69 |
0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 70 |
0x00, 0x08, 0x00, 0x00, 0x08, 0x00, 0x00, 0x10, |
| 71 |
0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, |
| 72 |
0x40, 0x40, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, |
| 73 |
0x00, 0xe0, 0xe0, 0xe0, 0xe0, 0xe0, 0xe0, 0xe0, |
| 74 |
0xe0, 0xe0, 0xe0, 0xe0, 0xe0, 0xe0, 0xe0, 0xe0, |
| 75 |
0xe0, 0xe0, 0xe0, 0xe0, 0xe0, 0xe0, 0xe0, 0xe0, |
| 76 |
0xe0, 0xe0, 0xe0, 0x00, 0x00, 0x00, 0x00, 0xc0, |
| 77 |
0x00, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, |
| 78 |
0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, |
| 79 |
0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, |
| 80 |
0xc0, 0xc0, 0xc0, 0x00, 0x00, 0x10, 0x00, 0x00, |
| 81 |
0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, |
| 82 |
0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, |
| 83 |
0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, |
| 84 |
0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, |
| 85 |
0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, |
| 86 |
0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, |
| 87 |
0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, |
| 88 |
0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, |
| 89 |
0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, |
| 90 |
0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, |
| 91 |
0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, |
| 92 |
0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, |
| 93 |
0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, |
| 94 |
0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, |
| 95 |
0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, |
| 96 |
0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, |
| 97 |
}; |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
struct report *report = NULL; |
| 102 |
struct config config; |
| 103 |
struct pass pass; |
| 104 |
struct sanity sanity; |
| 105 |
|
| 106 |
|
| 107 |
void config_default(struct config *conf) |
| 108 |
{ |
| 109 |
conf->pseudoop_prefix = '!'; |
| 110 |
conf->process_verbosity = 0; |
| 111 |
conf->warn_on_indented_labels = TRUE; |
| 112 |
conf->warn_on_type_mismatch = FALSE; |
| 113 |
conf->warn_bin_mask = 3; |
| 114 |
conf->max_errors = MAXERRORS; |
| 115 |
conf->sanity_limit = SANITY_LIMIT; |
| 116 |
conf->format_msvc = FALSE; |
| 117 |
conf->format_color = FALSE; |
| 118 |
conf->msg_stream = stderr; |
| 119 |
conf->honor_leading_zeroes = TRUE; |
| 120 |
conf->strict_segments = FALSE; |
| 121 |
conf->all_warnings_are_errors = FALSE; |
| 122 |
conf->test_new_features = FALSE; |
| 123 |
conf->dialect = V__CURRENT_VERSION; |
| 124 |
conf->politeness = 0; |
| 125 |
conf->debuglevel = DEBUGLEVEL_DEBUG; |
| 126 |
conf->platform_lib_prefix = PLATFORM_LIBPREFIX; |
| 127 |
conf->initial_cpu_type = NULL; |
| 128 |
conf->symbollist_filename = NULL; |
| 129 |
conf->vicelabels_filename = NULL; |
| 130 |
conf->output_filename = NULL; |
| 131 |
conf->outfile_format = OUTFILE_FORMAT_UNSPECIFIED; |
| 132 |
conf->report_filename = NULL; |
| 133 |
conf->mem_init_value = NO_VALUE_GIVEN; |
| 134 |
conf->initial_pc = NO_VALUE_GIVEN; |
| 135 |
conf->outfile_start = NO_VALUE_GIVEN; |
| 136 |
conf->outfile_limit = NO_VALUE_GIVEN; |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
void *safe_malloc(size_t size) |
| 143 |
{ |
| 144 |
void *block = malloc(size); |
| 145 |
|
| 146 |
if (block == NULL) |
| 147 |
throw_serious_error(exception_no_memory_left); |
| 148 |
|
| 149 |
return block; |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
static boolean in_addr_block = FALSE; |
| 157 |
static boolean in_nowarn_block = FALSE; |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
boolean parser_change_addr_block_flag(boolean new_value) |
| 163 |
{ |
| 164 |
boolean old_value = in_addr_block; |
| 165 |
|
| 166 |
in_addr_block = new_value; |
| 167 |
return old_value; |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
boolean parser_change_nowarn_block_flag(boolean new_value) |
| 173 |
{ |
| 174 |
boolean old_value = in_nowarn_block; |
| 175 |
|
| 176 |
in_nowarn_block = new_value; |
| 177 |
return old_value; |
| 178 |
} |
| 179 |
|
| 180 |
#define SF_FOUND_BLANK (1u << 0) // statement started with space or tab |
| 181 |
#define SF_FOUND_SYMBOL (1u << 1) // statement had label or symbol definition |
| 182 |
#define SF_ADDR_PREFIX (1u << 2) // explicit symbol definition is an address |
| 183 |
#define SF_NOWARN_PREFIX (1u << 3) // suppress warnings for this statement |
| 184 |
static bits statement_flags; |
| 185 |
|
| 186 |
|
| 187 |
extern void parser_set_addr_prefix(void) |
| 188 |
{ |
| 189 |
statement_flags |= SF_ADDR_PREFIX; |
| 190 |
} |
| 191 |
|
| 192 |
extern void parser_set_nowarn_prefix(void) |
| 193 |
{ |
| 194 |
statement_flags |= SF_NOWARN_PREFIX; |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
static int first_symbol_of_statement(void) |
| 199 |
{ |
| 200 |
if (statement_flags & SF_FOUND_SYMBOL) { |
| 201 |
throw_error("Unknown mnemonic"); |
| 202 |
parser_skip_remainder(); |
| 203 |
return FALSE; |
| 204 |
} |
| 205 |
statement_flags |= SF_FOUND_SYMBOL; |
| 206 |
return TRUE; |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
static void set_label(scope_t scope, bits force_bit, bits powers) |
| 215 |
{ |
| 216 |
struct symbol *symbol; |
| 217 |
struct object result; |
| 218 |
|
| 219 |
if ((statement_flags & SF_FOUND_BLANK) && config.warn_on_indented_labels) { |
| 220 |
throw_finalpass_warning("Label name not in leftmost column."); |
| 221 |
} |
| 222 |
symbol = symbol_find(scope); |
| 223 |
result.type = &type_number; |
| 224 |
programcounter_read_asterisk(&result.u.number); |
| 225 |
symbol_set_object(symbol, &result, powers); |
| 226 |
if (force_bit) |
| 227 |
symbol_set_force_bit(symbol, force_bit); |
| 228 |
|
| 229 |
if (scope == SCOPE_GLOBAL) |
| 230 |
section_new_cheap_scope(section_now); |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
void parse_assignment(scope_t scope, bits force_bit, bits powers) |
| 238 |
{ |
| 239 |
struct symbol *symbol; |
| 240 |
struct object result; |
| 241 |
|
| 242 |
symbol = symbol_find(scope); |
| 243 |
ALU_any_result(&result); |
| 244 |
|
| 245 |
if (in_addr_block || (statement_flags & SF_ADDR_PREFIX)) { |
| 246 |
|
| 247 |
if (result.type == &type_number) |
| 248 |
result.u.number.addr_refs = 1; |
| 249 |
} |
| 250 |
symbol_set_object(symbol, &result, powers); |
| 251 |
if (force_bit) |
| 252 |
symbol_set_force_bit(symbol, force_bit); |
| 253 |
} |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
static void parse_symbol_definition(scope_t scope) |
| 259 |
{ |
| 260 |
bits force_bit; |
| 261 |
|
| 262 |
force_bit = parser_get_force_bit(); |
| 263 |
if (GotByte == '=') { |
| 264 |
|
| 265 |
GetByte(); |
| 266 |
parse_assignment(scope, force_bit, POWER_NONE); |
| 267 |
parser_ensure_EOS(); |
| 268 |
} else { |
| 269 |
|
| 270 |
set_label(scope, force_bit, POWER_NONE); |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
static void parse_mnemo_or_global_symbol_def(void) |
| 277 |
{ |
| 278 |
|
| 279 |
if (cpu_current_type->keyword_is_mnemonic(parser_read_keyword())) |
| 280 |
return; |
| 281 |
|
| 282 |
|
| 283 |
if (!first_symbol_of_statement()) |
| 284 |
return; |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
if ((*GLOBALDYNABUF_CURRENT == (char) 0xa0) |
| 290 |
|| ((GlobalDynaBuf->size >= 2) && (GLOBALDYNABUF_CURRENT[0] == (char) 0xc2) && (GLOBALDYNABUF_CURRENT[1] == (char) 0xa0))) { |
| 291 |
throw_finalpass_warning("Symbol name starts with a shift-space character."); |
| 292 |
} |
| 293 |
parse_symbol_definition(SCOPE_GLOBAL); |
| 294 |
} |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
static void parse_local_symbol_def(void) |
| 299 |
{ |
| 300 |
scope_t scope; |
| 301 |
|
| 302 |
if (!first_symbol_of_statement()) |
| 303 |
return; |
| 304 |
|
| 305 |
if (input_read_scope_and_symbol_name(&scope) == 0) |
| 306 |
parse_symbol_definition(scope); |
| 307 |
} |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
static void parse_backward_anon_def(void) |
| 312 |
{ |
| 313 |
if (!first_symbol_of_statement()) |
| 314 |
return; |
| 315 |
|
| 316 |
dynabuf_clear(GlobalDynaBuf); |
| 317 |
do { |
| 318 |
DYNABUF_APPEND(GlobalDynaBuf, '-'); |
| 319 |
} while (GetByte() == '-'); |
| 320 |
dynabuf_append(GlobalDynaBuf, '\0'); |
| 321 |
|
| 322 |
set_label(section_now->local_scope, NO_FORCE_BIT, POWER_CHANGE_VALUE); |
| 323 |
} |
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
static void parse_forward_anon_def(void) |
| 328 |
{ |
| 329 |
if (!first_symbol_of_statement()) |
| 330 |
return; |
| 331 |
|
| 332 |
dynabuf_clear(GlobalDynaBuf); |
| 333 |
dynabuf_append(GlobalDynaBuf, '+'); |
| 334 |
while (GotByte == '+') { |
| 335 |
DYNABUF_APPEND(GlobalDynaBuf, '+'); |
| 336 |
GetByte(); |
| 337 |
} |
| 338 |
symbol_fix_forward_anon_name(TRUE); |
| 339 |
dynabuf_append(GlobalDynaBuf, '\0'); |
| 340 |
|
| 341 |
set_label(section_now->local_scope, NO_FORCE_BIT, POWER_NONE); |
| 342 |
} |
| 343 |
|
| 344 |
|
| 345 |
static enum shortcut current_shortcut_state = SHORTCUT_NONE; |
| 346 |
|
| 347 |
enum shortcut parser_get_shortcut(void) |
| 348 |
{ |
| 349 |
return current_shortcut_state; |
| 350 |
} |
| 351 |
|
| 352 |
void parser_set_shortcut(enum shortcut new_shortcut) |
| 353 |
{ |
| 354 |
|
| 355 |
switch (new_shortcut) { |
| 356 |
case SHORTCUT_NONE: |
| 357 |
if (current_shortcut_state == SHORTCUT_NONE) |
| 358 |
BUG("ShortcutDoubleNone", 0); |
| 359 |
current_shortcut_state = new_shortcut; |
| 360 |
break; |
| 361 |
case SHORTCUT_BREAK: |
| 362 |
case SHORTCUT_CONT: |
| 363 |
case SHORTCUT_RETURN: |
| 364 |
if (current_shortcut_state != SHORTCUT_NONE) |
| 365 |
BUG("ShortcutDouble", current_shortcut_state); |
| 366 |
current_shortcut_state = new_shortcut; |
| 367 |
break; |
| 368 |
default: |
| 369 |
BUG("IllegalShortcut", new_shortcut); |
| 370 |
} |
| 371 |
} |
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
static boolean too_many_errors = FALSE; |
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
void parse_until_eob_or_eof(void) |
| 384 |
{ |
| 385 |
|
| 386 |
|
| 387 |
GetByte(); |
| 388 |
|
| 389 |
while ((GotByte != CHAR_EOB) && (GotByte != CHAR_EOF)) { |
| 390 |
|
| 391 |
statement_flags = 0; |
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
do { |
| 397 |
|
| 398 |
|
| 399 |
if (GotByte == config.pseudoop_prefix) { |
| 400 |
pseudoopcode_parse(); |
| 401 |
} else { |
| 402 |
switch (GotByte) { |
| 403 |
case CHAR_EOS: |
| 404 |
|
| 405 |
|
| 406 |
break; |
| 407 |
case ' ': |
| 408 |
statement_flags |= SF_FOUND_BLANK; |
| 409 |
|
| 410 |
case CHAR_SOL: |
| 411 |
GetByte(); |
| 412 |
break; |
| 413 |
case '-': |
| 414 |
parse_backward_anon_def(); |
| 415 |
break; |
| 416 |
case '+': |
| 417 |
GetByte(); |
| 418 |
if ((GotByte == LOCAL_PREFIX) |
| 419 |
|| (GotByte == CHEAP_PREFIX) |
| 420 |
|| (BYTE_CONTINUES_KEYWORD(GotByte))) |
| 421 |
macro_parse_call(); |
| 422 |
else |
| 423 |
parse_forward_anon_def(); |
| 424 |
break; |
| 425 |
case '*': |
| 426 |
notreallypo_setpc(); |
| 427 |
break; |
| 428 |
case LOCAL_PREFIX: |
| 429 |
case CHEAP_PREFIX: |
| 430 |
parse_local_symbol_def(); |
| 431 |
break; |
| 432 |
default: |
| 433 |
if (BYTE_STARTS_KEYWORD(GotByte)) { |
| 434 |
parse_mnemo_or_global_symbol_def(); |
| 435 |
} else { |
| 436 |
throw_error(exception_syntax); |
| 437 |
parser_skip_remainder(); |
| 438 |
} |
| 439 |
} |
| 440 |
} |
| 441 |
} while (GotByte != CHAR_EOS); |
| 442 |
output_end_statement(); |
| 443 |
|
| 444 |
if (too_many_errors) |
| 445 |
exit(ACME_finalize(EXIT_FAILURE)); |
| 446 |
|
| 447 |
if (current_shortcut_state == SHORTCUT_NONE) { |
| 448 |
|
| 449 |
GetByte(); |
| 450 |
} else { |
| 451 |
|
| 452 |
input_block_skip(); |
| 453 |
} |
| 454 |
} |
| 455 |
} |
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
int parse_optional_block(void) |
| 462 |
{ |
| 463 |
SKIPSPACE(); |
| 464 |
if (GotByte != CHAR_SOB) |
| 465 |
return FALSE; |
| 466 |
|
| 467 |
parse_until_eob_or_eof(); |
| 468 |
if (GotByte != CHAR_EOB) |
| 469 |
throw_serious_error(exception_no_right_brace); |
| 470 |
GetByte(); |
| 471 |
return TRUE; |
| 472 |
} |
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
void parse_source_code_file(FILE *fd, const char *eternal_plat_filename) |
| 480 |
{ |
| 481 |
struct inputchange_buf icb; |
| 482 |
const char *ppb; |
| 483 |
boolean break_cont_allowed; |
| 484 |
boolean return_allowed; |
| 485 |
|
| 486 |
|
| 487 |
if (config.process_verbosity >= 3) |
| 488 |
printf("Parsing source file \"%s\".\n", eternal_plat_filename); |
| 489 |
|
| 490 |
|
| 491 |
ppb = input_plat_pathref_filename; |
| 492 |
input_plat_pathref_filename = eternal_plat_filename; |
| 493 |
|
| 494 |
inputchange_new_file(&icb, fd, eternal_plat_filename); |
| 495 |
|
| 496 |
|
| 497 |
break_cont_allowed = parser_allow_break_cont(FALSE); |
| 498 |
return_allowed = parser_allow_return(FALSE); |
| 499 |
|
| 500 |
|
| 501 |
parse_until_eob_or_eof(); |
| 502 |
if (GotByte != CHAR_EOF) |
| 503 |
throw_error("Expected EOF, found '}' instead." ); |
| 504 |
|
| 505 |
|
| 506 |
parser_allow_return(return_allowed); |
| 507 |
parser_allow_break_cont(break_cont_allowed); |
| 508 |
|
| 509 |
inputchange_back(&icb); |
| 510 |
|
| 511 |
input_plat_pathref_filename = ppb; |
| 512 |
} |
| 513 |
|
| 514 |
|
| 515 |
bits parser_get_force_bit(void) |
| 516 |
{ |
| 517 |
char byte; |
| 518 |
bits force_bit = 0; |
| 519 |
|
| 520 |
if (GotByte == '+') { |
| 521 |
byte = GetByte(); |
| 522 |
if (byte == '1') |
| 523 |
force_bit = NUMBER_FORCES_8; |
| 524 |
else if (byte == '2') |
| 525 |
force_bit = NUMBER_FORCES_16; |
| 526 |
else if (byte == '3') |
| 527 |
force_bit = NUMBER_FORCES_24; |
| 528 |
if (force_bit) |
| 529 |
GetByte(); |
| 530 |
else |
| 531 |
throw_error("Illegal postfix."); |
| 532 |
} |
| 533 |
SKIPSPACE(); |
| 534 |
return force_bit; |
| 535 |
} |
| 536 |
|
| 537 |
|
| 538 |
boolean parser_allow_break_cont(boolean new_state) |
| 539 |
{ |
| 540 |
static boolean flag = FALSE; |
| 541 |
boolean temp; |
| 542 |
|
| 543 |
temp = flag; |
| 544 |
flag = new_state; |
| 545 |
return temp; |
| 546 |
} |
| 547 |
|
| 548 |
boolean parser_allow_return(boolean new_state) |
| 549 |
{ |
| 550 |
static boolean flag = FALSE; |
| 551 |
boolean temp; |
| 552 |
|
| 553 |
temp = flag; |
| 554 |
flag = new_state; |
| 555 |
return temp; |
| 556 |
} |
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
static void print_msg(const char *message, const char *ansicolor, const char *type, struct location *opt_alt_loc) |
| 566 |
{ |
| 567 |
const char *resetcolor = "\033[0m"; |
| 568 |
struct location location; |
| 569 |
|
| 570 |
if (!config.format_color) { |
| 571 |
ansicolor = ""; |
| 572 |
resetcolor = ""; |
| 573 |
} |
| 574 |
|
| 575 |
|
| 576 |
if (opt_alt_loc) |
| 577 |
location = *opt_alt_loc; |
| 578 |
else |
| 579 |
input_get_location(&location); |
| 580 |
|
| 581 |
if (config.format_msvc) { |
| 582 |
fprintf(config.msg_stream, "%s(%d) : %s%s%s (%s %s): %s\n", |
| 583 |
location.plat_filename, location.line_number, |
| 584 |
ansicolor, type, resetcolor, |
| 585 |
section_now->type, section_now->title, message); |
| 586 |
} else { |
| 587 |
fprintf(config.msg_stream, "%s%s%s - File %s, line %d (%s %s): %s\n", |
| 588 |
ansicolor, type, resetcolor, |
| 589 |
location.plat_filename, location.line_number, |
| 590 |
section_now->type, section_now->title, message); |
| 591 |
} |
| 592 |
} |
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
static boolean error_is_in_cli_args = TRUE; |
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
void throw_message(enum debuglevel level, const char msg[], struct location *opt_alt_loc) |
| 604 |
{ |
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
if (level < DEBUGLEVEL_SERIOUS) |
| 612 |
level = DEBUGLEVEL_SERIOUS; |
| 613 |
|
| 614 |
switch (level) { |
| 615 |
case DEBUGLEVEL_SERIOUS: |
| 616 |
|
| 617 |
|
| 618 |
PLATFORM_SERIOUS(msg); |
| 619 |
print_msg(msg, "\033[1m\033[31m", "Serious error", opt_alt_loc); |
| 620 |
|
| 621 |
exit(ACME_finalize(EXIT_FAILURE)); |
| 622 |
case DEBUGLEVEL_ERROR: |
| 623 |
|
| 624 |
|
| 625 |
PLATFORM_ERROR(msg); |
| 626 |
print_msg(msg, "\033[31m", "Error", opt_alt_loc); |
| 627 |
++pass.counters.errors; |
| 628 |
if (pass.counters.errors >= config.max_errors) |
| 629 |
too_many_errors = TRUE; |
| 630 |
break; |
| 631 |
case DEBUGLEVEL_WARNING: |
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
if (in_nowarn_block || (statement_flags & SF_NOWARN_PREFIX)) |
| 636 |
break; |
| 637 |
PLATFORM_WARNING(msg); |
| 638 |
print_msg(msg, "\033[33m", "Warning", opt_alt_loc); |
| 639 |
++pass.counters.warnings; |
| 640 |
|
| 641 |
if (config.all_warnings_are_errors) { |
| 642 |
++pass.counters.errors; |
| 643 |
if (pass.counters.errors >= config.max_errors) |
| 644 |
too_many_errors = TRUE; |
| 645 |
} |
| 646 |
break; |
| 647 |
case DEBUGLEVEL_INFO: |
| 648 |
PLATFORM_INFO(msg); |
| 649 |
print_msg(msg, "\033[32m", "Info", opt_alt_loc); |
| 650 |
break; |
| 651 |
default: |
| 652 |
|
| 653 |
print_msg(msg, "\033[36m", "Debug", opt_alt_loc); |
| 654 |
break; |
| 655 |
} |
| 656 |
} |
| 657 |
|
| 658 |
|
| 659 |
void throw_warning(const char msg[]) |
| 660 |
{ |
| 661 |
throw_message(DEBUGLEVEL_WARNING, msg, NULL); |
| 662 |
} |
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
void throw_error(const char msg[]) |
| 668 |
{ |
| 669 |
if (error_is_in_cli_args) |
| 670 |
ACME_cli_args_error(msg); |
| 671 |
else |
| 672 |
throw_message(DEBUGLEVEL_ERROR, msg, NULL); |
| 673 |
} |
| 674 |
|
| 675 |
|
| 676 |
extern void throw_serious_error(const char msg[]) |
| 677 |
{ |
| 678 |
throw_message(DEBUGLEVEL_SERIOUS, msg, NULL); |
| 679 |
} |
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
void throw_pass1_warning(const char msg[]) |
| 685 |
{ |
| 686 |
if (pass.number == 1) |
| 687 |
throw_warning(msg); |
| 688 |
} |
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
void throw_finalpass_warning(const char msg[]) |
| 694 |
{ |
| 695 |
if (pass.flags.is_final_pass) |
| 696 |
throw_warning(msg); |
| 697 |
} |
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
void throw_redef_error(const char error_msg[], struct location *old_def, const char info_msg[]) |
| 702 |
{ |
| 703 |
const char *buffered_section_type; |
| 704 |
char *buffered_section_title; |
| 705 |
|
| 706 |
|
| 707 |
throw_error(error_msg); |
| 708 |
|
| 709 |
|
| 710 |
if (old_def->plat_filename == NULL) |
| 711 |
return; |
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
buffered_section_type = section_now->type; |
| 717 |
buffered_section_title = section_now->title; |
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
section_now->type = "earlier"; |
| 723 |
section_now->title = "section"; |
| 724 |
|
| 725 |
throw_message(DEBUGLEVEL_INFO, info_msg, old_def); |
| 726 |
|
| 727 |
section_now->type = buffered_section_type; |
| 728 |
section_now->title = buffered_section_title; |
| 729 |
} |
| 730 |
|
| 731 |
|
| 732 |
void throw__done_with_cli_args(void) |
| 733 |
{ |
| 734 |
error_is_in_cli_args = FALSE; |
| 735 |
} |
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
void BUG(const char *message, int code) |
| 740 |
{ |
| 741 |
throw_warning("Bug in ACME, code follows"); |
| 742 |
fprintf(stderr, "(0x%x:)", code); |
| 743 |
throw_serious_error(message); |
| 744 |
} |
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
void countorthrow_value_error(const char *msg) |
| 750 |
{ |
| 751 |
boolean complain; |
| 752 |
|
| 753 |
if (config.dialect >= V0_98__PATHS_AND_SYMBOLCHANGE) |
| 754 |
complain = pass.flags.throw_all_errors; |
| 755 |
else |
| 756 |
complain = TRUE; |
| 757 |
|
| 758 |
if (complain) |
| 759 |
throw_error(msg); |
| 760 |
else |
| 761 |
++pass.counters.suppressed_errors; |
| 762 |
} |
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
void output_object(struct object *object, struct iter_context *iter) |
| 767 |
{ |
| 768 |
struct listitem *item; |
| 769 |
int length; |
| 770 |
char *read; |
| 771 |
|
| 772 |
if (object->type == &type_number) { |
| 773 |
if (object->u.number.ntype == NUMTYPE_UNDEFINED) |
| 774 |
iter->fn(0); |
| 775 |
else if (object->u.number.ntype == NUMTYPE_INT) |
| 776 |
iter->fn(object->u.number.val.intval); |
| 777 |
else if (object->u.number.ntype == NUMTYPE_FLOAT) |
| 778 |
iter->fn(object->u.number.val.fpval); |
| 779 |
else |
| 780 |
BUG("IllegalNumberType0", object->u.number.ntype); |
| 781 |
} else if (object->type == &type_list) { |
| 782 |
|
| 783 |
item = object->u.listhead->next; |
| 784 |
while (item != object->u.listhead) { |
| 785 |
output_object(&item->u.payload, iter); |
| 786 |
item = item->next; |
| 787 |
} |
| 788 |
} else if (object->type == &type_string) { |
| 789 |
|
| 790 |
read = object->u.string->payload; |
| 791 |
length = object->u.string->length; |
| 792 |
|
| 793 |
|
| 794 |
if (iter->accept_long_strings || (length < 2)) { |
| 795 |
while (length--) |
| 796 |
iter->fn(iter->stringxor ^ encoding_encode_char(*(read++))); |
| 797 |
} else { |
| 798 |
throw_error("There's more than one character."); |
| 799 |
} |
| 800 |
} else { |
| 801 |
BUG("IllegalObjectType", 0); |
| 802 |
} |
| 803 |
} |
| 804 |
|
| 805 |
|
| 806 |
|
| 807 |
void output_8(intval_t value) |
| 808 |
{ |
| 809 |
if ((value < -0x80) || (value > 0xff)) |
| 810 |
countorthrow_value_error(exception_number_out_of_8b_range); |
| 811 |
output_byte(value); |
| 812 |
} |
| 813 |
|
| 814 |
|
| 815 |
|
| 816 |
void output_be16(intval_t value) |
| 817 |
{ |
| 818 |
if ((value < -0x8000) || (value > 0xffff)) |
| 819 |
countorthrow_value_error(exception_number_out_of_16b_range); |
| 820 |
output_byte(value >> 8); |
| 821 |
output_byte(value); |
| 822 |
} |
| 823 |
|
| 824 |
|
| 825 |
|
| 826 |
void output_le16(intval_t value) |
| 827 |
{ |
| 828 |
if ((value < -0x8000) || (value > 0xffff)) |
| 829 |
countorthrow_value_error(exception_number_out_of_16b_range); |
| 830 |
output_byte(value); |
| 831 |
output_byte(value >> 8); |
| 832 |
} |
| 833 |
|
| 834 |
|
| 835 |
|
| 836 |
void output_be24(intval_t value) |
| 837 |
{ |
| 838 |
if ((value < -0x800000) || (value > 0xffffff)) |
| 839 |
countorthrow_value_error(exception_number_out_of_24b_range); |
| 840 |
output_byte(value >> 16); |
| 841 |
output_byte(value >> 8); |
| 842 |
output_byte(value); |
| 843 |
} |
| 844 |
|
| 845 |
|
| 846 |
|
| 847 |
void output_le24(intval_t value) |
| 848 |
{ |
| 849 |
if ((value < -0x800000) || (value > 0xffffff)) |
| 850 |
countorthrow_value_error(exception_number_out_of_24b_range); |
| 851 |
output_byte(value); |
| 852 |
output_byte(value >> 8); |
| 853 |
output_byte(value >> 16); |
| 854 |
} |
| 855 |
|
| 856 |
|
| 857 |
|
| 858 |
|
| 859 |
|
| 860 |
|
| 861 |
|
| 862 |
|
| 863 |
|
| 864 |
void output_be32(intval_t value) |
| 865 |
{ |
| 866 |
|
| 867 |
|
| 868 |
output_byte(value >> 24); |
| 869 |
output_byte(value >> 16); |
| 870 |
output_byte(value >> 8); |
| 871 |
output_byte(value); |
| 872 |
} |
| 873 |
|
| 874 |
|
| 875 |
|
| 876 |
void output_le32(intval_t value) |
| 877 |
{ |
| 878 |
|
| 879 |
|
| 880 |
output_byte(value); |
| 881 |
output_byte(value >> 8); |
| 882 |
output_byte(value >> 16); |
| 883 |
output_byte(value >> 24); |
| 884 |
} |
| 885 |
|
| 886 |
|
| 887 |
|
| 888 |
const char outputformat_names[] = "'plain', 'cbm', 'apple'"; |
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
enum outfile_format outputformat_find(void) |
| 893 |
{ |
| 894 |
if (strcmp(GlobalDynaBuf->buffer, "plain") == 0) |
| 895 |
return OUTFILE_FORMAT_PLAIN; |
| 896 |
else if (strcmp(GlobalDynaBuf->buffer, "cbm") == 0) |
| 897 |
return OUTFILE_FORMAT_CBM; |
| 898 |
else if (strcmp(GlobalDynaBuf->buffer, "apple") == 0) |
| 899 |
return OUTFILE_FORMAT_APPLE; |
| 900 |
|
| 901 |
|
| 902 |
|
| 903 |
return OUTFILE_FORMAT_UNSPECIFIED; |
| 904 |
} |