blob: 2ca489868255571632e8d5679098fbbee0824fff [file] [log] [blame]
ager@chromium.orgeadaf222009-06-16 09:43:10 +00001// Copyright 2006-2009 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
kasperl@chromium.org71affb52009-05-26 05:44:31 +000035namespace v8 {
36namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000037
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000038MacroAssembler::MacroAssembler(void* buffer, int size)
39 : Assembler(buffer, size),
40 unresolved_(0),
kasper.lund7276f142008-07-30 08:49:36 +000041 generating_stub_(false),
kasperl@chromium.org061ef742009-02-27 12:16:20 +000042 allow_stub_calls_(true),
43 code_object_(Heap::undefined_value()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000044}
45
46
47// We always generate arm code, never thumb code, even if V8 is compiled to
48// thumb, so we require inter-working support
kasperl@chromium.org2abc4502009-07-02 07:00:29 +000049#if defined(__thumb__) && !defined(USE_THUMB_INTERWORK)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000050#error "flag -mthumb-interwork missing"
51#endif
52
53
54// We do not support thumb inter-working with an arm architecture not supporting
55// the blx instruction (below v5t)
kasperl@chromium.org2abc4502009-07-02 07:00:29 +000056#if defined(USE_THUMB_INTERWORK)
kasperl@chromium.org71affb52009-05-26 05:44:31 +000057#if !defined(__ARM_ARCH_5T__) && \
58 !defined(__ARM_ARCH_5TE__) && \
59 !defined(__ARM_ARCH_7A__) && \
60 !defined(__ARM_ARCH_7__)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000061// 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
kasperl@chromium.org2abc4502009-07-02 07:00:29 +000068#if defined(USE_THUMB_INTERWORK) || defined(__ARM_ARCH_5__)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000069#define USE_BLX 1
70#endif
71
72// Using bx does not yield better code, so use it only when required
kasperl@chromium.org2abc4502009-07-02 07:00:29 +000073#if defined(USE_THUMB_INTERWORK)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000074#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 // Set lr for return at current pc + 8.
127 mov(lr, Operand(pc), LeaveCC, cond);
128 // Emit a ldr<cond> pc, [pc + offset of target in constant pool].
129 mov(pc, Operand(target, rmode), LeaveCC, cond);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000130 // If USE_BLX is defined, we could emit a 'mov ip, target', followed by a
131 // 'blx ip'; however, the code would not be shorter than the above sequence
132 // and the target address of the call would be referenced by the first
133 // instruction rather than the second one, which would make it harder to patch
134 // (two instructions before the return address, instead of one).
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000135 ASSERT(kPatchReturnSequenceLength == sizeof(Instr));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000136}
137
138
ager@chromium.org236ad962008-09-25 09:45:57 +0000139void MacroAssembler::Call(byte* target, RelocInfo::Mode rmode,
140 Condition cond) {
141 ASSERT(!RelocInfo::IsCodeTarget(rmode));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000142 Call(reinterpret_cast<intptr_t>(target), rmode, cond);
143}
144
145
ager@chromium.org236ad962008-09-25 09:45:57 +0000146void MacroAssembler::Call(Handle<Code> code, RelocInfo::Mode rmode,
147 Condition cond) {
148 ASSERT(RelocInfo::IsCodeTarget(rmode));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000149 // 'code' is always generated ARM code, never THUMB code
150 Call(reinterpret_cast<intptr_t>(code.location()), rmode, cond);
151}
152
153
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000154void MacroAssembler::Ret(Condition cond) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000155#if USE_BX
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000156 bx(lr, cond);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000157#else
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000158 mov(pc, Operand(lr), LeaveCC, cond);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000159#endif
160}
161
162
ager@chromium.org8bb60582008-12-11 12:02:20 +0000163void MacroAssembler::SmiJumpTable(Register index, Vector<Label*> targets) {
164 // Empty the const pool.
165 CheckConstPool(true, true);
166 add(pc, pc, Operand(index,
167 LSL,
168 assembler::arm::Instr::kInstrSizeLog2 - kSmiTagSize));
169 BlockConstPoolBefore(pc_offset() + (targets.length() + 1) * sizeof(Instr));
170 nop(); // Jump table alignment.
171 for (int i = 0; i < targets.length(); i++) {
172 b(targets[i]);
173 }
174}
175
176
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000177void MacroAssembler::LoadRoot(Register destination,
178 Heap::RootListIndex index,
179 Condition cond) {
180 ldr(destination, MemOperand(r10, index << kPointerSizeLog2), cond);
181}
182
183
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000184// Will clobber 4 registers: object, offset, scratch, ip. The
185// register 'object' contains a heap object pointer. The heap object
186// tag is shifted away.
187void MacroAssembler::RecordWrite(Register object, Register offset,
188 Register scratch) {
189 // This is how much we shift the remembered set bit offset to get the
190 // offset of the word in the remembered set. We divide by kBitsPerInt (32,
191 // shift right 5) and then multiply by kIntSize (4, shift left 2).
192 const int kRSetWordShift = 3;
193
194 Label fast, done;
195
kasper.lund7276f142008-07-30 08:49:36 +0000196 // First, test that the object is not in the new space. We cannot set
197 // remembered set bits in the new space.
198 // object: heap object pointer (with tag)
199 // offset: offset to store location from the object
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000200 and_(scratch, object, Operand(Heap::NewSpaceMask()));
201 cmp(scratch, Operand(ExternalReference::new_space_start()));
202 b(eq, &done);
203
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000204 // Compute the bit offset in the remembered set.
kasper.lund7276f142008-07-30 08:49:36 +0000205 // object: heap object pointer (with tag)
206 // offset: offset to store location from the object
207 mov(ip, Operand(Page::kPageAlignmentMask)); // load mask only once
208 and_(scratch, object, Operand(ip)); // offset into page of the object
209 add(offset, scratch, Operand(offset)); // add offset into the object
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000210 mov(offset, Operand(offset, LSR, kObjectAlignmentBits));
211
212 // Compute the page address from the heap object pointer.
kasper.lund7276f142008-07-30 08:49:36 +0000213 // object: heap object pointer (with tag)
214 // offset: bit offset of store position in the remembered set
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000215 bic(object, object, Operand(ip));
216
217 // If the bit offset lies beyond the normal remembered set range, it is in
218 // the extra remembered set area of a large object.
kasper.lund7276f142008-07-30 08:49:36 +0000219 // object: page start
220 // offset: bit offset of store position in the remembered set
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000221 cmp(offset, Operand(Page::kPageSize / kPointerSize));
222 b(lt, &fast);
223
224 // Adjust the bit offset to be relative to the start of the extra
225 // remembered set and the start address to be the address of the extra
226 // remembered set.
227 sub(offset, offset, Operand(Page::kPageSize / kPointerSize));
228 // Load the array length into 'scratch' and multiply by four to get the
229 // size in bytes of the elements.
230 ldr(scratch, MemOperand(object, Page::kObjectStartOffset
231 + FixedArray::kLengthOffset));
232 mov(scratch, Operand(scratch, LSL, kObjectAlignmentBits));
233 // Add the page header (including remembered set), array header, and array
234 // body size to the page address.
235 add(object, object, Operand(Page::kObjectStartOffset
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000236 + FixedArray::kHeaderSize));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000237 add(object, object, Operand(scratch));
238
239 bind(&fast);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000240 // Get address of the rset word.
kasper.lund7276f142008-07-30 08:49:36 +0000241 // object: start of the remembered set (page start for the fast case)
242 // offset: bit offset of store position in the remembered set
243 bic(scratch, offset, Operand(kBitsPerInt - 1)); // clear the bit offset
244 add(object, object, Operand(scratch, LSR, kRSetWordShift));
245 // Get bit offset in the rset word.
246 // object: address of remembered set word
247 // offset: bit offset of store position
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000248 and_(offset, offset, Operand(kBitsPerInt - 1));
249
250 ldr(scratch, MemOperand(object));
251 mov(ip, Operand(1));
252 orr(scratch, scratch, Operand(ip, LSL, offset));
253 str(scratch, MemOperand(object));
254
255 bind(&done);
256}
257
258
ager@chromium.org7c537e22008-10-16 08:43:32 +0000259void MacroAssembler::EnterFrame(StackFrame::Type type) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000260 // r0-r3: preserved
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000261 stm(db_w, sp, cp.bit() | fp.bit() | lr.bit());
262 mov(ip, Operand(Smi::FromInt(type)));
263 push(ip);
kasperl@chromium.org061ef742009-02-27 12:16:20 +0000264 mov(ip, Operand(CodeObject()));
265 push(ip);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000266 add(fp, sp, Operand(3 * kPointerSize)); // Adjust FP to point to saved FP.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000267}
268
269
ager@chromium.org7c537e22008-10-16 08:43:32 +0000270void MacroAssembler::LeaveFrame(StackFrame::Type type) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000271 // r0: preserved
272 // r1: preserved
273 // r2: preserved
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000274
ager@chromium.org7c537e22008-10-16 08:43:32 +0000275 // Drop the execution stack down to the frame pointer and restore
276 // the caller frame pointer and return address.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000277 mov(sp, fp);
278 ldm(ia_w, sp, fp.bit() | lr.bit());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000279}
280
281
ager@chromium.org236ad962008-09-25 09:45:57 +0000282void MacroAssembler::EnterExitFrame(StackFrame::Type type) {
283 ASSERT(type == StackFrame::EXIT || type == StackFrame::EXIT_DEBUG);
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000284
285 // Compute the argv pointer and keep it in a callee-saved register.
286 // r0 is argc.
287 add(r6, sp, Operand(r0, LSL, kPointerSizeLog2));
288 sub(r6, r6, Operand(kPointerSize));
289
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000290 // Compute callee's stack pointer before making changes and save it as
291 // ip register so that it is restored as sp register on exit, thereby
ager@chromium.org236ad962008-09-25 09:45:57 +0000292 // popping the args.
293
294 // ip = sp + kPointerSize * #args;
295 add(ip, sp, Operand(r0, LSL, kPointerSizeLog2));
296
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000297 // Align the stack at this point. After this point we have 5 pushes,
298 // so in fact we have to unalign here! See also the assert on the
299 // alignment immediately below.
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000300#if defined(V8_HOST_ARCH_ARM)
301 // Running on the real platform. Use the alignment as mandated by the local
302 // environment.
303 // Note: This will break if we ever start generating snapshots on one ARM
304 // platform for another ARM platform with a different alignment.
305 int activation_frame_alignment = OS::ActivationFrameAlignment();
306#else // defined(V8_HOST_ARCH_ARM)
307 // If we are using the simulator then we should always align to the expected
308 // alignment. As the simulator is used to generate snapshots we do not know
309 // if the target platform will need alignment, so we will always align at
310 // this point here.
311 int activation_frame_alignment = 2 * kPointerSize;
312#endif // defined(V8_HOST_ARCH_ARM)
313 if (activation_frame_alignment != kPointerSize) {
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000314 // This code needs to be made more general if this assert doesn't hold.
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000315 ASSERT(activation_frame_alignment == 2 * kPointerSize);
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000316 mov(r7, Operand(Smi::FromInt(0)));
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000317 tst(sp, Operand(activation_frame_alignment - 1));
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000318 push(r7, eq); // Conditional push instruction.
319 }
320
ager@chromium.org236ad962008-09-25 09:45:57 +0000321 // Push in reverse order: caller_fp, sp_on_exit, and caller_pc.
322 stm(db_w, sp, fp.bit() | ip.bit() | lr.bit());
323 mov(fp, Operand(sp)); // setup new frame pointer
324
325 // Push debug marker.
326 mov(ip, Operand(type == StackFrame::EXIT_DEBUG ? 1 : 0));
327 push(ip);
328
329 // Save the frame pointer and the context in top.
330 mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address)));
331 str(fp, MemOperand(ip));
332 mov(ip, Operand(ExternalReference(Top::k_context_address)));
333 str(cp, MemOperand(ip));
334
335 // Setup argc and the builtin function in callee-saved registers.
336 mov(r4, Operand(r0));
337 mov(r5, Operand(r1));
338
ager@chromium.org236ad962008-09-25 09:45:57 +0000339
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000340#ifdef ENABLE_DEBUGGER_SUPPORT
ager@chromium.org236ad962008-09-25 09:45:57 +0000341 // Save the state of all registers to the stack from the memory
342 // location. This is needed to allow nested break points.
343 if (type == StackFrame::EXIT_DEBUG) {
344 // Use sp as base to push.
345 CopyRegistersFromMemoryToStack(sp, kJSCallerSaved);
346 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000347#endif
ager@chromium.org236ad962008-09-25 09:45:57 +0000348}
349
350
351void MacroAssembler::LeaveExitFrame(StackFrame::Type type) {
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000352#ifdef ENABLE_DEBUGGER_SUPPORT
ager@chromium.org236ad962008-09-25 09:45:57 +0000353 // Restore the memory copy of the registers by digging them out from
354 // the stack. This is needed to allow nested break points.
355 if (type == StackFrame::EXIT_DEBUG) {
356 // This code intentionally clobbers r2 and r3.
357 const int kCallerSavedSize = kNumJSCallerSaved * kPointerSize;
358 const int kOffset = ExitFrameConstants::kDebugMarkOffset - kCallerSavedSize;
359 add(r3, fp, Operand(kOffset));
360 CopyRegistersFromStackToMemory(r3, r2, kJSCallerSaved);
361 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000362#endif
ager@chromium.org236ad962008-09-25 09:45:57 +0000363
364 // Clear top frame.
365 mov(r3, Operand(0));
366 mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address)));
367 str(r3, MemOperand(ip));
368
369 // Restore current context from top and clear it in debug mode.
370 mov(ip, Operand(ExternalReference(Top::k_context_address)));
371 ldr(cp, MemOperand(ip));
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000372#ifdef DEBUG
373 str(r3, MemOperand(ip));
374#endif
ager@chromium.org236ad962008-09-25 09:45:57 +0000375
376 // Pop the arguments, restore registers, and return.
377 mov(sp, Operand(fp)); // respect ABI stack constraint
378 ldm(ia, sp, fp.bit() | sp.bit() | pc.bit());
379}
380
381
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000382void MacroAssembler::InvokePrologue(const ParameterCount& expected,
383 const ParameterCount& actual,
384 Handle<Code> code_constant,
385 Register code_reg,
386 Label* done,
387 InvokeFlag flag) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000388 bool definitely_matches = false;
389 Label regular_invoke;
390
391 // Check whether the expected and actual arguments count match. If not,
392 // setup registers according to contract with ArgumentsAdaptorTrampoline:
393 // r0: actual arguments count
394 // r1: function (passed through to callee)
395 // r2: expected arguments count
396 // r3: callee code entry
397
398 // The code below is made a lot easier because the calling code already sets
399 // up actual and expected registers according to the contract if values are
400 // passed in registers.
401 ASSERT(actual.is_immediate() || actual.reg().is(r0));
402 ASSERT(expected.is_immediate() || expected.reg().is(r2));
403 ASSERT((!code_constant.is_null() && code_reg.is(no_reg)) || code_reg.is(r3));
404
405 if (expected.is_immediate()) {
406 ASSERT(actual.is_immediate());
407 if (expected.immediate() == actual.immediate()) {
408 definitely_matches = true;
409 } else {
410 mov(r0, Operand(actual.immediate()));
411 const int sentinel = SharedFunctionInfo::kDontAdaptArgumentsSentinel;
412 if (expected.immediate() == sentinel) {
413 // Don't worry about adapting arguments for builtins that
414 // don't want that done. Skip adaption code by making it look
415 // like we have a match between expected and actual number of
416 // arguments.
417 definitely_matches = true;
418 } else {
419 mov(r2, Operand(expected.immediate()));
420 }
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000421 }
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000422 } else {
423 if (actual.is_immediate()) {
424 cmp(expected.reg(), Operand(actual.immediate()));
425 b(eq, &regular_invoke);
426 mov(r0, Operand(actual.immediate()));
427 } else {
428 cmp(expected.reg(), Operand(actual.reg()));
429 b(eq, &regular_invoke);
430 }
431 }
432
433 if (!definitely_matches) {
434 if (!code_constant.is_null()) {
435 mov(r3, Operand(code_constant));
436 add(r3, r3, Operand(Code::kHeaderSize - kHeapObjectTag));
437 }
438
439 Handle<Code> adaptor =
440 Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline));
441 if (flag == CALL_FUNCTION) {
ager@chromium.org236ad962008-09-25 09:45:57 +0000442 Call(adaptor, RelocInfo::CODE_TARGET);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000443 b(done);
444 } else {
ager@chromium.org236ad962008-09-25 09:45:57 +0000445 Jump(adaptor, RelocInfo::CODE_TARGET);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000446 }
447 bind(&regular_invoke);
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000448 }
449}
450
451
452void MacroAssembler::InvokeCode(Register code,
453 const ParameterCount& expected,
454 const ParameterCount& actual,
455 InvokeFlag flag) {
456 Label done;
457
458 InvokePrologue(expected, actual, Handle<Code>::null(), code, &done, flag);
459 if (flag == CALL_FUNCTION) {
460 Call(code);
461 } else {
462 ASSERT(flag == JUMP_FUNCTION);
463 Jump(code);
464 }
465
466 // Continue here if InvokePrologue does handle the invocation due to
467 // mismatched parameter counts.
468 bind(&done);
469}
470
471
472void MacroAssembler::InvokeCode(Handle<Code> code,
473 const ParameterCount& expected,
474 const ParameterCount& actual,
ager@chromium.org236ad962008-09-25 09:45:57 +0000475 RelocInfo::Mode rmode,
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000476 InvokeFlag flag) {
477 Label done;
478
479 InvokePrologue(expected, actual, code, no_reg, &done, flag);
480 if (flag == CALL_FUNCTION) {
481 Call(code, rmode);
482 } else {
483 Jump(code, rmode);
484 }
485
486 // Continue here if InvokePrologue does handle the invocation due to
487 // mismatched parameter counts.
488 bind(&done);
489}
490
491
492void MacroAssembler::InvokeFunction(Register fun,
493 const ParameterCount& actual,
494 InvokeFlag flag) {
495 // Contract with called JS functions requires that function is passed in r1.
496 ASSERT(fun.is(r1));
497
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000498 Register expected_reg = r2;
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000499 Register code_reg = r3;
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000500
501 ldr(code_reg, FieldMemOperand(r1, JSFunction::kSharedFunctionInfoOffset));
502 ldr(cp, FieldMemOperand(r1, JSFunction::kContextOffset));
503 ldr(expected_reg,
504 FieldMemOperand(code_reg,
505 SharedFunctionInfo::kFormalParameterCountOffset));
506 ldr(code_reg,
507 MemOperand(code_reg, SharedFunctionInfo::kCodeOffset - kHeapObjectTag));
508 add(code_reg, code_reg, Operand(Code::kHeaderSize - kHeapObjectTag));
509
510 ParameterCount expected(expected_reg);
511 InvokeCode(code_reg, expected, actual, flag);
512}
513
514
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000515#ifdef ENABLE_DEBUGGER_SUPPORT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000516void MacroAssembler::SaveRegistersToMemory(RegList regs) {
517 ASSERT((regs & ~kJSCallerSaved) == 0);
518 // Copy the content of registers to memory location.
519 for (int i = 0; i < kNumJSCallerSaved; i++) {
520 int r = JSCallerSavedCode(i);
521 if ((regs & (1 << r)) != 0) {
522 Register reg = { r };
523 mov(ip, Operand(ExternalReference(Debug_Address::Register(i))));
524 str(reg, MemOperand(ip));
525 }
526 }
527}
528
529
530void MacroAssembler::RestoreRegistersFromMemory(RegList regs) {
531 ASSERT((regs & ~kJSCallerSaved) == 0);
532 // Copy the content of memory location to registers.
533 for (int i = kNumJSCallerSaved; --i >= 0;) {
534 int r = JSCallerSavedCode(i);
535 if ((regs & (1 << r)) != 0) {
536 Register reg = { r };
537 mov(ip, Operand(ExternalReference(Debug_Address::Register(i))));
538 ldr(reg, MemOperand(ip));
539 }
540 }
541}
542
543
544void MacroAssembler::CopyRegistersFromMemoryToStack(Register base,
545 RegList regs) {
546 ASSERT((regs & ~kJSCallerSaved) == 0);
547 // Copy the content of the memory location to the stack and adjust base.
548 for (int i = kNumJSCallerSaved; --i >= 0;) {
549 int r = JSCallerSavedCode(i);
550 if ((regs & (1 << r)) != 0) {
551 mov(ip, Operand(ExternalReference(Debug_Address::Register(i))));
552 ldr(ip, MemOperand(ip));
553 str(ip, MemOperand(base, 4, NegPreIndex));
554 }
555 }
556}
557
558
559void MacroAssembler::CopyRegistersFromStackToMemory(Register base,
560 Register scratch,
561 RegList regs) {
562 ASSERT((regs & ~kJSCallerSaved) == 0);
563 // Copy the content of the stack to the memory location and adjust base.
564 for (int i = 0; i < kNumJSCallerSaved; i++) {
565 int r = JSCallerSavedCode(i);
566 if ((regs & (1 << r)) != 0) {
567 mov(ip, Operand(ExternalReference(Debug_Address::Register(i))));
568 ldr(scratch, MemOperand(base, 4, PostIndex));
569 str(scratch, MemOperand(ip));
570 }
571 }
572}
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000573#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000574
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000575
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000576void MacroAssembler::PushTryHandler(CodeLocation try_location,
577 HandlerType type) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000578 // Adjust this code if not the case.
579 ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000580 // The pc (return address) is passed in register lr.
581 if (try_location == IN_JAVASCRIPT) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000582 if (type == TRY_CATCH_HANDLER) {
583 mov(r3, Operand(StackHandler::TRY_CATCH));
584 } else {
585 mov(r3, Operand(StackHandler::TRY_FINALLY));
586 }
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000587 ASSERT(StackHandlerConstants::kStateOffset == 1 * kPointerSize
588 && StackHandlerConstants::kFPOffset == 2 * kPointerSize
589 && StackHandlerConstants::kPCOffset == 3 * kPointerSize);
590 stm(db_w, sp, r3.bit() | fp.bit() | lr.bit());
591 // Save the current handler as the next handler.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000592 mov(r3, Operand(ExternalReference(Top::k_handler_address)));
593 ldr(r1, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000594 ASSERT(StackHandlerConstants::kNextOffset == 0);
595 push(r1);
596 // Link this handler as the new current one.
597 str(sp, MemOperand(r3));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000598 } else {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000599 // Must preserve r0-r4, r5-r7 are available.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000600 ASSERT(try_location == IN_JS_ENTRY);
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000601 // The frame pointer does not point to a JS frame so we save NULL
602 // for fp. We expect the code throwing an exception to check fp
603 // before dereferencing it to restore the context.
604 mov(ip, Operand(0)); // To save a NULL frame pointer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000605 mov(r6, Operand(StackHandler::ENTRY));
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000606 ASSERT(StackHandlerConstants::kStateOffset == 1 * kPointerSize
607 && StackHandlerConstants::kFPOffset == 2 * kPointerSize
608 && StackHandlerConstants::kPCOffset == 3 * kPointerSize);
609 stm(db_w, sp, r6.bit() | ip.bit() | lr.bit());
610 // Save the current handler as the next handler.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000611 mov(r7, Operand(ExternalReference(Top::k_handler_address)));
612 ldr(r6, MemOperand(r7));
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000613 ASSERT(StackHandlerConstants::kNextOffset == 0);
614 push(r6);
615 // Link this handler as the new current one.
616 str(sp, MemOperand(r7));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000617 }
618}
619
620
621Register MacroAssembler::CheckMaps(JSObject* object, Register object_reg,
622 JSObject* holder, Register holder_reg,
623 Register scratch,
624 Label* miss) {
625 // Make sure there's no overlap between scratch and the other
626 // registers.
627 ASSERT(!scratch.is(object_reg) && !scratch.is(holder_reg));
628
629 // Keep track of the current object in register reg.
630 Register reg = object_reg;
631 int depth = 1;
632
633 // Check the maps in the prototype chain.
634 // Traverse the prototype chain from the object and do map checks.
635 while (object != holder) {
636 depth++;
637
638 // Only global objects and objects that do not require access
639 // checks are allowed in stubs.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000640 ASSERT(object->IsJSGlobalProxy() || !object->IsAccessCheckNeeded());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000641
642 // Get the map of the current object.
643 ldr(scratch, FieldMemOperand(reg, HeapObject::kMapOffset));
644 cmp(scratch, Operand(Handle<Map>(object->map())));
645
646 // Branch on the result of the map check.
647 b(ne, miss);
648
649 // Check access rights to the global object. This has to happen
650 // after the map check so that we know that the object is
651 // actually a global object.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000652 if (object->IsJSGlobalProxy()) {
653 CheckAccessGlobalProxy(reg, scratch, miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000654 // Restore scratch register to be the map of the object. In the
655 // new space case below, we load the prototype from the map in
656 // the scratch register.
657 ldr(scratch, FieldMemOperand(reg, HeapObject::kMapOffset));
658 }
659
660 reg = holder_reg; // from now the object is in holder_reg
661 JSObject* prototype = JSObject::cast(object->GetPrototype());
662 if (Heap::InNewSpace(prototype)) {
663 // The prototype is in new space; we cannot store a reference
664 // to it in the code. Load it from the map.
665 ldr(reg, FieldMemOperand(scratch, Map::kPrototypeOffset));
666 } else {
667 // The prototype is in old space; load it directly.
668 mov(reg, Operand(Handle<JSObject>(prototype)));
669 }
670
671 // Go to the next object in the prototype chain.
672 object = prototype;
673 }
674
675 // Check the holder map.
676 ldr(scratch, FieldMemOperand(reg, HeapObject::kMapOffset));
677 cmp(scratch, Operand(Handle<Map>(object->map())));
678 b(ne, miss);
679
680 // Log the check depth.
681 LOG(IntEvent("check-maps-depth", depth));
682
683 // Perform security check for access to the global object and return
684 // the holder register.
685 ASSERT(object == holder);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000686 ASSERT(object->IsJSGlobalProxy() || !object->IsAccessCheckNeeded());
687 if (object->IsJSGlobalProxy()) {
688 CheckAccessGlobalProxy(reg, scratch, miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000689 }
690 return reg;
691}
692
693
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000694void MacroAssembler::CheckAccessGlobalProxy(Register holder_reg,
695 Register scratch,
696 Label* miss) {
697 Label same_contexts;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000698
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000699 ASSERT(!holder_reg.is(scratch));
700 ASSERT(!holder_reg.is(ip));
701 ASSERT(!scratch.is(ip));
702
703 // Load current lexical context from the stack frame.
704 ldr(scratch, MemOperand(fp, StandardFrameConstants::kContextOffset));
705 // In debug mode, make sure the lexical context is set.
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000706#ifdef DEBUG
707 cmp(scratch, Operand(0));
708 Check(ne, "we should not have an empty lexical context");
709#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000710
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000711 // Load the global context of the current context.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000712 int offset = Context::kHeaderSize + Context::GLOBAL_INDEX * kPointerSize;
713 ldr(scratch, FieldMemOperand(scratch, offset));
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000714 ldr(scratch, FieldMemOperand(scratch, GlobalObject::kGlobalContextOffset));
715
716 // Check the context is a global context.
717 if (FLAG_debug_code) {
718 // TODO(119): avoid push(holder_reg)/pop(holder_reg)
719 // Cannot use ip as a temporary in this verification code. Due to the fact
720 // that ip is clobbered as part of cmp with an object Operand.
721 push(holder_reg); // Temporarily save holder on the stack.
722 // Read the first word and compare to the global_context_map.
723 ldr(holder_reg, FieldMemOperand(scratch, HeapObject::kMapOffset));
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000724 LoadRoot(ip, Heap::kGlobalContextMapRootIndex);
725 cmp(holder_reg, ip);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000726 Check(eq, "JSGlobalObject::global_context should be a global context.");
727 pop(holder_reg); // Restore holder.
728 }
729
730 // Check if both contexts are the same.
731 ldr(ip, FieldMemOperand(holder_reg, JSGlobalProxy::kContextOffset));
732 cmp(scratch, Operand(ip));
733 b(eq, &same_contexts);
734
735 // Check the context is a global context.
736 if (FLAG_debug_code) {
737 // TODO(119): avoid push(holder_reg)/pop(holder_reg)
738 // Cannot use ip as a temporary in this verification code. Due to the fact
739 // that ip is clobbered as part of cmp with an object Operand.
740 push(holder_reg); // Temporarily save holder on the stack.
741 mov(holder_reg, ip); // Move ip to its holding place.
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000742 LoadRoot(ip, Heap::kNullValueRootIndex);
743 cmp(holder_reg, ip);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000744 Check(ne, "JSGlobalProxy::context() should not be null.");
745
746 ldr(holder_reg, FieldMemOperand(holder_reg, HeapObject::kMapOffset));
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000747 LoadRoot(ip, Heap::kGlobalContextMapRootIndex);
748 cmp(holder_reg, ip);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000749 Check(eq, "JSGlobalObject::global_context should be a global context.");
750 // Restore ip is not needed. ip is reloaded below.
751 pop(holder_reg); // Restore holder.
752 // Restore ip to holder's context.
753 ldr(ip, FieldMemOperand(holder_reg, JSGlobalProxy::kContextOffset));
754 }
755
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000756 // Check that the security token in the calling global object is
757 // compatible with the security token in the receiving global
758 // object.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000759 int token_offset = Context::kHeaderSize +
760 Context::SECURITY_TOKEN_INDEX * kPointerSize;
761
762 ldr(scratch, FieldMemOperand(scratch, token_offset));
763 ldr(ip, FieldMemOperand(ip, token_offset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000764 cmp(scratch, Operand(ip));
765 b(ne, miss);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000766
767 bind(&same_contexts);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000768}
769
770
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000771void MacroAssembler::AllocateObjectInNewSpace(int object_size,
772 Register result,
773 Register scratch1,
774 Register scratch2,
775 Label* gc_required,
776 bool tag_allocated_object) {
777 ASSERT(!result.is(scratch1));
778 ASSERT(!scratch1.is(scratch2));
779
780 // Load address of new object into result and allocation top address into
781 // scratch1.
782 ExternalReference new_space_allocation_top =
783 ExternalReference::new_space_allocation_top_address();
784 mov(scratch1, Operand(new_space_allocation_top));
785 ldr(result, MemOperand(scratch1));
786
787 // Calculate new top and bail out if new space is exhausted. Use result
788 // to calculate the new top.
789 ExternalReference new_space_allocation_limit =
790 ExternalReference::new_space_allocation_limit_address();
791 mov(scratch2, Operand(new_space_allocation_limit));
792 ldr(scratch2, MemOperand(scratch2));
793 add(result, result, Operand(object_size));
794 cmp(result, Operand(scratch2));
795 b(hi, gc_required);
796
797 // Update allocation top. result temporarily holds the new top,
798 str(result, MemOperand(scratch1));
799
800 // Tag and adjust back to start of new object.
801 if (tag_allocated_object) {
802 sub(result, result, Operand(object_size - kHeapObjectTag));
803 } else {
804 sub(result, result, Operand(object_size));
805 }
806}
807
808
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000809void MacroAssembler::CompareObjectType(Register function,
810 Register map,
811 Register type_reg,
812 InstanceType type) {
813 ldr(map, FieldMemOperand(function, HeapObject::kMapOffset));
814 ldrb(type_reg, FieldMemOperand(map, Map::kInstanceTypeOffset));
815 cmp(type_reg, Operand(type));
816}
817
818
819void MacroAssembler::TryGetFunctionPrototype(Register function,
820 Register result,
821 Register scratch,
822 Label* miss) {
823 // Check that the receiver isn't a smi.
824 BranchOnSmi(function, miss);
825
826 // Check that the function really is a function. Load map into result reg.
827 CompareObjectType(function, result, scratch, JS_FUNCTION_TYPE);
828 b(ne, miss);
829
830 // Make sure that the function has an instance prototype.
831 Label non_instance;
832 ldrb(scratch, FieldMemOperand(result, Map::kBitFieldOffset));
833 tst(scratch, Operand(1 << Map::kHasNonInstancePrototype));
834 b(ne, &non_instance);
835
836 // Get the prototype or initial map from the function.
837 ldr(result,
838 FieldMemOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
839
840 // If the prototype or initial map is the hole, don't return it and
841 // simply miss the cache instead. This will allow us to allocate a
842 // prototype object on-demand in the runtime system.
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000843 LoadRoot(ip, Heap::kTheHoleValueRootIndex);
844 cmp(result, ip);
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000845 b(eq, miss);
846
847 // If the function does not have an initial map, we're done.
848 Label done;
849 CompareObjectType(result, scratch, scratch, MAP_TYPE);
850 b(ne, &done);
851
852 // Get the prototype from the initial map.
853 ldr(result, FieldMemOperand(result, Map::kPrototypeOffset));
854 jmp(&done);
855
856 // Non-instance prototype: Fetch prototype from constructor field
857 // in initial map.
858 bind(&non_instance);
859 ldr(result, FieldMemOperand(result, Map::kConstructorOffset));
860
861 // All done.
862 bind(&done);
863}
864
865
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000866void MacroAssembler::CallStub(CodeStub* stub, Condition cond) {
kasper.lund7276f142008-07-30 08:49:36 +0000867 ASSERT(allow_stub_calls()); // stub calls are not allowed in some stubs
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000868 Call(stub->GetCode(), RelocInfo::CODE_TARGET, cond);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000869}
870
871
872void MacroAssembler::StubReturn(int argc) {
873 ASSERT(argc >= 1 && generating_stub());
874 if (argc > 1)
875 add(sp, sp, Operand((argc - 1) * kPointerSize));
876 Ret();
877}
878
mads.s.ager31e71382008-08-13 09:32:07 +0000879
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000880void MacroAssembler::IllegalOperation(int num_arguments) {
881 if (num_arguments > 0) {
882 add(sp, sp, Operand(num_arguments * kPointerSize));
883 }
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000884 LoadRoot(r0, Heap::kUndefinedValueRootIndex);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000885}
886
887
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000888void MacroAssembler::CallRuntime(Runtime::Function* f, int num_arguments) {
mads.s.ager31e71382008-08-13 09:32:07 +0000889 // All parameters are on the stack. r0 has the return value after call.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000890
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000891 // If the expected number of arguments of the runtime function is
892 // constant, we check that the actual number of arguments match the
893 // expectation.
894 if (f->nargs >= 0 && f->nargs != num_arguments) {
895 IllegalOperation(num_arguments);
896 return;
897 }
kasper.lund7276f142008-07-30 08:49:36 +0000898
mads.s.ager31e71382008-08-13 09:32:07 +0000899 Runtime::FunctionId function_id =
900 static_cast<Runtime::FunctionId>(f->stub_id);
901 RuntimeStub stub(function_id, num_arguments);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000902 CallStub(&stub);
903}
904
905
906void MacroAssembler::CallRuntime(Runtime::FunctionId fid, int num_arguments) {
907 CallRuntime(Runtime::FunctionForId(fid), num_arguments);
908}
909
910
mads.s.ager31e71382008-08-13 09:32:07 +0000911void MacroAssembler::TailCallRuntime(const ExternalReference& ext,
912 int num_arguments) {
913 // TODO(1236192): Most runtime routines don't need the number of
914 // arguments passed in because it is constant. At some point we
915 // should remove this need and make the runtime routine entry code
916 // smarter.
917 mov(r0, Operand(num_arguments));
918 JumpToBuiltin(ext);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000919}
920
921
922void MacroAssembler::JumpToBuiltin(const ExternalReference& builtin) {
923#if defined(__thumb__)
924 // Thumb mode builtin.
925 ASSERT((reinterpret_cast<intptr_t>(builtin.address()) & 1) == 1);
926#endif
927 mov(r1, Operand(builtin));
928 CEntryStub stub;
ager@chromium.org236ad962008-09-25 09:45:57 +0000929 Jump(stub.GetCode(), RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000930}
931
932
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000933Handle<Code> MacroAssembler::ResolveBuiltin(Builtins::JavaScript id,
934 bool* resolved) {
935 // Contract with compiled functions is that the function is passed in r1.
936 int builtins_offset =
937 JSBuiltinsObject::kJSBuiltinsOffset + (id * kPointerSize);
938 ldr(r1, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
939 ldr(r1, FieldMemOperand(r1, GlobalObject::kBuiltinsOffset));
940 ldr(r1, FieldMemOperand(r1, builtins_offset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000941
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000942 return Builtins::GetCode(id, resolved);
943}
944
945
946void MacroAssembler::InvokeBuiltin(Builtins::JavaScript id,
947 InvokeJSFlags flags) {
948 bool resolved;
949 Handle<Code> code = ResolveBuiltin(id, &resolved);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000950
951 if (flags == CALL_JS) {
ager@chromium.org236ad962008-09-25 09:45:57 +0000952 Call(code, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000953 } else {
954 ASSERT(flags == JUMP_JS);
ager@chromium.org236ad962008-09-25 09:45:57 +0000955 Jump(code, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000956 }
957
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000958 if (!resolved) {
959 const char* name = Builtins::GetName(id);
960 int argc = Builtins::GetArgumentsCount(id);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000961 uint32_t flags =
962 Bootstrapper::FixupFlagsArgumentsCount::encode(argc) |
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000963 Bootstrapper::FixupFlagsIsPCRelative::encode(true) |
964 Bootstrapper::FixupFlagsUseCodeObject::encode(false);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000965 Unresolved entry = { pc_offset() - sizeof(Instr), flags, name };
966 unresolved_.Add(entry);
967 }
968}
969
970
971void MacroAssembler::GetBuiltinEntry(Register target, Builtins::JavaScript id) {
972 bool resolved;
973 Handle<Code> code = ResolveBuiltin(id, &resolved);
974
975 mov(target, Operand(code));
976 if (!resolved) {
977 const char* name = Builtins::GetName(id);
978 int argc = Builtins::GetArgumentsCount(id);
979 uint32_t flags =
980 Bootstrapper::FixupFlagsArgumentsCount::encode(argc) |
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000981 Bootstrapper::FixupFlagsIsPCRelative::encode(true) |
982 Bootstrapper::FixupFlagsUseCodeObject::encode(true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000983 Unresolved entry = { pc_offset() - sizeof(Instr), flags, name };
984 unresolved_.Add(entry);
985 }
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000986
987 add(target, target, Operand(Code::kHeaderSize - kHeapObjectTag));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000988}
989
990
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000991void MacroAssembler::SetCounter(StatsCounter* counter, int value,
992 Register scratch1, Register scratch2) {
993 if (FLAG_native_code_counters && counter->Enabled()) {
994 mov(scratch1, Operand(value));
995 mov(scratch2, Operand(ExternalReference(counter)));
996 str(scratch1, MemOperand(scratch2));
997 }
998}
999
1000
1001void MacroAssembler::IncrementCounter(StatsCounter* counter, int value,
1002 Register scratch1, Register scratch2) {
1003 ASSERT(value > 0);
1004 if (FLAG_native_code_counters && counter->Enabled()) {
1005 mov(scratch2, Operand(ExternalReference(counter)));
1006 ldr(scratch1, MemOperand(scratch2));
1007 add(scratch1, scratch1, Operand(value));
1008 str(scratch1, MemOperand(scratch2));
1009 }
1010}
1011
1012
1013void MacroAssembler::DecrementCounter(StatsCounter* counter, int value,
1014 Register scratch1, Register scratch2) {
1015 ASSERT(value > 0);
1016 if (FLAG_native_code_counters && counter->Enabled()) {
1017 mov(scratch2, Operand(ExternalReference(counter)));
1018 ldr(scratch1, MemOperand(scratch2));
1019 sub(scratch1, scratch1, Operand(value));
1020 str(scratch1, MemOperand(scratch2));
1021 }
1022}
1023
1024
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001025void MacroAssembler::Assert(Condition cc, const char* msg) {
1026 if (FLAG_debug_code)
1027 Check(cc, msg);
1028}
1029
1030
1031void MacroAssembler::Check(Condition cc, const char* msg) {
1032 Label L;
1033 b(cc, &L);
1034 Abort(msg);
1035 // will not return here
1036 bind(&L);
1037}
1038
1039
1040void MacroAssembler::Abort(const char* msg) {
1041 // We want to pass the msg string like a smi to avoid GC
1042 // problems, however msg is not guaranteed to be aligned
1043 // properly. Instead, we pass an aligned pointer that is
ager@chromium.org32912102009-01-16 10:38:43 +00001044 // a proper v8 smi, but also pass the alignment difference
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001045 // from the real pointer as a smi.
1046 intptr_t p1 = reinterpret_cast<intptr_t>(msg);
1047 intptr_t p0 = (p1 & ~kSmiTagMask) + kSmiTag;
1048 ASSERT(reinterpret_cast<Object*>(p0)->IsSmi());
1049#ifdef DEBUG
1050 if (msg != NULL) {
1051 RecordComment("Abort message: ");
1052 RecordComment(msg);
1053 }
1054#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001055 mov(r0, Operand(p0));
1056 push(r0);
1057 mov(r0, Operand(Smi::FromInt(p1 - p0)));
mads.s.ager31e71382008-08-13 09:32:07 +00001058 push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001059 CallRuntime(Runtime::kAbort, 2);
1060 // will not return here
1061}
1062
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001063
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001064} } // namespace v8::internal