Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mosa.Utility.Launcher: Code style changes #1222

Merged
merged 2 commits into from
May 4, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 0 additions & 1 deletion Source/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ resharper_csharp_allow_comment_after_lbrace = true
resharper_csharp_blank_lines_around_field = 0
resharper_csharp_blank_lines_around_invocable = 0
resharper_csharp_blank_lines_around_region = 0
resharper_csharp_case_block_braces = next_line_shifted_2
resharper_csharp_indent_statement_pars = outside
resharper_csharp_max_line_length = 15750
resharper_csharp_remove_blank_lines_near_braces_in_code = false
Expand Down
21 changes: 8 additions & 13 deletions Source/Mosa.Utility.Launcher/BaseLauncher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,19 @@ public BaseLauncher(MosaSettings mosaSettings, CompilerHooks compilerHook)

protected void OutputStatus(string status)
{
if (string.IsNullOrEmpty(status)) return;
if (string.IsNullOrEmpty(status))
return;

OutputEvent(status);
}

protected virtual void OutputEvent(string status)
{
CompilerHooks.NotifyStatus?.Invoke(status);
}
protected virtual void OutputEvent(string status) => CompilerHooks.NotifyStatus?.Invoke(status);

protected static byte[] GetResource(string path, string name)
{
return GetResource($"{path
.Replace(".", "._")
.Replace(@"\", "._")
.Replace("/", "._")
.Replace("-", "_")}.{name}");
}
protected static byte[] GetResource(string path, string name) => GetResource($"{path
.Replace(".", "._")
.Replace(@"\", "._")
.Replace("/", "._")
.Replace("-", "_")}.{name}");

protected Process CreateApplicationProcess(string app, string args)
{
Expand Down
144 changes: 61 additions & 83 deletions Source/Mosa.Utility.Launcher/Builder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,7 @@ public Builder(MosaSettings mosaSettings, CompilerHooks compilerHooks)
{
Counters = new List<string>();

if (CompilerHooks.NotifyEvent == null)
{
CompilerHooks.NotifyEvent = NotifyEvent;
}
CompilerHooks.NotifyEvent ??= NotifyEvent;
}

public void Build()
Expand Down Expand Up @@ -69,14 +66,10 @@ public void Build()
BuildImage();

if (!string.IsNullOrWhiteSpace(MosaSettings.NasmFile))
{
LaunchNDISASM();
}

if (!string.IsNullOrWhiteSpace(MosaSettings.AsmFile))
{
GenerateASMFile();
}

IsSucccessful = true;
}
Expand All @@ -87,8 +80,6 @@ public void Build()
}
finally
{
//compiler = null;

Stopwatch.Stop();
}
}
Expand All @@ -98,7 +89,6 @@ private bool Compile()
OutputStatus($"Compiling: {MosaSettings.SourceFiles[0]}");

var compiler = new MosaCompiler(MosaSettings, CompilerHooks, new ClrModuleLoader(), new ClrTypeResolver());

compiler.Load();
compiler.Initialize();
compiler.Setup();
Expand All @@ -112,31 +102,32 @@ private bool Compile()

private void BuildImage()
{
if (string.IsNullOrWhiteSpace(MosaSettings.ImageFormat)) return;
if (string.IsNullOrWhiteSpace(MosaSettings.ImageFormat))
return;

OutputStatus($"Generating Image: {MosaSettings.ImageFormat}");

switch (MosaSettings.ImageFormat)
{
case "vmdk":
{
var imageFile = Path.Combine(MosaSettings.TemporaryFolder, $"{Path.GetFileNameWithoutExtension(MosaSettings.ImageFile)}.img");
CreateDiskImage(imageFile);
CreateVMDK(imageFile);
break;
}
{
var imageFile = Path.Combine(MosaSettings.TemporaryFolder, $"{Path.GetFileNameWithoutExtension(MosaSettings.ImageFile)}.img");
CreateDiskImage(imageFile);
CreateVMDK(imageFile);
break;
}
case "vdi":
{
var imageFile = Path.Combine(MosaSettings.TemporaryFolder, $"{Path.GetFileNameWithoutExtension(MosaSettings.ImageFile)}.img");
CreateDiskImage(imageFile);
CreateVDI(imageFile);
break;
}
{
var imageFile = Path.Combine(MosaSettings.TemporaryFolder, $"{Path.GetFileNameWithoutExtension(MosaSettings.ImageFile)}.img");
CreateDiskImage(imageFile);
CreateVDI(imageFile);
break;
}
default:
{
CreateDiskImage(MosaSettings.ImageFile);
break;
}
{
CreateDiskImage(MosaSettings.ImageFile);
break;
}
}
}

Expand All @@ -151,7 +142,6 @@ private void AddCounters(string data)
private void CreateDiskImage(string imageFile)
{
var bootImageOptions = new BootImageOptions();

bootImageOptions.IncludeFiles.Add(new IncludeFile("limine.cfg", GetLimineCFG()));
bootImageOptions.IncludeFiles.Add(new IncludeFile("limine.sys", GetResource("limine", "limine.sys")));
bootImageOptions.IncludeFiles.Add(new IncludeFile(MosaSettings.OutputFile, "kernel.bin"));
Expand Down Expand Up @@ -196,39 +186,28 @@ private void CreateDiskImage(string imageFile)
Generator.Create(bootImageOptions);
}

private byte[] GetLimineCFG()
{
return Encoding.ASCII.GetBytes($"TIMEOUT={MosaSettings.BootLoaderTimeout}\nINTERFACE_RESOLUTION=640x480\nINTERFACE_BRANDING=Managed Operating System Alliance\n:{MosaSettings.OSName}\nPROTOCOL=multiboot2\nKERNEL_PATH=boot:///kernel.bin");
}
private byte[] GetLimineCFG() => Encoding.ASCII.GetBytes($"""
TIMEOUT={MosaSettings.BootLoaderTimeout}
INTERFACE_RESOLUTION=640x480
INTERFACE_BRANDING=Managed Operating System Alliance
:{MosaSettings.OSName}
PROTOCOL=multiboot2
KERNEL_PATH=boot:///kernel.bin
""");

private void CreateVMDK(string source)
{
var arg = $"convert -f raw -O vmdk \"{source}\" \"{MosaSettings.ImageFile}\"";

LaunchApplicationWithOutput(MosaSettings.QemuImgApp, arg);
}
=> LaunchApplicationWithOutput(MosaSettings.QemuImgApp, $"convert -f raw -O vmdk \"{source}\" \"{MosaSettings.ImageFile}\"");

private void CreateVDI(string source)
{
var arg = $"convert -f raw -O vdi \"{source}\" \"{MosaSettings.ImageFile}\"";

LaunchApplicationWithOutput(MosaSettings.QemuImgApp, arg);
}
=> LaunchApplicationWithOutput(MosaSettings.QemuImgApp, $"convert -f raw -O vdi \"{source}\" \"{MosaSettings.ImageFile}\"");

private void LaunchNDISASM()
{
OutputStatus($"Executing NDISASM: {MosaSettings.NasmFile}");

//var textSection = Linker.Sections[(int)SectionKind.Text];
var startingAddress = MosaSettings.BaseAddress + MultibootHeaderLength;
var fileOffset = Linker.BaseFileOffset + MultibootHeaderLength;

var arg = $"-b 32 -o0x{startingAddress:x} -e0x{fileOffset:x} \"{MosaSettings.OutputFile}\"";

//var nasmfile = Path.Combine(LauncherSettings.ImageFolder, $"{Path.GetFileNameWithoutExtension(LauncherSettings.SourceFiles[0])}.nasm");

var process = LaunchApplication(MosaSettings.NdisasmApp, arg);

var process = LaunchApplication(MosaSettings.NdisasmApp, $"-b 32 -o0x{startingAddress:x} -e0x{fileOffset:x} \"{MosaSettings.OutputFile}\"");
var output = GetOutput(process);

File.WriteAllText(MosaSettings.NasmFile, output);
Expand Down Expand Up @@ -257,7 +236,6 @@ private void GenerateASMFile()
var length = textSection.Size;

var code2 = File.ReadAllBytes(MosaSettings.OutputFile);

var code = new byte[code2.Length];

for (ulong i = fileOffset; i < (ulong)code2.Length; i++)
Expand All @@ -266,46 +244,46 @@ private void GenerateASMFile()
var disassembler = new Disassembler.Disassembler(MosaSettings.Platform);
disassembler.SetMemory(code, startingAddress);

using (var dest = File.CreateText(MosaSettings.AsmFile))
using var dest = File.CreateText(MosaSettings.AsmFile);

foreach (var instruction in disassembler.Decode())
{
foreach (var instruction in disassembler.Decode())
{
if (map.TryGetValue(instruction.Address, out List<string> list))
{
foreach (var entry in list)
{
dest.WriteLine($"; {entry}");
}
}

dest.WriteLine(instruction.Full);

if (instruction.Address > startingAddress + length)
break;
}
if (map.TryGetValue(instruction.Address, out List<string> list))
foreach (var entry in list)
dest.WriteLine($"; {entry}");

dest.WriteLine(instruction.Full);

if (instruction.Address > startingAddress + length)
break;
}
}

private void NotifyEvent(CompilerEvent compilerEvent, string message, int threadID)
{
if (compilerEvent is CompilerEvent.Exception)
switch (compilerEvent)
{
var status = $"[Exception] {message}";

OutputStatus(status);
}
else if (compilerEvent is CompilerEvent.CompilerStart or CompilerEvent.CompilerEnd or CompilerEvent.CompilingMethodsStart or CompilerEvent.CompilingMethodsCompleted or CompilerEvent.InlineMethodsScheduled or CompilerEvent.LinkingStart or CompilerEvent.LinkingEnd or CompilerEvent.Warning or CompilerEvent.Error)
{
var status = $"{compilerEvent.ToText()}";
case CompilerEvent.Exception:
{
var status = $"[Exception] {message}";

if (!string.IsNullOrEmpty(message))
status += $" => {message}";
OutputStatus(status);
break;
}
case CompilerEvent.CompilerStart or CompilerEvent.CompilerEnd or CompilerEvent.CompilingMethodsStart or CompilerEvent.CompilingMethodsCompleted or CompilerEvent.InlineMethodsScheduled or CompilerEvent.LinkingStart or CompilerEvent.LinkingEnd or CompilerEvent.Warning or CompilerEvent.Error:
{
var status = compilerEvent.ToText();
if (!string.IsNullOrEmpty(message))
status += $" => {message}";

OutputStatus(status);
}
else if (compilerEvent == CompilerEvent.Counter)
{
AddCounters(message);
OutputStatus(status);
break;
}
case CompilerEvent.Counter:
{
AddCounters(message);
break;
}
}
}
}