statement.g 7.5 KB

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