statement.g 7.4 KB

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