blob: 67c7bdd30864ae455b02d61f2d45a9aac8d6c712 [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
36// Forward declaration.
37class JumpTarget;
38
39
40// Helper types to make flags easier to read at call sites.
41enum InvokeFlag {
42 CALL_FUNCTION,
43 JUMP_FUNCTION
44};
45
46enum CodeLocation {
47 IN_JAVASCRIPT,
48 IN_JS_ENTRY,
49 IN_C_ENTRY
50};
51
52enum HandlerType {
53 TRY_CATCH_HANDLER,
54 TRY_FINALLY_HANDLER,
55 JS_ENTRY_HANDLER
56};
57
58
59// MacroAssembler implements a collection of frequently used macros.
60class MacroAssembler: public Assembler {
61 public:
62 MacroAssembler(void* buffer, int size);
63
64 // ---------------------------------------------------------------------------
65 // GC Support
66
67 // Set the remembered set bit for [object+offset].
68 // object is the object being stored into, value is the object being stored.
69 // If offset is zero, then the scratch register contains the array index into
70 // the elements array represented as a Smi.
71 // All registers are clobbered by the operation.
72 void RecordWrite(Register object,
73 int offset,
74 Register value,
75 Register scratch);
76
77#ifdef ENABLE_DEBUGGER_SUPPORT
78 // ---------------------------------------------------------------------------
79 // Debugger Support
80
81 void SaveRegistersToMemory(RegList regs);
82 void RestoreRegistersFromMemory(RegList regs);
83 void PushRegistersFromMemory(RegList regs);
84 void PopRegistersToMemory(RegList regs);
85 void CopyRegistersFromStackToMemory(Register base,
86 Register scratch,
87 RegList regs);
88#endif
89
90 // ---------------------------------------------------------------------------
91 // Activation frames
92
93 void EnterInternalFrame() { EnterFrame(StackFrame::INTERNAL); }
94 void LeaveInternalFrame() { LeaveFrame(StackFrame::INTERNAL); }
95
96 void EnterConstructFrame() { EnterFrame(StackFrame::CONSTRUCT); }
97 void LeaveConstructFrame() { LeaveFrame(StackFrame::CONSTRUCT); }
98
99 // Enter specific kind of exit frame; either EXIT or
100 // EXIT_DEBUG. Expects the number of arguments in register eax and
101 // sets up the number of arguments in register edi and the pointer
102 // to the first argument in register esi.
103 void EnterExitFrame(StackFrame::Type type);
104
105 // Leave the current exit frame. Expects the return value in
106 // register eax:edx (untouched) and the pointer to the first
107 // argument in register esi.
108 void LeaveExitFrame(StackFrame::Type type);
109
110
111 // ---------------------------------------------------------------------------
112 // JavaScript invokes
113
114 // Invoke the JavaScript function code by either calling or jumping.
115 void InvokeCode(const Operand& code,
116 const ParameterCount& expected,
117 const ParameterCount& actual,
118 InvokeFlag flag);
119
120 void InvokeCode(Handle<Code> code,
121 const ParameterCount& expected,
122 const ParameterCount& actual,
123 RelocInfo::Mode rmode,
124 InvokeFlag flag);
125
126 // Invoke the JavaScript function in the given register. Changes the
127 // current context to the context in the function before invoking.
128 void InvokeFunction(Register function,
129 const ParameterCount& actual,
130 InvokeFlag flag);
131
132 // Invoke specified builtin JavaScript function. Adds an entry to
133 // the unresolved list if the name does not resolve.
134 void InvokeBuiltin(Builtins::JavaScript id, InvokeFlag flag);
135
136 // Store the code object for the given builtin in the target register.
137 void GetBuiltinEntry(Register target, Builtins::JavaScript id);
138
139 // Expression support
140 void Set(Register dst, const Immediate& x);
141 void Set(const Operand& dst, const Immediate& x);
142
143 // Compare object type for heap object.
144 // Incoming register is heap_object and outgoing register is map.
145 void CmpObjectType(Register heap_object, InstanceType type, Register map);
146
147 // Compare instance type for map.
148 void CmpInstanceType(Register map, InstanceType type);
149
150 // FCmp is similar to integer cmp, but requires unsigned
151 // jcc instructions (je, ja, jae, jb, jbe, je, and jz).
152 void FCmp();
153
154 // ---------------------------------------------------------------------------
155 // Exception handling
156
157 // Push a new try handler and link into try handler chain.
158 // The return address must be pushed before calling this helper.
159 // On exit, eax contains TOS (next_sp).
160 void PushTryHandler(CodeLocation try_location, HandlerType type);
161
162
163 // ---------------------------------------------------------------------------
164 // Inline caching support
165
166 // Generates code that verifies that the maps of objects in the
167 // prototype chain of object hasn't changed since the code was
168 // generated and branches to the miss label if any map has. If
169 // necessary the function also generates code for security check
170 // in case of global object holders. The scratch and holder
171 // registers are always clobbered, but the object register is only
172 // clobbered if it the same as the holder register. The function
173 // returns a register containing the holder - either object_reg or
174 // holder_reg.
175 Register CheckMaps(JSObject* object, Register object_reg,
176 JSObject* holder, Register holder_reg,
177 Register scratch, Label* miss);
178
179 // Generate code for checking access rights - used for security checks
180 // on access to global objects across environments. The holder register
181 // is left untouched, but the scratch register is clobbered.
182 void CheckAccessGlobalProxy(Register holder_reg,
183 Register scratch,
184 Label* miss);
185
186
187 // ---------------------------------------------------------------------------
188 // Support functions.
189
190 // Check if result is zero and op is negative.
191 void NegativeZeroTest(Register result, Register op, Label* then_label);
192
193 // Check if result is zero and op is negative in code using jump targets.
194 void NegativeZeroTest(CodeGenerator* cgen,
195 Register result,
196 Register op,
197 JumpTarget* then_target);
198
199 // Check if result is zero and any of op1 and op2 are negative.
200 // Register scratch is destroyed, and it must be different from op2.
201 void NegativeZeroTest(Register result, Register op1, Register op2,
202 Register scratch, Label* then_label);
203
204 // Try to get function prototype of a function and puts the value in
205 // the result register. Checks that the function really is a
206 // function and jumps to the miss label if the fast checks fail. The
207 // function register will be untouched; the other registers may be
208 // clobbered.
209 void TryGetFunctionPrototype(Register function,
210 Register result,
211 Register scratch,
212 Label* miss);
213
214 // Generates code for reporting that an illegal operation has
215 // occurred.
216 void IllegalOperation(int num_arguments);
217
218 // ---------------------------------------------------------------------------
219 // Runtime calls
220
221 // Call a code stub.
222 void CallStub(CodeStub* stub);
223
224 // Return from a code stub after popping its arguments.
225 void StubReturn(int argc);
226
227 // Call a runtime routine.
228 // Eventually this should be used for all C calls.
229 void CallRuntime(Runtime::Function* f, int num_arguments);
230
231 // Convenience function: Same as above, but takes the fid instead.
232 void CallRuntime(Runtime::FunctionId id, int num_arguments);
233
234 // Tail call of a runtime routine (jump).
235 // Like JumpToBuiltin, but also takes care of passing the number
236 // of arguments.
237 void TailCallRuntime(const ExternalReference& ext, int num_arguments);
238
239 // Jump to the builtin routine.
240 void JumpToBuiltin(const ExternalReference& ext);
241
242
243 // ---------------------------------------------------------------------------
244 // Utilities
245
246 void Ret();
247
248 struct Unresolved {
249 int pc;
250 uint32_t flags; // see Bootstrapper::FixupFlags decoders/encoders.
251 const char* name;
252 };
253 List<Unresolved>* unresolved() { return &unresolved_; }
254
255 Handle<Object> CodeObject() { return code_object_; }
256
257
258 // ---------------------------------------------------------------------------
259 // StatsCounter support
260
261 void SetCounter(StatsCounter* counter, int value);
262 void IncrementCounter(StatsCounter* counter, int value);
263 void DecrementCounter(StatsCounter* counter, int value);
264
265
266 // ---------------------------------------------------------------------------
267 // Debugging
268
269 // Calls Abort(msg) if the condition cc is not satisfied.
270 // Use --debug_code to enable.
271 void Assert(Condition cc, const char* msg);
272
273 // Like Assert(), but always enabled.
274 void Check(Condition cc, const char* msg);
275
276 // Print a message to stdout and abort execution.
277 void Abort(const char* msg);
278
279 // Verify restrictions about code generated in stubs.
280 void set_generating_stub(bool value) { generating_stub_ = value; }
281 bool generating_stub() { return generating_stub_; }
282 void set_allow_stub_calls(bool value) { allow_stub_calls_ = value; }
283 bool allow_stub_calls() { return allow_stub_calls_; }
284
285 private:
286 List<Unresolved> unresolved_;
287 bool generating_stub_;
288 bool allow_stub_calls_;
289 Handle<Object> code_object_; // This handle will be patched with the code
290 // code object on installation.
291
292 // Helper functions for generating invokes.
293 void InvokePrologue(const ParameterCount& expected,
294 const ParameterCount& actual,
295 Handle<Code> code_constant,
296 const Operand& code_operand,
297 Label* done,
298 InvokeFlag flag);
299
300 // Get the code for the given builtin. Returns if able to resolve
301 // the function in the 'resolved' flag.
302 Handle<Code> ResolveBuiltin(Builtins::JavaScript id, bool* resolved);
303
304 // Activation support.
305 void EnterFrame(StackFrame::Type type);
306 void LeaveFrame(StackFrame::Type type);
307};
308
309
310// The code patcher is used to patch (typically) small parts of code e.g. for
311// debugging and other types of instrumentation. When using the code patcher
312// the exact number of bytes specified must be emitted. Is not legal to emit
313// relocation information. If any of these constraints are violated it causes
314// an assertion.
315class CodePatcher {
316 public:
317 CodePatcher(byte* address, int size);
318 virtual ~CodePatcher();
319
320 // Macro assembler to emit code.
321 MacroAssembler* masm() { return &masm_; }
322
323 private:
324 byte* address_; // The address of the code being patched.
325 int size_; // Number of bytes of the expected patch size.
326 MacroAssembler masm_; // Macro assembler used to generate the code.
327};
328
329
330// -----------------------------------------------------------------------------
331// Static helper functions.
332
333// Generate an Operand for loading a field from an object.
334static inline Operand FieldOperand(Register object, int offset) {
335 return Operand(object, offset - kHeapObjectTag);
336}
337
338
339// Generate an Operand for loading an indexed field from an object.
340static inline Operand FieldOperand(Register object,
341 Register index,
342 ScaleFactor scale,
343 int offset) {
344 return Operand(object, index, scale, offset - kHeapObjectTag);
345}
346
347
348#ifdef GENERATED_CODE_COVERAGE
349extern void LogGeneratedCodeCoverage(const char* file_line);
350#define CODE_COVERAGE_STRINGIFY(x) #x
351#define CODE_COVERAGE_TOSTRING(x) CODE_COVERAGE_STRINGIFY(x)
352#define __FILE_LINE__ __FILE__ ":" CODE_COVERAGE_TOSTRING(__LINE__)
353#define ACCESS_MASM(masm) { \
354 byte* x64_coverage_function = \
355 reinterpret_cast<byte*>(FUNCTION_ADDR(LogGeneratedCodeCoverage)); \
356 masm->pushfd(); \
357 masm->pushad(); \
358 masm->push(Immediate(reinterpret_cast<int>(&__FILE_LINE__))); \
359 masm->call(x64_coverage_function, RelocInfo::RUNTIME_ENTRY); \
360 masm->pop(rax); \
361 masm->popad(); \
362 masm->popfd(); \
363 } \
364 masm->
365#else
366#define ACCESS_MASM(masm) masm->
367#endif
368
369
370} } // namespace v8::internal
371
372#endif // V8_X64_MACRO_ASSEMBLER_X64_H_