Skip to content

Commit

Permalink
Implement live variable analysis
Browse files Browse the repository at this point in the history
Implement the basics of variable life analysis.
Define the range from start to end offset where a variable is used and
allocates space for used variables. Also defines which variables need to
be cleared before use.
Also moves consant values to a lower offset on the stack if possibles.

Signed-off-by: Adam Laszlo Kulcsar <kuladam@inf.u-szeged.hu>
  • Loading branch information
kulcsaradam committed Jul 23, 2024
1 parent b947a7d commit f99de50
Show file tree
Hide file tree
Showing 8 changed files with 2,465 additions and 112 deletions.
84 changes: 84 additions & 0 deletions src/interpreter/ByteCode.h
Original file line number Diff line number Diff line change
Expand Up @@ -767,6 +767,7 @@ class BinaryOperation : public ByteCode {
const ByteCodeStackOffset* srcOffset() const { return m_srcOffset; }
ByteCodeStackOffset dstOffset() const { return m_dstOffset; }
void setDstOffset(ByteCodeStackOffset o) { m_dstOffset = o; }
void setSrcOffset(ByteCodeStackOffset o, size_t index) { m_srcOffset[index] = o; }
#if !defined(NDEBUG)
void dump(size_t pos)
{
Expand Down Expand Up @@ -809,6 +810,14 @@ class UnaryOperation : public ByteCode {
}
ByteCodeStackOffset srcOffset() const { return m_srcOffset; }
ByteCodeStackOffset dstOffset() const { return m_dstOffset; }
void setDstOffset(ByteCodeStackOffset newOffset)
{
m_dstOffset = newOffset;
}
void setSrcOffset(ByteCodeStackOffset newOffset)
{
m_srcOffset = newOffset;
}
#if !defined(NDEBUG)
void dump(size_t pos)
{
Expand Down Expand Up @@ -860,7 +869,9 @@ class Move : public ByteCode {
}

ByteCodeStackOffset srcOffset() const { return m_srcOffset; }
void setSrcOffset(ByteCodeStackOffset o) { m_srcOffset = o; }
ByteCodeStackOffset dstOffset() const { return m_dstOffset; }
void setDstOffset(ByteCodeStackOffset o) { m_dstOffset = o; }

protected:
ByteCodeStackOffset m_srcOffset;
Expand Down Expand Up @@ -1015,7 +1026,9 @@ class Load32 : public ByteCode {
}

ByteCodeStackOffset srcOffset() const { return m_srcOffset; }
void setSrcOffset(ByteCodeStackOffset o) { m_srcOffset = o; }
ByteCodeStackOffset dstOffset() const { return m_dstOffset; }
void setDstOffset(ByteCodeStackOffset o) { m_dstOffset = o; }

#if !defined(NDEBUG)
void dump(size_t pos)
Expand Down Expand Up @@ -1067,7 +1080,9 @@ class Store32 : public ByteCode {
}

ByteCodeStackOffset src0Offset() const { return m_src0Offset; }
void setSrc0Offset(ByteCodeStackOffset o) { m_src0Offset = o; }
ByteCodeStackOffset src1Offset() const { return m_src1Offset; }
void setSrc1Offset(ByteCodeStackOffset o) { m_src1Offset = o; }

#if !defined(NDEBUG)
void dump(size_t pos)
Expand Down Expand Up @@ -1144,6 +1159,7 @@ class JumpIfTrue : public ByteCode {
}

ByteCodeStackOffset srcOffset() const { return m_srcOffset; }
void setSrcOffset(ByteCodeStackOffset o) { m_srcOffset = o; }
int32_t offset() const { return m_offset; }
void setOffset(int32_t offset)
{
Expand Down Expand Up @@ -1174,6 +1190,7 @@ class JumpIfFalse : public ByteCode {
}

ByteCodeStackOffset srcOffset() const { return m_srcOffset; }
void setSrcOffset(ByteCodeStackOffset o) { m_srcOffset = o; }
int32_t offset() const { return m_offset; }
void setOffset(int32_t offset)
{
Expand Down Expand Up @@ -1208,11 +1225,15 @@ class Select : public ByteCode {
}

ByteCodeStackOffset condOffset() const { return m_condOffset; }
void setCondOffset(ByteCodeStackOffset o) { m_condOffset = o; }
uint16_t valueSize() const { return m_valueSize; }
bool isFloat() const { return m_isFloat != 0; }
ByteCodeStackOffset src0Offset() const { return m_src0Offset; }
void setSrc0Offset(ByteCodeStackOffset o) { m_src0Offset = o; }
ByteCodeStackOffset src1Offset() const { return m_src1Offset; }
void setSrc1Offset(ByteCodeStackOffset o) { m_src1Offset = o; }
ByteCodeStackOffset dstOffset() const { return m_dstOffset; }
void setDstOffset(ByteCodeStackOffset o) { m_dstOffset = o; }

#if !defined(NDEBUG)
void dump(size_t pos)
Expand Down Expand Up @@ -1245,6 +1266,7 @@ class BrTable : public ByteCode {
}

ByteCodeStackOffset condOffset() const { return m_condOffset; }
void setCondOffset(ByteCodeStackOffset o) { m_condOffset = o; }
int32_t defaultOffset() const { return m_defaultOffset; }
static inline size_t offsetOfDefault() { return offsetof(BrTable, m_defaultOffset); }

Expand Down Expand Up @@ -1283,6 +1305,7 @@ class MemorySize : public ByteCode {
}

ByteCodeStackOffset dstOffset() const { return m_dstOffset; }
void setDstOffset(ByteCodeStackOffset o) { m_dstOffset = o; }

#if !defined(NDEBUG)
void dump(size_t pos)
Expand Down Expand Up @@ -1315,6 +1338,10 @@ class MemoryInit : public ByteCode {
{
return m_srcOffsets;
}
void setSrcOffset(ByteCodeStackOffset o, size_t idx)
{
m_srcOffsets[idx] = o;
}

#if !defined(NDEBUG)
void dump(size_t pos)
Expand Down Expand Up @@ -1346,6 +1373,10 @@ class MemoryCopy : public ByteCode {
{
return m_srcOffsets;
}
void setSrcOffset(ByteCodeStackOffset o, size_t idx)
{
m_srcOffsets[idx] = o;
}

#if !defined(NDEBUG)
void dump(size_t pos)
Expand Down Expand Up @@ -1373,6 +1404,10 @@ class MemoryFill : public ByteCode {
{
return m_srcOffsets;
}
void setSrcOffset(ByteCodeStackOffset o, size_t idx)
{
m_srcOffsets[idx] = o;
}

#if !defined(NDEBUG)
void dump(size_t pos)
Expand Down Expand Up @@ -1423,7 +1458,9 @@ class MemoryGrow : public ByteCode {
}

ByteCodeStackOffset srcOffset() const { return m_srcOffset; }
void setSrcOffset(ByteCodeStackOffset o) { m_srcOffset = o; }
ByteCodeStackOffset dstOffset() const { return m_dstOffset; }
void setDstOffset(ByteCodeStackOffset o) { m_dstOffset = o; }

#if !defined(NDEBUG)
void dump(size_t pos)
Expand Down Expand Up @@ -1452,7 +1489,9 @@ class MemoryLoad : public ByteCode {

uint32_t offset() const { return m_offset; }
ByteCodeStackOffset srcOffset() const { return m_srcOffset; }
void setSrcOffset(ByteCodeStackOffset o) { m_srcOffset = o; }
ByteCodeStackOffset dstOffset() const { return m_dstOffset; }
void setDstOffset(ByteCodeStackOffset o) { m_dstOffset = o; }

#if !defined(NDEBUG)
void dump(size_t pos)
Expand Down Expand Up @@ -1481,8 +1520,11 @@ class SIMDMemoryLoad : public ByteCode {
uint32_t offset() const { return m_offset; }
ByteCodeStackOffset index() const { return m_index; }
ByteCodeStackOffset src0Offset() const { return m_src0Offset; }
void setSrc0Offset(ByteCodeStackOffset o) { m_src0Offset = o; }
ByteCodeStackOffset src1Offset() const { return m_src1Offset; }
void setSrc1Offset(ByteCodeStackOffset o) { m_src1Offset = o; }
ByteCodeStackOffset dstOffset() const { return m_dstOffset; }
void setDstOffset(ByteCodeStackOffset o) { m_dstOffset = o; }

#if !defined(NDEBUG)
void dump(size_t pos)
Expand Down Expand Up @@ -1550,7 +1592,9 @@ class MemoryStore : public ByteCode {

uint32_t offset() const { return m_offset; }
ByteCodeStackOffset src0Offset() const { return m_src0Offset; }
void setSrc0Offset(ByteCodeStackOffset o) { m_src0Offset = o; }
ByteCodeStackOffset src1Offset() const { return m_src1Offset; }
void setSrc1Offset(ByteCodeStackOffset o) { m_src1Offset = o; }

#if !defined(NDEBUG)
void dump(size_t pos)
Expand All @@ -1577,8 +1621,11 @@ class SIMDMemoryStore : public ByteCode {

uint32_t offset() const { return m_offset; }
ByteCodeStackOffset index() const { return m_index; }
void setIndex(ByteCodeStackOffset o) { m_index = o; }
ByteCodeStackOffset src0Offset() const { return m_src0Offset; }
void setSrc0Offset(ByteCodeStackOffset o) { m_src0Offset = o; }
ByteCodeStackOffset src1Offset() const { return m_src1Offset; }
void setSrc1Offset(ByteCodeStackOffset o) { m_src1Offset = o; }

#if !defined(NDEBUG)
void dump(size_t pos)
Expand All @@ -1604,8 +1651,11 @@ class SIMDExtractLane : public ByteCode {
}

ByteCodeStackOffset index() const { return m_index; }
void setIndex(ByteCodeStackOffset o) { m_index = o; }
ByteCodeStackOffset srcOffset() const { return m_srcOffset; }
void setSrcOffset(ByteCodeStackOffset o) { m_srcOffset = o; }
ByteCodeStackOffset dstOffset() const { return m_dstOffset; }
void setDstOffset(ByteCodeStackOffset o) { m_dstOffset = o; }

#if !defined(NDEBUG)
void dump(size_t pos)
Expand All @@ -1631,7 +1681,9 @@ class SIMDReplaceLane : public ByteCode {

uint32_t index() const { return m_index; }
const ByteCodeStackOffset* srcOffsets() const { return m_srcOffsets; }
void setSrcOffset(ByteCodeStackOffset o, size_t idx) { m_srcOffsets[idx] = o; }
ByteCodeStackOffset dstOffset() const { return m_dstOffset; }
void setDstOffset(ByteCodeStackOffset o) { m_dstOffset = o; }

#if !defined(NDEBUG)
void dump(size_t pos)
Expand Down Expand Up @@ -1954,7 +2006,12 @@ class V128BitSelect : public ByteCode {
{
return m_srcOffsets;
}
void setSrcOffset(ByteCodeStackOffset o, size_t idx)
{
m_srcOffsets[idx] = o;
}
ByteCodeStackOffset dstOffset() const { return m_dstOffset; }
void setDstOffset(ByteCodeStackOffset o) { m_dstOffset = o; }

#if !defined(NDEBUG)
void dump(size_t pos)
Expand Down Expand Up @@ -2010,7 +2067,9 @@ class I8X16Shuffle : public ByteCode {
}

const ByteCodeStackOffset* srcOffsets() const { return m_srcOffsets; }
void setSrcOffset(ByteCodeStackOffset o, size_t idx) { m_srcOffsets[idx] = o; }
ByteCodeStackOffset dstOffset() const { return m_dstOffset; }
void setDstOffset(ByteCodeStackOffset o) { m_dstOffset = o; }
const uint8_t* value() const { return m_value; }

#if !defined(NDEBUG)
Expand All @@ -2037,7 +2096,9 @@ class TableGet : public ByteCode {

uint32_t tableIndex() const { return m_tableIndex; }
ByteCodeStackOffset srcOffset() const { return m_srcOffset; }
void setSrcOffset(ByteCodeStackOffset o) { m_srcOffset = o; }
ByteCodeStackOffset dstOffset() const { return m_dstOffset; }
void setDstOffset(ByteCodeStackOffset o) { m_dstOffset = o; }

#if !defined(NDEBUG)
void dump(size_t pos)
Expand Down Expand Up @@ -2066,7 +2127,9 @@ class TableSet : public ByteCode {
}

ByteCodeStackOffset src0Offset() const { return m_src0Offset; }
void setSrc0Offset(ByteCodeStackOffset o) { m_src0Offset = o; }
ByteCodeStackOffset src1Offset() const { return m_src1Offset; }
void setSrc1Offset(ByteCodeStackOffset o) { m_src1Offset = o; }
uint32_t tableIndex() const { return m_tableIndex; }

#if !defined(NDEBUG)
Expand Down Expand Up @@ -2098,8 +2161,11 @@ class TableGrow : public ByteCode {

uint32_t tableIndex() const { return m_tableIndex; }
ByteCodeStackOffset src0Offset() const { return m_src0Offset; }
void setSrc0Offset(ByteCodeStackOffset o) { m_src0Offset = o; }
ByteCodeStackOffset src1Offset() const { return m_src1Offset; }
void setSrc1Offset(ByteCodeStackOffset o) { m_src1Offset = o; }
ByteCodeStackOffset dstOffset() const { return m_dstOffset; }
void setDstOffset(ByteCodeStackOffset o) { m_dstOffset = o; }

#if !defined(NDEBUG)
void dump(size_t pos)
Expand Down Expand Up @@ -2130,6 +2196,7 @@ class TableSize : public ByteCode {

uint32_t tableIndex() const { return m_tableIndex; }
ByteCodeStackOffset dstOffset() const { return m_dstOffset; }
void setDstOffset(ByteCodeStackOffset o) { m_dstOffset = o; }

#if !defined(NDEBUG)
void dump(size_t pos)
Expand Down Expand Up @@ -2161,6 +2228,10 @@ class TableCopy : public ByteCode {
{
return m_srcOffsets;
}
void setSrcOffset(ByteCodeStackOffset o, size_t idx)
{
m_srcOffsets[idx] = o;
}

#if !defined(NDEBUG)
void dump(size_t pos)
Expand Down Expand Up @@ -2193,6 +2264,11 @@ class TableFill : public ByteCode {
{
return m_srcOffsets;
}
void setSrcOffset(ByteCodeStackOffset o, size_t idx)
{
m_srcOffsets[idx] = o;
}

#if !defined(NDEBUG)
void dump(size_t pos)
{
Expand Down Expand Up @@ -2225,6 +2301,11 @@ class TableInit : public ByteCode {
{
return m_srcOffsets;
}
void setSrcOffset(ByteCodeStackOffset o, size_t idx)
{
m_srcOffsets[idx] = o;
}

#if !defined(NDEBUG)
void dump(size_t pos)
{
Expand Down Expand Up @@ -2276,6 +2357,7 @@ class RefFunc : public ByteCode {
}

ByteCodeStackOffset dstOffset() const { return m_dstOffset; }
void setDstOffset(ByteCodeStackOffset o) { m_dstOffset = o; }
uint32_t funcIndex() const { return m_funcIndex; }

#if !defined(NDEBUG)
Expand All @@ -2302,6 +2384,7 @@ class GlobalGet32 : public ByteCode {
}

ByteCodeStackOffset dstOffset() const { return m_dstOffset; }
void setDstOffset(ByteCodeStackOffset o) { m_dstOffset = o; }
uint32_t index() const { return m_index; }

#if !defined(NDEBUG)
Expand Down Expand Up @@ -2382,6 +2465,7 @@ class GlobalSet32 : public ByteCode {
{
}

void setSrcOffset(ByteCodeStackOffset o) { m_srcOffset = o; }
ByteCodeStackOffset srcOffset() const { return m_srcOffset; }
uint32_t index() const { return m_index; }

Expand Down
Loading

0 comments on commit f99de50

Please sign in to comment.