cli improvements
This commit is contained in:
4
example/.gitignore
vendored
4
example/.gitignore
vendored
@@ -1,2 +1,2 @@
|
|||||||
bin
|
build
|
||||||
bin-int
|
out
|
||||||
@@ -1,17 +1,18 @@
|
|||||||
|
CC = clang
|
||||||
NUBC = ../src/compiler/NubLang.CLI/bin/Debug/net9.0/nubc
|
NUBC = ../src/compiler/NubLang.CLI/bin/Debug/net9.0/nubc
|
||||||
|
|
||||||
out: $(NUBC) main.nub
|
out: build/out.a
|
||||||
$(NUBC) main.nub
|
$(CC) -o out build/out.a
|
||||||
|
|
||||||
|
build/out.a: $(NUBC) src/main.nub
|
||||||
|
$(NUBC) src/main.nub
|
||||||
|
|
||||||
$(NUBC):
|
$(NUBC):
|
||||||
dotnet build ../src/compiler/NubLang.CLI/NubLang.CLI.csproj
|
dotnet build ../src/compiler/NubLang.CLI/NubLang.CLI.csproj
|
||||||
|
|
||||||
run: out
|
run: out
|
||||||
out
|
./out
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
|
@rm -r build 2>/dev/null || true
|
||||||
@rm out 2>/dev/null || true
|
@rm out 2>/dev/null || true
|
||||||
@rm out.a 2>/dev/null || true
|
|
||||||
@find . -name "*.o" -type f -delete
|
|
||||||
@find . -name "*.s" -type f -delete
|
|
||||||
@find . -name "*.ssa" -type f -delete
|
|
||||||
|
|||||||
8
example/src/main.nub
Normal file
8
example/src/main.nub
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
// c
|
||||||
|
extern func puts(text: cstring)
|
||||||
|
|
||||||
|
func main(args: []cstring): i64
|
||||||
|
{
|
||||||
|
puts("Hello, World")
|
||||||
|
return 0
|
||||||
|
}
|
||||||
@@ -27,28 +27,4 @@ public static class GCC
|
|||||||
|
|
||||||
return process.ExitCode == 0;
|
return process.ExitCode == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static async Task<bool> Link(string executablePath, params IEnumerable<string> objectFiles)
|
|
||||||
{
|
|
||||||
using var process = new Process();
|
|
||||||
process.StartInfo = new ProcessStartInfo("gcc", ["-nostartfiles", "-o", executablePath, ..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("gcc error when linking:\n" + errors);
|
|
||||||
}
|
|
||||||
|
|
||||||
return process.ExitCode == 0;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -5,9 +5,5 @@ namespace NubLang.CLI;
|
|||||||
public class Options
|
public class Options
|
||||||
{
|
{
|
||||||
public string? OutputPath { get; set; }
|
public string? OutputPath { get; set; }
|
||||||
public bool Link { get; set; } = true;
|
|
||||||
public List<SourceFile> Files { get; } = [];
|
public List<SourceFile> Files { get; } = [];
|
||||||
public bool EmitSsa { get; set; } = false;
|
|
||||||
public bool EmitAsm { get; set; } = false;
|
|
||||||
public bool EmitObj { get; set; } = false;
|
|
||||||
}
|
}
|
||||||
@@ -15,37 +15,24 @@ var options = new Options();
|
|||||||
for (var i = 0; i < args.Length; i++)
|
for (var i = 0; i < args.Length; i++)
|
||||||
{
|
{
|
||||||
var arg = args[i];
|
var arg = args[i];
|
||||||
|
switch (arg)
|
||||||
if (arg == "-o")
|
|
||||||
{
|
{
|
||||||
i++;
|
case "-o":
|
||||||
if (i >= args.Length - 1)
|
|
||||||
{
|
{
|
||||||
Console.Error.WriteLine($"{arg} must be followed by a value");
|
++i;
|
||||||
return 1;
|
if (i >= args.Length)
|
||||||
}
|
{
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
options.OutputPath = args[i];
|
options.OutputPath = args[i];
|
||||||
}
|
break;
|
||||||
else if (arg == "-c")
|
}
|
||||||
{
|
default:
|
||||||
options.Link = false;
|
{
|
||||||
}
|
options.Files.Add(new SourceFile(arg));
|
||||||
else if (arg == "-ssa")
|
break;
|
||||||
{
|
}
|
||||||
options.EmitSsa = true;
|
|
||||||
}
|
|
||||||
else if (arg == "-asm")
|
|
||||||
{
|
|
||||||
options.EmitAsm = true;
|
|
||||||
}
|
|
||||||
else if (arg == "-obj")
|
|
||||||
{
|
|
||||||
options.EmitObj = true;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
options.Files.Add(new SourceFile(arg));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -104,35 +91,31 @@ var objectFiles = new List<string>();
|
|||||||
for (var i = 0; i < typedSyntaxTrees.Count; i++)
|
for (var i = 0; i < typedSyntaxTrees.Count; i++)
|
||||||
{
|
{
|
||||||
var syntaxTree = typedSyntaxTrees[i];
|
var syntaxTree = typedSyntaxTrees[i];
|
||||||
var outFileName = Path.ChangeExtension(options.Files[i].Path, null);
|
var outFileName = Path.Combine("build", "code", Path.ChangeExtension(options.Files[i].Path, null));
|
||||||
|
|
||||||
|
var outFileDir = Path.GetDirectoryName(outFileName);
|
||||||
|
if (!string.IsNullOrEmpty(outFileDir))
|
||||||
|
{
|
||||||
|
Directory.CreateDirectory(outFileDir);
|
||||||
|
}
|
||||||
|
|
||||||
var generator = new QBEGenerator(syntaxTree, typedDefinitionTable);
|
var generator = new QBEGenerator(syntaxTree, typedDefinitionTable);
|
||||||
var ssa = generator.Emit();
|
var ssa = generator.Emit();
|
||||||
|
|
||||||
var ssaFilePath = $"{outFileName}.ssa";
|
var ssaFilePath = Path.ChangeExtension(outFileName, "ssa");
|
||||||
File.WriteAllText(ssaFilePath, ssa);
|
File.WriteAllText(ssaFilePath, ssa);
|
||||||
|
|
||||||
var asmFilePath = $"{outFileName}.s";
|
var asmFilePath = Path.ChangeExtension(outFileName, "s");
|
||||||
var qbeSuccess = await QBE.Invoke(ssaFilePath, asmFilePath);
|
var qbeSuccess = await QBE.Invoke(ssaFilePath, asmFilePath);
|
||||||
|
|
||||||
if (!options.EmitSsa)
|
|
||||||
{
|
|
||||||
File.Delete(ssaFilePath);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!qbeSuccess)
|
if (!qbeSuccess)
|
||||||
{
|
{
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
var objFilePath = $"{outFileName}.o";
|
var objFilePath = Path.ChangeExtension(outFileName, "o");
|
||||||
var asmSuccess = await GCC.Assemble(asmFilePath, objFilePath);
|
var asmSuccess = await GCC.Assemble(asmFilePath, objFilePath);
|
||||||
|
|
||||||
if (!options.EmitAsm)
|
|
||||||
{
|
|
||||||
File.Delete(asmFilePath);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!asmSuccess)
|
if (!asmSuccess)
|
||||||
{
|
{
|
||||||
return 1;
|
return 1;
|
||||||
@@ -141,50 +124,43 @@ for (var i = 0; i < typedSyntaxTrees.Count; i++)
|
|||||||
objectFiles.Add(objFilePath);
|
objectFiles.Add(objFilePath);
|
||||||
}
|
}
|
||||||
|
|
||||||
var runtimeFilePath = "libruntime_x64.a";
|
const string runtimeName = "libruntime_x64.a";
|
||||||
|
|
||||||
var resources = Assembly.GetExecutingAssembly().GetManifestResourceNames();
|
var resources = Assembly.GetExecutingAssembly().GetManifestResourceNames();
|
||||||
var runtime = resources.First(r => r.EndsWith(runtimeFilePath));
|
var runtime = resources.First(r => r.EndsWith(runtimeName));
|
||||||
|
|
||||||
await using var reader = Assembly.GetExecutingAssembly().GetManifestResourceStream(runtime);
|
await using var reader = Assembly.GetExecutingAssembly().GetManifestResourceStream(runtime);
|
||||||
|
|
||||||
if (reader == null)
|
if (reader == null)
|
||||||
{
|
{
|
||||||
Console.Error.WriteLine($"Cannot open stream to '{runtimeFilePath}'");
|
Console.Error.WriteLine($"Cannot open stream to '{runtimeName}'");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
await using var writer = new FileStream(runtimeFilePath, FileMode.Create);
|
var runtimePath = Path.Combine("build", "runtime", runtimeName);
|
||||||
|
var runtimeDir = Path.GetDirectoryName(runtimePath);
|
||||||
|
if (!string.IsNullOrEmpty(runtimeDir))
|
||||||
|
{
|
||||||
|
Directory.CreateDirectory(runtimeDir);
|
||||||
|
}
|
||||||
|
|
||||||
|
await using var writer = new FileStream(runtimePath, FileMode.Create);
|
||||||
|
|
||||||
reader.CopyTo(writer);
|
reader.CopyTo(writer);
|
||||||
|
|
||||||
objectFiles.Add(runtimeFilePath);
|
objectFiles.Add(runtimePath);
|
||||||
|
|
||||||
if (options.Link)
|
var outPath = options.OutputPath ?? Path.Combine("build", "out.a");
|
||||||
|
var outDir = Path.GetDirectoryName(outPath);
|
||||||
|
if (!string.IsNullOrEmpty(outDir))
|
||||||
{
|
{
|
||||||
var outPath = options.OutputPath ?? "out";
|
Directory.CreateDirectory(outDir);
|
||||||
var linkResult = await GCC.Link(outPath, objectFiles);
|
|
||||||
if (!linkResult)
|
|
||||||
{
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
var outPath = options.OutputPath ?? "out.a";
|
|
||||||
var archiveResult = await Archive.Invoke(outPath, objectFiles);
|
|
||||||
if (!archiveResult)
|
|
||||||
{
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!options.EmitObj)
|
var archiveResult = await Archive.Invoke(outPath, objectFiles);
|
||||||
|
if (!archiveResult)
|
||||||
{
|
{
|
||||||
foreach (var objectFile in objectFiles)
|
return 1;
|
||||||
{
|
|
||||||
File.Delete(objectFile);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
Reference in New Issue
Block a user