blob: 03a2f8ea1c1a3b01aa5e420a26caf7147cd84d23 [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// -----------------------------------------------------------------------------
47// Labels represent pc locations; they are typically jump or call targets.
48// After declaration, a label can be freely used to denote known or (yet)
49// unknown pc location. Assembler::bind() is used to bind a label to the
50// current pc. A label can be bound only once.
51
52class Label BASE_EMBEDDED {
53 public:
54 INLINE(Label()) { Unuse(); }
55 INLINE(~Label()) { ASSERT(!is_linked()); }
56
57 INLINE(void Unuse()) { pos_ = 0; }
58
59 INLINE(bool is_bound() const) { return pos_ < 0; }
60 INLINE(bool is_unused() const) { return pos_ == 0; }
61 INLINE(bool is_linked() const) { return pos_ > 0; }
62
63 // Returns the position of bound or linked labels. Cannot be used
64 // for unused labels.
65 int pos() const;
66
67 private:
68 // pos_ encodes both the binding state (via its sign)
69 // and the binding position (via its value) of a label.
70 //
71 // pos_ < 0 bound label, pos() returns the jump target position
72 // pos_ == 0 unused label
73 // pos_ > 0 linked label, pos() returns the last reference position
74 int pos_;
75
76 void bind_to(int pos) {
77 pos_ = -pos - 1;
78 ASSERT(is_bound());
79 }
80 void link_to(int pos) {
81 pos_ = pos + 1;
82 ASSERT(is_linked());
83 }
84
85 friend class Assembler;
86 friend class RegexpAssembler;
87 friend class Displacement;
88 friend class ShadowTarget;
89 friend class RegExpMacroAssemblerIrregexp;
90};
91
92
93// -----------------------------------------------------------------------------
94// Relocation information
95
96
97// Relocation information consists of the address (pc) of the datum
98// to which the relocation information applies, the relocation mode
99// (rmode), and an optional data field. The relocation mode may be
100// "descriptive" and not indicate a need for relocation, but simply
101// describe a property of the datum. Such rmodes are useful for GC
102// and nice disassembly output.
103
104class RelocInfo BASE_EMBEDDED {
105 public:
106 // The constant kNoPosition is used with the collecting of source positions
107 // in the relocation information. Two types of source positions are collected
108 // "position" (RelocMode position) and "statement position" (RelocMode
109 // statement_position). The "position" is collected at places in the source
110 // code which are of interest when making stack traces to pin-point the source
111 // location of a stack frame as close as possible. The "statement position" is
112 // collected at the beginning at each statement, and is used to indicate
113 // possible break locations. kNoPosition is used to indicate an
114 // invalid/uninitialized position value.
115 static const int kNoPosition = -1;
116
117 enum Mode {
118 // Please note the order is important (see IsCodeTarget, IsGCRelocMode).
119 CONSTRUCT_CALL, // code target that is a call to a JavaScript constructor.
120 CODE_TARGET_CONTEXT, // code target used for contextual loads.
Andrei Popescu402d9372010-02-26 13:31:12 +0000121 DEBUG_BREAK,
Steve Blocka7e24c12009-10-30 11:49:00 +0000122 CODE_TARGET, // code target which is not any of the above.
123 EMBEDDED_OBJECT,
124 EMBEDDED_STRING,
125
126 // Everything after runtime_entry (inclusive) is not GC'ed.
127 RUNTIME_ENTRY,
128 JS_RETURN, // Marks start of the ExitJSFrame code.
129 COMMENT,
130 POSITION, // See comment for kNoPosition above.
131 STATEMENT_POSITION, // See comment for kNoPosition above.
132 EXTERNAL_REFERENCE, // The address of an external C++ function.
133 INTERNAL_REFERENCE, // An address inside the same function.
134
135 // add more as needed
136 // Pseudo-types
137 NUMBER_OF_MODES, // must be no greater than 14 - see RelocInfoWriter
138 NONE, // never recorded
139 LAST_CODE_ENUM = CODE_TARGET,
140 LAST_GCED_ENUM = EMBEDDED_STRING
141 };
142
143
144 RelocInfo() {}
145 RelocInfo(byte* pc, Mode rmode, intptr_t data)
146 : pc_(pc), rmode_(rmode), data_(data) {
147 }
148
149 static inline bool IsConstructCall(Mode mode) {
150 return mode == CONSTRUCT_CALL;
151 }
152 static inline bool IsCodeTarget(Mode mode) {
153 return mode <= LAST_CODE_ENUM;
154 }
155 // Is the relocation mode affected by GC?
156 static inline bool IsGCRelocMode(Mode mode) {
157 return mode <= LAST_GCED_ENUM;
158 }
159 static inline bool IsJSReturn(Mode mode) {
160 return mode == JS_RETURN;
161 }
162 static inline bool IsComment(Mode mode) {
163 return mode == COMMENT;
164 }
165 static inline bool IsPosition(Mode mode) {
166 return mode == POSITION || mode == STATEMENT_POSITION;
167 }
168 static inline bool IsStatementPosition(Mode mode) {
169 return mode == STATEMENT_POSITION;
170 }
171 static inline bool IsExternalReference(Mode mode) {
172 return mode == EXTERNAL_REFERENCE;
173 }
174 static inline bool IsInternalReference(Mode mode) {
175 return mode == INTERNAL_REFERENCE;
176 }
177 static inline int ModeMask(Mode mode) { return 1 << mode; }
178
179 // Accessors
180 byte* pc() const { return pc_; }
181 void set_pc(byte* pc) { pc_ = pc; }
182 Mode rmode() const { return rmode_; }
183 intptr_t data() const { return data_; }
184
185 // Apply a relocation by delta bytes
186 INLINE(void apply(intptr_t delta));
187
188 // Read/modify the code target in the branch/call instruction
189 // this relocation applies to;
190 // can only be called if IsCodeTarget(rmode_) || rmode_ == RUNTIME_ENTRY
191 INLINE(Address target_address());
192 INLINE(void set_target_address(Address target));
193 INLINE(Object* target_object());
Steve Block3ce2e202009-11-05 08:53:23 +0000194 INLINE(Handle<Object> target_object_handle(Assembler* origin));
Steve Blocka7e24c12009-10-30 11:49:00 +0000195 INLINE(Object** target_object_address());
196 INLINE(void set_target_object(Object* target));
197
198 // Read the address of the word containing the target_address. Can only
199 // be called if IsCodeTarget(rmode_) || rmode_ == RUNTIME_ENTRY.
200 INLINE(Address target_address_address());
201
202 // Read/modify the reference in the instruction this relocation
203 // applies to; can only be called if rmode_ is external_reference
204 INLINE(Address* target_reference_address());
205
206 // Read/modify the address of a call instruction. This is used to relocate
207 // the break points where straight-line code is patched with a call
208 // instruction.
209 INLINE(Address call_address());
210 INLINE(void set_call_address(Address target));
211 INLINE(Object* call_object());
212 INLINE(Object** call_object_address());
213 INLINE(void set_call_object(Object* target));
214
215 // Patch the code with some other code.
216 void PatchCode(byte* instructions, int instruction_count);
217
218 // Patch the code with a call.
219 void PatchCodeWithCall(Address target, int guard_bytes);
Steve Block3ce2e202009-11-05 08:53:23 +0000220
221 // Check whether this return sequence has been patched
222 // with a call to the debugger.
223 INLINE(bool IsPatchedReturnSequence());
Steve Blocka7e24c12009-10-30 11:49:00 +0000224
225#ifdef ENABLE_DISASSEMBLER
226 // Printing
227 static const char* RelocModeName(Mode rmode);
228 void Print();
229#endif // ENABLE_DISASSEMBLER
230#ifdef DEBUG
231 // Debugging
232 void Verify();
233#endif
234
235 static const int kCodeTargetMask = (1 << (LAST_CODE_ENUM + 1)) - 1;
236 static const int kPositionMask = 1 << POSITION | 1 << STATEMENT_POSITION;
237 static const int kDebugMask = kPositionMask | 1 << COMMENT;
238 static const int kApplyMask; // Modes affected by apply. Depends on arch.
239
240 private:
241 // On ARM, note that pc_ is the address of the constant pool entry
242 // to be relocated and not the address of the instruction
243 // referencing the constant pool entry (except when rmode_ ==
244 // comment).
245 byte* pc_;
246 Mode rmode_;
247 intptr_t data_;
248 friend class RelocIterator;
249};
250
251
252// RelocInfoWriter serializes a stream of relocation info. It writes towards
253// lower addresses.
254class RelocInfoWriter BASE_EMBEDDED {
255 public:
256 RelocInfoWriter() : pos_(NULL), last_pc_(NULL), last_data_(0) {}
257 RelocInfoWriter(byte* pos, byte* pc) : pos_(pos), last_pc_(pc),
258 last_data_(0) {}
259
260 byte* pos() const { return pos_; }
261 byte* last_pc() const { return last_pc_; }
262
263 void Write(const RelocInfo* rinfo);
264
265 // Update the state of the stream after reloc info buffer
266 // and/or code is moved while the stream is active.
267 void Reposition(byte* pos, byte* pc) {
268 pos_ = pos;
269 last_pc_ = pc;
270 }
271
272 // Max size (bytes) of a written RelocInfo. Longest encoding is
273 // ExtraTag, VariableLengthPCJump, ExtraTag, pc_delta, ExtraTag, data_delta.
274 // On ia32 and arm this is 1 + 4 + 1 + 1 + 1 + 4 = 12.
275 // On x64 this is 1 + 4 + 1 + 1 + 1 + 8 == 16;
276 // Here we use the maximum of the two.
277 static const int kMaxSize = 16;
278
279 private:
280 inline uint32_t WriteVariableLengthPCJump(uint32_t pc_delta);
281 inline void WriteTaggedPC(uint32_t pc_delta, int tag);
282 inline void WriteExtraTaggedPC(uint32_t pc_delta, int extra_tag);
283 inline void WriteExtraTaggedData(intptr_t data_delta, int top_tag);
284 inline void WriteTaggedData(intptr_t data_delta, int tag);
285 inline void WriteExtraTag(int extra_tag, int top_tag);
286
287 byte* pos_;
288 byte* last_pc_;
289 intptr_t last_data_;
290 DISALLOW_COPY_AND_ASSIGN(RelocInfoWriter);
291};
292
293
294// A RelocIterator iterates over relocation information.
295// Typical use:
296//
297// for (RelocIterator it(code); !it.done(); it.next()) {
298// // do something with it.rinfo() here
299// }
300//
301// A mask can be specified to skip unwanted modes.
302class RelocIterator: public Malloced {
303 public:
304 // Create a new iterator positioned at
305 // the beginning of the reloc info.
306 // Relocation information with mode k is included in the
307 // iteration iff bit k of mode_mask is set.
308 explicit RelocIterator(Code* code, int mode_mask = -1);
309 explicit RelocIterator(const CodeDesc& desc, int mode_mask = -1);
310
311 // Iteration
312 bool done() const { return done_; }
313 void next();
314
315 // Return pointer valid until next next().
316 RelocInfo* rinfo() {
317 ASSERT(!done());
318 return &rinfo_;
319 }
320
321 private:
322 // Advance* moves the position before/after reading.
323 // *Read* reads from current byte(s) into rinfo_.
324 // *Get* just reads and returns info on current byte.
325 void Advance(int bytes = 1) { pos_ -= bytes; }
326 int AdvanceGetTag();
327 int GetExtraTag();
328 int GetTopTag();
329 void ReadTaggedPC();
330 void AdvanceReadPC();
331 void AdvanceReadData();
332 void AdvanceReadVariableLengthPCJump();
333 int GetPositionTypeTag();
334 void ReadTaggedData();
335
336 static RelocInfo::Mode DebugInfoModeFromTag(int tag);
337
338 // If the given mode is wanted, set it in rinfo_ and return true.
339 // Else return false. Used for efficiently skipping unwanted modes.
340 bool SetMode(RelocInfo::Mode mode) {
341 return (mode_mask_ & 1 << mode) ? (rinfo_.rmode_ = mode, true) : false;
342 }
343
344 byte* pos_;
345 byte* end_;
346 RelocInfo rinfo_;
347 bool done_;
348 int mode_mask_;
349 DISALLOW_COPY_AND_ASSIGN(RelocIterator);
350};
351
352
353//------------------------------------------------------------------------------
354// External function
355
356//----------------------------------------------------------------------------
357class IC_Utility;
358class SCTableReference;
359#ifdef ENABLE_DEBUGGER_SUPPORT
360class Debug_Address;
361#endif
362
363
364typedef void* ExternalReferenceRedirector(void* original, bool fp_return);
365
366
367// An ExternalReference represents a C++ address used in the generated
368// code. All references to C++ functions and variables must be encapsulated in
369// an ExternalReference instance. This is done in order to track the origin of
370// all external references in the code so that they can be bound to the correct
371// addresses when deserializing a heap.
372class ExternalReference BASE_EMBEDDED {
373 public:
374 explicit ExternalReference(Builtins::CFunctionId id);
375
Steve Blockd0582a62009-12-15 09:54:21 +0000376 explicit ExternalReference(ApiFunction* ptr);
377
Steve Blocka7e24c12009-10-30 11:49:00 +0000378 explicit ExternalReference(Builtins::Name name);
379
380 explicit ExternalReference(Runtime::FunctionId id);
381
382 explicit ExternalReference(Runtime::Function* f);
383
384 explicit ExternalReference(const IC_Utility& ic_utility);
385
386#ifdef ENABLE_DEBUGGER_SUPPORT
387 explicit ExternalReference(const Debug_Address& debug_address);
388#endif
389
390 explicit ExternalReference(StatsCounter* counter);
391
392 explicit ExternalReference(Top::AddressId id);
393
394 explicit ExternalReference(const SCTableReference& table_ref);
395
396 // One-of-a-kind references. These references are not part of a general
397 // pattern. This means that they have to be added to the
398 // ExternalReferenceTable in serialize.cc manually.
399
400 static ExternalReference perform_gc_function();
Steve Block6ded16b2010-05-10 14:33:55 +0100401 static ExternalReference fill_heap_number_with_random_function();
402 static ExternalReference random_uint32_function();
Andrei Popescu402d9372010-02-26 13:31:12 +0000403 static ExternalReference transcendental_cache_array_address();
Steve Blocka7e24c12009-10-30 11:49:00 +0000404
Leon Clarkee46be812010-01-19 14:06:41 +0000405 // Static data in the keyed lookup cache.
406 static ExternalReference keyed_lookup_cache_keys();
407 static ExternalReference keyed_lookup_cache_field_offsets();
408
Steve Blocka7e24c12009-10-30 11:49:00 +0000409 // Static variable Factory::the_hole_value.location()
410 static ExternalReference the_hole_value_location();
411
412 // Static variable Heap::roots_address()
413 static ExternalReference roots_address();
414
415 // Static variable StackGuard::address_of_jslimit()
Steve Blockd0582a62009-12-15 09:54:21 +0000416 static ExternalReference address_of_stack_limit();
417
418 // Static variable StackGuard::address_of_real_jslimit()
419 static ExternalReference address_of_real_stack_limit();
Steve Blocka7e24c12009-10-30 11:49:00 +0000420
421 // Static variable RegExpStack::limit_address()
422 static ExternalReference address_of_regexp_stack_limit();
423
Leon Clarkee46be812010-01-19 14:06:41 +0000424 // Static variables for RegExp.
425 static ExternalReference address_of_static_offsets_vector();
426 static ExternalReference address_of_regexp_stack_memory_address();
427 static ExternalReference address_of_regexp_stack_memory_size();
428
Steve Blocka7e24c12009-10-30 11:49:00 +0000429 // Static variable Heap::NewSpaceStart()
430 static ExternalReference new_space_start();
Andrei Popescu402d9372010-02-26 13:31:12 +0000431 static ExternalReference new_space_mask();
Steve Blocka7e24c12009-10-30 11:49:00 +0000432 static ExternalReference heap_always_allocate_scope_depth();
433
434 // Used for fast allocation in generated code.
435 static ExternalReference new_space_allocation_top_address();
436 static ExternalReference new_space_allocation_limit_address();
437
438 static ExternalReference double_fp_operation(Token::Value operation);
439 static ExternalReference compare_doubles();
440
Steve Blockd0582a62009-12-15 09:54:21 +0000441 static ExternalReference handle_scope_extensions_address();
442 static ExternalReference handle_scope_next_address();
443 static ExternalReference handle_scope_limit_address();
444
445 static ExternalReference scheduled_exception_address();
446
Steve Block6ded16b2010-05-10 14:33:55 +0100447 static ExternalReference compile_array_pop_call();
448 static ExternalReference compile_array_push_call();
449
Steve Blocka7e24c12009-10-30 11:49:00 +0000450 Address address() const {return reinterpret_cast<Address>(address_);}
451
452#ifdef ENABLE_DEBUGGER_SUPPORT
453 // Function Debug::Break()
454 static ExternalReference debug_break();
455
456 // Used to check if single stepping is enabled in generated code.
457 static ExternalReference debug_step_in_fp_address();
458#endif
459
Steve Block6ded16b2010-05-10 14:33:55 +0100460#ifndef V8_INTERPRETED_REGEXP
Steve Blocka7e24c12009-10-30 11:49:00 +0000461 // C functions called from RegExp generated code.
462
463 // Function NativeRegExpMacroAssembler::CaseInsensitiveCompareUC16()
464 static ExternalReference re_case_insensitive_compare_uc16();
465
466 // Function RegExpMacroAssembler*::CheckStackGuardState()
467 static ExternalReference re_check_stack_guard_state();
468
469 // Function NativeRegExpMacroAssembler::GrowStack()
470 static ExternalReference re_grow_stack();
Leon Clarkee46be812010-01-19 14:06:41 +0000471
472 // byte NativeRegExpMacroAssembler::word_character_bitmap
473 static ExternalReference re_word_character_map();
474
Steve Blocka7e24c12009-10-30 11:49:00 +0000475#endif
476
477 // This lets you register a function that rewrites all external references.
478 // Used by the ARM simulator to catch calls to external references.
479 static void set_redirector(ExternalReferenceRedirector* redirector) {
480 ASSERT(redirector_ == NULL); // We can't stack them.
481 redirector_ = redirector;
482 }
483
484 private:
485 explicit ExternalReference(void* address)
486 : address_(address) {}
487
488 static ExternalReferenceRedirector* redirector_;
489
490 static void* Redirect(void* address, bool fp_return = false) {
491 if (redirector_ == NULL) return address;
Steve Blockd0582a62009-12-15 09:54:21 +0000492 void* answer = (*redirector_)(address, fp_return);
493 return answer;
Steve Blocka7e24c12009-10-30 11:49:00 +0000494 }
495
496 static void* Redirect(Address address_arg, bool fp_return = false) {
497 void* address = reinterpret_cast<void*>(address_arg);
Steve Blockd0582a62009-12-15 09:54:21 +0000498 void* answer = (redirector_ == NULL) ?
499 address :
500 (*redirector_)(address, fp_return);
501 return answer;
Steve Blocka7e24c12009-10-30 11:49:00 +0000502 }
503
504 void* address_;
505};
506
507
508// -----------------------------------------------------------------------------
509// Utility functions
510
511static inline bool is_intn(int x, int n) {
512 return -(1 << (n-1)) <= x && x < (1 << (n-1));
513}
514
Steve Blocka7e24c12009-10-30 11:49:00 +0000515static inline bool is_int8(int x) { return is_intn(x, 8); }
Andrei Popescu31002712010-02-23 13:46:05 +0000516static inline bool is_int16(int x) { return is_intn(x, 16); }
517static inline bool is_int18(int x) { return is_intn(x, 18); }
518static inline bool is_int24(int x) { return is_intn(x, 24); }
Steve Blocka7e24c12009-10-30 11:49:00 +0000519
520static inline bool is_uintn(int x, int n) {
521 return (x & -(1 << n)) == 0;
522}
523
524static inline bool is_uint2(int x) { return is_uintn(x, 2); }
525static inline bool is_uint3(int x) { return is_uintn(x, 3); }
526static inline bool is_uint4(int x) { return is_uintn(x, 4); }
527static inline bool is_uint5(int x) { return is_uintn(x, 5); }
528static inline bool is_uint6(int x) { return is_uintn(x, 6); }
529static inline bool is_uint8(int x) { return is_uintn(x, 8); }
Andrei Popescu31002712010-02-23 13:46:05 +0000530static inline bool is_uint10(int x) { return is_uintn(x, 10); }
Steve Blocka7e24c12009-10-30 11:49:00 +0000531static inline bool is_uint12(int x) { return is_uintn(x, 12); }
532static inline bool is_uint16(int x) { return is_uintn(x, 16); }
533static inline bool is_uint24(int x) { return is_uintn(x, 24); }
Andrei Popescu31002712010-02-23 13:46:05 +0000534static inline bool is_uint26(int x) { return is_uintn(x, 26); }
535static inline bool is_uint28(int x) { return is_uintn(x, 28); }
536
537static inline int NumberOfBitsSet(uint32_t x) {
538 unsigned int num_bits_set;
539 for (num_bits_set = 0; x; x >>= 1) {
540 num_bits_set += x & 1;
541 }
542 return num_bits_set;
543}
Steve Blocka7e24c12009-10-30 11:49:00 +0000544
545} } // namespace v8::internal
546
547#endif // V8_ASSEMBLER_H_