| Leon Clarke | 888f672 | 2010-01-27 15:57:47 +0000 | [diff] [blame^] | 1 | // Copyright 2010 the V8 project authors. All rights reserved. |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 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_ARM_CODEGEN_ARM_H_ |
| 29 | #define V8_ARM_CODEGEN_ARM_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 | |
| Leon Clarke | 888f672 | 2010-01-27 15:57:47 +0000 | [diff] [blame^] | 46 | // A reference is a C++ stack-allocated object that puts a |
| 47 | // reference on the virtual frame. The reference may be consumed |
| 48 | // by GetValue, TakeValue, SetValue, and Codegen::UnloadReference. |
| 49 | // When the lifetime (scope) of a valid reference ends, it must have |
| 50 | // been consumed, and be in state UNLOADED. |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 51 | class Reference BASE_EMBEDDED { |
| 52 | public: |
| 53 | // The values of the types is important, see size(). |
| Leon Clarke | 888f672 | 2010-01-27 15:57:47 +0000 | [diff] [blame^] | 54 | enum Type { UNLOADED = -2, ILLEGAL = -1, SLOT = 0, NAMED = 1, KEYED = 2 }; |
| 55 | Reference(CodeGenerator* cgen, |
| 56 | Expression* expression, |
| 57 | bool persist_after_get = false); |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 58 | ~Reference(); |
| 59 | |
| 60 | Expression* expression() const { return expression_; } |
| 61 | Type type() const { return type_; } |
| 62 | void set_type(Type value) { |
| Leon Clarke | 888f672 | 2010-01-27 15:57:47 +0000 | [diff] [blame^] | 63 | ASSERT_EQ(ILLEGAL, type_); |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 64 | type_ = value; |
| 65 | } |
| 66 | |
| Leon Clarke | 888f672 | 2010-01-27 15:57:47 +0000 | [diff] [blame^] | 67 | void set_unloaded() { |
| 68 | ASSERT_NE(ILLEGAL, type_); |
| 69 | ASSERT_NE(UNLOADED, type_); |
| 70 | type_ = UNLOADED; |
| 71 | } |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 72 | // The size the reference takes up on the stack. |
| Leon Clarke | 888f672 | 2010-01-27 15:57:47 +0000 | [diff] [blame^] | 73 | int size() const { |
| 74 | return (type_ < SLOT) ? 0 : type_; |
| 75 | } |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 76 | |
| 77 | bool is_illegal() const { return type_ == ILLEGAL; } |
| 78 | bool is_slot() const { return type_ == SLOT; } |
| 79 | bool is_property() const { return type_ == NAMED || type_ == KEYED; } |
| Leon Clarke | 888f672 | 2010-01-27 15:57:47 +0000 | [diff] [blame^] | 80 | bool is_unloaded() const { return type_ == UNLOADED; } |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 81 | |
| 82 | // Return the name. Only valid for named property references. |
| 83 | Handle<String> GetName(); |
| 84 | |
| 85 | // Generate code to push the value of the reference on top of the |
| 86 | // expression stack. The reference is expected to be already on top of |
| Leon Clarke | 888f672 | 2010-01-27 15:57:47 +0000 | [diff] [blame^] | 87 | // the expression stack, and it is consumed by the call unless the |
| 88 | // reference is for a compound assignment. |
| 89 | // If the reference is not consumed, it is left in place under its value. |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 90 | void GetValue(); |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 91 | |
| Leon Clarke | 888f672 | 2010-01-27 15:57:47 +0000 | [diff] [blame^] | 92 | // Generate code to pop a reference, push the value of the reference, |
| 93 | // and then spill the stack frame. |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 94 | inline void GetValueAndSpill(); |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 95 | |
| 96 | // Generate code to store the value on top of the expression stack in the |
| 97 | // reference. The reference is expected to be immediately below the value |
| Leon Clarke | 888f672 | 2010-01-27 15:57:47 +0000 | [diff] [blame^] | 98 | // on the expression stack. The value is stored in the location specified |
| 99 | // by the reference, and is left on top of the stack, after the reference |
| 100 | // is popped from beneath it (unloaded). |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 101 | void SetValue(InitState init_state); |
| 102 | |
| 103 | private: |
| 104 | CodeGenerator* cgen_; |
| 105 | Expression* expression_; |
| 106 | Type type_; |
| Leon Clarke | 888f672 | 2010-01-27 15:57:47 +0000 | [diff] [blame^] | 107 | // Keep the reference on the stack after get, so it can be used by set later. |
| 108 | bool persist_after_get_; |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 109 | }; |
| 110 | |
| 111 | |
| 112 | // ------------------------------------------------------------------------- |
| 113 | // Code generation state |
| 114 | |
| 115 | // The state is passed down the AST by the code generator (and back up, in |
| 116 | // the form of the state of the label pair). It is threaded through the |
| 117 | // call stack. Constructing a state implicitly pushes it on the owning code |
| 118 | // generator's stack of states, and destroying one implicitly pops it. |
| 119 | |
| 120 | class CodeGenState BASE_EMBEDDED { |
| 121 | public: |
| 122 | // Create an initial code generator state. Destroying the initial state |
| 123 | // leaves the code generator with a NULL state. |
| 124 | explicit CodeGenState(CodeGenerator* owner); |
| 125 | |
| 126 | // Create a code generator state based on a code generator's current |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 127 | // state. The new state has its own pair of branch labels. |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 128 | CodeGenState(CodeGenerator* owner, |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 129 | JumpTarget* true_target, |
| 130 | JumpTarget* false_target); |
| 131 | |
| 132 | // Destroy a code generator state and restore the owning code generator's |
| 133 | // previous state. |
| 134 | ~CodeGenState(); |
| 135 | |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 136 | JumpTarget* true_target() const { return true_target_; } |
| 137 | JumpTarget* false_target() const { return false_target_; } |
| 138 | |
| 139 | private: |
| 140 | CodeGenerator* owner_; |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 141 | JumpTarget* true_target_; |
| 142 | JumpTarget* false_target_; |
| 143 | CodeGenState* previous_; |
| 144 | }; |
| 145 | |
| 146 | |
| 147 | // ------------------------------------------------------------------------- |
| 148 | // CodeGenerator |
| 149 | |
| 150 | class CodeGenerator: public AstVisitor { |
| 151 | public: |
| 152 | // Takes a function literal, generates code for it. This function should only |
| 153 | // be called by compiler.cc. |
| 154 | static Handle<Code> MakeCode(FunctionLiteral* fun, |
| 155 | Handle<Script> script, |
| 156 | bool is_eval); |
| 157 | |
| Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 158 | // Printing of AST, etc. as requested by flags. |
| 159 | static void MakeCodePrologue(FunctionLiteral* fun); |
| 160 | |
| 161 | // Allocate and install the code. |
| 162 | static Handle<Code> MakeCodeEpilogue(FunctionLiteral* fun, |
| 163 | MacroAssembler* masm, |
| 164 | Code::Flags flags, |
| 165 | Handle<Script> script); |
| 166 | |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 167 | #ifdef ENABLE_LOGGING_AND_PROFILING |
| 168 | static bool ShouldGenerateLog(Expression* type); |
| 169 | #endif |
| 170 | |
| 171 | static void SetFunctionInfo(Handle<JSFunction> fun, |
| 172 | FunctionLiteral* lit, |
| 173 | bool is_toplevel, |
| 174 | Handle<Script> script); |
| 175 | |
| Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 176 | static void RecordPositions(MacroAssembler* masm, int pos); |
| 177 | |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 178 | // Accessors |
| 179 | MacroAssembler* masm() { return masm_; } |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 180 | VirtualFrame* frame() const { return frame_; } |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 181 | Handle<Script> script() { return script_; } |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 182 | |
| 183 | bool has_valid_frame() const { return frame_ != NULL; } |
| 184 | |
| 185 | // Set the virtual frame to be new_frame, with non-frame register |
| 186 | // reference counts given by non_frame_registers. The non-frame |
| 187 | // register reference counts of the old frame are returned in |
| 188 | // non_frame_registers. |
| 189 | void SetFrame(VirtualFrame* new_frame, RegisterFile* non_frame_registers); |
| 190 | |
| 191 | void DeleteFrame(); |
| 192 | |
| 193 | RegisterAllocator* allocator() const { return allocator_; } |
| 194 | |
| 195 | CodeGenState* state() { return state_; } |
| 196 | void set_state(CodeGenState* state) { state_ = state; } |
| 197 | |
| 198 | void AddDeferred(DeferredCode* code) { deferred_.Add(code); } |
| 199 | |
| 200 | static const int kUnknownIntValue = -1; |
| 201 | |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 202 | private: |
| 203 | // Construction/Destruction |
| 204 | CodeGenerator(int buffer_size, Handle<Script> script, bool is_eval); |
| 205 | virtual ~CodeGenerator() { delete masm_; } |
| 206 | |
| 207 | // Accessors |
| 208 | Scope* scope() const { return scope_; } |
| 209 | |
| 210 | // Generating deferred code. |
| 211 | void ProcessDeferred(); |
| 212 | |
| 213 | bool is_eval() { return is_eval_; } |
| 214 | |
| 215 | // State |
| 216 | bool has_cc() const { return cc_reg_ != al; } |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 217 | JumpTarget* true_target() const { return state_->true_target(); } |
| 218 | JumpTarget* false_target() const { return state_->false_target(); } |
| 219 | |
| 220 | // We don't track loop nesting level on ARM yet. |
| 221 | int loop_nesting() const { return 0; } |
| 222 | |
| 223 | // Node visitors. |
| 224 | void VisitStatements(ZoneList<Statement*>* statements); |
| 225 | |
| 226 | #define DEF_VISIT(type) \ |
| 227 | void Visit##type(type* node); |
| 228 | AST_NODE_LIST(DEF_VISIT) |
| 229 | #undef DEF_VISIT |
| 230 | |
| 231 | // Visit a statement and then spill the virtual frame if control flow can |
| 232 | // reach the end of the statement (ie, it does not exit via break, |
| 233 | // continue, return, or throw). This function is used temporarily while |
| 234 | // the code generator is being transformed. |
| 235 | inline void VisitAndSpill(Statement* statement); |
| 236 | |
| 237 | // Visit a list of statements and then spill the virtual frame if control |
| 238 | // flow can reach the end of the list. |
| 239 | inline void VisitStatementsAndSpill(ZoneList<Statement*>* statements); |
| 240 | |
| 241 | // Main code generation function |
| 242 | void GenCode(FunctionLiteral* fun); |
| 243 | |
| 244 | // The following are used by class Reference. |
| 245 | void LoadReference(Reference* ref); |
| 246 | void UnloadReference(Reference* ref); |
| 247 | |
| Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 248 | static MemOperand ContextOperand(Register context, int index) { |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 249 | return MemOperand(context, Context::SlotOffset(index)); |
| 250 | } |
| 251 | |
| 252 | MemOperand SlotOperand(Slot* slot, Register tmp); |
| 253 | |
| 254 | MemOperand ContextSlotOperandCheckExtensions(Slot* slot, |
| 255 | Register tmp, |
| 256 | Register tmp2, |
| 257 | JumpTarget* slow); |
| 258 | |
| 259 | // Expressions |
| Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 260 | static MemOperand GlobalObject() { |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 261 | return ContextOperand(cp, Context::GLOBAL_INDEX); |
| 262 | } |
| 263 | |
| 264 | void LoadCondition(Expression* x, |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 265 | JumpTarget* true_target, |
| 266 | JumpTarget* false_target, |
| 267 | bool force_cc); |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 268 | void Load(Expression* expr); |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 269 | void LoadGlobal(); |
| 270 | void LoadGlobalReceiver(Register scratch); |
| 271 | |
| 272 | // Generate code to push the value of an expression on top of the frame |
| 273 | // and then spill the frame fully to memory. This function is used |
| 274 | // temporarily while the code generator is being transformed. |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 275 | inline void LoadAndSpill(Expression* expression); |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 276 | |
| 277 | // Call LoadCondition and then spill the virtual frame unless control flow |
| 278 | // cannot reach the end of the expression (ie, by emitting only |
| 279 | // unconditional jumps to the control targets). |
| 280 | inline void LoadConditionAndSpill(Expression* expression, |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 281 | JumpTarget* true_target, |
| 282 | JumpTarget* false_target, |
| 283 | bool force_control); |
| 284 | |
| 285 | // Read a value from a slot and leave it on top of the expression stack. |
| 286 | void LoadFromSlot(Slot* slot, TypeofState typeof_state); |
| Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 287 | // Store the value on top of the stack to a slot. |
| 288 | void StoreToSlot(Slot* slot, InitState init_state); |
| Leon Clarke | 888f672 | 2010-01-27 15:57:47 +0000 | [diff] [blame^] | 289 | // Load a keyed property, leaving it in r0. The receiver and key are |
| 290 | // passed on the stack, and remain there. |
| 291 | void EmitKeyedLoad(bool is_global); |
| Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 292 | |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 293 | void LoadFromGlobalSlotCheckExtensions(Slot* slot, |
| 294 | TypeofState typeof_state, |
| 295 | Register tmp, |
| 296 | Register tmp2, |
| 297 | JumpTarget* slow); |
| 298 | |
| 299 | // Special code for typeof expressions: Unfortunately, we must |
| 300 | // be careful when loading the expression in 'typeof' |
| 301 | // expressions. We are not allowed to throw reference errors for |
| 302 | // non-existing properties of the global object, so we must make it |
| 303 | // look like an explicit property access, instead of an access |
| 304 | // through the context chain. |
| 305 | void LoadTypeofExpression(Expression* x); |
| 306 | |
| 307 | void ToBoolean(JumpTarget* true_target, JumpTarget* false_target); |
| 308 | |
| 309 | void GenericBinaryOperation(Token::Value op, |
| 310 | OverwriteMode overwrite_mode, |
| 311 | int known_rhs = kUnknownIntValue); |
| 312 | void Comparison(Condition cc, |
| 313 | Expression* left, |
| 314 | Expression* right, |
| 315 | bool strict = false); |
| 316 | |
| 317 | void SmiOperation(Token::Value op, |
| 318 | Handle<Object> value, |
| 319 | bool reversed, |
| 320 | OverwriteMode mode); |
| 321 | |
| Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 322 | void CallWithArguments(ZoneList<Expression*>* arguments, |
| 323 | CallFunctionFlags flags, |
| 324 | int position); |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 325 | |
| 326 | // Control flow |
| 327 | void Branch(bool if_true, JumpTarget* target); |
| 328 | void CheckStack(); |
| 329 | |
| 330 | struct InlineRuntimeLUT { |
| 331 | void (CodeGenerator::*method)(ZoneList<Expression*>*); |
| 332 | const char* name; |
| 333 | }; |
| 334 | |
| 335 | static InlineRuntimeLUT* FindInlineRuntimeLUT(Handle<String> name); |
| 336 | bool CheckForInlineRuntimeCall(CallRuntime* node); |
| 337 | static bool PatchInlineRuntimeEntry(Handle<String> name, |
| 338 | const InlineRuntimeLUT& new_entry, |
| 339 | InlineRuntimeLUT* old_entry); |
| 340 | |
| Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 341 | static Handle<Code> ComputeLazyCompile(int argc); |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 342 | void ProcessDeclarations(ZoneList<Declaration*>* declarations); |
| 343 | |
| Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 344 | static Handle<Code> ComputeCallInitialize(int argc, InLoopFlag in_loop); |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 345 | |
| 346 | // Declare global variables and functions in the given array of |
| 347 | // name/value pairs. |
| 348 | void DeclareGlobals(Handle<FixedArray> pairs); |
| 349 | |
| 350 | // Instantiate the function boilerplate. |
| 351 | void InstantiateBoilerplate(Handle<JSFunction> boilerplate); |
| 352 | |
| 353 | // Support for type checks. |
| 354 | void GenerateIsSmi(ZoneList<Expression*>* args); |
| 355 | void GenerateIsNonNegativeSmi(ZoneList<Expression*>* args); |
| 356 | void GenerateIsArray(ZoneList<Expression*>* args); |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 357 | void GenerateIsObject(ZoneList<Expression*>* args); |
| 358 | void GenerateIsFunction(ZoneList<Expression*>* args); |
| Leon Clarke | 888f672 | 2010-01-27 15:57:47 +0000 | [diff] [blame^] | 359 | void GenerateIsUndetectableObject(ZoneList<Expression*>* args); |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 360 | |
| 361 | // Support for construct call checks. |
| 362 | void GenerateIsConstructCall(ZoneList<Expression*>* args); |
| 363 | |
| 364 | // Support for arguments.length and arguments[?]. |
| 365 | void GenerateArgumentsLength(ZoneList<Expression*>* args); |
| 366 | void GenerateArgumentsAccess(ZoneList<Expression*>* args); |
| 367 | |
| 368 | // Support for accessing the class and value fields of an object. |
| 369 | void GenerateClassOf(ZoneList<Expression*>* args); |
| 370 | void GenerateValueOf(ZoneList<Expression*>* args); |
| 371 | void GenerateSetValueOf(ZoneList<Expression*>* args); |
| 372 | |
| 373 | // Fast support for charCodeAt(n). |
| 374 | void GenerateFastCharCodeAt(ZoneList<Expression*>* args); |
| 375 | |
| 376 | // Fast support for object equality testing. |
| 377 | void GenerateObjectEquals(ZoneList<Expression*>* args); |
| 378 | |
| 379 | void GenerateLog(ZoneList<Expression*>* args); |
| 380 | |
| 381 | // Fast support for Math.random(). |
| 382 | void GenerateRandomPositiveSmi(ZoneList<Expression*>* args); |
| 383 | |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 384 | // Fast support for StringAdd. |
| 385 | void GenerateStringAdd(ZoneList<Expression*>* args); |
| 386 | |
| Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 387 | // Fast support for SubString. |
| 388 | void GenerateSubString(ZoneList<Expression*>* args); |
| 389 | |
| 390 | // Fast support for StringCompare. |
| 391 | void GenerateStringCompare(ZoneList<Expression*>* args); |
| 392 | |
| 393 | // Support for direct calls from JavaScript to native RegExp code. |
| 394 | void GenerateRegExpExec(ZoneList<Expression*>* args); |
| 395 | |
| Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 396 | // Simple condition analysis. |
| 397 | enum ConditionAnalysis { |
| 398 | ALWAYS_TRUE, |
| 399 | ALWAYS_FALSE, |
| 400 | DONT_KNOW |
| 401 | }; |
| 402 | ConditionAnalysis AnalyzeCondition(Expression* cond); |
| 403 | |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 404 | // Methods used to indicate which source code is generated for. Source |
| 405 | // positions are collected by the assembler and emitted with the relocation |
| 406 | // information. |
| 407 | void CodeForFunctionPosition(FunctionLiteral* fun); |
| 408 | void CodeForReturnPosition(FunctionLiteral* fun); |
| 409 | void CodeForStatementPosition(Statement* node); |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 410 | void CodeForDoWhileConditionPosition(DoWhileStatement* stmt); |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 411 | void CodeForSourcePosition(int pos); |
| 412 | |
| 413 | #ifdef DEBUG |
| 414 | // True if the registers are valid for entry to a block. |
| 415 | bool HasValidEntryRegisters(); |
| 416 | #endif |
| 417 | |
| 418 | bool is_eval_; // Tells whether code is generated for eval. |
| 419 | |
| 420 | Handle<Script> script_; |
| 421 | List<DeferredCode*> deferred_; |
| 422 | |
| 423 | // Assembler |
| 424 | MacroAssembler* masm_; // to generate code |
| 425 | |
| 426 | // Code generation state |
| 427 | Scope* scope_; |
| 428 | VirtualFrame* frame_; |
| 429 | RegisterAllocator* allocator_; |
| 430 | Condition cc_reg_; |
| 431 | CodeGenState* state_; |
| 432 | |
| 433 | // Jump targets |
| 434 | BreakTarget function_return_; |
| 435 | |
| 436 | // True if the function return is shadowed (ie, jumping to the target |
| 437 | // function_return_ does not jump to the true function return, but rather |
| 438 | // to some unlinking code). |
| 439 | bool function_return_is_shadowed_; |
| 440 | |
| 441 | static InlineRuntimeLUT kInlineRuntimeLUT[]; |
| 442 | |
| 443 | friend class VirtualFrame; |
| 444 | friend class JumpTarget; |
| 445 | friend class Reference; |
| Leon Clarke | 888f672 | 2010-01-27 15:57:47 +0000 | [diff] [blame^] | 446 | friend class FullCodeGenerator; |
| 447 | friend class FullCodeGenSyntaxChecker; |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 448 | |
| 449 | DISALLOW_COPY_AND_ASSIGN(CodeGenerator); |
| 450 | }; |
| 451 | |
| 452 | |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 453 | class GenericBinaryOpStub : public CodeStub { |
| 454 | public: |
| 455 | GenericBinaryOpStub(Token::Value op, |
| 456 | OverwriteMode mode, |
| 457 | int constant_rhs = CodeGenerator::kUnknownIntValue) |
| 458 | : op_(op), |
| 459 | mode_(mode), |
| 460 | constant_rhs_(constant_rhs), |
| Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 461 | specialized_on_rhs_(RhsIsOneWeWantToOptimizeFor(op, constant_rhs)), |
| 462 | name_(NULL) { } |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 463 | |
| 464 | private: |
| 465 | Token::Value op_; |
| 466 | OverwriteMode mode_; |
| 467 | int constant_rhs_; |
| 468 | bool specialized_on_rhs_; |
| Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 469 | char* name_; |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 470 | |
| 471 | static const int kMaxKnownRhs = 0x40000000; |
| 472 | |
| 473 | // Minor key encoding in 16 bits. |
| 474 | class ModeBits: public BitField<OverwriteMode, 0, 2> {}; |
| 475 | class OpBits: public BitField<Token::Value, 2, 6> {}; |
| 476 | class KnownIntBits: public BitField<int, 8, 8> {}; |
| 477 | |
| 478 | Major MajorKey() { return GenericBinaryOp; } |
| 479 | int MinorKey() { |
| 480 | // Encode the parameters in a unique 16 bit value. |
| 481 | return OpBits::encode(op_) |
| 482 | | ModeBits::encode(mode_) |
| 483 | | KnownIntBits::encode(MinorKeyForKnownInt()); |
| 484 | } |
| 485 | |
| 486 | void Generate(MacroAssembler* masm); |
| 487 | void HandleNonSmiBitwiseOp(MacroAssembler* masm); |
| 488 | |
| 489 | static bool RhsIsOneWeWantToOptimizeFor(Token::Value op, int constant_rhs) { |
| 490 | if (constant_rhs == CodeGenerator::kUnknownIntValue) return false; |
| 491 | if (op == Token::DIV) return constant_rhs >= 2 && constant_rhs <= 3; |
| 492 | if (op == Token::MOD) { |
| 493 | if (constant_rhs <= 1) return false; |
| 494 | if (constant_rhs <= 10) return true; |
| 495 | if (constant_rhs <= kMaxKnownRhs && IsPowerOf2(constant_rhs)) return true; |
| 496 | return false; |
| 497 | } |
| 498 | return false; |
| 499 | } |
| 500 | |
| 501 | int MinorKeyForKnownInt() { |
| 502 | if (!specialized_on_rhs_) return 0; |
| 503 | if (constant_rhs_ <= 10) return constant_rhs_ + 1; |
| 504 | ASSERT(IsPowerOf2(constant_rhs_)); |
| 505 | int key = 12; |
| 506 | int d = constant_rhs_; |
| 507 | while ((d & 1) == 0) { |
| 508 | key++; |
| 509 | d >>= 1; |
| 510 | } |
| 511 | return key; |
| 512 | } |
| 513 | |
| Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 514 | const char* GetName(); |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 515 | |
| 516 | #ifdef DEBUG |
| 517 | void Print() { |
| 518 | if (!specialized_on_rhs_) { |
| 519 | PrintF("GenericBinaryOpStub (%s)\n", Token::String(op_)); |
| 520 | } else { |
| 521 | PrintF("GenericBinaryOpStub (%s by %d)\n", |
| 522 | Token::String(op_), |
| 523 | constant_rhs_); |
| 524 | } |
| 525 | } |
| 526 | #endif |
| 527 | }; |
| 528 | |
| 529 | |
| Leon Clarke | 888f672 | 2010-01-27 15:57:47 +0000 | [diff] [blame^] | 530 | class StringCompareStub: public CodeStub { |
| 531 | public: |
| 532 | StringCompareStub() { } |
| 533 | |
| 534 | // Compare two flat ASCII strings and returns result in r0. |
| 535 | // Does not use the stack. |
| 536 | static void GenerateCompareFlatAsciiStrings(MacroAssembler* masm, |
| 537 | Register left, |
| 538 | Register right, |
| 539 | Register scratch1, |
| 540 | Register scratch2, |
| 541 | Register scratch3, |
| 542 | Register scratch4); |
| 543 | |
| 544 | private: |
| 545 | Major MajorKey() { return StringCompare; } |
| 546 | int MinorKey() { return 0; } |
| 547 | |
| 548 | void Generate(MacroAssembler* masm); |
| 549 | }; |
| 550 | |
| 551 | |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 552 | } } // namespace v8::internal |
| 553 | |
| 554 | #endif // V8_ARM_CODEGEN_ARM_H_ |