Skip to content

Commit

Permalink
First compile
Browse files Browse the repository at this point in the history
  • Loading branch information
Hachem-H committed Aug 9, 2023
1 parent bd8ec24 commit 8117cdf
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
2 changes: 2 additions & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ iridisSourceFiles = [
'src/main.c'
]


iridisIncludeDirectories = [
include_directories('Vendor/stb'),
include_directories('src'),
Expand All @@ -23,4 +24,5 @@ iridisIncludeDirectories = [
executable('iridis',
sources: iridisSourceFiles,
include_directories: iridisIncludeDirectories,
link_args: ['-lLLVM-15' ]
)
36 changes: 33 additions & 3 deletions src/Backend/Parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include <stdio.h>
#include <stb_ds.h>

#include <llvm-c/ExecutionEngine.h>

static Token* GetToken(Parser* parser)
{
return &parser->tokens[parser->currentTokenIndex];
Expand Down Expand Up @@ -61,7 +63,15 @@ static void HandleIdentifier(Parser* parser)
{
ExpressionAST expression;
expression.procedure.name = strdup(identifierName);
LOG_INFO("Got procedure with name: %s", expression.procedure.name);

LLVMTypeRef returnType = LLVMVoidType();
LLVMTypeRef paramTypes[] = {};

LLVMTypeRef functionType = LLVMFunctionType(returnType, paramTypes, 0, 0);
LLVMValueRef entryPointFunction = LLVMAddFunction(parser->module, expression.procedure.name, functionType);

LLVMBasicBlockRef entryBlock = LLVMAppendBasicBlock(entryPointFunction, "entry");
LLVMPositionBuilderAtEnd(parser->builder, entryBlock);
free(expression.procedure.name);
} break;

Expand All @@ -70,8 +80,10 @@ static void HandleIdentifier(Parser* parser)
{
ExpressionAST expression;
expression.structure.name = strdup(identifierName);
LOG_INFO("Got structure with name: %s", expression.structure.name);
free(expression.procedure.name);

LLVMTypeRef structTypes[] = {};
LLVMStructSetBody(LLVMStructCreateNamed(LLVMGetGlobalContext(), expression.structure.name), structTypes, 0, false);
free(expression.structure.name);
} break;

default:
Expand All @@ -85,11 +97,21 @@ static void HandleIdentifier(Parser* parser)

void Parse(char* sourceCode)
{
// TODO(Hachem): Proper backend
LLVMInitializeX86TargetInfo();
LLVMInitializeX86Target();
LLVMInitializeX86TargetMC();
LLVMInitializeX86AsmParser();
LLVMInitializeX86AsmPrinter();

Parser parser;
parser.tokens = Tokenize(sourceCode);
parser.sourceLines = NULL;
parser.currentTokenIndex = 0;

parser.builder = LLVMCreateBuilder();
parser.module = LLVMModuleCreateWithName("Test");

char* sourceLine = strtok(sourceCode, "\n");
while (sourceLine != NULL)
{
Expand All @@ -104,7 +126,15 @@ void Parse(char* sourceCode)

token = GetNextToken(&parser);
}

char* generatedIR = LLVMPrintModuleToString(parser.module);
printf("%s\n", generatedIR);
LLVMDisposeMessage(generatedIR);

stbds_arrfree(parser.sourceLines);
DestroyTokens(parser.tokens);

LLVMDisposeModule(parser.module);
LLVMDisposeBuilder(parser.builder);
}

5 changes: 5 additions & 0 deletions src/Backend/Parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@

#include "Core/Log.h"

#include <llvm-c/Core.h>

typedef struct Parser_t
{
int currentTokenIndex;
char** sourceLines;
Token* tokens;

LLVMBuilderRef builder;
LLVMModuleRef module;
} Parser;

void Parse(char* sourceCode);

0 comments on commit 8117cdf

Please sign in to comment.