]> git.saurik.com Git - apple/javascriptcore.git/blame_incremental - ftl/FTLAbbreviations.h
JavaScriptCore-7601.1.46.3.tar.gz
[apple/javascriptcore.git] / ftl / FTLAbbreviations.h
... / ...
CommitLineData
1/*
2 * Copyright (C) 2013, 2015 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#ifndef FTLAbbreviations_h
27#define FTLAbbreviations_h
28
29#if ENABLE(FTL_JIT)
30
31#include "FTLAbbreviatedTypes.h"
32#include "FTLValueFromBlock.h"
33#include "LLVMAPI.h"
34#include <cstring>
35
36namespace JSC { namespace FTL {
37
38// This file contains short-form calls into the LLVM C API. It is meant to
39// save typing and make the lowering code clearer. If we ever call an LLVM C API
40// function more than once in the FTL lowering code, we should add a shortcut for
41// it here.
42
43#if USE(JSVALUE32_64)
44#error "The FTL backend assumes that pointers are 64-bit."
45#endif
46
47static inline LType voidType(LContext context) { return llvm->VoidTypeInContext(context); }
48static inline LType int1Type(LContext context) { return llvm->Int1TypeInContext(context); }
49static inline LType int8Type(LContext context) { return llvm->Int8TypeInContext(context); }
50static inline LType int16Type(LContext context) { return llvm->Int16TypeInContext(context); }
51static inline LType int32Type(LContext context) { return llvm->Int32TypeInContext(context); }
52static inline LType int64Type(LContext context) { return llvm->Int64TypeInContext(context); }
53static inline LType intPtrType(LContext context) { return llvm->Int64TypeInContext(context); }
54static inline LType floatType(LContext context) { return llvm->FloatTypeInContext(context); }
55static inline LType doubleType(LContext context) { return llvm->DoubleTypeInContext(context); }
56
57static inline LType pointerType(LType type) { return llvm->PointerType(type, 0); }
58static inline LType arrayType(LType type, unsigned count) { return llvm->ArrayType(type, count); }
59static inline LType vectorType(LType type, unsigned count) { return llvm->VectorType(type, count); }
60
61enum PackingMode { NotPacked, Packed };
62static inline LType structType(LContext context, LType* elementTypes, unsigned elementCount, PackingMode packing = NotPacked)
63{
64 return llvm->StructTypeInContext(context, elementTypes, elementCount, packing == Packed);
65}
66static inline LType structType(LContext context, PackingMode packing = NotPacked)
67{
68 return structType(context, 0, 0, packing);
69}
70static inline LType structType(LContext context, LType element1, PackingMode packing = NotPacked)
71{
72 return structType(context, &element1, 1, packing);
73}
74static inline LType structType(LContext context, LType element1, LType element2, PackingMode packing = NotPacked)
75{
76 LType elements[] = { element1, element2 };
77 return structType(context, elements, 2, packing);
78}
79
80// FIXME: Make the Variadicity argument not be the last argument to functionType() so that this function
81// can use C++11 variadic templates
82// https://bugs.webkit.org/show_bug.cgi?id=141575
83enum Variadicity { NotVariadic, Variadic };
84static inline LType functionType(LType returnType, const LType* paramTypes, unsigned paramCount, Variadicity variadicity)
85{
86 return llvm->FunctionType(returnType, const_cast<LType*>(paramTypes), paramCount, variadicity == Variadic);
87}
88template<typename VectorType>
89inline LType functionType(LType returnType, const VectorType& vector, Variadicity variadicity = NotVariadic)
90{
91 return functionType(returnType, vector.begin(), vector.size(), variadicity);
92}
93static inline LType functionType(LType returnType, Variadicity variadicity = NotVariadic)
94{
95 return functionType(returnType, 0, 0, variadicity);
96}
97static inline LType functionType(LType returnType, LType param1, Variadicity variadicity = NotVariadic)
98{
99 return functionType(returnType, &param1, 1, variadicity);
100}
101static inline LType functionType(LType returnType, LType param1, LType param2, Variadicity variadicity = NotVariadic)
102{
103 LType paramTypes[] = { param1, param2 };
104 return functionType(returnType, paramTypes, 2, variadicity);
105}
106static inline LType functionType(LType returnType, LType param1, LType param2, LType param3, Variadicity variadicity = NotVariadic)
107{
108 LType paramTypes[] = { param1, param2, param3 };
109 return functionType(returnType, paramTypes, 3, variadicity);
110}
111static inline LType functionType(LType returnType, LType param1, LType param2, LType param3, LType param4, Variadicity variadicity = NotVariadic)
112{
113 LType paramTypes[] = { param1, param2, param3, param4 };
114 return functionType(returnType, paramTypes, 4, variadicity);
115}
116static inline LType functionType(LType returnType, LType param1, LType param2, LType param3, LType param4, LType param5, Variadicity variadicity = NotVariadic)
117{
118 LType paramTypes[] = { param1, param2, param3, param4, param5 };
119 return functionType(returnType, paramTypes, 5, variadicity);
120}
121static inline LType functionType(LType returnType, LType param1, LType param2, LType param3, LType param4, LType param5, LType param6, Variadicity variadicity = NotVariadic)
122{
123 LType paramTypes[] = { param1, param2, param3, param4, param5, param6 };
124 return functionType(returnType, paramTypes, 6, variadicity);
125}
126
127static inline LType typeOf(LValue value) { return llvm->TypeOf(value); }
128
129static inline LType getElementType(LType value) { return llvm->GetElementType(value); }
130
131static inline unsigned mdKindID(LContext context, const char* string) { return llvm->GetMDKindIDInContext(context, string, std::strlen(string)); }
132static inline LValue mdString(LContext context, const char* string, unsigned length) { return llvm->MDStringInContext(context, string, length); }
133static inline LValue mdString(LContext context, const char* string) { return mdString(context, string, std::strlen(string)); }
134static inline LValue mdNode(LContext context, LValue* args, unsigned numArgs) { return llvm->MDNodeInContext(context, args, numArgs); }
135template<typename VectorType>
136static inline LValue mdNode(LContext context, const VectorType& vector) { return mdNode(context, const_cast<LValue*>(vector.begin()), vector.size()); }
137static inline LValue mdNode(LContext context) { return mdNode(context, 0, 0); }
138static inline LValue mdNode(LContext context, LValue arg1) { return mdNode(context, &arg1, 1); }
139static inline LValue mdNode(LContext context, LValue arg1, LValue arg2)
140{
141 LValue args[] = { arg1, arg2 };
142 return mdNode(context, args, 2);
143}
144static inline LValue mdNode(LContext context, LValue arg1, LValue arg2, LValue arg3)
145{
146 LValue args[] = { arg1, arg2, arg3 };
147 return mdNode(context, args, 3);
148}
149
150static inline void setMetadata(LValue instruction, unsigned kind, LValue metadata) { llvm->SetMetadata(instruction, kind, metadata); }
151
152static inline LValue getFirstInstruction(LBasicBlock block) { return llvm->GetFirstInstruction(block); }
153static inline LValue getNextInstruction(LValue instruction) { return llvm->GetNextInstruction(instruction); }
154
155
156static inline LValue addFunction(LModule module, const char* name, LType type) { return llvm->AddFunction(module, name, type); }
157static inline LValue getNamedFunction(LModule module, const char* name) { return llvm->GetNamedFunction(module, name); }
158static inline LValue getFirstFunction(LModule module) { return llvm->GetFirstFunction(module); }
159static inline LValue getNextFunction(LValue function) { return llvm->GetNextFunction(function); }
160
161static inline void setFunctionCallingConv(LValue function, LCallConv convention) { llvm->SetFunctionCallConv(function, convention); }
162static inline void addTargetDependentFunctionAttr(LValue function, const char* key, const char* value) { llvm->AddTargetDependentFunctionAttr(function, key, value); }
163static inline void removeFunctionAttr(LValue function, LLVMAttribute pa) { llvm->RemoveFunctionAttr(function, pa); }
164
165static inline LLVMLinkage getLinkage(LValue global) { return llvm->GetLinkage(global); }
166static inline void setLinkage(LValue global, LLVMLinkage linkage) { llvm->SetLinkage(global, linkage); }
167static inline void setVisibility(LValue global, LLVMVisibility viz) { llvm->SetVisibility(global, viz); }
168static inline LLVMBool isDeclaration(LValue global) { return llvm->IsDeclaration(global); }
169
170static inline LLVMBool linkModules(LModule dest, LModule str, LLVMLinkerMode mode, char** outMessage) { return llvm->LinkModules(dest, str, mode, outMessage); }
171
172static inline const char * getValueName(LValue global) { return llvm->GetValueName(global); }
173
174static inline LValue getNamedGlobal(LModule module, const char* name) { return llvm->GetNamedGlobal(module, name); }
175static inline LValue getFirstGlobal(LModule module) { return llvm->GetFirstGlobal(module); }
176static inline LValue getNextGlobal(LValue global) { return llvm->GetNextGlobal(global); }
177
178static inline LValue addExternFunction(LModule module, const char* name, LType type)
179{
180 LValue result = addFunction(module, name, type);
181 setLinkage(result, LLVMExternalLinkage);
182 return result;
183}
184
185static inline LLVMBool createMemoryBufferWithContentsOfFile(const char* path, LLVMMemoryBufferRef* outMemBuf, char** outMessage)
186{
187 return llvm->CreateMemoryBufferWithContentsOfFile(path, outMemBuf, outMessage);
188}
189
190
191static inline LLVMBool parseBitcodeInContext(LLVMContextRef contextRef, LLVMMemoryBufferRef memBuf, LModule *outModule, char **outMessage)
192{
193 return llvm->ParseBitcodeInContext(contextRef, memBuf, outModule, outMessage);
194}
195
196
197static inline void disposeMemoryBuffer(LLVMMemoryBufferRef memBuf){ llvm->DisposeMemoryBuffer(memBuf); }
198
199
200static inline LModule moduleCreateWithNameInContext(const char* moduleID, LContext context){ return llvm->ModuleCreateWithNameInContext(moduleID, context); }
201static inline void disposeModule(LModule m){ llvm->DisposeModule(m); }
202
203static inline void disposeMessage(char* outMsg) { llvm->DisposeMessage(outMsg); }
204
205static inline LValue getParam(LValue function, unsigned index) { return llvm->GetParam(function, index); }
206
207static inline void getParamTypes(LType function, LType* dest) { return llvm->GetParamTypes(function, dest); }
208static inline LValue getUndef(LType type) { return llvm->GetUndef(type); }
209
210enum BitExtension { ZeroExtend, SignExtend };
211static inline LValue constInt(LType type, unsigned long long value, BitExtension extension = ZeroExtend) { return llvm->ConstInt(type, value, extension == SignExtend); }
212static inline LValue constReal(LType type, double value) { return llvm->ConstReal(type, value); }
213static inline LValue constIntToPtr(LValue value, LType type) { return llvm->ConstIntToPtr(value, type); }
214static inline LValue constNull(LType type) { return llvm->ConstNull(type); }
215static inline LValue constBitCast(LValue value, LType type) { return llvm->ConstBitCast(value, type); }
216
217static inline LBasicBlock getFirstBasicBlock(LValue function) { return llvm->GetFirstBasicBlock(function); }
218static inline LBasicBlock getNextBasicBlock(LBasicBlock block) { return llvm->GetNextBasicBlock(block); }
219
220static inline LBasicBlock appendBasicBlock(LContext context, LValue function, const char* name = "") { return llvm->AppendBasicBlockInContext(context, function, name); }
221static inline LBasicBlock insertBasicBlock(LContext context, LBasicBlock beforeBasicBlock, const char* name = "") { return llvm->InsertBasicBlockInContext(context, beforeBasicBlock, name); }
222
223static inline LValue buildPhi(LBuilder builder, LType type) { return llvm->BuildPhi(builder, type, ""); }
224static inline void addIncoming(LValue phi, const LValue* values, const LBasicBlock* blocks, unsigned numPredecessors)
225{
226 llvm->AddIncoming(phi, const_cast<LValue*>(values), const_cast<LBasicBlock*>(blocks), numPredecessors);
227}
228static inline void addIncoming(LValue phi, ValueFromBlock value1)
229{
230 LValue value = value1.value();
231 LBasicBlock block = value1.block();
232 addIncoming(phi, &value, &block, 1);
233}
234static inline void addIncoming(LValue phi, ValueFromBlock value1, ValueFromBlock value2)
235{
236 LValue values[] = { value1.value(), value2.value() };
237 LBasicBlock blocks[] = { value1.block(), value2.block() };
238 addIncoming(phi, values, blocks, 2);
239}
240static inline LValue buildPhi(LBuilder builder, LType type, ValueFromBlock value1)
241{
242 LValue result = buildPhi(builder, type);
243 addIncoming(result, value1);
244 return result;
245}
246static inline LValue buildPhi(
247 LBuilder builder, LType type, ValueFromBlock value1, ValueFromBlock value2)
248{
249 LValue result = buildPhi(builder, type);
250 addIncoming(result, value1, value2);
251 return result;
252}
253
254static inline LValue buildAlloca(LBuilder builder, LType type) { return llvm->BuildAlloca(builder, type, ""); }
255static inline LValue buildAdd(LBuilder builder, LValue left, LValue right) { return llvm->BuildAdd(builder, left, right, ""); }
256static inline LValue buildSub(LBuilder builder, LValue left, LValue right) { return llvm->BuildSub(builder, left, right, ""); }
257static inline LValue buildMul(LBuilder builder, LValue left, LValue right) { return llvm->BuildMul(builder, left, right, ""); }
258static inline LValue buildDiv(LBuilder builder, LValue left, LValue right) { return llvm->BuildSDiv(builder, left, right, ""); }
259static inline LValue buildRem(LBuilder builder, LValue left, LValue right) { return llvm->BuildSRem(builder, left, right, ""); }
260static inline LValue buildNeg(LBuilder builder, LValue value) { return llvm->BuildNeg(builder, value, ""); }
261static inline LValue buildFAdd(LBuilder builder, LValue left, LValue right) { return llvm->BuildFAdd(builder, left, right, ""); }
262static inline LValue buildFSub(LBuilder builder, LValue left, LValue right) { return llvm->BuildFSub(builder, left, right, ""); }
263static inline LValue buildFMul(LBuilder builder, LValue left, LValue right) { return llvm->BuildFMul(builder, left, right, ""); }
264static inline LValue buildFDiv(LBuilder builder, LValue left, LValue right) { return llvm->BuildFDiv(builder, left, right, ""); }
265static inline LValue buildFRem(LBuilder builder, LValue left, LValue right) { return llvm->BuildFRem(builder, left, right, ""); }
266static inline LValue buildFNeg(LBuilder builder, LValue value) { return llvm->BuildFNeg(builder, value, ""); }
267static inline LValue buildAnd(LBuilder builder, LValue left, LValue right) { return llvm->BuildAnd(builder, left, right, ""); }
268static inline LValue buildOr(LBuilder builder, LValue left, LValue right) { return llvm->BuildOr(builder, left, right, ""); }
269static inline LValue buildXor(LBuilder builder, LValue left, LValue right) { return llvm->BuildXor(builder, left, right, ""); }
270static inline LValue buildShl(LBuilder builder, LValue left, LValue right) { return llvm->BuildShl(builder, left, right, ""); }
271static inline LValue buildAShr(LBuilder builder, LValue left, LValue right) { return llvm->BuildAShr(builder, left, right, ""); }
272static inline LValue buildLShr(LBuilder builder, LValue left, LValue right) { return llvm->BuildLShr(builder, left, right, ""); }
273static inline LValue buildNot(LBuilder builder, LValue value) { return llvm->BuildNot(builder, value, ""); }
274static inline LValue buildLoad(LBuilder builder, LValue pointer) { return llvm->BuildLoad(builder, pointer, ""); }
275static inline LValue buildStore(LBuilder builder, LValue value, LValue pointer) { return llvm->BuildStore(builder, value, pointer); }
276static inline LValue buildSExt(LBuilder builder, LValue value, LType type) { return llvm->BuildSExt(builder, value, type, ""); }
277static inline LValue buildZExt(LBuilder builder, LValue value, LType type) { return llvm->BuildZExt(builder, value, type, ""); }
278static inline LValue buildFPToSI(LBuilder builder, LValue value, LType type) { return llvm->BuildFPToSI(builder, value, type, ""); }
279static inline LValue buildFPToUI(LBuilder builder, LValue value, LType type) { return llvm->BuildFPToUI(builder, value, type, ""); }
280static inline LValue buildSIToFP(LBuilder builder, LValue value, LType type) { return llvm->BuildSIToFP(builder, value, type, ""); }
281static inline LValue buildUIToFP(LBuilder builder, LValue value, LType type) { return llvm->BuildUIToFP(builder, value, type, ""); }
282static inline LValue buildIntCast(LBuilder builder, LValue value, LType type) { return llvm->BuildIntCast(builder, value, type, ""); }
283static inline LValue buildFPCast(LBuilder builder, LValue value, LType type) { return llvm->BuildFPCast(builder, value, type, ""); }
284static inline LValue buildIntToPtr(LBuilder builder, LValue value, LType type) { return llvm->BuildIntToPtr(builder, value, type, ""); }
285static inline LValue buildPtrToInt(LBuilder builder, LValue value, LType type) { return llvm->BuildPtrToInt(builder, value, type, ""); }
286static inline LValue buildBitCast(LBuilder builder, LValue value, LType type) { return llvm->BuildBitCast(builder, value, type, ""); }
287static inline LValue buildICmp(LBuilder builder, LIntPredicate cond, LValue left, LValue right) { return llvm->BuildICmp(builder, cond, left, right, ""); }
288static inline LValue buildFCmp(LBuilder builder, LRealPredicate cond, LValue left, LValue right) { return llvm->BuildFCmp(builder, cond, left, right, ""); }
289static inline LValue buildInsertElement(LBuilder builder, LValue vector, LValue element, LValue index) { return llvm->BuildInsertElement(builder, vector, element, index, ""); }
290
291enum SynchronizationScope { SingleThread, CrossThread };
292static inline LValue buildFence(LBuilder builder, LAtomicOrdering ordering, SynchronizationScope scope = CrossThread)
293{
294 return llvm->BuildFence(builder, ordering, scope == SingleThread, "");
295}
296
297static inline LValue buildCall(LBuilder builder, LValue function, const LValue* args, unsigned numArgs)
298{
299 return llvm->BuildCall(builder, function, const_cast<LValue*>(args), numArgs, "");
300}
301template<typename VectorType>
302inline LValue buildCall(LBuilder builder, LValue function, const VectorType& vector)
303{
304 return buildCall(builder, function, vector.begin(), vector.size());
305}
306static inline LValue buildCall(LBuilder builder, LValue function)
307{
308 return buildCall(builder, function, 0, 0);
309}
310static inline LValue buildCall(LBuilder builder, LValue function, LValue arg1)
311{
312 return buildCall(builder, function, &arg1, 1);
313}
314template<typename... Args>
315LValue buildCall(LBuilder builder, LValue function, LValue arg1, Args... args)
316{
317 LValue argsArray[] = { arg1, args... };
318 return buildCall(builder, function, argsArray, sizeof(argsArray) / sizeof(LValue));
319}
320
321static inline void setInstructionCallingConvention(LValue instruction, LCallConv callingConvention) { llvm->SetInstructionCallConv(instruction, callingConvention); }
322static inline LValue buildExtractValue(LBuilder builder, LValue aggVal, unsigned index) { return llvm->BuildExtractValue(builder, aggVal, index, ""); }
323static inline LValue buildSelect(LBuilder builder, LValue condition, LValue taken, LValue notTaken) { return llvm->BuildSelect(builder, condition, taken, notTaken, ""); }
324static inline LValue buildBr(LBuilder builder, LBasicBlock destination) { return llvm->BuildBr(builder, destination); }
325static inline LValue buildCondBr(LBuilder builder, LValue condition, LBasicBlock taken, LBasicBlock notTaken) { return llvm->BuildCondBr(builder, condition, taken, notTaken); }
326static inline LValue buildSwitch(LBuilder builder, LValue value, LBasicBlock fallThrough, unsigned numCases) { return llvm->BuildSwitch(builder, value, fallThrough, numCases); }
327static inline void addCase(LValue switchInst, LValue value, LBasicBlock target) { llvm->AddCase(switchInst, value, target); }
328template<typename VectorType>
329static inline LValue buildSwitch(LBuilder builder, LValue value, const VectorType& cases, LBasicBlock fallThrough)
330{
331 LValue result = buildSwitch(builder, value, fallThrough, cases.size());
332 for (unsigned i = 0; i < cases.size(); ++i)
333 addCase(result, cases[i].value(), cases[i].target());
334 return result;
335}
336static inline LValue buildRet(LBuilder builder, LValue value) { return llvm->BuildRet(builder, value); }
337static inline LValue buildUnreachable(LBuilder builder) { return llvm->BuildUnreachable(builder); }
338
339static inline void dumpModule(LModule module) { llvm->DumpModule(module); }
340static inline void verifyModule(LModule module)
341{
342 char* error = 0;
343 llvm->VerifyModule(module, LLVMAbortProcessAction, &error);
344 llvm->DisposeMessage(error);
345}
346
347} } // namespace JSC::FTL
348
349#endif // ENABLE(FTL_JIT)
350
351#endif // FTLAbbreviations_h
352