Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add atoi/fprintf #447

Merged
merged 1 commit into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions Cesium.CodeGen/Ir/Expressions/FunctionCallExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,16 @@ public override IExpression Lower(IDeclarationScope scope)
{
if (index >= firstVarArgArgument)
{
var expressionType = a.GetExpressionType(scope);
var loweredArg = a.Lower(scope);
var expressionType = loweredArg.GetExpressionType(scope);
if (expressionType.Equals(scope.CTypeSystem.Float))
{
// Seems to be float always use float-point registers and as such we need to covert to double.
return new TypeCastExpression(scope.CTypeSystem.Double, a.Lower(scope));
return new TypeCastExpression(scope.CTypeSystem.Double, loweredArg);
}
else
{
return a.Lower(scope);
return loweredArg;
}
}

Expand Down
15 changes: 9 additions & 6 deletions Cesium.Compiler/stdlib/stdio.h
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
#pragma once
typedef void FILE;

#define stdin 0
#define stdout 1
#define stderr 2

__cli_import("Cesium.Runtime.StdIoFunctions::PutS")
int puts(char *s);

__cli_import("Cesium.Runtime.StdIoFunctions::PrintF")
int printf(char* s, ...);

__cli_import("Cesium.Runtime.StdIoFunctions::FPrintF")
int fprintf(FILE* stream, char* s, ...);

__cli_import("Cesium.Runtime.StdIoFunctions::PutChar")
int putchar(char _Character);

typedef void FILE;

#define stdin 0
#define stdout 1
#define stderr 2

__cli_import("Cesium.Runtime.StdIoFunctions::PutC")
int putc(char _Character, FILE* stream);
3 changes: 3 additions & 0 deletions Cesium.Compiler/stdlib/stdlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,6 @@ void abort(void);

#define EXIT_SUCCESS 0
#define EXIT_FAILURE 1

__cli_import("Cesium.Runtime.StdLibFunctions::Atoi")
int atoi(const char* str);
45 changes: 37 additions & 8 deletions Cesium.Runtime/StdIoFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ record class StreamHandle

private static List<StreamHandle> handles = new();

private const int StdIn = 0;

private const int StdOut = 1;

private const int StdErr = 2;

static StdIoFunctions()
{
handles.Add(new StreamHandle()
Expand Down Expand Up @@ -87,21 +93,40 @@ public static int PutC(byte character, void* stream)
}

public static int PrintF(byte* str, void* varargs)
{
return FPrintF((void*)(IntPtr)StdOut, str, varargs);
}

public static int FPrintF(void* stream, byte* str, void* varargs)
{
var formatString = Unmarshal(str);
if (formatString == null)
{
return -1;
}

var streamHandle = GetStreamHandle(stream);
if (streamHandle == null)
{
return -1;
}

var streamWriterAccessor = streamHandle.Writer;
if (streamWriterAccessor == null)
{
return -1;
}

var streamWriter = streamWriterAccessor();

int currentPosition = 0;
var formatStartPosition = formatString.IndexOf('%', currentPosition);
int consumedArgs = 0;
int consumedBytes = 0;
while (formatStartPosition >= 0)
{
var lengthTillPercent = formatStartPosition - currentPosition;
Console.Write(formatString.Substring(currentPosition, lengthTillPercent));
streamWriter.Write(formatString.Substring(currentPosition, lengthTillPercent));
consumedBytes += lengthTillPercent;
int addition = 1;
string formatSpecifier = formatString[formatStartPosition + addition].ToString();
Expand All @@ -115,12 +140,12 @@ public static int PrintF(byte* str, void* varargs)
{
case "s":
string? stringValue = Unmarshal((byte*)((long*)varargs)[consumedArgs]);
Console.Write(stringValue);
streamWriter.Write(stringValue);
consumedBytes += stringValue?.Length ?? 0;
consumedArgs++;
break;
case "c":
Console.Write((char)(byte)((long*)varargs)[consumedArgs]);
streamWriter.Write((char)(byte)((long*)varargs)[consumedArgs]);
consumedBytes++;
consumedArgs++;
break;
Expand All @@ -129,32 +154,36 @@ public static int PrintF(byte* str, void* varargs)
case "i":
int intValue = (int)((long*)varargs)[consumedArgs];
var intValueString = intValue.ToString();
Console.Write(intValueString);
streamWriter.Write(intValueString);
consumedBytes += intValueString.Length;
consumedArgs++;
break;
case "u":
case "lu":
uint uintValue = (uint)((long*)varargs)[consumedArgs];
var uintValueString = uintValue.ToString();
Console.Write(uintValueString);
streamWriter.Write(uintValueString);
consumedBytes += uintValueString.Length;
consumedArgs++;
break;
case "f":
var floatNumber = ((double*)varargs)[consumedArgs];
string floatNumberString = floatNumber.ToString("F6");
Console.Write(floatNumberString);
streamWriter.Write(floatNumberString);
consumedBytes += floatNumberString.Length;
consumedArgs++;
break;
case "p":
nint pointerValue = ((nint*)varargs)[consumedArgs];
string pointerValueString = pointerValue.ToString("X");
Console.Write(pointerValueString);
streamWriter.Write(pointerValueString);
consumedBytes += pointerValueString.Length;
consumedArgs++;
break;
case "%":
streamWriter.Write('%');
consumedBytes += 1;
break;
default:
throw new FormatException($"Format specifier {formatSpecifier} is not supported");
}
Expand All @@ -164,7 +193,7 @@ public static int PrintF(byte* str, void* varargs)
}

string remainderString = formatString.Substring(currentPosition);
Console.Write(remainderString);
streamWriter.Write(remainderString);
return consumedBytes + remainderString.Length;
}

Expand Down
6 changes: 6 additions & 0 deletions Cesium.Runtime/StdLibFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,10 @@ public static void Free(void* ptr)
return NativeMemory.AlignedAlloc(size, alignment);
#endif
}

public static int Atoi(byte* ptr)
{
var str = StdIoFunctions.Unmarshal(ptr);
return Convert.ToInt32(str);
}
}
Loading