blob: 10110d35eebb3765357ef99596c813e15bce7295 [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
ager@chromium.orge2902be2009-06-08 12:21:35 +000036// Default scratch register used by MacroAssembler (and other code that needs
37// a spare register). The register isn't callee save, and not used by the
38// function calling convention.
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +000039static const Register kScratchRegister = { 10 }; // r10.
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +000040static const Register kRootRegister = { 13 }; // r13
ager@chromium.orge2902be2009-06-08 12:21:35 +000041
fschneider@chromium.org0c20e672010-01-14 15:28:53 +000042// Convenience for platform-independent signatures.
43typedef Operand MemOperand;
44
ager@chromium.org9085a012009-05-11 19:22:57 +000045// Forward declaration.
46class JumpTarget;
47
ager@chromium.org4af710e2009-09-15 12:20:11 +000048struct SmiIndex {
49 SmiIndex(Register index_register, ScaleFactor scale)
50 : reg(index_register),
51 scale(scale) {}
52 Register reg;
53 ScaleFactor scale;
54};
ager@chromium.org9085a012009-05-11 19:22:57 +000055
ager@chromium.org9085a012009-05-11 19:22:57 +000056// MacroAssembler implements a collection of frequently used macros.
57class MacroAssembler: public Assembler {
58 public:
59 MacroAssembler(void* buffer, int size);
60
ager@chromium.org18ad94b2009-09-02 08:22:29 +000061 void LoadRoot(Register destination, Heap::RootListIndex index);
62 void CompareRoot(Register with, Heap::RootListIndex index);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +000063 void CompareRoot(Operand with, Heap::RootListIndex index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +000064 void PushRoot(Heap::RootListIndex index);
65
ager@chromium.org9085a012009-05-11 19:22:57 +000066 // ---------------------------------------------------------------------------
67 // GC Support
68
69 // Set the remembered set bit for [object+offset].
70 // object is the object being stored into, value is the object being stored.
71 // If offset is zero, then the scratch register contains the array index into
72 // the elements array represented as a Smi.
73 // All registers are clobbered by the operation.
74 void RecordWrite(Register object,
75 int offset,
76 Register value,
77 Register scratch);
78
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +000079 // Set the remembered set bit for [object+offset].
80 // The value is known to not be a smi.
81 // object is the object being stored into, value is the object being stored.
82 // If offset is zero, then the scratch register contains the array index into
83 // the elements array represented as a Smi.
84 // All registers are clobbered by the operation.
85 void RecordWriteNonSmi(Register object,
86 int offset,
87 Register value,
88 Register scratch);
89
90
ager@chromium.org9085a012009-05-11 19:22:57 +000091#ifdef ENABLE_DEBUGGER_SUPPORT
92 // ---------------------------------------------------------------------------
93 // Debugger Support
94
95 void SaveRegistersToMemory(RegList regs);
96 void RestoreRegistersFromMemory(RegList regs);
97 void PushRegistersFromMemory(RegList regs);
98 void PopRegistersToMemory(RegList regs);
99 void CopyRegistersFromStackToMemory(Register base,
100 Register scratch,
101 RegList regs);
ager@chromium.org5c838252010-02-19 08:53:10 +0000102 void DebugBreak();
ager@chromium.org9085a012009-05-11 19:22:57 +0000103#endif
104
105 // ---------------------------------------------------------------------------
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000106 // Stack limit support
107
108 // Do simple test for stack overflow. This doesn't handle an overflow.
109 void StackLimitCheck(Label* on_stack_limit_hit);
110
111 // ---------------------------------------------------------------------------
ager@chromium.org9085a012009-05-11 19:22:57 +0000112 // Activation frames
113
114 void EnterInternalFrame() { EnterFrame(StackFrame::INTERNAL); }
115 void LeaveInternalFrame() { LeaveFrame(StackFrame::INTERNAL); }
116
117 void EnterConstructFrame() { EnterFrame(StackFrame::CONSTRUCT); }
118 void LeaveConstructFrame() { LeaveFrame(StackFrame::CONSTRUCT); }
119
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000120 // Enter specific kind of exit frame; either in normal or
121 // debug mode. Expects the number of arguments in register rax and
ager@chromium.orga1645e22009-09-09 19:27:10 +0000122 // sets up the number of arguments in register rdi and the pointer
123 // to the first argument in register rsi.
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000124 void EnterExitFrame(ExitFrame::Mode mode, int result_size = 1);
ager@chromium.org9085a012009-05-11 19:22:57 +0000125
ager@chromium.orga1645e22009-09-09 19:27:10 +0000126 // Leave the current exit frame. Expects/provides the return value in
127 // register rax:rdx (untouched) and the pointer to the first
128 // argument in register rsi.
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000129 void LeaveExitFrame(ExitFrame::Mode mode, int result_size = 1);
ager@chromium.org9085a012009-05-11 19:22:57 +0000130
131
132 // ---------------------------------------------------------------------------
133 // JavaScript invokes
134
135 // Invoke the JavaScript function code by either calling or jumping.
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000136 void InvokeCode(Register code,
ager@chromium.org9085a012009-05-11 19:22:57 +0000137 const ParameterCount& expected,
138 const ParameterCount& actual,
139 InvokeFlag flag);
140
141 void InvokeCode(Handle<Code> code,
142 const ParameterCount& expected,
143 const ParameterCount& actual,
144 RelocInfo::Mode rmode,
145 InvokeFlag flag);
146
147 // Invoke the JavaScript function in the given register. Changes the
148 // current context to the context in the function before invoking.
149 void InvokeFunction(Register function,
150 const ParameterCount& actual,
151 InvokeFlag flag);
152
ager@chromium.org5c838252010-02-19 08:53:10 +0000153 void InvokeFunction(JSFunction* function,
154 const ParameterCount& actual,
155 InvokeFlag flag);
156
ager@chromium.org9085a012009-05-11 19:22:57 +0000157 // Invoke specified builtin JavaScript function. Adds an entry to
158 // the unresolved list if the name does not resolve.
159 void InvokeBuiltin(Builtins::JavaScript id, InvokeFlag flag);
160
161 // Store the code object for the given builtin in the target register.
162 void GetBuiltinEntry(Register target, Builtins::JavaScript id);
163
ager@chromium.org4af710e2009-09-15 12:20:11 +0000164
165 // ---------------------------------------------------------------------------
166 // Smi tagging, untagging and operations on tagged smis.
167
168 // Conversions between tagged smi values and non-tagged integer values.
169
170 // Tag an integer value. The result must be known to be a valid smi value.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000171 // Only uses the low 32 bits of the src register. Sets the N and Z flags
172 // based on the value of the resulting integer.
ager@chromium.org4af710e2009-09-15 12:20:11 +0000173 void Integer32ToSmi(Register dst, Register src);
174
175 // Tag an integer value if possible, or jump the integer value cannot be
176 // represented as a smi. Only uses the low 32 bit of the src registers.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000177 // NOTICE: Destroys the dst register even if unsuccessful!
ager@chromium.org4af710e2009-09-15 12:20:11 +0000178 void Integer32ToSmi(Register dst, Register src, Label* on_overflow);
179
180 // Adds constant to src and tags the result as a smi.
181 // Result must be a valid smi.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000182 void Integer64PlusConstantToSmi(Register dst, Register src, int constant);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000183
184 // Convert smi to 32-bit integer. I.e., not sign extended into
185 // high 32 bits of destination.
186 void SmiToInteger32(Register dst, Register src);
187
188 // Convert smi to 64-bit integer (sign extended if necessary).
189 void SmiToInteger64(Register dst, Register src);
190
191 // Multiply a positive smi's integer value by a power of two.
192 // Provides result as 64-bit integer value.
193 void PositiveSmiTimesPowerOfTwoToInteger64(Register dst,
194 Register src,
195 int power);
196
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000197 // Simple comparison of smis.
198 void SmiCompare(Register dst, Register src);
199 void SmiCompare(Register dst, Smi* src);
200 void SmiCompare(const Operand& dst, Register src);
201 void SmiCompare(const Operand& dst, Smi* src);
202 // Sets sign and zero flags depending on value of smi in register.
203 void SmiTest(Register src);
204
ager@chromium.org4af710e2009-09-15 12:20:11 +0000205 // Functions performing a check on a known or potential smi. Returns
206 // a condition that is satisfied if the check is successful.
207
208 // Is the value a tagged smi.
209 Condition CheckSmi(Register src);
210
ager@chromium.org4af710e2009-09-15 12:20:11 +0000211 // Is the value a positive tagged smi.
212 Condition CheckPositiveSmi(Register src);
213
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000214 // Are both values tagged smis.
ager@chromium.org4af710e2009-09-15 12:20:11 +0000215 Condition CheckBothSmi(Register first, Register second);
216
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000217 // Are both values tagged smis.
218 Condition CheckBothPositiveSmi(Register first, Register second);
219
220 // Are either value a tagged smi.
221 Condition CheckEitherSmi(Register first, Register second);
222
ager@chromium.org4af710e2009-09-15 12:20:11 +0000223 // Is the value the minimum smi value (since we are using
224 // two's complement numbers, negating the value is known to yield
225 // a non-smi value).
226 Condition CheckIsMinSmi(Register src);
227
ager@chromium.org4af710e2009-09-15 12:20:11 +0000228 // Checks whether an 32-bit integer value is a valid for conversion
229 // to a smi.
230 Condition CheckInteger32ValidSmiValue(Register src);
231
ager@chromium.org3811b432009-10-28 14:53:37 +0000232 // Checks whether an 32-bit unsigned integer value is a valid for
233 // conversion to a smi.
234 Condition CheckUInteger32ValidSmiValue(Register src);
235
ager@chromium.org4af710e2009-09-15 12:20:11 +0000236 // Test-and-jump functions. Typically combines a check function
237 // above with a conditional jump.
238
239 // Jump if the value cannot be represented by a smi.
240 void JumpIfNotValidSmiValue(Register src, Label* on_invalid);
241
ager@chromium.org3811b432009-10-28 14:53:37 +0000242 // Jump if the unsigned integer value cannot be represented by a smi.
243 void JumpIfUIntNotValidSmiValue(Register src, Label* on_invalid);
244
ager@chromium.org4af710e2009-09-15 12:20:11 +0000245 // Jump to label if the value is a tagged smi.
246 void JumpIfSmi(Register src, Label* on_smi);
247
248 // Jump to label if the value is not a tagged smi.
249 void JumpIfNotSmi(Register src, Label* on_not_smi);
250
251 // Jump to label if the value is not a positive tagged smi.
252 void JumpIfNotPositiveSmi(Register src, Label* on_not_smi);
253
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000254 // Jump to label if the value, which must be a tagged smi, has value equal
ager@chromium.org4af710e2009-09-15 12:20:11 +0000255 // to the constant.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000256 void JumpIfSmiEqualsConstant(Register src, Smi* constant, Label* on_equals);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000257
ager@chromium.org4af710e2009-09-15 12:20:11 +0000258 // Jump if either or both register are not smi values.
259 void JumpIfNotBothSmi(Register src1, Register src2, Label* on_not_both_smi);
260
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000261 // Jump if either or both register are not positive smi values.
262 void JumpIfNotBothPositiveSmi(Register src1, Register src2,
263 Label* on_not_both_smi);
264
ager@chromium.org4af710e2009-09-15 12:20:11 +0000265 // Operations on tagged smi values.
266
267 // Smis represent a subset of integers. The subset is always equivalent to
268 // a two's complement interpretation of a fixed number of bits.
269
270 // Optimistically adds an integer constant to a supposed smi.
271 // If the src is not a smi, or the result is not a smi, jump to
272 // the label.
273 void SmiTryAddConstant(Register dst,
274 Register src,
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000275 Smi* constant,
ager@chromium.org4af710e2009-09-15 12:20:11 +0000276 Label* on_not_smi_result);
277
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000278 // Add an integer constant to a tagged smi, giving a tagged smi as result.
279 // No overflow testing on the result is done.
280 void SmiAddConstant(Register dst, Register src, Smi* constant);
281
ager@chromium.org4af710e2009-09-15 12:20:11 +0000282 // Add an integer constant to a tagged smi, giving a tagged smi as result,
283 // or jumping to a label if the result cannot be represented by a smi.
ager@chromium.org4af710e2009-09-15 12:20:11 +0000284 void SmiAddConstant(Register dst,
285 Register src,
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000286 Smi* constant,
ager@chromium.org4af710e2009-09-15 12:20:11 +0000287 Label* on_not_smi_result);
288
289 // Subtract an integer constant from a tagged smi, giving a tagged smi as
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000290 // result. No testing on the result is done.
291 void SmiSubConstant(Register dst, Register src, Smi* constant);
292
293 // Subtract an integer constant from a tagged smi, giving a tagged smi as
ager@chromium.org4af710e2009-09-15 12:20:11 +0000294 // result, or jumping to a label if the result cannot be represented by a smi.
ager@chromium.org4af710e2009-09-15 12:20:11 +0000295 void SmiSubConstant(Register dst,
296 Register src,
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000297 Smi* constant,
ager@chromium.org4af710e2009-09-15 12:20:11 +0000298 Label* on_not_smi_result);
299
300 // Negating a smi can give a negative zero or too large positive value.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000301 // NOTICE: This operation jumps on success, not failure!
ager@chromium.org4af710e2009-09-15 12:20:11 +0000302 void SmiNeg(Register dst,
303 Register src,
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000304 Label* on_smi_result);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000305
306 // Adds smi values and return the result as a smi.
307 // If dst is src1, then src1 will be destroyed, even if
308 // the operation is unsuccessful.
309 void SmiAdd(Register dst,
310 Register src1,
311 Register src2,
312 Label* on_not_smi_result);
313
314 // Subtracts smi values and return the result as a smi.
315 // If dst is src1, then src1 will be destroyed, even if
316 // the operation is unsuccessful.
317 void SmiSub(Register dst,
318 Register src1,
319 Register src2,
320 Label* on_not_smi_result);
321
322 // Multiplies smi values and return the result as a smi,
323 // if possible.
324 // If dst is src1, then src1 will be destroyed, even if
325 // the operation is unsuccessful.
326 void SmiMul(Register dst,
327 Register src1,
328 Register src2,
329 Label* on_not_smi_result);
330
331 // Divides one smi by another and returns the quotient.
332 // Clobbers rax and rdx registers.
333 void SmiDiv(Register dst,
334 Register src1,
335 Register src2,
336 Label* on_not_smi_result);
337
338 // Divides one smi by another and returns the remainder.
339 // Clobbers rax and rdx registers.
340 void SmiMod(Register dst,
341 Register src1,
342 Register src2,
343 Label* on_not_smi_result);
344
345 // Bitwise operations.
346 void SmiNot(Register dst, Register src);
347 void SmiAnd(Register dst, Register src1, Register src2);
348 void SmiOr(Register dst, Register src1, Register src2);
349 void SmiXor(Register dst, Register src1, Register src2);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000350 void SmiAndConstant(Register dst, Register src1, Smi* constant);
351 void SmiOrConstant(Register dst, Register src1, Smi* constant);
352 void SmiXorConstant(Register dst, Register src1, Smi* constant);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000353
354 void SmiShiftLeftConstant(Register dst,
355 Register src,
356 int shift_value,
357 Label* on_not_smi_result);
358 void SmiShiftLogicalRightConstant(Register dst,
359 Register src,
360 int shift_value,
361 Label* on_not_smi_result);
362 void SmiShiftArithmeticRightConstant(Register dst,
363 Register src,
364 int shift_value);
365
366 // Shifts a smi value to the left, and returns the result if that is a smi.
367 // Uses and clobbers rcx, so dst may not be rcx.
368 void SmiShiftLeft(Register dst,
369 Register src1,
370 Register src2,
371 Label* on_not_smi_result);
372 // Shifts a smi value to the right, shifting in zero bits at the top, and
373 // returns the unsigned intepretation of the result if that is a smi.
374 // Uses and clobbers rcx, so dst may not be rcx.
375 void SmiShiftLogicalRight(Register dst,
376 Register src1,
377 Register src2,
378 Label* on_not_smi_result);
379 // Shifts a smi value to the right, sign extending the top, and
380 // returns the signed intepretation of the result. That will always
381 // be a valid smi value, since it's numerically smaller than the
382 // original.
383 // Uses and clobbers rcx, so dst may not be rcx.
384 void SmiShiftArithmeticRight(Register dst,
385 Register src1,
386 Register src2);
387
388 // Specialized operations
389
390 // Select the non-smi register of two registers where exactly one is a
391 // smi. If neither are smis, jump to the failure label.
392 void SelectNonSmi(Register dst,
393 Register src1,
394 Register src2,
395 Label* on_not_smis);
396
397 // Converts, if necessary, a smi to a combination of number and
398 // multiplier to be used as a scaled index.
399 // The src register contains a *positive* smi value. The shift is the
400 // power of two to multiply the index value by (e.g.
401 // to index by smi-value * kPointerSize, pass the smi and kPointerSizeLog2).
402 // The returned index register may be either src or dst, depending
403 // on what is most efficient. If src and dst are different registers,
404 // src is always unchanged.
405 SmiIndex SmiToIndex(Register dst, Register src, int shift);
406
407 // Converts a positive smi to a negative index.
408 SmiIndex SmiToNegativeIndex(Register dst, Register src, int shift);
409
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000410 // Basic Smi operations.
ager@chromium.org3811b432009-10-28 14:53:37 +0000411 void Move(Register dst, Smi* source) {
412 Set(dst, reinterpret_cast<int64_t>(source));
413 }
414
415 void Move(const Operand& dst, Smi* source) {
416 Set(dst, reinterpret_cast<int64_t>(source));
417 }
418
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000419 void Push(Smi* smi);
420 void Test(const Operand& dst, Smi* source);
421
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000422 // ---------------------------------------------------------------------------
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000423 // String macros.
424 void JumpIfNotBothSequentialAsciiStrings(Register first_object,
425 Register second_object,
426 Register scratch1,
427 Register scratch2,
428 Label* on_not_both_flat_ascii);
429
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000430 // Check whether the instance type represents a flat ascii string. Jump to the
431 // label if not. If the instance type can be scratched specify same register
432 // for both instance type and scratch.
433 void JumpIfInstanceTypeIsNotSequentialAscii(Register instance_type,
434 Register scratch,
435 Label *on_not_flat_ascii_string);
436
437 void JumpIfBothInstanceTypesAreNotSequentialAscii(
438 Register first_object_instance_type,
439 Register second_object_instance_type,
440 Register scratch1,
441 Register scratch2,
442 Label* on_fail);
443
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000444 // ---------------------------------------------------------------------------
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000445 // Macro instructions.
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000446
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000447 // Load a register with a long value as efficiently as possible.
ager@chromium.orge2902be2009-06-08 12:21:35 +0000448 void Set(Register dst, int64_t x);
449 void Set(const Operand& dst, int64_t x);
ager@chromium.org9085a012009-05-11 19:22:57 +0000450
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000451 // Handle support
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000452 void Move(Register dst, Handle<Object> source);
453 void Move(const Operand& dst, Handle<Object> source);
454 void Cmp(Register dst, Handle<Object> source);
ager@chromium.org3e875802009-06-29 08:26:34 +0000455 void Cmp(const Operand& dst, Handle<Object> source);
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000456 void Push(Handle<Object> source);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000457
458 // Emit code to discard a non-negative number of pointer-sized elements
459 // from the stack, clobbering only the rsp register.
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000460 void Drop(int stack_elements);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000461
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000462 void Call(Label* target) { call(target); }
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000463
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000464 // Control Flow
465 void Jump(Address destination, RelocInfo::Mode rmode);
466 void Jump(ExternalReference ext);
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000467 void Jump(Handle<Code> code_object, RelocInfo::Mode rmode);
468
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000469 void Call(Address destination, RelocInfo::Mode rmode);
470 void Call(ExternalReference ext);
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000471 void Call(Handle<Code> code_object, RelocInfo::Mode rmode);
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000472
ager@chromium.org9085a012009-05-11 19:22:57 +0000473 // Compare object type for heap object.
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000474 // Always use unsigned comparisons: above and below, not less and greater.
ager@chromium.org9085a012009-05-11 19:22:57 +0000475 // Incoming register is heap_object and outgoing register is map.
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000476 // They may be the same register, and may be kScratchRegister.
ager@chromium.org9085a012009-05-11 19:22:57 +0000477 void CmpObjectType(Register heap_object, InstanceType type, Register map);
478
479 // Compare instance type for map.
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000480 // Always use unsigned comparisons: above and below, not less and greater.
ager@chromium.org9085a012009-05-11 19:22:57 +0000481 void CmpInstanceType(Register map, InstanceType type);
482
ager@chromium.org5c838252010-02-19 08:53:10 +0000483 // Check if the map of an object is equal to a specified map and
484 // branch to label if not. Skip the smi check if not required
485 // (object is known to be a heap object)
486 void CheckMap(Register obj,
487 Handle<Map> map,
488 Label* fail,
489 bool is_heap_object);
490
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000491 // Check if the object in register heap_object is a string. Afterwards the
492 // register map contains the object map and the register instance_type
493 // contains the instance_type. The registers map and instance_type can be the
494 // same in which case it contains the instance type afterwards. Either of the
495 // registers map and instance_type can be the same as heap_object.
496 Condition IsObjectStringType(Register heap_object,
497 Register map,
498 Register instance_type);
499
ager@chromium.org9085a012009-05-11 19:22:57 +0000500 // FCmp is similar to integer cmp, but requires unsigned
501 // jcc instructions (je, ja, jae, jb, jbe, je, and jz).
502 void FCmp();
503
ager@chromium.org5c838252010-02-19 08:53:10 +0000504 // Abort execution if argument is not a number. Used in debug code.
505 void AbortIfNotNumber(Register object, const char* msg);
506
lrn@chromium.org25156de2010-04-06 13:10:27 +0000507 // Abort execution if argument is not a smi. Used in debug code.
508 void AbortIfNotSmi(Register object, const char* msg);
509
ager@chromium.org9085a012009-05-11 19:22:57 +0000510 // ---------------------------------------------------------------------------
511 // Exception handling
512
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000513 // Push a new try handler and link into try handler chain. The return
514 // address must be pushed before calling this helper.
ager@chromium.org9085a012009-05-11 19:22:57 +0000515 void PushTryHandler(CodeLocation try_location, HandlerType type);
516
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000517 // Unlink the stack handler on top of the stack from the try handler chain.
518 void PopTryHandler();
ager@chromium.org9085a012009-05-11 19:22:57 +0000519
520 // ---------------------------------------------------------------------------
521 // Inline caching support
522
523 // Generates code that verifies that the maps of objects in the
524 // prototype chain of object hasn't changed since the code was
525 // generated and branches to the miss label if any map has. If
526 // necessary the function also generates code for security check
527 // in case of global object holders. The scratch and holder
528 // registers are always clobbered, but the object register is only
529 // clobbered if it the same as the holder register. The function
530 // returns a register containing the holder - either object_reg or
531 // holder_reg.
532 Register CheckMaps(JSObject* object, Register object_reg,
533 JSObject* holder, Register holder_reg,
534 Register scratch, Label* miss);
535
536 // Generate code for checking access rights - used for security checks
537 // on access to global objects across environments. The holder register
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000538 // is left untouched, but the scratch register and kScratchRegister,
539 // which must be different, are clobbered.
ager@chromium.org9085a012009-05-11 19:22:57 +0000540 void CheckAccessGlobalProxy(Register holder_reg,
541 Register scratch,
542 Label* miss);
543
544
545 // ---------------------------------------------------------------------------
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000546 // Allocation support
547
548 // Allocate an object in new space. If the new space is exhausted control
549 // continues at the gc_required label. The allocated object is returned in
550 // result and end of the new object is returned in result_end. The register
551 // scratch can be passed as no_reg in which case an additional object
552 // reference will be added to the reloc info. The returned pointers in result
553 // and result_end have not yet been tagged as heap objects. If
554 // result_contains_top_on_entry is true the content of result is known to be
555 // the allocation top on entry (could be result_end from a previous call to
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000556 // AllocateInNewSpace). If result_contains_top_on_entry is true scratch
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000557 // should be no_reg as it is never used.
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000558 void AllocateInNewSpace(int object_size,
559 Register result,
560 Register result_end,
561 Register scratch,
562 Label* gc_required,
563 AllocationFlags flags);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000564
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000565 void AllocateInNewSpace(int header_size,
566 ScaleFactor element_size,
567 Register element_count,
568 Register result,
569 Register result_end,
570 Register scratch,
571 Label* gc_required,
572 AllocationFlags flags);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000573
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000574 void AllocateInNewSpace(Register object_size,
575 Register result,
576 Register result_end,
577 Register scratch,
578 Label* gc_required,
579 AllocationFlags flags);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000580
581 // Undo allocation in new space. The object passed and objects allocated after
582 // it will no longer be allocated. Make sure that no pointers are left to the
583 // object(s) no longer allocated as they would be invalid when allocation is
584 // un-done.
585 void UndoAllocationInNewSpace(Register object);
586
ager@chromium.org3811b432009-10-28 14:53:37 +0000587 // Allocate a heap number in new space with undefined value. Returns
588 // tagged pointer in result register, or jumps to gc_required if new
589 // space is full.
590 void AllocateHeapNumber(Register result,
591 Register scratch,
592 Label* gc_required);
593
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000594 // Allocate a sequential string. All the header fields of the string object
595 // are initialized.
596 void AllocateTwoByteString(Register result,
597 Register length,
598 Register scratch1,
599 Register scratch2,
600 Register scratch3,
601 Label* gc_required);
602 void AllocateAsciiString(Register result,
603 Register length,
604 Register scratch1,
605 Register scratch2,
606 Register scratch3,
607 Label* gc_required);
608
609 // Allocate a raw cons string object. Only the map field of the result is
610 // initialized.
611 void AllocateConsString(Register result,
612 Register scratch1,
613 Register scratch2,
614 Label* gc_required);
615 void AllocateAsciiConsString(Register result,
616 Register scratch1,
617 Register scratch2,
618 Label* gc_required);
619
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000620 // ---------------------------------------------------------------------------
ager@chromium.org9085a012009-05-11 19:22:57 +0000621 // Support functions.
622
623 // Check if result is zero and op is negative.
624 void NegativeZeroTest(Register result, Register op, Label* then_label);
625
626 // Check if result is zero and op is negative in code using jump targets.
627 void NegativeZeroTest(CodeGenerator* cgen,
628 Register result,
629 Register op,
630 JumpTarget* then_target);
631
632 // Check if result is zero and any of op1 and op2 are negative.
633 // Register scratch is destroyed, and it must be different from op2.
634 void NegativeZeroTest(Register result, Register op1, Register op2,
635 Register scratch, Label* then_label);
636
637 // Try to get function prototype of a function and puts the value in
638 // the result register. Checks that the function really is a
639 // function and jumps to the miss label if the fast checks fail. The
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000640 // function register will be untouched; the other register may be
ager@chromium.org9085a012009-05-11 19:22:57 +0000641 // clobbered.
642 void TryGetFunctionPrototype(Register function,
643 Register result,
ager@chromium.org9085a012009-05-11 19:22:57 +0000644 Label* miss);
645
646 // Generates code for reporting that an illegal operation has
647 // occurred.
648 void IllegalOperation(int num_arguments);
649
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000650 // Find the function context up the context chain.
651 void LoadContext(Register dst, int context_chain_length);
652
ager@chromium.org9085a012009-05-11 19:22:57 +0000653 // ---------------------------------------------------------------------------
654 // Runtime calls
655
656 // Call a code stub.
657 void CallStub(CodeStub* stub);
658
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000659 // Tail call a code stub (jump).
660 void TailCallStub(CodeStub* stub);
661
ager@chromium.org9085a012009-05-11 19:22:57 +0000662 // Return from a code stub after popping its arguments.
663 void StubReturn(int argc);
664
665 // Call a runtime routine.
ager@chromium.org9085a012009-05-11 19:22:57 +0000666 void CallRuntime(Runtime::Function* f, int num_arguments);
667
668 // Convenience function: Same as above, but takes the fid instead.
669 void CallRuntime(Runtime::FunctionId id, int num_arguments);
670
ager@chromium.org5c838252010-02-19 08:53:10 +0000671 // Convenience function: call an external reference.
672 void CallExternalReference(const ExternalReference& ext,
673 int num_arguments);
674
ager@chromium.org9085a012009-05-11 19:22:57 +0000675 // Tail call of a runtime routine (jump).
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000676 // Like JumpToExternalReference, but also takes care of passing the number
677 // of parameters.
678 void TailCallExternalReference(const ExternalReference& ext,
679 int num_arguments,
680 int result_size);
681
682 // Convenience function: tail call a runtime routine (jump).
683 void TailCallRuntime(Runtime::FunctionId fid,
ager@chromium.orga1645e22009-09-09 19:27:10 +0000684 int num_arguments,
685 int result_size);
ager@chromium.org9085a012009-05-11 19:22:57 +0000686
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000687 // Jump to a runtime routine.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000688 void JumpToExternalReference(const ExternalReference& ext, int result_size);
ager@chromium.org9085a012009-05-11 19:22:57 +0000689
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000690 // Before calling a C-function from generated code, align arguments on stack.
691 // After aligning the frame, arguments must be stored in esp[0], esp[4],
692 // etc., not pushed. The argument count assumes all arguments are word sized.
693 // The number of slots reserved for arguments depends on platform. On Windows
694 // stack slots are reserved for the arguments passed in registers. On other
695 // platforms stack slots are only reserved for the arguments actually passed
696 // on the stack.
697 void PrepareCallCFunction(int num_arguments);
698
699 // Calls a C function and cleans up the space for arguments allocated
700 // by PrepareCallCFunction. The called function is not allowed to trigger a
701 // garbage collection, since that might move the code and invalidate the
702 // return address (unless this is somehow accounted for by the called
703 // function).
704 void CallCFunction(ExternalReference function, int num_arguments);
705 void CallCFunction(Register function, int num_arguments);
706
707 // Calculate the number of stack slots to reserve for arguments when calling a
708 // C function.
709 int ArgumentStackSlotsForCFunctionCall(int num_arguments);
ager@chromium.org9085a012009-05-11 19:22:57 +0000710
711 // ---------------------------------------------------------------------------
712 // Utilities
713
714 void Ret();
715
ager@chromium.org9085a012009-05-11 19:22:57 +0000716 Handle<Object> CodeObject() { return code_object_; }
717
718
719 // ---------------------------------------------------------------------------
720 // StatsCounter support
721
722 void SetCounter(StatsCounter* counter, int value);
723 void IncrementCounter(StatsCounter* counter, int value);
724 void DecrementCounter(StatsCounter* counter, int value);
725
726
727 // ---------------------------------------------------------------------------
728 // Debugging
729
730 // Calls Abort(msg) if the condition cc is not satisfied.
731 // Use --debug_code to enable.
732 void Assert(Condition cc, const char* msg);
733
734 // Like Assert(), but always enabled.
735 void Check(Condition cc, const char* msg);
736
737 // Print a message to stdout and abort execution.
738 void Abort(const char* msg);
739
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000740 // Check that the stack is aligned.
741 void CheckStackAlignment();
742
ager@chromium.org9085a012009-05-11 19:22:57 +0000743 // Verify restrictions about code generated in stubs.
744 void set_generating_stub(bool value) { generating_stub_ = value; }
745 bool generating_stub() { return generating_stub_; }
746 void set_allow_stub_calls(bool value) { allow_stub_calls_ = value; }
747 bool allow_stub_calls() { return allow_stub_calls_; }
748
749 private:
ager@chromium.org9085a012009-05-11 19:22:57 +0000750 bool generating_stub_;
751 bool allow_stub_calls_;
ager@chromium.org5c838252010-02-19 08:53:10 +0000752 // This handle will be patched with the code object on installation.
753 Handle<Object> code_object_;
ager@chromium.org9085a012009-05-11 19:22:57 +0000754
755 // Helper functions for generating invokes.
756 void InvokePrologue(const ParameterCount& expected,
757 const ParameterCount& actual,
758 Handle<Code> code_constant,
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000759 Register code_register,
ager@chromium.org9085a012009-05-11 19:22:57 +0000760 Label* done,
761 InvokeFlag flag);
762
ager@chromium.org9085a012009-05-11 19:22:57 +0000763 // Activation support.
764 void EnterFrame(StackFrame::Type type);
765 void LeaveFrame(StackFrame::Type type);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000766
767 // Allocation support helpers.
768 void LoadAllocationTopHelper(Register result,
769 Register result_end,
770 Register scratch,
ager@chromium.orga1645e22009-09-09 19:27:10 +0000771 AllocationFlags flags);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000772 void UpdateAllocationTopHelper(Register result_end, Register scratch);
ager@chromium.org9085a012009-05-11 19:22:57 +0000773};
774
775
ager@chromium.org4af710e2009-09-15 12:20:11 +0000776// The code patcher is used to patch (typically) small parts of code e.g. for
777// debugging and other types of instrumentation. When using the code patcher
778// the exact number of bytes specified must be emitted. Is not legal to emit
779// relocation information. If any of these constraints are violated it causes
780// an assertion.
781class CodePatcher {
782 public:
783 CodePatcher(byte* address, int size);
784 virtual ~CodePatcher();
785
786 // Macro assembler to emit code.
787 MacroAssembler* masm() { return &masm_; }
788
789 private:
790 byte* address_; // The address of the code being patched.
791 int size_; // Number of bytes of the expected patch size.
792 MacroAssembler masm_; // Macro assembler used to generate the code.
793};
794
795
ager@chromium.org9085a012009-05-11 19:22:57 +0000796// -----------------------------------------------------------------------------
797// Static helper functions.
798
799// Generate an Operand for loading a field from an object.
800static inline Operand FieldOperand(Register object, int offset) {
801 return Operand(object, offset - kHeapObjectTag);
802}
803
804
805// Generate an Operand for loading an indexed field from an object.
806static inline Operand FieldOperand(Register object,
807 Register index,
808 ScaleFactor scale,
809 int offset) {
810 return Operand(object, index, scale, offset - kHeapObjectTag);
811}
812
813
814#ifdef GENERATED_CODE_COVERAGE
815extern void LogGeneratedCodeCoverage(const char* file_line);
816#define CODE_COVERAGE_STRINGIFY(x) #x
817#define CODE_COVERAGE_TOSTRING(x) CODE_COVERAGE_STRINGIFY(x)
818#define __FILE_LINE__ __FILE__ ":" CODE_COVERAGE_TOSTRING(__LINE__)
819#define ACCESS_MASM(masm) { \
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000820 byte* x64_coverage_function = \
ager@chromium.org9085a012009-05-11 19:22:57 +0000821 reinterpret_cast<byte*>(FUNCTION_ADDR(LogGeneratedCodeCoverage)); \
822 masm->pushfd(); \
823 masm->pushad(); \
824 masm->push(Immediate(reinterpret_cast<int>(&__FILE_LINE__))); \
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000825 masm->call(x64_coverage_function, RelocInfo::RUNTIME_ENTRY); \
ager@chromium.org9085a012009-05-11 19:22:57 +0000826 masm->pop(rax); \
827 masm->popad(); \
828 masm->popfd(); \
829 } \
830 masm->
831#else
832#define ACCESS_MASM(masm) masm->
833#endif
834
835
836} } // namespace v8::internal
837
838#endif // V8_X64_MACRO_ASSEMBLER_X64_H_