blob: 513992c67aa5ffe681f28b864fd7a93d0b2a280a [file] [log] [blame]
jkummerow@chromium.org05ed9dd2012-01-23 14:42:48 +00001// Copyright 2012 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
jkummerow@chromium.org05ed9dd2012-01-23 14:42:48 +0000118 void DoCheckMapCommon(Register reg, Register scratch, Handle<Map> map,
119 CompareMapMode mode, LEnvironment* env);
120
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000121 // Parallel move support.
122 void DoParallelMove(LParallelMove* move);
123 void DoGap(LGap* instr);
124
125 // Emit frame translation commands for an environment.
126 void WriteTranslation(LEnvironment* environment, Translation* translation);
127
128 // Declare methods that deal with the individual node types.
129#define DECLARE_DO(type) void Do##type(L##type* node);
130 LITHIUM_CONCRETE_INSTRUCTION_LIST(DECLARE_DO)
131#undef DECLARE_DO
132
133 private:
134 enum Status {
135 UNUSED,
136 GENERATING,
137 DONE,
138 ABORTED
139 };
140
141 bool is_unused() const { return status_ == UNUSED; }
142 bool is_generating() const { return status_ == GENERATING; }
143 bool is_done() const { return status_ == DONE; }
144 bool is_aborted() const { return status_ == ABORTED; }
145
146 StrictModeFlag strict_mode_flag() const {
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000147 return info()->is_classic_mode() ? kNonStrictMode : kStrictMode;
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000148 }
149
150 LChunk* chunk() const { return chunk_; }
151 Scope* scope() const { return scope_; }
152 HGraph* graph() const { return chunk_->graph(); }
153
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000154 Register scratch0() { return kLithiumScratchReg; }
155 Register scratch1() { return kLithiumScratchReg2; }
156 DoubleRegister double_scratch0() { return kLithiumScratchDouble; }
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000157
158 int GetNextEmittedBlock(int block);
159 LInstruction* GetNextInstruction();
160
161 void EmitClassOfTest(Label* if_true,
162 Label* if_false,
163 Handle<String> class_name,
164 Register input,
165 Register temporary,
166 Register temporary2);
167
168 int GetStackSlotCount() const { return chunk()->spill_slot_count(); }
169 int GetParameterCount() const { return scope()->num_parameters(); }
170
171 void Abort(const char* format, ...);
172 void Comment(const char* format, ...);
173
174 void AddDeferredCode(LDeferredCode* code) { deferred_.Add(code); }
175
176 // Code generation passes. Returns true if code generation should
177 // continue.
178 bool GeneratePrologue();
179 bool GenerateBody();
180 bool GenerateDeferredCode();
181 bool GenerateDeoptJumpTable();
182 bool GenerateSafepointTable();
183
184 enum SafepointMode {
185 RECORD_SIMPLE_SAFEPOINT,
186 RECORD_SAFEPOINT_WITH_REGISTERS_AND_NO_ARGUMENTS
187 };
188
189 void CallCode(Handle<Code> code,
190 RelocInfo::Mode mode,
191 LInstruction* instr);
192
193 void CallCodeGeneric(Handle<Code> code,
194 RelocInfo::Mode mode,
195 LInstruction* instr,
196 SafepointMode safepoint_mode);
197
198 void CallRuntime(const Runtime::Function* function,
199 int num_arguments,
200 LInstruction* instr);
201
202 void CallRuntime(Runtime::FunctionId id,
203 int num_arguments,
204 LInstruction* instr) {
205 const Runtime::Function* function = Runtime::FunctionForId(id);
206 CallRuntime(function, num_arguments, instr);
207 }
208
209 void CallRuntimeFromDeferred(Runtime::FunctionId id,
210 int argc,
211 LInstruction* instr);
212
213 // Generate a direct call to a known function. Expects the function
214 // to be in a1.
215 void CallKnownFunction(Handle<JSFunction> function,
216 int arity,
217 LInstruction* instr,
218 CallKind call_kind);
219
220 void LoadHeapObject(Register result, Handle<HeapObject> object);
221
erikcorry0ad885c2011-11-21 13:51:57 +0000222 void RecordSafepointWithLazyDeopt(LInstruction* instr,
223 SafepointMode safepoint_mode);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000224
erikcorry0ad885c2011-11-21 13:51:57 +0000225 void RegisterEnvironmentForDeoptimization(LEnvironment* environment,
226 Safepoint::DeoptMode mode);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000227 void DeoptimizeIf(Condition cc,
228 LEnvironment* environment,
jkummerow@chromium.org05ed9dd2012-01-23 14:42:48 +0000229 Register src1 = zero_reg,
230 const Operand& src2 = Operand(zero_reg));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000231
232 void AddToTranslation(Translation* translation,
233 LOperand* op,
234 bool is_tagged);
235 void PopulateDeoptimizationData(Handle<Code> code);
236 int DefineDeoptimizationLiteral(Handle<Object> literal);
237
238 void PopulateDeoptimizationLiteralsWithInlinedFunctions();
239
240 Register ToRegister(int index) const;
241 DoubleRegister ToDoubleRegister(int index) const;
242
243 // Specific math operations - used from DoUnaryMathOperation.
244 void EmitIntegerMathAbs(LUnaryMathOperation* instr);
245 void DoMathAbs(LUnaryMathOperation* instr);
246 void DoMathFloor(LUnaryMathOperation* instr);
247 void DoMathRound(LUnaryMathOperation* instr);
248 void DoMathSqrt(LUnaryMathOperation* instr);
249 void DoMathPowHalf(LUnaryMathOperation* instr);
250 void DoMathLog(LUnaryMathOperation* instr);
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +0000251 void DoMathTan(LUnaryMathOperation* instr);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000252 void DoMathCos(LUnaryMathOperation* instr);
253 void DoMathSin(LUnaryMathOperation* instr);
254
255 // Support for recording safepoint and position information.
256 void RecordSafepoint(LPointerMap* pointers,
257 Safepoint::Kind kind,
258 int arguments,
erikcorry0ad885c2011-11-21 13:51:57 +0000259 Safepoint::DeoptMode mode);
260 void RecordSafepoint(LPointerMap* pointers, Safepoint::DeoptMode mode);
261 void RecordSafepoint(Safepoint::DeoptMode mode);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000262 void RecordSafepointWithRegisters(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 RecordSafepointWithRegistersAndDoubles(LPointerMap* pointers,
266 int arguments,
erikcorry0ad885c2011-11-21 13:51:57 +0000267 Safepoint::DeoptMode mode);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000268 void RecordPosition(int position);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000269
270 static Condition TokenToCondition(Token::Value op, bool is_unsigned);
271 void EmitGoto(int block);
272 void EmitBranch(int left_block,
273 int right_block,
274 Condition cc,
275 Register src1,
276 const Operand& src2);
277 void EmitBranchF(int left_block,
278 int right_block,
279 Condition cc,
280 FPURegister src1,
281 FPURegister src2);
282 void EmitCmpI(LOperand* left, LOperand* right);
283 void EmitNumberUntagD(Register input,
284 DoubleRegister result,
285 bool deoptimize_on_undefined,
ulan@chromium.org2efb9002012-01-19 15:36:35 +0000286 bool deoptimize_on_minus_zero,
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000287 LEnvironment* env);
288
289 // Emits optimized code for typeof x == "y". Modifies input register.
290 // Returns the condition on which a final split to
291 // true and false label should be made, to optimize fallthrough.
292 // Returns two registers in cmp1 and cmp2 that can be used in the
293 // Branch instruction after EmitTypeofIs.
294 Condition EmitTypeofIs(Label* true_label,
295 Label* false_label,
296 Register input,
297 Handle<String> type_name,
298 Register& cmp1,
299 Operand& cmp2);
300
301 // Emits optimized code for %_IsObject(x). Preserves input register.
302 // Returns the condition on which a final split to
303 // true and false label should be made, to optimize fallthrough.
304 Condition EmitIsObject(Register input,
305 Register temp1,
erikcorry0ad885c2011-11-21 13:51:57 +0000306 Register temp2,
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000307 Label* is_not_object,
308 Label* is_object);
309
mstarzinger@chromium.orgf8c6bd52011-11-23 12:13:52 +0000310 // Emits optimized code for %_IsString(x). Preserves input register.
311 // Returns the condition on which a final split to
312 // true and false label should be made, to optimize fallthrough.
313 Condition EmitIsString(Register input,
314 Register temp1,
315 Label* is_not_string);
316
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000317 // Emits optimized code for %_IsConstructCall().
318 // Caller should branch on equal condition.
319 void EmitIsConstructCall(Register temp1, Register temp2);
320
321 void EmitLoadFieldOrConstantFunction(Register result,
322 Register object,
323 Handle<Map> type,
324 Handle<String> name);
325
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +0000326 // Emits optimized code to deep-copy the contents of statically known
327 // object graphs (e.g. object literal boilerplate).
328 void EmitDeepCopy(Handle<JSObject> object,
329 Register result,
330 Register source,
331 int* offset);
332
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000333 struct JumpTableEntry {
334 explicit inline JumpTableEntry(Address entry)
335 : label(),
336 address(entry) { }
337 Label label;
338 Address address;
339 };
340
erikcorry0ad885c2011-11-21 13:51:57 +0000341 void EnsureSpaceForLazyDeopt();
342
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000343 LChunk* const chunk_;
344 MacroAssembler* const masm_;
345 CompilationInfo* const info_;
346
347 int current_block_;
348 int current_instruction_;
349 const ZoneList<LInstruction*>* instructions_;
350 ZoneList<LEnvironment*> deoptimizations_;
351 ZoneList<JumpTableEntry> deopt_jump_table_;
352 ZoneList<Handle<Object> > deoptimization_literals_;
353 int inlined_function_count_;
354 Scope* const scope_;
355 Status status_;
356 TranslationBuffer translations_;
357 ZoneList<LDeferredCode*> deferred_;
358 int osr_pc_offset_;
erikcorry0ad885c2011-11-21 13:51:57 +0000359 int last_lazy_deopt_pc_;
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000360
361 // Builder that keeps track of safepoints in the code. The table
362 // itself is emitted at the end of the generated code.
363 SafepointTableBuilder safepoints_;
364
365 // Compiler from a set of parallel moves to a sequential list of moves.
366 LGapResolver resolver_;
367
368 Safepoint::Kind expected_safepoint_kind_;
369
370 class PushSafepointRegistersScope BASE_EMBEDDED {
371 public:
372 PushSafepointRegistersScope(LCodeGen* codegen,
373 Safepoint::Kind kind)
374 : codegen_(codegen) {
375 ASSERT(codegen_->expected_safepoint_kind_ == Safepoint::kSimple);
376 codegen_->expected_safepoint_kind_ = kind;
377
378 switch (codegen_->expected_safepoint_kind_) {
379 case Safepoint::kWithRegisters:
380 codegen_->masm_->PushSafepointRegisters();
381 break;
382 case Safepoint::kWithRegistersAndDoubles:
383 codegen_->masm_->PushSafepointRegistersAndDoubles();
384 break;
385 default:
386 UNREACHABLE();
387 }
388 }
389
390 ~PushSafepointRegistersScope() {
391 Safepoint::Kind kind = codegen_->expected_safepoint_kind_;
392 ASSERT((kind & Safepoint::kWithRegisters) != 0);
393 switch (kind) {
394 case Safepoint::kWithRegisters:
395 codegen_->masm_->PopSafepointRegisters();
396 break;
397 case Safepoint::kWithRegistersAndDoubles:
398 codegen_->masm_->PopSafepointRegistersAndDoubles();
399 break;
400 default:
401 UNREACHABLE();
402 }
403 codegen_->expected_safepoint_kind_ = Safepoint::kSimple;
404 }
405
406 private:
407 LCodeGen* codegen_;
408 };
409
410 friend class LDeferredCode;
411 friend class LEnvironment;
412 friend class SafepointGenerator;
413 DISALLOW_COPY_AND_ASSIGN(LCodeGen);
414};
415
416
417class LDeferredCode: public ZoneObject {
418 public:
419 explicit LDeferredCode(LCodeGen* codegen)
420 : codegen_(codegen),
421 external_exit_(NULL),
422 instruction_index_(codegen->current_instruction_) {
423 codegen->AddDeferredCode(this);
424 }
425
426 virtual ~LDeferredCode() { }
427 virtual void Generate() = 0;
428 virtual LInstruction* instr() = 0;
429
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000430 void SetExit(Label* exit) { external_exit_ = exit; }
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000431 Label* entry() { return &entry_; }
432 Label* exit() { return external_exit_ != NULL ? external_exit_ : &exit_; }
433 int instruction_index() const { return instruction_index_; }
434
435 protected:
436 LCodeGen* codegen() const { return codegen_; }
437 MacroAssembler* masm() const { return codegen_->masm(); }
438
439 private:
440 LCodeGen* codegen_;
441 Label entry_;
442 Label exit_;
443 Label* external_exit_;
444 int instruction_index_;
lrn@chromium.org7516f052011-03-30 08:52:27 +0000445};
ager@chromium.org5c838252010-02-19 08:53:10 +0000446
447} } // namespace v8::internal
448
lrn@chromium.org7516f052011-03-30 08:52:27 +0000449#endif // V8_MIPS_LITHIUM_CODEGEN_MIPS_H_