blob: 395bbd5480f0b59506dcfba3319266931cd5c999 [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 Murdochb8e0da22011-05-16 14:20:40 +010038#include "gdb-jit.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000039#include "runtime.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000040#include "token.h"
41
42namespace v8 {
43namespace internal {
44
45
46// -----------------------------------------------------------------------------
Steve Block44f0eee2011-05-26 01:26:41 +010047// Platform independent assembler base class.
48
49class AssemblerBase: public Malloced {
50 public:
Steve Block053d10c2011-06-13 19:13:29 +010051 explicit AssemblerBase(Isolate* isolate);
Steve Block44f0eee2011-05-26 01:26:41 +010052
53 Isolate* isolate() const { return isolate_; }
Steve Block053d10c2011-06-13 19:13:29 +010054 int jit_cookie() { return jit_cookie_; }
Steve Block44f0eee2011-05-26 01:26:41 +010055
56 private:
57 Isolate* isolate_;
Steve Block053d10c2011-06-13 19:13:29 +010058 int jit_cookie_;
Steve Block44f0eee2011-05-26 01:26:41 +010059};
60
61// -----------------------------------------------------------------------------
Ben Murdochb0fe1622011-05-05 13:52:32 +010062// Common double constants.
63
64class DoubleConstant: public AllStatic {
65 public:
66 static const double min_int;
67 static const double one_half;
Ben Murdochb8e0da22011-05-16 14:20:40 +010068 static const double minus_zero;
Ben Murdochb0fe1622011-05-05 13:52:32 +010069 static const double negative_infinity;
Steve Block44f0eee2011-05-26 01:26:41 +010070 static const double nan;
Ben Murdochb0fe1622011-05-05 13:52:32 +010071};
72
73
74// -----------------------------------------------------------------------------
Steve Blocka7e24c12009-10-30 11:49:00 +000075// Labels represent pc locations; they are typically jump or call targets.
76// After declaration, a label can be freely used to denote known or (yet)
77// unknown pc location. Assembler::bind() is used to bind a label to the
78// current pc. A label can be bound only once.
79
80class Label BASE_EMBEDDED {
81 public:
82 INLINE(Label()) { Unuse(); }
83 INLINE(~Label()) { ASSERT(!is_linked()); }
84
85 INLINE(void Unuse()) { pos_ = 0; }
86
Kristian Monsen0d5e1162010-09-30 15:31:59 +010087 INLINE(bool is_bound() const) { return pos_ < 0; }
Steve Blocka7e24c12009-10-30 11:49:00 +000088 INLINE(bool is_unused() const) { return pos_ == 0; }
89 INLINE(bool is_linked() const) { return pos_ > 0; }
90
91 // Returns the position of bound or linked labels. Cannot be used
92 // for unused labels.
93 int pos() const;
94
95 private:
96 // pos_ encodes both the binding state (via its sign)
97 // and the binding position (via its value) of a label.
98 //
99 // pos_ < 0 bound label, pos() returns the jump target position
100 // pos_ == 0 unused label
101 // pos_ > 0 linked label, pos() returns the last reference position
102 int pos_;
103
104 void bind_to(int pos) {
105 pos_ = -pos - 1;
106 ASSERT(is_bound());
107 }
108 void link_to(int pos) {
109 pos_ = pos + 1;
110 ASSERT(is_linked());
111 }
112
113 friend class Assembler;
114 friend class RegexpAssembler;
115 friend class Displacement;
Steve Blocka7e24c12009-10-30 11:49:00 +0000116 friend class RegExpMacroAssemblerIrregexp;
117};
118
119
120// -----------------------------------------------------------------------------
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100121// NearLabels are labels used for short jumps (in Intel jargon).
122// NearLabels should be used if it can be guaranteed that the jump range is
123// within -128 to +127. We already use short jumps when jumping backwards,
124// so using a NearLabel will only have performance impact if used for forward
125// jumps.
126class NearLabel BASE_EMBEDDED {
127 public:
128 NearLabel() { Unuse(); }
129 ~NearLabel() { ASSERT(!is_linked()); }
130
131 void Unuse() {
132 pos_ = -1;
133 unresolved_branches_ = 0;
134#ifdef DEBUG
135 for (int i = 0; i < kMaxUnresolvedBranches; i++) {
136 unresolved_positions_[i] = -1;
137 }
138#endif
139 }
140
141 int pos() {
142 ASSERT(is_bound());
143 return pos_;
144 }
145
146 bool is_bound() { return pos_ >= 0; }
147 bool is_linked() { return !is_bound() && unresolved_branches_ > 0; }
148 bool is_unused() { return !is_bound() && unresolved_branches_ == 0; }
149
150 void bind_to(int position) {
151 ASSERT(!is_bound());
152 pos_ = position;
153 }
154
155 void link_to(int position) {
156 ASSERT(!is_bound());
157 ASSERT(unresolved_branches_ < kMaxUnresolvedBranches);
158 unresolved_positions_[unresolved_branches_++] = position;
159 }
160
161 private:
162 static const int kMaxUnresolvedBranches = 8;
163 int pos_;
164 int unresolved_branches_;
165 int unresolved_positions_[kMaxUnresolvedBranches];
166
167 friend class Assembler;
168};
169
170
171// -----------------------------------------------------------------------------
Steve Blocka7e24c12009-10-30 11:49:00 +0000172// Relocation information
173
174
175// Relocation information consists of the address (pc) of the datum
176// to which the relocation information applies, the relocation mode
177// (rmode), and an optional data field. The relocation mode may be
178// "descriptive" and not indicate a need for relocation, but simply
179// describe a property of the datum. Such rmodes are useful for GC
180// and nice disassembly output.
181
182class RelocInfo BASE_EMBEDDED {
183 public:
184 // The constant kNoPosition is used with the collecting of source positions
185 // in the relocation information. Two types of source positions are collected
186 // "position" (RelocMode position) and "statement position" (RelocMode
187 // statement_position). The "position" is collected at places in the source
188 // code which are of interest when making stack traces to pin-point the source
189 // location of a stack frame as close as possible. The "statement position" is
190 // collected at the beginning at each statement, and is used to indicate
191 // possible break locations. kNoPosition is used to indicate an
192 // invalid/uninitialized position value.
193 static const int kNoPosition = -1;
194
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100195 // This string is used to add padding comments to the reloc info in cases
196 // where we are not sure to have enough space for patching in during
197 // lazy deoptimization. This is the case if we have indirect calls for which
198 // we do not normally record relocation info.
199 static const char* kFillerCommentString;
200
201 // The minimum size of a comment is equal to three bytes for the extra tagged
202 // pc + the tag for the data, and kPointerSize for the actual pointer to the
203 // comment.
204 static const int kMinRelocCommentSize = 3 + kPointerSize;
205
206 // The maximum size for a call instruction including pc-jump.
207 static const int kMaxCallSize = 6;
208
Steve Block44f0eee2011-05-26 01:26:41 +0100209 // The maximum pc delta that will use the short encoding.
210 static const int kMaxSmallPCDelta;
211
Steve Blocka7e24c12009-10-30 11:49:00 +0000212 enum Mode {
213 // Please note the order is important (see IsCodeTarget, IsGCRelocMode).
214 CONSTRUCT_CALL, // code target that is a call to a JavaScript constructor.
Steve Block1e0659c2011-05-24 12:43:12 +0100215 CODE_TARGET_CONTEXT, // Code target used for contextual loads and stores.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100216 DEBUG_BREAK, // Code target for the debugger statement.
217 CODE_TARGET, // Code target which is not any of the above.
Steve Blocka7e24c12009-10-30 11:49:00 +0000218 EMBEDDED_OBJECT,
Ben Murdochb0fe1622011-05-05 13:52:32 +0100219 GLOBAL_PROPERTY_CELL,
220
Steve Blocka7e24c12009-10-30 11:49:00 +0000221 // Everything after runtime_entry (inclusive) is not GC'ed.
222 RUNTIME_ENTRY,
223 JS_RETURN, // Marks start of the ExitJSFrame code.
224 COMMENT,
225 POSITION, // See comment for kNoPosition above.
226 STATEMENT_POSITION, // See comment for kNoPosition above.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100227 DEBUG_BREAK_SLOT, // Additional code inserted for debug break slot.
Steve Blocka7e24c12009-10-30 11:49:00 +0000228 EXTERNAL_REFERENCE, // The address of an external C++ function.
229 INTERNAL_REFERENCE, // An address inside the same function.
230
231 // add more as needed
232 // Pseudo-types
233 NUMBER_OF_MODES, // must be no greater than 14 - see RelocInfoWriter
234 NONE, // never recorded
235 LAST_CODE_ENUM = CODE_TARGET,
Steve Block1e0659c2011-05-24 12:43:12 +0100236 LAST_GCED_ENUM = GLOBAL_PROPERTY_CELL
Steve Blocka7e24c12009-10-30 11:49:00 +0000237 };
238
239
240 RelocInfo() {}
241 RelocInfo(byte* pc, Mode rmode, intptr_t data)
242 : pc_(pc), rmode_(rmode), data_(data) {
243 }
244
245 static inline bool IsConstructCall(Mode mode) {
246 return mode == CONSTRUCT_CALL;
247 }
248 static inline bool IsCodeTarget(Mode mode) {
249 return mode <= LAST_CODE_ENUM;
250 }
251 // Is the relocation mode affected by GC?
252 static inline bool IsGCRelocMode(Mode mode) {
253 return mode <= LAST_GCED_ENUM;
254 }
255 static inline bool IsJSReturn(Mode mode) {
256 return mode == JS_RETURN;
257 }
258 static inline bool IsComment(Mode mode) {
259 return mode == COMMENT;
260 }
261 static inline bool IsPosition(Mode mode) {
262 return mode == POSITION || mode == STATEMENT_POSITION;
263 }
264 static inline bool IsStatementPosition(Mode mode) {
265 return mode == STATEMENT_POSITION;
266 }
267 static inline bool IsExternalReference(Mode mode) {
268 return mode == EXTERNAL_REFERENCE;
269 }
270 static inline bool IsInternalReference(Mode mode) {
271 return mode == INTERNAL_REFERENCE;
272 }
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100273 static inline bool IsDebugBreakSlot(Mode mode) {
274 return mode == DEBUG_BREAK_SLOT;
275 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000276 static inline int ModeMask(Mode mode) { return 1 << mode; }
277
278 // Accessors
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100279 byte* pc() const { return pc_; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000280 void set_pc(byte* pc) { pc_ = pc; }
281 Mode rmode() const { return rmode_; }
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100282 intptr_t data() const { return data_; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000283
284 // Apply a relocation by delta bytes
285 INLINE(void apply(intptr_t delta));
286
Leon Clarkef7060e22010-06-03 12:02:55 +0100287 // Is the pointer this relocation info refers to coded like a plain pointer
288 // or is it strange in some way (eg relative or patched into a series of
289 // instructions).
290 bool IsCodedSpecially();
291
Steve Blocka7e24c12009-10-30 11:49:00 +0000292 // Read/modify the code target in the branch/call instruction
293 // this relocation applies to;
294 // can only be called if IsCodeTarget(rmode_) || rmode_ == RUNTIME_ENTRY
295 INLINE(Address target_address());
296 INLINE(void set_target_address(Address target));
297 INLINE(Object* target_object());
Steve Block3ce2e202009-11-05 08:53:23 +0000298 INLINE(Handle<Object> target_object_handle(Assembler* origin));
Steve Blocka7e24c12009-10-30 11:49:00 +0000299 INLINE(Object** target_object_address());
300 INLINE(void set_target_object(Object* target));
Ben Murdochb0fe1622011-05-05 13:52:32 +0100301 INLINE(JSGlobalPropertyCell* target_cell());
302 INLINE(Handle<JSGlobalPropertyCell> target_cell_handle());
303 INLINE(void set_target_cell(JSGlobalPropertyCell* cell));
304
Steve Blocka7e24c12009-10-30 11:49:00 +0000305
Leon Clarkef7060e22010-06-03 12:02:55 +0100306 // Read the address of the word containing the target_address in an
307 // instruction stream. What this means exactly is architecture-independent.
308 // The only architecture-independent user of this function is the serializer.
309 // The serializer uses it to find out how many raw bytes of instruction to
310 // output before the next target. Architecture-independent code shouldn't
311 // dereference the pointer it gets back from this.
Steve Blocka7e24c12009-10-30 11:49:00 +0000312 INLINE(Address target_address_address());
Leon Clarkef7060e22010-06-03 12:02:55 +0100313 // This indicates how much space a target takes up when deserializing a code
314 // stream. For most architectures this is just the size of a pointer. For
315 // an instruction like movw/movt where the target bits are mixed into the
316 // instruction bits the size of the target will be zero, indicating that the
317 // serializer should not step forwards in memory after a target is resolved
318 // and written. In this case the target_address_address function above
319 // should return the end of the instructions to be patched, allowing the
320 // deserializer to deserialize the instructions as raw bytes and put them in
321 // place, ready to be patched with the target.
322 INLINE(int target_address_size());
Steve Blocka7e24c12009-10-30 11:49:00 +0000323
324 // Read/modify the reference in the instruction this relocation
325 // applies to; can only be called if rmode_ is external_reference
326 INLINE(Address* target_reference_address());
327
328 // Read/modify the address of a call instruction. This is used to relocate
329 // the break points where straight-line code is patched with a call
330 // instruction.
331 INLINE(Address call_address());
332 INLINE(void set_call_address(Address target));
333 INLINE(Object* call_object());
Steve Blocka7e24c12009-10-30 11:49:00 +0000334 INLINE(void set_call_object(Object* target));
Ben Murdochbb769b22010-08-11 14:56:33 +0100335 INLINE(Object** call_object_address());
Steve Blocka7e24c12009-10-30 11:49:00 +0000336
Steve Block44f0eee2011-05-26 01:26:41 +0100337 template<typename StaticVisitor> inline void Visit(Heap* heap);
Leon Clarkef7060e22010-06-03 12:02:55 +0100338 inline void Visit(ObjectVisitor* v);
339
Steve Blocka7e24c12009-10-30 11:49:00 +0000340 // Patch the code with some other code.
341 void PatchCode(byte* instructions, int instruction_count);
342
343 // Patch the code with a call.
344 void PatchCodeWithCall(Address target, int guard_bytes);
Steve Block3ce2e202009-11-05 08:53:23 +0000345
346 // Check whether this return sequence has been patched
347 // with a call to the debugger.
348 INLINE(bool IsPatchedReturnSequence());
Steve Blocka7e24c12009-10-30 11:49:00 +0000349
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100350 // Check whether this debug break slot has been patched with a call to the
351 // debugger.
352 INLINE(bool IsPatchedDebugBreakSlotSequence());
353
Steve Blocka7e24c12009-10-30 11:49:00 +0000354#ifdef ENABLE_DISASSEMBLER
355 // Printing
356 static const char* RelocModeName(Mode rmode);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100357 void Print(FILE* out);
Steve Blocka7e24c12009-10-30 11:49:00 +0000358#endif // ENABLE_DISASSEMBLER
359#ifdef DEBUG
360 // Debugging
361 void Verify();
362#endif
363
364 static const int kCodeTargetMask = (1 << (LAST_CODE_ENUM + 1)) - 1;
365 static const int kPositionMask = 1 << POSITION | 1 << STATEMENT_POSITION;
366 static const int kDebugMask = kPositionMask | 1 << COMMENT;
367 static const int kApplyMask; // Modes affected by apply. Depends on arch.
368
369 private:
370 // On ARM, note that pc_ is the address of the constant pool entry
371 // to be relocated and not the address of the instruction
372 // referencing the constant pool entry (except when rmode_ ==
373 // comment).
374 byte* pc_;
375 Mode rmode_;
376 intptr_t data_;
377 friend class RelocIterator;
378};
379
380
381// RelocInfoWriter serializes a stream of relocation info. It writes towards
382// lower addresses.
383class RelocInfoWriter BASE_EMBEDDED {
384 public:
385 RelocInfoWriter() : pos_(NULL), last_pc_(NULL), last_data_(0) {}
386 RelocInfoWriter(byte* pos, byte* pc) : pos_(pos), last_pc_(pc),
387 last_data_(0) {}
388
389 byte* pos() const { return pos_; }
390 byte* last_pc() const { return last_pc_; }
391
392 void Write(const RelocInfo* rinfo);
393
394 // Update the state of the stream after reloc info buffer
395 // and/or code is moved while the stream is active.
396 void Reposition(byte* pos, byte* pc) {
397 pos_ = pos;
398 last_pc_ = pc;
399 }
400
401 // Max size (bytes) of a written RelocInfo. Longest encoding is
402 // ExtraTag, VariableLengthPCJump, ExtraTag, pc_delta, ExtraTag, data_delta.
403 // On ia32 and arm this is 1 + 4 + 1 + 1 + 1 + 4 = 12.
404 // On x64 this is 1 + 4 + 1 + 1 + 1 + 8 == 16;
405 // Here we use the maximum of the two.
406 static const int kMaxSize = 16;
407
408 private:
409 inline uint32_t WriteVariableLengthPCJump(uint32_t pc_delta);
410 inline void WriteTaggedPC(uint32_t pc_delta, int tag);
411 inline void WriteExtraTaggedPC(uint32_t pc_delta, int extra_tag);
412 inline void WriteExtraTaggedData(intptr_t data_delta, int top_tag);
413 inline void WriteTaggedData(intptr_t data_delta, int tag);
414 inline void WriteExtraTag(int extra_tag, int top_tag);
415
416 byte* pos_;
417 byte* last_pc_;
418 intptr_t last_data_;
419 DISALLOW_COPY_AND_ASSIGN(RelocInfoWriter);
420};
421
422
423// A RelocIterator iterates over relocation information.
424// Typical use:
425//
426// for (RelocIterator it(code); !it.done(); it.next()) {
427// // do something with it.rinfo() here
428// }
429//
430// A mask can be specified to skip unwanted modes.
431class RelocIterator: public Malloced {
432 public:
433 // Create a new iterator positioned at
434 // the beginning of the reloc info.
435 // Relocation information with mode k is included in the
436 // iteration iff bit k of mode_mask is set.
437 explicit RelocIterator(Code* code, int mode_mask = -1);
438 explicit RelocIterator(const CodeDesc& desc, int mode_mask = -1);
439
440 // Iteration
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100441 bool done() const { return done_; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000442 void next();
443
444 // Return pointer valid until next next().
445 RelocInfo* rinfo() {
446 ASSERT(!done());
447 return &rinfo_;
448 }
449
450 private:
451 // Advance* moves the position before/after reading.
452 // *Read* reads from current byte(s) into rinfo_.
453 // *Get* just reads and returns info on current byte.
454 void Advance(int bytes = 1) { pos_ -= bytes; }
455 int AdvanceGetTag();
456 int GetExtraTag();
457 int GetTopTag();
458 void ReadTaggedPC();
459 void AdvanceReadPC();
460 void AdvanceReadData();
461 void AdvanceReadVariableLengthPCJump();
462 int GetPositionTypeTag();
463 void ReadTaggedData();
464
465 static RelocInfo::Mode DebugInfoModeFromTag(int tag);
466
467 // If the given mode is wanted, set it in rinfo_ and return true.
468 // Else return false. Used for efficiently skipping unwanted modes.
469 bool SetMode(RelocInfo::Mode mode) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100470 return (mode_mask_ & (1 << mode)) ? (rinfo_.rmode_ = mode, true) : false;
Steve Blocka7e24c12009-10-30 11:49:00 +0000471 }
472
473 byte* pos_;
474 byte* end_;
475 RelocInfo rinfo_;
476 bool done_;
477 int mode_mask_;
478 DISALLOW_COPY_AND_ASSIGN(RelocIterator);
479};
480
481
482//------------------------------------------------------------------------------
483// External function
484
485//----------------------------------------------------------------------------
486class IC_Utility;
487class SCTableReference;
488#ifdef ENABLE_DEBUGGER_SUPPORT
489class Debug_Address;
490#endif
491
492
Steve Blocka7e24c12009-10-30 11:49:00 +0000493// An ExternalReference represents a C++ address used in the generated
494// code. All references to C++ functions and variables must be encapsulated in
495// an ExternalReference instance. This is done in order to track the origin of
496// all external references in the code so that they can be bound to the correct
497// addresses when deserializing a heap.
498class ExternalReference BASE_EMBEDDED {
499 public:
Steve Block1e0659c2011-05-24 12:43:12 +0100500 // Used in the simulator to support different native api calls.
Steve Block1e0659c2011-05-24 12:43:12 +0100501 enum Type {
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100502 // Builtin call.
503 // MaybeObject* f(v8::internal::Arguments).
Steve Block1e0659c2011-05-24 12:43:12 +0100504 BUILTIN_CALL, // default
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100505
506 // Builtin call that returns floating point.
507 // double f(double, double).
Steve Block1e0659c2011-05-24 12:43:12 +0100508 FP_RETURN_CALL,
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100509
510 // Direct call to API function callback.
511 // Handle<Value> f(v8::Arguments&)
512 DIRECT_API_CALL,
513
514 // Direct call to accessor getter callback.
515 // Handle<value> f(Local<String> property, AccessorInfo& info)
516 DIRECT_GETTER_CALL
Steve Block1e0659c2011-05-24 12:43:12 +0100517 };
518
519 typedef void* ExternalReferenceRedirector(void* original, Type type);
520
Steve Block44f0eee2011-05-26 01:26:41 +0100521 ExternalReference(Builtins::CFunctionId id, Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000522
Steve Block44f0eee2011-05-26 01:26:41 +0100523 ExternalReference(ApiFunction* ptr, Type type, Isolate* isolate);
Steve Blockd0582a62009-12-15 09:54:21 +0000524
Steve Block44f0eee2011-05-26 01:26:41 +0100525 ExternalReference(Builtins::Name name, Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000526
Steve Block44f0eee2011-05-26 01:26:41 +0100527 ExternalReference(Runtime::FunctionId id, Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000528
Steve Block44f0eee2011-05-26 01:26:41 +0100529 ExternalReference(const Runtime::Function* f, Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000530
Steve Block44f0eee2011-05-26 01:26:41 +0100531 ExternalReference(const IC_Utility& ic_utility, Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000532
533#ifdef ENABLE_DEBUGGER_SUPPORT
Steve Block44f0eee2011-05-26 01:26:41 +0100534 ExternalReference(const Debug_Address& debug_address, Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000535#endif
536
537 explicit ExternalReference(StatsCounter* counter);
538
Steve Block44f0eee2011-05-26 01:26:41 +0100539 ExternalReference(Isolate::AddressId id, Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000540
541 explicit ExternalReference(const SCTableReference& table_ref);
542
Steve Block44f0eee2011-05-26 01:26:41 +0100543 // Isolate::Current() as an external reference.
544 static ExternalReference isolate_address();
545
Steve Blocka7e24c12009-10-30 11:49:00 +0000546 // One-of-a-kind references. These references are not part of a general
547 // pattern. This means that they have to be added to the
548 // ExternalReferenceTable in serialize.cc manually.
549
Steve Block44f0eee2011-05-26 01:26:41 +0100550 static ExternalReference perform_gc_function(Isolate* isolate);
551 static ExternalReference fill_heap_number_with_random_function(
552 Isolate* isolate);
553 static ExternalReference random_uint32_function(Isolate* isolate);
554 static ExternalReference transcendental_cache_array_address(Isolate* isolate);
555 static ExternalReference delete_handle_scope_extensions(Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000556
Ben Murdochb0fe1622011-05-05 13:52:32 +0100557 // Deoptimization support.
Steve Block44f0eee2011-05-26 01:26:41 +0100558 static ExternalReference new_deoptimizer_function(Isolate* isolate);
559 static ExternalReference compute_output_frames_function(Isolate* isolate);
560 static ExternalReference global_contexts_list(Isolate* isolate);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100561
Leon Clarkee46be812010-01-19 14:06:41 +0000562 // Static data in the keyed lookup cache.
Steve Block44f0eee2011-05-26 01:26:41 +0100563 static ExternalReference keyed_lookup_cache_keys(Isolate* isolate);
564 static ExternalReference keyed_lookup_cache_field_offsets(Isolate* isolate);
Leon Clarkee46be812010-01-19 14:06:41 +0000565
Steve Blocka7e24c12009-10-30 11:49:00 +0000566 // Static variable Factory::the_hole_value.location()
Steve Block44f0eee2011-05-26 01:26:41 +0100567 static ExternalReference the_hole_value_location(Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000568
Ben Murdoch086aeea2011-05-13 15:57:08 +0100569 // Static variable Factory::arguments_marker.location()
Steve Block44f0eee2011-05-26 01:26:41 +0100570 static ExternalReference arguments_marker_location(Isolate* isolate);
Ben Murdoch086aeea2011-05-13 15:57:08 +0100571
Steve Blocka7e24c12009-10-30 11:49:00 +0000572 // Static variable Heap::roots_address()
Steve Block44f0eee2011-05-26 01:26:41 +0100573 static ExternalReference roots_address(Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000574
575 // Static variable StackGuard::address_of_jslimit()
Steve Block44f0eee2011-05-26 01:26:41 +0100576 static ExternalReference address_of_stack_limit(Isolate* isolate);
Steve Blockd0582a62009-12-15 09:54:21 +0000577
578 // Static variable StackGuard::address_of_real_jslimit()
Steve Block44f0eee2011-05-26 01:26:41 +0100579 static ExternalReference address_of_real_stack_limit(Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000580
581 // Static variable RegExpStack::limit_address()
Steve Block44f0eee2011-05-26 01:26:41 +0100582 static ExternalReference address_of_regexp_stack_limit(Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000583
Leon Clarkee46be812010-01-19 14:06:41 +0000584 // Static variables for RegExp.
Steve Block44f0eee2011-05-26 01:26:41 +0100585 static ExternalReference address_of_static_offsets_vector(Isolate* isolate);
586 static ExternalReference address_of_regexp_stack_memory_address(
587 Isolate* isolate);
588 static ExternalReference address_of_regexp_stack_memory_size(
589 Isolate* isolate);
Leon Clarkee46be812010-01-19 14:06:41 +0000590
Steve Blocka7e24c12009-10-30 11:49:00 +0000591 // Static variable Heap::NewSpaceStart()
Steve Block44f0eee2011-05-26 01:26:41 +0100592 static ExternalReference new_space_start(Isolate* isolate);
593 static ExternalReference new_space_mask(Isolate* isolate);
594 static ExternalReference heap_always_allocate_scope_depth(Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000595
596 // Used for fast allocation in generated code.
Steve Block44f0eee2011-05-26 01:26:41 +0100597 static ExternalReference new_space_allocation_top_address(Isolate* isolate);
598 static ExternalReference new_space_allocation_limit_address(Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000599
Steve Block44f0eee2011-05-26 01:26:41 +0100600 static ExternalReference double_fp_operation(Token::Value operation,
601 Isolate* isolate);
602 static ExternalReference compare_doubles(Isolate* isolate);
603 static ExternalReference power_double_double_function(Isolate* isolate);
604 static ExternalReference power_double_int_function(Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000605
Steve Blockd0582a62009-12-15 09:54:21 +0000606 static ExternalReference handle_scope_next_address();
607 static ExternalReference handle_scope_limit_address();
John Reck59135872010-11-02 12:39:01 -0700608 static ExternalReference handle_scope_level_address();
Steve Blockd0582a62009-12-15 09:54:21 +0000609
Steve Block44f0eee2011-05-26 01:26:41 +0100610 static ExternalReference scheduled_exception_address(Isolate* isolate);
Steve Blockd0582a62009-12-15 09:54:21 +0000611
Ben Murdochb0fe1622011-05-05 13:52:32 +0100612 // Static variables containing common double constants.
613 static ExternalReference address_of_min_int();
614 static ExternalReference address_of_one_half();
Ben Murdochb8e0da22011-05-16 14:20:40 +0100615 static ExternalReference address_of_minus_zero();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100616 static ExternalReference address_of_negative_infinity();
Steve Block44f0eee2011-05-26 01:26:41 +0100617 static ExternalReference address_of_nan();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100618
Steve Block44f0eee2011-05-26 01:26:41 +0100619 static ExternalReference math_sin_double_function(Isolate* isolate);
620 static ExternalReference math_cos_double_function(Isolate* isolate);
621 static ExternalReference math_log_double_function(Isolate* isolate);
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100622
Steve Blocka7e24c12009-10-30 11:49:00 +0000623 Address address() const {return reinterpret_cast<Address>(address_);}
624
625#ifdef ENABLE_DEBUGGER_SUPPORT
626 // Function Debug::Break()
Steve Block44f0eee2011-05-26 01:26:41 +0100627 static ExternalReference debug_break(Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000628
629 // Used to check if single stepping is enabled in generated code.
Steve Block44f0eee2011-05-26 01:26:41 +0100630 static ExternalReference debug_step_in_fp_address(Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000631#endif
632
Steve Block6ded16b2010-05-10 14:33:55 +0100633#ifndef V8_INTERPRETED_REGEXP
Steve Blocka7e24c12009-10-30 11:49:00 +0000634 // C functions called from RegExp generated code.
635
636 // Function NativeRegExpMacroAssembler::CaseInsensitiveCompareUC16()
Steve Block44f0eee2011-05-26 01:26:41 +0100637 static ExternalReference re_case_insensitive_compare_uc16(Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000638
639 // Function RegExpMacroAssembler*::CheckStackGuardState()
Steve Block44f0eee2011-05-26 01:26:41 +0100640 static ExternalReference re_check_stack_guard_state(Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000641
642 // Function NativeRegExpMacroAssembler::GrowStack()
Steve Block44f0eee2011-05-26 01:26:41 +0100643 static ExternalReference re_grow_stack(Isolate* isolate);
Leon Clarkee46be812010-01-19 14:06:41 +0000644
645 // byte NativeRegExpMacroAssembler::word_character_bitmap
646 static ExternalReference re_word_character_map();
647
Steve Blocka7e24c12009-10-30 11:49:00 +0000648#endif
649
650 // This lets you register a function that rewrites all external references.
651 // Used by the ARM simulator to catch calls to external references.
652 static void set_redirector(ExternalReferenceRedirector* redirector) {
Steve Block44f0eee2011-05-26 01:26:41 +0100653 // We can't stack them.
654 ASSERT(Isolate::Current()->external_reference_redirector() == NULL);
655 Isolate::Current()->set_external_reference_redirector(
656 reinterpret_cast<ExternalReferenceRedirectorPointer*>(redirector));
Steve Blocka7e24c12009-10-30 11:49:00 +0000657 }
658
659 private:
660 explicit ExternalReference(void* address)
661 : address_(address) {}
662
Steve Block44f0eee2011-05-26 01:26:41 +0100663 static void* Redirect(Isolate* isolate,
664 void* address,
Steve Block1e0659c2011-05-24 12:43:12 +0100665 Type type = ExternalReference::BUILTIN_CALL) {
Steve Block44f0eee2011-05-26 01:26:41 +0100666 ExternalReferenceRedirector* redirector =
667 reinterpret_cast<ExternalReferenceRedirector*>(
668 isolate->external_reference_redirector());
669 if (redirector == NULL) return address;
670 void* answer = (*redirector)(address, type);
Steve Blockd0582a62009-12-15 09:54:21 +0000671 return answer;
Steve Blocka7e24c12009-10-30 11:49:00 +0000672 }
673
Steve Block44f0eee2011-05-26 01:26:41 +0100674 static void* Redirect(Isolate* isolate,
675 Address address_arg,
Steve Block1e0659c2011-05-24 12:43:12 +0100676 Type type = ExternalReference::BUILTIN_CALL) {
Steve Block44f0eee2011-05-26 01:26:41 +0100677 ExternalReferenceRedirector* redirector =
678 reinterpret_cast<ExternalReferenceRedirector*>(
679 isolate->external_reference_redirector());
Steve Blocka7e24c12009-10-30 11:49:00 +0000680 void* address = reinterpret_cast<void*>(address_arg);
Steve Block44f0eee2011-05-26 01:26:41 +0100681 void* answer = (redirector == NULL) ?
Steve Blockd0582a62009-12-15 09:54:21 +0000682 address :
Steve Block44f0eee2011-05-26 01:26:41 +0100683 (*redirector)(address, type);
Steve Blockd0582a62009-12-15 09:54:21 +0000684 return answer;
Steve Blocka7e24c12009-10-30 11:49:00 +0000685 }
686
687 void* address_;
688};
689
690
691// -----------------------------------------------------------------------------
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800692// Position recording support
693
Ben Murdochb0fe1622011-05-05 13:52:32 +0100694struct PositionState {
695 PositionState() : current_position(RelocInfo::kNoPosition),
696 written_position(RelocInfo::kNoPosition),
697 current_statement_position(RelocInfo::kNoPosition),
698 written_statement_position(RelocInfo::kNoPosition) {}
699
700 int current_position;
701 int written_position;
702
703 int current_statement_position;
704 int written_statement_position;
705};
706
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800707
708class PositionsRecorder BASE_EMBEDDED {
709 public:
710 explicit PositionsRecorder(Assembler* assembler)
Ben Murdochb8e0da22011-05-16 14:20:40 +0100711 : assembler_(assembler) {
712#ifdef ENABLE_GDB_JIT_INTERFACE
713 gdbjit_lineinfo_ = NULL;
714#endif
715 }
716
717#ifdef ENABLE_GDB_JIT_INTERFACE
718 ~PositionsRecorder() {
719 delete gdbjit_lineinfo_;
720 }
721
722 void StartGDBJITLineInfoRecording() {
723 if (FLAG_gdbjit) {
724 gdbjit_lineinfo_ = new GDBJITLineInfo();
725 }
726 }
727
728 GDBJITLineInfo* DetachGDBJITLineInfo() {
729 GDBJITLineInfo* lineinfo = gdbjit_lineinfo_;
730 gdbjit_lineinfo_ = NULL; // To prevent deallocation in destructor.
731 return lineinfo;
732 }
733#endif
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800734
Ben Murdochb0fe1622011-05-05 13:52:32 +0100735 // Set current position to pos.
736 void RecordPosition(int pos);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800737
738 // Set current statement position to pos.
739 void RecordStatementPosition(int pos);
740
741 // Write recorded positions to relocation information.
742 bool WriteRecordedPositions();
743
Ben Murdochb0fe1622011-05-05 13:52:32 +0100744 int current_position() const { return state_.current_position; }
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800745
Ben Murdochb0fe1622011-05-05 13:52:32 +0100746 int current_statement_position() const {
747 return state_.current_statement_position;
748 }
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800749
750 private:
751 Assembler* assembler_;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100752 PositionState state_;
Ben Murdochb8e0da22011-05-16 14:20:40 +0100753#ifdef ENABLE_GDB_JIT_INTERFACE
754 GDBJITLineInfo* gdbjit_lineinfo_;
755#endif
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800756
Ben Murdochb0fe1622011-05-05 13:52:32 +0100757 friend class PreservePositionScope;
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800758
Ben Murdochb0fe1622011-05-05 13:52:32 +0100759 DISALLOW_COPY_AND_ASSIGN(PositionsRecorder);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800760};
761
762
Ben Murdochb0fe1622011-05-05 13:52:32 +0100763class PreservePositionScope BASE_EMBEDDED {
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800764 public:
Ben Murdochb0fe1622011-05-05 13:52:32 +0100765 explicit PreservePositionScope(PositionsRecorder* positions_recorder)
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800766 : positions_recorder_(positions_recorder),
Ben Murdochb0fe1622011-05-05 13:52:32 +0100767 saved_state_(positions_recorder->state_) {}
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800768
Ben Murdochb0fe1622011-05-05 13:52:32 +0100769 ~PreservePositionScope() {
770 positions_recorder_->state_ = saved_state_;
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800771 }
772
773 private:
774 PositionsRecorder* positions_recorder_;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100775 const PositionState saved_state_;
776
777 DISALLOW_COPY_AND_ASSIGN(PreservePositionScope);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800778};
779
780
781// -----------------------------------------------------------------------------
Steve Blocka7e24c12009-10-30 11:49:00 +0000782// Utility functions
783
784static inline bool is_intn(int x, int n) {
785 return -(1 << (n-1)) <= x && x < (1 << (n-1));
786}
787
Steve Blocka7e24c12009-10-30 11:49:00 +0000788static inline bool is_int8(int x) { return is_intn(x, 8); }
Andrei Popescu31002712010-02-23 13:46:05 +0000789static inline bool is_int16(int x) { return is_intn(x, 16); }
790static inline bool is_int18(int x) { return is_intn(x, 18); }
791static inline bool is_int24(int x) { return is_intn(x, 24); }
Steve Blocka7e24c12009-10-30 11:49:00 +0000792
793static inline bool is_uintn(int x, int n) {
794 return (x & -(1 << n)) == 0;
795}
796
797static inline bool is_uint2(int x) { return is_uintn(x, 2); }
798static inline bool is_uint3(int x) { return is_uintn(x, 3); }
799static inline bool is_uint4(int x) { return is_uintn(x, 4); }
800static inline bool is_uint5(int x) { return is_uintn(x, 5); }
801static inline bool is_uint6(int x) { return is_uintn(x, 6); }
802static inline bool is_uint8(int x) { return is_uintn(x, 8); }
Andrei Popescu31002712010-02-23 13:46:05 +0000803static inline bool is_uint10(int x) { return is_uintn(x, 10); }
Steve Blocka7e24c12009-10-30 11:49:00 +0000804static inline bool is_uint12(int x) { return is_uintn(x, 12); }
805static inline bool is_uint16(int x) { return is_uintn(x, 16); }
806static inline bool is_uint24(int x) { return is_uintn(x, 24); }
Andrei Popescu31002712010-02-23 13:46:05 +0000807static inline bool is_uint26(int x) { return is_uintn(x, 26); }
808static inline bool is_uint28(int x) { return is_uintn(x, 28); }
809
810static inline int NumberOfBitsSet(uint32_t x) {
811 unsigned int num_bits_set;
812 for (num_bits_set = 0; x; x >>= 1) {
813 num_bits_set += x & 1;
814 }
815 return num_bits_set;
816}
Steve Blocka7e24c12009-10-30 11:49:00 +0000817
Ben Murdochb0fe1622011-05-05 13:52:32 +0100818// Computes pow(x, y) with the special cases in the spec for Math.pow.
819double power_double_int(double x, int y);
820double power_double_double(double x, double y);
821
Steve Blocka7e24c12009-10-30 11:49:00 +0000822} } // namespace v8::internal
823
824#endif // V8_ASSEMBLER_H_