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.
7 #include "src/lithium-codegen.h"
9 #if V8_TARGET_ARCH_IA32
10 #include "src/ia32/lithium-ia32.h"
11 #include "src/ia32/lithium-codegen-ia32.h"
12 #elif V8_TARGET_ARCH_X64
13 #include "src/x64/lithium-x64.h"
14 #include "src/x64/lithium-codegen-x64.h"
15 #elif V8_TARGET_ARCH_ARM
16 #include "src/arm/lithium-arm.h"
17 #include "src/arm/lithium-codegen-arm.h"
18 #elif V8_TARGET_ARCH_ARM64
19 #include "src/arm64/lithium-arm64.h"
20 #include "src/arm64/lithium-codegen-arm64.h"
21 #elif V8_TARGET_ARCH_MIPS
22 #include "src/mips/lithium-mips.h"
23 #include "src/mips/lithium-codegen-mips.h"
24 #elif V8_TARGET_ARCH_X87
25 #include "src/x87/lithium-x87.h"
26 #include "src/x87/lithium-codegen-x87.h"
28 #error Unsupported target architecture.
35 HGraph* LCodeGenBase::graph() const {
36 return chunk()->graph();
40 LCodeGenBase::LCodeGenBase(LChunk* chunk,
41 MacroAssembler* assembler,
42 CompilationInfo* info)
43 : chunk_(static_cast<LPlatformChunk*>(chunk)),
49 current_instruction_(-1),
50 instructions_(chunk->instructions()),
51 last_lazy_deopt_pc_(0) {
55 bool LCodeGenBase::GenerateBody() {
56 ASSERT(is_generating());
57 bool emit_instructions = true;
58 LCodeGen* codegen = static_cast<LCodeGen*>(this);
59 for (current_instruction_ = 0;
60 !is_aborted() && current_instruction_ < instructions_->length();
61 current_instruction_++) {
62 LInstruction* instr = instructions_->at(current_instruction_);
64 // Don't emit code for basic blocks with a replacement.
65 if (instr->IsLabel()) {
66 emit_instructions = !LLabel::cast(instr)->HasReplacement() &&
67 (!FLAG_unreachable_code_elimination ||
68 instr->hydrogen_value()->block()->IsReachable());
69 if (FLAG_code_comments && !emit_instructions) {
71 ";;; <@%d,#%d> -------------------- B%d (unreachable/replaced) "
72 "--------------------",
74 instr->hydrogen_value()->id(),
75 instr->hydrogen_value()->block()->block_id());
78 if (!emit_instructions) continue;
80 if (FLAG_code_comments && instr->HasInterestingComment(codegen)) {
81 Comment(";;; <@%d,#%d> %s",
83 instr->hydrogen_value()->id(),
87 GenerateBodyInstructionPre(instr);
89 HValue* value = instr->hydrogen_value();
90 if (!value->position().IsUnknown()) {
91 RecordAndWritePosition(
92 chunk()->graph()->SourcePositionToScriptPosition(value->position()));
95 instr->CompileToNative(codegen);
97 GenerateBodyInstructionPost(instr);
99 EnsureSpaceForLazyDeopt(Deoptimizer::patch_size());
100 last_lazy_deopt_pc_ = masm()->pc_offset();
101 return !is_aborted();
105 void LCodeGenBase::CheckEnvironmentUsage() {
107 bool dead_block = false;
108 for (int i = 0; i < instructions_->length(); i++) {
109 LInstruction* instr = instructions_->at(i);
110 HValue* hval = instr->hydrogen_value();
111 if (instr->IsLabel()) dead_block = LLabel::cast(instr)->HasReplacement();
112 if (dead_block || !hval->block()->IsReachable()) continue;
114 HInstruction* hinstr = HInstruction::cast(hval);
115 if (!hinstr->CanDeoptimize() && instr->HasEnvironment()) {
116 V8_Fatal(__FILE__, __LINE__, "CanDeoptimize is wrong for %s (%s)\n",
117 hinstr->Mnemonic(), instr->Mnemonic());
120 if (instr->HasEnvironment() && !instr->environment()->has_been_used()) {
121 V8_Fatal(__FILE__, __LINE__, "unused environment for %s (%s)\n",
122 hinstr->Mnemonic(), instr->Mnemonic());
129 void LCodeGenBase::Comment(const char* format, ...) {
130 if (!FLAG_code_comments) return;
132 StringBuilder builder(buffer, ARRAY_SIZE(buffer));
134 va_start(arguments, format);
135 builder.AddFormattedList(format, arguments);
138 // Copy the string before recording it in the assembler to avoid
139 // issues when the stack allocated buffer goes out of scope.
140 size_t length = builder.position();
141 Vector<char> copy = Vector<char>::New(static_cast<int>(length) + 1);
142 MemCopy(copy.start(), builder.Finalize(), copy.length());
143 masm()->RecordComment(copy.start());
147 int LCodeGenBase::GetNextEmittedBlock() const {
148 for (int i = current_block_ + 1; i < graph()->blocks()->length(); ++i) {
149 if (!graph()->blocks()->at(i)->IsReachable()) continue;
150 if (!chunk_->GetLabel(i)->HasReplacement()) return i;
156 static void AddWeakObjectToCodeDependency(Isolate* isolate,
157 Handle<Object> object,
159 Heap* heap = isolate->heap();
160 heap->EnsureWeakObjectToCodeTable();
161 Handle<DependentCode> dep(heap->LookupWeakObjectToCodeDependency(object));
162 dep = DependentCode::Insert(dep, DependentCode::kWeakCodeGroup, code);
163 heap->AddWeakObjectToCodeDependency(object, dep);
167 void LCodeGenBase::RegisterWeakObjectsInOptimizedCode(Handle<Code> code) {
168 ASSERT(code->is_optimized_code());
169 ZoneList<Handle<Map> > maps(1, zone());
170 ZoneList<Handle<JSObject> > objects(1, zone());
171 ZoneList<Handle<Cell> > cells(1, zone());
172 int mode_mask = RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT) |
173 RelocInfo::ModeMask(RelocInfo::CELL);
174 for (RelocIterator it(*code, mode_mask); !it.done(); it.next()) {
175 RelocInfo::Mode mode = it.rinfo()->rmode();
176 if (mode == RelocInfo::CELL &&
177 code->IsWeakObjectInOptimizedCode(it.rinfo()->target_cell())) {
178 Handle<Cell> cell(it.rinfo()->target_cell());
179 cells.Add(cell, zone());
180 } else if (mode == RelocInfo::EMBEDDED_OBJECT &&
181 code->IsWeakObjectInOptimizedCode(it.rinfo()->target_object())) {
182 if (it.rinfo()->target_object()->IsMap()) {
183 Handle<Map> map(Map::cast(it.rinfo()->target_object()));
184 maps.Add(map, zone());
185 } else if (it.rinfo()->target_object()->IsJSObject()) {
186 Handle<JSObject> object(JSObject::cast(it.rinfo()->target_object()));
187 objects.Add(object, zone());
188 } else if (it.rinfo()->target_object()->IsCell()) {
189 Handle<Cell> cell(Cell::cast(it.rinfo()->target_object()));
190 cells.Add(cell, zone());
194 if (FLAG_enable_ool_constant_pool) {
195 code->constant_pool()->set_weak_object_state(
196 ConstantPoolArray::WEAK_OBJECTS_IN_OPTIMIZED_CODE);
199 // This disables verification of weak embedded objects after full GC.
200 // AddDependentCode can cause a GC, which would observe the state where
201 // this code is not yet in the depended code lists of the embedded maps.
202 NoWeakObjectVerificationScope disable_verification_of_embedded_objects;
204 for (int i = 0; i < maps.length(); i++) {
205 Map::AddDependentCode(maps.at(i), DependentCode::kWeakCodeGroup, code);
207 for (int i = 0; i < objects.length(); i++) {
208 AddWeakObjectToCodeDependency(isolate(), objects.at(i), code);
210 for (int i = 0; i < cells.length(); i++) {
211 AddWeakObjectToCodeDependency(isolate(), cells.at(i), code);
216 void LCodeGenBase::Abort(BailoutReason reason) {
217 info()->set_bailout_reason(reason);
222 void LCodeGenBase::AddDeprecationDependency(Handle<Map> map) {
223 if (map->is_deprecated()) return Abort(kMapBecameDeprecated);
224 chunk_->AddDeprecationDependency(map);
228 void LCodeGenBase::AddStabilityDependency(Handle<Map> map) {
229 if (!map->is_stable()) return Abort(kMapBecameUnstable);
230 chunk_->AddStabilityDependency(map);
233 } } // namespace v8::internal