150107Smsmith/*- 250107Smsmith * Aic7xxx SCSI host adapter firmware asssembler 350107Smsmith * 450107Smsmith * SPDX-License-Identifier: BSD-3-Clause 550107Smsmith * 650107Smsmith * Copyright (c) 1997, 1998, 2000, 2001 Justin T. Gibbs. 750107Smsmith * Copyright (c) 2001, 2002 Adaptec Inc. 850107Smsmith * All rights reserved. 950107Smsmith * 1050107Smsmith * Redistribution and use in source and binary forms, with or without 1150107Smsmith * modification, are permitted provided that the following conditions 1250107Smsmith * are met: 1350107Smsmith * 1. Redistributions of source code must retain the above copyright 1450107Smsmith * notice, this list of conditions, and the following disclaimer, 1550107Smsmith * without modification. 1650107Smsmith * 2. Redistributions in binary form must reproduce at minimum a disclaimer 1750107Smsmith * substantially similar to the "NO WARRANTY" disclaimer below 1850107Smsmith * ("Disclaimer") and any redistribution must be conditioned upon 1950107Smsmith * including a substantially similar Disclaimer requirement for further 2050107Smsmith * binary redistribution. 2150107Smsmith * 3. Neither the names of the above-listed copyright holders nor the names 2250107Smsmith * of any contributors may be used to endorse or promote products derived 2350107Smsmith * from this software without specific prior written permission. 2450107Smsmith * 2550107Smsmith * Alternatively, this software may be distributed under the terms of the 2650107Smsmith * GNU General Public License ("GPL") version 2 as published by the Free 27116182Sobrien * Software Foundation. 28116182Sobrien * 29116182Sobrien * NO WARRANTY 3050107Smsmith * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 3150107Smsmith * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 3274914Sjhb * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR 3350107Smsmith * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 3469893Sjhb * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 3576166Smarkm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 3650107Smsmith * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 3750107Smsmith * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 3850107Smsmith * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 3969774Sphk * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 4050107Smsmith * POSSIBILITY OF SUCH DAMAGES. 4150107Smsmith * 4260938Sjake * $Id: //depot/aic7xxx/aic7xxx/aicasm/aicasm.c#23 $ 4350107Smsmith */ 4467535Sjhb#include <sys/types.h> 4550107Smsmith#include <sys/mman.h> 4650107Smsmith 4750107Smsmith#include <ctype.h> 4850107Smsmith#include <inttypes.h> 4950107Smsmith#include <regex.h> 5050107Smsmith#include <stdio.h> 5150107Smsmith#include <stdlib.h> 52138439Sjkoshy#include <string.h> 53112111Sjhb#include <sysexits.h> 5469893Sjhb#include <unistd.h> 5569893Sjhb 5669893Sjhb#if defined(__linux__) || defined(__GLIBC__) 5769893Sjhb#include <endian.h> 5869893Sjhb#else 5969893Sjhb#include <machine/endian.h> 6069893Sjhb#endif 61112111Sjhb 62112111Sjhb#include "aicasm.h" 6369893Sjhb#include "aicasm_symbol.h" 6469893Sjhb#include "aicasm_insformat.h" 65177253Srwatson 6669893Sjhbtypedef struct patch { 6750107Smsmith STAILQ_ENTRY(patch) links; 6850107Smsmith int patch_func; 6950107Smsmith u_int begin; 7050107Smsmith u_int skip_instr; 7150107Smsmith u_int skip_patch; 72138439Sjkoshy} patch_t; 7350107Smsmith 7450107SmsmithSTAILQ_HEAD(patch_list, patch) patches; 75112111Sjhb 7650107Smsmithstatic void usage(void) __dead2; 7750107Smsmithstatic void back_patch(void); 7850107Smsmithstatic void output_code(void); 7969893Sjhbstatic void output_listing(char *ifilename); 8050107Smsmithstatic void dump_scope(scope_t *scope); 8166205Smsmithstatic void emit_patch(scope_t *scope, int patch); 8272200Sbmilekicstatic int check_patch(patch_t **start_patch, unsigned int start_instr, 8366205Smsmith unsigned int *skip_addr, int *func_vals); 8450107Smsmith 8550107Smsmithstruct path_list search_path; 8650107Smsmithint includes_search_curdir; 87112111Sjhbchar *appname; 8850107Smsmithchar *stock_include_file; 8950107SmsmithFILE *ofile; 9050107Smsmithchar *ofilename; 91112111Sjhbchar *regfilename; 92112111SjhbFILE *regfile; 93112111Sjhbchar *listfilename; 94112111SjhbFILE *listfile; 95112111Sjhbchar *regdiagfilename; 96112111SjhbFILE *regdiagfile; 97112111Sjhbint src_mode; 98112111Sjhbint dst_mode; 99112111Sjhb 100112111Sjhbstatic STAILQ_HEAD(,instruction) seq_program; 101112111Sjhbstruct cs_tailq cs_tailq; 102112111Sjhbstruct scope_list scope_stack; 103112111Sjhbsymlist_t patch_functions; 104112111Sjhb 105112111Sjhb#if DEBUG 106112111Sjhbextern int yy_flex_debug; 107112111Sjhbextern int mm_flex_debug; 108112111Sjhbextern int yydebug; 109112111Sjhbextern int mmdebug; 11066205Smsmith#endif 11150107Smsmithextern FILE *yyin; 11250107Smsmithextern int yyparse(void); 113112111Sjhb 11450107Smsmithint main(int argc, char *argv[]); 115112111Sjhb 116112111Sjhbint 11750107Smsmithmain(int argc, char *argv[]) 11893743Salfred{ 119112111Sjhb int ch; 12050107Smsmith int retval; 121112111Sjhb char *inputfilename; 122112111Sjhb scope_t *sentinal; 12350107Smsmith 12450107Smsmith STAILQ_INIT(&patches); 12550107Smsmith SLIST_INIT(&search_path); 126112111Sjhb STAILQ_INIT(&seq_program); 127112111Sjhb TAILQ_INIT(&cs_tailq); 128112111Sjhb SLIST_INIT(&scope_stack); 12950107Smsmith 130112111Sjhb /* Set Sentinal scope node */ 131112111Sjhb sentinal = scope_alloc(); 132112111Sjhb sentinal->type = SCOPE_ROOT; 133112111Sjhb 134112111Sjhb includes_search_curdir = 1; 135112111Sjhb appname = *argv; 13650107Smsmith regfile = NULL; 13750107Smsmith listfile = NULL; 13850107Smsmith#if DEBUG 13950107Smsmith yy_flex_debug = 0; 14050107Smsmith mm_flex_debug = 0; 14150107Smsmith yydebug = 0; 142112111Sjhb mmdebug = 0; 14350107Smsmith#endif 14450107Smsmith while ((ch = getopt(argc, argv, "d:i:l:n:o:p:r:I:X")) != -1) { 14550107Smsmith switch(ch) { 14650107Smsmith case 'd': 14750107Smsmith#if DEBUG 14850107Smsmith if (strcmp(optarg, "s") == 0) { 14950107Smsmith yy_flex_debug = 1; 15050107Smsmith mm_flex_debug = 1; 151112111Sjhb } else if (strcmp(optarg, "p") == 0) { 15250107Smsmith yydebug = 1; 15350107Smsmith mmdebug = 1; 154112111Sjhb } else { 155112111Sjhb fprintf(stderr, "%s: -d Requires either an " 156112111Sjhb "'s' or 'p' argument\n", appname); 157112111Sjhb usage(); 158112111Sjhb } 159112111Sjhb#else 160112111Sjhb stop("-d: Assembler not built with debugging " 161112111Sjhb "information", EX_SOFTWARE); 162112111Sjhb#endif 163112111Sjhb break; 16450107Smsmith case 'i': 16550107Smsmith stock_include_file = optarg; 166112111Sjhb break; 167112111Sjhb case 'l': 168112111Sjhb /* Create a program listing */ 169112111Sjhb if ((listfile = fopen(optarg, "w")) == NULL) { 170112111Sjhb perror(optarg); 171112111Sjhb stop(NULL, EX_CANTCREAT); 172112111Sjhb } 173112111Sjhb listfilename = optarg; 174112111Sjhb break; 175112111Sjhb case 'n': 176112111Sjhb /* Don't complain about the -nostdinc directive */ 177112111Sjhb if (strcmp(optarg, "ostdinc")) { 178112111Sjhb fprintf(stderr, "%s: Unknown option -%c%s\n", 17950107Smsmith appname, ch, optarg); 18050107Smsmith usage(); 181200652Sthompsa /* NOTREACHED */ 182200652Sthompsa } 183112111Sjhb break; 18450107Smsmith case 'o': 18550107Smsmith if ((ofile = fopen(optarg, "w")) == NULL) { 186112111Sjhb perror(optarg); 187112111Sjhb stop(NULL, EX_CANTCREAT); 188112111Sjhb } 189112111Sjhb ofilename = optarg; 190138439Sjkoshy break; 191112111Sjhb case 'p': 192112111Sjhb /* Create Register Diagnostic "printing" Functions */ 193112111Sjhb if ((regdiagfile = fopen(optarg, "w")) == NULL) { 194112111Sjhb perror(optarg); 195112111Sjhb stop(NULL, EX_CANTCREAT); 196112111Sjhb } 197112111Sjhb regdiagfilename = optarg; 198112111Sjhb break; 199112111Sjhb case 'r': 200112111Sjhb if ((regfile = fopen(optarg, "w")) == NULL) { 201112111Sjhb perror(optarg); 202112111Sjhb stop(NULL, EX_CANTCREAT); 203112111Sjhb } 204112111Sjhb regfilename = optarg; 20550107Smsmith break; 206138439Sjkoshy case 'I': 20750107Smsmith { 20850107Smsmith path_entry_t include_dir; 20980703Sjake 21080703Sjake if (strcmp(optarg, "-") == 0) { 21180703Sjake if (includes_search_curdir == 0) { 21266205Smsmith fprintf(stderr, "%s: Warning - '-I-' " 21350107Smsmith "specified multiple " 21472200Sbmilekic "times\n", appname); 215112111Sjhb } 216112111Sjhb includes_search_curdir = 0; 217112111Sjhb for (include_dir = SLIST_FIRST(&search_path); 21872200Sbmilekic include_dir != NULL; 21966205Smsmith include_dir = SLIST_NEXT(include_dir, 22050107Smsmith links)) 22150107Smsmith /* 22266205Smsmith * All entries before a '-I-' only 223112111Sjhb * apply to includes specified with 224112111Sjhb * quotes instead of "<>". 225112111Sjhb */ 226112111Sjhb include_dir->quoted_includes_only = 1; 227112111Sjhb } else { 228112111Sjhb include_dir = 229112111Sjhb (path_entry_t)malloc(sizeof(*include_dir)); 230200652Sthompsa if (include_dir == NULL) { 231112111Sjhb perror(optarg); 232112111Sjhb stop(NULL, EX_OSERR); 233112111Sjhb } 234200652Sthompsa include_dir->directory = strdup(optarg); 235112111Sjhb if (include_dir->directory == NULL) { 236112111Sjhb perror(optarg); 237112111Sjhb stop(NULL, EX_OSERR); 238200652Sthompsa } 239112111Sjhb include_dir->quoted_includes_only = 0; 240112111Sjhb SLIST_INSERT_HEAD(&search_path, include_dir, 241200652Sthompsa links); 242200652Sthompsa } 243112111Sjhb break; 244 } 245 case 'X': 246 /* icc version of -nostdinc */ 247 break; 248 case '?': 249 default: 250 usage(); 251 /* NOTREACHED */ 252 } 253 } 254 argc -= optind; 255 argv += optind; 256 257 if (argc != 1) { 258 fprintf(stderr, "%s: No input file specified\n", appname); 259 usage(); 260 /* NOTREACHED */ 261 } 262 263 if (regdiagfile != NULL 264 && (regfile == NULL || stock_include_file == NULL)) { 265 fprintf(stderr, 266 "%s: The -p option requires the -r and -i options.\n", 267 appname); 268 usage(); 269 /* NOTREACHED */ 270 } 271 symtable_open(); 272 inputfilename = *argv; 273 include_file(*argv, SOURCE_FILE); 274 retval = yyparse(); 275 if (retval == 0) { 276 if (SLIST_FIRST(&scope_stack) == NULL 277 || SLIST_FIRST(&scope_stack)->type != SCOPE_ROOT) { 278 stop("Unterminated conditional expression", EX_DATAERR); 279 /* NOTREACHED */ 280 } 281 282 /* Process outmost scope */ 283 process_scope(SLIST_FIRST(&scope_stack)); 284 /* 285 * Descend the tree of scopes and insert/emit 286 * patches as appropriate. We perform a depth first 287 * transversal, recursively handling each scope. 288 */ 289 /* start at the root scope */ 290 dump_scope(SLIST_FIRST(&scope_stack)); 291 292 /* Patch up forward jump addresses */ 293 back_patch(); 294 295 if (ofile != NULL) 296 output_code(); 297 if (regfile != NULL) 298 symtable_dump(regfile, regdiagfile); 299 if (listfile != NULL) 300 output_listing(inputfilename); 301 } 302 303 stop(NULL, 0); 304 /* NOTREACHED */ 305 return (0); 306} 307 308static void 309usage(void) 310{ 311 312 (void)fprintf(stderr, 313"usage: %-16s [-nostdinc|-X] [-I-] [-I directory] [-o output_file]\n" 314" [-r register_output_file [-p register_diag_file -i includefile]]\n" 315" [-l program_list_file]\n" 316" input_file\n", appname); 317 exit(EX_USAGE); 318} 319 320static void 321back_patch(void) 322{ 323 struct instruction *cur_instr; 324 325 for (cur_instr = STAILQ_FIRST(&seq_program); 326 cur_instr != NULL; 327 cur_instr = STAILQ_NEXT(cur_instr, links)) { 328 if (cur_instr->patch_label != NULL) { 329 struct ins_format3 *f3_instr; 330 u_int address; 331 332 if (cur_instr->patch_label->type != LABEL) { 333 char buf[255]; 334 335 snprintf(buf, sizeof(buf), 336 "Undefined label %s", 337 cur_instr->patch_label->name); 338 stop(buf, EX_DATAERR); 339 /* NOTREACHED */ 340 } 341 f3_instr = &cur_instr->format.format3; 342 address = f3_instr->address; 343 address += cur_instr->patch_label->info.linfo->address; 344 f3_instr->address = address; 345 } 346 } 347} 348 349static void 350output_code(void) 351{ 352 struct instruction *cur_instr; 353 patch_t *cur_patch; 354 critical_section_t *cs; 355 symbol_node_t *cur_node; 356 int instrcount; 357 358 instrcount = 0; 359 fprintf(ofile, 360"/*\n" 361" * DO NOT EDIT - This file is automatically generated\n" 362" * from the following source files:\n" 363" *\n" 364"%s */\n", versions); 365 366 fprintf(ofile, "static uint8_t seqprog[] = {\n"); 367 for (cur_instr = STAILQ_FIRST(&seq_program); 368 cur_instr != NULL; 369 cur_instr = STAILQ_NEXT(cur_instr, links)) { 370 fprintf(ofile, "%s\t0x%02x, 0x%02x, 0x%02x, 0x%02x", 371 cur_instr == STAILQ_FIRST(&seq_program) ? "" : ",\n", 372#if BYTE_ORDER == LITTLE_ENDIAN 373 cur_instr->format.bytes[0], 374 cur_instr->format.bytes[1], 375 cur_instr->format.bytes[2], 376 cur_instr->format.bytes[3]); 377#else 378 cur_instr->format.bytes[3], 379 cur_instr->format.bytes[2], 380 cur_instr->format.bytes[1], 381 cur_instr->format.bytes[0]); 382#endif 383 instrcount++; 384 } 385 fprintf(ofile, "\n};\n\n"); 386 387 if (patch_arg_list == NULL) 388 stop("Patch argument list not defined", 389 EX_DATAERR); 390 391 /* 392 * Output patch information. Patch functions first. 393 */ 394 fprintf(ofile, 395"typedef int %spatch_func_t (%s);\n", prefix, patch_arg_list); 396 397 for (cur_node = SLIST_FIRST(&patch_functions); 398 cur_node != NULL; 399 cur_node = SLIST_NEXT(cur_node,links)) { 400 fprintf(ofile, 401"static %spatch_func_t %spatch%d_func;\n" 402"\n" 403"static int\n" 404"%spatch%d_func(%s)\n" 405"{\n" 406" return (%s);\n" 407"}\n\n", 408 prefix, 409 prefix, 410 cur_node->symbol->info.condinfo->func_num, 411 prefix, 412 cur_node->symbol->info.condinfo->func_num, 413 patch_arg_list, 414 cur_node->symbol->name); 415 } 416 417 fprintf(ofile, 418"static struct patch {\n" 419" %spatch_func_t *patch_func;\n" 420" uint32_t begin :10,\n" 421" skip_instr :10,\n" 422" skip_patch :12;\n" 423"} patches[] = {\n", prefix); 424 425 for (cur_patch = STAILQ_FIRST(&patches); 426 cur_patch != NULL; 427 cur_patch = STAILQ_NEXT(cur_patch,links)) { 428 fprintf(ofile, "%s\t{ %spatch%d_func, %d, %d, %d }", 429 cur_patch == STAILQ_FIRST(&patches) ? "" : ",\n", 430 prefix, 431 cur_patch->patch_func, cur_patch->begin, 432 cur_patch->skip_instr, cur_patch->skip_patch); 433 } 434 435 fprintf(ofile, "\n};\n\n"); 436 437 fprintf(ofile, 438"static struct cs {\n" 439" uint16_t begin;\n" 440" uint16_t end;\n" 441"} critical_sections[] = {\n"); 442 443 for (cs = TAILQ_FIRST(&cs_tailq); 444 cs != NULL; 445 cs = TAILQ_NEXT(cs, links)) { 446 fprintf(ofile, "%s\t{ %d, %d }", 447 cs == TAILQ_FIRST(&cs_tailq) ? "" : ",\n", 448 cs->begin_addr, cs->end_addr); 449 } 450 451 fprintf(ofile, "\n};\n\n"); 452 453 fprintf(ofile, 454"static const int num_critical_sections = sizeof(critical_sections)\n" 455" / sizeof(*critical_sections);\n"); 456 457 fprintf(stderr, "%s: %d instructions used\n", appname, instrcount); 458} 459 460static void 461dump_scope(scope_t *scope) 462{ 463 scope_t *cur_scope; 464 465 /* 466 * Emit the first patch for this scope 467 */ 468 emit_patch(scope, 0); 469 470 /* 471 * Dump each scope within this one. 472 */ 473 cur_scope = TAILQ_FIRST(&scope->inner_scope); 474 475 while (cur_scope != NULL) { 476 dump_scope(cur_scope); 477 478 cur_scope = TAILQ_NEXT(cur_scope, scope_links); 479 } 480 481 /* 482 * Emit the second, closing, patch for this scope 483 */ 484 emit_patch(scope, 1); 485} 486 487void 488emit_patch(scope_t *scope, int patch) 489{ 490 patch_info_t *pinfo; 491 patch_t *new_patch; 492 493 pinfo = &scope->patches[patch]; 494 495 if (pinfo->skip_instr == 0) 496 /* No-Op patch */ 497 return; 498 499 new_patch = (patch_t *)malloc(sizeof(*new_patch)); 500 501 if (new_patch == NULL) 502 stop("Could not malloc patch structure", EX_OSERR); 503 504 memset(new_patch, 0, sizeof(*new_patch)); 505 506 if (patch == 0) { 507 new_patch->patch_func = scope->func_num; 508 new_patch->begin = scope->begin_addr; 509 } else { 510 new_patch->patch_func = 0; 511 new_patch->begin = scope->end_addr; 512 } 513 new_patch->skip_instr = pinfo->skip_instr; 514 new_patch->skip_patch = pinfo->skip_patch; 515 STAILQ_INSERT_TAIL(&patches, new_patch, links); 516} 517 518void 519output_listing(char *ifilename) 520{ 521 char buf[1024]; 522 FILE *ifile; 523 struct instruction *cur_instr; 524 patch_t *cur_patch; 525 symbol_node_t *cur_func; 526 int *func_values; 527 int instrcount; 528 int instrptr; 529 unsigned int line; 530 int func_count; 531 unsigned int skip_addr; 532 533 instrcount = 0; 534 instrptr = 0; 535 line = 1; 536 skip_addr = 0; 537 if ((ifile = fopen(ifilename, "r")) == NULL) { 538 perror(ifilename); 539 stop(NULL, EX_DATAERR); 540 } 541 542 /* 543 * Determine which options to apply to this listing. 544 */ 545 for (func_count = 0, cur_func = SLIST_FIRST(&patch_functions); 546 cur_func != NULL; 547 cur_func = SLIST_NEXT(cur_func, links)) 548 func_count++; 549 550 func_values = NULL; 551 if (func_count != 0) { 552 func_values = (int *)malloc(func_count * sizeof(int)); 553 554 if (func_values == NULL) 555 stop("Could not malloc", EX_OSERR); 556 557 func_values[0] = 0; /* FALSE func */ 558 func_count--; 559 560 /* 561 * Ask the user to fill in the return values for 562 * the rest of the functions. 563 */ 564 565 566 for (cur_func = SLIST_FIRST(&patch_functions); 567 cur_func != NULL && SLIST_NEXT(cur_func, links) != NULL; 568 cur_func = SLIST_NEXT(cur_func, links), func_count--) { 569 int input; 570 571 fprintf(stdout, "\n(%s)\n", cur_func->symbol->name); 572 fprintf(stdout, 573 "Enter the return value for " 574 "this expression[T/F]:"); 575 576 while (1) { 577 input = getchar(); 578 input = toupper(input); 579 580 if (input == 'T') { 581 func_values[func_count] = 1; 582 break; 583 } else if (input == 'F') { 584 func_values[func_count] = 0; 585 break; 586 } 587 } 588 if (isatty(fileno(stdin)) == 0) 589 putchar(input); 590 } 591 fprintf(stdout, "\nThanks!\n"); 592 } 593 594 /* Now output the listing */ 595 cur_patch = STAILQ_FIRST(&patches); 596 for (cur_instr = STAILQ_FIRST(&seq_program); 597 cur_instr != NULL; 598 cur_instr = STAILQ_NEXT(cur_instr, links), instrcount++) { 599 if (check_patch(&cur_patch, instrcount, 600 &skip_addr, func_values) == 0) { 601 /* Don't count this instruction as it is in a patch 602 * that was removed. 603 */ 604 continue; 605 } 606 607 while (line < cur_instr->srcline) { 608 fgets(buf, sizeof(buf), ifile); 609 fprintf(listfile, " \t%s", buf); 610 line++; 611 } 612 fprintf(listfile, "%04x %02x%02x%02x%02x", instrptr, 613#if BYTE_ORDER == LITTLE_ENDIAN 614 cur_instr->format.bytes[0], 615 cur_instr->format.bytes[1], 616 cur_instr->format.bytes[2], 617 cur_instr->format.bytes[3]); 618#else 619 cur_instr->format.bytes[3], 620 cur_instr->format.bytes[2], 621 cur_instr->format.bytes[1], 622 cur_instr->format.bytes[0]); 623#endif 624 /* 625 * Macro expansions can cause several instructions 626 * to be output for a single source line. Only 627 * advance the line once in these cases. 628 */ 629 if (line == cur_instr->srcline) { 630 fgets(buf, sizeof(buf), ifile); 631 fprintf(listfile, "\t%s", buf); 632 line++; 633 } else { 634 fprintf(listfile, "\n"); 635 } 636 instrptr++; 637 } 638 free(func_values); 639 640 /* Dump the remainder of the file */ 641 while(fgets(buf, sizeof(buf), ifile) != NULL) 642 fprintf(listfile, " %s", buf); 643 644 fclose(ifile); 645} 646 647static int 648check_patch(patch_t **start_patch, unsigned int start_instr, 649 unsigned int *skip_addr, int *func_vals) 650{ 651 patch_t *cur_patch; 652 653 cur_patch = *start_patch; 654 655 while (cur_patch != NULL && start_instr == cur_patch->begin) { 656 if (func_vals[cur_patch->patch_func] == 0) { 657 int skip; 658 659 /* Start rejecting code */ 660 *skip_addr = start_instr + cur_patch->skip_instr; 661 for (skip = cur_patch->skip_patch; 662 skip > 0 && cur_patch != NULL; 663 skip--) 664 cur_patch = STAILQ_NEXT(cur_patch, links); 665 } else { 666 /* Accepted this patch. Advance to the next 667 * one and wait for our instruction pointer to 668 * hit this point. 669 */ 670 cur_patch = STAILQ_NEXT(cur_patch, links); 671 } 672 } 673 674 *start_patch = cur_patch; 675 if (start_instr < *skip_addr) 676 /* Still skipping */ 677 return (0); 678 679 return (1); 680} 681 682/* 683 * Print out error information if appropriate, and clean up before 684 * terminating the program. 685 */ 686void 687stop(const char *string, int err_code) 688{ 689 if (string != NULL) { 690 fprintf(stderr, "%s: ", appname); 691 if (yyfilename != NULL) { 692 fprintf(stderr, "Stopped at file %s, line %d - ", 693 yyfilename, yylineno); 694 } 695 fprintf(stderr, "%s\n", string); 696 } 697 698 if (ofile != NULL) { 699 fclose(ofile); 700 if (err_code != 0) { 701 fprintf(stderr, "%s: Removing %s due to error\n", 702 appname, ofilename); 703 unlink(ofilename); 704 } 705 } 706 707 if (regfile != NULL) { 708 fclose(regfile); 709 if (err_code != 0) { 710 fprintf(stderr, "%s: Removing %s due to error\n", 711 appname, regfilename); 712 unlink(regfilename); 713 } 714 } 715 716 if (listfile != NULL) { 717 fclose(listfile); 718 if (err_code != 0) { 719 fprintf(stderr, "%s: Removing %s due to error\n", 720 appname, listfilename); 721 unlink(listfilename); 722 } 723 } 724 725 symlist_free(&patch_functions); 726 symtable_close(); 727 728 exit(err_code); 729} 730 731struct instruction * 732seq_alloc(void) 733{ 734 struct instruction *new_instr; 735 736 new_instr = (struct instruction *)malloc(sizeof(struct instruction)); 737 if (new_instr == NULL) 738 stop("Unable to malloc instruction object", EX_SOFTWARE); 739 memset(new_instr, 0, sizeof(*new_instr)); 740 STAILQ_INSERT_TAIL(&seq_program, new_instr, links); 741 new_instr->srcline = yylineno; 742 return new_instr; 743} 744 745critical_section_t * 746cs_alloc(void) 747{ 748 critical_section_t *new_cs; 749 750 new_cs= (critical_section_t *)malloc(sizeof(critical_section_t)); 751 if (new_cs == NULL) 752 stop("Unable to malloc critical_section object", EX_SOFTWARE); 753 memset(new_cs, 0, sizeof(*new_cs)); 754 755 TAILQ_INSERT_TAIL(&cs_tailq, new_cs, links); 756 return new_cs; 757} 758 759scope_t * 760scope_alloc(void) 761{ 762 scope_t *new_scope; 763 764 new_scope = (scope_t *)malloc(sizeof(scope_t)); 765 if (new_scope == NULL) 766 stop("Unable to malloc scope object", EX_SOFTWARE); 767 memset(new_scope, 0, sizeof(*new_scope)); 768 TAILQ_INIT(&new_scope->inner_scope); 769 770 if (SLIST_FIRST(&scope_stack) != NULL) { 771 TAILQ_INSERT_TAIL(&SLIST_FIRST(&scope_stack)->inner_scope, 772 new_scope, scope_links); 773 } 774 /* This patch is now the current scope */ 775 SLIST_INSERT_HEAD(&scope_stack, new_scope, scope_stack_links); 776 return new_scope; 777} 778 779void 780process_scope(scope_t *scope) 781{ 782 /* 783 * We are "leaving" this scope. We should now have 784 * enough information to process the lists of scopes 785 * we encapsulate. 786 */ 787 scope_t *cur_scope; 788 u_int skip_patch_count; 789 u_int skip_instr_count; 790 791 cur_scope = TAILQ_LAST(&scope->inner_scope, scope_tailq); 792 skip_patch_count = 0; 793 skip_instr_count = 0; 794 while (cur_scope != NULL) { 795 u_int patch0_patch_skip; 796 797 patch0_patch_skip = 0; 798 switch (cur_scope->type) { 799 case SCOPE_IF: 800 case SCOPE_ELSE_IF: 801 if (skip_instr_count != 0) { 802 /* Create a tail patch */ 803 patch0_patch_skip++; 804 cur_scope->patches[1].skip_patch = 805 skip_patch_count + 1; 806 cur_scope->patches[1].skip_instr = 807 skip_instr_count; 808 } 809 810 /* Count Head patch */ 811 patch0_patch_skip++; 812 813 /* Count any patches contained in our inner scope */ 814 patch0_patch_skip += cur_scope->inner_scope_patches; 815 816 cur_scope->patches[0].skip_patch = patch0_patch_skip; 817 cur_scope->patches[0].skip_instr = 818 cur_scope->end_addr - cur_scope->begin_addr; 819 820 skip_instr_count += cur_scope->patches[0].skip_instr; 821 822 skip_patch_count += patch0_patch_skip; 823 if (cur_scope->type == SCOPE_IF) { 824 scope->inner_scope_patches += skip_patch_count; 825 skip_patch_count = 0; 826 skip_instr_count = 0; 827 } 828 break; 829 case SCOPE_ELSE: 830 /* Count any patches contained in our innter scope */ 831 skip_patch_count += cur_scope->inner_scope_patches; 832 833 skip_instr_count += cur_scope->end_addr 834 - cur_scope->begin_addr; 835 break; 836 case SCOPE_ROOT: 837 stop("Unexpected scope type encountered", EX_SOFTWARE); 838 /* NOTREACHED */ 839 } 840 841 cur_scope = TAILQ_PREV(cur_scope, scope_tailq, scope_links); 842 } 843} 844