Skip to content

Commit cf8df59

Browse files
kevinferraremaximilien-noal
authored andcommitted
fix: correct loadOffset handling in generated overrides
1 parent 72da247 commit cf8df59

13 files changed

Lines changed: 991 additions & 184 deletions

‎src/Spice86.Core/Emulator/ReverseEngineer/CfgCodeGeneration/Ast/CyclicJumpNode.cs‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ namespace Spice86.Core.Emulator.ReverseEngineer.CfgCodeGeneration.Ast;
55
/// <summary>A cross-partition jump that can cycle: it bounces through the entry dispatcher when the target method says so, otherwise returns the dispatcher.</summary>
66
/// <param name="MethodName">The generated method the jump can re-enter.</param>
77
/// <param name="LoadOffset">The load offset the jump enters <paramref name="MethodName"/> with.</param>
8-
/// <param name="SingleEntry"><c>true</c> when the method has one entry: the dispatcher target is its cached
9-
/// <c>Entry(M)</c> wrapper and the trampoline never reloads <c>loadOffset</c>.</param>
8+
/// <param name="SingleEntry"><c>true</c> when the target method has one entry: the dispatcher target is
9+
/// its cached <c>Entry(M)</c> wrapper. Whether the trampoline reloads <c>loadOffset</c> depends on the
10+
/// source method's own entry dispatch, not on this flag.</param>
1011
internal sealed record CyclicJumpNode(string MethodName, int LoadOffset, bool SingleEntry) : IVisitableAstNode {
1112
/// <inheritdoc />
1213
public T Accept<T>(IAstVisitor<T> astVisitor) => CodeGenerationNode.RequireVisitor(astVisitor, this).VisitCyclicJumpNode(this);

‎src/Spice86.Core/Emulator/ReverseEngineer/CfgCodeGeneration/CSharpExpressionPrinter.cs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,9 @@ private CSharpExpressionPrinter(AnalyzedMethod? method, ProgramAnalysis? analysi
134134
/// its header or its step.</summary>
135135
internal GotoTargets MethodGotoTargets => _method?.Prepared.Targets ?? GotoTargets.None;
136136

137+
/// <summary>Whether the current method dispatches between multiple entry points.</summary>
138+
internal bool NeedsEntryDispatch => _method?.Prepared.Method.NeedsEntryDispatch ?? false;
139+
137140
/// <summary>Prints <paramref name="node"/>, which must be an expression.</summary>
138141
public CSharpFragment Print(IVisitableAstNode node) => node.Accept(this);
139142

‎src/Spice86.Core/Emulator/ReverseEngineer/CfgCodeGeneration/CSharpStatementPrinter.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,7 @@ private void PrintCyclicJump(CyclicJumpNode node) {
627627
? GeneratedOverrideNames.EntryFieldNameOf(delegateName)
628628
: delegateName;
629629
writer.OpenBlock($"if (JumpDispatcher.Jump({target}, 0x{node.LoadOffset:X4}))");
630-
if (!node.SingleEntry) {
630+
if (expressions.NeedsEntryDispatch) {
631631
writer.Line("loadOffset = JumpDispatcher.NextEntryAddress;");
632632
}
633633
writer.Line("goto entrydispatcher;");

‎src/Spice86.Core/Emulator/ReverseEngineer/CfgCodeGeneration/InstructionLowerer.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ [new SegmentVariableNode(expectedReturn.Segment),
186186
new OffsetLiteralNode(handler.Entry.Address.Offset),
187187
new MethodGroupNode(Context.GetMethodName(handler.Partition),
188188
Context.GetEntryLoadOffset(handler.Partition, handler.Entry.Node),
189-
handler.Partition.Entries.Count == 1)],
189+
Context.GetEntries(handler.Partition).Count == 1)],
190190
handler.Effect, HardwareInterruptEffect),
191191
Transfer.EmitPostCallContinuation(node.Instruction, continuation));
192192
}

‎src/Spice86.Core/Emulator/ReverseEngineer/CfgCodeGeneration/TransferEmitter.cs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public IVisitableAstNode Emit(ResolvedCfgEdge edge, bool allowCallOutContinuatio
5151

5252
string methodName = context.GetMethodName(targetPartition);
5353
int loadOffset = context.GetEntryLoadOffset(targetPartition, edge.Target);
54-
bool singleEntry = targetPartition.Entries.Count == 1;
54+
bool singleEntry = context.GetEntries(targetPartition).Count == 1;
5555
switch (transfer.Kind) {
5656
case CfgCodePartitionTransferKind.CrossPartitionFlow:
5757
return PartitionReturn(methodName, loadOffset, singleEntry);
@@ -195,7 +195,7 @@ public MethodGroupNode FunctionReference(ResolvedCfgEdge targetEdge) {
195195
CfgCodePartition targetPartition = context.GetPartition(targetEdge.Target);
196196
string methodName = context.GetMethodName(targetPartition);
197197
int loadOffset = context.GetEntryLoadOffset(targetPartition, targetEdge.Target);
198-
return new MethodGroupNode(methodName, loadOffset, targetPartition.Entries.Count == 1);
198+
return new MethodGroupNode(methodName, loadOffset, context.GetEntries(targetPartition).Count == 1);
199199
}
200200

201201
/// <summary>
@@ -222,7 +222,7 @@ public CpuFaultHandler FaultHandler(ResolvedCfgEdge edge) {
222222

223223
string methodName = context.GetMethodName(targetPartition);
224224
int loadOffset = context.GetEntryLoadOffset(targetPartition, edge.Target);
225-
return new CpuFaultHandler(edge.Target.Address, methodName, loadOffset, targetPartition.Entries.Count == 1);
225+
return new CpuFaultHandler(edge.Target.Address, methodName, loadOffset, context.GetEntries(targetPartition).Count == 1);
226226
}
227227

228228
private static IVisitableAstNode PartitionReturn(string methodName, int loadOffset, bool singleEntry) {

‎tests/Spice86.Tests/AsmFixtures/AsmFixtureCatalog.cs‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,10 @@ public static AsmFixture Partition(string binName) {
532532
result.State.SS.Should().Be(0x1000, $"{binName}: start sets SS to 0x1000");
533533
result.State.SP.Should().Be(0x0100, $"{binName}: start sets SP to 0x0100");
534534
result.State.AX.Should().Be(0x1000, $"{binName}: start sets AX to 0x1000 and no call changes it");
535-
result.State.CX.Should().Be(0x0001, $"{binName}: the last root entry leaves CX = 1");
535+
result.State.BX.Should().Be(0x0001, $"{binName}: the cycle resumes at the secondary shared entry");
536+
result.State.CX.Should().Be(0x0002, $"{binName}: the cycle uses neither original return path");
537+
result.State.DX.Should().Be(0x0000, $"{binName}: the cycle finishes all three iterations");
538+
result.State.SI.Should().Be(0x0004, $"{binName}: the shared handler runs through an interrupt, a return, a call and a tail jump");
536539
});
537540
case "partition_multi_entry_disconnected_shared":
538541
return ResultFixture(new AsmRunSettings { BinName = binName, MaxCycles = 10000 }, result => {

‎tests/Spice86.Tests/Resources/GeneratedCodeReadability/partition_multi_entry_irreducible_shared.spec.json‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"sourceSha256": "32b8f9e622aac6f507a1fdd52289ac572576412372f634dab4ed3c6e21a6e069",
2+
"sourceSha256": "f696128e170bb475d68f64090d0c565a4a50a8259f4227c232adc4d0af8bce1d",
33
"minimum": {
44
"StringServiceCalls": 0,
55
"deviceCalls.Cmos": 0,
@@ -27,18 +27,18 @@
2727
},
2828
"maximum": {
2929
"BookkeepingCalls": 0,
30-
"Casts": 0,
30+
"Casts": 1,
3131
"InterruptCallReturnedElsewhere": 0,
32-
"InterruptCalls": 0,
32+
"InterruptCalls": 1,
3333
"LocalReads": 0,
3434
"LocalVariables": 0,
3535
"LocalWrites": 0,
36-
"PlainRegisterAssignments": 5,
36+
"PlainRegisterAssignments": 14,
3737
"PortWrapperCalls": 0,
3838
"RawIoCalls": 0,
39-
"RegisterReads": 2,
40-
"RegisterWrites": 5,
41-
"Statements": 36,
39+
"RegisterReads": 18,
40+
"RegisterWrites": 16,
41+
"Statements": 107,
4242
"WriteBacks": 0
4343
}
4444
}
Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,38 @@
11
use16
22

33
start:
4+
xor ax, ax
5+
mov ds, ax
46
mov ax, 1000h
57
mov ss, ax
68
mov sp, 0100h
9+
mov dx, 0
710
mov cx, 0
811
call root_a
912
mov cx, 1
1013
call root_b
14+
15+
; Record cycle_bridge as a function without entering the cycle.
16+
mov cx, 2
17+
call cycle_bridge
18+
mov bx, 0
19+
mov dx, 3
20+
call cycle_driver
21+
22+
; All four transfers reach one IRET handler. The push/ret adds return-target evidence.
23+
mov word [0200h], shared_target
24+
mov word [0202h], cs
25+
mov si, 0
26+
int 80h
27+
pushf
28+
push cs
29+
call return_caller
30+
pushf
31+
push cs
32+
call shared_target
33+
pushf
34+
push cs
35+
call jump_caller
1136
hlt
1237

1338
root_a:
@@ -17,16 +42,45 @@ root_b:
1742
jmp shared_entry_b
1843

1944
shared_entry_a:
45+
inc bx
2046
cmp cx, 0
2147
je shared_entry_b
22-
ret
48+
cmp dx, 0
49+
je shared_done
50+
dec dx
51+
jmp cycle_bridge
2352

2453
shared_entry_b:
2554
cmp cx, 1
2655
je shared_entry_a
56+
cmp dx, 0
57+
je shared_done
58+
dec dx
59+
jmp cycle_bridge
60+
61+
shared_done:
62+
ret
63+
64+
cycle_bridge:
65+
cmp dx, 3
66+
je shared_entry_a
67+
jmp shared_entry_b
68+
69+
cycle_driver:
70+
jmp cycle_bridge
71+
72+
return_caller:
73+
push shared_target
2774
ret
2875

76+
jump_caller:
77+
jmp shared_target
78+
79+
shared_target:
80+
inc si
81+
iret
82+
2983
rb 65520-$
3084
jmp start
3185
rb 65535-$
32-
db 0FFh
86+
db 0FFh
Binary file not shown.

0 commit comments

Comments
 (0)