Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(123)

Side by Side Diff: src/codegen-ia32.h

Issue 6334: Simplify CodeGenerator hierarchy by not using a base class. (Closed) Base URL: http://v8.googlecode.com/svn/branches/bleeding_edge/
Patch Set: '' Created 12 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/codegen-arm.cc ('k') | src/codegen-ia32.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2006-2008 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
4 // met:
5 //
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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.
27
28 #ifndef V8_CODEGEN_IA32_H_
29 #define V8_CODEGEN_IA32_H_
30
31 #include "scopes.h"
32
33 namespace v8 { namespace internal {
34
35 // Forward declarations
36 class DeferredCode;
37
38 // Mode to overwrite BinaryExpression values.
39 enum OverwriteMode { NO_OVERWRITE, OVERWRITE_LEFT, OVERWRITE_RIGHT };
40
41
42 // -----------------------------------------------------------------------------
43 // Reference support
44
45 // A reference is a C++ stack-allocated object that keeps an ECMA
46 // reference on the execution stack while in scope. For variables
47 // the reference is empty, indicating that it isn't necessary to
48 // store state on the stack for keeping track of references to those.
49 // For properties, we keep either one (named) or two (indexed) values
50 // on the execution stack to represent the reference.
51
52 enum InitState { CONST_INIT, NOT_CONST_INIT };
53 enum TypeofState { INSIDE_TYPEOF, NOT_INSIDE_TYPEOF };
54
55 class Reference BASE_EMBEDDED {
56 public:
57 // The values of the types is important, see size().
58 enum Type { ILLEGAL = -1, SLOT = 0, NAMED = 1, KEYED = 2 };
59 Reference(CodeGenerator* cgen, Expression* expression);
60 ~Reference();
61
62 Expression* expression() const { return expression_; }
63 Type type() const { return type_; }
64 void set_type(Type value) {
65 ASSERT(type_ == ILLEGAL);
66 type_ = value;
67 }
68
69 // The size of the reference or -1 if the reference is illegal.
70 int size() const { return type_; }
71
72 bool is_illegal() const { return type_ == ILLEGAL; }
73 bool is_slot() const { return type_ == SLOT; }
74 bool is_property() const { return type_ == NAMED || type_ == KEYED; }
75
76 // Return the name. Only valid for named property references.
77 Handle<String> GetName();
78
79 // Generate code to push the value of the reference on top of the
80 // expression stack. The reference is expected to be already on top of
81 // the expression stack, and it is left in place with its value above it.
82 void GetValue(TypeofState typeof_state);
83
84 // Generate code to store the value on top of the expression stack in the
85 // reference. The reference is expected to be immediately below the value
86 // on the expression stack. The stored value is left in place (with the
87 // reference intact below it) to support chained assignments.
88 void SetValue(InitState init_state);
89
90 private:
91 CodeGenerator* cgen_;
92 Expression* expression_;
93 Type type_;
94 };
95
96
97 // -------------------------------------------------------------------------
98 // Code generation state
99
100 // The state is passed down the AST by the code generator (and back up, in
101 // the form of the state of the label pair). It is threaded through the
102 // call stack. Constructing a state implicitly pushes it on the owning code
103 // generator's stack of states, and destroying one implicitly pops it.
104
105 class CodeGenState BASE_EMBEDDED {
106 public:
107 // Create an initial code generator state. Destroying the initial state
108 // leaves the code generator with a NULL state.
109 explicit CodeGenState(CodeGenerator* owner);
110
111 // Create a code generator state based on a code generator's current
112 // state. The new state has its own access type and pair of branch
113 // labels, and no reference.
114 CodeGenState(CodeGenerator* owner,
115 TypeofState typeof_state,
116 Label* true_target,
117 Label* false_target);
118
119 // Destroy a code generator state and restore the owning code generator's
120 // previous state.
121 ~CodeGenState();
122
123 TypeofState typeof_state() const { return typeof_state_; }
124 Label* true_target() const { return true_target_; }
125 Label* false_target() const { return false_target_; }
126
127 private:
128 CodeGenerator* owner_;
129 TypeofState typeof_state_;
130 Label* true_target_;
131 Label* false_target_;
132 CodeGenState* previous_;
133 };
134
135
136
137
138 // -----------------------------------------------------------------------------
139 // CodeGenerator
140
141 //
142 class CodeGenerator: public Visitor {
143 public:
144 // Takes a function literal, generates code for it. This function should only
145 // be called by compiler.cc.
146 static Handle<Code> MakeCode(FunctionLiteral* fun,
147 Handle<Script> script,
148 bool is_eval);
149
150 static void SetFunctionInfo(Handle<JSFunction> fun,
151 int length,
152 int function_token_position,
153 int start_position,
154 int end_position,
155 bool is_expression,
156 bool is_toplevel,
157 Handle<Script> script);
158
159 // Accessors
160 MacroAssembler* masm() { return masm_; }
161
162 CodeGenState* state() { return state_; }
163 void set_state(CodeGenState* state) { state_ = state; }
164
165 void AddDeferred(DeferredCode* code) { deferred_.Add(code); }
166
167 private:
168 // Construction/Destruction
169 CodeGenerator(int buffer_size, Handle<Script> script, bool is_eval);
170 virtual ~CodeGenerator() { delete masm_; }
171
172 // Accessors
173 Scope* scope() const { return scope_; }
174
175 void ProcessDeferred();
176
177 bool is_eval() { return is_eval_; }
178
179 // State
180 bool has_cc() const { return cc_reg_ >= 0; }
181 TypeofState typeof_state() const { return state_->typeof_state(); }
182 Label* true_target() const { return state_->true_target(); }
183 Label* false_target() const { return state_->false_target(); }
184
185
186 // Node visitors.
187 #define DEF_VISIT(type) \
188 void Visit##type(type* node);
189 NODE_LIST(DEF_VISIT)
190 #undef DEF_VISIT
191
192 // Main code generation function
193 void GenCode(FunctionLiteral* fun);
194
195 // The following are used by class Reference.
196 void LoadReference(Reference* ref);
197 void UnloadReference(Reference* ref);
198
199 // Support functions for accessing parameters and other operands.
200 Operand ParameterOperand(int index) const {
201 int num_parameters = scope()->num_parameters();
202 ASSERT(-2 <= index && index < num_parameters);
203 return Operand(ebp, (1 + num_parameters - index) * kPointerSize);
204 }
205
206 Operand ReceiverOperand() const { return ParameterOperand(-1); }
207
208 Operand FunctionOperand() const {
209 return Operand(ebp, JavaScriptFrameConstants::kFunctionOffset);
210 }
211
212 Operand ContextOperand(Register context, int index) const {
213 return Operand(context, Context::SlotOffset(index));
214 }
215
216 Operand SlotOperand(Slot* slot, Register tmp);
217
218
219 // Expressions
220 Operand GlobalObject() const {
221 return ContextOperand(esi, Context::GLOBAL_INDEX);
222 }
223
224 void LoadCondition(Expression* x,
225 TypeofState typeof_state,
226 Label* true_target,
227 Label* false_target,
228 bool force_cc);
229 void Load(Expression* x, TypeofState typeof_state = NOT_INSIDE_TYPEOF);
230 void LoadGlobal();
231
232 // Read a value from a slot and leave it on top of the expression stack.
233 void LoadFromSlot(Slot* slot, TypeofState typeof_state);
234
235 // Special code for typeof expressions: Unfortunately, we must
236 // be careful when loading the expression in 'typeof'
237 // expressions. We are not allowed to throw reference errors for
238 // non-existing properties of the global object, so we must make it
239 // look like an explicit property access, instead of an access
240 // through the context chain.
241 void LoadTypeofExpression(Expression* x);
242
243 void ToBoolean(Label* true_target, Label* false_target);
244
245 void GenericBinaryOperation(Token::Value op,
246 const OverwriteMode overwrite_mode = NO_OVERWRITE);
247 void Comparison(Condition cc, bool strict = false);
248
249 // Inline small integer literals. To prevent long attacker-controlled byte
250 // sequences, we only inline small Smi:s.
251 static const int kMaxSmiInlinedBits = 16;
252 bool IsInlineSmi(Literal* literal);
253 void SmiComparison(Condition cc, Handle<Object> value, bool strict = false);
254 void SmiOperation(Token::Value op,
255 Handle<Object> value,
256 bool reversed,
257 OverwriteMode overwrite_mode);
258
259 void CallWithArguments(ZoneList<Expression*>* arguments, int position);
260
261 // Control flow
262 void Branch(bool if_true, Label* L);
263 void CheckStack();
264 void CleanStack(int num_bytes);
265
266 bool CheckForInlineRuntimeCall(CallRuntime* node);
267 Handle<JSFunction> BuildBoilerplate(FunctionLiteral* node);
268 void ProcessDeclarations(ZoneList<Declaration*>* declarations);
269
270 Handle<Code> ComputeCallInitialize(int argc);
271
272 // Declare global variables and functions in the given array of
273 // name/value pairs.
274 void DeclareGlobals(Handle<FixedArray> pairs);
275
276 // Instantiate the function boilerplate.
277 void InstantiateBoilerplate(Handle<JSFunction> boilerplate);
278
279 // Support for type checks.
280 void GenerateIsSmi(ZoneList<Expression*>* args);
281 void GenerateIsNonNegativeSmi(ZoneList<Expression*>* args);
282 void GenerateIsArray(ZoneList<Expression*>* args);
283
284 // Support for arguments.length and arguments[?].
285 void GenerateArgumentsLength(ZoneList<Expression*>* args);
286 void GenerateArgumentsAccess(ZoneList<Expression*>* args);
287
288 // Support for accessing the value field of an object (used by Date).
289 void GenerateValueOf(ZoneList<Expression*>* args);
290 void GenerateSetValueOf(ZoneList<Expression*>* args);
291
292 // Fast support for charCodeAt(n).
293 void GenerateFastCharCodeAt(ZoneList<Expression*>* args);
294
295 // Fast support for object equality testing.
296 void GenerateObjectEquals(ZoneList<Expression*>* args);
297
298
299 // Methods and constants for fast case switch statement support.
300 //
301 // Only allow fast-case switch if the range of labels is at most
302 // this factor times the number of case labels.
303 // Value is derived from comparing the size of code generated by the normal
304 // switch code for Smi-labels to the size of a single pointer. If code
305 // quality increases this number should be decreased to match.
306 static const int kFastSwitchMaxOverheadFactor = 5;
307
308 // Minimal number of switch cases required before we allow jump-table
309 // optimization.
310 static const int kFastSwitchMinCaseCount = 5;
311
312 // The limit of the range of a fast-case switch, as a factor of the number
313 // of cases of the switch. Each platform should return a value that
314 // is optimal compared to the default code generated for a switch statement
315 // on that platform.
316 int FastCaseSwitchMaxOverheadFactor();
317
318 // The minimal number of cases in a switch before the fast-case switch
319 // optimization is enabled. Each platform should return a value that
320 // is optimal compared to the default code generated for a switch statement
321 // on that platform.
322 int FastCaseSwitchMinCaseCount();
323
324 // Allocate a jump table and create code to jump through it.
325 // Should call GenerateFastCaseSwitchCases to generate the code for
326 // all the cases at the appropriate point.
327 void GenerateFastCaseSwitchJumpTable(SwitchStatement* node, int min_index,
328 int range, Label *fail_label,
329 SmartPointer<Label*> &case_targets,
330 SmartPointer<Label>& case_labels);
331
332 // Generate the code for cases for the fast case switch.
333 // Called by GenerateFastCaseSwitchJumpTable.
334 void GenerateFastCaseSwitchCases(SwitchStatement* node,
335 SmartPointer<Label> &case_labels);
336
337 // Fast support for constant-Smi switches.
338 void GenerateFastCaseSwitchStatement(SwitchStatement *node, int min_index,
339 int range, int default_index);
340
341 // Fast support for constant-Smi switches. Tests whether switch statement
342 // permits optimization and calls GenerateFastCaseSwitch if it does.
343 // Returns true if the fast-case switch was generated, and false if not.
344 bool TryGenerateFastCaseSwitchStatement(SwitchStatement *node);
345
346
347 // Bottle-neck interface to call the Assembler to generate the statement
348 // position. This allows us to easily control whether statement positions
349 // should be generated or not.
350 void RecordStatementPosition(Node* node);
351
352 // Activation frames.
353 void EnterJSFrame();
354 void ExitJSFrame();
355
356
357 bool is_eval_; // Tells whether code is generated for eval.
358 Handle<Script> script_;
359 List<DeferredCode*> deferred_;
360
361 // Assembler
362 MacroAssembler* masm_; // to generate code
363
364 // Code generation state
365 Scope* scope_;
366 Condition cc_reg_;
367 CodeGenState* state_;
368 bool is_inside_try_;
369 int break_stack_height_;
370
371 // Labels
372 Label function_return_;
373
374 friend class Reference;
375 friend class Property;
376 friend class VariableProxy;
377 friend class Slot;
378
379 DISALLOW_COPY_AND_ASSIGN(CodeGenerator);
380 };
381
382
383 } } // namespace v8::internal
384
385 #endif // V8_CODEGEN_IA32_H_
OLDNEW
« no previous file with comments | « src/codegen-arm.cc ('k') | src/codegen-ia32.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698