blob: ee82da197a87b7c92ef18f89c66adaed87f9d670 [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),
kasperl@chromium.org061ef742009-02-27 12:16:20 +000046 allow_stub_calls_(true),
47 code_object_(Heap::undefined_value()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000048}
49
50
51// We always generate arm code, never thumb code, even if V8 is compiled to
52// thumb, so we require inter-working support
53#if defined(__thumb__) && !defined(__THUMB_INTERWORK__)
54#error "flag -mthumb-interwork missing"
55#endif
56
57
58// We do not support thumb inter-working with an arm architecture not supporting
59// the blx instruction (below v5t)
60#if defined(__THUMB_INTERWORK__)
61#if !defined(__ARM_ARCH_5T__) && !defined(__ARM_ARCH_5TE__)
62// add tests for other versions above v5t as required
63#error "for thumb inter-working we require architecture v5t or above"
64#endif
65#endif
66
67
68// Using blx may yield better code, so use it when required or when available
69#if defined(__THUMB_INTERWORK__) || defined(__ARM_ARCH_5__)
70#define USE_BLX 1
71#endif
72
73// Using bx does not yield better code, so use it only when required
74#if defined(__THUMB_INTERWORK__)
75#define USE_BX 1
76#endif
77
78
79void MacroAssembler::Jump(Register target, Condition cond) {
80#if USE_BX
81 bx(target, cond);
82#else
83 mov(pc, Operand(target), LeaveCC, cond);
84#endif
85}
86
87
ager@chromium.org236ad962008-09-25 09:45:57 +000088void MacroAssembler::Jump(intptr_t target, RelocInfo::Mode rmode,
89 Condition cond) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000090#if USE_BX
91 mov(ip, Operand(target, rmode), LeaveCC, cond);
92 bx(ip, cond);
93#else
94 mov(pc, Operand(target, rmode), LeaveCC, cond);
95#endif
96}
97
98
ager@chromium.org236ad962008-09-25 09:45:57 +000099void MacroAssembler::Jump(byte* target, RelocInfo::Mode rmode,
100 Condition cond) {
101 ASSERT(!RelocInfo::IsCodeTarget(rmode));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000102 Jump(reinterpret_cast<intptr_t>(target), rmode, cond);
103}
104
105
ager@chromium.org236ad962008-09-25 09:45:57 +0000106void MacroAssembler::Jump(Handle<Code> code, RelocInfo::Mode rmode,
107 Condition cond) {
108 ASSERT(RelocInfo::IsCodeTarget(rmode));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000109 // 'code' is always generated ARM code, never THUMB code
110 Jump(reinterpret_cast<intptr_t>(code.location()), rmode, cond);
111}
112
113
114void MacroAssembler::Call(Register target, Condition cond) {
115#if USE_BLX
116 blx(target, cond);
117#else
118 // set lr for return at current pc + 8
119 mov(lr, Operand(pc), LeaveCC, cond);
120 mov(pc, Operand(target), LeaveCC, cond);
121#endif
122}
123
124
ager@chromium.org236ad962008-09-25 09:45:57 +0000125void MacroAssembler::Call(intptr_t target, RelocInfo::Mode rmode,
126 Condition cond) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000127#if !defined(__arm__)
ager@chromium.org236ad962008-09-25 09:45:57 +0000128 if (rmode == RelocInfo::RUNTIME_ENTRY) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000129 mov(r2, Operand(target, rmode), LeaveCC, cond);
130 // Set lr for return at current pc + 8.
131 mov(lr, Operand(pc), LeaveCC, cond);
132 // Emit a ldr<cond> pc, [pc + offset of target in constant pool].
133 // Notify the simulator of the transition to C code.
134 swi(assembler::arm::call_rt_r2);
135 } else {
136 // set lr for return at current pc + 8
137 mov(lr, Operand(pc), LeaveCC, cond);
138 // emit a ldr<cond> pc, [pc + offset of target in constant pool]
139 mov(pc, Operand(target, rmode), LeaveCC, cond);
140 }
141#else
142 // Set lr for return at current pc + 8.
143 mov(lr, Operand(pc), LeaveCC, cond);
144 // Emit a ldr<cond> pc, [pc + offset of target in constant pool].
145 mov(pc, Operand(target, rmode), LeaveCC, cond);
146#endif // !defined(__arm__)
147 // If USE_BLX is defined, we could emit a 'mov ip, target', followed by a
148 // 'blx ip'; however, the code would not be shorter than the above sequence
149 // and the target address of the call would be referenced by the first
150 // instruction rather than the second one, which would make it harder to patch
151 // (two instructions before the return address, instead of one).
152 ASSERT(kTargetAddrToReturnAddrDist == sizeof(Instr));
153}
154
155
ager@chromium.org236ad962008-09-25 09:45:57 +0000156void MacroAssembler::Call(byte* target, RelocInfo::Mode rmode,
157 Condition cond) {
158 ASSERT(!RelocInfo::IsCodeTarget(rmode));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000159 Call(reinterpret_cast<intptr_t>(target), rmode, cond);
160}
161
162
ager@chromium.org236ad962008-09-25 09:45:57 +0000163void MacroAssembler::Call(Handle<Code> code, RelocInfo::Mode rmode,
164 Condition cond) {
165 ASSERT(RelocInfo::IsCodeTarget(rmode));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000166 // 'code' is always generated ARM code, never THUMB code
167 Call(reinterpret_cast<intptr_t>(code.location()), rmode, cond);
168}
169
170
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000171void MacroAssembler::Ret(Condition cond) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000172#if USE_BX
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000173 bx(lr, cond);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000174#else
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000175 mov(pc, Operand(lr), LeaveCC, cond);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000176#endif
177}
178
179
ager@chromium.org8bb60582008-12-11 12:02:20 +0000180void MacroAssembler::SmiJumpTable(Register index, Vector<Label*> targets) {
181 // Empty the const pool.
182 CheckConstPool(true, true);
183 add(pc, pc, Operand(index,
184 LSL,
185 assembler::arm::Instr::kInstrSizeLog2 - kSmiTagSize));
186 BlockConstPoolBefore(pc_offset() + (targets.length() + 1) * sizeof(Instr));
187 nop(); // Jump table alignment.
188 for (int i = 0; i < targets.length(); i++) {
189 b(targets[i]);
190 }
191}
192
193
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000194// Will clobber 4 registers: object, offset, scratch, ip. The
195// register 'object' contains a heap object pointer. The heap object
196// tag is shifted away.
197void MacroAssembler::RecordWrite(Register object, Register offset,
198 Register scratch) {
199 // This is how much we shift the remembered set bit offset to get the
200 // offset of the word in the remembered set. We divide by kBitsPerInt (32,
201 // shift right 5) and then multiply by kIntSize (4, shift left 2).
202 const int kRSetWordShift = 3;
203
204 Label fast, done;
205
kasper.lund7276f142008-07-30 08:49:36 +0000206 // First, test that the object is not in the new space. We cannot set
207 // remembered set bits in the new space.
208 // object: heap object pointer (with tag)
209 // offset: offset to store location from the object
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000210 and_(scratch, object, Operand(Heap::NewSpaceMask()));
211 cmp(scratch, Operand(ExternalReference::new_space_start()));
212 b(eq, &done);
213
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000214 // Compute the bit offset in the remembered set.
kasper.lund7276f142008-07-30 08:49:36 +0000215 // object: heap object pointer (with tag)
216 // offset: offset to store location from the object
217 mov(ip, Operand(Page::kPageAlignmentMask)); // load mask only once
218 and_(scratch, object, Operand(ip)); // offset into page of the object
219 add(offset, scratch, Operand(offset)); // add offset into the object
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000220 mov(offset, Operand(offset, LSR, kObjectAlignmentBits));
221
222 // Compute the page address from the heap object pointer.
kasper.lund7276f142008-07-30 08:49:36 +0000223 // object: heap object pointer (with tag)
224 // offset: bit offset of store position in the remembered set
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000225 bic(object, object, Operand(ip));
226
227 // If the bit offset lies beyond the normal remembered set range, it is in
228 // the extra remembered set area of a large object.
kasper.lund7276f142008-07-30 08:49:36 +0000229 // object: page start
230 // offset: bit offset of store position in the remembered set
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000231 cmp(offset, Operand(Page::kPageSize / kPointerSize));
232 b(lt, &fast);
233
234 // Adjust the bit offset to be relative to the start of the extra
235 // remembered set and the start address to be the address of the extra
236 // remembered set.
237 sub(offset, offset, Operand(Page::kPageSize / kPointerSize));
238 // Load the array length into 'scratch' and multiply by four to get the
239 // size in bytes of the elements.
240 ldr(scratch, MemOperand(object, Page::kObjectStartOffset
241 + FixedArray::kLengthOffset));
242 mov(scratch, Operand(scratch, LSL, kObjectAlignmentBits));
243 // Add the page header (including remembered set), array header, and array
244 // body size to the page address.
245 add(object, object, Operand(Page::kObjectStartOffset
246 + Array::kHeaderSize));
247 add(object, object, Operand(scratch));
248
249 bind(&fast);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000250 // Get address of the rset word.
kasper.lund7276f142008-07-30 08:49:36 +0000251 // object: start of the remembered set (page start for the fast case)
252 // offset: bit offset of store position in the remembered set
253 bic(scratch, offset, Operand(kBitsPerInt - 1)); // clear the bit offset
254 add(object, object, Operand(scratch, LSR, kRSetWordShift));
255 // Get bit offset in the rset word.
256 // object: address of remembered set word
257 // offset: bit offset of store position
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000258 and_(offset, offset, Operand(kBitsPerInt - 1));
259
260 ldr(scratch, MemOperand(object));
261 mov(ip, Operand(1));
262 orr(scratch, scratch, Operand(ip, LSL, offset));
263 str(scratch, MemOperand(object));
264
265 bind(&done);
266}
267
268
ager@chromium.org7c537e22008-10-16 08:43:32 +0000269void MacroAssembler::EnterFrame(StackFrame::Type type) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000270 // r0-r3: preserved
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000271 stm(db_w, sp, cp.bit() | fp.bit() | lr.bit());
272 mov(ip, Operand(Smi::FromInt(type)));
273 push(ip);
kasperl@chromium.org061ef742009-02-27 12:16:20 +0000274 mov(ip, Operand(CodeObject()));
275 push(ip);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000276 add(fp, sp, Operand(3 * kPointerSize)); // Adjust FP to point to saved FP.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000277}
278
279
ager@chromium.org7c537e22008-10-16 08:43:32 +0000280void MacroAssembler::LeaveFrame(StackFrame::Type type) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000281 // r0: preserved
282 // r1: preserved
283 // r2: preserved
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000284
ager@chromium.org7c537e22008-10-16 08:43:32 +0000285 // Drop the execution stack down to the frame pointer and restore
286 // the caller frame pointer and return address.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000287 mov(sp, fp);
288 ldm(ia_w, sp, fp.bit() | lr.bit());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000289}
290
291
ager@chromium.org236ad962008-09-25 09:45:57 +0000292void MacroAssembler::EnterExitFrame(StackFrame::Type type) {
293 ASSERT(type == StackFrame::EXIT || type == StackFrame::EXIT_DEBUG);
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000294
295 // Compute the argv pointer and keep it in a callee-saved register.
296 // r0 is argc.
297 add(r6, sp, Operand(r0, LSL, kPointerSizeLog2));
298 sub(r6, r6, Operand(kPointerSize));
299
ager@chromium.org236ad962008-09-25 09:45:57 +0000300 // Compute parameter pointer before making changes and save it as ip
301 // register so that it is restored as sp register on exit, thereby
302 // popping the args.
303
304 // ip = sp + kPointerSize * #args;
305 add(ip, sp, Operand(r0, LSL, kPointerSizeLog2));
306
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000307 // Align the stack at this point. After this point we have 5 pushes,
308 // so in fact we have to unalign here! See also the assert on the
309 // alignment immediately below.
310 if (OS::ActivationFrameAlignment() != kPointerSize) {
311 // This code needs to be made more general if this assert doesn't hold.
312 ASSERT(OS::ActivationFrameAlignment() == 2 * kPointerSize);
313 mov(r7, Operand(Smi::FromInt(0)));
314 tst(sp, Operand(OS::ActivationFrameAlignment() - 1));
315 push(r7, eq); // Conditional push instruction.
316 }
317
ager@chromium.org236ad962008-09-25 09:45:57 +0000318 // Push in reverse order: caller_fp, sp_on_exit, and caller_pc.
319 stm(db_w, sp, fp.bit() | ip.bit() | lr.bit());
320 mov(fp, Operand(sp)); // setup new frame pointer
321
322 // Push debug marker.
323 mov(ip, Operand(type == StackFrame::EXIT_DEBUG ? 1 : 0));
324 push(ip);
325
326 // Save the frame pointer and the context in top.
327 mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address)));
328 str(fp, MemOperand(ip));
329 mov(ip, Operand(ExternalReference(Top::k_context_address)));
330 str(cp, MemOperand(ip));
331
332 // Setup argc and the builtin function in callee-saved registers.
333 mov(r4, Operand(r0));
334 mov(r5, Operand(r1));
335
ager@chromium.org236ad962008-09-25 09:45:57 +0000336
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000337#ifdef ENABLE_DEBUGGER_SUPPORT
ager@chromium.org236ad962008-09-25 09:45:57 +0000338 // Save the state of all registers to the stack from the memory
339 // location. This is needed to allow nested break points.
340 if (type == StackFrame::EXIT_DEBUG) {
341 // Use sp as base to push.
342 CopyRegistersFromMemoryToStack(sp, kJSCallerSaved);
343 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000344#endif
ager@chromium.org236ad962008-09-25 09:45:57 +0000345}
346
347
348void MacroAssembler::LeaveExitFrame(StackFrame::Type type) {
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000349#ifdef ENABLE_DEBUGGER_SUPPORT
ager@chromium.org236ad962008-09-25 09:45:57 +0000350 // Restore the memory copy of the registers by digging them out from
351 // the stack. This is needed to allow nested break points.
352 if (type == StackFrame::EXIT_DEBUG) {
353 // This code intentionally clobbers r2 and r3.
354 const int kCallerSavedSize = kNumJSCallerSaved * kPointerSize;
355 const int kOffset = ExitFrameConstants::kDebugMarkOffset - kCallerSavedSize;
356 add(r3, fp, Operand(kOffset));
357 CopyRegistersFromStackToMemory(r3, r2, kJSCallerSaved);
358 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000359#endif
ager@chromium.org236ad962008-09-25 09:45:57 +0000360
361 // Clear top frame.
362 mov(r3, Operand(0));
363 mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address)));
364 str(r3, MemOperand(ip));
365
366 // Restore current context from top and clear it in debug mode.
367 mov(ip, Operand(ExternalReference(Top::k_context_address)));
368 ldr(cp, MemOperand(ip));
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000369#ifdef DEBUG
370 str(r3, MemOperand(ip));
371#endif
ager@chromium.org236ad962008-09-25 09:45:57 +0000372
373 // Pop the arguments, restore registers, and return.
374 mov(sp, Operand(fp)); // respect ABI stack constraint
375 ldm(ia, sp, fp.bit() | sp.bit() | pc.bit());
376}
377
378
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000379void MacroAssembler::InvokePrologue(const ParameterCount& expected,
380 const ParameterCount& actual,
381 Handle<Code> code_constant,
382 Register code_reg,
383 Label* done,
384 InvokeFlag flag) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000385 bool definitely_matches = false;
386 Label regular_invoke;
387
388 // Check whether the expected and actual arguments count match. If not,
389 // setup registers according to contract with ArgumentsAdaptorTrampoline:
390 // r0: actual arguments count
391 // r1: function (passed through to callee)
392 // r2: expected arguments count
393 // r3: callee code entry
394
395 // The code below is made a lot easier because the calling code already sets
396 // up actual and expected registers according to the contract if values are
397 // passed in registers.
398 ASSERT(actual.is_immediate() || actual.reg().is(r0));
399 ASSERT(expected.is_immediate() || expected.reg().is(r2));
400 ASSERT((!code_constant.is_null() && code_reg.is(no_reg)) || code_reg.is(r3));
401
402 if (expected.is_immediate()) {
403 ASSERT(actual.is_immediate());
404 if (expected.immediate() == actual.immediate()) {
405 definitely_matches = true;
406 } else {
407 mov(r0, Operand(actual.immediate()));
408 const int sentinel = SharedFunctionInfo::kDontAdaptArgumentsSentinel;
409 if (expected.immediate() == sentinel) {
410 // Don't worry about adapting arguments for builtins that
411 // don't want that done. Skip adaption code by making it look
412 // like we have a match between expected and actual number of
413 // arguments.
414 definitely_matches = true;
415 } else {
416 mov(r2, Operand(expected.immediate()));
417 }
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000418 }
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000419 } else {
420 if (actual.is_immediate()) {
421 cmp(expected.reg(), Operand(actual.immediate()));
422 b(eq, &regular_invoke);
423 mov(r0, Operand(actual.immediate()));
424 } else {
425 cmp(expected.reg(), Operand(actual.reg()));
426 b(eq, &regular_invoke);
427 }
428 }
429
430 if (!definitely_matches) {
431 if (!code_constant.is_null()) {
432 mov(r3, Operand(code_constant));
433 add(r3, r3, Operand(Code::kHeaderSize - kHeapObjectTag));
434 }
435
436 Handle<Code> adaptor =
437 Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline));
438 if (flag == CALL_FUNCTION) {
ager@chromium.org236ad962008-09-25 09:45:57 +0000439 Call(adaptor, RelocInfo::CODE_TARGET);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000440 b(done);
441 } else {
ager@chromium.org236ad962008-09-25 09:45:57 +0000442 Jump(adaptor, RelocInfo::CODE_TARGET);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000443 }
444 bind(&regular_invoke);
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000445 }
446}
447
448
449void MacroAssembler::InvokeCode(Register code,
450 const ParameterCount& expected,
451 const ParameterCount& actual,
452 InvokeFlag flag) {
453 Label done;
454
455 InvokePrologue(expected, actual, Handle<Code>::null(), code, &done, flag);
456 if (flag == CALL_FUNCTION) {
457 Call(code);
458 } else {
459 ASSERT(flag == JUMP_FUNCTION);
460 Jump(code);
461 }
462
463 // Continue here if InvokePrologue does handle the invocation due to
464 // mismatched parameter counts.
465 bind(&done);
466}
467
468
469void MacroAssembler::InvokeCode(Handle<Code> code,
470 const ParameterCount& expected,
471 const ParameterCount& actual,
ager@chromium.org236ad962008-09-25 09:45:57 +0000472 RelocInfo::Mode rmode,
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000473 InvokeFlag flag) {
474 Label done;
475
476 InvokePrologue(expected, actual, code, no_reg, &done, flag);
477 if (flag == CALL_FUNCTION) {
478 Call(code, rmode);
479 } else {
480 Jump(code, rmode);
481 }
482
483 // Continue here if InvokePrologue does handle the invocation due to
484 // mismatched parameter counts.
485 bind(&done);
486}
487
488
489void MacroAssembler::InvokeFunction(Register fun,
490 const ParameterCount& actual,
491 InvokeFlag flag) {
492 // Contract with called JS functions requires that function is passed in r1.
493 ASSERT(fun.is(r1));
494
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000495 Register expected_reg = r2;
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000496 Register code_reg = r3;
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000497
498 ldr(code_reg, FieldMemOperand(r1, JSFunction::kSharedFunctionInfoOffset));
499 ldr(cp, FieldMemOperand(r1, JSFunction::kContextOffset));
500 ldr(expected_reg,
501 FieldMemOperand(code_reg,
502 SharedFunctionInfo::kFormalParameterCountOffset));
503 ldr(code_reg,
504 MemOperand(code_reg, SharedFunctionInfo::kCodeOffset - kHeapObjectTag));
505 add(code_reg, code_reg, Operand(Code::kHeaderSize - kHeapObjectTag));
506
507 ParameterCount expected(expected_reg);
508 InvokeCode(code_reg, expected, actual, flag);
509}
510
511
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000512#ifdef ENABLE_DEBUGGER_SUPPORT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000513void MacroAssembler::SaveRegistersToMemory(RegList regs) {
514 ASSERT((regs & ~kJSCallerSaved) == 0);
515 // Copy the content of registers to memory location.
516 for (int i = 0; i < kNumJSCallerSaved; i++) {
517 int r = JSCallerSavedCode(i);
518 if ((regs & (1 << r)) != 0) {
519 Register reg = { r };
520 mov(ip, Operand(ExternalReference(Debug_Address::Register(i))));
521 str(reg, MemOperand(ip));
522 }
523 }
524}
525
526
527void MacroAssembler::RestoreRegistersFromMemory(RegList regs) {
528 ASSERT((regs & ~kJSCallerSaved) == 0);
529 // Copy the content of memory location to registers.
530 for (int i = kNumJSCallerSaved; --i >= 0;) {
531 int r = JSCallerSavedCode(i);
532 if ((regs & (1 << r)) != 0) {
533 Register reg = { r };
534 mov(ip, Operand(ExternalReference(Debug_Address::Register(i))));
535 ldr(reg, MemOperand(ip));
536 }
537 }
538}
539
540
541void MacroAssembler::CopyRegistersFromMemoryToStack(Register base,
542 RegList regs) {
543 ASSERT((regs & ~kJSCallerSaved) == 0);
544 // Copy the content of the memory location to the stack and adjust base.
545 for (int i = kNumJSCallerSaved; --i >= 0;) {
546 int r = JSCallerSavedCode(i);
547 if ((regs & (1 << r)) != 0) {
548 mov(ip, Operand(ExternalReference(Debug_Address::Register(i))));
549 ldr(ip, MemOperand(ip));
550 str(ip, MemOperand(base, 4, NegPreIndex));
551 }
552 }
553}
554
555
556void MacroAssembler::CopyRegistersFromStackToMemory(Register base,
557 Register scratch,
558 RegList regs) {
559 ASSERT((regs & ~kJSCallerSaved) == 0);
560 // Copy the content of the stack to the memory location and adjust base.
561 for (int i = 0; i < kNumJSCallerSaved; i++) {
562 int r = JSCallerSavedCode(i);
563 if ((regs & (1 << r)) != 0) {
564 mov(ip, Operand(ExternalReference(Debug_Address::Register(i))));
565 ldr(scratch, MemOperand(base, 4, PostIndex));
566 str(scratch, MemOperand(ip));
567 }
568 }
569}
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000570#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000571
572void MacroAssembler::PushTryHandler(CodeLocation try_location,
573 HandlerType type) {
574 ASSERT(StackHandlerConstants::kSize == 6 * kPointerSize); // adjust this code
575 // The pc (return address) is passed in register lr.
576 if (try_location == IN_JAVASCRIPT) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000577 stm(db_w, sp, pp.bit() | fp.bit() | lr.bit());
578 if (type == TRY_CATCH_HANDLER) {
579 mov(r3, Operand(StackHandler::TRY_CATCH));
580 } else {
581 mov(r3, Operand(StackHandler::TRY_FINALLY));
582 }
583 push(r3); // state
584 mov(r3, Operand(ExternalReference(Top::k_handler_address)));
585 ldr(r1, MemOperand(r3));
586 push(r1); // next sp
587 str(sp, MemOperand(r3)); // chain handler
mads.s.ager31e71382008-08-13 09:32:07 +0000588 mov(r0, Operand(Smi::FromInt(StackHandler::kCodeNotPresent))); // new TOS
589 push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000590 } else {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000591 // Must preserve r0-r4, r5-r7 are available.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000592 ASSERT(try_location == IN_JS_ENTRY);
593 // The parameter pointer is meaningless here and fp does not point to a JS
594 // frame. So we save NULL for both pp and fp. We expect the code throwing an
595 // exception to check fp before dereferencing it to restore the context.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000596 mov(pp, Operand(0)); // set pp to NULL
597 mov(ip, Operand(0)); // to save a NULL fp
598 stm(db_w, sp, pp.bit() | ip.bit() | lr.bit());
599 mov(r6, Operand(StackHandler::ENTRY));
600 push(r6); // state
601 mov(r7, Operand(ExternalReference(Top::k_handler_address)));
602 ldr(r6, MemOperand(r7));
603 push(r6); // next sp
604 str(sp, MemOperand(r7)); // chain handler
mads.s.ager31e71382008-08-13 09:32:07 +0000605 mov(r5, Operand(Smi::FromInt(StackHandler::kCodeNotPresent))); // new TOS
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000606 push(r5); // flush TOS
607 }
608}
609
610
611Register MacroAssembler::CheckMaps(JSObject* object, Register object_reg,
612 JSObject* holder, Register holder_reg,
613 Register scratch,
614 Label* miss) {
615 // Make sure there's no overlap between scratch and the other
616 // registers.
617 ASSERT(!scratch.is(object_reg) && !scratch.is(holder_reg));
618
619 // Keep track of the current object in register reg.
620 Register reg = object_reg;
621 int depth = 1;
622
623 // Check the maps in the prototype chain.
624 // Traverse the prototype chain from the object and do map checks.
625 while (object != holder) {
626 depth++;
627
628 // Only global objects and objects that do not require access
629 // checks are allowed in stubs.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000630 ASSERT(object->IsJSGlobalProxy() || !object->IsAccessCheckNeeded());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000631
632 // Get the map of the current object.
633 ldr(scratch, FieldMemOperand(reg, HeapObject::kMapOffset));
634 cmp(scratch, Operand(Handle<Map>(object->map())));
635
636 // Branch on the result of the map check.
637 b(ne, miss);
638
639 // Check access rights to the global object. This has to happen
640 // after the map check so that we know that the object is
641 // actually a global object.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000642 if (object->IsJSGlobalProxy()) {
643 CheckAccessGlobalProxy(reg, scratch, miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000644 // Restore scratch register to be the map of the object. In the
645 // new space case below, we load the prototype from the map in
646 // the scratch register.
647 ldr(scratch, FieldMemOperand(reg, HeapObject::kMapOffset));
648 }
649
650 reg = holder_reg; // from now the object is in holder_reg
651 JSObject* prototype = JSObject::cast(object->GetPrototype());
652 if (Heap::InNewSpace(prototype)) {
653 // The prototype is in new space; we cannot store a reference
654 // to it in the code. Load it from the map.
655 ldr(reg, FieldMemOperand(scratch, Map::kPrototypeOffset));
656 } else {
657 // The prototype is in old space; load it directly.
658 mov(reg, Operand(Handle<JSObject>(prototype)));
659 }
660
661 // Go to the next object in the prototype chain.
662 object = prototype;
663 }
664
665 // Check the holder map.
666 ldr(scratch, FieldMemOperand(reg, HeapObject::kMapOffset));
667 cmp(scratch, Operand(Handle<Map>(object->map())));
668 b(ne, miss);
669
670 // Log the check depth.
671 LOG(IntEvent("check-maps-depth", depth));
672
673 // Perform security check for access to the global object and return
674 // the holder register.
675 ASSERT(object == holder);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000676 ASSERT(object->IsJSGlobalProxy() || !object->IsAccessCheckNeeded());
677 if (object->IsJSGlobalProxy()) {
678 CheckAccessGlobalProxy(reg, scratch, miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000679 }
680 return reg;
681}
682
683
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000684void MacroAssembler::CheckAccessGlobalProxy(Register holder_reg,
685 Register scratch,
686 Label* miss) {
687 Label same_contexts;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000688
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000689 ASSERT(!holder_reg.is(scratch));
690 ASSERT(!holder_reg.is(ip));
691 ASSERT(!scratch.is(ip));
692
693 // Load current lexical context from the stack frame.
694 ldr(scratch, MemOperand(fp, StandardFrameConstants::kContextOffset));
695 // In debug mode, make sure the lexical context is set.
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000696#ifdef DEBUG
697 cmp(scratch, Operand(0));
698 Check(ne, "we should not have an empty lexical context");
699#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000700
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000701 // Load the global context of the current context.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000702 int offset = Context::kHeaderSize + Context::GLOBAL_INDEX * kPointerSize;
703 ldr(scratch, FieldMemOperand(scratch, offset));
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000704 ldr(scratch, FieldMemOperand(scratch, GlobalObject::kGlobalContextOffset));
705
706 // Check the context is a global context.
707 if (FLAG_debug_code) {
708 // TODO(119): avoid push(holder_reg)/pop(holder_reg)
709 // Cannot use ip as a temporary in this verification code. Due to the fact
710 // that ip is clobbered as part of cmp with an object Operand.
711 push(holder_reg); // Temporarily save holder on the stack.
712 // Read the first word and compare to the global_context_map.
713 ldr(holder_reg, FieldMemOperand(scratch, HeapObject::kMapOffset));
714 cmp(holder_reg, Operand(Factory::global_context_map()));
715 Check(eq, "JSGlobalObject::global_context should be a global context.");
716 pop(holder_reg); // Restore holder.
717 }
718
719 // Check if both contexts are the same.
720 ldr(ip, FieldMemOperand(holder_reg, JSGlobalProxy::kContextOffset));
721 cmp(scratch, Operand(ip));
722 b(eq, &same_contexts);
723
724 // Check the context is a global context.
725 if (FLAG_debug_code) {
726 // TODO(119): avoid push(holder_reg)/pop(holder_reg)
727 // Cannot use ip as a temporary in this verification code. Due to the fact
728 // that ip is clobbered as part of cmp with an object Operand.
729 push(holder_reg); // Temporarily save holder on the stack.
730 mov(holder_reg, ip); // Move ip to its holding place.
731 cmp(holder_reg, Operand(Factory::null_value()));
732 Check(ne, "JSGlobalProxy::context() should not be null.");
733
734 ldr(holder_reg, FieldMemOperand(holder_reg, HeapObject::kMapOffset));
735 cmp(holder_reg, Operand(Factory::global_context_map()));
736 Check(eq, "JSGlobalObject::global_context should be a global context.");
737 // Restore ip is not needed. ip is reloaded below.
738 pop(holder_reg); // Restore holder.
739 // Restore ip to holder's context.
740 ldr(ip, FieldMemOperand(holder_reg, JSGlobalProxy::kContextOffset));
741 }
742
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000743 // Check that the security token in the calling global object is
744 // compatible with the security token in the receiving global
745 // object.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000746 int token_offset = Context::kHeaderSize +
747 Context::SECURITY_TOKEN_INDEX * kPointerSize;
748
749 ldr(scratch, FieldMemOperand(scratch, token_offset));
750 ldr(ip, FieldMemOperand(ip, token_offset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000751 cmp(scratch, Operand(ip));
752 b(ne, miss);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000753
754 bind(&same_contexts);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000755}
756
757
758void MacroAssembler::CallStub(CodeStub* stub) {
kasper.lund7276f142008-07-30 08:49:36 +0000759 ASSERT(allow_stub_calls()); // stub calls are not allowed in some stubs
ager@chromium.org236ad962008-09-25 09:45:57 +0000760 Call(stub->GetCode(), RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000761}
762
763
764void MacroAssembler::StubReturn(int argc) {
765 ASSERT(argc >= 1 && generating_stub());
766 if (argc > 1)
767 add(sp, sp, Operand((argc - 1) * kPointerSize));
768 Ret();
769}
770
mads.s.ager31e71382008-08-13 09:32:07 +0000771
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000772void MacroAssembler::IllegalOperation(int num_arguments) {
773 if (num_arguments > 0) {
774 add(sp, sp, Operand(num_arguments * kPointerSize));
775 }
776 mov(r0, Operand(Factory::undefined_value()));
777}
778
779
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000780void MacroAssembler::CallRuntime(Runtime::Function* f, int num_arguments) {
mads.s.ager31e71382008-08-13 09:32:07 +0000781 // All parameters are on the stack. r0 has the return value after call.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000782
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000783 // If the expected number of arguments of the runtime function is
784 // constant, we check that the actual number of arguments match the
785 // expectation.
786 if (f->nargs >= 0 && f->nargs != num_arguments) {
787 IllegalOperation(num_arguments);
788 return;
789 }
kasper.lund7276f142008-07-30 08:49:36 +0000790
mads.s.ager31e71382008-08-13 09:32:07 +0000791 Runtime::FunctionId function_id =
792 static_cast<Runtime::FunctionId>(f->stub_id);
793 RuntimeStub stub(function_id, num_arguments);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000794 CallStub(&stub);
795}
796
797
798void MacroAssembler::CallRuntime(Runtime::FunctionId fid, int num_arguments) {
799 CallRuntime(Runtime::FunctionForId(fid), num_arguments);
800}
801
802
mads.s.ager31e71382008-08-13 09:32:07 +0000803void MacroAssembler::TailCallRuntime(const ExternalReference& ext,
804 int num_arguments) {
805 // TODO(1236192): Most runtime routines don't need the number of
806 // arguments passed in because it is constant. At some point we
807 // should remove this need and make the runtime routine entry code
808 // smarter.
809 mov(r0, Operand(num_arguments));
810 JumpToBuiltin(ext);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000811}
812
813
814void MacroAssembler::JumpToBuiltin(const ExternalReference& builtin) {
815#if defined(__thumb__)
816 // Thumb mode builtin.
817 ASSERT((reinterpret_cast<intptr_t>(builtin.address()) & 1) == 1);
818#endif
819 mov(r1, Operand(builtin));
820 CEntryStub stub;
ager@chromium.org236ad962008-09-25 09:45:57 +0000821 Jump(stub.GetCode(), RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000822}
823
824
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000825Handle<Code> MacroAssembler::ResolveBuiltin(Builtins::JavaScript id,
826 bool* resolved) {
827 // Contract with compiled functions is that the function is passed in r1.
828 int builtins_offset =
829 JSBuiltinsObject::kJSBuiltinsOffset + (id * kPointerSize);
830 ldr(r1, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
831 ldr(r1, FieldMemOperand(r1, GlobalObject::kBuiltinsOffset));
832 ldr(r1, FieldMemOperand(r1, builtins_offset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000833
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000834 return Builtins::GetCode(id, resolved);
835}
836
837
838void MacroAssembler::InvokeBuiltin(Builtins::JavaScript id,
839 InvokeJSFlags flags) {
840 bool resolved;
841 Handle<Code> code = ResolveBuiltin(id, &resolved);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000842
843 if (flags == CALL_JS) {
ager@chromium.org236ad962008-09-25 09:45:57 +0000844 Call(code, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000845 } else {
846 ASSERT(flags == JUMP_JS);
ager@chromium.org236ad962008-09-25 09:45:57 +0000847 Jump(code, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000848 }
849
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000850 if (!resolved) {
851 const char* name = Builtins::GetName(id);
852 int argc = Builtins::GetArgumentsCount(id);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000853 uint32_t flags =
854 Bootstrapper::FixupFlagsArgumentsCount::encode(argc) |
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000855 Bootstrapper::FixupFlagsIsPCRelative::encode(true) |
856 Bootstrapper::FixupFlagsUseCodeObject::encode(false);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000857 Unresolved entry = { pc_offset() - sizeof(Instr), flags, name };
858 unresolved_.Add(entry);
859 }
860}
861
862
863void MacroAssembler::GetBuiltinEntry(Register target, Builtins::JavaScript id) {
864 bool resolved;
865 Handle<Code> code = ResolveBuiltin(id, &resolved);
866
867 mov(target, Operand(code));
868 if (!resolved) {
869 const char* name = Builtins::GetName(id);
870 int argc = Builtins::GetArgumentsCount(id);
871 uint32_t flags =
872 Bootstrapper::FixupFlagsArgumentsCount::encode(argc) |
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000873 Bootstrapper::FixupFlagsIsPCRelative::encode(true) |
874 Bootstrapper::FixupFlagsUseCodeObject::encode(true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000875 Unresolved entry = { pc_offset() - sizeof(Instr), flags, name };
876 unresolved_.Add(entry);
877 }
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000878
879 add(target, target, Operand(Code::kHeaderSize - kHeapObjectTag));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000880}
881
882
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000883void MacroAssembler::SetCounter(StatsCounter* counter, int value,
884 Register scratch1, Register scratch2) {
885 if (FLAG_native_code_counters && counter->Enabled()) {
886 mov(scratch1, Operand(value));
887 mov(scratch2, Operand(ExternalReference(counter)));
888 str(scratch1, MemOperand(scratch2));
889 }
890}
891
892
893void MacroAssembler::IncrementCounter(StatsCounter* counter, int value,
894 Register scratch1, Register scratch2) {
895 ASSERT(value > 0);
896 if (FLAG_native_code_counters && counter->Enabled()) {
897 mov(scratch2, Operand(ExternalReference(counter)));
898 ldr(scratch1, MemOperand(scratch2));
899 add(scratch1, scratch1, Operand(value));
900 str(scratch1, MemOperand(scratch2));
901 }
902}
903
904
905void MacroAssembler::DecrementCounter(StatsCounter* counter, int value,
906 Register scratch1, Register scratch2) {
907 ASSERT(value > 0);
908 if (FLAG_native_code_counters && counter->Enabled()) {
909 mov(scratch2, Operand(ExternalReference(counter)));
910 ldr(scratch1, MemOperand(scratch2));
911 sub(scratch1, scratch1, Operand(value));
912 str(scratch1, MemOperand(scratch2));
913 }
914}
915
916
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000917void MacroAssembler::Assert(Condition cc, const char* msg) {
918 if (FLAG_debug_code)
919 Check(cc, msg);
920}
921
922
923void MacroAssembler::Check(Condition cc, const char* msg) {
924 Label L;
925 b(cc, &L);
926 Abort(msg);
927 // will not return here
928 bind(&L);
929}
930
931
932void MacroAssembler::Abort(const char* msg) {
933 // We want to pass the msg string like a smi to avoid GC
934 // problems, however msg is not guaranteed to be aligned
935 // properly. Instead, we pass an aligned pointer that is
ager@chromium.org32912102009-01-16 10:38:43 +0000936 // a proper v8 smi, but also pass the alignment difference
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000937 // from the real pointer as a smi.
938 intptr_t p1 = reinterpret_cast<intptr_t>(msg);
939 intptr_t p0 = (p1 & ~kSmiTagMask) + kSmiTag;
940 ASSERT(reinterpret_cast<Object*>(p0)->IsSmi());
941#ifdef DEBUG
942 if (msg != NULL) {
943 RecordComment("Abort message: ");
944 RecordComment(msg);
945 }
946#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000947 mov(r0, Operand(p0));
948 push(r0);
949 mov(r0, Operand(Smi::FromInt(p1 - p0)));
mads.s.ager31e71382008-08-13 09:32:07 +0000950 push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000951 CallRuntime(Runtime::kAbort, 2);
952 // will not return here
953}
954
955} } // namespace v8::internal