blob: 0d1a3855975816820d0e91c5ed5e6cad6b21f841 [file] [log] [blame]
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001// Copyright 2010 the V8 project authors. All rights reserved.
ager@chromium.org7c537e22008-10-16 08:43:32 +00002// 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
ager@chromium.org5ec48922009-05-05 07:25:34 +000028#ifndef V8_ARM_CODEGEN_ARM_H_
29#define V8_ARM_CODEGEN_ARM_H_
ager@chromium.org7c537e22008-10-16 08:43:32 +000030
kasperl@chromium.org71affb52009-05-26 05:44:31 +000031namespace v8 {
32namespace internal {
ager@chromium.org7c537e22008-10-16 08:43:32 +000033
34// Forward declarations
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000035class CompilationInfo;
ager@chromium.org7c537e22008-10-16 08:43:32 +000036class DeferredCode;
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000037class RegisterAllocator;
38class RegisterFile;
ager@chromium.org7c537e22008-10-16 08:43:32 +000039
ager@chromium.org3bf7b912008-11-17 09:09:45 +000040enum InitState { CONST_INIT, NOT_CONST_INIT };
41enum TypeofState { INSIDE_TYPEOF, NOT_INSIDE_TYPEOF };
ager@chromium.org7c537e22008-10-16 08:43:32 +000042
ager@chromium.org3bf7b912008-11-17 09:09:45 +000043
44// -------------------------------------------------------------------------
ager@chromium.org7c537e22008-10-16 08:43:32 +000045// Reference support
46
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000047// A reference is a C++ stack-allocated object that puts a
48// reference on the virtual frame. The reference may be consumed
49// by GetValue, TakeValue, SetValue, and Codegen::UnloadReference.
50// When the lifetime (scope) of a valid reference ends, it must have
51// been consumed, and be in state UNLOADED.
ager@chromium.org7c537e22008-10-16 08:43:32 +000052class Reference BASE_EMBEDDED {
53 public:
54 // The values of the types is important, see size().
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000055 enum Type { UNLOADED = -2, ILLEGAL = -1, SLOT = 0, NAMED = 1, KEYED = 2 };
56 Reference(CodeGenerator* cgen,
57 Expression* expression,
58 bool persist_after_get = false);
ager@chromium.org7c537e22008-10-16 08:43:32 +000059 ~Reference();
60
61 Expression* expression() const { return expression_; }
62 Type type() const { return type_; }
63 void set_type(Type value) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000064 ASSERT_EQ(ILLEGAL, type_);
ager@chromium.org7c537e22008-10-16 08:43:32 +000065 type_ = value;
66 }
67
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000068 void set_unloaded() {
69 ASSERT_NE(ILLEGAL, type_);
70 ASSERT_NE(UNLOADED, type_);
71 type_ = UNLOADED;
72 }
ager@chromium.org3bf7b912008-11-17 09:09:45 +000073 // The size the reference takes up on the stack.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000074 int size() const {
75 return (type_ < SLOT) ? 0 : type_;
76 }
ager@chromium.org7c537e22008-10-16 08:43:32 +000077
78 bool is_illegal() const { return type_ == ILLEGAL; }
79 bool is_slot() const { return type_ == SLOT; }
80 bool is_property() const { return type_ == NAMED || type_ == KEYED; }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000081 bool is_unloaded() const { return type_ == UNLOADED; }
ager@chromium.org7c537e22008-10-16 08:43:32 +000082
83 // Return the name. Only valid for named property references.
84 Handle<String> GetName();
85
86 // Generate code to push the value of the reference on top of the
87 // expression stack. The reference is expected to be already on top of
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000088 // the expression stack, and it is consumed by the call unless the
89 // reference is for a compound assignment.
90 // If the reference is not consumed, it is left in place under its value.
ager@chromium.orgc4c92722009-11-18 14:12:51 +000091 void GetValue();
ager@chromium.org7c537e22008-10-16 08:43:32 +000092
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000093 // Generate code to pop a reference, push the value of the reference,
94 // and then spill the stack frame.
ager@chromium.orgc4c92722009-11-18 14:12:51 +000095 inline void GetValueAndSpill();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +000096
ager@chromium.org7c537e22008-10-16 08:43:32 +000097 // Generate code to store the value on top of the expression stack in the
98 // reference. The reference is expected to be immediately below the value
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000099 // on the expression stack. The value is stored in the location specified
100 // by the reference, and is left on top of the stack, after the reference
101 // is popped from beneath it (unloaded).
ager@chromium.org7c537e22008-10-16 08:43:32 +0000102 void SetValue(InitState init_state);
103
104 private:
105 CodeGenerator* cgen_;
106 Expression* expression_;
107 Type type_;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000108 // Keep the reference on the stack after get, so it can be used by set later.
109 bool persist_after_get_;
ager@chromium.org7c537e22008-10-16 08:43:32 +0000110};
111
112
113// -------------------------------------------------------------------------
114// Code generation state
115
116// The state is passed down the AST by the code generator (and back up, in
117// the form of the state of the label pair). It is threaded through the
118// call stack. Constructing a state implicitly pushes it on the owning code
119// generator's stack of states, and destroying one implicitly pops it.
120
121class CodeGenState BASE_EMBEDDED {
122 public:
123 // Create an initial code generator state. Destroying the initial state
124 // leaves the code generator with a NULL state.
125 explicit CodeGenState(CodeGenerator* owner);
126
127 // Create a code generator state based on a code generator's current
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000128 // state. The new state has its own pair of branch labels.
ager@chromium.org7c537e22008-10-16 08:43:32 +0000129 CodeGenState(CodeGenerator* owner,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000130 JumpTarget* true_target,
131 JumpTarget* false_target);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000132
133 // Destroy a code generator state and restore the owning code generator's
134 // previous state.
135 ~CodeGenState();
136
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000137 JumpTarget* true_target() const { return true_target_; }
138 JumpTarget* false_target() const { return false_target_; }
ager@chromium.org7c537e22008-10-16 08:43:32 +0000139
140 private:
141 CodeGenerator* owner_;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000142 JumpTarget* true_target_;
143 JumpTarget* false_target_;
ager@chromium.org7c537e22008-10-16 08:43:32 +0000144 CodeGenState* previous_;
145};
146
147
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000148// -------------------------------------------------------------------------
ager@chromium.org7c537e22008-10-16 08:43:32 +0000149// CodeGenerator
150
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000151class CodeGenerator: public AstVisitor {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000152 public:
153 // Takes a function literal, generates code for it. This function should only
154 // be called by compiler.cc.
ager@chromium.org5c838252010-02-19 08:53:10 +0000155 static Handle<Code> MakeCode(CompilationInfo* info);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000156
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000157 // Printing of AST, etc. as requested by flags.
ager@chromium.org5c838252010-02-19 08:53:10 +0000158 static void MakeCodePrologue(CompilationInfo* info);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000159
160 // Allocate and install the code.
ager@chromium.org5c838252010-02-19 08:53:10 +0000161 static Handle<Code> MakeCodeEpilogue(MacroAssembler* masm,
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000162 Code::Flags flags,
ager@chromium.org5c838252010-02-19 08:53:10 +0000163 CompilationInfo* info);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000164
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +0000165#ifdef ENABLE_LOGGING_AND_PROFILING
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000166 static bool ShouldGenerateLog(Expression* type);
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +0000167#endif
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000168
ager@chromium.org7c537e22008-10-16 08:43:32 +0000169 static void SetFunctionInfo(Handle<JSFunction> fun,
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000170 FunctionLiteral* lit,
ager@chromium.org7c537e22008-10-16 08:43:32 +0000171 bool is_toplevel,
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000172 Handle<Script> script);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000173
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000174 static void RecordPositions(MacroAssembler* masm, int pos);
175
ager@chromium.org7c537e22008-10-16 08:43:32 +0000176 // Accessors
177 MacroAssembler* masm() { return masm_; }
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000178 VirtualFrame* frame() const { return frame_; }
ager@chromium.org5c838252010-02-19 08:53:10 +0000179 inline Handle<Script> script();
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000180
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000181 bool has_valid_frame() const { return frame_ != NULL; }
182
183 // Set the virtual frame to be new_frame, with non-frame register
184 // reference counts given by non_frame_registers. The non-frame
185 // register reference counts of the old frame are returned in
186 // non_frame_registers.
187 void SetFrame(VirtualFrame* new_frame, RegisterFile* non_frame_registers);
188
189 void DeleteFrame();
190
191 RegisterAllocator* allocator() const { return allocator_; }
192
ager@chromium.org7c537e22008-10-16 08:43:32 +0000193 CodeGenState* state() { return state_; }
194 void set_state(CodeGenState* state) { state_ = state; }
195
196 void AddDeferred(DeferredCode* code) { deferred_.Add(code); }
197
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000198 static const int kUnknownIntValue = -1;
199
vegorov@chromium.orgf8372902010-03-15 10:26:20 +0000200 // If the name is an inline runtime function call return the number of
201 // expected arguments. Otherwise return -1.
202 static int InlineRuntimeCallArgumentsCount(Handle<String> name);
203
ager@chromium.org7c537e22008-10-16 08:43:32 +0000204 private:
205 // Construction/Destruction
ager@chromium.org5c838252010-02-19 08:53:10 +0000206 explicit CodeGenerator(MacroAssembler* masm);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000207
208 // Accessors
ager@chromium.org5c838252010-02-19 08:53:10 +0000209 inline bool is_eval();
sgjesse@chromium.org833cdd72010-02-26 10:06:16 +0000210 inline Scope* scope();
ager@chromium.org7c537e22008-10-16 08:43:32 +0000211
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000212 // Generating deferred code.
ager@chromium.org7c537e22008-10-16 08:43:32 +0000213 void ProcessDeferred();
214
ager@chromium.org7c537e22008-10-16 08:43:32 +0000215 // State
216 bool has_cc() const { return cc_reg_ != al; }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000217 JumpTarget* true_target() const { return state_->true_target(); }
218 JumpTarget* false_target() const { return state_->false_target(); }
ager@chromium.org7c537e22008-10-16 08:43:32 +0000219
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000220 // We don't track loop nesting level on ARM yet.
221 int loop_nesting() const { return 0; }
ager@chromium.org7c537e22008-10-16 08:43:32 +0000222
223 // Node visitors.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000224 void VisitStatements(ZoneList<Statement*>* statements);
225
ager@chromium.org7c537e22008-10-16 08:43:32 +0000226#define DEF_VISIT(type) \
227 void Visit##type(type* node);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000228 AST_NODE_LIST(DEF_VISIT)
ager@chromium.org7c537e22008-10-16 08:43:32 +0000229#undef DEF_VISIT
230
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000231 // Visit a statement and then spill the virtual frame if control flow can
232 // reach the end of the statement (ie, it does not exit via break,
233 // continue, return, or throw). This function is used temporarily while
234 // the code generator is being transformed.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +0000235 inline void VisitAndSpill(Statement* statement);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000236
237 // Visit a list of statements and then spill the virtual frame if control
238 // flow can reach the end of the list.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +0000239 inline void VisitStatementsAndSpill(ZoneList<Statement*>* statements);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000240
ager@chromium.org7c537e22008-10-16 08:43:32 +0000241 // Main code generation function
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +0000242 void Generate(CompilationInfo* info);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000243
244 // The following are used by class Reference.
245 void LoadReference(Reference* ref);
246 void UnloadReference(Reference* ref);
247
ager@chromium.org3811b432009-10-28 14:53:37 +0000248 static MemOperand ContextOperand(Register context, int index) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000249 return MemOperand(context, Context::SlotOffset(index));
250 }
251
252 MemOperand SlotOperand(Slot* slot, Register tmp);
253
ager@chromium.org381abbb2009-02-25 13:23:22 +0000254 MemOperand ContextSlotOperandCheckExtensions(Slot* slot,
255 Register tmp,
256 Register tmp2,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000257 JumpTarget* slow);
ager@chromium.org381abbb2009-02-25 13:23:22 +0000258
ager@chromium.org7c537e22008-10-16 08:43:32 +0000259 // Expressions
ager@chromium.org3811b432009-10-28 14:53:37 +0000260 static MemOperand GlobalObject() {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000261 return ContextOperand(cp, Context::GLOBAL_INDEX);
262 }
263
264 void LoadCondition(Expression* x,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000265 JumpTarget* true_target,
266 JumpTarget* false_target,
ager@chromium.org7c537e22008-10-16 08:43:32 +0000267 bool force_cc);
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000268 void Load(Expression* expr);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000269 void LoadGlobal();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000270 void LoadGlobalReceiver(Register scratch);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000271
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000272 // Generate code to push the value of an expression on top of the frame
273 // and then spill the frame fully to memory. This function is used
274 // temporarily while the code generator is being transformed.
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000275 inline void LoadAndSpill(Expression* expression);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000276
277 // Call LoadCondition and then spill the virtual frame unless control flow
278 // cannot reach the end of the expression (ie, by emitting only
279 // unconditional jumps to the control targets).
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +0000280 inline void LoadConditionAndSpill(Expression* expression,
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +0000281 JumpTarget* true_target,
282 JumpTarget* false_target,
283 bool force_control);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000284
ager@chromium.org7c537e22008-10-16 08:43:32 +0000285 // Read a value from a slot and leave it on top of the expression stack.
286 void LoadFromSlot(Slot* slot, TypeofState typeof_state);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000287 // Store the value on top of the stack to a slot.
288 void StoreToSlot(Slot* slot, InitState init_state);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000289 // Load a keyed property, leaving it in r0. The receiver and key are
290 // passed on the stack, and remain there.
291 void EmitKeyedLoad(bool is_global);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000292
ager@chromium.org381abbb2009-02-25 13:23:22 +0000293 void LoadFromGlobalSlotCheckExtensions(Slot* slot,
294 TypeofState typeof_state,
295 Register tmp,
296 Register tmp2,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000297 JumpTarget* slow);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000298
299 // Special code for typeof expressions: Unfortunately, we must
300 // be careful when loading the expression in 'typeof'
301 // expressions. We are not allowed to throw reference errors for
302 // non-existing properties of the global object, so we must make it
303 // look like an explicit property access, instead of an access
304 // through the context chain.
305 void LoadTypeofExpression(Expression* x);
306
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000307 void ToBoolean(JumpTarget* true_target, JumpTarget* false_target);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000308
ager@chromium.orgb26c50a2010-03-26 09:27:16 +0000309 // Generate code that computes a shortcutting logical operation.
310 void GenerateLogicalBooleanOperation(BinaryOperation* node);
311
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000312 void GenericBinaryOperation(Token::Value op,
313 OverwriteMode overwrite_mode,
314 int known_rhs = kUnknownIntValue);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000315 void Comparison(Condition cc,
316 Expression* left,
317 Expression* right,
318 bool strict = false);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000319
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000320 void SmiOperation(Token::Value op,
321 Handle<Object> value,
322 bool reversed,
323 OverwriteMode mode);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000324
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000325 void CallWithArguments(ZoneList<Expression*>* arguments,
326 CallFunctionFlags flags,
327 int position);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000328
329 // Control flow
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000330 void Branch(bool if_true, JumpTarget* target);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000331 void CheckStack();
ager@chromium.org7c537e22008-10-16 08:43:32 +0000332
ager@chromium.org9085a012009-05-11 19:22:57 +0000333 struct InlineRuntimeLUT {
334 void (CodeGenerator::*method)(ZoneList<Expression*>*);
335 const char* name;
vegorov@chromium.orgf8372902010-03-15 10:26:20 +0000336 int nargs;
ager@chromium.org9085a012009-05-11 19:22:57 +0000337 };
338
339 static InlineRuntimeLUT* FindInlineRuntimeLUT(Handle<String> name);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000340 bool CheckForInlineRuntimeCall(CallRuntime* node);
ager@chromium.org9085a012009-05-11 19:22:57 +0000341 static bool PatchInlineRuntimeEntry(Handle<String> name,
342 const InlineRuntimeLUT& new_entry,
343 InlineRuntimeLUT* old_entry);
344
ager@chromium.org3811b432009-10-28 14:53:37 +0000345 static Handle<Code> ComputeLazyCompile(int argc);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000346 void ProcessDeclarations(ZoneList<Declaration*>* declarations);
347
ager@chromium.org3811b432009-10-28 14:53:37 +0000348 static Handle<Code> ComputeCallInitialize(int argc, InLoopFlag in_loop);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000349
350 // Declare global variables and functions in the given array of
351 // name/value pairs.
352 void DeclareGlobals(Handle<FixedArray> pairs);
353
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000354 // Instantiate the function based on the shared function info.
355 void InstantiateFunction(Handle<SharedFunctionInfo> function_info);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000356
357 // Support for type checks.
358 void GenerateIsSmi(ZoneList<Expression*>* args);
359 void GenerateIsNonNegativeSmi(ZoneList<Expression*>* args);
360 void GenerateIsArray(ZoneList<Expression*>* args);
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +0000361 void GenerateIsRegExp(ZoneList<Expression*>* args);
ager@chromium.org6141cbe2009-11-20 12:14:52 +0000362 void GenerateIsObject(ZoneList<Expression*>* args);
363 void GenerateIsFunction(ZoneList<Expression*>* args);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000364 void GenerateIsUndetectableObject(ZoneList<Expression*>* args);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000365
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000366 // Support for construct call checks.
367 void GenerateIsConstructCall(ZoneList<Expression*>* args);
368
ager@chromium.org7c537e22008-10-16 08:43:32 +0000369 // Support for arguments.length and arguments[?].
370 void GenerateArgumentsLength(ZoneList<Expression*>* args);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +0000371 void GenerateArguments(ZoneList<Expression*>* args);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000372
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000373 // Support for accessing the class and value fields of an object.
374 void GenerateClassOf(ZoneList<Expression*>* args);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000375 void GenerateValueOf(ZoneList<Expression*>* args);
376 void GenerateSetValueOf(ZoneList<Expression*>* args);
377
378 // Fast support for charCodeAt(n).
379 void GenerateFastCharCodeAt(ZoneList<Expression*>* args);
380
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000381 // Fast support for string.charAt(n) and string[n].
382 void GenerateCharFromCode(ZoneList<Expression*>* args);
383
ager@chromium.org7c537e22008-10-16 08:43:32 +0000384 // Fast support for object equality testing.
385 void GenerateObjectEquals(ZoneList<Expression*>* args);
386
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000387 void GenerateLog(ZoneList<Expression*>* args);
388
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000389 // Fast support for Math.random().
390 void GenerateRandomPositiveSmi(ZoneList<Expression*>* args);
391
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000392 // Fast support for StringAdd.
393 void GenerateStringAdd(ZoneList<Expression*>* args);
394
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000395 // Fast support for SubString.
396 void GenerateSubString(ZoneList<Expression*>* args);
397
398 // Fast support for StringCompare.
399 void GenerateStringCompare(ZoneList<Expression*>* args);
400
401 // Support for direct calls from JavaScript to native RegExp code.
402 void GenerateRegExpExec(ZoneList<Expression*>* args);
403
ager@chromium.org5c838252010-02-19 08:53:10 +0000404 // Fast support for number to string.
405 void GenerateNumberToString(ZoneList<Expression*>* args);
406
vegorov@chromium.orgf8372902010-03-15 10:26:20 +0000407 // Fast call to math functions.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000408 void GenerateMathPow(ZoneList<Expression*>* args);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000409 void GenerateMathSin(ZoneList<Expression*>* args);
410 void GenerateMathCos(ZoneList<Expression*>* args);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000411 void GenerateMathSqrt(ZoneList<Expression*>* args);
412
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000413 // Simple condition analysis.
414 enum ConditionAnalysis {
415 ALWAYS_TRUE,
416 ALWAYS_FALSE,
417 DONT_KNOW
418 };
419 ConditionAnalysis AnalyzeCondition(Expression* cond);
420
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000421 // Methods used to indicate which source code is generated for. Source
422 // positions are collected by the assembler and emitted with the relocation
423 // information.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000424 void CodeForFunctionPosition(FunctionLiteral* fun);
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +0000425 void CodeForReturnPosition(FunctionLiteral* fun);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000426 void CodeForStatementPosition(Statement* node);
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000427 void CodeForDoWhileConditionPosition(DoWhileStatement* stmt);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000428 void CodeForSourcePosition(int pos);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000429
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000430#ifdef DEBUG
431 // True if the registers are valid for entry to a block.
432 bool HasValidEntryRegisters();
433#endif
434
ager@chromium.org7c537e22008-10-16 08:43:32 +0000435 List<DeferredCode*> deferred_;
436
437 // Assembler
438 MacroAssembler* masm_; // to generate code
439
ager@chromium.org5c838252010-02-19 08:53:10 +0000440 CompilationInfo* info_;
441
ager@chromium.org7c537e22008-10-16 08:43:32 +0000442 // Code generation state
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000443 VirtualFrame* frame_;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000444 RegisterAllocator* allocator_;
ager@chromium.org7c537e22008-10-16 08:43:32 +0000445 Condition cc_reg_;
446 CodeGenState* state_;
ager@chromium.org7c537e22008-10-16 08:43:32 +0000447
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000448 // Jump targets
449 BreakTarget function_return_;
450
451 // True if the function return is shadowed (ie, jumping to the target
452 // function_return_ does not jump to the true function return, but rather
453 // to some unlinking code).
454 bool function_return_is_shadowed_;
455
ager@chromium.org9085a012009-05-11 19:22:57 +0000456 static InlineRuntimeLUT kInlineRuntimeLUT[];
457
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000458 friend class VirtualFrame;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000459 friend class JumpTarget;
ager@chromium.org7c537e22008-10-16 08:43:32 +0000460 friend class Reference;
ager@chromium.org3811b432009-10-28 14:53:37 +0000461 friend class FastCodeGenerator;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000462 friend class FullCodeGenerator;
463 friend class FullCodeGenSyntaxChecker;
ager@chromium.org7c537e22008-10-16 08:43:32 +0000464
465 DISALLOW_COPY_AND_ASSIGN(CodeGenerator);
466};
467
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000468
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +0000469class GenericBinaryOpStub : public CodeStub {
470 public:
471 GenericBinaryOpStub(Token::Value op,
472 OverwriteMode mode,
473 int constant_rhs = CodeGenerator::kUnknownIntValue)
474 : op_(op),
475 mode_(mode),
476 constant_rhs_(constant_rhs),
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000477 specialized_on_rhs_(RhsIsOneWeWantToOptimizeFor(op, constant_rhs)),
478 name_(NULL) { }
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +0000479
480 private:
481 Token::Value op_;
482 OverwriteMode mode_;
483 int constant_rhs_;
484 bool specialized_on_rhs_;
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000485 char* name_;
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +0000486
487 static const int kMaxKnownRhs = 0x40000000;
488
489 // Minor key encoding in 16 bits.
490 class ModeBits: public BitField<OverwriteMode, 0, 2> {};
491 class OpBits: public BitField<Token::Value, 2, 6> {};
492 class KnownIntBits: public BitField<int, 8, 8> {};
493
494 Major MajorKey() { return GenericBinaryOp; }
495 int MinorKey() {
496 // Encode the parameters in a unique 16 bit value.
497 return OpBits::encode(op_)
498 | ModeBits::encode(mode_)
499 | KnownIntBits::encode(MinorKeyForKnownInt());
500 }
501
502 void Generate(MacroAssembler* masm);
503 void HandleNonSmiBitwiseOp(MacroAssembler* masm);
504
505 static bool RhsIsOneWeWantToOptimizeFor(Token::Value op, int constant_rhs) {
506 if (constant_rhs == CodeGenerator::kUnknownIntValue) return false;
507 if (op == Token::DIV) return constant_rhs >= 2 && constant_rhs <= 3;
508 if (op == Token::MOD) {
509 if (constant_rhs <= 1) return false;
510 if (constant_rhs <= 10) return true;
511 if (constant_rhs <= kMaxKnownRhs && IsPowerOf2(constant_rhs)) return true;
512 return false;
513 }
514 return false;
515 }
516
517 int MinorKeyForKnownInt() {
518 if (!specialized_on_rhs_) return 0;
519 if (constant_rhs_ <= 10) return constant_rhs_ + 1;
520 ASSERT(IsPowerOf2(constant_rhs_));
521 int key = 12;
522 int d = constant_rhs_;
523 while ((d & 1) == 0) {
524 key++;
525 d >>= 1;
526 }
527 return key;
528 }
529
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000530 const char* GetName();
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +0000531
532#ifdef DEBUG
533 void Print() {
534 if (!specialized_on_rhs_) {
535 PrintF("GenericBinaryOpStub (%s)\n", Token::String(op_));
536 } else {
537 PrintF("GenericBinaryOpStub (%s by %d)\n",
538 Token::String(op_),
539 constant_rhs_);
540 }
541 }
542#endif
543};
544
545
ager@chromium.org5c838252010-02-19 08:53:10 +0000546class StringStubBase: public CodeStub {
547 public:
548 // Generate code for copying characters using a simple loop. This should only
549 // be used in places where the number of characters is small and the
550 // additional setup and checking in GenerateCopyCharactersLong adds too much
551 // overhead. Copying of overlapping regions is not supported.
552 // Dest register ends at the position after the last character written.
553 void GenerateCopyCharacters(MacroAssembler* masm,
554 Register dest,
555 Register src,
556 Register count,
557 Register scratch,
558 bool ascii);
559
560 // Generate code for copying a large number of characters. This function
561 // is allowed to spend extra time setting up conditions to make copying
562 // faster. Copying of overlapping regions is not supported.
563 // Dest register ends at the position after the last character written.
564 void GenerateCopyCharactersLong(MacroAssembler* masm,
565 Register dest,
566 Register src,
567 Register count,
568 Register scratch1,
569 Register scratch2,
570 Register scratch3,
571 Register scratch4,
572 Register scratch5,
573 int flags);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000574
575
576 // Probe the symbol table for a two character string. If the string is
577 // not found by probing a jump to the label not_found is performed. This jump
578 // does not guarantee that the string is not in the symbol table. If the
579 // string is found the code falls through with the string in register r0.
580 // Contents of both c1 and c2 registers are modified. At the exit c1 is
581 // guaranteed to contain halfword with low and high bytes equal to
582 // initial contents of c1 and c2 respectively.
583 void GenerateTwoCharacterSymbolTableProbe(MacroAssembler* masm,
584 Register c1,
585 Register c2,
586 Register scratch1,
587 Register scratch2,
588 Register scratch3,
589 Register scratch4,
590 Register scratch5,
591 Label* not_found);
592
593 // Generate string hash.
594 void GenerateHashInit(MacroAssembler* masm,
595 Register hash,
596 Register character);
597
598 void GenerateHashAddCharacter(MacroAssembler* masm,
599 Register hash,
600 Register character);
601
602 void GenerateHashGetHash(MacroAssembler* masm,
603 Register hash);
ager@chromium.org5c838252010-02-19 08:53:10 +0000604};
605
606
607// Flag that indicates how to generate code for the stub StringAddStub.
608enum StringAddFlags {
609 NO_STRING_ADD_FLAGS = 0,
610 NO_STRING_CHECK_IN_STUB = 1 << 0 // Omit string check in stub.
611};
612
613
614class StringAddStub: public StringStubBase {
615 public:
616 explicit StringAddStub(StringAddFlags flags) {
617 string_check_ = ((flags & NO_STRING_CHECK_IN_STUB) == 0);
618 }
619
620 private:
621 Major MajorKey() { return StringAdd; }
622 int MinorKey() { return string_check_ ? 0 : 1; }
623
624 void Generate(MacroAssembler* masm);
625
626 // Should the stub check whether arguments are strings?
627 bool string_check_;
628};
629
630
631class SubStringStub: public StringStubBase {
632 public:
633 SubStringStub() {}
634
635 private:
636 Major MajorKey() { return SubString; }
637 int MinorKey() { return 0; }
638
639 void Generate(MacroAssembler* masm);
640};
641
642
643
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000644class StringCompareStub: public CodeStub {
645 public:
646 StringCompareStub() { }
647
648 // Compare two flat ASCII strings and returns result in r0.
649 // Does not use the stack.
650 static void GenerateCompareFlatAsciiStrings(MacroAssembler* masm,
651 Register left,
652 Register right,
653 Register scratch1,
654 Register scratch2,
655 Register scratch3,
656 Register scratch4);
657
658 private:
659 Major MajorKey() { return StringCompare; }
660 int MinorKey() { return 0; }
661
662 void Generate(MacroAssembler* masm);
663};
664
665
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000666// This stub can convert a signed int32 to a heap number (double). It does
667// not work for int32s that are in Smi range! No GC occurs during this stub
668// so you don't have to set up the frame.
669class WriteInt32ToHeapNumberStub : public CodeStub {
670 public:
671 WriteInt32ToHeapNumberStub(Register the_int,
672 Register the_heap_number,
673 Register scratch)
674 : the_int_(the_int),
675 the_heap_number_(the_heap_number),
676 scratch_(scratch) { }
677
678 private:
679 Register the_int_;
680 Register the_heap_number_;
681 Register scratch_;
682
683 // Minor key encoding in 16 bits.
684 class IntRegisterBits: public BitField<int, 0, 4> {};
685 class HeapNumberRegisterBits: public BitField<int, 4, 4> {};
686 class ScratchRegisterBits: public BitField<int, 8, 4> {};
687
688 Major MajorKey() { return WriteInt32ToHeapNumber; }
689 int MinorKey() {
690 // Encode the parameters in a unique 16 bit value.
691 return IntRegisterBits::encode(the_int_.code())
692 | HeapNumberRegisterBits::encode(the_heap_number_.code())
693 | ScratchRegisterBits::encode(scratch_.code());
694 }
695
696 void Generate(MacroAssembler* masm);
697
698 const char* GetName() { return "WriteInt32ToHeapNumberStub"; }
699
700#ifdef DEBUG
701 void Print() { PrintF("WriteInt32ToHeapNumberStub\n"); }
702#endif
703};
704
705
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000706class NumberToStringStub: public CodeStub {
707 public:
708 NumberToStringStub() { }
709
710 // Generate code to do a lookup in the number string cache. If the number in
711 // the register object is found in the cache the generated code falls through
712 // with the result in the result register. The object and the result register
713 // can be the same. If the number is not found in the cache the code jumps to
714 // the label not_found with only the content of register object unchanged.
715 static void GenerateLookupNumberStringCache(MacroAssembler* masm,
716 Register object,
717 Register result,
718 Register scratch1,
719 Register scratch2,
720 bool object_is_smi,
721 Label* not_found);
722
723 private:
724 Major MajorKey() { return NumberToString; }
725 int MinorKey() { return 0; }
726
727 void Generate(MacroAssembler* masm);
728
729 const char* GetName() { return "NumberToStringStub"; }
730
731#ifdef DEBUG
732 void Print() {
733 PrintF("NumberToStringStub\n");
734 }
735#endif
736};
737
738
ager@chromium.org7c537e22008-10-16 08:43:32 +0000739} } // namespace v8::internal
740
ager@chromium.org5ec48922009-05-05 07:25:34 +0000741#endif // V8_ARM_CODEGEN_ARM_H_