-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Unify FunctionCall and IndirectFunctionCall argument emitting
- Loading branch information
Showing
5 changed files
with
98 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
Cesium.CodeGen/Ir/Expressions/FunctionCallExpressionBase.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
using Cesium.CodeGen.Contexts; | ||
using Cesium.CodeGen.Extensions; | ||
using Cesium.CodeGen.Ir.Types; | ||
using Mono.Cecil.Cil; | ||
using Mono.Cecil.Rocks; | ||
|
||
namespace Cesium.CodeGen.Ir.Expressions; | ||
|
||
internal abstract class FunctionCallExpressionBase : IExpression | ||
{ | ||
public abstract IExpression Lower(IDeclarationScope scope); | ||
public abstract void EmitTo(IEmitScope scope); | ||
public abstract IType GetExpressionType(IDeclarationScope scope); | ||
|
||
protected void EmitArgumentList(IEmitScope scope, ParametersInfo? paramInfo, IReadOnlyList<IExpression> arguments) | ||
{ | ||
var explicitParametersCount = paramInfo?.Parameters.Count ?? 0; | ||
var varArgParametersCount = arguments.Count - explicitParametersCount; | ||
|
||
VariableDefinition? varArgBuffer = null; | ||
|
||
if (paramInfo?.IsVarArg == true) | ||
{ | ||
// TODO: See https://github.com/ForNeVeR/Cesium/issues/285 | ||
// Using sparse population of the parameters on the stack. 8 bytes should be enough for anybody. | ||
// Also we need perform localloc on empty stack, so we will use local variable to save vararg buffer to temporary variable. | ||
if (varArgParametersCount == 0) | ||
{ | ||
scope.AddInstruction(OpCodes.Ldnull); | ||
} | ||
else | ||
{ | ||
scope.AddInstruction(OpCodes.Ldc_I4, varArgParametersCount * 8); | ||
scope.AddInstruction(OpCodes.Localloc); | ||
} | ||
|
||
varArgBuffer = new VariableDefinition(scope.Context.TypeSystem.Void.MakePointerType()); | ||
scope.Method.Body.Variables.Add(varArgBuffer); | ||
scope.AddInstruction(OpCodes.Stloc, varArgBuffer); | ||
} | ||
|
||
foreach (var argument in arguments.Take(explicitParametersCount)) | ||
argument.EmitTo(scope); | ||
|
||
if (paramInfo?.IsVarArg == true) | ||
{ | ||
for (var i = 0; i < varArgParametersCount; i++) | ||
{ | ||
var argument = arguments[i + explicitParametersCount]; | ||
scope.AddInstruction(OpCodes.Ldloc, varArgBuffer!); | ||
scope.AddInstruction(OpCodes.Ldc_I4, i * 8); | ||
scope.AddInstruction(OpCodes.Add); | ||
argument.EmitTo(scope); | ||
scope.AddInstruction(OpCodes.Stind_I); | ||
} | ||
|
||
scope.AddInstruction(OpCodes.Ldloc, varArgBuffer!); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters