Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +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_FULL_CODEGEN_H_ |
| 29 | #define V8_FULL_CODEGEN_H_ |
| 30 | |
| 31 | #include "v8.h" |
| 32 | |
| 33 | #include "ast.h" |
| 34 | |
| 35 | namespace v8 { |
| 36 | namespace internal { |
| 37 | |
| 38 | class FullCodeGenSyntaxChecker: public AstVisitor { |
| 39 | public: |
| 40 | FullCodeGenSyntaxChecker() : has_supported_syntax_(true) {} |
| 41 | |
| 42 | void Check(FunctionLiteral* fun); |
| 43 | |
| 44 | bool has_supported_syntax() { return has_supported_syntax_; } |
| 45 | |
| 46 | private: |
| 47 | void VisitDeclarations(ZoneList<Declaration*>* decls); |
| 48 | void VisitStatements(ZoneList<Statement*>* stmts); |
| 49 | |
| 50 | // AST node visit functions. |
| 51 | #define DECLARE_VISIT(type) virtual void Visit##type(type* node); |
| 52 | AST_NODE_LIST(DECLARE_VISIT) |
| 53 | #undef DECLARE_VISIT |
| 54 | |
| 55 | bool has_supported_syntax_; |
| 56 | |
| 57 | DISALLOW_COPY_AND_ASSIGN(FullCodeGenSyntaxChecker); |
| 58 | }; |
| 59 | |
| 60 | |
| 61 | // ----------------------------------------------------------------------------- |
| 62 | // Full code generator. |
| 63 | |
| 64 | class FullCodeGenerator: public AstVisitor { |
| 65 | public: |
| 66 | FullCodeGenerator(MacroAssembler* masm, Handle<Script> script, bool is_eval) |
| 67 | : masm_(masm), |
| 68 | function_(NULL), |
| 69 | script_(script), |
| 70 | is_eval_(is_eval), |
| 71 | nesting_stack_(NULL), |
| 72 | loop_depth_(0), |
| 73 | location_(kStack), |
| 74 | true_label_(NULL), |
| 75 | false_label_(NULL) { |
| 76 | } |
| 77 | |
| 78 | static Handle<Code> MakeCode(FunctionLiteral* fun, |
| 79 | Handle<Script> script, |
| 80 | bool is_eval); |
| 81 | |
| 82 | void Generate(FunctionLiteral* fun); |
| 83 | |
| 84 | private: |
| 85 | class Breakable; |
| 86 | class Iteration; |
| 87 | class TryCatch; |
| 88 | class TryFinally; |
| 89 | class Finally; |
| 90 | class ForIn; |
| 91 | |
| 92 | class NestedStatement BASE_EMBEDDED { |
| 93 | public: |
| 94 | explicit NestedStatement(FullCodeGenerator* codegen) : codegen_(codegen) { |
| 95 | // Link into codegen's nesting stack. |
| 96 | previous_ = codegen->nesting_stack_; |
| 97 | codegen->nesting_stack_ = this; |
| 98 | } |
| 99 | virtual ~NestedStatement() { |
| 100 | // Unlink from codegen's nesting stack. |
| 101 | ASSERT_EQ(this, codegen_->nesting_stack_); |
| 102 | codegen_->nesting_stack_ = previous_; |
| 103 | } |
| 104 | |
| 105 | virtual Breakable* AsBreakable() { return NULL; } |
| 106 | virtual Iteration* AsIteration() { return NULL; } |
| 107 | virtual TryCatch* AsTryCatch() { return NULL; } |
| 108 | virtual TryFinally* AsTryFinally() { return NULL; } |
| 109 | virtual Finally* AsFinally() { return NULL; } |
| 110 | virtual ForIn* AsForIn() { return NULL; } |
| 111 | |
| 112 | virtual bool IsContinueTarget(Statement* target) { return false; } |
| 113 | virtual bool IsBreakTarget(Statement* target) { return false; } |
| 114 | |
| 115 | // Generate code to leave the nested statement. This includes |
| 116 | // cleaning up any stack elements in use and restoring the |
| 117 | // stack to the expectations of the surrounding statements. |
| 118 | // Takes a number of stack elements currently on top of the |
| 119 | // nested statement's stack, and returns a number of stack |
| 120 | // elements left on top of the surrounding statement's stack. |
| 121 | // The generated code must preserve the result register (which |
| 122 | // contains the value in case of a return). |
| 123 | virtual int Exit(int stack_depth) { |
| 124 | // Default implementation for the case where there is |
| 125 | // nothing to clean up. |
| 126 | return stack_depth; |
| 127 | } |
| 128 | NestedStatement* outer() { return previous_; } |
| 129 | protected: |
| 130 | MacroAssembler* masm() { return codegen_->masm(); } |
| 131 | private: |
| 132 | FullCodeGenerator* codegen_; |
| 133 | NestedStatement* previous_; |
| 134 | DISALLOW_COPY_AND_ASSIGN(NestedStatement); |
| 135 | }; |
| 136 | |
| 137 | class Breakable : public NestedStatement { |
| 138 | public: |
| 139 | Breakable(FullCodeGenerator* codegen, |
| 140 | BreakableStatement* break_target) |
| 141 | : NestedStatement(codegen), |
| 142 | target_(break_target) {} |
| 143 | virtual ~Breakable() {} |
| 144 | virtual Breakable* AsBreakable() { return this; } |
| 145 | virtual bool IsBreakTarget(Statement* statement) { |
| 146 | return target_ == statement; |
| 147 | } |
| 148 | BreakableStatement* statement() { return target_; } |
| 149 | Label* break_target() { return &break_target_label_; } |
| 150 | private: |
| 151 | BreakableStatement* target_; |
| 152 | Label break_target_label_; |
| 153 | DISALLOW_COPY_AND_ASSIGN(Breakable); |
| 154 | }; |
| 155 | |
| 156 | class Iteration : public Breakable { |
| 157 | public: |
| 158 | Iteration(FullCodeGenerator* codegen, |
| 159 | IterationStatement* iteration_statement) |
| 160 | : Breakable(codegen, iteration_statement) {} |
| 161 | virtual ~Iteration() {} |
| 162 | virtual Iteration* AsIteration() { return this; } |
| 163 | virtual bool IsContinueTarget(Statement* statement) { |
| 164 | return this->statement() == statement; |
| 165 | } |
| 166 | Label* continue_target() { return &continue_target_label_; } |
| 167 | private: |
| 168 | Label continue_target_label_; |
| 169 | DISALLOW_COPY_AND_ASSIGN(Iteration); |
| 170 | }; |
| 171 | |
| 172 | // The environment inside the try block of a try/catch statement. |
| 173 | class TryCatch : public NestedStatement { |
| 174 | public: |
| 175 | explicit TryCatch(FullCodeGenerator* codegen, Label* catch_entry) |
| 176 | : NestedStatement(codegen), catch_entry_(catch_entry) { } |
| 177 | virtual ~TryCatch() {} |
| 178 | virtual TryCatch* AsTryCatch() { return this; } |
| 179 | Label* catch_entry() { return catch_entry_; } |
| 180 | virtual int Exit(int stack_depth); |
| 181 | private: |
| 182 | Label* catch_entry_; |
| 183 | DISALLOW_COPY_AND_ASSIGN(TryCatch); |
| 184 | }; |
| 185 | |
| 186 | // The environment inside the try block of a try/finally statement. |
| 187 | class TryFinally : public NestedStatement { |
| 188 | public: |
| 189 | explicit TryFinally(FullCodeGenerator* codegen, Label* finally_entry) |
| 190 | : NestedStatement(codegen), finally_entry_(finally_entry) { } |
| 191 | virtual ~TryFinally() {} |
| 192 | virtual TryFinally* AsTryFinally() { return this; } |
| 193 | Label* finally_entry() { return finally_entry_; } |
| 194 | virtual int Exit(int stack_depth); |
| 195 | private: |
| 196 | Label* finally_entry_; |
| 197 | DISALLOW_COPY_AND_ASSIGN(TryFinally); |
| 198 | }; |
| 199 | |
| 200 | // A FinallyEnvironment represents being inside a finally block. |
| 201 | // Abnormal termination of the finally block needs to clean up |
| 202 | // the block's parameters from the stack. |
| 203 | class Finally : public NestedStatement { |
| 204 | public: |
| 205 | explicit Finally(FullCodeGenerator* codegen) : NestedStatement(codegen) { } |
| 206 | virtual ~Finally() {} |
| 207 | virtual Finally* AsFinally() { return this; } |
| 208 | virtual int Exit(int stack_depth) { |
| 209 | return stack_depth + kFinallyStackElementCount; |
| 210 | } |
| 211 | private: |
| 212 | // Number of extra stack slots occupied during a finally block. |
| 213 | static const int kFinallyStackElementCount = 2; |
| 214 | DISALLOW_COPY_AND_ASSIGN(Finally); |
| 215 | }; |
| 216 | |
| 217 | // A ForInEnvironment represents being inside a for-in loop. |
| 218 | // Abnormal termination of the for-in block needs to clean up |
| 219 | // the block's temporary storage from the stack. |
| 220 | class ForIn : public Iteration { |
| 221 | public: |
| 222 | ForIn(FullCodeGenerator* codegen, |
| 223 | ForInStatement* statement) |
| 224 | : Iteration(codegen, statement) { } |
| 225 | virtual ~ForIn() {} |
| 226 | virtual ForIn* AsForIn() { return this; } |
| 227 | virtual int Exit(int stack_depth) { |
| 228 | return stack_depth + kForInStackElementCount; |
| 229 | } |
| 230 | private: |
| 231 | // TODO(lrn): Check that this value is correct when implementing |
| 232 | // for-in. |
| 233 | static const int kForInStackElementCount = 5; |
| 234 | DISALLOW_COPY_AND_ASSIGN(ForIn); |
| 235 | }; |
| 236 | |
| 237 | enum Location { |
| 238 | kAccumulator, |
| 239 | kStack |
| 240 | }; |
| 241 | |
| 242 | int SlotOffset(Slot* slot); |
| 243 | |
| 244 | // Emit code to convert a pure value (in a register, slot, as a literal, |
| 245 | // or on top of the stack) into the result expected according to an |
| 246 | // expression context. |
| 247 | void Apply(Expression::Context context, Register reg); |
| 248 | |
| 249 | // Slot cannot have type Slot::LOOKUP. |
| 250 | void Apply(Expression::Context context, Slot* slot); |
| 251 | |
| 252 | void Apply(Expression::Context context, Literal* lit); |
| 253 | void ApplyTOS(Expression::Context context); |
| 254 | |
| 255 | // Emit code to discard count elements from the top of stack, then convert |
| 256 | // a pure value into the result expected according to an expression |
| 257 | // context. |
| 258 | void DropAndApply(int count, Expression::Context context, Register reg); |
| 259 | |
| 260 | // Emit code to convert pure control flow to a pair of labels into the |
| 261 | // result expected according to an expression context. |
| 262 | void Apply(Expression::Context context, |
| 263 | Label* materialize_true, |
| 264 | Label* materialize_false); |
| 265 | |
| 266 | // Helper function to convert a pure value into a test context. The value |
| 267 | // is expected on the stack or the accumulator, depending on the platform. |
| 268 | // See the platform-specific implementation for details. |
| 269 | void DoTest(Expression::Context context); |
| 270 | |
| 271 | void Move(Slot* dst, Register source, Register scratch1, Register scratch2); |
| 272 | void Move(Register dst, Slot* source); |
| 273 | |
| 274 | // Return an operand used to read/write to a known (ie, non-LOOKUP) slot. |
| 275 | // May emit code to traverse the context chain, destroying the scratch |
| 276 | // register. |
| 277 | MemOperand EmitSlotSearch(Slot* slot, Register scratch); |
| 278 | |
| 279 | void VisitForEffect(Expression* expr) { |
| 280 | Expression::Context saved_context = context_; |
| 281 | context_ = Expression::kEffect; |
| 282 | Visit(expr); |
| 283 | context_ = saved_context; |
| 284 | } |
| 285 | |
| 286 | void VisitForValue(Expression* expr, Location where) { |
| 287 | Expression::Context saved_context = context_; |
| 288 | Location saved_location = location_; |
| 289 | context_ = Expression::kValue; |
| 290 | location_ = where; |
| 291 | Visit(expr); |
| 292 | context_ = saved_context; |
| 293 | location_ = saved_location; |
| 294 | } |
| 295 | |
| 296 | void VisitForControl(Expression* expr, Label* if_true, Label* if_false) { |
| 297 | Expression::Context saved_context = context_; |
| 298 | Label* saved_true = true_label_; |
| 299 | Label* saved_false = false_label_; |
| 300 | context_ = Expression::kTest; |
| 301 | true_label_ = if_true; |
| 302 | false_label_ = if_false; |
| 303 | Visit(expr); |
| 304 | context_ = saved_context; |
| 305 | true_label_ = saved_true; |
| 306 | false_label_ = saved_false; |
| 307 | } |
| 308 | |
| 309 | void VisitForValueControl(Expression* expr, |
| 310 | Location where, |
| 311 | Label* if_true, |
| 312 | Label* if_false) { |
| 313 | Expression::Context saved_context = context_; |
| 314 | Location saved_location = location_; |
| 315 | Label* saved_true = true_label_; |
| 316 | Label* saved_false = false_label_; |
| 317 | context_ = Expression::kValueTest; |
| 318 | location_ = where; |
| 319 | true_label_ = if_true; |
| 320 | false_label_ = if_false; |
| 321 | Visit(expr); |
| 322 | context_ = saved_context; |
| 323 | location_ = saved_location; |
| 324 | true_label_ = saved_true; |
| 325 | false_label_ = saved_false; |
| 326 | } |
| 327 | |
| 328 | void VisitForControlValue(Expression* expr, |
| 329 | Location where, |
| 330 | Label* if_true, |
| 331 | Label* if_false) { |
| 332 | Expression::Context saved_context = context_; |
| 333 | Location saved_location = location_; |
| 334 | Label* saved_true = true_label_; |
| 335 | Label* saved_false = false_label_; |
| 336 | context_ = Expression::kTestValue; |
| 337 | location_ = where; |
| 338 | true_label_ = if_true; |
| 339 | false_label_ = if_false; |
| 340 | Visit(expr); |
| 341 | context_ = saved_context; |
| 342 | location_ = saved_location; |
| 343 | true_label_ = saved_true; |
| 344 | false_label_ = saved_false; |
| 345 | } |
| 346 | |
| 347 | void VisitDeclarations(ZoneList<Declaration*>* declarations); |
| 348 | void DeclareGlobals(Handle<FixedArray> pairs); |
| 349 | |
| 350 | // Platform-specific return sequence |
| 351 | void EmitReturnSequence(int position); |
| 352 | |
| 353 | // Platform-specific code sequences for calls |
| 354 | void EmitCallWithStub(Call* expr); |
| 355 | void EmitCallWithIC(Call* expr, Handle<Object> name, RelocInfo::Mode mode); |
| 356 | |
| 357 | // Platform-specific code for loading variables. |
| 358 | void EmitVariableLoad(Variable* expr, Expression::Context context); |
| 359 | |
| 360 | // Platform-specific support for compiling assignments. |
| 361 | |
| 362 | // Load a value from a named property. |
| 363 | // The receiver is left on the stack by the IC. |
| 364 | void EmitNamedPropertyLoad(Property* expr); |
| 365 | |
| 366 | // Load a value from a keyed property. |
| 367 | // The receiver and the key is left on the stack by the IC. |
| 368 | void EmitKeyedPropertyLoad(Property* expr); |
| 369 | |
| 370 | // Apply the compound assignment operator. Expects the left operand on top |
| 371 | // of the stack and the right one in the accumulator. |
| 372 | void EmitBinaryOp(Token::Value op, Expression::Context context); |
| 373 | |
| 374 | // Complete a variable assignment. The right-hand-side value is expected |
| 375 | // in the accumulator. |
| 376 | void EmitVariableAssignment(Variable* var, Expression::Context context); |
| 377 | |
| 378 | // Complete a named property assignment. The receiver is expected on top |
| 379 | // of the stack and the right-hand-side value in the accumulator. |
| 380 | void EmitNamedPropertyAssignment(Assignment* expr); |
| 381 | |
| 382 | // Complete a keyed property assignment. The receiver and key are |
| 383 | // expected on top of the stack and the right-hand-side value in the |
| 384 | // accumulator. |
| 385 | void EmitKeyedPropertyAssignment(Assignment* expr); |
| 386 | |
| 387 | void SetFunctionPosition(FunctionLiteral* fun); |
| 388 | void SetReturnPosition(FunctionLiteral* fun); |
| 389 | void SetStatementPosition(Statement* stmt); |
| 390 | void SetStatementPosition(int pos); |
| 391 | void SetSourcePosition(int pos); |
| 392 | |
| 393 | // Non-local control flow support. |
| 394 | void EnterFinallyBlock(); |
| 395 | void ExitFinallyBlock(); |
| 396 | |
| 397 | // Loop nesting counter. |
| 398 | int loop_depth() { return loop_depth_; } |
| 399 | void increment_loop_depth() { loop_depth_++; } |
| 400 | void decrement_loop_depth() { |
| 401 | ASSERT(loop_depth_ > 0); |
| 402 | loop_depth_--; |
| 403 | } |
| 404 | |
| 405 | MacroAssembler* masm() { return masm_; } |
| 406 | static Register result_register(); |
| 407 | static Register context_register(); |
| 408 | |
| 409 | // Set fields in the stack frame. Offsets are the frame pointer relative |
| 410 | // offsets defined in, e.g., StandardFrameConstants. |
| 411 | void StoreToFrameField(int frame_offset, Register value); |
| 412 | |
| 413 | // Load a value from the current context. Indices are defined as an enum |
| 414 | // in v8::internal::Context. |
| 415 | void LoadContextField(Register dst, int context_index); |
| 416 | |
| 417 | // AST node visit functions. |
| 418 | #define DECLARE_VISIT(type) virtual void Visit##type(type* node); |
| 419 | AST_NODE_LIST(DECLARE_VISIT) |
| 420 | #undef DECLARE_VISIT |
| 421 | // Handles the shortcutted logical binary operations in VisitBinaryOperation. |
| 422 | void EmitLogicalOperation(BinaryOperation* expr); |
| 423 | |
| 424 | MacroAssembler* masm_; |
| 425 | FunctionLiteral* function_; |
| 426 | Handle<Script> script_; |
| 427 | bool is_eval_; |
| 428 | Label return_label_; |
| 429 | NestedStatement* nesting_stack_; |
| 430 | int loop_depth_; |
| 431 | |
| 432 | Expression::Context context_; |
| 433 | Location location_; |
| 434 | Label* true_label_; |
| 435 | Label* false_label_; |
| 436 | |
| 437 | friend class NestedStatement; |
| 438 | |
| 439 | DISALLOW_COPY_AND_ASSIGN(FullCodeGenerator); |
| 440 | }; |
| 441 | |
| 442 | |
| 443 | } } // namespace v8::internal |
| 444 | |
| 445 | #endif // V8_FULL_CODEGEN_H_ |