blob: 0e34c9f854244d9540f0dd46b9697eebc195382a [file] [log] [blame]
Ben Murdoch85b71792012-04-11 18:30:58 +01001// Copyright 2011 the V8 project authors. All rights reserved.
Ben Murdochb0fe1622011-05-05 13:52:32 +01002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#ifndef V8_ARM_LITHIUM_CODEGEN_ARM_H_
29#define V8_ARM_LITHIUM_CODEGEN_ARM_H_
30
31#include "arm/lithium-arm.h"
Ben Murdoche0cee9b2011-05-25 10:26:03 +010032#include "arm/lithium-gap-resolver-arm.h"
Ben Murdochb0fe1622011-05-05 13:52:32 +010033#include "deoptimizer.h"
34#include "safepoint-table.h"
35#include "scopes.h"
36
37namespace v8 {
38namespace internal {
39
40// Forward declarations.
41class LDeferredCode;
42class SafepointGenerator;
43
Ben Murdochb0fe1622011-05-05 13:52:32 +010044class LCodeGen BASE_EMBEDDED {
45 public:
46 LCodeGen(LChunk* chunk, MacroAssembler* assembler, CompilationInfo* info)
47 : chunk_(chunk),
48 masm_(assembler),
49 info_(info),
50 current_block_(-1),
51 current_instruction_(-1),
52 instructions_(chunk->instructions()),
53 deoptimizations_(4),
Ben Murdoch257744e2011-11-30 15:57:28 +000054 deopt_jump_table_(4),
Ben Murdochb0fe1622011-05-05 13:52:32 +010055 deoptimization_literals_(8),
56 inlined_function_count_(0),
Ben Murdoche0cee9b2011-05-25 10:26:03 +010057 scope_(info->scope()),
Ben Murdochb0fe1622011-05-05 13:52:32 +010058 status_(UNUSED),
59 deferred_(8),
Ben Murdoche0cee9b2011-05-25 10:26:03 +010060 osr_pc_offset_(-1),
Ben Murdoch2b4ba112012-01-20 14:57:15 +000061 last_lazy_deopt_pc_(0),
Ben Murdoch8b112d22011-06-08 16:22:53 +010062 resolver_(this),
63 expected_safepoint_kind_(Safepoint::kSimple) {
Ben Murdochb0fe1622011-05-05 13:52:32 +010064 PopulateDeoptimizationLiteralsWithInlinedFunctions();
65 }
66
Ben Murdoche0cee9b2011-05-25 10:26:03 +010067
68 // Simple accessors.
69 MacroAssembler* masm() const { return masm_; }
70 CompilationInfo* info() const { return info_; }
Steve Block44f0eee2011-05-26 01:26:41 +010071 Isolate* isolate() const { return info_->isolate(); }
72 Factory* factory() const { return isolate()->factory(); }
73 Heap* heap() const { return isolate()->heap(); }
Ben Murdoche0cee9b2011-05-25 10:26:03 +010074
75 // Support for converting LOperands to assembler types.
76 // LOperand must be a register.
77 Register ToRegister(LOperand* op) const;
78
79 // LOperand is loaded into scratch, unless already a register.
80 Register EmitLoadRegister(LOperand* op, Register scratch);
81
82 // LOperand must be a double register.
83 DoubleRegister ToDoubleRegister(LOperand* op) const;
84
85 // LOperand is loaded into dbl_scratch, unless already a double register.
86 DoubleRegister EmitLoadDoubleRegister(LOperand* op,
87 SwVfpRegister flt_scratch,
88 DoubleRegister dbl_scratch);
89 int ToInteger32(LConstantOperand* op) const;
90 Operand ToOperand(LOperand* op);
91 MemOperand ToMemOperand(LOperand* op) const;
92 // Returns a MemOperand pointing to the high word of a DoubleStackSlot.
93 MemOperand ToHighMemOperand(LOperand* op) const;
94
Ben Murdochb0fe1622011-05-05 13:52:32 +010095 // Try to generate code for the entire chunk, but it may fail if the
96 // chunk contains constructs we cannot handle. Returns true if the
97 // code generation attempt succeeded.
98 bool GenerateCode();
99
100 // Finish the code by setting stack height, safepoint, and bailout
101 // information on it.
102 void FinishCode(Handle<Code> code);
103
104 // Deferred code support.
Steve Block1e0659c2011-05-24 12:43:12 +0100105 template<int T>
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100106 void DoDeferredBinaryOpStub(LTemplateInstruction<1, 2, T>* instr,
107 Token::Value op);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100108 void DoDeferredNumberTagD(LNumberTagD* instr);
109 void DoDeferredNumberTagI(LNumberTagI* instr);
110 void DoDeferredTaggedToI(LTaggedToI* instr);
111 void DoDeferredMathAbsTaggedHeapNumber(LUnaryMathOperation* instr);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000112 void DoDeferredStackCheck(LStackCheck* instr);
Steve Block1e0659c2011-05-24 12:43:12 +0100113 void DoDeferredStringCharCodeAt(LStringCharCodeAt* instr);
Steve Block44f0eee2011-05-26 01:26:41 +0100114 void DoDeferredStringCharFromCode(LStringCharFromCode* instr);
Ben Murdoch2b4ba112012-01-20 14:57:15 +0000115 void DoDeferredInstanceOfKnownGlobal(LInstanceOfKnownGlobal* instr,
116 Label* map_check);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100117
118 // Parallel move support.
119 void DoParallelMove(LParallelMove* move);
Ben Murdoch257744e2011-11-30 15:57:28 +0000120 void DoGap(LGap* instr);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100121
Ben Murdochb8e0da22011-05-16 14:20:40 +0100122 // Emit frame translation commands for an environment.
123 void WriteTranslation(LEnvironment* environment, Translation* translation);
124
Ben Murdochb0fe1622011-05-05 13:52:32 +0100125 // Declare methods that deal with the individual node types.
126#define DECLARE_DO(type) void Do##type(L##type* node);
127 LITHIUM_CONCRETE_INSTRUCTION_LIST(DECLARE_DO)
128#undef DECLARE_DO
129
130 private:
131 enum Status {
132 UNUSED,
133 GENERATING,
134 DONE,
135 ABORTED
136 };
137
138 bool is_unused() const { return status_ == UNUSED; }
139 bool is_generating() const { return status_ == GENERATING; }
140 bool is_done() const { return status_ == DONE; }
141 bool is_aborted() const { return status_ == ABORTED; }
142
Ben Murdoch85b71792012-04-11 18:30:58 +0100143 int strict_mode_flag() const {
144 return info()->is_strict_mode() ? kStrictMode : kNonStrictMode;
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100145 }
146
Ben Murdochb0fe1622011-05-05 13:52:32 +0100147 LChunk* chunk() const { return chunk_; }
148 Scope* scope() const { return scope_; }
149 HGraph* graph() const { return chunk_->graph(); }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100150
Steve Block9fac8402011-05-12 15:51:54 +0100151 Register scratch0() { return r9; }
Ben Murdoch692be652012-01-10 18:47:50 +0000152 DwVfpRegister double_scratch0() { return kScratchDoubleReg; }
Steve Block9fac8402011-05-12 15:51:54 +0100153
Ben Murdochb0fe1622011-05-05 13:52:32 +0100154 int GetNextEmittedBlock(int block);
155 LInstruction* GetNextInstruction();
156
157 void EmitClassOfTest(Label* if_true,
158 Label* if_false,
159 Handle<String> class_name,
160 Register input,
161 Register temporary,
162 Register temporary2);
163
Ben Murdoch257744e2011-11-30 15:57:28 +0000164 int GetStackSlotCount() const { return chunk()->spill_slot_count(); }
165 int GetParameterCount() const { return scope()->num_parameters(); }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100166
167 void Abort(const char* format, ...);
168 void Comment(const char* format, ...);
169
170 void AddDeferredCode(LDeferredCode* code) { deferred_.Add(code); }
171
172 // Code generation passes. Returns true if code generation should
173 // continue.
174 bool GeneratePrologue();
175 bool GenerateBody();
176 bool GenerateDeferredCode();
Ben Murdoch257744e2011-11-30 15:57:28 +0000177 bool GenerateDeoptJumpTable();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100178 bool GenerateSafepointTable();
179
Ben Murdoch8b112d22011-06-08 16:22:53 +0100180 enum SafepointMode {
181 RECORD_SIMPLE_SAFEPOINT,
182 RECORD_SAFEPOINT_WITH_REGISTERS_AND_NO_ARGUMENTS
183 };
184
Ben Murdochb0fe1622011-05-05 13:52:32 +0100185 void CallCode(Handle<Code> code,
186 RelocInfo::Mode mode,
187 LInstruction* instr);
Ben Murdoch8b112d22011-06-08 16:22:53 +0100188
189 void CallCodeGeneric(Handle<Code> code,
190 RelocInfo::Mode mode,
191 LInstruction* instr,
192 SafepointMode safepoint_mode);
193
Steve Block44f0eee2011-05-26 01:26:41 +0100194 void CallRuntime(const Runtime::Function* function,
Ben Murdochb0fe1622011-05-05 13:52:32 +0100195 int num_arguments,
196 LInstruction* instr);
Ben Murdoch8b112d22011-06-08 16:22:53 +0100197
Ben Murdochb0fe1622011-05-05 13:52:32 +0100198 void CallRuntime(Runtime::FunctionId id,
199 int num_arguments,
200 LInstruction* instr) {
Steve Block44f0eee2011-05-26 01:26:41 +0100201 const Runtime::Function* function = Runtime::FunctionForId(id);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100202 CallRuntime(function, num_arguments, instr);
203 }
204
Ben Murdoch8b112d22011-06-08 16:22:53 +0100205 void CallRuntimeFromDeferred(Runtime::FunctionId id,
206 int argc,
207 LInstruction* instr);
208
Ben Murdochb0fe1622011-05-05 13:52:32 +0100209 // Generate a direct call to a known function. Expects the function
Ben Murdoch85b71792012-04-11 18:30:58 +0100210 // to be in edi.
Ben Murdochb0fe1622011-05-05 13:52:32 +0100211 void CallKnownFunction(Handle<JSFunction> function,
212 int arity,
Ben Murdoch257744e2011-11-30 15:57:28 +0000213 LInstruction* instr,
214 CallKind call_kind);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100215
Ben Murdochb8e0da22011-05-16 14:20:40 +0100216 void LoadHeapObject(Register result, Handle<HeapObject> object);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100217
Ben Murdoch2b4ba112012-01-20 14:57:15 +0000218 void RecordSafepointWithLazyDeopt(LInstruction* instr,
219 SafepointMode safepoint_mode);
Ben Murdoch8b112d22011-06-08 16:22:53 +0100220
Ben Murdoch2b4ba112012-01-20 14:57:15 +0000221 void RegisterEnvironmentForDeoptimization(LEnvironment* environment,
222 Safepoint::DeoptMode mode);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100223 void DeoptimizeIf(Condition cc, LEnvironment* environment);
224
225 void AddToTranslation(Translation* translation,
226 LOperand* op,
227 bool is_tagged);
228 void PopulateDeoptimizationData(Handle<Code> code);
229 int DefineDeoptimizationLiteral(Handle<Object> literal);
230
231 void PopulateDeoptimizationLiteralsWithInlinedFunctions();
232
233 Register ToRegister(int index) const;
234 DoubleRegister ToDoubleRegister(int index) const;
235
Ben Murdochb0fe1622011-05-05 13:52:32 +0100236 // Specific math operations - used from DoUnaryMathOperation.
Steve Block1e0659c2011-05-24 12:43:12 +0100237 void EmitIntegerMathAbs(LUnaryMathOperation* instr);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100238 void DoMathAbs(LUnaryMathOperation* instr);
239 void DoMathFloor(LUnaryMathOperation* instr);
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100240 void DoMathRound(LUnaryMathOperation* instr);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100241 void DoMathSqrt(LUnaryMathOperation* instr);
Steve Block44f0eee2011-05-26 01:26:41 +0100242 void DoMathPowHalf(LUnaryMathOperation* instr);
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100243 void DoMathLog(LUnaryMathOperation* instr);
244 void DoMathCos(LUnaryMathOperation* instr);
245 void DoMathSin(LUnaryMathOperation* instr);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100246
247 // Support for recording safepoint and position information.
Steve Block1e0659c2011-05-24 12:43:12 +0100248 void RecordSafepoint(LPointerMap* pointers,
249 Safepoint::Kind kind,
250 int arguments,
Ben Murdoch2b4ba112012-01-20 14:57:15 +0000251 Safepoint::DeoptMode mode);
252 void RecordSafepoint(LPointerMap* pointers, Safepoint::DeoptMode mode);
253 void RecordSafepoint(Safepoint::DeoptMode mode);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100254 void RecordSafepointWithRegisters(LPointerMap* pointers,
255 int arguments,
Ben Murdoch2b4ba112012-01-20 14:57:15 +0000256 Safepoint::DeoptMode mode);
Ben Murdochb8e0da22011-05-16 14:20:40 +0100257 void RecordSafepointWithRegistersAndDoubles(LPointerMap* pointers,
258 int arguments,
Ben Murdoch2b4ba112012-01-20 14:57:15 +0000259 Safepoint::DeoptMode mode);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100260 void RecordPosition(int position);
261
262 static Condition TokenToCondition(Token::Value op, bool is_unsigned);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000263 void EmitGoto(int block);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100264 void EmitBranch(int left_block, int right_block, Condition cc);
Ben Murdoch85b71792012-04-11 18:30:58 +0100265 void EmitCmpI(LOperand* left, LOperand* right);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100266 void EmitNumberUntagD(Register input,
267 DoubleRegister result,
Ben Murdoch7d3e7fc2011-07-12 16:37:06 +0100268 bool deoptimize_on_undefined,
Ben Murdochb0fe1622011-05-05 13:52:32 +0100269 LEnvironment* env);
270
271 // Emits optimized code for typeof x == "y". Modifies input register.
272 // Returns the condition on which a final split to
273 // true and false label should be made, to optimize fallthrough.
Ben Murdoch85b71792012-04-11 18:30:58 +0100274 Condition EmitTypeofIs(Label* true_label, Label* false_label,
275 Register input, Handle<String> type_name);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100276
277 // Emits optimized code for %_IsObject(x). Preserves input register.
278 // Returns the condition on which a final split to
279 // true and false label should be made, to optimize fallthrough.
280 Condition EmitIsObject(Register input,
281 Register temp1,
Ben Murdochb0fe1622011-05-05 13:52:32 +0100282 Label* is_not_object,
283 Label* is_object);
284
Steve Block1e0659c2011-05-24 12:43:12 +0100285 // Emits optimized code for %_IsConstructCall().
286 // Caller should branch on equal condition.
287 void EmitIsConstructCall(Register temp1, Register temp2);
288
Ben Murdoch257744e2011-11-30 15:57:28 +0000289 void EmitLoadFieldOrConstantFunction(Register result,
290 Register object,
291 Handle<Map> type,
292 Handle<String> name);
293
294 struct JumpTableEntry {
295 explicit inline JumpTableEntry(Address entry)
296 : label(),
297 address(entry) { }
298 Label label;
299 Address address;
300 };
Steve Block44f0eee2011-05-26 01:26:41 +0100301
Ben Murdoch2b4ba112012-01-20 14:57:15 +0000302 void EnsureSpaceForLazyDeopt();
303
Ben Murdochb0fe1622011-05-05 13:52:32 +0100304 LChunk* const chunk_;
305 MacroAssembler* const masm_;
306 CompilationInfo* const info_;
307
308 int current_block_;
309 int current_instruction_;
310 const ZoneList<LInstruction*>* instructions_;
311 ZoneList<LEnvironment*> deoptimizations_;
Ben Murdoch257744e2011-11-30 15:57:28 +0000312 ZoneList<JumpTableEntry> deopt_jump_table_;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100313 ZoneList<Handle<Object> > deoptimization_literals_;
314 int inlined_function_count_;
315 Scope* const scope_;
316 Status status_;
317 TranslationBuffer translations_;
318 ZoneList<LDeferredCode*> deferred_;
319 int osr_pc_offset_;
Ben Murdoch2b4ba112012-01-20 14:57:15 +0000320 int last_lazy_deopt_pc_;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100321
322 // Builder that keeps track of safepoints in the code. The table
323 // itself is emitted at the end of the generated code.
324 SafepointTableBuilder safepoints_;
325
Ben Murdochb8e0da22011-05-16 14:20:40 +0100326 // Compiler from a set of parallel moves to a sequential list of moves.
327 LGapResolver resolver_;
328
Ben Murdoch8b112d22011-06-08 16:22:53 +0100329 Safepoint::Kind expected_safepoint_kind_;
330
331 class PushSafepointRegistersScope BASE_EMBEDDED {
332 public:
333 PushSafepointRegistersScope(LCodeGen* codegen,
334 Safepoint::Kind kind)
335 : codegen_(codegen) {
336 ASSERT(codegen_->expected_safepoint_kind_ == Safepoint::kSimple);
337 codegen_->expected_safepoint_kind_ = kind;
338
339 switch (codegen_->expected_safepoint_kind_) {
340 case Safepoint::kWithRegisters:
341 codegen_->masm_->PushSafepointRegisters();
342 break;
343 case Safepoint::kWithRegistersAndDoubles:
344 codegen_->masm_->PushSafepointRegistersAndDoubles();
345 break;
346 default:
347 UNREACHABLE();
348 }
349 }
350
351 ~PushSafepointRegistersScope() {
352 Safepoint::Kind kind = codegen_->expected_safepoint_kind_;
353 ASSERT((kind & Safepoint::kWithRegisters) != 0);
354 switch (kind) {
355 case Safepoint::kWithRegisters:
356 codegen_->masm_->PopSafepointRegisters();
357 break;
358 case Safepoint::kWithRegistersAndDoubles:
359 codegen_->masm_->PopSafepointRegistersAndDoubles();
360 break;
361 default:
362 UNREACHABLE();
363 }
364 codegen_->expected_safepoint_kind_ = Safepoint::kSimple;
365 }
366
367 private:
368 LCodeGen* codegen_;
369 };
370
Ben Murdochb0fe1622011-05-05 13:52:32 +0100371 friend class LDeferredCode;
372 friend class LEnvironment;
373 friend class SafepointGenerator;
374 DISALLOW_COPY_AND_ASSIGN(LCodeGen);
375};
376
377
378class LDeferredCode: public ZoneObject {
379 public:
380 explicit LDeferredCode(LCodeGen* codegen)
Ben Murdoch85b71792012-04-11 18:30:58 +0100381 : codegen_(codegen), external_exit_(NULL) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100382 codegen->AddDeferredCode(this);
383 }
384
385 virtual ~LDeferredCode() { }
386 virtual void Generate() = 0;
387
Ben Murdoch85b71792012-04-11 18:30:58 +0100388 void SetExit(Label *exit) { external_exit_ = exit; }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100389 Label* entry() { return &entry_; }
390 Label* exit() { return external_exit_ != NULL ? external_exit_ : &exit_; }
391
392 protected:
393 LCodeGen* codegen() const { return codegen_; }
394 MacroAssembler* masm() const { return codegen_->masm(); }
395
396 private:
397 LCodeGen* codegen_;
398 Label entry_;
399 Label exit_;
400 Label* external_exit_;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100401};
402
403} } // namespace v8::internal
404
405#endif // V8_ARM_LITHIUM_CODEGEN_ARM_H_