blob: 8ae3b3c5d37c071f1a933a0b5497bd5fad51597d [file] [log] [blame]
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001// Copyright 2014 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#ifndef V8_PPC_LITHIUM_CODEGEN_PPC_H_
6#define V8_PPC_LITHIUM_CODEGEN_PPC_H_
7
8#include "src/ppc/lithium-ppc.h"
9
10#include "src/ppc/lithium-gap-resolver-ppc.h"
11#include "src/deoptimizer.h"
12#include "src/lithium-codegen.h"
13#include "src/safepoint-table.h"
14#include "src/scopes.h"
15#include "src/utils.h"
16
17namespace v8 {
18namespace internal {
19
20// Forward declarations.
21class LDeferredCode;
22class SafepointGenerator;
23
24class LCodeGen : public LCodeGenBase {
25 public:
26 LCodeGen(LChunk* chunk, MacroAssembler* assembler, CompilationInfo* info)
27 : LCodeGenBase(chunk, assembler, info),
28 deoptimizations_(4, info->zone()),
29 jump_table_(4, info->zone()),
30 deoptimization_literals_(8, info->zone()),
31 inlined_function_count_(0),
32 scope_(info->scope()),
33 translations_(info->zone()),
34 deferred_(8, info->zone()),
35 osr_pc_offset_(-1),
36 frame_is_built_(false),
37 safepoints_(info->zone()),
38 resolver_(this),
39 expected_safepoint_kind_(Safepoint::kSimple) {
40 PopulateDeoptimizationLiteralsWithInlinedFunctions();
41 }
42
43
44 int LookupDestination(int block_id) const {
45 return chunk()->LookupDestination(block_id);
46 }
47
48 bool IsNextEmittedBlock(int block_id) const {
49 return LookupDestination(block_id) == GetNextEmittedBlock();
50 }
51
52 bool NeedsEagerFrame() const {
53 return GetStackSlotCount() > 0 || info()->is_non_deferred_calling() ||
54 !info()->IsStub() || info()->requires_frame();
55 }
56 bool NeedsDeferredFrame() const {
57 return !NeedsEagerFrame() && info()->is_deferred_calling();
58 }
59
60 LinkRegisterStatus GetLinkRegisterState() const {
61 return frame_is_built_ ? kLRHasBeenSaved : kLRHasNotBeenSaved;
62 }
63
64 // Support for converting LOperands to assembler types.
65 // LOperand must be a register.
66 Register ToRegister(LOperand* op) const;
67
68 // LOperand is loaded into scratch, unless already a register.
69 Register EmitLoadRegister(LOperand* op, Register scratch);
70
71 // LConstantOperand must be an Integer32 or Smi
72 void EmitLoadIntegerConstant(LConstantOperand* const_op, Register dst);
73
74 // LOperand must be a double register.
75 DoubleRegister ToDoubleRegister(LOperand* op) const;
76
77 intptr_t ToRepresentation(LConstantOperand* op,
78 const Representation& r) const;
79 int32_t ToInteger32(LConstantOperand* op) const;
80 Smi* ToSmi(LConstantOperand* op) const;
81 double ToDouble(LConstantOperand* op) const;
82 Operand ToOperand(LOperand* op);
83 MemOperand ToMemOperand(LOperand* op) const;
84 // Returns a MemOperand pointing to the high word of a DoubleStackSlot.
85 MemOperand ToHighMemOperand(LOperand* op) const;
86
87 bool IsInteger32(LConstantOperand* op) const;
88 bool IsSmi(LConstantOperand* op) const;
89 Handle<Object> ToHandle(LConstantOperand* op) const;
90
91 // Try to generate code for the entire chunk, but it may fail if the
92 // chunk contains constructs we cannot handle. Returns true if the
93 // code generation attempt succeeded.
94 bool GenerateCode();
95
96 // Finish the code by setting stack height, safepoint, and bailout
97 // information on it.
98 void FinishCode(Handle<Code> code);
99
100 // Deferred code support.
101 void DoDeferredNumberTagD(LNumberTagD* instr);
102
103 enum IntegerSignedness { SIGNED_INT32, UNSIGNED_INT32 };
104 void DoDeferredNumberTagIU(LInstruction* instr, LOperand* value,
105 LOperand* temp1, LOperand* temp2,
106 IntegerSignedness signedness);
107
108 void DoDeferredTaggedToI(LTaggedToI* instr);
109 void DoDeferredMathAbsTaggedHeapNumber(LMathAbs* instr);
110 void DoDeferredStackCheck(LStackCheck* instr);
111 void DoDeferredStringCharCodeAt(LStringCharCodeAt* instr);
112 void DoDeferredStringCharFromCode(LStringCharFromCode* instr);
113 void DoDeferredAllocate(LAllocate* instr);
114 void DoDeferredInstanceOfKnownGlobal(LInstanceOfKnownGlobal* instr,
115 Label* map_check);
116 void DoDeferredInstanceMigration(LCheckMaps* instr, Register object);
117 void DoDeferredLoadMutableDouble(LLoadFieldByIndex* instr, Register result,
118 Register object, Register index);
119
120 // Parallel move support.
121 void DoParallelMove(LParallelMove* move);
122 void DoGap(LGap* instr);
123
124 MemOperand PrepareKeyedOperand(Register key, Register base,
125 bool key_is_constant, bool key_is_tagged,
126 int constant_key, int element_size_shift,
127 int base_offset);
128
129 // Emit frame translation commands for an environment.
130 void WriteTranslation(LEnvironment* environment, Translation* translation);
131
132// Declare methods that deal with the individual node types.
133#define DECLARE_DO(type) void Do##type(L##type* node);
134 LITHIUM_CONCRETE_INSTRUCTION_LIST(DECLARE_DO)
135#undef DECLARE_DO
136
137 private:
138 StrictMode strict_mode() const { return info()->strict_mode(); }
139
140 Scope* scope() const { return scope_; }
141
142 Register scratch0() { return r11; }
143 DoubleRegister double_scratch0() { return kScratchDoubleReg; }
144
145 LInstruction* GetNextInstruction();
146
147 void EmitClassOfTest(Label* if_true, Label* if_false,
148 Handle<String> class_name, Register input,
149 Register temporary, Register temporary2);
150
151 int GetStackSlotCount() const { return chunk()->spill_slot_count(); }
152
153 void AddDeferredCode(LDeferredCode* code) { deferred_.Add(code, zone()); }
154
155 void SaveCallerDoubles();
156 void RestoreCallerDoubles();
157
158 // Code generation passes. Returns true if code generation should
159 // continue.
160 void GenerateBodyInstructionPre(LInstruction* instr) OVERRIDE;
161 bool GeneratePrologue();
162 bool GenerateDeferredCode();
163 bool GenerateJumpTable();
164 bool GenerateSafepointTable();
165
166 // Generates the custom OSR entrypoint and sets the osr_pc_offset.
167 void GenerateOsrPrologue();
168
169 enum SafepointMode {
170 RECORD_SIMPLE_SAFEPOINT,
171 RECORD_SAFEPOINT_WITH_REGISTERS_AND_NO_ARGUMENTS
172 };
173
174 void CallCode(Handle<Code> code, RelocInfo::Mode mode, LInstruction* instr);
175
176 void CallCodeGeneric(Handle<Code> code, RelocInfo::Mode mode,
177 LInstruction* instr, SafepointMode safepoint_mode);
178
179 void CallRuntime(const Runtime::Function* function, int num_arguments,
180 LInstruction* instr,
181 SaveFPRegsMode save_doubles = kDontSaveFPRegs);
182
183 void CallRuntime(Runtime::FunctionId id, int num_arguments,
184 LInstruction* instr) {
185 const Runtime::Function* function = Runtime::FunctionForId(id);
186 CallRuntime(function, num_arguments, instr);
187 }
188
189 void LoadContextFromDeferred(LOperand* context);
190 void CallRuntimeFromDeferred(Runtime::FunctionId id, int argc,
191 LInstruction* instr, LOperand* context);
192
193 enum R4State { R4_UNINITIALIZED, R4_CONTAINS_TARGET };
194
195 // Generate a direct call to a known function. Expects the function
196 // to be in r4.
197 void CallKnownFunction(Handle<JSFunction> function,
198 int formal_parameter_count, int arity,
199 LInstruction* instr, R4State r4_state);
200
201 void RecordSafepointWithLazyDeopt(LInstruction* instr,
202 SafepointMode safepoint_mode);
203
204 void RegisterEnvironmentForDeoptimization(LEnvironment* environment,
205 Safepoint::DeoptMode mode);
206 void DeoptimizeIf(Condition condition, LInstruction* instr,
207 const char* detail, Deoptimizer::BailoutType bailout_type,
208 CRegister cr = cr7);
209 void DeoptimizeIf(Condition condition, LInstruction* instr,
210 const char* detail, CRegister cr = cr7);
211
212 void AddToTranslation(LEnvironment* environment, Translation* translation,
213 LOperand* op, bool is_tagged, bool is_uint32,
214 int* object_index_pointer,
215 int* dematerialized_index_pointer);
216 void PopulateDeoptimizationData(Handle<Code> code);
217 int DefineDeoptimizationLiteral(Handle<Object> literal);
218
219 void PopulateDeoptimizationLiteralsWithInlinedFunctions();
220
221 Register ToRegister(int index) const;
222 DoubleRegister ToDoubleRegister(int index) const;
223
224 MemOperand BuildSeqStringOperand(Register string, LOperand* index,
225 String::Encoding encoding);
226
227 void EmitMathAbs(LMathAbs* instr);
228#if V8_TARGET_ARCH_PPC64
229 void EmitInteger32MathAbs(LMathAbs* instr);
230#endif
231
232 // Support for recording safepoint and position information.
233 void RecordSafepoint(LPointerMap* pointers, Safepoint::Kind kind,
234 int arguments, Safepoint::DeoptMode mode);
235 void RecordSafepoint(LPointerMap* pointers, Safepoint::DeoptMode mode);
236 void RecordSafepoint(Safepoint::DeoptMode mode);
237 void RecordSafepointWithRegisters(LPointerMap* pointers, int arguments,
238 Safepoint::DeoptMode mode);
239
240 void RecordAndWritePosition(int position) OVERRIDE;
241
242 static Condition TokenToCondition(Token::Value op);
243 void EmitGoto(int block);
244
245 // EmitBranch expects to be the last instruction of a block.
246 template <class InstrType>
247 void EmitBranch(InstrType instr, Condition condition, CRegister cr = cr7);
248 template <class InstrType>
249 void EmitFalseBranch(InstrType instr, Condition condition,
250 CRegister cr = cr7);
251 void EmitNumberUntagD(LNumberUntagD* instr, Register input,
252 DoubleRegister result, NumberUntagDMode mode);
253
254 // Emits optimized code for typeof x == "y". Modifies input register.
255 // Returns the condition on which a final split to
256 // true and false label should be made, to optimize fallthrough.
257 Condition EmitTypeofIs(Label* true_label, Label* false_label, Register input,
258 Handle<String> type_name);
259
260 // Emits optimized code for %_IsObject(x). Preserves input register.
261 // Returns the condition on which a final split to
262 // true and false label should be made, to optimize fallthrough.
263 Condition EmitIsObject(Register input, Register temp1, Label* is_not_object,
264 Label* is_object);
265
266 // Emits optimized code for %_IsString(x). Preserves input register.
267 // Returns the condition on which a final split to
268 // true and false label should be made, to optimize fallthrough.
269 Condition EmitIsString(Register input, Register temp1, Label* is_not_string,
270 SmiCheck check_needed);
271
272 // Emits optimized code for %_IsConstructCall().
273 // Caller should branch on equal condition.
274 void EmitIsConstructCall(Register temp1, Register temp2);
275
276 // Emits optimized code to deep-copy the contents of statically known
277 // object graphs (e.g. object literal boilerplate).
278 void EmitDeepCopy(Handle<JSObject> object, Register result, Register source,
279 int* offset, AllocationSiteMode mode);
280
281 void EnsureSpaceForLazyDeopt(int space_needed) OVERRIDE;
282 void DoLoadKeyedExternalArray(LLoadKeyed* instr);
283 void DoLoadKeyedFixedDoubleArray(LLoadKeyed* instr);
284 void DoLoadKeyedFixedArray(LLoadKeyed* instr);
285 void DoStoreKeyedExternalArray(LStoreKeyed* instr);
286 void DoStoreKeyedFixedDoubleArray(LStoreKeyed* instr);
287 void DoStoreKeyedFixedArray(LStoreKeyed* instr);
288
289 template <class T>
290 void EmitVectorLoadICRegisters(T* instr);
291
292 ZoneList<LEnvironment*> deoptimizations_;
293 ZoneList<Deoptimizer::JumpTableEntry> jump_table_;
294 ZoneList<Handle<Object> > deoptimization_literals_;
295 int inlined_function_count_;
296 Scope* const scope_;
297 TranslationBuffer translations_;
298 ZoneList<LDeferredCode*> deferred_;
299 int osr_pc_offset_;
300 bool frame_is_built_;
301
302 // Builder that keeps track of safepoints in the code. The table
303 // itself is emitted at the end of the generated code.
304 SafepointTableBuilder safepoints_;
305
306 // Compiler from a set of parallel moves to a sequential list of moves.
307 LGapResolver resolver_;
308
309 Safepoint::Kind expected_safepoint_kind_;
310
311 class PushSafepointRegistersScope FINAL BASE_EMBEDDED {
312 public:
313 explicit PushSafepointRegistersScope(LCodeGen* codegen)
314 : codegen_(codegen) {
315 DCHECK(codegen_->info()->is_calling());
316 DCHECK(codegen_->expected_safepoint_kind_ == Safepoint::kSimple);
317 codegen_->expected_safepoint_kind_ = Safepoint::kWithRegisters;
318 StoreRegistersStateStub stub(codegen_->isolate());
319 codegen_->masm_->CallStub(&stub);
320 }
321
322 ~PushSafepointRegistersScope() {
323 DCHECK(codegen_->expected_safepoint_kind_ == Safepoint::kWithRegisters);
324 RestoreRegistersStateStub stub(codegen_->isolate());
325 codegen_->masm_->CallStub(&stub);
326 codegen_->expected_safepoint_kind_ = Safepoint::kSimple;
327 }
328
329 private:
330 LCodeGen* codegen_;
331 };
332
333 friend class LDeferredCode;
334 friend class LEnvironment;
335 friend class SafepointGenerator;
336 DISALLOW_COPY_AND_ASSIGN(LCodeGen);
337};
338
339
340class LDeferredCode : public ZoneObject {
341 public:
342 explicit LDeferredCode(LCodeGen* codegen)
343 : codegen_(codegen),
344 external_exit_(NULL),
345 instruction_index_(codegen->current_instruction_) {
346 codegen->AddDeferredCode(this);
347 }
348
349 virtual ~LDeferredCode() {}
350 virtual void Generate() = 0;
351 virtual LInstruction* instr() = 0;
352
353 void SetExit(Label* exit) { external_exit_ = exit; }
354 Label* entry() { return &entry_; }
355 Label* exit() { return external_exit_ != NULL ? external_exit_ : &exit_; }
356 int instruction_index() const { return instruction_index_; }
357
358 protected:
359 LCodeGen* codegen() const { return codegen_; }
360 MacroAssembler* masm() const { return codegen_->masm(); }
361
362 private:
363 LCodeGen* codegen_;
364 Label entry_;
365 Label exit_;
366 Label* external_exit_;
367 int instruction_index_;
368};
369}
370} // namespace v8::internal
371
372#endif // V8_PPC_LITHIUM_CODEGEN_PPC_H_