blob: bb613ed6c711e1246b66b2a579d401cd8af26d95 [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// Copyright 2006-2009 the V8 project authors. All rights reserved.
2// 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"
33#include "codegen-inl.h"
34#include "debug.h"
35#include "runtime.h"
36#include "serialize.h"
37
38namespace v8 {
39namespace internal {
40
41// -------------------------------------------------------------------------
42// MacroAssembler implementation.
43
44MacroAssembler::MacroAssembler(void* buffer, int size)
45 : Assembler(buffer, size),
Steve Blocka7e24c12009-10-30 11:49:00 +000046 generating_stub_(false),
47 allow_stub_calls_(true),
48 code_object_(Heap::undefined_value()) {
49}
50
51
Steve Block6ded16b2010-05-10 14:33:55 +010052void MacroAssembler::RecordWriteHelper(Register object,
53 Register addr,
54 Register scratch) {
55 if (FLAG_debug_code) {
56 // Check that the object is not in new space.
57 Label not_in_new_space;
58 InNewSpace(object, scratch, not_equal, &not_in_new_space);
59 Abort("new-space object passed to RecordWriteHelper");
60 bind(&not_in_new_space);
61 }
62
Steve Blocka7e24c12009-10-30 11:49:00 +000063 // Compute the page start address from the heap object pointer, and reuse
64 // the 'object' register for it.
Steve Block6ded16b2010-05-10 14:33:55 +010065 and_(object, ~Page::kPageAlignmentMask);
Steve Blocka7e24c12009-10-30 11:49:00 +000066
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +010067 // Compute number of region covering addr. See Page::GetRegionNumberForAddress
68 // method for more details.
69 and_(addr, Page::kPageAlignmentMask);
70 shr(addr, Page::kRegionSizeLog2);
Steve Blocka7e24c12009-10-30 11:49:00 +000071
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +010072 // Set dirty mark for region.
73 bts(Operand(object, Page::kDirtyFlagOffset), addr);
Steve Blocka7e24c12009-10-30 11:49:00 +000074}
75
76
Steve Block6ded16b2010-05-10 14:33:55 +010077void MacroAssembler::InNewSpace(Register object,
78 Register scratch,
79 Condition cc,
80 Label* branch) {
81 ASSERT(cc == equal || cc == not_equal);
82 if (Serializer::enabled()) {
83 // Can't do arithmetic on external references if it might get serialized.
84 mov(scratch, Operand(object));
85 // The mask isn't really an address. We load it as an external reference in
86 // case the size of the new space is different between the snapshot maker
87 // and the running system.
88 and_(Operand(scratch), Immediate(ExternalReference::new_space_mask()));
89 cmp(Operand(scratch), Immediate(ExternalReference::new_space_start()));
90 j(cc, branch);
91 } else {
92 int32_t new_space_start = reinterpret_cast<int32_t>(
93 ExternalReference::new_space_start().address());
94 lea(scratch, Operand(object, -new_space_start));
95 and_(scratch, Heap::NewSpaceMask());
96 j(cc, branch);
Steve Blocka7e24c12009-10-30 11:49:00 +000097 }
Steve Blocka7e24c12009-10-30 11:49:00 +000098}
99
100
Kristian Monsen50ef84f2010-07-29 15:18:00 +0100101void MacroAssembler::RecordWrite(Register object,
102 int offset,
103 Register value,
104 Register scratch) {
Leon Clarke4515c472010-02-03 11:58:03 +0000105 // The compiled code assumes that record write doesn't change the
106 // context register, so we check that none of the clobbered
107 // registers are esi.
108 ASSERT(!object.is(esi) && !value.is(esi) && !scratch.is(esi));
109
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100110 // First, check if a write barrier is even needed. The tests below
111 // catch stores of Smis and stores into young gen.
Steve Blocka7e24c12009-10-30 11:49:00 +0000112 Label done;
113
114 // Skip barrier if writing a smi.
115 ASSERT_EQ(0, kSmiTag);
116 test(value, Immediate(kSmiTagMask));
117 j(zero, &done);
118
Steve Block6ded16b2010-05-10 14:33:55 +0100119 InNewSpace(object, value, equal, &done);
Steve Blocka7e24c12009-10-30 11:49:00 +0000120
Steve Block6ded16b2010-05-10 14:33:55 +0100121 // The offset is relative to a tagged or untagged HeapObject pointer,
122 // so either offset or offset + kHeapObjectTag must be a
123 // multiple of kPointerSize.
124 ASSERT(IsAligned(offset, kPointerSize) ||
125 IsAligned(offset + kHeapObjectTag, kPointerSize));
126
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100127 Register dst = scratch;
128 if (offset != 0) {
129 lea(dst, Operand(object, offset));
Steve Blocka7e24c12009-10-30 11:49:00 +0000130 } else {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100131 // Array access: calculate the destination address in the same manner as
132 // KeyedStoreIC::GenerateGeneric. Multiply a smi by 2 to get an offset
133 // into an array of words.
134 ASSERT_EQ(1, kSmiTagSize);
135 ASSERT_EQ(0, kSmiTag);
136 lea(dst, Operand(object, dst, times_half_pointer_size,
137 FixedArray::kHeaderSize - kHeapObjectTag));
Steve Blocka7e24c12009-10-30 11:49:00 +0000138 }
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100139 RecordWriteHelper(object, dst, value);
Steve Blocka7e24c12009-10-30 11:49:00 +0000140
141 bind(&done);
Leon Clarke4515c472010-02-03 11:58:03 +0000142
143 // Clobber all input registers when running with the debug-code flag
144 // turned on to provoke errors.
145 if (FLAG_debug_code) {
Steve Block6ded16b2010-05-10 14:33:55 +0100146 mov(object, Immediate(BitCast<int32_t>(kZapValue)));
147 mov(value, Immediate(BitCast<int32_t>(kZapValue)));
148 mov(scratch, Immediate(BitCast<int32_t>(kZapValue)));
Leon Clarke4515c472010-02-03 11:58:03 +0000149 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000150}
151
152
Steve Block8defd9f2010-07-08 12:39:36 +0100153void MacroAssembler::RecordWrite(Register object,
154 Register address,
155 Register value) {
156 // The compiled code assumes that record write doesn't change the
157 // context register, so we check that none of the clobbered
158 // registers are esi.
159 ASSERT(!object.is(esi) && !value.is(esi) && !address.is(esi));
160
161 // First, check if a write barrier is even needed. The tests below
162 // catch stores of Smis and stores into young gen.
163 Label done;
164
165 // Skip barrier if writing a smi.
166 ASSERT_EQ(0, kSmiTag);
167 test(value, Immediate(kSmiTagMask));
168 j(zero, &done);
169
170 InNewSpace(object, value, equal, &done);
171
172 RecordWriteHelper(object, address, value);
173
174 bind(&done);
175
176 // Clobber all input registers when running with the debug-code flag
177 // turned on to provoke errors.
178 if (FLAG_debug_code) {
179 mov(object, Immediate(BitCast<int32_t>(kZapValue)));
180 mov(address, Immediate(BitCast<int32_t>(kZapValue)));
181 mov(value, Immediate(BitCast<int32_t>(kZapValue)));
182 }
183}
184
185
Steve Blockd0582a62009-12-15 09:54:21 +0000186void MacroAssembler::StackLimitCheck(Label* on_stack_overflow) {
187 cmp(esp,
188 Operand::StaticVariable(ExternalReference::address_of_stack_limit()));
189 j(below, on_stack_overflow);
190}
191
192
Steve Blocka7e24c12009-10-30 11:49:00 +0000193#ifdef ENABLE_DEBUGGER_SUPPORT
194void MacroAssembler::SaveRegistersToMemory(RegList regs) {
195 ASSERT((regs & ~kJSCallerSaved) == 0);
196 // Copy the content of registers to memory location.
197 for (int i = 0; i < kNumJSCallerSaved; i++) {
198 int r = JSCallerSavedCode(i);
199 if ((regs & (1 << r)) != 0) {
200 Register reg = { r };
201 ExternalReference reg_addr =
202 ExternalReference(Debug_Address::Register(i));
203 mov(Operand::StaticVariable(reg_addr), reg);
204 }
205 }
206}
207
208
209void MacroAssembler::RestoreRegistersFromMemory(RegList regs) {
210 ASSERT((regs & ~kJSCallerSaved) == 0);
211 // Copy the content of memory location to registers.
212 for (int i = kNumJSCallerSaved; --i >= 0;) {
213 int r = JSCallerSavedCode(i);
214 if ((regs & (1 << r)) != 0) {
215 Register reg = { r };
216 ExternalReference reg_addr =
217 ExternalReference(Debug_Address::Register(i));
218 mov(reg, Operand::StaticVariable(reg_addr));
219 }
220 }
221}
222
223
224void MacroAssembler::PushRegistersFromMemory(RegList regs) {
225 ASSERT((regs & ~kJSCallerSaved) == 0);
226 // Push the content of the memory location to the stack.
227 for (int i = 0; i < kNumJSCallerSaved; i++) {
228 int r = JSCallerSavedCode(i);
229 if ((regs & (1 << r)) != 0) {
230 ExternalReference reg_addr =
231 ExternalReference(Debug_Address::Register(i));
232 push(Operand::StaticVariable(reg_addr));
233 }
234 }
235}
236
237
238void MacroAssembler::PopRegistersToMemory(RegList regs) {
239 ASSERT((regs & ~kJSCallerSaved) == 0);
240 // Pop the content from the stack to the memory location.
241 for (int i = kNumJSCallerSaved; --i >= 0;) {
242 int r = JSCallerSavedCode(i);
243 if ((regs & (1 << r)) != 0) {
244 ExternalReference reg_addr =
245 ExternalReference(Debug_Address::Register(i));
246 pop(Operand::StaticVariable(reg_addr));
247 }
248 }
249}
250
251
252void MacroAssembler::CopyRegistersFromStackToMemory(Register base,
253 Register scratch,
254 RegList regs) {
255 ASSERT((regs & ~kJSCallerSaved) == 0);
256 // Copy the content of the stack to the memory location and adjust base.
257 for (int i = kNumJSCallerSaved; --i >= 0;) {
258 int r = JSCallerSavedCode(i);
259 if ((regs & (1 << r)) != 0) {
260 mov(scratch, Operand(base, 0));
261 ExternalReference reg_addr =
262 ExternalReference(Debug_Address::Register(i));
263 mov(Operand::StaticVariable(reg_addr), scratch);
264 lea(base, Operand(base, kPointerSize));
265 }
266 }
267}
Andrei Popescu402d9372010-02-26 13:31:12 +0000268
269void MacroAssembler::DebugBreak() {
270 Set(eax, Immediate(0));
271 mov(ebx, Immediate(ExternalReference(Runtime::kDebugBreak)));
272 CEntryStub ces(1);
273 call(ces.GetCode(), RelocInfo::DEBUG_BREAK);
274}
Steve Blocka7e24c12009-10-30 11:49:00 +0000275#endif
276
277void MacroAssembler::Set(Register dst, const Immediate& x) {
278 if (x.is_zero()) {
279 xor_(dst, Operand(dst)); // shorter than mov
280 } else {
281 mov(dst, x);
282 }
283}
284
285
286void MacroAssembler::Set(const Operand& dst, const Immediate& x) {
287 mov(dst, x);
288}
289
290
291void MacroAssembler::CmpObjectType(Register heap_object,
292 InstanceType type,
293 Register map) {
294 mov(map, FieldOperand(heap_object, HeapObject::kMapOffset));
295 CmpInstanceType(map, type);
296}
297
298
299void MacroAssembler::CmpInstanceType(Register map, InstanceType type) {
300 cmpb(FieldOperand(map, Map::kInstanceTypeOffset),
301 static_cast<int8_t>(type));
302}
303
304
Andrei Popescu31002712010-02-23 13:46:05 +0000305void MacroAssembler::CheckMap(Register obj,
306 Handle<Map> map,
307 Label* fail,
308 bool is_heap_object) {
309 if (!is_heap_object) {
310 test(obj, Immediate(kSmiTagMask));
311 j(zero, fail);
312 }
313 cmp(FieldOperand(obj, HeapObject::kMapOffset), Immediate(map));
314 j(not_equal, fail);
315}
316
317
Leon Clarkee46be812010-01-19 14:06:41 +0000318Condition MacroAssembler::IsObjectStringType(Register heap_object,
319 Register map,
320 Register instance_type) {
321 mov(map, FieldOperand(heap_object, HeapObject::kMapOffset));
322 movzx_b(instance_type, FieldOperand(map, Map::kInstanceTypeOffset));
323 ASSERT(kNotStringTag != 0);
324 test(instance_type, Immediate(kIsNotStringMask));
325 return zero;
326}
327
328
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100329void MacroAssembler::IsObjectJSObjectType(Register heap_object,
330 Register map,
331 Register scratch,
332 Label* fail) {
333 mov(map, FieldOperand(heap_object, HeapObject::kMapOffset));
334 IsInstanceJSObjectType(map, scratch, fail);
335}
336
337
338void MacroAssembler::IsInstanceJSObjectType(Register map,
339 Register scratch,
340 Label* fail) {
341 movzx_b(scratch, FieldOperand(map, Map::kInstanceTypeOffset));
342 sub(Operand(scratch), Immediate(FIRST_JS_OBJECT_TYPE));
343 cmp(scratch, LAST_JS_OBJECT_TYPE - FIRST_JS_OBJECT_TYPE);
344 j(above, fail);
345}
346
347
Steve Blocka7e24c12009-10-30 11:49:00 +0000348void MacroAssembler::FCmp() {
Steve Blockd0582a62009-12-15 09:54:21 +0000349 if (CpuFeatures::IsSupported(CMOV)) {
Steve Block3ce2e202009-11-05 08:53:23 +0000350 fucomip();
351 ffree(0);
352 fincstp();
353 } else {
354 fucompp();
355 push(eax);
356 fnstsw_ax();
357 sahf();
358 pop(eax);
359 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000360}
361
362
Steve Block6ded16b2010-05-10 14:33:55 +0100363void MacroAssembler::AbortIfNotNumber(Register object) {
Andrei Popescu402d9372010-02-26 13:31:12 +0000364 Label ok;
365 test(object, Immediate(kSmiTagMask));
366 j(zero, &ok);
367 cmp(FieldOperand(object, HeapObject::kMapOffset),
368 Factory::heap_number_map());
Steve Block6ded16b2010-05-10 14:33:55 +0100369 Assert(equal, "Operand not a number");
Andrei Popescu402d9372010-02-26 13:31:12 +0000370 bind(&ok);
371}
372
373
Steve Block6ded16b2010-05-10 14:33:55 +0100374void MacroAssembler::AbortIfNotSmi(Register object) {
375 test(object, Immediate(kSmiTagMask));
376 Assert(equal, "Operand not a smi");
377}
378
379
Steve Blocka7e24c12009-10-30 11:49:00 +0000380void MacroAssembler::EnterFrame(StackFrame::Type type) {
381 push(ebp);
382 mov(ebp, Operand(esp));
383 push(esi);
384 push(Immediate(Smi::FromInt(type)));
385 push(Immediate(CodeObject()));
386 if (FLAG_debug_code) {
387 cmp(Operand(esp, 0), Immediate(Factory::undefined_value()));
388 Check(not_equal, "code object not properly patched");
389 }
390}
391
392
393void MacroAssembler::LeaveFrame(StackFrame::Type type) {
394 if (FLAG_debug_code) {
395 cmp(Operand(ebp, StandardFrameConstants::kMarkerOffset),
396 Immediate(Smi::FromInt(type)));
397 Check(equal, "stack frame types must match");
398 }
399 leave();
400}
401
Steve Blockd0582a62009-12-15 09:54:21 +0000402void MacroAssembler::EnterExitFramePrologue(ExitFrame::Mode mode) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000403 // Setup the frame structure on the stack.
404 ASSERT(ExitFrameConstants::kCallerSPDisplacement == +2 * kPointerSize);
405 ASSERT(ExitFrameConstants::kCallerPCOffset == +1 * kPointerSize);
406 ASSERT(ExitFrameConstants::kCallerFPOffset == 0 * kPointerSize);
407 push(ebp);
408 mov(ebp, Operand(esp));
409
410 // Reserve room for entry stack pointer and push the debug marker.
411 ASSERT(ExitFrameConstants::kSPOffset == -1 * kPointerSize);
Andrei Popescu402d9372010-02-26 13:31:12 +0000412 push(Immediate(0)); // Saved entry sp, patched before call.
413 push(Immediate(CodeObject())); // Accessed from ExitFrame::code_slot.
Steve Blocka7e24c12009-10-30 11:49:00 +0000414
415 // Save the frame pointer and the context in top.
416 ExternalReference c_entry_fp_address(Top::k_c_entry_fp_address);
417 ExternalReference context_address(Top::k_context_address);
418 mov(Operand::StaticVariable(c_entry_fp_address), ebp);
419 mov(Operand::StaticVariable(context_address), esi);
Steve Blockd0582a62009-12-15 09:54:21 +0000420}
Steve Blocka7e24c12009-10-30 11:49:00 +0000421
Steve Blockd0582a62009-12-15 09:54:21 +0000422void MacroAssembler::EnterExitFrameEpilogue(ExitFrame::Mode mode, int argc) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000423#ifdef ENABLE_DEBUGGER_SUPPORT
424 // Save the state of all registers to the stack from the memory
425 // location. This is needed to allow nested break points.
Steve Blockd0582a62009-12-15 09:54:21 +0000426 if (mode == ExitFrame::MODE_DEBUG) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000427 // TODO(1243899): This should be symmetric to
428 // CopyRegistersFromStackToMemory() but it isn't! esp is assumed
429 // correct here, but computed for the other call. Very error
430 // prone! FIX THIS. Actually there are deeper problems with
431 // register saving than this asymmetry (see the bug report
432 // associated with this issue).
433 PushRegistersFromMemory(kJSCallerSaved);
434 }
435#endif
436
Steve Blockd0582a62009-12-15 09:54:21 +0000437 // Reserve space for arguments.
438 sub(Operand(esp), Immediate(argc * kPointerSize));
Steve Blocka7e24c12009-10-30 11:49:00 +0000439
440 // Get the required frame alignment for the OS.
441 static const int kFrameAlignment = OS::ActivationFrameAlignment();
442 if (kFrameAlignment > 0) {
443 ASSERT(IsPowerOf2(kFrameAlignment));
444 and_(esp, -kFrameAlignment);
445 }
446
447 // Patch the saved entry sp.
448 mov(Operand(ebp, ExitFrameConstants::kSPOffset), esp);
449}
450
451
Steve Blockd0582a62009-12-15 09:54:21 +0000452void MacroAssembler::EnterExitFrame(ExitFrame::Mode mode) {
453 EnterExitFramePrologue(mode);
454
455 // Setup argc and argv in callee-saved registers.
456 int offset = StandardFrameConstants::kCallerSPOffset - kPointerSize;
457 mov(edi, Operand(eax));
458 lea(esi, Operand(ebp, eax, times_4, offset));
459
460 EnterExitFrameEpilogue(mode, 2);
461}
462
463
464void MacroAssembler::EnterApiExitFrame(ExitFrame::Mode mode,
465 int stack_space,
466 int argc) {
467 EnterExitFramePrologue(mode);
468
469 int offset = StandardFrameConstants::kCallerSPOffset - kPointerSize;
470 lea(esi, Operand(ebp, (stack_space * kPointerSize) + offset));
471
472 EnterExitFrameEpilogue(mode, argc);
473}
474
475
476void MacroAssembler::LeaveExitFrame(ExitFrame::Mode mode) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000477#ifdef ENABLE_DEBUGGER_SUPPORT
478 // Restore the memory copy of the registers by digging them out from
479 // the stack. This is needed to allow nested break points.
Steve Blockd0582a62009-12-15 09:54:21 +0000480 if (mode == ExitFrame::MODE_DEBUG) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000481 // It's okay to clobber register ebx below because we don't need
482 // the function pointer after this.
483 const int kCallerSavedSize = kNumJSCallerSaved * kPointerSize;
Steve Blockd0582a62009-12-15 09:54:21 +0000484 int kOffset = ExitFrameConstants::kCodeOffset - kCallerSavedSize;
Steve Blocka7e24c12009-10-30 11:49:00 +0000485 lea(ebx, Operand(ebp, kOffset));
486 CopyRegistersFromStackToMemory(ebx, ecx, kJSCallerSaved);
487 }
488#endif
489
490 // Get the return address from the stack and restore the frame pointer.
491 mov(ecx, Operand(ebp, 1 * kPointerSize));
492 mov(ebp, Operand(ebp, 0 * kPointerSize));
493
494 // Pop the arguments and the receiver from the caller stack.
495 lea(esp, Operand(esi, 1 * kPointerSize));
496
497 // Restore current context from top and clear it in debug mode.
498 ExternalReference context_address(Top::k_context_address);
499 mov(esi, Operand::StaticVariable(context_address));
500#ifdef DEBUG
501 mov(Operand::StaticVariable(context_address), Immediate(0));
502#endif
503
504 // Push the return address to get ready to return.
505 push(ecx);
506
507 // Clear the top frame.
508 ExternalReference c_entry_fp_address(Top::k_c_entry_fp_address);
509 mov(Operand::StaticVariable(c_entry_fp_address), Immediate(0));
510}
511
512
513void MacroAssembler::PushTryHandler(CodeLocation try_location,
514 HandlerType type) {
515 // Adjust this code if not the case.
516 ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
517 // The pc (return address) is already on TOS.
518 if (try_location == IN_JAVASCRIPT) {
519 if (type == TRY_CATCH_HANDLER) {
520 push(Immediate(StackHandler::TRY_CATCH));
521 } else {
522 push(Immediate(StackHandler::TRY_FINALLY));
523 }
524 push(ebp);
525 } else {
526 ASSERT(try_location == IN_JS_ENTRY);
527 // The frame pointer does not point to a JS frame so we save NULL
528 // for ebp. We expect the code throwing an exception to check ebp
529 // before dereferencing it to restore the context.
530 push(Immediate(StackHandler::ENTRY));
531 push(Immediate(0)); // NULL frame pointer.
532 }
533 // Save the current handler as the next handler.
534 push(Operand::StaticVariable(ExternalReference(Top::k_handler_address)));
535 // Link this handler as the new current one.
536 mov(Operand::StaticVariable(ExternalReference(Top::k_handler_address)), esp);
537}
538
539
Leon Clarkee46be812010-01-19 14:06:41 +0000540void MacroAssembler::PopTryHandler() {
541 ASSERT_EQ(0, StackHandlerConstants::kNextOffset);
542 pop(Operand::StaticVariable(ExternalReference(Top::k_handler_address)));
543 add(Operand(esp), Immediate(StackHandlerConstants::kSize - kPointerSize));
544}
545
546
Steve Blocka7e24c12009-10-30 11:49:00 +0000547void MacroAssembler::CheckAccessGlobalProxy(Register holder_reg,
548 Register scratch,
549 Label* miss) {
550 Label same_contexts;
551
552 ASSERT(!holder_reg.is(scratch));
553
554 // Load current lexical context from the stack frame.
555 mov(scratch, Operand(ebp, StandardFrameConstants::kContextOffset));
556
557 // When generating debug code, make sure the lexical context is set.
558 if (FLAG_debug_code) {
559 cmp(Operand(scratch), Immediate(0));
560 Check(not_equal, "we should not have an empty lexical context");
561 }
562 // Load the global context of the current context.
563 int offset = Context::kHeaderSize + Context::GLOBAL_INDEX * kPointerSize;
564 mov(scratch, FieldOperand(scratch, offset));
565 mov(scratch, FieldOperand(scratch, GlobalObject::kGlobalContextOffset));
566
567 // Check the context is a global context.
568 if (FLAG_debug_code) {
569 push(scratch);
570 // Read the first word and compare to global_context_map.
571 mov(scratch, FieldOperand(scratch, HeapObject::kMapOffset));
572 cmp(scratch, Factory::global_context_map());
573 Check(equal, "JSGlobalObject::global_context should be a global context.");
574 pop(scratch);
575 }
576
577 // Check if both contexts are the same.
578 cmp(scratch, FieldOperand(holder_reg, JSGlobalProxy::kContextOffset));
579 j(equal, &same_contexts, taken);
580
581 // Compare security tokens, save holder_reg on the stack so we can use it
582 // as a temporary register.
583 //
584 // TODO(119): avoid push(holder_reg)/pop(holder_reg)
585 push(holder_reg);
586 // Check that the security token in the calling global object is
587 // compatible with the security token in the receiving global
588 // object.
589 mov(holder_reg, FieldOperand(holder_reg, JSGlobalProxy::kContextOffset));
590
591 // Check the context is a global context.
592 if (FLAG_debug_code) {
593 cmp(holder_reg, Factory::null_value());
594 Check(not_equal, "JSGlobalProxy::context() should not be null.");
595
596 push(holder_reg);
597 // Read the first word and compare to global_context_map(),
598 mov(holder_reg, FieldOperand(holder_reg, HeapObject::kMapOffset));
599 cmp(holder_reg, Factory::global_context_map());
600 Check(equal, "JSGlobalObject::global_context should be a global context.");
601 pop(holder_reg);
602 }
603
604 int token_offset = Context::kHeaderSize +
605 Context::SECURITY_TOKEN_INDEX * kPointerSize;
606 mov(scratch, FieldOperand(scratch, token_offset));
607 cmp(scratch, FieldOperand(holder_reg, token_offset));
608 pop(holder_reg);
609 j(not_equal, miss, not_taken);
610
611 bind(&same_contexts);
612}
613
614
615void MacroAssembler::LoadAllocationTopHelper(Register result,
616 Register result_end,
617 Register scratch,
618 AllocationFlags flags) {
619 ExternalReference new_space_allocation_top =
620 ExternalReference::new_space_allocation_top_address();
621
622 // Just return if allocation top is already known.
623 if ((flags & RESULT_CONTAINS_TOP) != 0) {
624 // No use of scratch if allocation top is provided.
625 ASSERT(scratch.is(no_reg));
626#ifdef DEBUG
627 // Assert that result actually contains top on entry.
628 cmp(result, Operand::StaticVariable(new_space_allocation_top));
629 Check(equal, "Unexpected allocation top");
630#endif
631 return;
632 }
633
634 // Move address of new object to result. Use scratch register if available.
635 if (scratch.is(no_reg)) {
636 mov(result, Operand::StaticVariable(new_space_allocation_top));
637 } else {
638 ASSERT(!scratch.is(result_end));
639 mov(Operand(scratch), Immediate(new_space_allocation_top));
640 mov(result, Operand(scratch, 0));
641 }
642}
643
644
645void MacroAssembler::UpdateAllocationTopHelper(Register result_end,
646 Register scratch) {
Steve Blockd0582a62009-12-15 09:54:21 +0000647 if (FLAG_debug_code) {
648 test(result_end, Immediate(kObjectAlignmentMask));
649 Check(zero, "Unaligned allocation in new space");
650 }
651
Steve Blocka7e24c12009-10-30 11:49:00 +0000652 ExternalReference new_space_allocation_top =
653 ExternalReference::new_space_allocation_top_address();
654
655 // Update new top. Use scratch if available.
656 if (scratch.is(no_reg)) {
657 mov(Operand::StaticVariable(new_space_allocation_top), result_end);
658 } else {
659 mov(Operand(scratch, 0), result_end);
660 }
661}
662
663
664void MacroAssembler::AllocateInNewSpace(int object_size,
665 Register result,
666 Register result_end,
667 Register scratch,
668 Label* gc_required,
669 AllocationFlags flags) {
670 ASSERT(!result.is(result_end));
671
672 // Load address of new object into result.
673 LoadAllocationTopHelper(result, result_end, scratch, flags);
674
675 // Calculate new top and bail out if new space is exhausted.
676 ExternalReference new_space_allocation_limit =
677 ExternalReference::new_space_allocation_limit_address();
678 lea(result_end, Operand(result, object_size));
679 cmp(result_end, Operand::StaticVariable(new_space_allocation_limit));
680 j(above, gc_required, not_taken);
681
Steve Blocka7e24c12009-10-30 11:49:00 +0000682 // Tag result if requested.
683 if ((flags & TAG_OBJECT) != 0) {
Leon Clarkee46be812010-01-19 14:06:41 +0000684 lea(result, Operand(result, kHeapObjectTag));
Steve Blocka7e24c12009-10-30 11:49:00 +0000685 }
Leon Clarkee46be812010-01-19 14:06:41 +0000686
687 // Update allocation top.
688 UpdateAllocationTopHelper(result_end, scratch);
Steve Blocka7e24c12009-10-30 11:49:00 +0000689}
690
691
692void MacroAssembler::AllocateInNewSpace(int header_size,
693 ScaleFactor element_size,
694 Register element_count,
695 Register result,
696 Register result_end,
697 Register scratch,
698 Label* gc_required,
699 AllocationFlags flags) {
700 ASSERT(!result.is(result_end));
701
702 // Load address of new object into result.
703 LoadAllocationTopHelper(result, result_end, scratch, flags);
704
705 // Calculate new top and bail out if new space is exhausted.
706 ExternalReference new_space_allocation_limit =
707 ExternalReference::new_space_allocation_limit_address();
708 lea(result_end, Operand(result, element_count, element_size, header_size));
709 cmp(result_end, Operand::StaticVariable(new_space_allocation_limit));
710 j(above, gc_required);
711
Steve Blocka7e24c12009-10-30 11:49:00 +0000712 // Tag result if requested.
713 if ((flags & TAG_OBJECT) != 0) {
Leon Clarkee46be812010-01-19 14:06:41 +0000714 lea(result, Operand(result, kHeapObjectTag));
Steve Blocka7e24c12009-10-30 11:49:00 +0000715 }
Leon Clarkee46be812010-01-19 14:06:41 +0000716
717 // Update allocation top.
718 UpdateAllocationTopHelper(result_end, scratch);
Steve Blocka7e24c12009-10-30 11:49:00 +0000719}
720
721
722void MacroAssembler::AllocateInNewSpace(Register object_size,
723 Register result,
724 Register result_end,
725 Register scratch,
726 Label* gc_required,
727 AllocationFlags flags) {
728 ASSERT(!result.is(result_end));
729
730 // Load address of new object into result.
731 LoadAllocationTopHelper(result, result_end, scratch, flags);
732
733 // Calculate new top and bail out if new space is exhausted.
734 ExternalReference new_space_allocation_limit =
735 ExternalReference::new_space_allocation_limit_address();
736 if (!object_size.is(result_end)) {
737 mov(result_end, object_size);
738 }
739 add(result_end, Operand(result));
740 cmp(result_end, Operand::StaticVariable(new_space_allocation_limit));
741 j(above, gc_required, not_taken);
742
Steve Blocka7e24c12009-10-30 11:49:00 +0000743 // Tag result if requested.
744 if ((flags & TAG_OBJECT) != 0) {
Leon Clarkee46be812010-01-19 14:06:41 +0000745 lea(result, Operand(result, kHeapObjectTag));
Steve Blocka7e24c12009-10-30 11:49:00 +0000746 }
Leon Clarkee46be812010-01-19 14:06:41 +0000747
748 // Update allocation top.
749 UpdateAllocationTopHelper(result_end, scratch);
Steve Blocka7e24c12009-10-30 11:49:00 +0000750}
751
752
753void MacroAssembler::UndoAllocationInNewSpace(Register object) {
754 ExternalReference new_space_allocation_top =
755 ExternalReference::new_space_allocation_top_address();
756
757 // Make sure the object has no tag before resetting top.
758 and_(Operand(object), Immediate(~kHeapObjectTagMask));
759#ifdef DEBUG
760 cmp(object, Operand::StaticVariable(new_space_allocation_top));
761 Check(below, "Undo allocation of non allocated memory");
762#endif
763 mov(Operand::StaticVariable(new_space_allocation_top), object);
764}
765
766
Steve Block3ce2e202009-11-05 08:53:23 +0000767void MacroAssembler::AllocateHeapNumber(Register result,
768 Register scratch1,
769 Register scratch2,
770 Label* gc_required) {
771 // Allocate heap number in new space.
772 AllocateInNewSpace(HeapNumber::kSize,
773 result,
774 scratch1,
775 scratch2,
776 gc_required,
777 TAG_OBJECT);
778
779 // Set the map.
780 mov(FieldOperand(result, HeapObject::kMapOffset),
781 Immediate(Factory::heap_number_map()));
782}
783
784
Steve Blockd0582a62009-12-15 09:54:21 +0000785void MacroAssembler::AllocateTwoByteString(Register result,
786 Register length,
787 Register scratch1,
788 Register scratch2,
789 Register scratch3,
790 Label* gc_required) {
791 // Calculate the number of bytes needed for the characters in the string while
792 // observing object alignment.
793 ASSERT((SeqTwoByteString::kHeaderSize & kObjectAlignmentMask) == 0);
Steve Blockd0582a62009-12-15 09:54:21 +0000794 ASSERT(kShortSize == 2);
Leon Clarkee46be812010-01-19 14:06:41 +0000795 // scratch1 = length * 2 + kObjectAlignmentMask.
796 lea(scratch1, Operand(length, length, times_1, kObjectAlignmentMask));
Steve Blockd0582a62009-12-15 09:54:21 +0000797 and_(Operand(scratch1), Immediate(~kObjectAlignmentMask));
798
799 // Allocate two byte string in new space.
800 AllocateInNewSpace(SeqTwoByteString::kHeaderSize,
801 times_1,
802 scratch1,
803 result,
804 scratch2,
805 scratch3,
806 gc_required,
807 TAG_OBJECT);
808
809 // Set the map, length and hash field.
810 mov(FieldOperand(result, HeapObject::kMapOffset),
811 Immediate(Factory::string_map()));
Steve Block6ded16b2010-05-10 14:33:55 +0100812 mov(scratch1, length);
813 SmiTag(scratch1);
814 mov(FieldOperand(result, String::kLengthOffset), scratch1);
Steve Blockd0582a62009-12-15 09:54:21 +0000815 mov(FieldOperand(result, String::kHashFieldOffset),
816 Immediate(String::kEmptyHashField));
817}
818
819
820void MacroAssembler::AllocateAsciiString(Register result,
821 Register length,
822 Register scratch1,
823 Register scratch2,
824 Register scratch3,
825 Label* gc_required) {
826 // Calculate the number of bytes needed for the characters in the string while
827 // observing object alignment.
828 ASSERT((SeqAsciiString::kHeaderSize & kObjectAlignmentMask) == 0);
829 mov(scratch1, length);
830 ASSERT(kCharSize == 1);
831 add(Operand(scratch1), Immediate(kObjectAlignmentMask));
832 and_(Operand(scratch1), Immediate(~kObjectAlignmentMask));
833
834 // Allocate ascii string in new space.
835 AllocateInNewSpace(SeqAsciiString::kHeaderSize,
836 times_1,
837 scratch1,
838 result,
839 scratch2,
840 scratch3,
841 gc_required,
842 TAG_OBJECT);
843
844 // Set the map, length and hash field.
845 mov(FieldOperand(result, HeapObject::kMapOffset),
846 Immediate(Factory::ascii_string_map()));
Steve Block6ded16b2010-05-10 14:33:55 +0100847 mov(scratch1, length);
848 SmiTag(scratch1);
849 mov(FieldOperand(result, String::kLengthOffset), scratch1);
Steve Blockd0582a62009-12-15 09:54:21 +0000850 mov(FieldOperand(result, String::kHashFieldOffset),
851 Immediate(String::kEmptyHashField));
852}
853
854
855void MacroAssembler::AllocateConsString(Register result,
856 Register scratch1,
857 Register scratch2,
858 Label* gc_required) {
859 // Allocate heap number in new space.
860 AllocateInNewSpace(ConsString::kSize,
861 result,
862 scratch1,
863 scratch2,
864 gc_required,
865 TAG_OBJECT);
866
867 // Set the map. The other fields are left uninitialized.
868 mov(FieldOperand(result, HeapObject::kMapOffset),
869 Immediate(Factory::cons_string_map()));
870}
871
872
873void MacroAssembler::AllocateAsciiConsString(Register result,
874 Register scratch1,
875 Register scratch2,
876 Label* gc_required) {
877 // Allocate heap number in new space.
878 AllocateInNewSpace(ConsString::kSize,
879 result,
880 scratch1,
881 scratch2,
882 gc_required,
883 TAG_OBJECT);
884
885 // Set the map. The other fields are left uninitialized.
886 mov(FieldOperand(result, HeapObject::kMapOffset),
887 Immediate(Factory::cons_ascii_string_map()));
888}
889
890
Steve Blocka7e24c12009-10-30 11:49:00 +0000891void MacroAssembler::NegativeZeroTest(CodeGenerator* cgen,
892 Register result,
893 Register op,
894 JumpTarget* then_target) {
895 JumpTarget ok;
896 test(result, Operand(result));
897 ok.Branch(not_zero, taken);
898 test(op, Operand(op));
899 then_target->Branch(sign, not_taken);
900 ok.Bind();
901}
902
903
904void MacroAssembler::NegativeZeroTest(Register result,
905 Register op,
906 Label* then_label) {
907 Label ok;
908 test(result, Operand(result));
909 j(not_zero, &ok, taken);
910 test(op, Operand(op));
911 j(sign, then_label, not_taken);
912 bind(&ok);
913}
914
915
916void MacroAssembler::NegativeZeroTest(Register result,
917 Register op1,
918 Register op2,
919 Register scratch,
920 Label* then_label) {
921 Label ok;
922 test(result, Operand(result));
923 j(not_zero, &ok, taken);
924 mov(scratch, Operand(op1));
925 or_(scratch, Operand(op2));
926 j(sign, then_label, not_taken);
927 bind(&ok);
928}
929
930
931void MacroAssembler::TryGetFunctionPrototype(Register function,
932 Register result,
933 Register scratch,
934 Label* miss) {
935 // Check that the receiver isn't a smi.
936 test(function, Immediate(kSmiTagMask));
937 j(zero, miss, not_taken);
938
939 // Check that the function really is a function.
940 CmpObjectType(function, JS_FUNCTION_TYPE, result);
941 j(not_equal, miss, not_taken);
942
943 // Make sure that the function has an instance prototype.
944 Label non_instance;
945 movzx_b(scratch, FieldOperand(result, Map::kBitFieldOffset));
946 test(scratch, Immediate(1 << Map::kHasNonInstancePrototype));
947 j(not_zero, &non_instance, not_taken);
948
949 // Get the prototype or initial map from the function.
950 mov(result,
951 FieldOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
952
953 // If the prototype or initial map is the hole, don't return it and
954 // simply miss the cache instead. This will allow us to allocate a
955 // prototype object on-demand in the runtime system.
956 cmp(Operand(result), Immediate(Factory::the_hole_value()));
957 j(equal, miss, not_taken);
958
959 // If the function does not have an initial map, we're done.
960 Label done;
961 CmpObjectType(result, MAP_TYPE, scratch);
962 j(not_equal, &done);
963
964 // Get the prototype from the initial map.
965 mov(result, FieldOperand(result, Map::kPrototypeOffset));
966 jmp(&done);
967
968 // Non-instance prototype: Fetch prototype from constructor field
969 // in initial map.
970 bind(&non_instance);
971 mov(result, FieldOperand(result, Map::kConstructorOffset));
972
973 // All done.
974 bind(&done);
975}
976
977
978void MacroAssembler::CallStub(CodeStub* stub) {
Leon Clarkee46be812010-01-19 14:06:41 +0000979 ASSERT(allow_stub_calls()); // Calls are not allowed in some stubs.
Steve Blocka7e24c12009-10-30 11:49:00 +0000980 call(stub->GetCode(), RelocInfo::CODE_TARGET);
981}
982
983
Leon Clarkee46be812010-01-19 14:06:41 +0000984Object* MacroAssembler::TryCallStub(CodeStub* stub) {
985 ASSERT(allow_stub_calls()); // Calls are not allowed in some stubs.
986 Object* result = stub->TryGetCode();
987 if (!result->IsFailure()) {
988 call(Handle<Code>(Code::cast(result)), RelocInfo::CODE_TARGET);
989 }
990 return result;
991}
992
993
Steve Blockd0582a62009-12-15 09:54:21 +0000994void MacroAssembler::TailCallStub(CodeStub* stub) {
Leon Clarkee46be812010-01-19 14:06:41 +0000995 ASSERT(allow_stub_calls()); // Calls are not allowed in some stubs.
Steve Blockd0582a62009-12-15 09:54:21 +0000996 jmp(stub->GetCode(), RelocInfo::CODE_TARGET);
997}
998
999
Leon Clarkee46be812010-01-19 14:06:41 +00001000Object* MacroAssembler::TryTailCallStub(CodeStub* stub) {
1001 ASSERT(allow_stub_calls()); // Calls are not allowed in some stubs.
1002 Object* result = stub->TryGetCode();
1003 if (!result->IsFailure()) {
1004 jmp(Handle<Code>(Code::cast(result)), RelocInfo::CODE_TARGET);
1005 }
1006 return result;
1007}
1008
1009
Steve Blocka7e24c12009-10-30 11:49:00 +00001010void MacroAssembler::StubReturn(int argc) {
1011 ASSERT(argc >= 1 && generating_stub());
1012 ret((argc - 1) * kPointerSize);
1013}
1014
1015
1016void MacroAssembler::IllegalOperation(int num_arguments) {
1017 if (num_arguments > 0) {
1018 add(Operand(esp), Immediate(num_arguments * kPointerSize));
1019 }
1020 mov(eax, Immediate(Factory::undefined_value()));
1021}
1022
1023
1024void MacroAssembler::CallRuntime(Runtime::FunctionId id, int num_arguments) {
1025 CallRuntime(Runtime::FunctionForId(id), num_arguments);
1026}
1027
1028
Leon Clarkee46be812010-01-19 14:06:41 +00001029Object* MacroAssembler::TryCallRuntime(Runtime::FunctionId id,
1030 int num_arguments) {
1031 return TryCallRuntime(Runtime::FunctionForId(id), num_arguments);
1032}
1033
1034
Steve Blocka7e24c12009-10-30 11:49:00 +00001035void MacroAssembler::CallRuntime(Runtime::Function* f, int num_arguments) {
1036 // If the expected number of arguments of the runtime function is
1037 // constant, we check that the actual number of arguments match the
1038 // expectation.
1039 if (f->nargs >= 0 && f->nargs != num_arguments) {
1040 IllegalOperation(num_arguments);
1041 return;
1042 }
1043
Leon Clarke4515c472010-02-03 11:58:03 +00001044 // TODO(1236192): Most runtime routines don't need the number of
1045 // arguments passed in because it is constant. At some point we
1046 // should remove this need and make the runtime routine entry code
1047 // smarter.
1048 Set(eax, Immediate(num_arguments));
1049 mov(ebx, Immediate(ExternalReference(f)));
1050 CEntryStub ces(1);
1051 CallStub(&ces);
Steve Blocka7e24c12009-10-30 11:49:00 +00001052}
1053
1054
Andrei Popescu402d9372010-02-26 13:31:12 +00001055void MacroAssembler::CallExternalReference(ExternalReference ref,
1056 int num_arguments) {
1057 mov(eax, Immediate(num_arguments));
1058 mov(ebx, Immediate(ref));
1059
1060 CEntryStub stub(1);
1061 CallStub(&stub);
1062}
1063
1064
Leon Clarkee46be812010-01-19 14:06:41 +00001065Object* MacroAssembler::TryCallRuntime(Runtime::Function* f,
1066 int num_arguments) {
1067 if (f->nargs >= 0 && f->nargs != num_arguments) {
1068 IllegalOperation(num_arguments);
1069 // Since we did not call the stub, there was no allocation failure.
1070 // Return some non-failure object.
1071 return Heap::undefined_value();
1072 }
1073
Leon Clarke4515c472010-02-03 11:58:03 +00001074 // TODO(1236192): Most runtime routines don't need the number of
1075 // arguments passed in because it is constant. At some point we
1076 // should remove this need and make the runtime routine entry code
1077 // smarter.
1078 Set(eax, Immediate(num_arguments));
1079 mov(ebx, Immediate(ExternalReference(f)));
1080 CEntryStub ces(1);
1081 return TryCallStub(&ces);
Leon Clarkee46be812010-01-19 14:06:41 +00001082}
1083
1084
Steve Block6ded16b2010-05-10 14:33:55 +01001085void MacroAssembler::TailCallExternalReference(const ExternalReference& ext,
1086 int num_arguments,
1087 int result_size) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001088 // TODO(1236192): Most runtime routines don't need the number of
1089 // arguments passed in because it is constant. At some point we
1090 // should remove this need and make the runtime routine entry code
1091 // smarter.
1092 Set(eax, Immediate(num_arguments));
Steve Block6ded16b2010-05-10 14:33:55 +01001093 JumpToExternalReference(ext);
1094}
1095
1096
1097void MacroAssembler::TailCallRuntime(Runtime::FunctionId fid,
1098 int num_arguments,
1099 int result_size) {
1100 TailCallExternalReference(ExternalReference(fid), num_arguments, result_size);
Steve Blocka7e24c12009-10-30 11:49:00 +00001101}
1102
1103
Steve Blockd0582a62009-12-15 09:54:21 +00001104void MacroAssembler::PushHandleScope(Register scratch) {
1105 // Push the number of extensions, smi-tagged so the gc will ignore it.
1106 ExternalReference extensions_address =
1107 ExternalReference::handle_scope_extensions_address();
1108 mov(scratch, Operand::StaticVariable(extensions_address));
1109 ASSERT_EQ(0, kSmiTag);
1110 shl(scratch, kSmiTagSize);
1111 push(scratch);
1112 mov(Operand::StaticVariable(extensions_address), Immediate(0));
1113 // Push next and limit pointers which will be wordsize aligned and
1114 // hence automatically smi tagged.
1115 ExternalReference next_address =
1116 ExternalReference::handle_scope_next_address();
1117 push(Operand::StaticVariable(next_address));
1118 ExternalReference limit_address =
1119 ExternalReference::handle_scope_limit_address();
1120 push(Operand::StaticVariable(limit_address));
1121}
1122
1123
Leon Clarkee46be812010-01-19 14:06:41 +00001124Object* MacroAssembler::PopHandleScopeHelper(Register saved,
1125 Register scratch,
1126 bool gc_allowed) {
1127 Object* result = NULL;
Steve Blockd0582a62009-12-15 09:54:21 +00001128 ExternalReference extensions_address =
1129 ExternalReference::handle_scope_extensions_address();
1130 Label write_back;
1131 mov(scratch, Operand::StaticVariable(extensions_address));
1132 cmp(Operand(scratch), Immediate(0));
1133 j(equal, &write_back);
1134 // Calling a runtime function messes with registers so we save and
1135 // restore any one we're asked not to change
1136 if (saved.is_valid()) push(saved);
Leon Clarkee46be812010-01-19 14:06:41 +00001137 if (gc_allowed) {
1138 CallRuntime(Runtime::kDeleteHandleScopeExtensions, 0);
1139 } else {
1140 result = TryCallRuntime(Runtime::kDeleteHandleScopeExtensions, 0);
1141 if (result->IsFailure()) return result;
1142 }
Steve Blockd0582a62009-12-15 09:54:21 +00001143 if (saved.is_valid()) pop(saved);
1144
1145 bind(&write_back);
1146 ExternalReference limit_address =
1147 ExternalReference::handle_scope_limit_address();
1148 pop(Operand::StaticVariable(limit_address));
1149 ExternalReference next_address =
1150 ExternalReference::handle_scope_next_address();
1151 pop(Operand::StaticVariable(next_address));
1152 pop(scratch);
1153 shr(scratch, kSmiTagSize);
1154 mov(Operand::StaticVariable(extensions_address), scratch);
Leon Clarkee46be812010-01-19 14:06:41 +00001155
1156 return result;
1157}
1158
1159
1160void MacroAssembler::PopHandleScope(Register saved, Register scratch) {
1161 PopHandleScopeHelper(saved, scratch, true);
1162}
1163
1164
1165Object* MacroAssembler::TryPopHandleScope(Register saved, Register scratch) {
1166 return PopHandleScopeHelper(saved, scratch, false);
Steve Blockd0582a62009-12-15 09:54:21 +00001167}
1168
1169
Steve Block6ded16b2010-05-10 14:33:55 +01001170void MacroAssembler::JumpToExternalReference(const ExternalReference& ext) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001171 // Set the entry point and jump to the C entry runtime stub.
1172 mov(ebx, Immediate(ext));
1173 CEntryStub ces(1);
1174 jmp(ces.GetCode(), RelocInfo::CODE_TARGET);
1175}
1176
1177
1178void MacroAssembler::InvokePrologue(const ParameterCount& expected,
1179 const ParameterCount& actual,
1180 Handle<Code> code_constant,
1181 const Operand& code_operand,
1182 Label* done,
1183 InvokeFlag flag) {
1184 bool definitely_matches = false;
1185 Label invoke;
1186 if (expected.is_immediate()) {
1187 ASSERT(actual.is_immediate());
1188 if (expected.immediate() == actual.immediate()) {
1189 definitely_matches = true;
1190 } else {
1191 mov(eax, actual.immediate());
1192 const int sentinel = SharedFunctionInfo::kDontAdaptArgumentsSentinel;
1193 if (expected.immediate() == sentinel) {
1194 // Don't worry about adapting arguments for builtins that
1195 // don't want that done. Skip adaption code by making it look
1196 // like we have a match between expected and actual number of
1197 // arguments.
1198 definitely_matches = true;
1199 } else {
1200 mov(ebx, expected.immediate());
1201 }
1202 }
1203 } else {
1204 if (actual.is_immediate()) {
1205 // Expected is in register, actual is immediate. This is the
1206 // case when we invoke function values without going through the
1207 // IC mechanism.
1208 cmp(expected.reg(), actual.immediate());
1209 j(equal, &invoke);
1210 ASSERT(expected.reg().is(ebx));
1211 mov(eax, actual.immediate());
1212 } else if (!expected.reg().is(actual.reg())) {
1213 // Both expected and actual are in (different) registers. This
1214 // is the case when we invoke functions using call and apply.
1215 cmp(expected.reg(), Operand(actual.reg()));
1216 j(equal, &invoke);
1217 ASSERT(actual.reg().is(eax));
1218 ASSERT(expected.reg().is(ebx));
1219 }
1220 }
1221
1222 if (!definitely_matches) {
1223 Handle<Code> adaptor =
1224 Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline));
1225 if (!code_constant.is_null()) {
1226 mov(edx, Immediate(code_constant));
1227 add(Operand(edx), Immediate(Code::kHeaderSize - kHeapObjectTag));
1228 } else if (!code_operand.is_reg(edx)) {
1229 mov(edx, code_operand);
1230 }
1231
1232 if (flag == CALL_FUNCTION) {
1233 call(adaptor, RelocInfo::CODE_TARGET);
1234 jmp(done);
1235 } else {
1236 jmp(adaptor, RelocInfo::CODE_TARGET);
1237 }
1238 bind(&invoke);
1239 }
1240}
1241
1242
1243void MacroAssembler::InvokeCode(const Operand& code,
1244 const ParameterCount& expected,
1245 const ParameterCount& actual,
1246 InvokeFlag flag) {
1247 Label done;
1248 InvokePrologue(expected, actual, Handle<Code>::null(), code, &done, flag);
1249 if (flag == CALL_FUNCTION) {
1250 call(code);
1251 } else {
1252 ASSERT(flag == JUMP_FUNCTION);
1253 jmp(code);
1254 }
1255 bind(&done);
1256}
1257
1258
1259void MacroAssembler::InvokeCode(Handle<Code> code,
1260 const ParameterCount& expected,
1261 const ParameterCount& actual,
1262 RelocInfo::Mode rmode,
1263 InvokeFlag flag) {
1264 Label done;
1265 Operand dummy(eax);
1266 InvokePrologue(expected, actual, code, dummy, &done, flag);
1267 if (flag == CALL_FUNCTION) {
1268 call(code, rmode);
1269 } else {
1270 ASSERT(flag == JUMP_FUNCTION);
1271 jmp(code, rmode);
1272 }
1273 bind(&done);
1274}
1275
1276
1277void MacroAssembler::InvokeFunction(Register fun,
1278 const ParameterCount& actual,
1279 InvokeFlag flag) {
1280 ASSERT(fun.is(edi));
1281 mov(edx, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset));
1282 mov(esi, FieldOperand(edi, JSFunction::kContextOffset));
1283 mov(ebx, FieldOperand(edx, SharedFunctionInfo::kFormalParameterCountOffset));
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001284 SmiUntag(ebx);
Steve Blocka7e24c12009-10-30 11:49:00 +00001285 mov(edx, FieldOperand(edx, SharedFunctionInfo::kCodeOffset));
1286 lea(edx, FieldOperand(edx, Code::kHeaderSize));
1287
1288 ParameterCount expected(ebx);
1289 InvokeCode(Operand(edx), expected, actual, flag);
1290}
1291
1292
Andrei Popescu402d9372010-02-26 13:31:12 +00001293void MacroAssembler::InvokeFunction(JSFunction* function,
1294 const ParameterCount& actual,
1295 InvokeFlag flag) {
1296 ASSERT(function->is_compiled());
1297 // Get the function and setup the context.
1298 mov(edi, Immediate(Handle<JSFunction>(function)));
1299 mov(esi, FieldOperand(edi, JSFunction::kContextOffset));
Steve Blocka7e24c12009-10-30 11:49:00 +00001300
Andrei Popescu402d9372010-02-26 13:31:12 +00001301 // Invoke the cached code.
1302 Handle<Code> code(function->code());
1303 ParameterCount expected(function->shared()->formal_parameter_count());
1304 InvokeCode(code, expected, actual, RelocInfo::CODE_TARGET, flag);
1305}
1306
1307
1308void MacroAssembler::InvokeBuiltin(Builtins::JavaScript id, InvokeFlag flag) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001309 // Calls are not allowed in some stubs.
1310 ASSERT(flag == JUMP_FUNCTION || allow_stub_calls());
1311
1312 // Rely on the assertion to check that the number of provided
1313 // arguments match the expected number of arguments. Fake a
1314 // parameter count to avoid emitting code to do the check.
1315 ParameterCount expected(0);
Andrei Popescu402d9372010-02-26 13:31:12 +00001316 GetBuiltinEntry(edx, id);
1317 InvokeCode(Operand(edx), expected, expected, flag);
Steve Blocka7e24c12009-10-30 11:49:00 +00001318}
1319
1320
1321void MacroAssembler::GetBuiltinEntry(Register target, Builtins::JavaScript id) {
Steve Block6ded16b2010-05-10 14:33:55 +01001322 ASSERT(!target.is(edi));
1323
1324 // Load the builtins object into target register.
1325 mov(target, Operand(esi, Context::SlotOffset(Context::GLOBAL_INDEX)));
1326 mov(target, FieldOperand(target, GlobalObject::kBuiltinsOffset));
1327
Andrei Popescu402d9372010-02-26 13:31:12 +00001328 // Load the JavaScript builtin function from the builtins object.
Steve Block6ded16b2010-05-10 14:33:55 +01001329 mov(edi, FieldOperand(target, JSBuiltinsObject::OffsetOfFunctionWithId(id)));
1330
1331 // Load the code entry point from the builtins object.
1332 mov(target, FieldOperand(target, JSBuiltinsObject::OffsetOfCodeWithId(id)));
1333 if (FLAG_debug_code) {
1334 // Make sure the code objects in the builtins object and in the
1335 // builtin function are the same.
1336 push(target);
1337 mov(target, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset));
1338 mov(target, FieldOperand(target, SharedFunctionInfo::kCodeOffset));
1339 cmp(target, Operand(esp, 0));
1340 Assert(equal, "Builtin code object changed");
1341 pop(target);
1342 }
1343 lea(target, FieldOperand(target, Code::kHeaderSize));
Steve Blocka7e24c12009-10-30 11:49:00 +00001344}
1345
1346
Steve Blockd0582a62009-12-15 09:54:21 +00001347void MacroAssembler::LoadContext(Register dst, int context_chain_length) {
1348 if (context_chain_length > 0) {
1349 // Move up the chain of contexts to the context containing the slot.
1350 mov(dst, Operand(esi, Context::SlotOffset(Context::CLOSURE_INDEX)));
1351 // Load the function context (which is the incoming, outer context).
1352 mov(dst, FieldOperand(dst, JSFunction::kContextOffset));
1353 for (int i = 1; i < context_chain_length; i++) {
1354 mov(dst, Operand(dst, Context::SlotOffset(Context::CLOSURE_INDEX)));
1355 mov(dst, FieldOperand(dst, JSFunction::kContextOffset));
1356 }
1357 // The context may be an intermediate context, not a function context.
1358 mov(dst, Operand(dst, Context::SlotOffset(Context::FCONTEXT_INDEX)));
1359 } else { // Slot is in the current function context.
1360 // The context may be an intermediate context, not a function context.
1361 mov(dst, Operand(esi, Context::SlotOffset(Context::FCONTEXT_INDEX)));
1362 }
1363}
1364
1365
1366
Steve Blocka7e24c12009-10-30 11:49:00 +00001367void MacroAssembler::Ret() {
1368 ret(0);
1369}
1370
1371
Leon Clarkee46be812010-01-19 14:06:41 +00001372void MacroAssembler::Drop(int stack_elements) {
1373 if (stack_elements > 0) {
1374 add(Operand(esp), Immediate(stack_elements * kPointerSize));
1375 }
1376}
1377
1378
1379void MacroAssembler::Move(Register dst, Handle<Object> value) {
1380 mov(dst, value);
1381}
1382
1383
Steve Blocka7e24c12009-10-30 11:49:00 +00001384void MacroAssembler::SetCounter(StatsCounter* counter, int value) {
1385 if (FLAG_native_code_counters && counter->Enabled()) {
1386 mov(Operand::StaticVariable(ExternalReference(counter)), Immediate(value));
1387 }
1388}
1389
1390
1391void MacroAssembler::IncrementCounter(StatsCounter* counter, int value) {
1392 ASSERT(value > 0);
1393 if (FLAG_native_code_counters && counter->Enabled()) {
1394 Operand operand = Operand::StaticVariable(ExternalReference(counter));
1395 if (value == 1) {
1396 inc(operand);
1397 } else {
1398 add(operand, Immediate(value));
1399 }
1400 }
1401}
1402
1403
1404void MacroAssembler::DecrementCounter(StatsCounter* counter, int value) {
1405 ASSERT(value > 0);
1406 if (FLAG_native_code_counters && counter->Enabled()) {
1407 Operand operand = Operand::StaticVariable(ExternalReference(counter));
1408 if (value == 1) {
1409 dec(operand);
1410 } else {
1411 sub(operand, Immediate(value));
1412 }
1413 }
1414}
1415
1416
Leon Clarked91b9f72010-01-27 17:25:45 +00001417void MacroAssembler::IncrementCounter(Condition cc,
1418 StatsCounter* counter,
1419 int value) {
1420 ASSERT(value > 0);
1421 if (FLAG_native_code_counters && counter->Enabled()) {
1422 Label skip;
1423 j(NegateCondition(cc), &skip);
1424 pushfd();
1425 IncrementCounter(counter, value);
1426 popfd();
1427 bind(&skip);
1428 }
1429}
1430
1431
1432void MacroAssembler::DecrementCounter(Condition cc,
1433 StatsCounter* counter,
1434 int value) {
1435 ASSERT(value > 0);
1436 if (FLAG_native_code_counters && counter->Enabled()) {
1437 Label skip;
1438 j(NegateCondition(cc), &skip);
1439 pushfd();
1440 DecrementCounter(counter, value);
1441 popfd();
1442 bind(&skip);
1443 }
1444}
1445
1446
Steve Blocka7e24c12009-10-30 11:49:00 +00001447void MacroAssembler::Assert(Condition cc, const char* msg) {
1448 if (FLAG_debug_code) Check(cc, msg);
1449}
1450
1451
1452void MacroAssembler::Check(Condition cc, const char* msg) {
1453 Label L;
1454 j(cc, &L, taken);
1455 Abort(msg);
1456 // will not return here
1457 bind(&L);
1458}
1459
1460
Steve Block6ded16b2010-05-10 14:33:55 +01001461void MacroAssembler::CheckStackAlignment() {
1462 int frame_alignment = OS::ActivationFrameAlignment();
1463 int frame_alignment_mask = frame_alignment - 1;
1464 if (frame_alignment > kPointerSize) {
1465 ASSERT(IsPowerOf2(frame_alignment));
1466 Label alignment_as_expected;
1467 test(esp, Immediate(frame_alignment_mask));
1468 j(zero, &alignment_as_expected);
1469 // Abort if stack is not aligned.
1470 int3();
1471 bind(&alignment_as_expected);
1472 }
1473}
1474
1475
Steve Blocka7e24c12009-10-30 11:49:00 +00001476void MacroAssembler::Abort(const char* msg) {
1477 // We want to pass the msg string like a smi to avoid GC
1478 // problems, however msg is not guaranteed to be aligned
1479 // properly. Instead, we pass an aligned pointer that is
1480 // a proper v8 smi, but also pass the alignment difference
1481 // from the real pointer as a smi.
1482 intptr_t p1 = reinterpret_cast<intptr_t>(msg);
1483 intptr_t p0 = (p1 & ~kSmiTagMask) + kSmiTag;
1484 ASSERT(reinterpret_cast<Object*>(p0)->IsSmi());
1485#ifdef DEBUG
1486 if (msg != NULL) {
1487 RecordComment("Abort message: ");
1488 RecordComment(msg);
1489 }
1490#endif
Steve Blockd0582a62009-12-15 09:54:21 +00001491 // Disable stub call restrictions to always allow calls to abort.
1492 set_allow_stub_calls(true);
1493
Steve Blocka7e24c12009-10-30 11:49:00 +00001494 push(eax);
1495 push(Immediate(p0));
1496 push(Immediate(reinterpret_cast<intptr_t>(Smi::FromInt(p1 - p0))));
1497 CallRuntime(Runtime::kAbort, 2);
1498 // will not return here
Steve Blockd0582a62009-12-15 09:54:21 +00001499 int3();
Steve Blocka7e24c12009-10-30 11:49:00 +00001500}
1501
1502
Andrei Popescu402d9372010-02-26 13:31:12 +00001503void MacroAssembler::JumpIfInstanceTypeIsNotSequentialAscii(
1504 Register instance_type,
1505 Register scratch,
Steve Block6ded16b2010-05-10 14:33:55 +01001506 Label* failure) {
Andrei Popescu402d9372010-02-26 13:31:12 +00001507 if (!scratch.is(instance_type)) {
1508 mov(scratch, instance_type);
1509 }
1510 and_(scratch,
1511 kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask);
1512 cmp(scratch, kStringTag | kSeqStringTag | kAsciiStringTag);
1513 j(not_equal, failure);
1514}
1515
1516
Leon Clarked91b9f72010-01-27 17:25:45 +00001517void MacroAssembler::JumpIfNotBothSequentialAsciiStrings(Register object1,
1518 Register object2,
1519 Register scratch1,
1520 Register scratch2,
1521 Label* failure) {
1522 // Check that both objects are not smis.
1523 ASSERT_EQ(0, kSmiTag);
1524 mov(scratch1, Operand(object1));
1525 and_(scratch1, Operand(object2));
1526 test(scratch1, Immediate(kSmiTagMask));
1527 j(zero, failure);
1528
1529 // Load instance type for both strings.
1530 mov(scratch1, FieldOperand(object1, HeapObject::kMapOffset));
1531 mov(scratch2, FieldOperand(object2, HeapObject::kMapOffset));
1532 movzx_b(scratch1, FieldOperand(scratch1, Map::kInstanceTypeOffset));
1533 movzx_b(scratch2, FieldOperand(scratch2, Map::kInstanceTypeOffset));
1534
1535 // Check that both are flat ascii strings.
1536 const int kFlatAsciiStringMask =
1537 kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask;
1538 const int kFlatAsciiStringTag = ASCII_STRING_TYPE;
1539 // Interleave bits from both instance types and compare them in one check.
1540 ASSERT_EQ(0, kFlatAsciiStringMask & (kFlatAsciiStringMask << 3));
1541 and_(scratch1, kFlatAsciiStringMask);
1542 and_(scratch2, kFlatAsciiStringMask);
1543 lea(scratch1, Operand(scratch1, scratch2, times_8, 0));
1544 cmp(scratch1, kFlatAsciiStringTag | (kFlatAsciiStringTag << 3));
1545 j(not_equal, failure);
1546}
1547
1548
Steve Block6ded16b2010-05-10 14:33:55 +01001549void MacroAssembler::PrepareCallCFunction(int num_arguments, Register scratch) {
1550 int frameAlignment = OS::ActivationFrameAlignment();
1551 if (frameAlignment != 0) {
1552 // Make stack end at alignment and make room for num_arguments words
1553 // and the original value of esp.
1554 mov(scratch, esp);
1555 sub(Operand(esp), Immediate((num_arguments + 1) * kPointerSize));
1556 ASSERT(IsPowerOf2(frameAlignment));
1557 and_(esp, -frameAlignment);
1558 mov(Operand(esp, num_arguments * kPointerSize), scratch);
1559 } else {
1560 sub(Operand(esp), Immediate(num_arguments * kPointerSize));
1561 }
1562}
1563
1564
1565void MacroAssembler::CallCFunction(ExternalReference function,
1566 int num_arguments) {
1567 // Trashing eax is ok as it will be the return value.
1568 mov(Operand(eax), Immediate(function));
1569 CallCFunction(eax, num_arguments);
1570}
1571
1572
1573void MacroAssembler::CallCFunction(Register function,
1574 int num_arguments) {
1575 // Check stack alignment.
1576 if (FLAG_debug_code) {
1577 CheckStackAlignment();
1578 }
1579
1580 call(Operand(function));
1581 if (OS::ActivationFrameAlignment() != 0) {
1582 mov(esp, Operand(esp, num_arguments * kPointerSize));
1583 } else {
1584 add(Operand(esp), Immediate(num_arguments * sizeof(int32_t)));
1585 }
1586}
1587
1588
Steve Blocka7e24c12009-10-30 11:49:00 +00001589CodePatcher::CodePatcher(byte* address, int size)
1590 : address_(address), size_(size), masm_(address, size + Assembler::kGap) {
1591 // Create a new macro assembler pointing to the address of the code to patch.
1592 // The size is adjusted with kGap on order for the assembler to generate size
1593 // bytes of instructions without failing with buffer size constraints.
1594 ASSERT(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
1595}
1596
1597
1598CodePatcher::~CodePatcher() {
1599 // Indicate that code has changed.
1600 CPU::FlushICache(address_, size_);
1601
1602 // Check that the code was patched as expected.
1603 ASSERT(masm_.pc_ == address_ + size_);
1604 ASSERT(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
1605}
1606
1607
1608} } // namespace v8::internal
Leon Clarkef7060e22010-06-03 12:02:55 +01001609
1610#endif // V8_TARGET_ARCH_IA32