Codechange: Replace type-punning with `std::bit_cast` in squirrel. (#12224)

This commit is contained in:
Peter Nelson 2024-03-09 17:13:17 +00:00 committed by GitHub
parent de8a840db5
commit 55a328c586
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 16 additions and 22 deletions

View File

@ -74,12 +74,6 @@ macro(compile_flags)
# We use 'ABCD' multichar for SaveLoad chunks identifiers
-Wno-multichar
# Compilers complains about that we break strict-aliasing.
# On most places we don't see how to fix it, and it doesn't
# break anything. So disable strict-aliasing to make the
# compiler all happy.
-fno-strict-aliasing
)
# Ninja processes the output so the output from the compiler

View File

@ -650,8 +650,7 @@ public:
_fs->AddInstruction(_OP_LOADINT, _exst._deref,_integer(constval));
}
else if(ctype == OT_FLOAT && sizeof(SQFloat) == sizeof(SQInt32)) {
SQFloat f = _float(constval);
_fs->AddInstruction(_OP_LOADFLOAT, _exst._deref,*((SQInt32 *)&f));
_fs->AddInstruction(_OP_LOADFLOAT, _exst._deref, std::bit_cast<SQInt32>(_float(constval)));
}
else {
_fs->AddInstruction(_OP_LOAD, _exst._deref, _fs->GetConstant(constval));
@ -697,7 +696,7 @@ public:
break;
case TK_FLOAT:
if(sizeof(SQFloat) == sizeof(SQInt32)) {
_fs->AddInstruction(_OP_LOADFLOAT, _fs->PushTarget(),*((SQInt32 *)&_lex._fvalue));
_fs->AddInstruction(_OP_LOADFLOAT, _fs->PushTarget(), std::bit_cast<SQInt32>(_lex._fvalue));
}
else {
_fs->AddInstruction(_OP_LOAD, _fs->PushTarget(), _fs->GetNumericConstant(_lex._fvalue));

View File

@ -199,7 +199,7 @@ void SQFuncState::Dump(SQFunctionProto *func)
}
}
else if(inst.op==_OP_LOADFLOAT) {
printf("[%03d] %15s %d %f %d %d\n",n,g_InstrDesc[inst.op].name,inst._arg0,*((SQFloat*)&inst._arg1),inst._arg2,inst._arg3);
printf("[%03d] %15s %d %f %d %d\n",n,g_InstrDesc[inst.op].name,inst._arg0,std::bit_cast<SQFloat>(inst._arg1),inst._arg2,inst._arg3);
}
else if(inst.op==_OP_ARITH){
printf("[%03d] %15s %d %d %d %c\n",n,g_InstrDesc[inst.op].name,inst._arg0,inst._arg1,inst._arg2,inst._arg3);
@ -242,19 +242,20 @@ SQInteger SQFuncState::GetConstant(const SQObject &cons)
void SQFuncState::SetIntructionParams(SQInteger pos,SQInteger arg0,SQInteger arg1,SQInteger arg2,SQInteger arg3)
{
_instructions[pos]._arg0=(unsigned char)*((SQUnsignedInteger *)&arg0);
_instructions[pos]._arg1=(SQInt32)*((SQUnsignedInteger *)&arg1);
_instructions[pos]._arg2=(unsigned char)*((SQUnsignedInteger *)&arg2);
_instructions[pos]._arg3=(unsigned char)*((SQUnsignedInteger *)&arg3);
_instructions[pos]._arg0 = (unsigned char)std::bit_cast<SQUnsignedInteger>(arg0);
_instructions[pos]._arg1 = (SQInt32)std::bit_cast<SQUnsignedInteger>(arg1);
_instructions[pos]._arg2 = (unsigned char)std::bit_cast<SQUnsignedInteger>(arg2);
_instructions[pos]._arg3 = (unsigned char)std::bit_cast<SQUnsignedInteger>(arg3);
}
void SQFuncState::SetIntructionParam(SQInteger pos,SQInteger arg,SQInteger val)
{
switch(arg){
case 0:_instructions[pos]._arg0=(unsigned char)*((SQUnsignedInteger *)&val);break;
case 1:case 4:_instructions[pos]._arg1=(SQInt32)*((SQUnsignedInteger *)&val);break;
case 2:_instructions[pos]._arg2=(unsigned char)*((SQUnsignedInteger *)&val);break;
case 3:_instructions[pos]._arg3=(unsigned char)*((SQUnsignedInteger *)&val);break;
case 0: _instructions[pos]._arg0 = (unsigned char)std::bit_cast<SQUnsignedInteger>(val); break;
case 1:
case 4: _instructions[pos]._arg1 = (SQInt32)std::bit_cast<SQUnsignedInteger>(val); break;
case 2: _instructions[pos]._arg2 = (unsigned char)std::bit_cast<SQUnsignedInteger>(val); break;
case 3: _instructions[pos]._arg3 = (unsigned char)std::bit_cast<SQUnsignedInteger>(val); break;
};
}

View File

@ -51,7 +51,7 @@ bool SQVM::BW_OP(SQUnsignedInteger op,SQObjectPtr &trg,const SQObjectPtr &o1,con
case BW_XOR: res = i1 ^ i2; break;
case BW_SHIFTL: res = i1 << i2; break;
case BW_SHIFTR: res = i1 >> i2; break;
case BW_USHIFTR:res = (SQInteger)(*((SQUnsignedInteger*)&i1) >> i2); break;
case BW_USHIFTR:res = (SQInteger)(std::bit_cast<SQUnsignedInteger>(i1) >> i2); break;
default: { Raise_Error("internal vm error bitwise op failed"); return false; }
}
}
@ -472,10 +472,10 @@ bool SQVM::DerefInc(SQInteger op,SQObjectPtr &target, SQObjectPtr &self, SQObjec
#define arg0 (_i_._arg0)
#define arg1 (_i_._arg1)
#define sarg1 (*(const_cast<SQInt32 *>(&_i_._arg1)))
#define sarg1 (std::bit_cast<SQInt32>(_i_._arg1))
#define arg2 (_i_._arg2)
#define arg3 (_i_._arg3)
#define sarg3 ((SQInteger)*((const signed char *)&_i_._arg3))
#define sarg3 ((SQInteger)std::bit_cast<char>(_i_._arg3))
SQRESULT SQVM::Suspend()
{
@ -764,7 +764,7 @@ exception_restore:
continue;
case _OP_LOAD: TARGET = ci->_literals[arg1]; continue;
case _OP_LOADINT: TARGET = (SQInteger)arg1; continue;
case _OP_LOADFLOAT: TARGET = *((const SQFloat *)&arg1); continue;
case _OP_LOADFLOAT: TARGET = std::bit_cast<SQFloat>(arg1); continue;
case _OP_DLOAD: TARGET = ci->_literals[arg1]; STK(arg2) = ci->_literals[arg3];continue;
case _OP_TAILCALL:
temp_reg = STK(arg1);