Skip to content

Commit

Permalink
Added sizeof support for stackallocs
Browse files Browse the repository at this point in the history
  • Loading branch information
QFSW committed Mar 26, 2019
1 parent bd14b1f commit d22d113
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 0 deletions.
1 change: 1 addition & 0 deletions include/codegen/allocationFrame.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
1 change: 1 addition & 0 deletions include/codegen/allocator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
1 change: 1 addition & 0 deletions include/context/mipsContext.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
7 changes: 7 additions & 0 deletions src/codegen/allocationFrame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
13 changes: 13 additions & 0 deletions src/codegen/allocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
13 changes: 13 additions & 0 deletions src/context/mipsContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit d22d113

Please sign in to comment.