ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 1 | // Copyright 2006-2009 the V8 project authors. All rights reserved. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 35 | namespace v8 { |
| 36 | namespace internal { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 37 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 38 | MacroAssembler::MacroAssembler(void* buffer, int size) |
| 39 | : Assembler(buffer, size), |
| 40 | unresolved_(0), |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 41 | generating_stub_(false), |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 42 | allow_stub_calls_(true), |
| 43 | code_object_(Heap::undefined_value()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 44 | } |
| 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.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame^] | 49 | #if defined(__thumb__) && !defined(USE_THUMB_INTERWORK) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 50 | #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.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame^] | 56 | #if defined(USE_THUMB_INTERWORK) |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 57 | #if !defined(__ARM_ARCH_5T__) && \ |
| 58 | !defined(__ARM_ARCH_5TE__) && \ |
| 59 | !defined(__ARM_ARCH_7A__) && \ |
| 60 | !defined(__ARM_ARCH_7__) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 61 | // add tests for other versions above v5t as required |
| 62 | #error "for thumb inter-working we require architecture v5t or above" |
| 63 | #endif |
| 64 | #endif |
| 65 | |
| 66 | |
| 67 | // Using blx may yield better code, so use it when required or when available |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame^] | 68 | #if defined(USE_THUMB_INTERWORK) || defined(__ARM_ARCH_5__) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 69 | #define USE_BLX 1 |
| 70 | #endif |
| 71 | |
| 72 | // Using bx does not yield better code, so use it only when required |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame^] | 73 | #if defined(USE_THUMB_INTERWORK) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 74 | #define USE_BX 1 |
| 75 | #endif |
| 76 | |
| 77 | |
| 78 | void 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.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 87 | void MacroAssembler::Jump(intptr_t target, RelocInfo::Mode rmode, |
| 88 | Condition cond) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 89 | #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.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 98 | void MacroAssembler::Jump(byte* target, RelocInfo::Mode rmode, |
| 99 | Condition cond) { |
| 100 | ASSERT(!RelocInfo::IsCodeTarget(rmode)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 101 | Jump(reinterpret_cast<intptr_t>(target), rmode, cond); |
| 102 | } |
| 103 | |
| 104 | |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 105 | void MacroAssembler::Jump(Handle<Code> code, RelocInfo::Mode rmode, |
| 106 | Condition cond) { |
| 107 | ASSERT(RelocInfo::IsCodeTarget(rmode)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 108 | // 'code' is always generated ARM code, never THUMB code |
| 109 | Jump(reinterpret_cast<intptr_t>(code.location()), rmode, cond); |
| 110 | } |
| 111 | |
| 112 | |
| 113 | void 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.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 124 | void MacroAssembler::Call(intptr_t target, RelocInfo::Mode rmode, |
| 125 | Condition cond) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 126 | // 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.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 130 | // 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). |
| 135 | ASSERT(kTargetAddrToReturnAddrDist == sizeof(Instr)); |
| 136 | } |
| 137 | |
| 138 | |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 139 | void MacroAssembler::Call(byte* target, RelocInfo::Mode rmode, |
| 140 | Condition cond) { |
| 141 | ASSERT(!RelocInfo::IsCodeTarget(rmode)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 142 | Call(reinterpret_cast<intptr_t>(target), rmode, cond); |
| 143 | } |
| 144 | |
| 145 | |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 146 | void MacroAssembler::Call(Handle<Code> code, RelocInfo::Mode rmode, |
| 147 | Condition cond) { |
| 148 | ASSERT(RelocInfo::IsCodeTarget(rmode)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 149 | // 'code' is always generated ARM code, never THUMB code |
| 150 | Call(reinterpret_cast<intptr_t>(code.location()), rmode, cond); |
| 151 | } |
| 152 | |
| 153 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 154 | void MacroAssembler::Ret(Condition cond) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 155 | #if USE_BX |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 156 | bx(lr, cond); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 157 | #else |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 158 | mov(pc, Operand(lr), LeaveCC, cond); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 159 | #endif |
| 160 | } |
| 161 | |
| 162 | |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 163 | void 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 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 177 | // 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. |
| 180 | void 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.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 189 | // 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.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 193 | and_(scratch, object, Operand(Heap::NewSpaceMask())); |
| 194 | cmp(scratch, Operand(ExternalReference::new_space_start())); |
| 195 | b(eq, &done); |
| 196 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 197 | // Compute the bit offset in the remembered set. |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 198 | // 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.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 203 | mov(offset, Operand(offset, LSR, kObjectAlignmentBits)); |
| 204 | |
| 205 | // Compute the page address from the heap object pointer. |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 206 | // object: heap object pointer (with tag) |
| 207 | // offset: bit offset of store position in the remembered set |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 208 | 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.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 212 | // object: page start |
| 213 | // offset: bit offset of store position in the remembered set |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 214 | 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.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 233 | // Get address of the rset word. |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 234 | // 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.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 241 | 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 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 252 | void MacroAssembler::EnterFrame(StackFrame::Type type) { |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 253 | // r0-r3: preserved |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 254 | stm(db_w, sp, cp.bit() | fp.bit() | lr.bit()); |
| 255 | mov(ip, Operand(Smi::FromInt(type))); |
| 256 | push(ip); |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 257 | mov(ip, Operand(CodeObject())); |
| 258 | push(ip); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 259 | add(fp, sp, Operand(3 * kPointerSize)); // Adjust FP to point to saved FP. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 263 | void MacroAssembler::LeaveFrame(StackFrame::Type type) { |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 264 | // r0: preserved |
| 265 | // r1: preserved |
| 266 | // r2: preserved |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 267 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 268 | // Drop the execution stack down to the frame pointer and restore |
| 269 | // the caller frame pointer and return address. |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 270 | mov(sp, fp); |
| 271 | ldm(ia_w, sp, fp.bit() | lr.bit()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 275 | void MacroAssembler::EnterExitFrame(StackFrame::Type type) { |
| 276 | ASSERT(type == StackFrame::EXIT || type == StackFrame::EXIT_DEBUG); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 277 | |
| 278 | // Compute the argv pointer and keep it in a callee-saved register. |
| 279 | // r0 is argc. |
| 280 | add(r6, sp, Operand(r0, LSL, kPointerSizeLog2)); |
| 281 | sub(r6, r6, Operand(kPointerSize)); |
| 282 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 283 | // Compute callee's stack pointer before making changes and save it as |
| 284 | // ip register so that it is restored as sp register on exit, thereby |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 285 | // popping the args. |
| 286 | |
| 287 | // ip = sp + kPointerSize * #args; |
| 288 | add(ip, sp, Operand(r0, LSL, kPointerSizeLog2)); |
| 289 | |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 290 | // Align the stack at this point. After this point we have 5 pushes, |
| 291 | // so in fact we have to unalign here! See also the assert on the |
| 292 | // alignment immediately below. |
| 293 | if (OS::ActivationFrameAlignment() != kPointerSize) { |
| 294 | // This code needs to be made more general if this assert doesn't hold. |
| 295 | ASSERT(OS::ActivationFrameAlignment() == 2 * kPointerSize); |
| 296 | mov(r7, Operand(Smi::FromInt(0))); |
| 297 | tst(sp, Operand(OS::ActivationFrameAlignment() - 1)); |
| 298 | push(r7, eq); // Conditional push instruction. |
| 299 | } |
| 300 | |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 301 | // Push in reverse order: caller_fp, sp_on_exit, and caller_pc. |
| 302 | stm(db_w, sp, fp.bit() | ip.bit() | lr.bit()); |
| 303 | mov(fp, Operand(sp)); // setup new frame pointer |
| 304 | |
| 305 | // Push debug marker. |
| 306 | mov(ip, Operand(type == StackFrame::EXIT_DEBUG ? 1 : 0)); |
| 307 | push(ip); |
| 308 | |
| 309 | // Save the frame pointer and the context in top. |
| 310 | mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address))); |
| 311 | str(fp, MemOperand(ip)); |
| 312 | mov(ip, Operand(ExternalReference(Top::k_context_address))); |
| 313 | str(cp, MemOperand(ip)); |
| 314 | |
| 315 | // Setup argc and the builtin function in callee-saved registers. |
| 316 | mov(r4, Operand(r0)); |
| 317 | mov(r5, Operand(r1)); |
| 318 | |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 319 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 320 | #ifdef ENABLE_DEBUGGER_SUPPORT |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 321 | // Save the state of all registers to the stack from the memory |
| 322 | // location. This is needed to allow nested break points. |
| 323 | if (type == StackFrame::EXIT_DEBUG) { |
| 324 | // Use sp as base to push. |
| 325 | CopyRegistersFromMemoryToStack(sp, kJSCallerSaved); |
| 326 | } |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 327 | #endif |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | |
| 331 | void MacroAssembler::LeaveExitFrame(StackFrame::Type type) { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 332 | #ifdef ENABLE_DEBUGGER_SUPPORT |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 333 | // Restore the memory copy of the registers by digging them out from |
| 334 | // the stack. This is needed to allow nested break points. |
| 335 | if (type == StackFrame::EXIT_DEBUG) { |
| 336 | // This code intentionally clobbers r2 and r3. |
| 337 | const int kCallerSavedSize = kNumJSCallerSaved * kPointerSize; |
| 338 | const int kOffset = ExitFrameConstants::kDebugMarkOffset - kCallerSavedSize; |
| 339 | add(r3, fp, Operand(kOffset)); |
| 340 | CopyRegistersFromStackToMemory(r3, r2, kJSCallerSaved); |
| 341 | } |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 342 | #endif |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 343 | |
| 344 | // Clear top frame. |
| 345 | mov(r3, Operand(0)); |
| 346 | mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address))); |
| 347 | str(r3, MemOperand(ip)); |
| 348 | |
| 349 | // Restore current context from top and clear it in debug mode. |
| 350 | mov(ip, Operand(ExternalReference(Top::k_context_address))); |
| 351 | ldr(cp, MemOperand(ip)); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 352 | #ifdef DEBUG |
| 353 | str(r3, MemOperand(ip)); |
| 354 | #endif |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 355 | |
| 356 | // Pop the arguments, restore registers, and return. |
| 357 | mov(sp, Operand(fp)); // respect ABI stack constraint |
| 358 | ldm(ia, sp, fp.bit() | sp.bit() | pc.bit()); |
| 359 | } |
| 360 | |
| 361 | |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 362 | void MacroAssembler::InvokePrologue(const ParameterCount& expected, |
| 363 | const ParameterCount& actual, |
| 364 | Handle<Code> code_constant, |
| 365 | Register code_reg, |
| 366 | Label* done, |
| 367 | InvokeFlag flag) { |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 368 | bool definitely_matches = false; |
| 369 | Label regular_invoke; |
| 370 | |
| 371 | // Check whether the expected and actual arguments count match. If not, |
| 372 | // setup registers according to contract with ArgumentsAdaptorTrampoline: |
| 373 | // r0: actual arguments count |
| 374 | // r1: function (passed through to callee) |
| 375 | // r2: expected arguments count |
| 376 | // r3: callee code entry |
| 377 | |
| 378 | // The code below is made a lot easier because the calling code already sets |
| 379 | // up actual and expected registers according to the contract if values are |
| 380 | // passed in registers. |
| 381 | ASSERT(actual.is_immediate() || actual.reg().is(r0)); |
| 382 | ASSERT(expected.is_immediate() || expected.reg().is(r2)); |
| 383 | ASSERT((!code_constant.is_null() && code_reg.is(no_reg)) || code_reg.is(r3)); |
| 384 | |
| 385 | if (expected.is_immediate()) { |
| 386 | ASSERT(actual.is_immediate()); |
| 387 | if (expected.immediate() == actual.immediate()) { |
| 388 | definitely_matches = true; |
| 389 | } else { |
| 390 | mov(r0, Operand(actual.immediate())); |
| 391 | const int sentinel = SharedFunctionInfo::kDontAdaptArgumentsSentinel; |
| 392 | if (expected.immediate() == sentinel) { |
| 393 | // Don't worry about adapting arguments for builtins that |
| 394 | // don't want that done. Skip adaption code by making it look |
| 395 | // like we have a match between expected and actual number of |
| 396 | // arguments. |
| 397 | definitely_matches = true; |
| 398 | } else { |
| 399 | mov(r2, Operand(expected.immediate())); |
| 400 | } |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 401 | } |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 402 | } else { |
| 403 | if (actual.is_immediate()) { |
| 404 | cmp(expected.reg(), Operand(actual.immediate())); |
| 405 | b(eq, ®ular_invoke); |
| 406 | mov(r0, Operand(actual.immediate())); |
| 407 | } else { |
| 408 | cmp(expected.reg(), Operand(actual.reg())); |
| 409 | b(eq, ®ular_invoke); |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | if (!definitely_matches) { |
| 414 | if (!code_constant.is_null()) { |
| 415 | mov(r3, Operand(code_constant)); |
| 416 | add(r3, r3, Operand(Code::kHeaderSize - kHeapObjectTag)); |
| 417 | } |
| 418 | |
| 419 | Handle<Code> adaptor = |
| 420 | Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline)); |
| 421 | if (flag == CALL_FUNCTION) { |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 422 | Call(adaptor, RelocInfo::CODE_TARGET); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 423 | b(done); |
| 424 | } else { |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 425 | Jump(adaptor, RelocInfo::CODE_TARGET); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 426 | } |
| 427 | bind(®ular_invoke); |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 428 | } |
| 429 | } |
| 430 | |
| 431 | |
| 432 | void MacroAssembler::InvokeCode(Register code, |
| 433 | const ParameterCount& expected, |
| 434 | const ParameterCount& actual, |
| 435 | InvokeFlag flag) { |
| 436 | Label done; |
| 437 | |
| 438 | InvokePrologue(expected, actual, Handle<Code>::null(), code, &done, flag); |
| 439 | if (flag == CALL_FUNCTION) { |
| 440 | Call(code); |
| 441 | } else { |
| 442 | ASSERT(flag == JUMP_FUNCTION); |
| 443 | Jump(code); |
| 444 | } |
| 445 | |
| 446 | // Continue here if InvokePrologue does handle the invocation due to |
| 447 | // mismatched parameter counts. |
| 448 | bind(&done); |
| 449 | } |
| 450 | |
| 451 | |
| 452 | void MacroAssembler::InvokeCode(Handle<Code> code, |
| 453 | const ParameterCount& expected, |
| 454 | const ParameterCount& actual, |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 455 | RelocInfo::Mode rmode, |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 456 | InvokeFlag flag) { |
| 457 | Label done; |
| 458 | |
| 459 | InvokePrologue(expected, actual, code, no_reg, &done, flag); |
| 460 | if (flag == CALL_FUNCTION) { |
| 461 | Call(code, rmode); |
| 462 | } else { |
| 463 | Jump(code, rmode); |
| 464 | } |
| 465 | |
| 466 | // Continue here if InvokePrologue does handle the invocation due to |
| 467 | // mismatched parameter counts. |
| 468 | bind(&done); |
| 469 | } |
| 470 | |
| 471 | |
| 472 | void MacroAssembler::InvokeFunction(Register fun, |
| 473 | const ParameterCount& actual, |
| 474 | InvokeFlag flag) { |
| 475 | // Contract with called JS functions requires that function is passed in r1. |
| 476 | ASSERT(fun.is(r1)); |
| 477 | |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 478 | Register expected_reg = r2; |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 479 | Register code_reg = r3; |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 480 | |
| 481 | ldr(code_reg, FieldMemOperand(r1, JSFunction::kSharedFunctionInfoOffset)); |
| 482 | ldr(cp, FieldMemOperand(r1, JSFunction::kContextOffset)); |
| 483 | ldr(expected_reg, |
| 484 | FieldMemOperand(code_reg, |
| 485 | SharedFunctionInfo::kFormalParameterCountOffset)); |
| 486 | ldr(code_reg, |
| 487 | MemOperand(code_reg, SharedFunctionInfo::kCodeOffset - kHeapObjectTag)); |
| 488 | add(code_reg, code_reg, Operand(Code::kHeaderSize - kHeapObjectTag)); |
| 489 | |
| 490 | ParameterCount expected(expected_reg); |
| 491 | InvokeCode(code_reg, expected, actual, flag); |
| 492 | } |
| 493 | |
| 494 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 495 | #ifdef ENABLE_DEBUGGER_SUPPORT |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 496 | void MacroAssembler::SaveRegistersToMemory(RegList regs) { |
| 497 | ASSERT((regs & ~kJSCallerSaved) == 0); |
| 498 | // Copy the content of registers to memory location. |
| 499 | for (int i = 0; i < kNumJSCallerSaved; i++) { |
| 500 | int r = JSCallerSavedCode(i); |
| 501 | if ((regs & (1 << r)) != 0) { |
| 502 | Register reg = { r }; |
| 503 | mov(ip, Operand(ExternalReference(Debug_Address::Register(i)))); |
| 504 | str(reg, MemOperand(ip)); |
| 505 | } |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | |
| 510 | void MacroAssembler::RestoreRegistersFromMemory(RegList regs) { |
| 511 | ASSERT((regs & ~kJSCallerSaved) == 0); |
| 512 | // Copy the content of memory location to registers. |
| 513 | for (int i = kNumJSCallerSaved; --i >= 0;) { |
| 514 | int r = JSCallerSavedCode(i); |
| 515 | if ((regs & (1 << r)) != 0) { |
| 516 | Register reg = { r }; |
| 517 | mov(ip, Operand(ExternalReference(Debug_Address::Register(i)))); |
| 518 | ldr(reg, MemOperand(ip)); |
| 519 | } |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | |
| 524 | void MacroAssembler::CopyRegistersFromMemoryToStack(Register base, |
| 525 | RegList regs) { |
| 526 | ASSERT((regs & ~kJSCallerSaved) == 0); |
| 527 | // Copy the content of the memory location to the stack and adjust base. |
| 528 | for (int i = kNumJSCallerSaved; --i >= 0;) { |
| 529 | int r = JSCallerSavedCode(i); |
| 530 | if ((regs & (1 << r)) != 0) { |
| 531 | mov(ip, Operand(ExternalReference(Debug_Address::Register(i)))); |
| 532 | ldr(ip, MemOperand(ip)); |
| 533 | str(ip, MemOperand(base, 4, NegPreIndex)); |
| 534 | } |
| 535 | } |
| 536 | } |
| 537 | |
| 538 | |
| 539 | void MacroAssembler::CopyRegistersFromStackToMemory(Register base, |
| 540 | Register scratch, |
| 541 | RegList regs) { |
| 542 | ASSERT((regs & ~kJSCallerSaved) == 0); |
| 543 | // Copy the content of the stack to the memory location and adjust base. |
| 544 | for (int i = 0; i < kNumJSCallerSaved; i++) { |
| 545 | int r = JSCallerSavedCode(i); |
| 546 | if ((regs & (1 << r)) != 0) { |
| 547 | mov(ip, Operand(ExternalReference(Debug_Address::Register(i)))); |
| 548 | ldr(scratch, MemOperand(base, 4, PostIndex)); |
| 549 | str(scratch, MemOperand(ip)); |
| 550 | } |
| 551 | } |
| 552 | } |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 553 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 554 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 555 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 556 | void MacroAssembler::PushTryHandler(CodeLocation try_location, |
| 557 | HandlerType type) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 558 | // Adjust this code if not the case. |
| 559 | ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 560 | // The pc (return address) is passed in register lr. |
| 561 | if (try_location == IN_JAVASCRIPT) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 562 | if (type == TRY_CATCH_HANDLER) { |
| 563 | mov(r3, Operand(StackHandler::TRY_CATCH)); |
| 564 | } else { |
| 565 | mov(r3, Operand(StackHandler::TRY_FINALLY)); |
| 566 | } |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 567 | ASSERT(StackHandlerConstants::kStateOffset == 1 * kPointerSize |
| 568 | && StackHandlerConstants::kFPOffset == 2 * kPointerSize |
| 569 | && StackHandlerConstants::kPCOffset == 3 * kPointerSize); |
| 570 | stm(db_w, sp, r3.bit() | fp.bit() | lr.bit()); |
| 571 | // Save the current handler as the next handler. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 572 | mov(r3, Operand(ExternalReference(Top::k_handler_address))); |
| 573 | ldr(r1, MemOperand(r3)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 574 | ASSERT(StackHandlerConstants::kNextOffset == 0); |
| 575 | push(r1); |
| 576 | // Link this handler as the new current one. |
| 577 | str(sp, MemOperand(r3)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 578 | } else { |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 579 | // Must preserve r0-r4, r5-r7 are available. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 580 | ASSERT(try_location == IN_JS_ENTRY); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 581 | // The frame pointer does not point to a JS frame so we save NULL |
| 582 | // for fp. We expect the code throwing an exception to check fp |
| 583 | // before dereferencing it to restore the context. |
| 584 | mov(ip, Operand(0)); // To save a NULL frame pointer. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 585 | mov(r6, Operand(StackHandler::ENTRY)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 586 | ASSERT(StackHandlerConstants::kStateOffset == 1 * kPointerSize |
| 587 | && StackHandlerConstants::kFPOffset == 2 * kPointerSize |
| 588 | && StackHandlerConstants::kPCOffset == 3 * kPointerSize); |
| 589 | stm(db_w, sp, r6.bit() | ip.bit() | lr.bit()); |
| 590 | // Save the current handler as the next handler. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 591 | mov(r7, Operand(ExternalReference(Top::k_handler_address))); |
| 592 | ldr(r6, MemOperand(r7)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 593 | ASSERT(StackHandlerConstants::kNextOffset == 0); |
| 594 | push(r6); |
| 595 | // Link this handler as the new current one. |
| 596 | str(sp, MemOperand(r7)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 597 | } |
| 598 | } |
| 599 | |
| 600 | |
| 601 | Register MacroAssembler::CheckMaps(JSObject* object, Register object_reg, |
| 602 | JSObject* holder, Register holder_reg, |
| 603 | Register scratch, |
| 604 | Label* miss) { |
| 605 | // Make sure there's no overlap between scratch and the other |
| 606 | // registers. |
| 607 | ASSERT(!scratch.is(object_reg) && !scratch.is(holder_reg)); |
| 608 | |
| 609 | // Keep track of the current object in register reg. |
| 610 | Register reg = object_reg; |
| 611 | int depth = 1; |
| 612 | |
| 613 | // Check the maps in the prototype chain. |
| 614 | // Traverse the prototype chain from the object and do map checks. |
| 615 | while (object != holder) { |
| 616 | depth++; |
| 617 | |
| 618 | // Only global objects and objects that do not require access |
| 619 | // checks are allowed in stubs. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 620 | ASSERT(object->IsJSGlobalProxy() || !object->IsAccessCheckNeeded()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 621 | |
| 622 | // Get the map of the current object. |
| 623 | ldr(scratch, FieldMemOperand(reg, HeapObject::kMapOffset)); |
| 624 | cmp(scratch, Operand(Handle<Map>(object->map()))); |
| 625 | |
| 626 | // Branch on the result of the map check. |
| 627 | b(ne, miss); |
| 628 | |
| 629 | // Check access rights to the global object. This has to happen |
| 630 | // after the map check so that we know that the object is |
| 631 | // actually a global object. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 632 | if (object->IsJSGlobalProxy()) { |
| 633 | CheckAccessGlobalProxy(reg, scratch, miss); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 634 | // Restore scratch register to be the map of the object. In the |
| 635 | // new space case below, we load the prototype from the map in |
| 636 | // the scratch register. |
| 637 | ldr(scratch, FieldMemOperand(reg, HeapObject::kMapOffset)); |
| 638 | } |
| 639 | |
| 640 | reg = holder_reg; // from now the object is in holder_reg |
| 641 | JSObject* prototype = JSObject::cast(object->GetPrototype()); |
| 642 | if (Heap::InNewSpace(prototype)) { |
| 643 | // The prototype is in new space; we cannot store a reference |
| 644 | // to it in the code. Load it from the map. |
| 645 | ldr(reg, FieldMemOperand(scratch, Map::kPrototypeOffset)); |
| 646 | } else { |
| 647 | // The prototype is in old space; load it directly. |
| 648 | mov(reg, Operand(Handle<JSObject>(prototype))); |
| 649 | } |
| 650 | |
| 651 | // Go to the next object in the prototype chain. |
| 652 | object = prototype; |
| 653 | } |
| 654 | |
| 655 | // Check the holder map. |
| 656 | ldr(scratch, FieldMemOperand(reg, HeapObject::kMapOffset)); |
| 657 | cmp(scratch, Operand(Handle<Map>(object->map()))); |
| 658 | b(ne, miss); |
| 659 | |
| 660 | // Log the check depth. |
| 661 | LOG(IntEvent("check-maps-depth", depth)); |
| 662 | |
| 663 | // Perform security check for access to the global object and return |
| 664 | // the holder register. |
| 665 | ASSERT(object == holder); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 666 | ASSERT(object->IsJSGlobalProxy() || !object->IsAccessCheckNeeded()); |
| 667 | if (object->IsJSGlobalProxy()) { |
| 668 | CheckAccessGlobalProxy(reg, scratch, miss); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 669 | } |
| 670 | return reg; |
| 671 | } |
| 672 | |
| 673 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 674 | void MacroAssembler::CheckAccessGlobalProxy(Register holder_reg, |
| 675 | Register scratch, |
| 676 | Label* miss) { |
| 677 | Label same_contexts; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 678 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 679 | ASSERT(!holder_reg.is(scratch)); |
| 680 | ASSERT(!holder_reg.is(ip)); |
| 681 | ASSERT(!scratch.is(ip)); |
| 682 | |
| 683 | // Load current lexical context from the stack frame. |
| 684 | ldr(scratch, MemOperand(fp, StandardFrameConstants::kContextOffset)); |
| 685 | // In debug mode, make sure the lexical context is set. |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 686 | #ifdef DEBUG |
| 687 | cmp(scratch, Operand(0)); |
| 688 | Check(ne, "we should not have an empty lexical context"); |
| 689 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 690 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 691 | // Load the global context of the current context. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 692 | int offset = Context::kHeaderSize + Context::GLOBAL_INDEX * kPointerSize; |
| 693 | ldr(scratch, FieldMemOperand(scratch, offset)); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 694 | ldr(scratch, FieldMemOperand(scratch, GlobalObject::kGlobalContextOffset)); |
| 695 | |
| 696 | // Check the context is a global context. |
| 697 | if (FLAG_debug_code) { |
| 698 | // TODO(119): avoid push(holder_reg)/pop(holder_reg) |
| 699 | // Cannot use ip as a temporary in this verification code. Due to the fact |
| 700 | // that ip is clobbered as part of cmp with an object Operand. |
| 701 | push(holder_reg); // Temporarily save holder on the stack. |
| 702 | // Read the first word and compare to the global_context_map. |
| 703 | ldr(holder_reg, FieldMemOperand(scratch, HeapObject::kMapOffset)); |
| 704 | cmp(holder_reg, Operand(Factory::global_context_map())); |
| 705 | Check(eq, "JSGlobalObject::global_context should be a global context."); |
| 706 | pop(holder_reg); // Restore holder. |
| 707 | } |
| 708 | |
| 709 | // Check if both contexts are the same. |
| 710 | ldr(ip, FieldMemOperand(holder_reg, JSGlobalProxy::kContextOffset)); |
| 711 | cmp(scratch, Operand(ip)); |
| 712 | b(eq, &same_contexts); |
| 713 | |
| 714 | // Check the context is a global context. |
| 715 | if (FLAG_debug_code) { |
| 716 | // TODO(119): avoid push(holder_reg)/pop(holder_reg) |
| 717 | // Cannot use ip as a temporary in this verification code. Due to the fact |
| 718 | // that ip is clobbered as part of cmp with an object Operand. |
| 719 | push(holder_reg); // Temporarily save holder on the stack. |
| 720 | mov(holder_reg, ip); // Move ip to its holding place. |
| 721 | cmp(holder_reg, Operand(Factory::null_value())); |
| 722 | Check(ne, "JSGlobalProxy::context() should not be null."); |
| 723 | |
| 724 | ldr(holder_reg, FieldMemOperand(holder_reg, HeapObject::kMapOffset)); |
| 725 | cmp(holder_reg, Operand(Factory::global_context_map())); |
| 726 | Check(eq, "JSGlobalObject::global_context should be a global context."); |
| 727 | // Restore ip is not needed. ip is reloaded below. |
| 728 | pop(holder_reg); // Restore holder. |
| 729 | // Restore ip to holder's context. |
| 730 | ldr(ip, FieldMemOperand(holder_reg, JSGlobalProxy::kContextOffset)); |
| 731 | } |
| 732 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 733 | // Check that the security token in the calling global object is |
| 734 | // compatible with the security token in the receiving global |
| 735 | // object. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 736 | int token_offset = Context::kHeaderSize + |
| 737 | Context::SECURITY_TOKEN_INDEX * kPointerSize; |
| 738 | |
| 739 | ldr(scratch, FieldMemOperand(scratch, token_offset)); |
| 740 | ldr(ip, FieldMemOperand(ip, token_offset)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 741 | cmp(scratch, Operand(ip)); |
| 742 | b(ne, miss); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 743 | |
| 744 | bind(&same_contexts); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 745 | } |
| 746 | |
| 747 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 748 | void MacroAssembler::CompareObjectType(Register function, |
| 749 | Register map, |
| 750 | Register type_reg, |
| 751 | InstanceType type) { |
| 752 | ldr(map, FieldMemOperand(function, HeapObject::kMapOffset)); |
| 753 | ldrb(type_reg, FieldMemOperand(map, Map::kInstanceTypeOffset)); |
| 754 | cmp(type_reg, Operand(type)); |
| 755 | } |
| 756 | |
| 757 | |
| 758 | void MacroAssembler::TryGetFunctionPrototype(Register function, |
| 759 | Register result, |
| 760 | Register scratch, |
| 761 | Label* miss) { |
| 762 | // Check that the receiver isn't a smi. |
| 763 | BranchOnSmi(function, miss); |
| 764 | |
| 765 | // Check that the function really is a function. Load map into result reg. |
| 766 | CompareObjectType(function, result, scratch, JS_FUNCTION_TYPE); |
| 767 | b(ne, miss); |
| 768 | |
| 769 | // Make sure that the function has an instance prototype. |
| 770 | Label non_instance; |
| 771 | ldrb(scratch, FieldMemOperand(result, Map::kBitFieldOffset)); |
| 772 | tst(scratch, Operand(1 << Map::kHasNonInstancePrototype)); |
| 773 | b(ne, &non_instance); |
| 774 | |
| 775 | // Get the prototype or initial map from the function. |
| 776 | ldr(result, |
| 777 | FieldMemOperand(function, JSFunction::kPrototypeOrInitialMapOffset)); |
| 778 | |
| 779 | // If the prototype or initial map is the hole, don't return it and |
| 780 | // simply miss the cache instead. This will allow us to allocate a |
| 781 | // prototype object on-demand in the runtime system. |
| 782 | cmp(result, Operand(Factory::the_hole_value())); |
| 783 | b(eq, miss); |
| 784 | |
| 785 | // If the function does not have an initial map, we're done. |
| 786 | Label done; |
| 787 | CompareObjectType(result, scratch, scratch, MAP_TYPE); |
| 788 | b(ne, &done); |
| 789 | |
| 790 | // Get the prototype from the initial map. |
| 791 | ldr(result, FieldMemOperand(result, Map::kPrototypeOffset)); |
| 792 | jmp(&done); |
| 793 | |
| 794 | // Non-instance prototype: Fetch prototype from constructor field |
| 795 | // in initial map. |
| 796 | bind(&non_instance); |
| 797 | ldr(result, FieldMemOperand(result, Map::kConstructorOffset)); |
| 798 | |
| 799 | // All done. |
| 800 | bind(&done); |
| 801 | } |
| 802 | |
| 803 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 804 | void MacroAssembler::CallStub(CodeStub* stub) { |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 805 | ASSERT(allow_stub_calls()); // stub calls are not allowed in some stubs |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 806 | Call(stub->GetCode(), RelocInfo::CODE_TARGET); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 807 | } |
| 808 | |
| 809 | |
| 810 | void MacroAssembler::StubReturn(int argc) { |
| 811 | ASSERT(argc >= 1 && generating_stub()); |
| 812 | if (argc > 1) |
| 813 | add(sp, sp, Operand((argc - 1) * kPointerSize)); |
| 814 | Ret(); |
| 815 | } |
| 816 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 817 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 818 | void MacroAssembler::IllegalOperation(int num_arguments) { |
| 819 | if (num_arguments > 0) { |
| 820 | add(sp, sp, Operand(num_arguments * kPointerSize)); |
| 821 | } |
| 822 | mov(r0, Operand(Factory::undefined_value())); |
| 823 | } |
| 824 | |
| 825 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 826 | void MacroAssembler::CallRuntime(Runtime::Function* f, int num_arguments) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 827 | // All parameters are on the stack. r0 has the return value after call. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 828 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 829 | // If the expected number of arguments of the runtime function is |
| 830 | // constant, we check that the actual number of arguments match the |
| 831 | // expectation. |
| 832 | if (f->nargs >= 0 && f->nargs != num_arguments) { |
| 833 | IllegalOperation(num_arguments); |
| 834 | return; |
| 835 | } |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 836 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 837 | Runtime::FunctionId function_id = |
| 838 | static_cast<Runtime::FunctionId>(f->stub_id); |
| 839 | RuntimeStub stub(function_id, num_arguments); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 840 | CallStub(&stub); |
| 841 | } |
| 842 | |
| 843 | |
| 844 | void MacroAssembler::CallRuntime(Runtime::FunctionId fid, int num_arguments) { |
| 845 | CallRuntime(Runtime::FunctionForId(fid), num_arguments); |
| 846 | } |
| 847 | |
| 848 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 849 | void MacroAssembler::TailCallRuntime(const ExternalReference& ext, |
| 850 | int num_arguments) { |
| 851 | // TODO(1236192): Most runtime routines don't need the number of |
| 852 | // arguments passed in because it is constant. At some point we |
| 853 | // should remove this need and make the runtime routine entry code |
| 854 | // smarter. |
| 855 | mov(r0, Operand(num_arguments)); |
| 856 | JumpToBuiltin(ext); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 857 | } |
| 858 | |
| 859 | |
| 860 | void MacroAssembler::JumpToBuiltin(const ExternalReference& builtin) { |
| 861 | #if defined(__thumb__) |
| 862 | // Thumb mode builtin. |
| 863 | ASSERT((reinterpret_cast<intptr_t>(builtin.address()) & 1) == 1); |
| 864 | #endif |
| 865 | mov(r1, Operand(builtin)); |
| 866 | CEntryStub stub; |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 867 | Jump(stub.GetCode(), RelocInfo::CODE_TARGET); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 868 | } |
| 869 | |
| 870 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 871 | Handle<Code> MacroAssembler::ResolveBuiltin(Builtins::JavaScript id, |
| 872 | bool* resolved) { |
| 873 | // Contract with compiled functions is that the function is passed in r1. |
| 874 | int builtins_offset = |
| 875 | JSBuiltinsObject::kJSBuiltinsOffset + (id * kPointerSize); |
| 876 | ldr(r1, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX))); |
| 877 | ldr(r1, FieldMemOperand(r1, GlobalObject::kBuiltinsOffset)); |
| 878 | ldr(r1, FieldMemOperand(r1, builtins_offset)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 879 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 880 | return Builtins::GetCode(id, resolved); |
| 881 | } |
| 882 | |
| 883 | |
| 884 | void MacroAssembler::InvokeBuiltin(Builtins::JavaScript id, |
| 885 | InvokeJSFlags flags) { |
| 886 | bool resolved; |
| 887 | Handle<Code> code = ResolveBuiltin(id, &resolved); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 888 | |
| 889 | if (flags == CALL_JS) { |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 890 | Call(code, RelocInfo::CODE_TARGET); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 891 | } else { |
| 892 | ASSERT(flags == JUMP_JS); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 893 | Jump(code, RelocInfo::CODE_TARGET); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 894 | } |
| 895 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 896 | if (!resolved) { |
| 897 | const char* name = Builtins::GetName(id); |
| 898 | int argc = Builtins::GetArgumentsCount(id); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 899 | uint32_t flags = |
| 900 | Bootstrapper::FixupFlagsArgumentsCount::encode(argc) | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 901 | Bootstrapper::FixupFlagsIsPCRelative::encode(true) | |
| 902 | Bootstrapper::FixupFlagsUseCodeObject::encode(false); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 903 | Unresolved entry = { pc_offset() - sizeof(Instr), flags, name }; |
| 904 | unresolved_.Add(entry); |
| 905 | } |
| 906 | } |
| 907 | |
| 908 | |
| 909 | void MacroAssembler::GetBuiltinEntry(Register target, Builtins::JavaScript id) { |
| 910 | bool resolved; |
| 911 | Handle<Code> code = ResolveBuiltin(id, &resolved); |
| 912 | |
| 913 | mov(target, Operand(code)); |
| 914 | if (!resolved) { |
| 915 | const char* name = Builtins::GetName(id); |
| 916 | int argc = Builtins::GetArgumentsCount(id); |
| 917 | uint32_t flags = |
| 918 | Bootstrapper::FixupFlagsArgumentsCount::encode(argc) | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 919 | Bootstrapper::FixupFlagsIsPCRelative::encode(true) | |
| 920 | Bootstrapper::FixupFlagsUseCodeObject::encode(true); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 921 | Unresolved entry = { pc_offset() - sizeof(Instr), flags, name }; |
| 922 | unresolved_.Add(entry); |
| 923 | } |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 924 | |
| 925 | add(target, target, Operand(Code::kHeaderSize - kHeapObjectTag)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 926 | } |
| 927 | |
| 928 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 929 | void MacroAssembler::SetCounter(StatsCounter* counter, int value, |
| 930 | Register scratch1, Register scratch2) { |
| 931 | if (FLAG_native_code_counters && counter->Enabled()) { |
| 932 | mov(scratch1, Operand(value)); |
| 933 | mov(scratch2, Operand(ExternalReference(counter))); |
| 934 | str(scratch1, MemOperand(scratch2)); |
| 935 | } |
| 936 | } |
| 937 | |
| 938 | |
| 939 | void MacroAssembler::IncrementCounter(StatsCounter* counter, int value, |
| 940 | Register scratch1, Register scratch2) { |
| 941 | ASSERT(value > 0); |
| 942 | if (FLAG_native_code_counters && counter->Enabled()) { |
| 943 | mov(scratch2, Operand(ExternalReference(counter))); |
| 944 | ldr(scratch1, MemOperand(scratch2)); |
| 945 | add(scratch1, scratch1, Operand(value)); |
| 946 | str(scratch1, MemOperand(scratch2)); |
| 947 | } |
| 948 | } |
| 949 | |
| 950 | |
| 951 | void MacroAssembler::DecrementCounter(StatsCounter* counter, int value, |
| 952 | Register scratch1, Register scratch2) { |
| 953 | ASSERT(value > 0); |
| 954 | if (FLAG_native_code_counters && counter->Enabled()) { |
| 955 | mov(scratch2, Operand(ExternalReference(counter))); |
| 956 | ldr(scratch1, MemOperand(scratch2)); |
| 957 | sub(scratch1, scratch1, Operand(value)); |
| 958 | str(scratch1, MemOperand(scratch2)); |
| 959 | } |
| 960 | } |
| 961 | |
| 962 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 963 | void MacroAssembler::Assert(Condition cc, const char* msg) { |
| 964 | if (FLAG_debug_code) |
| 965 | Check(cc, msg); |
| 966 | } |
| 967 | |
| 968 | |
| 969 | void MacroAssembler::Check(Condition cc, const char* msg) { |
| 970 | Label L; |
| 971 | b(cc, &L); |
| 972 | Abort(msg); |
| 973 | // will not return here |
| 974 | bind(&L); |
| 975 | } |
| 976 | |
| 977 | |
| 978 | void MacroAssembler::Abort(const char* msg) { |
| 979 | // We want to pass the msg string like a smi to avoid GC |
| 980 | // problems, however msg is not guaranteed to be aligned |
| 981 | // properly. Instead, we pass an aligned pointer that is |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 982 | // a proper v8 smi, but also pass the alignment difference |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 983 | // from the real pointer as a smi. |
| 984 | intptr_t p1 = reinterpret_cast<intptr_t>(msg); |
| 985 | intptr_t p0 = (p1 & ~kSmiTagMask) + kSmiTag; |
| 986 | ASSERT(reinterpret_cast<Object*>(p0)->IsSmi()); |
| 987 | #ifdef DEBUG |
| 988 | if (msg != NULL) { |
| 989 | RecordComment("Abort message: "); |
| 990 | RecordComment(msg); |
| 991 | } |
| 992 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 993 | mov(r0, Operand(p0)); |
| 994 | push(r0); |
| 995 | mov(r0, Operand(Smi::FromInt(p1 - p0))); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 996 | push(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 997 | CallRuntime(Runtime::kAbort, 2); |
| 998 | // will not return here |
| 999 | } |
| 1000 | |
| 1001 | } } // namespace v8::internal |