Skip to content

Commit 1deb3f8

Browse files
kgSimaTian
authored andcommitted
Implement sizeof() in the interpreter (#115745)
Implements CEE_SIZEOF in the interpreter
1 parent 3a6e85c commit 1deb3f8

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

src/coreclr/interpreter/compiler.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3500,6 +3500,16 @@ int InterpCompiler::GenerateCode(CORINFO_METHOD_INFO* methodInfo)
35003500
m_pLastNewIns->SetDVar(m_pStackPointer[-1].var);
35013501
m_ip++;
35023502
break;
3503+
case CEE_SIZEOF:
3504+
{
3505+
CORINFO_CLASS_HANDLE clsHnd = ResolveClassToken(getU4LittleEndian(m_ip + 1));
3506+
AddIns(INTOP_LDC_I4);
3507+
m_pLastNewIns->data[0] = m_compHnd->getClassSize(clsHnd);
3508+
PushStackType(StackTypeI4, NULL);
3509+
m_pLastNewIns->SetDVar(m_pStackPointer[-1].var);
3510+
m_ip += 5;
3511+
break;
3512+
}
35033513
default:
35043514
assert(0);
35053515
break;

src/tests/JIT/interpreter/Interpreter.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,9 @@ public static void RunInterpreterTests()
416416
if (!TestArray())
417417
Environment.FailFast(null);
418418

419+
if (!TestSizeof())
420+
Environment.FailFast(null);
421+
419422
System.GC.Collect();
420423
}
421424

@@ -945,4 +948,15 @@ public static bool ArrayDouble(int length, double value)
945948

946949
return true;
947950
}
951+
952+
public static unsafe bool TestSizeof()
953+
{
954+
if (sizeof(int) != 4)
955+
return false;
956+
if (sizeof(double) != 8)
957+
return false;
958+
if (sizeof(MyStruct) != 4)
959+
return false;
960+
return true;
961+
}
948962
}

0 commit comments

Comments
 (0)