blob: 1f103d45b9b191566e0b2ce236b1c6b56bbe9a25 [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.
karlklose@chromium.org44bc7082011-04-11 12:33:05 +000033// Copyright 2011 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
lrn@chromium.org1c092762011-05-09 09:42:16 +000038#include "allocation.h"
erik.corry@gmail.com0511e242011-01-19 11:11:08 +000039#include "gdb-jit.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000040#include "runtime.h"
ager@chromium.org65dad4b2009-04-23 08:48:43 +000041#include "token.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000042
kasperl@chromium.org71affb52009-05-26 05:44:31 +000043namespace v8 {
44namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000045
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +000046const unsigned kNoASTId = -1;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000047// -----------------------------------------------------------------------------
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000048// Platform independent assembler base class.
49
50class AssemblerBase: public Malloced {
51 public:
52 explicit AssemblerBase(Isolate* isolate) : isolate_(isolate) {}
53
54 Isolate* isolate() const { return isolate_; }
55
56 private:
57 Isolate* isolate_;
58};
59
60// -----------------------------------------------------------------------------
kasperl@chromium.orga5551262010-12-07 12:49:48 +000061// Common double constants.
62
63class DoubleConstant: public AllStatic {
64 public:
65 static const double min_int;
66 static const double one_half;
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +000067 static const double minus_zero;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +000068 static const double negative_infinity;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000069 static const double nan;
kasperl@chromium.orga5551262010-12-07 12:49:48 +000070};
71
72
73// -----------------------------------------------------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000074// Labels represent pc locations; they are typically jump or call targets.
75// After declaration, a label can be freely used to denote known or (yet)
76// unknown pc location. Assembler::bind() is used to bind a label to the
77// current pc. A label can be bound only once.
78
kasperl@chromium.org7be3c992009-03-12 07:19:55 +000079class Label BASE_EMBEDDED {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000080 public:
karlklose@chromium.org83a47282011-05-11 11:54:09 +000081 enum Distance {
82 kNear, kFar
83 };
84
85 INLINE(Label()) {
86 Unuse();
87 UnuseNear();
88 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000089 INLINE(~Label()) { ASSERT(!is_linked()); }
90
91 INLINE(void Unuse()) { pos_ = 0; }
karlklose@chromium.org83a47282011-05-11 11:54:09 +000092 INLINE(void UnuseNear()) { near_link_pos_ = 0; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000093
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +000094 INLINE(bool is_bound() const) { return pos_ < 0; }
karlklose@chromium.org83a47282011-05-11 11:54:09 +000095 INLINE(bool is_unused() const) { return pos_ == 0 && near_link_pos_ == 0; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000096 INLINE(bool is_linked() const) { return pos_ > 0; }
karlklose@chromium.org83a47282011-05-11 11:54:09 +000097 INLINE(bool is_near_linked() const) { return near_link_pos_ > 0; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000098
99 // Returns the position of bound or linked labels. Cannot be used
100 // for unused labels.
101 int pos() const;
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000102 int near_link_pos() const { return near_link_pos_ - 1; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000103
104 private:
105 // pos_ encodes both the binding state (via its sign)
106 // and the binding position (via its value) of a label.
107 //
108 // pos_ < 0 bound label, pos() returns the jump target position
109 // pos_ == 0 unused label
110 // pos_ > 0 linked label, pos() returns the last reference position
111 int pos_;
112
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000113 // Behaves like |pos_| in the "> 0" case, but for near jumps to this label.
114 int near_link_pos_;
115
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000116 void bind_to(int pos) {
117 pos_ = -pos - 1;
118 ASSERT(is_bound());
119 }
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000120 void link_to(int pos, Distance distance = kFar) {
121 if (distance == kNear) {
122 near_link_pos_ = pos + 1;
123 ASSERT(is_near_linked());
124 } else {
125 pos_ = pos + 1;
126 ASSERT(is_linked());
127 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000128 }
129
130 friend class Assembler;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000131 friend class RegexpAssembler;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000132 friend class Displacement;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000133 friend class RegExpMacroAssemblerIrregexp;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000134};
135
136
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000137// -----------------------------------------------------------------------------
138// Relocation information
139
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000140
141// Relocation information consists of the address (pc) of the datum
142// to which the relocation information applies, the relocation mode
143// (rmode), and an optional data field. The relocation mode may be
144// "descriptive" and not indicate a need for relocation, but simply
145// describe a property of the datum. Such rmodes are useful for GC
146// and nice disassembly output.
147
148class RelocInfo BASE_EMBEDDED {
149 public:
ager@chromium.org236ad962008-09-25 09:45:57 +0000150 // The constant kNoPosition is used with the collecting of source positions
151 // in the relocation information. Two types of source positions are collected
152 // "position" (RelocMode position) and "statement position" (RelocMode
153 // statement_position). The "position" is collected at places in the source
154 // code which are of interest when making stack traces to pin-point the source
155 // location of a stack frame as close as possible. The "statement position" is
156 // collected at the beginning at each statement, and is used to indicate
157 // possible break locations. kNoPosition is used to indicate an
158 // invalid/uninitialized position value.
159 static const int kNoPosition = -1;
160
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +0000161 // This string is used to add padding comments to the reloc info in cases
162 // where we are not sure to have enough space for patching in during
163 // lazy deoptimization. This is the case if we have indirect calls for which
164 // we do not normally record relocation info.
165 static const char* kFillerCommentString;
166
ricow@chromium.org22334512011-02-25 07:28:50 +0000167 // The minimum size of a comment is equal to three bytes for the extra tagged
168 // pc + the tag for the data, and kPointerSize for the actual pointer to the
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000169 // comment.
ricow@chromium.org22334512011-02-25 07:28:50 +0000170 static const int kMinRelocCommentSize = 3 + kPointerSize;
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000171
172 // The maximum size for a call instruction including pc-jump.
173 static const int kMaxCallSize = 6;
174
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000175 // The maximum pc delta that will use the short encoding.
176 static const int kMaxSmallPCDelta;
177
ager@chromium.org236ad962008-09-25 09:45:57 +0000178 enum Mode {
179 // Please note the order is important (see IsCodeTarget, IsGCRelocMode).
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000180 CODE_TARGET, // Code target which is not any of the above.
181 CODE_TARGET_WITH_ID,
ager@chromium.org236ad962008-09-25 09:45:57 +0000182 CONSTRUCT_CALL, // code target that is a call to a JavaScript constructor.
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000183 CODE_TARGET_CONTEXT, // Code target used for contextual loads and stores.
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000184 DEBUG_BREAK, // Code target for the debugger statement.
ager@chromium.org236ad962008-09-25 09:45:57 +0000185 EMBEDDED_OBJECT,
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000186 GLOBAL_PROPERTY_CELL,
187
ager@chromium.org236ad962008-09-25 09:45:57 +0000188 // Everything after runtime_entry (inclusive) is not GC'ed.
189 RUNTIME_ENTRY,
190 JS_RETURN, // Marks start of the ExitJSFrame code.
191 COMMENT,
192 POSITION, // See comment for kNoPosition above.
193 STATEMENT_POSITION, // See comment for kNoPosition above.
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000194 DEBUG_BREAK_SLOT, // Additional code inserted for debug break slot.
ager@chromium.org236ad962008-09-25 09:45:57 +0000195 EXTERNAL_REFERENCE, // The address of an external C++ function.
196 INTERNAL_REFERENCE, // An address inside the same function.
197
198 // add more as needed
199 // Pseudo-types
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000200 NUMBER_OF_MODES, // There are at most 14 modes with noncompact encoding.
ager@chromium.org236ad962008-09-25 09:45:57 +0000201 NONE, // never recorded
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000202 LAST_CODE_ENUM = DEBUG_BREAK,
203 LAST_GCED_ENUM = GLOBAL_PROPERTY_CELL,
204 // Modes <= LAST_COMPACT_ENUM are guaranteed to have compact encoding.
205 LAST_COMPACT_ENUM = CODE_TARGET_WITH_ID
ager@chromium.org236ad962008-09-25 09:45:57 +0000206 };
207
208
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000209 RelocInfo() {}
ager@chromium.org236ad962008-09-25 09:45:57 +0000210 RelocInfo(byte* pc, Mode rmode, intptr_t data)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000211 : pc_(pc), rmode_(rmode), data_(data) {
212 }
213
ager@chromium.org236ad962008-09-25 09:45:57 +0000214 static inline bool IsConstructCall(Mode mode) {
215 return mode == CONSTRUCT_CALL;
216 }
217 static inline bool IsCodeTarget(Mode mode) {
218 return mode <= LAST_CODE_ENUM;
219 }
220 // Is the relocation mode affected by GC?
221 static inline bool IsGCRelocMode(Mode mode) {
222 return mode <= LAST_GCED_ENUM;
223 }
224 static inline bool IsJSReturn(Mode mode) {
225 return mode == JS_RETURN;
226 }
227 static inline bool IsComment(Mode mode) {
228 return mode == COMMENT;
229 }
230 static inline bool IsPosition(Mode mode) {
231 return mode == POSITION || mode == STATEMENT_POSITION;
232 }
233 static inline bool IsStatementPosition(Mode mode) {
234 return mode == STATEMENT_POSITION;
235 }
236 static inline bool IsExternalReference(Mode mode) {
237 return mode == EXTERNAL_REFERENCE;
238 }
239 static inline bool IsInternalReference(Mode mode) {
240 return mode == INTERNAL_REFERENCE;
241 }
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000242 static inline bool IsDebugBreakSlot(Mode mode) {
243 return mode == DEBUG_BREAK_SLOT;
244 }
ager@chromium.org236ad962008-09-25 09:45:57 +0000245 static inline int ModeMask(Mode mode) { return 1 << mode; }
246
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000247 // Accessors
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000248 byte* pc() const { return pc_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000249 void set_pc(byte* pc) { pc_ = pc; }
ager@chromium.org236ad962008-09-25 09:45:57 +0000250 Mode rmode() const { return rmode_; }
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000251 intptr_t data() const { return data_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000252
253 // Apply a relocation by delta bytes
ager@chromium.org3e875802009-06-29 08:26:34 +0000254 INLINE(void apply(intptr_t delta));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000255
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000256 // Is the pointer this relocation info refers to coded like a plain pointer
257 // or is it strange in some way (eg relative or patched into a series of
258 // instructions).
259 bool IsCodedSpecially();
260
ager@chromium.org32912102009-01-16 10:38:43 +0000261 // Read/modify the code target in the branch/call instruction
262 // this relocation applies to;
263 // can only be called if IsCodeTarget(rmode_) || rmode_ == RUNTIME_ENTRY
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000264 INLINE(Address target_address());
265 INLINE(void set_target_address(Address target));
266 INLINE(Object* target_object());
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000267 INLINE(Handle<Object> target_object_handle(Assembler* origin));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000268 INLINE(Object** target_object_address());
269 INLINE(void set_target_object(Object* target));
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000270 INLINE(JSGlobalPropertyCell* target_cell());
271 INLINE(Handle<JSGlobalPropertyCell> target_cell_handle());
272 INLINE(void set_target_cell(JSGlobalPropertyCell* cell));
273
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000274
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000275 // Read the address of the word containing the target_address in an
276 // instruction stream. What this means exactly is architecture-independent.
277 // The only architecture-independent user of this function is the serializer.
278 // The serializer uses it to find out how many raw bytes of instruction to
279 // output before the next target. Architecture-independent code shouldn't
280 // dereference the pointer it gets back from this.
ager@chromium.org32912102009-01-16 10:38:43 +0000281 INLINE(Address target_address_address());
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000282 // This indicates how much space a target takes up when deserializing a code
283 // stream. For most architectures this is just the size of a pointer. For
284 // an instruction like movw/movt where the target bits are mixed into the
285 // instruction bits the size of the target will be zero, indicating that the
286 // serializer should not step forwards in memory after a target is resolved
287 // and written. In this case the target_address_address function above
288 // should return the end of the instructions to be patched, allowing the
289 // deserializer to deserialize the instructions as raw bytes and put them in
290 // place, ready to be patched with the target.
291 INLINE(int target_address_size());
ager@chromium.org32912102009-01-16 10:38:43 +0000292
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000293 // Read/modify the reference in the instruction this relocation
294 // applies to; can only be called if rmode_ is external_reference
295 INLINE(Address* target_reference_address());
296
297 // Read/modify the address of a call instruction. This is used to relocate
298 // the break points where straight-line code is patched with a call
299 // instruction.
300 INLINE(Address call_address());
301 INLINE(void set_call_address(Address target));
302 INLINE(Object* call_object());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000303 INLINE(void set_call_object(Object* target));
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +0000304 INLINE(Object** call_object_address());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000305
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000306 template<typename StaticVisitor> inline void Visit(Heap* heap);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000307 inline void Visit(ObjectVisitor* v);
308
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000309 // Patch the code with some other code.
iposva@chromium.org245aa852009-02-10 00:49:54 +0000310 void PatchCode(byte* instructions, int instruction_count);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000311
312 // Patch the code with a call.
iposva@chromium.org245aa852009-02-10 00:49:54 +0000313 void PatchCodeWithCall(Address target, int guard_bytes);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000314
315 // Check whether this return sequence has been patched
316 // with a call to the debugger.
317 INLINE(bool IsPatchedReturnSequence());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000318
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000319 // Check whether this debug break slot has been patched with a call to the
320 // debugger.
321 INLINE(bool IsPatchedDebugBreakSlotSequence());
322
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000323#ifdef ENABLE_DISASSEMBLER
324 // Printing
ager@chromium.org236ad962008-09-25 09:45:57 +0000325 static const char* RelocModeName(Mode rmode);
whesse@chromium.org023421e2010-12-21 12:19:12 +0000326 void Print(FILE* out);
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000327#endif // ENABLE_DISASSEMBLER
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000328#ifdef DEBUG
329 // Debugging
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000330 void Verify();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000331#endif
332
ager@chromium.org236ad962008-09-25 09:45:57 +0000333 static const int kCodeTargetMask = (1 << (LAST_CODE_ENUM + 1)) - 1;
334 static const int kPositionMask = 1 << POSITION | 1 << STATEMENT_POSITION;
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000335 static const int kDataMask =
336 (1 << CODE_TARGET_WITH_ID) | kPositionMask | (1 << COMMENT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000337 static const int kApplyMask; // Modes affected by apply. Depends on arch.
338
339 private:
340 // On ARM, note that pc_ is the address of the constant pool entry
341 // to be relocated and not the address of the instruction
342 // referencing the constant pool entry (except when rmode_ ==
343 // comment).
344 byte* pc_;
ager@chromium.org236ad962008-09-25 09:45:57 +0000345 Mode rmode_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000346 intptr_t data_;
347 friend class RelocIterator;
348};
349
350
351// RelocInfoWriter serializes a stream of relocation info. It writes towards
352// lower addresses.
353class RelocInfoWriter BASE_EMBEDDED {
354 public:
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000355 RelocInfoWriter() : pos_(NULL),
356 last_pc_(NULL),
357 last_id_(0),
358 last_position_(0) {}
359 RelocInfoWriter(byte* pos, byte* pc) : pos_(pos),
360 last_pc_(pc),
361 last_id_(0),
362 last_position_(0) {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000363
364 byte* pos() const { return pos_; }
365 byte* last_pc() const { return last_pc_; }
366
367 void Write(const RelocInfo* rinfo);
368
369 // Update the state of the stream after reloc info buffer
370 // and/or code is moved while the stream is active.
371 void Reposition(byte* pos, byte* pc) {
372 pos_ = pos;
373 last_pc_ = pc;
374 }
375
ager@chromium.org3e875802009-06-29 08:26:34 +0000376 // Max size (bytes) of a written RelocInfo. Longest encoding is
377 // ExtraTag, VariableLengthPCJump, ExtraTag, pc_delta, ExtraTag, data_delta.
378 // On ia32 and arm this is 1 + 4 + 1 + 1 + 1 + 4 = 12.
379 // On x64 this is 1 + 4 + 1 + 1 + 1 + 8 == 16;
380 // Here we use the maximum of the two.
381 static const int kMaxSize = 16;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000382
383 private:
384 inline uint32_t WriteVariableLengthPCJump(uint32_t pc_delta);
385 inline void WriteTaggedPC(uint32_t pc_delta, int tag);
386 inline void WriteExtraTaggedPC(uint32_t pc_delta, int extra_tag);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000387 inline void WriteExtraTaggedIntData(int data_delta, int top_tag);
ager@chromium.orge2902be2009-06-08 12:21:35 +0000388 inline void WriteExtraTaggedData(intptr_t data_delta, int top_tag);
389 inline void WriteTaggedData(intptr_t data_delta, int tag);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000390 inline void WriteExtraTag(int extra_tag, int top_tag);
391
392 byte* pos_;
393 byte* last_pc_;
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000394 int last_id_;
395 int last_position_;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000396 DISALLOW_COPY_AND_ASSIGN(RelocInfoWriter);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000397};
398
399
400// A RelocIterator iterates over relocation information.
401// Typical use:
402//
403// for (RelocIterator it(code); !it.done(); it.next()) {
404// // do something with it.rinfo() here
405// }
406//
407// A mask can be specified to skip unwanted modes.
408class RelocIterator: public Malloced {
409 public:
410 // Create a new iterator positioned at
411 // the beginning of the reloc info.
412 // Relocation information with mode k is included in the
413 // iteration iff bit k of mode_mask is set.
414 explicit RelocIterator(Code* code, int mode_mask = -1);
415 explicit RelocIterator(const CodeDesc& desc, int mode_mask = -1);
416
417 // Iteration
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000418 bool done() const { return done_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000419 void next();
420
421 // Return pointer valid until next next().
422 RelocInfo* rinfo() {
423 ASSERT(!done());
424 return &rinfo_;
425 }
426
427 private:
428 // Advance* moves the position before/after reading.
429 // *Read* reads from current byte(s) into rinfo_.
430 // *Get* just reads and returns info on current byte.
431 void Advance(int bytes = 1) { pos_ -= bytes; }
432 int AdvanceGetTag();
433 int GetExtraTag();
434 int GetTopTag();
435 void ReadTaggedPC();
436 void AdvanceReadPC();
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000437 void AdvanceReadId();
438 void AdvanceReadPosition();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000439 void AdvanceReadData();
440 void AdvanceReadVariableLengthPCJump();
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000441 int GetLocatableTypeTag();
442 void ReadTaggedId();
443 void ReadTaggedPosition();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000444
445 // If the given mode is wanted, set it in rinfo_ and return true.
446 // Else return false. Used for efficiently skipping unwanted modes.
ager@chromium.org236ad962008-09-25 09:45:57 +0000447 bool SetMode(RelocInfo::Mode mode) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000448 return (mode_mask_ & (1 << mode)) ? (rinfo_.rmode_ = mode, true) : false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000449 }
450
451 byte* pos_;
452 byte* end_;
453 RelocInfo rinfo_;
454 bool done_;
455 int mode_mask_;
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000456 int last_id_;
457 int last_position_;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000458 DISALLOW_COPY_AND_ASSIGN(RelocIterator);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000459};
460
461
462//------------------------------------------------------------------------------
463// External function
464
465//----------------------------------------------------------------------------
466class IC_Utility;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000467class SCTableReference;
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000468#ifdef ENABLE_DEBUGGER_SUPPORT
469class Debug_Address;
470#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000471
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000472
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000473// An ExternalReference represents a C++ address used in the generated
474// code. All references to C++ functions and variables must be encapsulated in
475// an ExternalReference instance. This is done in order to track the origin of
476// all external references in the code so that they can be bound to the correct
477// addresses when deserializing a heap.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000478class ExternalReference BASE_EMBEDDED {
479 public:
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000480 // Used in the simulator to support different native api calls.
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000481 enum Type {
vegorov@chromium.org5d6c1f52011-02-28 13:13:38 +0000482 // Builtin call.
483 // MaybeObject* f(v8::internal::Arguments).
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000484 BUILTIN_CALL, // default
vegorov@chromium.org5d6c1f52011-02-28 13:13:38 +0000485
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000486 // Builtin that takes float arguments and returns an int.
487 // int f(double, double).
488 BUILTIN_COMPARE_CALL,
489
vegorov@chromium.org5d6c1f52011-02-28 13:13:38 +0000490 // Builtin call that returns floating point.
491 // double f(double, double).
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000492 BUILTIN_FP_FP_CALL,
493
494 // Builtin call that returns floating point.
495 // double f(double).
496 BUILTIN_FP_CALL,
497
498 // Builtin call that returns floating point.
499 // double f(double, int).
500 BUILTIN_FP_INT_CALL,
vegorov@chromium.org5d6c1f52011-02-28 13:13:38 +0000501
502 // Direct call to API function callback.
503 // Handle<Value> f(v8::Arguments&)
504 DIRECT_API_CALL,
505
506 // Direct call to accessor getter callback.
507 // Handle<value> f(Local<String> property, AccessorInfo& info)
508 DIRECT_GETTER_CALL
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000509 };
510
511 typedef void* ExternalReferenceRedirector(void* original, Type type);
512
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000513 ExternalReference(Builtins::CFunctionId id, Isolate* isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000514
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000515 ExternalReference(ApiFunction* ptr, Type type, Isolate* isolate);
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000516
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000517 ExternalReference(Builtins::Name name, Isolate* isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000518
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000519 ExternalReference(Runtime::FunctionId id, Isolate* isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000520
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000521 ExternalReference(const Runtime::Function* f, Isolate* isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000522
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000523 ExternalReference(const IC_Utility& ic_utility, Isolate* isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000524
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000525#ifdef ENABLE_DEBUGGER_SUPPORT
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000526 ExternalReference(const Debug_Address& debug_address, Isolate* isolate);
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000527#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000528
529 explicit ExternalReference(StatsCounter* counter);
530
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000531 ExternalReference(Isolate::AddressId id, Isolate* isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000532
533 explicit ExternalReference(const SCTableReference& table_ref);
534
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000535 // Isolate::Current() as an external reference.
536 static ExternalReference isolate_address();
537
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000538 // One-of-a-kind references. These references are not part of a general
539 // pattern. This means that they have to be added to the
540 // ExternalReferenceTable in serialize.cc manually.
541
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000542 static ExternalReference perform_gc_function(Isolate* isolate);
543 static ExternalReference fill_heap_number_with_random_function(
544 Isolate* isolate);
545 static ExternalReference random_uint32_function(Isolate* isolate);
546 static ExternalReference transcendental_cache_array_address(Isolate* isolate);
547 static ExternalReference delete_handle_scope_extensions(Isolate* isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000548
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000549 // Deoptimization support.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000550 static ExternalReference new_deoptimizer_function(Isolate* isolate);
551 static ExternalReference compute_output_frames_function(Isolate* isolate);
552 static ExternalReference global_contexts_list(Isolate* isolate);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000553
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000554 // Static data in the keyed lookup cache.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000555 static ExternalReference keyed_lookup_cache_keys(Isolate* isolate);
556 static ExternalReference keyed_lookup_cache_field_offsets(Isolate* isolate);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000557
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000558 // Static variable Factory::the_hole_value.location()
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000559 static ExternalReference the_hole_value_location(Isolate* isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000560
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000561 // Static variable Factory::arguments_marker.location()
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000562 static ExternalReference arguments_marker_location(Isolate* isolate);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000563
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000564 // Static variable Heap::roots_address()
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000565 static ExternalReference roots_address(Isolate* isolate);
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000566
ager@chromium.org6f10e412009-02-13 10:11:16 +0000567 // Static variable StackGuard::address_of_jslimit()
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000568 static ExternalReference address_of_stack_limit(Isolate* isolate);
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000569
570 // Static variable StackGuard::address_of_real_jslimit()
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000571 static ExternalReference address_of_real_stack_limit(Isolate* isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000572
ager@chromium.org32912102009-01-16 10:38:43 +0000573 // Static variable RegExpStack::limit_address()
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000574 static ExternalReference address_of_regexp_stack_limit(Isolate* isolate);
ager@chromium.org32912102009-01-16 10:38:43 +0000575
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000576 // Static variables for RegExp.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000577 static ExternalReference address_of_static_offsets_vector(Isolate* isolate);
578 static ExternalReference address_of_regexp_stack_memory_address(
579 Isolate* isolate);
580 static ExternalReference address_of_regexp_stack_memory_size(
581 Isolate* isolate);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000582
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000583 // Static variable Heap::NewSpaceStart()
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000584 static ExternalReference new_space_start(Isolate* isolate);
585 static ExternalReference new_space_mask(Isolate* isolate);
586 static ExternalReference heap_always_allocate_scope_depth(Isolate* isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000587
588 // Used for fast allocation in generated code.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000589 static ExternalReference new_space_allocation_top_address(Isolate* isolate);
590 static ExternalReference new_space_allocation_limit_address(Isolate* isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000591
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000592 static ExternalReference double_fp_operation(Token::Value operation,
593 Isolate* isolate);
594 static ExternalReference compare_doubles(Isolate* isolate);
595 static ExternalReference power_double_double_function(Isolate* isolate);
596 static ExternalReference power_double_int_function(Isolate* isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000597
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000598 static ExternalReference handle_scope_next_address();
599 static ExternalReference handle_scope_limit_address();
lrn@chromium.org303ada72010-10-27 09:33:13 +0000600 static ExternalReference handle_scope_level_address();
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000601
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000602 static ExternalReference scheduled_exception_address(Isolate* isolate);
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000603
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000604 // Static variables containing common double constants.
605 static ExternalReference address_of_min_int();
606 static ExternalReference address_of_one_half();
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000607 static ExternalReference address_of_minus_zero();
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000608 static ExternalReference address_of_negative_infinity();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000609 static ExternalReference address_of_nan();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000610
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000611 static ExternalReference math_sin_double_function(Isolate* isolate);
612 static ExternalReference math_cos_double_function(Isolate* isolate);
613 static ExternalReference math_log_double_function(Isolate* isolate);
karlklose@chromium.org8f806e82011-03-07 14:06:08 +0000614
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000615 Address address() const {return reinterpret_cast<Address>(address_);}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000616
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000617#ifdef ENABLE_DEBUGGER_SUPPORT
618 // Function Debug::Break()
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000619 static ExternalReference debug_break(Isolate* isolate);
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000620
621 // Used to check if single stepping is enabled in generated code.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000622 static ExternalReference debug_step_in_fp_address(Isolate* isolate);
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000623#endif
624
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000625#ifndef V8_INTERPRETED_REGEXP
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000626 // C functions called from RegExp generated code.
627
628 // Function NativeRegExpMacroAssembler::CaseInsensitiveCompareUC16()
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000629 static ExternalReference re_case_insensitive_compare_uc16(Isolate* isolate);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000630
631 // Function RegExpMacroAssembler*::CheckStackGuardState()
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000632 static ExternalReference re_check_stack_guard_state(Isolate* isolate);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000633
634 // Function NativeRegExpMacroAssembler::GrowStack()
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000635 static ExternalReference re_grow_stack(Isolate* isolate);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000636
637 // byte NativeRegExpMacroAssembler::word_character_bitmap
638 static ExternalReference re_word_character_map();
639
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000640#endif
641
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000642 // This lets you register a function that rewrites all external references.
643 // Used by the ARM simulator to catch calls to external references.
lrn@chromium.org1c092762011-05-09 09:42:16 +0000644 static void set_redirector(Isolate* isolate,
645 ExternalReferenceRedirector* redirector) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000646 // We can't stack them.
lrn@chromium.org1c092762011-05-09 09:42:16 +0000647 ASSERT(isolate->external_reference_redirector() == NULL);
648 isolate->set_external_reference_redirector(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000649 reinterpret_cast<ExternalReferenceRedirectorPointer*>(redirector));
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000650 }
651
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000652 private:
653 explicit ExternalReference(void* address)
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000654 : address_(address) {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000655
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000656 static void* Redirect(Isolate* isolate,
657 void* address,
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000658 Type type = ExternalReference::BUILTIN_CALL) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000659 ExternalReferenceRedirector* redirector =
660 reinterpret_cast<ExternalReferenceRedirector*>(
661 isolate->external_reference_redirector());
662 if (redirector == NULL) return address;
663 void* answer = (*redirector)(address, type);
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000664 return answer;
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000665 }
666
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000667 static void* Redirect(Isolate* isolate,
668 Address address_arg,
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000669 Type type = ExternalReference::BUILTIN_CALL) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000670 ExternalReferenceRedirector* redirector =
671 reinterpret_cast<ExternalReferenceRedirector*>(
672 isolate->external_reference_redirector());
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000673 void* address = reinterpret_cast<void*>(address_arg);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000674 void* answer = (redirector == NULL) ?
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000675 address :
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000676 (*redirector)(address, type);
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000677 return answer;
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000678 }
679
680 void* address_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000681};
682
683
684// -----------------------------------------------------------------------------
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000685// Position recording support
686
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000687struct PositionState {
688 PositionState() : current_position(RelocInfo::kNoPosition),
689 written_position(RelocInfo::kNoPosition),
690 current_statement_position(RelocInfo::kNoPosition),
691 written_statement_position(RelocInfo::kNoPosition) {}
692
693 int current_position;
694 int written_position;
695
696 int current_statement_position;
697 int written_statement_position;
698};
699
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000700
701class PositionsRecorder BASE_EMBEDDED {
702 public:
703 explicit PositionsRecorder(Assembler* assembler)
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000704 : assembler_(assembler) {
705#ifdef ENABLE_GDB_JIT_INTERFACE
706 gdbjit_lineinfo_ = NULL;
707#endif
708 }
709
710#ifdef ENABLE_GDB_JIT_INTERFACE
711 ~PositionsRecorder() {
712 delete gdbjit_lineinfo_;
713 }
714
715 void StartGDBJITLineInfoRecording() {
716 if (FLAG_gdbjit) {
717 gdbjit_lineinfo_ = new GDBJITLineInfo();
718 }
719 }
720
721 GDBJITLineInfo* DetachGDBJITLineInfo() {
722 GDBJITLineInfo* lineinfo = gdbjit_lineinfo_;
723 gdbjit_lineinfo_ = NULL; // To prevent deallocation in destructor.
724 return lineinfo;
725 }
726#endif
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000727
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000728 // Set current position to pos.
729 void RecordPosition(int pos);
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000730
731 // Set current statement position to pos.
732 void RecordStatementPosition(int pos);
733
734 // Write recorded positions to relocation information.
735 bool WriteRecordedPositions();
736
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000737 int current_position() const { return state_.current_position; }
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000738
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000739 int current_statement_position() const {
740 return state_.current_statement_position;
741 }
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000742
743 private:
744 Assembler* assembler_;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000745 PositionState state_;
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000746#ifdef ENABLE_GDB_JIT_INTERFACE
747 GDBJITLineInfo* gdbjit_lineinfo_;
748#endif
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000749
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000750 friend class PreservePositionScope;
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000751
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000752 DISALLOW_COPY_AND_ASSIGN(PositionsRecorder);
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000753};
754
755
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000756class PreservePositionScope BASE_EMBEDDED {
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000757 public:
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000758 explicit PreservePositionScope(PositionsRecorder* positions_recorder)
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000759 : positions_recorder_(positions_recorder),
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000760 saved_state_(positions_recorder->state_) {}
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000761
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000762 ~PreservePositionScope() {
763 positions_recorder_->state_ = saved_state_;
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000764 }
765
766 private:
767 PositionsRecorder* positions_recorder_;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000768 const PositionState saved_state_;
769
770 DISALLOW_COPY_AND_ASSIGN(PreservePositionScope);
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000771};
772
773
774// -----------------------------------------------------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000775// Utility functions
776
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000777static inline bool is_intn(int x, int n) {
778 return -(1 << (n-1)) <= x && x < (1 << (n-1));
779}
780
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000781static inline bool is_int8(int x) { return is_intn(x, 8); }
ager@chromium.org5c838252010-02-19 08:53:10 +0000782static inline bool is_int16(int x) { return is_intn(x, 16); }
783static inline bool is_int18(int x) { return is_intn(x, 18); }
784static inline bool is_int24(int x) { return is_intn(x, 24); }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000785
786static inline bool is_uintn(int x, int n) {
787 return (x & -(1 << n)) == 0;
788}
789
ager@chromium.orge2902be2009-06-08 12:21:35 +0000790static inline bool is_uint2(int x) { return is_uintn(x, 2); }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000791static inline bool is_uint3(int x) { return is_uintn(x, 3); }
792static inline bool is_uint4(int x) { return is_uintn(x, 4); }
793static inline bool is_uint5(int x) { return is_uintn(x, 5); }
ager@chromium.orge2902be2009-06-08 12:21:35 +0000794static inline bool is_uint6(int x) { return is_uintn(x, 6); }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000795static inline bool is_uint8(int x) { return is_uintn(x, 8); }
ager@chromium.org5c838252010-02-19 08:53:10 +0000796static inline bool is_uint10(int x) { return is_uintn(x, 10); }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000797static inline bool is_uint12(int x) { return is_uintn(x, 12); }
798static inline bool is_uint16(int x) { return is_uintn(x, 16); }
799static inline bool is_uint24(int x) { return is_uintn(x, 24); }
ager@chromium.org5c838252010-02-19 08:53:10 +0000800static inline bool is_uint26(int x) { return is_uintn(x, 26); }
801static inline bool is_uint28(int x) { return is_uintn(x, 28); }
802
803static inline int NumberOfBitsSet(uint32_t x) {
804 unsigned int num_bits_set;
805 for (num_bits_set = 0; x; x >>= 1) {
806 num_bits_set += x & 1;
807 }
808 return num_bits_set;
809}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000810
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000811// Computes pow(x, y) with the special cases in the spec for Math.pow.
812double power_double_int(double x, int y);
813double power_double_double(double x, double y);
814
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000815// Helper class for generating code or data associated with the code
816// right after a call instruction. As an example this can be used to
817// generate safepoint data after calls for crankshaft.
818class CallWrapper {
819 public:
820 CallWrapper() { }
821 virtual ~CallWrapper() { }
822 // Called just before emitting a call. Argument is the size of the generated
823 // call code.
824 virtual void BeforeCall(int call_size) const = 0;
825 // Called just after emitting a call, i.e., at the return site for the call.
826 virtual void AfterCall() const = 0;
827};
828
829class NullCallWrapper : public CallWrapper {
830 public:
831 NullCallWrapper() { }
832 virtual ~NullCallWrapper() { }
833 virtual void BeforeCall(int call_size) const { }
834 virtual void AfterCall() const { }
835};
836
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000837} } // namespace v8::internal
838
839#endif // V8_ASSEMBLER_H_