blob: fb5ac1f007955a2c3c32610b5736dec0c53ab407 [file] [log] [blame]
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +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.
yangguo@chromium.org659ceec2012-01-26 07:37:54 +000033// Copyright 2012 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000034
35#ifndef V8_ASSEMBLER_H_
36#define V8_ASSEMBLER_H_
37
yangguo@chromium.org659ceec2012-01-26 07:37:54 +000038#include "v8.h"
39
lrn@chromium.org1c092762011-05-09 09:42:16 +000040#include "allocation.h"
yangguo@chromium.org659ceec2012-01-26 07:37:54 +000041#include "builtins.h"
erik.corry@gmail.com0511e242011-01-19 11:11:08 +000042#include "gdb-jit.h"
yangguo@chromium.org659ceec2012-01-26 07:37:54 +000043#include "isolate.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000044#include "runtime.h"
ager@chromium.org65dad4b2009-04-23 08:48:43 +000045#include "token.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000046
kasperl@chromium.org71affb52009-05-26 05:44:31 +000047namespace v8 {
yangguo@chromium.org659ceec2012-01-26 07:37:54 +000048
49class ApiFunction;
50
kasperl@chromium.org71affb52009-05-26 05:44:31 +000051namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000052
yangguo@chromium.org659ceec2012-01-26 07:37:54 +000053struct StatsCounter;
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +000054const unsigned kNoASTId = -1;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000055// -----------------------------------------------------------------------------
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000056// Platform independent assembler base class.
57
58class AssemblerBase: public Malloced {
59 public:
vegorov@chromium.org7304bca2011-05-16 12:14:13 +000060 explicit AssemblerBase(Isolate* isolate);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000061
62 Isolate* isolate() const { return isolate_; }
vegorov@chromium.org7304bca2011-05-16 12:14:13 +000063 int jit_cookie() { return jit_cookie_; }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000064
jkummerow@chromium.org28faa982012-04-13 09:58:30 +000065 // Overwrite a host NaN with a quiet target NaN. Used by mksnapshot for
66 // cross-snapshotting.
67 static void QuietNaN(HeapObject* nan) { }
68
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000069 private:
70 Isolate* isolate_;
vegorov@chromium.org7304bca2011-05-16 12:14:13 +000071 int jit_cookie_;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000072};
73
kasperl@chromium.orga5551262010-12-07 12:49:48 +000074
75// -----------------------------------------------------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000076// Labels represent pc locations; they are typically jump or call targets.
77// After declaration, a label can be freely used to denote known or (yet)
78// unknown pc location. Assembler::bind() is used to bind a label to the
79// current pc. A label can be bound only once.
80
kasperl@chromium.org7be3c992009-03-12 07:19:55 +000081class Label BASE_EMBEDDED {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000082 public:
karlklose@chromium.org83a47282011-05-11 11:54:09 +000083 enum Distance {
84 kNear, kFar
85 };
86
87 INLINE(Label()) {
88 Unuse();
89 UnuseNear();
90 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000091
whesse@chromium.org7b260152011-06-20 15:33:18 +000092 INLINE(~Label()) {
93 ASSERT(!is_linked());
94 ASSERT(!is_near_linked());
95 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000096
whesse@chromium.org7b260152011-06-20 15:33:18 +000097 INLINE(void Unuse()) { pos_ = 0; }
98 INLINE(void UnuseNear()) { near_link_pos_ = 0; }
99
100 INLINE(bool is_bound() const) { return pos_ < 0; }
101 INLINE(bool is_unused() const) { return pos_ == 0 && near_link_pos_ == 0; }
102 INLINE(bool is_linked() const) { return pos_ > 0; }
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000103 INLINE(bool is_near_linked() const) { return near_link_pos_ > 0; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000104
105 // Returns the position of bound or linked labels. Cannot be used
106 // for unused labels.
107 int pos() const;
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000108 int near_link_pos() const { return near_link_pos_ - 1; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000109
110 private:
111 // pos_ encodes both the binding state (via its sign)
112 // and the binding position (via its value) of a label.
113 //
114 // pos_ < 0 bound label, pos() returns the jump target position
115 // pos_ == 0 unused label
116 // pos_ > 0 linked label, pos() returns the last reference position
117 int pos_;
118
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000119 // Behaves like |pos_| in the "> 0" case, but for near jumps to this label.
120 int near_link_pos_;
121
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000122 void bind_to(int pos) {
123 pos_ = -pos - 1;
124 ASSERT(is_bound());
125 }
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000126 void link_to(int pos, Distance distance = kFar) {
127 if (distance == kNear) {
128 near_link_pos_ = pos + 1;
129 ASSERT(is_near_linked());
130 } else {
131 pos_ = pos + 1;
132 ASSERT(is_linked());
133 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000134 }
135
136 friend class Assembler;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000137 friend class RegexpAssembler;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000138 friend class Displacement;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000139 friend class RegExpMacroAssemblerIrregexp;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000140};
141
142
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000143enum SaveFPRegsMode { kDontSaveFPRegs, kSaveFPRegs };
144
145
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000146// -----------------------------------------------------------------------------
147// Relocation information
148
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000149
150// Relocation information consists of the address (pc) of the datum
151// to which the relocation information applies, the relocation mode
152// (rmode), and an optional data field. The relocation mode may be
153// "descriptive" and not indicate a need for relocation, but simply
154// describe a property of the datum. Such rmodes are useful for GC
155// and nice disassembly output.
156
157class RelocInfo BASE_EMBEDDED {
158 public:
ager@chromium.org236ad962008-09-25 09:45:57 +0000159 // The constant kNoPosition is used with the collecting of source positions
160 // in the relocation information. Two types of source positions are collected
161 // "position" (RelocMode position) and "statement position" (RelocMode
162 // statement_position). The "position" is collected at places in the source
163 // code which are of interest when making stack traces to pin-point the source
164 // location of a stack frame as close as possible. The "statement position" is
165 // collected at the beginning at each statement, and is used to indicate
166 // possible break locations. kNoPosition is used to indicate an
167 // invalid/uninitialized position value.
168 static const int kNoPosition = -1;
169
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +0000170 // This string is used to add padding comments to the reloc info in cases
171 // where we are not sure to have enough space for patching in during
172 // lazy deoptimization. This is the case if we have indirect calls for which
173 // we do not normally record relocation info.
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +0000174 static const char* const kFillerCommentString;
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +0000175
ricow@chromium.org22334512011-02-25 07:28:50 +0000176 // The minimum size of a comment is equal to three bytes for the extra tagged
177 // pc + the tag for the data, and kPointerSize for the actual pointer to the
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000178 // comment.
ricow@chromium.org22334512011-02-25 07:28:50 +0000179 static const int kMinRelocCommentSize = 3 + kPointerSize;
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000180
181 // The maximum size for a call instruction including pc-jump.
182 static const int kMaxCallSize = 6;
183
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000184 // The maximum pc delta that will use the short encoding.
185 static const int kMaxSmallPCDelta;
186
ager@chromium.org236ad962008-09-25 09:45:57 +0000187 enum Mode {
188 // Please note the order is important (see IsCodeTarget, IsGCRelocMode).
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000189 CODE_TARGET, // Code target which is not any of the above.
190 CODE_TARGET_WITH_ID,
ager@chromium.org236ad962008-09-25 09:45:57 +0000191 CONSTRUCT_CALL, // code target that is a call to a JavaScript constructor.
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000192 CODE_TARGET_CONTEXT, // Code target used for contextual loads and stores.
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000193 DEBUG_BREAK, // Code target for the debugger statement.
ager@chromium.org236ad962008-09-25 09:45:57 +0000194 EMBEDDED_OBJECT,
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000195 GLOBAL_PROPERTY_CELL,
196
ager@chromium.org236ad962008-09-25 09:45:57 +0000197 // Everything after runtime_entry (inclusive) is not GC'ed.
198 RUNTIME_ENTRY,
199 JS_RETURN, // Marks start of the ExitJSFrame code.
200 COMMENT,
201 POSITION, // See comment for kNoPosition above.
202 STATEMENT_POSITION, // See comment for kNoPosition above.
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000203 DEBUG_BREAK_SLOT, // Additional code inserted for debug break slot.
ager@chromium.org236ad962008-09-25 09:45:57 +0000204 EXTERNAL_REFERENCE, // The address of an external C++ function.
205 INTERNAL_REFERENCE, // An address inside the same function.
206
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000207 // Marks a constant pool. Only used on ARM.
208 // It uses a custom noncompact encoding.
209 CONST_POOL,
210
ager@chromium.org236ad962008-09-25 09:45:57 +0000211 // add more as needed
212 // Pseudo-types
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000213 NUMBER_OF_MODES, // There are at most 15 modes with noncompact encoding.
ager@chromium.org236ad962008-09-25 09:45:57 +0000214 NONE, // never recorded
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000215 LAST_CODE_ENUM = DEBUG_BREAK,
216 LAST_GCED_ENUM = GLOBAL_PROPERTY_CELL,
217 // Modes <= LAST_COMPACT_ENUM are guaranteed to have compact encoding.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000218 LAST_COMPACT_ENUM = CODE_TARGET_WITH_ID,
219 LAST_STANDARD_NONCOMPACT_ENUM = INTERNAL_REFERENCE
ager@chromium.org236ad962008-09-25 09:45:57 +0000220 };
221
222
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000223 RelocInfo() {}
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000224
225 RelocInfo(byte* pc, Mode rmode, intptr_t data, Code* host)
226 : pc_(pc), rmode_(rmode), data_(data), host_(host) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000227 }
228
ager@chromium.org236ad962008-09-25 09:45:57 +0000229 static inline bool IsConstructCall(Mode mode) {
230 return mode == CONSTRUCT_CALL;
231 }
232 static inline bool IsCodeTarget(Mode mode) {
233 return mode <= LAST_CODE_ENUM;
234 }
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +0000235 static inline bool IsEmbeddedObject(Mode mode) {
236 return mode == EMBEDDED_OBJECT;
237 }
ager@chromium.org236ad962008-09-25 09:45:57 +0000238 // Is the relocation mode affected by GC?
239 static inline bool IsGCRelocMode(Mode mode) {
240 return mode <= LAST_GCED_ENUM;
241 }
242 static inline bool IsJSReturn(Mode mode) {
243 return mode == JS_RETURN;
244 }
245 static inline bool IsComment(Mode mode) {
246 return mode == COMMENT;
247 }
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000248 static inline bool IsConstPool(Mode mode) {
249 return mode == CONST_POOL;
250 }
ager@chromium.org236ad962008-09-25 09:45:57 +0000251 static inline bool IsPosition(Mode mode) {
252 return mode == POSITION || mode == STATEMENT_POSITION;
253 }
254 static inline bool IsStatementPosition(Mode mode) {
255 return mode == STATEMENT_POSITION;
256 }
257 static inline bool IsExternalReference(Mode mode) {
258 return mode == EXTERNAL_REFERENCE;
259 }
260 static inline bool IsInternalReference(Mode mode) {
261 return mode == INTERNAL_REFERENCE;
262 }
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000263 static inline bool IsDebugBreakSlot(Mode mode) {
264 return mode == DEBUG_BREAK_SLOT;
265 }
ager@chromium.org236ad962008-09-25 09:45:57 +0000266 static inline int ModeMask(Mode mode) { return 1 << mode; }
267
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000268 // Accessors
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000269 byte* pc() const { return pc_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000270 void set_pc(byte* pc) { pc_ = pc; }
ager@chromium.org236ad962008-09-25 09:45:57 +0000271 Mode rmode() const { return rmode_; }
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000272 intptr_t data() const { return data_; }
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000273 Code* host() const { return host_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000274
275 // Apply a relocation by delta bytes
ager@chromium.org3e875802009-06-29 08:26:34 +0000276 INLINE(void apply(intptr_t delta));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000277
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000278 // Is the pointer this relocation info refers to coded like a plain pointer
ulan@chromium.org2efb9002012-01-19 15:36:35 +0000279 // or is it strange in some way (e.g. relative or patched into a series of
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000280 // instructions).
281 bool IsCodedSpecially();
282
ager@chromium.org32912102009-01-16 10:38:43 +0000283 // Read/modify the code target in the branch/call instruction
284 // this relocation applies to;
285 // can only be called if IsCodeTarget(rmode_) || rmode_ == RUNTIME_ENTRY
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000286 INLINE(Address target_address());
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000287 INLINE(void set_target_address(Address target,
288 WriteBarrierMode mode = UPDATE_WRITE_BARRIER));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000289 INLINE(Object* target_object());
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000290 INLINE(Handle<Object> target_object_handle(Assembler* origin));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000291 INLINE(Object** target_object_address());
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000292 INLINE(void set_target_object(Object* target,
293 WriteBarrierMode mode = UPDATE_WRITE_BARRIER));
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000294 INLINE(JSGlobalPropertyCell* target_cell());
295 INLINE(Handle<JSGlobalPropertyCell> target_cell_handle());
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000296 INLINE(void set_target_cell(JSGlobalPropertyCell* cell,
297 WriteBarrierMode mode = UPDATE_WRITE_BARRIER));
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000298
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000299
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000300 // Read the address of the word containing the target_address in an
301 // instruction stream. What this means exactly is architecture-independent.
302 // The only architecture-independent user of this function is the serializer.
303 // The serializer uses it to find out how many raw bytes of instruction to
304 // output before the next target. Architecture-independent code shouldn't
305 // dereference the pointer it gets back from this.
ager@chromium.org32912102009-01-16 10:38:43 +0000306 INLINE(Address target_address_address());
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000307 // This indicates how much space a target takes up when deserializing a code
308 // stream. For most architectures this is just the size of a pointer. For
309 // an instruction like movw/movt where the target bits are mixed into the
310 // instruction bits the size of the target will be zero, indicating that the
311 // serializer should not step forwards in memory after a target is resolved
312 // and written. In this case the target_address_address function above
313 // should return the end of the instructions to be patched, allowing the
314 // deserializer to deserialize the instructions as raw bytes and put them in
315 // place, ready to be patched with the target.
316 INLINE(int target_address_size());
ager@chromium.org32912102009-01-16 10:38:43 +0000317
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000318 // Read/modify the reference in the instruction this relocation
319 // applies to; can only be called if rmode_ is external_reference
320 INLINE(Address* target_reference_address());
321
322 // Read/modify the address of a call instruction. This is used to relocate
323 // the break points where straight-line code is patched with a call
324 // instruction.
325 INLINE(Address call_address());
326 INLINE(void set_call_address(Address target));
327 INLINE(Object* call_object());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000328 INLINE(void set_call_object(Object* target));
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +0000329 INLINE(Object** call_object_address());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000330
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000331 template<typename StaticVisitor> inline void Visit(Heap* heap);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000332 inline void Visit(ObjectVisitor* v);
333
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000334 // Patch the code with some other code.
iposva@chromium.org245aa852009-02-10 00:49:54 +0000335 void PatchCode(byte* instructions, int instruction_count);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000336
337 // Patch the code with a call.
iposva@chromium.org245aa852009-02-10 00:49:54 +0000338 void PatchCodeWithCall(Address target, int guard_bytes);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000339
340 // Check whether this return sequence has been patched
341 // with a call to the debugger.
342 INLINE(bool IsPatchedReturnSequence());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000343
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000344 // Check whether this debug break slot has been patched with a call to the
345 // debugger.
346 INLINE(bool IsPatchedDebugBreakSlotSequence());
347
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000348#ifdef ENABLE_DISASSEMBLER
349 // Printing
ager@chromium.org236ad962008-09-25 09:45:57 +0000350 static const char* RelocModeName(Mode rmode);
whesse@chromium.org023421e2010-12-21 12:19:12 +0000351 void Print(FILE* out);
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000352#endif // ENABLE_DISASSEMBLER
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000353#ifdef DEBUG
354 // Debugging
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000355 void Verify();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000356#endif
357
ager@chromium.org236ad962008-09-25 09:45:57 +0000358 static const int kCodeTargetMask = (1 << (LAST_CODE_ENUM + 1)) - 1;
359 static const int kPositionMask = 1 << POSITION | 1 << STATEMENT_POSITION;
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000360 static const int kDataMask =
361 (1 << CODE_TARGET_WITH_ID) | kPositionMask | (1 << COMMENT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000362 static const int kApplyMask; // Modes affected by apply. Depends on arch.
363
364 private:
365 // On ARM, note that pc_ is the address of the constant pool entry
366 // to be relocated and not the address of the instruction
367 // referencing the constant pool entry (except when rmode_ ==
368 // comment).
369 byte* pc_;
ager@chromium.org236ad962008-09-25 09:45:57 +0000370 Mode rmode_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000371 intptr_t data_;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000372 Code* host_;
danno@chromium.org40cb8782011-05-25 07:58:50 +0000373#ifdef V8_TARGET_ARCH_MIPS
374 // Code and Embedded Object pointers in mips are stored split
375 // across two consecutive 32-bit instructions. Heap management
376 // routines expect to access these pointers indirectly. The following
377 // location provides a place for these pointers to exist natually
378 // when accessed via the Iterator.
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000379 Object* reconstructed_obj_ptr_;
danno@chromium.org40cb8782011-05-25 07:58:50 +0000380 // External-reference pointers are also split across instruction-pairs
381 // in mips, but are accessed via indirect pointers. This location
382 // provides a place for that pointer to exist naturally. Its address
383 // is returned by RelocInfo::target_reference_address().
384 Address reconstructed_adr_ptr_;
385#endif // V8_TARGET_ARCH_MIPS
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000386 friend class RelocIterator;
387};
388
389
390// RelocInfoWriter serializes a stream of relocation info. It writes towards
391// lower addresses.
392class RelocInfoWriter BASE_EMBEDDED {
393 public:
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000394 RelocInfoWriter() : pos_(NULL),
395 last_pc_(NULL),
396 last_id_(0),
397 last_position_(0) {}
398 RelocInfoWriter(byte* pos, byte* pc) : pos_(pos),
399 last_pc_(pc),
400 last_id_(0),
401 last_position_(0) {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000402
403 byte* pos() const { return pos_; }
404 byte* last_pc() const { return last_pc_; }
405
406 void Write(const RelocInfo* rinfo);
407
408 // Update the state of the stream after reloc info buffer
409 // and/or code is moved while the stream is active.
410 void Reposition(byte* pos, byte* pc) {
411 pos_ = pos;
412 last_pc_ = pc;
413 }
414
ager@chromium.org3e875802009-06-29 08:26:34 +0000415 // Max size (bytes) of a written RelocInfo. Longest encoding is
416 // ExtraTag, VariableLengthPCJump, ExtraTag, pc_delta, ExtraTag, data_delta.
417 // On ia32 and arm this is 1 + 4 + 1 + 1 + 1 + 4 = 12.
418 // On x64 this is 1 + 4 + 1 + 1 + 1 + 8 == 16;
419 // Here we use the maximum of the two.
420 static const int kMaxSize = 16;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000421
422 private:
423 inline uint32_t WriteVariableLengthPCJump(uint32_t pc_delta);
424 inline void WriteTaggedPC(uint32_t pc_delta, int tag);
425 inline void WriteExtraTaggedPC(uint32_t pc_delta, int extra_tag);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000426 inline void WriteExtraTaggedIntData(int data_delta, int top_tag);
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000427 inline void WriteExtraTaggedConstPoolData(int data);
ager@chromium.orge2902be2009-06-08 12:21:35 +0000428 inline void WriteExtraTaggedData(intptr_t data_delta, int top_tag);
429 inline void WriteTaggedData(intptr_t data_delta, int tag);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000430 inline void WriteExtraTag(int extra_tag, int top_tag);
431
432 byte* pos_;
433 byte* last_pc_;
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000434 int last_id_;
435 int last_position_;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000436 DISALLOW_COPY_AND_ASSIGN(RelocInfoWriter);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000437};
438
439
440// A RelocIterator iterates over relocation information.
441// Typical use:
442//
443// for (RelocIterator it(code); !it.done(); it.next()) {
444// // do something with it.rinfo() here
445// }
446//
447// A mask can be specified to skip unwanted modes.
448class RelocIterator: public Malloced {
449 public:
450 // Create a new iterator positioned at
451 // the beginning of the reloc info.
452 // Relocation information with mode k is included in the
453 // iteration iff bit k of mode_mask is set.
454 explicit RelocIterator(Code* code, int mode_mask = -1);
455 explicit RelocIterator(const CodeDesc& desc, int mode_mask = -1);
456
457 // Iteration
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000458 bool done() const { return done_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000459 void next();
460
461 // Return pointer valid until next next().
462 RelocInfo* rinfo() {
463 ASSERT(!done());
464 return &rinfo_;
465 }
466
467 private:
468 // Advance* moves the position before/after reading.
469 // *Read* reads from current byte(s) into rinfo_.
470 // *Get* just reads and returns info on current byte.
471 void Advance(int bytes = 1) { pos_ -= bytes; }
472 int AdvanceGetTag();
473 int GetExtraTag();
474 int GetTopTag();
475 void ReadTaggedPC();
476 void AdvanceReadPC();
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000477 void AdvanceReadId();
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000478 void AdvanceReadConstPoolData();
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000479 void AdvanceReadPosition();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000480 void AdvanceReadData();
481 void AdvanceReadVariableLengthPCJump();
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000482 int GetLocatableTypeTag();
483 void ReadTaggedId();
484 void ReadTaggedPosition();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000485
486 // If the given mode is wanted, set it in rinfo_ and return true.
487 // Else return false. Used for efficiently skipping unwanted modes.
ager@chromium.org236ad962008-09-25 09:45:57 +0000488 bool SetMode(RelocInfo::Mode mode) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000489 return (mode_mask_ & (1 << mode)) ? (rinfo_.rmode_ = mode, true) : false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000490 }
491
492 byte* pos_;
493 byte* end_;
494 RelocInfo rinfo_;
495 bool done_;
496 int mode_mask_;
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000497 int last_id_;
498 int last_position_;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000499 DISALLOW_COPY_AND_ASSIGN(RelocIterator);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000500};
501
502
503//------------------------------------------------------------------------------
504// External function
505
506//----------------------------------------------------------------------------
507class IC_Utility;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000508class SCTableReference;
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000509#ifdef ENABLE_DEBUGGER_SUPPORT
510class Debug_Address;
511#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000512
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000513
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000514// An ExternalReference represents a C++ address used in the generated
515// code. All references to C++ functions and variables must be encapsulated in
516// an ExternalReference instance. This is done in order to track the origin of
517// all external references in the code so that they can be bound to the correct
518// addresses when deserializing a heap.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000519class ExternalReference BASE_EMBEDDED {
520 public:
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000521 // Used in the simulator to support different native api calls.
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000522 enum Type {
vegorov@chromium.org5d6c1f52011-02-28 13:13:38 +0000523 // Builtin call.
524 // MaybeObject* f(v8::internal::Arguments).
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000525 BUILTIN_CALL, // default
vegorov@chromium.org5d6c1f52011-02-28 13:13:38 +0000526
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000527 // Builtin that takes float arguments and returns an int.
528 // int f(double, double).
529 BUILTIN_COMPARE_CALL,
530
vegorov@chromium.org5d6c1f52011-02-28 13:13:38 +0000531 // Builtin call that returns floating point.
532 // double f(double, double).
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000533 BUILTIN_FP_FP_CALL,
534
535 // Builtin call that returns floating point.
536 // double f(double).
537 BUILTIN_FP_CALL,
538
539 // Builtin call that returns floating point.
540 // double f(double, int).
541 BUILTIN_FP_INT_CALL,
vegorov@chromium.org5d6c1f52011-02-28 13:13:38 +0000542
543 // Direct call to API function callback.
544 // Handle<Value> f(v8::Arguments&)
545 DIRECT_API_CALL,
546
547 // Direct call to accessor getter callback.
548 // Handle<value> f(Local<String> property, AccessorInfo& info)
549 DIRECT_GETTER_CALL
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000550 };
551
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000552 static void SetUp();
553
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000554 typedef void* ExternalReferenceRedirector(void* original, Type type);
555
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000556 ExternalReference(Builtins::CFunctionId id, Isolate* isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000557
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000558 ExternalReference(ApiFunction* ptr, Type type, Isolate* isolate);
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000559
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000560 ExternalReference(Builtins::Name name, Isolate* isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000561
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000562 ExternalReference(Runtime::FunctionId id, Isolate* isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000563
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000564 ExternalReference(const Runtime::Function* f, Isolate* isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000565
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000566 ExternalReference(const IC_Utility& ic_utility, Isolate* isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000567
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000568#ifdef ENABLE_DEBUGGER_SUPPORT
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000569 ExternalReference(const Debug_Address& debug_address, Isolate* isolate);
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000570#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000571
572 explicit ExternalReference(StatsCounter* counter);
573
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000574 ExternalReference(Isolate::AddressId id, Isolate* isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000575
576 explicit ExternalReference(const SCTableReference& table_ref);
577
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000578 // Isolate::Current() as an external reference.
579 static ExternalReference isolate_address();
580
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000581 // One-of-a-kind references. These references are not part of a general
582 // pattern. This means that they have to be added to the
583 // ExternalReferenceTable in serialize.cc manually.
584
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000585 static ExternalReference incremental_marking_record_write_function(
586 Isolate* isolate);
587 static ExternalReference incremental_evacuation_record_write_function(
588 Isolate* isolate);
589 static ExternalReference store_buffer_overflow_function(
590 Isolate* isolate);
591 static ExternalReference flush_icache_function(Isolate* isolate);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000592 static ExternalReference perform_gc_function(Isolate* isolate);
593 static ExternalReference fill_heap_number_with_random_function(
594 Isolate* isolate);
595 static ExternalReference random_uint32_function(Isolate* isolate);
596 static ExternalReference transcendental_cache_array_address(Isolate* isolate);
597 static ExternalReference delete_handle_scope_extensions(Isolate* isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000598
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +0000599 static ExternalReference get_date_field_function(Isolate* isolate);
600 static ExternalReference date_cache_stamp(Isolate* isolate);
601
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000602 // Deoptimization support.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000603 static ExternalReference new_deoptimizer_function(Isolate* isolate);
604 static ExternalReference compute_output_frames_function(Isolate* isolate);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000605
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000606 // Static data in the keyed lookup cache.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000607 static ExternalReference keyed_lookup_cache_keys(Isolate* isolate);
608 static ExternalReference keyed_lookup_cache_field_offsets(Isolate* isolate);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000609
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000610 // Static variable Heap::roots_array_start()
611 static ExternalReference roots_array_start(Isolate* isolate);
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000612
ager@chromium.org6f10e412009-02-13 10:11:16 +0000613 // Static variable StackGuard::address_of_jslimit()
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000614 static ExternalReference address_of_stack_limit(Isolate* isolate);
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000615
616 // Static variable StackGuard::address_of_real_jslimit()
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000617 static ExternalReference address_of_real_stack_limit(Isolate* isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000618
ager@chromium.org32912102009-01-16 10:38:43 +0000619 // Static variable RegExpStack::limit_address()
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000620 static ExternalReference address_of_regexp_stack_limit(Isolate* isolate);
ager@chromium.org32912102009-01-16 10:38:43 +0000621
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000622 // Static variables for RegExp.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000623 static ExternalReference address_of_static_offsets_vector(Isolate* isolate);
624 static ExternalReference address_of_regexp_stack_memory_address(
625 Isolate* isolate);
626 static ExternalReference address_of_regexp_stack_memory_size(
627 Isolate* isolate);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000628
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000629 // Static variable Heap::NewSpaceStart()
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000630 static ExternalReference new_space_start(Isolate* isolate);
631 static ExternalReference new_space_mask(Isolate* isolate);
632 static ExternalReference heap_always_allocate_scope_depth(Isolate* isolate);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000633 static ExternalReference new_space_mark_bits(Isolate* isolate);
634
635 // Write barrier.
636 static ExternalReference store_buffer_top(Isolate* isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000637
638 // Used for fast allocation in generated code.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000639 static ExternalReference new_space_allocation_top_address(Isolate* isolate);
640 static ExternalReference new_space_allocation_limit_address(Isolate* isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000641
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000642 static ExternalReference double_fp_operation(Token::Value operation,
643 Isolate* isolate);
644 static ExternalReference compare_doubles(Isolate* isolate);
645 static ExternalReference power_double_double_function(Isolate* isolate);
646 static ExternalReference power_double_int_function(Isolate* isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000647
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000648 static ExternalReference handle_scope_next_address();
649 static ExternalReference handle_scope_limit_address();
lrn@chromium.org303ada72010-10-27 09:33:13 +0000650 static ExternalReference handle_scope_level_address();
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000651
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000652 static ExternalReference scheduled_exception_address(Isolate* isolate);
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000653 static ExternalReference address_of_pending_message_obj(Isolate* isolate);
654 static ExternalReference address_of_has_pending_message(Isolate* isolate);
655 static ExternalReference address_of_pending_message_script(Isolate* isolate);
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000656
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000657 // Static variables containing common double constants.
658 static ExternalReference address_of_min_int();
659 static ExternalReference address_of_one_half();
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000660 static ExternalReference address_of_minus_zero();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000661 static ExternalReference address_of_zero();
662 static ExternalReference address_of_uint8_max_value();
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000663 static ExternalReference address_of_negative_infinity();
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +0000664 static ExternalReference address_of_canonical_non_hole_nan();
665 static ExternalReference address_of_the_hole_nan();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000666
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000667 static ExternalReference math_sin_double_function(Isolate* isolate);
668 static ExternalReference math_cos_double_function(Isolate* isolate);
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000669 static ExternalReference math_tan_double_function(Isolate* isolate);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000670 static ExternalReference math_log_double_function(Isolate* isolate);
karlklose@chromium.org8f806e82011-03-07 14:06:08 +0000671
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000672 static ExternalReference page_flags(Page* page);
673
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000674 Address address() const {return reinterpret_cast<Address>(address_);}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000675
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000676#ifdef ENABLE_DEBUGGER_SUPPORT
677 // Function Debug::Break()
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000678 static ExternalReference debug_break(Isolate* isolate);
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000679
680 // Used to check if single stepping is enabled in generated code.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000681 static ExternalReference debug_step_in_fp_address(Isolate* isolate);
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000682#endif
683
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000684#ifndef V8_INTERPRETED_REGEXP
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000685 // C functions called from RegExp generated code.
686
687 // Function NativeRegExpMacroAssembler::CaseInsensitiveCompareUC16()
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000688 static ExternalReference re_case_insensitive_compare_uc16(Isolate* isolate);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000689
690 // Function RegExpMacroAssembler*::CheckStackGuardState()
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000691 static ExternalReference re_check_stack_guard_state(Isolate* isolate);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000692
693 // Function NativeRegExpMacroAssembler::GrowStack()
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000694 static ExternalReference re_grow_stack(Isolate* isolate);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000695
696 // byte NativeRegExpMacroAssembler::word_character_bitmap
697 static ExternalReference re_word_character_map();
698
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000699#endif
700
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000701 // This lets you register a function that rewrites all external references.
702 // Used by the ARM simulator to catch calls to external references.
lrn@chromium.org1c092762011-05-09 09:42:16 +0000703 static void set_redirector(Isolate* isolate,
704 ExternalReferenceRedirector* redirector) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000705 // We can't stack them.
lrn@chromium.org1c092762011-05-09 09:42:16 +0000706 ASSERT(isolate->external_reference_redirector() == NULL);
707 isolate->set_external_reference_redirector(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000708 reinterpret_cast<ExternalReferenceRedirectorPointer*>(redirector));
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000709 }
710
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000711 private:
712 explicit ExternalReference(void* address)
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000713 : address_(address) {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000714
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000715 static void* Redirect(Isolate* isolate,
716 void* address,
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000717 Type type = ExternalReference::BUILTIN_CALL) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000718 ExternalReferenceRedirector* redirector =
719 reinterpret_cast<ExternalReferenceRedirector*>(
720 isolate->external_reference_redirector());
721 if (redirector == NULL) return address;
722 void* answer = (*redirector)(address, type);
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000723 return answer;
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000724 }
725
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000726 static void* Redirect(Isolate* isolate,
727 Address address_arg,
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000728 Type type = ExternalReference::BUILTIN_CALL) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000729 ExternalReferenceRedirector* redirector =
730 reinterpret_cast<ExternalReferenceRedirector*>(
731 isolate->external_reference_redirector());
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000732 void* address = reinterpret_cast<void*>(address_arg);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000733 void* answer = (redirector == NULL) ?
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000734 address :
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000735 (*redirector)(address, type);
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000736 return answer;
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000737 }
738
739 void* address_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000740};
741
742
743// -----------------------------------------------------------------------------
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000744// Position recording support
745
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000746struct PositionState {
747 PositionState() : current_position(RelocInfo::kNoPosition),
748 written_position(RelocInfo::kNoPosition),
749 current_statement_position(RelocInfo::kNoPosition),
750 written_statement_position(RelocInfo::kNoPosition) {}
751
752 int current_position;
753 int written_position;
754
755 int current_statement_position;
756 int written_statement_position;
757};
758
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000759
760class PositionsRecorder BASE_EMBEDDED {
761 public:
762 explicit PositionsRecorder(Assembler* assembler)
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000763 : assembler_(assembler) {
764#ifdef ENABLE_GDB_JIT_INTERFACE
765 gdbjit_lineinfo_ = NULL;
766#endif
767 }
768
769#ifdef ENABLE_GDB_JIT_INTERFACE
770 ~PositionsRecorder() {
771 delete gdbjit_lineinfo_;
772 }
773
774 void StartGDBJITLineInfoRecording() {
775 if (FLAG_gdbjit) {
776 gdbjit_lineinfo_ = new GDBJITLineInfo();
777 }
778 }
779
780 GDBJITLineInfo* DetachGDBJITLineInfo() {
781 GDBJITLineInfo* lineinfo = gdbjit_lineinfo_;
782 gdbjit_lineinfo_ = NULL; // To prevent deallocation in destructor.
783 return lineinfo;
784 }
785#endif
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000786
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000787 // Set current position to pos.
788 void RecordPosition(int pos);
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000789
790 // Set current statement position to pos.
791 void RecordStatementPosition(int pos);
792
793 // Write recorded positions to relocation information.
794 bool WriteRecordedPositions();
795
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000796 int current_position() const { return state_.current_position; }
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000797
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000798 int current_statement_position() const {
799 return state_.current_statement_position;
800 }
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000801
802 private:
803 Assembler* assembler_;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000804 PositionState state_;
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000805#ifdef ENABLE_GDB_JIT_INTERFACE
806 GDBJITLineInfo* gdbjit_lineinfo_;
807#endif
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000808
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000809 friend class PreservePositionScope;
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000810
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000811 DISALLOW_COPY_AND_ASSIGN(PositionsRecorder);
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000812};
813
814
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000815class PreservePositionScope BASE_EMBEDDED {
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000816 public:
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000817 explicit PreservePositionScope(PositionsRecorder* positions_recorder)
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000818 : positions_recorder_(positions_recorder),
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000819 saved_state_(positions_recorder->state_) {}
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000820
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000821 ~PreservePositionScope() {
822 positions_recorder_->state_ = saved_state_;
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000823 }
824
825 private:
826 PositionsRecorder* positions_recorder_;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000827 const PositionState saved_state_;
828
829 DISALLOW_COPY_AND_ASSIGN(PreservePositionScope);
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000830};
831
832
833// -----------------------------------------------------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000834// Utility functions
835
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000836inline bool is_intn(int x, int n) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000837 return -(1 << (n-1)) <= x && x < (1 << (n-1));
838}
839
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000840inline bool is_int8(int x) { return is_intn(x, 8); }
841inline bool is_int16(int x) { return is_intn(x, 16); }
842inline bool is_int18(int x) { return is_intn(x, 18); }
843inline bool is_int24(int x) { return is_intn(x, 24); }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000844
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000845inline bool is_uintn(int x, int n) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000846 return (x & -(1 << n)) == 0;
847}
848
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000849inline bool is_uint2(int x) { return is_uintn(x, 2); }
850inline bool is_uint3(int x) { return is_uintn(x, 3); }
851inline bool is_uint4(int x) { return is_uintn(x, 4); }
852inline bool is_uint5(int x) { return is_uintn(x, 5); }
853inline bool is_uint6(int x) { return is_uintn(x, 6); }
854inline bool is_uint8(int x) { return is_uintn(x, 8); }
855inline bool is_uint10(int x) { return is_uintn(x, 10); }
856inline bool is_uint12(int x) { return is_uintn(x, 12); }
857inline bool is_uint16(int x) { return is_uintn(x, 16); }
858inline bool is_uint24(int x) { return is_uintn(x, 24); }
859inline bool is_uint26(int x) { return is_uintn(x, 26); }
860inline bool is_uint28(int x) { return is_uintn(x, 28); }
ager@chromium.org5c838252010-02-19 08:53:10 +0000861
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000862inline int NumberOfBitsSet(uint32_t x) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000863 unsigned int num_bits_set;
864 for (num_bits_set = 0; x; x >>= 1) {
865 num_bits_set += x & 1;
866 }
867 return num_bits_set;
868}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000869
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000870bool EvalComparison(Token::Value op, double op1, double op2);
871
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000872// Computes pow(x, y) with the special cases in the spec for Math.pow.
873double power_double_int(double x, int y);
874double power_double_double(double x, double y);
875
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000876// Helper class for generating code or data associated with the code
877// right after a call instruction. As an example this can be used to
878// generate safepoint data after calls for crankshaft.
879class CallWrapper {
880 public:
881 CallWrapper() { }
882 virtual ~CallWrapper() { }
883 // Called just before emitting a call. Argument is the size of the generated
884 // call code.
885 virtual void BeforeCall(int call_size) const = 0;
886 // Called just after emitting a call, i.e., at the return site for the call.
887 virtual void AfterCall() const = 0;
888};
889
890class NullCallWrapper : public CallWrapper {
891 public:
892 NullCallWrapper() { }
893 virtual ~NullCallWrapper() { }
894 virtual void BeforeCall(int call_size) const { }
895 virtual void AfterCall() const { }
896};
897
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000898} } // namespace v8::internal
899
900#endif // V8_ASSEMBLER_H_