Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2006-2008 the V8 project authors. All rights reserved. | 1 // Copyright 2010 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. |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 45 explicit CompilationInfo(Handle<Script> script); | 45 explicit CompilationInfo(Handle<Script> script); |
| 46 explicit CompilationInfo(Handle<SharedFunctionInfo> shared_info); | 46 explicit CompilationInfo(Handle<SharedFunctionInfo> shared_info); |
| 47 explicit CompilationInfo(Handle<JSFunction> closure); | 47 explicit CompilationInfo(Handle<JSFunction> closure); |
| 48 | 48 |
| 49 bool is_lazy() const { return (flags_ & IsLazy::mask()) != 0; } | 49 bool is_lazy() const { return (flags_ & IsLazy::mask()) != 0; } |
| 50 bool is_eval() const { return (flags_ & IsEval::mask()) != 0; } | 50 bool is_eval() const { return (flags_ & IsEval::mask()) != 0; } |
| 51 bool is_global() const { return (flags_ & IsGlobal::mask()) != 0; } | 51 bool is_global() const { return (flags_ & IsGlobal::mask()) != 0; } |
| 52 bool is_json() const { return (flags_ & IsJson::mask()) != 0; } | 52 bool is_json() const { return (flags_ & IsJson::mask()) != 0; } |
| 53 bool is_in_loop() const { return (flags_ & IsInLoop::mask()) != 0; } | 53 bool is_in_loop() const { return (flags_ & IsInLoop::mask()) != 0; } |
| 54 FunctionLiteral* function() const { return function_; } | 54 FunctionLiteral* function() const { return function_; } |
| 55 Scope* scope() const { return function_->scope(); } | 55 Scope* scope() const { return scope_; } |
| 56 Handle<Code> code() const { return code_; } | |
| 56 Handle<JSFunction> closure() const { return closure_; } | 57 Handle<JSFunction> closure() const { return closure_; } |
| 57 Handle<SharedFunctionInfo> shared_info() const { return shared_info_; } | 58 Handle<SharedFunctionInfo> shared_info() const { return shared_info_; } |
| 58 Handle<Script> script() const { return script_; } | 59 Handle<Script> script() const { return script_; } |
| 59 v8::Extension* extension() const { return extension_; } | 60 v8::Extension* extension() const { return extension_; } |
| 60 ScriptDataImpl* pre_parse_data() const { return pre_parse_data_; } | 61 ScriptDataImpl* pre_parse_data() const { return pre_parse_data_; } |
| 62 Handle<Context> calling_context() const { return calling_context_; } | |
| 61 | 63 |
| 62 void MarkAsEval() { | 64 void MarkAsEval() { |
| 63 ASSERT(!is_lazy()); | 65 ASSERT(!is_lazy()); |
| 64 flags_ |= IsEval::encode(true); | 66 flags_ |= IsEval::encode(true); |
| 65 } | 67 } |
| 66 void MarkAsGlobal() { | 68 void MarkAsGlobal() { |
| 67 ASSERT(!is_lazy()); | 69 ASSERT(!is_lazy()); |
| 68 flags_ |= IsGlobal::encode(true); | 70 flags_ |= IsGlobal::encode(true); |
| 69 } | 71 } |
| 70 void MarkAsJson() { | 72 void MarkAsJson() { |
| 71 ASSERT(!is_lazy()); | 73 ASSERT(!is_lazy()); |
| 72 flags_ |= IsJson::encode(true); | 74 flags_ |= IsJson::encode(true); |
| 73 } | 75 } |
| 74 void MarkAsInLoop() { | 76 void MarkAsInLoop() { |
| 75 ASSERT(is_lazy()); | 77 ASSERT(is_lazy()); |
| 76 flags_ |= IsInLoop::encode(true); | 78 flags_ |= IsInLoop::encode(true); |
| 77 } | 79 } |
| 78 void SetFunction(FunctionLiteral* literal) { | 80 void SetFunction(FunctionLiteral* literal) { |
| 79 ASSERT(function_ == NULL); | 81 ASSERT(function_ == NULL); |
| 80 function_ = literal; | 82 function_ = literal; |
| 81 } | 83 } |
| 84 void SetScope(Scope* scope) { | |
| 85 ASSERT(scope_ == NULL); | |
|
fschneider
2010/10/04 14:24:54
Maybe make the things SetOncePointer if they are i
| |
| 86 scope_ = scope; | |
| 87 } | |
| 88 void SetCode(Handle<Code> code) { code_ = code; } | |
| 82 void SetExtension(v8::Extension* extension) { | 89 void SetExtension(v8::Extension* extension) { |
| 83 ASSERT(!is_lazy()); | 90 ASSERT(!is_lazy()); |
| 84 extension_ = extension; | 91 extension_ = extension; |
| 85 } | 92 } |
| 86 void SetPreParseData(ScriptDataImpl* pre_parse_data) { | 93 void SetPreParseData(ScriptDataImpl* pre_parse_data) { |
| 87 ASSERT(!is_lazy()); | 94 ASSERT(!is_lazy()); |
| 88 pre_parse_data_ = pre_parse_data; | 95 pre_parse_data_ = pre_parse_data; |
| 89 } | 96 } |
| 97 void SetCallingContext(Handle<Context> context) { | |
| 98 ASSERT(is_eval()); | |
| 99 calling_context_ = context; | |
| 100 } | |
| 90 | 101 |
| 91 private: | 102 private: |
| 92 // Flags using template class BitField<type, start, length>. All are | 103 // Flags using template class BitField<type, start, length>. All are |
| 93 // false by default. | 104 // false by default. |
| 94 // | 105 // |
| 95 // Compilation is either eager or lazy. | 106 // Compilation is either eager or lazy. |
| 96 class IsLazy: public BitField<bool, 0, 1> {}; | 107 class IsLazy: public BitField<bool, 0, 1> {}; |
| 97 // Flags that can be set for eager compilation. | 108 // Flags that can be set for eager compilation. |
| 98 class IsEval: public BitField<bool, 1, 1> {}; | 109 class IsEval: public BitField<bool, 1, 1> {}; |
| 99 class IsGlobal: public BitField<bool, 2, 1> {}; | 110 class IsGlobal: public BitField<bool, 2, 1> {}; |
| 100 class IsJson: public BitField<bool, 3, 1> {}; | 111 class IsJson: public BitField<bool, 3, 1> {}; |
| 101 // Flags that can be set for lazy compilation. | 112 // Flags that can be set for lazy compilation. |
| 102 class IsInLoop: public BitField<bool, 4, 1> {}; | 113 class IsInLoop: public BitField<bool, 4, 1> {}; |
| 103 | 114 |
| 104 unsigned flags_; | 115 unsigned flags_; |
| 105 | 116 |
| 106 // Fields filled in by the compilation pipeline. | 117 // Fields filled in by the compilation pipeline. |
| 118 // AST filled in by the parser. | |
| 107 FunctionLiteral* function_; | 119 FunctionLiteral* function_; |
| 120 // The scope of the function literal as a convenience. Set to indidicate | |
| 121 // that scopes have been analyzed. | |
| 108 Scope* scope_; | 122 Scope* scope_; |
| 123 // The compiled code. | |
| 124 Handle<Code> code_; | |
| 109 | 125 |
| 110 // Possible initial inputs to the compilation process. | 126 // Possible initial inputs to the compilation process. |
| 111 Handle<JSFunction> closure_; | 127 Handle<JSFunction> closure_; |
| 112 Handle<SharedFunctionInfo> shared_info_; | 128 Handle<SharedFunctionInfo> shared_info_; |
| 113 Handle<Script> script_; | 129 Handle<Script> script_; |
| 114 | 130 |
| 115 // Fields possibly needed for eager compilation, NULL by default. | 131 // Fields possibly needed for eager compilation, NULL by default. |
| 116 v8::Extension* extension_; | 132 v8::Extension* extension_; |
| 117 ScriptDataImpl* pre_parse_data_; | 133 ScriptDataImpl* pre_parse_data_; |
| 118 | 134 |
| 135 // The context of the caller is needed for eval code, and will be a null | |
| 136 // handle otherwise. | |
| 137 Handle<Context> calling_context_; | |
| 138 | |
| 119 DISALLOW_COPY_AND_ASSIGN(CompilationInfo); | 139 DISALLOW_COPY_AND_ASSIGN(CompilationInfo); |
| 120 }; | 140 }; |
| 121 | 141 |
| 122 | 142 |
| 123 // The V8 compiler | 143 // The V8 compiler |
| 124 // | 144 // |
| 125 // General strategy: Source code is translated into an anonymous function w/o | 145 // General strategy: Source code is translated into an anonymous function w/o |
| 126 // parameters which then can be executed. If the source code contains other | 146 // parameters which then can be executed. If the source code contains other |
| 127 // functions, they will be compiled and allocated as part of the compilation | 147 // functions, they will be compiled and allocated as part of the compilation |
| 128 // of the source code. | 148 // of the source code. |
| 129 | 149 |
| 130 // Please note this interface returns shared function infos. | 150 // Please note this interface returns shared function infos. This means you |
| 131 // This means you need to call Factory::NewFunctionFromSharedFunctionInfo | 151 // need to call Factory::NewFunctionFromSharedFunctionInfo before you have a |
| 132 // before you have a real function with a context. | 152 // real function with a context. |
| 133 | 153 |
| 134 class Compiler : public AllStatic { | 154 class Compiler : public AllStatic { |
| 135 public: | 155 public: |
| 136 enum ValidationState { DONT_VALIDATE_JSON, VALIDATE_JSON }; | 156 enum ValidationState { DONT_VALIDATE_JSON, VALIDATE_JSON }; |
| 137 | 157 |
| 138 // All routines return a JSFunction. | 158 // All routines return a JSFunction. |
| 139 // If an error occurs an exception is raised and | 159 // If an error occurs an exception is raised and |
| 140 // the return handle contains NULL. | 160 // the return handle contains NULL. |
| 141 | 161 |
| 142 // Compile a String source within a context. | 162 // Compile a String source within a context. |
| 143 static Handle<SharedFunctionInfo> Compile(Handle<String> source, | 163 static Handle<SharedFunctionInfo> Compile(Handle<String> source, |
| 144 Handle<Object> script_name, | 164 Handle<Object> script_name, |
| 145 int line_offset, | 165 int line_offset, |
| 146 int column_offset, | 166 int column_offset, |
| 147 v8::Extension* extension, | 167 v8::Extension* extension, |
| 148 ScriptDataImpl* pre_data, | 168 ScriptDataImpl* pre_data, |
| 149 Handle<Object> script_data, | 169 Handle<Object> script_data, |
| 150 NativesFlag is_natives_code); | 170 NativesFlag is_natives_code); |
| 151 | 171 |
| 152 // Compile a String source within a context for Eval. | 172 // Compile a String source within a context for Eval. |
| 153 static Handle<SharedFunctionInfo> CompileEval(Handle<String> source, | 173 static Handle<SharedFunctionInfo> CompileEval(Handle<String> source, |
| 154 Handle<Context> context, | 174 Handle<Context> context, |
| 155 bool is_global, | 175 bool is_global, |
| 156 ValidationState validation); | 176 ValidationState validation); |
| 157 | 177 |
| 158 // Compile from function info (used for lazy compilation). Returns | 178 // Compile from function info (used for lazy compilation). Returns true on |
| 159 // true on success and false if the compilation resulted in a stack | 179 // success and false if the compilation resulted in a stack overflow. |
| 160 // overflow. | |
| 161 static bool CompileLazy(CompilationInfo* info); | 180 static bool CompileLazy(CompilationInfo* info); |
| 162 | 181 |
| 163 // Compile a shared function info object (the function is possibly | 182 // Compile a shared function info object (the function is possibly lazily |
| 164 // lazily compiled). Called recursively from a backend code | 183 // compiled). |
| 165 // generator 'caller' to build the shared function info. | |
| 166 static Handle<SharedFunctionInfo> BuildFunctionInfo(FunctionLiteral* node, | 184 static Handle<SharedFunctionInfo> BuildFunctionInfo(FunctionLiteral* node, |
| 167 Handle<Script> script, | 185 Handle<Script> script); |
| 168 AstVisitor* caller); | |
| 169 | 186 |
| 170 // Set the function info for a newly compiled function. | 187 // Set the function info for a newly compiled function. |
| 171 static void SetFunctionInfo(Handle<SharedFunctionInfo> function_info, | 188 static void SetFunctionInfo(Handle<SharedFunctionInfo> function_info, |
| 172 FunctionLiteral* lit, | 189 FunctionLiteral* lit, |
| 173 bool is_toplevel, | 190 bool is_toplevel, |
| 174 Handle<Script> script); | 191 Handle<Script> script); |
| 175 | 192 |
| 193 #ifdef ENABLE_DEBUGGER_SUPPORT | |
| 194 static bool MakeCodeForLiveEdit(CompilationInfo* info); | |
| 195 #endif | |
| 196 | |
| 176 private: | 197 private: |
| 177 static void RecordFunctionCompilation(Logger::LogEventsAndTags tag, | 198 static void RecordFunctionCompilation(Logger::LogEventsAndTags tag, |
| 178 Handle<String> name, | 199 Handle<String> name, |
| 179 Handle<String> inferred_name, | |
| 180 int start_position, | 200 int start_position, |
| 181 Handle<Script> script, | 201 CompilationInfo* info); |
| 182 Handle<Code> code); | |
| 183 }; | 202 }; |
| 184 | 203 |
| 185 | 204 |
| 186 #ifdef ENABLE_DEBUGGER_SUPPORT | |
| 187 | |
| 188 Handle<Code> MakeCodeForLiveEdit(CompilationInfo* info); | |
| 189 | |
| 190 #endif | |
| 191 | |
| 192 | |
| 193 // During compilation we need a global list of handles to constants | 205 // During compilation we need a global list of handles to constants |
| 194 // for frame elements. When the zone gets deleted, we make sure to | 206 // for frame elements. When the zone gets deleted, we make sure to |
| 195 // clear this list of handles as well. | 207 // clear this list of handles as well. |
| 196 class CompilationZoneScope : public ZoneScope { | 208 class CompilationZoneScope : public ZoneScope { |
| 197 public: | 209 public: |
| 198 explicit CompilationZoneScope(ZoneScopeMode mode) : ZoneScope(mode) { } | 210 explicit CompilationZoneScope(ZoneScopeMode mode) : ZoneScope(mode) { } |
| 199 virtual ~CompilationZoneScope() { | 211 virtual ~CompilationZoneScope() { |
| 200 if (ShouldDeleteOnExit()) { | 212 if (ShouldDeleteOnExit()) { |
| 201 FrameElement::ClearConstantList(); | 213 FrameElement::ClearConstantList(); |
| 202 Result::ClearConstantList(); | 214 Result::ClearConstantList(); |
| 203 } | 215 } |
| 204 } | 216 } |
| 205 }; | 217 }; |
| 206 | 218 |
| 207 | 219 |
| 208 } } // namespace v8::internal | 220 } } // namespace v8::internal |
| 209 | 221 |
| 210 #endif // V8_COMPILER_H_ | 222 #endif // V8_COMPILER_H_ |
| OLD | NEW |