lex.c 24 KB

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