| OLD | NEW |
| 1 // Copyright 2006-2008 the V8 project authors. All rights reserved. | 1 // Copyright 2006-2008 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| (...skipping 20 matching lines...) Expand all Loading... |
| 31 #include "scopes.h" | 31 #include "scopes.h" |
| 32 | 32 |
| 33 namespace v8 { namespace internal { | 33 namespace v8 { namespace internal { |
| 34 | 34 |
| 35 // Forward declarations | 35 // Forward declarations |
| 36 class DeferredCode; | 36 class DeferredCode; |
| 37 | 37 |
| 38 // Mode to overwrite BinaryExpression values. | 38 // Mode to overwrite BinaryExpression values. |
| 39 enum OverwriteMode { NO_OVERWRITE, OVERWRITE_LEFT, OVERWRITE_RIGHT }; | 39 enum OverwriteMode { NO_OVERWRITE, OVERWRITE_LEFT, OVERWRITE_RIGHT }; |
| 40 | 40 |
| 41 enum InitState { CONST_INIT, NOT_CONST_INIT }; |
| 42 enum TypeofState { INSIDE_TYPEOF, NOT_INSIDE_TYPEOF }; |
| 41 | 43 |
| 42 // ----------------------------------------------------------------------------- | 44 |
| 45 // ------------------------------------------------------------------------- |
| 46 // Virtual frame |
| 47 |
| 48 class VirtualFrame BASE_EMBEDDED { |
| 49 public: |
| 50 explicit VirtualFrame(CodeGenerator* cgen); |
| 51 |
| 52 void Enter(); |
| 53 void Exit(); |
| 54 |
| 55 void AllocateLocals(); |
| 56 |
| 57 MemOperand Top() const { return MemOperand(sp, 0); } |
| 58 |
| 59 MemOperand Element(int index) const { |
| 60 return MemOperand(sp, index * kPointerSize); |
| 61 } |
| 62 |
| 63 MemOperand Local(int index) const { |
| 64 ASSERT(0 <= index && index < frame_local_count_); |
| 65 return MemOperand(fp, kLocal0Offset - index * kPointerSize); |
| 66 } |
| 67 |
| 68 MemOperand Function() const { return MemOperand(fp, kFunctionOffset); } |
| 69 |
| 70 MemOperand Context() const { return MemOperand(fp, kContextOffset); } |
| 71 |
| 72 MemOperand Parameter(int index) const { |
| 73 // Index -1 corresponds to the receiver. |
| 74 ASSERT(-1 <= index && index <= parameter_count_); |
| 75 return MemOperand(fp, (1 + parameter_count_ - index) * kPointerSize); |
| 76 } |
| 77 |
| 78 inline void Drop(int count); |
| 79 |
| 80 inline void Pop(); |
| 81 inline void Pop(Register reg); |
| 82 |
| 83 inline void Push(Register reg); |
| 84 |
| 85 private: |
| 86 static const int kLocal0Offset = JavaScriptFrameConstants::kLocal0Offset; |
| 87 static const int kFunctionOffset = JavaScriptFrameConstants::kFunctionOffset; |
| 88 static const int kContextOffset = StandardFrameConstants::kContextOffset; |
| 89 |
| 90 MacroAssembler* masm_; |
| 91 int frame_local_count_; |
| 92 int parameter_count_; |
| 93 }; |
| 94 |
| 95 |
| 96 // ------------------------------------------------------------------------- |
| 43 // Reference support | 97 // Reference support |
| 44 | 98 |
| 45 // A reference is a C++ stack-allocated object that keeps an ECMA | 99 // A reference is a C++ stack-allocated object that keeps an ECMA |
| 46 // reference on the execution stack while in scope. For variables | 100 // reference on the execution stack while in scope. For variables |
| 47 // the reference is empty, indicating that it isn't necessary to | 101 // the reference is empty, indicating that it isn't necessary to |
| 48 // store state on the stack for keeping track of references to those. | 102 // store state on the stack for keeping track of references to those. |
| 49 // For properties, we keep either one (named) or two (indexed) values | 103 // For properties, we keep either one (named) or two (indexed) values |
| 50 // on the execution stack to represent the reference. | 104 // on the execution stack to represent the reference. |
| 51 | 105 |
| 52 enum InitState { CONST_INIT, NOT_CONST_INIT }; | |
| 53 enum TypeofState { INSIDE_TYPEOF, NOT_INSIDE_TYPEOF }; | |
| 54 | |
| 55 class Reference BASE_EMBEDDED { | 106 class Reference BASE_EMBEDDED { |
| 56 public: | 107 public: |
| 57 // The values of the types is important, see size(). | 108 // The values of the types is important, see size(). |
| 58 enum Type { ILLEGAL = -1, SLOT = 0, NAMED = 1, KEYED = 2 }; | 109 enum Type { ILLEGAL = -1, SLOT = 0, NAMED = 1, KEYED = 2 }; |
| 59 Reference(CodeGenerator* cgen, Expression* expression); | 110 Reference(CodeGenerator* cgen, Expression* expression); |
| 60 ~Reference(); | 111 ~Reference(); |
| 61 | 112 |
| 62 Expression* expression() const { return expression_; } | 113 Expression* expression() const { return expression_; } |
| 63 Type type() const { return type_; } | 114 Type type() const { return type_; } |
| 64 void set_type(Type value) { | 115 void set_type(Type value) { |
| 65 ASSERT(type_ == ILLEGAL); | 116 ASSERT(type_ == ILLEGAL); |
| 66 type_ = value; | 117 type_ = value; |
| 67 } | 118 } |
| 68 | 119 |
| 69 // The size of the reference or -1 if the reference is illegal. | 120 // The size the reference takes up on the stack. |
| 70 int size() const { return type_; } | 121 int size() const { return (type_ == ILLEGAL) ? 0 : type_; } |
| 71 | 122 |
| 72 bool is_illegal() const { return type_ == ILLEGAL; } | 123 bool is_illegal() const { return type_ == ILLEGAL; } |
| 73 bool is_slot() const { return type_ == SLOT; } | 124 bool is_slot() const { return type_ == SLOT; } |
| 74 bool is_property() const { return type_ == NAMED || type_ == KEYED; } | 125 bool is_property() const { return type_ == NAMED || type_ == KEYED; } |
| 75 | 126 |
| 76 // Return the name. Only valid for named property references. | 127 // Return the name. Only valid for named property references. |
| 77 Handle<String> GetName(); | 128 Handle<String> GetName(); |
| 78 | 129 |
| 79 // Generate code to push the value of the reference on top of the | 130 // Generate code to push the value of the reference on top of the |
| 80 // expression stack. The reference is expected to be already on top of | 131 // expression stack. The reference is expected to be already on top of |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 126 | 177 |
| 127 private: | 178 private: |
| 128 CodeGenerator* owner_; | 179 CodeGenerator* owner_; |
| 129 TypeofState typeof_state_; | 180 TypeofState typeof_state_; |
| 130 Label* true_target_; | 181 Label* true_target_; |
| 131 Label* false_target_; | 182 Label* false_target_; |
| 132 CodeGenState* previous_; | 183 CodeGenState* previous_; |
| 133 }; | 184 }; |
| 134 | 185 |
| 135 | 186 |
| 136 // ----------------------------------------------------------------------------- | 187 // ------------------------------------------------------------------------- |
| 137 // CodeGenerator | 188 // CodeGenerator |
| 138 | 189 |
| 139 class CodeGenerator: public Visitor { | 190 class CodeGenerator: public Visitor { |
| 140 public: | 191 public: |
| 141 // Takes a function literal, generates code for it. This function should only | 192 // Takes a function literal, generates code for it. This function should only |
| 142 // be called by compiler.cc. | 193 // be called by compiler.cc. |
| 143 static Handle<Code> MakeCode(FunctionLiteral* fun, | 194 static Handle<Code> MakeCode(FunctionLiteral* fun, |
| 144 Handle<Script> script, | 195 Handle<Script> script, |
| 145 bool is_eval); | 196 bool is_eval); |
| 146 | 197 |
| 147 static void SetFunctionInfo(Handle<JSFunction> fun, | 198 static void SetFunctionInfo(Handle<JSFunction> fun, |
| 148 int length, | 199 int length, |
| 149 int function_token_position, | 200 int function_token_position, |
| 150 int start_position, | 201 int start_position, |
| 151 int end_position, | 202 int end_position, |
| 152 bool is_expression, | 203 bool is_expression, |
| 153 bool is_toplevel, | 204 bool is_toplevel, |
| 154 Handle<Script> script); | 205 Handle<Script> script); |
| 155 | 206 |
| 156 // Accessors | 207 // Accessors |
| 157 MacroAssembler* masm() { return masm_; } | 208 MacroAssembler* masm() { return masm_; } |
| 158 | 209 |
| 210 VirtualFrame* frame() const { return frame_; } |
| 211 |
| 159 CodeGenState* state() { return state_; } | 212 CodeGenState* state() { return state_; } |
| 160 void set_state(CodeGenState* state) { state_ = state; } | 213 void set_state(CodeGenState* state) { state_ = state; } |
| 161 | 214 |
| 162 void AddDeferred(DeferredCode* code) { deferred_.Add(code); } | 215 void AddDeferred(DeferredCode* code) { deferred_.Add(code); } |
| 163 | 216 |
| 164 private: | 217 private: |
| 165 // Construction/Destruction | 218 // Construction/Destruction |
| 166 CodeGenerator(int buffer_size, Handle<Script> script, bool is_eval); | 219 CodeGenerator(int buffer_size, Handle<Script> script, bool is_eval); |
| 167 virtual ~CodeGenerator() { delete masm_; } | 220 virtual ~CodeGenerator() { delete masm_; } |
| 168 | 221 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 186 NODE_LIST(DEF_VISIT) | 239 NODE_LIST(DEF_VISIT) |
| 187 #undef DEF_VISIT | 240 #undef DEF_VISIT |
| 188 | 241 |
| 189 // Main code generation function | 242 // Main code generation function |
| 190 void GenCode(FunctionLiteral* fun); | 243 void GenCode(FunctionLiteral* fun); |
| 191 | 244 |
| 192 // The following are used by class Reference. | 245 // The following are used by class Reference. |
| 193 void LoadReference(Reference* ref); | 246 void LoadReference(Reference* ref); |
| 194 void UnloadReference(Reference* ref); | 247 void UnloadReference(Reference* ref); |
| 195 | 248 |
| 196 // Support functions for accessing parameters and other operands. | |
| 197 MemOperand ParameterOperand(int index) const { | |
| 198 int num_parameters = scope()->num_parameters(); | |
| 199 // index -2 corresponds to the activated closure, -1 corresponds | |
| 200 // to the receiver | |
| 201 ASSERT(-2 <= index && index < num_parameters); | |
| 202 int offset = (1 + num_parameters - index) * kPointerSize; | |
| 203 return MemOperand(fp, offset); | |
| 204 } | |
| 205 | |
| 206 MemOperand FunctionOperand() const { | |
| 207 return MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset); | |
| 208 } | |
| 209 | |
| 210 MemOperand ContextOperand(Register context, int index) const { | 249 MemOperand ContextOperand(Register context, int index) const { |
| 211 return MemOperand(context, Context::SlotOffset(index)); | 250 return MemOperand(context, Context::SlotOffset(index)); |
| 212 } | 251 } |
| 213 | 252 |
| 214 MemOperand SlotOperand(Slot* slot, Register tmp); | 253 MemOperand SlotOperand(Slot* slot, Register tmp); |
| 215 | 254 |
| 216 // Expressions | 255 // Expressions |
| 217 MemOperand GlobalObject() const { | 256 MemOperand GlobalObject() const { |
| 218 return ContextOperand(cp, Context::GLOBAL_INDEX); | 257 return ContextOperand(cp, Context::GLOBAL_INDEX); |
| 219 } | 258 } |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 334 // permits optimization and calls GenerateFastCaseSwitch if it does. | 373 // permits optimization and calls GenerateFastCaseSwitch if it does. |
| 335 // Returns true if the fast-case switch was generated, and false if not. | 374 // Returns true if the fast-case switch was generated, and false if not. |
| 336 bool TryGenerateFastCaseSwitchStatement(SwitchStatement* node); | 375 bool TryGenerateFastCaseSwitchStatement(SwitchStatement* node); |
| 337 | 376 |
| 338 | 377 |
| 339 // Bottle-neck interface to call the Assembler to generate the statement | 378 // Bottle-neck interface to call the Assembler to generate the statement |
| 340 // position. This allows us to easily control whether statement positions | 379 // position. This allows us to easily control whether statement positions |
| 341 // should be generated or not. | 380 // should be generated or not. |
| 342 void RecordStatementPosition(Node* node); | 381 void RecordStatementPosition(Node* node); |
| 343 | 382 |
| 344 // Activation frames. | |
| 345 void EnterJSFrame(); | |
| 346 void ExitJSFrame(); | |
| 347 | |
| 348 | |
| 349 bool is_eval_; // Tells whether code is generated for eval. | 383 bool is_eval_; // Tells whether code is generated for eval. |
| 350 Handle<Script> script_; | 384 Handle<Script> script_; |
| 351 List<DeferredCode*> deferred_; | 385 List<DeferredCode*> deferred_; |
| 352 | 386 |
| 353 // Assembler | 387 // Assembler |
| 354 MacroAssembler* masm_; // to generate code | 388 MacroAssembler* masm_; // to generate code |
| 355 | 389 |
| 356 // Code generation state | 390 // Code generation state |
| 357 Scope* scope_; | 391 Scope* scope_; |
| 392 VirtualFrame* frame_; |
| 358 Condition cc_reg_; | 393 Condition cc_reg_; |
| 359 CodeGenState* state_; | 394 CodeGenState* state_; |
| 360 bool is_inside_try_; | 395 bool is_inside_try_; |
| 361 int break_stack_height_; | 396 int break_stack_height_; |
| 362 | 397 |
| 363 // Labels | 398 // Labels |
| 364 Label function_return_; | 399 Label function_return_; |
| 365 | 400 |
| 401 friend class VirtualFrame; |
| 366 friend class Reference; | 402 friend class Reference; |
| 367 friend class Property; | |
| 368 friend class VariableProxy; | |
| 369 friend class Slot; | |
| 370 | 403 |
| 371 DISALLOW_COPY_AND_ASSIGN(CodeGenerator); | 404 DISALLOW_COPY_AND_ASSIGN(CodeGenerator); |
| 372 }; | 405 }; |
| 373 | 406 |
| 374 } } // namespace v8::internal | 407 } } // namespace v8::internal |
| 375 | 408 |
| 376 #endif // V8_CODEGEN_ARM_H_ | 409 #endif // V8_CODEGEN_ARM_H_ |
| OLD | NEW |