123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520 |
- /*
- * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
- * See the copyright notice in the ACK home directory, in the file "Copyright".
- */
- /* $Id$ */
- /* STATEMENT SYNTAX PARSER */
- {
- #include "lint.h"
- #ifndef LINT
- #include <em.h>
- #else
- #include "l_em.h"
- #include "l_lint.h"
- #endif /* LINT */
- #include "debug.h"
- #include "botch_free.h"
- #include "dbsymtab.h"
- #include "arith.h"
- #include "LLlex.h"
- #include "type.h"
- #include "idf.h"
- #include "label.h"
- #include "expr.h"
- #include "code.h"
- #include "stack.h"
- #include "def.h"
- #ifdef DBSYMTAB
- #include <stb.h>
- #endif /* DBSYMTAB */
- extern int level;
- extern char options[];
- }
- /* Each statement construction is stacked in order to trace a ???
- statement to such a construction. Example: a case statement should
- be recognized as a piece of the most enclosing switch statement.
- */
- /* 9 */
- statement
- {
- #ifdef LINT
- lint_statement();
- #endif /* LINT */
- }
- :
- %if (AHEAD != ':')
- expression_statement
- |
- label ':' statement
- |
- compound_statement
- |
- if_statement
- |
- while_statement
- |
- do_statement
- |
- for_statement
- |
- switch_statement
- |
- case_statement
- |
- default_statement
- |
- BREAK
- {
- code_break();
- #ifdef LINT
- lint_break_stmt();
- #endif /* LINT */
- }
- ';'
- |
- CONTINUE
- {
- code_continue();
- #ifdef LINT
- lint_continue_stmt();
- #endif /* LINT */
- }
- ';'
- |
- return_statement
- |
- jump
- |
- ';'
- |
- asm_statement
- ;
- expression_statement
- { struct expr *expr;
- }
- :
- expression(&expr)
- ';'
- {
- #ifdef DEBUG
- print_expr("expression_statement", expr);
- #endif /* DEBUG */
- code_expr(expr, RVAL, FALSE, NO_LABEL, NO_LABEL);
- free_expression(expr);
- }
- ;
- label
- { struct idf *idf;
- }
- :
- identifier(&idf)
- {
- /* This allows the following absurd case:
- typedef int grz;
- main() {
- grz: printf("A labelled statement\n");
- }
- */
- #ifdef LINT
- lint_label();
- #endif /* LINT */
- define_label(idf);
- C_df_ilb((label)idf->id_def->df_address);
- }
- ;
- if_statement
- {
- struct expr *expr;
- label l_true = text_label();
- label l_false = text_label();
- label l_end = text_label();
- }
- :
- IF
- '('
- expression(&expr)
- {
- opnd2test(&expr, IF);
- if (is_cp_cst(expr)) {
- /* The comparison has been optimized
- to a 0 or 1.
- */
- if (expr->VL_VALUE == (arith)0) {
- C_bra(l_false);
- }
- /* else fall through */
- #ifdef LINT
- start_if_part(1);
- #endif /* LINT */
- }
- else {
- code_expr(expr, RVAL, TRUE, l_true, l_false);
- C_df_ilb(l_true);
- #ifdef LINT
- start_if_part(0);
- #endif /* LINT */
- }
- free_expression(expr);
- }
- ')'
- statement
- [%prefer
- ELSE
- {
- #ifdef LINT
- start_else_part();
- #endif /* LINT */
- C_bra(l_end);
- C_df_ilb(l_false);
- }
- statement
- { C_df_ilb(l_end);
- #ifdef LINT
- end_if_else_stmt();
- #endif /* LINT */
- }
- |
- empty
- { C_df_ilb(l_false);
- #ifdef LINT
- end_if_stmt();
- #endif /* LINT */
- }
- ]
- ;
- while_statement
- {
- struct expr *expr;
- label l_break = text_label();
- label l_continue = text_label();
- label l_body = text_label();
- }
- :
- WHILE
- {
- stack_stmt(l_break, l_continue);
- C_df_ilb(l_continue);
- }
- '('
- expression(&expr)
- {
- opnd2test(&expr, WHILE);
- if (is_cp_cst(expr)) {
- if (expr->VL_VALUE == (arith)0) {
- C_bra(l_break);
- }
- }
- else {
- code_expr(expr, RVAL, TRUE, l_body, l_break);
- C_df_ilb(l_body);
- }
- #ifdef LINT
- start_while_stmt(expr);
- #endif /* LINT */
- }
- ')'
- statement
- {
- C_bra(l_continue);
- C_df_ilb(l_break);
- unstack_stmt();
- free_expression(expr);
- #ifdef LINT
- end_loop_body();
- end_loop_stmt();
- #endif /* LINT */
- }
- ;
- do_statement
- { struct expr *expr;
- label l_break = text_label();
- label l_continue = text_label();
- label l_body = text_label();
- }
- :
- DO
- { C_df_ilb(l_body);
- stack_stmt(l_break, l_continue);
- #ifdef LINT
- start_do_stmt();
- #endif /* LINT */
- }
- statement
- WHILE
- '('
- {
- #ifdef LINT
- end_loop_body();
- #endif /* LINT */
- C_df_ilb(l_continue);
- }
- expression(&expr)
- {
- opnd2test(&expr, WHILE);
- if (is_cp_cst(expr)) {
- if (expr->VL_VALUE == (arith)1) {
- C_bra(l_body);
- }
- #ifdef LINT
- end_do_stmt(1, expr->VL_VALUE != (arith)0);
- #endif /* LINT */
- }
- else {
- code_expr(expr, RVAL, TRUE, l_body, l_break);
- #ifdef LINT
- end_do_stmt(0, 0);
- #endif /* LINT */
- }
- C_df_ilb(l_break);
- }
- ')'
- ';'
- {
- unstack_stmt();
- free_expression(expr);
- }
- ;
- for_statement
- { struct expr *e_init = 0, *e_test = 0, *e_incr = 0;
- label l_break = text_label();
- label l_continue = text_label();
- label l_body = text_label();
- label l_test = text_label();
- }
- :
- FOR
- { stack_stmt(l_break, l_continue);
- }
- '('
- [
- expression(&e_init)
- { code_expr(e_init, RVAL, FALSE, NO_LABEL, NO_LABEL);
- }
- ]?
- ';'
- { C_df_ilb(l_test);
- }
- [
- expression(&e_test)
- {
- opnd2test(&e_test, FOR);
- if (is_cp_cst(e_test)) {
- if (e_test->VL_VALUE == (arith)0) {
- C_bra(l_break);
- }
- }
- else {
- code_expr(e_test, RVAL, TRUE, l_body, l_break);
- C_df_ilb(l_body);
- }
- }
- ]?
- ';'
- expression(&e_incr)?
- ')'
- {
- #ifdef LINT
- start_for_stmt(e_test);
- #endif /* LINT */
- }
- statement
- {
- #ifdef LINT
- end_loop_body();
- #endif /* LINT */
- C_df_ilb(l_continue);
- if (e_incr)
- code_expr(e_incr, RVAL, FALSE,
- NO_LABEL, NO_LABEL);
- C_bra(l_test);
- C_df_ilb(l_break);
- unstack_stmt();
- free_expression(e_init);
- free_expression(e_test);
- free_expression(e_incr);
- #ifdef LINT
- end_loop_stmt();
- #endif /* LINT */
- }
- ;
- switch_statement
- {
- struct expr *expr;
- }
- :
- SWITCH
- '('
- expression(&expr)
- {
- code_startswitch(&expr);
- #ifdef LINT
- start_switch_part(is_cp_cst(expr));
- #endif /* LINT */
- }
- ')'
- statement
- {
- #ifdef LINT
- end_switch_stmt();
- #endif /* LINT */
- code_endswitch();
- free_expression(expr);
- }
- ;
- case_statement
- {
- struct expr *expr;
- }
- :
- CASE
- constant_expression(&expr)
- {
- #ifdef LINT
- lint_case_stmt(0);
- #endif /* LINT */
- code_case(expr);
- free_expression(expr);
- }
- ':'
- statement
- ;
- default_statement
- :
- DEFAULT
- {
- #ifdef LINT
- lint_case_stmt(1);
- #endif /* LINT */
- code_default();
- }
- ':'
- statement
- ;
- return_statement
- { struct expr *expr = 0;
- }
- :
- RETURN
- [
- expression(&expr)
- {
- #ifdef LINT
- lint_ret_conv(expr);
- #endif /* LINT */
- do_return_expr(expr);
- free_expression(expr);
- #ifdef LINT
- lint_return_stmt(VALRETURNED);
- #endif /* LINT */
- }
- |
- empty
- {
- do_return();
- #ifdef LINT
- lint_return_stmt(NOVALRETURNED);
- #endif /* LINT */
- }
- ]
- ';'
- ;
- jump
- { struct idf *idf;
- }
- :
- GOTO
- identifier(&idf)
- ';'
- {
- apply_label(idf);
- C_bra((label)idf->id_def->df_address);
- #ifdef LINT
- lint_jump_stmt(idf);
- #endif /* LINT */
- }
- ;
- compound_statement
- {
- #ifdef DBSYMTAB
- static int brc_level = 1;
- int decl_seen = brc_level == 1;
- #endif /* DBSYMTAB */
- }
- :
- '{'
- {
- stack_level();
- }
- [%while ((DOT != IDENTIFIER && AHEAD != ':') ||
- (DOT == IDENTIFIER && AHEAD == IDENTIFIER))
- /* >>> conflict on TYPE_IDENTIFIER, IDENTIFIER */
- declaration
- {
- #ifdef DBSYMTAB
- decl_seen++;
- #endif /* DBSYMTAB */
- }
- ]*
- {
- #ifdef DBSYMTAB
- ++brc_level;
- if (options['g'] && decl_seen) {
- C_ms_std((char *) 0, N_LBRAC, brc_level);
- }
- #endif /* DBSYMTAB */
- }
- [%persistent
- statement
- ]*
- '}'
- {
- unstack_level();
- #ifdef DBSYMTAB
- if (options['g'] && decl_seen) {
- C_ms_std((char *) 0, N_RBRAC, brc_level);
- }
- brc_level--;
- #endif /* DBSYMTAB */
- }
- ;
- asm_statement
- { char *asm_bts;
- int asm_len;
- }
- :
- ASM
- '('
- STRING
- { asm_bts = dot.tk_bts;
- asm_len = dot.tk_len;
- }
- ')'
- ';'
- { code_asm(asm_bts, asm_len);
- }
- ;
|