blob: d8f2fba42a7fa462d5a25d28dde012e4f63651e3 [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.
155 void EnterExitFrame(int arg_stack_space = 0);
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.
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +0000164 void LeaveExitFrame();
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.
171 void PushSafepointRegisters() { UNIMPLEMENTED(); }
172 void PopSafepointRegisters() { UNIMPLEMENTED(); }
173 static int SafepointRegisterStackIndex(int reg_code) {
174 UNIMPLEMENTED();
175 return 0;
176 }
177
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);
275
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +0000276 // Is the value a non-negative tagged smi.
277 Condition CheckNonNegativeSmi(Register src);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000278
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000279 // Are both values tagged smis.
ager@chromium.org4af710e2009-09-15 12:20:11 +0000280 Condition CheckBothSmi(Register first, Register second);
281
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +0000282 // Are both values non-negative tagged smis.
283 Condition CheckBothNonNegativeSmi(Register first, Register second);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000284
285 // Are either value a tagged smi.
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +0000286 Condition CheckEitherSmi(Register first,
287 Register second,
288 Register scratch = kScratchRegister);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000289
ager@chromium.org4af710e2009-09-15 12:20:11 +0000290 // Is the value the minimum smi value (since we are using
291 // two's complement numbers, negating the value is known to yield
292 // a non-smi value).
293 Condition CheckIsMinSmi(Register src);
294
ager@chromium.org4af710e2009-09-15 12:20:11 +0000295 // Checks whether an 32-bit integer value is a valid for conversion
296 // to a smi.
297 Condition CheckInteger32ValidSmiValue(Register src);
298
ager@chromium.org3811b432009-10-28 14:53:37 +0000299 // Checks whether an 32-bit unsigned integer value is a valid for
300 // conversion to a smi.
301 Condition CheckUInteger32ValidSmiValue(Register src);
302
ager@chromium.org4af710e2009-09-15 12:20:11 +0000303 // Test-and-jump functions. Typically combines a check function
304 // above with a conditional jump.
305
306 // Jump if the value cannot be represented by a smi.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000307 template <typename LabelType>
308 void JumpIfNotValidSmiValue(Register src, LabelType* on_invalid);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000309
ager@chromium.org3811b432009-10-28 14:53:37 +0000310 // Jump if the unsigned integer value cannot be represented by a smi.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000311 template <typename LabelType>
312 void JumpIfUIntNotValidSmiValue(Register src, LabelType* on_invalid);
ager@chromium.org3811b432009-10-28 14:53:37 +0000313
ager@chromium.org4af710e2009-09-15 12:20:11 +0000314 // Jump to label if the value is a tagged smi.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000315 template <typename LabelType>
316 void JumpIfSmi(Register src, LabelType* on_smi);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000317
318 // Jump to label if the value is not a tagged smi.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000319 template <typename LabelType>
320 void JumpIfNotSmi(Register src, LabelType* on_not_smi);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000321
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +0000322 // Jump to label if the value is not a non-negative tagged smi.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000323 template <typename LabelType>
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +0000324 void JumpUnlessNonNegativeSmi(Register src, LabelType* on_not_smi);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000325
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000326 // Jump to label if the value, which must be a tagged smi, has value equal
ager@chromium.org4af710e2009-09-15 12:20:11 +0000327 // to the constant.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000328 template <typename LabelType>
329 void JumpIfSmiEqualsConstant(Register src,
330 Smi* constant,
331 LabelType* on_equals);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000332
ager@chromium.org4af710e2009-09-15 12:20:11 +0000333 // Jump if either or both register are not smi values.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000334 template <typename LabelType>
335 void JumpIfNotBothSmi(Register src1,
336 Register src2,
337 LabelType* on_not_both_smi);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000338
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +0000339 // Jump if either or both register are not non-negative smi values.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000340 template <typename LabelType>
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +0000341 void JumpUnlessBothNonNegativeSmi(Register src1, Register src2,
342 LabelType* on_not_both_smi);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000343
ager@chromium.org4af710e2009-09-15 12:20:11 +0000344 // Operations on tagged smi values.
345
346 // Smis represent a subset of integers. The subset is always equivalent to
347 // a two's complement interpretation of a fixed number of bits.
348
349 // Optimistically adds an integer constant to a supposed smi.
350 // If the src is not a smi, or the result is not a smi, jump to
351 // the label.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000352 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000353 void SmiTryAddConstant(Register dst,
354 Register src,
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000355 Smi* constant,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000356 LabelType* on_not_smi_result);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000357
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000358 // Add an integer constant to a tagged smi, giving a tagged smi as result.
359 // No overflow testing on the result is done.
360 void SmiAddConstant(Register dst, Register src, Smi* constant);
361
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000362 // Add an integer constant to a tagged smi, giving a tagged smi as result.
363 // No overflow testing on the result is done.
364 void SmiAddConstant(const Operand& dst, Smi* constant);
365
ager@chromium.org4af710e2009-09-15 12:20:11 +0000366 // Add an integer constant to a tagged smi, giving a tagged smi as result,
367 // or jumping to a label if the result cannot be represented by a smi.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000368 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000369 void SmiAddConstant(Register dst,
370 Register src,
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000371 Smi* constant,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000372 LabelType* on_not_smi_result);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000373
374 // Subtract an integer constant from a tagged smi, giving a tagged smi as
ager@chromium.orgac091b72010-05-05 07:34:42 +0000375 // result. No testing on the result is done. Sets the N and Z flags
376 // based on the value of the resulting integer.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000377 void SmiSubConstant(Register dst, Register src, Smi* constant);
378
379 // Subtract an integer constant from a tagged smi, giving a tagged smi as
ager@chromium.org4af710e2009-09-15 12:20:11 +0000380 // result, or jumping to a label if the result cannot be represented by a smi.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000381 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000382 void SmiSubConstant(Register dst,
383 Register src,
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000384 Smi* constant,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000385 LabelType* on_not_smi_result);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000386
387 // Negating a smi can give a negative zero or too large positive value.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000388 // NOTICE: This operation jumps on success, not failure!
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000389 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000390 void SmiNeg(Register dst,
391 Register src,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000392 LabelType* on_smi_result);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000393
394 // Adds smi values and return the result as a smi.
395 // If dst is src1, then src1 will be destroyed, even if
396 // the operation is unsuccessful.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000397 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000398 void SmiAdd(Register dst,
399 Register src1,
400 Register src2,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000401 LabelType* on_not_smi_result);
402
403 void SmiAdd(Register dst,
404 Register src1,
405 Register src2);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000406
407 // Subtracts smi values and return the result as a smi.
408 // If dst is src1, then src1 will be destroyed, even if
409 // the operation is unsuccessful.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000410 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000411 void SmiSub(Register dst,
412 Register src1,
413 Register src2,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000414 LabelType* on_not_smi_result);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000415
ager@chromium.orgac091b72010-05-05 07:34:42 +0000416 void SmiSub(Register dst,
417 Register src1,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000418 Register src2);
419
420 template <typename LabelType>
421 void SmiSub(Register dst,
422 Register src1,
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000423 const Operand& src2,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000424 LabelType* on_not_smi_result);
425
426 void SmiSub(Register dst,
427 Register src1,
428 const Operand& src2);
ager@chromium.orgac091b72010-05-05 07:34:42 +0000429
ager@chromium.org4af710e2009-09-15 12:20:11 +0000430 // Multiplies smi values and return the result as a smi,
431 // if possible.
432 // If dst is src1, then src1 will be destroyed, even if
433 // the operation is unsuccessful.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000434 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000435 void SmiMul(Register dst,
436 Register src1,
437 Register src2,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000438 LabelType* on_not_smi_result);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000439
440 // Divides one smi by another and returns the quotient.
441 // Clobbers rax and rdx registers.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000442 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000443 void SmiDiv(Register dst,
444 Register src1,
445 Register src2,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000446 LabelType* on_not_smi_result);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000447
448 // Divides one smi by another and returns the remainder.
449 // Clobbers rax and rdx registers.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000450 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000451 void SmiMod(Register dst,
452 Register src1,
453 Register src2,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000454 LabelType* on_not_smi_result);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000455
456 // Bitwise operations.
457 void SmiNot(Register dst, Register src);
458 void SmiAnd(Register dst, Register src1, Register src2);
459 void SmiOr(Register dst, Register src1, Register src2);
460 void SmiXor(Register dst, Register src1, Register src2);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000461 void SmiAndConstant(Register dst, Register src1, Smi* constant);
462 void SmiOrConstant(Register dst, Register src1, Smi* constant);
463 void SmiXorConstant(Register dst, Register src1, Smi* constant);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000464
465 void SmiShiftLeftConstant(Register dst,
466 Register src,
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000467 int shift_value);
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000468 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000469 void SmiShiftLogicalRightConstant(Register dst,
470 Register src,
471 int shift_value,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000472 LabelType* on_not_smi_result);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000473 void SmiShiftArithmeticRightConstant(Register dst,
474 Register src,
475 int shift_value);
476
477 // Shifts a smi value to the left, and returns the result if that is a smi.
478 // Uses and clobbers rcx, so dst may not be rcx.
479 void SmiShiftLeft(Register dst,
480 Register src1,
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000481 Register src2);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000482 // Shifts a smi value to the right, shifting in zero bits at the top, and
483 // returns the unsigned intepretation of the result if that is a smi.
484 // Uses and clobbers rcx, so dst may not be rcx.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000485 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000486 void SmiShiftLogicalRight(Register dst,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000487 Register src1,
488 Register src2,
489 LabelType* on_not_smi_result);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000490 // Shifts a smi value to the right, sign extending the top, and
491 // returns the signed intepretation of the result. That will always
492 // be a valid smi value, since it's numerically smaller than the
493 // original.
494 // Uses and clobbers rcx, so dst may not be rcx.
495 void SmiShiftArithmeticRight(Register dst,
496 Register src1,
497 Register src2);
498
499 // Specialized operations
500
501 // Select the non-smi register of two registers where exactly one is a
502 // smi. If neither are smis, jump to the failure label.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000503 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000504 void SelectNonSmi(Register dst,
505 Register src1,
506 Register src2,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000507 LabelType* on_not_smis);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000508
509 // Converts, if necessary, a smi to a combination of number and
510 // multiplier to be used as a scaled index.
511 // The src register contains a *positive* smi value. The shift is the
512 // power of two to multiply the index value by (e.g.
513 // to index by smi-value * kPointerSize, pass the smi and kPointerSizeLog2).
514 // The returned index register may be either src or dst, depending
515 // on what is most efficient. If src and dst are different registers,
516 // src is always unchanged.
517 SmiIndex SmiToIndex(Register dst, Register src, int shift);
518
519 // Converts a positive smi to a negative index.
520 SmiIndex SmiToNegativeIndex(Register dst, Register src, int shift);
521
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000522 // Basic Smi operations.
ager@chromium.org3811b432009-10-28 14:53:37 +0000523 void Move(Register dst, Smi* source) {
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000524 LoadSmiConstant(dst, source);
ager@chromium.org3811b432009-10-28 14:53:37 +0000525 }
526
527 void Move(const Operand& dst, Smi* source) {
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000528 Register constant = GetSmiConstant(source);
529 movq(dst, constant);
ager@chromium.org3811b432009-10-28 14:53:37 +0000530 }
531
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000532 void Push(Smi* smi);
533 void Test(const Operand& dst, Smi* source);
534
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000535 // ---------------------------------------------------------------------------
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000536 // String macros.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000537 template <typename LabelType>
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000538 void JumpIfNotBothSequentialAsciiStrings(Register first_object,
539 Register second_object,
540 Register scratch1,
541 Register scratch2,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000542 LabelType* on_not_both_flat_ascii);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000543
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000544 // Check whether the instance type represents a flat ascii string. Jump to the
545 // label if not. If the instance type can be scratched specify same register
546 // for both instance type and scratch.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000547 template <typename LabelType>
548 void JumpIfInstanceTypeIsNotSequentialAscii(
549 Register instance_type,
550 Register scratch,
551 LabelType *on_not_flat_ascii_string);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000552
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000553 template <typename LabelType>
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000554 void JumpIfBothInstanceTypesAreNotSequentialAscii(
555 Register first_object_instance_type,
556 Register second_object_instance_type,
557 Register scratch1,
558 Register scratch2,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000559 LabelType* on_fail);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000560
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000561 // ---------------------------------------------------------------------------
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000562 // Macro instructions.
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000563
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000564 // Load a register with a long value as efficiently as possible.
ager@chromium.orge2902be2009-06-08 12:21:35 +0000565 void Set(Register dst, int64_t x);
566 void Set(const Operand& dst, int64_t x);
ager@chromium.org9085a012009-05-11 19:22:57 +0000567
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000568 // Move if the registers are not identical.
569 void Move(Register target, Register source);
570
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000571 // Handle support
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000572 void Move(Register dst, Handle<Object> source);
573 void Move(const Operand& dst, Handle<Object> source);
574 void Cmp(Register dst, Handle<Object> source);
ager@chromium.org3e875802009-06-29 08:26:34 +0000575 void Cmp(const Operand& dst, Handle<Object> source);
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000576 void Push(Handle<Object> source);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000577
578 // Emit code to discard a non-negative number of pointer-sized elements
579 // from the stack, clobbering only the rsp register.
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000580 void Drop(int stack_elements);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000581
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000582 void Call(Label* target) { call(target); }
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000583
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000584 // Control Flow
585 void Jump(Address destination, RelocInfo::Mode rmode);
586 void Jump(ExternalReference ext);
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000587 void Jump(Handle<Code> code_object, RelocInfo::Mode rmode);
588
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000589 void Call(Address destination, RelocInfo::Mode rmode);
590 void Call(ExternalReference ext);
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000591 void Call(Handle<Code> code_object, RelocInfo::Mode rmode);
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000592
ager@chromium.org9085a012009-05-11 19:22:57 +0000593 // Compare object type for heap object.
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000594 // Always use unsigned comparisons: above and below, not less and greater.
ager@chromium.org9085a012009-05-11 19:22:57 +0000595 // Incoming register is heap_object and outgoing register is map.
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000596 // They may be the same register, and may be kScratchRegister.
ager@chromium.org9085a012009-05-11 19:22:57 +0000597 void CmpObjectType(Register heap_object, InstanceType type, Register map);
598
599 // Compare instance type for map.
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000600 // Always use unsigned comparisons: above and below, not less and greater.
ager@chromium.org9085a012009-05-11 19:22:57 +0000601 void CmpInstanceType(Register map, InstanceType type);
602
ager@chromium.org5c838252010-02-19 08:53:10 +0000603 // Check if the map of an object is equal to a specified map and
604 // branch to label if not. Skip the smi check if not required
605 // (object is known to be a heap object)
606 void CheckMap(Register obj,
607 Handle<Map> map,
608 Label* fail,
609 bool is_heap_object);
610
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000611 // Check if the object in register heap_object is a string. Afterwards the
612 // register map contains the object map and the register instance_type
613 // contains the instance_type. The registers map and instance_type can be the
614 // same in which case it contains the instance type afterwards. Either of the
615 // registers map and instance_type can be the same as heap_object.
616 Condition IsObjectStringType(Register heap_object,
617 Register map,
618 Register instance_type);
619
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000620 // FCmp compares and pops the two values on top of the FPU stack.
621 // The flag results are similar to integer cmp, but requires unsigned
ager@chromium.org9085a012009-05-11 19:22:57 +0000622 // jcc instructions (je, ja, jae, jb, jbe, je, and jz).
623 void FCmp();
624
ager@chromium.org5c838252010-02-19 08:53:10 +0000625 // Abort execution if argument is not a number. Used in debug code.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000626 void AbortIfNotNumber(Register object);
ager@chromium.org5c838252010-02-19 08:53:10 +0000627
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000628 // Abort execution if argument is a smi. Used in debug code.
629 void AbortIfSmi(Register object);
630
lrn@chromium.org25156de2010-04-06 13:10:27 +0000631 // Abort execution if argument is not a smi. Used in debug code.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000632 void AbortIfNotSmi(Register object);
lrn@chromium.org25156de2010-04-06 13:10:27 +0000633
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000634 // Abort execution if argument is not the root value with the given index.
635 void AbortIfNotRootValue(Register src,
636 Heap::RootListIndex root_value_index,
637 const char* message);
638
ager@chromium.org9085a012009-05-11 19:22:57 +0000639 // ---------------------------------------------------------------------------
640 // Exception handling
641
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000642 // Push a new try handler and link into try handler chain. The return
643 // address must be pushed before calling this helper.
ager@chromium.org9085a012009-05-11 19:22:57 +0000644 void PushTryHandler(CodeLocation try_location, HandlerType type);
645
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000646 // Unlink the stack handler on top of the stack from the try handler chain.
647 void PopTryHandler();
ager@chromium.org9085a012009-05-11 19:22:57 +0000648
649 // ---------------------------------------------------------------------------
650 // Inline caching support
651
ager@chromium.org9085a012009-05-11 19:22:57 +0000652 // Generate code for checking access rights - used for security checks
653 // on access to global objects across environments. The holder register
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000654 // is left untouched, but the scratch register and kScratchRegister,
655 // which must be different, are clobbered.
ager@chromium.org9085a012009-05-11 19:22:57 +0000656 void CheckAccessGlobalProxy(Register holder_reg,
657 Register scratch,
658 Label* miss);
659
660
661 // ---------------------------------------------------------------------------
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000662 // Allocation support
663
664 // Allocate an object in new space. If the new space is exhausted control
665 // continues at the gc_required label. The allocated object is returned in
666 // result and end of the new object is returned in result_end. The register
667 // scratch can be passed as no_reg in which case an additional object
668 // reference will be added to the reloc info. The returned pointers in result
669 // and result_end have not yet been tagged as heap objects. If
670 // result_contains_top_on_entry is true the content of result is known to be
671 // the allocation top on entry (could be result_end from a previous call to
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000672 // AllocateInNewSpace). If result_contains_top_on_entry is true scratch
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000673 // should be no_reg as it is never used.
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000674 void AllocateInNewSpace(int object_size,
675 Register result,
676 Register result_end,
677 Register scratch,
678 Label* gc_required,
679 AllocationFlags flags);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000680
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000681 void AllocateInNewSpace(int header_size,
682 ScaleFactor element_size,
683 Register element_count,
684 Register result,
685 Register result_end,
686 Register scratch,
687 Label* gc_required,
688 AllocationFlags flags);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000689
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000690 void AllocateInNewSpace(Register 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
697 // Undo allocation in new space. The object passed and objects allocated after
698 // it will no longer be allocated. Make sure that no pointers are left to the
699 // object(s) no longer allocated as they would be invalid when allocation is
700 // un-done.
701 void UndoAllocationInNewSpace(Register object);
702
ager@chromium.org3811b432009-10-28 14:53:37 +0000703 // Allocate a heap number in new space with undefined value. Returns
704 // tagged pointer in result register, or jumps to gc_required if new
705 // space is full.
706 void AllocateHeapNumber(Register result,
707 Register scratch,
708 Label* gc_required);
709
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000710 // Allocate a sequential string. All the header fields of the string object
711 // are initialized.
712 void AllocateTwoByteString(Register result,
713 Register length,
714 Register scratch1,
715 Register scratch2,
716 Register scratch3,
717 Label* gc_required);
718 void AllocateAsciiString(Register result,
719 Register length,
720 Register scratch1,
721 Register scratch2,
722 Register scratch3,
723 Label* gc_required);
724
725 // Allocate a raw cons string object. Only the map field of the result is
726 // initialized.
727 void AllocateConsString(Register result,
728 Register scratch1,
729 Register scratch2,
730 Label* gc_required);
731 void AllocateAsciiConsString(Register result,
732 Register scratch1,
733 Register scratch2,
734 Label* gc_required);
735
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000736 // ---------------------------------------------------------------------------
ager@chromium.org9085a012009-05-11 19:22:57 +0000737 // Support functions.
738
739 // Check if result is zero and op is negative.
740 void NegativeZeroTest(Register result, Register op, Label* then_label);
741
742 // Check if result is zero and op is negative in code using jump targets.
743 void NegativeZeroTest(CodeGenerator* cgen,
744 Register result,
745 Register op,
746 JumpTarget* then_target);
747
748 // Check if result is zero and any of op1 and op2 are negative.
749 // Register scratch is destroyed, and it must be different from op2.
750 void NegativeZeroTest(Register result, Register op1, Register op2,
751 Register scratch, Label* then_label);
752
753 // Try to get function prototype of a function and puts the value in
754 // the result register. Checks that the function really is a
755 // function and jumps to the miss label if the fast checks fail. The
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000756 // function register will be untouched; the other register may be
ager@chromium.org9085a012009-05-11 19:22:57 +0000757 // clobbered.
758 void TryGetFunctionPrototype(Register function,
759 Register result,
ager@chromium.org9085a012009-05-11 19:22:57 +0000760 Label* miss);
761
762 // Generates code for reporting that an illegal operation has
763 // occurred.
764 void IllegalOperation(int num_arguments);
765
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000766 // Picks out an array index from the hash field.
767 // Register use:
768 // hash - holds the index's hash. Clobbered.
769 // index - holds the overwritten index on exit.
770 void IndexFromHash(Register hash, Register index);
771
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000772 // Find the function context up the context chain.
773 void LoadContext(Register dst, int context_chain_length);
774
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000775 // Load the global function with the given index.
776 void LoadGlobalFunction(int index, Register function);
777
778 // Load the initial map from the global function. The registers
779 // function and map can be the same.
780 void LoadGlobalFunctionInitialMap(Register function, Register map);
781
ager@chromium.org9085a012009-05-11 19:22:57 +0000782 // ---------------------------------------------------------------------------
783 // Runtime calls
784
785 // Call a code stub.
786 void CallStub(CodeStub* stub);
787
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000788 // Call a code stub and return the code object called. Try to generate
789 // the code if necessary. Do not perform a GC but instead return a retry
790 // after GC failure.
lrn@chromium.org303ada72010-10-27 09:33:13 +0000791 MUST_USE_RESULT MaybeObject* TryCallStub(CodeStub* stub);
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000792
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000793 // Tail call a code stub (jump).
794 void TailCallStub(CodeStub* stub);
795
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000796 // Tail call a code stub (jump) and return the code object called. Try to
797 // generate the code if necessary. Do not perform a GC but instead return
798 // a retry after GC failure.
lrn@chromium.org303ada72010-10-27 09:33:13 +0000799 MUST_USE_RESULT MaybeObject* TryTailCallStub(CodeStub* stub);
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000800
ager@chromium.org9085a012009-05-11 19:22:57 +0000801 // Return from a code stub after popping its arguments.
802 void StubReturn(int argc);
803
804 // Call a runtime routine.
ager@chromium.org9085a012009-05-11 19:22:57 +0000805 void CallRuntime(Runtime::Function* f, int num_arguments);
806
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000807 // Call a runtime function, returning the CodeStub object called.
808 // Try to generate the stub code if necessary. Do not perform a GC
809 // but instead return a retry after GC failure.
lrn@chromium.org303ada72010-10-27 09:33:13 +0000810 MUST_USE_RESULT MaybeObject* TryCallRuntime(Runtime::Function* f,
811 int num_arguments);
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000812
ager@chromium.org9085a012009-05-11 19:22:57 +0000813 // Convenience function: Same as above, but takes the fid instead.
814 void CallRuntime(Runtime::FunctionId id, int num_arguments);
815
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000816 // Convenience function: Same as above, but takes the fid instead.
lrn@chromium.org303ada72010-10-27 09:33:13 +0000817 MUST_USE_RESULT MaybeObject* TryCallRuntime(Runtime::FunctionId id,
818 int num_arguments);
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000819
ager@chromium.org5c838252010-02-19 08:53:10 +0000820 // Convenience function: call an external reference.
821 void CallExternalReference(const ExternalReference& ext,
822 int num_arguments);
823
ager@chromium.org9085a012009-05-11 19:22:57 +0000824 // Tail call of a runtime routine (jump).
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000825 // Like JumpToExternalReference, but also takes care of passing the number
826 // of parameters.
827 void TailCallExternalReference(const ExternalReference& ext,
828 int num_arguments,
829 int result_size);
830
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +0000831 MUST_USE_RESULT MaybeObject* TryTailCallExternalReference(
832 const ExternalReference& ext, int num_arguments, int result_size);
833
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000834 // Convenience function: tail call a runtime routine (jump).
835 void TailCallRuntime(Runtime::FunctionId fid,
ager@chromium.orga1645e22009-09-09 19:27:10 +0000836 int num_arguments,
837 int result_size);
ager@chromium.org9085a012009-05-11 19:22:57 +0000838
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +0000839 MUST_USE_RESULT MaybeObject* TryTailCallRuntime(Runtime::FunctionId fid,
840 int num_arguments,
841 int result_size);
842
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000843 // Jump to a runtime routine.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000844 void JumpToExternalReference(const ExternalReference& ext, int result_size);
ager@chromium.org9085a012009-05-11 19:22:57 +0000845
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +0000846 // Jump to a runtime routine.
847 MaybeObject* TryJumpToExternalReference(const ExternalReference& ext,
848 int result_size);
lrn@chromium.org303ada72010-10-27 09:33:13 +0000849
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +0000850 // Prepares stack to put arguments (aligns and so on).
851 // WIN64 calling convention requires to put the pointer to the return value
852 // slot into rcx (rcx must be preserverd until TryCallApiFunctionAndReturn).
853 // Saves context (rsi). Clobbers rax. Allocates arg_stack_space * kPointerSize
854 // inside the exit frame (not GCed) accessible via StackSpaceOperand.
855 void PrepareCallApiFunction(int arg_stack_space);
856
857 // Calls an API function. Allocates HandleScope, extracts
858 // returned value from handle and propagates exceptions.
859 // Clobbers r12, r14, rbx and caller-save registers. Restores context.
860 // On return removes stack_space * kPointerSize (GCed).
861 MUST_USE_RESULT MaybeObject* TryCallApiFunctionAndReturn(
862 ApiFunction* function, int stack_space);
lrn@chromium.org303ada72010-10-27 09:33:13 +0000863
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000864 // Before calling a C-function from generated code, align arguments on stack.
865 // After aligning the frame, arguments must be stored in esp[0], esp[4],
866 // etc., not pushed. The argument count assumes all arguments are word sized.
867 // The number of slots reserved for arguments depends on platform. On Windows
868 // stack slots are reserved for the arguments passed in registers. On other
869 // platforms stack slots are only reserved for the arguments actually passed
870 // on the stack.
871 void PrepareCallCFunction(int num_arguments);
872
873 // Calls a C function and cleans up the space for arguments allocated
874 // by PrepareCallCFunction. The called function is not allowed to trigger a
875 // garbage collection, since that might move the code and invalidate the
876 // return address (unless this is somehow accounted for by the called
877 // function).
878 void CallCFunction(ExternalReference function, int num_arguments);
879 void CallCFunction(Register function, int num_arguments);
880
881 // Calculate the number of stack slots to reserve for arguments when calling a
882 // C function.
883 int ArgumentStackSlotsForCFunctionCall(int num_arguments);
ager@chromium.org9085a012009-05-11 19:22:57 +0000884
885 // ---------------------------------------------------------------------------
886 // Utilities
887
888 void Ret();
889
ager@chromium.org9085a012009-05-11 19:22:57 +0000890 Handle<Object> CodeObject() { return code_object_; }
891
892
893 // ---------------------------------------------------------------------------
894 // StatsCounter support
895
896 void SetCounter(StatsCounter* counter, int value);
897 void IncrementCounter(StatsCounter* counter, int value);
898 void DecrementCounter(StatsCounter* counter, int value);
899
900
901 // ---------------------------------------------------------------------------
902 // Debugging
903
904 // Calls Abort(msg) if the condition cc is not satisfied.
905 // Use --debug_code to enable.
906 void Assert(Condition cc, const char* msg);
907
ricow@chromium.org0b9f8502010-08-18 07:45:01 +0000908 void AssertFastElements(Register elements);
909
ager@chromium.org9085a012009-05-11 19:22:57 +0000910 // Like Assert(), but always enabled.
911 void Check(Condition cc, const char* msg);
912
913 // Print a message to stdout and abort execution.
914 void Abort(const char* msg);
915
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000916 // Check that the stack is aligned.
917 void CheckStackAlignment();
918
ager@chromium.org9085a012009-05-11 19:22:57 +0000919 // Verify restrictions about code generated in stubs.
920 void set_generating_stub(bool value) { generating_stub_ = value; }
921 bool generating_stub() { return generating_stub_; }
922 void set_allow_stub_calls(bool value) { allow_stub_calls_ = value; }
923 bool allow_stub_calls() { return allow_stub_calls_; }
924
925 private:
ager@chromium.org9085a012009-05-11 19:22:57 +0000926 bool generating_stub_;
927 bool allow_stub_calls_;
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000928
929 // Returns a register holding the smi value. The register MUST NOT be
930 // modified. It may be the "smi 1 constant" register.
931 Register GetSmiConstant(Smi* value);
932
933 // Moves the smi value to the destination register.
934 void LoadSmiConstant(Register dst, Smi* value);
935
ager@chromium.org5c838252010-02-19 08:53:10 +0000936 // This handle will be patched with the code object on installation.
937 Handle<Object> code_object_;
ager@chromium.org9085a012009-05-11 19:22:57 +0000938
939 // Helper functions for generating invokes.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000940 template <typename LabelType>
ager@chromium.org9085a012009-05-11 19:22:57 +0000941 void InvokePrologue(const ParameterCount& expected,
942 const ParameterCount& actual,
943 Handle<Code> code_constant,
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000944 Register code_register,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000945 LabelType* done,
ager@chromium.org9085a012009-05-11 19:22:57 +0000946 InvokeFlag flag);
947
ager@chromium.org9085a012009-05-11 19:22:57 +0000948 // Activation support.
949 void EnterFrame(StackFrame::Type type);
950 void LeaveFrame(StackFrame::Type type);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000951
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000952 void EnterExitFramePrologue(bool save_rax);
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +0000953
954 // Allocates arg_stack_space * kPointerSize memory (not GCed) on the stack
955 // accessible via StackSpaceOperand.
956 void EnterExitFrameEpilogue(int arg_stack_space);
957
958 void LeaveExitFrameEpilogue();
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000959
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000960 // Allocation support helpers.
ager@chromium.orgac091b72010-05-05 07:34:42 +0000961 // Loads the top of new-space into the result register.
ager@chromium.orgac091b72010-05-05 07:34:42 +0000962 // Otherwise the address of the new-space top is loaded into scratch (if
963 // scratch is valid), and the new-space top is loaded into result.
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000964 void LoadAllocationTopHelper(Register result,
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000965 Register scratch,
ager@chromium.orga1645e22009-09-09 19:27:10 +0000966 AllocationFlags flags);
ager@chromium.orgac091b72010-05-05 07:34:42 +0000967 // Update allocation top with value in result_end register.
968 // If scratch is valid, it contains the address of the allocation top.
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000969 void UpdateAllocationTopHelper(Register result_end, Register scratch);
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000970
971 // Helper for PopHandleScope. Allowed to perform a GC and returns
972 // NULL if gc_allowed. Does not perform a GC if !gc_allowed, and
973 // possibly returns a failure object indicating an allocation failure.
974 Object* PopHandleScopeHelper(Register saved,
975 Register scratch,
976 bool gc_allowed);
ager@chromium.org9085a012009-05-11 19:22:57 +0000977};
978
979
ager@chromium.org4af710e2009-09-15 12:20:11 +0000980// The code patcher is used to patch (typically) small parts of code e.g. for
981// debugging and other types of instrumentation. When using the code patcher
982// the exact number of bytes specified must be emitted. Is not legal to emit
983// relocation information. If any of these constraints are violated it causes
984// an assertion.
985class CodePatcher {
986 public:
987 CodePatcher(byte* address, int size);
988 virtual ~CodePatcher();
989
990 // Macro assembler to emit code.
991 MacroAssembler* masm() { return &masm_; }
992
993 private:
994 byte* address_; // The address of the code being patched.
995 int size_; // Number of bytes of the expected patch size.
996 MacroAssembler masm_; // Macro assembler used to generate the code.
997};
998
999
ager@chromium.org9085a012009-05-11 19:22:57 +00001000// -----------------------------------------------------------------------------
1001// Static helper functions.
1002
1003// Generate an Operand for loading a field from an object.
1004static inline Operand FieldOperand(Register object, int offset) {
1005 return Operand(object, offset - kHeapObjectTag);
1006}
1007
1008
1009// Generate an Operand for loading an indexed field from an object.
1010static inline Operand FieldOperand(Register object,
1011 Register index,
1012 ScaleFactor scale,
1013 int offset) {
1014 return Operand(object, index, scale, offset - kHeapObjectTag);
1015}
1016
1017
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00001018static inline Operand ContextOperand(Register context, int index) {
1019 return Operand(context, Context::SlotOffset(index));
1020}
1021
1022
1023static inline Operand GlobalObjectOperand() {
1024 return ContextOperand(rsi, Context::GLOBAL_INDEX);
1025}
1026
1027
1028// Provides access to exit frame stack space (not GCed).
1029static inline Operand StackSpaceOperand(int index) {
1030#ifdef _WIN64
1031 const int kShaddowSpace = 4;
1032 return Operand(rsp, (index + kShaddowSpace) * kPointerSize);
1033#else
1034 return Operand(rsp, index * kPointerSize);
1035#endif
1036}
1037
1038
1039
ager@chromium.org9085a012009-05-11 19:22:57 +00001040#ifdef GENERATED_CODE_COVERAGE
1041extern void LogGeneratedCodeCoverage(const char* file_line);
1042#define CODE_COVERAGE_STRINGIFY(x) #x
1043#define CODE_COVERAGE_TOSTRING(x) CODE_COVERAGE_STRINGIFY(x)
1044#define __FILE_LINE__ __FILE__ ":" CODE_COVERAGE_TOSTRING(__LINE__)
1045#define ACCESS_MASM(masm) { \
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00001046 byte* x64_coverage_function = \
ager@chromium.org9085a012009-05-11 19:22:57 +00001047 reinterpret_cast<byte*>(FUNCTION_ADDR(LogGeneratedCodeCoverage)); \
1048 masm->pushfd(); \
1049 masm->pushad(); \
1050 masm->push(Immediate(reinterpret_cast<int>(&__FILE_LINE__))); \
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00001051 masm->call(x64_coverage_function, RelocInfo::RUNTIME_ENTRY); \
ager@chromium.org9085a012009-05-11 19:22:57 +00001052 masm->pop(rax); \
1053 masm->popad(); \
1054 masm->popfd(); \
1055 } \
1056 masm->
1057#else
1058#define ACCESS_MASM(masm) masm->
1059#endif
1060
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001061// -----------------------------------------------------------------------------
1062// Template implementations.
1063
1064static int kSmiShift = kSmiTagSize + kSmiShiftSize;
1065
1066
1067template <typename LabelType>
1068void MacroAssembler::SmiNeg(Register dst,
1069 Register src,
1070 LabelType* on_smi_result) {
1071 if (dst.is(src)) {
1072 ASSERT(!dst.is(kScratchRegister));
1073 movq(kScratchRegister, src);
1074 neg(dst); // Low 32 bits are retained as zero by negation.
1075 // Test if result is zero or Smi::kMinValue.
1076 cmpq(dst, kScratchRegister);
1077 j(not_equal, on_smi_result);
1078 movq(src, kScratchRegister);
1079 } else {
1080 movq(dst, src);
1081 neg(dst);
1082 cmpq(dst, src);
1083 // If the result is zero or Smi::kMinValue, negation failed to create a smi.
1084 j(not_equal, on_smi_result);
1085 }
1086}
1087
1088
1089template <typename LabelType>
1090void MacroAssembler::SmiAdd(Register dst,
1091 Register src1,
1092 Register src2,
1093 LabelType* on_not_smi_result) {
1094 ASSERT_NOT_NULL(on_not_smi_result);
1095 ASSERT(!dst.is(src2));
1096 if (dst.is(src1)) {
1097 movq(kScratchRegister, src1);
1098 addq(kScratchRegister, src2);
1099 j(overflow, on_not_smi_result);
1100 movq(dst, kScratchRegister);
1101 } else {
1102 movq(dst, src1);
1103 addq(dst, src2);
1104 j(overflow, on_not_smi_result);
1105 }
1106}
1107
1108
1109template <typename LabelType>
1110void MacroAssembler::SmiSub(Register dst,
1111 Register src1,
1112 Register src2,
1113 LabelType* on_not_smi_result) {
1114 ASSERT_NOT_NULL(on_not_smi_result);
1115 ASSERT(!dst.is(src2));
1116 if (dst.is(src1)) {
1117 cmpq(dst, src2);
1118 j(overflow, on_not_smi_result);
1119 subq(dst, src2);
1120 } else {
1121 movq(dst, src1);
1122 subq(dst, src2);
1123 j(overflow, on_not_smi_result);
1124 }
1125}
1126
1127
1128template <typename LabelType>
1129void MacroAssembler::SmiSub(Register dst,
1130 Register src1,
1131 const Operand& src2,
1132 LabelType* on_not_smi_result) {
1133 ASSERT_NOT_NULL(on_not_smi_result);
1134 if (dst.is(src1)) {
1135 movq(kScratchRegister, src2);
1136 cmpq(src1, kScratchRegister);
1137 j(overflow, on_not_smi_result);
1138 subq(src1, kScratchRegister);
1139 } else {
1140 movq(dst, src1);
1141 subq(dst, src2);
1142 j(overflow, on_not_smi_result);
1143 }
1144}
1145
1146
1147template <typename LabelType>
1148void MacroAssembler::SmiMul(Register dst,
1149 Register src1,
1150 Register src2,
1151 LabelType* on_not_smi_result) {
1152 ASSERT(!dst.is(src2));
1153 ASSERT(!dst.is(kScratchRegister));
1154 ASSERT(!src1.is(kScratchRegister));
1155 ASSERT(!src2.is(kScratchRegister));
1156
1157 if (dst.is(src1)) {
1158 NearLabel failure, zero_correct_result;
1159 movq(kScratchRegister, src1); // Create backup for later testing.
1160 SmiToInteger64(dst, src1);
1161 imul(dst, src2);
1162 j(overflow, &failure);
1163
1164 // Check for negative zero result. If product is zero, and one
1165 // argument is negative, go to slow case.
1166 NearLabel correct_result;
1167 testq(dst, dst);
1168 j(not_zero, &correct_result);
1169
1170 movq(dst, kScratchRegister);
1171 xor_(dst, src2);
1172 j(positive, &zero_correct_result); // Result was positive zero.
1173
1174 bind(&failure); // Reused failure exit, restores src1.
1175 movq(src1, kScratchRegister);
1176 jmp(on_not_smi_result);
1177
1178 bind(&zero_correct_result);
1179 xor_(dst, dst);
1180
1181 bind(&correct_result);
1182 } else {
1183 SmiToInteger64(dst, src1);
1184 imul(dst, src2);
1185 j(overflow, on_not_smi_result);
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 // One of src1 and src2 is zero, the check whether the other is
1192 // negative.
1193 movq(kScratchRegister, src1);
1194 xor_(kScratchRegister, src2);
1195 j(negative, on_not_smi_result);
1196 bind(&correct_result);
1197 }
1198}
1199
1200
1201template <typename LabelType>
1202void MacroAssembler::SmiTryAddConstant(Register dst,
1203 Register src,
1204 Smi* constant,
1205 LabelType* on_not_smi_result) {
1206 // Does not assume that src is a smi.
1207 ASSERT_EQ(static_cast<int>(1), static_cast<int>(kSmiTagMask));
1208 ASSERT_EQ(0, kSmiTag);
1209 ASSERT(!dst.is(kScratchRegister));
1210 ASSERT(!src.is(kScratchRegister));
1211
1212 JumpIfNotSmi(src, on_not_smi_result);
1213 Register tmp = (dst.is(src) ? kScratchRegister : dst);
1214 LoadSmiConstant(tmp, constant);
1215 addq(tmp, src);
1216 j(overflow, on_not_smi_result);
1217 if (dst.is(src)) {
1218 movq(dst, tmp);
1219 }
1220}
1221
1222
1223template <typename LabelType>
1224void MacroAssembler::SmiAddConstant(Register dst,
1225 Register src,
1226 Smi* constant,
1227 LabelType* on_not_smi_result) {
1228 if (constant->value() == 0) {
1229 if (!dst.is(src)) {
1230 movq(dst, src);
1231 }
1232 } else if (dst.is(src)) {
1233 ASSERT(!dst.is(kScratchRegister));
1234
1235 LoadSmiConstant(kScratchRegister, constant);
1236 addq(kScratchRegister, src);
1237 j(overflow, on_not_smi_result);
1238 movq(dst, kScratchRegister);
1239 } else {
1240 LoadSmiConstant(dst, constant);
1241 addq(dst, src);
1242 j(overflow, on_not_smi_result);
1243 }
1244}
1245
1246
1247template <typename LabelType>
1248void MacroAssembler::SmiSubConstant(Register dst,
1249 Register src,
1250 Smi* constant,
1251 LabelType* on_not_smi_result) {
1252 if (constant->value() == 0) {
1253 if (!dst.is(src)) {
1254 movq(dst, src);
1255 }
1256 } else if (dst.is(src)) {
1257 ASSERT(!dst.is(kScratchRegister));
1258 if (constant->value() == Smi::kMinValue) {
1259 // Subtracting min-value from any non-negative value will overflow.
1260 // We test the non-negativeness before doing the subtraction.
1261 testq(src, src);
1262 j(not_sign, on_not_smi_result);
1263 LoadSmiConstant(kScratchRegister, constant);
1264 subq(dst, kScratchRegister);
1265 } else {
1266 // Subtract by adding the negation.
1267 LoadSmiConstant(kScratchRegister, Smi::FromInt(-constant->value()));
1268 addq(kScratchRegister, dst);
1269 j(overflow, on_not_smi_result);
1270 movq(dst, kScratchRegister);
1271 }
1272 } else {
1273 if (constant->value() == Smi::kMinValue) {
1274 // Subtracting min-value from any non-negative value will overflow.
1275 // We test the non-negativeness before doing the subtraction.
1276 testq(src, src);
1277 j(not_sign, on_not_smi_result);
1278 LoadSmiConstant(dst, constant);
1279 // Adding and subtracting the min-value gives the same result, it only
1280 // differs on the overflow bit, which we don't check here.
1281 addq(dst, src);
1282 } else {
1283 // Subtract by adding the negation.
1284 LoadSmiConstant(dst, Smi::FromInt(-(constant->value())));
1285 addq(dst, src);
1286 j(overflow, on_not_smi_result);
1287 }
1288 }
1289}
1290
1291
1292template <typename LabelType>
1293void MacroAssembler::SmiDiv(Register dst,
1294 Register src1,
1295 Register src2,
1296 LabelType* on_not_smi_result) {
1297 ASSERT(!src1.is(kScratchRegister));
1298 ASSERT(!src2.is(kScratchRegister));
1299 ASSERT(!dst.is(kScratchRegister));
1300 ASSERT(!src2.is(rax));
1301 ASSERT(!src2.is(rdx));
1302 ASSERT(!src1.is(rdx));
1303
1304 // Check for 0 divisor (result is +/-Infinity).
1305 NearLabel positive_divisor;
1306 testq(src2, src2);
1307 j(zero, on_not_smi_result);
1308
1309 if (src1.is(rax)) {
1310 movq(kScratchRegister, src1);
1311 }
1312 SmiToInteger32(rax, src1);
1313 // We need to rule out dividing Smi::kMinValue by -1, since that would
1314 // overflow in idiv and raise an exception.
1315 // We combine this with negative zero test (negative zero only happens
1316 // when dividing zero by a negative number).
1317
1318 // We overshoot a little and go to slow case if we divide min-value
1319 // by any negative value, not just -1.
1320 NearLabel safe_div;
1321 testl(rax, Immediate(0x7fffffff));
1322 j(not_zero, &safe_div);
1323 testq(src2, src2);
1324 if (src1.is(rax)) {
1325 j(positive, &safe_div);
1326 movq(src1, kScratchRegister);
1327 jmp(on_not_smi_result);
1328 } else {
1329 j(negative, on_not_smi_result);
1330 }
1331 bind(&safe_div);
1332
1333 SmiToInteger32(src2, src2);
1334 // Sign extend src1 into edx:eax.
1335 cdq();
1336 idivl(src2);
1337 Integer32ToSmi(src2, src2);
1338 // Check that the remainder is zero.
1339 testl(rdx, rdx);
1340 if (src1.is(rax)) {
1341 NearLabel smi_result;
1342 j(zero, &smi_result);
1343 movq(src1, kScratchRegister);
1344 jmp(on_not_smi_result);
1345 bind(&smi_result);
1346 } else {
1347 j(not_zero, on_not_smi_result);
1348 }
1349 if (!dst.is(src1) && src1.is(rax)) {
1350 movq(src1, kScratchRegister);
1351 }
1352 Integer32ToSmi(dst, rax);
1353}
1354
1355
1356template <typename LabelType>
1357void MacroAssembler::SmiMod(Register dst,
1358 Register src1,
1359 Register src2,
1360 LabelType* on_not_smi_result) {
1361 ASSERT(!dst.is(kScratchRegister));
1362 ASSERT(!src1.is(kScratchRegister));
1363 ASSERT(!src2.is(kScratchRegister));
1364 ASSERT(!src2.is(rax));
1365 ASSERT(!src2.is(rdx));
1366 ASSERT(!src1.is(rdx));
1367 ASSERT(!src1.is(src2));
1368
1369 testq(src2, src2);
1370 j(zero, on_not_smi_result);
1371
1372 if (src1.is(rax)) {
1373 movq(kScratchRegister, src1);
1374 }
1375 SmiToInteger32(rax, src1);
1376 SmiToInteger32(src2, src2);
1377
1378 // Test for the edge case of dividing Smi::kMinValue by -1 (will overflow).
1379 NearLabel safe_div;
1380 cmpl(rax, Immediate(Smi::kMinValue));
1381 j(not_equal, &safe_div);
1382 cmpl(src2, Immediate(-1));
1383 j(not_equal, &safe_div);
1384 // Retag inputs and go slow case.
1385 Integer32ToSmi(src2, src2);
1386 if (src1.is(rax)) {
1387 movq(src1, kScratchRegister);
1388 }
1389 jmp(on_not_smi_result);
1390 bind(&safe_div);
1391
1392 // Sign extend eax into edx:eax.
1393 cdq();
1394 idivl(src2);
1395 // Restore smi tags on inputs.
1396 Integer32ToSmi(src2, src2);
1397 if (src1.is(rax)) {
1398 movq(src1, kScratchRegister);
1399 }
1400 // Check for a negative zero result. If the result is zero, and the
1401 // dividend is negative, go slow to return a floating point negative zero.
1402 NearLabel smi_result;
1403 testl(rdx, rdx);
1404 j(not_zero, &smi_result);
1405 testq(src1, src1);
1406 j(negative, on_not_smi_result);
1407 bind(&smi_result);
1408 Integer32ToSmi(dst, rdx);
1409}
1410
1411
1412template <typename LabelType>
1413void MacroAssembler::SmiShiftLogicalRightConstant(
1414 Register dst, Register src, int shift_value, LabelType* on_not_smi_result) {
1415 // Logic right shift interprets its result as an *unsigned* number.
1416 if (dst.is(src)) {
1417 UNIMPLEMENTED(); // Not used.
1418 } else {
1419 movq(dst, src);
1420 if (shift_value == 0) {
1421 testq(dst, dst);
1422 j(negative, on_not_smi_result);
1423 }
1424 shr(dst, Immediate(shift_value + kSmiShift));
1425 shl(dst, Immediate(kSmiShift));
1426 }
1427}
1428
1429
1430template <typename LabelType>
1431void MacroAssembler::SmiShiftLogicalRight(Register dst,
1432 Register src1,
1433 Register src2,
1434 LabelType* on_not_smi_result) {
1435 ASSERT(!dst.is(kScratchRegister));
1436 ASSERT(!src1.is(kScratchRegister));
1437 ASSERT(!src2.is(kScratchRegister));
1438 ASSERT(!dst.is(rcx));
1439 NearLabel result_ok;
1440 if (src1.is(rcx) || src2.is(rcx)) {
1441 movq(kScratchRegister, rcx);
1442 }
1443 if (!dst.is(src1)) {
1444 movq(dst, src1);
1445 }
1446 SmiToInteger32(rcx, src2);
1447 orl(rcx, Immediate(kSmiShift));
1448 shr_cl(dst); // Shift is rcx modulo 0x1f + 32.
1449 shl(dst, Immediate(kSmiShift));
1450 testq(dst, dst);
1451 if (src1.is(rcx) || src2.is(rcx)) {
1452 NearLabel positive_result;
1453 j(positive, &positive_result);
1454 if (src1.is(rcx)) {
1455 movq(src1, kScratchRegister);
1456 } else {
1457 movq(src2, kScratchRegister);
1458 }
1459 jmp(on_not_smi_result);
1460 bind(&positive_result);
1461 } else {
1462 j(negative, on_not_smi_result); // src2 was zero and src1 negative.
1463 }
1464}
1465
1466
1467template <typename LabelType>
1468void MacroAssembler::SelectNonSmi(Register dst,
1469 Register src1,
1470 Register src2,
1471 LabelType* on_not_smis) {
1472 ASSERT(!dst.is(kScratchRegister));
1473 ASSERT(!src1.is(kScratchRegister));
1474 ASSERT(!src2.is(kScratchRegister));
1475 ASSERT(!dst.is(src1));
1476 ASSERT(!dst.is(src2));
1477 // Both operands must not be smis.
1478#ifdef DEBUG
1479 if (allow_stub_calls()) { // Check contains a stub call.
1480 Condition not_both_smis = NegateCondition(CheckBothSmi(src1, src2));
1481 Check(not_both_smis, "Both registers were smis in SelectNonSmi.");
1482 }
1483#endif
1484 ASSERT_EQ(0, kSmiTag);
1485 ASSERT_EQ(0, Smi::FromInt(0));
1486 movl(kScratchRegister, Immediate(kSmiTagMask));
1487 and_(kScratchRegister, src1);
1488 testl(kScratchRegister, src2);
1489 // If non-zero then both are smis.
1490 j(not_zero, on_not_smis);
1491
1492 // Exactly one operand is a smi.
1493 ASSERT_EQ(1, static_cast<int>(kSmiTagMask));
1494 // kScratchRegister still holds src1 & kSmiTag, which is either zero or one.
1495 subq(kScratchRegister, Immediate(1));
1496 // If src1 is a smi, then scratch register all 1s, else it is all 0s.
1497 movq(dst, src1);
1498 xor_(dst, src2);
1499 and_(dst, kScratchRegister);
1500 // If src1 is a smi, dst holds src1 ^ src2, else it is zero.
1501 xor_(dst, src1);
1502 // If src1 is a smi, dst is src2, else it is src1, i.e., the non-smi.
1503}
1504
1505
1506template <typename LabelType>
1507void MacroAssembler::JumpIfSmi(Register src, LabelType* on_smi) {
1508 ASSERT_EQ(0, kSmiTag);
1509 Condition smi = CheckSmi(src);
1510 j(smi, on_smi);
1511}
1512
1513
1514template <typename LabelType>
1515void MacroAssembler::JumpIfNotSmi(Register src, LabelType* on_not_smi) {
1516 Condition smi = CheckSmi(src);
1517 j(NegateCondition(smi), on_not_smi);
1518}
1519
1520
1521template <typename LabelType>
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +00001522void MacroAssembler::JumpUnlessNonNegativeSmi(
1523 Register src, LabelType* on_not_smi_or_negative) {
1524 Condition non_negative_smi = CheckNonNegativeSmi(src);
1525 j(NegateCondition(non_negative_smi), on_not_smi_or_negative);
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001526}
1527
1528
1529template <typename LabelType>
1530void MacroAssembler::JumpIfSmiEqualsConstant(Register src,
1531 Smi* constant,
1532 LabelType* on_equals) {
1533 SmiCompare(src, constant);
1534 j(equal, on_equals);
1535}
1536
1537
1538template <typename LabelType>
1539void MacroAssembler::JumpIfNotValidSmiValue(Register src,
1540 LabelType* on_invalid) {
1541 Condition is_valid = CheckInteger32ValidSmiValue(src);
1542 j(NegateCondition(is_valid), on_invalid);
1543}
1544
1545
1546template <typename LabelType>
1547void MacroAssembler::JumpIfUIntNotValidSmiValue(Register src,
1548 LabelType* on_invalid) {
1549 Condition is_valid = CheckUInteger32ValidSmiValue(src);
1550 j(NegateCondition(is_valid), on_invalid);
1551}
1552
1553
1554template <typename LabelType>
1555void MacroAssembler::JumpIfNotBothSmi(Register src1,
1556 Register src2,
1557 LabelType* on_not_both_smi) {
1558 Condition both_smi = CheckBothSmi(src1, src2);
1559 j(NegateCondition(both_smi), on_not_both_smi);
1560}
1561
1562
1563template <typename LabelType>
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +00001564void MacroAssembler::JumpUnlessBothNonNegativeSmi(Register src1,
1565 Register src2,
1566 LabelType* on_not_both_smi) {
1567 Condition both_smi = CheckBothNonNegativeSmi(src1, src2);
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001568 j(NegateCondition(both_smi), on_not_both_smi);
1569}
1570
1571
1572template <typename LabelType>
1573void MacroAssembler::JumpIfNotBothSequentialAsciiStrings(Register first_object,
1574 Register second_object,
1575 Register scratch1,
1576 Register scratch2,
1577 LabelType* on_fail) {
1578 // Check that both objects are not smis.
1579 Condition either_smi = CheckEitherSmi(first_object, second_object);
1580 j(either_smi, on_fail);
1581
1582 // Load instance type for both strings.
1583 movq(scratch1, FieldOperand(first_object, HeapObject::kMapOffset));
1584 movq(scratch2, FieldOperand(second_object, HeapObject::kMapOffset));
1585 movzxbl(scratch1, FieldOperand(scratch1, Map::kInstanceTypeOffset));
1586 movzxbl(scratch2, FieldOperand(scratch2, Map::kInstanceTypeOffset));
1587
1588 // Check that both are flat ascii strings.
1589 ASSERT(kNotStringTag != 0);
1590 const int kFlatAsciiStringMask =
1591 kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask;
1592 const int kFlatAsciiStringTag = ASCII_STRING_TYPE;
1593
1594 andl(scratch1, Immediate(kFlatAsciiStringMask));
1595 andl(scratch2, Immediate(kFlatAsciiStringMask));
1596 // Interleave the bits to check both scratch1 and scratch2 in one test.
1597 ASSERT_EQ(0, kFlatAsciiStringMask & (kFlatAsciiStringMask << 3));
1598 lea(scratch1, Operand(scratch1, scratch2, times_8, 0));
1599 cmpl(scratch1,
1600 Immediate(kFlatAsciiStringTag + (kFlatAsciiStringTag << 3)));
1601 j(not_equal, on_fail);
1602}
1603
1604
1605template <typename LabelType>
1606void MacroAssembler::JumpIfInstanceTypeIsNotSequentialAscii(
1607 Register instance_type,
1608 Register scratch,
1609 LabelType *failure) {
1610 if (!scratch.is(instance_type)) {
1611 movl(scratch, instance_type);
1612 }
1613
1614 const int kFlatAsciiStringMask =
1615 kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask;
1616
1617 andl(scratch, Immediate(kFlatAsciiStringMask));
1618 cmpl(scratch, Immediate(kStringTag | kSeqStringTag | kAsciiStringTag));
1619 j(not_equal, failure);
1620}
1621
1622
1623template <typename LabelType>
1624void MacroAssembler::JumpIfBothInstanceTypesAreNotSequentialAscii(
1625 Register first_object_instance_type,
1626 Register second_object_instance_type,
1627 Register scratch1,
1628 Register scratch2,
1629 LabelType* on_fail) {
1630 // Load instance type for both strings.
1631 movq(scratch1, first_object_instance_type);
1632 movq(scratch2, second_object_instance_type);
1633
1634 // Check that both are flat ascii strings.
1635 ASSERT(kNotStringTag != 0);
1636 const int kFlatAsciiStringMask =
1637 kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask;
1638 const int kFlatAsciiStringTag = ASCII_STRING_TYPE;
1639
1640 andl(scratch1, Immediate(kFlatAsciiStringMask));
1641 andl(scratch2, Immediate(kFlatAsciiStringMask));
1642 // Interleave the bits to check both scratch1 and scratch2 in one test.
1643 ASSERT_EQ(0, kFlatAsciiStringMask & (kFlatAsciiStringMask << 3));
1644 lea(scratch1, Operand(scratch1, scratch2, times_8, 0));
1645 cmpl(scratch1,
1646 Immediate(kFlatAsciiStringTag + (kFlatAsciiStringTag << 3)));
1647 j(not_equal, on_fail);
1648}
1649
1650
1651template <typename LabelType>
1652void MacroAssembler::InNewSpace(Register object,
1653 Register scratch,
1654 Condition cc,
1655 LabelType* branch) {
1656 if (Serializer::enabled()) {
1657 // Can't do arithmetic on external references if it might get serialized.
1658 // The mask isn't really an address. We load it as an external reference in
1659 // case the size of the new space is different between the snapshot maker
1660 // and the running system.
1661 if (scratch.is(object)) {
1662 movq(kScratchRegister, ExternalReference::new_space_mask());
1663 and_(scratch, kScratchRegister);
1664 } else {
1665 movq(scratch, ExternalReference::new_space_mask());
1666 and_(scratch, object);
1667 }
1668 movq(kScratchRegister, ExternalReference::new_space_start());
1669 cmpq(scratch, kScratchRegister);
1670 j(cc, branch);
1671 } else {
1672 ASSERT(is_int32(static_cast<int64_t>(Heap::NewSpaceMask())));
1673 intptr_t new_space_start =
1674 reinterpret_cast<intptr_t>(Heap::NewSpaceStart());
1675 movq(kScratchRegister, -new_space_start, RelocInfo::NONE);
1676 if (scratch.is(object)) {
1677 addq(scratch, kScratchRegister);
1678 } else {
1679 lea(scratch, Operand(object, kScratchRegister, times_1, 0));
1680 }
1681 and_(scratch, Immediate(static_cast<int32_t>(Heap::NewSpaceMask())));
1682 j(cc, branch);
1683 }
1684}
1685
1686
1687template <typename LabelType>
1688void MacroAssembler::InvokePrologue(const ParameterCount& expected,
1689 const ParameterCount& actual,
1690 Handle<Code> code_constant,
1691 Register code_register,
1692 LabelType* done,
1693 InvokeFlag flag) {
1694 bool definitely_matches = false;
1695 NearLabel invoke;
1696 if (expected.is_immediate()) {
1697 ASSERT(actual.is_immediate());
1698 if (expected.immediate() == actual.immediate()) {
1699 definitely_matches = true;
1700 } else {
1701 Set(rax, actual.immediate());
1702 if (expected.immediate() ==
1703 SharedFunctionInfo::kDontAdaptArgumentsSentinel) {
1704 // Don't worry about adapting arguments for built-ins that
1705 // don't want that done. Skip adaption code by making it look
1706 // like we have a match between expected and actual number of
1707 // arguments.
1708 definitely_matches = true;
1709 } else {
1710 Set(rbx, expected.immediate());
1711 }
1712 }
1713 } else {
1714 if (actual.is_immediate()) {
1715 // Expected is in register, actual is immediate. This is the
1716 // case when we invoke function values without going through the
1717 // IC mechanism.
1718 cmpq(expected.reg(), Immediate(actual.immediate()));
1719 j(equal, &invoke);
1720 ASSERT(expected.reg().is(rbx));
1721 Set(rax, actual.immediate());
1722 } else if (!expected.reg().is(actual.reg())) {
1723 // Both expected and actual are in (different) registers. This
1724 // is the case when we invoke functions using call and apply.
1725 cmpq(expected.reg(), actual.reg());
1726 j(equal, &invoke);
1727 ASSERT(actual.reg().is(rax));
1728 ASSERT(expected.reg().is(rbx));
1729 }
1730 }
1731
1732 if (!definitely_matches) {
1733 Handle<Code> adaptor =
1734 Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline));
1735 if (!code_constant.is_null()) {
1736 movq(rdx, code_constant, RelocInfo::EMBEDDED_OBJECT);
1737 addq(rdx, Immediate(Code::kHeaderSize - kHeapObjectTag));
1738 } else if (!code_register.is(rdx)) {
1739 movq(rdx, code_register);
1740 }
1741
1742 if (flag == CALL_FUNCTION) {
1743 Call(adaptor, RelocInfo::CODE_TARGET);
1744 jmp(done);
1745 } else {
1746 Jump(adaptor, RelocInfo::CODE_TARGET);
1747 }
1748 bind(&invoke);
1749 }
1750}
1751
ager@chromium.org9085a012009-05-11 19:22:57 +00001752
1753} } // namespace v8::internal
1754
1755#endif // V8_X64_MACRO_ASSEMBLER_X64_H_