statement.g 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. /* $Header$ */
  2. /* STATEMENT SYNTAX PARSER */
  3. {
  4. #include <em.h>
  5. #include "debug.h"
  6. #include "botch_free.h"
  7. #include "arith.h"
  8. #include "LLlex.h"
  9. #include "type.h"
  10. #include "idf.h"
  11. #include "label.h"
  12. #include "expr.h"
  13. #include "code.h"
  14. #include "stack.h"
  15. #include "def.h"
  16. extern int level;
  17. }
  18. /* Each statement construction is stacked in order to trace a
  19. statement to such a construction. Example: a case statement should
  20. be recognized as a piece of the most enclosing switch statement.
  21. */
  22. /* 9 */
  23. statement
  24. :
  25. [%if (AHEAD != ':')
  26. expression_statement
  27. |
  28. label ':' statement
  29. |
  30. compound_statement((arith *)0)
  31. |
  32. if_statement
  33. |
  34. while_statement
  35. |
  36. do_statement
  37. |
  38. for_statement
  39. |
  40. switch_statement
  41. |
  42. case_statement
  43. |
  44. default_statement
  45. |
  46. break_statement
  47. |
  48. continue_statement
  49. |
  50. return_statement
  51. |
  52. jump
  53. |
  54. ';'
  55. |
  56. asm_statement
  57. ]
  58. ;
  59. expression_statement
  60. { struct expr *expr;
  61. }
  62. :
  63. expression(&expr)
  64. ';'
  65. {
  66. #ifdef DEBUG
  67. print_expr("Full expression", expr);
  68. #endif DEBUG
  69. code_expr(expr, RVAL, FALSE, NO_LABEL, NO_LABEL);
  70. free_expression(expr);
  71. }
  72. ;
  73. label
  74. { struct idf *idf;
  75. }
  76. :
  77. identifier(&idf)
  78. {
  79. /* This allows the following absurd case:
  80. typedef int grz;
  81. main() {
  82. grz: printf("A labelled statement\n");
  83. }
  84. */
  85. define_label(idf);
  86. C_df_ilb((label)idf->id_def->df_address);
  87. }
  88. ;
  89. if_statement
  90. {
  91. struct expr *expr;
  92. label l_true = text_label();
  93. label l_false = text_label();
  94. label l_end = text_label();
  95. }
  96. :
  97. IF
  98. '('
  99. expression(&expr)
  100. {
  101. opnd2test(&expr, IF);
  102. if (is_cp_cst(expr)) {
  103. /* The comparison has been optimized
  104. to a 0 or 1.
  105. */
  106. if (expr->VL_VALUE == (arith)0) {
  107. C_bra(l_false);
  108. }
  109. /* else fall through */
  110. }
  111. else {
  112. code_expr(expr, RVAL, TRUE, l_true, l_false);
  113. C_df_ilb(l_true);
  114. }
  115. free_expression(expr);
  116. }
  117. ')'
  118. statement
  119. [%prefer
  120. ELSE
  121. {
  122. C_bra(l_end);
  123. C_df_ilb(l_false);
  124. }
  125. statement
  126. { C_df_ilb(l_end);
  127. }
  128. |
  129. empty
  130. { C_df_ilb(l_false);
  131. }
  132. ]
  133. ;
  134. while_statement
  135. {
  136. struct expr *expr;
  137. label l_break = text_label();
  138. label l_continue = text_label();
  139. label l_body = text_label();
  140. }
  141. :
  142. WHILE
  143. {
  144. stack_stmt(l_break, l_continue);
  145. C_df_ilb(l_continue);
  146. }
  147. '('
  148. expression(&expr)
  149. {
  150. opnd2test(&expr, WHILE);
  151. if (is_cp_cst(expr)) {
  152. if (expr->VL_VALUE == (arith)0) {
  153. C_bra(l_break);
  154. }
  155. }
  156. else {
  157. code_expr(expr, RVAL, TRUE, l_body, l_break);
  158. C_df_ilb(l_body);
  159. }
  160. }
  161. ')'
  162. statement
  163. {
  164. C_bra(l_continue);
  165. C_df_ilb(l_break);
  166. unstack_stmt();
  167. free_expression(expr);
  168. }
  169. ;
  170. do_statement
  171. { struct expr *expr;
  172. label l_break = text_label();
  173. label l_continue = text_label();
  174. label l_body = text_label();
  175. }
  176. :
  177. DO
  178. { C_df_ilb(l_body);
  179. stack_stmt(l_break, l_continue);
  180. }
  181. statement
  182. WHILE
  183. '('
  184. { C_df_ilb(l_continue);
  185. }
  186. expression(&expr)
  187. {
  188. opnd2test(&expr, WHILE);
  189. if (is_cp_cst(expr)) {
  190. if (expr->VL_VALUE == (arith)1) {
  191. C_bra(l_body);
  192. }
  193. }
  194. else {
  195. code_expr(expr, RVAL, TRUE, l_body, l_break);
  196. }
  197. C_df_ilb(l_break);
  198. }
  199. ')'
  200. ';'
  201. {
  202. unstack_stmt();
  203. free_expression(expr);
  204. }
  205. ;
  206. for_statement
  207. { struct expr *e_init = 0, *e_test = 0, *e_incr = 0;
  208. label l_break = text_label();
  209. label l_continue = text_label();
  210. label l_body = text_label();
  211. label l_test = text_label();
  212. }
  213. :
  214. FOR
  215. { stack_stmt(l_break, l_continue);
  216. }
  217. '('
  218. [
  219. expression(&e_init)
  220. { code_expr(e_init, RVAL, FALSE, NO_LABEL, NO_LABEL);
  221. }
  222. ]?
  223. ';'
  224. { C_df_ilb(l_test);
  225. }
  226. [
  227. expression(&e_test)
  228. {
  229. opnd2test(&e_test, FOR);
  230. if (is_cp_cst(e_test)) {
  231. if (e_test->VL_VALUE == (arith)0) {
  232. C_bra(l_break);
  233. }
  234. }
  235. else {
  236. code_expr(e_test, RVAL, TRUE, l_body, l_break);
  237. C_df_ilb(l_body);
  238. }
  239. }
  240. ]?
  241. ';'
  242. expression(&e_incr)?
  243. ')'
  244. statement
  245. {
  246. C_df_ilb(l_continue);
  247. if (e_incr)
  248. code_expr(e_incr, RVAL, FALSE,
  249. NO_LABEL, NO_LABEL);
  250. C_bra(l_test);
  251. C_df_ilb(l_break);
  252. unstack_stmt();
  253. free_expression(e_init);
  254. free_expression(e_test);
  255. free_expression(e_incr);
  256. }
  257. ;
  258. switch_statement
  259. {
  260. struct expr *expr;
  261. }
  262. :
  263. SWITCH
  264. '('
  265. expression(&expr)
  266. {
  267. code_startswitch(&expr);
  268. }
  269. ')'
  270. statement
  271. {
  272. code_endswitch();
  273. free_expression(expr);
  274. }
  275. ;
  276. case_statement
  277. {
  278. struct expr *expr;
  279. }
  280. :
  281. CASE
  282. constant_expression(&expr)
  283. {
  284. code_case(expr);
  285. free_expression(expr);
  286. }
  287. ':'
  288. statement
  289. ;
  290. default_statement
  291. :
  292. DEFAULT
  293. {
  294. code_default();
  295. }
  296. ':'
  297. statement
  298. ;
  299. break_statement
  300. :
  301. BREAK
  302. {code_break();}
  303. ';'
  304. ;
  305. continue_statement
  306. :
  307. CONTINUE
  308. {code_continue();}
  309. ';'
  310. ;
  311. return_statement
  312. { struct expr *expr = 0;
  313. }
  314. :
  315. RETURN
  316. [
  317. expression(&expr)
  318. {
  319. do_return_expr(expr);
  320. free_expression(expr);
  321. }
  322. |
  323. empty
  324. {
  325. do_return();
  326. }
  327. ]
  328. ';'
  329. ;
  330. jump
  331. { struct idf *idf;
  332. }
  333. :
  334. GOTO
  335. identifier(&idf)
  336. ';'
  337. {
  338. apply_label(idf);
  339. C_bra((label)idf->id_def->df_address);
  340. }
  341. ;
  342. compound_statement(arith *nbytes;):
  343. '{'
  344. {
  345. stack_level();
  346. }
  347. [%while (AHEAD != ':') /* >>> conflict on TYPE_IDENTIFIER */
  348. declaration
  349. ]*
  350. [%persistent
  351. statement
  352. ]*
  353. '}'
  354. {
  355. if (nbytes)
  356. *nbytes = (- local_level->sl_max_block);
  357. unstack_level();
  358. }
  359. ;
  360. asm_statement
  361. { char *asm_bts;
  362. int asm_len;
  363. }
  364. :
  365. ASM
  366. '('
  367. STRING
  368. { asm_bts = dot.tk_bts;
  369. asm_len = dot.tk_len;
  370. }
  371. ')'
  372. ';'
  373. { code_asm(asm_bts, asm_len);
  374. }
  375. ;