lex.c 24 KB

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