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 <[email protected]>
  • Loading branch information
kulcsaradam committed Aug 7, 2024
1 parent 75dc3e4 commit 889b81a
Show file tree
Hide file tree
Showing 8 changed files with 2,541 additions and 165 deletions.
67 changes: 67 additions & 0 deletions src/interpreter/ByteCode.h
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,8 @@ class ByteCodeOffset2 : public ByteCode {

ByteCodeStackOffset stackOffset1() const { return m_stackOffset1; }
ByteCodeStackOffset stackOffset2() const { return m_stackOffset2; }
void setStackOffset1(ByteCodeStackOffset o) { m_stackOffset1 = o; }
void setStackOffset2(ByteCodeStackOffset o) { m_stackOffset2 = o; }

protected:
ByteCodeStackOffset m_stackOffset1;
Expand All @@ -687,6 +689,7 @@ class ByteCodeOffsetValue : public ByteCode {
}

ByteCodeStackOffset stackOffset() const { return m_stackOffset; }
void setStackOffset(ByteCodeStackOffset o) { m_stackOffset = o; }
uint32_t uint32Value() const { return m_value; }
int32_t int32Value() const { return static_cast<int32_t>(m_value); }

Expand Down Expand Up @@ -802,6 +805,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 @@ -1188,11 +1192,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 @@ -1225,6 +1233,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 @@ -1263,6 +1272,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 @@ -1295,6 +1305,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 @@ -1326,6 +1340,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 @@ -1353,6 +1371,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 @@ -1403,7 +1425,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 @@ -1432,7 +1456,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 @@ -1461,8 +1487,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 @@ -1530,7 +1559,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 @@ -1557,8 +1588,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 @@ -1584,8 +1618,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 @@ -1611,7 +1648,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 @@ -1934,7 +1973,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 @@ -1990,7 +2034,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 @@ -2017,7 +2063,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 @@ -2046,7 +2094,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 @@ -2078,8 +2128,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 @@ -2135,6 +2188,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 @@ -2167,6 +2224,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 @@ -2199,6 +2261,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
Loading

0 comments on commit 889b81a

Please sign in to comment.