blob: 95cd1cfacd3c35b40f45cec23d5ffbbcbdba5822 [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
kmillikin@chromium.org4111b802010-05-03 10:34:42 +000069 // Check if object is in new space. The condition cc can be equal or
70 // not_equal. If it is equal a jump will be done if the object is on new
71 // space. The register scratch can be object itself, but it will be clobbered.
72 void InNewSpace(Register object,
73 Register scratch,
74 Condition cc,
75 Label* branch);
76
ager@chromium.org9085a012009-05-11 19:22:57 +000077 // Set the remembered set bit for [object+offset].
78 // object is the object being stored into, value is the object being stored.
79 // If offset is zero, then the scratch register contains the array index into
80 // the elements array represented as a Smi.
81 // All registers are clobbered by the operation.
82 void RecordWrite(Register object,
83 int offset,
84 Register value,
85 Register scratch);
86
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +000087 // Set the remembered set bit for [object+offset].
88 // The value is known to not be a smi.
89 // object is the object being stored into, value is the object being stored.
90 // If offset is zero, then the scratch register contains the array index into
91 // the elements array represented as a Smi.
92 // All registers are clobbered by the operation.
93 void RecordWriteNonSmi(Register object,
94 int offset,
95 Register value,
96 Register scratch);
97
98
ager@chromium.org9085a012009-05-11 19:22:57 +000099#ifdef ENABLE_DEBUGGER_SUPPORT
100 // ---------------------------------------------------------------------------
101 // Debugger Support
102
103 void SaveRegistersToMemory(RegList regs);
104 void RestoreRegistersFromMemory(RegList regs);
105 void PushRegistersFromMemory(RegList regs);
106 void PopRegistersToMemory(RegList regs);
107 void CopyRegistersFromStackToMemory(Register base,
108 Register scratch,
109 RegList regs);
ager@chromium.org5c838252010-02-19 08:53:10 +0000110 void DebugBreak();
ager@chromium.org9085a012009-05-11 19:22:57 +0000111#endif
112
113 // ---------------------------------------------------------------------------
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000114 // Stack limit support
115
116 // Do simple test for stack overflow. This doesn't handle an overflow.
117 void StackLimitCheck(Label* on_stack_limit_hit);
118
119 // ---------------------------------------------------------------------------
ager@chromium.org9085a012009-05-11 19:22:57 +0000120 // Activation frames
121
122 void EnterInternalFrame() { EnterFrame(StackFrame::INTERNAL); }
123 void LeaveInternalFrame() { LeaveFrame(StackFrame::INTERNAL); }
124
125 void EnterConstructFrame() { EnterFrame(StackFrame::CONSTRUCT); }
126 void LeaveConstructFrame() { LeaveFrame(StackFrame::CONSTRUCT); }
127
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000128 // Enter specific kind of exit frame; either in normal or
129 // debug mode. Expects the number of arguments in register rax and
ager@chromium.orga1645e22009-09-09 19:27:10 +0000130 // sets up the number of arguments in register rdi and the pointer
131 // to the first argument in register rsi.
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000132 void EnterExitFrame(ExitFrame::Mode mode, int result_size = 1);
ager@chromium.org9085a012009-05-11 19:22:57 +0000133
ager@chromium.orga1645e22009-09-09 19:27:10 +0000134 // Leave the current exit frame. Expects/provides the return value in
135 // register rax:rdx (untouched) and the pointer to the first
136 // argument in register rsi.
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000137 void LeaveExitFrame(ExitFrame::Mode mode, int result_size = 1);
ager@chromium.org9085a012009-05-11 19:22:57 +0000138
139
140 // ---------------------------------------------------------------------------
141 // JavaScript invokes
142
143 // Invoke the JavaScript function code by either calling or jumping.
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000144 void InvokeCode(Register code,
ager@chromium.org9085a012009-05-11 19:22:57 +0000145 const ParameterCount& expected,
146 const ParameterCount& actual,
147 InvokeFlag flag);
148
149 void InvokeCode(Handle<Code> code,
150 const ParameterCount& expected,
151 const ParameterCount& actual,
152 RelocInfo::Mode rmode,
153 InvokeFlag flag);
154
155 // Invoke the JavaScript function in the given register. Changes the
156 // current context to the context in the function before invoking.
157 void InvokeFunction(Register function,
158 const ParameterCount& actual,
159 InvokeFlag flag);
160
ager@chromium.org5c838252010-02-19 08:53:10 +0000161 void InvokeFunction(JSFunction* function,
162 const ParameterCount& actual,
163 InvokeFlag flag);
164
ager@chromium.org9085a012009-05-11 19:22:57 +0000165 // Invoke specified builtin JavaScript function. Adds an entry to
166 // the unresolved list if the name does not resolve.
167 void InvokeBuiltin(Builtins::JavaScript id, InvokeFlag flag);
168
169 // Store the code object for the given builtin in the target register.
170 void GetBuiltinEntry(Register target, Builtins::JavaScript id);
171
ager@chromium.org4af710e2009-09-15 12:20:11 +0000172
173 // ---------------------------------------------------------------------------
174 // Smi tagging, untagging and operations on tagged smis.
175
176 // Conversions between tagged smi values and non-tagged integer values.
177
178 // Tag an integer value. The result must be known to be a valid smi value.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000179 // Only uses the low 32 bits of the src register. Sets the N and Z flags
180 // based on the value of the resulting integer.
ager@chromium.org4af710e2009-09-15 12:20:11 +0000181 void Integer32ToSmi(Register dst, Register src);
182
183 // Tag an integer value if possible, or jump the integer value cannot be
184 // represented as a smi. Only uses the low 32 bit of the src registers.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000185 // NOTICE: Destroys the dst register even if unsuccessful!
ager@chromium.org4af710e2009-09-15 12:20:11 +0000186 void Integer32ToSmi(Register dst, Register src, Label* on_overflow);
187
188 // Adds constant to src and tags the result as a smi.
189 // Result must be a valid smi.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000190 void Integer64PlusConstantToSmi(Register dst, Register src, int constant);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000191
192 // Convert smi to 32-bit integer. I.e., not sign extended into
193 // high 32 bits of destination.
194 void SmiToInteger32(Register dst, Register src);
195
196 // Convert smi to 64-bit integer (sign extended if necessary).
197 void SmiToInteger64(Register dst, Register src);
198
199 // Multiply a positive smi's integer value by a power of two.
200 // Provides result as 64-bit integer value.
201 void PositiveSmiTimesPowerOfTwoToInteger64(Register dst,
202 Register src,
203 int power);
204
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000205 // Simple comparison of smis.
206 void SmiCompare(Register dst, Register src);
207 void SmiCompare(Register dst, Smi* src);
208 void SmiCompare(const Operand& dst, Register src);
209 void SmiCompare(const Operand& dst, Smi* src);
210 // Sets sign and zero flags depending on value of smi in register.
211 void SmiTest(Register src);
212
ager@chromium.org4af710e2009-09-15 12:20:11 +0000213 // Functions performing a check on a known or potential smi. Returns
214 // a condition that is satisfied if the check is successful.
215
216 // Is the value a tagged smi.
217 Condition CheckSmi(Register src);
218
ager@chromium.org4af710e2009-09-15 12:20:11 +0000219 // Is the value a positive tagged smi.
220 Condition CheckPositiveSmi(Register src);
221
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000222 // Are both values tagged smis.
ager@chromium.org4af710e2009-09-15 12:20:11 +0000223 Condition CheckBothSmi(Register first, Register second);
224
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000225 // Are both values tagged smis.
226 Condition CheckBothPositiveSmi(Register first, Register second);
227
228 // Are either value a tagged smi.
229 Condition CheckEitherSmi(Register first, Register second);
230
ager@chromium.org4af710e2009-09-15 12:20:11 +0000231 // Is the value the minimum smi value (since we are using
232 // two's complement numbers, negating the value is known to yield
233 // a non-smi value).
234 Condition CheckIsMinSmi(Register src);
235
ager@chromium.org4af710e2009-09-15 12:20:11 +0000236 // Checks whether an 32-bit integer value is a valid for conversion
237 // to a smi.
238 Condition CheckInteger32ValidSmiValue(Register src);
239
ager@chromium.org3811b432009-10-28 14:53:37 +0000240 // Checks whether an 32-bit unsigned integer value is a valid for
241 // conversion to a smi.
242 Condition CheckUInteger32ValidSmiValue(Register src);
243
ager@chromium.org4af710e2009-09-15 12:20:11 +0000244 // Test-and-jump functions. Typically combines a check function
245 // above with a conditional jump.
246
247 // Jump if the value cannot be represented by a smi.
248 void JumpIfNotValidSmiValue(Register src, Label* on_invalid);
249
ager@chromium.org3811b432009-10-28 14:53:37 +0000250 // Jump if the unsigned integer value cannot be represented by a smi.
251 void JumpIfUIntNotValidSmiValue(Register src, Label* on_invalid);
252
ager@chromium.org4af710e2009-09-15 12:20:11 +0000253 // Jump to label if the value is a tagged smi.
254 void JumpIfSmi(Register src, Label* on_smi);
255
256 // Jump to label if the value is not a tagged smi.
257 void JumpIfNotSmi(Register src, Label* on_not_smi);
258
259 // Jump to label if the value is not a positive tagged smi.
260 void JumpIfNotPositiveSmi(Register src, Label* on_not_smi);
261
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000262 // Jump to label if the value, which must be a tagged smi, has value equal
ager@chromium.org4af710e2009-09-15 12:20:11 +0000263 // to the constant.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000264 void JumpIfSmiEqualsConstant(Register src, Smi* constant, Label* on_equals);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000265
ager@chromium.org4af710e2009-09-15 12:20:11 +0000266 // Jump if either or both register are not smi values.
267 void JumpIfNotBothSmi(Register src1, Register src2, Label* on_not_both_smi);
268
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000269 // Jump if either or both register are not positive smi values.
270 void JumpIfNotBothPositiveSmi(Register src1, Register src2,
271 Label* on_not_both_smi);
272
ager@chromium.org4af710e2009-09-15 12:20:11 +0000273 // Operations on tagged smi values.
274
275 // Smis represent a subset of integers. The subset is always equivalent to
276 // a two's complement interpretation of a fixed number of bits.
277
278 // Optimistically adds an integer constant to a supposed smi.
279 // If the src is not a smi, or the result is not a smi, jump to
280 // the label.
281 void SmiTryAddConstant(Register dst,
282 Register src,
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000283 Smi* constant,
ager@chromium.org4af710e2009-09-15 12:20:11 +0000284 Label* on_not_smi_result);
285
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000286 // Add an integer constant to a tagged smi, giving a tagged smi as result.
287 // No overflow testing on the result is done.
288 void SmiAddConstant(Register dst, Register src, Smi* constant);
289
ager@chromium.org4af710e2009-09-15 12:20:11 +0000290 // Add an integer constant to a tagged smi, giving a tagged smi as result,
291 // or jumping to a label if the result cannot be represented by a smi.
ager@chromium.org4af710e2009-09-15 12:20:11 +0000292 void SmiAddConstant(Register dst,
293 Register src,
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000294 Smi* constant,
ager@chromium.org4af710e2009-09-15 12:20:11 +0000295 Label* on_not_smi_result);
296
297 // Subtract an integer constant from a tagged smi, giving a tagged smi as
kmillikin@chromium.orgf8253d72010-05-03 09:56:08 +0000298 // result. No testing on the result is done.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000299 void SmiSubConstant(Register dst, Register src, Smi* constant);
300
301 // Subtract an integer constant from a tagged smi, giving a tagged smi as
ager@chromium.org4af710e2009-09-15 12:20:11 +0000302 // result, or jumping to a label if the result cannot be represented by a smi.
ager@chromium.org4af710e2009-09-15 12:20:11 +0000303 void SmiSubConstant(Register dst,
304 Register src,
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000305 Smi* constant,
ager@chromium.org4af710e2009-09-15 12:20:11 +0000306 Label* on_not_smi_result);
307
308 // Negating a smi can give a negative zero or too large positive value.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000309 // NOTICE: This operation jumps on success, not failure!
ager@chromium.org4af710e2009-09-15 12:20:11 +0000310 void SmiNeg(Register dst,
311 Register src,
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000312 Label* on_smi_result);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000313
314 // Adds 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 SmiAdd(Register dst,
318 Register src1,
319 Register src2,
320 Label* on_not_smi_result);
321
322 // Subtracts smi values and return the result as a smi.
323 // If dst is src1, then src1 will be destroyed, even if
324 // the operation is unsuccessful.
325 void SmiSub(Register dst,
326 Register src1,
327 Register src2,
328 Label* on_not_smi_result);
329
330 // Multiplies smi values and return the result as a smi,
331 // if possible.
332 // If dst is src1, then src1 will be destroyed, even if
333 // the operation is unsuccessful.
334 void SmiMul(Register dst,
335 Register src1,
336 Register src2,
337 Label* on_not_smi_result);
338
339 // Divides one smi by another and returns the quotient.
340 // Clobbers rax and rdx registers.
341 void SmiDiv(Register dst,
342 Register src1,
343 Register src2,
344 Label* on_not_smi_result);
345
346 // Divides one smi by another and returns the remainder.
347 // Clobbers rax and rdx registers.
348 void SmiMod(Register dst,
349 Register src1,
350 Register src2,
351 Label* on_not_smi_result);
352
353 // Bitwise operations.
354 void SmiNot(Register dst, Register src);
355 void SmiAnd(Register dst, Register src1, Register src2);
356 void SmiOr(Register dst, Register src1, Register src2);
357 void SmiXor(Register dst, Register src1, Register src2);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000358 void SmiAndConstant(Register dst, Register src1, Smi* constant);
359 void SmiOrConstant(Register dst, Register src1, Smi* constant);
360 void SmiXorConstant(Register dst, Register src1, Smi* constant);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000361
362 void SmiShiftLeftConstant(Register dst,
363 Register src,
364 int shift_value,
365 Label* on_not_smi_result);
366 void SmiShiftLogicalRightConstant(Register dst,
367 Register src,
368 int shift_value,
369 Label* on_not_smi_result);
370 void SmiShiftArithmeticRightConstant(Register dst,
371 Register src,
372 int shift_value);
373
374 // Shifts a smi value to the left, and returns the result if that is a smi.
375 // Uses and clobbers rcx, so dst may not be rcx.
376 void SmiShiftLeft(Register dst,
377 Register src1,
378 Register src2,
379 Label* on_not_smi_result);
380 // Shifts a smi value to the right, shifting in zero bits at the top, and
381 // returns the unsigned intepretation of the result if that is a smi.
382 // Uses and clobbers rcx, so dst may not be rcx.
383 void SmiShiftLogicalRight(Register dst,
384 Register src1,
385 Register src2,
386 Label* on_not_smi_result);
387 // Shifts a smi value to the right, sign extending the top, and
388 // returns the signed intepretation of the result. That will always
389 // be a valid smi value, since it's numerically smaller than the
390 // original.
391 // Uses and clobbers rcx, so dst may not be rcx.
392 void SmiShiftArithmeticRight(Register dst,
393 Register src1,
394 Register src2);
395
396 // Specialized operations
397
398 // Select the non-smi register of two registers where exactly one is a
399 // smi. If neither are smis, jump to the failure label.
400 void SelectNonSmi(Register dst,
401 Register src1,
402 Register src2,
403 Label* on_not_smis);
404
405 // Converts, if necessary, a smi to a combination of number and
406 // multiplier to be used as a scaled index.
407 // The src register contains a *positive* smi value. The shift is the
408 // power of two to multiply the index value by (e.g.
409 // to index by smi-value * kPointerSize, pass the smi and kPointerSizeLog2).
410 // The returned index register may be either src or dst, depending
411 // on what is most efficient. If src and dst are different registers,
412 // src is always unchanged.
413 SmiIndex SmiToIndex(Register dst, Register src, int shift);
414
415 // Converts a positive smi to a negative index.
416 SmiIndex SmiToNegativeIndex(Register dst, Register src, int shift);
417
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000418 // Basic Smi operations.
ager@chromium.org3811b432009-10-28 14:53:37 +0000419 void Move(Register dst, Smi* source) {
420 Set(dst, reinterpret_cast<int64_t>(source));
421 }
422
423 void Move(const Operand& dst, Smi* source) {
424 Set(dst, reinterpret_cast<int64_t>(source));
425 }
426
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000427 void Push(Smi* smi);
428 void Test(const Operand& dst, Smi* source);
429
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000430 // ---------------------------------------------------------------------------
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000431 // String macros.
432 void JumpIfNotBothSequentialAsciiStrings(Register first_object,
433 Register second_object,
434 Register scratch1,
435 Register scratch2,
436 Label* on_not_both_flat_ascii);
437
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000438 // Check whether the instance type represents a flat ascii string. Jump to the
439 // label if not. If the instance type can be scratched specify same register
440 // for both instance type and scratch.
441 void JumpIfInstanceTypeIsNotSequentialAscii(Register instance_type,
442 Register scratch,
443 Label *on_not_flat_ascii_string);
444
445 void JumpIfBothInstanceTypesAreNotSequentialAscii(
446 Register first_object_instance_type,
447 Register second_object_instance_type,
448 Register scratch1,
449 Register scratch2,
450 Label* on_fail);
451
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000452 // ---------------------------------------------------------------------------
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000453 // Macro instructions.
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000454
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000455 // Load a register with a long value as efficiently as possible.
ager@chromium.orge2902be2009-06-08 12:21:35 +0000456 void Set(Register dst, int64_t x);
457 void Set(const Operand& dst, int64_t x);
ager@chromium.org9085a012009-05-11 19:22:57 +0000458
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000459 // Handle support
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000460 void Move(Register dst, Handle<Object> source);
461 void Move(const Operand& dst, Handle<Object> source);
462 void Cmp(Register dst, Handle<Object> source);
ager@chromium.org3e875802009-06-29 08:26:34 +0000463 void Cmp(const Operand& dst, Handle<Object> source);
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000464 void Push(Handle<Object> source);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000465
466 // Emit code to discard a non-negative number of pointer-sized elements
467 // from the stack, clobbering only the rsp register.
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000468 void Drop(int stack_elements);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000469
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000470 void Call(Label* target) { call(target); }
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000471
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000472 // Control Flow
473 void Jump(Address destination, RelocInfo::Mode rmode);
474 void Jump(ExternalReference ext);
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000475 void Jump(Handle<Code> code_object, RelocInfo::Mode rmode);
476
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000477 void Call(Address destination, RelocInfo::Mode rmode);
478 void Call(ExternalReference ext);
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000479 void Call(Handle<Code> code_object, RelocInfo::Mode rmode);
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000480
ager@chromium.org9085a012009-05-11 19:22:57 +0000481 // Compare object type for heap object.
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000482 // Always use unsigned comparisons: above and below, not less and greater.
ager@chromium.org9085a012009-05-11 19:22:57 +0000483 // Incoming register is heap_object and outgoing register is map.
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000484 // They may be the same register, and may be kScratchRegister.
ager@chromium.org9085a012009-05-11 19:22:57 +0000485 void CmpObjectType(Register heap_object, InstanceType type, Register map);
486
487 // Compare instance type for map.
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000488 // Always use unsigned comparisons: above and below, not less and greater.
ager@chromium.org9085a012009-05-11 19:22:57 +0000489 void CmpInstanceType(Register map, InstanceType type);
490
ager@chromium.org5c838252010-02-19 08:53:10 +0000491 // Check if the map of an object is equal to a specified map and
492 // branch to label if not. Skip the smi check if not required
493 // (object is known to be a heap object)
494 void CheckMap(Register obj,
495 Handle<Map> map,
496 Label* fail,
497 bool is_heap_object);
498
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000499 // Check if the object in register heap_object is a string. Afterwards the
500 // register map contains the object map and the register instance_type
501 // contains the instance_type. The registers map and instance_type can be the
502 // same in which case it contains the instance type afterwards. Either of the
503 // registers map and instance_type can be the same as heap_object.
504 Condition IsObjectStringType(Register heap_object,
505 Register map,
506 Register instance_type);
507
ager@chromium.org9085a012009-05-11 19:22:57 +0000508 // FCmp is similar to integer cmp, but requires unsigned
509 // jcc instructions (je, ja, jae, jb, jbe, je, and jz).
510 void FCmp();
511
ager@chromium.org5c838252010-02-19 08:53:10 +0000512 // Abort execution if argument is not a number. Used in debug code.
513 void AbortIfNotNumber(Register object, const char* msg);
514
lrn@chromium.org25156de2010-04-06 13:10:27 +0000515 // Abort execution if argument is not a smi. Used in debug code.
516 void AbortIfNotSmi(Register object, const char* msg);
517
ager@chromium.org9085a012009-05-11 19:22:57 +0000518 // ---------------------------------------------------------------------------
519 // Exception handling
520
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000521 // Push a new try handler and link into try handler chain. The return
522 // address must be pushed before calling this helper.
ager@chromium.org9085a012009-05-11 19:22:57 +0000523 void PushTryHandler(CodeLocation try_location, HandlerType type);
524
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000525 // Unlink the stack handler on top of the stack from the try handler chain.
526 void PopTryHandler();
ager@chromium.org9085a012009-05-11 19:22:57 +0000527
528 // ---------------------------------------------------------------------------
529 // Inline caching support
530
531 // Generates code that verifies that the maps of objects in the
532 // prototype chain of object hasn't changed since the code was
533 // generated and branches to the miss label if any map has. If
534 // necessary the function also generates code for security check
535 // in case of global object holders. The scratch and holder
536 // registers are always clobbered, but the object register is only
537 // clobbered if it the same as the holder register. The function
538 // returns a register containing the holder - either object_reg or
539 // holder_reg.
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000540 // The function can optionally (when save_at_depth !=
541 // kInvalidProtoDepth) save the object at the given depth by moving
542 // it to [rsp + kPointerSize].
ager@chromium.org9085a012009-05-11 19:22:57 +0000543 Register CheckMaps(JSObject* object, Register object_reg,
544 JSObject* holder, Register holder_reg,
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000545 Register scratch,
546 int save_at_depth,
547 Label* miss);
ager@chromium.org9085a012009-05-11 19:22:57 +0000548
549 // Generate code for checking access rights - used for security checks
550 // on access to global objects across environments. The holder register
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000551 // is left untouched, but the scratch register and kScratchRegister,
552 // which must be different, are clobbered.
ager@chromium.org9085a012009-05-11 19:22:57 +0000553 void CheckAccessGlobalProxy(Register holder_reg,
554 Register scratch,
555 Label* miss);
556
557
558 // ---------------------------------------------------------------------------
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000559 // Allocation support
560
561 // Allocate an object in new space. If the new space is exhausted control
562 // continues at the gc_required label. The allocated object is returned in
563 // result and end of the new object is returned in result_end. The register
564 // scratch can be passed as no_reg in which case an additional object
565 // reference will be added to the reloc info. The returned pointers in result
566 // and result_end have not yet been tagged as heap objects. If
567 // result_contains_top_on_entry is true the content of result is known to be
568 // the allocation top on entry (could be result_end from a previous call to
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000569 // AllocateInNewSpace). If result_contains_top_on_entry is true scratch
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000570 // should be no_reg as it is never used.
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000571 void AllocateInNewSpace(int object_size,
572 Register result,
573 Register result_end,
574 Register scratch,
575 Label* gc_required,
576 AllocationFlags flags);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000577
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000578 void AllocateInNewSpace(int header_size,
579 ScaleFactor element_size,
580 Register element_count,
581 Register result,
582 Register result_end,
583 Register scratch,
584 Label* gc_required,
585 AllocationFlags flags);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000586
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000587 void AllocateInNewSpace(Register object_size,
588 Register result,
589 Register result_end,
590 Register scratch,
591 Label* gc_required,
592 AllocationFlags flags);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000593
594 // Undo allocation in new space. The object passed and objects allocated after
595 // it will no longer be allocated. Make sure that no pointers are left to the
596 // object(s) no longer allocated as they would be invalid when allocation is
597 // un-done.
598 void UndoAllocationInNewSpace(Register object);
599
ager@chromium.org3811b432009-10-28 14:53:37 +0000600 // Allocate a heap number in new space with undefined value. Returns
601 // tagged pointer in result register, or jumps to gc_required if new
602 // space is full.
603 void AllocateHeapNumber(Register result,
604 Register scratch,
605 Label* gc_required);
606
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000607 // Allocate a sequential string. All the header fields of the string object
608 // are initialized.
609 void AllocateTwoByteString(Register result,
610 Register length,
611 Register scratch1,
612 Register scratch2,
613 Register scratch3,
614 Label* gc_required);
615 void AllocateAsciiString(Register result,
616 Register length,
617 Register scratch1,
618 Register scratch2,
619 Register scratch3,
620 Label* gc_required);
621
622 // Allocate a raw cons string object. Only the map field of the result is
623 // initialized.
624 void AllocateConsString(Register result,
625 Register scratch1,
626 Register scratch2,
627 Label* gc_required);
628 void AllocateAsciiConsString(Register result,
629 Register scratch1,
630 Register scratch2,
631 Label* gc_required);
632
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000633 // ---------------------------------------------------------------------------
ager@chromium.org9085a012009-05-11 19:22:57 +0000634 // Support functions.
635
636 // Check if result is zero and op is negative.
637 void NegativeZeroTest(Register result, Register op, Label* then_label);
638
639 // Check if result is zero and op is negative in code using jump targets.
640 void NegativeZeroTest(CodeGenerator* cgen,
641 Register result,
642 Register op,
643 JumpTarget* then_target);
644
645 // Check if result is zero and any of op1 and op2 are negative.
646 // Register scratch is destroyed, and it must be different from op2.
647 void NegativeZeroTest(Register result, Register op1, Register op2,
648 Register scratch, Label* then_label);
649
650 // Try to get function prototype of a function and puts the value in
651 // the result register. Checks that the function really is a
652 // function and jumps to the miss label if the fast checks fail. The
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000653 // function register will be untouched; the other register may be
ager@chromium.org9085a012009-05-11 19:22:57 +0000654 // clobbered.
655 void TryGetFunctionPrototype(Register function,
656 Register result,
ager@chromium.org9085a012009-05-11 19:22:57 +0000657 Label* miss);
658
659 // Generates code for reporting that an illegal operation has
660 // occurred.
661 void IllegalOperation(int num_arguments);
662
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000663 // Find the function context up the context chain.
664 void LoadContext(Register dst, int context_chain_length);
665
ager@chromium.org9085a012009-05-11 19:22:57 +0000666 // ---------------------------------------------------------------------------
667 // Runtime calls
668
669 // Call a code stub.
670 void CallStub(CodeStub* stub);
671
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000672 // Tail call a code stub (jump).
673 void TailCallStub(CodeStub* stub);
674
ager@chromium.org9085a012009-05-11 19:22:57 +0000675 // Return from a code stub after popping its arguments.
676 void StubReturn(int argc);
677
678 // Call a runtime routine.
ager@chromium.org9085a012009-05-11 19:22:57 +0000679 void CallRuntime(Runtime::Function* f, int num_arguments);
680
681 // Convenience function: Same as above, but takes the fid instead.
682 void CallRuntime(Runtime::FunctionId id, int num_arguments);
683
ager@chromium.org5c838252010-02-19 08:53:10 +0000684 // Convenience function: call an external reference.
685 void CallExternalReference(const ExternalReference& ext,
686 int num_arguments);
687
ager@chromium.org9085a012009-05-11 19:22:57 +0000688 // Tail call of a runtime routine (jump).
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000689 // Like JumpToExternalReference, but also takes care of passing the number
690 // of parameters.
691 void TailCallExternalReference(const ExternalReference& ext,
692 int num_arguments,
693 int result_size);
694
695 // Convenience function: tail call a runtime routine (jump).
696 void TailCallRuntime(Runtime::FunctionId fid,
ager@chromium.orga1645e22009-09-09 19:27:10 +0000697 int num_arguments,
698 int result_size);
ager@chromium.org9085a012009-05-11 19:22:57 +0000699
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000700 // Jump to a runtime routine.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000701 void JumpToExternalReference(const ExternalReference& ext, int result_size);
ager@chromium.org9085a012009-05-11 19:22:57 +0000702
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000703 // Before calling a C-function from generated code, align arguments on stack.
704 // After aligning the frame, arguments must be stored in esp[0], esp[4],
705 // etc., not pushed. The argument count assumes all arguments are word sized.
706 // The number of slots reserved for arguments depends on platform. On Windows
707 // stack slots are reserved for the arguments passed in registers. On other
708 // platforms stack slots are only reserved for the arguments actually passed
709 // on the stack.
710 void PrepareCallCFunction(int num_arguments);
711
712 // Calls a C function and cleans up the space for arguments allocated
713 // by PrepareCallCFunction. The called function is not allowed to trigger a
714 // garbage collection, since that might move the code and invalidate the
715 // return address (unless this is somehow accounted for by the called
716 // function).
717 void CallCFunction(ExternalReference function, int num_arguments);
718 void CallCFunction(Register function, int num_arguments);
719
720 // Calculate the number of stack slots to reserve for arguments when calling a
721 // C function.
722 int ArgumentStackSlotsForCFunctionCall(int num_arguments);
ager@chromium.org9085a012009-05-11 19:22:57 +0000723
724 // ---------------------------------------------------------------------------
725 // Utilities
726
727 void Ret();
728
ager@chromium.org9085a012009-05-11 19:22:57 +0000729 Handle<Object> CodeObject() { return code_object_; }
730
731
732 // ---------------------------------------------------------------------------
733 // StatsCounter support
734
735 void SetCounter(StatsCounter* counter, int value);
736 void IncrementCounter(StatsCounter* counter, int value);
737 void DecrementCounter(StatsCounter* counter, int value);
738
739
740 // ---------------------------------------------------------------------------
741 // Debugging
742
743 // Calls Abort(msg) if the condition cc is not satisfied.
744 // Use --debug_code to enable.
745 void Assert(Condition cc, const char* msg);
746
747 // Like Assert(), but always enabled.
748 void Check(Condition cc, const char* msg);
749
750 // Print a message to stdout and abort execution.
751 void Abort(const char* msg);
752
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000753 // Check that the stack is aligned.
754 void CheckStackAlignment();
755
ager@chromium.org9085a012009-05-11 19:22:57 +0000756 // Verify restrictions about code generated in stubs.
757 void set_generating_stub(bool value) { generating_stub_ = value; }
758 bool generating_stub() { return generating_stub_; }
759 void set_allow_stub_calls(bool value) { allow_stub_calls_ = value; }
760 bool allow_stub_calls() { return allow_stub_calls_; }
761
762 private:
ager@chromium.org9085a012009-05-11 19:22:57 +0000763 bool generating_stub_;
764 bool allow_stub_calls_;
ager@chromium.org5c838252010-02-19 08:53:10 +0000765 // This handle will be patched with the code object on installation.
766 Handle<Object> code_object_;
ager@chromium.org9085a012009-05-11 19:22:57 +0000767
768 // Helper functions for generating invokes.
769 void InvokePrologue(const ParameterCount& expected,
770 const ParameterCount& actual,
771 Handle<Code> code_constant,
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000772 Register code_register,
ager@chromium.org9085a012009-05-11 19:22:57 +0000773 Label* done,
774 InvokeFlag flag);
775
ager@chromium.org9085a012009-05-11 19:22:57 +0000776 // Activation support.
777 void EnterFrame(StackFrame::Type type);
778 void LeaveFrame(StackFrame::Type type);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000779
780 // Allocation support helpers.
781 void LoadAllocationTopHelper(Register result,
782 Register result_end,
783 Register scratch,
ager@chromium.orga1645e22009-09-09 19:27:10 +0000784 AllocationFlags flags);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000785 void UpdateAllocationTopHelper(Register result_end, Register scratch);
ager@chromium.org9085a012009-05-11 19:22:57 +0000786};
787
788
ager@chromium.org4af710e2009-09-15 12:20:11 +0000789// The code patcher is used to patch (typically) small parts of code e.g. for
790// debugging and other types of instrumentation. When using the code patcher
791// the exact number of bytes specified must be emitted. Is not legal to emit
792// relocation information. If any of these constraints are violated it causes
793// an assertion.
794class CodePatcher {
795 public:
796 CodePatcher(byte* address, int size);
797 virtual ~CodePatcher();
798
799 // Macro assembler to emit code.
800 MacroAssembler* masm() { return &masm_; }
801
802 private:
803 byte* address_; // The address of the code being patched.
804 int size_; // Number of bytes of the expected patch size.
805 MacroAssembler masm_; // Macro assembler used to generate the code.
806};
807
808
ager@chromium.org9085a012009-05-11 19:22:57 +0000809// -----------------------------------------------------------------------------
810// Static helper functions.
811
812// Generate an Operand for loading a field from an object.
813static inline Operand FieldOperand(Register object, int offset) {
814 return Operand(object, offset - kHeapObjectTag);
815}
816
817
818// Generate an Operand for loading an indexed field from an object.
819static inline Operand FieldOperand(Register object,
820 Register index,
821 ScaleFactor scale,
822 int offset) {
823 return Operand(object, index, scale, offset - kHeapObjectTag);
824}
825
826
827#ifdef GENERATED_CODE_COVERAGE
828extern void LogGeneratedCodeCoverage(const char* file_line);
829#define CODE_COVERAGE_STRINGIFY(x) #x
830#define CODE_COVERAGE_TOSTRING(x) CODE_COVERAGE_STRINGIFY(x)
831#define __FILE_LINE__ __FILE__ ":" CODE_COVERAGE_TOSTRING(__LINE__)
832#define ACCESS_MASM(masm) { \
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000833 byte* x64_coverage_function = \
ager@chromium.org9085a012009-05-11 19:22:57 +0000834 reinterpret_cast<byte*>(FUNCTION_ADDR(LogGeneratedCodeCoverage)); \
835 masm->pushfd(); \
836 masm->pushad(); \
837 masm->push(Immediate(reinterpret_cast<int>(&__FILE_LINE__))); \
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000838 masm->call(x64_coverage_function, RelocInfo::RUNTIME_ENTRY); \
ager@chromium.org9085a012009-05-11 19:22:57 +0000839 masm->pop(rax); \
840 masm->popad(); \
841 masm->popfd(); \
842 } \
843 masm->
844#else
845#define ACCESS_MASM(masm) masm->
846#endif
847
848
849} } // namespace v8::internal
850
851#endif // V8_X64_MACRO_ASSEMBLER_X64_H_