blob: 44a76a40e7d34012027df7629923d5b23405125d [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
69 // ---------------------------------------------------------------------------
70 // GC Support
71
72 // Set the remembered set bit for [object+offset].
73 // object is the object being stored into, value is the object being stored.
74 // If offset is zero, then the scratch register contains the array index into
75 // the elements array represented as a Smi.
76 // All registers are clobbered by the operation.
77 void RecordWrite(Register object,
78 int offset,
79 Register value,
80 Register scratch);
81
82#ifdef ENABLE_DEBUGGER_SUPPORT
83 // ---------------------------------------------------------------------------
84 // Debugger Support
85
86 void SaveRegistersToMemory(RegList regs);
87 void RestoreRegistersFromMemory(RegList regs);
88 void PushRegistersFromMemory(RegList regs);
89 void PopRegistersToMemory(RegList regs);
90 void CopyRegistersFromStackToMemory(Register base,
91 Register scratch,
92 RegList regs);
93#endif
94
95 // ---------------------------------------------------------------------------
96 // Activation frames
97
98 void EnterInternalFrame() { EnterFrame(StackFrame::INTERNAL); }
99 void LeaveInternalFrame() { LeaveFrame(StackFrame::INTERNAL); }
100
101 void EnterConstructFrame() { EnterFrame(StackFrame::CONSTRUCT); }
102 void LeaveConstructFrame() { LeaveFrame(StackFrame::CONSTRUCT); }
103
104 // Enter specific kind of exit frame; either EXIT or
105 // EXIT_DEBUG. Expects the number of arguments in register eax and
106 // sets up the number of arguments in register edi and the pointer
107 // to the first argument in register esi.
108 void EnterExitFrame(StackFrame::Type type);
109
110 // Leave the current exit frame. Expects the return value in
111 // register eax:edx (untouched) and the pointer to the first
112 // argument in register esi.
113 void LeaveExitFrame(StackFrame::Type type);
114
115
116 // ---------------------------------------------------------------------------
117 // JavaScript invokes
118
119 // Invoke the JavaScript function code by either calling or jumping.
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000120 void InvokeCode(Register code,
ager@chromium.org9085a012009-05-11 19:22:57 +0000121 const ParameterCount& expected,
122 const ParameterCount& actual,
123 InvokeFlag flag);
124
125 void InvokeCode(Handle<Code> code,
126 const ParameterCount& expected,
127 const ParameterCount& actual,
128 RelocInfo::Mode rmode,
129 InvokeFlag flag);
130
131 // Invoke the JavaScript function in the given register. Changes the
132 // current context to the context in the function before invoking.
133 void InvokeFunction(Register function,
134 const ParameterCount& actual,
135 InvokeFlag flag);
136
137 // Invoke specified builtin JavaScript function. Adds an entry to
138 // the unresolved list if the name does not resolve.
139 void InvokeBuiltin(Builtins::JavaScript id, InvokeFlag flag);
140
141 // Store the code object for the given builtin in the target register.
142 void GetBuiltinEntry(Register target, Builtins::JavaScript id);
143
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000144 // ---------------------------------------------------------------------------
145 // Macro instructions
146
ager@chromium.org9085a012009-05-11 19:22:57 +0000147 // Expression support
ager@chromium.orge2902be2009-06-08 12:21:35 +0000148 void Set(Register dst, int64_t x);
149 void Set(const Operand& dst, int64_t x);
ager@chromium.org9085a012009-05-11 19:22:57 +0000150
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000151 // Handle support
152 bool IsUnsafeSmi(Smi* value);
153 bool IsUnsafeSmi(Handle<Object> value) {
154 return IsUnsafeSmi(Smi::cast(*value));
155 }
156
157 void LoadUnsafeSmi(Register dst, Smi* source);
158 void LoadUnsafeSmi(Register dst, Handle<Object> source) {
159 LoadUnsafeSmi(dst, Smi::cast(*source));
160 }
161
162 void Move(Register dst, Handle<Object> source);
163 void Move(const Operand& dst, Handle<Object> source);
164 void Cmp(Register dst, Handle<Object> source);
ager@chromium.org3e875802009-06-29 08:26:34 +0000165 void Cmp(const Operand& dst, Handle<Object> source);
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000166 void Push(Handle<Object> source);
167
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000168 // Control Flow
169 void Jump(Address destination, RelocInfo::Mode rmode);
170 void Jump(ExternalReference ext);
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000171 void Jump(Handle<Code> code_object, RelocInfo::Mode rmode);
172
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000173 void Call(Address destination, RelocInfo::Mode rmode);
174 void Call(ExternalReference ext);
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000175 void Call(Handle<Code> code_object, RelocInfo::Mode rmode);
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000176
ager@chromium.org9085a012009-05-11 19:22:57 +0000177 // Compare object type for heap object.
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000178 // Always use unsigned comparisons: above and below, not less and greater.
ager@chromium.org9085a012009-05-11 19:22:57 +0000179 // Incoming register is heap_object and outgoing register is map.
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000180 // They may be the same register, and may be kScratchRegister.
ager@chromium.org9085a012009-05-11 19:22:57 +0000181 void CmpObjectType(Register heap_object, InstanceType type, Register map);
182
183 // Compare instance type for map.
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000184 // Always use unsigned comparisons: above and below, not less and greater.
ager@chromium.org9085a012009-05-11 19:22:57 +0000185 void CmpInstanceType(Register map, InstanceType type);
186
187 // FCmp is similar to integer cmp, but requires unsigned
188 // jcc instructions (je, ja, jae, jb, jbe, je, and jz).
189 void FCmp();
190
191 // ---------------------------------------------------------------------------
192 // Exception handling
193
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000194 // Push a new try handler and link into try handler chain. The return
195 // address must be pushed before calling this helper.
ager@chromium.org9085a012009-05-11 19:22:57 +0000196 void PushTryHandler(CodeLocation try_location, HandlerType type);
197
198
199 // ---------------------------------------------------------------------------
200 // Inline caching support
201
202 // Generates code that verifies that the maps of objects in the
203 // prototype chain of object hasn't changed since the code was
204 // generated and branches to the miss label if any map has. If
205 // necessary the function also generates code for security check
206 // in case of global object holders. The scratch and holder
207 // registers are always clobbered, but the object register is only
208 // clobbered if it the same as the holder register. The function
209 // returns a register containing the holder - either object_reg or
210 // holder_reg.
211 Register CheckMaps(JSObject* object, Register object_reg,
212 JSObject* holder, Register holder_reg,
213 Register scratch, Label* miss);
214
215 // Generate code for checking access rights - used for security checks
216 // on access to global objects across environments. The holder register
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000217 // is left untouched, but the scratch register and kScratchRegister,
218 // which must be different, are clobbered.
ager@chromium.org9085a012009-05-11 19:22:57 +0000219 void CheckAccessGlobalProxy(Register holder_reg,
220 Register scratch,
221 Label* miss);
222
223
224 // ---------------------------------------------------------------------------
225 // Support functions.
226
227 // Check if result is zero and op is negative.
228 void NegativeZeroTest(Register result, Register op, Label* then_label);
229
230 // Check if result is zero and op is negative in code using jump targets.
231 void NegativeZeroTest(CodeGenerator* cgen,
232 Register result,
233 Register op,
234 JumpTarget* then_target);
235
236 // Check if result is zero and any of op1 and op2 are negative.
237 // Register scratch is destroyed, and it must be different from op2.
238 void NegativeZeroTest(Register result, Register op1, Register op2,
239 Register scratch, Label* then_label);
240
241 // Try to get function prototype of a function and puts the value in
242 // the result register. Checks that the function really is a
243 // function and jumps to the miss label if the fast checks fail. The
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000244 // function register will be untouched; the other register may be
ager@chromium.org9085a012009-05-11 19:22:57 +0000245 // clobbered.
246 void TryGetFunctionPrototype(Register function,
247 Register result,
ager@chromium.org9085a012009-05-11 19:22:57 +0000248 Label* miss);
249
250 // Generates code for reporting that an illegal operation has
251 // occurred.
252 void IllegalOperation(int num_arguments);
253
254 // ---------------------------------------------------------------------------
255 // Runtime calls
256
257 // Call a code stub.
258 void CallStub(CodeStub* stub);
259
260 // Return from a code stub after popping its arguments.
261 void StubReturn(int argc);
262
263 // Call a runtime routine.
264 // Eventually this should be used for all C calls.
265 void CallRuntime(Runtime::Function* f, int num_arguments);
266
267 // Convenience function: Same as above, but takes the fid instead.
268 void CallRuntime(Runtime::FunctionId id, int num_arguments);
269
270 // Tail call of a runtime routine (jump).
271 // Like JumpToBuiltin, but also takes care of passing the number
272 // of arguments.
273 void TailCallRuntime(const ExternalReference& ext, int num_arguments);
274
275 // Jump to the builtin routine.
276 void JumpToBuiltin(const ExternalReference& ext);
277
278
279 // ---------------------------------------------------------------------------
280 // Utilities
281
282 void Ret();
283
284 struct Unresolved {
285 int pc;
286 uint32_t flags; // see Bootstrapper::FixupFlags decoders/encoders.
287 const char* name;
288 };
289 List<Unresolved>* unresolved() { return &unresolved_; }
290
291 Handle<Object> CodeObject() { return code_object_; }
292
293
294 // ---------------------------------------------------------------------------
295 // StatsCounter support
296
297 void SetCounter(StatsCounter* counter, int value);
298 void IncrementCounter(StatsCounter* counter, int value);
299 void DecrementCounter(StatsCounter* counter, int value);
300
301
302 // ---------------------------------------------------------------------------
303 // Debugging
304
305 // Calls Abort(msg) if the condition cc is not satisfied.
306 // Use --debug_code to enable.
307 void Assert(Condition cc, const char* msg);
308
309 // Like Assert(), but always enabled.
310 void Check(Condition cc, const char* msg);
311
312 // Print a message to stdout and abort execution.
313 void Abort(const char* msg);
314
315 // Verify restrictions about code generated in stubs.
316 void set_generating_stub(bool value) { generating_stub_ = value; }
317 bool generating_stub() { return generating_stub_; }
318 void set_allow_stub_calls(bool value) { allow_stub_calls_ = value; }
319 bool allow_stub_calls() { return allow_stub_calls_; }
320
321 private:
322 List<Unresolved> unresolved_;
323 bool generating_stub_;
324 bool allow_stub_calls_;
325 Handle<Object> code_object_; // This handle will be patched with the code
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000326 // object on installation.
ager@chromium.org9085a012009-05-11 19:22:57 +0000327
328 // Helper functions for generating invokes.
329 void InvokePrologue(const ParameterCount& expected,
330 const ParameterCount& actual,
331 Handle<Code> code_constant,
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000332 Register code_register,
ager@chromium.org9085a012009-05-11 19:22:57 +0000333 Label* done,
334 InvokeFlag flag);
335
336 // Get the code for the given builtin. Returns if able to resolve
337 // the function in the 'resolved' flag.
338 Handle<Code> ResolveBuiltin(Builtins::JavaScript id, bool* resolved);
339
340 // Activation support.
341 void EnterFrame(StackFrame::Type type);
342 void LeaveFrame(StackFrame::Type type);
343};
344
345
346// The code patcher is used to patch (typically) small parts of code e.g. for
347// debugging and other types of instrumentation. When using the code patcher
348// the exact number of bytes specified must be emitted. Is not legal to emit
349// relocation information. If any of these constraints are violated it causes
350// an assertion.
351class CodePatcher {
352 public:
353 CodePatcher(byte* address, int size);
354 virtual ~CodePatcher();
355
356 // Macro assembler to emit code.
357 MacroAssembler* masm() { return &masm_; }
358
359 private:
360 byte* address_; // The address of the code being patched.
361 int size_; // Number of bytes of the expected patch size.
362 MacroAssembler masm_; // Macro assembler used to generate the code.
363};
364
365
366// -----------------------------------------------------------------------------
367// Static helper functions.
368
369// Generate an Operand for loading a field from an object.
370static inline Operand FieldOperand(Register object, int offset) {
371 return Operand(object, offset - kHeapObjectTag);
372}
373
374
375// Generate an Operand for loading an indexed field from an object.
376static inline Operand FieldOperand(Register object,
377 Register index,
378 ScaleFactor scale,
379 int offset) {
380 return Operand(object, index, scale, offset - kHeapObjectTag);
381}
382
383
384#ifdef GENERATED_CODE_COVERAGE
385extern void LogGeneratedCodeCoverage(const char* file_line);
386#define CODE_COVERAGE_STRINGIFY(x) #x
387#define CODE_COVERAGE_TOSTRING(x) CODE_COVERAGE_STRINGIFY(x)
388#define __FILE_LINE__ __FILE__ ":" CODE_COVERAGE_TOSTRING(__LINE__)
389#define ACCESS_MASM(masm) { \
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000390 byte* x64_coverage_function = \
ager@chromium.org9085a012009-05-11 19:22:57 +0000391 reinterpret_cast<byte*>(FUNCTION_ADDR(LogGeneratedCodeCoverage)); \
392 masm->pushfd(); \
393 masm->pushad(); \
394 masm->push(Immediate(reinterpret_cast<int>(&__FILE_LINE__))); \
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000395 masm->call(x64_coverage_function, RelocInfo::RUNTIME_ENTRY); \
ager@chromium.org9085a012009-05-11 19:22:57 +0000396 masm->pop(rax); \
397 masm->popad(); \
398 masm->popfd(); \
399 } \
400 masm->
401#else
402#define ACCESS_MASM(masm) masm->
403#endif
404
405
406} } // namespace v8::internal
407
408#endif // V8_X64_MACRO_ASSEMBLER_X64_H_