blob: 1577433591f222be952eb48c9d3215c3c7667a93 [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"
Leon Clarkef7060e22010-06-03 12:02:55 +010041#include "objects.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000042
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.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100121 CODE_TARGET_CONTEXT, // Code target used for contextual loads.
122 DEBUG_BREAK, // Code target for the debugger statement.
123 CODE_TARGET, // Code target which is not any of the above.
Steve Blocka7e24c12009-10-30 11:49:00 +0000124 EMBEDDED_OBJECT,
Steve Blocka7e24c12009-10-30 11:49:00 +0000125
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.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100132 DEBUG_BREAK_SLOT, // Additional code inserted for debug break slot.
Steve Blocka7e24c12009-10-30 11:49:00 +0000133 EXTERNAL_REFERENCE, // The address of an external C++ function.
134 INTERNAL_REFERENCE, // An address inside the same function.
135
136 // add more as needed
137 // Pseudo-types
138 NUMBER_OF_MODES, // must be no greater than 14 - see RelocInfoWriter
139 NONE, // never recorded
140 LAST_CODE_ENUM = CODE_TARGET,
Leon Clarkef7060e22010-06-03 12:02:55 +0100141 LAST_GCED_ENUM = EMBEDDED_OBJECT
Steve Blocka7e24c12009-10-30 11:49:00 +0000142 };
143
144
145 RelocInfo() {}
146 RelocInfo(byte* pc, Mode rmode, intptr_t data)
147 : pc_(pc), rmode_(rmode), data_(data) {
148 }
149
150 static inline bool IsConstructCall(Mode mode) {
151 return mode == CONSTRUCT_CALL;
152 }
153 static inline bool IsCodeTarget(Mode mode) {
154 return mode <= LAST_CODE_ENUM;
155 }
156 // Is the relocation mode affected by GC?
157 static inline bool IsGCRelocMode(Mode mode) {
158 return mode <= LAST_GCED_ENUM;
159 }
160 static inline bool IsJSReturn(Mode mode) {
161 return mode == JS_RETURN;
162 }
163 static inline bool IsComment(Mode mode) {
164 return mode == COMMENT;
165 }
166 static inline bool IsPosition(Mode mode) {
167 return mode == POSITION || mode == STATEMENT_POSITION;
168 }
169 static inline bool IsStatementPosition(Mode mode) {
170 return mode == STATEMENT_POSITION;
171 }
172 static inline bool IsExternalReference(Mode mode) {
173 return mode == EXTERNAL_REFERENCE;
174 }
175 static inline bool IsInternalReference(Mode mode) {
176 return mode == INTERNAL_REFERENCE;
177 }
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100178 static inline bool IsDebugBreakSlot(Mode mode) {
179 return mode == DEBUG_BREAK_SLOT;
180 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000181 static inline int ModeMask(Mode mode) { return 1 << mode; }
182
183 // Accessors
184 byte* pc() const { return pc_; }
185 void set_pc(byte* pc) { pc_ = pc; }
186 Mode rmode() const { return rmode_; }
187 intptr_t data() const { return data_; }
188
189 // Apply a relocation by delta bytes
190 INLINE(void apply(intptr_t delta));
191
Leon Clarkef7060e22010-06-03 12:02:55 +0100192 // Is the pointer this relocation info refers to coded like a plain pointer
193 // or is it strange in some way (eg relative or patched into a series of
194 // instructions).
195 bool IsCodedSpecially();
196
Steve Blocka7e24c12009-10-30 11:49:00 +0000197 // Read/modify the code target in the branch/call instruction
198 // this relocation applies to;
199 // can only be called if IsCodeTarget(rmode_) || rmode_ == RUNTIME_ENTRY
200 INLINE(Address target_address());
201 INLINE(void set_target_address(Address target));
202 INLINE(Object* target_object());
Steve Block3ce2e202009-11-05 08:53:23 +0000203 INLINE(Handle<Object> target_object_handle(Assembler* origin));
Steve Blocka7e24c12009-10-30 11:49:00 +0000204 INLINE(Object** target_object_address());
205 INLINE(void set_target_object(Object* target));
206
Leon Clarkef7060e22010-06-03 12:02:55 +0100207 // Read the address of the word containing the target_address in an
208 // instruction stream. What this means exactly is architecture-independent.
209 // The only architecture-independent user of this function is the serializer.
210 // The serializer uses it to find out how many raw bytes of instruction to
211 // output before the next target. Architecture-independent code shouldn't
212 // dereference the pointer it gets back from this.
Steve Blocka7e24c12009-10-30 11:49:00 +0000213 INLINE(Address target_address_address());
Leon Clarkef7060e22010-06-03 12:02:55 +0100214 // This indicates how much space a target takes up when deserializing a code
215 // stream. For most architectures this is just the size of a pointer. For
216 // an instruction like movw/movt where the target bits are mixed into the
217 // instruction bits the size of the target will be zero, indicating that the
218 // serializer should not step forwards in memory after a target is resolved
219 // and written. In this case the target_address_address function above
220 // should return the end of the instructions to be patched, allowing the
221 // deserializer to deserialize the instructions as raw bytes and put them in
222 // place, ready to be patched with the target.
223 INLINE(int target_address_size());
Steve Blocka7e24c12009-10-30 11:49:00 +0000224
225 // Read/modify the reference in the instruction this relocation
226 // applies to; can only be called if rmode_ is external_reference
227 INLINE(Address* target_reference_address());
228
229 // Read/modify the address of a call instruction. This is used to relocate
230 // the break points where straight-line code is patched with a call
231 // instruction.
232 INLINE(Address call_address());
233 INLINE(void set_call_address(Address target));
234 INLINE(Object* call_object());
Steve Blocka7e24c12009-10-30 11:49:00 +0000235 INLINE(void set_call_object(Object* target));
Ben Murdochbb769b22010-08-11 14:56:33 +0100236 INLINE(Object** call_object_address());
Steve Blocka7e24c12009-10-30 11:49:00 +0000237
Iain Merrick75681382010-08-19 15:07:18 +0100238 template<typename StaticVisitor> inline void Visit();
Leon Clarkef7060e22010-06-03 12:02:55 +0100239 inline void Visit(ObjectVisitor* v);
240
Steve Blocka7e24c12009-10-30 11:49:00 +0000241 // Patch the code with some other code.
242 void PatchCode(byte* instructions, int instruction_count);
243
244 // Patch the code with a call.
245 void PatchCodeWithCall(Address target, int guard_bytes);
Steve Block3ce2e202009-11-05 08:53:23 +0000246
247 // Check whether this return sequence has been patched
248 // with a call to the debugger.
249 INLINE(bool IsPatchedReturnSequence());
Steve Blocka7e24c12009-10-30 11:49:00 +0000250
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100251 // Check whether this debug break slot has been patched with a call to the
252 // debugger.
253 INLINE(bool IsPatchedDebugBreakSlotSequence());
254
Steve Blocka7e24c12009-10-30 11:49:00 +0000255#ifdef ENABLE_DISASSEMBLER
256 // Printing
257 static const char* RelocModeName(Mode rmode);
258 void Print();
259#endif // ENABLE_DISASSEMBLER
260#ifdef DEBUG
261 // Debugging
262 void Verify();
263#endif
264
265 static const int kCodeTargetMask = (1 << (LAST_CODE_ENUM + 1)) - 1;
266 static const int kPositionMask = 1 << POSITION | 1 << STATEMENT_POSITION;
267 static const int kDebugMask = kPositionMask | 1 << COMMENT;
268 static const int kApplyMask; // Modes affected by apply. Depends on arch.
269
270 private:
271 // On ARM, note that pc_ is the address of the constant pool entry
272 // to be relocated and not the address of the instruction
273 // referencing the constant pool entry (except when rmode_ ==
274 // comment).
275 byte* pc_;
276 Mode rmode_;
277 intptr_t data_;
278 friend class RelocIterator;
279};
280
281
282// RelocInfoWriter serializes a stream of relocation info. It writes towards
283// lower addresses.
284class RelocInfoWriter BASE_EMBEDDED {
285 public:
286 RelocInfoWriter() : pos_(NULL), last_pc_(NULL), last_data_(0) {}
287 RelocInfoWriter(byte* pos, byte* pc) : pos_(pos), last_pc_(pc),
288 last_data_(0) {}
289
290 byte* pos() const { return pos_; }
291 byte* last_pc() const { return last_pc_; }
292
293 void Write(const RelocInfo* rinfo);
294
295 // Update the state of the stream after reloc info buffer
296 // and/or code is moved while the stream is active.
297 void Reposition(byte* pos, byte* pc) {
298 pos_ = pos;
299 last_pc_ = pc;
300 }
301
302 // Max size (bytes) of a written RelocInfo. Longest encoding is
303 // ExtraTag, VariableLengthPCJump, ExtraTag, pc_delta, ExtraTag, data_delta.
304 // On ia32 and arm this is 1 + 4 + 1 + 1 + 1 + 4 = 12.
305 // On x64 this is 1 + 4 + 1 + 1 + 1 + 8 == 16;
306 // Here we use the maximum of the two.
307 static const int kMaxSize = 16;
308
309 private:
310 inline uint32_t WriteVariableLengthPCJump(uint32_t pc_delta);
311 inline void WriteTaggedPC(uint32_t pc_delta, int tag);
312 inline void WriteExtraTaggedPC(uint32_t pc_delta, int extra_tag);
313 inline void WriteExtraTaggedData(intptr_t data_delta, int top_tag);
314 inline void WriteTaggedData(intptr_t data_delta, int tag);
315 inline void WriteExtraTag(int extra_tag, int top_tag);
316
317 byte* pos_;
318 byte* last_pc_;
319 intptr_t last_data_;
320 DISALLOW_COPY_AND_ASSIGN(RelocInfoWriter);
321};
322
323
324// A RelocIterator iterates over relocation information.
325// Typical use:
326//
327// for (RelocIterator it(code); !it.done(); it.next()) {
328// // do something with it.rinfo() here
329// }
330//
331// A mask can be specified to skip unwanted modes.
332class RelocIterator: public Malloced {
333 public:
334 // Create a new iterator positioned at
335 // the beginning of the reloc info.
336 // Relocation information with mode k is included in the
337 // iteration iff bit k of mode_mask is set.
338 explicit RelocIterator(Code* code, int mode_mask = -1);
339 explicit RelocIterator(const CodeDesc& desc, int mode_mask = -1);
340
341 // Iteration
342 bool done() const { return done_; }
343 void next();
344
345 // Return pointer valid until next next().
346 RelocInfo* rinfo() {
347 ASSERT(!done());
348 return &rinfo_;
349 }
350
351 private:
352 // Advance* moves the position before/after reading.
353 // *Read* reads from current byte(s) into rinfo_.
354 // *Get* just reads and returns info on current byte.
355 void Advance(int bytes = 1) { pos_ -= bytes; }
356 int AdvanceGetTag();
357 int GetExtraTag();
358 int GetTopTag();
359 void ReadTaggedPC();
360 void AdvanceReadPC();
361 void AdvanceReadData();
362 void AdvanceReadVariableLengthPCJump();
363 int GetPositionTypeTag();
364 void ReadTaggedData();
365
366 static RelocInfo::Mode DebugInfoModeFromTag(int tag);
367
368 // If the given mode is wanted, set it in rinfo_ and return true.
369 // Else return false. Used for efficiently skipping unwanted modes.
370 bool SetMode(RelocInfo::Mode mode) {
371 return (mode_mask_ & 1 << mode) ? (rinfo_.rmode_ = mode, true) : false;
372 }
373
374 byte* pos_;
375 byte* end_;
376 RelocInfo rinfo_;
377 bool done_;
378 int mode_mask_;
379 DISALLOW_COPY_AND_ASSIGN(RelocIterator);
380};
381
382
383//------------------------------------------------------------------------------
384// External function
385
386//----------------------------------------------------------------------------
387class IC_Utility;
388class SCTableReference;
389#ifdef ENABLE_DEBUGGER_SUPPORT
390class Debug_Address;
391#endif
392
393
394typedef void* ExternalReferenceRedirector(void* original, bool fp_return);
395
396
397// An ExternalReference represents a C++ address used in the generated
398// code. All references to C++ functions and variables must be encapsulated in
399// an ExternalReference instance. This is done in order to track the origin of
400// all external references in the code so that they can be bound to the correct
401// addresses when deserializing a heap.
402class ExternalReference BASE_EMBEDDED {
403 public:
404 explicit ExternalReference(Builtins::CFunctionId id);
405
Steve Blockd0582a62009-12-15 09:54:21 +0000406 explicit ExternalReference(ApiFunction* ptr);
407
Steve Blocka7e24c12009-10-30 11:49:00 +0000408 explicit ExternalReference(Builtins::Name name);
409
410 explicit ExternalReference(Runtime::FunctionId id);
411
412 explicit ExternalReference(Runtime::Function* f);
413
414 explicit ExternalReference(const IC_Utility& ic_utility);
415
416#ifdef ENABLE_DEBUGGER_SUPPORT
417 explicit ExternalReference(const Debug_Address& debug_address);
418#endif
419
420 explicit ExternalReference(StatsCounter* counter);
421
422 explicit ExternalReference(Top::AddressId id);
423
424 explicit ExternalReference(const SCTableReference& table_ref);
425
426 // One-of-a-kind references. These references are not part of a general
427 // pattern. This means that they have to be added to the
428 // ExternalReferenceTable in serialize.cc manually.
429
430 static ExternalReference perform_gc_function();
Steve Block6ded16b2010-05-10 14:33:55 +0100431 static ExternalReference fill_heap_number_with_random_function();
432 static ExternalReference random_uint32_function();
Andrei Popescu402d9372010-02-26 13:31:12 +0000433 static ExternalReference transcendental_cache_array_address();
Steve Blocka7e24c12009-10-30 11:49:00 +0000434
Leon Clarkee46be812010-01-19 14:06:41 +0000435 // Static data in the keyed lookup cache.
436 static ExternalReference keyed_lookup_cache_keys();
437 static ExternalReference keyed_lookup_cache_field_offsets();
438
Steve Blocka7e24c12009-10-30 11:49:00 +0000439 // Static variable Factory::the_hole_value.location()
440 static ExternalReference the_hole_value_location();
441
442 // Static variable Heap::roots_address()
443 static ExternalReference roots_address();
444
445 // Static variable StackGuard::address_of_jslimit()
Steve Blockd0582a62009-12-15 09:54:21 +0000446 static ExternalReference address_of_stack_limit();
447
448 // Static variable StackGuard::address_of_real_jslimit()
449 static ExternalReference address_of_real_stack_limit();
Steve Blocka7e24c12009-10-30 11:49:00 +0000450
451 // Static variable RegExpStack::limit_address()
452 static ExternalReference address_of_regexp_stack_limit();
453
Leon Clarkee46be812010-01-19 14:06:41 +0000454 // Static variables for RegExp.
455 static ExternalReference address_of_static_offsets_vector();
456 static ExternalReference address_of_regexp_stack_memory_address();
457 static ExternalReference address_of_regexp_stack_memory_size();
458
Steve Blocka7e24c12009-10-30 11:49:00 +0000459 // Static variable Heap::NewSpaceStart()
460 static ExternalReference new_space_start();
Andrei Popescu402d9372010-02-26 13:31:12 +0000461 static ExternalReference new_space_mask();
Steve Blocka7e24c12009-10-30 11:49:00 +0000462 static ExternalReference heap_always_allocate_scope_depth();
463
464 // Used for fast allocation in generated code.
465 static ExternalReference new_space_allocation_top_address();
466 static ExternalReference new_space_allocation_limit_address();
467
468 static ExternalReference double_fp_operation(Token::Value operation);
469 static ExternalReference compare_doubles();
470
Steve Blockd0582a62009-12-15 09:54:21 +0000471 static ExternalReference handle_scope_extensions_address();
472 static ExternalReference handle_scope_next_address();
473 static ExternalReference handle_scope_limit_address();
474
475 static ExternalReference scheduled_exception_address();
476
Steve Blocka7e24c12009-10-30 11:49:00 +0000477 Address address() const {return reinterpret_cast<Address>(address_);}
478
479#ifdef ENABLE_DEBUGGER_SUPPORT
480 // Function Debug::Break()
481 static ExternalReference debug_break();
482
483 // Used to check if single stepping is enabled in generated code.
484 static ExternalReference debug_step_in_fp_address();
485#endif
486
Steve Block6ded16b2010-05-10 14:33:55 +0100487#ifndef V8_INTERPRETED_REGEXP
Steve Blocka7e24c12009-10-30 11:49:00 +0000488 // C functions called from RegExp generated code.
489
490 // Function NativeRegExpMacroAssembler::CaseInsensitiveCompareUC16()
491 static ExternalReference re_case_insensitive_compare_uc16();
492
493 // Function RegExpMacroAssembler*::CheckStackGuardState()
494 static ExternalReference re_check_stack_guard_state();
495
496 // Function NativeRegExpMacroAssembler::GrowStack()
497 static ExternalReference re_grow_stack();
Leon Clarkee46be812010-01-19 14:06:41 +0000498
499 // byte NativeRegExpMacroAssembler::word_character_bitmap
500 static ExternalReference re_word_character_map();
501
Steve Blocka7e24c12009-10-30 11:49:00 +0000502#endif
503
504 // This lets you register a function that rewrites all external references.
505 // Used by the ARM simulator to catch calls to external references.
506 static void set_redirector(ExternalReferenceRedirector* redirector) {
507 ASSERT(redirector_ == NULL); // We can't stack them.
508 redirector_ = redirector;
509 }
510
511 private:
512 explicit ExternalReference(void* address)
513 : address_(address) {}
514
515 static ExternalReferenceRedirector* redirector_;
516
517 static void* Redirect(void* address, bool fp_return = false) {
518 if (redirector_ == NULL) return address;
Steve Blockd0582a62009-12-15 09:54:21 +0000519 void* answer = (*redirector_)(address, fp_return);
520 return answer;
Steve Blocka7e24c12009-10-30 11:49:00 +0000521 }
522
523 static void* Redirect(Address address_arg, bool fp_return = false) {
524 void* address = reinterpret_cast<void*>(address_arg);
Steve Blockd0582a62009-12-15 09:54:21 +0000525 void* answer = (redirector_ == NULL) ?
526 address :
527 (*redirector_)(address, fp_return);
528 return answer;
Steve Blocka7e24c12009-10-30 11:49:00 +0000529 }
530
531 void* address_;
532};
533
534
535// -----------------------------------------------------------------------------
536// Utility functions
537
538static inline bool is_intn(int x, int n) {
539 return -(1 << (n-1)) <= x && x < (1 << (n-1));
540}
541
Steve Blocka7e24c12009-10-30 11:49:00 +0000542static inline bool is_int8(int x) { return is_intn(x, 8); }
Andrei Popescu31002712010-02-23 13:46:05 +0000543static inline bool is_int16(int x) { return is_intn(x, 16); }
544static inline bool is_int18(int x) { return is_intn(x, 18); }
545static inline bool is_int24(int x) { return is_intn(x, 24); }
Steve Blocka7e24c12009-10-30 11:49:00 +0000546
547static inline bool is_uintn(int x, int n) {
548 return (x & -(1 << n)) == 0;
549}
550
551static inline bool is_uint2(int x) { return is_uintn(x, 2); }
552static inline bool is_uint3(int x) { return is_uintn(x, 3); }
553static inline bool is_uint4(int x) { return is_uintn(x, 4); }
554static inline bool is_uint5(int x) { return is_uintn(x, 5); }
555static inline bool is_uint6(int x) { return is_uintn(x, 6); }
556static inline bool is_uint8(int x) { return is_uintn(x, 8); }
Andrei Popescu31002712010-02-23 13:46:05 +0000557static inline bool is_uint10(int x) { return is_uintn(x, 10); }
Steve Blocka7e24c12009-10-30 11:49:00 +0000558static inline bool is_uint12(int x) { return is_uintn(x, 12); }
559static inline bool is_uint16(int x) { return is_uintn(x, 16); }
560static inline bool is_uint24(int x) { return is_uintn(x, 24); }
Andrei Popescu31002712010-02-23 13:46:05 +0000561static inline bool is_uint26(int x) { return is_uintn(x, 26); }
562static inline bool is_uint28(int x) { return is_uintn(x, 28); }
563
564static inline int NumberOfBitsSet(uint32_t x) {
565 unsigned int num_bits_set;
566 for (num_bits_set = 0; x; x >>= 1) {
567 num_bits_set += x & 1;
568 }
569 return num_bits_set;
570}
Steve Blocka7e24c12009-10-30 11:49:00 +0000571
572} } // namespace v8::internal
573
574#endif // V8_ASSEMBLER_H_