lex.c 23 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516
  1. #include "cc.h"
  2. #include "y.tab.h"
  3. #ifndef CPP
  4. #define CPP "/bin/cpp"
  5. #endif
  6. /*
  7. * known debug flags
  8. * -o file output file
  9. * -D name define
  10. * -I path include
  11. * -a acid declaration output
  12. * -M constant multiplication
  13. * -B non ANSI
  14. * -A !B
  15. * -d print declarations
  16. * -t print type trees
  17. * -L print every NAME symbol
  18. * -i print initialization
  19. * -F format specification check
  20. * -r print registerization
  21. * -v verbose printing
  22. * -X abort on error
  23. * -w print warnings
  24. * -m print add/sub/mul trees
  25. * -s print structure offsets (with -a or -aa)
  26. * -n print acid to file (%.c=%.acid) (with -a or -aa)
  27. * -p use standard cpp ANSI preprocessor (not on windows)
  28. * -V enable void* conversion warnings
  29. */
  30. void
  31. main(int argc, char *argv[])
  32. {
  33. char *defs[50], *p;
  34. int nproc, nout, status, i, c, ndef;
  35. memset(debug, 0, sizeof(debug));
  36. tinit();
  37. cinit();
  38. ginit();
  39. arginit();
  40. tufield = simplet((1L<<tfield->etype) | BUNSIGNED);
  41. ndef = 0;
  42. outfile = 0;
  43. include[ninclude++] = ".";
  44. ARGBEGIN {
  45. default:
  46. c = ARGC();
  47. if(c >= 0 && c < sizeof(debug))
  48. debug[c]++;
  49. break;
  50. case 'o':
  51. outfile = ARGF();
  52. break;
  53. case 'D':
  54. p = ARGF();
  55. if(p) {
  56. defs[ndef++] = p;
  57. dodefine(p);
  58. }
  59. break;
  60. case 'I':
  61. p = ARGF();
  62. setinclude(p);
  63. break;
  64. } ARGEND
  65. if(argc < 1 && outfile == 0) {
  66. print("usage: %cc [-options] files\n", thechar);
  67. errorexit();
  68. }
  69. if(argc > 1 && systemtype(Windows)){
  70. print("can't compile multiple files on windows\n");
  71. errorexit();
  72. }
  73. if(argc > 1 && !systemtype(Windows)) {
  74. nproc = 1;
  75. if(p = getenv("NPROC"))
  76. nproc = atol(p); /* */
  77. c = 0;
  78. nout = 0;
  79. for(;;) {
  80. while(nout < nproc && argc > 0) {
  81. i = myfork();
  82. if(i < 0) {
  83. i = mywait(&status);
  84. if(i < 0) {
  85. print("cannot create a process\n");
  86. errorexit();
  87. }
  88. if(status)
  89. c++;
  90. nout--;
  91. continue;
  92. }
  93. if(i == 0) {
  94. fprint(2, "%s:\n", *argv);
  95. if (compile(*argv, defs, ndef))
  96. errorexit();
  97. exits(0);
  98. }
  99. nout++;
  100. argc--;
  101. argv++;
  102. }
  103. i = mywait(&status);
  104. if(i < 0) {
  105. if(c)
  106. errorexit();
  107. exits(0);
  108. }
  109. if(status)
  110. c++;
  111. nout--;
  112. }
  113. }
  114. if(argc == 0)
  115. c = compile("stdin", defs, ndef);
  116. else
  117. c = compile(argv[0], defs, ndef);
  118. if(c)
  119. errorexit();
  120. exits(0);
  121. }
  122. int
  123. compile(char *file, char **defs, int ndef)
  124. {
  125. char ofile[100], incfile[20];
  126. char *p, *av[100], opt[256];
  127. int i, c, fd[2];
  128. strcpy(ofile, file);
  129. p = utfrrune(ofile, pathchar());
  130. if(p) {
  131. include[0] = ofile;
  132. *p++ = 0;
  133. } else
  134. p = ofile;
  135. if(outfile == 0) {
  136. outfile = p;
  137. if(outfile) {
  138. if(p = utfrrune(outfile, '.'))
  139. if(p[1] == 'c' && p[2] == 0)
  140. p[0] = 0;
  141. p = utfrune(outfile, 0);
  142. if(debug['a'] && debug['n'])
  143. strcat(p, ".acid");
  144. else if(debug['P'] && debug['n'])
  145. strcat(p, "_pickle.c");
  146. else {
  147. p[0] = '.';
  148. p[1] = thechar;
  149. p[2] = 0;
  150. }
  151. } else
  152. outfile = "/dev/null";
  153. }
  154. if(p = getenv("INCLUDE")) {
  155. setinclude(p);
  156. } else {
  157. if(systemtype(Plan9)) {
  158. sprint(incfile, "/%s/include", thestring);
  159. setinclude(strdup(incfile));
  160. setinclude("/sys/include");
  161. }
  162. }
  163. if((debug['a'] || debug['P']) && !debug['n']) {
  164. outfile = 0;
  165. Binit(&outbuf, 1, OWRITE);
  166. } else {
  167. c = mycreat(outfile, 0664);
  168. if(c < 0) {
  169. diag(Z, "cannot open %s", outfile);
  170. outfile = 0;
  171. errorexit();
  172. }
  173. Binit(&outbuf, c, OWRITE);
  174. }
  175. newio();
  176. /* Use an ANSI preprocessor */
  177. if(debug['p']) {
  178. if(systemtype(Windows)) {
  179. diag(Z, "-p option not supported on windows");
  180. errorexit();
  181. }
  182. if(myaccess(file) < 0) {
  183. diag(Z, "%s does not exist", file);
  184. errorexit();
  185. }
  186. if(mypipe(fd) < 0) {
  187. diag(Z, "pipe failed");
  188. errorexit();
  189. }
  190. switch(myfork()) {
  191. case -1:
  192. diag(Z, "fork failed");
  193. errorexit();
  194. case 0:
  195. close(fd[0]);
  196. mydup(fd[1], 1);
  197. close(fd[1]);
  198. av[0] = CPP;
  199. i = 1;
  200. if(debug['+']) {
  201. sprint(opt, "-+");
  202. av[i++] = strdup(opt);
  203. }
  204. for(c = 0; c < ndef; c++) {
  205. sprint(opt, "-D%s", defs[c]);
  206. av[i++] = strdup(opt);
  207. }
  208. for(c = 0; c < ninclude; c++) {
  209. sprint(opt, "-I%s", include[c]);
  210. av[i++] = strdup(opt);
  211. }
  212. if(strcmp(file, "stdin") != 0)
  213. av[i++] = file;
  214. av[i] = 0;
  215. if(debug['p'] > 1) {
  216. for(c = 0; c < i; c++)
  217. fprint(2, "%s ", av[c]);
  218. print("\n");
  219. }
  220. myexec(av[0], av);
  221. fprint(2, "can't exec C preprocessor %s: %r\n", CPP);
  222. errorexit();
  223. default:
  224. close(fd[1]);
  225. newfile(file, fd[0]);
  226. break;
  227. }
  228. } else {
  229. if(strcmp(file, "stdin") == 0)
  230. newfile(file, 0);
  231. else
  232. newfile(file, -1);
  233. }
  234. yyparse();
  235. if(!debug['a'] && !debug['P'])
  236. gclean();
  237. return nerrors;
  238. }
  239. void
  240. errorexit(void)
  241. {
  242. if(outfile)
  243. remove(outfile);
  244. exits("error");
  245. }
  246. void
  247. pushio(void)
  248. {
  249. Io *i;
  250. i = iostack;
  251. if(i == I) {
  252. yyerror("botch in pushio");
  253. errorexit();
  254. }
  255. i->p = fi.p;
  256. i->c = fi.c;
  257. }
  258. void
  259. newio(void)
  260. {
  261. Io *i;
  262. static pushdepth = 0;
  263. i = iofree;
  264. if(i == I) {
  265. pushdepth++;
  266. if(pushdepth > 1000) {
  267. yyerror("macro/io expansion too deep");
  268. errorexit();
  269. }
  270. i = alloc(sizeof(*i));
  271. } else
  272. iofree = i->link;
  273. i->c = 0;
  274. i->f = -1;
  275. ionext = i;
  276. }
  277. void
  278. newfile(char *s, int f)
  279. {
  280. Io *i;
  281. if(debug['e'])
  282. print("%L: %s\n", lineno, s);
  283. i = ionext;
  284. i->link = iostack;
  285. iostack = i;
  286. i->f = f;
  287. if(f < 0)
  288. i->f = open(s, 0);
  289. if(i->f < 0) {
  290. yyerror("%cc: %r: %s", thechar, s);
  291. errorexit();
  292. }
  293. fi.c = 0;
  294. linehist(s, 0);
  295. }
  296. Sym*
  297. slookup(char *s)
  298. {
  299. strcpy(symb, s);
  300. return lookup();
  301. }
  302. Sym*
  303. lookup(void)
  304. {
  305. Sym *s;
  306. ulong h;
  307. char *p;
  308. int c, n;
  309. h = 0;
  310. for(p=symb; *p;) {
  311. h = h * 3;
  312. h += *p++;
  313. }
  314. n = (p - symb) + 1;
  315. if((long)h < 0)
  316. h = ~h;
  317. h %= NHASH;
  318. c = symb[0];
  319. for(s = hash[h]; s != S; s = s->link) {
  320. if(s->name[0] != c)
  321. continue;
  322. if(strcmp(s->name, symb) == 0)
  323. return s;
  324. }
  325. s = alloc(sizeof(*s));
  326. s->name = alloc(n);
  327. memmove(s->name, symb, n);
  328. strcpy(s->name, symb);
  329. s->link = hash[h];
  330. hash[h] = s;
  331. syminit(s);
  332. return s;
  333. }
  334. void
  335. syminit(Sym *s)
  336. {
  337. s->lexical = LNAME;
  338. s->block = 0;
  339. s->offset = 0;
  340. s->type = T;
  341. s->suetag = T;
  342. s->class = CXXX;
  343. s->aused = 0;
  344. }
  345. #define EOF (-1)
  346. #define IGN (-2)
  347. #define ESC (1<<20)
  348. #define GETC() ((--fi.c < 0)? filbuf(): (*fi.p++ & 0xff))
  349. enum
  350. {
  351. Numdec = 1<<0,
  352. Numlong = 1<<1,
  353. Numuns = 1<<2,
  354. Numvlong = 1<<3,
  355. Numflt = 1<<4,
  356. };
  357. long
  358. yylex(void)
  359. {
  360. vlong vv;
  361. long c, c1;
  362. char *cp;
  363. Rune rune;
  364. Sym *s;
  365. if(peekc != IGN) {
  366. c = peekc;
  367. peekc = IGN;
  368. goto l1;
  369. }
  370. l0:
  371. c = GETC();
  372. l1:
  373. if(c >= Runeself) {
  374. /*
  375. * extension --
  376. * all multibyte runes are alpha
  377. */
  378. cp = symb;
  379. goto talph;
  380. }
  381. if(isspace(c)) {
  382. if(c == '\n')
  383. lineno++;
  384. goto l0;
  385. }
  386. if(isalpha(c)) {
  387. cp = symb;
  388. if(c != 'L')
  389. goto talph;
  390. *cp++ = c;
  391. c = GETC();
  392. if(c == '\'') {
  393. /* L'x' */
  394. c = escchar('\'', 1, 0);
  395. if(c == EOF)
  396. c = '\'';
  397. c1 = escchar('\'', 1, 0);
  398. if(c1 != EOF) {
  399. yyerror("missing '");
  400. peekc = c1;
  401. }
  402. yylval.vval = convvtox(c, TUSHORT);
  403. return LUCONST;
  404. }
  405. if(c == '"') {
  406. goto caselq;
  407. }
  408. goto talph;
  409. }
  410. if(isdigit(c))
  411. goto tnum;
  412. switch(c)
  413. {
  414. case EOF:
  415. peekc = EOF;
  416. return -1;
  417. case '_':
  418. cp = symb;
  419. goto talph;
  420. case '#':
  421. domacro();
  422. goto l0;
  423. case '.':
  424. c1 = GETC();
  425. if(isdigit(c1)) {
  426. cp = symb;
  427. *cp++ = c;
  428. c = c1;
  429. c1 = 0;
  430. goto casedot;
  431. }
  432. break;
  433. case '"':
  434. strcpy(symb, "\"<string>\"");
  435. cp = alloc(0);
  436. c1 = 0;
  437. /* "..." */
  438. for(;;) {
  439. c = escchar('"', 0, 1);
  440. if(c == EOF)
  441. break;
  442. if(c & ESC) {
  443. cp = allocn(cp, c1, 1);
  444. cp[c1++] = c;
  445. } else {
  446. rune = c;
  447. c = runelen(rune);
  448. cp = allocn(cp, c1, c);
  449. runetochar(cp+c1, &rune);
  450. c1 += c;
  451. }
  452. }
  453. yylval.sval.l = c1;
  454. do {
  455. cp = allocn(cp, c1, 1);
  456. cp[c1++] = 0;
  457. } while(c1 & MAXALIGN);
  458. yylval.sval.s = cp;
  459. return LSTRING;
  460. caselq:
  461. /* L"..." */
  462. strcpy(symb, "\"L<string>\"");
  463. cp = alloc(0);
  464. c1 = 0;
  465. for(;;) {
  466. c = escchar('"', 1, 0);
  467. if(c == EOF)
  468. break;
  469. cp = allocn(cp, c1, sizeof(ushort));
  470. *(ushort*)(cp + c1) = c;
  471. c1 += sizeof(ushort);
  472. }
  473. yylval.sval.l = c1;
  474. do {
  475. cp = allocn(cp, c1, sizeof(ushort));
  476. *(ushort*)(cp + c1) = 0;
  477. c1 += sizeof(ushort);
  478. } while(c1 & MAXALIGN);
  479. yylval.sval.s = cp;
  480. return LLSTRING;
  481. case '\'':
  482. /* '.' */
  483. c = escchar('\'', 0, 0);
  484. if(c == EOF)
  485. c = '\'';
  486. c1 = escchar('\'', 0, 0);
  487. if(c1 != EOF) {
  488. yyerror("missing '");
  489. peekc = c1;
  490. }
  491. vv = c;
  492. yylval.vval = convvtox(vv, TUCHAR);
  493. if(yylval.vval != vv)
  494. yyerror("overflow in character constant: 0x%lx", c);
  495. else
  496. if(c & 0x80)
  497. warn(Z, "sign-extended character constant");
  498. yylval.vval = convvtox(vv, TCHAR);
  499. return LCONST;
  500. case '/':
  501. c1 = GETC();
  502. if(c1 == '*') {
  503. for(;;) {
  504. c = getr();
  505. while(c == '*') {
  506. c = getr();
  507. if(c == '/')
  508. goto l0;
  509. }
  510. if(c == EOF) {
  511. yyerror("eof in comment");
  512. errorexit();
  513. }
  514. }
  515. }
  516. if(c1 == '/') {
  517. for(;;) {
  518. c = getr();
  519. if(c == '\n')
  520. goto l0;
  521. if(c == EOF) {
  522. yyerror("eof in comment");
  523. errorexit();
  524. }
  525. }
  526. }
  527. if(c1 == '=')
  528. return LDVE;
  529. break;
  530. case '*':
  531. c1 = GETC();
  532. if(c1 == '=')
  533. return LMLE;
  534. break;
  535. case '%':
  536. c1 = GETC();
  537. if(c1 == '=')
  538. return LMDE;
  539. break;
  540. case '+':
  541. c1 = GETC();
  542. if(c1 == '+')
  543. return LPP;
  544. if(c1 == '=')
  545. return LPE;
  546. break;
  547. case '-':
  548. c1 = GETC();
  549. if(c1 == '-')
  550. return LMM;
  551. if(c1 == '=')
  552. return LME;
  553. if(c1 == '>')
  554. return LMG;
  555. break;
  556. case '>':
  557. c1 = GETC();
  558. if(c1 == '>') {
  559. c = LRSH;
  560. c1 = GETC();
  561. if(c1 == '=')
  562. return LRSHE;
  563. break;
  564. }
  565. if(c1 == '=')
  566. return LGE;
  567. break;
  568. case '<':
  569. c1 = GETC();
  570. if(c1 == '<') {
  571. c = LLSH;
  572. c1 = GETC();
  573. if(c1 == '=')
  574. return LLSHE;
  575. break;
  576. }
  577. if(c1 == '=')
  578. return LLE;
  579. break;
  580. case '=':
  581. c1 = GETC();
  582. if(c1 == '=')
  583. return LEQ;
  584. break;
  585. case '!':
  586. c1 = GETC();
  587. if(c1 == '=')
  588. return LNE;
  589. break;
  590. case '&':
  591. c1 = GETC();
  592. if(c1 == '&')
  593. return LANDAND;
  594. if(c1 == '=')
  595. return LANDE;
  596. break;
  597. case '|':
  598. c1 = GETC();
  599. if(c1 == '|')
  600. return LOROR;
  601. if(c1 == '=')
  602. return LORE;
  603. break;
  604. case '^':
  605. c1 = GETC();
  606. if(c1 == '=')
  607. return LXORE;
  608. break;
  609. default:
  610. return c;
  611. }
  612. peekc = c1;
  613. return c;
  614. talph:
  615. /*
  616. * cp is set to symb and some
  617. * prefix has been stored
  618. */
  619. for(;;) {
  620. if(c >= Runeself) {
  621. for(c1=0;;) {
  622. cp[c1++] = c;
  623. if(fullrune(cp, c1))
  624. break;
  625. c = GETC();
  626. }
  627. cp += c1;
  628. c = GETC();
  629. continue;
  630. }
  631. if(!isalnum(c) && c != '_')
  632. break;
  633. *cp++ = c;
  634. c = GETC();
  635. }
  636. *cp = 0;
  637. if(debug['L'])
  638. print("%L: %s\n", lineno, symb);
  639. peekc = c;
  640. s = lookup();
  641. if(s->macro) {
  642. newio();
  643. cp = ionext->b;
  644. macexpand(s, cp);
  645. pushio();
  646. ionext->link = iostack;
  647. iostack = ionext;
  648. fi.p = cp;
  649. fi.c = strlen(cp);
  650. if(peekc != IGN) {
  651. cp[fi.c++] = peekc;
  652. cp[fi.c] = 0;
  653. peekc = IGN;
  654. }
  655. goto l0;
  656. }
  657. yylval.sym = s;
  658. if(s->class == CTYPEDEF || s->class == CTYPESTR)
  659. return LTYPE;
  660. return s->lexical;
  661. tnum:
  662. c1 = 0;
  663. cp = symb;
  664. if(c != '0') {
  665. c1 |= Numdec;
  666. for(;;) {
  667. *cp++ = c;
  668. c = GETC();
  669. if(isdigit(c))
  670. continue;
  671. goto dc;
  672. }
  673. }
  674. *cp++ = c;
  675. c = GETC();
  676. if(c == 'x' || c == 'X')
  677. for(;;) {
  678. *cp++ = c;
  679. c = GETC();
  680. if(isdigit(c))
  681. continue;
  682. if(c >= 'a' && c <= 'f')
  683. continue;
  684. if(c >= 'A' && c <= 'F')
  685. continue;
  686. if(cp == symb+2)
  687. yyerror("malformed hex constant");
  688. goto ncu;
  689. }
  690. if(c < '0' || c > '7')
  691. goto dc;
  692. for(;;) {
  693. if(c >= '0' && c <= '7') {
  694. *cp++ = c;
  695. c = GETC();
  696. continue;
  697. }
  698. goto ncu;
  699. }
  700. dc:
  701. if(c == '.')
  702. goto casedot;
  703. if(c == 'e' || c == 'E')
  704. goto casee;
  705. ncu:
  706. if((c == 'U' || c == 'u') && !(c1 & Numuns)) {
  707. c = GETC();
  708. c1 |= Numuns;
  709. goto ncu;
  710. }
  711. if((c == 'L' || c == 'l') && !(c1 & Numvlong)) {
  712. c = GETC();
  713. if(c1 & Numlong)
  714. c1 |= Numvlong;
  715. c1 |= Numlong;
  716. goto ncu;
  717. }
  718. *cp = 0;
  719. peekc = c;
  720. if(mpatov(symb, &yylval.vval))
  721. yyerror("overflow in constant");
  722. vv = yylval.vval;
  723. if(c1 & Numvlong) {
  724. if(c1 & Numuns) {
  725. c = LUVLCONST;
  726. goto nret;
  727. }
  728. yylval.vval = convvtox(yylval.vval, TVLONG);
  729. if(yylval.vval < 0) {
  730. c = LUVLCONST;
  731. goto nret;
  732. }
  733. c = LVLCONST;
  734. goto nret;
  735. }
  736. if(c1 & Numlong) {
  737. if(c1 & Numuns) {
  738. c = LULCONST;
  739. goto nret;
  740. }
  741. yylval.vval = convvtox(yylval.vval, TLONG);
  742. if(yylval.vval < 0) {
  743. c = LULCONST;
  744. goto nret;
  745. }
  746. c = LLCONST;
  747. goto nret;
  748. }
  749. if(c1 & Numuns) {
  750. c = LUCONST;
  751. goto nret;
  752. }
  753. yylval.vval = convvtox(yylval.vval, TINT);
  754. if(yylval.vval < 0) {
  755. c = LUCONST;
  756. goto nret;
  757. }
  758. c = LCONST;
  759. goto nret;
  760. nret:
  761. return c;
  762. casedot:
  763. for(;;) {
  764. *cp++ = c;
  765. c = GETC();
  766. if(!isdigit(c))
  767. break;
  768. }
  769. if(c != 'e' && c != 'E')
  770. goto caseout;
  771. casee:
  772. *cp++ = 'e';
  773. c = GETC();
  774. if(c == '+' || c == '-') {
  775. *cp++ = c;
  776. c = GETC();
  777. }
  778. if(!isdigit(c))
  779. yyerror("malformed fp constant exponent");
  780. while(isdigit(c)) {
  781. *cp++ = c;
  782. c = GETC();
  783. }
  784. caseout:
  785. if(c == 'L' || c == 'l') {
  786. c = GETC();
  787. c1 |= Numlong;
  788. } else
  789. if(c == 'F' || c == 'f') {
  790. c = GETC();
  791. c1 |= Numflt;
  792. }
  793. *cp = 0;
  794. peekc = c;
  795. yylval.dval = strtod(symb, nil);
  796. if(isInf(yylval.dval, 1) || isInf(yylval.dval, -1)) {
  797. yyerror("overflow in float constant");
  798. yylval.dval = 0;
  799. }
  800. if(c1 & Numflt)
  801. return LFCONST;
  802. return LDCONST;
  803. }
  804. /*
  805. * convert a string, s, to vlong in *v
  806. * return conversion overflow.
  807. * required syntax is [0[x]]d*
  808. */
  809. int
  810. mpatov(char *s, vlong *v)
  811. {
  812. vlong n, nn;
  813. int c;
  814. n = 0;
  815. c = *s;
  816. if(c == '0')
  817. goto oct;
  818. while(c = *s++) {
  819. if(c >= '0' && c <= '9')
  820. nn = n*10 + c-'0';
  821. else
  822. goto bad;
  823. if(n < 0 && nn >= 0)
  824. goto bad;
  825. n = nn;
  826. }
  827. goto out;
  828. oct:
  829. s++;
  830. c = *s;
  831. if(c == 'x' || c == 'X')
  832. goto hex;
  833. while(c = *s++) {
  834. if(c >= '0' || c <= '7')
  835. nn = n*8 + c-'0';
  836. else
  837. goto bad;
  838. if(n < 0 && nn >= 0)
  839. goto bad;
  840. n = nn;
  841. }
  842. goto out;
  843. hex:
  844. s++;
  845. while(c = *s++) {
  846. if(c >= '0' && c <= '9')
  847. c += 0-'0';
  848. else
  849. if(c >= 'a' && c <= 'f')
  850. c += 10-'a';
  851. else
  852. if(c >= 'A' && c <= 'F')
  853. c += 10-'A';
  854. else
  855. goto bad;
  856. nn = n*16 + c;
  857. if(n < 0 && nn >= 0)
  858. goto bad;
  859. n = nn;
  860. }
  861. out:
  862. *v = n;
  863. return 0;
  864. bad:
  865. *v = ~0;
  866. return 1;
  867. }
  868. int
  869. getc(void)
  870. {
  871. int c;
  872. if(peekc != IGN) {
  873. c = peekc;
  874. peekc = IGN;
  875. } else
  876. c = GETC();
  877. if(c == '\n')
  878. lineno++;
  879. if(c == EOF) {
  880. yyerror("End of file");
  881. errorexit();
  882. }
  883. return c;
  884. }
  885. long
  886. getr(void)
  887. {
  888. int c, i;
  889. char str[UTFmax+1];
  890. Rune rune;
  891. c = getc();
  892. if(c < Runeself)
  893. return c;
  894. i = 0;
  895. str[i++] = c;
  896. loop:
  897. c = getc();
  898. str[i++] = c;
  899. if(!fullrune(str, i))
  900. goto loop;
  901. c = chartorune(&rune, str);
  902. if(rune == Runeerror && c == 1) {
  903. nearln = lineno;
  904. diag(Z, "illegal rune in string");
  905. for(c=0; c<i; c++)
  906. print(" %.2x", *(uchar*)(str+c));
  907. print("\n");
  908. }
  909. return rune;
  910. }
  911. int
  912. getnsc(void)
  913. {
  914. int c;
  915. if(peekc != IGN) {
  916. c = peekc;
  917. peekc = IGN;
  918. } else
  919. c = GETC();
  920. for(;;) {
  921. if(!isspace(c))
  922. return c;
  923. if(c == '\n') {
  924. lineno++;
  925. return c;
  926. }
  927. c = GETC();
  928. }
  929. return 0;
  930. }
  931. void
  932. unget(int c)
  933. {
  934. peekc = c;
  935. if(c == '\n')
  936. lineno--;
  937. }
  938. long
  939. escchar(long e, int longflg, int escflg)
  940. {
  941. long c, l;
  942. int i;
  943. loop:
  944. c = getr();
  945. if(c == '\n') {
  946. yyerror("newline in string");
  947. return EOF;
  948. }
  949. if(c != '\\') {
  950. if(c == e)
  951. c = EOF;
  952. return c;
  953. }
  954. c = getr();
  955. if(c == 'x') {
  956. /*
  957. * note this is not ansi,
  958. * supposed to only accept 2 hex
  959. */
  960. i = 2;
  961. if(longflg)
  962. i = 4;
  963. l = 0;
  964. for(; i>0; i--) {
  965. c = getc();
  966. if(c >= '0' && c <= '9') {
  967. l = l*16 + c-'0';
  968. continue;
  969. }
  970. if(c >= 'a' && c <= 'f') {
  971. l = l*16 + c-'a' + 10;
  972. continue;
  973. }
  974. if(c >= 'A' && c <= 'F') {
  975. l = l*16 + c-'A' + 10;
  976. continue;
  977. }
  978. unget(c);
  979. break;
  980. }
  981. if(escflg)
  982. l |= ESC;
  983. return l;
  984. }
  985. if(c >= '0' && c <= '7') {
  986. /*
  987. * note this is not ansi,
  988. * supposed to only accept 3 oct
  989. */
  990. i = 2;
  991. if(longflg)
  992. i = 5;
  993. l = c - '0';
  994. for(; i>0; i--) {
  995. c = getc();
  996. if(c >= '0' && c <= '7') {
  997. l = l*8 + c-'0';
  998. continue;
  999. }
  1000. unget(c);
  1001. }
  1002. if(escflg)
  1003. l |= ESC;
  1004. return l;
  1005. }
  1006. switch(c)
  1007. {
  1008. case '\n': goto loop;
  1009. case 'n': return '\n';
  1010. case 't': return '\t';
  1011. case 'b': return '\b';
  1012. case 'r': return '\r';
  1013. case 'f': return '\f';
  1014. case 'a': return '\a';
  1015. case 'v': return '\v';
  1016. }
  1017. return c;
  1018. }
  1019. struct
  1020. {
  1021. char *name;
  1022. ushort lexical;
  1023. ushort type;
  1024. } itab[] =
  1025. {
  1026. "auto", LAUTO, 0,
  1027. "break", LBREAK, 0,
  1028. "case", LCASE, 0,
  1029. "char", LCHAR, TCHAR,
  1030. "const", LCONSTNT, 0,
  1031. "continue", LCONTINUE, 0,
  1032. "default", LDEFAULT, 0,
  1033. "do", LDO, 0,
  1034. "double", LDOUBLE, TDOUBLE,
  1035. "else", LELSE, 0,
  1036. "enum", LENUM, 0,
  1037. "extern", LEXTERN, 0,
  1038. "float", LFLOAT, TFLOAT,
  1039. "for", LFOR, 0,
  1040. "goto", LGOTO, 0,
  1041. "if", LIF, 0,
  1042. "int", LINT, TINT,
  1043. "long", LLONG, TLONG,
  1044. "register", LREGISTER, 0,
  1045. "return", LRETURN, 0,
  1046. "SET", LSET, 0,
  1047. "short", LSHORT, TSHORT,
  1048. "signed", LSIGNED, 0,
  1049. "signof", LSIGNOF, 0,
  1050. "sizeof", LSIZEOF, 0,
  1051. "static", LSTATIC, 0,
  1052. "struct", LSTRUCT, 0,
  1053. "switch", LSWITCH, 0,
  1054. "typedef", LTYPEDEF, 0,
  1055. "typestr", LTYPESTR, 0,
  1056. "union", LUNION, 0,
  1057. "unsigned", LUNSIGNED, 0,
  1058. "USED", LUSED, 0,
  1059. "void", LVOID, TVOID,
  1060. "volatile", LVOLATILE, 0,
  1061. "while", LWHILE, 0,
  1062. 0
  1063. };
  1064. void
  1065. cinit(void)
  1066. {
  1067. Sym *s;
  1068. int i;
  1069. Type *t;
  1070. nerrors = 0;
  1071. lineno = 1;
  1072. iostack = I;
  1073. iofree = I;
  1074. peekc = IGN;
  1075. nhunk = 0;
  1076. types[TXXX] = T;
  1077. types[TCHAR] = typ(TCHAR, T);
  1078. types[TUCHAR] = typ(TUCHAR, T);
  1079. types[TSHORT] = typ(TSHORT, T);
  1080. types[TUSHORT] = typ(TUSHORT, T);
  1081. types[TINT] = typ(TINT, T);
  1082. types[TUINT] = typ(TUINT, T);
  1083. types[TLONG] = typ(TLONG, T);
  1084. types[TULONG] = typ(TULONG, T);
  1085. types[TVLONG] = typ(TVLONG, T);
  1086. types[TUVLONG] = typ(TUVLONG, T);
  1087. types[TFLOAT] = typ(TFLOAT, T);
  1088. types[TDOUBLE] = typ(TDOUBLE, T);
  1089. types[TVOID] = typ(TVOID, T);
  1090. types[TENUM] = typ(TENUM, T);
  1091. types[TFUNC] = typ(TFUNC, types[TINT]);
  1092. types[TIND] = typ(TIND, types[TVOID]);
  1093. for(i=0; i<NHASH; i++)
  1094. hash[i] = S;
  1095. for(i=0; itab[i].name; i++) {
  1096. s = slookup(itab[i].name);
  1097. s->lexical = itab[i].lexical;
  1098. if(itab[i].type != 0)
  1099. s->type = types[itab[i].type];
  1100. }
  1101. blockno = 0;
  1102. autobn = 0;
  1103. autoffset = 0;
  1104. t = typ(TARRAY, types[TCHAR]);
  1105. t->width = 0;
  1106. symstring = slookup(".string");
  1107. symstring->class = CSTATIC;
  1108. symstring->type = t;
  1109. t = typ(TARRAY, types[TCHAR]);
  1110. t->width = 0;
  1111. nodproto = new(OPROTO, Z, Z);
  1112. dclstack = D;
  1113. pathname = allocn(pathname, 0, 100);
  1114. if(mygetwd(pathname, 99) == 0) {
  1115. pathname = allocn(pathname, 100, 900);
  1116. if(mygetwd(pathname, 999) == 0)
  1117. strcpy(pathname, "/???");
  1118. }
  1119. fmtinstall('O', Oconv);
  1120. fmtinstall('T', Tconv);
  1121. fmtinstall('F', FNconv);
  1122. fmtinstall('L', Lconv);
  1123. fmtinstall('Q', Qconv);
  1124. fmtinstall('|', VBconv);
  1125. }
  1126. int
  1127. filbuf(void)
  1128. {
  1129. Io *i;
  1130. loop:
  1131. i = iostack;
  1132. if(i == I)
  1133. return EOF;
  1134. if(i->f < 0)
  1135. goto pop;
  1136. fi.c = read(i->f, i->b, BUFSIZ) - 1;
  1137. if(fi.c < 0) {
  1138. close(i->f);
  1139. linehist(0, 0);
  1140. goto pop;
  1141. }
  1142. fi.p = i->b + 1;
  1143. return i->b[0] & 0xff;
  1144. pop:
  1145. iostack = i->link;
  1146. i->link = iofree;
  1147. iofree = i;
  1148. i = iostack;
  1149. if(i == I)
  1150. return EOF;
  1151. fi.p = i->p;
  1152. fi.c = i->c;
  1153. if(--fi.c < 0)
  1154. goto loop;
  1155. return *fi.p++ & 0xff;
  1156. }
  1157. int
  1158. Oconv(Fmt *fp)
  1159. {
  1160. int a;
  1161. a = va_arg(fp->args, int);
  1162. if(a < OXXX || a > OEND)
  1163. return fmtprint(fp, "***badO %d***", a);
  1164. return fmtstrcpy(fp, onames[a]);
  1165. }
  1166. int
  1167. Lconv(Fmt *fp)
  1168. {
  1169. char str[STRINGSZ], s[STRINGSZ];
  1170. Hist *h;
  1171. struct
  1172. {
  1173. Hist* incl; /* start of this include file */
  1174. long idel; /* delta line number to apply to include */
  1175. Hist* line; /* start of this #line directive */
  1176. long ldel; /* delta line number to apply to #line */
  1177. } a[HISTSZ];
  1178. long l, d;
  1179. int i, n;
  1180. l = va_arg(fp->args, long);
  1181. n = 0;
  1182. for(h = hist; h != H; h = h->link) {
  1183. if(l < h->line)
  1184. break;
  1185. if(h->name) {
  1186. if(h->offset != 0) { /* #line directive, not #pragma */
  1187. if(n > 0 && n < HISTSZ && h->offset >= 0) {
  1188. a[n-1].line = h;
  1189. a[n-1].ldel = h->line - h->offset + 1;
  1190. }
  1191. } else {
  1192. if(n < HISTSZ) { /* beginning of file */
  1193. a[n].incl = h;
  1194. a[n].idel = h->line;
  1195. a[n].line = 0;
  1196. }
  1197. n++;
  1198. }
  1199. continue;
  1200. }
  1201. n--;
  1202. if(n > 0 && n < HISTSZ) {
  1203. d = h->line - a[n].incl->line;
  1204. a[n-1].ldel += d;
  1205. a[n-1].idel += d;
  1206. }
  1207. }
  1208. if(n > HISTSZ)
  1209. n = HISTSZ;
  1210. str[0] = 0;
  1211. for(i=n-1; i>=0; i--) {
  1212. if(i != n-1) {
  1213. if(fp->flags & ~(FmtWidth|FmtPrec)) /* BUG ROB - was f3 */
  1214. break;
  1215. strcat(str, " ");
  1216. }
  1217. if(a[i].line)
  1218. snprint(s, STRINGSZ, "%s:%ld[%s:%ld]",
  1219. a[i].line->name, l-a[i].ldel+1,
  1220. a[i].incl->name, l-a[i].idel+1);
  1221. else
  1222. snprint(s, STRINGSZ, "%s:%ld",
  1223. a[i].incl->name, l-a[i].idel+1);
  1224. if(strlen(s)+strlen(str) >= STRINGSZ-10)
  1225. break;
  1226. strcat(str, s);
  1227. l = a[i].incl->line - 1; /* now print out start of this file */
  1228. }
  1229. if(n == 0)
  1230. strcat(str, "<eof>");
  1231. return fmtstrcpy(fp, str);
  1232. }
  1233. int
  1234. Tconv(Fmt *fp)
  1235. {
  1236. char str[STRINGSZ+20], s[STRINGSZ+20];
  1237. Type *t, *t1;
  1238. int et;
  1239. long n;
  1240. str[0] = 0;
  1241. for(t = va_arg(fp->args, Type*); t != T; t = t->link) {
  1242. et = t->etype;
  1243. if(str[0])
  1244. strcat(str, " ");
  1245. if(t->garb) {
  1246. sprint(s, "%s ", gnames[t->garb]);
  1247. if(strlen(str) + strlen(s) < STRINGSZ)
  1248. strcat(str, s);
  1249. }
  1250. sprint(s, "%s", tnames[et]);
  1251. if(strlen(str) + strlen(s) < STRINGSZ)
  1252. strcat(str, s);
  1253. if(et == TFUNC && (t1 = t->down)) {
  1254. sprint(s, "(%T", t1);
  1255. if(strlen(str) + strlen(s) < STRINGSZ)
  1256. strcat(str, s);
  1257. while(t1 = t1->down) {
  1258. sprint(s, ", %T", t1);
  1259. if(strlen(str) + strlen(s) < STRINGSZ)
  1260. strcat(str, s);
  1261. }
  1262. if(strlen(str) + strlen(s) < STRINGSZ)
  1263. strcat(str, ")");
  1264. }
  1265. if(et == TARRAY) {
  1266. n = t->width;
  1267. if(t->link && t->link->width)
  1268. n /= t->link->width;
  1269. sprint(s, "[%ld]", n);
  1270. if(strlen(str) + strlen(s) < STRINGSZ)
  1271. strcat(str, s);
  1272. }
  1273. if(t->nbits) {
  1274. sprint(s, " %d:%d", t->shift, t->nbits);
  1275. if(strlen(str) + strlen(s) < STRINGSZ)
  1276. strcat(str, s);
  1277. }
  1278. if(typesu[et]) {
  1279. if(t->tag) {
  1280. strcat(str, " ");
  1281. if(strlen(str) + strlen(t->tag->name) < STRINGSZ)
  1282. strcat(str, t->tag->name);
  1283. } else
  1284. strcat(str, " {}");
  1285. break;
  1286. }
  1287. }
  1288. return fmtstrcpy(fp, str);
  1289. }
  1290. int
  1291. FNconv(Fmt *fp)
  1292. {
  1293. char *str;
  1294. Node *n;
  1295. n = va_arg(fp->args, Node*);
  1296. str = "<indirect>";
  1297. if(n != Z && (n->op == ONAME || n->op == ODOT || n->op == OELEM))
  1298. str = n->sym->name;
  1299. return fmtstrcpy(fp, str);
  1300. }
  1301. int
  1302. Qconv(Fmt *fp)
  1303. {
  1304. char str[STRINGSZ+20], *s;
  1305. long b;
  1306. int i;
  1307. str[0] = 0;
  1308. for(b = va_arg(fp->args, long); b;) {
  1309. i = bitno(b);
  1310. if(str[0])
  1311. strcat(str, " ");
  1312. s = qnames[i];
  1313. if(strlen(str) + strlen(s) >= STRINGSZ)
  1314. break;
  1315. strcat(str, s);
  1316. b &= ~(1L << i);
  1317. }
  1318. return fmtstrcpy(fp, str);
  1319. }
  1320. int
  1321. VBconv(Fmt *fp)
  1322. {
  1323. char str[STRINGSZ];
  1324. int i, n, t, pc;
  1325. n = va_arg(fp->args, int);
  1326. pc = 0; /* BUG: was printcol */
  1327. i = 0;
  1328. while(pc < n) {
  1329. t = (pc+4) & ~3;
  1330. if(t <= n) {
  1331. str[i++] = '\t';
  1332. pc = t;
  1333. continue;
  1334. }
  1335. str[i++] = ' ';
  1336. pc++;
  1337. }
  1338. str[i] = 0;
  1339. return fmtstrcpy(fp, str);
  1340. }
  1341. /*
  1342. * real allocs
  1343. */
  1344. void*
  1345. alloc(long n)
  1346. {
  1347. void *p;
  1348. while((ulong)hunk & MAXALIGN) {
  1349. hunk++;
  1350. nhunk--;
  1351. }
  1352. while(nhunk < n)
  1353. gethunk();
  1354. p = hunk;
  1355. nhunk -= n;
  1356. hunk += n;
  1357. return p;
  1358. }
  1359. void*
  1360. allocn(void *p, long on, long n)
  1361. {
  1362. void *q;
  1363. q = (uchar*)p + on;
  1364. if(q != hunk || nhunk < n) {
  1365. while(nhunk < on+n)
  1366. gethunk();
  1367. memmove(hunk, p, on);
  1368. p = hunk;
  1369. hunk += on;
  1370. nhunk -= on;
  1371. }
  1372. hunk += n;
  1373. nhunk -= n;
  1374. return p;
  1375. }
  1376. void
  1377. setinclude(char *p)
  1378. {
  1379. int i;
  1380. char *e;
  1381. while(*p != 0) {
  1382. e = strchr(p, ' ');
  1383. if(e != 0)
  1384. *e = '\0';
  1385. for(i=1; i < ninclude; i++)
  1386. if(strcmp(p, include[i]) == 0)
  1387. break;
  1388. if(i >= ninclude)
  1389. include[ninclude++] = p;
  1390. if(ninclude > nelem(include)) {
  1391. diag(Z, "ninclude too small %d", nelem(include));
  1392. exits("ninclude");
  1393. }
  1394. if(e == 0)
  1395. break;
  1396. p = e+1;
  1397. }
  1398. }