blob: 971b95022781a288e799cb216dded820f5f18bc3 [file] [log] [blame]
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +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.
ager@chromium.org9258b6b2008-09-11 09:11:10 +000033// Copyright 2006-2008 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000034
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
42namespace v8 { namespace internal {
43
44
45// -----------------------------------------------------------------------------
46// Labels represent pc locations; they are typically jump or call targets.
47// After declaration, a label can be freely used to denote known or (yet)
48// unknown pc location. Assembler::bind() is used to bind a label to the
49// current pc. A label can be bound only once.
50
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +000051class Label : public ZoneObject { // LabelShadows are dynamically allocated.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000052 public:
53 INLINE(Label()) { Unuse(); }
54 INLINE(~Label()) { ASSERT(!is_linked()); }
55
56 INLINE(void Unuse()) { pos_ = 0; }
57
58 INLINE(bool is_bound() const) { return pos_ < 0; }
59 INLINE(bool is_unused() const) { return pos_ == 0; }
60 INLINE(bool is_linked() const) { return pos_ > 0; }
61
62 // Returns the position of bound or linked labels. Cannot be used
63 // for unused labels.
64 int pos() const;
65
66 private:
67 // pos_ encodes both the binding state (via its sign)
68 // and the binding position (via its value) of a label.
69 //
70 // pos_ < 0 bound label, pos() returns the jump target position
71 // pos_ == 0 unused label
72 // pos_ > 0 linked label, pos() returns the last reference position
73 int pos_;
74
75 void bind_to(int pos) {
76 pos_ = -pos - 1;
77 ASSERT(is_bound());
78 }
79 void link_to(int pos) {
80 pos_ = pos + 1;
81 ASSERT(is_linked());
82 }
83
84 friend class Assembler;
ager@chromium.orga74f0da2008-12-03 16:05:52 +000085 friend class RegexpAssembler;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000086 friend class Displacement;
87 friend class LabelShadow;
ager@chromium.orga74f0da2008-12-03 16:05:52 +000088 friend class RegExpMacroAssemblerIrregexp;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000089};
90
91
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +000092// A LabelShadow represents a label that is temporarily shadowed by another
93// label (represented by the original label during shadowing). They are used
94// to catch jumps to labels in certain contexts, e.g. try blocks. After
95// shadowing ends, the formerly shadowed label is again represented by the
96// original label and the LabelShadow can be used as a label in its own
97// right, representing the formerly shadowing label.
98class LabelShadow : public Label {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000099 public:
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000100 explicit LabelShadow(Label* original) {
101 ASSERT(original != NULL);
102 original_label_ = original;
103 original_pos_ = original->pos_;
104 original->Unuse();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000105#ifdef DEBUG
106 is_shadowing_ = true;
107#endif
108 }
109
110 ~LabelShadow() {
111 ASSERT(!is_shadowing_);
112 }
113
114 void StopShadowing() {
115 ASSERT(is_shadowing_ && is_unused());
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000116 pos_ = original_label_->pos_;
117 original_label_->pos_ = original_pos_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000118#ifdef DEBUG
119 is_shadowing_ = false;
120#endif
121 }
122
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000123 Label* original_label() const { return original_label_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000124
125 private:
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000126 // During shadowing, the currently shadowing label. After shadowing, the
127 // label that was shadowed.
128 Label* original_label_;
129
130 // During shadowing, the saved state of the original label.
131 int original_pos_;
132
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000133#ifdef DEBUG
134 bool is_shadowing_;
135#endif
136};
137
138
139// -----------------------------------------------------------------------------
140// Relocation information
141
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000142
143// Relocation information consists of the address (pc) of the datum
144// to which the relocation information applies, the relocation mode
145// (rmode), and an optional data field. The relocation mode may be
146// "descriptive" and not indicate a need for relocation, but simply
147// describe a property of the datum. Such rmodes are useful for GC
148// and nice disassembly output.
149
150class RelocInfo BASE_EMBEDDED {
151 public:
ager@chromium.org236ad962008-09-25 09:45:57 +0000152 // The constant kNoPosition is used with the collecting of source positions
153 // in the relocation information. Two types of source positions are collected
154 // "position" (RelocMode position) and "statement position" (RelocMode
155 // statement_position). The "position" is collected at places in the source
156 // code which are of interest when making stack traces to pin-point the source
157 // location of a stack frame as close as possible. The "statement position" is
158 // collected at the beginning at each statement, and is used to indicate
159 // possible break locations. kNoPosition is used to indicate an
160 // invalid/uninitialized position value.
161 static const int kNoPosition = -1;
162
163 enum Mode {
164 // Please note the order is important (see IsCodeTarget, IsGCRelocMode).
165 CONSTRUCT_CALL, // code target that is a call to a JavaScript constructor.
166 CODE_TARGET_CONTEXT, // code target used for contextual loads.
167 CODE_TARGET, // code target which is not any of the above.
168 EMBEDDED_OBJECT,
169 EMBEDDED_STRING,
170
171 // Everything after runtime_entry (inclusive) is not GC'ed.
172 RUNTIME_ENTRY,
173 JS_RETURN, // Marks start of the ExitJSFrame code.
174 COMMENT,
175 POSITION, // See comment for kNoPosition above.
176 STATEMENT_POSITION, // See comment for kNoPosition above.
177 EXTERNAL_REFERENCE, // The address of an external C++ function.
178 INTERNAL_REFERENCE, // An address inside the same function.
179
180 // add more as needed
181 // Pseudo-types
182 NUMBER_OF_MODES, // must be no greater than 14 - see RelocInfoWriter
183 NONE, // never recorded
184 LAST_CODE_ENUM = CODE_TARGET,
185 LAST_GCED_ENUM = EMBEDDED_STRING
186 };
187
188
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000189 RelocInfo() {}
ager@chromium.org236ad962008-09-25 09:45:57 +0000190 RelocInfo(byte* pc, Mode rmode, intptr_t data)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000191 : pc_(pc), rmode_(rmode), data_(data) {
192 }
193
ager@chromium.org236ad962008-09-25 09:45:57 +0000194 static inline bool IsConstructCall(Mode mode) {
195 return mode == CONSTRUCT_CALL;
196 }
197 static inline bool IsCodeTarget(Mode mode) {
198 return mode <= LAST_CODE_ENUM;
199 }
200 // Is the relocation mode affected by GC?
201 static inline bool IsGCRelocMode(Mode mode) {
202 return mode <= LAST_GCED_ENUM;
203 }
204 static inline bool IsJSReturn(Mode mode) {
205 return mode == JS_RETURN;
206 }
207 static inline bool IsComment(Mode mode) {
208 return mode == COMMENT;
209 }
210 static inline bool IsPosition(Mode mode) {
211 return mode == POSITION || mode == STATEMENT_POSITION;
212 }
213 static inline bool IsStatementPosition(Mode mode) {
214 return mode == STATEMENT_POSITION;
215 }
216 static inline bool IsExternalReference(Mode mode) {
217 return mode == EXTERNAL_REFERENCE;
218 }
219 static inline bool IsInternalReference(Mode mode) {
220 return mode == INTERNAL_REFERENCE;
221 }
222 static inline int ModeMask(Mode mode) { return 1 << mode; }
223
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000224 // Accessors
225 byte* pc() const { return pc_; }
226 void set_pc(byte* pc) { pc_ = pc; }
ager@chromium.org236ad962008-09-25 09:45:57 +0000227 Mode rmode() const { return rmode_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000228 intptr_t data() const { return data_; }
229
230 // Apply a relocation by delta bytes
231 INLINE(void apply(int delta));
232
233 // Read/modify the code target in the branch/call instruction this relocation
ager@chromium.org236ad962008-09-25 09:45:57 +0000234 // applies to; can only be called if IsCodeTarget(rmode_)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000235 INLINE(Address target_address());
236 INLINE(void set_target_address(Address target));
237 INLINE(Object* target_object());
238 INLINE(Object** target_object_address());
239 INLINE(void set_target_object(Object* target));
240
241 // Read/modify the reference in the instruction this relocation
242 // applies to; can only be called if rmode_ is external_reference
243 INLINE(Address* target_reference_address());
244
245 // Read/modify the address of a call instruction. This is used to relocate
246 // the break points where straight-line code is patched with a call
247 // instruction.
248 INLINE(Address call_address());
249 INLINE(void set_call_address(Address target));
250 INLINE(Object* call_object());
251 INLINE(Object** call_object_address());
252 INLINE(void set_call_object(Object* target));
253
254 // Patch the code with some other code.
255 void patch_code(byte* instructions, int instruction_count);
256
257 // Patch the code with a call.
258 void patch_code_with_call(Address target, int guard_bytes);
259 INLINE(bool is_call_instruction());
260
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000261#ifdef ENABLE_DISASSEMBLER
262 // Printing
ager@chromium.org236ad962008-09-25 09:45:57 +0000263 static const char* RelocModeName(Mode rmode);
mads.s.ager31e71382008-08-13 09:32:07 +0000264 void Print();
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000265#endif // ENABLE_DISASSEMBLER
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000266#ifdef DEBUG
267 // Debugging
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000268 void Verify();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000269#endif
270
ager@chromium.org236ad962008-09-25 09:45:57 +0000271 static const int kCodeTargetMask = (1 << (LAST_CODE_ENUM + 1)) - 1;
272 static const int kPositionMask = 1 << POSITION | 1 << STATEMENT_POSITION;
273 static const int kDebugMask = kPositionMask | 1 << COMMENT;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000274 static const int kApplyMask; // Modes affected by apply. Depends on arch.
275
276 private:
277 // On ARM, note that pc_ is the address of the constant pool entry
278 // to be relocated and not the address of the instruction
279 // referencing the constant pool entry (except when rmode_ ==
280 // comment).
281 byte* pc_;
ager@chromium.org236ad962008-09-25 09:45:57 +0000282 Mode rmode_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000283 intptr_t data_;
284 friend class RelocIterator;
285};
286
287
288// RelocInfoWriter serializes a stream of relocation info. It writes towards
289// lower addresses.
290class RelocInfoWriter BASE_EMBEDDED {
291 public:
292 RelocInfoWriter() : pos_(NULL), last_pc_(NULL), last_data_(0) {}
293 RelocInfoWriter(byte* pos, byte* pc) : pos_(pos), last_pc_(pc),
294 last_data_(0) {}
295
296 byte* pos() const { return pos_; }
297 byte* last_pc() const { return last_pc_; }
298
299 void Write(const RelocInfo* rinfo);
300
301 // Update the state of the stream after reloc info buffer
302 // and/or code is moved while the stream is active.
303 void Reposition(byte* pos, byte* pc) {
304 pos_ = pos;
305 last_pc_ = pc;
306 }
307
308 // Max size (bytes) of a written RelocInfo.
309 static const int kMaxSize = 12;
310
311 private:
312 inline uint32_t WriteVariableLengthPCJump(uint32_t pc_delta);
313 inline void WriteTaggedPC(uint32_t pc_delta, int tag);
314 inline void WriteExtraTaggedPC(uint32_t pc_delta, int extra_tag);
315 inline void WriteExtraTaggedData(int32_t data_delta, int top_tag);
316 inline void WriteTaggedData(int32_t data_delta, int tag);
317 inline void WriteExtraTag(int extra_tag, int top_tag);
318
319 byte* pos_;
320 byte* last_pc_;
321 intptr_t last_data_;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000322 DISALLOW_COPY_AND_ASSIGN(RelocInfoWriter);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000323};
324
325
326// A RelocIterator iterates over relocation information.
327// Typical use:
328//
329// for (RelocIterator it(code); !it.done(); it.next()) {
330// // do something with it.rinfo() here
331// }
332//
333// A mask can be specified to skip unwanted modes.
334class RelocIterator: public Malloced {
335 public:
336 // Create a new iterator positioned at
337 // the beginning of the reloc info.
338 // Relocation information with mode k is included in the
339 // iteration iff bit k of mode_mask is set.
340 explicit RelocIterator(Code* code, int mode_mask = -1);
341 explicit RelocIterator(const CodeDesc& desc, int mode_mask = -1);
342
343 // Iteration
344 bool done() const { return done_; }
345 void next();
346
347 // Return pointer valid until next next().
348 RelocInfo* rinfo() {
349 ASSERT(!done());
350 return &rinfo_;
351 }
352
353 private:
354 // Advance* moves the position before/after reading.
355 // *Read* reads from current byte(s) into rinfo_.
356 // *Get* just reads and returns info on current byte.
357 void Advance(int bytes = 1) { pos_ -= bytes; }
358 int AdvanceGetTag();
359 int GetExtraTag();
360 int GetTopTag();
361 void ReadTaggedPC();
362 void AdvanceReadPC();
363 void AdvanceReadData();
364 void AdvanceReadVariableLengthPCJump();
365 int GetPositionTypeTag();
366 void ReadTaggedData();
367
ager@chromium.org236ad962008-09-25 09:45:57 +0000368 static RelocInfo::Mode DebugInfoModeFromTag(int tag);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000369
370 // If the given mode is wanted, set it in rinfo_ and return true.
371 // Else return false. Used for efficiently skipping unwanted modes.
ager@chromium.org236ad962008-09-25 09:45:57 +0000372 bool SetMode(RelocInfo::Mode mode) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000373 return (mode_mask_ & 1 << mode) ? (rinfo_.rmode_ = mode, true) : false;
374 }
375
376 byte* pos_;
377 byte* end_;
378 RelocInfo rinfo_;
379 bool done_;
380 int mode_mask_;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000381 DISALLOW_COPY_AND_ASSIGN(RelocIterator);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000382};
383
384
385//------------------------------------------------------------------------------
386// External function
387
388//----------------------------------------------------------------------------
389class IC_Utility;
390class Debug_Address;
391class SCTableReference;
392
393// An ExternalReference represents a C++ address called from the generated
394// code. All references to C++ functions and must be encapsulated in an
395// ExternalReference instance. This is done in order to track the origin of
396// all external references in the code.
397class ExternalReference BASE_EMBEDDED {
398 public:
399 explicit ExternalReference(Builtins::CFunctionId id);
400
401 explicit ExternalReference(Builtins::Name name);
402
403 explicit ExternalReference(Runtime::FunctionId id);
404
405 explicit ExternalReference(Runtime::Function* f);
406
407 explicit ExternalReference(const IC_Utility& ic_utility);
408
409 explicit ExternalReference(const Debug_Address& debug_address);
410
411 explicit ExternalReference(StatsCounter* counter);
412
413 explicit ExternalReference(Top::AddressId id);
414
415 explicit ExternalReference(const SCTableReference& table_ref);
416
417 // One-of-a-kind references. These references are not part of a general
418 // pattern. This means that they have to be added to the
419 // ExternalReferenceTable in serialize.cc manually.
420
421 static ExternalReference builtin_passed_function();
422
423 // Static variable Factory::the_hole_value.location()
424 static ExternalReference the_hole_value_location();
425
426 // Static variable StackGuard::address_of_limit()
427 static ExternalReference address_of_stack_guard_limit();
428
429 // Function Debug::Break()
430 static ExternalReference debug_break();
431
432 // Static variable Heap::NewSpaceStart()
433 static ExternalReference new_space_start();
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000434 static ExternalReference heap_always_allocate_scope_depth();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000435
436 // Used for fast allocation in generated code.
437 static ExternalReference new_space_allocation_top_address();
438 static ExternalReference new_space_allocation_limit_address();
439
440 // Used to check if single stepping is enabled in generated code.
441 static ExternalReference debug_step_in_fp_address();
442
443 Address address() const {return address_;}
444
445 private:
446 explicit ExternalReference(void* address)
447 : address_(reinterpret_cast<Address>(address)) {}
448
449 Address address_;
450};
451
452
453// -----------------------------------------------------------------------------
454// Utility functions
455
456// Move these into inline file?
457
458static inline bool is_intn(int x, int n) {
459 return -(1 << (n-1)) <= x && x < (1 << (n-1));
460}
461
462static inline bool is_int24(int x) { return is_intn(x, 24); }
463static inline bool is_int8(int x) { return is_intn(x, 8); }
464
465static inline bool is_uintn(int x, int n) {
466 return (x & -(1 << n)) == 0;
467}
468
469static inline bool is_uint3(int x) { return is_uintn(x, 3); }
470static inline bool is_uint4(int x) { return is_uintn(x, 4); }
471static inline bool is_uint5(int x) { return is_uintn(x, 5); }
472static inline bool is_uint8(int x) { return is_uintn(x, 8); }
473static inline bool is_uint12(int x) { return is_uintn(x, 12); }
474static inline bool is_uint16(int x) { return is_uintn(x, 16); }
475static inline bool is_uint24(int x) { return is_uintn(x, 24); }
476
477} } // namespace v8::internal
478
479#endif // V8_ASSEMBLER_H_