blob: 2e957e4a17ed0c738f8cb9eda8a4b2a312bb7ee7 [file] [log] [blame]
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001// Copyright 2006-2008 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +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
30#include "bootstrapper.h"
31#include "codegen-inl.h"
32#include "debug.h"
33#include "runtime.h"
34
35namespace v8 { namespace internal {
36
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000037// Give alias names to registers
38Register cp = { 8 }; // JavaScript context pointer
39Register pp = { 10 }; // parameter pointer
40
41
42MacroAssembler::MacroAssembler(void* buffer, int size)
43 : Assembler(buffer, size),
44 unresolved_(0),
kasper.lund7276f142008-07-30 08:49:36 +000045 generating_stub_(false),
46 allow_stub_calls_(true) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000047}
48
49
50// We always generate arm code, never thumb code, even if V8 is compiled to
51// thumb, so we require inter-working support
52#if defined(__thumb__) && !defined(__THUMB_INTERWORK__)
53#error "flag -mthumb-interwork missing"
54#endif
55
56
57// We do not support thumb inter-working with an arm architecture not supporting
58// the blx instruction (below v5t)
59#if defined(__THUMB_INTERWORK__)
60#if !defined(__ARM_ARCH_5T__) && !defined(__ARM_ARCH_5TE__)
61// add tests for other versions above v5t as required
62#error "for thumb inter-working we require architecture v5t or above"
63#endif
64#endif
65
66
67// Using blx may yield better code, so use it when required or when available
68#if defined(__THUMB_INTERWORK__) || defined(__ARM_ARCH_5__)
69#define USE_BLX 1
70#endif
71
72// Using bx does not yield better code, so use it only when required
73#if defined(__THUMB_INTERWORK__)
74#define USE_BX 1
75#endif
76
77
78void MacroAssembler::Jump(Register target, Condition cond) {
79#if USE_BX
80 bx(target, cond);
81#else
82 mov(pc, Operand(target), LeaveCC, cond);
83#endif
84}
85
86
ager@chromium.org236ad962008-09-25 09:45:57 +000087void MacroAssembler::Jump(intptr_t target, RelocInfo::Mode rmode,
88 Condition cond) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000089#if USE_BX
90 mov(ip, Operand(target, rmode), LeaveCC, cond);
91 bx(ip, cond);
92#else
93 mov(pc, Operand(target, rmode), LeaveCC, cond);
94#endif
95}
96
97
ager@chromium.org236ad962008-09-25 09:45:57 +000098void MacroAssembler::Jump(byte* target, RelocInfo::Mode rmode,
99 Condition cond) {
100 ASSERT(!RelocInfo::IsCodeTarget(rmode));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000101 Jump(reinterpret_cast<intptr_t>(target), rmode, cond);
102}
103
104
ager@chromium.org236ad962008-09-25 09:45:57 +0000105void MacroAssembler::Jump(Handle<Code> code, RelocInfo::Mode rmode,
106 Condition cond) {
107 ASSERT(RelocInfo::IsCodeTarget(rmode));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000108 // 'code' is always generated ARM code, never THUMB code
109 Jump(reinterpret_cast<intptr_t>(code.location()), rmode, cond);
110}
111
112
113void MacroAssembler::Call(Register target, Condition cond) {
114#if USE_BLX
115 blx(target, cond);
116#else
117 // set lr for return at current pc + 8
118 mov(lr, Operand(pc), LeaveCC, cond);
119 mov(pc, Operand(target), LeaveCC, cond);
120#endif
121}
122
123
ager@chromium.org236ad962008-09-25 09:45:57 +0000124void MacroAssembler::Call(intptr_t target, RelocInfo::Mode rmode,
125 Condition cond) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000126#if !defined(__arm__)
ager@chromium.org236ad962008-09-25 09:45:57 +0000127 if (rmode == RelocInfo::RUNTIME_ENTRY) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000128 mov(r2, Operand(target, rmode), LeaveCC, cond);
129 // Set lr for return at current pc + 8.
130 mov(lr, Operand(pc), LeaveCC, cond);
131 // Emit a ldr<cond> pc, [pc + offset of target in constant pool].
132 // Notify the simulator of the transition to C code.
133 swi(assembler::arm::call_rt_r2);
134 } else {
135 // set lr for return at current pc + 8
136 mov(lr, Operand(pc), LeaveCC, cond);
137 // emit a ldr<cond> pc, [pc + offset of target in constant pool]
138 mov(pc, Operand(target, rmode), LeaveCC, cond);
139 }
140#else
141 // Set lr for return at current pc + 8.
142 mov(lr, Operand(pc), LeaveCC, cond);
143 // Emit a ldr<cond> pc, [pc + offset of target in constant pool].
144 mov(pc, Operand(target, rmode), LeaveCC, cond);
145#endif // !defined(__arm__)
146 // If USE_BLX is defined, we could emit a 'mov ip, target', followed by a
147 // 'blx ip'; however, the code would not be shorter than the above sequence
148 // and the target address of the call would be referenced by the first
149 // instruction rather than the second one, which would make it harder to patch
150 // (two instructions before the return address, instead of one).
151 ASSERT(kTargetAddrToReturnAddrDist == sizeof(Instr));
152}
153
154
ager@chromium.org236ad962008-09-25 09:45:57 +0000155void MacroAssembler::Call(byte* target, RelocInfo::Mode rmode,
156 Condition cond) {
157 ASSERT(!RelocInfo::IsCodeTarget(rmode));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000158 Call(reinterpret_cast<intptr_t>(target), rmode, cond);
159}
160
161
ager@chromium.org236ad962008-09-25 09:45:57 +0000162void MacroAssembler::Call(Handle<Code> code, RelocInfo::Mode rmode,
163 Condition cond) {
164 ASSERT(RelocInfo::IsCodeTarget(rmode));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000165 // 'code' is always generated ARM code, never THUMB code
166 Call(reinterpret_cast<intptr_t>(code.location()), rmode, cond);
167}
168
169
170void MacroAssembler::Ret() {
171#if USE_BX
172 bx(lr);
173#else
174 mov(pc, Operand(lr));
175#endif
176}
177
178
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000179// Will clobber 4 registers: object, offset, scratch, ip. The
180// register 'object' contains a heap object pointer. The heap object
181// tag is shifted away.
182void MacroAssembler::RecordWrite(Register object, Register offset,
183 Register scratch) {
184 // This is how much we shift the remembered set bit offset to get the
185 // offset of the word in the remembered set. We divide by kBitsPerInt (32,
186 // shift right 5) and then multiply by kIntSize (4, shift left 2).
187 const int kRSetWordShift = 3;
188
189 Label fast, done;
190
kasper.lund7276f142008-07-30 08:49:36 +0000191 // First, test that the object is not in the new space. We cannot set
192 // remembered set bits in the new space.
193 // object: heap object pointer (with tag)
194 // offset: offset to store location from the object
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000195 and_(scratch, object, Operand(Heap::NewSpaceMask()));
196 cmp(scratch, Operand(ExternalReference::new_space_start()));
197 b(eq, &done);
198
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000199 // Compute the bit offset in the remembered set.
kasper.lund7276f142008-07-30 08:49:36 +0000200 // object: heap object pointer (with tag)
201 // offset: offset to store location from the object
202 mov(ip, Operand(Page::kPageAlignmentMask)); // load mask only once
203 and_(scratch, object, Operand(ip)); // offset into page of the object
204 add(offset, scratch, Operand(offset)); // add offset into the object
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000205 mov(offset, Operand(offset, LSR, kObjectAlignmentBits));
206
207 // Compute the page address from the heap object pointer.
kasper.lund7276f142008-07-30 08:49:36 +0000208 // object: heap object pointer (with tag)
209 // offset: bit offset of store position in the remembered set
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000210 bic(object, object, Operand(ip));
211
212 // If the bit offset lies beyond the normal remembered set range, it is in
213 // the extra remembered set area of a large object.
kasper.lund7276f142008-07-30 08:49:36 +0000214 // object: page start
215 // offset: bit offset of store position in the remembered set
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000216 cmp(offset, Operand(Page::kPageSize / kPointerSize));
217 b(lt, &fast);
218
219 // Adjust the bit offset to be relative to the start of the extra
220 // remembered set and the start address to be the address of the extra
221 // remembered set.
222 sub(offset, offset, Operand(Page::kPageSize / kPointerSize));
223 // Load the array length into 'scratch' and multiply by four to get the
224 // size in bytes of the elements.
225 ldr(scratch, MemOperand(object, Page::kObjectStartOffset
226 + FixedArray::kLengthOffset));
227 mov(scratch, Operand(scratch, LSL, kObjectAlignmentBits));
228 // Add the page header (including remembered set), array header, and array
229 // body size to the page address.
230 add(object, object, Operand(Page::kObjectStartOffset
231 + Array::kHeaderSize));
232 add(object, object, Operand(scratch));
233
234 bind(&fast);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000235 // Get address of the rset word.
kasper.lund7276f142008-07-30 08:49:36 +0000236 // object: start of the remembered set (page start for the fast case)
237 // offset: bit offset of store position in the remembered set
238 bic(scratch, offset, Operand(kBitsPerInt - 1)); // clear the bit offset
239 add(object, object, Operand(scratch, LSR, kRSetWordShift));
240 // Get bit offset in the rset word.
241 // object: address of remembered set word
242 // offset: bit offset of store position
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000243 and_(offset, offset, Operand(kBitsPerInt - 1));
244
245 ldr(scratch, MemOperand(object));
246 mov(ip, Operand(1));
247 orr(scratch, scratch, Operand(ip, LSL, offset));
248 str(scratch, MemOperand(object));
249
250 bind(&done);
251}
252
253
ager@chromium.org7c537e22008-10-16 08:43:32 +0000254void MacroAssembler::EnterFrame(StackFrame::Type type) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000255 // r0-r3: preserved
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000256 stm(db_w, sp, cp.bit() | fp.bit() | lr.bit());
257 mov(ip, Operand(Smi::FromInt(type)));
258 push(ip);
259 mov(ip, Operand(0));
260 push(ip); // Push an empty code cache slot.
261 add(fp, sp, Operand(3 * kPointerSize)); // Adjust FP to point to saved FP.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000262}
263
264
ager@chromium.org7c537e22008-10-16 08:43:32 +0000265void MacroAssembler::LeaveFrame(StackFrame::Type type) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000266 // r0: preserved
267 // r1: preserved
268 // r2: preserved
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000269
ager@chromium.org7c537e22008-10-16 08:43:32 +0000270 // Drop the execution stack down to the frame pointer and restore
271 // the caller frame pointer and return address.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000272 mov(sp, fp);
273 ldm(ia_w, sp, fp.bit() | lr.bit());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000274}
275
276
ager@chromium.org236ad962008-09-25 09:45:57 +0000277void MacroAssembler::EnterExitFrame(StackFrame::Type type) {
278 ASSERT(type == StackFrame::EXIT || type == StackFrame::EXIT_DEBUG);
279 // Compute parameter pointer before making changes and save it as ip
280 // register so that it is restored as sp register on exit, thereby
281 // popping the args.
282
283 // ip = sp + kPointerSize * #args;
284 add(ip, sp, Operand(r0, LSL, kPointerSizeLog2));
285
286 // Push in reverse order: caller_fp, sp_on_exit, and caller_pc.
287 stm(db_w, sp, fp.bit() | ip.bit() | lr.bit());
288 mov(fp, Operand(sp)); // setup new frame pointer
289
290 // Push debug marker.
291 mov(ip, Operand(type == StackFrame::EXIT_DEBUG ? 1 : 0));
292 push(ip);
293
294 // Save the frame pointer and the context in top.
295 mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address)));
296 str(fp, MemOperand(ip));
297 mov(ip, Operand(ExternalReference(Top::k_context_address)));
298 str(cp, MemOperand(ip));
299
300 // Setup argc and the builtin function in callee-saved registers.
301 mov(r4, Operand(r0));
302 mov(r5, Operand(r1));
303
304 // Compute the argv pointer and keep it in a callee-saved register.
305 add(r6, fp, Operand(r4, LSL, kPointerSizeLog2));
306 add(r6, r6, Operand(ExitFrameConstants::kPPDisplacement - kPointerSize));
307
308 // Save the state of all registers to the stack from the memory
309 // location. This is needed to allow nested break points.
310 if (type == StackFrame::EXIT_DEBUG) {
311 // Use sp as base to push.
312 CopyRegistersFromMemoryToStack(sp, kJSCallerSaved);
313 }
314}
315
316
317void MacroAssembler::LeaveExitFrame(StackFrame::Type type) {
318 // Restore the memory copy of the registers by digging them out from
319 // the stack. This is needed to allow nested break points.
320 if (type == StackFrame::EXIT_DEBUG) {
321 // This code intentionally clobbers r2 and r3.
322 const int kCallerSavedSize = kNumJSCallerSaved * kPointerSize;
323 const int kOffset = ExitFrameConstants::kDebugMarkOffset - kCallerSavedSize;
324 add(r3, fp, Operand(kOffset));
325 CopyRegistersFromStackToMemory(r3, r2, kJSCallerSaved);
326 }
327
328 // Clear top frame.
329 mov(r3, Operand(0));
330 mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address)));
331 str(r3, MemOperand(ip));
332
333 // Restore current context from top and clear it in debug mode.
334 mov(ip, Operand(ExternalReference(Top::k_context_address)));
335 ldr(cp, MemOperand(ip));
336 if (kDebug) {
337 str(r3, MemOperand(ip));
338 }
339
340 // Pop the arguments, restore registers, and return.
341 mov(sp, Operand(fp)); // respect ABI stack constraint
342 ldm(ia, sp, fp.bit() | sp.bit() | pc.bit());
343}
344
345
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000346void MacroAssembler::InvokePrologue(const ParameterCount& expected,
347 const ParameterCount& actual,
348 Handle<Code> code_constant,
349 Register code_reg,
350 Label* done,
351 InvokeFlag flag) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000352 bool definitely_matches = false;
353 Label regular_invoke;
354
355 // Check whether the expected and actual arguments count match. If not,
356 // setup registers according to contract with ArgumentsAdaptorTrampoline:
357 // r0: actual arguments count
358 // r1: function (passed through to callee)
359 // r2: expected arguments count
360 // r3: callee code entry
361
362 // The code below is made a lot easier because the calling code already sets
363 // up actual and expected registers according to the contract if values are
364 // passed in registers.
365 ASSERT(actual.is_immediate() || actual.reg().is(r0));
366 ASSERT(expected.is_immediate() || expected.reg().is(r2));
367 ASSERT((!code_constant.is_null() && code_reg.is(no_reg)) || code_reg.is(r3));
368
369 if (expected.is_immediate()) {
370 ASSERT(actual.is_immediate());
371 if (expected.immediate() == actual.immediate()) {
372 definitely_matches = true;
373 } else {
374 mov(r0, Operand(actual.immediate()));
375 const int sentinel = SharedFunctionInfo::kDontAdaptArgumentsSentinel;
376 if (expected.immediate() == sentinel) {
377 // Don't worry about adapting arguments for builtins that
378 // don't want that done. Skip adaption code by making it look
379 // like we have a match between expected and actual number of
380 // arguments.
381 definitely_matches = true;
382 } else {
383 mov(r2, Operand(expected.immediate()));
384 }
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000385 }
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000386 } else {
387 if (actual.is_immediate()) {
388 cmp(expected.reg(), Operand(actual.immediate()));
389 b(eq, &regular_invoke);
390 mov(r0, Operand(actual.immediate()));
391 } else {
392 cmp(expected.reg(), Operand(actual.reg()));
393 b(eq, &regular_invoke);
394 }
395 }
396
397 if (!definitely_matches) {
398 if (!code_constant.is_null()) {
399 mov(r3, Operand(code_constant));
400 add(r3, r3, Operand(Code::kHeaderSize - kHeapObjectTag));
401 }
402
403 Handle<Code> adaptor =
404 Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline));
405 if (flag == CALL_FUNCTION) {
ager@chromium.org236ad962008-09-25 09:45:57 +0000406 Call(adaptor, RelocInfo::CODE_TARGET);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000407 b(done);
408 } else {
ager@chromium.org236ad962008-09-25 09:45:57 +0000409 Jump(adaptor, RelocInfo::CODE_TARGET);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000410 }
411 bind(&regular_invoke);
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000412 }
413}
414
415
416void MacroAssembler::InvokeCode(Register code,
417 const ParameterCount& expected,
418 const ParameterCount& actual,
419 InvokeFlag flag) {
420 Label done;
421
422 InvokePrologue(expected, actual, Handle<Code>::null(), code, &done, flag);
423 if (flag == CALL_FUNCTION) {
424 Call(code);
425 } else {
426 ASSERT(flag == JUMP_FUNCTION);
427 Jump(code);
428 }
429
430 // Continue here if InvokePrologue does handle the invocation due to
431 // mismatched parameter counts.
432 bind(&done);
433}
434
435
436void MacroAssembler::InvokeCode(Handle<Code> code,
437 const ParameterCount& expected,
438 const ParameterCount& actual,
ager@chromium.org236ad962008-09-25 09:45:57 +0000439 RelocInfo::Mode rmode,
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000440 InvokeFlag flag) {
441 Label done;
442
443 InvokePrologue(expected, actual, code, no_reg, &done, flag);
444 if (flag == CALL_FUNCTION) {
445 Call(code, rmode);
446 } else {
447 Jump(code, rmode);
448 }
449
450 // Continue here if InvokePrologue does handle the invocation due to
451 // mismatched parameter counts.
452 bind(&done);
453}
454
455
456void MacroAssembler::InvokeFunction(Register fun,
457 const ParameterCount& actual,
458 InvokeFlag flag) {
459 // Contract with called JS functions requires that function is passed in r1.
460 ASSERT(fun.is(r1));
461
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000462 Register expected_reg = r2;
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000463 Register code_reg = r3;
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000464
465 ldr(code_reg, FieldMemOperand(r1, JSFunction::kSharedFunctionInfoOffset));
466 ldr(cp, FieldMemOperand(r1, JSFunction::kContextOffset));
467 ldr(expected_reg,
468 FieldMemOperand(code_reg,
469 SharedFunctionInfo::kFormalParameterCountOffset));
470 ldr(code_reg,
471 MemOperand(code_reg, SharedFunctionInfo::kCodeOffset - kHeapObjectTag));
472 add(code_reg, code_reg, Operand(Code::kHeaderSize - kHeapObjectTag));
473
474 ParameterCount expected(expected_reg);
475 InvokeCode(code_reg, expected, actual, flag);
476}
477
478
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000479void MacroAssembler::SaveRegistersToMemory(RegList regs) {
480 ASSERT((regs & ~kJSCallerSaved) == 0);
481 // Copy the content of registers to memory location.
482 for (int i = 0; i < kNumJSCallerSaved; i++) {
483 int r = JSCallerSavedCode(i);
484 if ((regs & (1 << r)) != 0) {
485 Register reg = { r };
486 mov(ip, Operand(ExternalReference(Debug_Address::Register(i))));
487 str(reg, MemOperand(ip));
488 }
489 }
490}
491
492
493void MacroAssembler::RestoreRegistersFromMemory(RegList regs) {
494 ASSERT((regs & ~kJSCallerSaved) == 0);
495 // Copy the content of memory location to registers.
496 for (int i = kNumJSCallerSaved; --i >= 0;) {
497 int r = JSCallerSavedCode(i);
498 if ((regs & (1 << r)) != 0) {
499 Register reg = { r };
500 mov(ip, Operand(ExternalReference(Debug_Address::Register(i))));
501 ldr(reg, MemOperand(ip));
502 }
503 }
504}
505
506
507void MacroAssembler::CopyRegistersFromMemoryToStack(Register base,
508 RegList regs) {
509 ASSERT((regs & ~kJSCallerSaved) == 0);
510 // Copy the content of the memory location to the stack and adjust base.
511 for (int i = kNumJSCallerSaved; --i >= 0;) {
512 int r = JSCallerSavedCode(i);
513 if ((regs & (1 << r)) != 0) {
514 mov(ip, Operand(ExternalReference(Debug_Address::Register(i))));
515 ldr(ip, MemOperand(ip));
516 str(ip, MemOperand(base, 4, NegPreIndex));
517 }
518 }
519}
520
521
522void MacroAssembler::CopyRegistersFromStackToMemory(Register base,
523 Register scratch,
524 RegList regs) {
525 ASSERT((regs & ~kJSCallerSaved) == 0);
526 // Copy the content of the stack to the memory location and adjust base.
527 for (int i = 0; i < kNumJSCallerSaved; i++) {
528 int r = JSCallerSavedCode(i);
529 if ((regs & (1 << r)) != 0) {
530 mov(ip, Operand(ExternalReference(Debug_Address::Register(i))));
531 ldr(scratch, MemOperand(base, 4, PostIndex));
532 str(scratch, MemOperand(ip));
533 }
534 }
535}
536
537
538void MacroAssembler::PushTryHandler(CodeLocation try_location,
539 HandlerType type) {
540 ASSERT(StackHandlerConstants::kSize == 6 * kPointerSize); // adjust this code
541 // The pc (return address) is passed in register lr.
542 if (try_location == IN_JAVASCRIPT) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000543 stm(db_w, sp, pp.bit() | fp.bit() | lr.bit());
544 if (type == TRY_CATCH_HANDLER) {
545 mov(r3, Operand(StackHandler::TRY_CATCH));
546 } else {
547 mov(r3, Operand(StackHandler::TRY_FINALLY));
548 }
549 push(r3); // state
550 mov(r3, Operand(ExternalReference(Top::k_handler_address)));
551 ldr(r1, MemOperand(r3));
552 push(r1); // next sp
553 str(sp, MemOperand(r3)); // chain handler
mads.s.ager31e71382008-08-13 09:32:07 +0000554 mov(r0, Operand(Smi::FromInt(StackHandler::kCodeNotPresent))); // new TOS
555 push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000556 } else {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000557 // Must preserve r0-r4, r5-r7 are available.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000558 ASSERT(try_location == IN_JS_ENTRY);
559 // The parameter pointer is meaningless here and fp does not point to a JS
560 // frame. So we save NULL for both pp and fp. We expect the code throwing an
561 // exception to check fp before dereferencing it to restore the context.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000562 mov(pp, Operand(0)); // set pp to NULL
563 mov(ip, Operand(0)); // to save a NULL fp
564 stm(db_w, sp, pp.bit() | ip.bit() | lr.bit());
565 mov(r6, Operand(StackHandler::ENTRY));
566 push(r6); // state
567 mov(r7, Operand(ExternalReference(Top::k_handler_address)));
568 ldr(r6, MemOperand(r7));
569 push(r6); // next sp
570 str(sp, MemOperand(r7)); // chain handler
mads.s.ager31e71382008-08-13 09:32:07 +0000571 mov(r5, Operand(Smi::FromInt(StackHandler::kCodeNotPresent))); // new TOS
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000572 push(r5); // flush TOS
573 }
574}
575
576
577Register MacroAssembler::CheckMaps(JSObject* object, Register object_reg,
578 JSObject* holder, Register holder_reg,
579 Register scratch,
580 Label* miss) {
581 // Make sure there's no overlap between scratch and the other
582 // registers.
583 ASSERT(!scratch.is(object_reg) && !scratch.is(holder_reg));
584
585 // Keep track of the current object in register reg.
586 Register reg = object_reg;
587 int depth = 1;
588
589 // Check the maps in the prototype chain.
590 // Traverse the prototype chain from the object and do map checks.
591 while (object != holder) {
592 depth++;
593
594 // Only global objects and objects that do not require access
595 // checks are allowed in stubs.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000596 ASSERT(object->IsJSGlobalProxy() || !object->IsAccessCheckNeeded());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000597
598 // Get the map of the current object.
599 ldr(scratch, FieldMemOperand(reg, HeapObject::kMapOffset));
600 cmp(scratch, Operand(Handle<Map>(object->map())));
601
602 // Branch on the result of the map check.
603 b(ne, miss);
604
605 // Check access rights to the global object. This has to happen
606 // after the map check so that we know that the object is
607 // actually a global object.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000608 if (object->IsJSGlobalProxy()) {
609 CheckAccessGlobalProxy(reg, scratch, miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000610 // Restore scratch register to be the map of the object. In the
611 // new space case below, we load the prototype from the map in
612 // the scratch register.
613 ldr(scratch, FieldMemOperand(reg, HeapObject::kMapOffset));
614 }
615
616 reg = holder_reg; // from now the object is in holder_reg
617 JSObject* prototype = JSObject::cast(object->GetPrototype());
618 if (Heap::InNewSpace(prototype)) {
619 // The prototype is in new space; we cannot store a reference
620 // to it in the code. Load it from the map.
621 ldr(reg, FieldMemOperand(scratch, Map::kPrototypeOffset));
622 } else {
623 // The prototype is in old space; load it directly.
624 mov(reg, Operand(Handle<JSObject>(prototype)));
625 }
626
627 // Go to the next object in the prototype chain.
628 object = prototype;
629 }
630
631 // Check the holder map.
632 ldr(scratch, FieldMemOperand(reg, HeapObject::kMapOffset));
633 cmp(scratch, Operand(Handle<Map>(object->map())));
634 b(ne, miss);
635
636 // Log the check depth.
637 LOG(IntEvent("check-maps-depth", depth));
638
639 // Perform security check for access to the global object and return
640 // the holder register.
641 ASSERT(object == holder);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000642 ASSERT(object->IsJSGlobalProxy() || !object->IsAccessCheckNeeded());
643 if (object->IsJSGlobalProxy()) {
644 CheckAccessGlobalProxy(reg, scratch, miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000645 }
646 return reg;
647}
648
649
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000650void MacroAssembler::CheckAccessGlobalProxy(Register holder_reg,
651 Register scratch,
652 Label* miss) {
653 Label same_contexts;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000654
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000655 ASSERT(!holder_reg.is(scratch));
656 ASSERT(!holder_reg.is(ip));
657 ASSERT(!scratch.is(ip));
658
659 // Load current lexical context from the stack frame.
660 ldr(scratch, MemOperand(fp, StandardFrameConstants::kContextOffset));
661 // In debug mode, make sure the lexical context is set.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000662 if (kDebug) {
663 cmp(scratch, Operand(0));
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000664 Check(ne, "we should not have an empty lexical context");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000665 }
666
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000667 // Load the global context of the current context.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000668 int offset = Context::kHeaderSize + Context::GLOBAL_INDEX * kPointerSize;
669 ldr(scratch, FieldMemOperand(scratch, offset));
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000670 ldr(scratch, FieldMemOperand(scratch, GlobalObject::kGlobalContextOffset));
671
672 // Check the context is a global context.
673 if (FLAG_debug_code) {
674 // TODO(119): avoid push(holder_reg)/pop(holder_reg)
675 // Cannot use ip as a temporary in this verification code. Due to the fact
676 // that ip is clobbered as part of cmp with an object Operand.
677 push(holder_reg); // Temporarily save holder on the stack.
678 // Read the first word and compare to the global_context_map.
679 ldr(holder_reg, FieldMemOperand(scratch, HeapObject::kMapOffset));
680 cmp(holder_reg, Operand(Factory::global_context_map()));
681 Check(eq, "JSGlobalObject::global_context should be a global context.");
682 pop(holder_reg); // Restore holder.
683 }
684
685 // Check if both contexts are the same.
686 ldr(ip, FieldMemOperand(holder_reg, JSGlobalProxy::kContextOffset));
687 cmp(scratch, Operand(ip));
688 b(eq, &same_contexts);
689
690 // Check the context is a global context.
691 if (FLAG_debug_code) {
692 // TODO(119): avoid push(holder_reg)/pop(holder_reg)
693 // Cannot use ip as a temporary in this verification code. Due to the fact
694 // that ip is clobbered as part of cmp with an object Operand.
695 push(holder_reg); // Temporarily save holder on the stack.
696 mov(holder_reg, ip); // Move ip to its holding place.
697 cmp(holder_reg, Operand(Factory::null_value()));
698 Check(ne, "JSGlobalProxy::context() should not be null.");
699
700 ldr(holder_reg, FieldMemOperand(holder_reg, HeapObject::kMapOffset));
701 cmp(holder_reg, Operand(Factory::global_context_map()));
702 Check(eq, "JSGlobalObject::global_context should be a global context.");
703 // Restore ip is not needed. ip is reloaded below.
704 pop(holder_reg); // Restore holder.
705 // Restore ip to holder's context.
706 ldr(ip, FieldMemOperand(holder_reg, JSGlobalProxy::kContextOffset));
707 }
708
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000709 // Check that the security token in the calling global object is
710 // compatible with the security token in the receiving global
711 // object.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000712 int token_offset = Context::kHeaderSize +
713 Context::SECURITY_TOKEN_INDEX * kPointerSize;
714
715 ldr(scratch, FieldMemOperand(scratch, token_offset));
716 ldr(ip, FieldMemOperand(ip, token_offset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000717 cmp(scratch, Operand(ip));
718 b(ne, miss);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000719
720 bind(&same_contexts);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000721}
722
723
724void MacroAssembler::CallStub(CodeStub* stub) {
kasper.lund7276f142008-07-30 08:49:36 +0000725 ASSERT(allow_stub_calls()); // stub calls are not allowed in some stubs
ager@chromium.org236ad962008-09-25 09:45:57 +0000726 Call(stub->GetCode(), RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000727}
728
729
730void MacroAssembler::StubReturn(int argc) {
731 ASSERT(argc >= 1 && generating_stub());
732 if (argc > 1)
733 add(sp, sp, Operand((argc - 1) * kPointerSize));
734 Ret();
735}
736
mads.s.ager31e71382008-08-13 09:32:07 +0000737
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000738void MacroAssembler::IllegalOperation(int num_arguments) {
739 if (num_arguments > 0) {
740 add(sp, sp, Operand(num_arguments * kPointerSize));
741 }
742 mov(r0, Operand(Factory::undefined_value()));
743}
744
745
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000746void MacroAssembler::CallRuntime(Runtime::Function* f, int num_arguments) {
mads.s.ager31e71382008-08-13 09:32:07 +0000747 // All parameters are on the stack. r0 has the return value after call.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000748
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000749 // If the expected number of arguments of the runtime function is
750 // constant, we check that the actual number of arguments match the
751 // expectation.
752 if (f->nargs >= 0 && f->nargs != num_arguments) {
753 IllegalOperation(num_arguments);
754 return;
755 }
kasper.lund7276f142008-07-30 08:49:36 +0000756
mads.s.ager31e71382008-08-13 09:32:07 +0000757 Runtime::FunctionId function_id =
758 static_cast<Runtime::FunctionId>(f->stub_id);
759 RuntimeStub stub(function_id, num_arguments);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000760 CallStub(&stub);
761}
762
763
764void MacroAssembler::CallRuntime(Runtime::FunctionId fid, int num_arguments) {
765 CallRuntime(Runtime::FunctionForId(fid), num_arguments);
766}
767
768
mads.s.ager31e71382008-08-13 09:32:07 +0000769void MacroAssembler::TailCallRuntime(const ExternalReference& ext,
770 int num_arguments) {
771 // TODO(1236192): Most runtime routines don't need the number of
772 // arguments passed in because it is constant. At some point we
773 // should remove this need and make the runtime routine entry code
774 // smarter.
775 mov(r0, Operand(num_arguments));
776 JumpToBuiltin(ext);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000777}
778
779
780void MacroAssembler::JumpToBuiltin(const ExternalReference& builtin) {
781#if defined(__thumb__)
782 // Thumb mode builtin.
783 ASSERT((reinterpret_cast<intptr_t>(builtin.address()) & 1) == 1);
784#endif
785 mov(r1, Operand(builtin));
786 CEntryStub stub;
ager@chromium.org236ad962008-09-25 09:45:57 +0000787 Jump(stub.GetCode(), RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000788}
789
790
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000791Handle<Code> MacroAssembler::ResolveBuiltin(Builtins::JavaScript id,
792 bool* resolved) {
793 // Contract with compiled functions is that the function is passed in r1.
794 int builtins_offset =
795 JSBuiltinsObject::kJSBuiltinsOffset + (id * kPointerSize);
796 ldr(r1, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
797 ldr(r1, FieldMemOperand(r1, GlobalObject::kBuiltinsOffset));
798 ldr(r1, FieldMemOperand(r1, builtins_offset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000799
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000800 return Builtins::GetCode(id, resolved);
801}
802
803
804void MacroAssembler::InvokeBuiltin(Builtins::JavaScript id,
805 InvokeJSFlags flags) {
806 bool resolved;
807 Handle<Code> code = ResolveBuiltin(id, &resolved);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000808
809 if (flags == CALL_JS) {
ager@chromium.org236ad962008-09-25 09:45:57 +0000810 Call(code, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000811 } else {
812 ASSERT(flags == JUMP_JS);
ager@chromium.org236ad962008-09-25 09:45:57 +0000813 Jump(code, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000814 }
815
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000816 if (!resolved) {
817 const char* name = Builtins::GetName(id);
818 int argc = Builtins::GetArgumentsCount(id);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000819 uint32_t flags =
820 Bootstrapper::FixupFlagsArgumentsCount::encode(argc) |
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000821 Bootstrapper::FixupFlagsIsPCRelative::encode(true);
822 Unresolved entry = { pc_offset() - sizeof(Instr), flags, name };
823 unresolved_.Add(entry);
824 }
825}
826
827
828void MacroAssembler::GetBuiltinEntry(Register target, Builtins::JavaScript id) {
829 bool resolved;
830 Handle<Code> code = ResolveBuiltin(id, &resolved);
831
832 mov(target, Operand(code));
833 if (!resolved) {
834 const char* name = Builtins::GetName(id);
835 int argc = Builtins::GetArgumentsCount(id);
836 uint32_t flags =
837 Bootstrapper::FixupFlagsArgumentsCount::encode(argc) |
838 Bootstrapper::FixupFlagsIsPCRelative::encode(true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000839 Unresolved entry = { pc_offset() - sizeof(Instr), flags, name };
840 unresolved_.Add(entry);
841 }
842}
843
844
845void MacroAssembler::Assert(Condition cc, const char* msg) {
846 if (FLAG_debug_code)
847 Check(cc, msg);
848}
849
850
851void MacroAssembler::Check(Condition cc, const char* msg) {
852 Label L;
853 b(cc, &L);
854 Abort(msg);
855 // will not return here
856 bind(&L);
857}
858
859
860void MacroAssembler::Abort(const char* msg) {
861 // We want to pass the msg string like a smi to avoid GC
862 // problems, however msg is not guaranteed to be aligned
863 // properly. Instead, we pass an aligned pointer that is
864 // a proper v8 smi, but also pass the aligment difference
865 // from the real pointer as a smi.
866 intptr_t p1 = reinterpret_cast<intptr_t>(msg);
867 intptr_t p0 = (p1 & ~kSmiTagMask) + kSmiTag;
868 ASSERT(reinterpret_cast<Object*>(p0)->IsSmi());
869#ifdef DEBUG
870 if (msg != NULL) {
871 RecordComment("Abort message: ");
872 RecordComment(msg);
873 }
874#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000875 mov(r0, Operand(p0));
876 push(r0);
877 mov(r0, Operand(Smi::FromInt(p1 - p0)));
mads.s.ager31e71382008-08-13 09:32:07 +0000878 push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000879 CallRuntime(Runtime::kAbort, 2);
880 // will not return here
881}
882
883} } // namespace v8::internal