blob: 66f891bd760ff40efd4d7204ffca21a4d7cf1c0a [file] [log] [blame]
Andrei Popescu31002712010-02-23 13:46:05 +00001// Copyright 2010 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
29#ifndef V8_MIPS_CODEGEN_MIPS_H_
30#define V8_MIPS_CODEGEN_MIPS_H_
31
32namespace v8 {
33namespace internal {
34
35// Forward declarations
36class CompilationInfo;
37class DeferredCode;
38class RegisterAllocator;
39class RegisterFile;
40
41enum InitState { CONST_INIT, NOT_CONST_INIT };
42enum TypeofState { INSIDE_TYPEOF, NOT_INSIDE_TYPEOF };
43
44
Steve Block6ded16b2010-05-10 14:33:55 +010045// -----------------------------------------------------------------------------
46// Reference support
47
48// A reference is a C++ stack-allocated object that keeps an ECMA
49// reference on the execution stack while in scope. For variables
50// the reference is empty, indicating that it isn't necessary to
51// store state on the stack for keeping track of references to those.
52// For properties, we keep either one (named) or two (indexed) values
53// on the execution stack to represent the reference.
54class Reference BASE_EMBEDDED {
55 public:
56 // The values of the types is important, see size().
57 enum Type { UNLOADED = -2, ILLEGAL = -1, SLOT = 0, NAMED = 1, KEYED = 2 };
58 Reference(CodeGenerator* cgen,
59 Expression* expression,
60 bool persist_after_get = false);
61 ~Reference();
62
63 Expression* expression() const { return expression_; }
64 Type type() const { return type_; }
65 void set_type(Type value) {
66 ASSERT_EQ(ILLEGAL, type_);
67 type_ = value;
68 }
69
70 void set_unloaded() {
71 ASSERT_NE(ILLEGAL, type_);
72 ASSERT_NE(UNLOADED, type_);
73 type_ = UNLOADED;
74 }
75 // The size the reference takes up on the stack.
76 int size() const {
77 return (type_ < SLOT) ? 0 : type_;
78 }
79
80 bool is_illegal() const { return type_ == ILLEGAL; }
81 bool is_slot() const { return type_ == SLOT; }
82 bool is_property() const { return type_ == NAMED || type_ == KEYED; }
83 bool is_unloaded() const { return type_ == UNLOADED; }
84
85 // Return the name. Only valid for named property references.
86 Handle<String> GetName();
87
88 // Generate code to push the value of the reference on top of the
89 // expression stack. The reference is expected to be already on top of
90 // the expression stack, and it is consumed by the call unless the
91 // reference is for a compound assignment.
92 // If the reference is not consumed, it is left in place under its value.
93 void GetValue();
94
95 // Generate code to pop a reference, push the value of the reference,
96 // and then spill the stack frame.
97 inline void GetValueAndSpill();
98
99 // Generate code to store the value on top of the expression stack in the
100 // reference. The reference is expected to be immediately below the value
101 // on the expression stack. The value is stored in the location specified
102 // by the reference, and is left on top of the stack, after the reference
103 // is popped from beneath it (unloaded).
104 void SetValue(InitState init_state);
105
106 private:
107 CodeGenerator* cgen_;
108 Expression* expression_;
109 Type type_;
110 // Keep the reference on the stack after get, so it can be used by set later.
111 bool persist_after_get_;
112};
113
114
115// -----------------------------------------------------------------------------
Andrei Popescu31002712010-02-23 13:46:05 +0000116// Code generation state
117
118// The state is passed down the AST by the code generator (and back up, in
119// the form of the state of the label pair). It is threaded through the
120// call stack. Constructing a state implicitly pushes it on the owning code
121// generator's stack of states, and destroying one implicitly pops it.
122
123class CodeGenState BASE_EMBEDDED {
124 public:
125 // Create an initial code generator state. Destroying the initial state
126 // leaves the code generator with a NULL state.
127 explicit CodeGenState(CodeGenerator* owner);
128
129 // Create a code generator state based on a code generator's current
130 // state. The new state has its own typeof state and pair of branch
131 // labels.
132 CodeGenState(CodeGenerator* owner,
133 JumpTarget* true_target,
134 JumpTarget* false_target);
135
136 // Destroy a code generator state and restore the owning code generator's
137 // previous state.
138 ~CodeGenState();
139
140 TypeofState typeof_state() const { return typeof_state_; }
141 JumpTarget* true_target() const { return true_target_; }
142 JumpTarget* false_target() const { return false_target_; }
143
144 private:
145 // The owning code generator.
146 CodeGenerator* owner_;
147
148 // A flag indicating whether we are compiling the immediate subexpression
149 // of a typeof expression.
150 TypeofState typeof_state_;
151
152 JumpTarget* true_target_;
153 JumpTarget* false_target_;
154
155 // The previous state of the owning code generator, restored when
156 // this state is destroyed.
157 CodeGenState* previous_;
158};
159
160
161
Steve Block6ded16b2010-05-10 14:33:55 +0100162// -----------------------------------------------------------------------------
Andrei Popescu31002712010-02-23 13:46:05 +0000163// CodeGenerator
164
165class CodeGenerator: public AstVisitor {
166 public:
167 // Compilation mode. Either the compiler is used as the primary
168 // compiler and needs to setup everything or the compiler is used as
169 // the secondary compiler for split compilation and has to handle
170 // bailouts.
171 enum Mode {
172 PRIMARY,
173 SECONDARY
174 };
175
176 // Takes a function literal, generates code for it. This function should only
177 // be called by compiler.cc.
178 static Handle<Code> MakeCode(CompilationInfo* info);
179
180 // Printing of AST, etc. as requested by flags.
181 static void MakeCodePrologue(CompilationInfo* info);
182
183 // Allocate and install the code.
184 static Handle<Code> MakeCodeEpilogue(MacroAssembler* masm,
185 Code::Flags flags,
186 CompilationInfo* info);
187
188#ifdef ENABLE_LOGGING_AND_PROFILING
189 static bool ShouldGenerateLog(Expression* type);
190#endif
191
192 static void SetFunctionInfo(Handle<JSFunction> fun,
193 FunctionLiteral* lit,
194 bool is_toplevel,
195 Handle<Script> script);
196
197 static void RecordPositions(MacroAssembler* masm, int pos);
198
199 // Accessors
200 MacroAssembler* masm() { return masm_; }
201 VirtualFrame* frame() const { return frame_; }
202 inline Handle<Script> script();
203
204 bool has_valid_frame() const { return frame_ != NULL; }
205
206 // Set the virtual frame to be new_frame, with non-frame register
207 // reference counts given by non_frame_registers. The non-frame
208 // register reference counts of the old frame are returned in
209 // non_frame_registers.
210 void SetFrame(VirtualFrame* new_frame, RegisterFile* non_frame_registers);
211
212 void DeleteFrame();
213
214 RegisterAllocator* allocator() const { return allocator_; }
215
216 CodeGenState* state() { return state_; }
217 void set_state(CodeGenState* state) { state_ = state; }
218
219 void AddDeferred(DeferredCode* code) { deferred_.Add(code); }
220
221 static const int kUnknownIntValue = -1;
222
223 // Number of instructions used for the JS return sequence. The constant is
224 // used by the debugger to patch the JS return sequence.
Steve Block6ded16b2010-05-10 14:33:55 +0100225 static const int kJSReturnSequenceLength = 7;
226
227 // If the name is an inline runtime function call return the number of
228 // expected arguments. Otherwise return -1.
229 static int InlineRuntimeCallArgumentsCount(Handle<String> name);
Andrei Popescu31002712010-02-23 13:46:05 +0000230
231 private:
232 // Construction/Destruction.
233 explicit CodeGenerator(MacroAssembler* masm);
Andrei Popescu31002712010-02-23 13:46:05 +0000234
235 // Accessors.
236 inline bool is_eval();
Steve Block6ded16b2010-05-10 14:33:55 +0100237 inline Scope* scope();
Andrei Popescu31002712010-02-23 13:46:05 +0000238
239 // Generating deferred code.
240 void ProcessDeferred();
241
242 // State
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100243 bool has_cc() const { return cc_reg_ != cc_always; }
Andrei Popescu31002712010-02-23 13:46:05 +0000244 TypeofState typeof_state() const { return state_->typeof_state(); }
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100245 JumpTarget* true_target() const { return state_->true_target(); }
246 JumpTarget* false_target() const { return state_->false_target(); }
Andrei Popescu31002712010-02-23 13:46:05 +0000247
248 // We don't track loop nesting level on mips yet.
249 int loop_nesting() const { return 0; }
250
251 // Node visitors.
252 void VisitStatements(ZoneList<Statement*>* statements);
253
254#define DEF_VISIT(type) \
255 void Visit##type(type* node);
256 AST_NODE_LIST(DEF_VISIT)
257#undef DEF_VISIT
258
Steve Block6ded16b2010-05-10 14:33:55 +0100259 // Visit a statement and then spill the virtual frame if control flow can
260 // reach the end of the statement (ie, it does not exit via break,
261 // continue, return, or throw). This function is used temporarily while
262 // the code generator is being transformed.
263 inline void VisitAndSpill(Statement* statement);
264
265 // Visit a list of statements and then spill the virtual frame if control
266 // flow can reach the end of the list.
267 inline void VisitStatementsAndSpill(ZoneList<Statement*>* statements);
268
Andrei Popescu31002712010-02-23 13:46:05 +0000269 // Main code generation function
Steve Block6ded16b2010-05-10 14:33:55 +0100270 void Generate(CompilationInfo* info);
271
272 // The following are used by class Reference.
273 void LoadReference(Reference* ref);
274 void UnloadReference(Reference* ref);
275
276 MemOperand ContextOperand(Register context, int index) const {
277 return MemOperand(context, Context::SlotOffset(index));
278 }
279
280 MemOperand SlotOperand(Slot* slot, Register tmp);
281
282 // Expressions
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100283 MemOperand GlobalObject() const {
Steve Block6ded16b2010-05-10 14:33:55 +0100284 return ContextOperand(cp, Context::GLOBAL_INDEX);
285 }
286
287 void LoadCondition(Expression* x,
288 JumpTarget* true_target,
289 JumpTarget* false_target,
290 bool force_cc);
291 void Load(Expression* x);
292 void LoadGlobal();
293
294 // Generate code to push the value of an expression on top of the frame
295 // and then spill the frame fully to memory. This function is used
296 // temporarily while the code generator is being transformed.
297 inline void LoadAndSpill(Expression* expression);
298
299 // Read a value from a slot and leave it on top of the expression stack.
300 void LoadFromSlot(Slot* slot, TypeofState typeof_state);
301 // Store the value on top of the stack to a slot.
302 void StoreToSlot(Slot* slot, InitState init_state);
Andrei Popescu31002712010-02-23 13:46:05 +0000303
304 struct InlineRuntimeLUT {
305 void (CodeGenerator::*method)(ZoneList<Expression*>*);
306 const char* name;
Steve Block6ded16b2010-05-10 14:33:55 +0100307 int nargs;
Andrei Popescu31002712010-02-23 13:46:05 +0000308 };
309
310 static InlineRuntimeLUT* FindInlineRuntimeLUT(Handle<String> name);
311 bool CheckForInlineRuntimeCall(CallRuntime* node);
Andrei Popescu31002712010-02-23 13:46:05 +0000312
313 static Handle<Code> ComputeLazyCompile(int argc);
314 void ProcessDeclarations(ZoneList<Declaration*>* declarations);
315
316 Handle<Code> ComputeCallInitialize(int argc, InLoopFlag in_loop);
317
318 // Declare global variables and functions in the given array of
319 // name/value pairs.
320 void DeclareGlobals(Handle<FixedArray> pairs);
321
322 // Support for type checks.
323 void GenerateIsSmi(ZoneList<Expression*>* args);
324 void GenerateIsNonNegativeSmi(ZoneList<Expression*>* args);
325 void GenerateIsArray(ZoneList<Expression*>* args);
Andrei Popescu402d9372010-02-26 13:31:12 +0000326 void GenerateIsRegExp(ZoneList<Expression*>* args);
Andrei Popescu31002712010-02-23 13:46:05 +0000327
328 // Support for construct call checks.
329 void GenerateIsConstructCall(ZoneList<Expression*>* args);
330
331 // Support for arguments.length and arguments[?].
332 void GenerateArgumentsLength(ZoneList<Expression*>* args);
Steve Block6ded16b2010-05-10 14:33:55 +0100333 void GenerateArguments(ZoneList<Expression*>* args);
Andrei Popescu31002712010-02-23 13:46:05 +0000334
335 // Support for accessing the class and value fields of an object.
336 void GenerateClassOf(ZoneList<Expression*>* args);
337 void GenerateValueOf(ZoneList<Expression*>* args);
338 void GenerateSetValueOf(ZoneList<Expression*>* args);
339
340 // Fast support for charCodeAt(n).
341 void GenerateFastCharCodeAt(ZoneList<Expression*>* args);
342
Steve Block6ded16b2010-05-10 14:33:55 +0100343 // Fast support for string.charAt(n) and string[n].
344 void GenerateCharFromCode(ZoneList<Expression*>* args);
345
Andrei Popescu31002712010-02-23 13:46:05 +0000346 // Fast support for object equality testing.
347 void GenerateObjectEquals(ZoneList<Expression*>* args);
348
349 void GenerateLog(ZoneList<Expression*>* args);
350
351 // Fast support for Math.random().
Steve Block6ded16b2010-05-10 14:33:55 +0100352 void GenerateRandomHeapNumber(ZoneList<Expression*>* args);
Andrei Popescu31002712010-02-23 13:46:05 +0000353
354 void GenerateIsObject(ZoneList<Expression*>* args);
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100355 void GenerateIsSpecObject(ZoneList<Expression*>* args);
Andrei Popescu31002712010-02-23 13:46:05 +0000356 void GenerateIsFunction(ZoneList<Expression*>* args);
357 void GenerateIsUndetectableObject(ZoneList<Expression*>* args);
358 void GenerateStringAdd(ZoneList<Expression*>* args);
359 void GenerateSubString(ZoneList<Expression*>* args);
360 void GenerateStringCompare(ZoneList<Expression*>* args);
361 void GenerateRegExpExec(ZoneList<Expression*>* args);
Andrei Popescu402d9372010-02-26 13:31:12 +0000362 void GenerateNumberToString(ZoneList<Expression*>* args);
363
Steve Block6ded16b2010-05-10 14:33:55 +0100364 // Fast call to math functions.
365 void GenerateMathPow(ZoneList<Expression*>* args);
366 void GenerateMathSin(ZoneList<Expression*>* args);
367 void GenerateMathCos(ZoneList<Expression*>* args);
368 void GenerateMathSqrt(ZoneList<Expression*>* args);
Andrei Popescu31002712010-02-23 13:46:05 +0000369
370 // Simple condition analysis.
371 enum ConditionAnalysis {
372 ALWAYS_TRUE,
373 ALWAYS_FALSE,
374 DONT_KNOW
375 };
376 ConditionAnalysis AnalyzeCondition(Expression* cond);
377
378 // Methods used to indicate which source code is generated for. Source
379 // positions are collected by the assembler and emitted with the relocation
380 // information.
381 void CodeForFunctionPosition(FunctionLiteral* fun);
382 void CodeForReturnPosition(FunctionLiteral* fun);
383 void CodeForStatementPosition(Statement* node);
384 void CodeForDoWhileConditionPosition(DoWhileStatement* stmt);
385 void CodeForSourcePosition(int pos);
386
387#ifdef DEBUG
388 // True if the registers are valid for entry to a block.
389 bool HasValidEntryRegisters();
390#endif
391
392 bool is_eval_; // Tells whether code is generated for eval.
393
394 Handle<Script> script_;
395 List<DeferredCode*> deferred_;
396
397 // Assembler
398 MacroAssembler* masm_; // to generate code
399
400 CompilationInfo* info_;
401
402 // Code generation state
Andrei Popescu31002712010-02-23 13:46:05 +0000403 VirtualFrame* frame_;
404 RegisterAllocator* allocator_;
405 Condition cc_reg_;
406 CodeGenState* state_;
407
408 // Jump targets
409 BreakTarget function_return_;
410
411 // True if the function return is shadowed (ie, jumping to the target
412 // function_return_ does not jump to the true function return, but rather
413 // to some unlinking code).
414 bool function_return_is_shadowed_;
415
416 static InlineRuntimeLUT kInlineRuntimeLUT[];
417
418 friend class VirtualFrame;
419 friend class JumpTarget;
420 friend class Reference;
421 friend class FastCodeGenerator;
Steve Block6ded16b2010-05-10 14:33:55 +0100422 friend class FullCodeGenerator;
Andrei Popescu31002712010-02-23 13:46:05 +0000423 friend class FullCodeGenSyntaxChecker;
424
425 DISALLOW_COPY_AND_ASSIGN(CodeGenerator);
426};
427
428
429} } // namespace v8::internal
430
431#endif // V8_MIPS_CODEGEN_MIPS_H_