parse.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433
  1. #include "all.h"
  2. #include <ctype.h>
  3. #include <stdarg.h>
  4. enum {
  5. Ksb = 4, /* matches Oarg/Opar/Jret */
  6. Kub,
  7. Ksh,
  8. Kuh,
  9. Kc,
  10. K0,
  11. Ke = -2, /* erroneous mode */
  12. Km = Kl, /* memory pointer */
  13. };
  14. Op optab[NOp] = {
  15. #undef F
  16. #define F(cf, hi, id, co, as, im, ic, lg, cv, pn) \
  17. .canfold = cf, \
  18. .hasid = hi, .idval = id, \
  19. .commutes = co, .assoc = as, \
  20. .idemp = im, \
  21. .cmpeqwl = ic, .cmplgtewl = lg, .eqval = cv, \
  22. .pinned = pn
  23. #define O(op, k, flags) [O##op]={.name = #op, .argcls = k, flags},
  24. #include "ops.h"
  25. #undef F
  26. };
  27. typedef enum {
  28. PXXX,
  29. PLbl,
  30. PPhi,
  31. PIns,
  32. PEnd,
  33. } PState;
  34. enum Token {
  35. Txxx = 0,
  36. /* aliases */
  37. Tloadw = NPubOp,
  38. Tloadl,
  39. Tloads,
  40. Tloadd,
  41. Talloc1,
  42. Talloc2,
  43. Tblit,
  44. Tcall,
  45. Tenv,
  46. Tphi,
  47. Tjmp,
  48. Tjnz,
  49. Tret,
  50. Thlt,
  51. Texport,
  52. Tthread,
  53. Tcommon,
  54. Tfunc,
  55. Ttype,
  56. Tdata,
  57. Tsection,
  58. Talign,
  59. Tdbgfile,
  60. Tl,
  61. Tw,
  62. Tsh,
  63. Tuh,
  64. Th,
  65. Tsb,
  66. Tub,
  67. Tb,
  68. Td,
  69. Ts,
  70. Tz,
  71. Tint,
  72. Tflts,
  73. Tfltd,
  74. Ttmp,
  75. Tlbl,
  76. Tglo,
  77. Ttyp,
  78. Tstr,
  79. Tplus,
  80. Teq,
  81. Tcomma,
  82. Tlparen,
  83. Trparen,
  84. Tlbrace,
  85. Trbrace,
  86. Tnl,
  87. Tdots,
  88. Teof,
  89. Ntok
  90. };
  91. static char *kwmap[Ntok] = {
  92. [Tloadw] = "loadw",
  93. [Tloadl] = "loadl",
  94. [Tloads] = "loads",
  95. [Tloadd] = "loadd",
  96. [Talloc1] = "alloc1",
  97. [Talloc2] = "alloc2",
  98. [Tblit] = "blit",
  99. [Tcall] = "call",
  100. [Tenv] = "env",
  101. [Tphi] = "phi",
  102. [Tjmp] = "jmp",
  103. [Tjnz] = "jnz",
  104. [Tret] = "ret",
  105. [Thlt] = "hlt",
  106. [Texport] = "export",
  107. [Tthread] = "thread",
  108. [Tcommon] = "common",
  109. [Tfunc] = "function",
  110. [Ttype] = "type",
  111. [Tdata] = "data",
  112. [Tsection] = "section",
  113. [Talign] = "align",
  114. [Tdbgfile] = "dbgfile",
  115. [Tsb] = "sb",
  116. [Tub] = "ub",
  117. [Tsh] = "sh",
  118. [Tuh] = "uh",
  119. [Tb] = "b",
  120. [Th] = "h",
  121. [Tw] = "w",
  122. [Tl] = "l",
  123. [Ts] = "s",
  124. [Td] = "d",
  125. [Tz] = "z",
  126. [Tdots] = "...",
  127. };
  128. enum {
  129. NPred = 63,
  130. TMask = 16383, /* for temps hash */
  131. BMask = 8191, /* for blocks hash */
  132. K = 11183273, /* found using tools/lexh.c */
  133. M = 23,
  134. };
  135. static uchar lexh[1 << (32-M)];
  136. static FILE *inf;
  137. static char *inpath;
  138. static int thead;
  139. static struct {
  140. char chr;
  141. double fltd;
  142. float flts;
  143. int64_t num;
  144. char *str;
  145. } tokval;
  146. static int lnum;
  147. static Fn *curf;
  148. static int *tmph;
  149. static int tmphcap;
  150. static Phi **plink;
  151. static Blk *curb;
  152. static Blk **blink;
  153. static Blk *blkh[BMask+1];
  154. static int nblk;
  155. static int rcls;
  156. static uint ntyp;
  157. void
  158. err(char *s, ...)
  159. {
  160. va_list ap;
  161. va_start(ap, s);
  162. fprintf(stderr, "qbe:%s:%d: ", inpath, lnum);
  163. vfprintf(stderr, s, ap);
  164. fprintf(stderr, "\n");
  165. va_end(ap);
  166. exit(1);
  167. }
  168. static void
  169. lexinit()
  170. {
  171. static int done;
  172. int i;
  173. long h;
  174. if (done)
  175. return;
  176. for (i=0; i<NPubOp; ++i)
  177. if (optab[i].name)
  178. kwmap[i] = optab[i].name;
  179. assert(Ntok <= UCHAR_MAX);
  180. for (i=0; i<Ntok; ++i)
  181. if (kwmap[i]) {
  182. h = hash(kwmap[i])*K >> M;
  183. assert(lexh[h] == Txxx);
  184. lexh[h] = i;
  185. }
  186. done = 1;
  187. }
  188. static int64_t
  189. getint()
  190. {
  191. uint64_t n;
  192. int c, m;
  193. n = 0;
  194. c = fgetc(inf);
  195. m = (c == '-');
  196. if (m)
  197. c = fgetc(inf);
  198. do {
  199. n = 10*n + (c - '0');
  200. c = fgetc(inf);
  201. } while ('0' <= c && c <= '9');
  202. ungetc(c, inf);
  203. if (m)
  204. n = 1 + ~n;
  205. return *(int64_t *)&n;
  206. }
  207. static int
  208. lex()
  209. {
  210. static char tok[NString];
  211. int c, i, esc;
  212. int t;
  213. do
  214. c = fgetc(inf);
  215. while (isblank(c));
  216. t = Txxx;
  217. tokval.chr = c;
  218. switch (c) {
  219. case EOF:
  220. return Teof;
  221. case ',':
  222. return Tcomma;
  223. case '(':
  224. return Tlparen;
  225. case ')':
  226. return Trparen;
  227. case '{':
  228. return Tlbrace;
  229. case '}':
  230. return Trbrace;
  231. case '=':
  232. return Teq;
  233. case '+':
  234. return Tplus;
  235. case 's':
  236. if (fscanf(inf, "_%f", &tokval.flts) != 1)
  237. break;
  238. return Tflts;
  239. case 'd':
  240. if (fscanf(inf, "_%lf", &tokval.fltd) != 1)
  241. break;
  242. return Tfltd;
  243. case '%':
  244. t = Ttmp;
  245. c = fgetc(inf);
  246. goto Alpha;
  247. case '@':
  248. t = Tlbl;
  249. c = fgetc(inf);
  250. goto Alpha;
  251. case '$':
  252. t = Tglo;
  253. if ((c = fgetc(inf)) == '"')
  254. goto Quoted;
  255. goto Alpha;
  256. case ':':
  257. t = Ttyp;
  258. c = fgetc(inf);
  259. goto Alpha;
  260. case '#':
  261. while ((c=fgetc(inf)) != '\n' && c != EOF)
  262. ;
  263. /* fall through */
  264. case '\n':
  265. lnum++;
  266. return Tnl;
  267. }
  268. if (isdigit(c) || c == '-') {
  269. ungetc(c, inf);
  270. tokval.num = getint();
  271. return Tint;
  272. }
  273. if (c == '"') {
  274. t = Tstr;
  275. Quoted:
  276. tokval.str = vnew(2, 1, PFn);
  277. tokval.str[0] = c;
  278. esc = 0;
  279. for (i=1;; i++) {
  280. c = fgetc(inf);
  281. if (c == EOF)
  282. err("unterminated string");
  283. vgrow(&tokval.str, i+2);
  284. tokval.str[i] = c;
  285. if (c == '"' && !esc) {
  286. tokval.str[i+1] = 0;
  287. return t;
  288. }
  289. esc = (c == '\\' && !esc);
  290. }
  291. }
  292. Alpha:
  293. if (!isalpha(c) && c != '.' && c != '_')
  294. err("invalid character %c (%d)", c, c);
  295. i = 0;
  296. do {
  297. if (i >= NString-1)
  298. err("identifier too long");
  299. tok[i++] = c;
  300. c = fgetc(inf);
  301. } while (isalpha(c) || c == '$' || c == '.' || c == '_' || isdigit(c));
  302. tok[i] = 0;
  303. ungetc(c, inf);
  304. tokval.str = tok;
  305. if (t != Txxx) {
  306. return t;
  307. }
  308. t = lexh[hash(tok)*K >> M];
  309. if (t == Txxx || strcmp(kwmap[t], tok) != 0) {
  310. err("unknown keyword %s", tok);
  311. return Txxx;
  312. }
  313. return t;
  314. }
  315. static int
  316. peek()
  317. {
  318. if (thead == Txxx)
  319. thead = lex();
  320. return thead;
  321. }
  322. static int
  323. next()
  324. {
  325. int t;
  326. t = peek();
  327. thead = Txxx;
  328. return t;
  329. }
  330. static int
  331. nextnl()
  332. {
  333. int t;
  334. while ((t = next()) == Tnl)
  335. ;
  336. return t;
  337. }
  338. static void
  339. expect(int t)
  340. {
  341. static char *ttoa[] = {
  342. [Tlbl] = "label",
  343. [Tcomma] = ",",
  344. [Teq] = "=",
  345. [Tnl] = "newline",
  346. [Tlparen] = "(",
  347. [Trparen] = ")",
  348. [Tlbrace] = "{",
  349. [Trbrace] = "}",
  350. [Teof] = 0,
  351. };
  352. char buf[128], *s1, *s2;
  353. int t1;
  354. t1 = next();
  355. if (t == t1)
  356. return;
  357. s1 = ttoa[t] ? ttoa[t] : "??";
  358. s2 = ttoa[t1] ? ttoa[t1] : "??";
  359. sprintf(buf, "%s expected, got %s instead", s1, s2);
  360. err(buf);
  361. }
  362. static Ref
  363. tmpref(char *v)
  364. {
  365. int t, i;
  366. if (tmphcap/2 <= curf->ntmp-Tmp0) {
  367. free(tmph);
  368. tmphcap = tmphcap ? tmphcap*2 : TMask+1;
  369. tmph = emalloc(tmphcap * sizeof tmph[0]);
  370. for (t=Tmp0; t<curf->ntmp; t++) {
  371. i = hash(curf->tmp[t].name) & (tmphcap-1);
  372. for (; tmph[i]; i=(i+1) & (tmphcap-1))
  373. ;
  374. tmph[i] = t;
  375. }
  376. }
  377. i = hash(v) & (tmphcap-1);
  378. for (; tmph[i]; i=(i+1) & (tmphcap-1)) {
  379. t = tmph[i];
  380. if (strcmp(curf->tmp[t].name, v) == 0)
  381. return TMP(t);
  382. }
  383. t = curf->ntmp;
  384. tmph[i] = t;
  385. newtmp(0, Kx, curf);
  386. strcpy(curf->tmp[t].name, v);
  387. return TMP(t);
  388. }
  389. static Ref
  390. parseref()
  391. {
  392. Con c;
  393. memset(&c, 0, sizeof c);
  394. switch (next()) {
  395. default:
  396. return R;
  397. case Ttmp:
  398. return tmpref(tokval.str);
  399. case Tint:
  400. c.type = CBits;
  401. c.bits.i = tokval.num;
  402. break;
  403. case Tflts:
  404. c.type = CBits;
  405. c.bits.s = tokval.flts;
  406. c.flt = 1;
  407. break;
  408. case Tfltd:
  409. c.type = CBits;
  410. c.bits.d = tokval.fltd;
  411. c.flt = 2;
  412. break;
  413. case Tthread:
  414. c.sym.type = SThr;
  415. expect(Tglo);
  416. /* fall through */
  417. case Tglo:
  418. c.type = CAddr;
  419. c.sym.id = intern(tokval.str);
  420. break;
  421. }
  422. return newcon(&c, curf);
  423. }
  424. static int
  425. findtyp(int i)
  426. {
  427. while (--i >= 0)
  428. if (strcmp(tokval.str, typ[i].name) == 0)
  429. return i;
  430. err("undefined type :%s", tokval.str);
  431. }
  432. static int
  433. parsecls(int *tyn)
  434. {
  435. switch (next()) {
  436. default:
  437. err("invalid class specifier");
  438. case Ttyp:
  439. *tyn = findtyp(ntyp);
  440. return Kc;
  441. case Tsb:
  442. return Ksb;
  443. case Tub:
  444. return Kub;
  445. case Tsh:
  446. return Ksh;
  447. case Tuh:
  448. return Kuh;
  449. case Tw:
  450. return Kw;
  451. case Tl:
  452. return Kl;
  453. case Ts:
  454. return Ks;
  455. case Td:
  456. return Kd;
  457. }
  458. }
  459. static int
  460. parserefl(int arg)
  461. {
  462. int k, ty, env, hasenv, vararg;
  463. Ref r;
  464. hasenv = 0;
  465. vararg = 0;
  466. expect(Tlparen);
  467. while (peek() != Trparen) {
  468. if (curi - insb >= NIns)
  469. err("too many instructions");
  470. if (!arg && vararg)
  471. err("no parameters allowed after '...'");
  472. switch (peek()) {
  473. case Tdots:
  474. if (vararg)
  475. err("only one '...' allowed");
  476. vararg = 1;
  477. if (arg) {
  478. *curi = (Ins){.op = Oargv};
  479. curi++;
  480. }
  481. next();
  482. goto Next;
  483. case Tenv:
  484. if (hasenv)
  485. err("only one environment allowed");
  486. hasenv = 1;
  487. env = 1;
  488. next();
  489. k = Kl;
  490. break;
  491. default:
  492. env = 0;
  493. k = parsecls(&ty);
  494. break;
  495. }
  496. r = parseref();
  497. if (req(r, R))
  498. err("invalid argument");
  499. if (!arg && rtype(r) != RTmp)
  500. err("invalid function parameter");
  501. if (env)
  502. if (arg)
  503. *curi = (Ins){Oarge, k, R, {r}};
  504. else
  505. *curi = (Ins){Opare, k, r, {R}};
  506. else if (k == Kc)
  507. if (arg)
  508. *curi = (Ins){Oargc, Kl, R, {TYPE(ty), r}};
  509. else
  510. *curi = (Ins){Oparc, Kl, r, {TYPE(ty)}};
  511. else if (k >= Ksb)
  512. if (arg)
  513. *curi = (Ins){Oargsb+(k-Ksb), Kw, R, {r}};
  514. else
  515. *curi = (Ins){Oparsb+(k-Ksb), Kw, r, {R}};
  516. else
  517. if (arg)
  518. *curi = (Ins){Oarg, k, R, {r}};
  519. else
  520. *curi = (Ins){Opar, k, r, {R}};
  521. curi++;
  522. Next:
  523. if (peek() == Trparen)
  524. break;
  525. expect(Tcomma);
  526. }
  527. expect(Trparen);
  528. return vararg;
  529. }
  530. static Blk *
  531. findblk(char *name)
  532. {
  533. Blk *b;
  534. uint32_t h;
  535. h = hash(name) & BMask;
  536. for (b=blkh[h]; b; b=b->dlink)
  537. if (strcmp(b->name, name) == 0)
  538. return b;
  539. b = newblk();
  540. b->id = nblk++;
  541. strcpy(b->name, name);
  542. b->dlink = blkh[h];
  543. blkh[h] = b;
  544. return b;
  545. }
  546. static void
  547. closeblk()
  548. {
  549. idup(curb, insb, curi-insb);
  550. blink = &curb->link;
  551. curi = insb;
  552. }
  553. static PState
  554. parseline(PState ps)
  555. {
  556. Ref arg[NPred] = {R};
  557. Blk *blk[NPred];
  558. Phi *phi;
  559. Ref r;
  560. Blk *b;
  561. Con *c;
  562. int t, op, i, k, ty;
  563. t = nextnl();
  564. if (ps == PLbl && t != Tlbl && t != Trbrace)
  565. err("label or } expected");
  566. switch (t) {
  567. case Ttmp:
  568. r = tmpref(tokval.str);
  569. expect(Teq);
  570. k = parsecls(&ty);
  571. op = next();
  572. break;
  573. default:
  574. if (isstore(t)) {
  575. case Tblit:
  576. case Tcall:
  577. case Ovastart:
  578. /* operations without result */
  579. r = R;
  580. k = Kw;
  581. op = t;
  582. break;
  583. }
  584. err("label, instruction or jump expected");
  585. case Trbrace:
  586. return PEnd;
  587. case Tlbl:
  588. b = findblk(tokval.str);
  589. if (curb && curb->jmp.type == Jxxx) {
  590. closeblk();
  591. curb->jmp.type = Jjmp;
  592. curb->s1 = b;
  593. }
  594. if (b->jmp.type != Jxxx)
  595. err("multiple definitions of block @%s", b->name);
  596. *blink = b;
  597. curb = b;
  598. plink = &curb->phi;
  599. expect(Tnl);
  600. return PPhi;
  601. case Tret:
  602. curb->jmp.type = Jretw + rcls;
  603. if (peek() == Tnl)
  604. curb->jmp.type = Jret0;
  605. else if (rcls != K0) {
  606. r = parseref();
  607. if (req(r, R))
  608. err("invalid return value");
  609. curb->jmp.arg = r;
  610. }
  611. goto Close;
  612. case Tjmp:
  613. curb->jmp.type = Jjmp;
  614. goto Jump;
  615. case Tjnz:
  616. curb->jmp.type = Jjnz;
  617. r = parseref();
  618. if (req(r, R))
  619. err("invalid argument for jnz jump");
  620. curb->jmp.arg = r;
  621. expect(Tcomma);
  622. Jump:
  623. expect(Tlbl);
  624. curb->s1 = findblk(tokval.str);
  625. if (curb->jmp.type != Jjmp) {
  626. expect(Tcomma);
  627. expect(Tlbl);
  628. curb->s2 = findblk(tokval.str);
  629. }
  630. if (curb->s1 == curf->start || curb->s2 == curf->start)
  631. err("invalid jump to the start block");
  632. goto Close;
  633. case Thlt:
  634. curb->jmp.type = Jhlt;
  635. Close:
  636. expect(Tnl);
  637. closeblk();
  638. return PLbl;
  639. case Odbgloc:
  640. op = t;
  641. k = Kw;
  642. r = R;
  643. expect(Tint);
  644. arg[0] = INT(tokval.num);
  645. if (arg[0].val != tokval.num)
  646. err("line number too big");
  647. if (peek() == Tcomma) {
  648. next();
  649. expect(Tint);
  650. arg[1] = INT(tokval.num);
  651. if (arg[1].val != tokval.num)
  652. err("column number too big");
  653. } else
  654. arg[1] = INT(0);
  655. goto Ins;
  656. }
  657. if (op == Tcall) {
  658. curf->leaf = 0;
  659. arg[0] = parseref();
  660. parserefl(1);
  661. op = Ocall;
  662. expect(Tnl);
  663. if (k == Kc) {
  664. k = Kl;
  665. arg[1] = TYPE(ty);
  666. }
  667. if (k >= Ksb)
  668. k = Kw;
  669. goto Ins;
  670. }
  671. if (op == Tloadw)
  672. op = Oloadsw;
  673. if (op >= Tloadl && op <= Tloadd)
  674. op = Oload;
  675. if (op == Talloc1 || op == Talloc2)
  676. op = Oalloc;
  677. if (op == Ovastart && !curf->vararg)
  678. err("cannot use vastart in non-variadic function");
  679. if (k >= Ksb)
  680. err("size class must be w, l, s, or d");
  681. i = 0;
  682. if (peek() != Tnl)
  683. for (;;) {
  684. if (i == NPred)
  685. err("too many arguments");
  686. if (op == Tphi) {
  687. expect(Tlbl);
  688. blk[i] = findblk(tokval.str);
  689. }
  690. arg[i] = parseref();
  691. if (req(arg[i], R))
  692. err("invalid instruction argument");
  693. i++;
  694. t = peek();
  695. if (t == Tnl)
  696. break;
  697. if (t != Tcomma)
  698. err(", or end of line expected");
  699. next();
  700. }
  701. next();
  702. switch (op) {
  703. case Tphi:
  704. if (ps != PPhi || curb == curf->start)
  705. err("unexpected phi instruction");
  706. phi = alloc(sizeof *phi);
  707. phi->to = r;
  708. phi->cls = k;
  709. phi->arg = vnew(i, sizeof arg[0], PFn);
  710. memcpy(phi->arg, arg, i * sizeof arg[0]);
  711. phi->blk = vnew(i, sizeof blk[0], PFn);
  712. memcpy(phi->blk, blk, i * sizeof blk[0]);
  713. phi->narg = i;
  714. *plink = phi;
  715. plink = &phi->link;
  716. return PPhi;
  717. case Tblit:
  718. if (curi - insb >= NIns-1)
  719. err("too many instructions");
  720. memset(curi, 0, 2 * sizeof(Ins));
  721. curi->op = Oblit0;
  722. curi->arg[0] = arg[0];
  723. curi->arg[1] = arg[1];
  724. curi++;
  725. if (rtype(arg[2]) != RCon)
  726. err("blit size must be constant");
  727. c = &curf->con[arg[2].val];
  728. r = INT(c->bits.i);
  729. if (c->type != CBits
  730. || rsval(r) < 0
  731. || rsval(r) != c->bits.i)
  732. err("invalid blit size");
  733. curi->op = Oblit1;
  734. curi->arg[0] = r;
  735. curi++;
  736. return PIns;
  737. default:
  738. if (op >= NPubOp)
  739. err("invalid instruction");
  740. Ins:
  741. if (curi - insb >= NIns)
  742. err("too many instructions");
  743. curi->op = op;
  744. curi->cls = k;
  745. curi->to = r;
  746. curi->arg[0] = arg[0];
  747. curi->arg[1] = arg[1];
  748. curi++;
  749. return PIns;
  750. }
  751. }
  752. static int
  753. usecheck(Ref r, int k, Fn *fn)
  754. {
  755. return rtype(r) != RTmp || fn->tmp[r.val].cls == k
  756. || (fn->tmp[r.val].cls == Kl && k == Kw);
  757. }
  758. static void
  759. typecheck(Fn *fn)
  760. {
  761. Blk *b;
  762. Phi *p;
  763. Ins *i;
  764. uint n;
  765. int k;
  766. Tmp *t;
  767. Ref r;
  768. BSet pb[1], ppb[1];
  769. fillpreds(fn);
  770. bsinit(pb, fn->nblk);
  771. bsinit(ppb, fn->nblk);
  772. for (b=fn->start; b; b=b->link) {
  773. for (p=b->phi; p; p=p->link)
  774. fn->tmp[p->to.val].cls = p->cls;
  775. for (i=b->ins; i<&b->ins[b->nins]; i++)
  776. if (rtype(i->to) == RTmp) {
  777. t = &fn->tmp[i->to.val];
  778. if (clsmerge(&t->cls, i->cls))
  779. err("temporary %%%s is assigned with"
  780. " multiple types", t->name);
  781. }
  782. }
  783. for (b=fn->start; b; b=b->link) {
  784. bszero(pb);
  785. for (n=0; n<b->npred; n++)
  786. bsset(pb, b->pred[n]->id);
  787. for (p=b->phi; p; p=p->link) {
  788. bszero(ppb);
  789. t = &fn->tmp[p->to.val];
  790. for (n=0; n<p->narg; n++) {
  791. k = t->cls;
  792. if (bshas(ppb, p->blk[n]->id))
  793. err("multiple entries for @%s in phi %%%s",
  794. p->blk[n]->name, t->name);
  795. if (!usecheck(p->arg[n], k, fn))
  796. err("invalid type for operand %%%s in phi %%%s",
  797. fn->tmp[p->arg[n].val].name, t->name);
  798. bsset(ppb, p->blk[n]->id);
  799. }
  800. if (!bsequal(pb, ppb))
  801. err("predecessors not matched in phi %%%s", t->name);
  802. }
  803. for (i=b->ins; i<&b->ins[b->nins]; i++)
  804. for (n=0; n<2; n++) {
  805. k = optab[i->op].argcls[n][i->cls];
  806. r = i->arg[n];
  807. t = &fn->tmp[r.val];
  808. if (k == Ke)
  809. err("invalid instruction type in %s",
  810. optab[i->op].name);
  811. if (rtype(r) == RType)
  812. continue;
  813. if (rtype(r) != -1 && k == Kx)
  814. err("no %s operand expected in %s",
  815. n == 1 ? "second" : "first",
  816. optab[i->op].name);
  817. if (rtype(r) == -1 && k != Kx)
  818. err("missing %s operand in %s",
  819. n == 1 ? "second" : "first",
  820. optab[i->op].name);
  821. if (!usecheck(r, k, fn))
  822. err("invalid type for %s operand %%%s in %s",
  823. n == 1 ? "second" : "first",
  824. t->name, optab[i->op].name);
  825. }
  826. r = b->jmp.arg;
  827. if (isret(b->jmp.type)) {
  828. if (b->jmp.type == Jretc)
  829. k = Kl;
  830. else if (b->jmp.type >= Jretsb)
  831. k = Kw;
  832. else
  833. k = b->jmp.type - Jretw;
  834. if (!usecheck(r, k, fn))
  835. goto JErr;
  836. }
  837. if (b->jmp.type == Jjnz && !usecheck(r, Kw, fn))
  838. JErr:
  839. err("invalid type for jump argument %%%s in block @%s",
  840. fn->tmp[r.val].name, b->name);
  841. if (b->s1 && b->s1->jmp.type == Jxxx)
  842. err("block @%s is used undefined", b->s1->name);
  843. if (b->s2 && b->s2->jmp.type == Jxxx)
  844. err("block @%s is used undefined", b->s2->name);
  845. }
  846. }
  847. static Fn *
  848. parsefn(Lnk *lnk)
  849. {
  850. Blk *b;
  851. int i;
  852. PState ps;
  853. curb = 0;
  854. nblk = 0;
  855. curi = insb;
  856. curf = alloc(sizeof *curf);
  857. curf->ntmp = 0;
  858. curf->ncon = 2;
  859. curf->tmp = vnew(curf->ntmp, sizeof curf->tmp[0], PFn);
  860. curf->con = vnew(curf->ncon, sizeof curf->con[0], PFn);
  861. for (i=0; i<Tmp0; ++i)
  862. if (T.fpr0 <= i && i < T.fpr0 + T.nfpr)
  863. newtmp(0, Kd, curf);
  864. else
  865. newtmp(0, Kl, curf);
  866. curf->con[0].type = CBits;
  867. curf->con[0].bits.i = 0xdeaddead; /* UNDEF */
  868. curf->con[1].type = CBits;
  869. curf->lnk = *lnk;
  870. curf->leaf = 1;
  871. blink = &curf->start;
  872. curf->retty = Kx;
  873. if (peek() != Tglo)
  874. rcls = parsecls(&curf->retty);
  875. else
  876. rcls = K0;
  877. if (next() != Tglo)
  878. err("function name expected");
  879. strncpy(curf->name, tokval.str, NString-1);
  880. curf->vararg = parserefl(0);
  881. if (nextnl() != Tlbrace)
  882. err("function body must start with {");
  883. ps = PLbl;
  884. do
  885. ps = parseline(ps);
  886. while (ps != PEnd);
  887. if (!curb)
  888. err("empty function");
  889. if (curb->jmp.type == Jxxx)
  890. err("last block misses jump");
  891. curf->mem = vnew(0, sizeof curf->mem[0], PFn);
  892. curf->nmem = 0;
  893. curf->nblk = nblk;
  894. curf->rpo = vnew(nblk, sizeof curf->rpo[0], PFn);
  895. for (b=curf->start; b; b=b->link)
  896. b->dlink = 0; /* was trashed by findblk() */
  897. for (i=0; i<BMask+1; ++i)
  898. blkh[i] = 0;
  899. memset(tmph, 0, tmphcap * sizeof tmph[0]);
  900. typecheck(curf);
  901. return curf;
  902. }
  903. static void
  904. parsefields(Field *fld, Typ *ty, int t)
  905. {
  906. Typ *ty1;
  907. int n, c, a, al, type;
  908. uint64_t sz, s;
  909. n = 0;
  910. sz = 0;
  911. al = ty->align;
  912. while (t != Trbrace) {
  913. ty1 = 0;
  914. switch (t) {
  915. default: err("invalid type member specifier");
  916. case Td: type = Fd; s = 8; a = 3; break;
  917. case Tl: type = Fl; s = 8; a = 3; break;
  918. case Ts: type = Fs; s = 4; a = 2; break;
  919. case Tw: type = Fw; s = 4; a = 2; break;
  920. case Th: type = Fh; s = 2; a = 1; break;
  921. case Tb: type = Fb; s = 1; a = 0; break;
  922. case Ttyp:
  923. type = FTyp;
  924. ty1 = &typ[findtyp(ntyp-1)];
  925. s = ty1->size;
  926. a = ty1->align;
  927. break;
  928. }
  929. if (a > al)
  930. al = a;
  931. a = (1 << a) - 1;
  932. a = ((sz + a) & ~a) - sz;
  933. if (a) {
  934. if (n < NField) {
  935. /* padding */
  936. fld[n].type = FPad;
  937. fld[n].len = a;
  938. n++;
  939. }
  940. }
  941. t = nextnl();
  942. if (t == Tint) {
  943. c = tokval.num;
  944. t = nextnl();
  945. } else
  946. c = 1;
  947. sz += a + c*s;
  948. if (type == FTyp)
  949. s = ty1 - typ;
  950. for (; c>0 && n<NField; c--, n++) {
  951. fld[n].type = type;
  952. fld[n].len = s;
  953. }
  954. if (t != Tcomma)
  955. break;
  956. t = nextnl();
  957. }
  958. if (t != Trbrace)
  959. err(", or } expected");
  960. fld[n].type = FEnd;
  961. a = 1 << al;
  962. if (sz < ty->size)
  963. sz = ty->size;
  964. ty->size = (sz + a - 1) & -a;
  965. ty->align = al;
  966. }
  967. static void
  968. parsetyp()
  969. {
  970. Typ *ty;
  971. int t, al;
  972. uint n;
  973. /* be careful if extending the syntax
  974. * to handle nested types, any pointer
  975. * held to typ[] might be invalidated!
  976. */
  977. vgrow(&typ, ntyp+1);
  978. ty = &typ[ntyp++];
  979. ty->isdark = 0;
  980. ty->isunion = 0;
  981. ty->align = -1;
  982. ty->size = 0;
  983. if (nextnl() != Ttyp || nextnl() != Teq)
  984. err("type name and then = expected");
  985. strcpy(ty->name, tokval.str);
  986. t = nextnl();
  987. if (t == Talign) {
  988. if (nextnl() != Tint)
  989. err("alignment expected");
  990. for (al=0; tokval.num /= 2; al++)
  991. ;
  992. ty->align = al;
  993. t = nextnl();
  994. }
  995. if (t != Tlbrace)
  996. err("type body must start with {");
  997. t = nextnl();
  998. if (t == Tint) {
  999. ty->isdark = 1;
  1000. ty->size = tokval.num;
  1001. if (ty->align == -1)
  1002. err("dark types need alignment");
  1003. if (nextnl() != Trbrace)
  1004. err("} expected");
  1005. return;
  1006. }
  1007. n = 0;
  1008. ty->fields = vnew(1, sizeof ty->fields[0], PHeap);
  1009. if (t == Tlbrace) {
  1010. ty->isunion = 1;
  1011. do {
  1012. if (t != Tlbrace)
  1013. err("invalid union member");
  1014. vgrow(&ty->fields, n+1);
  1015. parsefields(ty->fields[n++], ty, nextnl());
  1016. t = nextnl();
  1017. } while (t != Trbrace);
  1018. } else
  1019. parsefields(ty->fields[n++], ty, t);
  1020. ty->nunion = n;
  1021. }
  1022. static void
  1023. parsedatref(Dat *d)
  1024. {
  1025. int t;
  1026. d->isref = 1;
  1027. d->u.ref.name = tokval.str;
  1028. d->u.ref.off = 0;
  1029. t = peek();
  1030. if (t == Tplus) {
  1031. next();
  1032. if (next() != Tint)
  1033. err("invalid token after offset in ref");
  1034. d->u.ref.off = tokval.num;
  1035. }
  1036. }
  1037. static void
  1038. parsedatstr(Dat *d)
  1039. {
  1040. d->isstr = 1;
  1041. d->u.str = tokval.str;
  1042. }
  1043. static void
  1044. parsedat(void cb(Dat *), Lnk *lnk)
  1045. {
  1046. char name[NString] = {0};
  1047. int t;
  1048. Dat d;
  1049. if (nextnl() != Tglo || nextnl() != Teq)
  1050. err("data name, then = expected");
  1051. strncpy(name, tokval.str, NString-1);
  1052. t = nextnl();
  1053. lnk->align = 8;
  1054. if (t == Talign) {
  1055. if (nextnl() != Tint)
  1056. err("alignment expected");
  1057. if (tokval.num <= 0 || tokval.num > CHAR_MAX
  1058. || (tokval.num & (tokval.num-1)) != 0)
  1059. err("invalid alignment");
  1060. lnk->align = tokval.num;
  1061. t = nextnl();
  1062. }
  1063. d.type = DStart;
  1064. d.name = name;
  1065. d.lnk = lnk;
  1066. cb(&d);
  1067. if (t != Tlbrace)
  1068. err("expected data contents in { .. }");
  1069. for (;;) {
  1070. switch (nextnl()) {
  1071. default: err("invalid size specifier %c in data", tokval.chr);
  1072. case Trbrace: goto Done;
  1073. case Tl: d.type = DL; break;
  1074. case Tw: d.type = DW; break;
  1075. case Th: d.type = DH; break;
  1076. case Tb: d.type = DB; break;
  1077. case Ts: d.type = DW; break;
  1078. case Td: d.type = DL; break;
  1079. case Tz: d.type = DZ; break;
  1080. }
  1081. t = nextnl();
  1082. do {
  1083. d.isstr = 0;
  1084. d.isref = 0;
  1085. memset(&d.u, 0, sizeof d.u);
  1086. if (t == Tflts)
  1087. d.u.flts = tokval.flts;
  1088. else if (t == Tfltd)
  1089. d.u.fltd = tokval.fltd;
  1090. else if (t == Tint)
  1091. d.u.num = tokval.num;
  1092. else if (t == Tglo)
  1093. parsedatref(&d);
  1094. else if (t == Tstr)
  1095. parsedatstr(&d);
  1096. else
  1097. err("constant literal expected");
  1098. cb(&d);
  1099. t = nextnl();
  1100. } while (t == Tint || t == Tflts || t == Tfltd || t == Tstr || t == Tglo);
  1101. if (t == Trbrace)
  1102. break;
  1103. if (t != Tcomma)
  1104. err(", or } expected");
  1105. }
  1106. Done:
  1107. d.type = DEnd;
  1108. cb(&d);
  1109. }
  1110. static int
  1111. parselnk(Lnk *lnk)
  1112. {
  1113. int t, haslnk;
  1114. for (haslnk=0;; haslnk=1)
  1115. switch ((t=nextnl())) {
  1116. case Texport:
  1117. lnk->export = 1;
  1118. break;
  1119. case Tthread:
  1120. lnk->thread = 1;
  1121. break;
  1122. case Tcommon:
  1123. lnk->common = 1;
  1124. break;
  1125. case Tsection:
  1126. if (lnk->sec)
  1127. err("only one section allowed");
  1128. if (next() != Tstr)
  1129. err("section \"name\" expected");
  1130. lnk->sec = tokval.str;
  1131. if (peek() == Tstr) {
  1132. next();
  1133. lnk->secf = tokval.str;
  1134. }
  1135. break;
  1136. default:
  1137. if (t == Tfunc && lnk->thread)
  1138. err("only data may have thread linkage");
  1139. if (haslnk && t != Tdata && t != Tfunc)
  1140. err("only data and function have linkage");
  1141. return t;
  1142. }
  1143. }
  1144. void
  1145. parse(FILE *f, char *path, void dbgfile(char *), void data(Dat *), void func(Fn *))
  1146. {
  1147. Lnk lnk;
  1148. uint n;
  1149. lexinit();
  1150. inf = f;
  1151. inpath = path;
  1152. lnum = 1;
  1153. thead = Txxx;
  1154. ntyp = 0;
  1155. typ = vnew(0, sizeof typ[0], PHeap);
  1156. for (;;) {
  1157. lnk = (Lnk){0};
  1158. switch (parselnk(&lnk)) {
  1159. default:
  1160. err("top-level definition expected");
  1161. case Tdbgfile:
  1162. expect(Tstr);
  1163. dbgfile(tokval.str);
  1164. break;
  1165. case Tfunc:
  1166. lnk.align = 16;
  1167. func(parsefn(&lnk));
  1168. break;
  1169. case Tdata:
  1170. parsedat(data, &lnk);
  1171. break;
  1172. case Ttype:
  1173. parsetyp();
  1174. break;
  1175. case Teof:
  1176. for (n=0; n<ntyp; n++)
  1177. if (typ[n].nunion)
  1178. vfree(typ[n].fields);
  1179. vfree(typ);
  1180. return;
  1181. }
  1182. }
  1183. }
  1184. static void
  1185. printcon(Con *c, FILE *f)
  1186. {
  1187. switch (c->type) {
  1188. case CUndef:
  1189. break;
  1190. case CAddr:
  1191. if (c->sym.type == SThr)
  1192. fprintf(f, "thread ");
  1193. fprintf(f, "$%s", str(c->sym.id));
  1194. if (c->bits.i)
  1195. fprintf(f, "%+"PRIi64, c->bits.i);
  1196. break;
  1197. case CBits:
  1198. if (c->flt == 1)
  1199. fprintf(f, "s_%f", c->bits.s);
  1200. else if (c->flt == 2)
  1201. fprintf(f, "d_%lf", c->bits.d);
  1202. else
  1203. fprintf(f, "%"PRIi64, c->bits.i);
  1204. break;
  1205. }
  1206. }
  1207. void
  1208. printref(Ref r, Fn *fn, FILE *f)
  1209. {
  1210. int i;
  1211. Mem *m;
  1212. switch (rtype(r)) {
  1213. case RTmp:
  1214. if (r.val < Tmp0)
  1215. fprintf(f, "R%d", r.val);
  1216. else
  1217. fprintf(f, "%%%s", fn->tmp[r.val].name);
  1218. break;
  1219. case RCon:
  1220. if (req(r, UNDEF))
  1221. fprintf(f, "UNDEF");
  1222. else
  1223. printcon(&fn->con[r.val], f);
  1224. break;
  1225. case RSlot:
  1226. fprintf(f, "S%d", rsval(r));
  1227. break;
  1228. case RCall:
  1229. fprintf(f, "%04x", r.val);
  1230. break;
  1231. case RType:
  1232. fprintf(f, ":%s", typ[r.val].name);
  1233. break;
  1234. case RMem:
  1235. i = 0;
  1236. m = &fn->mem[r.val];
  1237. fputc('[', f);
  1238. if (m->offset.type != CUndef) {
  1239. printcon(&m->offset, f);
  1240. i = 1;
  1241. }
  1242. if (!req(m->base, R)) {
  1243. if (i)
  1244. fprintf(f, " + ");
  1245. printref(m->base, fn, f);
  1246. i = 1;
  1247. }
  1248. if (!req(m->index, R)) {
  1249. if (i)
  1250. fprintf(f, " + ");
  1251. fprintf(f, "%d * ", m->scale);
  1252. printref(m->index, fn, f);
  1253. }
  1254. fputc(']', f);
  1255. break;
  1256. case RInt:
  1257. fprintf(f, "%d", rsval(r));
  1258. break;
  1259. case -1:
  1260. fprintf(f, "R");
  1261. break;
  1262. }
  1263. }
  1264. void
  1265. printfn(Fn *fn, FILE *f)
  1266. {
  1267. static char ktoc[] = "wlsd";
  1268. static char *jtoa[NJmp] = {
  1269. #define X(j) [J##j] = #j,
  1270. JMPS(X)
  1271. #undef X
  1272. };
  1273. Blk *b;
  1274. Phi *p;
  1275. Ins *i;
  1276. uint n;
  1277. fprintf(f, "function $%s() {\n", fn->name);
  1278. for (b=fn->start; b; b=b->link) {
  1279. fprintf(f, "@%s\n", b->name);
  1280. for (p=b->phi; p; p=p->link) {
  1281. fprintf(f, "\t");
  1282. printref(p->to, fn, f);
  1283. fprintf(f, " =%c phi ", ktoc[p->cls]);
  1284. assert(p->narg);
  1285. for (n=0;; n++) {
  1286. fprintf(f, "@%s ", p->blk[n]->name);
  1287. printref(p->arg[n], fn, f);
  1288. if (n == p->narg-1) {
  1289. fprintf(f, "\n");
  1290. break;
  1291. } else
  1292. fprintf(f, ", ");
  1293. }
  1294. }
  1295. for (i=b->ins; i<&b->ins[b->nins]; i++) {
  1296. fprintf(f, "\t");
  1297. if (!req(i->to, R)) {
  1298. printref(i->to, fn, f);
  1299. fprintf(f, " =%c ", ktoc[i->cls]);
  1300. }
  1301. assert(optab[i->op].name);
  1302. fprintf(f, "%s", optab[i->op].name);
  1303. if (req(i->to, R))
  1304. switch (i->op) {
  1305. case Oarg:
  1306. case Oswap:
  1307. case Oxcmp:
  1308. case Oacmp:
  1309. case Oacmn:
  1310. case Oafcmp:
  1311. case Oxtest:
  1312. case Oxdiv:
  1313. case Oxidiv:
  1314. fputc(ktoc[i->cls], f);
  1315. }
  1316. if (!req(i->arg[0], R)) {
  1317. fprintf(f, " ");
  1318. printref(i->arg[0], fn, f);
  1319. }
  1320. if (!req(i->arg[1], R)) {
  1321. fprintf(f, ", ");
  1322. printref(i->arg[1], fn, f);
  1323. }
  1324. fprintf(f, "\n");
  1325. }
  1326. switch (b->jmp.type) {
  1327. case Jret0:
  1328. case Jretsb:
  1329. case Jretub:
  1330. case Jretsh:
  1331. case Jretuh:
  1332. case Jretw:
  1333. case Jretl:
  1334. case Jrets:
  1335. case Jretd:
  1336. case Jretc:
  1337. fprintf(f, "\t%s", jtoa[b->jmp.type]);
  1338. if (b->jmp.type != Jret0 || !req(b->jmp.arg, R)) {
  1339. fprintf(f, " ");
  1340. printref(b->jmp.arg, fn, f);
  1341. }
  1342. if (b->jmp.type == Jretc)
  1343. fprintf(f, ", :%s", typ[fn->retty].name);
  1344. fprintf(f, "\n");
  1345. break;
  1346. case Jhlt:
  1347. fprintf(f, "\thlt\n");
  1348. break;
  1349. case Jjmp:
  1350. if (b->s1 != b->link)
  1351. fprintf(f, "\tjmp @%s\n", b->s1->name);
  1352. break;
  1353. default:
  1354. fprintf(f, "\t%s ", jtoa[b->jmp.type]);
  1355. if (b->jmp.type == Jjnz) {
  1356. printref(b->jmp.arg, fn, f);
  1357. fprintf(f, ", ");
  1358. }
  1359. assert(b->s1 && b->s2);
  1360. fprintf(f, "@%s, @%s\n", b->s1->name, b->s2->name);
  1361. break;
  1362. }
  1363. }
  1364. fprintf(f, "}\n");
  1365. }