Skip to content
Permalink
Browse files

- added implicit initialization of reused register variables in ZScript

  • Loading branch information...
alexey-lysiuk authored and madame-rachelle committed Jun 1, 2019
1 parent d3e6ed3 commit 6f1df5b5abd9b9abd82971f40a1ad8e9465fcd8d
Showing with 57 additions and 2 deletions.
  1. +45 −2 src/scripting/backend/codegen.cpp
  2. +4 −0 src/scripting/backend/vmbuilder.cpp
  3. +8 −0 src/scripting/backend/vmbuilder.h
@@ -11350,8 +11350,51 @@ ExpEmit FxLocalVariableDeclaration::Emit(VMFunctionBuilder *build)
{
if (RegNum == -1)
{
if (!(VarFlags & VARF_Out)) RegNum = build->Registers[ValueType->GetRegType()].Get(RegCount);
else RegNum = build->Registers[REGT_POINTER].Get(1);
if (!(VarFlags & VARF_Out))
{
const int regType = ValueType->GetRegType();
assert(regType <= REGT_TYPE);

auto& registers = build->Registers[regType];
RegNum = registers.Get(RegCount);

for (int reg = RegNum, end = RegNum + RegCount; reg < end; ++reg)
{
if (!registers.IsDirty(reg))
{
continue;
}

ScriptPosition.Message(MSG_DEBUGMSG, "Implicit initialization of variable %s\n", Name.GetChars());

switch (regType)
{
case REGT_INT:
build->Emit(OP_LI, reg, 0, 0);
break;

case REGT_FLOAT:
build->Emit(OP_LKF, reg, build->GetConstantFloat(0.0));
break;

case REGT_STRING:
build->Emit(OP_LKS, reg, build->GetConstantString(nullptr));
break;

case REGT_POINTER:
build->Emit(OP_LKP, reg, build->GetConstantAddress(nullptr));
break;

default:
assert(false);
break;
}
}
}
else
{
RegNum = build->Registers[REGT_POINTER].Get(1);
}
}
}
else
@@ -367,6 +367,7 @@ void VMFunctionBuilder::ParamChange(int delta)
VMFunctionBuilder::RegAvailability::RegAvailability()
{
memset(Used, 0, sizeof(Used));
memset(Dirty, 0, sizeof(Dirty));
MostUsed = 0;
}

@@ -493,16 +494,19 @@ void VMFunctionBuilder::RegAvailability::Return(int reg, int count)
// because for that case it pushes the self pointer a second time without reallocating, so it gets freed twice.
//assert((Used[firstword] & mask) == mask);
Used[firstword] &= ~mask;
Dirty[firstword] |= mask;
}
else
{ // Range is in two words.
partialmask = mask << firstbit;
assert((Used[firstword] & partialmask) == partialmask);
Used[firstword] &= ~partialmask;
Dirty[firstword] |= partialmask;

partialmask = mask >> (32 - firstbit);
assert((Used[firstword + 1] & partialmask) == partialmask);
Used[firstword + 1] &= ~partialmask;
Dirty[firstword + 1] |= partialmask;
}
}

@@ -37,8 +37,16 @@ class VMFunctionBuilder
void Return(int reg, int count);
bool Reuse(int regnum);

bool IsDirty(int reg) const
{
const int firstword = reg / 32;
const int firstbit = reg & 31;
return Dirty[firstword] & (1 << firstbit);
}

private:
VM_UWORD Used[256/32]; // Bitmap of used registers (bit set means reg is used)
VM_UWORD Dirty[256/32];
int MostUsed;

friend class VMFunctionBuilder;

0 comments on commit 6f1df5b

Please sign in to comment.
You can’t perform that action at this time.