- exported thinker iterator and drop item chain to scripting. Unlike its native counterpart the script-side iterator is wrapped into a DObject to allow proper handling for memory management. - fixed: The VMFunctionBuilder only distinguished between member and action functions but failed on static ones. - fixed: FxAssign did not add all needed type casts. Except for purely numeric types it will now wrap the expression in an FxTypeCast. Numeric handling remains unchanged for both performance reasons and not altering semantics for DECORATE. - exported all internal flags as variables to scripting. They still cannot be used in an actor definition. - make ATAG_STATE the same as ATAG_GENERIC. Since state pointers exist as actual variables they can take both values which on occasion can trigger some asserts. - gave PClass a bExported flag, so that scripts cannot see purely internal classes. Especially the types like PInt can cause problems. Todo: we need readonly references to safely expose the actor defaults. Right now some badly behaving code could overwrite them.
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(int numimplicits);
|
|
~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];
|
|
|
|
// amount of implicit parameters so that proper code can be emitted for method calls
|
|
int NumImplicits;
|
|
|
|
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 PrintableName;
|
|
bool FromDecorate;
|
|
};
|
|
|
|
TArray<Item> mItems;
|
|
|
|
public:
|
|
VMFunction *AddFunction(PFunction *func, FxExpression *code, const FString &name, bool fromdecorate);
|
|
void Build();
|
|
};
|
|
|
|
extern FFunctionBuildList FunctionBuildList;
|
|
#endif
|