Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/Spice86.Core/Emulator/Http/EmbeddedHostLifetime.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace Spice86.Core.Emulator.Http;

using Microsoft.Extensions.Hosting;

/// <summary>
/// A no-op host lifetime so embedded web hosts do not hook console signals and stop with the emulator.
/// </summary>
internal sealed class EmbeddedHostLifetime : IHostLifetime {
public Task WaitForStartAsync(CancellationToken cancellationToken) {
return Task.CompletedTask;
}

public Task StopAsync(CancellationToken cancellationToken) {
return Task.CompletedTask;
}
}
11 changes: 0 additions & 11 deletions src/Spice86.Core/Emulator/Http/Spice86HttpApiServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,4 @@ public void Dispose() {
_disposed = true;
((IHost)_webApplication).Dispose();
}

// custom IHostLifetime so the HTTP server can close when the emulation loop exits.
private sealed class EmbeddedHostLifetime : IHostLifetime {
public Task WaitForStartAsync(CancellationToken cancellationToken) {
return Task.CompletedTask;
}

public Task StopAsync(CancellationToken cancellationToken) {
return Task.CompletedTask;
}
}
}
15 changes: 4 additions & 11 deletions src/Spice86.Core/Emulator/Mcp/McpHttpHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ namespace Spice86.Core.Emulator.Mcp;
using Microsoft.AspNetCore.Hosting.Server.Features;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

using Serilog;
using Serilog.Core;

using Spice86.Core.Emulator.Http;
using Spice86.Mcp;

using System.Linq;
Expand Down Expand Up @@ -44,6 +46,7 @@ public void Start(EmulatorMcpServices services, int port = 8081,
builder.Logging.AddSerilog(_mcpFileLogger);

builder.Services.AddSingleton(services);
builder.Services.AddSingleton<IHostLifetime, EmbeddedHostLifetime>();

// Register additional services (from override projects) before building
if (additionalServices != null) {
Expand Down Expand Up @@ -115,17 +118,7 @@ public void Start(EmulatorMcpServices services, int port = 8081,
}

public void Stop() {
if (_disposed) {
return;
}
_disposed = true;
if (_app != null) {
_app.StopAsync().GetAwaiter().GetResult();
_app.DisposeAsync().AsTask().GetAwaiter().GetResult();
_app = null;
}
_mcpFileLogger?.Dispose();
_mcpFileLogger = null;
Task.Run(() => DisposeAsync().AsTask()).GetAwaiter().GetResult();

@maximilien-noal maximilien-noal Nov 12, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sync over async and thread pool thread used for a lightweight task (the Task is not CPU bound)

The getAwaiter getResult should be removed, task.Run should be removed. And the Task should be awaited. And the method should be of type async Task. And the caller should be async, and so on (async is viral)

To avoid a continuation on the UI thread SynchronizationContext use .ConfigureAwait(false)

await someTask.ConfigureAwait(false)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed to be done in a separate PR, thanks for the review :)

}

public async ValueTask DisposeAsync() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,18 +118,22 @@ public GeneratedCSharpProgram Generate(CfgPartitionedProgram program, DumpProgre
flagReadCensus.Merge(methodCensus.FlagReads);
webCensus.Merge(methodCensus.Webs);
}
progress.EndPhase();

progress.BeginPhase(DumpPhase.CodeProgramAnalysis);
ProgramAnalysis analysis = ProgramAnalysis.Compute(prepared, context, effectBuilder,
program.Inputs.ProgramImages);
progress.Detail($"Calling conventions fixed point finished in {analysis.CallingConventions.Iterations} iterations");
program.Inputs.ProgramImages, progress);

RewritePreparedBodies(prepared, program, analysis);

IReadOnlyList<SpeculativeRunFieldPlan> speculativeRunFields = NameSpeculativeRunFields(prepared);
progress.EndPhase();

progress.BeginPhase(DumpPhase.CodePrinting);
List<(ushort Segment, string Text, bool HasFrame)> methodTexts = new(plan.Methods.Count);
for (int i = 0; i < prepared.Count; i++) {
PreparedMethod preparedMethod = prepared[i];
progress.Step(i + 1, prepared.Count, preparedMethod.Method.MethodName);
GenerationStepTimer timer = timers[i];
CSharpSourceWriter methodWriter = new();
methodWriter.Indent();
Expand Down Expand Up @@ -269,7 +273,7 @@ private static List<DataSymbolLabel> DataSymbolLabels(
/// </summary>
internal static string SlowMethodLine(int current, int total, string name, int instructions, int blocks,
int statements, GenerationStepTimer timer) =>
$"Code generation: method {current} / {total} {name} took {timer.TotalMilliseconds} ms ({instructions} instructions, {blocks} IR blocks, {statements} statements): "
$"Code generation: slow method {current} / {total} {name} took {timer.TotalMilliseconds} ms ({instructions} instructions, {blocks} IR blocks, {statements} statements): "
+ string.Join(", ", timer.Steps.Select(step => $"{step.Step} {step.Milliseconds} ms"));

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,14 @@ private IrCallingConventions(CfgGeneratorContext context) {
/// <summary>How many iterations the last <see cref="Compute"/> ran to the fixed point.</summary>
internal int Iterations { get; private set; }

/// <summary>Computes the conventions of <paramref name="methods"/> to a fixed point.</summary>
/// <summary>Computes the conventions of <paramref name="methods"/> to a fixed point, invoking
/// <paramref name="roundStarted"/> with the 1-based number of each fixed-point iteration.</summary>
public static IrCallingConventions Compute(IReadOnlyList<PreparedMethod> methods,
CfgGeneratorContext context) {
CfgGeneratorContext context, Action<int> roundStarted) {
IrCallingConventions conventions = new(context);
conventions.Preprocess(methods);
conventions.SeedUnknownWays();
conventions.Iterate();
conventions.Iterate(roundStarted);
return conventions;
}

Expand Down Expand Up @@ -491,7 +492,7 @@ private IrPreservation CreatePreservation(PreparedMethod source, IrStackSlots sl
return call.Resolved ? TargetsChanges(call) : CpuValues.All;
}

private void Iterate() {
private void Iterate(Action<int> roundStarted) {
// Each round recomputes only the entry sites and outputs whose inputs changed: a site when
// an input, a change or an output it reads moved; an outputs when its entry's changes or a
// caller's liveness inputs moved. Writes happen in the same positions as a full recompute,
Expand All @@ -500,6 +501,7 @@ private void Iterate() {
HashSet<CfgCodePartitionEntry> sitesDirty = new(_methodByEntry.Keys);
HashSet<CfgCodePartitionEntry> outputsDirty = new(_states.Keys);
for (int iteration = 0; ; iteration++) {
roundStarted(iteration + 1);
bool changed = false;
foreach (MethodData method in _methods) {
foreach (EntrySite site in method.Entries) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ namespace Spice86.Core.Emulator.ReverseEngineer.CfgCodeGeneration;
using Spice86.Core.Emulator.ReverseEngineer.CfgCodeGeneration.Ir;
using Spice86.Core.Emulator.ReverseEngineer.CfgCodeGeneration.Ir.Memory;
using Spice86.Core.Emulator.ReverseEngineer.FunctionPartitioning.Model;
using Spice86.Core.Emulator.StateSerialization;

using System.Linq;

Expand All @@ -13,14 +14,23 @@ namespace Spice86.Core.Emulator.ReverseEngineer.CfgCodeGeneration;
/// </summary>
internal sealed record ProgramAnalysis(IrSegmentSummaries SegmentSummaries, IrCallingConventions CallingConventions,
TypedSignatures TypedSignatures, GlobalAccessClassification GlobalClasses) {
private const int PartCount = 4;

/// <summary>Runs the program-wide analyses of <paramref name="methods"/> in dependency order:
/// segment summaries, calling conventions, typed signatures, then the globals
/// classification over the final segment values.</summary>
/// classification over the final segment values. Each analysis is reported as a step of
/// <paramref name="progress"/> before it starts.</summary>
public static ProgramAnalysis Compute(IReadOnlyList<PreparedMethod> methods, CfgGeneratorContext context,
HardwareInterruptEffectBuilder effectBuilder, ProgramImages images) {
HardwareInterruptEffectBuilder effectBuilder, ProgramImages images, DumpProgressReporter progress) {
progress.Step(1, PartCount, "segment summaries");
IrSegmentSummaries summaries = IrSegmentSummaries.Compute(methods, context, effectBuilder);
IrCallingConventions conventions = IrCallingConventions.Compute(methods, context);
progress.Step(2, PartCount, "calling conventions");
IrCallingConventions conventions = IrCallingConventions.Compute(methods, context,
round => progress.Step(2, PartCount, $"calling conventions, round {round}"));
progress.Detail($"Calling conventions fixed point finished in {conventions.Iterations} iterations");
progress.Step(3, PartCount, "typed signatures");
TypedSignatures signatures = TypedSignatures.Compute(methods, conventions, context, effectBuilder);
progress.Step(4, PartCount, "globals classification");
GlobalAccessClassification classes = GlobalAccessClassification.Build(
methods.Select(method => (method.IrMethod, summaries.ValuesFor(method.Method), method.Ssa)),
images);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,24 @@ public enum DumpPhase {
/// <summary>Writing the execution flow JSON dump.</summary>
ExecutionFlow,

/// <summary>Code generation: program analysis.</summary>
/// <summary>Code generation: building the analysis context.</summary>
CodeAnalysis,

/// <summary>Code generation: planning and emitter setup.</summary>
CodePlan,

/// <summary>Code generation: per-method emission; progress is reported through steps.</summary>
/// <summary>Code generation: per-method preparation (lifting and passes); progress is reported
/// through steps.</summary>
CodeMethods,

/// <summary>Code generation: program-wide analyses (segment summaries, calling conventions,
/// typed signatures, globals) over the prepared methods.</summary>
CodeProgramAnalysis,

/// <summary>Code generation: per-method analysis and printing; progress is reported through
/// steps.</summary>
CodePrinting,

/// <summary>Writing the generated C# source file.</summary>
CodeSource,

Expand Down Expand Up @@ -73,9 +82,11 @@ public sealed class DumpProgressReporter {
[DumpPhase.CfgPartitions] = "CFG partitions",
[DumpPhase.CfgReload] = "CFG reload dump",
[DumpPhase.ExecutionFlow] = "Execution flow",
[DumpPhase.CodeAnalysis] = "Code generation: analysis",
[DumpPhase.CodePlan] = "Code generation: planning",
[DumpPhase.CodeMethods] = "Code generation: methods",
[DumpPhase.CodeAnalysis] = "Code generation (1/5): analysis",
[DumpPhase.CodePlan] = "Code generation (2/5): planning",
[DumpPhase.CodeMethods] = "Code generation (3/5): preparing methods",
[DumpPhase.CodeProgramAnalysis] = "Code generation (4/5): program analysis",
[DumpPhase.CodePrinting] = "Code generation (5/5): printing methods",
[DumpPhase.CodeSource] = "Generated C# source",
[DumpPhase.CodeProject] = "Generated project",
[DumpPhase.Symbols] = "Ghidra symbols",
Expand All @@ -84,7 +95,17 @@ public sealed class DumpProgressReporter {

private static readonly IReadOnlyDictionary<DumpPhase, string> StepLabels =
new Dictionary<DumpPhase, string> {
[DumpPhase.CodeMethods] = "Code generation: method"
[DumpPhase.CodeMethods] = "Code generation (3/5): preparing method",
[DumpPhase.CodeProgramAnalysis] = "Code generation (4/5): program analysis step",
[DumpPhase.CodePrinting] = "Code generation (5/5): printing method"
};

// The code-generation phases share one progress bar: each owns a fixed slice of it.
private static readonly IReadOnlyDictionary<DumpPhase, (double Start, double Share)> CodeGenerationShares =
new Dictionary<DumpPhase, (double Start, double Share)> {
[DumpPhase.CodeMethods] = (0.0, 0.4),
[DumpPhase.CodeProgramAnalysis] = (0.4, 0.5),
[DumpPhase.CodePrinting] = (0.9, 0.1)
};

private readonly ITimeProvider _timeProvider;
Expand Down Expand Up @@ -130,6 +151,21 @@ public DumpProgressReporter(ILogger logger, ITimeProvider timeProvider) {
internal static string StepText(DumpPhase phase, int current, int total, string item) =>
$"{StepLabelFor(phase)} {current} / {total} ({current * 100 / total}%): {item}";

/// <summary>The fraction of the progress bar step <paramref name="current"/> of
/// <paramref name="total"/> in <paramref name="phase"/> reaches. Code-generation phases map their
/// steps into their fixed slice of the shared bar; other phases span the whole bar.</summary>
internal static double ProgressFraction(DumpPhase phase, int current, int total) {
if (CodeGenerationShares.TryGetValue(phase, out (double Start, double Share) share)) {
return share.Start + share.Share * current / total;
}
return (double)current / total;
}

/// <summary>Whether <paramref name="phase"/> continues the shared code-generation progress bar
/// instead of resetting it.</summary>
internal static bool ContinuesProgress(DumpPhase phase) =>
CodeGenerationShares.TryGetValue(phase, out (double Start, double Share) share) && share.Start > 0;

/// <summary>Starts a dump into <paramref name="directory"/>.</summary>
public void BeginDump(string directory) {
_directory = directory;
Expand Down
14 changes: 8 additions & 6 deletions src/Spice86/ViewModels/DumpProgressViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public partial class DumpProgressViewModel : ObservableObject {
/// <summary>Current progress value while steps are being reported.</summary>
[ObservableProperty] private double _progressValue;

/// <summary>Total step count of the phase currently reporting steps.</summary>
/// <summary>Maximum of the progress bar: always 1, steps map to a fraction of it.</summary>
[ObservableProperty] private double _progressMaximum = 1;

/// <summary>True while no step has been reported yet in the current phase.</summary>
Expand All @@ -41,16 +41,18 @@ public void AddMessage(string message) {
/// <summary>Reports step <paramref name="current"/> of <paramref name="total"/> in <paramref name="phase"/>, naming <paramref name="item"/>.</summary>
public void ReportStep(DumpPhase phase, int current, int total, string item) {
IsIndeterminate = false;
ProgressMaximum = total;
ProgressValue = current;
ProgressValue = DumpProgressReporter.ProgressFraction(phase, current, total);
PhaseText = DumpProgressReporter.StepText(phase, current, total, item);
}

/// <summary>Resets the progress bar to indeterminate when a new phase begins.</summary>
public void BeginPhase() {
/// <summary>Resets the progress bar to indeterminate when a new phase begins, unless the phase
/// continues the shared code-generation bar.</summary>
public void BeginPhase(DumpPhase phase) {
if (DumpProgressReporter.ContinuesProgress(phase)) {
return;
}
IsIndeterminate = true;
ProgressValue = 0;
ProgressMaximum = 1;
}

/// <summary>Marks the dump as finished; the window may then close.</summary>
Expand Down
2 changes: 1 addition & 1 deletion src/Spice86/ViewModels/Services/DumpProgressUiService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public DumpProgressUiService(DumpProgressReporter reporter, IUIDispatcher uiDisp
}

private void OnPhaseBegan(DumpPhase phase) =>
_uiDispatcher.Post(() => _viewModel?.BeginPhase());
_uiDispatcher.Post(() => _viewModel?.BeginPhase(phase));

private void OnDumpStarted() => _uiDispatcher.Post(() => {
_viewModel = new DumpProgressViewModel();
Expand Down
36 changes: 27 additions & 9 deletions tests/Spice86.Tests/CfgCodeGeneration/GeneratorProgressTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ namespace Spice86.Tests.CfgCodeGeneration;
using static Spice86.Tests.CfgCodeGeneration.Ir.IrTestPrograms;

/// <summary>
/// The generator reports one CodeMethods step per emitted method through
/// <see cref="DumpProgressReporter"/>.
/// The generator reports one CodeMethods preparation step, the CodeProgramAnalysis parts and one
/// CodePrinting step per method through <see cref="DumpProgressReporter"/>.
/// </summary>
public class GeneratorProgressTest {
[Fact]
public void GenerateReportsCodeMethodsStepsOneToMethodCount() {
public void GenerateReportsStepsOfEveryCodeGenerationPhase() {
TestInstructionHelper helper = new();
CfgInstruction first = Parse(helper, 0x0000, 0x90);
CfgInstruction ret = Parse(helper, 0x0001, 0xC3);
Expand All @@ -37,14 +37,32 @@ public void GenerateReportsCodeMethodsStepsOneToMethodCount() {
DumpProgressReporter reporter = new(NullLogger.Instance, new SystemTimeProvider());
List<(DumpPhase Phase, int Current, int Total, string Item)> steps = new();
reporter.StepReported += (phase, current, total, item) => steps.Add((phase, current, total, item));
List<string> messages = new();
reporter.MessageLogged += messages.Add;

new CfgCSharpGenerator().Generate(program, reporter);

steps.Should().NotBeEmpty();
steps.Should().OnlyContain(s => s.Phase == DumpPhase.CodeMethods);
steps.Select(s => s.Current).Should().Equal(Enumerable.Range(1, steps.Count));
steps.Select(s => s.Total).Distinct().Should().Equal(steps.Count);
steps.Select(s => s.Item).Should().Equal("test_F000_0000_F0000");
List<(DumpPhase Phase, int Current, int Total, string Item)> methodsSteps =
steps.Where(s => s.Phase == DumpPhase.CodeMethods).ToList();
methodsSteps.Should().Equal((DumpPhase.CodeMethods, 1, 1, "test_F000_0000_F0000"));

List<(DumpPhase Phase, int Current, int Total, string Item)> analysisSteps =
steps.Where(s => s.Phase == DumpPhase.CodeProgramAnalysis).ToList();
analysisSteps.Select(s => s.Item).Take(3).Should().Equal(
"segment summaries", "calling conventions", "calling conventions, round 1");
analysisSteps.Select(s => s.Item).TakeLast(2).Should().Equal(
"typed signatures", "globals classification");
analysisSteps.Should().OnlyContain(s => s.Total == 4);
analysisSteps.Where(s => s.Current == 2).Should()
.OnlyContain(s => s.Item.StartsWith("calling conventions"));

List<(DumpPhase Phase, int Current, int Total, string Item)> printingSteps =
steps.Where(s => s.Phase == DumpPhase.CodePrinting).ToList();
printingSteps.Should().Equal((DumpPhase.CodePrinting, 1, 1, "test_F000_0000_F0000"));

steps.Select(s => s.Phase).Distinct().Should().Equal(
DumpPhase.CodeMethods, DumpPhase.CodeProgramAnalysis, DumpPhase.CodePrinting);
messages.Should().Contain(m => m.StartsWith("Calling conventions fixed point finished in "));
}

[Fact]
Expand Down Expand Up @@ -87,7 +105,7 @@ public void SlowMethodLineNamesEveryMeasuredStepInOrder() {

string line = CfgCSharpGenerator.SlowMethodLine(3, 7, "test_F000_0000_F0000", 30, 4, 80, timer);

line.Should().Be("Code generation: method 3 / 7 test_F000_0000_F0000 took 1212 ms " +
line.Should().Be("Code generation: slow method 3 / 7 test_F000_0000_F0000 took 1212 ms " +
"(30 instructions, 4 IR blocks, 80 statements): lift 5 ms, ssa build 1 1200 ms, printing 7 ms");
}
}
2 changes: 2 additions & 0 deletions tests/Spice86.Tests/DumpProgressTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public class DumpProgressTest {
DumpPhase.CodeAnalysis,
DumpPhase.CodePlan,
DumpPhase.CodeMethods,
DumpPhase.CodeProgramAnalysis,
DumpPhase.CodePrinting,
DumpPhase.CodeSource,
DumpPhase.CodeProject,
DumpPhase.Symbols,
Expand Down
Loading
Loading