codeb: erster ansatz fuer 'IF expr THEN statseq ELSE statseq END'
authorBernhard Urban <lewurm@gmail.com>
Sat, 29 May 2010 22:00:22 +0000 (00:00 +0200)
committerBernhard Urban <lewurm@gmail.com>
Sat, 29 May 2010 22:00:22 +0000 (00:00 +0200)
codeb/parser.y

index 427a7330f1f1b5a1c5978a730ae57c15f8192323..7d9af97417b0c5a81b5baa2296f4e3a7de7620b4 100644 (file)
@@ -46,7 +46,7 @@
 @attributes { struct symbol *f; int offsetcount; } FeldID
 @attributes { struct symbol *s; } Methoddef
 @attributes { struct symbol *s; int gparamges; } Exprs
 @attributes { struct symbol *f; int offsetcount; } FeldID
 @attributes { struct symbol *s; } Methoddef
 @attributes { struct symbol *s; int gparamges; } Exprs
-@attributes { struct symbol *s; int gparamges; int lblcnt_in; int lblcnt_out; } Statseq
+@attributes { struct symbol *s; int gparamges; int lblcnt_in; int lblcnt_out; } Statseq Elsestat
 @attributes { struct symbol *s; int gparamges; struct treenode *node; short imm; } Expr Minusterm Multerm Orterm Feld Term
 @attributes { struct symbol *s; int gparamges; struct treenode *node; } Lexpr
 @attributes { struct symbol *sin; int gparamges; struct symbol *sout; struct treenode *node; int vars; int lblcnt_in; int lblcnt_out; } Statement
 @attributes { struct symbol *s; int gparamges; struct treenode *node; short imm; } Expr Minusterm Multerm Orterm Feld Term
 @attributes { struct symbol *s; int gparamges; struct treenode *node; } Lexpr
 @attributes { struct symbol *sin; int gparamges; struct symbol *sout; struct treenode *node; int vars; int lblcnt_in; int lblcnt_out; } Statement
@@ -203,18 +203,28 @@ Statement:
                @gen @revorder(1) printf("%s_ifend_%d:\n", get_func_name(), @Statement.lblcnt_in@);
          @}
 
                @gen @revorder(1) printf("%s_ifend_%d:\n", get_func_name(), @Statement.lblcnt_in@);
          @}
 
-       | IF Expr THEN Statseq ELSE Statseq END
+       | IF Expr THEN Statseq Elsestat END
          @{
                statinout()
          @{
                statinout()
-               lblcountinout()
-               /* TODO */
                @i @Statseq.0.lblcnt_in@ =  @Statement.lblcnt_in@ + 1;
                @i @Statseq.0.lblcnt_in@ =  @Statement.lblcnt_in@ + 1;
-               @i @Statseq.1.lblcnt_in@ =  @Statement.lblcnt_in@ + 1;
+               @i @Elsestat.lblcnt_in@ =  @Statement.lblcnt_in@;
+               /* TODO: welchen wert lblcnt_out zuweisen? :/ */
+               @i @Statement.lblcnt_out@ = @Statseq.lblcnt_out@;
+
                xxputsin(@Expr.s@,)
                xxputsin(@Statseq.0.s@,)
                xxputsin(@Expr.s@,)
                xxputsin(@Statseq.0.s@,)
-               xxputsin(@Statseq.1.s@,)
-           @i @Statement.node@ = TREENULL;
+               xxputsin(@Elsestat.s@,)
+
+               @i @Statement.node@ = new_node(O_IF, @Expr.node@, TREENULL);
                @i @Statement.vars@ = 0;
                @i @Statement.vars@ = 0;
+
+               @reg @Statement.node@->reg = @Expr.node@->reg = next_reg((char *)NULL, @Expr.gparamges@);
+               @gen {
+                       write_tree(@Statement.node@, 0); burm_label(@Statement.node@); burm_reduce(@Statement.node@, 1);
+                       /* TODO: kann ich mir das test wirklich wegan and davor sparen? */
+                       printf("\ttest %s1, %%rax\n\tjz %s_ifelse_%d\n", "$", get_func_name(), @Statement.lblcnt_in@);
+               }
+               @gen @revorder(1) printf("%s_ifend_%d:\n", get_func_name(), @Statement.lblcnt_in@);
          @}
 
        | WHILE Expr DO Statseq END
          @}
 
        | WHILE Expr DO Statseq END
@@ -243,6 +253,15 @@ Statement:
          @}
        ;
 
          @}
        ;
 
+Elsestat:
+         ELSE Statseq
+         @{
+               @i @Statseq.lblcnt_in@ =  @Elsestat.lblcnt_in@ + 1;
+               @i @Elsestat.lblcnt_out@ =  @Statseq.lblcnt_out@;
+
+               @gen printf("\tjmp %s_ifend_%d\n%s_ifelse_%d:\n", get_func_name(), @Elsestat.lblcnt_in@, get_func_name(), @Elsestat.lblcnt_in@);
+         @}
+
 Lexpr:
          IDENT
          @{
 Lexpr:
          IDENT
          @{