blob: b68ad38970892491b039c28f589b2201263b3a2a [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
38#include "runtime.h"
39#include "top.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000040#include "token.h"
41
42namespace v8 {
43namespace internal {
44
45
46// -----------------------------------------------------------------------------
Ben Murdochb0fe1622011-05-05 13:52:32 +010047// Common double constants.
48
49class DoubleConstant: public AllStatic {
50 public:
51 static const double min_int;
52 static const double one_half;
53 static const double negative_infinity;
54};
55
56
57// -----------------------------------------------------------------------------
Steve Blocka7e24c12009-10-30 11:49:00 +000058// Labels represent pc locations; they are typically jump or call targets.
59// After declaration, a label can be freely used to denote known or (yet)
60// unknown pc location. Assembler::bind() is used to bind a label to the
61// current pc. A label can be bound only once.
62
63class Label BASE_EMBEDDED {
64 public:
65 INLINE(Label()) { Unuse(); }
66 INLINE(~Label()) { ASSERT(!is_linked()); }
67
68 INLINE(void Unuse()) { pos_ = 0; }
69
Kristian Monsen0d5e1162010-09-30 15:31:59 +010070 INLINE(bool is_bound() const) { return pos_ < 0; }
Steve Blocka7e24c12009-10-30 11:49:00 +000071 INLINE(bool is_unused() const) { return pos_ == 0; }
72 INLINE(bool is_linked() const) { return pos_ > 0; }
73
74 // Returns the position of bound or linked labels. Cannot be used
75 // for unused labels.
76 int pos() const;
77
78 private:
79 // pos_ encodes both the binding state (via its sign)
80 // and the binding position (via its value) of a label.
81 //
82 // pos_ < 0 bound label, pos() returns the jump target position
83 // pos_ == 0 unused label
84 // pos_ > 0 linked label, pos() returns the last reference position
85 int pos_;
86
87 void bind_to(int pos) {
88 pos_ = -pos - 1;
89 ASSERT(is_bound());
90 }
91 void link_to(int pos) {
92 pos_ = pos + 1;
93 ASSERT(is_linked());
94 }
95
96 friend class Assembler;
97 friend class RegexpAssembler;
98 friend class Displacement;
99 friend class ShadowTarget;
100 friend class RegExpMacroAssemblerIrregexp;
101};
102
103
104// -----------------------------------------------------------------------------
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100105// NearLabels are labels used for short jumps (in Intel jargon).
106// NearLabels should be used if it can be guaranteed that the jump range is
107// within -128 to +127. We already use short jumps when jumping backwards,
108// so using a NearLabel will only have performance impact if used for forward
109// jumps.
110class NearLabel BASE_EMBEDDED {
111 public:
112 NearLabel() { Unuse(); }
113 ~NearLabel() { ASSERT(!is_linked()); }
114
115 void Unuse() {
116 pos_ = -1;
117 unresolved_branches_ = 0;
118#ifdef DEBUG
119 for (int i = 0; i < kMaxUnresolvedBranches; i++) {
120 unresolved_positions_[i] = -1;
121 }
122#endif
123 }
124
125 int pos() {
126 ASSERT(is_bound());
127 return pos_;
128 }
129
130 bool is_bound() { return pos_ >= 0; }
131 bool is_linked() { return !is_bound() && unresolved_branches_ > 0; }
132 bool is_unused() { return !is_bound() && unresolved_branches_ == 0; }
133
134 void bind_to(int position) {
135 ASSERT(!is_bound());
136 pos_ = position;
137 }
138
139 void link_to(int position) {
140 ASSERT(!is_bound());
141 ASSERT(unresolved_branches_ < kMaxUnresolvedBranches);
142 unresolved_positions_[unresolved_branches_++] = position;
143 }
144
145 private:
146 static const int kMaxUnresolvedBranches = 8;
147 int pos_;
148 int unresolved_branches_;
149 int unresolved_positions_[kMaxUnresolvedBranches];
150
151 friend class Assembler;
152};
153
154
155// -----------------------------------------------------------------------------
Steve Blocka7e24c12009-10-30 11:49:00 +0000156// Relocation information
157
158
159// Relocation information consists of the address (pc) of the datum
160// to which the relocation information applies, the relocation mode
161// (rmode), and an optional data field. The relocation mode may be
162// "descriptive" and not indicate a need for relocation, but simply
163// describe a property of the datum. Such rmodes are useful for GC
164// and nice disassembly output.
165
166class RelocInfo BASE_EMBEDDED {
167 public:
168 // The constant kNoPosition is used with the collecting of source positions
169 // in the relocation information. Two types of source positions are collected
170 // "position" (RelocMode position) and "statement position" (RelocMode
171 // statement_position). The "position" is collected at places in the source
172 // code which are of interest when making stack traces to pin-point the source
173 // location of a stack frame as close as possible. The "statement position" is
174 // collected at the beginning at each statement, and is used to indicate
175 // possible break locations. kNoPosition is used to indicate an
176 // invalid/uninitialized position value.
177 static const int kNoPosition = -1;
178
179 enum Mode {
180 // Please note the order is important (see IsCodeTarget, IsGCRelocMode).
181 CONSTRUCT_CALL, // code target that is a call to a JavaScript constructor.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100182 CODE_TARGET_CONTEXT, // Code target used for contextual loads.
183 DEBUG_BREAK, // Code target for the debugger statement.
184 CODE_TARGET, // Code target which is not any of the above.
Steve Blocka7e24c12009-10-30 11:49:00 +0000185 EMBEDDED_OBJECT,
Steve Blocka7e24c12009-10-30 11:49:00 +0000186
Ben Murdochb0fe1622011-05-05 13:52:32 +0100187 GLOBAL_PROPERTY_CELL,
188
Steve Blocka7e24c12009-10-30 11:49:00 +0000189 // Everything after runtime_entry (inclusive) is not GC'ed.
190 RUNTIME_ENTRY,
191 JS_RETURN, // Marks start of the ExitJSFrame code.
192 COMMENT,
193 POSITION, // See comment for kNoPosition above.
194 STATEMENT_POSITION, // See comment for kNoPosition above.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100195 DEBUG_BREAK_SLOT, // Additional code inserted for debug break slot.
Steve Blocka7e24c12009-10-30 11:49:00 +0000196 EXTERNAL_REFERENCE, // The address of an external C++ function.
197 INTERNAL_REFERENCE, // An address inside the same function.
198
199 // add more as needed
200 // Pseudo-types
201 NUMBER_OF_MODES, // must be no greater than 14 - see RelocInfoWriter
202 NONE, // never recorded
203 LAST_CODE_ENUM = CODE_TARGET,
Leon Clarkef7060e22010-06-03 12:02:55 +0100204 LAST_GCED_ENUM = EMBEDDED_OBJECT
Steve Blocka7e24c12009-10-30 11:49:00 +0000205 };
206
207
208 RelocInfo() {}
209 RelocInfo(byte* pc, Mode rmode, intptr_t data)
210 : pc_(pc), rmode_(rmode), data_(data) {
211 }
212
213 static inline bool IsConstructCall(Mode mode) {
214 return mode == CONSTRUCT_CALL;
215 }
216 static inline bool IsCodeTarget(Mode mode) {
217 return mode <= LAST_CODE_ENUM;
218 }
219 // Is the relocation mode affected by GC?
220 static inline bool IsGCRelocMode(Mode mode) {
221 return mode <= LAST_GCED_ENUM;
222 }
223 static inline bool IsJSReturn(Mode mode) {
224 return mode == JS_RETURN;
225 }
226 static inline bool IsComment(Mode mode) {
227 return mode == COMMENT;
228 }
229 static inline bool IsPosition(Mode mode) {
230 return mode == POSITION || mode == STATEMENT_POSITION;
231 }
232 static inline bool IsStatementPosition(Mode mode) {
233 return mode == STATEMENT_POSITION;
234 }
235 static inline bool IsExternalReference(Mode mode) {
236 return mode == EXTERNAL_REFERENCE;
237 }
238 static inline bool IsInternalReference(Mode mode) {
239 return mode == INTERNAL_REFERENCE;
240 }
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100241 static inline bool IsDebugBreakSlot(Mode mode) {
242 return mode == DEBUG_BREAK_SLOT;
243 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000244 static inline int ModeMask(Mode mode) { return 1 << mode; }
245
246 // Accessors
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100247 byte* pc() const { return pc_; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000248 void set_pc(byte* pc) { pc_ = pc; }
249 Mode rmode() const { return rmode_; }
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100250 intptr_t data() const { return data_; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000251
252 // Apply a relocation by delta bytes
253 INLINE(void apply(intptr_t delta));
254
Leon Clarkef7060e22010-06-03 12:02:55 +0100255 // Is the pointer this relocation info refers to coded like a plain pointer
256 // or is it strange in some way (eg relative or patched into a series of
257 // instructions).
258 bool IsCodedSpecially();
259
Steve Blocka7e24c12009-10-30 11:49:00 +0000260 // Read/modify the code target in the branch/call instruction
261 // this relocation applies to;
262 // can only be called if IsCodeTarget(rmode_) || rmode_ == RUNTIME_ENTRY
263 INLINE(Address target_address());
264 INLINE(void set_target_address(Address target));
265 INLINE(Object* target_object());
Steve Block3ce2e202009-11-05 08:53:23 +0000266 INLINE(Handle<Object> target_object_handle(Assembler* origin));
Steve Blocka7e24c12009-10-30 11:49:00 +0000267 INLINE(Object** target_object_address());
268 INLINE(void set_target_object(Object* target));
Ben Murdochb0fe1622011-05-05 13:52:32 +0100269 INLINE(JSGlobalPropertyCell* target_cell());
270 INLINE(Handle<JSGlobalPropertyCell> target_cell_handle());
271 INLINE(void set_target_cell(JSGlobalPropertyCell* cell));
272
Steve Blocka7e24c12009-10-30 11:49:00 +0000273
Leon Clarkef7060e22010-06-03 12:02:55 +0100274 // Read the address of the word containing the target_address in an
275 // instruction stream. What this means exactly is architecture-independent.
276 // The only architecture-independent user of this function is the serializer.
277 // The serializer uses it to find out how many raw bytes of instruction to
278 // output before the next target. Architecture-independent code shouldn't
279 // dereference the pointer it gets back from this.
Steve Blocka7e24c12009-10-30 11:49:00 +0000280 INLINE(Address target_address_address());
Leon Clarkef7060e22010-06-03 12:02:55 +0100281 // This indicates how much space a target takes up when deserializing a code
282 // stream. For most architectures this is just the size of a pointer. For
283 // an instruction like movw/movt where the target bits are mixed into the
284 // instruction bits the size of the target will be zero, indicating that the
285 // serializer should not step forwards in memory after a target is resolved
286 // and written. In this case the target_address_address function above
287 // should return the end of the instructions to be patched, allowing the
288 // deserializer to deserialize the instructions as raw bytes and put them in
289 // place, ready to be patched with the target.
290 INLINE(int target_address_size());
Steve Blocka7e24c12009-10-30 11:49:00 +0000291
292 // Read/modify the reference in the instruction this relocation
293 // applies to; can only be called if rmode_ is external_reference
294 INLINE(Address* target_reference_address());
295
296 // Read/modify the address of a call instruction. This is used to relocate
297 // the break points where straight-line code is patched with a call
298 // instruction.
299 INLINE(Address call_address());
300 INLINE(void set_call_address(Address target));
301 INLINE(Object* call_object());
Steve Blocka7e24c12009-10-30 11:49:00 +0000302 INLINE(void set_call_object(Object* target));
Ben Murdochbb769b22010-08-11 14:56:33 +0100303 INLINE(Object** call_object_address());
Steve Blocka7e24c12009-10-30 11:49:00 +0000304
Iain Merrick75681382010-08-19 15:07:18 +0100305 template<typename StaticVisitor> inline void Visit();
Leon Clarkef7060e22010-06-03 12:02:55 +0100306 inline void Visit(ObjectVisitor* v);
307
Steve Blocka7e24c12009-10-30 11:49:00 +0000308 // Patch the code with some other code.
309 void PatchCode(byte* instructions, int instruction_count);
310
311 // Patch the code with a call.
312 void PatchCodeWithCall(Address target, int guard_bytes);
Steve Block3ce2e202009-11-05 08:53:23 +0000313
314 // Check whether this return sequence has been patched
315 // with a call to the debugger.
316 INLINE(bool IsPatchedReturnSequence());
Steve Blocka7e24c12009-10-30 11:49:00 +0000317
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100318 // Check whether this debug break slot has been patched with a call to the
319 // debugger.
320 INLINE(bool IsPatchedDebugBreakSlotSequence());
321
Steve Blocka7e24c12009-10-30 11:49:00 +0000322#ifdef ENABLE_DISASSEMBLER
323 // Printing
324 static const char* RelocModeName(Mode rmode);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100325 void Print(FILE* out);
Steve Blocka7e24c12009-10-30 11:49:00 +0000326#endif // ENABLE_DISASSEMBLER
327#ifdef DEBUG
328 // Debugging
329 void Verify();
330#endif
331
332 static const int kCodeTargetMask = (1 << (LAST_CODE_ENUM + 1)) - 1;
333 static const int kPositionMask = 1 << POSITION | 1 << STATEMENT_POSITION;
334 static const int kDebugMask = kPositionMask | 1 << COMMENT;
335 static const int kApplyMask; // Modes affected by apply. Depends on arch.
336
337 private:
338 // On ARM, note that pc_ is the address of the constant pool entry
339 // to be relocated and not the address of the instruction
340 // referencing the constant pool entry (except when rmode_ ==
341 // comment).
342 byte* pc_;
343 Mode rmode_;
344 intptr_t data_;
345 friend class RelocIterator;
346};
347
348
349// RelocInfoWriter serializes a stream of relocation info. It writes towards
350// lower addresses.
351class RelocInfoWriter BASE_EMBEDDED {
352 public:
353 RelocInfoWriter() : pos_(NULL), last_pc_(NULL), last_data_(0) {}
354 RelocInfoWriter(byte* pos, byte* pc) : pos_(pos), last_pc_(pc),
355 last_data_(0) {}
356
357 byte* pos() const { return pos_; }
358 byte* last_pc() const { return last_pc_; }
359
360 void Write(const RelocInfo* rinfo);
361
362 // Update the state of the stream after reloc info buffer
363 // and/or code is moved while the stream is active.
364 void Reposition(byte* pos, byte* pc) {
365 pos_ = pos;
366 last_pc_ = pc;
367 }
368
369 // Max size (bytes) of a written RelocInfo. Longest encoding is
370 // ExtraTag, VariableLengthPCJump, ExtraTag, pc_delta, ExtraTag, data_delta.
371 // On ia32 and arm this is 1 + 4 + 1 + 1 + 1 + 4 = 12.
372 // On x64 this is 1 + 4 + 1 + 1 + 1 + 8 == 16;
373 // Here we use the maximum of the two.
374 static const int kMaxSize = 16;
375
376 private:
377 inline uint32_t WriteVariableLengthPCJump(uint32_t pc_delta);
378 inline void WriteTaggedPC(uint32_t pc_delta, int tag);
379 inline void WriteExtraTaggedPC(uint32_t pc_delta, int extra_tag);
380 inline void WriteExtraTaggedData(intptr_t data_delta, int top_tag);
381 inline void WriteTaggedData(intptr_t data_delta, int tag);
382 inline void WriteExtraTag(int extra_tag, int top_tag);
383
384 byte* pos_;
385 byte* last_pc_;
386 intptr_t last_data_;
387 DISALLOW_COPY_AND_ASSIGN(RelocInfoWriter);
388};
389
390
391// A RelocIterator iterates over relocation information.
392// Typical use:
393//
394// for (RelocIterator it(code); !it.done(); it.next()) {
395// // do something with it.rinfo() here
396// }
397//
398// A mask can be specified to skip unwanted modes.
399class RelocIterator: public Malloced {
400 public:
401 // Create a new iterator positioned at
402 // the beginning of the reloc info.
403 // Relocation information with mode k is included in the
404 // iteration iff bit k of mode_mask is set.
405 explicit RelocIterator(Code* code, int mode_mask = -1);
406 explicit RelocIterator(const CodeDesc& desc, int mode_mask = -1);
407
408 // Iteration
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100409 bool done() const { return done_; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000410 void next();
411
412 // Return pointer valid until next next().
413 RelocInfo* rinfo() {
414 ASSERT(!done());
415 return &rinfo_;
416 }
417
418 private:
419 // Advance* moves the position before/after reading.
420 // *Read* reads from current byte(s) into rinfo_.
421 // *Get* just reads and returns info on current byte.
422 void Advance(int bytes = 1) { pos_ -= bytes; }
423 int AdvanceGetTag();
424 int GetExtraTag();
425 int GetTopTag();
426 void ReadTaggedPC();
427 void AdvanceReadPC();
428 void AdvanceReadData();
429 void AdvanceReadVariableLengthPCJump();
430 int GetPositionTypeTag();
431 void ReadTaggedData();
432
433 static RelocInfo::Mode DebugInfoModeFromTag(int tag);
434
435 // If the given mode is wanted, set it in rinfo_ and return true.
436 // Else return false. Used for efficiently skipping unwanted modes.
437 bool SetMode(RelocInfo::Mode mode) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100438 return (mode_mask_ & (1 << mode)) ? (rinfo_.rmode_ = mode, true) : false;
Steve Blocka7e24c12009-10-30 11:49:00 +0000439 }
440
441 byte* pos_;
442 byte* end_;
443 RelocInfo rinfo_;
444 bool done_;
445 int mode_mask_;
446 DISALLOW_COPY_AND_ASSIGN(RelocIterator);
447};
448
449
450//------------------------------------------------------------------------------
451// External function
452
453//----------------------------------------------------------------------------
454class IC_Utility;
455class SCTableReference;
456#ifdef ENABLE_DEBUGGER_SUPPORT
457class Debug_Address;
458#endif
459
460
461typedef void* ExternalReferenceRedirector(void* original, bool fp_return);
462
463
464// An ExternalReference represents a C++ address used in the generated
465// code. All references to C++ functions and variables must be encapsulated in
466// an ExternalReference instance. This is done in order to track the origin of
467// all external references in the code so that they can be bound to the correct
468// addresses when deserializing a heap.
469class ExternalReference BASE_EMBEDDED {
470 public:
471 explicit ExternalReference(Builtins::CFunctionId id);
472
Steve Blockd0582a62009-12-15 09:54:21 +0000473 explicit ExternalReference(ApiFunction* ptr);
474
Steve Blocka7e24c12009-10-30 11:49:00 +0000475 explicit ExternalReference(Builtins::Name name);
476
477 explicit ExternalReference(Runtime::FunctionId id);
478
479 explicit ExternalReference(Runtime::Function* f);
480
481 explicit ExternalReference(const IC_Utility& ic_utility);
482
483#ifdef ENABLE_DEBUGGER_SUPPORT
484 explicit ExternalReference(const Debug_Address& debug_address);
485#endif
486
487 explicit ExternalReference(StatsCounter* counter);
488
489 explicit ExternalReference(Top::AddressId id);
490
491 explicit ExternalReference(const SCTableReference& table_ref);
492
493 // One-of-a-kind references. These references are not part of a general
494 // pattern. This means that they have to be added to the
495 // ExternalReferenceTable in serialize.cc manually.
496
497 static ExternalReference perform_gc_function();
Steve Block6ded16b2010-05-10 14:33:55 +0100498 static ExternalReference fill_heap_number_with_random_function();
499 static ExternalReference random_uint32_function();
Andrei Popescu402d9372010-02-26 13:31:12 +0000500 static ExternalReference transcendental_cache_array_address();
John Reck59135872010-11-02 12:39:01 -0700501 static ExternalReference delete_handle_scope_extensions();
Steve Blocka7e24c12009-10-30 11:49:00 +0000502
Ben Murdochb0fe1622011-05-05 13:52:32 +0100503 // Deoptimization support.
504 static ExternalReference new_deoptimizer_function();
505 static ExternalReference compute_output_frames_function();
506 static ExternalReference global_contexts_list();
507
Leon Clarkee46be812010-01-19 14:06:41 +0000508 // Static data in the keyed lookup cache.
509 static ExternalReference keyed_lookup_cache_keys();
510 static ExternalReference keyed_lookup_cache_field_offsets();
511
Steve Blocka7e24c12009-10-30 11:49:00 +0000512 // Static variable Factory::the_hole_value.location()
513 static ExternalReference the_hole_value_location();
514
515 // Static variable Heap::roots_address()
516 static ExternalReference roots_address();
517
518 // Static variable StackGuard::address_of_jslimit()
Steve Blockd0582a62009-12-15 09:54:21 +0000519 static ExternalReference address_of_stack_limit();
520
521 // Static variable StackGuard::address_of_real_jslimit()
522 static ExternalReference address_of_real_stack_limit();
Steve Blocka7e24c12009-10-30 11:49:00 +0000523
524 // Static variable RegExpStack::limit_address()
525 static ExternalReference address_of_regexp_stack_limit();
526
Leon Clarkee46be812010-01-19 14:06:41 +0000527 // Static variables for RegExp.
528 static ExternalReference address_of_static_offsets_vector();
529 static ExternalReference address_of_regexp_stack_memory_address();
530 static ExternalReference address_of_regexp_stack_memory_size();
531
Steve Blocka7e24c12009-10-30 11:49:00 +0000532 // Static variable Heap::NewSpaceStart()
533 static ExternalReference new_space_start();
Andrei Popescu402d9372010-02-26 13:31:12 +0000534 static ExternalReference new_space_mask();
Steve Blocka7e24c12009-10-30 11:49:00 +0000535 static ExternalReference heap_always_allocate_scope_depth();
536
537 // Used for fast allocation in generated code.
538 static ExternalReference new_space_allocation_top_address();
539 static ExternalReference new_space_allocation_limit_address();
540
541 static ExternalReference double_fp_operation(Token::Value operation);
542 static ExternalReference compare_doubles();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100543 static ExternalReference power_double_double_function();
544 static ExternalReference power_double_int_function();
Steve Blocka7e24c12009-10-30 11:49:00 +0000545
Steve Blockd0582a62009-12-15 09:54:21 +0000546 static ExternalReference handle_scope_next_address();
547 static ExternalReference handle_scope_limit_address();
John Reck59135872010-11-02 12:39:01 -0700548 static ExternalReference handle_scope_level_address();
Steve Blockd0582a62009-12-15 09:54:21 +0000549
550 static ExternalReference scheduled_exception_address();
551
Ben Murdochb0fe1622011-05-05 13:52:32 +0100552 // Static variables containing common double constants.
553 static ExternalReference address_of_min_int();
554 static ExternalReference address_of_one_half();
555 static ExternalReference address_of_negative_infinity();
556
Steve Blocka7e24c12009-10-30 11:49:00 +0000557 Address address() const {return reinterpret_cast<Address>(address_);}
558
559#ifdef ENABLE_DEBUGGER_SUPPORT
560 // Function Debug::Break()
561 static ExternalReference debug_break();
562
563 // Used to check if single stepping is enabled in generated code.
564 static ExternalReference debug_step_in_fp_address();
565#endif
566
Steve Block6ded16b2010-05-10 14:33:55 +0100567#ifndef V8_INTERPRETED_REGEXP
Steve Blocka7e24c12009-10-30 11:49:00 +0000568 // C functions called from RegExp generated code.
569
570 // Function NativeRegExpMacroAssembler::CaseInsensitiveCompareUC16()
571 static ExternalReference re_case_insensitive_compare_uc16();
572
573 // Function RegExpMacroAssembler*::CheckStackGuardState()
574 static ExternalReference re_check_stack_guard_state();
575
576 // Function NativeRegExpMacroAssembler::GrowStack()
577 static ExternalReference re_grow_stack();
Leon Clarkee46be812010-01-19 14:06:41 +0000578
579 // byte NativeRegExpMacroAssembler::word_character_bitmap
580 static ExternalReference re_word_character_map();
581
Steve Blocka7e24c12009-10-30 11:49:00 +0000582#endif
583
584 // This lets you register a function that rewrites all external references.
585 // Used by the ARM simulator to catch calls to external references.
586 static void set_redirector(ExternalReferenceRedirector* redirector) {
587 ASSERT(redirector_ == NULL); // We can't stack them.
588 redirector_ = redirector;
589 }
590
591 private:
592 explicit ExternalReference(void* address)
593 : address_(address) {}
594
595 static ExternalReferenceRedirector* redirector_;
596
597 static void* Redirect(void* address, bool fp_return = false) {
598 if (redirector_ == NULL) return address;
Steve Blockd0582a62009-12-15 09:54:21 +0000599 void* answer = (*redirector_)(address, fp_return);
600 return answer;
Steve Blocka7e24c12009-10-30 11:49:00 +0000601 }
602
603 static void* Redirect(Address address_arg, bool fp_return = false) {
604 void* address = reinterpret_cast<void*>(address_arg);
Steve Blockd0582a62009-12-15 09:54:21 +0000605 void* answer = (redirector_ == NULL) ?
606 address :
607 (*redirector_)(address, fp_return);
608 return answer;
Steve Blocka7e24c12009-10-30 11:49:00 +0000609 }
610
611 void* address_;
612};
613
614
615// -----------------------------------------------------------------------------
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800616// Position recording support
617
Ben Murdochb0fe1622011-05-05 13:52:32 +0100618struct PositionState {
619 PositionState() : current_position(RelocInfo::kNoPosition),
620 written_position(RelocInfo::kNoPosition),
621 current_statement_position(RelocInfo::kNoPosition),
622 written_statement_position(RelocInfo::kNoPosition) {}
623
624 int current_position;
625 int written_position;
626
627 int current_statement_position;
628 int written_statement_position;
629};
630
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800631
632class PositionsRecorder BASE_EMBEDDED {
633 public:
634 explicit PositionsRecorder(Assembler* assembler)
Ben Murdochb0fe1622011-05-05 13:52:32 +0100635 : assembler_(assembler) {}
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800636
Ben Murdochb0fe1622011-05-05 13:52:32 +0100637 // Set current position to pos.
638 void RecordPosition(int pos);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800639
640 // Set current statement position to pos.
641 void RecordStatementPosition(int pos);
642
643 // Write recorded positions to relocation information.
644 bool WriteRecordedPositions();
645
Ben Murdochb0fe1622011-05-05 13:52:32 +0100646 int current_position() const { return state_.current_position; }
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800647
Ben Murdochb0fe1622011-05-05 13:52:32 +0100648 int current_statement_position() const {
649 return state_.current_statement_position;
650 }
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800651
652 private:
653 Assembler* assembler_;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100654 PositionState state_;
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800655
Ben Murdochb0fe1622011-05-05 13:52:32 +0100656 friend class PreservePositionScope;
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800657
Ben Murdochb0fe1622011-05-05 13:52:32 +0100658 DISALLOW_COPY_AND_ASSIGN(PositionsRecorder);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800659};
660
661
Ben Murdochb0fe1622011-05-05 13:52:32 +0100662class PreservePositionScope BASE_EMBEDDED {
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800663 public:
Ben Murdochb0fe1622011-05-05 13:52:32 +0100664 explicit PreservePositionScope(PositionsRecorder* positions_recorder)
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800665 : positions_recorder_(positions_recorder),
Ben Murdochb0fe1622011-05-05 13:52:32 +0100666 saved_state_(positions_recorder->state_) {}
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800667
Ben Murdochb0fe1622011-05-05 13:52:32 +0100668 ~PreservePositionScope() {
669 positions_recorder_->state_ = saved_state_;
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800670 }
671
672 private:
673 PositionsRecorder* positions_recorder_;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100674 const PositionState saved_state_;
675
676 DISALLOW_COPY_AND_ASSIGN(PreservePositionScope);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800677};
678
679
680// -----------------------------------------------------------------------------
Steve Blocka7e24c12009-10-30 11:49:00 +0000681// Utility functions
682
683static inline bool is_intn(int x, int n) {
684 return -(1 << (n-1)) <= x && x < (1 << (n-1));
685}
686
Steve Blocka7e24c12009-10-30 11:49:00 +0000687static inline bool is_int8(int x) { return is_intn(x, 8); }
Andrei Popescu31002712010-02-23 13:46:05 +0000688static inline bool is_int16(int x) { return is_intn(x, 16); }
689static inline bool is_int18(int x) { return is_intn(x, 18); }
690static inline bool is_int24(int x) { return is_intn(x, 24); }
Steve Blocka7e24c12009-10-30 11:49:00 +0000691
692static inline bool is_uintn(int x, int n) {
693 return (x & -(1 << n)) == 0;
694}
695
696static inline bool is_uint2(int x) { return is_uintn(x, 2); }
697static inline bool is_uint3(int x) { return is_uintn(x, 3); }
698static inline bool is_uint4(int x) { return is_uintn(x, 4); }
699static inline bool is_uint5(int x) { return is_uintn(x, 5); }
700static inline bool is_uint6(int x) { return is_uintn(x, 6); }
701static inline bool is_uint8(int x) { return is_uintn(x, 8); }
Andrei Popescu31002712010-02-23 13:46:05 +0000702static inline bool is_uint10(int x) { return is_uintn(x, 10); }
Steve Blocka7e24c12009-10-30 11:49:00 +0000703static inline bool is_uint12(int x) { return is_uintn(x, 12); }
704static inline bool is_uint16(int x) { return is_uintn(x, 16); }
705static inline bool is_uint24(int x) { return is_uintn(x, 24); }
Andrei Popescu31002712010-02-23 13:46:05 +0000706static inline bool is_uint26(int x) { return is_uintn(x, 26); }
707static inline bool is_uint28(int x) { return is_uintn(x, 28); }
708
709static inline int NumberOfBitsSet(uint32_t x) {
710 unsigned int num_bits_set;
711 for (num_bits_set = 0; x; x >>= 1) {
712 num_bits_set += x & 1;
713 }
714 return num_bits_set;
715}
Steve Blocka7e24c12009-10-30 11:49:00 +0000716
Ben Murdochb0fe1622011-05-05 13:52:32 +0100717// Computes pow(x, y) with the special cases in the spec for Math.pow.
718double power_double_int(double x, int y);
719double power_double_double(double x, double y);
720
Steve Blocka7e24c12009-10-30 11:49:00 +0000721} } // namespace v8::internal
722
723#endif // V8_ASSEMBLER_H_