blob: 9da2676a99326710376e3c165222a38ea332cf6a [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
44
ager@chromium.org9085a012009-05-11 19:22:57 +000045// MacroAssembler implements a collection of frequently used macros.
46class MacroAssembler: public Assembler {
47 public:
48 MacroAssembler(void* buffer, int size);
49
ager@chromium.org18ad94b2009-09-02 08:22:29 +000050 void LoadRoot(Register destination, Heap::RootListIndex index);
51 void CompareRoot(Register with, Heap::RootListIndex index);
52 void PushRoot(Heap::RootListIndex index);
53
ager@chromium.org9085a012009-05-11 19:22:57 +000054 // ---------------------------------------------------------------------------
55 // GC Support
56
57 // Set the remembered set bit for [object+offset].
58 // object is the object being stored into, value is the object being stored.
59 // If offset is zero, then the scratch register contains the array index into
60 // the elements array represented as a Smi.
61 // All registers are clobbered by the operation.
62 void RecordWrite(Register object,
63 int offset,
64 Register value,
65 Register scratch);
66
67#ifdef ENABLE_DEBUGGER_SUPPORT
68 // ---------------------------------------------------------------------------
69 // Debugger Support
70
71 void SaveRegistersToMemory(RegList regs);
72 void RestoreRegistersFromMemory(RegList regs);
73 void PushRegistersFromMemory(RegList regs);
74 void PopRegistersToMemory(RegList regs);
75 void CopyRegistersFromStackToMemory(Register base,
76 Register scratch,
77 RegList regs);
78#endif
79
80 // ---------------------------------------------------------------------------
81 // Activation frames
82
83 void EnterInternalFrame() { EnterFrame(StackFrame::INTERNAL); }
84 void LeaveInternalFrame() { LeaveFrame(StackFrame::INTERNAL); }
85
86 void EnterConstructFrame() { EnterFrame(StackFrame::CONSTRUCT); }
87 void LeaveConstructFrame() { LeaveFrame(StackFrame::CONSTRUCT); }
88
89 // Enter specific kind of exit frame; either EXIT or
ager@chromium.orga1645e22009-09-09 19:27:10 +000090 // EXIT_DEBUG. Expects the number of arguments in register rax and
91 // sets up the number of arguments in register rdi and the pointer
92 // to the first argument in register rsi.
93 void EnterExitFrame(StackFrame::Type type, int result_size = 1);
ager@chromium.org9085a012009-05-11 19:22:57 +000094
ager@chromium.orga1645e22009-09-09 19:27:10 +000095 // Leave the current exit frame. Expects/provides the return value in
96 // register rax:rdx (untouched) and the pointer to the first
97 // argument in register rsi.
98 void LeaveExitFrame(StackFrame::Type type, int result_size = 1);
ager@chromium.org9085a012009-05-11 19:22:57 +000099
100
101 // ---------------------------------------------------------------------------
102 // JavaScript invokes
103
104 // Invoke the JavaScript function code by either calling or jumping.
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000105 void InvokeCode(Register code,
ager@chromium.org9085a012009-05-11 19:22:57 +0000106 const ParameterCount& expected,
107 const ParameterCount& actual,
108 InvokeFlag flag);
109
110 void InvokeCode(Handle<Code> code,
111 const ParameterCount& expected,
112 const ParameterCount& actual,
113 RelocInfo::Mode rmode,
114 InvokeFlag flag);
115
116 // Invoke the JavaScript function in the given register. Changes the
117 // current context to the context in the function before invoking.
118 void InvokeFunction(Register function,
119 const ParameterCount& actual,
120 InvokeFlag flag);
121
122 // Invoke specified builtin JavaScript function. Adds an entry to
123 // the unresolved list if the name does not resolve.
124 void InvokeBuiltin(Builtins::JavaScript id, InvokeFlag flag);
125
126 // Store the code object for the given builtin in the target register.
127 void GetBuiltinEntry(Register target, Builtins::JavaScript id);
128
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000129 // ---------------------------------------------------------------------------
130 // Macro instructions
131
ager@chromium.org9085a012009-05-11 19:22:57 +0000132 // Expression support
ager@chromium.orge2902be2009-06-08 12:21:35 +0000133 void Set(Register dst, int64_t x);
134 void Set(const Operand& dst, int64_t x);
ager@chromium.org9085a012009-05-11 19:22:57 +0000135
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000136 // Handle support
137 bool IsUnsafeSmi(Smi* value);
138 bool IsUnsafeSmi(Handle<Object> value) {
139 return IsUnsafeSmi(Smi::cast(*value));
140 }
141
142 void LoadUnsafeSmi(Register dst, Smi* source);
143 void LoadUnsafeSmi(Register dst, Handle<Object> source) {
144 LoadUnsafeSmi(dst, Smi::cast(*source));
145 }
146
147 void Move(Register dst, Handle<Object> source);
148 void Move(const Operand& dst, Handle<Object> source);
149 void Cmp(Register dst, Handle<Object> source);
ager@chromium.org3e875802009-06-29 08:26:34 +0000150 void Cmp(const Operand& dst, Handle<Object> source);
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000151 void Push(Handle<Object> source);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000152 void Push(Smi* smi);
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000153
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000154 // Control Flow
155 void Jump(Address destination, RelocInfo::Mode rmode);
156 void Jump(ExternalReference ext);
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000157 void Jump(Handle<Code> code_object, RelocInfo::Mode rmode);
158
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000159 void Call(Address destination, RelocInfo::Mode rmode);
160 void Call(ExternalReference ext);
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000161 void Call(Handle<Code> code_object, RelocInfo::Mode rmode);
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000162
ager@chromium.org9085a012009-05-11 19:22:57 +0000163 // Compare object type for heap object.
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000164 // Always use unsigned comparisons: above and below, not less and greater.
ager@chromium.org9085a012009-05-11 19:22:57 +0000165 // Incoming register is heap_object and outgoing register is map.
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000166 // They may be the same register, and may be kScratchRegister.
ager@chromium.org9085a012009-05-11 19:22:57 +0000167 void CmpObjectType(Register heap_object, InstanceType type, Register map);
168
169 // Compare instance type for map.
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000170 // Always use unsigned comparisons: above and below, not less and greater.
ager@chromium.org9085a012009-05-11 19:22:57 +0000171 void CmpInstanceType(Register map, InstanceType type);
172
173 // FCmp is similar to integer cmp, but requires unsigned
174 // jcc instructions (je, ja, jae, jb, jbe, je, and jz).
175 void FCmp();
176
177 // ---------------------------------------------------------------------------
178 // Exception handling
179
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000180 // Push a new try handler and link into try handler chain. The return
181 // address must be pushed before calling this helper.
ager@chromium.org9085a012009-05-11 19:22:57 +0000182 void PushTryHandler(CodeLocation try_location, HandlerType type);
183
184
185 // ---------------------------------------------------------------------------
186 // Inline caching support
187
188 // Generates code that verifies that the maps of objects in the
189 // prototype chain of object hasn't changed since the code was
190 // generated and branches to the miss label if any map has. If
191 // necessary the function also generates code for security check
192 // in case of global object holders. The scratch and holder
193 // registers are always clobbered, but the object register is only
194 // clobbered if it the same as the holder register. The function
195 // returns a register containing the holder - either object_reg or
196 // holder_reg.
197 Register CheckMaps(JSObject* object, Register object_reg,
198 JSObject* holder, Register holder_reg,
199 Register scratch, Label* miss);
200
201 // Generate code for checking access rights - used for security checks
202 // on access to global objects across environments. The holder register
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000203 // is left untouched, but the scratch register and kScratchRegister,
204 // which must be different, are clobbered.
ager@chromium.org9085a012009-05-11 19:22:57 +0000205 void CheckAccessGlobalProxy(Register holder_reg,
206 Register scratch,
207 Label* miss);
208
209
210 // ---------------------------------------------------------------------------
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000211 // Allocation support
212
213 // Allocate an object in new space. If the new space is exhausted control
214 // continues at the gc_required label. The allocated object is returned in
215 // result and end of the new object is returned in result_end. The register
216 // scratch can be passed as no_reg in which case an additional object
217 // reference will be added to the reloc info. The returned pointers in result
218 // and result_end have not yet been tagged as heap objects. If
219 // result_contains_top_on_entry is true the content of result is known to be
220 // the allocation top on entry (could be result_end from a previous call to
221 // AllocateObjectInNewSpace). If result_contains_top_on_entry is true scratch
222 // should be no_reg as it is never used.
223 void AllocateObjectInNewSpace(int object_size,
224 Register result,
225 Register result_end,
226 Register scratch,
227 Label* gc_required,
ager@chromium.orga1645e22009-09-09 19:27:10 +0000228 AllocationFlags flags);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000229
230 void AllocateObjectInNewSpace(int header_size,
231 ScaleFactor element_size,
232 Register element_count,
233 Register result,
234 Register result_end,
235 Register scratch,
236 Label* gc_required,
ager@chromium.orga1645e22009-09-09 19:27:10 +0000237 AllocationFlags flags);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000238
239 void AllocateObjectInNewSpace(Register object_size,
240 Register result,
241 Register result_end,
242 Register scratch,
243 Label* gc_required,
ager@chromium.orga1645e22009-09-09 19:27:10 +0000244 AllocationFlags flags);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000245
246 // Undo allocation in new space. The object passed and objects allocated after
247 // it will no longer be allocated. Make sure that no pointers are left to the
248 // object(s) no longer allocated as they would be invalid when allocation is
249 // un-done.
250 void UndoAllocationInNewSpace(Register object);
251
252 // ---------------------------------------------------------------------------
ager@chromium.org9085a012009-05-11 19:22:57 +0000253 // Support functions.
254
255 // Check if result is zero and op is negative.
256 void NegativeZeroTest(Register result, Register op, Label* then_label);
257
258 // Check if result is zero and op is negative in code using jump targets.
259 void NegativeZeroTest(CodeGenerator* cgen,
260 Register result,
261 Register op,
262 JumpTarget* then_target);
263
264 // Check if result is zero and any of op1 and op2 are negative.
265 // Register scratch is destroyed, and it must be different from op2.
266 void NegativeZeroTest(Register result, Register op1, Register op2,
267 Register scratch, Label* then_label);
268
269 // Try to get function prototype of a function and puts the value in
270 // the result register. Checks that the function really is a
271 // function and jumps to the miss label if the fast checks fail. The
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000272 // function register will be untouched; the other register may be
ager@chromium.org9085a012009-05-11 19:22:57 +0000273 // clobbered.
274 void TryGetFunctionPrototype(Register function,
275 Register result,
ager@chromium.org9085a012009-05-11 19:22:57 +0000276 Label* miss);
277
278 // Generates code for reporting that an illegal operation has
279 // occurred.
280 void IllegalOperation(int num_arguments);
281
282 // ---------------------------------------------------------------------------
283 // Runtime calls
284
285 // Call a code stub.
286 void CallStub(CodeStub* stub);
287
288 // Return from a code stub after popping its arguments.
289 void StubReturn(int argc);
290
291 // Call a runtime routine.
292 // Eventually this should be used for all C calls.
293 void CallRuntime(Runtime::Function* f, int num_arguments);
294
295 // Convenience function: Same as above, but takes the fid instead.
296 void CallRuntime(Runtime::FunctionId id, int num_arguments);
297
298 // Tail call of a runtime routine (jump).
299 // Like JumpToBuiltin, but also takes care of passing the number
300 // of arguments.
ager@chromium.orga1645e22009-09-09 19:27:10 +0000301 void TailCallRuntime(const ExternalReference& ext,
302 int num_arguments,
303 int result_size);
ager@chromium.org9085a012009-05-11 19:22:57 +0000304
305 // Jump to the builtin routine.
ager@chromium.orga1645e22009-09-09 19:27:10 +0000306 void JumpToBuiltin(const ExternalReference& ext, int result_size);
ager@chromium.org9085a012009-05-11 19:22:57 +0000307
308
309 // ---------------------------------------------------------------------------
310 // Utilities
311
312 void Ret();
313
314 struct Unresolved {
315 int pc;
316 uint32_t flags; // see Bootstrapper::FixupFlags decoders/encoders.
317 const char* name;
318 };
319 List<Unresolved>* unresolved() { return &unresolved_; }
320
321 Handle<Object> CodeObject() { return code_object_; }
322
323
324 // ---------------------------------------------------------------------------
325 // StatsCounter support
326
327 void SetCounter(StatsCounter* counter, int value);
328 void IncrementCounter(StatsCounter* counter, int value);
329 void DecrementCounter(StatsCounter* counter, int value);
330
331
332 // ---------------------------------------------------------------------------
333 // Debugging
334
335 // Calls Abort(msg) if the condition cc is not satisfied.
336 // Use --debug_code to enable.
337 void Assert(Condition cc, const char* msg);
338
339 // Like Assert(), but always enabled.
340 void Check(Condition cc, const char* msg);
341
342 // Print a message to stdout and abort execution.
343 void Abort(const char* msg);
344
345 // Verify restrictions about code generated in stubs.
346 void set_generating_stub(bool value) { generating_stub_ = value; }
347 bool generating_stub() { return generating_stub_; }
348 void set_allow_stub_calls(bool value) { allow_stub_calls_ = value; }
349 bool allow_stub_calls() { return allow_stub_calls_; }
350
351 private:
352 List<Unresolved> unresolved_;
353 bool generating_stub_;
354 bool allow_stub_calls_;
355 Handle<Object> code_object_; // This handle will be patched with the code
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000356 // object on installation.
ager@chromium.org9085a012009-05-11 19:22:57 +0000357
358 // Helper functions for generating invokes.
359 void InvokePrologue(const ParameterCount& expected,
360 const ParameterCount& actual,
361 Handle<Code> code_constant,
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000362 Register code_register,
ager@chromium.org9085a012009-05-11 19:22:57 +0000363 Label* done,
364 InvokeFlag flag);
365
366 // Get the code for the given builtin. Returns if able to resolve
367 // the function in the 'resolved' flag.
368 Handle<Code> ResolveBuiltin(Builtins::JavaScript id, bool* resolved);
369
370 // Activation support.
371 void EnterFrame(StackFrame::Type type);
372 void LeaveFrame(StackFrame::Type type);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000373
374 // Allocation support helpers.
375 void LoadAllocationTopHelper(Register result,
376 Register result_end,
377 Register scratch,
ager@chromium.orga1645e22009-09-09 19:27:10 +0000378 AllocationFlags flags);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000379 void UpdateAllocationTopHelper(Register result_end, Register scratch);
ager@chromium.org9085a012009-05-11 19:22:57 +0000380};
381
382
ager@chromium.org9085a012009-05-11 19:22:57 +0000383// -----------------------------------------------------------------------------
384// Static helper functions.
385
386// Generate an Operand for loading a field from an object.
387static inline Operand FieldOperand(Register object, int offset) {
388 return Operand(object, offset - kHeapObjectTag);
389}
390
391
392// Generate an Operand for loading an indexed field from an object.
393static inline Operand FieldOperand(Register object,
394 Register index,
395 ScaleFactor scale,
396 int offset) {
397 return Operand(object, index, scale, offset - kHeapObjectTag);
398}
399
400
401#ifdef GENERATED_CODE_COVERAGE
402extern void LogGeneratedCodeCoverage(const char* file_line);
403#define CODE_COVERAGE_STRINGIFY(x) #x
404#define CODE_COVERAGE_TOSTRING(x) CODE_COVERAGE_STRINGIFY(x)
405#define __FILE_LINE__ __FILE__ ":" CODE_COVERAGE_TOSTRING(__LINE__)
406#define ACCESS_MASM(masm) { \
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000407 byte* x64_coverage_function = \
ager@chromium.org9085a012009-05-11 19:22:57 +0000408 reinterpret_cast<byte*>(FUNCTION_ADDR(LogGeneratedCodeCoverage)); \
409 masm->pushfd(); \
410 masm->pushad(); \
411 masm->push(Immediate(reinterpret_cast<int>(&__FILE_LINE__))); \
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000412 masm->call(x64_coverage_function, RelocInfo::RUNTIME_ENTRY); \
ager@chromium.org9085a012009-05-11 19:22:57 +0000413 masm->pop(rax); \
414 masm->popad(); \
415 masm->popfd(); \
416 } \
417 masm->
418#else
419#define ACCESS_MASM(masm) masm->
420#endif
421
422
423} } // namespace v8::internal
424
425#endif // V8_X64_MACRO_ASSEMBLER_X64_H_