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