aicasm_gram.y 40 KB

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