aicasm_gram.y 41 KB

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