From 8d9c8502069d689f973a40b6de64018b97dc2048 Mon Sep 17 00:00:00 2001
From: Pasha Bibko <156938226+PashaBibko@users.noreply.github.com>
Date: Sat, 3 May 2025 21:48:16 +0100
Subject: [PATCH] COMPILES TO EXE
---
IR-Generator/src/Generator.cpp | 2 -
LX-Build/CommandProcess.cs | 47 +++++++++++
LX-Build/LX-API.cs | 3 +-
LX-Build/Main.cs | 45 ++++++++++-
LX-Compiler.sln | 18 -----
Linker/Linker.vcxproj | 144 ---------------------------------
Linker/Linker.vcxproj.filters | 13 ---
Linker/Linker.vcxproj.user | 4 -
8 files changed, 92 insertions(+), 184 deletions(-)
create mode 100644 LX-Build/CommandProcess.cs
delete mode 100644 Linker/Linker.vcxproj
delete mode 100644 Linker/Linker.vcxproj.filters
delete mode 100644 Linker/Linker.vcxproj.user
diff --git a/IR-Generator/src/Generator.cpp b/IR-Generator/src/Generator.cpp
index 7d73367..46d1911 100644
--- a/IR-Generator/src/Generator.cpp
+++ b/IR-Generator/src/Generator.cpp
@@ -35,8 +35,6 @@ namespace LX
}
}
-//int main(int argc, char** argv)
-
extern "C" int __declspec(dllexport) GenIR(const char* arg1, const char* arg2, const char* arg3)
{
// Creates the file paths outside of the try-catch so they can be used in errors //
diff --git a/LX-Build/CommandProcess.cs b/LX-Build/CommandProcess.cs
new file mode 100644
index 0000000..6be0671
--- /dev/null
+++ b/LX-Build/CommandProcess.cs
@@ -0,0 +1,47 @@
+using System;
+using System.Collections.Generic;
+using System.Diagnostics;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace LX_Build
+{
+ internal class CommandProcess
+ {
+ private readonly string m_ErrorMessage;
+ private readonly int m_ExitCode;
+
+ public int ExitCode() => m_ExitCode;
+
+ public string Error() => m_ErrorMessage;
+
+ public CommandProcess(string command)
+ {
+ // Creates a process to run the command //
+ ProcessStartInfo info = new()
+ {
+ FileName = "cmd.exe",
+ Arguments = $"/c {command}",
+ RedirectStandardError = true,
+ RedirectStandardOutput = true,
+ UseShellExecute = false,
+ CreateNoWindow = true,
+ };
+
+ // Starts the process //
+ using Process process = Process.Start(info);
+
+ // Reads the streams of the output //
+ string output = process.StandardOutput.ReadToEnd();
+ string error = process.StandardError.ReadToEnd();
+
+ // Waits for the process to exit //
+ process.WaitForExit();
+
+ // Assigns output of the error for external access //
+ m_ExitCode = process.ExitCode;
+ m_ErrorMessage = error;
+ }
+ }
+}
diff --git a/LX-Build/LX-API.cs b/LX-Build/LX-API.cs
index 4b94363..ec80b6b 100644
--- a/LX-Build/LX-API.cs
+++ b/LX-Build/LX-API.cs
@@ -11,7 +11,8 @@ namespace LX_Build
private static partial bool SetDllDirectory(string lpPathName);
// Imports the Frontend of the compiler //
- [LibraryImport("Generator.dll", StringMarshalling = StringMarshalling.Custom, StringMarshallingCustomType = typeof(System.Runtime.InteropServices.Marshalling.AnsiStringMarshaller))]
+ [LibraryImport ("Generator.dll", StringMarshalling = StringMarshalling.Custom,
+ StringMarshallingCustomType = typeof(System.Runtime.InteropServices.Marshalling.AnsiStringMarshaller))]
[UnmanagedCallConv(CallConvs = new Type[] { typeof(System.Runtime.CompilerServices.CallConvCdecl) })]
public static partial int GenIR(string arg1, string arg2, string? arg3);
diff --git a/LX-Build/Main.cs b/LX-Build/Main.cs
index 17c3500..59331a0 100644
--- a/LX-Build/Main.cs
+++ b/LX-Build/Main.cs
@@ -1,16 +1,57 @@
using System;
+using System.Diagnostics;
namespace LX_Build
{
class Program
{
- static void Main(string[] args)
+ static void CompileToObj(string inPath, string outPath)
{
- // Initalises the CPP interface //
+ // The command to compile the IR to .obj //
+ string command = $"llc -filetype=obj -o \"{outPath}\" \"{inPath}\"";
+
+ // Runs the command //
+ CommandProcess process = new(command);
+
+ if (process.ExitCode() == 0)
+ {
+ Console.WriteLine(inPath + " -> " + outPath);
+ return;
+ }
+
+ Console.WriteLine(process.Error());
+ }
+
+ static void LinkToExe(string objectFile)
+ {
+ // The command to link the object file to an .exe //
+ string command = $"lld-link /OUT:example/Main.exe {objectFile} /ENTRY:main";
+
+ // Runs the command //
+ CommandProcess process = new(command);
+
+ if (process.ExitCode() == 0)
+ {
+ Console.WriteLine(objectFile + " -> " + "Main.exe");
+ return;
+ }
+
+ Console.WriteLine(process.Error());
+ }
+
+ static void Main()
+ {
+ // Initalises the CPP interface, MUST ALWAYS BE CALLED FIRST //
LX_API.Init();
// Generates LLVM IR with the example files //
_ = LX_API.GenIR("example/main.lx", "example/main.ll", "example/log");
+
+ // Compilers the LLVM IR to an object file using the command line //
+ CompileToObj("example/main.ll", "example/main.obj");
+
+ // Links the object file to an .exe //
+ LinkToExe("example/main.obj");
}
}
}
diff --git a/LX-Compiler.sln b/LX-Compiler.sln
index 8f58eb1..9c593f4 100644
--- a/LX-Compiler.sln
+++ b/LX-Compiler.sln
@@ -15,13 +15,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Generator", "IR-Generator\I
{D6EAFB31-4AFD-4989-9522-D6609AC4ED64} = {D6EAFB31-4AFD-4989-9522-D6609AC4ED64}
EndProjectSection
EndProject
-Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Linker", "Linker\Linker.vcxproj", "{043AFDA7-5AC5-4DD2-ACA8-029051E72961}"
-EndProject
-Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Backend-Modules", "Backend-Modules", "{E45BA655-FA9A-412B-BCD8-7F85E94859B8}"
-EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LX-Build", "LX-Build\LX-Build.csproj", "{DE9DB560-F002-4922-98E6-558C3D04C607}"
ProjectSection(ProjectDependencies) = postProject
- {043AFDA7-5AC5-4DD2-ACA8-029051E72961} = {043AFDA7-5AC5-4DD2-ACA8-029051E72961}
{C88042E2-0E09-4383-93F8-C79F9EE1E897} = {C88042E2-0E09-4383-93F8-C79F9EE1E897}
EndProjectSection
EndProject
@@ -71,18 +66,6 @@ Global
{C88042E2-0E09-4383-93F8-C79F9EE1E897}.Release|x64.Build.0 = Release|x64
{C88042E2-0E09-4383-93F8-C79F9EE1E897}.Release|x86.ActiveCfg = Release|Win32
{C88042E2-0E09-4383-93F8-C79F9EE1E897}.Release|x86.Build.0 = Release|Win32
- {043AFDA7-5AC5-4DD2-ACA8-029051E72961}.Debug|Any CPU.ActiveCfg = Debug|x64
- {043AFDA7-5AC5-4DD2-ACA8-029051E72961}.Debug|Any CPU.Build.0 = Debug|x64
- {043AFDA7-5AC5-4DD2-ACA8-029051E72961}.Debug|x64.ActiveCfg = Debug|x64
- {043AFDA7-5AC5-4DD2-ACA8-029051E72961}.Debug|x64.Build.0 = Debug|x64
- {043AFDA7-5AC5-4DD2-ACA8-029051E72961}.Debug|x86.ActiveCfg = Debug|Win32
- {043AFDA7-5AC5-4DD2-ACA8-029051E72961}.Debug|x86.Build.0 = Debug|Win32
- {043AFDA7-5AC5-4DD2-ACA8-029051E72961}.Release|Any CPU.ActiveCfg = Release|x64
- {043AFDA7-5AC5-4DD2-ACA8-029051E72961}.Release|Any CPU.Build.0 = Release|x64
- {043AFDA7-5AC5-4DD2-ACA8-029051E72961}.Release|x64.ActiveCfg = Release|x64
- {043AFDA7-5AC5-4DD2-ACA8-029051E72961}.Release|x64.Build.0 = Release|x64
- {043AFDA7-5AC5-4DD2-ACA8-029051E72961}.Release|x86.ActiveCfg = Release|Win32
- {043AFDA7-5AC5-4DD2-ACA8-029051E72961}.Release|x86.Build.0 = Release|Win32
{DE9DB560-F002-4922-98E6-558C3D04C607}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DE9DB560-F002-4922-98E6-558C3D04C607}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DE9DB560-F002-4922-98E6-558C3D04C607}.Debug|x64.ActiveCfg = Debug|Any CPU
@@ -103,7 +86,6 @@ Global
{4E4019F5-12E0-4EE2-9658-A0DD3038EEDA} = {02EA681E-C7D8-13C7-8484-4AC65E1B71E8}
{D6EAFB31-4AFD-4989-9522-D6609AC4ED64} = {02EA681E-C7D8-13C7-8484-4AC65E1B71E8}
{C88042E2-0E09-4383-93F8-C79F9EE1E897} = {02EA681E-C7D8-13C7-8484-4AC65E1B71E8}
- {043AFDA7-5AC5-4DD2-ACA8-029051E72961} = {E45BA655-FA9A-412B-BCD8-7F85E94859B8}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {411AFEC9-4075-4FCC-B95A-9288BC822E90}
diff --git a/Linker/Linker.vcxproj b/Linker/Linker.vcxproj
deleted file mode 100644
index 28a26e2..0000000
--- a/Linker/Linker.vcxproj
+++ /dev/null
@@ -1,144 +0,0 @@
-
-
-
-
- Debug
- Win32
-
-
- Release
- Win32
-
-
- Debug
- x64
-
-
- Release
- x64
-
-
-
- 17.0
- Win32Proj
- {043afda7-5ac5-4dd2-aca8-029051e72961}
- Linker
- 10.0
-
-
-
- Application
- true
- v143
- Unicode
-
-
- Application
- false
- v143
- true
- Unicode
-
-
- Application
- true
- v143
- Unicode
-
-
- Application
- false
- v143
- true
- Unicode
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- $(SolutionDir)bin\$(Platform)\$(Configuration)\
- $(SolutionDir)bin\inter\$(Platform)\$(Configuration)\$(ProjectName)\
-
-
- $(SolutionDir)bin\$(Platform)\$(Configuration)\
- $(SolutionDir)bin\inter\$(Platform)\$(Configuration)\$(ProjectName)\
-
-
-
- Level3
- true
- WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)
- true
-
-
- Console
- true
-
-
-
-
- Level3
- true
- true
- true
- WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)
- true
-
-
- Console
- true
- true
- true
-
-
-
-
- Level3
- true
- _DEBUG;_CONSOLE;%(PreprocessorDefinitions)
- true
- stdcpp20
-
-
- Console
- true
-
-
-
-
- Level3
- true
- true
- true
- NDEBUG;_CONSOLE;%(PreprocessorDefinitions)
- true
- stdcpp20
-
-
- Console
- true
- true
- true
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/Linker/Linker.vcxproj.filters b/Linker/Linker.vcxproj.filters
deleted file mode 100644
index ce609f0..0000000
--- a/Linker/Linker.vcxproj.filters
+++ /dev/null
@@ -1,13 +0,0 @@
-
-
-
-
- {4FC737F1-C7A5-4376-A066-2A32D752A2FF}
- cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx
-
-
- {93995380-89BD-4b04-88EB-625FBE52EBFB}
- h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd
-
-
-
\ No newline at end of file
diff --git a/Linker/Linker.vcxproj.user b/Linker/Linker.vcxproj.user
deleted file mode 100644
index 88a5509..0000000
--- a/Linker/Linker.vcxproj.user
+++ /dev/null
@@ -1,4 +0,0 @@
-
-
-
-
\ No newline at end of file