blob: 0acce0543c4b8d7d15936a66c301fc1d62fb40a6 [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.
Andrei Popescu402d9372010-02-26 13:31:12 +000050static const Register kScratchRegister = { 10 }; // r10.
Steve Block6ded16b2010-05-10 14:33:55 +010051static const Register kRootRegister = { 13 }; // r13
Steve Blocka7e24c12009-10-30 11:49:00 +000052
Leon Clarkee46be812010-01-19 14:06:41 +000053// Convenience for platform-independent signatures.
54typedef Operand MemOperand;
55
Steve Blocka7e24c12009-10-30 11:49:00 +000056// Forward declaration.
57class JumpTarget;
58
59struct SmiIndex {
60 SmiIndex(Register index_register, ScaleFactor scale)
61 : reg(index_register),
62 scale(scale) {}
63 Register reg;
64 ScaleFactor scale;
65};
66
67// MacroAssembler implements a collection of frequently used macros.
68class MacroAssembler: public Assembler {
69 public:
70 MacroAssembler(void* buffer, int size);
71
72 void LoadRoot(Register destination, Heap::RootListIndex index);
73 void CompareRoot(Register with, Heap::RootListIndex index);
74 void CompareRoot(Operand with, Heap::RootListIndex index);
75 void PushRoot(Heap::RootListIndex index);
Kristian Monsen25f61362010-05-21 11:50:48 +010076 void StoreRoot(Register source, Heap::RootListIndex index);
Steve Blocka7e24c12009-10-30 11:49:00 +000077
78 // ---------------------------------------------------------------------------
79 // GC Support
80
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +010081 // For page containing |object| mark region covering |addr| dirty.
82 // RecordWriteHelper only works if the object is not in new
Steve Block6ded16b2010-05-10 14:33:55 +010083 // space.
84 void RecordWriteHelper(Register object,
85 Register addr,
86 Register scratch);
87
88 // 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
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +010096 // For page containing |object| mark region covering [object+offset] dirty.
Steve Blocka7e24c12009-10-30 11:49:00 +000097 // 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
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100106 // For page containing |object| mark region covering [object+offset] dirty.
Steve Block3ce2e202009-11-05 08:53:23 +0000107 // 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
Steve Blocka7e24c12009-10-30 11:49:00 +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);
Andrei Popescu402d9372010-02-26 13:31:12 +0000128 void DebugBreak();
Steve Blocka7e24c12009-10-30 11:49:00 +0000129#endif
130
131 // ---------------------------------------------------------------------------
Steve Blockd0582a62009-12-15 09:54:21 +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 // ---------------------------------------------------------------------------
Steve Blocka7e24c12009-10-30 11:49:00 +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
Steve Blockd0582a62009-12-15 09:54:21 +0000146 // Enter specific kind of exit frame; either in normal or
147 // debug mode. Expects the number of arguments in register rax and
Steve Blocka7e24c12009-10-30 11:49:00 +0000148 // sets up the number of arguments in register rdi and the pointer
149 // to the first argument in register rsi.
Steve Blockd0582a62009-12-15 09:54:21 +0000150 void EnterExitFrame(ExitFrame::Mode mode, int result_size = 1);
Steve Blocka7e24c12009-10-30 11:49:00 +0000151
152 // 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.
Steve Blockd0582a62009-12-15 09:54:21 +0000155 void LeaveExitFrame(ExitFrame::Mode mode, int result_size = 1);
Steve Blocka7e24c12009-10-30 11:49:00 +0000156
157
158 // ---------------------------------------------------------------------------
159 // JavaScript invokes
160
161 // Invoke the JavaScript function code by either calling or jumping.
162 void InvokeCode(Register code,
163 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
Andrei Popescu402d9372010-02-26 13:31:12 +0000179 void InvokeFunction(JSFunction* function,
180 const ParameterCount& actual,
181 InvokeFlag flag);
182
Steve Blocka7e24c12009-10-30 11:49:00 +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
190
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.
Leon Clarke4515c472010-02-03 11:58:03 +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.
Steve Blocka7e24c12009-10-30 11:49:00 +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.
Steve Block3ce2e202009-11-05 08:53:23 +0000203 // NOTICE: Destroys the dst register even if unsuccessful!
Steve Blocka7e24c12009-10-30 11:49:00 +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.
Steve Block3ce2e202009-11-05 08:53:23 +0000208 void Integer64PlusConstantToSmi(Register dst, Register src, int constant);
Steve Blocka7e24c12009-10-30 11:49:00 +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);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100213 void SmiToInteger32(Register dst, const Operand& src);
Steve Blocka7e24c12009-10-30 11:49:00 +0000214
215 // Convert smi to 64-bit integer (sign extended if necessary).
216 void SmiToInteger64(Register dst, Register src);
217
218 // Multiply a positive smi's integer value by a power of two.
219 // Provides result as 64-bit integer value.
220 void PositiveSmiTimesPowerOfTwoToInteger64(Register dst,
221 Register src,
222 int power);
223
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100224 // Divide a positive smi's integer value by a power of two.
225 // Provides result as 32-bit integer value.
226 void PositiveSmiDivPowerOfTwoToInteger32(Register dst,
227 Register src,
228 int power);
229
230
Steve Block3ce2e202009-11-05 08:53:23 +0000231 // Simple comparison of smis.
232 void SmiCompare(Register dst, Register src);
233 void SmiCompare(Register dst, Smi* src);
Steve Block6ded16b2010-05-10 14:33:55 +0100234 void SmiCompare(Register dst, const Operand& src);
Steve Block3ce2e202009-11-05 08:53:23 +0000235 void SmiCompare(const Operand& dst, Register src);
236 void SmiCompare(const Operand& dst, Smi* src);
237 // Sets sign and zero flags depending on value of smi in register.
238 void SmiTest(Register src);
239
Steve Blocka7e24c12009-10-30 11:49:00 +0000240 // Functions performing a check on a known or potential smi. Returns
241 // a condition that is satisfied if the check is successful.
242
243 // Is the value a tagged smi.
244 Condition CheckSmi(Register src);
245
Steve Blocka7e24c12009-10-30 11:49:00 +0000246 // Is the value a positive tagged smi.
247 Condition CheckPositiveSmi(Register src);
248
Leon Clarkee46be812010-01-19 14:06:41 +0000249 // Are both values tagged smis.
Steve Blocka7e24c12009-10-30 11:49:00 +0000250 Condition CheckBothSmi(Register first, Register second);
251
Leon Clarked91b9f72010-01-27 17:25:45 +0000252 // Are both values tagged smis.
253 Condition CheckBothPositiveSmi(Register first, Register second);
254
Leon Clarkee46be812010-01-19 14:06:41 +0000255 // Are either value a tagged smi.
256 Condition CheckEitherSmi(Register first, Register second);
257
Steve Blocka7e24c12009-10-30 11:49:00 +0000258 // Is the value the minimum smi value (since we are using
259 // two's complement numbers, negating the value is known to yield
260 // a non-smi value).
261 Condition CheckIsMinSmi(Register src);
262
Steve Blocka7e24c12009-10-30 11:49:00 +0000263 // Checks whether an 32-bit integer value is a valid for conversion
264 // to a smi.
265 Condition CheckInteger32ValidSmiValue(Register src);
266
Steve Block3ce2e202009-11-05 08:53:23 +0000267 // Checks whether an 32-bit unsigned integer value is a valid for
268 // conversion to a smi.
269 Condition CheckUInteger32ValidSmiValue(Register src);
270
Steve Blocka7e24c12009-10-30 11:49:00 +0000271 // Test-and-jump functions. Typically combines a check function
272 // above with a conditional jump.
273
274 // Jump if the value cannot be represented by a smi.
275 void JumpIfNotValidSmiValue(Register src, Label* on_invalid);
276
Steve Block3ce2e202009-11-05 08:53:23 +0000277 // Jump if the unsigned integer value cannot be represented by a smi.
278 void JumpIfUIntNotValidSmiValue(Register src, Label* on_invalid);
279
Steve Blocka7e24c12009-10-30 11:49:00 +0000280 // Jump to label if the value is a tagged smi.
281 void JumpIfSmi(Register src, Label* on_smi);
282
283 // Jump to label if the value is not a tagged smi.
284 void JumpIfNotSmi(Register src, Label* on_not_smi);
285
286 // Jump to label if the value is not a positive tagged smi.
287 void JumpIfNotPositiveSmi(Register src, Label* on_not_smi);
288
Steve Block3ce2e202009-11-05 08:53:23 +0000289 // Jump to label if the value, which must be a tagged smi, has value equal
Steve Blocka7e24c12009-10-30 11:49:00 +0000290 // to the constant.
Steve Block3ce2e202009-11-05 08:53:23 +0000291 void JumpIfSmiEqualsConstant(Register src, Smi* constant, Label* on_equals);
Steve Blocka7e24c12009-10-30 11:49:00 +0000292
293 // Jump if either or both register are not smi values.
294 void JumpIfNotBothSmi(Register src1, Register src2, Label* on_not_both_smi);
295
Leon Clarked91b9f72010-01-27 17:25:45 +0000296 // Jump if either or both register are not positive smi values.
297 void JumpIfNotBothPositiveSmi(Register src1, Register src2,
298 Label* on_not_both_smi);
299
Steve Blocka7e24c12009-10-30 11:49:00 +0000300 // Operations on tagged smi values.
301
302 // Smis represent a subset of integers. The subset is always equivalent to
303 // a two's complement interpretation of a fixed number of bits.
304
305 // Optimistically adds an integer constant to a supposed smi.
306 // If the src is not a smi, or the result is not a smi, jump to
307 // the label.
308 void SmiTryAddConstant(Register dst,
309 Register src,
Steve Block3ce2e202009-11-05 08:53:23 +0000310 Smi* constant,
Steve Blocka7e24c12009-10-30 11:49:00 +0000311 Label* on_not_smi_result);
312
Steve Block3ce2e202009-11-05 08:53:23 +0000313 // Add an integer constant to a tagged smi, giving a tagged smi as result.
314 // No overflow testing on the result is done.
315 void SmiAddConstant(Register dst, Register src, Smi* constant);
316
Leon Clarkef7060e22010-06-03 12:02:55 +0100317 // Add an integer constant to a tagged smi, giving a tagged smi as result.
318 // No overflow testing on the result is done.
319 void SmiAddConstant(const Operand& dst, Smi* constant);
320
Steve Blocka7e24c12009-10-30 11:49:00 +0000321 // Add an integer constant to a tagged smi, giving a tagged smi as result,
322 // or jumping to a label if the result cannot be represented by a smi.
Steve Blocka7e24c12009-10-30 11:49:00 +0000323 void SmiAddConstant(Register dst,
324 Register src,
Steve Block3ce2e202009-11-05 08:53:23 +0000325 Smi* constant,
Steve Blocka7e24c12009-10-30 11:49:00 +0000326 Label* on_not_smi_result);
327
328 // Subtract an integer constant from a tagged smi, giving a tagged smi as
Steve Block6ded16b2010-05-10 14:33:55 +0100329 // result. No testing on the result is done. Sets the N and Z flags
330 // based on the value of the resulting integer.
Steve Block3ce2e202009-11-05 08:53:23 +0000331 void SmiSubConstant(Register dst, Register src, Smi* constant);
332
333 // Subtract an integer constant from a tagged smi, giving a tagged smi as
Steve Blocka7e24c12009-10-30 11:49:00 +0000334 // result, or jumping to a label if the result cannot be represented by a smi.
Steve Blocka7e24c12009-10-30 11:49:00 +0000335 void SmiSubConstant(Register dst,
336 Register src,
Steve Block3ce2e202009-11-05 08:53:23 +0000337 Smi* constant,
Steve Blocka7e24c12009-10-30 11:49:00 +0000338 Label* on_not_smi_result);
339
340 // Negating a smi can give a negative zero or too large positive value.
Steve Block3ce2e202009-11-05 08:53:23 +0000341 // NOTICE: This operation jumps on success, not failure!
Steve Blocka7e24c12009-10-30 11:49:00 +0000342 void SmiNeg(Register dst,
343 Register src,
Steve Block3ce2e202009-11-05 08:53:23 +0000344 Label* on_smi_result);
Steve Blocka7e24c12009-10-30 11:49:00 +0000345
346 // Adds smi values and return the result as a smi.
347 // If dst is src1, then src1 will be destroyed, even if
348 // the operation is unsuccessful.
349 void SmiAdd(Register dst,
350 Register src1,
351 Register src2,
352 Label* on_not_smi_result);
353
354 // Subtracts smi values and return the result as a smi.
355 // If dst is src1, then src1 will be destroyed, even if
356 // the operation is unsuccessful.
357 void SmiSub(Register dst,
358 Register src1,
359 Register src2,
360 Label* on_not_smi_result);
361
Steve Block6ded16b2010-05-10 14:33:55 +0100362 void SmiSub(Register dst,
363 Register src1,
Leon Clarkef7060e22010-06-03 12:02:55 +0100364 const Operand& src2,
Steve Block6ded16b2010-05-10 14:33:55 +0100365 Label* on_not_smi_result);
366
Steve Blocka7e24c12009-10-30 11:49:00 +0000367 // Multiplies smi values and return the result as a smi,
368 // if possible.
369 // If dst is src1, then src1 will be destroyed, even if
370 // the operation is unsuccessful.
371 void SmiMul(Register dst,
372 Register src1,
373 Register src2,
374 Label* on_not_smi_result);
375
376 // Divides one smi by another and returns the quotient.
377 // Clobbers rax and rdx registers.
378 void SmiDiv(Register dst,
379 Register src1,
380 Register src2,
381 Label* on_not_smi_result);
382
383 // Divides one smi by another and returns the remainder.
384 // Clobbers rax and rdx registers.
385 void SmiMod(Register dst,
386 Register src1,
387 Register src2,
388 Label* on_not_smi_result);
389
390 // Bitwise operations.
391 void SmiNot(Register dst, Register src);
392 void SmiAnd(Register dst, Register src1, Register src2);
393 void SmiOr(Register dst, Register src1, Register src2);
394 void SmiXor(Register dst, Register src1, Register src2);
Steve Block3ce2e202009-11-05 08:53:23 +0000395 void SmiAndConstant(Register dst, Register src1, Smi* constant);
396 void SmiOrConstant(Register dst, Register src1, Smi* constant);
397 void SmiXorConstant(Register dst, Register src1, Smi* constant);
Steve Blocka7e24c12009-10-30 11:49:00 +0000398
399 void SmiShiftLeftConstant(Register dst,
400 Register src,
Kristian Monsen25f61362010-05-21 11:50:48 +0100401 int shift_value);
Steve Blocka7e24c12009-10-30 11:49:00 +0000402 void SmiShiftLogicalRightConstant(Register dst,
403 Register src,
404 int shift_value,
405 Label* on_not_smi_result);
406 void SmiShiftArithmeticRightConstant(Register dst,
407 Register src,
408 int shift_value);
409
410 // Shifts a smi value to the left, and returns the result if that is a smi.
411 // Uses and clobbers rcx, so dst may not be rcx.
412 void SmiShiftLeft(Register dst,
413 Register src1,
Kristian Monsen25f61362010-05-21 11:50:48 +0100414 Register src2);
Steve Blocka7e24c12009-10-30 11:49:00 +0000415 // Shifts a smi value to the right, shifting in zero bits at the top, and
416 // returns the unsigned intepretation of the result if that is a smi.
417 // Uses and clobbers rcx, so dst may not be rcx.
418 void SmiShiftLogicalRight(Register dst,
419 Register src1,
420 Register src2,
421 Label* on_not_smi_result);
422 // Shifts a smi value to the right, sign extending the top, and
423 // returns the signed intepretation of the result. That will always
424 // be a valid smi value, since it's numerically smaller than the
425 // original.
426 // Uses and clobbers rcx, so dst may not be rcx.
427 void SmiShiftArithmeticRight(Register dst,
428 Register src1,
429 Register src2);
430
431 // Specialized operations
432
433 // Select the non-smi register of two registers where exactly one is a
434 // smi. If neither are smis, jump to the failure label.
435 void SelectNonSmi(Register dst,
436 Register src1,
437 Register src2,
438 Label* on_not_smis);
439
440 // Converts, if necessary, a smi to a combination of number and
441 // multiplier to be used as a scaled index.
442 // The src register contains a *positive* smi value. The shift is the
443 // power of two to multiply the index value by (e.g.
444 // to index by smi-value * kPointerSize, pass the smi and kPointerSizeLog2).
445 // The returned index register may be either src or dst, depending
446 // on what is most efficient. If src and dst are different registers,
447 // src is always unchanged.
448 SmiIndex SmiToIndex(Register dst, Register src, int shift);
449
450 // Converts a positive smi to a negative index.
451 SmiIndex SmiToNegativeIndex(Register dst, Register src, int shift);
452
Steve Block3ce2e202009-11-05 08:53:23 +0000453 // Basic Smi operations.
454 void Move(Register dst, Smi* source) {
455 Set(dst, reinterpret_cast<int64_t>(source));
456 }
457
458 void Move(const Operand& dst, Smi* source) {
459 Set(dst, reinterpret_cast<int64_t>(source));
460 }
461
462 void Push(Smi* smi);
463 void Test(const Operand& dst, Smi* source);
464
Steve Blocka7e24c12009-10-30 11:49:00 +0000465 // ---------------------------------------------------------------------------
Leon Clarkee46be812010-01-19 14:06:41 +0000466 // String macros.
467 void JumpIfNotBothSequentialAsciiStrings(Register first_object,
468 Register second_object,
469 Register scratch1,
470 Register scratch2,
471 Label* on_not_both_flat_ascii);
472
Steve Block6ded16b2010-05-10 14:33:55 +0100473 // Check whether the instance type represents a flat ascii string. Jump to the
474 // label if not. If the instance type can be scratched specify same register
475 // for both instance type and scratch.
476 void JumpIfInstanceTypeIsNotSequentialAscii(Register instance_type,
477 Register scratch,
478 Label *on_not_flat_ascii_string);
479
480 void JumpIfBothInstanceTypesAreNotSequentialAscii(
481 Register first_object_instance_type,
482 Register second_object_instance_type,
483 Register scratch1,
484 Register scratch2,
485 Label* on_fail);
486
Leon Clarkee46be812010-01-19 14:06:41 +0000487 // ---------------------------------------------------------------------------
488 // Macro instructions.
Steve Blocka7e24c12009-10-30 11:49:00 +0000489
Steve Block3ce2e202009-11-05 08:53:23 +0000490 // Load a register with a long value as efficiently as possible.
Steve Blocka7e24c12009-10-30 11:49:00 +0000491 void Set(Register dst, int64_t x);
492 void Set(const Operand& dst, int64_t x);
493
494 // Handle support
Steve Blocka7e24c12009-10-30 11:49:00 +0000495 void Move(Register dst, Handle<Object> source);
496 void Move(const Operand& dst, Handle<Object> source);
497 void Cmp(Register dst, Handle<Object> source);
498 void Cmp(const Operand& dst, Handle<Object> source);
499 void Push(Handle<Object> source);
Steve Blocka7e24c12009-10-30 11:49:00 +0000500
Leon Clarkee46be812010-01-19 14:06:41 +0000501 // Emit code to discard a non-negative number of pointer-sized elements
502 // from the stack, clobbering only the rsp register.
503 void Drop(int stack_elements);
504
505 void Call(Label* target) { call(target); }
506
Steve Blocka7e24c12009-10-30 11:49:00 +0000507 // Control Flow
508 void Jump(Address destination, RelocInfo::Mode rmode);
509 void Jump(ExternalReference ext);
510 void Jump(Handle<Code> code_object, RelocInfo::Mode rmode);
511
512 void Call(Address destination, RelocInfo::Mode rmode);
513 void Call(ExternalReference ext);
514 void Call(Handle<Code> code_object, RelocInfo::Mode rmode);
515
516 // Compare object type for heap object.
517 // Always use unsigned comparisons: above and below, not less and greater.
518 // Incoming register is heap_object and outgoing register is map.
519 // They may be the same register, and may be kScratchRegister.
520 void CmpObjectType(Register heap_object, InstanceType type, Register map);
521
522 // Compare instance type for map.
523 // Always use unsigned comparisons: above and below, not less and greater.
524 void CmpInstanceType(Register map, InstanceType type);
525
Andrei Popescu31002712010-02-23 13:46:05 +0000526 // Check if the map of an object is equal to a specified map and
527 // branch to label if not. Skip the smi check if not required
528 // (object is known to be a heap object)
529 void CheckMap(Register obj,
530 Handle<Map> map,
531 Label* fail,
532 bool is_heap_object);
533
Leon Clarked91b9f72010-01-27 17:25:45 +0000534 // Check if the object in register heap_object is a string. Afterwards the
535 // register map contains the object map and the register instance_type
536 // contains the instance_type. The registers map and instance_type can be the
537 // same in which case it contains the instance type afterwards. Either of the
538 // registers map and instance_type can be the same as heap_object.
539 Condition IsObjectStringType(Register heap_object,
540 Register map,
541 Register instance_type);
542
Steve Blocka7e24c12009-10-30 11:49:00 +0000543 // FCmp is similar to integer cmp, but requires unsigned
544 // jcc instructions (je, ja, jae, jb, jbe, je, and jz).
545 void FCmp();
546
Andrei Popescu402d9372010-02-26 13:31:12 +0000547 // Abort execution if argument is not a number. Used in debug code.
Leon Clarkef7060e22010-06-03 12:02:55 +0100548 void AbortIfNotNumber(Register object);
Andrei Popescu402d9372010-02-26 13:31:12 +0000549
Steve Block6ded16b2010-05-10 14:33:55 +0100550 // Abort execution if argument is not a smi. Used in debug code.
Leon Clarkef7060e22010-06-03 12:02:55 +0100551 void AbortIfNotSmi(Register object);
Steve Block6ded16b2010-05-10 14:33:55 +0100552
Steve Blocka7e24c12009-10-30 11:49:00 +0000553 // ---------------------------------------------------------------------------
554 // Exception handling
555
556 // Push a new try handler and link into try handler chain. The return
557 // address must be pushed before calling this helper.
558 void PushTryHandler(CodeLocation try_location, HandlerType type);
559
Leon Clarkee46be812010-01-19 14:06:41 +0000560 // Unlink the stack handler on top of the stack from the try handler chain.
561 void PopTryHandler();
Steve Blocka7e24c12009-10-30 11:49:00 +0000562
563 // ---------------------------------------------------------------------------
564 // Inline caching support
565
566 // Generates code that verifies that the maps of objects in the
567 // prototype chain of object hasn't changed since the code was
568 // generated and branches to the miss label if any map has. If
569 // necessary the function also generates code for security check
570 // in case of global object holders. The scratch and holder
571 // registers are always clobbered, but the object register is only
572 // clobbered if it the same as the holder register. The function
573 // returns a register containing the holder - either object_reg or
574 // holder_reg.
Steve Block6ded16b2010-05-10 14:33:55 +0100575 // The function can optionally (when save_at_depth !=
576 // kInvalidProtoDepth) save the object at the given depth by moving
577 // it to [rsp + kPointerSize].
Steve Blocka7e24c12009-10-30 11:49:00 +0000578 Register CheckMaps(JSObject* object, Register object_reg,
579 JSObject* holder, Register holder_reg,
Steve Block6ded16b2010-05-10 14:33:55 +0100580 Register scratch,
581 int save_at_depth,
582 Label* miss);
Steve Blocka7e24c12009-10-30 11:49:00 +0000583
584 // Generate code for checking access rights - used for security checks
585 // on access to global objects across environments. The holder register
586 // is left untouched, but the scratch register and kScratchRegister,
587 // which must be different, are clobbered.
588 void CheckAccessGlobalProxy(Register holder_reg,
589 Register scratch,
590 Label* miss);
591
592
593 // ---------------------------------------------------------------------------
594 // Allocation support
595
596 // Allocate an object in new space. If the new space is exhausted control
597 // continues at the gc_required label. The allocated object is returned in
598 // result and end of the new object is returned in result_end. The register
599 // scratch can be passed as no_reg in which case an additional object
600 // reference will be added to the reloc info. The returned pointers in result
601 // and result_end have not yet been tagged as heap objects. If
602 // result_contains_top_on_entry is true the content of result is known to be
603 // the allocation top on entry (could be result_end from a previous call to
604 // AllocateInNewSpace). If result_contains_top_on_entry is true scratch
605 // should be no_reg as it is never used.
606 void AllocateInNewSpace(int object_size,
607 Register result,
608 Register result_end,
609 Register scratch,
610 Label* gc_required,
611 AllocationFlags flags);
612
613 void AllocateInNewSpace(int header_size,
614 ScaleFactor element_size,
615 Register element_count,
616 Register result,
617 Register result_end,
618 Register scratch,
619 Label* gc_required,
620 AllocationFlags flags);
621
622 void AllocateInNewSpace(Register object_size,
623 Register result,
624 Register result_end,
625 Register scratch,
626 Label* gc_required,
627 AllocationFlags flags);
628
629 // Undo allocation in new space. The object passed and objects allocated after
630 // it will no longer be allocated. Make sure that no pointers are left to the
631 // object(s) no longer allocated as they would be invalid when allocation is
632 // un-done.
633 void UndoAllocationInNewSpace(Register object);
634
Steve Block3ce2e202009-11-05 08:53:23 +0000635 // Allocate a heap number in new space with undefined value. Returns
636 // tagged pointer in result register, or jumps to gc_required if new
637 // space is full.
638 void AllocateHeapNumber(Register result,
639 Register scratch,
640 Label* gc_required);
641
Leon Clarkee46be812010-01-19 14:06:41 +0000642 // Allocate a sequential string. All the header fields of the string object
643 // are initialized.
644 void AllocateTwoByteString(Register result,
645 Register length,
646 Register scratch1,
647 Register scratch2,
648 Register scratch3,
649 Label* gc_required);
650 void AllocateAsciiString(Register result,
651 Register length,
652 Register scratch1,
653 Register scratch2,
654 Register scratch3,
655 Label* gc_required);
656
657 // Allocate a raw cons string object. Only the map field of the result is
658 // initialized.
659 void AllocateConsString(Register result,
660 Register scratch1,
661 Register scratch2,
662 Label* gc_required);
663 void AllocateAsciiConsString(Register result,
664 Register scratch1,
665 Register scratch2,
666 Label* gc_required);
667
Steve Blocka7e24c12009-10-30 11:49:00 +0000668 // ---------------------------------------------------------------------------
669 // Support functions.
670
671 // Check if result is zero and op is negative.
672 void NegativeZeroTest(Register result, Register op, Label* then_label);
673
674 // Check if result is zero and op is negative in code using jump targets.
675 void NegativeZeroTest(CodeGenerator* cgen,
676 Register result,
677 Register op,
678 JumpTarget* then_target);
679
680 // Check if result is zero and any of op1 and op2 are negative.
681 // Register scratch is destroyed, and it must be different from op2.
682 void NegativeZeroTest(Register result, Register op1, Register op2,
683 Register scratch, Label* then_label);
684
685 // Try to get function prototype of a function and puts the value in
686 // the result register. Checks that the function really is a
687 // function and jumps to the miss label if the fast checks fail. The
688 // function register will be untouched; the other register may be
689 // clobbered.
690 void TryGetFunctionPrototype(Register function,
691 Register result,
692 Label* miss);
693
694 // Generates code for reporting that an illegal operation has
695 // occurred.
696 void IllegalOperation(int num_arguments);
697
Steve Blockd0582a62009-12-15 09:54:21 +0000698 // Find the function context up the context chain.
699 void LoadContext(Register dst, int context_chain_length);
700
Steve Blocka7e24c12009-10-30 11:49:00 +0000701 // ---------------------------------------------------------------------------
702 // Runtime calls
703
704 // Call a code stub.
705 void CallStub(CodeStub* stub);
706
Leon Clarkee46be812010-01-19 14:06:41 +0000707 // Tail call a code stub (jump).
708 void TailCallStub(CodeStub* stub);
709
Steve Blocka7e24c12009-10-30 11:49:00 +0000710 // Return from a code stub after popping its arguments.
711 void StubReturn(int argc);
712
713 // Call a runtime routine.
Steve Blocka7e24c12009-10-30 11:49:00 +0000714 void CallRuntime(Runtime::Function* f, int num_arguments);
715
716 // Convenience function: Same as above, but takes the fid instead.
717 void CallRuntime(Runtime::FunctionId id, int num_arguments);
718
Andrei Popescu402d9372010-02-26 13:31:12 +0000719 // Convenience function: call an external reference.
720 void CallExternalReference(const ExternalReference& ext,
721 int num_arguments);
722
Steve Blocka7e24c12009-10-30 11:49:00 +0000723 // Tail call of a runtime routine (jump).
Steve Block6ded16b2010-05-10 14:33:55 +0100724 // Like JumpToExternalReference, but also takes care of passing the number
725 // of parameters.
726 void TailCallExternalReference(const ExternalReference& ext,
727 int num_arguments,
728 int result_size);
729
730 // Convenience function: tail call a runtime routine (jump).
731 void TailCallRuntime(Runtime::FunctionId fid,
Steve Blocka7e24c12009-10-30 11:49:00 +0000732 int num_arguments,
733 int result_size);
734
735 // Jump to a runtime routine.
Steve Block6ded16b2010-05-10 14:33:55 +0100736 void JumpToExternalReference(const ExternalReference& ext, int result_size);
Steve Blocka7e24c12009-10-30 11:49:00 +0000737
Leon Clarke4515c472010-02-03 11:58:03 +0000738 // Before calling a C-function from generated code, align arguments on stack.
739 // After aligning the frame, arguments must be stored in esp[0], esp[4],
740 // etc., not pushed. The argument count assumes all arguments are word sized.
741 // The number of slots reserved for arguments depends on platform. On Windows
742 // stack slots are reserved for the arguments passed in registers. On other
743 // platforms stack slots are only reserved for the arguments actually passed
744 // on the stack.
745 void PrepareCallCFunction(int num_arguments);
746
747 // Calls a C function and cleans up the space for arguments allocated
748 // by PrepareCallCFunction. The called function is not allowed to trigger a
749 // garbage collection, since that might move the code and invalidate the
750 // return address (unless this is somehow accounted for by the called
751 // function).
752 void CallCFunction(ExternalReference function, int num_arguments);
753 void CallCFunction(Register function, int num_arguments);
754
755 // Calculate the number of stack slots to reserve for arguments when calling a
756 // C function.
757 int ArgumentStackSlotsForCFunctionCall(int num_arguments);
Steve Blocka7e24c12009-10-30 11:49:00 +0000758
759 // ---------------------------------------------------------------------------
760 // Utilities
761
762 void Ret();
763
Steve Blocka7e24c12009-10-30 11:49:00 +0000764 Handle<Object> CodeObject() { return code_object_; }
765
766
767 // ---------------------------------------------------------------------------
768 // StatsCounter support
769
770 void SetCounter(StatsCounter* counter, int value);
771 void IncrementCounter(StatsCounter* counter, int value);
772 void DecrementCounter(StatsCounter* counter, int value);
773
774
775 // ---------------------------------------------------------------------------
776 // Debugging
777
778 // Calls Abort(msg) if the condition cc is not satisfied.
779 // Use --debug_code to enable.
780 void Assert(Condition cc, const char* msg);
781
782 // Like Assert(), but always enabled.
783 void Check(Condition cc, const char* msg);
784
785 // Print a message to stdout and abort execution.
786 void Abort(const char* msg);
787
Steve Block6ded16b2010-05-10 14:33:55 +0100788 // Check that the stack is aligned.
789 void CheckStackAlignment();
790
Steve Blocka7e24c12009-10-30 11:49:00 +0000791 // Verify restrictions about code generated in stubs.
792 void set_generating_stub(bool value) { generating_stub_ = value; }
793 bool generating_stub() { return generating_stub_; }
794 void set_allow_stub_calls(bool value) { allow_stub_calls_ = value; }
795 bool allow_stub_calls() { return allow_stub_calls_; }
796
797 private:
Steve Blocka7e24c12009-10-30 11:49:00 +0000798 bool generating_stub_;
799 bool allow_stub_calls_;
Andrei Popescu31002712010-02-23 13:46:05 +0000800 // This handle will be patched with the code object on installation.
801 Handle<Object> code_object_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000802
803 // Helper functions for generating invokes.
804 void InvokePrologue(const ParameterCount& expected,
805 const ParameterCount& actual,
806 Handle<Code> code_constant,
807 Register code_register,
808 Label* done,
809 InvokeFlag flag);
810
Steve Blocka7e24c12009-10-30 11:49:00 +0000811 // Activation support.
812 void EnterFrame(StackFrame::Type type);
813 void LeaveFrame(StackFrame::Type type);
814
815 // Allocation support helpers.
Steve Block6ded16b2010-05-10 14:33:55 +0100816 // Loads the top of new-space into the result register.
817 // If flags contains RESULT_CONTAINS_TOP then result_end is valid and
818 // already contains the top of new-space, and scratch is invalid.
819 // Otherwise the address of the new-space top is loaded into scratch (if
820 // scratch is valid), and the new-space top is loaded into result.
Steve Blocka7e24c12009-10-30 11:49:00 +0000821 void LoadAllocationTopHelper(Register result,
822 Register result_end,
823 Register scratch,
824 AllocationFlags flags);
Steve Block6ded16b2010-05-10 14:33:55 +0100825 // Update allocation top with value in result_end register.
826 // If scratch is valid, it contains the address of the allocation top.
Steve Blocka7e24c12009-10-30 11:49:00 +0000827 void UpdateAllocationTopHelper(Register result_end, Register scratch);
828};
829
830
831// The code patcher is used to patch (typically) small parts of code e.g. for
832// debugging and other types of instrumentation. When using the code patcher
833// the exact number of bytes specified must be emitted. Is not legal to emit
834// relocation information. If any of these constraints are violated it causes
835// an assertion.
836class CodePatcher {
837 public:
838 CodePatcher(byte* address, int size);
839 virtual ~CodePatcher();
840
841 // Macro assembler to emit code.
842 MacroAssembler* masm() { return &masm_; }
843
844 private:
845 byte* address_; // The address of the code being patched.
846 int size_; // Number of bytes of the expected patch size.
847 MacroAssembler masm_; // Macro assembler used to generate the code.
848};
849
850
851// -----------------------------------------------------------------------------
852// Static helper functions.
853
854// Generate an Operand for loading a field from an object.
855static inline Operand FieldOperand(Register object, int offset) {
856 return Operand(object, offset - kHeapObjectTag);
857}
858
859
860// Generate an Operand for loading an indexed field from an object.
861static inline Operand FieldOperand(Register object,
862 Register index,
863 ScaleFactor scale,
864 int offset) {
865 return Operand(object, index, scale, offset - kHeapObjectTag);
866}
867
868
869#ifdef GENERATED_CODE_COVERAGE
870extern void LogGeneratedCodeCoverage(const char* file_line);
871#define CODE_COVERAGE_STRINGIFY(x) #x
872#define CODE_COVERAGE_TOSTRING(x) CODE_COVERAGE_STRINGIFY(x)
873#define __FILE_LINE__ __FILE__ ":" CODE_COVERAGE_TOSTRING(__LINE__)
874#define ACCESS_MASM(masm) { \
875 byte* x64_coverage_function = \
876 reinterpret_cast<byte*>(FUNCTION_ADDR(LogGeneratedCodeCoverage)); \
877 masm->pushfd(); \
878 masm->pushad(); \
879 masm->push(Immediate(reinterpret_cast<int>(&__FILE_LINE__))); \
880 masm->call(x64_coverage_function, RelocInfo::RUNTIME_ENTRY); \
881 masm->pop(rax); \
882 masm->popad(); \
883 masm->popfd(); \
884 } \
885 masm->
886#else
887#define ACCESS_MASM(masm) masm->
888#endif
889
890
891} } // namespace v8::internal
892
893#endif // V8_X64_MACRO_ASSEMBLER_X64_H_