void printExpression(FILE* file, IR_myExp exp, int spaceNum) { newline(file), indentSpace(file, spaceNum); if (exp == NULL) return printNull(file); switch (exp->kind) { case IR_BinOperation: return printBinOperation(file, exp, spaceNum); case IR_Call: return printCall(file, exp, spaceNum); case IR_Const: return printConst(file, exp, spaceNum); case IR_ESeq: return printESeq(file, exp, spaceNum); case IR_Mem: return printMem(file, exp, spaceNum); case IR_Name: return printName(file, exp, spaceNum); case IR_Temp: return printTemp(file, exp, spaceNum); default: assert (false); } }
void printRK(Function *f, lu08 reg) { if(reg<CG_REG_COUNT) { printf("%d\t", reg); } else { printf("%d", reg); printConst(f, reg - CG_REG_COUNT); } }
int main (void) { printf("%d\n", 01777);// eight digit hex printf("%d\n", 0x1777); // 16 digit hex typeLength(); outputControllSymbol(); printf("%d\n", TOP); printf("%d\n", SINT_MAX); char* pDest; StrCpy(pDest, "hello world!"); printf("%s\n", pDest); printConst(); printf("%d\n", isNegative(200)); printf("%c\n", (char)300324); variableScope(); pointer(); printStruct(); return 0; }
void printIntruction(Function *f, Instruction* i) { switch(i->i.unpacked.opc) { case OP_MOVE:/* A B R(A) := R(B) */ printf("OP_MOVE "); break; case OP_LOADK:/* A Bx R(A) := Kst(Bx) */ printf("OP_LOADK "); break; case OP_LOADBOOL:/* A B C R(A) := (Bool)B; if (C) pc++ */ printf("OP_LOADBOOL "); break; case OP_LOADNIL:/* A B R(A) := ... := R(B) := nil */ printf("OP_LOADNIL "); break; case OP_GETUPVAL:/* A B R(A) := UpValue[B] */ printf("OP_GETUPVAL "); break; case OP_GETGLOBAL:/* A Bx R(A) := Gbl[Kst(Bx)] */ printf("OP_GETGLOBAL "); break; case OP_GETTABLE:/* A B C R(A) := R(B)[RK(C)] */ printf("OP_GETTABLE "); break; case OP_SETGLOBAL:/* A Bx Gbl[Kst(Bx)] := R(A) */ printf("OP_SETGLOBAL "); break; case OP_SETUPVAL:/* A B UpValue[B] := R(A) */ printf("OP_SETUPVAL "); break; case OP_SETTABLE:/* A B C R(A)[RK(B)] := RK(C) */ printf("OP_SETTABLE "); break; case OP_NEWTABLE:/* A B C R(A) := {} (size = B,C) */ printf("OP_NEWTABLE "); break; case OP_SELF:/* A B C R(A+1) := R(B); R(A) := R(B)[RK(C)] */ printf("OP_SELF "); break; case OP_ADD:/* A B C R(A) := RK(B) + RK(C) */ printf("OP_ADD\t"); break; case OP_SUB:/* A B C R(A) := RK(B) - RK(C) */ printf("OP_SUB\t"); break; case OP_MUL:/* A B C R(A) := RK(B) * RK(C) */ printf("OP_MUL\t"); break; case OP_DIV:/* A B C R(A) := RK(B) / RK(C) */ printf("OP_DIV\t"); break; case OP_MOD:/* A B C R(A) := RK(B) % RK(C) */ printf("OP_MOD\t"); break; case OP_POW:/* A B C R(A) := RK(B) ^ RK(C) */ printf("OP_POW\t"); break; case OP_UNM:/* A B R(A) := -R(B) */ printf("OP_UNM\t"); break; case OP_NOT:/* A B R(A) := not R(B) */ printf("OP_NOT\t"); break; case OP_LEN:/* A B R(A) := length of R(B) */ printf("OP_LEN\t"); break; case OP_CONCAT:/* A B C R(A) := R(B).. ... ..R(C) */ printf("OP_CONCAT "); break; case OP_JMP:/* sBx pc+=sBx */ printf("OP_JMP\t"); break; case OP_EQ:/* A B C if ((RK(B) == RK(C)) ~= A) then pc++ */ printf("OP_EQ\t"); break; case OP_LT:/* A B C if ((RK(B) < RK(C)) ~= A) then pc++ */ printf("OP_LT\t"); break; case OP_LE:/* A B C if ((RK(B) <= RK(C)) ~= A) then pc++ */ printf("OP_LE\t"); break; case OP_TEST:/* A C if not (R(A) <=> C) then pc++ */ printf("OP_TEST "); break; case OP_TESTSET:/* A B C if (R(B) <=> C) then R(A) := R(B) else pc++ */ printf("OP_TESTSET "); break; case OP_CALL:/* A B C R(A), ... ,R(A+C-2) := R(A)(R(A+1), ... ,R(A+B-1)) */ printf("OP_CALL "); break; case OP_TAILCALL:/* A B C return R(A)(R(A+1), ... ,R(A+B-1)) */ printf("OP_TAILCALL "); break; case OP_RETURN:/* A B return R(A), ... ,R(A+B-2) (see note) */ printf("OP_RETURN "); break; case OP_FORLOOP:/* A sBx R(A)+=R(A+2); if R(A) <?= R(A+1) then { pc+=sBx; R(A+3)=R(A) }*/ printf("OP_FORLOOP "); break; case OP_FORPREP:/* A sBx R(A)-=R(A+2); pc+=sBx */ printf("OP_FORPREP "); break; case OP_TFORLOOP:/* A C R(A+3), ... ,R(A+2+C) := R(A)(R(A+1), R(A+2)); if R(A+3) ~= nil then R(A+2)=R(A+3) else pc++ */ printf("OP_TFORLOOP "); break; case OP_SETLIST:/* A B C R(A)[(C-1)*FPF+i] := R(A+i), 1 <= i <= B */ printf("OP_SETLIST "); break; case OP_CLOSE:/* A close all variables in the stack up to (>=) R(A)*/ printf("OP_CLOSE "); break; case OP_CLOSURE:/* A Bx R(A) := closure(KPROTO[Bx], R(A), ... ,R(A+n)) */ printf("OP_CLOSURE "); break; case OP_VARARG:/* A B R(A), R(A+1), ..., R(A+B-1) = vararg */ printf("OP_VARARG "); break; } printf("\t"); switch(i->i.unpacked.opc) { case OP_MOVE:/* A B R(A) := R(B) */ case OP_UNM:/* A B R(A) := -R(B) */ case OP_NOT:/* A B R(A) := not R(B) */ case OP_LOADNIL:/* A B R(A) := ... := R(B) := nil */ case OP_GETUPVAL:/* A B R(A) := UpValue[B] */ case OP_SETUPVAL:/* A B UpValue[B] := R(A) */ case OP_LEN:/* A B R(A) := length of R(B) */ case OP_RETURN:/* A B return R(A), ... ,R(A+B-2) (see note) */ printf("%d\t%d\t", i->i.unpacked.a, i->i.unpacked.bx.l.b); break; case OP_LOADK:/* A Bx R(A) := Kst(Bx) */ case OP_GETGLOBAL:/* A Bx R(A) := Gbl[Kst(Bx)] */ case OP_SETGLOBAL:/* A Bx Gbl[Kst(Bx)] := R(A) */ printf("%d\t%d", i->i.unpacked.a, i->i.unpacked.bx.bx); printConst(f, i->i.unpacked.bx.bx); break; case OP_LOADBOOL:/* A B C R(A) := (Bool)B; if (C) pc++ */ case OP_NEWTABLE:/* A B C R(A) := {} (size = B,C) */ printf("%d\t%d\t%d", i->i.unpacked.a, i->i.unpacked.bx.l.b, i->i.unpacked.bx.l.c); break; case OP_GETTABLE:/* A B C R(A) := R(B)[RK(C)] */ break; case OP_SETTABLE:/* A B C R(A)[RK(B)] := RK(C) */ break; case OP_SELF:/* A B C R(A+1) := R(B); R(A) := R(B)[RK(C)] */ break; case OP_ADD:/* A B C R(A) := RK(B) + RK(C) */ case OP_SUB:/* A B C R(A) := RK(B) - RK(C) */ case OP_MUL:/* A B C R(A) := RK(B) * RK(C) */ case OP_DIV:/* A B C R(A) := RK(B) / RK(C) */ case OP_MOD:/* A B C R(A) := RK(B) % RK(C) */ case OP_POW:/* A B C R(A) := RK(B) ^ RK(C) */ case OP_EQ:/* A B C if ((RK(B) == RK(C)) ~= A) then pc++ */ case OP_LT:/* A B C if ((RK(B) < RK(C)) ~= A) then pc++ */ case OP_LE:/* A B C if ((RK(B) <= RK(C)) ~= A) then pc++ */ case OP_CALL:/* A B C R(A), ... ,R(A+C-2) := R(A)(R(A+1), ... ,R(A+B-1)) */ case OP_TEST:/* A C if not (R(A) <=> C) then pc++ */ case OP_TESTSET:/* A B C if (R(B) <=> C) then R(A) := R(B) else pc++ */ case OP_TAILCALL:/* A B C return R(A)(R(A+1), ... ,R(A+B-1)) */ printRK(f, i->i.unpacked.a); printRK(f, i->i.unpacked.bx.l.b); printRK(f, i->i.unpacked.bx.l.c); break; case OP_CONCAT:/* A B C R(A) := R(B).. ... ..R(C) */ break; case OP_JMP:/* sBx pc+=sBx */ printf("%d", i->i.unpacked.bx.bx); break; break; case OP_FORLOOP:/* A sBx R(A)+=R(A+2); if R(A) <?= R(A+1) then { pc+=sBx; R(A+3)=R(A) }*/ break; case OP_FORPREP:/* A sBx R(A)-=R(A+2); pc+=sBx */ break; case OP_TFORLOOP:/* A C R(A+3), ... ,R(A+2+C) := R(A)(R(A+1), R(A+2)); if R(A+3) ~= nil then R(A+2)=R(A+3) else pc++ */ break; case OP_SETLIST:/* A B C R(A)[(C-1)*FPF+i] := R(A+i), 1 <= i <= B */ break; case OP_CLOSE:/* A close all variables in the stack up to (>=) R(A)*/ break; case OP_CLOSURE:/* A Bx R(A) := closure(KPROTO[Bx], R(A), ... ,R(A+n)) */ break; case OP_VARARG:/* A B R(A), R(A+1), ..., R(A+B-1) = vararg */ break; } printf("\n"); }