blob: d0d9e19c9013344990304438a152577199e80995 [file] [log] [blame]
Ben Murdochc7cc0282012-03-05 14:35:55 +00001// Copyright 2012 the V8 project authors. All rights reserved.
Steve Blocka7e24c12009-10-30 11:49:00 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#include "v8.h"
29
Leon Clarkef7060e22010-06-03 12:02:55 +010030#if defined(V8_TARGET_ARCH_IA32)
31
Steve Blocka7e24c12009-10-30 11:49:00 +000032#include "bootstrapper.h"
Ben Murdoch8b112d22011-06-08 16:22:53 +010033#include "codegen.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000034#include "debug.h"
35#include "runtime.h"
36#include "serialize.h"
37
38namespace v8 {
39namespace internal {
40
41// -------------------------------------------------------------------------
42// MacroAssembler implementation.
43
Ben Murdoch8b112d22011-06-08 16:22:53 +010044MacroAssembler::MacroAssembler(Isolate* arg_isolate, void* buffer, int size)
45 : Assembler(arg_isolate, buffer, size),
Steve Blocka7e24c12009-10-30 11:49:00 +000046 generating_stub_(false),
Ben Murdoch592a9fc2012-03-05 11:04:45 +000047 allow_stub_calls_(true),
48 has_frame_(false) {
Ben Murdoch8b112d22011-06-08 16:22:53 +010049 if (isolate() != NULL) {
50 code_object_ = Handle<Object>(isolate()->heap()->undefined_value(),
51 isolate());
52 }
Steve Blocka7e24c12009-10-30 11:49:00 +000053}
54
55
Ben Murdoch592a9fc2012-03-05 11:04:45 +000056void MacroAssembler::InNewSpace(
57 Register object,
58 Register scratch,
59 Condition cc,
60 Label* condition_met,
61 Label::Distance condition_met_distance) {
62 ASSERT(cc == equal || cc == not_equal);
63 if (scratch.is(object)) {
64 and_(scratch, Immediate(~Page::kPageAlignmentMask));
65 } else {
66 mov(scratch, Immediate(~Page::kPageAlignmentMask));
67 and_(scratch, object);
Steve Block6ded16b2010-05-10 14:33:55 +010068 }
Ben Murdoch592a9fc2012-03-05 11:04:45 +000069 // Check that we can use a test_b.
70 ASSERT(MemoryChunk::IN_FROM_SPACE < 8);
71 ASSERT(MemoryChunk::IN_TO_SPACE < 8);
72 int mask = (1 << MemoryChunk::IN_FROM_SPACE)
73 | (1 << MemoryChunk::IN_TO_SPACE);
74 // If non-zero, the page belongs to new-space.
75 test_b(Operand(scratch, MemoryChunk::kFlagsOffset),
76 static_cast<uint8_t>(mask));
77 j(cc, condition_met, condition_met_distance);
78}
Steve Block6ded16b2010-05-10 14:33:55 +010079
Steve Blocka7e24c12009-10-30 11:49:00 +000080
Ben Murdoch592a9fc2012-03-05 11:04:45 +000081void MacroAssembler::RememberedSetHelper(
82 Register object, // Only used for debug checks.
83 Register addr,
84 Register scratch,
85 SaveFPRegsMode save_fp,
86 MacroAssembler::RememberedSetFinalAction and_then) {
87 Label done;
88 if (FLAG_debug_code) {
89 Label ok;
90 JumpIfNotInNewSpace(object, scratch, &ok, Label::kNear);
91 int3();
92 bind(&ok);
93 }
94 // Load store buffer top.
95 ExternalReference store_buffer =
96 ExternalReference::store_buffer_top(isolate());
97 mov(scratch, Operand::StaticVariable(store_buffer));
98 // Store pointer to buffer.
99 mov(Operand(scratch, 0), addr);
100 // Increment buffer top.
101 add(scratch, Immediate(kPointerSize));
102 // Write back new top of buffer.
103 mov(Operand::StaticVariable(store_buffer), scratch);
104 // Call stub on end of buffer.
105 // Check for end of buffer.
106 test(scratch, Immediate(StoreBuffer::kStoreBufferOverflowBit));
107 if (and_then == kReturnAtEnd) {
108 Label buffer_overflowed;
109 j(not_equal, &buffer_overflowed, Label::kNear);
110 ret(0);
111 bind(&buffer_overflowed);
112 } else {
113 ASSERT(and_then == kFallThroughAtEnd);
114 j(equal, &done, Label::kNear);
115 }
116 StoreBufferOverflowStub store_buffer_overflow =
117 StoreBufferOverflowStub(save_fp);
118 CallStub(&store_buffer_overflow);
119 if (and_then == kReturnAtEnd) {
120 ret(0);
121 } else {
122 ASSERT(and_then == kFallThroughAtEnd);
123 bind(&done);
124 }
Ben Murdoch257744e2011-11-30 15:57:28 +0000125}
126
127
128void MacroAssembler::ClampDoubleToUint8(XMMRegister input_reg,
129 XMMRegister scratch_reg,
130 Register result_reg) {
131 Label done;
132 ExternalReference zero_ref = ExternalReference::address_of_zero();
133 movdbl(scratch_reg, Operand::StaticVariable(zero_ref));
134 Set(result_reg, Immediate(0));
135 ucomisd(input_reg, scratch_reg);
136 j(below, &done, Label::kNear);
137 ExternalReference half_ref = ExternalReference::address_of_one_half();
138 movdbl(scratch_reg, Operand::StaticVariable(half_ref));
139 addsd(scratch_reg, input_reg);
140 cvttsd2si(result_reg, Operand(scratch_reg));
141 test(result_reg, Immediate(0xFFFFFF00));
142 j(zero, &done, Label::kNear);
143 Set(result_reg, Immediate(255));
144 bind(&done);
145}
146
147
148void MacroAssembler::ClampUint8(Register reg) {
149 Label done;
150 test(reg, Immediate(0xFFFFFF00));
151 j(zero, &done, Label::kNear);
152 setcc(negative, reg); // 1 if negative, 0 if positive.
153 dec_b(reg); // 0 if negative, 255 if positive.
154 bind(&done);
155}
156
157
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000158void MacroAssembler::RecordWriteArray(Register object,
159 Register value,
160 Register index,
161 SaveFPRegsMode save_fp,
162 RememberedSetAction remembered_set_action,
163 SmiCheck smi_check) {
164 // First, check if a write barrier is even needed. The tests below
165 // catch stores of Smis.
166 Label done;
167
168 // Skip barrier if writing a smi.
169 if (smi_check == INLINE_SMI_CHECK) {
170 ASSERT_EQ(0, kSmiTag);
171 test(value, Immediate(kSmiTagMask));
172 j(zero, &done);
173 }
174
175 // Array access: calculate the destination address in the same manner as
176 // KeyedStoreIC::GenerateGeneric. Multiply a smi by 2 to get an offset
177 // into an array of words.
178 Register dst = index;
179 lea(dst, Operand(object, index, times_half_pointer_size,
180 FixedArray::kHeaderSize - kHeapObjectTag));
181
182 RecordWrite(
183 object, dst, value, save_fp, remembered_set_action, OMIT_SMI_CHECK);
184
185 bind(&done);
186
187 // Clobber clobbered input registers when running with the debug-code flag
188 // turned on to provoke errors.
189 if (emit_debug_code()) {
190 mov(value, Immediate(BitCast<int32_t>(kZapValue)));
191 mov(index, Immediate(BitCast<int32_t>(kZapValue)));
Ben Murdoch257744e2011-11-30 15:57:28 +0000192 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000193}
194
195
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000196void MacroAssembler::RecordWriteField(
197 Register object,
198 int offset,
199 Register value,
200 Register dst,
201 SaveFPRegsMode save_fp,
202 RememberedSetAction remembered_set_action,
203 SmiCheck smi_check) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100204 // First, check if a write barrier is even needed. The tests below
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000205 // catch stores of Smis.
Ben Murdoch257744e2011-11-30 15:57:28 +0000206 Label done;
Steve Blocka7e24c12009-10-30 11:49:00 +0000207
208 // Skip barrier if writing a smi.
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000209 if (smi_check == INLINE_SMI_CHECK) {
210 JumpIfSmi(value, &done, Label::kNear);
Steve Blocka7e24c12009-10-30 11:49:00 +0000211 }
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000212
213 // Although the object register is tagged, the offset is relative to the start
214 // of the object, so so offset must be a multiple of kPointerSize.
215 ASSERT(IsAligned(offset, kPointerSize));
216
217 lea(dst, FieldOperand(object, offset));
218 if (emit_debug_code()) {
219 Label ok;
220 test_b(dst, (1 << kPointerSizeLog2) - 1);
221 j(zero, &ok, Label::kNear);
222 int3();
223 bind(&ok);
224 }
225
226 RecordWrite(
227 object, dst, value, save_fp, remembered_set_action, OMIT_SMI_CHECK);
Steve Blocka7e24c12009-10-30 11:49:00 +0000228
229 bind(&done);
Leon Clarke4515c472010-02-03 11:58:03 +0000230
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000231 // Clobber clobbered input registers when running with the debug-code flag
Leon Clarke4515c472010-02-03 11:58:03 +0000232 // turned on to provoke errors.
Steve Block44f0eee2011-05-26 01:26:41 +0100233 if (emit_debug_code()) {
Steve Block6ded16b2010-05-10 14:33:55 +0100234 mov(value, Immediate(BitCast<int32_t>(kZapValue)));
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000235 mov(dst, Immediate(BitCast<int32_t>(kZapValue)));
Leon Clarke4515c472010-02-03 11:58:03 +0000236 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000237}
238
239
Steve Block8defd9f2010-07-08 12:39:36 +0100240void MacroAssembler::RecordWrite(Register object,
241 Register address,
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000242 Register value,
243 SaveFPRegsMode fp_mode,
244 RememberedSetAction remembered_set_action,
245 SmiCheck smi_check) {
246 ASSERT(!object.is(value));
247 ASSERT(!object.is(address));
248 ASSERT(!value.is(address));
249 if (emit_debug_code()) {
250 AbortIfSmi(object);
251 }
252
253 if (remembered_set_action == OMIT_REMEMBERED_SET &&
254 !FLAG_incremental_marking) {
255 return;
256 }
257
258 if (FLAG_debug_code) {
259 Label ok;
260 cmp(value, Operand(address, 0));
261 j(equal, &ok, Label::kNear);
262 int3();
263 bind(&ok);
264 }
265
Steve Block8defd9f2010-07-08 12:39:36 +0100266 // First, check if a write barrier is even needed. The tests below
267 // catch stores of Smis and stores into young gen.
268 Label done;
269
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000270 if (smi_check == INLINE_SMI_CHECK) {
271 // Skip barrier if writing a smi.
272 JumpIfSmi(value, &done, Label::kNear);
273 }
Steve Block8defd9f2010-07-08 12:39:36 +0100274
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000275 CheckPageFlag(value,
276 value, // Used as scratch.
277 MemoryChunk::kPointersToHereAreInterestingMask,
278 zero,
279 &done,
280 Label::kNear);
281 CheckPageFlag(object,
282 value, // Used as scratch.
283 MemoryChunk::kPointersFromHereAreInterestingMask,
284 zero,
285 &done,
286 Label::kNear);
Steve Block8defd9f2010-07-08 12:39:36 +0100287
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000288 RecordWriteStub stub(object, value, address, remembered_set_action, fp_mode);
289 CallStub(&stub);
Steve Block8defd9f2010-07-08 12:39:36 +0100290
291 bind(&done);
292
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000293 // Clobber clobbered registers when running with the debug-code flag
Steve Block8defd9f2010-07-08 12:39:36 +0100294 // turned on to provoke errors.
Steve Block44f0eee2011-05-26 01:26:41 +0100295 if (emit_debug_code()) {
Steve Block8defd9f2010-07-08 12:39:36 +0100296 mov(address, Immediate(BitCast<int32_t>(kZapValue)));
297 mov(value, Immediate(BitCast<int32_t>(kZapValue)));
298 }
299}
300
301
Steve Blocka7e24c12009-10-30 11:49:00 +0000302#ifdef ENABLE_DEBUGGER_SUPPORT
Andrei Popescu402d9372010-02-26 13:31:12 +0000303void MacroAssembler::DebugBreak() {
304 Set(eax, Immediate(0));
Steve Block44f0eee2011-05-26 01:26:41 +0100305 mov(ebx, Immediate(ExternalReference(Runtime::kDebugBreak, isolate())));
Andrei Popescu402d9372010-02-26 13:31:12 +0000306 CEntryStub ces(1);
307 call(ces.GetCode(), RelocInfo::DEBUG_BREAK);
308}
Steve Blocka7e24c12009-10-30 11:49:00 +0000309#endif
310
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100311
Steve Blocka7e24c12009-10-30 11:49:00 +0000312void MacroAssembler::Set(Register dst, const Immediate& x) {
313 if (x.is_zero()) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000314 xor_(dst, dst); // Shorter than mov.
Steve Blocka7e24c12009-10-30 11:49:00 +0000315 } else {
316 mov(dst, x);
317 }
318}
319
320
321void MacroAssembler::Set(const Operand& dst, const Immediate& x) {
322 mov(dst, x);
323}
324
325
Steve Block053d10c2011-06-13 19:13:29 +0100326bool MacroAssembler::IsUnsafeImmediate(const Immediate& x) {
327 static const int kMaxImmediateBits = 17;
328 if (x.rmode_ != RelocInfo::NONE) return false;
329 return !is_intn(x.x_, kMaxImmediateBits);
330}
331
332
333void MacroAssembler::SafeSet(Register dst, const Immediate& x) {
334 if (IsUnsafeImmediate(x) && jit_cookie() != 0) {
335 Set(dst, Immediate(x.x_ ^ jit_cookie()));
336 xor_(dst, jit_cookie());
337 } else {
338 Set(dst, x);
339 }
340}
341
342
343void MacroAssembler::SafePush(const Immediate& x) {
344 if (IsUnsafeImmediate(x) && jit_cookie() != 0) {
345 push(Immediate(x.x_ ^ jit_cookie()));
346 xor_(Operand(esp, 0), Immediate(jit_cookie()));
347 } else {
348 push(x);
349 }
350}
351
352
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000353void MacroAssembler::CompareRoot(Register with, Heap::RootListIndex index) {
354 // see ROOT_ACCESSOR macro in factory.h
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000355 Handle<Object> value(&isolate()->heap()->roots_array_start()[index]);
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000356 cmp(with, value);
357}
358
359
Ben Murdochc7cc0282012-03-05 14:35:55 +0000360void MacroAssembler::CompareRoot(const Operand& with,
361 Heap::RootListIndex index) {
362 // see ROOT_ACCESSOR macro in factory.h
363 Handle<Object> value(&isolate()->heap()->roots_array_start()[index]);
364 cmp(with, value);
365}
366
367
Steve Blocka7e24c12009-10-30 11:49:00 +0000368void MacroAssembler::CmpObjectType(Register heap_object,
369 InstanceType type,
370 Register map) {
371 mov(map, FieldOperand(heap_object, HeapObject::kMapOffset));
372 CmpInstanceType(map, type);
373}
374
375
376void MacroAssembler::CmpInstanceType(Register map, InstanceType type) {
377 cmpb(FieldOperand(map, Map::kInstanceTypeOffset),
378 static_cast<int8_t>(type));
379}
380
381
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000382void MacroAssembler::CheckFastElements(Register map,
383 Label* fail,
384 Label::Distance distance) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000385 STATIC_ASSERT(FAST_SMI_ONLY_ELEMENTS == 0);
386 STATIC_ASSERT(FAST_ELEMENTS == 1);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000387 cmpb(FieldOperand(map, Map::kBitField2Offset),
388 Map::kMaximumBitField2FastElementValue);
389 j(above, fail, distance);
390}
391
392
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000393void MacroAssembler::CheckFastObjectElements(Register map,
394 Label* fail,
395 Label::Distance distance) {
396 STATIC_ASSERT(FAST_SMI_ONLY_ELEMENTS == 0);
397 STATIC_ASSERT(FAST_ELEMENTS == 1);
398 cmpb(FieldOperand(map, Map::kBitField2Offset),
399 Map::kMaximumBitField2FastSmiOnlyElementValue);
400 j(below_equal, fail, distance);
401 cmpb(FieldOperand(map, Map::kBitField2Offset),
402 Map::kMaximumBitField2FastElementValue);
403 j(above, fail, distance);
404}
405
406
407void MacroAssembler::CheckFastSmiOnlyElements(Register map,
408 Label* fail,
409 Label::Distance distance) {
410 STATIC_ASSERT(FAST_SMI_ONLY_ELEMENTS == 0);
411 cmpb(FieldOperand(map, Map::kBitField2Offset),
412 Map::kMaximumBitField2FastSmiOnlyElementValue);
413 j(above, fail, distance);
414}
415
416
417void MacroAssembler::StoreNumberToDoubleElements(
418 Register maybe_number,
419 Register elements,
420 Register key,
421 Register scratch1,
422 XMMRegister scratch2,
423 Label* fail,
424 bool specialize_for_processor) {
425 Label smi_value, done, maybe_nan, not_nan, is_nan, have_double_value;
426 JumpIfSmi(maybe_number, &smi_value, Label::kNear);
427
428 CheckMap(maybe_number,
429 isolate()->factory()->heap_number_map(),
430 fail,
431 DONT_DO_SMI_CHECK);
432
433 // Double value, canonicalize NaN.
434 uint32_t offset = HeapNumber::kValueOffset + sizeof(kHoleNanLower32);
435 cmp(FieldOperand(maybe_number, offset),
436 Immediate(kNaNOrInfinityLowerBoundUpper32));
437 j(greater_equal, &maybe_nan, Label::kNear);
438
439 bind(&not_nan);
440 ExternalReference canonical_nan_reference =
441 ExternalReference::address_of_canonical_non_hole_nan();
442 if (CpuFeatures::IsSupported(SSE2) && specialize_for_processor) {
443 CpuFeatures::Scope use_sse2(SSE2);
444 movdbl(scratch2, FieldOperand(maybe_number, HeapNumber::kValueOffset));
445 bind(&have_double_value);
446 movdbl(FieldOperand(elements, key, times_4, FixedDoubleArray::kHeaderSize),
447 scratch2);
448 } else {
449 fld_d(FieldOperand(maybe_number, HeapNumber::kValueOffset));
450 bind(&have_double_value);
451 fstp_d(FieldOperand(elements, key, times_4, FixedDoubleArray::kHeaderSize));
452 }
453 jmp(&done);
454
455 bind(&maybe_nan);
456 // Could be NaN or Infinity. If fraction is not zero, it's NaN, otherwise
457 // it's an Infinity, and the non-NaN code path applies.
458 j(greater, &is_nan, Label::kNear);
459 cmp(FieldOperand(maybe_number, HeapNumber::kValueOffset), Immediate(0));
460 j(zero, &not_nan);
461 bind(&is_nan);
462 if (CpuFeatures::IsSupported(SSE2) && specialize_for_processor) {
463 CpuFeatures::Scope use_sse2(SSE2);
464 movdbl(scratch2, Operand::StaticVariable(canonical_nan_reference));
465 } else {
466 fld_d(Operand::StaticVariable(canonical_nan_reference));
467 }
468 jmp(&have_double_value, Label::kNear);
469
470 bind(&smi_value);
471 // Value is a smi. Convert to a double and store.
472 // Preserve original value.
473 mov(scratch1, maybe_number);
474 SmiUntag(scratch1);
475 if (CpuFeatures::IsSupported(SSE2) && specialize_for_processor) {
476 CpuFeatures::Scope fscope(SSE2);
477 cvtsi2sd(scratch2, scratch1);
478 movdbl(FieldOperand(elements, key, times_4, FixedDoubleArray::kHeaderSize),
479 scratch2);
480 } else {
481 push(scratch1);
482 fild_s(Operand(esp, 0));
483 pop(scratch1);
484 fstp_d(FieldOperand(elements, key, times_4, FixedDoubleArray::kHeaderSize));
485 }
486 bind(&done);
487}
488
489
Ben Murdochc7cc0282012-03-05 14:35:55 +0000490void MacroAssembler::CompareMap(Register obj,
491 Handle<Map> map,
492 Label* early_success,
493 CompareMapMode mode) {
494 cmp(FieldOperand(obj, HeapObject::kMapOffset), map);
495 if (mode == ALLOW_ELEMENT_TRANSITION_MAPS) {
496 Map* transitioned_fast_element_map(
497 map->LookupElementsTransitionMap(FAST_ELEMENTS, NULL));
498 ASSERT(transitioned_fast_element_map == NULL ||
499 map->elements_kind() != FAST_ELEMENTS);
500 if (transitioned_fast_element_map != NULL) {
501 j(equal, early_success, Label::kNear);
502 cmp(FieldOperand(obj, HeapObject::kMapOffset),
503 Handle<Map>(transitioned_fast_element_map));
504 }
505
506 Map* transitioned_double_map(
507 map->LookupElementsTransitionMap(FAST_DOUBLE_ELEMENTS, NULL));
508 ASSERT(transitioned_double_map == NULL ||
509 map->elements_kind() == FAST_SMI_ONLY_ELEMENTS);
510 if (transitioned_double_map != NULL) {
511 j(equal, early_success, Label::kNear);
512 cmp(FieldOperand(obj, HeapObject::kMapOffset),
513 Handle<Map>(transitioned_double_map));
514 }
515 }
516}
517
518
Andrei Popescu31002712010-02-23 13:46:05 +0000519void MacroAssembler::CheckMap(Register obj,
520 Handle<Map> map,
521 Label* fail,
Ben Murdochc7cc0282012-03-05 14:35:55 +0000522 SmiCheckType smi_check_type,
523 CompareMapMode mode) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000524 if (smi_check_type == DO_SMI_CHECK) {
525 JumpIfSmi(obj, fail);
Andrei Popescu31002712010-02-23 13:46:05 +0000526 }
Ben Murdochc7cc0282012-03-05 14:35:55 +0000527
528 Label success;
529 CompareMap(obj, map, &success, mode);
Andrei Popescu31002712010-02-23 13:46:05 +0000530 j(not_equal, fail);
Ben Murdochc7cc0282012-03-05 14:35:55 +0000531 bind(&success);
Andrei Popescu31002712010-02-23 13:46:05 +0000532}
533
534
Ben Murdoch257744e2011-11-30 15:57:28 +0000535void MacroAssembler::DispatchMap(Register obj,
536 Handle<Map> map,
537 Handle<Code> success,
538 SmiCheckType smi_check_type) {
539 Label fail;
540 if (smi_check_type == DO_SMI_CHECK) {
541 JumpIfSmi(obj, &fail);
542 }
543 cmp(FieldOperand(obj, HeapObject::kMapOffset), Immediate(map));
544 j(equal, success);
545
546 bind(&fail);
547}
548
549
Leon Clarkee46be812010-01-19 14:06:41 +0000550Condition MacroAssembler::IsObjectStringType(Register heap_object,
551 Register map,
552 Register instance_type) {
553 mov(map, FieldOperand(heap_object, HeapObject::kMapOffset));
554 movzx_b(instance_type, FieldOperand(map, Map::kInstanceTypeOffset));
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000555 STATIC_ASSERT(kNotStringTag != 0);
Leon Clarkee46be812010-01-19 14:06:41 +0000556 test(instance_type, Immediate(kIsNotStringMask));
557 return zero;
558}
559
560
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100561void MacroAssembler::IsObjectJSObjectType(Register heap_object,
562 Register map,
563 Register scratch,
564 Label* fail) {
565 mov(map, FieldOperand(heap_object, HeapObject::kMapOffset));
566 IsInstanceJSObjectType(map, scratch, fail);
567}
568
569
570void MacroAssembler::IsInstanceJSObjectType(Register map,
571 Register scratch,
572 Label* fail) {
573 movzx_b(scratch, FieldOperand(map, Map::kInstanceTypeOffset));
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000574 sub(scratch, Immediate(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000575 cmp(scratch,
576 LAST_NONCALLABLE_SPEC_OBJECT_TYPE - FIRST_NONCALLABLE_SPEC_OBJECT_TYPE);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100577 j(above, fail);
578}
579
580
Steve Blocka7e24c12009-10-30 11:49:00 +0000581void MacroAssembler::FCmp() {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100582 if (CpuFeatures::IsSupported(CMOV)) {
Steve Block3ce2e202009-11-05 08:53:23 +0000583 fucomip();
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000584 fstp(0);
Steve Block3ce2e202009-11-05 08:53:23 +0000585 } else {
586 fucompp();
587 push(eax);
588 fnstsw_ax();
589 sahf();
590 pop(eax);
591 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000592}
593
594
Steve Block6ded16b2010-05-10 14:33:55 +0100595void MacroAssembler::AbortIfNotNumber(Register object) {
Andrei Popescu402d9372010-02-26 13:31:12 +0000596 Label ok;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000597 JumpIfSmi(object, &ok);
Andrei Popescu402d9372010-02-26 13:31:12 +0000598 cmp(FieldOperand(object, HeapObject::kMapOffset),
Steve Block44f0eee2011-05-26 01:26:41 +0100599 isolate()->factory()->heap_number_map());
Steve Block6ded16b2010-05-10 14:33:55 +0100600 Assert(equal, "Operand not a number");
Andrei Popescu402d9372010-02-26 13:31:12 +0000601 bind(&ok);
602}
603
604
Steve Block6ded16b2010-05-10 14:33:55 +0100605void MacroAssembler::AbortIfNotSmi(Register object) {
606 test(object, Immediate(kSmiTagMask));
Iain Merrick75681382010-08-19 15:07:18 +0100607 Assert(equal, "Operand is not a smi");
608}
609
610
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100611void MacroAssembler::AbortIfNotString(Register object) {
612 test(object, Immediate(kSmiTagMask));
613 Assert(not_equal, "Operand is not a string");
614 push(object);
615 mov(object, FieldOperand(object, HeapObject::kMapOffset));
616 CmpInstanceType(object, FIRST_NONSTRING_TYPE);
617 pop(object);
618 Assert(below, "Operand is not a string");
619}
620
621
Iain Merrick75681382010-08-19 15:07:18 +0100622void MacroAssembler::AbortIfSmi(Register object) {
623 test(object, Immediate(kSmiTagMask));
624 Assert(not_equal, "Operand is a smi");
Steve Block6ded16b2010-05-10 14:33:55 +0100625}
626
627
Steve Blocka7e24c12009-10-30 11:49:00 +0000628void MacroAssembler::EnterFrame(StackFrame::Type type) {
629 push(ebp);
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000630 mov(ebp, esp);
Steve Blocka7e24c12009-10-30 11:49:00 +0000631 push(esi);
632 push(Immediate(Smi::FromInt(type)));
633 push(Immediate(CodeObject()));
Steve Block44f0eee2011-05-26 01:26:41 +0100634 if (emit_debug_code()) {
635 cmp(Operand(esp, 0), Immediate(isolate()->factory()->undefined_value()));
Steve Blocka7e24c12009-10-30 11:49:00 +0000636 Check(not_equal, "code object not properly patched");
637 }
638}
639
640
641void MacroAssembler::LeaveFrame(StackFrame::Type type) {
Steve Block44f0eee2011-05-26 01:26:41 +0100642 if (emit_debug_code()) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000643 cmp(Operand(ebp, StandardFrameConstants::kMarkerOffset),
644 Immediate(Smi::FromInt(type)));
645 Check(equal, "stack frame types must match");
646 }
647 leave();
648}
649
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100650
651void MacroAssembler::EnterExitFramePrologue() {
Ben Murdochc7cc0282012-03-05 14:35:55 +0000652 // Set up the frame structure on the stack.
Steve Blocka7e24c12009-10-30 11:49:00 +0000653 ASSERT(ExitFrameConstants::kCallerSPDisplacement == +2 * kPointerSize);
654 ASSERT(ExitFrameConstants::kCallerPCOffset == +1 * kPointerSize);
655 ASSERT(ExitFrameConstants::kCallerFPOffset == 0 * kPointerSize);
656 push(ebp);
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000657 mov(ebp, esp);
Steve Blocka7e24c12009-10-30 11:49:00 +0000658
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100659 // Reserve room for entry stack pointer and push the code object.
Steve Blocka7e24c12009-10-30 11:49:00 +0000660 ASSERT(ExitFrameConstants::kSPOffset == -1 * kPointerSize);
Andrei Popescu402d9372010-02-26 13:31:12 +0000661 push(Immediate(0)); // Saved entry sp, patched before call.
662 push(Immediate(CodeObject())); // Accessed from ExitFrame::code_slot.
Steve Blocka7e24c12009-10-30 11:49:00 +0000663
664 // Save the frame pointer and the context in top.
Ben Murdoch589d6972011-11-30 16:04:58 +0000665 ExternalReference c_entry_fp_address(Isolate::kCEntryFPAddress,
Steve Block44f0eee2011-05-26 01:26:41 +0100666 isolate());
Ben Murdoch589d6972011-11-30 16:04:58 +0000667 ExternalReference context_address(Isolate::kContextAddress,
Steve Block44f0eee2011-05-26 01:26:41 +0100668 isolate());
Steve Blocka7e24c12009-10-30 11:49:00 +0000669 mov(Operand::StaticVariable(c_entry_fp_address), ebp);
670 mov(Operand::StaticVariable(context_address), esi);
Steve Blockd0582a62009-12-15 09:54:21 +0000671}
Steve Blocka7e24c12009-10-30 11:49:00 +0000672
Steve Blocka7e24c12009-10-30 11:49:00 +0000673
Ben Murdochb0fe1622011-05-05 13:52:32 +0100674void MacroAssembler::EnterExitFrameEpilogue(int argc, bool save_doubles) {
675 // Optionally save all XMM registers.
676 if (save_doubles) {
677 CpuFeatures::Scope scope(SSE2);
678 int space = XMMRegister::kNumRegisters * kDoubleSize + argc * kPointerSize;
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000679 sub(esp, Immediate(space));
Steve Block1e0659c2011-05-24 12:43:12 +0100680 const int offset = -2 * kPointerSize;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100681 for (int i = 0; i < XMMRegister::kNumRegisters; i++) {
682 XMMRegister reg = XMMRegister::from_code(i);
683 movdbl(Operand(ebp, offset - ((i + 1) * kDoubleSize)), reg);
684 }
685 } else {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000686 sub(esp, Immediate(argc * kPointerSize));
Ben Murdochb0fe1622011-05-05 13:52:32 +0100687 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000688
689 // Get the required frame alignment for the OS.
Steve Block44f0eee2011-05-26 01:26:41 +0100690 const int kFrameAlignment = OS::ActivationFrameAlignment();
Steve Blocka7e24c12009-10-30 11:49:00 +0000691 if (kFrameAlignment > 0) {
692 ASSERT(IsPowerOf2(kFrameAlignment));
693 and_(esp, -kFrameAlignment);
694 }
695
696 // Patch the saved entry sp.
697 mov(Operand(ebp, ExitFrameConstants::kSPOffset), esp);
698}
699
700
Ben Murdochb0fe1622011-05-05 13:52:32 +0100701void MacroAssembler::EnterExitFrame(bool save_doubles) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100702 EnterExitFramePrologue();
Steve Blockd0582a62009-12-15 09:54:21 +0000703
Ben Murdochc7cc0282012-03-05 14:35:55 +0000704 // Set up argc and argv in callee-saved registers.
Steve Blockd0582a62009-12-15 09:54:21 +0000705 int offset = StandardFrameConstants::kCallerSPOffset - kPointerSize;
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000706 mov(edi, eax);
Steve Blockd0582a62009-12-15 09:54:21 +0000707 lea(esi, Operand(ebp, eax, times_4, offset));
708
Steve Block44f0eee2011-05-26 01:26:41 +0100709 // Reserve space for argc, argv and isolate.
710 EnterExitFrameEpilogue(3, save_doubles);
Steve Blockd0582a62009-12-15 09:54:21 +0000711}
712
713
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800714void MacroAssembler::EnterApiExitFrame(int argc) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100715 EnterExitFramePrologue();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100716 EnterExitFrameEpilogue(argc, false);
Steve Blockd0582a62009-12-15 09:54:21 +0000717}
718
719
Ben Murdochb0fe1622011-05-05 13:52:32 +0100720void MacroAssembler::LeaveExitFrame(bool save_doubles) {
721 // Optionally restore all XMM registers.
722 if (save_doubles) {
723 CpuFeatures::Scope scope(SSE2);
Steve Block1e0659c2011-05-24 12:43:12 +0100724 const int offset = -2 * kPointerSize;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100725 for (int i = 0; i < XMMRegister::kNumRegisters; i++) {
726 XMMRegister reg = XMMRegister::from_code(i);
727 movdbl(reg, Operand(ebp, offset - ((i + 1) * kDoubleSize)));
728 }
729 }
730
Steve Blocka7e24c12009-10-30 11:49:00 +0000731 // Get the return address from the stack and restore the frame pointer.
732 mov(ecx, Operand(ebp, 1 * kPointerSize));
733 mov(ebp, Operand(ebp, 0 * kPointerSize));
734
735 // Pop the arguments and the receiver from the caller stack.
736 lea(esp, Operand(esi, 1 * kPointerSize));
737
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800738 // Push the return address to get ready to return.
739 push(ecx);
740
741 LeaveExitFrameEpilogue();
742}
743
744void MacroAssembler::LeaveExitFrameEpilogue() {
Steve Blocka7e24c12009-10-30 11:49:00 +0000745 // Restore current context from top and clear it in debug mode.
Ben Murdoch589d6972011-11-30 16:04:58 +0000746 ExternalReference context_address(Isolate::kContextAddress, isolate());
Steve Blocka7e24c12009-10-30 11:49:00 +0000747 mov(esi, Operand::StaticVariable(context_address));
748#ifdef DEBUG
749 mov(Operand::StaticVariable(context_address), Immediate(0));
750#endif
751
Steve Blocka7e24c12009-10-30 11:49:00 +0000752 // Clear the top frame.
Ben Murdoch589d6972011-11-30 16:04:58 +0000753 ExternalReference c_entry_fp_address(Isolate::kCEntryFPAddress,
Steve Block44f0eee2011-05-26 01:26:41 +0100754 isolate());
Steve Blocka7e24c12009-10-30 11:49:00 +0000755 mov(Operand::StaticVariable(c_entry_fp_address), Immediate(0));
756}
757
758
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800759void MacroAssembler::LeaveApiExitFrame() {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000760 mov(esp, ebp);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800761 pop(ebp);
762
763 LeaveExitFrameEpilogue();
764}
765
766
Steve Blocka7e24c12009-10-30 11:49:00 +0000767void MacroAssembler::PushTryHandler(CodeLocation try_location,
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000768 HandlerType type,
769 int handler_index) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000770 // Adjust this code if not the case.
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000771 STATIC_ASSERT(StackHandlerConstants::kSize == 5 * kPointerSize);
772 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000773 STATIC_ASSERT(StackHandlerConstants::kCodeOffset == 1 * kPointerSize);
774 STATIC_ASSERT(StackHandlerConstants::kStateOffset == 2 * kPointerSize);
775 STATIC_ASSERT(StackHandlerConstants::kContextOffset == 3 * kPointerSize);
776 STATIC_ASSERT(StackHandlerConstants::kFPOffset == 4 * kPointerSize);
777
778 // We will build up the handler from the bottom by pushing on the stack.
779 // First compute the state and push the frame pointer and context.
780 unsigned state = StackHandler::OffsetField::encode(handler_index);
Steve Blocka7e24c12009-10-30 11:49:00 +0000781 if (try_location == IN_JAVASCRIPT) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000782 push(ebp);
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000783 push(esi);
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000784 state |= (type == TRY_CATCH_HANDLER)
785 ? StackHandler::KindField::encode(StackHandler::TRY_CATCH)
786 : StackHandler::KindField::encode(StackHandler::TRY_FINALLY);
Steve Blocka7e24c12009-10-30 11:49:00 +0000787 } else {
788 ASSERT(try_location == IN_JS_ENTRY);
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000789 // The frame pointer does not point to a JS frame so we save NULL for
790 // ebp. We expect the code throwing an exception to check ebp before
791 // dereferencing it to restore the context.
Steve Blocka7e24c12009-10-30 11:49:00 +0000792 push(Immediate(0)); // NULL frame pointer.
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000793 push(Immediate(Smi::FromInt(0))); // No context.
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000794 state |= StackHandler::KindField::encode(StackHandler::ENTRY);
Steve Blocka7e24c12009-10-30 11:49:00 +0000795 }
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000796
797 // Push the state and the code object.
798 push(Immediate(state));
Ben Murdochc7cc0282012-03-05 14:35:55 +0000799 Push(CodeObject());
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000800
801 // Link the current handler as the next handler.
802 ExternalReference handler_address(Isolate::kHandlerAddress, isolate());
803 push(Operand::StaticVariable(handler_address));
804 // Set this new handler as the current one.
805 mov(Operand::StaticVariable(handler_address), esp);
Steve Blocka7e24c12009-10-30 11:49:00 +0000806}
807
808
Leon Clarkee46be812010-01-19 14:06:41 +0000809void MacroAssembler::PopTryHandler() {
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000810 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000811 ExternalReference handler_address(Isolate::kHandlerAddress, isolate());
812 pop(Operand::StaticVariable(handler_address));
813 add(esp, Immediate(StackHandlerConstants::kSize - kPointerSize));
814}
815
816
817void MacroAssembler::JumpToHandlerEntry() {
818 // Compute the handler entry address and jump to it. The handler table is
819 // a fixed array of (smi-tagged) code offsets.
820 // eax = exception, edi = code object, edx = state.
821 mov(ebx, FieldOperand(edi, Code::kHandlerTableOffset));
822 shr(edx, StackHandler::kKindWidth);
823 mov(edx, FieldOperand(ebx, edx, times_4, FixedArray::kHeaderSize));
824 SmiUntag(edx);
825 lea(edi, FieldOperand(edi, edx, times_1, Code::kHeaderSize));
826 jmp(edi);
Leon Clarkee46be812010-01-19 14:06:41 +0000827}
828
829
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100830void MacroAssembler::Throw(Register value) {
831 // Adjust this code if not the case.
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000832 STATIC_ASSERT(StackHandlerConstants::kSize == 5 * kPointerSize);
833 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000834 STATIC_ASSERT(StackHandlerConstants::kCodeOffset == 1 * kPointerSize);
835 STATIC_ASSERT(StackHandlerConstants::kStateOffset == 2 * kPointerSize);
836 STATIC_ASSERT(StackHandlerConstants::kContextOffset == 3 * kPointerSize);
837 STATIC_ASSERT(StackHandlerConstants::kFPOffset == 4 * kPointerSize);
838
839 // The exception is expected in eax.
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100840 if (!value.is(eax)) {
841 mov(eax, value);
842 }
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000843 // Drop the stack pointer to the top of the top handler.
844 ExternalReference handler_address(Isolate::kHandlerAddress, isolate());
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100845 mov(esp, Operand::StaticVariable(handler_address));
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000846 // Restore the next handler.
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100847 pop(Operand::StaticVariable(handler_address));
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000848
849 // Remove the code object and state, compute the handler address in edi.
850 pop(edi); // Code object.
851 pop(edx); // Index and state.
852
853 // Restore the context and frame pointer.
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000854 pop(esi); // Context.
855 pop(ebp); // Frame pointer.
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100856
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000857 // If the handler is a JS frame, restore the context to the frame.
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000858 // (kind == ENTRY) == (ebp == 0) == (esi == 0), so we could test either
859 // ebp or esi.
Ben Murdoch257744e2011-11-30 15:57:28 +0000860 Label skip;
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000861 test(esi, esi);
862 j(zero, &skip, Label::kNear);
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000863 mov(Operand(ebp, StandardFrameConstants::kContextOffset), esi);
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100864 bind(&skip);
865
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000866 JumpToHandlerEntry();
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100867}
868
869
870void MacroAssembler::ThrowUncatchable(UncatchableExceptionType type,
871 Register value) {
872 // Adjust this code if not the case.
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000873 STATIC_ASSERT(StackHandlerConstants::kSize == 5 * kPointerSize);
874 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000875 STATIC_ASSERT(StackHandlerConstants::kCodeOffset == 1 * kPointerSize);
876 STATIC_ASSERT(StackHandlerConstants::kStateOffset == 2 * kPointerSize);
877 STATIC_ASSERT(StackHandlerConstants::kContextOffset == 3 * kPointerSize);
878 STATIC_ASSERT(StackHandlerConstants::kFPOffset == 4 * kPointerSize);
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100879
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000880 // The exception is expected in eax.
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100881 if (type == OUT_OF_MEMORY) {
882 // Set external caught exception to false.
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000883 ExternalReference external_caught(Isolate::kExternalCaughtExceptionAddress,
884 isolate());
885 mov(Operand::StaticVariable(external_caught), Immediate(false));
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100886
887 // Set pending exception and eax to out of memory exception.
Ben Murdoch589d6972011-11-30 16:04:58 +0000888 ExternalReference pending_exception(Isolate::kPendingExceptionAddress,
Steve Block44f0eee2011-05-26 01:26:41 +0100889 isolate());
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100890 mov(eax, reinterpret_cast<int32_t>(Failure::OutOfMemoryException()));
891 mov(Operand::StaticVariable(pending_exception), eax);
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000892 } else if (!value.is(eax)) {
893 mov(eax, value);
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100894 }
895
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000896 // Drop the stack pointer to the top of the top stack handler.
897 ExternalReference handler_address(Isolate::kHandlerAddress, isolate());
898 mov(esp, Operand::StaticVariable(handler_address));
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100899
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000900 // Unwind the handlers until the top ENTRY handler is found.
901 Label fetch_next, check_kind;
902 jmp(&check_kind, Label::kNear);
903 bind(&fetch_next);
904 mov(esp, Operand(esp, StackHandlerConstants::kNextOffset));
905
906 bind(&check_kind);
907 STATIC_ASSERT(StackHandler::ENTRY == 0);
908 test(Operand(esp, StackHandlerConstants::kStateOffset),
909 Immediate(StackHandler::KindField::kMask));
910 j(not_zero, &fetch_next);
911
912 // Set the top handler address to next handler past the top ENTRY handler.
913 pop(Operand::StaticVariable(handler_address));
914
915 // Remove the code object and state, compute the handler address in edi.
916 pop(edi); // Code object.
917 pop(edx); // Index and state.
918
919 // Clear the context pointer and frame pointer (0 was saved in the handler).
920 pop(esi);
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100921 pop(ebp);
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100922
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000923 JumpToHandlerEntry();
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100924}
925
926
Steve Blocka7e24c12009-10-30 11:49:00 +0000927void MacroAssembler::CheckAccessGlobalProxy(Register holder_reg,
928 Register scratch,
929 Label* miss) {
930 Label same_contexts;
931
932 ASSERT(!holder_reg.is(scratch));
933
934 // Load current lexical context from the stack frame.
935 mov(scratch, Operand(ebp, StandardFrameConstants::kContextOffset));
936
937 // When generating debug code, make sure the lexical context is set.
Steve Block44f0eee2011-05-26 01:26:41 +0100938 if (emit_debug_code()) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000939 cmp(scratch, Immediate(0));
Steve Blocka7e24c12009-10-30 11:49:00 +0000940 Check(not_equal, "we should not have an empty lexical context");
941 }
942 // Load the global context of the current context.
943 int offset = Context::kHeaderSize + Context::GLOBAL_INDEX * kPointerSize;
944 mov(scratch, FieldOperand(scratch, offset));
945 mov(scratch, FieldOperand(scratch, GlobalObject::kGlobalContextOffset));
946
947 // Check the context is a global context.
Steve Block44f0eee2011-05-26 01:26:41 +0100948 if (emit_debug_code()) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000949 push(scratch);
950 // Read the first word and compare to global_context_map.
951 mov(scratch, FieldOperand(scratch, HeapObject::kMapOffset));
Steve Block44f0eee2011-05-26 01:26:41 +0100952 cmp(scratch, isolate()->factory()->global_context_map());
Steve Blocka7e24c12009-10-30 11:49:00 +0000953 Check(equal, "JSGlobalObject::global_context should be a global context.");
954 pop(scratch);
955 }
956
957 // Check if both contexts are the same.
958 cmp(scratch, FieldOperand(holder_reg, JSGlobalProxy::kContextOffset));
Ben Murdoch257744e2011-11-30 15:57:28 +0000959 j(equal, &same_contexts);
Steve Blocka7e24c12009-10-30 11:49:00 +0000960
961 // Compare security tokens, save holder_reg on the stack so we can use it
962 // as a temporary register.
963 //
964 // TODO(119): avoid push(holder_reg)/pop(holder_reg)
965 push(holder_reg);
966 // Check that the security token in the calling global object is
967 // compatible with the security token in the receiving global
968 // object.
969 mov(holder_reg, FieldOperand(holder_reg, JSGlobalProxy::kContextOffset));
970
971 // Check the context is a global context.
Steve Block44f0eee2011-05-26 01:26:41 +0100972 if (emit_debug_code()) {
973 cmp(holder_reg, isolate()->factory()->null_value());
Steve Blocka7e24c12009-10-30 11:49:00 +0000974 Check(not_equal, "JSGlobalProxy::context() should not be null.");
975
976 push(holder_reg);
977 // Read the first word and compare to global_context_map(),
978 mov(holder_reg, FieldOperand(holder_reg, HeapObject::kMapOffset));
Steve Block44f0eee2011-05-26 01:26:41 +0100979 cmp(holder_reg, isolate()->factory()->global_context_map());
Steve Blocka7e24c12009-10-30 11:49:00 +0000980 Check(equal, "JSGlobalObject::global_context should be a global context.");
981 pop(holder_reg);
982 }
983
984 int token_offset = Context::kHeaderSize +
985 Context::SECURITY_TOKEN_INDEX * kPointerSize;
986 mov(scratch, FieldOperand(scratch, token_offset));
987 cmp(scratch, FieldOperand(holder_reg, token_offset));
988 pop(holder_reg);
Ben Murdoch257744e2011-11-30 15:57:28 +0000989 j(not_equal, miss);
Steve Blocka7e24c12009-10-30 11:49:00 +0000990
991 bind(&same_contexts);
992}
993
994
Ben Murdochc7cc0282012-03-05 14:35:55 +0000995// Compute the hash code from the untagged key. This must be kept in sync
996// with ComputeIntegerHash in utils.h.
997//
998// Note: r0 will contain hash code
999void MacroAssembler::GetNumberHash(Register r0, Register scratch) {
1000 // Xor original key with a seed.
1001 if (Serializer::enabled()) {
1002 ExternalReference roots_array_start =
1003 ExternalReference::roots_array_start(isolate());
1004 mov(scratch, Immediate(Heap::kHashSeedRootIndex));
1005 mov(scratch,
1006 Operand::StaticArray(scratch, times_pointer_size, roots_array_start));
1007 SmiUntag(scratch);
1008 xor_(r0, scratch);
1009 } else {
1010 int32_t seed = isolate()->heap()->HashSeed();
1011 xor_(r0, Immediate(seed));
1012 }
1013
1014 // hash = ~hash + (hash << 15);
1015 mov(scratch, r0);
1016 not_(r0);
1017 shl(scratch, 15);
1018 add(r0, scratch);
1019 // hash = hash ^ (hash >> 12);
1020 mov(scratch, r0);
1021 shr(scratch, 12);
1022 xor_(r0, scratch);
1023 // hash = hash + (hash << 2);
1024 lea(r0, Operand(r0, r0, times_4, 0));
1025 // hash = hash ^ (hash >> 4);
1026 mov(scratch, r0);
1027 shr(scratch, 4);
1028 xor_(r0, scratch);
1029 // hash = hash * 2057;
1030 imul(r0, r0, 2057);
1031 // hash = hash ^ (hash >> 16);
1032 mov(scratch, r0);
1033 shr(scratch, 16);
1034 xor_(r0, scratch);
1035}
1036
1037
1038
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001039void MacroAssembler::LoadFromNumberDictionary(Label* miss,
1040 Register elements,
1041 Register key,
1042 Register r0,
1043 Register r1,
1044 Register r2,
1045 Register result) {
1046 // Register use:
1047 //
1048 // elements - holds the slow-case elements of the receiver and is unchanged.
1049 //
1050 // key - holds the smi key on entry and is unchanged.
1051 //
1052 // Scratch registers:
1053 //
1054 // r0 - holds the untagged key on entry and holds the hash once computed.
1055 //
1056 // r1 - used to hold the capacity mask of the dictionary
1057 //
1058 // r2 - used for the index into the dictionary.
1059 //
1060 // result - holds the result on exit if the load succeeds and we fall through.
1061
1062 Label done;
1063
Ben Murdochc7cc0282012-03-05 14:35:55 +00001064 GetNumberHash(r0, r1);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001065
1066 // Compute capacity mask.
Ben Murdochc7cc0282012-03-05 14:35:55 +00001067 mov(r1, FieldOperand(elements, SeededNumberDictionary::kCapacityOffset));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001068 shr(r1, kSmiTagSize); // convert smi to int
1069 dec(r1);
1070
1071 // Generate an unrolled loop that performs a few probes before giving up.
1072 const int kProbes = 4;
1073 for (int i = 0; i < kProbes; i++) {
1074 // Use r2 for index calculations and keep the hash intact in r0.
1075 mov(r2, r0);
1076 // Compute the masked index: (hash + i + i * i) & mask.
1077 if (i > 0) {
Ben Murdochc7cc0282012-03-05 14:35:55 +00001078 add(r2, Immediate(SeededNumberDictionary::GetProbeOffset(i)));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001079 }
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001080 and_(r2, r1);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001081
1082 // Scale the index by multiplying by the entry size.
Ben Murdochc7cc0282012-03-05 14:35:55 +00001083 ASSERT(SeededNumberDictionary::kEntrySize == 3);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001084 lea(r2, Operand(r2, r2, times_2, 0)); // r2 = r2 * 3
1085
1086 // Check if the key matches.
1087 cmp(key, FieldOperand(elements,
1088 r2,
1089 times_pointer_size,
Ben Murdochc7cc0282012-03-05 14:35:55 +00001090 SeededNumberDictionary::kElementsStartOffset));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001091 if (i != (kProbes - 1)) {
1092 j(equal, &done);
1093 } else {
1094 j(not_equal, miss);
1095 }
1096 }
1097
1098 bind(&done);
1099 // Check that the value is a normal propety.
1100 const int kDetailsOffset =
Ben Murdochc7cc0282012-03-05 14:35:55 +00001101 SeededNumberDictionary::kElementsStartOffset + 2 * kPointerSize;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001102 ASSERT_EQ(NORMAL, 0);
1103 test(FieldOperand(elements, r2, times_pointer_size, kDetailsOffset),
Ben Murdoch589d6972011-11-30 16:04:58 +00001104 Immediate(PropertyDetails::TypeField::kMask << kSmiTagSize));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001105 j(not_zero, miss);
1106
1107 // Get the value at the masked, scaled index.
1108 const int kValueOffset =
Ben Murdochc7cc0282012-03-05 14:35:55 +00001109 SeededNumberDictionary::kElementsStartOffset + kPointerSize;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001110 mov(result, FieldOperand(elements, r2, times_pointer_size, kValueOffset));
1111}
1112
1113
Steve Blocka7e24c12009-10-30 11:49:00 +00001114void MacroAssembler::LoadAllocationTopHelper(Register result,
Steve Blocka7e24c12009-10-30 11:49:00 +00001115 Register scratch,
1116 AllocationFlags flags) {
1117 ExternalReference new_space_allocation_top =
Steve Block44f0eee2011-05-26 01:26:41 +01001118 ExternalReference::new_space_allocation_top_address(isolate());
Steve Blocka7e24c12009-10-30 11:49:00 +00001119
1120 // Just return if allocation top is already known.
1121 if ((flags & RESULT_CONTAINS_TOP) != 0) {
1122 // No use of scratch if allocation top is provided.
1123 ASSERT(scratch.is(no_reg));
1124#ifdef DEBUG
1125 // Assert that result actually contains top on entry.
1126 cmp(result, Operand::StaticVariable(new_space_allocation_top));
1127 Check(equal, "Unexpected allocation top");
1128#endif
1129 return;
1130 }
1131
1132 // Move address of new object to result. Use scratch register if available.
1133 if (scratch.is(no_reg)) {
1134 mov(result, Operand::StaticVariable(new_space_allocation_top));
1135 } else {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001136 mov(scratch, Immediate(new_space_allocation_top));
Steve Blocka7e24c12009-10-30 11:49:00 +00001137 mov(result, Operand(scratch, 0));
1138 }
1139}
1140
1141
1142void MacroAssembler::UpdateAllocationTopHelper(Register result_end,
1143 Register scratch) {
Steve Block44f0eee2011-05-26 01:26:41 +01001144 if (emit_debug_code()) {
Steve Blockd0582a62009-12-15 09:54:21 +00001145 test(result_end, Immediate(kObjectAlignmentMask));
1146 Check(zero, "Unaligned allocation in new space");
1147 }
1148
Steve Blocka7e24c12009-10-30 11:49:00 +00001149 ExternalReference new_space_allocation_top =
Steve Block44f0eee2011-05-26 01:26:41 +01001150 ExternalReference::new_space_allocation_top_address(isolate());
Steve Blocka7e24c12009-10-30 11:49:00 +00001151
1152 // Update new top. Use scratch if available.
1153 if (scratch.is(no_reg)) {
1154 mov(Operand::StaticVariable(new_space_allocation_top), result_end);
1155 } else {
1156 mov(Operand(scratch, 0), result_end);
1157 }
1158}
1159
1160
1161void MacroAssembler::AllocateInNewSpace(int object_size,
1162 Register result,
1163 Register result_end,
1164 Register scratch,
1165 Label* gc_required,
1166 AllocationFlags flags) {
John Reck59135872010-11-02 12:39:01 -07001167 if (!FLAG_inline_new) {
Steve Block44f0eee2011-05-26 01:26:41 +01001168 if (emit_debug_code()) {
John Reck59135872010-11-02 12:39:01 -07001169 // Trash the registers to simulate an allocation failure.
1170 mov(result, Immediate(0x7091));
1171 if (result_end.is_valid()) {
1172 mov(result_end, Immediate(0x7191));
1173 }
1174 if (scratch.is_valid()) {
1175 mov(scratch, Immediate(0x7291));
1176 }
1177 }
1178 jmp(gc_required);
1179 return;
1180 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001181 ASSERT(!result.is(result_end));
1182
1183 // Load address of new object into result.
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001184 LoadAllocationTopHelper(result, scratch, flags);
Steve Blocka7e24c12009-10-30 11:49:00 +00001185
Ben Murdochbb769b22010-08-11 14:56:33 +01001186 Register top_reg = result_end.is_valid() ? result_end : result;
1187
Steve Blocka7e24c12009-10-30 11:49:00 +00001188 // Calculate new top and bail out if new space is exhausted.
1189 ExternalReference new_space_allocation_limit =
Steve Block44f0eee2011-05-26 01:26:41 +01001190 ExternalReference::new_space_allocation_limit_address(isolate());
Ben Murdochbb769b22010-08-11 14:56:33 +01001191
Steve Block1e0659c2011-05-24 12:43:12 +01001192 if (!top_reg.is(result)) {
1193 mov(top_reg, result);
Ben Murdochbb769b22010-08-11 14:56:33 +01001194 }
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001195 add(top_reg, Immediate(object_size));
Ben Murdoch257744e2011-11-30 15:57:28 +00001196 j(carry, gc_required);
Ben Murdochbb769b22010-08-11 14:56:33 +01001197 cmp(top_reg, Operand::StaticVariable(new_space_allocation_limit));
Ben Murdoch257744e2011-11-30 15:57:28 +00001198 j(above, gc_required);
Steve Blocka7e24c12009-10-30 11:49:00 +00001199
Leon Clarkee46be812010-01-19 14:06:41 +00001200 // Update allocation top.
Ben Murdochbb769b22010-08-11 14:56:33 +01001201 UpdateAllocationTopHelper(top_reg, scratch);
1202
1203 // Tag result if requested.
1204 if (top_reg.is(result)) {
1205 if ((flags & TAG_OBJECT) != 0) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001206 sub(result, Immediate(object_size - kHeapObjectTag));
Ben Murdochbb769b22010-08-11 14:56:33 +01001207 } else {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001208 sub(result, Immediate(object_size));
Ben Murdochbb769b22010-08-11 14:56:33 +01001209 }
1210 } else if ((flags & TAG_OBJECT) != 0) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001211 add(result, Immediate(kHeapObjectTag));
Ben Murdochbb769b22010-08-11 14:56:33 +01001212 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001213}
1214
1215
1216void MacroAssembler::AllocateInNewSpace(int header_size,
1217 ScaleFactor element_size,
1218 Register element_count,
1219 Register result,
1220 Register result_end,
1221 Register scratch,
1222 Label* gc_required,
1223 AllocationFlags flags) {
John Reck59135872010-11-02 12:39:01 -07001224 if (!FLAG_inline_new) {
Steve Block44f0eee2011-05-26 01:26:41 +01001225 if (emit_debug_code()) {
John Reck59135872010-11-02 12:39:01 -07001226 // Trash the registers to simulate an allocation failure.
1227 mov(result, Immediate(0x7091));
1228 mov(result_end, Immediate(0x7191));
1229 if (scratch.is_valid()) {
1230 mov(scratch, Immediate(0x7291));
1231 }
1232 // Register element_count is not modified by the function.
1233 }
1234 jmp(gc_required);
1235 return;
1236 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001237 ASSERT(!result.is(result_end));
1238
1239 // Load address of new object into result.
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001240 LoadAllocationTopHelper(result, scratch, flags);
Steve Blocka7e24c12009-10-30 11:49:00 +00001241
1242 // Calculate new top and bail out if new space is exhausted.
1243 ExternalReference new_space_allocation_limit =
Steve Block44f0eee2011-05-26 01:26:41 +01001244 ExternalReference::new_space_allocation_limit_address(isolate());
Steve Block1e0659c2011-05-24 12:43:12 +01001245
1246 // We assume that element_count*element_size + header_size does not
1247 // overflow.
1248 lea(result_end, Operand(element_count, element_size, header_size));
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001249 add(result_end, result);
Steve Block1e0659c2011-05-24 12:43:12 +01001250 j(carry, gc_required);
Steve Blocka7e24c12009-10-30 11:49:00 +00001251 cmp(result_end, Operand::StaticVariable(new_space_allocation_limit));
1252 j(above, gc_required);
1253
Steve Blocka7e24c12009-10-30 11:49:00 +00001254 // Tag result if requested.
1255 if ((flags & TAG_OBJECT) != 0) {
Leon Clarkee46be812010-01-19 14:06:41 +00001256 lea(result, Operand(result, kHeapObjectTag));
Steve Blocka7e24c12009-10-30 11:49:00 +00001257 }
Leon Clarkee46be812010-01-19 14:06:41 +00001258
1259 // Update allocation top.
1260 UpdateAllocationTopHelper(result_end, scratch);
Steve Blocka7e24c12009-10-30 11:49:00 +00001261}
1262
1263
1264void MacroAssembler::AllocateInNewSpace(Register object_size,
1265 Register result,
1266 Register result_end,
1267 Register scratch,
1268 Label* gc_required,
1269 AllocationFlags flags) {
John Reck59135872010-11-02 12:39:01 -07001270 if (!FLAG_inline_new) {
Steve Block44f0eee2011-05-26 01:26:41 +01001271 if (emit_debug_code()) {
John Reck59135872010-11-02 12:39:01 -07001272 // Trash the registers to simulate an allocation failure.
1273 mov(result, Immediate(0x7091));
1274 mov(result_end, Immediate(0x7191));
1275 if (scratch.is_valid()) {
1276 mov(scratch, Immediate(0x7291));
1277 }
1278 // object_size is left unchanged by this function.
1279 }
1280 jmp(gc_required);
1281 return;
1282 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001283 ASSERT(!result.is(result_end));
1284
1285 // Load address of new object into result.
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001286 LoadAllocationTopHelper(result, scratch, flags);
Steve Blocka7e24c12009-10-30 11:49:00 +00001287
1288 // Calculate new top and bail out if new space is exhausted.
1289 ExternalReference new_space_allocation_limit =
Steve Block44f0eee2011-05-26 01:26:41 +01001290 ExternalReference::new_space_allocation_limit_address(isolate());
Steve Blocka7e24c12009-10-30 11:49:00 +00001291 if (!object_size.is(result_end)) {
1292 mov(result_end, object_size);
1293 }
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001294 add(result_end, result);
Ben Murdoch257744e2011-11-30 15:57:28 +00001295 j(carry, gc_required);
Steve Blocka7e24c12009-10-30 11:49:00 +00001296 cmp(result_end, Operand::StaticVariable(new_space_allocation_limit));
Ben Murdoch257744e2011-11-30 15:57:28 +00001297 j(above, gc_required);
Steve Blocka7e24c12009-10-30 11:49:00 +00001298
Steve Blocka7e24c12009-10-30 11:49:00 +00001299 // Tag result if requested.
1300 if ((flags & TAG_OBJECT) != 0) {
Leon Clarkee46be812010-01-19 14:06:41 +00001301 lea(result, Operand(result, kHeapObjectTag));
Steve Blocka7e24c12009-10-30 11:49:00 +00001302 }
Leon Clarkee46be812010-01-19 14:06:41 +00001303
1304 // Update allocation top.
1305 UpdateAllocationTopHelper(result_end, scratch);
Steve Blocka7e24c12009-10-30 11:49:00 +00001306}
1307
1308
1309void MacroAssembler::UndoAllocationInNewSpace(Register object) {
1310 ExternalReference new_space_allocation_top =
Steve Block44f0eee2011-05-26 01:26:41 +01001311 ExternalReference::new_space_allocation_top_address(isolate());
Steve Blocka7e24c12009-10-30 11:49:00 +00001312
1313 // Make sure the object has no tag before resetting top.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001314 and_(object, Immediate(~kHeapObjectTagMask));
Steve Blocka7e24c12009-10-30 11:49:00 +00001315#ifdef DEBUG
1316 cmp(object, Operand::StaticVariable(new_space_allocation_top));
1317 Check(below, "Undo allocation of non allocated memory");
1318#endif
1319 mov(Operand::StaticVariable(new_space_allocation_top), object);
1320}
1321
1322
Steve Block3ce2e202009-11-05 08:53:23 +00001323void MacroAssembler::AllocateHeapNumber(Register result,
1324 Register scratch1,
1325 Register scratch2,
1326 Label* gc_required) {
1327 // Allocate heap number in new space.
1328 AllocateInNewSpace(HeapNumber::kSize,
1329 result,
1330 scratch1,
1331 scratch2,
1332 gc_required,
1333 TAG_OBJECT);
1334
1335 // Set the map.
1336 mov(FieldOperand(result, HeapObject::kMapOffset),
Steve Block44f0eee2011-05-26 01:26:41 +01001337 Immediate(isolate()->factory()->heap_number_map()));
Steve Block3ce2e202009-11-05 08:53:23 +00001338}
1339
1340
Steve Blockd0582a62009-12-15 09:54:21 +00001341void MacroAssembler::AllocateTwoByteString(Register result,
1342 Register length,
1343 Register scratch1,
1344 Register scratch2,
1345 Register scratch3,
1346 Label* gc_required) {
1347 // Calculate the number of bytes needed for the characters in the string while
1348 // observing object alignment.
1349 ASSERT((SeqTwoByteString::kHeaderSize & kObjectAlignmentMask) == 0);
Steve Blockd0582a62009-12-15 09:54:21 +00001350 ASSERT(kShortSize == 2);
Leon Clarkee46be812010-01-19 14:06:41 +00001351 // scratch1 = length * 2 + kObjectAlignmentMask.
1352 lea(scratch1, Operand(length, length, times_1, kObjectAlignmentMask));
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001353 and_(scratch1, Immediate(~kObjectAlignmentMask));
Steve Blockd0582a62009-12-15 09:54:21 +00001354
1355 // Allocate two byte string in new space.
1356 AllocateInNewSpace(SeqTwoByteString::kHeaderSize,
1357 times_1,
1358 scratch1,
1359 result,
1360 scratch2,
1361 scratch3,
1362 gc_required,
1363 TAG_OBJECT);
1364
1365 // Set the map, length and hash field.
1366 mov(FieldOperand(result, HeapObject::kMapOffset),
Steve Block44f0eee2011-05-26 01:26:41 +01001367 Immediate(isolate()->factory()->string_map()));
Steve Block6ded16b2010-05-10 14:33:55 +01001368 mov(scratch1, length);
1369 SmiTag(scratch1);
1370 mov(FieldOperand(result, String::kLengthOffset), scratch1);
Steve Blockd0582a62009-12-15 09:54:21 +00001371 mov(FieldOperand(result, String::kHashFieldOffset),
1372 Immediate(String::kEmptyHashField));
1373}
1374
1375
1376void MacroAssembler::AllocateAsciiString(Register result,
1377 Register length,
1378 Register scratch1,
1379 Register scratch2,
1380 Register scratch3,
1381 Label* gc_required) {
1382 // Calculate the number of bytes needed for the characters in the string while
1383 // observing object alignment.
1384 ASSERT((SeqAsciiString::kHeaderSize & kObjectAlignmentMask) == 0);
1385 mov(scratch1, length);
1386 ASSERT(kCharSize == 1);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001387 add(scratch1, Immediate(kObjectAlignmentMask));
1388 and_(scratch1, Immediate(~kObjectAlignmentMask));
Steve Blockd0582a62009-12-15 09:54:21 +00001389
Ben Murdochc7cc0282012-03-05 14:35:55 +00001390 // Allocate ASCII string in new space.
Steve Blockd0582a62009-12-15 09:54:21 +00001391 AllocateInNewSpace(SeqAsciiString::kHeaderSize,
1392 times_1,
1393 scratch1,
1394 result,
1395 scratch2,
1396 scratch3,
1397 gc_required,
1398 TAG_OBJECT);
1399
1400 // Set the map, length and hash field.
1401 mov(FieldOperand(result, HeapObject::kMapOffset),
Steve Block44f0eee2011-05-26 01:26:41 +01001402 Immediate(isolate()->factory()->ascii_string_map()));
Steve Block6ded16b2010-05-10 14:33:55 +01001403 mov(scratch1, length);
1404 SmiTag(scratch1);
1405 mov(FieldOperand(result, String::kLengthOffset), scratch1);
Steve Blockd0582a62009-12-15 09:54:21 +00001406 mov(FieldOperand(result, String::kHashFieldOffset),
1407 Immediate(String::kEmptyHashField));
1408}
1409
1410
Iain Merrick9ac36c92010-09-13 15:29:50 +01001411void MacroAssembler::AllocateAsciiString(Register result,
1412 int length,
1413 Register scratch1,
1414 Register scratch2,
1415 Label* gc_required) {
1416 ASSERT(length > 0);
1417
Ben Murdochc7cc0282012-03-05 14:35:55 +00001418 // Allocate ASCII string in new space.
Iain Merrick9ac36c92010-09-13 15:29:50 +01001419 AllocateInNewSpace(SeqAsciiString::SizeFor(length),
1420 result,
1421 scratch1,
1422 scratch2,
1423 gc_required,
1424 TAG_OBJECT);
1425
1426 // Set the map, length and hash field.
1427 mov(FieldOperand(result, HeapObject::kMapOffset),
Steve Block44f0eee2011-05-26 01:26:41 +01001428 Immediate(isolate()->factory()->ascii_string_map()));
Iain Merrick9ac36c92010-09-13 15:29:50 +01001429 mov(FieldOperand(result, String::kLengthOffset),
1430 Immediate(Smi::FromInt(length)));
1431 mov(FieldOperand(result, String::kHashFieldOffset),
1432 Immediate(String::kEmptyHashField));
1433}
1434
1435
Ben Murdoch589d6972011-11-30 16:04:58 +00001436void MacroAssembler::AllocateTwoByteConsString(Register result,
Steve Blockd0582a62009-12-15 09:54:21 +00001437 Register scratch1,
1438 Register scratch2,
1439 Label* gc_required) {
1440 // Allocate heap number in new space.
1441 AllocateInNewSpace(ConsString::kSize,
1442 result,
1443 scratch1,
1444 scratch2,
1445 gc_required,
1446 TAG_OBJECT);
1447
1448 // Set the map. The other fields are left uninitialized.
1449 mov(FieldOperand(result, HeapObject::kMapOffset),
Steve Block44f0eee2011-05-26 01:26:41 +01001450 Immediate(isolate()->factory()->cons_string_map()));
Steve Blockd0582a62009-12-15 09:54:21 +00001451}
1452
1453
1454void MacroAssembler::AllocateAsciiConsString(Register result,
1455 Register scratch1,
1456 Register scratch2,
1457 Label* gc_required) {
1458 // Allocate heap number in new space.
1459 AllocateInNewSpace(ConsString::kSize,
1460 result,
1461 scratch1,
1462 scratch2,
1463 gc_required,
1464 TAG_OBJECT);
1465
1466 // Set the map. The other fields are left uninitialized.
1467 mov(FieldOperand(result, HeapObject::kMapOffset),
Steve Block44f0eee2011-05-26 01:26:41 +01001468 Immediate(isolate()->factory()->cons_ascii_string_map()));
Steve Blockd0582a62009-12-15 09:54:21 +00001469}
1470
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001471
Ben Murdoch589d6972011-11-30 16:04:58 +00001472void MacroAssembler::AllocateTwoByteSlicedString(Register result,
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001473 Register scratch1,
1474 Register scratch2,
1475 Label* gc_required) {
1476 // Allocate heap number in new space.
1477 AllocateInNewSpace(SlicedString::kSize,
1478 result,
1479 scratch1,
1480 scratch2,
1481 gc_required,
1482 TAG_OBJECT);
1483
1484 // Set the map. The other fields are left uninitialized.
1485 mov(FieldOperand(result, HeapObject::kMapOffset),
1486 Immediate(isolate()->factory()->sliced_string_map()));
1487}
1488
1489
1490void MacroAssembler::AllocateAsciiSlicedString(Register result,
1491 Register scratch1,
1492 Register scratch2,
1493 Label* gc_required) {
1494 // Allocate heap number in new space.
1495 AllocateInNewSpace(SlicedString::kSize,
1496 result,
1497 scratch1,
1498 scratch2,
1499 gc_required,
1500 TAG_OBJECT);
1501
1502 // Set the map. The other fields are left uninitialized.
1503 mov(FieldOperand(result, HeapObject::kMapOffset),
1504 Immediate(isolate()->factory()->sliced_ascii_string_map()));
1505}
1506
1507
Ben Murdochb8e0da22011-05-16 14:20:40 +01001508// Copy memory, byte-by-byte, from source to destination. Not optimized for
1509// long or aligned copies. The contents of scratch and length are destroyed.
1510// Source and destination are incremented by length.
1511// Many variants of movsb, loop unrolling, word moves, and indexed operands
1512// have been tried here already, and this is fastest.
1513// A simpler loop is faster on small copies, but 30% slower on large ones.
1514// The cld() instruction must have been emitted, to set the direction flag(),
1515// before calling this function.
1516void MacroAssembler::CopyBytes(Register source,
1517 Register destination,
1518 Register length,
1519 Register scratch) {
1520 Label loop, done, short_string, short_loop;
1521 // Experimentation shows that the short string loop is faster if length < 10.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001522 cmp(length, Immediate(10));
Ben Murdochb8e0da22011-05-16 14:20:40 +01001523 j(less_equal, &short_string);
1524
1525 ASSERT(source.is(esi));
1526 ASSERT(destination.is(edi));
1527 ASSERT(length.is(ecx));
1528
1529 // Because source is 4-byte aligned in our uses of this function,
1530 // we keep source aligned for the rep_movs call by copying the odd bytes
1531 // at the end of the ranges.
1532 mov(scratch, Operand(source, length, times_1, -4));
1533 mov(Operand(destination, length, times_1, -4), scratch);
1534 mov(scratch, ecx);
1535 shr(ecx, 2);
1536 rep_movs();
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001537 and_(scratch, Immediate(0x3));
1538 add(destination, scratch);
Ben Murdochb8e0da22011-05-16 14:20:40 +01001539 jmp(&done);
1540
1541 bind(&short_string);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001542 test(length, length);
Ben Murdochb8e0da22011-05-16 14:20:40 +01001543 j(zero, &done);
1544
1545 bind(&short_loop);
1546 mov_b(scratch, Operand(source, 0));
1547 mov_b(Operand(destination, 0), scratch);
1548 inc(source);
1549 inc(destination);
1550 dec(length);
1551 j(not_zero, &short_loop);
1552
1553 bind(&done);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001554}
1555
Steve Blockd0582a62009-12-15 09:54:21 +00001556
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001557void MacroAssembler::InitializeFieldsWithFiller(Register start_offset,
1558 Register end_offset,
1559 Register filler) {
1560 Label loop, entry;
1561 jmp(&entry);
1562 bind(&loop);
1563 mov(Operand(start_offset, 0), filler);
1564 add(start_offset, Immediate(kPointerSize));
1565 bind(&entry);
1566 cmp(start_offset, end_offset);
1567 j(less, &loop);
1568}
1569
1570
1571void MacroAssembler::BooleanBitTest(Register object,
1572 int field_offset,
1573 int bit_index) {
1574 bit_index += kSmiTagSize + kSmiShiftSize;
1575 ASSERT(IsPowerOf2(kBitsPerByte));
1576 int byte_index = bit_index / kBitsPerByte;
1577 int byte_bit_index = bit_index & (kBitsPerByte - 1);
1578 test_b(FieldOperand(object, field_offset + byte_index),
1579 static_cast<byte>(1 << byte_bit_index));
1580}
1581
1582
1583
Steve Blocka7e24c12009-10-30 11:49:00 +00001584void MacroAssembler::NegativeZeroTest(Register result,
1585 Register op,
1586 Label* then_label) {
1587 Label ok;
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001588 test(result, result);
Ben Murdoch257744e2011-11-30 15:57:28 +00001589 j(not_zero, &ok);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001590 test(op, op);
Ben Murdoch257744e2011-11-30 15:57:28 +00001591 j(sign, then_label);
Steve Blocka7e24c12009-10-30 11:49:00 +00001592 bind(&ok);
1593}
1594
1595
1596void MacroAssembler::NegativeZeroTest(Register result,
1597 Register op1,
1598 Register op2,
1599 Register scratch,
1600 Label* then_label) {
1601 Label ok;
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001602 test(result, result);
Ben Murdoch257744e2011-11-30 15:57:28 +00001603 j(not_zero, &ok);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001604 mov(scratch, op1);
1605 or_(scratch, op2);
Ben Murdoch257744e2011-11-30 15:57:28 +00001606 j(sign, then_label);
Steve Blocka7e24c12009-10-30 11:49:00 +00001607 bind(&ok);
1608}
1609
1610
1611void MacroAssembler::TryGetFunctionPrototype(Register function,
1612 Register result,
1613 Register scratch,
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001614 Label* miss,
1615 bool miss_on_bound_function) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001616 // Check that the receiver isn't a smi.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001617 JumpIfSmi(function, miss);
Steve Blocka7e24c12009-10-30 11:49:00 +00001618
1619 // Check that the function really is a function.
1620 CmpObjectType(function, JS_FUNCTION_TYPE, result);
Ben Murdoch257744e2011-11-30 15:57:28 +00001621 j(not_equal, miss);
Steve Blocka7e24c12009-10-30 11:49:00 +00001622
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001623 if (miss_on_bound_function) {
1624 // If a bound function, go to miss label.
1625 mov(scratch,
1626 FieldOperand(function, JSFunction::kSharedFunctionInfoOffset));
1627 BooleanBitTest(scratch, SharedFunctionInfo::kCompilerHintsOffset,
1628 SharedFunctionInfo::kBoundFunction);
1629 j(not_zero, miss);
1630 }
1631
Steve Blocka7e24c12009-10-30 11:49:00 +00001632 // Make sure that the function has an instance prototype.
1633 Label non_instance;
1634 movzx_b(scratch, FieldOperand(result, Map::kBitFieldOffset));
1635 test(scratch, Immediate(1 << Map::kHasNonInstancePrototype));
Ben Murdoch257744e2011-11-30 15:57:28 +00001636 j(not_zero, &non_instance);
Steve Blocka7e24c12009-10-30 11:49:00 +00001637
1638 // Get the prototype or initial map from the function.
1639 mov(result,
1640 FieldOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
1641
1642 // If the prototype or initial map is the hole, don't return it and
1643 // simply miss the cache instead. This will allow us to allocate a
1644 // prototype object on-demand in the runtime system.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001645 cmp(result, Immediate(isolate()->factory()->the_hole_value()));
Ben Murdoch257744e2011-11-30 15:57:28 +00001646 j(equal, miss);
Steve Blocka7e24c12009-10-30 11:49:00 +00001647
1648 // If the function does not have an initial map, we're done.
1649 Label done;
1650 CmpObjectType(result, MAP_TYPE, scratch);
1651 j(not_equal, &done);
1652
1653 // Get the prototype from the initial map.
1654 mov(result, FieldOperand(result, Map::kPrototypeOffset));
1655 jmp(&done);
1656
1657 // Non-instance prototype: Fetch prototype from constructor field
1658 // in initial map.
1659 bind(&non_instance);
1660 mov(result, FieldOperand(result, Map::kConstructorOffset));
1661
1662 // All done.
1663 bind(&done);
1664}
1665
1666
Ben Murdoch257744e2011-11-30 15:57:28 +00001667void MacroAssembler::CallStub(CodeStub* stub, unsigned ast_id) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001668 ASSERT(AllowThisStubCall(stub)); // Calls are not allowed in some stubs.
Ben Murdoch257744e2011-11-30 15:57:28 +00001669 call(stub->GetCode(), RelocInfo::CODE_TARGET, ast_id);
Steve Blocka7e24c12009-10-30 11:49:00 +00001670}
1671
1672
Steve Blockd0582a62009-12-15 09:54:21 +00001673void MacroAssembler::TailCallStub(CodeStub* stub) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001674 ASSERT(allow_stub_calls_ || stub->CompilingCallsToThisStubIsGCSafe());
Steve Blockd0582a62009-12-15 09:54:21 +00001675 jmp(stub->GetCode(), RelocInfo::CODE_TARGET);
1676}
1677
1678
Steve Blocka7e24c12009-10-30 11:49:00 +00001679void MacroAssembler::StubReturn(int argc) {
1680 ASSERT(argc >= 1 && generating_stub());
1681 ret((argc - 1) * kPointerSize);
1682}
1683
1684
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001685bool MacroAssembler::AllowThisStubCall(CodeStub* stub) {
1686 if (!has_frame_ && stub->SometimesSetsUpAFrame()) return false;
1687 return allow_stub_calls_ || stub->CompilingCallsToThisStubIsGCSafe();
1688}
1689
1690
Steve Blocka7e24c12009-10-30 11:49:00 +00001691void MacroAssembler::IllegalOperation(int num_arguments) {
1692 if (num_arguments > 0) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001693 add(esp, Immediate(num_arguments * kPointerSize));
Steve Blocka7e24c12009-10-30 11:49:00 +00001694 }
Steve Block44f0eee2011-05-26 01:26:41 +01001695 mov(eax, Immediate(isolate()->factory()->undefined_value()));
Steve Blocka7e24c12009-10-30 11:49:00 +00001696}
1697
1698
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001699void MacroAssembler::IndexFromHash(Register hash, Register index) {
1700 // The assert checks that the constants for the maximum number of digits
1701 // for an array index cached in the hash field and the number of bits
1702 // reserved for it does not conflict.
1703 ASSERT(TenToThe(String::kMaxCachedArrayIndexLength) <
1704 (1 << String::kArrayIndexValueBits));
1705 // We want the smi-tagged index in key. kArrayIndexValueMask has zeros in
1706 // the low kHashShift bits.
1707 and_(hash, String::kArrayIndexValueMask);
1708 STATIC_ASSERT(String::kHashShift >= kSmiTagSize && kSmiTag == 0);
1709 if (String::kHashShift > kSmiTagSize) {
1710 shr(hash, String::kHashShift - kSmiTagSize);
1711 }
1712 if (!index.is(hash)) {
1713 mov(index, hash);
1714 }
1715}
1716
1717
Steve Blocka7e24c12009-10-30 11:49:00 +00001718void MacroAssembler::CallRuntime(Runtime::FunctionId id, int num_arguments) {
1719 CallRuntime(Runtime::FunctionForId(id), num_arguments);
1720}
1721
1722
Ben Murdochb0fe1622011-05-05 13:52:32 +01001723void MacroAssembler::CallRuntimeSaveDoubles(Runtime::FunctionId id) {
Steve Block44f0eee2011-05-26 01:26:41 +01001724 const Runtime::Function* function = Runtime::FunctionForId(id);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001725 Set(eax, Immediate(function->nargs));
Steve Block44f0eee2011-05-26 01:26:41 +01001726 mov(ebx, Immediate(ExternalReference(function, isolate())));
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001727 CEntryStub ces(1, kSaveFPRegs);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001728 CallStub(&ces);
1729}
1730
1731
Steve Block44f0eee2011-05-26 01:26:41 +01001732void MacroAssembler::CallRuntime(const Runtime::Function* f,
1733 int num_arguments) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001734 // If the expected number of arguments of the runtime function is
1735 // constant, we check that the actual number of arguments match the
1736 // expectation.
1737 if (f->nargs >= 0 && f->nargs != num_arguments) {
1738 IllegalOperation(num_arguments);
1739 return;
1740 }
1741
Leon Clarke4515c472010-02-03 11:58:03 +00001742 // TODO(1236192): Most runtime routines don't need the number of
1743 // arguments passed in because it is constant. At some point we
1744 // should remove this need and make the runtime routine entry code
1745 // smarter.
1746 Set(eax, Immediate(num_arguments));
Steve Block44f0eee2011-05-26 01:26:41 +01001747 mov(ebx, Immediate(ExternalReference(f, isolate())));
Leon Clarke4515c472010-02-03 11:58:03 +00001748 CEntryStub ces(1);
1749 CallStub(&ces);
Steve Blocka7e24c12009-10-30 11:49:00 +00001750}
1751
1752
Ben Murdochbb769b22010-08-11 14:56:33 +01001753void MacroAssembler::CallExternalReference(ExternalReference ref,
1754 int num_arguments) {
1755 mov(eax, Immediate(num_arguments));
1756 mov(ebx, Immediate(ref));
1757
1758 CEntryStub stub(1);
1759 CallStub(&stub);
1760}
1761
1762
Steve Block6ded16b2010-05-10 14:33:55 +01001763void MacroAssembler::TailCallExternalReference(const ExternalReference& ext,
1764 int num_arguments,
1765 int result_size) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001766 // TODO(1236192): Most runtime routines don't need the number of
1767 // arguments passed in because it is constant. At some point we
1768 // should remove this need and make the runtime routine entry code
1769 // smarter.
1770 Set(eax, Immediate(num_arguments));
Steve Block6ded16b2010-05-10 14:33:55 +01001771 JumpToExternalReference(ext);
1772}
1773
1774
1775void MacroAssembler::TailCallRuntime(Runtime::FunctionId fid,
1776 int num_arguments,
1777 int result_size) {
Steve Block44f0eee2011-05-26 01:26:41 +01001778 TailCallExternalReference(ExternalReference(fid, isolate()),
1779 num_arguments,
1780 result_size);
Steve Blocka7e24c12009-10-30 11:49:00 +00001781}
1782
1783
Ben Murdochb0fe1622011-05-05 13:52:32 +01001784// If true, a Handle<T> returned by value from a function with cdecl calling
1785// convention will be returned directly as a value of location_ field in a
1786// register eax.
1787// If false, it is returned as a pointer to a preallocated by caller memory
1788// region. Pointer to this region should be passed to a function as an
1789// implicit first argument.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001790#if defined(USING_BSD_ABI) || defined(__MINGW32__) || defined(__CYGWIN__)
Ben Murdochb0fe1622011-05-05 13:52:32 +01001791static const bool kReturnHandlesDirectly = true;
John Reck59135872010-11-02 12:39:01 -07001792#else
Ben Murdochb0fe1622011-05-05 13:52:32 +01001793static const bool kReturnHandlesDirectly = false;
John Reck59135872010-11-02 12:39:01 -07001794#endif
1795
1796
1797Operand ApiParameterOperand(int index) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001798 return Operand(
1799 esp, (index + (kReturnHandlesDirectly ? 0 : 1)) * kPointerSize);
John Reck59135872010-11-02 12:39:01 -07001800}
1801
1802
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001803void MacroAssembler::PrepareCallApiFunction(int argc) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001804 if (kReturnHandlesDirectly) {
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001805 EnterApiExitFrame(argc);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001806 // When handles are returned directly we don't have to allocate extra
John Reck59135872010-11-02 12:39:01 -07001807 // space for and pass an out parameter.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001808 if (emit_debug_code()) {
1809 mov(esi, Immediate(BitCast<int32_t>(kZapValue)));
1810 }
John Reck59135872010-11-02 12:39:01 -07001811 } else {
1812 // We allocate two additional slots: return value and pointer to it.
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001813 EnterApiExitFrame(argc + 2);
John Reck59135872010-11-02 12:39:01 -07001814
John Reck59135872010-11-02 12:39:01 -07001815 // The argument slots are filled as follows:
1816 //
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001817 // n + 1: output slot
John Reck59135872010-11-02 12:39:01 -07001818 // n: arg n
1819 // ...
1820 // 1: arg1
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001821 // 0: pointer to the output slot
John Reck59135872010-11-02 12:39:01 -07001822
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001823 lea(esi, Operand(esp, (argc + 1) * kPointerSize));
1824 mov(Operand(esp, 0 * kPointerSize), esi);
Steve Block44f0eee2011-05-26 01:26:41 +01001825 if (emit_debug_code()) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001826 mov(Operand(esi, 0), Immediate(0));
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001827 }
1828 }
1829}
1830
1831
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001832void MacroAssembler::CallApiFunctionAndReturn(Address function_address,
1833 int stack_space) {
Steve Blockd0582a62009-12-15 09:54:21 +00001834 ExternalReference next_address =
1835 ExternalReference::handle_scope_next_address();
Steve Blockd0582a62009-12-15 09:54:21 +00001836 ExternalReference limit_address =
1837 ExternalReference::handle_scope_limit_address();
John Reck59135872010-11-02 12:39:01 -07001838 ExternalReference level_address =
1839 ExternalReference::handle_scope_level_address();
Steve Blockd0582a62009-12-15 09:54:21 +00001840
John Reck59135872010-11-02 12:39:01 -07001841 // Allocate HandleScope in callee-save registers.
1842 mov(ebx, Operand::StaticVariable(next_address));
1843 mov(edi, Operand::StaticVariable(limit_address));
1844 add(Operand::StaticVariable(level_address), Immediate(1));
Steve Blockd0582a62009-12-15 09:54:21 +00001845
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001846 // Call the api function.
1847 call(function_address, RelocInfo::RUNTIME_ENTRY);
John Reck59135872010-11-02 12:39:01 -07001848
Ben Murdochb0fe1622011-05-05 13:52:32 +01001849 if (!kReturnHandlesDirectly) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001850 // PrepareCallApiFunction saved pointer to the output slot into
1851 // callee-save register esi.
1852 mov(eax, Operand(esi, 0));
Leon Clarkee46be812010-01-19 14:06:41 +00001853 }
Steve Blockd0582a62009-12-15 09:54:21 +00001854
John Reck59135872010-11-02 12:39:01 -07001855 Label empty_handle;
1856 Label prologue;
1857 Label promote_scheduled_exception;
1858 Label delete_allocated_handles;
1859 Label leave_exit_frame;
Leon Clarkee46be812010-01-19 14:06:41 +00001860
John Reck59135872010-11-02 12:39:01 -07001861 // Check if the result handle holds 0.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001862 test(eax, eax);
Ben Murdoch257744e2011-11-30 15:57:28 +00001863 j(zero, &empty_handle);
John Reck59135872010-11-02 12:39:01 -07001864 // It was non-zero. Dereference to get the result value.
1865 mov(eax, Operand(eax, 0));
1866 bind(&prologue);
1867 // No more valid handles (the result handle was the last one). Restore
1868 // previous handle scope.
1869 mov(Operand::StaticVariable(next_address), ebx);
1870 sub(Operand::StaticVariable(level_address), Immediate(1));
1871 Assert(above_equal, "Invalid HandleScope level");
1872 cmp(edi, Operand::StaticVariable(limit_address));
Ben Murdoch257744e2011-11-30 15:57:28 +00001873 j(not_equal, &delete_allocated_handles);
John Reck59135872010-11-02 12:39:01 -07001874 bind(&leave_exit_frame);
Leon Clarkee46be812010-01-19 14:06:41 +00001875
John Reck59135872010-11-02 12:39:01 -07001876 // Check if the function scheduled an exception.
1877 ExternalReference scheduled_exception_address =
Steve Block44f0eee2011-05-26 01:26:41 +01001878 ExternalReference::scheduled_exception_address(isolate());
John Reck59135872010-11-02 12:39:01 -07001879 cmp(Operand::StaticVariable(scheduled_exception_address),
Steve Block44f0eee2011-05-26 01:26:41 +01001880 Immediate(isolate()->factory()->the_hole_value()));
Ben Murdoch257744e2011-11-30 15:57:28 +00001881 j(not_equal, &promote_scheduled_exception);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001882 LeaveApiExitFrame();
1883 ret(stack_space * kPointerSize);
John Reck59135872010-11-02 12:39:01 -07001884 bind(&promote_scheduled_exception);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001885 TailCallRuntime(Runtime::kPromoteScheduledException, 0, 1);
1886
John Reck59135872010-11-02 12:39:01 -07001887 bind(&empty_handle);
1888 // It was zero; the result is undefined.
Steve Block44f0eee2011-05-26 01:26:41 +01001889 mov(eax, isolate()->factory()->undefined_value());
John Reck59135872010-11-02 12:39:01 -07001890 jmp(&prologue);
Leon Clarkee46be812010-01-19 14:06:41 +00001891
John Reck59135872010-11-02 12:39:01 -07001892 // HandleScope limit has changed. Delete allocated extensions.
Steve Block44f0eee2011-05-26 01:26:41 +01001893 ExternalReference delete_extensions =
1894 ExternalReference::delete_handle_scope_extensions(isolate());
John Reck59135872010-11-02 12:39:01 -07001895 bind(&delete_allocated_handles);
1896 mov(Operand::StaticVariable(limit_address), edi);
1897 mov(edi, eax);
Steve Block44f0eee2011-05-26 01:26:41 +01001898 mov(Operand(esp, 0), Immediate(ExternalReference::isolate_address()));
1899 mov(eax, Immediate(delete_extensions));
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001900 call(eax);
John Reck59135872010-11-02 12:39:01 -07001901 mov(eax, edi);
1902 jmp(&leave_exit_frame);
Steve Blockd0582a62009-12-15 09:54:21 +00001903}
1904
1905
Steve Block6ded16b2010-05-10 14:33:55 +01001906void MacroAssembler::JumpToExternalReference(const ExternalReference& ext) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001907 // Set the entry point and jump to the C entry runtime stub.
1908 mov(ebx, Immediate(ext));
1909 CEntryStub ces(1);
1910 jmp(ces.GetCode(), RelocInfo::CODE_TARGET);
1911}
1912
1913
Ben Murdoch257744e2011-11-30 15:57:28 +00001914void MacroAssembler::SetCallKind(Register dst, CallKind call_kind) {
1915 // This macro takes the dst register to make the code more readable
1916 // at the call sites. However, the dst register has to be ecx to
1917 // follow the calling convention which requires the call type to be
1918 // in ecx.
1919 ASSERT(dst.is(ecx));
1920 if (call_kind == CALL_AS_FUNCTION) {
1921 // Set to some non-zero smi by updating the least significant
1922 // byte.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001923 mov_b(dst, 1 << kSmiTagSize);
Ben Murdoch257744e2011-11-30 15:57:28 +00001924 } else {
1925 // Set to smi zero by clearing the register.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001926 xor_(dst, dst);
Ben Murdoch257744e2011-11-30 15:57:28 +00001927 }
1928}
1929
1930
Steve Blocka7e24c12009-10-30 11:49:00 +00001931void MacroAssembler::InvokePrologue(const ParameterCount& expected,
1932 const ParameterCount& actual,
1933 Handle<Code> code_constant,
1934 const Operand& code_operand,
Ben Murdoch257744e2011-11-30 15:57:28 +00001935 Label* done,
Ben Murdochc7cc0282012-03-05 14:35:55 +00001936 bool* definitely_mismatches,
Ben Murdochb0fe1622011-05-05 13:52:32 +01001937 InvokeFlag flag,
Ben Murdoch257744e2011-11-30 15:57:28 +00001938 Label::Distance done_near,
1939 const CallWrapper& call_wrapper,
1940 CallKind call_kind) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001941 bool definitely_matches = false;
Ben Murdochc7cc0282012-03-05 14:35:55 +00001942 *definitely_mismatches = false;
Steve Blocka7e24c12009-10-30 11:49:00 +00001943 Label invoke;
1944 if (expected.is_immediate()) {
1945 ASSERT(actual.is_immediate());
1946 if (expected.immediate() == actual.immediate()) {
1947 definitely_matches = true;
1948 } else {
1949 mov(eax, actual.immediate());
1950 const int sentinel = SharedFunctionInfo::kDontAdaptArgumentsSentinel;
1951 if (expected.immediate() == sentinel) {
1952 // Don't worry about adapting arguments for builtins that
1953 // don't want that done. Skip adaption code by making it look
1954 // like we have a match between expected and actual number of
1955 // arguments.
1956 definitely_matches = true;
1957 } else {
Ben Murdochc7cc0282012-03-05 14:35:55 +00001958 *definitely_mismatches = true;
Steve Blocka7e24c12009-10-30 11:49:00 +00001959 mov(ebx, expected.immediate());
1960 }
1961 }
1962 } else {
1963 if (actual.is_immediate()) {
1964 // Expected is in register, actual is immediate. This is the
1965 // case when we invoke function values without going through the
1966 // IC mechanism.
1967 cmp(expected.reg(), actual.immediate());
1968 j(equal, &invoke);
1969 ASSERT(expected.reg().is(ebx));
1970 mov(eax, actual.immediate());
1971 } else if (!expected.reg().is(actual.reg())) {
1972 // Both expected and actual are in (different) registers. This
1973 // is the case when we invoke functions using call and apply.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001974 cmp(expected.reg(), actual.reg());
Steve Blocka7e24c12009-10-30 11:49:00 +00001975 j(equal, &invoke);
1976 ASSERT(actual.reg().is(eax));
1977 ASSERT(expected.reg().is(ebx));
1978 }
1979 }
1980
1981 if (!definitely_matches) {
1982 Handle<Code> adaptor =
Steve Block44f0eee2011-05-26 01:26:41 +01001983 isolate()->builtins()->ArgumentsAdaptorTrampoline();
Steve Blocka7e24c12009-10-30 11:49:00 +00001984 if (!code_constant.is_null()) {
1985 mov(edx, Immediate(code_constant));
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001986 add(edx, Immediate(Code::kHeaderSize - kHeapObjectTag));
Steve Blocka7e24c12009-10-30 11:49:00 +00001987 } else if (!code_operand.is_reg(edx)) {
1988 mov(edx, code_operand);
1989 }
1990
1991 if (flag == CALL_FUNCTION) {
Ben Murdoch257744e2011-11-30 15:57:28 +00001992 call_wrapper.BeforeCall(CallSize(adaptor, RelocInfo::CODE_TARGET));
1993 SetCallKind(ecx, call_kind);
Steve Blocka7e24c12009-10-30 11:49:00 +00001994 call(adaptor, RelocInfo::CODE_TARGET);
Ben Murdoch257744e2011-11-30 15:57:28 +00001995 call_wrapper.AfterCall();
Ben Murdochc7cc0282012-03-05 14:35:55 +00001996 if (!*definitely_mismatches) {
1997 jmp(done, done_near);
1998 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001999 } else {
Ben Murdoch257744e2011-11-30 15:57:28 +00002000 SetCallKind(ecx, call_kind);
Steve Blocka7e24c12009-10-30 11:49:00 +00002001 jmp(adaptor, RelocInfo::CODE_TARGET);
2002 }
2003 bind(&invoke);
2004 }
2005}
2006
2007
2008void MacroAssembler::InvokeCode(const Operand& code,
2009 const ParameterCount& expected,
2010 const ParameterCount& actual,
Ben Murdochb0fe1622011-05-05 13:52:32 +01002011 InvokeFlag flag,
Ben Murdoch257744e2011-11-30 15:57:28 +00002012 const CallWrapper& call_wrapper,
2013 CallKind call_kind) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002014 // You can't call a function without a valid frame.
2015 ASSERT(flag == JUMP_FUNCTION || has_frame());
2016
Ben Murdoch257744e2011-11-30 15:57:28 +00002017 Label done;
Ben Murdochc7cc0282012-03-05 14:35:55 +00002018 bool definitely_mismatches = false;
Ben Murdochb0fe1622011-05-05 13:52:32 +01002019 InvokePrologue(expected, actual, Handle<Code>::null(), code,
Ben Murdochc7cc0282012-03-05 14:35:55 +00002020 &done, &definitely_mismatches, flag, Label::kNear,
2021 call_wrapper, call_kind);
2022 if (!definitely_mismatches) {
2023 if (flag == CALL_FUNCTION) {
2024 call_wrapper.BeforeCall(CallSize(code));
2025 SetCallKind(ecx, call_kind);
2026 call(code);
2027 call_wrapper.AfterCall();
2028 } else {
2029 ASSERT(flag == JUMP_FUNCTION);
2030 SetCallKind(ecx, call_kind);
2031 jmp(code);
2032 }
2033 bind(&done);
Steve Blocka7e24c12009-10-30 11:49:00 +00002034 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002035}
2036
2037
2038void MacroAssembler::InvokeCode(Handle<Code> code,
2039 const ParameterCount& expected,
2040 const ParameterCount& actual,
2041 RelocInfo::Mode rmode,
Ben Murdochb0fe1622011-05-05 13:52:32 +01002042 InvokeFlag flag,
Ben Murdoch257744e2011-11-30 15:57:28 +00002043 const CallWrapper& call_wrapper,
2044 CallKind call_kind) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002045 // You can't call a function without a valid frame.
2046 ASSERT(flag == JUMP_FUNCTION || has_frame());
2047
Ben Murdoch257744e2011-11-30 15:57:28 +00002048 Label done;
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002049 Operand dummy(eax, 0);
Ben Murdochc7cc0282012-03-05 14:35:55 +00002050 bool definitely_mismatches = false;
2051 InvokePrologue(expected, actual, code, dummy, &done, &definitely_mismatches,
2052 flag, Label::kNear, call_wrapper, call_kind);
2053 if (!definitely_mismatches) {
2054 if (flag == CALL_FUNCTION) {
2055 call_wrapper.BeforeCall(CallSize(code, rmode));
2056 SetCallKind(ecx, call_kind);
2057 call(code, rmode);
2058 call_wrapper.AfterCall();
2059 } else {
2060 ASSERT(flag == JUMP_FUNCTION);
2061 SetCallKind(ecx, call_kind);
2062 jmp(code, rmode);
2063 }
2064 bind(&done);
Steve Blocka7e24c12009-10-30 11:49:00 +00002065 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002066}
2067
2068
2069void MacroAssembler::InvokeFunction(Register fun,
2070 const ParameterCount& actual,
Ben Murdochb0fe1622011-05-05 13:52:32 +01002071 InvokeFlag flag,
Ben Murdoch257744e2011-11-30 15:57:28 +00002072 const CallWrapper& call_wrapper,
2073 CallKind call_kind) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002074 // You can't call a function without a valid frame.
2075 ASSERT(flag == JUMP_FUNCTION || has_frame());
2076
Steve Blocka7e24c12009-10-30 11:49:00 +00002077 ASSERT(fun.is(edi));
2078 mov(edx, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset));
2079 mov(esi, FieldOperand(edi, JSFunction::kContextOffset));
2080 mov(ebx, FieldOperand(edx, SharedFunctionInfo::kFormalParameterCountOffset));
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002081 SmiUntag(ebx);
Steve Blocka7e24c12009-10-30 11:49:00 +00002082
2083 ParameterCount expected(ebx);
Steve Block791712a2010-08-27 10:21:07 +01002084 InvokeCode(FieldOperand(edi, JSFunction::kCodeEntryOffset),
Ben Murdoch257744e2011-11-30 15:57:28 +00002085 expected, actual, flag, call_wrapper, call_kind);
Steve Blocka7e24c12009-10-30 11:49:00 +00002086}
2087
2088
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002089void MacroAssembler::InvokeFunction(Handle<JSFunction> function,
Andrei Popescu402d9372010-02-26 13:31:12 +00002090 const ParameterCount& actual,
Ben Murdochb0fe1622011-05-05 13:52:32 +01002091 InvokeFlag flag,
Ben Murdoch257744e2011-11-30 15:57:28 +00002092 const CallWrapper& call_wrapper,
2093 CallKind call_kind) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002094 // You can't call a function without a valid frame.
2095 ASSERT(flag == JUMP_FUNCTION || has_frame());
2096
Andrei Popescu402d9372010-02-26 13:31:12 +00002097 // Get the function and setup the context.
Ben Murdochc7cc0282012-03-05 14:35:55 +00002098 LoadHeapObject(edi, function);
Andrei Popescu402d9372010-02-26 13:31:12 +00002099 mov(esi, FieldOperand(edi, JSFunction::kContextOffset));
Ben Murdochb0fe1622011-05-05 13:52:32 +01002100
Andrei Popescu402d9372010-02-26 13:31:12 +00002101 ParameterCount expected(function->shared()->formal_parameter_count());
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002102 // We call indirectly through the code field in the function to
2103 // allow recompilation to take effect without changing any of the
2104 // call sites.
2105 InvokeCode(FieldOperand(edi, JSFunction::kCodeEntryOffset),
2106 expected, actual, flag, call_wrapper, call_kind);
Andrei Popescu402d9372010-02-26 13:31:12 +00002107}
2108
2109
Ben Murdochb0fe1622011-05-05 13:52:32 +01002110void MacroAssembler::InvokeBuiltin(Builtins::JavaScript id,
2111 InvokeFlag flag,
Ben Murdoch257744e2011-11-30 15:57:28 +00002112 const CallWrapper& call_wrapper) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002113 // You can't call a builtin without a valid frame.
2114 ASSERT(flag == JUMP_FUNCTION || has_frame());
Steve Blocka7e24c12009-10-30 11:49:00 +00002115
2116 // Rely on the assertion to check that the number of provided
2117 // arguments match the expected number of arguments. Fake a
2118 // parameter count to avoid emitting code to do the check.
2119 ParameterCount expected(0);
Steve Block791712a2010-08-27 10:21:07 +01002120 GetBuiltinFunction(edi, id);
2121 InvokeCode(FieldOperand(edi, JSFunction::kCodeEntryOffset),
Ben Murdoch257744e2011-11-30 15:57:28 +00002122 expected, expected, flag, call_wrapper, CALL_AS_METHOD);
Steve Blocka7e24c12009-10-30 11:49:00 +00002123}
2124
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002125
Steve Block791712a2010-08-27 10:21:07 +01002126void MacroAssembler::GetBuiltinFunction(Register target,
2127 Builtins::JavaScript id) {
2128 // Load the JavaScript builtin function from the builtins object.
2129 mov(target, Operand(esi, Context::SlotOffset(Context::GLOBAL_INDEX)));
2130 mov(target, FieldOperand(target, GlobalObject::kBuiltinsOffset));
2131 mov(target, FieldOperand(target,
2132 JSBuiltinsObject::OffsetOfFunctionWithId(id)));
2133}
Steve Blocka7e24c12009-10-30 11:49:00 +00002134
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002135
Steve Blocka7e24c12009-10-30 11:49:00 +00002136void MacroAssembler::GetBuiltinEntry(Register target, Builtins::JavaScript id) {
Steve Block6ded16b2010-05-10 14:33:55 +01002137 ASSERT(!target.is(edi));
Andrei Popescu402d9372010-02-26 13:31:12 +00002138 // Load the JavaScript builtin function from the builtins object.
Steve Block791712a2010-08-27 10:21:07 +01002139 GetBuiltinFunction(edi, id);
2140 // Load the code entry point from the function into the target register.
2141 mov(target, FieldOperand(edi, JSFunction::kCodeEntryOffset));
Steve Blocka7e24c12009-10-30 11:49:00 +00002142}
2143
2144
Steve Blockd0582a62009-12-15 09:54:21 +00002145void MacroAssembler::LoadContext(Register dst, int context_chain_length) {
2146 if (context_chain_length > 0) {
2147 // Move up the chain of contexts to the context containing the slot.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002148 mov(dst, Operand(esi, Context::SlotOffset(Context::PREVIOUS_INDEX)));
Steve Blockd0582a62009-12-15 09:54:21 +00002149 for (int i = 1; i < context_chain_length; i++) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002150 mov(dst, Operand(dst, Context::SlotOffset(Context::PREVIOUS_INDEX)));
Steve Blockd0582a62009-12-15 09:54:21 +00002151 }
Steve Block1e0659c2011-05-24 12:43:12 +01002152 } else {
2153 // Slot is in the current function context. Move it into the
2154 // destination register in case we store into it (the write barrier
2155 // cannot be allowed to destroy the context in esi).
2156 mov(dst, esi);
2157 }
2158
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002159 // We should not have found a with context by walking the context chain
Steve Block1e0659c2011-05-24 12:43:12 +01002160 // (i.e., the static scope chain and runtime context chain do not agree).
2161 // A variable occurring in such a scope should have slot type LOOKUP and
2162 // not CONTEXT.
Steve Block44f0eee2011-05-26 01:26:41 +01002163 if (emit_debug_code()) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002164 cmp(FieldOperand(dst, HeapObject::kMapOffset),
2165 isolate()->factory()->with_context_map());
2166 Check(not_equal, "Variable resolved to with context.");
Steve Blockd0582a62009-12-15 09:54:21 +00002167 }
2168}
2169
2170
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002171void MacroAssembler::LoadGlobalFunction(int index, Register function) {
2172 // Load the global or builtins object from the current context.
2173 mov(function, Operand(esi, Context::SlotOffset(Context::GLOBAL_INDEX)));
2174 // Load the global context from the global or builtins object.
2175 mov(function, FieldOperand(function, GlobalObject::kGlobalContextOffset));
2176 // Load the function from the global context.
2177 mov(function, Operand(function, Context::SlotOffset(index)));
2178}
2179
2180
2181void MacroAssembler::LoadGlobalFunctionInitialMap(Register function,
2182 Register map) {
2183 // Load the initial map. The global functions all have initial maps.
2184 mov(map, FieldOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
Steve Block44f0eee2011-05-26 01:26:41 +01002185 if (emit_debug_code()) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002186 Label ok, fail;
Ben Murdoch257744e2011-11-30 15:57:28 +00002187 CheckMap(map, isolate()->factory()->meta_map(), &fail, DO_SMI_CHECK);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002188 jmp(&ok);
2189 bind(&fail);
2190 Abort("Global functions must have initial map");
2191 bind(&ok);
2192 }
2193}
2194
Steve Blockd0582a62009-12-15 09:54:21 +00002195
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002196// Store the value in register src in the safepoint register stack
2197// slot for register dst.
2198void MacroAssembler::StoreToSafepointRegisterSlot(Register dst, Register src) {
2199 mov(SafepointRegisterSlot(dst), src);
2200}
2201
2202
2203void MacroAssembler::StoreToSafepointRegisterSlot(Register dst, Immediate src) {
2204 mov(SafepointRegisterSlot(dst), src);
2205}
2206
2207
2208void MacroAssembler::LoadFromSafepointRegisterSlot(Register dst, Register src) {
2209 mov(dst, SafepointRegisterSlot(src));
2210}
2211
2212
2213Operand MacroAssembler::SafepointRegisterSlot(Register reg) {
2214 return Operand(esp, SafepointRegisterStackIndex(reg.code()) * kPointerSize);
2215}
2216
2217
Ben Murdochb0fe1622011-05-05 13:52:32 +01002218int MacroAssembler::SafepointRegisterStackIndex(int reg_code) {
2219 // The registers are pushed starting with the lowest encoding,
2220 // which means that lowest encodings are furthest away from
2221 // the stack pointer.
2222 ASSERT(reg_code >= 0 && reg_code < kNumSafepointRegisters);
2223 return kNumSafepointRegisters - reg_code - 1;
2224}
2225
2226
Ben Murdochc7cc0282012-03-05 14:35:55 +00002227void MacroAssembler::LoadHeapObject(Register result,
2228 Handle<HeapObject> object) {
2229 if (isolate()->heap()->InNewSpace(*object)) {
2230 Handle<JSGlobalPropertyCell> cell =
2231 isolate()->factory()->NewJSGlobalPropertyCell(object);
2232 mov(result, Operand::Cell(cell));
2233 } else {
2234 mov(result, object);
2235 }
2236}
2237
2238
2239void MacroAssembler::PushHeapObject(Handle<HeapObject> object) {
2240 if (isolate()->heap()->InNewSpace(*object)) {
2241 Handle<JSGlobalPropertyCell> cell =
2242 isolate()->factory()->NewJSGlobalPropertyCell(object);
2243 push(Operand::Cell(cell));
2244 } else {
2245 Push(object);
2246 }
2247}
2248
2249
Steve Blocka7e24c12009-10-30 11:49:00 +00002250void MacroAssembler::Ret() {
2251 ret(0);
2252}
2253
2254
Steve Block1e0659c2011-05-24 12:43:12 +01002255void MacroAssembler::Ret(int bytes_dropped, Register scratch) {
2256 if (is_uint16(bytes_dropped)) {
2257 ret(bytes_dropped);
2258 } else {
2259 pop(scratch);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002260 add(esp, Immediate(bytes_dropped));
Steve Block1e0659c2011-05-24 12:43:12 +01002261 push(scratch);
2262 ret(0);
2263 }
2264}
2265
2266
Leon Clarkee46be812010-01-19 14:06:41 +00002267void MacroAssembler::Drop(int stack_elements) {
2268 if (stack_elements > 0) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002269 add(esp, Immediate(stack_elements * kPointerSize));
Leon Clarkee46be812010-01-19 14:06:41 +00002270 }
2271}
2272
2273
Kristian Monsen0d5e1162010-09-30 15:31:59 +01002274void MacroAssembler::Move(Register dst, Register src) {
2275 if (!dst.is(src)) {
2276 mov(dst, src);
2277 }
2278}
2279
2280
Steve Blocka7e24c12009-10-30 11:49:00 +00002281void MacroAssembler::SetCounter(StatsCounter* counter, int value) {
2282 if (FLAG_native_code_counters && counter->Enabled()) {
2283 mov(Operand::StaticVariable(ExternalReference(counter)), Immediate(value));
2284 }
2285}
2286
2287
2288void MacroAssembler::IncrementCounter(StatsCounter* counter, int value) {
2289 ASSERT(value > 0);
2290 if (FLAG_native_code_counters && counter->Enabled()) {
2291 Operand operand = Operand::StaticVariable(ExternalReference(counter));
2292 if (value == 1) {
2293 inc(operand);
2294 } else {
2295 add(operand, Immediate(value));
2296 }
2297 }
2298}
2299
2300
2301void MacroAssembler::DecrementCounter(StatsCounter* counter, int value) {
2302 ASSERT(value > 0);
2303 if (FLAG_native_code_counters && counter->Enabled()) {
2304 Operand operand = Operand::StaticVariable(ExternalReference(counter));
2305 if (value == 1) {
2306 dec(operand);
2307 } else {
2308 sub(operand, Immediate(value));
2309 }
2310 }
2311}
2312
2313
Leon Clarked91b9f72010-01-27 17:25:45 +00002314void MacroAssembler::IncrementCounter(Condition cc,
2315 StatsCounter* counter,
2316 int value) {
2317 ASSERT(value > 0);
2318 if (FLAG_native_code_counters && counter->Enabled()) {
2319 Label skip;
2320 j(NegateCondition(cc), &skip);
2321 pushfd();
2322 IncrementCounter(counter, value);
2323 popfd();
2324 bind(&skip);
2325 }
2326}
2327
2328
2329void MacroAssembler::DecrementCounter(Condition cc,
2330 StatsCounter* counter,
2331 int value) {
2332 ASSERT(value > 0);
2333 if (FLAG_native_code_counters && counter->Enabled()) {
2334 Label skip;
2335 j(NegateCondition(cc), &skip);
2336 pushfd();
2337 DecrementCounter(counter, value);
2338 popfd();
2339 bind(&skip);
2340 }
2341}
2342
2343
Steve Blocka7e24c12009-10-30 11:49:00 +00002344void MacroAssembler::Assert(Condition cc, const char* msg) {
Steve Block44f0eee2011-05-26 01:26:41 +01002345 if (emit_debug_code()) Check(cc, msg);
Steve Blocka7e24c12009-10-30 11:49:00 +00002346}
2347
2348
Iain Merrick75681382010-08-19 15:07:18 +01002349void MacroAssembler::AssertFastElements(Register elements) {
Steve Block44f0eee2011-05-26 01:26:41 +01002350 if (emit_debug_code()) {
2351 Factory* factory = isolate()->factory();
Iain Merrick75681382010-08-19 15:07:18 +01002352 Label ok;
2353 cmp(FieldOperand(elements, HeapObject::kMapOffset),
Steve Block44f0eee2011-05-26 01:26:41 +01002354 Immediate(factory->fixed_array_map()));
Iain Merrick75681382010-08-19 15:07:18 +01002355 j(equal, &ok);
2356 cmp(FieldOperand(elements, HeapObject::kMapOffset),
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002357 Immediate(factory->fixed_double_array_map()));
2358 j(equal, &ok);
2359 cmp(FieldOperand(elements, HeapObject::kMapOffset),
Steve Block44f0eee2011-05-26 01:26:41 +01002360 Immediate(factory->fixed_cow_array_map()));
Iain Merrick75681382010-08-19 15:07:18 +01002361 j(equal, &ok);
2362 Abort("JSObject with fast elements map has slow elements");
2363 bind(&ok);
2364 }
2365}
2366
2367
Steve Blocka7e24c12009-10-30 11:49:00 +00002368void MacroAssembler::Check(Condition cc, const char* msg) {
2369 Label L;
Ben Murdoch257744e2011-11-30 15:57:28 +00002370 j(cc, &L);
Steve Blocka7e24c12009-10-30 11:49:00 +00002371 Abort(msg);
2372 // will not return here
2373 bind(&L);
2374}
2375
2376
Steve Block6ded16b2010-05-10 14:33:55 +01002377void MacroAssembler::CheckStackAlignment() {
2378 int frame_alignment = OS::ActivationFrameAlignment();
2379 int frame_alignment_mask = frame_alignment - 1;
2380 if (frame_alignment > kPointerSize) {
2381 ASSERT(IsPowerOf2(frame_alignment));
2382 Label alignment_as_expected;
2383 test(esp, Immediate(frame_alignment_mask));
2384 j(zero, &alignment_as_expected);
2385 // Abort if stack is not aligned.
2386 int3();
2387 bind(&alignment_as_expected);
2388 }
2389}
2390
2391
Steve Blocka7e24c12009-10-30 11:49:00 +00002392void MacroAssembler::Abort(const char* msg) {
2393 // We want to pass the msg string like a smi to avoid GC
2394 // problems, however msg is not guaranteed to be aligned
2395 // properly. Instead, we pass an aligned pointer that is
2396 // a proper v8 smi, but also pass the alignment difference
2397 // from the real pointer as a smi.
2398 intptr_t p1 = reinterpret_cast<intptr_t>(msg);
2399 intptr_t p0 = (p1 & ~kSmiTagMask) + kSmiTag;
2400 ASSERT(reinterpret_cast<Object*>(p0)->IsSmi());
2401#ifdef DEBUG
2402 if (msg != NULL) {
2403 RecordComment("Abort message: ");
2404 RecordComment(msg);
2405 }
2406#endif
Steve Blockd0582a62009-12-15 09:54:21 +00002407
Steve Blocka7e24c12009-10-30 11:49:00 +00002408 push(eax);
2409 push(Immediate(p0));
2410 push(Immediate(reinterpret_cast<intptr_t>(Smi::FromInt(p1 - p0))));
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002411 // Disable stub call restrictions to always allow calls to abort.
2412 if (!has_frame_) {
2413 // We don't actually want to generate a pile of code for this, so just
2414 // claim there is a stack frame, without generating one.
2415 FrameScope scope(this, StackFrame::NONE);
2416 CallRuntime(Runtime::kAbort, 2);
2417 } else {
2418 CallRuntime(Runtime::kAbort, 2);
2419 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002420 // will not return here
Steve Blockd0582a62009-12-15 09:54:21 +00002421 int3();
Steve Blocka7e24c12009-10-30 11:49:00 +00002422}
2423
2424
Ben Murdoch257744e2011-11-30 15:57:28 +00002425void MacroAssembler::LoadInstanceDescriptors(Register map,
2426 Register descriptors) {
2427 mov(descriptors,
2428 FieldOperand(map, Map::kInstanceDescriptorsOrBitField3Offset));
2429 Label not_smi;
2430 JumpIfNotSmi(descriptors, &not_smi);
2431 mov(descriptors, isolate()->factory()->empty_descriptor_array());
2432 bind(&not_smi);
Iain Merrick75681382010-08-19 15:07:18 +01002433}
2434
2435
Kristian Monsen0d5e1162010-09-30 15:31:59 +01002436void MacroAssembler::LoadPowerOf2(XMMRegister dst,
2437 Register scratch,
2438 int power) {
2439 ASSERT(is_uintn(power + HeapNumber::kExponentBias,
2440 HeapNumber::kExponentBits));
2441 mov(scratch, Immediate(power + HeapNumber::kExponentBias));
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002442 movd(dst, scratch);
Kristian Monsen0d5e1162010-09-30 15:31:59 +01002443 psllq(dst, HeapNumber::kMantissaBits);
2444}
2445
2446
Andrei Popescu402d9372010-02-26 13:31:12 +00002447void MacroAssembler::JumpIfInstanceTypeIsNotSequentialAscii(
2448 Register instance_type,
2449 Register scratch,
Steve Block6ded16b2010-05-10 14:33:55 +01002450 Label* failure) {
Andrei Popescu402d9372010-02-26 13:31:12 +00002451 if (!scratch.is(instance_type)) {
2452 mov(scratch, instance_type);
2453 }
2454 and_(scratch,
2455 kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask);
2456 cmp(scratch, kStringTag | kSeqStringTag | kAsciiStringTag);
2457 j(not_equal, failure);
2458}
2459
2460
Leon Clarked91b9f72010-01-27 17:25:45 +00002461void MacroAssembler::JumpIfNotBothSequentialAsciiStrings(Register object1,
2462 Register object2,
2463 Register scratch1,
2464 Register scratch2,
2465 Label* failure) {
2466 // Check that both objects are not smis.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002467 STATIC_ASSERT(kSmiTag == 0);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002468 mov(scratch1, object1);
2469 and_(scratch1, object2);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002470 JumpIfSmi(scratch1, failure);
Leon Clarked91b9f72010-01-27 17:25:45 +00002471
2472 // Load instance type for both strings.
2473 mov(scratch1, FieldOperand(object1, HeapObject::kMapOffset));
2474 mov(scratch2, FieldOperand(object2, HeapObject::kMapOffset));
2475 movzx_b(scratch1, FieldOperand(scratch1, Map::kInstanceTypeOffset));
2476 movzx_b(scratch2, FieldOperand(scratch2, Map::kInstanceTypeOffset));
2477
Ben Murdochc7cc0282012-03-05 14:35:55 +00002478 // Check that both are flat ASCII strings.
Leon Clarked91b9f72010-01-27 17:25:45 +00002479 const int kFlatAsciiStringMask =
2480 kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask;
2481 const int kFlatAsciiStringTag = ASCII_STRING_TYPE;
2482 // Interleave bits from both instance types and compare them in one check.
2483 ASSERT_EQ(0, kFlatAsciiStringMask & (kFlatAsciiStringMask << 3));
2484 and_(scratch1, kFlatAsciiStringMask);
2485 and_(scratch2, kFlatAsciiStringMask);
2486 lea(scratch1, Operand(scratch1, scratch2, times_8, 0));
2487 cmp(scratch1, kFlatAsciiStringTag | (kFlatAsciiStringTag << 3));
2488 j(not_equal, failure);
2489}
2490
2491
Steve Block6ded16b2010-05-10 14:33:55 +01002492void MacroAssembler::PrepareCallCFunction(int num_arguments, Register scratch) {
Ben Murdoch8b112d22011-06-08 16:22:53 +01002493 int frame_alignment = OS::ActivationFrameAlignment();
2494 if (frame_alignment != 0) {
Steve Block6ded16b2010-05-10 14:33:55 +01002495 // Make stack end at alignment and make room for num_arguments words
2496 // and the original value of esp.
2497 mov(scratch, esp);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002498 sub(esp, Immediate((num_arguments + 1) * kPointerSize));
Ben Murdoch8b112d22011-06-08 16:22:53 +01002499 ASSERT(IsPowerOf2(frame_alignment));
2500 and_(esp, -frame_alignment);
Steve Block6ded16b2010-05-10 14:33:55 +01002501 mov(Operand(esp, num_arguments * kPointerSize), scratch);
2502 } else {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002503 sub(esp, Immediate(num_arguments * kPointerSize));
Steve Block6ded16b2010-05-10 14:33:55 +01002504 }
2505}
2506
2507
2508void MacroAssembler::CallCFunction(ExternalReference function,
2509 int num_arguments) {
2510 // Trashing eax is ok as it will be the return value.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002511 mov(eax, Immediate(function));
Steve Block6ded16b2010-05-10 14:33:55 +01002512 CallCFunction(eax, num_arguments);
2513}
2514
2515
2516void MacroAssembler::CallCFunction(Register function,
2517 int num_arguments) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002518 ASSERT(has_frame());
Steve Block6ded16b2010-05-10 14:33:55 +01002519 // Check stack alignment.
Steve Block44f0eee2011-05-26 01:26:41 +01002520 if (emit_debug_code()) {
Steve Block6ded16b2010-05-10 14:33:55 +01002521 CheckStackAlignment();
2522 }
2523
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002524 call(function);
Steve Block6ded16b2010-05-10 14:33:55 +01002525 if (OS::ActivationFrameAlignment() != 0) {
2526 mov(esp, Operand(esp, num_arguments * kPointerSize));
2527 } else {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002528 add(esp, Immediate(num_arguments * kPointerSize));
Steve Block6ded16b2010-05-10 14:33:55 +01002529 }
2530}
2531
2532
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002533bool AreAliased(Register r1, Register r2, Register r3, Register r4) {
2534 if (r1.is(r2)) return true;
2535 if (r1.is(r3)) return true;
2536 if (r1.is(r4)) return true;
2537 if (r2.is(r3)) return true;
2538 if (r2.is(r4)) return true;
2539 if (r3.is(r4)) return true;
2540 return false;
2541}
2542
2543
Steve Blocka7e24c12009-10-30 11:49:00 +00002544CodePatcher::CodePatcher(byte* address, int size)
Ben Murdoch8b112d22011-06-08 16:22:53 +01002545 : address_(address),
2546 size_(size),
2547 masm_(Isolate::Current(), address, size + Assembler::kGap) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002548 // Create a new macro assembler pointing to the address of the code to patch.
2549 // The size is adjusted with kGap on order for the assembler to generate size
2550 // bytes of instructions without failing with buffer size constraints.
2551 ASSERT(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
2552}
2553
2554
2555CodePatcher::~CodePatcher() {
2556 // Indicate that code has changed.
2557 CPU::FlushICache(address_, size_);
2558
2559 // Check that the code was patched as expected.
2560 ASSERT(masm_.pc_ == address_ + size_);
2561 ASSERT(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
2562}
2563
2564
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002565void MacroAssembler::CheckPageFlag(
2566 Register object,
2567 Register scratch,
2568 int mask,
2569 Condition cc,
2570 Label* condition_met,
2571 Label::Distance condition_met_distance) {
2572 ASSERT(cc == zero || cc == not_zero);
2573 if (scratch.is(object)) {
2574 and_(scratch, Immediate(~Page::kPageAlignmentMask));
2575 } else {
2576 mov(scratch, Immediate(~Page::kPageAlignmentMask));
2577 and_(scratch, object);
2578 }
2579 if (mask < (1 << kBitsPerByte)) {
2580 test_b(Operand(scratch, MemoryChunk::kFlagsOffset),
2581 static_cast<uint8_t>(mask));
2582 } else {
2583 test(Operand(scratch, MemoryChunk::kFlagsOffset), Immediate(mask));
2584 }
2585 j(cc, condition_met, condition_met_distance);
2586}
2587
2588
2589void MacroAssembler::JumpIfBlack(Register object,
2590 Register scratch0,
2591 Register scratch1,
2592 Label* on_black,
2593 Label::Distance on_black_near) {
2594 HasColor(object, scratch0, scratch1,
2595 on_black, on_black_near,
2596 1, 0); // kBlackBitPattern.
2597 ASSERT(strcmp(Marking::kBlackBitPattern, "10") == 0);
2598}
2599
2600
2601void MacroAssembler::HasColor(Register object,
2602 Register bitmap_scratch,
2603 Register mask_scratch,
2604 Label* has_color,
2605 Label::Distance has_color_distance,
2606 int first_bit,
2607 int second_bit) {
2608 ASSERT(!AreAliased(object, bitmap_scratch, mask_scratch, ecx));
2609
2610 GetMarkBits(object, bitmap_scratch, mask_scratch);
2611
2612 Label other_color, word_boundary;
2613 test(mask_scratch, Operand(bitmap_scratch, MemoryChunk::kHeaderSize));
2614 j(first_bit == 1 ? zero : not_zero, &other_color, Label::kNear);
2615 add(mask_scratch, mask_scratch); // Shift left 1 by adding.
2616 j(zero, &word_boundary, Label::kNear);
2617 test(mask_scratch, Operand(bitmap_scratch, MemoryChunk::kHeaderSize));
2618 j(second_bit == 1 ? not_zero : zero, has_color, has_color_distance);
2619 jmp(&other_color, Label::kNear);
2620
2621 bind(&word_boundary);
2622 test_b(Operand(bitmap_scratch, MemoryChunk::kHeaderSize + kPointerSize), 1);
2623
2624 j(second_bit == 1 ? not_zero : zero, has_color, has_color_distance);
2625 bind(&other_color);
2626}
2627
2628
2629void MacroAssembler::GetMarkBits(Register addr_reg,
2630 Register bitmap_reg,
2631 Register mask_reg) {
2632 ASSERT(!AreAliased(addr_reg, mask_reg, bitmap_reg, ecx));
2633 mov(bitmap_reg, Immediate(~Page::kPageAlignmentMask));
2634 and_(bitmap_reg, addr_reg);
2635 mov(ecx, addr_reg);
2636 int shift =
2637 Bitmap::kBitsPerCellLog2 + kPointerSizeLog2 - Bitmap::kBytesPerCellLog2;
2638 shr(ecx, shift);
2639 and_(ecx,
2640 (Page::kPageAlignmentMask >> shift) & ~(Bitmap::kBytesPerCell - 1));
2641
2642 add(bitmap_reg, ecx);
2643 mov(ecx, addr_reg);
2644 shr(ecx, kPointerSizeLog2);
2645 and_(ecx, (1 << Bitmap::kBitsPerCellLog2) - 1);
2646 mov(mask_reg, Immediate(1));
2647 shl_cl(mask_reg);
2648}
2649
2650
2651void MacroAssembler::EnsureNotWhite(
2652 Register value,
2653 Register bitmap_scratch,
2654 Register mask_scratch,
2655 Label* value_is_white_and_not_data,
2656 Label::Distance distance) {
2657 ASSERT(!AreAliased(value, bitmap_scratch, mask_scratch, ecx));
2658 GetMarkBits(value, bitmap_scratch, mask_scratch);
2659
2660 // If the value is black or grey we don't need to do anything.
2661 ASSERT(strcmp(Marking::kWhiteBitPattern, "00") == 0);
2662 ASSERT(strcmp(Marking::kBlackBitPattern, "10") == 0);
2663 ASSERT(strcmp(Marking::kGreyBitPattern, "11") == 0);
2664 ASSERT(strcmp(Marking::kImpossibleBitPattern, "01") == 0);
2665
2666 Label done;
2667
2668 // Since both black and grey have a 1 in the first position and white does
2669 // not have a 1 there we only need to check one bit.
2670 test(mask_scratch, Operand(bitmap_scratch, MemoryChunk::kHeaderSize));
2671 j(not_zero, &done, Label::kNear);
2672
2673 if (FLAG_debug_code) {
2674 // Check for impossible bit pattern.
2675 Label ok;
2676 push(mask_scratch);
2677 // shl. May overflow making the check conservative.
2678 add(mask_scratch, mask_scratch);
2679 test(mask_scratch, Operand(bitmap_scratch, MemoryChunk::kHeaderSize));
2680 j(zero, &ok, Label::kNear);
2681 int3();
2682 bind(&ok);
2683 pop(mask_scratch);
2684 }
2685
2686 // Value is white. We check whether it is data that doesn't need scanning.
2687 // Currently only checks for HeapNumber and non-cons strings.
2688 Register map = ecx; // Holds map while checking type.
2689 Register length = ecx; // Holds length of object after checking type.
2690 Label not_heap_number;
2691 Label is_data_object;
2692
2693 // Check for heap-number
2694 mov(map, FieldOperand(value, HeapObject::kMapOffset));
2695 cmp(map, FACTORY->heap_number_map());
2696 j(not_equal, &not_heap_number, Label::kNear);
2697 mov(length, Immediate(HeapNumber::kSize));
2698 jmp(&is_data_object, Label::kNear);
2699
2700 bind(&not_heap_number);
2701 // Check for strings.
2702 ASSERT(kIsIndirectStringTag == 1 && kIsIndirectStringMask == 1);
2703 ASSERT(kNotStringTag == 0x80 && kIsNotStringMask == 0x80);
2704 // If it's a string and it's not a cons string then it's an object containing
2705 // no GC pointers.
2706 Register instance_type = ecx;
2707 movzx_b(instance_type, FieldOperand(map, Map::kInstanceTypeOffset));
2708 test_b(instance_type, kIsIndirectStringMask | kIsNotStringMask);
2709 j(not_zero, value_is_white_and_not_data);
2710 // It's a non-indirect (non-cons and non-slice) string.
2711 // If it's external, the length is just ExternalString::kSize.
2712 // Otherwise it's String::kHeaderSize + string->length() * (1 or 2).
2713 Label not_external;
2714 // External strings are the only ones with the kExternalStringTag bit
2715 // set.
2716 ASSERT_EQ(0, kSeqStringTag & kExternalStringTag);
2717 ASSERT_EQ(0, kConsStringTag & kExternalStringTag);
2718 test_b(instance_type, kExternalStringTag);
2719 j(zero, &not_external, Label::kNear);
2720 mov(length, Immediate(ExternalString::kSize));
2721 jmp(&is_data_object, Label::kNear);
2722
2723 bind(&not_external);
2724 // Sequential string, either ASCII or UC16.
2725 ASSERT(kAsciiStringTag == 0x04);
2726 and_(length, Immediate(kStringEncodingMask));
2727 xor_(length, Immediate(kStringEncodingMask));
2728 add(length, Immediate(0x04));
2729 // Value now either 4 (if ASCII) or 8 (if UC16), i.e., char-size shifted
2730 // by 2. If we multiply the string length as smi by this, it still
2731 // won't overflow a 32-bit value.
2732 ASSERT_EQ(SeqAsciiString::kMaxSize, SeqTwoByteString::kMaxSize);
2733 ASSERT(SeqAsciiString::kMaxSize <=
2734 static_cast<int>(0xffffffffu >> (2 + kSmiTagSize)));
2735 imul(length, FieldOperand(value, String::kLengthOffset));
2736 shr(length, 2 + kSmiTagSize + kSmiShiftSize);
2737 add(length, Immediate(SeqString::kHeaderSize + kObjectAlignmentMask));
2738 and_(length, Immediate(~kObjectAlignmentMask));
2739
2740 bind(&is_data_object);
2741 // Value is a data object, and it is white. Mark it black. Since we know
2742 // that the object is white we can make it black by flipping one bit.
2743 or_(Operand(bitmap_scratch, MemoryChunk::kHeaderSize), mask_scratch);
2744
2745 and_(bitmap_scratch, Immediate(~Page::kPageAlignmentMask));
2746 add(Operand(bitmap_scratch, MemoryChunk::kLiveBytesOffset),
2747 length);
2748 if (FLAG_debug_code) {
2749 mov(length, Operand(bitmap_scratch, MemoryChunk::kLiveBytesOffset));
2750 cmp(length, Operand(bitmap_scratch, MemoryChunk::kSizeOffset));
2751 Check(less_equal, "Live Bytes Count overflow chunk size");
2752 }
2753
2754 bind(&done);
2755}
2756
Steve Blocka7e24c12009-10-30 11:49:00 +00002757} } // namespace v8::internal
Leon Clarkef7060e22010-06-03 12:02:55 +01002758
2759#endif // V8_TARGET_ARCH_IA32