PageRenderTime 56ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

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

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