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