OLD | NEW |
(Empty) | |
| 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/compiler/code-generator.h" |
| 6 |
| 7 #include "src/compiler/code-generator-impl.h" |
| 8 #include "src/compiler/linkage.h" |
| 9 |
| 10 namespace v8 { |
| 11 namespace internal { |
| 12 namespace compiler { |
| 13 |
| 14 CodeGenerator::CodeGenerator(InstructionSequence* code) |
| 15 : code_(code), |
| 16 current_block_(NULL), |
| 17 current_source_position_(SourcePosition::Invalid()), |
| 18 masm_(code->zone()->isolate(), NULL, 0), |
| 19 resolver_(this), |
| 20 safepoints_(code->zone()), |
| 21 lazy_deoptimization_entries_( |
| 22 LazyDeoptimizationEntries::allocator_type(code->zone())), |
| 23 deoptimization_states_( |
| 24 DeoptimizationStates::allocator_type(code->zone())), |
| 25 deoptimization_literals_(Literals::allocator_type(code->zone())), |
| 26 translations_(code->zone()) { |
| 27 deoptimization_states_.resize(code->GetDeoptimizationEntryCount(), NULL); |
| 28 } |
| 29 |
| 30 |
| 31 Handle<Code> CodeGenerator::GenerateCode() { |
| 32 CompilationInfo* info = linkage()->info(); |
| 33 |
| 34 // Emit a code line info recording start event. |
| 35 PositionsRecorder* recorder = masm()->positions_recorder(); |
| 36 LOG_CODE_EVENT(isolate(), CodeStartLinePosInfoRecordEvent(recorder)); |
| 37 |
| 38 // Place function entry hook if requested to do so. |
| 39 if (linkage()->GetIncomingDescriptor()->IsJSFunctionCall()) { |
| 40 ProfileEntryHookStub::MaybeCallEntryHook(masm()); |
| 41 } |
| 42 |
| 43 // Architecture-specific, linkage-specific prologue. |
| 44 info->set_prologue_offset(masm()->pc_offset()); |
| 45 AssemblePrologue(); |
| 46 |
| 47 // Assemble all instructions. |
| 48 for (InstructionSequence::const_iterator i = code()->begin(); |
| 49 i != code()->end(); ++i) { |
| 50 AssembleInstruction(*i); |
| 51 } |
| 52 |
| 53 FinishCode(masm()); |
| 54 |
| 55 safepoints()->Emit(masm(), frame()->GetSpillSlotCount()); |
| 56 |
| 57 // TODO(titzer): what are the right code flags here? |
| 58 Code::Kind kind = Code::STUB; |
| 59 if (linkage()->GetIncomingDescriptor()->IsJSFunctionCall()) { |
| 60 kind = Code::OPTIMIZED_FUNCTION; |
| 61 } |
| 62 Handle<Code> result = v8::internal::CodeGenerator::MakeCodeEpilogue( |
| 63 masm(), Code::ComputeFlags(kind), info); |
| 64 result->set_is_turbofanned(true); |
| 65 result->set_stack_slots(frame()->GetSpillSlotCount()); |
| 66 result->set_safepoint_table_offset(safepoints()->GetCodeOffset()); |
| 67 |
| 68 PopulateDeoptimizationData(result); |
| 69 |
| 70 // Emit a code line info recording stop event. |
| 71 void* line_info = recorder->DetachJITHandlerData(); |
| 72 LOG_CODE_EVENT(isolate(), CodeEndLinePosInfoRecordEvent(*result, line_info)); |
| 73 |
| 74 return result; |
| 75 } |
| 76 |
| 77 |
| 78 void CodeGenerator::RecordSafepoint(PointerMap* pointers, Safepoint::Kind kind, |
| 79 int arguments, |
| 80 Safepoint::DeoptMode deopt_mode) { |
| 81 const ZoneList<InstructionOperand*>* operands = |
| 82 pointers->GetNormalizedOperands(); |
| 83 Safepoint safepoint = |
| 84 safepoints()->DefineSafepoint(masm(), kind, arguments, deopt_mode); |
| 85 for (int i = 0; i < operands->length(); i++) { |
| 86 InstructionOperand* pointer = operands->at(i); |
| 87 if (pointer->IsStackSlot()) { |
| 88 safepoint.DefinePointerSlot(pointer->index(), zone()); |
| 89 } else if (pointer->IsRegister() && (kind & Safepoint::kWithRegisters)) { |
| 90 Register reg = Register::FromAllocationIndex(pointer->index()); |
| 91 safepoint.DefinePointerRegister(reg, zone()); |
| 92 } |
| 93 } |
| 94 } |
| 95 |
| 96 |
| 97 void CodeGenerator::AssembleInstruction(Instruction* instr) { |
| 98 if (instr->IsBlockStart()) { |
| 99 // Bind a label for a block start and handle parallel moves. |
| 100 BlockStartInstruction* block_start = BlockStartInstruction::cast(instr); |
| 101 current_block_ = block_start->block(); |
| 102 if (FLAG_code_comments) { |
| 103 // TODO(titzer): these code comments are a giant memory leak. |
| 104 Vector<char> buffer = Vector<char>::New(32); |
| 105 SNPrintF(buffer, "-- B%d start --", block_start->block()->id()); |
| 106 masm()->RecordComment(buffer.start()); |
| 107 } |
| 108 masm()->bind(block_start->label()); |
| 109 } |
| 110 if (instr->IsGapMoves()) { |
| 111 // Handle parallel moves associated with the gap instruction. |
| 112 AssembleGap(GapInstruction::cast(instr)); |
| 113 } else if (instr->IsSourcePosition()) { |
| 114 AssembleSourcePosition(SourcePositionInstruction::cast(instr)); |
| 115 } else { |
| 116 // Assemble architecture-specific code for the instruction. |
| 117 AssembleArchInstruction(instr); |
| 118 |
| 119 // Assemble branches or boolean materializations after this instruction. |
| 120 FlagsMode mode = FlagsModeField::decode(instr->opcode()); |
| 121 FlagsCondition condition = FlagsConditionField::decode(instr->opcode()); |
| 122 switch (mode) { |
| 123 case kFlags_none: |
| 124 return; |
| 125 case kFlags_set: |
| 126 return AssembleArchBoolean(instr, condition); |
| 127 case kFlags_branch: |
| 128 return AssembleArchBranch(instr, condition); |
| 129 } |
| 130 UNREACHABLE(); |
| 131 } |
| 132 } |
| 133 |
| 134 |
| 135 void CodeGenerator::AssembleSourcePosition(SourcePositionInstruction* instr) { |
| 136 SourcePosition source_position = instr->source_position(); |
| 137 if (source_position == current_source_position_) return; |
| 138 ASSERT(!source_position.IsInvalid()); |
| 139 if (!source_position.IsUnknown()) { |
| 140 int code_pos = source_position.raw(); |
| 141 masm()->positions_recorder()->RecordPosition(source_position.raw()); |
| 142 masm()->positions_recorder()->WriteRecordedPositions(); |
| 143 if (FLAG_code_comments) { |
| 144 Vector<char> buffer = Vector<char>::New(256); |
| 145 CompilationInfo* info = linkage()->info(); |
| 146 int ln = Script::GetLineNumber(info->script(), code_pos); |
| 147 int cn = Script::GetColumnNumber(info->script(), code_pos); |
| 148 if (info->script()->name()->IsString()) { |
| 149 Handle<String> file(String::cast(info->script()->name())); |
| 150 base::OS::SNPrintF(buffer.start(), buffer.length(), "-- %s:%d:%d --", |
| 151 file->ToCString().get(), ln, cn); |
| 152 } else { |
| 153 base::OS::SNPrintF(buffer.start(), buffer.length(), |
| 154 "-- <unknown>:%d:%d --", ln, cn); |
| 155 } |
| 156 masm()->RecordComment(buffer.start()); |
| 157 } |
| 158 } |
| 159 current_source_position_ = source_position; |
| 160 } |
| 161 |
| 162 |
| 163 void CodeGenerator::AssembleGap(GapInstruction* instr) { |
| 164 for (int i = GapInstruction::FIRST_INNER_POSITION; |
| 165 i <= GapInstruction::LAST_INNER_POSITION; i++) { |
| 166 GapInstruction::InnerPosition inner_pos = |
| 167 static_cast<GapInstruction::InnerPosition>(i); |
| 168 ParallelMove* move = instr->GetParallelMove(inner_pos); |
| 169 if (move != NULL) resolver()->Resolve(move); |
| 170 } |
| 171 } |
| 172 |
| 173 |
| 174 void CodeGenerator::PopulateDeoptimizationData(Handle<Code> code_object) { |
| 175 CompilationInfo* info = linkage()->info(); |
| 176 int deopt_count = code()->GetDeoptimizationEntryCount(); |
| 177 int patch_count = lazy_deoptimization_entries_.size(); |
| 178 if (patch_count == 0 && deopt_count == 0) return; |
| 179 Handle<DeoptimizationInputData> data = DeoptimizationInputData::New( |
| 180 isolate(), deopt_count, patch_count, TENURED); |
| 181 |
| 182 Handle<ByteArray> translation_array = |
| 183 translations_.CreateByteArray(isolate()->factory()); |
| 184 |
| 185 data->SetTranslationByteArray(*translation_array); |
| 186 data->SetInlinedFunctionCount(Smi::FromInt(0)); |
| 187 data->SetOptimizationId(Smi::FromInt(info->optimization_id())); |
| 188 // TODO(jarin) The following code was copied over from Lithium, not sure |
| 189 // whether the scope or the IsOptimizing condition are really needed. |
| 190 if (info->IsOptimizing()) { |
| 191 // Reference to shared function info does not change between phases. |
| 192 AllowDeferredHandleDereference allow_handle_dereference; |
| 193 data->SetSharedFunctionInfo(*info->shared_info()); |
| 194 } else { |
| 195 data->SetSharedFunctionInfo(Smi::FromInt(0)); |
| 196 } |
| 197 |
| 198 Handle<FixedArray> literals = isolate()->factory()->NewFixedArray( |
| 199 deoptimization_literals_.size(), TENURED); |
| 200 { |
| 201 AllowDeferredHandleDereference copy_handles; |
| 202 for (unsigned i = 0; i < deoptimization_literals_.size(); i++) { |
| 203 literals->set(i, *deoptimization_literals_[i]); |
| 204 } |
| 205 data->SetLiteralArray(*literals); |
| 206 } |
| 207 |
| 208 // No OSR in Turbofan yet... |
| 209 BailoutId osr_ast_id = BailoutId::None(); |
| 210 data->SetOsrAstId(Smi::FromInt(osr_ast_id.ToInt())); |
| 211 data->SetOsrPcOffset(Smi::FromInt(-1)); |
| 212 |
| 213 // Populate deoptimization entries. |
| 214 for (int i = 0; i < deopt_count; i++) { |
| 215 FrameStateDescriptor descriptor = code()->GetDeoptimizationEntry(i); |
| 216 data->SetAstId(i, descriptor.bailout_id()); |
| 217 data->SetTranslationIndex(i, Smi::FromInt(0)); |
| 218 data->SetArgumentsStackHeight(i, Smi::FromInt(0)); |
| 219 data->SetPc(i, Smi::FromInt(-1)); |
| 220 } |
| 221 |
| 222 // Populate the return address patcher entries. |
| 223 for (int i = 0; i < patch_count; ++i) { |
| 224 LazyDeoptimizationEntry entry = lazy_deoptimization_entries_[i]; |
| 225 ASSERT(entry.position_after_call() == entry.continuation()->pos() || |
| 226 IsNopForSmiCodeInlining(code_object, entry.position_after_call(), |
| 227 entry.continuation()->pos())); |
| 228 data->SetReturnAddressPc(i, Smi::FromInt(entry.position_after_call())); |
| 229 data->SetPatchedAddressPc(i, Smi::FromInt(entry.deoptimization()->pos())); |
| 230 } |
| 231 |
| 232 code_object->set_deoptimization_data(*data); |
| 233 } |
| 234 |
| 235 |
| 236 void CodeGenerator::RecordLazyDeoptimizationEntry(Instruction* instr) { |
| 237 InstructionOperandConverter i(this, instr); |
| 238 |
| 239 Label after_call; |
| 240 masm()->bind(&after_call); |
| 241 |
| 242 // The continuation and deoptimization are the last two inputs: |
| 243 BasicBlock* cont_block = i.InputBlock(instr->InputCount() - 2); |
| 244 BasicBlock* deopt_block = i.InputBlock(instr->InputCount() - 1); |
| 245 |
| 246 Label* cont_label = code_->GetLabel(cont_block); |
| 247 Label* deopt_label = code_->GetLabel(deopt_block); |
| 248 |
| 249 lazy_deoptimization_entries_.push_back( |
| 250 LazyDeoptimizationEntry(after_call.pos(), cont_label, deopt_label)); |
| 251 } |
| 252 |
| 253 |
| 254 int CodeGenerator::DefineDeoptimizationLiteral(Handle<Object> literal) { |
| 255 int result = deoptimization_literals_.size(); |
| 256 for (unsigned i = 0; i < deoptimization_literals_.size(); ++i) { |
| 257 if (deoptimization_literals_[i].is_identical_to(literal)) return i; |
| 258 } |
| 259 deoptimization_literals_.push_back(literal); |
| 260 return result; |
| 261 } |
| 262 |
| 263 |
| 264 void CodeGenerator::BuildTranslation(Instruction* instr, |
| 265 int deoptimization_id) { |
| 266 // We should build translation only once. |
| 267 ASSERT_EQ(NULL, deoptimization_states_[deoptimization_id]); |
| 268 |
| 269 // TODO(jarin) This should build translation codes from the instruction inputs |
| 270 // and from the framestate descriptor. At the moment, we only create a dummy |
| 271 // translation. |
| 272 |
| 273 FrameStateDescriptor descriptor = |
| 274 code()->GetDeoptimizationEntry(deoptimization_id); |
| 275 Translation translation(&translations_, 1, 1, zone()); |
| 276 translation.BeginJSFrame(descriptor.bailout_id(), Translation::kSelfLiteralId, |
| 277 0); |
| 278 int undefined_literal_id = |
| 279 DefineDeoptimizationLiteral(isolate()->factory()->undefined_value()); |
| 280 translation.StoreLiteral(undefined_literal_id); |
| 281 |
| 282 deoptimization_states_[deoptimization_id] = |
| 283 new (zone()) DeoptimizationState(translation.index()); |
| 284 } |
| 285 |
| 286 } // namespace compiler |
| 287 } // namespace internal |
| 288 } // namespace v8 |
OLD | NEW |