Skip to content

Commit 1bd56d4

Browse files
authored
Implement interpreter CEE_ADD_OVF and CEE_SUB_OVF opcodes (#116694)
These are needed for a couple of coreclr tests
1 parent 1a52cd6 commit 1bd56d4

File tree

3 files changed

+117
-0
lines changed

3 files changed

+117
-0
lines changed

src/coreclr/interpreter/compiler.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3972,10 +3972,26 @@ int InterpCompiler::GenerateCode(CORINFO_METHOD_INFO* methodInfo)
39723972
EmitBinaryArithmeticOp(INTOP_ADD_I4);
39733973
m_ip++;
39743974
break;
3975+
case CEE_ADD_OVF:
3976+
EmitBinaryArithmeticOp(INTOP_ADD_OVF_I4);
3977+
m_ip++;
3978+
break;
3979+
case CEE_ADD_OVF_UN:
3980+
EmitBinaryArithmeticOp(INTOP_ADD_OVF_UN_I4);
3981+
m_ip++;
3982+
break;
39753983
case CEE_SUB:
39763984
EmitBinaryArithmeticOp(INTOP_SUB_I4);
39773985
m_ip++;
39783986
break;
3987+
case CEE_SUB_OVF:
3988+
EmitBinaryArithmeticOp(INTOP_SUB_OVF_I4);
3989+
m_ip++;
3990+
break;
3991+
case CEE_SUB_OVF_UN:
3992+
EmitBinaryArithmeticOp(INTOP_SUB_OVF_UN_I4);
3993+
m_ip++;
3994+
break;
39793995
case CEE_MUL:
39803996
EmitBinaryArithmeticOp(INTOP_MUL_I4);
39813997
m_ip++;

src/coreclr/interpreter/intops.def

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,11 +221,23 @@ OPDEF(INTOP_ADD_I8, "add.i8", 4, 1, 2, InterpOpNoArgs)
221221
OPDEF(INTOP_ADD_R4, "add.r4", 4, 1, 2, InterpOpNoArgs)
222222
OPDEF(INTOP_ADD_R8, "add.r8", 4, 1, 2, InterpOpNoArgs)
223223

224+
OPDEF(INTOP_ADD_OVF_I4, "add.ovf.i4", 4, 1, 2, InterpOpNoArgs)
225+
OPDEF(INTOP_ADD_OVF_I8, "add.ovf.i8", 4, 1, 2, InterpOpNoArgs)
226+
227+
OPDEF(INTOP_ADD_OVF_UN_I4, "add.ovf.un.i4", 4, 1, 2, InterpOpNoArgs)
228+
OPDEF(INTOP_ADD_OVF_UN_I8, "add.ovf.un.i8", 4, 1, 2, InterpOpNoArgs)
229+
224230
OPDEF(INTOP_SUB_I4, "sub.i4", 4, 1, 2, InterpOpNoArgs)
225231
OPDEF(INTOP_SUB_I8, "sub.i8", 4, 1, 2, InterpOpNoArgs)
226232
OPDEF(INTOP_SUB_R4, "sub.r4", 4, 1, 2, InterpOpNoArgs)
227233
OPDEF(INTOP_SUB_R8, "sub.r8", 4, 1, 2, InterpOpNoArgs)
228234

235+
OPDEF(INTOP_SUB_OVF_I4, "sub.ovf.i4", 4, 1, 2, InterpOpNoArgs)
236+
OPDEF(INTOP_SUB_OVF_I8, "sub.ovf.i8", 4, 1, 2, InterpOpNoArgs)
237+
238+
OPDEF(INTOP_SUB_OVF_UN_I4, "sub.ovf.un.i4", 4, 1, 2, InterpOpNoArgs)
239+
OPDEF(INTOP_SUB_OVF_UN_I8, "sub.ovf.un.i8", 4, 1, 2, InterpOpNoArgs)
240+
229241
OPDEF(INTOP_MUL_I4, "mul.i4", 4, 1, 2, InterpOpNoArgs)
230242
OPDEF(INTOP_MUL_I8, "mul.i8", 4, 1, 2, InterpOpNoArgs)
231243
OPDEF(INTOP_MUL_R4, "mul.r4", 4, 1, 2, InterpOpNoArgs)

src/coreclr/vm/interpexec.cpp

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -980,6 +980,50 @@ void InterpExecMethod(InterpreterFrame *pInterpreterFrame, InterpMethodContextFr
980980
LOCAL_VAR(ip[1], int64_t) = LOCAL_VAR(ip[2], int64_t) + ip[3];
981981
ip += 4;
982982
break;
983+
case INTOP_ADD_OVF_I4:
984+
{
985+
int32_t i1 = LOCAL_VAR(ip[2], int32_t);
986+
int32_t i2 = LOCAL_VAR(ip[3], int32_t);
987+
int32_t i3;
988+
if (!ClrSafeInt<int32_t>::addition(i1, i2, i3))
989+
COMPlusThrow(kOverflowException);
990+
LOCAL_VAR(ip[1], int32_t) = i3;
991+
ip += 4;
992+
break;
993+
}
994+
case INTOP_ADD_OVF_I8:
995+
{
996+
int64_t i1 = LOCAL_VAR(ip[2], int64_t);
997+
int64_t i2 = LOCAL_VAR(ip[3], int64_t);
998+
int64_t i3;
999+
if (!ClrSafeInt<int64_t>::addition(i1, i2, i3))
1000+
COMPlusThrow(kOverflowException);
1001+
LOCAL_VAR(ip[1], int64_t) = i3;
1002+
ip += 4;
1003+
break;
1004+
}
1005+
case INTOP_ADD_OVF_UN_I4:
1006+
{
1007+
uint32_t i1 = LOCAL_VAR(ip[2], uint32_t);
1008+
uint32_t i2 = LOCAL_VAR(ip[3], uint32_t);
1009+
uint32_t i3;
1010+
if (!ClrSafeInt<uint32_t>::addition(i1, i2, i3))
1011+
COMPlusThrow(kOverflowException);
1012+
LOCAL_VAR(ip[1], uint32_t) = i3;
1013+
ip += 4;
1014+
break;
1015+
}
1016+
case INTOP_ADD_OVF_UN_I8:
1017+
{
1018+
uint64_t i1 = LOCAL_VAR(ip[2], uint64_t);
1019+
uint64_t i2 = LOCAL_VAR(ip[3], uint64_t);
1020+
uint64_t i3;
1021+
if (!ClrSafeInt<uint64_t>::addition(i1, i2, i3))
1022+
COMPlusThrow(kOverflowException);
1023+
LOCAL_VAR(ip[1], uint64_t) = i3;
1024+
ip += 4;
1025+
break;
1026+
}
9831027
case INTOP_SUB_I4:
9841028
LOCAL_VAR(ip[1], int32_t) = LOCAL_VAR(ip[2], int32_t) - LOCAL_VAR(ip[3], int32_t);
9851029
ip += 4;
@@ -997,6 +1041,51 @@ void InterpExecMethod(InterpreterFrame *pInterpreterFrame, InterpMethodContextFr
9971041
ip += 4;
9981042
break;
9991043

1044+
case INTOP_SUB_OVF_I4:
1045+
{
1046+
int32_t i1 = LOCAL_VAR(ip[2], int32_t);
1047+
int32_t i2 = LOCAL_VAR(ip[3], int32_t);
1048+
int32_t i3;
1049+
if (!ClrSafeInt<int32_t>::subtraction(i1, i2, i3))
1050+
COMPlusThrow(kOverflowException);
1051+
LOCAL_VAR(ip[1], int32_t) = i3;
1052+
ip += 4;
1053+
break;
1054+
}
1055+
case INTOP_SUB_OVF_I8:
1056+
{
1057+
int64_t i1 = LOCAL_VAR(ip[2], int64_t);
1058+
int64_t i2 = LOCAL_VAR(ip[3], int64_t);
1059+
int64_t i3;
1060+
if (!ClrSafeInt<int64_t>::subtraction(i1, i2, i3))
1061+
COMPlusThrow(kOverflowException);
1062+
LOCAL_VAR(ip[1], int64_t) = i3;
1063+
ip += 4;
1064+
break;
1065+
}
1066+
case INTOP_SUB_OVF_UN_I4:
1067+
{
1068+
uint32_t i1 = LOCAL_VAR(ip[2], uint32_t);
1069+
uint32_t i2 = LOCAL_VAR(ip[3], uint32_t);
1070+
uint32_t i3;
1071+
if (!ClrSafeInt<uint32_t>::subtraction(i1, i2, i3))
1072+
COMPlusThrow(kOverflowException);
1073+
LOCAL_VAR(ip[1], uint32_t) = i3;
1074+
ip += 4;
1075+
break;
1076+
}
1077+
case INTOP_SUB_OVF_UN_I8:
1078+
{
1079+
uint64_t i1 = LOCAL_VAR(ip[2], uint64_t);
1080+
uint64_t i2 = LOCAL_VAR(ip[3], uint64_t);
1081+
uint64_t i3;
1082+
if (!ClrSafeInt<uint64_t>::subtraction(i1, i2, i3))
1083+
COMPlusThrow(kOverflowException);
1084+
LOCAL_VAR(ip[1], uint64_t) = i3;
1085+
ip += 4;
1086+
break;
1087+
}
1088+
10001089
case INTOP_MUL_I4:
10011090
LOCAL_VAR(ip[1], int32_t) = LOCAL_VAR(ip[2], int32_t) * LOCAL_VAR(ip[3], int32_t);
10021091
ip += 4;

0 commit comments

Comments
 (0)