lex.c 23 KB

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