aicasm_symbol.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  1. /*
  2. * Aic7xxx SCSI host adapter firmware asssembler symbol table implementation
  3. *
  4. * Copyright (c) 1997 Justin T. Gibbs.
  5. * Copyright (c) 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_symbol.c#24 $
  41. *
  42. * $FreeBSD$
  43. */
  44. #include <sys/types.h>
  45. #ifdef __linux__
  46. #include "aicdb.h"
  47. #else
  48. #include <db.h>
  49. #endif
  50. #include <fcntl.h>
  51. #include <inttypes.h>
  52. #include <regex.h>
  53. #include <stdio.h>
  54. #include <stdlib.h>
  55. #include <string.h>
  56. #include <sysexits.h>
  57. #include "aicasm_symbol.h"
  58. #include "aicasm.h"
  59. static DB *symtable;
  60. symbol_t *
  61. symbol_create(char *name)
  62. {
  63. symbol_t *new_symbol;
  64. new_symbol = (symbol_t *)malloc(sizeof(symbol_t));
  65. if (new_symbol == NULL) {
  66. perror("Unable to create new symbol");
  67. exit(EX_SOFTWARE);
  68. }
  69. memset(new_symbol, 0, sizeof(*new_symbol));
  70. new_symbol->name = strdup(name);
  71. if (new_symbol->name == NULL)
  72. stop("Unable to strdup symbol name", EX_SOFTWARE);
  73. new_symbol->type = UNINITIALIZED;
  74. return (new_symbol);
  75. }
  76. void
  77. symbol_delete(symbol_t *symbol)
  78. {
  79. if (symtable != NULL) {
  80. DBT key;
  81. key.data = symbol->name;
  82. key.size = strlen(symbol->name);
  83. symtable->del(symtable, &key, /*flags*/0);
  84. }
  85. switch(symbol->type) {
  86. case SCBLOC:
  87. case SRAMLOC:
  88. case REGISTER:
  89. if (symbol->info.rinfo != NULL)
  90. free(symbol->info.rinfo);
  91. break;
  92. case ALIAS:
  93. if (symbol->info.ainfo != NULL)
  94. free(symbol->info.ainfo);
  95. break;
  96. case MASK:
  97. case FIELD:
  98. case ENUM:
  99. case ENUM_ENTRY:
  100. if (symbol->info.finfo != NULL) {
  101. symlist_free(&symbol->info.finfo->symrefs);
  102. free(symbol->info.finfo);
  103. }
  104. break;
  105. case DOWNLOAD_CONST:
  106. case CONST:
  107. if (symbol->info.cinfo != NULL)
  108. free(symbol->info.cinfo);
  109. break;
  110. case LABEL:
  111. if (symbol->info.linfo != NULL)
  112. free(symbol->info.linfo);
  113. break;
  114. case UNINITIALIZED:
  115. default:
  116. break;
  117. }
  118. free(symbol->name);
  119. free(symbol);
  120. }
  121. void
  122. symtable_open()
  123. {
  124. symtable = dbopen(/*filename*/NULL,
  125. O_CREAT | O_NONBLOCK | O_RDWR, /*mode*/0, DB_HASH,
  126. /*openinfo*/NULL);
  127. if (symtable == NULL) {
  128. perror("Symbol table creation failed");
  129. exit(EX_SOFTWARE);
  130. /* NOTREACHED */
  131. }
  132. }
  133. void
  134. symtable_close()
  135. {
  136. if (symtable != NULL) {
  137. DBT key;
  138. DBT data;
  139. while (symtable->seq(symtable, &key, &data, R_FIRST) == 0) {
  140. symbol_t *stored_ptr;
  141. memcpy(&stored_ptr, data.data, sizeof(stored_ptr));
  142. symbol_delete(stored_ptr);
  143. }
  144. symtable->close(symtable);
  145. }
  146. }
  147. /*
  148. * The semantics of get is to return an uninitialized symbol entry
  149. * if a lookup fails.
  150. */
  151. symbol_t *
  152. symtable_get(char *name)
  153. {
  154. symbol_t *stored_ptr;
  155. DBT key;
  156. DBT data;
  157. int retval;
  158. key.data = (void *)name;
  159. key.size = strlen(name);
  160. if ((retval = symtable->get(symtable, &key, &data, /*flags*/0)) != 0) {
  161. if (retval == -1) {
  162. perror("Symbol table get operation failed");
  163. exit(EX_SOFTWARE);
  164. /* NOTREACHED */
  165. } else if (retval == 1) {
  166. /* Symbol wasn't found, so create a new one */
  167. symbol_t *new_symbol;
  168. new_symbol = symbol_create(name);
  169. data.data = &new_symbol;
  170. data.size = sizeof(new_symbol);
  171. if (symtable->put(symtable, &key, &data,
  172. /*flags*/0) !=0) {
  173. perror("Symtable put failed");
  174. exit(EX_SOFTWARE);
  175. }
  176. return (new_symbol);
  177. } else {
  178. perror("Unexpected return value from db get routine");
  179. exit(EX_SOFTWARE);
  180. /* NOTREACHED */
  181. }
  182. }
  183. memcpy(&stored_ptr, data.data, sizeof(stored_ptr));
  184. return (stored_ptr);
  185. }
  186. symbol_node_t *
  187. symlist_search(symlist_t *symlist, char *symname)
  188. {
  189. symbol_node_t *curnode;
  190. curnode = SLIST_FIRST(symlist);
  191. while(curnode != NULL) {
  192. if (strcmp(symname, curnode->symbol->name) == 0)
  193. break;
  194. curnode = SLIST_NEXT(curnode, links);
  195. }
  196. return (curnode);
  197. }
  198. void
  199. symlist_add(symlist_t *symlist, symbol_t *symbol, int how)
  200. {
  201. symbol_node_t *newnode;
  202. newnode = (symbol_node_t *)malloc(sizeof(symbol_node_t));
  203. if (newnode == NULL) {
  204. stop("symlist_add: Unable to malloc symbol_node", EX_SOFTWARE);
  205. /* NOTREACHED */
  206. }
  207. newnode->symbol = symbol;
  208. if (how == SYMLIST_SORT) {
  209. symbol_node_t *curnode;
  210. int field;
  211. field = FALSE;
  212. switch(symbol->type) {
  213. case REGISTER:
  214. case SCBLOC:
  215. case SRAMLOC:
  216. break;
  217. case FIELD:
  218. case MASK:
  219. case ENUM:
  220. case ENUM_ENTRY:
  221. field = TRUE;
  222. break;
  223. default:
  224. stop("symlist_add: Invalid symbol type for sorting",
  225. EX_SOFTWARE);
  226. /* NOTREACHED */
  227. }
  228. curnode = SLIST_FIRST(symlist);
  229. if (curnode == NULL
  230. || (field
  231. && (curnode->symbol->type > newnode->symbol->type
  232. || (curnode->symbol->type == newnode->symbol->type
  233. && (curnode->symbol->info.finfo->value >
  234. newnode->symbol->info.finfo->value))))
  235. || (!field && (curnode->symbol->info.rinfo->address >
  236. newnode->symbol->info.rinfo->address))) {
  237. SLIST_INSERT_HEAD(symlist, newnode, links);
  238. return;
  239. }
  240. while (1) {
  241. if (SLIST_NEXT(curnode, links) == NULL) {
  242. SLIST_INSERT_AFTER(curnode, newnode,
  243. links);
  244. break;
  245. } else {
  246. symbol_t *cursymbol;
  247. cursymbol = SLIST_NEXT(curnode, links)->symbol;
  248. if ((field
  249. && (cursymbol->type > symbol->type
  250. || (cursymbol->type == symbol->type
  251. && (cursymbol->info.finfo->value >
  252. symbol->info.finfo->value))))
  253. || (!field
  254. && (cursymbol->info.rinfo->address >
  255. symbol->info.rinfo->address))) {
  256. SLIST_INSERT_AFTER(curnode, newnode,
  257. links);
  258. break;
  259. }
  260. }
  261. curnode = SLIST_NEXT(curnode, links);
  262. }
  263. } else {
  264. SLIST_INSERT_HEAD(symlist, newnode, links);
  265. }
  266. }
  267. void
  268. symlist_free(symlist_t *symlist)
  269. {
  270. symbol_node_t *node1, *node2;
  271. node1 = SLIST_FIRST(symlist);
  272. while (node1 != NULL) {
  273. node2 = SLIST_NEXT(node1, links);
  274. free(node1);
  275. node1 = node2;
  276. }
  277. SLIST_INIT(symlist);
  278. }
  279. void
  280. symlist_merge(symlist_t *symlist_dest, symlist_t *symlist_src1,
  281. symlist_t *symlist_src2)
  282. {
  283. symbol_node_t *node;
  284. *symlist_dest = *symlist_src1;
  285. while((node = SLIST_FIRST(symlist_src2)) != NULL) {
  286. SLIST_REMOVE_HEAD(symlist_src2, links);
  287. SLIST_INSERT_HEAD(symlist_dest, node, links);
  288. }
  289. /* These are now empty */
  290. SLIST_INIT(symlist_src1);
  291. SLIST_INIT(symlist_src2);
  292. }
  293. void
  294. aic_print_file_prologue(FILE *ofile)
  295. {
  296. if (ofile == NULL)
  297. return;
  298. fprintf(ofile,
  299. "/*\n"
  300. " * DO NOT EDIT - This file is automatically generated\n"
  301. " * from the following source files:\n"
  302. " *\n"
  303. "%s */\n",
  304. versions);
  305. }
  306. void
  307. aic_print_include(FILE *dfile, char *include_file)
  308. {
  309. if (dfile == NULL)
  310. return;
  311. fprintf(dfile, "\n#include \"%s\"\n\n", include_file);
  312. }
  313. void
  314. aic_print_reg_dump_types(FILE *ofile)
  315. {
  316. if (ofile == NULL)
  317. return;
  318. fprintf(ofile,
  319. "typedef int (%sreg_print_t)(u_int, u_int *, u_int);\n"
  320. "typedef struct %sreg_parse_entry {\n"
  321. " char *name;\n"
  322. " uint8_t value;\n"
  323. " uint8_t mask;\n"
  324. "} %sreg_parse_entry_t;\n"
  325. "\n",
  326. prefix, prefix, prefix);
  327. }
  328. static void
  329. aic_print_reg_dump_start(FILE *dfile, symbol_node_t *regnode)
  330. {
  331. if (dfile == NULL)
  332. return;
  333. fprintf(dfile,
  334. "static %sreg_parse_entry_t %s_parse_table[] = {\n",
  335. prefix,
  336. regnode->symbol->name);
  337. }
  338. static void
  339. aic_print_reg_dump_end(FILE *ofile, FILE *dfile,
  340. symbol_node_t *regnode, u_int num_entries)
  341. {
  342. char *lower_name;
  343. char *letter;
  344. lower_name = strdup(regnode->symbol->name);
  345. if (lower_name == NULL)
  346. stop("Unable to strdup symbol name", EX_SOFTWARE);
  347. for (letter = lower_name; *letter != '\0'; letter++)
  348. *letter = tolower(*letter);
  349. if (dfile != NULL) {
  350. if (num_entries != 0)
  351. fprintf(dfile,
  352. "\n"
  353. "};\n"
  354. "\n");
  355. fprintf(dfile,
  356. "int\n"
  357. "%s%s_print(u_int regvalue, u_int *cur_col, u_int wrap)\n"
  358. "{\n"
  359. " return (%sprint_register(%s%s, %d, \"%s\",\n"
  360. " 0x%02x, regvalue, cur_col, wrap));\n"
  361. "}\n"
  362. "\n",
  363. prefix,
  364. lower_name,
  365. prefix,
  366. num_entries != 0 ? regnode->symbol->name : "NULL",
  367. num_entries != 0 ? "_parse_table" : "",
  368. num_entries,
  369. regnode->symbol->name,
  370. regnode->symbol->info.rinfo->address);
  371. }
  372. fprintf(ofile,
  373. "#if AIC_DEBUG_REGISTERS\n"
  374. "%sreg_print_t %s%s_print;\n"
  375. "#else\n"
  376. "#define %s%s_print(regvalue, cur_col, wrap) \\\n"
  377. " %sprint_register(NULL, 0, \"%s\", 0x%02x, regvalue, cur_col, wrap)\n"
  378. "#endif\n"
  379. "\n",
  380. prefix,
  381. prefix,
  382. lower_name,
  383. prefix,
  384. lower_name,
  385. prefix,
  386. regnode->symbol->name,
  387. regnode->symbol->info.rinfo->address);
  388. }
  389. static void
  390. aic_print_reg_dump_entry(FILE *dfile, symbol_node_t *curnode)
  391. {
  392. int num_tabs;
  393. if (dfile == NULL)
  394. return;
  395. fprintf(dfile,
  396. " { \"%s\",",
  397. curnode->symbol->name);
  398. num_tabs = 3 - (strlen(curnode->symbol->name) + 5) / 8;
  399. while (num_tabs-- > 0)
  400. fputc('\t', dfile);
  401. fprintf(dfile, "0x%02x, 0x%02x }",
  402. curnode->symbol->info.finfo->value,
  403. curnode->symbol->info.finfo->mask);
  404. }
  405. void
  406. symtable_dump(FILE *ofile, FILE *dfile)
  407. {
  408. /*
  409. * Sort the registers by address with a simple insertion sort.
  410. * Put bitmasks next to the first register that defines them.
  411. * Put constants at the end.
  412. */
  413. symlist_t registers;
  414. symlist_t masks;
  415. symlist_t constants;
  416. symlist_t download_constants;
  417. symlist_t aliases;
  418. symlist_t exported_labels;
  419. symbol_node_t *curnode;
  420. symbol_node_t *regnode;
  421. DBT key;
  422. DBT data;
  423. int flag;
  424. u_int i;
  425. if (symtable == NULL)
  426. return;
  427. SLIST_INIT(&registers);
  428. SLIST_INIT(&masks);
  429. SLIST_INIT(&constants);
  430. SLIST_INIT(&download_constants);
  431. SLIST_INIT(&aliases);
  432. SLIST_INIT(&exported_labels);
  433. flag = R_FIRST;
  434. while (symtable->seq(symtable, &key, &data, flag) == 0) {
  435. symbol_t *cursym;
  436. memcpy(&cursym, data.data, sizeof(cursym));
  437. switch(cursym->type) {
  438. case REGISTER:
  439. case SCBLOC:
  440. case SRAMLOC:
  441. symlist_add(&registers, cursym, SYMLIST_SORT);
  442. break;
  443. case MASK:
  444. case FIELD:
  445. case ENUM:
  446. case ENUM_ENTRY:
  447. symlist_add(&masks, cursym, SYMLIST_SORT);
  448. break;
  449. case CONST:
  450. symlist_add(&constants, cursym,
  451. SYMLIST_INSERT_HEAD);
  452. break;
  453. case DOWNLOAD_CONST:
  454. symlist_add(&download_constants, cursym,
  455. SYMLIST_INSERT_HEAD);
  456. break;
  457. case ALIAS:
  458. symlist_add(&aliases, cursym,
  459. SYMLIST_INSERT_HEAD);
  460. break;
  461. case LABEL:
  462. if (cursym->info.linfo->exported == 0)
  463. break;
  464. symlist_add(&exported_labels, cursym,
  465. SYMLIST_INSERT_HEAD);
  466. break;
  467. default:
  468. break;
  469. }
  470. flag = R_NEXT;
  471. }
  472. /* Register dianostic functions/declarations first. */
  473. aic_print_file_prologue(ofile);
  474. aic_print_reg_dump_types(ofile);
  475. aic_print_file_prologue(dfile);
  476. aic_print_include(dfile, stock_include_file);
  477. SLIST_FOREACH(curnode, &registers, links) {
  478. switch(curnode->symbol->type) {
  479. case REGISTER:
  480. case SCBLOC:
  481. case SRAMLOC:
  482. {
  483. symlist_t *fields;
  484. symbol_node_t *fieldnode;
  485. int num_entries;
  486. num_entries = 0;
  487. fields = &curnode->symbol->info.rinfo->fields;
  488. SLIST_FOREACH(fieldnode, fields, links) {
  489. if (num_entries == 0)
  490. aic_print_reg_dump_start(dfile,
  491. curnode);
  492. else if (dfile != NULL)
  493. fputs(",\n", dfile);
  494. num_entries++;
  495. aic_print_reg_dump_entry(dfile, fieldnode);
  496. }
  497. aic_print_reg_dump_end(ofile, dfile,
  498. curnode, num_entries);
  499. }
  500. default:
  501. break;
  502. }
  503. }
  504. /* Fold in the masks and bits */
  505. while (SLIST_FIRST(&masks) != NULL) {
  506. char *regname;
  507. curnode = SLIST_FIRST(&masks);
  508. SLIST_REMOVE_HEAD(&masks, links);
  509. regnode = SLIST_FIRST(&curnode->symbol->info.finfo->symrefs);
  510. regname = regnode->symbol->name;
  511. regnode = symlist_search(&registers, regname);
  512. SLIST_INSERT_AFTER(regnode, curnode, links);
  513. }
  514. /* Add the aliases */
  515. while (SLIST_FIRST(&aliases) != NULL) {
  516. char *regname;
  517. curnode = SLIST_FIRST(&aliases);
  518. SLIST_REMOVE_HEAD(&aliases, links);
  519. regname = curnode->symbol->info.ainfo->parent->name;
  520. regnode = symlist_search(&registers, regname);
  521. SLIST_INSERT_AFTER(regnode, curnode, links);
  522. }
  523. /* Output generated #defines. */
  524. while (SLIST_FIRST(&registers) != NULL) {
  525. symbol_node_t *curnode;
  526. u_int value;
  527. char *tab_str;
  528. char *tab_str2;
  529. curnode = SLIST_FIRST(&registers);
  530. SLIST_REMOVE_HEAD(&registers, links);
  531. switch(curnode->symbol->type) {
  532. case REGISTER:
  533. case SCBLOC:
  534. case SRAMLOC:
  535. fprintf(ofile, "\n");
  536. value = curnode->symbol->info.rinfo->address;
  537. tab_str = "\t";
  538. tab_str2 = "\t\t";
  539. break;
  540. case ALIAS:
  541. {
  542. symbol_t *parent;
  543. parent = curnode->symbol->info.ainfo->parent;
  544. value = parent->info.rinfo->address;
  545. tab_str = "\t";
  546. tab_str2 = "\t\t";
  547. break;
  548. }
  549. case MASK:
  550. case FIELD:
  551. case ENUM:
  552. case ENUM_ENTRY:
  553. value = curnode->symbol->info.finfo->value;
  554. tab_str = "\t\t";
  555. tab_str2 = "\t";
  556. break;
  557. default:
  558. value = 0; /* Quiet compiler */
  559. tab_str = NULL;
  560. tab_str2 = NULL;
  561. stop("symtable_dump: Invalid symbol type "
  562. "encountered", EX_SOFTWARE);
  563. break;
  564. }
  565. fprintf(ofile, "#define%s%-16s%s0x%02x\n",
  566. tab_str, curnode->symbol->name, tab_str2,
  567. value);
  568. free(curnode);
  569. }
  570. fprintf(ofile, "\n\n");
  571. while (SLIST_FIRST(&constants) != NULL) {
  572. symbol_node_t *curnode;
  573. curnode = SLIST_FIRST(&constants);
  574. SLIST_REMOVE_HEAD(&constants, links);
  575. fprintf(ofile, "#define\t%-8s\t0x%02x\n",
  576. curnode->symbol->name,
  577. curnode->symbol->info.cinfo->value);
  578. free(curnode);
  579. }
  580. fprintf(ofile, "\n\n/* Downloaded Constant Definitions */\n");
  581. for (i = 0; SLIST_FIRST(&download_constants) != NULL; i++) {
  582. symbol_node_t *curnode;
  583. curnode = SLIST_FIRST(&download_constants);
  584. SLIST_REMOVE_HEAD(&download_constants, links);
  585. fprintf(ofile, "#define\t%-8s\t0x%02x\n",
  586. curnode->symbol->name,
  587. curnode->symbol->info.cinfo->value);
  588. free(curnode);
  589. }
  590. fprintf(ofile, "#define\tDOWNLOAD_CONST_COUNT\t0x%02x\n", i);
  591. fprintf(ofile, "\n\n/* Exported Labels */\n");
  592. while (SLIST_FIRST(&exported_labels) != NULL) {
  593. symbol_node_t *curnode;
  594. curnode = SLIST_FIRST(&exported_labels);
  595. SLIST_REMOVE_HEAD(&exported_labels, links);
  596. fprintf(ofile, "#define\tLABEL_%-8s\t0x%02x\n",
  597. curnode->symbol->name,
  598. curnode->symbol->info.linfo->address);
  599. free(curnode);
  600. }
  601. }