lex.c 24 KB

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