blob: 2aa4ce0556f257c35f607c69816d466619521ed9 [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.
39static const Register kScratchRegister = r10;
40
ager@chromium.org9085a012009-05-11 19:22:57 +000041// Forward declaration.
42class JumpTarget;
43
ager@chromium.org4af710e2009-09-15 12:20:11 +000044struct SmiIndex {
45 SmiIndex(Register index_register, ScaleFactor scale)
46 : reg(index_register),
47 scale(scale) {}
48 Register reg;
49 ScaleFactor scale;
50};
ager@chromium.org9085a012009-05-11 19:22:57 +000051
ager@chromium.org9085a012009-05-11 19:22:57 +000052// MacroAssembler implements a collection of frequently used macros.
53class MacroAssembler: public Assembler {
54 public:
55 MacroAssembler(void* buffer, int size);
56
ager@chromium.org18ad94b2009-09-02 08:22:29 +000057 void LoadRoot(Register destination, Heap::RootListIndex index);
58 void CompareRoot(Register with, Heap::RootListIndex index);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +000059 void CompareRoot(Operand with, Heap::RootListIndex index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +000060 void PushRoot(Heap::RootListIndex index);
61
ager@chromium.org9085a012009-05-11 19:22:57 +000062 // ---------------------------------------------------------------------------
63 // GC Support
64
65 // Set the remembered set bit for [object+offset].
66 // object is the object being stored into, value is the object being stored.
67 // If offset is zero, then the scratch register contains the array index into
68 // the elements array represented as a Smi.
69 // All registers are clobbered by the operation.
70 void RecordWrite(Register object,
71 int offset,
72 Register value,
73 Register scratch);
74
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +000075 // Set the remembered set bit for [object+offset].
76 // The value is known to not be a smi.
77 // object is the object being stored into, value is the object being stored.
78 // If offset is zero, then the scratch register contains the array index into
79 // the elements array represented as a Smi.
80 // All registers are clobbered by the operation.
81 void RecordWriteNonSmi(Register object,
82 int offset,
83 Register value,
84 Register scratch);
85
86
ager@chromium.org9085a012009-05-11 19:22:57 +000087#ifdef ENABLE_DEBUGGER_SUPPORT
88 // ---------------------------------------------------------------------------
89 // Debugger Support
90
91 void SaveRegistersToMemory(RegList regs);
92 void RestoreRegistersFromMemory(RegList regs);
93 void PushRegistersFromMemory(RegList regs);
94 void PopRegistersToMemory(RegList regs);
95 void CopyRegistersFromStackToMemory(Register base,
96 Register scratch,
97 RegList regs);
98#endif
99
100 // ---------------------------------------------------------------------------
101 // Activation frames
102
103 void EnterInternalFrame() { EnterFrame(StackFrame::INTERNAL); }
104 void LeaveInternalFrame() { LeaveFrame(StackFrame::INTERNAL); }
105
106 void EnterConstructFrame() { EnterFrame(StackFrame::CONSTRUCT); }
107 void LeaveConstructFrame() { LeaveFrame(StackFrame::CONSTRUCT); }
108
109 // Enter specific kind of exit frame; either EXIT or
ager@chromium.orga1645e22009-09-09 19:27:10 +0000110 // EXIT_DEBUG. Expects the number of arguments in register rax and
111 // sets up the number of arguments in register rdi and the pointer
112 // to the first argument in register rsi.
113 void EnterExitFrame(StackFrame::Type type, int result_size = 1);
ager@chromium.org9085a012009-05-11 19:22:57 +0000114
ager@chromium.orga1645e22009-09-09 19:27:10 +0000115 // Leave the current exit frame. Expects/provides the return value in
116 // register rax:rdx (untouched) and the pointer to the first
117 // argument in register rsi.
118 void LeaveExitFrame(StackFrame::Type type, int result_size = 1);
ager@chromium.org9085a012009-05-11 19:22:57 +0000119
120
121 // ---------------------------------------------------------------------------
122 // JavaScript invokes
123
124 // Invoke the JavaScript function code by either calling or jumping.
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000125 void InvokeCode(Register code,
ager@chromium.org9085a012009-05-11 19:22:57 +0000126 const ParameterCount& expected,
127 const ParameterCount& actual,
128 InvokeFlag flag);
129
130 void InvokeCode(Handle<Code> code,
131 const ParameterCount& expected,
132 const ParameterCount& actual,
133 RelocInfo::Mode rmode,
134 InvokeFlag flag);
135
136 // Invoke the JavaScript function in the given register. Changes the
137 // current context to the context in the function before invoking.
138 void InvokeFunction(Register function,
139 const ParameterCount& actual,
140 InvokeFlag flag);
141
142 // Invoke specified builtin JavaScript function. Adds an entry to
143 // the unresolved list if the name does not resolve.
144 void InvokeBuiltin(Builtins::JavaScript id, InvokeFlag flag);
145
146 // Store the code object for the given builtin in the target register.
147 void GetBuiltinEntry(Register target, Builtins::JavaScript id);
148
ager@chromium.org4af710e2009-09-15 12:20:11 +0000149
150 // ---------------------------------------------------------------------------
151 // Smi tagging, untagging and operations on tagged smis.
152
153 // Conversions between tagged smi values and non-tagged integer values.
154
155 // Tag an integer value. The result must be known to be a valid smi value.
156 // Only uses the low 32 bits of the src register.
157 void Integer32ToSmi(Register dst, Register src);
158
159 // Tag an integer value if possible, or jump the integer value cannot be
160 // represented as a smi. Only uses the low 32 bit of the src registers.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000161 // NOTICE: Destroys the dst register even if unsuccessful!
ager@chromium.org4af710e2009-09-15 12:20:11 +0000162 void Integer32ToSmi(Register dst, Register src, Label* on_overflow);
163
164 // Adds constant to src and tags the result as a smi.
165 // Result must be a valid smi.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000166 void Integer64PlusConstantToSmi(Register dst, Register src, int constant);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000167
168 // Convert smi to 32-bit integer. I.e., not sign extended into
169 // high 32 bits of destination.
170 void SmiToInteger32(Register dst, Register src);
171
172 // Convert smi to 64-bit integer (sign extended if necessary).
173 void SmiToInteger64(Register dst, Register src);
174
175 // Multiply a positive smi's integer value by a power of two.
176 // Provides result as 64-bit integer value.
177 void PositiveSmiTimesPowerOfTwoToInteger64(Register dst,
178 Register src,
179 int power);
180
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000181 // Simple comparison of smis.
182 void SmiCompare(Register dst, Register src);
183 void SmiCompare(Register dst, Smi* src);
184 void SmiCompare(const Operand& dst, Register src);
185 void SmiCompare(const Operand& dst, Smi* src);
186 // Sets sign and zero flags depending on value of smi in register.
187 void SmiTest(Register src);
188
ager@chromium.org4af710e2009-09-15 12:20:11 +0000189 // Functions performing a check on a known or potential smi. Returns
190 // a condition that is satisfied if the check is successful.
191
192 // Is the value a tagged smi.
193 Condition CheckSmi(Register src);
194
ager@chromium.org4af710e2009-09-15 12:20:11 +0000195 // Is the value a positive tagged smi.
196 Condition CheckPositiveSmi(Register src);
197
ager@chromium.org4af710e2009-09-15 12:20:11 +0000198 // Are both values are tagged smis.
199 Condition CheckBothSmi(Register first, Register second);
200
ager@chromium.org4af710e2009-09-15 12:20:11 +0000201 // Is the value the minimum smi value (since we are using
202 // two's complement numbers, negating the value is known to yield
203 // a non-smi value).
204 Condition CheckIsMinSmi(Register src);
205
ager@chromium.org4af710e2009-09-15 12:20:11 +0000206 // Checks whether an 32-bit integer value is a valid for conversion
207 // to a smi.
208 Condition CheckInteger32ValidSmiValue(Register src);
209
210 // Test-and-jump functions. Typically combines a check function
211 // above with a conditional jump.
212
213 // Jump if the value cannot be represented by a smi.
214 void JumpIfNotValidSmiValue(Register src, Label* on_invalid);
215
216 // Jump to label if the value is a tagged smi.
217 void JumpIfSmi(Register src, Label* on_smi);
218
219 // Jump to label if the value is not a tagged smi.
220 void JumpIfNotSmi(Register src, Label* on_not_smi);
221
222 // Jump to label if the value is not a positive tagged smi.
223 void JumpIfNotPositiveSmi(Register src, Label* on_not_smi);
224
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000225 // Jump to label if the value, which must be a tagged smi, has value equal
ager@chromium.org4af710e2009-09-15 12:20:11 +0000226 // to the constant.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000227 void JumpIfSmiEqualsConstant(Register src, Smi* constant, Label* on_equals);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000228
ager@chromium.org4af710e2009-09-15 12:20:11 +0000229 // Jump if either or both register are not smi values.
230 void JumpIfNotBothSmi(Register src1, Register src2, Label* on_not_both_smi);
231
232 // Operations on tagged smi values.
233
234 // Smis represent a subset of integers. The subset is always equivalent to
235 // a two's complement interpretation of a fixed number of bits.
236
237 // Optimistically adds an integer constant to a supposed smi.
238 // If the src is not a smi, or the result is not a smi, jump to
239 // the label.
240 void SmiTryAddConstant(Register dst,
241 Register src,
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000242 Smi* constant,
ager@chromium.org4af710e2009-09-15 12:20:11 +0000243 Label* on_not_smi_result);
244
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000245 // Add an integer constant to a tagged smi, giving a tagged smi as result.
246 // No overflow testing on the result is done.
247 void SmiAddConstant(Register dst, Register src, Smi* constant);
248
ager@chromium.org4af710e2009-09-15 12:20:11 +0000249 // Add an integer constant to a tagged smi, giving a tagged smi as result,
250 // or jumping to a label if the result cannot be represented by a smi.
ager@chromium.org4af710e2009-09-15 12:20:11 +0000251 void SmiAddConstant(Register dst,
252 Register src,
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000253 Smi* constant,
ager@chromium.org4af710e2009-09-15 12:20:11 +0000254 Label* on_not_smi_result);
255
256 // Subtract an integer constant from a tagged smi, giving a tagged smi as
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000257 // result. No testing on the result is done.
258 void SmiSubConstant(Register dst, Register src, Smi* constant);
259
260 // Subtract an integer constant from a tagged smi, giving a tagged smi as
ager@chromium.org4af710e2009-09-15 12:20:11 +0000261 // result, or jumping to a label if the result cannot be represented by a smi.
ager@chromium.org4af710e2009-09-15 12:20:11 +0000262 void SmiSubConstant(Register dst,
263 Register src,
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000264 Smi* constant,
ager@chromium.org4af710e2009-09-15 12:20:11 +0000265 Label* on_not_smi_result);
266
267 // Negating a smi can give a negative zero or too large positive value.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000268 // NOTICE: This operation jumps on success, not failure!
ager@chromium.org4af710e2009-09-15 12:20:11 +0000269 void SmiNeg(Register dst,
270 Register src,
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000271 Label* on_smi_result);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000272
273 // Adds smi values and return the result as a smi.
274 // If dst is src1, then src1 will be destroyed, even if
275 // the operation is unsuccessful.
276 void SmiAdd(Register dst,
277 Register src1,
278 Register src2,
279 Label* on_not_smi_result);
280
281 // Subtracts smi values and return the result as a smi.
282 // If dst is src1, then src1 will be destroyed, even if
283 // the operation is unsuccessful.
284 void SmiSub(Register dst,
285 Register src1,
286 Register src2,
287 Label* on_not_smi_result);
288
289 // Multiplies smi values and return the result as a smi,
290 // if possible.
291 // If dst is src1, then src1 will be destroyed, even if
292 // the operation is unsuccessful.
293 void SmiMul(Register dst,
294 Register src1,
295 Register src2,
296 Label* on_not_smi_result);
297
298 // Divides one smi by another and returns the quotient.
299 // Clobbers rax and rdx registers.
300 void SmiDiv(Register dst,
301 Register src1,
302 Register src2,
303 Label* on_not_smi_result);
304
305 // Divides one smi by another and returns the remainder.
306 // Clobbers rax and rdx registers.
307 void SmiMod(Register dst,
308 Register src1,
309 Register src2,
310 Label* on_not_smi_result);
311
312 // Bitwise operations.
313 void SmiNot(Register dst, Register src);
314 void SmiAnd(Register dst, Register src1, Register src2);
315 void SmiOr(Register dst, Register src1, Register src2);
316 void SmiXor(Register dst, Register src1, Register src2);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000317 void SmiAndConstant(Register dst, Register src1, Smi* constant);
318 void SmiOrConstant(Register dst, Register src1, Smi* constant);
319 void SmiXorConstant(Register dst, Register src1, Smi* constant);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000320
321 void SmiShiftLeftConstant(Register dst,
322 Register src,
323 int shift_value,
324 Label* on_not_smi_result);
325 void SmiShiftLogicalRightConstant(Register dst,
326 Register src,
327 int shift_value,
328 Label* on_not_smi_result);
329 void SmiShiftArithmeticRightConstant(Register dst,
330 Register src,
331 int shift_value);
332
333 // Shifts a smi value to the left, and returns the result if that is a smi.
334 // Uses and clobbers rcx, so dst may not be rcx.
335 void SmiShiftLeft(Register dst,
336 Register src1,
337 Register src2,
338 Label* on_not_smi_result);
339 // Shifts a smi value to the right, shifting in zero bits at the top, and
340 // returns the unsigned intepretation of the result if that is a smi.
341 // Uses and clobbers rcx, so dst may not be rcx.
342 void SmiShiftLogicalRight(Register dst,
343 Register src1,
344 Register src2,
345 Label* on_not_smi_result);
346 // Shifts a smi value to the right, sign extending the top, and
347 // returns the signed intepretation of the result. That will always
348 // be a valid smi value, since it's numerically smaller than the
349 // original.
350 // Uses and clobbers rcx, so dst may not be rcx.
351 void SmiShiftArithmeticRight(Register dst,
352 Register src1,
353 Register src2);
354
355 // Specialized operations
356
357 // Select the non-smi register of two registers where exactly one is a
358 // smi. If neither are smis, jump to the failure label.
359 void SelectNonSmi(Register dst,
360 Register src1,
361 Register src2,
362 Label* on_not_smis);
363
364 // Converts, if necessary, a smi to a combination of number and
365 // multiplier to be used as a scaled index.
366 // The src register contains a *positive* smi value. The shift is the
367 // power of two to multiply the index value by (e.g.
368 // to index by smi-value * kPointerSize, pass the smi and kPointerSizeLog2).
369 // The returned index register may be either src or dst, depending
370 // on what is most efficient. If src and dst are different registers,
371 // src is always unchanged.
372 SmiIndex SmiToIndex(Register dst, Register src, int shift);
373
374 // Converts a positive smi to a negative index.
375 SmiIndex SmiToNegativeIndex(Register dst, Register src, int shift);
376
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000377 bool IsUnsafeSmi(Smi* value);
378 void LoadUnsafeSmi(Register dst, Smi* source);
379
380 // Basic Smi operations.
381 void Move(Register dst, Smi* source);
382 void Move(const Operand& dst, Smi* source);
383 void Push(Smi* smi);
384 void Test(const Operand& dst, Smi* source);
385
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000386 // ---------------------------------------------------------------------------
387 // Macro instructions
388
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000389 // Load a register with a long value as efficiently as possible.
ager@chromium.orge2902be2009-06-08 12:21:35 +0000390 void Set(Register dst, int64_t x);
391 void Set(const Operand& dst, int64_t x);
ager@chromium.org9085a012009-05-11 19:22:57 +0000392
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000393 // Handle support
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000394 bool IsUnsafeSmi(Handle<Object> value) {
395 return IsUnsafeSmi(Smi::cast(*value));
396 }
397
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000398 void LoadUnsafeSmi(Register dst, Handle<Object> source) {
399 LoadUnsafeSmi(dst, Smi::cast(*source));
400 }
401
402 void Move(Register dst, Handle<Object> source);
403 void Move(const Operand& dst, Handle<Object> source);
404 void Cmp(Register dst, Handle<Object> source);
ager@chromium.org3e875802009-06-29 08:26:34 +0000405 void Cmp(const Operand& dst, Handle<Object> source);
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000406 void Push(Handle<Object> source);
407
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000408 // Control Flow
409 void Jump(Address destination, RelocInfo::Mode rmode);
410 void Jump(ExternalReference ext);
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000411 void Jump(Handle<Code> code_object, RelocInfo::Mode rmode);
412
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000413 void Call(Address destination, RelocInfo::Mode rmode);
414 void Call(ExternalReference ext);
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000415 void Call(Handle<Code> code_object, RelocInfo::Mode rmode);
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000416
ager@chromium.org9085a012009-05-11 19:22:57 +0000417 // Compare object type for heap object.
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000418 // Always use unsigned comparisons: above and below, not less and greater.
ager@chromium.org9085a012009-05-11 19:22:57 +0000419 // Incoming register is heap_object and outgoing register is map.
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000420 // They may be the same register, and may be kScratchRegister.
ager@chromium.org9085a012009-05-11 19:22:57 +0000421 void CmpObjectType(Register heap_object, InstanceType type, Register map);
422
423 // Compare instance type for map.
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000424 // Always use unsigned comparisons: above and below, not less and greater.
ager@chromium.org9085a012009-05-11 19:22:57 +0000425 void CmpInstanceType(Register map, InstanceType type);
426
427 // FCmp is similar to integer cmp, but requires unsigned
428 // jcc instructions (je, ja, jae, jb, jbe, je, and jz).
429 void FCmp();
430
431 // ---------------------------------------------------------------------------
432 // Exception handling
433
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000434 // Push a new try handler and link into try handler chain. The return
435 // address must be pushed before calling this helper.
ager@chromium.org9085a012009-05-11 19:22:57 +0000436 void PushTryHandler(CodeLocation try_location, HandlerType type);
437
438
439 // ---------------------------------------------------------------------------
440 // Inline caching support
441
442 // Generates code that verifies that the maps of objects in the
443 // prototype chain of object hasn't changed since the code was
444 // generated and branches to the miss label if any map has. If
445 // necessary the function also generates code for security check
446 // in case of global object holders. The scratch and holder
447 // registers are always clobbered, but the object register is only
448 // clobbered if it the same as the holder register. The function
449 // returns a register containing the holder - either object_reg or
450 // holder_reg.
451 Register CheckMaps(JSObject* object, Register object_reg,
452 JSObject* holder, Register holder_reg,
453 Register scratch, Label* miss);
454
455 // Generate code for checking access rights - used for security checks
456 // on access to global objects across environments. The holder register
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000457 // is left untouched, but the scratch register and kScratchRegister,
458 // which must be different, are clobbered.
ager@chromium.org9085a012009-05-11 19:22:57 +0000459 void CheckAccessGlobalProxy(Register holder_reg,
460 Register scratch,
461 Label* miss);
462
463
464 // ---------------------------------------------------------------------------
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000465 // Allocation support
466
467 // Allocate an object in new space. If the new space is exhausted control
468 // continues at the gc_required label. The allocated object is returned in
469 // result and end of the new object is returned in result_end. The register
470 // scratch can be passed as no_reg in which case an additional object
471 // reference will be added to the reloc info. The returned pointers in result
472 // and result_end have not yet been tagged as heap objects. If
473 // result_contains_top_on_entry is true the content of result is known to be
474 // the allocation top on entry (could be result_end from a previous call to
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000475 // AllocateInNewSpace). If result_contains_top_on_entry is true scratch
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000476 // should be no_reg as it is never used.
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000477 void AllocateInNewSpace(int object_size,
478 Register result,
479 Register result_end,
480 Register scratch,
481 Label* gc_required,
482 AllocationFlags flags);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000483
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000484 void AllocateInNewSpace(int header_size,
485 ScaleFactor element_size,
486 Register element_count,
487 Register result,
488 Register result_end,
489 Register scratch,
490 Label* gc_required,
491 AllocationFlags flags);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000492
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000493 void AllocateInNewSpace(Register object_size,
494 Register result,
495 Register result_end,
496 Register scratch,
497 Label* gc_required,
498 AllocationFlags flags);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000499
500 // Undo allocation in new space. The object passed and objects allocated after
501 // it will no longer be allocated. Make sure that no pointers are left to the
502 // object(s) no longer allocated as they would be invalid when allocation is
503 // un-done.
504 void UndoAllocationInNewSpace(Register object);
505
506 // ---------------------------------------------------------------------------
ager@chromium.org9085a012009-05-11 19:22:57 +0000507 // Support functions.
508
509 // Check if result is zero and op is negative.
510 void NegativeZeroTest(Register result, Register op, Label* then_label);
511
512 // Check if result is zero and op is negative in code using jump targets.
513 void NegativeZeroTest(CodeGenerator* cgen,
514 Register result,
515 Register op,
516 JumpTarget* then_target);
517
518 // Check if result is zero and any of op1 and op2 are negative.
519 // Register scratch is destroyed, and it must be different from op2.
520 void NegativeZeroTest(Register result, Register op1, Register op2,
521 Register scratch, Label* then_label);
522
523 // Try to get function prototype of a function and puts the value in
524 // the result register. Checks that the function really is a
525 // function and jumps to the miss label if the fast checks fail. The
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000526 // function register will be untouched; the other register may be
ager@chromium.org9085a012009-05-11 19:22:57 +0000527 // clobbered.
528 void TryGetFunctionPrototype(Register function,
529 Register result,
ager@chromium.org9085a012009-05-11 19:22:57 +0000530 Label* miss);
531
532 // Generates code for reporting that an illegal operation has
533 // occurred.
534 void IllegalOperation(int num_arguments);
535
536 // ---------------------------------------------------------------------------
537 // Runtime calls
538
539 // Call a code stub.
540 void CallStub(CodeStub* stub);
541
542 // Return from a code stub after popping its arguments.
543 void StubReturn(int argc);
544
545 // Call a runtime routine.
546 // Eventually this should be used for all C calls.
547 void CallRuntime(Runtime::Function* f, int num_arguments);
548
549 // Convenience function: Same as above, but takes the fid instead.
550 void CallRuntime(Runtime::FunctionId id, int num_arguments);
551
552 // Tail call of a runtime routine (jump).
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000553 // Like JumpToRuntime, but also takes care of passing the number
ager@chromium.org9085a012009-05-11 19:22:57 +0000554 // of arguments.
ager@chromium.orga1645e22009-09-09 19:27:10 +0000555 void TailCallRuntime(const ExternalReference& ext,
556 int num_arguments,
557 int result_size);
ager@chromium.org9085a012009-05-11 19:22:57 +0000558
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000559 // Jump to a runtime routine.
560 void JumpToRuntime(const ExternalReference& ext, int result_size);
ager@chromium.org9085a012009-05-11 19:22:57 +0000561
562
563 // ---------------------------------------------------------------------------
564 // Utilities
565
566 void Ret();
567
568 struct Unresolved {
569 int pc;
570 uint32_t flags; // see Bootstrapper::FixupFlags decoders/encoders.
571 const char* name;
572 };
573 List<Unresolved>* unresolved() { return &unresolved_; }
574
575 Handle<Object> CodeObject() { return code_object_; }
576
577
578 // ---------------------------------------------------------------------------
579 // StatsCounter support
580
581 void SetCounter(StatsCounter* counter, int value);
582 void IncrementCounter(StatsCounter* counter, int value);
583 void DecrementCounter(StatsCounter* counter, int value);
584
585
586 // ---------------------------------------------------------------------------
587 // Debugging
588
589 // Calls Abort(msg) if the condition cc is not satisfied.
590 // Use --debug_code to enable.
591 void Assert(Condition cc, const char* msg);
592
593 // Like Assert(), but always enabled.
594 void Check(Condition cc, const char* msg);
595
596 // Print a message to stdout and abort execution.
597 void Abort(const char* msg);
598
599 // Verify restrictions about code generated in stubs.
600 void set_generating_stub(bool value) { generating_stub_ = value; }
601 bool generating_stub() { return generating_stub_; }
602 void set_allow_stub_calls(bool value) { allow_stub_calls_ = value; }
603 bool allow_stub_calls() { return allow_stub_calls_; }
604
605 private:
606 List<Unresolved> unresolved_;
607 bool generating_stub_;
608 bool allow_stub_calls_;
609 Handle<Object> code_object_; // This handle will be patched with the code
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000610 // object on installation.
ager@chromium.org9085a012009-05-11 19:22:57 +0000611
612 // Helper functions for generating invokes.
613 void InvokePrologue(const ParameterCount& expected,
614 const ParameterCount& actual,
615 Handle<Code> code_constant,
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000616 Register code_register,
ager@chromium.org9085a012009-05-11 19:22:57 +0000617 Label* done,
618 InvokeFlag flag);
619
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000620 // Prepares for a call or jump to a builtin by doing two things:
621 // 1. Emits code that fetches the builtin's function object from the context
622 // at runtime, and puts it in the register rdi.
623 // 2. Fetches the builtin's code object, and returns it in a handle, at
624 // compile time, so that later code can emit instructions to jump or call
625 // the builtin directly. If the code object has not yet been created, it
626 // returns the builtin code object for IllegalFunction, and sets the
627 // output parameter "resolved" to false. Code that uses the return value
628 // should then add the address and the builtin name to the list of fixups
629 // called unresolved_, which is fixed up by the bootstrapper.
ager@chromium.org9085a012009-05-11 19:22:57 +0000630 Handle<Code> ResolveBuiltin(Builtins::JavaScript id, bool* resolved);
631
632 // Activation support.
633 void EnterFrame(StackFrame::Type type);
634 void LeaveFrame(StackFrame::Type type);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000635
636 // Allocation support helpers.
637 void LoadAllocationTopHelper(Register result,
638 Register result_end,
639 Register scratch,
ager@chromium.orga1645e22009-09-09 19:27:10 +0000640 AllocationFlags flags);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000641 void UpdateAllocationTopHelper(Register result_end, Register scratch);
ager@chromium.org9085a012009-05-11 19:22:57 +0000642};
643
644
ager@chromium.org4af710e2009-09-15 12:20:11 +0000645// The code patcher is used to patch (typically) small parts of code e.g. for
646// debugging and other types of instrumentation. When using the code patcher
647// the exact number of bytes specified must be emitted. Is not legal to emit
648// relocation information. If any of these constraints are violated it causes
649// an assertion.
650class CodePatcher {
651 public:
652 CodePatcher(byte* address, int size);
653 virtual ~CodePatcher();
654
655 // Macro assembler to emit code.
656 MacroAssembler* masm() { return &masm_; }
657
658 private:
659 byte* address_; // The address of the code being patched.
660 int size_; // Number of bytes of the expected patch size.
661 MacroAssembler masm_; // Macro assembler used to generate the code.
662};
663
664
ager@chromium.org9085a012009-05-11 19:22:57 +0000665// -----------------------------------------------------------------------------
666// Static helper functions.
667
668// Generate an Operand for loading a field from an object.
669static inline Operand FieldOperand(Register object, int offset) {
670 return Operand(object, offset - kHeapObjectTag);
671}
672
673
674// Generate an Operand for loading an indexed field from an object.
675static inline Operand FieldOperand(Register object,
676 Register index,
677 ScaleFactor scale,
678 int offset) {
679 return Operand(object, index, scale, offset - kHeapObjectTag);
680}
681
682
683#ifdef GENERATED_CODE_COVERAGE
684extern void LogGeneratedCodeCoverage(const char* file_line);
685#define CODE_COVERAGE_STRINGIFY(x) #x
686#define CODE_COVERAGE_TOSTRING(x) CODE_COVERAGE_STRINGIFY(x)
687#define __FILE_LINE__ __FILE__ ":" CODE_COVERAGE_TOSTRING(__LINE__)
688#define ACCESS_MASM(masm) { \
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000689 byte* x64_coverage_function = \
ager@chromium.org9085a012009-05-11 19:22:57 +0000690 reinterpret_cast<byte*>(FUNCTION_ADDR(LogGeneratedCodeCoverage)); \
691 masm->pushfd(); \
692 masm->pushad(); \
693 masm->push(Immediate(reinterpret_cast<int>(&__FILE_LINE__))); \
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000694 masm->call(x64_coverage_function, RelocInfo::RUNTIME_ENTRY); \
ager@chromium.org9085a012009-05-11 19:22:57 +0000695 masm->pop(rax); \
696 masm->popad(); \
697 masm->popfd(); \
698 } \
699 masm->
700#else
701#define ACCESS_MASM(masm) masm->
702#endif
703
704
705} } // namespace v8::internal
706
707#endif // V8_X64_MACRO_ASSEMBLER_X64_H_