Пример #1
0
//Execute cycle
void execute(FILE *ofp)
{
	fprintf(ofp, "%d\t%s\t%d\t%d\t", pc-1, opcodes[ir.op], ir.l, ir.m);
	switch( ir.op )
	{
		case 1 :
			LIT(ir.l, ir.m);
			break;
		case 2 :
			OPR(ir.l, ir.m);
			break;
		case 3 :
			LOD(ir.l, ir.m);
			break;
		case 4 :
			STO(ir.l, ir.m);
			break;
		case 5 :
			CAL(ir.l, ir.m);
			break;
		case 6 :
			INC(ir.l, ir.m);
			break;
		case 7 :
			JMP(ir.l, ir.m);
			break;
		case 8 :
			JPC(ir.l, ir.m);
			break;
		case 9 :
			SIO(ir.l, ir.m);
			break;
		case 10 :
			SIO(ir.l, ir.m);
			break;
		case 11 :
			SIO(ir.l, ir.m);
			break;
		
		default 	: 
			break;
	}
	fprintf(ofp, "%d\t%d\t%d\t", pc, bp, sp);
	
	int i = 1;
	int countAR = 0;
	for(i = 1; i <= sp; i++)
	{
		if(countAR < numAR && ar[countAR] < i)
		{	
			countAR++;
			fprintf(ofp, " |");
		}
		
		fprintf(ofp, " %d", stack[i]);
	}
	
	fprintf(ofp, "\n");
	
}
Пример #2
0
// Uses value in IR to determine course of action
// Returns false if errors
bool interpreter() {
  bool success = true;
  //While no error flag and no timer interrupt
  while (success && timer_interrupt < QUANTUM) {
    machine.IR = main_memory[MMU(machine.PC)];
    machine.PC++; // Increment Program Counter
    unsigned short int op = getOpcode(machine.IR);
    switch (op) {
      case 0:  success = LOD(); break;
      case 1:  success = STO(); break;
      case 2:  success = ADD(); break;
      case 3:  success = SUB(); break;
      case 4:  success = ADR(); break;
      case 5:  success = SUR(); break;
      case 6:  success = AND(); break;
      case 7:  success = IOR(); break;
      case 8:  success = NOT(); break;
      case 9:  success = JMP(); break;
      case 10: success = JEQ(); break;
      case 11: success = JGT(); break;
      case 12: success = JLT(); break;
      case 13: success = CMP(); break;
      case 14: success = CLR(); break;
      case 15:    return HLT(); break; //Quit early on HLT
      default: success = false; break;
    }
    usleep(1000000); // Sleep 1 second to allow easier instruction tracing
    (*sysclock)++;
    timer_interrupt++;
  }
  timer_interrupt = 0;
  return success;
}
Пример #3
0
MVM_Object *
create_array_literal_object(MVM_VirtualMachine *mvm, int size)
{
    MVM_Object *array;
    int i;

    array = mvm_create_array_object_i(mvm, size);
    for (i = 0; i < size; i++) {
        array->u.array.u.object[i] = STO(mvm, -size+i);
    }

    return array;
}
Пример #4
0
static void
execute(MVM_VirtualMachine *mvm, Function *func,
        MVM_Byte *code, int code_size)
{
    int         base;
    MVM_Executable *exe;
    int         pc;
    //MVM_Value   ret;

    pc = mvm->pc;
    exe = mvm->current_executable;

    while (pc < code_size) {
        /*printf("%s  sp(%d)\t\n",
                mvm_opcode_info[code[pc]].mnemonic,
                                mvm->stack.stack_pointer);*/


        switch (code[pc]) {
        case MVM_PUSH_INT_1BYTE:
            STI_WRITE(mvm, 0, code[pc+1]);
            //printf("int byte %d\n",code[pc+1]);
            mvm->stack.stack_pointer++;
            pc += 2;
            break;
        case MVM_PUSH_INT_2BYTE:
            STI_WRITE(mvm, 0, GET_2BYTE_INT(&code[pc+1]));
            mvm->stack.stack_pointer++;
            pc += 3;
            break;
        case MVM_PUSH_INT:
            STI_WRITE(mvm, 0,
                      exe->constant_pool[GET_2BYTE_INT(&code[pc+1])].u.c_integer);
            mvm->stack.stack_pointer++;
            pc += 3;
            break;
        case MVM_PUSH_DOUBLE_0:
            STD_WRITE(mvm, 0, 0.0);
            mvm->stack.stack_pointer++;
            pc++;
            break;
        case MVM_PUSH_DOUBLE_1:
            STD_WRITE(mvm, 0, 1.0);
            mvm->stack.stack_pointer++;
            pc++;
            break;
        case MVM_PUSH_DOUBLE:
            STD_WRITE(mvm, 0,
                      exe->constant_pool[GET_2BYTE_INT(&code[pc+1])].u.c_decimal);
            mvm->stack.stack_pointer++;
            pc += 3;
            break;
        case MVM_PUSH_STRING:
            STO_WRITE(mvm, 0,
                      mvm_literal_to_mvm_string_i(mvm,
                                                  exe->constant_pool
                                                  [GET_2BYTE_INT(&code
                                                          [pc+1])]
                                                  .u.c_string));
            mvm->stack.stack_pointer++;
            pc += 3;
            break;
        case MVM_PUSH_NULL:
            STO_WRITE(mvm, 0, NULL);
            mvm->stack.stack_pointer++;
            pc++;
            break;
        case MVM_PUSH_STACK_INT:
            STI_WRITE(mvm, 0,
                      STI_I(mvm, base + GET_2BYTE_INT(&code[pc+1])));
            mvm->stack.stack_pointer++;
            pc += 3;
            break;
        case MVM_PUSH_STACK_DOUBLE:
            STD_WRITE(mvm, 0,
                      STD_I(mvm, base + GET_2BYTE_INT(&code[pc+1])));
            mvm->stack.stack_pointer++;
            pc += 3;
            break;
        case MVM_PUSH_STACK_OBJECT:
            STO_WRITE(mvm, 0,
                      STO_I(mvm, base + GET_2BYTE_INT(&code[pc+1])));
            mvm->stack.stack_pointer++;
            pc += 3;
            break;
        case MVM_POP_STACK_INT:
            STI_WRITE_I(mvm, base + GET_2BYTE_INT(&code[pc+1]),
                        STI(mvm, -1));
            mvm->stack.stack_pointer--;
            pc += 3;
            break;
        case MVM_POP_STACK_DOUBLE:
            STD_WRITE_I(mvm, base + GET_2BYTE_INT(&code[pc+1]),
                        STD(mvm, -1));
            mvm->stack.stack_pointer--;
            pc += 3;
            break;
        case MVM_POP_STACK_OBJECT:
            STO_WRITE_I(mvm, base + GET_2BYTE_INT(&code[pc+1]),
                        STO(mvm, -1));
            mvm->stack.stack_pointer--;
            pc += 3;
            break;
        case MVM_PUSH_STATIC_INT:
            STI_WRITE(mvm, 0,
                      mvm->static_v.variable[GET_2BYTE_INT(&code[pc+1])]
                      .int_value);
            mvm->stack.stack_pointer++;
            pc += 3;
            break;
        case MVM_PUSH_STATIC_DOUBLE:
            STD_WRITE(mvm, 0,
                      mvm->static_v.variable[GET_2BYTE_INT(&code[pc+1])]
                      .double_value);
            mvm->stack.stack_pointer++;
            pc += 3;
            break;
        case MVM_PUSH_STATIC_OBJECT:
            STO_WRITE(mvm, 0,mvm->static_v.variable[GET_2BYTE_INT(&code[pc+1])].object);
            mvm->stack.stack_pointer++;
            pc += 3;
            break;
        case MVM_POP_STATIC_INT:
            mvm->static_v.variable[GET_2BYTE_INT(&code[pc+1])].int_value
                = STI(mvm, -1);
            mvm->stack.stack_pointer--;
            pc += 3;
            break;
        case MVM_POP_STATIC_DOUBLE:
            mvm->static_v.variable[GET_2BYTE_INT(&code[pc+1])]
            .double_value
                = STD(mvm, -1);
            mvm->stack.stack_pointer--;
            pc += 3;
            break;
        case MVM_POP_STATIC_OBJECT:
            mvm->static_v.variable[GET_2BYTE_INT(&code[pc+1])].object
                = STO(mvm, -1);
            mvm->stack.stack_pointer--;
            pc += 3;
            break;
        case MVM_PUSH_ARRAY_INT:
        {
            MVM_Object *array = STO(mvm, -2);
            int index = STI(mvm, -1);
            int int_value;

            restore_pc(mvm, exe, func, pc);
            int_value = MVM_array_get_int(mvm, array, index);

            STI_WRITE(mvm, -2, int_value);
            mvm->stack.stack_pointer--;
            pc++;
            break;
        }
        case MVM_PUSH_ARRAY_DOUBLE:
        {
            MVM_Object *array = STO(mvm, -2);
            int index = STI(mvm, -1);
            double double_value;

            restore_pc(mvm, exe, func, pc);
            double_value = MVM_array_get_double(mvm, array, index);

            STD_WRITE(mvm, -2, double_value);
            mvm->stack.stack_pointer--;
            pc++;
            break;
        }
        case MVM_PUSH_ARRAY_OBJECT:
        {
            MVM_Object *array = STO(mvm, -2);
            int index = STI(mvm, -1);
            MVM_Object *object;

            restore_pc(mvm, exe, func, pc);
            object = MVM_array_get_object(mvm, array, index);

            STO_WRITE(mvm, -2, object);
            mvm->stack.stack_pointer--;
            pc++;
            break;
        }
        case MVM_POP_ARRAY_INT:
        {
            int value = STI(mvm, -3);
            MVM_Object *array = STO(mvm, -2);
            int index = STI(mvm, -1);

            restore_pc(mvm, exe, func, pc);
            MVM_array_set_int(mvm, array, index, value);
            mvm->stack.stack_pointer -= 3;
            pc++;
            break;
        }
        case MVM_POP_ARRAY_DOUBLE:
        {
            double value = STD(mvm, -3);
            MVM_Object *array = STO(mvm, -2);
            int index = STI(mvm, -1);

            restore_pc(mvm, exe, func, pc);
            MVM_array_set_double(mvm, array, index, value);
            mvm->stack.stack_pointer -= 3;
            pc++;
            break;
        }
        case MVM_POP_ARRAY_OBJECT:
        {
            MVM_Object *value = STO(mvm, -3);
            MVM_Object *array = STO(mvm, -2);
            int index = STI(mvm, -1);

            restore_pc(mvm, exe, func, pc);
            MVM_array_set_object(mvm, array, index, value);
            mvm->stack.stack_pointer -= 3;
            pc++;
            break;
        }
        case MVM_PUSH_FIELD_INT:
        {
            MVM_Object *obj = STO(mvm, -1);
            int index = GET_2BYTE_INT(&code[pc+1]);

            //check_null_pointer(exe, func, pc, obj);
            STI_WRITE(mvm, -1,
                      obj->u.class_object.field[index].int_value);
            pc += 3;
            break;
        }
        case MVM_PUSH_FIELD_DOUBLE:
        {
            MVM_Object *obj = STO(mvm, -1);
            int index = GET_2BYTE_INT(&code[pc+1]);

            //check_null_pointer(exe, func, pc, obj);
            STD_WRITE(mvm, -1,
                      obj->u.class_object.field[index].double_value);
            pc += 3;
            break;
        }
        case MVM_PUSH_FIELD_OBJECT:
        {
            MVM_Object *obj = STO(mvm, -1);
            int index = GET_2BYTE_INT(&code[pc+1]);

            //check_null_pointer(exe, func, pc, obj);
            STO_WRITE(mvm, -1, obj->u.class_object.field[index].object);
            pc += 3;
            break;
        }
        case MVM_POP_FIELD_INT:
        {
            MVM_Object *obj = STO(mvm, -1);
            int index = GET_2BYTE_INT(&code[pc+1]);
            obj->u.class_object.field[index].int_value = STI(mvm, -2);
            mvm->stack.stack_pointer -= 2;
            pc += 3;
            break;
        }
        case MVM_POP_FIELD_DOUBLE:
        {
            MVM_Object *obj = STO(mvm, -1);
            int index = GET_2BYTE_INT(&code[pc+1]);
            obj->u.class_object.field[index].double_value = STD(mvm, -2);
            mvm->stack.stack_pointer -= 2;
            pc += 3;
            break;
        }
        case MVM_POP_FIELD_OBJECT:
        {
            MVM_Object *obj = STO(mvm, -1);
            int index = GET_2BYTE_INT(&code[pc+1]);

            //check_null_pointer(exe, func, pc, obj);
            obj->u.class_object.field[index].object = STO(mvm, -2);
            mvm->stack.stack_pointer -= 2;
            pc += 3;
            break;
        }
        case MVM_ADD_INT:
            STI(mvm, -2) = STI(mvm, -2) + STI(mvm, -1);
            mvm->stack.stack_pointer--;
            pc++;
            break;
        case MVM_ADD_DOUBLE:
            STD(mvm, -2) = STD(mvm, -2) + STD(mvm, -1);
            mvm->stack.stack_pointer--;
            pc++;
            break;
        case MVM_ADD_STRING:
            STO(mvm, -2) = chain_string(mvm,
                                        STO(mvm, -2),
                                        STO(mvm, -1));
            mvm->stack.stack_pointer--;
            pc++;
            break;
        case MVM_SUB_INT:
            STI(mvm, -2) = STI(mvm, -2) - STI(mvm, -1);
            mvm->stack.stack_pointer--;
            pc++;
            break;
        case MVM_SUB_DOUBLE:
            STD(mvm, -2) = STD(mvm, -2) - STD(mvm, -1);
            mvm->stack.stack_pointer--;
            pc++;
            break;
        case MVM_MUL_INT:
            STI(mvm, -2) = STI(mvm, -2) * STI(mvm, -1);
            mvm->stack.stack_pointer--;
            pc++;
            break;
        case MVM_MUL_DOUBLE:
            STD(mvm, -2) = STD(mvm, -2) * STD(mvm, -1);
            mvm->stack.stack_pointer--;
            pc++;
            break;
        case MVM_DIV_INT:
            if (STI(mvm, -1) == 0) {
                //mvm_error(exe, func, pc, DIVISION_BY_ZERO_ERR,
                //          MESSAGE_ARGUMENT_END);
            }
            STI(mvm, -2) = STI(mvm, -2) / STI(mvm, -1);
            mvm->stack.stack_pointer--;
            pc++;
            break;
        case MVM_DIV_DOUBLE:
            STD(mvm, -2) = STD(mvm, -2) / STD(mvm, -1);
            mvm->stack.stack_pointer--;
            pc++;
            break;
        case MVM_MOD_INT:
            STI(mvm, -2) = STI(mvm, -2) % STI(mvm, -1);
            mvm->stack.stack_pointer--;
            pc++;
            break;
        case MVM_MOD_DOUBLE:
            STD(mvm, -2) = fmod(STD(mvm, -2), STD(mvm, -1));
            mvm->stack.stack_pointer--;
            pc++;
            break;
        case MVM_MINUS_INT:
            STI(mvm, -1) = -STI(mvm, -1);
            pc++;
            break;
        case MVM_MINUS_DOUBLE:
            STD(mvm, -1) = -STD(mvm, -1);
            pc++;
            break;
        case MVM_INCREMENT:
            STI(mvm, -1)++;
            pc++;
            break;
        case MVM_DECREMENT:
            STI(mvm, -1)--;
            pc++;
            break;
        case MVM_CAST_INT_TO_DOUBLE:
            STD(mvm, -1) = (double)STI(mvm, -1);
            pc++;
            break;
        case MVM_CAST_DOUBLE_TO_INT:
            STI(mvm, -1) = (int)STD(mvm, -1);
            pc++;
            break;
        case MVM_CAST_BOOLEAN_TO_STRING:
            if (STI(mvm, -1)) {
                STO_WRITE(mvm, -1,
                          mvm_literal_to_mvm_string_i(mvm, TRUE_STRING));
            } else {
                STO_WRITE(mvm, -1,
                          mvm_literal_to_mvm_string_i(mvm, FALSE_STRING));
            }
            pc++;
            break;
        case MVM_CAST_INT_TO_STRING:
        {
            char buf[LINE_BUF_SIZE];
            MVM_Char *str;

            sprintf(buf, "%d", STI(mvm, -1));
            restore_pc(mvm, exe, func, pc);
            str = str_alloc(buf);
            STO_WRITE(mvm, -1,
                      mvm_create_mvm_string_i(mvm, str));
            pc++;
            break;
        }
        case MVM_CAST_DOUBLE_TO_STRING:
        {
            char buf[LINE_BUF_SIZE];
            MVM_Char *str;

            sprintf(buf, "%f", STD(mvm, -1));
            restore_pc(mvm, exe, func, pc);
            str = str_alloc(buf);
            STO_WRITE(mvm, -1,
                      mvm_create_mvm_string_i(mvm, str));
            pc++;
            break;
        }
        case MVM_EQ_INT:
            STI(mvm, -2) = (STI(mvm, -2) == STI(mvm, -1));
            mvm->stack.stack_pointer--;
            pc++;
            break;
        case MVM_EQ_DOUBLE:
            STI(mvm, -2) = (STD(mvm, -2) == STD(mvm, -1));
            mvm->stack.stack_pointer--;
            pc++;
            break;
        case MVM_EQ_OBJECT:
            STI_WRITE(mvm, -2, STO(mvm, -2) == STO(mvm, -1));
            mvm->stack.stack_pointer--;
            pc++;
            break;
        case MVM_EQ_STRING:
            STI_WRITE(mvm, -2,
                      !strcmp(STO(mvm, -2)->u.string.string, STO(mvm, -1)->u.string.string));
            mvm->stack.stack_pointer--;
            pc++;
            break;
        case MVM_GT_INT:
            STI(mvm, -2) = (STI(mvm, -2) > STI(mvm, -1));
            mvm->stack.stack_pointer--;
            pc++;
            break;
        case MVM_GT_DOUBLE:
            STI(mvm, -2) = (STD(mvm, -2) > STD(mvm, -1));
            mvm->stack.stack_pointer--;
            pc++;
            break;
        case MVM_GT_STRING:
            STI_WRITE(mvm, -2,
                      strcmp(STO(mvm, -2)->u.string.string,
                             STO(mvm, -1)->u.string.string) > 0);
            mvm->stack.stack_pointer--;
            pc++;
            break;
        case MVM_GE_INT:
            STI(mvm, -2) = (STI(mvm, -2) >= STI(mvm, -1));
            mvm->stack.stack_pointer--;
            pc++;
            break;
        case MVM_GE_DOUBLE:
            STI(mvm, -2) = (STD(mvm, -2) >= STD(mvm, -1));
            mvm->stack.stack_pointer--;
            pc++;
            break;
        case MVM_GE_STRING:
            STI_WRITE(mvm, -2,
                      strcmp(STO(mvm, -2)->u.string.string,
                             STO(mvm, -1)->u.string.string) >= 0);
            mvm->stack.stack_pointer--;
            pc++;
            break;
        case MVM_LT_INT:
            STI(mvm, -2) = (STI(mvm, -2) < STI(mvm, -1));
            mvm->stack.stack_pointer--;
            pc++;
            break;
        case MVM_LT_DOUBLE:
            STI(mvm, -2) = (STD(mvm, -2) < STD(mvm, -1));
            mvm->stack.stack_pointer--;
            pc++;
            break;
        case MVM_LT_STRING:
            STI_WRITE(mvm, -2,
                      strcmp(STO(mvm, -2)->u.string.string,
                             STO(mvm, -1)->u.string.string) < 0);
            mvm->stack.stack_pointer--;
            pc++;
            break;
        case MVM_LE_INT:
            STI(mvm, -2) = (STI(mvm, -2) <= STI(mvm, -1));
            mvm->stack.stack_pointer--;
            pc++;
            break;
        case MVM_LE_DOUBLE:
            STI(mvm, -2) = (STD(mvm, -2) <= STD(mvm, -1));
            mvm->stack.stack_pointer--;
            pc++;
            break;
        case MVM_LE_STRING:
            STI_WRITE(mvm, -2,
                      strcmp(STO(mvm, -2)->u.string.string,
                             STO(mvm, -1)->u.string.string) <= 0);
            mvm->stack.stack_pointer--;
            pc++;
            break;
        case MVM_NE_INT:
            STI(mvm, -2) = (STI(mvm, -2) != STI(mvm, -1));
            mvm->stack.stack_pointer--;
            pc++;
            break;
        case MVM_NE_DOUBLE:
            STI(mvm, -2) = (STD(mvm, -2) != STD(mvm, -1));
            mvm->stack.stack_pointer--;
            pc++;
            break;
        case MVM_NE_OBJECT:
            STI_WRITE(mvm, -2, STO(mvm, -2) != STO(mvm, -1));
            mvm->stack.stack_pointer--;
            pc++;
            break;
        case MVM_NE_STRING:
            STI_WRITE(mvm, -2,
                      strcmp(STO(mvm, -2)->u.string.string,
                             STO(mvm, -1)->u.string.string) != 0);
            mvm->stack.stack_pointer--;
            pc++;
            break;
        case MVM_LOGICAL_AND:
            STI(mvm, -2) = (STI(mvm, -2) && STI(mvm, -1));
            mvm->stack.stack_pointer--;
            pc++;
            break;
        case MVM_LOGICAL_OR:
            STI(mvm, -2) = (STI(mvm, -2) || STI(mvm, -1));
            mvm->stack.stack_pointer--;
            pc++;
            break;
        case MVM_LOGICAL_NOT:
            STI(mvm, -1) = !STI(mvm, -1);
            pc++;
            break;
        case MVM_POP:
            mvm->stack.stack_pointer--;
            pc++;
            break;
        case MVM_DUPLICATE:
            mvm->stack.stack[mvm->stack.stack_pointer]
                = mvm->stack.stack[mvm->stack.stack_pointer-1];
            mvm->stack.stack_pointer++;
            pc++;
            break;
        case MVM_JUMP:
            pc = GET_2BYTE_INT(&code[pc+1]);
            break;
        case MVM_JUMP_IF_TRUE:
            if (STI(mvm, -1)) {
                pc = GET_2BYTE_INT(&code[pc+1]);
            } else {
                pc += 3;
            }
            mvm->stack.stack_pointer--;
            break;
        case MVM_JUMP_IF_FALSE:
            if (!STI(mvm, -1)) {
                pc = GET_2BYTE_INT(&code[pc+1]);
            } else {
                pc += 3;
            }
            mvm->stack.stack_pointer--;
            break;
        case MVM_PUSH_FUNCTION:
            STI_WRITE(mvm, 0, GET_2BYTE_INT(&code[pc+1]));
            mvm->stack.stack_pointer++;
            pc += 3;
            break;
        case MVM_INVOKE:
        {
            int func_idx = STI(mvm, -1);
            if (mvm->function[func_idx].kind == NATIVE_FUNCTION) {
                invoke_native_function(mvm, &mvm->function[func_idx],
                                       &mvm->stack.stack_pointer);
                pc++;
            } else {
                invoke_minic_function(mvm, &func, &mvm->function[func_idx],
                                      &code, &code_size, &pc,
                                      &mvm->stack.stack_pointer, &base,
                                      &exe);
            }
            break;
        }
        case MVM_RETURN:
            return_function(mvm, &func, &code, &code_size, &pc,
                            &mvm->stack.stack_pointer, &base,
                            &exe);
            break;
        case MVM_NEW_ARRAY:
        {
            int dim = code[pc+1];
            MVM_TypeSpecifier *type
                = &exe->type_specifier[GET_2BYTE_INT(&code[pc+2])];
            MVM_Object *array;

            restore_pc(mvm, exe, func, pc);
            array = create_array(mvm, dim, type);
            mvm->stack.stack_pointer -= dim;
            STO_WRITE(mvm, 0, array);
            mvm->stack.stack_pointer++;
            pc += 4;
            break;
        }
        case MVM_NEW_ARRAY_LITERAL_INT: /* FALLTHRU */
        {
            int size = GET_2BYTE_INT(&code[pc+1]);
            MVM_Object *array;

            restore_pc(mvm, exe, func, pc);
            array = create_array_literal_int(mvm, size);
            mvm->stack.stack_pointer -= size;
            STO_WRITE(mvm, 0, array);
            mvm->stack.stack_pointer++;
            pc += 3;
            break;
        }
        case MVM_NEW_ARRAY_LITERAL_DOUBLE: /* FALLTHRU */
        {
            int size = GET_2BYTE_INT(&code[pc+1]);
            MVM_Object *array;

            restore_pc(mvm, exe, func, pc);
            array = create_array_literal_double(mvm, size);
            mvm->stack.stack_pointer -= size;
            STO_WRITE(mvm, 0, array);
            mvm->stack.stack_pointer++;
            pc += 3;
            break;
        }
        case MVM_NEW_ARRAY_LITERAL_OBJECT: /* FALLTHRU */
        {
            int size = GET_2BYTE_INT(&code[pc+1]);
            MVM_Object *array;

            restore_pc(mvm, exe, func, pc);
            array = create_array_literal_object(mvm, size);
            mvm->stack.stack_pointer -= size;
            STO_WRITE(mvm, 0, array);
            mvm->stack.stack_pointer++;
            pc += 3;
            break;
        }
        default:
            break;
        }
        /* MEM_check_all_blocks(); */
    }

    //return ret;
}
Пример #5
0
/* This is the messy part. This is what happens when you copy-paste one thing that works make the rest of it work */
int execute(int **mem, int *pc, int flow, int *areg, int *breg, int *creg){
	int i;
	int *tryte;
	tryte = mem[*pc];

	/* output instructions */
	if (tryte[0]==-1&&tryte[1]==-1&&tryte[2]==0){
		if(flow) puts("OUT A ");
		printf("PRINT %d\n",tern2dec(areg));
		*pc+=1;
	}else if (tryte[0]==0&&tryte[1]==-1&&tryte[2]==0){
		if(flow) puts("OUT B ");
		printf("PRINT %d\n",tern2dec(breg));
		*pc+=1;
	}else if (tryte[0]==1&&tryte[1]==-1&&tryte[2]==0){
		if(flow) puts("OUT C ");
		printf("PRINT %d\n",tern2dec(creg));
		*pc+=1;

	/* input instructions */
	}else if (tryte[0]==-1&&tryte[1]==0&&tryte[2]==1){
		if(flow) puts("IN A ");
		printf("IN A? :");
		scanf("%d",&i);
		dec2tern(i);
		for(i=0;i<WIDTH;i++){
			areg[i] = itryte[i];
		}
		*pc+=1;
	}else if (tryte[0]==0&&tryte[1]==0&&tryte[2]==1){
		if(flow) puts("IN B ");
		printf("IN B? :");
		scanf("%d",&i);
		dec2tern(i);
		for(i=0;i<WIDTH;i++){
			breg[i] = itryte[i];
		}
		*pc+=1;
	}else if (tryte[0]==1&&tryte[1]==0&&tryte[2]==1){
		if(flow) puts("IN C ");
		scanf("%d",&i);
		dec2tern(i);
		for(i=0;i<WIDTH;i++){
			creg[i] = itryte[i];
		}
		*pc+=1;

		*pc+=1;

	/* load instructions */
	}else if (tryte[0]==-1&&tryte[1]==-1&&tryte[2]==-1){
		if(flow) puts("LD A ");
		for(i=0;i<WIDTH;i++){
			areg[i]=mem[364+tern2dec(mem[*pc+1])][i];
		}
		*pc+=2;
	}else if (tryte[0]==0&&tryte[1]==-1&&tryte[2]==-1){
		if(flow) puts("LD B ");
		for(i=0;i<WIDTH;i++){
			breg[i]=mem[364+tern2dec(mem[*pc+1])][i];
		}
		*pc+=2;
	}else if (tryte[0]==1&&tryte[1]==-1&&tryte[2]==-1){
		if(flow) puts("LD C ");
		for(i=0;i<WIDTH;i++){
			creg[i]=mem[364+tern2dec(mem[*pc+1])][i];
		}
		*pc+=2;

	/* increment instructions */
	}else if (tryte[0]==-1 &&tryte[1]==0 &&tryte[2]==-1){
		if(flow) puts("INC A");
		incrTryte(areg);
		*pc+=1;
	}else if (tryte[0]==0 &&tryte[1]==0 &&tryte[2]==-1){
		if(flow) puts("INC B");
		incrTryte(breg);
		*pc+=1;
	}else if (tryte[0]==1 &&tryte[1]==0 &&tryte[2]==-1){
		if(flow) puts("INC C");
		incrTryte(creg);
		*pc+=1;

	/* conditional skip instructions */
	}else if (tryte[0]==-1&&tryte[1]==1&&tryte[2]==-1){
		if(flow) puts("SKP A ");
		if(condSkip(areg)) *pc+=3;
		else *pc+=1;
	}else if (tryte[0]==0&&tryte[1]==1&&tryte[2]==-1){
		if(flow) puts("SKP B ");
		if(condSkip(breg)) *pc+=3;
		else *pc+=1;
	}else if (tryte[0]==1&&tryte[1]==1&&tryte[2]==-1){
		if(flow) puts("SKP C ");
		if(condSkip(creg)) *pc+=3;
		else *pc+=1;

	/* 3 ADD instructions */
	}else if(tryte[0]==-1 &&tryte[1]==1 &&tryte[2]==1){
		if(flow) puts("ADD A");
		addTryte(areg,mem[364+tern2dec(mem[*pc+1])]);
		*pc+=2;

	}else if(tryte[0]==0 &&tryte[1]==1 &&tryte[2]==1){
		if(flow) puts("ADD B");
		addTryte(breg,mem[364+tern2dec(mem[*pc+1])]);
		*pc+=2;

	}else if(tryte[0]==1 &&tryte[1]==1 &&tryte[2]==1){
		if(flow) puts("ADD C");
		addTryte(creg,mem[364+tern2dec(mem[*pc+1])]);
		*pc+=2;

	/* 3 STO instructions */
	}else if(tryte[0]==-1 &&tryte[1]==-1 &&tryte[2]==1){
		if(flow) puts("STO A");
		STO(areg,mem[364+tern2dec(mem[*pc+1])]);
		*pc+=2;
	}else if(tryte[0]==0 &&tryte[1]==-1 &&tryte[2]==1){
		if(flow) puts("STO B");
		STO(breg,mem[364+tern2dec(mem[*pc+1])]);
		*pc+=2;
	}else if(tryte[0]==1 &&tryte[1]==-1 &&tryte[2]==1){
		if(flow) puts("STO C");
		STO(creg,mem[364+tern2dec(mem[*pc+1])]);
		*pc+=2;

	/* HALT Instruction */
	}else if(tryte[0]==-1 &&tryte[1]==0 &&tryte[2]==0){
		if(flow) puts("HALT");
		return 1;

	/* Bitwise invert instructions */
	}else if(tryte[0]==-1 &&tryte[1]==1 &&tryte[2]==0){
		if(flow) puts("INV A");
		invTryte(areg);
		*pc+=1;
	}else if(tryte[0]==0 &&tryte[1]==1 &&tryte[2]==0){
		if(flow) puts("INV B");
		invTryte(breg);
		*pc+=1;
	}else if(tryte[0]==1 &&tryte[1]==1 &&tryte[2]==0){
		if(flow) puts("INV C");
		invTryte(creg);
		*pc+=1;

	/* Unconditional JP instruction */
	}else if (tryte[0]==1&&tryte[1]==0&&tryte[2]==0){
		if(flow) puts("JP");
		JP(mem[*pc+1],pc);
		
	/* NOOP instruction */
	}else if (tryte[0]==0&&tryte[1]==0&&tryte[2]==0){
		if(flow) puts("NOOP");
		*pc+=1;
	}else{ 
		if(flow){
			dispTryte(tryte);
			puts("?");
		}
		*pc+=1;
	}

	return 0;
}