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. * -t print type trees
  26. * -V enable void* conversion warnings
  27. * -v verbose printing
  28. * -w print warnings
  29. * -X abort on error
  30. */
  31. void
  32. main(int argc, char *argv[])
  33. {
  34. char *defs[50], *p;
  35. int nproc, nout, status, i, c, ndef;
  36. memset(debug, 0, sizeof(debug));
  37. tinit();
  38. cinit();
  39. ginit();
  40. arginit();
  41. profileflg = 1; /* #pragma can turn it off */
  42. tufield = simplet((1L<<tfield->etype) | BUNSIGNED);
  43. ndef = 0;
  44. outfile = 0;
  45. include[ninclude++] = ".";
  46. ARGBEGIN {
  47. default:
  48. c = ARGC();
  49. if(c >= 0 && c < sizeof(debug))
  50. debug[c]++;
  51. break;
  52. case 'l': /* for little-endian mips */
  53. if(thechar != 'v'){
  54. print("can only use -l with vc");
  55. errorexit();
  56. }
  57. thechar = '0';
  58. thestring = "spim";
  59. break;
  60. case 'o':
  61. outfile = ARGF();
  62. break;
  63. case 'D':
  64. p = ARGF();
  65. if(p) {
  66. defs[ndef++] = p;
  67. dodefine(p);
  68. }
  69. break;
  70. case 'I':
  71. p = ARGF();
  72. setinclude(p);
  73. break;
  74. } ARGEND
  75. if(argc < 1 && outfile == 0) {
  76. print("usage: %cc [-options] files\n", thechar);
  77. errorexit();
  78. }
  79. if(argc > 1 && systemtype(Windows)){
  80. print("can't compile multiple files on windows\n");
  81. errorexit();
  82. }
  83. if(argc > 1 && !systemtype(Windows)) {
  84. nproc = 1;
  85. if(p = getenv("NPROC"))
  86. nproc = atol(p); /* */
  87. c = 0;
  88. nout = 0;
  89. for(;;) {
  90. while(nout < nproc && argc > 0) {
  91. i = myfork();
  92. if(i < 0) {
  93. i = mywait(&status);
  94. if(i < 0) {
  95. print("cannot create a process\n");
  96. errorexit();
  97. }
  98. if(status)
  99. c++;
  100. nout--;
  101. continue;
  102. }
  103. if(i == 0) {
  104. fprint(2, "%s:\n", *argv);
  105. if (compile(*argv, defs, ndef))
  106. errorexit();
  107. exits(0);
  108. }
  109. nout++;
  110. argc--;
  111. argv++;
  112. }
  113. i = mywait(&status);
  114. if(i < 0) {
  115. if(c)
  116. errorexit();
  117. exits(0);
  118. }
  119. if(status)
  120. c++;
  121. nout--;
  122. }
  123. }
  124. if(argc == 0)
  125. c = compile("stdin", defs, ndef);
  126. else
  127. c = compile(argv[0], defs, ndef);
  128. if(c)
  129. errorexit();
  130. exits(0);
  131. }
  132. int
  133. compile(char *file, char **defs, int ndef)
  134. {
  135. char ofile[400], incfile[20];
  136. char *p, *av[100], opt[256];
  137. int i, c, fd[2];
  138. strcpy(ofile, file);
  139. p = utfrrune(ofile, pathchar());
  140. if(p) {
  141. *p++ = 0;
  142. if(!debug['.'])
  143. include[0] = strdup(ofile);
  144. } else
  145. p = ofile;
  146. if(outfile == 0) {
  147. outfile = p;
  148. if(outfile) {
  149. if(p = utfrrune(outfile, '.'))
  150. if(p[1] == 'c' && p[2] == 0)
  151. p[0] = 0;
  152. p = utfrune(outfile, 0);
  153. if(debug['a'] && debug['n'])
  154. strcat(p, ".acid");
  155. else if(debug['Z'] && debug['n'])
  156. strcat(p, "_pickle.c");
  157. else {
  158. p[0] = '.';
  159. p[1] = thechar;
  160. p[2] = 0;
  161. }
  162. } else
  163. outfile = "/dev/null";
  164. }
  165. if(p = getenv("INCLUDE")) {
  166. setinclude(p);
  167. } else {
  168. if(systemtype(Plan9)) {
  169. sprint(incfile, "/%s/include", thestring);
  170. setinclude(strdup(incfile));
  171. setinclude("/sys/include");
  172. }
  173. }
  174. if((debug['a'] || debug['Z']) && !debug['n']) {
  175. outfile = 0;
  176. Binit(&outbuf, dup(1, -1), OWRITE);
  177. dup(2, 1);
  178. } else {
  179. c = mycreat(outfile, 0664);
  180. if(c < 0) {
  181. diag(Z, "cannot open %s", outfile);
  182. outfile = 0;
  183. errorexit();
  184. }
  185. Binit(&outbuf, c, OWRITE);
  186. }
  187. Binit(&diagbuf, 1, OWRITE);
  188. newio();
  189. /* Use an ANSI preprocessor */
  190. if(debug['p']) {
  191. if(systemtype(Windows)) {
  192. diag(Z, "-p option not supported on windows");
  193. errorexit();
  194. }
  195. if(myaccess(file) < 0) {
  196. diag(Z, "%s does not exist", file);
  197. errorexit();
  198. }
  199. if(mypipe(fd) < 0) {
  200. diag(Z, "pipe failed");
  201. errorexit();
  202. }
  203. switch(myfork()) {
  204. case -1:
  205. diag(Z, "fork failed");
  206. errorexit();
  207. case 0:
  208. close(fd[0]);
  209. mydup(fd[1], 1);
  210. close(fd[1]);
  211. av[0] = CPP;
  212. i = 1;
  213. if(debug['+']) {
  214. sprint(opt, "-+");
  215. av[i++] = strdup(opt);
  216. }
  217. for(c = 0; c < ndef; c++) {
  218. sprint(opt, "-D%s", defs[c]);
  219. av[i++] = strdup(opt);
  220. }
  221. for(c = 0; c < ninclude; c++) {
  222. sprint(opt, "-I%s", include[c]);
  223. av[i++] = strdup(opt);
  224. }
  225. if(strcmp(file, "stdin") != 0)
  226. av[i++] = file;
  227. av[i] = 0;
  228. if(debug['p'] > 1) {
  229. for(c = 0; c < i; c++)
  230. fprint(2, "%s ", av[c]);
  231. fprint(2, "\n");
  232. }
  233. myexec(av[0], av);
  234. fprint(2, "can't exec C preprocessor %s: %r\n", CPP);
  235. errorexit();
  236. default:
  237. close(fd[1]);
  238. newfile(file, fd[0]);
  239. break;
  240. }
  241. } else {
  242. if(strcmp(file, "stdin") == 0)
  243. newfile(file, 0);
  244. else
  245. newfile(file, -1);
  246. }
  247. yyparse();
  248. if(!debug['a'] && !debug['Z'])
  249. gclean();
  250. return nerrors;
  251. }
  252. void
  253. errorexit(void)
  254. {
  255. if(outfile)
  256. remove(outfile);
  257. exits("error");
  258. }
  259. void
  260. pushio(void)
  261. {
  262. Io *i;
  263. i = iostack;
  264. if(i == I) {
  265. yyerror("botch in pushio");
  266. errorexit();
  267. }
  268. i->p = fi.p;
  269. i->c = fi.c;
  270. }
  271. void
  272. newio(void)
  273. {
  274. Io *i;
  275. static int pushdepth = 0;
  276. i = iofree;
  277. if(i == I) {
  278. pushdepth++;
  279. if(pushdepth > 1000) {
  280. yyerror("macro/io expansion too deep");
  281. errorexit();
  282. }
  283. i = alloc(sizeof(*i));
  284. } else
  285. iofree = i->link;
  286. i->c = 0;
  287. i->f = -1;
  288. ionext = i;
  289. }
  290. void
  291. newfile(char *s, int f)
  292. {
  293. Io *i;
  294. if(debug['e'])
  295. print("%L: %s\n", lineno, s);
  296. i = ionext;
  297. i->link = iostack;
  298. iostack = i;
  299. i->f = f;
  300. if(f < 0)
  301. i->f = open(s, 0);
  302. if(i->f < 0) {
  303. yyerror("%cc: %r: %s", thechar, s);
  304. errorexit();
  305. }
  306. fi.c = 0;
  307. linehist(s, 0);
  308. }
  309. Sym*
  310. slookup(char *s)
  311. {
  312. strcpy(symb, s);
  313. return lookup();
  314. }
  315. Sym*
  316. lookup(void)
  317. {
  318. Sym *s;
  319. ulong h;
  320. char *p;
  321. int c, n;
  322. h = 0;
  323. for(p=symb; *p;) {
  324. h = h * 3;
  325. h += *p++;
  326. }
  327. n = (p - symb) + 1;
  328. if((long)h < 0)
  329. h = ~h;
  330. h %= NHASH;
  331. c = symb[0];
  332. for(s = hash[h]; s != S; s = s->link) {
  333. if(s->name[0] != c)
  334. continue;
  335. if(strcmp(s->name, symb) == 0)
  336. return s;
  337. }
  338. s = alloc(sizeof(*s));
  339. s->name = alloc(n);
  340. memmove(s->name, symb, n);
  341. strcpy(s->name, symb);
  342. s->link = hash[h];
  343. hash[h] = s;
  344. syminit(s);
  345. return s;
  346. }
  347. void
  348. syminit(Sym *s)
  349. {
  350. s->lexical = LNAME;
  351. s->block = 0;
  352. s->offset = 0;
  353. s->type = T;
  354. s->suetag = T;
  355. s->class = CXXX;
  356. s->aused = 0;
  357. s->sig = SIGNONE;
  358. }
  359. #define EOF (-1)
  360. #define IGN (-2)
  361. #define ESC (1<<20)
  362. #define GETC() ((--fi.c < 0)? filbuf(): (*fi.p++ & 0xff))
  363. enum
  364. {
  365. Numdec = 1<<0,
  366. Numlong = 1<<1,
  367. Numuns = 1<<2,
  368. Numvlong = 1<<3,
  369. Numflt = 1<<4,
  370. };
  371. long
  372. yylex(void)
  373. {
  374. vlong vv;
  375. long c, c1, t;
  376. char *cp;
  377. Rune rune;
  378. Sym *s;
  379. if(peekc != IGN) {
  380. c = peekc;
  381. peekc = IGN;
  382. goto l1;
  383. }
  384. l0:
  385. c = GETC();
  386. l1:
  387. if(c >= Runeself) {
  388. /*
  389. * extension --
  390. * all multibyte runes are alpha
  391. */
  392. cp = symb;
  393. goto talph;
  394. }
  395. if(isspace(c)) {
  396. if(c == '\n')
  397. lineno++;
  398. goto l0;
  399. }
  400. if(isalpha(c)) {
  401. cp = symb;
  402. if(c != 'L')
  403. goto talph;
  404. *cp++ = c;
  405. c = GETC();
  406. if(c == '\'') {
  407. /* L'x' */
  408. c = escchar('\'', 1, 0);
  409. if(c == EOF)
  410. c = '\'';
  411. c1 = escchar('\'', 1, 0);
  412. if(c1 != EOF) {
  413. yyerror("missing '");
  414. peekc = c1;
  415. }
  416. yylval.vval = convvtox(c, TUSHORT);
  417. return LUCONST;
  418. }
  419. if(c == '"') {
  420. goto caselq;
  421. }
  422. goto talph;
  423. }
  424. if(isdigit(c))
  425. goto tnum;
  426. switch(c)
  427. {
  428. case EOF:
  429. peekc = EOF;
  430. return -1;
  431. case '_':
  432. cp = symb;
  433. goto talph;
  434. case '#':
  435. domacro();
  436. goto l0;
  437. case '.':
  438. c1 = GETC();
  439. if(isdigit(c1)) {
  440. cp = symb;
  441. *cp++ = c;
  442. c = c1;
  443. c1 = 0;
  444. goto casedot;
  445. }
  446. break;
  447. case '"':
  448. strcpy(symb, "\"<string>\"");
  449. cp = alloc(0);
  450. c1 = 0;
  451. /* "..." */
  452. for(;;) {
  453. c = escchar('"', 0, 1);
  454. if(c == EOF)
  455. break;
  456. if(c & ESC) {
  457. cp = allocn(cp, c1, 1);
  458. cp[c1++] = c;
  459. } else {
  460. rune = c;
  461. c = runelen(rune);
  462. cp = allocn(cp, c1, c);
  463. runetochar(cp+c1, &rune);
  464. c1 += c;
  465. }
  466. }
  467. yylval.sval.l = c1;
  468. do {
  469. cp = allocn(cp, c1, 1);
  470. cp[c1++] = 0;
  471. } while(c1 & MAXALIGN);
  472. yylval.sval.s = cp;
  473. return LSTRING;
  474. caselq:
  475. /* L"..." */
  476. strcpy(symb, "\"L<string>\"");
  477. cp = alloc(0);
  478. c1 = 0;
  479. for(;;) {
  480. c = escchar('"', 1, 0);
  481. if(c == EOF)
  482. break;
  483. cp = allocn(cp, c1, sizeof(ushort));
  484. *(ushort*)(cp + c1) = c;
  485. c1 += sizeof(ushort);
  486. }
  487. yylval.sval.l = c1;
  488. do {
  489. cp = allocn(cp, c1, sizeof(ushort));
  490. *(ushort*)(cp + c1) = 0;
  491. c1 += sizeof(ushort);
  492. } while(c1 & MAXALIGN);
  493. yylval.sval.s = cp;
  494. return LLSTRING;
  495. case '\'':
  496. /* '.' */
  497. c = escchar('\'', 0, 0);
  498. if(c == EOF)
  499. c = '\'';
  500. c1 = escchar('\'', 0, 0);
  501. if(c1 != EOF) {
  502. yyerror("missing '");
  503. peekc = c1;
  504. }
  505. vv = c;
  506. yylval.vval = convvtox(vv, TUCHAR);
  507. if(yylval.vval != vv)
  508. yyerror("overflow in character constant: 0x%lx", c);
  509. else
  510. if(c & 0x80){
  511. nearln = lineno;
  512. warn(Z, "sign-extended character constant");
  513. }
  514. yylval.vval = convvtox(vv, TCHAR);
  515. return LCONST;
  516. case '/':
  517. c1 = GETC();
  518. if(c1 == '*') {
  519. for(;;) {
  520. c = getr();
  521. while(c == '*') {
  522. c = getr();
  523. if(c == '/')
  524. goto l0;
  525. }
  526. if(c == EOF) {
  527. yyerror("eof in comment");
  528. errorexit();
  529. }
  530. }
  531. }
  532. if(c1 == '/') {
  533. for(;;) {
  534. c = getr();
  535. if(c == '\n')
  536. goto l0;
  537. if(c == EOF) {
  538. yyerror("eof in comment");
  539. errorexit();
  540. }
  541. }
  542. }
  543. if(c1 == '=')
  544. return LDVE;
  545. break;
  546. case '*':
  547. c1 = GETC();
  548. if(c1 == '=')
  549. return LMLE;
  550. break;
  551. case '%':
  552. c1 = GETC();
  553. if(c1 == '=')
  554. return LMDE;
  555. break;
  556. case '+':
  557. c1 = GETC();
  558. if(c1 == '+')
  559. return LPP;
  560. if(c1 == '=')
  561. return LPE;
  562. break;
  563. case '-':
  564. c1 = GETC();
  565. if(c1 == '-')
  566. return LMM;
  567. if(c1 == '=')
  568. return LME;
  569. if(c1 == '>')
  570. return LMG;
  571. break;
  572. case '>':
  573. c1 = GETC();
  574. if(c1 == '>') {
  575. c = LRSH;
  576. c1 = GETC();
  577. if(c1 == '=')
  578. return LRSHE;
  579. break;
  580. }
  581. if(c1 == '=')
  582. return LGE;
  583. break;
  584. case '<':
  585. c1 = GETC();
  586. if(c1 == '<') {
  587. c = LLSH;
  588. c1 = GETC();
  589. if(c1 == '=')
  590. return LLSHE;
  591. break;
  592. }
  593. if(c1 == '=')
  594. return LLE;
  595. break;
  596. case '=':
  597. c1 = GETC();
  598. if(c1 == '=')
  599. return LEQ;
  600. break;
  601. case '!':
  602. c1 = GETC();
  603. if(c1 == '=')
  604. return LNE;
  605. break;
  606. case '&':
  607. c1 = GETC();
  608. if(c1 == '&')
  609. return LANDAND;
  610. if(c1 == '=')
  611. return LANDE;
  612. break;
  613. case '|':
  614. c1 = GETC();
  615. if(c1 == '|')
  616. return LOROR;
  617. if(c1 == '=')
  618. return LORE;
  619. break;
  620. case '^':
  621. c1 = GETC();
  622. if(c1 == '=')
  623. return LXORE;
  624. break;
  625. default:
  626. return c;
  627. }
  628. peekc = c1;
  629. return c;
  630. talph:
  631. /*
  632. * cp is set to symb and some
  633. * prefix has been stored
  634. */
  635. for(;;) {
  636. if(c >= Runeself) {
  637. for(c1=0;;) {
  638. cp[c1++] = c;
  639. if(fullrune(cp, c1))
  640. break;
  641. c = GETC();
  642. }
  643. cp += c1;
  644. c = GETC();
  645. continue;
  646. }
  647. if(!isalnum(c) && c != '_')
  648. break;
  649. *cp++ = c;
  650. c = GETC();
  651. }
  652. *cp = 0;
  653. if(debug['L'])
  654. print("%L: %s\n", lineno, symb);
  655. peekc = c;
  656. s = lookup();
  657. if(s->macro) {
  658. newio();
  659. cp = ionext->b;
  660. macexpand(s, cp);
  661. pushio();
  662. ionext->link = iostack;
  663. iostack = ionext;
  664. fi.p = cp;
  665. fi.c = strlen(cp);
  666. if(peekc != IGN) {
  667. cp[fi.c++] = peekc;
  668. cp[fi.c] = 0;
  669. peekc = IGN;
  670. }
  671. goto l0;
  672. }
  673. yylval.sym = s;
  674. if(s->class == CTYPEDEF || s->class == CTYPESTR)
  675. return LTYPE;
  676. return s->lexical;
  677. tnum:
  678. c1 = 0;
  679. cp = symb;
  680. if(c != '0') {
  681. c1 |= Numdec;
  682. for(;;) {
  683. *cp++ = c;
  684. c = GETC();
  685. if(isdigit(c))
  686. continue;
  687. goto dc;
  688. }
  689. }
  690. *cp++ = c;
  691. c = GETC();
  692. if(c == 'x' || c == 'X')
  693. for(;;) {
  694. *cp++ = c;
  695. c = GETC();
  696. if(isdigit(c))
  697. continue;
  698. if(c >= 'a' && c <= 'f')
  699. continue;
  700. if(c >= 'A' && c <= 'F')
  701. continue;
  702. if(cp == symb+2)
  703. yyerror("malformed hex constant");
  704. goto ncu;
  705. }
  706. if(c < '0' || c > '7')
  707. goto dc;
  708. for(;;) {
  709. if(c >= '0' && c <= '7') {
  710. *cp++ = c;
  711. c = GETC();
  712. continue;
  713. }
  714. goto ncu;
  715. }
  716. dc:
  717. if(c == '.')
  718. goto casedot;
  719. if(c == 'e' || c == 'E')
  720. goto casee;
  721. ncu:
  722. if((c == 'U' || c == 'u') && !(c1 & Numuns)) {
  723. c = GETC();
  724. c1 |= Numuns;
  725. goto ncu;
  726. }
  727. if((c == 'L' || c == 'l') && !(c1 & Numvlong)) {
  728. c = GETC();
  729. if(c1 & Numlong)
  730. c1 |= Numvlong;
  731. c1 |= Numlong;
  732. goto ncu;
  733. }
  734. *cp = 0;
  735. peekc = c;
  736. if(mpatov(symb, &yylval.vval))
  737. yyerror("overflow in constant");
  738. vv = yylval.vval;
  739. if(c1 & Numvlong) {
  740. if((c1 & Numuns) || convvtox(vv, TVLONG) < 0) {
  741. c = LUVLCONST;
  742. t = TUVLONG;
  743. goto nret;
  744. }
  745. c = LVLCONST;
  746. t = TVLONG;
  747. goto nret;
  748. }
  749. if(c1 & Numlong) {
  750. if((c1 & Numuns) || convvtox(vv, TLONG) < 0) {
  751. c = LULCONST;
  752. t = TULONG;
  753. goto nret;
  754. }
  755. c = LLCONST;
  756. t = TLONG;
  757. goto nret;
  758. }
  759. if((c1 & Numuns) || convvtox(vv, TINT) < 0) {
  760. c = LUCONST;
  761. t = TUINT;
  762. goto nret;
  763. }
  764. c = LCONST;
  765. t = TINT;
  766. goto nret;
  767. nret:
  768. yylval.vval = convvtox(vv, t);
  769. if(yylval.vval != vv){
  770. nearln = lineno;
  771. warn(Z, "truncated constant: %T %s", types[t], symb);
  772. }
  773. return c;
  774. casedot:
  775. for(;;) {
  776. *cp++ = c;
  777. c = GETC();
  778. if(!isdigit(c))
  779. break;
  780. }
  781. if(c != 'e' && c != 'E')
  782. goto caseout;
  783. casee:
  784. *cp++ = 'e';
  785. c = GETC();
  786. if(c == '+' || c == '-') {
  787. *cp++ = c;
  788. c = GETC();
  789. }
  790. if(!isdigit(c))
  791. yyerror("malformed fp constant exponent");
  792. while(isdigit(c)) {
  793. *cp++ = c;
  794. c = GETC();
  795. }
  796. caseout:
  797. if(c == 'L' || c == 'l') {
  798. c = GETC();
  799. c1 |= Numlong;
  800. } else
  801. if(c == 'F' || c == 'f') {
  802. c = GETC();
  803. c1 |= Numflt;
  804. }
  805. *cp = 0;
  806. peekc = c;
  807. yylval.dval = strtod(symb, nil);
  808. if(isInf(yylval.dval, 1) || isInf(yylval.dval, -1)) {
  809. yyerror("overflow in float constant");
  810. yylval.dval = 0;
  811. }
  812. if(c1 & Numflt)
  813. return LFCONST;
  814. return LDCONST;
  815. }
  816. /*
  817. * convert a string, s, to vlong in *v
  818. * return conversion overflow.
  819. * required syntax is [0[x]]d*
  820. */
  821. int
  822. mpatov(char *s, vlong *v)
  823. {
  824. vlong n, nn;
  825. int c;
  826. n = 0;
  827. c = *s;
  828. if(c == '0')
  829. goto oct;
  830. while(c = *s++) {
  831. if(c >= '0' && c <= '9')
  832. nn = n*10 + c-'0';
  833. else
  834. goto bad;
  835. if(n < 0 && nn >= 0)
  836. goto bad;
  837. n = nn;
  838. }
  839. goto out;
  840. oct:
  841. s++;
  842. c = *s;
  843. if(c == 'x' || c == 'X')
  844. goto hex;
  845. while(c = *s++) {
  846. if(c >= '0' || c <= '7')
  847. nn = n*8 + c-'0';
  848. else
  849. goto bad;
  850. if(n < 0 && nn >= 0)
  851. goto bad;
  852. n = nn;
  853. }
  854. goto out;
  855. hex:
  856. s++;
  857. while(c = *s++) {
  858. if(c >= '0' && c <= '9')
  859. c += 0-'0';
  860. else
  861. if(c >= 'a' && c <= 'f')
  862. c += 10-'a';
  863. else
  864. if(c >= 'A' && c <= 'F')
  865. c += 10-'A';
  866. else
  867. goto bad;
  868. nn = n*16 + c;
  869. if(n < 0 && nn >= 0)
  870. goto bad;
  871. n = nn;
  872. }
  873. out:
  874. *v = n;
  875. return 0;
  876. bad:
  877. *v = ~0;
  878. return 1;
  879. }
  880. int
  881. getc(void)
  882. {
  883. int c;
  884. if(peekc != IGN) {
  885. c = peekc;
  886. peekc = IGN;
  887. } else
  888. c = GETC();
  889. if(c == '\n')
  890. lineno++;
  891. if(c == EOF) {
  892. yyerror("End of file");
  893. errorexit();
  894. }
  895. return c;
  896. }
  897. long
  898. getr(void)
  899. {
  900. int c, i;
  901. char str[UTFmax+1];
  902. Rune rune;
  903. c = getc();
  904. if(c < Runeself)
  905. return c;
  906. i = 0;
  907. str[i++] = c;
  908. loop:
  909. c = getc();
  910. str[i++] = c;
  911. if(!fullrune(str, i))
  912. goto loop;
  913. c = chartorune(&rune, str);
  914. if(rune == Runeerror && c == 1) {
  915. nearln = lineno;
  916. diag(Z, "illegal rune in string");
  917. for(c=0; c<i; c++)
  918. print(" %.2x", *(uchar*)(str+c));
  919. print("\n");
  920. }
  921. return rune;
  922. }
  923. int
  924. getnsc(void)
  925. {
  926. int c;
  927. if(peekc != IGN) {
  928. c = peekc;
  929. peekc = IGN;
  930. } else
  931. c = GETC();
  932. for(;;) {
  933. if(!isspace(c))
  934. return c;
  935. if(c == '\n') {
  936. lineno++;
  937. return c;
  938. }
  939. c = GETC();
  940. }
  941. return 0;
  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. }