From 004710fcead479aced1c2501afdfcc8718500e6b Mon Sep 17 00:00:00 2001 From: Simon Robertshaw Date: Thu, 31 Jan 2013 23:35:16 +0000 Subject: [PATCH] Keep track of variable types --- src/cat/LuaScriptInterface.cpp | 4 +- src/pim/Generator.cpp | 89 ++++++++++++++++++++++++++++++++-- src/pim/Generator.h | 12 +++-- src/pim/Machine.cpp | 1 + src/pim/Opcodes.h | 1 + src/pim/Opcodes.inl | 2 + src/pim/Parser.cpp | 29 +++++++++-- src/pim/Types.h | 13 +++++ 8 files changed, 139 insertions(+), 12 deletions(-) create mode 100644 src/pim/Types.h diff --git a/src/cat/LuaScriptInterface.cpp b/src/cat/LuaScriptInterface.cpp index 78a9ff025..19110c523 100644 --- a/src/cat/LuaScriptInterface.cpp +++ b/src/cat/LuaScriptInterface.cpp @@ -958,8 +958,8 @@ int LuaScriptInterface::updateVM(UPDATE_FUNC_ARGS) machine->CSPush(i); machine->CSPush(x); machine->CSPush(y); - //machine->Call(0); - machine->CallCompiled(0); + machine->Call(0); + //machine->CallCompiled(0); /*vm::VirtualMachine * vMachine = updateVirtualMachines[parts[i].type]; diff --git a/src/pim/Generator.cpp b/src/pim/Generator.cpp index a79121182..0cbdbfc1c 100644 --- a/src/pim/Generator.cpp +++ b/src/pim/Generator.cpp @@ -274,11 +274,30 @@ namespace pim variableType = type; } + void Generator::PushType(int type) + { + typeStack.push(type); + } + + void Generator::pushType(int type) + { + typeStack.push(type); + } + + void Generator::popType(int count) + { + while(count--) + { + typeStack.pop(); + } + } + void Generator::ScopeVariable(std::string label) { - currentScope->Definitions.push_back(Definition(label, variableType, currentScope->FrameSize)); + currentScope->Definitions.push_back(Definition(label, typeStack.top(), currentScope->FrameSize)); currentScope->FrameSize += 4; currentScope->LocalFrameSize += 4; + typeStack.pop(); output << "#declare " << label << " " << currentScope->FrameSize-4 << std::endl; } @@ -290,22 +309,42 @@ namespace pim void Generator::LoadVariable(std::string label) { + Definition d = currentScope->GetDefinition(label); + + pushType(d.Type); writeOpcode(Opcode::Load); - writeConstant(currentScope->GetDefinition(label).StackPosition); + writeConstant(d.StackPosition); output << "load " << label << std::endl; } void Generator::StoreVariable(std::string label) { + Definition d = currentScope->GetDefinition(label); + int otherType = typeStack.top(); + + if(otherType != d.Type) + { + if(otherType == DataType::Integer && d.Type == DataType::Float) + { + ToFloat(); + } + else if(otherType == DataType::Float && d.Type == DataType::Integer) + { + ToInteger(); + } + } + popType(2); + writeOpcode(Opcode::Store); - writeConstant(currentScope->GetDefinition(label).StackPosition); + writeConstant(d.StackPosition); output << "store " << label << std::endl; } void Generator::RTConstant(std::string name) { + pushType(Type::Integer); writeOpcode(Opcode::Constant); writeConstantMacroPlaceholder(name); @@ -314,6 +353,7 @@ namespace pim void Generator::Constant(std::string constant) { + pushType(DataType::Integer); writeOpcode(Opcode::Constant); writeConstant(constant); @@ -331,6 +371,7 @@ namespace pim void Generator::Discard() { + popType(1); writeOpcode(Opcode::Discard); output << "discard" << std::endl; @@ -338,6 +379,7 @@ namespace pim void Generator::Duplicate() { + pushType(typeStack.top()); writeOpcode(Opcode::Duplicate); output << "duplicate" << std::endl; @@ -345,6 +387,8 @@ namespace pim void Generator::Add() { + popType(2); + pushType(DataType::Integer); writeOpcode(Opcode::Add); output << "add" << std::endl; @@ -352,6 +396,8 @@ namespace pim void Generator::Subtract() { + popType(2); + pushType(DataType::Integer); writeOpcode(Opcode::Subtract); output << "sub" << std::endl; @@ -359,13 +405,32 @@ namespace pim void Generator::Multiply() { + popType(2); + pushType(DataType::Integer); writeOpcode(Opcode::Multiply); + output << "mul" << std::endl; } + void Generator::ToInteger() + { + popType(1); + pushType(DataType::Integer); + writeOpcode(OpCode::ToInteger); + } + + void Generator::ToFloat() + { + popType(1); + pushType(DataType::Float); + writeOpcode(OpCode::ToFloat); + } + void Generator::Divide() { + popType(2); + pushType(DataType::Integer); writeOpcode(Opcode::Divide); output << "div" << std::endl; @@ -374,6 +439,8 @@ namespace pim void Generator::Modulus() { writeOpcode(Opcode::Modulus); + popType(2); + pushType(DataType::Integer); output << "add" << std::endl; } @@ -401,6 +468,8 @@ namespace pim void Generator::GetParticle() { + popType(2); + pushType(DataType::Integer); writeOpcode(Opcode::Get); output << "getpart" << std::endl; @@ -408,6 +477,9 @@ namespace pim void Generator::GetPosition() { + popType(1); + pushType(DataType::Integer); + pushType(DataType::Integer); writeOpcode(Opcode::Position); output << "getpos" << std::endl; @@ -415,6 +487,7 @@ namespace pim void Generator::KillParticle() { + popType(1); writeOpcode(Opcode::Kill); output << "kill" << std::endl; @@ -422,6 +495,8 @@ namespace pim void Generator::LoadProperty(std::string property) { + popType(2); + pushType(DataType::Integer); writeOpcode(Opcode::LoadProperty); writeConstantPropertyPlaceholder(property); @@ -430,6 +505,7 @@ namespace pim void Generator::StoreProperty(std::string property) { + popType(2); writeOpcode(Opcode::StoreProperty); writeConstantPropertyPlaceholder(property); @@ -449,6 +525,7 @@ namespace pim void Generator::JumpEqual(std::string label) { + popType(2); writeOpcode(Opcode::JumpEqual); writeConstantPlaceholder(label); @@ -457,6 +534,7 @@ namespace pim void Generator::JumpNotEqual(std::string label) { + popType(2); writeOpcode(Opcode::JumpNotEqual); writeConstantPlaceholder(label); @@ -465,6 +543,7 @@ namespace pim void Generator::JumpGreater(std::string label) { + popType(2); writeOpcode(Opcode::JumpGreater); writeConstantPlaceholder(label); @@ -473,6 +552,7 @@ namespace pim void Generator::JumpGreaterEqual(std::string label) { + popType(2); writeOpcode(Opcode::JumpGreaterEqual); writeConstantPlaceholder(label); @@ -481,6 +561,7 @@ namespace pim void Generator::JumpLess(std::string label) { + popType(2); writeOpcode(Opcode::JumpLess); writeConstantPlaceholder(label); @@ -489,6 +570,7 @@ namespace pim void Generator::JumpLessEqual(std::string label) { + popType(2); writeOpcode(Opcode::JumpLessEqual); writeConstantPlaceholder(label); @@ -497,6 +579,7 @@ namespace pim void Generator::Jump(std::string label) { + popType(2); writeOpcode(Opcode::Jump); writeConstantPlaceholder(label); diff --git a/src/pim/Generator.h b/src/pim/Generator.h index 2eb1029f1..db793dd57 100644 --- a/src/pim/Generator.h +++ b/src/pim/Generator.h @@ -5,6 +5,7 @@ #include #include #include "Token.h" +#include "Types.h" namespace pim { namespace compiler @@ -36,10 +37,6 @@ namespace pim } ~SymbolNotFoundException() throw() {}; }; - class Type - { - enum { Integer = Token::IntegerSymbol, Decimal = Token::DecimalSymbol }; - }; class Definition { public: @@ -88,6 +85,7 @@ namespace pim class Generator { int variableType; + std::stack typeStack; std::stack scopes; Scope * currentScope; std::ostream & output; @@ -110,6 +108,9 @@ namespace pim std::vector program; + void pushType(int type); + void popType(int count); + void defineLabel(std::string label); void writeOpcode(int opcode); void writeConstant(std::string constant); @@ -135,6 +136,7 @@ namespace pim void ScopeVariableType(int type); void ScopeVariable(std::string label); + void PushType(int type); void PushVariableAddress(std::string label); // void Store(); void LoadVariable(std::string label); @@ -151,6 +153,8 @@ namespace pim void Divide(); void Modulus(); void Negate(); + void ToInteger(); + void ToFloat(); void TransformParticle(); void CreateParticle(); diff --git a/src/pim/Machine.cpp b/src/pim/Machine.cpp index e0985fe28..a87ddb6c1 100644 --- a/src/pim/Machine.cpp +++ b/src/pim/Machine.cpp @@ -4,6 +4,7 @@ #include #include "Machine.h" #include "Opcodes.h" +#include "Types.h" #include "simulation/Simulation.h" #include "X86Native.h" namespace pim diff --git a/src/pim/Opcodes.h b/src/pim/Opcodes.h index 4e1a7ce61..eb2eb4a1a 100644 --- a/src/pim/Opcodes.h +++ b/src/pim/Opcodes.h @@ -2,6 +2,7 @@ namespace pim { struct Opcode { + public: enum { #define OPDEF(name) name, diff --git a/src/pim/Opcodes.inl b/src/pim/Opcodes.inl index 4b2829425..14ed1cf6e 100644 --- a/src/pim/Opcodes.inl +++ b/src/pim/Opcodes.inl @@ -1,4 +1,6 @@ OPDEF(Load) +OPDEF(ToFloat) +OPDEF(ToInteger) OPDEF(Store) OPDEF(Constant) OPDEF(Increment) diff --git a/src/pim/Parser.cpp b/src/pim/Parser.cpp index 4b164c897..9ad31032e 100644 --- a/src/pim/Parser.cpp +++ b/src/pim/Parser.cpp @@ -1,6 +1,7 @@ //Syntax analyser #include "Parser.h" #include "Format.h" +#include "Types.h" namespace pim { namespace compiler @@ -106,7 +107,18 @@ namespace pim */ void Parser::argument() { - generator->ScopeVariableType(token.Symbol); + int type; + switch(token.Symbol) + { + case Token::DecimalConstant: + type = DataType::Float; + break; + case Token::IntegerConstant: + case Token::ParticleConstant: + type = DataType::Integer; + break; + } + generator->ScopeVariableType(type); if(!accept(Token::IntegerSymbol)) if(!accept(Token::DecimalSymbol)) if(!accept(Token::ParticleSymbol)) @@ -130,7 +142,18 @@ namespace pim */ void Parser::declaration() { - generator->ScopeVariableType(token.Symbol); + int type; + switch(token.Symbol) + { + case Token::DecimalConstant: + type = DataType::Float; + break; + case Token::IntegerConstant: + case Token::ParticleConstant: + type = DataType::Integer; + break; + } + generator->ScopeVariableType(type); if(!accept(Token::IntegerSymbol)) if(!accept(Token::DecimalSymbol)) if(!accept(Token::ParticleSymbol)) @@ -296,7 +319,7 @@ namespace pim generator->PushLocalScope(loopLabel+"Start"); neighbourVariable = token.Source; expect(Token::Identifier); - generator->ScopeVariableType(Token::IntegerConstant); + generator->ScopeVariableType(DataType::Integer); generator->ScopeVariable(neighbourVariable); generator->ScopeVariable(xVar); generator->ScopeVariable(yVar); diff --git a/src/pim/Types.h b/src/pim/Types.h new file mode 100644 index 000000000..0bcf7cb49 --- /dev/null +++ b/src/pim/Types.h @@ -0,0 +1,13 @@ +#pragma once +namespace pim +{ + struct DataType + { + public: + enum + { + Integer, + Float + }; + }; +} \ No newline at end of file