PageRenderTime 46ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

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

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