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