parser.y 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505
  1. %{
  2. /*
  3. * Copyright (C) 2006-2010 Michael Buesch <m@bues.ch>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2
  7. * as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #include "main.h"
  15. #include "initvals.h"
  16. #include "util.h"
  17. #include <stdlib.h>
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include <stdint.h>
  21. extern char *yytext;
  22. extern void yyerror(const char *);
  23. extern int yyparse(void);
  24. extern int yylex(void);
  25. static struct operand * store_oper_sanity(struct operand *oper);
  26. static void assembler_assertion_failed(void);
  27. /* The current .section */
  28. extern int section;
  29. /* Pointer to the current initvals section data structure. */
  30. extern struct initvals_sect *cur_initvals_sect;
  31. %}
  32. %token SECTION_TEXT SECTION_IVALS
  33. %token ASM_ARCH ASM_START ASM_ASSERT SPR GPR OFFR LR COMMA SEMICOLON BRACK_OPEN BRACK_CLOSE PAREN_OPEN PAREN_CLOSE HEXNUM DECNUM ARCH_NEWWORLD ARCH_OLDWORLD LABEL IDENT LABELREF
  34. %token EQUAL NOT_EQUAL LOGICAL_OR LOGICAL_AND PLUS MINUS MULTIPLY DIVIDE BITW_OR BITW_AND BITW_XOR BITW_NOT LEFTSHIFT RIGHTSHIFT
  35. %token OP_MUL OP_ADD OP_ADDSC OP_ADDC OP_ADDSCC OP_SUB OP_SUBSC OP_SUBC OP_SUBSCC OP_SRA OP_OR OP_AND OP_XOR OP_SR OP_SRX OP_SL OP_RL OP_RR OP_NAND OP_ORX OP_MOV OP_JMP OP_JAND OP_JNAND OP_JS OP_JNS OP_JE OP_JNE OP_JLS OP_JGES OP_JGS OP_JLES OP_JL OP_JGE OP_JG OP_JLE OP_JZX OP_JNZX OP_JEXT OP_JNEXT OP_JDN OP_JDPZ OP_JDP OP_JDNZ OP_CALL OP_CALLS OP_RET OP_RETS OP_TKIPH OP_TKIPHS OP_TKIPL OP_TKIPLS OP_NAP RAW_CODE
  36. %token IVAL_MMIO16 IVAL_MMIO32 IVAL_PHY IVAL_RADIO IVAL_SHM16 IVAL_SHM32 IVAL_TRAM
  37. %start line
  38. %%
  39. line : line_terminator {
  40. /* empty */
  41. }
  42. | line statement line_terminator {
  43. struct statement *s = $2;
  44. if (s) {
  45. if (section != SECTION_TEXT)
  46. yyerror("Microcode text instruction in non .text section");
  47. memcpy(&s->info, &cur_lineinfo, sizeof(struct lineinfo));
  48. list_add_tail(&s->list, &infile.sl);
  49. }
  50. }
  51. | line section_switch line_terminator {
  52. }
  53. | line ivals_write line_terminator {
  54. struct initval_op *io = $2;
  55. if (section != SECTION_IVALS)
  56. yyerror("InitVals write in non .initvals section");
  57. memcpy(&io->info, &cur_lineinfo, sizeof(struct lineinfo));
  58. INIT_LIST_HEAD(&io->list);
  59. list_add_tail(&io->list, &cur_initvals_sect->ops);
  60. }
  61. ;
  62. /* Allow terminating lines with the ";" char */
  63. line_terminator : /* Nothing */
  64. | SEMICOLON line_terminator
  65. ;
  66. section_switch : SECTION_TEXT {
  67. section = SECTION_TEXT;
  68. }
  69. | SECTION_IVALS PAREN_OPEN identifier PAREN_CLOSE {
  70. const char *sectname = $3;
  71. struct initvals_sect *s;
  72. cur_initvals_sect = NULL;
  73. /* Search if there already is a section by that name. */
  74. list_for_each_entry(s, &infile.ivals, list) {
  75. if (strcmp(sectname, s->name) == 0)
  76. cur_initvals_sect = s;
  77. }
  78. if (!cur_initvals_sect) {
  79. /* Not found, create a new one. */
  80. s = xmalloc(sizeof(struct initvals_sect));
  81. s->name = sectname;
  82. INIT_LIST_HEAD(&s->ops);
  83. INIT_LIST_HEAD(&s->list);
  84. list_add_tail(&s->list, &infile.ivals);
  85. cur_initvals_sect = s;
  86. }
  87. section = SECTION_IVALS;
  88. }
  89. ;
  90. ivals_write : IVAL_MMIO16 imm_value COMMA imm_value {
  91. struct initval_op *iop = xmalloc(sizeof(struct initval_op));
  92. iop->type = IVAL_W_MMIO16;
  93. iop->args[0] = (unsigned int)(unsigned long)$2;
  94. iop->args[1] = (unsigned int)(unsigned long)$4;
  95. $$ = iop;
  96. }
  97. | IVAL_MMIO32 imm_value COMMA imm_value {
  98. struct initval_op *iop = xmalloc(sizeof(struct initval_op));
  99. iop->type = IVAL_W_MMIO32;
  100. iop->args[0] = (unsigned int)(unsigned long)$2;
  101. iop->args[1] = (unsigned int)(unsigned long)$4;
  102. $$ = iop;
  103. }
  104. | IVAL_PHY imm_value COMMA imm_value {
  105. struct initval_op *iop = xmalloc(sizeof(struct initval_op));
  106. iop->type = IVAL_W_PHY;
  107. iop->args[0] = (unsigned int)(unsigned long)$2;
  108. iop->args[1] = (unsigned int)(unsigned long)$4;
  109. $$ = iop;
  110. }
  111. | IVAL_RADIO imm_value COMMA imm_value {
  112. struct initval_op *iop = xmalloc(sizeof(struct initval_op));
  113. iop->type = IVAL_W_RADIO;
  114. iop->args[0] = (unsigned int)(unsigned long)$2;
  115. iop->args[1] = (unsigned int)(unsigned long)$4;
  116. $$ = iop;
  117. }
  118. | IVAL_SHM16 imm_value COMMA imm_value COMMA imm_value {
  119. struct initval_op *iop = xmalloc(sizeof(struct initval_op));
  120. iop->type = IVAL_W_SHM16;
  121. iop->args[0] = (unsigned int)(unsigned long)$2;
  122. iop->args[1] = (unsigned int)(unsigned long)$4;
  123. iop->args[2] = (unsigned int)(unsigned long)$6;
  124. $$ = iop;
  125. }
  126. | IVAL_SHM32 imm_value COMMA imm_value COMMA imm_value {
  127. struct initval_op *iop = xmalloc(sizeof(struct initval_op));
  128. iop->type = IVAL_W_SHM32;
  129. iop->args[0] = (unsigned int)(unsigned long)$2;
  130. iop->args[1] = (unsigned int)(unsigned long)$4;
  131. iop->args[2] = (unsigned int)(unsigned long)$6;
  132. $$ = iop;
  133. }
  134. | IVAL_TRAM imm_value COMMA imm_value {
  135. struct initval_op *iop = xmalloc(sizeof(struct initval_op));
  136. iop->type = IVAL_W_TRAM;
  137. iop->args[0] = (unsigned int)(unsigned long)$2;
  138. iop->args[1] = (unsigned int)(unsigned long)$4;
  139. $$ = iop;
  140. }
  141. ;
  142. statement : asmdir {
  143. struct asmdir *ad = $1;
  144. if (ad) {
  145. struct statement *s = xmalloc(sizeof(struct statement));
  146. INIT_LIST_HEAD(&s->list);
  147. s->type = STMT_ASMDIR;
  148. s->u.asmdir = $1;
  149. $$ = s;
  150. } else
  151. $$ = NULL;
  152. }
  153. | label {
  154. struct statement *s = xmalloc(sizeof(struct statement));
  155. INIT_LIST_HEAD(&s->list);
  156. s->type = STMT_LABEL;
  157. s->u.label = $1;
  158. $$ = s;
  159. }
  160. | insn_mul {
  161. struct statement *s = xmalloc(sizeof(struct statement));
  162. INIT_LIST_HEAD(&s->list);
  163. s->type = STMT_INSN;
  164. s->u.insn = $1;
  165. $$ = s;
  166. }
  167. | insn_add {
  168. struct statement *s = xmalloc(sizeof(struct statement));
  169. INIT_LIST_HEAD(&s->list);
  170. s->type = STMT_INSN;
  171. s->u.insn = $1;
  172. $$ = s;
  173. }
  174. | insn_addsc {
  175. struct statement *s = xmalloc(sizeof(struct statement));
  176. INIT_LIST_HEAD(&s->list);
  177. s->type = STMT_INSN;
  178. s->u.insn = $1;
  179. $$ = s;
  180. }
  181. | insn_addc {
  182. struct statement *s = xmalloc(sizeof(struct statement));
  183. INIT_LIST_HEAD(&s->list);
  184. s->type = STMT_INSN;
  185. s->u.insn = $1;
  186. $$ = s;
  187. }
  188. | insn_addscc {
  189. struct statement *s = xmalloc(sizeof(struct statement));
  190. INIT_LIST_HEAD(&s->list);
  191. s->type = STMT_INSN;
  192. s->u.insn = $1;
  193. $$ = s;
  194. }
  195. | insn_sub {
  196. struct statement *s = xmalloc(sizeof(struct statement));
  197. INIT_LIST_HEAD(&s->list);
  198. s->type = STMT_INSN;
  199. s->u.insn = $1;
  200. $$ = s;
  201. }
  202. | insn_subsc {
  203. struct statement *s = xmalloc(sizeof(struct statement));
  204. INIT_LIST_HEAD(&s->list);
  205. s->type = STMT_INSN;
  206. s->u.insn = $1;
  207. $$ = s;
  208. }
  209. | insn_subc {
  210. struct statement *s = xmalloc(sizeof(struct statement));
  211. INIT_LIST_HEAD(&s->list);
  212. s->type = STMT_INSN;
  213. s->u.insn = $1;
  214. $$ = s;
  215. }
  216. | insn_subscc {
  217. struct statement *s = xmalloc(sizeof(struct statement));
  218. INIT_LIST_HEAD(&s->list);
  219. s->type = STMT_INSN;
  220. s->u.insn = $1;
  221. $$ = s;
  222. }
  223. | insn_sra {
  224. struct statement *s = xmalloc(sizeof(struct statement));
  225. INIT_LIST_HEAD(&s->list);
  226. s->type = STMT_INSN;
  227. s->u.insn = $1;
  228. $$ = s;
  229. }
  230. | insn_or {
  231. struct statement *s = xmalloc(sizeof(struct statement));
  232. INIT_LIST_HEAD(&s->list);
  233. s->type = STMT_INSN;
  234. s->u.insn = $1;
  235. $$ = s;
  236. }
  237. | insn_and {
  238. struct statement *s = xmalloc(sizeof(struct statement));
  239. INIT_LIST_HEAD(&s->list);
  240. s->type = STMT_INSN;
  241. s->u.insn = $1;
  242. $$ = s;
  243. }
  244. | insn_xor {
  245. struct statement *s = xmalloc(sizeof(struct statement));
  246. INIT_LIST_HEAD(&s->list);
  247. s->type = STMT_INSN;
  248. s->u.insn = $1;
  249. $$ = s;
  250. }
  251. | insn_sr {
  252. struct statement *s = xmalloc(sizeof(struct statement));
  253. INIT_LIST_HEAD(&s->list);
  254. s->type = STMT_INSN;
  255. s->u.insn = $1;
  256. $$ = s;
  257. }
  258. | insn_srx {
  259. struct statement *s = xmalloc(sizeof(struct statement));
  260. INIT_LIST_HEAD(&s->list);
  261. s->type = STMT_INSN;
  262. s->u.insn = $1;
  263. $$ = s;
  264. }
  265. | insn_sl {
  266. struct statement *s = xmalloc(sizeof(struct statement));
  267. INIT_LIST_HEAD(&s->list);
  268. s->type = STMT_INSN;
  269. s->u.insn = $1;
  270. $$ = s;
  271. }
  272. | insn_rl {
  273. struct statement *s = xmalloc(sizeof(struct statement));
  274. INIT_LIST_HEAD(&s->list);
  275. s->type = STMT_INSN;
  276. s->u.insn = $1;
  277. $$ = s;
  278. }
  279. | insn_rr {
  280. struct statement *s = xmalloc(sizeof(struct statement));
  281. INIT_LIST_HEAD(&s->list);
  282. s->type = STMT_INSN;
  283. s->u.insn = $1;
  284. $$ = s;
  285. }
  286. | insn_nand {
  287. struct statement *s = xmalloc(sizeof(struct statement));
  288. INIT_LIST_HEAD(&s->list);
  289. s->type = STMT_INSN;
  290. s->u.insn = $1;
  291. $$ = s;
  292. }
  293. | insn_orx {
  294. struct statement *s = xmalloc(sizeof(struct statement));
  295. INIT_LIST_HEAD(&s->list);
  296. s->type = STMT_INSN;
  297. s->u.insn = $1;
  298. $$ = s;
  299. }
  300. | insn_mov {
  301. struct statement *s = xmalloc(sizeof(struct statement));
  302. INIT_LIST_HEAD(&s->list);
  303. s->type = STMT_INSN;
  304. s->u.insn = $1;
  305. $$ = s;
  306. }
  307. | insn_jmp {
  308. struct statement *s = xmalloc(sizeof(struct statement));
  309. INIT_LIST_HEAD(&s->list);
  310. s->type = STMT_INSN;
  311. s->u.insn = $1;
  312. $$ = s;
  313. }
  314. | insn_jand {
  315. struct statement *s = xmalloc(sizeof(struct statement));
  316. INIT_LIST_HEAD(&s->list);
  317. s->type = STMT_INSN;
  318. s->u.insn = $1;
  319. $$ = s;
  320. }
  321. | insn_jnand {
  322. struct statement *s = xmalloc(sizeof(struct statement));
  323. INIT_LIST_HEAD(&s->list);
  324. s->type = STMT_INSN;
  325. s->u.insn = $1;
  326. $$ = s;
  327. }
  328. | insn_js {
  329. struct statement *s = xmalloc(sizeof(struct statement));
  330. INIT_LIST_HEAD(&s->list);
  331. s->type = STMT_INSN;
  332. s->u.insn = $1;
  333. $$ = s;
  334. }
  335. | insn_jns {
  336. struct statement *s = xmalloc(sizeof(struct statement));
  337. INIT_LIST_HEAD(&s->list);
  338. s->type = STMT_INSN;
  339. s->u.insn = $1;
  340. $$ = s;
  341. }
  342. | insn_je {
  343. struct statement *s = xmalloc(sizeof(struct statement));
  344. INIT_LIST_HEAD(&s->list);
  345. s->type = STMT_INSN;
  346. s->u.insn = $1;
  347. $$ = s;
  348. }
  349. | insn_jne {
  350. struct statement *s = xmalloc(sizeof(struct statement));
  351. INIT_LIST_HEAD(&s->list);
  352. s->type = STMT_INSN;
  353. s->u.insn = $1;
  354. $$ = s;
  355. }
  356. | insn_jls {
  357. struct statement *s = xmalloc(sizeof(struct statement));
  358. INIT_LIST_HEAD(&s->list);
  359. s->type = STMT_INSN;
  360. s->u.insn = $1;
  361. $$ = s;
  362. }
  363. | insn_jges {
  364. struct statement *s = xmalloc(sizeof(struct statement));
  365. INIT_LIST_HEAD(&s->list);
  366. s->type = STMT_INSN;
  367. s->u.insn = $1;
  368. $$ = s;
  369. }
  370. | insn_jgs {
  371. struct statement *s = xmalloc(sizeof(struct statement));
  372. INIT_LIST_HEAD(&s->list);
  373. s->type = STMT_INSN;
  374. s->u.insn = $1;
  375. $$ = s;
  376. }
  377. | insn_jles {
  378. struct statement *s = xmalloc(sizeof(struct statement));
  379. INIT_LIST_HEAD(&s->list);
  380. s->type = STMT_INSN;
  381. s->u.insn = $1;
  382. $$ = s;
  383. }
  384. | insn_jdn {
  385. struct statement *s = xmalloc(sizeof(struct statement));
  386. INIT_LIST_HEAD(&s->list);
  387. s->type = STMT_INSN;
  388. s->u.insn = $1;
  389. $$ = s;
  390. }
  391. | insn_jdpz {
  392. struct statement *s = xmalloc(sizeof(struct statement));
  393. INIT_LIST_HEAD(&s->list);
  394. s->type = STMT_INSN;
  395. s->u.insn = $1;
  396. $$ = s;
  397. }
  398. | insn_jdp {
  399. struct statement *s = xmalloc(sizeof(struct statement));
  400. INIT_LIST_HEAD(&s->list);
  401. s->type = STMT_INSN;
  402. s->u.insn = $1;
  403. $$ = s;
  404. }
  405. | insn_jdnz {
  406. struct statement *s = xmalloc(sizeof(struct statement));
  407. INIT_LIST_HEAD(&s->list);
  408. s->type = STMT_INSN;
  409. s->u.insn = $1;
  410. $$ = s;
  411. }
  412. | insn_jl {
  413. struct statement *s = xmalloc(sizeof(struct statement));
  414. INIT_LIST_HEAD(&s->list);
  415. s->type = STMT_INSN;
  416. s->u.insn = $1;
  417. $$ = s;
  418. }
  419. | insn_jge {
  420. struct statement *s = xmalloc(sizeof(struct statement));
  421. INIT_LIST_HEAD(&s->list);
  422. s->type = STMT_INSN;
  423. s->u.insn = $1;
  424. $$ = s;
  425. }
  426. | insn_jg {
  427. struct statement *s = xmalloc(sizeof(struct statement));
  428. INIT_LIST_HEAD(&s->list);
  429. s->type = STMT_INSN;
  430. s->u.insn = $1;
  431. $$ = s;
  432. }
  433. | insn_jle {
  434. struct statement *s = xmalloc(sizeof(struct statement));
  435. INIT_LIST_HEAD(&s->list);
  436. s->type = STMT_INSN;
  437. s->u.insn = $1;
  438. $$ = s;
  439. }
  440. | insn_jzx {
  441. struct statement *s = xmalloc(sizeof(struct statement));
  442. INIT_LIST_HEAD(&s->list);
  443. s->type = STMT_INSN;
  444. s->u.insn = $1;
  445. $$ = s;
  446. }
  447. | insn_jnzx {
  448. struct statement *s = xmalloc(sizeof(struct statement));
  449. INIT_LIST_HEAD(&s->list);
  450. s->type = STMT_INSN;
  451. s->u.insn = $1;
  452. $$ = s;
  453. }
  454. | insn_jext {
  455. struct statement *s = xmalloc(sizeof(struct statement));
  456. INIT_LIST_HEAD(&s->list);
  457. s->type = STMT_INSN;
  458. s->u.insn = $1;
  459. $$ = s;
  460. }
  461. | insn_jnext {
  462. struct statement *s = xmalloc(sizeof(struct statement));
  463. INIT_LIST_HEAD(&s->list);
  464. s->type = STMT_INSN;
  465. s->u.insn = $1;
  466. $$ = s;
  467. }
  468. | insn_call {
  469. struct statement *s = xmalloc(sizeof(struct statement));
  470. INIT_LIST_HEAD(&s->list);
  471. s->type = STMT_INSN;
  472. s->u.insn = $1;
  473. $$ = s;
  474. }
  475. | insn_calls {
  476. struct statement *s = xmalloc(sizeof(struct statement));
  477. INIT_LIST_HEAD(&s->list);
  478. s->type = STMT_INSN;
  479. s->u.insn = $1;
  480. $$ = s;
  481. }
  482. | insn_ret {
  483. struct statement *s = xmalloc(sizeof(struct statement));
  484. INIT_LIST_HEAD(&s->list);
  485. s->type = STMT_INSN;
  486. s->u.insn = $1;
  487. $$ = s;
  488. }
  489. | insn_rets {
  490. struct statement *s = xmalloc(sizeof(struct statement));
  491. INIT_LIST_HEAD(&s->list);
  492. s->type = STMT_INSN;
  493. s->u.insn = $1;
  494. $$ = s;
  495. }
  496. | insn_tkiph {
  497. struct statement *s = xmalloc(sizeof(struct statement));
  498. INIT_LIST_HEAD(&s->list);
  499. s->type = STMT_INSN;
  500. s->u.insn = $1;
  501. $$ = s;
  502. }
  503. | insn_tkiphs {
  504. struct statement *s = xmalloc(sizeof(struct statement));
  505. INIT_LIST_HEAD(&s->list);
  506. s->type = STMT_INSN;
  507. s->u.insn = $1;
  508. $$ = s;
  509. }
  510. | insn_tkipl {
  511. struct statement *s = xmalloc(sizeof(struct statement));
  512. INIT_LIST_HEAD(&s->list);
  513. s->type = STMT_INSN;
  514. s->u.insn = $1;
  515. $$ = s;
  516. }
  517. | insn_tkipls {
  518. struct statement *s = xmalloc(sizeof(struct statement));
  519. INIT_LIST_HEAD(&s->list);
  520. s->type = STMT_INSN;
  521. s->u.insn = $1;
  522. $$ = s;
  523. }
  524. | insn_nap {
  525. struct statement *s = xmalloc(sizeof(struct statement));
  526. INIT_LIST_HEAD(&s->list);
  527. s->type = STMT_INSN;
  528. s->u.insn = $1;
  529. $$ = s;
  530. }
  531. | insn_raw {
  532. struct statement *s = xmalloc(sizeof(struct statement));
  533. INIT_LIST_HEAD(&s->list);
  534. s->type = STMT_INSN;
  535. s->u.insn = $1;
  536. $$ = s;
  537. }
  538. ;
  539. /* ASM directives */
  540. asmdir : ASM_ARCH hexnum_decnum {
  541. struct asmdir *ad = xmalloc(sizeof(struct asmdir));
  542. ad->type = ADIR_ARCH;
  543. ad->u.arch = (unsigned int)(unsigned long)$2;
  544. $$ = ad;
  545. }
  546. | ASM_START identifier {
  547. struct asmdir *ad = xmalloc(sizeof(struct asmdir));
  548. struct label *label = xmalloc(sizeof(struct label));
  549. label->name = $2;
  550. label->direction = LABELREF_ABSOLUTE;
  551. ad->type = ADIR_START;
  552. ad->u.start = label;
  553. $$ = ad;
  554. }
  555. | asm_assert {
  556. $$ = NULL;
  557. }
  558. ;
  559. asm_assert : ASM_ASSERT assertion {
  560. unsigned int ok = (unsigned int)(unsigned long)$2;
  561. if (!ok)
  562. assembler_assertion_failed();
  563. $$ = NULL;
  564. }
  565. ;
  566. assertion : PAREN_OPEN assert_expr PAREN_CLOSE {
  567. $$ = $2;
  568. }
  569. | PAREN_OPEN assertion LOGICAL_OR assertion PAREN_CLOSE {
  570. unsigned int a = (unsigned int)(unsigned long)$2;
  571. unsigned int b = (unsigned int)(unsigned long)$4;
  572. unsigned int result = (a || b);
  573. $$ = (void *)(unsigned long)result;
  574. }
  575. | PAREN_OPEN assertion LOGICAL_AND assertion PAREN_CLOSE {
  576. unsigned int a = (unsigned int)(unsigned long)$2;
  577. unsigned int b = (unsigned int)(unsigned long)$4;
  578. unsigned int result = (a && b);
  579. $$ = (void *)(unsigned long)result;
  580. }
  581. ;
  582. assert_expr : imm_value EQUAL imm_value {
  583. unsigned int a = (unsigned int)(unsigned long)$1;
  584. unsigned int b = (unsigned int)(unsigned long)$3;
  585. unsigned int result = (a == b);
  586. $$ = (void *)(unsigned long)result;
  587. }
  588. | imm_value NOT_EQUAL imm_value {
  589. unsigned int a = (unsigned int)(unsigned long)$1;
  590. unsigned int b = (unsigned int)(unsigned long)$3;
  591. unsigned int result = (a != b);
  592. $$ = (void *)(unsigned long)result;
  593. }
  594. ;
  595. label : LABEL {
  596. struct label *label = xmalloc(sizeof(struct label));
  597. char *l;
  598. l = xstrdup(yytext);
  599. l[strlen(l) - 1] = '\0';
  600. label->name = l;
  601. $$ = label;
  602. }
  603. ;
  604. /* multiply */
  605. insn_mul : OP_MUL operlist_3 {
  606. struct instruction *insn = xmalloc(sizeof(struct instruction));
  607. insn->op = OP_MUL;
  608. insn->operands = $2;
  609. $$ = insn;
  610. }
  611. ;
  612. /* add */
  613. insn_add : OP_ADD operlist_3 {
  614. struct instruction *insn = xmalloc(sizeof(struct instruction));
  615. insn->op = OP_ADD;
  616. insn->operands = $2;
  617. $$ = insn;
  618. }
  619. ;
  620. /* add. */
  621. insn_addsc : OP_ADDSC operlist_3 {
  622. struct instruction *insn = xmalloc(sizeof(struct instruction));
  623. insn->op = OP_ADDSC;
  624. insn->operands = $2;
  625. $$ = insn;
  626. }
  627. ;
  628. /* addc */
  629. insn_addc : OP_ADDC operlist_3 {
  630. struct instruction *insn = xmalloc(sizeof(struct instruction));
  631. insn->op = OP_ADDC;
  632. insn->operands = $2;
  633. $$ = insn;
  634. }
  635. ;
  636. /* addc. */
  637. insn_addscc : OP_ADDSCC operlist_3 {
  638. struct instruction *insn = xmalloc(sizeof(struct instruction));
  639. insn->op = OP_ADDSCC;
  640. insn->operands = $2;
  641. $$ = insn;
  642. }
  643. ;
  644. /* sub */
  645. insn_sub : OP_SUB operlist_3 {
  646. struct instruction *insn = xmalloc(sizeof(struct instruction));
  647. insn->op = OP_SUB;
  648. insn->operands = $2;
  649. $$ = insn;
  650. }
  651. ;
  652. /* sub. */
  653. insn_subsc : OP_SUBSC operlist_3 {
  654. struct instruction *insn = xmalloc(sizeof(struct instruction));
  655. insn->op = OP_SUBSC;
  656. insn->operands = $2;
  657. $$ = insn;
  658. }
  659. ;
  660. /* subc */
  661. insn_subc : OP_SUBC operlist_3 {
  662. struct instruction *insn = xmalloc(sizeof(struct instruction));
  663. insn->op = OP_SUBC;
  664. insn->operands = $2;
  665. $$ = insn;
  666. }
  667. ;
  668. /* subc. */
  669. insn_subscc : OP_SUBSCC operlist_3 {
  670. struct instruction *insn = xmalloc(sizeof(struct instruction));
  671. insn->op = OP_SUBSCC;
  672. insn->operands = $2;
  673. $$ = insn;
  674. }
  675. ;
  676. insn_sra : OP_SRA operlist_3 {
  677. struct instruction *insn = xmalloc(sizeof(struct instruction));
  678. insn->op = OP_SRA;
  679. insn->operands = $2;
  680. $$ = insn;
  681. }
  682. ;
  683. insn_or : OP_OR operlist_3 {
  684. struct instruction *insn = xmalloc(sizeof(struct instruction));
  685. insn->op = OP_OR;
  686. insn->operands = $2;
  687. $$ = insn;
  688. }
  689. ;
  690. insn_and : OP_AND operlist_3 {
  691. struct instruction *insn = xmalloc(sizeof(struct instruction));
  692. insn->op = OP_AND;
  693. insn->operands = $2;
  694. $$ = insn;
  695. }
  696. ;
  697. insn_xor : OP_XOR operlist_3 {
  698. struct instruction *insn = xmalloc(sizeof(struct instruction));
  699. insn->op = OP_XOR;
  700. insn->operands = $2;
  701. $$ = insn;
  702. }
  703. ;
  704. insn_sr : OP_SR operlist_3 {
  705. struct instruction *insn = xmalloc(sizeof(struct instruction));
  706. insn->op = OP_SR;
  707. insn->operands = $2;
  708. $$ = insn;
  709. }
  710. ;
  711. insn_srx : OP_SRX extended_operlist {
  712. struct instruction *insn = xmalloc(sizeof(struct instruction));
  713. insn->op = OP_SRX;
  714. insn->operands = $2;
  715. $$ = insn;
  716. }
  717. ;
  718. insn_sl : OP_SL operlist_3 {
  719. struct instruction *insn = xmalloc(sizeof(struct instruction));
  720. insn->op = OP_SL;
  721. insn->operands = $2;
  722. $$ = insn;
  723. }
  724. ;
  725. insn_rl : OP_RL operlist_3 {
  726. struct instruction *insn = xmalloc(sizeof(struct instruction));
  727. insn->op = OP_RL;
  728. insn->operands = $2;
  729. $$ = insn;
  730. }
  731. ;
  732. insn_rr : OP_RR operlist_3 {
  733. struct instruction *insn = xmalloc(sizeof(struct instruction));
  734. insn->op = OP_RR;
  735. insn->operands = $2;
  736. $$ = insn;
  737. }
  738. ;
  739. insn_nand : OP_NAND operlist_3 {
  740. struct instruction *insn = xmalloc(sizeof(struct instruction));
  741. insn->op = OP_NAND;
  742. insn->operands = $2;
  743. $$ = insn;
  744. }
  745. ;
  746. insn_orx : OP_ORX extended_operlist {
  747. struct instruction *insn = xmalloc(sizeof(struct instruction));
  748. insn->op = OP_ORX;
  749. insn->operands = $2;
  750. $$ = insn;
  751. }
  752. ;
  753. insn_mov : OP_MOV operlist_2 {
  754. struct instruction *insn = xmalloc(sizeof(struct instruction));
  755. insn->op = OP_MOV;
  756. insn->operands = $2;
  757. $$ = insn;
  758. }
  759. ;
  760. insn_jmp : OP_JMP labelref {
  761. struct instruction *insn = xmalloc(sizeof(struct instruction));
  762. struct operlist *ol = xmalloc(sizeof(struct operlist));
  763. ol->oper[0] = $2;
  764. insn->op = OP_JMP;
  765. insn->operands = ol;
  766. $$ = insn;
  767. }
  768. ;
  769. insn_jand : OP_JAND operlist_3 {
  770. struct instruction *insn = xmalloc(sizeof(struct instruction));
  771. insn->op = OP_JAND;
  772. insn->operands = $2;
  773. $$ = insn;
  774. }
  775. ;
  776. insn_jnand : OP_JNAND operlist_3 {
  777. struct instruction *insn = xmalloc(sizeof(struct instruction));
  778. insn->op = OP_JNAND;
  779. insn->operands = $2;
  780. $$ = insn;
  781. }
  782. ;
  783. insn_js : OP_JS operlist_3 {
  784. struct instruction *insn = xmalloc(sizeof(struct instruction));
  785. insn->op = OP_JS;
  786. insn->operands = $2;
  787. $$ = insn;
  788. }
  789. ;
  790. insn_jns : OP_JNS operlist_3 {
  791. struct instruction *insn = xmalloc(sizeof(struct instruction));
  792. insn->op = OP_JNS;
  793. insn->operands = $2;
  794. $$ = insn;
  795. }
  796. ;
  797. insn_je : OP_JE operlist_3 {
  798. struct instruction *insn = xmalloc(sizeof(struct instruction));
  799. insn->op = OP_JE;
  800. insn->operands = $2;
  801. $$ = insn;
  802. }
  803. ;
  804. insn_jne : OP_JNE operlist_3 {
  805. struct instruction *insn = xmalloc(sizeof(struct instruction));
  806. insn->op = OP_JNE;
  807. insn->operands = $2;
  808. $$ = insn;
  809. }
  810. ;
  811. insn_jls : OP_JLS operlist_3 {
  812. struct instruction *insn = xmalloc(sizeof(struct instruction));
  813. insn->op = OP_JLS;
  814. insn->operands = $2;
  815. $$ = insn;
  816. }
  817. ;
  818. insn_jges : OP_JGES operlist_3 {
  819. struct instruction *insn = xmalloc(sizeof(struct instruction));
  820. insn->op = OP_JGES;
  821. insn->operands = $2;
  822. $$ = insn;
  823. }
  824. ;
  825. insn_jgs : OP_JGS operlist_3 {
  826. struct instruction *insn = xmalloc(sizeof(struct instruction));
  827. insn->op = OP_JGS;
  828. insn->operands = $2;
  829. $$ = insn;
  830. }
  831. ;
  832. insn_jles : OP_JLES operlist_3 {
  833. struct instruction *insn = xmalloc(sizeof(struct instruction));
  834. insn->op = OP_JLES;
  835. insn->operands = $2;
  836. $$ = insn;
  837. }
  838. ;
  839. insn_jl : OP_JL operlist_3 {
  840. struct instruction *insn = xmalloc(sizeof(struct instruction));
  841. insn->op = OP_JL;
  842. insn->operands = $2;
  843. $$ = insn;
  844. }
  845. ;
  846. insn_jge : OP_JGE operlist_3 {
  847. struct instruction *insn = xmalloc(sizeof(struct instruction));
  848. insn->op = OP_JGE;
  849. insn->operands = $2;
  850. $$ = insn;
  851. }
  852. ;
  853. insn_jg : OP_JG operlist_3 {
  854. struct instruction *insn = xmalloc(sizeof(struct instruction));
  855. insn->op = OP_JG;
  856. insn->operands = $2;
  857. $$ = insn;
  858. }
  859. ;
  860. insn_jle : OP_JLE operlist_3 {
  861. struct instruction *insn = xmalloc(sizeof(struct instruction));
  862. insn->op = OP_JLE;
  863. insn->operands = $2;
  864. $$ = insn;
  865. }
  866. ;
  867. insn_jzx : OP_JZX extended_operlist {
  868. struct instruction *insn = xmalloc(sizeof(struct instruction));
  869. insn->op = OP_JZX;
  870. insn->operands = $2;
  871. $$ = insn;
  872. }
  873. ;
  874. insn_jnzx : OP_JNZX extended_operlist {
  875. struct instruction *insn = xmalloc(sizeof(struct instruction));
  876. insn->op = OP_JNZX;
  877. insn->operands = $2;
  878. $$ = insn;
  879. }
  880. ;
  881. insn_jdn : OP_JDN operlist_3 {
  882. struct instruction *insn = xmalloc(sizeof(struct instruction));
  883. insn->op = OP_JDN;
  884. insn->operands = $2;
  885. $$ = insn;
  886. }
  887. ;
  888. insn_jdpz : OP_JDPZ operlist_3 {
  889. struct instruction *insn = xmalloc(sizeof(struct instruction));
  890. insn->op = OP_JDPZ;
  891. insn->operands = $2;
  892. $$ = insn;
  893. }
  894. ;
  895. insn_jdp : OP_JDP operlist_3 {
  896. struct instruction *insn = xmalloc(sizeof(struct instruction));
  897. insn->op = OP_JDP;
  898. insn->operands = $2;
  899. $$ = insn;
  900. }
  901. ;
  902. insn_jdnz : OP_JDNZ operlist_3 {
  903. struct instruction *insn = xmalloc(sizeof(struct instruction));
  904. insn->op = OP_JDNZ;
  905. insn->operands = $2;
  906. $$ = insn;
  907. }
  908. ;
  909. insn_jext : OP_JEXT external_jump_operands {
  910. struct instruction *insn = xmalloc(sizeof(struct instruction));
  911. insn->op = OP_JEXT;
  912. insn->operands = $2;
  913. $$ = insn;
  914. }
  915. ;
  916. insn_jnext : OP_JNEXT external_jump_operands {
  917. struct instruction *insn = xmalloc(sizeof(struct instruction));
  918. insn->op = OP_JNEXT;
  919. insn->operands = $2;
  920. $$ = insn;
  921. }
  922. ;
  923. linkreg : LR regnr {
  924. $$ = $2;
  925. }
  926. ;
  927. insn_call : OP_CALL linkreg COMMA labelref {
  928. struct instruction *insn = xmalloc(sizeof(struct instruction));
  929. struct operlist *ol = xmalloc(sizeof(struct operlist));
  930. struct operand *oper_lr = xmalloc(sizeof(struct operand));
  931. struct operand *oper_zero = xmalloc(sizeof(struct operand));
  932. oper_zero->type = OPER_RAW;
  933. oper_zero->u.raw = 0;
  934. oper_lr->type = OPER_RAW;
  935. oper_lr->u.raw = (unsigned long)$2;
  936. ol->oper[0] = oper_lr;
  937. ol->oper[1] = oper_zero;
  938. ol->oper[2] = $4;
  939. insn->op = OP_CALL;
  940. insn->operands = ol;
  941. $$ = insn;
  942. }
  943. ;
  944. insn_calls : OP_CALLS labelref {
  945. struct instruction *insn = xmalloc(sizeof(struct instruction));
  946. struct operlist *ol = xmalloc(sizeof(struct operlist));
  947. struct operand *oper_r0 = xmalloc(sizeof(struct operand));
  948. struct registr *r0 = xmalloc(sizeof(struct registr));
  949. r0->type = GPR;
  950. r0->nr = 0;
  951. oper_r0->type = OPER_REG;
  952. oper_r0->u.reg = r0;
  953. ol->oper[0] = oper_r0;
  954. ol->oper[1] = oper_r0;
  955. ol->oper[2] = $2;
  956. insn->op = OP_CALLS;
  957. insn->operands = ol;
  958. $$ = insn;
  959. }
  960. ;
  961. insn_ret : OP_RET linkreg COMMA linkreg {
  962. struct instruction *insn = xmalloc(sizeof(struct instruction));
  963. struct operlist *ol = xmalloc(sizeof(struct operlist));
  964. struct operand *oper_lr0 = xmalloc(sizeof(struct operand));
  965. struct operand *oper_lr1 = xmalloc(sizeof(struct operand));
  966. struct operand *oper_zero = xmalloc(sizeof(struct operand));
  967. oper_zero->type = OPER_RAW;
  968. oper_zero->u.raw = 0;
  969. oper_lr0->type = OPER_RAW;
  970. oper_lr0->u.raw = (unsigned long)$2;
  971. oper_lr1->type = OPER_RAW;
  972. oper_lr1->u.raw = (unsigned long)$4;
  973. ol->oper[0] = oper_lr0;
  974. ol->oper[1] = oper_zero;
  975. ol->oper[2] = oper_lr1;
  976. insn->op = OP_RET;
  977. insn->operands = ol;
  978. $$ = insn;
  979. }
  980. ;
  981. insn_rets : OP_RETS {
  982. struct instruction *insn = xmalloc(sizeof(struct instruction));
  983. struct operlist *ol = xmalloc(sizeof(struct operlist));
  984. struct operand *oper_r0 = xmalloc(sizeof(struct operand));
  985. struct operand *oper_zero = xmalloc(sizeof(struct operand));
  986. struct registr *r0 = xmalloc(sizeof(struct registr));
  987. oper_zero->type = OPER_RAW;
  988. oper_zero->u.raw = 0;
  989. r0->type = GPR;
  990. r0->nr = 0;
  991. oper_r0->type = OPER_REG;
  992. oper_r0->u.reg = r0;
  993. ol->oper[0] = oper_r0;
  994. ol->oper[1] = oper_r0;
  995. ol->oper[2] = oper_zero;
  996. insn->op = OP_RETS;
  997. insn->operands = ol;
  998. $$ = insn;
  999. }
  1000. ;
  1001. insn_tkiph : OP_TKIPH operlist_2 {
  1002. struct instruction *insn = xmalloc(sizeof(struct instruction));
  1003. struct operlist *ol = $2;
  1004. struct operand *flags = xmalloc(sizeof(struct operand));
  1005. struct immediate *imm = xmalloc(sizeof(struct immediate));
  1006. imm->imm = 0x1;
  1007. flags->type = OPER_IMM;
  1008. flags->u.imm = imm;
  1009. ol->oper[2] = ol->oper[1];
  1010. ol->oper[1] = flags;
  1011. insn->op = OP_TKIPH;
  1012. insn->operands = ol;
  1013. $$ = insn;
  1014. }
  1015. ;
  1016. insn_tkiphs : OP_TKIPHS operlist_2 {
  1017. struct instruction *insn = xmalloc(sizeof(struct instruction));
  1018. struct operlist *ol = $2;
  1019. struct operand *flags = xmalloc(sizeof(struct operand));
  1020. struct immediate *imm = xmalloc(sizeof(struct immediate));
  1021. imm->imm = 0x1 | 0x2;
  1022. flags->type = OPER_IMM;
  1023. flags->u.imm = imm;
  1024. ol->oper[2] = ol->oper[1];
  1025. ol->oper[1] = flags;
  1026. insn->op = OP_TKIPH;
  1027. insn->operands = ol;
  1028. $$ = insn;
  1029. }
  1030. ;
  1031. insn_tkipl : OP_TKIPL operlist_2 {
  1032. struct instruction *insn = xmalloc(sizeof(struct instruction));
  1033. struct operlist *ol = $2;
  1034. struct operand *flags = xmalloc(sizeof(struct operand));
  1035. struct immediate *imm = xmalloc(sizeof(struct immediate));
  1036. imm->imm = 0x0;
  1037. flags->type = OPER_IMM;
  1038. flags->u.imm = imm;
  1039. ol->oper[2] = ol->oper[1];
  1040. ol->oper[1] = flags;
  1041. insn->op = OP_TKIPH;
  1042. insn->operands = ol;
  1043. $$ = insn;
  1044. }
  1045. ;
  1046. insn_tkipls : OP_TKIPLS operlist_2 {
  1047. struct instruction *insn = xmalloc(sizeof(struct instruction));
  1048. struct operlist *ol = $2;
  1049. struct operand *flags = xmalloc(sizeof(struct operand));
  1050. struct immediate *imm = xmalloc(sizeof(struct immediate));
  1051. imm->imm = 0x0 | 0x2;
  1052. flags->type = OPER_IMM;
  1053. flags->u.imm = imm;
  1054. ol->oper[2] = ol->oper[1];
  1055. ol->oper[1] = flags;
  1056. insn->op = OP_TKIPH;
  1057. insn->operands = ol;
  1058. $$ = insn;
  1059. }
  1060. ;
  1061. insn_nap : OP_NAP {
  1062. struct instruction *insn = xmalloc(sizeof(struct instruction));
  1063. struct operlist *ol = xmalloc(sizeof(struct operlist));
  1064. struct operand *regop = xmalloc(sizeof(struct operand));
  1065. struct operand *zeroop = xmalloc(sizeof(struct operand));
  1066. struct registr *r0 = xmalloc(sizeof(struct registr));
  1067. r0->type = GPR;
  1068. r0->nr = 0;
  1069. regop->type = OPER_REG;
  1070. regop->u.reg = r0;
  1071. zeroop->type = OPER_RAW;
  1072. zeroop->u.raw = 0x000;
  1073. ol->oper[0] = regop;
  1074. ol->oper[1] = regop;
  1075. ol->oper[2] = zeroop;
  1076. insn->op = OP_NAP;
  1077. insn->operands = ol;
  1078. $$ = insn;
  1079. }
  1080. ;
  1081. insn_raw : raw_code operlist_3 {
  1082. struct instruction *insn = xmalloc(sizeof(struct instruction));
  1083. insn->op = RAW_CODE;
  1084. insn->operands = $2;
  1085. insn->opcode = (unsigned long)$1;
  1086. $$ = insn;
  1087. }
  1088. ;
  1089. raw_code : RAW_CODE {
  1090. yytext++; /* skip @ */
  1091. $$ = (void *)(unsigned long)strtoul(yytext, NULL, 16);
  1092. }
  1093. ;
  1094. extended_operlist : imm_value COMMA imm_value COMMA operand COMMA operand COMMA operand {
  1095. struct operlist *ol = xmalloc(sizeof(struct operlist));
  1096. struct operand *mask_oper = xmalloc(sizeof(struct operand));
  1097. struct operand *shift_oper = xmalloc(sizeof(struct operand));
  1098. mask_oper->type = OPER_RAW;
  1099. mask_oper->u.raw = (unsigned long)$1;
  1100. shift_oper->type = OPER_RAW;
  1101. shift_oper->u.raw = (unsigned long)$3;
  1102. ol->oper[0] = mask_oper;
  1103. ol->oper[1] = shift_oper;
  1104. ol->oper[2] = $5;
  1105. ol->oper[3] = $7;
  1106. ol->oper[4] = store_oper_sanity($9);
  1107. $$ = ol;
  1108. }
  1109. ;
  1110. external_jump_operands : imm COMMA labelref {
  1111. struct operlist *ol = xmalloc(sizeof(struct operlist));
  1112. struct operand *cond = xmalloc(sizeof(struct operand));
  1113. cond->type = OPER_IMM;
  1114. cond->u.imm = $1;
  1115. ol->oper[0] = cond;
  1116. ol->oper[1] = $3;
  1117. $$ = ol;
  1118. }
  1119. ;
  1120. operlist_2 : operand COMMA operand {
  1121. struct operlist *ol = xmalloc(sizeof(struct operlist));
  1122. ol->oper[0] = $1;
  1123. ol->oper[1] = store_oper_sanity($3);
  1124. $$ = ol;
  1125. }
  1126. ;
  1127. operlist_3 : operand COMMA operand COMMA operand {
  1128. struct operlist *ol = xmalloc(sizeof(struct operlist));
  1129. ol->oper[0] = $1;
  1130. ol->oper[1] = $3;
  1131. ol->oper[2] = store_oper_sanity($5);
  1132. $$ = ol;
  1133. }
  1134. ;
  1135. operand : reg {
  1136. struct operand *oper = xmalloc(sizeof(struct operand));
  1137. oper->type = OPER_REG;
  1138. oper->u.reg = $1;
  1139. $$ = oper;
  1140. }
  1141. | mem {
  1142. struct operand *oper = xmalloc(sizeof(struct operand));
  1143. oper->type = OPER_MEM;
  1144. oper->u.mem = $1;
  1145. $$ = oper;
  1146. }
  1147. | raw_code {
  1148. struct operand *oper = xmalloc(sizeof(struct operand));
  1149. oper->type = OPER_RAW;
  1150. oper->u.raw = (unsigned long)$1;
  1151. $$ = oper;
  1152. }
  1153. | imm {
  1154. struct operand *oper = xmalloc(sizeof(struct operand));
  1155. oper->type = OPER_IMM;
  1156. oper->u.imm = $1;
  1157. $$ = oper;
  1158. }
  1159. | labelref {
  1160. $$ = $1;
  1161. }
  1162. ;
  1163. reg : GPR regnr {
  1164. struct registr *reg = xmalloc(sizeof(struct registr));
  1165. reg->type = GPR;
  1166. reg->nr = (unsigned long)$2;
  1167. $$ = reg;
  1168. }
  1169. | SPR {
  1170. struct registr *reg = xmalloc(sizeof(struct registr));
  1171. reg->type = SPR;
  1172. yytext += 3; /* skip "spr" */
  1173. reg->nr = strtoul(yytext, NULL, 16);
  1174. $$ = reg;
  1175. }
  1176. | OFFR regnr {
  1177. struct registr *reg = xmalloc(sizeof(struct registr));
  1178. reg->type = OFFR;
  1179. reg->nr = (unsigned long)$2;
  1180. $$ = reg;
  1181. }
  1182. ;
  1183. mem : BRACK_OPEN imm BRACK_CLOSE {
  1184. struct memory *mem = xmalloc(sizeof(struct memory));
  1185. struct immediate *offset_imm = $2;
  1186. mem->type = MEM_DIRECT;
  1187. mem->offset = offset_imm->imm;
  1188. free(offset_imm);
  1189. $$ = mem;
  1190. }
  1191. | BRACK_OPEN imm COMMA OFFR regnr BRACK_CLOSE {
  1192. struct memory *mem = xmalloc(sizeof(struct memory));
  1193. struct immediate *offset_imm = $2;
  1194. mem->type = MEM_INDIRECT;
  1195. mem->offset = offset_imm->imm;
  1196. free(offset_imm);
  1197. mem->offr_nr = (unsigned long)$5;
  1198. $$ = mem;
  1199. }
  1200. ;
  1201. imm : imm_value {
  1202. struct immediate *imm = xmalloc(sizeof(struct immediate));
  1203. imm->imm = (unsigned long)$1;
  1204. $$ = imm;
  1205. }
  1206. ;
  1207. imm_value : hexnum_decnum {
  1208. $$ = $1;
  1209. }
  1210. | complex_imm {
  1211. $$ = $1;
  1212. }
  1213. ;
  1214. complex_imm : PAREN_OPEN complex_imm_arg complex_imm_oper complex_imm_arg PAREN_CLOSE {
  1215. unsigned long a = (unsigned long)$2;
  1216. unsigned long b = (unsigned long)$4;
  1217. unsigned long operation = (unsigned long)$3;
  1218. unsigned long res = 31337;
  1219. switch (operation) {
  1220. case PLUS:
  1221. res = a + b;
  1222. break;
  1223. case MINUS:
  1224. res = a - b;
  1225. break;
  1226. case MULTIPLY:
  1227. res = a * b;
  1228. break;
  1229. case DIVIDE:
  1230. res = a / b;
  1231. break;
  1232. case BITW_OR:
  1233. res = a | b;
  1234. break;
  1235. case BITW_AND:
  1236. res = a & b;
  1237. break;
  1238. case BITW_XOR:
  1239. res = a ^ b;
  1240. break;
  1241. case LEFTSHIFT:
  1242. res = a << b;
  1243. break;
  1244. case RIGHTSHIFT:
  1245. res = a >> b;
  1246. break;
  1247. default:
  1248. yyerror("Internal parser BUG. complex_imm oper unknown");
  1249. }
  1250. $$ = (void *)res;
  1251. }
  1252. | PAREN_OPEN complex_imm PAREN_CLOSE {
  1253. $$ = $2;
  1254. }
  1255. | PAREN_OPEN asm_assert PAREN_CLOSE {
  1256. /* Inline assertion. Always return zero */
  1257. $$ = (void *)(unsigned long)(unsigned int)0;
  1258. }
  1259. | PAREN_OPEN BITW_NOT complex_imm PAREN_CLOSE {
  1260. unsigned long n = (unsigned long)$3;
  1261. n = ~n;
  1262. $$ = (void *)n;
  1263. }
  1264. | PAREN_OPEN complex_imm_const PAREN_CLOSE {
  1265. $$ = $2;
  1266. }
  1267. ;
  1268. complex_imm_oper : PLUS {
  1269. $$ = (void *)(unsigned long)PLUS;
  1270. }
  1271. | MINUS {
  1272. $$ = (void *)(unsigned long)MINUS;
  1273. }
  1274. | MULTIPLY {
  1275. $$ = (void *)(unsigned long)MULTIPLY;
  1276. }
  1277. | DIVIDE {
  1278. $$ = (void *)(unsigned long)DIVIDE;
  1279. }
  1280. | BITW_OR {
  1281. $$ = (void *)(unsigned long)BITW_OR;
  1282. }
  1283. | BITW_AND {
  1284. $$ = (void *)(unsigned long)BITW_AND;
  1285. }
  1286. | BITW_XOR {
  1287. $$ = (void *)(unsigned long)BITW_XOR;
  1288. }
  1289. | LEFTSHIFT {
  1290. $$ = (void *)(unsigned long)LEFTSHIFT;
  1291. }
  1292. | RIGHTSHIFT {
  1293. $$ = (void *)(unsigned long)RIGHTSHIFT;
  1294. }
  1295. ;
  1296. complex_imm_arg : complex_imm_const {
  1297. $$ = $1;
  1298. }
  1299. | complex_imm {
  1300. $$ = $1;
  1301. }
  1302. ;
  1303. complex_imm_const : hexnum_decnum {
  1304. $$ = $1;
  1305. }
  1306. | BITW_NOT hexnum_decnum {
  1307. unsigned long n = (unsigned long)$2;
  1308. n = ~n;
  1309. $$ = (void *)n;
  1310. }
  1311. ;
  1312. hexnum : HEXNUM {
  1313. while (yytext[0] != 'x') {
  1314. if (yytext[0] == '\0')
  1315. yyerror("Internal HEXNUM parser error");
  1316. yytext++;
  1317. }
  1318. yytext++;
  1319. $$ = (void *)(unsigned long)strtoul(yytext, NULL, 16);
  1320. }
  1321. ;
  1322. decnum : DECNUM {
  1323. $$ = (void *)(unsigned long)strtol(yytext, NULL, 10);
  1324. }
  1325. ;
  1326. hexnum_decnum : hexnum {
  1327. $$ = $1;
  1328. }
  1329. | decnum {
  1330. $$ = $1;
  1331. }
  1332. ;
  1333. labelref : identifier {
  1334. struct operand *oper = xmalloc(sizeof(struct operand));
  1335. struct label *label = xmalloc(sizeof(struct label));
  1336. label->name = $1;
  1337. label->direction = LABELREF_ABSOLUTE;
  1338. oper->type = OPER_LABEL;
  1339. oper->u.label = label;
  1340. $$ = oper;
  1341. }
  1342. | identifier MINUS {
  1343. struct operand *oper = xmalloc(sizeof(struct operand));
  1344. struct label *label = xmalloc(sizeof(struct label));
  1345. label->name = $1;
  1346. label->direction = LABELREF_RELATIVE_BACK;
  1347. oper->type = OPER_LABEL;
  1348. oper->u.label = label;
  1349. $$ = oper;
  1350. }
  1351. | identifier PLUS {
  1352. struct operand *oper = xmalloc(sizeof(struct operand));
  1353. struct label *label = xmalloc(sizeof(struct label));
  1354. label->name = $1;
  1355. label->direction = LABELREF_RELATIVE_FORWARD;
  1356. oper->type = OPER_LABEL;
  1357. oper->u.label = label;
  1358. $$ = oper;
  1359. }
  1360. ;
  1361. regnr : DECNUM {
  1362. $$ = (void *)(unsigned long)strtoul(yytext, NULL, 10);
  1363. }
  1364. ;
  1365. identifier : IDENT {
  1366. $$ = xstrdup(yytext);
  1367. }
  1368. ;
  1369. %%
  1370. int section = SECTION_TEXT; /* default to .text section */
  1371. struct initvals_sect *cur_initvals_sect;
  1372. void yyerror(const char *str)
  1373. {
  1374. unsigned int i;
  1375. fprintf(stderr,
  1376. "Parser ERROR (file \"%s\", line %u, col %u):\n",
  1377. cur_lineinfo.file,
  1378. cur_lineinfo.lineno,
  1379. cur_lineinfo.column);
  1380. fprintf(stderr, "%s\n", cur_lineinfo.linecopy);
  1381. for (i = 0; i < cur_lineinfo.column - 1; i++)
  1382. fprintf(stderr, " ");
  1383. fprintf(stderr, "^\n");
  1384. fprintf(stderr, "%s\n", str);
  1385. exit(1);
  1386. }
  1387. static struct operand * store_oper_sanity(struct operand *oper)
  1388. {
  1389. if (oper->type == OPER_IMM &&
  1390. oper->u.imm->imm != 0) {
  1391. yyerror("Only 0x000 Immediate is allowed for "
  1392. "Output operands");
  1393. }
  1394. return oper;
  1395. }
  1396. static void assembler_assertion_failed(void)
  1397. {
  1398. yyerror("Assembler %assert failed");
  1399. }