From 463432cbf131f610f96d4831c1fdfc73d4a13000 Mon Sep 17 00:00:00 2001 From: "Frank A. Krueger" Date: Fri, 2 Feb 2024 16:03:29 -0800 Subject: [PATCH] Interpreter sizeof report num values so low level functions can work --- CLanguage/Compiler/EmitContext.cs | 4 ++-- CLanguage/Syntax/SizeOfExpression.cs | 2 +- CLanguage/Syntax/SizeOfTypeExpression.cs | 2 +- CLanguageTests/ClassTests.cs | 4 ++-- CLanguageTests/InterpreterTests.cs | 14 ++++++++------ 5 files changed, 14 insertions(+), 12 deletions(-) diff --git a/CLanguage/Compiler/EmitContext.cs b/CLanguage/Compiler/EmitContext.cs index 645ddd8..05963c7 100644 --- a/CLanguage/Compiler/EmitContext.cs +++ b/CLanguage/Compiler/EmitContext.cs @@ -129,10 +129,10 @@ public void EmitCast (CType fromType, CType toType) // Demote arrays to pointers } else if (fromType is CArrayType && toType.IsVoidPointer) { - // Demote arrays to void pointers without size check. Better hope sizeof works. + // Demote arrays to void pointers without size check. sizeof() reports type.NumValues. } else if (fromType.IsPointer && toType.IsVoidPointer) { - // Demote pointers to void pointers without size check. Better hope sizeof works. + // Demote pointers to void pointers without size check. sizeof() reports type.NumValues. } else if (fromType is CEnumType et && toType is CIntType bt) { // Enums act like ints diff --git a/CLanguage/Syntax/SizeOfExpression.cs b/CLanguage/Syntax/SizeOfExpression.cs index 3e4387a..99f44d5 100644 --- a/CLanguage/Syntax/SizeOfExpression.cs +++ b/CLanguage/Syntax/SizeOfExpression.cs @@ -25,7 +25,7 @@ public override CType GetEvaluatedCType (EmitContext ec) protected override void DoEmit(EmitContext ec) { var type = Query.GetEvaluatedCType (ec); - Value cval = type.GetByteSize (ec); + Value cval = type.NumValues; ec.Emit (OpCode.LoadConstant, cval); } } diff --git a/CLanguage/Syntax/SizeOfTypeExpression.cs b/CLanguage/Syntax/SizeOfTypeExpression.cs index 14d94a7..a931330 100644 --- a/CLanguage/Syntax/SizeOfTypeExpression.cs +++ b/CLanguage/Syntax/SizeOfTypeExpression.cs @@ -22,7 +22,7 @@ public override CType GetEvaluatedCType (EmitContext ec) protected override void DoEmit (EmitContext ec) { var type = ec.ResolveTypeName (TypeName); - Value cval = type.GetByteSize (ec); + Value cval = type.NumValues; ec.Emit (OpCode.LoadConstant, cval); } } diff --git a/CLanguageTests/ClassTests.cs b/CLanguageTests/ClassTests.cs index f3c7602..c793835 100644 --- a/CLanguageTests/ClassTests.cs +++ b/CLanguageTests/ClassTests.cs @@ -33,8 +33,8 @@ typedef class C { } OtherC; OtherC c; void main() { - assertAreEqual(2, sizeof(c)); - assertAreEqual(2, sizeof(OtherC)); + assertAreEqual(1, sizeof(c)); + assertAreEqual(1, sizeof(OtherC)); } "); } diff --git a/CLanguageTests/InterpreterTests.cs b/CLanguageTests/InterpreterTests.cs index 1465aef..874a772 100644 --- a/CLanguageTests/InterpreterTests.cs +++ b/CLanguageTests/InterpreterTests.cs @@ -330,7 +330,7 @@ public void SizeofInt () var i = Run (@" void main () { int s = sizeof(int); - assertAreEqual (2, s); + assertAreEqual (1, s); }"); } @@ -340,7 +340,7 @@ public void SizeofSizeofInt () var i = Run (@" void main () { int s = sizeof(sizeof(int)); - assertAreEqual (4, s); + assertAreEqual (1, s); }"); } @@ -350,7 +350,7 @@ public void SizeofIntPtr () var i = Run (@" void main () { int s = sizeof(int*); - assertAreEqual (2, s); + assertAreEqual (1, s); }"); } @@ -360,7 +360,7 @@ public void SizeofLongInt () var i = Run (@" void main () { int s = sizeof(long int); - assertAreEqual (4, s); + assertAreEqual (1, s); }"); } @@ -370,8 +370,10 @@ public void SizeofArray () var i = Run (@" int array[] = { 0, 1, 2, 3, 4 }; void main () { - int num = sizeof(array) / sizeof(array[0]); - int num2 = sizeof(array) / sizeof(int); + int sa = sizeof(array); + assertAreEqual (5, sa); + int num = sa / sizeof(array[0]); + int num2 = sa / sizeof(int); assertAreEqual (5, num); assertAreEqual (5, num2); }");