blob: 62fe04d1c74750d0cf1924bf0980157199457a27 [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.
33// Copyright 2006-2009 the V8 project authors. All rights reserved.
34
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:
51 explicit AssemblerBase(Isolate* isolate) : isolate_(isolate) {}
52
53 Isolate* isolate() const { return isolate_; }
54
55 private:
56 Isolate* isolate_;
57};
58
59// -----------------------------------------------------------------------------
Ben Murdochb0fe1622011-05-05 13:52:32 +010060// Common double constants.
61
62class DoubleConstant: public AllStatic {
63 public:
64 static const double min_int;
65 static const double one_half;
Ben Murdochb8e0da22011-05-16 14:20:40 +010066 static const double minus_zero;
Ben Murdochb0fe1622011-05-05 13:52:32 +010067 static const double negative_infinity;
Steve Block44f0eee2011-05-26 01:26:41 +010068 static const double nan;
Ben Murdochb0fe1622011-05-05 13:52:32 +010069};
70
71
72// -----------------------------------------------------------------------------
Steve Blocka7e24c12009-10-30 11:49:00 +000073// Labels represent pc locations; they are typically jump or call targets.
74// After declaration, a label can be freely used to denote known or (yet)
75// unknown pc location. Assembler::bind() is used to bind a label to the
76// current pc. A label can be bound only once.
77
78class Label BASE_EMBEDDED {
79 public:
80 INLINE(Label()) { Unuse(); }
81 INLINE(~Label()) { ASSERT(!is_linked()); }
82
83 INLINE(void Unuse()) { pos_ = 0; }
84
Kristian Monsen0d5e1162010-09-30 15:31:59 +010085 INLINE(bool is_bound() const) { return pos_ < 0; }
Steve Blocka7e24c12009-10-30 11:49:00 +000086 INLINE(bool is_unused() const) { return pos_ == 0; }
87 INLINE(bool is_linked() const) { return pos_ > 0; }
88
89 // Returns the position of bound or linked labels. Cannot be used
90 // for unused labels.
91 int pos() const;
92
93 private:
94 // pos_ encodes both the binding state (via its sign)
95 // and the binding position (via its value) of a label.
96 //
97 // pos_ < 0 bound label, pos() returns the jump target position
98 // pos_ == 0 unused label
99 // pos_ > 0 linked label, pos() returns the last reference position
100 int pos_;
101
102 void bind_to(int pos) {
103 pos_ = -pos - 1;
104 ASSERT(is_bound());
105 }
106 void link_to(int pos) {
107 pos_ = pos + 1;
108 ASSERT(is_linked());
109 }
110
111 friend class Assembler;
112 friend class RegexpAssembler;
113 friend class Displacement;
114 friend class ShadowTarget;
115 friend class RegExpMacroAssemblerIrregexp;
116};
117
118
119// -----------------------------------------------------------------------------
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100120// NearLabels are labels used for short jumps (in Intel jargon).
121// NearLabels should be used if it can be guaranteed that the jump range is
122// within -128 to +127. We already use short jumps when jumping backwards,
123// so using a NearLabel will only have performance impact if used for forward
124// jumps.
125class NearLabel BASE_EMBEDDED {
126 public:
127 NearLabel() { Unuse(); }
128 ~NearLabel() { ASSERT(!is_linked()); }
129
130 void Unuse() {
131 pos_ = -1;
132 unresolved_branches_ = 0;
133#ifdef DEBUG
134 for (int i = 0; i < kMaxUnresolvedBranches; i++) {
135 unresolved_positions_[i] = -1;
136 }
137#endif
138 }
139
140 int pos() {
141 ASSERT(is_bound());
142 return pos_;
143 }
144
145 bool is_bound() { return pos_ >= 0; }
146 bool is_linked() { return !is_bound() && unresolved_branches_ > 0; }
147 bool is_unused() { return !is_bound() && unresolved_branches_ == 0; }
148
149 void bind_to(int position) {
150 ASSERT(!is_bound());
151 pos_ = position;
152 }
153
154 void link_to(int position) {
155 ASSERT(!is_bound());
156 ASSERT(unresolved_branches_ < kMaxUnresolvedBranches);
157 unresolved_positions_[unresolved_branches_++] = position;
158 }
159
160 private:
161 static const int kMaxUnresolvedBranches = 8;
162 int pos_;
163 int unresolved_branches_;
164 int unresolved_positions_[kMaxUnresolvedBranches];
165
166 friend class Assembler;
167};
168
169
170// -----------------------------------------------------------------------------
Steve Blocka7e24c12009-10-30 11:49:00 +0000171// Relocation information
172
173
174// Relocation information consists of the address (pc) of the datum
175// to which the relocation information applies, the relocation mode
176// (rmode), and an optional data field. The relocation mode may be
177// "descriptive" and not indicate a need for relocation, but simply
178// describe a property of the datum. Such rmodes are useful for GC
179// and nice disassembly output.
180
181class RelocInfo BASE_EMBEDDED {
182 public:
183 // The constant kNoPosition is used with the collecting of source positions
184 // in the relocation information. Two types of source positions are collected
185 // "position" (RelocMode position) and "statement position" (RelocMode
186 // statement_position). The "position" is collected at places in the source
187 // code which are of interest when making stack traces to pin-point the source
188 // location of a stack frame as close as possible. The "statement position" is
189 // collected at the beginning at each statement, and is used to indicate
190 // possible break locations. kNoPosition is used to indicate an
191 // invalid/uninitialized position value.
192 static const int kNoPosition = -1;
193
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100194 // This string is used to add padding comments to the reloc info in cases
195 // where we are not sure to have enough space for patching in during
196 // lazy deoptimization. This is the case if we have indirect calls for which
197 // we do not normally record relocation info.
198 static const char* kFillerCommentString;
199
200 // The minimum size of a comment is equal to three bytes for the extra tagged
201 // pc + the tag for the data, and kPointerSize for the actual pointer to the
202 // comment.
203 static const int kMinRelocCommentSize = 3 + kPointerSize;
204
205 // The maximum size for a call instruction including pc-jump.
206 static const int kMaxCallSize = 6;
207
Steve Block44f0eee2011-05-26 01:26:41 +0100208 // The maximum pc delta that will use the short encoding.
209 static const int kMaxSmallPCDelta;
210
Steve Blocka7e24c12009-10-30 11:49:00 +0000211 enum Mode {
212 // Please note the order is important (see IsCodeTarget, IsGCRelocMode).
213 CONSTRUCT_CALL, // code target that is a call to a JavaScript constructor.
Steve Block1e0659c2011-05-24 12:43:12 +0100214 CODE_TARGET_CONTEXT, // Code target used for contextual loads and stores.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100215 DEBUG_BREAK, // Code target for the debugger statement.
216 CODE_TARGET, // Code target which is not any of the above.
Steve Blocka7e24c12009-10-30 11:49:00 +0000217 EMBEDDED_OBJECT,
Ben Murdochb0fe1622011-05-05 13:52:32 +0100218 GLOBAL_PROPERTY_CELL,
219
Steve Blocka7e24c12009-10-30 11:49:00 +0000220 // Everything after runtime_entry (inclusive) is not GC'ed.
221 RUNTIME_ENTRY,
222 JS_RETURN, // Marks start of the ExitJSFrame code.
223 COMMENT,
224 POSITION, // See comment for kNoPosition above.
225 STATEMENT_POSITION, // See comment for kNoPosition above.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100226 DEBUG_BREAK_SLOT, // Additional code inserted for debug break slot.
Steve Blocka7e24c12009-10-30 11:49:00 +0000227 EXTERNAL_REFERENCE, // The address of an external C++ function.
228 INTERNAL_REFERENCE, // An address inside the same function.
229
230 // add more as needed
231 // Pseudo-types
232 NUMBER_OF_MODES, // must be no greater than 14 - see RelocInfoWriter
233 NONE, // never recorded
234 LAST_CODE_ENUM = CODE_TARGET,
Steve Block1e0659c2011-05-24 12:43:12 +0100235 LAST_GCED_ENUM = GLOBAL_PROPERTY_CELL
Steve Blocka7e24c12009-10-30 11:49:00 +0000236 };
237
238
239 RelocInfo() {}
240 RelocInfo(byte* pc, Mode rmode, intptr_t data)
241 : pc_(pc), rmode_(rmode), data_(data) {
242 }
243
244 static inline bool IsConstructCall(Mode mode) {
245 return mode == CONSTRUCT_CALL;
246 }
247 static inline bool IsCodeTarget(Mode mode) {
248 return mode <= LAST_CODE_ENUM;
249 }
250 // Is the relocation mode affected by GC?
251 static inline bool IsGCRelocMode(Mode mode) {
252 return mode <= LAST_GCED_ENUM;
253 }
254 static inline bool IsJSReturn(Mode mode) {
255 return mode == JS_RETURN;
256 }
257 static inline bool IsComment(Mode mode) {
258 return mode == COMMENT;
259 }
260 static inline bool IsPosition(Mode mode) {
261 return mode == POSITION || mode == STATEMENT_POSITION;
262 }
263 static inline bool IsStatementPosition(Mode mode) {
264 return mode == STATEMENT_POSITION;
265 }
266 static inline bool IsExternalReference(Mode mode) {
267 return mode == EXTERNAL_REFERENCE;
268 }
269 static inline bool IsInternalReference(Mode mode) {
270 return mode == INTERNAL_REFERENCE;
271 }
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100272 static inline bool IsDebugBreakSlot(Mode mode) {
273 return mode == DEBUG_BREAK_SLOT;
274 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000275 static inline int ModeMask(Mode mode) { return 1 << mode; }
276
277 // Accessors
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100278 byte* pc() const { return pc_; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000279 void set_pc(byte* pc) { pc_ = pc; }
280 Mode rmode() const { return rmode_; }
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100281 intptr_t data() const { return data_; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000282
283 // Apply a relocation by delta bytes
284 INLINE(void apply(intptr_t delta));
285
Leon Clarkef7060e22010-06-03 12:02:55 +0100286 // Is the pointer this relocation info refers to coded like a plain pointer
287 // or is it strange in some way (eg relative or patched into a series of
288 // instructions).
289 bool IsCodedSpecially();
290
Steve Blocka7e24c12009-10-30 11:49:00 +0000291 // Read/modify the code target in the branch/call instruction
292 // this relocation applies to;
293 // can only be called if IsCodeTarget(rmode_) || rmode_ == RUNTIME_ENTRY
294 INLINE(Address target_address());
295 INLINE(void set_target_address(Address target));
296 INLINE(Object* target_object());
Steve Block3ce2e202009-11-05 08:53:23 +0000297 INLINE(Handle<Object> target_object_handle(Assembler* origin));
Steve Blocka7e24c12009-10-30 11:49:00 +0000298 INLINE(Object** target_object_address());
299 INLINE(void set_target_object(Object* target));
Ben Murdochb0fe1622011-05-05 13:52:32 +0100300 INLINE(JSGlobalPropertyCell* target_cell());
301 INLINE(Handle<JSGlobalPropertyCell> target_cell_handle());
302 INLINE(void set_target_cell(JSGlobalPropertyCell* cell));
303
Steve Blocka7e24c12009-10-30 11:49:00 +0000304
Leon Clarkef7060e22010-06-03 12:02:55 +0100305 // Read the address of the word containing the target_address in an
306 // instruction stream. What this means exactly is architecture-independent.
307 // The only architecture-independent user of this function is the serializer.
308 // The serializer uses it to find out how many raw bytes of instruction to
309 // output before the next target. Architecture-independent code shouldn't
310 // dereference the pointer it gets back from this.
Steve Blocka7e24c12009-10-30 11:49:00 +0000311 INLINE(Address target_address_address());
Leon Clarkef7060e22010-06-03 12:02:55 +0100312 // This indicates how much space a target takes up when deserializing a code
313 // stream. For most architectures this is just the size of a pointer. For
314 // an instruction like movw/movt where the target bits are mixed into the
315 // instruction bits the size of the target will be zero, indicating that the
316 // serializer should not step forwards in memory after a target is resolved
317 // and written. In this case the target_address_address function above
318 // should return the end of the instructions to be patched, allowing the
319 // deserializer to deserialize the instructions as raw bytes and put them in
320 // place, ready to be patched with the target.
321 INLINE(int target_address_size());
Steve Blocka7e24c12009-10-30 11:49:00 +0000322
323 // Read/modify the reference in the instruction this relocation
324 // applies to; can only be called if rmode_ is external_reference
325 INLINE(Address* target_reference_address());
326
327 // Read/modify the address of a call instruction. This is used to relocate
328 // the break points where straight-line code is patched with a call
329 // instruction.
330 INLINE(Address call_address());
331 INLINE(void set_call_address(Address target));
332 INLINE(Object* call_object());
Steve Blocka7e24c12009-10-30 11:49:00 +0000333 INLINE(void set_call_object(Object* target));
Ben Murdochbb769b22010-08-11 14:56:33 +0100334 INLINE(Object** call_object_address());
Steve Blocka7e24c12009-10-30 11:49:00 +0000335
Steve Block44f0eee2011-05-26 01:26:41 +0100336 template<typename StaticVisitor> inline void Visit(Heap* heap);
Leon Clarkef7060e22010-06-03 12:02:55 +0100337 inline void Visit(ObjectVisitor* v);
338
Steve Blocka7e24c12009-10-30 11:49:00 +0000339 // Patch the code with some other code.
340 void PatchCode(byte* instructions, int instruction_count);
341
342 // Patch the code with a call.
343 void PatchCodeWithCall(Address target, int guard_bytes);
Steve Block3ce2e202009-11-05 08:53:23 +0000344
345 // Check whether this return sequence has been patched
346 // with a call to the debugger.
347 INLINE(bool IsPatchedReturnSequence());
Steve Blocka7e24c12009-10-30 11:49:00 +0000348
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100349 // Check whether this debug break slot has been patched with a call to the
350 // debugger.
351 INLINE(bool IsPatchedDebugBreakSlotSequence());
352
Steve Blocka7e24c12009-10-30 11:49:00 +0000353#ifdef ENABLE_DISASSEMBLER
354 // Printing
355 static const char* RelocModeName(Mode rmode);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100356 void Print(FILE* out);
Steve Blocka7e24c12009-10-30 11:49:00 +0000357#endif // ENABLE_DISASSEMBLER
358#ifdef DEBUG
359 // Debugging
360 void Verify();
361#endif
362
363 static const int kCodeTargetMask = (1 << (LAST_CODE_ENUM + 1)) - 1;
364 static const int kPositionMask = 1 << POSITION | 1 << STATEMENT_POSITION;
365 static const int kDebugMask = kPositionMask | 1 << COMMENT;
366 static const int kApplyMask; // Modes affected by apply. Depends on arch.
367
368 private:
369 // On ARM, note that pc_ is the address of the constant pool entry
370 // to be relocated and not the address of the instruction
371 // referencing the constant pool entry (except when rmode_ ==
372 // comment).
373 byte* pc_;
374 Mode rmode_;
375 intptr_t data_;
376 friend class RelocIterator;
377};
378
379
380// RelocInfoWriter serializes a stream of relocation info. It writes towards
381// lower addresses.
382class RelocInfoWriter BASE_EMBEDDED {
383 public:
384 RelocInfoWriter() : pos_(NULL), last_pc_(NULL), last_data_(0) {}
385 RelocInfoWriter(byte* pos, byte* pc) : pos_(pos), last_pc_(pc),
386 last_data_(0) {}
387
388 byte* pos() const { return pos_; }
389 byte* last_pc() const { return last_pc_; }
390
391 void Write(const RelocInfo* rinfo);
392
393 // Update the state of the stream after reloc info buffer
394 // and/or code is moved while the stream is active.
395 void Reposition(byte* pos, byte* pc) {
396 pos_ = pos;
397 last_pc_ = pc;
398 }
399
400 // Max size (bytes) of a written RelocInfo. Longest encoding is
401 // ExtraTag, VariableLengthPCJump, ExtraTag, pc_delta, ExtraTag, data_delta.
402 // On ia32 and arm this is 1 + 4 + 1 + 1 + 1 + 4 = 12.
403 // On x64 this is 1 + 4 + 1 + 1 + 1 + 8 == 16;
404 // Here we use the maximum of the two.
405 static const int kMaxSize = 16;
406
407 private:
408 inline uint32_t WriteVariableLengthPCJump(uint32_t pc_delta);
409 inline void WriteTaggedPC(uint32_t pc_delta, int tag);
410 inline void WriteExtraTaggedPC(uint32_t pc_delta, int extra_tag);
411 inline void WriteExtraTaggedData(intptr_t data_delta, int top_tag);
412 inline void WriteTaggedData(intptr_t data_delta, int tag);
413 inline void WriteExtraTag(int extra_tag, int top_tag);
414
415 byte* pos_;
416 byte* last_pc_;
417 intptr_t last_data_;
418 DISALLOW_COPY_AND_ASSIGN(RelocInfoWriter);
419};
420
421
422// A RelocIterator iterates over relocation information.
423// Typical use:
424//
425// for (RelocIterator it(code); !it.done(); it.next()) {
426// // do something with it.rinfo() here
427// }
428//
429// A mask can be specified to skip unwanted modes.
430class RelocIterator: public Malloced {
431 public:
432 // Create a new iterator positioned at
433 // the beginning of the reloc info.
434 // Relocation information with mode k is included in the
435 // iteration iff bit k of mode_mask is set.
436 explicit RelocIterator(Code* code, int mode_mask = -1);
437 explicit RelocIterator(const CodeDesc& desc, int mode_mask = -1);
438
439 // Iteration
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100440 bool done() const { return done_; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000441 void next();
442
443 // Return pointer valid until next next().
444 RelocInfo* rinfo() {
445 ASSERT(!done());
446 return &rinfo_;
447 }
448
449 private:
450 // Advance* moves the position before/after reading.
451 // *Read* reads from current byte(s) into rinfo_.
452 // *Get* just reads and returns info on current byte.
453 void Advance(int bytes = 1) { pos_ -= bytes; }
454 int AdvanceGetTag();
455 int GetExtraTag();
456 int GetTopTag();
457 void ReadTaggedPC();
458 void AdvanceReadPC();
459 void AdvanceReadData();
460 void AdvanceReadVariableLengthPCJump();
461 int GetPositionTypeTag();
462 void ReadTaggedData();
463
464 static RelocInfo::Mode DebugInfoModeFromTag(int tag);
465
466 // If the given mode is wanted, set it in rinfo_ and return true.
467 // Else return false. Used for efficiently skipping unwanted modes.
468 bool SetMode(RelocInfo::Mode mode) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100469 return (mode_mask_ & (1 << mode)) ? (rinfo_.rmode_ = mode, true) : false;
Steve Blocka7e24c12009-10-30 11:49:00 +0000470 }
471
472 byte* pos_;
473 byte* end_;
474 RelocInfo rinfo_;
475 bool done_;
476 int mode_mask_;
477 DISALLOW_COPY_AND_ASSIGN(RelocIterator);
478};
479
480
481//------------------------------------------------------------------------------
482// External function
483
484//----------------------------------------------------------------------------
485class IC_Utility;
486class SCTableReference;
487#ifdef ENABLE_DEBUGGER_SUPPORT
488class Debug_Address;
489#endif
490
491
Steve Blocka7e24c12009-10-30 11:49:00 +0000492// An ExternalReference represents a C++ address used in the generated
493// code. All references to C++ functions and variables must be encapsulated in
494// an ExternalReference instance. This is done in order to track the origin of
495// all external references in the code so that they can be bound to the correct
496// addresses when deserializing a heap.
497class ExternalReference BASE_EMBEDDED {
498 public:
Steve Block1e0659c2011-05-24 12:43:12 +0100499 // Used in the simulator to support different native api calls.
Steve Block1e0659c2011-05-24 12:43:12 +0100500 enum Type {
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100501 // Builtin call.
502 // MaybeObject* f(v8::internal::Arguments).
Steve Block1e0659c2011-05-24 12:43:12 +0100503 BUILTIN_CALL, // default
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100504
505 // Builtin call that returns floating point.
506 // double f(double, double).
Steve Block1e0659c2011-05-24 12:43:12 +0100507 FP_RETURN_CALL,
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100508
509 // Direct call to API function callback.
510 // Handle<Value> f(v8::Arguments&)
511 DIRECT_API_CALL,
512
513 // Direct call to accessor getter callback.
514 // Handle<value> f(Local<String> property, AccessorInfo& info)
515 DIRECT_GETTER_CALL
Steve Block1e0659c2011-05-24 12:43:12 +0100516 };
517
518 typedef void* ExternalReferenceRedirector(void* original, Type type);
519
Steve Block44f0eee2011-05-26 01:26:41 +0100520 ExternalReference(Builtins::CFunctionId id, Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000521
Steve Block44f0eee2011-05-26 01:26:41 +0100522 ExternalReference(ApiFunction* ptr, Type type, Isolate* isolate);
Steve Blockd0582a62009-12-15 09:54:21 +0000523
Steve Block44f0eee2011-05-26 01:26:41 +0100524 ExternalReference(Builtins::Name name, Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000525
Steve Block44f0eee2011-05-26 01:26:41 +0100526 ExternalReference(Runtime::FunctionId id, Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000527
Steve Block44f0eee2011-05-26 01:26:41 +0100528 ExternalReference(const Runtime::Function* f, Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000529
Steve Block44f0eee2011-05-26 01:26:41 +0100530 ExternalReference(const IC_Utility& ic_utility, Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000531
532#ifdef ENABLE_DEBUGGER_SUPPORT
Steve Block44f0eee2011-05-26 01:26:41 +0100533 ExternalReference(const Debug_Address& debug_address, Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000534#endif
535
536 explicit ExternalReference(StatsCounter* counter);
537
Steve Block44f0eee2011-05-26 01:26:41 +0100538 ExternalReference(Isolate::AddressId id, Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000539
540 explicit ExternalReference(const SCTableReference& table_ref);
541
Steve Block44f0eee2011-05-26 01:26:41 +0100542 // Isolate::Current() as an external reference.
543 static ExternalReference isolate_address();
544
Steve Blocka7e24c12009-10-30 11:49:00 +0000545 // One-of-a-kind references. These references are not part of a general
546 // pattern. This means that they have to be added to the
547 // ExternalReferenceTable in serialize.cc manually.
548
Steve Block44f0eee2011-05-26 01:26:41 +0100549 static ExternalReference perform_gc_function(Isolate* isolate);
550 static ExternalReference fill_heap_number_with_random_function(
551 Isolate* isolate);
552 static ExternalReference random_uint32_function(Isolate* isolate);
553 static ExternalReference transcendental_cache_array_address(Isolate* isolate);
554 static ExternalReference delete_handle_scope_extensions(Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000555
Ben Murdochb0fe1622011-05-05 13:52:32 +0100556 // Deoptimization support.
Steve Block44f0eee2011-05-26 01:26:41 +0100557 static ExternalReference new_deoptimizer_function(Isolate* isolate);
558 static ExternalReference compute_output_frames_function(Isolate* isolate);
559 static ExternalReference global_contexts_list(Isolate* isolate);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100560
Leon Clarkee46be812010-01-19 14:06:41 +0000561 // Static data in the keyed lookup cache.
Steve Block44f0eee2011-05-26 01:26:41 +0100562 static ExternalReference keyed_lookup_cache_keys(Isolate* isolate);
563 static ExternalReference keyed_lookup_cache_field_offsets(Isolate* isolate);
Leon Clarkee46be812010-01-19 14:06:41 +0000564
Steve Blocka7e24c12009-10-30 11:49:00 +0000565 // Static variable Factory::the_hole_value.location()
Steve Block44f0eee2011-05-26 01:26:41 +0100566 static ExternalReference the_hole_value_location(Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000567
Ben Murdoch086aeea2011-05-13 15:57:08 +0100568 // Static variable Factory::arguments_marker.location()
Steve Block44f0eee2011-05-26 01:26:41 +0100569 static ExternalReference arguments_marker_location(Isolate* isolate);
Ben Murdoch086aeea2011-05-13 15:57:08 +0100570
Steve Blocka7e24c12009-10-30 11:49:00 +0000571 // Static variable Heap::roots_address()
Steve Block44f0eee2011-05-26 01:26:41 +0100572 static ExternalReference roots_address(Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000573
574 // Static variable StackGuard::address_of_jslimit()
Steve Block44f0eee2011-05-26 01:26:41 +0100575 static ExternalReference address_of_stack_limit(Isolate* isolate);
Steve Blockd0582a62009-12-15 09:54:21 +0000576
577 // Static variable StackGuard::address_of_real_jslimit()
Steve Block44f0eee2011-05-26 01:26:41 +0100578 static ExternalReference address_of_real_stack_limit(Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000579
580 // Static variable RegExpStack::limit_address()
Steve Block44f0eee2011-05-26 01:26:41 +0100581 static ExternalReference address_of_regexp_stack_limit(Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000582
Leon Clarkee46be812010-01-19 14:06:41 +0000583 // Static variables for RegExp.
Steve Block44f0eee2011-05-26 01:26:41 +0100584 static ExternalReference address_of_static_offsets_vector(Isolate* isolate);
585 static ExternalReference address_of_regexp_stack_memory_address(
586 Isolate* isolate);
587 static ExternalReference address_of_regexp_stack_memory_size(
588 Isolate* isolate);
Leon Clarkee46be812010-01-19 14:06:41 +0000589
Steve Blocka7e24c12009-10-30 11:49:00 +0000590 // Static variable Heap::NewSpaceStart()
Steve Block44f0eee2011-05-26 01:26:41 +0100591 static ExternalReference new_space_start(Isolate* isolate);
592 static ExternalReference new_space_mask(Isolate* isolate);
593 static ExternalReference heap_always_allocate_scope_depth(Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000594
595 // Used for fast allocation in generated code.
Steve Block44f0eee2011-05-26 01:26:41 +0100596 static ExternalReference new_space_allocation_top_address(Isolate* isolate);
597 static ExternalReference new_space_allocation_limit_address(Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000598
Steve Block44f0eee2011-05-26 01:26:41 +0100599 static ExternalReference double_fp_operation(Token::Value operation,
600 Isolate* isolate);
601 static ExternalReference compare_doubles(Isolate* isolate);
602 static ExternalReference power_double_double_function(Isolate* isolate);
603 static ExternalReference power_double_int_function(Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000604
Steve Blockd0582a62009-12-15 09:54:21 +0000605 static ExternalReference handle_scope_next_address();
606 static ExternalReference handle_scope_limit_address();
John Reck59135872010-11-02 12:39:01 -0700607 static ExternalReference handle_scope_level_address();
Steve Blockd0582a62009-12-15 09:54:21 +0000608
Steve Block44f0eee2011-05-26 01:26:41 +0100609 static ExternalReference scheduled_exception_address(Isolate* isolate);
Steve Blockd0582a62009-12-15 09:54:21 +0000610
Ben Murdochb0fe1622011-05-05 13:52:32 +0100611 // Static variables containing common double constants.
612 static ExternalReference address_of_min_int();
613 static ExternalReference address_of_one_half();
Ben Murdochb8e0da22011-05-16 14:20:40 +0100614 static ExternalReference address_of_minus_zero();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100615 static ExternalReference address_of_negative_infinity();
Steve Block44f0eee2011-05-26 01:26:41 +0100616 static ExternalReference address_of_nan();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100617
Steve Block44f0eee2011-05-26 01:26:41 +0100618 static ExternalReference math_sin_double_function(Isolate* isolate);
619 static ExternalReference math_cos_double_function(Isolate* isolate);
620 static ExternalReference math_log_double_function(Isolate* isolate);
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100621
Steve Blocka7e24c12009-10-30 11:49:00 +0000622 Address address() const {return reinterpret_cast<Address>(address_);}
623
624#ifdef ENABLE_DEBUGGER_SUPPORT
625 // Function Debug::Break()
Steve Block44f0eee2011-05-26 01:26:41 +0100626 static ExternalReference debug_break(Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000627
628 // Used to check if single stepping is enabled in generated code.
Steve Block44f0eee2011-05-26 01:26:41 +0100629 static ExternalReference debug_step_in_fp_address(Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000630#endif
631
Steve Block6ded16b2010-05-10 14:33:55 +0100632#ifndef V8_INTERPRETED_REGEXP
Steve Blocka7e24c12009-10-30 11:49:00 +0000633 // C functions called from RegExp generated code.
634
635 // Function NativeRegExpMacroAssembler::CaseInsensitiveCompareUC16()
Steve Block44f0eee2011-05-26 01:26:41 +0100636 static ExternalReference re_case_insensitive_compare_uc16(Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000637
638 // Function RegExpMacroAssembler*::CheckStackGuardState()
Steve Block44f0eee2011-05-26 01:26:41 +0100639 static ExternalReference re_check_stack_guard_state(Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000640
641 // Function NativeRegExpMacroAssembler::GrowStack()
Steve Block44f0eee2011-05-26 01:26:41 +0100642 static ExternalReference re_grow_stack(Isolate* isolate);
Leon Clarkee46be812010-01-19 14:06:41 +0000643
644 // byte NativeRegExpMacroAssembler::word_character_bitmap
645 static ExternalReference re_word_character_map();
646
Steve Blocka7e24c12009-10-30 11:49:00 +0000647#endif
648
649 // This lets you register a function that rewrites all external references.
650 // Used by the ARM simulator to catch calls to external references.
651 static void set_redirector(ExternalReferenceRedirector* redirector) {
Steve Block44f0eee2011-05-26 01:26:41 +0100652 // We can't stack them.
653 ASSERT(Isolate::Current()->external_reference_redirector() == NULL);
654 Isolate::Current()->set_external_reference_redirector(
655 reinterpret_cast<ExternalReferenceRedirectorPointer*>(redirector));
Steve Blocka7e24c12009-10-30 11:49:00 +0000656 }
657
658 private:
659 explicit ExternalReference(void* address)
660 : address_(address) {}
661
Steve Block44f0eee2011-05-26 01:26:41 +0100662 static void* Redirect(Isolate* isolate,
663 void* address,
Steve Block1e0659c2011-05-24 12:43:12 +0100664 Type type = ExternalReference::BUILTIN_CALL) {
Steve Block44f0eee2011-05-26 01:26:41 +0100665 ExternalReferenceRedirector* redirector =
666 reinterpret_cast<ExternalReferenceRedirector*>(
667 isolate->external_reference_redirector());
668 if (redirector == NULL) return address;
669 void* answer = (*redirector)(address, type);
Steve Blockd0582a62009-12-15 09:54:21 +0000670 return answer;
Steve Blocka7e24c12009-10-30 11:49:00 +0000671 }
672
Steve Block44f0eee2011-05-26 01:26:41 +0100673 static void* Redirect(Isolate* isolate,
674 Address address_arg,
Steve Block1e0659c2011-05-24 12:43:12 +0100675 Type type = ExternalReference::BUILTIN_CALL) {
Steve Block44f0eee2011-05-26 01:26:41 +0100676 ExternalReferenceRedirector* redirector =
677 reinterpret_cast<ExternalReferenceRedirector*>(
678 isolate->external_reference_redirector());
Steve Blocka7e24c12009-10-30 11:49:00 +0000679 void* address = reinterpret_cast<void*>(address_arg);
Steve Block44f0eee2011-05-26 01:26:41 +0100680 void* answer = (redirector == NULL) ?
Steve Blockd0582a62009-12-15 09:54:21 +0000681 address :
Steve Block44f0eee2011-05-26 01:26:41 +0100682 (*redirector)(address, type);
Steve Blockd0582a62009-12-15 09:54:21 +0000683 return answer;
Steve Blocka7e24c12009-10-30 11:49:00 +0000684 }
685
686 void* address_;
687};
688
689
690// -----------------------------------------------------------------------------
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800691// Position recording support
692
Ben Murdochb0fe1622011-05-05 13:52:32 +0100693struct PositionState {
694 PositionState() : current_position(RelocInfo::kNoPosition),
695 written_position(RelocInfo::kNoPosition),
696 current_statement_position(RelocInfo::kNoPosition),
697 written_statement_position(RelocInfo::kNoPosition) {}
698
699 int current_position;
700 int written_position;
701
702 int current_statement_position;
703 int written_statement_position;
704};
705
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800706
707class PositionsRecorder BASE_EMBEDDED {
708 public:
709 explicit PositionsRecorder(Assembler* assembler)
Ben Murdochb8e0da22011-05-16 14:20:40 +0100710 : assembler_(assembler) {
711#ifdef ENABLE_GDB_JIT_INTERFACE
712 gdbjit_lineinfo_ = NULL;
713#endif
714 }
715
716#ifdef ENABLE_GDB_JIT_INTERFACE
717 ~PositionsRecorder() {
718 delete gdbjit_lineinfo_;
719 }
720
721 void StartGDBJITLineInfoRecording() {
722 if (FLAG_gdbjit) {
723 gdbjit_lineinfo_ = new GDBJITLineInfo();
724 }
725 }
726
727 GDBJITLineInfo* DetachGDBJITLineInfo() {
728 GDBJITLineInfo* lineinfo = gdbjit_lineinfo_;
729 gdbjit_lineinfo_ = NULL; // To prevent deallocation in destructor.
730 return lineinfo;
731 }
732#endif
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800733
Ben Murdochb0fe1622011-05-05 13:52:32 +0100734 // Set current position to pos.
735 void RecordPosition(int pos);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800736
737 // Set current statement position to pos.
738 void RecordStatementPosition(int pos);
739
740 // Write recorded positions to relocation information.
741 bool WriteRecordedPositions();
742
Ben Murdochb0fe1622011-05-05 13:52:32 +0100743 int current_position() const { return state_.current_position; }
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800744
Ben Murdochb0fe1622011-05-05 13:52:32 +0100745 int current_statement_position() const {
746 return state_.current_statement_position;
747 }
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800748
749 private:
750 Assembler* assembler_;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100751 PositionState state_;
Ben Murdochb8e0da22011-05-16 14:20:40 +0100752#ifdef ENABLE_GDB_JIT_INTERFACE
753 GDBJITLineInfo* gdbjit_lineinfo_;
754#endif
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800755
Ben Murdochb0fe1622011-05-05 13:52:32 +0100756 friend class PreservePositionScope;
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800757
Ben Murdochb0fe1622011-05-05 13:52:32 +0100758 DISALLOW_COPY_AND_ASSIGN(PositionsRecorder);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800759};
760
761
Ben Murdochb0fe1622011-05-05 13:52:32 +0100762class PreservePositionScope BASE_EMBEDDED {
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800763 public:
Ben Murdochb0fe1622011-05-05 13:52:32 +0100764 explicit PreservePositionScope(PositionsRecorder* positions_recorder)
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800765 : positions_recorder_(positions_recorder),
Ben Murdochb0fe1622011-05-05 13:52:32 +0100766 saved_state_(positions_recorder->state_) {}
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800767
Ben Murdochb0fe1622011-05-05 13:52:32 +0100768 ~PreservePositionScope() {
769 positions_recorder_->state_ = saved_state_;
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800770 }
771
772 private:
773 PositionsRecorder* positions_recorder_;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100774 const PositionState saved_state_;
775
776 DISALLOW_COPY_AND_ASSIGN(PreservePositionScope);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800777};
778
779
780// -----------------------------------------------------------------------------
Steve Blocka7e24c12009-10-30 11:49:00 +0000781// Utility functions
782
783static inline bool is_intn(int x, int n) {
784 return -(1 << (n-1)) <= x && x < (1 << (n-1));
785}
786
Steve Blocka7e24c12009-10-30 11:49:00 +0000787static inline bool is_int8(int x) { return is_intn(x, 8); }
Andrei Popescu31002712010-02-23 13:46:05 +0000788static inline bool is_int16(int x) { return is_intn(x, 16); }
789static inline bool is_int18(int x) { return is_intn(x, 18); }
790static inline bool is_int24(int x) { return is_intn(x, 24); }
Steve Blocka7e24c12009-10-30 11:49:00 +0000791
792static inline bool is_uintn(int x, int n) {
793 return (x & -(1 << n)) == 0;
794}
795
796static inline bool is_uint2(int x) { return is_uintn(x, 2); }
797static inline bool is_uint3(int x) { return is_uintn(x, 3); }
798static inline bool is_uint4(int x) { return is_uintn(x, 4); }
799static inline bool is_uint5(int x) { return is_uintn(x, 5); }
800static inline bool is_uint6(int x) { return is_uintn(x, 6); }
801static inline bool is_uint8(int x) { return is_uintn(x, 8); }
Andrei Popescu31002712010-02-23 13:46:05 +0000802static inline bool is_uint10(int x) { return is_uintn(x, 10); }
Steve Blocka7e24c12009-10-30 11:49:00 +0000803static inline bool is_uint12(int x) { return is_uintn(x, 12); }
804static inline bool is_uint16(int x) { return is_uintn(x, 16); }
805static inline bool is_uint24(int x) { return is_uintn(x, 24); }
Andrei Popescu31002712010-02-23 13:46:05 +0000806static inline bool is_uint26(int x) { return is_uintn(x, 26); }
807static inline bool is_uint28(int x) { return is_uintn(x, 28); }
808
809static inline int NumberOfBitsSet(uint32_t x) {
810 unsigned int num_bits_set;
811 for (num_bits_set = 0; x; x >>= 1) {
812 num_bits_set += x & 1;
813 }
814 return num_bits_set;
815}
Steve Blocka7e24c12009-10-30 11:49:00 +0000816
Ben Murdochb0fe1622011-05-05 13:52:32 +0100817// Computes pow(x, y) with the special cases in the spec for Math.pow.
818double power_double_int(double x, int y);
819double power_double_double(double x, double y);
820
Steve Blocka7e24c12009-10-30 11:49:00 +0000821} } // namespace v8::internal
822
823#endif // V8_ASSEMBLER_H_