Fix bugs in cli

This commit is contained in:
nub31
2025-08-18 16:29:41 +02:00
parent b8c69b5583
commit 860e1fd0e4
4 changed files with 53 additions and 47 deletions

View File

@@ -4,19 +4,18 @@ namespace NubLang.CLI;
public static class GCC
{
public static async Task<bool> Assemble(string asm, string objPath)
public static async Task<bool> Assemble(string asmPath, string outPath)
{
var dir = Path.GetDirectoryName(objPath);
var dir = Path.GetDirectoryName(outPath);
if (dir != null)
{
Directory.CreateDirectory(dir);
}
using var process = new Process();
process.StartInfo = new ProcessStartInfo("gcc", ["-xassembler", "-c", "-o", objPath, "-"])
process.StartInfo = new ProcessStartInfo("gcc", ["-xassembler", "-c", "-o", outPath, asmPath])
{
UseShellExecute = false,
RedirectStandardInput = true,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true
@@ -24,9 +23,6 @@ public static class GCC
process.Start();
await process.StandardInput.WriteAsync(asm);
process.StandardInput.Close();
await process.WaitForExitAsync();
var errors = await process.StandardError.ReadToEndAsync();
@@ -50,7 +46,6 @@ public static class GCC
process.StartInfo = new ProcessStartInfo("gcc", ["-nostartfiles", "-o", executablePath, ..objectFiles])
{
UseShellExecute = false,
RedirectStandardInput = true,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true

View File

@@ -14,7 +14,8 @@ const string BIN_DIR = "bin";
const string INT_DIR = "bin-int";
var INT_BUILTIN_DIR = Path.Join(INT_DIR, "builtin");
var INT_OBJECT_DIR = Path.Join(INT_DIR, "obj");
var INT_DEBUG_DIR = Path.Join(INT_DIR, "debug");
var INT_SSA_DIR = Path.Join(INT_DIR, "ssa");
var INT_ASM_DIR = Path.Join(INT_DIR, "asm");
if (Directory.Exists(INT_DIR))
{
@@ -24,7 +25,8 @@ if (Directory.Exists(INT_DIR))
Directory.CreateDirectory(BIN_DIR);
Directory.CreateDirectory(INT_BUILTIN_DIR);
Directory.CreateDirectory(INT_OBJECT_DIR);
Directory.CreateDirectory(INT_DEBUG_DIR);
Directory.CreateDirectory(INT_SSA_DIR);
Directory.CreateDirectory(INT_ASM_DIR);
var options = new Options();
@@ -55,7 +57,6 @@ for (var i = 0; i < args.Length; i++)
}
else if (arg == "-c")
{
i++;
options.Link = false;
}
else
@@ -123,24 +124,25 @@ for (var i = 0; i < typedSyntaxTrees.Count; i++)
var generator = new QBEGenerator(syntaxTree, typedDefinitionTable);
var ssa = generator.Emit();
File.WriteAllText(Path.Join(INT_DEBUG_DIR, $"{outFileName}.ssa"), ssa);
var asm = await QBE.Invoke(ssa);
if (asm == null)
var ssaPath = Path.Join(INT_SSA_DIR, $"{outFileName}.ssa");
File.WriteAllText(ssaPath, ssa);
var asmFilePath = Path.Join(INT_ASM_DIR, $"{outFileName}.s");
var qbeSuccess = await QBE.Invoke(ssaPath, asmFilePath);
if (!qbeSuccess)
{
return 1;
}
File.WriteAllText(Path.Join(INT_DEBUG_DIR, $"{outFileName}.s"), asm);
var fileName = Path.Join(INT_OBJECT_DIR, $"{outFileName}.o");
var asmSuccess = await GCC.Assemble(asm, fileName);
var objFilePath = Path.Join(INT_OBJECT_DIR, $"{outFileName}.o");
var asmSuccess = await GCC.Assemble(asmFilePath, objFilePath);
if (!asmSuccess)
{
return 1;
}
objectFiles.Add(fileName);
objectFiles.Add(objFilePath);
}
if (options.CustomRuntime == null)

View File

@@ -4,13 +4,12 @@ namespace NubLang.CLI;
public static class QBE
{
public static async Task<string?> Invoke(string ssa)
public static async Task<bool> Invoke(string ssaPath, string outPath)
{
using var process = new Process();
process.StartInfo = new ProcessStartInfo("qbe")
process.StartInfo = new ProcessStartInfo("qbe", ["-o", outPath, ssaPath])
{
UseShellExecute = false,
RedirectStandardInput = true,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true
@@ -18,9 +17,6 @@ public static class QBE
process.Start();
await process.StandardInput.WriteAsync(ssa);
process.StandardInput.Close();
await process.WaitForExitAsync();
var errors = await process.StandardError.ReadToEndAsync();
@@ -29,8 +25,6 @@ public static class QBE
await Console.Error.WriteLineAsync("qbe error:\n" + errors);
}
var asm = await process.StandardOutput.ReadToEndAsync();
return process.ExitCode == 0 ? asm : null;
return process.ExitCode == 0;
}
}
}