Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame^] | 1 | // Copyright 2010 the V8 project authors. All rights reserved. |
| 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_X64_CODE_STUBS_X64_H_ |
| 29 | #define V8_X64_CODE_STUBS_X64_H_ |
| 30 | |
| 31 | #include "ic-inl.h" |
| 32 | #include "type-info.h" |
| 33 | |
| 34 | namespace v8 { |
| 35 | namespace internal { |
| 36 | |
| 37 | |
| 38 | // Compute a transcendental math function natively, or call the |
| 39 | // TranscendentalCache runtime function. |
| 40 | class TranscendentalCacheStub: public CodeStub { |
| 41 | public: |
| 42 | explicit TranscendentalCacheStub(TranscendentalCache::Type type) |
| 43 | : type_(type) {} |
| 44 | void Generate(MacroAssembler* masm); |
| 45 | private: |
| 46 | TranscendentalCache::Type type_; |
| 47 | Major MajorKey() { return TranscendentalCache; } |
| 48 | int MinorKey() { return type_; } |
| 49 | Runtime::FunctionId RuntimeFunction(); |
| 50 | void GenerateOperation(MacroAssembler* masm, Label* on_nan_result); |
| 51 | }; |
| 52 | |
| 53 | |
| 54 | class ToBooleanStub: public CodeStub { |
| 55 | public: |
| 56 | ToBooleanStub() { } |
| 57 | |
| 58 | void Generate(MacroAssembler* masm); |
| 59 | |
| 60 | private: |
| 61 | Major MajorKey() { return ToBoolean; } |
| 62 | int MinorKey() { return 0; } |
| 63 | }; |
| 64 | |
| 65 | |
| 66 | // Flag that indicates how to generate code for the stub GenericBinaryOpStub. |
| 67 | enum GenericBinaryFlags { |
| 68 | NO_GENERIC_BINARY_FLAGS = 0, |
| 69 | NO_SMI_CODE_IN_STUB = 1 << 0 // Omit smi code in stub. |
| 70 | }; |
| 71 | |
| 72 | |
| 73 | class GenericBinaryOpStub: public CodeStub { |
| 74 | public: |
| 75 | GenericBinaryOpStub(Token::Value op, |
| 76 | OverwriteMode mode, |
| 77 | GenericBinaryFlags flags, |
| 78 | TypeInfo operands_type = TypeInfo::Unknown()) |
| 79 | : op_(op), |
| 80 | mode_(mode), |
| 81 | flags_(flags), |
| 82 | args_in_registers_(false), |
| 83 | args_reversed_(false), |
| 84 | static_operands_type_(operands_type), |
| 85 | runtime_operands_type_(BinaryOpIC::DEFAULT), |
| 86 | name_(NULL) { |
| 87 | ASSERT(OpBits::is_valid(Token::NUM_TOKENS)); |
| 88 | } |
| 89 | |
| 90 | GenericBinaryOpStub(int key, BinaryOpIC::TypeInfo type_info) |
| 91 | : op_(OpBits::decode(key)), |
| 92 | mode_(ModeBits::decode(key)), |
| 93 | flags_(FlagBits::decode(key)), |
| 94 | args_in_registers_(ArgsInRegistersBits::decode(key)), |
| 95 | args_reversed_(ArgsReversedBits::decode(key)), |
| 96 | static_operands_type_(TypeInfo::ExpandedRepresentation( |
| 97 | StaticTypeInfoBits::decode(key))), |
| 98 | runtime_operands_type_(type_info), |
| 99 | name_(NULL) { |
| 100 | } |
| 101 | |
| 102 | // Generate code to call the stub with the supplied arguments. This will add |
| 103 | // code at the call site to prepare arguments either in registers or on the |
| 104 | // stack together with the actual call. |
| 105 | void GenerateCall(MacroAssembler* masm, Register left, Register right); |
| 106 | void GenerateCall(MacroAssembler* masm, Register left, Smi* right); |
| 107 | void GenerateCall(MacroAssembler* masm, Smi* left, Register right); |
| 108 | |
| 109 | bool ArgsInRegistersSupported() { |
| 110 | return (op_ == Token::ADD) || (op_ == Token::SUB) |
| 111 | || (op_ == Token::MUL) || (op_ == Token::DIV); |
| 112 | } |
| 113 | |
| 114 | private: |
| 115 | Token::Value op_; |
| 116 | OverwriteMode mode_; |
| 117 | GenericBinaryFlags flags_; |
| 118 | bool args_in_registers_; // Arguments passed in registers not on the stack. |
| 119 | bool args_reversed_; // Left and right argument are swapped. |
| 120 | |
| 121 | // Number type information of operands, determined by code generator. |
| 122 | TypeInfo static_operands_type_; |
| 123 | |
| 124 | // Operand type information determined at runtime. |
| 125 | BinaryOpIC::TypeInfo runtime_operands_type_; |
| 126 | |
| 127 | char* name_; |
| 128 | |
| 129 | const char* GetName(); |
| 130 | |
| 131 | #ifdef DEBUG |
| 132 | void Print() { |
| 133 | PrintF("GenericBinaryOpStub %d (op %s), " |
| 134 | "(mode %d, flags %d, registers %d, reversed %d, only_numbers %s)\n", |
| 135 | MinorKey(), |
| 136 | Token::String(op_), |
| 137 | static_cast<int>(mode_), |
| 138 | static_cast<int>(flags_), |
| 139 | static_cast<int>(args_in_registers_), |
| 140 | static_cast<int>(args_reversed_), |
| 141 | static_operands_type_.ToString()); |
| 142 | } |
| 143 | #endif |
| 144 | |
| 145 | // Minor key encoding in 17 bits TTNNNFRAOOOOOOOMM. |
| 146 | class ModeBits: public BitField<OverwriteMode, 0, 2> {}; |
| 147 | class OpBits: public BitField<Token::Value, 2, 7> {}; |
| 148 | class ArgsInRegistersBits: public BitField<bool, 9, 1> {}; |
| 149 | class ArgsReversedBits: public BitField<bool, 10, 1> {}; |
| 150 | class FlagBits: public BitField<GenericBinaryFlags, 11, 1> {}; |
| 151 | class StaticTypeInfoBits: public BitField<int, 12, 3> {}; |
| 152 | class RuntimeTypeInfoBits: public BitField<BinaryOpIC::TypeInfo, 15, 2> {}; |
| 153 | |
| 154 | Major MajorKey() { return GenericBinaryOp; } |
| 155 | int MinorKey() { |
| 156 | // Encode the parameters in a unique 18 bit value. |
| 157 | return OpBits::encode(op_) |
| 158 | | ModeBits::encode(mode_) |
| 159 | | FlagBits::encode(flags_) |
| 160 | | ArgsInRegistersBits::encode(args_in_registers_) |
| 161 | | ArgsReversedBits::encode(args_reversed_) |
| 162 | | StaticTypeInfoBits::encode( |
| 163 | static_operands_type_.ThreeBitRepresentation()) |
| 164 | | RuntimeTypeInfoBits::encode(runtime_operands_type_); |
| 165 | } |
| 166 | |
| 167 | void Generate(MacroAssembler* masm); |
| 168 | void GenerateSmiCode(MacroAssembler* masm, Label* slow); |
| 169 | void GenerateLoadArguments(MacroAssembler* masm); |
| 170 | void GenerateReturn(MacroAssembler* masm); |
| 171 | void GenerateRegisterArgsPush(MacroAssembler* masm); |
| 172 | void GenerateTypeTransition(MacroAssembler* masm); |
| 173 | |
| 174 | bool IsOperationCommutative() { |
| 175 | return (op_ == Token::ADD) || (op_ == Token::MUL); |
| 176 | } |
| 177 | |
| 178 | void SetArgsInRegisters() { args_in_registers_ = true; } |
| 179 | void SetArgsReversed() { args_reversed_ = true; } |
| 180 | bool HasSmiCodeInStub() { return (flags_ & NO_SMI_CODE_IN_STUB) == 0; } |
| 181 | bool HasArgsInRegisters() { return args_in_registers_; } |
| 182 | bool HasArgsReversed() { return args_reversed_; } |
| 183 | |
| 184 | bool ShouldGenerateSmiCode() { |
| 185 | return HasSmiCodeInStub() && |
| 186 | runtime_operands_type_ != BinaryOpIC::HEAP_NUMBERS && |
| 187 | runtime_operands_type_ != BinaryOpIC::STRINGS; |
| 188 | } |
| 189 | |
| 190 | bool ShouldGenerateFPCode() { |
| 191 | return runtime_operands_type_ != BinaryOpIC::STRINGS; |
| 192 | } |
| 193 | |
| 194 | virtual int GetCodeKind() { return Code::BINARY_OP_IC; } |
| 195 | |
| 196 | virtual InlineCacheState GetICState() { |
| 197 | return BinaryOpIC::ToState(runtime_operands_type_); |
| 198 | } |
| 199 | |
| 200 | friend class CodeGenerator; |
| 201 | }; |
| 202 | |
| 203 | class StringHelper : public AllStatic { |
| 204 | public: |
| 205 | // Generate code for copying characters using a simple loop. This should only |
| 206 | // be used in places where the number of characters is small and the |
| 207 | // additional setup and checking in GenerateCopyCharactersREP adds too much |
| 208 | // overhead. Copying of overlapping regions is not supported. |
| 209 | static void GenerateCopyCharacters(MacroAssembler* masm, |
| 210 | Register dest, |
| 211 | Register src, |
| 212 | Register count, |
| 213 | bool ascii); |
| 214 | |
| 215 | // Generate code for copying characters using the rep movs instruction. |
| 216 | // Copies rcx characters from rsi to rdi. Copying of overlapping regions is |
| 217 | // not supported. |
| 218 | static void GenerateCopyCharactersREP(MacroAssembler* masm, |
| 219 | Register dest, // Must be rdi. |
| 220 | Register src, // Must be rsi. |
| 221 | Register count, // Must be rcx. |
| 222 | bool ascii); |
| 223 | |
| 224 | |
| 225 | // Probe the symbol table for a two character string. If the string is |
| 226 | // not found by probing a jump to the label not_found is performed. This jump |
| 227 | // does not guarantee that the string is not in the symbol table. If the |
| 228 | // string is found the code falls through with the string in register rax. |
| 229 | static void GenerateTwoCharacterSymbolTableProbe(MacroAssembler* masm, |
| 230 | Register c1, |
| 231 | Register c2, |
| 232 | Register scratch1, |
| 233 | Register scratch2, |
| 234 | Register scratch3, |
| 235 | Register scratch4, |
| 236 | Label* not_found); |
| 237 | |
| 238 | // Generate string hash. |
| 239 | static void GenerateHashInit(MacroAssembler* masm, |
| 240 | Register hash, |
| 241 | Register character, |
| 242 | Register scratch); |
| 243 | static void GenerateHashAddCharacter(MacroAssembler* masm, |
| 244 | Register hash, |
| 245 | Register character, |
| 246 | Register scratch); |
| 247 | static void GenerateHashGetHash(MacroAssembler* masm, |
| 248 | Register hash, |
| 249 | Register scratch); |
| 250 | |
| 251 | private: |
| 252 | DISALLOW_IMPLICIT_CONSTRUCTORS(StringHelper); |
| 253 | }; |
| 254 | |
| 255 | |
| 256 | // Flag that indicates how to generate code for the stub StringAddStub. |
| 257 | enum StringAddFlags { |
| 258 | NO_STRING_ADD_FLAGS = 0, |
| 259 | NO_STRING_CHECK_IN_STUB = 1 << 0 // Omit string check in stub. |
| 260 | }; |
| 261 | |
| 262 | |
| 263 | class StringAddStub: public CodeStub { |
| 264 | public: |
| 265 | explicit StringAddStub(StringAddFlags flags) { |
| 266 | string_check_ = ((flags & NO_STRING_CHECK_IN_STUB) == 0); |
| 267 | } |
| 268 | |
| 269 | private: |
| 270 | Major MajorKey() { return StringAdd; } |
| 271 | int MinorKey() { return string_check_ ? 0 : 1; } |
| 272 | |
| 273 | void Generate(MacroAssembler* masm); |
| 274 | |
| 275 | // Should the stub check whether arguments are strings? |
| 276 | bool string_check_; |
| 277 | }; |
| 278 | |
| 279 | |
| 280 | class SubStringStub: public CodeStub { |
| 281 | public: |
| 282 | SubStringStub() {} |
| 283 | |
| 284 | private: |
| 285 | Major MajorKey() { return SubString; } |
| 286 | int MinorKey() { return 0; } |
| 287 | |
| 288 | void Generate(MacroAssembler* masm); |
| 289 | }; |
| 290 | |
| 291 | |
| 292 | class StringCompareStub: public CodeStub { |
| 293 | public: |
| 294 | explicit StringCompareStub() {} |
| 295 | |
| 296 | // Compare two flat ascii strings and returns result in rax after popping two |
| 297 | // arguments from the stack. |
| 298 | static void GenerateCompareFlatAsciiStrings(MacroAssembler* masm, |
| 299 | Register left, |
| 300 | Register right, |
| 301 | Register scratch1, |
| 302 | Register scratch2, |
| 303 | Register scratch3, |
| 304 | Register scratch4); |
| 305 | |
| 306 | private: |
| 307 | Major MajorKey() { return StringCompare; } |
| 308 | int MinorKey() { return 0; } |
| 309 | |
| 310 | void Generate(MacroAssembler* masm); |
| 311 | }; |
| 312 | |
| 313 | |
| 314 | class NumberToStringStub: public CodeStub { |
| 315 | public: |
| 316 | NumberToStringStub() { } |
| 317 | |
| 318 | // Generate code to do a lookup in the number string cache. If the number in |
| 319 | // the register object is found in the cache the generated code falls through |
| 320 | // with the result in the result register. The object and the result register |
| 321 | // can be the same. If the number is not found in the cache the code jumps to |
| 322 | // the label not_found with only the content of register object unchanged. |
| 323 | static void GenerateLookupNumberStringCache(MacroAssembler* masm, |
| 324 | Register object, |
| 325 | Register result, |
| 326 | Register scratch1, |
| 327 | Register scratch2, |
| 328 | bool object_is_smi, |
| 329 | Label* not_found); |
| 330 | |
| 331 | private: |
| 332 | static void GenerateConvertHashCodeToIndex(MacroAssembler* masm, |
| 333 | Register hash, |
| 334 | Register mask); |
| 335 | |
| 336 | Major MajorKey() { return NumberToString; } |
| 337 | int MinorKey() { return 0; } |
| 338 | |
| 339 | void Generate(MacroAssembler* masm); |
| 340 | |
| 341 | const char* GetName() { return "NumberToStringStub"; } |
| 342 | |
| 343 | #ifdef DEBUG |
| 344 | void Print() { |
| 345 | PrintF("NumberToStringStub\n"); |
| 346 | } |
| 347 | #endif |
| 348 | }; |
| 349 | |
| 350 | |
| 351 | class RecordWriteStub : public CodeStub { |
| 352 | public: |
| 353 | RecordWriteStub(Register object, Register addr, Register scratch) |
| 354 | : object_(object), addr_(addr), scratch_(scratch) { } |
| 355 | |
| 356 | void Generate(MacroAssembler* masm); |
| 357 | |
| 358 | private: |
| 359 | Register object_; |
| 360 | Register addr_; |
| 361 | Register scratch_; |
| 362 | |
| 363 | #ifdef DEBUG |
| 364 | void Print() { |
| 365 | PrintF("RecordWriteStub (object reg %d), (addr reg %d), (scratch reg %d)\n", |
| 366 | object_.code(), addr_.code(), scratch_.code()); |
| 367 | } |
| 368 | #endif |
| 369 | |
| 370 | // Minor key encoding in 12 bits. 4 bits for each of the three |
| 371 | // registers (object, address and scratch) OOOOAAAASSSS. |
| 372 | class ScratchBits : public BitField<uint32_t, 0, 4> {}; |
| 373 | class AddressBits : public BitField<uint32_t, 4, 4> {}; |
| 374 | class ObjectBits : public BitField<uint32_t, 8, 4> {}; |
| 375 | |
| 376 | Major MajorKey() { return RecordWrite; } |
| 377 | |
| 378 | int MinorKey() { |
| 379 | // Encode the registers. |
| 380 | return ObjectBits::encode(object_.code()) | |
| 381 | AddressBits::encode(addr_.code()) | |
| 382 | ScratchBits::encode(scratch_.code()); |
| 383 | } |
| 384 | }; |
| 385 | |
| 386 | |
| 387 | } } // namespace v8::internal |
| 388 | |
| 389 | #endif // V8_X64_CODE_STUBS_X64_H_ |