blob: 5e5b9c9fe1eba05c81cc5c88318e060cc0c074c8 [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
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000254void MacroAssembler::EnterInternalFrame() {
255 // r0-r3: preserved
256 int type = StackFrame::INTERNAL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000257
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000258 stm(db_w, sp, cp.bit() | fp.bit() | lr.bit());
259 mov(ip, Operand(Smi::FromInt(type)));
260 push(ip);
261 mov(ip, Operand(0));
262 push(ip); // Push an empty code cache slot.
263 add(fp, sp, Operand(3 * kPointerSize)); // Adjust FP to point to saved FP.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000264}
265
266
ager@chromium.org236ad962008-09-25 09:45:57 +0000267void MacroAssembler::LeaveInternalFrame() {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000268 // r0: preserved
269 // r1: preserved
270 // r2: preserved
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000271
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000272 // Drop the execution stack down to the frame pointer and restore the caller
273 // frame pointer and return address.
274 mov(sp, fp);
275 ldm(ia_w, sp, fp.bit() | lr.bit());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000276}
277
278
ager@chromium.org236ad962008-09-25 09:45:57 +0000279void MacroAssembler::EnterExitFrame(StackFrame::Type type) {
280 ASSERT(type == StackFrame::EXIT || type == StackFrame::EXIT_DEBUG);
281 // Compute parameter pointer before making changes and save it as ip
282 // register so that it is restored as sp register on exit, thereby
283 // popping the args.
284
285 // ip = sp + kPointerSize * #args;
286 add(ip, sp, Operand(r0, LSL, kPointerSizeLog2));
287
288 // Push in reverse order: caller_fp, sp_on_exit, and caller_pc.
289 stm(db_w, sp, fp.bit() | ip.bit() | lr.bit());
290 mov(fp, Operand(sp)); // setup new frame pointer
291
292 // Push debug marker.
293 mov(ip, Operand(type == StackFrame::EXIT_DEBUG ? 1 : 0));
294 push(ip);
295
296 // Save the frame pointer and the context in top.
297 mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address)));
298 str(fp, MemOperand(ip));
299 mov(ip, Operand(ExternalReference(Top::k_context_address)));
300 str(cp, MemOperand(ip));
301
302 // Setup argc and the builtin function in callee-saved registers.
303 mov(r4, Operand(r0));
304 mov(r5, Operand(r1));
305
306 // Compute the argv pointer and keep it in a callee-saved register.
307 add(r6, fp, Operand(r4, LSL, kPointerSizeLog2));
308 add(r6, r6, Operand(ExitFrameConstants::kPPDisplacement - kPointerSize));
309
310 // Save the state of all registers to the stack from the memory
311 // location. This is needed to allow nested break points.
312 if (type == StackFrame::EXIT_DEBUG) {
313 // Use sp as base to push.
314 CopyRegistersFromMemoryToStack(sp, kJSCallerSaved);
315 }
316}
317
318
319void MacroAssembler::LeaveExitFrame(StackFrame::Type type) {
320 // Restore the memory copy of the registers by digging them out from
321 // the stack. This is needed to allow nested break points.
322 if (type == StackFrame::EXIT_DEBUG) {
323 // This code intentionally clobbers r2 and r3.
324 const int kCallerSavedSize = kNumJSCallerSaved * kPointerSize;
325 const int kOffset = ExitFrameConstants::kDebugMarkOffset - kCallerSavedSize;
326 add(r3, fp, Operand(kOffset));
327 CopyRegistersFromStackToMemory(r3, r2, kJSCallerSaved);
328 }
329
330 // Clear top frame.
331 mov(r3, Operand(0));
332 mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address)));
333 str(r3, MemOperand(ip));
334
335 // Restore current context from top and clear it in debug mode.
336 mov(ip, Operand(ExternalReference(Top::k_context_address)));
337 ldr(cp, MemOperand(ip));
338 if (kDebug) {
339 str(r3, MemOperand(ip));
340 }
341
342 // Pop the arguments, restore registers, and return.
343 mov(sp, Operand(fp)); // respect ABI stack constraint
344 ldm(ia, sp, fp.bit() | sp.bit() | pc.bit());
345}
346
347
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000348void MacroAssembler::InvokePrologue(const ParameterCount& expected,
349 const ParameterCount& actual,
350 Handle<Code> code_constant,
351 Register code_reg,
352 Label* done,
353 InvokeFlag flag) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000354 bool definitely_matches = false;
355 Label regular_invoke;
356
357 // Check whether the expected and actual arguments count match. If not,
358 // setup registers according to contract with ArgumentsAdaptorTrampoline:
359 // r0: actual arguments count
360 // r1: function (passed through to callee)
361 // r2: expected arguments count
362 // r3: callee code entry
363
364 // The code below is made a lot easier because the calling code already sets
365 // up actual and expected registers according to the contract if values are
366 // passed in registers.
367 ASSERT(actual.is_immediate() || actual.reg().is(r0));
368 ASSERT(expected.is_immediate() || expected.reg().is(r2));
369 ASSERT((!code_constant.is_null() && code_reg.is(no_reg)) || code_reg.is(r3));
370
371 if (expected.is_immediate()) {
372 ASSERT(actual.is_immediate());
373 if (expected.immediate() == actual.immediate()) {
374 definitely_matches = true;
375 } else {
376 mov(r0, Operand(actual.immediate()));
377 const int sentinel = SharedFunctionInfo::kDontAdaptArgumentsSentinel;
378 if (expected.immediate() == sentinel) {
379 // Don't worry about adapting arguments for builtins that
380 // don't want that done. Skip adaption code by making it look
381 // like we have a match between expected and actual number of
382 // arguments.
383 definitely_matches = true;
384 } else {
385 mov(r2, Operand(expected.immediate()));
386 }
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000387 }
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000388 } else {
389 if (actual.is_immediate()) {
390 cmp(expected.reg(), Operand(actual.immediate()));
391 b(eq, &regular_invoke);
392 mov(r0, Operand(actual.immediate()));
393 } else {
394 cmp(expected.reg(), Operand(actual.reg()));
395 b(eq, &regular_invoke);
396 }
397 }
398
399 if (!definitely_matches) {
400 if (!code_constant.is_null()) {
401 mov(r3, Operand(code_constant));
402 add(r3, r3, Operand(Code::kHeaderSize - kHeapObjectTag));
403 }
404
405 Handle<Code> adaptor =
406 Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline));
407 if (flag == CALL_FUNCTION) {
ager@chromium.org236ad962008-09-25 09:45:57 +0000408 Call(adaptor, RelocInfo::CODE_TARGET);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000409 b(done);
410 } else {
ager@chromium.org236ad962008-09-25 09:45:57 +0000411 Jump(adaptor, RelocInfo::CODE_TARGET);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000412 }
413 bind(&regular_invoke);
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000414 }
415}
416
417
418void MacroAssembler::InvokeCode(Register code,
419 const ParameterCount& expected,
420 const ParameterCount& actual,
421 InvokeFlag flag) {
422 Label done;
423
424 InvokePrologue(expected, actual, Handle<Code>::null(), code, &done, flag);
425 if (flag == CALL_FUNCTION) {
426 Call(code);
427 } else {
428 ASSERT(flag == JUMP_FUNCTION);
429 Jump(code);
430 }
431
432 // Continue here if InvokePrologue does handle the invocation due to
433 // mismatched parameter counts.
434 bind(&done);
435}
436
437
438void MacroAssembler::InvokeCode(Handle<Code> code,
439 const ParameterCount& expected,
440 const ParameterCount& actual,
ager@chromium.org236ad962008-09-25 09:45:57 +0000441 RelocInfo::Mode rmode,
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000442 InvokeFlag flag) {
443 Label done;
444
445 InvokePrologue(expected, actual, code, no_reg, &done, flag);
446 if (flag == CALL_FUNCTION) {
447 Call(code, rmode);
448 } else {
449 Jump(code, rmode);
450 }
451
452 // Continue here if InvokePrologue does handle the invocation due to
453 // mismatched parameter counts.
454 bind(&done);
455}
456
457
458void MacroAssembler::InvokeFunction(Register fun,
459 const ParameterCount& actual,
460 InvokeFlag flag) {
461 // Contract with called JS functions requires that function is passed in r1.
462 ASSERT(fun.is(r1));
463
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000464 Register expected_reg = r2;
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000465 Register code_reg = r3;
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000466
467 ldr(code_reg, FieldMemOperand(r1, JSFunction::kSharedFunctionInfoOffset));
468 ldr(cp, FieldMemOperand(r1, JSFunction::kContextOffset));
469 ldr(expected_reg,
470 FieldMemOperand(code_reg,
471 SharedFunctionInfo::kFormalParameterCountOffset));
472 ldr(code_reg,
473 MemOperand(code_reg, SharedFunctionInfo::kCodeOffset - kHeapObjectTag));
474 add(code_reg, code_reg, Operand(Code::kHeaderSize - kHeapObjectTag));
475
476 ParameterCount expected(expected_reg);
477 InvokeCode(code_reg, expected, actual, flag);
478}
479
480
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000481void MacroAssembler::SaveRegistersToMemory(RegList regs) {
482 ASSERT((regs & ~kJSCallerSaved) == 0);
483 // Copy the content of registers to memory location.
484 for (int i = 0; i < kNumJSCallerSaved; i++) {
485 int r = JSCallerSavedCode(i);
486 if ((regs & (1 << r)) != 0) {
487 Register reg = { r };
488 mov(ip, Operand(ExternalReference(Debug_Address::Register(i))));
489 str(reg, MemOperand(ip));
490 }
491 }
492}
493
494
495void MacroAssembler::RestoreRegistersFromMemory(RegList regs) {
496 ASSERT((regs & ~kJSCallerSaved) == 0);
497 // Copy the content of memory location to registers.
498 for (int i = kNumJSCallerSaved; --i >= 0;) {
499 int r = JSCallerSavedCode(i);
500 if ((regs & (1 << r)) != 0) {
501 Register reg = { r };
502 mov(ip, Operand(ExternalReference(Debug_Address::Register(i))));
503 ldr(reg, MemOperand(ip));
504 }
505 }
506}
507
508
509void MacroAssembler::CopyRegistersFromMemoryToStack(Register base,
510 RegList regs) {
511 ASSERT((regs & ~kJSCallerSaved) == 0);
512 // Copy the content of the memory location to the stack and adjust base.
513 for (int i = kNumJSCallerSaved; --i >= 0;) {
514 int r = JSCallerSavedCode(i);
515 if ((regs & (1 << r)) != 0) {
516 mov(ip, Operand(ExternalReference(Debug_Address::Register(i))));
517 ldr(ip, MemOperand(ip));
518 str(ip, MemOperand(base, 4, NegPreIndex));
519 }
520 }
521}
522
523
524void MacroAssembler::CopyRegistersFromStackToMemory(Register base,
525 Register scratch,
526 RegList regs) {
527 ASSERT((regs & ~kJSCallerSaved) == 0);
528 // Copy the content of the stack to the memory location and adjust base.
529 for (int i = 0; i < kNumJSCallerSaved; i++) {
530 int r = JSCallerSavedCode(i);
531 if ((regs & (1 << r)) != 0) {
532 mov(ip, Operand(ExternalReference(Debug_Address::Register(i))));
533 ldr(scratch, MemOperand(base, 4, PostIndex));
534 str(scratch, MemOperand(ip));
535 }
536 }
537}
538
539
540void MacroAssembler::PushTryHandler(CodeLocation try_location,
541 HandlerType type) {
542 ASSERT(StackHandlerConstants::kSize == 6 * kPointerSize); // adjust this code
543 // The pc (return address) is passed in register lr.
544 if (try_location == IN_JAVASCRIPT) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000545 stm(db_w, sp, pp.bit() | fp.bit() | lr.bit());
546 if (type == TRY_CATCH_HANDLER) {
547 mov(r3, Operand(StackHandler::TRY_CATCH));
548 } else {
549 mov(r3, Operand(StackHandler::TRY_FINALLY));
550 }
551 push(r3); // state
552 mov(r3, Operand(ExternalReference(Top::k_handler_address)));
553 ldr(r1, MemOperand(r3));
554 push(r1); // next sp
555 str(sp, MemOperand(r3)); // chain handler
mads.s.ager31e71382008-08-13 09:32:07 +0000556 mov(r0, Operand(Smi::FromInt(StackHandler::kCodeNotPresent))); // new TOS
557 push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000558 } else {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000559 // Must preserve r0-r4, r5-r7 are available.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000560 ASSERT(try_location == IN_JS_ENTRY);
561 // The parameter pointer is meaningless here and fp does not point to a JS
562 // frame. So we save NULL for both pp and fp. We expect the code throwing an
563 // exception to check fp before dereferencing it to restore the context.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000564 mov(pp, Operand(0)); // set pp to NULL
565 mov(ip, Operand(0)); // to save a NULL fp
566 stm(db_w, sp, pp.bit() | ip.bit() | lr.bit());
567 mov(r6, Operand(StackHandler::ENTRY));
568 push(r6); // state
569 mov(r7, Operand(ExternalReference(Top::k_handler_address)));
570 ldr(r6, MemOperand(r7));
571 push(r6); // next sp
572 str(sp, MemOperand(r7)); // chain handler
mads.s.ager31e71382008-08-13 09:32:07 +0000573 mov(r5, Operand(Smi::FromInt(StackHandler::kCodeNotPresent))); // new TOS
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000574 push(r5); // flush TOS
575 }
576}
577
578
579Register MacroAssembler::CheckMaps(JSObject* object, Register object_reg,
580 JSObject* holder, Register holder_reg,
581 Register scratch,
582 Label* miss) {
583 // Make sure there's no overlap between scratch and the other
584 // registers.
585 ASSERT(!scratch.is(object_reg) && !scratch.is(holder_reg));
586
587 // Keep track of the current object in register reg.
588 Register reg = object_reg;
589 int depth = 1;
590
591 // Check the maps in the prototype chain.
592 // Traverse the prototype chain from the object and do map checks.
593 while (object != holder) {
594 depth++;
595
596 // Only global objects and objects that do not require access
597 // checks are allowed in stubs.
598 ASSERT(object->IsJSGlobalObject() || !object->IsAccessCheckNeeded());
599
600 // Get the map of the current object.
601 ldr(scratch, FieldMemOperand(reg, HeapObject::kMapOffset));
602 cmp(scratch, Operand(Handle<Map>(object->map())));
603
604 // Branch on the result of the map check.
605 b(ne, miss);
606
607 // Check access rights to the global object. This has to happen
608 // after the map check so that we know that the object is
609 // actually a global object.
610 if (object->IsJSGlobalObject()) {
611 CheckAccessGlobal(reg, scratch, miss);
612 // Restore scratch register to be the map of the object. In the
613 // new space case below, we load the prototype from the map in
614 // the scratch register.
615 ldr(scratch, FieldMemOperand(reg, HeapObject::kMapOffset));
616 }
617
618 reg = holder_reg; // from now the object is in holder_reg
619 JSObject* prototype = JSObject::cast(object->GetPrototype());
620 if (Heap::InNewSpace(prototype)) {
621 // The prototype is in new space; we cannot store a reference
622 // to it in the code. Load it from the map.
623 ldr(reg, FieldMemOperand(scratch, Map::kPrototypeOffset));
624 } else {
625 // The prototype is in old space; load it directly.
626 mov(reg, Operand(Handle<JSObject>(prototype)));
627 }
628
629 // Go to the next object in the prototype chain.
630 object = prototype;
631 }
632
633 // Check the holder map.
634 ldr(scratch, FieldMemOperand(reg, HeapObject::kMapOffset));
635 cmp(scratch, Operand(Handle<Map>(object->map())));
636 b(ne, miss);
637
638 // Log the check depth.
639 LOG(IntEvent("check-maps-depth", depth));
640
641 // Perform security check for access to the global object and return
642 // the holder register.
643 ASSERT(object == holder);
644 ASSERT(object->IsJSGlobalObject() || !object->IsAccessCheckNeeded());
645 if (object->IsJSGlobalObject()) {
646 CheckAccessGlobal(reg, scratch, miss);
647 }
648 return reg;
649}
650
651
652void MacroAssembler::CheckAccessGlobal(Register holder_reg,
653 Register scratch,
654 Label* miss) {
655 ASSERT(!holder_reg.is(scratch));
656
657 // Load the security context.
658 mov(scratch, Operand(Top::security_context_address()));
659 ldr(scratch, MemOperand(scratch));
660 // In debug mode, make sure the security context is set.
661 if (kDebug) {
662 cmp(scratch, Operand(0));
663 Check(ne, "we should not have an empty security context");
664 }
665
666 // Load the global object of the security context.
667 int offset = Context::kHeaderSize + Context::GLOBAL_INDEX * kPointerSize;
668 ldr(scratch, FieldMemOperand(scratch, offset));
669 // Check that the security token in the calling global object is
670 // compatible with the security token in the receiving global
671 // object.
672 ldr(scratch, FieldMemOperand(scratch, JSGlobalObject::kSecurityTokenOffset));
673 ldr(ip, FieldMemOperand(holder_reg, JSGlobalObject::kSecurityTokenOffset));
674 cmp(scratch, Operand(ip));
675 b(ne, miss);
676}
677
678
679void MacroAssembler::CallStub(CodeStub* stub) {
kasper.lund7276f142008-07-30 08:49:36 +0000680 ASSERT(allow_stub_calls()); // stub calls are not allowed in some stubs
ager@chromium.org236ad962008-09-25 09:45:57 +0000681 Call(stub->GetCode(), RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000682}
683
684
685void MacroAssembler::StubReturn(int argc) {
686 ASSERT(argc >= 1 && generating_stub());
687 if (argc > 1)
688 add(sp, sp, Operand((argc - 1) * kPointerSize));
689 Ret();
690}
691
mads.s.ager31e71382008-08-13 09:32:07 +0000692
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000693void MacroAssembler::CallRuntime(Runtime::Function* f, int num_arguments) {
mads.s.ager31e71382008-08-13 09:32:07 +0000694 // All parameters are on the stack. r0 has the return value after call.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000695
mads.s.ager31e71382008-08-13 09:32:07 +0000696 // Either the expected number of arguments is unknown, or the actual
697 // number of arguments match the expectation.
698 ASSERT(f->nargs < 0 || f->nargs == num_arguments);
kasper.lund7276f142008-07-30 08:49:36 +0000699
mads.s.ager31e71382008-08-13 09:32:07 +0000700 Runtime::FunctionId function_id =
701 static_cast<Runtime::FunctionId>(f->stub_id);
702 RuntimeStub stub(function_id, num_arguments);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000703 CallStub(&stub);
704}
705
706
707void MacroAssembler::CallRuntime(Runtime::FunctionId fid, int num_arguments) {
708 CallRuntime(Runtime::FunctionForId(fid), num_arguments);
709}
710
711
mads.s.ager31e71382008-08-13 09:32:07 +0000712void MacroAssembler::TailCallRuntime(const ExternalReference& ext,
713 int num_arguments) {
714 // TODO(1236192): Most runtime routines don't need the number of
715 // arguments passed in because it is constant. At some point we
716 // should remove this need and make the runtime routine entry code
717 // smarter.
718 mov(r0, Operand(num_arguments));
719 JumpToBuiltin(ext);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000720}
721
722
723void MacroAssembler::JumpToBuiltin(const ExternalReference& builtin) {
724#if defined(__thumb__)
725 // Thumb mode builtin.
726 ASSERT((reinterpret_cast<intptr_t>(builtin.address()) & 1) == 1);
727#endif
728 mov(r1, Operand(builtin));
729 CEntryStub stub;
ager@chromium.org236ad962008-09-25 09:45:57 +0000730 Jump(stub.GetCode(), RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000731}
732
733
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000734Handle<Code> MacroAssembler::ResolveBuiltin(Builtins::JavaScript id,
735 bool* resolved) {
736 // Contract with compiled functions is that the function is passed in r1.
737 int builtins_offset =
738 JSBuiltinsObject::kJSBuiltinsOffset + (id * kPointerSize);
739 ldr(r1, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
740 ldr(r1, FieldMemOperand(r1, GlobalObject::kBuiltinsOffset));
741 ldr(r1, FieldMemOperand(r1, builtins_offset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000742
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000743 return Builtins::GetCode(id, resolved);
744}
745
746
747void MacroAssembler::InvokeBuiltin(Builtins::JavaScript id,
748 InvokeJSFlags flags) {
749 bool resolved;
750 Handle<Code> code = ResolveBuiltin(id, &resolved);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000751
752 if (flags == CALL_JS) {
ager@chromium.org236ad962008-09-25 09:45:57 +0000753 Call(code, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000754 } else {
755 ASSERT(flags == JUMP_JS);
ager@chromium.org236ad962008-09-25 09:45:57 +0000756 Jump(code, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000757 }
758
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000759 if (!resolved) {
760 const char* name = Builtins::GetName(id);
761 int argc = Builtins::GetArgumentsCount(id);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000762 uint32_t flags =
763 Bootstrapper::FixupFlagsArgumentsCount::encode(argc) |
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000764 Bootstrapper::FixupFlagsIsPCRelative::encode(true);
765 Unresolved entry = { pc_offset() - sizeof(Instr), flags, name };
766 unresolved_.Add(entry);
767 }
768}
769
770
771void MacroAssembler::GetBuiltinEntry(Register target, Builtins::JavaScript id) {
772 bool resolved;
773 Handle<Code> code = ResolveBuiltin(id, &resolved);
774
775 mov(target, Operand(code));
776 if (!resolved) {
777 const char* name = Builtins::GetName(id);
778 int argc = Builtins::GetArgumentsCount(id);
779 uint32_t flags =
780 Bootstrapper::FixupFlagsArgumentsCount::encode(argc) |
781 Bootstrapper::FixupFlagsIsPCRelative::encode(true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000782 Unresolved entry = { pc_offset() - sizeof(Instr), flags, name };
783 unresolved_.Add(entry);
784 }
785}
786
787
788void MacroAssembler::Assert(Condition cc, const char* msg) {
789 if (FLAG_debug_code)
790 Check(cc, msg);
791}
792
793
794void MacroAssembler::Check(Condition cc, const char* msg) {
795 Label L;
796 b(cc, &L);
797 Abort(msg);
798 // will not return here
799 bind(&L);
800}
801
802
803void MacroAssembler::Abort(const char* msg) {
804 // We want to pass the msg string like a smi to avoid GC
805 // problems, however msg is not guaranteed to be aligned
806 // properly. Instead, we pass an aligned pointer that is
807 // a proper v8 smi, but also pass the aligment difference
808 // from the real pointer as a smi.
809 intptr_t p1 = reinterpret_cast<intptr_t>(msg);
810 intptr_t p0 = (p1 & ~kSmiTagMask) + kSmiTag;
811 ASSERT(reinterpret_cast<Object*>(p0)->IsSmi());
812#ifdef DEBUG
813 if (msg != NULL) {
814 RecordComment("Abort message: ");
815 RecordComment(msg);
816 }
817#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000818 mov(r0, Operand(p0));
819 push(r0);
820 mov(r0, Operand(Smi::FromInt(p1 - p0)));
mads.s.ager31e71382008-08-13 09:32:07 +0000821 push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000822 CallRuntime(Runtime::kAbort, 2);
823 // will not return here
824}
825
826} } // namespace v8::internal