lex.c 23 KB

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