mirror of
https://github.com/OpenTTD/OpenTTD.git
synced 2025-01-18 05:01:13 +00:00
Codechange: Replace type-punning with std::bit_cast
in squirrel. (#12224)
This commit is contained in:
parent
de8a840db5
commit
55a328c586
@ -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
|
||||
|
@ -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));
|
||||
|
19
src/3rdparty/squirrel/squirrel/sqfuncstate.cpp
vendored
19
src/3rdparty/squirrel/squirrel/sqfuncstate.cpp
vendored
@ -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;
|
||||
};
|
||||
}
|
||||
|
||||
|
8
src/3rdparty/squirrel/squirrel/sqvm.cpp
vendored
8
src/3rdparty/squirrel/squirrel/sqvm.cpp
vendored
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user