OLD | NEW |
1 // Copyright 2009 the V8 project authors. All rights reserved. | 1 // Copyright 2009 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 |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 | 118 |
119 | 119 |
120 void CodeGenerator::DeleteFrame() { | 120 void CodeGenerator::DeleteFrame() { |
121 if (has_valid_frame()) { | 121 if (has_valid_frame()) { |
122 frame_->DetachFromCodeGenerator(); | 122 frame_->DetachFromCodeGenerator(); |
123 frame_ = NULL; | 123 frame_ = NULL; |
124 } | 124 } |
125 } | 125 } |
126 | 126 |
127 | 127 |
128 // Generate the code. Takes a function literal, generates code for it, assemble | 128 void CodeGenerator::MakeCodePrologue(FunctionLiteral* fun) { |
129 // all the pieces into a Code object. This function is only to be called by | |
130 // the compiler.cc code. | |
131 Handle<Code> CodeGenerator::MakeCode(FunctionLiteral* flit, | |
132 Handle<Script> script, | |
133 bool is_eval) { | |
134 #ifdef ENABLE_DISASSEMBLER | |
135 bool print_code = Bootstrapper::IsActive() | |
136 ? FLAG_print_builtin_code | |
137 : FLAG_print_code; | |
138 #endif | |
139 | |
140 #ifdef DEBUG | 129 #ifdef DEBUG |
141 bool print_source = false; | 130 bool print_source = false; |
142 bool print_ast = false; | 131 bool print_ast = false; |
143 bool print_json_ast = false; | 132 bool print_json_ast = false; |
144 const char* ftype; | 133 const char* ftype; |
145 | 134 |
146 if (Bootstrapper::IsActive()) { | 135 if (Bootstrapper::IsActive()) { |
147 print_source = FLAG_print_builtin_source; | 136 print_source = FLAG_print_builtin_source; |
148 print_ast = FLAG_print_builtin_ast; | 137 print_ast = FLAG_print_builtin_ast; |
149 print_json_ast = FLAG_print_builtin_json_ast; | 138 print_json_ast = FLAG_print_builtin_json_ast; |
150 ftype = "builtin"; | 139 ftype = "builtin"; |
151 } else { | 140 } else { |
152 print_source = FLAG_print_source; | 141 print_source = FLAG_print_source; |
153 print_ast = FLAG_print_ast; | 142 print_ast = FLAG_print_ast; |
154 print_json_ast = FLAG_print_json_ast; | 143 print_json_ast = FLAG_print_json_ast; |
155 ftype = "user-defined"; | 144 ftype = "user-defined"; |
156 } | 145 } |
157 | 146 |
158 if (FLAG_trace_codegen || print_source || print_ast) { | 147 if (FLAG_trace_codegen || print_source || print_ast) { |
159 PrintF("*** Generate code for %s function: ", ftype); | 148 PrintF("*** Generate code for %s function: ", ftype); |
160 flit->name()->ShortPrint(); | 149 fun->name()->ShortPrint(); |
161 PrintF(" ***\n"); | 150 PrintF(" ***\n"); |
162 } | 151 } |
163 | 152 |
164 if (print_source) { | 153 if (print_source) { |
165 PrintF("--- Source from AST ---\n%s\n", PrettyPrinter().PrintProgram(flit)); | 154 PrintF("--- Source from AST ---\n%s\n", PrettyPrinter().PrintProgram(fun)); |
166 } | 155 } |
167 | 156 |
168 if (print_ast) { | 157 if (print_ast) { |
169 PrintF("--- AST ---\n%s\n", AstPrinter().PrintProgram(flit)); | 158 PrintF("--- AST ---\n%s\n", AstPrinter().PrintProgram(fun)); |
170 } | 159 } |
171 | 160 |
172 if (print_json_ast) { | 161 if (print_json_ast) { |
173 JsonAstBuilder builder; | 162 JsonAstBuilder builder; |
174 PrintF("%s", builder.BuildProgram(flit)); | 163 PrintF("%s", builder.BuildProgram(fun)); |
175 } | 164 } |
176 #endif // DEBUG | 165 #endif // DEBUG |
| 166 } |
177 | 167 |
178 // Generate code. | |
179 const int initial_buffer_size = 4 * KB; | |
180 CodeGenerator cgen(initial_buffer_size, script, is_eval); | |
181 CodeGeneratorScope scope(&cgen); | |
182 cgen.GenCode(flit); | |
183 if (cgen.HasStackOverflow()) { | |
184 ASSERT(!Top::has_pending_exception()); | |
185 return Handle<Code>::null(); | |
186 } | |
187 | 168 |
188 // Allocate and install the code. Time the rest of this function as | 169 Handle<Code> CodeGenerator::MakeCodeEpilogue(FunctionLiteral* fun, |
189 // code creation. | 170 MacroAssembler* masm, |
190 HistogramTimerScope timer(&Counters::code_creation); | 171 Code::Flags flags, |
| 172 Handle<Script> script) { |
| 173 // Allocate and install the code. |
191 CodeDesc desc; | 174 CodeDesc desc; |
192 cgen.masm()->GetCode(&desc); | 175 masm->GetCode(&desc); |
193 ZoneScopeInfo sinfo(flit->scope()); | 176 ZoneScopeInfo sinfo(fun->scope()); |
194 InLoopFlag in_loop = (cgen.loop_nesting() != 0) ? IN_LOOP : NOT_IN_LOOP; | 177 Handle<Code> code = |
195 Code::Flags flags = Code::ComputeFlags(Code::FUNCTION, in_loop); | 178 Factory::NewCode(desc, &sinfo, flags, masm->CodeObject()); |
196 Handle<Code> code = Factory::NewCode(desc, | |
197 &sinfo, | |
198 flags, | |
199 cgen.masm()->CodeObject()); | |
200 | 179 |
201 // Add unresolved entries in the code to the fixup list. | 180 // Add unresolved entries in the code to the fixup list. |
202 Bootstrapper::AddFixup(*code, cgen.masm()); | 181 Bootstrapper::AddFixup(*code, masm); |
203 | 182 |
204 #ifdef ENABLE_DISASSEMBLER | 183 #ifdef ENABLE_DISASSEMBLER |
| 184 bool print_code = Bootstrapper::IsActive() |
| 185 ? FLAG_print_builtin_code |
| 186 : FLAG_print_code; |
205 if (print_code) { | 187 if (print_code) { |
206 // Print the source code if available. | 188 // Print the source code if available. |
207 if (!script->IsUndefined() && !script->source()->IsUndefined()) { | 189 if (!script->IsUndefined() && !script->source()->IsUndefined()) { |
208 PrintF("--- Raw source ---\n"); | 190 PrintF("--- Raw source ---\n"); |
209 StringInputBuffer stream(String::cast(script->source())); | 191 StringInputBuffer stream(String::cast(script->source())); |
210 stream.Seek(flit->start_position()); | 192 stream.Seek(fun->start_position()); |
211 // flit->end_position() points to the last character in the stream. We | 193 // fun->end_position() points to the last character in the stream. We |
212 // need to compensate by adding one to calculate the length. | 194 // need to compensate by adding one to calculate the length. |
213 int source_len = flit->end_position() - flit->start_position() + 1; | 195 int source_len = fun->end_position() - fun->start_position() + 1; |
214 for (int i = 0; i < source_len; i++) { | 196 for (int i = 0; i < source_len; i++) { |
215 if (stream.has_more()) PrintF("%c", stream.GetNext()); | 197 if (stream.has_more()) PrintF("%c", stream.GetNext()); |
216 } | 198 } |
217 PrintF("\n\n"); | 199 PrintF("\n\n"); |
218 } | 200 } |
219 PrintF("--- Code ---\n"); | 201 PrintF("--- Code ---\n"); |
220 code->Disassemble(*flit->name()->ToCString()); | 202 code->Disassemble(*fun->name()->ToCString()); |
221 } | 203 } |
222 #endif // ENABLE_DISASSEMBLER | 204 #endif // ENABLE_DISASSEMBLER |
223 | 205 |
224 if (!code.is_null()) { | 206 if (!code.is_null()) { |
225 Counters::total_compiled_code_size.Increment(code->instruction_size()); | 207 Counters::total_compiled_code_size.Increment(code->instruction_size()); |
226 } | 208 } |
| 209 return code; |
| 210 } |
227 | 211 |
228 return code; | 212 |
| 213 // Generate the code. Takes a function literal, generates code for it, assemble |
| 214 // all the pieces into a Code object. This function is only to be called by |
| 215 // the compiler.cc code. |
| 216 Handle<Code> CodeGenerator::MakeCode(FunctionLiteral* fun, |
| 217 Handle<Script> script, |
| 218 bool is_eval) { |
| 219 MakeCodePrologue(fun); |
| 220 // Generate code. |
| 221 const int kInitialBufferSize = 4 * KB; |
| 222 CodeGenerator cgen(kInitialBufferSize, script, is_eval); |
| 223 CodeGeneratorScope scope(&cgen); |
| 224 cgen.GenCode(fun); |
| 225 if (cgen.HasStackOverflow()) { |
| 226 ASSERT(!Top::has_pending_exception()); |
| 227 return Handle<Code>::null(); |
| 228 } |
| 229 |
| 230 InLoopFlag in_loop = (cgen.loop_nesting() != 0) ? IN_LOOP : NOT_IN_LOOP; |
| 231 Code::Flags flags = Code::ComputeFlags(Code::FUNCTION, in_loop); |
| 232 return MakeCodeEpilogue(fun, cgen.masm(), flags, script); |
229 } | 233 } |
230 | 234 |
231 | 235 |
232 #ifdef ENABLE_LOGGING_AND_PROFILING | 236 #ifdef ENABLE_LOGGING_AND_PROFILING |
233 | 237 |
234 bool CodeGenerator::ShouldGenerateLog(Expression* type) { | 238 bool CodeGenerator::ShouldGenerateLog(Expression* type) { |
235 ASSERT(type != NULL); | 239 ASSERT(type != NULL); |
236 if (!Logger::is_logging()) return false; | 240 if (!Logger::is_logging()) return false; |
237 Handle<String> name = Handle<String>::cast(type->AsLiteral()->handle()); | 241 Handle<String> name = Handle<String>::cast(type->AsLiteral()->handle()); |
238 if (FLAG_log_regexp) { | 242 if (FLAG_log_regexp) { |
(...skipping 302 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
541 void ArgumentsAccessStub::Generate(MacroAssembler* masm) { | 545 void ArgumentsAccessStub::Generate(MacroAssembler* masm) { |
542 switch (type_) { | 546 switch (type_) { |
543 case READ_LENGTH: GenerateReadLength(masm); break; | 547 case READ_LENGTH: GenerateReadLength(masm); break; |
544 case READ_ELEMENT: GenerateReadElement(masm); break; | 548 case READ_ELEMENT: GenerateReadElement(masm); break; |
545 case NEW_OBJECT: GenerateNewObject(masm); break; | 549 case NEW_OBJECT: GenerateNewObject(masm); break; |
546 } | 550 } |
547 } | 551 } |
548 | 552 |
549 | 553 |
550 } } // namespace v8::internal | 554 } } // namespace v8::internal |
OLD | NEW |