sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1 | // Copyright 2009 the V8 project authors. 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 "codegen-inl.h" |
| 31 | #include "compiler.h" |
| 32 | #include "debug.h" |
| 33 | #include "full-codegen.h" |
| 34 | #include "parser.h" |
sgjesse@chromium.org | 833cdd7 | 2010-02-26 10:06:16 +0000 | [diff] [blame^] | 35 | #include "scopes.h" |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 36 | |
| 37 | namespace v8 { |
| 38 | namespace internal { |
| 39 | |
| 40 | #define __ ACCESS_MASM(masm_) |
| 41 | |
| 42 | // Generate code for a JS function. On entry to the function the receiver |
| 43 | // and arguments have been pushed on the stack left to right. The actual |
| 44 | // argument count matches the formal parameter count expected by the |
| 45 | // function. |
| 46 | // |
| 47 | // The live registers are: |
| 48 | // o r1: the JS function object being called (ie, ourselves) |
| 49 | // o cp: our context |
| 50 | // o fp: our caller's frame pointer |
| 51 | // o sp: stack pointer |
| 52 | // o lr: return address |
| 53 | // |
| 54 | // The function builds a JS frame. Please see JavaScriptFrameConstants in |
| 55 | // frames-arm.h for its layout. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 56 | void FullCodeGenerator::Generate(CompilationInfo* info, Mode mode) { |
| 57 | ASSERT(info_ == NULL); |
| 58 | info_ = info; |
| 59 | SetFunctionPosition(function()); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 60 | |
| 61 | if (mode == PRIMARY) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 62 | int locals_count = scope()->num_stack_slots(); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 63 | |
| 64 | __ stm(db_w, sp, r1.bit() | cp.bit() | fp.bit() | lr.bit()); |
| 65 | if (locals_count > 0) { |
| 66 | // Load undefined value here, so the value is ready for the loop |
| 67 | // below. |
| 68 | __ LoadRoot(ip, Heap::kUndefinedValueRootIndex); |
| 69 | } |
| 70 | // Adjust fp to point to caller's fp. |
| 71 | __ add(fp, sp, Operand(2 * kPointerSize)); |
| 72 | |
| 73 | { Comment cmnt(masm_, "[ Allocate locals"); |
| 74 | for (int i = 0; i < locals_count; i++) { |
| 75 | __ push(ip); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | bool function_in_register = true; |
| 80 | |
| 81 | // Possibly allocate a local context. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 82 | if (scope()->num_heap_slots() > 0) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 83 | Comment cmnt(masm_, "[ Allocate local context"); |
| 84 | // Argument to NewContext is the function, which is in r1. |
| 85 | __ push(r1); |
| 86 | __ CallRuntime(Runtime::kNewContext, 1); |
| 87 | function_in_register = false; |
| 88 | // Context is returned in both r0 and cp. It replaces the context |
| 89 | // passed to us. It's saved in the stack and kept live in cp. |
| 90 | __ str(cp, MemOperand(fp, StandardFrameConstants::kContextOffset)); |
| 91 | // Copy any necessary parameters into the context. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 92 | int num_parameters = scope()->num_parameters(); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 93 | for (int i = 0; i < num_parameters; i++) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 94 | Slot* slot = scope()->parameter(i)->slot(); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 95 | if (slot != NULL && slot->type() == Slot::CONTEXT) { |
| 96 | int parameter_offset = StandardFrameConstants::kCallerSPOffset + |
| 97 | (num_parameters - 1 - i) * kPointerSize; |
| 98 | // Load parameter from stack. |
| 99 | __ ldr(r0, MemOperand(fp, parameter_offset)); |
| 100 | // Store it in the context. |
| 101 | __ mov(r1, Operand(Context::SlotOffset(slot->index()))); |
| 102 | __ str(r0, MemOperand(cp, r1)); |
| 103 | // Update the write barrier. This clobbers all involved |
| 104 | // registers, so we have use a third register to avoid |
| 105 | // clobbering cp. |
| 106 | __ mov(r2, Operand(cp)); |
| 107 | __ RecordWrite(r2, r1, r0); |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 112 | Variable* arguments = scope()->arguments()->AsVariable(); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 113 | if (arguments != NULL) { |
| 114 | // Function uses arguments object. |
| 115 | Comment cmnt(masm_, "[ Allocate arguments object"); |
| 116 | if (!function_in_register) { |
| 117 | // Load this again, if it's used by the local context below. |
| 118 | __ ldr(r3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset)); |
| 119 | } else { |
| 120 | __ mov(r3, r1); |
| 121 | } |
| 122 | // Receiver is just before the parameters on the caller's stack. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 123 | int offset = scope()->num_parameters() * kPointerSize; |
| 124 | __ add(r2, fp, |
| 125 | Operand(StandardFrameConstants::kCallerSPOffset + offset)); |
| 126 | __ mov(r1, Operand(Smi::FromInt(scope()->num_parameters()))); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 127 | __ stm(db_w, sp, r3.bit() | r2.bit() | r1.bit()); |
| 128 | |
| 129 | // Arguments to ArgumentsAccessStub: |
| 130 | // function, receiver address, parameter count. |
| 131 | // The stub will rewrite receiever and parameter count if the previous |
| 132 | // stack frame was an arguments adapter frame. |
| 133 | ArgumentsAccessStub stub(ArgumentsAccessStub::NEW_OBJECT); |
| 134 | __ CallStub(&stub); |
| 135 | // Duplicate the value; move-to-slot operation might clobber registers. |
| 136 | __ mov(r3, r0); |
| 137 | Move(arguments->slot(), r0, r1, r2); |
| 138 | Slot* dot_arguments_slot = |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 139 | scope()->arguments_shadow()->AsVariable()->slot(); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 140 | Move(dot_arguments_slot, r3, r1, r2); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | // Check the stack for overflow or break request. |
| 145 | // Put the lr setup instruction in the delay slot. The kInstrSize is |
| 146 | // added to the implicit 8 byte offset that always applies to operations |
| 147 | // with pc and gives a return address 12 bytes down. |
| 148 | { Comment cmnt(masm_, "[ Stack check"); |
| 149 | __ LoadRoot(r2, Heap::kStackLimitRootIndex); |
| 150 | __ add(lr, pc, Operand(Assembler::kInstrSize)); |
| 151 | __ cmp(sp, Operand(r2)); |
| 152 | StackCheckStub stub; |
| 153 | __ mov(pc, |
| 154 | Operand(reinterpret_cast<intptr_t>(stub.GetCode().location()), |
| 155 | RelocInfo::CODE_TARGET), |
| 156 | LeaveCC, |
| 157 | lo); |
| 158 | } |
| 159 | |
| 160 | { Comment cmnt(masm_, "[ Declarations"); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 161 | VisitDeclarations(scope()->declarations()); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | if (FLAG_trace) { |
| 165 | __ CallRuntime(Runtime::kTraceEnter, 0); |
| 166 | } |
| 167 | |
| 168 | { Comment cmnt(masm_, "[ Body"); |
| 169 | ASSERT(loop_depth() == 0); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 170 | VisitStatements(function()->body()); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 171 | ASSERT(loop_depth() == 0); |
| 172 | } |
| 173 | |
| 174 | { Comment cmnt(masm_, "[ return <undefined>;"); |
| 175 | // Emit a 'return undefined' in case control fell off the end of the |
| 176 | // body. |
| 177 | __ LoadRoot(r0, Heap::kUndefinedValueRootIndex); |
| 178 | } |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 179 | EmitReturnSequence(function()->end_position()); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | |
| 183 | void FullCodeGenerator::EmitReturnSequence(int position) { |
| 184 | Comment cmnt(masm_, "[ Return sequence"); |
| 185 | if (return_label_.is_bound()) { |
| 186 | __ b(&return_label_); |
| 187 | } else { |
| 188 | __ bind(&return_label_); |
| 189 | if (FLAG_trace) { |
| 190 | // Push the return value on the stack as the parameter. |
| 191 | // Runtime::TraceExit returns its parameter in r0. |
| 192 | __ push(r0); |
| 193 | __ CallRuntime(Runtime::kTraceExit, 1); |
| 194 | } |
| 195 | |
| 196 | // Add a label for checking the size of the code used for returning. |
| 197 | Label check_exit_codesize; |
| 198 | masm_->bind(&check_exit_codesize); |
| 199 | |
| 200 | // Calculate the exact length of the return sequence and make sure that |
| 201 | // the constant pool is not emitted inside of the return sequence. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 202 | int num_parameters = scope()->num_parameters(); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 203 | int32_t sp_delta = (num_parameters + 1) * kPointerSize; |
| 204 | int return_sequence_length = Assembler::kJSReturnSequenceLength; |
| 205 | if (!masm_->ImmediateFitsAddrMode1Instruction(sp_delta)) { |
| 206 | // Additional mov instruction generated. |
| 207 | return_sequence_length++; |
| 208 | } |
| 209 | masm_->BlockConstPoolFor(return_sequence_length); |
| 210 | |
| 211 | CodeGenerator::RecordPositions(masm_, position); |
| 212 | __ RecordJSReturn(); |
| 213 | __ mov(sp, fp); |
| 214 | __ ldm(ia_w, sp, fp.bit() | lr.bit()); |
| 215 | __ add(sp, sp, Operand(sp_delta)); |
| 216 | __ Jump(lr); |
| 217 | |
| 218 | // Check that the size of the code used for returning matches what is |
| 219 | // expected by the debugger. The add instruction above is an addressing |
| 220 | // mode 1 instruction where there are restrictions on which immediate values |
| 221 | // can be encoded in the instruction and which immediate values requires |
| 222 | // use of an additional instruction for moving the immediate to a temporary |
| 223 | // register. |
| 224 | ASSERT_EQ(return_sequence_length, |
| 225 | masm_->InstructionsGeneratedSince(&check_exit_codesize)); |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | |
| 230 | void FullCodeGenerator::Apply(Expression::Context context, Register reg) { |
| 231 | switch (context) { |
| 232 | case Expression::kUninitialized: |
| 233 | UNREACHABLE(); |
| 234 | |
| 235 | case Expression::kEffect: |
| 236 | // Nothing to do. |
| 237 | break; |
| 238 | |
| 239 | case Expression::kValue: |
| 240 | // Move value into place. |
| 241 | switch (location_) { |
| 242 | case kAccumulator: |
| 243 | if (!reg.is(result_register())) __ mov(result_register(), reg); |
| 244 | break; |
| 245 | case kStack: |
| 246 | __ push(reg); |
| 247 | break; |
| 248 | } |
| 249 | break; |
| 250 | |
| 251 | case Expression::kValueTest: |
| 252 | case Expression::kTestValue: |
| 253 | // Push an extra copy of the value in case it's needed. |
| 254 | __ push(reg); |
| 255 | // Fall through. |
| 256 | |
| 257 | case Expression::kTest: |
| 258 | // We always call the runtime on ARM, so push the value as argument. |
| 259 | __ push(reg); |
| 260 | DoTest(context); |
| 261 | break; |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | |
| 266 | void FullCodeGenerator::Apply(Expression::Context context, Slot* slot) { |
| 267 | switch (context) { |
| 268 | case Expression::kUninitialized: |
| 269 | UNREACHABLE(); |
| 270 | case Expression::kEffect: |
| 271 | // Nothing to do. |
| 272 | break; |
| 273 | case Expression::kValue: |
| 274 | case Expression::kTest: |
| 275 | case Expression::kValueTest: |
| 276 | case Expression::kTestValue: |
| 277 | // On ARM we have to move the value into a register to do anything |
| 278 | // with it. |
| 279 | Move(result_register(), slot); |
| 280 | Apply(context, result_register()); |
| 281 | break; |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | |
| 286 | void FullCodeGenerator::Apply(Expression::Context context, Literal* lit) { |
| 287 | switch (context) { |
| 288 | case Expression::kUninitialized: |
| 289 | UNREACHABLE(); |
| 290 | case Expression::kEffect: |
| 291 | break; |
| 292 | // Nothing to do. |
| 293 | case Expression::kValue: |
| 294 | case Expression::kTest: |
| 295 | case Expression::kValueTest: |
| 296 | case Expression::kTestValue: |
| 297 | // On ARM we have to move the value into a register to do anything |
| 298 | // with it. |
| 299 | __ mov(result_register(), Operand(lit->handle())); |
| 300 | Apply(context, result_register()); |
| 301 | break; |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | |
| 306 | void FullCodeGenerator::ApplyTOS(Expression::Context context) { |
| 307 | switch (context) { |
| 308 | case Expression::kUninitialized: |
| 309 | UNREACHABLE(); |
| 310 | |
| 311 | case Expression::kEffect: |
| 312 | __ Drop(1); |
| 313 | break; |
| 314 | |
| 315 | case Expression::kValue: |
| 316 | switch (location_) { |
| 317 | case kAccumulator: |
| 318 | __ pop(result_register()); |
| 319 | break; |
| 320 | case kStack: |
| 321 | break; |
| 322 | } |
| 323 | break; |
| 324 | |
| 325 | case Expression::kValueTest: |
| 326 | case Expression::kTestValue: |
| 327 | // Duplicate the value on the stack in case it's needed. |
| 328 | __ ldr(ip, MemOperand(sp)); |
| 329 | __ push(ip); |
| 330 | // Fall through. |
| 331 | |
| 332 | case Expression::kTest: |
| 333 | DoTest(context); |
| 334 | break; |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | |
| 339 | void FullCodeGenerator::DropAndApply(int count, |
| 340 | Expression::Context context, |
| 341 | Register reg) { |
| 342 | ASSERT(count > 0); |
| 343 | ASSERT(!reg.is(sp)); |
| 344 | switch (context) { |
| 345 | case Expression::kUninitialized: |
| 346 | UNREACHABLE(); |
| 347 | |
| 348 | case Expression::kEffect: |
| 349 | __ Drop(count); |
| 350 | break; |
| 351 | |
| 352 | case Expression::kValue: |
| 353 | switch (location_) { |
| 354 | case kAccumulator: |
| 355 | __ Drop(count); |
| 356 | if (!reg.is(result_register())) __ mov(result_register(), reg); |
| 357 | break; |
| 358 | case kStack: |
| 359 | if (count > 1) __ Drop(count - 1); |
| 360 | __ str(reg, MemOperand(sp)); |
| 361 | break; |
| 362 | } |
| 363 | break; |
| 364 | |
| 365 | case Expression::kTest: |
| 366 | if (count > 1) __ Drop(count - 1); |
| 367 | __ str(reg, MemOperand(sp)); |
| 368 | DoTest(context); |
| 369 | break; |
| 370 | |
| 371 | case Expression::kValueTest: |
| 372 | case Expression::kTestValue: |
| 373 | if (count == 1) { |
| 374 | __ str(reg, MemOperand(sp)); |
| 375 | __ push(reg); |
| 376 | } else { // count > 1 |
| 377 | __ Drop(count - 2); |
| 378 | __ str(reg, MemOperand(sp, kPointerSize)); |
| 379 | __ str(reg, MemOperand(sp)); |
| 380 | } |
| 381 | DoTest(context); |
| 382 | break; |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | |
| 387 | void FullCodeGenerator::Apply(Expression::Context context, |
| 388 | Label* materialize_true, |
| 389 | Label* materialize_false) { |
| 390 | switch (context) { |
| 391 | case Expression::kUninitialized: |
| 392 | |
| 393 | case Expression::kEffect: |
| 394 | ASSERT_EQ(materialize_true, materialize_false); |
| 395 | __ bind(materialize_true); |
| 396 | break; |
| 397 | |
| 398 | case Expression::kValue: { |
| 399 | Label done; |
| 400 | __ bind(materialize_true); |
| 401 | __ mov(result_register(), Operand(Factory::true_value())); |
| 402 | __ jmp(&done); |
| 403 | __ bind(materialize_false); |
| 404 | __ mov(result_register(), Operand(Factory::false_value())); |
| 405 | __ bind(&done); |
| 406 | switch (location_) { |
| 407 | case kAccumulator: |
| 408 | break; |
| 409 | case kStack: |
| 410 | __ push(result_register()); |
| 411 | break; |
| 412 | } |
| 413 | break; |
| 414 | } |
| 415 | |
| 416 | case Expression::kTest: |
| 417 | break; |
| 418 | |
| 419 | case Expression::kValueTest: |
| 420 | __ bind(materialize_true); |
| 421 | __ mov(result_register(), Operand(Factory::true_value())); |
| 422 | switch (location_) { |
| 423 | case kAccumulator: |
| 424 | break; |
| 425 | case kStack: |
| 426 | __ push(result_register()); |
| 427 | break; |
| 428 | } |
| 429 | __ jmp(true_label_); |
| 430 | break; |
| 431 | |
| 432 | case Expression::kTestValue: |
| 433 | __ bind(materialize_false); |
| 434 | __ mov(result_register(), Operand(Factory::false_value())); |
| 435 | switch (location_) { |
| 436 | case kAccumulator: |
| 437 | break; |
| 438 | case kStack: |
| 439 | __ push(result_register()); |
| 440 | break; |
| 441 | } |
| 442 | __ jmp(false_label_); |
| 443 | break; |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | |
| 448 | void FullCodeGenerator::DoTest(Expression::Context context) { |
| 449 | // The value to test is pushed on the stack, and duplicated on the stack |
| 450 | // if necessary (for value/test and test/value contexts). |
| 451 | ASSERT_NE(NULL, true_label_); |
| 452 | ASSERT_NE(NULL, false_label_); |
| 453 | |
| 454 | // Call the runtime to find the boolean value of the source and then |
| 455 | // translate it into control flow to the pair of labels. |
| 456 | __ CallRuntime(Runtime::kToBool, 1); |
| 457 | __ LoadRoot(ip, Heap::kTrueValueRootIndex); |
| 458 | __ cmp(r0, ip); |
| 459 | |
| 460 | // Complete based on the context. |
| 461 | switch (context) { |
| 462 | case Expression::kUninitialized: |
| 463 | case Expression::kEffect: |
| 464 | case Expression::kValue: |
| 465 | UNREACHABLE(); |
| 466 | |
| 467 | case Expression::kTest: |
| 468 | __ b(eq, true_label_); |
| 469 | __ jmp(false_label_); |
| 470 | break; |
| 471 | |
| 472 | case Expression::kValueTest: { |
| 473 | Label discard; |
| 474 | switch (location_) { |
| 475 | case kAccumulator: |
| 476 | __ b(ne, &discard); |
| 477 | __ pop(result_register()); |
| 478 | __ jmp(true_label_); |
| 479 | break; |
| 480 | case kStack: |
| 481 | __ b(eq, true_label_); |
| 482 | break; |
| 483 | } |
| 484 | __ bind(&discard); |
| 485 | __ Drop(1); |
| 486 | __ jmp(false_label_); |
| 487 | break; |
| 488 | } |
| 489 | |
| 490 | case Expression::kTestValue: { |
| 491 | Label discard; |
| 492 | switch (location_) { |
| 493 | case kAccumulator: |
| 494 | __ b(eq, &discard); |
| 495 | __ pop(result_register()); |
| 496 | __ jmp(false_label_); |
| 497 | break; |
| 498 | case kStack: |
| 499 | __ b(ne, false_label_); |
| 500 | break; |
| 501 | } |
| 502 | __ bind(&discard); |
| 503 | __ Drop(1); |
| 504 | __ jmp(true_label_); |
| 505 | break; |
| 506 | } |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | |
| 511 | MemOperand FullCodeGenerator::EmitSlotSearch(Slot* slot, Register scratch) { |
| 512 | switch (slot->type()) { |
| 513 | case Slot::PARAMETER: |
| 514 | case Slot::LOCAL: |
| 515 | return MemOperand(fp, SlotOffset(slot)); |
| 516 | case Slot::CONTEXT: { |
| 517 | int context_chain_length = |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 518 | scope()->ContextChainLength(slot->var()->scope()); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 519 | __ LoadContext(scratch, context_chain_length); |
| 520 | return CodeGenerator::ContextOperand(scratch, slot->index()); |
| 521 | } |
| 522 | case Slot::LOOKUP: |
| 523 | UNREACHABLE(); |
| 524 | } |
| 525 | UNREACHABLE(); |
| 526 | return MemOperand(r0, 0); |
| 527 | } |
| 528 | |
| 529 | |
| 530 | void FullCodeGenerator::Move(Register destination, Slot* source) { |
| 531 | // Use destination as scratch. |
| 532 | MemOperand slot_operand = EmitSlotSearch(source, destination); |
| 533 | __ ldr(destination, slot_operand); |
| 534 | } |
| 535 | |
| 536 | |
| 537 | void FullCodeGenerator::Move(Slot* dst, |
| 538 | Register src, |
| 539 | Register scratch1, |
| 540 | Register scratch2) { |
| 541 | ASSERT(dst->type() != Slot::LOOKUP); // Not yet implemented. |
| 542 | ASSERT(!scratch1.is(src) && !scratch2.is(src)); |
| 543 | MemOperand location = EmitSlotSearch(dst, scratch1); |
| 544 | __ str(src, location); |
| 545 | // Emit the write barrier code if the location is in the heap. |
| 546 | if (dst->type() == Slot::CONTEXT) { |
| 547 | __ mov(scratch2, Operand(Context::SlotOffset(dst->index()))); |
| 548 | __ RecordWrite(scratch1, scratch2, src); |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | |
| 553 | void FullCodeGenerator::VisitDeclaration(Declaration* decl) { |
| 554 | Comment cmnt(masm_, "[ Declaration"); |
| 555 | Variable* var = decl->proxy()->var(); |
| 556 | ASSERT(var != NULL); // Must have been resolved. |
| 557 | Slot* slot = var->slot(); |
| 558 | Property* prop = var->AsProperty(); |
| 559 | |
| 560 | if (slot != NULL) { |
| 561 | switch (slot->type()) { |
| 562 | case Slot::PARAMETER: |
| 563 | case Slot::LOCAL: |
| 564 | if (decl->mode() == Variable::CONST) { |
| 565 | __ LoadRoot(ip, Heap::kTheHoleValueRootIndex); |
| 566 | __ str(ip, MemOperand(fp, SlotOffset(slot))); |
| 567 | } else if (decl->fun() != NULL) { |
| 568 | VisitForValue(decl->fun(), kAccumulator); |
| 569 | __ str(result_register(), MemOperand(fp, SlotOffset(slot))); |
| 570 | } |
| 571 | break; |
| 572 | |
| 573 | case Slot::CONTEXT: |
| 574 | // We bypass the general EmitSlotSearch because we know more about |
| 575 | // this specific context. |
| 576 | |
| 577 | // The variable in the decl always resides in the current context. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 578 | ASSERT_EQ(0, scope()->ContextChainLength(var->scope())); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 579 | if (FLAG_debug_code) { |
| 580 | // Check if we have the correct context pointer. |
| 581 | __ ldr(r1, |
| 582 | CodeGenerator::ContextOperand(cp, Context::FCONTEXT_INDEX)); |
| 583 | __ cmp(r1, cp); |
| 584 | __ Check(eq, "Unexpected declaration in current context."); |
| 585 | } |
| 586 | if (decl->mode() == Variable::CONST) { |
| 587 | __ LoadRoot(ip, Heap::kTheHoleValueRootIndex); |
| 588 | __ str(ip, CodeGenerator::ContextOperand(cp, slot->index())); |
| 589 | // No write barrier since the_hole_value is in old space. |
| 590 | } else if (decl->fun() != NULL) { |
| 591 | VisitForValue(decl->fun(), kAccumulator); |
| 592 | __ str(result_register(), |
| 593 | CodeGenerator::ContextOperand(cp, slot->index())); |
| 594 | int offset = Context::SlotOffset(slot->index()); |
| 595 | __ mov(r2, Operand(offset)); |
| 596 | // We know that we have written a function, which is not a smi. |
| 597 | __ mov(r1, Operand(cp)); |
| 598 | __ RecordWrite(r1, r2, result_register()); |
| 599 | } |
| 600 | break; |
| 601 | |
| 602 | case Slot::LOOKUP: { |
| 603 | __ mov(r2, Operand(var->name())); |
| 604 | // Declaration nodes are always introduced in one of two modes. |
| 605 | ASSERT(decl->mode() == Variable::VAR || |
| 606 | decl->mode() == Variable::CONST); |
| 607 | PropertyAttributes attr = |
| 608 | (decl->mode() == Variable::VAR) ? NONE : READ_ONLY; |
| 609 | __ mov(r1, Operand(Smi::FromInt(attr))); |
| 610 | // Push initial value, if any. |
| 611 | // Note: For variables we must not push an initial value (such as |
| 612 | // 'undefined') because we may have a (legal) redeclaration and we |
| 613 | // must not destroy the current value. |
| 614 | if (decl->mode() == Variable::CONST) { |
| 615 | __ LoadRoot(r0, Heap::kTheHoleValueRootIndex); |
| 616 | __ stm(db_w, sp, cp.bit() | r2.bit() | r1.bit() | r0.bit()); |
| 617 | } else if (decl->fun() != NULL) { |
| 618 | __ stm(db_w, sp, cp.bit() | r2.bit() | r1.bit()); |
| 619 | // Push initial value for function declaration. |
| 620 | VisitForValue(decl->fun(), kStack); |
| 621 | } else { |
| 622 | __ mov(r0, Operand(Smi::FromInt(0))); // No initial value! |
| 623 | __ stm(db_w, sp, cp.bit() | r2.bit() | r1.bit() | r0.bit()); |
| 624 | } |
| 625 | __ CallRuntime(Runtime::kDeclareContextSlot, 4); |
| 626 | break; |
| 627 | } |
| 628 | } |
| 629 | |
| 630 | } else if (prop != NULL) { |
| 631 | if (decl->fun() != NULL || decl->mode() == Variable::CONST) { |
| 632 | // We are declaring a function or constant that rewrites to a |
| 633 | // property. Use (keyed) IC to set the initial value. |
| 634 | VisitForValue(prop->obj(), kStack); |
| 635 | VisitForValue(prop->key(), kStack); |
| 636 | |
| 637 | if (decl->fun() != NULL) { |
| 638 | VisitForValue(decl->fun(), kAccumulator); |
| 639 | } else { |
| 640 | __ LoadRoot(result_register(), Heap::kTheHoleValueRootIndex); |
| 641 | } |
| 642 | |
| 643 | Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize)); |
| 644 | __ Call(ic, RelocInfo::CODE_TARGET); |
| 645 | |
| 646 | // Value in r0 is ignored (declarations are statements). Receiver |
| 647 | // and key on stack are discarded. |
| 648 | __ Drop(2); |
| 649 | } |
| 650 | } |
| 651 | } |
| 652 | |
| 653 | |
| 654 | void FullCodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) { |
| 655 | // Call the runtime to declare the globals. |
| 656 | // The context is the first argument. |
| 657 | __ mov(r1, Operand(pairs)); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 658 | __ mov(r0, Operand(Smi::FromInt(is_eval() ? 1 : 0))); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 659 | __ stm(db_w, sp, cp.bit() | r1.bit() | r0.bit()); |
| 660 | __ CallRuntime(Runtime::kDeclareGlobals, 3); |
| 661 | // Return value is ignored. |
| 662 | } |
| 663 | |
| 664 | |
| 665 | void FullCodeGenerator::VisitFunctionLiteral(FunctionLiteral* expr) { |
| 666 | Comment cmnt(masm_, "[ FunctionLiteral"); |
| 667 | |
| 668 | // Build the function boilerplate and instantiate it. |
| 669 | Handle<JSFunction> boilerplate = |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 670 | Compiler::BuildBoilerplate(expr, script(), this); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 671 | if (HasStackOverflow()) return; |
| 672 | |
| 673 | ASSERT(boilerplate->IsBoilerplate()); |
| 674 | |
| 675 | // Create a new closure. |
| 676 | __ mov(r0, Operand(boilerplate)); |
| 677 | __ stm(db_w, sp, cp.bit() | r0.bit()); |
| 678 | __ CallRuntime(Runtime::kNewClosure, 2); |
| 679 | Apply(context_, r0); |
| 680 | } |
| 681 | |
| 682 | |
| 683 | void FullCodeGenerator::VisitVariableProxy(VariableProxy* expr) { |
| 684 | Comment cmnt(masm_, "[ VariableProxy"); |
| 685 | EmitVariableLoad(expr->var(), context_); |
| 686 | } |
| 687 | |
| 688 | |
| 689 | void FullCodeGenerator::EmitVariableLoad(Variable* var, |
| 690 | Expression::Context context) { |
| 691 | // Four cases: non-this global variables, lookup slots, all other |
| 692 | // types of slots, and parameters that rewrite to explicit property |
| 693 | // accesses on the arguments object. |
| 694 | Slot* slot = var->slot(); |
| 695 | Property* property = var->AsProperty(); |
| 696 | |
| 697 | if (var->is_global() && !var->is_this()) { |
| 698 | Comment cmnt(masm_, "Global variable"); |
| 699 | // Use inline caching. Variable name is passed in r2 and the global |
| 700 | // object on the stack. |
| 701 | __ ldr(ip, CodeGenerator::GlobalObject()); |
| 702 | __ push(ip); |
| 703 | __ mov(r2, Operand(var->name())); |
| 704 | Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize)); |
| 705 | __ Call(ic, RelocInfo::CODE_TARGET_CONTEXT); |
| 706 | DropAndApply(1, context, r0); |
| 707 | |
| 708 | } else if (slot != NULL && slot->type() == Slot::LOOKUP) { |
| 709 | Comment cmnt(masm_, "Lookup slot"); |
| 710 | __ mov(r1, Operand(var->name())); |
| 711 | __ stm(db_w, sp, cp.bit() | r1.bit()); // Context and name. |
| 712 | __ CallRuntime(Runtime::kLoadContextSlot, 2); |
| 713 | Apply(context, r0); |
| 714 | |
| 715 | } else if (slot != NULL) { |
| 716 | Comment cmnt(masm_, (slot->type() == Slot::CONTEXT) |
| 717 | ? "Context slot" |
| 718 | : "Stack slot"); |
| 719 | Apply(context, slot); |
| 720 | |
| 721 | } else { |
| 722 | Comment cmnt(masm_, "Rewritten parameter"); |
| 723 | ASSERT_NOT_NULL(property); |
| 724 | // Rewritten parameter accesses are of the form "slot[literal]". |
| 725 | |
| 726 | // Assert that the object is in a slot. |
| 727 | Variable* object_var = property->obj()->AsVariableProxy()->AsVariable(); |
| 728 | ASSERT_NOT_NULL(object_var); |
| 729 | Slot* object_slot = object_var->slot(); |
| 730 | ASSERT_NOT_NULL(object_slot); |
| 731 | |
| 732 | // Load the object. |
| 733 | Move(r2, object_slot); |
| 734 | |
| 735 | // Assert that the key is a smi. |
| 736 | Literal* key_literal = property->key()->AsLiteral(); |
| 737 | ASSERT_NOT_NULL(key_literal); |
| 738 | ASSERT(key_literal->handle()->IsSmi()); |
| 739 | |
| 740 | // Load the key. |
| 741 | __ mov(r1, Operand(key_literal->handle())); |
| 742 | |
| 743 | // Push both as arguments to ic. |
| 744 | __ stm(db_w, sp, r2.bit() | r1.bit()); |
| 745 | |
| 746 | // Do a keyed property load. |
| 747 | Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize)); |
| 748 | __ Call(ic, RelocInfo::CODE_TARGET); |
| 749 | |
| 750 | // Drop key and object left on the stack by IC, and push the result. |
| 751 | DropAndApply(2, context, r0); |
| 752 | } |
| 753 | } |
| 754 | |
| 755 | |
| 756 | void FullCodeGenerator::VisitRegExpLiteral(RegExpLiteral* expr) { |
| 757 | Comment cmnt(masm_, "[ RegExpLiteral"); |
| 758 | Label done; |
| 759 | // Registers will be used as follows: |
| 760 | // r4 = JS function, literals array |
| 761 | // r3 = literal index |
| 762 | // r2 = RegExp pattern |
| 763 | // r1 = RegExp flags |
| 764 | // r0 = temp + return value (RegExp literal) |
| 765 | __ ldr(r0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset)); |
| 766 | __ ldr(r4, FieldMemOperand(r0, JSFunction::kLiteralsOffset)); |
| 767 | int literal_offset = |
| 768 | FixedArray::kHeaderSize + expr->literal_index() * kPointerSize; |
| 769 | __ ldr(r0, FieldMemOperand(r4, literal_offset)); |
| 770 | __ LoadRoot(ip, Heap::kUndefinedValueRootIndex); |
| 771 | __ cmp(r0, ip); |
| 772 | __ b(ne, &done); |
| 773 | __ mov(r3, Operand(Smi::FromInt(expr->literal_index()))); |
| 774 | __ mov(r2, Operand(expr->pattern())); |
| 775 | __ mov(r1, Operand(expr->flags())); |
| 776 | __ stm(db_w, sp, r4.bit() | r3.bit() | r2.bit() | r1.bit()); |
| 777 | __ CallRuntime(Runtime::kMaterializeRegExpLiteral, 4); |
| 778 | __ bind(&done); |
| 779 | Apply(context_, r0); |
| 780 | } |
| 781 | |
| 782 | |
| 783 | void FullCodeGenerator::VisitObjectLiteral(ObjectLiteral* expr) { |
| 784 | Comment cmnt(masm_, "[ ObjectLiteral"); |
| 785 | __ ldr(r2, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset)); |
| 786 | __ ldr(r2, FieldMemOperand(r2, JSFunction::kLiteralsOffset)); |
| 787 | __ mov(r1, Operand(Smi::FromInt(expr->literal_index()))); |
| 788 | __ mov(r0, Operand(expr->constant_properties())); |
| 789 | __ stm(db_w, sp, r2.bit() | r1.bit() | r0.bit()); |
| 790 | if (expr->depth() > 1) { |
| 791 | __ CallRuntime(Runtime::kCreateObjectLiteral, 3); |
| 792 | } else { |
| 793 | __ CallRuntime(Runtime::kCreateObjectLiteralShallow, 3); |
| 794 | } |
| 795 | |
| 796 | // If result_saved is true the result is on top of the stack. If |
| 797 | // result_saved is false the result is in r0. |
| 798 | bool result_saved = false; |
| 799 | |
| 800 | for (int i = 0; i < expr->properties()->length(); i++) { |
| 801 | ObjectLiteral::Property* property = expr->properties()->at(i); |
| 802 | if (property->IsCompileTimeValue()) continue; |
| 803 | |
| 804 | Literal* key = property->key(); |
| 805 | Expression* value = property->value(); |
| 806 | if (!result_saved) { |
| 807 | __ push(r0); // Save result on stack |
| 808 | result_saved = true; |
| 809 | } |
| 810 | switch (property->kind()) { |
| 811 | case ObjectLiteral::Property::CONSTANT: |
| 812 | UNREACHABLE(); |
| 813 | case ObjectLiteral::Property::MATERIALIZED_LITERAL: |
| 814 | ASSERT(!CompileTimeValue::IsCompileTimeValue(property->value())); |
| 815 | // Fall through. |
| 816 | case ObjectLiteral::Property::COMPUTED: |
| 817 | if (key->handle()->IsSymbol()) { |
| 818 | VisitForValue(value, kAccumulator); |
| 819 | __ mov(r2, Operand(key->handle())); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 820 | __ ldr(r1, MemOperand(sp)); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 821 | Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize)); |
| 822 | __ Call(ic, RelocInfo::CODE_TARGET); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 823 | break; |
| 824 | } |
| 825 | // Fall through. |
| 826 | case ObjectLiteral::Property::PROTOTYPE: |
| 827 | // Duplicate receiver on stack. |
| 828 | __ ldr(r0, MemOperand(sp)); |
| 829 | __ push(r0); |
| 830 | VisitForValue(key, kStack); |
| 831 | VisitForValue(value, kStack); |
| 832 | __ CallRuntime(Runtime::kSetProperty, 3); |
| 833 | break; |
| 834 | case ObjectLiteral::Property::GETTER: |
| 835 | case ObjectLiteral::Property::SETTER: |
| 836 | // Duplicate receiver on stack. |
| 837 | __ ldr(r0, MemOperand(sp)); |
| 838 | __ push(r0); |
| 839 | VisitForValue(key, kStack); |
| 840 | __ mov(r1, Operand(property->kind() == ObjectLiteral::Property::SETTER ? |
| 841 | Smi::FromInt(1) : |
| 842 | Smi::FromInt(0))); |
| 843 | __ push(r1); |
| 844 | VisitForValue(value, kStack); |
| 845 | __ CallRuntime(Runtime::kDefineAccessor, 4); |
| 846 | break; |
| 847 | } |
| 848 | } |
| 849 | |
| 850 | if (result_saved) { |
| 851 | ApplyTOS(context_); |
| 852 | } else { |
| 853 | Apply(context_, r0); |
| 854 | } |
| 855 | } |
| 856 | |
| 857 | |
| 858 | void FullCodeGenerator::VisitArrayLiteral(ArrayLiteral* expr) { |
| 859 | Comment cmnt(masm_, "[ ArrayLiteral"); |
| 860 | __ ldr(r3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset)); |
| 861 | __ ldr(r3, FieldMemOperand(r3, JSFunction::kLiteralsOffset)); |
| 862 | __ mov(r2, Operand(Smi::FromInt(expr->literal_index()))); |
| 863 | __ mov(r1, Operand(expr->constant_elements())); |
| 864 | __ stm(db_w, sp, r3.bit() | r2.bit() | r1.bit()); |
| 865 | if (expr->depth() > 1) { |
| 866 | __ CallRuntime(Runtime::kCreateArrayLiteral, 3); |
| 867 | } else { |
| 868 | __ CallRuntime(Runtime::kCreateArrayLiteralShallow, 3); |
| 869 | } |
| 870 | |
| 871 | bool result_saved = false; // Is the result saved to the stack? |
| 872 | |
| 873 | // Emit code to evaluate all the non-constant subexpressions and to store |
| 874 | // them into the newly cloned array. |
| 875 | ZoneList<Expression*>* subexprs = expr->values(); |
| 876 | for (int i = 0, len = subexprs->length(); i < len; i++) { |
| 877 | Expression* subexpr = subexprs->at(i); |
| 878 | // If the subexpression is a literal or a simple materialized literal it |
| 879 | // is already set in the cloned array. |
| 880 | if (subexpr->AsLiteral() != NULL || |
| 881 | CompileTimeValue::IsCompileTimeValue(subexpr)) { |
| 882 | continue; |
| 883 | } |
| 884 | |
| 885 | if (!result_saved) { |
| 886 | __ push(r0); |
| 887 | result_saved = true; |
| 888 | } |
| 889 | VisitForValue(subexpr, kAccumulator); |
| 890 | |
| 891 | // Store the subexpression value in the array's elements. |
| 892 | __ ldr(r1, MemOperand(sp)); // Copy of array literal. |
| 893 | __ ldr(r1, FieldMemOperand(r1, JSObject::kElementsOffset)); |
| 894 | int offset = FixedArray::kHeaderSize + (i * kPointerSize); |
| 895 | __ str(result_register(), FieldMemOperand(r1, offset)); |
| 896 | |
| 897 | // Update the write barrier for the array store with r0 as the scratch |
| 898 | // register. |
| 899 | __ mov(r2, Operand(offset)); |
| 900 | __ RecordWrite(r1, r2, result_register()); |
| 901 | } |
| 902 | |
| 903 | if (result_saved) { |
| 904 | ApplyTOS(context_); |
| 905 | } else { |
| 906 | Apply(context_, r0); |
| 907 | } |
| 908 | } |
| 909 | |
| 910 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 911 | void FullCodeGenerator::VisitAssignment(Assignment* expr) { |
| 912 | Comment cmnt(masm_, "[ Assignment"); |
| 913 | ASSERT(expr->op() != Token::INIT_CONST); |
| 914 | // Left-hand side can only be a property, a global or a (parameter or local) |
| 915 | // slot. Variables with rewrite to .arguments are treated as KEYED_PROPERTY. |
| 916 | enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY }; |
| 917 | LhsKind assign_type = VARIABLE; |
| 918 | Property* prop = expr->target()->AsProperty(); |
| 919 | if (prop != NULL) { |
| 920 | assign_type = |
| 921 | (prop->key()->IsPropertyName()) ? NAMED_PROPERTY : KEYED_PROPERTY; |
| 922 | } |
| 923 | |
| 924 | // Evaluate LHS expression. |
| 925 | switch (assign_type) { |
| 926 | case VARIABLE: |
| 927 | // Nothing to do here. |
| 928 | break; |
| 929 | case NAMED_PROPERTY: |
| 930 | if (expr->is_compound()) { |
| 931 | // We need the receiver both on the stack and in the accumulator. |
| 932 | VisitForValue(prop->obj(), kAccumulator); |
| 933 | __ push(result_register()); |
| 934 | } else { |
| 935 | VisitForValue(prop->obj(), kStack); |
| 936 | } |
| 937 | break; |
| 938 | case KEYED_PROPERTY: |
| 939 | VisitForValue(prop->obj(), kStack); |
| 940 | VisitForValue(prop->key(), kStack); |
| 941 | break; |
| 942 | } |
| 943 | |
| 944 | // If we have a compound assignment: Get value of LHS expression and |
| 945 | // store in on top of the stack. |
| 946 | if (expr->is_compound()) { |
| 947 | Location saved_location = location_; |
| 948 | location_ = kStack; |
| 949 | switch (assign_type) { |
| 950 | case VARIABLE: |
| 951 | EmitVariableLoad(expr->target()->AsVariableProxy()->var(), |
| 952 | Expression::kValue); |
| 953 | break; |
| 954 | case NAMED_PROPERTY: |
| 955 | EmitNamedPropertyLoad(prop); |
| 956 | __ push(result_register()); |
| 957 | break; |
| 958 | case KEYED_PROPERTY: |
| 959 | EmitKeyedPropertyLoad(prop); |
| 960 | __ push(result_register()); |
| 961 | break; |
| 962 | } |
| 963 | location_ = saved_location; |
| 964 | } |
| 965 | |
| 966 | // Evaluate RHS expression. |
| 967 | Expression* rhs = expr->value(); |
| 968 | VisitForValue(rhs, kAccumulator); |
| 969 | |
| 970 | // If we have a compound assignment: Apply operator. |
| 971 | if (expr->is_compound()) { |
| 972 | Location saved_location = location_; |
| 973 | location_ = kAccumulator; |
| 974 | EmitBinaryOp(expr->binary_op(), Expression::kValue); |
| 975 | location_ = saved_location; |
| 976 | } |
| 977 | |
| 978 | // Record source position before possible IC call. |
| 979 | SetSourcePosition(expr->position()); |
| 980 | |
| 981 | // Store the value. |
| 982 | switch (assign_type) { |
| 983 | case VARIABLE: |
| 984 | EmitVariableAssignment(expr->target()->AsVariableProxy()->var(), |
| 985 | context_); |
| 986 | break; |
| 987 | case NAMED_PROPERTY: |
| 988 | EmitNamedPropertyAssignment(expr); |
| 989 | break; |
| 990 | case KEYED_PROPERTY: |
| 991 | EmitKeyedPropertyAssignment(expr); |
| 992 | break; |
| 993 | } |
| 994 | } |
| 995 | |
| 996 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 997 | void FullCodeGenerator::EmitNamedPropertyLoad(Property* prop) { |
| 998 | SetSourcePosition(prop->position()); |
| 999 | Literal* key = prop->key()->AsLiteral(); |
| 1000 | __ mov(r2, Operand(key->handle())); |
| 1001 | Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize)); |
| 1002 | __ Call(ic, RelocInfo::CODE_TARGET); |
| 1003 | } |
| 1004 | |
| 1005 | |
| 1006 | void FullCodeGenerator::EmitKeyedPropertyLoad(Property* prop) { |
| 1007 | SetSourcePosition(prop->position()); |
| 1008 | Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize)); |
| 1009 | __ Call(ic, RelocInfo::CODE_TARGET); |
| 1010 | } |
| 1011 | |
| 1012 | |
| 1013 | void FullCodeGenerator::EmitBinaryOp(Token::Value op, |
| 1014 | Expression::Context context) { |
| 1015 | __ pop(r1); |
| 1016 | GenericBinaryOpStub stub(op, NO_OVERWRITE); |
| 1017 | __ CallStub(&stub); |
| 1018 | Apply(context, r0); |
| 1019 | } |
| 1020 | |
| 1021 | |
| 1022 | void FullCodeGenerator::EmitVariableAssignment(Variable* var, |
| 1023 | Expression::Context context) { |
| 1024 | // Three main cases: global variables, lookup slots, and all other |
| 1025 | // types of slots. Left-hand-side parameters that rewrite to |
| 1026 | // explicit property accesses do not reach here. |
| 1027 | ASSERT(var != NULL); |
| 1028 | ASSERT(var->is_global() || var->slot() != NULL); |
| 1029 | |
| 1030 | Slot* slot = var->slot(); |
| 1031 | if (var->is_global()) { |
| 1032 | ASSERT(!var->is_this()); |
| 1033 | // Assignment to a global variable. Use inline caching for the |
| 1034 | // assignment. Right-hand-side value is passed in r0, variable name in |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1035 | // r2, and the global object in r1. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1036 | __ mov(r2, Operand(var->name())); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1037 | __ ldr(r1, CodeGenerator::GlobalObject()); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1038 | Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize)); |
| 1039 | __ Call(ic, RelocInfo::CODE_TARGET); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1040 | |
| 1041 | } else if (slot != NULL && slot->type() == Slot::LOOKUP) { |
| 1042 | __ push(result_register()); // Value. |
| 1043 | __ mov(r1, Operand(var->name())); |
| 1044 | __ stm(db_w, sp, cp.bit() | r1.bit()); // Context and name. |
| 1045 | __ CallRuntime(Runtime::kStoreContextSlot, 3); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1046 | |
| 1047 | } else if (var->slot() != NULL) { |
| 1048 | Slot* slot = var->slot(); |
| 1049 | switch (slot->type()) { |
| 1050 | case Slot::LOCAL: |
| 1051 | case Slot::PARAMETER: |
| 1052 | __ str(result_register(), MemOperand(fp, SlotOffset(slot))); |
| 1053 | break; |
| 1054 | |
| 1055 | case Slot::CONTEXT: { |
| 1056 | MemOperand target = EmitSlotSearch(slot, r1); |
| 1057 | __ str(result_register(), target); |
| 1058 | |
| 1059 | // RecordWrite may destroy all its register arguments. |
| 1060 | __ mov(r3, result_register()); |
| 1061 | int offset = FixedArray::kHeaderSize + slot->index() * kPointerSize; |
| 1062 | |
| 1063 | __ mov(r2, Operand(offset)); |
| 1064 | __ RecordWrite(r1, r2, r3); |
| 1065 | break; |
| 1066 | } |
| 1067 | |
| 1068 | case Slot::LOOKUP: |
| 1069 | UNREACHABLE(); |
| 1070 | break; |
| 1071 | } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1072 | |
| 1073 | } else { |
| 1074 | // Variables rewritten as properties are not treated as variables in |
| 1075 | // assignments. |
| 1076 | UNREACHABLE(); |
| 1077 | } |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1078 | Apply(context, result_register()); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1079 | } |
| 1080 | |
| 1081 | |
| 1082 | void FullCodeGenerator::EmitNamedPropertyAssignment(Assignment* expr) { |
| 1083 | // Assignment to a property, using a named store IC. |
| 1084 | Property* prop = expr->target()->AsProperty(); |
| 1085 | ASSERT(prop != NULL); |
| 1086 | ASSERT(prop->key()->AsLiteral() != NULL); |
| 1087 | |
| 1088 | // If the assignment starts a block of assignments to the same object, |
| 1089 | // change to slow case to avoid the quadratic behavior of repeatedly |
| 1090 | // adding fast properties. |
| 1091 | if (expr->starts_initialization_block()) { |
| 1092 | __ push(result_register()); |
| 1093 | __ ldr(ip, MemOperand(sp, kPointerSize)); // Receiver is now under value. |
| 1094 | __ push(ip); |
| 1095 | __ CallRuntime(Runtime::kToSlowProperties, 1); |
| 1096 | __ pop(result_register()); |
| 1097 | } |
| 1098 | |
| 1099 | // Record source code position before IC call. |
| 1100 | SetSourcePosition(expr->position()); |
| 1101 | __ mov(r2, Operand(prop->key()->AsLiteral()->handle())); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1102 | if (expr->ends_initialization_block()) { |
| 1103 | __ ldr(r1, MemOperand(sp)); |
| 1104 | } else { |
| 1105 | __ pop(r1); |
| 1106 | } |
| 1107 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1108 | Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize)); |
| 1109 | __ Call(ic, RelocInfo::CODE_TARGET); |
| 1110 | |
| 1111 | // If the assignment ends an initialization block, revert to fast case. |
| 1112 | if (expr->ends_initialization_block()) { |
| 1113 | __ push(r0); // Result of assignment, saved even if not needed. |
| 1114 | __ ldr(ip, MemOperand(sp, kPointerSize)); // Receiver is under value. |
| 1115 | __ push(ip); |
| 1116 | __ CallRuntime(Runtime::kToFastProperties, 1); |
| 1117 | __ pop(r0); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1118 | DropAndApply(1, context_, r0); |
| 1119 | } else { |
| 1120 | Apply(context_, r0); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1121 | } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1122 | } |
| 1123 | |
| 1124 | |
| 1125 | void FullCodeGenerator::EmitKeyedPropertyAssignment(Assignment* expr) { |
| 1126 | // Assignment to a property, using a keyed store IC. |
| 1127 | |
| 1128 | // If the assignment starts a block of assignments to the same object, |
| 1129 | // change to slow case to avoid the quadratic behavior of repeatedly |
| 1130 | // adding fast properties. |
| 1131 | if (expr->starts_initialization_block()) { |
| 1132 | __ push(result_register()); |
| 1133 | // Receiver is now under the key and value. |
| 1134 | __ ldr(ip, MemOperand(sp, 2 * kPointerSize)); |
| 1135 | __ push(ip); |
| 1136 | __ CallRuntime(Runtime::kToSlowProperties, 1); |
| 1137 | __ pop(result_register()); |
| 1138 | } |
| 1139 | |
| 1140 | // Record source code position before IC call. |
| 1141 | SetSourcePosition(expr->position()); |
| 1142 | Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize)); |
| 1143 | __ Call(ic, RelocInfo::CODE_TARGET); |
| 1144 | |
| 1145 | // If the assignment ends an initialization block, revert to fast case. |
| 1146 | if (expr->ends_initialization_block()) { |
| 1147 | __ push(r0); // Result of assignment, saved even if not needed. |
| 1148 | // Receiver is under the key and value. |
| 1149 | __ ldr(ip, MemOperand(sp, 2 * kPointerSize)); |
| 1150 | __ push(ip); |
| 1151 | __ CallRuntime(Runtime::kToFastProperties, 1); |
| 1152 | __ pop(r0); |
| 1153 | } |
| 1154 | |
| 1155 | // Receiver and key are still on stack. |
| 1156 | DropAndApply(2, context_, r0); |
| 1157 | } |
| 1158 | |
| 1159 | |
| 1160 | void FullCodeGenerator::VisitProperty(Property* expr) { |
| 1161 | Comment cmnt(masm_, "[ Property"); |
| 1162 | Expression* key = expr->key(); |
| 1163 | |
| 1164 | // Evaluate receiver. |
| 1165 | VisitForValue(expr->obj(), kStack); |
| 1166 | |
| 1167 | if (key->IsPropertyName()) { |
| 1168 | EmitNamedPropertyLoad(expr); |
| 1169 | // Drop receiver left on the stack by IC. |
| 1170 | DropAndApply(1, context_, r0); |
| 1171 | } else { |
| 1172 | VisitForValue(expr->key(), kStack); |
| 1173 | EmitKeyedPropertyLoad(expr); |
| 1174 | // Drop key and receiver left on the stack by IC. |
| 1175 | DropAndApply(2, context_, r0); |
| 1176 | } |
| 1177 | } |
| 1178 | |
| 1179 | void FullCodeGenerator::EmitCallWithIC(Call* expr, |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1180 | Handle<Object> name, |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1181 | RelocInfo::Mode mode) { |
| 1182 | // Code common for calls using the IC. |
| 1183 | ZoneList<Expression*>* args = expr->arguments(); |
| 1184 | int arg_count = args->length(); |
| 1185 | for (int i = 0; i < arg_count; i++) { |
| 1186 | VisitForValue(args->at(i), kStack); |
| 1187 | } |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1188 | __ mov(r2, Operand(name)); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1189 | // Record source position for debugger. |
| 1190 | SetSourcePosition(expr->position()); |
| 1191 | // Call the IC initialization code. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1192 | InLoopFlag in_loop = (loop_depth() > 0) ? IN_LOOP : NOT_IN_LOOP; |
| 1193 | Handle<Code> ic = CodeGenerator::ComputeCallInitialize(arg_count, in_loop); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1194 | __ Call(ic, mode); |
| 1195 | // Restore context register. |
| 1196 | __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset)); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1197 | Apply(context_, r0); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1198 | } |
| 1199 | |
| 1200 | |
| 1201 | void FullCodeGenerator::EmitCallWithStub(Call* expr) { |
| 1202 | // Code common for calls using the call stub. |
| 1203 | ZoneList<Expression*>* args = expr->arguments(); |
| 1204 | int arg_count = args->length(); |
| 1205 | for (int i = 0; i < arg_count; i++) { |
| 1206 | VisitForValue(args->at(i), kStack); |
| 1207 | } |
| 1208 | // Record source position for debugger. |
| 1209 | SetSourcePosition(expr->position()); |
| 1210 | CallFunctionStub stub(arg_count, NOT_IN_LOOP, RECEIVER_MIGHT_BE_VALUE); |
| 1211 | __ CallStub(&stub); |
| 1212 | // Restore context register. |
| 1213 | __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset)); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1214 | DropAndApply(1, context_, r0); |
| 1215 | } |
| 1216 | |
| 1217 | |
| 1218 | void FullCodeGenerator::VisitCall(Call* expr) { |
| 1219 | Comment cmnt(masm_, "[ Call"); |
| 1220 | Expression* fun = expr->expression(); |
| 1221 | Variable* var = fun->AsVariableProxy()->AsVariable(); |
| 1222 | |
| 1223 | if (var != NULL && var->is_possibly_eval()) { |
| 1224 | // Call to the identifier 'eval'. |
| 1225 | UNREACHABLE(); |
| 1226 | } else if (var != NULL && !var->is_this() && var->is_global()) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1227 | // Push global object as receiver for the call IC. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1228 | __ ldr(r0, CodeGenerator::GlobalObject()); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1229 | __ push(r0); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1230 | EmitCallWithIC(expr, var->name(), RelocInfo::CODE_TARGET_CONTEXT); |
| 1231 | } else if (var != NULL && var->slot() != NULL && |
| 1232 | var->slot()->type() == Slot::LOOKUP) { |
| 1233 | // Call to a lookup slot. |
| 1234 | UNREACHABLE(); |
| 1235 | } else if (fun->AsProperty() != NULL) { |
| 1236 | // Call to an object property. |
| 1237 | Property* prop = fun->AsProperty(); |
| 1238 | Literal* key = prop->key()->AsLiteral(); |
| 1239 | if (key != NULL && key->handle()->IsSymbol()) { |
| 1240 | // Call to a named property, use call IC. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1241 | VisitForValue(prop->obj(), kStack); |
| 1242 | EmitCallWithIC(expr, key->handle(), RelocInfo::CODE_TARGET); |
| 1243 | } else { |
| 1244 | // Call to a keyed property, use keyed load IC followed by function |
| 1245 | // call. |
| 1246 | VisitForValue(prop->obj(), kStack); |
| 1247 | VisitForValue(prop->key(), kStack); |
| 1248 | // Record source code position for IC call. |
| 1249 | SetSourcePosition(prop->position()); |
| 1250 | Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize)); |
| 1251 | __ Call(ic, RelocInfo::CODE_TARGET); |
| 1252 | // Load receiver object into r1. |
| 1253 | if (prop->is_synthetic()) { |
| 1254 | __ ldr(r1, CodeGenerator::GlobalObject()); |
| 1255 | __ ldr(r1, FieldMemOperand(r1, GlobalObject::kGlobalReceiverOffset)); |
| 1256 | } else { |
| 1257 | __ ldr(r1, MemOperand(sp, kPointerSize)); |
| 1258 | } |
| 1259 | // Overwrite (object, key) with (function, receiver). |
| 1260 | __ str(r0, MemOperand(sp, kPointerSize)); |
| 1261 | __ str(r1, MemOperand(sp)); |
| 1262 | EmitCallWithStub(expr); |
| 1263 | } |
| 1264 | } else { |
| 1265 | // Call to some other expression. If the expression is an anonymous |
| 1266 | // function literal not called in a loop, mark it as one that should |
| 1267 | // also use the fast code generator. |
| 1268 | FunctionLiteral* lit = fun->AsFunctionLiteral(); |
| 1269 | if (lit != NULL && |
| 1270 | lit->name()->Equals(Heap::empty_string()) && |
| 1271 | loop_depth() == 0) { |
| 1272 | lit->set_try_full_codegen(true); |
| 1273 | } |
| 1274 | VisitForValue(fun, kStack); |
| 1275 | // Load global receiver object. |
| 1276 | __ ldr(r1, CodeGenerator::GlobalObject()); |
| 1277 | __ ldr(r1, FieldMemOperand(r1, GlobalObject::kGlobalReceiverOffset)); |
| 1278 | __ push(r1); |
| 1279 | // Emit function call. |
| 1280 | EmitCallWithStub(expr); |
| 1281 | } |
| 1282 | } |
| 1283 | |
| 1284 | |
| 1285 | void FullCodeGenerator::VisitCallNew(CallNew* expr) { |
| 1286 | Comment cmnt(masm_, "[ CallNew"); |
| 1287 | // According to ECMA-262, section 11.2.2, page 44, the function |
| 1288 | // expression in new calls must be evaluated before the |
| 1289 | // arguments. |
| 1290 | // Push function on the stack. |
| 1291 | VisitForValue(expr->expression(), kStack); |
| 1292 | |
| 1293 | // Push global object (receiver). |
| 1294 | __ ldr(r0, CodeGenerator::GlobalObject()); |
| 1295 | __ push(r0); |
| 1296 | // Push the arguments ("left-to-right") on the stack. |
| 1297 | ZoneList<Expression*>* args = expr->arguments(); |
| 1298 | int arg_count = args->length(); |
| 1299 | for (int i = 0; i < arg_count; i++) { |
| 1300 | VisitForValue(args->at(i), kStack); |
| 1301 | } |
| 1302 | |
| 1303 | // Call the construct call builtin that handles allocation and |
| 1304 | // constructor invocation. |
| 1305 | SetSourcePosition(expr->position()); |
| 1306 | |
| 1307 | // Load function, arg_count into r1 and r0. |
| 1308 | __ mov(r0, Operand(arg_count)); |
| 1309 | // Function is in sp[arg_count + 1]. |
| 1310 | __ ldr(r1, MemOperand(sp, (arg_count + 1) * kPointerSize)); |
| 1311 | |
| 1312 | Handle<Code> construct_builtin(Builtins::builtin(Builtins::JSConstructCall)); |
| 1313 | __ Call(construct_builtin, RelocInfo::CONSTRUCT_CALL); |
| 1314 | |
| 1315 | // Replace function on TOS with result in r0, or pop it. |
| 1316 | DropAndApply(1, context_, r0); |
| 1317 | } |
| 1318 | |
| 1319 | |
| 1320 | void FullCodeGenerator::VisitCallRuntime(CallRuntime* expr) { |
| 1321 | Comment cmnt(masm_, "[ CallRuntime"); |
| 1322 | ZoneList<Expression*>* args = expr->arguments(); |
| 1323 | |
| 1324 | if (expr->is_jsruntime()) { |
| 1325 | // Prepare for calling JS runtime function. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1326 | __ ldr(r0, CodeGenerator::GlobalObject()); |
| 1327 | __ ldr(r0, FieldMemOperand(r0, GlobalObject::kBuiltinsOffset)); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1328 | __ push(r0); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1329 | } |
| 1330 | |
| 1331 | // Push the arguments ("left-to-right"). |
| 1332 | int arg_count = args->length(); |
| 1333 | for (int i = 0; i < arg_count; i++) { |
| 1334 | VisitForValue(args->at(i), kStack); |
| 1335 | } |
| 1336 | |
| 1337 | if (expr->is_jsruntime()) { |
| 1338 | // Call the JS runtime function. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1339 | __ mov(r2, Operand(expr->name())); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1340 | Handle<Code> ic = CodeGenerator::ComputeCallInitialize(arg_count, |
| 1341 | NOT_IN_LOOP); |
| 1342 | __ Call(ic, RelocInfo::CODE_TARGET); |
| 1343 | // Restore context register. |
| 1344 | __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset)); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1345 | } else { |
| 1346 | // Call the C runtime function. |
| 1347 | __ CallRuntime(expr->function(), arg_count); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1348 | } |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1349 | Apply(context_, r0); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1350 | } |
| 1351 | |
| 1352 | |
| 1353 | void FullCodeGenerator::VisitUnaryOperation(UnaryOperation* expr) { |
| 1354 | switch (expr->op()) { |
| 1355 | case Token::VOID: { |
| 1356 | Comment cmnt(masm_, "[ UnaryOperation (VOID)"); |
| 1357 | VisitForEffect(expr->expression()); |
| 1358 | switch (context_) { |
| 1359 | case Expression::kUninitialized: |
| 1360 | UNREACHABLE(); |
| 1361 | break; |
| 1362 | case Expression::kEffect: |
| 1363 | break; |
| 1364 | case Expression::kValue: |
| 1365 | __ LoadRoot(result_register(), Heap::kUndefinedValueRootIndex); |
| 1366 | switch (location_) { |
| 1367 | case kAccumulator: |
| 1368 | break; |
| 1369 | case kStack: |
| 1370 | __ push(result_register()); |
| 1371 | break; |
| 1372 | } |
| 1373 | break; |
| 1374 | case Expression::kTestValue: |
| 1375 | // Value is false so it's needed. |
| 1376 | __ LoadRoot(result_register(), Heap::kUndefinedValueRootIndex); |
| 1377 | switch (location_) { |
| 1378 | case kAccumulator: |
| 1379 | break; |
| 1380 | case kStack: |
| 1381 | __ push(result_register()); |
| 1382 | break; |
| 1383 | } |
| 1384 | // Fall through. |
| 1385 | case Expression::kTest: |
| 1386 | case Expression::kValueTest: |
| 1387 | __ jmp(false_label_); |
| 1388 | break; |
| 1389 | } |
| 1390 | break; |
| 1391 | } |
| 1392 | |
| 1393 | case Token::NOT: { |
| 1394 | Comment cmnt(masm_, "[ UnaryOperation (NOT)"); |
| 1395 | Label materialize_true, materialize_false, done; |
| 1396 | // Initially assume a pure test context. Notice that the labels are |
| 1397 | // swapped. |
| 1398 | Label* if_true = false_label_; |
| 1399 | Label* if_false = true_label_; |
| 1400 | switch (context_) { |
| 1401 | case Expression::kUninitialized: |
| 1402 | UNREACHABLE(); |
| 1403 | break; |
| 1404 | case Expression::kEffect: |
| 1405 | if_true = &done; |
| 1406 | if_false = &done; |
| 1407 | break; |
| 1408 | case Expression::kValue: |
| 1409 | if_true = &materialize_false; |
| 1410 | if_false = &materialize_true; |
| 1411 | break; |
| 1412 | case Expression::kTest: |
| 1413 | break; |
| 1414 | case Expression::kValueTest: |
| 1415 | if_false = &materialize_true; |
| 1416 | break; |
| 1417 | case Expression::kTestValue: |
| 1418 | if_true = &materialize_false; |
| 1419 | break; |
| 1420 | } |
| 1421 | VisitForControl(expr->expression(), if_true, if_false); |
| 1422 | Apply(context_, if_false, if_true); // Labels swapped. |
| 1423 | break; |
| 1424 | } |
| 1425 | |
| 1426 | case Token::TYPEOF: { |
| 1427 | Comment cmnt(masm_, "[ UnaryOperation (TYPEOF)"); |
| 1428 | VariableProxy* proxy = expr->expression()->AsVariableProxy(); |
| 1429 | if (proxy != NULL && |
| 1430 | !proxy->var()->is_this() && |
| 1431 | proxy->var()->is_global()) { |
| 1432 | Comment cmnt(masm_, "Global variable"); |
| 1433 | __ ldr(r0, CodeGenerator::GlobalObject()); |
| 1434 | __ push(r0); |
| 1435 | __ mov(r2, Operand(proxy->name())); |
| 1436 | Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize)); |
| 1437 | // Use a regular load, not a contextual load, to avoid a reference |
| 1438 | // error. |
| 1439 | __ Call(ic, RelocInfo::CODE_TARGET); |
| 1440 | __ str(r0, MemOperand(sp)); |
| 1441 | } else if (proxy != NULL && |
| 1442 | proxy->var()->slot() != NULL && |
| 1443 | proxy->var()->slot()->type() == Slot::LOOKUP) { |
| 1444 | __ mov(r0, Operand(proxy->name())); |
| 1445 | __ stm(db_w, sp, cp.bit() | r0.bit()); |
| 1446 | __ CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2); |
| 1447 | __ push(r0); |
| 1448 | } else { |
| 1449 | // This expression cannot throw a reference error at the top level. |
| 1450 | VisitForValue(expr->expression(), kStack); |
| 1451 | } |
| 1452 | |
| 1453 | __ CallRuntime(Runtime::kTypeof, 1); |
| 1454 | Apply(context_, r0); |
| 1455 | break; |
| 1456 | } |
| 1457 | |
| 1458 | case Token::ADD: { |
| 1459 | Comment cmt(masm_, "[ UnaryOperation (ADD)"); |
| 1460 | VisitForValue(expr->expression(), kAccumulator); |
| 1461 | Label no_conversion; |
| 1462 | __ tst(result_register(), Operand(kSmiTagMask)); |
| 1463 | __ b(eq, &no_conversion); |
| 1464 | __ push(r0); |
| 1465 | __ InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS); |
| 1466 | __ bind(&no_conversion); |
| 1467 | Apply(context_, result_register()); |
| 1468 | break; |
| 1469 | } |
| 1470 | |
| 1471 | case Token::SUB: { |
| 1472 | Comment cmt(masm_, "[ UnaryOperation (SUB)"); |
| 1473 | bool overwrite = |
| 1474 | (expr->expression()->AsBinaryOperation() != NULL && |
| 1475 | expr->expression()->AsBinaryOperation()->ResultOverwriteAllowed()); |
| 1476 | GenericUnaryOpStub stub(Token::SUB, overwrite); |
| 1477 | // GenericUnaryOpStub expects the argument to be in the |
| 1478 | // accumulator register r0. |
| 1479 | VisitForValue(expr->expression(), kAccumulator); |
| 1480 | __ CallStub(&stub); |
| 1481 | Apply(context_, r0); |
| 1482 | break; |
| 1483 | } |
| 1484 | |
| 1485 | case Token::BIT_NOT: { |
| 1486 | Comment cmt(masm_, "[ UnaryOperation (BIT_NOT)"); |
| 1487 | bool overwrite = |
| 1488 | (expr->expression()->AsBinaryOperation() != NULL && |
| 1489 | expr->expression()->AsBinaryOperation()->ResultOverwriteAllowed()); |
| 1490 | GenericUnaryOpStub stub(Token::BIT_NOT, overwrite); |
| 1491 | // GenericUnaryOpStub expects the argument to be in the |
| 1492 | // accumulator register r0. |
| 1493 | VisitForValue(expr->expression(), kAccumulator); |
| 1494 | // Avoid calling the stub for Smis. |
| 1495 | Label smi, done; |
| 1496 | __ tst(result_register(), Operand(kSmiTagMask)); |
| 1497 | __ b(eq, &smi); |
| 1498 | // Non-smi: call stub leaving result in accumulator register. |
| 1499 | __ CallStub(&stub); |
| 1500 | __ b(&done); |
| 1501 | // Perform operation directly on Smis. |
| 1502 | __ bind(&smi); |
| 1503 | __ mvn(result_register(), Operand(result_register())); |
| 1504 | // Bit-clear inverted smi-tag. |
| 1505 | __ bic(result_register(), result_register(), Operand(kSmiTagMask)); |
| 1506 | __ bind(&done); |
| 1507 | Apply(context_, result_register()); |
| 1508 | break; |
| 1509 | } |
| 1510 | |
| 1511 | default: |
| 1512 | UNREACHABLE(); |
| 1513 | } |
| 1514 | } |
| 1515 | |
| 1516 | |
| 1517 | void FullCodeGenerator::VisitCountOperation(CountOperation* expr) { |
| 1518 | Comment cmnt(masm_, "[ CountOperation"); |
| 1519 | |
| 1520 | // Expression can only be a property, a global or a (parameter or local) |
| 1521 | // slot. Variables with rewrite to .arguments are treated as KEYED_PROPERTY. |
| 1522 | enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY }; |
| 1523 | LhsKind assign_type = VARIABLE; |
| 1524 | Property* prop = expr->expression()->AsProperty(); |
| 1525 | // In case of a property we use the uninitialized expression context |
| 1526 | // of the key to detect a named property. |
| 1527 | if (prop != NULL) { |
| 1528 | assign_type = |
| 1529 | (prop->key()->IsPropertyName()) ? NAMED_PROPERTY : KEYED_PROPERTY; |
| 1530 | } |
| 1531 | |
| 1532 | // Evaluate expression and get value. |
| 1533 | if (assign_type == VARIABLE) { |
| 1534 | ASSERT(expr->expression()->AsVariableProxy()->var() != NULL); |
| 1535 | Location saved_location = location_; |
| 1536 | location_ = kAccumulator; |
| 1537 | EmitVariableLoad(expr->expression()->AsVariableProxy()->var(), |
| 1538 | Expression::kValue); |
| 1539 | location_ = saved_location; |
| 1540 | } else { |
| 1541 | // Reserve space for result of postfix operation. |
| 1542 | if (expr->is_postfix() && context_ != Expression::kEffect) { |
| 1543 | __ mov(ip, Operand(Smi::FromInt(0))); |
| 1544 | __ push(ip); |
| 1545 | } |
| 1546 | VisitForValue(prop->obj(), kStack); |
| 1547 | if (assign_type == NAMED_PROPERTY) { |
| 1548 | EmitNamedPropertyLoad(prop); |
| 1549 | } else { |
| 1550 | VisitForValue(prop->key(), kStack); |
| 1551 | EmitKeyedPropertyLoad(prop); |
| 1552 | } |
| 1553 | } |
| 1554 | |
| 1555 | // Call ToNumber only if operand is not a smi. |
| 1556 | Label no_conversion; |
| 1557 | __ tst(r0, Operand(kSmiTagMask)); |
| 1558 | __ b(eq, &no_conversion); |
| 1559 | __ push(r0); |
| 1560 | __ InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS); |
| 1561 | __ bind(&no_conversion); |
| 1562 | |
| 1563 | // Save result for postfix expressions. |
| 1564 | if (expr->is_postfix()) { |
| 1565 | switch (context_) { |
| 1566 | case Expression::kUninitialized: |
| 1567 | UNREACHABLE(); |
| 1568 | case Expression::kEffect: |
| 1569 | // Do not save result. |
| 1570 | break; |
| 1571 | case Expression::kValue: |
| 1572 | case Expression::kTest: |
| 1573 | case Expression::kValueTest: |
| 1574 | case Expression::kTestValue: |
| 1575 | // Save the result on the stack. If we have a named or keyed property |
| 1576 | // we store the result under the receiver that is currently on top |
| 1577 | // of the stack. |
| 1578 | switch (assign_type) { |
| 1579 | case VARIABLE: |
| 1580 | __ push(r0); |
| 1581 | break; |
| 1582 | case NAMED_PROPERTY: |
| 1583 | __ str(r0, MemOperand(sp, kPointerSize)); |
| 1584 | break; |
| 1585 | case KEYED_PROPERTY: |
| 1586 | __ str(r0, MemOperand(sp, 2 * kPointerSize)); |
| 1587 | break; |
| 1588 | } |
| 1589 | break; |
| 1590 | } |
| 1591 | } |
| 1592 | |
| 1593 | |
| 1594 | // Inline smi case if we are in a loop. |
| 1595 | Label stub_call, done; |
| 1596 | if (loop_depth() > 0) { |
| 1597 | __ add(r0, r0, Operand(expr->op() == Token::INC |
| 1598 | ? Smi::FromInt(1) |
| 1599 | : Smi::FromInt(-1))); |
| 1600 | __ b(vs, &stub_call); |
| 1601 | // We could eliminate this smi check if we split the code at |
| 1602 | // the first smi check before calling ToNumber. |
| 1603 | __ tst(r0, Operand(kSmiTagMask)); |
| 1604 | __ b(eq, &done); |
| 1605 | __ bind(&stub_call); |
| 1606 | // Call stub. Undo operation first. |
| 1607 | __ sub(r0, r0, Operand(r1)); |
| 1608 | } |
| 1609 | __ mov(r1, Operand(expr->op() == Token::INC |
| 1610 | ? Smi::FromInt(1) |
| 1611 | : Smi::FromInt(-1))); |
| 1612 | GenericBinaryOpStub stub(Token::ADD, NO_OVERWRITE); |
| 1613 | __ CallStub(&stub); |
| 1614 | __ bind(&done); |
| 1615 | |
| 1616 | // Store the value returned in r0. |
| 1617 | switch (assign_type) { |
| 1618 | case VARIABLE: |
| 1619 | if (expr->is_postfix()) { |
| 1620 | EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(), |
| 1621 | Expression::kEffect); |
| 1622 | // For all contexts except kEffect: We have the result on |
| 1623 | // top of the stack. |
| 1624 | if (context_ != Expression::kEffect) { |
| 1625 | ApplyTOS(context_); |
| 1626 | } |
| 1627 | } else { |
| 1628 | EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(), |
| 1629 | context_); |
| 1630 | } |
| 1631 | break; |
| 1632 | case NAMED_PROPERTY: { |
| 1633 | __ mov(r2, Operand(prop->key()->AsLiteral()->handle())); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1634 | __ pop(r1); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1635 | Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize)); |
| 1636 | __ Call(ic, RelocInfo::CODE_TARGET); |
| 1637 | if (expr->is_postfix()) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1638 | if (context_ != Expression::kEffect) { |
| 1639 | ApplyTOS(context_); |
| 1640 | } |
| 1641 | } else { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1642 | Apply(context_, r0); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1643 | } |
| 1644 | break; |
| 1645 | } |
| 1646 | case KEYED_PROPERTY: { |
| 1647 | Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize)); |
| 1648 | __ Call(ic, RelocInfo::CODE_TARGET); |
| 1649 | if (expr->is_postfix()) { |
| 1650 | __ Drop(2); // Result is on the stack under the key and the receiver. |
| 1651 | if (context_ != Expression::kEffect) { |
| 1652 | ApplyTOS(context_); |
| 1653 | } |
| 1654 | } else { |
| 1655 | DropAndApply(2, context_, r0); |
| 1656 | } |
| 1657 | break; |
| 1658 | } |
| 1659 | } |
| 1660 | } |
| 1661 | |
| 1662 | |
| 1663 | void FullCodeGenerator::VisitBinaryOperation(BinaryOperation* expr) { |
| 1664 | Comment cmnt(masm_, "[ BinaryOperation"); |
| 1665 | switch (expr->op()) { |
| 1666 | case Token::COMMA: |
| 1667 | VisitForEffect(expr->left()); |
| 1668 | Visit(expr->right()); |
| 1669 | break; |
| 1670 | |
| 1671 | case Token::OR: |
| 1672 | case Token::AND: |
| 1673 | EmitLogicalOperation(expr); |
| 1674 | break; |
| 1675 | |
| 1676 | case Token::ADD: |
| 1677 | case Token::SUB: |
| 1678 | case Token::DIV: |
| 1679 | case Token::MOD: |
| 1680 | case Token::MUL: |
| 1681 | case Token::BIT_OR: |
| 1682 | case Token::BIT_AND: |
| 1683 | case Token::BIT_XOR: |
| 1684 | case Token::SHL: |
| 1685 | case Token::SHR: |
| 1686 | case Token::SAR: |
| 1687 | VisitForValue(expr->left(), kStack); |
| 1688 | VisitForValue(expr->right(), kAccumulator); |
| 1689 | EmitBinaryOp(expr->op(), context_); |
| 1690 | break; |
| 1691 | |
| 1692 | default: |
| 1693 | UNREACHABLE(); |
| 1694 | } |
| 1695 | } |
| 1696 | |
| 1697 | |
| 1698 | void FullCodeGenerator::VisitCompareOperation(CompareOperation* expr) { |
| 1699 | Comment cmnt(masm_, "[ CompareOperation"); |
| 1700 | |
| 1701 | // Always perform the comparison for its control flow. Pack the result |
| 1702 | // into the expression's context after the comparison is performed. |
| 1703 | Label materialize_true, materialize_false, done; |
| 1704 | // Initially assume we are in a test context. |
| 1705 | Label* if_true = true_label_; |
| 1706 | Label* if_false = false_label_; |
| 1707 | switch (context_) { |
| 1708 | case Expression::kUninitialized: |
| 1709 | UNREACHABLE(); |
| 1710 | break; |
| 1711 | case Expression::kEffect: |
| 1712 | if_true = &done; |
| 1713 | if_false = &done; |
| 1714 | break; |
| 1715 | case Expression::kValue: |
| 1716 | if_true = &materialize_true; |
| 1717 | if_false = &materialize_false; |
| 1718 | break; |
| 1719 | case Expression::kTest: |
| 1720 | break; |
| 1721 | case Expression::kValueTest: |
| 1722 | if_true = &materialize_true; |
| 1723 | break; |
| 1724 | case Expression::kTestValue: |
| 1725 | if_false = &materialize_false; |
| 1726 | break; |
| 1727 | } |
| 1728 | |
| 1729 | VisitForValue(expr->left(), kStack); |
| 1730 | switch (expr->op()) { |
| 1731 | case Token::IN: |
| 1732 | VisitForValue(expr->right(), kStack); |
| 1733 | __ InvokeBuiltin(Builtins::IN, CALL_JS); |
| 1734 | __ LoadRoot(ip, Heap::kTrueValueRootIndex); |
| 1735 | __ cmp(r0, ip); |
| 1736 | __ b(eq, if_true); |
| 1737 | __ jmp(if_false); |
| 1738 | break; |
| 1739 | |
| 1740 | case Token::INSTANCEOF: { |
| 1741 | VisitForValue(expr->right(), kStack); |
| 1742 | InstanceofStub stub; |
| 1743 | __ CallStub(&stub); |
| 1744 | __ tst(r0, r0); |
| 1745 | __ b(eq, if_true); // The stub returns 0 for true. |
| 1746 | __ jmp(if_false); |
| 1747 | break; |
| 1748 | } |
| 1749 | |
| 1750 | default: { |
| 1751 | VisitForValue(expr->right(), kAccumulator); |
| 1752 | Condition cc = eq; |
| 1753 | bool strict = false; |
| 1754 | switch (expr->op()) { |
| 1755 | case Token::EQ_STRICT: |
| 1756 | strict = true; |
| 1757 | // Fall through |
| 1758 | case Token::EQ: |
| 1759 | cc = eq; |
| 1760 | __ pop(r1); |
| 1761 | break; |
| 1762 | case Token::LT: |
| 1763 | cc = lt; |
| 1764 | __ pop(r1); |
| 1765 | break; |
| 1766 | case Token::GT: |
| 1767 | // Reverse left and right sides to obtain ECMA-262 conversion order. |
| 1768 | cc = lt; |
| 1769 | __ mov(r1, result_register()); |
| 1770 | __ pop(r0); |
| 1771 | break; |
| 1772 | case Token::LTE: |
| 1773 | // Reverse left and right sides to obtain ECMA-262 conversion order. |
| 1774 | cc = ge; |
| 1775 | __ mov(r1, result_register()); |
| 1776 | __ pop(r0); |
| 1777 | break; |
| 1778 | case Token::GTE: |
| 1779 | cc = ge; |
| 1780 | __ pop(r1); |
| 1781 | break; |
| 1782 | case Token::IN: |
| 1783 | case Token::INSTANCEOF: |
| 1784 | default: |
| 1785 | UNREACHABLE(); |
| 1786 | } |
| 1787 | |
| 1788 | // The comparison stub expects the smi vs. smi case to be handled |
| 1789 | // before it is called. |
| 1790 | Label slow_case; |
| 1791 | __ orr(r2, r0, Operand(r1)); |
| 1792 | __ tst(r2, Operand(kSmiTagMask)); |
| 1793 | __ b(ne, &slow_case); |
| 1794 | __ cmp(r1, r0); |
| 1795 | __ b(cc, if_true); |
| 1796 | __ jmp(if_false); |
| 1797 | |
| 1798 | __ bind(&slow_case); |
| 1799 | CompareStub stub(cc, strict); |
| 1800 | __ CallStub(&stub); |
| 1801 | __ cmp(r0, Operand(0)); |
| 1802 | __ b(cc, if_true); |
| 1803 | __ jmp(if_false); |
| 1804 | } |
| 1805 | } |
| 1806 | |
| 1807 | // Convert the result of the comparison into one expected for this |
| 1808 | // expression's context. |
| 1809 | Apply(context_, if_true, if_false); |
| 1810 | } |
| 1811 | |
| 1812 | |
| 1813 | void FullCodeGenerator::VisitThisFunction(ThisFunction* expr) { |
| 1814 | __ ldr(r0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset)); |
| 1815 | Apply(context_, r0); |
| 1816 | } |
| 1817 | |
| 1818 | |
| 1819 | Register FullCodeGenerator::result_register() { return r0; } |
| 1820 | |
| 1821 | |
| 1822 | Register FullCodeGenerator::context_register() { return cp; } |
| 1823 | |
| 1824 | |
| 1825 | void FullCodeGenerator::StoreToFrameField(int frame_offset, Register value) { |
| 1826 | ASSERT_EQ(POINTER_SIZE_ALIGN(frame_offset), frame_offset); |
| 1827 | __ str(value, MemOperand(fp, frame_offset)); |
| 1828 | } |
| 1829 | |
| 1830 | |
| 1831 | void FullCodeGenerator::LoadContextField(Register dst, int context_index) { |
| 1832 | __ ldr(dst, CodeGenerator::ContextOperand(cp, context_index)); |
| 1833 | } |
| 1834 | |
| 1835 | |
| 1836 | // ---------------------------------------------------------------------------- |
| 1837 | // Non-local control flow support. |
| 1838 | |
| 1839 | void FullCodeGenerator::EnterFinallyBlock() { |
| 1840 | ASSERT(!result_register().is(r1)); |
| 1841 | // Store result register while executing finally block. |
| 1842 | __ push(result_register()); |
| 1843 | // Cook return address in link register to stack (smi encoded Code* delta) |
| 1844 | __ sub(r1, lr, Operand(masm_->CodeObject())); |
| 1845 | ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize); |
| 1846 | ASSERT_EQ(0, kSmiTag); |
| 1847 | __ add(r1, r1, Operand(r1)); // Convert to smi. |
| 1848 | __ push(r1); |
| 1849 | } |
| 1850 | |
| 1851 | |
| 1852 | void FullCodeGenerator::ExitFinallyBlock() { |
| 1853 | ASSERT(!result_register().is(r1)); |
| 1854 | // Restore result register from stack. |
| 1855 | __ pop(r1); |
| 1856 | // Uncook return address and return. |
| 1857 | __ pop(result_register()); |
| 1858 | ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize); |
| 1859 | __ mov(r1, Operand(r1, ASR, 1)); // Un-smi-tag value. |
| 1860 | __ add(pc, r1, Operand(masm_->CodeObject())); |
| 1861 | } |
| 1862 | |
| 1863 | |
| 1864 | #undef __ |
| 1865 | |
| 1866 | } } // namespace v8::internal |