aicasm_gram.y 40 KB

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