2009-09-25 03:44:58 +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();
|
2014-12-31 05:31:07 +00:00
|
|
|
int GetMostUsed() { return MostUsed; }
|
2009-10-11 00:02:14 +00:00
|
|
|
int Get(int count); // Returns the first register in the range
|
|
|
|
void Return(int reg, int count);
|
2014-12-22 02:43:24 +00:00
|
|
|
bool Reuse(int regnum);
|
2009-09-25 03:44:58 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
VM_UWORD Used[256/32]; // Bitmap of used registers (bit set means reg is used)
|
|
|
|
int MostUsed;
|
|
|
|
|
|
|
|
friend class VMFunctionBuilder;
|
|
|
|
};
|
|
|
|
|
|
|
|
VMFunctionBuilder();
|
|
|
|
~VMFunctionBuilder();
|
|
|
|
|
|
|
|
VMScriptFunction *MakeFunction();
|
|
|
|
|
|
|
|
// 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 newly-emitted instruction.
|
|
|
|
size_t Emit(int opcode, int opa, int opb, int opc);
|
2009-10-11 00:02:14 +00:00
|
|
|
size_t Emit(int opcode, int opa, VM_SHALF opbc);
|
|
|
|
size_t Emit(int opcode, int opabc);
|
2013-07-28 02:35:22 +00:00
|
|
|
size_t EmitParamInt(int value);
|
2009-10-11 00:02:14 +00:00
|
|
|
size_t EmitLoadInt(int regnum, int value);
|
2012-10-28 04:36:52 +00:00
|
|
|
size_t EmitRetInt(int retnum, bool final, int value);
|
2009-10-11 00:02:14 +00:00
|
|
|
|
|
|
|
void Backpatch(size_t addr, size_t target);
|
|
|
|
void BackpatchToHere(size_t addr);
|
2009-09-25 03:44:58 +00:00
|
|
|
|
|
|
|
// Write out complete constant tables.
|
|
|
|
void FillIntConstants(int *konst);
|
|
|
|
void FillFloatConstants(double *konst);
|
|
|
|
void FillAddressConstants(FVoidObj *konst, VM_ATAG *tags);
|
|
|
|
void FillStringConstants(FString *strings);
|
|
|
|
|
2015-04-28 23:03:50 +00:00
|
|
|
// PARAM increases ActiveParam; CALL decreases it.
|
|
|
|
void ParamChange(int delta);
|
|
|
|
|
2009-09-25 03:44:58 +00:00
|
|
|
// Track available registers.
|
2009-10-11 00:02:14 +00:00
|
|
|
RegAvailability Registers[4];
|
|
|
|
|
2009-09-25 03:44:58 +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;
|
|
|
|
|
2009-10-17 00:33:23 +00:00
|
|
|
TArray<VMOP> Code;
|
2009-10-14 02:39:13 +00:00
|
|
|
|
2009-09-25 03:44:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|