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