OLD | NEW |
(Empty) | |
| 1 // Copyright 2006-2008 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_CODEGEN_ARM_H_ |
| 29 #define V8_CODEGEN_ARM_H_ |
| 30 |
| 31 #include "scopes.h" |
| 32 |
| 33 namespace v8 { namespace internal { |
| 34 |
| 35 // Forward declarations |
| 36 class DeferredCode; |
| 37 |
| 38 // Mode to overwrite BinaryExpression values. |
| 39 enum OverwriteMode { NO_OVERWRITE, OVERWRITE_LEFT, OVERWRITE_RIGHT }; |
| 40 |
| 41 |
| 42 // ----------------------------------------------------------------------------- |
| 43 // Reference support |
| 44 |
| 45 // A reference is a C++ stack-allocated object that keeps an ECMA |
| 46 // reference on the execution stack while in scope. For variables |
| 47 // the reference is empty, indicating that it isn't necessary to |
| 48 // store state on the stack for keeping track of references to those. |
| 49 // For properties, we keep either one (named) or two (indexed) values |
| 50 // on the execution stack to represent the reference. |
| 51 |
| 52 enum InitState { CONST_INIT, NOT_CONST_INIT }; |
| 53 enum TypeofState { INSIDE_TYPEOF, NOT_INSIDE_TYPEOF }; |
| 54 |
| 55 class Reference BASE_EMBEDDED { |
| 56 public: |
| 57 // The values of the types is important, see size(). |
| 58 enum Type { ILLEGAL = -1, SLOT = 0, NAMED = 1, KEYED = 2 }; |
| 59 Reference(CodeGenerator* cgen, Expression* expression); |
| 60 ~Reference(); |
| 61 |
| 62 Expression* expression() const { return expression_; } |
| 63 Type type() const { return type_; } |
| 64 void set_type(Type value) { |
| 65 ASSERT(type_ == ILLEGAL); |
| 66 type_ = value; |
| 67 } |
| 68 |
| 69 // The size of the reference or -1 if the reference is illegal. |
| 70 int size() const { return type_; } |
| 71 |
| 72 bool is_illegal() const { return type_ == ILLEGAL; } |
| 73 bool is_slot() const { return type_ == SLOT; } |
| 74 bool is_property() const { return type_ == NAMED || type_ == KEYED; } |
| 75 |
| 76 // Return the name. Only valid for named property references. |
| 77 Handle<String> GetName(); |
| 78 |
| 79 // 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 |
| 81 // the expression stack, and it is left in place with its value above it. |
| 82 void GetValue(TypeofState typeof_state); |
| 83 |
| 84 // Generate code to store the value on top of the expression stack in the |
| 85 // reference. The reference is expected to be immediately below the value |
| 86 // on the expression stack. The stored value is left in place (with the |
| 87 // reference intact below it) to support chained assignments. |
| 88 void SetValue(InitState init_state); |
| 89 |
| 90 private: |
| 91 CodeGenerator* cgen_; |
| 92 Expression* expression_; |
| 93 Type type_; |
| 94 }; |
| 95 |
| 96 |
| 97 // ------------------------------------------------------------------------- |
| 98 // Code generation state |
| 99 |
| 100 // The state is passed down the AST by the code generator (and back up, in |
| 101 // the form of the state of the label pair). It is threaded through the |
| 102 // call stack. Constructing a state implicitly pushes it on the owning code |
| 103 // generator's stack of states, and destroying one implicitly pops it. |
| 104 |
| 105 class CodeGenState BASE_EMBEDDED { |
| 106 public: |
| 107 // Create an initial code generator state. Destroying the initial state |
| 108 // leaves the code generator with a NULL state. |
| 109 explicit CodeGenState(CodeGenerator* owner); |
| 110 |
| 111 // Create a code generator state based on a code generator's current |
| 112 // state. The new state has its own typeof state and pair of branch |
| 113 // labels. |
| 114 CodeGenState(CodeGenerator* owner, |
| 115 TypeofState typeof_state, |
| 116 Label* true_target, |
| 117 Label* false_target); |
| 118 |
| 119 // Destroy a code generator state and restore the owning code generator's |
| 120 // previous state. |
| 121 ~CodeGenState(); |
| 122 |
| 123 TypeofState typeof_state() const { return typeof_state_; } |
| 124 Label* true_target() const { return true_target_; } |
| 125 Label* false_target() const { return false_target_; } |
| 126 |
| 127 private: |
| 128 CodeGenerator* owner_; |
| 129 TypeofState typeof_state_; |
| 130 Label* true_target_; |
| 131 Label* false_target_; |
| 132 CodeGenState* previous_; |
| 133 }; |
| 134 |
| 135 |
| 136 // ----------------------------------------------------------------------------- |
| 137 // CodeGenerator |
| 138 |
| 139 class CodeGenerator: public Visitor { |
| 140 public: |
| 141 // Takes a function literal, generates code for it. This function should only |
| 142 // be called by compiler.cc. |
| 143 static Handle<Code> MakeCode(FunctionLiteral* fun, |
| 144 Handle<Script> script, |
| 145 bool is_eval); |
| 146 |
| 147 static void SetFunctionInfo(Handle<JSFunction> fun, |
| 148 int length, |
| 149 int function_token_position, |
| 150 int start_position, |
| 151 int end_position, |
| 152 bool is_expression, |
| 153 bool is_toplevel, |
| 154 Handle<Script> script); |
| 155 |
| 156 // Accessors |
| 157 MacroAssembler* masm() { return masm_; } |
| 158 |
| 159 CodeGenState* state() { return state_; } |
| 160 void set_state(CodeGenState* state) { state_ = state; } |
| 161 |
| 162 void AddDeferred(DeferredCode* code) { deferred_.Add(code); } |
| 163 |
| 164 private: |
| 165 // Construction/Destruction |
| 166 CodeGenerator(int buffer_size, Handle<Script> script, bool is_eval); |
| 167 virtual ~CodeGenerator() { delete masm_; } |
| 168 |
| 169 // Accessors |
| 170 Scope* scope() const { return scope_; } |
| 171 |
| 172 void ProcessDeferred(); |
| 173 |
| 174 bool is_eval() { return is_eval_; } |
| 175 |
| 176 // State |
| 177 bool has_cc() const { return cc_reg_ != al; } |
| 178 TypeofState typeof_state() const { return state_->typeof_state(); } |
| 179 Label* true_target() const { return state_->true_target(); } |
| 180 Label* false_target() const { return state_->false_target(); } |
| 181 |
| 182 |
| 183 // Node visitors. |
| 184 #define DEF_VISIT(type) \ |
| 185 void Visit##type(type* node); |
| 186 NODE_LIST(DEF_VISIT) |
| 187 #undef DEF_VISIT |
| 188 |
| 189 // Main code generation function |
| 190 void GenCode(FunctionLiteral* fun); |
| 191 |
| 192 // The following are used by class Reference. |
| 193 void LoadReference(Reference* ref); |
| 194 void UnloadReference(Reference* ref); |
| 195 |
| 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 { |
| 211 return MemOperand(context, Context::SlotOffset(index)); |
| 212 } |
| 213 |
| 214 MemOperand SlotOperand(Slot* slot, Register tmp); |
| 215 |
| 216 // Expressions |
| 217 MemOperand GlobalObject() const { |
| 218 return ContextOperand(cp, Context::GLOBAL_INDEX); |
| 219 } |
| 220 |
| 221 void LoadCondition(Expression* x, |
| 222 TypeofState typeof_state, |
| 223 Label* true_target, |
| 224 Label* false_target, |
| 225 bool force_cc); |
| 226 void Load(Expression* x, TypeofState typeof_state = NOT_INSIDE_TYPEOF); |
| 227 void LoadGlobal(); |
| 228 |
| 229 // Read a value from a slot and leave it on top of the expression stack. |
| 230 void LoadFromSlot(Slot* slot, TypeofState typeof_state); |
| 231 |
| 232 // Special code for typeof expressions: Unfortunately, we must |
| 233 // be careful when loading the expression in 'typeof' |
| 234 // expressions. We are not allowed to throw reference errors for |
| 235 // non-existing properties of the global object, so we must make it |
| 236 // look like an explicit property access, instead of an access |
| 237 // through the context chain. |
| 238 void LoadTypeofExpression(Expression* x); |
| 239 |
| 240 void ToBoolean(Label* true_target, Label* false_target); |
| 241 |
| 242 void GenericBinaryOperation(Token::Value op); |
| 243 void Comparison(Condition cc, bool strict = false); |
| 244 |
| 245 void SmiOperation(Token::Value op, Handle<Object> value, bool reversed); |
| 246 |
| 247 void CallWithArguments(ZoneList<Expression*>* arguments, int position); |
| 248 |
| 249 // Control flow |
| 250 void Branch(bool if_true, Label* L); |
| 251 void CheckStack(); |
| 252 void CleanStack(int num_bytes); |
| 253 |
| 254 bool CheckForInlineRuntimeCall(CallRuntime* node); |
| 255 Handle<JSFunction> BuildBoilerplate(FunctionLiteral* node); |
| 256 void ProcessDeclarations(ZoneList<Declaration*>* declarations); |
| 257 |
| 258 Handle<Code> ComputeCallInitialize(int argc); |
| 259 |
| 260 // Declare global variables and functions in the given array of |
| 261 // name/value pairs. |
| 262 void DeclareGlobals(Handle<FixedArray> pairs); |
| 263 |
| 264 // Instantiate the function boilerplate. |
| 265 void InstantiateBoilerplate(Handle<JSFunction> boilerplate); |
| 266 |
| 267 // Support for type checks. |
| 268 void GenerateIsSmi(ZoneList<Expression*>* args); |
| 269 void GenerateIsNonNegativeSmi(ZoneList<Expression*>* args); |
| 270 void GenerateIsArray(ZoneList<Expression*>* args); |
| 271 |
| 272 // Support for arguments.length and arguments[?]. |
| 273 void GenerateArgumentsLength(ZoneList<Expression*>* args); |
| 274 void GenerateArgumentsAccess(ZoneList<Expression*>* args); |
| 275 |
| 276 // Support for accessing the value field of an object (used by Date). |
| 277 void GenerateValueOf(ZoneList<Expression*>* args); |
| 278 void GenerateSetValueOf(ZoneList<Expression*>* args); |
| 279 |
| 280 // Fast support for charCodeAt(n). |
| 281 void GenerateFastCharCodeAt(ZoneList<Expression*>* args); |
| 282 |
| 283 // Fast support for object equality testing. |
| 284 void GenerateObjectEquals(ZoneList<Expression*>* args); |
| 285 |
| 286 // Methods and constants for fast case switch statement support. |
| 287 // |
| 288 // Only allow fast-case switch if the range of labels is at most |
| 289 // this factor times the number of case labels. |
| 290 // Value is derived from comparing the size of code generated by the normal |
| 291 // switch code for Smi-labels to the size of a single pointer. If code |
| 292 // quality increases this number should be decreased to match. |
| 293 static const int kFastSwitchMaxOverheadFactor = 10; |
| 294 |
| 295 // Minimal number of switch cases required before we allow jump-table |
| 296 // optimization. |
| 297 static const int kFastSwitchMinCaseCount = 5; |
| 298 |
| 299 // The limit of the range of a fast-case switch, as a factor of the number |
| 300 // of cases of the switch. Each platform should return a value that |
| 301 // is optimal compared to the default code generated for a switch statement |
| 302 // on that platform. |
| 303 int FastCaseSwitchMaxOverheadFactor(); |
| 304 |
| 305 // The minimal number of cases in a switch before the fast-case switch |
| 306 // optimization is enabled. Each platform should return a value that |
| 307 // is optimal compared to the default code generated for a switch statement |
| 308 // on that platform. |
| 309 int FastCaseSwitchMinCaseCount(); |
| 310 |
| 311 // Allocate a jump table and create code to jump through it. |
| 312 // Should call GenerateFastCaseSwitchCases to generate the code for |
| 313 // all the cases at the appropriate point. |
| 314 void GenerateFastCaseSwitchJumpTable(SwitchStatement* node, int min_index, |
| 315 int range, Label *fail_label, |
| 316 SmartPointer<Label*> &case_targets, |
| 317 SmartPointer<Label>& case_labels); |
| 318 |
| 319 // Generate the code for cases for the fast case switch. |
| 320 // Called by GenerateFastCaseSwitchJumpTable. |
| 321 void GenerateFastCaseSwitchCases(SwitchStatement* node, |
| 322 SmartPointer<Label> &case_labels); |
| 323 |
| 324 // Fast support for constant-Smi switches. |
| 325 void GenerateFastCaseSwitchStatement(SwitchStatement *node, int min_index, |
| 326 int range, int default_index); |
| 327 |
| 328 // Fast support for constant-Smi switches. Tests whether switch statement |
| 329 // permits optimization and calls GenerateFastCaseSwitch if it does. |
| 330 // Returns true if the fast-case switch was generated, and false if not. |
| 331 bool TryGenerateFastCaseSwitchStatement(SwitchStatement *node); |
| 332 |
| 333 |
| 334 // Bottle-neck interface to call the Assembler to generate the statement |
| 335 // position. This allows us to easily control whether statement positions |
| 336 // should be generated or not. |
| 337 void RecordStatementPosition(Node* node); |
| 338 |
| 339 // Activation frames. |
| 340 void EnterJSFrame(); |
| 341 void ExitJSFrame(); |
| 342 |
| 343 |
| 344 bool is_eval_; // Tells whether code is generated for eval. |
| 345 Handle<Script> script_; |
| 346 List<DeferredCode*> deferred_; |
| 347 |
| 348 // Assembler |
| 349 MacroAssembler* masm_; // to generate code |
| 350 |
| 351 // Code generation state |
| 352 Scope* scope_; |
| 353 Condition cc_reg_; |
| 354 CodeGenState* state_; |
| 355 bool is_inside_try_; |
| 356 int break_stack_height_; |
| 357 |
| 358 // Labels |
| 359 Label function_return_; |
| 360 |
| 361 friend class Reference; |
| 362 friend class Property; |
| 363 friend class VariableProxy; |
| 364 friend class Slot; |
| 365 |
| 366 DISALLOW_COPY_AND_ASSIGN(CodeGenerator); |
| 367 }; |
| 368 |
| 369 } } // namespace v8::internal |
| 370 |
| 371 #endif // V8_CODEGEN_ARM_H_ |
OLD | NEW |