void Interpreter::loadFunParamsInCtx(uint16_t id) { BytecodeFunction* fun = ((BytecodeFunction*)_code->functionById(id)); uint16_t n = fun->parametersNumber(); FunctionContext* ctx = this->topContext(); //cout << "loading params: " << n << endl; //cout << "stack size " << _stack.size() << endl; for (uint16_t i = 0; i < n; ++i) { VarType type = fun->parameterType(i); switch (type) { case VT_INT: ctx->storeInt(ctx->getId(), i, popInt()); //cout << "int loaded from bc" << endl; break; case VT_DOUBLE: ctx->storeDouble(ctx->getId(), i, popDouble()); //cout << "double loaded from bc" << endl; break; default: //cout << "loading STRIG or INVALID from bc" << endl; assert(false); break; } } }
void BytecodeGenerator::visitCallNode(CallNode* node) { BytecodeFunction* funToCall = _state.bcFunByName(node->name()); assert(funToCall); Bytecode* bc = _state.currentBcToFill(); for(size_t i = node->parametersNumber(); i > 0; --i) { node->parameterAt(i - 1)->visit(this); VarType expectedArgType = funToCall->parameterType(i - 1); VarType actualArgType = nodeType(node->parameterAt(i - 1)); if(actualArgType != expectedArgType) { Instruction castInsn = actualArgType == VT_INT ? BC_I2D : BC_D2I; bc->addInsn(castInsn); } } bc->addInsn(BC_CALL); bc->addInt16(funToCall->id()); }
void BytecodeVisitor::visitCallNode(CallNode *node) { LOG_Visitor("visitCallNode"); BytecodeFunction *calledFunction = context->getFunction(node->name()); if (node->parametersNumber() != calledFunction->parametersNumber()) { throw TranslationError("Incorrect number of parameters at calling function " + calledFunction->name(), node->position()); } for (int32_t i = node->parametersNumber() - 1; i >= 0; --i) { uint32_t j = (uint32_t) i; node->parameterAt(j)->visit(this); cast(calledFunction->parameterType(j), node, "casting call-node parameters"); } uint16_t functionID = calledFunction->id(); bc()->addInsn(BC_CALL); bc()->addUInt16(functionID); topOfStackType = calledFunction->returnType(); }
void BytecodeVisitor::visitCallNode(CallNode *node) { BytecodeFunction* bf = (BytecodeFunction*) code_->functionByName(node->name()); if (!bf) { throw new runtime_error("Function does not exist"); } if (node->parametersNumber() != bf->parametersNumber()) { throw new runtime_error("Uncorrect parameters number"); } for (size_t i = node->parametersNumber(); node->parametersNumber() > 0 && i > 0; --i) { node->parameterAt(i-1)->visit(this); castTos(bf->parameterType(i-1)); } bc()->addInsn(BC_CALL); bc()->addUInt16(bf->id()); setTosType(bf->returnType()); }