Skip to content

Commit

Permalink
(#354) FunctionCallExpression: extract the type conversion into a sep…
Browse files Browse the repository at this point in the history
…arate method
  • Loading branch information
ForNeVeR committed Nov 2, 2023
1 parent 84e53f9 commit 8e894e0
Showing 1 changed file with 26 additions and 44 deletions.
70 changes: 26 additions & 44 deletions Cesium.CodeGen/Ir/Expressions/FunctionCallExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,30 +71,7 @@ public override IExpression Lower(IDeclarationScope scope)
return new IndirectFunctionCallExpression(
new GetValueExpression(new LValueLocalVariable(var.Type, var.Identifier)),
f,
_arguments.Select((a, index) =>
{
int firstVarArgArgument = 0;
if (f.Parameters?.IsVarArg == true)
{
firstVarArgArgument = f.Parameters.Parameters.Count;
}

if (index >= firstVarArgArgument)
{
var expressionType = a.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));
}
else
{
return a.Lower(scope);
}
}

return a.Lower(scope);
}).ToList());
ConvertArgs(scope, f.Parameters));
}

var callee = scope.GetFunctionInfo(functionName);
Expand All @@ -103,34 +80,39 @@ public override IExpression Lower(IDeclarationScope scope)
throw new CompilationException($"Function \"{functionName}\" was not found.");
}

return new FunctionCallExpression(
_function,
callee,
ConvertArgs(scope, callee.Parameters));
}

private List<IExpression> ConvertArgs(IDeclarationScope scope, ParametersInfo? parameters)
{
int firstVarArgArgument = 0;
if (callee.Parameters?.IsVarArg == true)
if (parameters?.IsVarArg == true)
{
firstVarArgArgument = callee.Parameters.Parameters.Count;
firstVarArgArgument = parameters.Parameters.Count;
}

return new FunctionCallExpression(
_function,
callee,
_arguments.Select((a, index) =>
return _arguments.Select((a, index) =>
{
if (index >= firstVarArgArgument)
{
if (index >= firstVarArgArgument)
var loweredArg = a.Lower(scope);
var expressionType = loweredArg.GetExpressionType(scope);
if (expressionType.Equals(scope.CTypeSystem.Float))
{
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
{
return loweredArg;
}
// 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
{
return loweredArg;
}
}

return a.Lower(scope);
}).ToList());
return a.Lower(scope);
}).ToList();
}

public override void EmitTo(IEmitScope scope)
Expand Down

0 comments on commit 8e894e0

Please sign in to comment.