From d22d1136be5362b4bfb35848ecbac1e34c7bb2cf Mon Sep 17 00:00:00 2001 From: QFSW Date: Tue, 26 Mar 2019 16:35:22 +0000 Subject: [PATCH] Added sizeof support for stackallocs --- include/codegen/allocationFrame.hpp | 1 + include/codegen/allocator.hpp | 1 + include/context/mipsContext.hpp | 1 + src/codegen/allocationFrame.cpp | 7 +++++++ src/codegen/allocator.cpp | 13 +++++++++++++ src/context/mipsContext.cpp | 13 +++++++++++++ 6 files changed, 36 insertions(+) diff --git a/include/codegen/allocationFrame.hpp b/include/codegen/allocationFrame.hpp index 5ebb504..dd4eb47 100644 --- a/include/codegen/allocationFrame.hpp +++ b/include/codegen/allocationFrame.hpp @@ -9,6 +9,7 @@ class AllocationFrame public: void allocate(Allocation allocation); int getAllocationOffset(std::string name) const; + int getAllocationSize(std::string name) const; bool isAllocated(std::string name) const; int getFrameSize() const; diff --git a/include/codegen/allocator.hpp b/include/codegen/allocator.hpp index 1429d47..a93e71a 100644 --- a/include/codegen/allocator.hpp +++ b/include/codegen/allocator.hpp @@ -9,6 +9,7 @@ class Allocator public: void allocate(Allocation allocation); int getAllocationOffset(std::string name) const; + int getAllocationSize(std::string name) const; bool isAllocated(std::string name) const; void popFrame(); void pushFrame(); diff --git a/include/context/mipsContext.hpp b/include/context/mipsContext.hpp index 05eb5c0..545f3fb 100644 --- a/include/context/mipsContext.hpp +++ b/include/context/mipsContext.hpp @@ -28,6 +28,7 @@ class MIPSContext : public Context void loadAddress(std::string varName, std::string destReg); void postProcessInstrs(); void removeGlobalInits(std::string symbol); + int getAllocationSize(std::string regName) const; std::string getAllocationLocation(std::string regName) const; const Allocator& getAllocator() const; diff --git a/src/codegen/allocationFrame.cpp b/src/codegen/allocationFrame.cpp index 37846d1..3c7e59f 100644 --- a/src/codegen/allocationFrame.cpp +++ b/src/codegen/allocationFrame.cpp @@ -16,6 +16,13 @@ int AllocationFrame::getAllocationOffset(std::string name) const return _allocationOffsets[pos]; } +int AllocationFrame::getAllocationSize(std::string name) const +{ + int pos = getAllocationPosition(name); + if (pos < 0) { throw "allocation " + name + " could not be found"; } + return _allocations[pos].size; +} + bool AllocationFrame::isAllocated(std::string name) const { return getAllocationPosition(name) != -1; diff --git a/src/codegen/allocator.cpp b/src/codegen/allocator.cpp index 5fea19d..0e7b507 100644 --- a/src/codegen/allocator.cpp +++ b/src/codegen/allocator.cpp @@ -20,6 +20,19 @@ int Allocator::getAllocationOffset(std::string name) const throw "allocation " + name + " could not be found"; } +int Allocator::getAllocationSize(std::string name) const +{ + for (int i = _frames.size() - 1; i >= 0; --i) + { + if (_frames[i].isAllocated(name)) + { + return _frames[i].getAllocationSize(name); + } + } + + throw "allocation " + name + " could not be found"; +} + bool Allocator::isAllocated(std::string name) const { for (size_t i = 0; i < _frames.size(); ++i) diff --git a/src/context/mipsContext.cpp b/src/context/mipsContext.cpp index 8f15c0c..e19ff51 100644 --- a/src/context/mipsContext.cpp +++ b/src/context/mipsContext.cpp @@ -265,6 +265,19 @@ std::string MIPSContext::getAllocationLocation(std::string regName) const } } +int MIPSContext::getAllocationSize(std::string regName) const +{ + if (_globals.count(regName) == 0) + { + return 4; + } + else + { + if (_arrays.count(regName) == 0) { return _allocator.getAllocationSize(regName); } + else { return _allocator.getAllocationSize("__" + regName + "__"); } + } +} + void MIPSContext::loadAddress(std::string destReg, std::string varName) { if (_globals.count(varName) == 0)