blob: 353abdbbc163000fb8175d523c6d5950a7d59d76 [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"
Emily Bernierd0a1eb72015-03-24 16:35:39 -040042#include "src/runtime/runtime.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000043
44namespace v8 {
Ben Murdoch3ef787d2012-04-12 10:51:47 +010045
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000046// Forward declarations.
Ben Murdoch3ef787d2012-04-12 10:51:47 +010047class ApiFunction;
48
Steve Blocka7e24c12009-10-30 11:49:00 +000049namespace internal {
50
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000051// Forward declarations.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000052class StatsCounter;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000053
Steve Blocka7e24c12009-10-30 11:49:00 +000054// -----------------------------------------------------------------------------
Steve Block44f0eee2011-05-26 01:26:41 +010055// Platform independent assembler base class.
56
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000057enum class CodeObjectRequired { kNo, kYes };
58
59
Steve Block44f0eee2011-05-26 01:26:41 +010060class AssemblerBase: public Malloced {
61 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +000062 AssemblerBase(Isolate* isolate, void* buffer, int buffer_size);
63 virtual ~AssemblerBase();
Steve Block44f0eee2011-05-26 01:26:41 +010064
65 Isolate* isolate() const { return isolate_; }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000066 int jit_cookie() const { return jit_cookie_; }
67
68 bool emit_debug_code() const { return emit_debug_code_; }
69 void set_emit_debug_code(bool value) { emit_debug_code_ = value; }
70
71 bool serializer_enabled() const { return serializer_enabled_; }
72 void enable_serializer() { serializer_enabled_ = true; }
73
74 bool predictable_code_size() const { return predictable_code_size_; }
75 void set_predictable_code_size(bool value) { predictable_code_size_ = value; }
76
77 uint64_t enabled_cpu_features() const { return enabled_cpu_features_; }
78 void set_enabled_cpu_features(uint64_t features) {
79 enabled_cpu_features_ = features;
80 }
81 bool IsEnabled(CpuFeature f) {
82 return (enabled_cpu_features_ & (static_cast<uint64_t>(1) << f)) != 0;
83 }
Steve Block44f0eee2011-05-26 01:26:41 +010084
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000085 bool is_constant_pool_available() const {
86 if (FLAG_enable_embedded_constant_pool) {
87 return constant_pool_available_;
Emily Bernierd0a1eb72015-03-24 16:35:39 -040088 } else {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000089 // Embedded constant pool not supported on this architecture.
Emily Bernierd0a1eb72015-03-24 16:35:39 -040090 UNREACHABLE();
91 return false;
92 }
93 }
94
Ben Murdochdb1b4382012-04-26 19:03:50 +010095 // Overwrite a host NaN with a quiet target NaN. Used by mksnapshot for
96 // cross-snapshotting.
97 static void QuietNaN(HeapObject* nan) { }
98
Ben Murdochb8a8cc12014-11-26 15:28:44 +000099 int pc_offset() const { return static_cast<int>(pc_ - buffer_); }
100
101 // This function is called when code generation is aborted, so that
102 // the assembler could clean up internal data structures.
103 virtual void AbortedCodeGeneration() { }
104
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000105 // Debugging
106 void Print();
107
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000108 static const int kMinimalBufferSize = 4*KB;
109
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000110 static void FlushICache(Isolate* isolate, void* start, size_t size);
111
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000112 protected:
113 // The buffer into which code and relocation info are generated. It could
114 // either be owned by the assembler or be provided externally.
115 byte* buffer_;
116 int buffer_size_;
117 bool own_buffer_;
118
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000119 void set_constant_pool_available(bool available) {
120 if (FLAG_enable_embedded_constant_pool) {
121 constant_pool_available_ = available;
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400122 } else {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000123 // Embedded constant pool not supported on this architecture.
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400124 UNREACHABLE();
125 }
126 }
127
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000128 // The program counter, which points into the buffer above and moves forward.
129 byte* pc_;
130
Steve Block44f0eee2011-05-26 01:26:41 +0100131 private:
132 Isolate* isolate_;
Steve Block053d10c2011-06-13 19:13:29 +0100133 int jit_cookie_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000134 uint64_t enabled_cpu_features_;
135 bool emit_debug_code_;
136 bool predictable_code_size_;
137 bool serializer_enabled_;
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400138
139 // Indicates whether the constant pool can be accessed, which is only possible
140 // if the pp register points to the current code object's constant pool.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000141 bool constant_pool_available_;
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400142
143 // Constant pool.
144 friend class FrameAndConstantPoolScope;
145 friend class ConstantPoolUnavailableScope;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000146};
147
148
149// Avoids emitting debug code during the lifetime of this scope object.
150class DontEmitDebugCodeScope BASE_EMBEDDED {
151 public:
152 explicit DontEmitDebugCodeScope(AssemblerBase* assembler)
153 : assembler_(assembler), old_value_(assembler->emit_debug_code()) {
154 assembler_->set_emit_debug_code(false);
155 }
156 ~DontEmitDebugCodeScope() {
157 assembler_->set_emit_debug_code(old_value_);
158 }
159 private:
160 AssemblerBase* assembler_;
161 bool old_value_;
162};
163
164
165// Avoids using instructions that vary in size in unpredictable ways between the
166// snapshot and the running VM.
167class PredictableCodeSizeScope {
168 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000169 explicit PredictableCodeSizeScope(AssemblerBase* assembler);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000170 PredictableCodeSizeScope(AssemblerBase* assembler, int expected_size);
171 ~PredictableCodeSizeScope();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000172 void ExpectSize(int expected_size) { expected_size_ = expected_size; }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000173
174 private:
175 AssemblerBase* assembler_;
176 int expected_size_;
177 int start_offset_;
178 bool old_value_;
179};
180
181
182// Enable a specified feature within a scope.
183class CpuFeatureScope BASE_EMBEDDED {
184 public:
185#ifdef DEBUG
186 CpuFeatureScope(AssemblerBase* assembler, CpuFeature f);
187 ~CpuFeatureScope();
188
189 private:
190 AssemblerBase* assembler_;
191 uint64_t old_enabled_;
192#else
193 CpuFeatureScope(AssemblerBase* assembler, CpuFeature f) {}
194#endif
195};
196
197
198// CpuFeatures keeps track of which features are supported by the target CPU.
199// Supported features must be enabled by a CpuFeatureScope before use.
200// Example:
201// if (assembler->IsSupported(SSE3)) {
202// CpuFeatureScope fscope(assembler, SSE3);
203// // Generate code containing SSE3 instructions.
204// } else {
205// // Generate alternative code.
206// }
207class CpuFeatures : public AllStatic {
208 public:
209 static void Probe(bool cross_compile) {
210 STATIC_ASSERT(NUMBER_OF_CPU_FEATURES <= kBitsPerInt);
211 if (initialized_) return;
212 initialized_ = true;
213 ProbeImpl(cross_compile);
214 }
215
216 static unsigned SupportedFeatures() {
217 Probe(false);
218 return supported_;
219 }
220
221 static bool IsSupported(CpuFeature f) {
222 return (supported_ & (1u << f)) != 0;
223 }
224
225 static inline bool SupportsCrankshaft();
226
Ben Murdoch097c5b22016-05-18 11:27:45 +0100227 static inline unsigned icache_line_size() {
228 DCHECK(icache_line_size_ != 0);
229 return icache_line_size_;
230 }
231
232 static inline unsigned dcache_line_size() {
233 DCHECK(dcache_line_size_ != 0);
234 return dcache_line_size_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000235 }
236
237 static void PrintTarget();
238 static void PrintFeatures();
239
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000240 private:
241 friend class ExternalReference;
242 friend class AssemblerBase;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000243 // Flush instruction cache.
244 static void FlushICache(void* start, size_t size);
245
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000246 // Platform-dependent implementation.
247 static void ProbeImpl(bool cross_compile);
248
249 static unsigned supported_;
Ben Murdoch097c5b22016-05-18 11:27:45 +0100250 static unsigned icache_line_size_;
251 static unsigned dcache_line_size_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000252 static bool initialized_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000253 DISALLOW_COPY_AND_ASSIGN(CpuFeatures);
Steve Block44f0eee2011-05-26 01:26:41 +0100254};
255
Ben Murdochb0fe1622011-05-05 13:52:32 +0100256
257// -----------------------------------------------------------------------------
Steve Blocka7e24c12009-10-30 11:49:00 +0000258// Labels represent pc locations; they are typically jump or call targets.
259// After declaration, a label can be freely used to denote known or (yet)
260// unknown pc location. Assembler::bind() is used to bind a label to the
261// current pc. A label can be bound only once.
262
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400263class Label {
Steve Blocka7e24c12009-10-30 11:49:00 +0000264 public:
Ben Murdoch257744e2011-11-30 15:57:28 +0000265 enum Distance {
266 kNear, kFar
267 };
268
269 INLINE(Label()) {
270 Unuse();
271 UnuseNear();
272 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000273
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000274 INLINE(~Label()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000275 DCHECK(!is_linked());
276 DCHECK(!is_near_linked());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000277 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000278
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000279 INLINE(void Unuse()) { pos_ = 0; }
280 INLINE(void UnuseNear()) { near_link_pos_ = 0; }
281
282 INLINE(bool is_bound() const) { return pos_ < 0; }
283 INLINE(bool is_unused() const) { return pos_ == 0 && near_link_pos_ == 0; }
284 INLINE(bool is_linked() const) { return pos_ > 0; }
Ben Murdoch257744e2011-11-30 15:57:28 +0000285 INLINE(bool is_near_linked() const) { return near_link_pos_ > 0; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000286
287 // Returns the position of bound or linked labels. Cannot be used
288 // for unused labels.
289 int pos() const;
Ben Murdoch257744e2011-11-30 15:57:28 +0000290 int near_link_pos() const { return near_link_pos_ - 1; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000291
292 private:
293 // pos_ encodes both the binding state (via its sign)
294 // and the binding position (via its value) of a label.
295 //
296 // pos_ < 0 bound label, pos() returns the jump target position
297 // pos_ == 0 unused label
298 // pos_ > 0 linked label, pos() returns the last reference position
299 int pos_;
300
Ben Murdoch257744e2011-11-30 15:57:28 +0000301 // Behaves like |pos_| in the "> 0" case, but for near jumps to this label.
302 int near_link_pos_;
303
Steve Blocka7e24c12009-10-30 11:49:00 +0000304 void bind_to(int pos) {
305 pos_ = -pos - 1;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000306 DCHECK(is_bound());
Steve Blocka7e24c12009-10-30 11:49:00 +0000307 }
Ben Murdoch257744e2011-11-30 15:57:28 +0000308 void link_to(int pos, Distance distance = kFar) {
309 if (distance == kNear) {
310 near_link_pos_ = pos + 1;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000311 DCHECK(is_near_linked());
Ben Murdoch257744e2011-11-30 15:57:28 +0000312 } else {
313 pos_ = pos + 1;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000314 DCHECK(is_linked());
Ben Murdoch257744e2011-11-30 15:57:28 +0000315 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000316 }
317
318 friend class Assembler;
Steve Blocka7e24c12009-10-30 11:49:00 +0000319 friend class Displacement;
Steve Blocka7e24c12009-10-30 11:49:00 +0000320 friend class RegExpMacroAssemblerIrregexp;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000321
322#if V8_TARGET_ARCH_ARM64
323 // On ARM64, the Assembler keeps track of pointers to Labels to resolve
324 // branches to distant targets. Copying labels would confuse the Assembler.
325 DISALLOW_COPY_AND_ASSIGN(Label); // NOLINT
326#endif
Steve Blocka7e24c12009-10-30 11:49:00 +0000327};
328
329
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100330enum SaveFPRegsMode { kDontSaveFPRegs, kSaveFPRegs };
331
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000332enum ArgvMode { kArgvOnStack, kArgvInRegister };
333
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000334// Specifies whether to perform icache flush operations on RelocInfo updates.
335// If FLUSH_ICACHE_IF_NEEDED, the icache will always be flushed if an
336// instruction was modified. If SKIP_ICACHE_FLUSH the flush will always be
337// skipped (only use this if you will flush the icache manually before it is
338// executed).
339enum ICacheFlushMode { FLUSH_ICACHE_IF_NEEDED, SKIP_ICACHE_FLUSH };
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100340
Steve Blocka7e24c12009-10-30 11:49:00 +0000341// -----------------------------------------------------------------------------
342// Relocation information
343
344
345// Relocation information consists of the address (pc) of the datum
346// to which the relocation information applies, the relocation mode
347// (rmode), and an optional data field. The relocation mode may be
348// "descriptive" and not indicate a need for relocation, but simply
349// describe a property of the datum. Such rmodes are useful for GC
350// and nice disassembly output.
351
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000352class RelocInfo {
Steve Blocka7e24c12009-10-30 11:49:00 +0000353 public:
354 // The constant kNoPosition is used with the collecting of source positions
355 // in the relocation information. Two types of source positions are collected
356 // "position" (RelocMode position) and "statement position" (RelocMode
357 // statement_position). The "position" is collected at places in the source
358 // code which are of interest when making stack traces to pin-point the source
359 // location of a stack frame as close as possible. The "statement position" is
360 // collected at the beginning at each statement, and is used to indicate
361 // possible break locations. kNoPosition is used to indicate an
362 // invalid/uninitialized position value.
363 static const int kNoPosition = -1;
364
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100365 // This string is used to add padding comments to the reloc info in cases
366 // where we are not sure to have enough space for patching in during
367 // lazy deoptimization. This is the case if we have indirect calls for which
368 // we do not normally record relocation info.
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000369 static const char* const kFillerCommentString;
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100370
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000371 // The minimum size of a comment is equal to two bytes for the extra tagged
372 // pc and kPointerSize for the actual pointer to the comment.
373 static const int kMinRelocCommentSize = 2 + kPointerSize;
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100374
375 // The maximum size for a call instruction including pc-jump.
376 static const int kMaxCallSize = 6;
377
Steve Block44f0eee2011-05-26 01:26:41 +0100378 // The maximum pc delta that will use the short encoding.
379 static const int kMaxSmallPCDelta;
380
Steve Blocka7e24c12009-10-30 11:49:00 +0000381 enum Mode {
382 // Please note the order is important (see IsCodeTarget, IsGCRelocMode).
Ben Murdoch257744e2011-11-30 15:57:28 +0000383 CODE_TARGET, // Code target which is not any of the above.
384 CODE_TARGET_WITH_ID,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000385 DEBUGGER_STATEMENT, // Code target for the debugger statement.
Steve Blocka7e24c12009-10-30 11:49:00 +0000386 EMBEDDED_OBJECT,
Ben Murdochda12d292016-06-02 14:46:10 +0100387 // To relocate pointers into the wasm memory embedded in wasm code
388 WASM_MEMORY_REFERENCE,
Ben Murdochc5610432016-08-08 18:44:38 +0100389 WASM_MEMORY_SIZE_REFERENCE,
390 CELL,
Ben Murdochb0fe1622011-05-05 13:52:32 +0100391
Steve Blocka7e24c12009-10-30 11:49:00 +0000392 // Everything after runtime_entry (inclusive) is not GC'ed.
393 RUNTIME_ENTRY,
Steve Blocka7e24c12009-10-30 11:49:00 +0000394 COMMENT,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000395 POSITION, // See comment for kNoPosition above.
Steve Blocka7e24c12009-10-30 11:49:00 +0000396 STATEMENT_POSITION, // See comment for kNoPosition above.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000397
398 // Additional code inserted for debug break slot.
399 DEBUG_BREAK_SLOT_AT_POSITION,
400 DEBUG_BREAK_SLOT_AT_RETURN,
401 DEBUG_BREAK_SLOT_AT_CALL,
Ben Murdochda12d292016-06-02 14:46:10 +0100402 DEBUG_BREAK_SLOT_AT_TAIL_CALL,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000403
Steve Blocka7e24c12009-10-30 11:49:00 +0000404 EXTERNAL_REFERENCE, // The address of an external C++ function.
405 INTERNAL_REFERENCE, // An address inside the same function.
406
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000407 // Encoded internal reference, used only on MIPS, MIPS64 and PPC.
408 INTERNAL_REFERENCE_ENCODED,
409
410 // Continuation points for a generator yield.
411 GENERATOR_CONTINUATION,
412
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000413 // Marks constant and veneer pools. Only used on ARM and ARM64.
414 // They use a custom noncompact encoding.
415 CONST_POOL,
416 VENEER_POOL,
417
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000418 DEOPT_REASON, // Deoptimization reason index.
Ben Murdochc5610432016-08-08 18:44:38 +0100419 DEOPT_ID, // Deoptimization inlining id.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000420
421 // This is not an actual reloc mode, but used to encode a long pc jump that
422 // cannot be encoded as part of another record.
423 PC_JUMP,
424
Steve Blocka7e24c12009-10-30 11:49:00 +0000425 // Pseudo-types
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000426 NUMBER_OF_MODES,
427 NONE32, // never recorded 32-bit value
428 NONE64, // never recorded 64-bit value
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000429 CODE_AGE_SEQUENCE, // Not stored in RelocInfo array, used explictly by
430 // code aging.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000431
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000432 FIRST_REAL_RELOC_MODE = CODE_TARGET,
433 LAST_REAL_RELOC_MODE = VENEER_POOL,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000434 LAST_CODE_ENUM = DEBUGGER_STATEMENT,
Ben Murdochc5610432016-08-08 18:44:38 +0100435 LAST_GCED_ENUM = WASM_MEMORY_SIZE_REFERENCE,
Ben Murdochda12d292016-06-02 14:46:10 +0100436 FIRST_SHAREABLE_RELOC_MODE = CELL,
Steve Blocka7e24c12009-10-30 11:49:00 +0000437 };
438
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000439 STATIC_ASSERT(NUMBER_OF_MODES <= kBitsPerInt);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100440
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000441 explicit RelocInfo(Isolate* isolate) : isolate_(isolate) {
442 DCHECK_NOT_NULL(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000443 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000444
445 RelocInfo(Isolate* isolate, byte* pc, Mode rmode, intptr_t data, Code* host)
446 : isolate_(isolate), pc_(pc), rmode_(rmode), data_(data), host_(host) {
447 DCHECK_NOT_NULL(isolate);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000448 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000449
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000450 static inline bool IsRealRelocMode(Mode mode) {
451 return mode >= FIRST_REAL_RELOC_MODE &&
452 mode <= LAST_REAL_RELOC_MODE;
453 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000454 static inline bool IsCodeTarget(Mode mode) {
455 return mode <= LAST_CODE_ENUM;
456 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100457 static inline bool IsEmbeddedObject(Mode mode) {
458 return mode == EMBEDDED_OBJECT;
459 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000460 static inline bool IsCell(Mode mode) { return mode == CELL; }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000461 static inline bool IsRuntimeEntry(Mode mode) {
462 return mode == RUNTIME_ENTRY;
463 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000464 // Is the relocation mode affected by GC?
465 static inline bool IsGCRelocMode(Mode mode) {
466 return mode <= LAST_GCED_ENUM;
467 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000468 static inline bool IsComment(Mode mode) {
469 return mode == COMMENT;
470 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000471 static inline bool IsConstPool(Mode mode) {
472 return mode == CONST_POOL;
473 }
474 static inline bool IsVeneerPool(Mode mode) {
475 return mode == VENEER_POOL;
476 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000477 static inline bool IsDeoptReason(Mode mode) {
478 return mode == DEOPT_REASON;
479 }
Ben Murdochc5610432016-08-08 18:44:38 +0100480 static inline bool IsDeoptId(Mode mode) {
481 return mode == DEOPT_ID;
482 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000483 static inline bool IsPosition(Mode mode) {
484 return mode == POSITION || mode == STATEMENT_POSITION;
485 }
486 static inline bool IsStatementPosition(Mode mode) {
487 return mode == STATEMENT_POSITION;
488 }
489 static inline bool IsExternalReference(Mode mode) {
490 return mode == EXTERNAL_REFERENCE;
491 }
492 static inline bool IsInternalReference(Mode mode) {
493 return mode == INTERNAL_REFERENCE;
494 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000495 static inline bool IsInternalReferenceEncoded(Mode mode) {
496 return mode == INTERNAL_REFERENCE_ENCODED;
497 }
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100498 static inline bool IsDebugBreakSlot(Mode mode) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000499 return IsDebugBreakSlotAtPosition(mode) || IsDebugBreakSlotAtReturn(mode) ||
Ben Murdochda12d292016-06-02 14:46:10 +0100500 IsDebugBreakSlotAtCall(mode) || IsDebugBreakSlotAtTailCall(mode);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000501 }
502 static inline bool IsDebugBreakSlotAtPosition(Mode mode) {
503 return mode == DEBUG_BREAK_SLOT_AT_POSITION;
504 }
505 static inline bool IsDebugBreakSlotAtReturn(Mode mode) {
506 return mode == DEBUG_BREAK_SLOT_AT_RETURN;
507 }
508 static inline bool IsDebugBreakSlotAtCall(Mode mode) {
509 return mode == DEBUG_BREAK_SLOT_AT_CALL;
510 }
Ben Murdochda12d292016-06-02 14:46:10 +0100511 static inline bool IsDebugBreakSlotAtTailCall(Mode mode) {
512 return mode == DEBUG_BREAK_SLOT_AT_TAIL_CALL;
513 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000514 static inline bool IsDebuggerStatement(Mode mode) {
515 return mode == DEBUGGER_STATEMENT;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100516 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000517 static inline bool IsNone(Mode mode) {
518 return mode == NONE32 || mode == NONE64;
519 }
520 static inline bool IsCodeAgeSequence(Mode mode) {
521 return mode == CODE_AGE_SEQUENCE;
522 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000523 static inline bool IsGeneratorContinuation(Mode mode) {
524 return mode == GENERATOR_CONTINUATION;
525 }
Ben Murdochda12d292016-06-02 14:46:10 +0100526 static inline bool IsWasmMemoryReference(Mode mode) {
527 return mode == WASM_MEMORY_REFERENCE;
528 }
Ben Murdochc5610432016-08-08 18:44:38 +0100529 static inline bool IsWasmMemorySizeReference(Mode mode) {
530 return mode == WASM_MEMORY_SIZE_REFERENCE;
531 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000532 static inline int ModeMask(Mode mode) { return 1 << mode; }
533
534 // Accessors
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000535 Isolate* isolate() const { return isolate_; }
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100536 byte* pc() const { return pc_; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000537 void set_pc(byte* pc) { pc_ = pc; }
538 Mode rmode() const { return rmode_; }
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100539 intptr_t data() const { return data_; }
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100540 Code* host() const { return host_; }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000541 void set_host(Code* host) { host_ = host; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000542
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000543 // Apply a relocation by delta bytes. When the code object is moved, PC
544 // relative addresses have to be updated as well as absolute addresses
545 // inside the code (internal references).
546 // Do not forget to flush the icache afterwards!
547 INLINE(void apply(intptr_t delta));
Steve Blocka7e24c12009-10-30 11:49:00 +0000548
Leon Clarkef7060e22010-06-03 12:02:55 +0100549 // Is the pointer this relocation info refers to coded like a plain pointer
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100550 // or is it strange in some way (e.g. relative or patched into a series of
Leon Clarkef7060e22010-06-03 12:02:55 +0100551 // instructions).
552 bool IsCodedSpecially();
553
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000554 // If true, the pointer this relocation info refers to is an entry in the
555 // constant pool, otherwise the pointer is embedded in the instruction stream.
556 bool IsInConstantPool();
557
Ben Murdochc5610432016-08-08 18:44:38 +0100558 Address wasm_memory_reference();
559 uint32_t wasm_memory_size_reference();
560 void update_wasm_memory_reference(
561 Address old_base, Address new_base, uint32_t old_size, uint32_t new_size,
562 ICacheFlushMode icache_flush_mode = SKIP_ICACHE_FLUSH);
563
Steve Blocka7e24c12009-10-30 11:49:00 +0000564 // this relocation applies to;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000565 // can only be called if IsCodeTarget(rmode_) || IsRuntimeEntry(rmode_)
Steve Blocka7e24c12009-10-30 11:49:00 +0000566 INLINE(Address target_address());
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100567 INLINE(void set_target_address(Address target,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000568 WriteBarrierMode write_barrier_mode =
569 UPDATE_WRITE_BARRIER,
570 ICacheFlushMode icache_flush_mode =
571 FLUSH_ICACHE_IF_NEEDED));
Steve Blocka7e24c12009-10-30 11:49:00 +0000572 INLINE(Object* target_object());
Steve Block3ce2e202009-11-05 08:53:23 +0000573 INLINE(Handle<Object> target_object_handle(Assembler* origin));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100574 INLINE(void set_target_object(Object* target,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000575 WriteBarrierMode write_barrier_mode =
576 UPDATE_WRITE_BARRIER,
577 ICacheFlushMode icache_flush_mode =
578 FLUSH_ICACHE_IF_NEEDED));
579 INLINE(Address target_runtime_entry(Assembler* origin));
580 INLINE(void set_target_runtime_entry(Address target,
581 WriteBarrierMode write_barrier_mode =
582 UPDATE_WRITE_BARRIER,
583 ICacheFlushMode icache_flush_mode =
584 FLUSH_ICACHE_IF_NEEDED));
585 INLINE(Cell* target_cell());
586 INLINE(Handle<Cell> target_cell_handle());
587 INLINE(void set_target_cell(Cell* cell,
588 WriteBarrierMode write_barrier_mode =
589 UPDATE_WRITE_BARRIER,
590 ICacheFlushMode icache_flush_mode =
591 FLUSH_ICACHE_IF_NEEDED));
592 INLINE(Handle<Object> code_age_stub_handle(Assembler* origin));
593 INLINE(Code* code_age_stub());
594 INLINE(void set_code_age_stub(Code* stub,
595 ICacheFlushMode icache_flush_mode =
596 FLUSH_ICACHE_IF_NEEDED));
Ben Murdochb0fe1622011-05-05 13:52:32 +0100597
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000598 // Returns the address of the constant pool entry where the target address
599 // is held. This should only be called if IsInConstantPool returns true.
600 INLINE(Address constant_pool_entry_address());
Steve Blocka7e24c12009-10-30 11:49:00 +0000601
Leon Clarkef7060e22010-06-03 12:02:55 +0100602 // Read the address of the word containing the target_address in an
603 // instruction stream. What this means exactly is architecture-independent.
604 // The only architecture-independent user of this function is the serializer.
605 // The serializer uses it to find out how many raw bytes of instruction to
606 // output before the next target. Architecture-independent code shouldn't
607 // dereference the pointer it gets back from this.
Steve Blocka7e24c12009-10-30 11:49:00 +0000608 INLINE(Address target_address_address());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000609
Leon Clarkef7060e22010-06-03 12:02:55 +0100610 // This indicates how much space a target takes up when deserializing a code
611 // stream. For most architectures this is just the size of a pointer. For
612 // an instruction like movw/movt where the target bits are mixed into the
613 // instruction bits the size of the target will be zero, indicating that the
614 // serializer should not step forwards in memory after a target is resolved
615 // and written. In this case the target_address_address function above
616 // should return the end of the instructions to be patched, allowing the
617 // deserializer to deserialize the instructions as raw bytes and put them in
618 // place, ready to be patched with the target.
619 INLINE(int target_address_size());
Steve Blocka7e24c12009-10-30 11:49:00 +0000620
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000621 // Read the reference in the instruction this relocation
622 // applies to; can only be called if rmode_ is EXTERNAL_REFERENCE.
623 INLINE(Address target_external_reference());
624
625 // Read the reference in the instruction this relocation
626 // applies to; can only be called if rmode_ is INTERNAL_REFERENCE.
627 INLINE(Address target_internal_reference());
628
629 // Return the reference address this relocation applies to;
630 // can only be called if rmode_ is INTERNAL_REFERENCE.
631 INLINE(Address target_internal_reference_address());
Steve Blocka7e24c12009-10-30 11:49:00 +0000632
633 // Read/modify the address of a call instruction. This is used to relocate
634 // the break points where straight-line code is patched with a call
635 // instruction.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000636 INLINE(Address debug_call_address());
637 INLINE(void set_debug_call_address(Address target));
Steve Blocka7e24c12009-10-30 11:49:00 +0000638
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000639 // Wipe out a relocation to a fixed value, used for making snapshots
640 // reproducible.
641 INLINE(void WipeOut());
642
Steve Block44f0eee2011-05-26 01:26:41 +0100643 template<typename StaticVisitor> inline void Visit(Heap* heap);
Ben Murdochc5610432016-08-08 18:44:38 +0100644
645 template <typename ObjectVisitor>
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000646 inline void Visit(Isolate* isolate, ObjectVisitor* v);
Leon Clarkef7060e22010-06-03 12:02:55 +0100647
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100648 // Check whether this debug break slot has been patched with a call to the
649 // debugger.
Ben Murdoch097c5b22016-05-18 11:27:45 +0100650 bool IsPatchedDebugBreakSlotSequence();
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100651
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000652#ifdef DEBUG
653 // Check whether the given code contains relocation information that
654 // either is position-relative or movable by the garbage collector.
655 static bool RequiresRelocation(const CodeDesc& desc);
656#endif
657
Steve Blocka7e24c12009-10-30 11:49:00 +0000658#ifdef ENABLE_DISASSEMBLER
659 // Printing
660 static const char* RelocModeName(Mode rmode);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400661 void Print(Isolate* isolate, std::ostream& os); // NOLINT
Steve Blocka7e24c12009-10-30 11:49:00 +0000662#endif // ENABLE_DISASSEMBLER
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000663#ifdef VERIFY_HEAP
664 void Verify(Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000665#endif
666
667 static const int kCodeTargetMask = (1 << (LAST_CODE_ENUM + 1)) - 1;
668 static const int kPositionMask = 1 << POSITION | 1 << STATEMENT_POSITION;
Ben Murdoch257744e2011-11-30 15:57:28 +0000669 static const int kDataMask =
670 (1 << CODE_TARGET_WITH_ID) | kPositionMask | (1 << COMMENT);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000671 static const int kDebugBreakSlotMask = 1 << DEBUG_BREAK_SLOT_AT_POSITION |
672 1 << DEBUG_BREAK_SLOT_AT_RETURN |
673 1 << DEBUG_BREAK_SLOT_AT_CALL;
674 static const int kApplyMask; // Modes affected by apply. Depends on arch.
Steve Blocka7e24c12009-10-30 11:49:00 +0000675
676 private:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000677 Isolate* isolate_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000678 // On ARM, note that pc_ is the address of the constant pool entry
679 // to be relocated and not the address of the instruction
680 // referencing the constant pool entry (except when rmode_ ==
681 // comment).
682 byte* pc_;
683 Mode rmode_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000684 intptr_t data_;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100685 Code* host_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000686 friend class RelocIterator;
687};
688
689
690// RelocInfoWriter serializes a stream of relocation info. It writes towards
691// lower addresses.
692class RelocInfoWriter BASE_EMBEDDED {
693 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000694 RelocInfoWriter()
695 : pos_(NULL),
696 last_pc_(NULL),
697 last_id_(0),
698 last_position_(0),
699 last_mode_(RelocInfo::NUMBER_OF_MODES),
700 next_position_candidate_pos_delta_(0),
701 next_position_candidate_pc_delta_(0),
702 next_position_candidate_flushed_(true) {}
703 RelocInfoWriter(byte* pos, byte* pc)
704 : pos_(pos),
705 last_pc_(pc),
706 last_id_(0),
707 last_position_(0),
708 last_mode_(RelocInfo::NUMBER_OF_MODES),
709 next_position_candidate_pos_delta_(0),
710 next_position_candidate_pc_delta_(0),
711 next_position_candidate_flushed_(true) {}
Steve Blocka7e24c12009-10-30 11:49:00 +0000712
713 byte* pos() const { return pos_; }
714 byte* last_pc() const { return last_pc_; }
715
716 void Write(const RelocInfo* rinfo);
717
718 // Update the state of the stream after reloc info buffer
719 // and/or code is moved while the stream is active.
720 void Reposition(byte* pos, byte* pc) {
721 pos_ = pos;
722 last_pc_ = pc;
723 }
724
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000725 void Finish() { FlushPosition(); }
726
Steve Blocka7e24c12009-10-30 11:49:00 +0000727 // Max size (bytes) of a written RelocInfo. Longest encoding is
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000728 // ExtraTag, VariableLengthPCJump, ExtraTag, pc_delta, data_delta.
729 // On ia32 and arm this is 1 + 4 + 1 + 1 + 4 = 11.
730 // On x64 this is 1 + 4 + 1 + 1 + 8 == 15;
Steve Blocka7e24c12009-10-30 11:49:00 +0000731 // Here we use the maximum of the two.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000732 static const int kMaxSize = 15;
Steve Blocka7e24c12009-10-30 11:49:00 +0000733
734 private:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000735 inline uint32_t WriteLongPCJump(uint32_t pc_delta);
736
737 inline void WriteShortTaggedPC(uint32_t pc_delta, int tag);
738 inline void WriteShortTaggedData(intptr_t data_delta, int tag);
739
740 inline void WriteMode(RelocInfo::Mode rmode);
741 inline void WriteModeAndPC(uint32_t pc_delta, RelocInfo::Mode rmode);
742 inline void WriteIntData(int data_delta);
743 inline void WriteData(intptr_t data_delta);
744 inline void WritePosition(int pc_delta, int pos_delta, RelocInfo::Mode rmode);
745
746 void FlushPosition();
Steve Blocka7e24c12009-10-30 11:49:00 +0000747
748 byte* pos_;
749 byte* last_pc_;
Ben Murdoch257744e2011-11-30 15:57:28 +0000750 int last_id_;
751 int last_position_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000752 RelocInfo::Mode last_mode_;
753 int next_position_candidate_pos_delta_;
754 uint32_t next_position_candidate_pc_delta_;
755 bool next_position_candidate_flushed_;
756
Steve Blocka7e24c12009-10-30 11:49:00 +0000757 DISALLOW_COPY_AND_ASSIGN(RelocInfoWriter);
758};
759
760
761// A RelocIterator iterates over relocation information.
762// Typical use:
763//
764// for (RelocIterator it(code); !it.done(); it.next()) {
765// // do something with it.rinfo() here
766// }
767//
768// A mask can be specified to skip unwanted modes.
769class RelocIterator: public Malloced {
770 public:
771 // Create a new iterator positioned at
772 // the beginning of the reloc info.
773 // Relocation information with mode k is included in the
774 // iteration iff bit k of mode_mask is set.
775 explicit RelocIterator(Code* code, int mode_mask = -1);
776 explicit RelocIterator(const CodeDesc& desc, int mode_mask = -1);
777
778 // Iteration
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100779 bool done() const { return done_; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000780 void next();
781
782 // Return pointer valid until next next().
783 RelocInfo* rinfo() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000784 DCHECK(!done());
Steve Blocka7e24c12009-10-30 11:49:00 +0000785 return &rinfo_;
786 }
787
788 private:
789 // Advance* moves the position before/after reading.
790 // *Read* reads from current byte(s) into rinfo_.
791 // *Get* just reads and returns info on current byte.
792 void Advance(int bytes = 1) { pos_ -= bytes; }
793 int AdvanceGetTag();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000794 RelocInfo::Mode GetMode();
795
796 void AdvanceReadLongPCJump();
797
798 int GetShortDataTypeTag();
799 void ReadShortTaggedPC();
800 void ReadShortTaggedId();
801 void ReadShortTaggedPosition();
802 void ReadShortTaggedData();
803
Steve Blocka7e24c12009-10-30 11:49:00 +0000804 void AdvanceReadPC();
Ben Murdoch257744e2011-11-30 15:57:28 +0000805 void AdvanceReadId();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000806 void AdvanceReadInt();
Ben Murdoch257744e2011-11-30 15:57:28 +0000807 void AdvanceReadPosition();
Steve Blocka7e24c12009-10-30 11:49:00 +0000808 void AdvanceReadData();
Steve Blocka7e24c12009-10-30 11:49:00 +0000809
810 // If the given mode is wanted, set it in rinfo_ and return true.
811 // Else return false. Used for efficiently skipping unwanted modes.
812 bool SetMode(RelocInfo::Mode mode) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100813 return (mode_mask_ & (1 << mode)) ? (rinfo_.rmode_ = mode, true) : false;
Steve Blocka7e24c12009-10-30 11:49:00 +0000814 }
815
816 byte* pos_;
817 byte* end_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000818 byte* code_age_sequence_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000819 RelocInfo rinfo_;
820 bool done_;
821 int mode_mask_;
Ben Murdoch257744e2011-11-30 15:57:28 +0000822 int last_id_;
823 int last_position_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000824 DISALLOW_COPY_AND_ASSIGN(RelocIterator);
825};
826
827
828//------------------------------------------------------------------------------
829// External function
830
831//----------------------------------------------------------------------------
Steve Blocka7e24c12009-10-30 11:49:00 +0000832class SCTableReference;
Steve Blocka7e24c12009-10-30 11:49:00 +0000833class Debug_Address;
Steve Blocka7e24c12009-10-30 11:49:00 +0000834
835
Steve Blocka7e24c12009-10-30 11:49:00 +0000836// An ExternalReference represents a C++ address used in the generated
837// code. All references to C++ functions and variables must be encapsulated in
838// an ExternalReference instance. This is done in order to track the origin of
839// all external references in the code so that they can be bound to the correct
840// addresses when deserializing a heap.
841class ExternalReference BASE_EMBEDDED {
842 public:
Steve Block1e0659c2011-05-24 12:43:12 +0100843 // Used in the simulator to support different native api calls.
Steve Block1e0659c2011-05-24 12:43:12 +0100844 enum Type {
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100845 // Builtin call.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000846 // Object* f(v8::internal::Arguments).
Steve Block1e0659c2011-05-24 12:43:12 +0100847 BUILTIN_CALL, // default
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100848
Ben Murdoch097c5b22016-05-18 11:27:45 +0100849 // Builtin call returning object pair.
850 // ObjectPair f(v8::internal::Arguments).
851 BUILTIN_CALL_PAIR,
852
853 // Builtin call that returns .
854 // ObjectTriple f(v8::internal::Arguments).
855 BUILTIN_CALL_TRIPLE,
856
Ben Murdoch257744e2011-11-30 15:57:28 +0000857 // Builtin that takes float arguments and returns an int.
858 // int f(double, double).
859 BUILTIN_COMPARE_CALL,
860
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100861 // Builtin call that returns floating point.
862 // double f(double, double).
Ben Murdoch257744e2011-11-30 15:57:28 +0000863 BUILTIN_FP_FP_CALL,
864
865 // Builtin call that returns floating point.
866 // double f(double).
867 BUILTIN_FP_CALL,
868
869 // Builtin call that returns floating point.
870 // double f(double, int).
871 BUILTIN_FP_INT_CALL,
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100872
873 // Direct call to API function callback.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000874 // void f(v8::FunctionCallbackInfo&)
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100875 DIRECT_API_CALL,
876
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000877 // Call to function callback via InvokeFunctionCallback.
878 // void f(v8::FunctionCallbackInfo&, v8::FunctionCallback)
879 PROFILING_API_CALL,
880
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100881 // Direct call to accessor getter callback.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000882 // void f(Local<Name> property, PropertyCallbackInfo& info)
883 DIRECT_GETTER_CALL,
884
885 // Call to accessor getter callback via InvokeAccessorGetterCallback.
886 // void f(Local<Name> property, PropertyCallbackInfo& info,
887 // AccessorNameGetterCallback callback)
888 PROFILING_GETTER_CALL
Steve Block1e0659c2011-05-24 12:43:12 +0100889 };
890
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000891 static void SetUp();
892 static void InitializeMathExpData();
893 static void TearDownMathExpData();
894
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000895 typedef void* ExternalReferenceRedirector(Isolate* isolate, void* original,
896 Type type);
Steve Block1e0659c2011-05-24 12:43:12 +0100897
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000898 ExternalReference() : address_(NULL) {}
899
Steve Block44f0eee2011-05-26 01:26:41 +0100900 ExternalReference(Builtins::CFunctionId id, Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000901
Steve Block44f0eee2011-05-26 01:26:41 +0100902 ExternalReference(ApiFunction* ptr, Type type, Isolate* isolate);
Steve Blockd0582a62009-12-15 09:54:21 +0000903
Steve Block44f0eee2011-05-26 01:26:41 +0100904 ExternalReference(Builtins::Name name, Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000905
Steve Block44f0eee2011-05-26 01:26:41 +0100906 ExternalReference(Runtime::FunctionId id, Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000907
Steve Block44f0eee2011-05-26 01:26:41 +0100908 ExternalReference(const Runtime::Function* f, Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000909
Steve Blocka7e24c12009-10-30 11:49:00 +0000910 explicit ExternalReference(StatsCounter* counter);
911
Steve Block44f0eee2011-05-26 01:26:41 +0100912 ExternalReference(Isolate::AddressId id, Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000913
914 explicit ExternalReference(const SCTableReference& table_ref);
915
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000916 // Isolate as an external reference.
917 static ExternalReference isolate_address(Isolate* isolate);
Steve Block44f0eee2011-05-26 01:26:41 +0100918
Steve Blocka7e24c12009-10-30 11:49:00 +0000919 // One-of-a-kind references. These references are not part of a general
920 // pattern. This means that they have to be added to the
921 // ExternalReferenceTable in serialize.cc manually.
922
Ben Murdoch097c5b22016-05-18 11:27:45 +0100923 static ExternalReference interpreter_dispatch_table_address(Isolate* isolate);
Ben Murdochc5610432016-08-08 18:44:38 +0100924 static ExternalReference interpreter_dispatch_counters(Isolate* isolate);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100925
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100926 static ExternalReference incremental_marking_record_write_function(
927 Isolate* isolate);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100928 static ExternalReference incremental_marking_record_write_code_entry_function(
929 Isolate* isolate);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100930 static ExternalReference store_buffer_overflow_function(
931 Isolate* isolate);
Steve Block44f0eee2011-05-26 01:26:41 +0100932 static ExternalReference delete_handle_scope_extensions(Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000933
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100934 static ExternalReference get_date_field_function(Isolate* isolate);
935 static ExternalReference date_cache_stamp(Isolate* isolate);
936
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000937 static ExternalReference get_make_code_young_function(Isolate* isolate);
938 static ExternalReference get_mark_code_as_executed_function(Isolate* isolate);
939
Ben Murdochb0fe1622011-05-05 13:52:32 +0100940 // Deoptimization support.
Steve Block44f0eee2011-05-26 01:26:41 +0100941 static ExternalReference new_deoptimizer_function(Isolate* isolate);
942 static ExternalReference compute_output_frames_function(Isolate* isolate);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100943
Ben Murdochda12d292016-06-02 14:46:10 +0100944 static ExternalReference wasm_f32_trunc(Isolate* isolate);
945 static ExternalReference wasm_f32_floor(Isolate* isolate);
946 static ExternalReference wasm_f32_ceil(Isolate* isolate);
947 static ExternalReference wasm_f32_nearest_int(Isolate* isolate);
948 static ExternalReference wasm_f64_trunc(Isolate* isolate);
949 static ExternalReference wasm_f64_floor(Isolate* isolate);
950 static ExternalReference wasm_f64_ceil(Isolate* isolate);
951 static ExternalReference wasm_f64_nearest_int(Isolate* isolate);
952 static ExternalReference wasm_int64_to_float32(Isolate* isolate);
953 static ExternalReference wasm_uint64_to_float32(Isolate* isolate);
954 static ExternalReference wasm_int64_to_float64(Isolate* isolate);
955 static ExternalReference wasm_uint64_to_float64(Isolate* isolate);
956 static ExternalReference wasm_float32_to_int64(Isolate* isolate);
957 static ExternalReference wasm_float32_to_uint64(Isolate* isolate);
958 static ExternalReference wasm_float64_to_int64(Isolate* isolate);
959 static ExternalReference wasm_float64_to_uint64(Isolate* isolate);
960 static ExternalReference wasm_int64_div(Isolate* isolate);
961 static ExternalReference wasm_int64_mod(Isolate* isolate);
962 static ExternalReference wasm_uint64_div(Isolate* isolate);
963 static ExternalReference wasm_uint64_mod(Isolate* isolate);
Ben Murdochc5610432016-08-08 18:44:38 +0100964 static ExternalReference wasm_word32_ctz(Isolate* isolate);
965 static ExternalReference wasm_word64_ctz(Isolate* isolate);
966 static ExternalReference wasm_word32_popcnt(Isolate* isolate);
967 static ExternalReference wasm_word64_popcnt(Isolate* isolate);
Ben Murdochda12d292016-06-02 14:46:10 +0100968
969 static ExternalReference f64_acos_wrapper_function(Isolate* isolate);
970 static ExternalReference f64_asin_wrapper_function(Isolate* isolate);
971 static ExternalReference f64_atan_wrapper_function(Isolate* isolate);
972 static ExternalReference f64_cos_wrapper_function(Isolate* isolate);
973 static ExternalReference f64_sin_wrapper_function(Isolate* isolate);
974 static ExternalReference f64_tan_wrapper_function(Isolate* isolate);
975 static ExternalReference f64_exp_wrapper_function(Isolate* isolate);
976 static ExternalReference f64_log_wrapper_function(Isolate* isolate);
977 static ExternalReference f64_atan2_wrapper_function(Isolate* isolate);
978 static ExternalReference f64_pow_wrapper_function(Isolate* isolate);
979 static ExternalReference f64_mod_wrapper_function(Isolate* isolate);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100980
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000981 // Log support.
982 static ExternalReference log_enter_external_function(Isolate* isolate);
983 static ExternalReference log_leave_external_function(Isolate* isolate);
984
Leon Clarkee46be812010-01-19 14:06:41 +0000985 // Static data in the keyed lookup cache.
Steve Block44f0eee2011-05-26 01:26:41 +0100986 static ExternalReference keyed_lookup_cache_keys(Isolate* isolate);
987 static ExternalReference keyed_lookup_cache_field_offsets(Isolate* isolate);
Leon Clarkee46be812010-01-19 14:06:41 +0000988
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100989 // Static variable Heap::roots_array_start()
990 static ExternalReference roots_array_start(Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000991
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000992 // Static variable Heap::allocation_sites_list_address()
993 static ExternalReference allocation_sites_list_address(Isolate* isolate);
994
Steve Blocka7e24c12009-10-30 11:49:00 +0000995 // Static variable StackGuard::address_of_jslimit()
Steve Block44f0eee2011-05-26 01:26:41 +0100996 static ExternalReference address_of_stack_limit(Isolate* isolate);
Steve Blockd0582a62009-12-15 09:54:21 +0000997
998 // Static variable StackGuard::address_of_real_jslimit()
Steve Block44f0eee2011-05-26 01:26:41 +0100999 static ExternalReference address_of_real_stack_limit(Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00001000
1001 // Static variable RegExpStack::limit_address()
Steve Block44f0eee2011-05-26 01:26:41 +01001002 static ExternalReference address_of_regexp_stack_limit(Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00001003
Leon Clarkee46be812010-01-19 14:06:41 +00001004 // Static variables for RegExp.
Steve Block44f0eee2011-05-26 01:26:41 +01001005 static ExternalReference address_of_static_offsets_vector(Isolate* isolate);
1006 static ExternalReference address_of_regexp_stack_memory_address(
1007 Isolate* isolate);
1008 static ExternalReference address_of_regexp_stack_memory_size(
1009 Isolate* isolate);
Leon Clarkee46be812010-01-19 14:06:41 +00001010
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001011 // Write barrier.
1012 static ExternalReference store_buffer_top(Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00001013
1014 // Used for fast allocation in generated code.
Steve Block44f0eee2011-05-26 01:26:41 +01001015 static ExternalReference new_space_allocation_top_address(Isolate* isolate);
1016 static ExternalReference new_space_allocation_limit_address(Isolate* isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001017 static ExternalReference old_space_allocation_top_address(Isolate* isolate);
1018 static ExternalReference old_space_allocation_limit_address(Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00001019
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001020 static ExternalReference mod_two_doubles_operation(Isolate* isolate);
Steve Block44f0eee2011-05-26 01:26:41 +01001021 static ExternalReference power_double_double_function(Isolate* isolate);
1022 static ExternalReference power_double_int_function(Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00001023
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001024 static ExternalReference handle_scope_next_address(Isolate* isolate);
1025 static ExternalReference handle_scope_limit_address(Isolate* isolate);
1026 static ExternalReference handle_scope_level_address(Isolate* isolate);
Steve Blockd0582a62009-12-15 09:54:21 +00001027
Steve Block44f0eee2011-05-26 01:26:41 +01001028 static ExternalReference scheduled_exception_address(Isolate* isolate);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001029 static ExternalReference address_of_pending_message_obj(Isolate* isolate);
Steve Blockd0582a62009-12-15 09:54:21 +00001030
Ben Murdochb0fe1622011-05-05 13:52:32 +01001031 // Static variables containing common double constants.
1032 static ExternalReference address_of_min_int();
1033 static ExternalReference address_of_one_half();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001034 static ExternalReference address_of_minus_one_half();
Ben Murdochb0fe1622011-05-05 13:52:32 +01001035 static ExternalReference address_of_negative_infinity();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001036 static ExternalReference address_of_the_hole_nan();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001037 static ExternalReference address_of_uint32_bias();
Ben Murdochb0fe1622011-05-05 13:52:32 +01001038
Steve Block44f0eee2011-05-26 01:26:41 +01001039 static ExternalReference math_log_double_function(Isolate* isolate);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001040
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001041 static ExternalReference math_exp_constants(int constant_index);
1042 static ExternalReference math_exp_log_table();
Steve Blocka7e24c12009-10-30 11:49:00 +00001043
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001044 static ExternalReference page_flags(Page* page);
1045
1046 static ExternalReference ForDeoptEntry(Address entry);
1047
1048 static ExternalReference cpu_features();
1049
Ben Murdochda12d292016-06-02 14:46:10 +01001050 static ExternalReference is_tail_call_elimination_enabled_address(
1051 Isolate* isolate);
1052
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001053 static ExternalReference debug_is_active_address(Isolate* isolate);
1054 static ExternalReference debug_after_break_target_address(Isolate* isolate);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001055
1056 static ExternalReference is_profiling_address(Isolate* isolate);
1057 static ExternalReference invoke_function_callback(Isolate* isolate);
1058 static ExternalReference invoke_accessor_getter_callback(Isolate* isolate);
1059
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001060 static ExternalReference virtual_handler_register(Isolate* isolate);
1061 static ExternalReference virtual_slot_register(Isolate* isolate);
1062
1063 static ExternalReference runtime_function_table_address(Isolate* isolate);
1064
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001065 Address address() const { return reinterpret_cast<Address>(address_); }
1066
Steve Blocka7e24c12009-10-30 11:49:00 +00001067 // Used to check if single stepping is enabled in generated code.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001068 static ExternalReference debug_step_in_enabled_address(Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00001069
Steve Block6ded16b2010-05-10 14:33:55 +01001070#ifndef V8_INTERPRETED_REGEXP
Steve Blocka7e24c12009-10-30 11:49:00 +00001071 // C functions called from RegExp generated code.
1072
1073 // Function NativeRegExpMacroAssembler::CaseInsensitiveCompareUC16()
Steve Block44f0eee2011-05-26 01:26:41 +01001074 static ExternalReference re_case_insensitive_compare_uc16(Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00001075
1076 // Function RegExpMacroAssembler*::CheckStackGuardState()
Steve Block44f0eee2011-05-26 01:26:41 +01001077 static ExternalReference re_check_stack_guard_state(Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00001078
1079 // Function NativeRegExpMacroAssembler::GrowStack()
Steve Block44f0eee2011-05-26 01:26:41 +01001080 static ExternalReference re_grow_stack(Isolate* isolate);
Leon Clarkee46be812010-01-19 14:06:41 +00001081
1082 // byte NativeRegExpMacroAssembler::word_character_bitmap
1083 static ExternalReference re_word_character_map();
1084
Steve Blocka7e24c12009-10-30 11:49:00 +00001085#endif
1086
1087 // This lets you register a function that rewrites all external references.
1088 // Used by the ARM simulator to catch calls to external references.
Ben Murdoch257744e2011-11-30 15:57:28 +00001089 static void set_redirector(Isolate* isolate,
1090 ExternalReferenceRedirector* redirector) {
Steve Block44f0eee2011-05-26 01:26:41 +01001091 // We can't stack them.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001092 DCHECK(isolate->external_reference_redirector() == NULL);
Ben Murdoch257744e2011-11-30 15:57:28 +00001093 isolate->set_external_reference_redirector(
Steve Block44f0eee2011-05-26 01:26:41 +01001094 reinterpret_cast<ExternalReferenceRedirectorPointer*>(redirector));
Steve Blocka7e24c12009-10-30 11:49:00 +00001095 }
1096
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001097 static ExternalReference stress_deopt_count(Isolate* isolate);
1098
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001099 static ExternalReference fixed_typed_array_base_data_offset();
1100
Steve Blocka7e24c12009-10-30 11:49:00 +00001101 private:
1102 explicit ExternalReference(void* address)
1103 : address_(address) {}
1104
Steve Block44f0eee2011-05-26 01:26:41 +01001105 static void* Redirect(Isolate* isolate,
Steve Block44f0eee2011-05-26 01:26:41 +01001106 Address address_arg,
Steve Block1e0659c2011-05-24 12:43:12 +01001107 Type type = ExternalReference::BUILTIN_CALL) {
Steve Block44f0eee2011-05-26 01:26:41 +01001108 ExternalReferenceRedirector* redirector =
1109 reinterpret_cast<ExternalReferenceRedirector*>(
1110 isolate->external_reference_redirector());
Steve Blocka7e24c12009-10-30 11:49:00 +00001111 void* address = reinterpret_cast<void*>(address_arg);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001112 void* answer =
1113 (redirector == NULL) ? address : (*redirector)(isolate, address, type);
Steve Blockd0582a62009-12-15 09:54:21 +00001114 return answer;
Steve Blocka7e24c12009-10-30 11:49:00 +00001115 }
1116
1117 void* address_;
1118};
1119
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001120bool operator==(ExternalReference, ExternalReference);
1121bool operator!=(ExternalReference, ExternalReference);
1122
1123size_t hash_value(ExternalReference);
1124
1125std::ostream& operator<<(std::ostream&, ExternalReference);
1126
Steve Blocka7e24c12009-10-30 11:49:00 +00001127
1128// -----------------------------------------------------------------------------
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001129// Position recording support
1130
Ben Murdochb0fe1622011-05-05 13:52:32 +01001131struct PositionState {
1132 PositionState() : current_position(RelocInfo::kNoPosition),
1133 written_position(RelocInfo::kNoPosition),
1134 current_statement_position(RelocInfo::kNoPosition),
1135 written_statement_position(RelocInfo::kNoPosition) {}
1136
1137 int current_position;
1138 int written_position;
1139
1140 int current_statement_position;
1141 int written_statement_position;
1142};
1143
Ben Murdochda12d292016-06-02 14:46:10 +01001144class AssemblerPositionsRecorder : public PositionsRecorder {
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001145 public:
Ben Murdochda12d292016-06-02 14:46:10 +01001146 explicit AssemblerPositionsRecorder(Assembler* assembler)
1147 : assembler_(assembler) {}
Ben Murdochb8e0da22011-05-16 14:20:40 +01001148
Ben Murdochb0fe1622011-05-05 13:52:32 +01001149 // Set current position to pos.
1150 void RecordPosition(int pos);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001151
1152 // Set current statement position to pos.
1153 void RecordStatementPosition(int pos);
1154
1155 // Write recorded positions to relocation information.
1156 bool WriteRecordedPositions();
1157
Ben Murdochb0fe1622011-05-05 13:52:32 +01001158 int current_position() const { return state_.current_position; }
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001159
Ben Murdochb0fe1622011-05-05 13:52:32 +01001160 int current_statement_position() const {
1161 return state_.current_statement_position;
1162 }
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001163
1164 private:
1165 Assembler* assembler_;
Ben Murdochb0fe1622011-05-05 13:52:32 +01001166 PositionState state_;
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001167
Ben Murdochda12d292016-06-02 14:46:10 +01001168 DISALLOW_COPY_AND_ASSIGN(AssemblerPositionsRecorder);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001169};
1170
1171
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001172// -----------------------------------------------------------------------------
Steve Blocka7e24c12009-10-30 11:49:00 +00001173// Utility functions
1174
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001175inline int NumberOfBitsSet(uint32_t x) {
Andrei Popescu31002712010-02-23 13:46:05 +00001176 unsigned int num_bits_set;
1177 for (num_bits_set = 0; x; x >>= 1) {
1178 num_bits_set += x & 1;
1179 }
1180 return num_bits_set;
1181}
Steve Blocka7e24c12009-10-30 11:49:00 +00001182
Ben Murdochb0fe1622011-05-05 13:52:32 +01001183// Computes pow(x, y) with the special cases in the spec for Math.pow.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001184double power_helper(Isolate* isolate, double x, double y);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001185double power_double_int(double x, int y);
1186double power_double_double(double x, double y);
1187
Ben Murdoch257744e2011-11-30 15:57:28 +00001188// Helper class for generating code or data associated with the code
1189// right after a call instruction. As an example this can be used to
1190// generate safepoint data after calls for crankshaft.
1191class CallWrapper {
1192 public:
1193 CallWrapper() { }
1194 virtual ~CallWrapper() { }
1195 // Called just before emitting a call. Argument is the size of the generated
1196 // call code.
1197 virtual void BeforeCall(int call_size) const = 0;
1198 // Called just after emitting a call, i.e., at the return site for the call.
1199 virtual void AfterCall() const = 0;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001200 // Return whether call needs to check for debug stepping.
1201 virtual bool NeedsDebugStepCheck() const { return false; }
Ben Murdoch257744e2011-11-30 15:57:28 +00001202};
1203
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001204
Ben Murdoch257744e2011-11-30 15:57:28 +00001205class NullCallWrapper : public CallWrapper {
1206 public:
1207 NullCallWrapper() { }
1208 virtual ~NullCallWrapper() { }
1209 virtual void BeforeCall(int call_size) const { }
1210 virtual void AfterCall() const { }
1211};
1212
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001213
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001214class CheckDebugStepCallWrapper : public CallWrapper {
1215 public:
1216 CheckDebugStepCallWrapper() {}
1217 virtual ~CheckDebugStepCallWrapper() {}
1218 virtual void BeforeCall(int call_size) const {}
1219 virtual void AfterCall() const {}
1220 virtual bool NeedsDebugStepCheck() const { return true; }
1221};
Steve Blocka7e24c12009-10-30 11:49:00 +00001222
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001223
1224// -----------------------------------------------------------------------------
1225// Constant pool support
1226
1227class ConstantPoolEntry {
1228 public:
1229 ConstantPoolEntry() {}
1230 ConstantPoolEntry(int position, intptr_t value, bool sharing_ok)
1231 : position_(position),
1232 merged_index_(sharing_ok ? SHARING_ALLOWED : SHARING_PROHIBITED),
1233 value_(value) {}
1234 ConstantPoolEntry(int position, double value)
1235 : position_(position), merged_index_(SHARING_ALLOWED), value64_(value) {}
1236
1237 int position() const { return position_; }
1238 bool sharing_ok() const { return merged_index_ != SHARING_PROHIBITED; }
1239 bool is_merged() const { return merged_index_ >= 0; }
1240 int merged_index(void) const {
1241 DCHECK(is_merged());
1242 return merged_index_;
1243 }
1244 void set_merged_index(int index) {
1245 merged_index_ = index;
1246 DCHECK(is_merged());
1247 }
1248 int offset(void) const {
1249 DCHECK(merged_index_ >= 0);
1250 return merged_index_;
1251 }
1252 void set_offset(int offset) {
1253 DCHECK(offset >= 0);
1254 merged_index_ = offset;
1255 }
1256 intptr_t value() const { return value_; }
1257 uint64_t value64() const { return bit_cast<uint64_t>(value64_); }
1258
1259 enum Type { INTPTR, DOUBLE, NUMBER_OF_TYPES };
1260
1261 static int size(Type type) {
1262 return (type == INTPTR) ? kPointerSize : kDoubleSize;
1263 }
1264
1265 enum Access { REGULAR, OVERFLOWED };
1266
1267 private:
1268 int position_;
1269 int merged_index_;
1270 union {
1271 intptr_t value_;
1272 double value64_;
1273 };
1274 enum { SHARING_PROHIBITED = -2, SHARING_ALLOWED = -1 };
1275};
1276
1277
1278// -----------------------------------------------------------------------------
1279// Embedded constant pool support
1280
1281class ConstantPoolBuilder BASE_EMBEDDED {
1282 public:
1283 ConstantPoolBuilder(int ptr_reach_bits, int double_reach_bits);
1284
1285 // Add pointer-sized constant to the embedded constant pool
1286 ConstantPoolEntry::Access AddEntry(int position, intptr_t value,
1287 bool sharing_ok) {
1288 ConstantPoolEntry entry(position, value, sharing_ok);
1289 return AddEntry(entry, ConstantPoolEntry::INTPTR);
1290 }
1291
1292 // Add double constant to the embedded constant pool
1293 ConstantPoolEntry::Access AddEntry(int position, double value) {
1294 ConstantPoolEntry entry(position, value);
1295 return AddEntry(entry, ConstantPoolEntry::DOUBLE);
1296 }
1297
1298 // Previews the access type required for the next new entry to be added.
1299 ConstantPoolEntry::Access NextAccess(ConstantPoolEntry::Type type) const;
1300
1301 bool IsEmpty() {
1302 return info_[ConstantPoolEntry::INTPTR].entries.empty() &&
1303 info_[ConstantPoolEntry::INTPTR].shared_entries.empty() &&
1304 info_[ConstantPoolEntry::DOUBLE].entries.empty() &&
1305 info_[ConstantPoolEntry::DOUBLE].shared_entries.empty();
1306 }
1307
1308 // Emit the constant pool. Invoke only after all entries have been
1309 // added and all instructions have been emitted.
1310 // Returns position of the emitted pool (zero implies no constant pool).
1311 int Emit(Assembler* assm);
1312
1313 // Returns the label associated with the start of the constant pool.
1314 // Linking to this label in the function prologue may provide an
1315 // efficient means of constant pool pointer register initialization
1316 // on some architectures.
1317 inline Label* EmittedPosition() { return &emitted_label_; }
1318
1319 private:
1320 ConstantPoolEntry::Access AddEntry(ConstantPoolEntry& entry,
1321 ConstantPoolEntry::Type type);
1322 void EmitSharedEntries(Assembler* assm, ConstantPoolEntry::Type type);
1323 void EmitGroup(Assembler* assm, ConstantPoolEntry::Access access,
1324 ConstantPoolEntry::Type type);
1325
1326 struct PerTypeEntryInfo {
1327 PerTypeEntryInfo() : regular_count(0), overflow_start(-1) {}
1328 bool overflow() const {
1329 return (overflow_start >= 0 &&
1330 overflow_start < static_cast<int>(entries.size()));
1331 }
1332 int regular_reach_bits;
1333 int regular_count;
1334 int overflow_start;
1335 std::vector<ConstantPoolEntry> entries;
1336 std::vector<ConstantPoolEntry> shared_entries;
1337 };
1338
1339 Label emitted_label_; // Records pc_offset of emitted pool
1340 PerTypeEntryInfo info_[ConstantPoolEntry::NUMBER_OF_TYPES];
1341};
1342
1343} // namespace internal
1344} // namespace v8
Steve Blocka7e24c12009-10-30 11:49:00 +00001345#endif // V8_ASSEMBLER_H_