statement.g 6.8 KB

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