lex.c 23 KB

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