blob: e8bc5d6caa675366cb3d895e59aafb3c62c85d36 [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.
ager@chromium.orgeadaf222009-06-16 09:43:10 +000033// Copyright 2006-2009 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
erik.corry@gmail.com0511e242011-01-19 11:11:08 +000038#include "gdb-jit.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000039#include "runtime.h"
40#include "top.h"
ager@chromium.org65dad4b2009-04-23 08:48:43 +000041#include "token.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000042
kasperl@chromium.org71affb52009-05-26 05:44:31 +000043namespace v8 {
44namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000045
46
47// -----------------------------------------------------------------------------
kasperl@chromium.orga5551262010-12-07 12:49:48 +000048// Common double constants.
49
50class DoubleConstant: public AllStatic {
51 public:
52 static const double min_int;
53 static const double one_half;
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +000054 static const double minus_zero;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +000055 static const double negative_infinity;
kasperl@chromium.orga5551262010-12-07 12:49:48 +000056};
57
58
59// -----------------------------------------------------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000060// Labels represent pc locations; they are typically jump or call targets.
61// After declaration, a label can be freely used to denote known or (yet)
62// unknown pc location. Assembler::bind() is used to bind a label to the
63// current pc. A label can be bound only once.
64
kasperl@chromium.org7be3c992009-03-12 07:19:55 +000065class Label BASE_EMBEDDED {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000066 public:
67 INLINE(Label()) { Unuse(); }
68 INLINE(~Label()) { ASSERT(!is_linked()); }
69
70 INLINE(void Unuse()) { pos_ = 0; }
71
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +000072 INLINE(bool is_bound() const) { return pos_ < 0; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000073 INLINE(bool is_unused() const) { return pos_ == 0; }
74 INLINE(bool is_linked() const) { return pos_ > 0; }
75
76 // Returns the position of bound or linked labels. Cannot be used
77 // for unused labels.
78 int pos() const;
79
80 private:
81 // pos_ encodes both the binding state (via its sign)
82 // and the binding position (via its value) of a label.
83 //
84 // pos_ < 0 bound label, pos() returns the jump target position
85 // pos_ == 0 unused label
86 // pos_ > 0 linked label, pos() returns the last reference position
87 int pos_;
88
89 void bind_to(int pos) {
90 pos_ = -pos - 1;
91 ASSERT(is_bound());
92 }
93 void link_to(int pos) {
94 pos_ = pos + 1;
95 ASSERT(is_linked());
96 }
97
98 friend class Assembler;
ager@chromium.orga74f0da2008-12-03 16:05:52 +000099 friend class RegexpAssembler;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000100 friend class Displacement;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000101 friend class ShadowTarget;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000102 friend class RegExpMacroAssemblerIrregexp;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000103};
104
105
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000106// -----------------------------------------------------------------------------
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000107// NearLabels are labels used for short jumps (in Intel jargon).
108// NearLabels should be used if it can be guaranteed that the jump range is
109// within -128 to +127. We already use short jumps when jumping backwards,
110// so using a NearLabel will only have performance impact if used for forward
111// jumps.
112class NearLabel BASE_EMBEDDED {
113 public:
114 NearLabel() { Unuse(); }
115 ~NearLabel() { ASSERT(!is_linked()); }
116
117 void Unuse() {
118 pos_ = -1;
119 unresolved_branches_ = 0;
120#ifdef DEBUG
121 for (int i = 0; i < kMaxUnresolvedBranches; i++) {
122 unresolved_positions_[i] = -1;
123 }
124#endif
125 }
126
127 int pos() {
128 ASSERT(is_bound());
129 return pos_;
130 }
131
132 bool is_bound() { return pos_ >= 0; }
133 bool is_linked() { return !is_bound() && unresolved_branches_ > 0; }
134 bool is_unused() { return !is_bound() && unresolved_branches_ == 0; }
135
136 void bind_to(int position) {
137 ASSERT(!is_bound());
138 pos_ = position;
139 }
140
141 void link_to(int position) {
142 ASSERT(!is_bound());
143 ASSERT(unresolved_branches_ < kMaxUnresolvedBranches);
144 unresolved_positions_[unresolved_branches_++] = position;
145 }
146
147 private:
148 static const int kMaxUnresolvedBranches = 8;
149 int pos_;
150 int unresolved_branches_;
151 int unresolved_positions_[kMaxUnresolvedBranches];
152
153 friend class Assembler;
154};
155
156
157// -----------------------------------------------------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000158// Relocation information
159
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000160
161// Relocation information consists of the address (pc) of the datum
162// to which the relocation information applies, the relocation mode
163// (rmode), and an optional data field. The relocation mode may be
164// "descriptive" and not indicate a need for relocation, but simply
165// describe a property of the datum. Such rmodes are useful for GC
166// and nice disassembly output.
167
168class RelocInfo BASE_EMBEDDED {
169 public:
ager@chromium.org236ad962008-09-25 09:45:57 +0000170 // The constant kNoPosition is used with the collecting of source positions
171 // in the relocation information. Two types of source positions are collected
172 // "position" (RelocMode position) and "statement position" (RelocMode
173 // statement_position). The "position" is collected at places in the source
174 // code which are of interest when making stack traces to pin-point the source
175 // location of a stack frame as close as possible. The "statement position" is
176 // collected at the beginning at each statement, and is used to indicate
177 // possible break locations. kNoPosition is used to indicate an
178 // invalid/uninitialized position value.
179 static const int kNoPosition = -1;
180
181 enum Mode {
182 // Please note the order is important (see IsCodeTarget, IsGCRelocMode).
183 CONSTRUCT_CALL, // code target that is a call to a JavaScript constructor.
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000184 CODE_TARGET_CONTEXT, // Code target used for contextual loads.
185 DEBUG_BREAK, // Code target for the debugger statement.
186 CODE_TARGET, // Code target which is not any of the above.
ager@chromium.org236ad962008-09-25 09:45:57 +0000187 EMBEDDED_OBJECT,
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000188 GLOBAL_PROPERTY_CELL,
189
ager@chromium.org236ad962008-09-25 09:45:57 +0000190 // Everything after runtime_entry (inclusive) is not GC'ed.
191 RUNTIME_ENTRY,
192 JS_RETURN, // Marks start of the ExitJSFrame code.
193 COMMENT,
194 POSITION, // See comment for kNoPosition above.
195 STATEMENT_POSITION, // See comment for kNoPosition above.
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000196 DEBUG_BREAK_SLOT, // Additional code inserted for debug break slot.
ager@chromium.org236ad962008-09-25 09:45:57 +0000197 EXTERNAL_REFERENCE, // The address of an external C++ function.
198 INTERNAL_REFERENCE, // An address inside the same function.
199
200 // add more as needed
201 // Pseudo-types
202 NUMBER_OF_MODES, // must be no greater than 14 - see RelocInfoWriter
203 NONE, // never recorded
204 LAST_CODE_ENUM = CODE_TARGET,
ager@chromium.org378b34e2011-01-28 08:04:38 +0000205 LAST_GCED_ENUM = GLOBAL_PROPERTY_CELL
ager@chromium.org236ad962008-09-25 09:45:57 +0000206 };
207
208
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000209 RelocInfo() {}
ager@chromium.org236ad962008-09-25 09:45:57 +0000210 RelocInfo(byte* pc, Mode rmode, intptr_t data)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000211 : pc_(pc), rmode_(rmode), data_(data) {
212 }
213
ager@chromium.org236ad962008-09-25 09:45:57 +0000214 static inline bool IsConstructCall(Mode mode) {
215 return mode == CONSTRUCT_CALL;
216 }
217 static inline bool IsCodeTarget(Mode mode) {
218 return mode <= LAST_CODE_ENUM;
219 }
220 // Is the relocation mode affected by GC?
221 static inline bool IsGCRelocMode(Mode mode) {
222 return mode <= LAST_GCED_ENUM;
223 }
224 static inline bool IsJSReturn(Mode mode) {
225 return mode == JS_RETURN;
226 }
227 static inline bool IsComment(Mode mode) {
228 return mode == COMMENT;
229 }
230 static inline bool IsPosition(Mode mode) {
231 return mode == POSITION || mode == STATEMENT_POSITION;
232 }
233 static inline bool IsStatementPosition(Mode mode) {
234 return mode == STATEMENT_POSITION;
235 }
236 static inline bool IsExternalReference(Mode mode) {
237 return mode == EXTERNAL_REFERENCE;
238 }
239 static inline bool IsInternalReference(Mode mode) {
240 return mode == INTERNAL_REFERENCE;
241 }
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000242 static inline bool IsDebugBreakSlot(Mode mode) {
243 return mode == DEBUG_BREAK_SLOT;
244 }
ager@chromium.org236ad962008-09-25 09:45:57 +0000245 static inline int ModeMask(Mode mode) { return 1 << mode; }
246
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000247 // Accessors
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000248 byte* pc() const { return pc_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000249 void set_pc(byte* pc) { pc_ = pc; }
ager@chromium.org236ad962008-09-25 09:45:57 +0000250 Mode rmode() const { return rmode_; }
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000251 intptr_t data() const { return data_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000252
253 // Apply a relocation by delta bytes
ager@chromium.org3e875802009-06-29 08:26:34 +0000254 INLINE(void apply(intptr_t delta));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000255
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000256 // Is the pointer this relocation info refers to coded like a plain pointer
257 // or is it strange in some way (eg relative or patched into a series of
258 // instructions).
259 bool IsCodedSpecially();
260
ager@chromium.org32912102009-01-16 10:38:43 +0000261 // Read/modify the code target in the branch/call instruction
262 // this relocation applies to;
263 // can only be called if IsCodeTarget(rmode_) || rmode_ == RUNTIME_ENTRY
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000264 INLINE(Address target_address());
265 INLINE(void set_target_address(Address target));
266 INLINE(Object* target_object());
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000267 INLINE(Handle<Object> target_object_handle(Assembler* origin));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000268 INLINE(Object** target_object_address());
269 INLINE(void set_target_object(Object* target));
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000270 INLINE(JSGlobalPropertyCell* target_cell());
271 INLINE(Handle<JSGlobalPropertyCell> target_cell_handle());
272 INLINE(void set_target_cell(JSGlobalPropertyCell* cell));
273
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000274
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000275 // Read the address of the word containing the target_address in an
276 // instruction stream. What this means exactly is architecture-independent.
277 // The only architecture-independent user of this function is the serializer.
278 // The serializer uses it to find out how many raw bytes of instruction to
279 // output before the next target. Architecture-independent code shouldn't
280 // dereference the pointer it gets back from this.
ager@chromium.org32912102009-01-16 10:38:43 +0000281 INLINE(Address target_address_address());
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000282 // This indicates how much space a target takes up when deserializing a code
283 // stream. For most architectures this is just the size of a pointer. For
284 // an instruction like movw/movt where the target bits are mixed into the
285 // instruction bits the size of the target will be zero, indicating that the
286 // serializer should not step forwards in memory after a target is resolved
287 // and written. In this case the target_address_address function above
288 // should return the end of the instructions to be patched, allowing the
289 // deserializer to deserialize the instructions as raw bytes and put them in
290 // place, ready to be patched with the target.
291 INLINE(int target_address_size());
ager@chromium.org32912102009-01-16 10:38:43 +0000292
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000293 // Read/modify the reference in the instruction this relocation
294 // applies to; can only be called if rmode_ is external_reference
295 INLINE(Address* target_reference_address());
296
297 // Read/modify the address of a call instruction. This is used to relocate
298 // the break points where straight-line code is patched with a call
299 // instruction.
300 INLINE(Address call_address());
301 INLINE(void set_call_address(Address target));
302 INLINE(Object* call_object());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000303 INLINE(void set_call_object(Object* target));
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +0000304 INLINE(Object** call_object_address());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000305
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000306 template<typename StaticVisitor> inline void Visit();
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000307 inline void Visit(ObjectVisitor* v);
308
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000309 // Patch the code with some other code.
iposva@chromium.org245aa852009-02-10 00:49:54 +0000310 void PatchCode(byte* instructions, int instruction_count);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000311
312 // Patch the code with a call.
iposva@chromium.org245aa852009-02-10 00:49:54 +0000313 void PatchCodeWithCall(Address target, int guard_bytes);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000314
315 // Check whether this return sequence has been patched
316 // with a call to the debugger.
317 INLINE(bool IsPatchedReturnSequence());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000318
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000319 // Check whether this debug break slot has been patched with a call to the
320 // debugger.
321 INLINE(bool IsPatchedDebugBreakSlotSequence());
322
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000323#ifdef ENABLE_DISASSEMBLER
324 // Printing
ager@chromium.org236ad962008-09-25 09:45:57 +0000325 static const char* RelocModeName(Mode rmode);
whesse@chromium.org023421e2010-12-21 12:19:12 +0000326 void Print(FILE* out);
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000327#endif // ENABLE_DISASSEMBLER
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000328#ifdef DEBUG
329 // Debugging
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000330 void Verify();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000331#endif
332
ager@chromium.org236ad962008-09-25 09:45:57 +0000333 static const int kCodeTargetMask = (1 << (LAST_CODE_ENUM + 1)) - 1;
334 static const int kPositionMask = 1 << POSITION | 1 << STATEMENT_POSITION;
335 static const int kDebugMask = kPositionMask | 1 << COMMENT;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000336 static const int kApplyMask; // Modes affected by apply. Depends on arch.
337
338 private:
339 // On ARM, note that pc_ is the address of the constant pool entry
340 // to be relocated and not the address of the instruction
341 // referencing the constant pool entry (except when rmode_ ==
342 // comment).
343 byte* pc_;
ager@chromium.org236ad962008-09-25 09:45:57 +0000344 Mode rmode_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000345 intptr_t data_;
346 friend class RelocIterator;
347};
348
349
350// RelocInfoWriter serializes a stream of relocation info. It writes towards
351// lower addresses.
352class RelocInfoWriter BASE_EMBEDDED {
353 public:
354 RelocInfoWriter() : pos_(NULL), last_pc_(NULL), last_data_(0) {}
355 RelocInfoWriter(byte* pos, byte* pc) : pos_(pos), last_pc_(pc),
356 last_data_(0) {}
357
358 byte* pos() const { return pos_; }
359 byte* last_pc() const { return last_pc_; }
360
361 void Write(const RelocInfo* rinfo);
362
363 // Update the state of the stream after reloc info buffer
364 // and/or code is moved while the stream is active.
365 void Reposition(byte* pos, byte* pc) {
366 pos_ = pos;
367 last_pc_ = pc;
368 }
369
ager@chromium.org3e875802009-06-29 08:26:34 +0000370 // Max size (bytes) of a written RelocInfo. Longest encoding is
371 // ExtraTag, VariableLengthPCJump, ExtraTag, pc_delta, ExtraTag, data_delta.
372 // On ia32 and arm this is 1 + 4 + 1 + 1 + 1 + 4 = 12.
373 // On x64 this is 1 + 4 + 1 + 1 + 1 + 8 == 16;
374 // Here we use the maximum of the two.
375 static const int kMaxSize = 16;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000376
377 private:
378 inline uint32_t WriteVariableLengthPCJump(uint32_t pc_delta);
379 inline void WriteTaggedPC(uint32_t pc_delta, int tag);
380 inline void WriteExtraTaggedPC(uint32_t pc_delta, int extra_tag);
ager@chromium.orge2902be2009-06-08 12:21:35 +0000381 inline void WriteExtraTaggedData(intptr_t data_delta, int top_tag);
382 inline void WriteTaggedData(intptr_t data_delta, int tag);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000383 inline void WriteExtraTag(int extra_tag, int top_tag);
384
385 byte* pos_;
386 byte* last_pc_;
387 intptr_t last_data_;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000388 DISALLOW_COPY_AND_ASSIGN(RelocInfoWriter);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000389};
390
391
392// A RelocIterator iterates over relocation information.
393// Typical use:
394//
395// for (RelocIterator it(code); !it.done(); it.next()) {
396// // do something with it.rinfo() here
397// }
398//
399// A mask can be specified to skip unwanted modes.
400class RelocIterator: public Malloced {
401 public:
402 // Create a new iterator positioned at
403 // the beginning of the reloc info.
404 // Relocation information with mode k is included in the
405 // iteration iff bit k of mode_mask is set.
406 explicit RelocIterator(Code* code, int mode_mask = -1);
407 explicit RelocIterator(const CodeDesc& desc, int mode_mask = -1);
408
409 // Iteration
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000410 bool done() const { return done_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000411 void next();
412
413 // Return pointer valid until next next().
414 RelocInfo* rinfo() {
415 ASSERT(!done());
416 return &rinfo_;
417 }
418
419 private:
420 // Advance* moves the position before/after reading.
421 // *Read* reads from current byte(s) into rinfo_.
422 // *Get* just reads and returns info on current byte.
423 void Advance(int bytes = 1) { pos_ -= bytes; }
424 int AdvanceGetTag();
425 int GetExtraTag();
426 int GetTopTag();
427 void ReadTaggedPC();
428 void AdvanceReadPC();
429 void AdvanceReadData();
430 void AdvanceReadVariableLengthPCJump();
431 int GetPositionTypeTag();
432 void ReadTaggedData();
433
ager@chromium.org236ad962008-09-25 09:45:57 +0000434 static RelocInfo::Mode DebugInfoModeFromTag(int tag);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000435
436 // If the given mode is wanted, set it in rinfo_ and return true.
437 // Else return false. Used for efficiently skipping unwanted modes.
ager@chromium.org236ad962008-09-25 09:45:57 +0000438 bool SetMode(RelocInfo::Mode mode) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000439 return (mode_mask_ & (1 << mode)) ? (rinfo_.rmode_ = mode, true) : false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000440 }
441
442 byte* pos_;
443 byte* end_;
444 RelocInfo rinfo_;
445 bool done_;
446 int mode_mask_;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000447 DISALLOW_COPY_AND_ASSIGN(RelocIterator);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000448};
449
450
451//------------------------------------------------------------------------------
452// External function
453
454//----------------------------------------------------------------------------
455class IC_Utility;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000456class SCTableReference;
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000457#ifdef ENABLE_DEBUGGER_SUPPORT
458class Debug_Address;
459#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000460
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000461
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000462// An ExternalReference represents a C++ address used in the generated
463// code. All references to C++ functions and variables must be encapsulated in
464// an ExternalReference instance. This is done in order to track the origin of
465// all external references in the code so that they can be bound to the correct
466// addresses when deserializing a heap.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000467class ExternalReference BASE_EMBEDDED {
468 public:
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000469 // Used in the simulator to support different native api calls.
470 //
471 // BUILTIN_CALL - builtin call.
472 // MaybeObject* f(v8::internal::Arguments).
473 //
474 // FP_RETURN_CALL - builtin call that returns floating point.
475 // double f(double, double).
476 //
477 // DIRECT_CALL - direct call to API function native callback
478 // from generated code.
479 // Handle<Value> f(v8::Arguments&)
480 //
481 enum Type {
482 BUILTIN_CALL, // default
483 FP_RETURN_CALL,
484 DIRECT_CALL
485 };
486
487 typedef void* ExternalReferenceRedirector(void* original, Type type);
488
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000489 explicit ExternalReference(Builtins::CFunctionId id);
490
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000491 explicit ExternalReference(ApiFunction* ptr, Type type);
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000492
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000493 explicit ExternalReference(Builtins::Name name);
494
495 explicit ExternalReference(Runtime::FunctionId id);
496
497 explicit ExternalReference(Runtime::Function* f);
498
499 explicit ExternalReference(const IC_Utility& ic_utility);
500
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000501#ifdef ENABLE_DEBUGGER_SUPPORT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000502 explicit ExternalReference(const Debug_Address& debug_address);
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000503#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000504
505 explicit ExternalReference(StatsCounter* counter);
506
507 explicit ExternalReference(Top::AddressId id);
508
509 explicit ExternalReference(const SCTableReference& table_ref);
510
511 // One-of-a-kind references. These references are not part of a general
512 // pattern. This means that they have to be added to the
513 // ExternalReferenceTable in serialize.cc manually.
514
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000515 static ExternalReference perform_gc_function();
ager@chromium.org357bf652010-04-12 11:30:10 +0000516 static ExternalReference fill_heap_number_with_random_function();
517 static ExternalReference random_uint32_function();
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000518 static ExternalReference transcendental_cache_array_address();
lrn@chromium.org303ada72010-10-27 09:33:13 +0000519 static ExternalReference delete_handle_scope_extensions();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000520
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000521 // Deoptimization support.
522 static ExternalReference new_deoptimizer_function();
523 static ExternalReference compute_output_frames_function();
524 static ExternalReference global_contexts_list();
525
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000526 // Static data in the keyed lookup cache.
527 static ExternalReference keyed_lookup_cache_keys();
528 static ExternalReference keyed_lookup_cache_field_offsets();
529
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000530 // Static variable Factory::the_hole_value.location()
531 static ExternalReference the_hole_value_location();
532
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000533 // Static variable Factory::arguments_marker.location()
534 static ExternalReference arguments_marker_location();
535
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000536 // Static variable Heap::roots_address()
537 static ExternalReference roots_address();
538
ager@chromium.org6f10e412009-02-13 10:11:16 +0000539 // Static variable StackGuard::address_of_jslimit()
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000540 static ExternalReference address_of_stack_limit();
541
542 // Static variable StackGuard::address_of_real_jslimit()
543 static ExternalReference address_of_real_stack_limit();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000544
ager@chromium.org32912102009-01-16 10:38:43 +0000545 // Static variable RegExpStack::limit_address()
546 static ExternalReference address_of_regexp_stack_limit();
547
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000548 // Static variables for RegExp.
549 static ExternalReference address_of_static_offsets_vector();
550 static ExternalReference address_of_regexp_stack_memory_address();
551 static ExternalReference address_of_regexp_stack_memory_size();
552
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000553 // Static variable Heap::NewSpaceStart()
554 static ExternalReference new_space_start();
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000555 static ExternalReference new_space_mask();
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000556 static ExternalReference heap_always_allocate_scope_depth();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000557
558 // Used for fast allocation in generated code.
559 static ExternalReference new_space_allocation_top_address();
560 static ExternalReference new_space_allocation_limit_address();
561
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000562 static ExternalReference double_fp_operation(Token::Value operation);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000563 static ExternalReference compare_doubles();
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000564 static ExternalReference power_double_double_function();
565 static ExternalReference power_double_int_function();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000566
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000567 static ExternalReference handle_scope_next_address();
568 static ExternalReference handle_scope_limit_address();
lrn@chromium.org303ada72010-10-27 09:33:13 +0000569 static ExternalReference handle_scope_level_address();
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000570
571 static ExternalReference scheduled_exception_address();
572
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000573 // Static variables containing common double constants.
574 static ExternalReference address_of_min_int();
575 static ExternalReference address_of_one_half();
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000576 static ExternalReference address_of_minus_zero();
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000577 static ExternalReference address_of_negative_infinity();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000578
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000579 Address address() const {return reinterpret_cast<Address>(address_);}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000580
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000581#ifdef ENABLE_DEBUGGER_SUPPORT
582 // Function Debug::Break()
583 static ExternalReference debug_break();
584
585 // Used to check if single stepping is enabled in generated code.
586 static ExternalReference debug_step_in_fp_address();
587#endif
588
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000589#ifndef V8_INTERPRETED_REGEXP
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000590 // C functions called from RegExp generated code.
591
592 // Function NativeRegExpMacroAssembler::CaseInsensitiveCompareUC16()
593 static ExternalReference re_case_insensitive_compare_uc16();
594
595 // Function RegExpMacroAssembler*::CheckStackGuardState()
596 static ExternalReference re_check_stack_guard_state();
597
598 // Function NativeRegExpMacroAssembler::GrowStack()
599 static ExternalReference re_grow_stack();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000600
601 // byte NativeRegExpMacroAssembler::word_character_bitmap
602 static ExternalReference re_word_character_map();
603
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000604#endif
605
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000606 // This lets you register a function that rewrites all external references.
607 // Used by the ARM simulator to catch calls to external references.
608 static void set_redirector(ExternalReferenceRedirector* redirector) {
609 ASSERT(redirector_ == NULL); // We can't stack them.
610 redirector_ = redirector;
611 }
612
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000613 private:
614 explicit ExternalReference(void* address)
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000615 : address_(address) {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000616
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000617 static ExternalReferenceRedirector* redirector_;
618
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000619 static void* Redirect(void* address,
620 Type type = ExternalReference::BUILTIN_CALL) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000621 if (redirector_ == NULL) return address;
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000622 void* answer = (*redirector_)(address, type);
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000623 return answer;
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000624 }
625
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000626 static void* Redirect(Address address_arg,
627 Type type = ExternalReference::BUILTIN_CALL) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000628 void* address = reinterpret_cast<void*>(address_arg);
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000629 void* answer = (redirector_ == NULL) ?
630 address :
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000631 (*redirector_)(address, type);
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000632 return answer;
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000633 }
634
635 void* address_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000636};
637
638
639// -----------------------------------------------------------------------------
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000640// Position recording support
641
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000642struct PositionState {
643 PositionState() : current_position(RelocInfo::kNoPosition),
644 written_position(RelocInfo::kNoPosition),
645 current_statement_position(RelocInfo::kNoPosition),
646 written_statement_position(RelocInfo::kNoPosition) {}
647
648 int current_position;
649 int written_position;
650
651 int current_statement_position;
652 int written_statement_position;
653};
654
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000655
656class PositionsRecorder BASE_EMBEDDED {
657 public:
658 explicit PositionsRecorder(Assembler* assembler)
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000659 : assembler_(assembler) {
660#ifdef ENABLE_GDB_JIT_INTERFACE
661 gdbjit_lineinfo_ = NULL;
662#endif
663 }
664
665#ifdef ENABLE_GDB_JIT_INTERFACE
666 ~PositionsRecorder() {
667 delete gdbjit_lineinfo_;
668 }
669
670 void StartGDBJITLineInfoRecording() {
671 if (FLAG_gdbjit) {
672 gdbjit_lineinfo_ = new GDBJITLineInfo();
673 }
674 }
675
676 GDBJITLineInfo* DetachGDBJITLineInfo() {
677 GDBJITLineInfo* lineinfo = gdbjit_lineinfo_;
678 gdbjit_lineinfo_ = NULL; // To prevent deallocation in destructor.
679 return lineinfo;
680 }
681#endif
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000682
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000683 // Set current position to pos.
684 void RecordPosition(int pos);
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000685
686 // Set current statement position to pos.
687 void RecordStatementPosition(int pos);
688
689 // Write recorded positions to relocation information.
690 bool WriteRecordedPositions();
691
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000692 int current_position() const { return state_.current_position; }
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000693
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000694 int current_statement_position() const {
695 return state_.current_statement_position;
696 }
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000697
698 private:
699 Assembler* assembler_;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000700 PositionState state_;
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000701#ifdef ENABLE_GDB_JIT_INTERFACE
702 GDBJITLineInfo* gdbjit_lineinfo_;
703#endif
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000704
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000705 friend class PreservePositionScope;
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000706
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000707 DISALLOW_COPY_AND_ASSIGN(PositionsRecorder);
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000708};
709
710
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000711class PreservePositionScope BASE_EMBEDDED {
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000712 public:
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000713 explicit PreservePositionScope(PositionsRecorder* positions_recorder)
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000714 : positions_recorder_(positions_recorder),
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000715 saved_state_(positions_recorder->state_) {}
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000716
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000717 ~PreservePositionScope() {
718 positions_recorder_->state_ = saved_state_;
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000719 }
720
721 private:
722 PositionsRecorder* positions_recorder_;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000723 const PositionState saved_state_;
724
725 DISALLOW_COPY_AND_ASSIGN(PreservePositionScope);
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000726};
727
728
729// -----------------------------------------------------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000730// Utility functions
731
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000732static inline bool is_intn(int x, int n) {
733 return -(1 << (n-1)) <= x && x < (1 << (n-1));
734}
735
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000736static inline bool is_int8(int x) { return is_intn(x, 8); }
ager@chromium.org5c838252010-02-19 08:53:10 +0000737static inline bool is_int16(int x) { return is_intn(x, 16); }
738static inline bool is_int18(int x) { return is_intn(x, 18); }
739static inline bool is_int24(int x) { return is_intn(x, 24); }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000740
741static inline bool is_uintn(int x, int n) {
742 return (x & -(1 << n)) == 0;
743}
744
ager@chromium.orge2902be2009-06-08 12:21:35 +0000745static inline bool is_uint2(int x) { return is_uintn(x, 2); }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000746static inline bool is_uint3(int x) { return is_uintn(x, 3); }
747static inline bool is_uint4(int x) { return is_uintn(x, 4); }
748static inline bool is_uint5(int x) { return is_uintn(x, 5); }
ager@chromium.orge2902be2009-06-08 12:21:35 +0000749static inline bool is_uint6(int x) { return is_uintn(x, 6); }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000750static inline bool is_uint8(int x) { return is_uintn(x, 8); }
ager@chromium.org5c838252010-02-19 08:53:10 +0000751static inline bool is_uint10(int x) { return is_uintn(x, 10); }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000752static inline bool is_uint12(int x) { return is_uintn(x, 12); }
753static inline bool is_uint16(int x) { return is_uintn(x, 16); }
754static inline bool is_uint24(int x) { return is_uintn(x, 24); }
ager@chromium.org5c838252010-02-19 08:53:10 +0000755static inline bool is_uint26(int x) { return is_uintn(x, 26); }
756static inline bool is_uint28(int x) { return is_uintn(x, 28); }
757
758static inline int NumberOfBitsSet(uint32_t x) {
759 unsigned int num_bits_set;
760 for (num_bits_set = 0; x; x >>= 1) {
761 num_bits_set += x & 1;
762 }
763 return num_bits_set;
764}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000765
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000766// Computes pow(x, y) with the special cases in the spec for Math.pow.
767double power_double_int(double x, int y);
768double power_double_double(double x, double y);
769
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000770} } // namespace v8::internal
771
772#endif // V8_ASSEMBLER_H_