blob: 8bb3190448fdc16f0b31107b70551d0a664a2712 [file] [log] [blame]
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001// Copyright 2010 the V8 project authors. All rights reserved.
ager@chromium.org5ec48922009-05-05 07:25:34 +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.org9085a012009-05-11 19:22:57 +000028#ifndef V8_X64_MACRO_ASSEMBLER_X64_H_
29#define V8_X64_MACRO_ASSEMBLER_X64_H_
30
31#include "assembler.h"
32
kasperl@chromium.org71affb52009-05-26 05:44:31 +000033namespace v8 {
34namespace internal {
ager@chromium.org9085a012009-05-11 19:22:57 +000035
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +000036// 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
ager@chromium.orge2902be2009-06-08 12:21:35 +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.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +000050static 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;
ager@chromium.orge2902be2009-06-08 12:21:35 +000055
fschneider@chromium.org0c20e672010-01-14 15:28:53 +000056// Convenience for platform-independent signatures.
57typedef Operand MemOperand;
58
ager@chromium.org9085a012009-05-11 19:22:57 +000059// Forward declaration.
60class JumpTarget;
61
ager@chromium.org4af710e2009-09-15 12:20:11 +000062struct SmiIndex {
63 SmiIndex(Register index_register, ScaleFactor scale)
64 : reg(index_register),
65 scale(scale) {}
66 Register reg;
67 ScaleFactor scale;
68};
ager@chromium.org9085a012009-05-11 19:22:57 +000069
ager@chromium.org9085a012009-05-11 19:22:57 +000070// MacroAssembler implements a collection of frequently used macros.
71class MacroAssembler: public Assembler {
72 public:
73 MacroAssembler(void* buffer, int size);
74
ager@chromium.org18ad94b2009-09-02 08:22:29 +000075 void LoadRoot(Register destination, Heap::RootListIndex index);
76 void CompareRoot(Register with, Heap::RootListIndex index);
ricow@chromium.org83aa5492011-02-07 12:42:56 +000077 void CompareRoot(const Operand& with, Heap::RootListIndex index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +000078 void PushRoot(Heap::RootListIndex index);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +000079 void StoreRoot(Register source, Heap::RootListIndex index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +000080
ager@chromium.org9085a012009-05-11 19:22:57 +000081 // ---------------------------------------------------------------------------
82 // GC Support
83
ricow@chromium.org30ce4112010-05-31 10:38:25 +000084 // For page containing |object| mark region covering |addr| dirty.
85 // RecordWriteHelper only works if the object is not in new
ager@chromium.orgac091b72010-05-05 07:34:42 +000086 // space.
87 void RecordWriteHelper(Register object,
88 Register addr,
89 Register scratch);
90
kmillikin@chromium.org4111b802010-05-03 10:34:42 +000091 // 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.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +000094 template <typename LabelType>
kmillikin@chromium.org4111b802010-05-03 10:34:42 +000095 void InNewSpace(Register object,
96 Register scratch,
97 Condition cc,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +000098 LabelType* branch);
kmillikin@chromium.org4111b802010-05-03 10:34:42 +000099
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000100 // 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
whesse@chromium.org4a5224e2010-10-20 12:37:07 +0000104 // 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.
ager@chromium.org9085a012009-05-11 19:22:57 +0000107 void RecordWrite(Register object,
108 int offset,
109 Register value,
110 Register scratch);
111
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000112 // 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
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000121 // For page containing |object| mark region covering [object+offset] dirty.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +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
whesse@chromium.org4a5224e2010-10-20 12:37:07 +0000125 // the elements array represented as an untagged 32-bit integer.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000126 // All registers are clobbered by the operation.
127 void RecordWriteNonSmi(Register object,
128 int offset,
129 Register value,
130 Register scratch);
131
ager@chromium.org9085a012009-05-11 19:22:57 +0000132#ifdef ENABLE_DEBUGGER_SUPPORT
133 // ---------------------------------------------------------------------------
134 // Debugger Support
135
ager@chromium.org5c838252010-02-19 08:53:10 +0000136 void DebugBreak();
ager@chromium.org9085a012009-05-11 19:22:57 +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
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000148 // Enter specific kind of exit frame; either in normal or
149 // debug mode. Expects the number of arguments in register rax and
ager@chromium.orga1645e22009-09-09 19:27:10 +0000150 // sets up the number of arguments in register rdi and the pointer
151 // to the first argument in register rsi.
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +0000152 //
153 // Allocates arg_stack_space * kPointerSize memory (not GCed) on the stack
154 // accessible via StackSpaceOperand.
ager@chromium.org0ee099b2011-01-25 14:06:47 +0000155 void EnterExitFrame(int arg_stack_space = 0, bool save_doubles = false);
ager@chromium.org9085a012009-05-11 19:22:57 +0000156
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +0000157 // 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);
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000160
ager@chromium.orga1645e22009-09-09 19:27:10 +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.
ager@chromium.org0ee099b2011-01-25 14:06:47 +0000164 void LeaveExitFrame(bool save_doubles = false);
ager@chromium.org9085a012009-05-11 19:22:57 +0000165
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +0000166 // Leave the current exit frame. Expects/provides the return value in
167 // register rax (untouched).
168 void LeaveApiExitFrame();
ager@chromium.org9085a012009-05-11 19:22:57 +0000169
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000170 // Push and pop the registers that can hold pointers.
ager@chromium.org0ee099b2011-01-25 14:06:47 +0000171 void PushSafepointRegisters() { Pushad(); }
172 void PopSafepointRegisters() { Popad(); }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000173 static int SafepointRegisterStackIndex(int reg_code) {
ager@chromium.org0ee099b2011-01-25 14:06:47 +0000174 return kSafepointPushRegisterIndices[reg_code];
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000175 }
176
ager@chromium.org0ee099b2011-01-25 14:06:47 +0000177
ager@chromium.org9085a012009-05-11 19:22:57 +0000178 // ---------------------------------------------------------------------------
179 // JavaScript invokes
180
181 // Invoke the JavaScript function code by either calling or jumping.
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000182 void InvokeCode(Register code,
ager@chromium.org9085a012009-05-11 19:22:57 +0000183 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
ager@chromium.org5c838252010-02-19 08:53:10 +0000199 void InvokeFunction(JSFunction* function,
200 const ParameterCount& actual,
201 InvokeFlag flag);
202
ager@chromium.org9085a012009-05-11 19:22:57 +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
erik.corry@gmail.com145eff52010-08-23 11:36:18 +0000207 // Store the function for the given builtin in the target register.
208 void GetBuiltinFunction(Register target, Builtins::JavaScript id);
209
ager@chromium.org9085a012009-05-11 19:22:57 +0000210 // Store the code object for the given builtin in the target register.
211 void GetBuiltinEntry(Register target, Builtins::JavaScript id);
212
ager@chromium.org4af710e2009-09-15 12:20:11 +0000213
214 // ---------------------------------------------------------------------------
215 // Smi tagging, untagging and operations on tagged smis.
216
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000217 void InitializeSmiConstantRegister() {
218 movq(kSmiConstantRegister,
219 reinterpret_cast<uint64_t>(Smi::FromInt(kSmiConstantRegisterValue)),
220 RelocInfo::NONE);
221 }
222
ager@chromium.org4af710e2009-09-15 12:20:11 +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.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000226 // Only uses the low 32 bits of the src register. Sets the N and Z flags
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000227 // based on the value of the resulting smi.
ager@chromium.org4af710e2009-09-15 12:20:11 +0000228 void Integer32ToSmi(Register dst, Register src);
229
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000230 // Stores an integer32 value into a memory field that already holds a smi.
231 void Integer32ToSmiField(const Operand& dst, Register src);
232
ager@chromium.org4af710e2009-09-15 12:20:11 +0000233 // Adds constant to src and tags the result as a smi.
234 // Result must be a valid smi.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000235 void Integer64PlusConstantToSmi(Register dst, Register src, int constant);
ager@chromium.org4af710e2009-09-15 12:20:11 +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);
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000240 void SmiToInteger32(Register dst, const Operand& src);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000241
242 // Convert smi to 64-bit integer (sign extended if necessary).
243 void SmiToInteger64(Register dst, Register src);
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000244 void SmiToInteger64(Register dst, const Operand& src);
ager@chromium.org4af710e2009-09-15 12:20:11 +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
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000252 // 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
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000259 // Simple comparison of smis.
260 void SmiCompare(Register dst, Register src);
261 void SmiCompare(Register dst, Smi* src);
ager@chromium.orgac091b72010-05-05 07:34:42 +0000262 void SmiCompare(Register dst, const Operand& src);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000263 void SmiCompare(const Operand& dst, Register src);
264 void SmiCompare(const Operand& dst, Smi* src);
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000265 // Compare the int32 in src register to the value of the smi stored at dst.
266 void SmiCompareInteger32(const Operand& dst, Register src);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000267 // Sets sign and zero flags depending on value of smi in register.
268 void SmiTest(Register src);
269
ager@chromium.org4af710e2009-09-15 12:20:11 +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);
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000275 Condition CheckSmi(const Operand& src);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000276
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +0000277 // Is the value a non-negative tagged smi.
278 Condition CheckNonNegativeSmi(Register src);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000279
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000280 // Are both values tagged smis.
ager@chromium.org4af710e2009-09-15 12:20:11 +0000281 Condition CheckBothSmi(Register first, Register second);
282
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +0000283 // Are both values non-negative tagged smis.
284 Condition CheckBothNonNegativeSmi(Register first, Register second);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000285
286 // Are either value a tagged smi.
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +0000287 Condition CheckEitherSmi(Register first,
288 Register second,
289 Register scratch = kScratchRegister);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000290
ager@chromium.org4af710e2009-09-15 12:20:11 +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
ager@chromium.org4af710e2009-09-15 12:20:11 +0000296 // Checks whether an 32-bit integer value is a valid for conversion
297 // to a smi.
298 Condition CheckInteger32ValidSmiValue(Register src);
299
ager@chromium.org3811b432009-10-28 14:53:37 +0000300 // Checks whether an 32-bit unsigned integer value is a valid for
301 // conversion to a smi.
302 Condition CheckUInteger32ValidSmiValue(Register src);
303
ager@chromium.org0ee099b2011-01-25 14:06:47 +0000304 // 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
ager@chromium.org4af710e2009-09-15 12:20:11 +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.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000313 template <typename LabelType>
314 void JumpIfNotValidSmiValue(Register src, LabelType* on_invalid);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000315
ager@chromium.org3811b432009-10-28 14:53:37 +0000316 // Jump if the unsigned integer value cannot be represented by a smi.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000317 template <typename LabelType>
318 void JumpIfUIntNotValidSmiValue(Register src, LabelType* on_invalid);
ager@chromium.org3811b432009-10-28 14:53:37 +0000319
ager@chromium.org4af710e2009-09-15 12:20:11 +0000320 // Jump to label if the value is a tagged smi.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000321 template <typename LabelType>
322 void JumpIfSmi(Register src, LabelType* on_smi);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000323
324 // Jump to label if the value is not a tagged smi.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000325 template <typename LabelType>
326 void JumpIfNotSmi(Register src, LabelType* on_not_smi);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000327
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +0000328 // Jump to label if the value is not a non-negative tagged smi.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000329 template <typename LabelType>
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +0000330 void JumpUnlessNonNegativeSmi(Register src, LabelType* on_not_smi);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000331
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000332 // Jump to label if the value, which must be a tagged smi, has value equal
ager@chromium.org4af710e2009-09-15 12:20:11 +0000333 // to the constant.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000334 template <typename LabelType>
335 void JumpIfSmiEqualsConstant(Register src,
336 Smi* constant,
337 LabelType* on_equals);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000338
ager@chromium.org4af710e2009-09-15 12:20:11 +0000339 // Jump if either or both register are not smi values.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000340 template <typename LabelType>
341 void JumpIfNotBothSmi(Register src1,
342 Register src2,
343 LabelType* on_not_both_smi);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000344
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +0000345 // Jump if either or both register are not non-negative smi values.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000346 template <typename LabelType>
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +0000347 void JumpUnlessBothNonNegativeSmi(Register src1, Register src2,
348 LabelType* on_not_both_smi);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000349
ager@chromium.org4af710e2009-09-15 12:20:11 +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.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000358 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000359 void SmiTryAddConstant(Register dst,
360 Register src,
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000361 Smi* constant,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000362 LabelType* on_not_smi_result);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000363
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +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
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000368 // 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
ager@chromium.org4af710e2009-09-15 12:20:11 +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.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000374 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000375 void SmiAddConstant(Register dst,
376 Register src,
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000377 Smi* constant,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000378 LabelType* on_not_smi_result);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000379
380 // Subtract an integer constant from a tagged smi, giving a tagged smi as
ager@chromium.orgac091b72010-05-05 07:34:42 +0000381 // result. No testing on the result is done. Sets the N and Z flags
382 // based on the value of the resulting integer.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000383 void SmiSubConstant(Register dst, Register src, Smi* constant);
384
385 // Subtract an integer constant from a tagged smi, giving a tagged smi as
ager@chromium.org4af710e2009-09-15 12:20:11 +0000386 // result, or jumping to a label if the result cannot be represented by a smi.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000387 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000388 void SmiSubConstant(Register dst,
389 Register src,
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000390 Smi* constant,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000391 LabelType* on_not_smi_result);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000392
393 // Negating a smi can give a negative zero or too large positive value.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000394 // NOTICE: This operation jumps on success, not failure!
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000395 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000396 void SmiNeg(Register dst,
397 Register src,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000398 LabelType* on_smi_result);
ager@chromium.org4af710e2009-09-15 12:20:11 +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.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000403 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000404 void SmiAdd(Register dst,
405 Register src1,
406 Register src2,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000407 LabelType* on_not_smi_result);
408
409 void SmiAdd(Register dst,
410 Register src1,
411 Register src2);
ager@chromium.org4af710e2009-09-15 12:20:11 +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.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000416 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000417 void SmiSub(Register dst,
418 Register src1,
419 Register src2,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000420 LabelType* on_not_smi_result);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000421
ager@chromium.orgac091b72010-05-05 07:34:42 +0000422 void SmiSub(Register dst,
423 Register src1,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000424 Register src2);
425
426 template <typename LabelType>
427 void SmiSub(Register dst,
428 Register src1,
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000429 const Operand& src2,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000430 LabelType* on_not_smi_result);
431
432 void SmiSub(Register dst,
433 Register src1,
434 const Operand& src2);
ager@chromium.orgac091b72010-05-05 07:34:42 +0000435
ager@chromium.org4af710e2009-09-15 12:20:11 +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.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000440 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000441 void SmiMul(Register dst,
442 Register src1,
443 Register src2,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000444 LabelType* on_not_smi_result);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000445
446 // Divides one smi by another and returns the quotient.
447 // Clobbers rax and rdx registers.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000448 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000449 void SmiDiv(Register dst,
450 Register src1,
451 Register src2,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000452 LabelType* on_not_smi_result);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000453
454 // Divides one smi by another and returns the remainder.
455 // Clobbers rax and rdx registers.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000456 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000457 void SmiMod(Register dst,
458 Register src1,
459 Register src2,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000460 LabelType* on_not_smi_result);
ager@chromium.org4af710e2009-09-15 12:20:11 +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);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +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);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000470
471 void SmiShiftLeftConstant(Register dst,
472 Register src,
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000473 int shift_value);
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000474 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000475 void SmiShiftLogicalRightConstant(Register dst,
476 Register src,
477 int shift_value,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000478 LabelType* on_not_smi_result);
ager@chromium.org4af710e2009-09-15 12:20:11 +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,
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000487 Register src2);
ager@chromium.org4af710e2009-09-15 12:20:11 +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.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000491 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000492 void SmiShiftLogicalRight(Register dst,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000493 Register src1,
494 Register src2,
495 LabelType* on_not_smi_result);
ager@chromium.org4af710e2009-09-15 12:20:11 +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.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000509 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000510 void SelectNonSmi(Register dst,
511 Register src1,
512 Register src2,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000513 LabelType* on_not_smis);
ager@chromium.org4af710e2009-09-15 12:20:11 +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
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000528 // Basic Smi operations.
ager@chromium.org3811b432009-10-28 14:53:37 +0000529 void Move(Register dst, Smi* source) {
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000530 LoadSmiConstant(dst, source);
ager@chromium.org3811b432009-10-28 14:53:37 +0000531 }
532
533 void Move(const Operand& dst, Smi* source) {
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000534 Register constant = GetSmiConstant(source);
535 movq(dst, constant);
ager@chromium.org3811b432009-10-28 14:53:37 +0000536 }
537
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000538 void Push(Smi* smi);
539 void Test(const Operand& dst, Smi* source);
540
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000541 // ---------------------------------------------------------------------------
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000542 // String macros.
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000543
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
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000551 template <typename LabelType>
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000552 void JumpIfNotBothSequentialAsciiStrings(Register first_object,
553 Register second_object,
554 Register scratch1,
555 Register scratch2,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000556 LabelType* on_not_both_flat_ascii);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000557
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000558 // 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.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000561 template <typename LabelType>
562 void JumpIfInstanceTypeIsNotSequentialAscii(
563 Register instance_type,
564 Register scratch,
565 LabelType *on_not_flat_ascii_string);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000566
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000567 template <typename LabelType>
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000568 void JumpIfBothInstanceTypesAreNotSequentialAscii(
569 Register first_object_instance_type,
570 Register second_object_instance_type,
571 Register scratch1,
572 Register scratch2,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000573 LabelType* on_fail);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000574
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000575 // ---------------------------------------------------------------------------
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000576 // Macro instructions.
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000577
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000578 // Load a register with a long value as efficiently as possible.
ager@chromium.orge2902be2009-06-08 12:21:35 +0000579 void Set(Register dst, int64_t x);
580 void Set(const Operand& dst, int64_t x);
ager@chromium.org9085a012009-05-11 19:22:57 +0000581
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000582 // Move if the registers are not identical.
583 void Move(Register target, Register source);
584
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000585 // Handle support
ager@chromium.org5aa501c2009-06-23 07:57:28 +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);
ager@chromium.org3e875802009-06-29 08:26:34 +0000589 void Cmp(const Operand& dst, Handle<Object> source);
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000590 void Push(Handle<Object> source);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000591
592 // Emit code to discard a non-negative number of pointer-sized elements
593 // from the stack, clobbering only the rsp register.
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000594 void Drop(int stack_elements);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000595
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000596 void Call(Label* target) { call(target); }
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000597
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000598 // Control Flow
599 void Jump(Address destination, RelocInfo::Mode rmode);
600 void Jump(ExternalReference ext);
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000601 void Jump(Handle<Code> code_object, RelocInfo::Mode rmode);
602
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000603 void Call(Address destination, RelocInfo::Mode rmode);
604 void Call(ExternalReference ext);
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000605 void Call(Handle<Code> code_object, RelocInfo::Mode rmode);
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000606
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000607 // 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
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000613 // 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();
ager@chromium.org0ee099b2011-01-25 14:06:47 +0000619 // Sets the stack as after performing Popad, without actually loading the
620 // registers.
621 void Dropad();
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000622
ager@chromium.org9085a012009-05-11 19:22:57 +0000623 // Compare object type for heap object.
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000624 // Always use unsigned comparisons: above and below, not less and greater.
ager@chromium.org9085a012009-05-11 19:22:57 +0000625 // Incoming register is heap_object and outgoing register is map.
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000626 // They may be the same register, and may be kScratchRegister.
ager@chromium.org9085a012009-05-11 19:22:57 +0000627 void CmpObjectType(Register heap_object, InstanceType type, Register map);
628
629 // Compare instance type for map.
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000630 // Always use unsigned comparisons: above and below, not less and greater.
ager@chromium.org9085a012009-05-11 19:22:57 +0000631 void CmpInstanceType(Register map, InstanceType type);
632
ager@chromium.org5c838252010-02-19 08:53:10 +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
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +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
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000650 // 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
ager@chromium.org9085a012009-05-11 19:22:57 +0000652 // jcc instructions (je, ja, jae, jb, jbe, je, and jz).
653 void FCmp();
654
ager@chromium.org5c838252010-02-19 08:53:10 +0000655 // Abort execution if argument is not a number. Used in debug code.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000656 void AbortIfNotNumber(Register object);
ager@chromium.org5c838252010-02-19 08:53:10 +0000657
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000658 // Abort execution if argument is a smi. Used in debug code.
659 void AbortIfSmi(Register object);
660
lrn@chromium.org25156de2010-04-06 13:10:27 +0000661 // Abort execution if argument is not a smi. Used in debug code.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000662 void AbortIfNotSmi(Register object);
lrn@chromium.org25156de2010-04-06 13:10:27 +0000663
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000664 // 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
ager@chromium.org9085a012009-05-11 19:22:57 +0000669 // ---------------------------------------------------------------------------
670 // Exception handling
671
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000672 // Push a new try handler and link into try handler chain. The return
673 // address must be pushed before calling this helper.
ager@chromium.org9085a012009-05-11 19:22:57 +0000674 void PushTryHandler(CodeLocation try_location, HandlerType type);
675
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000676 // Unlink the stack handler on top of the stack from the try handler chain.
677 void PopTryHandler();
ager@chromium.org9085a012009-05-11 19:22:57 +0000678
679 // ---------------------------------------------------------------------------
680 // Inline caching support
681
ager@chromium.org9085a012009-05-11 19:22:57 +0000682 // Generate code for checking access rights - used for security checks
683 // on access to global objects across environments. The holder register
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000684 // is left untouched, but the scratch register and kScratchRegister,
685 // which must be different, are clobbered.
ager@chromium.org9085a012009-05-11 19:22:57 +0000686 void CheckAccessGlobalProxy(Register holder_reg,
687 Register scratch,
688 Label* miss);
689
690
691 // ---------------------------------------------------------------------------
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000692 // 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
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000702 // AllocateInNewSpace). If result_contains_top_on_entry is true scratch
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000703 // should be no_reg as it is never used.
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000704 void AllocateInNewSpace(int object_size,
705 Register result,
706 Register result_end,
707 Register scratch,
708 Label* gc_required,
709 AllocationFlags flags);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000710
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000711 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);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000719
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000720 void AllocateInNewSpace(Register object_size,
721 Register result,
722 Register result_end,
723 Register scratch,
724 Label* gc_required,
725 AllocationFlags flags);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000726
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
ager@chromium.org3811b432009-10-28 14:53:37 +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
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +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
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000766 // ---------------------------------------------------------------------------
ager@chromium.org9085a012009-05-11 19:22:57 +0000767 // 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
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000786 // function register will be untouched; the other register may be
ager@chromium.org9085a012009-05-11 19:22:57 +0000787 // clobbered.
788 void TryGetFunctionPrototype(Register function,
789 Register result,
ager@chromium.org9085a012009-05-11 19:22:57 +0000790 Label* miss);
791
792 // Generates code for reporting that an illegal operation has
793 // occurred.
794 void IllegalOperation(int num_arguments);
795
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000796 // 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
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000802 // Find the function context up the context chain.
803 void LoadContext(Register dst, int context_chain_length);
804
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000805 // 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
ager@chromium.org9085a012009-05-11 19:22:57 +0000812 // ---------------------------------------------------------------------------
813 // Runtime calls
814
815 // Call a code stub.
816 void CallStub(CodeStub* stub);
817
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000818 // 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.
lrn@chromium.org303ada72010-10-27 09:33:13 +0000821 MUST_USE_RESULT MaybeObject* TryCallStub(CodeStub* stub);
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000822
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000823 // Tail call a code stub (jump).
824 void TailCallStub(CodeStub* stub);
825
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000826 // 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.
lrn@chromium.org303ada72010-10-27 09:33:13 +0000829 MUST_USE_RESULT MaybeObject* TryTailCallStub(CodeStub* stub);
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000830
ager@chromium.org9085a012009-05-11 19:22:57 +0000831 // Return from a code stub after popping its arguments.
832 void StubReturn(int argc);
833
834 // Call a runtime routine.
ager@chromium.org9085a012009-05-11 19:22:57 +0000835 void CallRuntime(Runtime::Function* f, int num_arguments);
836
ager@chromium.org0ee099b2011-01-25 14:06:47 +0000837 // Call a runtime function and save the value of XMM registers.
838 void CallRuntimeSaveDoubles(Runtime::FunctionId id);
839
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000840 // 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.
lrn@chromium.org303ada72010-10-27 09:33:13 +0000843 MUST_USE_RESULT MaybeObject* TryCallRuntime(Runtime::Function* f,
844 int num_arguments);
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000845
ager@chromium.org9085a012009-05-11 19:22:57 +0000846 // Convenience function: Same as above, but takes the fid instead.
847 void CallRuntime(Runtime::FunctionId id, int num_arguments);
848
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000849 // Convenience function: Same as above, but takes the fid instead.
lrn@chromium.org303ada72010-10-27 09:33:13 +0000850 MUST_USE_RESULT MaybeObject* TryCallRuntime(Runtime::FunctionId id,
851 int num_arguments);
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000852
ager@chromium.org5c838252010-02-19 08:53:10 +0000853 // Convenience function: call an external reference.
854 void CallExternalReference(const ExternalReference& ext,
855 int num_arguments);
856
ager@chromium.org9085a012009-05-11 19:22:57 +0000857 // Tail call of a runtime routine (jump).
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000858 // 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
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +0000864 MUST_USE_RESULT MaybeObject* TryTailCallExternalReference(
865 const ExternalReference& ext, int num_arguments, int result_size);
866
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000867 // Convenience function: tail call a runtime routine (jump).
868 void TailCallRuntime(Runtime::FunctionId fid,
ager@chromium.orga1645e22009-09-09 19:27:10 +0000869 int num_arguments,
870 int result_size);
ager@chromium.org9085a012009-05-11 19:22:57 +0000871
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +0000872 MUST_USE_RESULT MaybeObject* TryTailCallRuntime(Runtime::FunctionId fid,
873 int num_arguments,
874 int result_size);
875
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000876 // Jump to a runtime routine.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000877 void JumpToExternalReference(const ExternalReference& ext, int result_size);
ager@chromium.org9085a012009-05-11 19:22:57 +0000878
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +0000879 // Jump to a runtime routine.
880 MaybeObject* TryJumpToExternalReference(const ExternalReference& ext,
881 int result_size);
lrn@chromium.org303ada72010-10-27 09:33:13 +0000882
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +0000883 // 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);
lrn@chromium.org303ada72010-10-27 09:33:13 +0000896
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +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);
ager@chromium.org9085a012009-05-11 19:22:57 +0000917
918 // ---------------------------------------------------------------------------
919 // Utilities
920
921 void Ret();
922
ager@chromium.org9085a012009-05-11 19:22:57 +0000923 Handle<Object> CodeObject() { return code_object_; }
924
925
926 // ---------------------------------------------------------------------------
927 // StatsCounter support
928
929 void SetCounter(StatsCounter* counter, int value);
930 void IncrementCounter(StatsCounter* counter, int value);
931 void DecrementCounter(StatsCounter* counter, int value);
932
933
934 // ---------------------------------------------------------------------------
935 // Debugging
936
937 // Calls Abort(msg) if the condition cc is not satisfied.
938 // Use --debug_code to enable.
939 void Assert(Condition cc, const char* msg);
940
ricow@chromium.org0b9f8502010-08-18 07:45:01 +0000941 void AssertFastElements(Register elements);
942
ager@chromium.org9085a012009-05-11 19:22:57 +0000943 // Like Assert(), but always enabled.
944 void Check(Condition cc, const char* msg);
945
946 // Print a message to stdout and abort execution.
947 void Abort(const char* msg);
948
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000949 // Check that the stack is aligned.
950 void CheckStackAlignment();
951
ager@chromium.org9085a012009-05-11 19:22:57 +0000952 // Verify restrictions about code generated in stubs.
953 void set_generating_stub(bool value) { generating_stub_ = value; }
954 bool generating_stub() { return generating_stub_; }
955 void set_allow_stub_calls(bool value) { allow_stub_calls_ = value; }
956 bool allow_stub_calls() { return allow_stub_calls_; }
957
958 private:
ager@chromium.org0ee099b2011-01-25 14:06:47 +0000959 // Order general registers are pushed by Pushad.
960 // rax, rcx, rdx, rbx, rsi, rdi, r8, r9, r11, r12, r14.
961 static int kSafepointPushRegisterIndices[Register::kNumRegisters];
ager@chromium.org9085a012009-05-11 19:22:57 +0000962 bool generating_stub_;
963 bool allow_stub_calls_;
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000964
965 // Returns a register holding the smi value. The register MUST NOT be
966 // modified. It may be the "smi 1 constant" register.
967 Register GetSmiConstant(Smi* value);
968
969 // Moves the smi value to the destination register.
970 void LoadSmiConstant(Register dst, Smi* value);
971
ager@chromium.org5c838252010-02-19 08:53:10 +0000972 // This handle will be patched with the code object on installation.
973 Handle<Object> code_object_;
ager@chromium.org9085a012009-05-11 19:22:57 +0000974
975 // Helper functions for generating invokes.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000976 template <typename LabelType>
ager@chromium.org9085a012009-05-11 19:22:57 +0000977 void InvokePrologue(const ParameterCount& expected,
978 const ParameterCount& actual,
979 Handle<Code> code_constant,
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000980 Register code_register,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000981 LabelType* done,
ager@chromium.org9085a012009-05-11 19:22:57 +0000982 InvokeFlag flag);
983
ager@chromium.org9085a012009-05-11 19:22:57 +0000984 // Activation support.
985 void EnterFrame(StackFrame::Type type);
986 void LeaveFrame(StackFrame::Type type);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000987
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000988 void EnterExitFramePrologue(bool save_rax);
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +0000989
990 // Allocates arg_stack_space * kPointerSize memory (not GCed) on the stack
991 // accessible via StackSpaceOperand.
ager@chromium.org0ee099b2011-01-25 14:06:47 +0000992 void EnterExitFrameEpilogue(int arg_stack_space, bool save_doubles);
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +0000993
994 void LeaveExitFrameEpilogue();
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000995
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000996 // Allocation support helpers.
ager@chromium.orgac091b72010-05-05 07:34:42 +0000997 // Loads the top of new-space into the result register.
ager@chromium.orgac091b72010-05-05 07:34:42 +0000998 // Otherwise the address of the new-space top is loaded into scratch (if
999 // scratch is valid), and the new-space top is loaded into result.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001000 void LoadAllocationTopHelper(Register result,
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001001 Register scratch,
ager@chromium.orga1645e22009-09-09 19:27:10 +00001002 AllocationFlags flags);
ager@chromium.orgac091b72010-05-05 07:34:42 +00001003 // Update allocation top with value in result_end register.
1004 // If scratch is valid, it contains the address of the allocation top.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001005 void UpdateAllocationTopHelper(Register result_end, Register scratch);
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001006
1007 // Helper for PopHandleScope. Allowed to perform a GC and returns
1008 // NULL if gc_allowed. Does not perform a GC if !gc_allowed, and
1009 // possibly returns a failure object indicating an allocation failure.
1010 Object* PopHandleScopeHelper(Register saved,
1011 Register scratch,
1012 bool gc_allowed);
ager@chromium.org9085a012009-05-11 19:22:57 +00001013};
1014
1015
ager@chromium.org4af710e2009-09-15 12:20:11 +00001016// The code patcher is used to patch (typically) small parts of code e.g. for
1017// debugging and other types of instrumentation. When using the code patcher
1018// the exact number of bytes specified must be emitted. Is not legal to emit
1019// relocation information. If any of these constraints are violated it causes
1020// an assertion.
1021class CodePatcher {
1022 public:
1023 CodePatcher(byte* address, int size);
1024 virtual ~CodePatcher();
1025
1026 // Macro assembler to emit code.
1027 MacroAssembler* masm() { return &masm_; }
1028
1029 private:
1030 byte* address_; // The address of the code being patched.
1031 int size_; // Number of bytes of the expected patch size.
1032 MacroAssembler masm_; // Macro assembler used to generate the code.
1033};
1034
1035
ager@chromium.org9085a012009-05-11 19:22:57 +00001036// -----------------------------------------------------------------------------
1037// Static helper functions.
1038
1039// Generate an Operand for loading a field from an object.
1040static inline Operand FieldOperand(Register object, int offset) {
1041 return Operand(object, offset - kHeapObjectTag);
1042}
1043
1044
1045// Generate an Operand for loading an indexed field from an object.
1046static inline Operand FieldOperand(Register object,
1047 Register index,
1048 ScaleFactor scale,
1049 int offset) {
1050 return Operand(object, index, scale, offset - kHeapObjectTag);
1051}
1052
1053
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00001054static inline Operand ContextOperand(Register context, int index) {
1055 return Operand(context, Context::SlotOffset(index));
1056}
1057
1058
1059static inline Operand GlobalObjectOperand() {
1060 return ContextOperand(rsi, Context::GLOBAL_INDEX);
1061}
1062
1063
1064// Provides access to exit frame stack space (not GCed).
1065static inline Operand StackSpaceOperand(int index) {
1066#ifdef _WIN64
1067 const int kShaddowSpace = 4;
1068 return Operand(rsp, (index + kShaddowSpace) * kPointerSize);
1069#else
1070 return Operand(rsp, index * kPointerSize);
1071#endif
1072}
1073
1074
1075
ager@chromium.org9085a012009-05-11 19:22:57 +00001076#ifdef GENERATED_CODE_COVERAGE
1077extern void LogGeneratedCodeCoverage(const char* file_line);
1078#define CODE_COVERAGE_STRINGIFY(x) #x
1079#define CODE_COVERAGE_TOSTRING(x) CODE_COVERAGE_STRINGIFY(x)
1080#define __FILE_LINE__ __FILE__ ":" CODE_COVERAGE_TOSTRING(__LINE__)
1081#define ACCESS_MASM(masm) { \
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00001082 byte* x64_coverage_function = \
ager@chromium.org9085a012009-05-11 19:22:57 +00001083 reinterpret_cast<byte*>(FUNCTION_ADDR(LogGeneratedCodeCoverage)); \
1084 masm->pushfd(); \
1085 masm->pushad(); \
1086 masm->push(Immediate(reinterpret_cast<int>(&__FILE_LINE__))); \
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00001087 masm->call(x64_coverage_function, RelocInfo::RUNTIME_ENTRY); \
ager@chromium.org9085a012009-05-11 19:22:57 +00001088 masm->pop(rax); \
1089 masm->popad(); \
1090 masm->popfd(); \
1091 } \
1092 masm->
1093#else
1094#define ACCESS_MASM(masm) masm->
1095#endif
1096
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001097// -----------------------------------------------------------------------------
1098// Template implementations.
1099
1100static int kSmiShift = kSmiTagSize + kSmiShiftSize;
1101
1102
1103template <typename LabelType>
1104void MacroAssembler::SmiNeg(Register dst,
1105 Register src,
1106 LabelType* on_smi_result) {
1107 if (dst.is(src)) {
1108 ASSERT(!dst.is(kScratchRegister));
1109 movq(kScratchRegister, src);
1110 neg(dst); // Low 32 bits are retained as zero by negation.
1111 // Test if result is zero or Smi::kMinValue.
1112 cmpq(dst, kScratchRegister);
1113 j(not_equal, on_smi_result);
1114 movq(src, kScratchRegister);
1115 } else {
1116 movq(dst, src);
1117 neg(dst);
1118 cmpq(dst, src);
1119 // If the result is zero or Smi::kMinValue, negation failed to create a smi.
1120 j(not_equal, on_smi_result);
1121 }
1122}
1123
1124
1125template <typename LabelType>
1126void MacroAssembler::SmiAdd(Register dst,
1127 Register src1,
1128 Register src2,
1129 LabelType* on_not_smi_result) {
1130 ASSERT_NOT_NULL(on_not_smi_result);
1131 ASSERT(!dst.is(src2));
1132 if (dst.is(src1)) {
1133 movq(kScratchRegister, src1);
1134 addq(kScratchRegister, src2);
1135 j(overflow, on_not_smi_result);
1136 movq(dst, kScratchRegister);
1137 } else {
1138 movq(dst, src1);
1139 addq(dst, src2);
1140 j(overflow, on_not_smi_result);
1141 }
1142}
1143
1144
1145template <typename LabelType>
1146void MacroAssembler::SmiSub(Register dst,
1147 Register src1,
1148 Register src2,
1149 LabelType* on_not_smi_result) {
1150 ASSERT_NOT_NULL(on_not_smi_result);
1151 ASSERT(!dst.is(src2));
1152 if (dst.is(src1)) {
1153 cmpq(dst, src2);
1154 j(overflow, on_not_smi_result);
1155 subq(dst, src2);
1156 } else {
1157 movq(dst, src1);
1158 subq(dst, src2);
1159 j(overflow, on_not_smi_result);
1160 }
1161}
1162
1163
1164template <typename LabelType>
1165void MacroAssembler::SmiSub(Register dst,
1166 Register src1,
1167 const Operand& src2,
1168 LabelType* on_not_smi_result) {
1169 ASSERT_NOT_NULL(on_not_smi_result);
1170 if (dst.is(src1)) {
1171 movq(kScratchRegister, src2);
1172 cmpq(src1, kScratchRegister);
1173 j(overflow, on_not_smi_result);
1174 subq(src1, kScratchRegister);
1175 } else {
1176 movq(dst, src1);
1177 subq(dst, src2);
1178 j(overflow, on_not_smi_result);
1179 }
1180}
1181
1182
1183template <typename LabelType>
1184void MacroAssembler::SmiMul(Register dst,
1185 Register src1,
1186 Register src2,
1187 LabelType* on_not_smi_result) {
1188 ASSERT(!dst.is(src2));
1189 ASSERT(!dst.is(kScratchRegister));
1190 ASSERT(!src1.is(kScratchRegister));
1191 ASSERT(!src2.is(kScratchRegister));
1192
1193 if (dst.is(src1)) {
1194 NearLabel failure, zero_correct_result;
1195 movq(kScratchRegister, src1); // Create backup for later testing.
1196 SmiToInteger64(dst, src1);
1197 imul(dst, src2);
1198 j(overflow, &failure);
1199
1200 // Check for negative zero result. If product is zero, and one
1201 // argument is negative, go to slow case.
1202 NearLabel correct_result;
1203 testq(dst, dst);
1204 j(not_zero, &correct_result);
1205
1206 movq(dst, kScratchRegister);
1207 xor_(dst, src2);
1208 j(positive, &zero_correct_result); // Result was positive zero.
1209
1210 bind(&failure); // Reused failure exit, restores src1.
1211 movq(src1, kScratchRegister);
1212 jmp(on_not_smi_result);
1213
1214 bind(&zero_correct_result);
lrn@chromium.org5d00b602011-01-05 09:51:43 +00001215 Set(dst, 0);
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001216
1217 bind(&correct_result);
1218 } else {
1219 SmiToInteger64(dst, src1);
1220 imul(dst, src2);
1221 j(overflow, on_not_smi_result);
1222 // Check for negative zero result. If product is zero, and one
1223 // argument is negative, go to slow case.
1224 NearLabel correct_result;
1225 testq(dst, dst);
1226 j(not_zero, &correct_result);
1227 // One of src1 and src2 is zero, the check whether the other is
1228 // negative.
1229 movq(kScratchRegister, src1);
1230 xor_(kScratchRegister, src2);
1231 j(negative, on_not_smi_result);
1232 bind(&correct_result);
1233 }
1234}
1235
1236
1237template <typename LabelType>
1238void MacroAssembler::SmiTryAddConstant(Register dst,
1239 Register src,
1240 Smi* constant,
1241 LabelType* on_not_smi_result) {
1242 // Does not assume that src is a smi.
1243 ASSERT_EQ(static_cast<int>(1), static_cast<int>(kSmiTagMask));
1244 ASSERT_EQ(0, kSmiTag);
1245 ASSERT(!dst.is(kScratchRegister));
1246 ASSERT(!src.is(kScratchRegister));
1247
1248 JumpIfNotSmi(src, on_not_smi_result);
1249 Register tmp = (dst.is(src) ? kScratchRegister : dst);
1250 LoadSmiConstant(tmp, constant);
1251 addq(tmp, src);
1252 j(overflow, on_not_smi_result);
1253 if (dst.is(src)) {
1254 movq(dst, tmp);
1255 }
1256}
1257
1258
1259template <typename LabelType>
1260void MacroAssembler::SmiAddConstant(Register dst,
1261 Register src,
1262 Smi* constant,
1263 LabelType* on_not_smi_result) {
1264 if (constant->value() == 0) {
1265 if (!dst.is(src)) {
1266 movq(dst, src);
1267 }
1268 } else if (dst.is(src)) {
1269 ASSERT(!dst.is(kScratchRegister));
1270
1271 LoadSmiConstant(kScratchRegister, constant);
1272 addq(kScratchRegister, src);
1273 j(overflow, on_not_smi_result);
1274 movq(dst, kScratchRegister);
1275 } else {
1276 LoadSmiConstant(dst, constant);
1277 addq(dst, src);
1278 j(overflow, on_not_smi_result);
1279 }
1280}
1281
1282
1283template <typename LabelType>
1284void MacroAssembler::SmiSubConstant(Register dst,
1285 Register src,
1286 Smi* constant,
1287 LabelType* on_not_smi_result) {
1288 if (constant->value() == 0) {
1289 if (!dst.is(src)) {
1290 movq(dst, src);
1291 }
1292 } else if (dst.is(src)) {
1293 ASSERT(!dst.is(kScratchRegister));
1294 if (constant->value() == Smi::kMinValue) {
1295 // Subtracting min-value from any non-negative value will overflow.
1296 // We test the non-negativeness before doing the subtraction.
1297 testq(src, src);
1298 j(not_sign, on_not_smi_result);
1299 LoadSmiConstant(kScratchRegister, constant);
1300 subq(dst, kScratchRegister);
1301 } else {
1302 // Subtract by adding the negation.
1303 LoadSmiConstant(kScratchRegister, Smi::FromInt(-constant->value()));
1304 addq(kScratchRegister, dst);
1305 j(overflow, on_not_smi_result);
1306 movq(dst, kScratchRegister);
1307 }
1308 } else {
1309 if (constant->value() == Smi::kMinValue) {
1310 // Subtracting min-value from any non-negative value will overflow.
1311 // We test the non-negativeness before doing the subtraction.
1312 testq(src, src);
1313 j(not_sign, on_not_smi_result);
1314 LoadSmiConstant(dst, constant);
1315 // Adding and subtracting the min-value gives the same result, it only
1316 // differs on the overflow bit, which we don't check here.
1317 addq(dst, src);
1318 } else {
1319 // Subtract by adding the negation.
1320 LoadSmiConstant(dst, Smi::FromInt(-(constant->value())));
1321 addq(dst, src);
1322 j(overflow, on_not_smi_result);
1323 }
1324 }
1325}
1326
1327
1328template <typename LabelType>
1329void MacroAssembler::SmiDiv(Register dst,
1330 Register src1,
1331 Register src2,
1332 LabelType* on_not_smi_result) {
1333 ASSERT(!src1.is(kScratchRegister));
1334 ASSERT(!src2.is(kScratchRegister));
1335 ASSERT(!dst.is(kScratchRegister));
1336 ASSERT(!src2.is(rax));
1337 ASSERT(!src2.is(rdx));
1338 ASSERT(!src1.is(rdx));
1339
1340 // Check for 0 divisor (result is +/-Infinity).
1341 NearLabel positive_divisor;
1342 testq(src2, src2);
1343 j(zero, on_not_smi_result);
1344
1345 if (src1.is(rax)) {
1346 movq(kScratchRegister, src1);
1347 }
1348 SmiToInteger32(rax, src1);
1349 // We need to rule out dividing Smi::kMinValue by -1, since that would
1350 // overflow in idiv and raise an exception.
1351 // We combine this with negative zero test (negative zero only happens
1352 // when dividing zero by a negative number).
1353
1354 // We overshoot a little and go to slow case if we divide min-value
1355 // by any negative value, not just -1.
1356 NearLabel safe_div;
1357 testl(rax, Immediate(0x7fffffff));
1358 j(not_zero, &safe_div);
1359 testq(src2, src2);
1360 if (src1.is(rax)) {
1361 j(positive, &safe_div);
1362 movq(src1, kScratchRegister);
1363 jmp(on_not_smi_result);
1364 } else {
1365 j(negative, on_not_smi_result);
1366 }
1367 bind(&safe_div);
1368
1369 SmiToInteger32(src2, src2);
1370 // Sign extend src1 into edx:eax.
1371 cdq();
1372 idivl(src2);
1373 Integer32ToSmi(src2, src2);
1374 // Check that the remainder is zero.
1375 testl(rdx, rdx);
1376 if (src1.is(rax)) {
1377 NearLabel smi_result;
1378 j(zero, &smi_result);
1379 movq(src1, kScratchRegister);
1380 jmp(on_not_smi_result);
1381 bind(&smi_result);
1382 } else {
1383 j(not_zero, on_not_smi_result);
1384 }
1385 if (!dst.is(src1) && src1.is(rax)) {
1386 movq(src1, kScratchRegister);
1387 }
1388 Integer32ToSmi(dst, rax);
1389}
1390
1391
1392template <typename LabelType>
1393void MacroAssembler::SmiMod(Register dst,
1394 Register src1,
1395 Register src2,
1396 LabelType* on_not_smi_result) {
1397 ASSERT(!dst.is(kScratchRegister));
1398 ASSERT(!src1.is(kScratchRegister));
1399 ASSERT(!src2.is(kScratchRegister));
1400 ASSERT(!src2.is(rax));
1401 ASSERT(!src2.is(rdx));
1402 ASSERT(!src1.is(rdx));
1403 ASSERT(!src1.is(src2));
1404
1405 testq(src2, src2);
1406 j(zero, on_not_smi_result);
1407
1408 if (src1.is(rax)) {
1409 movq(kScratchRegister, src1);
1410 }
1411 SmiToInteger32(rax, src1);
1412 SmiToInteger32(src2, src2);
1413
1414 // Test for the edge case of dividing Smi::kMinValue by -1 (will overflow).
1415 NearLabel safe_div;
1416 cmpl(rax, Immediate(Smi::kMinValue));
1417 j(not_equal, &safe_div);
1418 cmpl(src2, Immediate(-1));
1419 j(not_equal, &safe_div);
1420 // Retag inputs and go slow case.
1421 Integer32ToSmi(src2, src2);
1422 if (src1.is(rax)) {
1423 movq(src1, kScratchRegister);
1424 }
1425 jmp(on_not_smi_result);
1426 bind(&safe_div);
1427
1428 // Sign extend eax into edx:eax.
1429 cdq();
1430 idivl(src2);
1431 // Restore smi tags on inputs.
1432 Integer32ToSmi(src2, src2);
1433 if (src1.is(rax)) {
1434 movq(src1, kScratchRegister);
1435 }
1436 // Check for a negative zero result. If the result is zero, and the
1437 // dividend is negative, go slow to return a floating point negative zero.
1438 NearLabel smi_result;
1439 testl(rdx, rdx);
1440 j(not_zero, &smi_result);
1441 testq(src1, src1);
1442 j(negative, on_not_smi_result);
1443 bind(&smi_result);
1444 Integer32ToSmi(dst, rdx);
1445}
1446
1447
1448template <typename LabelType>
1449void MacroAssembler::SmiShiftLogicalRightConstant(
1450 Register dst, Register src, int shift_value, LabelType* on_not_smi_result) {
1451 // Logic right shift interprets its result as an *unsigned* number.
1452 if (dst.is(src)) {
1453 UNIMPLEMENTED(); // Not used.
1454 } else {
1455 movq(dst, src);
1456 if (shift_value == 0) {
1457 testq(dst, dst);
1458 j(negative, on_not_smi_result);
1459 }
1460 shr(dst, Immediate(shift_value + kSmiShift));
1461 shl(dst, Immediate(kSmiShift));
1462 }
1463}
1464
1465
1466template <typename LabelType>
1467void MacroAssembler::SmiShiftLogicalRight(Register dst,
1468 Register src1,
1469 Register src2,
1470 LabelType* on_not_smi_result) {
1471 ASSERT(!dst.is(kScratchRegister));
1472 ASSERT(!src1.is(kScratchRegister));
1473 ASSERT(!src2.is(kScratchRegister));
1474 ASSERT(!dst.is(rcx));
ricow@chromium.org83aa5492011-02-07 12:42:56 +00001475 // dst and src1 can be the same, because the one case that bails out
1476 // is a shift by 0, which leaves dst, and therefore src1, unchanged.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001477 NearLabel result_ok;
1478 if (src1.is(rcx) || src2.is(rcx)) {
1479 movq(kScratchRegister, rcx);
1480 }
1481 if (!dst.is(src1)) {
1482 movq(dst, src1);
1483 }
1484 SmiToInteger32(rcx, src2);
1485 orl(rcx, Immediate(kSmiShift));
1486 shr_cl(dst); // Shift is rcx modulo 0x1f + 32.
1487 shl(dst, Immediate(kSmiShift));
1488 testq(dst, dst);
1489 if (src1.is(rcx) || src2.is(rcx)) {
1490 NearLabel positive_result;
1491 j(positive, &positive_result);
1492 if (src1.is(rcx)) {
1493 movq(src1, kScratchRegister);
1494 } else {
1495 movq(src2, kScratchRegister);
1496 }
1497 jmp(on_not_smi_result);
1498 bind(&positive_result);
1499 } else {
1500 j(negative, on_not_smi_result); // src2 was zero and src1 negative.
1501 }
1502}
1503
1504
1505template <typename LabelType>
1506void MacroAssembler::SelectNonSmi(Register dst,
1507 Register src1,
1508 Register src2,
1509 LabelType* on_not_smis) {
1510 ASSERT(!dst.is(kScratchRegister));
1511 ASSERT(!src1.is(kScratchRegister));
1512 ASSERT(!src2.is(kScratchRegister));
1513 ASSERT(!dst.is(src1));
1514 ASSERT(!dst.is(src2));
1515 // Both operands must not be smis.
1516#ifdef DEBUG
1517 if (allow_stub_calls()) { // Check contains a stub call.
1518 Condition not_both_smis = NegateCondition(CheckBothSmi(src1, src2));
1519 Check(not_both_smis, "Both registers were smis in SelectNonSmi.");
1520 }
1521#endif
1522 ASSERT_EQ(0, kSmiTag);
1523 ASSERT_EQ(0, Smi::FromInt(0));
1524 movl(kScratchRegister, Immediate(kSmiTagMask));
1525 and_(kScratchRegister, src1);
1526 testl(kScratchRegister, src2);
1527 // If non-zero then both are smis.
1528 j(not_zero, on_not_smis);
1529
1530 // Exactly one operand is a smi.
1531 ASSERT_EQ(1, static_cast<int>(kSmiTagMask));
1532 // kScratchRegister still holds src1 & kSmiTag, which is either zero or one.
1533 subq(kScratchRegister, Immediate(1));
1534 // If src1 is a smi, then scratch register all 1s, else it is all 0s.
1535 movq(dst, src1);
1536 xor_(dst, src2);
1537 and_(dst, kScratchRegister);
1538 // If src1 is a smi, dst holds src1 ^ src2, else it is zero.
1539 xor_(dst, src1);
1540 // If src1 is a smi, dst is src2, else it is src1, i.e., the non-smi.
1541}
1542
1543
1544template <typename LabelType>
1545void MacroAssembler::JumpIfSmi(Register src, LabelType* on_smi) {
1546 ASSERT_EQ(0, kSmiTag);
1547 Condition smi = CheckSmi(src);
1548 j(smi, on_smi);
1549}
1550
1551
1552template <typename LabelType>
1553void MacroAssembler::JumpIfNotSmi(Register src, LabelType* on_not_smi) {
1554 Condition smi = CheckSmi(src);
1555 j(NegateCondition(smi), on_not_smi);
1556}
1557
1558
1559template <typename LabelType>
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +00001560void MacroAssembler::JumpUnlessNonNegativeSmi(
1561 Register src, LabelType* on_not_smi_or_negative) {
1562 Condition non_negative_smi = CheckNonNegativeSmi(src);
1563 j(NegateCondition(non_negative_smi), on_not_smi_or_negative);
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001564}
1565
1566
1567template <typename LabelType>
1568void MacroAssembler::JumpIfSmiEqualsConstant(Register src,
1569 Smi* constant,
1570 LabelType* on_equals) {
1571 SmiCompare(src, constant);
1572 j(equal, on_equals);
1573}
1574
1575
1576template <typename LabelType>
1577void MacroAssembler::JumpIfNotValidSmiValue(Register src,
1578 LabelType* on_invalid) {
1579 Condition is_valid = CheckInteger32ValidSmiValue(src);
1580 j(NegateCondition(is_valid), on_invalid);
1581}
1582
1583
1584template <typename LabelType>
1585void MacroAssembler::JumpIfUIntNotValidSmiValue(Register src,
1586 LabelType* on_invalid) {
1587 Condition is_valid = CheckUInteger32ValidSmiValue(src);
1588 j(NegateCondition(is_valid), on_invalid);
1589}
1590
1591
1592template <typename LabelType>
1593void MacroAssembler::JumpIfNotBothSmi(Register src1,
1594 Register src2,
1595 LabelType* on_not_both_smi) {
1596 Condition both_smi = CheckBothSmi(src1, src2);
1597 j(NegateCondition(both_smi), on_not_both_smi);
1598}
1599
1600
1601template <typename LabelType>
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +00001602void MacroAssembler::JumpUnlessBothNonNegativeSmi(Register src1,
1603 Register src2,
1604 LabelType* on_not_both_smi) {
1605 Condition both_smi = CheckBothNonNegativeSmi(src1, src2);
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001606 j(NegateCondition(both_smi), on_not_both_smi);
1607}
1608
1609
1610template <typename LabelType>
ricow@chromium.org83aa5492011-02-07 12:42:56 +00001611void MacroAssembler::JumpIfNotString(Register object,
1612 Register object_map,
1613 LabelType* not_string) {
1614 Condition is_smi = CheckSmi(object);
1615 j(is_smi, not_string);
1616 CmpObjectType(object, FIRST_NONSTRING_TYPE, object_map);
1617 j(above_equal, not_string);
1618}
1619
1620
1621template <typename LabelType>
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001622void MacroAssembler::JumpIfNotBothSequentialAsciiStrings(Register first_object,
1623 Register second_object,
1624 Register scratch1,
1625 Register scratch2,
1626 LabelType* on_fail) {
1627 // Check that both objects are not smis.
1628 Condition either_smi = CheckEitherSmi(first_object, second_object);
1629 j(either_smi, on_fail);
1630
1631 // Load instance type for both strings.
1632 movq(scratch1, FieldOperand(first_object, HeapObject::kMapOffset));
1633 movq(scratch2, FieldOperand(second_object, HeapObject::kMapOffset));
1634 movzxbl(scratch1, FieldOperand(scratch1, Map::kInstanceTypeOffset));
1635 movzxbl(scratch2, FieldOperand(scratch2, Map::kInstanceTypeOffset));
1636
1637 // Check that both are flat ascii strings.
1638 ASSERT(kNotStringTag != 0);
1639 const int kFlatAsciiStringMask =
1640 kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask;
1641 const int kFlatAsciiStringTag = ASCII_STRING_TYPE;
1642
1643 andl(scratch1, Immediate(kFlatAsciiStringMask));
1644 andl(scratch2, Immediate(kFlatAsciiStringMask));
1645 // Interleave the bits to check both scratch1 and scratch2 in one test.
1646 ASSERT_EQ(0, kFlatAsciiStringMask & (kFlatAsciiStringMask << 3));
1647 lea(scratch1, Operand(scratch1, scratch2, times_8, 0));
1648 cmpl(scratch1,
1649 Immediate(kFlatAsciiStringTag + (kFlatAsciiStringTag << 3)));
1650 j(not_equal, on_fail);
1651}
1652
1653
1654template <typename LabelType>
1655void MacroAssembler::JumpIfInstanceTypeIsNotSequentialAscii(
1656 Register instance_type,
1657 Register scratch,
1658 LabelType *failure) {
1659 if (!scratch.is(instance_type)) {
1660 movl(scratch, instance_type);
1661 }
1662
1663 const int kFlatAsciiStringMask =
1664 kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask;
1665
1666 andl(scratch, Immediate(kFlatAsciiStringMask));
1667 cmpl(scratch, Immediate(kStringTag | kSeqStringTag | kAsciiStringTag));
1668 j(not_equal, failure);
1669}
1670
1671
1672template <typename LabelType>
1673void MacroAssembler::JumpIfBothInstanceTypesAreNotSequentialAscii(
1674 Register first_object_instance_type,
1675 Register second_object_instance_type,
1676 Register scratch1,
1677 Register scratch2,
1678 LabelType* on_fail) {
1679 // Load instance type for both strings.
1680 movq(scratch1, first_object_instance_type);
1681 movq(scratch2, second_object_instance_type);
1682
1683 // Check that both are flat ascii strings.
1684 ASSERT(kNotStringTag != 0);
1685 const int kFlatAsciiStringMask =
1686 kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask;
1687 const int kFlatAsciiStringTag = ASCII_STRING_TYPE;
1688
1689 andl(scratch1, Immediate(kFlatAsciiStringMask));
1690 andl(scratch2, Immediate(kFlatAsciiStringMask));
1691 // Interleave the bits to check both scratch1 and scratch2 in one test.
1692 ASSERT_EQ(0, kFlatAsciiStringMask & (kFlatAsciiStringMask << 3));
1693 lea(scratch1, Operand(scratch1, scratch2, times_8, 0));
1694 cmpl(scratch1,
1695 Immediate(kFlatAsciiStringTag + (kFlatAsciiStringTag << 3)));
1696 j(not_equal, on_fail);
1697}
1698
1699
1700template <typename LabelType>
1701void MacroAssembler::InNewSpace(Register object,
1702 Register scratch,
1703 Condition cc,
1704 LabelType* branch) {
1705 if (Serializer::enabled()) {
1706 // Can't do arithmetic on external references if it might get serialized.
1707 // The mask isn't really an address. We load it as an external reference in
1708 // case the size of the new space is different between the snapshot maker
1709 // and the running system.
1710 if (scratch.is(object)) {
1711 movq(kScratchRegister, ExternalReference::new_space_mask());
1712 and_(scratch, kScratchRegister);
1713 } else {
1714 movq(scratch, ExternalReference::new_space_mask());
1715 and_(scratch, object);
1716 }
1717 movq(kScratchRegister, ExternalReference::new_space_start());
1718 cmpq(scratch, kScratchRegister);
1719 j(cc, branch);
1720 } else {
1721 ASSERT(is_int32(static_cast<int64_t>(Heap::NewSpaceMask())));
1722 intptr_t new_space_start =
1723 reinterpret_cast<intptr_t>(Heap::NewSpaceStart());
1724 movq(kScratchRegister, -new_space_start, RelocInfo::NONE);
1725 if (scratch.is(object)) {
1726 addq(scratch, kScratchRegister);
1727 } else {
1728 lea(scratch, Operand(object, kScratchRegister, times_1, 0));
1729 }
1730 and_(scratch, Immediate(static_cast<int32_t>(Heap::NewSpaceMask())));
1731 j(cc, branch);
1732 }
1733}
1734
1735
1736template <typename LabelType>
1737void MacroAssembler::InvokePrologue(const ParameterCount& expected,
1738 const ParameterCount& actual,
1739 Handle<Code> code_constant,
1740 Register code_register,
1741 LabelType* done,
1742 InvokeFlag flag) {
1743 bool definitely_matches = false;
1744 NearLabel invoke;
1745 if (expected.is_immediate()) {
1746 ASSERT(actual.is_immediate());
1747 if (expected.immediate() == actual.immediate()) {
1748 definitely_matches = true;
1749 } else {
1750 Set(rax, actual.immediate());
1751 if (expected.immediate() ==
1752 SharedFunctionInfo::kDontAdaptArgumentsSentinel) {
1753 // Don't worry about adapting arguments for built-ins that
1754 // don't want that done. Skip adaption code by making it look
1755 // like we have a match between expected and actual number of
1756 // arguments.
1757 definitely_matches = true;
1758 } else {
1759 Set(rbx, expected.immediate());
1760 }
1761 }
1762 } else {
1763 if (actual.is_immediate()) {
1764 // Expected is in register, actual is immediate. This is the
1765 // case when we invoke function values without going through the
1766 // IC mechanism.
1767 cmpq(expected.reg(), Immediate(actual.immediate()));
1768 j(equal, &invoke);
1769 ASSERT(expected.reg().is(rbx));
1770 Set(rax, actual.immediate());
1771 } else if (!expected.reg().is(actual.reg())) {
1772 // Both expected and actual are in (different) registers. This
1773 // is the case when we invoke functions using call and apply.
1774 cmpq(expected.reg(), actual.reg());
1775 j(equal, &invoke);
1776 ASSERT(actual.reg().is(rax));
1777 ASSERT(expected.reg().is(rbx));
1778 }
1779 }
1780
1781 if (!definitely_matches) {
1782 Handle<Code> adaptor =
1783 Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline));
1784 if (!code_constant.is_null()) {
1785 movq(rdx, code_constant, RelocInfo::EMBEDDED_OBJECT);
1786 addq(rdx, Immediate(Code::kHeaderSize - kHeapObjectTag));
1787 } else if (!code_register.is(rdx)) {
1788 movq(rdx, code_register);
1789 }
1790
1791 if (flag == CALL_FUNCTION) {
1792 Call(adaptor, RelocInfo::CODE_TARGET);
1793 jmp(done);
1794 } else {
1795 Jump(adaptor, RelocInfo::CODE_TARGET);
1796 }
1797 bind(&invoke);
1798 }
1799}
1800
ager@chromium.org9085a012009-05-11 19:22:57 +00001801
1802} } // namespace v8::internal
1803
1804#endif // V8_X64_MACRO_ASSEMBLER_X64_H_