PageRenderTime 226ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/drivers/scsi/aic7xxx/aicasm/aicasm.c

https://github.com/mturquette/linux
C | 844 lines | 623 code | 103 blank | 118 comment | 147 complexity | fba8430f74c93c5833b870fafcca2f95 MD5 | raw file
  1. /*
  2. * Aic7xxx SCSI host adapter firmware assembler
  3. *
  4. * Copyright (c) 1997, 1998, 2000, 2001 Justin T. Gibbs.
  5. * Copyright (c) 2001, 2002 Adaptec Inc.
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions, and the following disclaimer,
  13. * without modification.
  14. * 2. Redistributions in binary form must reproduce at minimum a disclaimer
  15. * substantially similar to the "NO WARRANTY" disclaimer below
  16. * ("Disclaimer") and any redistribution must be conditioned upon
  17. * including a substantially similar Disclaimer requirement for further
  18. * binary redistribution.
  19. * 3. Neither the names of the above-listed copyright holders nor the names
  20. * of any contributors may be used to endorse or promote products derived
  21. * from this software without specific prior written permission.
  22. *
  23. * Alternatively, this software may be distributed under the terms of the
  24. * GNU General Public License ("GPL") version 2 as published by the Free
  25. * Software Foundation.
  26. *
  27. * NO WARRANTY
  28. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  29. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  30. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
  31. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  32. * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  33. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  34. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  36. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  37. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  38. * POSSIBILITY OF SUCH DAMAGES.
  39. *
  40. * $Id: //depot/aic7xxx/aic7xxx/aicasm/aicasm.c#23 $
  41. *
  42. * $FreeBSD$
  43. */
  44. #include <sys/types.h>
  45. #include <sys/mman.h>
  46. #include <ctype.h>
  47. #include <inttypes.h>
  48. #include <regex.h>
  49. #include <stdio.h>
  50. #include <stdlib.h>
  51. #include <string.h>
  52. #include <sysexits.h>
  53. #include <unistd.h>
  54. #if linux
  55. #include <endian.h>
  56. #else
  57. #include <machine/endian.h>
  58. #endif
  59. #include "aicasm.h"
  60. #include "aicasm_symbol.h"
  61. #include "aicasm_insformat.h"
  62. typedef struct patch {
  63. STAILQ_ENTRY(patch) links;
  64. int patch_func;
  65. u_int begin;
  66. u_int skip_instr;
  67. u_int skip_patch;
  68. } patch_t;
  69. STAILQ_HEAD(patch_list, patch) patches;
  70. static void usage(void);
  71. static void back_patch(void);
  72. static void output_code(void);
  73. static void output_listing(char *ifilename);
  74. static void dump_scope(scope_t *scope);
  75. static void emit_patch(scope_t *scope, int patch);
  76. static int check_patch(patch_t **start_patch, int start_instr,
  77. int *skip_addr, int *func_vals);
  78. struct path_list search_path;
  79. int includes_search_curdir;
  80. char *appname;
  81. char *stock_include_file;
  82. FILE *ofile;
  83. char *ofilename;
  84. char *regfilename;
  85. FILE *regfile;
  86. char *listfilename;
  87. FILE *listfile;
  88. char *regdiagfilename;
  89. FILE *regdiagfile;
  90. int src_mode;
  91. int dst_mode;
  92. static STAILQ_HEAD(,instruction) seq_program;
  93. struct cs_tailq cs_tailq;
  94. struct scope_list scope_stack;
  95. symlist_t patch_functions;
  96. #if DEBUG
  97. extern int yy_flex_debug;
  98. extern int mm_flex_debug;
  99. extern int yydebug;
  100. extern int mmdebug;
  101. #endif
  102. extern FILE *yyin;
  103. extern int yyparse(void);
  104. int main(int argc, char *argv[]);
  105. int
  106. main(int argc, char *argv[])
  107. {
  108. extern char *optarg;
  109. extern int optind;
  110. int ch;
  111. int retval;
  112. char *inputfilename;
  113. scope_t *sentinal;
  114. STAILQ_INIT(&patches);
  115. SLIST_INIT(&search_path);
  116. STAILQ_INIT(&seq_program);
  117. TAILQ_INIT(&cs_tailq);
  118. SLIST_INIT(&scope_stack);
  119. /* Set Sentinal scope node */
  120. sentinal = scope_alloc();
  121. sentinal->type = SCOPE_ROOT;
  122. includes_search_curdir = 1;
  123. appname = *argv;
  124. regfile = NULL;
  125. listfile = NULL;
  126. #if DEBUG
  127. yy_flex_debug = 0;
  128. mm_flex_debug = 0;
  129. yydebug = 0;
  130. mmdebug = 0;
  131. #endif
  132. while ((ch = getopt(argc, argv, "d:i:l:n:o:p:r:I:")) != -1) {
  133. switch(ch) {
  134. case 'd':
  135. #if DEBUG
  136. if (strcmp(optarg, "s") == 0) {
  137. yy_flex_debug = 1;
  138. mm_flex_debug = 1;
  139. } else if (strcmp(optarg, "p") == 0) {
  140. yydebug = 1;
  141. mmdebug = 1;
  142. } else {
  143. fprintf(stderr, "%s: -d Requires either an "
  144. "'s' or 'p' argument\n", appname);
  145. usage();
  146. }
  147. #else
  148. stop("-d: Assembler not built with debugging "
  149. "information", EX_SOFTWARE);
  150. #endif
  151. break;
  152. case 'i':
  153. stock_include_file = optarg;
  154. break;
  155. case 'l':
  156. /* Create a program listing */
  157. if ((listfile = fopen(optarg, "w")) == NULL) {
  158. perror(optarg);
  159. stop(NULL, EX_CANTCREAT);
  160. }
  161. listfilename = optarg;
  162. break;
  163. case 'n':
  164. /* Don't complain about the -nostdinc directrive */
  165. if (strcmp(optarg, "ostdinc")) {
  166. fprintf(stderr, "%s: Unknown option -%c%s\n",
  167. appname, ch, optarg);
  168. usage();
  169. /* NOTREACHED */
  170. }
  171. break;
  172. case 'o':
  173. if ((ofile = fopen(optarg, "w")) == NULL) {
  174. perror(optarg);
  175. stop(NULL, EX_CANTCREAT);
  176. }
  177. ofilename = optarg;
  178. break;
  179. case 'p':
  180. /* Create Register Diagnostic "printing" Functions */
  181. if ((regdiagfile = fopen(optarg, "w")) == NULL) {
  182. perror(optarg);
  183. stop(NULL, EX_CANTCREAT);
  184. }
  185. regdiagfilename = optarg;
  186. break;
  187. case 'r':
  188. if ((regfile = fopen(optarg, "w")) == NULL) {
  189. perror(optarg);
  190. stop(NULL, EX_CANTCREAT);
  191. }
  192. regfilename = optarg;
  193. break;
  194. case 'I':
  195. {
  196. path_entry_t include_dir;
  197. if (strcmp(optarg, "-") == 0) {
  198. if (includes_search_curdir == 0) {
  199. fprintf(stderr, "%s: Warning - '-I-' "
  200. "specified multiple "
  201. "times\n", appname);
  202. }
  203. includes_search_curdir = 0;
  204. for (include_dir = SLIST_FIRST(&search_path);
  205. include_dir != NULL;
  206. include_dir = SLIST_NEXT(include_dir,
  207. links))
  208. /*
  209. * All entries before a '-I-' only
  210. * apply to includes specified with
  211. * quotes instead of "<>".
  212. */
  213. include_dir->quoted_includes_only = 1;
  214. } else {
  215. include_dir =
  216. (path_entry_t)malloc(sizeof(*include_dir));
  217. if (include_dir == NULL) {
  218. perror(optarg);
  219. stop(NULL, EX_OSERR);
  220. }
  221. include_dir->directory = strdup(optarg);
  222. if (include_dir->directory == NULL) {
  223. perror(optarg);
  224. stop(NULL, EX_OSERR);
  225. }
  226. include_dir->quoted_includes_only = 0;
  227. SLIST_INSERT_HEAD(&search_path, include_dir,
  228. links);
  229. }
  230. break;
  231. }
  232. case '?':
  233. default:
  234. usage();
  235. /* NOTREACHED */
  236. }
  237. }
  238. argc -= optind;
  239. argv += optind;
  240. if (argc != 1) {
  241. fprintf(stderr, "%s: No input file specifiled\n", appname);
  242. usage();
  243. /* NOTREACHED */
  244. }
  245. if (regdiagfile != NULL
  246. && (regfile == NULL || stock_include_file == NULL)) {
  247. fprintf(stderr,
  248. "%s: The -p option requires the -r and -i options.\n",
  249. appname);
  250. usage();
  251. /* NOTREACHED */
  252. }
  253. symtable_open();
  254. inputfilename = *argv;
  255. include_file(*argv, SOURCE_FILE);
  256. retval = yyparse();
  257. if (retval == 0) {
  258. if (SLIST_FIRST(&scope_stack) == NULL
  259. || SLIST_FIRST(&scope_stack)->type != SCOPE_ROOT) {
  260. stop("Unterminated conditional expression", EX_DATAERR);
  261. /* NOTREACHED */
  262. }
  263. /* Process outmost scope */
  264. process_scope(SLIST_FIRST(&scope_stack));
  265. /*
  266. * Decend the tree of scopes and insert/emit
  267. * patches as appropriate. We perform a depth first
  268. * tranversal, recursively handling each scope.
  269. */
  270. /* start at the root scope */
  271. dump_scope(SLIST_FIRST(&scope_stack));
  272. /* Patch up forward jump addresses */
  273. back_patch();
  274. if (ofile != NULL)
  275. output_code();
  276. if (regfile != NULL)
  277. symtable_dump(regfile, regdiagfile);
  278. if (listfile != NULL)
  279. output_listing(inputfilename);
  280. }
  281. stop(NULL, 0);
  282. /* NOTREACHED */
  283. return (0);
  284. }
  285. static void
  286. usage()
  287. {
  288. (void)fprintf(stderr,
  289. "usage: %-16s [-nostdinc] [-I-] [-I directory] [-o output_file]\n"
  290. " [-r register_output_file [-p register_diag_file -i includefile]]\n"
  291. " [-l program_list_file]\n"
  292. " input_file\n", appname);
  293. exit(EX_USAGE);
  294. }
  295. static void
  296. back_patch()
  297. {
  298. struct instruction *cur_instr;
  299. for (cur_instr = STAILQ_FIRST(&seq_program);
  300. cur_instr != NULL;
  301. cur_instr = STAILQ_NEXT(cur_instr, links)) {
  302. if (cur_instr->patch_label != NULL) {
  303. struct ins_format3 *f3_instr;
  304. u_int address;
  305. if (cur_instr->patch_label->type != LABEL) {
  306. char buf[255];
  307. snprintf(buf, sizeof(buf),
  308. "Undefined label %s",
  309. cur_instr->patch_label->name);
  310. stop(buf, EX_DATAERR);
  311. /* NOTREACHED */
  312. }
  313. f3_instr = &cur_instr->format.format3;
  314. address = f3_instr->address;
  315. address += cur_instr->patch_label->info.linfo->address;
  316. f3_instr->address = address;
  317. }
  318. }
  319. }
  320. static void
  321. output_code()
  322. {
  323. struct instruction *cur_instr;
  324. patch_t *cur_patch;
  325. critical_section_t *cs;
  326. symbol_node_t *cur_node;
  327. int instrcount;
  328. instrcount = 0;
  329. fprintf(ofile,
  330. "/*\n"
  331. " * DO NOT EDIT - This file is automatically generated\n"
  332. " * from the following source files:\n"
  333. " *\n"
  334. "%s */\n", versions);
  335. fprintf(ofile, "static const uint8_t seqprog[] = {\n");
  336. for (cur_instr = STAILQ_FIRST(&seq_program);
  337. cur_instr != NULL;
  338. cur_instr = STAILQ_NEXT(cur_instr, links)) {
  339. fprintf(ofile, "%s\t0x%02x, 0x%02x, 0x%02x, 0x%02x",
  340. cur_instr == STAILQ_FIRST(&seq_program) ? "" : ",\n",
  341. #ifdef __LITTLE_ENDIAN
  342. cur_instr->format.bytes[0],
  343. cur_instr->format.bytes[1],
  344. cur_instr->format.bytes[2],
  345. cur_instr->format.bytes[3]);
  346. #else
  347. cur_instr->format.bytes[3],
  348. cur_instr->format.bytes[2],
  349. cur_instr->format.bytes[1],
  350. cur_instr->format.bytes[0]);
  351. #endif
  352. instrcount++;
  353. }
  354. fprintf(ofile, "\n};\n\n");
  355. if (patch_arg_list == NULL)
  356. stop("Patch argument list not defined",
  357. EX_DATAERR);
  358. /*
  359. * Output patch information. Patch functions first.
  360. */
  361. fprintf(ofile,
  362. "typedef int %spatch_func_t (%s);\n", prefix, patch_arg_list);
  363. for (cur_node = SLIST_FIRST(&patch_functions);
  364. cur_node != NULL;
  365. cur_node = SLIST_NEXT(cur_node,links)) {
  366. fprintf(ofile,
  367. "static %spatch_func_t %spatch%d_func;\n"
  368. "\n"
  369. "static int\n"
  370. "%spatch%d_func(%s)\n"
  371. "{\n"
  372. " return (%s);\n"
  373. "}\n\n",
  374. prefix,
  375. prefix,
  376. cur_node->symbol->info.condinfo->func_num,
  377. prefix,
  378. cur_node->symbol->info.condinfo->func_num,
  379. patch_arg_list,
  380. cur_node->symbol->name);
  381. }
  382. fprintf(ofile,
  383. "static const struct patch {\n"
  384. " %spatch_func_t *patch_func;\n"
  385. " uint32_t begin :10,\n"
  386. " skip_instr :10,\n"
  387. " skip_patch :12;\n"
  388. "} patches[] = {\n", prefix);
  389. for (cur_patch = STAILQ_FIRST(&patches);
  390. cur_patch != NULL;
  391. cur_patch = STAILQ_NEXT(cur_patch,links)) {
  392. fprintf(ofile, "%s\t{ %spatch%d_func, %d, %d, %d }",
  393. cur_patch == STAILQ_FIRST(&patches) ? "" : ",\n",
  394. prefix,
  395. cur_patch->patch_func, cur_patch->begin,
  396. cur_patch->skip_instr, cur_patch->skip_patch);
  397. }
  398. fprintf(ofile, "\n};\n\n");
  399. fprintf(ofile,
  400. "static const struct cs {\n"
  401. " uint16_t begin;\n"
  402. " uint16_t end;\n"
  403. "} critical_sections[] = {\n");
  404. for (cs = TAILQ_FIRST(&cs_tailq);
  405. cs != NULL;
  406. cs = TAILQ_NEXT(cs, links)) {
  407. fprintf(ofile, "%s\t{ %d, %d }",
  408. cs == TAILQ_FIRST(&cs_tailq) ? "" : ",\n",
  409. cs->begin_addr, cs->end_addr);
  410. }
  411. fprintf(ofile, "\n};\n\n");
  412. fprintf(ofile,
  413. "static const int num_critical_sections = sizeof(critical_sections)\n"
  414. " / sizeof(*critical_sections);\n");
  415. fprintf(stderr, "%s: %d instructions used\n", appname, instrcount);
  416. }
  417. static void
  418. dump_scope(scope_t *scope)
  419. {
  420. scope_t *cur_scope;
  421. /*
  422. * Emit the first patch for this scope
  423. */
  424. emit_patch(scope, 0);
  425. /*
  426. * Dump each scope within this one.
  427. */
  428. cur_scope = TAILQ_FIRST(&scope->inner_scope);
  429. while (cur_scope != NULL) {
  430. dump_scope(cur_scope);
  431. cur_scope = TAILQ_NEXT(cur_scope, scope_links);
  432. }
  433. /*
  434. * Emit the second, closing, patch for this scope
  435. */
  436. emit_patch(scope, 1);
  437. }
  438. void
  439. emit_patch(scope_t *scope, int patch)
  440. {
  441. patch_info_t *pinfo;
  442. patch_t *new_patch;
  443. pinfo = &scope->patches[patch];
  444. if (pinfo->skip_instr == 0)
  445. /* No-Op patch */
  446. return;
  447. new_patch = (patch_t *)malloc(sizeof(*new_patch));
  448. if (new_patch == NULL)
  449. stop("Could not malloc patch structure", EX_OSERR);
  450. memset(new_patch, 0, sizeof(*new_patch));
  451. if (patch == 0) {
  452. new_patch->patch_func = scope->func_num;
  453. new_patch->begin = scope->begin_addr;
  454. } else {
  455. new_patch->patch_func = 0;
  456. new_patch->begin = scope->end_addr;
  457. }
  458. new_patch->skip_instr = pinfo->skip_instr;
  459. new_patch->skip_patch = pinfo->skip_patch;
  460. STAILQ_INSERT_TAIL(&patches, new_patch, links);
  461. }
  462. void
  463. output_listing(char *ifilename)
  464. {
  465. char buf[1024];
  466. FILE *ifile;
  467. struct instruction *cur_instr;
  468. patch_t *cur_patch;
  469. symbol_node_t *cur_func;
  470. int *func_values;
  471. int instrcount;
  472. int instrptr;
  473. int line;
  474. int func_count;
  475. int skip_addr;
  476. instrcount = 0;
  477. instrptr = 0;
  478. line = 1;
  479. skip_addr = 0;
  480. if ((ifile = fopen(ifilename, "r")) == NULL) {
  481. perror(ifilename);
  482. stop(NULL, EX_DATAERR);
  483. }
  484. /*
  485. * Determine which options to apply to this listing.
  486. */
  487. for (func_count = 0, cur_func = SLIST_FIRST(&patch_functions);
  488. cur_func != NULL;
  489. cur_func = SLIST_NEXT(cur_func, links))
  490. func_count++;
  491. func_values = NULL;
  492. if (func_count != 0) {
  493. func_values = (int *)malloc(func_count * sizeof(int));
  494. if (func_values == NULL)
  495. stop("Could not malloc", EX_OSERR);
  496. func_values[0] = 0; /* FALSE func */
  497. func_count--;
  498. /*
  499. * Ask the user to fill in the return values for
  500. * the rest of the functions.
  501. */
  502. for (cur_func = SLIST_FIRST(&patch_functions);
  503. cur_func != NULL && SLIST_NEXT(cur_func, links) != NULL;
  504. cur_func = SLIST_NEXT(cur_func, links), func_count--) {
  505. int input;
  506. fprintf(stdout, "\n(%s)\n", cur_func->symbol->name);
  507. fprintf(stdout,
  508. "Enter the return value for "
  509. "this expression[T/F]:");
  510. while (1) {
  511. input = getchar();
  512. input = toupper(input);
  513. if (input == 'T') {
  514. func_values[func_count] = 1;
  515. break;
  516. } else if (input == 'F') {
  517. func_values[func_count] = 0;
  518. break;
  519. }
  520. }
  521. if (isatty(fileno(stdin)) == 0)
  522. putchar(input);
  523. }
  524. fprintf(stdout, "\nThanks!\n");
  525. }
  526. /* Now output the listing */
  527. cur_patch = STAILQ_FIRST(&patches);
  528. for (cur_instr = STAILQ_FIRST(&seq_program);
  529. cur_instr != NULL;
  530. cur_instr = STAILQ_NEXT(cur_instr, links), instrcount++) {
  531. if (check_patch(&cur_patch, instrcount,
  532. &skip_addr, func_values) == 0) {
  533. /* Don't count this instruction as it is in a patch
  534. * that was removed.
  535. */
  536. continue;
  537. }
  538. while (line < cur_instr->srcline) {
  539. fgets(buf, sizeof(buf), ifile);
  540. fprintf(listfile, " \t%s", buf);
  541. line++;
  542. }
  543. fprintf(listfile, "%04x %02x%02x%02x%02x", instrptr,
  544. #ifdef __LITTLE_ENDIAN
  545. cur_instr->format.bytes[0],
  546. cur_instr->format.bytes[1],
  547. cur_instr->format.bytes[2],
  548. cur_instr->format.bytes[3]);
  549. #else
  550. cur_instr->format.bytes[3],
  551. cur_instr->format.bytes[2],
  552. cur_instr->format.bytes[1],
  553. cur_instr->format.bytes[0]);
  554. #endif
  555. /*
  556. * Macro expansions can cause several instructions
  557. * to be output for a single source line. Only
  558. * advance the line once in these cases.
  559. */
  560. if (line == cur_instr->srcline) {
  561. fgets(buf, sizeof(buf), ifile);
  562. fprintf(listfile, "\t%s", buf);
  563. line++;
  564. } else {
  565. fprintf(listfile, "\n");
  566. }
  567. instrptr++;
  568. }
  569. /* Dump the remainder of the file */
  570. while(fgets(buf, sizeof(buf), ifile) != NULL)
  571. fprintf(listfile, " %s", buf);
  572. fclose(ifile);
  573. }
  574. static int
  575. check_patch(patch_t **start_patch, int start_instr,
  576. int *skip_addr, int *func_vals)
  577. {
  578. patch_t *cur_patch;
  579. cur_patch = *start_patch;
  580. while (cur_patch != NULL && start_instr == cur_patch->begin) {
  581. if (func_vals[cur_patch->patch_func] == 0) {
  582. int skip;
  583. /* Start rejecting code */
  584. *skip_addr = start_instr + cur_patch->skip_instr;
  585. for (skip = cur_patch->skip_patch;
  586. skip > 0 && cur_patch != NULL;
  587. skip--)
  588. cur_patch = STAILQ_NEXT(cur_patch, links);
  589. } else {
  590. /* Accepted this patch. Advance to the next
  591. * one and wait for our intruction pointer to
  592. * hit this point.
  593. */
  594. cur_patch = STAILQ_NEXT(cur_patch, links);
  595. }
  596. }
  597. *start_patch = cur_patch;
  598. if (start_instr < *skip_addr)
  599. /* Still skipping */
  600. return (0);
  601. return (1);
  602. }
  603. /*
  604. * Print out error information if appropriate, and clean up before
  605. * terminating the program.
  606. */
  607. void
  608. stop(const char *string, int err_code)
  609. {
  610. if (string != NULL) {
  611. fprintf(stderr, "%s: ", appname);
  612. if (yyfilename != NULL) {
  613. fprintf(stderr, "Stopped at file %s, line %d - ",
  614. yyfilename, yylineno);
  615. }
  616. fprintf(stderr, "%s\n", string);
  617. }
  618. if (ofile != NULL) {
  619. fclose(ofile);
  620. if (err_code != 0) {
  621. fprintf(stderr, "%s: Removing %s due to error\n",
  622. appname, ofilename);
  623. unlink(ofilename);
  624. }
  625. }
  626. if (regfile != NULL) {
  627. fclose(regfile);
  628. if (err_code != 0) {
  629. fprintf(stderr, "%s: Removing %s due to error\n",
  630. appname, regfilename);
  631. unlink(regfilename);
  632. }
  633. }
  634. if (listfile != NULL) {
  635. fclose(listfile);
  636. if (err_code != 0) {
  637. fprintf(stderr, "%s: Removing %s due to error\n",
  638. appname, listfilename);
  639. unlink(listfilename);
  640. }
  641. }
  642. symlist_free(&patch_functions);
  643. symtable_close();
  644. exit(err_code);
  645. }
  646. struct instruction *
  647. seq_alloc()
  648. {
  649. struct instruction *new_instr;
  650. new_instr = (struct instruction *)malloc(sizeof(struct instruction));
  651. if (new_instr == NULL)
  652. stop("Unable to malloc instruction object", EX_SOFTWARE);
  653. memset(new_instr, 0, sizeof(*new_instr));
  654. STAILQ_INSERT_TAIL(&seq_program, new_instr, links);
  655. new_instr->srcline = yylineno;
  656. return new_instr;
  657. }
  658. critical_section_t *
  659. cs_alloc()
  660. {
  661. critical_section_t *new_cs;
  662. new_cs= (critical_section_t *)malloc(sizeof(critical_section_t));
  663. if (new_cs == NULL)
  664. stop("Unable to malloc critical_section object", EX_SOFTWARE);
  665. memset(new_cs, 0, sizeof(*new_cs));
  666. TAILQ_INSERT_TAIL(&cs_tailq, new_cs, links);
  667. return new_cs;
  668. }
  669. scope_t *
  670. scope_alloc()
  671. {
  672. scope_t *new_scope;
  673. new_scope = (scope_t *)malloc(sizeof(scope_t));
  674. if (new_scope == NULL)
  675. stop("Unable to malloc scope object", EX_SOFTWARE);
  676. memset(new_scope, 0, sizeof(*new_scope));
  677. TAILQ_INIT(&new_scope->inner_scope);
  678. if (SLIST_FIRST(&scope_stack) != NULL) {
  679. TAILQ_INSERT_TAIL(&SLIST_FIRST(&scope_stack)->inner_scope,
  680. new_scope, scope_links);
  681. }
  682. /* This patch is now the current scope */
  683. SLIST_INSERT_HEAD(&scope_stack, new_scope, scope_stack_links);
  684. return new_scope;
  685. }
  686. void
  687. process_scope(scope_t *scope)
  688. {
  689. /*
  690. * We are "leaving" this scope. We should now have
  691. * enough information to process the lists of scopes
  692. * we encapsulate.
  693. */
  694. scope_t *cur_scope;
  695. u_int skip_patch_count;
  696. u_int skip_instr_count;
  697. cur_scope = TAILQ_LAST(&scope->inner_scope, scope_tailq);
  698. skip_patch_count = 0;
  699. skip_instr_count = 0;
  700. while (cur_scope != NULL) {
  701. u_int patch0_patch_skip;
  702. patch0_patch_skip = 0;
  703. switch (cur_scope->type) {
  704. case SCOPE_IF:
  705. case SCOPE_ELSE_IF:
  706. if (skip_instr_count != 0) {
  707. /* Create a tail patch */
  708. patch0_patch_skip++;
  709. cur_scope->patches[1].skip_patch =
  710. skip_patch_count + 1;
  711. cur_scope->patches[1].skip_instr =
  712. skip_instr_count;
  713. }
  714. /* Count Head patch */
  715. patch0_patch_skip++;
  716. /* Count any patches contained in our inner scope */
  717. patch0_patch_skip += cur_scope->inner_scope_patches;
  718. cur_scope->patches[0].skip_patch = patch0_patch_skip;
  719. cur_scope->patches[0].skip_instr =
  720. cur_scope->end_addr - cur_scope->begin_addr;
  721. skip_instr_count += cur_scope->patches[0].skip_instr;
  722. skip_patch_count += patch0_patch_skip;
  723. if (cur_scope->type == SCOPE_IF) {
  724. scope->inner_scope_patches += skip_patch_count;
  725. skip_patch_count = 0;
  726. skip_instr_count = 0;
  727. }
  728. break;
  729. case SCOPE_ELSE:
  730. /* Count any patches contained in our innter scope */
  731. skip_patch_count += cur_scope->inner_scope_patches;
  732. skip_instr_count += cur_scope->end_addr
  733. - cur_scope->begin_addr;
  734. break;
  735. case SCOPE_ROOT:
  736. stop("Unexpected scope type encountered", EX_SOFTWARE);
  737. /* NOTREACHED */
  738. }
  739. cur_scope = TAILQ_PREV(cur_scope, scope_tailq, scope_links);
  740. }
  741. }