blob: 2a54681990b70abb3d875280a645fe7fd7b2b62d [file] [log] [blame]
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001// Copyright 2011 the V8 project authors. All rights reserved.
ager@chromium.org5c838252010-02-19 08:53:10 +00002// 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
lrn@chromium.org7516f052011-03-30 08:52:27 +000028#ifndef V8_MIPS_LITHIUM_CODEGEN_MIPS_H_
29#define V8_MIPS_LITHIUM_CODEGEN_MIPS_H_
ager@chromium.org5c838252010-02-19 08:53:10 +000030
lrn@chromium.org7516f052011-03-30 08:52:27 +000031#include "mips/lithium-mips.h"
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +000032#include "mips/lithium-gap-resolver-mips.h"
lrn@chromium.org7516f052011-03-30 08:52:27 +000033#include "deoptimizer.h"
34#include "safepoint-table.h"
35#include "scopes.h"
36
ager@chromium.org5c838252010-02-19 08:53:10 +000037namespace v8 {
38namespace internal {
39
lrn@chromium.org7516f052011-03-30 08:52:27 +000040// Forward declarations.
41class LDeferredCode;
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +000042class SafepointGenerator;
ager@chromium.org5c838252010-02-19 08:53:10 +000043
lrn@chromium.org7516f052011-03-30 08:52:27 +000044class LCodeGen BASE_EMBEDDED {
45 public:
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +000046 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),
54 deopt_jump_table_(4),
55 deoptimization_literals_(8),
56 inlined_function_count_(0),
57 scope_(info->scope()),
58 status_(UNUSED),
59 deferred_(8),
60 osr_pc_offset_(-1),
erikcorry0ad885c2011-11-21 13:51:57 +000061 last_lazy_deopt_pc_(0),
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +000062 resolver_(this),
63 expected_safepoint_kind_(Safepoint::kSimple) {
64 PopulateDeoptimizationLiteralsWithInlinedFunctions();
65 }
66
67
68 // Simple accessors.
69 MacroAssembler* masm() const { return masm_; }
70 CompilationInfo* info() const { return info_; }
71 Isolate* isolate() const { return info_->isolate(); }
72 Factory* factory() const { return isolate()->factory(); }
73 Heap* heap() const { return isolate()->heap(); }
74
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 FloatRegister flt_scratch,
88 DoubleRegister dbl_scratch);
89 int ToInteger32(LConstantOperand* op) const;
90 double ToDouble(LConstantOperand* op) const;
91 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;
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000095
rossberg@chromium.orgfab14982012-01-05 15:02:15 +000096 bool IsInteger32(LConstantOperand* op) const;
97 Handle<Object> ToHandle(LConstantOperand* op) const;
98
lrn@chromium.org7516f052011-03-30 08:52:27 +000099 // Try to generate code for the entire chunk, but it may fail if the
100 // chunk contains constructs we cannot handle. Returns true if the
101 // code generation attempt succeeded.
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000102 bool GenerateCode();
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000103
lrn@chromium.org7516f052011-03-30 08:52:27 +0000104 // Finish the code by setting stack height, safepoint, and bailout
105 // information on it.
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000106 void FinishCode(Handle<Code> code);
107
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000108 void DoDeferredNumberTagD(LNumberTagD* instr);
109 void DoDeferredNumberTagI(LNumberTagI* instr);
110 void DoDeferredTaggedToI(LTaggedToI* instr);
111 void DoDeferredMathAbsTaggedHeapNumber(LUnaryMathOperation* instr);
112 void DoDeferredStackCheck(LStackCheck* instr);
113 void DoDeferredStringCharCodeAt(LStringCharCodeAt* instr);
114 void DoDeferredStringCharFromCode(LStringCharFromCode* instr);
erikcorry0ad885c2011-11-21 13:51:57 +0000115 void DoDeferredInstanceOfKnownGlobal(LInstanceOfKnownGlobal* instr,
116 Label* map_check);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000117
118 // Parallel move support.
119 void DoParallelMove(LParallelMove* move);
120 void DoGap(LGap* instr);
121
122 // Emit frame translation commands for an environment.
123 void WriteTranslation(LEnvironment* environment, Translation* translation);
124
125 // 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
143 StrictModeFlag strict_mode_flag() const {
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000144 return info()->is_classic_mode() ? kNonStrictMode : kStrictMode;
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000145 }
146
147 LChunk* chunk() const { return chunk_; }
148 Scope* scope() const { return scope_; }
149 HGraph* graph() const { return chunk_->graph(); }
150
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000151 Register scratch0() { return kLithiumScratchReg; }
152 Register scratch1() { return kLithiumScratchReg2; }
153 DoubleRegister double_scratch0() { return kLithiumScratchDouble; }
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000154
155 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
165 int GetStackSlotCount() const { return chunk()->spill_slot_count(); }
166 int GetParameterCount() const { return scope()->num_parameters(); }
167
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();
178 bool GenerateDeoptJumpTable();
179 bool GenerateSafepointTable();
180
181 enum SafepointMode {
182 RECORD_SIMPLE_SAFEPOINT,
183 RECORD_SAFEPOINT_WITH_REGISTERS_AND_NO_ARGUMENTS
184 };
185
186 void CallCode(Handle<Code> code,
187 RelocInfo::Mode mode,
188 LInstruction* instr);
189
190 void CallCodeGeneric(Handle<Code> code,
191 RelocInfo::Mode mode,
192 LInstruction* instr,
193 SafepointMode safepoint_mode);
194
195 void CallRuntime(const Runtime::Function* function,
196 int num_arguments,
197 LInstruction* instr);
198
199 void CallRuntime(Runtime::FunctionId id,
200 int num_arguments,
201 LInstruction* instr) {
202 const Runtime::Function* function = Runtime::FunctionForId(id);
203 CallRuntime(function, num_arguments, instr);
204 }
205
206 void CallRuntimeFromDeferred(Runtime::FunctionId id,
207 int argc,
208 LInstruction* instr);
209
210 // Generate a direct call to a known function. Expects the function
211 // to be in a1.
212 void CallKnownFunction(Handle<JSFunction> function,
213 int arity,
214 LInstruction* instr,
215 CallKind call_kind);
216
217 void LoadHeapObject(Register result, Handle<HeapObject> object);
218
erikcorry0ad885c2011-11-21 13:51:57 +0000219 void RecordSafepointWithLazyDeopt(LInstruction* instr,
220 SafepointMode safepoint_mode);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000221
erikcorry0ad885c2011-11-21 13:51:57 +0000222 void RegisterEnvironmentForDeoptimization(LEnvironment* environment,
223 Safepoint::DeoptMode mode);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000224 void DeoptimizeIf(Condition cc,
225 LEnvironment* environment,
226 Register src1,
227 const Operand& src2);
228
229 void AddToTranslation(Translation* translation,
230 LOperand* op,
231 bool is_tagged);
232 void PopulateDeoptimizationData(Handle<Code> code);
233 int DefineDeoptimizationLiteral(Handle<Object> literal);
234
235 void PopulateDeoptimizationLiteralsWithInlinedFunctions();
236
237 Register ToRegister(int index) const;
238 DoubleRegister ToDoubleRegister(int index) const;
239
240 // Specific math operations - used from DoUnaryMathOperation.
241 void EmitIntegerMathAbs(LUnaryMathOperation* instr);
242 void DoMathAbs(LUnaryMathOperation* instr);
243 void DoMathFloor(LUnaryMathOperation* instr);
244 void DoMathRound(LUnaryMathOperation* instr);
245 void DoMathSqrt(LUnaryMathOperation* instr);
246 void DoMathPowHalf(LUnaryMathOperation* instr);
247 void DoMathLog(LUnaryMathOperation* instr);
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +0000248 void DoMathTan(LUnaryMathOperation* instr);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000249 void DoMathCos(LUnaryMathOperation* instr);
250 void DoMathSin(LUnaryMathOperation* instr);
251
252 // Support for recording safepoint and position information.
253 void RecordSafepoint(LPointerMap* pointers,
254 Safepoint::Kind kind,
255 int arguments,
erikcorry0ad885c2011-11-21 13:51:57 +0000256 Safepoint::DeoptMode mode);
257 void RecordSafepoint(LPointerMap* pointers, Safepoint::DeoptMode mode);
258 void RecordSafepoint(Safepoint::DeoptMode mode);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000259 void RecordSafepointWithRegisters(LPointerMap* pointers,
260 int arguments,
erikcorry0ad885c2011-11-21 13:51:57 +0000261 Safepoint::DeoptMode mode);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000262 void RecordSafepointWithRegistersAndDoubles(LPointerMap* pointers,
263 int arguments,
erikcorry0ad885c2011-11-21 13:51:57 +0000264 Safepoint::DeoptMode mode);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000265 void RecordPosition(int position);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000266
267 static Condition TokenToCondition(Token::Value op, bool is_unsigned);
268 void EmitGoto(int block);
269 void EmitBranch(int left_block,
270 int right_block,
271 Condition cc,
272 Register src1,
273 const Operand& src2);
274 void EmitBranchF(int left_block,
275 int right_block,
276 Condition cc,
277 FPURegister src1,
278 FPURegister src2);
279 void EmitCmpI(LOperand* left, LOperand* right);
280 void EmitNumberUntagD(Register input,
281 DoubleRegister result,
282 bool deoptimize_on_undefined,
283 LEnvironment* env);
284
285 // Emits optimized code for typeof x == "y". Modifies input register.
286 // Returns the condition on which a final split to
287 // true and false label should be made, to optimize fallthrough.
288 // Returns two registers in cmp1 and cmp2 that can be used in the
289 // Branch instruction after EmitTypeofIs.
290 Condition EmitTypeofIs(Label* true_label,
291 Label* false_label,
292 Register input,
293 Handle<String> type_name,
294 Register& cmp1,
295 Operand& cmp2);
296
297 // Emits optimized code for %_IsObject(x). Preserves input register.
298 // Returns the condition on which a final split to
299 // true and false label should be made, to optimize fallthrough.
300 Condition EmitIsObject(Register input,
301 Register temp1,
erikcorry0ad885c2011-11-21 13:51:57 +0000302 Register temp2,
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000303 Label* is_not_object,
304 Label* is_object);
305
mstarzinger@chromium.orgf8c6bd52011-11-23 12:13:52 +0000306 // Emits optimized code for %_IsString(x). Preserves input register.
307 // Returns the condition on which a final split to
308 // true and false label should be made, to optimize fallthrough.
309 Condition EmitIsString(Register input,
310 Register temp1,
311 Label* is_not_string);
312
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000313 // Emits optimized code for %_IsConstructCall().
314 // Caller should branch on equal condition.
315 void EmitIsConstructCall(Register temp1, Register temp2);
316
317 void EmitLoadFieldOrConstantFunction(Register result,
318 Register object,
319 Handle<Map> type,
320 Handle<String> name);
321
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +0000322 // Emits optimized code to deep-copy the contents of statically known
323 // object graphs (e.g. object literal boilerplate).
324 void EmitDeepCopy(Handle<JSObject> object,
325 Register result,
326 Register source,
327 int* offset);
328
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000329 struct JumpTableEntry {
330 explicit inline JumpTableEntry(Address entry)
331 : label(),
332 address(entry) { }
333 Label label;
334 Address address;
335 };
336
erikcorry0ad885c2011-11-21 13:51:57 +0000337 void EnsureSpaceForLazyDeopt();
338
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000339 LChunk* const chunk_;
340 MacroAssembler* const masm_;
341 CompilationInfo* const info_;
342
343 int current_block_;
344 int current_instruction_;
345 const ZoneList<LInstruction*>* instructions_;
346 ZoneList<LEnvironment*> deoptimizations_;
347 ZoneList<JumpTableEntry> deopt_jump_table_;
348 ZoneList<Handle<Object> > deoptimization_literals_;
349 int inlined_function_count_;
350 Scope* const scope_;
351 Status status_;
352 TranslationBuffer translations_;
353 ZoneList<LDeferredCode*> deferred_;
354 int osr_pc_offset_;
erikcorry0ad885c2011-11-21 13:51:57 +0000355 int last_lazy_deopt_pc_;
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000356
357 // Builder that keeps track of safepoints in the code. The table
358 // itself is emitted at the end of the generated code.
359 SafepointTableBuilder safepoints_;
360
361 // Compiler from a set of parallel moves to a sequential list of moves.
362 LGapResolver resolver_;
363
364 Safepoint::Kind expected_safepoint_kind_;
365
366 class PushSafepointRegistersScope BASE_EMBEDDED {
367 public:
368 PushSafepointRegistersScope(LCodeGen* codegen,
369 Safepoint::Kind kind)
370 : codegen_(codegen) {
371 ASSERT(codegen_->expected_safepoint_kind_ == Safepoint::kSimple);
372 codegen_->expected_safepoint_kind_ = kind;
373
374 switch (codegen_->expected_safepoint_kind_) {
375 case Safepoint::kWithRegisters:
376 codegen_->masm_->PushSafepointRegisters();
377 break;
378 case Safepoint::kWithRegistersAndDoubles:
379 codegen_->masm_->PushSafepointRegistersAndDoubles();
380 break;
381 default:
382 UNREACHABLE();
383 }
384 }
385
386 ~PushSafepointRegistersScope() {
387 Safepoint::Kind kind = codegen_->expected_safepoint_kind_;
388 ASSERT((kind & Safepoint::kWithRegisters) != 0);
389 switch (kind) {
390 case Safepoint::kWithRegisters:
391 codegen_->masm_->PopSafepointRegisters();
392 break;
393 case Safepoint::kWithRegistersAndDoubles:
394 codegen_->masm_->PopSafepointRegistersAndDoubles();
395 break;
396 default:
397 UNREACHABLE();
398 }
399 codegen_->expected_safepoint_kind_ = Safepoint::kSimple;
400 }
401
402 private:
403 LCodeGen* codegen_;
404 };
405
406 friend class LDeferredCode;
407 friend class LEnvironment;
408 friend class SafepointGenerator;
409 DISALLOW_COPY_AND_ASSIGN(LCodeGen);
410};
411
412
413class LDeferredCode: public ZoneObject {
414 public:
415 explicit LDeferredCode(LCodeGen* codegen)
416 : codegen_(codegen),
417 external_exit_(NULL),
418 instruction_index_(codegen->current_instruction_) {
419 codegen->AddDeferredCode(this);
420 }
421
422 virtual ~LDeferredCode() { }
423 virtual void Generate() = 0;
424 virtual LInstruction* instr() = 0;
425
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000426 void SetExit(Label* exit) { external_exit_ = exit; }
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000427 Label* entry() { return &entry_; }
428 Label* exit() { return external_exit_ != NULL ? external_exit_ : &exit_; }
429 int instruction_index() const { return instruction_index_; }
430
431 protected:
432 LCodeGen* codegen() const { return codegen_; }
433 MacroAssembler* masm() const { return codegen_->masm(); }
434
435 private:
436 LCodeGen* codegen_;
437 Label entry_;
438 Label exit_;
439 Label* external_exit_;
440 int instruction_index_;
lrn@chromium.org7516f052011-03-30 08:52:27 +0000441};
ager@chromium.org5c838252010-02-19 08:53:10 +0000442
443} } // namespace v8::internal
444
lrn@chromium.org7516f052011-03-30 08:52:27 +0000445#endif // V8_MIPS_LITHIUM_CODEGEN_MIPS_H_