ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 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 | |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 28 | #ifndef V8_ARM_CODEGEN_ARM_H_ |
| 29 | #define V8_ARM_CODEGEN_ARM_H_ |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 30 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 31 | namespace v8 { |
| 32 | namespace internal { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 33 | |
| 34 | // Forward declarations |
| 35 | class DeferredCode; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 36 | class RegisterAllocator; |
| 37 | class RegisterFile; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 38 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 39 | enum InitState { CONST_INIT, NOT_CONST_INIT }; |
| 40 | enum TypeofState { INSIDE_TYPEOF, NOT_INSIDE_TYPEOF }; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 41 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 42 | |
| 43 | // ------------------------------------------------------------------------- |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 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 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 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 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 67 | // The size the reference takes up on the stack. |
| 68 | int size() const { return (type_ == ILLEGAL) ? 0 : type_; } |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 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. |
| 80 | void GetValue(TypeofState typeof_state); |
| 81 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 82 | // Generate code to push the value of a reference on top of the expression |
| 83 | // stack and then spill the stack frame. This function is used temporarily |
| 84 | // while the code generator is being transformed. |
| 85 | inline void GetValueAndSpill(TypeofState typeof_state); |
| 86 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 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 | // Code generation state |
| 102 | |
| 103 | // The state is passed down the AST by the code generator (and back up, in |
| 104 | // the form of the state of the label pair). It is threaded through the |
| 105 | // call stack. Constructing a state implicitly pushes it on the owning code |
| 106 | // generator's stack of states, and destroying one implicitly pops it. |
| 107 | |
| 108 | class CodeGenState BASE_EMBEDDED { |
| 109 | public: |
| 110 | // Create an initial code generator state. Destroying the initial state |
| 111 | // leaves the code generator with a NULL state. |
| 112 | explicit CodeGenState(CodeGenerator* owner); |
| 113 | |
| 114 | // Create a code generator state based on a code generator's current |
| 115 | // state. The new state has its own typeof state and pair of branch |
| 116 | // labels. |
| 117 | CodeGenState(CodeGenerator* owner, |
| 118 | TypeofState typeof_state, |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 119 | JumpTarget* true_target, |
| 120 | JumpTarget* false_target); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 121 | |
| 122 | // Destroy a code generator state and restore the owning code generator's |
| 123 | // previous state. |
| 124 | ~CodeGenState(); |
| 125 | |
| 126 | TypeofState typeof_state() const { return typeof_state_; } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 127 | JumpTarget* true_target() const { return true_target_; } |
| 128 | JumpTarget* false_target() const { return false_target_; } |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 129 | |
| 130 | private: |
| 131 | CodeGenerator* owner_; |
| 132 | TypeofState typeof_state_; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 133 | JumpTarget* true_target_; |
| 134 | JumpTarget* false_target_; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 135 | CodeGenState* previous_; |
| 136 | }; |
| 137 | |
| 138 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 139 | // ------------------------------------------------------------------------- |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 140 | // CodeGenerator |
| 141 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 142 | class CodeGenerator: public AstVisitor { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 143 | public: |
| 144 | // Takes a function literal, generates code for it. This function should only |
| 145 | // be called by compiler.cc. |
| 146 | static Handle<Code> MakeCode(FunctionLiteral* fun, |
| 147 | Handle<Script> script, |
| 148 | bool is_eval); |
| 149 | |
christian.plesner.hansen@gmail.com | aca4968 | 2009-01-07 14:29:04 +0000 | [diff] [blame] | 150 | #ifdef ENABLE_LOGGING_AND_PROFILING |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 151 | static bool ShouldGenerateLog(Expression* type); |
christian.plesner.hansen@gmail.com | aca4968 | 2009-01-07 14:29:04 +0000 | [diff] [blame] | 152 | #endif |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 153 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 154 | static void SetFunctionInfo(Handle<JSFunction> fun, |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame^] | 155 | FunctionLiteral* lit, |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 156 | bool is_toplevel, |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame^] | 157 | Handle<Script> script); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 158 | |
| 159 | // Accessors |
| 160 | MacroAssembler* masm() { return masm_; } |
| 161 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 162 | VirtualFrame* frame() const { return frame_; } |
| 163 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 164 | bool has_valid_frame() const { return frame_ != NULL; } |
| 165 | |
| 166 | // Set the virtual frame to be new_frame, with non-frame register |
| 167 | // reference counts given by non_frame_registers. The non-frame |
| 168 | // register reference counts of the old frame are returned in |
| 169 | // non_frame_registers. |
| 170 | void SetFrame(VirtualFrame* new_frame, RegisterFile* non_frame_registers); |
| 171 | |
| 172 | void DeleteFrame(); |
| 173 | |
| 174 | RegisterAllocator* allocator() const { return allocator_; } |
| 175 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 176 | CodeGenState* state() { return state_; } |
| 177 | void set_state(CodeGenState* state) { state_ = state; } |
| 178 | |
| 179 | void AddDeferred(DeferredCode* code) { deferred_.Add(code); } |
| 180 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 181 | static const int kUnknownIntValue = -1; |
| 182 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 183 | private: |
| 184 | // Construction/Destruction |
| 185 | CodeGenerator(int buffer_size, Handle<Script> script, bool is_eval); |
| 186 | virtual ~CodeGenerator() { delete masm_; } |
| 187 | |
| 188 | // Accessors |
| 189 | Scope* scope() const { return scope_; } |
| 190 | |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 191 | // Generating deferred code. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 192 | void ProcessDeferred(); |
| 193 | |
| 194 | bool is_eval() { return is_eval_; } |
| 195 | |
| 196 | // State |
| 197 | bool has_cc() const { return cc_reg_ != al; } |
| 198 | TypeofState typeof_state() const { return state_->typeof_state(); } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 199 | JumpTarget* true_target() const { return state_->true_target(); } |
| 200 | JumpTarget* false_target() const { return state_->false_target(); } |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 201 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 202 | // We don't track loop nesting level on ARM yet. |
| 203 | int loop_nesting() const { return 0; } |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 204 | |
| 205 | // Node visitors. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 206 | void VisitStatements(ZoneList<Statement*>* statements); |
| 207 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 208 | #define DEF_VISIT(type) \ |
| 209 | void Visit##type(type* node); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 210 | AST_NODE_LIST(DEF_VISIT) |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 211 | #undef DEF_VISIT |
| 212 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 213 | // Visit a statement and then spill the virtual frame if control flow can |
| 214 | // reach the end of the statement (ie, it does not exit via break, |
| 215 | // continue, return, or throw). This function is used temporarily while |
| 216 | // the code generator is being transformed. |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 217 | inline void VisitAndSpill(Statement* statement); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 218 | |
| 219 | // Visit a list of statements and then spill the virtual frame if control |
| 220 | // flow can reach the end of the list. |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 221 | inline void VisitStatementsAndSpill(ZoneList<Statement*>* statements); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 222 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 223 | // Main code generation function |
| 224 | void GenCode(FunctionLiteral* fun); |
| 225 | |
| 226 | // The following are used by class Reference. |
| 227 | void LoadReference(Reference* ref); |
| 228 | void UnloadReference(Reference* ref); |
| 229 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 230 | MemOperand ContextOperand(Register context, int index) const { |
| 231 | return MemOperand(context, Context::SlotOffset(index)); |
| 232 | } |
| 233 | |
| 234 | MemOperand SlotOperand(Slot* slot, Register tmp); |
| 235 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 236 | MemOperand ContextSlotOperandCheckExtensions(Slot* slot, |
| 237 | Register tmp, |
| 238 | Register tmp2, |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 239 | JumpTarget* slow); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 240 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 241 | // Expressions |
| 242 | MemOperand GlobalObject() const { |
| 243 | return ContextOperand(cp, Context::GLOBAL_INDEX); |
| 244 | } |
| 245 | |
| 246 | void LoadCondition(Expression* x, |
| 247 | TypeofState typeof_state, |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 248 | JumpTarget* true_target, |
| 249 | JumpTarget* false_target, |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 250 | bool force_cc); |
| 251 | void Load(Expression* x, TypeofState typeof_state = NOT_INSIDE_TYPEOF); |
| 252 | void LoadGlobal(); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 253 | void LoadGlobalReceiver(Register scratch); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 254 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 255 | // Generate code to push the value of an expression on top of the frame |
| 256 | // and then spill the frame fully to memory. This function is used |
| 257 | // temporarily while the code generator is being transformed. |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 258 | inline void LoadAndSpill(Expression* expression, |
| 259 | TypeofState typeof_state = NOT_INSIDE_TYPEOF); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 260 | |
| 261 | // Call LoadCondition and then spill the virtual frame unless control flow |
| 262 | // cannot reach the end of the expression (ie, by emitting only |
| 263 | // unconditional jumps to the control targets). |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 264 | inline void LoadConditionAndSpill(Expression* expression, |
| 265 | TypeofState typeof_state, |
| 266 | JumpTarget* true_target, |
| 267 | JumpTarget* false_target, |
| 268 | bool force_control); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 269 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 270 | // Read a value from a slot and leave it on top of the expression stack. |
| 271 | void LoadFromSlot(Slot* slot, TypeofState typeof_state); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 272 | void LoadFromGlobalSlotCheckExtensions(Slot* slot, |
| 273 | TypeofState typeof_state, |
| 274 | Register tmp, |
| 275 | Register tmp2, |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 276 | JumpTarget* slow); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 277 | |
| 278 | // Special code for typeof expressions: Unfortunately, we must |
| 279 | // be careful when loading the expression in 'typeof' |
| 280 | // expressions. We are not allowed to throw reference errors for |
| 281 | // non-existing properties of the global object, so we must make it |
| 282 | // look like an explicit property access, instead of an access |
| 283 | // through the context chain. |
| 284 | void LoadTypeofExpression(Expression* x); |
| 285 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 286 | void ToBoolean(JumpTarget* true_target, JumpTarget* false_target); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 287 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 288 | void GenericBinaryOperation(Token::Value op, |
| 289 | OverwriteMode overwrite_mode, |
| 290 | int known_rhs = kUnknownIntValue); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 291 | void Comparison(Condition cc, |
| 292 | Expression* left, |
| 293 | Expression* right, |
| 294 | bool strict = false); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 295 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 296 | void SmiOperation(Token::Value op, |
| 297 | Handle<Object> value, |
| 298 | bool reversed, |
| 299 | OverwriteMode mode); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 300 | |
| 301 | void CallWithArguments(ZoneList<Expression*>* arguments, int position); |
| 302 | |
| 303 | // Control flow |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 304 | void Branch(bool if_true, JumpTarget* target); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 305 | void CheckStack(); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 306 | |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 307 | struct InlineRuntimeLUT { |
| 308 | void (CodeGenerator::*method)(ZoneList<Expression*>*); |
| 309 | const char* name; |
| 310 | }; |
| 311 | |
| 312 | static InlineRuntimeLUT* FindInlineRuntimeLUT(Handle<String> name); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 313 | bool CheckForInlineRuntimeCall(CallRuntime* node); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 314 | static bool PatchInlineRuntimeEntry(Handle<String> name, |
| 315 | const InlineRuntimeLUT& new_entry, |
| 316 | InlineRuntimeLUT* old_entry); |
| 317 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 318 | Handle<JSFunction> BuildBoilerplate(FunctionLiteral* node); |
| 319 | void ProcessDeclarations(ZoneList<Declaration*>* declarations); |
| 320 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 321 | Handle<Code> ComputeCallInitialize(int argc, InLoopFlag in_loop); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 322 | |
| 323 | // Declare global variables and functions in the given array of |
| 324 | // name/value pairs. |
| 325 | void DeclareGlobals(Handle<FixedArray> pairs); |
| 326 | |
| 327 | // Instantiate the function boilerplate. |
| 328 | void InstantiateBoilerplate(Handle<JSFunction> boilerplate); |
| 329 | |
| 330 | // Support for type checks. |
| 331 | void GenerateIsSmi(ZoneList<Expression*>* args); |
| 332 | void GenerateIsNonNegativeSmi(ZoneList<Expression*>* args); |
| 333 | void GenerateIsArray(ZoneList<Expression*>* args); |
| 334 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 335 | // Support for construct call checks. |
| 336 | void GenerateIsConstructCall(ZoneList<Expression*>* args); |
| 337 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 338 | // Support for arguments.length and arguments[?]. |
| 339 | void GenerateArgumentsLength(ZoneList<Expression*>* args); |
| 340 | void GenerateArgumentsAccess(ZoneList<Expression*>* args); |
| 341 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 342 | // Support for accessing the class and value fields of an object. |
| 343 | void GenerateClassOf(ZoneList<Expression*>* args); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 344 | void GenerateValueOf(ZoneList<Expression*>* args); |
| 345 | void GenerateSetValueOf(ZoneList<Expression*>* args); |
| 346 | |
| 347 | // Fast support for charCodeAt(n). |
| 348 | void GenerateFastCharCodeAt(ZoneList<Expression*>* args); |
| 349 | |
| 350 | // Fast support for object equality testing. |
| 351 | void GenerateObjectEquals(ZoneList<Expression*>* args); |
| 352 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 353 | void GenerateLog(ZoneList<Expression*>* args); |
| 354 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 355 | // Fast support for Math.random(). |
| 356 | void GenerateRandomPositiveSmi(ZoneList<Expression*>* args); |
| 357 | |
| 358 | // Fast support for Math.sin and Math.cos. |
| 359 | enum MathOp { SIN, COS }; |
| 360 | void GenerateFastMathOp(MathOp op, ZoneList<Expression*>* args); |
| 361 | inline void GenerateMathSin(ZoneList<Expression*>* args); |
| 362 | inline void GenerateMathCos(ZoneList<Expression*>* args); |
| 363 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 364 | // Methods used to indicate which source code is generated for. Source |
| 365 | // positions are collected by the assembler and emitted with the relocation |
| 366 | // information. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 367 | void CodeForFunctionPosition(FunctionLiteral* fun); |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 368 | void CodeForReturnPosition(FunctionLiteral* fun); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 369 | void CodeForStatementPosition(AstNode* node); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 370 | void CodeForSourcePosition(int pos); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 371 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 372 | #ifdef DEBUG |
| 373 | // True if the registers are valid for entry to a block. |
| 374 | bool HasValidEntryRegisters(); |
| 375 | #endif |
| 376 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 377 | bool is_eval_; // Tells whether code is generated for eval. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 378 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 379 | Handle<Script> script_; |
| 380 | List<DeferredCode*> deferred_; |
| 381 | |
| 382 | // Assembler |
| 383 | MacroAssembler* masm_; // to generate code |
| 384 | |
| 385 | // Code generation state |
| 386 | Scope* scope_; |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 387 | VirtualFrame* frame_; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 388 | RegisterAllocator* allocator_; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 389 | Condition cc_reg_; |
| 390 | CodeGenState* state_; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 391 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 392 | // Jump targets |
| 393 | BreakTarget function_return_; |
| 394 | |
| 395 | // True if the function return is shadowed (ie, jumping to the target |
| 396 | // function_return_ does not jump to the true function return, but rather |
| 397 | // to some unlinking code). |
| 398 | bool function_return_is_shadowed_; |
| 399 | |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 400 | static InlineRuntimeLUT kInlineRuntimeLUT[]; |
| 401 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 402 | friend class VirtualFrame; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 403 | friend class JumpTarget; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 404 | friend class Reference; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 405 | |
| 406 | DISALLOW_COPY_AND_ASSIGN(CodeGenerator); |
| 407 | }; |
| 408 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 409 | |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 410 | class GenericBinaryOpStub : public CodeStub { |
| 411 | public: |
| 412 | GenericBinaryOpStub(Token::Value op, |
| 413 | OverwriteMode mode, |
| 414 | int constant_rhs = CodeGenerator::kUnknownIntValue) |
| 415 | : op_(op), |
| 416 | mode_(mode), |
| 417 | constant_rhs_(constant_rhs), |
| 418 | specialized_on_rhs_(RhsIsOneWeWantToOptimizeFor(op, constant_rhs)) { } |
| 419 | |
| 420 | private: |
| 421 | Token::Value op_; |
| 422 | OverwriteMode mode_; |
| 423 | int constant_rhs_; |
| 424 | bool specialized_on_rhs_; |
| 425 | |
| 426 | static const int kMaxKnownRhs = 0x40000000; |
| 427 | |
| 428 | // Minor key encoding in 16 bits. |
| 429 | class ModeBits: public BitField<OverwriteMode, 0, 2> {}; |
| 430 | class OpBits: public BitField<Token::Value, 2, 6> {}; |
| 431 | class KnownIntBits: public BitField<int, 8, 8> {}; |
| 432 | |
| 433 | Major MajorKey() { return GenericBinaryOp; } |
| 434 | int MinorKey() { |
| 435 | // Encode the parameters in a unique 16 bit value. |
| 436 | return OpBits::encode(op_) |
| 437 | | ModeBits::encode(mode_) |
| 438 | | KnownIntBits::encode(MinorKeyForKnownInt()); |
| 439 | } |
| 440 | |
| 441 | void Generate(MacroAssembler* masm); |
| 442 | void HandleNonSmiBitwiseOp(MacroAssembler* masm); |
| 443 | |
| 444 | static bool RhsIsOneWeWantToOptimizeFor(Token::Value op, int constant_rhs) { |
| 445 | if (constant_rhs == CodeGenerator::kUnknownIntValue) return false; |
| 446 | if (op == Token::DIV) return constant_rhs >= 2 && constant_rhs <= 3; |
| 447 | if (op == Token::MOD) { |
| 448 | if (constant_rhs <= 1) return false; |
| 449 | if (constant_rhs <= 10) return true; |
| 450 | if (constant_rhs <= kMaxKnownRhs && IsPowerOf2(constant_rhs)) return true; |
| 451 | return false; |
| 452 | } |
| 453 | return false; |
| 454 | } |
| 455 | |
| 456 | int MinorKeyForKnownInt() { |
| 457 | if (!specialized_on_rhs_) return 0; |
| 458 | if (constant_rhs_ <= 10) return constant_rhs_ + 1; |
| 459 | ASSERT(IsPowerOf2(constant_rhs_)); |
| 460 | int key = 12; |
| 461 | int d = constant_rhs_; |
| 462 | while ((d & 1) == 0) { |
| 463 | key++; |
| 464 | d >>= 1; |
| 465 | } |
| 466 | return key; |
| 467 | } |
| 468 | |
| 469 | const char* GetName() { |
| 470 | switch (op_) { |
| 471 | case Token::ADD: return "GenericBinaryOpStub_ADD"; |
| 472 | case Token::SUB: return "GenericBinaryOpStub_SUB"; |
| 473 | case Token::MUL: return "GenericBinaryOpStub_MUL"; |
| 474 | case Token::DIV: return "GenericBinaryOpStub_DIV"; |
| 475 | case Token::MOD: return "GenericBinaryOpStub_MOD"; |
| 476 | case Token::BIT_OR: return "GenericBinaryOpStub_BIT_OR"; |
| 477 | case Token::BIT_AND: return "GenericBinaryOpStub_BIT_AND"; |
| 478 | case Token::BIT_XOR: return "GenericBinaryOpStub_BIT_XOR"; |
| 479 | case Token::SAR: return "GenericBinaryOpStub_SAR"; |
| 480 | case Token::SHL: return "GenericBinaryOpStub_SHL"; |
| 481 | case Token::SHR: return "GenericBinaryOpStub_SHR"; |
| 482 | default: return "GenericBinaryOpStub"; |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | #ifdef DEBUG |
| 487 | void Print() { |
| 488 | if (!specialized_on_rhs_) { |
| 489 | PrintF("GenericBinaryOpStub (%s)\n", Token::String(op_)); |
| 490 | } else { |
| 491 | PrintF("GenericBinaryOpStub (%s by %d)\n", |
| 492 | Token::String(op_), |
| 493 | constant_rhs_); |
| 494 | } |
| 495 | } |
| 496 | #endif |
| 497 | }; |
| 498 | |
| 499 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 500 | } } // namespace v8::internal |
| 501 | |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 502 | #endif // V8_ARM_CODEGEN_ARM_H_ |