statement.g 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  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 "arith.h"
  19. #include "LLlex.h"
  20. #include "type.h"
  21. #include "idf.h"
  22. #include "label.h"
  23. #include "expr.h"
  24. #include "code.h"
  25. #include "stack.h"
  26. #include "def.h"
  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. /* 9 */
  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. asm_statement
  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. label
  107. { struct idf *idf;
  108. }
  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_def->df_address);
  123. }
  124. ;
  125. if_statement
  126. {
  127. struct expr *expr;
  128. label l_true = text_label();
  129. label l_false = text_label();
  130. label l_end = text_label();
  131. }
  132. :
  133. IF
  134. '('
  135. expression(&expr)
  136. {
  137. opnd2test(&expr, IF);
  138. if (is_cp_cst(expr)) {
  139. /* The comparison has been optimized
  140. to a 0 or 1.
  141. */
  142. if (expr->VL_VALUE == (arith)0) {
  143. C_bra(l_false);
  144. }
  145. /* else fall through */
  146. #ifdef LINT
  147. start_if_part(1);
  148. #endif /* LINT */
  149. }
  150. else {
  151. code_expr(expr, RVAL, TRUE, l_true, l_false);
  152. C_df_ilb(l_true);
  153. #ifdef LINT
  154. start_if_part(0);
  155. #endif /* LINT */
  156. }
  157. free_expression(expr);
  158. }
  159. ')'
  160. statement
  161. [%prefer
  162. ELSE
  163. {
  164. #ifdef LINT
  165. start_else_part();
  166. #endif /* LINT */
  167. C_bra(l_end);
  168. C_df_ilb(l_false);
  169. }
  170. statement
  171. { C_df_ilb(l_end);
  172. #ifdef LINT
  173. end_if_else_stmt();
  174. #endif /* LINT */
  175. }
  176. |
  177. empty
  178. { C_df_ilb(l_false);
  179. #ifdef LINT
  180. end_if_stmt();
  181. #endif /* LINT */
  182. }
  183. ]
  184. ;
  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. }
  207. else {
  208. code_expr(expr, RVAL, TRUE, l_body, l_break);
  209. C_df_ilb(l_body);
  210. }
  211. #ifdef LINT
  212. start_while_stmt(expr);
  213. #endif /* LINT */
  214. }
  215. ')'
  216. statement
  217. {
  218. C_bra(l_continue);
  219. C_df_ilb(l_break);
  220. unstack_stmt();
  221. free_expression(expr);
  222. #ifdef LINT
  223. end_loop_body();
  224. end_loop_stmt();
  225. #endif /* LINT */
  226. }
  227. ;
  228. do_statement
  229. { struct expr *expr;
  230. label l_break = text_label();
  231. label l_continue = text_label();
  232. label l_body = text_label();
  233. }
  234. :
  235. DO
  236. { C_df_ilb(l_body);
  237. stack_stmt(l_break, l_continue);
  238. #ifdef LINT
  239. start_do_stmt();
  240. #endif /* LINT */
  241. }
  242. statement
  243. WHILE
  244. '('
  245. {
  246. #ifdef LINT
  247. end_loop_body();
  248. #endif /* LINT */
  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. for_statement
  278. { struct expr *e_init = 0, *e_test = 0, *e_incr = 0;
  279. label l_break = text_label();
  280. label l_continue = text_label();
  281. label l_body = text_label();
  282. label l_test = text_label();
  283. }
  284. :
  285. FOR
  286. { stack_stmt(l_break, l_continue);
  287. }
  288. '('
  289. [
  290. expression(&e_init)
  291. { code_expr(e_init, RVAL, FALSE, NO_LABEL, NO_LABEL);
  292. }
  293. ]?
  294. ';'
  295. { C_df_ilb(l_test);
  296. }
  297. [
  298. expression(&e_test)
  299. {
  300. opnd2test(&e_test, FOR);
  301. if (is_cp_cst(e_test)) {
  302. if (e_test->VL_VALUE == (arith)0) {
  303. C_bra(l_break);
  304. }
  305. }
  306. else {
  307. code_expr(e_test, RVAL, TRUE, l_body, l_break);
  308. C_df_ilb(l_body);
  309. }
  310. }
  311. ]?
  312. ';'
  313. expression(&e_incr)?
  314. ')'
  315. {
  316. #ifdef LINT
  317. start_for_stmt(e_test);
  318. #endif /* LINT */
  319. }
  320. statement
  321. {
  322. #ifdef LINT
  323. end_loop_body();
  324. #endif /* LINT */
  325. C_df_ilb(l_continue);
  326. if (e_incr)
  327. code_expr(e_incr, RVAL, FALSE,
  328. NO_LABEL, NO_LABEL);
  329. C_bra(l_test);
  330. C_df_ilb(l_break);
  331. unstack_stmt();
  332. free_expression(e_init);
  333. free_expression(e_test);
  334. free_expression(e_incr);
  335. #ifdef LINT
  336. end_loop_stmt();
  337. #endif /* LINT */
  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(is_cp_cst(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);
  403. #endif /* LINT */
  404. do_return_expr(expr);
  405. free_expression(expr);
  406. #ifdef LINT
  407. lint_return_stmt(VALRETURNED);
  408. #endif /* LINT */
  409. }
  410. |
  411. empty
  412. {
  413. do_return();
  414. #ifdef LINT
  415. lint_return_stmt(NOVALRETURNED);
  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. #ifdef DBSYMTAB
  439. static int brc_level = 1;
  440. int decl_seen = brc_level == 1;
  441. #endif /* DBSYMTAB */
  442. }
  443. :
  444. '{'
  445. {
  446. stack_level();
  447. }
  448. [%while ((DOT != IDENTIFIER && AHEAD != ':') ||
  449. (DOT == IDENTIFIER && AHEAD == IDENTIFIER))
  450. /* >>> conflict on TYPE_IDENTIFIER, IDENTIFIER */
  451. declaration
  452. {
  453. #ifdef DBSYMTAB
  454. decl_seen++;
  455. #endif /* DBSYMTAB */
  456. }
  457. ]*
  458. {
  459. #ifdef DBSYMTAB
  460. ++brc_level;
  461. if (options['g'] && decl_seen) {
  462. C_ms_std((char *) 0, N_LBRAC, brc_level);
  463. }
  464. #endif /* DBSYMTAB */
  465. }
  466. [%persistent
  467. statement
  468. ]*
  469. '}'
  470. {
  471. unstack_level();
  472. #ifdef DBSYMTAB
  473. if (options['g'] && decl_seen) {
  474. C_ms_std((char *) 0, N_RBRAC, brc_level);
  475. }
  476. brc_level--;
  477. #endif /* DBSYMTAB */
  478. }
  479. ;
  480. asm_statement
  481. { char *asm_bts;
  482. int asm_len;
  483. }
  484. :
  485. ASM
  486. '('
  487. STRING
  488. { asm_bts = dot.tk_bts;
  489. asm_len = dot.tk_len;
  490. }
  491. ')'
  492. ';'
  493. { code_asm(asm_bts, asm_len);
  494. }
  495. ;