blob: 086da560e4fcd80ecf3a322f987c85ecc82df132 [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
5#include "src/compiler/code-generator.h"
6
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007#include "src/address-map.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008#include "src/compiler/code-generator-impl.h"
9#include "src/compiler/linkage.h"
10#include "src/compiler/pipeline.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000011#include "src/frames-inl.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012
13namespace v8 {
14namespace internal {
15namespace compiler {
16
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000017class CodeGenerator::JumpTable final : public ZoneObject {
18 public:
19 JumpTable(JumpTable* next, Label** targets, size_t target_count)
20 : next_(next), targets_(targets), target_count_(target_count) {}
21
22 Label* label() { return &label_; }
23 JumpTable* next() const { return next_; }
24 Label** targets() const { return targets_; }
25 size_t target_count() const { return target_count_; }
26
27 private:
28 Label label_;
29 JumpTable* const next_;
30 Label** const targets_;
31 size_t const target_count_;
32};
33
Emily Bernierd0a1eb72015-03-24 16:35:39 -040034CodeGenerator::CodeGenerator(Frame* frame, Linkage* linkage,
35 InstructionSequence* code, CompilationInfo* info)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000036 : frame_access_state_(new (code->zone()) FrameAccessState(frame)),
Emily Bernierd0a1eb72015-03-24 16:35:39 -040037 linkage_(linkage),
38 code_(code),
39 info_(info),
40 labels_(zone()->NewArray<Label>(code->InstructionBlockCount())),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000041 current_block_(RpoNumber::Invalid()),
42 current_source_position_(SourcePosition::Unknown()),
43 masm_(info->isolate(), nullptr, 0, CodeObjectRequired::kYes),
Ben Murdochb8a8cc12014-11-26 15:28:44 +000044 resolver_(this),
45 safepoints_(code->zone()),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000046 handlers_(code->zone()),
Ben Murdochda12d292016-06-02 14:46:10 +010047 deoptimization_exits_(code->zone()),
Ben Murdochb8a8cc12014-11-26 15:28:44 +000048 deoptimization_states_(code->zone()),
49 deoptimization_literals_(code->zone()),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000050 inlined_function_count_(0),
Ben Murdochb8a8cc12014-11-26 15:28:44 +000051 translations_(code->zone()),
Emily Bernierd0a1eb72015-03-24 16:35:39 -040052 last_lazy_deopt_pc_(0),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000053 jump_tables_(nullptr),
54 ools_(nullptr),
55 osr_pc_offset_(-1) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -040056 for (int i = 0; i < code->InstructionBlockCount(); ++i) {
57 new (&labels_[i]) Label;
58 }
59}
Ben Murdochb8a8cc12014-11-26 15:28:44 +000060
Ben Murdochb8a8cc12014-11-26 15:28:44 +000061Handle<Code> CodeGenerator::GenerateCode() {
Emily Bernierd0a1eb72015-03-24 16:35:39 -040062 CompilationInfo* info = this->info();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000063
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000064 // Open a frame scope to indicate that there is a frame on the stack. The
65 // MANUAL indicates that the scope shouldn't actually generate code to set up
66 // the frame (that is done in AssemblePrologue).
67 FrameScope frame_scope(masm(), StackFrame::MANUAL);
68
Ben Murdochb8a8cc12014-11-26 15:28:44 +000069 // Emit a code line info recording start event.
70 PositionsRecorder* recorder = masm()->positions_recorder();
71 LOG_CODE_EVENT(isolate(), CodeStartLinePosInfoRecordEvent(recorder));
72
73 // Place function entry hook if requested to do so.
74 if (linkage()->GetIncomingDescriptor()->IsJSFunctionCall()) {
75 ProfileEntryHookStub::MaybeCallEntryHook(masm());
76 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000077 // Architecture-specific, linkage-specific prologue.
78 info->set_prologue_offset(masm()->pc_offset());
Ben Murdochb8a8cc12014-11-26 15:28:44 +000079
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000080 // Define deoptimization literals for all inlined functions.
81 DCHECK_EQ(0u, deoptimization_literals_.size());
Ben Murdochda12d292016-06-02 14:46:10 +010082 for (const CompilationInfo::InlinedFunctionHolder& inlined :
83 info->inlined_functions()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000084 if (!inlined.shared_info.is_identical_to(info->shared_info())) {
85 DefineDeoptimizationLiteral(inlined.shared_info);
86 }
87 }
88 inlined_function_count_ = deoptimization_literals_.size();
89
90 // Define deoptimization literals for all unoptimized code objects of inlined
91 // functions. This ensures unoptimized code is kept alive by optimized code.
Ben Murdochda12d292016-06-02 14:46:10 +010092 for (const CompilationInfo::InlinedFunctionHolder& inlined :
93 info->inlined_functions()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000094 if (!inlined.shared_info.is_identical_to(info->shared_info())) {
95 DefineDeoptimizationLiteral(inlined.inlined_code_object_root);
96 }
97 }
98
Ben Murdochda12d292016-06-02 14:46:10 +010099 // Finish the Frame
100 frame()->AlignFrame(kFrameAlignmentInBytes);
101 AssembleSetupStackPointer();
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400102 // Assemble all non-deferred blocks, followed by deferred ones.
103 for (int deferred = 0; deferred < 2; ++deferred) {
Ben Murdochda12d292016-06-02 14:46:10 +0100104 for (const InstructionBlock* block : code()->instruction_blocks()) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400105 if (block->IsDeferred() == (deferred == 0)) {
106 continue;
107 }
108 // Align loop headers on 16-byte boundaries.
109 if (block->IsLoopHeader()) masm()->Align(16);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000110 // Ensure lazy deopt doesn't patch handler entry points.
111 if (block->IsHandler()) EnsureSpaceForLazyDeopt();
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400112 // Bind a label for a block.
113 current_block_ = block->rpo_number();
114 if (FLAG_code_comments) {
115 // TODO(titzer): these code comments are a giant memory leak.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000116 Vector<char> buffer = Vector<char>::New(200);
117 char* buffer_start = buffer.start();
118
119 int next = SNPrintF(
120 buffer, "-- B%d start%s%s%s%s", block->rpo_number().ToInt(),
121 block->IsDeferred() ? " (deferred)" : "",
122 block->needs_frame() ? "" : " (no frame)",
123 block->must_construct_frame() ? " (construct frame)" : "",
124 block->must_deconstruct_frame() ? " (deconstruct frame)" : "");
125
126 buffer = buffer.SubVector(next, buffer.length());
127
128 if (block->IsLoopHeader()) {
129 next =
130 SNPrintF(buffer, " (loop up to %d)", block->loop_end().ToInt());
131 buffer = buffer.SubVector(next, buffer.length());
132 }
133 if (block->loop_header().IsValid()) {
134 next =
135 SNPrintF(buffer, " (in loop %d)", block->loop_header().ToInt());
136 buffer = buffer.SubVector(next, buffer.length());
137 }
138 SNPrintF(buffer, " --");
139 masm()->RecordComment(buffer_start);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400140 }
Ben Murdochda12d292016-06-02 14:46:10 +0100141
142 frame_access_state()->MarkHasFrame(block->needs_frame());
143
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400144 masm()->bind(GetLabel(current_block_));
Ben Murdochda12d292016-06-02 14:46:10 +0100145 if (block->must_construct_frame()) {
146 AssemblePrologue();
147 // We need to setup the root register after we assemble the prologue, to
148 // avoid clobbering callee saved registers in case of C linkage and
149 // using the roots.
150 // TODO(mtrofin): investigate how we can avoid doing this repeatedly.
151 if (linkage()->GetIncomingDescriptor()->InitializeRootRegister()) {
152 masm()->InitializeRootRegister();
153 }
154 }
155
156 if (FLAG_enable_embedded_constant_pool && !block->needs_frame()) {
157 ConstantPoolUnavailableScope constant_pool_unavailable(masm());
158 AssembleBlock(block);
159 } else {
160 AssembleBlock(block);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400161 }
162 }
163 }
164
165 // Assemble all out-of-line code.
166 if (ools_) {
167 masm()->RecordComment("-- Out of line code --");
168 for (OutOfLineCode* ool = ools_; ool; ool = ool->next()) {
169 masm()->bind(ool->entry());
170 ool->Generate();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000171 if (ool->exit()->is_bound()) masm()->jmp(ool->exit());
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400172 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000173 }
174
Ben Murdochda12d292016-06-02 14:46:10 +0100175 // Assemble all eager deoptimization exits.
176 for (DeoptimizationExit* exit : deoptimization_exits_) {
177 masm()->bind(exit->label());
178 AssembleDeoptimizerCall(exit->deoptimization_id(), Deoptimizer::EAGER);
179 }
180
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400181 // Ensure there is space for lazy deoptimization in the code.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000182 if (info->ShouldEnsureSpaceForLazyDeopt()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000183 int target_offset = masm()->pc_offset() + Deoptimizer::patch_size();
184 while (masm()->pc_offset() < target_offset) {
185 masm()->nop();
186 }
187 }
188
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000189 FinishCode(masm());
190
191 // Emit the jump tables.
192 if (jump_tables_) {
193 masm()->Align(kPointerSize);
194 for (JumpTable* table = jump_tables_; table; table = table->next()) {
195 masm()->bind(table->label());
196 AssembleJumpTable(table->targets(), table->target_count());
197 }
198 }
199
Ben Murdoch097c5b22016-05-18 11:27:45 +0100200 safepoints()->Emit(masm(), frame()->GetTotalFrameSlotCount());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000201
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000202 Handle<Code> result =
203 v8::internal::CodeGenerator::MakeCodeEpilogue(masm(), info);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000204 result->set_is_turbofanned(true);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100205 result->set_stack_slots(frame()->GetTotalFrameSlotCount());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000206 result->set_safepoint_table_offset(safepoints()->GetCodeOffset());
207
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000208 // Emit exception handler table.
209 if (!handlers_.empty()) {
210 Handle<HandlerTable> table =
211 Handle<HandlerTable>::cast(isolate()->factory()->NewFixedArray(
212 HandlerTable::LengthForReturn(static_cast<int>(handlers_.size())),
213 TENURED));
214 for (size_t i = 0; i < handlers_.size(); ++i) {
215 int position = handlers_[i].handler->pos();
216 HandlerTable::CatchPrediction prediction = handlers_[i].caught_locally
217 ? HandlerTable::CAUGHT
218 : HandlerTable::UNCAUGHT;
219 table->SetReturnOffset(static_cast<int>(i), handlers_[i].pc_offset);
220 table->SetReturnHandler(static_cast<int>(i), position, prediction);
221 }
222 result->set_handler_table(*table);
223 }
224
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000225 PopulateDeoptimizationData(result);
226
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400227 // Ensure there is space for lazy deoptimization in the relocation info.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000228 if (info->ShouldEnsureSpaceForLazyDeopt()) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400229 Deoptimizer::EnsureRelocSpaceForLazyDeoptimization(result);
230 }
231
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000232 // Emit a code line info recording stop event.
233 void* line_info = recorder->DetachJITHandlerData();
Ben Murdochda12d292016-06-02 14:46:10 +0100234 LOG_CODE_EVENT(isolate(), CodeEndLinePosInfoRecordEvent(
235 AbstractCode::cast(*result), line_info));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000236
237 return result;
238}
239
240
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000241bool CodeGenerator::IsNextInAssemblyOrder(RpoNumber block) const {
242 return code()
243 ->InstructionBlockAt(current_block_)
244 ->ao_number()
245 .IsNext(code()->InstructionBlockAt(block)->ao_number());
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400246}
247
248
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000249void CodeGenerator::RecordSafepoint(ReferenceMap* references,
250 Safepoint::Kind kind, int arguments,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000251 Safepoint::DeoptMode deopt_mode) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000252 Safepoint safepoint =
253 safepoints()->DefineSafepoint(masm(), kind, arguments, deopt_mode);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000254 int stackSlotToSpillSlotDelta =
255 frame()->GetTotalFrameSlotCount() - frame()->GetSpillSlotCount();
Ben Murdochda12d292016-06-02 14:46:10 +0100256 for (const InstructionOperand& operand : references->reference_operands()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000257 if (operand.IsStackSlot()) {
258 int index = LocationOperand::cast(operand).index();
259 DCHECK(index >= 0);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100260 // We might index values in the fixed part of the frame (i.e. the
261 // closure pointer or the context pointer); these are not spill slots
262 // and therefore don't work with the SafepointTable currently, but
263 // we also don't need to worry about them, since the GC has special
264 // knowledge about those fields anyway.
265 if (index < stackSlotToSpillSlotDelta) continue;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000266 safepoint.DefinePointerSlot(index, zone());
267 } else if (operand.IsRegister() && (kind & Safepoint::kWithRegisters)) {
268 Register reg = LocationOperand::cast(operand).GetRegister();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000269 safepoint.DefinePointerRegister(reg, zone());
270 }
271 }
272}
273
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000274bool CodeGenerator::IsMaterializableFromFrame(Handle<HeapObject> object,
Ben Murdochda12d292016-06-02 14:46:10 +0100275 int* slot_return) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000276 if (linkage()->GetIncomingDescriptor()->IsJSFunctionCall()) {
277 if (info()->has_context() && object.is_identical_to(info()->context()) &&
278 !info()->is_osr()) {
Ben Murdochda12d292016-06-02 14:46:10 +0100279 *slot_return = Frame::kContextSlot;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000280 return true;
281 } else if (object.is_identical_to(info()->closure())) {
Ben Murdochda12d292016-06-02 14:46:10 +0100282 *slot_return = Frame::kJSFunctionSlot;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000283 return true;
284 }
285 }
286 return false;
287}
288
289
290bool CodeGenerator::IsMaterializableFromRoot(
291 Handle<HeapObject> object, Heap::RootListIndex* index_return) {
292 const CallDescriptor* incoming_descriptor =
293 linkage()->GetIncomingDescriptor();
294 if (incoming_descriptor->flags() & CallDescriptor::kCanUseRoots) {
295 RootIndexMap map(isolate());
296 int root_index = map.Lookup(*object);
297 if (root_index != RootIndexMap::kInvalidRootIndex) {
298 *index_return = static_cast<Heap::RootListIndex>(root_index);
299 return true;
300 }
301 }
302 return false;
303}
304
Ben Murdochda12d292016-06-02 14:46:10 +0100305void CodeGenerator::AssembleBlock(const InstructionBlock* block) {
306 for (int i = block->code_start(); i < block->code_end(); ++i) {
307 Instruction* instr = code()->InstructionAt(i);
308 AssembleInstruction(instr, block);
309 }
310}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000311
Ben Murdochda12d292016-06-02 14:46:10 +0100312void CodeGenerator::AssembleInstruction(Instruction* instr,
313 const InstructionBlock* block) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000314 AssembleGaps(instr);
Ben Murdochda12d292016-06-02 14:46:10 +0100315 DCHECK_IMPLIES(
316 block->must_deconstruct_frame(),
317 instr != code()->InstructionAt(block->last_instruction_index()) ||
318 instr->IsRet() || instr->IsJump());
319 if (instr->IsJump() && block->must_deconstruct_frame()) {
320 AssembleDeconstructFrame();
321 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000322 AssembleSourcePosition(instr);
323 // Assemble architecture-specific code for the instruction.
324 AssembleArchInstruction(instr);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000325
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000326 FlagsMode mode = FlagsModeField::decode(instr->opcode());
327 FlagsCondition condition = FlagsConditionField::decode(instr->opcode());
Ben Murdochda12d292016-06-02 14:46:10 +0100328 switch (mode) {
329 case kFlags_branch: {
330 // Assemble a branch after this instruction.
331 InstructionOperandConverter i(this, instr);
332 RpoNumber true_rpo = i.InputRpo(instr->InputCount() - 2);
333 RpoNumber false_rpo = i.InputRpo(instr->InputCount() - 1);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400334
Ben Murdochda12d292016-06-02 14:46:10 +0100335 if (true_rpo == false_rpo) {
336 // redundant branch.
337 if (!IsNextInAssemblyOrder(true_rpo)) {
338 AssembleArchJump(true_rpo);
339 }
340 return;
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400341 }
Ben Murdochda12d292016-06-02 14:46:10 +0100342 if (IsNextInAssemblyOrder(true_rpo)) {
343 // true block is next, can fall through if condition negated.
344 std::swap(true_rpo, false_rpo);
345 condition = NegateFlagsCondition(condition);
346 }
347 BranchInfo branch;
348 branch.condition = condition;
349 branch.true_label = GetLabel(true_rpo);
350 branch.false_label = GetLabel(false_rpo);
351 branch.fallthru = IsNextInAssemblyOrder(false_rpo);
352 // Assemble architecture-specific branch.
353 AssembleArchBranch(instr, &branch);
354 break;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000355 }
Ben Murdochda12d292016-06-02 14:46:10 +0100356 case kFlags_deoptimize: {
357 // Assemble a conditional eager deoptimization after this instruction.
358 InstructionOperandConverter i(this, instr);
359 size_t frame_state_offset = MiscField::decode(instr->opcode());
360 DeoptimizationExit* const exit =
361 AddDeoptimizationExit(instr, frame_state_offset);
362 Label continue_label;
363 BranchInfo branch;
364 branch.condition = condition;
365 branch.true_label = exit->label();
366 branch.false_label = &continue_label;
367 branch.fallthru = true;
368 // Assemble architecture-specific branch.
369 AssembleArchBranch(instr, &branch);
370 masm()->bind(&continue_label);
371 break;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000372 }
Ben Murdochda12d292016-06-02 14:46:10 +0100373 case kFlags_set: {
374 // Assemble a boolean materialization after this instruction.
375 AssembleArchBoolean(instr, condition);
376 break;
377 }
378 case kFlags_none: {
379 break;
380 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000381 }
382}
383
384
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000385void CodeGenerator::AssembleSourcePosition(Instruction* instr) {
386 SourcePosition source_position;
387 if (!code()->GetSourcePosition(instr, &source_position)) return;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000388 if (source_position == current_source_position_) return;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000389 current_source_position_ = source_position;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000390 if (source_position.IsUnknown()) return;
391 int code_pos = source_position.raw();
392 masm()->positions_recorder()->RecordPosition(code_pos);
393 masm()->positions_recorder()->WriteRecordedPositions();
394 if (FLAG_code_comments) {
395 Vector<char> buffer = Vector<char>::New(256);
396 CompilationInfo* info = this->info();
397 int ln = Script::GetLineNumber(info->script(), code_pos);
398 int cn = Script::GetColumnNumber(info->script(), code_pos);
399 if (info->script()->name()->IsString()) {
400 Handle<String> file(String::cast(info->script()->name()));
401 base::OS::SNPrintF(buffer.start(), buffer.length(), "-- %s:%d:%d --",
402 file->ToCString().get(), ln, cn);
403 } else {
404 base::OS::SNPrintF(buffer.start(), buffer.length(),
405 "-- <unknown>:%d:%d --", ln, cn);
406 }
407 masm()->RecordComment(buffer.start());
408 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000409}
410
411
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000412void CodeGenerator::AssembleGaps(Instruction* instr) {
413 for (int i = Instruction::FIRST_GAP_POSITION;
414 i <= Instruction::LAST_GAP_POSITION; i++) {
415 Instruction::GapPosition inner_pos =
416 static_cast<Instruction::GapPosition>(i);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000417 ParallelMove* move = instr->GetParallelMove(inner_pos);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000418 if (move != nullptr) resolver()->Resolve(move);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000419 }
420}
421
422
423void CodeGenerator::PopulateDeoptimizationData(Handle<Code> code_object) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400424 CompilationInfo* info = this->info();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000425 int deopt_count = static_cast<int>(deoptimization_states_.size());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000426 if (deopt_count == 0 && !info->is_osr()) return;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000427 Handle<DeoptimizationInputData> data =
428 DeoptimizationInputData::New(isolate(), deopt_count, TENURED);
429
430 Handle<ByteArray> translation_array =
431 translations_.CreateByteArray(isolate()->factory());
432
433 data->SetTranslationByteArray(*translation_array);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000434 data->SetInlinedFunctionCount(
435 Smi::FromInt(static_cast<int>(inlined_function_count_)));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000436 data->SetOptimizationId(Smi::FromInt(info->optimization_id()));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000437
438 if (info->has_shared_info()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000439 data->SetSharedFunctionInfo(*info->shared_info());
440 } else {
441 data->SetSharedFunctionInfo(Smi::FromInt(0));
442 }
443
444 Handle<FixedArray> literals = isolate()->factory()->NewFixedArray(
445 static_cast<int>(deoptimization_literals_.size()), TENURED);
446 {
447 AllowDeferredHandleDereference copy_handles;
448 for (unsigned i = 0; i < deoptimization_literals_.size(); i++) {
449 literals->set(i, *deoptimization_literals_[i]);
450 }
451 data->SetLiteralArray(*literals);
452 }
453
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000454 if (info->is_osr()) {
455 DCHECK(osr_pc_offset_ >= 0);
456 data->SetOsrAstId(Smi::FromInt(info_->osr_ast_id().ToInt()));
457 data->SetOsrPcOffset(Smi::FromInt(osr_pc_offset_));
458 } else {
459 BailoutId osr_ast_id = BailoutId::None();
460 data->SetOsrAstId(Smi::FromInt(osr_ast_id.ToInt()));
461 data->SetOsrPcOffset(Smi::FromInt(-1));
462 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000463
464 // Populate deoptimization entries.
465 for (int i = 0; i < deopt_count; i++) {
466 DeoptimizationState* deoptimization_state = deoptimization_states_[i];
467 data->SetAstId(i, deoptimization_state->bailout_id());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000468 CHECK(deoptimization_states_[i]);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000469 data->SetTranslationIndex(
470 i, Smi::FromInt(deoptimization_states_[i]->translation_id()));
471 data->SetArgumentsStackHeight(i, Smi::FromInt(0));
472 data->SetPc(i, Smi::FromInt(deoptimization_state->pc_offset()));
473 }
474
475 code_object->set_deoptimization_data(*data);
476}
477
478
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000479Label* CodeGenerator::AddJumpTable(Label** targets, size_t target_count) {
480 jump_tables_ = new (zone()) JumpTable(jump_tables_, targets, target_count);
481 return jump_tables_->label();
482}
483
484
485void CodeGenerator::RecordCallPosition(Instruction* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000486 CallDescriptor::Flags flags(MiscField::decode(instr->opcode()));
487
488 bool needs_frame_state = (flags & CallDescriptor::kNeedsFrameState);
489
490 RecordSafepoint(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000491 instr->reference_map(), Safepoint::kSimple, 0,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000492 needs_frame_state ? Safepoint::kLazyDeopt : Safepoint::kNoLazyDeopt);
493
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000494 if (flags & CallDescriptor::kHasExceptionHandler) {
495 InstructionOperandConverter i(this, instr);
496 bool caught = flags & CallDescriptor::kHasLocalCatchHandler;
497 RpoNumber handler_rpo = i.InputRpo(instr->InputCount() - 1);
498 handlers_.push_back({caught, GetLabel(handler_rpo), masm()->pc_offset()});
499 }
500
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000501 if (flags & CallDescriptor::kNeedsNopAfterCall) {
502 AddNopForSmiCodeInlining();
503 }
504
505 if (needs_frame_state) {
506 MarkLazyDeoptSite();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000507 // If the frame state is present, it starts at argument 1 (just after the
508 // code address).
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000509 size_t frame_state_offset = 1;
510 FrameStateDescriptor* descriptor =
511 GetFrameStateDescriptor(instr, frame_state_offset);
512 int pc_offset = masm()->pc_offset();
513 int deopt_state_id = BuildTranslation(instr, pc_offset, frame_state_offset,
514 descriptor->state_combine());
515 // If the pre-call frame state differs from the post-call one, produce the
516 // pre-call frame state, too.
517 // TODO(jarin) We might want to avoid building the pre-call frame state
518 // because it is only used to get locals and arguments (by the debugger and
519 // f.arguments), and those are the same in the pre-call and post-call
520 // states.
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400521 if (!descriptor->state_combine().IsOutputIgnored()) {
522 deopt_state_id = BuildTranslation(instr, -1, frame_state_offset,
523 OutputFrameStateCombine::Ignore());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000524 }
525#if DEBUG
526 // Make sure all the values live in stack slots or they are immediates.
527 // (The values should not live in register because registers are clobbered
528 // by calls.)
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400529 for (size_t i = 0; i < descriptor->GetSize(); i++) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000530 InstructionOperand* op = instr->InputAt(frame_state_offset + 1 + i);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400531 CHECK(op->IsStackSlot() || op->IsDoubleStackSlot() || op->IsImmediate());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000532 }
533#endif
534 safepoints()->RecordLazyDeoptimizationIndex(deopt_state_id);
535 }
536}
537
538
539int CodeGenerator::DefineDeoptimizationLiteral(Handle<Object> literal) {
540 int result = static_cast<int>(deoptimization_literals_.size());
541 for (unsigned i = 0; i < deoptimization_literals_.size(); ++i) {
542 if (deoptimization_literals_[i].is_identical_to(literal)) return i;
543 }
544 deoptimization_literals_.push_back(literal);
545 return result;
546}
547
548
549FrameStateDescriptor* CodeGenerator::GetFrameStateDescriptor(
550 Instruction* instr, size_t frame_state_offset) {
551 InstructionOperandConverter i(this, instr);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000552 InstructionSequence::StateId state_id =
553 InstructionSequence::StateId::FromInt(i.InputInt32(frame_state_offset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000554 return code()->GetFrameStateDescriptor(state_id);
555}
556
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400557
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000558void CodeGenerator::TranslateStateValueDescriptor(
559 StateValueDescriptor* desc, Translation* translation,
560 InstructionOperandIterator* iter) {
561 if (desc->IsNested()) {
562 translation->BeginCapturedObject(static_cast<int>(desc->size()));
563 for (size_t index = 0; index < desc->fields().size(); index++) {
564 TranslateStateValueDescriptor(&desc->fields()[index], translation, iter);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400565 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000566 } else if (desc->IsDuplicate()) {
567 translation->DuplicateObject(static_cast<int>(desc->id()));
568 } else {
569 DCHECK(desc->IsPlain());
570 AddTranslationForOperand(translation, iter->instruction(), iter->Advance(),
571 desc->type());
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400572 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000573}
574
575
576void CodeGenerator::TranslateFrameStateDescriptorOperands(
577 FrameStateDescriptor* desc, InstructionOperandIterator* iter,
578 OutputFrameStateCombine combine, Translation* translation) {
579 for (size_t index = 0; index < desc->GetSize(combine); index++) {
580 switch (combine.kind()) {
581 case OutputFrameStateCombine::kPushOutput: {
582 DCHECK(combine.GetPushCount() <= iter->instruction()->OutputCount());
583 size_t size_without_output =
584 desc->GetSize(OutputFrameStateCombine::Ignore());
585 // If the index is past the existing stack items in values_.
586 if (index >= size_without_output) {
587 // Materialize the result of the call instruction in this slot.
588 AddTranslationForOperand(
589 translation, iter->instruction(),
590 iter->instruction()->OutputAt(index - size_without_output),
591 MachineType::AnyTagged());
592 continue;
593 }
594 break;
595 }
596 case OutputFrameStateCombine::kPokeAt:
597 // The result of the call should be placed at position
598 // [index_from_top] in the stack (overwriting whatever was
599 // previously there).
600 size_t index_from_top =
601 desc->GetSize(combine) - 1 - combine.GetOffsetToPokeAt();
602 if (index >= index_from_top &&
603 index < index_from_top + iter->instruction()->OutputCount()) {
604 AddTranslationForOperand(
605 translation, iter->instruction(),
606 iter->instruction()->OutputAt(index - index_from_top),
607 MachineType::AnyTagged());
608 iter->Advance(); // We do not use this input, but we need to
609 // advace, as the input got replaced.
610 continue;
611 }
612 break;
613 }
614 StateValueDescriptor* value_desc = desc->GetStateValueDescriptor();
615 TranslateStateValueDescriptor(&value_desc->fields()[index], translation,
616 iter);
617 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400618}
619
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000620
621void CodeGenerator::BuildTranslationForFrameStateDescriptor(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000622 FrameStateDescriptor* descriptor, InstructionOperandIterator* iter,
623 Translation* translation, OutputFrameStateCombine state_combine) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000624 // Outer-most state must be added to translation first.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000625 if (descriptor->outer_state() != nullptr) {
626 BuildTranslationForFrameStateDescriptor(descriptor->outer_state(), iter,
627 translation,
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400628 OutputFrameStateCombine::Ignore());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000629 }
630
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000631 Handle<SharedFunctionInfo> shared_info;
632 if (!descriptor->shared_info().ToHandle(&shared_info)) {
633 if (!info()->has_shared_info()) {
634 return; // Stub with no SharedFunctionInfo.
635 }
636 shared_info = info()->shared_info();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000637 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000638 int shared_info_id = DefineDeoptimizationLiteral(shared_info);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000639
640 switch (descriptor->type()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000641 case FrameStateType::kJavaScriptFunction:
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000642 translation->BeginJSFrame(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000643 descriptor->bailout_id(), shared_info_id,
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400644 static_cast<unsigned int>(descriptor->GetSize(state_combine) -
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000645 (1 + descriptor->parameters_count())));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000646 break;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000647 case FrameStateType::kInterpretedFunction:
648 translation->BeginInterpretedFrame(
649 descriptor->bailout_id(), shared_info_id,
Ben Murdoch097c5b22016-05-18 11:27:45 +0100650 static_cast<unsigned int>(descriptor->locals_count() + 1));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000651 break;
652 case FrameStateType::kArgumentsAdaptor:
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000653 translation->BeginArgumentsAdaptorFrame(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000654 shared_info_id,
655 static_cast<unsigned int>(descriptor->parameters_count()));
656 break;
Ben Murdochda12d292016-06-02 14:46:10 +0100657 case FrameStateType::kTailCallerFunction:
658 translation->BeginTailCallerFrame(shared_info_id);
659 break;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000660 case FrameStateType::kConstructStub:
661 translation->BeginConstructStubFrame(
662 shared_info_id,
663 static_cast<unsigned int>(descriptor->parameters_count()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000664 break;
665 }
666
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000667 TranslateFrameStateDescriptorOperands(descriptor, iter, state_combine,
668 translation);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000669}
670
671
672int CodeGenerator::BuildTranslation(Instruction* instr, int pc_offset,
673 size_t frame_state_offset,
674 OutputFrameStateCombine state_combine) {
675 FrameStateDescriptor* descriptor =
676 GetFrameStateDescriptor(instr, frame_state_offset);
677 frame_state_offset++;
678
679 Translation translation(
680 &translations_, static_cast<int>(descriptor->GetFrameCount()),
681 static_cast<int>(descriptor->GetJSFrameCount()), zone());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000682 InstructionOperandIterator iter(instr, frame_state_offset);
683 BuildTranslationForFrameStateDescriptor(descriptor, &iter, &translation,
684 state_combine);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000685
686 int deoptimization_id = static_cast<int>(deoptimization_states_.size());
687
688 deoptimization_states_.push_back(new (zone()) DeoptimizationState(
689 descriptor->bailout_id(), translation.index(), pc_offset));
690
691 return deoptimization_id;
692}
693
694
695void CodeGenerator::AddTranslationForOperand(Translation* translation,
696 Instruction* instr,
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400697 InstructionOperand* op,
698 MachineType type) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000699 if (op->IsStackSlot()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000700 if (type.representation() == MachineRepresentation::kBit) {
701 translation->StoreBoolStackSlot(LocationOperand::cast(op)->index());
702 } else if (type == MachineType::Int8() || type == MachineType::Int16() ||
703 type == MachineType::Int32()) {
704 translation->StoreInt32StackSlot(LocationOperand::cast(op)->index());
705 } else if (type == MachineType::Uint8() || type == MachineType::Uint16() ||
706 type == MachineType::Uint32()) {
707 translation->StoreUint32StackSlot(LocationOperand::cast(op)->index());
708 } else if (type.representation() == MachineRepresentation::kTagged) {
709 translation->StoreStackSlot(LocationOperand::cast(op)->index());
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400710 } else {
711 CHECK(false);
712 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000713 } else if (op->IsDoubleStackSlot()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000714 DCHECK(IsFloatingPoint(type.representation()));
715 translation->StoreDoubleStackSlot(LocationOperand::cast(op)->index());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000716 } else if (op->IsRegister()) {
717 InstructionOperandConverter converter(this, instr);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000718 if (type.representation() == MachineRepresentation::kBit) {
719 translation->StoreBoolRegister(converter.ToRegister(op));
720 } else if (type == MachineType::Int8() || type == MachineType::Int16() ||
721 type == MachineType::Int32()) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400722 translation->StoreInt32Register(converter.ToRegister(op));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000723 } else if (type == MachineType::Uint8() || type == MachineType::Uint16() ||
724 type == MachineType::Uint32()) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400725 translation->StoreUint32Register(converter.ToRegister(op));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000726 } else if (type.representation() == MachineRepresentation::kTagged) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400727 translation->StoreRegister(converter.ToRegister(op));
728 } else {
729 CHECK(false);
730 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000731 } else if (op->IsDoubleRegister()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000732 DCHECK(IsFloatingPoint(type.representation()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000733 InstructionOperandConverter converter(this, instr);
734 translation->StoreDoubleRegister(converter.ToDoubleRegister(op));
735 } else if (op->IsImmediate()) {
736 InstructionOperandConverter converter(this, instr);
737 Constant constant = converter.ToConstant(op);
738 Handle<Object> constant_object;
739 switch (constant.type()) {
740 case Constant::kInt32:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000741 DCHECK(type == MachineType::Int32() || type == MachineType::Uint32() ||
742 type.representation() == MachineRepresentation::kBit);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000743 constant_object =
744 isolate()->factory()->NewNumberFromInt(constant.ToInt32());
745 break;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000746 case Constant::kFloat32:
747 DCHECK(type.representation() == MachineRepresentation::kFloat32 ||
748 type.representation() == MachineRepresentation::kTagged);
749 constant_object = isolate()->factory()->NewNumber(constant.ToFloat32());
750 break;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000751 case Constant::kFloat64:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000752 DCHECK(type.representation() == MachineRepresentation::kFloat64 ||
753 type.representation() == MachineRepresentation::kTagged);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000754 constant_object = isolate()->factory()->NewNumber(constant.ToFloat64());
755 break;
756 case Constant::kHeapObject:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000757 DCHECK(type.representation() == MachineRepresentation::kTagged);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000758 constant_object = constant.ToHeapObject();
759 break;
760 default:
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400761 CHECK(false);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000762 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000763 if (constant_object.is_identical_to(info()->closure())) {
764 translation->StoreJSFrameFunction();
765 } else {
766 int literal_id = DefineDeoptimizationLiteral(constant_object);
767 translation->StoreLiteral(literal_id);
768 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000769 } else {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400770 CHECK(false);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000771 }
772}
773
774
775void CodeGenerator::MarkLazyDeoptSite() {
776 last_lazy_deopt_pc_ = masm()->pc_offset();
777}
778
Ben Murdochda12d292016-06-02 14:46:10 +0100779DeoptimizationExit* CodeGenerator::AddDeoptimizationExit(
780 Instruction* instr, size_t frame_state_offset) {
781 int const deoptimization_id = BuildTranslation(
782 instr, -1, frame_state_offset, OutputFrameStateCombine::Ignore());
783 DeoptimizationExit* const exit =
784 new (zone()) DeoptimizationExit(deoptimization_id);
785 deoptimization_exits_.push_back(exit);
786 return exit;
787}
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000788
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000789int CodeGenerator::TailCallFrameStackSlotDelta(int stack_param_delta) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000790 // Leave the PC on the stack on platforms that have that as part of their ABI
791 int pc_slots = V8_TARGET_ARCH_STORES_RETURN_ADDRESS_ON_STACK ? 1 : 0;
Ben Murdochda12d292016-06-02 14:46:10 +0100792 int sp_slot_delta = frame_access_state()->has_frame()
793 ? (frame()->GetTotalFrameSlotCount() - pc_slots)
794 : 0;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000795 // Discard only slots that won't be used by new parameters.
796 sp_slot_delta += stack_param_delta;
797 return sp_slot_delta;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000798}
799
800
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400801OutOfLineCode::OutOfLineCode(CodeGenerator* gen)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000802 : frame_(gen->frame()), masm_(gen->masm()), next_(gen->ools_) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400803 gen->ools_ = this;
804}
805
806
807OutOfLineCode::~OutOfLineCode() {}
808
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000809} // namespace compiler
810} // namespace internal
811} // namespace v8