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. }
  179. p = utfrune(outfile, 0);
  180. if(debug['a'] && debug['n']){
  181. strcat(p, ".acid");
  182. } else if (debug['Z'] && debug['n']){
  183. strcat(p, "_pickle.c");
  184. } else {
  185. p[0] = '.';
  186. p[1] = thechar;
  187. p[2] = 0;
  188. }
  189. } else
  190. outfile = "/dev/null";
  191. }
  192. if((p = getenv("INCLUDE"))) {
  193. setinclude(p);
  194. } else {
  195. if(systemtype(Plan9)) {
  196. sprint(incfile, "/%s/include", thestring);
  197. setinclude(strdup(incfile));
  198. setinclude("/sys/include");
  199. }
  200. }
  201. if (first)
  202. Binit(&diagbuf, 1, OWRITE);
  203. /*
  204. * if we're writing acid to standard output, don't keep scratching
  205. * outbuf.
  206. */
  207. if((debug['a'] || debug['Z']) && !debug['n']) {
  208. if (first) {
  209. outfile = 0;
  210. Binit(&outbuf, dup(1, -1), OWRITE);
  211. dup(2, 1);
  212. }
  213. } else {
  214. c = mycreat(outfile, 0664);
  215. if(c < 0) {
  216. diag(Z, "cannot open %s - %r", outfile);
  217. outfile = 0;
  218. errorexit();
  219. }
  220. Binit(&outbuf, c, OWRITE);
  221. }
  222. newio();
  223. first = 0;
  224. /* Use an ANSI preprocessor */
  225. if(debug['p']) {
  226. if(systemtype(Windows)) {
  227. diag(Z, "-p option not supported on windows");
  228. errorexit();
  229. }
  230. if(myaccess(file) < 0) {
  231. diag(Z, "%s does not exist", file);
  232. errorexit();
  233. }
  234. if(mypipe(fd) < 0) {
  235. diag(Z, "pipe failed");
  236. errorexit();
  237. }
  238. switch(myfork()) {
  239. case -1:
  240. diag(Z, "fork failed");
  241. errorexit();
  242. case 0:
  243. close(fd[0]);
  244. mydup(fd[1], 1);
  245. close(fd[1]);
  246. av = alloc((3 + ndef + ninclude + 2) * sizeof *av);
  247. av[0] = CPP;
  248. i = 1;
  249. if(debug['.'])
  250. av[i++] = strdup("-.");
  251. /* 1999 ANSI C requires recognising // comments */
  252. av[i++] = strdup("-+");
  253. for(c = 0; c < ndef; c++) {
  254. sprint(opt, "-D%s", defs[c]);
  255. av[i++] = strdup(opt);
  256. }
  257. for(c = 0; c < ninclude; c++) {
  258. sprint(opt, "-I%s", include[c]);
  259. av[i++] = strdup(opt);
  260. }
  261. if(strcmp(file, "stdin") != 0)
  262. av[i++] = file;
  263. av[i] = 0;
  264. if(debug['p'] > 1) {
  265. for(c = 0; c < i; c++)
  266. fprint(2, "%s ", av[c]);
  267. fprint(2, "\n");
  268. }
  269. myexec(av[0], av);
  270. fprint(2, "can't exec C preprocessor %s: %r\n", CPP);
  271. errorexit();
  272. default:
  273. close(fd[1]);
  274. newfile(file, fd[0]);
  275. break;
  276. }
  277. } else {
  278. if(strcmp(file, "stdin") == 0)
  279. newfile(file, 0);
  280. else
  281. newfile(file, -1);
  282. }
  283. yyparse();
  284. if(!debug['a'] && !debug['Z'])
  285. gclean();
  286. return nerrors;
  287. }
  288. void
  289. errorexit(void)
  290. {
  291. if(outfile)
  292. remove(outfile);
  293. exits("error");
  294. }
  295. void
  296. pushio(void)
  297. {
  298. Io *i;
  299. i = iostack;
  300. if(i == I) {
  301. yyerror("botch in pushio");
  302. errorexit();
  303. }
  304. i->p = fi.p;
  305. i->c = fi.c;
  306. }
  307. void
  308. newio(void)
  309. {
  310. Io *i;
  311. static int pushdepth = 0;
  312. i = iofree;
  313. if(i == I) {
  314. pushdepth++;
  315. if(pushdepth > 1000) {
  316. yyerror("macro/io expansion too deep");
  317. errorexit();
  318. }
  319. i = alloc(sizeof(*i));
  320. } else
  321. iofree = i->link;
  322. i->c = 0;
  323. i->f = -1;
  324. ionext = i;
  325. }
  326. void
  327. newfile(char *s, int f)
  328. {
  329. Io *i;
  330. if(debug['e'])
  331. print("%L: %s\n", lineno, s);
  332. i = ionext;
  333. i->link = iostack;
  334. iostack = i;
  335. i->f = f;
  336. if(f < 0)
  337. i->f = open(s, 0);
  338. if(i->f < 0) {
  339. yyerror("%cc: %r: %s", thechar, s);
  340. errorexit();
  341. }
  342. fi.c = 0;
  343. linehist(s, 0);
  344. }
  345. Sym*
  346. slookup(char *s)
  347. {
  348. strcpy(symb, s);
  349. return lookup();
  350. }
  351. Sym*
  352. lookup(void)
  353. {
  354. Sym *s;
  355. ulong h;
  356. char *p;
  357. int c, n;
  358. h = 0;
  359. for(p=symb; *p;) {
  360. h = h * 3;
  361. h += *p++;
  362. }
  363. n = (p - symb) + 1;
  364. if((long)h < 0)
  365. h = ~h;
  366. h %= NHASH;
  367. c = symb[0];
  368. for(s = hash[h]; s != S; s = s->link) {
  369. if(s->name[0] != c)
  370. continue;
  371. if(strcmp(s->name, symb) == 0)
  372. return s;
  373. }
  374. s = alloc(sizeof(*s));
  375. s->name = alloc(n);
  376. memmove(s->name, symb, n);
  377. strcpy(s->name, symb);
  378. s->link = hash[h];
  379. hash[h] = s;
  380. syminit(s);
  381. return s;
  382. }
  383. void
  384. syminit(Sym *s)
  385. {
  386. s->lexical = LNAME;
  387. s->block = 0;
  388. s->offset = 0;
  389. s->type = T;
  390. s->suetag = T;
  391. s->class = CXXX;
  392. s->aused = 0;
  393. s->sig = SIGNONE;
  394. }
  395. #define EOF (-1)
  396. #define IGN (-2)
  397. #define ESC (Runemask+1) /* Rune flag: a literal byte */
  398. #define GETC() ((--fi.c < 0)? filbuf(): (*fi.p++ & 0xff))
  399. enum
  400. {
  401. Numdec = 1<<0,
  402. Numlong = 1<<1,
  403. Numuns = 1<<2,
  404. Numvlong = 1<<3,
  405. Numflt = 1<<4,
  406. };
  407. long
  408. yylex(void)
  409. {
  410. vlong vv;
  411. long c, c1, t;
  412. char *cp;
  413. Rune rune;
  414. Sym *s;
  415. if(peekc != IGN) {
  416. c = peekc;
  417. peekc = IGN;
  418. goto l1;
  419. }
  420. l0:
  421. c = GETC();
  422. l1:
  423. if(c >= Runeself) {
  424. /*
  425. * extension --
  426. * all multibyte runes are alpha
  427. */
  428. cp = symb;
  429. goto talph;
  430. }
  431. if(isspace(c)) {
  432. if(c == '\n')
  433. lineno++;
  434. goto l0;
  435. }
  436. if(isalpha(c)) {
  437. cp = symb;
  438. if(c != 'L')
  439. goto talph;
  440. *cp++ = c;
  441. c = GETC();
  442. if(c == '\'') {
  443. /* L'x' */
  444. c = escchar('\'', 1, 0);
  445. if(c == EOF)
  446. c = '\'';
  447. c1 = escchar('\'', 1, 0);
  448. if(c1 != EOF) {
  449. yyerror("missing '");
  450. peekc = c1;
  451. }
  452. yylval.vval = convvtox(c, TRUNE);
  453. return LUCONST;
  454. }
  455. if(c == '"') {
  456. goto caselq;
  457. }
  458. goto talph;
  459. }
  460. if(isdigit(c))
  461. goto tnum;
  462. switch(c)
  463. {
  464. case EOF:
  465. peekc = EOF;
  466. return -1;
  467. case '_':
  468. cp = symb;
  469. goto talph;
  470. case '#':
  471. domacro();
  472. goto l0;
  473. case '.':
  474. c1 = GETC();
  475. if(isdigit(c1)) {
  476. cp = symb;
  477. *cp++ = c;
  478. c = c1;
  479. c1 = 0;
  480. goto casedot;
  481. }
  482. break;
  483. case '"':
  484. strcpy(symb, "\"<string>\"");
  485. cp = alloc(0);
  486. c1 = 0;
  487. /* "..." */
  488. for(;;) {
  489. c = escchar('"', 0, 1);
  490. if(c == EOF)
  491. break;
  492. if(c & ESC) {
  493. cp = allocn(cp, c1, 1);
  494. cp[c1++] = c;
  495. } else {
  496. rune = c;
  497. c = runelen(rune);
  498. cp = allocn(cp, c1, c);
  499. runetochar(cp+c1, &rune);
  500. c1 += c;
  501. }
  502. }
  503. yylval.sval.l = c1;
  504. do {
  505. cp = allocn(cp, c1, 1);
  506. cp[c1++] = 0;
  507. } while(c1 & MAXALIGN);
  508. yylval.sval.s = cp;
  509. return LSTRING;
  510. caselq:
  511. /* L"..." */
  512. strcpy(symb, "\"L<string>\"");
  513. cp = alloc(0);
  514. c1 = 0;
  515. for(;;) {
  516. c = escchar('"', 1, 0);
  517. if(c == EOF)
  518. break;
  519. cp = allocn(cp, c1, sizeof(TRune));
  520. *(TRune*)(cp + c1) = c;
  521. c1 += sizeof(TRune);
  522. }
  523. yylval.sval.l = c1;
  524. do {
  525. cp = allocn(cp, c1, sizeof(TRune));
  526. *(TRune*)(cp + c1) = 0;
  527. c1 += sizeof(TRune);
  528. } while(c1 & MAXALIGN);
  529. yylval.sval.s = cp;
  530. return LLSTRING;
  531. case '\'':
  532. /* '.' */
  533. c = escchar('\'', 0, 0);
  534. if(c == EOF)
  535. c = '\'';
  536. c1 = escchar('\'', 0, 0);
  537. if(c1 != EOF) {
  538. yyerror("missing '");
  539. peekc = c1;
  540. }
  541. vv = c;
  542. yylval.vval = convvtox(vv, TUCHAR);
  543. if(yylval.vval != vv)
  544. yyerror("overflow in character constant: 0x%lx", c);
  545. else
  546. if(c & 0x80){
  547. nearln = lineno;
  548. warn(Z, "sign-extended character constant");
  549. }
  550. yylval.vval = convvtox(vv, TCHAR);
  551. return LCONST;
  552. case '/':
  553. c1 = GETC();
  554. if(c1 == '*') {
  555. for(;;) {
  556. c = getr();
  557. while(c == '*') {
  558. c = getr();
  559. if(c == '/')
  560. goto l0;
  561. }
  562. if(c == EOF) {
  563. yyerror("eof in comment");
  564. errorexit();
  565. }
  566. }
  567. }
  568. if(c1 == '/') {
  569. for(;;) {
  570. c = getr();
  571. if(c == '\n')
  572. goto l0;
  573. if(c == EOF) {
  574. yyerror("eof in comment");
  575. errorexit();
  576. }
  577. }
  578. }
  579. if(c1 == '=')
  580. return LDVE;
  581. break;
  582. case '*':
  583. c1 = GETC();
  584. if(c1 == '=')
  585. return LMLE;
  586. break;
  587. case '%':
  588. c1 = GETC();
  589. if(c1 == '=')
  590. return LMDE;
  591. break;
  592. case '+':
  593. c1 = GETC();
  594. if(c1 == '+')
  595. return LPP;
  596. if(c1 == '=')
  597. return LPE;
  598. break;
  599. case '-':
  600. c1 = GETC();
  601. if(c1 == '-')
  602. return LMM;
  603. if(c1 == '=')
  604. return LME;
  605. if(c1 == '>')
  606. return LMG;
  607. break;
  608. case '>':
  609. c1 = GETC();
  610. if(c1 == '>') {
  611. c = LRSH;
  612. c1 = GETC();
  613. if(c1 == '=')
  614. return LRSHE;
  615. break;
  616. }
  617. if(c1 == '=')
  618. return LGE;
  619. break;
  620. case '<':
  621. c1 = GETC();
  622. if(c1 == '<') {
  623. c = LLSH;
  624. c1 = GETC();
  625. if(c1 == '=')
  626. return LLSHE;
  627. break;
  628. }
  629. if(c1 == '=')
  630. return LLE;
  631. break;
  632. case '=':
  633. c1 = GETC();
  634. if(c1 == '=')
  635. return LEQ;
  636. break;
  637. case '!':
  638. c1 = GETC();
  639. if(c1 == '=')
  640. return LNE;
  641. break;
  642. case '&':
  643. c1 = GETC();
  644. if(c1 == '&')
  645. return LANDAND;
  646. if(c1 == '=')
  647. return LANDE;
  648. break;
  649. case '|':
  650. c1 = GETC();
  651. if(c1 == '|')
  652. return LOROR;
  653. if(c1 == '=')
  654. return LORE;
  655. break;
  656. case '^':
  657. c1 = GETC();
  658. if(c1 == '=')
  659. return LXORE;
  660. break;
  661. default:
  662. return c;
  663. }
  664. peekc = c1;
  665. return c;
  666. talph:
  667. /*
  668. * cp is set to symb and some
  669. * prefix has been stored
  670. */
  671. for(;;) {
  672. if(c >= Runeself) {
  673. for(c1=0;;) {
  674. cp[c1++] = c;
  675. if(fullrune(cp, c1))
  676. break;
  677. c = GETC();
  678. }
  679. cp += c1;
  680. c = GETC();
  681. continue;
  682. }
  683. if(!isalnum(c) && c != '_')
  684. break;
  685. *cp++ = c;
  686. c = GETC();
  687. }
  688. *cp = 0;
  689. if(debug['L'])
  690. print("%L: %s\n", lineno, symb);
  691. peekc = c;
  692. s = lookup();
  693. if(s->macro) {
  694. newio();
  695. cp = ionext->b;
  696. macexpand(s, cp);
  697. pushio();
  698. ionext->link = iostack;
  699. iostack = ionext;
  700. fi.p = cp;
  701. fi.c = strlen(cp);
  702. if(peekc != IGN) {
  703. cp[fi.c++] = peekc;
  704. cp[fi.c] = 0;
  705. peekc = IGN;
  706. }
  707. goto l0;
  708. }
  709. yylval.sym = s;
  710. if(s->class == CTYPEDEF || s->class == CTYPESTR)
  711. return LTYPE;
  712. return s->lexical;
  713. tnum:
  714. c1 = 0;
  715. cp = symb;
  716. if(c != '0') {
  717. c1 |= Numdec;
  718. for(;;) {
  719. *cp++ = c;
  720. c = GETC();
  721. if(isdigit(c))
  722. continue;
  723. goto dc;
  724. }
  725. }
  726. *cp++ = c;
  727. c = GETC();
  728. if(c == 'x' || c == 'X')
  729. for(;;) {
  730. *cp++ = c;
  731. c = GETC();
  732. if(isdigit(c))
  733. continue;
  734. if(c >= 'a' && c <= 'f')
  735. continue;
  736. if(c >= 'A' && c <= 'F')
  737. continue;
  738. if(cp == symb+2)
  739. yyerror("malformed hex constant");
  740. goto ncu;
  741. }
  742. if(c < '0' || c > '7')
  743. goto dc;
  744. for(;;) {
  745. if(c >= '0' && c <= '7') {
  746. *cp++ = c;
  747. c = GETC();
  748. continue;
  749. }
  750. goto ncu;
  751. }
  752. dc:
  753. if(c == '.')
  754. goto casedot;
  755. if(c == 'e' || c == 'E')
  756. goto casee;
  757. ncu:
  758. if((c == 'U' || c == 'u') && !(c1 & Numuns)) {
  759. c = GETC();
  760. c1 |= Numuns;
  761. goto ncu;
  762. }
  763. if((c == 'L' || c == 'l') && !(c1 & Numvlong)) {
  764. c = GETC();
  765. if(c1 & Numlong)
  766. c1 |= Numvlong;
  767. c1 |= Numlong;
  768. goto ncu;
  769. }
  770. *cp = 0;
  771. peekc = c;
  772. if(mpatov(symb, &yylval.vval))
  773. yyerror("overflow in constant");
  774. vv = yylval.vval;
  775. if(c1 & Numvlong) {
  776. if((c1 & Numuns) || convvtox(vv, TVLONG) < 0) {
  777. c = LUVLCONST;
  778. t = TUVLONG;
  779. goto nret;
  780. }
  781. c = LVLCONST;
  782. t = TVLONG;
  783. goto nret;
  784. }
  785. if(c1 & Numlong) {
  786. if((c1 & Numuns) || convvtox(vv, TLONG) < 0) {
  787. c = LULCONST;
  788. t = TULONG;
  789. goto nret;
  790. }
  791. c = LLCONST;
  792. t = TLONG;
  793. goto nret;
  794. }
  795. if((c1 & Numuns) || convvtox(vv, TINT) < 0) {
  796. c = LUCONST;
  797. t = TUINT;
  798. goto nret;
  799. }
  800. c = LCONST;
  801. t = TINT;
  802. goto nret;
  803. nret:
  804. yylval.vval = convvtox(vv, t);
  805. if(yylval.vval != vv){
  806. nearln = lineno;
  807. warn(Z, "truncated constant: %T %s", types[t], symb);
  808. }
  809. return c;
  810. casedot:
  811. for(;;) {
  812. *cp++ = c;
  813. c = GETC();
  814. if(!isdigit(c))
  815. break;
  816. }
  817. if(c != 'e' && c != 'E')
  818. goto caseout;
  819. casee:
  820. *cp++ = 'e';
  821. c = GETC();
  822. if(c == '+' || c == '-') {
  823. *cp++ = c;
  824. c = GETC();
  825. }
  826. if(!isdigit(c))
  827. yyerror("malformed fp constant exponent");
  828. while(isdigit(c)) {
  829. *cp++ = c;
  830. c = GETC();
  831. }
  832. caseout:
  833. if(c == 'L' || c == 'l') {
  834. c = GETC();
  835. c1 |= Numlong;
  836. } else
  837. if(c == 'F' || c == 'f') {
  838. c = GETC();
  839. c1 |= Numflt;
  840. }
  841. *cp = 0;
  842. peekc = c;
  843. yylval.dval = strtod(symb, nil);
  844. if(isInf(yylval.dval, 1) || isInf(yylval.dval, -1)) {
  845. yyerror("overflow in float constant");
  846. yylval.dval = 0;
  847. }
  848. if(c1 & Numflt)
  849. return LFCONST;
  850. return LDCONST;
  851. }
  852. /*
  853. * convert a string, s, to vlong in *v
  854. * return conversion overflow.
  855. * required syntax is [0[x]]d*
  856. */
  857. int
  858. mpatov(char *s, vlong *v)
  859. {
  860. vlong n, nn;
  861. int c;
  862. n = 0;
  863. c = *s;
  864. if(c == '0')
  865. goto oct;
  866. while((c = *s++)) {
  867. if(c >= '0' && c <= '9')
  868. nn = n*10 + c-'0';
  869. else
  870. goto bad;
  871. if(n < 0 && nn >= 0)
  872. goto bad;
  873. n = nn;
  874. }
  875. goto out;
  876. oct:
  877. s++;
  878. c = *s;
  879. if(c == 'x' || c == 'X')
  880. goto hex;
  881. while((c = *s++)) {
  882. if(c >= '0' || c <= '7')
  883. nn = n*8 + c-'0';
  884. else
  885. goto bad;
  886. if(n < 0 && nn >= 0)
  887. goto bad;
  888. n = nn;
  889. }
  890. goto out;
  891. hex:
  892. s++;
  893. while((c = *s++)) {
  894. if(c >= '0' && c <= '9')
  895. c += 0-'0';
  896. else
  897. if(c >= 'a' && c <= 'f')
  898. c += 10-'a';
  899. else
  900. if(c >= 'A' && c <= 'F')
  901. c += 10-'A';
  902. else
  903. goto bad;
  904. nn = n*16 + c;
  905. if(n < 0 && nn >= 0)
  906. goto bad;
  907. n = nn;
  908. }
  909. out:
  910. *v = n;
  911. return 0;
  912. bad:
  913. *v = ~0;
  914. return 1;
  915. }
  916. int
  917. getc(void)
  918. {
  919. int c;
  920. if(peekc != IGN) {
  921. c = peekc;
  922. peekc = IGN;
  923. } else
  924. c = GETC();
  925. if(c == '\n')
  926. lineno++;
  927. if(c == EOF) {
  928. yyerror("End of file");
  929. errorexit();
  930. }
  931. return c;
  932. }
  933. long
  934. getr(void)
  935. {
  936. int c, i;
  937. char str[UTFmax+1];
  938. Rune rune;
  939. c = getc();
  940. if(c < Runeself)
  941. return c;
  942. i = 0;
  943. str[i++] = c;
  944. loop:
  945. c = getc();
  946. str[i++] = c;
  947. if(!fullrune(str, i))
  948. goto loop;
  949. c = chartorune(&rune, str);
  950. if(rune == Runeerror && c == 1) {
  951. nearln = lineno;
  952. diag(Z, "illegal rune in string");
  953. for(c=0; c<i; c++)
  954. print(" %.2x", *(uchar*)(str+c));
  955. print("\n");
  956. }
  957. return rune;
  958. }
  959. int
  960. getnsc(void)
  961. {
  962. int c;
  963. if(peekc != IGN) {
  964. c = peekc;
  965. peekc = IGN;
  966. } else
  967. c = GETC();
  968. for(;;) {
  969. if(c >= Runeself || !isspace(c))
  970. return c;
  971. if(c == '\n') {
  972. lineno++;
  973. return c;
  974. }
  975. c = GETC();
  976. }
  977. }
  978. void
  979. unget(int c)
  980. {
  981. peekc = c;
  982. if(c == '\n')
  983. lineno--;
  984. }
  985. long
  986. escchar(long e, int longflg, int escflg)
  987. {
  988. long c, l;
  989. int i;
  990. loop:
  991. c = getr();
  992. if(c == '\n') {
  993. yyerror("newline in string");
  994. return EOF;
  995. }
  996. if(c != '\\') {
  997. if(c == e)
  998. c = EOF;
  999. return c;
  1000. }
  1001. c = getr();
  1002. if(c == 'x') {
  1003. /*
  1004. * note this is not ansi,
  1005. * supposed to only accept 2 hex
  1006. */
  1007. i = 2;
  1008. if(longflg)
  1009. i = 6;
  1010. l = 0;
  1011. for(; i>0; i--) {
  1012. c = getc();
  1013. if(c >= '0' && c <= '9') {
  1014. l = l*16 + c-'0';
  1015. continue;
  1016. }
  1017. if(c >= 'a' && c <= 'f') {
  1018. l = l*16 + c-'a' + 10;
  1019. continue;
  1020. }
  1021. if(c >= 'A' && c <= 'F') {
  1022. l = l*16 + c-'A' + 10;
  1023. continue;
  1024. }
  1025. unget(c);
  1026. break;
  1027. }
  1028. if(escflg)
  1029. l |= ESC;
  1030. return l;
  1031. }
  1032. if(c >= '0' && c <= '7') {
  1033. /*
  1034. * note this is not ansi,
  1035. * supposed to only accept 3 oct
  1036. */
  1037. i = 2;
  1038. if(longflg)
  1039. i = 8;
  1040. l = c - '0';
  1041. for(; i>0; i--) {
  1042. c = getc();
  1043. if(c >= '0' && c <= '7') {
  1044. l = l*8 + c-'0';
  1045. continue;
  1046. }
  1047. unget(c);
  1048. }
  1049. if(escflg)
  1050. l |= ESC;
  1051. return l;
  1052. }
  1053. switch(c)
  1054. {
  1055. case '\n': goto loop;
  1056. case 'n': return '\n';
  1057. case 't': return '\t';
  1058. case 'b': return '\b';
  1059. case 'r': return '\r';
  1060. case 'f': return '\f';
  1061. case 'a': return '\a';
  1062. case 'v': return '\v';
  1063. }
  1064. return c;
  1065. }
  1066. struct
  1067. {
  1068. char *name;
  1069. ushort lexical;
  1070. ushort type;
  1071. } itab[] = {
  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. long idel; /* delta line number to apply to include */
  1223. Hist* line; /* start of this #line directive */
  1224. long ldel; /* delta line number to apply to #line */
  1225. } a[HISTSZ];
  1226. long l, d;
  1227. int i, n;
  1228. l = va_arg(fp->args, long);
  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. long 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. long b;
  1354. int i;
  1355. str[0] = 0;
  1356. for(b = va_arg(fp->args, long); 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(long 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, long on, long n)
  1409. {
  1410. void *q;
  1411. q = (uchar*)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. }