blob: 5cbeae9124c3a01c5aa2e16366b1c505cb7a6cc0 [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
ager@chromium.org8bb60582008-12-11 12:02:20 +0000179void MacroAssembler::SmiJumpTable(Register index, Vector<Label*> targets) {
180 // Empty the const pool.
181 CheckConstPool(true, true);
182 add(pc, pc, Operand(index,
183 LSL,
184 assembler::arm::Instr::kInstrSizeLog2 - kSmiTagSize));
185 BlockConstPoolBefore(pc_offset() + (targets.length() + 1) * sizeof(Instr));
186 nop(); // Jump table alignment.
187 for (int i = 0; i < targets.length(); i++) {
188 b(targets[i]);
189 }
190}
191
192
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000193// Will clobber 4 registers: object, offset, scratch, ip. The
194// register 'object' contains a heap object pointer. The heap object
195// tag is shifted away.
196void MacroAssembler::RecordWrite(Register object, Register offset,
197 Register scratch) {
198 // This is how much we shift the remembered set bit offset to get the
199 // offset of the word in the remembered set. We divide by kBitsPerInt (32,
200 // shift right 5) and then multiply by kIntSize (4, shift left 2).
201 const int kRSetWordShift = 3;
202
203 Label fast, done;
204
kasper.lund7276f142008-07-30 08:49:36 +0000205 // First, test that the object is not in the new space. We cannot set
206 // remembered set bits in the new space.
207 // object: heap object pointer (with tag)
208 // offset: offset to store location from the object
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000209 and_(scratch, object, Operand(Heap::NewSpaceMask()));
210 cmp(scratch, Operand(ExternalReference::new_space_start()));
211 b(eq, &done);
212
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000213 // Compute the bit offset in the remembered set.
kasper.lund7276f142008-07-30 08:49:36 +0000214 // object: heap object pointer (with tag)
215 // offset: offset to store location from the object
216 mov(ip, Operand(Page::kPageAlignmentMask)); // load mask only once
217 and_(scratch, object, Operand(ip)); // offset into page of the object
218 add(offset, scratch, Operand(offset)); // add offset into the object
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000219 mov(offset, Operand(offset, LSR, kObjectAlignmentBits));
220
221 // Compute the page address from the heap object pointer.
kasper.lund7276f142008-07-30 08:49:36 +0000222 // object: heap object pointer (with tag)
223 // offset: bit offset of store position in the remembered set
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000224 bic(object, object, Operand(ip));
225
226 // If the bit offset lies beyond the normal remembered set range, it is in
227 // the extra remembered set area of a large object.
kasper.lund7276f142008-07-30 08:49:36 +0000228 // object: page start
229 // offset: bit offset of store position in the remembered set
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000230 cmp(offset, Operand(Page::kPageSize / kPointerSize));
231 b(lt, &fast);
232
233 // Adjust the bit offset to be relative to the start of the extra
234 // remembered set and the start address to be the address of the extra
235 // remembered set.
236 sub(offset, offset, Operand(Page::kPageSize / kPointerSize));
237 // Load the array length into 'scratch' and multiply by four to get the
238 // size in bytes of the elements.
239 ldr(scratch, MemOperand(object, Page::kObjectStartOffset
240 + FixedArray::kLengthOffset));
241 mov(scratch, Operand(scratch, LSL, kObjectAlignmentBits));
242 // Add the page header (including remembered set), array header, and array
243 // body size to the page address.
244 add(object, object, Operand(Page::kObjectStartOffset
245 + Array::kHeaderSize));
246 add(object, object, Operand(scratch));
247
248 bind(&fast);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000249 // Get address of the rset word.
kasper.lund7276f142008-07-30 08:49:36 +0000250 // object: start of the remembered set (page start for the fast case)
251 // offset: bit offset of store position in the remembered set
252 bic(scratch, offset, Operand(kBitsPerInt - 1)); // clear the bit offset
253 add(object, object, Operand(scratch, LSR, kRSetWordShift));
254 // Get bit offset in the rset word.
255 // object: address of remembered set word
256 // offset: bit offset of store position
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000257 and_(offset, offset, Operand(kBitsPerInt - 1));
258
259 ldr(scratch, MemOperand(object));
260 mov(ip, Operand(1));
261 orr(scratch, scratch, Operand(ip, LSL, offset));
262 str(scratch, MemOperand(object));
263
264 bind(&done);
265}
266
267
ager@chromium.org7c537e22008-10-16 08:43:32 +0000268void MacroAssembler::EnterFrame(StackFrame::Type type) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000269 // r0-r3: preserved
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000270 stm(db_w, sp, cp.bit() | fp.bit() | lr.bit());
271 mov(ip, Operand(Smi::FromInt(type)));
272 push(ip);
273 mov(ip, Operand(0));
274 push(ip); // Push an empty code cache slot.
275 add(fp, sp, Operand(3 * kPointerSize)); // Adjust FP to point to saved FP.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000276}
277
278
ager@chromium.org7c537e22008-10-16 08:43:32 +0000279void MacroAssembler::LeaveFrame(StackFrame::Type type) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000280 // r0: preserved
281 // r1: preserved
282 // r2: preserved
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000283
ager@chromium.org7c537e22008-10-16 08:43:32 +0000284 // Drop the execution stack down to the frame pointer and restore
285 // the caller frame pointer and return address.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000286 mov(sp, fp);
287 ldm(ia_w, sp, fp.bit() | lr.bit());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000288}
289
290
ager@chromium.org236ad962008-09-25 09:45:57 +0000291void MacroAssembler::EnterExitFrame(StackFrame::Type type) {
292 ASSERT(type == StackFrame::EXIT || type == StackFrame::EXIT_DEBUG);
293 // Compute parameter pointer before making changes and save it as ip
294 // register so that it is restored as sp register on exit, thereby
295 // popping the args.
296
297 // ip = sp + kPointerSize * #args;
298 add(ip, sp, Operand(r0, LSL, kPointerSizeLog2));
299
300 // Push in reverse order: caller_fp, sp_on_exit, and caller_pc.
301 stm(db_w, sp, fp.bit() | ip.bit() | lr.bit());
302 mov(fp, Operand(sp)); // setup new frame pointer
303
304 // Push debug marker.
305 mov(ip, Operand(type == StackFrame::EXIT_DEBUG ? 1 : 0));
306 push(ip);
307
308 // Save the frame pointer and the context in top.
309 mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address)));
310 str(fp, MemOperand(ip));
311 mov(ip, Operand(ExternalReference(Top::k_context_address)));
312 str(cp, MemOperand(ip));
313
314 // Setup argc and the builtin function in callee-saved registers.
315 mov(r4, Operand(r0));
316 mov(r5, Operand(r1));
317
318 // Compute the argv pointer and keep it in a callee-saved register.
319 add(r6, fp, Operand(r4, LSL, kPointerSizeLog2));
320 add(r6, r6, Operand(ExitFrameConstants::kPPDisplacement - kPointerSize));
321
322 // Save the state of all registers to the stack from the memory
323 // location. This is needed to allow nested break points.
324 if (type == StackFrame::EXIT_DEBUG) {
325 // Use sp as base to push.
326 CopyRegistersFromMemoryToStack(sp, kJSCallerSaved);
327 }
328}
329
330
331void MacroAssembler::LeaveExitFrame(StackFrame::Type type) {
332 // Restore the memory copy of the registers by digging them out from
333 // the stack. This is needed to allow nested break points.
334 if (type == StackFrame::EXIT_DEBUG) {
335 // This code intentionally clobbers r2 and r3.
336 const int kCallerSavedSize = kNumJSCallerSaved * kPointerSize;
337 const int kOffset = ExitFrameConstants::kDebugMarkOffset - kCallerSavedSize;
338 add(r3, fp, Operand(kOffset));
339 CopyRegistersFromStackToMemory(r3, r2, kJSCallerSaved);
340 }
341
342 // Clear top frame.
343 mov(r3, Operand(0));
344 mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address)));
345 str(r3, MemOperand(ip));
346
347 // Restore current context from top and clear it in debug mode.
348 mov(ip, Operand(ExternalReference(Top::k_context_address)));
349 ldr(cp, MemOperand(ip));
350 if (kDebug) {
351 str(r3, MemOperand(ip));
352 }
353
354 // Pop the arguments, restore registers, and return.
355 mov(sp, Operand(fp)); // respect ABI stack constraint
356 ldm(ia, sp, fp.bit() | sp.bit() | pc.bit());
357}
358
359
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000360void MacroAssembler::InvokePrologue(const ParameterCount& expected,
361 const ParameterCount& actual,
362 Handle<Code> code_constant,
363 Register code_reg,
364 Label* done,
365 InvokeFlag flag) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000366 bool definitely_matches = false;
367 Label regular_invoke;
368
369 // Check whether the expected and actual arguments count match. If not,
370 // setup registers according to contract with ArgumentsAdaptorTrampoline:
371 // r0: actual arguments count
372 // r1: function (passed through to callee)
373 // r2: expected arguments count
374 // r3: callee code entry
375
376 // The code below is made a lot easier because the calling code already sets
377 // up actual and expected registers according to the contract if values are
378 // passed in registers.
379 ASSERT(actual.is_immediate() || actual.reg().is(r0));
380 ASSERT(expected.is_immediate() || expected.reg().is(r2));
381 ASSERT((!code_constant.is_null() && code_reg.is(no_reg)) || code_reg.is(r3));
382
383 if (expected.is_immediate()) {
384 ASSERT(actual.is_immediate());
385 if (expected.immediate() == actual.immediate()) {
386 definitely_matches = true;
387 } else {
388 mov(r0, Operand(actual.immediate()));
389 const int sentinel = SharedFunctionInfo::kDontAdaptArgumentsSentinel;
390 if (expected.immediate() == sentinel) {
391 // Don't worry about adapting arguments for builtins that
392 // don't want that done. Skip adaption code by making it look
393 // like we have a match between expected and actual number of
394 // arguments.
395 definitely_matches = true;
396 } else {
397 mov(r2, Operand(expected.immediate()));
398 }
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000399 }
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000400 } else {
401 if (actual.is_immediate()) {
402 cmp(expected.reg(), Operand(actual.immediate()));
403 b(eq, &regular_invoke);
404 mov(r0, Operand(actual.immediate()));
405 } else {
406 cmp(expected.reg(), Operand(actual.reg()));
407 b(eq, &regular_invoke);
408 }
409 }
410
411 if (!definitely_matches) {
412 if (!code_constant.is_null()) {
413 mov(r3, Operand(code_constant));
414 add(r3, r3, Operand(Code::kHeaderSize - kHeapObjectTag));
415 }
416
417 Handle<Code> adaptor =
418 Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline));
419 if (flag == CALL_FUNCTION) {
ager@chromium.org236ad962008-09-25 09:45:57 +0000420 Call(adaptor, RelocInfo::CODE_TARGET);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000421 b(done);
422 } else {
ager@chromium.org236ad962008-09-25 09:45:57 +0000423 Jump(adaptor, RelocInfo::CODE_TARGET);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000424 }
425 bind(&regular_invoke);
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000426 }
427}
428
429
430void MacroAssembler::InvokeCode(Register code,
431 const ParameterCount& expected,
432 const ParameterCount& actual,
433 InvokeFlag flag) {
434 Label done;
435
436 InvokePrologue(expected, actual, Handle<Code>::null(), code, &done, flag);
437 if (flag == CALL_FUNCTION) {
438 Call(code);
439 } else {
440 ASSERT(flag == JUMP_FUNCTION);
441 Jump(code);
442 }
443
444 // Continue here if InvokePrologue does handle the invocation due to
445 // mismatched parameter counts.
446 bind(&done);
447}
448
449
450void MacroAssembler::InvokeCode(Handle<Code> code,
451 const ParameterCount& expected,
452 const ParameterCount& actual,
ager@chromium.org236ad962008-09-25 09:45:57 +0000453 RelocInfo::Mode rmode,
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000454 InvokeFlag flag) {
455 Label done;
456
457 InvokePrologue(expected, actual, code, no_reg, &done, flag);
458 if (flag == CALL_FUNCTION) {
459 Call(code, rmode);
460 } else {
461 Jump(code, rmode);
462 }
463
464 // Continue here if InvokePrologue does handle the invocation due to
465 // mismatched parameter counts.
466 bind(&done);
467}
468
469
470void MacroAssembler::InvokeFunction(Register fun,
471 const ParameterCount& actual,
472 InvokeFlag flag) {
473 // Contract with called JS functions requires that function is passed in r1.
474 ASSERT(fun.is(r1));
475
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000476 Register expected_reg = r2;
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000477 Register code_reg = r3;
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000478
479 ldr(code_reg, FieldMemOperand(r1, JSFunction::kSharedFunctionInfoOffset));
480 ldr(cp, FieldMemOperand(r1, JSFunction::kContextOffset));
481 ldr(expected_reg,
482 FieldMemOperand(code_reg,
483 SharedFunctionInfo::kFormalParameterCountOffset));
484 ldr(code_reg,
485 MemOperand(code_reg, SharedFunctionInfo::kCodeOffset - kHeapObjectTag));
486 add(code_reg, code_reg, Operand(Code::kHeaderSize - kHeapObjectTag));
487
488 ParameterCount expected(expected_reg);
489 InvokeCode(code_reg, expected, actual, flag);
490}
491
492
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000493void MacroAssembler::SaveRegistersToMemory(RegList regs) {
494 ASSERT((regs & ~kJSCallerSaved) == 0);
495 // Copy the content of registers to memory location.
496 for (int i = 0; i < kNumJSCallerSaved; i++) {
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 str(reg, MemOperand(ip));
502 }
503 }
504}
505
506
507void MacroAssembler::RestoreRegistersFromMemory(RegList regs) {
508 ASSERT((regs & ~kJSCallerSaved) == 0);
509 // Copy the content of memory location to registers.
510 for (int i = kNumJSCallerSaved; --i >= 0;) {
511 int r = JSCallerSavedCode(i);
512 if ((regs & (1 << r)) != 0) {
513 Register reg = { r };
514 mov(ip, Operand(ExternalReference(Debug_Address::Register(i))));
515 ldr(reg, MemOperand(ip));
516 }
517 }
518}
519
520
521void MacroAssembler::CopyRegistersFromMemoryToStack(Register base,
522 RegList regs) {
523 ASSERT((regs & ~kJSCallerSaved) == 0);
524 // Copy the content of the memory location to the stack and adjust base.
525 for (int i = kNumJSCallerSaved; --i >= 0;) {
526 int r = JSCallerSavedCode(i);
527 if ((regs & (1 << r)) != 0) {
528 mov(ip, Operand(ExternalReference(Debug_Address::Register(i))));
529 ldr(ip, MemOperand(ip));
530 str(ip, MemOperand(base, 4, NegPreIndex));
531 }
532 }
533}
534
535
536void MacroAssembler::CopyRegistersFromStackToMemory(Register base,
537 Register scratch,
538 RegList regs) {
539 ASSERT((regs & ~kJSCallerSaved) == 0);
540 // Copy the content of the stack to the memory location and adjust base.
541 for (int i = 0; i < kNumJSCallerSaved; i++) {
542 int r = JSCallerSavedCode(i);
543 if ((regs & (1 << r)) != 0) {
544 mov(ip, Operand(ExternalReference(Debug_Address::Register(i))));
545 ldr(scratch, MemOperand(base, 4, PostIndex));
546 str(scratch, MemOperand(ip));
547 }
548 }
549}
550
551
552void MacroAssembler::PushTryHandler(CodeLocation try_location,
553 HandlerType type) {
554 ASSERT(StackHandlerConstants::kSize == 6 * kPointerSize); // adjust this code
555 // The pc (return address) is passed in register lr.
556 if (try_location == IN_JAVASCRIPT) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000557 stm(db_w, sp, pp.bit() | fp.bit() | lr.bit());
558 if (type == TRY_CATCH_HANDLER) {
559 mov(r3, Operand(StackHandler::TRY_CATCH));
560 } else {
561 mov(r3, Operand(StackHandler::TRY_FINALLY));
562 }
563 push(r3); // state
564 mov(r3, Operand(ExternalReference(Top::k_handler_address)));
565 ldr(r1, MemOperand(r3));
566 push(r1); // next sp
567 str(sp, MemOperand(r3)); // chain handler
mads.s.ager31e71382008-08-13 09:32:07 +0000568 mov(r0, Operand(Smi::FromInt(StackHandler::kCodeNotPresent))); // new TOS
569 push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000570 } else {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000571 // Must preserve r0-r4, r5-r7 are available.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000572 ASSERT(try_location == IN_JS_ENTRY);
573 // The parameter pointer is meaningless here and fp does not point to a JS
574 // frame. So we save NULL for both pp and fp. We expect the code throwing an
575 // exception to check fp before dereferencing it to restore the context.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000576 mov(pp, Operand(0)); // set pp to NULL
577 mov(ip, Operand(0)); // to save a NULL fp
578 stm(db_w, sp, pp.bit() | ip.bit() | lr.bit());
579 mov(r6, Operand(StackHandler::ENTRY));
580 push(r6); // state
581 mov(r7, Operand(ExternalReference(Top::k_handler_address)));
582 ldr(r6, MemOperand(r7));
583 push(r6); // next sp
584 str(sp, MemOperand(r7)); // chain handler
mads.s.ager31e71382008-08-13 09:32:07 +0000585 mov(r5, Operand(Smi::FromInt(StackHandler::kCodeNotPresent))); // new TOS
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000586 push(r5); // flush TOS
587 }
588}
589
590
591Register MacroAssembler::CheckMaps(JSObject* object, Register object_reg,
592 JSObject* holder, Register holder_reg,
593 Register scratch,
594 Label* miss) {
595 // Make sure there's no overlap between scratch and the other
596 // registers.
597 ASSERT(!scratch.is(object_reg) && !scratch.is(holder_reg));
598
599 // Keep track of the current object in register reg.
600 Register reg = object_reg;
601 int depth = 1;
602
603 // Check the maps in the prototype chain.
604 // Traverse the prototype chain from the object and do map checks.
605 while (object != holder) {
606 depth++;
607
608 // Only global objects and objects that do not require access
609 // checks are allowed in stubs.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000610 ASSERT(object->IsJSGlobalProxy() || !object->IsAccessCheckNeeded());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000611
612 // Get the map of the current object.
613 ldr(scratch, FieldMemOperand(reg, HeapObject::kMapOffset));
614 cmp(scratch, Operand(Handle<Map>(object->map())));
615
616 // Branch on the result of the map check.
617 b(ne, miss);
618
619 // Check access rights to the global object. This has to happen
620 // after the map check so that we know that the object is
621 // actually a global object.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000622 if (object->IsJSGlobalProxy()) {
623 CheckAccessGlobalProxy(reg, scratch, miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000624 // Restore scratch register to be the map of the object. In the
625 // new space case below, we load the prototype from the map in
626 // the scratch register.
627 ldr(scratch, FieldMemOperand(reg, HeapObject::kMapOffset));
628 }
629
630 reg = holder_reg; // from now the object is in holder_reg
631 JSObject* prototype = JSObject::cast(object->GetPrototype());
632 if (Heap::InNewSpace(prototype)) {
633 // The prototype is in new space; we cannot store a reference
634 // to it in the code. Load it from the map.
635 ldr(reg, FieldMemOperand(scratch, Map::kPrototypeOffset));
636 } else {
637 // The prototype is in old space; load it directly.
638 mov(reg, Operand(Handle<JSObject>(prototype)));
639 }
640
641 // Go to the next object in the prototype chain.
642 object = prototype;
643 }
644
645 // Check the holder map.
646 ldr(scratch, FieldMemOperand(reg, HeapObject::kMapOffset));
647 cmp(scratch, Operand(Handle<Map>(object->map())));
648 b(ne, miss);
649
650 // Log the check depth.
651 LOG(IntEvent("check-maps-depth", depth));
652
653 // Perform security check for access to the global object and return
654 // the holder register.
655 ASSERT(object == holder);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000656 ASSERT(object->IsJSGlobalProxy() || !object->IsAccessCheckNeeded());
657 if (object->IsJSGlobalProxy()) {
658 CheckAccessGlobalProxy(reg, scratch, miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000659 }
660 return reg;
661}
662
663
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000664void MacroAssembler::CheckAccessGlobalProxy(Register holder_reg,
665 Register scratch,
666 Label* miss) {
667 Label same_contexts;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000668
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000669 ASSERT(!holder_reg.is(scratch));
670 ASSERT(!holder_reg.is(ip));
671 ASSERT(!scratch.is(ip));
672
673 // Load current lexical context from the stack frame.
674 ldr(scratch, MemOperand(fp, StandardFrameConstants::kContextOffset));
675 // In debug mode, make sure the lexical context is set.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000676 if (kDebug) {
677 cmp(scratch, Operand(0));
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000678 Check(ne, "we should not have an empty lexical context");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000679 }
680
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000681 // Load the global context of the current context.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000682 int offset = Context::kHeaderSize + Context::GLOBAL_INDEX * kPointerSize;
683 ldr(scratch, FieldMemOperand(scratch, offset));
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000684 ldr(scratch, FieldMemOperand(scratch, GlobalObject::kGlobalContextOffset));
685
686 // Check the context is a global context.
687 if (FLAG_debug_code) {
688 // TODO(119): avoid push(holder_reg)/pop(holder_reg)
689 // Cannot use ip as a temporary in this verification code. Due to the fact
690 // that ip is clobbered as part of cmp with an object Operand.
691 push(holder_reg); // Temporarily save holder on the stack.
692 // Read the first word and compare to the global_context_map.
693 ldr(holder_reg, FieldMemOperand(scratch, HeapObject::kMapOffset));
694 cmp(holder_reg, Operand(Factory::global_context_map()));
695 Check(eq, "JSGlobalObject::global_context should be a global context.");
696 pop(holder_reg); // Restore holder.
697 }
698
699 // Check if both contexts are the same.
700 ldr(ip, FieldMemOperand(holder_reg, JSGlobalProxy::kContextOffset));
701 cmp(scratch, Operand(ip));
702 b(eq, &same_contexts);
703
704 // Check the context is a global context.
705 if (FLAG_debug_code) {
706 // TODO(119): avoid push(holder_reg)/pop(holder_reg)
707 // Cannot use ip as a temporary in this verification code. Due to the fact
708 // that ip is clobbered as part of cmp with an object Operand.
709 push(holder_reg); // Temporarily save holder on the stack.
710 mov(holder_reg, ip); // Move ip to its holding place.
711 cmp(holder_reg, Operand(Factory::null_value()));
712 Check(ne, "JSGlobalProxy::context() should not be null.");
713
714 ldr(holder_reg, FieldMemOperand(holder_reg, HeapObject::kMapOffset));
715 cmp(holder_reg, Operand(Factory::global_context_map()));
716 Check(eq, "JSGlobalObject::global_context should be a global context.");
717 // Restore ip is not needed. ip is reloaded below.
718 pop(holder_reg); // Restore holder.
719 // Restore ip to holder's context.
720 ldr(ip, FieldMemOperand(holder_reg, JSGlobalProxy::kContextOffset));
721 }
722
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000723 // Check that the security token in the calling global object is
724 // compatible with the security token in the receiving global
725 // object.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000726 int token_offset = Context::kHeaderSize +
727 Context::SECURITY_TOKEN_INDEX * kPointerSize;
728
729 ldr(scratch, FieldMemOperand(scratch, token_offset));
730 ldr(ip, FieldMemOperand(ip, token_offset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000731 cmp(scratch, Operand(ip));
732 b(ne, miss);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000733
734 bind(&same_contexts);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000735}
736
737
738void MacroAssembler::CallStub(CodeStub* stub) {
kasper.lund7276f142008-07-30 08:49:36 +0000739 ASSERT(allow_stub_calls()); // stub calls are not allowed in some stubs
ager@chromium.org236ad962008-09-25 09:45:57 +0000740 Call(stub->GetCode(), RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000741}
742
743
744void MacroAssembler::StubReturn(int argc) {
745 ASSERT(argc >= 1 && generating_stub());
746 if (argc > 1)
747 add(sp, sp, Operand((argc - 1) * kPointerSize));
748 Ret();
749}
750
mads.s.ager31e71382008-08-13 09:32:07 +0000751
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000752void MacroAssembler::IllegalOperation(int num_arguments) {
753 if (num_arguments > 0) {
754 add(sp, sp, Operand(num_arguments * kPointerSize));
755 }
756 mov(r0, Operand(Factory::undefined_value()));
757}
758
759
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000760void MacroAssembler::CallRuntime(Runtime::Function* f, int num_arguments) {
mads.s.ager31e71382008-08-13 09:32:07 +0000761 // All parameters are on the stack. r0 has the return value after call.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000762
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000763 // If the expected number of arguments of the runtime function is
764 // constant, we check that the actual number of arguments match the
765 // expectation.
766 if (f->nargs >= 0 && f->nargs != num_arguments) {
767 IllegalOperation(num_arguments);
768 return;
769 }
kasper.lund7276f142008-07-30 08:49:36 +0000770
mads.s.ager31e71382008-08-13 09:32:07 +0000771 Runtime::FunctionId function_id =
772 static_cast<Runtime::FunctionId>(f->stub_id);
773 RuntimeStub stub(function_id, num_arguments);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000774 CallStub(&stub);
775}
776
777
778void MacroAssembler::CallRuntime(Runtime::FunctionId fid, int num_arguments) {
779 CallRuntime(Runtime::FunctionForId(fid), num_arguments);
780}
781
782
mads.s.ager31e71382008-08-13 09:32:07 +0000783void MacroAssembler::TailCallRuntime(const ExternalReference& ext,
784 int num_arguments) {
785 // TODO(1236192): Most runtime routines don't need the number of
786 // arguments passed in because it is constant. At some point we
787 // should remove this need and make the runtime routine entry code
788 // smarter.
789 mov(r0, Operand(num_arguments));
790 JumpToBuiltin(ext);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000791}
792
793
794void MacroAssembler::JumpToBuiltin(const ExternalReference& builtin) {
795#if defined(__thumb__)
796 // Thumb mode builtin.
797 ASSERT((reinterpret_cast<intptr_t>(builtin.address()) & 1) == 1);
798#endif
799 mov(r1, Operand(builtin));
800 CEntryStub stub;
ager@chromium.org236ad962008-09-25 09:45:57 +0000801 Jump(stub.GetCode(), RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000802}
803
804
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000805Handle<Code> MacroAssembler::ResolveBuiltin(Builtins::JavaScript id,
806 bool* resolved) {
807 // Contract with compiled functions is that the function is passed in r1.
808 int builtins_offset =
809 JSBuiltinsObject::kJSBuiltinsOffset + (id * kPointerSize);
810 ldr(r1, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
811 ldr(r1, FieldMemOperand(r1, GlobalObject::kBuiltinsOffset));
812 ldr(r1, FieldMemOperand(r1, builtins_offset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000813
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000814 return Builtins::GetCode(id, resolved);
815}
816
817
818void MacroAssembler::InvokeBuiltin(Builtins::JavaScript id,
819 InvokeJSFlags flags) {
820 bool resolved;
821 Handle<Code> code = ResolveBuiltin(id, &resolved);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000822
823 if (flags == CALL_JS) {
ager@chromium.org236ad962008-09-25 09:45:57 +0000824 Call(code, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000825 } else {
826 ASSERT(flags == JUMP_JS);
ager@chromium.org236ad962008-09-25 09:45:57 +0000827 Jump(code, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000828 }
829
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000830 if (!resolved) {
831 const char* name = Builtins::GetName(id);
832 int argc = Builtins::GetArgumentsCount(id);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000833 uint32_t flags =
834 Bootstrapper::FixupFlagsArgumentsCount::encode(argc) |
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000835 Bootstrapper::FixupFlagsIsPCRelative::encode(true) |
836 Bootstrapper::FixupFlagsUseCodeObject::encode(false);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000837 Unresolved entry = { pc_offset() - sizeof(Instr), flags, name };
838 unresolved_.Add(entry);
839 }
840}
841
842
843void MacroAssembler::GetBuiltinEntry(Register target, Builtins::JavaScript id) {
844 bool resolved;
845 Handle<Code> code = ResolveBuiltin(id, &resolved);
846
847 mov(target, Operand(code));
848 if (!resolved) {
849 const char* name = Builtins::GetName(id);
850 int argc = Builtins::GetArgumentsCount(id);
851 uint32_t flags =
852 Bootstrapper::FixupFlagsArgumentsCount::encode(argc) |
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000853 Bootstrapper::FixupFlagsIsPCRelative::encode(true) |
854 Bootstrapper::FixupFlagsUseCodeObject::encode(true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000855 Unresolved entry = { pc_offset() - sizeof(Instr), flags, name };
856 unresolved_.Add(entry);
857 }
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000858
859 add(target, target, Operand(Code::kHeaderSize - kHeapObjectTag));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000860}
861
862
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000863void MacroAssembler::SetCounter(StatsCounter* counter, int value,
864 Register scratch1, Register scratch2) {
865 if (FLAG_native_code_counters && counter->Enabled()) {
866 mov(scratch1, Operand(value));
867 mov(scratch2, Operand(ExternalReference(counter)));
868 str(scratch1, MemOperand(scratch2));
869 }
870}
871
872
873void MacroAssembler::IncrementCounter(StatsCounter* counter, int value,
874 Register scratch1, Register scratch2) {
875 ASSERT(value > 0);
876 if (FLAG_native_code_counters && counter->Enabled()) {
877 mov(scratch2, Operand(ExternalReference(counter)));
878 ldr(scratch1, MemOperand(scratch2));
879 add(scratch1, scratch1, Operand(value));
880 str(scratch1, MemOperand(scratch2));
881 }
882}
883
884
885void MacroAssembler::DecrementCounter(StatsCounter* counter, int value,
886 Register scratch1, Register scratch2) {
887 ASSERT(value > 0);
888 if (FLAG_native_code_counters && counter->Enabled()) {
889 mov(scratch2, Operand(ExternalReference(counter)));
890 ldr(scratch1, MemOperand(scratch2));
891 sub(scratch1, scratch1, Operand(value));
892 str(scratch1, MemOperand(scratch2));
893 }
894}
895
896
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000897void MacroAssembler::Assert(Condition cc, const char* msg) {
898 if (FLAG_debug_code)
899 Check(cc, msg);
900}
901
902
903void MacroAssembler::Check(Condition cc, const char* msg) {
904 Label L;
905 b(cc, &L);
906 Abort(msg);
907 // will not return here
908 bind(&L);
909}
910
911
912void MacroAssembler::Abort(const char* msg) {
913 // We want to pass the msg string like a smi to avoid GC
914 // problems, however msg is not guaranteed to be aligned
915 // properly. Instead, we pass an aligned pointer that is
916 // a proper v8 smi, but also pass the aligment difference
917 // from the real pointer as a smi.
918 intptr_t p1 = reinterpret_cast<intptr_t>(msg);
919 intptr_t p0 = (p1 & ~kSmiTagMask) + kSmiTag;
920 ASSERT(reinterpret_cast<Object*>(p0)->IsSmi());
921#ifdef DEBUG
922 if (msg != NULL) {
923 RecordComment("Abort message: ");
924 RecordComment(msg);
925 }
926#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000927 mov(r0, Operand(p0));
928 push(r0);
929 mov(r0, Operand(Smi::FromInt(p1 - p0)));
mads.s.ager31e71382008-08-13 09:32:07 +0000930 push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000931 CallRuntime(Runtime::kAbort, 2);
932 // will not return here
933}
934
935} } // namespace v8::internal