jkummerow@chromium.org | d8a3a14 | 2013-10-03 12:15:05 +0000 | [diff] [blame] | 1 | // Copyright 2013 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 "lithium-codegen.h" |
| 31 | |
| 32 | #if V8_TARGET_ARCH_IA32 |
| 33 | #include "ia32/lithium-ia32.h" |
| 34 | #include "ia32/lithium-codegen-ia32.h" |
| 35 | #elif V8_TARGET_ARCH_X64 |
| 36 | #include "x64/lithium-x64.h" |
| 37 | #include "x64/lithium-codegen-x64.h" |
| 38 | #elif V8_TARGET_ARCH_ARM |
| 39 | #include "arm/lithium-arm.h" |
| 40 | #include "arm/lithium-codegen-arm.h" |
| 41 | #elif V8_TARGET_ARCH_MIPS |
| 42 | #include "mips/lithium-mips.h" |
| 43 | #include "mips/lithium-codegen-mips.h" |
| 44 | #else |
| 45 | #error Unsupported target architecture. |
| 46 | #endif |
| 47 | |
| 48 | namespace v8 { |
| 49 | namespace internal { |
| 50 | |
| 51 | |
| 52 | HGraph* LCodeGenBase::graph() const { |
| 53 | return chunk()->graph(); |
| 54 | } |
| 55 | |
| 56 | |
| 57 | LCodeGenBase::LCodeGenBase(LChunk* chunk, |
| 58 | MacroAssembler* assembler, |
| 59 | CompilationInfo* info) |
| 60 | : chunk_(static_cast<LPlatformChunk*>(chunk)), |
| 61 | masm_(assembler), |
| 62 | info_(info), |
| 63 | zone_(info->zone()), |
| 64 | status_(UNUSED), |
| 65 | current_block_(-1), |
| 66 | current_instruction_(-1), |
| 67 | instructions_(chunk->instructions()), |
| 68 | last_lazy_deopt_pc_(0) { |
| 69 | } |
| 70 | |
| 71 | |
| 72 | bool LCodeGenBase::GenerateBody() { |
| 73 | ASSERT(is_generating()); |
| 74 | bool emit_instructions = true; |
| 75 | LCodeGen* codegen = static_cast<LCodeGen*>(this); |
| 76 | for (current_instruction_ = 0; |
| 77 | !is_aborted() && current_instruction_ < instructions_->length(); |
| 78 | current_instruction_++) { |
| 79 | LInstruction* instr = instructions_->at(current_instruction_); |
| 80 | |
| 81 | // Don't emit code for basic blocks with a replacement. |
| 82 | if (instr->IsLabel()) { |
| 83 | emit_instructions = !LLabel::cast(instr)->HasReplacement() && |
| 84 | (!FLAG_unreachable_code_elimination || |
| 85 | instr->hydrogen_value()->block()->IsReachable()); |
| 86 | if (FLAG_code_comments && !emit_instructions) { |
| 87 | Comment( |
| 88 | ";;; <@%d,#%d> -------------------- B%d (unreachable/replaced) " |
| 89 | "--------------------", |
| 90 | current_instruction_, |
| 91 | instr->hydrogen_value()->id(), |
| 92 | instr->hydrogen_value()->block()->block_id()); |
| 93 | } |
| 94 | } |
| 95 | if (!emit_instructions) continue; |
| 96 | |
| 97 | if (FLAG_code_comments && instr->HasInterestingComment(codegen)) { |
| 98 | Comment(";;; <@%d,#%d> %s", |
| 99 | current_instruction_, |
| 100 | instr->hydrogen_value()->id(), |
| 101 | instr->Mnemonic()); |
| 102 | } |
| 103 | |
| 104 | GenerateBodyInstructionPre(instr); |
| 105 | |
| 106 | RecordAndUpdatePosition(instr->position()); |
| 107 | |
| 108 | instr->CompileToNative(codegen); |
| 109 | |
| 110 | GenerateBodyInstructionPost(instr); |
| 111 | } |
| 112 | EnsureSpaceForLazyDeopt(Deoptimizer::patch_size()); |
| 113 | last_lazy_deopt_pc_ = masm()->pc_offset(); |
| 114 | return !is_aborted(); |
| 115 | } |
| 116 | |
| 117 | |
| 118 | void LCodeGenBase::Comment(const char* format, ...) { |
| 119 | if (!FLAG_code_comments) return; |
| 120 | char buffer[4 * KB]; |
| 121 | StringBuilder builder(buffer, ARRAY_SIZE(buffer)); |
| 122 | va_list arguments; |
| 123 | va_start(arguments, format); |
| 124 | builder.AddFormattedList(format, arguments); |
| 125 | va_end(arguments); |
| 126 | |
| 127 | // Copy the string before recording it in the assembler to avoid |
| 128 | // issues when the stack allocated buffer goes out of scope. |
| 129 | size_t length = builder.position(); |
| 130 | Vector<char> copy = Vector<char>::New(static_cast<int>(length) + 1); |
| 131 | OS::MemCopy(copy.start(), builder.Finalize(), copy.length()); |
| 132 | masm()->RecordComment(copy.start()); |
| 133 | } |
| 134 | |
| 135 | |
| 136 | int LCodeGenBase::GetNextEmittedBlock() const { |
| 137 | for (int i = current_block_ + 1; i < graph()->blocks()->length(); ++i) { |
| 138 | if (!chunk_->GetLabel(i)->HasReplacement()) return i; |
| 139 | } |
| 140 | return -1; |
| 141 | } |
| 142 | |
| 143 | |
| 144 | } } // namespace v8::internal |