blob: 4534b46d80206a227fd44ca587b54909534b4cd5 [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// 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
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005#include "src/lithium-codegen.h"
6
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007#include <sstream>
8
9#include "src/v8.h"
10
Ben Murdochb8a8cc12014-11-26 15:28:44 +000011#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#else
33#error Unsupported target architecture.
34#endif
35
36namespace v8 {
37namespace internal {
38
39
40HGraph* LCodeGenBase::graph() const {
41 return chunk()->graph();
42}
43
44
45LCodeGenBase::LCodeGenBase(LChunk* chunk,
46 MacroAssembler* assembler,
47 CompilationInfo* info)
48 : chunk_(static_cast<LPlatformChunk*>(chunk)),
49 masm_(assembler),
50 info_(info),
51 zone_(info->zone()),
52 status_(UNUSED),
53 current_block_(-1),
54 current_instruction_(-1),
55 instructions_(chunk->instructions()),
56 last_lazy_deopt_pc_(0) {
57}
58
59
60bool LCodeGenBase::GenerateBody() {
61 DCHECK(is_generating());
62 bool emit_instructions = true;
63 LCodeGen* codegen = static_cast<LCodeGen*>(this);
64 for (current_instruction_ = 0;
65 !is_aborted() && current_instruction_ < instructions_->length();
66 current_instruction_++) {
67 LInstruction* instr = instructions_->at(current_instruction_);
68
69 // Don't emit code for basic blocks with a replacement.
70 if (instr->IsLabel()) {
71 emit_instructions = !LLabel::cast(instr)->HasReplacement() &&
72 (!FLAG_unreachable_code_elimination ||
73 instr->hydrogen_value()->block()->IsReachable());
74 if (FLAG_code_comments && !emit_instructions) {
75 Comment(
76 ";;; <@%d,#%d> -------------------- B%d (unreachable/replaced) "
77 "--------------------",
78 current_instruction_,
79 instr->hydrogen_value()->id(),
80 instr->hydrogen_value()->block()->block_id());
81 }
82 }
83 if (!emit_instructions) continue;
84
85 if (FLAG_code_comments && instr->HasInterestingComment(codegen)) {
86 Comment(";;; <@%d,#%d> %s",
87 current_instruction_,
88 instr->hydrogen_value()->id(),
89 instr->Mnemonic());
90 }
91
92 GenerateBodyInstructionPre(instr);
93
94 HValue* value = instr->hydrogen_value();
95 if (!value->position().IsUnknown()) {
96 RecordAndWritePosition(
97 chunk()->graph()->SourcePositionToScriptPosition(value->position()));
98 }
99
100 instr->CompileToNative(codegen);
101
102 GenerateBodyInstructionPost(instr);
103 }
104 EnsureSpaceForLazyDeopt(Deoptimizer::patch_size());
105 last_lazy_deopt_pc_ = masm()->pc_offset();
106 return !is_aborted();
107}
108
109
110void LCodeGenBase::CheckEnvironmentUsage() {
111#ifdef DEBUG
112 bool dead_block = false;
113 for (int i = 0; i < instructions_->length(); i++) {
114 LInstruction* instr = instructions_->at(i);
115 HValue* hval = instr->hydrogen_value();
116 if (instr->IsLabel()) dead_block = LLabel::cast(instr)->HasReplacement();
117 if (dead_block || !hval->block()->IsReachable()) continue;
118
119 HInstruction* hinstr = HInstruction::cast(hval);
120 if (!hinstr->CanDeoptimize() && instr->HasEnvironment()) {
121 V8_Fatal(__FILE__, __LINE__, "CanDeoptimize is wrong for %s (%s)",
122 hinstr->Mnemonic(), instr->Mnemonic());
123 }
124
125 if (instr->HasEnvironment() && !instr->environment()->has_been_used()) {
126 V8_Fatal(__FILE__, __LINE__, "unused environment for %s (%s)",
127 hinstr->Mnemonic(), instr->Mnemonic());
128 }
129 }
130#endif
131}
132
133
134void LCodeGenBase::Comment(const char* format, ...) {
135 if (!FLAG_code_comments) return;
136 char buffer[4 * KB];
137 StringBuilder builder(buffer, arraysize(buffer));
138 va_list arguments;
139 va_start(arguments, format);
140 builder.AddFormattedList(format, arguments);
141 va_end(arguments);
142
143 // Copy the string before recording it in the assembler to avoid
144 // issues when the stack allocated buffer goes out of scope.
145 size_t length = builder.position();
146 Vector<char> copy = Vector<char>::New(static_cast<int>(length) + 1);
147 MemCopy(copy.start(), builder.Finalize(), copy.length());
148 masm()->RecordComment(copy.start());
149}
150
151
152void LCodeGenBase::DeoptComment(const Deoptimizer::Reason& reason) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400153 std::ostringstream os;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000154 os << ";;; deoptimize at " << HSourcePosition(reason.raw_position) << " "
155 << reason.mnemonic;
156 if (reason.detail != NULL) os << ": " << reason.detail;
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400157 Comment("%s", os.str().c_str());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000158}
159
160
161int LCodeGenBase::GetNextEmittedBlock() const {
162 for (int i = current_block_ + 1; i < graph()->blocks()->length(); ++i) {
163 if (!graph()->blocks()->at(i)->IsReachable()) continue;
164 if (!chunk_->GetLabel(i)->HasReplacement()) return i;
165 }
166 return -1;
167}
168
169
170static void AddWeakObjectToCodeDependency(Isolate* isolate,
171 Handle<Object> object,
172 Handle<Code> code) {
173 Heap* heap = isolate->heap();
174 heap->EnsureWeakObjectToCodeTable();
175 Handle<DependentCode> dep(heap->LookupWeakObjectToCodeDependency(object));
176 dep = DependentCode::Insert(dep, DependentCode::kWeakCodeGroup, code);
177 heap->AddWeakObjectToCodeDependency(object, dep);
178}
179
180
181void LCodeGenBase::RegisterWeakObjectsInOptimizedCode(Handle<Code> code) {
182 DCHECK(code->is_optimized_code());
183 ZoneList<Handle<Map> > maps(1, zone());
184 ZoneList<Handle<JSObject> > objects(1, zone());
185 ZoneList<Handle<Cell> > cells(1, zone());
186 int mode_mask = RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT) |
187 RelocInfo::ModeMask(RelocInfo::CELL);
188 for (RelocIterator it(*code, mode_mask); !it.done(); it.next()) {
189 RelocInfo::Mode mode = it.rinfo()->rmode();
190 if (mode == RelocInfo::CELL &&
191 code->IsWeakObjectInOptimizedCode(it.rinfo()->target_cell())) {
192 Handle<Cell> cell(it.rinfo()->target_cell());
193 cells.Add(cell, zone());
194 } else if (mode == RelocInfo::EMBEDDED_OBJECT &&
195 code->IsWeakObjectInOptimizedCode(it.rinfo()->target_object())) {
196 if (it.rinfo()->target_object()->IsMap()) {
197 Handle<Map> map(Map::cast(it.rinfo()->target_object()));
198 maps.Add(map, zone());
199 } else if (it.rinfo()->target_object()->IsJSObject()) {
200 Handle<JSObject> object(JSObject::cast(it.rinfo()->target_object()));
201 objects.Add(object, zone());
202 } else if (it.rinfo()->target_object()->IsCell()) {
203 Handle<Cell> cell(Cell::cast(it.rinfo()->target_object()));
204 cells.Add(cell, zone());
205 }
206 }
207 }
208 if (FLAG_enable_ool_constant_pool) {
209 code->constant_pool()->set_weak_object_state(
210 ConstantPoolArray::WEAK_OBJECTS_IN_OPTIMIZED_CODE);
211 }
212#ifdef VERIFY_HEAP
213 // This disables verification of weak embedded objects after full GC.
214 // AddDependentCode can cause a GC, which would observe the state where
215 // this code is not yet in the depended code lists of the embedded maps.
216 NoWeakObjectVerificationScope disable_verification_of_embedded_objects;
217#endif
218 for (int i = 0; i < maps.length(); i++) {
219 Map::AddDependentCode(maps.at(i), DependentCode::kWeakCodeGroup, code);
220 }
221 for (int i = 0; i < objects.length(); i++) {
222 AddWeakObjectToCodeDependency(isolate(), objects.at(i), code);
223 }
224 for (int i = 0; i < cells.length(); i++) {
225 AddWeakObjectToCodeDependency(isolate(), cells.at(i), code);
226 }
227}
228
229
230void LCodeGenBase::Abort(BailoutReason reason) {
231 info()->AbortOptimization(reason);
232 status_ = ABORTED;
233}
234
235
236void LCodeGenBase::Retry(BailoutReason reason) {
237 info()->RetryOptimization(reason);
238 status_ = ABORTED;
239}
240
241
242void LCodeGenBase::AddDeprecationDependency(Handle<Map> map) {
243 if (map->is_deprecated()) return Retry(kMapBecameDeprecated);
244 chunk_->AddDeprecationDependency(map);
245}
246
247
248void LCodeGenBase::AddStabilityDependency(Handle<Map> map) {
249 if (!map->is_stable()) return Retry(kMapBecameUnstable);
250 chunk_->AddStabilityDependency(map);
251}
252
253} } // namespace v8::internal