Skip to content

Commit

Permalink
feat: supplement more features
Browse files Browse the repository at this point in the history
  • Loading branch information
ApsarasX committed Mar 26, 2021
1 parent 9afc7c0 commit ca4db3a
Show file tree
Hide file tree
Showing 25 changed files with 625 additions and 224 deletions.
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
## 0.0.2 (2021-03-26)

### Features

- add Class Argument
- add ConstantFP.getNaN
- add Function.arg_size and Function.getArg
- delete Class FunctionCallee
- supplement more APIs of Class IRBuilder
- add more test cases
- add type signature for new APIs


## 0.0.1 (2021-03-26)

This is the first release of `llvm-bindings`.
Expand Down
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ set(
"src/IR/GlobalObject.cpp"
"src/IR/GlobalVariable.cpp"
"src/IR/Function.cpp"
"src/IR/Argument.cpp"
"src/IR/BasicBlock.cpp"
"src/IR/FunctionCallee.cpp"
"src/IR/DataLayout.cpp"
"src/IR/IRBuilder.cpp"
"src/IR/Verifier.cpp"
Expand Down
28 changes: 28 additions & 0 deletions include/IR/Argument.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#pragma once

#include <napi.h>
#include <llvm/IR/Argument.h>

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

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

static Napi::Object New(Napi::Env env, llvm::Argument *argument);

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

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

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

llvm::Argument *getLLVMPrimitive();

private:
llvm::Argument *argument = nullptr;

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

Napi::Value getArgNo(const Napi::CallbackInfo &info);
};
2 changes: 2 additions & 0 deletions include/IR/ConstantFP.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,6 @@ class ConstantFP : public Napi::ObjectWrap<ConstantFP> {
llvm::ConstantFP *constantFP = nullptr;

static Napi::Value get(const Napi::CallbackInfo &info);

static Napi::Value getNaN(const Napi::CallbackInfo &info);
};
4 changes: 0 additions & 4 deletions include/IR/ConstantInt.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,4 @@ class ConstantInt : public Napi::ObjectWrap<ConstantInt> {
llvm::ConstantInt *constantInt = nullptr;

static Napi::Value get(const Napi::CallbackInfo &info);

static Napi::Value getTrue(const Napi::CallbackInfo &info);

static Napi::Value getFalse(const Napi::CallbackInfo &info);
};
4 changes: 4 additions & 0 deletions include/IR/Function.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,8 @@ class Function : public Napi::ObjectWrap<Function> {
llvm::Function *function = nullptr;

static Napi::Value create(const Napi::CallbackInfo &info);

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

Napi::Value getArg(const Napi::CallbackInfo &info);
};
26 changes: 0 additions & 26 deletions include/IR/FunctionCallee.h

This file was deleted.

78 changes: 71 additions & 7 deletions include/IR/IRBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,37 @@
#include <napi.h>
#include <llvm/IR/IRBuilder.h>
#include "IR/Value.h"
#include "IR/ConstantInt.h"
#include "IR/Type.h"
#include "IR/IntegerType.h"

#define binOpFactoryMacro(binOpFuncType, extraArgs...) \
template<binOpFuncType method> \
Napi::Value binOpFactory(const Napi::CallbackInfo &info) { \
Napi::Env env = info.Env(); \
size_t argsLen = info.Length(); \
bool flag = argsLen >= 2 && Value::IsClassOf(info[0]) && Value::IsClassOf(info[1]); \
if (argsLen >= 3 && flag && info[2].IsString() || argsLen == 2 && flag) { \
size_t argsLen = info.Length(); \
if(argsLen >= 2 && Value::IsClassOf(info[0]) && Value::IsClassOf(info[1])) { \
llvm::Value *lhs = Value::Extract(info[0]); \
llvm::Value *rhs = Value::Extract(info[1]); \
const std::string &name = argsLen >= 3 ? info[2].As<Napi::String>().Utf8Value() : ""; \
llvm::Value *result = (builder->*method)( lhs, rhs, name, ##extraArgs); \
return Value::New(env, result); \
if(argsLen == 2 || argsLen >= 3 && info[2].IsString()) { \
const std::string &name = argsLen >= 3 ? info[2].As<Napi::String>().Utf8Value() : ""; \
llvm::Value *result = (builder->*method)( lhs, rhs, name, ##extraArgs); \
return Value::New(env, result); \
} \
} \
throw Napi::TypeError::New(env, "IRBuilder createBinaryOperation needs to be called with: (lhs: Value, rhs: Value, name?: string)"); \
} \
}

#define getIntFactoryMacro(funcType) \
template<funcType method> \
Napi::Value getIntFactory(const Napi::CallbackInfo &info) { \
Napi::Env env = info.Env(); \
if (info.Length() == 0 || !info[0].IsNumber()) { \
throw Napi::TypeError::New(env, "IRBuilder.#funcType need to be called with (value: number)"); \
} \
unsigned value = info[0].As<Napi::Number>(); \
return ConstantInt::New(env, (builder->*method)(value)); \
}

typedef llvm::IRBuilder<> LLVMIRBuilder;

Expand All @@ -30,6 +45,19 @@ typedef llvm::Value *(llvm::IRBuilderBase::*BinaryOperation)(llvm::Value *, llvm

typedef llvm::Value *(llvm::IRBuilderBase::*BinaryOperationWithBool)(llvm::Value *, llvm::Value *, const llvm::Twine &, bool isExact);

typedef llvm::ConstantInt *(llvm::IRBuilderBase::*GetBoolean)();

typedef llvm::ConstantInt *(llvm::IRBuilderBase::*GetInt8)(uint8_t);

typedef llvm::ConstantInt *(llvm::IRBuilderBase::*GetInt16)(uint16_t);

typedef llvm::ConstantInt *(llvm::IRBuilderBase::*GetInt32)(uint32_t);

typedef llvm::ConstantInt *(llvm::IRBuilderBase::*GetInt64)(uint64_t);

typedef llvm::IntegerType *(llvm::IRBuilderBase::*GetIntType)();

typedef llvm::Type *(llvm::IRBuilderBase::*GetType)();

class IRBuilder : public Napi::ObjectWrap<IRBuilder> {
public:
Expand Down Expand Up @@ -73,4 +101,40 @@ class IRBuilder : public Napi::ObjectWrap<IRBuilder> {
binOpFactoryMacro(BinaryOperation)

binOpFactoryMacro(BinaryOperationWithBool, false)

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


template<GetBoolean method>
Napi::Value getBoolFactory(const Napi::CallbackInfo &info) {
return ConstantInt::New(info.Env(), (builder->*method)());
}

getIntFactoryMacro(GetInt8)

getIntFactoryMacro(GetInt16)

getIntFactoryMacro(GetInt32)

getIntFactoryMacro(GetInt64)

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

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

template<GetIntType method>
Napi::Value getIntTypeFactory(const Napi::CallbackInfo &info) {
return IntegerType::New(info.Env(), (builder->*method)());
}

template<GetType method>
Napi::Value getTypeFactory(const Napi::CallbackInfo &info) {
return Type::New(info.Env(), (builder->*method)());
}

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

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

Napi::Value getIntPtrTy(const Napi::CallbackInfo &info);
};
Loading

0 comments on commit ca4db3a

Please sign in to comment.