PageRenderTime 33ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/drivers/scsi/aic7xxx/aicasm/aicasm_gram.y

https://github.com/mturquette/linux
Happy | 2004 lines | 1783 code | 221 blank | 0 comment | 0 complexity | 287246cbd1da14cdc5a6670cbb3c96a6 MD5 | raw file
  1. %{
  2. /*
  3. * Parser for the Aic7xxx SCSI Host adapter sequencer assembler.
  4. *
  5. * Copyright (c) 1997, 1998, 2000 Justin T. Gibbs.
  6. * Copyright (c) 2001, 2002 Adaptec Inc.
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions, and the following disclaimer,
  14. * without modification.
  15. * 2. Redistributions in binary form must reproduce at minimum a disclaimer
  16. * substantially similar to the "NO WARRANTY" disclaimer below
  17. * ("Disclaimer") and any redistribution must be conditioned upon
  18. * including a substantially similar Disclaimer requirement for further
  19. * binary redistribution.
  20. * 3. Neither the names of the above-listed copyright holders nor the names
  21. * of any contributors may be used to endorse or promote products derived
  22. * from this software without specific prior written permission.
  23. *
  24. * Alternatively, this software may be distributed under the terms of the
  25. * GNU General Public License ("GPL") version 2 as published by the Free
  26. * Software Foundation.
  27. *
  28. * NO WARRANTY
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  30. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  31. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
  32. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  33. * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  34. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  35. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  36. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  37. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  38. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  39. * POSSIBILITY OF SUCH DAMAGES.
  40. *
  41. * $Id: //depot/aic7xxx/aic7xxx/aicasm/aicasm_gram.y#30 $
  42. *
  43. * $FreeBSD$
  44. */
  45. #include <sys/types.h>
  46. #include <inttypes.h>
  47. #include <regex.h>
  48. #include <stdio.h>
  49. #include <stdlib.h>
  50. #include <string.h>
  51. #include <sysexits.h>
  52. #ifdef __linux__
  53. #include "../queue.h"
  54. #else
  55. #include <sys/queue.h>
  56. #endif
  57. #include "aicasm.h"
  58. #include "aicasm_symbol.h"
  59. #include "aicasm_insformat.h"
  60. int yylineno;
  61. char *yyfilename;
  62. char stock_prefix[] = "aic_";
  63. char *prefix = stock_prefix;
  64. char *patch_arg_list;
  65. char *versions;
  66. static char errbuf[255];
  67. static char regex_pattern[255];
  68. static symbol_t *cur_symbol;
  69. static symbol_t *field_symbol;
  70. static symbol_t *scb_or_sram_symbol;
  71. static symtype cur_symtype;
  72. static symbol_ref_t accumulator;
  73. static symbol_ref_t mode_ptr;
  74. static symbol_ref_t allones;
  75. static symbol_ref_t allzeros;
  76. static symbol_ref_t none;
  77. static symbol_ref_t sindex;
  78. static int instruction_ptr;
  79. static int num_srams;
  80. static int sram_or_scb_offset;
  81. static int download_constant_count;
  82. static int in_critical_section;
  83. static u_int enum_increment;
  84. static u_int enum_next_value;
  85. static void process_field(int field_type, symbol_t *sym, int mask);
  86. static void initialize_symbol(symbol_t *symbol);
  87. static void add_macro_arg(const char *argtext, int position);
  88. static void add_macro_body(const char *bodytext);
  89. static void process_register(symbol_t **p_symbol);
  90. static void format_1_instr(int opcode, symbol_ref_t *dest,
  91. expression_t *immed, symbol_ref_t *src, int ret);
  92. static void format_2_instr(int opcode, symbol_ref_t *dest,
  93. expression_t *places, symbol_ref_t *src, int ret);
  94. static void format_3_instr(int opcode, symbol_ref_t *src,
  95. expression_t *immed, symbol_ref_t *address);
  96. static void test_readable_symbol(symbol_t *symbol);
  97. static void test_writable_symbol(symbol_t *symbol);
  98. static void type_check(symbol_ref_t *sym, expression_t *expression, int and_op);
  99. static void make_expression(expression_t *immed, int value);
  100. static void add_conditional(symbol_t *symbol);
  101. static void add_version(const char *verstring);
  102. static int is_download_const(expression_t *immed);
  103. static int is_location_address(symbol_t *symbol);
  104. void yyerror(const char *string);
  105. #define SRAM_SYMNAME "SRAM_BASE"
  106. #define SCB_SYMNAME "SCB_BASE"
  107. %}
  108. %union {
  109. u_int value;
  110. char *str;
  111. symbol_t *sym;
  112. symbol_ref_t sym_ref;
  113. expression_t expression;
  114. }
  115. %token T_REGISTER
  116. %token <value> T_CONST
  117. %token T_EXPORT
  118. %token T_DOWNLOAD
  119. %token T_SCB
  120. %token T_SRAM
  121. %token T_ALIAS
  122. %token T_SIZE
  123. %token T_EXPR_LSHIFT
  124. %token T_EXPR_RSHIFT
  125. %token <value> T_ADDRESS
  126. %token T_COUNT
  127. %token T_ACCESS_MODE
  128. %token T_DONT_GENERATE_DEBUG_CODE
  129. %token T_MODES
  130. %token T_DEFINE
  131. %token T_SET_SRC_MODE
  132. %token T_SET_DST_MODE
  133. %token <value> T_MODE
  134. %token T_BEGIN_CS
  135. %token T_END_CS
  136. %token T_PAD_PAGE
  137. %token T_FIELD
  138. %token T_ENUM
  139. %token T_MASK
  140. %token <value> T_NUMBER
  141. %token <str> T_PATH T_STRING T_ARG T_MACROBODY
  142. %token <sym> T_CEXPR
  143. %token T_EOF T_INCLUDE T_VERSION T_PREFIX T_PATCH_ARG_LIST
  144. %token <value> T_SHR T_SHL T_ROR T_ROL
  145. %token <value> T_MVI T_MOV T_CLR T_BMOV
  146. %token <value> T_JMP T_JC T_JNC T_JE T_JNE T_JNZ T_JZ T_CALL
  147. %token <value> T_ADD T_ADC
  148. %token <value> T_INC T_DEC
  149. %token <value> T_STC T_CLC
  150. %token <value> T_CMP T_NOT T_XOR
  151. %token <value> T_TEST T_AND
  152. %token <value> T_OR
  153. /* 16 bit extensions, not implemented
  154. * %token <value> T_OR16 T_AND16 T_XOR16 T_ADD16
  155. * %token <value> T_ADC16 T_MVI16 T_TEST16 T_CMP16 T_CMPXCHG
  156. */
  157. %token T_RET
  158. %token T_NOP
  159. %token T_ACCUM T_ALLONES T_ALLZEROS T_NONE T_SINDEX T_MODE_PTR
  160. %token T_A
  161. %token <sym> T_SYMBOL
  162. %token T_NL
  163. %token T_IF T_ELSE T_ELSE_IF T_ENDIF
  164. %type <sym_ref> reg_symbol address destination source opt_source
  165. %type <expression> expression immediate immediate_or_a
  166. %type <value> export ret f1_opcode f2_opcode jmp_jc_jnc_call jz_jnz je_jne
  167. %type <value> mode_value mode_list macro_arglist
  168. %left '|'
  169. %left '&'
  170. %left T_EXPR_LSHIFT T_EXPR_RSHIFT
  171. %left '+' '-'
  172. %left '*' '/'
  173. %right '~'
  174. %nonassoc UMINUS
  175. %%
  176. program:
  177. include
  178. | program include
  179. | prefix
  180. | program prefix
  181. | patch_arg_list
  182. | program patch_arg_list
  183. | version
  184. | program version
  185. | register
  186. | program register
  187. | constant
  188. | program constant
  189. | macrodefn
  190. | program macrodefn
  191. | scratch_ram
  192. | program scratch_ram
  193. | scb
  194. | program scb
  195. | label
  196. | program label
  197. | set_src_mode
  198. | program set_src_mode
  199. | set_dst_mode
  200. | program set_dst_mode
  201. | critical_section_start
  202. | program critical_section_start
  203. | critical_section_end
  204. | program critical_section_end
  205. | conditional
  206. | program conditional
  207. | code
  208. | program code
  209. ;
  210. include:
  211. T_INCLUDE '<' T_PATH '>'
  212. {
  213. include_file($3, BRACKETED_INCLUDE);
  214. }
  215. | T_INCLUDE '"' T_PATH '"'
  216. {
  217. include_file($3, QUOTED_INCLUDE);
  218. }
  219. ;
  220. prefix:
  221. T_PREFIX '=' T_STRING
  222. {
  223. if (prefix != stock_prefix)
  224. stop("Prefix multiply defined",
  225. EX_DATAERR);
  226. prefix = strdup($3);
  227. if (prefix == NULL)
  228. stop("Unable to record prefix", EX_SOFTWARE);
  229. }
  230. ;
  231. patch_arg_list:
  232. T_PATCH_ARG_LIST '=' T_STRING
  233. {
  234. if (patch_arg_list != NULL)
  235. stop("Patch argument list multiply defined",
  236. EX_DATAERR);
  237. patch_arg_list = strdup($3);
  238. if (patch_arg_list == NULL)
  239. stop("Unable to record patch arg list", EX_SOFTWARE);
  240. }
  241. ;
  242. version:
  243. T_VERSION '=' T_STRING
  244. { add_version($3); }
  245. ;
  246. register:
  247. T_REGISTER { cur_symtype = REGISTER; } reg_definition
  248. ;
  249. reg_definition:
  250. T_SYMBOL '{'
  251. {
  252. if ($1->type != UNINITIALIZED) {
  253. stop("Register multiply defined", EX_DATAERR);
  254. /* NOTREACHED */
  255. }
  256. cur_symbol = $1;
  257. cur_symbol->type = cur_symtype;
  258. initialize_symbol(cur_symbol);
  259. }
  260. reg_attribute_list
  261. '}'
  262. {
  263. /*
  264. * Default to allowing everything in for registers
  265. * with no bit or mask definitions.
  266. */
  267. if (cur_symbol->info.rinfo->valid_bitmask == 0)
  268. cur_symbol->info.rinfo->valid_bitmask = 0xFF;
  269. if (cur_symbol->info.rinfo->size == 0)
  270. cur_symbol->info.rinfo->size = 1;
  271. /*
  272. * This might be useful for registers too.
  273. */
  274. if (cur_symbol->type != REGISTER) {
  275. if (cur_symbol->info.rinfo->address == 0)
  276. cur_symbol->info.rinfo->address =
  277. sram_or_scb_offset;
  278. sram_or_scb_offset +=
  279. cur_symbol->info.rinfo->size;
  280. }
  281. cur_symbol = NULL;
  282. }
  283. ;
  284. reg_attribute_list:
  285. reg_attribute
  286. | reg_attribute_list reg_attribute
  287. ;
  288. reg_attribute:
  289. reg_address
  290. | size
  291. | count
  292. | access_mode
  293. | dont_generate_debug_code
  294. | modes
  295. | field_defn
  296. | enum_defn
  297. | mask_defn
  298. | alias
  299. | accumulator
  300. | mode_pointer
  301. | allones
  302. | allzeros
  303. | none
  304. | sindex
  305. ;
  306. reg_address:
  307. T_ADDRESS T_NUMBER
  308. {
  309. cur_symbol->info.rinfo->address = $2;
  310. }
  311. ;
  312. size:
  313. T_SIZE T_NUMBER
  314. {
  315. cur_symbol->info.rinfo->size = $2;
  316. if (scb_or_sram_symbol != NULL) {
  317. u_int max_addr;
  318. u_int sym_max_addr;
  319. max_addr = scb_or_sram_symbol->info.rinfo->address
  320. + scb_or_sram_symbol->info.rinfo->size;
  321. sym_max_addr = cur_symbol->info.rinfo->address
  322. + cur_symbol->info.rinfo->size;
  323. if (sym_max_addr > max_addr)
  324. stop("SCB or SRAM space exhausted", EX_DATAERR);
  325. }
  326. }
  327. ;
  328. count:
  329. T_COUNT T_NUMBER
  330. {
  331. cur_symbol->count += $2;
  332. }
  333. ;
  334. access_mode:
  335. T_ACCESS_MODE T_MODE
  336. {
  337. cur_symbol->info.rinfo->mode = $2;
  338. }
  339. ;
  340. dont_generate_debug_code:
  341. T_DONT_GENERATE_DEBUG_CODE
  342. {
  343. cur_symbol->dont_generate_debug_code = 1;
  344. }
  345. ;
  346. modes:
  347. T_MODES mode_list
  348. {
  349. cur_symbol->info.rinfo->modes = $2;
  350. }
  351. ;
  352. mode_list:
  353. mode_value
  354. {
  355. $$ = $1;
  356. }
  357. | mode_list ',' mode_value
  358. {
  359. $$ = $1 | $3;
  360. }
  361. ;
  362. mode_value:
  363. T_NUMBER
  364. {
  365. if ($1 > 4) {
  366. stop("Valid register modes range between 0 and 4.",
  367. EX_DATAERR);
  368. /* NOTREACHED */
  369. }
  370. $$ = (0x1 << $1);
  371. }
  372. | T_SYMBOL
  373. {
  374. symbol_t *symbol;
  375. symbol = $1;
  376. if (symbol->type != CONST) {
  377. stop("Only \"const\" symbols allowed in "
  378. "mode definitions.", EX_DATAERR);
  379. /* NOTREACHED */
  380. }
  381. if (symbol->info.cinfo->value > 4) {
  382. stop("Valid register modes range between 0 and 4.",
  383. EX_DATAERR);
  384. /* NOTREACHED */
  385. }
  386. $$ = (0x1 << symbol->info.cinfo->value);
  387. }
  388. ;
  389. field_defn:
  390. T_FIELD
  391. {
  392. field_symbol = NULL;
  393. enum_next_value = 0;
  394. enum_increment = 1;
  395. }
  396. '{' enum_entry_list '}'
  397. | T_FIELD T_SYMBOL expression
  398. {
  399. process_field(FIELD, $2, $3.value);
  400. field_symbol = $2;
  401. enum_next_value = 0;
  402. enum_increment = 0x01 << (ffs($3.value) - 1);
  403. }
  404. '{' enum_entry_list '}'
  405. | T_FIELD T_SYMBOL expression
  406. {
  407. process_field(FIELD, $2, $3.value);
  408. }
  409. ;
  410. enum_defn:
  411. T_ENUM
  412. {
  413. field_symbol = NULL;
  414. enum_next_value = 0;
  415. enum_increment = 1;
  416. }
  417. '{' enum_entry_list '}'
  418. | T_ENUM T_SYMBOL expression
  419. {
  420. process_field(ENUM, $2, $3.value);
  421. field_symbol = $2;
  422. enum_next_value = 0;
  423. enum_increment = 0x01 << (ffs($3.value) - 1);
  424. }
  425. '{' enum_entry_list '}'
  426. ;
  427. enum_entry_list:
  428. enum_entry
  429. | enum_entry_list ',' enum_entry
  430. ;
  431. enum_entry:
  432. T_SYMBOL
  433. {
  434. process_field(ENUM_ENTRY, $1, enum_next_value);
  435. enum_next_value += enum_increment;
  436. }
  437. | T_SYMBOL expression
  438. {
  439. process_field(ENUM_ENTRY, $1, $2.value);
  440. enum_next_value = $2.value + enum_increment;
  441. }
  442. ;
  443. mask_defn:
  444. T_MASK T_SYMBOL expression
  445. {
  446. process_field(MASK, $2, $3.value);
  447. }
  448. ;
  449. alias:
  450. T_ALIAS T_SYMBOL
  451. {
  452. if ($2->type != UNINITIALIZED) {
  453. stop("Re-definition of register alias",
  454. EX_DATAERR);
  455. /* NOTREACHED */
  456. }
  457. $2->type = ALIAS;
  458. initialize_symbol($2);
  459. $2->info.ainfo->parent = cur_symbol;
  460. }
  461. ;
  462. accumulator:
  463. T_ACCUM
  464. {
  465. if (accumulator.symbol != NULL) {
  466. stop("Only one accumulator definition allowed",
  467. EX_DATAERR);
  468. /* NOTREACHED */
  469. }
  470. accumulator.symbol = cur_symbol;
  471. }
  472. ;
  473. mode_pointer:
  474. T_MODE_PTR
  475. {
  476. if (mode_ptr.symbol != NULL) {
  477. stop("Only one mode pointer definition allowed",
  478. EX_DATAERR);
  479. /* NOTREACHED */
  480. }
  481. mode_ptr.symbol = cur_symbol;
  482. }
  483. ;
  484. allones:
  485. T_ALLONES
  486. {
  487. if (allones.symbol != NULL) {
  488. stop("Only one definition of allones allowed",
  489. EX_DATAERR);
  490. /* NOTREACHED */
  491. }
  492. allones.symbol = cur_symbol;
  493. }
  494. ;
  495. allzeros:
  496. T_ALLZEROS
  497. {
  498. if (allzeros.symbol != NULL) {
  499. stop("Only one definition of allzeros allowed",
  500. EX_DATAERR);
  501. /* NOTREACHED */
  502. }
  503. allzeros.symbol = cur_symbol;
  504. }
  505. ;
  506. none:
  507. T_NONE
  508. {
  509. if (none.symbol != NULL) {
  510. stop("Only one definition of none allowed",
  511. EX_DATAERR);
  512. /* NOTREACHED */
  513. }
  514. none.symbol = cur_symbol;
  515. }
  516. ;
  517. sindex:
  518. T_SINDEX
  519. {
  520. if (sindex.symbol != NULL) {
  521. stop("Only one definition of sindex allowed",
  522. EX_DATAERR);
  523. /* NOTREACHED */
  524. }
  525. sindex.symbol = cur_symbol;
  526. }
  527. ;
  528. expression:
  529. expression '|' expression
  530. {
  531. $$.value = $1.value | $3.value;
  532. symlist_merge(&$$.referenced_syms,
  533. &$1.referenced_syms,
  534. &$3.referenced_syms);
  535. }
  536. | expression '&' expression
  537. {
  538. $$.value = $1.value & $3.value;
  539. symlist_merge(&$$.referenced_syms,
  540. &$1.referenced_syms,
  541. &$3.referenced_syms);
  542. }
  543. | expression '+' expression
  544. {
  545. $$.value = $1.value + $3.value;
  546. symlist_merge(&$$.referenced_syms,
  547. &$1.referenced_syms,
  548. &$3.referenced_syms);
  549. }
  550. | expression '-' expression
  551. {
  552. $$.value = $1.value - $3.value;
  553. symlist_merge(&($$.referenced_syms),
  554. &($1.referenced_syms),
  555. &($3.referenced_syms));
  556. }
  557. | expression '*' expression
  558. {
  559. $$.value = $1.value * $3.value;
  560. symlist_merge(&($$.referenced_syms),
  561. &($1.referenced_syms),
  562. &($3.referenced_syms));
  563. }
  564. | expression '/' expression
  565. {
  566. $$.value = $1.value / $3.value;
  567. symlist_merge(&($$.referenced_syms),
  568. &($1.referenced_syms),
  569. &($3.referenced_syms));
  570. }
  571. | expression T_EXPR_LSHIFT expression
  572. {
  573. $$.value = $1.value << $3.value;
  574. symlist_merge(&$$.referenced_syms,
  575. &$1.referenced_syms,
  576. &$3.referenced_syms);
  577. }
  578. | expression T_EXPR_RSHIFT expression
  579. {
  580. $$.value = $1.value >> $3.value;
  581. symlist_merge(&$$.referenced_syms,
  582. &$1.referenced_syms,
  583. &$3.referenced_syms);
  584. }
  585. | '(' expression ')'
  586. {
  587. $$ = $2;
  588. }
  589. | '~' expression
  590. {
  591. $$ = $2;
  592. $$.value = (~$$.value) & 0xFF;
  593. }
  594. | '-' expression %prec UMINUS
  595. {
  596. $$ = $2;
  597. $$.value = -$$.value;
  598. }
  599. | T_NUMBER
  600. {
  601. $$.value = $1;
  602. SLIST_INIT(&$$.referenced_syms);
  603. }
  604. | T_SYMBOL
  605. {
  606. symbol_t *symbol;
  607. symbol = $1;
  608. switch (symbol->type) {
  609. case ALIAS:
  610. symbol = $1->info.ainfo->parent;
  611. case REGISTER:
  612. case SCBLOC:
  613. case SRAMLOC:
  614. $$.value = symbol->info.rinfo->address;
  615. break;
  616. case MASK:
  617. case FIELD:
  618. case ENUM:
  619. case ENUM_ENTRY:
  620. $$.value = symbol->info.finfo->value;
  621. break;
  622. case DOWNLOAD_CONST:
  623. case CONST:
  624. $$.value = symbol->info.cinfo->value;
  625. break;
  626. case UNINITIALIZED:
  627. default:
  628. {
  629. snprintf(errbuf, sizeof(errbuf),
  630. "Undefined symbol %s referenced",
  631. symbol->name);
  632. stop(errbuf, EX_DATAERR);
  633. /* NOTREACHED */
  634. break;
  635. }
  636. }
  637. SLIST_INIT(&$$.referenced_syms);
  638. symlist_add(&$$.referenced_syms, symbol, SYMLIST_INSERT_HEAD);
  639. }
  640. ;
  641. constant:
  642. T_CONST T_SYMBOL expression
  643. {
  644. if ($2->type != UNINITIALIZED) {
  645. stop("Re-definition of symbol as a constant",
  646. EX_DATAERR);
  647. /* NOTREACHED */
  648. }
  649. $2->type = CONST;
  650. initialize_symbol($2);
  651. $2->info.cinfo->value = $3.value;
  652. }
  653. | T_CONST T_SYMBOL T_DOWNLOAD
  654. {
  655. if ($1) {
  656. stop("Invalid downloaded constant declaration",
  657. EX_DATAERR);
  658. /* NOTREACHED */
  659. }
  660. if ($2->type != UNINITIALIZED) {
  661. stop("Re-definition of symbol as a downloaded constant",
  662. EX_DATAERR);
  663. /* NOTREACHED */
  664. }
  665. $2->type = DOWNLOAD_CONST;
  666. initialize_symbol($2);
  667. $2->info.cinfo->value = download_constant_count++;
  668. }
  669. ;
  670. macrodefn_prologue:
  671. T_DEFINE T_SYMBOL
  672. {
  673. if ($2->type != UNINITIALIZED) {
  674. stop("Re-definition of symbol as a macro",
  675. EX_DATAERR);
  676. /* NOTREACHED */
  677. }
  678. cur_symbol = $2;
  679. cur_symbol->type = MACRO;
  680. initialize_symbol(cur_symbol);
  681. }
  682. ;
  683. macrodefn:
  684. macrodefn_prologue T_MACROBODY
  685. {
  686. add_macro_body($2);
  687. }
  688. | macrodefn_prologue '(' macro_arglist ')' T_MACROBODY
  689. {
  690. add_macro_body($5);
  691. cur_symbol->info.macroinfo->narg = $3;
  692. }
  693. ;
  694. macro_arglist:
  695. {
  696. /* Macros can take no arguments */
  697. $$ = 0;
  698. }
  699. | T_ARG
  700. {
  701. $$ = 1;
  702. add_macro_arg($1, 0);
  703. }
  704. | macro_arglist ',' T_ARG
  705. {
  706. if ($1 == 0) {
  707. stop("Comma without preceding argument in arg list",
  708. EX_DATAERR);
  709. /* NOTREACHED */
  710. }
  711. $$ = $1 + 1;
  712. add_macro_arg($3, $1);
  713. }
  714. ;
  715. scratch_ram:
  716. T_SRAM '{'
  717. {
  718. snprintf(errbuf, sizeof(errbuf), "%s%d", SRAM_SYMNAME,
  719. num_srams);
  720. cur_symbol = symtable_get(SRAM_SYMNAME);
  721. cur_symtype = SRAMLOC;
  722. cur_symbol->type = SRAMLOC;
  723. initialize_symbol(cur_symbol);
  724. cur_symbol->count += 1;
  725. }
  726. reg_address
  727. {
  728. sram_or_scb_offset = cur_symbol->info.rinfo->address;
  729. }
  730. size
  731. {
  732. scb_or_sram_symbol = cur_symbol;
  733. }
  734. scb_or_sram_attributes
  735. '}'
  736. {
  737. cur_symbol = NULL;
  738. scb_or_sram_symbol = NULL;
  739. }
  740. ;
  741. scb:
  742. T_SCB '{'
  743. {
  744. cur_symbol = symtable_get(SCB_SYMNAME);
  745. cur_symtype = SCBLOC;
  746. if (cur_symbol->type != UNINITIALIZED) {
  747. stop("Only one SRAM definition allowed",
  748. EX_SOFTWARE);
  749. /* NOTREACHED */
  750. }
  751. cur_symbol->type = SCBLOC;
  752. initialize_symbol(cur_symbol);
  753. /* 64 bytes of SCB space */
  754. cur_symbol->info.rinfo->size = 64;
  755. cur_symbol->count += 1;
  756. }
  757. reg_address
  758. {
  759. sram_or_scb_offset = cur_symbol->info.rinfo->address;
  760. }
  761. size
  762. {
  763. scb_or_sram_symbol = cur_symbol;
  764. }
  765. scb_or_sram_attributes
  766. '}'
  767. {
  768. cur_symbol = NULL;
  769. scb_or_sram_symbol = NULL;
  770. }
  771. ;
  772. scb_or_sram_attributes:
  773. /* NULL definition is okay */
  774. | modes
  775. | scb_or_sram_reg_list
  776. | modes scb_or_sram_reg_list
  777. ;
  778. scb_or_sram_reg_list:
  779. reg_definition
  780. | scb_or_sram_reg_list reg_definition
  781. ;
  782. reg_symbol:
  783. T_SYMBOL
  784. {
  785. process_register(&$1);
  786. $$.symbol = $1;
  787. $$.offset = 0;
  788. }
  789. | T_SYMBOL '[' T_SYMBOL ']'
  790. {
  791. process_register(&$1);
  792. if ($3->type != CONST) {
  793. stop("register offset must be a constant", EX_DATAERR);
  794. /* NOTREACHED */
  795. }
  796. if (($3->info.cinfo->value + 1) > $1->info.rinfo->size) {
  797. stop("Accessing offset beyond range of register",
  798. EX_DATAERR);
  799. /* NOTREACHED */
  800. }
  801. $$.symbol = $1;
  802. $$.offset = $3->info.cinfo->value;
  803. }
  804. | T_SYMBOL '[' T_NUMBER ']'
  805. {
  806. process_register(&$1);
  807. if (($3 + 1) > $1->info.rinfo->size) {
  808. stop("Accessing offset beyond range of register",
  809. EX_DATAERR);
  810. /* NOTREACHED */
  811. }
  812. $$.symbol = $1;
  813. $$.offset = $3;
  814. }
  815. | T_A
  816. {
  817. if (accumulator.symbol == NULL) {
  818. stop("No accumulator has been defined", EX_DATAERR);
  819. /* NOTREACHED */
  820. }
  821. $$.symbol = accumulator.symbol;
  822. $$.offset = 0;
  823. }
  824. ;
  825. destination:
  826. reg_symbol
  827. {
  828. test_writable_symbol($1.symbol);
  829. $$ = $1;
  830. }
  831. ;
  832. immediate:
  833. expression
  834. { $$ = $1; }
  835. ;
  836. immediate_or_a:
  837. expression
  838. {
  839. if ($1.value == 0 && is_download_const(&$1) == 0) {
  840. snprintf(errbuf, sizeof(errbuf),
  841. "\nExpression evaluates to 0 and thus "
  842. "references the accumulator.\n "
  843. "If this is the desired effect, use 'A' "
  844. "instead.\n");
  845. stop(errbuf, EX_DATAERR);
  846. }
  847. $$ = $1;
  848. }
  849. | T_A
  850. {
  851. SLIST_INIT(&$$.referenced_syms);
  852. symlist_add(&$$.referenced_syms, accumulator.symbol,
  853. SYMLIST_INSERT_HEAD);
  854. $$.value = 0;
  855. }
  856. ;
  857. source:
  858. reg_symbol
  859. {
  860. test_readable_symbol($1.symbol);
  861. $$ = $1;
  862. }
  863. ;
  864. opt_source:
  865. {
  866. $$.symbol = NULL;
  867. $$.offset = 0;
  868. }
  869. | ',' source
  870. { $$ = $2; }
  871. ;
  872. ret:
  873. { $$ = 0; }
  874. | T_RET
  875. { $$ = 1; }
  876. ;
  877. set_src_mode:
  878. T_SET_SRC_MODE T_NUMBER ';'
  879. {
  880. src_mode = $2;
  881. }
  882. ;
  883. set_dst_mode:
  884. T_SET_DST_MODE T_NUMBER ';'
  885. {
  886. dst_mode = $2;
  887. }
  888. ;
  889. critical_section_start:
  890. T_BEGIN_CS ';'
  891. {
  892. critical_section_t *cs;
  893. if (in_critical_section != FALSE) {
  894. stop("Critical Section within Critical Section",
  895. EX_DATAERR);
  896. /* NOTREACHED */
  897. }
  898. cs = cs_alloc();
  899. cs->begin_addr = instruction_ptr;
  900. in_critical_section = TRUE;
  901. }
  902. ;
  903. critical_section_end:
  904. T_END_CS ';'
  905. {
  906. critical_section_t *cs;
  907. if (in_critical_section == FALSE) {
  908. stop("Unballanced 'end_cs'", EX_DATAERR);
  909. /* NOTREACHED */
  910. }
  911. cs = TAILQ_LAST(&cs_tailq, cs_tailq);
  912. cs->end_addr = instruction_ptr;
  913. in_critical_section = FALSE;
  914. }
  915. ;
  916. export:
  917. { $$ = 0; }
  918. | T_EXPORT
  919. { $$ = 1; }
  920. ;
  921. label:
  922. export T_SYMBOL ':'
  923. {
  924. if ($2->type != UNINITIALIZED) {
  925. stop("Program label multiply defined", EX_DATAERR);
  926. /* NOTREACHED */
  927. }
  928. $2->type = LABEL;
  929. initialize_symbol($2);
  930. $2->info.linfo->address = instruction_ptr;
  931. $2->info.linfo->exported = $1;
  932. }
  933. ;
  934. address:
  935. T_SYMBOL
  936. {
  937. $$.symbol = $1;
  938. $$.offset = 0;
  939. }
  940. | T_SYMBOL '+' T_NUMBER
  941. {
  942. $$.symbol = $1;
  943. $$.offset = $3;
  944. }
  945. | T_SYMBOL '-' T_NUMBER
  946. {
  947. $$.symbol = $1;
  948. $$.offset = -$3;
  949. }
  950. | '.'
  951. {
  952. $$.symbol = NULL;
  953. $$.offset = 0;
  954. }
  955. | '.' '+' T_NUMBER
  956. {
  957. $$.symbol = NULL;
  958. $$.offset = $3;
  959. }
  960. | '.' '-' T_NUMBER
  961. {
  962. $$.symbol = NULL;
  963. $$.offset = -$3;
  964. }
  965. ;
  966. conditional:
  967. T_IF T_CEXPR '{'
  968. {
  969. scope_t *new_scope;
  970. add_conditional($2);
  971. new_scope = scope_alloc();
  972. new_scope->type = SCOPE_IF;
  973. new_scope->begin_addr = instruction_ptr;
  974. new_scope->func_num = $2->info.condinfo->func_num;
  975. }
  976. | T_ELSE T_IF T_CEXPR '{'
  977. {
  978. scope_t *new_scope;
  979. scope_t *scope_context;
  980. scope_t *last_scope;
  981. /*
  982. * Ensure that the previous scope is either an
  983. * if or and else if.
  984. */
  985. scope_context = SLIST_FIRST(&scope_stack);
  986. last_scope = TAILQ_LAST(&scope_context->inner_scope,
  987. scope_tailq);
  988. if (last_scope == NULL
  989. || last_scope->type == T_ELSE) {
  990. stop("'else if' without leading 'if'", EX_DATAERR);
  991. /* NOTREACHED */
  992. }
  993. add_conditional($3);
  994. new_scope = scope_alloc();
  995. new_scope->type = SCOPE_ELSE_IF;
  996. new_scope->begin_addr = instruction_ptr;
  997. new_scope->func_num = $3->info.condinfo->func_num;
  998. }
  999. | T_ELSE '{'
  1000. {
  1001. scope_t *new_scope;
  1002. scope_t *scope_context;
  1003. scope_t *last_scope;
  1004. /*
  1005. * Ensure that the previous scope is either an
  1006. * if or and else if.
  1007. */
  1008. scope_context = SLIST_FIRST(&scope_stack);
  1009. last_scope = TAILQ_LAST(&scope_context->inner_scope,
  1010. scope_tailq);
  1011. if (last_scope == NULL
  1012. || last_scope->type == SCOPE_ELSE) {
  1013. stop("'else' without leading 'if'", EX_DATAERR);
  1014. /* NOTREACHED */
  1015. }
  1016. new_scope = scope_alloc();
  1017. new_scope->type = SCOPE_ELSE;
  1018. new_scope->begin_addr = instruction_ptr;
  1019. }
  1020. ;
  1021. conditional:
  1022. '}'
  1023. {
  1024. scope_t *scope_context;
  1025. scope_context = SLIST_FIRST(&scope_stack);
  1026. if (scope_context->type == SCOPE_ROOT) {
  1027. stop("Unexpected '}' encountered", EX_DATAERR);
  1028. /* NOTREACHED */
  1029. }
  1030. scope_context->end_addr = instruction_ptr;
  1031. /* Pop the scope */
  1032. SLIST_REMOVE_HEAD(&scope_stack, scope_stack_links);
  1033. process_scope(scope_context);
  1034. if (SLIST_FIRST(&scope_stack) == NULL) {
  1035. stop("Unexpected '}' encountered", EX_DATAERR);
  1036. /* NOTREACHED */
  1037. }
  1038. }
  1039. ;
  1040. f1_opcode:
  1041. T_AND { $$ = AIC_OP_AND; }
  1042. | T_XOR { $$ = AIC_OP_XOR; }
  1043. | T_ADD { $$ = AIC_OP_ADD; }
  1044. | T_ADC { $$ = AIC_OP_ADC; }
  1045. ;
  1046. code:
  1047. f1_opcode destination ',' immediate_or_a opt_source ret ';'
  1048. {
  1049. format_1_instr($1, &$2, &$4, &$5, $6);
  1050. }
  1051. ;
  1052. code:
  1053. T_OR reg_symbol ',' immediate_or_a opt_source ret ';'
  1054. {
  1055. format_1_instr(AIC_OP_OR, &$2, &$4, &$5, $6);
  1056. }
  1057. ;
  1058. code:
  1059. T_INC destination opt_source ret ';'
  1060. {
  1061. expression_t immed;
  1062. make_expression(&immed, 1);
  1063. format_1_instr(AIC_OP_ADD, &$2, &immed, &$3, $4);
  1064. }
  1065. ;
  1066. code:
  1067. T_DEC destination opt_source ret ';'
  1068. {
  1069. expression_t immed;
  1070. make_expression(&immed, -1);
  1071. format_1_instr(AIC_OP_ADD, &$2, &immed, &$3, $4);
  1072. }
  1073. ;
  1074. code:
  1075. T_CLC ret ';'
  1076. {
  1077. expression_t immed;
  1078. make_expression(&immed, -1);
  1079. format_1_instr(AIC_OP_ADD, &none, &immed, &allzeros, $2);
  1080. }
  1081. | T_CLC T_MVI destination ',' immediate_or_a ret ';'
  1082. {
  1083. format_1_instr(AIC_OP_ADD, &$3, &$5, &allzeros, $6);
  1084. }
  1085. ;
  1086. code:
  1087. T_STC ret ';'
  1088. {
  1089. expression_t immed;
  1090. make_expression(&immed, 1);
  1091. format_1_instr(AIC_OP_ADD, &none, &immed, &allones, $2);
  1092. }
  1093. | T_STC destination ret ';'
  1094. {
  1095. expression_t immed;
  1096. make_expression(&immed, 1);
  1097. format_1_instr(AIC_OP_ADD, &$2, &immed, &allones, $3);
  1098. }
  1099. ;
  1100. code:
  1101. T_BMOV destination ',' source ',' immediate ret ';'
  1102. {
  1103. format_1_instr(AIC_OP_BMOV, &$2, &$6, &$4, $7);
  1104. }
  1105. ;
  1106. code:
  1107. T_MOV destination ',' source ret ';'
  1108. {
  1109. expression_t immed;
  1110. make_expression(&immed, 1);
  1111. format_1_instr(AIC_OP_BMOV, &$2, &immed, &$4, $5);
  1112. }
  1113. ;
  1114. code:
  1115. T_MVI destination ',' immediate ret ';'
  1116. {
  1117. if ($4.value == 0
  1118. && is_download_const(&$4) == 0) {
  1119. expression_t immed;
  1120. /*
  1121. * Allow move immediates of 0 so that macros,
  1122. * that can't know the immediate's value and
  1123. * otherwise compensate, still work.
  1124. */
  1125. make_expression(&immed, 1);
  1126. format_1_instr(AIC_OP_BMOV, &$2, &immed, &allzeros, $5);
  1127. } else {
  1128. format_1_instr(AIC_OP_OR, &$2, &$4, &allzeros, $5);
  1129. }
  1130. }
  1131. ;
  1132. code:
  1133. T_NOT destination opt_source ret ';'
  1134. {
  1135. expression_t immed;
  1136. make_expression(&immed, 0xff);
  1137. format_1_instr(AIC_OP_XOR, &$2, &immed, &$3, $4);
  1138. }
  1139. ;
  1140. code:
  1141. T_CLR destination ret ';'
  1142. {
  1143. expression_t immed;
  1144. make_expression(&immed, 0xff);
  1145. format_1_instr(AIC_OP_AND, &$2, &immed, &allzeros, $3);
  1146. }
  1147. ;
  1148. code:
  1149. T_NOP ret ';'
  1150. {
  1151. expression_t immed;
  1152. make_expression(&immed, 0xff);
  1153. format_1_instr(AIC_OP_AND, &none, &immed, &allzeros, $2);
  1154. }
  1155. ;
  1156. code:
  1157. T_RET ';'
  1158. {
  1159. expression_t immed;
  1160. make_expression(&immed, 0xff);
  1161. format_1_instr(AIC_OP_AND, &none, &immed, &allzeros, TRUE);
  1162. }
  1163. ;
  1164. /*
  1165. * This grammar differs from the one in the aic7xxx
  1166. * reference manual since the grammar listed there is
  1167. * ambiguous and causes a shift/reduce conflict.
  1168. * It also seems more logical as the "immediate"
  1169. * argument is listed as the second arg like the
  1170. * other formats.
  1171. */
  1172. f2_opcode:
  1173. T_SHL { $$ = AIC_OP_SHL; }
  1174. | T_SHR { $$ = AIC_OP_SHR; }
  1175. | T_ROL { $$ = AIC_OP_ROL; }
  1176. | T_ROR { $$ = AIC_OP_ROR; }
  1177. ;
  1178. /*
  1179. * 16bit opcodes, not used
  1180. *
  1181. *f4_opcode:
  1182. * T_OR16 { $$ = AIC_OP_OR16; }
  1183. *| T_AND16 { $$ = AIC_OP_AND16; }
  1184. *| T_XOR16 { $$ = AIC_OP_XOR16; }
  1185. *| T_ADD16 { $$ = AIC_OP_ADD16; }
  1186. *| T_ADC16 { $$ = AIC_OP_ADC16; }
  1187. *| T_MVI16 { $$ = AIC_OP_MVI16; }
  1188. *;
  1189. */
  1190. code:
  1191. f2_opcode destination ',' expression opt_source ret ';'
  1192. {
  1193. format_2_instr($1, &$2, &$4, &$5, $6);
  1194. }
  1195. ;
  1196. jmp_jc_jnc_call:
  1197. T_JMP { $$ = AIC_OP_JMP; }
  1198. | T_JC { $$ = AIC_OP_JC; }
  1199. | T_JNC { $$ = AIC_OP_JNC; }
  1200. | T_CALL { $$ = AIC_OP_CALL; }
  1201. ;
  1202. jz_jnz:
  1203. T_JZ { $$ = AIC_OP_JZ; }
  1204. | T_JNZ { $$ = AIC_OP_JNZ; }
  1205. ;
  1206. je_jne:
  1207. T_JE { $$ = AIC_OP_JE; }
  1208. | T_JNE { $$ = AIC_OP_JNE; }
  1209. ;
  1210. code:
  1211. jmp_jc_jnc_call address ';'
  1212. {
  1213. expression_t immed;
  1214. make_expression(&immed, 0);
  1215. format_3_instr($1, &sindex, &immed, &$2);
  1216. }
  1217. ;
  1218. code:
  1219. T_OR reg_symbol ',' immediate jmp_jc_jnc_call address ';'
  1220. {
  1221. type_check(&$2, &$4, AIC_OP_OR);
  1222. format_3_instr($5, &$2, &$4, &$6);
  1223. }
  1224. ;
  1225. code:
  1226. T_TEST source ',' immediate_or_a jz_jnz address ';'
  1227. {
  1228. format_3_instr($5, &$2, &$4, &$6);
  1229. }
  1230. ;
  1231. code:
  1232. T_CMP source ',' immediate_or_a je_jne address ';'
  1233. {
  1234. format_3_instr($5, &$2, &$4, &$6);
  1235. }
  1236. ;
  1237. code:
  1238. T_MOV source jmp_jc_jnc_call address ';'
  1239. {
  1240. expression_t immed;
  1241. make_expression(&immed, 0);
  1242. format_3_instr($3, &$2, &immed, &$4);
  1243. }
  1244. ;
  1245. code:
  1246. T_MVI immediate jmp_jc_jnc_call address ';'
  1247. {
  1248. format_3_instr($3, &allzeros, &$2, &$4);
  1249. }
  1250. ;
  1251. %%
  1252. static void
  1253. process_field(int field_type, symbol_t *sym, int value)
  1254. {
  1255. /*
  1256. * Add the current register to its
  1257. * symbol list, if it already exists,
  1258. * warn if we are setting it to a
  1259. * different value, or in the bit to
  1260. * the "allowed bits" of this register.
  1261. */
  1262. if (sym->type == UNINITIALIZED) {
  1263. sym->type = field_type;
  1264. initialize_symbol(sym);
  1265. sym->info.finfo->value = value;
  1266. if (field_type != ENUM_ENTRY) {
  1267. if (field_type != MASK && value == 0) {
  1268. stop("Empty Field, or Enum", EX_DATAERR);
  1269. /* NOTREACHED */
  1270. }
  1271. sym->info.finfo->value = value;
  1272. sym->info.finfo->mask = value;
  1273. } else if (field_symbol != NULL) {
  1274. sym->info.finfo->mask = field_symbol->info.finfo->value;
  1275. } else {
  1276. sym->info.finfo->mask = 0xFF;
  1277. }
  1278. } else if (sym->type != field_type) {
  1279. stop("Field definition mirrors a definition of the same "
  1280. " name, but a different type", EX_DATAERR);
  1281. /* NOTREACHED */
  1282. } else if (value != sym->info.finfo->value) {
  1283. stop("Field redefined with a conflicting value", EX_DATAERR);
  1284. /* NOTREACHED */
  1285. }
  1286. /* Fail if this symbol is already listed */
  1287. if (symlist_search(&(sym->info.finfo->symrefs),
  1288. cur_symbol->name) != NULL) {
  1289. stop("Field defined multiple times for register", EX_DATAERR);
  1290. /* NOTREACHED */
  1291. }
  1292. symlist_add(&(sym->info.finfo->symrefs), cur_symbol,
  1293. SYMLIST_INSERT_HEAD);
  1294. cur_symbol->info.rinfo->valid_bitmask |= sym->info.finfo->mask;
  1295. cur_symbol->info.rinfo->typecheck_masks = TRUE;
  1296. symlist_add(&(cur_symbol->info.rinfo->fields), sym, SYMLIST_SORT);
  1297. }
  1298. static void
  1299. initialize_symbol(symbol_t *symbol)
  1300. {
  1301. switch (symbol->type) {
  1302. case UNINITIALIZED:
  1303. stop("Call to initialize_symbol with type field unset",
  1304. EX_SOFTWARE);
  1305. /* NOTREACHED */
  1306. break;
  1307. case REGISTER:
  1308. case SRAMLOC:
  1309. case SCBLOC:
  1310. symbol->info.rinfo =
  1311. (struct reg_info *)malloc(sizeof(struct reg_info));
  1312. if (symbol->info.rinfo == NULL) {
  1313. stop("Can't create register info", EX_SOFTWARE);
  1314. /* NOTREACHED */
  1315. }
  1316. memset(symbol->info.rinfo, 0,
  1317. sizeof(struct reg_info));
  1318. SLIST_INIT(&(symbol->info.rinfo->fields));
  1319. /*
  1320. * Default to allowing access in all register modes
  1321. * or to the mode specified by the SCB or SRAM space
  1322. * we are in.
  1323. */
  1324. if (scb_or_sram_symbol != NULL)
  1325. symbol->info.rinfo->modes =
  1326. scb_or_sram_symbol->info.rinfo->modes;
  1327. else
  1328. symbol->info.rinfo->modes = ~0;
  1329. break;
  1330. case ALIAS:
  1331. symbol->info.ainfo =
  1332. (struct alias_info *)malloc(sizeof(struct alias_info));
  1333. if (symbol->info.ainfo == NULL) {
  1334. stop("Can't create alias info", EX_SOFTWARE);
  1335. /* NOTREACHED */
  1336. }
  1337. memset(symbol->info.ainfo, 0,
  1338. sizeof(struct alias_info));
  1339. break;
  1340. case MASK:
  1341. case FIELD:
  1342. case ENUM:
  1343. case ENUM_ENTRY:
  1344. symbol->info.finfo =
  1345. (struct field_info *)malloc(sizeof(struct field_info));
  1346. if (symbol->info.finfo == NULL) {
  1347. stop("Can't create field info", EX_SOFTWARE);
  1348. /* NOTREACHED */
  1349. }
  1350. memset(symbol->info.finfo, 0, sizeof(struct field_info));
  1351. SLIST_INIT(&(symbol->info.finfo->symrefs));
  1352. break;
  1353. case CONST:
  1354. case DOWNLOAD_CONST:
  1355. symbol->info.cinfo =
  1356. (struct const_info *)malloc(sizeof(struct const_info));
  1357. if (symbol->info.cinfo == NULL) {
  1358. stop("Can't create alias info", EX_SOFTWARE);
  1359. /* NOTREACHED */
  1360. }
  1361. memset(symbol->info.cinfo, 0,
  1362. sizeof(struct const_info));
  1363. break;
  1364. case LABEL:
  1365. symbol->info.linfo =
  1366. (struct label_info *)malloc(sizeof(struct label_info));
  1367. if (symbol->info.linfo == NULL) {
  1368. stop("Can't create label info", EX_SOFTWARE);
  1369. /* NOTREACHED */
  1370. }
  1371. memset(symbol->info.linfo, 0,
  1372. sizeof(struct label_info));
  1373. break;
  1374. case CONDITIONAL:
  1375. symbol->info.condinfo =
  1376. (struct cond_info *)malloc(sizeof(struct cond_info));
  1377. if (symbol->info.condinfo == NULL) {
  1378. stop("Can't create conditional info", EX_SOFTWARE);
  1379. /* NOTREACHED */
  1380. }
  1381. memset(symbol->info.condinfo, 0,
  1382. sizeof(struct cond_info));
  1383. break;
  1384. case MACRO:
  1385. symbol->info.macroinfo =
  1386. (struct macro_info *)malloc(sizeof(struct macro_info));
  1387. if (symbol->info.macroinfo == NULL) {
  1388. stop("Can't create macro info", EX_SOFTWARE);
  1389. /* NOTREACHED */
  1390. }
  1391. memset(symbol->info.macroinfo, 0,
  1392. sizeof(struct macro_info));
  1393. STAILQ_INIT(&symbol->info.macroinfo->args);
  1394. break;
  1395. default:
  1396. stop("Call to initialize_symbol with invalid symbol type",
  1397. EX_SOFTWARE);
  1398. /* NOTREACHED */
  1399. break;
  1400. }
  1401. }
  1402. static void
  1403. add_macro_arg(const char *argtext, int argnum)
  1404. {
  1405. struct macro_arg *marg;
  1406. int i;
  1407. int retval;
  1408. if (cur_symbol == NULL || cur_symbol->type != MACRO) {
  1409. stop("Invalid current symbol for adding macro arg",
  1410. EX_SOFTWARE);
  1411. /* NOTREACHED */
  1412. }
  1413. marg = (struct macro_arg *)malloc(sizeof(*marg));
  1414. if (marg == NULL) {
  1415. stop("Can't create macro_arg structure", EX_SOFTWARE);
  1416. /* NOTREACHED */
  1417. }
  1418. marg->replacement_text = NULL;
  1419. retval = snprintf(regex_pattern, sizeof(regex_pattern),
  1420. "[^-/A-Za-z0-9_](%s)([^-/A-Za-z0-9_]|$)",
  1421. argtext);
  1422. if (retval >= sizeof(regex_pattern)) {
  1423. stop("Regex text buffer too small for arg",
  1424. EX_SOFTWARE);
  1425. /* NOTREACHED */
  1426. }
  1427. retval = regcomp(&marg->arg_regex, regex_pattern, REG_EXTENDED);
  1428. if (retval != 0) {
  1429. stop("Regex compilation failed", EX_SOFTWARE);
  1430. /* NOTREACHED */
  1431. }
  1432. STAILQ_INSERT_TAIL(&cur_symbol->info.macroinfo->args, marg, links);
  1433. }
  1434. static void
  1435. add_macro_body(const char *bodytext)
  1436. {
  1437. if (cur_symbol == NULL || cur_symbol->type != MACRO) {
  1438. stop("Invalid current symbol for adding macro arg",
  1439. EX_SOFTWARE);
  1440. /* NOTREACHED */
  1441. }
  1442. cur_symbol->info.macroinfo->body = strdup(bodytext);
  1443. if (cur_symbol->info.macroinfo->body == NULL) {
  1444. stop("Can't duplicate macro body text", EX_SOFTWARE);
  1445. /* NOTREACHED */
  1446. }
  1447. }
  1448. static void
  1449. process_register(symbol_t **p_symbol)
  1450. {
  1451. symbol_t *symbol = *p_symbol;
  1452. if (symbol->type == UNINITIALIZED) {
  1453. snprintf(errbuf, sizeof(errbuf), "Undefined register %s",
  1454. symbol->name);
  1455. stop(errbuf, EX_DATAERR);
  1456. /* NOTREACHED */
  1457. } else if (symbol->type == ALIAS) {
  1458. *p_symbol = symbol->info.ainfo->parent;
  1459. } else if ((symbol->type != REGISTER)
  1460. && (symbol->type != SCBLOC)
  1461. && (symbol->type != SRAMLOC)) {
  1462. snprintf(errbuf, sizeof(errbuf),
  1463. "Specified symbol %s is not a register",
  1464. symbol->name);
  1465. stop(errbuf, EX_DATAERR);
  1466. }
  1467. }
  1468. static void
  1469. format_1_instr(int opcode, symbol_ref_t *dest, expression_t *immed,
  1470. symbol_ref_t *src, int ret)
  1471. {
  1472. struct instruction *instr;
  1473. struct ins_format1 *f1_instr;
  1474. if (src->symbol == NULL)
  1475. src = dest;
  1476. /* Test register permissions */
  1477. test_writable_symbol(dest->symbol);
  1478. test_readable_symbol(src->symbol);
  1479. if (!is_location_address(dest->symbol)) {
  1480. /* Ensure that immediate makes sense for this destination */
  1481. type_check(dest, immed, opcode);
  1482. }
  1483. /* Allocate sequencer space for the instruction and fill it out */
  1484. instr = seq_alloc();
  1485. f1_instr = &instr->format.format1;
  1486. f1_instr->ret = ret ? 1 : 0;
  1487. f1_instr->opcode = opcode;
  1488. f1_instr->destination = dest->symbol->info.rinfo->address
  1489. + dest->offset;
  1490. f1_instr->source = src->symbol->info.rinfo->address
  1491. + src->offset;
  1492. f1_instr->immediate = immed->value;
  1493. if (is_download_const(immed))
  1494. f1_instr->parity = 1;
  1495. else if (dest->symbol == mode_ptr.symbol) {
  1496. u_int src_value;
  1497. u_int dst_value;
  1498. /*
  1499. * Attempt to update mode information if
  1500. * we are operating on the mode register.
  1501. */
  1502. if (src->symbol == allones.symbol)
  1503. src_value = 0xFF;
  1504. else if (src->symbol == allzeros.symbol)
  1505. src_value = 0;
  1506. else if (src->symbol == mode_ptr.symbol)
  1507. src_value = (dst_mode << 4) | src_mode;
  1508. else
  1509. goto cant_update;
  1510. switch (opcode) {
  1511. case AIC_OP_AND:
  1512. dst_value = src_value & immed->value;
  1513. break;
  1514. case AIC_OP_XOR:
  1515. dst_value = src_value ^ immed->value;
  1516. break;
  1517. case AIC_OP_ADD:
  1518. dst_value = (src_value + immed->value) & 0xFF;
  1519. break;
  1520. case AIC_OP_OR:
  1521. dst_value = src_value | immed->value;
  1522. break;
  1523. case AIC_OP_BMOV:
  1524. dst_value = src_value;
  1525. break;
  1526. default:
  1527. goto cant_update;
  1528. }
  1529. src_mode = dst_value & 0xF;
  1530. dst_mode = (dst_value >> 4) & 0xF;
  1531. }
  1532. cant_update:
  1533. symlist_free(&immed->referenced_syms);
  1534. instruction_ptr++;
  1535. }
  1536. static void
  1537. format_2_instr(int opcode, symbol_ref_t *dest, expression_t *places,
  1538. symbol_ref_t *src, int ret)
  1539. {
  1540. struct instruction *instr;
  1541. struct ins_format2 *f2_instr;
  1542. uint8_t shift_control;
  1543. if (src->symbol == NULL)
  1544. src = dest;
  1545. /* Test register permissions */
  1546. test_writable_symbol(dest->symbol);
  1547. test_readable_symbol(src->symbol);
  1548. /* Allocate sequencer space for the instruction and fill it out */
  1549. instr = seq_alloc();
  1550. f2_instr = &instr->format.format2;
  1551. f2_instr->ret = ret ? 1 : 0;
  1552. f2_instr->opcode = AIC_OP_ROL;
  1553. f2_instr->destination = dest->symbol->info.rinfo->address
  1554. + dest->offset;
  1555. f2_instr->source = src->symbol->info.rinfo->address
  1556. + src->offset;
  1557. if (places->value > 8 || places->value <= 0) {
  1558. stop("illegal shift value", EX_DATAERR);
  1559. /* NOTREACHED */
  1560. }
  1561. switch (opcode) {
  1562. case AIC_OP_SHL:
  1563. if (places->value == 8)
  1564. shift_control = 0xf0;
  1565. else
  1566. shift_control = (places->value << 4) | places->value;
  1567. break;
  1568. case AIC_OP_SHR:
  1569. if (places->value == 8) {
  1570. shift_control = 0xf8;
  1571. } else {
  1572. shift_control = (places->value << 4)
  1573. | (8 - places->value)
  1574. | 0x08;
  1575. }
  1576. break;
  1577. case AIC_OP_ROL:
  1578. shift_control = places->value & 0x7;
  1579. break;
  1580. case AIC_OP_ROR:
  1581. shift_control = (8 - places->value) | 0x08;
  1582. break;
  1583. default:
  1584. shift_control = 0; /* Quiet Compiler */
  1585. stop("Invalid shift operation specified", EX_SOFTWARE);
  1586. /* NOTREACHED */
  1587. break;
  1588. };
  1589. f2_instr->shift_control = shift_control;
  1590. symlist_free(&places->referenced_syms);
  1591. instruction_ptr++;
  1592. }
  1593. static void
  1594. format_3_instr(int opcode, symbol_ref_t *src,
  1595. expression_t *immed, symbol_ref_t *address)
  1596. {
  1597. struct instruction *instr;
  1598. struct ins_format3 *f3_instr;
  1599. int addr;
  1600. /* Test register permissions */
  1601. test_readable_symbol(src->symbol);
  1602. /* Allocate sequencer space for the instruction and fill it out */
  1603. instr = seq_alloc();
  1604. f3_instr = &instr->format.format3;
  1605. if (address->symbol == NULL) {
  1606. /* 'dot' reference. Use the current instruction pointer */
  1607. addr = instruction_ptr + address->offset;
  1608. } else if (address->symbol->type == UNINITIALIZED) {
  1609. /* forward reference */
  1610. addr = address->offset;
  1611. instr->patch_label = address->symbol;
  1612. } else
  1613. addr = address->symbol->info.linfo->address + address->offset;
  1614. f3_instr->opcode = opcode;
  1615. f3_instr->address = addr;
  1616. f3_instr->source = src->symbol->info.rinfo->address
  1617. + src->offset;
  1618. f3_instr->immediate = immed->value;
  1619. if (is_download_const(immed))
  1620. f3_instr->parity = 1;
  1621. symlist_free(&immed->referenced_syms);
  1622. instruction_ptr++;
  1623. }
  1624. static void
  1625. test_readable_symbol(symbol_t *symbol)
  1626. {
  1627. if ((symbol->info.rinfo->modes & (0x1 << src_mode)) == 0) {
  1628. snprintf(errbuf, sizeof(errbuf),
  1629. "Register %s unavailable in source reg mode %d",
  1630. symbol->name, src_mode);
  1631. stop(errbuf, EX_DATAERR);
  1632. }
  1633. if (symbol->info.rinfo->mode == WO) {
  1634. stop("Write Only register specified as source",
  1635. EX_DATAERR);
  1636. /* NOTREACHED */
  1637. }
  1638. }
  1639. static void
  1640. test_writable_symbol(symbol_t *symbol)
  1641. {
  1642. if ((symbol->info.rinfo->modes & (0x1 << dst_mode)) == 0) {
  1643. snprintf(errbuf, sizeof(errbuf),
  1644. "Register %s unavailable in destination reg mode %d",
  1645. symbol->name, dst_mode);
  1646. stop(errbuf, EX_DATAERR);
  1647. }
  1648. if (symbol->info.rinfo->mode == RO) {
  1649. stop("Read Only register specified as destination",
  1650. EX_DATAERR);
  1651. /* NOTREACHED */
  1652. }
  1653. }
  1654. static void
  1655. type_check(symbol_ref_t *sym, expression_t *expression, int opcode)
  1656. {
  1657. symbol_t *symbol = sym->symbol;
  1658. symbol_node_t *node;
  1659. int and_op;
  1660. int8_t value, mask;
  1661. and_op = FALSE;
  1662. /*
  1663. * Make sure that we aren't attempting to write something
  1664. * that hasn't been defined. If this is an and operation,
  1665. * this is a mask, so "undefined" bits are okay.
  1666. */
  1667. if (opcode == AIC_OP_AND || opcode == AIC_OP_JNZ ||
  1668. opcode == AIC_OP_JZ || opcode == AIC_OP_JNE ||
  1669. opcode == AIC_OP_BMOV)
  1670. and_op = TRUE;
  1671. /*
  1672. * Defaulting to 8 bit logic
  1673. */
  1674. mask = (int8_t)~symbol->info.rinfo->valid_bitmask;
  1675. value = (int8_t)expression->value;
  1676. if (and_op == FALSE && (mask & value) != 0 ) {
  1677. snprintf(errbuf, sizeof(errbuf),
  1678. "Invalid bit(s) 0x%x in immediate written to %s",
  1679. (mask & value),
  1680. symbol->name);
  1681. stop(errbuf, EX_DATAERR);
  1682. /* NOTREACHED */
  1683. }
  1684. /*
  1685. * Now make sure that all of the symbols referenced by the
  1686. * expression are defined for this register.
  1687. */
  1688. if (symbol->info.rinfo->typecheck_masks != FALSE) {
  1689. for(node = expression->referenced_syms.slh_first;
  1690. node != NULL;
  1691. node = node->links.sle_next) {
  1692. if ((node->symbol->type == MASK
  1693. || node->symbol->type == FIELD
  1694. || node->symbol->type == ENUM
  1695. || node->symbol->type == ENUM_ENTRY)
  1696. && symlist_search(&node->symbol->info.finfo->symrefs,
  1697. symbol->name) == NULL) {
  1698. snprintf(errbuf, sizeof(errbuf),
  1699. "Invalid field or mask %s "
  1700. "for register %s",
  1701. node->symbol->name, symbol->name);
  1702. stop(errbuf, EX_DATAERR);
  1703. /* NOTREACHED */
  1704. }
  1705. }
  1706. }
  1707. }
  1708. static void
  1709. make_expression(expression_t *immed, int value)
  1710. {
  1711. SLIST_INIT(&immed->referenced_syms);
  1712. immed->value = value & 0xff;
  1713. }
  1714. static void
  1715. add_conditional(symbol_t *symbol)
  1716. {
  1717. static int numfuncs;
  1718. if (numfuncs == 0) {
  1719. /* add a special conditional, "0" */
  1720. symbol_t *false_func;
  1721. false_func = symtable_get("0");
  1722. if (false_func->type != UNINITIALIZED) {
  1723. stop("Conditional expression '0' "
  1724. "conflicts with a symbol", EX_DATAERR);
  1725. /* NOTREACHED */
  1726. }
  1727. false_func->type = CONDITIONAL;
  1728. initialize_symbol(false_func);
  1729. false_func->info.condinfo->func_num = numfuncs++;
  1730. symlist_add(&patch_functions, false_func, SYMLIST_INSERT_HEAD);
  1731. }
  1732. /* This condition has occurred before */
  1733. if (symbol->type == CONDITIONAL)
  1734. return;
  1735. if (symbol->type != UNINITIALIZED) {
  1736. stop("Conditional expression conflicts with a symbol",
  1737. EX_DATAERR);
  1738. /* NOTREACHED */
  1739. }
  1740. symbol->type = CONDITIONAL;
  1741. initialize_symbol(symbol);
  1742. symbol->info.condinfo->func_num = numfuncs++;
  1743. symlist_add(&patch_functions, symbol, SYMLIST_INSERT_HEAD);
  1744. }
  1745. static void
  1746. add_version(const char *verstring)
  1747. {
  1748. const char prefix[] = " * ";
  1749. int newlen;
  1750. int oldlen;
  1751. newlen = strlen(verstring) + strlen(prefix);
  1752. oldlen = 0;
  1753. if (versions != NULL)
  1754. oldlen = strlen(versions);
  1755. versions = realloc(versions, newlen + oldlen + 2);
  1756. if (versions == NULL)
  1757. stop("Can't allocate version string", EX_SOFTWARE);
  1758. strcpy(&versions[oldlen], prefix);
  1759. strcpy(&versions[oldlen + strlen(prefix)], verstring);
  1760. versions[newlen + oldlen] = '\n';
  1761. versions[newlen + oldlen + 1] = '\0';
  1762. }
  1763. void
  1764. yyerror(const char *string)
  1765. {
  1766. stop(string, EX_DATAERR);
  1767. }
  1768. static int
  1769. is_download_const(expression_t *immed)
  1770. {
  1771. if ((immed->referenced_syms.slh_first != NULL)
  1772. && (immed->referenced_syms.slh_first->symbol->type == DOWNLOAD_CONST))
  1773. return (TRUE);
  1774. return (FALSE);
  1775. }
  1776. static int
  1777. is_location_address(symbol_t *sym)
  1778. {
  1779. if (sym->type == SCBLOC ||
  1780. sym->type == SRAMLOC)
  1781. return (TRUE);
  1782. return (FALSE);
  1783. }