aicasm_gram.y 41 KB

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