blob: 29f1ea9ff1a8999250561303885f78415edb3fa8 [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 Murdoch8b112d22011-06-08 16:22:53 +010033// Copyright 2011 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 Murdoch257744e2011-11-30 15:57:28 +000038#include "allocation.h"
Ben Murdochb8e0da22011-05-16 14:20:40 +010039#include "gdb-jit.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000040#include "runtime.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000041#include "token.h"
42
43namespace v8 {
44namespace internal {
45
Ben Murdoch257744e2011-11-30 15:57:28 +000046const unsigned kNoASTId = -1;
Steve Blocka7e24c12009-10-30 11:49:00 +000047// -----------------------------------------------------------------------------
Steve Block44f0eee2011-05-26 01:26:41 +010048// Platform independent assembler base class.
49
50class AssemblerBase: public Malloced {
51 public:
Steve Block053d10c2011-06-13 19:13:29 +010052 explicit AssemblerBase(Isolate* isolate);
Steve Block44f0eee2011-05-26 01:26:41 +010053
54 Isolate* isolate() const { return isolate_; }
Steve Block053d10c2011-06-13 19:13:29 +010055 int jit_cookie() { return jit_cookie_; }
Steve Block44f0eee2011-05-26 01:26:41 +010056
57 private:
58 Isolate* isolate_;
Steve Block053d10c2011-06-13 19:13:29 +010059 int jit_cookie_;
Steve Block44f0eee2011-05-26 01:26:41 +010060};
61
62// -----------------------------------------------------------------------------
Ben Murdochb0fe1622011-05-05 13:52:32 +010063// Common double constants.
64
65class DoubleConstant: public AllStatic {
66 public:
67 static const double min_int;
68 static const double one_half;
Ben Murdochb8e0da22011-05-16 14:20:40 +010069 static const double minus_zero;
Ben Murdoch257744e2011-11-30 15:57:28 +000070 static const double zero;
71 static const double uint8_max_value;
Ben Murdochb0fe1622011-05-05 13:52:32 +010072 static const double negative_infinity;
Steve Block44f0eee2011-05-26 01:26:41 +010073 static const double nan;
Ben Murdochb0fe1622011-05-05 13:52:32 +010074};
75
76
77// -----------------------------------------------------------------------------
Steve Blocka7e24c12009-10-30 11:49:00 +000078// Labels represent pc locations; they are typically jump or call targets.
79// After declaration, a label can be freely used to denote known or (yet)
80// unknown pc location. Assembler::bind() is used to bind a label to the
81// current pc. A label can be bound only once.
82
83class Label BASE_EMBEDDED {
84 public:
Ben Murdoch257744e2011-11-30 15:57:28 +000085 enum Distance {
86 kNear, kFar
87 };
88
89 INLINE(Label()) {
90 Unuse();
91 UnuseNear();
92 }
Steve Blocka7e24c12009-10-30 11:49:00 +000093 INLINE(~Label()) { ASSERT(!is_linked()); }
94
95 INLINE(void Unuse()) { pos_ = 0; }
Ben Murdoch257744e2011-11-30 15:57:28 +000096 INLINE(void UnuseNear()) { near_link_pos_ = 0; }
Steve Blocka7e24c12009-10-30 11:49:00 +000097
Kristian Monsen0d5e1162010-09-30 15:31:59 +010098 INLINE(bool is_bound() const) { return pos_ < 0; }
Ben Murdoch257744e2011-11-30 15:57:28 +000099 INLINE(bool is_unused() const) { return pos_ == 0 && near_link_pos_ == 0; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000100 INLINE(bool is_linked() const) { return pos_ > 0; }
Ben Murdoch257744e2011-11-30 15:57:28 +0000101 INLINE(bool is_near_linked() const) { return near_link_pos_ > 0; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000102
103 // Returns the position of bound or linked labels. Cannot be used
104 // for unused labels.
105 int pos() const;
Ben Murdoch257744e2011-11-30 15:57:28 +0000106 int near_link_pos() const { return near_link_pos_ - 1; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000107
108 private:
109 // pos_ encodes both the binding state (via its sign)
110 // and the binding position (via its value) of a label.
111 //
112 // pos_ < 0 bound label, pos() returns the jump target position
113 // pos_ == 0 unused label
114 // pos_ > 0 linked label, pos() returns the last reference position
115 int pos_;
116
Ben Murdoch257744e2011-11-30 15:57:28 +0000117 // Behaves like |pos_| in the "> 0" case, but for near jumps to this label.
118 int near_link_pos_;
119
Steve Blocka7e24c12009-10-30 11:49:00 +0000120 void bind_to(int pos) {
121 pos_ = -pos - 1;
122 ASSERT(is_bound());
123 }
Ben Murdoch257744e2011-11-30 15:57:28 +0000124 void link_to(int pos, Distance distance = kFar) {
125 if (distance == kNear) {
126 near_link_pos_ = pos + 1;
127 ASSERT(is_near_linked());
128 } else {
129 pos_ = pos + 1;
130 ASSERT(is_linked());
131 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000132 }
133
134 friend class Assembler;
135 friend class RegexpAssembler;
136 friend class Displacement;
Steve Blocka7e24c12009-10-30 11:49:00 +0000137 friend class RegExpMacroAssemblerIrregexp;
138};
139
140
141// -----------------------------------------------------------------------------
142// Relocation information
143
144
145// Relocation information consists of the address (pc) of the datum
146// to which the relocation information applies, the relocation mode
147// (rmode), and an optional data field. The relocation mode may be
148// "descriptive" and not indicate a need for relocation, but simply
149// describe a property of the datum. Such rmodes are useful for GC
150// and nice disassembly output.
151
152class RelocInfo BASE_EMBEDDED {
153 public:
154 // The constant kNoPosition is used with the collecting of source positions
155 // in the relocation information. Two types of source positions are collected
156 // "position" (RelocMode position) and "statement position" (RelocMode
157 // statement_position). The "position" is collected at places in the source
158 // code which are of interest when making stack traces to pin-point the source
159 // location of a stack frame as close as possible. The "statement position" is
160 // collected at the beginning at each statement, and is used to indicate
161 // possible break locations. kNoPosition is used to indicate an
162 // invalid/uninitialized position value.
163 static const int kNoPosition = -1;
164
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100165 // This string is used to add padding comments to the reloc info in cases
166 // where we are not sure to have enough space for patching in during
167 // lazy deoptimization. This is the case if we have indirect calls for which
168 // we do not normally record relocation info.
169 static const char* kFillerCommentString;
170
171 // The minimum size of a comment is equal to three bytes for the extra tagged
172 // pc + the tag for the data, and kPointerSize for the actual pointer to the
173 // comment.
174 static const int kMinRelocCommentSize = 3 + kPointerSize;
175
176 // The maximum size for a call instruction including pc-jump.
177 static const int kMaxCallSize = 6;
178
Steve Block44f0eee2011-05-26 01:26:41 +0100179 // The maximum pc delta that will use the short encoding.
180 static const int kMaxSmallPCDelta;
181
Steve Blocka7e24c12009-10-30 11:49:00 +0000182 enum Mode {
183 // Please note the order is important (see IsCodeTarget, IsGCRelocMode).
Ben Murdoch257744e2011-11-30 15:57:28 +0000184 CODE_TARGET, // Code target which is not any of the above.
185 CODE_TARGET_WITH_ID,
Steve Blocka7e24c12009-10-30 11:49:00 +0000186 CONSTRUCT_CALL, // code target that is a call to a JavaScript constructor.
Steve Block1e0659c2011-05-24 12:43:12 +0100187 CODE_TARGET_CONTEXT, // Code target used for contextual loads and stores.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100188 DEBUG_BREAK, // Code target for the debugger statement.
Steve Blocka7e24c12009-10-30 11:49:00 +0000189 EMBEDDED_OBJECT,
Ben Murdochb0fe1622011-05-05 13:52:32 +0100190 GLOBAL_PROPERTY_CELL,
191
Steve Blocka7e24c12009-10-30 11:49:00 +0000192 // Everything after runtime_entry (inclusive) is not GC'ed.
193 RUNTIME_ENTRY,
194 JS_RETURN, // Marks start of the ExitJSFrame code.
195 COMMENT,
196 POSITION, // See comment for kNoPosition above.
197 STATEMENT_POSITION, // See comment for kNoPosition above.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100198 DEBUG_BREAK_SLOT, // Additional code inserted for debug break slot.
Steve Blocka7e24c12009-10-30 11:49:00 +0000199 EXTERNAL_REFERENCE, // The address of an external C++ function.
200 INTERNAL_REFERENCE, // An address inside the same function.
201
202 // add more as needed
203 // Pseudo-types
Ben Murdoch257744e2011-11-30 15:57:28 +0000204 NUMBER_OF_MODES, // There are at most 14 modes with noncompact encoding.
Steve Blocka7e24c12009-10-30 11:49:00 +0000205 NONE, // never recorded
Ben Murdoch257744e2011-11-30 15:57:28 +0000206 LAST_CODE_ENUM = DEBUG_BREAK,
207 LAST_GCED_ENUM = GLOBAL_PROPERTY_CELL,
208 // Modes <= LAST_COMPACT_ENUM are guaranteed to have compact encoding.
209 LAST_COMPACT_ENUM = CODE_TARGET_WITH_ID
Steve Blocka7e24c12009-10-30 11:49:00 +0000210 };
211
212
213 RelocInfo() {}
214 RelocInfo(byte* pc, Mode rmode, intptr_t data)
215 : pc_(pc), rmode_(rmode), data_(data) {
216 }
217
218 static inline bool IsConstructCall(Mode mode) {
219 return mode == CONSTRUCT_CALL;
220 }
221 static inline bool IsCodeTarget(Mode mode) {
222 return mode <= LAST_CODE_ENUM;
223 }
224 // Is the relocation mode affected by GC?
225 static inline bool IsGCRelocMode(Mode mode) {
226 return mode <= LAST_GCED_ENUM;
227 }
228 static inline bool IsJSReturn(Mode mode) {
229 return mode == JS_RETURN;
230 }
231 static inline bool IsComment(Mode mode) {
232 return mode == COMMENT;
233 }
234 static inline bool IsPosition(Mode mode) {
235 return mode == POSITION || mode == STATEMENT_POSITION;
236 }
237 static inline bool IsStatementPosition(Mode mode) {
238 return mode == STATEMENT_POSITION;
239 }
240 static inline bool IsExternalReference(Mode mode) {
241 return mode == EXTERNAL_REFERENCE;
242 }
243 static inline bool IsInternalReference(Mode mode) {
244 return mode == INTERNAL_REFERENCE;
245 }
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100246 static inline bool IsDebugBreakSlot(Mode mode) {
247 return mode == DEBUG_BREAK_SLOT;
248 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000249 static inline int ModeMask(Mode mode) { return 1 << mode; }
250
251 // Accessors
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100252 byte* pc() const { return pc_; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000253 void set_pc(byte* pc) { pc_ = pc; }
254 Mode rmode() const { return rmode_; }
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100255 intptr_t data() const { return data_; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000256
257 // Apply a relocation by delta bytes
258 INLINE(void apply(intptr_t delta));
259
Leon Clarkef7060e22010-06-03 12:02:55 +0100260 // Is the pointer this relocation info refers to coded like a plain pointer
261 // or is it strange in some way (eg relative or patched into a series of
262 // instructions).
263 bool IsCodedSpecially();
264
Steve Blocka7e24c12009-10-30 11:49:00 +0000265 // Read/modify the code target in the branch/call instruction
266 // this relocation applies to;
267 // can only be called if IsCodeTarget(rmode_) || rmode_ == RUNTIME_ENTRY
268 INLINE(Address target_address());
269 INLINE(void set_target_address(Address target));
270 INLINE(Object* target_object());
Steve Block3ce2e202009-11-05 08:53:23 +0000271 INLINE(Handle<Object> target_object_handle(Assembler* origin));
Steve Blocka7e24c12009-10-30 11:49:00 +0000272 INLINE(Object** target_object_address());
273 INLINE(void set_target_object(Object* target));
Ben Murdochb0fe1622011-05-05 13:52:32 +0100274 INLINE(JSGlobalPropertyCell* target_cell());
275 INLINE(Handle<JSGlobalPropertyCell> target_cell_handle());
276 INLINE(void set_target_cell(JSGlobalPropertyCell* cell));
277
Steve Blocka7e24c12009-10-30 11:49:00 +0000278
Leon Clarkef7060e22010-06-03 12:02:55 +0100279 // Read the address of the word containing the target_address in an
280 // instruction stream. What this means exactly is architecture-independent.
281 // The only architecture-independent user of this function is the serializer.
282 // The serializer uses it to find out how many raw bytes of instruction to
283 // output before the next target. Architecture-independent code shouldn't
284 // dereference the pointer it gets back from this.
Steve Blocka7e24c12009-10-30 11:49:00 +0000285 INLINE(Address target_address_address());
Leon Clarkef7060e22010-06-03 12:02:55 +0100286 // This indicates how much space a target takes up when deserializing a code
287 // stream. For most architectures this is just the size of a pointer. For
288 // an instruction like movw/movt where the target bits are mixed into the
289 // instruction bits the size of the target will be zero, indicating that the
290 // serializer should not step forwards in memory after a target is resolved
291 // and written. In this case the target_address_address function above
292 // should return the end of the instructions to be patched, allowing the
293 // deserializer to deserialize the instructions as raw bytes and put them in
294 // place, ready to be patched with the target.
295 INLINE(int target_address_size());
Steve Blocka7e24c12009-10-30 11:49:00 +0000296
297 // Read/modify the reference in the instruction this relocation
298 // applies to; can only be called if rmode_ is external_reference
299 INLINE(Address* target_reference_address());
300
301 // Read/modify the address of a call instruction. This is used to relocate
302 // the break points where straight-line code is patched with a call
303 // instruction.
304 INLINE(Address call_address());
305 INLINE(void set_call_address(Address target));
306 INLINE(Object* call_object());
Steve Blocka7e24c12009-10-30 11:49:00 +0000307 INLINE(void set_call_object(Object* target));
Ben Murdochbb769b22010-08-11 14:56:33 +0100308 INLINE(Object** call_object_address());
Steve Blocka7e24c12009-10-30 11:49:00 +0000309
Steve Block44f0eee2011-05-26 01:26:41 +0100310 template<typename StaticVisitor> inline void Visit(Heap* heap);
Leon Clarkef7060e22010-06-03 12:02:55 +0100311 inline void Visit(ObjectVisitor* v);
312
Steve Blocka7e24c12009-10-30 11:49:00 +0000313 // Patch the code with some other code.
314 void PatchCode(byte* instructions, int instruction_count);
315
316 // Patch the code with a call.
317 void PatchCodeWithCall(Address target, int guard_bytes);
Steve Block3ce2e202009-11-05 08:53:23 +0000318
319 // Check whether this return sequence has been patched
320 // with a call to the debugger.
321 INLINE(bool IsPatchedReturnSequence());
Steve Blocka7e24c12009-10-30 11:49:00 +0000322
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100323 // Check whether this debug break slot has been patched with a call to the
324 // debugger.
325 INLINE(bool IsPatchedDebugBreakSlotSequence());
326
Steve Blocka7e24c12009-10-30 11:49:00 +0000327#ifdef ENABLE_DISASSEMBLER
328 // Printing
329 static const char* RelocModeName(Mode rmode);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100330 void Print(FILE* out);
Steve Blocka7e24c12009-10-30 11:49:00 +0000331#endif // ENABLE_DISASSEMBLER
332#ifdef DEBUG
333 // Debugging
334 void Verify();
335#endif
336
337 static const int kCodeTargetMask = (1 << (LAST_CODE_ENUM + 1)) - 1;
338 static const int kPositionMask = 1 << POSITION | 1 << STATEMENT_POSITION;
Ben Murdoch257744e2011-11-30 15:57:28 +0000339 static const int kDataMask =
340 (1 << CODE_TARGET_WITH_ID) | kPositionMask | (1 << COMMENT);
Steve Blocka7e24c12009-10-30 11:49:00 +0000341 static const int kApplyMask; // Modes affected by apply. Depends on arch.
342
343 private:
344 // On ARM, note that pc_ is the address of the constant pool entry
345 // to be relocated and not the address of the instruction
346 // referencing the constant pool entry (except when rmode_ ==
347 // comment).
348 byte* pc_;
349 Mode rmode_;
350 intptr_t data_;
Ben Murdoch257744e2011-11-30 15:57:28 +0000351#ifdef V8_TARGET_ARCH_MIPS
352 // Code and Embedded Object pointers in mips are stored split
353 // across two consecutive 32-bit instructions. Heap management
354 // routines expect to access these pointers indirectly. The following
355 // location provides a place for these pointers to exist natually
356 // when accessed via the Iterator.
357 Object *reconstructed_obj_ptr_;
358 // External-reference pointers are also split across instruction-pairs
359 // in mips, but are accessed via indirect pointers. This location
360 // provides a place for that pointer to exist naturally. Its address
361 // is returned by RelocInfo::target_reference_address().
362 Address reconstructed_adr_ptr_;
363#endif // V8_TARGET_ARCH_MIPS
Steve Blocka7e24c12009-10-30 11:49:00 +0000364 friend class RelocIterator;
365};
366
367
368// RelocInfoWriter serializes a stream of relocation info. It writes towards
369// lower addresses.
370class RelocInfoWriter BASE_EMBEDDED {
371 public:
Ben Murdoch257744e2011-11-30 15:57:28 +0000372 RelocInfoWriter() : pos_(NULL),
373 last_pc_(NULL),
374 last_id_(0),
375 last_position_(0) {}
376 RelocInfoWriter(byte* pos, byte* pc) : pos_(pos),
377 last_pc_(pc),
378 last_id_(0),
379 last_position_(0) {}
Steve Blocka7e24c12009-10-30 11:49:00 +0000380
381 byte* pos() const { return pos_; }
382 byte* last_pc() const { return last_pc_; }
383
384 void Write(const RelocInfo* rinfo);
385
386 // Update the state of the stream after reloc info buffer
387 // and/or code is moved while the stream is active.
388 void Reposition(byte* pos, byte* pc) {
389 pos_ = pos;
390 last_pc_ = pc;
391 }
392
393 // Max size (bytes) of a written RelocInfo. Longest encoding is
394 // ExtraTag, VariableLengthPCJump, ExtraTag, pc_delta, ExtraTag, data_delta.
395 // On ia32 and arm this is 1 + 4 + 1 + 1 + 1 + 4 = 12.
396 // On x64 this is 1 + 4 + 1 + 1 + 1 + 8 == 16;
397 // Here we use the maximum of the two.
398 static const int kMaxSize = 16;
399
400 private:
401 inline uint32_t WriteVariableLengthPCJump(uint32_t pc_delta);
402 inline void WriteTaggedPC(uint32_t pc_delta, int tag);
403 inline void WriteExtraTaggedPC(uint32_t pc_delta, int extra_tag);
Ben Murdoch257744e2011-11-30 15:57:28 +0000404 inline void WriteExtraTaggedIntData(int data_delta, int top_tag);
Steve Blocka7e24c12009-10-30 11:49:00 +0000405 inline void WriteExtraTaggedData(intptr_t data_delta, int top_tag);
406 inline void WriteTaggedData(intptr_t data_delta, int tag);
407 inline void WriteExtraTag(int extra_tag, int top_tag);
408
409 byte* pos_;
410 byte* last_pc_;
Ben Murdoch257744e2011-11-30 15:57:28 +0000411 int last_id_;
412 int last_position_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000413 DISALLOW_COPY_AND_ASSIGN(RelocInfoWriter);
414};
415
416
417// A RelocIterator iterates over relocation information.
418// Typical use:
419//
420// for (RelocIterator it(code); !it.done(); it.next()) {
421// // do something with it.rinfo() here
422// }
423//
424// A mask can be specified to skip unwanted modes.
425class RelocIterator: public Malloced {
426 public:
427 // Create a new iterator positioned at
428 // the beginning of the reloc info.
429 // Relocation information with mode k is included in the
430 // iteration iff bit k of mode_mask is set.
431 explicit RelocIterator(Code* code, int mode_mask = -1);
432 explicit RelocIterator(const CodeDesc& desc, int mode_mask = -1);
433
434 // Iteration
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100435 bool done() const { return done_; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000436 void next();
437
438 // Return pointer valid until next next().
439 RelocInfo* rinfo() {
440 ASSERT(!done());
441 return &rinfo_;
442 }
443
444 private:
445 // Advance* moves the position before/after reading.
446 // *Read* reads from current byte(s) into rinfo_.
447 // *Get* just reads and returns info on current byte.
448 void Advance(int bytes = 1) { pos_ -= bytes; }
449 int AdvanceGetTag();
450 int GetExtraTag();
451 int GetTopTag();
452 void ReadTaggedPC();
453 void AdvanceReadPC();
Ben Murdoch257744e2011-11-30 15:57:28 +0000454 void AdvanceReadId();
455 void AdvanceReadPosition();
Steve Blocka7e24c12009-10-30 11:49:00 +0000456 void AdvanceReadData();
457 void AdvanceReadVariableLengthPCJump();
Ben Murdoch257744e2011-11-30 15:57:28 +0000458 int GetLocatableTypeTag();
459 void ReadTaggedId();
460 void ReadTaggedPosition();
Steve Blocka7e24c12009-10-30 11:49:00 +0000461
462 // If the given mode is wanted, set it in rinfo_ and return true.
463 // Else return false. Used for efficiently skipping unwanted modes.
464 bool SetMode(RelocInfo::Mode mode) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100465 return (mode_mask_ & (1 << mode)) ? (rinfo_.rmode_ = mode, true) : false;
Steve Blocka7e24c12009-10-30 11:49:00 +0000466 }
467
468 byte* pos_;
469 byte* end_;
470 RelocInfo rinfo_;
471 bool done_;
472 int mode_mask_;
Ben Murdoch257744e2011-11-30 15:57:28 +0000473 int last_id_;
474 int last_position_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000475 DISALLOW_COPY_AND_ASSIGN(RelocIterator);
476};
477
478
479//------------------------------------------------------------------------------
480// External function
481
482//----------------------------------------------------------------------------
483class IC_Utility;
484class SCTableReference;
485#ifdef ENABLE_DEBUGGER_SUPPORT
486class Debug_Address;
487#endif
488
489
Steve Blocka7e24c12009-10-30 11:49:00 +0000490// An ExternalReference represents a C++ address used in the generated
491// code. All references to C++ functions and variables must be encapsulated in
492// an ExternalReference instance. This is done in order to track the origin of
493// all external references in the code so that they can be bound to the correct
494// addresses when deserializing a heap.
495class ExternalReference BASE_EMBEDDED {
496 public:
Steve Block1e0659c2011-05-24 12:43:12 +0100497 // Used in the simulator to support different native api calls.
Steve Block1e0659c2011-05-24 12:43:12 +0100498 enum Type {
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100499 // Builtin call.
500 // MaybeObject* f(v8::internal::Arguments).
Steve Block1e0659c2011-05-24 12:43:12 +0100501 BUILTIN_CALL, // default
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100502
Ben Murdoch257744e2011-11-30 15:57:28 +0000503 // Builtin that takes float arguments and returns an int.
504 // int f(double, double).
505 BUILTIN_COMPARE_CALL,
506
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100507 // Builtin call that returns floating point.
508 // double f(double, double).
Ben Murdoch257744e2011-11-30 15:57:28 +0000509 BUILTIN_FP_FP_CALL,
510
511 // Builtin call that returns floating point.
512 // double f(double).
513 BUILTIN_FP_CALL,
514
515 // Builtin call that returns floating point.
516 // double f(double, int).
517 BUILTIN_FP_INT_CALL,
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100518
519 // Direct call to API function callback.
520 // Handle<Value> f(v8::Arguments&)
521 DIRECT_API_CALL,
522
523 // Direct call to accessor getter callback.
524 // Handle<value> f(Local<String> property, AccessorInfo& info)
525 DIRECT_GETTER_CALL
Steve Block1e0659c2011-05-24 12:43:12 +0100526 };
527
528 typedef void* ExternalReferenceRedirector(void* original, Type type);
529
Steve Block44f0eee2011-05-26 01:26:41 +0100530 ExternalReference(Builtins::CFunctionId id, Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000531
Steve Block44f0eee2011-05-26 01:26:41 +0100532 ExternalReference(ApiFunction* ptr, Type type, Isolate* isolate);
Steve Blockd0582a62009-12-15 09:54:21 +0000533
Steve Block44f0eee2011-05-26 01:26:41 +0100534 ExternalReference(Builtins::Name name, Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000535
Steve Block44f0eee2011-05-26 01:26:41 +0100536 ExternalReference(Runtime::FunctionId id, Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000537
Steve Block44f0eee2011-05-26 01:26:41 +0100538 ExternalReference(const Runtime::Function* f, Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000539
Steve Block44f0eee2011-05-26 01:26:41 +0100540 ExternalReference(const IC_Utility& ic_utility, Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000541
542#ifdef ENABLE_DEBUGGER_SUPPORT
Steve Block44f0eee2011-05-26 01:26:41 +0100543 ExternalReference(const Debug_Address& debug_address, Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000544#endif
545
546 explicit ExternalReference(StatsCounter* counter);
547
Steve Block44f0eee2011-05-26 01:26:41 +0100548 ExternalReference(Isolate::AddressId id, Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000549
550 explicit ExternalReference(const SCTableReference& table_ref);
551
Steve Block44f0eee2011-05-26 01:26:41 +0100552 // Isolate::Current() as an external reference.
553 static ExternalReference isolate_address();
554
Steve Blocka7e24c12009-10-30 11:49:00 +0000555 // One-of-a-kind references. These references are not part of a general
556 // pattern. This means that they have to be added to the
557 // ExternalReferenceTable in serialize.cc manually.
558
Steve Block44f0eee2011-05-26 01:26:41 +0100559 static ExternalReference perform_gc_function(Isolate* isolate);
560 static ExternalReference fill_heap_number_with_random_function(
561 Isolate* isolate);
562 static ExternalReference random_uint32_function(Isolate* isolate);
563 static ExternalReference transcendental_cache_array_address(Isolate* isolate);
564 static ExternalReference delete_handle_scope_extensions(Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000565
Ben Murdochb0fe1622011-05-05 13:52:32 +0100566 // Deoptimization support.
Steve Block44f0eee2011-05-26 01:26:41 +0100567 static ExternalReference new_deoptimizer_function(Isolate* isolate);
568 static ExternalReference compute_output_frames_function(Isolate* isolate);
569 static ExternalReference global_contexts_list(Isolate* isolate);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100570
Leon Clarkee46be812010-01-19 14:06:41 +0000571 // Static data in the keyed lookup cache.
Steve Block44f0eee2011-05-26 01:26:41 +0100572 static ExternalReference keyed_lookup_cache_keys(Isolate* isolate);
573 static ExternalReference keyed_lookup_cache_field_offsets(Isolate* isolate);
Leon Clarkee46be812010-01-19 14:06:41 +0000574
Steve Blocka7e24c12009-10-30 11:49:00 +0000575 // Static variable Factory::the_hole_value.location()
Steve Block44f0eee2011-05-26 01:26:41 +0100576 static ExternalReference the_hole_value_location(Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000577
Ben Murdoch086aeea2011-05-13 15:57:08 +0100578 // Static variable Factory::arguments_marker.location()
Steve Block44f0eee2011-05-26 01:26:41 +0100579 static ExternalReference arguments_marker_location(Isolate* isolate);
Ben Murdoch086aeea2011-05-13 15:57:08 +0100580
Steve Blocka7e24c12009-10-30 11:49:00 +0000581 // Static variable Heap::roots_address()
Steve Block44f0eee2011-05-26 01:26:41 +0100582 static ExternalReference roots_address(Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000583
584 // Static variable StackGuard::address_of_jslimit()
Steve Block44f0eee2011-05-26 01:26:41 +0100585 static ExternalReference address_of_stack_limit(Isolate* isolate);
Steve Blockd0582a62009-12-15 09:54:21 +0000586
587 // Static variable StackGuard::address_of_real_jslimit()
Steve Block44f0eee2011-05-26 01:26:41 +0100588 static ExternalReference address_of_real_stack_limit(Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000589
590 // Static variable RegExpStack::limit_address()
Steve Block44f0eee2011-05-26 01:26:41 +0100591 static ExternalReference address_of_regexp_stack_limit(Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000592
Leon Clarkee46be812010-01-19 14:06:41 +0000593 // Static variables for RegExp.
Steve Block44f0eee2011-05-26 01:26:41 +0100594 static ExternalReference address_of_static_offsets_vector(Isolate* isolate);
595 static ExternalReference address_of_regexp_stack_memory_address(
596 Isolate* isolate);
597 static ExternalReference address_of_regexp_stack_memory_size(
598 Isolate* isolate);
Leon Clarkee46be812010-01-19 14:06:41 +0000599
Steve Blocka7e24c12009-10-30 11:49:00 +0000600 // Static variable Heap::NewSpaceStart()
Steve Block44f0eee2011-05-26 01:26:41 +0100601 static ExternalReference new_space_start(Isolate* isolate);
602 static ExternalReference new_space_mask(Isolate* isolate);
603 static ExternalReference heap_always_allocate_scope_depth(Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000604
605 // Used for fast allocation in generated code.
Steve Block44f0eee2011-05-26 01:26:41 +0100606 static ExternalReference new_space_allocation_top_address(Isolate* isolate);
607 static ExternalReference new_space_allocation_limit_address(Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000608
Steve Block44f0eee2011-05-26 01:26:41 +0100609 static ExternalReference double_fp_operation(Token::Value operation,
610 Isolate* isolate);
611 static ExternalReference compare_doubles(Isolate* isolate);
612 static ExternalReference power_double_double_function(Isolate* isolate);
613 static ExternalReference power_double_int_function(Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000614
Steve Blockd0582a62009-12-15 09:54:21 +0000615 static ExternalReference handle_scope_next_address();
616 static ExternalReference handle_scope_limit_address();
John Reck59135872010-11-02 12:39:01 -0700617 static ExternalReference handle_scope_level_address();
Steve Blockd0582a62009-12-15 09:54:21 +0000618
Steve Block44f0eee2011-05-26 01:26:41 +0100619 static ExternalReference scheduled_exception_address(Isolate* isolate);
Steve Blockd0582a62009-12-15 09:54:21 +0000620
Ben Murdochb0fe1622011-05-05 13:52:32 +0100621 // Static variables containing common double constants.
622 static ExternalReference address_of_min_int();
623 static ExternalReference address_of_one_half();
Ben Murdochb8e0da22011-05-16 14:20:40 +0100624 static ExternalReference address_of_minus_zero();
Ben Murdoch257744e2011-11-30 15:57:28 +0000625 static ExternalReference address_of_zero();
626 static ExternalReference address_of_uint8_max_value();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100627 static ExternalReference address_of_negative_infinity();
Steve Block44f0eee2011-05-26 01:26:41 +0100628 static ExternalReference address_of_nan();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100629
Steve Block44f0eee2011-05-26 01:26:41 +0100630 static ExternalReference math_sin_double_function(Isolate* isolate);
631 static ExternalReference math_cos_double_function(Isolate* isolate);
632 static ExternalReference math_log_double_function(Isolate* isolate);
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100633
Steve Blocka7e24c12009-10-30 11:49:00 +0000634 Address address() const {return reinterpret_cast<Address>(address_);}
635
636#ifdef ENABLE_DEBUGGER_SUPPORT
637 // Function Debug::Break()
Steve Block44f0eee2011-05-26 01:26:41 +0100638 static ExternalReference debug_break(Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000639
640 // Used to check if single stepping is enabled in generated code.
Steve Block44f0eee2011-05-26 01:26:41 +0100641 static ExternalReference debug_step_in_fp_address(Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000642#endif
643
Steve Block6ded16b2010-05-10 14:33:55 +0100644#ifndef V8_INTERPRETED_REGEXP
Steve Blocka7e24c12009-10-30 11:49:00 +0000645 // C functions called from RegExp generated code.
646
647 // Function NativeRegExpMacroAssembler::CaseInsensitiveCompareUC16()
Steve Block44f0eee2011-05-26 01:26:41 +0100648 static ExternalReference re_case_insensitive_compare_uc16(Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000649
650 // Function RegExpMacroAssembler*::CheckStackGuardState()
Steve Block44f0eee2011-05-26 01:26:41 +0100651 static ExternalReference re_check_stack_guard_state(Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000652
653 // Function NativeRegExpMacroAssembler::GrowStack()
Steve Block44f0eee2011-05-26 01:26:41 +0100654 static ExternalReference re_grow_stack(Isolate* isolate);
Leon Clarkee46be812010-01-19 14:06:41 +0000655
656 // byte NativeRegExpMacroAssembler::word_character_bitmap
657 static ExternalReference re_word_character_map();
658
Steve Blocka7e24c12009-10-30 11:49:00 +0000659#endif
660
661 // This lets you register a function that rewrites all external references.
662 // Used by the ARM simulator to catch calls to external references.
Ben Murdoch257744e2011-11-30 15:57:28 +0000663 static void set_redirector(Isolate* isolate,
664 ExternalReferenceRedirector* redirector) {
Steve Block44f0eee2011-05-26 01:26:41 +0100665 // We can't stack them.
Ben Murdoch257744e2011-11-30 15:57:28 +0000666 ASSERT(isolate->external_reference_redirector() == NULL);
667 isolate->set_external_reference_redirector(
Steve Block44f0eee2011-05-26 01:26:41 +0100668 reinterpret_cast<ExternalReferenceRedirectorPointer*>(redirector));
Steve Blocka7e24c12009-10-30 11:49:00 +0000669 }
670
671 private:
672 explicit ExternalReference(void* address)
673 : address_(address) {}
674
Steve Block44f0eee2011-05-26 01:26:41 +0100675 static void* Redirect(Isolate* isolate,
676 void* address,
Steve Block1e0659c2011-05-24 12:43:12 +0100677 Type type = ExternalReference::BUILTIN_CALL) {
Steve Block44f0eee2011-05-26 01:26:41 +0100678 ExternalReferenceRedirector* redirector =
679 reinterpret_cast<ExternalReferenceRedirector*>(
680 isolate->external_reference_redirector());
681 if (redirector == NULL) return address;
682 void* answer = (*redirector)(address, type);
Steve Blockd0582a62009-12-15 09:54:21 +0000683 return answer;
Steve Blocka7e24c12009-10-30 11:49:00 +0000684 }
685
Steve Block44f0eee2011-05-26 01:26:41 +0100686 static void* Redirect(Isolate* isolate,
687 Address address_arg,
Steve Block1e0659c2011-05-24 12:43:12 +0100688 Type type = ExternalReference::BUILTIN_CALL) {
Steve Block44f0eee2011-05-26 01:26:41 +0100689 ExternalReferenceRedirector* redirector =
690 reinterpret_cast<ExternalReferenceRedirector*>(
691 isolate->external_reference_redirector());
Steve Blocka7e24c12009-10-30 11:49:00 +0000692 void* address = reinterpret_cast<void*>(address_arg);
Steve Block44f0eee2011-05-26 01:26:41 +0100693 void* answer = (redirector == NULL) ?
Steve Blockd0582a62009-12-15 09:54:21 +0000694 address :
Steve Block44f0eee2011-05-26 01:26:41 +0100695 (*redirector)(address, type);
Steve Blockd0582a62009-12-15 09:54:21 +0000696 return answer;
Steve Blocka7e24c12009-10-30 11:49:00 +0000697 }
698
699 void* address_;
700};
701
702
703// -----------------------------------------------------------------------------
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800704// Position recording support
705
Ben Murdochb0fe1622011-05-05 13:52:32 +0100706struct PositionState {
707 PositionState() : current_position(RelocInfo::kNoPosition),
708 written_position(RelocInfo::kNoPosition),
709 current_statement_position(RelocInfo::kNoPosition),
710 written_statement_position(RelocInfo::kNoPosition) {}
711
712 int current_position;
713 int written_position;
714
715 int current_statement_position;
716 int written_statement_position;
717};
718
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800719
720class PositionsRecorder BASE_EMBEDDED {
721 public:
722 explicit PositionsRecorder(Assembler* assembler)
Ben Murdochb8e0da22011-05-16 14:20:40 +0100723 : assembler_(assembler) {
724#ifdef ENABLE_GDB_JIT_INTERFACE
725 gdbjit_lineinfo_ = NULL;
726#endif
727 }
728
729#ifdef ENABLE_GDB_JIT_INTERFACE
730 ~PositionsRecorder() {
731 delete gdbjit_lineinfo_;
732 }
733
734 void StartGDBJITLineInfoRecording() {
735 if (FLAG_gdbjit) {
736 gdbjit_lineinfo_ = new GDBJITLineInfo();
737 }
738 }
739
740 GDBJITLineInfo* DetachGDBJITLineInfo() {
741 GDBJITLineInfo* lineinfo = gdbjit_lineinfo_;
742 gdbjit_lineinfo_ = NULL; // To prevent deallocation in destructor.
743 return lineinfo;
744 }
745#endif
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800746
Ben Murdochb0fe1622011-05-05 13:52:32 +0100747 // Set current position to pos.
748 void RecordPosition(int pos);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800749
750 // Set current statement position to pos.
751 void RecordStatementPosition(int pos);
752
753 // Write recorded positions to relocation information.
754 bool WriteRecordedPositions();
755
Ben Murdochb0fe1622011-05-05 13:52:32 +0100756 int current_position() const { return state_.current_position; }
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800757
Ben Murdochb0fe1622011-05-05 13:52:32 +0100758 int current_statement_position() const {
759 return state_.current_statement_position;
760 }
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800761
762 private:
763 Assembler* assembler_;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100764 PositionState state_;
Ben Murdochb8e0da22011-05-16 14:20:40 +0100765#ifdef ENABLE_GDB_JIT_INTERFACE
766 GDBJITLineInfo* gdbjit_lineinfo_;
767#endif
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800768
Ben Murdochb0fe1622011-05-05 13:52:32 +0100769 friend class PreservePositionScope;
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800770
Ben Murdochb0fe1622011-05-05 13:52:32 +0100771 DISALLOW_COPY_AND_ASSIGN(PositionsRecorder);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800772};
773
774
Ben Murdochb0fe1622011-05-05 13:52:32 +0100775class PreservePositionScope BASE_EMBEDDED {
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800776 public:
Ben Murdochb0fe1622011-05-05 13:52:32 +0100777 explicit PreservePositionScope(PositionsRecorder* positions_recorder)
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800778 : positions_recorder_(positions_recorder),
Ben Murdochb0fe1622011-05-05 13:52:32 +0100779 saved_state_(positions_recorder->state_) {}
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800780
Ben Murdochb0fe1622011-05-05 13:52:32 +0100781 ~PreservePositionScope() {
782 positions_recorder_->state_ = saved_state_;
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800783 }
784
785 private:
786 PositionsRecorder* positions_recorder_;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100787 const PositionState saved_state_;
788
789 DISALLOW_COPY_AND_ASSIGN(PreservePositionScope);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800790};
791
792
793// -----------------------------------------------------------------------------
Steve Blocka7e24c12009-10-30 11:49:00 +0000794// Utility functions
795
796static inline bool is_intn(int x, int n) {
797 return -(1 << (n-1)) <= x && x < (1 << (n-1));
798}
799
Steve Blocka7e24c12009-10-30 11:49:00 +0000800static inline bool is_int8(int x) { return is_intn(x, 8); }
Andrei Popescu31002712010-02-23 13:46:05 +0000801static inline bool is_int16(int x) { return is_intn(x, 16); }
802static inline bool is_int18(int x) { return is_intn(x, 18); }
803static inline bool is_int24(int x) { return is_intn(x, 24); }
Steve Blocka7e24c12009-10-30 11:49:00 +0000804
805static inline bool is_uintn(int x, int n) {
806 return (x & -(1 << n)) == 0;
807}
808
809static inline bool is_uint2(int x) { return is_uintn(x, 2); }
810static inline bool is_uint3(int x) { return is_uintn(x, 3); }
811static inline bool is_uint4(int x) { return is_uintn(x, 4); }
812static inline bool is_uint5(int x) { return is_uintn(x, 5); }
813static inline bool is_uint6(int x) { return is_uintn(x, 6); }
814static inline bool is_uint8(int x) { return is_uintn(x, 8); }
Andrei Popescu31002712010-02-23 13:46:05 +0000815static inline bool is_uint10(int x) { return is_uintn(x, 10); }
Steve Blocka7e24c12009-10-30 11:49:00 +0000816static inline bool is_uint12(int x) { return is_uintn(x, 12); }
817static inline bool is_uint16(int x) { return is_uintn(x, 16); }
818static inline bool is_uint24(int x) { return is_uintn(x, 24); }
Andrei Popescu31002712010-02-23 13:46:05 +0000819static inline bool is_uint26(int x) { return is_uintn(x, 26); }
820static inline bool is_uint28(int x) { return is_uintn(x, 28); }
821
822static inline int NumberOfBitsSet(uint32_t x) {
823 unsigned int num_bits_set;
824 for (num_bits_set = 0; x; x >>= 1) {
825 num_bits_set += x & 1;
826 }
827 return num_bits_set;
828}
Steve Blocka7e24c12009-10-30 11:49:00 +0000829
Ben Murdochb0fe1622011-05-05 13:52:32 +0100830// Computes pow(x, y) with the special cases in the spec for Math.pow.
831double power_double_int(double x, int y);
832double power_double_double(double x, double y);
833
Ben Murdoch257744e2011-11-30 15:57:28 +0000834// Helper class for generating code or data associated with the code
835// right after a call instruction. As an example this can be used to
836// generate safepoint data after calls for crankshaft.
837class CallWrapper {
838 public:
839 CallWrapper() { }
840 virtual ~CallWrapper() { }
841 // Called just before emitting a call. Argument is the size of the generated
842 // call code.
843 virtual void BeforeCall(int call_size) const = 0;
844 // Called just after emitting a call, i.e., at the return site for the call.
845 virtual void AfterCall() const = 0;
846};
847
848class NullCallWrapper : public CallWrapper {
849 public:
850 NullCallWrapper() { }
851 virtual ~NullCallWrapper() { }
852 virtual void BeforeCall(int call_size) const { }
853 virtual void AfterCall() const { }
854};
855
Steve Blocka7e24c12009-10-30 11:49:00 +0000856} } // namespace v8::internal
857
858#endif // V8_ASSEMBLER_H_