lex.c 24 KB

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