blob: c7139f4b2a5d758a44c709e5e91234011fbc7256 [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.
596 ASSERT(object->IsJSGlobalObject() || !object->IsAccessCheckNeeded());
597
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.
608 if (object->IsJSGlobalObject()) {
609 CheckAccessGlobal(reg, scratch, miss);
610 // 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);
642 ASSERT(object->IsJSGlobalObject() || !object->IsAccessCheckNeeded());
643 if (object->IsJSGlobalObject()) {
644 CheckAccessGlobal(reg, scratch, miss);
645 }
646 return reg;
647}
648
649
650void MacroAssembler::CheckAccessGlobal(Register holder_reg,
651 Register scratch,
652 Label* miss) {
653 ASSERT(!holder_reg.is(scratch));
654
655 // Load the security context.
656 mov(scratch, Operand(Top::security_context_address()));
657 ldr(scratch, MemOperand(scratch));
658 // In debug mode, make sure the security context is set.
659 if (kDebug) {
660 cmp(scratch, Operand(0));
661 Check(ne, "we should not have an empty security context");
662 }
663
664 // Load the global object of the security context.
665 int offset = Context::kHeaderSize + Context::GLOBAL_INDEX * kPointerSize;
666 ldr(scratch, FieldMemOperand(scratch, offset));
667 // Check that the security token in the calling global object is
668 // compatible with the security token in the receiving global
669 // object.
670 ldr(scratch, FieldMemOperand(scratch, JSGlobalObject::kSecurityTokenOffset));
671 ldr(ip, FieldMemOperand(holder_reg, JSGlobalObject::kSecurityTokenOffset));
672 cmp(scratch, Operand(ip));
673 b(ne, miss);
674}
675
676
677void MacroAssembler::CallStub(CodeStub* stub) {
kasper.lund7276f142008-07-30 08:49:36 +0000678 ASSERT(allow_stub_calls()); // stub calls are not allowed in some stubs
ager@chromium.org236ad962008-09-25 09:45:57 +0000679 Call(stub->GetCode(), RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000680}
681
682
683void MacroAssembler::StubReturn(int argc) {
684 ASSERT(argc >= 1 && generating_stub());
685 if (argc > 1)
686 add(sp, sp, Operand((argc - 1) * kPointerSize));
687 Ret();
688}
689
mads.s.ager31e71382008-08-13 09:32:07 +0000690
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000691void MacroAssembler::IllegalOperation(int num_arguments) {
692 if (num_arguments > 0) {
693 add(sp, sp, Operand(num_arguments * kPointerSize));
694 }
695 mov(r0, Operand(Factory::undefined_value()));
696}
697
698
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000699void MacroAssembler::CallRuntime(Runtime::Function* f, int num_arguments) {
mads.s.ager31e71382008-08-13 09:32:07 +0000700 // All parameters are on the stack. r0 has the return value after call.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000701
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000702 // If the expected number of arguments of the runtime function is
703 // constant, we check that the actual number of arguments match the
704 // expectation.
705 if (f->nargs >= 0 && f->nargs != num_arguments) {
706 IllegalOperation(num_arguments);
707 return;
708 }
kasper.lund7276f142008-07-30 08:49:36 +0000709
mads.s.ager31e71382008-08-13 09:32:07 +0000710 Runtime::FunctionId function_id =
711 static_cast<Runtime::FunctionId>(f->stub_id);
712 RuntimeStub stub(function_id, num_arguments);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000713 CallStub(&stub);
714}
715
716
717void MacroAssembler::CallRuntime(Runtime::FunctionId fid, int num_arguments) {
718 CallRuntime(Runtime::FunctionForId(fid), num_arguments);
719}
720
721
mads.s.ager31e71382008-08-13 09:32:07 +0000722void MacroAssembler::TailCallRuntime(const ExternalReference& ext,
723 int num_arguments) {
724 // TODO(1236192): Most runtime routines don't need the number of
725 // arguments passed in because it is constant. At some point we
726 // should remove this need and make the runtime routine entry code
727 // smarter.
728 mov(r0, Operand(num_arguments));
729 JumpToBuiltin(ext);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000730}
731
732
733void MacroAssembler::JumpToBuiltin(const ExternalReference& builtin) {
734#if defined(__thumb__)
735 // Thumb mode builtin.
736 ASSERT((reinterpret_cast<intptr_t>(builtin.address()) & 1) == 1);
737#endif
738 mov(r1, Operand(builtin));
739 CEntryStub stub;
ager@chromium.org236ad962008-09-25 09:45:57 +0000740 Jump(stub.GetCode(), RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000741}
742
743
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000744Handle<Code> MacroAssembler::ResolveBuiltin(Builtins::JavaScript id,
745 bool* resolved) {
746 // Contract with compiled functions is that the function is passed in r1.
747 int builtins_offset =
748 JSBuiltinsObject::kJSBuiltinsOffset + (id * kPointerSize);
749 ldr(r1, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
750 ldr(r1, FieldMemOperand(r1, GlobalObject::kBuiltinsOffset));
751 ldr(r1, FieldMemOperand(r1, builtins_offset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000752
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000753 return Builtins::GetCode(id, resolved);
754}
755
756
757void MacroAssembler::InvokeBuiltin(Builtins::JavaScript id,
758 InvokeJSFlags flags) {
759 bool resolved;
760 Handle<Code> code = ResolveBuiltin(id, &resolved);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000761
762 if (flags == CALL_JS) {
ager@chromium.org236ad962008-09-25 09:45:57 +0000763 Call(code, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000764 } else {
765 ASSERT(flags == JUMP_JS);
ager@chromium.org236ad962008-09-25 09:45:57 +0000766 Jump(code, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000767 }
768
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000769 if (!resolved) {
770 const char* name = Builtins::GetName(id);
771 int argc = Builtins::GetArgumentsCount(id);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000772 uint32_t flags =
773 Bootstrapper::FixupFlagsArgumentsCount::encode(argc) |
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000774 Bootstrapper::FixupFlagsIsPCRelative::encode(true);
775 Unresolved entry = { pc_offset() - sizeof(Instr), flags, name };
776 unresolved_.Add(entry);
777 }
778}
779
780
781void MacroAssembler::GetBuiltinEntry(Register target, Builtins::JavaScript id) {
782 bool resolved;
783 Handle<Code> code = ResolveBuiltin(id, &resolved);
784
785 mov(target, Operand(code));
786 if (!resolved) {
787 const char* name = Builtins::GetName(id);
788 int argc = Builtins::GetArgumentsCount(id);
789 uint32_t flags =
790 Bootstrapper::FixupFlagsArgumentsCount::encode(argc) |
791 Bootstrapper::FixupFlagsIsPCRelative::encode(true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000792 Unresolved entry = { pc_offset() - sizeof(Instr), flags, name };
793 unresolved_.Add(entry);
794 }
795}
796
797
798void MacroAssembler::Assert(Condition cc, const char* msg) {
799 if (FLAG_debug_code)
800 Check(cc, msg);
801}
802
803
804void MacroAssembler::Check(Condition cc, const char* msg) {
805 Label L;
806 b(cc, &L);
807 Abort(msg);
808 // will not return here
809 bind(&L);
810}
811
812
813void MacroAssembler::Abort(const char* msg) {
814 // We want to pass the msg string like a smi to avoid GC
815 // problems, however msg is not guaranteed to be aligned
816 // properly. Instead, we pass an aligned pointer that is
817 // a proper v8 smi, but also pass the aligment difference
818 // from the real pointer as a smi.
819 intptr_t p1 = reinterpret_cast<intptr_t>(msg);
820 intptr_t p0 = (p1 & ~kSmiTagMask) + kSmiTag;
821 ASSERT(reinterpret_cast<Object*>(p0)->IsSmi());
822#ifdef DEBUG
823 if (msg != NULL) {
824 RecordComment("Abort message: ");
825 RecordComment(msg);
826 }
827#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000828 mov(r0, Operand(p0));
829 push(r0);
830 mov(r0, Operand(Smi::FromInt(p1 - p0)));
mads.s.ager31e71382008-08-13 09:32:07 +0000831 push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000832 CallRuntime(Runtime::kAbort, 2);
833 // will not return here
834}
835
836} } // namespace v8::internal