lex.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <ctype.h>
  4. #include <bio.h>
  5. #include "../6c/6.out.h"
  6. #include "a.h"
  7. #include "y.tab.h"
  8. void
  9. main(int argc, char *argv[])
  10. {
  11. char *p;
  12. int nout, nproc, status, i, c;
  13. thechar = '6';
  14. thestring = "960";
  15. memset(debug, 0, sizeof(debug));
  16. cinit();
  17. outfile = 0;
  18. include[ninclude++] = ".";
  19. ARGBEGIN {
  20. default:
  21. c = ARGC();
  22. if(c >= 0 || c < sizeof(debug))
  23. debug[c] = 1;
  24. break;
  25. case 'o':
  26. outfile = ARGF();
  27. break;
  28. case 'D':
  29. p = ARGF();
  30. if(p)
  31. Dlist[nDlist++] = p;
  32. break;
  33. case 'I':
  34. p = ARGF();
  35. setinclude(p);
  36. break;
  37. } ARGEND
  38. if(*argv == 0) {
  39. print("usage: %ca [-options] file.s\n", thechar);
  40. errorexit();
  41. }
  42. if(argc > 1 && systemtype(Windows)){
  43. print("can't assemble multiple files on windows\n");
  44. errorexit();
  45. }
  46. if(argc > 1 && !systemtype(Windows)) {
  47. nproc = 1;
  48. if(p = getenv("NPROC"))
  49. nproc = atol(p); /* */
  50. c = 0;
  51. nout = 0;
  52. for(;;) {
  53. while(nout < nproc && argc > 0) {
  54. i = myfork();
  55. if(i < 0) {
  56. i = mywait(&status);
  57. if(i < 0)
  58. errorexit();
  59. if(status)
  60. c++;
  61. nout--;
  62. continue;
  63. }
  64. if(i == 0) {
  65. print("%s:\n", *argv);
  66. if(assemble(*argv))
  67. errorexit();
  68. exits(0);
  69. }
  70. nout++;
  71. argc--;
  72. argv++;
  73. }
  74. i = mywait(&status);
  75. if(i < 0) {
  76. if(c)
  77. errorexit();
  78. exits(0);
  79. }
  80. if(status)
  81. c++;
  82. nout--;
  83. }
  84. }
  85. if(assemble(argv[0]))
  86. errorexit();
  87. exits(0);
  88. }
  89. int
  90. assemble(char *file)
  91. {
  92. char ofile[100], incfile[20], *p;
  93. int i, of;
  94. strcpy(ofile, file);
  95. p = utfrrune(ofile, pathchar());
  96. if(p) {
  97. include[0] = ofile;
  98. *p++ = 0;
  99. } else
  100. p = ofile;
  101. if(outfile == 0) {
  102. outfile = p;
  103. if(outfile){
  104. p = utfrrune(outfile, '.');
  105. if(p)
  106. if(p[1] == 's' && p[2] == 0)
  107. p[0] = 0;
  108. p = utfrune(outfile, 0);
  109. p[0] = '.';
  110. p[1] = thechar;
  111. p[2] = 0;
  112. } else
  113. outfile = "/dev/null";
  114. }
  115. p = getenv("INCLUDE");
  116. if(p) {
  117. setinclude(p);
  118. } else {
  119. if(systemtype(Plan9)) {
  120. sprint(incfile,"/%s/include", thestring);
  121. setinclude(strdup(incfile));
  122. }
  123. }
  124. of = mycreat(outfile, 0664);
  125. if(of < 0) {
  126. yyerror("%ca: cannot create %s", thechar, outfile);
  127. errorexit();
  128. }
  129. Binit(&obuf, of, OWRITE);
  130. pass = 1;
  131. pinit(file);
  132. for(i=0; i<nDlist; i++)
  133. dodefine(Dlist[i]);
  134. yyparse();
  135. if(nerrors) {
  136. cclean();
  137. return nerrors;
  138. }
  139. pass = 2;
  140. outhist();
  141. pinit(file);
  142. for(i=0; i<nDlist; i++)
  143. dodefine(Dlist[i]);
  144. yyparse();
  145. cclean();
  146. return nerrors;
  147. }
  148. struct
  149. {
  150. char *name;
  151. ushort type;
  152. ushort value;
  153. } itab[] =
  154. {
  155. "SP", LSP, D_AUTO,
  156. "SB", LSB, D_EXTERN,
  157. "FP", LFP, D_PARAM,
  158. "PC", LPC, D_BRANCH,
  159. "R0", LRREG, D_R0+0, /* 960 pfp, r0 */
  160. "R1", LRREG, D_R0+1, /* 960 sp, r1 */
  161. "R2", LRREG, D_R0+2, /* 960 rip, r2 */
  162. "R3", LRREG, D_R0+3,
  163. "R4", LRREG, D_R0+4,
  164. "R5", LRREG, D_R0+5,
  165. "R6", LRREG, D_R0+6,
  166. "R7", LRREG, D_R0+7,
  167. "R8", LRREG, D_R0+8,
  168. "R9", LRREG, D_R0+9,
  169. "R10", LRREG, D_R0+10,
  170. "R11", LRREG, D_R0+11,
  171. "R12", LRREG, D_R0+12,
  172. "R13", LRREG, D_R0+13,
  173. "R14", LRREG, D_R0+14,
  174. "R15", LRREG, D_R0+15,
  175. "R16", LRREG, D_R0+16, /* g0 */
  176. "R17", LRREG, D_R0+17,
  177. "R18", LRREG, D_R0+18,
  178. "R19", LRREG, D_R0+19,
  179. "R20", LRREG, D_R0+20,
  180. "R21", LRREG, D_R0+21,
  181. "R22", LRREG, D_R0+22,
  182. "R23", LRREG, D_R0+23,
  183. "R24", LRREG, D_R0+24,
  184. "R25", LRREG, D_R0+25,
  185. "R26", LRREG, D_R0+26,
  186. "R27", LRREG, D_R0+27,
  187. "R28", LRREG, D_R0+28, /* static register */
  188. "R29", LRREG, D_R0+29, /* stack register */
  189. "R30", LRREG, D_R0+30, /* link register */
  190. "R31", LRREG, D_R0+31, /* 960 fp, g15 */
  191. "DATA", LTYPE1, ADATA,
  192. "ADJSP", LTYPEX, AADJSP,
  193. "SYSCALL", LTYPEX, ASYSCALL,
  194. "CALL", LTYPEX, ACALL,
  195. "CALLS", LTYPEX, ACALLS,
  196. "GLOBL", LTYPEB, AGLOBL,
  197. "TEXT", LTYPEB, ATEXT,
  198. "LONG", LTYPES, ALONG,
  199. "NOP", LTYPES, ANOP,
  200. "RTS", LTYPEB, ARTS,
  201. "ADDC", LTYPEB, AADDC,
  202. "ADDI", LTYPEB, AADDI,
  203. "ADDO", LTYPEB, AADDO,
  204. "ALTERBIT", LTYPEB, AALTERBIT,
  205. "AND", LTYPEB, AAND,
  206. "ANDNOT", LTYPEB, AANDNOT,
  207. "ATADD", LTYPEB, AATADD,
  208. "ATMOD", LTYPEB, AATMOD,
  209. "B", LTYPED, AB,
  210. "BAL", LTYPED, ABAL,
  211. "BBC", LTYPED, ABBC,
  212. "BBS", LTYPED, ABBS,
  213. "BE", LTYPED, ABE,
  214. "BG", LTYPED, ABG,
  215. "BGE", LTYPED, ABGE,
  216. "BL", LTYPED, ABL,
  217. "BLE", LTYPED, ABLE,
  218. "BNE", LTYPED, ABNE,
  219. "BNO", LTYPED, ABNO,
  220. "BO", LTYPED, ABO,
  221. "CHKBIT", LTYPEB, ACHKBIT,
  222. "CLRBIT", LTYPEB, ACLRBIT,
  223. "CMPDECI", LTYPEB, ACMPDECI,
  224. "CMPDECO", LTYPEB, ACMPDECO,
  225. "CMPI", LTYPEB, ACMPI,
  226. "CMPIBE", LTYPEB, ACMPIBE,
  227. "CMPIBG", LTYPEB, ACMPIBG,
  228. "CMPIBGE", LTYPEB, ACMPIBGE,
  229. "CMPIBL", LTYPEB, ACMPIBL,
  230. "CMPIBLE", LTYPEB, ACMPIBLE,
  231. "CMPIBNE", LTYPEB, ACMPIBNE,
  232. "CMPIBNO", LTYPEB, ACMPIBNO,
  233. "CMPIBO", LTYPEB, ACMPIBO,
  234. "CMPINCI", LTYPEB, ACMPINCI,
  235. "CMPINCO", LTYPEB, ACMPINCO,
  236. "CMPO", LTYPEB, ACMPO,
  237. "CMPOBE", LTYPEB, ACMPOBE,
  238. "CMPOBG", LTYPEB, ACMPOBG,
  239. "CMPOBGE", LTYPEB, ACMPOBGE,
  240. "CMPOBL", LTYPEB, ACMPOBL,
  241. "CMPOBLE", LTYPEB, ACMPOBLE,
  242. "CMPOBNE", LTYPEB, ACMPOBNE,
  243. "CONCMPI", LTYPEB, ACONCMPI,
  244. "CONCMPO", LTYPEB, ACONCMPO,
  245. "DADDC", LTYPEB, ADADDC,
  246. "DIVI", LTYPEB, ADIVI,
  247. "DIVO", LTYPEB, ADIVO,
  248. "DMOVT", LTYPEB, ADMOVT,
  249. "DSUBC", LTYPEB, ADSUBC,
  250. "EDIV", LTYPEB, AEDIV,
  251. "EMUL", LTYPEB, AEMUL,
  252. "EXTRACT", LTYPEB, AEXTRACT,
  253. "FAULTE", LTYPEB, AFAULTE,
  254. "FAULTG", LTYPEB, AFAULTG,
  255. "FAULTGE", LTYPEB, AFAULTGE,
  256. "FAULTL", LTYPEB, AFAULTL,
  257. "FAULTLE", LTYPEB, AFAULTLE,
  258. "FAULTNE", LTYPEB, AFAULTNE,
  259. "FAULTNO", LTYPEB, AFAULTNO,
  260. "FAULTO", LTYPEB, AFAULTO,
  261. "FLUSHREG", LTYPEB, AFLUSHREG,
  262. "FMARK", LTYPEB, AFMARK,
  263. "MARK", LTYPEB, AMARK,
  264. "MODAC", LTYPEB, AMODAC,
  265. "MODI", LTYPEB, AMODI,
  266. "MODIFY", LTYPEB, AMODIFY,
  267. "MODPC", LTYPEB, AMODPC,
  268. "MODTC", LTYPEB, AMODTC,
  269. "MOV", LTYPEB, AMOV,
  270. "MOVA", LTYPEB, AMOVA,
  271. "MOVIB", LTYPEB, AMOVIB,
  272. "MOVIS", LTYPEB, AMOVIS,
  273. "MOVOB", LTYPEB, AMOVOB,
  274. "MOVOS", LTYPEB, AMOVOS,
  275. "MOVQ", LTYPEB, AMOVQ,
  276. "MOVT", LTYPEB, AMOVT,
  277. "MOVV", LTYPEB, AMOVV,
  278. "MULI", LTYPEB, AMULI,
  279. "MULO", LTYPEB, AMULO,
  280. "NAND", LTYPEB, ANAND,
  281. "NOR", LTYPEB, ANOR,
  282. "NOT", LTYPEB, ANOT,
  283. "NOTAND", LTYPEB, ANOTAND,
  284. "NOTBIT", LTYPEB, ANOTBIT,
  285. "NOTOR", LTYPEB, ANOTOR,
  286. "OR", LTYPEB, AOR,
  287. "ORNOT", LTYPEB, AORNOT,
  288. "REMI", LTYPEB, AREMI,
  289. "REMO", LTYPEB, AREMO,
  290. "RET", LTYPEB, ARET,
  291. "ROTATE", LTYPEB, AROTATE,
  292. "SCANBIT", LTYPEB, ASCANBIT,
  293. "SCANBYTE", LTYPEB, ASCANBYTE,
  294. "SETBIT", LTYPEB, ASETBIT,
  295. "SHLI", LTYPEB, ASHLI,
  296. "SHLO", LTYPEB, ASHLO,
  297. "SHRDI", LTYPEB, ASHRDI,
  298. "SHRI", LTYPEB, ASHRI,
  299. "SHRO", LTYPEB, ASHRO,
  300. "SPANBIT", LTYPEB, ASPANBIT,
  301. "SUBC", LTYPEB, ASUBC,
  302. "SUBI", LTYPEB, ASUBI,
  303. "SUBO", LTYPEB, ASUBO,
  304. "SYNCF", LTYPEB, ASYNCF,
  305. "SYNMOV", LTYPEB, ASYNMOV,
  306. "SYNMOVQ", LTYPEB, ASYNMOVQ,
  307. "SYNMOVV", LTYPEB, ASYNMOVV,
  308. "SYSCTL", LTYPEB, ASYSCTL,
  309. "END", LTYPEB, AEND,
  310. "TESTE", LTYPED, ATESTE,
  311. "TESTG", LTYPED, ATESTG,
  312. "TESTGE", LTYPED, ATESTGE,
  313. "TESTL", LTYPED, ATESTL,
  314. "TESTLE", LTYPED, ATESTLE,
  315. "TESTNE", LTYPED, ATESTNE,
  316. "TESTNO", LTYPED, ATESTNO,
  317. "TESTO", LTYPED, ATESTO,
  318. "XNOR", LTYPEB, AXNOR,
  319. "XOR", LTYPEB, AXOR,
  320. 0
  321. };
  322. void
  323. cinit(void)
  324. {
  325. Sym *s;
  326. int i;
  327. nullgen.sym = S;
  328. nullgen.offset = 0;
  329. if(FPCHIP)
  330. nullgen.dval = 0;
  331. for(i=0; i<sizeof(nullgen.sval); i++)
  332. nullgen.sval[i] = 0;
  333. nullgen.type = D_NONE;
  334. nullgen.index = D_NONE;
  335. nullgen.scale = 0;
  336. nerrors = 0;
  337. iostack = I;
  338. iofree = I;
  339. peekc = IGN;
  340. nhunk = 0;
  341. for(i=0; i<NHASH; i++)
  342. hash[i] = S;
  343. for(i=0; itab[i].name; i++) {
  344. s = slookup(itab[i].name);
  345. if(s->type != LNAME)
  346. yyerror("double initialization %s", itab[i].name);
  347. s->type = itab[i].type;
  348. s->value = itab[i].value;
  349. }
  350. ALLOCN(pathname, 0, 100);
  351. if(getwd(pathname, 99) == 0) {
  352. ALLOCN(pathname, 100, 900);
  353. if(getwd(pathname, 999) == 0)
  354. strcpy(pathname, "/???");
  355. }
  356. }
  357. void
  358. checkscale(int scale)
  359. {
  360. switch(scale) {
  361. case 1:
  362. case 2:
  363. case 4:
  364. case 8:
  365. case 16:
  366. return;
  367. }
  368. yyerror("scale must be 1<<[01234]: %d", scale);
  369. }
  370. void
  371. syminit(Sym *s)
  372. {
  373. s->type = LNAME;
  374. s->value = 0;
  375. }
  376. void
  377. cclean(void)
  378. {
  379. Gen2 g2;
  380. g2.from = nullgen;
  381. g2.to = nullgen;
  382. outcode(AEND, &g2);
  383. Bflush(&obuf);
  384. }
  385. void
  386. zname(char *n, int t, int s)
  387. {
  388. Bputc(&obuf, ANAME); /* as */
  389. Bputc(&obuf, t); /* type */
  390. Bputc(&obuf, s); /* sym */
  391. while(*n) {
  392. Bputc(&obuf, *n);
  393. n++;
  394. }
  395. Bputc(&obuf, 0);
  396. }
  397. void
  398. zaddr(Gen *a, int s)
  399. {
  400. long l;
  401. int i, t;
  402. char *n;
  403. Ieee e;
  404. t = 0;
  405. if(a->index != D_NONE || a->scale != 0)
  406. t |= T_INDEX;
  407. if(s != 0)
  408. t |= T_SYM;
  409. switch(a->type) {
  410. default:
  411. t |= T_TYPE;
  412. case D_NONE:
  413. if(a->offset != 0)
  414. t |= T_OFFSET;
  415. break;
  416. case D_FCONST:
  417. t |= T_FCONST;
  418. break;
  419. case D_SCONST:
  420. t |= T_SCONST;
  421. break;
  422. }
  423. Bputc(&obuf, t);
  424. if(t & T_INDEX) { /* implies index, scale */
  425. Bputc(&obuf, a->index);
  426. Bputc(&obuf, a->scale);
  427. }
  428. if(t & T_OFFSET) { /* implies offset */
  429. l = a->offset;
  430. Bputc(&obuf, l);
  431. Bputc(&obuf, l>>8);
  432. Bputc(&obuf, l>>16);
  433. Bputc(&obuf, l>>24);
  434. }
  435. if(t & T_SYM) /* implies sym */
  436. Bputc(&obuf, s);
  437. if(t & T_FCONST) {
  438. ieeedtod(&e, a->dval);
  439. l = e.l;
  440. Bputc(&obuf, l);
  441. Bputc(&obuf, l>>8);
  442. Bputc(&obuf, l>>16);
  443. Bputc(&obuf, l>>24);
  444. l = e.h;
  445. Bputc(&obuf, l);
  446. Bputc(&obuf, l>>8);
  447. Bputc(&obuf, l>>16);
  448. Bputc(&obuf, l>>24);
  449. return;
  450. }
  451. if(t & T_SCONST) {
  452. n = a->sval;
  453. for(i=0; i<NSNAME; i++) {
  454. Bputc(&obuf, *n);
  455. n++;
  456. }
  457. return;
  458. }
  459. if(t & T_TYPE)
  460. Bputc(&obuf, a->type);
  461. }
  462. void
  463. outcode(int a, Gen2 *g2)
  464. {
  465. int sf, st, t;
  466. Sym *s;
  467. if(pass == 1)
  468. goto out;
  469. jackpot:
  470. sf = 0;
  471. s = g2->from.sym;
  472. while(s != S) {
  473. sf = s->sym;
  474. if(sf < 0 || sf >= NSYM)
  475. sf = 0;
  476. t = g2->from.type;
  477. if(t == D_ADDR)
  478. t = g2->from.index;
  479. if(h[sf].type == t)
  480. if(h[sf].sym == s)
  481. break;
  482. zname(s->name, t, sym);
  483. s->sym = sym;
  484. h[sym].sym = s;
  485. h[sym].type = t;
  486. sf = sym;
  487. sym++;
  488. if(sym >= NSYM)
  489. sym = 1;
  490. break;
  491. }
  492. st = 0;
  493. s = g2->to.sym;
  494. while(s != S) {
  495. st = s->sym;
  496. if(st < 0 || st >= NSYM)
  497. st = 0;
  498. t = g2->to.type;
  499. if(t == D_ADDR)
  500. t = g2->to.index;
  501. if(h[st].type == t)
  502. if(h[st].sym == s)
  503. break;
  504. zname(s->name, t, sym);
  505. s->sym = sym;
  506. h[sym].sym = s;
  507. h[sym].type = t;
  508. st = sym;
  509. sym++;
  510. if(sym >= NSYM)
  511. sym = 1;
  512. if(st == sf)
  513. goto jackpot;
  514. break;
  515. }
  516. Bputc(&obuf, a);
  517. Bputc(&obuf, lineno);
  518. Bputc(&obuf, lineno>>8);
  519. Bputc(&obuf, lineno>>16);
  520. Bputc(&obuf, lineno>>24);
  521. switch(t = g2->type) {
  522. case D_NONE:
  523. t = 0; /* 0 D_NONE */
  524. break;
  525. default:
  526. if(t < D_R0 || t >= D_R0+32) {
  527. yyerror("bad type in outcode: %d", t);
  528. t = D_R0;
  529. }
  530. t = (t - D_R0) + 1; /* 1-32 D_R0+ */
  531. break;
  532. case D_CONST:
  533. t = g2->offset;
  534. if(t >= 32) {
  535. yyerror("bad offset in outcode", t);
  536. t = 0;
  537. }
  538. t = (t - 0) + 33; /* 33-64 D_CONST+ */
  539. break;
  540. }
  541. Bputc(&obuf, t);
  542. zaddr(&g2->from, sf);
  543. zaddr(&g2->to, st);
  544. out:
  545. if(a != AGLOBL && a != ADATA)
  546. pc++;
  547. }
  548. void
  549. outhist(void)
  550. {
  551. Gen g;
  552. Hist *h;
  553. char *p, *q, *op;
  554. int n;
  555. g = nullgen;
  556. for(h = hist; h != H; h = h->link) {
  557. p = h->name;
  558. op = 0;
  559. if(p && p[0] != '/' && h->offset == 0 && pathname && pathname[0] == '/') {
  560. op = p;
  561. p = pathname;
  562. }
  563. while(p) {
  564. q = strchr(p, '/');
  565. if(q) {
  566. n = q-p;
  567. if(n == 0)
  568. n = 1; /* leading "/" */
  569. q++;
  570. } else {
  571. n = strlen(p);
  572. q = 0;
  573. }
  574. if(n) {
  575. Bputc(&obuf, ANAME);
  576. Bputc(&obuf, D_FILE); /* type */
  577. Bputc(&obuf, 1); /* sym */
  578. Bputc(&obuf, '<');
  579. Bwrite(&obuf, p, n);
  580. Bputc(&obuf, 0);
  581. }
  582. p = q;
  583. if(p == 0 && op) {
  584. p = op;
  585. op = 0;
  586. }
  587. }
  588. g.offset = h->offset;
  589. Bputc(&obuf, AHISTORY);
  590. Bputc(&obuf, h->line);
  591. Bputc(&obuf, h->line>>8);
  592. Bputc(&obuf, h->line>>16);
  593. Bputc(&obuf, h->line>>24);
  594. Bputc(&obuf, 0); /* reg */
  595. zaddr(&nullgen, 0);
  596. zaddr(&g, 0);
  597. }
  598. }
  599. void
  600. praghjdicks(void)
  601. {
  602. while(getnsc() != '\n')
  603. ;
  604. }
  605. void
  606. pragvararg(void)
  607. {
  608. while(getnsc() != '\n')
  609. ;
  610. }
  611. void
  612. pragfpround(void)
  613. {
  614. while(getnsc() != '\n')
  615. ;
  616. }
  617. #include "../cc/lexbody"
  618. #include "../cc/macbody"
  619. #include "../cc/compat"