blob: b089b090cc59fb820940844a56fb5f8552ee3d49 [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"
40#include "top.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000041#include "token.h"
42
43namespace v8 {
44namespace internal {
45
46
47// -----------------------------------------------------------------------------
Ben Murdochb0fe1622011-05-05 13:52:32 +010048// Common double constants.
49
50class DoubleConstant: public AllStatic {
51 public:
52 static const double min_int;
53 static const double one_half;
Ben Murdochb8e0da22011-05-16 14:20:40 +010054 static const double minus_zero;
Ben Murdochb0fe1622011-05-05 13:52:32 +010055 static const double negative_infinity;
56};
57
58
59// -----------------------------------------------------------------------------
Steve Blocka7e24c12009-10-30 11:49:00 +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
65class Label BASE_EMBEDDED {
66 public:
67 INLINE(Label()) { Unuse(); }
68 INLINE(~Label()) { ASSERT(!is_linked()); }
69
70 INLINE(void Unuse()) { pos_ = 0; }
71
Kristian Monsen0d5e1162010-09-30 15:31:59 +010072 INLINE(bool is_bound() const) { return pos_ < 0; }
Steve Blocka7e24c12009-10-30 11:49:00 +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;
99 friend class RegexpAssembler;
100 friend class Displacement;
101 friend class ShadowTarget;
102 friend class RegExpMacroAssemblerIrregexp;
103};
104
105
106// -----------------------------------------------------------------------------
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100107// 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// -----------------------------------------------------------------------------
Steve Blocka7e24c12009-10-30 11:49:00 +0000158// Relocation information
159
160
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:
170 // 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.
Steve Block1e0659c2011-05-24 12:43:12 +0100184 CODE_TARGET_CONTEXT, // Code target used for contextual loads and stores.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100185 DEBUG_BREAK, // Code target for the debugger statement.
186 CODE_TARGET, // Code target which is not any of the above.
Steve Blocka7e24c12009-10-30 11:49:00 +0000187 EMBEDDED_OBJECT,
Ben Murdochb0fe1622011-05-05 13:52:32 +0100188 GLOBAL_PROPERTY_CELL,
189
Steve Blocka7e24c12009-10-30 11:49:00 +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.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100196 DEBUG_BREAK_SLOT, // Additional code inserted for debug break slot.
Steve Blocka7e24c12009-10-30 11:49:00 +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,
Steve Block1e0659c2011-05-24 12:43:12 +0100205 LAST_GCED_ENUM = GLOBAL_PROPERTY_CELL
Steve Blocka7e24c12009-10-30 11:49:00 +0000206 };
207
208
209 RelocInfo() {}
210 RelocInfo(byte* pc, Mode rmode, intptr_t data)
211 : pc_(pc), rmode_(rmode), data_(data) {
212 }
213
214 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 }
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100242 static inline bool IsDebugBreakSlot(Mode mode) {
243 return mode == DEBUG_BREAK_SLOT;
244 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000245 static inline int ModeMask(Mode mode) { return 1 << mode; }
246
247 // Accessors
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100248 byte* pc() const { return pc_; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000249 void set_pc(byte* pc) { pc_ = pc; }
250 Mode rmode() const { return rmode_; }
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100251 intptr_t data() const { return data_; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000252
253 // Apply a relocation by delta bytes
254 INLINE(void apply(intptr_t delta));
255
Leon Clarkef7060e22010-06-03 12:02:55 +0100256 // 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
Steve Blocka7e24c12009-10-30 11:49:00 +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
264 INLINE(Address target_address());
265 INLINE(void set_target_address(Address target));
266 INLINE(Object* target_object());
Steve Block3ce2e202009-11-05 08:53:23 +0000267 INLINE(Handle<Object> target_object_handle(Assembler* origin));
Steve Blocka7e24c12009-10-30 11:49:00 +0000268 INLINE(Object** target_object_address());
269 INLINE(void set_target_object(Object* target));
Ben Murdochb0fe1622011-05-05 13:52:32 +0100270 INLINE(JSGlobalPropertyCell* target_cell());
271 INLINE(Handle<JSGlobalPropertyCell> target_cell_handle());
272 INLINE(void set_target_cell(JSGlobalPropertyCell* cell));
273
Steve Blocka7e24c12009-10-30 11:49:00 +0000274
Leon Clarkef7060e22010-06-03 12:02:55 +0100275 // 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.
Steve Blocka7e24c12009-10-30 11:49:00 +0000281 INLINE(Address target_address_address());
Leon Clarkef7060e22010-06-03 12:02:55 +0100282 // 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());
Steve Blocka7e24c12009-10-30 11:49:00 +0000292
293 // 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());
Steve Blocka7e24c12009-10-30 11:49:00 +0000303 INLINE(void set_call_object(Object* target));
Ben Murdochbb769b22010-08-11 14:56:33 +0100304 INLINE(Object** call_object_address());
Steve Blocka7e24c12009-10-30 11:49:00 +0000305
Iain Merrick75681382010-08-19 15:07:18 +0100306 template<typename StaticVisitor> inline void Visit();
Leon Clarkef7060e22010-06-03 12:02:55 +0100307 inline void Visit(ObjectVisitor* v);
308
Steve Blocka7e24c12009-10-30 11:49:00 +0000309 // Patch the code with some other code.
310 void PatchCode(byte* instructions, int instruction_count);
311
312 // Patch the code with a call.
313 void PatchCodeWithCall(Address target, int guard_bytes);
Steve Block3ce2e202009-11-05 08:53:23 +0000314
315 // Check whether this return sequence has been patched
316 // with a call to the debugger.
317 INLINE(bool IsPatchedReturnSequence());
Steve Blocka7e24c12009-10-30 11:49:00 +0000318
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100319 // Check whether this debug break slot has been patched with a call to the
320 // debugger.
321 INLINE(bool IsPatchedDebugBreakSlotSequence());
322
Steve Blocka7e24c12009-10-30 11:49:00 +0000323#ifdef ENABLE_DISASSEMBLER
324 // Printing
325 static const char* RelocModeName(Mode rmode);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100326 void Print(FILE* out);
Steve Blocka7e24c12009-10-30 11:49:00 +0000327#endif // ENABLE_DISASSEMBLER
328#ifdef DEBUG
329 // Debugging
330 void Verify();
331#endif
332
333 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;
336 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_;
344 Mode rmode_;
345 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
370 // 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;
376
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);
381 inline void WriteExtraTaggedData(intptr_t data_delta, int top_tag);
382 inline void WriteTaggedData(intptr_t data_delta, int tag);
383 inline void WriteExtraTag(int extra_tag, int top_tag);
384
385 byte* pos_;
386 byte* last_pc_;
387 intptr_t last_data_;
388 DISALLOW_COPY_AND_ASSIGN(RelocInfoWriter);
389};
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
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100410 bool done() const { return done_; }
Steve Blocka7e24c12009-10-30 11:49:00 +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
434 static RelocInfo::Mode DebugInfoModeFromTag(int tag);
435
436 // If the given mode is wanted, set it in rinfo_ and return true.
437 // Else return false. Used for efficiently skipping unwanted modes.
438 bool SetMode(RelocInfo::Mode mode) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100439 return (mode_mask_ & (1 << mode)) ? (rinfo_.rmode_ = mode, true) : false;
Steve Blocka7e24c12009-10-30 11:49:00 +0000440 }
441
442 byte* pos_;
443 byte* end_;
444 RelocInfo rinfo_;
445 bool done_;
446 int mode_mask_;
447 DISALLOW_COPY_AND_ASSIGN(RelocIterator);
448};
449
450
451//------------------------------------------------------------------------------
452// External function
453
454//----------------------------------------------------------------------------
455class IC_Utility;
456class SCTableReference;
457#ifdef ENABLE_DEBUGGER_SUPPORT
458class Debug_Address;
459#endif
460
461
Steve Blocka7e24c12009-10-30 11:49:00 +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.
467class ExternalReference BASE_EMBEDDED {
468 public:
Steve Block1e0659c2011-05-24 12:43:12 +0100469 // 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
Steve Blocka7e24c12009-10-30 11:49:00 +0000489 explicit ExternalReference(Builtins::CFunctionId id);
490
Steve Block1e0659c2011-05-24 12:43:12 +0100491 explicit ExternalReference(ApiFunction* ptr, Type type);
Steve Blockd0582a62009-12-15 09:54:21 +0000492
Steve Blocka7e24c12009-10-30 11:49:00 +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
501#ifdef ENABLE_DEBUGGER_SUPPORT
502 explicit ExternalReference(const Debug_Address& debug_address);
503#endif
504
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
515 static ExternalReference perform_gc_function();
Steve Block6ded16b2010-05-10 14:33:55 +0100516 static ExternalReference fill_heap_number_with_random_function();
517 static ExternalReference random_uint32_function();
Andrei Popescu402d9372010-02-26 13:31:12 +0000518 static ExternalReference transcendental_cache_array_address();
John Reck59135872010-11-02 12:39:01 -0700519 static ExternalReference delete_handle_scope_extensions();
Steve Blocka7e24c12009-10-30 11:49:00 +0000520
Ben Murdochb0fe1622011-05-05 13:52:32 +0100521 // Deoptimization support.
522 static ExternalReference new_deoptimizer_function();
523 static ExternalReference compute_output_frames_function();
524 static ExternalReference global_contexts_list();
525
Leon Clarkee46be812010-01-19 14:06:41 +0000526 // Static data in the keyed lookup cache.
527 static ExternalReference keyed_lookup_cache_keys();
528 static ExternalReference keyed_lookup_cache_field_offsets();
529
Steve Blocka7e24c12009-10-30 11:49:00 +0000530 // Static variable Factory::the_hole_value.location()
531 static ExternalReference the_hole_value_location();
532
Ben Murdoch086aeea2011-05-13 15:57:08 +0100533 // Static variable Factory::arguments_marker.location()
534 static ExternalReference arguments_marker_location();
535
Steve Blocka7e24c12009-10-30 11:49:00 +0000536 // Static variable Heap::roots_address()
537 static ExternalReference roots_address();
538
539 // Static variable StackGuard::address_of_jslimit()
Steve Blockd0582a62009-12-15 09:54:21 +0000540 static ExternalReference address_of_stack_limit();
541
542 // Static variable StackGuard::address_of_real_jslimit()
543 static ExternalReference address_of_real_stack_limit();
Steve Blocka7e24c12009-10-30 11:49:00 +0000544
545 // Static variable RegExpStack::limit_address()
546 static ExternalReference address_of_regexp_stack_limit();
547
Leon Clarkee46be812010-01-19 14:06:41 +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
Steve Blocka7e24c12009-10-30 11:49:00 +0000553 // Static variable Heap::NewSpaceStart()
554 static ExternalReference new_space_start();
Andrei Popescu402d9372010-02-26 13:31:12 +0000555 static ExternalReference new_space_mask();
Steve Blocka7e24c12009-10-30 11:49:00 +0000556 static ExternalReference heap_always_allocate_scope_depth();
557
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
562 static ExternalReference double_fp_operation(Token::Value operation);
563 static ExternalReference compare_doubles();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100564 static ExternalReference power_double_double_function();
565 static ExternalReference power_double_int_function();
Steve Blocka7e24c12009-10-30 11:49:00 +0000566
Steve Blockd0582a62009-12-15 09:54:21 +0000567 static ExternalReference handle_scope_next_address();
568 static ExternalReference handle_scope_limit_address();
John Reck59135872010-11-02 12:39:01 -0700569 static ExternalReference handle_scope_level_address();
Steve Blockd0582a62009-12-15 09:54:21 +0000570
571 static ExternalReference scheduled_exception_address();
572
Ben Murdochb0fe1622011-05-05 13:52:32 +0100573 // Static variables containing common double constants.
574 static ExternalReference address_of_min_int();
575 static ExternalReference address_of_one_half();
Ben Murdochb8e0da22011-05-16 14:20:40 +0100576 static ExternalReference address_of_minus_zero();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100577 static ExternalReference address_of_negative_infinity();
578
Steve Blocka7e24c12009-10-30 11:49:00 +0000579 Address address() const {return reinterpret_cast<Address>(address_);}
580
581#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
Steve Block6ded16b2010-05-10 14:33:55 +0100589#ifndef V8_INTERPRETED_REGEXP
Steve Blocka7e24c12009-10-30 11:49:00 +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();
Leon Clarkee46be812010-01-19 14:06:41 +0000600
601 // byte NativeRegExpMacroAssembler::word_character_bitmap
602 static ExternalReference re_word_character_map();
603
Steve Blocka7e24c12009-10-30 11:49:00 +0000604#endif
605
606 // 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
613 private:
614 explicit ExternalReference(void* address)
615 : address_(address) {}
616
617 static ExternalReferenceRedirector* redirector_;
618
Steve Block1e0659c2011-05-24 12:43:12 +0100619 static void* Redirect(void* address,
620 Type type = ExternalReference::BUILTIN_CALL) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000621 if (redirector_ == NULL) return address;
Steve Block1e0659c2011-05-24 12:43:12 +0100622 void* answer = (*redirector_)(address, type);
Steve Blockd0582a62009-12-15 09:54:21 +0000623 return answer;
Steve Blocka7e24c12009-10-30 11:49:00 +0000624 }
625
Steve Block1e0659c2011-05-24 12:43:12 +0100626 static void* Redirect(Address address_arg,
627 Type type = ExternalReference::BUILTIN_CALL) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000628 void* address = reinterpret_cast<void*>(address_arg);
Steve Blockd0582a62009-12-15 09:54:21 +0000629 void* answer = (redirector_ == NULL) ?
630 address :
Steve Block1e0659c2011-05-24 12:43:12 +0100631 (*redirector_)(address, type);
Steve Blockd0582a62009-12-15 09:54:21 +0000632 return answer;
Steve Blocka7e24c12009-10-30 11:49:00 +0000633 }
634
635 void* address_;
636};
637
638
639// -----------------------------------------------------------------------------
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800640// Position recording support
641
Ben Murdochb0fe1622011-05-05 13:52:32 +0100642struct 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
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800655
656class PositionsRecorder BASE_EMBEDDED {
657 public:
658 explicit PositionsRecorder(Assembler* assembler)
Ben Murdochb8e0da22011-05-16 14:20:40 +0100659 : 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
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800682
Ben Murdochb0fe1622011-05-05 13:52:32 +0100683 // Set current position to pos.
684 void RecordPosition(int pos);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800685
686 // Set current statement position to pos.
687 void RecordStatementPosition(int pos);
688
689 // Write recorded positions to relocation information.
690 bool WriteRecordedPositions();
691
Ben Murdochb0fe1622011-05-05 13:52:32 +0100692 int current_position() const { return state_.current_position; }
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800693
Ben Murdochb0fe1622011-05-05 13:52:32 +0100694 int current_statement_position() const {
695 return state_.current_statement_position;
696 }
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800697
698 private:
699 Assembler* assembler_;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100700 PositionState state_;
Ben Murdochb8e0da22011-05-16 14:20:40 +0100701#ifdef ENABLE_GDB_JIT_INTERFACE
702 GDBJITLineInfo* gdbjit_lineinfo_;
703#endif
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800704
Ben Murdochb0fe1622011-05-05 13:52:32 +0100705 friend class PreservePositionScope;
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800706
Ben Murdochb0fe1622011-05-05 13:52:32 +0100707 DISALLOW_COPY_AND_ASSIGN(PositionsRecorder);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800708};
709
710
Ben Murdochb0fe1622011-05-05 13:52:32 +0100711class PreservePositionScope BASE_EMBEDDED {
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800712 public:
Ben Murdochb0fe1622011-05-05 13:52:32 +0100713 explicit PreservePositionScope(PositionsRecorder* positions_recorder)
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800714 : positions_recorder_(positions_recorder),
Ben Murdochb0fe1622011-05-05 13:52:32 +0100715 saved_state_(positions_recorder->state_) {}
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800716
Ben Murdochb0fe1622011-05-05 13:52:32 +0100717 ~PreservePositionScope() {
718 positions_recorder_->state_ = saved_state_;
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800719 }
720
721 private:
722 PositionsRecorder* positions_recorder_;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100723 const PositionState saved_state_;
724
725 DISALLOW_COPY_AND_ASSIGN(PreservePositionScope);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800726};
727
728
729// -----------------------------------------------------------------------------
Steve Blocka7e24c12009-10-30 11:49:00 +0000730// Utility functions
731
732static inline bool is_intn(int x, int n) {
733 return -(1 << (n-1)) <= x && x < (1 << (n-1));
734}
735
Steve Blocka7e24c12009-10-30 11:49:00 +0000736static inline bool is_int8(int x) { return is_intn(x, 8); }
Andrei Popescu31002712010-02-23 13:46:05 +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); }
Steve Blocka7e24c12009-10-30 11:49:00 +0000740
741static inline bool is_uintn(int x, int n) {
742 return (x & -(1 << n)) == 0;
743}
744
745static inline bool is_uint2(int x) { return is_uintn(x, 2); }
746static 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); }
749static inline bool is_uint6(int x) { return is_uintn(x, 6); }
750static inline bool is_uint8(int x) { return is_uintn(x, 8); }
Andrei Popescu31002712010-02-23 13:46:05 +0000751static inline bool is_uint10(int x) { return is_uintn(x, 10); }
Steve Blocka7e24c12009-10-30 11:49:00 +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); }
Andrei Popescu31002712010-02-23 13:46:05 +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}
Steve Blocka7e24c12009-10-30 11:49:00 +0000765
Ben Murdochb0fe1622011-05-05 13:52:32 +0100766// 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
Steve Blocka7e24c12009-10-30 11:49:00 +0000770} } // namespace v8::internal
771
772#endif // V8_ASSEMBLER_H_