blob: e9dd1494d3f5c14b726d6e67a392252e220a7185 [file] [log] [blame]
Ben Murdochb8e0da22011-05-16 14:20:40 +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;
Ben Murdoch592a9fc2012-03-05 11:04:45 +000090 double ToDouble(LConstantOperand* op) const;
Ben Murdoche0cee9b2011-05-25 10:26:03 +010091 Operand ToOperand(LOperand* op);
92 MemOperand ToMemOperand(LOperand* op) const;
93 // Returns a MemOperand pointing to the high word of a DoubleStackSlot.
94 MemOperand ToHighMemOperand(LOperand* op) const;
95
Ben Murdochb0fe1622011-05-05 13:52:32 +010096 // Try to generate code for the entire chunk, but it may fail if the
97 // chunk contains constructs we cannot handle. Returns true if the
98 // code generation attempt succeeded.
99 bool GenerateCode();
100
101 // Finish the code by setting stack height, safepoint, and bailout
102 // information on it.
103 void FinishCode(Handle<Code> code);
104
105 // Deferred code support.
Steve Block1e0659c2011-05-24 12:43:12 +0100106 template<int T>
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100107 void DoDeferredBinaryOpStub(LTemplateInstruction<1, 2, T>* instr,
108 Token::Value op);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100109 void DoDeferredNumberTagD(LNumberTagD* instr);
110 void DoDeferredNumberTagI(LNumberTagI* instr);
111 void DoDeferredTaggedToI(LTaggedToI* instr);
112 void DoDeferredMathAbsTaggedHeapNumber(LUnaryMathOperation* instr);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000113 void DoDeferredStackCheck(LStackCheck* instr);
Steve Block1e0659c2011-05-24 12:43:12 +0100114 void DoDeferredStringCharCodeAt(LStringCharCodeAt* instr);
Steve Block44f0eee2011-05-26 01:26:41 +0100115 void DoDeferredStringCharFromCode(LStringCharFromCode* instr);
Ben Murdoch2b4ba112012-01-20 14:57:15 +0000116 void DoDeferredInstanceOfKnownGlobal(LInstanceOfKnownGlobal* instr,
117 Label* map_check);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100118
119 // Parallel move support.
120 void DoParallelMove(LParallelMove* move);
Ben Murdoch257744e2011-11-30 15:57:28 +0000121 void DoGap(LGap* instr);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100122
Ben Murdochb8e0da22011-05-16 14:20:40 +0100123 // Emit frame translation commands for an environment.
124 void WriteTranslation(LEnvironment* environment, Translation* translation);
125
Ben Murdochb0fe1622011-05-05 13:52:32 +0100126 // Declare methods that deal with the individual node types.
127#define DECLARE_DO(type) void Do##type(L##type* node);
128 LITHIUM_CONCRETE_INSTRUCTION_LIST(DECLARE_DO)
129#undef DECLARE_DO
130
131 private:
132 enum Status {
133 UNUSED,
134 GENERATING,
135 DONE,
136 ABORTED
137 };
138
139 bool is_unused() const { return status_ == UNUSED; }
140 bool is_generating() const { return status_ == GENERATING; }
141 bool is_done() const { return status_ == DONE; }
142 bool is_aborted() const { return status_ == ABORTED; }
143
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000144 StrictModeFlag strict_mode_flag() const {
145 return info()->is_classic_mode() ? kNonStrictMode : kStrictMode;
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100146 }
147
Ben Murdochb0fe1622011-05-05 13:52:32 +0100148 LChunk* chunk() const { return chunk_; }
149 Scope* scope() const { return scope_; }
150 HGraph* graph() const { return chunk_->graph(); }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100151
Steve Block9fac8402011-05-12 15:51:54 +0100152 Register scratch0() { return r9; }
Ben Murdoch692be652012-01-10 18:47:50 +0000153 DwVfpRegister double_scratch0() { return kScratchDoubleReg; }
Steve Block9fac8402011-05-12 15:51:54 +0100154
Ben Murdochb0fe1622011-05-05 13:52:32 +0100155 int GetNextEmittedBlock(int block);
156 LInstruction* GetNextInstruction();
157
158 void EmitClassOfTest(Label* if_true,
159 Label* if_false,
160 Handle<String> class_name,
161 Register input,
162 Register temporary,
163 Register temporary2);
164
Ben Murdoch257744e2011-11-30 15:57:28 +0000165 int GetStackSlotCount() const { return chunk()->spill_slot_count(); }
166 int GetParameterCount() const { return scope()->num_parameters(); }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100167
168 void Abort(const char* format, ...);
169 void Comment(const char* format, ...);
170
171 void AddDeferredCode(LDeferredCode* code) { deferred_.Add(code); }
172
173 // Code generation passes. Returns true if code generation should
174 // continue.
175 bool GeneratePrologue();
176 bool GenerateBody();
177 bool GenerateDeferredCode();
Ben Murdoch257744e2011-11-30 15:57:28 +0000178 bool GenerateDeoptJumpTable();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100179 bool GenerateSafepointTable();
180
Ben Murdoch8b112d22011-06-08 16:22:53 +0100181 enum SafepointMode {
182 RECORD_SIMPLE_SAFEPOINT,
183 RECORD_SAFEPOINT_WITH_REGISTERS_AND_NO_ARGUMENTS
184 };
185
Ben Murdochb0fe1622011-05-05 13:52:32 +0100186 void CallCode(Handle<Code> code,
187 RelocInfo::Mode mode,
188 LInstruction* instr);
Ben Murdoch8b112d22011-06-08 16:22:53 +0100189
190 void CallCodeGeneric(Handle<Code> code,
191 RelocInfo::Mode mode,
192 LInstruction* instr,
193 SafepointMode safepoint_mode);
194
Steve Block44f0eee2011-05-26 01:26:41 +0100195 void CallRuntime(const Runtime::Function* function,
Ben Murdochb0fe1622011-05-05 13:52:32 +0100196 int num_arguments,
197 LInstruction* instr);
Ben Murdoch8b112d22011-06-08 16:22:53 +0100198
Ben Murdochb0fe1622011-05-05 13:52:32 +0100199 void CallRuntime(Runtime::FunctionId id,
200 int num_arguments,
201 LInstruction* instr) {
Steve Block44f0eee2011-05-26 01:26:41 +0100202 const Runtime::Function* function = Runtime::FunctionForId(id);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100203 CallRuntime(function, num_arguments, instr);
204 }
205
Ben Murdoch8b112d22011-06-08 16:22:53 +0100206 void CallRuntimeFromDeferred(Runtime::FunctionId id,
207 int argc,
208 LInstruction* instr);
209
Ben Murdochb0fe1622011-05-05 13:52:32 +0100210 // Generate a direct call to a known function. Expects the function
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000211 // to be in r1.
Ben Murdochb0fe1622011-05-05 13:52:32 +0100212 void CallKnownFunction(Handle<JSFunction> function,
213 int arity,
Ben Murdoch257744e2011-11-30 15:57:28 +0000214 LInstruction* instr,
215 CallKind call_kind);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100216
Ben Murdochb8e0da22011-05-16 14:20:40 +0100217 void LoadHeapObject(Register result, Handle<HeapObject> object);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100218
Ben Murdoch2b4ba112012-01-20 14:57:15 +0000219 void RecordSafepointWithLazyDeopt(LInstruction* instr,
220 SafepointMode safepoint_mode);
Ben Murdoch8b112d22011-06-08 16:22:53 +0100221
Ben Murdoch2b4ba112012-01-20 14:57:15 +0000222 void RegisterEnvironmentForDeoptimization(LEnvironment* environment,
223 Safepoint::DeoptMode mode);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100224 void DeoptimizeIf(Condition cc, LEnvironment* environment);
225
226 void AddToTranslation(Translation* translation,
227 LOperand* op,
228 bool is_tagged);
229 void PopulateDeoptimizationData(Handle<Code> code);
230 int DefineDeoptimizationLiteral(Handle<Object> literal);
231
232 void PopulateDeoptimizationLiteralsWithInlinedFunctions();
233
234 Register ToRegister(int index) const;
235 DoubleRegister ToDoubleRegister(int index) const;
236
Ben Murdochb0fe1622011-05-05 13:52:32 +0100237 // Specific math operations - used from DoUnaryMathOperation.
Steve Block1e0659c2011-05-24 12:43:12 +0100238 void EmitIntegerMathAbs(LUnaryMathOperation* instr);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100239 void DoMathAbs(LUnaryMathOperation* instr);
240 void DoMathFloor(LUnaryMathOperation* instr);
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100241 void DoMathRound(LUnaryMathOperation* instr);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100242 void DoMathSqrt(LUnaryMathOperation* instr);
Steve Block44f0eee2011-05-26 01:26:41 +0100243 void DoMathPowHalf(LUnaryMathOperation* instr);
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100244 void DoMathLog(LUnaryMathOperation* instr);
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000245 void DoMathTan(LUnaryMathOperation* instr);
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100246 void DoMathCos(LUnaryMathOperation* instr);
247 void DoMathSin(LUnaryMathOperation* instr);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100248
249 // Support for recording safepoint and position information.
Steve Block1e0659c2011-05-24 12:43:12 +0100250 void RecordSafepoint(LPointerMap* pointers,
251 Safepoint::Kind kind,
252 int arguments,
Ben Murdoch2b4ba112012-01-20 14:57:15 +0000253 Safepoint::DeoptMode mode);
254 void RecordSafepoint(LPointerMap* pointers, Safepoint::DeoptMode mode);
255 void RecordSafepoint(Safepoint::DeoptMode mode);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100256 void RecordSafepointWithRegisters(LPointerMap* pointers,
257 int arguments,
Ben Murdoch2b4ba112012-01-20 14:57:15 +0000258 Safepoint::DeoptMode mode);
Ben Murdochb8e0da22011-05-16 14:20:40 +0100259 void RecordSafepointWithRegistersAndDoubles(LPointerMap* pointers,
260 int arguments,
Ben Murdoch2b4ba112012-01-20 14:57:15 +0000261 Safepoint::DeoptMode mode);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100262 void RecordPosition(int position);
263
264 static Condition TokenToCondition(Token::Value op, bool is_unsigned);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000265 void EmitGoto(int block);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100266 void EmitBranch(int left_block, int right_block, Condition cc);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100267 void EmitNumberUntagD(Register input,
268 DoubleRegister result,
Ben Murdoch7d3e7fc2011-07-12 16:37:06 +0100269 bool deoptimize_on_undefined,
Ben Murdochb0fe1622011-05-05 13:52:32 +0100270 LEnvironment* env);
271
272 // Emits optimized code for typeof x == "y". Modifies input register.
273 // Returns the condition on which a final split to
274 // true and false label should be made, to optimize fallthrough.
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000275 Condition EmitTypeofIs(Label* true_label,
276 Label* false_label,
277 Register input,
278 Handle<String> type_name);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100279
280 // Emits optimized code for %_IsObject(x). Preserves input register.
281 // Returns the condition on which a final split to
282 // true and false label should be made, to optimize fallthrough.
283 Condition EmitIsObject(Register input,
284 Register temp1,
Ben Murdochb0fe1622011-05-05 13:52:32 +0100285 Label* is_not_object,
286 Label* is_object);
287
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000288 // Emits optimized code for %_IsString(x). Preserves input register.
289 // Returns the condition on which a final split to
290 // true and false label should be made, to optimize fallthrough.
291 Condition EmitIsString(Register input,
292 Register temp1,
293 Label* is_not_string);
294
Steve Block1e0659c2011-05-24 12:43:12 +0100295 // Emits optimized code for %_IsConstructCall().
296 // Caller should branch on equal condition.
297 void EmitIsConstructCall(Register temp1, Register temp2);
298
Ben Murdoch257744e2011-11-30 15:57:28 +0000299 void EmitLoadFieldOrConstantFunction(Register result,
300 Register object,
301 Handle<Map> type,
302 Handle<String> name);
303
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000304 // Emits optimized code to deep-copy the contents of statically known
305 // object graphs (e.g. object literal boilerplate).
306 void EmitDeepCopy(Handle<JSObject> object,
307 Register result,
308 Register source,
309 int* offset);
310
Ben Murdoch257744e2011-11-30 15:57:28 +0000311 struct JumpTableEntry {
312 explicit inline JumpTableEntry(Address entry)
313 : label(),
314 address(entry) { }
315 Label label;
316 Address address;
317 };
Steve Block44f0eee2011-05-26 01:26:41 +0100318
Ben Murdoch2b4ba112012-01-20 14:57:15 +0000319 void EnsureSpaceForLazyDeopt();
320
Ben Murdochb0fe1622011-05-05 13:52:32 +0100321 LChunk* const chunk_;
322 MacroAssembler* const masm_;
323 CompilationInfo* const info_;
324
325 int current_block_;
326 int current_instruction_;
327 const ZoneList<LInstruction*>* instructions_;
328 ZoneList<LEnvironment*> deoptimizations_;
Ben Murdoch257744e2011-11-30 15:57:28 +0000329 ZoneList<JumpTableEntry> deopt_jump_table_;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100330 ZoneList<Handle<Object> > deoptimization_literals_;
331 int inlined_function_count_;
332 Scope* const scope_;
333 Status status_;
334 TranslationBuffer translations_;
335 ZoneList<LDeferredCode*> deferred_;
336 int osr_pc_offset_;
Ben Murdoch2b4ba112012-01-20 14:57:15 +0000337 int last_lazy_deopt_pc_;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100338
339 // Builder that keeps track of safepoints in the code. The table
340 // itself is emitted at the end of the generated code.
341 SafepointTableBuilder safepoints_;
342
Ben Murdochb8e0da22011-05-16 14:20:40 +0100343 // Compiler from a set of parallel moves to a sequential list of moves.
344 LGapResolver resolver_;
345
Ben Murdoch8b112d22011-06-08 16:22:53 +0100346 Safepoint::Kind expected_safepoint_kind_;
347
348 class PushSafepointRegistersScope BASE_EMBEDDED {
349 public:
350 PushSafepointRegistersScope(LCodeGen* codegen,
351 Safepoint::Kind kind)
352 : codegen_(codegen) {
353 ASSERT(codegen_->expected_safepoint_kind_ == Safepoint::kSimple);
354 codegen_->expected_safepoint_kind_ = kind;
355
356 switch (codegen_->expected_safepoint_kind_) {
357 case Safepoint::kWithRegisters:
358 codegen_->masm_->PushSafepointRegisters();
359 break;
360 case Safepoint::kWithRegistersAndDoubles:
361 codegen_->masm_->PushSafepointRegistersAndDoubles();
362 break;
363 default:
364 UNREACHABLE();
365 }
366 }
367
368 ~PushSafepointRegistersScope() {
369 Safepoint::Kind kind = codegen_->expected_safepoint_kind_;
370 ASSERT((kind & Safepoint::kWithRegisters) != 0);
371 switch (kind) {
372 case Safepoint::kWithRegisters:
373 codegen_->masm_->PopSafepointRegisters();
374 break;
375 case Safepoint::kWithRegistersAndDoubles:
376 codegen_->masm_->PopSafepointRegistersAndDoubles();
377 break;
378 default:
379 UNREACHABLE();
380 }
381 codegen_->expected_safepoint_kind_ = Safepoint::kSimple;
382 }
383
384 private:
385 LCodeGen* codegen_;
386 };
387
Ben Murdochb0fe1622011-05-05 13:52:32 +0100388 friend class LDeferredCode;
389 friend class LEnvironment;
390 friend class SafepointGenerator;
391 DISALLOW_COPY_AND_ASSIGN(LCodeGen);
392};
393
394
395class LDeferredCode: public ZoneObject {
396 public:
397 explicit LDeferredCode(LCodeGen* codegen)
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000398 : codegen_(codegen),
399 external_exit_(NULL),
400 instruction_index_(codegen->current_instruction_) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100401 codegen->AddDeferredCode(this);
402 }
403
404 virtual ~LDeferredCode() { }
405 virtual void Generate() = 0;
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000406 virtual LInstruction* instr() = 0;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100407
408 void SetExit(Label *exit) { external_exit_ = exit; }
409 Label* entry() { return &entry_; }
410 Label* exit() { return external_exit_ != NULL ? external_exit_ : &exit_; }
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000411 int instruction_index() const { return instruction_index_; }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100412
413 protected:
414 LCodeGen* codegen() const { return codegen_; }
415 MacroAssembler* masm() const { return codegen_->masm(); }
416
417 private:
418 LCodeGen* codegen_;
419 Label entry_;
420 Label exit_;
421 Label* external_exit_;
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000422 int instruction_index_;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100423};
424
425} } // namespace v8::internal
426
427#endif // V8_ARM_LITHIUM_CODEGEN_ARM_H_