blob: 0c2b7e8e21c95b2276bbd7d8c61c263e48dad49d [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// Copyright (c) 1994-2006 Sun Microsystems Inc.
2// 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 are
6// met:
7//
8// - Redistributions of source code must retain the above copyright notice,
9// this list of conditions and the following disclaimer.
10//
11// - Redistribution in binary form must reproduce the above copyright
12// notice, this list of conditions and the following disclaimer in the
13// documentation and/or other materials provided with the distribution.
14//
15// - Neither the name of Sun Microsystems or the names of contributors may
16// be used to endorse or promote products derived from this software without
17// specific prior written permission.
18//
19// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
20// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31// The original source code covered by the above license above has been
32// modified significantly by Google Inc.
Ben Murdoch3ef787d2012-04-12 10:51:47 +010033// Copyright 2012 the V8 project authors. All rights reserved.
Steve Blocka7e24c12009-10-30 11:49:00 +000034
35#ifndef V8_ASSEMBLER_H_
36#define V8_ASSEMBLER_H_
37
Ben Murdochb8a8cc12014-11-26 15:28:44 +000038#include "src/allocation.h"
39#include "src/builtins.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000040#include "src/isolate.h"
Ben Murdochda12d292016-06-02 14:46:10 +010041#include "src/log.h"
Ben Murdoch61f157c2016-09-16 13:49:30 +010042#include "src/register-configuration.h"
Emily Bernierd0a1eb72015-03-24 16:35:39 -040043#include "src/runtime/runtime.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000044
45namespace v8 {
Ben Murdoch3ef787d2012-04-12 10:51:47 +010046
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000047// Forward declarations.
Ben Murdoch3ef787d2012-04-12 10:51:47 +010048class ApiFunction;
49
Steve Blocka7e24c12009-10-30 11:49:00 +000050namespace internal {
51
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000052// Forward declarations.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000053class StatsCounter;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000054
Steve Blocka7e24c12009-10-30 11:49:00 +000055// -----------------------------------------------------------------------------
Steve Block44f0eee2011-05-26 01:26:41 +010056// Platform independent assembler base class.
57
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000058enum class CodeObjectRequired { kNo, kYes };
59
60
Steve Block44f0eee2011-05-26 01:26:41 +010061class AssemblerBase: public Malloced {
62 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +000063 AssemblerBase(Isolate* isolate, void* buffer, int buffer_size);
64 virtual ~AssemblerBase();
Steve Block44f0eee2011-05-26 01:26:41 +010065
66 Isolate* isolate() const { return isolate_; }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000067 int jit_cookie() const { return jit_cookie_; }
68
69 bool emit_debug_code() const { return emit_debug_code_; }
70 void set_emit_debug_code(bool value) { emit_debug_code_ = value; }
71
72 bool serializer_enabled() const { return serializer_enabled_; }
73 void enable_serializer() { serializer_enabled_ = true; }
74
75 bool predictable_code_size() const { return predictable_code_size_; }
76 void set_predictable_code_size(bool value) { predictable_code_size_ = value; }
77
78 uint64_t enabled_cpu_features() const { return enabled_cpu_features_; }
79 void set_enabled_cpu_features(uint64_t features) {
80 enabled_cpu_features_ = features;
81 }
82 bool IsEnabled(CpuFeature f) {
83 return (enabled_cpu_features_ & (static_cast<uint64_t>(1) << f)) != 0;
84 }
Steve Block44f0eee2011-05-26 01:26:41 +010085
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000086 bool is_constant_pool_available() const {
87 if (FLAG_enable_embedded_constant_pool) {
88 return constant_pool_available_;
Emily Bernierd0a1eb72015-03-24 16:35:39 -040089 } else {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000090 // Embedded constant pool not supported on this architecture.
Emily Bernierd0a1eb72015-03-24 16:35:39 -040091 UNREACHABLE();
92 return false;
93 }
94 }
95
Ben Murdochdb1b4382012-04-26 19:03:50 +010096 // Overwrite a host NaN with a quiet target NaN. Used by mksnapshot for
97 // cross-snapshotting.
98 static void QuietNaN(HeapObject* nan) { }
99
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000100 int pc_offset() const { return static_cast<int>(pc_ - buffer_); }
101
102 // This function is called when code generation is aborted, so that
103 // the assembler could clean up internal data structures.
104 virtual void AbortedCodeGeneration() { }
105
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000106 // Debugging
107 void Print();
108
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000109 static const int kMinimalBufferSize = 4*KB;
110
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000111 static void FlushICache(Isolate* isolate, void* start, size_t size);
112
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000113 protected:
114 // The buffer into which code and relocation info are generated. It could
115 // either be owned by the assembler or be provided externally.
116 byte* buffer_;
117 int buffer_size_;
118 bool own_buffer_;
119
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000120 void set_constant_pool_available(bool available) {
121 if (FLAG_enable_embedded_constant_pool) {
122 constant_pool_available_ = available;
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400123 } else {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000124 // Embedded constant pool not supported on this architecture.
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400125 UNREACHABLE();
126 }
127 }
128
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000129 // The program counter, which points into the buffer above and moves forward.
130 byte* pc_;
131
Steve Block44f0eee2011-05-26 01:26:41 +0100132 private:
133 Isolate* isolate_;
Steve Block053d10c2011-06-13 19:13:29 +0100134 int jit_cookie_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000135 uint64_t enabled_cpu_features_;
136 bool emit_debug_code_;
137 bool predictable_code_size_;
138 bool serializer_enabled_;
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400139
140 // Indicates whether the constant pool can be accessed, which is only possible
141 // if the pp register points to the current code object's constant pool.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000142 bool constant_pool_available_;
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400143
144 // Constant pool.
145 friend class FrameAndConstantPoolScope;
146 friend class ConstantPoolUnavailableScope;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000147};
148
149
150// Avoids emitting debug code during the lifetime of this scope object.
151class DontEmitDebugCodeScope BASE_EMBEDDED {
152 public:
153 explicit DontEmitDebugCodeScope(AssemblerBase* assembler)
154 : assembler_(assembler), old_value_(assembler->emit_debug_code()) {
155 assembler_->set_emit_debug_code(false);
156 }
157 ~DontEmitDebugCodeScope() {
158 assembler_->set_emit_debug_code(old_value_);
159 }
160 private:
161 AssemblerBase* assembler_;
162 bool old_value_;
163};
164
165
166// Avoids using instructions that vary in size in unpredictable ways between the
167// snapshot and the running VM.
168class PredictableCodeSizeScope {
169 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000170 explicit PredictableCodeSizeScope(AssemblerBase* assembler);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000171 PredictableCodeSizeScope(AssemblerBase* assembler, int expected_size);
172 ~PredictableCodeSizeScope();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000173 void ExpectSize(int expected_size) { expected_size_ = expected_size; }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000174
175 private:
176 AssemblerBase* assembler_;
177 int expected_size_;
178 int start_offset_;
179 bool old_value_;
180};
181
182
183// Enable a specified feature within a scope.
184class CpuFeatureScope BASE_EMBEDDED {
185 public:
186#ifdef DEBUG
187 CpuFeatureScope(AssemblerBase* assembler, CpuFeature f);
188 ~CpuFeatureScope();
189
190 private:
191 AssemblerBase* assembler_;
192 uint64_t old_enabled_;
193#else
194 CpuFeatureScope(AssemblerBase* assembler, CpuFeature f) {}
195#endif
196};
197
198
199// CpuFeatures keeps track of which features are supported by the target CPU.
200// Supported features must be enabled by a CpuFeatureScope before use.
201// Example:
202// if (assembler->IsSupported(SSE3)) {
203// CpuFeatureScope fscope(assembler, SSE3);
204// // Generate code containing SSE3 instructions.
205// } else {
206// // Generate alternative code.
207// }
208class CpuFeatures : public AllStatic {
209 public:
210 static void Probe(bool cross_compile) {
211 STATIC_ASSERT(NUMBER_OF_CPU_FEATURES <= kBitsPerInt);
212 if (initialized_) return;
213 initialized_ = true;
214 ProbeImpl(cross_compile);
215 }
216
217 static unsigned SupportedFeatures() {
218 Probe(false);
219 return supported_;
220 }
221
222 static bool IsSupported(CpuFeature f) {
223 return (supported_ & (1u << f)) != 0;
224 }
225
226 static inline bool SupportsCrankshaft();
227
Ben Murdoch097c5b22016-05-18 11:27:45 +0100228 static inline unsigned icache_line_size() {
229 DCHECK(icache_line_size_ != 0);
230 return icache_line_size_;
231 }
232
233 static inline unsigned dcache_line_size() {
234 DCHECK(dcache_line_size_ != 0);
235 return dcache_line_size_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000236 }
237
238 static void PrintTarget();
239 static void PrintFeatures();
240
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000241 private:
242 friend class ExternalReference;
243 friend class AssemblerBase;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000244 // Flush instruction cache.
245 static void FlushICache(void* start, size_t size);
246
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000247 // Platform-dependent implementation.
248 static void ProbeImpl(bool cross_compile);
249
250 static unsigned supported_;
Ben Murdoch097c5b22016-05-18 11:27:45 +0100251 static unsigned icache_line_size_;
252 static unsigned dcache_line_size_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000253 static bool initialized_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000254 DISALLOW_COPY_AND_ASSIGN(CpuFeatures);
Steve Block44f0eee2011-05-26 01:26:41 +0100255};
256
Ben Murdochb0fe1622011-05-05 13:52:32 +0100257
258// -----------------------------------------------------------------------------
Steve Blocka7e24c12009-10-30 11:49:00 +0000259// Labels represent pc locations; they are typically jump or call targets.
260// After declaration, a label can be freely used to denote known or (yet)
261// unknown pc location. Assembler::bind() is used to bind a label to the
262// current pc. A label can be bound only once.
263
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400264class Label {
Steve Blocka7e24c12009-10-30 11:49:00 +0000265 public:
Ben Murdoch257744e2011-11-30 15:57:28 +0000266 enum Distance {
267 kNear, kFar
268 };
269
270 INLINE(Label()) {
271 Unuse();
272 UnuseNear();
273 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000274
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000275 INLINE(~Label()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000276 DCHECK(!is_linked());
277 DCHECK(!is_near_linked());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000278 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000279
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000280 INLINE(void Unuse()) { pos_ = 0; }
281 INLINE(void UnuseNear()) { near_link_pos_ = 0; }
282
283 INLINE(bool is_bound() const) { return pos_ < 0; }
284 INLINE(bool is_unused() const) { return pos_ == 0 && near_link_pos_ == 0; }
285 INLINE(bool is_linked() const) { return pos_ > 0; }
Ben Murdoch257744e2011-11-30 15:57:28 +0000286 INLINE(bool is_near_linked() const) { return near_link_pos_ > 0; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000287
288 // Returns the position of bound or linked labels. Cannot be used
289 // for unused labels.
290 int pos() const;
Ben Murdoch257744e2011-11-30 15:57:28 +0000291 int near_link_pos() const { return near_link_pos_ - 1; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000292
293 private:
294 // pos_ encodes both the binding state (via its sign)
295 // and the binding position (via its value) of a label.
296 //
297 // pos_ < 0 bound label, pos() returns the jump target position
298 // pos_ == 0 unused label
299 // pos_ > 0 linked label, pos() returns the last reference position
300 int pos_;
301
Ben Murdoch257744e2011-11-30 15:57:28 +0000302 // Behaves like |pos_| in the "> 0" case, but for near jumps to this label.
303 int near_link_pos_;
304
Steve Blocka7e24c12009-10-30 11:49:00 +0000305 void bind_to(int pos) {
306 pos_ = -pos - 1;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000307 DCHECK(is_bound());
Steve Blocka7e24c12009-10-30 11:49:00 +0000308 }
Ben Murdoch257744e2011-11-30 15:57:28 +0000309 void link_to(int pos, Distance distance = kFar) {
310 if (distance == kNear) {
311 near_link_pos_ = pos + 1;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000312 DCHECK(is_near_linked());
Ben Murdoch257744e2011-11-30 15:57:28 +0000313 } else {
314 pos_ = pos + 1;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000315 DCHECK(is_linked());
Ben Murdoch257744e2011-11-30 15:57:28 +0000316 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000317 }
318
319 friend class Assembler;
Steve Blocka7e24c12009-10-30 11:49:00 +0000320 friend class Displacement;
Steve Blocka7e24c12009-10-30 11:49:00 +0000321 friend class RegExpMacroAssemblerIrregexp;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000322
323#if V8_TARGET_ARCH_ARM64
324 // On ARM64, the Assembler keeps track of pointers to Labels to resolve
325 // branches to distant targets. Copying labels would confuse the Assembler.
326 DISALLOW_COPY_AND_ASSIGN(Label); // NOLINT
327#endif
Steve Blocka7e24c12009-10-30 11:49:00 +0000328};
329
330
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100331enum SaveFPRegsMode { kDontSaveFPRegs, kSaveFPRegs };
332
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000333enum ArgvMode { kArgvOnStack, kArgvInRegister };
334
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000335// Specifies whether to perform icache flush operations on RelocInfo updates.
336// If FLUSH_ICACHE_IF_NEEDED, the icache will always be flushed if an
337// instruction was modified. If SKIP_ICACHE_FLUSH the flush will always be
338// skipped (only use this if you will flush the icache manually before it is
339// executed).
340enum ICacheFlushMode { FLUSH_ICACHE_IF_NEEDED, SKIP_ICACHE_FLUSH };
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100341
Steve Blocka7e24c12009-10-30 11:49:00 +0000342// -----------------------------------------------------------------------------
343// Relocation information
344
345
346// Relocation information consists of the address (pc) of the datum
347// to which the relocation information applies, the relocation mode
348// (rmode), and an optional data field. The relocation mode may be
349// "descriptive" and not indicate a need for relocation, but simply
350// describe a property of the datum. Such rmodes are useful for GC
351// and nice disassembly output.
352
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000353class RelocInfo {
Steve Blocka7e24c12009-10-30 11:49:00 +0000354 public:
355 // The constant kNoPosition is used with the collecting of source positions
356 // in the relocation information. Two types of source positions are collected
357 // "position" (RelocMode position) and "statement position" (RelocMode
358 // statement_position). The "position" is collected at places in the source
359 // code which are of interest when making stack traces to pin-point the source
360 // location of a stack frame as close as possible. The "statement position" is
361 // collected at the beginning at each statement, and is used to indicate
362 // possible break locations. kNoPosition is used to indicate an
363 // invalid/uninitialized position value.
364 static const int kNoPosition = -1;
365
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100366 // This string is used to add padding comments to the reloc info in cases
367 // where we are not sure to have enough space for patching in during
368 // lazy deoptimization. This is the case if we have indirect calls for which
369 // we do not normally record relocation info.
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000370 static const char* const kFillerCommentString;
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100371
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000372 // The minimum size of a comment is equal to two bytes for the extra tagged
373 // pc and kPointerSize for the actual pointer to the comment.
374 static const int kMinRelocCommentSize = 2 + kPointerSize;
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100375
376 // The maximum size for a call instruction including pc-jump.
377 static const int kMaxCallSize = 6;
378
Steve Block44f0eee2011-05-26 01:26:41 +0100379 // The maximum pc delta that will use the short encoding.
380 static const int kMaxSmallPCDelta;
381
Steve Blocka7e24c12009-10-30 11:49:00 +0000382 enum Mode {
383 // Please note the order is important (see IsCodeTarget, IsGCRelocMode).
Ben Murdoch257744e2011-11-30 15:57:28 +0000384 CODE_TARGET, // Code target which is not any of the above.
385 CODE_TARGET_WITH_ID,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000386 DEBUGGER_STATEMENT, // Code target for the debugger statement.
Steve Blocka7e24c12009-10-30 11:49:00 +0000387 EMBEDDED_OBJECT,
Ben Murdochda12d292016-06-02 14:46:10 +0100388 // To relocate pointers into the wasm memory embedded in wasm code
389 WASM_MEMORY_REFERENCE,
Ben Murdoch61f157c2016-09-16 13:49:30 +0100390 WASM_GLOBAL_REFERENCE,
Ben Murdochc5610432016-08-08 18:44:38 +0100391 WASM_MEMORY_SIZE_REFERENCE,
392 CELL,
Ben Murdochb0fe1622011-05-05 13:52:32 +0100393
Steve Blocka7e24c12009-10-30 11:49:00 +0000394 // Everything after runtime_entry (inclusive) is not GC'ed.
395 RUNTIME_ENTRY,
Steve Blocka7e24c12009-10-30 11:49:00 +0000396 COMMENT,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000397 POSITION, // See comment for kNoPosition above.
Steve Blocka7e24c12009-10-30 11:49:00 +0000398 STATEMENT_POSITION, // See comment for kNoPosition above.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000399
400 // Additional code inserted for debug break slot.
401 DEBUG_BREAK_SLOT_AT_POSITION,
402 DEBUG_BREAK_SLOT_AT_RETURN,
403 DEBUG_BREAK_SLOT_AT_CALL,
Ben Murdochda12d292016-06-02 14:46:10 +0100404 DEBUG_BREAK_SLOT_AT_TAIL_CALL,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000405
Steve Blocka7e24c12009-10-30 11:49:00 +0000406 EXTERNAL_REFERENCE, // The address of an external C++ function.
407 INTERNAL_REFERENCE, // An address inside the same function.
408
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000409 // Encoded internal reference, used only on MIPS, MIPS64 and PPC.
410 INTERNAL_REFERENCE_ENCODED,
411
412 // Continuation points for a generator yield.
413 GENERATOR_CONTINUATION,
414
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000415 // Marks constant and veneer pools. Only used on ARM and ARM64.
416 // They use a custom noncompact encoding.
417 CONST_POOL,
418 VENEER_POOL,
419
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000420 DEOPT_REASON, // Deoptimization reason index.
Ben Murdochc5610432016-08-08 18:44:38 +0100421 DEOPT_ID, // Deoptimization inlining id.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000422
423 // This is not an actual reloc mode, but used to encode a long pc jump that
424 // cannot be encoded as part of another record.
425 PC_JUMP,
426
Steve Blocka7e24c12009-10-30 11:49:00 +0000427 // Pseudo-types
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000428 NUMBER_OF_MODES,
429 NONE32, // never recorded 32-bit value
430 NONE64, // never recorded 64-bit value
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000431 CODE_AGE_SEQUENCE, // Not stored in RelocInfo array, used explictly by
432 // code aging.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000433
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000434 FIRST_REAL_RELOC_MODE = CODE_TARGET,
435 LAST_REAL_RELOC_MODE = VENEER_POOL,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000436 LAST_CODE_ENUM = DEBUGGER_STATEMENT,
Ben Murdochc5610432016-08-08 18:44:38 +0100437 LAST_GCED_ENUM = WASM_MEMORY_SIZE_REFERENCE,
Ben Murdochda12d292016-06-02 14:46:10 +0100438 FIRST_SHAREABLE_RELOC_MODE = CELL,
Steve Blocka7e24c12009-10-30 11:49:00 +0000439 };
440
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000441 STATIC_ASSERT(NUMBER_OF_MODES <= kBitsPerInt);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100442
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000443 explicit RelocInfo(Isolate* isolate) : isolate_(isolate) {
444 DCHECK_NOT_NULL(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000445 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000446
447 RelocInfo(Isolate* isolate, byte* pc, Mode rmode, intptr_t data, Code* host)
448 : isolate_(isolate), pc_(pc), rmode_(rmode), data_(data), host_(host) {
449 DCHECK_NOT_NULL(isolate);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000450 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000451
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000452 static inline bool IsRealRelocMode(Mode mode) {
Ben Murdoch61f157c2016-09-16 13:49:30 +0100453 return mode >= FIRST_REAL_RELOC_MODE && mode <= LAST_REAL_RELOC_MODE;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000454 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000455 static inline bool IsCodeTarget(Mode mode) {
456 return mode <= LAST_CODE_ENUM;
457 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100458 static inline bool IsEmbeddedObject(Mode mode) {
459 return mode == EMBEDDED_OBJECT;
460 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000461 static inline bool IsCell(Mode mode) { return mode == CELL; }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000462 static inline bool IsRuntimeEntry(Mode mode) {
463 return mode == RUNTIME_ENTRY;
464 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000465 // Is the relocation mode affected by GC?
466 static inline bool IsGCRelocMode(Mode mode) {
467 return mode <= LAST_GCED_ENUM;
468 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000469 static inline bool IsComment(Mode mode) {
470 return mode == COMMENT;
471 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000472 static inline bool IsConstPool(Mode mode) {
473 return mode == CONST_POOL;
474 }
475 static inline bool IsVeneerPool(Mode mode) {
476 return mode == VENEER_POOL;
477 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000478 static inline bool IsDeoptReason(Mode mode) {
479 return mode == DEOPT_REASON;
480 }
Ben Murdochc5610432016-08-08 18:44:38 +0100481 static inline bool IsDeoptId(Mode mode) {
482 return mode == DEOPT_ID;
483 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000484 static inline bool IsPosition(Mode mode) {
485 return mode == POSITION || mode == STATEMENT_POSITION;
486 }
487 static inline bool IsStatementPosition(Mode mode) {
488 return mode == STATEMENT_POSITION;
489 }
490 static inline bool IsExternalReference(Mode mode) {
491 return mode == EXTERNAL_REFERENCE;
492 }
493 static inline bool IsInternalReference(Mode mode) {
494 return mode == INTERNAL_REFERENCE;
495 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000496 static inline bool IsInternalReferenceEncoded(Mode mode) {
497 return mode == INTERNAL_REFERENCE_ENCODED;
498 }
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100499 static inline bool IsDebugBreakSlot(Mode mode) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000500 return IsDebugBreakSlotAtPosition(mode) || IsDebugBreakSlotAtReturn(mode) ||
Ben Murdochda12d292016-06-02 14:46:10 +0100501 IsDebugBreakSlotAtCall(mode) || IsDebugBreakSlotAtTailCall(mode);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000502 }
503 static inline bool IsDebugBreakSlotAtPosition(Mode mode) {
504 return mode == DEBUG_BREAK_SLOT_AT_POSITION;
505 }
506 static inline bool IsDebugBreakSlotAtReturn(Mode mode) {
507 return mode == DEBUG_BREAK_SLOT_AT_RETURN;
508 }
509 static inline bool IsDebugBreakSlotAtCall(Mode mode) {
510 return mode == DEBUG_BREAK_SLOT_AT_CALL;
511 }
Ben Murdochda12d292016-06-02 14:46:10 +0100512 static inline bool IsDebugBreakSlotAtTailCall(Mode mode) {
513 return mode == DEBUG_BREAK_SLOT_AT_TAIL_CALL;
514 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000515 static inline bool IsDebuggerStatement(Mode mode) {
516 return mode == DEBUGGER_STATEMENT;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100517 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000518 static inline bool IsNone(Mode mode) {
519 return mode == NONE32 || mode == NONE64;
520 }
521 static inline bool IsCodeAgeSequence(Mode mode) {
522 return mode == CODE_AGE_SEQUENCE;
523 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000524 static inline bool IsGeneratorContinuation(Mode mode) {
525 return mode == GENERATOR_CONTINUATION;
526 }
Ben Murdochda12d292016-06-02 14:46:10 +0100527 static inline bool IsWasmMemoryReference(Mode mode) {
528 return mode == WASM_MEMORY_REFERENCE;
529 }
Ben Murdochc5610432016-08-08 18:44:38 +0100530 static inline bool IsWasmMemorySizeReference(Mode mode) {
531 return mode == WASM_MEMORY_SIZE_REFERENCE;
532 }
Ben Murdoch61f157c2016-09-16 13:49:30 +0100533 static inline bool IsWasmGlobalReference(Mode mode) {
534 return mode == WASM_GLOBAL_REFERENCE;
535 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000536 static inline int ModeMask(Mode mode) { return 1 << mode; }
537
538 // Accessors
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000539 Isolate* isolate() const { return isolate_; }
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100540 byte* pc() const { return pc_; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000541 void set_pc(byte* pc) { pc_ = pc; }
542 Mode rmode() const { return rmode_; }
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100543 intptr_t data() const { return data_; }
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100544 Code* host() const { return host_; }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000545 void set_host(Code* host) { host_ = host; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000546
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000547 // Apply a relocation by delta bytes. When the code object is moved, PC
548 // relative addresses have to be updated as well as absolute addresses
549 // inside the code (internal references).
550 // Do not forget to flush the icache afterwards!
551 INLINE(void apply(intptr_t delta));
Steve Blocka7e24c12009-10-30 11:49:00 +0000552
Leon Clarkef7060e22010-06-03 12:02:55 +0100553 // Is the pointer this relocation info refers to coded like a plain pointer
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100554 // or is it strange in some way (e.g. relative or patched into a series of
Leon Clarkef7060e22010-06-03 12:02:55 +0100555 // instructions).
556 bool IsCodedSpecially();
557
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000558 // If true, the pointer this relocation info refers to is an entry in the
559 // constant pool, otherwise the pointer is embedded in the instruction stream.
560 bool IsInConstantPool();
561
Ben Murdochc5610432016-08-08 18:44:38 +0100562 Address wasm_memory_reference();
Ben Murdoch61f157c2016-09-16 13:49:30 +0100563 Address wasm_global_reference();
Ben Murdochc5610432016-08-08 18:44:38 +0100564 uint32_t wasm_memory_size_reference();
565 void update_wasm_memory_reference(
566 Address old_base, Address new_base, uint32_t old_size, uint32_t new_size,
567 ICacheFlushMode icache_flush_mode = SKIP_ICACHE_FLUSH);
Ben Murdoch61f157c2016-09-16 13:49:30 +0100568 void update_wasm_global_reference(
569 Address old_base, Address new_base,
570 ICacheFlushMode icache_flush_mode = SKIP_ICACHE_FLUSH);
Ben Murdochc5610432016-08-08 18:44:38 +0100571
Steve Blocka7e24c12009-10-30 11:49:00 +0000572 // this relocation applies to;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000573 // can only be called if IsCodeTarget(rmode_) || IsRuntimeEntry(rmode_)
Steve Blocka7e24c12009-10-30 11:49:00 +0000574 INLINE(Address target_address());
Ben Murdoch61f157c2016-09-16 13:49:30 +0100575 INLINE(void set_target_address(
576 Address target,
577 WriteBarrierMode write_barrier_mode = UPDATE_WRITE_BARRIER,
578 ICacheFlushMode icache_flush_mode = FLUSH_ICACHE_IF_NEEDED));
Steve Blocka7e24c12009-10-30 11:49:00 +0000579 INLINE(Object* target_object());
Steve Block3ce2e202009-11-05 08:53:23 +0000580 INLINE(Handle<Object> target_object_handle(Assembler* origin));
Ben Murdoch61f157c2016-09-16 13:49:30 +0100581 INLINE(void set_target_object(
582 Object* target,
583 WriteBarrierMode write_barrier_mode = UPDATE_WRITE_BARRIER,
584 ICacheFlushMode icache_flush_mode = FLUSH_ICACHE_IF_NEEDED));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000585 INLINE(Address target_runtime_entry(Assembler* origin));
Ben Murdoch61f157c2016-09-16 13:49:30 +0100586 INLINE(void set_target_runtime_entry(
587 Address target,
588 WriteBarrierMode write_barrier_mode = UPDATE_WRITE_BARRIER,
589 ICacheFlushMode icache_flush_mode = FLUSH_ICACHE_IF_NEEDED));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000590 INLINE(Cell* target_cell());
591 INLINE(Handle<Cell> target_cell_handle());
Ben Murdoch61f157c2016-09-16 13:49:30 +0100592 INLINE(void set_target_cell(
593 Cell* cell, WriteBarrierMode write_barrier_mode = UPDATE_WRITE_BARRIER,
594 ICacheFlushMode icache_flush_mode = FLUSH_ICACHE_IF_NEEDED));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000595 INLINE(Handle<Object> code_age_stub_handle(Assembler* origin));
596 INLINE(Code* code_age_stub());
Ben Murdoch61f157c2016-09-16 13:49:30 +0100597 INLINE(void set_code_age_stub(
598 Code* stub, ICacheFlushMode icache_flush_mode = FLUSH_ICACHE_IF_NEEDED));
Ben Murdochb0fe1622011-05-05 13:52:32 +0100599
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000600 // Returns the address of the constant pool entry where the target address
601 // is held. This should only be called if IsInConstantPool returns true.
602 INLINE(Address constant_pool_entry_address());
Steve Blocka7e24c12009-10-30 11:49:00 +0000603
Leon Clarkef7060e22010-06-03 12:02:55 +0100604 // Read the address of the word containing the target_address in an
605 // instruction stream. What this means exactly is architecture-independent.
606 // The only architecture-independent user of this function is the serializer.
607 // The serializer uses it to find out how many raw bytes of instruction to
608 // output before the next target. Architecture-independent code shouldn't
609 // dereference the pointer it gets back from this.
Steve Blocka7e24c12009-10-30 11:49:00 +0000610 INLINE(Address target_address_address());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000611
Leon Clarkef7060e22010-06-03 12:02:55 +0100612 // This indicates how much space a target takes up when deserializing a code
613 // stream. For most architectures this is just the size of a pointer. For
614 // an instruction like movw/movt where the target bits are mixed into the
615 // instruction bits the size of the target will be zero, indicating that the
616 // serializer should not step forwards in memory after a target is resolved
617 // and written. In this case the target_address_address function above
618 // should return the end of the instructions to be patched, allowing the
619 // deserializer to deserialize the instructions as raw bytes and put them in
620 // place, ready to be patched with the target.
621 INLINE(int target_address_size());
Steve Blocka7e24c12009-10-30 11:49:00 +0000622
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000623 // Read the reference in the instruction this relocation
624 // applies to; can only be called if rmode_ is EXTERNAL_REFERENCE.
625 INLINE(Address target_external_reference());
626
627 // Read the reference in the instruction this relocation
628 // applies to; can only be called if rmode_ is INTERNAL_REFERENCE.
629 INLINE(Address target_internal_reference());
630
631 // Return the reference address this relocation applies to;
632 // can only be called if rmode_ is INTERNAL_REFERENCE.
633 INLINE(Address target_internal_reference_address());
Steve Blocka7e24c12009-10-30 11:49:00 +0000634
635 // Read/modify the address of a call instruction. This is used to relocate
636 // the break points where straight-line code is patched with a call
637 // instruction.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000638 INLINE(Address debug_call_address());
639 INLINE(void set_debug_call_address(Address target));
Steve Blocka7e24c12009-10-30 11:49:00 +0000640
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000641 // Wipe out a relocation to a fixed value, used for making snapshots
642 // reproducible.
643 INLINE(void WipeOut());
644
Steve Block44f0eee2011-05-26 01:26:41 +0100645 template<typename StaticVisitor> inline void Visit(Heap* heap);
Ben Murdochc5610432016-08-08 18:44:38 +0100646
647 template <typename ObjectVisitor>
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000648 inline void Visit(Isolate* isolate, ObjectVisitor* v);
Leon Clarkef7060e22010-06-03 12:02:55 +0100649
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100650 // Check whether this debug break slot has been patched with a call to the
651 // debugger.
Ben Murdoch097c5b22016-05-18 11:27:45 +0100652 bool IsPatchedDebugBreakSlotSequence();
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100653
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000654#ifdef DEBUG
655 // Check whether the given code contains relocation information that
656 // either is position-relative or movable by the garbage collector.
657 static bool RequiresRelocation(const CodeDesc& desc);
658#endif
659
Steve Blocka7e24c12009-10-30 11:49:00 +0000660#ifdef ENABLE_DISASSEMBLER
661 // Printing
662 static const char* RelocModeName(Mode rmode);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400663 void Print(Isolate* isolate, std::ostream& os); // NOLINT
Steve Blocka7e24c12009-10-30 11:49:00 +0000664#endif // ENABLE_DISASSEMBLER
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000665#ifdef VERIFY_HEAP
666 void Verify(Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000667#endif
668
669 static const int kCodeTargetMask = (1 << (LAST_CODE_ENUM + 1)) - 1;
670 static const int kPositionMask = 1 << POSITION | 1 << STATEMENT_POSITION;
Ben Murdoch257744e2011-11-30 15:57:28 +0000671 static const int kDataMask =
672 (1 << CODE_TARGET_WITH_ID) | kPositionMask | (1 << COMMENT);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000673 static const int kDebugBreakSlotMask = 1 << DEBUG_BREAK_SLOT_AT_POSITION |
674 1 << DEBUG_BREAK_SLOT_AT_RETURN |
675 1 << DEBUG_BREAK_SLOT_AT_CALL;
676 static const int kApplyMask; // Modes affected by apply. Depends on arch.
Steve Blocka7e24c12009-10-30 11:49:00 +0000677
678 private:
Ben Murdoch61f157c2016-09-16 13:49:30 +0100679 void unchecked_update_wasm_memory_reference(Address address,
680 ICacheFlushMode flush_mode);
681 void unchecked_update_wasm_memory_size(uint32_t size,
682 ICacheFlushMode flush_mode);
683
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000684 Isolate* isolate_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000685 // On ARM, note that pc_ is the address of the constant pool entry
686 // to be relocated and not the address of the instruction
687 // referencing the constant pool entry (except when rmode_ ==
688 // comment).
689 byte* pc_;
690 Mode rmode_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000691 intptr_t data_;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100692 Code* host_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000693 friend class RelocIterator;
694};
695
696
697// RelocInfoWriter serializes a stream of relocation info. It writes towards
698// lower addresses.
699class RelocInfoWriter BASE_EMBEDDED {
700 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000701 RelocInfoWriter()
702 : pos_(NULL),
703 last_pc_(NULL),
704 last_id_(0),
705 last_position_(0),
706 last_mode_(RelocInfo::NUMBER_OF_MODES),
707 next_position_candidate_pos_delta_(0),
708 next_position_candidate_pc_delta_(0),
709 next_position_candidate_flushed_(true) {}
710 RelocInfoWriter(byte* pos, byte* pc)
711 : pos_(pos),
712 last_pc_(pc),
713 last_id_(0),
714 last_position_(0),
715 last_mode_(RelocInfo::NUMBER_OF_MODES),
716 next_position_candidate_pos_delta_(0),
717 next_position_candidate_pc_delta_(0),
718 next_position_candidate_flushed_(true) {}
Steve Blocka7e24c12009-10-30 11:49:00 +0000719
720 byte* pos() const { return pos_; }
721 byte* last_pc() const { return last_pc_; }
722
723 void Write(const RelocInfo* rinfo);
724
725 // Update the state of the stream after reloc info buffer
726 // and/or code is moved while the stream is active.
727 void Reposition(byte* pos, byte* pc) {
728 pos_ = pos;
729 last_pc_ = pc;
730 }
731
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000732 void Finish() { FlushPosition(); }
733
Steve Blocka7e24c12009-10-30 11:49:00 +0000734 // Max size (bytes) of a written RelocInfo. Longest encoding is
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000735 // ExtraTag, VariableLengthPCJump, ExtraTag, pc_delta, data_delta.
736 // On ia32 and arm this is 1 + 4 + 1 + 1 + 4 = 11.
737 // On x64 this is 1 + 4 + 1 + 1 + 8 == 15;
Steve Blocka7e24c12009-10-30 11:49:00 +0000738 // Here we use the maximum of the two.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000739 static const int kMaxSize = 15;
Steve Blocka7e24c12009-10-30 11:49:00 +0000740
741 private:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000742 inline uint32_t WriteLongPCJump(uint32_t pc_delta);
743
744 inline void WriteShortTaggedPC(uint32_t pc_delta, int tag);
745 inline void WriteShortTaggedData(intptr_t data_delta, int tag);
746
747 inline void WriteMode(RelocInfo::Mode rmode);
748 inline void WriteModeAndPC(uint32_t pc_delta, RelocInfo::Mode rmode);
749 inline void WriteIntData(int data_delta);
750 inline void WriteData(intptr_t data_delta);
751 inline void WritePosition(int pc_delta, int pos_delta, RelocInfo::Mode rmode);
752
753 void FlushPosition();
Steve Blocka7e24c12009-10-30 11:49:00 +0000754
755 byte* pos_;
756 byte* last_pc_;
Ben Murdoch257744e2011-11-30 15:57:28 +0000757 int last_id_;
758 int last_position_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000759 RelocInfo::Mode last_mode_;
760 int next_position_candidate_pos_delta_;
761 uint32_t next_position_candidate_pc_delta_;
762 bool next_position_candidate_flushed_;
763
Steve Blocka7e24c12009-10-30 11:49:00 +0000764 DISALLOW_COPY_AND_ASSIGN(RelocInfoWriter);
765};
766
767
768// A RelocIterator iterates over relocation information.
769// Typical use:
770//
771// for (RelocIterator it(code); !it.done(); it.next()) {
772// // do something with it.rinfo() here
773// }
774//
775// A mask can be specified to skip unwanted modes.
776class RelocIterator: public Malloced {
777 public:
778 // Create a new iterator positioned at
779 // the beginning of the reloc info.
780 // Relocation information with mode k is included in the
781 // iteration iff bit k of mode_mask is set.
782 explicit RelocIterator(Code* code, int mode_mask = -1);
783 explicit RelocIterator(const CodeDesc& desc, int mode_mask = -1);
784
785 // Iteration
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100786 bool done() const { return done_; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000787 void next();
788
789 // Return pointer valid until next next().
790 RelocInfo* rinfo() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000791 DCHECK(!done());
Steve Blocka7e24c12009-10-30 11:49:00 +0000792 return &rinfo_;
793 }
794
795 private:
796 // Advance* moves the position before/after reading.
797 // *Read* reads from current byte(s) into rinfo_.
798 // *Get* just reads and returns info on current byte.
799 void Advance(int bytes = 1) { pos_ -= bytes; }
800 int AdvanceGetTag();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000801 RelocInfo::Mode GetMode();
802
803 void AdvanceReadLongPCJump();
804
805 int GetShortDataTypeTag();
806 void ReadShortTaggedPC();
807 void ReadShortTaggedId();
808 void ReadShortTaggedPosition();
809 void ReadShortTaggedData();
810
Steve Blocka7e24c12009-10-30 11:49:00 +0000811 void AdvanceReadPC();
Ben Murdoch257744e2011-11-30 15:57:28 +0000812 void AdvanceReadId();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000813 void AdvanceReadInt();
Ben Murdoch257744e2011-11-30 15:57:28 +0000814 void AdvanceReadPosition();
Steve Blocka7e24c12009-10-30 11:49:00 +0000815 void AdvanceReadData();
Steve Blocka7e24c12009-10-30 11:49:00 +0000816
817 // If the given mode is wanted, set it in rinfo_ and return true.
818 // Else return false. Used for efficiently skipping unwanted modes.
819 bool SetMode(RelocInfo::Mode mode) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100820 return (mode_mask_ & (1 << mode)) ? (rinfo_.rmode_ = mode, true) : false;
Steve Blocka7e24c12009-10-30 11:49:00 +0000821 }
822
823 byte* pos_;
824 byte* end_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000825 byte* code_age_sequence_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000826 RelocInfo rinfo_;
827 bool done_;
828 int mode_mask_;
Ben Murdoch257744e2011-11-30 15:57:28 +0000829 int last_id_;
830 int last_position_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000831 DISALLOW_COPY_AND_ASSIGN(RelocIterator);
832};
833
834
835//------------------------------------------------------------------------------
836// External function
837
838//----------------------------------------------------------------------------
Steve Blocka7e24c12009-10-30 11:49:00 +0000839class SCTableReference;
Steve Blocka7e24c12009-10-30 11:49:00 +0000840class Debug_Address;
Steve Blocka7e24c12009-10-30 11:49:00 +0000841
842
Steve Blocka7e24c12009-10-30 11:49:00 +0000843// An ExternalReference represents a C++ address used in the generated
844// code. All references to C++ functions and variables must be encapsulated in
845// an ExternalReference instance. This is done in order to track the origin of
846// all external references in the code so that they can be bound to the correct
847// addresses when deserializing a heap.
848class ExternalReference BASE_EMBEDDED {
849 public:
Steve Block1e0659c2011-05-24 12:43:12 +0100850 // Used in the simulator to support different native api calls.
Steve Block1e0659c2011-05-24 12:43:12 +0100851 enum Type {
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100852 // Builtin call.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000853 // Object* f(v8::internal::Arguments).
Steve Block1e0659c2011-05-24 12:43:12 +0100854 BUILTIN_CALL, // default
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100855
Ben Murdoch097c5b22016-05-18 11:27:45 +0100856 // Builtin call returning object pair.
857 // ObjectPair f(v8::internal::Arguments).
858 BUILTIN_CALL_PAIR,
859
860 // Builtin call that returns .
861 // ObjectTriple f(v8::internal::Arguments).
862 BUILTIN_CALL_TRIPLE,
863
Ben Murdoch257744e2011-11-30 15:57:28 +0000864 // Builtin that takes float arguments and returns an int.
865 // int f(double, double).
866 BUILTIN_COMPARE_CALL,
867
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100868 // Builtin call that returns floating point.
869 // double f(double, double).
Ben Murdoch257744e2011-11-30 15:57:28 +0000870 BUILTIN_FP_FP_CALL,
871
872 // Builtin call that returns floating point.
873 // double f(double).
874 BUILTIN_FP_CALL,
875
876 // Builtin call that returns floating point.
877 // double f(double, int).
878 BUILTIN_FP_INT_CALL,
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100879
880 // Direct call to API function callback.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000881 // void f(v8::FunctionCallbackInfo&)
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100882 DIRECT_API_CALL,
883
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000884 // Call to function callback via InvokeFunctionCallback.
885 // void f(v8::FunctionCallbackInfo&, v8::FunctionCallback)
886 PROFILING_API_CALL,
887
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100888 // Direct call to accessor getter callback.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000889 // void f(Local<Name> property, PropertyCallbackInfo& info)
890 DIRECT_GETTER_CALL,
891
892 // Call to accessor getter callback via InvokeAccessorGetterCallback.
893 // void f(Local<Name> property, PropertyCallbackInfo& info,
894 // AccessorNameGetterCallback callback)
895 PROFILING_GETTER_CALL
Steve Block1e0659c2011-05-24 12:43:12 +0100896 };
897
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000898 static void SetUp();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000899
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000900 typedef void* ExternalReferenceRedirector(Isolate* isolate, void* original,
901 Type type);
Steve Block1e0659c2011-05-24 12:43:12 +0100902
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000903 ExternalReference() : address_(NULL) {}
904
Steve Block44f0eee2011-05-26 01:26:41 +0100905 ExternalReference(Builtins::CFunctionId id, Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000906
Steve Block44f0eee2011-05-26 01:26:41 +0100907 ExternalReference(ApiFunction* ptr, Type type, Isolate* isolate);
Steve Blockd0582a62009-12-15 09:54:21 +0000908
Steve Block44f0eee2011-05-26 01:26:41 +0100909 ExternalReference(Builtins::Name name, Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000910
Steve Block44f0eee2011-05-26 01:26:41 +0100911 ExternalReference(Runtime::FunctionId id, Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000912
Steve Block44f0eee2011-05-26 01:26:41 +0100913 ExternalReference(const Runtime::Function* f, Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000914
Steve Blocka7e24c12009-10-30 11:49:00 +0000915 explicit ExternalReference(StatsCounter* counter);
916
Steve Block44f0eee2011-05-26 01:26:41 +0100917 ExternalReference(Isolate::AddressId id, Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000918
919 explicit ExternalReference(const SCTableReference& table_ref);
920
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000921 // Isolate as an external reference.
922 static ExternalReference isolate_address(Isolate* isolate);
Steve Block44f0eee2011-05-26 01:26:41 +0100923
Steve Blocka7e24c12009-10-30 11:49:00 +0000924 // One-of-a-kind references. These references are not part of a general
925 // pattern. This means that they have to be added to the
926 // ExternalReferenceTable in serialize.cc manually.
927
Ben Murdoch097c5b22016-05-18 11:27:45 +0100928 static ExternalReference interpreter_dispatch_table_address(Isolate* isolate);
Ben Murdochc5610432016-08-08 18:44:38 +0100929 static ExternalReference interpreter_dispatch_counters(Isolate* isolate);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100930
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100931 static ExternalReference incremental_marking_record_write_function(
932 Isolate* isolate);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100933 static ExternalReference incremental_marking_record_write_code_entry_function(
934 Isolate* isolate);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100935 static ExternalReference store_buffer_overflow_function(
936 Isolate* isolate);
Steve Block44f0eee2011-05-26 01:26:41 +0100937 static ExternalReference delete_handle_scope_extensions(Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000938
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100939 static ExternalReference get_date_field_function(Isolate* isolate);
940 static ExternalReference date_cache_stamp(Isolate* isolate);
941
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000942 static ExternalReference get_make_code_young_function(Isolate* isolate);
943 static ExternalReference get_mark_code_as_executed_function(Isolate* isolate);
944
Ben Murdochb0fe1622011-05-05 13:52:32 +0100945 // Deoptimization support.
Steve Block44f0eee2011-05-26 01:26:41 +0100946 static ExternalReference new_deoptimizer_function(Isolate* isolate);
947 static ExternalReference compute_output_frames_function(Isolate* isolate);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100948
Ben Murdochda12d292016-06-02 14:46:10 +0100949 static ExternalReference wasm_f32_trunc(Isolate* isolate);
950 static ExternalReference wasm_f32_floor(Isolate* isolate);
951 static ExternalReference wasm_f32_ceil(Isolate* isolate);
952 static ExternalReference wasm_f32_nearest_int(Isolate* isolate);
953 static ExternalReference wasm_f64_trunc(Isolate* isolate);
954 static ExternalReference wasm_f64_floor(Isolate* isolate);
955 static ExternalReference wasm_f64_ceil(Isolate* isolate);
956 static ExternalReference wasm_f64_nearest_int(Isolate* isolate);
957 static ExternalReference wasm_int64_to_float32(Isolate* isolate);
958 static ExternalReference wasm_uint64_to_float32(Isolate* isolate);
959 static ExternalReference wasm_int64_to_float64(Isolate* isolate);
960 static ExternalReference wasm_uint64_to_float64(Isolate* isolate);
961 static ExternalReference wasm_float32_to_int64(Isolate* isolate);
962 static ExternalReference wasm_float32_to_uint64(Isolate* isolate);
963 static ExternalReference wasm_float64_to_int64(Isolate* isolate);
964 static ExternalReference wasm_float64_to_uint64(Isolate* isolate);
965 static ExternalReference wasm_int64_div(Isolate* isolate);
966 static ExternalReference wasm_int64_mod(Isolate* isolate);
967 static ExternalReference wasm_uint64_div(Isolate* isolate);
968 static ExternalReference wasm_uint64_mod(Isolate* isolate);
Ben Murdochc5610432016-08-08 18:44:38 +0100969 static ExternalReference wasm_word32_ctz(Isolate* isolate);
970 static ExternalReference wasm_word64_ctz(Isolate* isolate);
971 static ExternalReference wasm_word32_popcnt(Isolate* isolate);
972 static ExternalReference wasm_word64_popcnt(Isolate* isolate);
Ben Murdochda12d292016-06-02 14:46:10 +0100973
974 static ExternalReference f64_acos_wrapper_function(Isolate* isolate);
975 static ExternalReference f64_asin_wrapper_function(Isolate* isolate);
Ben Murdochda12d292016-06-02 14:46:10 +0100976 static ExternalReference f64_pow_wrapper_function(Isolate* isolate);
977 static ExternalReference f64_mod_wrapper_function(Isolate* isolate);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100978
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000979 // Log support.
980 static ExternalReference log_enter_external_function(Isolate* isolate);
981 static ExternalReference log_leave_external_function(Isolate* isolate);
982
Leon Clarkee46be812010-01-19 14:06:41 +0000983 // Static data in the keyed lookup cache.
Steve Block44f0eee2011-05-26 01:26:41 +0100984 static ExternalReference keyed_lookup_cache_keys(Isolate* isolate);
985 static ExternalReference keyed_lookup_cache_field_offsets(Isolate* isolate);
Leon Clarkee46be812010-01-19 14:06:41 +0000986
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100987 // Static variable Heap::roots_array_start()
988 static ExternalReference roots_array_start(Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000989
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000990 // Static variable Heap::allocation_sites_list_address()
991 static ExternalReference allocation_sites_list_address(Isolate* isolate);
992
Steve Blocka7e24c12009-10-30 11:49:00 +0000993 // Static variable StackGuard::address_of_jslimit()
Steve Block44f0eee2011-05-26 01:26:41 +0100994 static ExternalReference address_of_stack_limit(Isolate* isolate);
Steve Blockd0582a62009-12-15 09:54:21 +0000995
996 // Static variable StackGuard::address_of_real_jslimit()
Steve Block44f0eee2011-05-26 01:26:41 +0100997 static ExternalReference address_of_real_stack_limit(Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000998
999 // Static variable RegExpStack::limit_address()
Steve Block44f0eee2011-05-26 01:26:41 +01001000 static ExternalReference address_of_regexp_stack_limit(Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00001001
Leon Clarkee46be812010-01-19 14:06:41 +00001002 // Static variables for RegExp.
Steve Block44f0eee2011-05-26 01:26:41 +01001003 static ExternalReference address_of_static_offsets_vector(Isolate* isolate);
1004 static ExternalReference address_of_regexp_stack_memory_address(
1005 Isolate* isolate);
1006 static ExternalReference address_of_regexp_stack_memory_size(
1007 Isolate* isolate);
Leon Clarkee46be812010-01-19 14:06:41 +00001008
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001009 // Write barrier.
1010 static ExternalReference store_buffer_top(Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00001011
1012 // Used for fast allocation in generated code.
Steve Block44f0eee2011-05-26 01:26:41 +01001013 static ExternalReference new_space_allocation_top_address(Isolate* isolate);
1014 static ExternalReference new_space_allocation_limit_address(Isolate* isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001015 static ExternalReference old_space_allocation_top_address(Isolate* isolate);
1016 static ExternalReference old_space_allocation_limit_address(Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00001017
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001018 static ExternalReference mod_two_doubles_operation(Isolate* isolate);
Steve Block44f0eee2011-05-26 01:26:41 +01001019 static ExternalReference power_double_double_function(Isolate* isolate);
1020 static ExternalReference power_double_int_function(Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00001021
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001022 static ExternalReference handle_scope_next_address(Isolate* isolate);
1023 static ExternalReference handle_scope_limit_address(Isolate* isolate);
1024 static ExternalReference handle_scope_level_address(Isolate* isolate);
Steve Blockd0582a62009-12-15 09:54:21 +00001025
Steve Block44f0eee2011-05-26 01:26:41 +01001026 static ExternalReference scheduled_exception_address(Isolate* isolate);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001027 static ExternalReference address_of_pending_message_obj(Isolate* isolate);
Steve Blockd0582a62009-12-15 09:54:21 +00001028
Ben Murdochb0fe1622011-05-05 13:52:32 +01001029 // Static variables containing common double constants.
1030 static ExternalReference address_of_min_int();
1031 static ExternalReference address_of_one_half();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001032 static ExternalReference address_of_minus_one_half();
Ben Murdochb0fe1622011-05-05 13:52:32 +01001033 static ExternalReference address_of_negative_infinity();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001034 static ExternalReference address_of_the_hole_nan();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001035 static ExternalReference address_of_uint32_bias();
Ben Murdochb0fe1622011-05-05 13:52:32 +01001036
Ben Murdoch61f157c2016-09-16 13:49:30 +01001037 // IEEE 754 functions.
1038 static ExternalReference ieee754_atan_function(Isolate* isolate);
1039 static ExternalReference ieee754_atan2_function(Isolate* isolate);
1040 static ExternalReference ieee754_atanh_function(Isolate* isolate);
1041 static ExternalReference ieee754_cbrt_function(Isolate* isolate);
1042 static ExternalReference ieee754_cos_function(Isolate* isolate);
1043 static ExternalReference ieee754_exp_function(Isolate* isolate);
1044 static ExternalReference ieee754_expm1_function(Isolate* isolate);
1045 static ExternalReference ieee754_log_function(Isolate* isolate);
1046 static ExternalReference ieee754_log1p_function(Isolate* isolate);
1047 static ExternalReference ieee754_log10_function(Isolate* isolate);
1048 static ExternalReference ieee754_log2_function(Isolate* isolate);
1049 static ExternalReference ieee754_sin_function(Isolate* isolate);
1050 static ExternalReference ieee754_tan_function(Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00001051
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001052 static ExternalReference page_flags(Page* page);
1053
1054 static ExternalReference ForDeoptEntry(Address entry);
1055
1056 static ExternalReference cpu_features();
1057
Ben Murdochda12d292016-06-02 14:46:10 +01001058 static ExternalReference is_tail_call_elimination_enabled_address(
1059 Isolate* isolate);
1060
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001061 static ExternalReference debug_is_active_address(Isolate* isolate);
1062 static ExternalReference debug_after_break_target_address(Isolate* isolate);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001063
1064 static ExternalReference is_profiling_address(Isolate* isolate);
1065 static ExternalReference invoke_function_callback(Isolate* isolate);
1066 static ExternalReference invoke_accessor_getter_callback(Isolate* isolate);
1067
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001068 static ExternalReference virtual_handler_register(Isolate* isolate);
1069 static ExternalReference virtual_slot_register(Isolate* isolate);
1070
1071 static ExternalReference runtime_function_table_address(Isolate* isolate);
1072
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001073 Address address() const { return reinterpret_cast<Address>(address_); }
1074
Ben Murdoch61f157c2016-09-16 13:49:30 +01001075 // Used to read out the last step action of the debugger.
1076 static ExternalReference debug_last_step_action_address(Isolate* isolate);
1077
1078 // Used to check for suspended generator, used for stepping across await call.
1079 static ExternalReference debug_suspended_generator_address(Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00001080
Steve Block6ded16b2010-05-10 14:33:55 +01001081#ifndef V8_INTERPRETED_REGEXP
Steve Blocka7e24c12009-10-30 11:49:00 +00001082 // C functions called from RegExp generated code.
1083
1084 // Function NativeRegExpMacroAssembler::CaseInsensitiveCompareUC16()
Steve Block44f0eee2011-05-26 01:26:41 +01001085 static ExternalReference re_case_insensitive_compare_uc16(Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00001086
1087 // Function RegExpMacroAssembler*::CheckStackGuardState()
Steve Block44f0eee2011-05-26 01:26:41 +01001088 static ExternalReference re_check_stack_guard_state(Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00001089
1090 // Function NativeRegExpMacroAssembler::GrowStack()
Steve Block44f0eee2011-05-26 01:26:41 +01001091 static ExternalReference re_grow_stack(Isolate* isolate);
Leon Clarkee46be812010-01-19 14:06:41 +00001092
1093 // byte NativeRegExpMacroAssembler::word_character_bitmap
1094 static ExternalReference re_word_character_map();
1095
Steve Blocka7e24c12009-10-30 11:49:00 +00001096#endif
1097
1098 // This lets you register a function that rewrites all external references.
1099 // Used by the ARM simulator to catch calls to external references.
Ben Murdoch257744e2011-11-30 15:57:28 +00001100 static void set_redirector(Isolate* isolate,
1101 ExternalReferenceRedirector* redirector) {
Steve Block44f0eee2011-05-26 01:26:41 +01001102 // We can't stack them.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001103 DCHECK(isolate->external_reference_redirector() == NULL);
Ben Murdoch257744e2011-11-30 15:57:28 +00001104 isolate->set_external_reference_redirector(
Steve Block44f0eee2011-05-26 01:26:41 +01001105 reinterpret_cast<ExternalReferenceRedirectorPointer*>(redirector));
Steve Blocka7e24c12009-10-30 11:49:00 +00001106 }
1107
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001108 static ExternalReference stress_deopt_count(Isolate* isolate);
1109
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001110 static ExternalReference fixed_typed_array_base_data_offset();
1111
Steve Blocka7e24c12009-10-30 11:49:00 +00001112 private:
1113 explicit ExternalReference(void* address)
1114 : address_(address) {}
1115
Steve Block44f0eee2011-05-26 01:26:41 +01001116 static void* Redirect(Isolate* isolate,
Steve Block44f0eee2011-05-26 01:26:41 +01001117 Address address_arg,
Steve Block1e0659c2011-05-24 12:43:12 +01001118 Type type = ExternalReference::BUILTIN_CALL) {
Steve Block44f0eee2011-05-26 01:26:41 +01001119 ExternalReferenceRedirector* redirector =
1120 reinterpret_cast<ExternalReferenceRedirector*>(
1121 isolate->external_reference_redirector());
Steve Blocka7e24c12009-10-30 11:49:00 +00001122 void* address = reinterpret_cast<void*>(address_arg);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001123 void* answer =
1124 (redirector == NULL) ? address : (*redirector)(isolate, address, type);
Steve Blockd0582a62009-12-15 09:54:21 +00001125 return answer;
Steve Blocka7e24c12009-10-30 11:49:00 +00001126 }
1127
1128 void* address_;
1129};
1130
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001131bool operator==(ExternalReference, ExternalReference);
1132bool operator!=(ExternalReference, ExternalReference);
1133
1134size_t hash_value(ExternalReference);
1135
1136std::ostream& operator<<(std::ostream&, ExternalReference);
1137
Steve Blocka7e24c12009-10-30 11:49:00 +00001138
1139// -----------------------------------------------------------------------------
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001140// Position recording support
1141
Ben Murdochda12d292016-06-02 14:46:10 +01001142class AssemblerPositionsRecorder : public PositionsRecorder {
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001143 public:
Ben Murdochda12d292016-06-02 14:46:10 +01001144 explicit AssemblerPositionsRecorder(Assembler* assembler)
Ben Murdoch61f157c2016-09-16 13:49:30 +01001145 : assembler_(assembler),
1146 current_position_(RelocInfo::kNoPosition),
1147 written_position_(RelocInfo::kNoPosition),
1148 current_statement_position_(RelocInfo::kNoPosition),
1149 written_statement_position_(RelocInfo::kNoPosition) {}
Ben Murdochb8e0da22011-05-16 14:20:40 +01001150
Ben Murdochb0fe1622011-05-05 13:52:32 +01001151 // Set current position to pos.
1152 void RecordPosition(int pos);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001153
1154 // Set current statement position to pos.
1155 void RecordStatementPosition(int pos);
1156
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001157 private:
Ben Murdoch61f157c2016-09-16 13:49:30 +01001158 // Write recorded positions to relocation information.
1159 void WriteRecordedPositions();
1160
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001161 Assembler* assembler_;
Ben Murdoch61f157c2016-09-16 13:49:30 +01001162
1163 int current_position_;
1164 int written_position_;
1165
1166 int current_statement_position_;
1167 int written_statement_position_;
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001168
Ben Murdochda12d292016-06-02 14:46:10 +01001169 DISALLOW_COPY_AND_ASSIGN(AssemblerPositionsRecorder);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001170};
1171
1172
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001173// -----------------------------------------------------------------------------
Steve Blocka7e24c12009-10-30 11:49:00 +00001174// Utility functions
1175
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001176inline int NumberOfBitsSet(uint32_t x) {
Andrei Popescu31002712010-02-23 13:46:05 +00001177 unsigned int num_bits_set;
1178 for (num_bits_set = 0; x; x >>= 1) {
1179 num_bits_set += x & 1;
1180 }
1181 return num_bits_set;
1182}
Steve Blocka7e24c12009-10-30 11:49:00 +00001183
Ben Murdochb0fe1622011-05-05 13:52:32 +01001184// Computes pow(x, y) with the special cases in the spec for Math.pow.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001185double power_helper(Isolate* isolate, double x, double y);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001186double power_double_int(double x, int y);
1187double power_double_double(double x, double y);
1188
Ben Murdoch257744e2011-11-30 15:57:28 +00001189// Helper class for generating code or data associated with the code
1190// right after a call instruction. As an example this can be used to
1191// generate safepoint data after calls for crankshaft.
1192class CallWrapper {
1193 public:
1194 CallWrapper() { }
1195 virtual ~CallWrapper() { }
1196 // Called just before emitting a call. Argument is the size of the generated
1197 // call code.
1198 virtual void BeforeCall(int call_size) const = 0;
1199 // Called just after emitting a call, i.e., at the return site for the call.
1200 virtual void AfterCall() const = 0;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001201 // Return whether call needs to check for debug stepping.
1202 virtual bool NeedsDebugStepCheck() const { return false; }
Ben Murdoch257744e2011-11-30 15:57:28 +00001203};
1204
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001205
Ben Murdoch257744e2011-11-30 15:57:28 +00001206class NullCallWrapper : public CallWrapper {
1207 public:
1208 NullCallWrapper() { }
1209 virtual ~NullCallWrapper() { }
1210 virtual void BeforeCall(int call_size) const { }
1211 virtual void AfterCall() const { }
1212};
1213
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001214
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001215class CheckDebugStepCallWrapper : public CallWrapper {
1216 public:
1217 CheckDebugStepCallWrapper() {}
1218 virtual ~CheckDebugStepCallWrapper() {}
1219 virtual void BeforeCall(int call_size) const {}
1220 virtual void AfterCall() const {}
1221 virtual bool NeedsDebugStepCheck() const { return true; }
1222};
Steve Blocka7e24c12009-10-30 11:49:00 +00001223
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001224
1225// -----------------------------------------------------------------------------
1226// Constant pool support
1227
1228class ConstantPoolEntry {
1229 public:
1230 ConstantPoolEntry() {}
1231 ConstantPoolEntry(int position, intptr_t value, bool sharing_ok)
1232 : position_(position),
1233 merged_index_(sharing_ok ? SHARING_ALLOWED : SHARING_PROHIBITED),
1234 value_(value) {}
1235 ConstantPoolEntry(int position, double value)
1236 : position_(position), merged_index_(SHARING_ALLOWED), value64_(value) {}
1237
1238 int position() const { return position_; }
1239 bool sharing_ok() const { return merged_index_ != SHARING_PROHIBITED; }
1240 bool is_merged() const { return merged_index_ >= 0; }
1241 int merged_index(void) const {
1242 DCHECK(is_merged());
1243 return merged_index_;
1244 }
1245 void set_merged_index(int index) {
1246 merged_index_ = index;
1247 DCHECK(is_merged());
1248 }
1249 int offset(void) const {
1250 DCHECK(merged_index_ >= 0);
1251 return merged_index_;
1252 }
1253 void set_offset(int offset) {
1254 DCHECK(offset >= 0);
1255 merged_index_ = offset;
1256 }
1257 intptr_t value() const { return value_; }
1258 uint64_t value64() const { return bit_cast<uint64_t>(value64_); }
1259
1260 enum Type { INTPTR, DOUBLE, NUMBER_OF_TYPES };
1261
1262 static int size(Type type) {
1263 return (type == INTPTR) ? kPointerSize : kDoubleSize;
1264 }
1265
1266 enum Access { REGULAR, OVERFLOWED };
1267
1268 private:
1269 int position_;
1270 int merged_index_;
1271 union {
1272 intptr_t value_;
1273 double value64_;
1274 };
1275 enum { SHARING_PROHIBITED = -2, SHARING_ALLOWED = -1 };
1276};
1277
1278
1279// -----------------------------------------------------------------------------
1280// Embedded constant pool support
1281
1282class ConstantPoolBuilder BASE_EMBEDDED {
1283 public:
1284 ConstantPoolBuilder(int ptr_reach_bits, int double_reach_bits);
1285
1286 // Add pointer-sized constant to the embedded constant pool
1287 ConstantPoolEntry::Access AddEntry(int position, intptr_t value,
1288 bool sharing_ok) {
1289 ConstantPoolEntry entry(position, value, sharing_ok);
1290 return AddEntry(entry, ConstantPoolEntry::INTPTR);
1291 }
1292
1293 // Add double constant to the embedded constant pool
1294 ConstantPoolEntry::Access AddEntry(int position, double value) {
1295 ConstantPoolEntry entry(position, value);
1296 return AddEntry(entry, ConstantPoolEntry::DOUBLE);
1297 }
1298
1299 // Previews the access type required for the next new entry to be added.
1300 ConstantPoolEntry::Access NextAccess(ConstantPoolEntry::Type type) const;
1301
1302 bool IsEmpty() {
1303 return info_[ConstantPoolEntry::INTPTR].entries.empty() &&
1304 info_[ConstantPoolEntry::INTPTR].shared_entries.empty() &&
1305 info_[ConstantPoolEntry::DOUBLE].entries.empty() &&
1306 info_[ConstantPoolEntry::DOUBLE].shared_entries.empty();
1307 }
1308
1309 // Emit the constant pool. Invoke only after all entries have been
1310 // added and all instructions have been emitted.
1311 // Returns position of the emitted pool (zero implies no constant pool).
1312 int Emit(Assembler* assm);
1313
1314 // Returns the label associated with the start of the constant pool.
1315 // Linking to this label in the function prologue may provide an
1316 // efficient means of constant pool pointer register initialization
1317 // on some architectures.
1318 inline Label* EmittedPosition() { return &emitted_label_; }
1319
1320 private:
1321 ConstantPoolEntry::Access AddEntry(ConstantPoolEntry& entry,
1322 ConstantPoolEntry::Type type);
1323 void EmitSharedEntries(Assembler* assm, ConstantPoolEntry::Type type);
1324 void EmitGroup(Assembler* assm, ConstantPoolEntry::Access access,
1325 ConstantPoolEntry::Type type);
1326
1327 struct PerTypeEntryInfo {
1328 PerTypeEntryInfo() : regular_count(0), overflow_start(-1) {}
1329 bool overflow() const {
1330 return (overflow_start >= 0 &&
1331 overflow_start < static_cast<int>(entries.size()));
1332 }
1333 int regular_reach_bits;
1334 int regular_count;
1335 int overflow_start;
1336 std::vector<ConstantPoolEntry> entries;
1337 std::vector<ConstantPoolEntry> shared_entries;
1338 };
1339
1340 Label emitted_label_; // Records pc_offset of emitted pool
1341 PerTypeEntryInfo info_[ConstantPoolEntry::NUMBER_OF_TYPES];
1342};
1343
1344} // namespace internal
1345} // namespace v8
Steve Blocka7e24c12009-10-30 11:49:00 +00001346#endif // V8_ASSEMBLER_H_