blob: 6f5c32c2b4dff209c6786353e2e328161cd2f35c [file] [log] [blame]
ager@chromium.org9085a012009-05-11 19:22:57 +00001// Copyright 2006-2008 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
45// Helper types to make flags easier to read at call sites.
46enum InvokeFlag {
47 CALL_FUNCTION,
48 JUMP_FUNCTION
49};
50
51enum CodeLocation {
52 IN_JAVASCRIPT,
53 IN_JS_ENTRY,
54 IN_C_ENTRY
55};
56
57enum HandlerType {
58 TRY_CATCH_HANDLER,
59 TRY_FINALLY_HANDLER,
60 JS_ENTRY_HANDLER
61};
62
63
64// MacroAssembler implements a collection of frequently used macros.
65class MacroAssembler: public Assembler {
66 public:
67 MacroAssembler(void* buffer, int size);
68
ager@chromium.org18ad94b2009-09-02 08:22:29 +000069 void LoadRoot(Register destination, Heap::RootListIndex index);
70 void CompareRoot(Register with, Heap::RootListIndex index);
71 void PushRoot(Heap::RootListIndex index);
72
ager@chromium.org9085a012009-05-11 19:22:57 +000073 // ---------------------------------------------------------------------------
74 // GC Support
75
76 // Set the remembered set bit for [object+offset].
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 RecordWrite(Register object,
82 int offset,
83 Register value,
84 Register scratch);
85
86#ifdef ENABLE_DEBUGGER_SUPPORT
87 // ---------------------------------------------------------------------------
88 // Debugger Support
89
90 void SaveRegistersToMemory(RegList regs);
91 void RestoreRegistersFromMemory(RegList regs);
92 void PushRegistersFromMemory(RegList regs);
93 void PopRegistersToMemory(RegList regs);
94 void CopyRegistersFromStackToMemory(Register base,
95 Register scratch,
96 RegList regs);
97#endif
98
99 // ---------------------------------------------------------------------------
100 // Activation frames
101
102 void EnterInternalFrame() { EnterFrame(StackFrame::INTERNAL); }
103 void LeaveInternalFrame() { LeaveFrame(StackFrame::INTERNAL); }
104
105 void EnterConstructFrame() { EnterFrame(StackFrame::CONSTRUCT); }
106 void LeaveConstructFrame() { LeaveFrame(StackFrame::CONSTRUCT); }
107
108 // Enter specific kind of exit frame; either EXIT or
109 // EXIT_DEBUG. Expects the number of arguments in register eax and
110 // sets up the number of arguments in register edi and the pointer
111 // to the first argument in register esi.
112 void EnterExitFrame(StackFrame::Type type);
113
114 // Leave the current exit frame. Expects the return value in
115 // register eax:edx (untouched) and the pointer to the first
116 // argument in register esi.
117 void LeaveExitFrame(StackFrame::Type type);
118
119
120 // ---------------------------------------------------------------------------
121 // JavaScript invokes
122
123 // Invoke the JavaScript function code by either calling or jumping.
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000124 void InvokeCode(Register code,
ager@chromium.org9085a012009-05-11 19:22:57 +0000125 const ParameterCount& expected,
126 const ParameterCount& actual,
127 InvokeFlag flag);
128
129 void InvokeCode(Handle<Code> code,
130 const ParameterCount& expected,
131 const ParameterCount& actual,
132 RelocInfo::Mode rmode,
133 InvokeFlag flag);
134
135 // Invoke the JavaScript function in the given register. Changes the
136 // current context to the context in the function before invoking.
137 void InvokeFunction(Register function,
138 const ParameterCount& actual,
139 InvokeFlag flag);
140
141 // Invoke specified builtin JavaScript function. Adds an entry to
142 // the unresolved list if the name does not resolve.
143 void InvokeBuiltin(Builtins::JavaScript id, InvokeFlag flag);
144
145 // Store the code object for the given builtin in the target register.
146 void GetBuiltinEntry(Register target, Builtins::JavaScript id);
147
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000148 // ---------------------------------------------------------------------------
149 // Macro instructions
150
ager@chromium.org9085a012009-05-11 19:22:57 +0000151 // Expression support
ager@chromium.orge2902be2009-06-08 12:21:35 +0000152 void Set(Register dst, int64_t x);
153 void Set(const Operand& dst, int64_t x);
ager@chromium.org9085a012009-05-11 19:22:57 +0000154
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000155 // Handle support
156 bool IsUnsafeSmi(Smi* value);
157 bool IsUnsafeSmi(Handle<Object> value) {
158 return IsUnsafeSmi(Smi::cast(*value));
159 }
160
161 void LoadUnsafeSmi(Register dst, Smi* source);
162 void LoadUnsafeSmi(Register dst, Handle<Object> source) {
163 LoadUnsafeSmi(dst, Smi::cast(*source));
164 }
165
166 void Move(Register dst, Handle<Object> source);
167 void Move(const Operand& dst, Handle<Object> source);
168 void Cmp(Register dst, Handle<Object> source);
ager@chromium.org3e875802009-06-29 08:26:34 +0000169 void Cmp(const Operand& dst, Handle<Object> source);
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000170 void Push(Handle<Object> source);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000171 void Push(Smi* smi);
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000172
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000173 // Control Flow
174 void Jump(Address destination, RelocInfo::Mode rmode);
175 void Jump(ExternalReference ext);
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000176 void Jump(Handle<Code> code_object, RelocInfo::Mode rmode);
177
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000178 void Call(Address destination, RelocInfo::Mode rmode);
179 void Call(ExternalReference ext);
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000180 void Call(Handle<Code> code_object, RelocInfo::Mode rmode);
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000181
ager@chromium.org9085a012009-05-11 19:22:57 +0000182 // Compare object type for heap object.
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000183 // Always use unsigned comparisons: above and below, not less and greater.
ager@chromium.org9085a012009-05-11 19:22:57 +0000184 // Incoming register is heap_object and outgoing register is map.
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000185 // They may be the same register, and may be kScratchRegister.
ager@chromium.org9085a012009-05-11 19:22:57 +0000186 void CmpObjectType(Register heap_object, InstanceType type, Register map);
187
188 // Compare instance type for map.
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000189 // Always use unsigned comparisons: above and below, not less and greater.
ager@chromium.org9085a012009-05-11 19:22:57 +0000190 void CmpInstanceType(Register map, InstanceType type);
191
192 // FCmp is similar to integer cmp, but requires unsigned
193 // jcc instructions (je, ja, jae, jb, jbe, je, and jz).
194 void FCmp();
195
196 // ---------------------------------------------------------------------------
197 // Exception handling
198
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000199 // Push a new try handler and link into try handler chain. The return
200 // address must be pushed before calling this helper.
ager@chromium.org9085a012009-05-11 19:22:57 +0000201 void PushTryHandler(CodeLocation try_location, HandlerType type);
202
203
204 // ---------------------------------------------------------------------------
205 // Inline caching support
206
207 // Generates code that verifies that the maps of objects in the
208 // prototype chain of object hasn't changed since the code was
209 // generated and branches to the miss label if any map has. If
210 // necessary the function also generates code for security check
211 // in case of global object holders. The scratch and holder
212 // registers are always clobbered, but the object register is only
213 // clobbered if it the same as the holder register. The function
214 // returns a register containing the holder - either object_reg or
215 // holder_reg.
216 Register CheckMaps(JSObject* object, Register object_reg,
217 JSObject* holder, Register holder_reg,
218 Register scratch, Label* miss);
219
220 // Generate code for checking access rights - used for security checks
221 // on access to global objects across environments. The holder register
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000222 // is left untouched, but the scratch register and kScratchRegister,
223 // which must be different, are clobbered.
ager@chromium.org9085a012009-05-11 19:22:57 +0000224 void CheckAccessGlobalProxy(Register holder_reg,
225 Register scratch,
226 Label* miss);
227
228
229 // ---------------------------------------------------------------------------
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000230 // Allocation support
231
232 // Allocate an object in new space. If the new space is exhausted control
233 // continues at the gc_required label. The allocated object is returned in
234 // result and end of the new object is returned in result_end. The register
235 // scratch can be passed as no_reg in which case an additional object
236 // reference will be added to the reloc info. The returned pointers in result
237 // and result_end have not yet been tagged as heap objects. If
238 // result_contains_top_on_entry is true the content of result is known to be
239 // the allocation top on entry (could be result_end from a previous call to
240 // AllocateObjectInNewSpace). If result_contains_top_on_entry is true scratch
241 // should be no_reg as it is never used.
242 void AllocateObjectInNewSpace(int object_size,
243 Register result,
244 Register result_end,
245 Register scratch,
246 Label* gc_required,
247 bool result_contains_top_on_entry);
248
249 void AllocateObjectInNewSpace(int header_size,
250 ScaleFactor element_size,
251 Register element_count,
252 Register result,
253 Register result_end,
254 Register scratch,
255 Label* gc_required,
256 bool result_contains_top_on_entry);
257
258 void AllocateObjectInNewSpace(Register object_size,
259 Register result,
260 Register result_end,
261 Register scratch,
262 Label* gc_required,
263 bool result_contains_top_on_entry);
264
265 // Undo allocation in new space. The object passed and objects allocated after
266 // it will no longer be allocated. Make sure that no pointers are left to the
267 // object(s) no longer allocated as they would be invalid when allocation is
268 // un-done.
269 void UndoAllocationInNewSpace(Register object);
270
271 // ---------------------------------------------------------------------------
ager@chromium.org9085a012009-05-11 19:22:57 +0000272 // Support functions.
273
274 // Check if result is zero and op is negative.
275 void NegativeZeroTest(Register result, Register op, Label* then_label);
276
277 // Check if result is zero and op is negative in code using jump targets.
278 void NegativeZeroTest(CodeGenerator* cgen,
279 Register result,
280 Register op,
281 JumpTarget* then_target);
282
283 // Check if result is zero and any of op1 and op2 are negative.
284 // Register scratch is destroyed, and it must be different from op2.
285 void NegativeZeroTest(Register result, Register op1, Register op2,
286 Register scratch, Label* then_label);
287
288 // Try to get function prototype of a function and puts the value in
289 // the result register. Checks that the function really is a
290 // function and jumps to the miss label if the fast checks fail. The
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000291 // function register will be untouched; the other register may be
ager@chromium.org9085a012009-05-11 19:22:57 +0000292 // clobbered.
293 void TryGetFunctionPrototype(Register function,
294 Register result,
ager@chromium.org9085a012009-05-11 19:22:57 +0000295 Label* miss);
296
297 // Generates code for reporting that an illegal operation has
298 // occurred.
299 void IllegalOperation(int num_arguments);
300
301 // ---------------------------------------------------------------------------
302 // Runtime calls
303
304 // Call a code stub.
305 void CallStub(CodeStub* stub);
306
307 // Return from a code stub after popping its arguments.
308 void StubReturn(int argc);
309
310 // Call a runtime routine.
311 // Eventually this should be used for all C calls.
312 void CallRuntime(Runtime::Function* f, int num_arguments);
313
314 // Convenience function: Same as above, but takes the fid instead.
315 void CallRuntime(Runtime::FunctionId id, int num_arguments);
316
317 // Tail call of a runtime routine (jump).
318 // Like JumpToBuiltin, but also takes care of passing the number
319 // of arguments.
320 void TailCallRuntime(const ExternalReference& ext, int num_arguments);
321
322 // Jump to the builtin routine.
323 void JumpToBuiltin(const ExternalReference& ext);
324
325
326 // ---------------------------------------------------------------------------
327 // Utilities
328
329 void Ret();
330
331 struct Unresolved {
332 int pc;
333 uint32_t flags; // see Bootstrapper::FixupFlags decoders/encoders.
334 const char* name;
335 };
336 List<Unresolved>* unresolved() { return &unresolved_; }
337
338 Handle<Object> CodeObject() { return code_object_; }
339
340
341 // ---------------------------------------------------------------------------
342 // StatsCounter support
343
344 void SetCounter(StatsCounter* counter, int value);
345 void IncrementCounter(StatsCounter* counter, int value);
346 void DecrementCounter(StatsCounter* counter, int value);
347
348
349 // ---------------------------------------------------------------------------
350 // Debugging
351
352 // Calls Abort(msg) if the condition cc is not satisfied.
353 // Use --debug_code to enable.
354 void Assert(Condition cc, const char* msg);
355
356 // Like Assert(), but always enabled.
357 void Check(Condition cc, const char* msg);
358
359 // Print a message to stdout and abort execution.
360 void Abort(const char* msg);
361
362 // Verify restrictions about code generated in stubs.
363 void set_generating_stub(bool value) { generating_stub_ = value; }
364 bool generating_stub() { return generating_stub_; }
365 void set_allow_stub_calls(bool value) { allow_stub_calls_ = value; }
366 bool allow_stub_calls() { return allow_stub_calls_; }
367
368 private:
369 List<Unresolved> unresolved_;
370 bool generating_stub_;
371 bool allow_stub_calls_;
372 Handle<Object> code_object_; // This handle will be patched with the code
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000373 // object on installation.
ager@chromium.org9085a012009-05-11 19:22:57 +0000374
375 // Helper functions for generating invokes.
376 void InvokePrologue(const ParameterCount& expected,
377 const ParameterCount& actual,
378 Handle<Code> code_constant,
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000379 Register code_register,
ager@chromium.org9085a012009-05-11 19:22:57 +0000380 Label* done,
381 InvokeFlag flag);
382
383 // Get the code for the given builtin. Returns if able to resolve
384 // the function in the 'resolved' flag.
385 Handle<Code> ResolveBuiltin(Builtins::JavaScript id, bool* resolved);
386
387 // Activation support.
388 void EnterFrame(StackFrame::Type type);
389 void LeaveFrame(StackFrame::Type type);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000390
391 // Allocation support helpers.
392 void LoadAllocationTopHelper(Register result,
393 Register result_end,
394 Register scratch,
395 bool result_contains_top_on_entry);
396 void UpdateAllocationTopHelper(Register result_end, Register scratch);
ager@chromium.org9085a012009-05-11 19:22:57 +0000397};
398
399
400// The code patcher is used to patch (typically) small parts of code e.g. for
401// debugging and other types of instrumentation. When using the code patcher
402// the exact number of bytes specified must be emitted. Is not legal to emit
403// relocation information. If any of these constraints are violated it causes
404// an assertion.
405class CodePatcher {
406 public:
407 CodePatcher(byte* address, int size);
408 virtual ~CodePatcher();
409
410 // Macro assembler to emit code.
411 MacroAssembler* masm() { return &masm_; }
412
413 private:
414 byte* address_; // The address of the code being patched.
415 int size_; // Number of bytes of the expected patch size.
416 MacroAssembler masm_; // Macro assembler used to generate the code.
417};
418
419
420// -----------------------------------------------------------------------------
421// Static helper functions.
422
423// Generate an Operand for loading a field from an object.
424static inline Operand FieldOperand(Register object, int offset) {
425 return Operand(object, offset - kHeapObjectTag);
426}
427
428
429// Generate an Operand for loading an indexed field from an object.
430static inline Operand FieldOperand(Register object,
431 Register index,
432 ScaleFactor scale,
433 int offset) {
434 return Operand(object, index, scale, offset - kHeapObjectTag);
435}
436
437
438#ifdef GENERATED_CODE_COVERAGE
439extern void LogGeneratedCodeCoverage(const char* file_line);
440#define CODE_COVERAGE_STRINGIFY(x) #x
441#define CODE_COVERAGE_TOSTRING(x) CODE_COVERAGE_STRINGIFY(x)
442#define __FILE_LINE__ __FILE__ ":" CODE_COVERAGE_TOSTRING(__LINE__)
443#define ACCESS_MASM(masm) { \
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000444 byte* x64_coverage_function = \
ager@chromium.org9085a012009-05-11 19:22:57 +0000445 reinterpret_cast<byte*>(FUNCTION_ADDR(LogGeneratedCodeCoverage)); \
446 masm->pushfd(); \
447 masm->pushad(); \
448 masm->push(Immediate(reinterpret_cast<int>(&__FILE_LINE__))); \
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000449 masm->call(x64_coverage_function, RelocInfo::RUNTIME_ENTRY); \
ager@chromium.org9085a012009-05-11 19:22:57 +0000450 masm->pop(rax); \
451 masm->popad(); \
452 masm->popfd(); \
453 } \
454 masm->
455#else
456#define ACCESS_MASM(masm) masm->
457#endif
458
459
460} } // namespace v8::internal
461
462#endif // V8_X64_MACRO_ASSEMBLER_X64_H_