domacro.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  1. /*
  2. * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
  3. * See the copyright notice in the ACK home directory, in the file "Copyright".
  4. */
  5. /* $Header$ */
  6. /* PREPROCESSOR: CONTROLLINE INTERPRETER */
  7. #include "interface.h"
  8. #include <em_arith.h>
  9. #include "LLlex.h"
  10. #include "Lpars.h"
  11. #include "debug.h"
  12. #include "idf.h"
  13. #include "input.h"
  14. #include "ifdepth.h"
  15. #include "botch_free.h"
  16. #include "nparams.h"
  17. #include "parbufsize.h"
  18. #include "textsize.h"
  19. #include "idfsize.h"
  20. #include <assert.h>
  21. #include <alloc.h>
  22. #include "class.h"
  23. #include "macro.h"
  24. #include "bits.h"
  25. IMPORT char *inctable[]; /* list of include directories */
  26. IMPORT char *getwdir();
  27. PRIVATE char ifstack[IFDEPTH]; /* if-stack: the content of an entry is */
  28. /* 1 if a corresponding ELSE has been */
  29. /* encountered. */
  30. PRIVATE int nestlevel = -1; /* initially no nesting level. */
  31. PRIVATE char *
  32. GetIdentifier()
  33. {
  34. /* returns a pointer to the descriptor of the identifier that is
  35. read from the input stream. A null-pointer is returned if
  36. the input does not contain an identifier.
  37. The substitution of macros is disabled.
  38. */
  39. int tok;
  40. struct token tk;
  41. ReplaceMacros = 0;
  42. tok = GetToken(&tk);
  43. ReplaceMacros = 1;
  44. return tok == IDENTIFIER ? tk.tk_str : (char *)0;
  45. }
  46. /* domacro() is the control line interpreter. The '#' has already
  47. been read by the lexical analyzer by which domacro() is called.
  48. The token appearing directly after the '#' is obtained by calling
  49. the basic lexical analyzing function GetToken() and is interpreted
  50. to perform the action belonging to that token.
  51. An error message is produced when the token is not recognized,
  52. i.e. it is not one of "define" .. "undef" , integer or newline.
  53. */
  54. EXPORT
  55. domacro()
  56. {
  57. struct token tk; /* the token itself */
  58. register struct idf *id;
  59. switch(GetToken(&tk)) { /* select control line action */
  60. case IDENTIFIER: /* is it a macro keyword? */
  61. id = findidf(tk.tk_str);
  62. if (!id) {
  63. error("%s: unknown control", tk.tk_str);
  64. PushBack();
  65. skipline();
  66. free(tk.tk_str);
  67. break;
  68. }
  69. free(tk.tk_str);
  70. switch (id->id_resmac) {
  71. case K_DEFINE: /* "define" */
  72. do_define();
  73. break;
  74. case K_ELIF: /* "elif" */
  75. do_elif();
  76. break;
  77. case K_ELSE: /* "else" */
  78. do_else();
  79. break;
  80. case K_ENDIF: /* "endif" */
  81. do_endif();
  82. break;
  83. case K_IF: /* "if" */
  84. do_if();
  85. break;
  86. case K_IFDEF: /* "ifdef" */
  87. do_ifdef(1);
  88. break;
  89. case K_IFNDEF: /* "ifndef" */
  90. do_ifdef(0);
  91. break;
  92. case K_INCLUDE: /* "include" */
  93. do_include();
  94. break;
  95. case K_LINE: /* "line" */
  96. /* set LineNumber and FileName according to
  97. the arguments.
  98. */
  99. if (GetToken(&tk) != INTEGER) {
  100. error("#line without linenumber");
  101. PushBack();
  102. skipline();
  103. }
  104. else
  105. do_line((unsigned int)tk.tk_val);
  106. break;
  107. case K_UNDEF: /* "undef" */
  108. do_undef();
  109. break;
  110. default:
  111. /* invalid word seen after the '#' */
  112. error("%s: unknown control", id->id_text);
  113. PushBack();
  114. skipline();
  115. }
  116. break;
  117. case INTEGER: /* # <integer> [<filespecifier>]? */
  118. do_line((unsigned int)tk.tk_val);
  119. break;
  120. case EOF: /* only `#' on this line: do nothing, ignore */
  121. break;
  122. default: /* invalid token following '#' */
  123. error("illegal # line");
  124. PushBack();
  125. skipline();
  126. }
  127. }
  128. PRIVATE
  129. skip_block()
  130. {
  131. /* skip_block() skips the input from
  132. 1) a false #if, #ifdef, #ifndef or #elif until the
  133. corresponding #elif (resulting in true), #else or
  134. #endif is read.
  135. 2) a #else corresponding to a true #if, #ifdef,
  136. #ifndef or #elif until the corresponding #endif is
  137. seen.
  138. */
  139. register int ch;
  140. register int skiplevel = nestlevel; /* current nesting level */
  141. struct token tk;
  142. struct idf *id;
  143. NoUnstack++;
  144. for (;;) {
  145. LoadChar(ch); /* read first character after newline */
  146. if (ch != '#') {
  147. if (ch == EOI) {
  148. NoUnstack--;
  149. return;
  150. }
  151. PushBack();
  152. skipline();
  153. continue;
  154. }
  155. if (GetToken(&tk) != IDENTIFIER) {
  156. PushBack();
  157. skipline();
  158. continue;
  159. }
  160. /* an IDENTIFIER: look for #if, #ifdef and #ifndef
  161. without interpreting them.
  162. Interpret #else, #elif and #endif if they occur
  163. on the same level.
  164. */
  165. id = findidf(tk.tk_str);
  166. free(tk.tk_str);
  167. if (id) switch(id->id_resmac) {
  168. case K_IF:
  169. case K_IFDEF:
  170. case K_IFNDEF:
  171. push_if();
  172. continue;
  173. case K_ELIF:
  174. if (nestlevel == skiplevel) {
  175. nestlevel--;
  176. push_if();
  177. if (ifexpr()) {
  178. NoUnstack--;
  179. return;
  180. }
  181. }
  182. break;
  183. case K_ELSE:
  184. ++(ifstack[nestlevel]);
  185. if (nestlevel == skiplevel) {
  186. PushBack();
  187. skipline();
  188. NoUnstack--;
  189. return;
  190. }
  191. break;
  192. case K_ENDIF:
  193. assert(nestlevel >= 0);
  194. if (nestlevel == skiplevel) {
  195. PushBack();
  196. skipline();
  197. nestlevel--;
  198. NoUnstack--;
  199. return;
  200. }
  201. nestlevel--;
  202. break;
  203. }
  204. }
  205. }
  206. PRIVATE
  207. ifexpr()
  208. {
  209. /* ifexpr() returns whether the restricted constant
  210. expression following #if or #elif evaluates to true. This
  211. is done by calling the LLgen generated subparser for
  212. constant expressions. The result of this expression will
  213. be given in the extern long variable "ifval".
  214. */
  215. IMPORT arith ifval;
  216. int errors = err_occurred;
  217. ifval = (arith)0;
  218. AccDefined = 1;
  219. UnknownIdIsZero = 1;
  220. PushLex(); /* NEW parser */
  221. If_expr(); /* invoke constant expression parser */
  222. PopLex(); /* OLD parser */
  223. AccDefined = 0;
  224. UnknownIdIsZero = 0;
  225. return (errors == err_occurred) && (ifval != (arith)0);
  226. }
  227. PRIVATE
  228. do_include()
  229. {
  230. /* do_include() performs the inclusion of a file.
  231. */
  232. char *filenm;
  233. char *result;
  234. int tok;
  235. struct token tk;
  236. AccFileSpecifier = 1;
  237. if (((tok = GetToken(&tk)) == FILESPECIFIER) || tok == STRING)
  238. filenm = tk.tk_str;
  239. else {
  240. error("bad include syntax");
  241. filenm = (char *)0;
  242. }
  243. AccFileSpecifier = 0;
  244. PushBack();
  245. skipline();
  246. inctable[0] = WorkingDir;
  247. if (filenm) {
  248. if (!InsertFile(filenm, &inctable[tok==FILESPECIFIER],&result)){
  249. error("cannot find include file \"%s\"", filenm);
  250. }
  251. else {
  252. WorkingDir = getwdir(result);
  253. FileName = result;
  254. LineNumber = 1;
  255. }
  256. }
  257. }
  258. PRIVATE
  259. do_define()
  260. {
  261. /* do_define() interprets a #define control line.
  262. */
  263. char *str;
  264. int nformals = -1; /* keep track of the number of formals */
  265. char *formals[NPARAMS]; /* pointers to the names of the formals */
  266. char parbuf[PARBUFSIZE]; /* names of formals */
  267. char *repl_text; /* start of the replacement text */
  268. int length; /* length of the replacement text */
  269. register ch;
  270. char *get_text();
  271. /* read the #defined macro's name */
  272. if (!(str = GetIdentifier())) {
  273. error("#define: illegal macro name");
  274. PushBack();
  275. skipline();
  276. return;
  277. }
  278. /* there is a formal parameter list if the identifier is
  279. followed immediately by a '('.
  280. */
  281. LoadChar(ch);
  282. if (ch == '(') {
  283. if ((nformals = getparams(formals, parbuf)) == -1) {
  284. PushBack();
  285. skipline();
  286. free(str);
  287. return; /* an error occurred */
  288. }
  289. LoadChar(ch);
  290. }
  291. /* read the replacement text if there is any */
  292. ch = skipspaces(ch); /* find first character of the text */
  293. assert(ch != EOI);
  294. if (class(ch) == STNL) {
  295. /* Treat `#define something' as `#define something ""'
  296. */
  297. repl_text = "";
  298. length = 0;
  299. }
  300. else {
  301. PushBack();
  302. repl_text = get_text((nformals > 0) ? formals : 0, &length);
  303. }
  304. macro_def(str2idf(str, 0), repl_text, nformals, length, NOFLAG);
  305. LineNumber++;
  306. }
  307. PRIVATE
  308. push_if()
  309. {
  310. if (nestlevel >= IFDEPTH)
  311. fatal("too many nested #if/#ifdef/#ifndef");
  312. else
  313. ifstack[++nestlevel] = 0;
  314. }
  315. PRIVATE
  316. do_elif()
  317. {
  318. if (nestlevel < 0 || (ifstack[nestlevel])) {
  319. error("#elif without corresponding #if");
  320. PushBack();
  321. skipline();
  322. }
  323. else { /* restart at this level as if a #if is detected. */
  324. nestlevel--;
  325. push_if();
  326. skip_block();
  327. }
  328. }
  329. PRIVATE
  330. do_else()
  331. {
  332. PushBack();
  333. skipline();
  334. if (nestlevel < 0 || (ifstack[nestlevel]))
  335. error("#else without corresponding #if");
  336. else { /* mark this level as else-d */
  337. ++(ifstack[nestlevel]);
  338. skip_block();
  339. }
  340. }
  341. PRIVATE
  342. do_endif()
  343. {
  344. PushBack();
  345. skipline();
  346. if (nestlevel-- < 0)
  347. error("#endif without corresponding #if");
  348. }
  349. PRIVATE
  350. do_if()
  351. {
  352. push_if();
  353. if (!ifexpr()) /* a false #if/#elif expression */
  354. skip_block();
  355. }
  356. PRIVATE
  357. do_ifdef(how)
  358. {
  359. register struct idf *id;
  360. char *str;
  361. /* how == 1 : ifdef; how == 0 : ifndef
  362. */
  363. push_if();
  364. str = GetIdentifier();
  365. if (!str) {
  366. error("illegal #ifdef construction");
  367. id = 0;
  368. }
  369. else {
  370. id = findidf(str);
  371. free(str);
  372. }
  373. /* The next test is a shorthand for:
  374. (how && !id->id_macro) || (!how && id->id_macro)
  375. */
  376. if (how ^ (id && id->id_macro != 0))
  377. skip_block();
  378. else {
  379. PushBack();
  380. skipline();
  381. }
  382. }
  383. PRIVATE
  384. do_undef()
  385. {
  386. register struct idf *id;
  387. register char *str = GetIdentifier();
  388. /* Forget a macro definition. */
  389. if (str) {
  390. if (id = findidf(str)) {
  391. if (id->id_macro) { /* forget the macro */
  392. free_macro(id->id_macro);
  393. id->id_macro = (struct macro *) 0;
  394. } /* else: don't complain */
  395. }
  396. free(str);
  397. }
  398. else
  399. error("illegal #undef construction");
  400. PushBack();
  401. skipline();
  402. }
  403. PRIVATE
  404. do_line(l)
  405. unsigned int l;
  406. {
  407. struct token tk;
  408. LineNumber = l - 1; /* the number of the next input line */
  409. if (GetToken(&tk) == STRING) /* is there a filespecifier? */
  410. FileName = tk.tk_str;
  411. PushBack();
  412. skipline();
  413. }
  414. PRIVATE int
  415. getparams(buf, parbuf)
  416. char *buf[];
  417. char parbuf[];
  418. {
  419. /* getparams() reads the formal parameter list of a macro
  420. definition.
  421. The number of parameters is returned.
  422. As a formal parameter list is expected when calling this
  423. routine, -1 is returned if an error is detected, for
  424. example:
  425. #define one(1), where 1 is not an identifier.
  426. Note that the '(' has already been eaten.
  427. The names of the formal parameters are stored into parbuf.
  428. */
  429. register char **pbuf = &buf[0];
  430. register int c;
  431. register char *ptr = &parbuf[0];
  432. register char **pbuf2;
  433. LoadChar(c);
  434. c = skipspaces(c);
  435. if (c == ')') { /* no parameters: #define name() */
  436. *pbuf = (char *) 0;
  437. return 0;
  438. }
  439. for (;;) { /* eat the formal parameter list */
  440. if (class(c) != STIDF) { /* not an identifier */
  441. error("#define: bad formal parameter");
  442. return -1;
  443. }
  444. *pbuf = ptr; /* name of the formal */
  445. *ptr++ = c;
  446. if (ptr >= &parbuf[PARBUFSIZE])
  447. fatal("formal parameter buffer overflow");
  448. do { /* eat the identifier name */
  449. LoadChar(c);
  450. *ptr++ = c;
  451. if (ptr >= &parbuf[PARBUFSIZE])
  452. fatal("formal parameter buffer overflow");
  453. } while (in_idf(c));
  454. *(ptr - 1) = '\0'; /* mark end of the name */
  455. /* Check if this formal parameter is already used.
  456. Usually, macros do not have many parameters, so ...
  457. */
  458. for (pbuf2 = pbuf - 1; pbuf2 >= &buf[0]; pbuf2--) {
  459. if (!strcmp(*pbuf2, *pbuf)) {
  460. warning("formal parameter \"%s\" already used",
  461. *pbuf);
  462. }
  463. }
  464. pbuf++;
  465. c = skipspaces(c);
  466. if (c == ')') { /* end of the formal parameter list */
  467. *pbuf = (char *) 0;
  468. return pbuf - buf;
  469. }
  470. if (c != ',') {
  471. error("#define: bad formal parameter list");
  472. return -1;
  473. }
  474. LoadChar(c);
  475. c = skipspaces(c);
  476. }
  477. /*NOTREACHED*/
  478. }
  479. EXPORT
  480. macro_def(id, text, nformals, length, flags)
  481. register struct idf *id;
  482. char *text;
  483. {
  484. /* macro_def() puts the contents and information of a macro
  485. definition into a structure and stores it into the symbol
  486. table entry belonging to the name of the macro.
  487. A warning is given if the definition overwrites another.
  488. */
  489. register struct macro *newdef = id->id_macro;
  490. if (newdef) { /* is there a redefinition? */
  491. if (macroeq(newdef->mc_text, text))
  492. return;
  493. warning("redefine \"%s\"", id->id_text);
  494. }
  495. else {
  496. #ifdef DOBITS
  497. register char *p = id->id_text;
  498. #define setbit(bx) if (!*p) goto go_on; bits[*p++] |= (bx)
  499. setbit(bit0);
  500. setbit(bit1);
  501. setbit(bit2);
  502. setbit(bit3);
  503. setbit(bit4);
  504. setbit(bit5);
  505. setbit(bit6);
  506. setbit(bit7);
  507. go_on:
  508. #endif
  509. id->id_macro = newdef = new_macro();
  510. }
  511. newdef->mc_text = text; /* replacement text */
  512. newdef->mc_nps = nformals; /* nr of formals */
  513. newdef->mc_length = length; /* length of repl. text */
  514. newdef->mc_flag = flags; /* special flags */
  515. newdef->mc_count = 0;
  516. }
  517. PRIVATE int
  518. find_name(nm, index)
  519. char *nm, *index[];
  520. {
  521. /* find_name() returns the index of "nm" in the namelist
  522. "index" if it can be found there. 0 is returned if it is
  523. not there.
  524. */
  525. register char **ip = &index[0];
  526. while (*ip)
  527. if (strcmp(nm, *ip++) == 0)
  528. return ip - &index[0];
  529. /* arrived here, nm is not in the name list. */
  530. return 0;
  531. }
  532. PRIVATE char *
  533. get_text(formals, length)
  534. char *formals[];
  535. int *length;
  536. {
  537. /* get_text() copies the replacement text of a macro
  538. definition with zero, one or more parameters, thereby
  539. substituting each formal parameter by a special character
  540. (non-ascii: 0200 & (order-number in the formal parameter
  541. list)) in order to substitute this character later by the
  542. actual parameter. The replacement text is copied into
  543. itself because the copied text will contain fewer or the
  544. same amount of characters. The length of the replacement
  545. text is returned.
  546. Implementation:
  547. finite automaton : we are only interested in
  548. identifiers, because they might be replaced by some actual
  549. parameter. Other tokens will not be seen as such.
  550. */
  551. register int c;
  552. register unsigned int text_size;
  553. char *text = Malloc(text_size = ITEXTSIZE);
  554. register unsigned int pos = 0;
  555. LoadChar(c);
  556. while ((c != EOI) && (class(c) != STNL)) {
  557. if (c == '\\') { /* check for "\\\n" */
  558. LoadChar(c);
  559. if (c == '\n') {
  560. /* More than one line is used for the
  561. replacement text.
  562. Replace "\\\n" by " ".
  563. */
  564. text[pos++] = ' ';
  565. ++LineNumber;
  566. LoadChar(c);
  567. }
  568. else
  569. text[pos++] = '\\';
  570. if (pos == text_size)
  571. text = Srealloc(text, text_size <<= 1);
  572. }
  573. else
  574. if ( c == '/') {
  575. LoadChar(c);
  576. if (c == '*') {
  577. skipcomment();
  578. text[pos++] = ' ';
  579. LoadChar(c);
  580. }
  581. else
  582. text[pos++] = '/';
  583. if (pos == text_size)
  584. text = Srealloc(text, text_size <<= 1);
  585. }
  586. else
  587. if (formals && class(c) == STIDF) {
  588. char id_buf[IDFSIZE + 1];
  589. register char *idp = id_buf;
  590. int n;
  591. /* read identifier: it may be a formal parameter */
  592. *idp++ = c;
  593. do {
  594. LoadChar(c);
  595. if (idp <= &id_buf[IDFSIZE])
  596. *idp++ = c;
  597. } while (in_idf(c));
  598. *--idp = '\0';
  599. if (n = find_name(id_buf, formals)) {
  600. /* construct the formal parameter mark */
  601. text[pos++] = FORMALP | (char) n;
  602. if (pos == text_size)
  603. text = Srealloc(text,
  604. text_size <<= 1);
  605. }
  606. else {
  607. int sz = idp - id_buf;
  608. idp = id_buf;
  609. while (pos + sz >= text_size)
  610. text = Srealloc(text,
  611. text_size <<= 1);
  612. while (text[pos++] = *idp++) ;
  613. pos--;
  614. }
  615. }
  616. else {
  617. text[pos++] = c;
  618. if (pos == text_size)
  619. text = Srealloc(text, text_size <<= 1);
  620. LoadChar(c);
  621. }
  622. }
  623. text[pos++] = '\0';
  624. *length = pos - 1;
  625. return text;
  626. }
  627. #define BLANK(ch) ((ch == ' ') || (ch == '\t'))
  628. /* macroeq() decides whether two macro replacement texts are
  629. identical. This version compares the texts, which occur
  630. as strings, without taking care of the leading and trailing
  631. blanks (spaces and tabs).
  632. */
  633. PRIVATE
  634. macroeq(s, t)
  635. register char *s, *t;
  636. {
  637. /* skip leading spaces */
  638. while (BLANK(*s)) s++;
  639. while (BLANK(*t)) t++;
  640. /* first non-blank encountered in both strings */
  641. /* The actual comparison loop: */
  642. while (*s && *s == *t)
  643. s++, t++;
  644. /* two cases are possible when arrived here: */
  645. if (*s == '\0') { /* *s == '\0' */
  646. while (BLANK(*t)) t++;
  647. return *t == '\0';
  648. }
  649. else { /* *s != *t */
  650. while (BLANK(*s)) s++;
  651. while (BLANK(*t)) t++;
  652. return (*s == '\0') && (*t == '\0');
  653. }
  654. }