blob: 35369118751d8ff4faca4577305cd0f101e9a296 [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);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +000077 void CompareRoot(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.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000543 template <typename LabelType>
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000544 void JumpIfNotBothSequentialAsciiStrings(Register first_object,
545 Register second_object,
546 Register scratch1,
547 Register scratch2,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000548 LabelType* on_not_both_flat_ascii);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000549
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000550 // Check whether the instance type represents a flat ascii string. Jump to the
551 // label if not. If the instance type can be scratched specify same register
552 // for both instance type and scratch.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000553 template <typename LabelType>
554 void JumpIfInstanceTypeIsNotSequentialAscii(
555 Register instance_type,
556 Register scratch,
557 LabelType *on_not_flat_ascii_string);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000558
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000559 template <typename LabelType>
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000560 void JumpIfBothInstanceTypesAreNotSequentialAscii(
561 Register first_object_instance_type,
562 Register second_object_instance_type,
563 Register scratch1,
564 Register scratch2,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000565 LabelType* on_fail);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000566
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000567 // ---------------------------------------------------------------------------
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000568 // Macro instructions.
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000569
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000570 // Load a register with a long value as efficiently as possible.
ager@chromium.orge2902be2009-06-08 12:21:35 +0000571 void Set(Register dst, int64_t x);
572 void Set(const Operand& dst, int64_t x);
ager@chromium.org9085a012009-05-11 19:22:57 +0000573
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000574 // Move if the registers are not identical.
575 void Move(Register target, Register source);
576
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000577 // Handle support
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000578 void Move(Register dst, Handle<Object> source);
579 void Move(const Operand& dst, Handle<Object> source);
580 void Cmp(Register dst, Handle<Object> source);
ager@chromium.org3e875802009-06-29 08:26:34 +0000581 void Cmp(const Operand& dst, Handle<Object> source);
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000582 void Push(Handle<Object> source);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000583
584 // Emit code to discard a non-negative number of pointer-sized elements
585 // from the stack, clobbering only the rsp register.
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000586 void Drop(int stack_elements);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000587
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000588 void Call(Label* target) { call(target); }
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000589
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000590 // Control Flow
591 void Jump(Address destination, RelocInfo::Mode rmode);
592 void Jump(ExternalReference ext);
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000593 void Jump(Handle<Code> code_object, RelocInfo::Mode rmode);
594
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000595 void Call(Address destination, RelocInfo::Mode rmode);
596 void Call(ExternalReference ext);
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000597 void Call(Handle<Code> code_object, RelocInfo::Mode rmode);
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000598
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000599 // Non-x64 instructions.
600 // Push/pop all general purpose registers.
601 // Does not push rsp/rbp nor any of the assembler's special purpose registers
602 // (kScratchRegister, kSmiConstantRegister, kRootRegister).
603 void Pushad();
604 void Popad();
ager@chromium.org0ee099b2011-01-25 14:06:47 +0000605 // Sets the stack as after performing Popad, without actually loading the
606 // registers.
607 void Dropad();
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000608
ager@chromium.org9085a012009-05-11 19:22:57 +0000609 // Compare object type for heap object.
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000610 // Always use unsigned comparisons: above and below, not less and greater.
ager@chromium.org9085a012009-05-11 19:22:57 +0000611 // Incoming register is heap_object and outgoing register is map.
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000612 // They may be the same register, and may be kScratchRegister.
ager@chromium.org9085a012009-05-11 19:22:57 +0000613 void CmpObjectType(Register heap_object, InstanceType type, Register map);
614
615 // Compare instance type for map.
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000616 // Always use unsigned comparisons: above and below, not less and greater.
ager@chromium.org9085a012009-05-11 19:22:57 +0000617 void CmpInstanceType(Register map, InstanceType type);
618
ager@chromium.org5c838252010-02-19 08:53:10 +0000619 // Check if the map of an object is equal to a specified map and
620 // branch to label if not. Skip the smi check if not required
621 // (object is known to be a heap object)
622 void CheckMap(Register obj,
623 Handle<Map> map,
624 Label* fail,
625 bool is_heap_object);
626
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000627 // Check if the object in register heap_object is a string. Afterwards the
628 // register map contains the object map and the register instance_type
629 // contains the instance_type. The registers map and instance_type can be the
630 // same in which case it contains the instance type afterwards. Either of the
631 // registers map and instance_type can be the same as heap_object.
632 Condition IsObjectStringType(Register heap_object,
633 Register map,
634 Register instance_type);
635
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000636 // FCmp compares and pops the two values on top of the FPU stack.
637 // The flag results are similar to integer cmp, but requires unsigned
ager@chromium.org9085a012009-05-11 19:22:57 +0000638 // jcc instructions (je, ja, jae, jb, jbe, je, and jz).
639 void FCmp();
640
ager@chromium.org5c838252010-02-19 08:53:10 +0000641 // Abort execution if argument is not a number. Used in debug code.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000642 void AbortIfNotNumber(Register object);
ager@chromium.org5c838252010-02-19 08:53:10 +0000643
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000644 // Abort execution if argument is a smi. Used in debug code.
645 void AbortIfSmi(Register object);
646
lrn@chromium.org25156de2010-04-06 13:10:27 +0000647 // Abort execution if argument is not a smi. Used in debug code.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000648 void AbortIfNotSmi(Register object);
lrn@chromium.org25156de2010-04-06 13:10:27 +0000649
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000650 // Abort execution if argument is not the root value with the given index.
651 void AbortIfNotRootValue(Register src,
652 Heap::RootListIndex root_value_index,
653 const char* message);
654
ager@chromium.org9085a012009-05-11 19:22:57 +0000655 // ---------------------------------------------------------------------------
656 // Exception handling
657
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000658 // Push a new try handler and link into try handler chain. The return
659 // address must be pushed before calling this helper.
ager@chromium.org9085a012009-05-11 19:22:57 +0000660 void PushTryHandler(CodeLocation try_location, HandlerType type);
661
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000662 // Unlink the stack handler on top of the stack from the try handler chain.
663 void PopTryHandler();
ager@chromium.org9085a012009-05-11 19:22:57 +0000664
665 // ---------------------------------------------------------------------------
666 // Inline caching support
667
ager@chromium.org9085a012009-05-11 19:22:57 +0000668 // Generate code for checking access rights - used for security checks
669 // on access to global objects across environments. The holder register
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000670 // is left untouched, but the scratch register and kScratchRegister,
671 // which must be different, are clobbered.
ager@chromium.org9085a012009-05-11 19:22:57 +0000672 void CheckAccessGlobalProxy(Register holder_reg,
673 Register scratch,
674 Label* miss);
675
676
677 // ---------------------------------------------------------------------------
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000678 // Allocation support
679
680 // Allocate an object in new space. If the new space is exhausted control
681 // continues at the gc_required label. The allocated object is returned in
682 // result and end of the new object is returned in result_end. The register
683 // scratch can be passed as no_reg in which case an additional object
684 // reference will be added to the reloc info. The returned pointers in result
685 // and result_end have not yet been tagged as heap objects. If
686 // result_contains_top_on_entry is true the content of result is known to be
687 // the allocation top on entry (could be result_end from a previous call to
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000688 // AllocateInNewSpace). If result_contains_top_on_entry is true scratch
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000689 // should be no_reg as it is never used.
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000690 void AllocateInNewSpace(int object_size,
691 Register result,
692 Register result_end,
693 Register scratch,
694 Label* gc_required,
695 AllocationFlags flags);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000696
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000697 void AllocateInNewSpace(int header_size,
698 ScaleFactor element_size,
699 Register element_count,
700 Register result,
701 Register result_end,
702 Register scratch,
703 Label* gc_required,
704 AllocationFlags flags);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000705
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000706 void AllocateInNewSpace(Register object_size,
707 Register result,
708 Register result_end,
709 Register scratch,
710 Label* gc_required,
711 AllocationFlags flags);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000712
713 // Undo allocation in new space. The object passed and objects allocated after
714 // it will no longer be allocated. Make sure that no pointers are left to the
715 // object(s) no longer allocated as they would be invalid when allocation is
716 // un-done.
717 void UndoAllocationInNewSpace(Register object);
718
ager@chromium.org3811b432009-10-28 14:53:37 +0000719 // Allocate a heap number in new space with undefined value. Returns
720 // tagged pointer in result register, or jumps to gc_required if new
721 // space is full.
722 void AllocateHeapNumber(Register result,
723 Register scratch,
724 Label* gc_required);
725
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000726 // Allocate a sequential string. All the header fields of the string object
727 // are initialized.
728 void AllocateTwoByteString(Register result,
729 Register length,
730 Register scratch1,
731 Register scratch2,
732 Register scratch3,
733 Label* gc_required);
734 void AllocateAsciiString(Register result,
735 Register length,
736 Register scratch1,
737 Register scratch2,
738 Register scratch3,
739 Label* gc_required);
740
741 // Allocate a raw cons string object. Only the map field of the result is
742 // initialized.
743 void AllocateConsString(Register result,
744 Register scratch1,
745 Register scratch2,
746 Label* gc_required);
747 void AllocateAsciiConsString(Register result,
748 Register scratch1,
749 Register scratch2,
750 Label* gc_required);
751
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000752 // ---------------------------------------------------------------------------
ager@chromium.org9085a012009-05-11 19:22:57 +0000753 // Support functions.
754
755 // Check if result is zero and op is negative.
756 void NegativeZeroTest(Register result, Register op, Label* then_label);
757
758 // Check if result is zero and op is negative in code using jump targets.
759 void NegativeZeroTest(CodeGenerator* cgen,
760 Register result,
761 Register op,
762 JumpTarget* then_target);
763
764 // Check if result is zero and any of op1 and op2 are negative.
765 // Register scratch is destroyed, and it must be different from op2.
766 void NegativeZeroTest(Register result, Register op1, Register op2,
767 Register scratch, Label* then_label);
768
769 // Try to get function prototype of a function and puts the value in
770 // the result register. Checks that the function really is a
771 // function and jumps to the miss label if the fast checks fail. The
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000772 // function register will be untouched; the other register may be
ager@chromium.org9085a012009-05-11 19:22:57 +0000773 // clobbered.
774 void TryGetFunctionPrototype(Register function,
775 Register result,
ager@chromium.org9085a012009-05-11 19:22:57 +0000776 Label* miss);
777
778 // Generates code for reporting that an illegal operation has
779 // occurred.
780 void IllegalOperation(int num_arguments);
781
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000782 // Picks out an array index from the hash field.
783 // Register use:
784 // hash - holds the index's hash. Clobbered.
785 // index - holds the overwritten index on exit.
786 void IndexFromHash(Register hash, Register index);
787
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000788 // Find the function context up the context chain.
789 void LoadContext(Register dst, int context_chain_length);
790
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000791 // Load the global function with the given index.
792 void LoadGlobalFunction(int index, Register function);
793
794 // Load the initial map from the global function. The registers
795 // function and map can be the same.
796 void LoadGlobalFunctionInitialMap(Register function, Register map);
797
ager@chromium.org9085a012009-05-11 19:22:57 +0000798 // ---------------------------------------------------------------------------
799 // Runtime calls
800
801 // Call a code stub.
802 void CallStub(CodeStub* stub);
803
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000804 // Call a code stub and return the code object called. Try to generate
805 // the code if necessary. Do not perform a GC but instead return a retry
806 // after GC failure.
lrn@chromium.org303ada72010-10-27 09:33:13 +0000807 MUST_USE_RESULT MaybeObject* TryCallStub(CodeStub* stub);
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000808
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000809 // Tail call a code stub (jump).
810 void TailCallStub(CodeStub* stub);
811
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000812 // Tail call a code stub (jump) and return the code object called. Try to
813 // generate the code if necessary. Do not perform a GC but instead return
814 // a retry after GC failure.
lrn@chromium.org303ada72010-10-27 09:33:13 +0000815 MUST_USE_RESULT MaybeObject* TryTailCallStub(CodeStub* stub);
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000816
ager@chromium.org9085a012009-05-11 19:22:57 +0000817 // Return from a code stub after popping its arguments.
818 void StubReturn(int argc);
819
820 // Call a runtime routine.
ager@chromium.org9085a012009-05-11 19:22:57 +0000821 void CallRuntime(Runtime::Function* f, int num_arguments);
822
ager@chromium.org0ee099b2011-01-25 14:06:47 +0000823 // Call a runtime function and save the value of XMM registers.
824 void CallRuntimeSaveDoubles(Runtime::FunctionId id);
825
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000826 // Call a runtime function, returning the CodeStub object called.
827 // Try to generate the stub code if necessary. Do not perform a GC
828 // but instead return a retry after GC failure.
lrn@chromium.org303ada72010-10-27 09:33:13 +0000829 MUST_USE_RESULT MaybeObject* TryCallRuntime(Runtime::Function* f,
830 int num_arguments);
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000831
ager@chromium.org9085a012009-05-11 19:22:57 +0000832 // Convenience function: Same as above, but takes the fid instead.
833 void CallRuntime(Runtime::FunctionId id, int num_arguments);
834
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000835 // Convenience function: Same as above, but takes the fid instead.
lrn@chromium.org303ada72010-10-27 09:33:13 +0000836 MUST_USE_RESULT MaybeObject* TryCallRuntime(Runtime::FunctionId id,
837 int num_arguments);
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000838
ager@chromium.org5c838252010-02-19 08:53:10 +0000839 // Convenience function: call an external reference.
840 void CallExternalReference(const ExternalReference& ext,
841 int num_arguments);
842
ager@chromium.org9085a012009-05-11 19:22:57 +0000843 // Tail call of a runtime routine (jump).
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000844 // Like JumpToExternalReference, but also takes care of passing the number
845 // of parameters.
846 void TailCallExternalReference(const ExternalReference& ext,
847 int num_arguments,
848 int result_size);
849
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +0000850 MUST_USE_RESULT MaybeObject* TryTailCallExternalReference(
851 const ExternalReference& ext, int num_arguments, int result_size);
852
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000853 // Convenience function: tail call a runtime routine (jump).
854 void TailCallRuntime(Runtime::FunctionId fid,
ager@chromium.orga1645e22009-09-09 19:27:10 +0000855 int num_arguments,
856 int result_size);
ager@chromium.org9085a012009-05-11 19:22:57 +0000857
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +0000858 MUST_USE_RESULT MaybeObject* TryTailCallRuntime(Runtime::FunctionId fid,
859 int num_arguments,
860 int result_size);
861
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000862 // Jump to a runtime routine.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000863 void JumpToExternalReference(const ExternalReference& ext, int result_size);
ager@chromium.org9085a012009-05-11 19:22:57 +0000864
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +0000865 // Jump to a runtime routine.
866 MaybeObject* TryJumpToExternalReference(const ExternalReference& ext,
867 int result_size);
lrn@chromium.org303ada72010-10-27 09:33:13 +0000868
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +0000869 // Prepares stack to put arguments (aligns and so on).
870 // WIN64 calling convention requires to put the pointer to the return value
871 // slot into rcx (rcx must be preserverd until TryCallApiFunctionAndReturn).
872 // Saves context (rsi). Clobbers rax. Allocates arg_stack_space * kPointerSize
873 // inside the exit frame (not GCed) accessible via StackSpaceOperand.
874 void PrepareCallApiFunction(int arg_stack_space);
875
876 // Calls an API function. Allocates HandleScope, extracts
877 // returned value from handle and propagates exceptions.
878 // Clobbers r12, r14, rbx and caller-save registers. Restores context.
879 // On return removes stack_space * kPointerSize (GCed).
880 MUST_USE_RESULT MaybeObject* TryCallApiFunctionAndReturn(
881 ApiFunction* function, int stack_space);
lrn@chromium.org303ada72010-10-27 09:33:13 +0000882
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000883 // Before calling a C-function from generated code, align arguments on stack.
884 // After aligning the frame, arguments must be stored in esp[0], esp[4],
885 // etc., not pushed. The argument count assumes all arguments are word sized.
886 // The number of slots reserved for arguments depends on platform. On Windows
887 // stack slots are reserved for the arguments passed in registers. On other
888 // platforms stack slots are only reserved for the arguments actually passed
889 // on the stack.
890 void PrepareCallCFunction(int num_arguments);
891
892 // Calls a C function and cleans up the space for arguments allocated
893 // by PrepareCallCFunction. The called function is not allowed to trigger a
894 // garbage collection, since that might move the code and invalidate the
895 // return address (unless this is somehow accounted for by the called
896 // function).
897 void CallCFunction(ExternalReference function, int num_arguments);
898 void CallCFunction(Register function, int num_arguments);
899
900 // Calculate the number of stack slots to reserve for arguments when calling a
901 // C function.
902 int ArgumentStackSlotsForCFunctionCall(int num_arguments);
ager@chromium.org9085a012009-05-11 19:22:57 +0000903
904 // ---------------------------------------------------------------------------
905 // Utilities
906
907 void Ret();
908
ager@chromium.org9085a012009-05-11 19:22:57 +0000909 Handle<Object> CodeObject() { return code_object_; }
910
911
912 // ---------------------------------------------------------------------------
913 // StatsCounter support
914
915 void SetCounter(StatsCounter* counter, int value);
916 void IncrementCounter(StatsCounter* counter, int value);
917 void DecrementCounter(StatsCounter* counter, int value);
918
919
920 // ---------------------------------------------------------------------------
921 // Debugging
922
923 // Calls Abort(msg) if the condition cc is not satisfied.
924 // Use --debug_code to enable.
925 void Assert(Condition cc, const char* msg);
926
ricow@chromium.org0b9f8502010-08-18 07:45:01 +0000927 void AssertFastElements(Register elements);
928
ager@chromium.org9085a012009-05-11 19:22:57 +0000929 // Like Assert(), but always enabled.
930 void Check(Condition cc, const char* msg);
931
932 // Print a message to stdout and abort execution.
933 void Abort(const char* msg);
934
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000935 // Check that the stack is aligned.
936 void CheckStackAlignment();
937
ager@chromium.org9085a012009-05-11 19:22:57 +0000938 // Verify restrictions about code generated in stubs.
939 void set_generating_stub(bool value) { generating_stub_ = value; }
940 bool generating_stub() { return generating_stub_; }
941 void set_allow_stub_calls(bool value) { allow_stub_calls_ = value; }
942 bool allow_stub_calls() { return allow_stub_calls_; }
943
944 private:
ager@chromium.org0ee099b2011-01-25 14:06:47 +0000945 // Order general registers are pushed by Pushad.
946 // rax, rcx, rdx, rbx, rsi, rdi, r8, r9, r11, r12, r14.
947 static int kSafepointPushRegisterIndices[Register::kNumRegisters];
ager@chromium.org9085a012009-05-11 19:22:57 +0000948 bool generating_stub_;
949 bool allow_stub_calls_;
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000950
951 // Returns a register holding the smi value. The register MUST NOT be
952 // modified. It may be the "smi 1 constant" register.
953 Register GetSmiConstant(Smi* value);
954
955 // Moves the smi value to the destination register.
956 void LoadSmiConstant(Register dst, Smi* value);
957
ager@chromium.org5c838252010-02-19 08:53:10 +0000958 // This handle will be patched with the code object on installation.
959 Handle<Object> code_object_;
ager@chromium.org9085a012009-05-11 19:22:57 +0000960
961 // Helper functions for generating invokes.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000962 template <typename LabelType>
ager@chromium.org9085a012009-05-11 19:22:57 +0000963 void InvokePrologue(const ParameterCount& expected,
964 const ParameterCount& actual,
965 Handle<Code> code_constant,
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000966 Register code_register,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000967 LabelType* done,
ager@chromium.org9085a012009-05-11 19:22:57 +0000968 InvokeFlag flag);
969
ager@chromium.org9085a012009-05-11 19:22:57 +0000970 // Activation support.
971 void EnterFrame(StackFrame::Type type);
972 void LeaveFrame(StackFrame::Type type);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000973
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000974 void EnterExitFramePrologue(bool save_rax);
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +0000975
976 // Allocates arg_stack_space * kPointerSize memory (not GCed) on the stack
977 // accessible via StackSpaceOperand.
ager@chromium.org0ee099b2011-01-25 14:06:47 +0000978 void EnterExitFrameEpilogue(int arg_stack_space, bool save_doubles);
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +0000979
980 void LeaveExitFrameEpilogue();
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000981
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000982 // Allocation support helpers.
ager@chromium.orgac091b72010-05-05 07:34:42 +0000983 // Loads the top of new-space into the result register.
ager@chromium.orgac091b72010-05-05 07:34:42 +0000984 // Otherwise the address of the new-space top is loaded into scratch (if
985 // scratch is valid), and the new-space top is loaded into result.
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000986 void LoadAllocationTopHelper(Register result,
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000987 Register scratch,
ager@chromium.orga1645e22009-09-09 19:27:10 +0000988 AllocationFlags flags);
ager@chromium.orgac091b72010-05-05 07:34:42 +0000989 // Update allocation top with value in result_end register.
990 // If scratch is valid, it contains the address of the allocation top.
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000991 void UpdateAllocationTopHelper(Register result_end, Register scratch);
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000992
993 // Helper for PopHandleScope. Allowed to perform a GC and returns
994 // NULL if gc_allowed. Does not perform a GC if !gc_allowed, and
995 // possibly returns a failure object indicating an allocation failure.
996 Object* PopHandleScopeHelper(Register saved,
997 Register scratch,
998 bool gc_allowed);
ager@chromium.org9085a012009-05-11 19:22:57 +0000999};
1000
1001
ager@chromium.org4af710e2009-09-15 12:20:11 +00001002// The code patcher is used to patch (typically) small parts of code e.g. for
1003// debugging and other types of instrumentation. When using the code patcher
1004// the exact number of bytes specified must be emitted. Is not legal to emit
1005// relocation information. If any of these constraints are violated it causes
1006// an assertion.
1007class CodePatcher {
1008 public:
1009 CodePatcher(byte* address, int size);
1010 virtual ~CodePatcher();
1011
1012 // Macro assembler to emit code.
1013 MacroAssembler* masm() { return &masm_; }
1014
1015 private:
1016 byte* address_; // The address of the code being patched.
1017 int size_; // Number of bytes of the expected patch size.
1018 MacroAssembler masm_; // Macro assembler used to generate the code.
1019};
1020
1021
ager@chromium.org9085a012009-05-11 19:22:57 +00001022// -----------------------------------------------------------------------------
1023// Static helper functions.
1024
1025// Generate an Operand for loading a field from an object.
1026static inline Operand FieldOperand(Register object, int offset) {
1027 return Operand(object, offset - kHeapObjectTag);
1028}
1029
1030
1031// Generate an Operand for loading an indexed field from an object.
1032static inline Operand FieldOperand(Register object,
1033 Register index,
1034 ScaleFactor scale,
1035 int offset) {
1036 return Operand(object, index, scale, offset - kHeapObjectTag);
1037}
1038
1039
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00001040static inline Operand ContextOperand(Register context, int index) {
1041 return Operand(context, Context::SlotOffset(index));
1042}
1043
1044
1045static inline Operand GlobalObjectOperand() {
1046 return ContextOperand(rsi, Context::GLOBAL_INDEX);
1047}
1048
1049
1050// Provides access to exit frame stack space (not GCed).
1051static inline Operand StackSpaceOperand(int index) {
1052#ifdef _WIN64
1053 const int kShaddowSpace = 4;
1054 return Operand(rsp, (index + kShaddowSpace) * kPointerSize);
1055#else
1056 return Operand(rsp, index * kPointerSize);
1057#endif
1058}
1059
1060
1061
ager@chromium.org9085a012009-05-11 19:22:57 +00001062#ifdef GENERATED_CODE_COVERAGE
1063extern void LogGeneratedCodeCoverage(const char* file_line);
1064#define CODE_COVERAGE_STRINGIFY(x) #x
1065#define CODE_COVERAGE_TOSTRING(x) CODE_COVERAGE_STRINGIFY(x)
1066#define __FILE_LINE__ __FILE__ ":" CODE_COVERAGE_TOSTRING(__LINE__)
1067#define ACCESS_MASM(masm) { \
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00001068 byte* x64_coverage_function = \
ager@chromium.org9085a012009-05-11 19:22:57 +00001069 reinterpret_cast<byte*>(FUNCTION_ADDR(LogGeneratedCodeCoverage)); \
1070 masm->pushfd(); \
1071 masm->pushad(); \
1072 masm->push(Immediate(reinterpret_cast<int>(&__FILE_LINE__))); \
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00001073 masm->call(x64_coverage_function, RelocInfo::RUNTIME_ENTRY); \
ager@chromium.org9085a012009-05-11 19:22:57 +00001074 masm->pop(rax); \
1075 masm->popad(); \
1076 masm->popfd(); \
1077 } \
1078 masm->
1079#else
1080#define ACCESS_MASM(masm) masm->
1081#endif
1082
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001083// -----------------------------------------------------------------------------
1084// Template implementations.
1085
1086static int kSmiShift = kSmiTagSize + kSmiShiftSize;
1087
1088
1089template <typename LabelType>
1090void MacroAssembler::SmiNeg(Register dst,
1091 Register src,
1092 LabelType* on_smi_result) {
1093 if (dst.is(src)) {
1094 ASSERT(!dst.is(kScratchRegister));
1095 movq(kScratchRegister, src);
1096 neg(dst); // Low 32 bits are retained as zero by negation.
1097 // Test if result is zero or Smi::kMinValue.
1098 cmpq(dst, kScratchRegister);
1099 j(not_equal, on_smi_result);
1100 movq(src, kScratchRegister);
1101 } else {
1102 movq(dst, src);
1103 neg(dst);
1104 cmpq(dst, src);
1105 // If the result is zero or Smi::kMinValue, negation failed to create a smi.
1106 j(not_equal, on_smi_result);
1107 }
1108}
1109
1110
1111template <typename LabelType>
1112void MacroAssembler::SmiAdd(Register dst,
1113 Register src1,
1114 Register src2,
1115 LabelType* on_not_smi_result) {
1116 ASSERT_NOT_NULL(on_not_smi_result);
1117 ASSERT(!dst.is(src2));
1118 if (dst.is(src1)) {
1119 movq(kScratchRegister, src1);
1120 addq(kScratchRegister, src2);
1121 j(overflow, on_not_smi_result);
1122 movq(dst, kScratchRegister);
1123 } else {
1124 movq(dst, src1);
1125 addq(dst, src2);
1126 j(overflow, on_not_smi_result);
1127 }
1128}
1129
1130
1131template <typename LabelType>
1132void MacroAssembler::SmiSub(Register dst,
1133 Register src1,
1134 Register src2,
1135 LabelType* on_not_smi_result) {
1136 ASSERT_NOT_NULL(on_not_smi_result);
1137 ASSERT(!dst.is(src2));
1138 if (dst.is(src1)) {
1139 cmpq(dst, src2);
1140 j(overflow, on_not_smi_result);
1141 subq(dst, src2);
1142 } else {
1143 movq(dst, src1);
1144 subq(dst, src2);
1145 j(overflow, on_not_smi_result);
1146 }
1147}
1148
1149
1150template <typename LabelType>
1151void MacroAssembler::SmiSub(Register dst,
1152 Register src1,
1153 const Operand& src2,
1154 LabelType* on_not_smi_result) {
1155 ASSERT_NOT_NULL(on_not_smi_result);
1156 if (dst.is(src1)) {
1157 movq(kScratchRegister, src2);
1158 cmpq(src1, kScratchRegister);
1159 j(overflow, on_not_smi_result);
1160 subq(src1, kScratchRegister);
1161 } else {
1162 movq(dst, src1);
1163 subq(dst, src2);
1164 j(overflow, on_not_smi_result);
1165 }
1166}
1167
1168
1169template <typename LabelType>
1170void MacroAssembler::SmiMul(Register dst,
1171 Register src1,
1172 Register src2,
1173 LabelType* on_not_smi_result) {
1174 ASSERT(!dst.is(src2));
1175 ASSERT(!dst.is(kScratchRegister));
1176 ASSERT(!src1.is(kScratchRegister));
1177 ASSERT(!src2.is(kScratchRegister));
1178
1179 if (dst.is(src1)) {
1180 NearLabel failure, zero_correct_result;
1181 movq(kScratchRegister, src1); // Create backup for later testing.
1182 SmiToInteger64(dst, src1);
1183 imul(dst, src2);
1184 j(overflow, &failure);
1185
1186 // Check for negative zero result. If product is zero, and one
1187 // argument is negative, go to slow case.
1188 NearLabel correct_result;
1189 testq(dst, dst);
1190 j(not_zero, &correct_result);
1191
1192 movq(dst, kScratchRegister);
1193 xor_(dst, src2);
1194 j(positive, &zero_correct_result); // Result was positive zero.
1195
1196 bind(&failure); // Reused failure exit, restores src1.
1197 movq(src1, kScratchRegister);
1198 jmp(on_not_smi_result);
1199
1200 bind(&zero_correct_result);
lrn@chromium.org5d00b602011-01-05 09:51:43 +00001201 Set(dst, 0);
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001202
1203 bind(&correct_result);
1204 } else {
1205 SmiToInteger64(dst, src1);
1206 imul(dst, src2);
1207 j(overflow, on_not_smi_result);
1208 // Check for negative zero result. If product is zero, and one
1209 // argument is negative, go to slow case.
1210 NearLabel correct_result;
1211 testq(dst, dst);
1212 j(not_zero, &correct_result);
1213 // One of src1 and src2 is zero, the check whether the other is
1214 // negative.
1215 movq(kScratchRegister, src1);
1216 xor_(kScratchRegister, src2);
1217 j(negative, on_not_smi_result);
1218 bind(&correct_result);
1219 }
1220}
1221
1222
1223template <typename LabelType>
1224void MacroAssembler::SmiTryAddConstant(Register dst,
1225 Register src,
1226 Smi* constant,
1227 LabelType* on_not_smi_result) {
1228 // Does not assume that src is a smi.
1229 ASSERT_EQ(static_cast<int>(1), static_cast<int>(kSmiTagMask));
1230 ASSERT_EQ(0, kSmiTag);
1231 ASSERT(!dst.is(kScratchRegister));
1232 ASSERT(!src.is(kScratchRegister));
1233
1234 JumpIfNotSmi(src, on_not_smi_result);
1235 Register tmp = (dst.is(src) ? kScratchRegister : dst);
1236 LoadSmiConstant(tmp, constant);
1237 addq(tmp, src);
1238 j(overflow, on_not_smi_result);
1239 if (dst.is(src)) {
1240 movq(dst, tmp);
1241 }
1242}
1243
1244
1245template <typename LabelType>
1246void MacroAssembler::SmiAddConstant(Register dst,
1247 Register src,
1248 Smi* constant,
1249 LabelType* on_not_smi_result) {
1250 if (constant->value() == 0) {
1251 if (!dst.is(src)) {
1252 movq(dst, src);
1253 }
1254 } else if (dst.is(src)) {
1255 ASSERT(!dst.is(kScratchRegister));
1256
1257 LoadSmiConstant(kScratchRegister, constant);
1258 addq(kScratchRegister, src);
1259 j(overflow, on_not_smi_result);
1260 movq(dst, kScratchRegister);
1261 } else {
1262 LoadSmiConstant(dst, constant);
1263 addq(dst, src);
1264 j(overflow, on_not_smi_result);
1265 }
1266}
1267
1268
1269template <typename LabelType>
1270void MacroAssembler::SmiSubConstant(Register dst,
1271 Register src,
1272 Smi* constant,
1273 LabelType* on_not_smi_result) {
1274 if (constant->value() == 0) {
1275 if (!dst.is(src)) {
1276 movq(dst, src);
1277 }
1278 } else if (dst.is(src)) {
1279 ASSERT(!dst.is(kScratchRegister));
1280 if (constant->value() == Smi::kMinValue) {
1281 // Subtracting min-value from any non-negative value will overflow.
1282 // We test the non-negativeness before doing the subtraction.
1283 testq(src, src);
1284 j(not_sign, on_not_smi_result);
1285 LoadSmiConstant(kScratchRegister, constant);
1286 subq(dst, kScratchRegister);
1287 } else {
1288 // Subtract by adding the negation.
1289 LoadSmiConstant(kScratchRegister, Smi::FromInt(-constant->value()));
1290 addq(kScratchRegister, dst);
1291 j(overflow, on_not_smi_result);
1292 movq(dst, kScratchRegister);
1293 }
1294 } else {
1295 if (constant->value() == Smi::kMinValue) {
1296 // Subtracting min-value from any non-negative value will overflow.
1297 // We test the non-negativeness before doing the subtraction.
1298 testq(src, src);
1299 j(not_sign, on_not_smi_result);
1300 LoadSmiConstant(dst, constant);
1301 // Adding and subtracting the min-value gives the same result, it only
1302 // differs on the overflow bit, which we don't check here.
1303 addq(dst, src);
1304 } else {
1305 // Subtract by adding the negation.
1306 LoadSmiConstant(dst, Smi::FromInt(-(constant->value())));
1307 addq(dst, src);
1308 j(overflow, on_not_smi_result);
1309 }
1310 }
1311}
1312
1313
1314template <typename LabelType>
1315void MacroAssembler::SmiDiv(Register dst,
1316 Register src1,
1317 Register src2,
1318 LabelType* on_not_smi_result) {
1319 ASSERT(!src1.is(kScratchRegister));
1320 ASSERT(!src2.is(kScratchRegister));
1321 ASSERT(!dst.is(kScratchRegister));
1322 ASSERT(!src2.is(rax));
1323 ASSERT(!src2.is(rdx));
1324 ASSERT(!src1.is(rdx));
1325
1326 // Check for 0 divisor (result is +/-Infinity).
1327 NearLabel positive_divisor;
1328 testq(src2, src2);
1329 j(zero, on_not_smi_result);
1330
1331 if (src1.is(rax)) {
1332 movq(kScratchRegister, src1);
1333 }
1334 SmiToInteger32(rax, src1);
1335 // We need to rule out dividing Smi::kMinValue by -1, since that would
1336 // overflow in idiv and raise an exception.
1337 // We combine this with negative zero test (negative zero only happens
1338 // when dividing zero by a negative number).
1339
1340 // We overshoot a little and go to slow case if we divide min-value
1341 // by any negative value, not just -1.
1342 NearLabel safe_div;
1343 testl(rax, Immediate(0x7fffffff));
1344 j(not_zero, &safe_div);
1345 testq(src2, src2);
1346 if (src1.is(rax)) {
1347 j(positive, &safe_div);
1348 movq(src1, kScratchRegister);
1349 jmp(on_not_smi_result);
1350 } else {
1351 j(negative, on_not_smi_result);
1352 }
1353 bind(&safe_div);
1354
1355 SmiToInteger32(src2, src2);
1356 // Sign extend src1 into edx:eax.
1357 cdq();
1358 idivl(src2);
1359 Integer32ToSmi(src2, src2);
1360 // Check that the remainder is zero.
1361 testl(rdx, rdx);
1362 if (src1.is(rax)) {
1363 NearLabel smi_result;
1364 j(zero, &smi_result);
1365 movq(src1, kScratchRegister);
1366 jmp(on_not_smi_result);
1367 bind(&smi_result);
1368 } else {
1369 j(not_zero, on_not_smi_result);
1370 }
1371 if (!dst.is(src1) && src1.is(rax)) {
1372 movq(src1, kScratchRegister);
1373 }
1374 Integer32ToSmi(dst, rax);
1375}
1376
1377
1378template <typename LabelType>
1379void MacroAssembler::SmiMod(Register dst,
1380 Register src1,
1381 Register src2,
1382 LabelType* on_not_smi_result) {
1383 ASSERT(!dst.is(kScratchRegister));
1384 ASSERT(!src1.is(kScratchRegister));
1385 ASSERT(!src2.is(kScratchRegister));
1386 ASSERT(!src2.is(rax));
1387 ASSERT(!src2.is(rdx));
1388 ASSERT(!src1.is(rdx));
1389 ASSERT(!src1.is(src2));
1390
1391 testq(src2, src2);
1392 j(zero, on_not_smi_result);
1393
1394 if (src1.is(rax)) {
1395 movq(kScratchRegister, src1);
1396 }
1397 SmiToInteger32(rax, src1);
1398 SmiToInteger32(src2, src2);
1399
1400 // Test for the edge case of dividing Smi::kMinValue by -1 (will overflow).
1401 NearLabel safe_div;
1402 cmpl(rax, Immediate(Smi::kMinValue));
1403 j(not_equal, &safe_div);
1404 cmpl(src2, Immediate(-1));
1405 j(not_equal, &safe_div);
1406 // Retag inputs and go slow case.
1407 Integer32ToSmi(src2, src2);
1408 if (src1.is(rax)) {
1409 movq(src1, kScratchRegister);
1410 }
1411 jmp(on_not_smi_result);
1412 bind(&safe_div);
1413
1414 // Sign extend eax into edx:eax.
1415 cdq();
1416 idivl(src2);
1417 // Restore smi tags on inputs.
1418 Integer32ToSmi(src2, src2);
1419 if (src1.is(rax)) {
1420 movq(src1, kScratchRegister);
1421 }
1422 // Check for a negative zero result. If the result is zero, and the
1423 // dividend is negative, go slow to return a floating point negative zero.
1424 NearLabel smi_result;
1425 testl(rdx, rdx);
1426 j(not_zero, &smi_result);
1427 testq(src1, src1);
1428 j(negative, on_not_smi_result);
1429 bind(&smi_result);
1430 Integer32ToSmi(dst, rdx);
1431}
1432
1433
1434template <typename LabelType>
1435void MacroAssembler::SmiShiftLogicalRightConstant(
1436 Register dst, Register src, int shift_value, LabelType* on_not_smi_result) {
1437 // Logic right shift interprets its result as an *unsigned* number.
1438 if (dst.is(src)) {
1439 UNIMPLEMENTED(); // Not used.
1440 } else {
1441 movq(dst, src);
1442 if (shift_value == 0) {
1443 testq(dst, dst);
1444 j(negative, on_not_smi_result);
1445 }
1446 shr(dst, Immediate(shift_value + kSmiShift));
1447 shl(dst, Immediate(kSmiShift));
1448 }
1449}
1450
1451
1452template <typename LabelType>
1453void MacroAssembler::SmiShiftLogicalRight(Register dst,
1454 Register src1,
1455 Register src2,
1456 LabelType* on_not_smi_result) {
1457 ASSERT(!dst.is(kScratchRegister));
1458 ASSERT(!src1.is(kScratchRegister));
1459 ASSERT(!src2.is(kScratchRegister));
1460 ASSERT(!dst.is(rcx));
1461 NearLabel result_ok;
1462 if (src1.is(rcx) || src2.is(rcx)) {
1463 movq(kScratchRegister, rcx);
1464 }
1465 if (!dst.is(src1)) {
1466 movq(dst, src1);
1467 }
1468 SmiToInteger32(rcx, src2);
1469 orl(rcx, Immediate(kSmiShift));
1470 shr_cl(dst); // Shift is rcx modulo 0x1f + 32.
1471 shl(dst, Immediate(kSmiShift));
1472 testq(dst, dst);
1473 if (src1.is(rcx) || src2.is(rcx)) {
1474 NearLabel positive_result;
1475 j(positive, &positive_result);
1476 if (src1.is(rcx)) {
1477 movq(src1, kScratchRegister);
1478 } else {
1479 movq(src2, kScratchRegister);
1480 }
1481 jmp(on_not_smi_result);
1482 bind(&positive_result);
1483 } else {
1484 j(negative, on_not_smi_result); // src2 was zero and src1 negative.
1485 }
1486}
1487
1488
1489template <typename LabelType>
1490void MacroAssembler::SelectNonSmi(Register dst,
1491 Register src1,
1492 Register src2,
1493 LabelType* on_not_smis) {
1494 ASSERT(!dst.is(kScratchRegister));
1495 ASSERT(!src1.is(kScratchRegister));
1496 ASSERT(!src2.is(kScratchRegister));
1497 ASSERT(!dst.is(src1));
1498 ASSERT(!dst.is(src2));
1499 // Both operands must not be smis.
1500#ifdef DEBUG
1501 if (allow_stub_calls()) { // Check contains a stub call.
1502 Condition not_both_smis = NegateCondition(CheckBothSmi(src1, src2));
1503 Check(not_both_smis, "Both registers were smis in SelectNonSmi.");
1504 }
1505#endif
1506 ASSERT_EQ(0, kSmiTag);
1507 ASSERT_EQ(0, Smi::FromInt(0));
1508 movl(kScratchRegister, Immediate(kSmiTagMask));
1509 and_(kScratchRegister, src1);
1510 testl(kScratchRegister, src2);
1511 // If non-zero then both are smis.
1512 j(not_zero, on_not_smis);
1513
1514 // Exactly one operand is a smi.
1515 ASSERT_EQ(1, static_cast<int>(kSmiTagMask));
1516 // kScratchRegister still holds src1 & kSmiTag, which is either zero or one.
1517 subq(kScratchRegister, Immediate(1));
1518 // If src1 is a smi, then scratch register all 1s, else it is all 0s.
1519 movq(dst, src1);
1520 xor_(dst, src2);
1521 and_(dst, kScratchRegister);
1522 // If src1 is a smi, dst holds src1 ^ src2, else it is zero.
1523 xor_(dst, src1);
1524 // If src1 is a smi, dst is src2, else it is src1, i.e., the non-smi.
1525}
1526
1527
1528template <typename LabelType>
1529void MacroAssembler::JumpIfSmi(Register src, LabelType* on_smi) {
1530 ASSERT_EQ(0, kSmiTag);
1531 Condition smi = CheckSmi(src);
1532 j(smi, on_smi);
1533}
1534
1535
1536template <typename LabelType>
1537void MacroAssembler::JumpIfNotSmi(Register src, LabelType* on_not_smi) {
1538 Condition smi = CheckSmi(src);
1539 j(NegateCondition(smi), on_not_smi);
1540}
1541
1542
1543template <typename LabelType>
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +00001544void MacroAssembler::JumpUnlessNonNegativeSmi(
1545 Register src, LabelType* on_not_smi_or_negative) {
1546 Condition non_negative_smi = CheckNonNegativeSmi(src);
1547 j(NegateCondition(non_negative_smi), on_not_smi_or_negative);
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001548}
1549
1550
1551template <typename LabelType>
1552void MacroAssembler::JumpIfSmiEqualsConstant(Register src,
1553 Smi* constant,
1554 LabelType* on_equals) {
1555 SmiCompare(src, constant);
1556 j(equal, on_equals);
1557}
1558
1559
1560template <typename LabelType>
1561void MacroAssembler::JumpIfNotValidSmiValue(Register src,
1562 LabelType* on_invalid) {
1563 Condition is_valid = CheckInteger32ValidSmiValue(src);
1564 j(NegateCondition(is_valid), on_invalid);
1565}
1566
1567
1568template <typename LabelType>
1569void MacroAssembler::JumpIfUIntNotValidSmiValue(Register src,
1570 LabelType* on_invalid) {
1571 Condition is_valid = CheckUInteger32ValidSmiValue(src);
1572 j(NegateCondition(is_valid), on_invalid);
1573}
1574
1575
1576template <typename LabelType>
1577void MacroAssembler::JumpIfNotBothSmi(Register src1,
1578 Register src2,
1579 LabelType* on_not_both_smi) {
1580 Condition both_smi = CheckBothSmi(src1, src2);
1581 j(NegateCondition(both_smi), on_not_both_smi);
1582}
1583
1584
1585template <typename LabelType>
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +00001586void MacroAssembler::JumpUnlessBothNonNegativeSmi(Register src1,
1587 Register src2,
1588 LabelType* on_not_both_smi) {
1589 Condition both_smi = CheckBothNonNegativeSmi(src1, src2);
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001590 j(NegateCondition(both_smi), on_not_both_smi);
1591}
1592
1593
1594template <typename LabelType>
1595void MacroAssembler::JumpIfNotBothSequentialAsciiStrings(Register first_object,
1596 Register second_object,
1597 Register scratch1,
1598 Register scratch2,
1599 LabelType* on_fail) {
1600 // Check that both objects are not smis.
1601 Condition either_smi = CheckEitherSmi(first_object, second_object);
1602 j(either_smi, on_fail);
1603
1604 // Load instance type for both strings.
1605 movq(scratch1, FieldOperand(first_object, HeapObject::kMapOffset));
1606 movq(scratch2, FieldOperand(second_object, HeapObject::kMapOffset));
1607 movzxbl(scratch1, FieldOperand(scratch1, Map::kInstanceTypeOffset));
1608 movzxbl(scratch2, FieldOperand(scratch2, Map::kInstanceTypeOffset));
1609
1610 // Check that both are flat ascii strings.
1611 ASSERT(kNotStringTag != 0);
1612 const int kFlatAsciiStringMask =
1613 kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask;
1614 const int kFlatAsciiStringTag = ASCII_STRING_TYPE;
1615
1616 andl(scratch1, Immediate(kFlatAsciiStringMask));
1617 andl(scratch2, Immediate(kFlatAsciiStringMask));
1618 // Interleave the bits to check both scratch1 and scratch2 in one test.
1619 ASSERT_EQ(0, kFlatAsciiStringMask & (kFlatAsciiStringMask << 3));
1620 lea(scratch1, Operand(scratch1, scratch2, times_8, 0));
1621 cmpl(scratch1,
1622 Immediate(kFlatAsciiStringTag + (kFlatAsciiStringTag << 3)));
1623 j(not_equal, on_fail);
1624}
1625
1626
1627template <typename LabelType>
1628void MacroAssembler::JumpIfInstanceTypeIsNotSequentialAscii(
1629 Register instance_type,
1630 Register scratch,
1631 LabelType *failure) {
1632 if (!scratch.is(instance_type)) {
1633 movl(scratch, instance_type);
1634 }
1635
1636 const int kFlatAsciiStringMask =
1637 kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask;
1638
1639 andl(scratch, Immediate(kFlatAsciiStringMask));
1640 cmpl(scratch, Immediate(kStringTag | kSeqStringTag | kAsciiStringTag));
1641 j(not_equal, failure);
1642}
1643
1644
1645template <typename LabelType>
1646void MacroAssembler::JumpIfBothInstanceTypesAreNotSequentialAscii(
1647 Register first_object_instance_type,
1648 Register second_object_instance_type,
1649 Register scratch1,
1650 Register scratch2,
1651 LabelType* on_fail) {
1652 // Load instance type for both strings.
1653 movq(scratch1, first_object_instance_type);
1654 movq(scratch2, second_object_instance_type);
1655
1656 // Check that both are flat ascii strings.
1657 ASSERT(kNotStringTag != 0);
1658 const int kFlatAsciiStringMask =
1659 kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask;
1660 const int kFlatAsciiStringTag = ASCII_STRING_TYPE;
1661
1662 andl(scratch1, Immediate(kFlatAsciiStringMask));
1663 andl(scratch2, Immediate(kFlatAsciiStringMask));
1664 // Interleave the bits to check both scratch1 and scratch2 in one test.
1665 ASSERT_EQ(0, kFlatAsciiStringMask & (kFlatAsciiStringMask << 3));
1666 lea(scratch1, Operand(scratch1, scratch2, times_8, 0));
1667 cmpl(scratch1,
1668 Immediate(kFlatAsciiStringTag + (kFlatAsciiStringTag << 3)));
1669 j(not_equal, on_fail);
1670}
1671
1672
1673template <typename LabelType>
1674void MacroAssembler::InNewSpace(Register object,
1675 Register scratch,
1676 Condition cc,
1677 LabelType* branch) {
1678 if (Serializer::enabled()) {
1679 // Can't do arithmetic on external references if it might get serialized.
1680 // The mask isn't really an address. We load it as an external reference in
1681 // case the size of the new space is different between the snapshot maker
1682 // and the running system.
1683 if (scratch.is(object)) {
1684 movq(kScratchRegister, ExternalReference::new_space_mask());
1685 and_(scratch, kScratchRegister);
1686 } else {
1687 movq(scratch, ExternalReference::new_space_mask());
1688 and_(scratch, object);
1689 }
1690 movq(kScratchRegister, ExternalReference::new_space_start());
1691 cmpq(scratch, kScratchRegister);
1692 j(cc, branch);
1693 } else {
1694 ASSERT(is_int32(static_cast<int64_t>(Heap::NewSpaceMask())));
1695 intptr_t new_space_start =
1696 reinterpret_cast<intptr_t>(Heap::NewSpaceStart());
1697 movq(kScratchRegister, -new_space_start, RelocInfo::NONE);
1698 if (scratch.is(object)) {
1699 addq(scratch, kScratchRegister);
1700 } else {
1701 lea(scratch, Operand(object, kScratchRegister, times_1, 0));
1702 }
1703 and_(scratch, Immediate(static_cast<int32_t>(Heap::NewSpaceMask())));
1704 j(cc, branch);
1705 }
1706}
1707
1708
1709template <typename LabelType>
1710void MacroAssembler::InvokePrologue(const ParameterCount& expected,
1711 const ParameterCount& actual,
1712 Handle<Code> code_constant,
1713 Register code_register,
1714 LabelType* done,
1715 InvokeFlag flag) {
1716 bool definitely_matches = false;
1717 NearLabel invoke;
1718 if (expected.is_immediate()) {
1719 ASSERT(actual.is_immediate());
1720 if (expected.immediate() == actual.immediate()) {
1721 definitely_matches = true;
1722 } else {
1723 Set(rax, actual.immediate());
1724 if (expected.immediate() ==
1725 SharedFunctionInfo::kDontAdaptArgumentsSentinel) {
1726 // Don't worry about adapting arguments for built-ins that
1727 // don't want that done. Skip adaption code by making it look
1728 // like we have a match between expected and actual number of
1729 // arguments.
1730 definitely_matches = true;
1731 } else {
1732 Set(rbx, expected.immediate());
1733 }
1734 }
1735 } else {
1736 if (actual.is_immediate()) {
1737 // Expected is in register, actual is immediate. This is the
1738 // case when we invoke function values without going through the
1739 // IC mechanism.
1740 cmpq(expected.reg(), Immediate(actual.immediate()));
1741 j(equal, &invoke);
1742 ASSERT(expected.reg().is(rbx));
1743 Set(rax, actual.immediate());
1744 } else if (!expected.reg().is(actual.reg())) {
1745 // Both expected and actual are in (different) registers. This
1746 // is the case when we invoke functions using call and apply.
1747 cmpq(expected.reg(), actual.reg());
1748 j(equal, &invoke);
1749 ASSERT(actual.reg().is(rax));
1750 ASSERT(expected.reg().is(rbx));
1751 }
1752 }
1753
1754 if (!definitely_matches) {
1755 Handle<Code> adaptor =
1756 Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline));
1757 if (!code_constant.is_null()) {
1758 movq(rdx, code_constant, RelocInfo::EMBEDDED_OBJECT);
1759 addq(rdx, Immediate(Code::kHeaderSize - kHeapObjectTag));
1760 } else if (!code_register.is(rdx)) {
1761 movq(rdx, code_register);
1762 }
1763
1764 if (flag == CALL_FUNCTION) {
1765 Call(adaptor, RelocInfo::CODE_TARGET);
1766 jmp(done);
1767 } else {
1768 Jump(adaptor, RelocInfo::CODE_TARGET);
1769 }
1770 bind(&invoke);
1771 }
1772}
1773
ager@chromium.org9085a012009-05-11 19:22:57 +00001774
1775} } // namespace v8::internal
1776
1777#endif // V8_X64_MACRO_ASSEMBLER_X64_H_