Skip to content

Commit 6ad4cd1

Browse files
committed
Add a few sanity tests to ensure new interp functionality mostly works
Minor fixes to ensure tests work. Add support for returns of any type. Add opcode for loading full I4.
1 parent 468c24a commit 6ad4cd1

File tree

4 files changed

+80
-7
lines changed

4 files changed

+80
-7
lines changed

src/coreclr/interpreter/compiler.cpp

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -808,6 +808,8 @@ static InterpType GetInterpType(CorInfoType corInfoType)
808808
case CORINFO_TYPE_VALUECLASS:
809809
case CORINFO_TYPE_REFANY:
810810
return InterpTypeVT;
811+
case CORINFO_TYPE_VOID:
812+
return InterpTypeVoid;
811813
default:
812814
assert(0);
813815
break;
@@ -1629,6 +1631,13 @@ int InterpCompiler::GenerateCode(CORINFO_METHOD_INFO* methodInfo)
16291631
m_pLastIns->SetDVar(m_pStackPointer[-1].var);
16301632
m_ip += 2;
16311633
break;
1634+
case CEE_LDC_I4:
1635+
AddIns(INTOP_LDC_I4);
1636+
m_pLastIns->data[0] = getI4LittleEndian(m_ip + 1);
1637+
PushStackType(StackTypeI4, NULL);
1638+
m_pLastIns->SetDVar(m_pStackPointer[-1].var);
1639+
m_ip += 5;
1640+
break;
16321641
case CEE_LDNULL:
16331642
AddIns(INTOP_LDNULL);
16341643
PushStackType(StackTypeO, NULL);
@@ -1677,21 +1686,27 @@ int InterpCompiler::GenerateCode(CORINFO_METHOD_INFO* methodInfo)
16771686
case CEE_RET:
16781687
{
16791688
CORINFO_SIG_INFO sig = methodInfo->args;
1680-
if (sig.retType == CORINFO_TYPE_VOID)
1689+
InterpType retType = GetInterpType(sig.retType);
1690+
1691+
if (retType == InterpTypeVoid)
16811692
{
16821693
AddIns(INTOP_RET_VOID);
16831694
}
1684-
else if (sig.retType == CORINFO_TYPE_INT)
1695+
else if (retType == InterpTypeVT)
16851696
{
16861697
CHECK_STACK(1);
1687-
AddIns(INTOP_RET);
1698+
AddIns(INTOP_RET_VT);
16881699
m_pStackPointer--;
1689-
m_pLastIns->SetSVar(m_pStackPointer[0].var);
1700+
int32_t retVar = m_pStackPointer[0].var;
1701+
m_pLastIns->SetSVar(retVar);
1702+
m_pLastIns->data[0] = m_pVars[retVar].size;
16901703
}
16911704
else
16921705
{
1693-
// FIXME
1694-
assert(0);
1706+
CHECK_STACK(1);
1707+
AddIns(INTOP_RET);
1708+
m_pStackPointer--;
1709+
m_pLastIns->SetSVar(m_pStackPointer[0].var);
16951710
}
16961711
m_ip++;
16971712
break;

src/coreclr/interpreter/intops.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
OPDEF(INTOP_NOP, "nop", 1, 0, 0, InterpOpNoArgs)
1010
OPDEF(INTOP_RET, "ret", 2, 0, 1, InterpOpNoArgs)
11+
OPDEF(INTOP_RET_VT, "ret.vt", 3, 0, 1, InterpOpInt)
1112
OPDEF(INTOP_RET_VOID, "ret.void", 1, 0, 0, InterpOpNoArgs)
1213

1314
OPDEF(INTOP_LDC_I4, "ldc.i4", 3, 1, 0, InterpOpInt)

src/coreclr/vm/interpexec.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ void InterpExecMethod(InterpMethodContextFrame *pFrame, InterpThreadContext *pTh
6464
// Return stack slot sized value
6565
*(int64_t*)pFrame->pRetVal = LOCAL_VAR(ip[1], int64_t);
6666
goto EXIT_FRAME;
67+
case INTOP_RET_VT:
68+
memmove(pFrame->pRetVal, stack + ip[1], ip[2]);
69+
goto EXIT_FRAME;
6770
case INTOP_RET_VOID:
6871
goto EXIT_FRAME;
6972

src/tests/JIT/interpreter/Interpreter.cs

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,61 @@ static int Main(string[] args)
1515
[MethodImpl(MethodImplOptions.NoInlining)]
1616
public static void RunInterpreterTests()
1717
{
18-
// Console.WriteLine("Run interp tests");
18+
// Console.WriteLine("Run interp tests");
19+
if (SumN(50) != 1275)
20+
Environment.FailFast(null);
21+
if (Mul4(53, 24, 13, 131) != 2166216)
22+
Environment.FailFast(null);
23+
24+
TestSwitch();
25+
26+
if (!PowLoop(20, 10, 1661992960))
27+
Environment.FailFast(null);
28+
}
29+
30+
public static int Mul4(int a, int b, int c, int d)
31+
{
32+
return a * b * c * d;
1933
}
2034

35+
public static long SumN(int n)
36+
{
37+
if (n == 1)
38+
return 1;
39+
return (long)SumN(n - 1) + n;
40+
}
41+
42+
public static int SwitchOp(int a, int b, int op)
43+
{
44+
switch (op)
45+
{
46+
case 0:
47+
return a + b;
48+
case 1:
49+
return a - b;
50+
case 2:
51+
return a * b;
52+
default:
53+
return 42;
54+
}
55+
}
56+
57+
public static void TestSwitch()
58+
{
59+
int n0 = SwitchOp (20, 6, 0); // 26
60+
int n1 = SwitchOp (20, 6, 1); // 14
61+
int n2 = SwitchOp (20, 6, 2); // 120
62+
int n3 = SwitchOp (20, 6, 3); // 42
63+
64+
if ((n0 + n1 + n2 + n3) != 202)
65+
Environment.FailFast(null);
66+
}
67+
68+
public static bool PowLoop(int n, long nr, int expected)
69+
{
70+
long ret = 1;
71+
for (int i = 0; i < n; i++)
72+
ret *= nr;
73+
return (int)ret == expected;
74+
}
2175
}

0 commit comments

Comments
 (0)