lex.c 23 KB

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