Now ip changes in switch statement

This commit is contained in:
Alex Vanin 2014-01-28 14:13:21 +04:00
parent 808f62b97e
commit 73c63fa7aa

View file

@ -60,351 +60,351 @@ int main(int argc, char** argv)
while (exec_status)
{
switch (code[ip])
switch (code[ip++])
{
case INVALID:
//DO(INVALID, "Invalid instruction.", 1)
ip++; break;
break;
case DLOAD:
//DO(DLOAD, "Load double on TOS, inlined into insn stream.", 9)
d1 = *((double*)(code+(++ip)));
d1 = *((double*)(code+ip));
push_double(d1);
ip+=8; break;
case ILOAD:
//DO(ILOAD, "Load int on TOS, inlined into insn stream.", 9)
i1 = *((long long*)(code+(++ip)));
i1 = *((long long*)(code+ip));
push_int(i1);
ip+=8; break;
case SLOAD:
s1 = *((short*)(code+(++ip)));
s1 = *((short*)(code+ip));
push_int((long long)(const_index[s1]));
ip+=2; break;
case DLOAD0:
// DO(DLOAD0, "Load double 0 on TOS.", 1)
push_double(0);
ip++; break;
break;
case ILOAD0:
//DO(ILOAD0, "Load int 0 on TOS.", 1)
push_int(0);
ip++; break;
break;
case SLOAD0:
//DO(SLOAD0, "Load empty string on TOS.", 1)
push_int((long long)(const_index[0]));
ip++; break;
break;
case DLOAD1:
//DO(DLOAD1, "Load double 1 on TOS.", 1)
push_double(1);
ip++; break;
break;
case ILOAD1:
//DO(ILOAD1, "Load int 1 on TOS.", 1)
push_int(1);
ip++; break;
break;
case DLOADM1:
//DO(DLOADM1, "Load double -1 on TOS.", 1)
push_double(-1);
ip++; break;
break;
case ILOADM1:
//DO(ILOADM1, "Load int -1 on TOS.", 1)
push_int(-1);
ip++; break;
break;
case DADD:
//DO(DADD, "Add 2 doubles on TOS, push value back.", 1)
d1 = pop_double();
d2 = pop_double();
d1 += d2;
push_double(d1);
ip++; break;
break;
case IADD:
//DO(IADD, "Add 2 ints on TOS, push value back.", 1)
i1 = pop_int();
i2 = pop_int();
i1 += i2;
push_int(i1);
ip++; break;
break;
case DSUB:
//DO(DSUB, "Subtract 2 doubles on TOS (lower from upper), push value back.", 1)
d1 = pop_double();
d2 = pop_double();
d2 -= d1;
push_double(d2);
ip++; break;
break;
case ISUB:
//DO(ISUB, "Subtract 2 ints on TOS (lower from upper), push value back.", 1)
i1 = pop_int();
i2 = pop_int();
i2 -= i1;
push_int(i2);
ip++; break;
break;
case DMUL:
//DO(DMUL, "Multiply 2 doubles on TOS, push value back.", 1)
d1 = pop_double();
d2 = pop_double();
d1 *= d2;
push_double(d1);
ip++; break;
break;
case IMUL:
//DO(IMUL, "Multiply 2 ints on TOS, push value back.", 1)
i1 = pop_int();
i2 = pop_int();
i1 *= i2;
push_int(i1);
ip++; break;
break;
case DDIV:
//DO(DDIV, "Divide 2 doubles on TOS (upper to lower), push value back.", 1)
d1 = pop_double();
d2 = pop_double();
d1 = d1/d2;
push_double(d1);
ip++; break;
break;
case IDIV:
//DO(IDIV, "Divide 2 ints on TOS (upper to lower), push value back.", 1)
i1 = pop_int();
i2 = pop_int();
i1 = i1/i2;
push_int(i1);
ip++; break;
break;
case IMOD:
//DO(IMOD, "Modulo operation on 2 ints on TOS (upper to lower), push value back.", 1)
i1 = pop_int();
i2 = pop_int();
i1 = i1 % i2;
push_int(i1);
ip++; break;
break;
case DNEG:
//DO(DNEG, "Negate double on TOS.", 1)
d1 = pop_double();
d1 = -d1;
push_double(d1);
ip++; break;
break;
case INEG:
//DO(INEG, "Negate int on TOS.", 1)
i1 = pop_int();
i1 = - i1;
push_int(i1);
ip++; break;
break;
case IAOR:
//DO(IAOR, "Arithmetic OR of 2 ints on TOS, push value back.", 1)
i1 = pop_int();
i2 = pop_int();
i1 = i1 | i2;
push_int(i1);
ip++; break;
break;
case IAAND:
//DO(IAAND, "Arithmetic AND of 2 ints on TOS, push value back.", 1)
i1 = pop_int();
i2 = pop_int();
i1 = i1 & i2;
push_int(i1);
ip++; break;
break;
case IAXOR:
//DO(IAXOR, "Arithmetic XOR of 2 ints on TOS, push value back.", 1)
i1 = pop_int();
i2 = pop_int();
i1 = i1 ^ i2;
push_int(i1);
ip++; break;
break;
case IPRINT:
//DO(IPRINT, "Pop and print integer TOS.", 1)
i1 = pop_int();
printf("%llu", i1);
ip++; break;
break;
case DPRINT:
//DO(DPRINT, "Pop and print double TOS.", 1)
d1 = pop_double();
printf("%f", d1);
ip++; break;
break;
case SPRINT:
//DO(SPRINT, "Pop and print string TOS.", 1)
i1 = pop_int();
printf("%s", (char*)i1);
ip++; break;
break;
case I2D:
//DO(I2D, "Convert int on TOS to double.", 1)
i1 = pop_int();
d1 = (double)i1;
push_double(d1);
ip++; break;
break;
case D2I:
//DO(D2I, "Convert double on TOS to int.", 1)
d1 = pop_double();
i1 = (int)d1;
push_int(i1);
ip++; break;
break;
case S2I:
//DO(S2I, "Convert string on TOS to int.", 1)
ip++; break;
break;
case SWAP:
//DO(SWAP, "Swap 2 topmost values.", 1)
i1 = pop_int();
i2 = pop_int();
push_int(i1);
push_int(i2);
ip++; break;
break;
case POP:
//DO(POP, "Remove topmost value.", 1)
TOS--;
ip++; break;
break;
case LOADDVAR0:
//DO(LOADDVAR0, "Load double from variable 0, push on TOS.", 1)
d1 = getlocal_double(current_context, 0);
push_double(d1);
ip++;break;
break;
case LOADDVAR1:
//DO(LOADDVAR1, "Load double from variable 1, push on TOS.", 1)
d1 = getlocal_double(current_context, 1);
push_double(d1);
ip++;break;
break;
case LOADDVAR2:
//DO(LOADDVAR2, "Load double from variable 2, push on TOS.", 1)
d1 = getlocal_double(current_context, 2);
push_double(d1);
ip++;break;
break;
case LOADDVAR3:
//DO(LOADDVAR3, "Load double from variable 3, push on TOS.", 1)
d1 = getlocal_double(current_context, 3);
push_double(d1);
ip++; break;
break;
case LOADIVAR0:
//DO(LOADIVAR0, "Load int from variable 0, push on TOS.", 1)
i1 = getlocal_int(current_context, 0);
push_int(i1);
ip++; break;
break;
case LOADIVAR1:
//DO(LOADIVAR1, "Load int from variable 1, push on TOS.", 1)
i1 = getlocal_int(current_context, 1);
push_int(i1);
ip++; break;
break;
case LOADIVAR2:
//DO(LOADIVAR2, "Load int from variable 2, push on TOS.", 1)
i1 = getlocal_int(current_context, 2);
push_int(i1);
ip++; break;
break;
case LOADIVAR3:
//DO(LOADIVAR3, "Load int from variable 3, push on TOS.", 1)
i1 = getlocal_int(current_context, 3);
push_int(i1);
ip++; break;
break;
case LOADSVAR0:
//DO(LOADSVAR0, "Load string from variable 0, push on TOS.", 1)
i1 = (long long)getlocal_string(current_context, 0);
push_int(i1);
ip++; break;
break;
case LOADSVAR1:
//DO(LOADSVAR1, "Load string from variable 1, push on TOS.", 1)
i1 = (long long)getlocal_string(current_context, 1);
push_int(i1);
ip++; break;
break;
case LOADSVAR2:
//DO(LOADSVAR2, "Load string from variable 2, push on TOS.", 1)
i1 = (long long)getlocal_string(current_context, 2);
push_int(i1);
ip++; break;
break;
case LOADSVAR3:
//DO(LOADSVAR3, "Load string from variable 3, push on TOS.", 1)
i1 = (long long)getlocal_string(current_context, 3);
push_int(i1);
ip++; break;
break;
case STOREDVAR0:
//DO(STOREDVAR0, "Pop TOS and store to double variable 0.", 1)
d1 = pop_double();
putlocal_double(&d1, current_context, 0);
ip++; break;
break;
case STOREDVAR1:
//DO(STOREDVAR1, "Pop TOS and store to double variable 1.", 1)
d1 = pop_double();
putlocal_double(&d1, current_context, 1);
ip++; break;
break;
case STOREDVAR2:
//DO(STOREDVAR2, "Pop TOS and store to double variable 2.", 1)
d1 = pop_double();
putlocal_double(&d1, current_context, 2);
ip++; break;
break;
case STOREDVAR3:
//DO(STOREDVAR3, "Pop TOS and store to double variable 3.", 1)
d1 = pop_double();
putlocal_double(&d1, current_context, 3);
ip++; break;
break;
case STOREIVAR0:
//DO(STOREIVAR0, "Pop TOS and store to int variable 0.", 1)
i1 = pop_int();
putlocal_int(&i1, current_context, 0);
ip++; break;
break;
case STOREIVAR1:
//DO(STOREIVAR1, "Pop TOS and store to int variable 1.", 1)
i1 = pop_int();
putlocal_int(&i1, current_context, 1);
ip++; break;
break;
case STOREIVAR2:
//DO(STOREIVAR2, "Pop TOS and store to int variable 2.", 1)
i1 = pop_int();
putlocal_int(&i1, current_context, 2);
ip++; break;
break;
case STOREIVAR3:
//DO(STOREIVAR3, "Pop TOS and store to int variable 3.", 1)
i1 = pop_int();
putlocal_int(&i1, current_context, 3);
ip++; break;
break;
case STORESVAR0:
//DO(STORESVAR0, "Pop TOS and store to string variable 0.", 1)
i1 = pop_int();
putlocal_string((int*)&i1, current_context, 0);
ip++; break;
putlocal_string((char*)&i1, current_context, 0);
break;
case STORESVAR1:
//DO(STORESVAR1, "Pop TOS and store to string variable 1.", 1)
i1 = pop_int();
putlocal_string((int*)&i1, current_context, 1);
ip++; break;
putlocal_string((char*)&i1, current_context, 1);
break;
case STORESVAR2:
//DO(STORESVAR2, "Pop TOS and store to string variable 2.", 1)
i1 = pop_int();
putlocal_string((int*)&i1, current_context, 2);
ip++; break;
putlocal_string((char*)&i1, current_context, 2);
break;
case STORESVAR3:
//DO(STORESVAR3, "Pop TOS and store to string variable 3.", 1)
i1 = pop_int();
putlocal_string((int*)&i1, current_context, 3);
ip++; break;
putlocal_string((char*)&i1, current_context, 3);
break;
case LOADDVAR:
//DO(LOADDVAR, "Load double from variable, whose 2-byte is id inlined to insn stream, push on TOS.", 3)
s1 = *((short*)(code+(++ip)));
s1 = *((short*)(code+ip));
d1 = getlocal_double(current_context, s1);
push_double(d1);
ip+=2; break;
case LOADIVAR:
//DO(LOADIVAR, "Load int from variable, whose 2-byte id is inlined to insn stream, push on TOS.", 3)
s1 = *((short*)(code+(++ip)));
s1 = *((short*)(code+ip));
i1 = getlocal_int(current_context, s1);
push_int(i1);
ip+=2; break;
case LOADSVAR:
//DO(LOADSVAR, "Load string from variable, whose 2-byte id is inlined to insn stream, push on TOS.", 3)
s1 = *((short*)(code+(++ip)));
s1 = *((short*)(code+ip));
i1 = (long long)getlocal_string(current_context, s1);
push_int(i1);
ip+=2; break;
case STOREDVAR:
//DO(STOREDVAR, "Pop TOS and store to double variable, whose 2-byte id is inlined to insn stream.", 3)
s1 = *((short*)(code+(++ip)));
s1 = *((short*)(code+ip));
d1 = pop_double();
putlocal_double(&d1, current_context, s1);
ip+=2; break;
case STOREIVAR:
//DO(STOREIVAR, "Pop TOS and store to int variable, whose 2-byte id is inlined to insn stream.", 3)
s1 = *((short*)(code+(++ip)));
s1 = *((short*)(code+ip));
i1 = pop_int();
putlocal_int(&i1, current_context, s1);
ip+=2; break;
case STORESVAR:
//DO(STORESVAR, "Pop TOS and store to string variable, whose 2-byte id is inlined to insn stream.", 3)
s1 = *((short*)(code+(++ip)));
s1 = *((short*)(code+ip));
i1 = pop_int();
putlocal_string((int*)&i1, current_context, s1);
putlocal_string((char*)&i1, current_context, s1);
ip+=2; break;
case LOADCTXDVAR:
//DO(LOADCTXDVAR, "Load double from variable, whose 2-byte context and 2-byte id inlined to insn stream, push on TOS.", 5)
s1 = *((short*)(code+(++ip)));
s1 = *((short*)(code+ip));
ip+=2;
s2 = *((short*)(code+(ip)));
s2 = *((short*)(code+ip));
if (find_context(s1) != NULL)
{
d1 = getlocal_double(find_context(s1), s2);
@ -418,9 +418,9 @@ int main(int argc, char** argv)
ip+=2; break;
case LOADCTXIVAR:
//DO(LOADCTXIVAR, "Load int from variable, whose 2-byte context and 2-byte id is inlined to insn stream, push on TOS.", 5)
s1 = *((short*)(code+(++ip)));
s1 = *((short*)(code+ip));
ip+=2;
s2 = *((short*)(code+(ip)));
s2 = *((short*)(code+ip));
if (find_context(s1) != NULL)
{
i1 = getlocal_int(find_context(s1), s2);
@ -434,9 +434,9 @@ int main(int argc, char** argv)
ip+=2; break;
case LOADCTXSVAR:
//DO(LOADCTXSVAR, "Load string from variable, whose 2-byte context and 2-byte id is inlined to insn stream, push on TOS.", 5)
s1 = *((short*)(code+(++ip)));
s1 = *((short*)(code+ip));
ip+=2;
s2 = *((short*)(code+(ip)));
s2 = *((short*)(code+ip));
if (find_context(s1) != NULL)
{
i1 = (long long)getlocal_string(find_context(s1), s2);
@ -450,9 +450,9 @@ int main(int argc, char** argv)
ip+=2; break;
case STORECTXDVAR:
//DO(STORECTXDVAR, "Pop TOS and store to double variable, whose 2-byte context and 2-byte id is inlined to insn stream.", 5)
s1 = *((short*)(code+(++ip)));
s1 = *((short*)(code+ip));
ip+=2;
s2 = *((short*)(code+(ip)));
s2 = *((short*)(code+ip));
d1 = pop_double();
if (find_context(s1) != NULL)
{
@ -466,9 +466,9 @@ int main(int argc, char** argv)
ip+=2; break;
case STORECTXIVAR:
//DO(STORECTXIVAR, "Pop TOS and store to int variable, whose 2-byte context and 2-byte id is inlined to insn stream.", 5)
s1 = *((short*)(code+(++ip)));
s1 = *((short*)(code+ip));
ip+=2;
s2 = *((short*)(code+(ip)));
s2 = *((short*)(code+ip));
i1 = pop_int();
if (find_context(s1) != NULL)
{
@ -482,13 +482,13 @@ int main(int argc, char** argv)
ip+=2; break;
case STORECTXSVAR:
//DO(STORECTXSVAR, "Pop TOS and store to string variable, whose 2-byte context and 2-byte id is inlined to insn stream.", 5)
s1 = *((short*)(code+(++ip)));
s1 = *((short*)(code+ip));
ip+=2;
s2 = *((short*)(code+(ip)));
s2 = *((short*)(code+ip));
i1 = pop_int();
if (find_context(s1) != NULL)
{
putlocal_string((int*)&d1, find_context(s1), s2);
putlocal_string((char*)&d1, find_context(s1), s2);
}
else
{
@ -501,20 +501,20 @@ int main(int argc, char** argv)
d1 = TOS[tp-1];
d2 = TOS[tp-2];
push_double(cmp_double(&d1, &d2));
ip++; break;
break;
case ICMP:
//DO(ICMP, "Compare 2 topmost ints, pushing libc-style comparator value cmp(upper, lower) as integer.", 1)
i1 = get_int(tp-1);
i2 = get_int(tp-2);
push_int(cmp_int(&i1, &i2));
ip++; break;
break;
case JA:
//DO(JA, "Jump always, next two bytes - signed offset of jump destination.", 3)
s1 = *((short*)(code+(++ip)));
s1 = *((short*)(code+ip));
ip += (2+s1); break;
case IFICMPNE:
// DO(IFICMPNE, "Compare two topmost integers and jump if upper != lower, next two bytes - signed offset of jump destination.", 3)
s1 = *((short*)(code+(++ip)));
s1 = *((short*)(code+ip));
ip+=2;
i1 = get_int(tp-1);
i2 = get_int(tp-2);
@ -523,7 +523,7 @@ int main(int argc, char** argv)
break;
case IFICMPE:
//DO(IFICMPE, "Compare two topmost integers and jump if upper == lower, next two bytes - signed offset of jump destination.", 3)
s1 = *((short*)(code+(++ip)));
s1 = *((short*)(code+ip));
ip+=2;
i1 = get_int(tp-1);
i2 = get_int(tp-2);
@ -532,7 +532,7 @@ int main(int argc, char** argv)
break;
case IFICMPG:
//DO(IFICMPG, "Compare two topmost integers and jump if upper > lower, next two bytes - signed offset of jump destination.", 3)
s1 = *((short*)(code+(++ip)));
s1 = *((short*)(code+ip));
ip+=2;
i1 = get_int(tp-1);
i2 = get_int(tp-2);
@ -541,7 +541,7 @@ int main(int argc, char** argv)
break;
case IFICMPGE:
//DO(IFICMPGE, "Compare two topmost integers and jump if upper >= lower, next two bytes - signed offset of jump destination.", 3)
s1 = *((short*)(code+(++ip)));
s1 = *((short*)(code+ip));
ip+=2;
i1 = get_int(tp-1);
i2 = get_int(tp-2);
@ -550,7 +550,7 @@ int main(int argc, char** argv)
break;
case IFICMPL:
//DO(IFICMPL, "Compare two topmost integers and jump if upper < lower, next two bytes - signed offset of jump destination.", 3)
s1 = *((short*)(code+(++ip)));
s1 = *((short*)(code+ip));
ip+=2;
i1 = get_int(tp-1);
i2 = get_int(tp-2);
@ -559,7 +559,7 @@ int main(int argc, char** argv)
break;
case IFICMPLE:
//DO(IFICMPLE, "Compare two topmost integers and jump if upper <= lower, next two bytes - signed offset of jump destination.", 3)
s1 = *((short*)(code+(++ip)));
s1 = *((short*)(code+ip));
ip+=2;
i1 = get_int(tp-1);
i2 = get_int(tp-2);
@ -571,14 +571,14 @@ int main(int argc, char** argv)
i1 = get_int(tp-1);
d1 = TOS[tp-1];
printf("TOS: Double:%e Integer:%llu Hex:%#08llx", d1, i1, i1);
ip++; break;
break;
case STOP:
//DO(STOP, "Stop execution.", 1)
exec_status = 0;
break;
case CALL:
//DO(CALL, "Call function, next two bytes - unsigned function id.", 3)
s1 = *((short*)(code+(++ip)));
s1 = *((short*)(code+ip));
ip+=2; push_ret(ip);
push_context(current_context);
current_context = create_context(s1, phash_table, &code);
@ -592,7 +592,7 @@ int main(int argc, char** argv)
case BREAK:
//DO(BREAK, "Breakpoint for the debugger.", 1)
getchar();
ip++; break;
break;
}
}
remove_context(current_context);