Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1 | // Copyright 2009 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_X64_CODEGEN_X64_H_ |
| 29 | #define V8_X64_CODEGEN_X64_H_ |
| 30 | |
| 31 | namespace v8 { |
| 32 | namespace internal { |
| 33 | |
| 34 | // Forward declarations |
| 35 | class DeferredCode; |
| 36 | class RegisterAllocator; |
| 37 | class RegisterFile; |
| 38 | |
| 39 | enum InitState { CONST_INIT, NOT_CONST_INIT }; |
| 40 | enum TypeofState { INSIDE_TYPEOF, NOT_INSIDE_TYPEOF }; |
| 41 | |
| 42 | |
| 43 | // ------------------------------------------------------------------------- |
| 44 | // Reference support |
| 45 | |
| 46 | // A reference is a C++ stack-allocated object that keeps an ECMA |
| 47 | // reference on the execution stack while in scope. For variables |
| 48 | // the reference is empty, indicating that it isn't necessary to |
| 49 | // store state on the stack for keeping track of references to those. |
| 50 | // For properties, we keep either one (named) or two (indexed) values |
| 51 | // on the execution stack to represent the reference. |
| 52 | |
| 53 | class Reference BASE_EMBEDDED { |
| 54 | public: |
| 55 | // The values of the types is important, see size(). |
| 56 | enum Type { ILLEGAL = -1, SLOT = 0, NAMED = 1, KEYED = 2 }; |
| 57 | Reference(CodeGenerator* cgen, Expression* expression); |
| 58 | ~Reference(); |
| 59 | |
| 60 | Expression* expression() const { return expression_; } |
| 61 | Type type() const { return type_; } |
| 62 | void set_type(Type value) { |
| 63 | ASSERT(type_ == ILLEGAL); |
| 64 | type_ = value; |
| 65 | } |
| 66 | |
| 67 | // The size the reference takes up on the stack. |
| 68 | int size() const { return (type_ == ILLEGAL) ? 0 : type_; } |
| 69 | |
| 70 | bool is_illegal() const { return type_ == ILLEGAL; } |
| 71 | bool is_slot() const { return type_ == SLOT; } |
| 72 | bool is_property() const { return type_ == NAMED || type_ == KEYED; } |
| 73 | |
| 74 | // Return the name. Only valid for named property references. |
| 75 | Handle<String> GetName(); |
| 76 | |
| 77 | // Generate code to push the value of the reference on top of the |
| 78 | // expression stack. The reference is expected to be already on top of |
| 79 | // the expression stack, and it is left in place with its value above it. |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame^] | 80 | void GetValue(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 81 | |
| 82 | // Like GetValue except that the slot is expected to be written to before |
| 83 | // being read from again. Thae value of the reference may be invalidated, |
| 84 | // causing subsequent attempts to read it to fail. |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame^] | 85 | void TakeValue(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 86 | |
| 87 | // Generate code to store the value on top of the expression stack in the |
| 88 | // reference. The reference is expected to be immediately below the value |
| 89 | // on the expression stack. The stored value is left in place (with the |
| 90 | // reference intact below it) to support chained assignments. |
| 91 | void SetValue(InitState init_state); |
| 92 | |
| 93 | private: |
| 94 | CodeGenerator* cgen_; |
| 95 | Expression* expression_; |
| 96 | Type type_; |
| 97 | }; |
| 98 | |
| 99 | |
| 100 | // ------------------------------------------------------------------------- |
| 101 | // Control destinations. |
| 102 | |
| 103 | // A control destination encapsulates a pair of jump targets and a |
| 104 | // flag indicating which one is the preferred fall-through. The |
| 105 | // preferred fall-through must be unbound, the other may be already |
| 106 | // bound (ie, a backward target). |
| 107 | // |
| 108 | // The true and false targets may be jumped to unconditionally or |
| 109 | // control may split conditionally. Unconditional jumping and |
| 110 | // splitting should be emitted in tail position (as the last thing |
| 111 | // when compiling an expression) because they can cause either label |
| 112 | // to be bound or the non-fall through to be jumped to leaving an |
| 113 | // invalid virtual frame. |
| 114 | // |
| 115 | // The labels in the control destination can be extracted and |
| 116 | // manipulated normally without affecting the state of the |
| 117 | // destination. |
| 118 | |
| 119 | class ControlDestination BASE_EMBEDDED { |
| 120 | public: |
| 121 | ControlDestination(JumpTarget* true_target, |
| 122 | JumpTarget* false_target, |
| 123 | bool true_is_fall_through) |
| 124 | : true_target_(true_target), |
| 125 | false_target_(false_target), |
| 126 | true_is_fall_through_(true_is_fall_through), |
| 127 | is_used_(false) { |
| 128 | ASSERT(true_is_fall_through ? !true_target->is_bound() |
| 129 | : !false_target->is_bound()); |
| 130 | } |
| 131 | |
| 132 | // Accessors for the jump targets. Directly jumping or branching to |
| 133 | // or binding the targets will not update the destination's state. |
| 134 | JumpTarget* true_target() const { return true_target_; } |
| 135 | JumpTarget* false_target() const { return false_target_; } |
| 136 | |
| 137 | // True if the the destination has been jumped to unconditionally or |
| 138 | // control has been split to both targets. This predicate does not |
| 139 | // test whether the targets have been extracted and manipulated as |
| 140 | // raw jump targets. |
| 141 | bool is_used() const { return is_used_; } |
| 142 | |
| 143 | // True if the destination is used and the true target (respectively |
| 144 | // false target) was the fall through. If the target is backward, |
| 145 | // "fall through" included jumping unconditionally to it. |
| 146 | bool true_was_fall_through() const { |
| 147 | return is_used_ && true_is_fall_through_; |
| 148 | } |
| 149 | |
| 150 | bool false_was_fall_through() const { |
| 151 | return is_used_ && !true_is_fall_through_; |
| 152 | } |
| 153 | |
| 154 | // Emit a branch to one of the true or false targets, and bind the |
| 155 | // other target. Because this binds the fall-through target, it |
| 156 | // should be emitted in tail position (as the last thing when |
| 157 | // compiling an expression). |
| 158 | void Split(Condition cc) { |
| 159 | ASSERT(!is_used_); |
| 160 | if (true_is_fall_through_) { |
| 161 | false_target_->Branch(NegateCondition(cc)); |
| 162 | true_target_->Bind(); |
| 163 | } else { |
| 164 | true_target_->Branch(cc); |
| 165 | false_target_->Bind(); |
| 166 | } |
| 167 | is_used_ = true; |
| 168 | } |
| 169 | |
| 170 | // Emit an unconditional jump in tail position, to the true target |
| 171 | // (if the argument is true) or the false target. The "jump" will |
| 172 | // actually bind the jump target if it is forward, jump to it if it |
| 173 | // is backward. |
| 174 | void Goto(bool where) { |
| 175 | ASSERT(!is_used_); |
| 176 | JumpTarget* target = where ? true_target_ : false_target_; |
| 177 | if (target->is_bound()) { |
| 178 | target->Jump(); |
| 179 | } else { |
| 180 | target->Bind(); |
| 181 | } |
| 182 | is_used_ = true; |
| 183 | true_is_fall_through_ = where; |
| 184 | } |
| 185 | |
| 186 | // Mark this jump target as used as if Goto had been called, but |
| 187 | // without generating a jump or binding a label (the control effect |
| 188 | // should have already happened). This is used when the left |
| 189 | // subexpression of the short-circuit boolean operators are |
| 190 | // compiled. |
| 191 | void Use(bool where) { |
| 192 | ASSERT(!is_used_); |
| 193 | ASSERT((where ? true_target_ : false_target_)->is_bound()); |
| 194 | is_used_ = true; |
| 195 | true_is_fall_through_ = where; |
| 196 | } |
| 197 | |
| 198 | // Swap the true and false targets but keep the same actual label as |
| 199 | // the fall through. This is used when compiling negated |
| 200 | // expressions, where we want to swap the targets but preserve the |
| 201 | // state. |
| 202 | void Invert() { |
| 203 | JumpTarget* temp_target = true_target_; |
| 204 | true_target_ = false_target_; |
| 205 | false_target_ = temp_target; |
| 206 | |
| 207 | true_is_fall_through_ = !true_is_fall_through_; |
| 208 | } |
| 209 | |
| 210 | private: |
| 211 | // True and false jump targets. |
| 212 | JumpTarget* true_target_; |
| 213 | JumpTarget* false_target_; |
| 214 | |
| 215 | // Before using the destination: true if the true target is the |
| 216 | // preferred fall through, false if the false target is. After |
| 217 | // using the destination: true if the true target was actually used |
| 218 | // as the fall through, false if the false target was. |
| 219 | bool true_is_fall_through_; |
| 220 | |
| 221 | // True if the Split or Goto functions have been called. |
| 222 | bool is_used_; |
| 223 | }; |
| 224 | |
| 225 | |
| 226 | // ------------------------------------------------------------------------- |
| 227 | // Code generation state |
| 228 | |
| 229 | // The state is passed down the AST by the code generator (and back up, in |
| 230 | // the form of the state of the jump target pair). It is threaded through |
| 231 | // the call stack. Constructing a state implicitly pushes it on the owning |
| 232 | // code generator's stack of states, and destroying one implicitly pops it. |
| 233 | // |
| 234 | // The code generator state is only used for expressions, so statements have |
| 235 | // the initial state. |
| 236 | |
| 237 | class CodeGenState BASE_EMBEDDED { |
| 238 | public: |
| 239 | // Create an initial code generator state. Destroying the initial state |
| 240 | // leaves the code generator with a NULL state. |
| 241 | explicit CodeGenState(CodeGenerator* owner); |
| 242 | |
| 243 | // Create a code generator state based on a code generator's current |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame^] | 244 | // state. The new state has its own control destination. |
| 245 | CodeGenState(CodeGenerator* owner, ControlDestination* destination); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 246 | |
| 247 | // Destroy a code generator state and restore the owning code generator's |
| 248 | // previous state. |
| 249 | ~CodeGenState(); |
| 250 | |
| 251 | // Accessors for the state. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 252 | ControlDestination* destination() const { return destination_; } |
| 253 | |
| 254 | private: |
| 255 | // The owning code generator. |
| 256 | CodeGenerator* owner_; |
| 257 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 258 | // A control destination in case the expression has a control-flow |
| 259 | // effect. |
| 260 | ControlDestination* destination_; |
| 261 | |
| 262 | // The previous state of the owning code generator, restored when |
| 263 | // this state is destroyed. |
| 264 | CodeGenState* previous_; |
| 265 | }; |
| 266 | |
| 267 | |
| 268 | // ------------------------------------------------------------------------- |
| 269 | // Arguments allocation mode |
| 270 | |
| 271 | enum ArgumentsAllocationMode { |
| 272 | NO_ARGUMENTS_ALLOCATION, |
| 273 | EAGER_ARGUMENTS_ALLOCATION, |
| 274 | LAZY_ARGUMENTS_ALLOCATION |
| 275 | }; |
| 276 | |
| 277 | |
| 278 | // ------------------------------------------------------------------------- |
| 279 | // CodeGenerator |
| 280 | |
| 281 | class CodeGenerator: public AstVisitor { |
| 282 | public: |
| 283 | // Takes a function literal, generates code for it. This function should only |
| 284 | // be called by compiler.cc. |
| 285 | static Handle<Code> MakeCode(FunctionLiteral* fun, |
| 286 | Handle<Script> script, |
| 287 | bool is_eval); |
| 288 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 289 | // Printing of AST, etc. as requested by flags. |
| 290 | static void MakeCodePrologue(FunctionLiteral* fun); |
| 291 | |
| 292 | // Allocate and install the code. |
| 293 | static Handle<Code> MakeCodeEpilogue(FunctionLiteral* fun, |
| 294 | MacroAssembler* masm, |
| 295 | Code::Flags flags, |
| 296 | Handle<Script> script); |
| 297 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 298 | #ifdef ENABLE_LOGGING_AND_PROFILING |
| 299 | static bool ShouldGenerateLog(Expression* type); |
| 300 | #endif |
| 301 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 302 | static void RecordPositions(MacroAssembler* masm, int pos); |
| 303 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 304 | // Accessors |
| 305 | MacroAssembler* masm() { return masm_; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 306 | VirtualFrame* frame() const { return frame_; } |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame^] | 307 | Handle<Script> script() { return script_; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 308 | |
| 309 | bool has_valid_frame() const { return frame_ != NULL; } |
| 310 | |
| 311 | // Set the virtual frame to be new_frame, with non-frame register |
| 312 | // reference counts given by non_frame_registers. The non-frame |
| 313 | // register reference counts of the old frame are returned in |
| 314 | // non_frame_registers. |
| 315 | void SetFrame(VirtualFrame* new_frame, RegisterFile* non_frame_registers); |
| 316 | |
| 317 | void DeleteFrame(); |
| 318 | |
| 319 | RegisterAllocator* allocator() const { return allocator_; } |
| 320 | |
| 321 | CodeGenState* state() { return state_; } |
| 322 | void set_state(CodeGenState* state) { state_ = state; } |
| 323 | |
| 324 | void AddDeferred(DeferredCode* code) { deferred_.Add(code); } |
| 325 | |
| 326 | bool in_spilled_code() const { return in_spilled_code_; } |
| 327 | void set_in_spilled_code(bool flag) { in_spilled_code_ = flag; } |
| 328 | |
| 329 | private: |
| 330 | // Construction/Destruction |
| 331 | CodeGenerator(int buffer_size, Handle<Script> script, bool is_eval); |
| 332 | virtual ~CodeGenerator() { delete masm_; } |
| 333 | |
| 334 | // Accessors |
| 335 | Scope* scope() const { return scope_; } |
| 336 | |
| 337 | // Generating deferred code. |
| 338 | void ProcessDeferred(); |
| 339 | |
| 340 | bool is_eval() { return is_eval_; } |
| 341 | |
| 342 | // State |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 343 | ControlDestination* destination() const { return state_->destination(); } |
| 344 | |
| 345 | // Track loop nesting level. |
| 346 | int loop_nesting() const { return loop_nesting_; } |
| 347 | void IncrementLoopNesting() { loop_nesting_++; } |
| 348 | void DecrementLoopNesting() { loop_nesting_--; } |
| 349 | |
| 350 | |
| 351 | // Node visitors. |
| 352 | void VisitStatements(ZoneList<Statement*>* statements); |
| 353 | |
| 354 | #define DEF_VISIT(type) \ |
| 355 | void Visit##type(type* node); |
| 356 | AST_NODE_LIST(DEF_VISIT) |
| 357 | #undef DEF_VISIT |
| 358 | |
| 359 | // Visit a statement and then spill the virtual frame if control flow can |
| 360 | // reach the end of the statement (ie, it does not exit via break, |
| 361 | // continue, return, or throw). This function is used temporarily while |
| 362 | // the code generator is being transformed. |
| 363 | void VisitAndSpill(Statement* statement); |
| 364 | |
| 365 | // Visit a list of statements and then spill the virtual frame if control |
| 366 | // flow can reach the end of the list. |
| 367 | void VisitStatementsAndSpill(ZoneList<Statement*>* statements); |
| 368 | |
| 369 | // Main code generation function |
| 370 | void GenCode(FunctionLiteral* fun); |
| 371 | |
| 372 | // Generate the return sequence code. Should be called no more than |
| 373 | // once per compiled function, immediately after binding the return |
| 374 | // target (which can not be done more than once). |
| 375 | void GenerateReturnSequence(Result* return_value); |
| 376 | |
| 377 | // Returns the arguments allocation mode. |
| 378 | ArgumentsAllocationMode ArgumentsMode() const; |
| 379 | |
| 380 | // Store the arguments object and allocate it if necessary. |
| 381 | Result StoreArgumentsObject(bool initial); |
| 382 | |
| 383 | // The following are used by class Reference. |
| 384 | void LoadReference(Reference* ref); |
| 385 | void UnloadReference(Reference* ref); |
| 386 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 387 | static Operand ContextOperand(Register context, int index) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 388 | return Operand(context, Context::SlotOffset(index)); |
| 389 | } |
| 390 | |
| 391 | Operand SlotOperand(Slot* slot, Register tmp); |
| 392 | |
| 393 | Operand ContextSlotOperandCheckExtensions(Slot* slot, |
| 394 | Result tmp, |
| 395 | JumpTarget* slow); |
| 396 | |
| 397 | // Expressions |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 398 | static Operand GlobalObject() { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 399 | return ContextOperand(rsi, Context::GLOBAL_INDEX); |
| 400 | } |
| 401 | |
| 402 | void LoadCondition(Expression* x, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 403 | ControlDestination* destination, |
| 404 | bool force_control); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame^] | 405 | void Load(Expression* expr); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 406 | void LoadGlobal(); |
| 407 | void LoadGlobalReceiver(); |
| 408 | |
| 409 | // Generate code to push the value of an expression on top of the frame |
| 410 | // and then spill the frame fully to memory. This function is used |
| 411 | // temporarily while the code generator is being transformed. |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame^] | 412 | void LoadAndSpill(Expression* expression); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 413 | |
| 414 | // Read a value from a slot and leave it on top of the expression stack. |
| 415 | void LoadFromSlot(Slot* slot, TypeofState typeof_state); |
| 416 | void LoadFromSlotCheckForArguments(Slot* slot, TypeofState state); |
| 417 | Result LoadFromGlobalSlotCheckExtensions(Slot* slot, |
| 418 | TypeofState typeof_state, |
| 419 | JumpTarget* slow); |
| 420 | |
| 421 | // Store the value on top of the expression stack into a slot, leaving the |
| 422 | // value in place. |
| 423 | void StoreToSlot(Slot* slot, InitState init_state); |
| 424 | |
| 425 | // Special code for typeof expressions: Unfortunately, we must |
| 426 | // be careful when loading the expression in 'typeof' |
| 427 | // expressions. We are not allowed to throw reference errors for |
| 428 | // non-existing properties of the global object, so we must make it |
| 429 | // look like an explicit property access, instead of an access |
| 430 | // through the context chain. |
| 431 | void LoadTypeofExpression(Expression* x); |
| 432 | |
| 433 | // Translate the value on top of the frame into control flow to the |
| 434 | // control destination. |
| 435 | void ToBoolean(ControlDestination* destination); |
| 436 | |
| 437 | void GenericBinaryOperation( |
| 438 | Token::Value op, |
| 439 | SmiAnalysis* type, |
| 440 | OverwriteMode overwrite_mode); |
| 441 | |
| 442 | // If possible, combine two constant smi values using op to produce |
| 443 | // a smi result, and push it on the virtual frame, all at compile time. |
| 444 | // Returns true if it succeeds. Otherwise it has no effect. |
| 445 | bool FoldConstantSmis(Token::Value op, int left, int right); |
| 446 | |
| 447 | // Emit code to perform a binary operation on a constant |
| 448 | // smi and a likely smi. Consumes the Result *operand. |
| 449 | void ConstantSmiBinaryOperation(Token::Value op, |
| 450 | Result* operand, |
| 451 | Handle<Object> constant_operand, |
| 452 | SmiAnalysis* type, |
| 453 | bool reversed, |
| 454 | OverwriteMode overwrite_mode); |
| 455 | |
| 456 | // Emit code to perform a binary operation on two likely smis. |
| 457 | // The code to handle smi arguments is produced inline. |
| 458 | // Consumes the Results *left and *right. |
| 459 | void LikelySmiBinaryOperation(Token::Value op, |
| 460 | Result* left, |
| 461 | Result* right, |
| 462 | OverwriteMode overwrite_mode); |
| 463 | |
| 464 | void Comparison(Condition cc, |
| 465 | bool strict, |
| 466 | ControlDestination* destination); |
| 467 | |
| 468 | // To prevent long attacker-controlled byte sequences, integer constants |
| 469 | // from the JavaScript source are loaded in two parts if they are larger |
| 470 | // than 16 bits. |
| 471 | static const int kMaxSmiInlinedBits = 16; |
| 472 | bool IsUnsafeSmi(Handle<Object> value); |
| 473 | // Load an integer constant x into a register target using |
| 474 | // at most 16 bits of user-controlled data per assembly operation. |
| 475 | void LoadUnsafeSmi(Register target, Handle<Object> value); |
| 476 | |
| 477 | void CallWithArguments(ZoneList<Expression*>* arguments, int position); |
| 478 | |
| 479 | // Use an optimized version of Function.prototype.apply that avoid |
| 480 | // allocating the arguments object and just copies the arguments |
| 481 | // from the stack. |
| 482 | void CallApplyLazy(Property* apply, |
| 483 | Expression* receiver, |
| 484 | VariableProxy* arguments, |
| 485 | int position); |
| 486 | |
| 487 | void CheckStack(); |
| 488 | |
| 489 | struct InlineRuntimeLUT { |
| 490 | void (CodeGenerator::*method)(ZoneList<Expression*>*); |
| 491 | const char* name; |
| 492 | }; |
| 493 | static InlineRuntimeLUT* FindInlineRuntimeLUT(Handle<String> name); |
| 494 | bool CheckForInlineRuntimeCall(CallRuntime* node); |
| 495 | static bool PatchInlineRuntimeEntry(Handle<String> name, |
| 496 | const InlineRuntimeLUT& new_entry, |
| 497 | InlineRuntimeLUT* old_entry); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 498 | void ProcessDeclarations(ZoneList<Declaration*>* declarations); |
| 499 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 500 | static Handle<Code> ComputeCallInitialize(int argc, InLoopFlag in_loop); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 501 | |
| 502 | // Declare global variables and functions in the given array of |
| 503 | // name/value pairs. |
| 504 | void DeclareGlobals(Handle<FixedArray> pairs); |
| 505 | |
| 506 | // Instantiate the function boilerplate. |
| 507 | void InstantiateBoilerplate(Handle<JSFunction> boilerplate); |
| 508 | |
| 509 | // Support for type checks. |
| 510 | void GenerateIsSmi(ZoneList<Expression*>* args); |
| 511 | void GenerateIsNonNegativeSmi(ZoneList<Expression*>* args); |
| 512 | void GenerateIsArray(ZoneList<Expression*>* args); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame^] | 513 | void GenerateIsObject(ZoneList<Expression*>* args); |
| 514 | void GenerateIsFunction(ZoneList<Expression*>* args); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 515 | |
| 516 | // Support for construct call checks. |
| 517 | void GenerateIsConstructCall(ZoneList<Expression*>* args); |
| 518 | |
| 519 | // Support for arguments.length and arguments[?]. |
| 520 | void GenerateArgumentsLength(ZoneList<Expression*>* args); |
| 521 | void GenerateArgumentsAccess(ZoneList<Expression*>* args); |
| 522 | |
| 523 | // Support for accessing the class and value fields of an object. |
| 524 | void GenerateClassOf(ZoneList<Expression*>* args); |
| 525 | void GenerateValueOf(ZoneList<Expression*>* args); |
| 526 | void GenerateSetValueOf(ZoneList<Expression*>* args); |
| 527 | |
| 528 | // Fast support for charCodeAt(n). |
| 529 | void GenerateFastCharCodeAt(ZoneList<Expression*>* args); |
| 530 | |
| 531 | // Fast support for object equality testing. |
| 532 | void GenerateObjectEquals(ZoneList<Expression*>* args); |
| 533 | |
| 534 | void GenerateLog(ZoneList<Expression*>* args); |
| 535 | |
| 536 | void GenerateGetFramePointer(ZoneList<Expression*>* args); |
| 537 | |
| 538 | // Fast support for Math.random(). |
| 539 | void GenerateRandomPositiveSmi(ZoneList<Expression*>* args); |
| 540 | |
| 541 | // Fast support for Math.sin and Math.cos. |
| 542 | enum MathOp { SIN, COS }; |
| 543 | void GenerateFastMathOp(MathOp op, ZoneList<Expression*>* args); |
| 544 | inline void GenerateMathSin(ZoneList<Expression*>* args); |
| 545 | inline void GenerateMathCos(ZoneList<Expression*>* args); |
| 546 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame^] | 547 | // Fast support for StringAdd. |
| 548 | void GenerateStringAdd(ZoneList<Expression*>* args); |
| 549 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 550 | // Simple condition analysis. |
| 551 | enum ConditionAnalysis { |
| 552 | ALWAYS_TRUE, |
| 553 | ALWAYS_FALSE, |
| 554 | DONT_KNOW |
| 555 | }; |
| 556 | ConditionAnalysis AnalyzeCondition(Expression* cond); |
| 557 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 558 | // Methods used to indicate which source code is generated for. Source |
| 559 | // positions are collected by the assembler and emitted with the relocation |
| 560 | // information. |
| 561 | void CodeForFunctionPosition(FunctionLiteral* fun); |
| 562 | void CodeForReturnPosition(FunctionLiteral* fun); |
| 563 | void CodeForStatementPosition(Statement* node); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame^] | 564 | void CodeForDoWhileConditionPosition(DoWhileStatement* stmt); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 565 | void CodeForSourcePosition(int pos); |
| 566 | |
| 567 | #ifdef DEBUG |
| 568 | // True if the registers are valid for entry to a block. There should |
| 569 | // be no frame-external references to (non-reserved) registers. |
| 570 | bool HasValidEntryRegisters(); |
| 571 | #endif |
| 572 | |
| 573 | bool is_eval_; // Tells whether code is generated for eval. |
| 574 | Handle<Script> script_; |
| 575 | ZoneList<DeferredCode*> deferred_; |
| 576 | |
| 577 | // Assembler |
| 578 | MacroAssembler* masm_; // to generate code |
| 579 | |
| 580 | // Code generation state |
| 581 | Scope* scope_; |
| 582 | VirtualFrame* frame_; |
| 583 | RegisterAllocator* allocator_; |
| 584 | CodeGenState* state_; |
| 585 | int loop_nesting_; |
| 586 | |
| 587 | // Jump targets. |
| 588 | // The target of the return from the function. |
| 589 | BreakTarget function_return_; |
| 590 | |
| 591 | // True if the function return is shadowed (ie, jumping to the target |
| 592 | // function_return_ does not jump to the true function return, but rather |
| 593 | // to some unlinking code). |
| 594 | bool function_return_is_shadowed_; |
| 595 | |
| 596 | // True when we are in code that expects the virtual frame to be fully |
| 597 | // spilled. Some virtual frame function are disabled in DEBUG builds when |
| 598 | // called from spilled code, because they do not leave the virtual frame |
| 599 | // in a spilled state. |
| 600 | bool in_spilled_code_; |
| 601 | |
| 602 | static InlineRuntimeLUT kInlineRuntimeLUT[]; |
| 603 | |
| 604 | friend class VirtualFrame; |
| 605 | friend class JumpTarget; |
| 606 | friend class Reference; |
| 607 | friend class Result; |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 608 | friend class FastCodeGenerator; |
| 609 | friend class CodeGenSelector; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 610 | |
| 611 | friend class CodeGeneratorPatcher; // Used in test-log-stack-tracer.cc |
| 612 | |
| 613 | DISALLOW_COPY_AND_ASSIGN(CodeGenerator); |
| 614 | }; |
| 615 | |
| 616 | |
| 617 | // ------------------------------------------------------------------------- |
| 618 | // Code stubs |
| 619 | // |
| 620 | // These independent code objects are created once, and used multiple |
| 621 | // times by generated code to perform common tasks, often the slow |
| 622 | // case of a JavaScript operation. They are all subclasses of CodeStub, |
| 623 | // which is declared in code-stubs.h. |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame^] | 624 | class CallFunctionStub: public CodeStub { |
| 625 | public: |
| 626 | CallFunctionStub(int argc, InLoopFlag in_loop) |
| 627 | : argc_(argc), in_loop_(in_loop) { } |
| 628 | |
| 629 | void Generate(MacroAssembler* masm); |
| 630 | |
| 631 | private: |
| 632 | int argc_; |
| 633 | InLoopFlag in_loop_; |
| 634 | |
| 635 | #ifdef DEBUG |
| 636 | void Print() { PrintF("CallFunctionStub (args %d)\n", argc_); } |
| 637 | #endif |
| 638 | |
| 639 | Major MajorKey() { return CallFunction; } |
| 640 | int MinorKey() { return argc_; } |
| 641 | InLoopFlag InLoop() { return in_loop_; } |
| 642 | }; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 643 | |
| 644 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 645 | class ToBooleanStub: public CodeStub { |
| 646 | public: |
| 647 | ToBooleanStub() { } |
| 648 | |
| 649 | void Generate(MacroAssembler* masm); |
| 650 | |
| 651 | private: |
| 652 | Major MajorKey() { return ToBoolean; } |
| 653 | int MinorKey() { return 0; } |
| 654 | }; |
| 655 | |
| 656 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame^] | 657 | // Flag that indicates how to generate code for the stub GenericBinaryOpStub. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 658 | enum GenericBinaryFlags { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame^] | 659 | NO_GENERIC_BINARY_FLAGS = 0, |
| 660 | NO_SMI_CODE_IN_STUB = 1 << 0 // Omit smi code in stub. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 661 | }; |
| 662 | |
| 663 | |
| 664 | class GenericBinaryOpStub: public CodeStub { |
| 665 | public: |
| 666 | GenericBinaryOpStub(Token::Value op, |
| 667 | OverwriteMode mode, |
| 668 | GenericBinaryFlags flags) |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame^] | 669 | : op_(op), |
| 670 | mode_(mode), |
| 671 | flags_(flags), |
| 672 | args_in_registers_(false), |
| 673 | args_reversed_(false) { |
| 674 | use_sse3_ = CpuFeatures::IsSupported(SSE3); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 675 | ASSERT(OpBits::is_valid(Token::NUM_TOKENS)); |
| 676 | } |
| 677 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame^] | 678 | // Generate code to call the stub with the supplied arguments. This will add |
| 679 | // code at the call site to prepare arguments either in registers or on the |
| 680 | // stack together with the actual call. |
| 681 | void GenerateCall(MacroAssembler* masm, Register left, Register right); |
| 682 | void GenerateCall(MacroAssembler* masm, Register left, Smi* right); |
| 683 | void GenerateCall(MacroAssembler* masm, Smi* left, Register right); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 684 | |
| 685 | private: |
| 686 | Token::Value op_; |
| 687 | OverwriteMode mode_; |
| 688 | GenericBinaryFlags flags_; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame^] | 689 | bool args_in_registers_; // Arguments passed in registers not on the stack. |
| 690 | bool args_reversed_; // Left and right argument are swapped. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 691 | bool use_sse3_; |
| 692 | |
| 693 | const char* GetName(); |
| 694 | |
| 695 | #ifdef DEBUG |
| 696 | void Print() { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame^] | 697 | PrintF("GenericBinaryOpStub (op %s), " |
| 698 | "(mode %d, flags %d, registers %d, reversed %d)\n", |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 699 | Token::String(op_), |
| 700 | static_cast<int>(mode_), |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame^] | 701 | static_cast<int>(flags_), |
| 702 | static_cast<int>(args_in_registers_), |
| 703 | static_cast<int>(args_reversed_)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 704 | } |
| 705 | #endif |
| 706 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame^] | 707 | // Minor key encoding in 16 bits FRASOOOOOOOOOOMM. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 708 | class ModeBits: public BitField<OverwriteMode, 0, 2> {}; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame^] | 709 | class OpBits: public BitField<Token::Value, 2, 10> {}; |
| 710 | class SSE3Bits: public BitField<bool, 12, 1> {}; |
| 711 | class ArgsInRegistersBits: public BitField<bool, 13, 1> {}; |
| 712 | class ArgsReversedBits: public BitField<bool, 14, 1> {}; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 713 | class FlagBits: public BitField<GenericBinaryFlags, 15, 1> {}; |
| 714 | |
| 715 | Major MajorKey() { return GenericBinaryOp; } |
| 716 | int MinorKey() { |
| 717 | // Encode the parameters in a unique 16 bit value. |
| 718 | return OpBits::encode(op_) |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame^] | 719 | | ModeBits::encode(mode_) |
| 720 | | FlagBits::encode(flags_) |
| 721 | | SSE3Bits::encode(use_sse3_) |
| 722 | | ArgsInRegistersBits::encode(args_in_registers_) |
| 723 | | ArgsReversedBits::encode(args_reversed_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 724 | } |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame^] | 725 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 726 | void Generate(MacroAssembler* masm); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame^] | 727 | void GenerateSmiCode(MacroAssembler* masm, Label* slow); |
| 728 | void GenerateLoadArguments(MacroAssembler* masm); |
| 729 | void GenerateReturn(MacroAssembler* masm); |
| 730 | |
| 731 | bool ArgsInRegistersSupported() { |
| 732 | return ((op_ == Token::ADD) || (op_ == Token::SUB) |
| 733 | || (op_ == Token::MUL) || (op_ == Token::DIV)) |
| 734 | && flags_ != NO_SMI_CODE_IN_STUB; |
| 735 | } |
| 736 | bool IsOperationCommutative() { |
| 737 | return (op_ == Token::ADD) || (op_ == Token::MUL); |
| 738 | } |
| 739 | |
| 740 | void SetArgsInRegisters() { args_in_registers_ = true; } |
| 741 | void SetArgsReversed() { args_reversed_ = true; } |
| 742 | bool HasSmiCodeInStub() { return (flags_ & NO_SMI_CODE_IN_STUB) == 0; } |
| 743 | bool HasArgumentsInRegisters() { return args_in_registers_; } |
| 744 | bool HasArgumentsReversed() { return args_reversed_; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 745 | }; |
| 746 | |
| 747 | |
| 748 | } } // namespace v8::internal |
| 749 | |
| 750 | #endif // V8_X64_CODEGEN_X64_H_ |