blob: 10026359e4b545d26333a62cbbb025554c44d0e0 [file] [log] [blame]
Ben Murdochb0fe1622011-05-05 13:52:32 +01001// Copyright 2010 the V8 project authors. All rights reserved.
Steve Blocka7e24c12009-10-30 11:49:00 +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
28#ifndef V8_X64_MACRO_ASSEMBLER_X64_H_
29#define V8_X64_MACRO_ASSEMBLER_X64_H_
30
31#include "assembler.h"
32
33namespace v8 {
34namespace internal {
35
Kristian Monsen25f61362010-05-21 11:50:48 +010036// Flags used for the AllocateInNewSpace functions.
37enum AllocationFlags {
38 // No special flags.
39 NO_ALLOCATION_FLAGS = 0,
40 // Return the pointer to the allocated already tagged as a heap object.
41 TAG_OBJECT = 1 << 0,
42 // The content of the result register already contains the allocation top in
43 // new space.
44 RESULT_CONTAINS_TOP = 1 << 1
45};
46
Steve Blocka7e24c12009-10-30 11:49:00 +000047// Default scratch register used by MacroAssembler (and other code that needs
48// a spare register). The register isn't callee save, and not used by the
49// function calling convention.
Steve Block8defd9f2010-07-08 12:39:36 +010050static const Register kScratchRegister = { 10 }; // r10.
51static const Register kSmiConstantRegister = { 15 }; // r15 (callee save).
52static const Register kRootRegister = { 13 }; // r13 (callee save).
53// Value of smi in kSmiConstantRegister.
54static const int kSmiConstantRegisterValue = 1;
Steve Blocka7e24c12009-10-30 11:49:00 +000055
Leon Clarkee46be812010-01-19 14:06:41 +000056// Convenience for platform-independent signatures.
57typedef Operand MemOperand;
58
Steve Blocka7e24c12009-10-30 11:49:00 +000059// Forward declaration.
60class JumpTarget;
61
62struct SmiIndex {
63 SmiIndex(Register index_register, ScaleFactor scale)
64 : reg(index_register),
65 scale(scale) {}
66 Register reg;
67 ScaleFactor scale;
68};
69
70// MacroAssembler implements a collection of frequently used macros.
71class MacroAssembler: public Assembler {
72 public:
73 MacroAssembler(void* buffer, int size);
74
75 void LoadRoot(Register destination, Heap::RootListIndex index);
76 void CompareRoot(Register with, Heap::RootListIndex index);
Steve Block1e0659c2011-05-24 12:43:12 +010077 void CompareRoot(const Operand& with, Heap::RootListIndex index);
Steve Blocka7e24c12009-10-30 11:49:00 +000078 void PushRoot(Heap::RootListIndex index);
Kristian Monsen25f61362010-05-21 11:50:48 +010079 void StoreRoot(Register source, Heap::RootListIndex index);
Steve Blocka7e24c12009-10-30 11:49:00 +000080
81 // ---------------------------------------------------------------------------
82 // GC Support
83
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +010084 // For page containing |object| mark region covering |addr| dirty.
85 // RecordWriteHelper only works if the object is not in new
Steve Block6ded16b2010-05-10 14:33:55 +010086 // space.
87 void RecordWriteHelper(Register object,
88 Register addr,
89 Register scratch);
90
91 // Check if object is in new space. The condition cc can be equal or
92 // not_equal. If it is equal a jump will be done if the object is on new
93 // space. The register scratch can be object itself, but it will be clobbered.
Kristian Monsen0d5e1162010-09-30 15:31:59 +010094 template <typename LabelType>
Steve Block6ded16b2010-05-10 14:33:55 +010095 void InNewSpace(Register object,
96 Register scratch,
97 Condition cc,
Kristian Monsen0d5e1162010-09-30 15:31:59 +010098 LabelType* branch);
Steve Block6ded16b2010-05-10 14:33:55 +010099
Steve Block8defd9f2010-07-08 12:39:36 +0100100 // For page containing |object| mark region covering [object+offset]
101 // dirty. |object| is the object being stored into, |value| is the
102 // object being stored. If |offset| is zero, then the |scratch|
103 // register contains the array index into the elements array
Ben Murdochf87a2032010-10-22 12:50:53 +0100104 // represented as an untagged 32-bit integer. All registers are
105 // clobbered by the operation. RecordWrite filters out smis so it
106 // does not update the write barrier if the value is a smi.
Steve Blocka7e24c12009-10-30 11:49:00 +0000107 void RecordWrite(Register object,
108 int offset,
109 Register value,
110 Register scratch);
111
Steve Block8defd9f2010-07-08 12:39:36 +0100112 // For page containing |object| mark region covering [address]
113 // dirty. |object| is the object being stored into, |value| is the
114 // object being stored. All registers are clobbered by the
115 // operation. RecordWrite filters out smis so it does not update
116 // the write barrier if the value is a smi.
117 void RecordWrite(Register object,
118 Register address,
119 Register value);
120
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100121 // For page containing |object| mark region covering [object+offset] dirty.
Steve Block3ce2e202009-11-05 08:53:23 +0000122 // The value is known to not be a smi.
123 // object is the object being stored into, value is the object being stored.
124 // If offset is zero, then the scratch register contains the array index into
Ben Murdochf87a2032010-10-22 12:50:53 +0100125 // the elements array represented as an untagged 32-bit integer.
Steve Block3ce2e202009-11-05 08:53:23 +0000126 // All registers are clobbered by the operation.
127 void RecordWriteNonSmi(Register object,
128 int offset,
129 Register value,
130 Register scratch);
131
Steve Blocka7e24c12009-10-30 11:49:00 +0000132#ifdef ENABLE_DEBUGGER_SUPPORT
133 // ---------------------------------------------------------------------------
134 // Debugger Support
135
Andrei Popescu402d9372010-02-26 13:31:12 +0000136 void DebugBreak();
Steve Blocka7e24c12009-10-30 11:49:00 +0000137#endif
138
139 // ---------------------------------------------------------------------------
140 // Activation frames
141
142 void EnterInternalFrame() { EnterFrame(StackFrame::INTERNAL); }
143 void LeaveInternalFrame() { LeaveFrame(StackFrame::INTERNAL); }
144
145 void EnterConstructFrame() { EnterFrame(StackFrame::CONSTRUCT); }
146 void LeaveConstructFrame() { LeaveFrame(StackFrame::CONSTRUCT); }
147
Steve Blockd0582a62009-12-15 09:54:21 +0000148 // Enter specific kind of exit frame; either in normal or
149 // debug mode. Expects the number of arguments in register rax and
Steve Blocka7e24c12009-10-30 11:49:00 +0000150 // sets up the number of arguments in register rdi and the pointer
151 // to the first argument in register rsi.
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800152 //
153 // Allocates arg_stack_space * kPointerSize memory (not GCed) on the stack
154 // accessible via StackSpaceOperand.
Steve Block1e0659c2011-05-24 12:43:12 +0100155 void EnterExitFrame(int arg_stack_space = 0, bool save_doubles = false);
Steve Blocka7e24c12009-10-30 11:49:00 +0000156
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800157 // Enter specific kind of exit frame. Allocates arg_stack_space * kPointerSize
158 // memory (not GCed) on the stack accessible via StackSpaceOperand.
159 void EnterApiExitFrame(int arg_stack_space);
Ben Murdochbb769b22010-08-11 14:56:33 +0100160
Steve Blocka7e24c12009-10-30 11:49:00 +0000161 // Leave the current exit frame. Expects/provides the return value in
162 // register rax:rdx (untouched) and the pointer to the first
163 // argument in register rsi.
Steve Block1e0659c2011-05-24 12:43:12 +0100164 void LeaveExitFrame(bool save_doubles = false);
Steve Blocka7e24c12009-10-30 11:49:00 +0000165
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800166 // Leave the current exit frame. Expects/provides the return value in
167 // register rax (untouched).
168 void LeaveApiExitFrame();
Steve Blocka7e24c12009-10-30 11:49:00 +0000169
Ben Murdochb0fe1622011-05-05 13:52:32 +0100170 // Push and pop the registers that can hold pointers.
Steve Block1e0659c2011-05-24 12:43:12 +0100171 void PushSafepointRegisters() { Pushad(); }
172 void PopSafepointRegisters() { Popad(); }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100173 static int SafepointRegisterStackIndex(int reg_code) {
Steve Block1e0659c2011-05-24 12:43:12 +0100174 return kSafepointPushRegisterIndices[reg_code];
Ben Murdochb0fe1622011-05-05 13:52:32 +0100175 }
176
Steve Block1e0659c2011-05-24 12:43:12 +0100177
Steve Blocka7e24c12009-10-30 11:49:00 +0000178 // ---------------------------------------------------------------------------
179 // JavaScript invokes
180
181 // Invoke the JavaScript function code by either calling or jumping.
182 void InvokeCode(Register code,
183 const ParameterCount& expected,
184 const ParameterCount& actual,
185 InvokeFlag flag);
186
187 void InvokeCode(Handle<Code> code,
188 const ParameterCount& expected,
189 const ParameterCount& actual,
190 RelocInfo::Mode rmode,
191 InvokeFlag flag);
192
193 // Invoke the JavaScript function in the given register. Changes the
194 // current context to the context in the function before invoking.
195 void InvokeFunction(Register function,
196 const ParameterCount& actual,
197 InvokeFlag flag);
198
Andrei Popescu402d9372010-02-26 13:31:12 +0000199 void InvokeFunction(JSFunction* function,
200 const ParameterCount& actual,
201 InvokeFlag flag);
202
Steve Blocka7e24c12009-10-30 11:49:00 +0000203 // Invoke specified builtin JavaScript function. Adds an entry to
204 // the unresolved list if the name does not resolve.
205 void InvokeBuiltin(Builtins::JavaScript id, InvokeFlag flag);
206
Steve Block791712a2010-08-27 10:21:07 +0100207 // Store the function for the given builtin in the target register.
208 void GetBuiltinFunction(Register target, Builtins::JavaScript id);
209
Steve Blocka7e24c12009-10-30 11:49:00 +0000210 // Store the code object for the given builtin in the target register.
211 void GetBuiltinEntry(Register target, Builtins::JavaScript id);
212
213
214 // ---------------------------------------------------------------------------
215 // Smi tagging, untagging and operations on tagged smis.
216
Steve Block8defd9f2010-07-08 12:39:36 +0100217 void InitializeSmiConstantRegister() {
218 movq(kSmiConstantRegister,
219 reinterpret_cast<uint64_t>(Smi::FromInt(kSmiConstantRegisterValue)),
220 RelocInfo::NONE);
221 }
222
Steve Blocka7e24c12009-10-30 11:49:00 +0000223 // Conversions between tagged smi values and non-tagged integer values.
224
225 // Tag an integer value. The result must be known to be a valid smi value.
Leon Clarke4515c472010-02-03 11:58:03 +0000226 // Only uses the low 32 bits of the src register. Sets the N and Z flags
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100227 // based on the value of the resulting smi.
Steve Blocka7e24c12009-10-30 11:49:00 +0000228 void Integer32ToSmi(Register dst, Register src);
229
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100230 // Stores an integer32 value into a memory field that already holds a smi.
231 void Integer32ToSmiField(const Operand& dst, Register src);
232
Steve Blocka7e24c12009-10-30 11:49:00 +0000233 // Adds constant to src and tags the result as a smi.
234 // Result must be a valid smi.
Steve Block3ce2e202009-11-05 08:53:23 +0000235 void Integer64PlusConstantToSmi(Register dst, Register src, int constant);
Steve Blocka7e24c12009-10-30 11:49:00 +0000236
237 // Convert smi to 32-bit integer. I.e., not sign extended into
238 // high 32 bits of destination.
239 void SmiToInteger32(Register dst, Register src);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100240 void SmiToInteger32(Register dst, const Operand& src);
Steve Blocka7e24c12009-10-30 11:49:00 +0000241
242 // Convert smi to 64-bit integer (sign extended if necessary).
243 void SmiToInteger64(Register dst, Register src);
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100244 void SmiToInteger64(Register dst, const Operand& src);
Steve Blocka7e24c12009-10-30 11:49:00 +0000245
246 // Multiply a positive smi's integer value by a power of two.
247 // Provides result as 64-bit integer value.
248 void PositiveSmiTimesPowerOfTwoToInteger64(Register dst,
249 Register src,
250 int power);
251
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100252 // Divide a positive smi's integer value by a power of two.
253 // Provides result as 32-bit integer value.
254 void PositiveSmiDivPowerOfTwoToInteger32(Register dst,
255 Register src,
256 int power);
257
258
Steve Block3ce2e202009-11-05 08:53:23 +0000259 // Simple comparison of smis.
260 void SmiCompare(Register dst, Register src);
261 void SmiCompare(Register dst, Smi* src);
Steve Block6ded16b2010-05-10 14:33:55 +0100262 void SmiCompare(Register dst, const Operand& src);
Steve Block3ce2e202009-11-05 08:53:23 +0000263 void SmiCompare(const Operand& dst, Register src);
264 void SmiCompare(const Operand& dst, Smi* src);
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100265 // Compare the int32 in src register to the value of the smi stored at dst.
266 void SmiCompareInteger32(const Operand& dst, Register src);
Steve Block3ce2e202009-11-05 08:53:23 +0000267 // Sets sign and zero flags depending on value of smi in register.
268 void SmiTest(Register src);
269
Steve Blocka7e24c12009-10-30 11:49:00 +0000270 // Functions performing a check on a known or potential smi. Returns
271 // a condition that is satisfied if the check is successful.
272
273 // Is the value a tagged smi.
274 Condition CheckSmi(Register src);
Steve Block1e0659c2011-05-24 12:43:12 +0100275 Condition CheckSmi(const Operand& src);
Steve Blocka7e24c12009-10-30 11:49:00 +0000276
Ben Murdochf87a2032010-10-22 12:50:53 +0100277 // Is the value a non-negative tagged smi.
278 Condition CheckNonNegativeSmi(Register src);
Steve Blocka7e24c12009-10-30 11:49:00 +0000279
Leon Clarkee46be812010-01-19 14:06:41 +0000280 // Are both values tagged smis.
Steve Blocka7e24c12009-10-30 11:49:00 +0000281 Condition CheckBothSmi(Register first, Register second);
282
Ben Murdochf87a2032010-10-22 12:50:53 +0100283 // Are both values non-negative tagged smis.
284 Condition CheckBothNonNegativeSmi(Register first, Register second);
Leon Clarked91b9f72010-01-27 17:25:45 +0000285
Leon Clarkee46be812010-01-19 14:06:41 +0000286 // Are either value a tagged smi.
Ben Murdochbb769b22010-08-11 14:56:33 +0100287 Condition CheckEitherSmi(Register first,
288 Register second,
289 Register scratch = kScratchRegister);
Leon Clarkee46be812010-01-19 14:06:41 +0000290
Steve Blocka7e24c12009-10-30 11:49:00 +0000291 // Is the value the minimum smi value (since we are using
292 // two's complement numbers, negating the value is known to yield
293 // a non-smi value).
294 Condition CheckIsMinSmi(Register src);
295
Steve Blocka7e24c12009-10-30 11:49:00 +0000296 // Checks whether an 32-bit integer value is a valid for conversion
297 // to a smi.
298 Condition CheckInteger32ValidSmiValue(Register src);
299
Steve Block3ce2e202009-11-05 08:53:23 +0000300 // Checks whether an 32-bit unsigned integer value is a valid for
301 // conversion to a smi.
302 Condition CheckUInteger32ValidSmiValue(Register src);
303
Steve Block1e0659c2011-05-24 12:43:12 +0100304 // Check whether src is a Smi, and set dst to zero if it is a smi,
305 // and to one if it isn't.
306 void CheckSmiToIndicator(Register dst, Register src);
307 void CheckSmiToIndicator(Register dst, const Operand& src);
308
Steve Blocka7e24c12009-10-30 11:49:00 +0000309 // Test-and-jump functions. Typically combines a check function
310 // above with a conditional jump.
311
312 // Jump if the value cannot be represented by a smi.
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100313 template <typename LabelType>
314 void JumpIfNotValidSmiValue(Register src, LabelType* on_invalid);
Steve Blocka7e24c12009-10-30 11:49:00 +0000315
Steve Block3ce2e202009-11-05 08:53:23 +0000316 // Jump if the unsigned integer value cannot be represented by a smi.
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100317 template <typename LabelType>
318 void JumpIfUIntNotValidSmiValue(Register src, LabelType* on_invalid);
Steve Block3ce2e202009-11-05 08:53:23 +0000319
Steve Blocka7e24c12009-10-30 11:49:00 +0000320 // Jump to label if the value is a tagged smi.
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100321 template <typename LabelType>
322 void JumpIfSmi(Register src, LabelType* on_smi);
Steve Blocka7e24c12009-10-30 11:49:00 +0000323
324 // Jump to label if the value is not a tagged smi.
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100325 template <typename LabelType>
326 void JumpIfNotSmi(Register src, LabelType* on_not_smi);
Steve Blocka7e24c12009-10-30 11:49:00 +0000327
Ben Murdochf87a2032010-10-22 12:50:53 +0100328 // Jump to label if the value is not a non-negative tagged smi.
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100329 template <typename LabelType>
Ben Murdochf87a2032010-10-22 12:50:53 +0100330 void JumpUnlessNonNegativeSmi(Register src, LabelType* on_not_smi);
Steve Blocka7e24c12009-10-30 11:49:00 +0000331
Steve Block3ce2e202009-11-05 08:53:23 +0000332 // Jump to label if the value, which must be a tagged smi, has value equal
Steve Blocka7e24c12009-10-30 11:49:00 +0000333 // to the constant.
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100334 template <typename LabelType>
335 void JumpIfSmiEqualsConstant(Register src,
336 Smi* constant,
337 LabelType* on_equals);
Steve Blocka7e24c12009-10-30 11:49:00 +0000338
339 // Jump if either or both register are not smi values.
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100340 template <typename LabelType>
341 void JumpIfNotBothSmi(Register src1,
342 Register src2,
343 LabelType* on_not_both_smi);
Steve Blocka7e24c12009-10-30 11:49:00 +0000344
Ben Murdochf87a2032010-10-22 12:50:53 +0100345 // Jump if either or both register are not non-negative smi values.
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100346 template <typename LabelType>
Ben Murdochf87a2032010-10-22 12:50:53 +0100347 void JumpUnlessBothNonNegativeSmi(Register src1, Register src2,
348 LabelType* on_not_both_smi);
Leon Clarked91b9f72010-01-27 17:25:45 +0000349
Steve Blocka7e24c12009-10-30 11:49:00 +0000350 // Operations on tagged smi values.
351
352 // Smis represent a subset of integers. The subset is always equivalent to
353 // a two's complement interpretation of a fixed number of bits.
354
355 // Optimistically adds an integer constant to a supposed smi.
356 // If the src is not a smi, or the result is not a smi, jump to
357 // the label.
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100358 template <typename LabelType>
Steve Blocka7e24c12009-10-30 11:49:00 +0000359 void SmiTryAddConstant(Register dst,
360 Register src,
Steve Block3ce2e202009-11-05 08:53:23 +0000361 Smi* constant,
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100362 LabelType* on_not_smi_result);
Steve Blocka7e24c12009-10-30 11:49:00 +0000363
Steve Block3ce2e202009-11-05 08:53:23 +0000364 // Add an integer constant to a tagged smi, giving a tagged smi as result.
365 // No overflow testing on the result is done.
366 void SmiAddConstant(Register dst, Register src, Smi* constant);
367
Leon Clarkef7060e22010-06-03 12:02:55 +0100368 // Add an integer constant to a tagged smi, giving a tagged smi as result.
369 // No overflow testing on the result is done.
370 void SmiAddConstant(const Operand& dst, Smi* constant);
371
Steve Blocka7e24c12009-10-30 11:49:00 +0000372 // Add an integer constant to a tagged smi, giving a tagged smi as result,
373 // or jumping to a label if the result cannot be represented by a smi.
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100374 template <typename LabelType>
Steve Blocka7e24c12009-10-30 11:49:00 +0000375 void SmiAddConstant(Register dst,
376 Register src,
Steve Block3ce2e202009-11-05 08:53:23 +0000377 Smi* constant,
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100378 LabelType* on_not_smi_result);
Steve Blocka7e24c12009-10-30 11:49:00 +0000379
380 // Subtract an integer constant from a tagged smi, giving a tagged smi as
Steve Block6ded16b2010-05-10 14:33:55 +0100381 // result. No testing on the result is done. Sets the N and Z flags
382 // based on the value of the resulting integer.
Steve Block3ce2e202009-11-05 08:53:23 +0000383 void SmiSubConstant(Register dst, Register src, Smi* constant);
384
385 // Subtract an integer constant from a tagged smi, giving a tagged smi as
Steve Blocka7e24c12009-10-30 11:49:00 +0000386 // result, or jumping to a label if the result cannot be represented by a smi.
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100387 template <typename LabelType>
Steve Blocka7e24c12009-10-30 11:49:00 +0000388 void SmiSubConstant(Register dst,
389 Register src,
Steve Block3ce2e202009-11-05 08:53:23 +0000390 Smi* constant,
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100391 LabelType* on_not_smi_result);
Steve Blocka7e24c12009-10-30 11:49:00 +0000392
393 // Negating a smi can give a negative zero or too large positive value.
Steve Block3ce2e202009-11-05 08:53:23 +0000394 // NOTICE: This operation jumps on success, not failure!
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100395 template <typename LabelType>
Steve Blocka7e24c12009-10-30 11:49:00 +0000396 void SmiNeg(Register dst,
397 Register src,
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100398 LabelType* on_smi_result);
Steve Blocka7e24c12009-10-30 11:49:00 +0000399
400 // Adds smi values and return the result as a smi.
401 // If dst is src1, then src1 will be destroyed, even if
402 // the operation is unsuccessful.
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100403 template <typename LabelType>
Steve Blocka7e24c12009-10-30 11:49:00 +0000404 void SmiAdd(Register dst,
405 Register src1,
406 Register src2,
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100407 LabelType* on_not_smi_result);
408
409 void SmiAdd(Register dst,
410 Register src1,
411 Register src2);
Steve Blocka7e24c12009-10-30 11:49:00 +0000412
413 // Subtracts smi values and return the result as a smi.
414 // If dst is src1, then src1 will be destroyed, even if
415 // the operation is unsuccessful.
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100416 template <typename LabelType>
Steve Blocka7e24c12009-10-30 11:49:00 +0000417 void SmiSub(Register dst,
418 Register src1,
419 Register src2,
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100420 LabelType* on_not_smi_result);
Steve Blocka7e24c12009-10-30 11:49:00 +0000421
Steve Block6ded16b2010-05-10 14:33:55 +0100422 void SmiSub(Register dst,
423 Register src1,
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100424 Register src2);
425
426 template <typename LabelType>
427 void SmiSub(Register dst,
428 Register src1,
Leon Clarkef7060e22010-06-03 12:02:55 +0100429 const Operand& src2,
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100430 LabelType* on_not_smi_result);
431
432 void SmiSub(Register dst,
433 Register src1,
434 const Operand& src2);
Steve Block6ded16b2010-05-10 14:33:55 +0100435
Steve Blocka7e24c12009-10-30 11:49:00 +0000436 // Multiplies smi values and return the result as a smi,
437 // if possible.
438 // If dst is src1, then src1 will be destroyed, even if
439 // the operation is unsuccessful.
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100440 template <typename LabelType>
Steve Blocka7e24c12009-10-30 11:49:00 +0000441 void SmiMul(Register dst,
442 Register src1,
443 Register src2,
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100444 LabelType* on_not_smi_result);
Steve Blocka7e24c12009-10-30 11:49:00 +0000445
446 // Divides one smi by another and returns the quotient.
447 // Clobbers rax and rdx registers.
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100448 template <typename LabelType>
Steve Blocka7e24c12009-10-30 11:49:00 +0000449 void SmiDiv(Register dst,
450 Register src1,
451 Register src2,
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100452 LabelType* on_not_smi_result);
Steve Blocka7e24c12009-10-30 11:49:00 +0000453
454 // Divides one smi by another and returns the remainder.
455 // Clobbers rax and rdx registers.
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100456 template <typename LabelType>
Steve Blocka7e24c12009-10-30 11:49:00 +0000457 void SmiMod(Register dst,
458 Register src1,
459 Register src2,
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100460 LabelType* on_not_smi_result);
Steve Blocka7e24c12009-10-30 11:49:00 +0000461
462 // Bitwise operations.
463 void SmiNot(Register dst, Register src);
464 void SmiAnd(Register dst, Register src1, Register src2);
465 void SmiOr(Register dst, Register src1, Register src2);
466 void SmiXor(Register dst, Register src1, Register src2);
Steve Block3ce2e202009-11-05 08:53:23 +0000467 void SmiAndConstant(Register dst, Register src1, Smi* constant);
468 void SmiOrConstant(Register dst, Register src1, Smi* constant);
469 void SmiXorConstant(Register dst, Register src1, Smi* constant);
Steve Blocka7e24c12009-10-30 11:49:00 +0000470
471 void SmiShiftLeftConstant(Register dst,
472 Register src,
Kristian Monsen25f61362010-05-21 11:50:48 +0100473 int shift_value);
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100474 template <typename LabelType>
Steve Blocka7e24c12009-10-30 11:49:00 +0000475 void SmiShiftLogicalRightConstant(Register dst,
476 Register src,
477 int shift_value,
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100478 LabelType* on_not_smi_result);
Steve Blocka7e24c12009-10-30 11:49:00 +0000479 void SmiShiftArithmeticRightConstant(Register dst,
480 Register src,
481 int shift_value);
482
483 // Shifts a smi value to the left, and returns the result if that is a smi.
484 // Uses and clobbers rcx, so dst may not be rcx.
485 void SmiShiftLeft(Register dst,
486 Register src1,
Kristian Monsen25f61362010-05-21 11:50:48 +0100487 Register src2);
Steve Blocka7e24c12009-10-30 11:49:00 +0000488 // Shifts a smi value to the right, shifting in zero bits at the top, and
489 // returns the unsigned intepretation of the result if that is a smi.
490 // Uses and clobbers rcx, so dst may not be rcx.
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100491 template <typename LabelType>
Steve Blocka7e24c12009-10-30 11:49:00 +0000492 void SmiShiftLogicalRight(Register dst,
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100493 Register src1,
494 Register src2,
495 LabelType* on_not_smi_result);
Steve Blocka7e24c12009-10-30 11:49:00 +0000496 // Shifts a smi value to the right, sign extending the top, and
497 // returns the signed intepretation of the result. That will always
498 // be a valid smi value, since it's numerically smaller than the
499 // original.
500 // Uses and clobbers rcx, so dst may not be rcx.
501 void SmiShiftArithmeticRight(Register dst,
502 Register src1,
503 Register src2);
504
505 // Specialized operations
506
507 // Select the non-smi register of two registers where exactly one is a
508 // smi. If neither are smis, jump to the failure label.
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100509 template <typename LabelType>
Steve Blocka7e24c12009-10-30 11:49:00 +0000510 void SelectNonSmi(Register dst,
511 Register src1,
512 Register src2,
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100513 LabelType* on_not_smis);
Steve Blocka7e24c12009-10-30 11:49:00 +0000514
515 // Converts, if necessary, a smi to a combination of number and
516 // multiplier to be used as a scaled index.
517 // The src register contains a *positive* smi value. The shift is the
518 // power of two to multiply the index value by (e.g.
519 // to index by smi-value * kPointerSize, pass the smi and kPointerSizeLog2).
520 // The returned index register may be either src or dst, depending
521 // on what is most efficient. If src and dst are different registers,
522 // src is always unchanged.
523 SmiIndex SmiToIndex(Register dst, Register src, int shift);
524
525 // Converts a positive smi to a negative index.
526 SmiIndex SmiToNegativeIndex(Register dst, Register src, int shift);
527
Steve Block3ce2e202009-11-05 08:53:23 +0000528 // Basic Smi operations.
529 void Move(Register dst, Smi* source) {
Steve Block8defd9f2010-07-08 12:39:36 +0100530 LoadSmiConstant(dst, source);
Steve Block3ce2e202009-11-05 08:53:23 +0000531 }
532
533 void Move(const Operand& dst, Smi* source) {
Steve Block8defd9f2010-07-08 12:39:36 +0100534 Register constant = GetSmiConstant(source);
535 movq(dst, constant);
Steve Block3ce2e202009-11-05 08:53:23 +0000536 }
537
538 void Push(Smi* smi);
539 void Test(const Operand& dst, Smi* source);
540
Steve Blocka7e24c12009-10-30 11:49:00 +0000541 // ---------------------------------------------------------------------------
Leon Clarkee46be812010-01-19 14:06:41 +0000542 // String macros.
Steve Block1e0659c2011-05-24 12:43:12 +0100543
544 // If object is a string, its map is loaded into object_map.
545 template <typename LabelType>
546 void JumpIfNotString(Register object,
547 Register object_map,
548 LabelType* not_string);
549
550
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100551 template <typename LabelType>
Leon Clarkee46be812010-01-19 14:06:41 +0000552 void JumpIfNotBothSequentialAsciiStrings(Register first_object,
553 Register second_object,
554 Register scratch1,
555 Register scratch2,
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100556 LabelType* on_not_both_flat_ascii);
Leon Clarkee46be812010-01-19 14:06:41 +0000557
Steve Block6ded16b2010-05-10 14:33:55 +0100558 // Check whether the instance type represents a flat ascii string. Jump to the
559 // label if not. If the instance type can be scratched specify same register
560 // for both instance type and scratch.
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100561 template <typename LabelType>
562 void JumpIfInstanceTypeIsNotSequentialAscii(
563 Register instance_type,
564 Register scratch,
565 LabelType *on_not_flat_ascii_string);
Steve Block6ded16b2010-05-10 14:33:55 +0100566
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100567 template <typename LabelType>
Steve Block6ded16b2010-05-10 14:33:55 +0100568 void JumpIfBothInstanceTypesAreNotSequentialAscii(
569 Register first_object_instance_type,
570 Register second_object_instance_type,
571 Register scratch1,
572 Register scratch2,
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100573 LabelType* on_fail);
Steve Block6ded16b2010-05-10 14:33:55 +0100574
Leon Clarkee46be812010-01-19 14:06:41 +0000575 // ---------------------------------------------------------------------------
576 // Macro instructions.
Steve Blocka7e24c12009-10-30 11:49:00 +0000577
Steve Block3ce2e202009-11-05 08:53:23 +0000578 // Load a register with a long value as efficiently as possible.
Steve Blocka7e24c12009-10-30 11:49:00 +0000579 void Set(Register dst, int64_t x);
580 void Set(const Operand& dst, int64_t x);
581
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100582 // Move if the registers are not identical.
583 void Move(Register target, Register source);
584
Steve Blocka7e24c12009-10-30 11:49:00 +0000585 // Handle support
Steve Blocka7e24c12009-10-30 11:49:00 +0000586 void Move(Register dst, Handle<Object> source);
587 void Move(const Operand& dst, Handle<Object> source);
588 void Cmp(Register dst, Handle<Object> source);
589 void Cmp(const Operand& dst, Handle<Object> source);
590 void Push(Handle<Object> source);
Steve Blocka7e24c12009-10-30 11:49:00 +0000591
Leon Clarkee46be812010-01-19 14:06:41 +0000592 // Emit code to discard a non-negative number of pointer-sized elements
593 // from the stack, clobbering only the rsp register.
594 void Drop(int stack_elements);
595
596 void Call(Label* target) { call(target); }
597
Steve Blocka7e24c12009-10-30 11:49:00 +0000598 // Control Flow
599 void Jump(Address destination, RelocInfo::Mode rmode);
600 void Jump(ExternalReference ext);
601 void Jump(Handle<Code> code_object, RelocInfo::Mode rmode);
602
603 void Call(Address destination, RelocInfo::Mode rmode);
604 void Call(ExternalReference ext);
605 void Call(Handle<Code> code_object, RelocInfo::Mode rmode);
606
Steve Block1e0659c2011-05-24 12:43:12 +0100607 // Emit call to the code we are currently generating.
608 void CallSelf() {
609 Handle<Code> self(reinterpret_cast<Code**>(CodeObject().location()));
610 Call(self, RelocInfo::CODE_TARGET);
611 }
612
613 // Non-x64 instructions.
614 // Push/pop all general purpose registers.
615 // Does not push rsp/rbp nor any of the assembler's special purpose registers
616 // (kScratchRegister, kSmiConstantRegister, kRootRegister).
617 void Pushad();
618 void Popad();
619 // Sets the stack as after performing Popad, without actually loading the
620 // registers.
621 void Dropad();
622
Steve Blocka7e24c12009-10-30 11:49:00 +0000623 // Compare object type for heap object.
624 // Always use unsigned comparisons: above and below, not less and greater.
625 // Incoming register is heap_object and outgoing register is map.
626 // They may be the same register, and may be kScratchRegister.
627 void CmpObjectType(Register heap_object, InstanceType type, Register map);
628
629 // Compare instance type for map.
630 // Always use unsigned comparisons: above and below, not less and greater.
631 void CmpInstanceType(Register map, InstanceType type);
632
Andrei Popescu31002712010-02-23 13:46:05 +0000633 // Check if the map of an object is equal to a specified map and
634 // branch to label if not. Skip the smi check if not required
635 // (object is known to be a heap object)
636 void CheckMap(Register obj,
637 Handle<Map> map,
638 Label* fail,
639 bool is_heap_object);
640
Leon Clarked91b9f72010-01-27 17:25:45 +0000641 // Check if the object in register heap_object is a string. Afterwards the
642 // register map contains the object map and the register instance_type
643 // contains the instance_type. The registers map and instance_type can be the
644 // same in which case it contains the instance type afterwards. Either of the
645 // registers map and instance_type can be the same as heap_object.
646 Condition IsObjectStringType(Register heap_object,
647 Register map,
648 Register instance_type);
649
Steve Block8defd9f2010-07-08 12:39:36 +0100650 // FCmp compares and pops the two values on top of the FPU stack.
651 // The flag results are similar to integer cmp, but requires unsigned
Steve Blocka7e24c12009-10-30 11:49:00 +0000652 // jcc instructions (je, ja, jae, jb, jbe, je, and jz).
653 void FCmp();
654
Andrei Popescu402d9372010-02-26 13:31:12 +0000655 // Abort execution if argument is not a number. Used in debug code.
Leon Clarkef7060e22010-06-03 12:02:55 +0100656 void AbortIfNotNumber(Register object);
Andrei Popescu402d9372010-02-26 13:31:12 +0000657
Iain Merrick75681382010-08-19 15:07:18 +0100658 // Abort execution if argument is a smi. Used in debug code.
659 void AbortIfSmi(Register object);
660
Steve Block6ded16b2010-05-10 14:33:55 +0100661 // Abort execution if argument is not a smi. Used in debug code.
Leon Clarkef7060e22010-06-03 12:02:55 +0100662 void AbortIfNotSmi(Register object);
Steve Block6ded16b2010-05-10 14:33:55 +0100663
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100664 // Abort execution if argument is not the root value with the given index.
665 void AbortIfNotRootValue(Register src,
666 Heap::RootListIndex root_value_index,
667 const char* message);
668
Steve Blocka7e24c12009-10-30 11:49:00 +0000669 // ---------------------------------------------------------------------------
670 // Exception handling
671
672 // Push a new try handler and link into try handler chain. The return
673 // address must be pushed before calling this helper.
674 void PushTryHandler(CodeLocation try_location, HandlerType type);
675
Leon Clarkee46be812010-01-19 14:06:41 +0000676 // Unlink the stack handler on top of the stack from the try handler chain.
677 void PopTryHandler();
Steve Blocka7e24c12009-10-30 11:49:00 +0000678
679 // ---------------------------------------------------------------------------
680 // Inline caching support
681
Steve Blocka7e24c12009-10-30 11:49:00 +0000682 // Generate code for checking access rights - used for security checks
683 // on access to global objects across environments. The holder register
684 // is left untouched, but the scratch register and kScratchRegister,
685 // which must be different, are clobbered.
686 void CheckAccessGlobalProxy(Register holder_reg,
687 Register scratch,
688 Label* miss);
689
690
691 // ---------------------------------------------------------------------------
692 // Allocation support
693
694 // Allocate an object in new space. If the new space is exhausted control
695 // continues at the gc_required label. The allocated object is returned in
696 // result and end of the new object is returned in result_end. The register
697 // scratch can be passed as no_reg in which case an additional object
698 // reference will be added to the reloc info. The returned pointers in result
699 // and result_end have not yet been tagged as heap objects. If
700 // result_contains_top_on_entry is true the content of result is known to be
701 // the allocation top on entry (could be result_end from a previous call to
702 // AllocateInNewSpace). If result_contains_top_on_entry is true scratch
703 // should be no_reg as it is never used.
704 void AllocateInNewSpace(int object_size,
705 Register result,
706 Register result_end,
707 Register scratch,
708 Label* gc_required,
709 AllocationFlags flags);
710
711 void AllocateInNewSpace(int header_size,
712 ScaleFactor element_size,
713 Register element_count,
714 Register result,
715 Register result_end,
716 Register scratch,
717 Label* gc_required,
718 AllocationFlags flags);
719
720 void AllocateInNewSpace(Register object_size,
721 Register result,
722 Register result_end,
723 Register scratch,
724 Label* gc_required,
725 AllocationFlags flags);
726
727 // Undo allocation in new space. The object passed and objects allocated after
728 // it will no longer be allocated. Make sure that no pointers are left to the
729 // object(s) no longer allocated as they would be invalid when allocation is
730 // un-done.
731 void UndoAllocationInNewSpace(Register object);
732
Steve Block3ce2e202009-11-05 08:53:23 +0000733 // Allocate a heap number in new space with undefined value. Returns
734 // tagged pointer in result register, or jumps to gc_required if new
735 // space is full.
736 void AllocateHeapNumber(Register result,
737 Register scratch,
738 Label* gc_required);
739
Leon Clarkee46be812010-01-19 14:06:41 +0000740 // Allocate a sequential string. All the header fields of the string object
741 // are initialized.
742 void AllocateTwoByteString(Register result,
743 Register length,
744 Register scratch1,
745 Register scratch2,
746 Register scratch3,
747 Label* gc_required);
748 void AllocateAsciiString(Register result,
749 Register length,
750 Register scratch1,
751 Register scratch2,
752 Register scratch3,
753 Label* gc_required);
754
755 // Allocate a raw cons string object. Only the map field of the result is
756 // initialized.
757 void AllocateConsString(Register result,
758 Register scratch1,
759 Register scratch2,
760 Label* gc_required);
761 void AllocateAsciiConsString(Register result,
762 Register scratch1,
763 Register scratch2,
764 Label* gc_required);
765
Steve Blocka7e24c12009-10-30 11:49:00 +0000766 // ---------------------------------------------------------------------------
767 // Support functions.
768
769 // Check if result is zero and op is negative.
770 void NegativeZeroTest(Register result, Register op, Label* then_label);
771
772 // Check if result is zero and op is negative in code using jump targets.
773 void NegativeZeroTest(CodeGenerator* cgen,
774 Register result,
775 Register op,
776 JumpTarget* then_target);
777
778 // Check if result is zero and any of op1 and op2 are negative.
779 // Register scratch is destroyed, and it must be different from op2.
780 void NegativeZeroTest(Register result, Register op1, Register op2,
781 Register scratch, Label* then_label);
782
783 // Try to get function prototype of a function and puts the value in
784 // the result register. Checks that the function really is a
785 // function and jumps to the miss label if the fast checks fail. The
786 // function register will be untouched; the other register may be
787 // clobbered.
788 void TryGetFunctionPrototype(Register function,
789 Register result,
790 Label* miss);
791
792 // Generates code for reporting that an illegal operation has
793 // occurred.
794 void IllegalOperation(int num_arguments);
795
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100796 // Picks out an array index from the hash field.
797 // Register use:
798 // hash - holds the index's hash. Clobbered.
799 // index - holds the overwritten index on exit.
800 void IndexFromHash(Register hash, Register index);
801
Steve Blockd0582a62009-12-15 09:54:21 +0000802 // Find the function context up the context chain.
803 void LoadContext(Register dst, int context_chain_length);
804
Ben Murdochb0fe1622011-05-05 13:52:32 +0100805 // Load the global function with the given index.
806 void LoadGlobalFunction(int index, Register function);
807
808 // Load the initial map from the global function. The registers
809 // function and map can be the same.
810 void LoadGlobalFunctionInitialMap(Register function, Register map);
811
Steve Blocka7e24c12009-10-30 11:49:00 +0000812 // ---------------------------------------------------------------------------
813 // Runtime calls
814
815 // Call a code stub.
816 void CallStub(CodeStub* stub);
817
Ben Murdochbb769b22010-08-11 14:56:33 +0100818 // Call a code stub and return the code object called. Try to generate
819 // the code if necessary. Do not perform a GC but instead return a retry
820 // after GC failure.
John Reck59135872010-11-02 12:39:01 -0700821 MUST_USE_RESULT MaybeObject* TryCallStub(CodeStub* stub);
Ben Murdochbb769b22010-08-11 14:56:33 +0100822
Leon Clarkee46be812010-01-19 14:06:41 +0000823 // Tail call a code stub (jump).
824 void TailCallStub(CodeStub* stub);
825
Ben Murdochbb769b22010-08-11 14:56:33 +0100826 // Tail call a code stub (jump) and return the code object called. Try to
827 // generate the code if necessary. Do not perform a GC but instead return
828 // a retry after GC failure.
John Reck59135872010-11-02 12:39:01 -0700829 MUST_USE_RESULT MaybeObject* TryTailCallStub(CodeStub* stub);
Ben Murdochbb769b22010-08-11 14:56:33 +0100830
Steve Blocka7e24c12009-10-30 11:49:00 +0000831 // Return from a code stub after popping its arguments.
832 void StubReturn(int argc);
833
834 // Call a runtime routine.
Steve Blocka7e24c12009-10-30 11:49:00 +0000835 void CallRuntime(Runtime::Function* f, int num_arguments);
836
Steve Block1e0659c2011-05-24 12:43:12 +0100837 // Call a runtime function and save the value of XMM registers.
838 void CallRuntimeSaveDoubles(Runtime::FunctionId id);
839
Ben Murdochbb769b22010-08-11 14:56:33 +0100840 // Call a runtime function, returning the CodeStub object called.
841 // Try to generate the stub code if necessary. Do not perform a GC
842 // but instead return a retry after GC failure.
John Reck59135872010-11-02 12:39:01 -0700843 MUST_USE_RESULT MaybeObject* TryCallRuntime(Runtime::Function* f,
844 int num_arguments);
Ben Murdochbb769b22010-08-11 14:56:33 +0100845
Steve Blocka7e24c12009-10-30 11:49:00 +0000846 // Convenience function: Same as above, but takes the fid instead.
847 void CallRuntime(Runtime::FunctionId id, int num_arguments);
848
Ben Murdochbb769b22010-08-11 14:56:33 +0100849 // Convenience function: Same as above, but takes the fid instead.
John Reck59135872010-11-02 12:39:01 -0700850 MUST_USE_RESULT MaybeObject* TryCallRuntime(Runtime::FunctionId id,
851 int num_arguments);
Ben Murdochbb769b22010-08-11 14:56:33 +0100852
Andrei Popescu402d9372010-02-26 13:31:12 +0000853 // Convenience function: call an external reference.
854 void CallExternalReference(const ExternalReference& ext,
855 int num_arguments);
856
Steve Blocka7e24c12009-10-30 11:49:00 +0000857 // Tail call of a runtime routine (jump).
Steve Block6ded16b2010-05-10 14:33:55 +0100858 // Like JumpToExternalReference, but also takes care of passing the number
859 // of parameters.
860 void TailCallExternalReference(const ExternalReference& ext,
861 int num_arguments,
862 int result_size);
863
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800864 MUST_USE_RESULT MaybeObject* TryTailCallExternalReference(
865 const ExternalReference& ext, int num_arguments, int result_size);
866
Steve Block6ded16b2010-05-10 14:33:55 +0100867 // Convenience function: tail call a runtime routine (jump).
868 void TailCallRuntime(Runtime::FunctionId fid,
Steve Blocka7e24c12009-10-30 11:49:00 +0000869 int num_arguments,
870 int result_size);
871
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800872 MUST_USE_RESULT MaybeObject* TryTailCallRuntime(Runtime::FunctionId fid,
873 int num_arguments,
874 int result_size);
875
Steve Blocka7e24c12009-10-30 11:49:00 +0000876 // Jump to a runtime routine.
Steve Block6ded16b2010-05-10 14:33:55 +0100877 void JumpToExternalReference(const ExternalReference& ext, int result_size);
Steve Blocka7e24c12009-10-30 11:49:00 +0000878
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800879 // Jump to a runtime routine.
880 MaybeObject* TryJumpToExternalReference(const ExternalReference& ext,
881 int result_size);
John Reck59135872010-11-02 12:39:01 -0700882
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800883 // Prepares stack to put arguments (aligns and so on).
884 // WIN64 calling convention requires to put the pointer to the return value
885 // slot into rcx (rcx must be preserverd until TryCallApiFunctionAndReturn).
886 // Saves context (rsi). Clobbers rax. Allocates arg_stack_space * kPointerSize
887 // inside the exit frame (not GCed) accessible via StackSpaceOperand.
888 void PrepareCallApiFunction(int arg_stack_space);
889
890 // Calls an API function. Allocates HandleScope, extracts
891 // returned value from handle and propagates exceptions.
892 // Clobbers r12, r14, rbx and caller-save registers. Restores context.
893 // On return removes stack_space * kPointerSize (GCed).
894 MUST_USE_RESULT MaybeObject* TryCallApiFunctionAndReturn(
895 ApiFunction* function, int stack_space);
John Reck59135872010-11-02 12:39:01 -0700896
Leon Clarke4515c472010-02-03 11:58:03 +0000897 // Before calling a C-function from generated code, align arguments on stack.
898 // After aligning the frame, arguments must be stored in esp[0], esp[4],
899 // etc., not pushed. The argument count assumes all arguments are word sized.
900 // The number of slots reserved for arguments depends on platform. On Windows
901 // stack slots are reserved for the arguments passed in registers. On other
902 // platforms stack slots are only reserved for the arguments actually passed
903 // on the stack.
904 void PrepareCallCFunction(int num_arguments);
905
906 // Calls a C function and cleans up the space for arguments allocated
907 // by PrepareCallCFunction. The called function is not allowed to trigger a
908 // garbage collection, since that might move the code and invalidate the
909 // return address (unless this is somehow accounted for by the called
910 // function).
911 void CallCFunction(ExternalReference function, int num_arguments);
912 void CallCFunction(Register function, int num_arguments);
913
914 // Calculate the number of stack slots to reserve for arguments when calling a
915 // C function.
916 int ArgumentStackSlotsForCFunctionCall(int num_arguments);
Steve Blocka7e24c12009-10-30 11:49:00 +0000917
918 // ---------------------------------------------------------------------------
919 // Utilities
920
921 void Ret();
922
Steve Block1e0659c2011-05-24 12:43:12 +0100923 // Return and drop arguments from stack, where the number of arguments
924 // may be bigger than 2^16 - 1. Requires a scratch register.
925 void Ret(int bytes_dropped, Register scratch);
926
Steve Blocka7e24c12009-10-30 11:49:00 +0000927 Handle<Object> CodeObject() { return code_object_; }
928
929
930 // ---------------------------------------------------------------------------
931 // StatsCounter support
932
933 void SetCounter(StatsCounter* counter, int value);
934 void IncrementCounter(StatsCounter* counter, int value);
935 void DecrementCounter(StatsCounter* counter, int value);
936
937
938 // ---------------------------------------------------------------------------
939 // Debugging
940
941 // Calls Abort(msg) if the condition cc is not satisfied.
942 // Use --debug_code to enable.
943 void Assert(Condition cc, const char* msg);
944
Iain Merrick75681382010-08-19 15:07:18 +0100945 void AssertFastElements(Register elements);
946
Steve Blocka7e24c12009-10-30 11:49:00 +0000947 // Like Assert(), but always enabled.
948 void Check(Condition cc, const char* msg);
949
950 // Print a message to stdout and abort execution.
951 void Abort(const char* msg);
952
Steve Block6ded16b2010-05-10 14:33:55 +0100953 // Check that the stack is aligned.
954 void CheckStackAlignment();
955
Steve Blocka7e24c12009-10-30 11:49:00 +0000956 // Verify restrictions about code generated in stubs.
957 void set_generating_stub(bool value) { generating_stub_ = value; }
958 bool generating_stub() { return generating_stub_; }
959 void set_allow_stub_calls(bool value) { allow_stub_calls_ = value; }
960 bool allow_stub_calls() { return allow_stub_calls_; }
961
962 private:
Steve Block1e0659c2011-05-24 12:43:12 +0100963 // Order general registers are pushed by Pushad.
964 // rax, rcx, rdx, rbx, rsi, rdi, r8, r9, r11, r12, r14.
965 static int kSafepointPushRegisterIndices[Register::kNumRegisters];
Steve Blocka7e24c12009-10-30 11:49:00 +0000966 bool generating_stub_;
967 bool allow_stub_calls_;
Steve Block8defd9f2010-07-08 12:39:36 +0100968
969 // Returns a register holding the smi value. The register MUST NOT be
970 // modified. It may be the "smi 1 constant" register.
971 Register GetSmiConstant(Smi* value);
972
973 // Moves the smi value to the destination register.
974 void LoadSmiConstant(Register dst, Smi* value);
975
Andrei Popescu31002712010-02-23 13:46:05 +0000976 // This handle will be patched with the code object on installation.
977 Handle<Object> code_object_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000978
979 // Helper functions for generating invokes.
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100980 template <typename LabelType>
Steve Blocka7e24c12009-10-30 11:49:00 +0000981 void InvokePrologue(const ParameterCount& expected,
982 const ParameterCount& actual,
983 Handle<Code> code_constant,
984 Register code_register,
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100985 LabelType* done,
Steve Blocka7e24c12009-10-30 11:49:00 +0000986 InvokeFlag flag);
987
Steve Blocka7e24c12009-10-30 11:49:00 +0000988 // Activation support.
989 void EnterFrame(StackFrame::Type type);
990 void LeaveFrame(StackFrame::Type type);
991
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100992 void EnterExitFramePrologue(bool save_rax);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800993
994 // Allocates arg_stack_space * kPointerSize memory (not GCed) on the stack
995 // accessible via StackSpaceOperand.
Steve Block1e0659c2011-05-24 12:43:12 +0100996 void EnterExitFrameEpilogue(int arg_stack_space, bool save_doubles);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800997
998 void LeaveExitFrameEpilogue();
Ben Murdochbb769b22010-08-11 14:56:33 +0100999
Steve Blocka7e24c12009-10-30 11:49:00 +00001000 // Allocation support helpers.
Steve Block6ded16b2010-05-10 14:33:55 +01001001 // Loads the top of new-space into the result register.
Steve Block6ded16b2010-05-10 14:33:55 +01001002 // Otherwise the address of the new-space top is loaded into scratch (if
1003 // scratch is valid), and the new-space top is loaded into result.
Steve Blocka7e24c12009-10-30 11:49:00 +00001004 void LoadAllocationTopHelper(Register result,
Steve Blocka7e24c12009-10-30 11:49:00 +00001005 Register scratch,
1006 AllocationFlags flags);
Steve Block6ded16b2010-05-10 14:33:55 +01001007 // Update allocation top with value in result_end register.
1008 // If scratch is valid, it contains the address of the allocation top.
Steve Blocka7e24c12009-10-30 11:49:00 +00001009 void UpdateAllocationTopHelper(Register result_end, Register scratch);
Ben Murdochbb769b22010-08-11 14:56:33 +01001010
1011 // Helper for PopHandleScope. Allowed to perform a GC and returns
1012 // NULL if gc_allowed. Does not perform a GC if !gc_allowed, and
1013 // possibly returns a failure object indicating an allocation failure.
1014 Object* PopHandleScopeHelper(Register saved,
1015 Register scratch,
1016 bool gc_allowed);
Steve Blocka7e24c12009-10-30 11:49:00 +00001017};
1018
1019
1020// The code patcher is used to patch (typically) small parts of code e.g. for
1021// debugging and other types of instrumentation. When using the code patcher
1022// the exact number of bytes specified must be emitted. Is not legal to emit
1023// relocation information. If any of these constraints are violated it causes
1024// an assertion.
1025class CodePatcher {
1026 public:
1027 CodePatcher(byte* address, int size);
1028 virtual ~CodePatcher();
1029
1030 // Macro assembler to emit code.
1031 MacroAssembler* masm() { return &masm_; }
1032
1033 private:
1034 byte* address_; // The address of the code being patched.
1035 int size_; // Number of bytes of the expected patch size.
1036 MacroAssembler masm_; // Macro assembler used to generate the code.
1037};
1038
1039
1040// -----------------------------------------------------------------------------
1041// Static helper functions.
1042
1043// Generate an Operand for loading a field from an object.
1044static inline Operand FieldOperand(Register object, int offset) {
1045 return Operand(object, offset - kHeapObjectTag);
1046}
1047
1048
1049// Generate an Operand for loading an indexed field from an object.
1050static inline Operand FieldOperand(Register object,
1051 Register index,
1052 ScaleFactor scale,
1053 int offset) {
1054 return Operand(object, index, scale, offset - kHeapObjectTag);
1055}
1056
1057
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001058static inline Operand ContextOperand(Register context, int index) {
1059 return Operand(context, Context::SlotOffset(index));
1060}
1061
1062
1063static inline Operand GlobalObjectOperand() {
1064 return ContextOperand(rsi, Context::GLOBAL_INDEX);
1065}
1066
1067
1068// Provides access to exit frame stack space (not GCed).
1069static inline Operand StackSpaceOperand(int index) {
1070#ifdef _WIN64
1071 const int kShaddowSpace = 4;
1072 return Operand(rsp, (index + kShaddowSpace) * kPointerSize);
1073#else
1074 return Operand(rsp, index * kPointerSize);
1075#endif
1076}
1077
1078
1079
Steve Blocka7e24c12009-10-30 11:49:00 +00001080#ifdef GENERATED_CODE_COVERAGE
1081extern void LogGeneratedCodeCoverage(const char* file_line);
1082#define CODE_COVERAGE_STRINGIFY(x) #x
1083#define CODE_COVERAGE_TOSTRING(x) CODE_COVERAGE_STRINGIFY(x)
1084#define __FILE_LINE__ __FILE__ ":" CODE_COVERAGE_TOSTRING(__LINE__)
1085#define ACCESS_MASM(masm) { \
1086 byte* x64_coverage_function = \
1087 reinterpret_cast<byte*>(FUNCTION_ADDR(LogGeneratedCodeCoverage)); \
1088 masm->pushfd(); \
1089 masm->pushad(); \
1090 masm->push(Immediate(reinterpret_cast<int>(&__FILE_LINE__))); \
1091 masm->call(x64_coverage_function, RelocInfo::RUNTIME_ENTRY); \
1092 masm->pop(rax); \
1093 masm->popad(); \
1094 masm->popfd(); \
1095 } \
1096 masm->
1097#else
1098#define ACCESS_MASM(masm) masm->
1099#endif
1100
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001101// -----------------------------------------------------------------------------
1102// Template implementations.
1103
1104static int kSmiShift = kSmiTagSize + kSmiShiftSize;
1105
1106
1107template <typename LabelType>
1108void MacroAssembler::SmiNeg(Register dst,
1109 Register src,
1110 LabelType* on_smi_result) {
1111 if (dst.is(src)) {
1112 ASSERT(!dst.is(kScratchRegister));
1113 movq(kScratchRegister, src);
1114 neg(dst); // Low 32 bits are retained as zero by negation.
1115 // Test if result is zero or Smi::kMinValue.
1116 cmpq(dst, kScratchRegister);
1117 j(not_equal, on_smi_result);
1118 movq(src, kScratchRegister);
1119 } else {
1120 movq(dst, src);
1121 neg(dst);
1122 cmpq(dst, src);
1123 // If the result is zero or Smi::kMinValue, negation failed to create a smi.
1124 j(not_equal, on_smi_result);
1125 }
1126}
1127
1128
1129template <typename LabelType>
1130void MacroAssembler::SmiAdd(Register dst,
1131 Register src1,
1132 Register src2,
1133 LabelType* on_not_smi_result) {
1134 ASSERT_NOT_NULL(on_not_smi_result);
1135 ASSERT(!dst.is(src2));
1136 if (dst.is(src1)) {
1137 movq(kScratchRegister, src1);
1138 addq(kScratchRegister, src2);
1139 j(overflow, on_not_smi_result);
1140 movq(dst, kScratchRegister);
1141 } else {
1142 movq(dst, src1);
1143 addq(dst, src2);
1144 j(overflow, on_not_smi_result);
1145 }
1146}
1147
1148
1149template <typename LabelType>
1150void MacroAssembler::SmiSub(Register dst,
1151 Register src1,
1152 Register src2,
1153 LabelType* on_not_smi_result) {
1154 ASSERT_NOT_NULL(on_not_smi_result);
1155 ASSERT(!dst.is(src2));
1156 if (dst.is(src1)) {
1157 cmpq(dst, src2);
1158 j(overflow, on_not_smi_result);
1159 subq(dst, src2);
1160 } else {
1161 movq(dst, src1);
1162 subq(dst, src2);
1163 j(overflow, on_not_smi_result);
1164 }
1165}
1166
1167
1168template <typename LabelType>
1169void MacroAssembler::SmiSub(Register dst,
1170 Register src1,
1171 const Operand& src2,
1172 LabelType* on_not_smi_result) {
1173 ASSERT_NOT_NULL(on_not_smi_result);
1174 if (dst.is(src1)) {
1175 movq(kScratchRegister, src2);
1176 cmpq(src1, kScratchRegister);
1177 j(overflow, on_not_smi_result);
1178 subq(src1, kScratchRegister);
1179 } else {
1180 movq(dst, src1);
1181 subq(dst, src2);
1182 j(overflow, on_not_smi_result);
1183 }
1184}
1185
1186
1187template <typename LabelType>
1188void MacroAssembler::SmiMul(Register dst,
1189 Register src1,
1190 Register src2,
1191 LabelType* on_not_smi_result) {
1192 ASSERT(!dst.is(src2));
1193 ASSERT(!dst.is(kScratchRegister));
1194 ASSERT(!src1.is(kScratchRegister));
1195 ASSERT(!src2.is(kScratchRegister));
1196
1197 if (dst.is(src1)) {
1198 NearLabel failure, zero_correct_result;
1199 movq(kScratchRegister, src1); // Create backup for later testing.
1200 SmiToInteger64(dst, src1);
1201 imul(dst, src2);
1202 j(overflow, &failure);
1203
1204 // Check for negative zero result. If product is zero, and one
1205 // argument is negative, go to slow case.
1206 NearLabel correct_result;
1207 testq(dst, dst);
1208 j(not_zero, &correct_result);
1209
1210 movq(dst, kScratchRegister);
1211 xor_(dst, src2);
1212 j(positive, &zero_correct_result); // Result was positive zero.
1213
1214 bind(&failure); // Reused failure exit, restores src1.
1215 movq(src1, kScratchRegister);
1216 jmp(on_not_smi_result);
1217
1218 bind(&zero_correct_result);
Steve Block9fac8402011-05-12 15:51:54 +01001219 Set(dst, 0);
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001220
1221 bind(&correct_result);
1222 } else {
1223 SmiToInteger64(dst, src1);
1224 imul(dst, src2);
1225 j(overflow, on_not_smi_result);
1226 // Check for negative zero result. If product is zero, and one
1227 // argument is negative, go to slow case.
1228 NearLabel correct_result;
1229 testq(dst, dst);
1230 j(not_zero, &correct_result);
1231 // One of src1 and src2 is zero, the check whether the other is
1232 // negative.
1233 movq(kScratchRegister, src1);
1234 xor_(kScratchRegister, src2);
1235 j(negative, on_not_smi_result);
1236 bind(&correct_result);
1237 }
1238}
1239
1240
1241template <typename LabelType>
1242void MacroAssembler::SmiTryAddConstant(Register dst,
1243 Register src,
1244 Smi* constant,
1245 LabelType* on_not_smi_result) {
1246 // Does not assume that src is a smi.
1247 ASSERT_EQ(static_cast<int>(1), static_cast<int>(kSmiTagMask));
1248 ASSERT_EQ(0, kSmiTag);
1249 ASSERT(!dst.is(kScratchRegister));
1250 ASSERT(!src.is(kScratchRegister));
1251
1252 JumpIfNotSmi(src, on_not_smi_result);
1253 Register tmp = (dst.is(src) ? kScratchRegister : dst);
1254 LoadSmiConstant(tmp, constant);
1255 addq(tmp, src);
1256 j(overflow, on_not_smi_result);
1257 if (dst.is(src)) {
1258 movq(dst, tmp);
1259 }
1260}
1261
1262
1263template <typename LabelType>
1264void MacroAssembler::SmiAddConstant(Register dst,
1265 Register src,
1266 Smi* constant,
1267 LabelType* on_not_smi_result) {
1268 if (constant->value() == 0) {
1269 if (!dst.is(src)) {
1270 movq(dst, src);
1271 }
1272 } else if (dst.is(src)) {
1273 ASSERT(!dst.is(kScratchRegister));
1274
1275 LoadSmiConstant(kScratchRegister, constant);
1276 addq(kScratchRegister, src);
1277 j(overflow, on_not_smi_result);
1278 movq(dst, kScratchRegister);
1279 } else {
1280 LoadSmiConstant(dst, constant);
1281 addq(dst, src);
1282 j(overflow, on_not_smi_result);
1283 }
1284}
1285
1286
1287template <typename LabelType>
1288void MacroAssembler::SmiSubConstant(Register dst,
1289 Register src,
1290 Smi* constant,
1291 LabelType* on_not_smi_result) {
1292 if (constant->value() == 0) {
1293 if (!dst.is(src)) {
1294 movq(dst, src);
1295 }
1296 } else if (dst.is(src)) {
1297 ASSERT(!dst.is(kScratchRegister));
1298 if (constant->value() == Smi::kMinValue) {
1299 // Subtracting min-value from any non-negative value will overflow.
1300 // We test the non-negativeness before doing the subtraction.
1301 testq(src, src);
1302 j(not_sign, on_not_smi_result);
1303 LoadSmiConstant(kScratchRegister, constant);
1304 subq(dst, kScratchRegister);
1305 } else {
1306 // Subtract by adding the negation.
1307 LoadSmiConstant(kScratchRegister, Smi::FromInt(-constant->value()));
1308 addq(kScratchRegister, dst);
1309 j(overflow, on_not_smi_result);
1310 movq(dst, kScratchRegister);
1311 }
1312 } else {
1313 if (constant->value() == Smi::kMinValue) {
1314 // Subtracting min-value from any non-negative value will overflow.
1315 // We test the non-negativeness before doing the subtraction.
1316 testq(src, src);
1317 j(not_sign, on_not_smi_result);
1318 LoadSmiConstant(dst, constant);
1319 // Adding and subtracting the min-value gives the same result, it only
1320 // differs on the overflow bit, which we don't check here.
1321 addq(dst, src);
1322 } else {
1323 // Subtract by adding the negation.
1324 LoadSmiConstant(dst, Smi::FromInt(-(constant->value())));
1325 addq(dst, src);
1326 j(overflow, on_not_smi_result);
1327 }
1328 }
1329}
1330
1331
1332template <typename LabelType>
1333void MacroAssembler::SmiDiv(Register dst,
1334 Register src1,
1335 Register src2,
1336 LabelType* on_not_smi_result) {
1337 ASSERT(!src1.is(kScratchRegister));
1338 ASSERT(!src2.is(kScratchRegister));
1339 ASSERT(!dst.is(kScratchRegister));
1340 ASSERT(!src2.is(rax));
1341 ASSERT(!src2.is(rdx));
1342 ASSERT(!src1.is(rdx));
1343
1344 // Check for 0 divisor (result is +/-Infinity).
1345 NearLabel positive_divisor;
1346 testq(src2, src2);
1347 j(zero, on_not_smi_result);
1348
1349 if (src1.is(rax)) {
1350 movq(kScratchRegister, src1);
1351 }
1352 SmiToInteger32(rax, src1);
1353 // We need to rule out dividing Smi::kMinValue by -1, since that would
1354 // overflow in idiv and raise an exception.
1355 // We combine this with negative zero test (negative zero only happens
1356 // when dividing zero by a negative number).
1357
1358 // We overshoot a little and go to slow case if we divide min-value
1359 // by any negative value, not just -1.
1360 NearLabel safe_div;
1361 testl(rax, Immediate(0x7fffffff));
1362 j(not_zero, &safe_div);
1363 testq(src2, src2);
1364 if (src1.is(rax)) {
1365 j(positive, &safe_div);
1366 movq(src1, kScratchRegister);
1367 jmp(on_not_smi_result);
1368 } else {
1369 j(negative, on_not_smi_result);
1370 }
1371 bind(&safe_div);
1372
1373 SmiToInteger32(src2, src2);
1374 // Sign extend src1 into edx:eax.
1375 cdq();
1376 idivl(src2);
1377 Integer32ToSmi(src2, src2);
1378 // Check that the remainder is zero.
1379 testl(rdx, rdx);
1380 if (src1.is(rax)) {
1381 NearLabel smi_result;
1382 j(zero, &smi_result);
1383 movq(src1, kScratchRegister);
1384 jmp(on_not_smi_result);
1385 bind(&smi_result);
1386 } else {
1387 j(not_zero, on_not_smi_result);
1388 }
1389 if (!dst.is(src1) && src1.is(rax)) {
1390 movq(src1, kScratchRegister);
1391 }
1392 Integer32ToSmi(dst, rax);
1393}
1394
1395
1396template <typename LabelType>
1397void MacroAssembler::SmiMod(Register dst,
1398 Register src1,
1399 Register src2,
1400 LabelType* on_not_smi_result) {
1401 ASSERT(!dst.is(kScratchRegister));
1402 ASSERT(!src1.is(kScratchRegister));
1403 ASSERT(!src2.is(kScratchRegister));
1404 ASSERT(!src2.is(rax));
1405 ASSERT(!src2.is(rdx));
1406 ASSERT(!src1.is(rdx));
1407 ASSERT(!src1.is(src2));
1408
1409 testq(src2, src2);
1410 j(zero, on_not_smi_result);
1411
1412 if (src1.is(rax)) {
1413 movq(kScratchRegister, src1);
1414 }
1415 SmiToInteger32(rax, src1);
1416 SmiToInteger32(src2, src2);
1417
1418 // Test for the edge case of dividing Smi::kMinValue by -1 (will overflow).
1419 NearLabel safe_div;
1420 cmpl(rax, Immediate(Smi::kMinValue));
1421 j(not_equal, &safe_div);
1422 cmpl(src2, Immediate(-1));
1423 j(not_equal, &safe_div);
1424 // Retag inputs and go slow case.
1425 Integer32ToSmi(src2, src2);
1426 if (src1.is(rax)) {
1427 movq(src1, kScratchRegister);
1428 }
1429 jmp(on_not_smi_result);
1430 bind(&safe_div);
1431
1432 // Sign extend eax into edx:eax.
1433 cdq();
1434 idivl(src2);
1435 // Restore smi tags on inputs.
1436 Integer32ToSmi(src2, src2);
1437 if (src1.is(rax)) {
1438 movq(src1, kScratchRegister);
1439 }
1440 // Check for a negative zero result. If the result is zero, and the
1441 // dividend is negative, go slow to return a floating point negative zero.
1442 NearLabel smi_result;
1443 testl(rdx, rdx);
1444 j(not_zero, &smi_result);
1445 testq(src1, src1);
1446 j(negative, on_not_smi_result);
1447 bind(&smi_result);
1448 Integer32ToSmi(dst, rdx);
1449}
1450
1451
1452template <typename LabelType>
1453void MacroAssembler::SmiShiftLogicalRightConstant(
1454 Register dst, Register src, int shift_value, LabelType* on_not_smi_result) {
1455 // Logic right shift interprets its result as an *unsigned* number.
1456 if (dst.is(src)) {
1457 UNIMPLEMENTED(); // Not used.
1458 } else {
1459 movq(dst, src);
1460 if (shift_value == 0) {
1461 testq(dst, dst);
1462 j(negative, on_not_smi_result);
1463 }
1464 shr(dst, Immediate(shift_value + kSmiShift));
1465 shl(dst, Immediate(kSmiShift));
1466 }
1467}
1468
1469
1470template <typename LabelType>
1471void MacroAssembler::SmiShiftLogicalRight(Register dst,
1472 Register src1,
1473 Register src2,
1474 LabelType* on_not_smi_result) {
1475 ASSERT(!dst.is(kScratchRegister));
1476 ASSERT(!src1.is(kScratchRegister));
1477 ASSERT(!src2.is(kScratchRegister));
1478 ASSERT(!dst.is(rcx));
Steve Block1e0659c2011-05-24 12:43:12 +01001479 // dst and src1 can be the same, because the one case that bails out
1480 // is a shift by 0, which leaves dst, and therefore src1, unchanged.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001481 NearLabel result_ok;
1482 if (src1.is(rcx) || src2.is(rcx)) {
1483 movq(kScratchRegister, rcx);
1484 }
1485 if (!dst.is(src1)) {
1486 movq(dst, src1);
1487 }
1488 SmiToInteger32(rcx, src2);
1489 orl(rcx, Immediate(kSmiShift));
1490 shr_cl(dst); // Shift is rcx modulo 0x1f + 32.
1491 shl(dst, Immediate(kSmiShift));
1492 testq(dst, dst);
1493 if (src1.is(rcx) || src2.is(rcx)) {
1494 NearLabel positive_result;
1495 j(positive, &positive_result);
1496 if (src1.is(rcx)) {
1497 movq(src1, kScratchRegister);
1498 } else {
1499 movq(src2, kScratchRegister);
1500 }
1501 jmp(on_not_smi_result);
1502 bind(&positive_result);
1503 } else {
1504 j(negative, on_not_smi_result); // src2 was zero and src1 negative.
1505 }
1506}
1507
1508
1509template <typename LabelType>
1510void MacroAssembler::SelectNonSmi(Register dst,
1511 Register src1,
1512 Register src2,
1513 LabelType* on_not_smis) {
1514 ASSERT(!dst.is(kScratchRegister));
1515 ASSERT(!src1.is(kScratchRegister));
1516 ASSERT(!src2.is(kScratchRegister));
1517 ASSERT(!dst.is(src1));
1518 ASSERT(!dst.is(src2));
1519 // Both operands must not be smis.
1520#ifdef DEBUG
1521 if (allow_stub_calls()) { // Check contains a stub call.
1522 Condition not_both_smis = NegateCondition(CheckBothSmi(src1, src2));
1523 Check(not_both_smis, "Both registers were smis in SelectNonSmi.");
1524 }
1525#endif
1526 ASSERT_EQ(0, kSmiTag);
1527 ASSERT_EQ(0, Smi::FromInt(0));
1528 movl(kScratchRegister, Immediate(kSmiTagMask));
1529 and_(kScratchRegister, src1);
1530 testl(kScratchRegister, src2);
1531 // If non-zero then both are smis.
1532 j(not_zero, on_not_smis);
1533
1534 // Exactly one operand is a smi.
1535 ASSERT_EQ(1, static_cast<int>(kSmiTagMask));
1536 // kScratchRegister still holds src1 & kSmiTag, which is either zero or one.
1537 subq(kScratchRegister, Immediate(1));
1538 // If src1 is a smi, then scratch register all 1s, else it is all 0s.
1539 movq(dst, src1);
1540 xor_(dst, src2);
1541 and_(dst, kScratchRegister);
1542 // If src1 is a smi, dst holds src1 ^ src2, else it is zero.
1543 xor_(dst, src1);
1544 // If src1 is a smi, dst is src2, else it is src1, i.e., the non-smi.
1545}
1546
1547
1548template <typename LabelType>
1549void MacroAssembler::JumpIfSmi(Register src, LabelType* on_smi) {
1550 ASSERT_EQ(0, kSmiTag);
1551 Condition smi = CheckSmi(src);
1552 j(smi, on_smi);
1553}
1554
1555
1556template <typename LabelType>
1557void MacroAssembler::JumpIfNotSmi(Register src, LabelType* on_not_smi) {
1558 Condition smi = CheckSmi(src);
1559 j(NegateCondition(smi), on_not_smi);
1560}
1561
1562
1563template <typename LabelType>
Ben Murdochf87a2032010-10-22 12:50:53 +01001564void MacroAssembler::JumpUnlessNonNegativeSmi(
1565 Register src, LabelType* on_not_smi_or_negative) {
1566 Condition non_negative_smi = CheckNonNegativeSmi(src);
1567 j(NegateCondition(non_negative_smi), on_not_smi_or_negative);
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001568}
1569
1570
1571template <typename LabelType>
1572void MacroAssembler::JumpIfSmiEqualsConstant(Register src,
1573 Smi* constant,
1574 LabelType* on_equals) {
1575 SmiCompare(src, constant);
1576 j(equal, on_equals);
1577}
1578
1579
1580template <typename LabelType>
1581void MacroAssembler::JumpIfNotValidSmiValue(Register src,
1582 LabelType* on_invalid) {
1583 Condition is_valid = CheckInteger32ValidSmiValue(src);
1584 j(NegateCondition(is_valid), on_invalid);
1585}
1586
1587
1588template <typename LabelType>
1589void MacroAssembler::JumpIfUIntNotValidSmiValue(Register src,
1590 LabelType* on_invalid) {
1591 Condition is_valid = CheckUInteger32ValidSmiValue(src);
1592 j(NegateCondition(is_valid), on_invalid);
1593}
1594
1595
1596template <typename LabelType>
1597void MacroAssembler::JumpIfNotBothSmi(Register src1,
1598 Register src2,
1599 LabelType* on_not_both_smi) {
1600 Condition both_smi = CheckBothSmi(src1, src2);
1601 j(NegateCondition(both_smi), on_not_both_smi);
1602}
1603
1604
1605template <typename LabelType>
Ben Murdochf87a2032010-10-22 12:50:53 +01001606void MacroAssembler::JumpUnlessBothNonNegativeSmi(Register src1,
1607 Register src2,
1608 LabelType* on_not_both_smi) {
1609 Condition both_smi = CheckBothNonNegativeSmi(src1, src2);
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001610 j(NegateCondition(both_smi), on_not_both_smi);
1611}
1612
1613
1614template <typename LabelType>
Steve Block1e0659c2011-05-24 12:43:12 +01001615void MacroAssembler::JumpIfNotString(Register object,
1616 Register object_map,
1617 LabelType* not_string) {
1618 Condition is_smi = CheckSmi(object);
1619 j(is_smi, not_string);
1620 CmpObjectType(object, FIRST_NONSTRING_TYPE, object_map);
1621 j(above_equal, not_string);
1622}
1623
1624
1625template <typename LabelType>
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001626void MacroAssembler::JumpIfNotBothSequentialAsciiStrings(Register first_object,
1627 Register second_object,
1628 Register scratch1,
1629 Register scratch2,
1630 LabelType* on_fail) {
1631 // Check that both objects are not smis.
1632 Condition either_smi = CheckEitherSmi(first_object, second_object);
1633 j(either_smi, on_fail);
1634
1635 // Load instance type for both strings.
1636 movq(scratch1, FieldOperand(first_object, HeapObject::kMapOffset));
1637 movq(scratch2, FieldOperand(second_object, HeapObject::kMapOffset));
1638 movzxbl(scratch1, FieldOperand(scratch1, Map::kInstanceTypeOffset));
1639 movzxbl(scratch2, FieldOperand(scratch2, Map::kInstanceTypeOffset));
1640
1641 // Check that both are flat ascii strings.
1642 ASSERT(kNotStringTag != 0);
1643 const int kFlatAsciiStringMask =
1644 kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask;
1645 const int kFlatAsciiStringTag = ASCII_STRING_TYPE;
1646
1647 andl(scratch1, Immediate(kFlatAsciiStringMask));
1648 andl(scratch2, Immediate(kFlatAsciiStringMask));
1649 // Interleave the bits to check both scratch1 and scratch2 in one test.
1650 ASSERT_EQ(0, kFlatAsciiStringMask & (kFlatAsciiStringMask << 3));
1651 lea(scratch1, Operand(scratch1, scratch2, times_8, 0));
1652 cmpl(scratch1,
1653 Immediate(kFlatAsciiStringTag + (kFlatAsciiStringTag << 3)));
1654 j(not_equal, on_fail);
1655}
1656
1657
1658template <typename LabelType>
1659void MacroAssembler::JumpIfInstanceTypeIsNotSequentialAscii(
1660 Register instance_type,
1661 Register scratch,
1662 LabelType *failure) {
1663 if (!scratch.is(instance_type)) {
1664 movl(scratch, instance_type);
1665 }
1666
1667 const int kFlatAsciiStringMask =
1668 kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask;
1669
1670 andl(scratch, Immediate(kFlatAsciiStringMask));
1671 cmpl(scratch, Immediate(kStringTag | kSeqStringTag | kAsciiStringTag));
1672 j(not_equal, failure);
1673}
1674
1675
1676template <typename LabelType>
1677void MacroAssembler::JumpIfBothInstanceTypesAreNotSequentialAscii(
1678 Register first_object_instance_type,
1679 Register second_object_instance_type,
1680 Register scratch1,
1681 Register scratch2,
1682 LabelType* on_fail) {
1683 // Load instance type for both strings.
1684 movq(scratch1, first_object_instance_type);
1685 movq(scratch2, second_object_instance_type);
1686
1687 // Check that both are flat ascii strings.
1688 ASSERT(kNotStringTag != 0);
1689 const int kFlatAsciiStringMask =
1690 kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask;
1691 const int kFlatAsciiStringTag = ASCII_STRING_TYPE;
1692
1693 andl(scratch1, Immediate(kFlatAsciiStringMask));
1694 andl(scratch2, Immediate(kFlatAsciiStringMask));
1695 // Interleave the bits to check both scratch1 and scratch2 in one test.
1696 ASSERT_EQ(0, kFlatAsciiStringMask & (kFlatAsciiStringMask << 3));
1697 lea(scratch1, Operand(scratch1, scratch2, times_8, 0));
1698 cmpl(scratch1,
1699 Immediate(kFlatAsciiStringTag + (kFlatAsciiStringTag << 3)));
1700 j(not_equal, on_fail);
1701}
1702
1703
1704template <typename LabelType>
1705void MacroAssembler::InNewSpace(Register object,
1706 Register scratch,
1707 Condition cc,
1708 LabelType* branch) {
1709 if (Serializer::enabled()) {
1710 // Can't do arithmetic on external references if it might get serialized.
1711 // The mask isn't really an address. We load it as an external reference in
1712 // case the size of the new space is different between the snapshot maker
1713 // and the running system.
1714 if (scratch.is(object)) {
1715 movq(kScratchRegister, ExternalReference::new_space_mask());
1716 and_(scratch, kScratchRegister);
1717 } else {
1718 movq(scratch, ExternalReference::new_space_mask());
1719 and_(scratch, object);
1720 }
1721 movq(kScratchRegister, ExternalReference::new_space_start());
1722 cmpq(scratch, kScratchRegister);
1723 j(cc, branch);
1724 } else {
1725 ASSERT(is_int32(static_cast<int64_t>(Heap::NewSpaceMask())));
1726 intptr_t new_space_start =
1727 reinterpret_cast<intptr_t>(Heap::NewSpaceStart());
1728 movq(kScratchRegister, -new_space_start, RelocInfo::NONE);
1729 if (scratch.is(object)) {
1730 addq(scratch, kScratchRegister);
1731 } else {
1732 lea(scratch, Operand(object, kScratchRegister, times_1, 0));
1733 }
1734 and_(scratch, Immediate(static_cast<int32_t>(Heap::NewSpaceMask())));
1735 j(cc, branch);
1736 }
1737}
1738
1739
1740template <typename LabelType>
1741void MacroAssembler::InvokePrologue(const ParameterCount& expected,
1742 const ParameterCount& actual,
1743 Handle<Code> code_constant,
1744 Register code_register,
1745 LabelType* done,
1746 InvokeFlag flag) {
1747 bool definitely_matches = false;
1748 NearLabel invoke;
1749 if (expected.is_immediate()) {
1750 ASSERT(actual.is_immediate());
1751 if (expected.immediate() == actual.immediate()) {
1752 definitely_matches = true;
1753 } else {
1754 Set(rax, actual.immediate());
1755 if (expected.immediate() ==
1756 SharedFunctionInfo::kDontAdaptArgumentsSentinel) {
1757 // Don't worry about adapting arguments for built-ins that
1758 // don't want that done. Skip adaption code by making it look
1759 // like we have a match between expected and actual number of
1760 // arguments.
1761 definitely_matches = true;
1762 } else {
1763 Set(rbx, expected.immediate());
1764 }
1765 }
1766 } else {
1767 if (actual.is_immediate()) {
1768 // Expected is in register, actual is immediate. This is the
1769 // case when we invoke function values without going through the
1770 // IC mechanism.
1771 cmpq(expected.reg(), Immediate(actual.immediate()));
1772 j(equal, &invoke);
1773 ASSERT(expected.reg().is(rbx));
1774 Set(rax, actual.immediate());
1775 } else if (!expected.reg().is(actual.reg())) {
1776 // Both expected and actual are in (different) registers. This
1777 // is the case when we invoke functions using call and apply.
1778 cmpq(expected.reg(), actual.reg());
1779 j(equal, &invoke);
1780 ASSERT(actual.reg().is(rax));
1781 ASSERT(expected.reg().is(rbx));
1782 }
1783 }
1784
1785 if (!definitely_matches) {
1786 Handle<Code> adaptor =
1787 Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline));
1788 if (!code_constant.is_null()) {
1789 movq(rdx, code_constant, RelocInfo::EMBEDDED_OBJECT);
1790 addq(rdx, Immediate(Code::kHeaderSize - kHeapObjectTag));
1791 } else if (!code_register.is(rdx)) {
1792 movq(rdx, code_register);
1793 }
1794
1795 if (flag == CALL_FUNCTION) {
1796 Call(adaptor, RelocInfo::CODE_TARGET);
1797 jmp(done);
1798 } else {
1799 Jump(adaptor, RelocInfo::CODE_TARGET);
1800 }
1801 bind(&invoke);
1802 }
1803}
1804
Steve Blocka7e24c12009-10-30 11:49:00 +00001805
1806} } // namespace v8::internal
1807
1808#endif // V8_X64_MACRO_ASSEMBLER_X64_H_