Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: basic code generation #33

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
.idea/
.vscode/

.vscode/

cmake-build-*/

build/
Expand Down
7 changes: 7 additions & 0 deletions include/IR/CallingConv.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#pragma once

#include <napi.h>

namespace CallingConv {
void Init(Napi::Env env, Napi::Object &exports);
}
4 changes: 4 additions & 0 deletions include/IR/Function.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ class Function : public Napi::ObjectWrap<Function> {

Napi::Value getType(const Napi::CallbackInfo &info);

Napi::Value getCallingConv(const Napi::CallbackInfo &info);

void setCallingConv(const Napi::CallbackInfo &info);

void addFnAttr(const Napi::CallbackInfo &info);

void addParamAttr(const Napi::CallbackInfo &info);
Expand Down
66 changes: 66 additions & 0 deletions include/IR/PassManager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#pragma once

#include <napi.h>

#include <llvm/IR/PassManager.h>
#include "llvm/Pass.h"
#include "llvm/Passes/PassBuilder.h"

class ModulePassManager : public Napi::ObjectWrap<ModulePassManager> {
public:
static inline Napi::FunctionReference constructor; // NOLINT

static void Init(Napi::Env env, Napi::Object &exports);

static bool IsClassOf(const Napi::Value &value);

static llvm::ModulePassManager *Extract(const Napi::Value &value);

explicit ModulePassManager(const Napi::CallbackInfo &info);

llvm::ModulePassManager *getLLVMPrimitive();

private:
llvm::ModulePassManager *passManager;

llvm::PassBuilder *passBuilder;
llvm::ModuleAnalysisManager *moduleAnalysisManager;
llvm::LoopAnalysisManager *loopAnalysisManager;
llvm::FunctionAnalysisManager *functionAnalysisManager;
llvm::CGSCCAnalysisManager *cgsccAnalysisManager;

llvm::OptimizationLevel level;

Napi::Value createFunctionPassManager(const Napi::CallbackInfo &info);
void addFunctionPasses(const Napi::CallbackInfo &info);
void addVerifierPass(const Napi::CallbackInfo &info);
Napi::Value isEmpty(const Napi::CallbackInfo &info);

void run(const Napi::CallbackInfo &info);
};

class FunctionPassManager : public Napi::ObjectWrap<FunctionPassManager> {
public:
static inline Napi::FunctionReference constructor; // NOLINT

static void Init(Napi::Env env, Napi::Object &exports);

static Napi::Object New(Napi::Env env, llvm::FunctionPassManager *fpm);

static bool IsClassOf(const Napi::Value &value);

static llvm::FunctionPassManager *Extract(const Napi::Value &value);

explicit FunctionPassManager(const Napi::CallbackInfo &info);

llvm::FunctionPassManager *getLLVMPrimitive();

private:
llvm::FunctionPassManager *passManager = nullptr;

void addSROAPass(const Napi::CallbackInfo &info);
void addEarlyCSEPass(const Napi::CallbackInfo &info);
void addInstCombinePass(const Napi::CallbackInfo &info);

Napi::Value isEmpty(const Napi::CallbackInfo &info);
};
2 changes: 2 additions & 0 deletions include/IR/index.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "IR/GlobalObject.h"
#include "IR/GlobalVariable.h"
#include "IR/Function.h"
#include "IR/CallingConv.h"
#include "IR/Instruction.h"
#include "IR/Instructions.h"
#include "IR/IRBuilder.h"
Expand All @@ -26,5 +27,6 @@
#include "IR/DataLayout.h"
#include "IR/Verifier.h"
#include "IR/Intrinsic.h"
#include "IR/PassManager.h"

void InitIR(Napi::Env env, Napi::Object &exports);
16 changes: 16 additions & 0 deletions include/Passes/OptimizationLevel.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#pragma once

#include <napi.h>

namespace OptimizationLevel {
enum Level {
O0,
O1,
O2,
O3,
Os,
Oz,
};

void Init(Napi::Env env, Napi::Object &exports);
}
7 changes: 7 additions & 0 deletions include/Passes/ThinOrFullLTOPhase.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#pragma once

#include <napi.h>

namespace ThinOrFullLTOPhase {
void Init(Napi::Env env, Napi::Object &exports);
}
7 changes: 7 additions & 0 deletions include/Passes/index.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#pragma once

#include <napi.h>
#include "Passes/OptimizationLevel.h"
#include "Passes/ThinOrFullLTOPhase.h"

void InitPasses(Napi::Env env, Napi::Object &exports);
7 changes: 7 additions & 0 deletions include/Support/CodeGen.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#pragma once

#include <napi.h>

namespace CodeGen {
void Init(Napi::Env env, Napi::Object &exports);
}
1 change: 1 addition & 0 deletions include/Support/index.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
#include <napi.h>
#include "Support/SourceMgr.h"
#include "Support/TargetSelect.h"
#include "Support/CodeGen.h"

void InitSupport(Napi::Env env, Napi::Object &exports);
2 changes: 2 additions & 0 deletions include/Target/TargetMachine.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ class TargetMachine : public Napi::ObjectWrap<TargetMachine> {
const llvm::TargetMachine *targetMachine = nullptr;

Napi::Value createDataLayout(const Napi::CallbackInfo &info);
void emitToFile(const Napi::CallbackInfo &info);
Napi::Value emitToBuffer(const Napi::CallbackInfo &info);
};
26 changes: 23 additions & 3 deletions include/Util/ErrMsg.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace ErrMsg {
namespace Class {
namespace APInt {
constexpr const char *constructor = "APInt.constructor needs to be called with new (numBits: number, value: number, isSigned?: boolean)";
constexpr const char *constructor = "APInt.constructor needs to be called with new (numBits: number, value: number | bigint, isSigned?: boolean)";
}
namespace APFloat {
constexpr const char *constructor = "APFloat.constructor needs to be called with new (value: number)";
Expand Down Expand Up @@ -55,7 +55,8 @@ namespace ErrMsg {
constexpr const char *constructor = "StructType.constructor needs to be called with new (external: Napi::External<llvm::StructType>)";
constexpr const char *create = "StructType.create needs to be called with:"
"\n\t - (context: LLVMContext, name: string)"
"\n\t - (context: LLVMContext, elementTypes: Type[], name: string)";
"\n\t - (context: LLVMContext, elementTypes: Type[], name: string)"
"\n\t - (context: LLVMContext, elementTypes: Type[], name: string, isPacked: boolean = false)";
constexpr const char *get = "StructType.get needs to be called with:"
"\n\t - (context: LLVMContext)"
"\n\t - (context: LLVMContext, elementTypes: Type[])";
Expand Down Expand Up @@ -175,6 +176,7 @@ namespace ErrMsg {
constexpr const char *insertAfter = "Function.insertAfter needs to be called with (where: BasicBlock, basicBlock: BasicBlock)";
constexpr const char *setPersonalityFn = "Function.setPersonalityFn needs to be called with (fn: Constant)";
constexpr const char *setSubprogram = "Function.setSubprogram needs to be called with (subprogram: DISubprogram)";
constexpr const char *setCallingConv = "Function.setCallingConv needs to bu called with (cc: CallingConv)";
constexpr const char *addFnAttr = "Function.addFnAttr needs to be called with"
"\n\t - (kind: Attribute.AttrKind)"
"\n\t - (kind: string, value: string = "")"
Expand Down Expand Up @@ -565,13 +567,31 @@ namespace ErrMsg {
}
namespace Target {
constexpr const char *constructor = "Target.constructor needs to be called with new (external: Napi::External<llvm::Target>)";
constexpr const char *createTargetMachine = "Target.createTargetMachine needs to be called with (targetTriple: string, cpu: string, features?: string)";
constexpr const char *createTargetMachine = "Target.createTargetMachine needs to be called with:"
"\n\t -(targetTriple: string, cpu: string, features?: string)"
"\n\t -(targetTriple: string, cpu: string, features: string, reloc: Reloc)"
"\n\t -(targetTriple: string, cpu: string, features: string, reloc: Reloc, codeModel: CodeModel)"
"\n\t -(targetTriple: string, cpu: string, features: string, reloc: Reloc, codeModel: CodeModel, codeGenOpt: CodeGenOpt)"
"\n\t -(targetTriple: string, cpu: string, features: string, reloc: Reloc, codeModel: CodeModel, codeGenOpt: CodeGenOpt, jit: boolean)";
}
namespace TargetRegistry {
constexpr const char *lookupTarget = "TargetRegistry.lookupTarget needs to be called with (triple: string)";
}
namespace TargetMachine {
constexpr const char *constructor = "TargetMachine.constructor needs to be called with new (external: Napi::External<llvm::TargetMachine>)";
constexpr const char *emitToFile = "TargetMachine.emitToFile needs to be called with (module: Module, path: string, format: CodeGenFileType)";
constexpr const char *emitToBuffer = "TargetMachine.emitToBuffer needs to be called with (module: Module, format: CodeGenFileType)";
constexpr const char *addPassesToEmitFile = "Cannot register passes to emit file. Check registered ASM printers, targets and relevant stuff";
}
namespace ModulePassManager {
constexpr const char *constructor = "ModulePassManager.constructor needs to be called with new (optLevel: OptimizationLevel)";
constexpr const char *createFunctionPassManager = "ModulePassManager.createFunctionPassManager needs to be called with new (ltoPhase: ThinOrFullLTOPhase)";
constexpr const char *addFunctionPasses = "ModulePassManager.addFunctionPasses needs to be called with new (fpm: FunctionPassManager)";
constexpr const char *run = "ModulePassManager.run needs to be called with new (module: Module)";
}
namespace FunctionPassManager {
constexpr const char *constructor = "ModulePassManager.constructor needs to be called with new (external: Napi::External<llvm::TargetMachine>)";

}
}
namespace Namespace::Intrinsic {
Expand Down
Loading