statement.g 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  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. /* $Id$ */
  6. /* STATEMENT SYNTAX PARSER */
  7. {
  8. #include "lint.h"
  9. #ifndef LINT
  10. #include <em.h>
  11. #else
  12. #include "l_em.h"
  13. #include "l_lint.h"
  14. #endif /* LINT */
  15. #include "debug.h"
  16. #include "botch_free.h"
  17. #include "dbsymtab.h"
  18. #include <flt_arith.h>
  19. #include "arith.h"
  20. #include "LLlex.h"
  21. #include "type.h"
  22. #include "idf.h"
  23. #include "label.h"
  24. #include "expr.h"
  25. #include "code.h"
  26. #include "stack.h"
  27. #include "def.h"
  28. #ifdef DBSYMTAB
  29. #include <stb.h>
  30. #endif /* DBSYMTAB */
  31. extern int level;
  32. extern char options[];
  33. }
  34. /* Each statement construction is stacked in order to trace a
  35. * statement to such a construction. Example: a case statement should
  36. * be recognized as a piece of the most enclosing switch statement.
  37. */
  38. /* 3.6 */
  39. statement
  40. {
  41. #ifdef LINT
  42. lint_statement();
  43. #endif /* LINT */
  44. }
  45. :
  46. %if (AHEAD != ':')
  47. expression_statement
  48. |
  49. label ':' statement
  50. |
  51. compound_statement
  52. |
  53. if_statement
  54. |
  55. while_statement
  56. |
  57. do_statement
  58. |
  59. for_statement
  60. |
  61. switch_statement
  62. |
  63. case_statement
  64. |
  65. default_statement
  66. |
  67. BREAK
  68. {
  69. code_break();
  70. #ifdef LINT
  71. lint_break_stmt();
  72. #endif /* LINT */
  73. }
  74. ';'
  75. |
  76. CONTINUE
  77. {
  78. code_continue();
  79. #ifdef LINT
  80. lint_continue_stmt();
  81. #endif /* LINT */
  82. }
  83. ';'
  84. |
  85. return_statement
  86. |
  87. jump
  88. |
  89. ';'
  90. ;
  91. expression_statement
  92. { struct expr *expr;
  93. }
  94. :
  95. expression(&expr)
  96. ';'
  97. {
  98. #ifdef DEBUG
  99. print_expr("expression_statement", expr);
  100. #endif /* DEBUG */
  101. code_expr(expr, RVAL, FALSE, NO_LABEL, NO_LABEL);
  102. free_expression(expr);
  103. }
  104. ;
  105. /* 3.6.1 (partially) */
  106. label
  107. { struct idf *idf; }
  108. :
  109. identifier(&idf)
  110. {
  111. /* This allows the following absurd case:
  112. typedef int grz;
  113. main() {
  114. grz: printf("A labelled statement\n");
  115. }
  116. */
  117. #ifdef LINT
  118. lint_label();
  119. #endif /* LINT */
  120. define_label(idf);
  121. C_df_ilb((label)idf->id_label->df_address);
  122. }
  123. ;
  124. /* 3.6.4.1 */
  125. if_statement
  126. {
  127. struct expr *expr;
  128. label l_true = text_label();
  129. label l_false = text_label();
  130. label l_end = text_label();
  131. }
  132. :
  133. IF
  134. '('
  135. expression(&expr)
  136. {
  137. opnd2test(&expr, IF);
  138. if (is_cp_cst(expr)) {
  139. /* The comparison has been optimized
  140. to a 0 or 1.
  141. */
  142. if (expr->VL_VALUE == (arith)0) {
  143. C_bra(l_false);
  144. }
  145. /* else fall through */
  146. #ifdef LINT
  147. start_if_part(1);
  148. #endif /* LINT */
  149. }
  150. else {
  151. code_expr(expr, RVAL, TRUE, l_true, l_false);
  152. C_df_ilb(l_true);
  153. #ifdef LINT
  154. start_if_part(0);
  155. #endif /* LINT */
  156. }
  157. free_expression(expr);
  158. }
  159. ')'
  160. statement
  161. [%prefer
  162. ELSE
  163. {
  164. #ifdef LINT
  165. start_else_part();
  166. #endif /* LINT */
  167. C_bra(l_end);
  168. C_df_ilb(l_false);
  169. }
  170. statement
  171. { C_df_ilb(l_end);
  172. #ifdef LINT
  173. end_if_else_stmt();
  174. #endif /* LINT */
  175. }
  176. |
  177. empty
  178. { C_df_ilb(l_false);
  179. #ifdef LINT
  180. end_if_stmt();
  181. #endif /* LINT */
  182. }
  183. ]
  184. ;
  185. /* 3.6.5.3 */
  186. while_statement
  187. {
  188. struct expr *expr;
  189. label l_break = text_label();
  190. label l_continue = text_label();
  191. label l_body = text_label();
  192. }
  193. :
  194. WHILE
  195. {
  196. stack_stmt(l_break, l_continue);
  197. C_df_ilb(l_continue);
  198. }
  199. '('
  200. expression(&expr)
  201. {
  202. opnd2test(&expr, WHILE);
  203. if (is_cp_cst(expr)) {
  204. if (expr->VL_VALUE == (arith)0) {
  205. C_bra(l_break);
  206. }
  207. }
  208. else {
  209. code_expr(expr, RVAL, TRUE, l_body, l_break);
  210. C_df_ilb(l_body);
  211. }
  212. #ifdef LINT
  213. start_while_stmt(expr);
  214. #endif /* LINT */
  215. }
  216. ')'
  217. statement
  218. {
  219. C_bra(l_continue);
  220. C_df_ilb(l_break);
  221. unstack_stmt();
  222. free_expression(expr);
  223. #ifdef LINT
  224. end_loop_body();
  225. end_loop_stmt();
  226. #endif /* LINT */
  227. }
  228. ;
  229. /* 3.6.5.2 */
  230. do_statement
  231. { struct expr *expr;
  232. label l_break = text_label();
  233. label l_continue = text_label();
  234. label l_body = text_label();
  235. }
  236. :
  237. DO
  238. { C_df_ilb(l_body);
  239. stack_stmt(l_break, l_continue);
  240. #ifdef LINT
  241. start_do_stmt();
  242. #endif /* LINT */
  243. }
  244. statement
  245. WHILE
  246. '('
  247. {
  248. #ifdef LINT
  249. end_loop_body();
  250. #endif /* LINT */
  251. C_df_ilb(l_continue);
  252. }
  253. expression(&expr)
  254. {
  255. opnd2test(&expr, WHILE);
  256. if (is_cp_cst(expr)) {
  257. if (expr->VL_VALUE == (arith)1) {
  258. C_bra(l_body);
  259. }
  260. #ifdef LINT
  261. end_do_stmt(1, expr->VL_VALUE != (arith)0);
  262. #endif /* LINT */
  263. }
  264. else {
  265. code_expr(expr, RVAL, TRUE, l_body, l_break);
  266. #ifdef LINT
  267. end_do_stmt(0, 0);
  268. #endif /* LINT */
  269. }
  270. C_df_ilb(l_break);
  271. }
  272. ')'
  273. ';'
  274. {
  275. unstack_stmt();
  276. free_expression(expr);
  277. }
  278. ;
  279. /* 3.6.5.3 */
  280. for_statement
  281. { struct expr *e_init = 0, *e_test = 0, *e_incr = 0;
  282. label l_break = text_label();
  283. label l_continue = text_label();
  284. label l_body = text_label();
  285. label l_test = text_label();
  286. }
  287. :
  288. FOR
  289. { stack_stmt(l_break, l_continue);
  290. }
  291. '('
  292. [
  293. expression(&e_init)
  294. { code_expr(e_init, RVAL, FALSE, NO_LABEL, NO_LABEL);
  295. }
  296. ]?
  297. ';'
  298. { C_df_ilb(l_test);
  299. }
  300. [
  301. expression(&e_test)
  302. {
  303. opnd2test(&e_test, FOR);
  304. if (is_cp_cst(e_test)) {
  305. if (e_test->VL_VALUE == (arith)0) {
  306. C_bra(l_break);
  307. }
  308. }
  309. else {
  310. code_expr(e_test, RVAL, TRUE, l_body, l_break);
  311. C_df_ilb(l_body);
  312. }
  313. }
  314. ]?
  315. ';'
  316. expression(&e_incr)?
  317. ')'
  318. {
  319. #ifdef LINT
  320. start_for_stmt(e_test);
  321. #endif /* LINT */
  322. }
  323. statement
  324. {
  325. #ifdef LINT
  326. end_loop_body();
  327. #endif /* LINT */
  328. C_df_ilb(l_continue);
  329. if (e_incr)
  330. code_expr(e_incr, RVAL, FALSE,
  331. NO_LABEL, NO_LABEL);
  332. C_bra(l_test);
  333. C_df_ilb(l_break);
  334. unstack_stmt();
  335. free_expression(e_init);
  336. free_expression(e_test);
  337. free_expression(e_incr);
  338. #ifdef LINT
  339. end_loop_stmt();
  340. #endif /* LINT */
  341. }
  342. ;
  343. /* 3.6.4.2 */
  344. switch_statement
  345. {
  346. struct expr *expr;
  347. }
  348. :
  349. SWITCH
  350. '('
  351. expression(&expr)
  352. {
  353. code_startswitch(&expr);
  354. #ifdef LINT
  355. start_switch_part(is_cp_cst(expr));
  356. #endif /* LINT */
  357. }
  358. ')'
  359. statement
  360. {
  361. #ifdef LINT
  362. end_switch_stmt();
  363. #endif /* LINT */
  364. code_endswitch();
  365. free_expression(expr);
  366. }
  367. ;
  368. /* 3.6.1 (partially) */
  369. case_statement
  370. {
  371. struct expr *expr;
  372. }
  373. :
  374. CASE
  375. constant_expression(&expr)
  376. {
  377. #ifdef LINT
  378. lint_case_stmt(0);
  379. #endif /* LINT */
  380. code_case(expr);
  381. free_expression(expr);
  382. }
  383. ':'
  384. statement
  385. ;
  386. /* 3.6.1 (partially) */
  387. default_statement
  388. :
  389. DEFAULT
  390. {
  391. #ifdef LINT
  392. lint_case_stmt(1);
  393. #endif /* LINT */
  394. code_default();
  395. }
  396. ':'
  397. statement
  398. ;
  399. /* 3.6.6.4 */
  400. return_statement
  401. { struct expr *expr = 0;
  402. }
  403. :
  404. RETURN
  405. [
  406. expression(&expr)
  407. {
  408. #ifdef LINT
  409. lint_ret_conv(expr);
  410. #endif /* LINT */
  411. do_return_expr(expr);
  412. free_expression(expr);
  413. #ifdef LINT
  414. lint_return_stmt(VALRETURNED);
  415. #endif /* LINT */
  416. }
  417. |
  418. empty
  419. {
  420. do_return();
  421. #ifdef LINT
  422. lint_return_stmt(NOVALRETURNED);
  423. #endif /* LINT */
  424. }
  425. ]
  426. ';'
  427. ;
  428. /* 3.6.6.1 (partially) */
  429. jump
  430. { struct idf *idf;
  431. }
  432. :
  433. GOTO
  434. identifier(&idf)
  435. ';'
  436. {
  437. apply_label(idf);
  438. C_bra((label)idf->id_label->df_address);
  439. #ifdef LINT
  440. lint_jump_stmt(idf);
  441. #endif /* LINT */
  442. }
  443. ;
  444. /* 3.6.2 */
  445. compound_statement
  446. {
  447. #ifdef DBSYMTAB
  448. static int brc_level = 1;
  449. int decl_seen = brc_level == 1;
  450. #endif /* DBSYMTAB */
  451. }
  452. :
  453. '{'
  454. {
  455. stack_level();
  456. }
  457. [%while ((DOT != IDENTIFIER && AHEAD != ':') ||
  458. (DOT == IDENTIFIER && AHEAD == IDENTIFIER))
  459. /* >>> conflict on TYPE_IDENTIFIER, IDENTIFIER */
  460. declaration
  461. {
  462. #ifdef DBSYMTAB
  463. decl_seen++;
  464. #endif /* DBSYMTAB */
  465. }
  466. ]*
  467. {
  468. #ifdef DBSYMTAB
  469. ++brc_level;
  470. if (options['g'] && decl_seen) {
  471. C_ms_std((char *) 0, N_LBRAC, brc_level);
  472. }
  473. #endif /* DBSYMTAB */
  474. }
  475. [%persistent
  476. statement
  477. ]*
  478. '}'
  479. {
  480. unstack_level();
  481. #ifdef DBSYMTAB
  482. if (options['g'] && decl_seen) {
  483. C_ms_std((char *) 0, N_RBRAC, brc_level);
  484. }
  485. brc_level--;
  486. #endif /* DBSYMTAB */
  487. }
  488. ;