Skip to content

Commit

Permalink
Add signed->unsigned promotion for binary operators
Browse files Browse the repository at this point in the history
The C-style rules for integer promotion are that when you have a signed int and an unsigned int, if you can't promote to a wider type, then the signed type is promoted to an unsigned type.
  • Loading branch information
rheit authored and coelckers committed Aug 2, 2022
1 parent 15c5728 commit 7ce29fe
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/common/scripting/backend/codegen.cpp
Expand Up @@ -2685,6 +2685,19 @@ bool FxBinary::Promote(FCompileContext &ctx, bool forceint)
{
ValueType = TypeUInt32;
}
// If one side is an unsigned 32-bit int and the other side is a signed 32-bit int, the signed side is implicitly converted to unsigned.
else if (!ctx.FromDecorate && left->ValueType == TypeUInt32 && right->ValueType == TypeSInt32)
{
right = new FxIntCast(right, false, false, true);
right = right->Resolve(ctx);
ValueType = TypeUInt32;
}
else if (!ctx.FromDecorate && left->ValueType == TypeSInt32 && right->ValueType == TypeUInt32)
{
left = new FxIntCast(left, false, false, true);
left = left->Resolve(ctx);
ValueType = TypeUInt32;
}
else if (left->IsInteger() && right->IsInteger())
{
ValueType = TypeSInt32; // Addition and subtraction forces all integer-derived types to signed int.
Expand Down

0 comments on commit 7ce29fe

Please sign in to comment.