2016-03-01 15:47:10 +00:00
|
|
|
#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;
|
|
|
|
};
|
|
|
|
|
2016-10-02 02:47:15 +00:00
|
|
|
VMFunctionBuilder(bool checkself = false);
|
2016-03-01 15:47:10 +00:00
|
|
|
~VMFunctionBuilder();
|
|
|
|
|
2016-10-12 22:53:59 +00:00
|
|
|
void MakeFunction(VMScriptFunction *func);
|
2016-03-01 15:47:10 +00:00
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
2016-07-25 04:38:02 +00:00
|
|
|
// Returns the address of the next instruction to be emitted.
|
|
|
|
size_t GetAddress();
|
|
|
|
|
2016-03-01 15:47:10 +00:00
|
|
|
// 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];
|
|
|
|
|
2016-10-02 02:47:15 +00:00
|
|
|
// For use by DECORATE's self/stateowner sanitizer.
|
|
|
|
bool IsActionFunc;
|
|
|
|
|
2016-03-01 15:47:10 +00:00
|
|
|
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;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2016-10-12 22:53:59 +00:00
|
|
|
void DumpFunction(FILE *dump, VMScriptFunction *sfunc, const char *label, int labellen);
|
|
|
|
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
class FxExpression;
|
|
|
|
|
|
|
|
class FFunctionBuildList
|
|
|
|
{
|
|
|
|
struct Item
|
|
|
|
{
|
2016-10-15 12:36:08 +00:00
|
|
|
PFunction *Func = nullptr;
|
2016-10-12 22:53:59 +00:00
|
|
|
FxExpression *Code = nullptr;
|
|
|
|
PPrototype *Proto = nullptr;
|
|
|
|
VMScriptFunction *Function = nullptr;
|
|
|
|
FString DumpName;
|
|
|
|
int type; // temporary kludge
|
|
|
|
};
|
|
|
|
|
|
|
|
TArray<Item> mItems;
|
|
|
|
|
|
|
|
public:
|
2016-10-15 12:36:08 +00:00
|
|
|
VMFunction *AddFunction(PFunction *func, FxExpression *code, const FString &name, bool statecall = false);
|
2016-10-12 22:53:59 +00:00
|
|
|
void Build();
|
|
|
|
};
|
|
|
|
|
|
|
|
extern FFunctionBuildList FunctionBuildList;
|
2016-03-01 15:47:10 +00:00
|
|
|
#endif
|