blob: b3c8afbada4378372f3a41f6eebe3a876d7b3937 [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.
33// Copyright 2006-2008 Google Inc. All Rights Reserved.
34
35#include "v8.h"
36
37#include "arguments.h"
38#include "execution.h"
39#include "ic-inl.h"
40#include "factory.h"
41#include "runtime.h"
42#include "serialize.h"
43#include "stub-cache.h"
44
45namespace v8 { namespace internal {
46
47
48// -----------------------------------------------------------------------------
49// Implementation of Label
50
51int Label::pos() const {
52 if (pos_ < 0) return -pos_ - 1;
53 if (pos_ > 0) return pos_ - 1;
54 UNREACHABLE();
55 return 0;
56}
57
58
59// -----------------------------------------------------------------------------
60// Implementation of RelocInfoWriter and RelocIterator
61//
62// Encoding
63//
64// The most common modes are given single-byte encodings. Also, it is
65// easy to identify the type of reloc info and skip unwanted modes in
66// an iteration.
67//
68// The encoding relies on the fact that there are less than 14
69// different relocation modes.
70//
71// embedded_object: [6 bits pc delta] 00
72//
73// code_taget: [6 bits pc delta] 01
74//
75// position: [6 bits pc delta] 10,
76// [7 bits signed data delta] 0
77//
78// statement_position: [6 bits pc delta] 10,
79// [7 bits signed data delta] 1
80//
81// any nondata mode: 00 [4 bits rmode] 11,
82// 00 [6 bits pc delta]
83//
84// pc-jump: 00 1111 11,
85// 00 [6 bits pc delta]
86//
87// pc-jump: 01 1111 11,
88// (variable length) 7 - 26 bit pc delta, written in chunks of 7
89// bits, the lowest 7 bits written first.
90//
91// data-jump + pos: 00 1110 11,
92// signed int, lowest byte written first
93//
94// data-jump + st.pos: 01 1110 11,
95// signed int, lowest byte written first
96//
97// data-jump + comm.: 10 1110 11,
98// signed int, lowest byte written first
99//
100const int kMaxRelocModes = 14;
101
102const int kTagBits = 2;
103const int kTagMask = (1 << kTagBits) - 1;
104const int kExtraTagBits = 4;
105const int kPositionTypeTagBits = 1;
106const int kSmallDataBits = kBitsPerByte - kPositionTypeTagBits;
107
108const int kEmbeddedObjectTag = 0;
109const int kCodeTargetTag = 1;
110const int kPositionTag = 2;
111const int kDefaultTag = 3;
112
113const int kPCJumpTag = (1 << kExtraTagBits) - 1;
114
115const int kSmallPCDeltaBits = kBitsPerByte - kTagBits;
116const int kSmallPCDeltaMask = (1 << kSmallPCDeltaBits) - 1;
117
118const int kVariableLengthPCJumpTopTag = 1;
119const int kChunkBits = 7;
120const int kChunkMask = (1 << kChunkBits) - 1;
121const int kLastChunkTagBits = 1;
122const int kLastChunkTagMask = 1;
123const int kLastChunkTag = 1;
124
125
126const int kDataJumpTag = kPCJumpTag - 1;
127
128const int kNonstatementPositionTag = 0;
129const int kStatementPositionTag = 1;
130const int kCommentTag = 2;
131
132
133uint32_t RelocInfoWriter::WriteVariableLengthPCJump(uint32_t pc_delta) {
134 // Return if the pc_delta can fit in kSmallPCDeltaBits bits.
135 // Otherwise write a variable length PC jump for the bits that do
136 // not fit in the kSmallPCDeltaBits bits.
137 if (is_uintn(pc_delta, kSmallPCDeltaBits)) return pc_delta;
138 WriteExtraTag(kPCJumpTag, kVariableLengthPCJumpTopTag);
139 uint32_t pc_jump = pc_delta >> kSmallPCDeltaBits;
140 ASSERT(pc_jump > 0);
141 // Write kChunkBits size chunks of the pc_jump.
142 for (; pc_jump > 0; pc_jump = pc_jump >> kChunkBits) {
143 byte b = pc_jump & kChunkMask;
144 *--pos_ = b << kLastChunkTagBits;
145 }
146 // Tag the last chunk so it can be identified.
147 *pos_ = *pos_ | kLastChunkTag;
148 // Return the remaining kSmallPCDeltaBits of the pc_delta.
149 return pc_delta & kSmallPCDeltaMask;
150}
151
152
153void RelocInfoWriter::WriteTaggedPC(uint32_t pc_delta, int tag) {
154 // Write a byte of tagged pc-delta, possibly preceded by var. length pc-jump.
155 pc_delta = WriteVariableLengthPCJump(pc_delta);
156 *--pos_ = pc_delta << kTagBits | tag;
157}
158
159
160void RelocInfoWriter::WriteTaggedData(int32_t data_delta, int tag) {
161 *--pos_ = data_delta << kPositionTypeTagBits | tag;
162}
163
164
165void RelocInfoWriter::WriteExtraTag(int extra_tag, int top_tag) {
166 *--pos_ = top_tag << (kTagBits + kExtraTagBits) |
167 extra_tag << kTagBits |
168 kDefaultTag;
169}
170
171
172void RelocInfoWriter::WriteExtraTaggedPC(uint32_t pc_delta, int extra_tag) {
173 // Write two-byte tagged pc-delta, possibly preceded by var. length pc-jump.
174 pc_delta = WriteVariableLengthPCJump(pc_delta);
175 WriteExtraTag(extra_tag, 0);
176 *--pos_ = pc_delta;
177}
178
179
180void RelocInfoWriter::WriteExtraTaggedData(int32_t data_delta, int top_tag) {
181 WriteExtraTag(kDataJumpTag, top_tag);
182 for (int i = 0; i < kIntSize; i++) {
183 *--pos_ = data_delta;
184 data_delta = ArithmeticShiftRight(data_delta, kBitsPerByte);
185 }
186}
187
188
189void RelocInfoWriter::Write(const RelocInfo* rinfo) {
190#ifdef DEBUG
191 byte* begin_pos = pos_;
192#endif
193 Counters::reloc_info_count.Increment();
194 ASSERT(rinfo->pc() - last_pc_ >= 0);
195 ASSERT(reloc_mode_count < kMaxRelocModes);
196 // Use unsigned delta-encoding for pc.
197 uint32_t pc_delta = rinfo->pc() - last_pc_;
198 RelocMode rmode = rinfo->rmode();
199
200 // The two most common modes are given small tags, and usually fit in a byte.
201 if (rmode == embedded_object) {
202 WriteTaggedPC(pc_delta, kEmbeddedObjectTag);
203 } else if (rmode == code_target) {
204 WriteTaggedPC(pc_delta, kCodeTargetTag);
205 } else if (rmode == position || rmode == statement_position) {
206 // Use signed delta-encoding for data.
207 int32_t data_delta = rinfo->data() - last_data_;
208 int pos_type_tag = rmode == position ? kNonstatementPositionTag
209 : kStatementPositionTag;
210 // Check if data is small enough to fit in a tagged byte.
211 if (is_intn(data_delta, kSmallDataBits)) {
212 WriteTaggedPC(pc_delta, kPositionTag);
213 WriteTaggedData(data_delta, pos_type_tag);
214 last_data_ = rinfo->data();
215 } else {
216 // Otherwise, use costly encoding.
217 WriteExtraTaggedPC(pc_delta, kPCJumpTag);
218 WriteExtraTaggedData(data_delta, pos_type_tag);
219 last_data_ = rinfo->data();
220 }
221 } else if (rmode == comment) {
222 // Comments are normally not generated, so we use the costly encoding.
223 WriteExtraTaggedPC(pc_delta, kPCJumpTag);
224 WriteExtraTaggedData(rinfo->data() - last_data_, kCommentTag);
225 last_data_ = rinfo->data();
226 } else {
227 // For all other modes we simply use the mode as the extra tag.
228 // None of these modes need a data component.
229 ASSERT(rmode < kPCJumpTag && rmode < kDataJumpTag);
230 WriteExtraTaggedPC(pc_delta, rmode);
231 }
232 last_pc_ = rinfo->pc();
233#ifdef DEBUG
234 ASSERT(begin_pos - pos_ <= kMaxSize);
235#endif
236}
237
238
239inline int RelocIterator::AdvanceGetTag() {
240 return *--pos_ & kTagMask;
241}
242
243
244inline int RelocIterator::GetExtraTag() {
245 return (*pos_ >> kTagBits) & ((1 << kExtraTagBits) - 1);
246}
247
248
249inline int RelocIterator::GetTopTag() {
250 return *pos_ >> (kTagBits + kExtraTagBits);
251}
252
253
254inline void RelocIterator::ReadTaggedPC() {
255 rinfo_.pc_ += *pos_ >> kTagBits;
256}
257
258
259inline void RelocIterator::AdvanceReadPC() {
260 rinfo_.pc_ += *--pos_;
261}
262
263
264void RelocIterator::AdvanceReadData() {
265 int32_t x = 0;
266 for (int i = 0; i < kIntSize; i++) {
267 x |= *--pos_ << i * kBitsPerByte;
268 }
269 rinfo_.data_ += x;
270}
271
272
273void RelocIterator::AdvanceReadVariableLengthPCJump() {
274 // Read the 32-kSmallPCDeltaBits most significant bits of the
275 // pc jump in kChunkBits bit chunks and shift them into place.
276 // Stop when the last chunk is encountered.
277 uint32_t pc_jump = 0;
278 for (int i = 0; i < kIntSize; i++) {
279 byte pc_jump_part = *--pos_;
280 pc_jump |= (pc_jump_part >> kLastChunkTagBits) << i * kChunkBits;
281 if ((pc_jump_part & kLastChunkTagMask) == 1) break;
282 }
283 // The least significant kSmallPCDeltaBits bits will be added
284 // later.
285 rinfo_.pc_ += pc_jump << kSmallPCDeltaBits;
286}
287
288
289inline int RelocIterator::GetPositionTypeTag() {
290 return *pos_ & ((1 << kPositionTypeTagBits) - 1);
291}
292
293
294inline void RelocIterator::ReadTaggedData() {
295 int8_t signed_b = *pos_;
296 rinfo_.data_ += ArithmeticShiftRight(signed_b, kPositionTypeTagBits);
297}
298
299
300inline RelocMode RelocIterator::DebugInfoModeFromTag(int tag) {
301 if (tag == kStatementPositionTag) {
302 return statement_position;
303 } else if (tag == kNonstatementPositionTag) {
304 return position;
305 } else {
306 ASSERT(tag == kCommentTag);
307 return comment;
308 }
309}
310
311
312void RelocIterator::next() {
313 ASSERT(!done());
314 // Basically, do the opposite of RelocInfoWriter::Write.
315 // Reading of data is as far as possible avoided for unwanted modes,
316 // but we must always update the pc.
317 //
318 // We exit this loop by returning when we find a mode we want.
319 while (pos_ > end_) {
320 int tag = AdvanceGetTag();
321 if (tag == kEmbeddedObjectTag) {
322 ReadTaggedPC();
323 if (SetMode(embedded_object)) return;
324 } else if (tag == kCodeTargetTag) {
325 ReadTaggedPC();
326 if (*(reinterpret_cast<int**>(rinfo_.pc())) ==
327 reinterpret_cast<int*>(0x61)) {
328 tag = 0;
329 }
330 if (SetMode(code_target)) return;
331 } else if (tag == kPositionTag) {
332 ReadTaggedPC();
333 Advance();
334 // Check if we want source positions.
335 if (mode_mask_ & RelocInfo::kPositionMask) {
336 // Check if we want this type of source position.
337 if (SetMode(DebugInfoModeFromTag(GetPositionTypeTag()))) {
338 // Finally read the data before returning.
339 ReadTaggedData();
340 return;
341 }
342 }
343 } else {
344 ASSERT(tag == kDefaultTag);
345 int extra_tag = GetExtraTag();
346 if (extra_tag == kPCJumpTag) {
347 int top_tag = GetTopTag();
348 if (top_tag == kVariableLengthPCJumpTopTag) {
349 AdvanceReadVariableLengthPCJump();
350 } else {
351 AdvanceReadPC();
352 }
353 } else if (extra_tag == kDataJumpTag) {
354 // Check if we want debug modes (the only ones with data).
355 if (mode_mask_ & RelocInfo::kDebugMask) {
356 int top_tag = GetTopTag();
357 AdvanceReadData();
358 if (SetMode(DebugInfoModeFromTag(top_tag))) return;
359 } else {
360 // Otherwise, just skip over the data.
361 Advance(kIntSize);
362 }
363 } else {
364 AdvanceReadPC();
365 if (SetMode(static_cast<RelocMode>(extra_tag))) return;
366 }
367 }
368 }
369 done_ = true;
370}
371
372
373RelocIterator::RelocIterator(Code* code, int mode_mask) {
374 rinfo_.pc_ = code->instruction_start();
375 rinfo_.data_ = 0;
376 // relocation info is read backwards
377 pos_ = code->relocation_start() + code->relocation_size();
378 end_ = code->relocation_start();
379 done_ = false;
380 mode_mask_ = mode_mask;
381 if (mode_mask_ == 0) pos_ = end_;
382 next();
383}
384
385
386RelocIterator::RelocIterator(const CodeDesc& desc, int mode_mask) {
387 rinfo_.pc_ = desc.buffer;
388 rinfo_.data_ = 0;
389 // relocation info is read backwards
390 pos_ = desc.buffer + desc.buffer_size;
391 end_ = pos_ - desc.reloc_size;
392 done_ = false;
393 mode_mask_ = mode_mask;
394 if (mode_mask_ == 0) pos_ = end_;
395 next();
396}
397
398
399// -----------------------------------------------------------------------------
400// Implementation of RelocInfo
401
402
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000403#ifdef ENABLE_DISASSEMBLER
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000404const char* RelocInfo::RelocModeName(RelocMode rmode) {
405 switch (rmode) {
406 case no_reloc:
407 return "no reloc";
408 case embedded_object:
409 return "embedded object";
410 case embedded_string:
411 return "embedded string";
412 case js_construct_call:
413 return "code target (js construct call)";
414 case exit_js_frame:
415 return "code target (exit js frame)";
416 case code_target_context:
417 return "code target (context)";
418 case code_target:
419 return "code target";
420 case runtime_entry:
421 return "runtime entry";
422 case js_return:
423 return "js return";
424 case comment:
425 return "comment";
426 case position:
427 return "position";
428 case statement_position:
429 return "statement position";
430 case external_reference:
431 return "external reference";
432 case reloc_mode_count:
433 UNREACHABLE();
434 return "reloc_mode_count";
435 }
436 return "unknown relocation type";
437}
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000438#endif // ENABLE_DISASSEMBLER
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000439
440
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000441#ifdef DEBUG
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000442void RelocInfo::Print() {
443 PrintF("%p %s", pc_, RelocModeName(rmode_));
444 if (rmode_ == comment) {
445 PrintF(" (%s)", data_);
446 } else if (rmode_ == embedded_object) {
447 PrintF(" (");
448 target_object()->ShortPrint();
449 PrintF(")");
450 } else if (rmode_ == external_reference) {
451 ExternalReferenceEncoder ref_encoder;
452 PrintF(" (%s) (%p)",
453 ref_encoder.NameOfAddress(*target_reference_address()),
454 *target_reference_address());
455 } else if (is_code_target(rmode_)) {
456 Code* code = Debug::GetCodeTarget(target_address());
457 PrintF(" (%s) (%p)", Code::Kind2String(code->kind()), target_address());
458 } else if (is_position(rmode_)) {
459 PrintF(" (%d)", data());
460 }
461
462 PrintF("\n");
463}
464
465
466void RelocInfo::Verify() {
467 switch (rmode_) {
468 case embedded_object:
469 Object::VerifyPointer(target_object());
470 break;
471 case js_construct_call:
472 case exit_js_frame:
473 case code_target_context:
474 case code_target: {
475 // convert inline target address to code object
476 Address addr = target_address();
477 ASSERT(addr != NULL);
478 // Check that we can find the right code object.
479 HeapObject* code = HeapObject::FromAddress(addr - Code::kHeaderSize);
480 Object* found = Heap::FindCodeObject(addr);
481 ASSERT(found->IsCode());
482 ASSERT(code->address() == HeapObject::cast(found)->address());
483 break;
484 }
485 case embedded_string:
486 case runtime_entry:
487 case js_return:
488 case comment:
489 case position:
490 case statement_position:
491 case external_reference:
492 case no_reloc:
493 break;
494 case reloc_mode_count:
495 UNREACHABLE();
496 break;
497 }
498}
499#endif // DEBUG
500
501
502// -----------------------------------------------------------------------------
503// Implementation of ExternalReference
504
505ExternalReference::ExternalReference(Builtins::CFunctionId id)
506 : address_(Builtins::c_function_address(id)) {}
507
508
509ExternalReference::ExternalReference(Builtins::Name name)
510 : address_(Builtins::builtin_address(name)) {}
511
512
513ExternalReference::ExternalReference(Runtime::FunctionId id)
514 : address_(Runtime::FunctionForId(id)->entry) {}
515
516
517ExternalReference::ExternalReference(Runtime::Function* f)
518 : address_(f->entry) {}
519
520
521ExternalReference::ExternalReference(const IC_Utility& ic_utility)
522 : address_(ic_utility.address()) {}
523
524
525ExternalReference::ExternalReference(const Debug_Address& debug_address)
526 : address_(debug_address.address()) {}
527
528
529ExternalReference::ExternalReference(StatsCounter* counter)
530 : address_(reinterpret_cast<Address>(counter->GetInternalPointer())) {}
531
532
533ExternalReference::ExternalReference(Top::AddressId id)
534 : address_(Top::get_address_from_id(id)) {}
535
536
537ExternalReference::ExternalReference(const SCTableReference& table_ref)
538 : address_(table_ref.address()) {}
539
540
541ExternalReference ExternalReference::builtin_passed_function() {
542 return ExternalReference(&Builtins::builtin_passed_function);
543}
544
545ExternalReference ExternalReference::the_hole_value_location() {
546 return ExternalReference(Factory::the_hole_value().location());
547}
548
549
550ExternalReference ExternalReference::address_of_stack_guard_limit() {
551 return ExternalReference(StackGuard::address_of_jslimit());
552}
553
554
555ExternalReference ExternalReference::debug_break() {
556 return ExternalReference(FUNCTION_ADDR(Debug::Break));
557}
558
559
560ExternalReference ExternalReference::new_space_start() {
561 return ExternalReference(Heap::NewSpaceStart());
562}
563
564ExternalReference ExternalReference::new_space_allocation_top_address() {
565 return ExternalReference(Heap::NewSpaceAllocationTopAddress());
566}
567
568ExternalReference ExternalReference::new_space_allocation_limit_address() {
569 return ExternalReference(Heap::NewSpaceAllocationLimitAddress());
570}
571
572ExternalReference ExternalReference::debug_step_in_fp_address() {
573 return ExternalReference(Debug::step_in_fp_addr());
574}
575
576} } // namespace v8::internal