00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include "v8.h"
00029
00030 #include "bootstrapper.h"
00031 #include "codegen-inl.h"
00032 #include "compilation-cache.h"
00033 #include "compiler.h"
00034 #include "debug.h"
00035 #include "scopes.h"
00036 #include "rewriter.h"
00037 #include "usage-analyzer.h"
00038
00039 namespace v8 { namespace internal {
00040
00041 static Handle<Code> MakeCode(FunctionLiteral* literal,
00042 Handle<Script> script,
00043 bool is_eval) {
00044 ASSERT(literal != NULL);
00045
00046
00047 if (!Rewriter::Process(literal) || !AnalyzeVariableUsage(literal)) {
00048
00049
00050 return Handle<Code>::null();
00051 }
00052
00053
00054
00055
00056 Scope* top = literal->scope();
00057 while (top->outer_scope() != NULL) top = top->outer_scope();
00058 top->AllocateVariables();
00059
00060 #ifdef DEBUG
00061 if (Bootstrapper::IsActive() ?
00062 FLAG_print_builtin_scopes :
00063 FLAG_print_scopes) {
00064 literal->scope()->Print();
00065 }
00066 #endif
00067
00068
00069 Rewriter::Optimize(literal);
00070
00071
00072 Handle<Code> result = CodeGenerator::MakeCode(literal, script, is_eval);
00073 return result;
00074 }
00075
00076
00077 static Handle<JSFunction> MakeFunction(bool is_global,
00078 bool is_eval,
00079 Handle<Script> script,
00080 v8::Extension* extension,
00081 ScriptDataImpl* pre_data) {
00082 ZoneScope zone_scope(DELETE_ON_EXIT);
00083
00084
00085 StackGuard guard;
00086 PostponeInterruptsScope postpone;
00087
00088
00089 Debugger::OnBeforeCompile(script);
00090
00091
00092 ASSERT(is_eval || is_global);
00093
00094
00095 FunctionLiteral* lit = MakeAST(is_global, script, extension, pre_data);
00096
00097
00098 if (lit == NULL) {
00099 ASSERT(Top::has_pending_exception());
00100 return Handle<JSFunction>::null();
00101 }
00102
00103
00104
00105
00106 StatsRate* rate = is_eval
00107 ? &Counters::compile_eval
00108 : &Counters::compile;
00109 StatsRateScope timer(rate);
00110
00111
00112 Handle<Code> code = MakeCode(lit, script, is_eval);
00113
00114
00115 if (code.is_null()) {
00116 Top::StackOverflow();
00117 return Handle<JSFunction>::null();
00118 }
00119
00120 if (script->name()->IsString()) {
00121 SmartPointer<char> data =
00122 String::cast(script->name())->ToCString(DISALLOW_NULLS);
00123 LOG(CodeCreateEvent(is_eval ? "Eval" : "Script", *code, *data));
00124 } else {
00125 LOG(CodeCreateEvent(is_eval ? "Eval" : "Script", *code, ""));
00126 }
00127
00128
00129 Handle<JSFunction> fun =
00130 Factory::NewFunctionBoilerplate(lit->name(),
00131 lit->materialized_literal_count(),
00132 lit->contains_array_literal(),
00133 code);
00134
00135 CodeGenerator::SetFunctionInfo(fun, lit->scope()->num_parameters(),
00136 RelocInfo::kNoPosition,
00137 lit->start_position(), lit->end_position(),
00138 lit->is_expression(), true, script);
00139
00140
00141
00142
00143 SetExpectedNofPropertiesFromEstimate(fun, lit->expected_property_count());
00144
00145
00146 Debugger::OnAfterCompile(script, fun);
00147
00148 return fun;
00149 }
00150
00151
00152 static StaticResource<SafeStringInputBuffer> safe_string_input_buffer;
00153
00154
00155 Handle<JSFunction> Compiler::Compile(Handle<String> source,
00156 Handle<Object> script_name,
00157 int line_offset, int column_offset,
00158 v8::Extension* extension,
00159 ScriptDataImpl* input_pre_data) {
00160 Counters::total_load_size.Increment(source->length());
00161 Counters::total_compile_size.Increment(source->length());
00162
00163
00164 VMState state(COMPILER);
00165
00166
00167 Handle<JSFunction> result;
00168 if (extension == NULL) {
00169 result = CompilationCache::LookupScript(source,
00170 script_name,
00171 line_offset,
00172 column_offset);
00173 }
00174
00175 if (result.is_null()) {
00176
00177 ScriptDataImpl* pre_data = input_pre_data;
00178 if (pre_data == NULL && source->length() >= FLAG_min_preparse_length) {
00179 Access<SafeStringInputBuffer> buf(&safe_string_input_buffer);
00180 buf->Reset(source.location());
00181 pre_data = PreParse(buf.value(), extension);
00182 }
00183
00184
00185 Handle<Script> script = Factory::NewScript(source);
00186 if (!script_name.is_null()) {
00187 script->set_name(*script_name);
00188 script->set_line_offset(Smi::FromInt(line_offset));
00189 script->set_column_offset(Smi::FromInt(column_offset));
00190 }
00191
00192
00193 result = MakeFunction(true, false, script, extension, pre_data);
00194 if (extension == NULL && !result.is_null()) {
00195 CompilationCache::PutFunction(source, CompilationCache::SCRIPT, result);
00196 }
00197
00198
00199 if (input_pre_data == NULL && pre_data != NULL) {
00200 delete pre_data;
00201 }
00202 }
00203
00204 return result;
00205 }
00206
00207
00208 Handle<JSFunction> Compiler::CompileEval(Handle<String> source,
00209 int line_offset,
00210 bool is_global) {
00211 Counters::total_eval_size.Increment(source->length());
00212 Counters::total_compile_size.Increment(source->length());
00213
00214
00215 VMState state(COMPILER);
00216 CompilationCache::Entry entry = is_global
00217 ? CompilationCache::EVAL_GLOBAL
00218 : CompilationCache::EVAL_CONTEXTUAL;
00219
00220
00221
00222 Handle<JSFunction> result = CompilationCache::LookupEval(source, entry);
00223 if (result.is_null()) {
00224
00225 Handle<Script> script = Factory::NewScript(source);
00226 script->set_line_offset(Smi::FromInt(line_offset));
00227 result = MakeFunction(is_global, true, script, NULL, NULL);
00228 if (!result.is_null()) {
00229 CompilationCache::PutFunction(source, entry, result);
00230 }
00231 }
00232 return result;
00233 }
00234
00235
00236 bool Compiler::CompileLazy(Handle<SharedFunctionInfo> shared) {
00237 ZoneScope zone_scope(DELETE_ON_EXIT);
00238
00239
00240 VMState state(COMPILER);
00241
00242
00243 StackGuard guard;
00244 PostponeInterruptsScope postpone;
00245
00246
00247 Handle<String> name(String::cast(shared->name()));
00248 Handle<Script> script(Script::cast(shared->script()));
00249
00250 int start_position = shared->start_position();
00251 int end_position = shared->end_position();
00252 bool is_expression = shared->is_expression();
00253 Counters::total_compile_size.Increment(end_position - start_position);
00254
00255
00256
00257 FunctionLiteral* lit = MakeLazyAST(script, name,
00258 start_position,
00259 end_position,
00260 is_expression);
00261
00262
00263 if (lit == NULL) {
00264 ASSERT(Top::has_pending_exception());
00265 return false;
00266 }
00267
00268
00269
00270
00271 StatsRateScope timer(&Counters::compile_lazy);
00272
00273
00274 Handle<Code> code = MakeCode(lit, script, false);
00275
00276
00277 if (code.is_null()) {
00278 Top::StackOverflow();
00279 return false;
00280 }
00281
00282
00283 LOG(CodeCreateEvent("LazyCompile", *code, *lit->name()));
00284
00285
00286 shared->set_code(*code);
00287
00288
00289 SetExpectedNofPropertiesFromEstimate(shared, lit->expected_property_count());
00290
00291
00292 ASSERT(shared->is_compiled());
00293 return true;
00294 }
00295
00296
00297 } }