statement.g 6.7 KB

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