blob: 0ddbd5d3a2f9c532c698ca1af17333997ef32444 [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
Andrei Popescu31002712010-02-23 13:46:05 +0000144 // Check if the map of an object is equal to a specified map and
145 // branch to label if not. Skip the smi check if not required
146 // (object is known to be a heap object)
147 void CheckMap(Register obj,
148 Handle<Map> map,
149 Label* fail,
150 bool is_heap_object);
151
Leon Clarkee46be812010-01-19 14:06:41 +0000152 // Check if the object in register heap_object is a string. Afterwards the
153 // register map contains the object map and the register instance_type
154 // contains the instance_type. The registers map and instance_type can be the
155 // same in which case it contains the instance type afterwards. Either of the
156 // registers map and instance_type can be the same as heap_object.
157 Condition IsObjectStringType(Register heap_object,
158 Register map,
159 Register instance_type);
160
Steve Blocka7e24c12009-10-30 11:49:00 +0000161 // FCmp is similar to integer cmp, but requires unsigned
162 // jcc instructions (je, ja, jae, jb, jbe, je, and jz).
163 void FCmp();
164
Leon Clarkee46be812010-01-19 14:06:41 +0000165 // Smi tagging support.
166 void SmiTag(Register reg) {
167 ASSERT(kSmiTag == 0);
168 shl(reg, kSmiTagSize);
169 }
170 void SmiUntag(Register reg) {
171 sar(reg, kSmiTagSize);
172 }
173
Steve Blocka7e24c12009-10-30 11:49:00 +0000174 // ---------------------------------------------------------------------------
175 // Exception handling
176
177 // Push a new try handler and link into try handler chain. The return
178 // address must be pushed before calling this helper.
179 void PushTryHandler(CodeLocation try_location, HandlerType type);
180
Leon Clarkee46be812010-01-19 14:06:41 +0000181 // Unlink the stack handler on top of the stack from the try handler chain.
182 void PopTryHandler();
Steve Blocka7e24c12009-10-30 11:49:00 +0000183
184 // ---------------------------------------------------------------------------
185 // Inline caching support
186
187 // Generates code that verifies that the maps of objects in the
188 // prototype chain of object hasn't changed since the code was
189 // generated and branches to the miss label if any map has. If
190 // necessary the function also generates code for security check
191 // in case of global object holders. The scratch and holder
192 // registers are always clobbered, but the object register is only
193 // clobbered if it the same as the holder register. The function
194 // returns a register containing the holder - either object_reg or
195 // holder_reg.
196 Register CheckMaps(JSObject* object, Register object_reg,
197 JSObject* holder, Register holder_reg,
198 Register scratch, Label* miss);
199
200 // Generate code for checking access rights - used for security checks
201 // on access to global objects across environments. The holder register
202 // is left untouched, but the scratch register is clobbered.
203 void CheckAccessGlobalProxy(Register holder_reg,
204 Register scratch,
205 Label* miss);
206
207
208 // ---------------------------------------------------------------------------
209 // Allocation support
210
211 // Allocate an object in new space. If the new space is exhausted control
212 // continues at the gc_required label. The allocated object is returned in
213 // result and end of the new object is returned in result_end. The register
214 // scratch can be passed as no_reg in which case an additional object
215 // reference will be added to the reloc info. The returned pointers in result
216 // and result_end have not yet been tagged as heap objects. If
Steve Blockd0582a62009-12-15 09:54:21 +0000217 // result_contains_top_on_entry is true the content of result is known to be
Steve Blocka7e24c12009-10-30 11:49:00 +0000218 // the allocation top on entry (could be result_end from a previous call to
219 // AllocateInNewSpace). If result_contains_top_on_entry is true scratch
220 // should be no_reg as it is never used.
221 void AllocateInNewSpace(int object_size,
222 Register result,
223 Register result_end,
224 Register scratch,
225 Label* gc_required,
226 AllocationFlags flags);
227
228 void AllocateInNewSpace(int header_size,
229 ScaleFactor element_size,
230 Register element_count,
231 Register result,
232 Register result_end,
233 Register scratch,
234 Label* gc_required,
235 AllocationFlags flags);
236
237 void AllocateInNewSpace(Register object_size,
238 Register result,
239 Register result_end,
240 Register scratch,
241 Label* gc_required,
242 AllocationFlags flags);
243
244 // Undo allocation in new space. The object passed and objects allocated after
245 // it will no longer be allocated. Make sure that no pointers are left to the
246 // object(s) no longer allocated as they would be invalid when allocation is
247 // un-done.
248 void UndoAllocationInNewSpace(Register object);
249
Steve Block3ce2e202009-11-05 08:53:23 +0000250 // Allocate a heap number in new space with undefined value. The
251 // register scratch2 can be passed as no_reg; the others must be
252 // valid registers. Returns tagged pointer in result register, or
253 // jumps to gc_required if new space is full.
254 void AllocateHeapNumber(Register result,
255 Register scratch1,
256 Register scratch2,
257 Label* gc_required);
258
Steve Blockd0582a62009-12-15 09:54:21 +0000259 // Allocate a sequential string. All the header fields of the string object
260 // are initialized.
261 void AllocateTwoByteString(Register result,
262 Register length,
263 Register scratch1,
264 Register scratch2,
265 Register scratch3,
266 Label* gc_required);
267 void AllocateAsciiString(Register result,
268 Register length,
269 Register scratch1,
270 Register scratch2,
271 Register scratch3,
272 Label* gc_required);
273
274 // Allocate a raw cons string object. Only the map field of the result is
275 // initialized.
276 void AllocateConsString(Register result,
277 Register scratch1,
278 Register scratch2,
279 Label* gc_required);
280 void AllocateAsciiConsString(Register result,
281 Register scratch1,
282 Register scratch2,
283 Label* gc_required);
284
Steve Blocka7e24c12009-10-30 11:49:00 +0000285 // ---------------------------------------------------------------------------
286 // Support functions.
287
288 // Check if result is zero and op is negative.
289 void NegativeZeroTest(Register result, Register op, Label* then_label);
290
291 // Check if result is zero and op is negative in code using jump targets.
292 void NegativeZeroTest(CodeGenerator* cgen,
293 Register result,
294 Register op,
295 JumpTarget* then_target);
296
297 // Check if result is zero and any of op1 and op2 are negative.
298 // Register scratch is destroyed, and it must be different from op2.
299 void NegativeZeroTest(Register result, Register op1, Register op2,
300 Register scratch, Label* then_label);
301
302 // Try to get function prototype of a function and puts the value in
303 // the result register. Checks that the function really is a
304 // function and jumps to the miss label if the fast checks fail. The
305 // function register will be untouched; the other registers may be
306 // clobbered.
307 void TryGetFunctionPrototype(Register function,
308 Register result,
309 Register scratch,
310 Label* miss);
311
312 // Generates code for reporting that an illegal operation has
313 // occurred.
314 void IllegalOperation(int num_arguments);
315
316 // ---------------------------------------------------------------------------
317 // Runtime calls
318
Leon Clarkee46be812010-01-19 14:06:41 +0000319 // Call a code stub. Generate the code if necessary.
Steve Blocka7e24c12009-10-30 11:49:00 +0000320 void CallStub(CodeStub* stub);
321
Leon Clarkee46be812010-01-19 14:06:41 +0000322 // Call a code stub and return the code object called. Try to generate
323 // the code if necessary. Do not perform a GC but instead return a retry
324 // after GC failure.
325 Object* TryCallStub(CodeStub* stub);
326
327 // Tail call a code stub (jump). Generate the code if necessary.
Steve Blockd0582a62009-12-15 09:54:21 +0000328 void TailCallStub(CodeStub* stub);
329
Leon Clarkee46be812010-01-19 14:06:41 +0000330 // Tail call a code stub (jump) and return the code object called. Try to
331 // generate the code if necessary. Do not perform a GC but instead return
332 // a retry after GC failure.
333 Object* TryTailCallStub(CodeStub* stub);
334
Steve Blocka7e24c12009-10-30 11:49:00 +0000335 // Return from a code stub after popping its arguments.
336 void StubReturn(int argc);
337
338 // Call a runtime routine.
339 // Eventually this should be used for all C calls.
340 void CallRuntime(Runtime::Function* f, int num_arguments);
341
Leon Clarke4515c472010-02-03 11:58:03 +0000342 // Call a runtime function, returning the CodeStub object called.
Leon Clarkee46be812010-01-19 14:06:41 +0000343 // Try to generate the stub code if necessary. Do not perform a GC
344 // but instead return a retry after GC failure.
345 Object* TryCallRuntime(Runtime::Function* f, int num_arguments);
346
Steve Blocka7e24c12009-10-30 11:49:00 +0000347 // Convenience function: Same as above, but takes the fid instead.
348 void CallRuntime(Runtime::FunctionId id, int num_arguments);
349
Leon Clarkee46be812010-01-19 14:06:41 +0000350 // Convenience function: Same as above, but takes the fid instead.
351 Object* TryCallRuntime(Runtime::FunctionId id, int num_arguments);
352
Steve Blocka7e24c12009-10-30 11:49:00 +0000353 // Tail call of a runtime routine (jump).
354 // Like JumpToRuntime, but also takes care of passing the number
355 // of arguments.
356 void TailCallRuntime(const ExternalReference& ext,
357 int num_arguments,
358 int result_size);
359
Steve Blockd0582a62009-12-15 09:54:21 +0000360 void PushHandleScope(Register scratch);
361
362 // Pops a handle scope using the specified scratch register and
363 // ensuring that saved register, it is not no_reg, is left unchanged.
364 void PopHandleScope(Register saved, Register scratch);
365
Leon Clarkee46be812010-01-19 14:06:41 +0000366 // As PopHandleScope, but does not perform a GC. Instead, returns a
367 // retry after GC failure object if GC is necessary.
368 Object* TryPopHandleScope(Register saved, Register scratch);
369
Steve Blocka7e24c12009-10-30 11:49:00 +0000370 // Jump to a runtime routine.
371 void JumpToRuntime(const ExternalReference& ext);
372
373
374 // ---------------------------------------------------------------------------
375 // Utilities
376
377 void Ret();
378
Leon Clarkee46be812010-01-19 14:06:41 +0000379 // Emit code to discard a non-negative number of pointer-sized elements
380 // from the stack, clobbering only the esp register.
381 void Drop(int element_count);
382
383 void Call(Label* target) { call(target); }
384
385 void Move(Register target, Handle<Object> value);
386
Steve Blocka7e24c12009-10-30 11:49:00 +0000387 struct Unresolved {
388 int pc;
389 uint32_t flags; // see Bootstrapper::FixupFlags decoders/encoders.
390 const char* name;
391 };
392 List<Unresolved>* unresolved() { return &unresolved_; }
393
394 Handle<Object> CodeObject() { return code_object_; }
395
396
397 // ---------------------------------------------------------------------------
398 // StatsCounter support
399
400 void SetCounter(StatsCounter* counter, int value);
401 void IncrementCounter(StatsCounter* counter, int value);
402 void DecrementCounter(StatsCounter* counter, int value);
Leon Clarked91b9f72010-01-27 17:25:45 +0000403 void IncrementCounter(Condition cc, StatsCounter* counter, int value);
404 void DecrementCounter(Condition cc, StatsCounter* counter, int value);
Steve Blocka7e24c12009-10-30 11:49:00 +0000405
406
407 // ---------------------------------------------------------------------------
408 // Debugging
409
410 // Calls Abort(msg) if the condition cc is not satisfied.
411 // Use --debug_code to enable.
412 void Assert(Condition cc, const char* msg);
413
414 // Like Assert(), but always enabled.
415 void Check(Condition cc, const char* msg);
416
417 // Print a message to stdout and abort execution.
418 void Abort(const char* msg);
419
420 // Verify restrictions about code generated in stubs.
421 void set_generating_stub(bool value) { generating_stub_ = value; }
422 bool generating_stub() { return generating_stub_; }
423 void set_allow_stub_calls(bool value) { allow_stub_calls_ = value; }
424 bool allow_stub_calls() { return allow_stub_calls_; }
425
Leon Clarked91b9f72010-01-27 17:25:45 +0000426 // ---------------------------------------------------------------------------
427 // String utilities.
428
429 // Checks if both objects are sequential ASCII strings, and jumps to label
430 // if either is not.
431 void JumpIfNotBothSequentialAsciiStrings(Register object1,
432 Register object2,
433 Register scratch1,
434 Register scratch2,
435 Label *on_not_flat_ascii_strings);
436
Steve Blocka7e24c12009-10-30 11:49:00 +0000437 private:
438 List<Unresolved> unresolved_;
439 bool generating_stub_;
440 bool allow_stub_calls_;
Andrei Popescu31002712010-02-23 13:46:05 +0000441 // This handle will be patched with the code object on installation.
442 Handle<Object> code_object_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000443
444 // Helper functions for generating invokes.
445 void InvokePrologue(const ParameterCount& expected,
446 const ParameterCount& actual,
447 Handle<Code> code_constant,
448 const Operand& code_operand,
449 Label* done,
450 InvokeFlag flag);
451
452 // Prepares for a call or jump to a builtin by doing two things:
453 // 1. Emits code that fetches the builtin's function object from the context
454 // at runtime, and puts it in the register rdi.
455 // 2. Fetches the builtin's code object, and returns it in a handle, at
456 // compile time, so that later code can emit instructions to jump or call
457 // the builtin directly. If the code object has not yet been created, it
458 // returns the builtin code object for IllegalFunction, and sets the
459 // output parameter "resolved" to false. Code that uses the return value
460 // should then add the address and the builtin name to the list of fixups
461 // called unresolved_, which is fixed up by the bootstrapper.
462 Handle<Code> ResolveBuiltin(Builtins::JavaScript id, bool* resolved);
463
464 // Activation support.
465 void EnterFrame(StackFrame::Type type);
466 void LeaveFrame(StackFrame::Type type);
467
Steve Blockd0582a62009-12-15 09:54:21 +0000468 void EnterExitFramePrologue(ExitFrame::Mode mode);
469 void EnterExitFrameEpilogue(ExitFrame::Mode mode, int argc);
470
Steve Blocka7e24c12009-10-30 11:49:00 +0000471 // Allocation support helpers.
472 void LoadAllocationTopHelper(Register result,
473 Register result_end,
474 Register scratch,
475 AllocationFlags flags);
476 void UpdateAllocationTopHelper(Register result_end, Register scratch);
Leon Clarkee46be812010-01-19 14:06:41 +0000477
478 // Helper for PopHandleScope. Allowed to perform a GC and returns
479 // NULL if gc_allowed. Does not perform a GC if !gc_allowed, and
480 // possibly returns a failure object indicating an allocation failure.
481 Object* PopHandleScopeHelper(Register saved,
482 Register scratch,
483 bool gc_allowed);
Steve Blocka7e24c12009-10-30 11:49:00 +0000484};
485
486
487// The code patcher is used to patch (typically) small parts of code e.g. for
488// debugging and other types of instrumentation. When using the code patcher
489// the exact number of bytes specified must be emitted. Is not legal to emit
490// relocation information. If any of these constraints are violated it causes
491// an assertion.
492class CodePatcher {
493 public:
494 CodePatcher(byte* address, int size);
495 virtual ~CodePatcher();
496
497 // Macro assembler to emit code.
498 MacroAssembler* masm() { return &masm_; }
499
500 private:
501 byte* address_; // The address of the code being patched.
502 int size_; // Number of bytes of the expected patch size.
503 MacroAssembler masm_; // Macro assembler used to generate the code.
504};
505
506
507// -----------------------------------------------------------------------------
508// Static helper functions.
509
510// Generate an Operand for loading a field from an object.
511static inline Operand FieldOperand(Register object, int offset) {
512 return Operand(object, offset - kHeapObjectTag);
513}
514
515
516// Generate an Operand for loading an indexed field from an object.
517static inline Operand FieldOperand(Register object,
518 Register index,
519 ScaleFactor scale,
520 int offset) {
521 return Operand(object, index, scale, offset - kHeapObjectTag);
522}
523
524
525#ifdef GENERATED_CODE_COVERAGE
526extern void LogGeneratedCodeCoverage(const char* file_line);
527#define CODE_COVERAGE_STRINGIFY(x) #x
528#define CODE_COVERAGE_TOSTRING(x) CODE_COVERAGE_STRINGIFY(x)
529#define __FILE_LINE__ __FILE__ ":" CODE_COVERAGE_TOSTRING(__LINE__)
530#define ACCESS_MASM(masm) { \
531 byte* ia32_coverage_function = \
532 reinterpret_cast<byte*>(FUNCTION_ADDR(LogGeneratedCodeCoverage)); \
533 masm->pushfd(); \
534 masm->pushad(); \
535 masm->push(Immediate(reinterpret_cast<int>(&__FILE_LINE__))); \
536 masm->call(ia32_coverage_function, RelocInfo::RUNTIME_ENTRY); \
537 masm->pop(eax); \
538 masm->popad(); \
539 masm->popfd(); \
540 } \
541 masm->
542#else
543#define ACCESS_MASM(masm) masm->
544#endif
545
546
547} } // namespace v8::internal
548
549#endif // V8_IA32_MACRO_ASSEMBLER_IA32_H_