Skip to content

Add ConstantAggregateZero ConstantExpr and a few more methods #71

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

Open
wants to merge 30 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
112c2b8
Add ConstantExpr
vihanb Mar 2, 2018
b67b94b
Add a couple more methods
vihanb Mar 2, 2018
7547c2c
Add ConstantExpr.getBitCast
vihanb Mar 2, 2018
0242e53
Add DataLayout.getTypeAllocSize function
vihanb Mar 7, 2018
9e7208a
Add DataLayout.getTypeAllocSizeInBits
vihanb Mar 7, 2018
e078096
Add IntegerType class.
vihanb Mar 7, 2018
77dafe3
Merge branch 'add-getTypeAllocSize'
vihanb Mar 7, 2018
894dbf2
Add ConstantAggregateZero class
vihanb Mar 8, 2018
5c215c5
Add missing to IR
vihanb Mar 8, 2018
e066518
Add GEP for ConstantExpr
vihanb Mar 11, 2018
b3809c0
Use constant GEP.
vihanb Mar 11, 2018
366bb2e
Add IsNull call
vihanb Mar 22, 2018
fed76a6
Add unsigned remainder to IR builder
vihanb Mar 27, 2018
eac8051
Add casting functions
vihanb Apr 3, 2018
641984f
Merge remote-tracking branch 'upstream/master'
vihanb Oct 27, 2018
55f1f55
Merge remote-tracking branch 'upstream/master'
vihanb Nov 23, 2018
75ebf64
Add createIntToPtr and also address review comments
vihanb Dec 8, 2018
10cbb3a
Merge branch 'master' into master
vihanb Dec 12, 2018
170fc44
Fix tests and also add last method forgot to put
vihanb Dec 12, 2018
cac32ed
Add missing udiv
vihanb Dec 12, 2018
33eacb7
Oops
vihanb Dec 12, 2018
4690747
Fix IRBuilder tests, not sure why they were this way.
vihanb Dec 21, 2018
b45fd26
.bitWidth -> .getBitWidth() and other test fixes
vihanb Dec 21, 2018
a1ab7ba
Add floating point isNaN operation effectively
vihanb Jan 4, 2019
700295a
Add 'createUnreachable'
vihanb Jan 17, 2019
3357b27
Merge remote-tracking branch 'upstream/master'
vihanb May 14, 2019
76fad39
Merge remote-tracking branch 'refs/remotes/origin/master'
vihanb May 14, 2019
35b7014
Merge remote-tracking branch 'upstream/master'
vihanb May 18, 2019
9be0aa7
Add atomicRMW
vihanb Jun 8, 2019
827a1b3
Remove invalid fops for rmw operations.
vihanb Jul 30, 2019
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
76 changes: 74 additions & 2 deletions llvm-node.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,16 @@ declare namespace llvm {
Protected
}

enum AtomicOrdering {
NotAtomic,
Unordered,
Monotonic,
Acquire,
Release,
AcquireRelease,
SequentiallyConsistent
}

class Value {
static MaxAlignmentExponent: number;
static MaximumAlignment: number;
Expand Down Expand Up @@ -173,6 +183,32 @@ declare namespace llvm {
isAllOnesValue(): boolean;
}

class ConstantAggregateZero extends Constant {
static get(type: Type): Constant;

private constructor();
}

class ConstantExpr extends Constant {
static getPointerCast(constant: Constant, type: Type): Constant;

static getIntegerCast(constant: Constant, type: Type): Constant;

static getFPCast(constant: Constant, type: Type): Constant;

static getBitCast(constant: Constant, type: Type): Constant;

static getGetElementPtr(type: Type, constant: Constant, idxList: Constant[], inBounds?: boolean): Constant;

static getOr(constant1: Constant, constant2: Constant): Constant;

static getPointerBitCastOrAddrSpaceCast(constant: Constant, type: Type): Constant;

static getAlignOf(type: Type): Constant;

static getSizeOf(type: Type): Constant;
}

class ConstantFP extends Constant {
static get(context: LLVMContext, value: number): ConstantFP;
static get(type: Type, value: string): Constant;
Expand Down Expand Up @@ -367,7 +403,11 @@ declare namespace llvm {

getTypeStoreSize(type: Type): number;

getIntPtrType(context: LLVMContext, as: number): Type;
getTypeAllocSize(type: Type): number;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't find this method in the LLVM documentation nor that it is implemented in this PR. should this (and the following method be declared on DataLayout instead)?


getTypeAllocSizeInBits(type: Type): number;

getIntPtrType(context: LLVMContext, as: number): IntegerType;
}

class Type {
Expand Down Expand Up @@ -450,6 +490,7 @@ declare namespace llvm {

isArrayTy(): boolean;

isFloatingPointTy(): boolean;
isHalfTy(): boolean;

isPointerTy(): this is PointerType;
Expand All @@ -458,6 +499,7 @@ declare namespace llvm {

getPrimitiveSizeInBits(): number;


toString(): string;
}

Expand Down Expand Up @@ -536,6 +578,8 @@ declare namespace llvm {

createAShr(lhs: Value, rhs: Value, name?: string): Value;

createAtomicRMW(op: AtomicRMWInst.BinOp, ptr: Value, value: Value, ordering: AtomicOrdering): Value;

createBitCast(value: Value, destType: Type, name?: string): Value;

createBr(basicBlock: BasicBlock): Value;
Expand Down Expand Up @@ -583,6 +627,7 @@ declare namespace llvm {
createFSub(lhs: Value, rhs: Value, name?: string): Value;

createFPToSI(value: Value, type: Type, name?: string): Value;
createFPToUI(value: Value, type: Type, name?: string): Value;

createGlobalString(str: string, name?: string, addressSpace?: number): Value;

Expand Down Expand Up @@ -615,6 +660,8 @@ declare namespace llvm {

createICmpULT(lhs: Value, rhs: Value, name?: string): Value;

createIsNull(ptr: Value, name?: string): Value;

createLoad(ptr: Value, name?: string): Value;

createLShr(lhs: Value, rhs: Value, name?: string): Value;
Expand All @@ -633,6 +680,8 @@ declare namespace llvm {

createPtrToInt(value: Value, destType: Type, name?: string): Value;

createIntToPtr(value: Value, destType: Type, name?: string): Value;

createRet(value: Value): Value;

createRetVoid(): Value;
Expand All @@ -653,13 +702,36 @@ declare namespace llvm {

createSRem(lhs: Value, rhs: Value, name?: string): Value;

CreateURem(lhs: Value, rhs: Value, name?: string): Value;
createUDiv(lhs: Value, rhs: Value, name?: string): Value;

createURem(lhs: Value, rhs: Value, name?: string): Value;

createUnreachable(): Value;

createZExt(value: Value, destType: Type, name?: string): Value;

createZExtOrTrunc(value: Value, destType: Type, name?: string): Value;

createSExtOrTrunc(value: Value, destType: Type, name?: string): Value;

getInsertBlock(): BasicBlock | undefined;
}

namespace AtomicRMWInst {
enum BinOp {
Add,
Sub,
And,
Nand,
Or,
Xor,
Max,
Min,
UMax,
UMin
}
}

class LLVMContext {
constructor();

Expand Down
22 changes: 22 additions & 0 deletions src/ir/atomic-rmw-inst.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <llvm/IR/Instructions.h>
#include "atomic-rmw-inst.h"

NAN_MODULE_INIT(InitAtomicRMWInst) {
auto atomicRMWInst = Nan::New<v8::Object>();

auto binOp = Nan::New<v8::Object>();
Nan::Set(binOp, Nan::New("Add").ToLocalChecked(), Nan::New(static_cast<uint32_t>(llvm::AtomicRMWInst::BinOp::Add)));
Nan::Set(binOp, Nan::New("Sub").ToLocalChecked(), Nan::New(static_cast<uint32_t>(llvm::AtomicRMWInst::BinOp::Sub)));
Nan::Set(binOp, Nan::New("And").ToLocalChecked(), Nan::New(static_cast<uint32_t>(llvm::AtomicRMWInst::BinOp::And)));
Nan::Set(binOp, Nan::New("Nand").ToLocalChecked(), Nan::New(static_cast<uint32_t>(llvm::AtomicRMWInst::BinOp::Nand)));
Nan::Set(binOp, Nan::New("Or").ToLocalChecked(), Nan::New(static_cast<uint32_t>(llvm::AtomicRMWInst::BinOp::Or)));
Nan::Set(binOp, Nan::New("Xor").ToLocalChecked(), Nan::New(static_cast<uint32_t>(llvm::AtomicRMWInst::BinOp::Xor)));
Nan::Set(binOp, Nan::New("Max").ToLocalChecked(), Nan::New(static_cast<uint32_t>(llvm::AtomicRMWInst::BinOp::Max)));
Nan::Set(binOp, Nan::New("Min").ToLocalChecked(), Nan::New(static_cast<uint32_t>(llvm::AtomicRMWInst::BinOp::Min)));
Nan::Set(binOp, Nan::New("UMax").ToLocalChecked(), Nan::New(static_cast<uint32_t>(llvm::AtomicRMWInst::BinOp::UMax)));
Nan::Set(binOp, Nan::New("UMin").ToLocalChecked(), Nan::New(static_cast<uint32_t>(llvm::AtomicRMWInst::BinOp::UMin)));

Nan::Set(atomicRMWInst, Nan::New("BinOp").ToLocalChecked(), binOp);

Nan::Set(target, Nan::New("AtomicRMWInst").ToLocalChecked(), atomicRMWInst);
}
8 changes: 8 additions & 0 deletions src/ir/atomic-rmw-inst.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#ifndef LLVM_ATOMIC_RMW_INST_H
#define LLVM_ATOMIC_RMW_INST_H

#include <nan.h>

NAN_MODULE_INIT(InitAtomicRMWInst);

#endif //LLVM_ATOMIC_RMW_INST_H
67 changes: 67 additions & 0 deletions src/ir/constant-aggregate-zero.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#include "constant-aggregate-zero.h"
#include "type.h"

NAN_MODULE_INIT(ConstantAggregateZeroWrapper::Init) {
auto constantAggregateZero = Nan::GetFunction(Nan::New(constantAggregateZeroTemplate())).ToLocalChecked();

Nan::Set(target, Nan::New("ConstantAggregateZero").ToLocalChecked(), constantAggregateZero);
}

v8::Local<v8::Object> ConstantAggregateZeroWrapper::of(llvm::ConstantAggregateZero* constantAggregateZero) {
auto constructor = Nan::GetFunction(Nan::New(constantAggregateZeroTemplate())).ToLocalChecked();
v8::Local<v8::Value> args[1] = { Nan::New<v8::External> (constantAggregateZero) };

auto instance = Nan::NewInstance(constructor, 1, args).ToLocalChecked();

Nan::EscapableHandleScope escapableHandleScope {};
return escapableHandleScope.Escape(instance);
}

llvm::ConstantAggregateZero* ConstantAggregateZeroWrapper::getConstantAggregateZero() {
return static_cast<llvm::ConstantAggregateZero*>(getValue());
}

Nan::Persistent<v8::FunctionTemplate>& ConstantAggregateZeroWrapper::constantAggregateZeroTemplate() {
static Nan::Persistent<v8::FunctionTemplate> functionTemplate {};

if (functionTemplate.IsEmpty()) {
v8::Local<v8::FunctionTemplate> localTemplate = Nan::New<v8::FunctionTemplate>(ConstantAggregateZeroWrapper::New);
localTemplate->SetClassName(Nan::New("ConstantAggregateZero").ToLocalChecked());
localTemplate->InstanceTemplate()->SetInternalFieldCount(1);
localTemplate->Inherit(Nan::New(constantTemplate()));

Nan::SetMethod(localTemplate, "get", ConstantAggregateZeroWrapper::get);

functionTemplate.Reset(localTemplate);
}

return functionTemplate;
}

NAN_METHOD(ConstantAggregateZeroWrapper::New) {
if (!info.IsConstructCall()) {
return Nan::ThrowTypeError("ConstantAggregateZero constructor needs to be called with new");
}

if (!info[0]->IsExternal()) {
return Nan::ThrowTypeError("ConstantAggregateZero constructor needs to be called with: constantAggregateZero: External");
}

auto* constantAggregateZero = static_cast<llvm::ConstantAggregateZero*>(v8::External::Cast(*info[0])->Value());
auto* wrapper = new ConstantAggregateZeroWrapper { constantAggregateZero };
wrapper->Wrap(info.This());

info.GetReturnValue().Set(info.This());
}


NAN_METHOD(ConstantAggregateZeroWrapper::get) {
if (info.Length() != 1 || !TypeWrapper::isInstance(info[0])) {
return Nan::ThrowTypeError("get needs to be called with: type: Type");
}

auto* type = TypeWrapper::FromValue(info[0])->getType();
auto constantAggregateZero = llvm::ConstantAggregateZero::get(type);

info.GetReturnValue().Set(ConstantWrapper::of(constantAggregateZero));
}
25 changes: 25 additions & 0 deletions src/ir/constant-aggregate-zero.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#ifndef LLVM_NODE_CONSTANT_AGGREGATE_ZERO_H
#define LLVM_NODE_CONSTANT_AGGREGATE_ZERO_H

#include <nan.h>
#include <llvm/IR/Constants.h>
#include "constant.h"

class ConstantAggregateZeroWrapper: public ConstantWrapper, public FromValueMixin<ConstantAggregateZeroWrapper> {
public:
static NAN_MODULE_INIT(Init);
static v8::Local<v8::Object> of(llvm::ConstantAggregateZero* constantAggregateZero);
using FromValueMixin<ConstantAggregateZeroWrapper>::FromValue;
llvm::ConstantAggregateZero* getConstantAggregateZero();

private:
explicit ConstantAggregateZeroWrapper(llvm::ConstantAggregateZero* constantAggregateZero) : ConstantWrapper { constantAggregateZero }
{}

static Nan::Persistent<v8::FunctionTemplate>& constantAggregateZeroTemplate();

static NAN_METHOD(New);
static NAN_METHOD(get);
};

#endif //LLVM_NODE_CONSTANT_AGGREGATE_ZERO_H
Loading