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