Skip to content

Commit

Permalink
Interpreter sizeof report num values so low level functions can work
Browse files Browse the repository at this point in the history
  • Loading branch information
praeclarum committed Feb 3, 2024
1 parent 4cfed68 commit 463432c
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 12 deletions.
4 changes: 2 additions & 2 deletions CLanguage/Compiler/EmitContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion CLanguage/Syntax/SizeOfExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
2 changes: 1 addition & 1 deletion CLanguage/Syntax/SizeOfTypeExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
4 changes: 2 additions & 2 deletions CLanguageTests/ClassTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
");
}
Expand Down
14 changes: 8 additions & 6 deletions CLanguageTests/InterpreterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ public void SizeofInt ()
var i = Run (@"
void main () {
int s = sizeof(int);
assertAreEqual (2, s);
assertAreEqual (1, s);
}");
}

Expand All @@ -340,7 +340,7 @@ public void SizeofSizeofInt ()
var i = Run (@"
void main () {
int s = sizeof(sizeof(int));
assertAreEqual (4, s);
assertAreEqual (1, s);
}");
}

Expand All @@ -350,7 +350,7 @@ public void SizeofIntPtr ()
var i = Run (@"
void main () {
int s = sizeof(int*);
assertAreEqual (2, s);
assertAreEqual (1, s);
}");
}

Expand All @@ -360,7 +360,7 @@ public void SizeofLongInt ()
var i = Run (@"
void main () {
int s = sizeof(long int);
assertAreEqual (4, s);
assertAreEqual (1, s);
}");
}

Expand All @@ -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);
}");
Expand Down

0 comments on commit 463432c

Please sign in to comment.