Commit a4ace68b authored by Victor Yacovlev's avatar Victor Yacovlev

Fixed module initialization order

Current rules are:
1. Main module file (if several files) initializes at last
2. In-file modules initializes from topmost module to last
parent be9d01e9
......@@ -355,6 +355,7 @@ void Generator::addKumirModule(int id, const AST::Module *mod)
returnFromInit.type = Bytecode::RET;
initElem.type = Bytecode::EL_INIT;
initElem.module = quint8(id);
initElem.moduleName = mod->header.name.toStdWString();
initElem.instructions = instructions(id, -1, 0, mod->impl.initializerBody).toVector().toStdVector();
if (!initElem.instructions.empty())
initElem.instructions << returnFromInit;
......@@ -380,7 +381,7 @@ void Generator::addKumirModule(int id, const AST::Module *mod)
if (alg->header.specialType==AST::AlgorhitmTypeTesting) {
ft = Bytecode::EL_TESTING;
}
addFunction(i, id, ft, alg);
addFunction(i, id, ft, mod, alg);
}
if (mainMod && mainAlg) {
addInputArgumentsMainAlgorhitm(mainModId, mainAlgorhitmId, mainMod, mainAlg);
......@@ -599,7 +600,7 @@ QString typeSignature(const AST::Type & tp) {
return signature;
}
void Generator::addFunction(int id, int moduleId, Bytecode::ElemType type, const AST::Algorhitm *alg)
void Generator::addFunction(int id, int moduleId, Bytecode::ElemType type, const AST::Module * mod, const AST::Algorhitm *alg)
{
QString headerError = "";
QString beginError = "";
......@@ -661,6 +662,7 @@ void Generator::addFunction(int id, int moduleId, Bytecode::ElemType type, const
func.algId = func.id = id;
func.name = alg->header.name.toStdWString();
func.signature = signature.toStdWString();
func.moduleName = mod->header.name.toStdWString();
QList<Bytecode::Instruction> argHandle;
Bytecode::Instruction l;
......
......@@ -55,7 +55,7 @@ private:
return constantValue(type.toStdList(), dimension, value);
}
void addKumirModule(int id, const AST::Module * mod);
void addFunction(int id, int moduleId, Bytecode::ElemType type, const AST::Algorhitm * alg);
void addFunction(int id, int moduleId, Bytecode::ElemType type, const AST::Module * mod, const AST::Algorhitm * alg);
void addInputArgumentsMainAlgorhitm(int moduleId, int algorhitmId, const AST::Module * mod, const AST::Algorhitm * alg);
QList<Bytecode::Instruction> instructions(
......
......@@ -534,13 +534,13 @@ void KumirVM::reset()
// Each kumir module have 'initialization' section,
// so push all these sections (if any) into stack
// to call them BEFORE startup context
for (size_t moduleContextNo=0;
moduleContextNo<moduleContexts.size();
moduleContextNo++)
for (int moduleContextNo=moduleContexts.size()-1;
moduleContextNo>=0;
moduleContextNo--)
{
for (FunctionMap::iterator it=moduleContexts[moduleContextNo].inits.begin();
it!=moduleContexts[moduleContextNo].inits.end();
++it)
for (FunctionMap::iterator it=moduleContexts[moduleContextNo].inits.end();
it!=moduleContexts[moduleContextNo].inits.begin();
it--)
{
const TableElem & e = (*it).second;
if (e.instructions.size()>0) {
......
......@@ -237,11 +237,9 @@ inline void tableElemToBinaryStream(std::list<char> & ds, const TableElem &e)
}
valueToDataStream(ds, uint16_t(e.algId));
valueToDataStream(ds, uint16_t(e.id));
stringToDataStream(ds, e.name);
if (e.type==EL_EXTERN) {
stringToDataStream(ds, e.moduleName);
}
else if (e.type==EL_CONST) {
stringToDataStream(ds, e.name);
stringToDataStream(ds, e.moduleName);
if (e.type==EL_CONST) {
constantToDataStream(ds, e.vtype, e.initialValue, e.dimension);
}
else if (e.type==EL_FUNCTION || e.type==EL_MAIN || e.type==EL_TESTING || e.type==EL_BELOWMAIN || e.type==EL_INIT) {
......@@ -375,11 +373,9 @@ inline void tableElemFromBinaryStream(std::list<char> & ds, TableElem &e)
e.id = id;
stringFromDataStream(ds, s);
e.name = s;
if (e.type==EL_EXTERN) {
stringFromDataStream(ds, s);
e.moduleName = s;
}
else if (e.type==EL_CONST) {
stringFromDataStream(ds, s);
e.moduleName = s;
if (e.type==EL_CONST) {
constantFromDataStream(ds, e.vtype, e.initialValue, e.dimension);
}
else if (e.type==EL_FUNCTION || e.type==EL_MAIN || e.type==EL_TESTING || e.type==EL_BELOWMAIN || e.type==EL_INIT) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment