Skip to content

Commit 940e9d4

Browse files
Improve ARMv8 function calling (#560)
Co-authored-by: Sam Byass <SamboyCoding@users.noreply.github.com>
1 parent a911395 commit 940e9d4

3 files changed

Lines changed: 18 additions & 5 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,5 @@ obj/
1414
launchsettings.json
1515

1616
artifacts/
17+
18+
.DS_Store

Cpp2IL.Core/IlGenerator.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -304,9 +304,8 @@ private static List<CilInstruction> GenerateInstructions(Instruction instruction
304304
}
305305

306306
// Load normal params
307-
var callParams = instruction.Operands
308-
.Skip(thisParamIndex + (targetMethod.IsStatic ? 0 : -1))
309-
.Take(instruction.Operands.Count - 1 - thisParamIndex);
307+
var callParamIndex = instruction.OpCode == OpCode.Call ? (targetMethod.IsStatic ? 2 : 3) : (targetMethod.IsStatic ? 1 : 2);
308+
var callParams = instruction.Operands.Skip(callParamIndex);
310309
foreach (var param in callParams)
311310
LoadOperand(param, method, locals, writeLine, stringCtor);
312311

@@ -501,7 +500,7 @@ private static void LoadOperand(object operand, MethodDefinition method,
501500
break;
502501
}
503502
instructions.Add(CilOpCodes.Ldstr, "Unmanaged memory load: " + operand.ToString());
504-
instructions.Add(CilOpCodes.Newobj, importer.ImportMethod(stringCtor));
503+
instructions.Add(CilOpCodes.Call, importer.ImportMethod(writeLine));
505504
break;
506505
case RuntimeMethodInfoAnalysisContext:
507506
//Not fully implemented, these basically shouldn't actually ever exist in the final IL.

Cpp2IL.Core/InstructionSets/NewArmV8InstructionSet.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,19 @@ void AddCall(MethodAnalysisContext context, object? returnRegister2, ulong addre
250250
Add(address, OpCode.Move, dest2, mem2);
251251
break;
252252
case Arm64Mnemonic.BL:
253-
AddCall(context, GetReturnRegisterForContext(context), address, instruction.BranchTarget);
253+
if (context.AppContext.MethodsByAddress.TryGetValue(instruction.BranchTarget, out var possibleMethods))
254+
{
255+
if (possibleMethods.Count == 1)
256+
AddCall(context, GetReturnRegisterForContext(possibleMethods[0]), address, instruction.BranchTarget);
257+
else
258+
// TODO: Properly fix this case where branch address is potentially more than 1 method
259+
AddCall(context, GetReturnRegisterForContext(context), address, instruction.BranchTarget);
260+
}
261+
else
262+
{
263+
// TODO: properly handle unmanaged/API function
264+
AddCall(context, GetReturnRegisterForContext(context), address, instruction.BranchTarget);
265+
}
254266
break;
255267
case Arm64Mnemonic.RET:
256268
var returnRegister = GetReturnRegisterForContext(context);

0 commit comments

Comments
 (0)