Add warning for const integer truncation

This commit is contained in:
drmortalwombat 2023-04-09 09:45:13 +02:00
parent d5c347da9b
commit 85df217c50
6 changed files with 99 additions and 4 deletions

View File

@ -3,7 +3,7 @@
void vram_addr(unsigned long addr) void vram_addr(unsigned long addr)
{ {
vera.ctrl &= ~VERA_CTRL_ADDRSEL; vera.ctrl &= ~VERA_CTRL_ADDRSEL;
vera.addr = addr; vera.addr = (unsigned)addr;
vera.addrh = (char)((addr >> 16) & 1) | 0x10; vera.addrh = (char)((addr >> 16) & 1) | 0x10;
} }
@ -23,6 +23,14 @@ char vram_get(void)
return vera.data0; return vera.data0;
} }
unsigned vram_getw(void)
{
unsigned l = vera.data0;
unsigned h = vera.data0;
return (h << 8) | l;
}
void vram_put_at(unsigned long addr, char data) void vram_put_at(unsigned long addr, char data)
{ {
vram_addr(addr); vram_addr(addr);
@ -99,3 +107,25 @@ void vera_spr_image(char spr, unsigned addr32)
char b = vram_get() & 0x80; char b = vram_get() & 0x80;
vram_put((addr32 >> 8) | b); vram_put((addr32 >> 8) | b);
} }
void vera_pal_put(char index, unsigned color)
{
vram_addr(0x1fa00ul + 2 * index);
vram_putw(color);
}
unsigned vera_pal_get(char index)
{
vram_addr(0x1fa00ul + 2 * index);
return vram_getw();
}
void vera_pal_putn(char index, const unsigned * color, unsigned size)
{
vram_addr(0x1fa00ul + 2 * index);
while (size > 0)
{
vram_putw(*color++);
size--;
}
}

View File

@ -108,6 +108,8 @@ enum VERASpritePriority
VSPRPRI_FRONT VSPRPRI_FRONT
}; };
#define VERA_COLOR(r, g, b) (((unsigned)(r) << 8) | ((unsigned)(g) << 4) | (unsigned)(b))
#define vera (*(VERA *)0x9f20) #define vera (*(VERA *)0x9f20)
inline void vram_addr(unsigned long addr); inline void vram_addr(unsigned long addr);
@ -118,6 +120,8 @@ inline void vram_putw(unsigned data);
inline char vram_get(void); inline char vram_get(void);
inline unsigned vram_getw(void);
inline void vram_put_at(unsigned long addr, char data); inline void vram_put_at(unsigned long addr, char data);
inline char vram_get_at(unsigned long addr); inline char vram_get_at(unsigned long addr);
@ -134,6 +138,12 @@ void vera_spr_move(char spr, int x, int y);
void vera_spr_image(char spr, unsigned addr32); void vera_spr_image(char spr, unsigned addr32);
void vera_pal_put(char index, unsigned color);
unsigned vera_pal_get(char index);
void vera_pal_putn(char index, const unsigned * color, unsigned size);
#pragma compile("vera.c") #pragma compile("vera.c")
#endif #endif

View File

@ -4167,6 +4167,17 @@ void InterCodeBasicBlock::Append(InterInstruction * code)
this->mInstructions.Push(code); this->mInstructions.Push(code);
} }
const InterInstruction* InterCodeBasicBlock::FindByDst(int dst) const
{
int n = mInstructions.Size() - 1;
while (n >= 0 && mInstructions[n]->mDst.mTemp != dst)
n--;
if (n >= 0)
return mInstructions[n];
else
return nullptr;
}
void InterCodeBasicBlock::Close(InterCodeBasicBlock* trueJump, InterCodeBasicBlock* falseJump) void InterCodeBasicBlock::Close(InterCodeBasicBlock* trueJump, InterCodeBasicBlock* falseJump)
{ {
this->mTrueJump = trueJump; this->mTrueJump = trueJump;

View File

@ -384,6 +384,7 @@ public:
~InterCodeBasicBlock(void); ~InterCodeBasicBlock(void);
void Append(InterInstruction * code); void Append(InterInstruction * code);
const InterInstruction* FindByDst(int dst) const;
void Close(InterCodeBasicBlock* trueJump, InterCodeBasicBlock* falseJump); void Close(InterCodeBasicBlock* trueJump, InterCodeBasicBlock* falseJump);
void CollectEntries(void); void CollectEntries(void);

View File

@ -65,7 +65,7 @@ InterCodeGenerator::ExValue InterCodeGenerator::Dereference(InterCodeProcedure*
return v; return v;
} }
InterCodeGenerator::ExValue InterCodeGenerator::CoerceType(InterCodeProcedure* proc, Expression* exp, InterCodeBasicBlock*& block, ExValue v, Declaration* type) InterCodeGenerator::ExValue InterCodeGenerator::CoerceType(InterCodeProcedure* proc, Expression* exp, InterCodeBasicBlock*& block, ExValue v, Declaration* type, bool checkTrunc)
{ {
int stemp = v.mTemp; int stemp = v.mTemp;
@ -204,7 +204,50 @@ InterCodeGenerator::ExValue InterCodeGenerator::CoerceType(InterCodeProcedure* p
else else
{ {
// ignore size reduction // ignore size reduction
if (checkTrunc && v.mType->mSize > type->mSize)
{
const InterInstruction* ins = block->FindByDst(stemp);
if (ins && ins->mCode == IC_CONSTANT)
{
int64 min = 0, max = 0;
if (type->mFlags & DTF_SIGNED)
{
switch (type->mSize)
{
case 1:
min = -128; max = 127;
break;
case 2:
min = -32768; max = 32767;
break;
case 4:
min = -2147483648LL; max = 2147483647LL;
break;
}
}
else
{
switch (type->mSize)
{
case 1:
max = 255;
break;
case 2:
max = 65535;
break;
case 4:
max = 429467295LL;
break;
}
}
if (ins->mConst.mIntConst < min || ins->mConst.mIntConst > max)
{
mErrors->Error(exp->mLocation, EWARN_CONSTANT_TRUNCATED, "Integer constant truncated");
}
}
}
v.mType = type; v.mType = type;
} }
@ -1402,7 +1445,7 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration*
vll = CoerceType(proc, exp, block, vll, otype); vll = CoerceType(proc, exp, block, vll, otype);
} }
vr = CoerceType(proc, exp, block, vr, otype); vr = CoerceType(proc, exp, block, vr, otype, exp->mToken != TK_ASSIGN_AND);
InterInstruction * oins = new InterInstruction(exp->mLocation, IC_BINARY_OPERATOR); InterInstruction * oins = new InterInstruction(exp->mLocation, IC_BINARY_OPERATOR);
oins->mSrc[0].mType = InterTypeOf(otype); oins->mSrc[0].mType = InterTypeOf(otype);

View File

@ -56,7 +56,7 @@ protected:
void BuildSwitchTree(InterCodeProcedure* proc, Expression* exp, InterCodeBasicBlock* block, ExValue v, const SwitchNodeArray& nodes, int left, int right, InterCodeBasicBlock* dblock); void BuildSwitchTree(InterCodeProcedure* proc, Expression* exp, InterCodeBasicBlock* block, ExValue v, const SwitchNodeArray& nodes, int left, int right, InterCodeBasicBlock* dblock);
ExValue Dereference(InterCodeProcedure* proc, Expression* exp, InterCodeBasicBlock*& block, ExValue v, int level = 0); ExValue Dereference(InterCodeProcedure* proc, Expression* exp, InterCodeBasicBlock*& block, ExValue v, int level = 0);
ExValue CoerceType(InterCodeProcedure* proc, Expression* exp, InterCodeBasicBlock*& block, ExValue v, Declaration * type); ExValue CoerceType(InterCodeProcedure* proc, Expression* exp, InterCodeBasicBlock*& block, ExValue v, Declaration * type, bool checkTrunc = true);
ExValue TranslateExpression(Declaration * procType, InterCodeProcedure * proc, InterCodeBasicBlock*& block, Expression* exp, InterCodeBasicBlock* breakBlock, InterCodeBasicBlock* continueBlock, InlineMapper * inlineMapper, ExValue * lrexp = nullptr); ExValue TranslateExpression(Declaration * procType, InterCodeProcedure * proc, InterCodeBasicBlock*& block, Expression* exp, InterCodeBasicBlock* breakBlock, InterCodeBasicBlock* continueBlock, InlineMapper * inlineMapper, ExValue * lrexp = nullptr);
void TranslateLogic(Declaration* procType, InterCodeProcedure* proc, InterCodeBasicBlock* block, InterCodeBasicBlock* tblock, InterCodeBasicBlock* fblock, Expression* exp, InlineMapper* inlineMapper); void TranslateLogic(Declaration* procType, InterCodeProcedure* proc, InterCodeBasicBlock* block, InterCodeBasicBlock* tblock, InterCodeBasicBlock* fblock, Expression* exp, InlineMapper* inlineMapper);
ExValue TranslateInline(Declaration* procType, InterCodeProcedure* proc, InterCodeBasicBlock*& block, Expression* exp, InterCodeBasicBlock* breakBlock, InterCodeBasicBlock* continueBlock, InlineMapper* inlineMapper, bool inlineConstexpr); ExValue TranslateInline(Declaration* procType, InterCodeProcedure* proc, InterCodeBasicBlock*& block, Expression* exp, InterCodeBasicBlock* breakBlock, InterCodeBasicBlock* continueBlock, InlineMapper* inlineMapper, bool inlineConstexpr);