Changed organisation of TOS. Now its 8-byte stack instead 4-byte before

This commit is contained in:
Alex Vanin 2014-01-26 10:39:22 +04:00
parent c70ee283ba
commit 486b73fcea
3 changed files with 39 additions and 18 deletions

View file

@ -47,87 +47,109 @@ int main(int argc, char** argv)
push_int(-1); push_int(-1);
ip++; break; ip++; break;
case DADD: case DADD:
//DO(DADD, "Add 2 doubles on TOS, push value back.", 1)
d1 = pop_double(); d1 = pop_double();
d2 = pop_double(); d2 = pop_double();
d1 += d2; d1 += d2;
push_double(d1); push_double(d1);
ip++; break; ip++; break;
case IADD: case IADD:
//DO(IADD, "Add 2 ints on TOS, push value back.", 1)
i1 = pop_int(); i1 = pop_int();
i2 = pop_int(); i2 = pop_int();
i1 += i2; i1 += i2;
push_int(i1); push_int(i1);
ip++; break; ip++; break;
case DSUB: case DSUB:
//DO(DSUB, "Subtract 2 doubles on TOS (lower from upper), push value back.", 1)
d1 = pop_double(); d1 = pop_double();
d2 = pop_double(); d2 = pop_double();
d1 -= d2; d1 -= d2;
push_double(d1); push_double(d1);
ip++; break; ip++; break;
case ISUB: case ISUB:
//DO(ISUB, "Subtract 2 ints on TOS (lower from upper), push value back.", 1)
i1 = pop_int(); i1 = pop_int();
i2 = pop_int(); i2 = pop_int();
i1 -= i2; i1 -= i2;
push_int(i1); push_int(i1);
ip++; break; ip++; break;
case DMUL: case DMUL:
//DO(DMUL, "Multiply 2 doubles on TOS, push value back.", 1)
d1 = pop_double(); d1 = pop_double();
d2 = pop_double(); d2 = pop_double();
d1 *= d2; d1 *= d2;
push_double(d1); push_double(d1);
ip++; break; ip++; break;
case IMUL: case IMUL:
//DO(IMUL, "Multiply 2 ints on TOS, push value back.", 1)
i1 = pop_int(); i1 = pop_int();
i2 = pop_int(); i2 = pop_int();
i1 *= i2; i1 *= i2;
push_int(i1); push_int(i1);
ip++; break; ip++; break;
case DDIV: case DDIV:
//DO(DDIV, "Divide 2 doubles on TOS (upper to lower), push value back.", 1)
d1 = pop_double(); d1 = pop_double();
d2 = pop_double(); d2 = pop_double();
d1 /= d2; d1 /= d2;
push_double(d1); push_double(d1);
ip++; break; ip++; break;
case IDIV: case IDIV:
//DO(IDIV, "Divide 2 ints on TOS (upper to lower), push value back.", 1)
i1 = pop_int(); i1 = pop_int();
i2 = pop_int(); i2 = pop_int();
i1 /= i2; i1 /= i2;
push_int(i1); push_int(i1);
ip++; break; ip++; break;
case IMOD: case IMOD:
//DO(IMOD, "Modulo operation on 2 ints on TOS (upper to lower), push value back.", 1)
i1 = pop_int(); i1 = pop_int();
i2 = pop_int(); i2 = pop_int();
i1 %= i2; i1 %= i2;
push_int(i1); push_int(i1);
ip++; break; ip++; break;
case DNEG: case DNEG:
//DO(DNEG, "Negate double on TOS.", 1)
d1 = pop_double(); d1 = pop_double();
d1 = -d1; d1 = -d1;
push_double(d1); push_double(d1);
ip++; break; ip++; break;
case INEG: case INEG:
//DO(INEG, "Negate int on TOS.", 1)
i1 = pop_int(); i1 = pop_int();
i1 = - i1; i1 = - i1;
push_int(i1); push_int(i1);
ip++; break; ip++; break;
case IAOR: case IAOR:
//DO(IAOR, "Arithmetic OR of 2 ints on TOS, push value back.", 1)
i1 = pop_int(); i1 = pop_int();
i2 = pop_int(); i2 = pop_int();
i1 = i1 | i2; i1 = i1 | i2;
push_int(i1); push_int(i1);
ip++;break;
case DPRINT:
printf("%f", pop_double());
ip++; break; ip++; break;
case IAAND:
//DO(IAAND, "Arithmetic AND of 2 ints on TOS, push value back.", 1)
ip++; break;
case IAXOR:
//DO(IAXOR, "Arithmetic XOR of 2 ints on TOS, push value back.", 1)
case IPRINT: case IPRINT:
printf("%d", pop_int()); //DO(IPRINT, "Pop and print integer TOS.", 1)
i1 = pop_int();
push_int(i1);
printf("%d", i1);
ip++; break;
case DPRINT:
//DO(DPRINT, "Pop and print double TOS.", 1)
d1 = pop_double();
push_double(d1);
printf("%f", d1);
ip++; break; ip++; break;
case STOP: case STOP:
//DO(STOP, "Stop execution.", 1)
exec_status = 0; exec_status = 0;
break; break;
} }
} }
getchar();
return 0; return 0;
} }

View file

@ -3,33 +3,32 @@
void push_int(int num) void push_int(int num)
{ {
TOS[tp++] = num; tos_num number;
number.num_i[1] = num;
TOS[tp++] = number.num_d;
} }
int pop_int() int pop_int()
{ {
int num = TOS[--tp]; tos_num number;
return num; number.num_d = TOS[--tp];
return number.num_i[1];
} }
void push_double(double num) void push_double(double num)
{ {
tos_num cont = { num }; TOS[tp++] = num;
push_int(cont.num_i[0]);
push_int(cont.num_i[1]);
} }
double pop_double() double pop_double()
{ {
tos_num cont; double num = TOS[--tp];
cont.num_i[1] = pop_int(); return num;
cont.num_i[0] = pop_int();
return cont.num_d;
} }
int initTOS() int initTOS()
{ {
TOS = (int*)calloc(1, 1500); TOS = (double*)calloc(1, 12000);
tp = 0; tp = 0;
return 0; return 0;
} }

View file

@ -7,7 +7,7 @@ typedef union
int num_i[2]; int num_i[2];
} tos_num; } tos_num;
int* TOS; double* TOS;
int tp; int tp;
int initTOS(); int initTOS();
void push_int(int); void push_int(int);