Some cleanup

This commit is contained in:
nub31
2025-10-16 15:48:04 +02:00
parent 285ad74240
commit ca10288f52
10 changed files with 95 additions and 1510 deletions

View File

@@ -1,29 +0,0 @@
using System.Diagnostics;
namespace NubLang.CLI;
public static class Archive
{
public static async Task<bool> Invoke(string fileName, params IEnumerable<string> objectFiles)
{
using var process = new Process();
process.StartInfo = new ProcessStartInfo("ar", ["rcs", fileName, ..objectFiles])
{
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true
};
process.Start();
await process.WaitForExitAsync();
var errors = await process.StandardError.ReadToEndAsync();
if (!string.IsNullOrWhiteSpace(errors))
{
await Console.Error.WriteLineAsync(errors);
}
return process.ExitCode == 0;
}
}

View File

@@ -1,54 +0,0 @@
using System.Diagnostics;
namespace NubLang.CLI;
public static class GCC
{
public static async Task<bool> Assemble(string asmPath, string outPath)
{
using var process = new Process();
process.StartInfo = new ProcessStartInfo("x86_64-elf-as", ["-nostartfiles", "-o", outPath, asmPath])
{
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true
};
process.Start();
await process.WaitForExitAsync();
var errors = await process.StandardError.ReadToEndAsync();
if (!string.IsNullOrWhiteSpace(errors))
{
await Console.Error.WriteLineAsync(errors);
}
return process.ExitCode == 0;
}
public static async Task<bool> Compile(string cPath, string outPath)
{
using var process = new Process();
process.StartInfo = new ProcessStartInfo("gcc", ["-ffreestanding", "-nostartfiles", "-c", "-o", outPath, cPath])
{
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true
};
process.Start();
await process.WaitForExitAsync();
var errors = await process.StandardError.ReadToEndAsync();
if (!string.IsNullOrWhiteSpace(errors))
{
await Console.Error.WriteLineAsync(errors);
}
return process.ExitCode == 0;
}
}

View File

@@ -6,8 +6,6 @@ using NubLang.Generation;
using NubLang.Modules;
using NubLang.Syntax;
var sw = Stopwatch.StartNew();
var options = new Options();
for (var i = 0; i < args.Length; i++)
@@ -34,9 +32,6 @@ for (var i = 0; i < args.Length; i++)
}
}
Console.WriteLine($"Parse cli args: {sw.ElapsedMilliseconds}ms");
sw.Restart();
foreach (var file in options.Files)
{
if (!File.Exists(file))
@@ -46,9 +41,6 @@ foreach (var file in options.Files)
}
}
Console.WriteLine($"Check file exists: {sw.ElapsedMilliseconds}ms");
sw.Restart();
var diagnostics = new List<Diagnostic>();
var syntaxTrees = new List<SyntaxTree>();
@@ -58,26 +50,15 @@ foreach (var file in options.Files)
tokenizer.Tokenize();
diagnostics.AddRange(tokenizer.Diagnostics);
Console.WriteLine($" Tokenize: {Path.GetFileName(file)}: {sw.ElapsedMilliseconds}ms");
sw.Restart();
var parser = new Parser();
var syntaxTree = parser.Parse(tokenizer.Tokens);
diagnostics.AddRange(parser.Diagnostics);
Console.WriteLine($" Parse: {Path.GetFileName(file)}: {sw.ElapsedMilliseconds}ms");
sw.Restart();
syntaxTrees.Add(syntaxTree);
}
sw.Restart();
var moduleRepository = new ModuleRepository(syntaxTrees);
Console.WriteLine($"Create module repository: {sw.ElapsedMilliseconds}ms");
sw.Restart();
var definitions = new List<DefinitionNode>();
var referencedStructTypes = new HashSet<NubStructType>();
@@ -87,9 +68,6 @@ foreach (var syntaxTree in syntaxTrees)
var typeChecker = new TypeChecker(syntaxTree, moduleRepository);
typeChecker.Check();
Console.WriteLine($" Type check {syntaxTree.Metadata.ModuleName}: {sw.ElapsedMilliseconds}ms");
sw.Restart();
definitions.AddRange(typeChecker.Definitions);
diagnostics.AddRange(typeChecker.Diagnostics);
@@ -99,16 +77,11 @@ foreach (var syntaxTree in syntaxTrees)
}
}
sw.Restart();
foreach (var diagnostic in diagnostics)
{
Console.Error.WriteLine(diagnostic.FormatANSI());
}
Console.WriteLine($"Print diagnostics: {sw.ElapsedMilliseconds}ms");
sw.Restart();
if (diagnostics.Any(diagnostic => diagnostic.Severity == DiagnosticSeverity.Error))
{
return 1;
@@ -116,38 +89,20 @@ if (diagnostics.Any(diagnostic => diagnostic.Severity == DiagnosticSeverity.Erro
Directory.CreateDirectory(".build");
var generator = new CGenerator(definitions, referencedStructTypes);
var generator = new Generator(definitions, referencedStructTypes);
var c = generator.Emit();
var cFilePath = Path.Combine(".build", "out.c");
File.WriteAllText(cFilePath, c);
var objFilePath = Path.Combine(".build", "out.o");
var asmSuccess = await GCC.Compile(cFilePath, objFilePath);
if (!asmSuccess) return 1;
using var process = Process.Start("gcc", ["-ffreestanding", "-nostartfiles", "-c", "-o", Path.Combine(".build", "out.o"), cFilePath]);
// sw.Restart();
//
// var generator = new QBEGenerator(definitions, referencedStructTypes);
// var ssa = generator.Emit();
// var ssaFilePath = Path.Combine(".build", "out.ssa");
// File.WriteAllText(ssaFilePath, ssa);
//
// Console.WriteLine($"Emit ssa: {sw.ElapsedMilliseconds}ms");
// sw.Restart();
//
// var asmFilePath = Path.Combine(".build", "out.asm");
// var qbeSuccess = await QBE.Invoke(ssaFilePath, asmFilePath);
// if (!qbeSuccess) return 1;
//
// Console.WriteLine($"Emit asm: {sw.ElapsedMilliseconds}ms");
// sw.Restart();
//
// var objFilePath = Path.Combine(".build", "out.o");
// var asmSuccess = await GCC.Assemble(asmFilePath, objFilePath);
// if (!asmSuccess) return 1;
//
// Console.WriteLine($"Assemble: {sw.ElapsedMilliseconds}ms");
sw.Restart();
process.WaitForExit();
if (process.ExitCode != 0)
{
Console.Error.WriteLine($"gcc failed with exit code {process.ExitCode}");
return 1;
}
return 0;

View File

@@ -1,30 +0,0 @@
using System.Diagnostics;
namespace NubLang.CLI;
public static class QBE
{
public static async Task<bool> Invoke(string ssaPath, string outPath)
{
using var process = new Process();
process.StartInfo = new ProcessStartInfo("qbe", ["-o", outPath, ssaPath])
{
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true
};
process.Start();
await process.WaitForExitAsync();
var errors = await process.StandardError.ReadToEndAsync();
if (!string.IsNullOrWhiteSpace(errors))
{
await Console.Error.WriteLineAsync(errors);
}
return process.ExitCode == 0;
}
}