blob: 4af372a81a3eae3ed4742337b15fab141467a42e [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.
120 void InvokeCode(const Operand& code,
121 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
144 // Expression support
ager@chromium.orge2902be2009-06-08 12:21:35 +0000145 void Set(Register dst, int64_t x);
146 void Set(const Operand& dst, int64_t x);
ager@chromium.org9085a012009-05-11 19:22:57 +0000147
148 // Compare object type for heap object.
149 // Incoming register is heap_object and outgoing register is map.
150 void CmpObjectType(Register heap_object, InstanceType type, Register map);
151
152 // Compare instance type for map.
153 void CmpInstanceType(Register map, InstanceType type);
154
155 // FCmp is similar to integer cmp, but requires unsigned
156 // jcc instructions (je, ja, jae, jb, jbe, je, and jz).
157 void FCmp();
158
159 // ---------------------------------------------------------------------------
160 // Exception handling
161
162 // Push a new try handler and link into try handler chain.
163 // The return address must be pushed before calling this helper.
ager@chromium.orge2902be2009-06-08 12:21:35 +0000164 // On exit, rax contains TOS (next_sp).
ager@chromium.org9085a012009-05-11 19:22:57 +0000165 void PushTryHandler(CodeLocation try_location, HandlerType type);
166
167
168 // ---------------------------------------------------------------------------
169 // Inline caching support
170
171 // Generates code that verifies that the maps of objects in the
172 // prototype chain of object hasn't changed since the code was
173 // generated and branches to the miss label if any map has. If
174 // necessary the function also generates code for security check
175 // in case of global object holders. The scratch and holder
176 // registers are always clobbered, but the object register is only
177 // clobbered if it the same as the holder register. The function
178 // returns a register containing the holder - either object_reg or
179 // holder_reg.
180 Register CheckMaps(JSObject* object, Register object_reg,
181 JSObject* holder, Register holder_reg,
182 Register scratch, Label* miss);
183
184 // Generate code for checking access rights - used for security checks
185 // on access to global objects across environments. The holder register
186 // is left untouched, but the scratch register is clobbered.
187 void CheckAccessGlobalProxy(Register holder_reg,
188 Register scratch,
189 Label* miss);
190
191
192 // ---------------------------------------------------------------------------
193 // Support functions.
194
195 // Check if result is zero and op is negative.
196 void NegativeZeroTest(Register result, Register op, Label* then_label);
197
198 // Check if result is zero and op is negative in code using jump targets.
199 void NegativeZeroTest(CodeGenerator* cgen,
200 Register result,
201 Register op,
202 JumpTarget* then_target);
203
204 // Check if result is zero and any of op1 and op2 are negative.
205 // Register scratch is destroyed, and it must be different from op2.
206 void NegativeZeroTest(Register result, Register op1, Register op2,
207 Register scratch, Label* then_label);
208
209 // Try to get function prototype of a function and puts the value in
210 // the result register. Checks that the function really is a
211 // function and jumps to the miss label if the fast checks fail. The
212 // function register will be untouched; the other registers may be
213 // clobbered.
214 void TryGetFunctionPrototype(Register function,
215 Register result,
216 Register scratch,
217 Label* miss);
218
219 // Generates code for reporting that an illegal operation has
220 // occurred.
221 void IllegalOperation(int num_arguments);
222
223 // ---------------------------------------------------------------------------
224 // Runtime calls
225
226 // Call a code stub.
227 void CallStub(CodeStub* stub);
228
229 // Return from a code stub after popping its arguments.
230 void StubReturn(int argc);
231
232 // Call a runtime routine.
233 // Eventually this should be used for all C calls.
234 void CallRuntime(Runtime::Function* f, int num_arguments);
235
236 // Convenience function: Same as above, but takes the fid instead.
237 void CallRuntime(Runtime::FunctionId id, int num_arguments);
238
239 // Tail call of a runtime routine (jump).
240 // Like JumpToBuiltin, but also takes care of passing the number
241 // of arguments.
242 void TailCallRuntime(const ExternalReference& ext, int num_arguments);
243
244 // Jump to the builtin routine.
245 void JumpToBuiltin(const ExternalReference& ext);
246
247
248 // ---------------------------------------------------------------------------
249 // Utilities
250
251 void Ret();
252
253 struct Unresolved {
254 int pc;
255 uint32_t flags; // see Bootstrapper::FixupFlags decoders/encoders.
256 const char* name;
257 };
258 List<Unresolved>* unresolved() { return &unresolved_; }
259
260 Handle<Object> CodeObject() { return code_object_; }
261
262
263 // ---------------------------------------------------------------------------
264 // StatsCounter support
265
266 void SetCounter(StatsCounter* counter, int value);
267 void IncrementCounter(StatsCounter* counter, int value);
268 void DecrementCounter(StatsCounter* counter, int value);
269
270
271 // ---------------------------------------------------------------------------
272 // Debugging
273
274 // Calls Abort(msg) if the condition cc is not satisfied.
275 // Use --debug_code to enable.
276 void Assert(Condition cc, const char* msg);
277
278 // Like Assert(), but always enabled.
279 void Check(Condition cc, const char* msg);
280
281 // Print a message to stdout and abort execution.
282 void Abort(const char* msg);
283
284 // Verify restrictions about code generated in stubs.
285 void set_generating_stub(bool value) { generating_stub_ = value; }
286 bool generating_stub() { return generating_stub_; }
287 void set_allow_stub_calls(bool value) { allow_stub_calls_ = value; }
288 bool allow_stub_calls() { return allow_stub_calls_; }
289
290 private:
291 List<Unresolved> unresolved_;
292 bool generating_stub_;
293 bool allow_stub_calls_;
294 Handle<Object> code_object_; // This handle will be patched with the code
295 // code object on installation.
296
297 // Helper functions for generating invokes.
298 void InvokePrologue(const ParameterCount& expected,
299 const ParameterCount& actual,
300 Handle<Code> code_constant,
301 const Operand& code_operand,
302 Label* done,
303 InvokeFlag flag);
304
305 // Get the code for the given builtin. Returns if able to resolve
306 // the function in the 'resolved' flag.
307 Handle<Code> ResolveBuiltin(Builtins::JavaScript id, bool* resolved);
308
309 // Activation support.
310 void EnterFrame(StackFrame::Type type);
311 void LeaveFrame(StackFrame::Type type);
312};
313
314
315// The code patcher is used to patch (typically) small parts of code e.g. for
316// debugging and other types of instrumentation. When using the code patcher
317// the exact number of bytes specified must be emitted. Is not legal to emit
318// relocation information. If any of these constraints are violated it causes
319// an assertion.
320class CodePatcher {
321 public:
322 CodePatcher(byte* address, int size);
323 virtual ~CodePatcher();
324
325 // Macro assembler to emit code.
326 MacroAssembler* masm() { return &masm_; }
327
328 private:
329 byte* address_; // The address of the code being patched.
330 int size_; // Number of bytes of the expected patch size.
331 MacroAssembler masm_; // Macro assembler used to generate the code.
332};
333
334
335// -----------------------------------------------------------------------------
336// Static helper functions.
337
338// Generate an Operand for loading a field from an object.
339static inline Operand FieldOperand(Register object, int offset) {
340 return Operand(object, offset - kHeapObjectTag);
341}
342
343
344// Generate an Operand for loading an indexed field from an object.
345static inline Operand FieldOperand(Register object,
346 Register index,
347 ScaleFactor scale,
348 int offset) {
349 return Operand(object, index, scale, offset - kHeapObjectTag);
350}
351
352
353#ifdef GENERATED_CODE_COVERAGE
354extern void LogGeneratedCodeCoverage(const char* file_line);
355#define CODE_COVERAGE_STRINGIFY(x) #x
356#define CODE_COVERAGE_TOSTRING(x) CODE_COVERAGE_STRINGIFY(x)
357#define __FILE_LINE__ __FILE__ ":" CODE_COVERAGE_TOSTRING(__LINE__)
358#define ACCESS_MASM(masm) { \
359 byte* x64_coverage_function = \
360 reinterpret_cast<byte*>(FUNCTION_ADDR(LogGeneratedCodeCoverage)); \
361 masm->pushfd(); \
362 masm->pushad(); \
363 masm->push(Immediate(reinterpret_cast<int>(&__FILE_LINE__))); \
364 masm->call(x64_coverage_function, RelocInfo::RUNTIME_ENTRY); \
365 masm->pop(rax); \
366 masm->popad(); \
367 masm->popfd(); \
368 } \
369 masm->
370#else
371#define ACCESS_MASM(masm) masm->
372#endif
373
374
375} } // namespace v8::internal
376
377#endif // V8_X64_MACRO_ASSEMBLER_X64_H_