Skip to content

Commit

Permalink
- extended the register limit for 'param'.
Browse files Browse the repository at this point in the history
The instruction one free instruction byte so it's now using that to extend its argument's register range to 65535.
For param this is needed because it passes strings by reference and creating an implicit temporary copy for string constants does not work here.
  • Loading branch information
coelckers committed Oct 14, 2018
1 parent eee0946 commit dd719f0
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 113 deletions.
40 changes: 20 additions & 20 deletions src/d_dehacked.cpp
Expand Up @@ -632,26 +632,26 @@ static int GetLine (void)
// misc1 = vrange (arg +3), misc2 = hrange (arg+4)
static int CreateMushroomFunc(VMFunctionBuilder &buildit, int value1, int value2)
{ // A_Mushroom
buildit.Emit(OP_PARAM, 0, REGT_NIL, 0); // spawntype
buildit.Emit(OP_PARAM, 0, REGT_NIL, 0); // numspawns
buildit.Emit(OP_PARAM, REGT_NIL, 0); // spawntype
buildit.Emit(OP_PARAM, REGT_NIL, 0); // numspawns
buildit.Emit(OP_PARAMI, 1); // flag
// vrange
if (value1 == 0)
{
buildit.Emit(OP_PARAM, 0, REGT_NIL, 0);
buildit.Emit(OP_PARAM, REGT_NIL, 0);
}
else
{
buildit.Emit(OP_PARAM, 0, REGT_FLOAT | REGT_KONST, buildit.GetConstantFloat(DEHToDouble(value1)));
buildit.Emit(OP_PARAM, REGT_FLOAT | REGT_KONST, buildit.GetConstantFloat(DEHToDouble(value1)));
}
// hrange
if (value2 == 0)
{
buildit.Emit(OP_PARAM, 0, REGT_NIL, 0);
buildit.Emit(OP_PARAM, REGT_NIL, 0);
}
else
{
buildit.Emit(OP_PARAM, 0, REGT_FLOAT | REGT_KONST, buildit.GetConstantFloat(DEHToDouble(value2)));
buildit.Emit(OP_PARAM, REGT_FLOAT | REGT_KONST, buildit.GetConstantFloat(DEHToDouble(value2)));
}
return 5;
}
Expand All @@ -667,24 +667,24 @@ static int CreateSpawnFunc(VMFunctionBuilder &buildit, int value1, int value2)
int typereg = buildit.GetConstantAddress(InfoNames[value1-1]);
int heightreg = buildit.GetConstantFloat(value2);

buildit.Emit(OP_PARAM, 0, REGT_POINTER | REGT_KONST, typereg); // itemtype
buildit.Emit(OP_PARAM, 0, REGT_NIL, 0); // distance
buildit.Emit(OP_PARAM, 0, REGT_FLOAT | REGT_KONST, heightreg); // height
buildit.Emit(OP_PARAM, REGT_POINTER | REGT_KONST, typereg); // itemtype
buildit.Emit(OP_PARAM, REGT_NIL, 0); // distance
buildit.Emit(OP_PARAM, REGT_FLOAT | REGT_KONST, heightreg); // height
// The rest of the parameters to A_SpawnItem can just keep their defaults
return 3;
}

// misc1 = angle (in degrees) (arg +0 but factor in current actor angle too)
static int CreateTurnFunc(VMFunctionBuilder &buildit, int value1, int value2)
{ // A_Turn
buildit.Emit(OP_PARAM, 0, REGT_FLOAT | REGT_KONST, buildit.GetConstantFloat(value1)); // angle
buildit.Emit(OP_PARAM, REGT_FLOAT | REGT_KONST, buildit.GetConstantFloat(value1)); // angle
return 1;
}

// misc1 = angle (in degrees) (arg +0)
static int CreateFaceFunc(VMFunctionBuilder &buildit, int value1, int value2)
{ // A_FaceTarget
buildit.Emit(OP_PARAM, 0, REGT_FLOAT | REGT_KONST, buildit.GetConstantFloat(value1)); // angle
buildit.Emit(OP_PARAM, REGT_FLOAT | REGT_KONST, buildit.GetConstantFloat(value1)); // angle
return 1;
}

Expand All @@ -708,9 +708,9 @@ static int CreatePlaySoundFunc(VMFunctionBuilder &buildit, int value1, int value

buildit.EmitParamInt(SoundMap[value1-1]); // soundid
buildit.Emit(OP_PARAMI, CHAN_BODY); // channel
buildit.Emit(OP_PARAM, 0, REGT_FLOAT | REGT_KONST, float1); // volume
buildit.Emit(OP_PARAM, REGT_FLOAT | REGT_KONST, float1); // volume
buildit.Emit(OP_PARAMI, false); // looping
buildit.Emit(OP_PARAM, 0, REGT_FLOAT | REGT_KONST, attenreg); // attenuation
buildit.Emit(OP_PARAM, REGT_FLOAT | REGT_KONST, attenreg); // attenuation
return 5;
}

Expand All @@ -720,7 +720,7 @@ static int CreateRandomJumpFunc(VMFunctionBuilder &buildit, int value1, int valu
int statereg = buildit.GetConstantAddress(FindState(value1));

buildit.EmitParamInt(value2); // maxchance
buildit.Emit(OP_PARAM, 0, REGT_POINTER | REGT_KONST, statereg); // jumpto
buildit.Emit(OP_PARAM, REGT_POINTER | REGT_KONST, statereg); // jumpto
return 2;
}

Expand All @@ -742,11 +742,11 @@ static int CreateNailBombFunc(VMFunctionBuilder &buildit, int value1, int value2
{ // A_Explode
// This one does not actually have MBF-style parameters. But since
// we're aliasing it to an extension of A_Explode...
buildit.Emit(OP_PARAM, 0, REGT_NIL, 0); // damage
buildit.Emit(OP_PARAM, 0, REGT_NIL, 0); // distance
buildit.Emit(OP_PARAM, 0, REGT_NIL, 0); // flags
buildit.Emit(OP_PARAM, 0, REGT_NIL, 0); // alert
buildit.Emit(OP_PARAM, 0, REGT_NIL, 0); // fulldamagedistance
buildit.Emit(OP_PARAM, REGT_NIL, 0); // damage
buildit.Emit(OP_PARAM, REGT_NIL, 0); // distance
buildit.Emit(OP_PARAM, REGT_NIL, 0); // flags
buildit.Emit(OP_PARAM, REGT_NIL, 0); // alert
buildit.Emit(OP_PARAM, REGT_NIL, 0); // fulldamagedistance
buildit.Emit(OP_PARAMI, 30); // nails
buildit.Emit(OP_PARAMI, 10); // naildamage
return 7;
Expand Down Expand Up @@ -802,7 +802,7 @@ void SetDehParams(FState *state, int codepointer)
// Emit code to pass the standard action function parameters.
for (int i = 0; i < numargs; i++)
{
buildit.Emit(OP_PARAM, 0, REGT_POINTER, i);
buildit.Emit(OP_PARAM, REGT_POINTER, i);
}
// Emit code for action parameters.
int argcount = MBFCodePointerFactories[codepointer](buildit, value1, value2);
Expand Down
44 changes: 22 additions & 22 deletions src/scripting/backend/codegen.cpp
Expand Up @@ -519,12 +519,12 @@ static int EmitParameter(VMFunctionBuilder *build, FxExpression *operand, const
if (where.RegType == REGT_NIL)
{
pos.Message(MSG_ERROR, "Attempted to pass a non-value");
build->Emit(OP_PARAM, 0, where.RegType, where.RegNum);
build->Emit(OP_PARAM, where.RegType, where.RegNum);
return 1;
}
else
{
build->Emit(OP_PARAM, 0, EncodeRegType(where), where.RegNum);
build->Emit(OP_PARAM, EncodeRegType(where), where.RegNum);
if (tempstrings != nullptr && where.RegType == REGT_STRING && !where.Fixed && !where.Konst)
{
tempstrings->Push(where); // keep temp strings until after the function call.
Expand Down Expand Up @@ -5549,7 +5549,7 @@ ExpEmit FxRandom::Emit(VMFunctionBuilder *build)
if (build->FramePointer.Fixed) EmitTail = false; // do not tail call if the stack is in use
int opcode = (EmitTail ? OP_TAIL_K : OP_CALL_K);

build->Emit(OP_PARAM, 0, REGT_POINTER | REGT_KONST, build->GetConstantAddress(rng));
build->Emit(OP_PARAM, REGT_POINTER | REGT_KONST, build->GetConstantAddress(rng));
if (min != nullptr && max != nullptr)
{
EmitParameter(build, min, ScriptPosition);
Expand Down Expand Up @@ -5670,7 +5670,7 @@ ExpEmit FxRandomPick::Emit(VMFunctionBuilder *build)
assert(((PSymbolVMFunction *)sym)->Function != nullptr);
callfunc = ((PSymbolVMFunction *)sym)->Function;

build->Emit(OP_PARAM, 0, REGT_POINTER | REGT_KONST, build->GetConstantAddress(rng));
build->Emit(OP_PARAM, REGT_POINTER | REGT_KONST, build->GetConstantAddress(rng));
build->EmitParamInt(0);
build->EmitParamInt(choices.Size() - 1);
build->Emit(OP_CALL_K, build->GetConstantAddress(callfunc), 3, 1);
Expand Down Expand Up @@ -5800,7 +5800,7 @@ ExpEmit FxFRandom::Emit(VMFunctionBuilder *build)
if (build->FramePointer.Fixed) EmitTail = false; // do not tail call if the stack is in use
int opcode = (EmitTail ? OP_TAIL_K : OP_CALL_K);

build->Emit(OP_PARAM, 0, REGT_POINTER | REGT_KONST, build->GetConstantAddress(rng));
build->Emit(OP_PARAM, REGT_POINTER | REGT_KONST, build->GetConstantAddress(rng));
if (min != nullptr && max != nullptr)
{
EmitParameter(build, min, ScriptPosition);
Expand Down Expand Up @@ -5895,7 +5895,7 @@ ExpEmit FxRandom2::Emit(VMFunctionBuilder *build)
if (build->FramePointer.Fixed) EmitTail = false; // do not tail call if the stack is in use
int opcode = (EmitTail ? OP_TAIL_K : OP_CALL_K);

build->Emit(OP_PARAM, 0, REGT_POINTER | REGT_KONST, build->GetConstantAddress(rng));
build->Emit(OP_PARAM, REGT_POINTER | REGT_KONST, build->GetConstantAddress(rng));
EmitParameter(build, mask, ScriptPosition);
build->Emit(opcode, build->GetConstantAddress(callfunc), 2, 1);

Expand Down Expand Up @@ -5978,7 +5978,7 @@ ExpEmit FxRandomSeed::Emit(VMFunctionBuilder *build)
if (build->FramePointer.Fixed) EmitTail = false; // do not tail call if the stack is in use
int opcode = (EmitTail ? OP_TAIL_K : OP_CALL_K);

build->Emit(OP_PARAM, 0, REGT_POINTER | REGT_KONST, build->GetConstantAddress(rng));
build->Emit(OP_PARAM, REGT_POINTER | REGT_KONST, build->GetConstantAddress(rng));
EmitParameter(build, seed, ScriptPosition);
build->Emit(opcode, build->GetConstantAddress(callfunc), 2, 0);

Expand Down Expand Up @@ -8644,7 +8644,7 @@ ExpEmit FxActionSpecialCall::Emit(VMFunctionBuilder *build)
build->Emit(OP_PARAMI, abs(Special)); // pass special number

ExpEmit selfemit(Self->Emit(build));
build->Emit(OP_PARAM, 0, selfemit.Konst ? REGT_POINTER | REGT_KONST : REGT_POINTER, selfemit.RegNum); // pass special number
build->Emit(OP_PARAM, selfemit.Konst ? REGT_POINTER | REGT_KONST : REGT_POINTER, selfemit.RegNum); // pass special number
for (; i < ArgList.Size(); ++i)
{
FxExpression *argex = ArgList[i];
Expand All @@ -8664,7 +8664,7 @@ ExpEmit FxActionSpecialCall::Emit(VMFunctionBuilder *build)
else
{
ExpEmit arg(argex->Emit(build));
build->Emit(OP_PARAM, 0, arg.RegType, arg.RegNum);
build->Emit(OP_PARAM, arg.RegType, arg.RegNum);
arg.Free(build);
}
}
Expand Down Expand Up @@ -9086,26 +9086,26 @@ ExpEmit FxVMFunctionCall::Emit(VMFunctionBuilder *build)
if ((selfemit.Fixed && selfemit.Target) || selfemit.RegType == REGT_STRING)
{
// Address of a local variable.
build->Emit(OP_PARAM, 0, selfemit.RegType | REGT_ADDROF, selfemit.RegNum);
build->Emit(OP_PARAM, selfemit.RegType | REGT_ADDROF, selfemit.RegNum);
}
else
{
build->Emit(OP_PARAM, 0, selfemit.RegType, selfemit.RegNum);
build->Emit(OP_PARAM, selfemit.RegType, selfemit.RegNum);
}
count += 1;
if (Function->Variants[0].Flags & VARF_Action)
{
static_assert(NAP == 3, "This code needs to be updated if NAP changes");
if (build->NumImplicits == NAP && selfemit.RegNum == 0) // only pass this function's stateowner and stateinfo if the subfunction is run in self's context.
{
build->Emit(OP_PARAM, 0, REGT_POINTER, 1);
build->Emit(OP_PARAM, 0, REGT_POINTER, 2);
build->Emit(OP_PARAM, REGT_POINTER, 1);
build->Emit(OP_PARAM, REGT_POINTER, 2);
}
else
{
// pass self as stateowner, otherwise all attempts of the subfunction to retrieve a state from a name would fail.
build->Emit(OP_PARAM, 0, selfemit.RegType, selfemit.RegNum);
build->Emit(OP_PARAM, 0, REGT_POINTER | REGT_KONST, build->GetConstantAddress(nullptr));
build->Emit(OP_PARAM, selfemit.RegType, selfemit.RegNum);
build->Emit(OP_PARAM, REGT_POINTER | REGT_KONST, build->GetConstantAddress(nullptr));
}
count += 2;
}
Expand Down Expand Up @@ -10696,7 +10696,7 @@ ExpEmit FxReturnStatement::Emit(VMFunctionBuilder *build)
assert(pstr->mDestructor != nullptr);
ExpEmit reg(build, REGT_POINTER);
build->Emit(OP_ADDA_RK, reg.RegNum, build->FramePointer.RegNum, build->GetConstantInt(build->ConstructedStructs[i]->StackOffset));
build->Emit(OP_PARAM, 0, reg.RegType, reg.RegNum);
build->Emit(OP_PARAM, reg.RegType, reg.RegNum);
build->Emit(OP_CALL_K, build->GetConstantAddress(pstr->mDestructor), 1, 0);
reg.Free(build);
}
Expand Down Expand Up @@ -10893,8 +10893,8 @@ ExpEmit FxClassTypeCast::Emit(VMFunctionBuilder *build)
ExpEmit clsname = basex->Emit(build);
assert(!clsname.Konst);
ExpEmit dest(build, REGT_POINTER);
build->Emit(OP_PARAM, 0, clsname.RegType, clsname.RegNum);
build->Emit(OP_PARAM, 0, REGT_POINTER | REGT_KONST, build->GetConstantAddress(const_cast<PClass *>(desttype)));
build->Emit(OP_PARAM, clsname.RegType, clsname.RegNum);
build->Emit(OP_PARAM, REGT_POINTER | REGT_KONST, build->GetConstantAddress(const_cast<PClass *>(desttype)));

// Call the BuiltinNameToClass function to convert from 'name' to class.
VMFunction *callfunc;
Expand Down Expand Up @@ -11002,8 +11002,8 @@ ExpEmit FxClassPtrCast::Emit(VMFunctionBuilder *build)
{
ExpEmit clsname = basex->Emit(build);

build->Emit(OP_PARAM, 0, clsname.RegType, clsname.RegNum);
build->Emit(OP_PARAM, 0, REGT_POINTER | REGT_KONST, build->GetConstantAddress(desttype));
build->Emit(OP_PARAM, clsname.RegType, clsname.RegNum);
build->Emit(OP_PARAM, REGT_POINTER | REGT_KONST, build->GetConstantAddress(desttype));

VMFunction *callfunc;
PSymbol *sym = FindBuiltinFunction(NAME_BuiltinClassCast, BuiltinClassCast);
Expand Down Expand Up @@ -11388,7 +11388,7 @@ ExpEmit FxLocalVariableDeclaration::Emit(VMFunctionBuilder *build)
{
ExpEmit reg(build, REGT_POINTER);
build->Emit(OP_ADDA_RK, reg.RegNum, build->FramePointer.RegNum, build->GetConstantInt(StackOffset));
build->Emit(OP_PARAM, 0, reg.RegType, reg.RegNum);
build->Emit(OP_PARAM, reg.RegType, reg.RegNum);
build->Emit(OP_CALL_K, build->GetConstantAddress(pstr->mConstructor), 1, 0);
reg.Free(build);
}
Expand All @@ -11414,7 +11414,7 @@ void FxLocalVariableDeclaration::Release(VMFunctionBuilder *build)
{
ExpEmit reg(build, REGT_POINTER);
build->Emit(OP_ADDA_RK, reg.RegNum, build->FramePointer.RegNum, build->GetConstantInt(StackOffset));
build->Emit(OP_PARAM, 0, reg.RegType, reg.RegNum);
build->Emit(OP_PARAM, reg.RegType, reg.RegNum);
build->Emit(OP_CALL_K, build->GetConstantAddress(pstr->mDestructor), 1, 0);
reg.Free(build);
}
Expand Down
50 changes: 20 additions & 30 deletions src/scripting/backend/vmbuilder.cpp
Expand Up @@ -593,39 +593,19 @@ size_t VMFunctionBuilder::Emit(int opcode, int opa, int opb, int opc)
}
if (opc > 255)
{
if (opcode == OP_PARAM && (opb & REGT_KONST) && opc <= 32767)
if (opRemap[opcode].kReg != 4 || opc > 32767)
{
int regtype = opb & REGT_TYPE;
opb = regtype;
ExpEmit emit(this, regtype);
Emit(opcodes[regtype], emit.RegNum, opc);
opc = emit.RegNum;
emit.Free(this);
}
else
{
if (opRemap[opcode].kReg != 4 || opc > 32767)
{
I_Error("Register limit exceeded");
}
int regtype = opRemap[opcode].kType;
ExpEmit emit(this, regtype);
Emit(opcodes[regtype], emit.RegNum, opc);
opcode = opRemap[opcode].altOp;
opc = emit.RegNum;
emit.Free(this);
I_Error("Register limit exceeded");
}
int regtype = opRemap[opcode].kType;
ExpEmit emit(this, regtype);
Emit(opcodes[regtype], emit.RegNum, opc);
opcode = opRemap[opcode].altOp;
opc = emit.RegNum;
emit.Free(this);
}

if (opcode == OP_PARAM)
{
int chg;
if (opb & REGT_MULTIREG2) chg = 2;
else if (opb&REGT_MULTIREG3) chg = 3;
else chg = 1;
ParamChange(chg);
}
else if (opcode == OP_CALL || opcode == OP_CALL_K || opcode == OP_TAIL || opcode == OP_TAIL_K)
if (opcode == OP_CALL || opcode == OP_CALL_K || opcode == OP_TAIL || opcode == OP_TAIL_K)
{
ParamChange(-opb);
}
Expand All @@ -641,6 +621,16 @@ size_t VMFunctionBuilder::Emit(int opcode, int opa, VM_SHALF opbc)
{
assert(opcode >= 0 && opcode < NUM_OPS);
assert(opa >= 0 && opa <= 255);

if (opcode == OP_PARAM)
{
int chg;
if (opa & REGT_MULTIREG2) chg = 2;
else if (opa & REGT_MULTIREG3) chg = 3;
else chg = 1;
ParamChange(chg);
}

//assert(opbc >= -32768 && opbc <= 32767); always true due to parameter's width
VMOP op;
op.op = opcode;
Expand Down Expand Up @@ -681,7 +671,7 @@ size_t VMFunctionBuilder::EmitParamInt(int value)
}
else
{
return Emit(OP_PARAM, 0, REGT_INT | REGT_KONST, GetConstantInt(value));
return Emit(OP_PARAM, REGT_INT | REGT_KONST, GetConstantInt(value));
}
}

Expand Down

0 comments on commit dd719f0

Please sign in to comment.