Skip to content

Commit

Permalink
Implement live variable analysis
Browse files Browse the repository at this point in the history
Implement live variable analysis for function parameters, variables and constants.
Define the range from start to end offset where a variable is used and allocate space for used variables. Also define which variables need to be cleared before use.
Move constant values to a lower offset on the stack if possibles.

Signed-off-by: Adam Laszlo Kulcsar <[email protected]>
  • Loading branch information
kulcsaradam committed Oct 31, 2024
1 parent f82a879 commit cc50def
Show file tree
Hide file tree
Showing 9 changed files with 2,942 additions and 171 deletions.
83 changes: 83 additions & 0 deletions src/interpreter/ByteCode.h
Original file line number Diff line number Diff line change
Expand Up @@ -699,6 +699,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 Down Expand Up @@ -732,6 +734,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 @@ -911,6 +914,7 @@ class BinaryOperation : public ByteCodeOffset3 {
const ByteCodeStackOffset* srcOffset() const { return stackOffsets(); }
ByteCodeStackOffset dstOffset() const { return stackOffset3(); }
void setDstOffset(ByteCodeStackOffset o) { m_stackOffsets[2] = o; }
void setSrcOffset(ByteCodeStackOffset o, size_t index) { m_stackOffsets[index] = o; }
#if !defined(NDEBUG)
void dump(size_t pos)
{
Expand Down Expand Up @@ -984,6 +988,16 @@ class TernaryOperation : public ByteCodeOffset4 {
{
}

void setDstOffset(ByteCodeStackOffset o)
{
m_stackOffsets[3] = o;
}

void setStackOffset(ByteCodeStackOffset o, uint32_t index)
{
m_stackOffsets[index] = o;
}

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

ByteCodeStackOffset calleeOffset() const { return m_calleeOffset; }
void setCalleeOffset(ByteCodeStackOffset o) { m_calleeOffset = o; }
uint32_t tableIndex() const { return m_tableIndex; }
FunctionType* functionType() const { return m_functionType; }
ByteCodeStackOffset* stackOffsets() const
Expand Down Expand Up @@ -1334,11 +1349,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 @@ -1371,6 +1390,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 @@ -1409,6 +1429,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 @@ -1441,6 +1462,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 @@ -1471,6 +1496,10 @@ class MemoryCopy : public ByteCodeOffset3 {
{
return stackOffsets();
}
void setSrcOffset(ByteCodeStackOffset o, size_t idx)
{
m_stackOffsets[idx] = o;
}

#if !defined(NDEBUG)
void dump(size_t pos)
Expand All @@ -1495,6 +1524,10 @@ class MemoryFill : public ByteCodeOffset3 {
{
return stackOffsets();
}
void setSrcOffset(ByteCodeStackOffset o, size_t idx)
{
m_stackOffsets[idx] = o;
}

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

ByteCodeStackOffset srcOffset() const { return stackOffset1(); }
void setSrcOffset(ByteCodeStackOffset o) { m_stackOffset1 = o; }
ByteCodeStackOffset dstOffset() const { return stackOffset2(); }
void setDstOffset(ByteCodeStackOffset o) { m_stackOffset2 = o; }

#if !defined(NDEBUG)
void dump(size_t pos)
Expand All @@ -1564,6 +1599,8 @@ class MemoryLoad : public ByteCodeOffset2Value {
uint32_t offset() const { return uint32Value(); }
ByteCodeStackOffset srcOffset() const { return stackOffset1(); }
ByteCodeStackOffset dstOffset() const { return stackOffset2(); }
void setSrcOffset(ByteCodeStackOffset o) { m_stackOffset1 = o; }
void setDstOffset(ByteCodeStackOffset o) { m_stackOffset2 = o; }

#if !defined(NDEBUG)
void dump(size_t pos)
Expand All @@ -1588,8 +1625,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 @@ -1655,6 +1695,8 @@ class MemoryStore : public ByteCodeOffset2Value {
uint32_t offset() const { return uint32Value(); }
ByteCodeStackOffset src0Offset() const { return stackOffset1(); }
ByteCodeStackOffset src1Offset() const { return stackOffset2(); }
void setSrc0Offset(ByteCodeStackOffset o) { m_stackOffset1 = o; }
void setSrc1Offset(ByteCodeStackOffset o) { m_stackOffset2 = o; }

#if !defined(NDEBUG)
void dump(size_t pos)
Expand All @@ -1677,8 +1719,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 @@ -1704,8 +1749,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 @@ -1731,7 +1779,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 @@ -2011,6 +2061,14 @@ class V128BitSelect : public ByteCodeOffset4 {
{
}


void setSrcOffset(ByteCodeStackOffset o, size_t idx)
{
m_stackOffsets[idx] = o;
}

void setDstOffset(ByteCodeStackOffset o) { m_stackOffsets[3] = o; }

#if !defined(NDEBUG)
void dump(size_t pos)
{
Expand Down Expand Up @@ -2061,7 +2119,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 @@ -2087,6 +2147,9 @@ class TableGet : public ByteCodeOffset2Value {
ByteCodeStackOffset dstOffset() const { return stackOffset2(); }
uint32_t tableIndex() const { return uint32Value(); }

void setSrcOffset(ByteCodeStackOffset o) { m_stackOffset1 = o; }
void setDstOffset(ByteCodeStackOffset o) { m_stackOffset2 = o; }

#if !defined(NDEBUG)
void dump(size_t pos)
{
Expand All @@ -2105,9 +2168,12 @@ class TableSet : public ByteCodeOffset2Value {
{
}


ByteCodeStackOffset src0Offset() const { return stackOffset1(); }
ByteCodeStackOffset src1Offset() const { return stackOffset2(); }
uint32_t tableIndex() const { return uint32Value(); }
void setSrc0Offset(ByteCodeStackOffset o) { m_stackOffset1 = o; }
void setSrc1Offset(ByteCodeStackOffset o) { m_stackOffset2 = o; }

#if !defined(NDEBUG)
void dump(size_t pos)
Expand All @@ -2133,8 +2199,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 @@ -2190,6 +2259,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 @@ -2222,6 +2295,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 @@ -2254,6 +2332,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 cc50def

Please sign in to comment.