blob: a294ad678360e9ea2a7b95bbc7f15bfbce1b02f1 [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// Copyright 2009 the V8 project authors. All rights reserved.
2// 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
28#ifndef V8_X64_MACRO_ASSEMBLER_X64_H_
29#define V8_X64_MACRO_ASSEMBLER_X64_H_
30
31#include "assembler.h"
32
33namespace v8 {
34namespace internal {
35
Kristian Monsen25f61362010-05-21 11:50:48 +010036// 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
Steve Blocka7e24c12009-10-30 11:49:00 +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.
Steve Block8defd9f2010-07-08 12:39:36 +010050static const Register kScratchRegister = { 10 }; // r10.
51static const Register kSmiConstantRegister = { 15 }; // r15 (callee save).
52static const Register kRootRegister = { 13 }; // r13 (callee save).
53// Value of smi in kSmiConstantRegister.
54static const int kSmiConstantRegisterValue = 1;
Steve Blocka7e24c12009-10-30 11:49:00 +000055
Leon Clarkee46be812010-01-19 14:06:41 +000056// Convenience for platform-independent signatures.
57typedef Operand MemOperand;
58
Steve Blocka7e24c12009-10-30 11:49:00 +000059// Forward declaration.
60class JumpTarget;
61
62struct SmiIndex {
63 SmiIndex(Register index_register, ScaleFactor scale)
64 : reg(index_register),
65 scale(scale) {}
66 Register reg;
67 ScaleFactor scale;
68};
69
70// MacroAssembler implements a collection of frequently used macros.
71class MacroAssembler: public Assembler {
72 public:
73 MacroAssembler(void* buffer, int size);
74
75 void LoadRoot(Register destination, Heap::RootListIndex index);
76 void CompareRoot(Register with, Heap::RootListIndex index);
77 void CompareRoot(Operand with, Heap::RootListIndex index);
78 void PushRoot(Heap::RootListIndex index);
Kristian Monsen25f61362010-05-21 11:50:48 +010079 void StoreRoot(Register source, Heap::RootListIndex index);
Steve Blocka7e24c12009-10-30 11:49:00 +000080
81 // ---------------------------------------------------------------------------
82 // GC Support
83
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +010084 // For page containing |object| mark region covering |addr| dirty.
85 // RecordWriteHelper only works if the object is not in new
Steve Block6ded16b2010-05-10 14:33:55 +010086 // space.
87 void RecordWriteHelper(Register object,
88 Register addr,
89 Register scratch);
90
91 // Check if object is in new space. The condition cc can be equal or
92 // not_equal. If it is equal a jump will be done if the object is on new
93 // space. The register scratch can be object itself, but it will be clobbered.
94 void InNewSpace(Register object,
95 Register scratch,
96 Condition cc,
97 Label* branch);
98
Steve Block8defd9f2010-07-08 12:39:36 +010099 // For page containing |object| mark region covering [object+offset]
100 // dirty. |object| is the object being stored into, |value| is the
101 // object being stored. If |offset| is zero, then the |scratch|
102 // register contains the array index into the elements array
103 // represented as a Smi. All registers are clobbered by the
104 // operation. RecordWrite filters out smis so it does not update the
105 // write barrier if the value is a smi.
Steve Blocka7e24c12009-10-30 11:49:00 +0000106 void RecordWrite(Register object,
107 int offset,
108 Register value,
109 Register scratch);
110
Steve Block8defd9f2010-07-08 12:39:36 +0100111 // For page containing |object| mark region covering [address]
112 // dirty. |object| is the object being stored into, |value| is the
113 // object being stored. All registers are clobbered by the
114 // operation. RecordWrite filters out smis so it does not update
115 // the write barrier if the value is a smi.
116 void RecordWrite(Register object,
117 Register address,
118 Register value);
119
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100120 // For page containing |object| mark region covering [object+offset] dirty.
Steve Block3ce2e202009-11-05 08:53:23 +0000121 // The value is known to not be a smi.
122 // object is the object being stored into, value is the object being stored.
123 // If offset is zero, then the scratch register contains the array index into
124 // the elements array represented as a Smi.
125 // All registers are clobbered by the operation.
126 void RecordWriteNonSmi(Register object,
127 int offset,
128 Register value,
129 Register scratch);
130
Steve Blocka7e24c12009-10-30 11:49:00 +0000131#ifdef ENABLE_DEBUGGER_SUPPORT
132 // ---------------------------------------------------------------------------
133 // Debugger Support
134
135 void SaveRegistersToMemory(RegList regs);
136 void RestoreRegistersFromMemory(RegList regs);
137 void PushRegistersFromMemory(RegList regs);
138 void PopRegistersToMemory(RegList regs);
139 void CopyRegistersFromStackToMemory(Register base,
140 Register scratch,
141 RegList regs);
Andrei Popescu402d9372010-02-26 13:31:12 +0000142 void DebugBreak();
Steve Blocka7e24c12009-10-30 11:49:00 +0000143#endif
144
145 // ---------------------------------------------------------------------------
Steve Blockd0582a62009-12-15 09:54:21 +0000146 // Stack limit support
147
148 // Do simple test for stack overflow. This doesn't handle an overflow.
149 void StackLimitCheck(Label* on_stack_limit_hit);
150
151 // ---------------------------------------------------------------------------
Steve Blocka7e24c12009-10-30 11:49:00 +0000152 // Activation frames
153
154 void EnterInternalFrame() { EnterFrame(StackFrame::INTERNAL); }
155 void LeaveInternalFrame() { LeaveFrame(StackFrame::INTERNAL); }
156
157 void EnterConstructFrame() { EnterFrame(StackFrame::CONSTRUCT); }
158 void LeaveConstructFrame() { LeaveFrame(StackFrame::CONSTRUCT); }
159
Steve Blockd0582a62009-12-15 09:54:21 +0000160 // Enter specific kind of exit frame; either in normal or
161 // debug mode. Expects the number of arguments in register rax and
Steve Blocka7e24c12009-10-30 11:49:00 +0000162 // sets up the number of arguments in register rdi and the pointer
163 // to the first argument in register rsi.
Steve Blockd0582a62009-12-15 09:54:21 +0000164 void EnterExitFrame(ExitFrame::Mode mode, int result_size = 1);
Steve Blocka7e24c12009-10-30 11:49:00 +0000165
Ben Murdochbb769b22010-08-11 14:56:33 +0100166 void EnterApiExitFrame(ExitFrame::Mode mode,
167 int stack_space,
168 int argc,
169 int result_size = 1);
170
Steve Blocka7e24c12009-10-30 11:49:00 +0000171 // Leave the current exit frame. Expects/provides the return value in
172 // register rax:rdx (untouched) and the pointer to the first
173 // argument in register rsi.
Steve Blockd0582a62009-12-15 09:54:21 +0000174 void LeaveExitFrame(ExitFrame::Mode mode, int result_size = 1);
Steve Blocka7e24c12009-10-30 11:49:00 +0000175
176
177 // ---------------------------------------------------------------------------
178 // JavaScript invokes
179
180 // Invoke the JavaScript function code by either calling or jumping.
181 void InvokeCode(Register code,
182 const ParameterCount& expected,
183 const ParameterCount& actual,
184 InvokeFlag flag);
185
186 void InvokeCode(Handle<Code> code,
187 const ParameterCount& expected,
188 const ParameterCount& actual,
189 RelocInfo::Mode rmode,
190 InvokeFlag flag);
191
192 // Invoke the JavaScript function in the given register. Changes the
193 // current context to the context in the function before invoking.
194 void InvokeFunction(Register function,
195 const ParameterCount& actual,
196 InvokeFlag flag);
197
Andrei Popescu402d9372010-02-26 13:31:12 +0000198 void InvokeFunction(JSFunction* function,
199 const ParameterCount& actual,
200 InvokeFlag flag);
201
Steve Blocka7e24c12009-10-30 11:49:00 +0000202 // Invoke specified builtin JavaScript function. Adds an entry to
203 // the unresolved list if the name does not resolve.
204 void InvokeBuiltin(Builtins::JavaScript id, InvokeFlag flag);
205
206 // Store the code object for the given builtin in the target register.
207 void GetBuiltinEntry(Register target, Builtins::JavaScript id);
208
209
210 // ---------------------------------------------------------------------------
211 // Smi tagging, untagging and operations on tagged smis.
212
Steve Block8defd9f2010-07-08 12:39:36 +0100213 void InitializeSmiConstantRegister() {
214 movq(kSmiConstantRegister,
215 reinterpret_cast<uint64_t>(Smi::FromInt(kSmiConstantRegisterValue)),
216 RelocInfo::NONE);
217 }
218
Steve Blocka7e24c12009-10-30 11:49:00 +0000219 // Conversions between tagged smi values and non-tagged integer values.
220
221 // Tag an integer value. The result must be known to be a valid smi value.
Leon Clarke4515c472010-02-03 11:58:03 +0000222 // Only uses the low 32 bits of the src register. Sets the N and Z flags
223 // based on the value of the resulting integer.
Steve Blocka7e24c12009-10-30 11:49:00 +0000224 void Integer32ToSmi(Register dst, Register src);
225
226 // Tag an integer value if possible, or jump the integer value cannot be
227 // represented as a smi. Only uses the low 32 bit of the src registers.
Steve Block3ce2e202009-11-05 08:53:23 +0000228 // NOTICE: Destroys the dst register even if unsuccessful!
Steve Blocka7e24c12009-10-30 11:49:00 +0000229 void Integer32ToSmi(Register dst, Register src, Label* on_overflow);
230
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100231 // Stores an integer32 value into a memory field that already holds a smi.
232 void Integer32ToSmiField(const Operand& dst, Register src);
233
Steve Blocka7e24c12009-10-30 11:49:00 +0000234 // Adds constant to src and tags the result as a smi.
235 // Result must be a valid smi.
Steve Block3ce2e202009-11-05 08:53:23 +0000236 void Integer64PlusConstantToSmi(Register dst, Register src, int constant);
Steve Blocka7e24c12009-10-30 11:49:00 +0000237
238 // Convert smi to 32-bit integer. I.e., not sign extended into
239 // high 32 bits of destination.
240 void SmiToInteger32(Register dst, Register src);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100241 void SmiToInteger32(Register dst, const Operand& src);
Steve Blocka7e24c12009-10-30 11:49:00 +0000242
243 // Convert smi to 64-bit integer (sign extended if necessary).
244 void SmiToInteger64(Register dst, Register src);
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100245 void SmiToInteger64(Register dst, const Operand& src);
Steve Blocka7e24c12009-10-30 11:49:00 +0000246
247 // Multiply a positive smi's integer value by a power of two.
248 // Provides result as 64-bit integer value.
249 void PositiveSmiTimesPowerOfTwoToInteger64(Register dst,
250 Register src,
251 int power);
252
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100253 // Divide a positive smi's integer value by a power of two.
254 // Provides result as 32-bit integer value.
255 void PositiveSmiDivPowerOfTwoToInteger32(Register dst,
256 Register src,
257 int power);
258
259
Steve Block3ce2e202009-11-05 08:53:23 +0000260 // Simple comparison of smis.
261 void SmiCompare(Register dst, Register src);
262 void SmiCompare(Register dst, Smi* src);
Steve Block6ded16b2010-05-10 14:33:55 +0100263 void SmiCompare(Register dst, const Operand& src);
Steve Block3ce2e202009-11-05 08:53:23 +0000264 void SmiCompare(const Operand& dst, Register src);
265 void SmiCompare(const Operand& dst, Smi* src);
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100266 // Compare the int32 in src register to the value of the smi stored at dst.
267 void SmiCompareInteger32(const Operand& dst, Register src);
Steve Block3ce2e202009-11-05 08:53:23 +0000268 // Sets sign and zero flags depending on value of smi in register.
269 void SmiTest(Register src);
270
Steve Blocka7e24c12009-10-30 11:49:00 +0000271 // Functions performing a check on a known or potential smi. Returns
272 // a condition that is satisfied if the check is successful.
273
274 // Is the value a tagged smi.
275 Condition CheckSmi(Register src);
276
Steve Blocka7e24c12009-10-30 11:49:00 +0000277 // Is the value a positive tagged smi.
278 Condition CheckPositiveSmi(Register src);
279
Leon Clarkee46be812010-01-19 14:06:41 +0000280 // Are both values tagged smis.
Steve Blocka7e24c12009-10-30 11:49:00 +0000281 Condition CheckBothSmi(Register first, Register second);
282
Leon Clarked91b9f72010-01-27 17:25:45 +0000283 // Are both values tagged smis.
284 Condition CheckBothPositiveSmi(Register first, Register second);
285
Leon Clarkee46be812010-01-19 14:06:41 +0000286 // Are either value a tagged smi.
Ben Murdochbb769b22010-08-11 14:56:33 +0100287 Condition CheckEitherSmi(Register first,
288 Register second,
289 Register scratch = kScratchRegister);
Leon Clarkee46be812010-01-19 14:06:41 +0000290
Steve Blocka7e24c12009-10-30 11:49:00 +0000291 // Is the value the minimum smi value (since we are using
292 // two's complement numbers, negating the value is known to yield
293 // a non-smi value).
294 Condition CheckIsMinSmi(Register src);
295
Steve Blocka7e24c12009-10-30 11:49:00 +0000296 // Checks whether an 32-bit integer value is a valid for conversion
297 // to a smi.
298 Condition CheckInteger32ValidSmiValue(Register src);
299
Steve Block3ce2e202009-11-05 08:53:23 +0000300 // Checks whether an 32-bit unsigned integer value is a valid for
301 // conversion to a smi.
302 Condition CheckUInteger32ValidSmiValue(Register src);
303
Steve Blocka7e24c12009-10-30 11:49:00 +0000304 // Test-and-jump functions. Typically combines a check function
305 // above with a conditional jump.
306
307 // Jump if the value cannot be represented by a smi.
308 void JumpIfNotValidSmiValue(Register src, Label* on_invalid);
309
Steve Block3ce2e202009-11-05 08:53:23 +0000310 // Jump if the unsigned integer value cannot be represented by a smi.
311 void JumpIfUIntNotValidSmiValue(Register src, Label* on_invalid);
312
Steve Blocka7e24c12009-10-30 11:49:00 +0000313 // Jump to label if the value is a tagged smi.
314 void JumpIfSmi(Register src, Label* on_smi);
315
316 // Jump to label if the value is not a tagged smi.
317 void JumpIfNotSmi(Register src, Label* on_not_smi);
318
319 // Jump to label if the value is not a positive tagged smi.
320 void JumpIfNotPositiveSmi(Register src, Label* on_not_smi);
321
Steve Block3ce2e202009-11-05 08:53:23 +0000322 // Jump to label if the value, which must be a tagged smi, has value equal
Steve Blocka7e24c12009-10-30 11:49:00 +0000323 // to the constant.
Steve Block3ce2e202009-11-05 08:53:23 +0000324 void JumpIfSmiEqualsConstant(Register src, Smi* constant, Label* on_equals);
Steve Blocka7e24c12009-10-30 11:49:00 +0000325
326 // Jump if either or both register are not smi values.
327 void JumpIfNotBothSmi(Register src1, Register src2, Label* on_not_both_smi);
328
Leon Clarked91b9f72010-01-27 17:25:45 +0000329 // Jump if either or both register are not positive smi values.
330 void JumpIfNotBothPositiveSmi(Register src1, Register src2,
331 Label* on_not_both_smi);
332
Steve Blocka7e24c12009-10-30 11:49:00 +0000333 // Operations on tagged smi values.
334
335 // Smis represent a subset of integers. The subset is always equivalent to
336 // a two's complement interpretation of a fixed number of bits.
337
338 // Optimistically adds an integer constant to a supposed smi.
339 // If the src is not a smi, or the result is not a smi, jump to
340 // the label.
341 void SmiTryAddConstant(Register dst,
342 Register src,
Steve Block3ce2e202009-11-05 08:53:23 +0000343 Smi* constant,
Steve Blocka7e24c12009-10-30 11:49:00 +0000344 Label* on_not_smi_result);
345
Steve Block3ce2e202009-11-05 08:53:23 +0000346 // Add an integer constant to a tagged smi, giving a tagged smi as result.
347 // No overflow testing on the result is done.
348 void SmiAddConstant(Register dst, Register src, Smi* constant);
349
Leon Clarkef7060e22010-06-03 12:02:55 +0100350 // Add an integer constant to a tagged smi, giving a tagged smi as result.
351 // No overflow testing on the result is done.
352 void SmiAddConstant(const Operand& dst, Smi* constant);
353
Steve Blocka7e24c12009-10-30 11:49:00 +0000354 // Add an integer constant to a tagged smi, giving a tagged smi as result,
355 // or jumping to a label if the result cannot be represented by a smi.
Steve Blocka7e24c12009-10-30 11:49:00 +0000356 void SmiAddConstant(Register dst,
357 Register src,
Steve Block3ce2e202009-11-05 08:53:23 +0000358 Smi* constant,
Steve Blocka7e24c12009-10-30 11:49:00 +0000359 Label* on_not_smi_result);
360
361 // Subtract an integer constant from a tagged smi, giving a tagged smi as
Steve Block6ded16b2010-05-10 14:33:55 +0100362 // result. No testing on the result is done. Sets the N and Z flags
363 // based on the value of the resulting integer.
Steve Block3ce2e202009-11-05 08:53:23 +0000364 void SmiSubConstant(Register dst, Register src, Smi* constant);
365
366 // Subtract an integer constant from a tagged smi, giving a tagged smi as
Steve Blocka7e24c12009-10-30 11:49:00 +0000367 // result, or jumping to a label if the result cannot be represented by a smi.
Steve Blocka7e24c12009-10-30 11:49:00 +0000368 void SmiSubConstant(Register dst,
369 Register src,
Steve Block3ce2e202009-11-05 08:53:23 +0000370 Smi* constant,
Steve Blocka7e24c12009-10-30 11:49:00 +0000371 Label* on_not_smi_result);
372
373 // Negating a smi can give a negative zero or too large positive value.
Steve Block3ce2e202009-11-05 08:53:23 +0000374 // NOTICE: This operation jumps on success, not failure!
Steve Blocka7e24c12009-10-30 11:49:00 +0000375 void SmiNeg(Register dst,
376 Register src,
Steve Block3ce2e202009-11-05 08:53:23 +0000377 Label* on_smi_result);
Steve Blocka7e24c12009-10-30 11:49:00 +0000378
379 // Adds smi values and return the result as a smi.
380 // If dst is src1, then src1 will be destroyed, even if
381 // the operation is unsuccessful.
382 void SmiAdd(Register dst,
383 Register src1,
384 Register src2,
385 Label* on_not_smi_result);
386
387 // Subtracts smi values and return the result as a smi.
388 // If dst is src1, then src1 will be destroyed, even if
389 // the operation is unsuccessful.
390 void SmiSub(Register dst,
391 Register src1,
392 Register src2,
393 Label* on_not_smi_result);
394
Steve Block6ded16b2010-05-10 14:33:55 +0100395 void SmiSub(Register dst,
396 Register src1,
Leon Clarkef7060e22010-06-03 12:02:55 +0100397 const Operand& src2,
Steve Block6ded16b2010-05-10 14:33:55 +0100398 Label* on_not_smi_result);
399
Steve Blocka7e24c12009-10-30 11:49:00 +0000400 // Multiplies smi values and return the result as a smi,
401 // if possible.
402 // If dst is src1, then src1 will be destroyed, even if
403 // the operation is unsuccessful.
404 void SmiMul(Register dst,
405 Register src1,
406 Register src2,
407 Label* on_not_smi_result);
408
409 // Divides one smi by another and returns the quotient.
410 // Clobbers rax and rdx registers.
411 void SmiDiv(Register dst,
412 Register src1,
413 Register src2,
414 Label* on_not_smi_result);
415
416 // Divides one smi by another and returns the remainder.
417 // Clobbers rax and rdx registers.
418 void SmiMod(Register dst,
419 Register src1,
420 Register src2,
421 Label* on_not_smi_result);
422
423 // Bitwise operations.
424 void SmiNot(Register dst, Register src);
425 void SmiAnd(Register dst, Register src1, Register src2);
426 void SmiOr(Register dst, Register src1, Register src2);
427 void SmiXor(Register dst, Register src1, Register src2);
Steve Block3ce2e202009-11-05 08:53:23 +0000428 void SmiAndConstant(Register dst, Register src1, Smi* constant);
429 void SmiOrConstant(Register dst, Register src1, Smi* constant);
430 void SmiXorConstant(Register dst, Register src1, Smi* constant);
Steve Blocka7e24c12009-10-30 11:49:00 +0000431
432 void SmiShiftLeftConstant(Register dst,
433 Register src,
Kristian Monsen25f61362010-05-21 11:50:48 +0100434 int shift_value);
Steve Blocka7e24c12009-10-30 11:49:00 +0000435 void SmiShiftLogicalRightConstant(Register dst,
436 Register src,
437 int shift_value,
438 Label* on_not_smi_result);
439 void SmiShiftArithmeticRightConstant(Register dst,
440 Register src,
441 int shift_value);
442
443 // Shifts a smi value to the left, and returns the result if that is a smi.
444 // Uses and clobbers rcx, so dst may not be rcx.
445 void SmiShiftLeft(Register dst,
446 Register src1,
Kristian Monsen25f61362010-05-21 11:50:48 +0100447 Register src2);
Steve Blocka7e24c12009-10-30 11:49:00 +0000448 // Shifts a smi value to the right, shifting in zero bits at the top, and
449 // returns the unsigned intepretation of the result if that is a smi.
450 // Uses and clobbers rcx, so dst may not be rcx.
451 void SmiShiftLogicalRight(Register dst,
452 Register src1,
453 Register src2,
454 Label* on_not_smi_result);
455 // Shifts a smi value to the right, sign extending the top, and
456 // returns the signed intepretation of the result. That will always
457 // be a valid smi value, since it's numerically smaller than the
458 // original.
459 // Uses and clobbers rcx, so dst may not be rcx.
460 void SmiShiftArithmeticRight(Register dst,
461 Register src1,
462 Register src2);
463
464 // Specialized operations
465
466 // Select the non-smi register of two registers where exactly one is a
467 // smi. If neither are smis, jump to the failure label.
468 void SelectNonSmi(Register dst,
469 Register src1,
470 Register src2,
471 Label* on_not_smis);
472
473 // Converts, if necessary, a smi to a combination of number and
474 // multiplier to be used as a scaled index.
475 // The src register contains a *positive* smi value. The shift is the
476 // power of two to multiply the index value by (e.g.
477 // to index by smi-value * kPointerSize, pass the smi and kPointerSizeLog2).
478 // The returned index register may be either src or dst, depending
479 // on what is most efficient. If src and dst are different registers,
480 // src is always unchanged.
481 SmiIndex SmiToIndex(Register dst, Register src, int shift);
482
483 // Converts a positive smi to a negative index.
484 SmiIndex SmiToNegativeIndex(Register dst, Register src, int shift);
485
Steve Block3ce2e202009-11-05 08:53:23 +0000486 // Basic Smi operations.
487 void Move(Register dst, Smi* source) {
Steve Block8defd9f2010-07-08 12:39:36 +0100488 LoadSmiConstant(dst, source);
Steve Block3ce2e202009-11-05 08:53:23 +0000489 }
490
491 void Move(const Operand& dst, Smi* source) {
Steve Block8defd9f2010-07-08 12:39:36 +0100492 Register constant = GetSmiConstant(source);
493 movq(dst, constant);
Steve Block3ce2e202009-11-05 08:53:23 +0000494 }
495
496 void Push(Smi* smi);
497 void Test(const Operand& dst, Smi* source);
498
Steve Blocka7e24c12009-10-30 11:49:00 +0000499 // ---------------------------------------------------------------------------
Leon Clarkee46be812010-01-19 14:06:41 +0000500 // String macros.
501 void JumpIfNotBothSequentialAsciiStrings(Register first_object,
502 Register second_object,
503 Register scratch1,
504 Register scratch2,
505 Label* on_not_both_flat_ascii);
506
Steve Block6ded16b2010-05-10 14:33:55 +0100507 // Check whether the instance type represents a flat ascii string. Jump to the
508 // label if not. If the instance type can be scratched specify same register
509 // for both instance type and scratch.
510 void JumpIfInstanceTypeIsNotSequentialAscii(Register instance_type,
511 Register scratch,
512 Label *on_not_flat_ascii_string);
513
514 void JumpIfBothInstanceTypesAreNotSequentialAscii(
515 Register first_object_instance_type,
516 Register second_object_instance_type,
517 Register scratch1,
518 Register scratch2,
519 Label* on_fail);
520
Leon Clarkee46be812010-01-19 14:06:41 +0000521 // ---------------------------------------------------------------------------
522 // Macro instructions.
Steve Blocka7e24c12009-10-30 11:49:00 +0000523
Steve Block3ce2e202009-11-05 08:53:23 +0000524 // Load a register with a long value as efficiently as possible.
Steve Blocka7e24c12009-10-30 11:49:00 +0000525 void Set(Register dst, int64_t x);
526 void Set(const Operand& dst, int64_t x);
527
528 // Handle support
Steve Blocka7e24c12009-10-30 11:49:00 +0000529 void Move(Register dst, Handle<Object> source);
530 void Move(const Operand& dst, Handle<Object> source);
531 void Cmp(Register dst, Handle<Object> source);
532 void Cmp(const Operand& dst, Handle<Object> source);
533 void Push(Handle<Object> source);
Steve Blocka7e24c12009-10-30 11:49:00 +0000534
Leon Clarkee46be812010-01-19 14:06:41 +0000535 // Emit code to discard a non-negative number of pointer-sized elements
536 // from the stack, clobbering only the rsp register.
537 void Drop(int stack_elements);
538
539 void Call(Label* target) { call(target); }
540
Steve Blocka7e24c12009-10-30 11:49:00 +0000541 // Control Flow
542 void Jump(Address destination, RelocInfo::Mode rmode);
543 void Jump(ExternalReference ext);
544 void Jump(Handle<Code> code_object, RelocInfo::Mode rmode);
545
546 void Call(Address destination, RelocInfo::Mode rmode);
547 void Call(ExternalReference ext);
548 void Call(Handle<Code> code_object, RelocInfo::Mode rmode);
549
550 // Compare object type for heap object.
551 // Always use unsigned comparisons: above and below, not less and greater.
552 // Incoming register is heap_object and outgoing register is map.
553 // They may be the same register, and may be kScratchRegister.
554 void CmpObjectType(Register heap_object, InstanceType type, Register map);
555
556 // Compare instance type for map.
557 // Always use unsigned comparisons: above and below, not less and greater.
558 void CmpInstanceType(Register map, InstanceType type);
559
Andrei Popescu31002712010-02-23 13:46:05 +0000560 // Check if the map of an object is equal to a specified map and
561 // branch to label if not. Skip the smi check if not required
562 // (object is known to be a heap object)
563 void CheckMap(Register obj,
564 Handle<Map> map,
565 Label* fail,
566 bool is_heap_object);
567
Leon Clarked91b9f72010-01-27 17:25:45 +0000568 // Check if the object in register heap_object is a string. Afterwards the
569 // register map contains the object map and the register instance_type
570 // contains the instance_type. The registers map and instance_type can be the
571 // same in which case it contains the instance type afterwards. Either of the
572 // registers map and instance_type can be the same as heap_object.
573 Condition IsObjectStringType(Register heap_object,
574 Register map,
575 Register instance_type);
576
Steve Block8defd9f2010-07-08 12:39:36 +0100577 // FCmp compares and pops the two values on top of the FPU stack.
578 // The flag results are similar to integer cmp, but requires unsigned
Steve Blocka7e24c12009-10-30 11:49:00 +0000579 // jcc instructions (je, ja, jae, jb, jbe, je, and jz).
580 void FCmp();
581
Andrei Popescu402d9372010-02-26 13:31:12 +0000582 // Abort execution if argument is not a number. Used in debug code.
Leon Clarkef7060e22010-06-03 12:02:55 +0100583 void AbortIfNotNumber(Register object);
Andrei Popescu402d9372010-02-26 13:31:12 +0000584
Steve Block6ded16b2010-05-10 14:33:55 +0100585 // Abort execution if argument is not a smi. Used in debug code.
Leon Clarkef7060e22010-06-03 12:02:55 +0100586 void AbortIfNotSmi(Register object);
Steve Block6ded16b2010-05-10 14:33:55 +0100587
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100588 // Abort execution if argument is not the root value with the given index.
589 void AbortIfNotRootValue(Register src,
590 Heap::RootListIndex root_value_index,
591 const char* message);
592
Steve Blocka7e24c12009-10-30 11:49:00 +0000593 // ---------------------------------------------------------------------------
594 // Exception handling
595
596 // Push a new try handler and link into try handler chain. The return
597 // address must be pushed before calling this helper.
598 void PushTryHandler(CodeLocation try_location, HandlerType type);
599
Leon Clarkee46be812010-01-19 14:06:41 +0000600 // Unlink the stack handler on top of the stack from the try handler chain.
601 void PopTryHandler();
Steve Blocka7e24c12009-10-30 11:49:00 +0000602
603 // ---------------------------------------------------------------------------
604 // Inline caching support
605
Steve Blocka7e24c12009-10-30 11:49:00 +0000606 // Generate code for checking access rights - used for security checks
607 // on access to global objects across environments. The holder register
608 // is left untouched, but the scratch register and kScratchRegister,
609 // which must be different, are clobbered.
610 void CheckAccessGlobalProxy(Register holder_reg,
611 Register scratch,
612 Label* miss);
613
614
615 // ---------------------------------------------------------------------------
616 // Allocation support
617
618 // Allocate an object in new space. If the new space is exhausted control
619 // continues at the gc_required label. The allocated object is returned in
620 // result and end of the new object is returned in result_end. The register
621 // scratch can be passed as no_reg in which case an additional object
622 // reference will be added to the reloc info. The returned pointers in result
623 // and result_end have not yet been tagged as heap objects. If
624 // result_contains_top_on_entry is true the content of result is known to be
625 // the allocation top on entry (could be result_end from a previous call to
626 // AllocateInNewSpace). If result_contains_top_on_entry is true scratch
627 // should be no_reg as it is never used.
628 void AllocateInNewSpace(int object_size,
629 Register result,
630 Register result_end,
631 Register scratch,
632 Label* gc_required,
633 AllocationFlags flags);
634
635 void AllocateInNewSpace(int header_size,
636 ScaleFactor element_size,
637 Register element_count,
638 Register result,
639 Register result_end,
640 Register scratch,
641 Label* gc_required,
642 AllocationFlags flags);
643
644 void AllocateInNewSpace(Register object_size,
645 Register result,
646 Register result_end,
647 Register scratch,
648 Label* gc_required,
649 AllocationFlags flags);
650
651 // Undo allocation in new space. The object passed and objects allocated after
652 // it will no longer be allocated. Make sure that no pointers are left to the
653 // object(s) no longer allocated as they would be invalid when allocation is
654 // un-done.
655 void UndoAllocationInNewSpace(Register object);
656
Steve Block3ce2e202009-11-05 08:53:23 +0000657 // Allocate a heap number in new space with undefined value. Returns
658 // tagged pointer in result register, or jumps to gc_required if new
659 // space is full.
660 void AllocateHeapNumber(Register result,
661 Register scratch,
662 Label* gc_required);
663
Leon Clarkee46be812010-01-19 14:06:41 +0000664 // Allocate a sequential string. All the header fields of the string object
665 // are initialized.
666 void AllocateTwoByteString(Register result,
667 Register length,
668 Register scratch1,
669 Register scratch2,
670 Register scratch3,
671 Label* gc_required);
672 void AllocateAsciiString(Register result,
673 Register length,
674 Register scratch1,
675 Register scratch2,
676 Register scratch3,
677 Label* gc_required);
678
679 // Allocate a raw cons string object. Only the map field of the result is
680 // initialized.
681 void AllocateConsString(Register result,
682 Register scratch1,
683 Register scratch2,
684 Label* gc_required);
685 void AllocateAsciiConsString(Register result,
686 Register scratch1,
687 Register scratch2,
688 Label* gc_required);
689
Steve Blocka7e24c12009-10-30 11:49:00 +0000690 // ---------------------------------------------------------------------------
691 // Support functions.
692
693 // Check if result is zero and op is negative.
694 void NegativeZeroTest(Register result, Register op, Label* then_label);
695
696 // Check if result is zero and op is negative in code using jump targets.
697 void NegativeZeroTest(CodeGenerator* cgen,
698 Register result,
699 Register op,
700 JumpTarget* then_target);
701
702 // Check if result is zero and any of op1 and op2 are negative.
703 // Register scratch is destroyed, and it must be different from op2.
704 void NegativeZeroTest(Register result, Register op1, Register op2,
705 Register scratch, Label* then_label);
706
707 // Try to get function prototype of a function and puts the value in
708 // the result register. Checks that the function really is a
709 // function and jumps to the miss label if the fast checks fail. The
710 // function register will be untouched; the other register may be
711 // clobbered.
712 void TryGetFunctionPrototype(Register function,
713 Register result,
714 Label* miss);
715
716 // Generates code for reporting that an illegal operation has
717 // occurred.
718 void IllegalOperation(int num_arguments);
719
Steve Blockd0582a62009-12-15 09:54:21 +0000720 // Find the function context up the context chain.
721 void LoadContext(Register dst, int context_chain_length);
722
Steve Blocka7e24c12009-10-30 11:49:00 +0000723 // ---------------------------------------------------------------------------
724 // Runtime calls
725
726 // Call a code stub.
727 void CallStub(CodeStub* stub);
728
Ben Murdochbb769b22010-08-11 14:56:33 +0100729 // Call a code stub and return the code object called. Try to generate
730 // the code if necessary. Do not perform a GC but instead return a retry
731 // after GC failure.
732 Object* TryCallStub(CodeStub* stub);
733
Leon Clarkee46be812010-01-19 14:06:41 +0000734 // Tail call a code stub (jump).
735 void TailCallStub(CodeStub* stub);
736
Ben Murdochbb769b22010-08-11 14:56:33 +0100737 // Tail call a code stub (jump) and return the code object called. Try to
738 // generate the code if necessary. Do not perform a GC but instead return
739 // a retry after GC failure.
740 Object* TryTailCallStub(CodeStub* stub);
741
Steve Blocka7e24c12009-10-30 11:49:00 +0000742 // Return from a code stub after popping its arguments.
743 void StubReturn(int argc);
744
745 // Call a runtime routine.
Steve Blocka7e24c12009-10-30 11:49:00 +0000746 void CallRuntime(Runtime::Function* f, int num_arguments);
747
Ben Murdochbb769b22010-08-11 14:56:33 +0100748 // Call a runtime function, returning the CodeStub object called.
749 // Try to generate the stub code if necessary. Do not perform a GC
750 // but instead return a retry after GC failure.
751 Object* TryCallRuntime(Runtime::Function* f, int num_arguments);
752
Steve Blocka7e24c12009-10-30 11:49:00 +0000753 // Convenience function: Same as above, but takes the fid instead.
754 void CallRuntime(Runtime::FunctionId id, int num_arguments);
755
Ben Murdochbb769b22010-08-11 14:56:33 +0100756 // Convenience function: Same as above, but takes the fid instead.
757 Object* TryCallRuntime(Runtime::FunctionId id, int num_arguments);
758
Andrei Popescu402d9372010-02-26 13:31:12 +0000759 // Convenience function: call an external reference.
760 void CallExternalReference(const ExternalReference& ext,
761 int num_arguments);
762
Steve Blocka7e24c12009-10-30 11:49:00 +0000763 // Tail call of a runtime routine (jump).
Steve Block6ded16b2010-05-10 14:33:55 +0100764 // Like JumpToExternalReference, but also takes care of passing the number
765 // of parameters.
766 void TailCallExternalReference(const ExternalReference& ext,
767 int num_arguments,
768 int result_size);
769
770 // Convenience function: tail call a runtime routine (jump).
771 void TailCallRuntime(Runtime::FunctionId fid,
Steve Blocka7e24c12009-10-30 11:49:00 +0000772 int num_arguments,
773 int result_size);
774
Ben Murdochbb769b22010-08-11 14:56:33 +0100775 void PushHandleScope(Register scratch);
776
777 // Pops a handle scope using the specified scratch register and
778 // ensuring that saved register is left unchanged.
779 void PopHandleScope(Register saved, Register scratch);
780
781 // As PopHandleScope, but does not perform a GC. Instead, returns a
782 // retry after GC failure object if GC is necessary.
783 Object* TryPopHandleScope(Register saved, Register scratch);
784
Steve Blocka7e24c12009-10-30 11:49:00 +0000785 // Jump to a runtime routine.
Steve Block6ded16b2010-05-10 14:33:55 +0100786 void JumpToExternalReference(const ExternalReference& ext, int result_size);
Steve Blocka7e24c12009-10-30 11:49:00 +0000787
Leon Clarke4515c472010-02-03 11:58:03 +0000788 // Before calling a C-function from generated code, align arguments on stack.
789 // After aligning the frame, arguments must be stored in esp[0], esp[4],
790 // etc., not pushed. The argument count assumes all arguments are word sized.
791 // The number of slots reserved for arguments depends on platform. On Windows
792 // stack slots are reserved for the arguments passed in registers. On other
793 // platforms stack slots are only reserved for the arguments actually passed
794 // on the stack.
795 void PrepareCallCFunction(int num_arguments);
796
797 // Calls a C function and cleans up the space for arguments allocated
798 // by PrepareCallCFunction. The called function is not allowed to trigger a
799 // garbage collection, since that might move the code and invalidate the
800 // return address (unless this is somehow accounted for by the called
801 // function).
802 void CallCFunction(ExternalReference function, int num_arguments);
803 void CallCFunction(Register function, int num_arguments);
804
805 // Calculate the number of stack slots to reserve for arguments when calling a
806 // C function.
807 int ArgumentStackSlotsForCFunctionCall(int num_arguments);
Steve Blocka7e24c12009-10-30 11:49:00 +0000808
809 // ---------------------------------------------------------------------------
810 // Utilities
811
812 void Ret();
813
Steve Blocka7e24c12009-10-30 11:49:00 +0000814 Handle<Object> CodeObject() { return code_object_; }
815
816
817 // ---------------------------------------------------------------------------
818 // StatsCounter support
819
820 void SetCounter(StatsCounter* counter, int value);
821 void IncrementCounter(StatsCounter* counter, int value);
822 void DecrementCounter(StatsCounter* counter, int value);
823
824
825 // ---------------------------------------------------------------------------
826 // Debugging
827
828 // Calls Abort(msg) if the condition cc is not satisfied.
829 // Use --debug_code to enable.
830 void Assert(Condition cc, const char* msg);
831
832 // Like Assert(), but always enabled.
833 void Check(Condition cc, const char* msg);
834
835 // Print a message to stdout and abort execution.
836 void Abort(const char* msg);
837
Steve Block6ded16b2010-05-10 14:33:55 +0100838 // Check that the stack is aligned.
839 void CheckStackAlignment();
840
Steve Blocka7e24c12009-10-30 11:49:00 +0000841 // Verify restrictions about code generated in stubs.
842 void set_generating_stub(bool value) { generating_stub_ = value; }
843 bool generating_stub() { return generating_stub_; }
844 void set_allow_stub_calls(bool value) { allow_stub_calls_ = value; }
845 bool allow_stub_calls() { return allow_stub_calls_; }
846
847 private:
Steve Blocka7e24c12009-10-30 11:49:00 +0000848 bool generating_stub_;
849 bool allow_stub_calls_;
Steve Block8defd9f2010-07-08 12:39:36 +0100850
851 // Returns a register holding the smi value. The register MUST NOT be
852 // modified. It may be the "smi 1 constant" register.
853 Register GetSmiConstant(Smi* value);
854
855 // Moves the smi value to the destination register.
856 void LoadSmiConstant(Register dst, Smi* value);
857
Andrei Popescu31002712010-02-23 13:46:05 +0000858 // This handle will be patched with the code object on installation.
859 Handle<Object> code_object_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000860
861 // Helper functions for generating invokes.
862 void InvokePrologue(const ParameterCount& expected,
863 const ParameterCount& actual,
864 Handle<Code> code_constant,
865 Register code_register,
866 Label* done,
867 InvokeFlag flag);
868
Steve Blocka7e24c12009-10-30 11:49:00 +0000869 // Activation support.
870 void EnterFrame(StackFrame::Type type);
871 void LeaveFrame(StackFrame::Type type);
872
Ben Murdochbb769b22010-08-11 14:56:33 +0100873 void EnterExitFramePrologue(ExitFrame::Mode mode, bool save_rax);
874 void EnterExitFrameEpilogue(ExitFrame::Mode mode, int result_size, int argc);
875
Steve Blocka7e24c12009-10-30 11:49:00 +0000876 // Allocation support helpers.
Steve Block6ded16b2010-05-10 14:33:55 +0100877 // Loads the top of new-space into the result register.
878 // If flags contains RESULT_CONTAINS_TOP then result_end is valid and
879 // already contains the top of new-space, and scratch is invalid.
880 // Otherwise the address of the new-space top is loaded into scratch (if
881 // scratch is valid), and the new-space top is loaded into result.
Steve Blocka7e24c12009-10-30 11:49:00 +0000882 void LoadAllocationTopHelper(Register result,
883 Register result_end,
884 Register scratch,
885 AllocationFlags flags);
Steve Block6ded16b2010-05-10 14:33:55 +0100886 // Update allocation top with value in result_end register.
887 // If scratch is valid, it contains the address of the allocation top.
Steve Blocka7e24c12009-10-30 11:49:00 +0000888 void UpdateAllocationTopHelper(Register result_end, Register scratch);
Ben Murdochbb769b22010-08-11 14:56:33 +0100889
890 // Helper for PopHandleScope. Allowed to perform a GC and returns
891 // NULL if gc_allowed. Does not perform a GC if !gc_allowed, and
892 // possibly returns a failure object indicating an allocation failure.
893 Object* PopHandleScopeHelper(Register saved,
894 Register scratch,
895 bool gc_allowed);
Steve Blocka7e24c12009-10-30 11:49:00 +0000896};
897
898
899// The code patcher is used to patch (typically) small parts of code e.g. for
900// debugging and other types of instrumentation. When using the code patcher
901// the exact number of bytes specified must be emitted. Is not legal to emit
902// relocation information. If any of these constraints are violated it causes
903// an assertion.
904class CodePatcher {
905 public:
906 CodePatcher(byte* address, int size);
907 virtual ~CodePatcher();
908
909 // Macro assembler to emit code.
910 MacroAssembler* masm() { return &masm_; }
911
912 private:
913 byte* address_; // The address of the code being patched.
914 int size_; // Number of bytes of the expected patch size.
915 MacroAssembler masm_; // Macro assembler used to generate the code.
916};
917
918
919// -----------------------------------------------------------------------------
920// Static helper functions.
921
922// Generate an Operand for loading a field from an object.
923static inline Operand FieldOperand(Register object, int offset) {
924 return Operand(object, offset - kHeapObjectTag);
925}
926
927
928// Generate an Operand for loading an indexed field from an object.
929static inline Operand FieldOperand(Register object,
930 Register index,
931 ScaleFactor scale,
932 int offset) {
933 return Operand(object, index, scale, offset - kHeapObjectTag);
934}
935
936
937#ifdef GENERATED_CODE_COVERAGE
938extern void LogGeneratedCodeCoverage(const char* file_line);
939#define CODE_COVERAGE_STRINGIFY(x) #x
940#define CODE_COVERAGE_TOSTRING(x) CODE_COVERAGE_STRINGIFY(x)
941#define __FILE_LINE__ __FILE__ ":" CODE_COVERAGE_TOSTRING(__LINE__)
942#define ACCESS_MASM(masm) { \
943 byte* x64_coverage_function = \
944 reinterpret_cast<byte*>(FUNCTION_ADDR(LogGeneratedCodeCoverage)); \
945 masm->pushfd(); \
946 masm->pushad(); \
947 masm->push(Immediate(reinterpret_cast<int>(&__FILE_LINE__))); \
948 masm->call(x64_coverage_function, RelocInfo::RUNTIME_ENTRY); \
949 masm->pop(rax); \
950 masm->popad(); \
951 masm->popfd(); \
952 } \
953 masm->
954#else
955#define ACCESS_MASM(masm) masm->
956#endif
957
958
959} } // namespace v8::internal
960
961#endif // V8_X64_MACRO_ASSEMBLER_X64_H_