2020-12-13 08:04:42 -08:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "BfSystem.h"
|
|
|
|
#include "BfModule.h"
|
2020-12-17 04:51:05 -08:00
|
|
|
#include "BeefySysLib/util/Heap.h"
|
|
|
|
#include "BeefySysLib/util/AllocDebug.h"
|
2020-12-13 08:04:42 -08:00
|
|
|
|
|
|
|
NS_BF_BEGIN
|
|
|
|
|
|
|
|
class BfMethodInstance;
|
2020-12-14 10:20:44 -08:00
|
|
|
class BeModule;
|
|
|
|
class BeContext;
|
2020-12-13 08:04:42 -08:00
|
|
|
class BeDbgLoc;
|
|
|
|
class BeType;
|
|
|
|
class BeValue;
|
2020-12-19 14:19:33 -08:00
|
|
|
class BeConstant;
|
2020-12-13 08:04:42 -08:00
|
|
|
class BeInst;
|
|
|
|
class BeDbgFile;
|
|
|
|
class BePhiInst;
|
|
|
|
class BeFunction;
|
|
|
|
class BeSwitchInst;
|
2020-12-19 14:19:33 -08:00
|
|
|
class BeGlobalVariable;
|
2020-12-13 08:04:42 -08:00
|
|
|
class CeMachine;
|
|
|
|
class CeFunction;
|
|
|
|
|
2020-12-17 04:51:05 -08:00
|
|
|
typedef int addr_ce;
|
|
|
|
|
2020-12-13 08:04:42 -08:00
|
|
|
#define CEOP_SIZED(OPNAME) \
|
|
|
|
CeOp_##OPNAME##_8, \
|
|
|
|
CeOp_##OPNAME##_16, \
|
|
|
|
CeOp_##OPNAME##_32, \
|
|
|
|
CeOp_##OPNAME##_64, \
|
|
|
|
CeOp_##OPNAME##_X
|
|
|
|
|
|
|
|
#define CEOP_SIZED_NUMERIC(OPNAME) \
|
|
|
|
CeOp_##OPNAME##_I8, \
|
|
|
|
CeOp_##OPNAME##_I16, \
|
|
|
|
CeOp_##OPNAME##_I32, \
|
|
|
|
CeOp_##OPNAME##_I64
|
|
|
|
|
2020-12-17 04:51:05 -08:00
|
|
|
#define CEOP_SIZED_UNUMERIC(OPNAME) \
|
|
|
|
CeOp_##OPNAME##_U8, \
|
|
|
|
CeOp_##OPNAME##_U16, \
|
|
|
|
CeOp_##OPNAME##_U32, \
|
|
|
|
CeOp_##OPNAME##_U64
|
|
|
|
|
2020-12-13 08:04:42 -08:00
|
|
|
#define CEOP_SIZED_NUMERIC_PLUSF(OPNAME) \
|
|
|
|
CeOp_##OPNAME##_I8, \
|
|
|
|
CeOp_##OPNAME##_I16, \
|
|
|
|
CeOp_##OPNAME##_I32, \
|
|
|
|
CeOp_##OPNAME##_I64, \
|
|
|
|
CeOp_##OPNAME##_F32, \
|
|
|
|
CeOp_##OPNAME##_F64
|
|
|
|
|
2020-12-17 04:51:05 -08:00
|
|
|
enum CeErrorKind
|
|
|
|
{
|
|
|
|
CeErrorKind_None,
|
2020-12-22 04:50:37 -08:00
|
|
|
CeErrorKind_Error,
|
2020-12-17 04:51:05 -08:00
|
|
|
CeErrorKind_GlobalVariable,
|
|
|
|
CeErrorKind_FunctionPointer,
|
|
|
|
CeErrorKind_Intrinsic
|
|
|
|
};
|
|
|
|
|
2020-12-13 08:04:42 -08:00
|
|
|
enum CeOp : int16
|
|
|
|
{
|
|
|
|
CeOp_InvalidOp,
|
|
|
|
CeOp_Ret,
|
2020-12-30 13:24:13 -08:00
|
|
|
CeOp_SetRetType,
|
2020-12-13 08:04:42 -08:00
|
|
|
CeOp_Jmp,
|
|
|
|
CeOp_JmpIf,
|
|
|
|
CeOp_JmpIfNot,
|
|
|
|
|
2020-12-17 04:51:05 -08:00
|
|
|
CeOp_Error,
|
|
|
|
CeOp_DynamicCastCheck,
|
2020-12-19 14:19:33 -08:00
|
|
|
CeOp_GetReflectType,
|
2020-12-17 04:51:05 -08:00
|
|
|
CeOp_GetString,
|
|
|
|
CeOp_Malloc,
|
|
|
|
CeOp_Free,
|
|
|
|
|
|
|
|
CeOp_MemSet,
|
|
|
|
CeOp_MemSet_Const,
|
|
|
|
CeOp_MemCpy,
|
|
|
|
|
2020-12-14 10:20:44 -08:00
|
|
|
CeOp_FrameAddr_32,
|
|
|
|
CeOp_FrameAddr_64,
|
2020-12-19 14:19:33 -08:00
|
|
|
CeOp_FrameAddrOfs_32,
|
2020-12-13 08:04:42 -08:00
|
|
|
|
2020-12-22 04:50:37 -08:00
|
|
|
CeOp_ConstData,
|
2020-12-19 14:19:33 -08:00
|
|
|
CeOp_ConstDataRef,
|
2020-12-14 03:50:28 -08:00
|
|
|
CeOp_Zero,
|
2020-12-13 08:04:42 -08:00
|
|
|
CEOP_SIZED(Const),
|
|
|
|
CEOP_SIZED(Load),
|
2020-12-14 03:50:28 -08:00
|
|
|
CEOP_SIZED(Store),
|
2020-12-13 08:04:42 -08:00
|
|
|
CEOP_SIZED(Move),
|
|
|
|
CEOP_SIZED(Push),
|
2020-12-14 03:50:28 -08:00
|
|
|
CEOP_SIZED(Pop),
|
|
|
|
|
2020-12-13 08:04:42 -08:00
|
|
|
CeOp_AdjustSP,
|
2020-12-17 04:51:05 -08:00
|
|
|
CeOp_AdjustSPNeg,
|
|
|
|
CeOp_AdjustSPConst,
|
|
|
|
CeOp_GetSP,
|
|
|
|
CeOp_SetSP,
|
2020-12-19 14:19:33 -08:00
|
|
|
CeOp_GetStaticField,
|
|
|
|
CeOp_GetMethod,
|
|
|
|
CeOp_GetMethod_Inner,
|
|
|
|
CeOp_GetMethod_Virt,
|
|
|
|
CeOp_GetMethod_IFace,
|
2020-12-13 08:04:42 -08:00
|
|
|
CeOp_Call,
|
2020-12-17 04:51:05 -08:00
|
|
|
|
|
|
|
CeOp_Conv_I8_I16,
|
|
|
|
CeOp_Conv_I8_I32,
|
|
|
|
CeOp_Conv_I8_I64,
|
|
|
|
CeOp_Conv_I8_F32,
|
|
|
|
CeOp_Conv_I8_F64,
|
|
|
|
CeOp_Conv_I16_I32,
|
|
|
|
CeOp_Conv_I16_I64,
|
|
|
|
CeOp_Conv_I16_F32,
|
|
|
|
CeOp_Conv_I16_F64,
|
2020-12-13 08:04:42 -08:00
|
|
|
CeOp_Conv_I32_I64,
|
2020-12-17 04:51:05 -08:00
|
|
|
CeOp_Conv_I32_F32,
|
|
|
|
CeOp_Conv_I32_F64,
|
|
|
|
CeOp_Conv_I64_F32,
|
|
|
|
CeOp_Conv_I64_F64,
|
|
|
|
CeOp_Conv_U8_U16,
|
|
|
|
CeOp_Conv_U8_U32,
|
|
|
|
CeOp_Conv_U8_U64,
|
|
|
|
CeOp_Conv_U8_F32,
|
|
|
|
CeOp_Conv_U8_F64,
|
|
|
|
CeOp_Conv_U16_U32,
|
|
|
|
CeOp_Conv_U16_U64,
|
|
|
|
CeOp_Conv_U16_F32,
|
|
|
|
CeOp_Conv_U16_F64,
|
|
|
|
CeOp_Conv_U32_U64,
|
|
|
|
CeOp_Conv_U32_F32,
|
|
|
|
CeOp_Conv_U32_F64,
|
|
|
|
CeOp_Conv_U64_F32,
|
|
|
|
CeOp_Conv_U64_F64,
|
|
|
|
CeOp_Conv_F32_I8,
|
|
|
|
CeOp_Conv_F32_I16,
|
|
|
|
CeOp_Conv_F32_I32,
|
|
|
|
CeOp_Conv_F32_I64,
|
|
|
|
CeOp_Conv_F32_F64,
|
|
|
|
CeOp_Conv_F64_I8,
|
|
|
|
CeOp_Conv_F64_I16,
|
|
|
|
CeOp_Conv_F64_I32,
|
|
|
|
CeOp_Conv_F64_I64,
|
|
|
|
CeOp_Conv_F64_F32,
|
2020-12-13 08:04:42 -08:00
|
|
|
|
2020-12-14 03:50:28 -08:00
|
|
|
CEOP_SIZED_NUMERIC_PLUSF(AddConst),
|
|
|
|
CEOP_SIZED_NUMERIC_PLUSF(Add),
|
|
|
|
CEOP_SIZED_NUMERIC_PLUSF(Sub),
|
|
|
|
CEOP_SIZED_NUMERIC_PLUSF(Mul),
|
2020-12-17 04:51:05 -08:00
|
|
|
CEOP_SIZED_NUMERIC_PLUSF(Div),
|
|
|
|
CEOP_SIZED_UNUMERIC(Div),
|
|
|
|
CEOP_SIZED_NUMERIC_PLUSF(Mod),
|
|
|
|
CEOP_SIZED_UNUMERIC(Mod),
|
|
|
|
CEOP_SIZED_NUMERIC(And),
|
|
|
|
CEOP_SIZED_NUMERIC(Or),
|
|
|
|
CEOP_SIZED_NUMERIC(Xor),
|
|
|
|
CEOP_SIZED_NUMERIC(Shl),
|
|
|
|
CEOP_SIZED_NUMERIC(Shr),
|
|
|
|
CEOP_SIZED_UNUMERIC(Shr),
|
|
|
|
|
2020-12-13 08:04:42 -08:00
|
|
|
CEOP_SIZED_NUMERIC_PLUSF(Cmp_EQ),
|
2020-12-17 04:51:05 -08:00
|
|
|
CEOP_SIZED_NUMERIC_PLUSF(Cmp_NE),
|
2020-12-13 08:04:42 -08:00
|
|
|
CEOP_SIZED_NUMERIC_PLUSF(Cmp_SLT),
|
2020-12-14 03:50:28 -08:00
|
|
|
CEOP_SIZED_NUMERIC(Cmp_ULT),
|
|
|
|
CEOP_SIZED_NUMERIC_PLUSF(Cmp_SLE),
|
|
|
|
CEOP_SIZED_NUMERIC(Cmp_ULE),
|
2020-12-17 04:51:05 -08:00
|
|
|
CEOP_SIZED_NUMERIC_PLUSF(Cmp_SGT),
|
|
|
|
CEOP_SIZED_NUMERIC(Cmp_UGT),
|
|
|
|
CEOP_SIZED_NUMERIC_PLUSF(Cmp_SGE),
|
|
|
|
CEOP_SIZED_NUMERIC(Cmp_UGE),
|
2020-12-13 08:04:42 -08:00
|
|
|
|
|
|
|
CEOP_SIZED_NUMERIC_PLUSF(Neg),
|
2020-12-17 04:51:05 -08:00
|
|
|
CeOp_Not_I1,
|
|
|
|
CEOP_SIZED_NUMERIC(Not),
|
2020-12-13 08:04:42 -08:00
|
|
|
|
|
|
|
CeOp_COUNT
|
|
|
|
};
|
|
|
|
|
|
|
|
struct CeEmitEntry
|
|
|
|
{
|
|
|
|
int mCodePos;
|
|
|
|
int mFile;
|
|
|
|
int mLine;
|
|
|
|
int mColumn;
|
|
|
|
};
|
|
|
|
|
2020-12-17 04:51:05 -08:00
|
|
|
class CeFunctionInfo
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
String mName;
|
|
|
|
BfMethodInstance* mMethodInstance;
|
|
|
|
BfMethodRef mMethodRef;
|
|
|
|
CeFunction* mCeFunction;
|
|
|
|
int mRefCount;
|
|
|
|
|
|
|
|
public:
|
|
|
|
CeFunctionInfo()
|
|
|
|
{
|
|
|
|
mMethodInstance = NULL;
|
|
|
|
mCeFunction = NULL;
|
|
|
|
mRefCount = 0;
|
|
|
|
}
|
2020-12-23 08:53:38 -08:00
|
|
|
|
|
|
|
~CeFunctionInfo();
|
2020-12-17 04:51:05 -08:00
|
|
|
};
|
|
|
|
|
2020-12-13 08:04:42 -08:00
|
|
|
class CeCallEntry
|
|
|
|
{
|
|
|
|
public:
|
2020-12-17 04:51:05 -08:00
|
|
|
CeFunctionInfo* mFunctionInfo;
|
2020-12-13 08:04:42 -08:00
|
|
|
int mBindRevision;
|
|
|
|
CeFunction* mFunction;
|
|
|
|
|
|
|
|
public:
|
|
|
|
CeCallEntry()
|
|
|
|
{
|
2020-12-17 04:51:05 -08:00
|
|
|
mFunctionInfo = NULL;
|
2020-12-13 08:04:42 -08:00
|
|
|
mBindRevision = -1;
|
|
|
|
mFunction = NULL;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-12-17 04:51:05 -08:00
|
|
|
class CeStringEntry
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
int mStringId;
|
|
|
|
int mBindExecuteId;
|
|
|
|
addr_ce mStringAddr;
|
|
|
|
|
|
|
|
public:
|
|
|
|
CeStringEntry()
|
|
|
|
{
|
|
|
|
mStringId = -1;
|
|
|
|
mBindExecuteId = -1;
|
|
|
|
mStringAddr = 0;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
enum CeFunctionKind
|
|
|
|
{
|
|
|
|
CeFunctionKind_Normal,
|
|
|
|
CeFunctionKind_Extern,
|
|
|
|
CeFunctionKind_OOB,
|
2020-12-19 14:19:33 -08:00
|
|
|
CeFunctionKind_Malloc,
|
|
|
|
CeFunctionKind_Free,
|
2020-12-17 04:51:05 -08:00
|
|
|
CeFunctionKind_FatalError,
|
|
|
|
CeFunctionKind_DebugWrite,
|
|
|
|
CeFunctionKind_DebugWrite_Int,
|
2020-12-19 14:19:33 -08:00
|
|
|
CeFunctionKind_GetReflectType,
|
2020-12-23 08:53:38 -08:00
|
|
|
CeFunctionKind_GetReflectTypeById,
|
|
|
|
CeFunctionKind_Sleep,
|
2020-12-19 14:19:33 -08:00
|
|
|
CeFunctionKind_Char32_ToLower,
|
|
|
|
CeFunctionKind_Char32_ToUpper,
|
|
|
|
CeFunctionKind_Char32_IsLower,
|
|
|
|
CeFunctionKind_Char32_IsUpper,
|
|
|
|
CeFunctionKind_Char32_IsWhiteSpace_EX,
|
|
|
|
CeFunctionKind_Char32_IsLetterOrDigit,
|
|
|
|
CeFunctionKind_Char32_IsLetter,
|
|
|
|
CeFunctionKind_Char32_IsNumber,
|
|
|
|
};
|
|
|
|
|
2020-12-22 04:50:37 -08:00
|
|
|
class CeConstStructFixup
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
enum Kind
|
|
|
|
{
|
|
|
|
Kind_None,
|
|
|
|
Kind_StringPtr,
|
|
|
|
Kind_StringCharPtr,
|
|
|
|
};
|
|
|
|
|
|
|
|
public:
|
|
|
|
Kind mKind;
|
|
|
|
int mValue;
|
|
|
|
int mOffset;
|
|
|
|
};
|
|
|
|
|
2020-12-19 14:19:33 -08:00
|
|
|
class CeConstStructData
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Val128 mHash;
|
|
|
|
Array<uint8> mData;
|
2020-12-22 04:50:37 -08:00
|
|
|
Array<uint8> mFixedData;
|
|
|
|
Array<CeConstStructFixup> mFixups;
|
2020-12-19 14:19:33 -08:00
|
|
|
addr_ce mAddr;
|
|
|
|
int mBindExecuteId;
|
2020-12-22 04:50:37 -08:00
|
|
|
bool mQueueFixups;
|
2020-12-19 14:19:33 -08:00
|
|
|
|
|
|
|
public:
|
|
|
|
CeConstStructData()
|
|
|
|
{
|
|
|
|
mBindExecuteId = -1;
|
|
|
|
mAddr = 0;
|
2020-12-22 04:50:37 -08:00
|
|
|
mQueueFixups = false;
|
2020-12-19 14:19:33 -08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class CeInnerFunctionInfo
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
String mName;
|
|
|
|
BeFunction* mBeFunction;
|
|
|
|
CeFunction* mOwner;
|
|
|
|
};
|
|
|
|
|
|
|
|
class CeStaticFieldEntry
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
String mName;
|
|
|
|
int mTypeId;
|
|
|
|
int mSize;
|
|
|
|
addr_ce mAddr;
|
|
|
|
int mBindExecuteId;
|
|
|
|
|
|
|
|
public:
|
|
|
|
CeStaticFieldEntry()
|
|
|
|
{
|
|
|
|
mTypeId = -1;
|
|
|
|
mSize = 0;
|
|
|
|
mAddr = 0;
|
|
|
|
mBindExecuteId = -1;
|
|
|
|
}
|
2020-12-17 04:51:05 -08:00
|
|
|
};
|
|
|
|
|
2020-12-13 08:04:42 -08:00
|
|
|
class CeFunction
|
|
|
|
{
|
|
|
|
public:
|
2020-12-17 04:51:05 -08:00
|
|
|
CeFunctionInfo* mCeFunctionInfo;
|
2020-12-19 14:19:33 -08:00
|
|
|
CeInnerFunctionInfo* mCeInnerFunctionInfo;
|
|
|
|
BfMethodInstance* mMethodInstance;
|
2020-12-17 04:51:05 -08:00
|
|
|
CeFunctionKind mFunctionKind;
|
2020-12-30 13:24:13 -08:00
|
|
|
bool mGenerating;
|
2020-12-13 08:04:42 -08:00
|
|
|
bool mInitialized;
|
2020-12-30 13:24:13 -08:00
|
|
|
bool mFailed;
|
|
|
|
bool mIsVarReturn;
|
2020-12-13 08:04:42 -08:00
|
|
|
Array<uint8> mCode;
|
|
|
|
Array<String> mFiles;
|
|
|
|
Array<CeEmitEntry> mEmitTable;
|
|
|
|
Array<CeCallEntry> mCallTable;
|
2020-12-17 04:51:05 -08:00
|
|
|
Array<CeStringEntry> mStringTable;
|
2020-12-19 14:19:33 -08:00
|
|
|
Array<CeConstStructData> mConstStructTable;
|
|
|
|
Array<CeStaticFieldEntry> mStaticFieldTable;
|
2020-12-17 04:51:05 -08:00
|
|
|
Array<BfType*> mTypeTable;
|
2020-12-19 14:19:33 -08:00
|
|
|
Array<CeFunction*> mInnerFunctions;
|
2020-12-13 08:04:42 -08:00
|
|
|
String mGenError;
|
2020-12-17 04:51:05 -08:00
|
|
|
int mFrameSize;
|
2020-12-30 13:24:13 -08:00
|
|
|
int mMaxReturnSize;
|
2020-12-23 11:27:46 -08:00
|
|
|
int mId;
|
2020-12-13 08:04:42 -08:00
|
|
|
|
|
|
|
public:
|
|
|
|
CeFunction()
|
|
|
|
{
|
2020-12-17 04:51:05 -08:00
|
|
|
mCeFunctionInfo = NULL;
|
2020-12-19 14:19:33 -08:00
|
|
|
mCeInnerFunctionInfo = NULL;
|
2020-12-17 04:51:05 -08:00
|
|
|
mFunctionKind = CeFunctionKind_Normal;
|
2020-12-30 13:24:13 -08:00
|
|
|
mGenerating = false;
|
2020-12-13 08:04:42 -08:00
|
|
|
mInitialized = false;
|
|
|
|
mMethodInstance = NULL;
|
|
|
|
mFailed = false;
|
2020-12-30 13:24:13 -08:00
|
|
|
mIsVarReturn = false;
|
2020-12-13 08:04:42 -08:00
|
|
|
mFrameSize = 0;
|
2020-12-30 13:24:13 -08:00
|
|
|
mMaxReturnSize = 0;
|
2020-12-23 11:27:46 -08:00
|
|
|
mId = -1;
|
2020-12-13 08:04:42 -08:00
|
|
|
}
|
2020-12-17 04:51:05 -08:00
|
|
|
|
|
|
|
~CeFunction();
|
2020-12-13 08:04:42 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
enum CeEvalFlags
|
|
|
|
{
|
2020-12-22 04:50:37 -08:00
|
|
|
CeEvalFlags_None = 0,
|
|
|
|
CeEvalFlags_Cascade = 1
|
2020-12-13 08:04:42 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
enum CeOperandKind
|
|
|
|
{
|
|
|
|
CeOperandKind_None,
|
|
|
|
CeOperandKind_FrameOfs,
|
|
|
|
CeOperandKind_AllocaAddr,
|
|
|
|
CeOperandKind_Block,
|
2020-12-19 14:19:33 -08:00
|
|
|
CeOperandKind_Immediate,
|
2020-12-22 04:50:37 -08:00
|
|
|
CeOperandKind_ConstStructTableIdx,
|
2020-12-19 14:19:33 -08:00
|
|
|
CeOperandKind_CallTableIdx
|
2020-12-13 08:04:42 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
class CeOperand
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
CeOperandKind mKind;
|
|
|
|
union
|
|
|
|
{
|
|
|
|
int mFrameOfs;
|
|
|
|
int mBlockIdx;
|
2020-12-14 03:50:28 -08:00
|
|
|
int mImmediate;
|
2020-12-19 14:19:33 -08:00
|
|
|
int mCallTableIdx;
|
2020-12-22 04:50:37 -08:00
|
|
|
int mStructTableIdx;
|
2020-12-19 14:19:33 -08:00
|
|
|
BeConstant* mConstant;
|
2020-12-13 08:04:42 -08:00
|
|
|
};
|
|
|
|
BeType* mType;
|
|
|
|
|
|
|
|
public:
|
|
|
|
CeOperand()
|
|
|
|
{
|
|
|
|
mKind = CeOperandKind_None;
|
|
|
|
mFrameOfs = 0;
|
|
|
|
mType = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
operator bool() const
|
|
|
|
{
|
|
|
|
return mKind != CeOperandKind_None;
|
|
|
|
}
|
2020-12-14 03:50:28 -08:00
|
|
|
|
|
|
|
bool IsImmediate()
|
|
|
|
{
|
|
|
|
return mKind == CeOperandKind_Immediate;
|
|
|
|
}
|
2020-12-13 08:04:42 -08:00
|
|
|
};
|
|
|
|
|
2020-12-19 14:19:33 -08:00
|
|
|
#define BF_CE_STACK_SIZE 4*1024*1024
|
2020-12-17 04:51:05 -08:00
|
|
|
#define BF_CE_MAX_MEMORY 128*1024*1024
|
2020-12-19 14:19:33 -08:00
|
|
|
#define BF_CE_MAX_CARRYOVER_MEMORY BF_CE_STACK_SIZE + 1024*1024
|
|
|
|
#define BF_CE_MAX_CARRYOVER_HEAP 1024*1024
|
2020-12-13 08:04:42 -08:00
|
|
|
|
|
|
|
enum CeOperandInfoKind
|
|
|
|
{
|
|
|
|
CEOI_None,
|
|
|
|
CEOI_FrameRef,
|
|
|
|
CEOI_IMM8,
|
|
|
|
CEOI_IMM16,
|
|
|
|
CEOI_IMM32,
|
|
|
|
CEOI_IMM64,
|
2020-12-14 03:50:28 -08:00
|
|
|
CEOI_IMMF32,
|
|
|
|
CEOI_IMMF64,
|
2020-12-13 08:04:42 -08:00
|
|
|
CEOI_IMM_VAR,
|
|
|
|
CEOI_JMPREL
|
|
|
|
};
|
|
|
|
|
|
|
|
enum CeSizeClass
|
|
|
|
{
|
|
|
|
CeSizeClass_8,
|
|
|
|
CeSizeClass_16,
|
|
|
|
CeSizeClass_32,
|
|
|
|
CeSizeClass_64,
|
|
|
|
CeSizeClass_X,
|
|
|
|
};
|
|
|
|
|
|
|
|
class CeDumpContext
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
CeFunction* mCeFunction;
|
|
|
|
String mStr;
|
|
|
|
uint8* mStart;
|
|
|
|
uint8* mPtr;
|
|
|
|
uint8* mEnd;
|
|
|
|
|
|
|
|
public:
|
|
|
|
void DumpOperandInfo(CeOperandInfoKind operandInfoKind);
|
|
|
|
|
|
|
|
void Dump();
|
|
|
|
};
|
|
|
|
|
|
|
|
struct CePhiOutgoing
|
|
|
|
{
|
|
|
|
BeValue* mPhiValue;
|
|
|
|
BePhiInst* mPhiInst;
|
|
|
|
int mPhiBlockIdx;
|
|
|
|
};
|
|
|
|
|
|
|
|
class CeBlock
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
int mEmitOfs;
|
|
|
|
Array<CePhiOutgoing> mPhiOutgoing;
|
|
|
|
|
|
|
|
public:
|
|
|
|
CeBlock()
|
|
|
|
{
|
|
|
|
mEmitOfs = -1;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class CeJumpEntry
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
int mEmitPos;
|
|
|
|
int mBlockIdx;
|
|
|
|
};
|
|
|
|
|
|
|
|
class CeBuilder
|
|
|
|
{
|
|
|
|
public:
|
2020-12-19 14:19:33 -08:00
|
|
|
CeBuilder* mParentBuilder;
|
2020-12-17 04:51:05 -08:00
|
|
|
CeMachine* mCeMachine;
|
2020-12-13 08:04:42 -08:00
|
|
|
CeFunction* mCeFunction;
|
|
|
|
BeFunction* mBeFunction;
|
2020-12-22 04:50:37 -08:00
|
|
|
CeOperand mReturnVal;
|
2020-12-13 08:04:42 -08:00
|
|
|
BeType* mIntPtrType;
|
|
|
|
int mPtrSize;
|
|
|
|
|
|
|
|
String mError;
|
|
|
|
BeDbgLoc* mCurDbgLoc;
|
|
|
|
Array<CeBlock> mBlocks;
|
|
|
|
Array<CeJumpEntry> mJumpTable;
|
|
|
|
Dictionary<BeValue*, CeOperand> mValueToOperand;
|
|
|
|
int mFrameSize;
|
|
|
|
Dictionary<BeDbgFile*, int> mDbgFileMap;
|
|
|
|
Dictionary<BeFunction*, int> mFunctionMap;
|
2020-12-17 04:51:05 -08:00
|
|
|
Dictionary<int, int> mStringMap;
|
2020-12-19 14:19:33 -08:00
|
|
|
Dictionary<BeConstant*, int> mConstDataMap;
|
|
|
|
Dictionary<BeFunction*, int> mInnerFunctionMap;
|
|
|
|
Dictionary<BeGlobalVariable*, int> mStaticFieldMap;
|
2020-12-13 08:04:42 -08:00
|
|
|
|
|
|
|
public:
|
|
|
|
CeBuilder()
|
|
|
|
{
|
2020-12-19 14:19:33 -08:00
|
|
|
mParentBuilder = NULL;
|
2020-12-13 08:04:42 -08:00
|
|
|
mPtrSize = 0;
|
|
|
|
mCeFunction = NULL;
|
|
|
|
mBeFunction = NULL;
|
|
|
|
mCeMachine = NULL;
|
|
|
|
mCurDbgLoc = NULL;
|
|
|
|
mFrameSize = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Fail(const StringImpl& error);
|
|
|
|
|
|
|
|
CeOperand FrameAlloc(BeType* type);
|
2020-12-17 04:51:05 -08:00
|
|
|
CeOperand EmitConst(int64 val, int size);
|
2020-12-19 14:19:33 -08:00
|
|
|
CeErrorKind EmitConst(Array<uint8>& arr, BeConstant* constant);
|
2020-12-14 03:50:28 -08:00
|
|
|
CeOperand GetOperand(BeValue* value, bool allowAlloca = false, bool allowImmediate = false);
|
2020-12-13 08:04:42 -08:00
|
|
|
CeSizeClass GetSizeClass(int size);
|
|
|
|
int GetCodePos();
|
|
|
|
|
|
|
|
void HandleParams();
|
2020-12-17 04:51:05 -08:00
|
|
|
|
|
|
|
void Emit(uint8 val);
|
2020-12-13 08:04:42 -08:00
|
|
|
void Emit(CeOp val);
|
2020-12-17 04:51:05 -08:00
|
|
|
void EmitSizedOp(CeOp val, int size);
|
2020-12-13 08:04:42 -08:00
|
|
|
void Emit(int32 val);
|
2020-12-14 10:20:44 -08:00
|
|
|
void Emit(int64 val);
|
2020-12-13 08:04:42 -08:00
|
|
|
void Emit(bool val);
|
|
|
|
void Emit(void* ptr, int size);
|
2020-12-19 14:19:33 -08:00
|
|
|
void EmitZeroes(int size);
|
2020-12-13 08:04:42 -08:00
|
|
|
void EmitJump(CeOp op, const CeOperand& block);
|
|
|
|
void EmitBinarySwitchSection(BeSwitchInst* switchInst, int startIdx, int endIdx);
|
|
|
|
|
|
|
|
void EmitFrameOffset(const CeOperand& val);
|
|
|
|
void FlushPhi(CeBlock* ceBlock, int targetBlockIdx);
|
|
|
|
|
|
|
|
void EmitBinaryOp(CeOp iOp, CeOp fOp, const CeOperand& lhs, const CeOperand& rhs, CeOperand& result);
|
|
|
|
void EmitUnaryOp(CeOp iOp, CeOp fOp, const CeOperand& val, CeOperand& result);
|
|
|
|
void EmitSizedOp(CeOp op, const CeOperand& operand, CeOperand* result, bool allowNonStdSize);
|
2021-01-05 13:50:57 -08:00
|
|
|
void ProcessMethod(BfMethodInstance* methodInstance, BfMethodInstance* dupMethodInstance);
|
2020-12-13 08:04:42 -08:00
|
|
|
void Build();
|
|
|
|
};
|
|
|
|
|
|
|
|
class CeFrame
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
CeFunction* mFunction;
|
2020-12-17 04:51:05 -08:00
|
|
|
addr_ce mStackAddr;
|
|
|
|
addr_ce mFrameAddr;
|
2020-12-13 08:04:42 -08:00
|
|
|
uint8* mInstPtr;
|
2020-12-30 13:24:13 -08:00
|
|
|
BfType* mReturnType;
|
2020-12-13 08:04:42 -08:00
|
|
|
|
|
|
|
public:
|
|
|
|
CeFrame()
|
|
|
|
{
|
|
|
|
mFunction = NULL;
|
2020-12-19 14:19:33 -08:00
|
|
|
mStackAddr = 0;
|
|
|
|
mFrameAddr = 0;
|
2020-12-13 08:04:42 -08:00
|
|
|
mInstPtr = NULL;
|
2020-12-30 13:24:13 -08:00
|
|
|
mReturnType = NULL;
|
2020-12-13 08:04:42 -08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-12-19 14:19:33 -08:00
|
|
|
class CeStaticFieldInfo
|
2020-12-17 04:51:05 -08:00
|
|
|
{
|
2020-12-19 14:19:33 -08:00
|
|
|
public:
|
|
|
|
BfFieldInstance* mFieldInstance;
|
|
|
|
addr_ce mAddr;
|
|
|
|
|
|
|
|
public:
|
|
|
|
CeStaticFieldInfo()
|
|
|
|
{
|
|
|
|
mFieldInstance = NULL;
|
|
|
|
mAddr = 0;
|
|
|
|
}
|
2020-12-17 04:51:05 -08:00
|
|
|
};
|
|
|
|
|
2020-12-22 04:50:37 -08:00
|
|
|
class CeAppendAllocInfo
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
BfModule* mModule;
|
|
|
|
BfIRValue mAllocValue;
|
|
|
|
BfIRValue mAppendSizeValue;
|
|
|
|
};
|
|
|
|
|
2020-12-13 08:04:42 -08:00
|
|
|
class CeMachine
|
|
|
|
{
|
|
|
|
public:
|
2020-12-17 04:51:05 -08:00
|
|
|
Dictionary<BfMethodInstance*, CeFunctionInfo*> mFunctions;
|
|
|
|
Dictionary<String, CeFunctionInfo*> mNamedFunctionMap;
|
2020-12-23 11:27:46 -08:00
|
|
|
Dictionary<int, CeFunction*> mFunctionIdMap; // Only used for 32-bit
|
|
|
|
|
2020-12-13 08:04:42 -08:00
|
|
|
BfCompiler* mCompiler;
|
|
|
|
BfModule* mCeModule;
|
|
|
|
int mRevision;
|
2020-12-23 08:53:38 -08:00
|
|
|
int mRevisionExecuteTime;
|
|
|
|
int mExecuteId;
|
2020-12-23 11:27:46 -08:00
|
|
|
int mCurFunctionId;
|
2020-12-13 08:04:42 -08:00
|
|
|
|
2020-12-19 14:19:33 -08:00
|
|
|
// These are only valid for the current execution
|
2020-12-17 04:51:05 -08:00
|
|
|
ContiguousHeap* mHeap;
|
|
|
|
Array<CeFrame> mCallStack;
|
|
|
|
Array<uint8> mMemory;
|
|
|
|
Dictionary<int, addr_ce> mStringMap;
|
2020-12-19 14:19:33 -08:00
|
|
|
int mStringCharsOffset;
|
|
|
|
Dictionary<int, addr_ce> mReflectMap;
|
|
|
|
Dictionary<Val128, addr_ce> mConstDataMap;
|
|
|
|
Dictionary<String, CeStaticFieldInfo> mStaticFieldMap;
|
|
|
|
HashSet<int> mStaticCtorExecSet;
|
2020-12-23 11:27:46 -08:00
|
|
|
CeAppendAllocInfo* mAppendAllocInfo;
|
2020-12-17 04:51:05 -08:00
|
|
|
|
2021-01-01 15:33:17 -08:00
|
|
|
CeFunction* mPreparingFunction;
|
2020-12-14 03:50:28 -08:00
|
|
|
BfAstNode* mCurTargetSrc;
|
2020-12-23 08:53:38 -08:00
|
|
|
BfMethodInstance* mCurMethodInstance;
|
2020-12-17 04:51:05 -08:00
|
|
|
BfModule* mCurModule;
|
2020-12-30 13:24:13 -08:00
|
|
|
BfType* mCurExpectingType;
|
2020-12-14 03:50:28 -08:00
|
|
|
|
2020-12-13 08:04:42 -08:00
|
|
|
public:
|
|
|
|
CeMachine(BfCompiler* compiler);
|
|
|
|
~CeMachine();
|
2020-12-22 04:50:37 -08:00
|
|
|
|
|
|
|
BfError* Fail(const StringImpl& error);
|
2020-12-14 10:20:44 -08:00
|
|
|
BfError* Fail(const CeFrame& curFrame, const StringImpl& error);
|
2020-12-14 03:50:28 -08:00
|
|
|
|
2020-12-13 08:04:42 -08:00
|
|
|
void Init();
|
2020-12-17 04:51:05 -08:00
|
|
|
uint8* CeMalloc(int size);
|
|
|
|
bool CeFree(addr_ce addr);
|
2020-12-23 08:53:38 -08:00
|
|
|
addr_ce CeAllocArray(BfArrayType* arrayType, int count, addr_ce& elemsAddr);
|
2020-12-19 14:19:33 -08:00
|
|
|
addr_ce GetReflectType(int typeId);
|
|
|
|
addr_ce GetString(int stringId);
|
|
|
|
addr_ce GetConstantData(BeConstant* constant);
|
|
|
|
BfType* GetBfType(int typeId);
|
2020-12-22 04:50:37 -08:00
|
|
|
void PrepareConstStructEntry(CeConstStructData& constStructData);
|
2020-12-17 04:51:05 -08:00
|
|
|
|
2020-12-14 10:20:44 -08:00
|
|
|
BeContext* GetBeContext();
|
|
|
|
BeModule* GetBeModule();
|
2020-12-17 04:51:05 -08:00
|
|
|
void DerefMethodInfo(CeFunctionInfo* ceFunctionInfo);
|
2020-12-22 04:50:37 -08:00
|
|
|
void RemoveMethod(BfMethodInstance* methodInstance);
|
2020-12-23 08:53:38 -08:00
|
|
|
bool WriteConstant(BfModule* module, addr_ce addr, BfConstant* constant, BfType* type, bool isParams = false);
|
2020-12-22 04:50:37 -08:00
|
|
|
CeErrorKind WriteConstant(CeConstStructData& data, BeConstant* constVal);
|
|
|
|
BfIRValue CreateConstant(BfModule* module, uint8* ptr, BfType* type, BfType** outType = NULL);
|
2020-12-13 08:04:42 -08:00
|
|
|
void CreateFunction(BfMethodInstance* methodInstance, CeFunction* ceFunction);
|
2020-12-30 13:24:13 -08:00
|
|
|
bool Execute(CeFunction* startFunction, uint8* startStackPtr, uint8* startFramePtr, BfType*& returnType);
|
2020-12-13 08:04:42 -08:00
|
|
|
|
2020-12-19 14:19:33 -08:00
|
|
|
void PrepareFunction(CeFunction* methodInstance, CeBuilder* parentBuilder);
|
2020-12-23 11:27:46 -08:00
|
|
|
void MapFunctionId(CeFunction* ceFunction);
|
2020-12-23 08:53:38 -08:00
|
|
|
|
|
|
|
void CheckFunctions();
|
2020-12-14 10:20:44 -08:00
|
|
|
CeFunction* GetFunction(BfMethodInstance* methodInstance, BfIRValue func, bool& added);
|
2020-12-17 04:51:05 -08:00
|
|
|
CeFunction* GetPreparedFunction(BfMethodInstance* methodInstance);
|
2020-12-19 14:19:33 -08:00
|
|
|
|
2020-12-13 08:04:42 -08:00
|
|
|
public:
|
|
|
|
void CompileStarted();
|
2020-12-14 10:20:44 -08:00
|
|
|
void QueueMethod(BfMethodInstance* methodInstance, BfIRValue func);
|
2020-12-17 04:51:05 -08:00
|
|
|
void QueueMethod(BfModuleMethodInstance moduleMethodInstance);
|
2020-12-19 14:19:33 -08:00
|
|
|
void QueueStaticField(BfFieldInstance* fieldInstance, const StringImpl& mangledFieldName);
|
2020-12-22 04:50:37 -08:00
|
|
|
|
|
|
|
void SetAppendAllocInfo(BfModule* module, BfIRValue allocValue, BfIRValue appendSizeValue);
|
|
|
|
void ClearAppendAllocInfo();
|
|
|
|
|
|
|
|
BfTypedValue Call(BfAstNode* targetSrc, BfModule* module, BfMethodInstance* methodInstance, const BfSizedArray<BfIRValue>& args, CeEvalFlags flags, BfType* expectingType);
|
2020-12-13 08:04:42 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
NS_BF_END
|