Skip to content

Commit

Permalink
Fixes to MIR JIT. JIT executes now 🎊
Browse files Browse the repository at this point in the history
  • Loading branch information
muit committed Feb 5, 2024
1 parent 7924ed5 commit af62f96
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 17 deletions.
9 changes: 9 additions & 0 deletions Examples/Project/stdio.rf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"type": "CStatic",
"count": 1,
"components": {
"CNamespace": {
"0": "stdio"
}
}
}
2 changes: 2 additions & 0 deletions Libs/Backends/MIR/Compiler/Include/MIRBackendModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,7 @@ namespace rift
}

void Build(Compiler& compiler) override;

void PrintBuildFinish(Compiler& compiler) const;
};
} // namespace rift
11 changes: 5 additions & 6 deletions Libs/Backends/MIR/Compiler/Src/C2MIR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,15 @@ namespace rift::MIR
const CMIRModule& module)
{
auto getCode = [](void* data) -> p::i32 {
auto* codeLeft = static_cast<StringView*>(data);
if (codeLeft->size() > -1)
const char*& codeLeft = *static_cast<const char**>(data);
if (*codeLeft == '\0')
{
*codeLeft = Strings::RemoveFromStart(*codeLeft, 1);
return *codeLeft->data();
return EOF;
}
return EOF;
return *(codeLeft++);
};

StringView code = module.code;
const char* code = module.code.data();
if (!c2mir_compile(ctx, &options, getCode, &code, name.Data(), nullptr))
{
compiler.Error("C to MIR compilation failed");
Expand Down
27 changes: 22 additions & 5 deletions Libs/Backends/MIR/Compiler/Src/MIRBackendModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ namespace rift

if (compiler.HasErrors())
{
PrintBuildFinish(compiler);
return;
}

Expand All @@ -260,6 +261,17 @@ namespace rift
}
MIR_load_module(ctx, module);
}

if (!mainFunc)
{
compiler.Error("Main function not found in MIR generated code.");
}
}

if (compiler.HasErrors())
{
PrintBuildFinish(compiler);
return;
}

OpenSTDLibs();
Expand All @@ -283,24 +295,29 @@ namespace rift

p::DateTime startTime = p::DateTime::Now();
p::i32 resultCode = entry(); // Run!
MIR_gen_finish(ctx);

if (compiler.config.verbose)
{
p::Info(" execution -- {:.3f}s\n",
(p::DateTime::Now() - startTime).GetTotalSeconds());
p::Info("exit code: {}\n", resultCode);
}
MIR_gen_finish(ctx);
PrintBuildFinish(compiler);

CloseSTDLibs();
MIR_finish(ctx);
}

void MIRBackend::PrintBuildFinish(Compiler& compiler) const
{
if (!compiler.HasErrors())
{
p::Info("Build complete.");
}
else
{
p::Info("Build failed: {} errors", compiler.GetErrors().Size());
p::Error("Build failed: {} errors", compiler.GetErrors().Size());
}

CloseSTDLibs();
MIR_finish(ctx);
}
} // namespace rift
40 changes: 34 additions & 6 deletions Tools/mir/test/test.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,38 @@

#include <stdbool.h>
// Struct Declarations

void main()
// Function Declarations

// Struct Declarations
typedef struct TestStruct TestStruct;

// Struct Declarations
typedef struct Welcome Welcome;
typedef struct TestClass TestClass;

// Function Declarations
void Project_Main_Main();
void Project_Welcome_Print(long long in1, long long in2);

// Struct Definitions
struct TestStruct
{};

// Struct Definitions
struct Welcome
{};
struct TestClass
{};

// Function Definitions
void Project_Main_Main()
{
printf("hola");
}
void Project_Welcome_Print(long long in1, long long in2) {}

int main()
{
bool a = true;
int q = sizeof(int);
int nn = 2433;
printf("%i", q + nn);
Project_Main_Main();
return 0;
}

0 comments on commit af62f96

Please sign in to comment.