init llvm

This commit is contained in:
nub31
2025-10-21 18:03:27 +02:00
parent c3d774fd78
commit 3dab4c7e60
5 changed files with 955 additions and 535 deletions

View File

@@ -45,116 +45,126 @@ if (diagnostics.Any(diagnostic => diagnostic.Severity == DiagnosticSeverity.Erro
return 1;
}
var cPaths = new List<string>();
Directory.CreateDirectory(".build");
for (var i = 0; i < nubFiles.Length; i++)
{
var file = nubFiles[i];
var compilationUnit = compilationUnits[i];
var generator = new Generator(compilationUnit);
var directory = Path.GetDirectoryName(file);
if (!string.IsNullOrWhiteSpace(directory))
{
Directory.CreateDirectory(Path.Combine(".build", directory));
}
var path = Path.Combine(".build", Path.ChangeExtension(file, "c"));
File.WriteAllText(path, generator.Emit());
cPaths.Add(path);
Console.WriteLine(generator.Generate());
}
var objectPaths = new List<string>();
return 0;
foreach (var cPath in cPaths)
{
var objectPath = Path.ChangeExtension(cPath, "o");
using var compileProcess = Process.Start("gcc", [
"-ffreestanding", "-nostartfiles", "-std=c23",
"-g", "-lm",
"-c", "-o", objectPath,
cPath,
]);
compileProcess.WaitForExit();
if (compileProcess.ExitCode != 0)
{
Console.Error.WriteLine($"gcc failed with exit code {compileProcess.ExitCode}");
return 1;
}
objectPaths.Add(objectPath);
}
if (modules.TryGetValue("main", out var mainModule))
{
var mainFunction = mainModule
.Functions(true)
.FirstOrDefault(x => x.Prototype.ExternSymbol == "main");
if (mainFunction is { Prototype.ExternSymbol: not null })
{
var runtime = $"""
.intel_syntax noprefix
.text
.globl _start
_start:
mov rdi, [rsp] # argc
mov rsi, [rsp + 8] # argv
call {mainFunction.Prototype.ExternSymbol}
mov rdi, rax # Move return value into rdi
mov rax, 60 # syscall: exit
syscall
""";
var runtimePath = Path.Combine(".build", "runtime.s");
File.WriteAllText(runtimePath, runtime);
using var assembleProcess = Process.Start(new ProcessStartInfo("as", ["-g", "-c", runtimePath, "-o", Path.Combine(".build", "runtime.o")]));
if (assembleProcess == null) return 1;
assembleProcess.WaitForExit();
if (assembleProcess.ExitCode != 0)
{
Console.Error.WriteLine($"gcc failed with exit code {assembleProcess.ExitCode}");
return 1;
}
if (assembleProcess.ExitCode != 0) return 1;
using var linkProcess = Process.Start(new ProcessStartInfo("gcc", [
"-ffreestanding", "-nostartfiles", "-std=c23",
"-g", "-lm",
"-o", Path.Combine(".build", "out"),
..objectPaths,
Path.Combine(".build", "runtime.o"),
..objectFileArgs
]));
if (linkProcess == null) return 1;
linkProcess.WaitForExit();
if (linkProcess.ExitCode != 0)
{
Console.Error.WriteLine($"gcc failed with exit code {linkProcess.ExitCode}");
return 1;
}
Console.WriteLine("Build successful: .build/out");
}
else
{
Console.WriteLine("No main function found in module main, skipping link step");
}
}
else
{
Console.WriteLine("No main function found in module main, skipping link step");
}
return 0;
// var cPaths = new List<string>();
//
// Directory.CreateDirectory(".build");
//
// for (var i = 0; i < nubFiles.Length; i++)
// {
// var file = nubFiles[i];
// var compilationUnit = compilationUnits[i];
//
// var generator = new Generator(compilationUnit);
// var directory = Path.GetDirectoryName(file);
// if (!string.IsNullOrWhiteSpace(directory))
// {
// Directory.CreateDirectory(Path.Combine(".build", directory));
// }
//
// var path = Path.Combine(".build", Path.ChangeExtension(file, "c"));
// File.WriteAllText(path, generator.Emit());
// cPaths.Add(path);
// }
//
// var objectPaths = new List<string>();
//
// foreach (var cPath in cPaths)
// {
// var objectPath = Path.ChangeExtension(cPath, "o");
// using var compileProcess = Process.Start("gcc", [
// "-ffreestanding", "-nostartfiles", "-std=c23",
// "-g", "-lm",
// "-c", "-o", objectPath,
// cPath,
// ]);
//
// compileProcess.WaitForExit();
//
// if (compileProcess.ExitCode != 0)
// {
// Console.Error.WriteLine($"gcc failed with exit code {compileProcess.ExitCode}");
// return 1;
// }
//
// objectPaths.Add(objectPath);
// }
//
// if (modules.TryGetValue("main", out var mainModule))
// {
// var mainFunction = mainModule
// .Functions(true)
// .FirstOrDefault(x => x.Prototype.ExternSymbol == "main");
//
// if (mainFunction is { Prototype.ExternSymbol: not null })
// {
// var runtime = $"""
// .intel_syntax noprefix
//
// .text
// .globl _start
// _start:
// mov rdi, [rsp] # argc
// mov rsi, [rsp + 8] # argv
// call {mainFunction.Prototype.ExternSymbol}
// mov rdi, rax # Move return value into rdi
// mov rax, 60 # syscall: exit
// syscall
//
// """;
//
// var runtimePath = Path.Combine(".build", "runtime.s");
// File.WriteAllText(runtimePath, runtime);
//
// using var assembleProcess = Process.Start(new ProcessStartInfo("as", ["-g", "-c", runtimePath, "-o", Path.Combine(".build", "runtime.o")]));
// if (assembleProcess == null) return 1;
// assembleProcess.WaitForExit();
//
// if (assembleProcess.ExitCode != 0)
// {
// Console.Error.WriteLine($"gcc failed with exit code {assembleProcess.ExitCode}");
// return 1;
// }
//
// if (assembleProcess.ExitCode != 0) return 1;
//
// using var linkProcess = Process.Start(new ProcessStartInfo("gcc", [
// "-ffreestanding", "-nostartfiles", "-std=c23",
// "-g", "-lm",
// "-o", Path.Combine(".build", "out"),
// ..objectPaths,
// Path.Combine(".build", "runtime.o"),
// ..objectFileArgs
// ]));
//
// if (linkProcess == null) return 1;
// linkProcess.WaitForExit();
//
// if (linkProcess.ExitCode != 0)
// {
// Console.Error.WriteLine($"gcc failed with exit code {linkProcess.ExitCode}");
// return 1;
// }
//
// Console.WriteLine("Build successful: .build/out");
// }
// else
// {
// Console.WriteLine("No main function found in module main, skipping link step");
// }
// }
// else
// {
// Console.WriteLine("No main function found in module main, skipping link step");
// }
//
// return 0;