1 // Copyright 2013 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "src/lithium-codegen.h"
11 #if V8_TARGET_ARCH_IA32
12 #include "src/ia32/lithium-ia32.h" // NOLINT
13 #include "src/ia32/lithium-codegen-ia32.h" // NOLINT
14 #elif V8_TARGET_ARCH_X64
15 #include "src/x64/lithium-x64.h" // NOLINT
16 #include "src/x64/lithium-codegen-x64.h" // NOLINT
17 #elif V8_TARGET_ARCH_ARM
18 #include "src/arm/lithium-arm.h" // NOLINT
19 #include "src/arm/lithium-codegen-arm.h" // NOLINT
20 #elif V8_TARGET_ARCH_ARM64
21 #include "src/arm64/lithium-arm64.h" // NOLINT
22 #include "src/arm64/lithium-codegen-arm64.h" // NOLINT
23 #elif V8_TARGET_ARCH_MIPS
24 #include "src/mips/lithium-mips.h" // NOLINT
25 #include "src/mips/lithium-codegen-mips.h" // NOLINT
26 #elif V8_TARGET_ARCH_MIPS64
27 #include "src/mips64/lithium-mips64.h" // NOLINT
28 #include "src/mips64/lithium-codegen-mips64.h" // NOLINT
29 #elif V8_TARGET_ARCH_X87
30 #include "src/x87/lithium-x87.h" // NOLINT
31 #include "src/x87/lithium-codegen-x87.h" // NOLINT
32 #elif V8_TARGET_ARCH_PPC
33 #include "src/ppc/lithium-ppc.h" // NOLINT
34 #include "src/ppc/lithium-codegen-ppc.h" // NOLINT
36 #error Unsupported target architecture.
43 HGraph* LCodeGenBase::graph() const {
44 return chunk()->graph();
48 LCodeGenBase::LCodeGenBase(LChunk* chunk,
49 MacroAssembler* assembler,
50 CompilationInfo* info)
51 : chunk_(static_cast<LPlatformChunk*>(chunk)),
57 current_instruction_(-1),
58 instructions_(chunk->instructions()),
59 last_lazy_deopt_pc_(0) {
63 bool LCodeGenBase::GenerateBody() {
64 DCHECK(is_generating());
65 bool emit_instructions = true;
66 LCodeGen* codegen = static_cast<LCodeGen*>(this);
67 for (current_instruction_ = 0;
68 !is_aborted() && current_instruction_ < instructions_->length();
69 current_instruction_++) {
70 LInstruction* instr = instructions_->at(current_instruction_);
72 // Don't emit code for basic blocks with a replacement.
73 if (instr->IsLabel()) {
74 emit_instructions = !LLabel::cast(instr)->HasReplacement() &&
75 (!FLAG_unreachable_code_elimination ||
76 instr->hydrogen_value()->block()->IsReachable());
77 if (FLAG_code_comments && !emit_instructions) {
79 ";;; <@%d,#%d> -------------------- B%d (unreachable/replaced) "
80 "--------------------",
82 instr->hydrogen_value()->id(),
83 instr->hydrogen_value()->block()->block_id());
86 if (!emit_instructions) continue;
88 if (FLAG_code_comments && instr->HasInterestingComment(codegen)) {
89 Comment(";;; <@%d,#%d> %s",
91 instr->hydrogen_value()->id(),
95 GenerateBodyInstructionPre(instr);
97 HValue* value = instr->hydrogen_value();
98 if (!value->position().IsUnknown()) {
99 RecordAndWritePosition(
100 chunk()->graph()->SourcePositionToScriptPosition(value->position()));
103 instr->CompileToNative(codegen);
105 GenerateBodyInstructionPost(instr);
107 EnsureSpaceForLazyDeopt(Deoptimizer::patch_size());
108 last_lazy_deopt_pc_ = masm()->pc_offset();
109 return !is_aborted();
113 void LCodeGenBase::CheckEnvironmentUsage() {
115 bool dead_block = false;
116 for (int i = 0; i < instructions_->length(); i++) {
117 LInstruction* instr = instructions_->at(i);
118 HValue* hval = instr->hydrogen_value();
119 if (instr->IsLabel()) dead_block = LLabel::cast(instr)->HasReplacement();
120 if (dead_block || !hval->block()->IsReachable()) continue;
122 HInstruction* hinstr = HInstruction::cast(hval);
123 if (!hinstr->CanDeoptimize() && instr->HasEnvironment()) {
124 V8_Fatal(__FILE__, __LINE__, "CanDeoptimize is wrong for %s (%s)",
125 hinstr->Mnemonic(), instr->Mnemonic());
128 if (instr->HasEnvironment() && !instr->environment()->has_been_used()) {
129 V8_Fatal(__FILE__, __LINE__, "unused environment for %s (%s)",
130 hinstr->Mnemonic(), instr->Mnemonic());
137 void LCodeGenBase::Comment(const char* format, ...) {
138 if (!FLAG_code_comments) return;
140 StringBuilder builder(buffer, arraysize(buffer));
142 va_start(arguments, format);
143 builder.AddFormattedList(format, arguments);
146 // Copy the string before recording it in the assembler to avoid
147 // issues when the stack allocated buffer goes out of scope.
148 size_t length = builder.position();
149 Vector<char> copy = Vector<char>::New(static_cast<int>(length) + 1);
150 MemCopy(copy.start(), builder.Finalize(), copy.length());
151 masm()->RecordComment(copy.start());
155 void LCodeGenBase::DeoptComment(const Deoptimizer::DeoptInfo& deopt_info) {
156 masm()->RecordDeoptReason(deopt_info.deopt_reason, deopt_info.position);
160 int LCodeGenBase::GetNextEmittedBlock() const {
161 for (int i = current_block_ + 1; i < graph()->blocks()->length(); ++i) {
162 if (!graph()->blocks()->at(i)->IsReachable()) continue;
163 if (!chunk_->GetLabel(i)->HasReplacement()) return i;
169 void LCodeGenBase::Abort(BailoutReason reason) {
170 info()->AbortOptimization(reason);
175 void LCodeGenBase::Retry(BailoutReason reason) {
176 info()->RetryOptimization(reason);
181 void LCodeGenBase::AddDeprecationDependency(Handle<Map> map) {
182 if (map->is_deprecated()) return Retry(kMapBecameDeprecated);
183 chunk_->AddDeprecationDependency(map);
187 void LCodeGenBase::AddStabilityDependency(Handle<Map> map) {
188 if (!map->is_stable()) return Retry(kMapBecameUnstable);
189 chunk_->AddStabilityDependency(map);
193 Deoptimizer::DeoptInfo LCodeGenBase::MakeDeoptInfo(
194 LInstruction* instr, Deoptimizer::DeoptReason deopt_reason) {
195 Deoptimizer::DeoptInfo deopt_info(instr->hydrogen_value()->position(),
196 instr->Mnemonic(), deopt_reason);
197 HEnterInlined* enter_inlined = instr->environment()->entry();
198 deopt_info.inlining_id = enter_inlined ? enter_inlined->inlining_id() : 0;
201 } } // namespace v8::internal