{
if(need_stack)
printf("\tleave\n");
- printf("\tret\n");
+ printf("\tret\n\n\n");
}
void move(char *src, char *dst)
{
+ if(src == (char*) NULL) return;
if(strcmp(src,dst) != 0) {
printf("\tmovq %%%s, %%%s\n", src, dst);
}
printf("\tsub $%d, %%%s\n", KIDKID_VAL(0,1), KID_REG(1));
}
+void make_call(struct treenode *bnode)
+{
+ int j;
+ /* vars pushen */
+ for(j = bnode->paramges; j < bnode->paramges + bnode->vars; j++) {
+ printf("\tpushq %%%s\n", param_reg(j));
+ }
+ printf("\tcall %s\n", bnode->name);
+
+ /* vars poppen */
+ for(j = bnode->paramges + bnode->vars - 1; j >= bnode->paramges; j--) {
+ printf("\tpopq %%%s\n", param_reg(j));
+ }
+
+ /* params poppen */
+ for(j = bnode->soffset - 1; j >= 0; j--) {
+ printf("\tpopq %%%s\n", param_reg(j));
+ }
+}
+
+void prep_arg(struct treenode *bnode, int moveit)
+{
+ printf("\t//args-nr.: %%%s to %%%s (moveit: %i)\n", BN_REG, param_reg(bnode->soffset), moveit);
+ printf("\tpushq %%%s\n", param_reg(bnode->soffset));
+ if(moveit) { /* expr */
+ move(BN_REG, param_reg(bnode->soffset));
+ } else { /* just O_ID */
+ KIDREG2PARM(0);
+ move(KID_REG(0), param_reg(bnode->soffset));
+ }
+}
+
%}
%start begin
-%term O_RET=1 O_NULL=2 O_SUB=3 O_MUL=4 O_OR=5 O_LESS=6 O_EQ=7 O_ID=8 O_ADD=9 O_NUM=10 O_FIELD=11 O_MTWO=12 O_MFOUR=13 O_MEIGHT=14 O_MONE=15 O_ASSIGN=16 O_IF=17 O_BOOL=18 O_CALL=19
+%term O_RET=1 O_NULL=2 O_SUB=3 O_MUL=4 O_OR=5 O_LESS=6 O_EQ=7 O_ID=8 O_ADD=9 O_NUM=10 O_FIELD=11 O_MTWO=12 O_MFOUR=13 O_MEIGHT=14 O_MONE=15 O_ASSIGN=16 O_IF=17 O_BOOL=18 O_CALL=19 O_ARG=20 O_NOTHING=21
%%
begin: ret # 0 # printf("\n");
begin: assign # 0 #
begin: ifstat # 0 #
+begin: args # 0 #
assign: O_ASSIGN(expr, O_ID) # 1 # KIDREG2PARM(1); printf("\tmovq %%%s, %%%s\n", BN_REG, KID_REG(1));
expr: imm # 1 # moveimm(BN_VAL, BN_REG);
expr: O_BOOL(expr) # 0 #
-expr: O_CALL # 0 # printf("\tcall %s\n", bnode->name);
-
+expr: O_CALL(expr,expr) # 0 # make_call(bnode);
+expr: O_ARG(expr,expr) # 1 # prep_arg(bnode, 1);
+expr: O_ARG(O_ID,expr) # 1 # prep_arg(bnode, 0);
+expr: O_NOTHING # 0 #
expr: O_SUB(expr,expr) # 2 # gen_e_eno(bnode, "subq");
expr: O_SUB(expr,O_FIELD(expr)) # 2 # gen_e_field(bnode, "subq");
* sin: symboltabelle fuer statement ("eingabe")
* sout: symboltabelle die aus einem statement wieder rauskommt ("ausgabe")
*/
-@autoinh s gparamges
+@autoinh s gparamges vars_in
@autosyn node imm call
@attributes { char *name; } IDENT
@attributes { struct symbol *f; } Program Structdef;
@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 vars_in; int paramcount; struct treenode *node; } Exprs
@attributes { struct symbol *s; int gparamges; int lblcnt_in; int lblcnt_out; int vars_in; int vars_out; int call; } Statseq
@attributes { struct symbol *s; int gparamges; int lblcnt_in; int lblcnt_out; int reallblcnt; int vars_in; int vars_out; int call; } Elsestat
-@attributes { struct symbol *s; int gparamges; struct treenode *node; short imm; int call; } Expr Minusterm Multerm Orterm Feld Term
-@attributes { struct symbol *s; int gparamges; struct treenode *node; } Lexpr
+@attributes { struct symbol *s; int gparamges; int vars_in; struct treenode *node; short imm; int call; } Expr Minusterm Multerm Orterm Feld Term
+@attributes { struct symbol *s; int gparamges; int vars_in; struct treenode *node; } Lexpr
@attributes { struct symbol *sin; int gparamges; struct symbol *sout; struct treenode *node; int vars_in; int vars_out; int lblcnt_in; int lblcnt_out; int call; } Statement
@traversal @postorder c
| IDENT '(' Exprs ')'
@{
- @i @Term.node@ = new_call(@IDENT.name@, TREENULL, TREENULL);
+ /* TODO: soffset sollte eigentlich anzahl des paramcounts in Exprs + vars die existieren sein */
+ @i {
+ @Term.node@ = new_call(@IDENT.name@, new_arg(new_nothing(), new_nothing(), 0) /*this*/,
+ @Exprs.node@, @Term.gparamges@, @Term.vars_in@);
+ @Term.node@->soffset = @Exprs.paramcount@;
+ }
@i @Term.imm@ = 0;
@i @Term.call@ = 1;
+ @reg @Exprs.node@->reg = @Term.node@->reg;
@}
| Term '.' IDENT '(' Exprs ')'
Exprs:
Expr ',' Exprs
+ @{
+ @i @Exprs.0.node@ = new_arg(@Expr.0.node@, @Exprs.1.node@, @Exprs.1.paramcount@);
+ @i @Exprs.0.paramcount@ = @Exprs.1.paramcount@ + 1;
+ @reg @Expr.node@->reg = @Exprs.1.node@->reg = @Exprs.0.node@->reg;
+ @}
| Expr
+ @{
+ @i @Exprs.0.node@ = new_arg(@Expr.0.node@, new_nothing(), 1);
+ @i @Exprs.paramcount@ = 2;
+ @reg @Expr.node@->reg = @Exprs.0.node@->reg;
+ @}
|
+ @{
+ @i @Exprs.0.node@ = new_nothing();
+ @i @Exprs.paramcount@ = 1;
+ @}
;
%%
new->parent = TREENULL;
new->label = NULL;
new->name = new->reg = (char *)NULL;
- new->val = 0;
+ new->val = new->paramges = new->vars = 0;
new->param_index = -1;
return new;
}
+struct treenode *new_nothing(void)
+{
+ return new_node(O_NOTHING, TREENULL, TREENULL);
+}
+
struct treenode *new_node(int op, struct treenode *l, struct treenode *r)
{
struct treenode *new = TREENULL;
return new;
}
-struct treenode *new_call(char *name, struct treenode *l, struct treenode *r)
+struct treenode *new_arg(struct treenode *l, struct treenode *r, int soffset)
+{
+ struct treenode *new = new_node(O_ARG, l, r);
+
+#ifdef DDTREE
+ fprintf(stderr, "new_arg: %i (soffset)\n", soffset);
+#endif
+ new->soffset = soffset;
+ new->name = (char *) malloc(10);
+ sprintf(new->name, "%i", soffset);
+ return new;
+}
+
+struct treenode *new_call(char *name, struct treenode *l, struct treenode *r, int paramges, int vars)
{
struct treenode *new = new_node(O_CALL, l, r);
fprintf(stderr, "new_call\n");
#endif
new->name = name;
+ new->paramges = paramges;
+ new->vars = vars;
return new;
}
O_RET=1, O_NULL, O_SUB, O_MUL,
O_OR=5, O_LESS, O_EQ, O_ID, O_ADD,
O_NUM=10, O_FIELD, O_MTWO, O_MFOUR, O_MEIGHT,
- O_MONE=15, O_ASSIGN, O_IF, O_BOOL, O_CALL
+ O_MONE=15, O_ASSIGN, O_IF, O_BOOL, O_CALL,
+ O_ARG=20, O_NOTHING
};
static char *o_names[] = {
"", "O_RET", "O_NULL", "O_SUB", "O_MUL",
"O_OR", "O_LESS", "O_EQ", "O_ID", "O_ADD",
"O_NUM", "O_FIELD", "O_MTWO", "O_MFOUR", "O_MEIGHT",
- "O_MONE", "O_ASSIGN", "O_IF", "O_BOOL", "O_CALL"
+ "O_MONE", "O_ASSIGN", "O_IF", "O_BOOL", "O_CALL",
+ "O_ARG", "O_NOTHING"
};
struct treenode {
struct treenode *parent;
int param_index;
int soffset;
+ int paramges;
+ int vars;
};
typedef struct treenode *treenodep;
struct treenode *new_number(long val);
struct treenode *new_param(int op, char *name, struct treenode *l, struct treenode *r, int param_index);
struct treenode *new_field(char *name, struct treenode *l, struct treenode *r, int soffset);
-struct treenode *new_call(char *name, struct treenode *l, struct treenode *r);
+struct treenode *new_call(char *name, struct treenode *l, struct treenode *r, int paramges, int vars);
+struct treenode *new_arg(struct treenode *l, struct treenode *r, int soffset);
+struct treenode *new_nothing(void);
void write_tree(struct treenode *node, int ident);
#endif