blob: ec47d5712bae41a828b01fbb274f3453da79b251 [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"
40#include "zone-inl.h"
41#include "token.h"
42
43namespace v8 {
44namespace internal {
45
46
47// -----------------------------------------------------------------------------
48// Labels represent pc locations; they are typically jump or call targets.
49// After declaration, a label can be freely used to denote known or (yet)
50// unknown pc location. Assembler::bind() is used to bind a label to the
51// current pc. A label can be bound only once.
52
53class Label BASE_EMBEDDED {
54 public:
55 INLINE(Label()) { Unuse(); }
56 INLINE(~Label()) { ASSERT(!is_linked()); }
57
58 INLINE(void Unuse()) { pos_ = 0; }
59
60 INLINE(bool is_bound() const) { return pos_ < 0; }
61 INLINE(bool is_unused() const) { return pos_ == 0; }
62 INLINE(bool is_linked() const) { return pos_ > 0; }
63
64 // Returns the position of bound or linked labels. Cannot be used
65 // for unused labels.
66 int pos() const;
67
68 private:
69 // pos_ encodes both the binding state (via its sign)
70 // and the binding position (via its value) of a label.
71 //
72 // pos_ < 0 bound label, pos() returns the jump target position
73 // pos_ == 0 unused label
74 // pos_ > 0 linked label, pos() returns the last reference position
75 int pos_;
76
77 void bind_to(int pos) {
78 pos_ = -pos - 1;
79 ASSERT(is_bound());
80 }
81 void link_to(int pos) {
82 pos_ = pos + 1;
83 ASSERT(is_linked());
84 }
85
86 friend class Assembler;
87 friend class RegexpAssembler;
88 friend class Displacement;
89 friend class ShadowTarget;
90 friend class RegExpMacroAssemblerIrregexp;
91};
92
93
94// -----------------------------------------------------------------------------
95// Relocation information
96
97
98// Relocation information consists of the address (pc) of the datum
99// to which the relocation information applies, the relocation mode
100// (rmode), and an optional data field. The relocation mode may be
101// "descriptive" and not indicate a need for relocation, but simply
102// describe a property of the datum. Such rmodes are useful for GC
103// and nice disassembly output.
104
105class RelocInfo BASE_EMBEDDED {
106 public:
107 // The constant kNoPosition is used with the collecting of source positions
108 // in the relocation information. Two types of source positions are collected
109 // "position" (RelocMode position) and "statement position" (RelocMode
110 // statement_position). The "position" is collected at places in the source
111 // code which are of interest when making stack traces to pin-point the source
112 // location of a stack frame as close as possible. The "statement position" is
113 // collected at the beginning at each statement, and is used to indicate
114 // possible break locations. kNoPosition is used to indicate an
115 // invalid/uninitialized position value.
116 static const int kNoPosition = -1;
117
118 enum Mode {
119 // Please note the order is important (see IsCodeTarget, IsGCRelocMode).
120 CONSTRUCT_CALL, // code target that is a call to a JavaScript constructor.
121 CODE_TARGET_CONTEXT, // code target used for contextual loads.
122 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 Blocka7e24c12009-10-30 11:49:00 +0000401 static ExternalReference random_positive_smi_function();
402
Leon Clarkee46be812010-01-19 14:06:41 +0000403 // Static data in the keyed lookup cache.
404 static ExternalReference keyed_lookup_cache_keys();
405 static ExternalReference keyed_lookup_cache_field_offsets();
406
Steve Blocka7e24c12009-10-30 11:49:00 +0000407 // Static variable Factory::the_hole_value.location()
408 static ExternalReference the_hole_value_location();
409
410 // Static variable Heap::roots_address()
411 static ExternalReference roots_address();
412
413 // Static variable StackGuard::address_of_jslimit()
Steve Blockd0582a62009-12-15 09:54:21 +0000414 static ExternalReference address_of_stack_limit();
415
416 // Static variable StackGuard::address_of_real_jslimit()
417 static ExternalReference address_of_real_stack_limit();
Steve Blocka7e24c12009-10-30 11:49:00 +0000418
419 // Static variable RegExpStack::limit_address()
420 static ExternalReference address_of_regexp_stack_limit();
421
Leon Clarkee46be812010-01-19 14:06:41 +0000422 // Static variables for RegExp.
423 static ExternalReference address_of_static_offsets_vector();
424 static ExternalReference address_of_regexp_stack_memory_address();
425 static ExternalReference address_of_regexp_stack_memory_size();
426
Steve Blocka7e24c12009-10-30 11:49:00 +0000427 // Static variable Heap::NewSpaceStart()
428 static ExternalReference new_space_start();
429 static ExternalReference heap_always_allocate_scope_depth();
430
431 // Used for fast allocation in generated code.
432 static ExternalReference new_space_allocation_top_address();
433 static ExternalReference new_space_allocation_limit_address();
434
435 static ExternalReference double_fp_operation(Token::Value operation);
436 static ExternalReference compare_doubles();
437
Steve Blockd0582a62009-12-15 09:54:21 +0000438 static ExternalReference handle_scope_extensions_address();
439 static ExternalReference handle_scope_next_address();
440 static ExternalReference handle_scope_limit_address();
441
442 static ExternalReference scheduled_exception_address();
443
Steve Blocka7e24c12009-10-30 11:49:00 +0000444 Address address() const {return reinterpret_cast<Address>(address_);}
445
446#ifdef ENABLE_DEBUGGER_SUPPORT
447 // Function Debug::Break()
448 static ExternalReference debug_break();
449
450 // Used to check if single stepping is enabled in generated code.
451 static ExternalReference debug_step_in_fp_address();
452#endif
453
454#ifdef V8_NATIVE_REGEXP
455 // C functions called from RegExp generated code.
456
457 // Function NativeRegExpMacroAssembler::CaseInsensitiveCompareUC16()
458 static ExternalReference re_case_insensitive_compare_uc16();
459
460 // Function RegExpMacroAssembler*::CheckStackGuardState()
461 static ExternalReference re_check_stack_guard_state();
462
463 // Function NativeRegExpMacroAssembler::GrowStack()
464 static ExternalReference re_grow_stack();
Leon Clarkee46be812010-01-19 14:06:41 +0000465
466 // byte NativeRegExpMacroAssembler::word_character_bitmap
467 static ExternalReference re_word_character_map();
468
Steve Blocka7e24c12009-10-30 11:49:00 +0000469#endif
470
471 // This lets you register a function that rewrites all external references.
472 // Used by the ARM simulator to catch calls to external references.
473 static void set_redirector(ExternalReferenceRedirector* redirector) {
474 ASSERT(redirector_ == NULL); // We can't stack them.
475 redirector_ = redirector;
476 }
477
478 private:
479 explicit ExternalReference(void* address)
480 : address_(address) {}
481
482 static ExternalReferenceRedirector* redirector_;
483
484 static void* Redirect(void* address, bool fp_return = false) {
485 if (redirector_ == NULL) return address;
Steve Blockd0582a62009-12-15 09:54:21 +0000486 void* answer = (*redirector_)(address, fp_return);
487 return answer;
Steve Blocka7e24c12009-10-30 11:49:00 +0000488 }
489
490 static void* Redirect(Address address_arg, bool fp_return = false) {
491 void* address = reinterpret_cast<void*>(address_arg);
Steve Blockd0582a62009-12-15 09:54:21 +0000492 void* answer = (redirector_ == NULL) ?
493 address :
494 (*redirector_)(address, fp_return);
495 return answer;
Steve Blocka7e24c12009-10-30 11:49:00 +0000496 }
497
498 void* address_;
499};
500
501
502// -----------------------------------------------------------------------------
503// Utility functions
504
505static inline bool is_intn(int x, int n) {
506 return -(1 << (n-1)) <= x && x < (1 << (n-1));
507}
508
509static inline bool is_int24(int x) { return is_intn(x, 24); }
510static inline bool is_int8(int x) { return is_intn(x, 8); }
511
512static inline bool is_uintn(int x, int n) {
513 return (x & -(1 << n)) == 0;
514}
515
516static inline bool is_uint2(int x) { return is_uintn(x, 2); }
517static inline bool is_uint3(int x) { return is_uintn(x, 3); }
518static inline bool is_uint4(int x) { return is_uintn(x, 4); }
519static inline bool is_uint5(int x) { return is_uintn(x, 5); }
520static inline bool is_uint6(int x) { return is_uintn(x, 6); }
521static inline bool is_uint8(int x) { return is_uintn(x, 8); }
522static inline bool is_uint12(int x) { return is_uintn(x, 12); }
523static inline bool is_uint16(int x) { return is_uintn(x, 16); }
524static inline bool is_uint24(int x) { return is_uintn(x, 24); }
525
526} } // namespace v8::internal
527
528#endif // V8_ASSEMBLER_H_