ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 1 | // Copyright 2006-2008 the V8 project authors. All rights reserved. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +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 | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame^] | 28 | #ifndef V8_IA32_MACRO_ASSEMBLER_IA32_H_ |
| 29 | #define V8_IA32_MACRO_ASSEMBLER_IA32_H_ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 30 | |
| 31 | #include "assembler.h" |
| 32 | |
| 33 | namespace v8 { namespace internal { |
| 34 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 35 | // Forward declaration. |
| 36 | class JumpTarget; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 37 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 38 | |
| 39 | // Helper types to make flags easier to read at call sites. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 76 | #ifdef ENABLE_DEBUGGER_SUPPORT |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 87 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 88 | |
| 89 | // --------------------------------------------------------------------------- |
| 90 | // Activation frames |
| 91 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 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); } |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 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); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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, |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 122 | RelocInfo::Mode rmode, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 138 | // Expression support |
| 139 | void Set(Register dst, const Immediate& x); |
| 140 | void Set(const Operand& dst, const Immediate& x); |
| 141 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 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 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 181 | void CheckAccessGlobalProxy(Register holder_reg, |
| 182 | Register scratch, |
| 183 | Label* miss); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 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 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 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 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 213 | // Generates code for reporting that an illegal operation has |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 214 | // occurred. |
| 215 | void IllegalOperation(int num_arguments); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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 |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 235 | // of arguments. |
| 236 | void TailCallRuntime(const ExternalReference& ext, int num_arguments); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 237 | |
| 238 | // Jump to the builtin routine. |
| 239 | void JumpToBuiltin(const ExternalReference& ext); |
| 240 | |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 241 | |
| 242 | // --------------------------------------------------------------------------- |
| 243 | // Utilities |
| 244 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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 | |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 254 | Handle<Object> CodeObject() { return code_object_; } |
| 255 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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_; } |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 281 | void set_allow_stub_calls(bool value) { allow_stub_calls_ = value; } |
| 282 | bool allow_stub_calls() { return allow_stub_calls_; } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 283 | |
| 284 | private: |
| 285 | List<Unresolved> unresolved_; |
| 286 | bool generating_stub_; |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 287 | bool allow_stub_calls_; |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 288 | Handle<Object> code_object_; // This handle will be patched with the code |
| 289 | // code object on installation. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 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); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 302 | |
| 303 | // Activation support. |
| 304 | void EnterFrame(StackFrame::Type type); |
| 305 | void LeaveFrame(StackFrame::Type type); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 328 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 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 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 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* ia32_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(ia32_coverage_function, RelocInfo::RUNTIME_ENTRY); \ |
| 359 | masm->pop(eax); \ |
| 360 | masm->popad(); \ |
| 361 | masm->popfd(); \ |
| 362 | } \ |
| 363 | masm-> |
| 364 | #else |
| 365 | #define ACCESS_MASM(masm) masm-> |
| 366 | #endif |
| 367 | |
| 368 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 369 | } } // namespace v8::internal |
| 370 | |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame^] | 371 | #endif // V8_IA32_MACRO_ASSEMBLER_IA32_H_ |