blob: 8bcc651c15fdaee369cb067c300e54a4b978b981 [file] [log] [blame]
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001// Copyright 2006-2008 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +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
28#ifndef V8_MACRO_ASSEMBLER_IA32_H_
29#define V8_MACRO_ASSEMBLER_IA32_H_
30
31#include "assembler.h"
32
33namespace v8 { namespace internal {
34
35
36// Helper type to make boolean flag easier to read at call-site.
37enum InvokeFlag {
38 CALL_FUNCTION,
39 JUMP_FUNCTION
40};
41
42enum CodeLocation {
43 IN_JAVASCRIPT,
44 IN_JS_ENTRY,
45 IN_C_ENTRY
46};
47
48enum HandlerType {
49 TRY_CATCH_HANDLER,
50 TRY_FINALLY_HANDLER,
51 JS_ENTRY_HANDLER
52};
53
54
55// MacroAssembler implements a collection of frequently used macros.
56class MacroAssembler: public Assembler {
57 public:
58 MacroAssembler(void* buffer, int size);
59
60 // ---------------------------------------------------------------------------
61 // GC Support
62
63 // Set the remembered set bit for [object+offset].
64 // object is the object being stored into, value is the object being stored.
65 // If offset is zero, then the scratch register contains the array index into
66 // the elements array represented as a Smi.
67 // All registers are clobbered by the operation.
68 void RecordWrite(Register object,
69 int offset,
70 Register value,
71 Register scratch);
72
73
74 // ---------------------------------------------------------------------------
75 // Debugger Support
76
77 void SaveRegistersToMemory(RegList regs);
78 void RestoreRegistersFromMemory(RegList regs);
79 void PushRegistersFromMemory(RegList regs);
80 void PopRegistersToMemory(RegList regs);
81 void CopyRegistersFromStackToMemory(Register base,
82 Register scratch,
83 RegList regs);
84
85
86 // ---------------------------------------------------------------------------
87 // Activation frames
88
ager@chromium.org7c537e22008-10-16 08:43:32 +000089 void EnterInternalFrame() { EnterFrame(StackFrame::INTERNAL); }
90 void LeaveInternalFrame() { LeaveFrame(StackFrame::INTERNAL); }
91
92 void EnterConstructFrame() { EnterFrame(StackFrame::CONSTRUCT); }
93 void LeaveConstructFrame() { LeaveFrame(StackFrame::CONSTRUCT); }
ager@chromium.org236ad962008-09-25 09:45:57 +000094
95 // Enter specific kind of exit frame; either EXIT or
96 // EXIT_DEBUG. Expects the number of arguments in register eax and
97 // sets up the number of arguments in register edi and the pointer
98 // to the first argument in register esi.
99 void EnterExitFrame(StackFrame::Type type);
100
101 // Leave the current exit frame. Expects the return value in
102 // register eax:edx (untouched) and the pointer to the first
103 // argument in register esi.
104 void LeaveExitFrame(StackFrame::Type type);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000105
106
107 // ---------------------------------------------------------------------------
108 // JavaScript invokes
109
110 // Invoke the JavaScript function code by either calling or jumping.
111 void InvokeCode(const Operand& code,
112 const ParameterCount& expected,
113 const ParameterCount& actual,
114 InvokeFlag flag);
115
116 void InvokeCode(Handle<Code> code,
117 const ParameterCount& expected,
118 const ParameterCount& actual,
ager@chromium.org236ad962008-09-25 09:45:57 +0000119 RelocInfo::Mode rmode,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000120 InvokeFlag flag);
121
122 // Invoke the JavaScript function in the given register. Changes the
123 // current context to the context in the function before invoking.
124 void InvokeFunction(Register function,
125 const ParameterCount& actual,
126 InvokeFlag flag);
127
128 // Invoke specified builtin JavaScript function. Adds an entry to
129 // the unresolved list if the name does not resolve.
130 void InvokeBuiltin(Builtins::JavaScript id, InvokeFlag flag);
131
132 // Store the code object for the given builtin in the target register.
133 void GetBuiltinEntry(Register target, Builtins::JavaScript id);
134
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000135 // Expression support
136 void Set(Register dst, const Immediate& x);
137 void Set(const Operand& dst, const Immediate& x);
138
139 // FCmp is similar to integer cmp, but requires unsigned
140 // jcc instructions (je, ja, jae, jb, jbe, je, and jz).
141 void FCmp();
142
143 // ---------------------------------------------------------------------------
144 // Exception handling
145
146 // Push a new try handler and link into try handler chain.
147 // The return address must be pushed before calling this helper.
148 // On exit, eax contains TOS (next_sp).
149 void PushTryHandler(CodeLocation try_location, HandlerType type);
150
151
152 // ---------------------------------------------------------------------------
153 // Inline caching support
154
155 // Generates code that verifies that the maps of objects in the
156 // prototype chain of object hasn't changed since the code was
157 // generated and branches to the miss label if any map has. If
158 // necessary the function also generates code for security check
159 // in case of global object holders. The scratch and holder
160 // registers are always clobbered, but the object register is only
161 // clobbered if it the same as the holder register. The function
162 // returns a register containing the holder - either object_reg or
163 // holder_reg.
164 Register CheckMaps(JSObject* object, Register object_reg,
165 JSObject* holder, Register holder_reg,
166 Register scratch, Label* miss);
167
168 // Generate code for checking access rights - used for security checks
169 // on access to global objects across environments. The holder register
170 // is left untouched, but the scratch register is clobbered.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000171 void CheckAccessGlobalProxy(Register holder_reg,
172 Register scratch,
173 Label* miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000174
175
176 // ---------------------------------------------------------------------------
177 // Support functions.
178
179 // Check if result is zero and op is negative.
180 void NegativeZeroTest(Register result, Register op, Label* then_label);
181
182 // Check if result is zero and any of op1 and op2 are negative.
183 // Register scratch is destroyed, and it must be different from op2.
184 void NegativeZeroTest(Register result, Register op1, Register op2,
185 Register scratch, Label* then_label);
186
ager@chromium.org7c537e22008-10-16 08:43:32 +0000187 // Try to get function prototype of a function and puts the value in
188 // the result register. Checks that the function really is a
189 // function and jumps to the miss label if the fast checks fail. The
190 // function register will be untouched; the other registers may be
191 // clobbered.
192 void TryGetFunctionPrototype(Register function,
193 Register result,
194 Register scratch,
195 Label* miss);
196
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000197 // Generates code for reporting that an illegal operation has
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000198 // occurred.
199 void IllegalOperation(int num_arguments);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000200
201 // ---------------------------------------------------------------------------
202 // Runtime calls
203
204 // Call a code stub.
205 void CallStub(CodeStub* stub);
206
207 // Return from a code stub after popping its arguments.
208 void StubReturn(int argc);
209
210 // Call a runtime routine.
211 // Eventually this should be used for all C calls.
212 void CallRuntime(Runtime::Function* f, int num_arguments);
213
214 // Convenience function: Same as above, but takes the fid instead.
215 void CallRuntime(Runtime::FunctionId id, int num_arguments);
216
217 // Tail call of a runtime routine (jump).
218 // Like JumpToBuiltin, but also takes care of passing the number
mads.s.ager31e71382008-08-13 09:32:07 +0000219 // of arguments.
220 void TailCallRuntime(const ExternalReference& ext, int num_arguments);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000221
222 // Jump to the builtin routine.
223 void JumpToBuiltin(const ExternalReference& ext);
224
ager@chromium.org236ad962008-09-25 09:45:57 +0000225
226 // ---------------------------------------------------------------------------
227 // Utilities
228
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000229 void Ret();
230
231 struct Unresolved {
232 int pc;
233 uint32_t flags; // see Bootstrapper::FixupFlags decoders/encoders.
234 const char* name;
235 };
236 List<Unresolved>* unresolved() { return &unresolved_; }
237
238
239 // ---------------------------------------------------------------------------
240 // StatsCounter support
241
242 void SetCounter(StatsCounter* counter, int value);
243 void IncrementCounter(StatsCounter* counter, int value);
244 void DecrementCounter(StatsCounter* counter, int value);
245
246
247 // ---------------------------------------------------------------------------
248 // Debugging
249
250 // Calls Abort(msg) if the condition cc is not satisfied.
251 // Use --debug_code to enable.
252 void Assert(Condition cc, const char* msg);
253
254 // Like Assert(), but always enabled.
255 void Check(Condition cc, const char* msg);
256
257 // Print a message to stdout and abort execution.
258 void Abort(const char* msg);
259
260 // Verify restrictions about code generated in stubs.
261 void set_generating_stub(bool value) { generating_stub_ = value; }
262 bool generating_stub() { return generating_stub_; }
kasper.lund7276f142008-07-30 08:49:36 +0000263 void set_allow_stub_calls(bool value) { allow_stub_calls_ = value; }
264 bool allow_stub_calls() { return allow_stub_calls_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000265
266 private:
267 List<Unresolved> unresolved_;
268 bool generating_stub_;
kasper.lund7276f142008-07-30 08:49:36 +0000269 bool allow_stub_calls_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000270
271 // Helper functions for generating invokes.
272 void InvokePrologue(const ParameterCount& expected,
273 const ParameterCount& actual,
274 Handle<Code> code_constant,
275 const Operand& code_operand,
276 Label* done,
277 InvokeFlag flag);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000278
279 // Get the code for the given builtin. Returns if able to resolve
280 // the function in the 'resolved' flag.
281 Handle<Code> ResolveBuiltin(Builtins::JavaScript id, bool* resolved);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000282
283 // Activation support.
284 void EnterFrame(StackFrame::Type type);
285 void LeaveFrame(StackFrame::Type type);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000286};
287
288
289// The code patcher is used to patch (typically) small parts of code e.g. for
290// debugging and other types of instrumentation. When using the code patcher
291// the exact number of bytes specified must be emitted. Is not legal to emit
292// relocation information. If any of these constraints are violated it causes
293// an assertion.
294class CodePatcher {
295 public:
296 CodePatcher(byte* address, int size);
297 virtual ~CodePatcher();
298
299 // Macro assembler to emit code.
300 MacroAssembler* masm() { return &masm_; }
301
302 private:
303 byte* address_; // The address of the code being patched.
304 int size_; // Number of bytes of the expected patch size.
305 MacroAssembler masm_; // Macro assembler used to generate the code.
306};
307
308// -----------------------------------------------------------------------------
309// Static helper functions.
310
311// Generate an Operand for loading a field from an object.
312static inline Operand FieldOperand(Register object, int offset) {
313 return Operand(object, offset - kHeapObjectTag);
314}
315
316
kasper.lund7276f142008-07-30 08:49:36 +0000317// Generate an Operand for loading an indexed field from an object.
318static inline Operand FieldOperand(Register object,
319 Register index,
320 ScaleFactor scale,
321 int offset) {
322 return Operand(object, index, scale, offset - kHeapObjectTag);
323}
324
325
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000326} } // namespace v8::internal
327
328#endif // V8_MACRO_ASSEMBLER_IA32_H_