OLD | NEW |
1 // Copyright 2007-2008 the V8 project authors. All rights reserved. | 1 // Copyright 2007-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 #include "v8.h" | 28 #include "v8.h" |
29 | 29 |
| 30 #include "bootstrapper.h" |
30 #include "codegen-inl.h" | 31 #include "codegen-inl.h" |
31 #include "debug.h" | 32 #include "debug.h" |
| 33 #include "prettyprinter.h" |
| 34 #include "scopeinfo.h" |
32 #include "runtime.h" | 35 #include "runtime.h" |
33 #include "stub-cache.h" | 36 #include "stub-cache.h" |
34 | 37 |
35 namespace v8 { namespace internal { | 38 namespace v8 { namespace internal { |
36 | 39 |
37 DeferredCode::DeferredCode(CodeGenerator* generator) | 40 DeferredCode::DeferredCode(CodeGenerator* generator) |
38 : masm_(generator->masm()), | 41 : masm_(generator->masm()), |
39 generator_(generator), | 42 generator_(generator), |
40 statement_position_(masm_->last_statement_position()), | 43 statement_position_(masm_->last_statement_position()), |
41 position_(masm_->last_position()) { | 44 position_(masm_->last_position()) { |
(...skipping 19 matching lines...) Expand all Loading... |
61 masm->bind(code->enter()); | 64 masm->bind(code->enter()); |
62 Comment cmnt(masm, code->comment()); | 65 Comment cmnt(masm, code->comment()); |
63 code->Generate(); | 66 code->Generate(); |
64 if (code->exit()->is_bound()) { | 67 if (code->exit()->is_bound()) { |
65 masm->jmp(code->exit()); // platform independent? | 68 masm->jmp(code->exit()); // platform independent? |
66 } | 69 } |
67 } | 70 } |
68 } | 71 } |
69 | 72 |
70 | 73 |
| 74 // Generate the code. Takes a function literal, generates code for it, assemble |
| 75 // all the pieces into a Code object. This function is only to be called by |
| 76 // the compiler.cc code. |
| 77 Handle<Code> CodeGenerator::MakeCode(FunctionLiteral* flit, |
| 78 Handle<Script> script, |
| 79 bool is_eval) { |
| 80 #ifdef ENABLE_DISASSEMBLER |
| 81 bool print_code = FLAG_print_code && !Bootstrapper::IsActive(); |
| 82 #endif |
| 83 |
| 84 #ifdef DEBUG |
| 85 bool print_source = false; |
| 86 bool print_ast = false; |
| 87 const char* ftype; |
| 88 |
| 89 if (Bootstrapper::IsActive()) { |
| 90 print_source = FLAG_print_builtin_source; |
| 91 print_ast = FLAG_print_builtin_ast; |
| 92 print_code = FLAG_print_builtin_code; |
| 93 ftype = "builtin"; |
| 94 } else { |
| 95 print_source = FLAG_print_source; |
| 96 print_ast = FLAG_print_ast; |
| 97 ftype = "user-defined"; |
| 98 } |
| 99 |
| 100 if (FLAG_trace_codegen || print_source || print_ast) { |
| 101 PrintF("*** Generate code for %s function: ", ftype); |
| 102 flit->name()->ShortPrint(); |
| 103 PrintF(" ***\n"); |
| 104 } |
| 105 |
| 106 if (print_source) { |
| 107 PrintF("--- Source from AST ---\n%s\n", PrettyPrinter().PrintProgram(flit)); |
| 108 } |
| 109 |
| 110 if (print_ast) { |
| 111 PrintF("--- AST ---\n%s\n", AstPrinter().PrintProgram(flit)); |
| 112 } |
| 113 #endif // DEBUG |
| 114 |
| 115 // Generate code. |
| 116 const int initial_buffer_size = 4 * KB; |
| 117 CodeGenerator cgen(initial_buffer_size, script, is_eval); |
| 118 cgen.GenCode(flit); |
| 119 if (cgen.HasStackOverflow()) { |
| 120 ASSERT(!Top::has_pending_exception()); |
| 121 return Handle<Code>::null(); |
| 122 } |
| 123 |
| 124 // Process any deferred code. |
| 125 cgen.ProcessDeferred(); |
| 126 |
| 127 // Allocate and install the code. |
| 128 CodeDesc desc; |
| 129 cgen.masm()->GetCode(&desc); |
| 130 ScopeInfo<> sinfo(flit->scope()); |
| 131 Code::Flags flags = Code::ComputeFlags(Code::FUNCTION); |
| 132 Handle<Code> code = Factory::NewCode(desc, &sinfo, flags); |
| 133 |
| 134 // Add unresolved entries in the code to the fixup list. |
| 135 Bootstrapper::AddFixup(*code, cgen.masm()); |
| 136 |
| 137 #ifdef ENABLE_DISASSEMBLER |
| 138 if (print_code) { |
| 139 // Print the source code if available. |
| 140 if (!script->IsUndefined() && !script->source()->IsUndefined()) { |
| 141 PrintF("--- Raw source ---\n"); |
| 142 StringInputBuffer stream(String::cast(script->source())); |
| 143 stream.Seek(flit->start_position()); |
| 144 // flit->end_position() points to the last character in the stream. We |
| 145 // need to compensate by adding one to calculate the length. |
| 146 int source_len = flit->end_position() - flit->start_position() + 1; |
| 147 for (int i = 0; i < source_len; i++) { |
| 148 if (stream.has_more()) PrintF("%c", stream.GetNext()); |
| 149 } |
| 150 PrintF("\n\n"); |
| 151 } |
| 152 PrintF("--- Code ---\n"); |
| 153 code->Disassemble(); |
| 154 } |
| 155 #endif // ENABLE_DISASSEMBLER |
| 156 |
| 157 if (!code.is_null()) { |
| 158 Counters::total_compiled_code_size.Increment(code->instruction_size()); |
| 159 } |
| 160 |
| 161 return code; |
| 162 } |
| 163 |
| 164 |
71 // Sets the function info on a function. | 165 // Sets the function info on a function. |
72 // The start_position points to the first '(' character after the function name | 166 // The start_position points to the first '(' character after the function name |
73 // in the full script source. When counting characters in the script source the | 167 // in the full script source. When counting characters in the script source the |
74 // the first character is number 0 (not 1). | 168 // the first character is number 0 (not 1). |
75 void CodeGenerator::SetFunctionInfo(Handle<JSFunction> fun, | 169 void CodeGenerator::SetFunctionInfo(Handle<JSFunction> fun, |
76 int length, | 170 int length, |
77 int function_token_position, | 171 int function_token_position, |
78 int start_position, | 172 int start_position, |
79 int end_position, | 173 int end_position, |
80 bool is_expression, | 174 bool is_expression, |
(...skipping 284 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
365 void ArgumentsAccessStub::Generate(MacroAssembler* masm) { | 459 void ArgumentsAccessStub::Generate(MacroAssembler* masm) { |
366 switch (type_) { | 460 switch (type_) { |
367 case READ_LENGTH: GenerateReadLength(masm); break; | 461 case READ_LENGTH: GenerateReadLength(masm); break; |
368 case READ_ELEMENT: GenerateReadElement(masm); break; | 462 case READ_ELEMENT: GenerateReadElement(masm); break; |
369 case NEW_OBJECT: GenerateNewObject(masm); break; | 463 case NEW_OBJECT: GenerateNewObject(masm); break; |
370 } | 464 } |
371 } | 465 } |
372 | 466 |
373 | 467 |
374 } } // namespace v8::internal | 468 } } // namespace v8::internal |
OLD | NEW |