blob: ceecebf707ef4c294c4b82c77eeecef55e503ad5 [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// Copyright 2006-2009 the V8 project authors. All rights reserved.
2// 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_IA32_MACRO_ASSEMBLER_IA32_H_
29#define V8_IA32_MACRO_ASSEMBLER_IA32_H_
30
31#include "assembler.h"
32
33namespace v8 {
34namespace internal {
35
Leon Clarkee46be812010-01-19 14:06:41 +000036// Convenience for platform-independent signatures. We do not normally
37// distinguish memory operands from other operands on ia32.
38typedef Operand MemOperand;
39
Steve Blocka7e24c12009-10-30 11:49:00 +000040// Forward declaration.
41class JumpTarget;
42
Steve Blocka7e24c12009-10-30 11:49:00 +000043// MacroAssembler implements a collection of frequently used macros.
44class MacroAssembler: public Assembler {
45 public:
46 MacroAssembler(void* buffer, int size);
47
48 // ---------------------------------------------------------------------------
49 // GC Support
50
51 // Set the remembered set bit for [object+offset].
52 // object is the object being stored into, value is the object being stored.
53 // If offset is zero, then the scratch register contains the array index into
54 // the elements array represented as a Smi.
55 // All registers are clobbered by the operation.
56 void RecordWrite(Register object,
57 int offset,
58 Register value,
59 Register scratch);
60
61#ifdef ENABLE_DEBUGGER_SUPPORT
62 // ---------------------------------------------------------------------------
63 // Debugger Support
64
65 void SaveRegistersToMemory(RegList regs);
66 void RestoreRegistersFromMemory(RegList regs);
67 void PushRegistersFromMemory(RegList regs);
68 void PopRegistersToMemory(RegList regs);
69 void CopyRegistersFromStackToMemory(Register base,
70 Register scratch,
71 RegList regs);
72#endif
73
74 // ---------------------------------------------------------------------------
Steve Blockd0582a62009-12-15 09:54:21 +000075 // Stack limit support
76
77 // Do simple test for stack overflow. This doesn't handle an overflow.
78 void StackLimitCheck(Label* on_stack_limit_hit);
79
80 // ---------------------------------------------------------------------------
Steve Blocka7e24c12009-10-30 11:49:00 +000081 // Activation frames
82
83 void EnterInternalFrame() { EnterFrame(StackFrame::INTERNAL); }
84 void LeaveInternalFrame() { LeaveFrame(StackFrame::INTERNAL); }
85
86 void EnterConstructFrame() { EnterFrame(StackFrame::CONSTRUCT); }
87 void LeaveConstructFrame() { LeaveFrame(StackFrame::CONSTRUCT); }
88
Steve Blockd0582a62009-12-15 09:54:21 +000089 // Enter specific kind of exit frame; either in normal or debug mode.
90 // Expects the number of arguments in register eax and
Steve Blocka7e24c12009-10-30 11:49:00 +000091 // sets up the number of arguments in register edi and the pointer
92 // to the first argument in register esi.
Steve Blockd0582a62009-12-15 09:54:21 +000093 void EnterExitFrame(ExitFrame::Mode mode);
94
95 void EnterApiExitFrame(ExitFrame::Mode mode, int stack_space, int argc);
Steve Blocka7e24c12009-10-30 11:49:00 +000096
97 // Leave the current exit frame. Expects the return value in
98 // register eax:edx (untouched) and the pointer to the first
99 // argument in register esi.
Steve Blockd0582a62009-12-15 09:54:21 +0000100 void LeaveExitFrame(ExitFrame::Mode mode);
Steve Blocka7e24c12009-10-30 11:49:00 +0000101
Steve Blockd0582a62009-12-15 09:54:21 +0000102 // Find the function context up the context chain.
103 void LoadContext(Register dst, int context_chain_length);
Steve Blocka7e24c12009-10-30 11:49:00 +0000104
105 // ---------------------------------------------------------------------------
106 // JavaScript invokes
107
108 // Invoke the JavaScript function code by either calling or jumping.
109 void InvokeCode(const Operand& code,
110 const ParameterCount& expected,
111 const ParameterCount& actual,
112 InvokeFlag flag);
113
114 void InvokeCode(Handle<Code> code,
115 const ParameterCount& expected,
116 const ParameterCount& actual,
117 RelocInfo::Mode rmode,
118 InvokeFlag flag);
119
120 // Invoke the JavaScript function in the given register. Changes the
121 // current context to the context in the function before invoking.
122 void InvokeFunction(Register function,
123 const ParameterCount& actual,
124 InvokeFlag flag);
125
126 // Invoke specified builtin JavaScript function. Adds an entry to
127 // the unresolved list if the name does not resolve.
128 void InvokeBuiltin(Builtins::JavaScript id, InvokeFlag flag);
129
130 // Store the code object for the given builtin in the target register.
131 void GetBuiltinEntry(Register target, Builtins::JavaScript id);
132
133 // Expression support
134 void Set(Register dst, const Immediate& x);
135 void Set(const Operand& dst, const Immediate& x);
136
137 // Compare object type for heap object.
138 // Incoming register is heap_object and outgoing register is map.
139 void CmpObjectType(Register heap_object, InstanceType type, Register map);
140
141 // Compare instance type for map.
142 void CmpInstanceType(Register map, InstanceType type);
143
Leon Clarkee46be812010-01-19 14:06:41 +0000144 // Check if the object in register heap_object is a string. Afterwards the
145 // register map contains the object map and the register instance_type
146 // contains the instance_type. The registers map and instance_type can be the
147 // same in which case it contains the instance type afterwards. Either of the
148 // registers map and instance_type can be the same as heap_object.
149 Condition IsObjectStringType(Register heap_object,
150 Register map,
151 Register instance_type);
152
Steve Blocka7e24c12009-10-30 11:49:00 +0000153 // FCmp is similar to integer cmp, but requires unsigned
154 // jcc instructions (je, ja, jae, jb, jbe, je, and jz).
155 void FCmp();
156
Leon Clarkee46be812010-01-19 14:06:41 +0000157 // Smi tagging support.
158 void SmiTag(Register reg) {
159 ASSERT(kSmiTag == 0);
160 shl(reg, kSmiTagSize);
161 }
162 void SmiUntag(Register reg) {
163 sar(reg, kSmiTagSize);
164 }
165
Steve Blocka7e24c12009-10-30 11:49:00 +0000166 // ---------------------------------------------------------------------------
167 // Exception handling
168
169 // Push a new try handler and link into try handler chain. The return
170 // address must be pushed before calling this helper.
171 void PushTryHandler(CodeLocation try_location, HandlerType type);
172
Leon Clarkee46be812010-01-19 14:06:41 +0000173 // Unlink the stack handler on top of the stack from the try handler chain.
174 void PopTryHandler();
Steve Blocka7e24c12009-10-30 11:49:00 +0000175
176 // ---------------------------------------------------------------------------
177 // Inline caching support
178
179 // Generates code that verifies that the maps of objects in the
180 // prototype chain of object hasn't changed since the code was
181 // generated and branches to the miss label if any map has. If
182 // necessary the function also generates code for security check
183 // in case of global object holders. The scratch and holder
184 // registers are always clobbered, but the object register is only
185 // clobbered if it the same as the holder register. The function
186 // returns a register containing the holder - either object_reg or
187 // holder_reg.
188 Register CheckMaps(JSObject* object, Register object_reg,
189 JSObject* holder, Register holder_reg,
190 Register scratch, Label* miss);
191
192 // Generate code for checking access rights - used for security checks
193 // on access to global objects across environments. The holder register
194 // is left untouched, but the scratch register is clobbered.
195 void CheckAccessGlobalProxy(Register holder_reg,
196 Register scratch,
197 Label* miss);
198
199
200 // ---------------------------------------------------------------------------
201 // Allocation support
202
203 // Allocate an object in new space. If the new space is exhausted control
204 // continues at the gc_required label. The allocated object is returned in
205 // result and end of the new object is returned in result_end. The register
206 // scratch can be passed as no_reg in which case an additional object
207 // reference will be added to the reloc info. The returned pointers in result
208 // and result_end have not yet been tagged as heap objects. If
Steve Blockd0582a62009-12-15 09:54:21 +0000209 // result_contains_top_on_entry is true the content of result is known to be
Steve Blocka7e24c12009-10-30 11:49:00 +0000210 // the allocation top on entry (could be result_end from a previous call to
211 // AllocateInNewSpace). If result_contains_top_on_entry is true scratch
212 // should be no_reg as it is never used.
213 void AllocateInNewSpace(int object_size,
214 Register result,
215 Register result_end,
216 Register scratch,
217 Label* gc_required,
218 AllocationFlags flags);
219
220 void AllocateInNewSpace(int header_size,
221 ScaleFactor element_size,
222 Register element_count,
223 Register result,
224 Register result_end,
225 Register scratch,
226 Label* gc_required,
227 AllocationFlags flags);
228
229 void AllocateInNewSpace(Register object_size,
230 Register result,
231 Register result_end,
232 Register scratch,
233 Label* gc_required,
234 AllocationFlags flags);
235
236 // Undo allocation in new space. The object passed and objects allocated after
237 // it will no longer be allocated. Make sure that no pointers are left to the
238 // object(s) no longer allocated as they would be invalid when allocation is
239 // un-done.
240 void UndoAllocationInNewSpace(Register object);
241
Steve Block3ce2e202009-11-05 08:53:23 +0000242 // Allocate a heap number in new space with undefined value. The
243 // register scratch2 can be passed as no_reg; the others must be
244 // valid registers. Returns tagged pointer in result register, or
245 // jumps to gc_required if new space is full.
246 void AllocateHeapNumber(Register result,
247 Register scratch1,
248 Register scratch2,
249 Label* gc_required);
250
Steve Blockd0582a62009-12-15 09:54:21 +0000251 // Allocate a sequential string. All the header fields of the string object
252 // are initialized.
253 void AllocateTwoByteString(Register result,
254 Register length,
255 Register scratch1,
256 Register scratch2,
257 Register scratch3,
258 Label* gc_required);
259 void AllocateAsciiString(Register result,
260 Register length,
261 Register scratch1,
262 Register scratch2,
263 Register scratch3,
264 Label* gc_required);
265
266 // Allocate a raw cons string object. Only the map field of the result is
267 // initialized.
268 void AllocateConsString(Register result,
269 Register scratch1,
270 Register scratch2,
271 Label* gc_required);
272 void AllocateAsciiConsString(Register result,
273 Register scratch1,
274 Register scratch2,
275 Label* gc_required);
276
Steve Blocka7e24c12009-10-30 11:49:00 +0000277 // ---------------------------------------------------------------------------
278 // Support functions.
279
280 // Check if result is zero and op is negative.
281 void NegativeZeroTest(Register result, Register op, Label* then_label);
282
283 // Check if result is zero and op is negative in code using jump targets.
284 void NegativeZeroTest(CodeGenerator* cgen,
285 Register result,
286 Register op,
287 JumpTarget* then_target);
288
289 // Check if result is zero and any of op1 and op2 are negative.
290 // Register scratch is destroyed, and it must be different from op2.
291 void NegativeZeroTest(Register result, Register op1, Register op2,
292 Register scratch, Label* then_label);
293
294 // Try to get function prototype of a function and puts the value in
295 // the result register. Checks that the function really is a
296 // function and jumps to the miss label if the fast checks fail. The
297 // function register will be untouched; the other registers may be
298 // clobbered.
299 void TryGetFunctionPrototype(Register function,
300 Register result,
301 Register scratch,
302 Label* miss);
303
304 // Generates code for reporting that an illegal operation has
305 // occurred.
306 void IllegalOperation(int num_arguments);
307
308 // ---------------------------------------------------------------------------
309 // Runtime calls
310
Leon Clarkee46be812010-01-19 14:06:41 +0000311 // Call a code stub. Generate the code if necessary.
Steve Blocka7e24c12009-10-30 11:49:00 +0000312 void CallStub(CodeStub* stub);
313
Leon Clarkee46be812010-01-19 14:06:41 +0000314 // Call a code stub and return the code object called. Try to generate
315 // the code if necessary. Do not perform a GC but instead return a retry
316 // after GC failure.
317 Object* TryCallStub(CodeStub* stub);
318
319 // Tail call a code stub (jump). Generate the code if necessary.
Steve Blockd0582a62009-12-15 09:54:21 +0000320 void TailCallStub(CodeStub* stub);
321
Leon Clarkee46be812010-01-19 14:06:41 +0000322 // Tail call a code stub (jump) and return the code object called. Try to
323 // generate the code if necessary. Do not perform a GC but instead return
324 // a retry after GC failure.
325 Object* TryTailCallStub(CodeStub* stub);
326
Steve Blocka7e24c12009-10-30 11:49:00 +0000327 // Return from a code stub after popping its arguments.
328 void StubReturn(int argc);
329
330 // Call a runtime routine.
331 // Eventually this should be used for all C calls.
332 void CallRuntime(Runtime::Function* f, int num_arguments);
333
Leon Clarkee46be812010-01-19 14:06:41 +0000334 // Call a runtime function, returning the RuntimeStub object called.
335 // Try to generate the stub code if necessary. Do not perform a GC
336 // but instead return a retry after GC failure.
337 Object* TryCallRuntime(Runtime::Function* f, int num_arguments);
338
Steve Blocka7e24c12009-10-30 11:49:00 +0000339 // Convenience function: Same as above, but takes the fid instead.
340 void CallRuntime(Runtime::FunctionId id, int num_arguments);
341
Leon Clarkee46be812010-01-19 14:06:41 +0000342 // Convenience function: Same as above, but takes the fid instead.
343 Object* TryCallRuntime(Runtime::FunctionId id, int num_arguments);
344
Steve Blocka7e24c12009-10-30 11:49:00 +0000345 // Tail call of a runtime routine (jump).
346 // Like JumpToRuntime, but also takes care of passing the number
347 // of arguments.
348 void TailCallRuntime(const ExternalReference& ext,
349 int num_arguments,
350 int result_size);
351
Steve Blockd0582a62009-12-15 09:54:21 +0000352 void PushHandleScope(Register scratch);
353
354 // Pops a handle scope using the specified scratch register and
355 // ensuring that saved register, it is not no_reg, is left unchanged.
356 void PopHandleScope(Register saved, Register scratch);
357
Leon Clarkee46be812010-01-19 14:06:41 +0000358 // As PopHandleScope, but does not perform a GC. Instead, returns a
359 // retry after GC failure object if GC is necessary.
360 Object* TryPopHandleScope(Register saved, Register scratch);
361
Steve Blocka7e24c12009-10-30 11:49:00 +0000362 // Jump to a runtime routine.
363 void JumpToRuntime(const ExternalReference& ext);
364
365
366 // ---------------------------------------------------------------------------
367 // Utilities
368
369 void Ret();
370
Leon Clarkee46be812010-01-19 14:06:41 +0000371 // Emit code to discard a non-negative number of pointer-sized elements
372 // from the stack, clobbering only the esp register.
373 void Drop(int element_count);
374
375 void Call(Label* target) { call(target); }
376
377 void Move(Register target, Handle<Object> value);
378
Steve Blocka7e24c12009-10-30 11:49:00 +0000379 struct Unresolved {
380 int pc;
381 uint32_t flags; // see Bootstrapper::FixupFlags decoders/encoders.
382 const char* name;
383 };
384 List<Unresolved>* unresolved() { return &unresolved_; }
385
386 Handle<Object> CodeObject() { return code_object_; }
387
388
389 // ---------------------------------------------------------------------------
390 // StatsCounter support
391
392 void SetCounter(StatsCounter* counter, int value);
393 void IncrementCounter(StatsCounter* counter, int value);
394 void DecrementCounter(StatsCounter* counter, int value);
395
396
397 // ---------------------------------------------------------------------------
398 // Debugging
399
400 // Calls Abort(msg) if the condition cc is not satisfied.
401 // Use --debug_code to enable.
402 void Assert(Condition cc, const char* msg);
403
404 // Like Assert(), but always enabled.
405 void Check(Condition cc, const char* msg);
406
407 // Print a message to stdout and abort execution.
408 void Abort(const char* msg);
409
410 // Verify restrictions about code generated in stubs.
411 void set_generating_stub(bool value) { generating_stub_ = value; }
412 bool generating_stub() { return generating_stub_; }
413 void set_allow_stub_calls(bool value) { allow_stub_calls_ = value; }
414 bool allow_stub_calls() { return allow_stub_calls_; }
415
416 private:
417 List<Unresolved> unresolved_;
418 bool generating_stub_;
419 bool allow_stub_calls_;
420 Handle<Object> code_object_; // This handle will be patched with the
421 // code object on installation.
422
423 // Helper functions for generating invokes.
424 void InvokePrologue(const ParameterCount& expected,
425 const ParameterCount& actual,
426 Handle<Code> code_constant,
427 const Operand& code_operand,
428 Label* done,
429 InvokeFlag flag);
430
431 // Prepares for a call or jump to a builtin by doing two things:
432 // 1. Emits code that fetches the builtin's function object from the context
433 // at runtime, and puts it in the register rdi.
434 // 2. Fetches the builtin's code object, and returns it in a handle, at
435 // compile time, so that later code can emit instructions to jump or call
436 // the builtin directly. If the code object has not yet been created, it
437 // returns the builtin code object for IllegalFunction, and sets the
438 // output parameter "resolved" to false. Code that uses the return value
439 // should then add the address and the builtin name to the list of fixups
440 // called unresolved_, which is fixed up by the bootstrapper.
441 Handle<Code> ResolveBuiltin(Builtins::JavaScript id, bool* resolved);
442
443 // Activation support.
444 void EnterFrame(StackFrame::Type type);
445 void LeaveFrame(StackFrame::Type type);
446
Steve Blockd0582a62009-12-15 09:54:21 +0000447 void EnterExitFramePrologue(ExitFrame::Mode mode);
448 void EnterExitFrameEpilogue(ExitFrame::Mode mode, int argc);
449
Steve Blocka7e24c12009-10-30 11:49:00 +0000450 // Allocation support helpers.
451 void LoadAllocationTopHelper(Register result,
452 Register result_end,
453 Register scratch,
454 AllocationFlags flags);
455 void UpdateAllocationTopHelper(Register result_end, Register scratch);
Leon Clarkee46be812010-01-19 14:06:41 +0000456
457 // Helper for PopHandleScope. Allowed to perform a GC and returns
458 // NULL if gc_allowed. Does not perform a GC if !gc_allowed, and
459 // possibly returns a failure object indicating an allocation failure.
460 Object* PopHandleScopeHelper(Register saved,
461 Register scratch,
462 bool gc_allowed);
Steve Blocka7e24c12009-10-30 11:49:00 +0000463};
464
465
466// The code patcher is used to patch (typically) small parts of code e.g. for
467// debugging and other types of instrumentation. When using the code patcher
468// the exact number of bytes specified must be emitted. Is not legal to emit
469// relocation information. If any of these constraints are violated it causes
470// an assertion.
471class CodePatcher {
472 public:
473 CodePatcher(byte* address, int size);
474 virtual ~CodePatcher();
475
476 // Macro assembler to emit code.
477 MacroAssembler* masm() { return &masm_; }
478
479 private:
480 byte* address_; // The address of the code being patched.
481 int size_; // Number of bytes of the expected patch size.
482 MacroAssembler masm_; // Macro assembler used to generate the code.
483};
484
485
486// -----------------------------------------------------------------------------
487// Static helper functions.
488
489// Generate an Operand for loading a field from an object.
490static inline Operand FieldOperand(Register object, int offset) {
491 return Operand(object, offset - kHeapObjectTag);
492}
493
494
495// Generate an Operand for loading an indexed field from an object.
496static inline Operand FieldOperand(Register object,
497 Register index,
498 ScaleFactor scale,
499 int offset) {
500 return Operand(object, index, scale, offset - kHeapObjectTag);
501}
502
503
504#ifdef GENERATED_CODE_COVERAGE
505extern void LogGeneratedCodeCoverage(const char* file_line);
506#define CODE_COVERAGE_STRINGIFY(x) #x
507#define CODE_COVERAGE_TOSTRING(x) CODE_COVERAGE_STRINGIFY(x)
508#define __FILE_LINE__ __FILE__ ":" CODE_COVERAGE_TOSTRING(__LINE__)
509#define ACCESS_MASM(masm) { \
510 byte* ia32_coverage_function = \
511 reinterpret_cast<byte*>(FUNCTION_ADDR(LogGeneratedCodeCoverage)); \
512 masm->pushfd(); \
513 masm->pushad(); \
514 masm->push(Immediate(reinterpret_cast<int>(&__FILE_LINE__))); \
515 masm->call(ia32_coverage_function, RelocInfo::RUNTIME_ENTRY); \
516 masm->pop(eax); \
517 masm->popad(); \
518 masm->popfd(); \
519 } \
520 masm->
521#else
522#define ACCESS_MASM(masm) masm->
523#endif
524
525
526} } // namespace v8::internal
527
528#endif // V8_IA32_MACRO_ASSEMBLER_IA32_H_