Skip to content

Commit

Permalink
(#354) CodeGen: groundwork for CLR type conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
ForNeVeR committed Nov 2, 2023
1 parent 8e894e0 commit 2e93313
Showing 1 changed file with 27 additions and 11 deletions.
38 changes: 27 additions & 11 deletions Cesium.CodeGen/Ir/Expressions/FunctionCallExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ public override IExpression Lower(IDeclarationScope scope)
throw new CompilationException($"Function \"{functionName}\" was not found.");
}

if (callee.MethodReference is not null)
throw new WipException(WipException.ToDo,
"TODO: Inspect the actual method reference argument list instead of the formal argument list");
return new FunctionCallExpression(
_function,
callee,
Expand All @@ -96,25 +99,38 @@ private List<IExpression> ConvertArgs(IDeclarationScope scope, ParametersInfo? p

return _arguments.Select((a, index) =>
{
if (index >= firstVarArgArgument)
IType targetType;
var loweredArg = a.Lower(scope);
if (index < firstVarArgArgument)
{
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, loweredArg);
}
else
// Argument is not in vararg argument list. Just use the declared type.
targetType = parameters!.Parameters[index].Type;
}
else
{
// Argument is in a vararg list. Use the actual argument type, except for cases when it is float
// (convert to double then).
targetType = loweredArg.GetExpressionType(scope);
if (targetType.Equals(scope.CTypeSystem.Float))
{
return loweredArg;
targetType = scope.CTypeSystem.Double;
}
}

return a.Lower(scope);
return CastTypeIfRequired(scope, loweredArg, targetType);
}).ToList();
}

private static IExpression CastTypeIfRequired(IDeclarationScope scope, IExpression expression, IType targetType)
{
if (expression.GetExpressionType(scope).IsEqualTo(targetType))
{
return expression;
}

return new TypeCastExpression(targetType, expression);
}

public override void EmitTo(IEmitScope scope)
{
if (_callee == null)
Expand Down

0 comments on commit 2e93313

Please sign in to comment.