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 | |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 90 | GenericBinaryOpStub(int key, BinaryOpIC::TypeInfo runtime_operands_type) |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 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))), |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 98 | runtime_operands_type_(runtime_operands_type), |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 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), " |
Ben Murdoch | 086aeea | 2011-05-13 15:57:08 +0100 | [diff] [blame] | 134 | "(mode %d, flags %d, registers %d, reversed %d, type_info %s)\n", |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 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> {}; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 152 | class RuntimeTypeInfoBits: public BitField<BinaryOpIC::TypeInfo, 15, 3> {}; |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 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; |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame^] | 201 | friend class LCodeGen; |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 202 | }; |
| 203 | |
Ben Murdoch | 086aeea | 2011-05-13 15:57:08 +0100 | [diff] [blame] | 204 | |
| 205 | class TypeRecordingBinaryOpStub: public CodeStub { |
| 206 | public: |
| 207 | TypeRecordingBinaryOpStub(Token::Value op, OverwriteMode mode) |
| 208 | : op_(op), |
| 209 | mode_(mode), |
| 210 | operands_type_(TRBinaryOpIC::UNINITIALIZED), |
| 211 | result_type_(TRBinaryOpIC::UNINITIALIZED), |
| 212 | name_(NULL) { |
| 213 | ASSERT(OpBits::is_valid(Token::NUM_TOKENS)); |
| 214 | } |
| 215 | |
| 216 | TypeRecordingBinaryOpStub( |
| 217 | int key, |
| 218 | TRBinaryOpIC::TypeInfo operands_type, |
| 219 | TRBinaryOpIC::TypeInfo result_type = TRBinaryOpIC::UNINITIALIZED) |
| 220 | : op_(OpBits::decode(key)), |
| 221 | mode_(ModeBits::decode(key)), |
| 222 | operands_type_(operands_type), |
| 223 | result_type_(result_type), |
| 224 | name_(NULL) { } |
| 225 | |
| 226 | private: |
| 227 | enum SmiCodeGenerateHeapNumberResults { |
| 228 | ALLOW_HEAPNUMBER_RESULTS, |
| 229 | NO_HEAPNUMBER_RESULTS |
| 230 | }; |
| 231 | |
| 232 | Token::Value op_; |
| 233 | OverwriteMode mode_; |
| 234 | |
| 235 | // Operand type information determined at runtime. |
| 236 | TRBinaryOpIC::TypeInfo operands_type_; |
| 237 | TRBinaryOpIC::TypeInfo result_type_; |
| 238 | |
| 239 | char* name_; |
| 240 | |
| 241 | const char* GetName(); |
| 242 | |
| 243 | #ifdef DEBUG |
| 244 | void Print() { |
| 245 | PrintF("TypeRecordingBinaryOpStub %d (op %s), " |
| 246 | "(mode %d, runtime_type_info %s)\n", |
| 247 | MinorKey(), |
| 248 | Token::String(op_), |
| 249 | static_cast<int>(mode_), |
| 250 | TRBinaryOpIC::GetName(operands_type_)); |
| 251 | } |
| 252 | #endif |
| 253 | |
| 254 | // Minor key encoding in 15 bits RRRTTTOOOOOOOMM. |
| 255 | class ModeBits: public BitField<OverwriteMode, 0, 2> {}; |
| 256 | class OpBits: public BitField<Token::Value, 2, 7> {}; |
| 257 | class OperandTypeInfoBits: public BitField<TRBinaryOpIC::TypeInfo, 9, 3> {}; |
| 258 | class ResultTypeInfoBits: public BitField<TRBinaryOpIC::TypeInfo, 12, 3> {}; |
| 259 | |
| 260 | Major MajorKey() { return TypeRecordingBinaryOp; } |
| 261 | int MinorKey() { |
| 262 | return OpBits::encode(op_) |
| 263 | | ModeBits::encode(mode_) |
| 264 | | OperandTypeInfoBits::encode(operands_type_) |
| 265 | | ResultTypeInfoBits::encode(result_type_); |
| 266 | } |
| 267 | |
| 268 | void Generate(MacroAssembler* masm); |
| 269 | void GenerateGeneric(MacroAssembler* masm); |
| 270 | void GenerateSmiCode(MacroAssembler* masm, |
| 271 | Label* slow, |
| 272 | SmiCodeGenerateHeapNumberResults heapnumber_results); |
| 273 | void GenerateLoadArguments(MacroAssembler* masm); |
| 274 | void GenerateReturn(MacroAssembler* masm); |
| 275 | void GenerateUninitializedStub(MacroAssembler* masm); |
| 276 | void GenerateSmiStub(MacroAssembler* masm); |
| 277 | void GenerateInt32Stub(MacroAssembler* masm); |
| 278 | void GenerateHeapNumberStub(MacroAssembler* masm); |
| 279 | void GenerateStringStub(MacroAssembler* masm); |
| 280 | void GenerateGenericStub(MacroAssembler* masm); |
| 281 | |
| 282 | void GenerateHeapResultAllocation(MacroAssembler* masm, Label* alloc_failure); |
| 283 | void GenerateRegisterArgsPush(MacroAssembler* masm); |
| 284 | void GenerateTypeTransition(MacroAssembler* masm); |
| 285 | void GenerateTypeTransitionWithSavedArgs(MacroAssembler* masm); |
| 286 | |
| 287 | virtual int GetCodeKind() { return Code::TYPE_RECORDING_BINARY_OP_IC; } |
| 288 | |
| 289 | virtual InlineCacheState GetICState() { |
| 290 | return TRBinaryOpIC::ToState(operands_type_); |
| 291 | } |
| 292 | |
| 293 | virtual void FinishCode(Code* code) { |
| 294 | code->set_type_recording_binary_op_type(operands_type_); |
| 295 | code->set_type_recording_binary_op_result_type(result_type_); |
| 296 | } |
| 297 | |
| 298 | friend class CodeGenerator; |
| 299 | }; |
| 300 | |
| 301 | |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 302 | class StringHelper : public AllStatic { |
| 303 | public: |
| 304 | // Generate code for copying characters using a simple loop. This should only |
| 305 | // be used in places where the number of characters is small and the |
| 306 | // additional setup and checking in GenerateCopyCharactersREP adds too much |
| 307 | // overhead. Copying of overlapping regions is not supported. |
| 308 | static void GenerateCopyCharacters(MacroAssembler* masm, |
| 309 | Register dest, |
| 310 | Register src, |
| 311 | Register count, |
| 312 | bool ascii); |
| 313 | |
| 314 | // Generate code for copying characters using the rep movs instruction. |
| 315 | // Copies rcx characters from rsi to rdi. Copying of overlapping regions is |
| 316 | // not supported. |
| 317 | static void GenerateCopyCharactersREP(MacroAssembler* masm, |
| 318 | Register dest, // Must be rdi. |
| 319 | Register src, // Must be rsi. |
| 320 | Register count, // Must be rcx. |
| 321 | bool ascii); |
| 322 | |
| 323 | |
| 324 | // Probe the symbol table for a two character string. If the string is |
| 325 | // not found by probing a jump to the label not_found is performed. This jump |
| 326 | // does not guarantee that the string is not in the symbol table. If the |
| 327 | // string is found the code falls through with the string in register rax. |
| 328 | static void GenerateTwoCharacterSymbolTableProbe(MacroAssembler* masm, |
| 329 | Register c1, |
| 330 | Register c2, |
| 331 | Register scratch1, |
| 332 | Register scratch2, |
| 333 | Register scratch3, |
| 334 | Register scratch4, |
| 335 | Label* not_found); |
| 336 | |
| 337 | // Generate string hash. |
| 338 | static void GenerateHashInit(MacroAssembler* masm, |
| 339 | Register hash, |
| 340 | Register character, |
| 341 | Register scratch); |
| 342 | static void GenerateHashAddCharacter(MacroAssembler* masm, |
| 343 | Register hash, |
| 344 | Register character, |
| 345 | Register scratch); |
| 346 | static void GenerateHashGetHash(MacroAssembler* masm, |
| 347 | Register hash, |
| 348 | Register scratch); |
| 349 | |
| 350 | private: |
| 351 | DISALLOW_IMPLICIT_CONSTRUCTORS(StringHelper); |
| 352 | }; |
| 353 | |
| 354 | |
| 355 | // Flag that indicates how to generate code for the stub StringAddStub. |
| 356 | enum StringAddFlags { |
| 357 | NO_STRING_ADD_FLAGS = 0, |
| 358 | NO_STRING_CHECK_IN_STUB = 1 << 0 // Omit string check in stub. |
| 359 | }; |
| 360 | |
| 361 | |
| 362 | class StringAddStub: public CodeStub { |
| 363 | public: |
| 364 | explicit StringAddStub(StringAddFlags flags) { |
| 365 | string_check_ = ((flags & NO_STRING_CHECK_IN_STUB) == 0); |
| 366 | } |
| 367 | |
| 368 | private: |
| 369 | Major MajorKey() { return StringAdd; } |
| 370 | int MinorKey() { return string_check_ ? 0 : 1; } |
| 371 | |
| 372 | void Generate(MacroAssembler* masm); |
| 373 | |
| 374 | // Should the stub check whether arguments are strings? |
| 375 | bool string_check_; |
| 376 | }; |
| 377 | |
| 378 | |
| 379 | class SubStringStub: public CodeStub { |
| 380 | public: |
| 381 | SubStringStub() {} |
| 382 | |
| 383 | private: |
| 384 | Major MajorKey() { return SubString; } |
| 385 | int MinorKey() { return 0; } |
| 386 | |
| 387 | void Generate(MacroAssembler* masm); |
| 388 | }; |
| 389 | |
| 390 | |
| 391 | class StringCompareStub: public CodeStub { |
| 392 | public: |
| 393 | explicit StringCompareStub() {} |
| 394 | |
| 395 | // Compare two flat ascii strings and returns result in rax after popping two |
| 396 | // arguments from the stack. |
| 397 | static void GenerateCompareFlatAsciiStrings(MacroAssembler* masm, |
| 398 | Register left, |
| 399 | Register right, |
| 400 | Register scratch1, |
| 401 | Register scratch2, |
| 402 | Register scratch3, |
| 403 | Register scratch4); |
| 404 | |
| 405 | private: |
| 406 | Major MajorKey() { return StringCompare; } |
| 407 | int MinorKey() { return 0; } |
| 408 | |
| 409 | void Generate(MacroAssembler* masm); |
| 410 | }; |
| 411 | |
| 412 | |
| 413 | class NumberToStringStub: public CodeStub { |
| 414 | public: |
| 415 | NumberToStringStub() { } |
| 416 | |
| 417 | // Generate code to do a lookup in the number string cache. If the number in |
| 418 | // the register object is found in the cache the generated code falls through |
| 419 | // with the result in the result register. The object and the result register |
| 420 | // can be the same. If the number is not found in the cache the code jumps to |
| 421 | // the label not_found with only the content of register object unchanged. |
| 422 | static void GenerateLookupNumberStringCache(MacroAssembler* masm, |
| 423 | Register object, |
| 424 | Register result, |
| 425 | Register scratch1, |
| 426 | Register scratch2, |
| 427 | bool object_is_smi, |
| 428 | Label* not_found); |
| 429 | |
| 430 | private: |
| 431 | static void GenerateConvertHashCodeToIndex(MacroAssembler* masm, |
| 432 | Register hash, |
| 433 | Register mask); |
| 434 | |
| 435 | Major MajorKey() { return NumberToString; } |
| 436 | int MinorKey() { return 0; } |
| 437 | |
| 438 | void Generate(MacroAssembler* masm); |
| 439 | |
| 440 | const char* GetName() { return "NumberToStringStub"; } |
| 441 | |
| 442 | #ifdef DEBUG |
| 443 | void Print() { |
| 444 | PrintF("NumberToStringStub\n"); |
| 445 | } |
| 446 | #endif |
| 447 | }; |
| 448 | |
| 449 | |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 450 | } } // namespace v8::internal |
| 451 | |
| 452 | #endif // V8_X64_CODE_STUBS_X64_H_ |