1 // Copyright 2015 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef V8_COMPILER_CODE_ASSEMBLER_H_
6 #define V8_COMPILER_CODE_ASSEMBLER_H_
7
8 #include <initializer_list>
9 #include <map>
10 #include <memory>
11 #include <sstream>
12
13 // Clients of this interface shouldn't depend on lots of compiler internals.
14 // Do not include anything from src/compiler here!
15 #include "include/cppgc/source-location.h"
16 #include "src/base/macros.h"
17 #include "src/base/optional.h"
18 #include "src/base/type-traits.h"
19 #include "src/builtins/builtins.h"
20 #include "src/codegen/atomic-memory-order.h"
21 #include "src/codegen/code-factory.h"
22 #include "src/codegen/machine-type.h"
23 #include "src/codegen/source-position.h"
24 #include "src/codegen/tnode.h"
25 #include "src/heap/heap.h"
26 #include "src/objects/arguments.h"
27 #include "src/objects/data-handler.h"
28 #include "src/objects/heap-number.h"
29 #include "src/objects/js-array-buffer.h"
30 #include "src/objects/js-collection.h"
31 #include "src/objects/js-proxy.h"
32 #include "src/objects/map.h"
33 #include "src/objects/maybe-object.h"
34 #include "src/objects/object-type.h"
35 #include "src/objects/objects.h"
36 #include "src/objects/oddball.h"
37 #include "src/objects/smi.h"
38 #include "src/objects/tagged-index.h"
39 #include "src/runtime/runtime.h"
40 #include "src/utils/allocation.h"
41 #include "src/zone/zone-containers.h"
42
43 namespace v8 {
44 namespace internal {
45
46 // Forward declarations.
47 class AsmWasmData;
48 class AsyncGeneratorRequest;
49 struct AssemblerOptions;
50 class BigInt;
51 class CallInterfaceDescriptor;
52 class Callable;
53 class Factory;
54 class InterpreterData;
55 class Isolate;
56 class JSAsyncFunctionObject;
57 class JSAsyncGeneratorObject;
58 class JSCollator;
59 class JSCollection;
60 class JSDateTimeFormat;
61 class JSDisplayNames;
62 class JSListFormat;
63 class JSLocale;
64 class JSNumberFormat;
65 class JSPluralRules;
66 class JSRegExpStringIterator;
67 class JSRelativeTimeFormat;
68 class JSSegmentIterator;
69 class JSSegmenter;
70 class JSSegments;
71 class JSV8BreakIterator;
72 class JSWeakCollection;
73 class JSFinalizationRegistry;
74 class JSWeakMap;
75 class JSWeakRef;
76 class JSWeakSet;
77 class ProfileDataFromFile;
78 class PromiseCapability;
79 class PromiseFulfillReactionJobTask;
80 class PromiseReaction;
81 class PromiseReactionJobTask;
82 class PromiseRejectReactionJobTask;
83 class Zone;
84 #define MAKE_FORWARD_DECLARATION(Name) class Name;
85 TORQUE_DEFINED_CLASS_LIST(MAKE_FORWARD_DECLARATION)
86 #undef MAKE_FORWARD_DECLARATION
87
88 template <typename T>
89 class Signature;
90
91 enum class CheckBounds { kAlways, kDebugOnly };
NeedsBoundsCheck(CheckBounds check_bounds)92 inline bool NeedsBoundsCheck(CheckBounds check_bounds) {
93 switch (check_bounds) {
94 case CheckBounds::kAlways:
95 return true;
96 case CheckBounds::kDebugOnly:
97 return DEBUG_BOOL;
98 }
99 }
100
101 enum class StoreToObjectWriteBarrier { kNone, kMap, kFull };
102
103 class AccessCheckNeeded;
104 class BigIntBase;
105 class BigIntWrapper;
106 class ClassBoilerplate;
107 class BooleanWrapper;
108 class CompilationCacheTable;
109 class Constructor;
110 class Filler;
111 class FunctionTemplateRareData;
112 class HeapNumber;
113 class InternalizedString;
114 class JSArgumentsObject;
115 class JSArrayBufferView;
116 class JSContextExtensionObject;
117 class JSError;
118 class JSSloppyArgumentsObject;
119 class MapCache;
120 class NativeContext;
121 class NumberWrapper;
122 class ScriptWrapper;
123 class SloppyArgumentsElements;
124 class StringWrapper;
125 class SymbolWrapper;
126 class Undetectable;
127 class UniqueName;
128 class WasmCapiFunctionData;
129 class WasmTagObject;
130 class WasmExceptionPackage;
131 class WasmExceptionTag;
132 class WasmExportedFunctionData;
133 class WasmGlobalObject;
134 class WasmIndirectFunctionTable;
135 class WasmJSFunctionData;
136 class WasmMemoryObject;
137 class WasmModuleObject;
138 class WasmTableObject;
139
140 template <class T>
141 struct ObjectTypeOf {};
142
143 #define OBJECT_TYPE_CASE(Name) \
144 template <> \
145 struct ObjectTypeOf<Name> { \
146 static const ObjectType value = ObjectType::k##Name; \
147 };
148 #define OBJECT_TYPE_STRUCT_CASE(NAME, Name, name) \
149 template <> \
150 struct ObjectTypeOf<Name> { \
151 static const ObjectType value = ObjectType::k##Name; \
152 };
153 #define OBJECT_TYPE_TEMPLATE_CASE(Name) \
154 template <class... Args> \
155 struct ObjectTypeOf<Name<Args...>> { \
156 static const ObjectType value = ObjectType::k##Name; \
157 };
158 OBJECT_TYPE_CASE(Object)
159 OBJECT_TYPE_CASE(Smi)
160 OBJECT_TYPE_CASE(TaggedIndex)
161 OBJECT_TYPE_CASE(HeapObject)
162 OBJECT_TYPE_LIST(OBJECT_TYPE_CASE)
163 HEAP_OBJECT_ORDINARY_TYPE_LIST(OBJECT_TYPE_CASE)
164 STRUCT_LIST(OBJECT_TYPE_STRUCT_CASE)
165 HEAP_OBJECT_TEMPLATE_TYPE_LIST(OBJECT_TYPE_TEMPLATE_CASE)
166 #undef OBJECT_TYPE_CASE
167 #undef OBJECT_TYPE_STRUCT_CASE
168 #undef OBJECT_TYPE_TEMPLATE_CASE
169
170 #if defined(V8_HOST_ARCH_32_BIT)
171 #define BINT_IS_SMI
172 using BInt = Smi;
173 using AtomicInt64 = PairT<IntPtrT, IntPtrT>;
174 using AtomicUint64 = PairT<UintPtrT, UintPtrT>;
175 #elif defined(V8_HOST_ARCH_64_BIT)
176 #define BINT_IS_INTPTR
177 using BInt = IntPtrT;
178 using AtomicInt64 = IntPtrT;
179 using AtomicUint64 = UintPtrT;
180 #else
181 #error Unknown architecture.
182 #endif
183
184 namespace compiler {
185
186 class CallDescriptor;
187 class CodeAssemblerLabel;
188 class CodeAssemblerVariable;
189 template <class T>
190 class TypedCodeAssemblerVariable;
191 class CodeAssemblerState;
192 class JSGraph;
193 class Node;
194 class RawMachineAssembler;
195 class RawMachineLabel;
196 class SourcePositionTable;
197
198 using CodeAssemblerVariableList = ZoneVector<CodeAssemblerVariable*>;
199
200 using CodeAssemblerCallback = std::function<void()>;
201
202 template <class... Types>
203 class CodeAssemblerParameterizedLabel;
204
205 // This macro alias allows to use PairT<T1, T2> as a macro argument.
206 #define PAIR_TYPE(T1, T2) PairT<T1, T2>
207
208 #define CODE_ASSEMBLER_COMPARE_BINARY_OP_LIST(V) \
209 V(Float32Equal, BoolT, Float32T, Float32T) \
210 V(Float32LessThan, BoolT, Float32T, Float32T) \
211 V(Float32LessThanOrEqual, BoolT, Float32T, Float32T) \
212 V(Float32GreaterThan, BoolT, Float32T, Float32T) \
213 V(Float32GreaterThanOrEqual, BoolT, Float32T, Float32T) \
214 V(Float64Equal, BoolT, Float64T, Float64T) \
215 V(Float64NotEqual, BoolT, Float64T, Float64T) \
216 V(Float64LessThan, BoolT, Float64T, Float64T) \
217 V(Float64LessThanOrEqual, BoolT, Float64T, Float64T) \
218 V(Float64GreaterThan, BoolT, Float64T, Float64T) \
219 V(Float64GreaterThanOrEqual, BoolT, Float64T, Float64T) \
220 /* Use Word32Equal if you need Int32Equal */ \
221 V(Int32GreaterThan, BoolT, Word32T, Word32T) \
222 V(Int32GreaterThanOrEqual, BoolT, Word32T, Word32T) \
223 V(Int32LessThan, BoolT, Word32T, Word32T) \
224 V(Int32LessThanOrEqual, BoolT, Word32T, Word32T) \
225 /* Use WordEqual if you need IntPtrEqual */ \
226 V(IntPtrLessThan, BoolT, WordT, WordT) \
227 V(IntPtrLessThanOrEqual, BoolT, WordT, WordT) \
228 V(IntPtrGreaterThan, BoolT, WordT, WordT) \
229 V(IntPtrGreaterThanOrEqual, BoolT, WordT, WordT) \
230 /* Use Word32Equal if you need Uint32Equal */ \
231 V(Uint32LessThan, BoolT, Word32T, Word32T) \
232 V(Uint32LessThanOrEqual, BoolT, Word32T, Word32T) \
233 V(Uint32GreaterThan, BoolT, Word32T, Word32T) \
234 V(Uint32GreaterThanOrEqual, BoolT, Word32T, Word32T) \
235 /* Use WordEqual if you need UintPtrEqual */ \
236 V(UintPtrLessThan, BoolT, WordT, WordT) \
237 V(UintPtrLessThanOrEqual, BoolT, WordT, WordT) \
238 V(UintPtrGreaterThan, BoolT, WordT, WordT) \
239 V(UintPtrGreaterThanOrEqual, BoolT, WordT, WordT)
240
241 #define CODE_ASSEMBLER_BINARY_OP_LIST(V) \
242 CODE_ASSEMBLER_COMPARE_BINARY_OP_LIST(V) \
243 V(Float64Add, Float64T, Float64T, Float64T) \
244 V(Float64Sub, Float64T, Float64T, Float64T) \
245 V(Float64Mul, Float64T, Float64T, Float64T) \
246 V(Float64Div, Float64T, Float64T, Float64T) \
247 V(Float64Mod, Float64T, Float64T, Float64T) \
248 V(Float64Atan2, Float64T, Float64T, Float64T) \
249 V(Float64Pow, Float64T, Float64T, Float64T) \
250 V(Float64Max, Float64T, Float64T, Float64T) \
251 V(Float64Min, Float64T, Float64T, Float64T) \
252 V(Float64InsertLowWord32, Float64T, Float64T, Word32T) \
253 V(Float64InsertHighWord32, Float64T, Float64T, Word32T) \
254 V(I8x16Eq, I8x16T, I8x16T, I8x16T) \
255 V(IntPtrAdd, WordT, WordT, WordT) \
256 V(IntPtrSub, WordT, WordT, WordT) \
257 V(IntPtrMul, WordT, WordT, WordT) \
258 V(IntPtrDiv, IntPtrT, IntPtrT, IntPtrT) \
259 V(IntPtrAddWithOverflow, PAIR_TYPE(IntPtrT, BoolT), IntPtrT, IntPtrT) \
260 V(IntPtrSubWithOverflow, PAIR_TYPE(IntPtrT, BoolT), IntPtrT, IntPtrT) \
261 V(Int32Add, Word32T, Word32T, Word32T) \
262 V(Int32AddWithOverflow, PAIR_TYPE(Int32T, BoolT), Int32T, Int32T) \
263 V(Int32Sub, Word32T, Word32T, Word32T) \
264 V(Int32SubWithOverflow, PAIR_TYPE(Int32T, BoolT), Int32T, Int32T) \
265 V(Int32Mul, Word32T, Word32T, Word32T) \
266 V(Int32MulWithOverflow, PAIR_TYPE(Int32T, BoolT), Int32T, Int32T) \
267 V(Int32Div, Int32T, Int32T, Int32T) \
268 V(Int32Mod, Int32T, Int32T, Int32T) \
269 V(Int64Add, Word64T, Word64T, Word64T) \
270 V(Int64Sub, Word64T, Word64T, Word64T) \
271 V(Int64SubWithOverflow, PAIR_TYPE(Int64T, BoolT), Int64T, Int64T) \
272 V(Int64Mul, Word64T, Word64T, Word64T) \
273 V(Int64Div, Int64T, Int64T, Int64T) \
274 V(Int64Mod, Int64T, Int64T, Int64T) \
275 V(WordOr, WordT, WordT, WordT) \
276 V(WordAnd, WordT, WordT, WordT) \
277 V(WordXor, WordT, WordT, WordT) \
278 V(WordRor, WordT, WordT, IntegralT) \
279 V(WordShl, WordT, WordT, IntegralT) \
280 V(WordShr, WordT, WordT, IntegralT) \
281 V(WordSar, WordT, WordT, IntegralT) \
282 V(WordSarShiftOutZeros, WordT, WordT, IntegralT) \
283 V(Word32Or, Word32T, Word32T, Word32T) \
284 V(Word32And, Word32T, Word32T, Word32T) \
285 V(Word32Xor, Word32T, Word32T, Word32T) \
286 V(Word32Ror, Word32T, Word32T, Word32T) \
287 V(Word32Shl, Word32T, Word32T, Word32T) \
288 V(Word32Shr, Word32T, Word32T, Word32T) \
289 V(Word32Sar, Word32T, Word32T, Word32T) \
290 V(Word32SarShiftOutZeros, Word32T, Word32T, Word32T) \
291 V(Word64And, Word64T, Word64T, Word64T) \
292 V(Word64Or, Word64T, Word64T, Word64T) \
293 V(Word64Xor, Word64T, Word64T, Word64T) \
294 V(Word64Shl, Word64T, Word64T, Word64T) \
295 V(Word64Shr, Word64T, Word64T, Word64T) \
296 V(Word64Sar, Word64T, Word64T, Word64T)
297
298 TNode<Float64T> Float64Add(TNode<Float64T> a, TNode<Float64T> b);
299
300 #define CODE_ASSEMBLER_UNARY_OP_LIST(V) \
301 V(Float64Abs, Float64T, Float64T) \
302 V(Float64Acos, Float64T, Float64T) \
303 V(Float64Acosh, Float64T, Float64T) \
304 V(Float64Asin, Float64T, Float64T) \
305 V(Float64Asinh, Float64T, Float64T) \
306 V(Float64Atan, Float64T, Float64T) \
307 V(Float64Atanh, Float64T, Float64T) \
308 V(Float64Cos, Float64T, Float64T) \
309 V(Float64Cosh, Float64T, Float64T) \
310 V(Float64Exp, Float64T, Float64T) \
311 V(Float64Expm1, Float64T, Float64T) \
312 V(Float64Log, Float64T, Float64T) \
313 V(Float64Log1p, Float64T, Float64T) \
314 V(Float64Log2, Float64T, Float64T) \
315 V(Float64Log10, Float64T, Float64T) \
316 V(Float64Cbrt, Float64T, Float64T) \
317 V(Float64Neg, Float64T, Float64T) \
318 V(Float64Sin, Float64T, Float64T) \
319 V(Float64Sinh, Float64T, Float64T) \
320 V(Float64Sqrt, Float64T, Float64T) \
321 V(Float64Tan, Float64T, Float64T) \
322 V(Float64Tanh, Float64T, Float64T) \
323 V(Float64ExtractLowWord32, Uint32T, Float64T) \
324 V(Float64ExtractHighWord32, Uint32T, Float64T) \
325 V(BitcastTaggedToWord, IntPtrT, Object) \
326 V(BitcastTaggedToWordForTagAndSmiBits, IntPtrT, AnyTaggedT) \
327 V(BitcastMaybeObjectToWord, IntPtrT, MaybeObject) \
328 V(BitcastWordToTagged, Object, WordT) \
329 V(BitcastWordToTaggedSigned, Smi, WordT) \
330 V(TruncateFloat64ToFloat32, Float32T, Float64T) \
331 V(TruncateFloat64ToWord32, Uint32T, Float64T) \
332 V(TruncateInt64ToInt32, Int32T, Int64T) \
333 V(ChangeFloat32ToFloat64, Float64T, Float32T) \
334 V(ChangeFloat64ToUint32, Uint32T, Float64T) \
335 V(ChangeFloat64ToUint64, Uint64T, Float64T) \
336 V(ChangeInt32ToFloat64, Float64T, Int32T) \
337 V(ChangeInt32ToInt64, Int64T, Int32T) \
338 V(ChangeUint32ToFloat64, Float64T, Word32T) \
339 V(ChangeUint32ToUint64, Uint64T, Word32T) \
340 V(BitcastInt32ToFloat32, Float32T, Word32T) \
341 V(BitcastFloat32ToInt32, Uint32T, Float32T) \
342 V(RoundFloat64ToInt32, Int32T, Float64T) \
343 V(RoundInt32ToFloat32, Float32T, Int32T) \
344 V(Float64SilenceNaN, Float64T, Float64T) \
345 V(Float64RoundDown, Float64T, Float64T) \
346 V(Float64RoundUp, Float64T, Float64T) \
347 V(Float64RoundTiesEven, Float64T, Float64T) \
348 V(Float64RoundTruncate, Float64T, Float64T) \
349 V(Word32Clz, Int32T, Word32T) \
350 V(Word64Clz, Int64T, Word64T) \
351 V(Word32Ctz, Int32T, Word32T) \
352 V(Word64Ctz, Int64T, Word64T) \
353 V(Word32Popcnt, Int32T, Word32T) \
354 V(Word64Popcnt, Int64T, Word64T) \
355 V(Word32BitwiseNot, Word32T, Word32T) \
356 V(WordNot, WordT, WordT) \
357 V(Word64Not, Word64T, Word64T) \
358 V(I8x16BitMask, Int32T, I8x16T) \
359 V(I8x16Splat, I8x16T, Int32T) \
360 V(Int32AbsWithOverflow, PAIR_TYPE(Int32T, BoolT), Int32T) \
361 V(Int64AbsWithOverflow, PAIR_TYPE(Int64T, BoolT), Int64T) \
362 V(IntPtrAbsWithOverflow, PAIR_TYPE(IntPtrT, BoolT), IntPtrT) \
363 V(Word32BinaryNot, BoolT, Word32T) \
364 V(StackPointerGreaterThan, BoolT, WordT)
365
366 // A "public" interface used by components outside of compiler directory to
367 // create code objects with TurboFan's backend. This class is mostly a thin
368 // shim around the RawMachineAssembler, and its primary job is to ensure that
369 // the innards of the RawMachineAssembler and other compiler implementation
370 // details don't leak outside of the the compiler directory..
371 //
372 // V8 components that need to generate low-level code using this interface
373 // should include this header--and this header only--from the compiler
374 // directory (this is actually enforced). Since all interesting data
375 // structures are forward declared, it's not possible for clients to peek
376 // inside the compiler internals.
377 //
378 // In addition to providing isolation between TurboFan and code generation
379 // clients, CodeAssembler also provides an abstraction for creating variables
380 // and enhanced Label functionality to merge variable values along paths where
381 // they have differing values, including loops.
382 //
383 // The CodeAssembler itself is stateless (and instances are expected to be
384 // temporary-scoped and short-lived); all its state is encapsulated into
385 // a CodeAssemblerState instance.
386 class V8_EXPORT_PRIVATE CodeAssembler {
387 public:
CodeAssembler(CodeAssemblerState * state)388 explicit CodeAssembler(CodeAssemblerState* state) : state_(state) {}
389 ~CodeAssembler();
390
391 CodeAssembler(const CodeAssembler&) = delete;
392 CodeAssembler& operator=(const CodeAssembler&) = delete;
393
394 static Handle<Code> GenerateCode(CodeAssemblerState* state,
395 const AssemblerOptions& options,
396 const ProfileDataFromFile* profile_data);
397 bool Is64() const;
398 bool Is32() const;
399 bool IsFloat64RoundUpSupported() const;
400 bool IsFloat64RoundDownSupported() const;
401 bool IsFloat64RoundTiesEvenSupported() const;
402 bool IsFloat64RoundTruncateSupported() const;
403 bool IsInt32AbsWithOverflowSupported() const;
404 bool IsInt64AbsWithOverflowSupported() const;
405 bool IsIntPtrAbsWithOverflowSupported() const;
406 bool IsWord32PopcntSupported() const;
407 bool IsWord64PopcntSupported() const;
408 bool IsWord32CtzSupported() const;
409 bool IsWord64CtzSupported() const;
410
411 // Shortened aliases for use in CodeAssembler subclasses.
412 using Label = CodeAssemblerLabel;
413 template <class T>
414 using TVariable = TypedCodeAssemblerVariable<T>;
415 using VariableList = CodeAssemblerVariableList;
416
417 // ===========================================================================
418 // Base Assembler
419 // ===========================================================================
420
421 template <class PreviousType, bool FromTyped>
422 class CheckedNode {
423 public:
424 #ifdef DEBUG
CheckedNode(Node * node,CodeAssembler * code_assembler,const char * location)425 CheckedNode(Node* node, CodeAssembler* code_assembler, const char* location)
426 : node_(node), code_assembler_(code_assembler), location_(location) {}
427 #else
428 CheckedNode(compiler::Node* node, CodeAssembler*, const char*)
429 : node_(node) {}
430 #endif
431
432 template <class A>
433 operator TNode<A>() {
434 static_assert(
435 !std::is_same<A, MaybeObject>::value,
436 "Can't cast to MaybeObject, use explicit conversion functions. ");
437
438 static_assert(types_have_common_values<A, PreviousType>::value,
439 "Incompatible types: this cast can never succeed.");
440 static_assert(std::is_convertible<TNode<A>, TNode<Object>>::value,
441 "Coercion to untagged values cannot be "
442 "checked.");
443 static_assert(
444 !FromTyped ||
445 !std::is_convertible<TNode<PreviousType>, TNode<A>>::value,
446 "Unnecessary CAST: types are convertible.");
447 #ifdef DEBUG
448 if (FLAG_debug_code) {
449 if (std::is_same<PreviousType, MaybeObject>::value) {
450 code_assembler_->GenerateCheckMaybeObjectIsObject(
451 TNode<MaybeObject>::UncheckedCast(node_), location_);
452 }
453 TNode<ExternalReference> function = code_assembler_->ExternalConstant(
454 ExternalReference::check_object_type());
455 code_assembler_->CallCFunction(
456 function, MachineType::AnyTagged(),
457 std::make_pair(MachineType::AnyTagged(), node_),
458 std::make_pair(MachineType::TaggedSigned(),
459 code_assembler_->SmiConstant(
460 static_cast<int>(ObjectTypeOf<A>::value))),
461 std::make_pair(MachineType::AnyTagged(),
462 code_assembler_->StringConstant(location_)));
463 }
464 #endif
465 return TNode<A>::UncheckedCast(node_);
466 }
467
node()468 Node* node() const { return node_; }
469
470 private:
471 Node* node_;
472 #ifdef DEBUG
473 CodeAssembler* code_assembler_;
474 const char* location_;
475 #endif
476 };
477
478 template <class T>
UncheckedCast(Node * value)479 TNode<T> UncheckedCast(Node* value) {
480 return TNode<T>::UncheckedCast(value);
481 }
482 template <class T, class U>
UncheckedCast(TNode<U> value)483 TNode<T> UncheckedCast(TNode<U> value) {
484 static_assert(types_have_common_values<T, U>::value,
485 "Incompatible types: this cast can never succeed.");
486 return TNode<T>::UncheckedCast(value);
487 }
488
489 // ReinterpretCast<T>(v) has the power to cast even when the type of v is
490 // unrelated to T. Use with care.
491 template <class T>
ReinterpretCast(Node * value)492 TNode<T> ReinterpretCast(Node* value) {
493 return TNode<T>::UncheckedCast(value);
494 }
495
496 CheckedNode<Object, false> Cast(Node* value, const char* location = "") {
497 return {value, this, location};
498 }
499
500 template <class T>
501 CheckedNode<T, true> Cast(TNode<T> value, const char* location = "") {
502 return {value, this, location};
503 }
504
505 #ifdef DEBUG
506 #define STRINGIFY(x) #x
507 #define TO_STRING_LITERAL(x) STRINGIFY(x)
508 #define CAST(x) \
509 Cast(x, "CAST(" #x ") at " __FILE__ ":" TO_STRING_LITERAL(__LINE__))
510 #define TORQUE_CAST(x) \
511 ca_.Cast(x, "CAST(" #x ") at " __FILE__ ":" TO_STRING_LITERAL(__LINE__))
512 #else
513 #define CAST(x) Cast(x)
514 #define TORQUE_CAST(x) ca_.Cast(x)
515 #endif
516
517 #ifdef DEBUG
518 void GenerateCheckMaybeObjectIsObject(TNode<MaybeObject> node,
519 const char* location);
520 #endif
521
522 // Constants.
523 TNode<Int32T> Int32Constant(int32_t value);
524 TNode<Int64T> Int64Constant(int64_t value);
Uint64Constant(uint64_t value)525 TNode<Uint64T> Uint64Constant(uint64_t value) {
526 return Unsigned(Int64Constant(bit_cast<int64_t>(value)));
527 }
528 TNode<IntPtrT> IntPtrConstant(intptr_t value);
Uint32Constant(uint32_t value)529 TNode<Uint32T> Uint32Constant(uint32_t value) {
530 return Unsigned(Int32Constant(bit_cast<int32_t>(value)));
531 }
UintPtrConstant(uintptr_t value)532 TNode<UintPtrT> UintPtrConstant(uintptr_t value) {
533 return Unsigned(IntPtrConstant(bit_cast<intptr_t>(value)));
534 }
535 TNode<TaggedIndex> TaggedIndexConstant(intptr_t value);
PointerConstant(void * value)536 TNode<RawPtrT> PointerConstant(void* value) {
537 return ReinterpretCast<RawPtrT>(IntPtrConstant(bit_cast<intptr_t>(value)));
538 }
539 TNode<Number> NumberConstant(double value);
540 TNode<Smi> SmiConstant(Smi value);
541 TNode<Smi> SmiConstant(int value);
542 template <typename E,
543 typename = typename std::enable_if<std::is_enum<E>::value>::type>
SmiConstant(E value)544 TNode<Smi> SmiConstant(E value) {
545 STATIC_ASSERT(sizeof(E) <= sizeof(int));
546 return SmiConstant(static_cast<int>(value));
547 }
548 TNode<HeapObject> UntypedHeapConstant(Handle<HeapObject> object);
549 template <class Type>
HeapConstant(Handle<Type> object)550 TNode<Type> HeapConstant(Handle<Type> object) {
551 return UncheckedCast<Type>(UntypedHeapConstant(object));
552 }
553 TNode<String> StringConstant(const char* str);
554 TNode<Oddball> BooleanConstant(bool value);
555 TNode<ExternalReference> ExternalConstant(ExternalReference address);
556 TNode<Float32T> Float32Constant(double value);
557 TNode<Float64T> Float64Constant(double value);
Int32TrueConstant()558 TNode<BoolT> Int32TrueConstant() {
559 return ReinterpretCast<BoolT>(Int32Constant(1));
560 }
Int32FalseConstant()561 TNode<BoolT> Int32FalseConstant() {
562 return ReinterpretCast<BoolT>(Int32Constant(0));
563 }
BoolConstant(bool value)564 TNode<BoolT> BoolConstant(bool value) {
565 return value ? Int32TrueConstant() : Int32FalseConstant();
566 }
567
568 bool IsMapOffsetConstant(Node* node);
569
570 bool TryToInt32Constant(TNode<IntegralT> node, int32_t* out_value);
571 bool TryToInt64Constant(TNode<IntegralT> node, int64_t* out_value);
572 bool TryToIntPtrConstant(TNode<IntegralT> node, intptr_t* out_value);
573 bool TryToIntPtrConstant(TNode<Smi> tnode, intptr_t* out_value);
574 bool TryToSmiConstant(TNode<IntegralT> node, Smi* out_value);
575 bool TryToSmiConstant(TNode<Smi> node, Smi* out_value);
576
577 bool IsUndefinedConstant(TNode<Object> node);
578 bool IsNullConstant(TNode<Object> node);
579
Signed(TNode<Word32T> x)580 TNode<Int32T> Signed(TNode<Word32T> x) { return UncheckedCast<Int32T>(x); }
Signed(TNode<Word64T> x)581 TNode<Int64T> Signed(TNode<Word64T> x) { return UncheckedCast<Int64T>(x); }
Signed(TNode<WordT> x)582 TNode<IntPtrT> Signed(TNode<WordT> x) { return UncheckedCast<IntPtrT>(x); }
Unsigned(TNode<Word32T> x)583 TNode<Uint32T> Unsigned(TNode<Word32T> x) {
584 return UncheckedCast<Uint32T>(x);
585 }
Unsigned(TNode<Word64T> x)586 TNode<Uint64T> Unsigned(TNode<Word64T> x) {
587 return UncheckedCast<Uint64T>(x);
588 }
Unsigned(TNode<WordT> x)589 TNode<UintPtrT> Unsigned(TNode<WordT> x) {
590 return UncheckedCast<UintPtrT>(x);
591 }
592
593 static constexpr int kTargetParameterIndex = -1;
594
595 template <class T>
596 TNode<T> Parameter(
597 int value, cppgc::SourceLocation loc = cppgc::SourceLocation::Current()) {
598 static_assert(
599 std::is_convertible<TNode<T>, TNode<Object>>::value,
600 "Parameter is only for tagged types. Use UncheckedParameter instead.");
601 std::stringstream message;
602 message << "Parameter " << value;
603 if (loc.FileName()) {
604 message << " at " << loc.FileName() << ":" << loc.Line();
605 }
606 size_t buf_size = message.str().size() + 1;
607 char* message_dup = zone()->NewArray<char>(buf_size);
608 snprintf(message_dup, buf_size, "%s", message.str().c_str());
609
610 return Cast(UntypedParameter(value), message_dup);
611 }
612
613 template <class T>
UncheckedParameter(int value)614 TNode<T> UncheckedParameter(int value) {
615 return UncheckedCast<T>(UntypedParameter(value));
616 }
617
618 Node* UntypedParameter(int value);
619
620 TNode<Context> GetJSContextParameter();
621 void Return(TNode<Object> value);
622 void Return(TNode<Object> value1, TNode<Object> value2);
623 void Return(TNode<Object> value1, TNode<Object> value2, TNode<Object> value3);
624 void Return(TNode<Int32T> value);
625 void Return(TNode<Uint32T> value);
626 void Return(TNode<WordT> value);
627 void Return(TNode<Float32T> value);
628 void Return(TNode<Float64T> value);
629 void Return(TNode<WordT> value1, TNode<WordT> value2);
630 void PopAndReturn(Node* pop, Node* value);
631
632 void ReturnIf(TNode<BoolT> condition, TNode<Object> value);
633
634 void AbortCSADcheck(Node* message);
635 void DebugBreak();
636 void Unreachable();
Comment(const char * msg)637 void Comment(const char* msg) {
638 if (!FLAG_code_comments) return;
639 Comment(std::string(msg));
640 }
641 void Comment(std::string msg);
642 template <class... Args>
Comment(Args &&...args)643 void Comment(Args&&... args) {
644 if (!FLAG_code_comments) return;
645 std::ostringstream s;
646 USE((s << std::forward<Args>(args))...);
647 Comment(s.str());
648 }
649
650 void StaticAssert(TNode<BoolT> value,
651 const char* source = "unknown position");
652
653 // The following methods refer to source positions in CSA or Torque code
654 // compiled during mksnapshot, not JS compiled at runtime.
655 void SetSourcePosition(const char* file, int line);
656 void PushSourcePosition();
657 void PopSourcePosition();
658 class V8_NODISCARD SourcePositionScope {
659 public:
SourcePositionScope(CodeAssembler * ca)660 explicit SourcePositionScope(CodeAssembler* ca) : ca_(ca) {
661 ca->PushSourcePosition();
662 }
~SourcePositionScope()663 ~SourcePositionScope() { ca_->PopSourcePosition(); }
664
665 private:
666 CodeAssembler* ca_;
667 };
668 const std::vector<FileAndLine>& GetMacroSourcePositionStack() const;
669
670 void Bind(Label* label);
671 #if DEBUG
672 void Bind(Label* label, AssemblerDebugInfo debug_info);
673 #endif // DEBUG
674 void Goto(Label* label);
675 void GotoIf(TNode<IntegralT> condition, Label* true_label);
676 void GotoIfNot(TNode<IntegralT> condition, Label* false_label);
677 void Branch(TNode<IntegralT> condition, Label* true_label,
678 Label* false_label);
679
680 template <class T>
Uninitialized()681 TNode<T> Uninitialized() {
682 return {};
683 }
684
685 template <class... T>
Bind(CodeAssemblerParameterizedLabel<T...> * label,TNode<T> * ...phis)686 void Bind(CodeAssemblerParameterizedLabel<T...>* label, TNode<T>*... phis) {
687 Bind(label->plain_label());
688 label->CreatePhis(phis...);
689 }
690 template <class... T, class... Args>
Branch(TNode<BoolT> condition,CodeAssemblerParameterizedLabel<T...> * if_true,CodeAssemblerParameterizedLabel<T...> * if_false,Args...args)691 void Branch(TNode<BoolT> condition,
692 CodeAssemblerParameterizedLabel<T...>* if_true,
693 CodeAssemblerParameterizedLabel<T...>* if_false, Args... args) {
694 if_true->AddInputs(args...);
695 if_false->AddInputs(args...);
696 Branch(condition, if_true->plain_label(), if_false->plain_label());
697 }
698 template <class... T, class... U>
Branch(TNode<BoolT> condition,CodeAssemblerParameterizedLabel<T...> * if_true,std::vector<Node * > args_true,CodeAssemblerParameterizedLabel<U...> * if_false,std::vector<Node * > args_false)699 void Branch(TNode<BoolT> condition,
700 CodeAssemblerParameterizedLabel<T...>* if_true,
701 std::vector<Node*> args_true,
702 CodeAssemblerParameterizedLabel<U...>* if_false,
703 std::vector<Node*> args_false) {
704 if_true->AddInputsVector(std::move(args_true));
705 if_false->AddInputsVector(std::move(args_false));
706 Branch(condition, if_true->plain_label(), if_false->plain_label());
707 }
708
709 template <class... T, class... Args>
Goto(CodeAssemblerParameterizedLabel<T...> * label,Args...args)710 void Goto(CodeAssemblerParameterizedLabel<T...>* label, Args... args) {
711 label->AddInputs(args...);
712 Goto(label->plain_label());
713 }
714
715 void Branch(TNode<BoolT> condition, const std::function<void()>& true_body,
716 const std::function<void()>& false_body);
717 void Branch(TNode<BoolT> condition, Label* true_label,
718 const std::function<void()>& false_body);
719 void Branch(TNode<BoolT> condition, const std::function<void()>& true_body,
720 Label* false_label);
721
722 void Switch(Node* index, Label* default_label, const int32_t* case_values,
723 Label** case_labels, size_t case_count);
724
725 // Access to the frame pointer
726 TNode<RawPtrT> LoadFramePointer();
727 TNode<RawPtrT> LoadParentFramePointer();
728
729 // Load raw memory location.
730 Node* Load(MachineType type, Node* base);
731 template <class Type>
Load(MachineType type,TNode<RawPtr<Type>> base)732 TNode<Type> Load(MachineType type, TNode<RawPtr<Type>> base) {
733 DCHECK(
734 IsSubtype(type.representation(), MachineRepresentationOf<Type>::value));
735 return UncheckedCast<Type>(Load(type, static_cast<Node*>(base)));
736 }
737 Node* Load(MachineType type, Node* base, Node* offset);
738 template <class Type>
Load(Node * base)739 TNode<Type> Load(Node* base) {
740 return UncheckedCast<Type>(Load(MachineTypeOf<Type>::value, base));
741 }
742 template <class Type>
Load(Node * base,TNode<WordT> offset)743 TNode<Type> Load(Node* base, TNode<WordT> offset) {
744 return UncheckedCast<Type>(Load(MachineTypeOf<Type>::value, base, offset));
745 }
746 template <class Type>
AtomicLoad(AtomicMemoryOrder order,TNode<RawPtrT> base,TNode<WordT> offset)747 TNode<Type> AtomicLoad(AtomicMemoryOrder order, TNode<RawPtrT> base,
748 TNode<WordT> offset) {
749 return UncheckedCast<Type>(
750 AtomicLoad(MachineTypeOf<Type>::value, order, base, offset));
751 }
752 template <class Type>
753 TNode<Type> AtomicLoad64(AtomicMemoryOrder order, TNode<RawPtrT> base,
754 TNode<WordT> offset);
755 // Load uncompressed tagged value from (most likely off JS heap) memory
756 // location.
757 TNode<Object> LoadFullTagged(Node* base);
758 TNode<Object> LoadFullTagged(Node* base, TNode<IntPtrT> offset);
759
760 Node* LoadFromObject(MachineType type, TNode<Object> object,
761 TNode<IntPtrT> offset);
762
763 #ifdef V8_MAP_PACKING
764 Node* PackMapWord(Node* value);
765 #endif
766
767 // Load a value from the root array.
768 // If map packing is enabled, LoadRoot for a root map returns the unpacked map
769 // word (i.e., the map). Use LoadRootMapWord to obtain the packed map word
770 // instead.
771 TNode<Object> LoadRoot(RootIndex root_index);
772 TNode<AnyTaggedT> LoadRootMapWord(RootIndex root_index);
773
774 template <typename Type>
UnalignedLoad(TNode<RawPtrT> base,TNode<IntPtrT> offset)775 TNode<Type> UnalignedLoad(TNode<RawPtrT> base, TNode<IntPtrT> offset) {
776 MachineType mt = MachineTypeOf<Type>::value;
777 return UncheckedCast<Type>(UnalignedLoad(mt, base, offset));
778 }
779
780 // Store value to raw memory location.
781 void Store(Node* base, Node* value);
782 void Store(Node* base, Node* offset, Node* value);
783 void StoreEphemeronKey(Node* base, Node* offset, Node* value);
784 void StoreNoWriteBarrier(MachineRepresentation rep, Node* base, Node* value);
785 void StoreNoWriteBarrier(MachineRepresentation rep, Node* base, Node* offset,
786 Node* value);
787 void UnsafeStoreNoWriteBarrier(MachineRepresentation rep, Node* base,
788 Node* value);
789 void UnsafeStoreNoWriteBarrier(MachineRepresentation rep, Node* base,
790 Node* offset, Node* value);
791
792 // Stores uncompressed tagged value to (most likely off JS heap) memory
793 // location without write barrier.
794 void StoreFullTaggedNoWriteBarrier(TNode<RawPtrT> base,
795 TNode<Object> tagged_value);
796 void StoreFullTaggedNoWriteBarrier(TNode<RawPtrT> base, TNode<IntPtrT> offset,
797 TNode<Object> tagged_value);
798
799 // Optimized memory operations that map to Turbofan simplified nodes.
800 TNode<HeapObject> OptimizedAllocate(TNode<IntPtrT> size,
801 AllocationType allocation,
802 AllowLargeObjects allow_large_objects);
803 void StoreToObject(MachineRepresentation rep, TNode<Object> object,
804 TNode<IntPtrT> offset, Node* value,
805 StoreToObjectWriteBarrier write_barrier);
806 void OptimizedStoreField(MachineRepresentation rep, TNode<HeapObject> object,
807 int offset, Node* value);
808 void OptimizedStoreFieldAssertNoWriteBarrier(MachineRepresentation rep,
809 TNode<HeapObject> object,
810 int offset, Node* value);
811 void OptimizedStoreFieldUnsafeNoWriteBarrier(MachineRepresentation rep,
812 TNode<HeapObject> object,
813 int offset, Node* value);
814 void OptimizedStoreMap(TNode<HeapObject> object, TNode<Map>);
815 void AtomicStore(MachineRepresentation rep, AtomicMemoryOrder order,
816 TNode<RawPtrT> base, TNode<WordT> offset,
817 TNode<Word32T> value);
818 // {value_high} is used for 64-bit stores on 32-bit platforms, must be
819 // nullptr in other cases.
820 void AtomicStore64(AtomicMemoryOrder order, TNode<RawPtrT> base,
821 TNode<WordT> offset, TNode<UintPtrT> value,
822 TNode<UintPtrT> value_high);
823
824 TNode<Word32T> AtomicAdd(MachineType type, TNode<RawPtrT> base,
825 TNode<UintPtrT> offset, TNode<Word32T> value);
826 template <class Type>
827 TNode<Type> AtomicAdd64(TNode<RawPtrT> base, TNode<UintPtrT> offset,
828 TNode<UintPtrT> value, TNode<UintPtrT> value_high);
829
830 TNode<Word32T> AtomicSub(MachineType type, TNode<RawPtrT> base,
831 TNode<UintPtrT> offset, TNode<Word32T> value);
832 template <class Type>
833 TNode<Type> AtomicSub64(TNode<RawPtrT> base, TNode<UintPtrT> offset,
834 TNode<UintPtrT> value, TNode<UintPtrT> value_high);
835
836 TNode<Word32T> AtomicAnd(MachineType type, TNode<RawPtrT> base,
837 TNode<UintPtrT> offset, TNode<Word32T> value);
838 template <class Type>
839 TNode<Type> AtomicAnd64(TNode<RawPtrT> base, TNode<UintPtrT> offset,
840 TNode<UintPtrT> value, TNode<UintPtrT> value_high);
841
842 TNode<Word32T> AtomicOr(MachineType type, TNode<RawPtrT> base,
843 TNode<UintPtrT> offset, TNode<Word32T> value);
844 template <class Type>
845 TNode<Type> AtomicOr64(TNode<RawPtrT> base, TNode<UintPtrT> offset,
846 TNode<UintPtrT> value, TNode<UintPtrT> value_high);
847
848 TNode<Word32T> AtomicXor(MachineType type, TNode<RawPtrT> base,
849 TNode<UintPtrT> offset, TNode<Word32T> value);
850 template <class Type>
851 TNode<Type> AtomicXor64(TNode<RawPtrT> base, TNode<UintPtrT> offset,
852 TNode<UintPtrT> value, TNode<UintPtrT> value_high);
853
854 // Exchange value at raw memory location
855 TNode<Word32T> AtomicExchange(MachineType type, TNode<RawPtrT> base,
856 TNode<UintPtrT> offset, TNode<Word32T> value);
857 template <class Type>
858 TNode<Type> AtomicExchange64(TNode<RawPtrT> base, TNode<UintPtrT> offset,
859 TNode<UintPtrT> value,
860 TNode<UintPtrT> value_high);
861
862 // Compare and Exchange value at raw memory location
863 TNode<Word32T> AtomicCompareExchange(MachineType type, TNode<RawPtrT> base,
864 TNode<WordT> offset,
865 TNode<Word32T> old_value,
866 TNode<Word32T> new_value);
867
868 template <class Type>
869 TNode<Type> AtomicCompareExchange64(TNode<RawPtrT> base, TNode<WordT> offset,
870 TNode<UintPtrT> old_value,
871 TNode<UintPtrT> new_value,
872 TNode<UintPtrT> old_value_high,
873 TNode<UintPtrT> new_value_high);
874
875 // Store a value to the root array.
876 void StoreRoot(RootIndex root_index, TNode<Object> value);
877
878 // Basic arithmetic operations.
879 #define DECLARE_CODE_ASSEMBLER_BINARY_OP(name, ResType, Arg1Type, Arg2Type) \
880 TNode<ResType> name(TNode<Arg1Type> a, TNode<Arg2Type> b);
CODE_ASSEMBLER_BINARY_OP_LIST(DECLARE_CODE_ASSEMBLER_BINARY_OP)881 CODE_ASSEMBLER_BINARY_OP_LIST(DECLARE_CODE_ASSEMBLER_BINARY_OP)
882 #undef DECLARE_CODE_ASSEMBLER_BINARY_OP
883
884 TNode<UintPtrT> WordShr(TNode<UintPtrT> left, TNode<IntegralT> right) {
885 return Unsigned(WordShr(static_cast<TNode<WordT>>(left), right));
886 }
WordSar(TNode<IntPtrT> left,TNode<IntegralT> right)887 TNode<IntPtrT> WordSar(TNode<IntPtrT> left, TNode<IntegralT> right) {
888 return Signed(WordSar(static_cast<TNode<WordT>>(left), right));
889 }
WordShl(TNode<IntPtrT> left,TNode<IntegralT> right)890 TNode<IntPtrT> WordShl(TNode<IntPtrT> left, TNode<IntegralT> right) {
891 return Signed(WordShl(static_cast<TNode<WordT>>(left), right));
892 }
WordShl(TNode<UintPtrT> left,TNode<IntegralT> right)893 TNode<UintPtrT> WordShl(TNode<UintPtrT> left, TNode<IntegralT> right) {
894 return Unsigned(WordShl(static_cast<TNode<WordT>>(left), right));
895 }
896
Word32Shl(TNode<Int32T> left,TNode<Int32T> right)897 TNode<Int32T> Word32Shl(TNode<Int32T> left, TNode<Int32T> right) {
898 return Signed(Word32Shl(static_cast<TNode<Word32T>>(left), right));
899 }
Word32Shl(TNode<Uint32T> left,TNode<Uint32T> right)900 TNode<Uint32T> Word32Shl(TNode<Uint32T> left, TNode<Uint32T> right) {
901 return Unsigned(Word32Shl(static_cast<TNode<Word32T>>(left), right));
902 }
Word32Shr(TNode<Uint32T> left,TNode<Uint32T> right)903 TNode<Uint32T> Word32Shr(TNode<Uint32T> left, TNode<Uint32T> right) {
904 return Unsigned(Word32Shr(static_cast<TNode<Word32T>>(left), right));
905 }
Word32Sar(TNode<Int32T> left,TNode<Int32T> right)906 TNode<Int32T> Word32Sar(TNode<Int32T> left, TNode<Int32T> right) {
907 return Signed(Word32Sar(static_cast<TNode<Word32T>>(left), right));
908 }
909
Word64Shl(TNode<Int64T> left,TNode<Int64T> right)910 TNode<Int64T> Word64Shl(TNode<Int64T> left, TNode<Int64T> right) {
911 return Signed(Word64Shl(static_cast<TNode<Word64T>>(left), right));
912 }
Word64Shl(TNode<Uint64T> left,TNode<Uint64T> right)913 TNode<Uint64T> Word64Shl(TNode<Uint64T> left, TNode<Uint64T> right) {
914 return Unsigned(Word64Shl(static_cast<TNode<Word64T>>(left), right));
915 }
Word64Shr(TNode<Uint64T> left,TNode<Uint64T> right)916 TNode<Uint64T> Word64Shr(TNode<Uint64T> left, TNode<Uint64T> right) {
917 return Unsigned(Word64Shr(static_cast<TNode<Word64T>>(left), right));
918 }
Word64Sar(TNode<Int64T> left,TNode<Int64T> right)919 TNode<Int64T> Word64Sar(TNode<Int64T> left, TNode<Int64T> right) {
920 return Signed(Word64Sar(static_cast<TNode<Word64T>>(left), right));
921 }
922
Word64And(TNode<Int64T> left,TNode<Int64T> right)923 TNode<Int64T> Word64And(TNode<Int64T> left, TNode<Int64T> right) {
924 return Signed(Word64And(static_cast<TNode<Word64T>>(left), right));
925 }
Word64And(TNode<Uint64T> left,TNode<Uint64T> right)926 TNode<Uint64T> Word64And(TNode<Uint64T> left, TNode<Uint64T> right) {
927 return Unsigned(Word64And(static_cast<TNode<Word64T>>(left), right));
928 }
929
Word64Xor(TNode<Int64T> left,TNode<Int64T> right)930 TNode<Int64T> Word64Xor(TNode<Int64T> left, TNode<Int64T> right) {
931 return Signed(Word64Xor(static_cast<TNode<Word64T>>(left), right));
932 }
Word64Xor(TNode<Uint64T> left,TNode<Uint64T> right)933 TNode<Uint64T> Word64Xor(TNode<Uint64T> left, TNode<Uint64T> right) {
934 return Unsigned(Word64Xor(static_cast<TNode<Word64T>>(left), right));
935 }
936
Word64Not(TNode<Int64T> value)937 TNode<Int64T> Word64Not(TNode<Int64T> value) {
938 return Signed(Word64Not(static_cast<TNode<Word64T>>(value)));
939 }
Word64Not(TNode<Uint64T> value)940 TNode<Uint64T> Word64Not(TNode<Uint64T> value) {
941 return Unsigned(Word64Not(static_cast<TNode<Word64T>>(value)));
942 }
943
WordAnd(TNode<IntPtrT> left,TNode<IntPtrT> right)944 TNode<IntPtrT> WordAnd(TNode<IntPtrT> left, TNode<IntPtrT> right) {
945 return Signed(WordAnd(static_cast<TNode<WordT>>(left),
946 static_cast<TNode<WordT>>(right)));
947 }
WordAnd(TNode<UintPtrT> left,TNode<UintPtrT> right)948 TNode<UintPtrT> WordAnd(TNode<UintPtrT> left, TNode<UintPtrT> right) {
949 return Unsigned(WordAnd(static_cast<TNode<WordT>>(left),
950 static_cast<TNode<WordT>>(right)));
951 }
952
Word32And(TNode<Int32T> left,TNode<Int32T> right)953 TNode<Int32T> Word32And(TNode<Int32T> left, TNode<Int32T> right) {
954 return Signed(Word32And(static_cast<TNode<Word32T>>(left),
955 static_cast<TNode<Word32T>>(right)));
956 }
Word32And(TNode<Uint32T> left,TNode<Uint32T> right)957 TNode<Uint32T> Word32And(TNode<Uint32T> left, TNode<Uint32T> right) {
958 return Unsigned(Word32And(static_cast<TNode<Word32T>>(left),
959 static_cast<TNode<Word32T>>(right)));
960 }
961
WordOr(TNode<IntPtrT> left,TNode<IntPtrT> right)962 TNode<IntPtrT> WordOr(TNode<IntPtrT> left, TNode<IntPtrT> right) {
963 return Signed(WordOr(static_cast<TNode<WordT>>(left),
964 static_cast<TNode<WordT>>(right)));
965 }
966
Word32Or(TNode<Int32T> left,TNode<Int32T> right)967 TNode<Int32T> Word32Or(TNode<Int32T> left, TNode<Int32T> right) {
968 return Signed(Word32Or(static_cast<TNode<Word32T>>(left),
969 static_cast<TNode<Word32T>>(right)));
970 }
Word32Or(TNode<Uint32T> left,TNode<Uint32T> right)971 TNode<Uint32T> Word32Or(TNode<Uint32T> left, TNode<Uint32T> right) {
972 return Unsigned(Word32Or(static_cast<TNode<Word32T>>(left),
973 static_cast<TNode<Word32T>>(right)));
974 }
975
976 TNode<BoolT> IntPtrEqual(TNode<WordT> left, TNode<WordT> right);
977 TNode<BoolT> WordEqual(TNode<WordT> left, TNode<WordT> right);
978 TNode<BoolT> WordNotEqual(TNode<WordT> left, TNode<WordT> right);
979 TNode<BoolT> Word32Equal(TNode<Word32T> left, TNode<Word32T> right);
980 TNode<BoolT> Word32NotEqual(TNode<Word32T> left, TNode<Word32T> right);
981 TNode<BoolT> Word64Equal(TNode<Word64T> left, TNode<Word64T> right);
982 TNode<BoolT> Word64NotEqual(TNode<Word64T> left, TNode<Word64T> right);
983
WordNot(TNode<IntPtrT> a)984 TNode<IntPtrT> WordNot(TNode<IntPtrT> a) {
985 return Signed(WordNot(static_cast<TNode<WordT>>(a)));
986 }
Word32Or(TNode<BoolT> left,TNode<BoolT> right)987 TNode<BoolT> Word32Or(TNode<BoolT> left, TNode<BoolT> right) {
988 return UncheckedCast<BoolT>(Word32Or(static_cast<TNode<Word32T>>(left),
989 static_cast<TNode<Word32T>>(right)));
990 }
Word32And(TNode<BoolT> left,TNode<BoolT> right)991 TNode<BoolT> Word32And(TNode<BoolT> left, TNode<BoolT> right) {
992 return UncheckedCast<BoolT>(Word32And(static_cast<TNode<Word32T>>(left),
993 static_cast<TNode<Word32T>>(right)));
994 }
995
Int32Add(TNode<Int32T> left,TNode<Int32T> right)996 TNode<Int32T> Int32Add(TNode<Int32T> left, TNode<Int32T> right) {
997 return Signed(Int32Add(static_cast<TNode<Word32T>>(left),
998 static_cast<TNode<Word32T>>(right)));
999 }
1000
Uint32Add(TNode<Uint32T> left,TNode<Uint32T> right)1001 TNode<Uint32T> Uint32Add(TNode<Uint32T> left, TNode<Uint32T> right) {
1002 return Unsigned(Int32Add(static_cast<TNode<Word32T>>(left),
1003 static_cast<TNode<Word32T>>(right)));
1004 }
1005
Uint32Sub(TNode<Uint32T> left,TNode<Uint32T> right)1006 TNode<Uint32T> Uint32Sub(TNode<Uint32T> left, TNode<Uint32T> right) {
1007 return Unsigned(Int32Sub(static_cast<TNode<Word32T>>(left),
1008 static_cast<TNode<Word32T>>(right)));
1009 }
1010
Int32Sub(TNode<Int32T> left,TNode<Int32T> right)1011 TNode<Int32T> Int32Sub(TNode<Int32T> left, TNode<Int32T> right) {
1012 return Signed(Int32Sub(static_cast<TNode<Word32T>>(left),
1013 static_cast<TNode<Word32T>>(right)));
1014 }
1015
Int32Mul(TNode<Int32T> left,TNode<Int32T> right)1016 TNode<Int32T> Int32Mul(TNode<Int32T> left, TNode<Int32T> right) {
1017 return Signed(Int32Mul(static_cast<TNode<Word32T>>(left),
1018 static_cast<TNode<Word32T>>(right)));
1019 }
1020
Int64Add(TNode<Int64T> left,TNode<Int64T> right)1021 TNode<Int64T> Int64Add(TNode<Int64T> left, TNode<Int64T> right) {
1022 return Signed(Int64Add(static_cast<TNode<Word64T>>(left), right));
1023 }
1024
Uint64Add(TNode<Uint64T> left,TNode<Uint64T> right)1025 TNode<Uint64T> Uint64Add(TNode<Uint64T> left, TNode<Uint64T> right) {
1026 return Unsigned(Int64Add(static_cast<TNode<Word64T>>(left), right));
1027 }
1028
Int64Sub(TNode<Int64T> left,TNode<Int64T> right)1029 TNode<Int64T> Int64Sub(TNode<Int64T> left, TNode<Int64T> right) {
1030 return Signed(Int64Sub(static_cast<TNode<Word64T>>(left), right));
1031 }
1032
Uint64Sub(TNode<Uint64T> left,TNode<Uint64T> right)1033 TNode<Uint64T> Uint64Sub(TNode<Uint64T> left, TNode<Uint64T> right) {
1034 return Unsigned(Int64Sub(static_cast<TNode<Word64T>>(left), right));
1035 }
1036
Int64Mul(TNode<Int64T> left,TNode<Int64T> right)1037 TNode<Int64T> Int64Mul(TNode<Int64T> left, TNode<Int64T> right) {
1038 return Signed(Int64Mul(static_cast<TNode<Word64T>>(left), right));
1039 }
1040
Uint64Mul(TNode<Uint64T> left,TNode<Uint64T> right)1041 TNode<Uint64T> Uint64Mul(TNode<Uint64T> left, TNode<Uint64T> right) {
1042 return Unsigned(Int64Mul(static_cast<TNode<Word64T>>(left), right));
1043 }
1044
IntPtrAdd(TNode<IntPtrT> left,TNode<IntPtrT> right)1045 TNode<IntPtrT> IntPtrAdd(TNode<IntPtrT> left, TNode<IntPtrT> right) {
1046 return Signed(IntPtrAdd(static_cast<TNode<WordT>>(left),
1047 static_cast<TNode<WordT>>(right)));
1048 }
IntPtrSub(TNode<IntPtrT> left,TNode<IntPtrT> right)1049 TNode<IntPtrT> IntPtrSub(TNode<IntPtrT> left, TNode<IntPtrT> right) {
1050 return Signed(IntPtrSub(static_cast<TNode<WordT>>(left),
1051 static_cast<TNode<WordT>>(right)));
1052 }
IntPtrMul(TNode<IntPtrT> left,TNode<IntPtrT> right)1053 TNode<IntPtrT> IntPtrMul(TNode<IntPtrT> left, TNode<IntPtrT> right) {
1054 return Signed(IntPtrMul(static_cast<TNode<WordT>>(left),
1055 static_cast<TNode<WordT>>(right)));
1056 }
UintPtrAdd(TNode<UintPtrT> left,TNode<UintPtrT> right)1057 TNode<UintPtrT> UintPtrAdd(TNode<UintPtrT> left, TNode<UintPtrT> right) {
1058 return Unsigned(IntPtrAdd(static_cast<TNode<WordT>>(left),
1059 static_cast<TNode<WordT>>(right)));
1060 }
UintPtrSub(TNode<UintPtrT> left,TNode<UintPtrT> right)1061 TNode<UintPtrT> UintPtrSub(TNode<UintPtrT> left, TNode<UintPtrT> right) {
1062 return Unsigned(IntPtrSub(static_cast<TNode<WordT>>(left),
1063 static_cast<TNode<WordT>>(right)));
1064 }
RawPtrAdd(TNode<RawPtrT> left,TNode<IntPtrT> right)1065 TNode<RawPtrT> RawPtrAdd(TNode<RawPtrT> left, TNode<IntPtrT> right) {
1066 return ReinterpretCast<RawPtrT>(IntPtrAdd(left, right));
1067 }
RawPtrSub(TNode<RawPtrT> left,TNode<IntPtrT> right)1068 TNode<RawPtrT> RawPtrSub(TNode<RawPtrT> left, TNode<IntPtrT> right) {
1069 return ReinterpretCast<RawPtrT>(IntPtrSub(left, right));
1070 }
RawPtrSub(TNode<RawPtrT> left,TNode<RawPtrT> right)1071 TNode<IntPtrT> RawPtrSub(TNode<RawPtrT> left, TNode<RawPtrT> right) {
1072 return Signed(IntPtrSub(static_cast<TNode<WordT>>(left),
1073 static_cast<TNode<WordT>>(right)));
1074 }
1075
1076 TNode<WordT> WordShl(TNode<WordT> value, int shift);
1077 TNode<WordT> WordShr(TNode<WordT> value, int shift);
1078 TNode<WordT> WordSar(TNode<WordT> value, int shift);
WordShr(TNode<IntPtrT> value,int shift)1079 TNode<IntPtrT> WordShr(TNode<IntPtrT> value, int shift) {
1080 return UncheckedCast<IntPtrT>(WordShr(TNode<WordT>(value), shift));
1081 }
WordSar(TNode<IntPtrT> value,int shift)1082 TNode<IntPtrT> WordSar(TNode<IntPtrT> value, int shift) {
1083 return UncheckedCast<IntPtrT>(WordSar(TNode<WordT>(value), shift));
1084 }
1085 TNode<Word32T> Word32Shr(TNode<Word32T> value, int shift);
1086 TNode<Word32T> Word32Sar(TNode<Word32T> value, int shift);
1087
1088 // Unary
1089 #define DECLARE_CODE_ASSEMBLER_UNARY_OP(name, ResType, ArgType) \
1090 TNode<ResType> name(TNode<ArgType> a);
CODE_ASSEMBLER_UNARY_OP_LIST(DECLARE_CODE_ASSEMBLER_UNARY_OP)1091 CODE_ASSEMBLER_UNARY_OP_LIST(DECLARE_CODE_ASSEMBLER_UNARY_OP)
1092 #undef DECLARE_CODE_ASSEMBLER_UNARY_OP
1093
1094 template <class Dummy = void>
1095 TNode<IntPtrT> BitcastTaggedToWord(TNode<Smi> node) {
1096 static_assert(sizeof(Dummy) < 0,
1097 "Should use BitcastTaggedToWordForTagAndSmiBits instead.");
1098 }
1099
1100 // Changes a double to an inptr_t for pointer arithmetic outside of Smi range.
1101 // Assumes that the double can be exactly represented as an int.
1102 TNode<IntPtrT> ChangeFloat64ToIntPtr(TNode<Float64T> value);
1103 TNode<UintPtrT> ChangeFloat64ToUintPtr(TNode<Float64T> value);
1104 // Same in the opposite direction.
1105 TNode<Float64T> ChangeUintPtrToFloat64(TNode<UintPtrT> value);
1106
1107 // Changes an intptr_t to a double, e.g. for storing an element index
1108 // outside Smi range in a HeapNumber. Lossless on 32-bit,
1109 // rounds on 64-bit (which doesn't affect valid element indices).
1110 TNode<Float64T> RoundIntPtrToFloat64(Node* value);
1111 // No-op on 32-bit, otherwise zero extend.
1112 TNode<UintPtrT> ChangeUint32ToWord(TNode<Word32T> value);
1113 // No-op on 32-bit, otherwise sign extend.
1114 TNode<IntPtrT> ChangeInt32ToIntPtr(TNode<Word32T> value);
1115
1116 // Truncates a float to a 32-bit integer. If the float is outside of 32-bit
1117 // range, make sure that overflow detection is easy. In particular, return
1118 // int_min instead of int_max on arm platforms by using parameter
1119 // kSetOverflowToMin.
1120 TNode<Int32T> TruncateFloat32ToInt32(TNode<Float32T> value);
1121
1122 // Projections
1123 template <int index, class T1, class T2>
1124 TNode<typename std::tuple_element<index, std::tuple<T1, T2>>::type>
Projection(TNode<PairT<T1,T2>> value)1125 Projection(TNode<PairT<T1, T2>> value) {
1126 return UncheckedCast<
1127 typename std::tuple_element<index, std::tuple<T1, T2>>::type>(
1128 Projection(index, value));
1129 }
1130
1131 // Calls
1132 template <class T = Object, class... TArgs>
CallRuntime(Runtime::FunctionId function,TNode<Object> context,TArgs...args)1133 TNode<T> CallRuntime(Runtime::FunctionId function, TNode<Object> context,
1134 TArgs... args) {
1135 return UncheckedCast<T>(CallRuntimeImpl(
1136 function, context, {implicit_cast<TNode<Object>>(args)...}));
1137 }
1138
1139 template <class... TArgs>
TailCallRuntime(Runtime::FunctionId function,TNode<Object> context,TArgs...args)1140 void TailCallRuntime(Runtime::FunctionId function, TNode<Object> context,
1141 TArgs... args) {
1142 int argc = static_cast<int>(sizeof...(args));
1143 TNode<Int32T> arity = Int32Constant(argc);
1144 return TailCallRuntimeImpl(function, arity, context,
1145 {implicit_cast<TNode<Object>>(args)...});
1146 }
1147
1148 template <class... TArgs>
TailCallRuntime(Runtime::FunctionId function,TNode<Int32T> arity,TNode<Object> context,TArgs...args)1149 void TailCallRuntime(Runtime::FunctionId function, TNode<Int32T> arity,
1150 TNode<Object> context, TArgs... args) {
1151 return TailCallRuntimeImpl(function, arity, context,
1152 {implicit_cast<TNode<Object>>(args)...});
1153 }
1154
1155 //
1156 // If context passed to CallStub is nullptr, it won't be passed to the stub.
1157 //
1158
1159 template <class T = Object, class... TArgs>
CallStub(Callable const & callable,TNode<Object> context,TArgs...args)1160 TNode<T> CallStub(Callable const& callable, TNode<Object> context,
1161 TArgs... args) {
1162 TNode<Code> target = HeapConstant(callable.code());
1163 return CallStub<T>(callable.descriptor(), target, context, args...);
1164 }
1165
1166 template <class T = Object, class... TArgs>
CallStub(const CallInterfaceDescriptor & descriptor,TNode<Code> target,TNode<Object> context,TArgs...args)1167 TNode<T> CallStub(const CallInterfaceDescriptor& descriptor,
1168 TNode<Code> target, TNode<Object> context, TArgs... args) {
1169 return UncheckedCast<T>(CallStubR(StubCallMode::kCallCodeObject, descriptor,
1170 target, context, args...));
1171 }
1172
1173 template <class T = Object, class... TArgs>
CallBuiltinPointer(const CallInterfaceDescriptor & descriptor,TNode<BuiltinPtr> target,TNode<Object> context,TArgs...args)1174 TNode<T> CallBuiltinPointer(const CallInterfaceDescriptor& descriptor,
1175 TNode<BuiltinPtr> target, TNode<Object> context,
1176 TArgs... args) {
1177 return UncheckedCast<T>(CallStubR(StubCallMode::kCallBuiltinPointer,
1178 descriptor, target, context, args...));
1179 }
1180
1181 template <class... TArgs>
TailCallStub(Callable const & callable,TNode<Object> context,TArgs...args)1182 void TailCallStub(Callable const& callable, TNode<Object> context,
1183 TArgs... args) {
1184 TNode<Code> target = HeapConstant(callable.code());
1185 TailCallStub(callable.descriptor(), target, context, args...);
1186 }
1187
1188 template <class... TArgs>
TailCallStub(const CallInterfaceDescriptor & descriptor,TNode<Code> target,TNode<Object> context,TArgs...args)1189 void TailCallStub(const CallInterfaceDescriptor& descriptor,
1190 TNode<Code> target, TNode<Object> context, TArgs... args) {
1191 TailCallStubImpl(descriptor, target, context, {args...});
1192 }
1193
1194 template <class... TArgs>
1195 void TailCallBytecodeDispatch(const CallInterfaceDescriptor& descriptor,
1196 TNode<RawPtrT> target, TArgs... args);
1197
1198 template <class... TArgs>
TailCallStubThenBytecodeDispatch(const CallInterfaceDescriptor & descriptor,Node * target,Node * context,TArgs...args)1199 void TailCallStubThenBytecodeDispatch(
1200 const CallInterfaceDescriptor& descriptor, Node* target, Node* context,
1201 TArgs... args) {
1202 TailCallStubThenBytecodeDispatchImpl(descriptor, target, context,
1203 {args...});
1204 }
1205
1206 // Tailcalls to the given code object with JSCall linkage. The JS arguments
1207 // (including receiver) are supposed to be already on the stack.
1208 // This is a building block for implementing trampoline stubs that are
1209 // installed instead of code objects with JSCall linkage.
1210 // Note that no arguments adaption is going on here - all the JavaScript
1211 // arguments are left on the stack unmodified. Therefore, this tail call can
1212 // only be used after arguments adaptation has been performed already.
1213 void TailCallJSCode(TNode<Code> code, TNode<Context> context,
1214 TNode<JSFunction> function, TNode<Object> new_target,
1215 TNode<Int32T> arg_count);
1216
1217 template <class... TArgs>
CallJS(Callable const & callable,Node * context,Node * function,Node * receiver,TArgs...args)1218 TNode<Object> CallJS(Callable const& callable, Node* context, Node* function,
1219 Node* receiver, TArgs... args) {
1220 int argc = JSParameterCount(static_cast<int>(sizeof...(args)));
1221 TNode<Int32T> arity = Int32Constant(argc);
1222 TNode<Code> target = HeapConstant(callable.code());
1223 return CAST(CallJSStubImpl(callable.descriptor(), target, CAST(context),
1224 CAST(function), {}, arity, {receiver, args...}));
1225 }
1226
1227 template <class... TArgs>
ConstructJSWithTarget(Callable const & callable,Node * context,Node * function,Node * new_target,TArgs...args)1228 Node* ConstructJSWithTarget(Callable const& callable, Node* context,
1229 Node* function, Node* new_target, TArgs... args) {
1230 int argc = JSParameterCount(static_cast<int>(sizeof...(args)));
1231 TNode<Int32T> arity = Int32Constant(argc);
1232 TNode<Object> receiver = LoadRoot(RootIndex::kUndefinedValue);
1233 TNode<Code> target = HeapConstant(callable.code());
1234 return CallJSStubImpl(callable.descriptor(), target, CAST(context),
1235 CAST(function), CAST(new_target), arity,
1236 {receiver, args...});
1237 }
1238 template <class... TArgs>
ConstructJS(Callable const & callable,Node * context,Node * new_target,TArgs...args)1239 Node* ConstructJS(Callable const& callable, Node* context, Node* new_target,
1240 TArgs... args) {
1241 return ConstructJSWithTarget(callable, context, new_target, new_target,
1242 args...);
1243 }
1244
1245 Node* CallCFunctionN(Signature<MachineType>* signature, int input_count,
1246 Node* const* inputs);
1247
1248 // Type representing C function argument with type info.
1249 using CFunctionArg = std::pair<MachineType, Node*>;
1250
1251 // Call to a C function.
1252 template <class... CArgs>
CallCFunction(Node * function,base::Optional<MachineType> return_type,CArgs...cargs)1253 Node* CallCFunction(Node* function, base::Optional<MachineType> return_type,
1254 CArgs... cargs) {
1255 static_assert(v8::internal::conjunction<
1256 std::is_convertible<CArgs, CFunctionArg>...>::value,
1257 "invalid argument types");
1258 return CallCFunction(function, return_type, {cargs...});
1259 }
1260
1261 // Call to a C function without a function discriptor on AIX.
1262 template <class... CArgs>
CallCFunctionWithoutFunctionDescriptor(Node * function,MachineType return_type,CArgs...cargs)1263 Node* CallCFunctionWithoutFunctionDescriptor(Node* function,
1264 MachineType return_type,
1265 CArgs... cargs) {
1266 static_assert(v8::internal::conjunction<
1267 std::is_convertible<CArgs, CFunctionArg>...>::value,
1268 "invalid argument types");
1269 return CallCFunctionWithoutFunctionDescriptor(function, return_type,
1270 {cargs...});
1271 }
1272
1273 // Call to a C function, while saving/restoring caller registers.
1274 template <class... CArgs>
CallCFunctionWithCallerSavedRegisters(Node * function,MachineType return_type,SaveFPRegsMode mode,CArgs...cargs)1275 Node* CallCFunctionWithCallerSavedRegisters(Node* function,
1276 MachineType return_type,
1277 SaveFPRegsMode mode,
1278 CArgs... cargs) {
1279 static_assert(v8::internal::conjunction<
1280 std::is_convertible<CArgs, CFunctionArg>...>::value,
1281 "invalid argument types");
1282 return CallCFunctionWithCallerSavedRegisters(function, return_type, mode,
1283 {cargs...});
1284 }
1285
1286 // Helpers which delegate to RawMachineAssembler.
1287 Factory* factory() const;
1288 Isolate* isolate() const;
1289 Zone* zone() const;
1290
state()1291 CodeAssemblerState* state() { return state_; }
1292
1293 void BreakOnNode(int node_id);
1294
1295 bool UnalignedLoadSupported(MachineRepresentation rep) const;
1296 bool UnalignedStoreSupported(MachineRepresentation rep) const;
1297
1298 bool IsExceptionHandlerActive() const;
1299
1300 protected:
1301 void RegisterCallGenerationCallbacks(
1302 const CodeAssemblerCallback& call_prologue,
1303 const CodeAssemblerCallback& call_epilogue);
1304 void UnregisterCallGenerationCallbacks();
1305
1306 bool Word32ShiftIsSafe() const;
1307
1308 bool IsJSFunctionCall() const;
1309
1310 private:
1311 void HandleException(Node* result);
1312
1313 Node* CallCFunction(Node* function, base::Optional<MachineType> return_type,
1314 std::initializer_list<CFunctionArg> args);
1315
1316 Node* CallCFunctionWithoutFunctionDescriptor(
1317 Node* function, MachineType return_type,
1318 std::initializer_list<CFunctionArg> args);
1319
1320 Node* CallCFunctionWithCallerSavedRegisters(
1321 Node* function, MachineType return_type, SaveFPRegsMode mode,
1322 std::initializer_list<CFunctionArg> args);
1323
1324 Node* CallRuntimeImpl(Runtime::FunctionId function, TNode<Object> context,
1325 std::initializer_list<TNode<Object>> args);
1326
1327 void TailCallRuntimeImpl(Runtime::FunctionId function, TNode<Int32T> arity,
1328 TNode<Object> context,
1329 std::initializer_list<TNode<Object>> args);
1330
1331 void TailCallStubImpl(const CallInterfaceDescriptor& descriptor,
1332 TNode<Code> target, TNode<Object> context,
1333 std::initializer_list<Node*> args);
1334
1335 void TailCallStubThenBytecodeDispatchImpl(
1336 const CallInterfaceDescriptor& descriptor, Node* target, Node* context,
1337 std::initializer_list<Node*> args);
1338
1339 template <class... TArgs>
CallStubR(StubCallMode call_mode,const CallInterfaceDescriptor & descriptor,TNode<Object> target,TNode<Object> context,TArgs...args)1340 Node* CallStubR(StubCallMode call_mode,
1341 const CallInterfaceDescriptor& descriptor,
1342 TNode<Object> target, TNode<Object> context, TArgs... args) {
1343 return CallStubRImpl(call_mode, descriptor, target, context, {args...});
1344 }
1345
1346 Node* CallStubRImpl(StubCallMode call_mode,
1347 const CallInterfaceDescriptor& descriptor,
1348 TNode<Object> target, TNode<Object> context,
1349 std::initializer_list<Node*> args);
1350
1351 Node* CallJSStubImpl(const CallInterfaceDescriptor& descriptor,
1352 TNode<Object> target, TNode<Object> context,
1353 TNode<Object> function,
1354 base::Optional<TNode<Object>> new_target,
1355 TNode<Int32T> arity, std::initializer_list<Node*> args);
1356
1357 Node* CallStubN(StubCallMode call_mode,
1358 const CallInterfaceDescriptor& descriptor, int input_count,
1359 Node* const* inputs);
1360
1361 Node* AtomicLoad(MachineType type, AtomicMemoryOrder order,
1362 TNode<RawPtrT> base, TNode<WordT> offset);
1363
1364 Node* UnalignedLoad(MachineType type, TNode<RawPtrT> base,
1365 TNode<WordT> offset);
1366
1367 // These two don't have definitions and are here only for catching use cases
1368 // where the cast is not necessary.
1369 TNode<Int32T> Signed(TNode<Int32T> x);
1370 TNode<Uint32T> Unsigned(TNode<Uint32T> x);
1371
1372 Node* Projection(int index, Node* value);
1373
1374 RawMachineAssembler* raw_assembler() const;
1375 JSGraph* jsgraph() const;
1376
1377 // Calls respective callback registered in the state.
1378 void CallPrologue();
1379 void CallEpilogue();
1380
1381 CodeAssemblerState* state_;
1382 };
1383
1384 // TODO(solanes, v8:6949): this class should be merged into
1385 // TypedCodeAssemblerVariable. It's required to be separate for
1386 // CodeAssemblerVariableLists.
1387 class V8_EXPORT_PRIVATE CodeAssemblerVariable {
1388 public:
1389 CodeAssemblerVariable(const CodeAssemblerVariable&) = delete;
1390 CodeAssemblerVariable& operator=(const CodeAssemblerVariable&) = delete;
1391
1392 Node* value() const;
1393 MachineRepresentation rep() const;
1394 bool IsBound() const;
1395
1396 protected:
1397 explicit CodeAssemblerVariable(CodeAssembler* assembler,
1398 MachineRepresentation rep);
1399 CodeAssemblerVariable(CodeAssembler* assembler, MachineRepresentation rep,
1400 Node* initial_value);
1401 #if DEBUG
1402 CodeAssemblerVariable(CodeAssembler* assembler, AssemblerDebugInfo debug_info,
1403 MachineRepresentation rep);
1404 CodeAssemblerVariable(CodeAssembler* assembler, AssemblerDebugInfo debug_info,
1405 MachineRepresentation rep, Node* initial_value);
1406 #endif // DEBUG
1407
1408 ~CodeAssemblerVariable();
1409 void Bind(Node* value);
1410
1411 private:
1412 class Impl;
1413 friend class CodeAssemblerLabel;
1414 friend class CodeAssemblerState;
1415 friend std::ostream& operator<<(std::ostream&, const Impl&);
1416 friend std::ostream& operator<<(std::ostream&, const CodeAssemblerVariable&);
1417 struct ImplComparator {
1418 bool operator()(const CodeAssemblerVariable::Impl* a,
1419 const CodeAssemblerVariable::Impl* b) const;
1420 };
1421 Impl* impl_;
1422 CodeAssemblerState* state_;
1423 };
1424
1425 std::ostream& operator<<(std::ostream&, const CodeAssemblerVariable&);
1426 std::ostream& operator<<(std::ostream&, const CodeAssemblerVariable::Impl&);
1427
1428 template <class T>
1429 class TypedCodeAssemblerVariable : public CodeAssemblerVariable {
1430 public:
TypedCodeAssemblerVariable(TNode<T> initial_value,CodeAssembler * assembler)1431 TypedCodeAssemblerVariable(TNode<T> initial_value, CodeAssembler* assembler)
1432 : CodeAssemblerVariable(assembler, PhiMachineRepresentationOf<T>,
1433 initial_value) {}
TypedCodeAssemblerVariable(CodeAssembler * assembler)1434 explicit TypedCodeAssemblerVariable(CodeAssembler* assembler)
1435 : CodeAssemblerVariable(assembler, PhiMachineRepresentationOf<T>) {}
1436 #if DEBUG
TypedCodeAssemblerVariable(AssemblerDebugInfo debug_info,CodeAssembler * assembler)1437 TypedCodeAssemblerVariable(AssemblerDebugInfo debug_info,
1438 CodeAssembler* assembler)
1439 : CodeAssemblerVariable(assembler, debug_info,
1440 PhiMachineRepresentationOf<T>) {}
TypedCodeAssemblerVariable(AssemblerDebugInfo debug_info,TNode<T> initial_value,CodeAssembler * assembler)1441 TypedCodeAssemblerVariable(AssemblerDebugInfo debug_info,
1442 TNode<T> initial_value, CodeAssembler* assembler)
1443 : CodeAssemblerVariable(assembler, debug_info,
1444 PhiMachineRepresentationOf<T>, initial_value) {}
1445 #endif // DEBUG
1446
value()1447 TNode<T> value() const {
1448 return TNode<T>::UncheckedCast(CodeAssemblerVariable::value());
1449 }
1450
1451 void operator=(TNode<T> value) { Bind(value); }
1452 void operator=(const TypedCodeAssemblerVariable<T>& variable) {
1453 Bind(variable.value());
1454 }
1455
1456 private:
1457 using CodeAssemblerVariable::Bind;
1458 };
1459
1460 class V8_EXPORT_PRIVATE CodeAssemblerLabel {
1461 public:
1462 enum Type { kDeferred, kNonDeferred };
1463
1464 explicit CodeAssemblerLabel(
1465 CodeAssembler* assembler,
1466 CodeAssemblerLabel::Type type = CodeAssemblerLabel::kNonDeferred)
1467 : CodeAssemblerLabel(assembler, 0, nullptr, type) {}
1468 CodeAssemblerLabel(
1469 CodeAssembler* assembler,
1470 const CodeAssemblerVariableList& merged_variables,
1471 CodeAssemblerLabel::Type type = CodeAssemblerLabel::kNonDeferred)
1472 : CodeAssemblerLabel(assembler, merged_variables.size(),
1473 &(merged_variables[0]), type) {}
1474 CodeAssemblerLabel(
1475 CodeAssembler* assembler, size_t count,
1476 CodeAssemblerVariable* const* vars,
1477 CodeAssemblerLabel::Type type = CodeAssemblerLabel::kNonDeferred);
1478 CodeAssemblerLabel(
1479 CodeAssembler* assembler,
1480 std::initializer_list<CodeAssemblerVariable*> vars,
1481 CodeAssemblerLabel::Type type = CodeAssemblerLabel::kNonDeferred)
1482 : CodeAssemblerLabel(assembler, vars.size(), vars.begin(), type) {}
1483 CodeAssemblerLabel(
1484 CodeAssembler* assembler, CodeAssemblerVariable* merged_variable,
1485 CodeAssemblerLabel::Type type = CodeAssemblerLabel::kNonDeferred)
1486 : CodeAssemblerLabel(assembler, 1, &merged_variable, type) {}
1487 ~CodeAssemblerLabel();
1488
1489 // Cannot be copied because the destructor explicitly call the destructor of
1490 // the underlying {RawMachineLabel}, hence only one pointer can point to it.
1491 CodeAssemblerLabel(const CodeAssemblerLabel&) = delete;
1492 CodeAssemblerLabel& operator=(const CodeAssemblerLabel&) = delete;
1493
is_bound()1494 inline bool is_bound() const { return bound_; }
is_used()1495 inline bool is_used() const { return merge_count_ != 0; }
1496
1497 private:
1498 friend class CodeAssembler;
1499
1500 void Bind();
1501 #if DEBUG
1502 void Bind(AssemblerDebugInfo debug_info);
1503 #endif // DEBUG
1504 void UpdateVariablesAfterBind();
1505 void MergeVariables();
1506
1507 bool bound_;
1508 size_t merge_count_;
1509 CodeAssemblerState* state_;
1510 RawMachineLabel* label_;
1511 // Map of variables that need to be merged to their phi nodes (or placeholders
1512 // for those phis).
1513 std::map<CodeAssemblerVariable::Impl*, Node*,
1514 CodeAssemblerVariable::ImplComparator>
1515 variable_phis_;
1516 // Map of variables to the list of value nodes that have been added from each
1517 // merge path in their order of merging.
1518 std::map<CodeAssemblerVariable::Impl*, std::vector<Node*>,
1519 CodeAssemblerVariable::ImplComparator>
1520 variable_merges_;
1521 };
1522
1523 class CodeAssemblerParameterizedLabelBase {
1524 public:
is_used()1525 bool is_used() const { return plain_label_.is_used(); }
CodeAssemblerParameterizedLabelBase(CodeAssembler * assembler,size_t arity,CodeAssemblerLabel::Type type)1526 explicit CodeAssemblerParameterizedLabelBase(CodeAssembler* assembler,
1527 size_t arity,
1528 CodeAssemblerLabel::Type type)
1529 : state_(assembler->state()),
1530 phi_inputs_(arity),
1531 plain_label_(assembler, type) {}
1532
1533 protected:
plain_label()1534 CodeAssemblerLabel* plain_label() { return &plain_label_; }
1535 void AddInputs(std::vector<Node*> inputs);
1536 Node* CreatePhi(MachineRepresentation rep, const std::vector<Node*>& inputs);
1537 const std::vector<Node*>& CreatePhis(
1538 std::vector<MachineRepresentation> representations);
1539
1540 private:
1541 CodeAssemblerState* state_;
1542 std::vector<std::vector<Node*>> phi_inputs_;
1543 std::vector<Node*> phi_nodes_;
1544 CodeAssemblerLabel plain_label_;
1545 };
1546
1547 template <class... Types>
1548 class CodeAssemblerParameterizedLabel
1549 : public CodeAssemblerParameterizedLabelBase {
1550 public:
1551 static constexpr size_t kArity = sizeof...(Types);
CodeAssemblerParameterizedLabel(CodeAssembler * assembler,CodeAssemblerLabel::Type type)1552 explicit CodeAssemblerParameterizedLabel(CodeAssembler* assembler,
1553 CodeAssemblerLabel::Type type)
1554 : CodeAssemblerParameterizedLabelBase(assembler, kArity, type) {}
1555
1556 private:
1557 friend class CodeAssembler;
1558
AddInputsVector(std::vector<Node * > inputs)1559 void AddInputsVector(std::vector<Node*> inputs) {
1560 CodeAssemblerParameterizedLabelBase::AddInputs(std::move(inputs));
1561 }
AddInputs(TNode<Types>...inputs)1562 void AddInputs(TNode<Types>... inputs) {
1563 CodeAssemblerParameterizedLabelBase::AddInputs(
1564 std::vector<Node*>{inputs...});
1565 }
CreatePhis(TNode<Types> * ...results)1566 void CreatePhis(TNode<Types>*... results) {
1567 const std::vector<Node*>& phi_nodes =
1568 CodeAssemblerParameterizedLabelBase::CreatePhis(
1569 {PhiMachineRepresentationOf<Types>...});
1570 auto it = phi_nodes.begin();
1571 USE(it);
1572 ITERATE_PACK(AssignPhi(results, *(it++)));
1573 }
1574 template <class T>
AssignPhi(TNode<T> * result,Node * phi)1575 static void AssignPhi(TNode<T>* result, Node* phi) {
1576 if (phi != nullptr) *result = TNode<T>::UncheckedCast(phi);
1577 }
1578 };
1579
1580 using CodeAssemblerExceptionHandlerLabel =
1581 CodeAssemblerParameterizedLabel<Object>;
1582
1583 class V8_EXPORT_PRIVATE CodeAssemblerState {
1584 public:
1585 // Create with CallStub linkage.
1586 // |result_size| specifies the number of results returned by the stub.
1587 // TODO(rmcilroy): move result_size to the CallInterfaceDescriptor.
1588 CodeAssemblerState(Isolate* isolate, Zone* zone,
1589 const CallInterfaceDescriptor& descriptor, CodeKind kind,
1590 const char* name, Builtin builtin = Builtin::kNoBuiltinId);
1591
1592 // Create with JSCall linkage.
1593 CodeAssemblerState(Isolate* isolate, Zone* zone, int parameter_count,
1594 CodeKind kind, const char* name,
1595 Builtin builtin = Builtin::kNoBuiltinId);
1596
1597 ~CodeAssemblerState();
1598
1599 CodeAssemblerState(const CodeAssemblerState&) = delete;
1600 CodeAssemblerState& operator=(const CodeAssemblerState&) = delete;
1601
name()1602 const char* name() const { return name_; }
1603 int parameter_count() const;
1604
1605 #if DEBUG
1606 void PrintCurrentBlock(std::ostream& os);
1607 #endif // DEBUG
1608 bool InsideBlock();
1609 void SetInitialDebugInformation(const char* msg, const char* file, int line);
1610
1611 private:
1612 friend class CodeAssembler;
1613 friend class CodeAssemblerLabel;
1614 friend class CodeAssemblerVariable;
1615 friend class CodeAssemblerTester;
1616 friend class CodeAssemblerParameterizedLabelBase;
1617 friend class ScopedExceptionHandler;
1618
1619 CodeAssemblerState(Isolate* isolate, Zone* zone,
1620 CallDescriptor* call_descriptor, CodeKind kind,
1621 const char* name, Builtin builtin);
1622
1623 void PushExceptionHandler(CodeAssemblerExceptionHandlerLabel* label);
1624 void PopExceptionHandler();
1625
1626 std::unique_ptr<RawMachineAssembler> raw_assembler_;
1627 CodeKind kind_;
1628 const char* name_;
1629 Builtin builtin_;
1630 bool code_generated_;
1631 ZoneSet<CodeAssemblerVariable::Impl*, CodeAssemblerVariable::ImplComparator>
1632 variables_;
1633 CodeAssemblerCallback call_prologue_;
1634 CodeAssemblerCallback call_epilogue_;
1635 std::vector<CodeAssemblerExceptionHandlerLabel*> exception_handler_labels_;
1636 using VariableId = uint32_t;
1637 VariableId next_variable_id_ = 0;
1638 JSGraph* jsgraph_;
1639
1640 // Only used by CodeStubAssembler builtins.
1641 std::vector<FileAndLine> macro_call_stack_;
1642
NextVariableId()1643 VariableId NextVariableId() { return next_variable_id_++; }
1644 };
1645
1646 class V8_EXPORT_PRIVATE V8_NODISCARD ScopedExceptionHandler {
1647 public:
1648 ScopedExceptionHandler(CodeAssembler* assembler,
1649 CodeAssemblerExceptionHandlerLabel* label);
1650
1651 // Use this constructor for compatability/ports of old CSA code only. New code
1652 // should use the CodeAssemblerExceptionHandlerLabel version.
1653 ScopedExceptionHandler(CodeAssembler* assembler, CodeAssemblerLabel* label,
1654 TypedCodeAssemblerVariable<Object>* exception);
1655
1656 ~ScopedExceptionHandler();
1657
1658 private:
1659 bool has_handler_;
1660 CodeAssembler* assembler_;
1661 CodeAssemblerLabel* compatibility_label_;
1662 std::unique_ptr<CodeAssemblerExceptionHandlerLabel> label_;
1663 TypedCodeAssemblerVariable<Object>* exception_;
1664 };
1665
1666 } // namespace compiler
1667 } // namespace internal
1668 } // namespace v8
1669
1670 #endif // V8_COMPILER_CODE_ASSEMBLER_H_
1671