lex.c 24 KB

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