diff --git a/src/pim/Generator.cpp b/src/pim/Generator.cpp index 0cbdbfc1c..3f296b7ab 100644 --- a/src/pim/Generator.cpp +++ b/src/pim/Generator.cpp @@ -43,6 +43,15 @@ namespace pim program.push_back((constant>>24) & 0xFF); } + void Generator::writeConstantPlaceholderOffset(int value, int * offset) + { + valueOffsetPlaceholders.push_back(ValueOffsetPlaceholder(program.size(), std::pair(value, offset))); + program.push_back(0); + program.push_back(0); + program.push_back(0); + program.push_back(0); + } + void Generator::writeConstantPlaceholder(std::string label) { placeholders.push_back(Placeholder(program.size(), label)); @@ -118,6 +127,22 @@ namespace pim program[cPosition.first+3] = (value >> 24) & 0xFF; } + for(std::vector::iterator iter = valueOffsetPlaceholders.begin(), end = valueOffsetPlaceholders.end(); iter != end; ++iter) + { + ValueOffsetPlaceholder cPosition = *iter; + int value = cPosition.second.first; + int offset = *cPosition.second.second; + + std::cout << "Setting value placeholder at " << cPosition.first << " with " << value << " + " << offset << std::endl; + + value += offset; + + program[cPosition.first] = value & 0xFF; + program[cPosition.first+1] = (value >> 8) & 0xFF; + program[cPosition.first+2] = (value >> 16) & 0xFF; + program[cPosition.first+3] = (value >> 24) & 0xFF; + } + //Build file int macroSizePos, propSizePos, codeSizePos, macroSize = 0, propSize = 0, codeSize = program.size(); std::vector file; @@ -224,6 +249,8 @@ namespace pim scopes.push(currentScope); Scope * prevScope = currentScope; currentScope = new Scope(); + currentScope->FrameSize += 4; //Space for return address + currentScope->LocalFrameSize += 4; defineLabel(label); output << "." << label << std::endl; @@ -236,6 +263,7 @@ namespace pim currentScope = new Scope(); currentScope->Definitions.insert(currentScope->Definitions.begin(), prevScope->Definitions.begin(), prevScope->Definitions.end()); currentScope->FrameSize = prevScope->FrameSize; + currentScope->OldFrameSize = prevScope->OldFrameSize; defineLabel(label); output << "." << label << std::endl; @@ -243,16 +271,27 @@ namespace pim void Generator::PopScope() { - - writeOpcode(Opcode::Return); + writeOpcode(Opcode::Leave); writeConstant(currentScope->LocalFrameSize); - output << "return " << currentScope->LocalFrameSize << std::endl; + output << "leave " << currentScope->LocalFrameSize << std::endl; currentScope = scopes.top(); scopes.pop(); } + void Generator::ExitScope() + { + currentScope = scopes.top(); + scopes.pop(); + } + + void Generator::Return() + { + writeOpcode(Opcode::Return); + output << "return" << std::endl; + } + void Generator::ScopeLabel(std::string label) { //defineLabelwriteOpcode("." << label); @@ -294,12 +333,12 @@ namespace pim void Generator::ScopeVariable(std::string label) { - currentScope->Definitions.push_back(Definition(label, typeStack.top(), currentScope->FrameSize)); + currentScope->Definitions.push_back(Definition(label, variableType, currentScope->LocalFrameSize, currentScope)); currentScope->FrameSize += 4; currentScope->LocalFrameSize += 4; typeStack.pop(); - output << "#declare " << label << " " << currentScope->FrameSize-4 << std::endl; + output << "#declare " << label << " " << currentScope->LocalFrameSize-4 << std::endl; } void Generator::PushVariableAddress(std::string label) @@ -313,7 +352,8 @@ namespace pim pushType(d.Type); writeOpcode(Opcode::Load); - writeConstant(d.StackPosition); + + writeConstant(d.StackPosition+(currentScope->FrameSize-d.MyScope->FrameSize)); output << "load " << label << std::endl; } @@ -337,14 +377,17 @@ namespace pim popType(2); writeOpcode(Opcode::Store); - writeConstant(d.StackPosition); + + writeConstant(d.StackPosition+(currentScope->FrameSize-d.MyScope->FrameSize)); + //writeConstantPlaceholderOffset(currentScope->GetDefinition(label).StackPosition, &(currentScope->OldFrameSize)); + //writeConstant(currentScope->GetDefinition(label).StackPosition); output << "store " << label << std::endl; } void Generator::RTConstant(std::string name) { - pushType(Type::Integer); + pushType(DataType::Integer); writeOpcode(Opcode::Constant); writeConstantMacroPlaceholder(name); @@ -417,14 +460,14 @@ namespace pim { popType(1); pushType(DataType::Integer); - writeOpcode(OpCode::ToInteger); + writeOpcode(Opcode::ToInteger); } void Generator::ToFloat() { popType(1); pushType(DataType::Float); - writeOpcode(OpCode::ToFloat); + writeOpcode(Opcode::ToFloat); } void Generator::Divide() @@ -592,10 +635,5 @@ namespace pim } - void Generator::Return() - { - - } - } } \ No newline at end of file diff --git a/src/pim/Generator.h b/src/pim/Generator.h index db793dd57..ca20d6df5 100644 --- a/src/pim/Generator.h +++ b/src/pim/Generator.h @@ -37,16 +37,19 @@ namespace pim } ~SymbolNotFoundException() throw() {}; }; + class Scope; class Definition { public: std::string Name; int Type; int StackPosition; - Definition(std::string name, int type, int position) : + Scope * MyScope; + Definition(std::string name, int type, int position, Scope * myScope) : Type(type), Name(name), - StackPosition(position) + StackPosition(position), + MyScope(myScope) { } @@ -65,9 +68,11 @@ namespace pim std::vector