Skip to content

Commit

Permalink
Merge pull request #45 from praeclarum/memcmp
Browse files Browse the repository at this point in the history
Support low level memory functions using void*
  • Loading branch information
praeclarum authored Feb 3, 2024
2 parents 9f9d98c + c828da5 commit 1c72c81
Show file tree
Hide file tree
Showing 21 changed files with 197 additions and 116 deletions.
9 changes: 6 additions & 3 deletions .github/workflows/build-nugets.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@ jobs:
runs-on: macos-latest

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: Update Versions
env:
VERSION_PREFIX: 0.19
VERSION_PREFIX: 0.20
VERSION_SUFFIX: ${{github.run_number}}
run: |
VERSION=$VERSION_PREFIX.$VERSION_SUFFIX
sed -i bak "s:<Version>1.0.0</Version>:<Version>$VERSION</Version>:g" CLanguage/CLanguage.csproj
sed -i bak "s:<version>1.0.0</version>:<version>$VERSION</version>:g" CLanguage.Editor.nuspec
sed -i bak2 "s:version=\"1.0.0\":version=\"$VERSION\":g" CLanguage.Editor.nuspec
- name: Setup .NET Core
uses: actions/setup-dotnet@v1
uses: actions/setup-dotnet@v4
with:
dotnet-version: 6.0.300
- name: Install dependencies
Expand All @@ -39,13 +39,16 @@ jobs:
- name: Build iOS Editor
run: msbuild /p:Configuration=Release Editor/iOS/CLanguage.Editor.iOS.csproj
- name: Package Library
if: github.event_name == 'push'
run: |
mkdir PackageOut
cd CLanguage && dotnet pack --include-symbols --no-build -c Release -v normal -o ../PackageOut
- name: Package Editor
if: github.event_name == 'push'
run: |
nuget pack CLanguage.Editor.nuspec -OutputDirectory PackageOut
- name: Package
if: github.event_name == 'push'
uses: actions/upload-artifact@master
with:
path: PackageOut
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Test files

CLanguageTests/a.out
CLanguageTests/test.c
CLanguageTests/test*.c


/*.nupkg
Expand Down
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"cmake.configureOnOpen": false
"cmake.configureOnOpen": false,
"dotnet.preferCSharpExtension": true
}
8 changes: 7 additions & 1 deletion CLanguage/Compiler/EmitContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,18 @@ public void EmitCast (CType fromType, CType toType)
Emit (op);
// This conversion is implicit with how the evaluator stores its stuff
}
else if (fromBasicType != null && fromBasicType.IsIntegral && toType is CPointerType && fromBasicType.NumValues == toType.NumValues) {
else if (fromBasicType != null && fromBasicType.IsIntegral && toType is CPointerType) {
// Support `const char *p = 0;`
}
else if (fromType is CArrayType fat && toType is CPointerType tpt && fat.ElementType.NumValues == tpt.InnerType.NumValues) {
// Demote arrays to pointers
}
else if (fromType is CArrayType && toType.IsVoidPointer) {
// 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. sizeof() reports type.NumValues.
}
else if (fromType is CEnumType et && toType is CIntType bt) {
// Enums act like ints
}
Expand Down
155 changes: 80 additions & 75 deletions CLanguage/Interpreter/CInterpreter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CLanguage.Types;

using CLanguage.Types;

namespace CLanguage.Interpreter
{
public class CInterpreter
Expand Down Expand Up @@ -42,6 +42,11 @@ public Value ReadMemory (int address)
return Stack[address];
}

public Value WriteMemory (int address, Value value)
{
return Stack[address] = value;
}

public string ReadStringWithEncoding (int address, Encoding encoding)
{
var b = (byte)Stack[address];
Expand Down Expand Up @@ -238,92 +243,92 @@ public void Step (int microseconds)
}
}

public Value RunFunction (Value functionAddress, int microseconds)
{
Call (functionAddress);
return StepFunction (microseconds);
public Value RunFunction (Value functionAddress, int microseconds)
{
Call (functionAddress);
return StepFunction (microseconds);
}

public Value RunFunction (Value functionAddress, Value arg0, int microseconds)
{
Push (arg0);
Call (functionAddress);
return StepFunction (microseconds);
public Value RunFunction (Value functionAddress, Value arg0, int microseconds)
{
Push (arg0);
Call (functionAddress);
return StepFunction (microseconds);
}

public Value RunFunction (Value functionAddress, Value arg0, Value arg1, int microseconds)
{
Push (arg0);
Push (arg1);
Call (functionAddress);
return StepFunction (microseconds);
public Value RunFunction (Value functionAddress, Value arg0, Value arg1, int microseconds)
{
Push (arg0);
Push (arg1);
Call (functionAddress);
return StepFunction (microseconds);
}

public Value RunFunction (Value functionAddress, Value arg0, Value arg1, Value arg2, int microseconds)
{
Push (arg0);
Push (arg1);
Push (arg2);
Call (functionAddress);
return StepFunction (microseconds);
public Value RunFunction (Value functionAddress, Value arg0, Value arg1, Value arg2, int microseconds)
{
Push (arg0);
Push (arg1);
Push (arg2);
Call (functionAddress);
return StepFunction (microseconds);
}

Value StepFunction (int microseconds)
{
if (ActiveFrame == null)
return 0;

var startFI = FI;
var startReturnType = ActiveFrame?.Function.FunctionType.ReturnType;

if (microseconds <= SleepTime) {
SleepTime -= microseconds;
}
else {
RemainingTime = microseconds - SleepTime;
SleepTime = 0;

Value StepFunction (int microseconds)
{
if (ActiveFrame == null)
return 0;

var startFI = FI;
var startReturnType = ActiveFrame?.Function.FunctionType.ReturnType;

if (microseconds <= SleepTime) {
SleepTime -= microseconds;
}
else {
RemainingTime = microseconds - SleepTime;
SleepTime = 0;

try {
var a = ActiveFrame;
var a = ActiveFrame;
while (a != null && FI >= startFI && RemainingTime > 0) {
RemainingTime -= CpuSpeed;
RemainingTime -= CpuSpeed;
a.Function.Step (this, a);
a = ActiveFrame;
if (YieldedValue != 0)
break;
}
}
catch (Exception) {
Reset ();
throw;
}
}

// Pop frames until we are back to the caller
while (FI >= startFI) {
// Failed to return. Force it.
if (ActiveFrame?.Function.FunctionType.ReturnType is CType rt) {
// Return 0.
if (!rt.IsVoid) {
int n = rt.NumValues;
for (int i = 0; i < n; i++) {
Stack[SP++] = 0;
}
}
Return ();
}
else {
break;
}
}

// Get the return value
Value returnValue = 0;
int numReturnValues = startReturnType != null ? startReturnType.NumValues : 0;
for (var i = 0; i < numReturnValues; i++) {
returnValue = Stack[--SP];
}
return returnValue;
break;
}
}
catch (Exception) {
Reset ();
throw;
}
}

// Pop frames until we are back to the caller
while (FI >= startFI) {
// Failed to return. Force it.
if (ActiveFrame?.Function.FunctionType.ReturnType is CType rt) {
// Return 0.
if (!rt.IsVoid) {
int n = rt.NumValues;
for (int i = 0; i < n; i++) {
Stack[SP++] = 0;
}
}
Return ();
}
else {
break;
}
}

// Get the return value
Value returnValue = 0;
int numReturnValues = startReturnType != null ? startReturnType.NumValues : 0;
for (var i = 0; i < numReturnValues; i++) {
returnValue = Stack[--SP];
}
return returnValue;
}
}
}
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
32 changes: 21 additions & 11 deletions CLanguage/Types/CType.cs
Original file line number Diff line number Diff line change
@@ -1,31 +1,41 @@
using System;
using System;
using System.Linq;
using System.Text;
using CLanguage.Interpreter;
using CLanguage.Interpreter;
using CLanguage.Syntax;
using CLanguage.Compiler;
using CLanguage.Compiler;

namespace CLanguage.Types
{
public abstract class CType
{
public TypeQualifiers TypeQualifiers { get; set; }

public abstract int GetByteSize (EmitContext c);
public abstract int GetByteSize (EmitContext c);
public abstract int NumValues { get; }

public static readonly CVoidType Void = new CVoidType ();

public virtual bool IsIntegral => false;

public virtual bool IsVoid => false;

readonly Lazy<CPointerType> pointer;

public CPointerType Pointer => pointer.Value;

public virtual bool IsVoid => false;

readonly Lazy<CPointerType> pointer;

public CPointerType Pointer => pointer.Value;

public bool IsVoidPointer => this switch {
CPointerType pt => pt.InnerType.IsVoid || pt.InnerType.IsVoidPointer,
_ => false,
};

public bool IsPointer => this switch {
CPointerType pt => true,
_ => false,
};

public CType ()
{
{
pointer = new Lazy<CPointerType> (CreatePointerType);
}

Expand Down
2 changes: 1 addition & 1 deletion CLanguageTests/ArduinoInterpreterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ ArduinoTestMachineInfo.TestArduino Run (string code)
var fullCode = code + "\n\nvoid main() { __cinit(); setup(); while(1){loop();}}";
var i = CLanguageService.CreateInterpreter (fullCode, machine, new TestPrinter ());
i.Reset ("main");
i.Step ();
i.Run ();
return machine.Arduino;
}

Expand Down
2 changes: 1 addition & 1 deletion CLanguageTests/ArrayTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ CInterpreter Run (string code)
var fullCode = "void start() { __cinit(); main(); } " + code;
var i = CLanguageService.CreateInterpreter (fullCode, new ArduinoTestMachineInfo (), printer: new TestPrinter ());
i.Reset ("start");
i.Step ();
i.Run ();
return i;
}

Expand Down
2 changes: 1 addition & 1 deletion CLanguageTests/CastTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ CInterpreter Run (string code)
var fullCode = "void start() { __cinit(); main(); } " + code;
var i = CLanguageService.CreateInterpreter (fullCode, new ArduinoTestMachineInfo (), printer: new TestPrinter ());
i.Reset ("start");
i.Step ();
i.Run ();
return i;
}

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
2 changes: 1 addition & 1 deletion CLanguageTests/FloatTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ CInterpreter Run (string code)
var fullCode = "void start() { __cinit(); main(); } " + code;
var i = CLanguageService.CreateInterpreter (fullCode, new ArduinoTestMachineInfo (), printer: new TestPrinter ());
i.Reset ("start");
i.Step ();
i.Run ();
return i;
}

Expand Down
2 changes: 1 addition & 1 deletion CLanguageTests/IntegerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ CInterpreter Run (string code)
var fullCode = "void start() { __cinit(); main(); } " + code;
var i = CLanguageService.CreateInterpreter (fullCode, new ArduinoTestMachineInfo (), printer: new TestPrinter ());
i.Reset ("start");
i.Step ();
i.Run ();
return i;
}

Expand Down
Loading

0 comments on commit 1c72c81

Please sign in to comment.