blob: 21fcea420857d34597ebb854d2ad412e96e2200b [file] [log] [blame]
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001// Copyright 2006-2008 Google Inc. All Rights Reserved.
2// 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
37DECLARE_bool(debug_code);
38DECLARE_bool(optimize_locals);
39
40
41// Give alias names to registers
42Register cp = { 8 }; // JavaScript context pointer
43Register pp = { 10 }; // parameter pointer
44
45
46MacroAssembler::MacroAssembler(void* buffer, int size)
47 : Assembler(buffer, size),
48 unresolved_(0),
kasper.lund7276f142008-07-30 08:49:36 +000049 generating_stub_(false),
50 allow_stub_calls_(true) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000051}
52
53
54// We always generate arm code, never thumb code, even if V8 is compiled to
55// thumb, so we require inter-working support
56#if defined(__thumb__) && !defined(__THUMB_INTERWORK__)
57#error "flag -mthumb-interwork missing"
58#endif
59
60
61// We do not support thumb inter-working with an arm architecture not supporting
62// the blx instruction (below v5t)
63#if defined(__THUMB_INTERWORK__)
64#if !defined(__ARM_ARCH_5T__) && !defined(__ARM_ARCH_5TE__)
65// add tests for other versions above v5t as required
66#error "for thumb inter-working we require architecture v5t or above"
67#endif
68#endif
69
70
71// Using blx may yield better code, so use it when required or when available
72#if defined(__THUMB_INTERWORK__) || defined(__ARM_ARCH_5__)
73#define USE_BLX 1
74#endif
75
76// Using bx does not yield better code, so use it only when required
77#if defined(__THUMB_INTERWORK__)
78#define USE_BX 1
79#endif
80
81
82void MacroAssembler::Jump(Register target, Condition cond) {
83#if USE_BX
84 bx(target, cond);
85#else
86 mov(pc, Operand(target), LeaveCC, cond);
87#endif
88}
89
90
91void MacroAssembler::Jump(intptr_t target, RelocMode rmode, Condition cond) {
92#if USE_BX
93 mov(ip, Operand(target, rmode), LeaveCC, cond);
94 bx(ip, cond);
95#else
96 mov(pc, Operand(target, rmode), LeaveCC, cond);
97#endif
98}
99
100
101void MacroAssembler::Jump(byte* target, RelocMode rmode, Condition cond) {
102 ASSERT(!is_code_target(rmode));
103 Jump(reinterpret_cast<intptr_t>(target), rmode, cond);
104}
105
106
107void MacroAssembler::Jump(Handle<Code> code, RelocMode rmode, Condition cond) {
108 ASSERT(is_code_target(rmode));
109 // '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
125void MacroAssembler::Call(intptr_t target, RelocMode rmode, Condition cond) {
126#if !defined(__arm__)
127 if (rmode == runtime_entry) {
128 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
155void MacroAssembler::Call(byte* target, RelocMode rmode, Condition cond) {
156 ASSERT(!is_code_target(rmode));
157 Call(reinterpret_cast<intptr_t>(target), rmode, cond);
158}
159
160
161void MacroAssembler::Call(Handle<Code> code, RelocMode rmode, Condition cond) {
162 ASSERT(is_code_target(rmode));
163 // 'code' is always generated ARM code, never THUMB code
164 Call(reinterpret_cast<intptr_t>(code.location()), rmode, cond);
165}
166
167
168void MacroAssembler::Ret() {
169#if USE_BX
170 bx(lr);
171#else
172 mov(pc, Operand(lr));
173#endif
174}
175
176
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000177// Will clobber 4 registers: object, offset, scratch, ip. The
178// register 'object' contains a heap object pointer. The heap object
179// tag is shifted away.
180void MacroAssembler::RecordWrite(Register object, Register offset,
181 Register scratch) {
182 // This is how much we shift the remembered set bit offset to get the
183 // offset of the word in the remembered set. We divide by kBitsPerInt (32,
184 // shift right 5) and then multiply by kIntSize (4, shift left 2).
185 const int kRSetWordShift = 3;
186
187 Label fast, done;
188
kasper.lund7276f142008-07-30 08:49:36 +0000189 // First, test that the object is not in the new space. We cannot set
190 // remembered set bits in the new space.
191 // object: heap object pointer (with tag)
192 // offset: offset to store location from the object
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000193 and_(scratch, object, Operand(Heap::NewSpaceMask()));
194 cmp(scratch, Operand(ExternalReference::new_space_start()));
195 b(eq, &done);
196
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000197 // Compute the bit offset in the remembered set.
kasper.lund7276f142008-07-30 08:49:36 +0000198 // object: heap object pointer (with tag)
199 // offset: offset to store location from the object
200 mov(ip, Operand(Page::kPageAlignmentMask)); // load mask only once
201 and_(scratch, object, Operand(ip)); // offset into page of the object
202 add(offset, scratch, Operand(offset)); // add offset into the object
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000203 mov(offset, Operand(offset, LSR, kObjectAlignmentBits));
204
205 // Compute the page address from the heap object pointer.
kasper.lund7276f142008-07-30 08:49:36 +0000206 // object: heap object pointer (with tag)
207 // offset: bit offset of store position in the remembered set
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000208 bic(object, object, Operand(ip));
209
210 // If the bit offset lies beyond the normal remembered set range, it is in
211 // the extra remembered set area of a large object.
kasper.lund7276f142008-07-30 08:49:36 +0000212 // object: page start
213 // offset: bit offset of store position in the remembered set
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000214 cmp(offset, Operand(Page::kPageSize / kPointerSize));
215 b(lt, &fast);
216
217 // Adjust the bit offset to be relative to the start of the extra
218 // remembered set and the start address to be the address of the extra
219 // remembered set.
220 sub(offset, offset, Operand(Page::kPageSize / kPointerSize));
221 // Load the array length into 'scratch' and multiply by four to get the
222 // size in bytes of the elements.
223 ldr(scratch, MemOperand(object, Page::kObjectStartOffset
224 + FixedArray::kLengthOffset));
225 mov(scratch, Operand(scratch, LSL, kObjectAlignmentBits));
226 // Add the page header (including remembered set), array header, and array
227 // body size to the page address.
228 add(object, object, Operand(Page::kObjectStartOffset
229 + Array::kHeaderSize));
230 add(object, object, Operand(scratch));
231
232 bind(&fast);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000233 // Get address of the rset word.
kasper.lund7276f142008-07-30 08:49:36 +0000234 // object: start of the remembered set (page start for the fast case)
235 // offset: bit offset of store position in the remembered set
236 bic(scratch, offset, Operand(kBitsPerInt - 1)); // clear the bit offset
237 add(object, object, Operand(scratch, LSR, kRSetWordShift));
238 // Get bit offset in the rset word.
239 // object: address of remembered set word
240 // offset: bit offset of store position
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000241 and_(offset, offset, Operand(kBitsPerInt - 1));
242
243 ldr(scratch, MemOperand(object));
244 mov(ip, Operand(1));
245 orr(scratch, scratch, Operand(ip, LSL, offset));
246 str(scratch, MemOperand(object));
247
248 bind(&done);
249}
250
251
kasper.lund7276f142008-07-30 08:49:36 +0000252void MacroAssembler::EnterJSFrame(int argc) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000253 // Generate code entering a JS function called from a JS function
254 // stack: receiver, arguments
255 // r0: number of arguments (not including function, nor receiver)
256 // r1: preserved
257 // sp: stack pointer
258 // fp: frame pointer
259 // cp: callee's context
260 // pp: caller's parameter pointer
261 // lr: return address
262
263 // compute parameter pointer before making changes
264 // ip = sp + kPointerSize*(args_len+1); // +1 for receiver
265 add(ip, sp, Operand(r0, LSL, kPointerSizeLog2));
266 add(ip, ip, Operand(kPointerSize));
267
268 // push extra parameters if we don't have enough
269 // (this can only happen if argc > 0 to begin with)
270 if (argc > 0) {
271 Label loop, done;
272
273 // assume enough arguments to be the most common case
274 sub(r2, r0, Operand(argc), SetCC); // number of missing arguments
275 b(ge, &done); // enough arguments
276
277 // not enough arguments
278 mov(r3, Operand(Factory::undefined_value()));
279 bind(&loop);
280 push(r3);
281 add(r2, r2, Operand(1), SetCC);
282 b(lt, &loop);
283
284 bind(&done);
285 }
286
287 mov(r3, Operand(r0)); // args_len to be saved
288 mov(r2, Operand(cp)); // context to be saved
289
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000290 // push in reverse order: context (r2), args_len (r3), caller_pp, caller_fp,
kasper.lund7276f142008-07-30 08:49:36 +0000291 // sp_on_exit (ip == pp, may be patched on exit), return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000292 stm(db_w, sp, r2.bit() | r3.bit() | pp.bit() | fp.bit() |
kasper.lund7276f142008-07-30 08:49:36 +0000293 ip.bit() | lr.bit());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000294
295 // Setup new frame pointer.
296 add(fp, sp, Operand(-StandardFrameConstants::kContextOffset));
297 mov(pp, Operand(ip)); // setup new parameter pointer
298 mov(r0, Operand(0)); // spare slot to store caller code object during GC
mads.s.ager31e71382008-08-13 09:32:07 +0000299 push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000300 // r1: preserved
301}
302
303
kasper.lund7276f142008-07-30 08:49:36 +0000304void MacroAssembler::ExitJSFrame(ExitJSFlag flag) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000305 // r0: result
306 // sp: stack pointer
307 // fp: frame pointer
308 // pp: parameter pointer
309
kasper.lund7276f142008-07-30 08:49:36 +0000310 if (flag == DO_NOT_RETURN) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000311 add(r3, fp, Operand(JavaScriptFrameConstants::kSavedRegistersOffset));
312 }
313
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000314 if (flag == DO_NOT_RETURN) {
315 // restore sp as caller_sp (not as pp)
316 str(r3, MemOperand(fp, JavaScriptFrameConstants::kSPOnExitOffset));
317 }
318
319 if (flag == DO_NOT_RETURN && generating_stub()) {
320 // If we're generating a stub, we need to preserve the link
321 // register to be able to return to the place the stub was called
322 // from.
323 mov(ip, Operand(lr));
324 }
325
326 mov(sp, Operand(fp)); // respect ABI stack constraint
327 ldm(ia, sp, pp.bit() | fp.bit() | sp.bit() |
328 ((flag == RETURN) ? pc.bit() : lr.bit()));
329
330 if (flag == DO_NOT_RETURN && generating_stub()) {
331 // Return to the place where the stub was called without
332 // clobbering the value of the link register.
333 mov(pc, Operand(ip));
334 }
335
336 // r0: result
337 // sp: points to function arg (if return) or to last arg (if no return)
338 // fp: restored frame pointer
339 // pp: restored parameter pointer
340}
341
342
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000343void MacroAssembler::InvokePrologue(const ParameterCount& expected,
344 const ParameterCount& actual,
345 Handle<Code> code_constant,
346 Register code_reg,
347 Label* done,
348 InvokeFlag flag) {
349 if (actual.is_immediate()) {
350 mov(r0, Operand(actual.immediate())); // Push the number of arguments.
351 } else {
352 if (!actual.reg().is(r0)) {
353 mov(r0, Operand(actual.reg()));
354 }
355 }
356}
357
358
359void MacroAssembler::InvokeCode(Register code,
360 const ParameterCount& expected,
361 const ParameterCount& actual,
362 InvokeFlag flag) {
363 Label done;
364
365 InvokePrologue(expected, actual, Handle<Code>::null(), code, &done, flag);
366 if (flag == CALL_FUNCTION) {
367 Call(code);
368 } else {
369 ASSERT(flag == JUMP_FUNCTION);
370 Jump(code);
371 }
372
373 // Continue here if InvokePrologue does handle the invocation due to
374 // mismatched parameter counts.
375 bind(&done);
376}
377
378
379void MacroAssembler::InvokeCode(Handle<Code> code,
380 const ParameterCount& expected,
381 const ParameterCount& actual,
382 RelocMode rmode,
383 InvokeFlag flag) {
384 Label done;
385
386 InvokePrologue(expected, actual, code, no_reg, &done, flag);
387 if (flag == CALL_FUNCTION) {
388 Call(code, rmode);
389 } else {
390 Jump(code, rmode);
391 }
392
393 // Continue here if InvokePrologue does handle the invocation due to
394 // mismatched parameter counts.
395 bind(&done);
396}
397
398
399void MacroAssembler::InvokeFunction(Register fun,
400 const ParameterCount& actual,
401 InvokeFlag flag) {
402 // Contract with called JS functions requires that function is passed in r1.
403 ASSERT(fun.is(r1));
404
405 Register code_reg = r3;
406 Register expected_reg = r2;
407
408 // Make sure that the code and expected registers do not collide with the
409 // actual register being passed in.
410 if (actual.is_reg()) {
411 if (actual.reg().is(code_reg)) {
412 code_reg = r4;
413 } else if (actual.reg().is(expected_reg)) {
414 expected_reg = r4;
415 }
416 }
417
418 ldr(code_reg, FieldMemOperand(r1, JSFunction::kSharedFunctionInfoOffset));
419 ldr(cp, FieldMemOperand(r1, JSFunction::kContextOffset));
420 ldr(expected_reg,
421 FieldMemOperand(code_reg,
422 SharedFunctionInfo::kFormalParameterCountOffset));
423 ldr(code_reg,
424 MemOperand(code_reg, SharedFunctionInfo::kCodeOffset - kHeapObjectTag));
425 add(code_reg, code_reg, Operand(Code::kHeaderSize - kHeapObjectTag));
426
427 ParameterCount expected(expected_reg);
428 InvokeCode(code_reg, expected, actual, flag);
429}
430
431
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000432void MacroAssembler::SaveRegistersToMemory(RegList regs) {
433 ASSERT((regs & ~kJSCallerSaved) == 0);
434 // Copy the content of registers to memory location.
435 for (int i = 0; i < kNumJSCallerSaved; i++) {
436 int r = JSCallerSavedCode(i);
437 if ((regs & (1 << r)) != 0) {
438 Register reg = { r };
439 mov(ip, Operand(ExternalReference(Debug_Address::Register(i))));
440 str(reg, MemOperand(ip));
441 }
442 }
443}
444
445
446void MacroAssembler::RestoreRegistersFromMemory(RegList regs) {
447 ASSERT((regs & ~kJSCallerSaved) == 0);
448 // Copy the content of memory location to registers.
449 for (int i = kNumJSCallerSaved; --i >= 0;) {
450 int r = JSCallerSavedCode(i);
451 if ((regs & (1 << r)) != 0) {
452 Register reg = { r };
453 mov(ip, Operand(ExternalReference(Debug_Address::Register(i))));
454 ldr(reg, MemOperand(ip));
455 }
456 }
457}
458
459
460void MacroAssembler::CopyRegistersFromMemoryToStack(Register base,
461 RegList regs) {
462 ASSERT((regs & ~kJSCallerSaved) == 0);
463 // Copy the content of the memory location to the stack and adjust base.
464 for (int i = kNumJSCallerSaved; --i >= 0;) {
465 int r = JSCallerSavedCode(i);
466 if ((regs & (1 << r)) != 0) {
467 mov(ip, Operand(ExternalReference(Debug_Address::Register(i))));
468 ldr(ip, MemOperand(ip));
469 str(ip, MemOperand(base, 4, NegPreIndex));
470 }
471 }
472}
473
474
475void MacroAssembler::CopyRegistersFromStackToMemory(Register base,
476 Register scratch,
477 RegList regs) {
478 ASSERT((regs & ~kJSCallerSaved) == 0);
479 // Copy the content of the stack to the memory location and adjust base.
480 for (int i = 0; i < kNumJSCallerSaved; i++) {
481 int r = JSCallerSavedCode(i);
482 if ((regs & (1 << r)) != 0) {
483 mov(ip, Operand(ExternalReference(Debug_Address::Register(i))));
484 ldr(scratch, MemOperand(base, 4, PostIndex));
485 str(scratch, MemOperand(ip));
486 }
487 }
488}
489
490
491void MacroAssembler::PushTryHandler(CodeLocation try_location,
492 HandlerType type) {
493 ASSERT(StackHandlerConstants::kSize == 6 * kPointerSize); // adjust this code
494 // The pc (return address) is passed in register lr.
495 if (try_location == IN_JAVASCRIPT) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000496 stm(db_w, sp, pp.bit() | fp.bit() | lr.bit());
497 if (type == TRY_CATCH_HANDLER) {
498 mov(r3, Operand(StackHandler::TRY_CATCH));
499 } else {
500 mov(r3, Operand(StackHandler::TRY_FINALLY));
501 }
502 push(r3); // state
503 mov(r3, Operand(ExternalReference(Top::k_handler_address)));
504 ldr(r1, MemOperand(r3));
505 push(r1); // next sp
506 str(sp, MemOperand(r3)); // chain handler
mads.s.ager31e71382008-08-13 09:32:07 +0000507 mov(r0, Operand(Smi::FromInt(StackHandler::kCodeNotPresent))); // new TOS
508 push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000509 } else {
510 // Must preserve r0-r3, r5-r7 are available.
511 ASSERT(try_location == IN_JS_ENTRY);
512 // The parameter pointer is meaningless here and fp does not point to a JS
513 // frame. So we save NULL for both pp and fp. We expect the code throwing an
514 // exception to check fp before dereferencing it to restore the context.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000515 mov(pp, Operand(0)); // set pp to NULL
516 mov(ip, Operand(0)); // to save a NULL fp
517 stm(db_w, sp, pp.bit() | ip.bit() | lr.bit());
518 mov(r6, Operand(StackHandler::ENTRY));
519 push(r6); // state
520 mov(r7, Operand(ExternalReference(Top::k_handler_address)));
521 ldr(r6, MemOperand(r7));
522 push(r6); // next sp
523 str(sp, MemOperand(r7)); // chain handler
mads.s.ager31e71382008-08-13 09:32:07 +0000524 mov(r5, Operand(Smi::FromInt(StackHandler::kCodeNotPresent))); // new TOS
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000525 push(r5); // flush TOS
526 }
527}
528
529
530Register MacroAssembler::CheckMaps(JSObject* object, Register object_reg,
531 JSObject* holder, Register holder_reg,
532 Register scratch,
533 Label* miss) {
534 // Make sure there's no overlap between scratch and the other
535 // registers.
536 ASSERT(!scratch.is(object_reg) && !scratch.is(holder_reg));
537
538 // Keep track of the current object in register reg.
539 Register reg = object_reg;
540 int depth = 1;
541
542 // Check the maps in the prototype chain.
543 // Traverse the prototype chain from the object and do map checks.
544 while (object != holder) {
545 depth++;
546
547 // Only global objects and objects that do not require access
548 // checks are allowed in stubs.
549 ASSERT(object->IsJSGlobalObject() || !object->IsAccessCheckNeeded());
550
551 // Get the map of the current object.
552 ldr(scratch, FieldMemOperand(reg, HeapObject::kMapOffset));
553 cmp(scratch, Operand(Handle<Map>(object->map())));
554
555 // Branch on the result of the map check.
556 b(ne, miss);
557
558 // Check access rights to the global object. This has to happen
559 // after the map check so that we know that the object is
560 // actually a global object.
561 if (object->IsJSGlobalObject()) {
562 CheckAccessGlobal(reg, scratch, miss);
563 // Restore scratch register to be the map of the object. In the
564 // new space case below, we load the prototype from the map in
565 // the scratch register.
566 ldr(scratch, FieldMemOperand(reg, HeapObject::kMapOffset));
567 }
568
569 reg = holder_reg; // from now the object is in holder_reg
570 JSObject* prototype = JSObject::cast(object->GetPrototype());
571 if (Heap::InNewSpace(prototype)) {
572 // The prototype is in new space; we cannot store a reference
573 // to it in the code. Load it from the map.
574 ldr(reg, FieldMemOperand(scratch, Map::kPrototypeOffset));
575 } else {
576 // The prototype is in old space; load it directly.
577 mov(reg, Operand(Handle<JSObject>(prototype)));
578 }
579
580 // Go to the next object in the prototype chain.
581 object = prototype;
582 }
583
584 // Check the holder map.
585 ldr(scratch, FieldMemOperand(reg, HeapObject::kMapOffset));
586 cmp(scratch, Operand(Handle<Map>(object->map())));
587 b(ne, miss);
588
589 // Log the check depth.
590 LOG(IntEvent("check-maps-depth", depth));
591
592 // Perform security check for access to the global object and return
593 // the holder register.
594 ASSERT(object == holder);
595 ASSERT(object->IsJSGlobalObject() || !object->IsAccessCheckNeeded());
596 if (object->IsJSGlobalObject()) {
597 CheckAccessGlobal(reg, scratch, miss);
598 }
599 return reg;
600}
601
602
603void MacroAssembler::CheckAccessGlobal(Register holder_reg,
604 Register scratch,
605 Label* miss) {
606 ASSERT(!holder_reg.is(scratch));
607
608 // Load the security context.
609 mov(scratch, Operand(Top::security_context_address()));
610 ldr(scratch, MemOperand(scratch));
611 // In debug mode, make sure the security context is set.
612 if (kDebug) {
613 cmp(scratch, Operand(0));
614 Check(ne, "we should not have an empty security context");
615 }
616
617 // Load the global object of the security context.
618 int offset = Context::kHeaderSize + Context::GLOBAL_INDEX * kPointerSize;
619 ldr(scratch, FieldMemOperand(scratch, offset));
620 // Check that the security token in the calling global object is
621 // compatible with the security token in the receiving global
622 // object.
623 ldr(scratch, FieldMemOperand(scratch, JSGlobalObject::kSecurityTokenOffset));
624 ldr(ip, FieldMemOperand(holder_reg, JSGlobalObject::kSecurityTokenOffset));
625 cmp(scratch, Operand(ip));
626 b(ne, miss);
627}
628
629
630void MacroAssembler::CallStub(CodeStub* stub) {
kasper.lund7276f142008-07-30 08:49:36 +0000631 ASSERT(allow_stub_calls()); // stub calls are not allowed in some stubs
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000632 Call(stub->GetCode(), code_target);
633}
634
635
636void MacroAssembler::CallJSExitStub(CodeStub* stub) {
kasper.lund7276f142008-07-30 08:49:36 +0000637 ASSERT(allow_stub_calls()); // stub calls are not allowed in some stubs
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000638 Call(stub->GetCode(), exit_js_frame);
639}
640
641
642void MacroAssembler::StubReturn(int argc) {
643 ASSERT(argc >= 1 && generating_stub());
644 if (argc > 1)
645 add(sp, sp, Operand((argc - 1) * kPointerSize));
646 Ret();
647}
648
mads.s.ager31e71382008-08-13 09:32:07 +0000649
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000650void MacroAssembler::CallRuntime(Runtime::Function* f, int num_arguments) {
mads.s.ager31e71382008-08-13 09:32:07 +0000651 // All parameters are on the stack. r0 has the return value after call.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000652
mads.s.ager31e71382008-08-13 09:32:07 +0000653 // Either the expected number of arguments is unknown, or the actual
654 // number of arguments match the expectation.
655 ASSERT(f->nargs < 0 || f->nargs == num_arguments);
kasper.lund7276f142008-07-30 08:49:36 +0000656
mads.s.ager31e71382008-08-13 09:32:07 +0000657 Runtime::FunctionId function_id =
658 static_cast<Runtime::FunctionId>(f->stub_id);
659 RuntimeStub stub(function_id, num_arguments);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000660 CallStub(&stub);
661}
662
663
664void MacroAssembler::CallRuntime(Runtime::FunctionId fid, int num_arguments) {
665 CallRuntime(Runtime::FunctionForId(fid), num_arguments);
666}
667
668
mads.s.ager31e71382008-08-13 09:32:07 +0000669void MacroAssembler::TailCallRuntime(const ExternalReference& ext,
670 int num_arguments) {
671 // TODO(1236192): Most runtime routines don't need the number of
672 // arguments passed in because it is constant. At some point we
673 // should remove this need and make the runtime routine entry code
674 // smarter.
675 mov(r0, Operand(num_arguments));
676 JumpToBuiltin(ext);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000677}
678
679
680void MacroAssembler::JumpToBuiltin(const ExternalReference& builtin) {
681#if defined(__thumb__)
682 // Thumb mode builtin.
683 ASSERT((reinterpret_cast<intptr_t>(builtin.address()) & 1) == 1);
684#endif
685 mov(r1, Operand(builtin));
686 CEntryStub stub;
687 Jump(stub.GetCode(), code_target);
688}
689
690
691void MacroAssembler::InvokeBuiltin(const char* name,
692 int argc,
693 InvokeJSFlags flags) {
694 Handle<String> symbol = Factory::LookupAsciiSymbol(name);
695 Object* object = Top::security_context_builtins()->GetProperty(*symbol);
696 bool unresolved = true;
697 Code* code = Builtins::builtin(Builtins::Illegal);
698
699 if (object->IsJSFunction()) {
700 Handle<JSFunction> function(JSFunction::cast(object));
701 if (function->is_compiled() || CompileLazy(function, CLEAR_EXCEPTION)) {
702 code = function->code();
703 unresolved = false;
704 }
705 }
706
707 if (flags == CALL_JS) {
708 Call(Handle<Code>(code), code_target);
709 } else {
710 ASSERT(flags == JUMP_JS);
711 Jump(Handle<Code>(code), code_target);
712 }
713
714 if (unresolved) {
715 uint32_t flags =
716 Bootstrapper::FixupFlagsArgumentsCount::encode(argc) |
717 Bootstrapper::FixupFlagsIsPCRelative::encode(false);
718 Unresolved entry = { pc_offset() - sizeof(Instr), flags, name };
719 unresolved_.Add(entry);
720 }
721}
722
723
724void MacroAssembler::Assert(Condition cc, const char* msg) {
725 if (FLAG_debug_code)
726 Check(cc, msg);
727}
728
729
730void MacroAssembler::Check(Condition cc, const char* msg) {
731 Label L;
732 b(cc, &L);
733 Abort(msg);
734 // will not return here
735 bind(&L);
736}
737
738
739void MacroAssembler::Abort(const char* msg) {
740 // We want to pass the msg string like a smi to avoid GC
741 // problems, however msg is not guaranteed to be aligned
742 // properly. Instead, we pass an aligned pointer that is
743 // a proper v8 smi, but also pass the aligment difference
744 // from the real pointer as a smi.
745 intptr_t p1 = reinterpret_cast<intptr_t>(msg);
746 intptr_t p0 = (p1 & ~kSmiTagMask) + kSmiTag;
747 ASSERT(reinterpret_cast<Object*>(p0)->IsSmi());
748#ifdef DEBUG
749 if (msg != NULL) {
750 RecordComment("Abort message: ");
751 RecordComment(msg);
752 }
753#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000754 mov(r0, Operand(p0));
755 push(r0);
756 mov(r0, Operand(Smi::FromInt(p1 - p0)));
mads.s.ager31e71382008-08-13 09:32:07 +0000757 push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000758 CallRuntime(Runtime::kAbort, 2);
759 // will not return here
760}
761
762} } // namespace v8::internal