lex.c 24 KB

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