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