mirror of
https://github.com/ZDoom/qzdoom.git
synced 2024-12-15 15:01:42 +00:00
32a3f57a54
* Allow PFunction to work without a VMFunction being attached. * The Variant for a function must store the prototype itself instead of relying on the VMFunction it points to. Otherwise it would not be possible to reference a prototype during compilation of the function because it does not exist yet. * Give the variant a list of the function's argument's names, because these are also needed to compile the function. * create an anonymous function symbol when the function gets registered to the builder. At this point we have all the needed information to set it up correctly, but later this is no longer the case. This is the most convenient info to have here because it contains everything that's needed to compile the function in the proper context, so it has to be present when starting compilation. * added some preparations to implement special handling for weapons and custom inventory items, which can run action functions in another actor's context. This part is not active yet but the basics are present in SetImplicitArgs.
120 lines
2.9 KiB
C++
120 lines
2.9 KiB
C++
#ifndef VMUTIL_H
|
|
#define VMUTIL_H
|
|
|
|
#include "vm.h"
|
|
|
|
class VMFunctionBuilder
|
|
{
|
|
public:
|
|
// Keeps track of which registers are available by way of a bitmask table.
|
|
class RegAvailability
|
|
{
|
|
public:
|
|
RegAvailability();
|
|
int GetMostUsed() { return MostUsed; }
|
|
int Get(int count); // Returns the first register in the range
|
|
void Return(int reg, int count);
|
|
bool Reuse(int regnum);
|
|
|
|
private:
|
|
VM_UWORD Used[256/32]; // Bitmap of used registers (bit set means reg is used)
|
|
int MostUsed;
|
|
|
|
friend class VMFunctionBuilder;
|
|
};
|
|
|
|
VMFunctionBuilder(bool checkself = false);
|
|
~VMFunctionBuilder();
|
|
|
|
void MakeFunction(VMScriptFunction *func);
|
|
|
|
// Returns the constant register holding the value.
|
|
int GetConstantInt(int val);
|
|
int GetConstantFloat(double val);
|
|
int GetConstantAddress(void *ptr, VM_ATAG tag);
|
|
int GetConstantString(FString str);
|
|
|
|
// Returns the address of the next instruction to be emitted.
|
|
size_t GetAddress();
|
|
|
|
// Returns the address of the newly-emitted instruction.
|
|
size_t Emit(int opcode, int opa, int opb, int opc);
|
|
size_t Emit(int opcode, int opa, VM_SHALF opbc);
|
|
size_t Emit(int opcode, int opabc);
|
|
size_t EmitParamInt(int value);
|
|
size_t EmitLoadInt(int regnum, int value);
|
|
size_t EmitRetInt(int retnum, bool final, int value);
|
|
|
|
void Backpatch(size_t addr, size_t target);
|
|
void BackpatchToHere(size_t addr);
|
|
|
|
// Write out complete constant tables.
|
|
void FillIntConstants(int *konst);
|
|
void FillFloatConstants(double *konst);
|
|
void FillAddressConstants(FVoidObj *konst, VM_ATAG *tags);
|
|
void FillStringConstants(FString *strings);
|
|
|
|
// PARAM increases ActiveParam; CALL decreases it.
|
|
void ParamChange(int delta);
|
|
|
|
// Track available registers.
|
|
RegAvailability Registers[4];
|
|
|
|
// For use by DECORATE's self/stateowner sanitizer.
|
|
bool IsActionFunc;
|
|
|
|
private:
|
|
struct AddrKonst
|
|
{
|
|
int KonstNum;
|
|
VM_ATAG Tag;
|
|
};
|
|
// These map from the constant value to its position in the constant table.
|
|
TMap<int, int> IntConstants;
|
|
TMap<double, int> FloatConstants;
|
|
TMap<void *, AddrKonst> AddressConstants;
|
|
TMap<FString, int> StringConstants;
|
|
|
|
int NumIntConstants;
|
|
int NumFloatConstants;
|
|
int NumAddressConstants;
|
|
int NumStringConstants;
|
|
|
|
int MaxParam;
|
|
int ActiveParam;
|
|
|
|
TArray<VMOP> Code;
|
|
|
|
};
|
|
|
|
void DumpFunction(FILE *dump, VMScriptFunction *sfunc, const char *label, int labellen);
|
|
|
|
|
|
//==========================================================================
|
|
//
|
|
//
|
|
//
|
|
//==========================================================================
|
|
class FxExpression;
|
|
|
|
class FFunctionBuildList
|
|
{
|
|
struct Item
|
|
{
|
|
PFunction *Func = nullptr;
|
|
FxExpression *Code = nullptr;
|
|
PPrototype *Proto = nullptr;
|
|
VMScriptFunction *Function = nullptr;
|
|
FString DumpName;
|
|
int type; // temporary kludge
|
|
};
|
|
|
|
TArray<Item> mItems;
|
|
|
|
public:
|
|
VMFunction *AddFunction(PFunction *func, FxExpression *code, const FString &name, bool statecall = false);
|
|
void Build();
|
|
};
|
|
|
|
extern FFunctionBuildList FunctionBuildList;
|
|
#endif
|