242db222f2db03c60eaa3828cc1a02e36c119981
[platform/upstream/nodejs.git] / deps / v8 / src / lithium-codegen.cc
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.
4
5 #include "src/lithium-codegen.h"
6
7 #include <sstream>
8
9 #include "src/v8.h"
10
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
35 #else
36 #error Unsupported target architecture.
37 #endif
38
39 namespace v8 {
40 namespace internal {
41
42
43 HGraph* LCodeGenBase::graph() const {
44   return chunk()->graph();
45 }
46
47
48 LCodeGenBase::LCodeGenBase(LChunk* chunk,
49                            MacroAssembler* assembler,
50                            CompilationInfo* info)
51     : chunk_(static_cast<LPlatformChunk*>(chunk)),
52       masm_(assembler),
53       info_(info),
54       zone_(info->zone()),
55       status_(UNUSED),
56       current_block_(-1),
57       current_instruction_(-1),
58       instructions_(chunk->instructions()),
59       last_lazy_deopt_pc_(0) {
60 }
61
62
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_);
71
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) {
78         Comment(
79             ";;; <@%d,#%d> -------------------- B%d (unreachable/replaced) "
80             "--------------------",
81             current_instruction_,
82             instr->hydrogen_value()->id(),
83             instr->hydrogen_value()->block()->block_id());
84       }
85     }
86     if (!emit_instructions) continue;
87
88     if (FLAG_code_comments && instr->HasInterestingComment(codegen)) {
89       Comment(";;; <@%d,#%d> %s",
90               current_instruction_,
91               instr->hydrogen_value()->id(),
92               instr->Mnemonic());
93     }
94
95     GenerateBodyInstructionPre(instr);
96
97     HValue* value = instr->hydrogen_value();
98     if (!value->position().IsUnknown()) {
99       RecordAndWritePosition(
100         chunk()->graph()->SourcePositionToScriptPosition(value->position()));
101     }
102
103     instr->CompileToNative(codegen);
104
105     GenerateBodyInstructionPost(instr);
106   }
107   EnsureSpaceForLazyDeopt(Deoptimizer::patch_size());
108   last_lazy_deopt_pc_ = masm()->pc_offset();
109   return !is_aborted();
110 }
111
112
113 void LCodeGenBase::CheckEnvironmentUsage() {
114 #ifdef DEBUG
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;
121
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());
126     }
127
128     if (instr->HasEnvironment() && !instr->environment()->has_been_used()) {
129       V8_Fatal(__FILE__, __LINE__, "unused environment for %s (%s)",
130                hinstr->Mnemonic(), instr->Mnemonic());
131     }
132   }
133 #endif
134 }
135
136
137 void LCodeGenBase::Comment(const char* format, ...) {
138   if (!FLAG_code_comments) return;
139   char buffer[4 * KB];
140   StringBuilder builder(buffer, arraysize(buffer));
141   va_list arguments;
142   va_start(arguments, format);
143   builder.AddFormattedList(format, arguments);
144   va_end(arguments);
145
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());
152 }
153
154
155 void LCodeGenBase::DeoptComment(const Deoptimizer::DeoptInfo& deopt_info) {
156   masm()->RecordDeoptReason(deopt_info.deopt_reason, deopt_info.raw_position);
157 }
158
159
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;
164   }
165   return -1;
166 }
167
168
169 void LCodeGenBase::Abort(BailoutReason reason) {
170   info()->AbortOptimization(reason);
171   status_ = ABORTED;
172 }
173
174
175 void LCodeGenBase::Retry(BailoutReason reason) {
176   info()->RetryOptimization(reason);
177   status_ = ABORTED;
178 }
179
180
181 void LCodeGenBase::AddDeprecationDependency(Handle<Map> map) {
182   if (map->is_deprecated()) return Retry(kMapBecameDeprecated);
183   chunk_->AddDeprecationDependency(map);
184 }
185
186
187 void LCodeGenBase::AddStabilityDependency(Handle<Map> map) {
188   if (!map->is_stable()) return Retry(kMapBecameUnstable);
189   chunk_->AddStabilityDependency(map);
190 }
191
192 } }  // namespace v8::internal