lithium-codegen-ia32.h revision 69a99ed0b2b2ef69d393c371b03db3a98aaf880e
1// Copyright 2011 the V8 project authors. All rights reserved. 2// 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_IA32_LITHIUM_CODEGEN_IA32_H_ 29#define V8_IA32_LITHIUM_CODEGEN_IA32_H_ 30 31#include "ia32/lithium-ia32.h" 32 33#include "checks.h" 34#include "deoptimizer.h" 35#include "safepoint-table.h" 36#include "scopes.h" 37#include "ia32/lithium-gap-resolver-ia32.h" 38 39namespace v8 { 40namespace internal { 41 42// Forward declarations. 43class LDeferredCode; 44class LGapNode; 45class SafepointGenerator; 46 47class LCodeGen BASE_EMBEDDED { 48 public: 49 LCodeGen(LChunk* chunk, MacroAssembler* assembler, CompilationInfo* info) 50 : chunk_(chunk), 51 masm_(assembler), 52 info_(info), 53 current_block_(-1), 54 current_instruction_(-1), 55 instructions_(chunk->instructions()), 56 deoptimizations_(4), 57 deoptimization_literals_(8), 58 inlined_function_count_(0), 59 scope_(info->scope()), 60 status_(UNUSED), 61 deferred_(8), 62 osr_pc_offset_(-1), 63 deoptimization_reloc_size(), 64 resolver_(this), 65 expected_safepoint_kind_(Safepoint::kSimple) { 66 PopulateDeoptimizationLiteralsWithInlinedFunctions(); 67 } 68 69 // Simple accessors. 70 MacroAssembler* masm() const { return masm_; } 71 CompilationInfo* info() const { return info_; } 72 Isolate* isolate() const { return info_->isolate(); } 73 Factory* factory() const { return isolate()->factory(); } 74 Heap* heap() const { return isolate()->heap(); } 75 76 // Support for converting LOperands to assembler types. 77 Operand ToOperand(LOperand* op) const; 78 Register ToRegister(LOperand* op) const; 79 XMMRegister ToDoubleRegister(LOperand* op) const; 80 Immediate ToImmediate(LOperand* op); 81 82 // The operand denoting the second word (the one with a higher address) of 83 // a double stack slot. 84 Operand HighOperand(LOperand* op); 85 86 // Try to generate code for the entire chunk, but it may fail if the 87 // chunk contains constructs we cannot handle. Returns true if the 88 // code generation attempt succeeded. 89 bool GenerateCode(); 90 91 // Finish the code by setting stack height, safepoint, and bailout 92 // information on it. 93 void FinishCode(Handle<Code> code); 94 95 // Deferred code support. 96 void DoDeferredNumberTagD(LNumberTagD* instr); 97 void DoDeferredNumberTagI(LNumberTagI* instr); 98 void DoDeferredTaggedToI(LTaggedToI* instr); 99 void DoDeferredMathAbsTaggedHeapNumber(LUnaryMathOperation* instr); 100 void DoDeferredStackCheck(LStackCheck* instr); 101 void DoDeferredStringCharCodeAt(LStringCharCodeAt* instr); 102 void DoDeferredStringCharFromCode(LStringCharFromCode* instr); 103 void DoDeferredLInstanceOfKnownGlobal(LInstanceOfKnownGlobal* instr, 104 Label* map_check); 105 106 // Parallel move support. 107 void DoParallelMove(LParallelMove* move); 108 void DoGap(LGap* instr); 109 110 // Emit frame translation commands for an environment. 111 void WriteTranslation(LEnvironment* environment, Translation* translation); 112 113 void EnsureRelocSpaceForDeoptimization(); 114 115 // Declare methods that deal with the individual node types. 116#define DECLARE_DO(type) void Do##type(L##type* node); 117 LITHIUM_CONCRETE_INSTRUCTION_LIST(DECLARE_DO) 118#undef DECLARE_DO 119 120 private: 121 enum Status { 122 UNUSED, 123 GENERATING, 124 DONE, 125 ABORTED 126 }; 127 128 bool is_unused() const { return status_ == UNUSED; } 129 bool is_generating() const { return status_ == GENERATING; } 130 bool is_done() const { return status_ == DONE; } 131 bool is_aborted() const { return status_ == ABORTED; } 132 133 int strict_mode_flag() const { 134 return info()->is_strict_mode() ? kStrictMode : kNonStrictMode; 135 } 136 137 LChunk* chunk() const { return chunk_; } 138 Scope* scope() const { return scope_; } 139 HGraph* graph() const { return chunk_->graph(); } 140 141 int GetNextEmittedBlock(int block); 142 LInstruction* GetNextInstruction(); 143 144 void EmitClassOfTest(Label* if_true, 145 Label* if_false, 146 Handle<String> class_name, 147 Register input, 148 Register temporary, 149 Register temporary2); 150 151 int GetStackSlotCount() const { return chunk()->spill_slot_count(); } 152 int GetParameterCount() const { return scope()->num_parameters(); } 153 154 void Abort(const char* format, ...); 155 void Comment(const char* format, ...); 156 157 void AddDeferredCode(LDeferredCode* code) { deferred_.Add(code); } 158 159 // Code generation passes. Returns true if code generation should 160 // continue. 161 bool GeneratePrologue(); 162 bool GenerateBody(); 163 bool GenerateDeferredCode(); 164 // Pad the reloc info to ensure that we have enough space to patch during 165 // deoptimization. 166 bool GenerateRelocPadding(); 167 bool GenerateSafepointTable(); 168 169 enum SafepointMode { 170 RECORD_SIMPLE_SAFEPOINT, 171 RECORD_SAFEPOINT_WITH_REGISTERS_AND_NO_ARGUMENTS 172 }; 173 174 void CallCode(Handle<Code> code, 175 RelocInfo::Mode mode, 176 LInstruction* instr); 177 178 void CallCodeGeneric(Handle<Code> code, 179 RelocInfo::Mode mode, 180 LInstruction* instr, 181 SafepointMode safepoint_mode); 182 183 void CallRuntime(const Runtime::Function* fun, 184 int argc, 185 LInstruction* instr); 186 187 void CallRuntime(Runtime::FunctionId id, 188 int argc, 189 LInstruction* instr) { 190 const Runtime::Function* function = Runtime::FunctionForId(id); 191 CallRuntime(function, argc, instr); 192 } 193 194 void CallRuntimeFromDeferred(Runtime::FunctionId id, 195 int argc, 196 LInstruction* instr, 197 LOperand* context); 198 199 // Generate a direct call to a known function. Expects the function 200 // to be in edi. 201 void CallKnownFunction(Handle<JSFunction> function, 202 int arity, 203 LInstruction* instr, 204 CallKind call_kind); 205 206 void LoadHeapObject(Register result, Handle<HeapObject> object); 207 208 void RegisterLazyDeoptimization(LInstruction* instr, 209 SafepointMode safepoint_mode); 210 211 void RegisterEnvironmentForDeoptimization(LEnvironment* environment); 212 void DeoptimizeIf(Condition cc, LEnvironment* environment); 213 214 void AddToTranslation(Translation* translation, 215 LOperand* op, 216 bool is_tagged); 217 void PopulateDeoptimizationData(Handle<Code> code); 218 int DefineDeoptimizationLiteral(Handle<Object> literal); 219 220 void PopulateDeoptimizationLiteralsWithInlinedFunctions(); 221 222 Register ToRegister(int index) const; 223 XMMRegister ToDoubleRegister(int index) const; 224 int ToInteger32(LConstantOperand* op) const; 225 Operand BuildFastArrayOperand(LOperand* elements_pointer, 226 LOperand* key, 227 JSObject::ElementsKind elements_kind, 228 uint32_t offset); 229 230 // Specific math operations - used from DoUnaryMathOperation. 231 void EmitIntegerMathAbs(LUnaryMathOperation* instr); 232 void DoMathAbs(LUnaryMathOperation* instr); 233 void DoMathFloor(LUnaryMathOperation* instr); 234 void DoMathRound(LUnaryMathOperation* instr); 235 void DoMathSqrt(LUnaryMathOperation* instr); 236 void DoMathPowHalf(LUnaryMathOperation* instr); 237 void DoMathLog(LUnaryMathOperation* instr); 238 void DoMathCos(LUnaryMathOperation* instr); 239 void DoMathSin(LUnaryMathOperation* instr); 240 241 // Support for recording safepoint and position information. 242 void RecordSafepoint(LPointerMap* pointers, 243 Safepoint::Kind kind, 244 int arguments, 245 int deoptimization_index); 246 void RecordSafepoint(LPointerMap* pointers, int deoptimization_index); 247 void RecordSafepoint(int deoptimization_index); 248 void RecordSafepointWithRegisters(LPointerMap* pointers, 249 int arguments, 250 int deoptimization_index); 251 void RecordPosition(int position); 252 int LastSafepointEnd() { 253 return static_cast<int>(safepoints_.GetPcAfterGap()); 254 } 255 256 static Condition TokenToCondition(Token::Value op, bool is_unsigned); 257 void EmitGoto(int block); 258 void EmitBranch(int left_block, int right_block, Condition cc); 259 void EmitCmpI(LOperand* left, LOperand* right); 260 void EmitNumberUntagD(Register input, 261 XMMRegister result, 262 bool deoptimize_on_undefined, 263 LEnvironment* env); 264 265 // Emits optimized code for typeof x == "y". Modifies input register. 266 // Returns the condition on which a final split to 267 // true and false label should be made, to optimize fallthrough. 268 Condition EmitTypeofIs(Label* true_label, Label* false_label, 269 Register input, Handle<String> type_name); 270 271 // Emits optimized code for %_IsObject(x). Preserves input register. 272 // Returns the condition on which a final split to 273 // true and false label should be made, to optimize fallthrough. 274 Condition EmitIsObject(Register input, 275 Register temp1, 276 Label* is_not_object, 277 Label* is_object); 278 279 // Emits optimized code for %_IsConstructCall(). 280 // Caller should branch on equal condition. 281 void EmitIsConstructCall(Register temp); 282 283 void EmitLoadFieldOrConstantFunction(Register result, 284 Register object, 285 Handle<Map> type, 286 Handle<String> name); 287 288 LChunk* const chunk_; 289 MacroAssembler* const masm_; 290 CompilationInfo* const info_; 291 292 int current_block_; 293 int current_instruction_; 294 const ZoneList<LInstruction*>* instructions_; 295 ZoneList<LEnvironment*> deoptimizations_; 296 ZoneList<Handle<Object> > deoptimization_literals_; 297 int inlined_function_count_; 298 Scope* const scope_; 299 Status status_; 300 TranslationBuffer translations_; 301 ZoneList<LDeferredCode*> deferred_; 302 int osr_pc_offset_; 303 304 struct DeoptimizationRelocSize { 305 int min_size; 306 int last_pc_offset; 307 }; 308 309 DeoptimizationRelocSize deoptimization_reloc_size; 310 311 // Builder that keeps track of safepoints in the code. The table 312 // itself is emitted at the end of the generated code. 313 SafepointTableBuilder safepoints_; 314 315 // Compiler from a set of parallel moves to a sequential list of moves. 316 LGapResolver resolver_; 317 318 Safepoint::Kind expected_safepoint_kind_; 319 320 class PushSafepointRegistersScope BASE_EMBEDDED { 321 public: 322 explicit PushSafepointRegistersScope(LCodeGen* codegen) 323 : codegen_(codegen) { 324 ASSERT(codegen_->expected_safepoint_kind_ == Safepoint::kSimple); 325 codegen_->masm_->PushSafepointRegisters(); 326 codegen_->expected_safepoint_kind_ = Safepoint::kWithRegisters; 327 } 328 329 ~PushSafepointRegistersScope() { 330 ASSERT(codegen_->expected_safepoint_kind_ == Safepoint::kWithRegisters); 331 codegen_->masm_->PopSafepointRegisters(); 332 codegen_->expected_safepoint_kind_ = Safepoint::kSimple; 333 } 334 335 private: 336 LCodeGen* codegen_; 337 }; 338 339 friend class LDeferredCode; 340 friend class LEnvironment; 341 friend class SafepointGenerator; 342 DISALLOW_COPY_AND_ASSIGN(LCodeGen); 343}; 344 345 346class LDeferredCode: public ZoneObject { 347 public: 348 explicit LDeferredCode(LCodeGen* codegen) 349 : codegen_(codegen), external_exit_(NULL) { 350 codegen->AddDeferredCode(this); 351 } 352 353 virtual ~LDeferredCode() { } 354 virtual void Generate() = 0; 355 356 void SetExit(Label *exit) { external_exit_ = exit; } 357 Label* entry() { return &entry_; } 358 Label* exit() { return external_exit_ != NULL ? external_exit_ : &exit_; } 359 360 protected: 361 LCodeGen* codegen() const { return codegen_; } 362 MacroAssembler* masm() const { return codegen_->masm(); } 363 364 private: 365 LCodeGen* codegen_; 366 Label entry_; 367 Label exit_; 368 Label* external_exit_; 369}; 370 371} } // namespace v8::internal 372 373#endif // V8_IA32_LITHIUM_CODEGEN_IA32_H_ 374