erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 1 | // Copyright 2010 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 | |
| 28 | #ifndef V8_CODEGEN_H_ |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 29 | #define V8_CODEGEN_H_ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 30 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 31 | #include "code-stubs.h" |
| 32 | #include "runtime.h" |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [diff] [blame] | 33 | #include "type-info.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 34 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 35 | // Include the declaration of the architecture defined class CodeGenerator. |
| 36 | // The contract to the shared code is that the the CodeGenerator is a subclass |
| 37 | // of Visitor and that the following methods are available publicly: |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 38 | // MakeCode |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 39 | // MakeCodePrologue |
| 40 | // MakeCodeEpilogue |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 41 | // masm |
| 42 | // frame |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 43 | // script |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 44 | // has_valid_frame |
| 45 | // SetFrame |
| 46 | // DeleteFrame |
| 47 | // allocator |
| 48 | // AddDeferred |
| 49 | // in_spilled_code |
| 50 | // set_in_spilled_code |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 51 | // RecordPositions |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 52 | // |
| 53 | // These methods are either used privately by the shared code or implemented as |
| 54 | // shared code: |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 55 | // CodeGenerator |
| 56 | // ~CodeGenerator |
| 57 | // ProcessDeferred |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 58 | // Generate |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 59 | // ComputeLazyCompile |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 60 | // BuildFunctionInfo |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 61 | // ComputeCallInitialize |
| 62 | // ComputeCallInitializeInLoop |
| 63 | // ProcessDeclarations |
| 64 | // DeclareGlobals |
| 65 | // CheckForInlineRuntimeCall |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 66 | // AnalyzeCondition |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 67 | // CodeForFunctionPosition |
| 68 | // CodeForReturnPosition |
| 69 | // CodeForStatementPosition |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 70 | // CodeForDoWhileConditionPosition |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 71 | // CodeForSourcePosition |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 72 | |
lrn@chromium.org | c4e51ac | 2010-08-09 09:47:21 +0000 | [diff] [blame] | 73 | #if V8_TARGET_ARCH_IA32 |
| 74 | #include "ia32/codegen-ia32.h" |
| 75 | #elif V8_TARGET_ARCH_X64 |
| 76 | #include "x64/codegen-x64.h" |
| 77 | #elif V8_TARGET_ARCH_ARM |
| 78 | #include "arm/codegen-arm.h" |
| 79 | #elif V8_TARGET_ARCH_MIPS |
| 80 | #include "mips/codegen-mips.h" |
| 81 | #else |
| 82 | #error Unsupported target architecture. |
| 83 | #endif |
| 84 | |
| 85 | #include "register-allocator.h" |
| 86 | |
| 87 | namespace v8 { |
| 88 | namespace internal { |
| 89 | |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 90 | // Code generation can be nested. Code generation scopes form a stack |
| 91 | // of active code generators. |
| 92 | class CodeGeneratorScope BASE_EMBEDDED { |
| 93 | public: |
| 94 | explicit CodeGeneratorScope(CodeGenerator* cgen) { |
| 95 | previous_ = top_; |
| 96 | top_ = cgen; |
| 97 | } |
| 98 | |
| 99 | ~CodeGeneratorScope() { |
| 100 | top_ = previous_; |
| 101 | } |
| 102 | |
| 103 | static CodeGenerator* Current() { |
| 104 | ASSERT(top_ != NULL); |
| 105 | return top_; |
| 106 | } |
| 107 | |
| 108 | private: |
| 109 | static CodeGenerator* top_; |
| 110 | CodeGenerator* previous_; |
| 111 | }; |
| 112 | |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 113 | |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 114 | #if V8_TARGET_ARCH_IA32 || V8_TARGET_ARCH_X64 |
| 115 | |
| 116 | // State of used registers in a virtual frame. |
| 117 | class FrameRegisterState { |
| 118 | public: |
| 119 | // Captures the current state of the given frame. |
| 120 | explicit FrameRegisterState(VirtualFrame* frame); |
| 121 | |
| 122 | // Saves the state in the stack. |
| 123 | void Save(MacroAssembler* masm) const; |
| 124 | |
| 125 | // Restores the state from the stack. |
| 126 | void Restore(MacroAssembler* masm) const; |
| 127 | |
| 128 | private: |
| 129 | // Constants indicating special actions. They should not be multiples |
| 130 | // of kPointerSize so they will not collide with valid offsets from |
| 131 | // the frame pointer. |
| 132 | static const int kIgnore = -1; |
| 133 | static const int kPush = 1; |
| 134 | |
| 135 | // This flag is ored with a valid offset from the frame pointer, so |
| 136 | // it should fit in the low zero bits of a valid offset. |
| 137 | static const int kSyncedFlag = 2; |
| 138 | |
| 139 | int registers_[RegisterAllocator::kNumRegisters]; |
| 140 | }; |
| 141 | |
| 142 | #elif V8_TARGET_ARCH_ARM || V8_TARGET_ARCH_MIPS |
| 143 | |
| 144 | |
| 145 | class FrameRegisterState { |
| 146 | public: |
| 147 | inline FrameRegisterState(VirtualFrame frame) : frame_(frame) { } |
| 148 | |
| 149 | inline const VirtualFrame* frame() const { return &frame_; } |
| 150 | |
| 151 | private: |
| 152 | VirtualFrame frame_; |
| 153 | }; |
| 154 | |
| 155 | #else |
| 156 | |
| 157 | #error Unsupported target architecture. |
| 158 | |
| 159 | #endif |
| 160 | |
| 161 | |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 162 | // RuntimeCallHelper implementation that saves/restores state of a |
| 163 | // virtual frame. |
| 164 | class VirtualFrameRuntimeCallHelper : public RuntimeCallHelper { |
| 165 | public: |
| 166 | // Does not take ownership of |frame_state|. |
| 167 | explicit VirtualFrameRuntimeCallHelper(const FrameRegisterState* frame_state) |
| 168 | : frame_state_(frame_state) {} |
| 169 | |
| 170 | virtual void BeforeCall(MacroAssembler* masm) const; |
| 171 | |
| 172 | virtual void AfterCall(MacroAssembler* masm) const; |
| 173 | |
| 174 | private: |
| 175 | const FrameRegisterState* frame_state_; |
| 176 | }; |
| 177 | |
| 178 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 179 | // Deferred code objects are small pieces of code that are compiled |
| 180 | // out of line. They are used to defer the compilation of uncommon |
| 181 | // paths thereby avoiding expensive jumps around uncommon code parts. |
| 182 | class DeferredCode: public ZoneObject { |
| 183 | public: |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 184 | DeferredCode(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 185 | virtual ~DeferredCode() { } |
| 186 | |
| 187 | virtual void Generate() = 0; |
| 188 | |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 189 | MacroAssembler* masm() { return masm_; } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 190 | |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 191 | int statement_position() const { return statement_position_; } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 192 | int position() const { return position_; } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 193 | |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 194 | Label* entry_label() { return &entry_label_; } |
| 195 | Label* exit_label() { return &exit_label_; } |
| 196 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 197 | #ifdef DEBUG |
| 198 | void set_comment(const char* comment) { comment_ = comment; } |
| 199 | const char* comment() const { return comment_; } |
| 200 | #else |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 201 | void set_comment(const char* comment) { } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 202 | const char* comment() const { return ""; } |
| 203 | #endif |
| 204 | |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 205 | inline void Jump(); |
| 206 | inline void Branch(Condition cc); |
| 207 | void BindExit() { masm_->bind(&exit_label_); } |
| 208 | |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 209 | const FrameRegisterState* frame_state() const { return &frame_state_; } |
| 210 | |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 211 | void SaveRegisters(); |
| 212 | void RestoreRegisters(); |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 213 | void Exit(); |
| 214 | |
| 215 | // If this returns true then all registers will be saved for the duration |
| 216 | // of the Generate() call. Otherwise the registers are not saved and the |
| 217 | // Generate() call must bracket runtime any runtime calls with calls to |
| 218 | // SaveRegisters() and RestoreRegisters(). In this case the Generate |
| 219 | // method must also call Exit() in order to return to the non-deferred |
| 220 | // code. |
| 221 | virtual bool AutoSaveAndRestore() { return true; } |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 222 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 223 | protected: |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 224 | MacroAssembler* masm_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 225 | |
| 226 | private: |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 227 | int statement_position_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 228 | int position_; |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 229 | |
| 230 | Label entry_label_; |
| 231 | Label exit_label_; |
| 232 | |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 233 | FrameRegisterState frame_state_; |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 234 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 235 | #ifdef DEBUG |
| 236 | const char* comment_; |
| 237 | #endif |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 238 | DISALLOW_COPY_AND_ASSIGN(DeferredCode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 239 | }; |
| 240 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 241 | |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 242 | } } // namespace v8::internal |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 243 | |
| 244 | #endif // V8_CODEGEN_H_ |