statement.g 7.4 KB

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