Skip to content

Commit 3357b27

Browse files
committed
Merge remote-tracking branch 'upstream/master'
2 parents a1ab7ba + db59130 commit 3357b27

File tree

12 files changed

+1808
-2065
lines changed

12 files changed

+1808
-2065
lines changed

.travis.yml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ language: node_js
22
node_js:
33
- "11"
44
- "10"
5-
- "9"
65
- "8"
7-
- "7"
86
- "6"
97
cache: yarn
108
env:
@@ -13,13 +11,14 @@ env:
1311
- LLVM_DEB="deb http://apt.llvm.org/trusty/ llvm-toolchain-trusty-5.0 main" LLVM_VERSION=5.0
1412
- LLVM_DEB="deb http://apt.llvm.org/trusty/ llvm-toolchain-trusty-6.0 main" LLVM_VERSION=6.0
1513
- LLVM_DEB="deb http://apt.llvm.org/trusty/ llvm-toolchain-trusty-7 main" LLVM_VERSION=7
16-
- LLVM_DEB="deb http://apt.llvm.org/trusty/ llvm-toolchain-trusty main" LLVM_VERSION=8
14+
- LLVM_DEB="deb http://apt.llvm.org/trusty/ llvm-toolchain-trusty-8 main" LLVM_VERSION=8
15+
- LLVM_DEB="deb http://apt.llvm.org/trusty/ llvm-toolchain-trusty main" LLVM_VERSION=9
1716
before_install:
1817
- sudo add-apt-repository ppa:ubuntu-toolchain-r/test -y
1918
- wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key|sudo apt-key add -
2019
- echo $LLVM_DEB | sudo tee -a /etc/apt/sources.list
2120
- sudo apt-get update -qq
22-
- sudo apt-get install libedit-dev llvm-$LLVM_VERSION llvm-$LLVM_VERSION-dev
21+
- sudo apt-get install libedit-dev llvm-$LLVM_VERSION llvm-$LLVM_VERSION-dev clang-$LLVM_VERSION llvm-$LLVM_VERSION-tools
2322
- npm config set cmake_LLVM_DIR $(llvm-config-$LLVM_VERSION --cmakedir)
2423
deploy:
2524
provider: npm

llvm-node.d.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,22 +201,23 @@ declare namespace llvm {
201201

202202
class ConstantFP extends Constant {
203203
static get(context: LLVMContext, value: number): ConstantFP;
204+
static get(type: Type, value: string): Constant;
204205

205206
static getZeroValueForNegation(type: Type): Constant;
206207

207208
static getNegativeZero(type: Type): Constant;
208209

209210
static getNaN(type: Type): Constant;
210211

211-
static getInfinity(type: Type, negative?: boolean/* = false */): Constant;
212+
static getInfinity(type: Type, negative?: boolean /* = false */): Constant;
212213

213214
private constructor();
214215

215216
readonly value: number;
216217
}
217218

218219
class ConstantInt extends Constant {
219-
static get(context: LLVMContext, value: number, numBits?: number, signed?: boolean): ConstantInt;
220+
static get(context: LLVMContext, value: number | string, numBits?: number, signed?: boolean): ConstantInt;
220221

221222
static getFalse(context: LLVMContext): ConstantInt;
222223

@@ -225,6 +226,8 @@ declare namespace llvm {
225226
private constructor();
226227

227228
readonly value: number;
229+
230+
public toString(): string;
228231
}
229232

230233
class ConstantPointerNull extends Constant {
@@ -707,6 +710,11 @@ declare namespace llvm {
707710
private __marker: number;
708711
}
709712

713+
interface FunctionCallee {
714+
callee: Value;
715+
functionType: FunctionType;
716+
}
717+
710718
class Module {
711719
empty: boolean;
712720
readonly name: string;
@@ -723,7 +731,7 @@ declare namespace llvm {
723731

724732
getFunction(name: string): Function | undefined;
725733

726-
getOrInsertFunction(name: string, functionType: FunctionType): Constant;
734+
getOrInsertFunction(name: string, functionType: FunctionType): FunctionCallee;
727735

728736
getGlobalVariable(name: string, allowInternal?: boolean): GlobalVariable;
729737

package.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,26 @@
1212
},
1313
"engineStrict": true,
1414
"devDependencies": {
15-
"@types/jest": "^23.3.5",
15+
"@types/jest": "^24.0.11",
1616
"@types/requirejs": "^2.1.31",
1717
"generate-changelog": "^1.7.1",
18-
"jest": "^23.6.0",
18+
"jest": "^24.7.1",
1919
"node-gyp": "^3.8.0",
20-
"ts-jest": "^23.10.4",
21-
"typescript": "^3.1.3"
20+
"ts-jest": "^24.0.2",
21+
"typescript": "^3.4.4"
2222
},
2323
"dependencies": {
24-
"bindings": "^1.3.0",
25-
"cmake-js": "^4.0.1",
26-
"nan": "^2.11.1"
24+
"bindings": "^1.5.0",
25+
"cmake-js": "^5.2.0",
26+
"nan": "^2.13.2"
2727
},
2828
"scripts": {
2929
"configure": "cmake-js configure",
3030
"build": "cmake-js compile",
3131
"install": "cmake-js compile",
3232
"clean": "cmake-js clean",
3333
"rebuild": "cmake-js rebuild",
34-
"pretest": "tsc llvm-node.d.ts --strict",
34+
"pretest": "tsc --strict --noEmit --types . llvm-node.d.ts ",
3535
"test": "jest --ci --coverage --runInBand",
3636
"test:watch": "jest --watchAll",
3737
"release:major": "changelog -M && git add CHANGELOG.md && git commit -m 'updated CHANGELOG.md' && npm version major && git push origin && git push origin --tags",

src/ir/constant-fp.cc

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
#include "llvm-context.h"
77
#include "type.h"
88

9+
#include "../util/string.h"
10+
11+
912
NAN_MODULE_INIT(ConstantFPWrapper::Init) {
1013
auto constantFp = Nan::GetFunction(Nan::New(constantFpTemplate())).ToLocalChecked();
1114
Nan::Set(target, Nan::New("ConstantFP").ToLocalChecked(), constantFp);
@@ -28,16 +31,22 @@ NAN_METHOD(ConstantFPWrapper::New) {
2831
}
2932

3033
NAN_METHOD(ConstantFPWrapper::get) {
31-
if (info.Length() != 2 || !LLVMContextWrapper::isInstance(info[0]) || !info[1]->IsNumber()) {
32-
return Nan::ThrowTypeError("get needs to be called with: context: LLVMContext, value: number");
33-
}
34-
34+
if (info.Length() == 2 && LLVMContextWrapper::isInstance(info[0]) && info[1]->IsNumber()) {
3535
auto& context = LLVMContextWrapper::FromValue(info[0])->getContext();
3636
double number = Nan::To<double>(info[1]).FromJust();
3737

38-
auto* constant = llvm::ConstantFP::get(context, llvm::APFloat { number } );
38+
auto* constant = llvm::ConstantFP::get(context, llvm::APFloat { number });
3939

40-
info.GetReturnValue().Set(ConstantFPWrapper::of(constant));
40+
return info.GetReturnValue().Set(ConstantFPWrapper::of(constant));
41+
} else if (info.Length() == 2 && TypeWrapper::isInstance(info[0]) && info[1]->IsString()) {
42+
auto type = TypeWrapper::FromValue(info[0])->getType();
43+
auto number = ToString(info[1]);
44+
auto constant = llvm::ConstantFP::get(type, number);
45+
46+
return info.GetReturnValue().Set(ConstantWrapper::of(constant));
47+
}
48+
49+
return Nan::ThrowTypeError("get called with illegal arguments");
4150
}
4251

4352
NAN_METHOD(ConstantFPWrapper::getNaN) {
@@ -95,10 +104,15 @@ NAN_METHOD(ConstantFPWrapper::getNegativeZero) {
95104
}
96105

97106
NAN_GETTER(ConstantFPWrapper::getValueAPF) {
98-
auto* wrapper = ConstantFPWrapper::FromValue(info.Holder());
99-
auto value = wrapper->getConstantFP()->getValueAPF();
107+
auto* wrapper = ConstantFPWrapper::FromValue(info.Holder());
108+
auto fp = wrapper->getConstantFP();
109+
auto value = fp->getValueAPF();
100110

111+
if (fp->getType()->isFloatTy()) {
112+
info.GetReturnValue().Set(Nan::New(value.convertToFloat()));
113+
} else {
101114
info.GetReturnValue().Set(Nan::New(value.convertToDouble()));
115+
}
102116
}
103117

104118
llvm::ConstantFP *ConstantFPWrapper::getConstantFP() {

src/ir/constant-int.cc

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include "constant-int.h"
66
#include "llvm-context.h"
7+
#include "../util/string.h"
78

89
NAN_MODULE_INIT(ConstantIntWrapper::Init) {
910
auto constantInt = Nan::GetFunction(Nan::New(constantIntTemplate())).ToLocalChecked();
@@ -27,15 +28,14 @@ NAN_METHOD(ConstantIntWrapper::New) {
2728
}
2829

2930
NAN_METHOD(ConstantIntWrapper::get) {
30-
if (info.Length() < 2 || !LLVMContextWrapper::isInstance(info[0]) || !info[1]->IsNumber() ||
31+
if (info.Length() < 2 || !LLVMContextWrapper::isInstance(info[0]) || (!info[1]->IsNumber() && !info[1]->IsString()) ||
3132
(info.Length() > 2 && !info[2]->IsNumber() && !info[2]->IsUndefined()) ||
3233
(info.Length() > 3 && !info[3]->IsBoolean()) ||
3334
info.Length() > 4) {
34-
return Nan::ThrowTypeError("get needs to be called with: context: LLVMContext, value: number, numBits = 32, signed= true");
35+
return Nan::ThrowTypeError("get needs to be called with: context: LLVMContext, value: number|string, numBits = 32, signed= true");
3536
}
3637

3738
auto& context = LLVMContextWrapper::FromValue(info[0])->getContext();
38-
int64_t number = Nan::To<int64_t >(info[1]).FromJust();
3939
uint32_t numBits = 32;
4040
bool isSigned = true;
4141

@@ -47,9 +47,17 @@ NAN_METHOD(ConstantIntWrapper::get) {
4747
isSigned = Nan::To<bool>(info[3]).FromJust();
4848
}
4949

50-
auto* constant = llvm::ConstantInt::get(context, llvm::APInt { numBits, static_cast<uint64_t>(number), isSigned } );
50+
if (info[1]->IsString()) {
51+
auto number = ToString(Nan::To<v8::String>(info[1]).ToLocalChecked());
52+
auto* constant = llvm::ConstantInt::get(context, llvm::APInt { numBits, llvm::StringRef(number.c_str()), 10 } );
5153

52-
info.GetReturnValue().Set(ConstantIntWrapper::of(constant));
54+
info.GetReturnValue().Set(ConstantIntWrapper::of(constant));
55+
} else {
56+
int64_t number = Nan::To<int64_t>(info[1]).FromJust();
57+
auto* constant = llvm::ConstantInt::get(context, llvm::APInt { numBits, static_cast<uint64_t>(number), isSigned } );
58+
59+
info.GetReturnValue().Set(ConstantIntWrapper::of(constant));
60+
}
5361
}
5462

5563
NAN_METHOD(ConstantIntWrapper::getTrue) {
@@ -74,9 +82,18 @@ NAN_METHOD(ConstantIntWrapper::getFalse) {
7482
info.GetReturnValue().Set(ConstantIntWrapper::of(constant));
7583
}
7684

85+
NAN_METHOD(ConstantIntWrapper::toString) {
86+
auto* wrapper = ConstantIntWrapper::FromValue(info.Holder());
87+
auto constantInt = wrapper->getConstantInt();
88+
auto value = constantInt->getValue();
89+
90+
info.GetReturnValue().Set(Nan::New<v8::String>(value.toString(10, true)).ToLocalChecked());
91+
}
92+
7793
NAN_GETTER(ConstantIntWrapper::getValueApf) {
7894
auto* wrapper = ConstantIntWrapper::FromValue(info.Holder());
79-
auto value = wrapper->getConstantInt()->getValue();
95+
auto constantInt = wrapper->getConstantInt();
96+
auto value = constantInt->getValue();
8097

8198
info.GetReturnValue().Set(Nan::New(value.signedRoundToDouble()));
8299
}
@@ -106,6 +123,7 @@ Nan::Persistent<v8::FunctionTemplate>& ConstantIntWrapper::constantIntTemplate()
106123
Nan::SetMethod(localTemplate, "get", ConstantIntWrapper::get);
107124
Nan::SetMethod(localTemplate, "getFalse", ConstantIntWrapper::getFalse);
108125
Nan::SetMethod(localTemplate, "getTrue", ConstantIntWrapper::getTrue);
126+
Nan::SetPrototypeMethod(localTemplate, "toString", ConstantIntWrapper::toString);
109127
Nan::SetAccessor(localTemplate->InstanceTemplate(), Nan::New("value").ToLocalChecked(), ConstantIntWrapper::getValueApf);
110128

111129
functionTemplate.Reset(localTemplate);

src/ir/constant-int.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class ConstantIntWrapper: public ConstantWrapper, public FromValueMixin<Constant
2828
static NAN_METHOD(get);
2929
static NAN_METHOD(getTrue);
3030
static NAN_METHOD(getFalse);
31+
static NAN_METHOD(toString);
3132

3233
// instance
3334
static NAN_GETTER(getValueApf);

src/ir/module.cc

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,19 @@ NAN_METHOD(ModuleWrapper::getOrInsertFunction) {
9393
std::string name = ToString(info[0]);
9494
auto* fnType = FunctionTypeWrapper::FromValue(info[1])->getFunctionType();
9595

96-
info.GetReturnValue().Set(ConstantWrapper::of(module->getOrInsertFunction(name, fnType)));
96+
auto functionCallee = Nan::New<v8::Object>();
97+
98+
#if LLVM_VERSION_MAJOR < 9
99+
Nan::Set(functionCallee, Nan::New("callee").ToLocalChecked(), ConstantWrapper::of(module->getOrInsertFunction(name, fnType)));
100+
Nan::Set(functionCallee, Nan::New("functionType").ToLocalChecked(), FunctionTypeWrapper::of(fnType));
101+
#else
102+
auto llvmCallee = module->getOrInsertFunction(name, fnType);
103+
Nan::Set(functionCallee, Nan::New("callee").ToLocalChecked(), ValueWrapper::of(llvmCallee.getCallee()));
104+
Nan::Set(functionCallee, Nan::New("functionType").ToLocalChecked(), FunctionTypeWrapper::of(llvmCallee.getFunctionType()));
105+
#endif
106+
107+
Nan::EscapableHandleScope scope {};
108+
info.GetReturnValue().Set(scope.Escape(functionCallee));
97109
}
98110

99111
NAN_METHOD(ModuleWrapper::getGlobalVariable) {

src/ir/value.cc

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -144,28 +144,17 @@ v8::Local<v8::Object> ValueWrapper::of(llvm::Value *value)
144144
{
145145
v8::Local<v8::Object> result{};
146146

147-
if (llvm::Constant::classof(value))
148-
{
147+
if (llvm::Constant::classof(value)) {
149148
result = ConstantWrapper::of(static_cast<llvm::Constant *>(value));
150-
}
151-
else if (llvm::BasicBlock::classof(value))
152-
{
149+
} else if (llvm::BasicBlock::classof(value)) {
153150
result = BasicBlockWrapper::of(static_cast<llvm::BasicBlock *>(value));
154-
}
155-
else if (llvm::PHINode::classof(value))
156-
{
151+
} else if (llvm::PHINode::classof(value)) {
157152
result = PhiNodeWrapper::of(static_cast<llvm::PHINode *>(value));
158-
}
159-
else if (llvm::AllocaInst::classof(value))
160-
{
153+
} else if (llvm::AllocaInst::classof(value)) {
161154
result = AllocaInstWrapper::of(static_cast<llvm::AllocaInst *>(value));
162-
}
163-
else if (llvm::CallInst::classof(value))
164-
{
155+
} else if (llvm::CallInst::classof(value)) {
165156
result = CallInstWrapper::of(static_cast<llvm::CallInst *>(value));
166-
}
167-
else
168-
{
157+
} else {
169158
auto constructorFunction = Nan::GetFunction(Nan::New(valueTemplate())).ToLocalChecked();
170159
v8::Local<v8::Value> args[1] = {Nan::New<v8::External>(value)};
171160
result = Nan::NewInstance(constructorFunction, 1, args).ToLocalChecked();

test/ir/constant-fp.spec.ts

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,29 @@ describe("ir/constant-fp", () => {
2222

2323
describe("get", () => {
2424
const untypedGet = llvm.ConstantFP.get as any;
25-
it("creates a new float constant", () => {
25+
it("creates a new double constant", () => {
2626
const value = llvm.ConstantFP.get(context, 10);
2727

2828
expect(value.value).toBe(10);
2929
});
3030

31+
it("creates a new fp of the given type", () => {
32+
const floatType = llvm.Type.getFloatTy(context);
33+
34+
const value = llvm.ConstantFP.get(floatType, "3");
35+
36+
expect(value.type).toEqual(floatType);
37+
expect(value).toBeInstanceOf(llvm.ConstantFP);
38+
expect((value as llvm.ConstantFP).value).toBe(3);
39+
});
40+
3141
it("throws if not called with a context as first argument", () => {
32-
expect(() => untypedGet()).toThrowError("get needs to be called with: context: LLVMContext, value: number");
33-
expect(() => untypedGet({}).toThrowError("get needs to be called with: context: LLVMContext, value: number"));
42+
expect(() => untypedGet()).toThrowError("get called with illegal arguments");
43+
expect(() => untypedGet({}).toThrowError("get called with illegal arguments"));
3444
});
3545

3646
it("throws if not called with a number as second argument", () => {
37-
expect(() => untypedGet(context, "test")).toThrowError(
38-
"get needs to be called with: context: LLVMContext, value: number"
39-
);
47+
expect(() => untypedGet(context, "test")).toThrowError("get called with illegal arguments");
4048
});
4149
});
4250

@@ -86,5 +94,11 @@ describe("ir/constant-fp", () => {
8694

8795
expect(value.value).toBe(10);
8896
});
97+
98+
it("returns NaN for a NaN value", () => {
99+
const value = llvm.ConstantFP.get(context, NaN);
100+
101+
expect(value.value).toBeNaN();
102+
});
89103
});
90104
});

test/ir/constant-int.spec.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,26 @@ describe("ir/constant-int", () => {
1616
expect(intValue.type).toEqual(llvm.Type.getInt32Ty(context));
1717
});
1818

19+
it("returns the int64 value", () => {
20+
const intValue = llvm.ConstantInt.get(context, '9223372036854775807', 64);
21+
22+
expect(intValue.toString()).toBe('9223372036854775807');
23+
expect(intValue.type).toEqual(llvm.Type.getInt64Ty(context));
24+
});
25+
26+
27+
it("returns the negative int64 value", () => {
28+
const intValue = llvm.ConstantInt.get(context, '-9223372036854775807', 64);
29+
30+
expect(intValue.toString()).toBe('-9223372036854775807');
31+
expect(intValue.type).toEqual(llvm.Type.getInt64Ty(context));
32+
});
33+
1934
it("returns the int value with the specified number of bits", () => {
20-
const intValue = llvm.ConstantInt.get(context, 10, 64);
35+
const intValue = llvm.ConstantInt.get(context, 10, 32);
2136

2237
expect(intValue.value).toBe(10);
23-
expect(intValue.type).toEqual(llvm.Type.getInt64Ty(context));
38+
expect(intValue.type).toEqual(llvm.Type.getInt32Ty(context));
2439
});
2540
});
2641

0 commit comments

Comments
 (0)