void Generator::visitForNode(ForNode *node) { BinaryOpNode *bin = node->inExpr()->asBinaryOpNode(); assert(type(bin->left()) == VT_INT && type(bin->right()) == VT_INT); AstVar const * const var = node->var(); uint32_t jump, repeat; eval_int(bin->right()); eval_int(bin->left()); repeat = bytecode()->current(); dup_int(); save_variable(var); bytecode()->addInsn(BC_IFICMPG); jump = bytecode()->current(); bytecode()->addInt16(0); node->body()->visit(this); bytecode()->addInsn(BC_ILOAD1); bytecode()->addInsn(BC_IADD); bytecode()->addInsn(BC_JA); bytecode()->addInt16(repeat - bytecode()->current()); bytecode()->setInt16(jump, bytecode()->current() - jump); bytecode()->addInsn(BC_POP); bytecode()->addInsn(BC_POP); }
void BytecodeVisitor::visitForNode(ForNode *node) { LOG_Visitor("visitForNode"); VariableInContextDescriptor variableDescriptor = context->getVariableDescriptor(node->var()->name()); BinaryOpNode *innerExpression = (BinaryOpNode *) node->inExpr(); if (innerExpression->kind() != tRANGE) { throw TranslationError(string("Incorrect binary operation in for-expression. Exptected: RANGE, got: ") + tokenStr(innerExpression->kind()), node->position()); } if (node->var()->type() != VT_INT) { throw TranslationError(string("Incorrect type of for-variable. Exptected: INT, got: ") + typeToName(node->var()->type()), node->position()); } innerExpression->left()->visit(this); bc()->addInsn(BC_ILOADM1); bc()->addInsn(BC_IADD); storeVariable(variableDescriptor, innerExpression); Label begin(bc()); Label end(bc()); { bc()->bind(begin); loadVariable(variableDescriptor, innerExpression); bc()->addInsn(BC_ILOAD1); bc()->addInsn(BC_IADD); storeVariable(variableDescriptor, innerExpression); //condition innerExpression->right()->visit(this); loadVariable(variableDescriptor, innerExpression); bc()->addInsn(BC_SWAP); // goto end if greater bc()->addBranch(BC_IFICMPG, end); node->body()->visit(this); bc()->addBranch(BC_JA, begin); } bc()->bind(end); }
void ASTtoByteCodeTranslator::visitForNode(ForNode* node) { Bytecode *bytecode = funStack.top()->bytecode(); uint32_t mem1; BinaryOpNode *range = (BinaryOpNode*) node->inExpr(); range->right()->visit(this); range->left()->visit(this); mem1 = bytecode->current(); bytecode->addInsn(BC_STOREIVAR); bytecode->addInt16(varMap[node->var()]); node->body()->visit(this); bytecode->addInsn(BC_LOADIVAR); bytecode->addInt16(varMap[node->var()]); bytecode->addInsn(BC_ILOAD1); bytecode->addInsn(BC_IADD); bytecode->addInsn(BC_IFICMPLE); bytecode->addInt16(mem1 - bytecode->current()); }
void ByteCodeVisitor::visitForNode(mathvm::ForNode *node) { Label expr(bytecode()); Label outLabel(bytecode()); BinaryOpNode *binaryOpNode = node->inExpr()->asBinaryOpNode(); binaryOpNode->left()->visit(this); store(node->var()); bytecode()->bind(expr); load(node->var()); binaryOpNode->right()->visit(this); branch(BC_IFICMPL, outLabel); popStack(); popStack(); node->body()->visit(this); load(node->var()); insn(BC_ILOAD1); insn(BC_IADD); store(node->var()); branch(BC_JA, expr); bytecode()->bind(outLabel); }