Pointer arguments in constexpr evaluation

This commit is contained in:
drmortalwombat 2024-08-05 18:19:54 +02:00
parent 3df85b09fc
commit 6f069946ba
3 changed files with 76 additions and 18 deletions

View File

@ -2,7 +2,7 @@
#include <math.h> #include <math.h>
ConstexprInterpreter::Value::Value(void) ConstexprInterpreter::Value::Value(void)
: mDecType(TheVoidTypeDeclaration), : mDecType(TheVoidTypeDeclaration), mDecValue(nullptr),
mBaseValue(nullptr), mOffset(0), mBaseValue(nullptr), mOffset(0),
mData(mShortData), mDataSize(0) mData(mShortData), mDataSize(0)
{ {
@ -10,7 +10,7 @@ ConstexprInterpreter::Value::Value(void)
ConstexprInterpreter::Value::Value(const Location & location) ConstexprInterpreter::Value::Value(const Location & location)
: mLocation(location), : mLocation(location),
mDecType(TheVoidTypeDeclaration), mDecType(TheVoidTypeDeclaration), mDecValue(nullptr),
mBaseValue(nullptr), mOffset(0), mBaseValue(nullptr), mOffset(0),
mData(mShortData), mDataSize(0) mData(mShortData), mDataSize(0)
{ {
@ -18,7 +18,7 @@ ConstexprInterpreter::Value::Value(const Location & location)
ConstexprInterpreter::Value::Value(Expression* exp) ConstexprInterpreter::Value::Value(Expression* exp)
: mLocation(exp->mLocation), : mLocation(exp->mLocation),
mDecType(exp->mDecType), mDecType(exp->mDecType), mDecValue(nullptr),
mBaseValue(nullptr), mOffset(0), mBaseValue(nullptr), mOffset(0),
mDataSize(exp->mDecType->mSize) mDataSize(exp->mDecType->mSize)
{ {
@ -51,12 +51,15 @@ void ConstexprInterpreter::Value::PutConst(int offset, Declaration* dec)
for (int i = 0; i < dec->mBase->mSize; i++) for (int i = 0; i < dec->mBase->mSize; i++)
PutIntAt(dec->mData[i], offset + i, TheConstCharTypeDeclaration); PutIntAt(dec->mData[i], offset + i, TheConstCharTypeDeclaration);
break; break;
case DT_CONST_POINTER:
PutPtrAt(new Value(mLocation, dec->mValue->mDecValue, dec->mBase, 0), offset, dec);
break;
} }
} }
ConstexprInterpreter::Value::Value(const Location& location, Declaration* dec) ConstexprInterpreter::Value::Value(const Location& location, Declaration* dec)
: mLocation(location), : mLocation(location),
mDecType(dec), mDecType(dec), mDecValue(nullptr),
mBaseValue(nullptr), mOffset(0), mBaseValue(nullptr), mOffset(0),
mDataSize(dec->mSize) mDataSize(dec->mSize)
{ {
@ -68,7 +71,7 @@ ConstexprInterpreter::Value::Value(const Location& location, Declaration* dec)
ConstexprInterpreter::Value::Value(const Location& location, Declaration* dec, int size) ConstexprInterpreter::Value::Value(const Location& location, Declaration* dec, int size)
: mLocation(location), : mLocation(location),
mDecType(dec), mDecType(dec), mDecValue(nullptr),
mBaseValue(nullptr), mOffset(0), mBaseValue(nullptr), mOffset(0),
mDataSize(size) mDataSize(size)
{ {
@ -80,7 +83,7 @@ ConstexprInterpreter::Value::Value(const Location& location, Declaration* dec, i
ConstexprInterpreter::Value::Value(const Value& value) ConstexprInterpreter::Value::Value(const Value& value)
: mLocation(value.mLocation), : mLocation(value.mLocation),
mDecType(value.mDecType), mDecType(value.mDecType), mDecValue(nullptr),
mBaseValue(value.mBaseValue), mOffset(value.mOffset), mBaseValue(value.mBaseValue), mOffset(value.mOffset),
mDataSize(value.mDataSize) mDataSize(value.mDataSize)
@ -96,7 +99,7 @@ ConstexprInterpreter::Value::Value(const Value& value)
ConstexprInterpreter::Value::Value(Value&& value) ConstexprInterpreter::Value::Value(Value&& value)
: mLocation(value.mLocation), : mLocation(value.mLocation),
mDecType(value.mDecType), mDecType(value.mDecType), mDecValue(nullptr),
mBaseValue(value.mBaseValue), mOffset(value.mOffset), mBaseValue(value.mBaseValue), mOffset(value.mOffset),
mDataSize(value.mDataSize) mDataSize(value.mDataSize)
{ {
@ -115,7 +118,7 @@ ConstexprInterpreter::Value::Value(Value&& value)
ConstexprInterpreter::Value::Value(Value* value) ConstexprInterpreter::Value::Value(Value* value)
: mLocation(value->mLocation), : mLocation(value->mLocation),
mDecType(value->mDecType), mDecType(value->mDecType), mDecValue(value->mDecValue),
mBaseValue(value), mOffset(0), mBaseValue(value), mOffset(0),
mDataSize(0), mData(mShortData) mDataSize(0), mData(mShortData)
{ {
@ -123,15 +126,23 @@ ConstexprInterpreter::Value::Value(Value* value)
ConstexprInterpreter::Value::Value(const Location& location, Value* value, Declaration* type, int offset) ConstexprInterpreter::Value::Value(const Location& location, Value* value, Declaration* type, int offset)
: mLocation(location), : mLocation(location),
mDecType(type), mDecType(type), mDecValue(nullptr),
mBaseValue(value), mOffset(offset), mBaseValue(value), mOffset(offset),
mDataSize(0), mData(mShortData) mDataSize(0), mData(mShortData)
{ {
} }
ConstexprInterpreter::Value::Value(const Location& location, Declaration* value, Declaration* type, int offset)
: mLocation(location),
mDecType(type), mDecValue(value),
mBaseValue(nullptr), mOffset(offset),
mDataSize(0), mData(mShortData)
{
}
ConstexprInterpreter::Value::Value(const Location& location, const uint8* data, Declaration* type) ConstexprInterpreter::Value::Value(const Location& location, const uint8* data, Declaration* type)
: mLocation(location), : mLocation(location),
mDecType(type), mDecType(type), mDecValue(nullptr),
mBaseValue(nullptr), mOffset(0), mBaseValue(nullptr), mOffset(0),
mDataSize(type->mSize) mDataSize(type->mSize)
{ {
@ -146,7 +157,7 @@ ConstexprInterpreter::Value::Value(const Location& location, const uint8* data,
ConstexprInterpreter::Value::Value(const Location& location, const ValueItem* data, Declaration* type) ConstexprInterpreter::Value::Value(const Location& location, const ValueItem* data, Declaration* type)
: mLocation(location), : mLocation(location),
mDecType(type), mDecType(type), mDecValue(nullptr),
mBaseValue(nullptr), mOffset(0), mBaseValue(nullptr), mOffset(0),
mDataSize(type->mSize) mDataSize(type->mSize)
{ {
@ -339,6 +350,14 @@ ConstexprInterpreter::Value ConstexprInterpreter::Value::GetPtrAt(int at, Declar
return Value(mLocation, dp->mBaseValue, type, uint16(dp[0].mByte | ((uint32)(dp[1].mByte) << 8))); return Value(mLocation, dp->mBaseValue, type, uint16(dp[0].mByte | ((uint32)(dp[1].mByte) << 8)));
} }
void ConstexprInterpreter::Value::PutVarAt(Declaration* var, int64 v, int at, Declaration* type)
{
ValueItem* dp = GetAddr() + at;
dp[0].mByte = uint8(v & 0xff);
dp[1].mByte = uint8((v >> 8) & 0xff);
mDecValue = var;
}
void ConstexprInterpreter::Value::PutIntAt(int64 v, int at, Declaration* type) void ConstexprInterpreter::Value::PutIntAt(int64 v, int at, Declaration* type)
{ {
if (type->mType == DT_TYPE_FLOAT) if (type->mType == DT_TYPE_FLOAT)
@ -521,7 +540,26 @@ Declaration* ConstexprInterpreter::Value::GetConst(int offset, Declaration* type
Declaration* target; Declaration* target;
if (vp.mBaseValue->mDecType->mType == DT_TYPE_ARRAY) if (vp.mBaseValue->mDecValue)
{
target = new Declaration(mLocation, DT_VARIABLE_REF);
if (vp.mBaseValue->mDecValue->mType == DT_VARIABLE_REF)
{
target->mBase = vp.mBaseValue->mDecValue->mBase;
target->mOffset = vp.mBaseValue->mDecValue->mOffset;
}
else
target->mBase = vp.mBaseValue->mDecValue;
target->mOffset += vp.mOffset;
dec->mValue = new Expression(mLocation, EX_CONSTANT);
dec->mValue->mDecType = type;
dec->mValue->mDecValue = target;
return dec;
}
else if (vp.mBaseValue->mDecType->mType == DT_TYPE_ARRAY)
{ {
target = new Declaration(mLocation, DT_CONST_DATA); target = new Declaration(mLocation, DT_CONST_DATA);
target->mSize = vp.mBaseValue->mDataSize; target->mSize = vp.mBaseValue->mDataSize;
@ -540,6 +578,14 @@ Declaration* ConstexprInterpreter::Value::GetConst(int offset, Declaration* type
dec->mValue->mDecType = target->mBase; dec->mValue->mDecType = target->mBase;
dec->mValue->mDecValue = target; dec->mValue->mDecValue = target;
} }
else if (vp.mDecValue)
{
dec = new Declaration(mLocation, DT_VARIABLE_REF);
dec->mBase = vp.mDecValue;
dec->mFlags = 0;
dec->mSize = type->mSize;
dec->mOffset = vp.mOffset;
}
else else
{ {
dec = new Declaration(mLocation, DT_CONST_ADDRESS); dec = new Declaration(mLocation, DT_CONST_ADDRESS);
@ -690,7 +736,12 @@ bool ConstexprInterpreter::AddParam(int& pos, Expression* pex, Declaration* dec)
{ {
mParams[pos] = Value(pex->mLocation, pex->mDecValue->mBase); mParams[pos] = Value(pex->mLocation, pex->mDecValue->mBase);
if (pex->mDecValue->mSize > 0) if (pex->mDecValue->mSize > 0)
{
if (pex->mDecValue->mValue)
mParams[pos].PutConst(0, pex->mDecValue->mValue->mDecValue); mParams[pos].PutConst(0, pex->mDecValue->mValue->mDecValue);
else
return false;
}
} }
else else
return false; return false;

View File

@ -35,6 +35,7 @@ protected:
Value(const Value& value); Value(const Value& value);
Value(Value&& value); Value(Value&& value);
Value(const Location& location, Value * value, Declaration * type, int offset); Value(const Location& location, Value * value, Declaration * type, int offset);
Value(const Location& location, Declaration * value, Declaration* type, int offset);
Value(Value* value); Value(Value* value);
Value(const Location& location, const uint8 * data, Declaration* type); Value(const Location& location, const uint8 * data, Declaration* type);
Value(const Location& location, const ValueItem* data, Declaration* type); Value(const Location& location, const ValueItem* data, Declaration* type);
@ -48,7 +49,7 @@ protected:
void Assign(const Value& v); void Assign(const Value& v);
Location mLocation; Location mLocation;
Declaration * mDecType; Declaration * mDecType, * mDecValue;
Value * mBaseValue; Value * mBaseValue;
int mOffset; int mOffset;
ValueItem * mData; ValueItem * mData;
@ -71,6 +72,7 @@ protected:
void PutIntAt(int64 v, int at, Declaration* type); void PutIntAt(int64 v, int at, Declaration* type);
void PutFloatAt(double v, int at, Declaration* type); void PutFloatAt(double v, int at, Declaration* type);
void PutPtrAt(const Value& v, int at, Declaration* type); void PutPtrAt(const Value& v, int at, Declaration* type);
void PutVarAt(Declaration* var, int64 v, int at, Declaration* type);
void PutConst(int offset, Declaration * dec); void PutConst(int offset, Declaration * dec);
Declaration* GetConst(int offset, Declaration* type, LinkerSection* dataSection) const; Declaration* GetConst(int offset, Declaration* type, LinkerSection* dataSection) const;

View File

@ -21840,6 +21840,9 @@ bool NativeCodeBasicBlock::LoopRegisterXYMap(void)
if (xfree) if (xfree)
{ {
pblock->mIns.Push(NativeCodeInstruction(pblock->mBranchIns, ASMIT_LDX, ASMIM_ZERO_PAGE, mini)); pblock->mIns.Push(NativeCodeInstruction(pblock->mBranchIns, ASMIT_LDX, ASMIM_ZERO_PAGE, mini));
if (pblock->mExitRequiredRegs[CPU_REG_Z])
pblock->mIns.Push(NativeCodeInstruction(pblock->mBranchIns, ASMIT_ORA, ASMIM_IMMEDIATE, 0));
pblock->mExitRequiredRegs += CPU_REG_X; pblock->mExitRequiredRegs += CPU_REG_X;
for (int i = 0; i < eblocks.Size(); i++) for (int i = 0; i < eblocks.Size(); i++)
{ {
@ -21850,6 +21853,9 @@ bool NativeCodeBasicBlock::LoopRegisterXYMap(void)
else else
{ {
pblock->mIns.Push(NativeCodeInstruction(pblock->mBranchIns, ASMIT_LDY, ASMIM_ZERO_PAGE, mini)); pblock->mIns.Push(NativeCodeInstruction(pblock->mBranchIns, ASMIT_LDY, ASMIM_ZERO_PAGE, mini));
if (pblock->mExitRequiredRegs[CPU_REG_Z])
pblock->mIns.Push(NativeCodeInstruction(pblock->mBranchIns, ASMIT_ORA, ASMIM_IMMEDIATE, 0));
pblock->mExitRequiredRegs += CPU_REG_Y; pblock->mExitRequiredRegs += CPU_REG_Y;
for (int i = 0; i < eblocks.Size(); i++) for (int i = 0; i < eblocks.Size(); i++)
{ {
@ -50373,7 +50379,7 @@ void NativeCodeProcedure::Compile(InterCodeProcedure* proc)
mInterProc = proc; mInterProc = proc;
mInterProc->mLinkerObject->mNativeProc = this; mInterProc->mLinkerObject->mNativeProc = this;
CheckFunc = !strcmp(mInterProc->mIdent->mString, "opp::vector<struct opp::pair<i16,i16>>::reserve"); CheckFunc = !strcmp(mInterProc->mIdent->mString, "screen_flip");
int nblocks = proc->mBlocks.Size(); int nblocks = proc->mBlocks.Size();
tblocks = new NativeCodeBasicBlock * [nblocks]; tblocks = new NativeCodeBasicBlock * [nblocks];
@ -51525,7 +51531,6 @@ void NativeCodeProcedure::Optimize(void)
CheckBlocks(); CheckBlocks();
if (step > 4 && !changed) if (step > 4 && !changed)
{ {
ResetVisited(); ResetVisited();