| 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 |
| 11 // with the distribution. | 11 // with the distribution. |
| 12 // * Neither the name of Google Inc. nor the names of its | 12 // * Neither the name of Google Inc. nor the names of its |
| 13 // contributors may be used to endorse or promote products derived | 13 // contributors may be used to endorse or promote products derived |
| 14 // from this software without specific prior written permission. | 14 // from this software without specific prior written permission. |
| 15 // | 15 // |
| 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | 27 |
| 28 #ifndef V8_CODEGEN_H_ | 28 #ifndef V8_CODEGEN_H_ |
| 29 #define V8_CODEGEN_H_ |
| 29 | 30 |
| 30 #include "ast.h" | 31 #include "ast.h" |
| 31 #include "code-stubs.h" | 32 #include "code-stubs.h" |
| 32 #include "runtime.h" | 33 #include "runtime.h" |
| 33 | 34 |
| 34 #define V8_CODEGEN_H_ | 35 // Include the declaration of the architecture defined class CodeGenerator. |
| 36 // The contract to the shared code is that the the CodeGenerator is a subclass |
| 37 // of Visitor and that the following methods are available publicly: |
| 38 // CodeGenerator::MakeCode |
| 39 // CodeGenerator::SetFunctionInfo |
| 40 // CodeGenerator::AddDeferred |
| 41 // CodeGenerator::masm |
| 42 // |
| 43 // These methods are either used privately by the shared code or implemented as |
| 44 // shared code: |
| 45 // CodeGenerator::CodeGenerator |
| 46 // CodeGenerator::~CodeGenerator |
| 47 // CodeGenerator::ProcessDeferred |
| 48 // CodeGenerator::GenCode |
| 49 // CodeGenerator::BuildBoilerplate |
| 50 // CodeGenerator::ComputeCallInitialize |
| 51 // CodeGenerator::ProcessDeclarations |
| 52 // CodeGenerator::DeclareGlobals |
| 53 // CodeGenerator::CheckForInlineRuntimeCall |
| 54 // CodeGenerator::GenerateFastCaseSwitchStatement |
| 55 // CodeGenerator::GenerateFastCaseSwitchCases |
| 56 // CodeGenerator::TryGenerateFastCaseSwitchStatement |
| 57 // CodeGenerator::GenerateFastCaseSwitchJumpTable |
| 58 // CodeGenerator::FastCaseSwitchMinCaseCount |
| 59 // CodeGenerator::FastCaseSwitchMaxOverheadFactor |
| 60 |
| 61 #if defined(ARM) |
| 62 #include "codegen-arm.h" |
| 63 #else |
| 64 #include "codegen-ia32.h" |
| 65 #endif |
| 35 | 66 |
| 36 namespace v8 { namespace internal { | 67 namespace v8 { namespace internal { |
| 37 | 68 |
| 38 | 69 |
| 39 // Use lazy compilation; defaults to true. | 70 // Use lazy compilation; defaults to true. |
| 40 // NOTE: Do not remove non-lazy compilation until we can properly | 71 // NOTE: Do not remove non-lazy compilation until we can properly |
| 41 // install extensions with lazy compilation enabled. At the | 72 // install extensions with lazy compilation enabled. At the |
| 42 // moment, this doesn't work for the extensions in Google3, | 73 // moment, this doesn't work for the extensions in Google3, |
| 43 // and we can only run the tests with --nolazy. | 74 // and we can only run the tests with --nolazy. |
| 44 | 75 |
| 45 | 76 |
| 46 // Forward declaration. | |
| 47 class CodeGenerator; | |
| 48 | |
| 49 | |
| 50 // Deferred code objects are small pieces of code that are compiled | 77 // Deferred code objects are small pieces of code that are compiled |
| 51 // out of line. They are used to defer the compilation of uncommon | 78 // out of line. They are used to defer the compilation of uncommon |
| 52 // paths thereby avoiding expensive jumps around uncommon code parts. | 79 // paths thereby avoiding expensive jumps around uncommon code parts. |
| 53 class DeferredCode: public ZoneObject { | 80 class DeferredCode: public ZoneObject { |
| 54 public: | 81 public: |
| 55 explicit DeferredCode(CodeGenerator* generator); | 82 explicit DeferredCode(CodeGenerator* generator); |
| 56 virtual ~DeferredCode() { } | 83 virtual ~DeferredCode() { } |
| 57 | 84 |
| 58 virtual void Generate() = 0; | 85 virtual void Generate() = 0; |
| 59 | 86 |
| (...skipping 26 matching lines...) Expand all Loading... |
| 86 Label exit_; | 113 Label exit_; |
| 87 int statement_position_; | 114 int statement_position_; |
| 88 int position_; | 115 int position_; |
| 89 #ifdef DEBUG | 116 #ifdef DEBUG |
| 90 const char* comment_; | 117 const char* comment_; |
| 91 #endif | 118 #endif |
| 92 DISALLOW_COPY_AND_ASSIGN(DeferredCode); | 119 DISALLOW_COPY_AND_ASSIGN(DeferredCode); |
| 93 }; | 120 }; |
| 94 | 121 |
| 95 | 122 |
| 96 // A superclass for code generators. The implementations of methods | |
| 97 // declared in this class are partially in codegen.c and partially in | |
| 98 // codegen_<arch>.c. | |
| 99 class CodeGenerator: public Visitor { | |
| 100 public: | |
| 101 CodeGenerator(bool is_eval, | |
| 102 Handle<Script> script) | |
| 103 : is_eval_(is_eval), | |
| 104 script_(script), | |
| 105 deferred_(8) { } | |
| 106 | |
| 107 | |
| 108 // The code generator: Takes a function literal, generates code for it, | |
| 109 // and assembles it all into a Code* object. This function should only | |
| 110 // be called by compiler.cc. | |
| 111 static Handle<Code> MakeCode(FunctionLiteral* fun, | |
| 112 Handle<Script> script, | |
| 113 bool is_eval); | |
| 114 | |
| 115 static void SetFunctionInfo(Handle<JSFunction> fun, | |
| 116 int length, | |
| 117 int function_token_position, | |
| 118 int start_position, | |
| 119 int end_position, | |
| 120 bool is_expression, | |
| 121 bool is_toplevel, | |
| 122 Handle<Script> script); | |
| 123 | |
| 124 virtual MacroAssembler* masm() = 0; | |
| 125 | |
| 126 virtual Scope* scope() const = 0; | |
| 127 | |
| 128 void AddDeferred(DeferredCode* code) { deferred_.Add(code); } | |
| 129 void ProcessDeferred(); | |
| 130 | |
| 131 // Accessors for is_eval. | |
| 132 bool is_eval() { return is_eval_; } | |
| 133 | |
| 134 // Abstract node visitors. | |
| 135 #define DEF_VISIT(type) \ | |
| 136 virtual void Visit##type(type* node) = 0; | |
| 137 NODE_LIST(DEF_VISIT) | |
| 138 #undef DEF_VISIT | |
| 139 | |
| 140 protected: | |
| 141 bool CheckForInlineRuntimeCall(CallRuntime* node); | |
| 142 Handle<JSFunction> BuildBoilerplate(FunctionLiteral* node); | |
| 143 void ProcessDeclarations(ZoneList<Declaration*>* declarations); | |
| 144 | |
| 145 Handle<Code> ComputeCallInitialize(int argc); | |
| 146 | |
| 147 // Declare global variables and functions in the given array of | |
| 148 // name/value pairs. | |
| 149 virtual void DeclareGlobals(Handle<FixedArray> pairs) = 0; | |
| 150 | |
| 151 // Support for type checks. | |
| 152 virtual void GenerateIsSmi(ZoneList<Expression*>* args) = 0; | |
| 153 virtual void GenerateIsNonNegativeSmi(ZoneList<Expression*>* args) = 0; | |
| 154 virtual void GenerateIsArray(ZoneList<Expression*>* args) = 0; | |
| 155 | |
| 156 // Support for arguments.length and arguments[?]. | |
| 157 virtual void GenerateArgumentsLength(ZoneList<Expression*>* args) = 0; | |
| 158 virtual void GenerateArgumentsAccess(ZoneList<Expression*>* args) = 0; | |
| 159 | |
| 160 // Support for accessing the value field of an object (used by Date). | |
| 161 virtual void GenerateValueOf(ZoneList<Expression*>* args) = 0; | |
| 162 virtual void GenerateSetValueOf(ZoneList<Expression*>* args) = 0; | |
| 163 | |
| 164 // Fast support for charCodeAt(n). | |
| 165 virtual void GenerateFastCharCodeAt(ZoneList<Expression*>* args) = 0; | |
| 166 | |
| 167 // Fast support for object equality testing. | |
| 168 virtual void GenerateObjectEquals(ZoneList<Expression*>* args) = 0; | |
| 169 | |
| 170 | |
| 171 // Multiple methods for fast case switch statement support. | |
| 172 | |
| 173 // The limit of the range of a fast-case switch, as a factor of the number | |
| 174 // of cases of the switch. Each platform should return a value that | |
| 175 // is optimal compared to the default code generated for a switch statement | |
| 176 // on that platform. | |
| 177 virtual int FastCaseSwitchMaxOverheadFactor() = 0; | |
| 178 | |
| 179 // The minimal number of cases in a switch before the fast-case switch | |
| 180 // optimization is enabled. Each platform should return a value that | |
| 181 // is optimal compared to the default code generated for a switch statement | |
| 182 // on that platform. | |
| 183 virtual int FastCaseSwitchMinCaseCount() = 0; | |
| 184 | |
| 185 // Allocate a jump table and create code to jump through it. | |
| 186 // Should call GenerateFastCaseSwitchCases to generate the code for | |
| 187 // all the cases at the appropriate point. | |
| 188 virtual void GenerateFastCaseSwitchJumpTable( | |
| 189 SwitchStatement* node, int min_index, int range, Label *fail_label, | |
| 190 SmartPointer<Label*> &case_targets, SmartPointer<Label>& case_labels) = 0; | |
| 191 | |
| 192 // Generate the code for cases for the fast case switch. | |
| 193 // Called by GenerateFastCaseSwitchJumpTable. | |
| 194 virtual void GenerateFastCaseSwitchCases( | |
| 195 SwitchStatement* node, SmartPointer<Label> &case_labels); | |
| 196 | |
| 197 // Fast support for constant-Smi switches. | |
| 198 virtual void GenerateFastCaseSwitchStatement( | |
| 199 SwitchStatement *node, int min_index, int range, int default_index); | |
| 200 | |
| 201 // Fast support for constant-Smi switches. Tests whether switch statement | |
| 202 // permits optimization and calls GenerateFastCaseSwitch if it does. | |
| 203 // Returns true if the fast-case switch was generated, and false if not. | |
| 204 virtual bool TryGenerateFastCaseSwitchStatement(SwitchStatement *node); | |
| 205 | |
| 206 | |
| 207 private: | |
| 208 bool is_eval_; // Tells whether code is generated for eval. | |
| 209 Handle<Script> script_; | |
| 210 List<DeferredCode*> deferred_; | |
| 211 }; | |
| 212 | |
| 213 | |
| 214 // RuntimeStub models code stubs calling entry points in the Runtime class. | 123 // RuntimeStub models code stubs calling entry points in the Runtime class. |
| 215 class RuntimeStub : public CodeStub { | 124 class RuntimeStub : public CodeStub { |
| 216 public: | 125 public: |
| 217 explicit RuntimeStub(Runtime::FunctionId id, int num_arguments) | 126 explicit RuntimeStub(Runtime::FunctionId id, int num_arguments) |
| 218 : id_(id), num_arguments_(num_arguments) { } | 127 : id_(id), num_arguments_(num_arguments) { } |
| 219 | 128 |
| 220 void Generate(MacroAssembler* masm); | 129 void Generate(MacroAssembler* masm); |
| 221 | 130 |
| 222 // Disassembler support. It is useful to be able to print the name | 131 // Disassembler support. It is useful to be able to print the name |
| 223 // of the runtime function called through this stub. | 132 // of the runtime function called through this stub. |
| (...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 370 PrintF("ArgumentsAccessStub (type %d)\n", type_); | 279 PrintF("ArgumentsAccessStub (type %d)\n", type_); |
| 371 } | 280 } |
| 372 #endif | 281 #endif |
| 373 }; | 282 }; |
| 374 | 283 |
| 375 | 284 |
| 376 } // namespace internal | 285 } // namespace internal |
| 377 } // namespace v8 | 286 } // namespace v8 |
| 378 | 287 |
| 379 #endif // V8_CODEGEN_H_ | 288 #endif // V8_CODEGEN_H_ |
| OLD | NEW |