blob: eb1f42d244b35deec43fa8225e2beb9ec4a84150 [file] [log] [blame]
vegorov@chromium.org5d6c1f52011-02-28 13:13:38 +00001// Copyright 2011 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.
whesse@chromium.orgb08986c2011-03-14 16:13:42 +000051static const Register kSmiConstantRegister = { 12 }; // r12 (callee save).
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +000052static const Register kRootRegister = { 13 }; // r13 (callee save).
53// Value of smi in kSmiConstantRegister.
54static const int kSmiConstantRegisterValue = 1;
karlklose@chromium.org8f806e82011-03-07 14:06:08 +000055// Actual value of root register is offset from the root array's start
56// to take advantage of negitive 8-bit displacement values.
57static const int kRootRegisterBias = 128;
ager@chromium.orge2902be2009-06-08 12:21:35 +000058
fschneider@chromium.org0c20e672010-01-14 15:28:53 +000059// Convenience for platform-independent signatures.
60typedef Operand MemOperand;
61
ager@chromium.org9085a012009-05-11 19:22:57 +000062// Forward declaration.
63class JumpTarget;
ricow@chromium.orgeb96f4f2011-03-09 13:41:48 +000064class CallWrapper;
ager@chromium.org9085a012009-05-11 19:22:57 +000065
ager@chromium.org4af710e2009-09-15 12:20:11 +000066struct SmiIndex {
67 SmiIndex(Register index_register, ScaleFactor scale)
68 : reg(index_register),
69 scale(scale) {}
70 Register reg;
71 ScaleFactor scale;
72};
ager@chromium.org9085a012009-05-11 19:22:57 +000073
ager@chromium.org9085a012009-05-11 19:22:57 +000074// MacroAssembler implements a collection of frequently used macros.
75class MacroAssembler: public Assembler {
76 public:
77 MacroAssembler(void* buffer, int size);
78
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000079 // Prevent the use of the RootArray during the lifetime of this
80 // scope object.
81 class NoRootArrayScope BASE_EMBEDDED {
82 public:
83 explicit NoRootArrayScope(MacroAssembler* assembler)
84 : variable_(&assembler->root_array_available_),
85 old_value_(assembler->root_array_available_) {
86 assembler->root_array_available_ = false;
87 }
88 ~NoRootArrayScope() {
89 *variable_ = old_value_;
90 }
91 private:
92 bool* variable_;
93 bool old_value_;
94 };
95
96 // Operand pointing to an external reference.
97 // May emit code to set up the scratch register. The operand is
98 // only guaranteed to be correct as long as the scratch register
99 // isn't changed.
100 // If the operand is used more than once, use a scratch register
101 // that is guaranteed not to be clobbered.
102 Operand ExternalOperand(ExternalReference reference,
103 Register scratch = kScratchRegister);
104 // Loads and stores the value of an external reference.
105 // Special case code for load and store to take advantage of
106 // load_rax/store_rax if possible/necessary.
107 // For other operations, just use:
108 // Operand operand = ExternalOperand(extref);
109 // operation(operand, ..);
110 void Load(Register destination, ExternalReference source);
111 void Store(ExternalReference destination, Register source);
112 // Loads the address of the external reference into the destination
113 // register.
114 void LoadAddress(Register destination, ExternalReference source);
115 // Returns the size of the code generated by LoadAddress.
116 // Used by CallSize(ExternalReference) to find the size of a call.
117 int LoadAddressSize(ExternalReference source);
118
119 // Operations on roots in the root-array.
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000120 void LoadRoot(Register destination, Heap::RootListIndex index);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000121 void StoreRoot(Register source, Heap::RootListIndex index);
karlklose@chromium.org8f806e82011-03-07 14:06:08 +0000122 // Load a root value where the index (or part of it) is variable.
123 // The variable_offset register is added to the fixed_offset value
124 // to get the index into the root-array.
125 void LoadRootIndexed(Register destination,
126 Register variable_offset,
127 int fixed_offset);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000128 void CompareRoot(Register with, Heap::RootListIndex index);
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000129 void CompareRoot(const Operand& with, Heap::RootListIndex index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000130 void PushRoot(Heap::RootListIndex index);
131
ager@chromium.org9085a012009-05-11 19:22:57 +0000132 // ---------------------------------------------------------------------------
133 // GC Support
134
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000135 // For page containing |object| mark region covering |addr| dirty.
136 // RecordWriteHelper only works if the object is not in new
ager@chromium.orgac091b72010-05-05 07:34:42 +0000137 // space.
138 void RecordWriteHelper(Register object,
139 Register addr,
140 Register scratch);
141
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000142 // Check if object is in new space. The condition cc can be equal or
143 // not_equal. If it is equal a jump will be done if the object is on new
144 // space. The register scratch can be object itself, but it will be clobbered.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000145 template <typename LabelType>
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000146 void InNewSpace(Register object,
147 Register scratch,
148 Condition cc,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000149 LabelType* branch);
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000150
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000151 // For page containing |object| mark region covering [object+offset]
152 // dirty. |object| is the object being stored into, |value| is the
153 // object being stored. If |offset| is zero, then the |scratch|
154 // register contains the array index into the elements array
whesse@chromium.org4a5224e2010-10-20 12:37:07 +0000155 // represented as an untagged 32-bit integer. All registers are
156 // clobbered by the operation. RecordWrite filters out smis so it
157 // does not update the write barrier if the value is a smi.
ager@chromium.org9085a012009-05-11 19:22:57 +0000158 void RecordWrite(Register object,
159 int offset,
160 Register value,
161 Register scratch);
162
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000163 // For page containing |object| mark region covering [address]
164 // dirty. |object| is the object being stored into, |value| is the
165 // object being stored. All registers are clobbered by the
166 // operation. RecordWrite filters out smis so it does not update
167 // the write barrier if the value is a smi.
168 void RecordWrite(Register object,
169 Register address,
170 Register value);
171
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000172 // For page containing |object| mark region covering [object+offset] dirty.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000173 // The value is known to not be a smi.
174 // object is the object being stored into, value is the object being stored.
175 // If offset is zero, then the scratch register contains the array index into
whesse@chromium.org4a5224e2010-10-20 12:37:07 +0000176 // the elements array represented as an untagged 32-bit integer.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000177 // All registers are clobbered by the operation.
178 void RecordWriteNonSmi(Register object,
179 int offset,
180 Register value,
181 Register scratch);
182
ager@chromium.org9085a012009-05-11 19:22:57 +0000183#ifdef ENABLE_DEBUGGER_SUPPORT
184 // ---------------------------------------------------------------------------
185 // Debugger Support
186
ager@chromium.org5c838252010-02-19 08:53:10 +0000187 void DebugBreak();
ager@chromium.org9085a012009-05-11 19:22:57 +0000188#endif
189
190 // ---------------------------------------------------------------------------
191 // Activation frames
192
193 void EnterInternalFrame() { EnterFrame(StackFrame::INTERNAL); }
194 void LeaveInternalFrame() { LeaveFrame(StackFrame::INTERNAL); }
195
196 void EnterConstructFrame() { EnterFrame(StackFrame::CONSTRUCT); }
197 void LeaveConstructFrame() { LeaveFrame(StackFrame::CONSTRUCT); }
198
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000199 // Enter specific kind of exit frame; either in normal or
200 // debug mode. Expects the number of arguments in register rax and
ager@chromium.orga1645e22009-09-09 19:27:10 +0000201 // sets up the number of arguments in register rdi and the pointer
202 // to the first argument in register rsi.
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +0000203 //
204 // Allocates arg_stack_space * kPointerSize memory (not GCed) on the stack
205 // accessible via StackSpaceOperand.
ager@chromium.org0ee099b2011-01-25 14:06:47 +0000206 void EnterExitFrame(int arg_stack_space = 0, bool save_doubles = false);
ager@chromium.org9085a012009-05-11 19:22:57 +0000207
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +0000208 // Enter specific kind of exit frame. Allocates arg_stack_space * kPointerSize
209 // memory (not GCed) on the stack accessible via StackSpaceOperand.
210 void EnterApiExitFrame(int arg_stack_space);
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000211
ager@chromium.orga1645e22009-09-09 19:27:10 +0000212 // Leave the current exit frame. Expects/provides the return value in
213 // register rax:rdx (untouched) and the pointer to the first
214 // argument in register rsi.
ager@chromium.org0ee099b2011-01-25 14:06:47 +0000215 void LeaveExitFrame(bool save_doubles = false);
ager@chromium.org9085a012009-05-11 19:22:57 +0000216
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +0000217 // Leave the current exit frame. Expects/provides the return value in
218 // register rax (untouched).
219 void LeaveApiExitFrame();
ager@chromium.org9085a012009-05-11 19:22:57 +0000220
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000221 // Push and pop the registers that can hold pointers.
ager@chromium.org0ee099b2011-01-25 14:06:47 +0000222 void PushSafepointRegisters() { Pushad(); }
223 void PopSafepointRegisters() { Popad(); }
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000224 // Store the value in register src in the safepoint register stack
225 // slot for register dst.
226 void StoreToSafepointRegisterSlot(Register dst, Register src);
vegorov@chromium.org5d6c1f52011-02-28 13:13:38 +0000227 void LoadFromSafepointRegisterSlot(Register dst, Register src);
ager@chromium.org0ee099b2011-01-25 14:06:47 +0000228
karlklose@chromium.org8f806e82011-03-07 14:06:08 +0000229 void InitializeRootRegister() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000230 ExternalReference roots_address =
231 ExternalReference::roots_address(isolate());
karlklose@chromium.org8f806e82011-03-07 14:06:08 +0000232 movq(kRootRegister, roots_address);
233 addq(kRootRegister, Immediate(kRootRegisterBias));
234 }
235
ager@chromium.org9085a012009-05-11 19:22:57 +0000236 // ---------------------------------------------------------------------------
237 // JavaScript invokes
238
239 // Invoke the JavaScript function code by either calling or jumping.
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000240 void InvokeCode(Register code,
ager@chromium.org9085a012009-05-11 19:22:57 +0000241 const ParameterCount& expected,
242 const ParameterCount& actual,
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000243 InvokeFlag flag,
ricow@chromium.orgeb96f4f2011-03-09 13:41:48 +0000244 CallWrapper* call_wrapper = NULL);
ager@chromium.org9085a012009-05-11 19:22:57 +0000245
246 void InvokeCode(Handle<Code> code,
247 const ParameterCount& expected,
248 const ParameterCount& actual,
249 RelocInfo::Mode rmode,
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000250 InvokeFlag flag,
ricow@chromium.orgeb96f4f2011-03-09 13:41:48 +0000251 CallWrapper* call_wrapper = NULL);
ager@chromium.org9085a012009-05-11 19:22:57 +0000252
253 // Invoke the JavaScript function in the given register. Changes the
254 // current context to the context in the function before invoking.
255 void InvokeFunction(Register function,
256 const ParameterCount& actual,
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000257 InvokeFlag flag,
ricow@chromium.orgeb96f4f2011-03-09 13:41:48 +0000258 CallWrapper* call_wrapper = NULL);
ager@chromium.org9085a012009-05-11 19:22:57 +0000259
ager@chromium.org5c838252010-02-19 08:53:10 +0000260 void InvokeFunction(JSFunction* function,
261 const ParameterCount& actual,
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000262 InvokeFlag flag,
ricow@chromium.orgeb96f4f2011-03-09 13:41:48 +0000263 CallWrapper* call_wrapper = NULL);
ager@chromium.org5c838252010-02-19 08:53:10 +0000264
ager@chromium.org9085a012009-05-11 19:22:57 +0000265 // Invoke specified builtin JavaScript function. Adds an entry to
266 // the unresolved list if the name does not resolve.
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000267 void InvokeBuiltin(Builtins::JavaScript id,
268 InvokeFlag flag,
ricow@chromium.orgeb96f4f2011-03-09 13:41:48 +0000269 CallWrapper* call_wrapper = NULL);
ager@chromium.org9085a012009-05-11 19:22:57 +0000270
erik.corry@gmail.com145eff52010-08-23 11:36:18 +0000271 // Store the function for the given builtin in the target register.
272 void GetBuiltinFunction(Register target, Builtins::JavaScript id);
273
ager@chromium.org9085a012009-05-11 19:22:57 +0000274 // Store the code object for the given builtin in the target register.
275 void GetBuiltinEntry(Register target, Builtins::JavaScript id);
276
ager@chromium.org4af710e2009-09-15 12:20:11 +0000277
278 // ---------------------------------------------------------------------------
279 // Smi tagging, untagging and operations on tagged smis.
280
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000281 void InitializeSmiConstantRegister() {
282 movq(kSmiConstantRegister,
283 reinterpret_cast<uint64_t>(Smi::FromInt(kSmiConstantRegisterValue)),
284 RelocInfo::NONE);
285 }
286
ager@chromium.org4af710e2009-09-15 12:20:11 +0000287 // Conversions between tagged smi values and non-tagged integer values.
288
289 // Tag an integer value. The result must be known to be a valid smi value.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000290 // Only uses the low 32 bits of the src register. Sets the N and Z flags
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000291 // based on the value of the resulting smi.
ager@chromium.org4af710e2009-09-15 12:20:11 +0000292 void Integer32ToSmi(Register dst, Register src);
293
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000294 // Stores an integer32 value into a memory field that already holds a smi.
295 void Integer32ToSmiField(const Operand& dst, Register src);
296
ager@chromium.org4af710e2009-09-15 12:20:11 +0000297 // Adds constant to src and tags the result as a smi.
298 // Result must be a valid smi.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000299 void Integer64PlusConstantToSmi(Register dst, Register src, int constant);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000300
301 // Convert smi to 32-bit integer. I.e., not sign extended into
302 // high 32 bits of destination.
303 void SmiToInteger32(Register dst, Register src);
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000304 void SmiToInteger32(Register dst, const Operand& src);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000305
306 // Convert smi to 64-bit integer (sign extended if necessary).
307 void SmiToInteger64(Register dst, Register src);
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000308 void SmiToInteger64(Register dst, const Operand& src);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000309
310 // Multiply a positive smi's integer value by a power of two.
311 // Provides result as 64-bit integer value.
312 void PositiveSmiTimesPowerOfTwoToInteger64(Register dst,
313 Register src,
314 int power);
315
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000316 // Divide a positive smi's integer value by a power of two.
317 // Provides result as 32-bit integer value.
318 void PositiveSmiDivPowerOfTwoToInteger32(Register dst,
319 Register src,
320 int power);
321
322
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +0000323 // Simple comparison of smis. Both sides must be known smis to use these,
324 // otherwise use Cmp.
325 void SmiCompare(Register smi1, Register smi2);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000326 void SmiCompare(Register dst, Smi* src);
ager@chromium.orgac091b72010-05-05 07:34:42 +0000327 void SmiCompare(Register dst, const Operand& src);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000328 void SmiCompare(const Operand& dst, Register src);
329 void SmiCompare(const Operand& dst, Smi* src);
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000330 // Compare the int32 in src register to the value of the smi stored at dst.
331 void SmiCompareInteger32(const Operand& dst, Register src);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000332 // Sets sign and zero flags depending on value of smi in register.
333 void SmiTest(Register src);
334
ager@chromium.org4af710e2009-09-15 12:20:11 +0000335 // Functions performing a check on a known or potential smi. Returns
336 // a condition that is satisfied if the check is successful.
337
338 // Is the value a tagged smi.
339 Condition CheckSmi(Register src);
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000340 Condition CheckSmi(const Operand& src);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000341
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +0000342 // Is the value a non-negative tagged smi.
343 Condition CheckNonNegativeSmi(Register src);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000344
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000345 // Are both values tagged smis.
ager@chromium.org4af710e2009-09-15 12:20:11 +0000346 Condition CheckBothSmi(Register first, Register second);
347
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +0000348 // Are both values non-negative tagged smis.
349 Condition CheckBothNonNegativeSmi(Register first, Register second);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000350
351 // Are either value a tagged smi.
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +0000352 Condition CheckEitherSmi(Register first,
353 Register second,
354 Register scratch = kScratchRegister);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000355
ager@chromium.org4af710e2009-09-15 12:20:11 +0000356 // Is the value the minimum smi value (since we are using
357 // two's complement numbers, negating the value is known to yield
358 // a non-smi value).
359 Condition CheckIsMinSmi(Register src);
360
ager@chromium.org4af710e2009-09-15 12:20:11 +0000361 // Checks whether an 32-bit integer value is a valid for conversion
362 // to a smi.
363 Condition CheckInteger32ValidSmiValue(Register src);
364
ager@chromium.org3811b432009-10-28 14:53:37 +0000365 // Checks whether an 32-bit unsigned integer value is a valid for
366 // conversion to a smi.
367 Condition CheckUInteger32ValidSmiValue(Register src);
368
ager@chromium.org0ee099b2011-01-25 14:06:47 +0000369 // Check whether src is a Smi, and set dst to zero if it is a smi,
370 // and to one if it isn't.
371 void CheckSmiToIndicator(Register dst, Register src);
372 void CheckSmiToIndicator(Register dst, const Operand& src);
373
ager@chromium.org4af710e2009-09-15 12:20:11 +0000374 // Test-and-jump functions. Typically combines a check function
375 // above with a conditional jump.
376
377 // Jump if the value cannot be represented by a smi.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000378 template <typename LabelType>
379 void JumpIfNotValidSmiValue(Register src, LabelType* on_invalid);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000380
ager@chromium.org3811b432009-10-28 14:53:37 +0000381 // Jump if the unsigned integer value cannot be represented by a smi.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000382 template <typename LabelType>
383 void JumpIfUIntNotValidSmiValue(Register src, LabelType* on_invalid);
ager@chromium.org3811b432009-10-28 14:53:37 +0000384
ager@chromium.org4af710e2009-09-15 12:20:11 +0000385 // Jump to label if the value is a tagged smi.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000386 template <typename LabelType>
387 void JumpIfSmi(Register src, LabelType* on_smi);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000388
389 // Jump to label if the value is not a tagged smi.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000390 template <typename LabelType>
391 void JumpIfNotSmi(Register src, LabelType* on_not_smi);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000392
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +0000393 // Jump to label if the value is not a non-negative tagged smi.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000394 template <typename LabelType>
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +0000395 void JumpUnlessNonNegativeSmi(Register src, LabelType* on_not_smi);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000396
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000397 // Jump to label if the value, which must be a tagged smi, has value equal
ager@chromium.org4af710e2009-09-15 12:20:11 +0000398 // to the constant.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000399 template <typename LabelType>
400 void JumpIfSmiEqualsConstant(Register src,
401 Smi* constant,
402 LabelType* on_equals);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000403
ager@chromium.org4af710e2009-09-15 12:20:11 +0000404 // Jump if either or both register are not smi values.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000405 template <typename LabelType>
406 void JumpIfNotBothSmi(Register src1,
407 Register src2,
408 LabelType* on_not_both_smi);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000409
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +0000410 // Jump if either or both register are not non-negative smi values.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000411 template <typename LabelType>
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +0000412 void JumpUnlessBothNonNegativeSmi(Register src1, Register src2,
413 LabelType* on_not_both_smi);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000414
ager@chromium.org4af710e2009-09-15 12:20:11 +0000415 // Operations on tagged smi values.
416
417 // Smis represent a subset of integers. The subset is always equivalent to
418 // a two's complement interpretation of a fixed number of bits.
419
420 // Optimistically adds an integer constant to a supposed smi.
421 // If the src is not a smi, or the result is not a smi, jump to
422 // the label.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000423 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000424 void SmiTryAddConstant(Register dst,
425 Register src,
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000426 Smi* constant,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000427 LabelType* on_not_smi_result);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000428
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000429 // Add an integer constant to a tagged smi, giving a tagged smi as result.
430 // No overflow testing on the result is done.
431 void SmiAddConstant(Register dst, Register src, Smi* constant);
432
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000433 // Add an integer constant to a tagged smi, giving a tagged smi as result.
434 // No overflow testing on the result is done.
435 void SmiAddConstant(const Operand& dst, Smi* constant);
436
ager@chromium.org4af710e2009-09-15 12:20:11 +0000437 // Add an integer constant to a tagged smi, giving a tagged smi as result,
438 // or jumping to a label if the result cannot be represented by a smi.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000439 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000440 void SmiAddConstant(Register dst,
441 Register src,
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000442 Smi* constant,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000443 LabelType* on_not_smi_result);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000444
445 // Subtract an integer constant from a tagged smi, giving a tagged smi as
ager@chromium.orgac091b72010-05-05 07:34:42 +0000446 // result. No testing on the result is done. Sets the N and Z flags
447 // based on the value of the resulting integer.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000448 void SmiSubConstant(Register dst, Register src, Smi* constant);
449
450 // Subtract an integer constant from a tagged smi, giving a tagged smi as
ager@chromium.org4af710e2009-09-15 12:20:11 +0000451 // result, or jumping to a label if the result cannot be represented by a smi.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000452 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000453 void SmiSubConstant(Register dst,
454 Register src,
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000455 Smi* constant,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000456 LabelType* on_not_smi_result);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000457
458 // Negating a smi can give a negative zero or too large positive value.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000459 // NOTICE: This operation jumps on success, not failure!
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000460 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000461 void SmiNeg(Register dst,
462 Register src,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000463 LabelType* on_smi_result);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000464
465 // Adds smi values and return the result as a smi.
466 // If dst is src1, then src1 will be destroyed, even if
467 // the operation is unsuccessful.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000468 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000469 void SmiAdd(Register dst,
470 Register src1,
471 Register src2,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000472 LabelType* on_not_smi_result);
473
474 void SmiAdd(Register dst,
475 Register src1,
476 Register src2);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000477
478 // Subtracts smi values and return the result as a smi.
479 // If dst is src1, then src1 will be destroyed, even if
480 // the operation is unsuccessful.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000481 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000482 void SmiSub(Register dst,
483 Register src1,
484 Register src2,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000485 LabelType* on_not_smi_result);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000486
ager@chromium.orgac091b72010-05-05 07:34:42 +0000487 void SmiSub(Register dst,
488 Register src1,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000489 Register src2);
490
491 template <typename LabelType>
492 void SmiSub(Register dst,
493 Register src1,
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000494 const Operand& src2,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000495 LabelType* on_not_smi_result);
496
497 void SmiSub(Register dst,
498 Register src1,
499 const Operand& src2);
ager@chromium.orgac091b72010-05-05 07:34:42 +0000500
ager@chromium.org4af710e2009-09-15 12:20:11 +0000501 // Multiplies smi values and return the result as a smi,
502 // if possible.
503 // If dst is src1, then src1 will be destroyed, even if
504 // the operation is unsuccessful.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000505 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000506 void SmiMul(Register dst,
507 Register src1,
508 Register src2,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000509 LabelType* on_not_smi_result);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000510
511 // Divides one smi by another and returns the quotient.
512 // Clobbers rax and rdx registers.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000513 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000514 void SmiDiv(Register dst,
515 Register src1,
516 Register src2,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000517 LabelType* on_not_smi_result);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000518
519 // Divides one smi by another and returns the remainder.
520 // Clobbers rax and rdx registers.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000521 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000522 void SmiMod(Register dst,
523 Register src1,
524 Register src2,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000525 LabelType* on_not_smi_result);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000526
527 // Bitwise operations.
528 void SmiNot(Register dst, Register src);
529 void SmiAnd(Register dst, Register src1, Register src2);
530 void SmiOr(Register dst, Register src1, Register src2);
531 void SmiXor(Register dst, Register src1, Register src2);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000532 void SmiAndConstant(Register dst, Register src1, Smi* constant);
533 void SmiOrConstant(Register dst, Register src1, Smi* constant);
534 void SmiXorConstant(Register dst, Register src1, Smi* constant);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000535
536 void SmiShiftLeftConstant(Register dst,
537 Register src,
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000538 int shift_value);
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000539 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000540 void SmiShiftLogicalRightConstant(Register dst,
541 Register src,
542 int shift_value,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000543 LabelType* on_not_smi_result);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000544 void SmiShiftArithmeticRightConstant(Register dst,
545 Register src,
546 int shift_value);
547
548 // Shifts a smi value to the left, and returns the result if that is a smi.
549 // Uses and clobbers rcx, so dst may not be rcx.
550 void SmiShiftLeft(Register dst,
551 Register src1,
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000552 Register src2);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000553 // Shifts a smi value to the right, shifting in zero bits at the top, and
554 // returns the unsigned intepretation of the result if that is a smi.
555 // Uses and clobbers rcx, so dst may not be rcx.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000556 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000557 void SmiShiftLogicalRight(Register dst,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000558 Register src1,
559 Register src2,
560 LabelType* on_not_smi_result);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000561 // Shifts a smi value to the right, sign extending the top, and
562 // returns the signed intepretation of the result. That will always
563 // be a valid smi value, since it's numerically smaller than the
564 // original.
565 // Uses and clobbers rcx, so dst may not be rcx.
566 void SmiShiftArithmeticRight(Register dst,
567 Register src1,
568 Register src2);
569
570 // Specialized operations
571
572 // Select the non-smi register of two registers where exactly one is a
573 // smi. If neither are smis, jump to the failure label.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000574 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000575 void SelectNonSmi(Register dst,
576 Register src1,
577 Register src2,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000578 LabelType* on_not_smis);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000579
580 // Converts, if necessary, a smi to a combination of number and
581 // multiplier to be used as a scaled index.
582 // The src register contains a *positive* smi value. The shift is the
583 // power of two to multiply the index value by (e.g.
584 // to index by smi-value * kPointerSize, pass the smi and kPointerSizeLog2).
585 // The returned index register may be either src or dst, depending
586 // on what is most efficient. If src and dst are different registers,
587 // src is always unchanged.
588 SmiIndex SmiToIndex(Register dst, Register src, int shift);
589
590 // Converts a positive smi to a negative index.
591 SmiIndex SmiToNegativeIndex(Register dst, Register src, int shift);
592
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000593 // Basic Smi operations.
ager@chromium.org3811b432009-10-28 14:53:37 +0000594 void Move(Register dst, Smi* source) {
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000595 LoadSmiConstant(dst, source);
ager@chromium.org3811b432009-10-28 14:53:37 +0000596 }
597
598 void Move(const Operand& dst, Smi* source) {
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000599 Register constant = GetSmiConstant(source);
600 movq(dst, constant);
ager@chromium.org3811b432009-10-28 14:53:37 +0000601 }
602
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000603 void Push(Smi* smi);
604 void Test(const Operand& dst, Smi* source);
605
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000606 // ---------------------------------------------------------------------------
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000607 // String macros.
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000608
609 // If object is a string, its map is loaded into object_map.
610 template <typename LabelType>
611 void JumpIfNotString(Register object,
612 Register object_map,
613 LabelType* not_string);
614
615
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000616 template <typename LabelType>
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000617 void JumpIfNotBothSequentialAsciiStrings(Register first_object,
618 Register second_object,
619 Register scratch1,
620 Register scratch2,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000621 LabelType* on_not_both_flat_ascii);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000622
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000623 // Check whether the instance type represents a flat ascii string. Jump to the
624 // label if not. If the instance type can be scratched specify same register
625 // for both instance type and scratch.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000626 template <typename LabelType>
627 void JumpIfInstanceTypeIsNotSequentialAscii(
628 Register instance_type,
629 Register scratch,
630 LabelType *on_not_flat_ascii_string);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000631
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000632 template <typename LabelType>
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000633 void JumpIfBothInstanceTypesAreNotSequentialAscii(
634 Register first_object_instance_type,
635 Register second_object_instance_type,
636 Register scratch1,
637 Register scratch2,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000638 LabelType* on_fail);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000639
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000640 // ---------------------------------------------------------------------------
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000641 // Macro instructions.
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000642
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000643 // Load a register with a long value as efficiently as possible.
ager@chromium.orge2902be2009-06-08 12:21:35 +0000644 void Set(Register dst, int64_t x);
645 void Set(const Operand& dst, int64_t x);
ager@chromium.org9085a012009-05-11 19:22:57 +0000646
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000647 // Move if the registers are not identical.
648 void Move(Register target, Register source);
649
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000650 // Handle support
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000651 void Move(Register dst, Handle<Object> source);
652 void Move(const Operand& dst, Handle<Object> source);
653 void Cmp(Register dst, Handle<Object> source);
ager@chromium.org3e875802009-06-29 08:26:34 +0000654 void Cmp(const Operand& dst, Handle<Object> source);
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +0000655 void Cmp(Register dst, Smi* src);
656 void Cmp(const Operand& dst, Smi* src);
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000657 void Push(Handle<Object> source);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000658
659 // Emit code to discard a non-negative number of pointer-sized elements
660 // from the stack, clobbering only the rsp register.
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000661 void Drop(int stack_elements);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000662
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000663 void Call(Label* target) { call(target); }
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000664
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000665 // Control Flow
666 void Jump(Address destination, RelocInfo::Mode rmode);
667 void Jump(ExternalReference ext);
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000668 void Jump(Handle<Code> code_object, RelocInfo::Mode rmode);
669
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000670 void Call(Address destination, RelocInfo::Mode rmode);
671 void Call(ExternalReference ext);
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000672 void Call(Handle<Code> code_object, RelocInfo::Mode rmode);
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000673
ricow@chromium.orgeb96f4f2011-03-09 13:41:48 +0000674 // The size of the code generated for different call instructions.
675 int CallSize(Address destination, RelocInfo::Mode rmode) {
676 return kCallInstructionLength;
677 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000678 int CallSize(ExternalReference ext);
ricow@chromium.orgeb96f4f2011-03-09 13:41:48 +0000679 int CallSize(Handle<Code> code_object) {
680 // Code calls use 32-bit relative addressing.
681 return kShortCallInstructionLength;
682 }
683 int CallSize(Register target) {
684 // Opcode: REX_opt FF /2 m64
685 return (target.high_bit() != 0) ? 3 : 2;
686 }
687 int CallSize(const Operand& target) {
688 // Opcode: REX_opt FF /2 m64
689 return (target.requires_rex() ? 2 : 1) + target.operand_size();
690 }
691
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000692 // Emit call to the code we are currently generating.
693 void CallSelf() {
694 Handle<Code> self(reinterpret_cast<Code**>(CodeObject().location()));
695 Call(self, RelocInfo::CODE_TARGET);
696 }
697
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000698 // Non-x64 instructions.
699 // Push/pop all general purpose registers.
700 // Does not push rsp/rbp nor any of the assembler's special purpose registers
701 // (kScratchRegister, kSmiConstantRegister, kRootRegister).
702 void Pushad();
703 void Popad();
ager@chromium.org0ee099b2011-01-25 14:06:47 +0000704 // Sets the stack as after performing Popad, without actually loading the
705 // registers.
706 void Dropad();
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000707
ager@chromium.org9085a012009-05-11 19:22:57 +0000708 // Compare object type for heap object.
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000709 // Always use unsigned comparisons: above and below, not less and greater.
ager@chromium.org9085a012009-05-11 19:22:57 +0000710 // Incoming register is heap_object and outgoing register is map.
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000711 // They may be the same register, and may be kScratchRegister.
ager@chromium.org9085a012009-05-11 19:22:57 +0000712 void CmpObjectType(Register heap_object, InstanceType type, Register map);
713
714 // Compare instance type for map.
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000715 // Always use unsigned comparisons: above and below, not less and greater.
ager@chromium.org9085a012009-05-11 19:22:57 +0000716 void CmpInstanceType(Register map, InstanceType type);
717
ager@chromium.org5c838252010-02-19 08:53:10 +0000718 // Check if the map of an object is equal to a specified map and
719 // branch to label if not. Skip the smi check if not required
720 // (object is known to be a heap object)
721 void CheckMap(Register obj,
722 Handle<Map> map,
723 Label* fail,
724 bool is_heap_object);
725
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000726 // Check if the object in register heap_object is a string. Afterwards the
727 // register map contains the object map and the register instance_type
728 // contains the instance_type. The registers map and instance_type can be the
729 // same in which case it contains the instance type afterwards. Either of the
730 // registers map and instance_type can be the same as heap_object.
731 Condition IsObjectStringType(Register heap_object,
732 Register map,
733 Register instance_type);
734
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000735 // FCmp compares and pops the two values on top of the FPU stack.
736 // The flag results are similar to integer cmp, but requires unsigned
ager@chromium.org9085a012009-05-11 19:22:57 +0000737 // jcc instructions (je, ja, jae, jb, jbe, je, and jz).
738 void FCmp();
739
ager@chromium.org5c838252010-02-19 08:53:10 +0000740 // Abort execution if argument is not a number. Used in debug code.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000741 void AbortIfNotNumber(Register object);
ager@chromium.org5c838252010-02-19 08:53:10 +0000742
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000743 // Abort execution if argument is a smi. Used in debug code.
744 void AbortIfSmi(Register object);
745
lrn@chromium.org25156de2010-04-06 13:10:27 +0000746 // Abort execution if argument is not a smi. Used in debug code.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000747 void AbortIfNotSmi(Register object);
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +0000748 void AbortIfNotSmi(const Operand& object);
lrn@chromium.org25156de2010-04-06 13:10:27 +0000749
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +0000750 // Abort execution if argument is a string. Used in debug code.
751 void AbortIfNotString(Register object);
752
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000753 // Abort execution if argument is not the root value with the given index.
754 void AbortIfNotRootValue(Register src,
755 Heap::RootListIndex root_value_index,
756 const char* message);
757
ager@chromium.org9085a012009-05-11 19:22:57 +0000758 // ---------------------------------------------------------------------------
759 // Exception handling
760
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000761 // Push a new try handler and link into try handler chain. The return
762 // address must be pushed before calling this helper.
ager@chromium.org9085a012009-05-11 19:22:57 +0000763 void PushTryHandler(CodeLocation try_location, HandlerType type);
764
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000765 // Unlink the stack handler on top of the stack from the try handler chain.
766 void PopTryHandler();
ager@chromium.org9085a012009-05-11 19:22:57 +0000767
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +0000768 // Activate the top handler in the try hander chain and pass the
769 // thrown value.
770 void Throw(Register value);
771
772 // Propagate an uncatchable exception out of the current JS stack.
773 void ThrowUncatchable(UncatchableExceptionType type, Register value);
774
ager@chromium.org9085a012009-05-11 19:22:57 +0000775 // ---------------------------------------------------------------------------
776 // Inline caching support
777
ager@chromium.org9085a012009-05-11 19:22:57 +0000778 // Generate code for checking access rights - used for security checks
779 // on access to global objects across environments. The holder register
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000780 // is left untouched, but the scratch register and kScratchRegister,
781 // which must be different, are clobbered.
ager@chromium.org9085a012009-05-11 19:22:57 +0000782 void CheckAccessGlobalProxy(Register holder_reg,
783 Register scratch,
784 Label* miss);
785
786
787 // ---------------------------------------------------------------------------
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000788 // Allocation support
789
790 // Allocate an object in new space. If the new space is exhausted control
791 // continues at the gc_required label. The allocated object is returned in
792 // result and end of the new object is returned in result_end. The register
793 // scratch can be passed as no_reg in which case an additional object
794 // reference will be added to the reloc info. The returned pointers in result
795 // and result_end have not yet been tagged as heap objects. If
796 // result_contains_top_on_entry is true the content of result is known to be
797 // the allocation top on entry (could be result_end from a previous call to
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000798 // AllocateInNewSpace). If result_contains_top_on_entry is true scratch
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000799 // should be no_reg as it is never used.
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000800 void AllocateInNewSpace(int object_size,
801 Register result,
802 Register result_end,
803 Register scratch,
804 Label* gc_required,
805 AllocationFlags flags);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000806
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000807 void AllocateInNewSpace(int header_size,
808 ScaleFactor element_size,
809 Register element_count,
810 Register result,
811 Register result_end,
812 Register scratch,
813 Label* gc_required,
814 AllocationFlags flags);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000815
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000816 void AllocateInNewSpace(Register object_size,
817 Register result,
818 Register result_end,
819 Register scratch,
820 Label* gc_required,
821 AllocationFlags flags);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000822
823 // Undo allocation in new space. The object passed and objects allocated after
824 // it will no longer be allocated. Make sure that no pointers are left to the
825 // object(s) no longer allocated as they would be invalid when allocation is
826 // un-done.
827 void UndoAllocationInNewSpace(Register object);
828
ager@chromium.org3811b432009-10-28 14:53:37 +0000829 // Allocate a heap number in new space with undefined value. Returns
830 // tagged pointer in result register, or jumps to gc_required if new
831 // space is full.
832 void AllocateHeapNumber(Register result,
833 Register scratch,
834 Label* gc_required);
835
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000836 // Allocate a sequential string. All the header fields of the string object
837 // are initialized.
838 void AllocateTwoByteString(Register result,
839 Register length,
840 Register scratch1,
841 Register scratch2,
842 Register scratch3,
843 Label* gc_required);
844 void AllocateAsciiString(Register result,
845 Register length,
846 Register scratch1,
847 Register scratch2,
848 Register scratch3,
849 Label* gc_required);
850
851 // Allocate a raw cons string object. Only the map field of the result is
852 // initialized.
853 void AllocateConsString(Register result,
854 Register scratch1,
855 Register scratch2,
856 Label* gc_required);
857 void AllocateAsciiConsString(Register result,
858 Register scratch1,
859 Register scratch2,
860 Label* gc_required);
861
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000862 // ---------------------------------------------------------------------------
ager@chromium.org9085a012009-05-11 19:22:57 +0000863 // Support functions.
864
865 // Check if result is zero and op is negative.
866 void NegativeZeroTest(Register result, Register op, Label* then_label);
867
868 // Check if result is zero and op is negative in code using jump targets.
869 void NegativeZeroTest(CodeGenerator* cgen,
870 Register result,
871 Register op,
872 JumpTarget* then_target);
873
874 // Check if result is zero and any of op1 and op2 are negative.
875 // Register scratch is destroyed, and it must be different from op2.
876 void NegativeZeroTest(Register result, Register op1, Register op2,
877 Register scratch, Label* then_label);
878
879 // Try to get function prototype of a function and puts the value in
880 // the result register. Checks that the function really is a
881 // function and jumps to the miss label if the fast checks fail. The
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000882 // function register will be untouched; the other register may be
ager@chromium.org9085a012009-05-11 19:22:57 +0000883 // clobbered.
884 void TryGetFunctionPrototype(Register function,
885 Register result,
ager@chromium.org9085a012009-05-11 19:22:57 +0000886 Label* miss);
887
888 // Generates code for reporting that an illegal operation has
889 // occurred.
890 void IllegalOperation(int num_arguments);
891
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000892 // Picks out an array index from the hash field.
893 // Register use:
894 // hash - holds the index's hash. Clobbered.
895 // index - holds the overwritten index on exit.
896 void IndexFromHash(Register hash, Register index);
897
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000898 // Find the function context up the context chain.
899 void LoadContext(Register dst, int context_chain_length);
900
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000901 // Load the global function with the given index.
902 void LoadGlobalFunction(int index, Register function);
903
904 // Load the initial map from the global function. The registers
905 // function and map can be the same.
906 void LoadGlobalFunctionInitialMap(Register function, Register map);
907
ager@chromium.org9085a012009-05-11 19:22:57 +0000908 // ---------------------------------------------------------------------------
909 // Runtime calls
910
911 // Call a code stub.
912 void CallStub(CodeStub* stub);
913
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000914 // Call a code stub and return the code object called. Try to generate
915 // the code if necessary. Do not perform a GC but instead return a retry
916 // after GC failure.
lrn@chromium.org303ada72010-10-27 09:33:13 +0000917 MUST_USE_RESULT MaybeObject* TryCallStub(CodeStub* stub);
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000918
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000919 // Tail call a code stub (jump).
920 void TailCallStub(CodeStub* stub);
921
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000922 // Tail call a code stub (jump) and return the code object called. Try to
923 // generate the code if necessary. Do not perform a GC but instead return
924 // a retry after GC failure.
lrn@chromium.org303ada72010-10-27 09:33:13 +0000925 MUST_USE_RESULT MaybeObject* TryTailCallStub(CodeStub* stub);
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000926
ager@chromium.org9085a012009-05-11 19:22:57 +0000927 // Return from a code stub after popping its arguments.
928 void StubReturn(int argc);
929
930 // Call a runtime routine.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000931 void CallRuntime(const Runtime::Function* f, int num_arguments);
ager@chromium.org9085a012009-05-11 19:22:57 +0000932
ager@chromium.org0ee099b2011-01-25 14:06:47 +0000933 // Call a runtime function and save the value of XMM registers.
934 void CallRuntimeSaveDoubles(Runtime::FunctionId id);
935
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000936 // Call a runtime function, returning the CodeStub object called.
937 // Try to generate the stub code if necessary. Do not perform a GC
938 // but instead return a retry after GC failure.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000939 MUST_USE_RESULT MaybeObject* TryCallRuntime(const Runtime::Function* f,
lrn@chromium.org303ada72010-10-27 09:33:13 +0000940 int num_arguments);
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000941
ager@chromium.org9085a012009-05-11 19:22:57 +0000942 // Convenience function: Same as above, but takes the fid instead.
943 void CallRuntime(Runtime::FunctionId id, int num_arguments);
944
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000945 // Convenience function: Same as above, but takes the fid instead.
lrn@chromium.org303ada72010-10-27 09:33:13 +0000946 MUST_USE_RESULT MaybeObject* TryCallRuntime(Runtime::FunctionId id,
947 int num_arguments);
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000948
ager@chromium.org5c838252010-02-19 08:53:10 +0000949 // Convenience function: call an external reference.
950 void CallExternalReference(const ExternalReference& ext,
951 int num_arguments);
952
ager@chromium.org9085a012009-05-11 19:22:57 +0000953 // Tail call of a runtime routine (jump).
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000954 // Like JumpToExternalReference, but also takes care of passing the number
955 // of parameters.
956 void TailCallExternalReference(const ExternalReference& ext,
957 int num_arguments,
958 int result_size);
959
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +0000960 MUST_USE_RESULT MaybeObject* TryTailCallExternalReference(
961 const ExternalReference& ext, int num_arguments, int result_size);
962
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000963 // Convenience function: tail call a runtime routine (jump).
964 void TailCallRuntime(Runtime::FunctionId fid,
ager@chromium.orga1645e22009-09-09 19:27:10 +0000965 int num_arguments,
966 int result_size);
ager@chromium.org9085a012009-05-11 19:22:57 +0000967
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +0000968 MUST_USE_RESULT MaybeObject* TryTailCallRuntime(Runtime::FunctionId fid,
969 int num_arguments,
970 int result_size);
971
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000972 // Jump to a runtime routine.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000973 void JumpToExternalReference(const ExternalReference& ext, int result_size);
ager@chromium.org9085a012009-05-11 19:22:57 +0000974
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +0000975 // Jump to a runtime routine.
976 MaybeObject* TryJumpToExternalReference(const ExternalReference& ext,
977 int result_size);
lrn@chromium.org303ada72010-10-27 09:33:13 +0000978
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +0000979 // Prepares stack to put arguments (aligns and so on).
980 // WIN64 calling convention requires to put the pointer to the return value
981 // slot into rcx (rcx must be preserverd until TryCallApiFunctionAndReturn).
982 // Saves context (rsi). Clobbers rax. Allocates arg_stack_space * kPointerSize
983 // inside the exit frame (not GCed) accessible via StackSpaceOperand.
984 void PrepareCallApiFunction(int arg_stack_space);
985
986 // Calls an API function. Allocates HandleScope, extracts
987 // returned value from handle and propagates exceptions.
whesse@chromium.orgb08986c2011-03-14 16:13:42 +0000988 // Clobbers r14, r15, rbx and caller-save registers. Restores context.
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +0000989 // On return removes stack_space * kPointerSize (GCed).
990 MUST_USE_RESULT MaybeObject* TryCallApiFunctionAndReturn(
991 ApiFunction* function, int stack_space);
lrn@chromium.org303ada72010-10-27 09:33:13 +0000992
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000993 // Before calling a C-function from generated code, align arguments on stack.
994 // After aligning the frame, arguments must be stored in esp[0], esp[4],
995 // etc., not pushed. The argument count assumes all arguments are word sized.
996 // The number of slots reserved for arguments depends on platform. On Windows
997 // stack slots are reserved for the arguments passed in registers. On other
998 // platforms stack slots are only reserved for the arguments actually passed
999 // on the stack.
1000 void PrepareCallCFunction(int num_arguments);
1001
1002 // Calls a C function and cleans up the space for arguments allocated
1003 // by PrepareCallCFunction. The called function is not allowed to trigger a
1004 // garbage collection, since that might move the code and invalidate the
1005 // return address (unless this is somehow accounted for by the called
1006 // function).
1007 void CallCFunction(ExternalReference function, int num_arguments);
1008 void CallCFunction(Register function, int num_arguments);
1009
1010 // Calculate the number of stack slots to reserve for arguments when calling a
1011 // C function.
1012 int ArgumentStackSlotsForCFunctionCall(int num_arguments);
ager@chromium.org9085a012009-05-11 19:22:57 +00001013
1014 // ---------------------------------------------------------------------------
1015 // Utilities
1016
1017 void Ret();
1018
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +00001019 // Return and drop arguments from stack, where the number of arguments
1020 // may be bigger than 2^16 - 1. Requires a scratch register.
1021 void Ret(int bytes_dropped, Register scratch);
1022
ager@chromium.org9085a012009-05-11 19:22:57 +00001023 Handle<Object> CodeObject() { return code_object_; }
1024
1025
1026 // ---------------------------------------------------------------------------
1027 // StatsCounter support
1028
1029 void SetCounter(StatsCounter* counter, int value);
1030 void IncrementCounter(StatsCounter* counter, int value);
1031 void DecrementCounter(StatsCounter* counter, int value);
1032
1033
1034 // ---------------------------------------------------------------------------
1035 // Debugging
1036
1037 // Calls Abort(msg) if the condition cc is not satisfied.
1038 // Use --debug_code to enable.
1039 void Assert(Condition cc, const char* msg);
1040
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00001041 void AssertFastElements(Register elements);
1042
ager@chromium.org9085a012009-05-11 19:22:57 +00001043 // Like Assert(), but always enabled.
1044 void Check(Condition cc, const char* msg);
1045
1046 // Print a message to stdout and abort execution.
1047 void Abort(const char* msg);
1048
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001049 // Check that the stack is aligned.
1050 void CheckStackAlignment();
1051
ager@chromium.org9085a012009-05-11 19:22:57 +00001052 // Verify restrictions about code generated in stubs.
1053 void set_generating_stub(bool value) { generating_stub_ = value; }
1054 bool generating_stub() { return generating_stub_; }
1055 void set_allow_stub_calls(bool value) { allow_stub_calls_ = value; }
1056 bool allow_stub_calls() { return allow_stub_calls_; }
1057
1058 private:
ager@chromium.org0ee099b2011-01-25 14:06:47 +00001059 // Order general registers are pushed by Pushad.
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00001060 // rax, rcx, rdx, rbx, rsi, rdi, r8, r9, r11, r14, r15.
ager@chromium.org0ee099b2011-01-25 14:06:47 +00001061 static int kSafepointPushRegisterIndices[Register::kNumRegisters];
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00001062 static const int kNumSafepointSavedRegisters = 11;
1063
ager@chromium.org9085a012009-05-11 19:22:57 +00001064 bool generating_stub_;
1065 bool allow_stub_calls_;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001066 bool root_array_available_;
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001067
1068 // Returns a register holding the smi value. The register MUST NOT be
1069 // modified. It may be the "smi 1 constant" register.
1070 Register GetSmiConstant(Smi* value);
1071
1072 // Moves the smi value to the destination register.
1073 void LoadSmiConstant(Register dst, Smi* value);
1074
ager@chromium.org5c838252010-02-19 08:53:10 +00001075 // This handle will be patched with the code object on installation.
1076 Handle<Object> code_object_;
ager@chromium.org9085a012009-05-11 19:22:57 +00001077
1078 // Helper functions for generating invokes.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001079 template <typename LabelType>
ager@chromium.org9085a012009-05-11 19:22:57 +00001080 void InvokePrologue(const ParameterCount& expected,
1081 const ParameterCount& actual,
1082 Handle<Code> code_constant,
ager@chromium.orgeadaf222009-06-16 09:43:10 +00001083 Register code_register,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001084 LabelType* done,
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00001085 InvokeFlag flag,
ricow@chromium.orgeb96f4f2011-03-09 13:41:48 +00001086 CallWrapper* call_wrapper);
ager@chromium.org9085a012009-05-11 19:22:57 +00001087
ager@chromium.org9085a012009-05-11 19:22:57 +00001088 // Activation support.
1089 void EnterFrame(StackFrame::Type type);
1090 void LeaveFrame(StackFrame::Type type);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001091
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001092 void EnterExitFramePrologue(bool save_rax);
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00001093
1094 // Allocates arg_stack_space * kPointerSize memory (not GCed) on the stack
1095 // accessible via StackSpaceOperand.
ager@chromium.org0ee099b2011-01-25 14:06:47 +00001096 void EnterExitFrameEpilogue(int arg_stack_space, bool save_doubles);
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00001097
1098 void LeaveExitFrameEpilogue();
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001099
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001100 // Allocation support helpers.
ager@chromium.orgac091b72010-05-05 07:34:42 +00001101 // Loads the top of new-space into the result register.
ager@chromium.orgac091b72010-05-05 07:34:42 +00001102 // Otherwise the address of the new-space top is loaded into scratch (if
1103 // scratch is valid), and the new-space top is loaded into result.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001104 void LoadAllocationTopHelper(Register result,
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001105 Register scratch,
ager@chromium.orga1645e22009-09-09 19:27:10 +00001106 AllocationFlags flags);
ager@chromium.orgac091b72010-05-05 07:34:42 +00001107 // Update allocation top with value in result_end register.
1108 // If scratch is valid, it contains the address of the allocation top.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001109 void UpdateAllocationTopHelper(Register result_end, Register scratch);
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001110
1111 // Helper for PopHandleScope. Allowed to perform a GC and returns
1112 // NULL if gc_allowed. Does not perform a GC if !gc_allowed, and
1113 // possibly returns a failure object indicating an allocation failure.
1114 Object* PopHandleScopeHelper(Register saved,
1115 Register scratch,
1116 bool gc_allowed);
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00001117
1118
1119 // Compute memory operands for safepoint stack slots.
1120 Operand SafepointRegisterSlot(Register reg);
1121 static int SafepointRegisterStackIndex(int reg_code) {
1122 return kNumSafepointRegisters - kSafepointPushRegisterIndices[reg_code] - 1;
1123 }
1124
1125 // Needs access to SafepointRegisterStackIndex for optimized frame
1126 // traversal.
1127 friend class OptimizedFrame;
ager@chromium.org9085a012009-05-11 19:22:57 +00001128};
1129
1130
ager@chromium.org4af710e2009-09-15 12:20:11 +00001131// The code patcher is used to patch (typically) small parts of code e.g. for
1132// debugging and other types of instrumentation. When using the code patcher
1133// the exact number of bytes specified must be emitted. Is not legal to emit
1134// relocation information. If any of these constraints are violated it causes
1135// an assertion.
1136class CodePatcher {
1137 public:
1138 CodePatcher(byte* address, int size);
1139 virtual ~CodePatcher();
1140
1141 // Macro assembler to emit code.
1142 MacroAssembler* masm() { return &masm_; }
1143
1144 private:
1145 byte* address_; // The address of the code being patched.
1146 int size_; // Number of bytes of the expected patch size.
1147 MacroAssembler masm_; // Macro assembler used to generate the code.
1148};
1149
1150
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00001151// Helper class for generating code or data associated with the code
ricow@chromium.orgeb96f4f2011-03-09 13:41:48 +00001152// right before or after a call instruction. As an example this can be used to
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00001153// generate safepoint data after calls for crankshaft.
ricow@chromium.orgeb96f4f2011-03-09 13:41:48 +00001154class CallWrapper {
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00001155 public:
ricow@chromium.orgeb96f4f2011-03-09 13:41:48 +00001156 CallWrapper() { }
1157 virtual ~CallWrapper() { }
1158 // Called just before emitting a call. Argument is the size of the generated
1159 // call code.
1160 virtual void BeforeCall(int call_size) = 0;
1161 // Called just after emitting a call, i.e., at the return site for the call.
1162 virtual void AfterCall() = 0;
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00001163};
1164
1165
ager@chromium.org9085a012009-05-11 19:22:57 +00001166// -----------------------------------------------------------------------------
1167// Static helper functions.
1168
1169// Generate an Operand for loading a field from an object.
1170static inline Operand FieldOperand(Register object, int offset) {
1171 return Operand(object, offset - kHeapObjectTag);
1172}
1173
1174
1175// Generate an Operand for loading an indexed field from an object.
1176static inline Operand FieldOperand(Register object,
1177 Register index,
1178 ScaleFactor scale,
1179 int offset) {
1180 return Operand(object, index, scale, offset - kHeapObjectTag);
1181}
1182
1183
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00001184static inline Operand ContextOperand(Register context, int index) {
1185 return Operand(context, Context::SlotOffset(index));
1186}
1187
1188
1189static inline Operand GlobalObjectOperand() {
1190 return ContextOperand(rsi, Context::GLOBAL_INDEX);
1191}
1192
1193
1194// Provides access to exit frame stack space (not GCed).
1195static inline Operand StackSpaceOperand(int index) {
1196#ifdef _WIN64
1197 const int kShaddowSpace = 4;
1198 return Operand(rsp, (index + kShaddowSpace) * kPointerSize);
1199#else
1200 return Operand(rsp, index * kPointerSize);
1201#endif
1202}
1203
1204
1205
ager@chromium.org9085a012009-05-11 19:22:57 +00001206#ifdef GENERATED_CODE_COVERAGE
1207extern void LogGeneratedCodeCoverage(const char* file_line);
1208#define CODE_COVERAGE_STRINGIFY(x) #x
1209#define CODE_COVERAGE_TOSTRING(x) CODE_COVERAGE_STRINGIFY(x)
1210#define __FILE_LINE__ __FILE__ ":" CODE_COVERAGE_TOSTRING(__LINE__)
1211#define ACCESS_MASM(masm) { \
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00001212 byte* x64_coverage_function = \
ager@chromium.org9085a012009-05-11 19:22:57 +00001213 reinterpret_cast<byte*>(FUNCTION_ADDR(LogGeneratedCodeCoverage)); \
1214 masm->pushfd(); \
1215 masm->pushad(); \
1216 masm->push(Immediate(reinterpret_cast<int>(&__FILE_LINE__))); \
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00001217 masm->call(x64_coverage_function, RelocInfo::RUNTIME_ENTRY); \
ager@chromium.org9085a012009-05-11 19:22:57 +00001218 masm->pop(rax); \
1219 masm->popad(); \
1220 masm->popfd(); \
1221 } \
1222 masm->
1223#else
1224#define ACCESS_MASM(masm) masm->
1225#endif
1226
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001227// -----------------------------------------------------------------------------
1228// Template implementations.
1229
1230static int kSmiShift = kSmiTagSize + kSmiShiftSize;
1231
1232
1233template <typename LabelType>
1234void MacroAssembler::SmiNeg(Register dst,
1235 Register src,
1236 LabelType* on_smi_result) {
1237 if (dst.is(src)) {
1238 ASSERT(!dst.is(kScratchRegister));
1239 movq(kScratchRegister, src);
1240 neg(dst); // Low 32 bits are retained as zero by negation.
1241 // Test if result is zero or Smi::kMinValue.
1242 cmpq(dst, kScratchRegister);
1243 j(not_equal, on_smi_result);
1244 movq(src, kScratchRegister);
1245 } else {
1246 movq(dst, src);
1247 neg(dst);
1248 cmpq(dst, src);
1249 // If the result is zero or Smi::kMinValue, negation failed to create a smi.
1250 j(not_equal, on_smi_result);
1251 }
1252}
1253
1254
1255template <typename LabelType>
1256void MacroAssembler::SmiAdd(Register dst,
1257 Register src1,
1258 Register src2,
1259 LabelType* on_not_smi_result) {
1260 ASSERT_NOT_NULL(on_not_smi_result);
1261 ASSERT(!dst.is(src2));
1262 if (dst.is(src1)) {
1263 movq(kScratchRegister, src1);
1264 addq(kScratchRegister, src2);
1265 j(overflow, on_not_smi_result);
1266 movq(dst, kScratchRegister);
1267 } else {
1268 movq(dst, src1);
1269 addq(dst, src2);
1270 j(overflow, on_not_smi_result);
1271 }
1272}
1273
1274
1275template <typename LabelType>
1276void MacroAssembler::SmiSub(Register dst,
1277 Register src1,
1278 Register src2,
1279 LabelType* on_not_smi_result) {
1280 ASSERT_NOT_NULL(on_not_smi_result);
1281 ASSERT(!dst.is(src2));
1282 if (dst.is(src1)) {
1283 cmpq(dst, src2);
1284 j(overflow, on_not_smi_result);
1285 subq(dst, src2);
1286 } else {
1287 movq(dst, src1);
1288 subq(dst, src2);
1289 j(overflow, on_not_smi_result);
1290 }
1291}
1292
1293
1294template <typename LabelType>
1295void MacroAssembler::SmiSub(Register dst,
1296 Register src1,
1297 const Operand& src2,
1298 LabelType* on_not_smi_result) {
1299 ASSERT_NOT_NULL(on_not_smi_result);
1300 if (dst.is(src1)) {
1301 movq(kScratchRegister, src2);
1302 cmpq(src1, kScratchRegister);
1303 j(overflow, on_not_smi_result);
1304 subq(src1, kScratchRegister);
1305 } else {
1306 movq(dst, src1);
1307 subq(dst, src2);
1308 j(overflow, on_not_smi_result);
1309 }
1310}
1311
1312
1313template <typename LabelType>
1314void MacroAssembler::SmiMul(Register dst,
1315 Register src1,
1316 Register src2,
1317 LabelType* on_not_smi_result) {
1318 ASSERT(!dst.is(src2));
1319 ASSERT(!dst.is(kScratchRegister));
1320 ASSERT(!src1.is(kScratchRegister));
1321 ASSERT(!src2.is(kScratchRegister));
1322
1323 if (dst.is(src1)) {
1324 NearLabel failure, zero_correct_result;
1325 movq(kScratchRegister, src1); // Create backup for later testing.
1326 SmiToInteger64(dst, src1);
1327 imul(dst, src2);
1328 j(overflow, &failure);
1329
1330 // Check for negative zero result. If product is zero, and one
1331 // argument is negative, go to slow case.
1332 NearLabel correct_result;
1333 testq(dst, dst);
1334 j(not_zero, &correct_result);
1335
1336 movq(dst, kScratchRegister);
1337 xor_(dst, src2);
1338 j(positive, &zero_correct_result); // Result was positive zero.
1339
1340 bind(&failure); // Reused failure exit, restores src1.
1341 movq(src1, kScratchRegister);
1342 jmp(on_not_smi_result);
1343
1344 bind(&zero_correct_result);
lrn@chromium.org5d00b602011-01-05 09:51:43 +00001345 Set(dst, 0);
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001346
1347 bind(&correct_result);
1348 } else {
1349 SmiToInteger64(dst, src1);
1350 imul(dst, src2);
1351 j(overflow, on_not_smi_result);
1352 // Check for negative zero result. If product is zero, and one
1353 // argument is negative, go to slow case.
1354 NearLabel correct_result;
1355 testq(dst, dst);
1356 j(not_zero, &correct_result);
1357 // One of src1 and src2 is zero, the check whether the other is
1358 // negative.
1359 movq(kScratchRegister, src1);
1360 xor_(kScratchRegister, src2);
1361 j(negative, on_not_smi_result);
1362 bind(&correct_result);
1363 }
1364}
1365
1366
1367template <typename LabelType>
1368void MacroAssembler::SmiTryAddConstant(Register dst,
1369 Register src,
1370 Smi* constant,
1371 LabelType* on_not_smi_result) {
1372 // Does not assume that src is a smi.
1373 ASSERT_EQ(static_cast<int>(1), static_cast<int>(kSmiTagMask));
1374 ASSERT_EQ(0, kSmiTag);
1375 ASSERT(!dst.is(kScratchRegister));
1376 ASSERT(!src.is(kScratchRegister));
1377
1378 JumpIfNotSmi(src, on_not_smi_result);
1379 Register tmp = (dst.is(src) ? kScratchRegister : dst);
1380 LoadSmiConstant(tmp, constant);
1381 addq(tmp, src);
1382 j(overflow, on_not_smi_result);
1383 if (dst.is(src)) {
1384 movq(dst, tmp);
1385 }
1386}
1387
1388
1389template <typename LabelType>
1390void MacroAssembler::SmiAddConstant(Register dst,
1391 Register src,
1392 Smi* constant,
1393 LabelType* on_not_smi_result) {
1394 if (constant->value() == 0) {
1395 if (!dst.is(src)) {
1396 movq(dst, src);
1397 }
1398 } else if (dst.is(src)) {
1399 ASSERT(!dst.is(kScratchRegister));
1400
1401 LoadSmiConstant(kScratchRegister, constant);
1402 addq(kScratchRegister, src);
1403 j(overflow, on_not_smi_result);
1404 movq(dst, kScratchRegister);
1405 } else {
1406 LoadSmiConstant(dst, constant);
1407 addq(dst, src);
1408 j(overflow, on_not_smi_result);
1409 }
1410}
1411
1412
1413template <typename LabelType>
1414void MacroAssembler::SmiSubConstant(Register dst,
1415 Register src,
1416 Smi* constant,
1417 LabelType* on_not_smi_result) {
1418 if (constant->value() == 0) {
1419 if (!dst.is(src)) {
1420 movq(dst, src);
1421 }
1422 } else if (dst.is(src)) {
1423 ASSERT(!dst.is(kScratchRegister));
1424 if (constant->value() == Smi::kMinValue) {
1425 // Subtracting min-value from any non-negative value will overflow.
1426 // We test the non-negativeness before doing the subtraction.
1427 testq(src, src);
1428 j(not_sign, on_not_smi_result);
1429 LoadSmiConstant(kScratchRegister, constant);
1430 subq(dst, kScratchRegister);
1431 } else {
1432 // Subtract by adding the negation.
1433 LoadSmiConstant(kScratchRegister, Smi::FromInt(-constant->value()));
1434 addq(kScratchRegister, dst);
1435 j(overflow, on_not_smi_result);
1436 movq(dst, kScratchRegister);
1437 }
1438 } else {
1439 if (constant->value() == Smi::kMinValue) {
1440 // Subtracting min-value from any non-negative value will overflow.
1441 // We test the non-negativeness before doing the subtraction.
1442 testq(src, src);
1443 j(not_sign, on_not_smi_result);
1444 LoadSmiConstant(dst, constant);
1445 // Adding and subtracting the min-value gives the same result, it only
1446 // differs on the overflow bit, which we don't check here.
1447 addq(dst, src);
1448 } else {
1449 // Subtract by adding the negation.
1450 LoadSmiConstant(dst, Smi::FromInt(-(constant->value())));
1451 addq(dst, src);
1452 j(overflow, on_not_smi_result);
1453 }
1454 }
1455}
1456
1457
1458template <typename LabelType>
1459void MacroAssembler::SmiDiv(Register dst,
1460 Register src1,
1461 Register src2,
1462 LabelType* on_not_smi_result) {
1463 ASSERT(!src1.is(kScratchRegister));
1464 ASSERT(!src2.is(kScratchRegister));
1465 ASSERT(!dst.is(kScratchRegister));
1466 ASSERT(!src2.is(rax));
1467 ASSERT(!src2.is(rdx));
1468 ASSERT(!src1.is(rdx));
1469
1470 // Check for 0 divisor (result is +/-Infinity).
1471 NearLabel positive_divisor;
1472 testq(src2, src2);
1473 j(zero, on_not_smi_result);
1474
1475 if (src1.is(rax)) {
1476 movq(kScratchRegister, src1);
1477 }
1478 SmiToInteger32(rax, src1);
1479 // We need to rule out dividing Smi::kMinValue by -1, since that would
1480 // overflow in idiv and raise an exception.
1481 // We combine this with negative zero test (negative zero only happens
1482 // when dividing zero by a negative number).
1483
1484 // We overshoot a little and go to slow case if we divide min-value
1485 // by any negative value, not just -1.
1486 NearLabel safe_div;
1487 testl(rax, Immediate(0x7fffffff));
1488 j(not_zero, &safe_div);
1489 testq(src2, src2);
1490 if (src1.is(rax)) {
1491 j(positive, &safe_div);
1492 movq(src1, kScratchRegister);
1493 jmp(on_not_smi_result);
1494 } else {
1495 j(negative, on_not_smi_result);
1496 }
1497 bind(&safe_div);
1498
1499 SmiToInteger32(src2, src2);
1500 // Sign extend src1 into edx:eax.
1501 cdq();
1502 idivl(src2);
1503 Integer32ToSmi(src2, src2);
1504 // Check that the remainder is zero.
1505 testl(rdx, rdx);
1506 if (src1.is(rax)) {
1507 NearLabel smi_result;
1508 j(zero, &smi_result);
1509 movq(src1, kScratchRegister);
1510 jmp(on_not_smi_result);
1511 bind(&smi_result);
1512 } else {
1513 j(not_zero, on_not_smi_result);
1514 }
1515 if (!dst.is(src1) && src1.is(rax)) {
1516 movq(src1, kScratchRegister);
1517 }
1518 Integer32ToSmi(dst, rax);
1519}
1520
1521
1522template <typename LabelType>
1523void MacroAssembler::SmiMod(Register dst,
1524 Register src1,
1525 Register src2,
1526 LabelType* on_not_smi_result) {
1527 ASSERT(!dst.is(kScratchRegister));
1528 ASSERT(!src1.is(kScratchRegister));
1529 ASSERT(!src2.is(kScratchRegister));
1530 ASSERT(!src2.is(rax));
1531 ASSERT(!src2.is(rdx));
1532 ASSERT(!src1.is(rdx));
1533 ASSERT(!src1.is(src2));
1534
1535 testq(src2, src2);
1536 j(zero, on_not_smi_result);
1537
1538 if (src1.is(rax)) {
1539 movq(kScratchRegister, src1);
1540 }
1541 SmiToInteger32(rax, src1);
1542 SmiToInteger32(src2, src2);
1543
1544 // Test for the edge case of dividing Smi::kMinValue by -1 (will overflow).
1545 NearLabel safe_div;
1546 cmpl(rax, Immediate(Smi::kMinValue));
1547 j(not_equal, &safe_div);
1548 cmpl(src2, Immediate(-1));
1549 j(not_equal, &safe_div);
1550 // Retag inputs and go slow case.
1551 Integer32ToSmi(src2, src2);
1552 if (src1.is(rax)) {
1553 movq(src1, kScratchRegister);
1554 }
1555 jmp(on_not_smi_result);
1556 bind(&safe_div);
1557
1558 // Sign extend eax into edx:eax.
1559 cdq();
1560 idivl(src2);
1561 // Restore smi tags on inputs.
1562 Integer32ToSmi(src2, src2);
1563 if (src1.is(rax)) {
1564 movq(src1, kScratchRegister);
1565 }
1566 // Check for a negative zero result. If the result is zero, and the
1567 // dividend is negative, go slow to return a floating point negative zero.
1568 NearLabel smi_result;
1569 testl(rdx, rdx);
1570 j(not_zero, &smi_result);
1571 testq(src1, src1);
1572 j(negative, on_not_smi_result);
1573 bind(&smi_result);
1574 Integer32ToSmi(dst, rdx);
1575}
1576
1577
1578template <typename LabelType>
1579void MacroAssembler::SmiShiftLogicalRightConstant(
1580 Register dst, Register src, int shift_value, LabelType* on_not_smi_result) {
1581 // Logic right shift interprets its result as an *unsigned* number.
1582 if (dst.is(src)) {
1583 UNIMPLEMENTED(); // Not used.
1584 } else {
1585 movq(dst, src);
1586 if (shift_value == 0) {
1587 testq(dst, dst);
1588 j(negative, on_not_smi_result);
1589 }
1590 shr(dst, Immediate(shift_value + kSmiShift));
1591 shl(dst, Immediate(kSmiShift));
1592 }
1593}
1594
1595
1596template <typename LabelType>
1597void MacroAssembler::SmiShiftLogicalRight(Register dst,
1598 Register src1,
1599 Register src2,
1600 LabelType* on_not_smi_result) {
1601 ASSERT(!dst.is(kScratchRegister));
1602 ASSERT(!src1.is(kScratchRegister));
1603 ASSERT(!src2.is(kScratchRegister));
1604 ASSERT(!dst.is(rcx));
ricow@chromium.org83aa5492011-02-07 12:42:56 +00001605 // dst and src1 can be the same, because the one case that bails out
1606 // is a shift by 0, which leaves dst, and therefore src1, unchanged.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001607 NearLabel result_ok;
1608 if (src1.is(rcx) || src2.is(rcx)) {
1609 movq(kScratchRegister, rcx);
1610 }
1611 if (!dst.is(src1)) {
1612 movq(dst, src1);
1613 }
1614 SmiToInteger32(rcx, src2);
1615 orl(rcx, Immediate(kSmiShift));
1616 shr_cl(dst); // Shift is rcx modulo 0x1f + 32.
1617 shl(dst, Immediate(kSmiShift));
1618 testq(dst, dst);
1619 if (src1.is(rcx) || src2.is(rcx)) {
1620 NearLabel positive_result;
1621 j(positive, &positive_result);
1622 if (src1.is(rcx)) {
1623 movq(src1, kScratchRegister);
1624 } else {
1625 movq(src2, kScratchRegister);
1626 }
1627 jmp(on_not_smi_result);
1628 bind(&positive_result);
1629 } else {
1630 j(negative, on_not_smi_result); // src2 was zero and src1 negative.
1631 }
1632}
1633
1634
1635template <typename LabelType>
1636void MacroAssembler::SelectNonSmi(Register dst,
1637 Register src1,
1638 Register src2,
1639 LabelType* on_not_smis) {
1640 ASSERT(!dst.is(kScratchRegister));
1641 ASSERT(!src1.is(kScratchRegister));
1642 ASSERT(!src2.is(kScratchRegister));
1643 ASSERT(!dst.is(src1));
1644 ASSERT(!dst.is(src2));
1645 // Both operands must not be smis.
1646#ifdef DEBUG
1647 if (allow_stub_calls()) { // Check contains a stub call.
1648 Condition not_both_smis = NegateCondition(CheckBothSmi(src1, src2));
1649 Check(not_both_smis, "Both registers were smis in SelectNonSmi.");
1650 }
1651#endif
1652 ASSERT_EQ(0, kSmiTag);
1653 ASSERT_EQ(0, Smi::FromInt(0));
1654 movl(kScratchRegister, Immediate(kSmiTagMask));
1655 and_(kScratchRegister, src1);
1656 testl(kScratchRegister, src2);
1657 // If non-zero then both are smis.
1658 j(not_zero, on_not_smis);
1659
1660 // Exactly one operand is a smi.
1661 ASSERT_EQ(1, static_cast<int>(kSmiTagMask));
1662 // kScratchRegister still holds src1 & kSmiTag, which is either zero or one.
1663 subq(kScratchRegister, Immediate(1));
1664 // If src1 is a smi, then scratch register all 1s, else it is all 0s.
1665 movq(dst, src1);
1666 xor_(dst, src2);
1667 and_(dst, kScratchRegister);
1668 // If src1 is a smi, dst holds src1 ^ src2, else it is zero.
1669 xor_(dst, src1);
1670 // If src1 is a smi, dst is src2, else it is src1, i.e., the non-smi.
1671}
1672
1673
1674template <typename LabelType>
1675void MacroAssembler::JumpIfSmi(Register src, LabelType* on_smi) {
1676 ASSERT_EQ(0, kSmiTag);
1677 Condition smi = CheckSmi(src);
1678 j(smi, on_smi);
1679}
1680
1681
1682template <typename LabelType>
1683void MacroAssembler::JumpIfNotSmi(Register src, LabelType* on_not_smi) {
1684 Condition smi = CheckSmi(src);
1685 j(NegateCondition(smi), on_not_smi);
1686}
1687
1688
1689template <typename LabelType>
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +00001690void MacroAssembler::JumpUnlessNonNegativeSmi(
1691 Register src, LabelType* on_not_smi_or_negative) {
1692 Condition non_negative_smi = CheckNonNegativeSmi(src);
1693 j(NegateCondition(non_negative_smi), on_not_smi_or_negative);
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001694}
1695
1696
1697template <typename LabelType>
1698void MacroAssembler::JumpIfSmiEqualsConstant(Register src,
1699 Smi* constant,
1700 LabelType* on_equals) {
1701 SmiCompare(src, constant);
1702 j(equal, on_equals);
1703}
1704
1705
1706template <typename LabelType>
1707void MacroAssembler::JumpIfNotValidSmiValue(Register src,
1708 LabelType* on_invalid) {
1709 Condition is_valid = CheckInteger32ValidSmiValue(src);
1710 j(NegateCondition(is_valid), on_invalid);
1711}
1712
1713
1714template <typename LabelType>
1715void MacroAssembler::JumpIfUIntNotValidSmiValue(Register src,
1716 LabelType* on_invalid) {
1717 Condition is_valid = CheckUInteger32ValidSmiValue(src);
1718 j(NegateCondition(is_valid), on_invalid);
1719}
1720
1721
1722template <typename LabelType>
1723void MacroAssembler::JumpIfNotBothSmi(Register src1,
1724 Register src2,
1725 LabelType* on_not_both_smi) {
1726 Condition both_smi = CheckBothSmi(src1, src2);
1727 j(NegateCondition(both_smi), on_not_both_smi);
1728}
1729
1730
1731template <typename LabelType>
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +00001732void MacroAssembler::JumpUnlessBothNonNegativeSmi(Register src1,
1733 Register src2,
1734 LabelType* on_not_both_smi) {
1735 Condition both_smi = CheckBothNonNegativeSmi(src1, src2);
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001736 j(NegateCondition(both_smi), on_not_both_smi);
1737}
1738
1739
1740template <typename LabelType>
ricow@chromium.org83aa5492011-02-07 12:42:56 +00001741void MacroAssembler::JumpIfNotString(Register object,
1742 Register object_map,
1743 LabelType* not_string) {
1744 Condition is_smi = CheckSmi(object);
1745 j(is_smi, not_string);
1746 CmpObjectType(object, FIRST_NONSTRING_TYPE, object_map);
1747 j(above_equal, not_string);
1748}
1749
1750
1751template <typename LabelType>
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001752void MacroAssembler::JumpIfNotBothSequentialAsciiStrings(Register first_object,
1753 Register second_object,
1754 Register scratch1,
1755 Register scratch2,
1756 LabelType* on_fail) {
1757 // Check that both objects are not smis.
1758 Condition either_smi = CheckEitherSmi(first_object, second_object);
1759 j(either_smi, on_fail);
1760
1761 // Load instance type for both strings.
1762 movq(scratch1, FieldOperand(first_object, HeapObject::kMapOffset));
1763 movq(scratch2, FieldOperand(second_object, HeapObject::kMapOffset));
1764 movzxbl(scratch1, FieldOperand(scratch1, Map::kInstanceTypeOffset));
1765 movzxbl(scratch2, FieldOperand(scratch2, Map::kInstanceTypeOffset));
1766
1767 // Check that both are flat ascii strings.
1768 ASSERT(kNotStringTag != 0);
1769 const int kFlatAsciiStringMask =
1770 kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask;
1771 const int kFlatAsciiStringTag = ASCII_STRING_TYPE;
1772
1773 andl(scratch1, Immediate(kFlatAsciiStringMask));
1774 andl(scratch2, Immediate(kFlatAsciiStringMask));
1775 // Interleave the bits to check both scratch1 and scratch2 in one test.
1776 ASSERT_EQ(0, kFlatAsciiStringMask & (kFlatAsciiStringMask << 3));
1777 lea(scratch1, Operand(scratch1, scratch2, times_8, 0));
1778 cmpl(scratch1,
1779 Immediate(kFlatAsciiStringTag + (kFlatAsciiStringTag << 3)));
1780 j(not_equal, on_fail);
1781}
1782
1783
1784template <typename LabelType>
1785void MacroAssembler::JumpIfInstanceTypeIsNotSequentialAscii(
1786 Register instance_type,
1787 Register scratch,
1788 LabelType *failure) {
1789 if (!scratch.is(instance_type)) {
1790 movl(scratch, instance_type);
1791 }
1792
1793 const int kFlatAsciiStringMask =
1794 kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask;
1795
1796 andl(scratch, Immediate(kFlatAsciiStringMask));
1797 cmpl(scratch, Immediate(kStringTag | kSeqStringTag | kAsciiStringTag));
1798 j(not_equal, failure);
1799}
1800
1801
1802template <typename LabelType>
1803void MacroAssembler::JumpIfBothInstanceTypesAreNotSequentialAscii(
1804 Register first_object_instance_type,
1805 Register second_object_instance_type,
1806 Register scratch1,
1807 Register scratch2,
1808 LabelType* on_fail) {
1809 // Load instance type for both strings.
1810 movq(scratch1, first_object_instance_type);
1811 movq(scratch2, second_object_instance_type);
1812
1813 // Check that both are flat ascii strings.
1814 ASSERT(kNotStringTag != 0);
1815 const int kFlatAsciiStringMask =
1816 kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask;
1817 const int kFlatAsciiStringTag = ASCII_STRING_TYPE;
1818
1819 andl(scratch1, Immediate(kFlatAsciiStringMask));
1820 andl(scratch2, Immediate(kFlatAsciiStringMask));
1821 // Interleave the bits to check both scratch1 and scratch2 in one test.
1822 ASSERT_EQ(0, kFlatAsciiStringMask & (kFlatAsciiStringMask << 3));
1823 lea(scratch1, Operand(scratch1, scratch2, times_8, 0));
1824 cmpl(scratch1,
1825 Immediate(kFlatAsciiStringTag + (kFlatAsciiStringTag << 3)));
1826 j(not_equal, on_fail);
1827}
1828
1829
1830template <typename LabelType>
1831void MacroAssembler::InNewSpace(Register object,
1832 Register scratch,
1833 Condition cc,
1834 LabelType* branch) {
1835 if (Serializer::enabled()) {
1836 // Can't do arithmetic on external references if it might get serialized.
1837 // The mask isn't really an address. We load it as an external reference in
1838 // case the size of the new space is different between the snapshot maker
1839 // and the running system.
1840 if (scratch.is(object)) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001841 movq(kScratchRegister, ExternalReference::new_space_mask(isolate()));
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001842 and_(scratch, kScratchRegister);
1843 } else {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001844 movq(scratch, ExternalReference::new_space_mask(isolate()));
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001845 and_(scratch, object);
1846 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001847 movq(kScratchRegister, ExternalReference::new_space_start(isolate()));
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001848 cmpq(scratch, kScratchRegister);
1849 j(cc, branch);
1850 } else {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001851 ASSERT(is_int32(static_cast<int64_t>(HEAP->NewSpaceMask())));
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001852 intptr_t new_space_start =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001853 reinterpret_cast<intptr_t>(HEAP->NewSpaceStart());
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001854 movq(kScratchRegister, -new_space_start, RelocInfo::NONE);
1855 if (scratch.is(object)) {
1856 addq(scratch, kScratchRegister);
1857 } else {
1858 lea(scratch, Operand(object, kScratchRegister, times_1, 0));
1859 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001860 and_(scratch, Immediate(static_cast<int32_t>(HEAP->NewSpaceMask())));
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001861 j(cc, branch);
1862 }
1863}
1864
1865
1866template <typename LabelType>
1867void MacroAssembler::InvokePrologue(const ParameterCount& expected,
1868 const ParameterCount& actual,
1869 Handle<Code> code_constant,
1870 Register code_register,
1871 LabelType* done,
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00001872 InvokeFlag flag,
ricow@chromium.orgeb96f4f2011-03-09 13:41:48 +00001873 CallWrapper* call_wrapper) {
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001874 bool definitely_matches = false;
1875 NearLabel invoke;
1876 if (expected.is_immediate()) {
1877 ASSERT(actual.is_immediate());
1878 if (expected.immediate() == actual.immediate()) {
1879 definitely_matches = true;
1880 } else {
1881 Set(rax, actual.immediate());
1882 if (expected.immediate() ==
1883 SharedFunctionInfo::kDontAdaptArgumentsSentinel) {
1884 // Don't worry about adapting arguments for built-ins that
1885 // don't want that done. Skip adaption code by making it look
1886 // like we have a match between expected and actual number of
1887 // arguments.
1888 definitely_matches = true;
1889 } else {
1890 Set(rbx, expected.immediate());
1891 }
1892 }
1893 } else {
1894 if (actual.is_immediate()) {
1895 // Expected is in register, actual is immediate. This is the
1896 // case when we invoke function values without going through the
1897 // IC mechanism.
1898 cmpq(expected.reg(), Immediate(actual.immediate()));
1899 j(equal, &invoke);
1900 ASSERT(expected.reg().is(rbx));
1901 Set(rax, actual.immediate());
1902 } else if (!expected.reg().is(actual.reg())) {
1903 // Both expected and actual are in (different) registers. This
1904 // is the case when we invoke functions using call and apply.
1905 cmpq(expected.reg(), actual.reg());
1906 j(equal, &invoke);
1907 ASSERT(actual.reg().is(rax));
1908 ASSERT(expected.reg().is(rbx));
1909 }
1910 }
1911
1912 if (!definitely_matches) {
1913 Handle<Code> adaptor =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001914 Handle<Code>(Isolate::Current()->builtins()->builtin(
1915 Builtins::ArgumentsAdaptorTrampoline));
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001916 if (!code_constant.is_null()) {
1917 movq(rdx, code_constant, RelocInfo::EMBEDDED_OBJECT);
1918 addq(rdx, Immediate(Code::kHeaderSize - kHeapObjectTag));
1919 } else if (!code_register.is(rdx)) {
1920 movq(rdx, code_register);
1921 }
1922
1923 if (flag == CALL_FUNCTION) {
ricow@chromium.orgeb96f4f2011-03-09 13:41:48 +00001924 if (call_wrapper != NULL) call_wrapper->BeforeCall(CallSize(adaptor));
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001925 Call(adaptor, RelocInfo::CODE_TARGET);
ricow@chromium.orgeb96f4f2011-03-09 13:41:48 +00001926 if (call_wrapper != NULL) call_wrapper->AfterCall();
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001927 jmp(done);
1928 } else {
1929 Jump(adaptor, RelocInfo::CODE_TARGET);
1930 }
1931 bind(&invoke);
1932 }
1933}
1934
ager@chromium.org9085a012009-05-11 19:22:57 +00001935
1936} } // namespace v8::internal
1937
1938#endif // V8_X64_MACRO_ASSEMBLER_X64_H_