Tr_exp Tr_assignExp(Tr_exp lvalue, Tr_exp right){ debug("Tr_assignExp"); if(right!=NULL){ return Tr_Nx(T_Move(unEx(lvalue), unEx(right))); } else { return lvalue; } }
Tr_exp Tr_breakExp(){ //Get done label from the list, which should have been prepared before this function is called. Temp_label f = LL_peek(); T_stm st = T_Jump( T_Name(f), Temp_LabelList(f, NULL) ); return Tr_Nx(st); }
Tr_exp Tr_forExp(Tr_exp var, Tr_exp low, Tr_exp high, Tr_exp body){ debug("Tr_forExp"); T_stm st; T_exp v = unEx(var); Temp_label t = Temp_newlabel(); Temp_label f = LL_peek();//Get done label from the list, which should have been prepared before this function is called. Temp_label start = Temp_newlabel(); T_stm cond = T_Cjump(T_le, v, unEx(high), t, f); /* T_Move var <- low T_Label start T_CJump var <= high, t, f T_Label(t) body T_Move var + 1 T_Jump start T_Label(f) */ st = T_Seq(T_Move(v, unEx(low)), T_Seq(T_Label(start), T_Seq(cond, T_Seq(T_Label(t), T_Seq(unNx(body), T_Seq(T_Move(v, T_Binop(T_plus, v, T_Const(1))), T_Seq( T_Jump( T_Name(start), Temp_LabelList(start, NULL) ), T_Label(f) ) ) ) ) ) ) ); //Pop the label as it's no longer used LL_pop(); return Tr_Nx(st); }
Tr_exp Tr_whileExp(Tr_exp cond, Tr_exp body){ debug("Tr_whileExp"); T_stm st; struct Cx cx_cond = unCx(cond); Temp_label t = Temp_newlabel(); Temp_label f = LL_peek();//Get done label from the list, which should have been prepared before this function is called. Temp_label start = Temp_newlabel(); doPatch(cx_cond.trues, t); doPatch(cx_cond.falses, f); /* T_Label start cx_cond.stm (true->t, false->f) T_Label(t) body T_Jump start T_Label(f) */ st = T_Seq(T_Label(start), T_Seq(cx_cond.stm, T_Seq(T_Label(t), T_Seq(unNx(body), T_Seq( T_Jump( T_Name(start), Temp_LabelList(start, NULL) ), T_Label(f) ) ) ) ) ); //Pop the label as it's no longer used LL_pop(); return Tr_Nx(st); }
void print_frag(F_fragList fl, FILE * out) { if (!fl) { puts("fragList is NULL"); return; } while (fl) { F_frag f = fl->head; switch(f->kind) { case F_stringFrag: print(Tr_Ex(T_Name(f->u.string.label)), out); fprintf(out, "\n"); break; case F_procFrag: print(Tr_Nx(f->u.proc.body), out); break; default: assert(0 && "frag-kind is error"); } fl = fl->tail; } }
Tr_exp Tr_ifExp(Tr_exp cond, Tr_exp thenb, Tr_exp elseb){ T_exp ex; T_stm st; Tr_exp ret; struct Cx cx_cond = unCx(cond); Temp_label t = Temp_newlabel(); Temp_label f = Temp_newlabel(); Temp_label join = Temp_newlabel(); doPatch(cx_cond.trues, t); doPatch(cx_cond.falses, f); if(elseb==NULL){ //case 1: if-then debug("Tr_ifExp: if-then"); st = T_Seq(cx_cond.stm, T_Seq(T_Label(t), T_Seq(unNx(thenb), T_Label(f) ) ) ); ret = Tr_Nx(st); } else { //case 2: if-then-else if(thenb->kind==Tr_nx && elseb->kind==Tr_nx){ debug("Tr_ifExp: if-then-else (2 nx)"); //special case: two statements st = T_Seq(cx_cond.stm, T_Seq(T_Label(t), T_Seq(unNx(thenb), T_Seq( T_Jump( T_Name(join), Temp_LabelList(join, NULL) ), T_Seq(T_Label(f), T_Seq(unNx(elseb), T_Label(join) ) ) ) ) ) ); ret = Tr_Nx(st); //TODO: special case - two cx. } else { debug("Tr_ifExp: if-then-else"); Temp_temp r = Temp_newtemp(); ex = T_Eseq(cx_cond.stm, T_Eseq(T_Label(t), T_Eseq(T_Move(T_Temp(r), unEx(thenb)), T_Eseq( T_Jump( T_Name(join), Temp_LabelList(join, NULL) ), T_Eseq(T_Label(f), T_Eseq(T_Move(T_Temp(r), unEx(elseb)), T_Eseq(T_Label(join), T_Temp(r) ) ) ) ) ) ) ); ret = Tr_Ex(ex); } } return ret; }