lex.c 25 KB

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