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 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
119 | 119 |
120 | 120 |
121 void CodeGenerator::DeleteFrame() { | 121 void CodeGenerator::DeleteFrame() { |
122 if (has_valid_frame()) { | 122 if (has_valid_frame()) { |
123 frame_->DetachFromCodeGenerator(); | 123 frame_->DetachFromCodeGenerator(); |
124 frame_ = NULL; | 124 frame_ = NULL; |
125 } | 125 } |
126 } | 126 } |
127 | 127 |
128 | 128 |
129 void CodeGenerator::MakeCodePrologue(FunctionLiteral* fun) { | 129 void CodeGenerator::MakeCodePrologue(CompilationInfo* info) { |
130 #ifdef DEBUG | 130 #ifdef DEBUG |
131 bool print_source = false; | 131 bool print_source = false; |
132 bool print_ast = false; | 132 bool print_ast = false; |
133 bool print_json_ast = false; | 133 bool print_json_ast = false; |
134 const char* ftype; | 134 const char* ftype; |
135 | 135 |
136 if (Bootstrapper::IsActive()) { | 136 if (Bootstrapper::IsActive()) { |
137 print_source = FLAG_print_builtin_source; | 137 print_source = FLAG_print_builtin_source; |
138 print_ast = FLAG_print_builtin_ast; | 138 print_ast = FLAG_print_builtin_ast; |
139 print_json_ast = FLAG_print_builtin_json_ast; | 139 print_json_ast = FLAG_print_builtin_json_ast; |
140 ftype = "builtin"; | 140 ftype = "builtin"; |
141 } else { | 141 } else { |
142 print_source = FLAG_print_source; | 142 print_source = FLAG_print_source; |
143 print_ast = FLAG_print_ast; | 143 print_ast = FLAG_print_ast; |
144 print_json_ast = FLAG_print_json_ast; | 144 print_json_ast = FLAG_print_json_ast; |
145 ftype = "user-defined"; | 145 ftype = "user-defined"; |
146 } | 146 } |
147 | 147 |
148 if (FLAG_trace_codegen || print_source || print_ast) { | 148 if (FLAG_trace_codegen || print_source || print_ast) { |
149 PrintF("*** Generate code for %s function: ", ftype); | 149 PrintF("*** Generate code for %s function: ", ftype); |
150 fun->name()->ShortPrint(); | 150 info->function()->name()->ShortPrint(); |
151 PrintF(" ***\n"); | 151 PrintF(" ***\n"); |
152 } | 152 } |
153 | 153 |
154 if (print_source) { | 154 if (print_source) { |
155 PrintF("--- Source from AST ---\n%s\n", PrettyPrinter().PrintProgram(fun)); | 155 PrintF("--- Source from AST ---\n%s\n", |
| 156 PrettyPrinter().PrintProgram(info->function())); |
156 } | 157 } |
157 | 158 |
158 if (print_ast) { | 159 if (print_ast) { |
159 PrintF("--- AST ---\n%s\n", AstPrinter().PrintProgram(fun)); | 160 PrintF("--- AST ---\n%s\n", |
| 161 AstPrinter().PrintProgram(info->function())); |
160 } | 162 } |
161 | 163 |
162 if (print_json_ast) { | 164 if (print_json_ast) { |
163 JsonAstBuilder builder; | 165 JsonAstBuilder builder; |
164 PrintF("%s", builder.BuildProgram(fun)); | 166 PrintF("%s", builder.BuildProgram(info->function())); |
165 } | 167 } |
166 #endif // DEBUG | 168 #endif // DEBUG |
167 } | 169 } |
168 | 170 |
169 | 171 |
170 Handle<Code> CodeGenerator::MakeCodeEpilogue(FunctionLiteral* fun, | 172 Handle<Code> CodeGenerator::MakeCodeEpilogue(MacroAssembler* masm, |
171 MacroAssembler* masm, | |
172 Code::Flags flags, | 173 Code::Flags flags, |
173 Handle<Script> script) { | 174 CompilationInfo* info) { |
174 // Allocate and install the code. | 175 // Allocate and install the code. |
175 CodeDesc desc; | 176 CodeDesc desc; |
176 masm->GetCode(&desc); | 177 masm->GetCode(&desc); |
177 ZoneScopeInfo sinfo(fun->scope()); | 178 ZoneScopeInfo sinfo(info->scope()); |
178 Handle<Code> code = | 179 Handle<Code> code = |
179 Factory::NewCode(desc, &sinfo, flags, masm->CodeObject()); | 180 Factory::NewCode(desc, &sinfo, flags, masm->CodeObject()); |
180 | 181 |
181 // Add unresolved entries in the code to the fixup list. | 182 // Add unresolved entries in the code to the fixup list. |
182 Bootstrapper::AddFixup(*code, masm); | 183 Bootstrapper::AddFixup(*code, masm); |
183 | 184 |
184 #ifdef ENABLE_DISASSEMBLER | 185 #ifdef ENABLE_DISASSEMBLER |
185 bool print_code = Bootstrapper::IsActive() | 186 bool print_code = Bootstrapper::IsActive() |
186 ? FLAG_print_builtin_code | 187 ? FLAG_print_builtin_code |
187 : FLAG_print_code; | 188 : FLAG_print_code; |
188 if (print_code) { | 189 if (print_code) { |
189 // Print the source code if available. | 190 // Print the source code if available. |
| 191 Handle<Script> script = info->script(); |
| 192 FunctionLiteral* function = info->function(); |
190 if (!script->IsUndefined() && !script->source()->IsUndefined()) { | 193 if (!script->IsUndefined() && !script->source()->IsUndefined()) { |
191 PrintF("--- Raw source ---\n"); | 194 PrintF("--- Raw source ---\n"); |
192 StringInputBuffer stream(String::cast(script->source())); | 195 StringInputBuffer stream(String::cast(script->source())); |
193 stream.Seek(fun->start_position()); | 196 stream.Seek(function->start_position()); |
194 // fun->end_position() points to the last character in the stream. We | 197 // fun->end_position() points to the last character in the stream. We |
195 // need to compensate by adding one to calculate the length. | 198 // need to compensate by adding one to calculate the length. |
196 int source_len = fun->end_position() - fun->start_position() + 1; | 199 int source_len = |
| 200 function->end_position() - function->start_position() + 1; |
197 for (int i = 0; i < source_len; i++) { | 201 for (int i = 0; i < source_len; i++) { |
198 if (stream.has_more()) PrintF("%c", stream.GetNext()); | 202 if (stream.has_more()) PrintF("%c", stream.GetNext()); |
199 } | 203 } |
200 PrintF("\n\n"); | 204 PrintF("\n\n"); |
201 } | 205 } |
202 PrintF("--- Code ---\n"); | 206 PrintF("--- Code ---\n"); |
203 code->Disassemble(*fun->name()->ToCString()); | 207 code->Disassemble(*function->name()->ToCString()); |
204 } | 208 } |
205 #endif // ENABLE_DISASSEMBLER | 209 #endif // ENABLE_DISASSEMBLER |
206 | 210 |
207 if (!code.is_null()) { | 211 if (!code.is_null()) { |
208 Counters::total_compiled_code_size.Increment(code->instruction_size()); | 212 Counters::total_compiled_code_size.Increment(code->instruction_size()); |
209 } | 213 } |
210 return code; | 214 return code; |
211 } | 215 } |
212 | 216 |
213 | 217 |
214 // Generate the code. Takes a function literal, generates code for it, assemble | 218 // Generate the code. Takes a function literal, generates code for it, assemble |
215 // all the pieces into a Code object. This function is only to be called by | 219 // all the pieces into a Code object. This function is only to be called by |
216 // the compiler.cc code. | 220 // the compiler.cc code. |
217 Handle<Code> CodeGenerator::MakeCode(FunctionLiteral* fun, | 221 Handle<Code> CodeGenerator::MakeCode(CompilationInfo* info) { |
218 Handle<Script> script, | 222 Handle<Script> script = info->script(); |
219 bool is_eval, | |
220 CompilationInfo* info) { | |
221 if (!script->IsUndefined() && !script->source()->IsUndefined()) { | 223 if (!script->IsUndefined() && !script->source()->IsUndefined()) { |
222 int len = String::cast(script->source())->length(); | 224 int len = String::cast(script->source())->length(); |
223 Counters::total_old_codegen_source_size.Increment(len); | 225 Counters::total_old_codegen_source_size.Increment(len); |
224 } | 226 } |
225 MakeCodePrologue(fun); | 227 MakeCodePrologue(info); |
226 // Generate code. | 228 // Generate code. |
227 const int kInitialBufferSize = 4 * KB; | 229 const int kInitialBufferSize = 4 * KB; |
228 MacroAssembler masm(NULL, kInitialBufferSize); | 230 MacroAssembler masm(NULL, kInitialBufferSize); |
229 CodeGenerator cgen(&masm, script, is_eval); | 231 CodeGenerator cgen(&masm); |
230 CodeGeneratorScope scope(&cgen); | 232 CodeGeneratorScope scope(&cgen); |
231 cgen.Generate(fun, PRIMARY, info); | 233 cgen.Generate(info, PRIMARY); |
232 if (cgen.HasStackOverflow()) { | 234 if (cgen.HasStackOverflow()) { |
233 ASSERT(!Top::has_pending_exception()); | 235 ASSERT(!Top::has_pending_exception()); |
234 return Handle<Code>::null(); | 236 return Handle<Code>::null(); |
235 } | 237 } |
236 | 238 |
237 InLoopFlag in_loop = (cgen.loop_nesting() != 0) ? IN_LOOP : NOT_IN_LOOP; | 239 InLoopFlag in_loop = (cgen.loop_nesting() != 0) ? IN_LOOP : NOT_IN_LOOP; |
238 Code::Flags flags = Code::ComputeFlags(Code::FUNCTION, in_loop); | 240 Code::Flags flags = Code::ComputeFlags(Code::FUNCTION, in_loop); |
239 return MakeCodeEpilogue(fun, cgen.masm(), flags, script); | 241 return MakeCodeEpilogue(cgen.masm(), flags, info); |
240 } | 242 } |
241 | 243 |
242 | 244 |
243 #ifdef ENABLE_LOGGING_AND_PROFILING | 245 #ifdef ENABLE_LOGGING_AND_PROFILING |
244 | 246 |
245 bool CodeGenerator::ShouldGenerateLog(Expression* type) { | 247 bool CodeGenerator::ShouldGenerateLog(Expression* type) { |
246 ASSERT(type != NULL); | 248 ASSERT(type != NULL); |
247 if (!Logger::is_logging()) return false; | 249 if (!Logger::is_logging()) return false; |
248 Handle<String> name = Handle<String>::cast(type->AsLiteral()->handle()); | 250 Handle<String> name = Handle<String>::cast(type->AsLiteral()->handle()); |
249 if (FLAG_log_regexp) { | 251 if (FLAG_log_regexp) { |
(...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
507 | 509 |
508 #ifdef ENABLE_DEBUGGER_SUPPORT | 510 #ifdef ENABLE_DEBUGGER_SUPPORT |
509 void DebuggerStatementStub::Generate(MacroAssembler* masm) { | 511 void DebuggerStatementStub::Generate(MacroAssembler* masm) { |
510 Runtime::Function* f = Runtime::FunctionForId(Runtime::kDebugBreak); | 512 Runtime::Function* f = Runtime::FunctionForId(Runtime::kDebugBreak); |
511 masm->TailCallRuntime(ExternalReference(f), 0, f->result_size); | 513 masm->TailCallRuntime(ExternalReference(f), 0, f->result_size); |
512 } | 514 } |
513 #endif | 515 #endif |
514 | 516 |
515 | 517 |
516 } } // namespace v8::internal | 518 } } // namespace v8::internal |
OLD | NEW |