blob: 2eeb1fd254519f733a3a3c80663fd52328459c5f [file] [log] [blame]
ager@chromium.orga1645e22009-09-09 19:27:10 +00001// Copyright 2009 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.
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +000050static const Register kScratchRegister = { 10 }; // r10.
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +000051static const Register kRootRegister = { 13 }; // r13
ager@chromium.orge2902be2009-06-08 12:21:35 +000052
fschneider@chromium.org0c20e672010-01-14 15:28:53 +000053// Convenience for platform-independent signatures.
54typedef Operand MemOperand;
55
ager@chromium.org9085a012009-05-11 19:22:57 +000056// Forward declaration.
57class JumpTarget;
58
ager@chromium.org4af710e2009-09-15 12:20:11 +000059struct SmiIndex {
60 SmiIndex(Register index_register, ScaleFactor scale)
61 : reg(index_register),
62 scale(scale) {}
63 Register reg;
64 ScaleFactor scale;
65};
ager@chromium.org9085a012009-05-11 19:22:57 +000066
ager@chromium.org9085a012009-05-11 19:22:57 +000067// MacroAssembler implements a collection of frequently used macros.
68class MacroAssembler: public Assembler {
69 public:
70 MacroAssembler(void* buffer, int size);
71
ager@chromium.org18ad94b2009-09-02 08:22:29 +000072 void LoadRoot(Register destination, Heap::RootListIndex index);
73 void CompareRoot(Register with, Heap::RootListIndex index);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +000074 void CompareRoot(Operand with, Heap::RootListIndex index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +000075 void PushRoot(Heap::RootListIndex index);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +000076 void StoreRoot(Register source, Heap::RootListIndex index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +000077
ager@chromium.org9085a012009-05-11 19:22:57 +000078 // ---------------------------------------------------------------------------
79 // GC Support
80
ager@chromium.orgac091b72010-05-05 07:34:42 +000081 // Set the remebered set bit for an address which points into an
82 // object. RecordWriteHelper only works if the object is not in new
83 // space.
84 void RecordWriteHelper(Register object,
85 Register addr,
86 Register scratch);
87
kmillikin@chromium.org4111b802010-05-03 10:34:42 +000088 // Check if object is in new space. The condition cc can be equal or
89 // not_equal. If it is equal a jump will be done if the object is on new
90 // space. The register scratch can be object itself, but it will be clobbered.
91 void InNewSpace(Register object,
92 Register scratch,
93 Condition cc,
94 Label* branch);
95
ager@chromium.org9085a012009-05-11 19:22:57 +000096 // Set the remembered set bit for [object+offset].
97 // object is the object being stored into, value is the object being stored.
98 // If offset is zero, then the scratch register contains the array index into
99 // the elements array represented as a Smi.
100 // All registers are clobbered by the operation.
101 void RecordWrite(Register object,
102 int offset,
103 Register value,
104 Register scratch);
105
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000106 // Set the remembered set bit for [object+offset].
107 // The value is known to not be a smi.
108 // object is the object being stored into, value is the object being stored.
109 // If offset is zero, then the scratch register contains the array index into
110 // the elements array represented as a Smi.
111 // All registers are clobbered by the operation.
112 void RecordWriteNonSmi(Register object,
113 int offset,
114 Register value,
115 Register scratch);
116
ager@chromium.org9085a012009-05-11 19:22:57 +0000117#ifdef ENABLE_DEBUGGER_SUPPORT
118 // ---------------------------------------------------------------------------
119 // Debugger Support
120
121 void SaveRegistersToMemory(RegList regs);
122 void RestoreRegistersFromMemory(RegList regs);
123 void PushRegistersFromMemory(RegList regs);
124 void PopRegistersToMemory(RegList regs);
125 void CopyRegistersFromStackToMemory(Register base,
126 Register scratch,
127 RegList regs);
ager@chromium.org5c838252010-02-19 08:53:10 +0000128 void DebugBreak();
ager@chromium.org9085a012009-05-11 19:22:57 +0000129#endif
130
131 // ---------------------------------------------------------------------------
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000132 // Stack limit support
133
134 // Do simple test for stack overflow. This doesn't handle an overflow.
135 void StackLimitCheck(Label* on_stack_limit_hit);
136
137 // ---------------------------------------------------------------------------
ager@chromium.org9085a012009-05-11 19:22:57 +0000138 // Activation frames
139
140 void EnterInternalFrame() { EnterFrame(StackFrame::INTERNAL); }
141 void LeaveInternalFrame() { LeaveFrame(StackFrame::INTERNAL); }
142
143 void EnterConstructFrame() { EnterFrame(StackFrame::CONSTRUCT); }
144 void LeaveConstructFrame() { LeaveFrame(StackFrame::CONSTRUCT); }
145
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000146 // Enter specific kind of exit frame; either in normal or
147 // debug mode. Expects the number of arguments in register rax and
ager@chromium.orga1645e22009-09-09 19:27:10 +0000148 // sets up the number of arguments in register rdi and the pointer
149 // to the first argument in register rsi.
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000150 void EnterExitFrame(ExitFrame::Mode mode, int result_size = 1);
ager@chromium.org9085a012009-05-11 19:22:57 +0000151
ager@chromium.orga1645e22009-09-09 19:27:10 +0000152 // Leave the current exit frame. Expects/provides the return value in
153 // register rax:rdx (untouched) and the pointer to the first
154 // argument in register rsi.
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000155 void LeaveExitFrame(ExitFrame::Mode mode, int result_size = 1);
ager@chromium.org9085a012009-05-11 19:22:57 +0000156
157
158 // ---------------------------------------------------------------------------
159 // JavaScript invokes
160
161 // Invoke the JavaScript function code by either calling or jumping.
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000162 void InvokeCode(Register code,
ager@chromium.org9085a012009-05-11 19:22:57 +0000163 const ParameterCount& expected,
164 const ParameterCount& actual,
165 InvokeFlag flag);
166
167 void InvokeCode(Handle<Code> code,
168 const ParameterCount& expected,
169 const ParameterCount& actual,
170 RelocInfo::Mode rmode,
171 InvokeFlag flag);
172
173 // Invoke the JavaScript function in the given register. Changes the
174 // current context to the context in the function before invoking.
175 void InvokeFunction(Register function,
176 const ParameterCount& actual,
177 InvokeFlag flag);
178
ager@chromium.org5c838252010-02-19 08:53:10 +0000179 void InvokeFunction(JSFunction* function,
180 const ParameterCount& actual,
181 InvokeFlag flag);
182
ager@chromium.org9085a012009-05-11 19:22:57 +0000183 // Invoke specified builtin JavaScript function. Adds an entry to
184 // the unresolved list if the name does not resolve.
185 void InvokeBuiltin(Builtins::JavaScript id, InvokeFlag flag);
186
187 // Store the code object for the given builtin in the target register.
188 void GetBuiltinEntry(Register target, Builtins::JavaScript id);
189
ager@chromium.org4af710e2009-09-15 12:20:11 +0000190
191 // ---------------------------------------------------------------------------
192 // Smi tagging, untagging and operations on tagged smis.
193
194 // Conversions between tagged smi values and non-tagged integer values.
195
196 // Tag an integer value. The result must be known to be a valid smi value.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000197 // Only uses the low 32 bits of the src register. Sets the N and Z flags
198 // based on the value of the resulting integer.
ager@chromium.org4af710e2009-09-15 12:20:11 +0000199 void Integer32ToSmi(Register dst, Register src);
200
201 // Tag an integer value if possible, or jump the integer value cannot be
202 // represented as a smi. Only uses the low 32 bit of the src registers.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000203 // NOTICE: Destroys the dst register even if unsuccessful!
ager@chromium.org4af710e2009-09-15 12:20:11 +0000204 void Integer32ToSmi(Register dst, Register src, Label* on_overflow);
205
206 // Adds constant to src and tags the result as a smi.
207 // Result must be a valid smi.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000208 void Integer64PlusConstantToSmi(Register dst, Register src, int constant);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000209
210 // Convert smi to 32-bit integer. I.e., not sign extended into
211 // high 32 bits of destination.
212 void SmiToInteger32(Register dst, Register src);
213
214 // Convert smi to 64-bit integer (sign extended if necessary).
215 void SmiToInteger64(Register dst, Register src);
216
217 // Multiply a positive smi's integer value by a power of two.
218 // Provides result as 64-bit integer value.
219 void PositiveSmiTimesPowerOfTwoToInteger64(Register dst,
220 Register src,
221 int power);
222
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000223 // Simple comparison of smis.
224 void SmiCompare(Register dst, Register src);
225 void SmiCompare(Register dst, Smi* src);
ager@chromium.orgac091b72010-05-05 07:34:42 +0000226 void SmiCompare(Register dst, const Operand& src);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000227 void SmiCompare(const Operand& dst, Register src);
228 void SmiCompare(const Operand& dst, Smi* src);
229 // Sets sign and zero flags depending on value of smi in register.
230 void SmiTest(Register src);
231
ager@chromium.org4af710e2009-09-15 12:20:11 +0000232 // Functions performing a check on a known or potential smi. Returns
233 // a condition that is satisfied if the check is successful.
234
235 // Is the value a tagged smi.
236 Condition CheckSmi(Register src);
237
ager@chromium.org4af710e2009-09-15 12:20:11 +0000238 // Is the value a positive tagged smi.
239 Condition CheckPositiveSmi(Register src);
240
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000241 // Are both values tagged smis.
ager@chromium.org4af710e2009-09-15 12:20:11 +0000242 Condition CheckBothSmi(Register first, Register second);
243
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000244 // Are both values tagged smis.
245 Condition CheckBothPositiveSmi(Register first, Register second);
246
247 // Are either value a tagged smi.
248 Condition CheckEitherSmi(Register first, Register second);
249
ager@chromium.org4af710e2009-09-15 12:20:11 +0000250 // Is the value the minimum smi value (since we are using
251 // two's complement numbers, negating the value is known to yield
252 // a non-smi value).
253 Condition CheckIsMinSmi(Register src);
254
ager@chromium.org4af710e2009-09-15 12:20:11 +0000255 // Checks whether an 32-bit integer value is a valid for conversion
256 // to a smi.
257 Condition CheckInteger32ValidSmiValue(Register src);
258
ager@chromium.org3811b432009-10-28 14:53:37 +0000259 // Checks whether an 32-bit unsigned integer value is a valid for
260 // conversion to a smi.
261 Condition CheckUInteger32ValidSmiValue(Register src);
262
ager@chromium.org4af710e2009-09-15 12:20:11 +0000263 // Test-and-jump functions. Typically combines a check function
264 // above with a conditional jump.
265
266 // Jump if the value cannot be represented by a smi.
267 void JumpIfNotValidSmiValue(Register src, Label* on_invalid);
268
ager@chromium.org3811b432009-10-28 14:53:37 +0000269 // Jump if the unsigned integer value cannot be represented by a smi.
270 void JumpIfUIntNotValidSmiValue(Register src, Label* on_invalid);
271
ager@chromium.org4af710e2009-09-15 12:20:11 +0000272 // Jump to label if the value is a tagged smi.
273 void JumpIfSmi(Register src, Label* on_smi);
274
275 // Jump to label if the value is not a tagged smi.
276 void JumpIfNotSmi(Register src, Label* on_not_smi);
277
278 // Jump to label if the value is not a positive tagged smi.
279 void JumpIfNotPositiveSmi(Register src, Label* on_not_smi);
280
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000281 // Jump to label if the value, which must be a tagged smi, has value equal
ager@chromium.org4af710e2009-09-15 12:20:11 +0000282 // to the constant.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000283 void JumpIfSmiEqualsConstant(Register src, Smi* constant, Label* on_equals);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000284
ager@chromium.org4af710e2009-09-15 12:20:11 +0000285 // Jump if either or both register are not smi values.
286 void JumpIfNotBothSmi(Register src1, Register src2, Label* on_not_both_smi);
287
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000288 // Jump if either or both register are not positive smi values.
289 void JumpIfNotBothPositiveSmi(Register src1, Register src2,
290 Label* on_not_both_smi);
291
ager@chromium.org4af710e2009-09-15 12:20:11 +0000292 // Operations on tagged smi values.
293
294 // Smis represent a subset of integers. The subset is always equivalent to
295 // a two's complement interpretation of a fixed number of bits.
296
297 // Optimistically adds an integer constant to a supposed smi.
298 // If the src is not a smi, or the result is not a smi, jump to
299 // the label.
300 void SmiTryAddConstant(Register dst,
301 Register src,
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000302 Smi* constant,
ager@chromium.org4af710e2009-09-15 12:20:11 +0000303 Label* on_not_smi_result);
304
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000305 // Add an integer constant to a tagged smi, giving a tagged smi as result.
306 // No overflow testing on the result is done.
307 void SmiAddConstant(Register dst, Register src, Smi* constant);
308
ager@chromium.org4af710e2009-09-15 12:20:11 +0000309 // Add an integer constant to a tagged smi, giving a tagged smi as result,
310 // or jumping to a label if the result cannot be represented by a smi.
ager@chromium.org4af710e2009-09-15 12:20:11 +0000311 void SmiAddConstant(Register dst,
312 Register src,
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000313 Smi* constant,
ager@chromium.org4af710e2009-09-15 12:20:11 +0000314 Label* on_not_smi_result);
315
316 // Subtract an integer constant from a tagged smi, giving a tagged smi as
ager@chromium.orgac091b72010-05-05 07:34:42 +0000317 // result. No testing on the result is done. Sets the N and Z flags
318 // based on the value of the resulting integer.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000319 void SmiSubConstant(Register dst, Register src, Smi* constant);
320
321 // Subtract an integer constant from a tagged smi, giving a tagged smi as
ager@chromium.org4af710e2009-09-15 12:20:11 +0000322 // result, or jumping to a label if the result cannot be represented by a smi.
ager@chromium.org4af710e2009-09-15 12:20:11 +0000323 void SmiSubConstant(Register dst,
324 Register src,
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000325 Smi* constant,
ager@chromium.org4af710e2009-09-15 12:20:11 +0000326 Label* on_not_smi_result);
327
328 // Negating a smi can give a negative zero or too large positive value.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000329 // NOTICE: This operation jumps on success, not failure!
ager@chromium.org4af710e2009-09-15 12:20:11 +0000330 void SmiNeg(Register dst,
331 Register src,
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000332 Label* on_smi_result);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000333
334 // Adds smi values and return the result as a smi.
335 // If dst is src1, then src1 will be destroyed, even if
336 // the operation is unsuccessful.
337 void SmiAdd(Register dst,
338 Register src1,
339 Register src2,
340 Label* on_not_smi_result);
341
342 // Subtracts smi values and return the result as a smi.
343 // If dst is src1, then src1 will be destroyed, even if
344 // the operation is unsuccessful.
345 void SmiSub(Register dst,
346 Register src1,
347 Register src2,
348 Label* on_not_smi_result);
349
ager@chromium.orgac091b72010-05-05 07:34:42 +0000350 void SmiSub(Register dst,
351 Register src1,
352 Operand const& src2,
353 Label* on_not_smi_result);
354
ager@chromium.org4af710e2009-09-15 12:20:11 +0000355 // Multiplies smi values and return the result as a smi,
356 // if possible.
357 // If dst is src1, then src1 will be destroyed, even if
358 // the operation is unsuccessful.
359 void SmiMul(Register dst,
360 Register src1,
361 Register src2,
362 Label* on_not_smi_result);
363
364 // Divides one smi by another and returns the quotient.
365 // Clobbers rax and rdx registers.
366 void SmiDiv(Register dst,
367 Register src1,
368 Register src2,
369 Label* on_not_smi_result);
370
371 // Divides one smi by another and returns the remainder.
372 // Clobbers rax and rdx registers.
373 void SmiMod(Register dst,
374 Register src1,
375 Register src2,
376 Label* on_not_smi_result);
377
378 // Bitwise operations.
379 void SmiNot(Register dst, Register src);
380 void SmiAnd(Register dst, Register src1, Register src2);
381 void SmiOr(Register dst, Register src1, Register src2);
382 void SmiXor(Register dst, Register src1, Register src2);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000383 void SmiAndConstant(Register dst, Register src1, Smi* constant);
384 void SmiOrConstant(Register dst, Register src1, Smi* constant);
385 void SmiXorConstant(Register dst, Register src1, Smi* constant);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000386
387 void SmiShiftLeftConstant(Register dst,
388 Register src,
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000389 int shift_value);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000390 void SmiShiftLogicalRightConstant(Register dst,
391 Register src,
392 int shift_value,
393 Label* on_not_smi_result);
394 void SmiShiftArithmeticRightConstant(Register dst,
395 Register src,
396 int shift_value);
397
398 // Shifts a smi value to the left, and returns the result if that is a smi.
399 // Uses and clobbers rcx, so dst may not be rcx.
400 void SmiShiftLeft(Register dst,
401 Register src1,
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000402 Register src2);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000403 // Shifts a smi value to the right, shifting in zero bits at the top, and
404 // returns the unsigned intepretation of the result if that is a smi.
405 // Uses and clobbers rcx, so dst may not be rcx.
406 void SmiShiftLogicalRight(Register dst,
407 Register src1,
408 Register src2,
409 Label* on_not_smi_result);
410 // Shifts a smi value to the right, sign extending the top, and
411 // returns the signed intepretation of the result. That will always
412 // be a valid smi value, since it's numerically smaller than the
413 // original.
414 // Uses and clobbers rcx, so dst may not be rcx.
415 void SmiShiftArithmeticRight(Register dst,
416 Register src1,
417 Register src2);
418
419 // Specialized operations
420
421 // Select the non-smi register of two registers where exactly one is a
422 // smi. If neither are smis, jump to the failure label.
423 void SelectNonSmi(Register dst,
424 Register src1,
425 Register src2,
426 Label* on_not_smis);
427
428 // Converts, if necessary, a smi to a combination of number and
429 // multiplier to be used as a scaled index.
430 // The src register contains a *positive* smi value. The shift is the
431 // power of two to multiply the index value by (e.g.
432 // to index by smi-value * kPointerSize, pass the smi and kPointerSizeLog2).
433 // The returned index register may be either src or dst, depending
434 // on what is most efficient. If src and dst are different registers,
435 // src is always unchanged.
436 SmiIndex SmiToIndex(Register dst, Register src, int shift);
437
438 // Converts a positive smi to a negative index.
439 SmiIndex SmiToNegativeIndex(Register dst, Register src, int shift);
440
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000441 // Basic Smi operations.
ager@chromium.org3811b432009-10-28 14:53:37 +0000442 void Move(Register dst, Smi* source) {
443 Set(dst, reinterpret_cast<int64_t>(source));
444 }
445
446 void Move(const Operand& dst, Smi* source) {
447 Set(dst, reinterpret_cast<int64_t>(source));
448 }
449
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000450 void Push(Smi* smi);
451 void Test(const Operand& dst, Smi* source);
452
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000453 // ---------------------------------------------------------------------------
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000454 // String macros.
455 void JumpIfNotBothSequentialAsciiStrings(Register first_object,
456 Register second_object,
457 Register scratch1,
458 Register scratch2,
459 Label* on_not_both_flat_ascii);
460
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000461 // Check whether the instance type represents a flat ascii string. Jump to the
462 // label if not. If the instance type can be scratched specify same register
463 // for both instance type and scratch.
464 void JumpIfInstanceTypeIsNotSequentialAscii(Register instance_type,
465 Register scratch,
466 Label *on_not_flat_ascii_string);
467
468 void JumpIfBothInstanceTypesAreNotSequentialAscii(
469 Register first_object_instance_type,
470 Register second_object_instance_type,
471 Register scratch1,
472 Register scratch2,
473 Label* on_fail);
474
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000475 // ---------------------------------------------------------------------------
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000476 // Macro instructions.
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000477
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000478 // Load a register with a long value as efficiently as possible.
ager@chromium.orge2902be2009-06-08 12:21:35 +0000479 void Set(Register dst, int64_t x);
480 void Set(const Operand& dst, int64_t x);
ager@chromium.org9085a012009-05-11 19:22:57 +0000481
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000482 // Handle support
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000483 void Move(Register dst, Handle<Object> source);
484 void Move(const Operand& dst, Handle<Object> source);
485 void Cmp(Register dst, Handle<Object> source);
ager@chromium.org3e875802009-06-29 08:26:34 +0000486 void Cmp(const Operand& dst, Handle<Object> source);
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000487 void Push(Handle<Object> source);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000488
489 // Emit code to discard a non-negative number of pointer-sized elements
490 // from the stack, clobbering only the rsp register.
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000491 void Drop(int stack_elements);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000492
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000493 void Call(Label* target) { call(target); }
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000494
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000495 // Control Flow
496 void Jump(Address destination, RelocInfo::Mode rmode);
497 void Jump(ExternalReference ext);
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000498 void Jump(Handle<Code> code_object, RelocInfo::Mode rmode);
499
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000500 void Call(Address destination, RelocInfo::Mode rmode);
501 void Call(ExternalReference ext);
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000502 void Call(Handle<Code> code_object, RelocInfo::Mode rmode);
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000503
ager@chromium.org9085a012009-05-11 19:22:57 +0000504 // Compare object type for heap object.
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000505 // Always use unsigned comparisons: above and below, not less and greater.
ager@chromium.org9085a012009-05-11 19:22:57 +0000506 // Incoming register is heap_object and outgoing register is map.
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000507 // They may be the same register, and may be kScratchRegister.
ager@chromium.org9085a012009-05-11 19:22:57 +0000508 void CmpObjectType(Register heap_object, InstanceType type, Register map);
509
510 // Compare instance type for map.
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000511 // Always use unsigned comparisons: above and below, not less and greater.
ager@chromium.org9085a012009-05-11 19:22:57 +0000512 void CmpInstanceType(Register map, InstanceType type);
513
ager@chromium.org5c838252010-02-19 08:53:10 +0000514 // Check if the map of an object is equal to a specified map and
515 // branch to label if not. Skip the smi check if not required
516 // (object is known to be a heap object)
517 void CheckMap(Register obj,
518 Handle<Map> map,
519 Label* fail,
520 bool is_heap_object);
521
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000522 // Check if the object in register heap_object is a string. Afterwards the
523 // register map contains the object map and the register instance_type
524 // contains the instance_type. The registers map and instance_type can be the
525 // same in which case it contains the instance type afterwards. Either of the
526 // registers map and instance_type can be the same as heap_object.
527 Condition IsObjectStringType(Register heap_object,
528 Register map,
529 Register instance_type);
530
ager@chromium.org9085a012009-05-11 19:22:57 +0000531 // FCmp is similar to integer cmp, but requires unsigned
532 // jcc instructions (je, ja, jae, jb, jbe, je, and jz).
533 void FCmp();
534
ager@chromium.org5c838252010-02-19 08:53:10 +0000535 // Abort execution if argument is not a number. Used in debug code.
536 void AbortIfNotNumber(Register object, const char* msg);
537
lrn@chromium.org25156de2010-04-06 13:10:27 +0000538 // Abort execution if argument is not a smi. Used in debug code.
539 void AbortIfNotSmi(Register object, const char* msg);
540
ager@chromium.org9085a012009-05-11 19:22:57 +0000541 // ---------------------------------------------------------------------------
542 // Exception handling
543
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000544 // Push a new try handler and link into try handler chain. The return
545 // address must be pushed before calling this helper.
ager@chromium.org9085a012009-05-11 19:22:57 +0000546 void PushTryHandler(CodeLocation try_location, HandlerType type);
547
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000548 // Unlink the stack handler on top of the stack from the try handler chain.
549 void PopTryHandler();
ager@chromium.org9085a012009-05-11 19:22:57 +0000550
551 // ---------------------------------------------------------------------------
552 // Inline caching support
553
554 // Generates code that verifies that the maps of objects in the
555 // prototype chain of object hasn't changed since the code was
556 // generated and branches to the miss label if any map has. If
557 // necessary the function also generates code for security check
558 // in case of global object holders. The scratch and holder
559 // registers are always clobbered, but the object register is only
560 // clobbered if it the same as the holder register. The function
561 // returns a register containing the holder - either object_reg or
562 // holder_reg.
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000563 // The function can optionally (when save_at_depth !=
564 // kInvalidProtoDepth) save the object at the given depth by moving
565 // it to [rsp + kPointerSize].
ager@chromium.org9085a012009-05-11 19:22:57 +0000566 Register CheckMaps(JSObject* object, Register object_reg,
567 JSObject* holder, Register holder_reg,
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000568 Register scratch,
569 int save_at_depth,
570 Label* miss);
ager@chromium.org9085a012009-05-11 19:22:57 +0000571
572 // Generate code for checking access rights - used for security checks
573 // on access to global objects across environments. The holder register
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000574 // is left untouched, but the scratch register and kScratchRegister,
575 // which must be different, are clobbered.
ager@chromium.org9085a012009-05-11 19:22:57 +0000576 void CheckAccessGlobalProxy(Register holder_reg,
577 Register scratch,
578 Label* miss);
579
580
581 // ---------------------------------------------------------------------------
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000582 // Allocation support
583
584 // Allocate an object in new space. If the new space is exhausted control
585 // continues at the gc_required label. The allocated object is returned in
586 // result and end of the new object is returned in result_end. The register
587 // scratch can be passed as no_reg in which case an additional object
588 // reference will be added to the reloc info. The returned pointers in result
589 // and result_end have not yet been tagged as heap objects. If
590 // result_contains_top_on_entry is true the content of result is known to be
591 // the allocation top on entry (could be result_end from a previous call to
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000592 // AllocateInNewSpace). If result_contains_top_on_entry is true scratch
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000593 // should be no_reg as it is never used.
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000594 void AllocateInNewSpace(int object_size,
595 Register result,
596 Register result_end,
597 Register scratch,
598 Label* gc_required,
599 AllocationFlags flags);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000600
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000601 void AllocateInNewSpace(int header_size,
602 ScaleFactor element_size,
603 Register element_count,
604 Register result,
605 Register result_end,
606 Register scratch,
607 Label* gc_required,
608 AllocationFlags flags);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000609
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000610 void AllocateInNewSpace(Register object_size,
611 Register result,
612 Register result_end,
613 Register scratch,
614 Label* gc_required,
615 AllocationFlags flags);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000616
617 // Undo allocation in new space. The object passed and objects allocated after
618 // it will no longer be allocated. Make sure that no pointers are left to the
619 // object(s) no longer allocated as they would be invalid when allocation is
620 // un-done.
621 void UndoAllocationInNewSpace(Register object);
622
ager@chromium.org3811b432009-10-28 14:53:37 +0000623 // Allocate a heap number in new space with undefined value. Returns
624 // tagged pointer in result register, or jumps to gc_required if new
625 // space is full.
626 void AllocateHeapNumber(Register result,
627 Register scratch,
628 Label* gc_required);
629
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000630 // Allocate a sequential string. All the header fields of the string object
631 // are initialized.
632 void AllocateTwoByteString(Register result,
633 Register length,
634 Register scratch1,
635 Register scratch2,
636 Register scratch3,
637 Label* gc_required);
638 void AllocateAsciiString(Register result,
639 Register length,
640 Register scratch1,
641 Register scratch2,
642 Register scratch3,
643 Label* gc_required);
644
645 // Allocate a raw cons string object. Only the map field of the result is
646 // initialized.
647 void AllocateConsString(Register result,
648 Register scratch1,
649 Register scratch2,
650 Label* gc_required);
651 void AllocateAsciiConsString(Register result,
652 Register scratch1,
653 Register scratch2,
654 Label* gc_required);
655
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000656 // ---------------------------------------------------------------------------
ager@chromium.org9085a012009-05-11 19:22:57 +0000657 // Support functions.
658
659 // Check if result is zero and op is negative.
660 void NegativeZeroTest(Register result, Register op, Label* then_label);
661
662 // Check if result is zero and op is negative in code using jump targets.
663 void NegativeZeroTest(CodeGenerator* cgen,
664 Register result,
665 Register op,
666 JumpTarget* then_target);
667
668 // Check if result is zero and any of op1 and op2 are negative.
669 // Register scratch is destroyed, and it must be different from op2.
670 void NegativeZeroTest(Register result, Register op1, Register op2,
671 Register scratch, Label* then_label);
672
673 // Try to get function prototype of a function and puts the value in
674 // the result register. Checks that the function really is a
675 // function and jumps to the miss label if the fast checks fail. The
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000676 // function register will be untouched; the other register may be
ager@chromium.org9085a012009-05-11 19:22:57 +0000677 // clobbered.
678 void TryGetFunctionPrototype(Register function,
679 Register result,
ager@chromium.org9085a012009-05-11 19:22:57 +0000680 Label* miss);
681
682 // Generates code for reporting that an illegal operation has
683 // occurred.
684 void IllegalOperation(int num_arguments);
685
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000686 // Find the function context up the context chain.
687 void LoadContext(Register dst, int context_chain_length);
688
ager@chromium.org9085a012009-05-11 19:22:57 +0000689 // ---------------------------------------------------------------------------
690 // Runtime calls
691
692 // Call a code stub.
693 void CallStub(CodeStub* stub);
694
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000695 // Tail call a code stub (jump).
696 void TailCallStub(CodeStub* stub);
697
ager@chromium.org9085a012009-05-11 19:22:57 +0000698 // Return from a code stub after popping its arguments.
699 void StubReturn(int argc);
700
701 // Call a runtime routine.
ager@chromium.org9085a012009-05-11 19:22:57 +0000702 void CallRuntime(Runtime::Function* f, int num_arguments);
703
704 // Convenience function: Same as above, but takes the fid instead.
705 void CallRuntime(Runtime::FunctionId id, int num_arguments);
706
ager@chromium.org5c838252010-02-19 08:53:10 +0000707 // Convenience function: call an external reference.
708 void CallExternalReference(const ExternalReference& ext,
709 int num_arguments);
710
ager@chromium.org9085a012009-05-11 19:22:57 +0000711 // Tail call of a runtime routine (jump).
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000712 // Like JumpToExternalReference, but also takes care of passing the number
713 // of parameters.
714 void TailCallExternalReference(const ExternalReference& ext,
715 int num_arguments,
716 int result_size);
717
718 // Convenience function: tail call a runtime routine (jump).
719 void TailCallRuntime(Runtime::FunctionId fid,
ager@chromium.orga1645e22009-09-09 19:27:10 +0000720 int num_arguments,
721 int result_size);
ager@chromium.org9085a012009-05-11 19:22:57 +0000722
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000723 // Jump to a runtime routine.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000724 void JumpToExternalReference(const ExternalReference& ext, int result_size);
ager@chromium.org9085a012009-05-11 19:22:57 +0000725
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000726 // Before calling a C-function from generated code, align arguments on stack.
727 // After aligning the frame, arguments must be stored in esp[0], esp[4],
728 // etc., not pushed. The argument count assumes all arguments are word sized.
729 // The number of slots reserved for arguments depends on platform. On Windows
730 // stack slots are reserved for the arguments passed in registers. On other
731 // platforms stack slots are only reserved for the arguments actually passed
732 // on the stack.
733 void PrepareCallCFunction(int num_arguments);
734
735 // Calls a C function and cleans up the space for arguments allocated
736 // by PrepareCallCFunction. The called function is not allowed to trigger a
737 // garbage collection, since that might move the code and invalidate the
738 // return address (unless this is somehow accounted for by the called
739 // function).
740 void CallCFunction(ExternalReference function, int num_arguments);
741 void CallCFunction(Register function, int num_arguments);
742
743 // Calculate the number of stack slots to reserve for arguments when calling a
744 // C function.
745 int ArgumentStackSlotsForCFunctionCall(int num_arguments);
ager@chromium.org9085a012009-05-11 19:22:57 +0000746
747 // ---------------------------------------------------------------------------
748 // Utilities
749
750 void Ret();
751
ager@chromium.org9085a012009-05-11 19:22:57 +0000752 Handle<Object> CodeObject() { return code_object_; }
753
754
755 // ---------------------------------------------------------------------------
756 // StatsCounter support
757
758 void SetCounter(StatsCounter* counter, int value);
759 void IncrementCounter(StatsCounter* counter, int value);
760 void DecrementCounter(StatsCounter* counter, int value);
761
762
763 // ---------------------------------------------------------------------------
764 // Debugging
765
766 // Calls Abort(msg) if the condition cc is not satisfied.
767 // Use --debug_code to enable.
768 void Assert(Condition cc, const char* msg);
769
770 // Like Assert(), but always enabled.
771 void Check(Condition cc, const char* msg);
772
773 // Print a message to stdout and abort execution.
774 void Abort(const char* msg);
775
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000776 // Check that the stack is aligned.
777 void CheckStackAlignment();
778
ager@chromium.org9085a012009-05-11 19:22:57 +0000779 // Verify restrictions about code generated in stubs.
780 void set_generating_stub(bool value) { generating_stub_ = value; }
781 bool generating_stub() { return generating_stub_; }
782 void set_allow_stub_calls(bool value) { allow_stub_calls_ = value; }
783 bool allow_stub_calls() { return allow_stub_calls_; }
784
785 private:
ager@chromium.org9085a012009-05-11 19:22:57 +0000786 bool generating_stub_;
787 bool allow_stub_calls_;
ager@chromium.org5c838252010-02-19 08:53:10 +0000788 // This handle will be patched with the code object on installation.
789 Handle<Object> code_object_;
ager@chromium.org9085a012009-05-11 19:22:57 +0000790
791 // Helper functions for generating invokes.
792 void InvokePrologue(const ParameterCount& expected,
793 const ParameterCount& actual,
794 Handle<Code> code_constant,
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000795 Register code_register,
ager@chromium.org9085a012009-05-11 19:22:57 +0000796 Label* done,
797 InvokeFlag flag);
798
ager@chromium.org9085a012009-05-11 19:22:57 +0000799 // Activation support.
800 void EnterFrame(StackFrame::Type type);
801 void LeaveFrame(StackFrame::Type type);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000802
803 // Allocation support helpers.
ager@chromium.orgac091b72010-05-05 07:34:42 +0000804 // Loads the top of new-space into the result register.
805 // If flags contains RESULT_CONTAINS_TOP then result_end is valid and
806 // already contains the top of new-space, and scratch is invalid.
807 // Otherwise the address of the new-space top is loaded into scratch (if
808 // scratch is valid), and the new-space top is loaded into result.
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000809 void LoadAllocationTopHelper(Register result,
810 Register result_end,
811 Register scratch,
ager@chromium.orga1645e22009-09-09 19:27:10 +0000812 AllocationFlags flags);
ager@chromium.orgac091b72010-05-05 07:34:42 +0000813 // Update allocation top with value in result_end register.
814 // If scratch is valid, it contains the address of the allocation top.
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000815 void UpdateAllocationTopHelper(Register result_end, Register scratch);
ager@chromium.org9085a012009-05-11 19:22:57 +0000816};
817
818
ager@chromium.org4af710e2009-09-15 12:20:11 +0000819// The code patcher is used to patch (typically) small parts of code e.g. for
820// debugging and other types of instrumentation. When using the code patcher
821// the exact number of bytes specified must be emitted. Is not legal to emit
822// relocation information. If any of these constraints are violated it causes
823// an assertion.
824class CodePatcher {
825 public:
826 CodePatcher(byte* address, int size);
827 virtual ~CodePatcher();
828
829 // Macro assembler to emit code.
830 MacroAssembler* masm() { return &masm_; }
831
832 private:
833 byte* address_; // The address of the code being patched.
834 int size_; // Number of bytes of the expected patch size.
835 MacroAssembler masm_; // Macro assembler used to generate the code.
836};
837
838
ager@chromium.org9085a012009-05-11 19:22:57 +0000839// -----------------------------------------------------------------------------
840// Static helper functions.
841
842// Generate an Operand for loading a field from an object.
843static inline Operand FieldOperand(Register object, int offset) {
844 return Operand(object, offset - kHeapObjectTag);
845}
846
847
848// Generate an Operand for loading an indexed field from an object.
849static inline Operand FieldOperand(Register object,
850 Register index,
851 ScaleFactor scale,
852 int offset) {
853 return Operand(object, index, scale, offset - kHeapObjectTag);
854}
855
856
857#ifdef GENERATED_CODE_COVERAGE
858extern void LogGeneratedCodeCoverage(const char* file_line);
859#define CODE_COVERAGE_STRINGIFY(x) #x
860#define CODE_COVERAGE_TOSTRING(x) CODE_COVERAGE_STRINGIFY(x)
861#define __FILE_LINE__ __FILE__ ":" CODE_COVERAGE_TOSTRING(__LINE__)
862#define ACCESS_MASM(masm) { \
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000863 byte* x64_coverage_function = \
ager@chromium.org9085a012009-05-11 19:22:57 +0000864 reinterpret_cast<byte*>(FUNCTION_ADDR(LogGeneratedCodeCoverage)); \
865 masm->pushfd(); \
866 masm->pushad(); \
867 masm->push(Immediate(reinterpret_cast<int>(&__FILE_LINE__))); \
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000868 masm->call(x64_coverage_function, RelocInfo::RUNTIME_ENTRY); \
ager@chromium.org9085a012009-05-11 19:22:57 +0000869 masm->pop(rax); \
870 masm->popad(); \
871 masm->popfd(); \
872 } \
873 masm->
874#else
875#define ACCESS_MASM(masm) masm->
876#endif
877
878
879} } // namespace v8::internal
880
881#endif // V8_X64_MACRO_ASSEMBLER_X64_H_