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