cast.tq 15.6 KB
Newer Older
Tobias Tebbi's avatar
Tobias Tebbi committed
1 2 3 4
// Copyright 2019 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
extern macro IsBigInt(HeapObject): bool;
Tobias Tebbi's avatar
Tobias Tebbi committed
6
extern macro IsConstructor(HeapObject): bool;
7 8
extern macro IsCustomElementsReceiverInstanceType(int32): bool;
extern macro IsExtensibleMap(Map): bool;
Tobias Tebbi's avatar
Tobias Tebbi committed
9
extern macro IsNumberNormalized(Number): bool;
10
extern macro IsSafeInteger(Object): bool;
Tobias Tebbi's avatar
Tobias Tebbi committed
11

12 13 14 15 16 17 18 19 20 21
@export
macro IsAccessorInfo(o: HeapObject): bool {
  return Is<AccessorInfo>(o);
}

@export
macro IsAccessorPair(o: HeapObject): bool {
  return Is<AccessorPair>(o);
}

22 23
@export
macro IsAllocationSite(o: HeapObject): bool {
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
  return Is<AllocationSite>(o);
}

@export
macro IsCell(o: HeapObject): bool {
  return Is<Cell>(o);
}

@export
macro IsCode(o: HeapObject): bool {
  return Is<Code>(o);
}

@export
macro IsContext(o: HeapObject): bool {
  return Is<Context>(o);
}

@export
macro IsCoverageInfo(o: HeapObject): bool {
  return Is<CoverageInfo>(o);
}

@export
macro IsDebugInfo(o: HeapObject): bool {
  return Is<DebugInfo>(o);
}

@export
macro IsFixedDoubleArray(o: HeapObject): bool {
  return Is<FixedDoubleArray>(o);
}

@export
macro IsFeedbackCell(o: HeapObject): bool {
  return Is<FeedbackCell>(o);
}

@export
macro IsFeedbackVector(o: HeapObject): bool {
  return Is<FeedbackVector>(o);
}

@export
macro IsHeapNumber(o: HeapObject): bool {
  return Is<HeapNumber>(o);
}

@export
macro IsNativeContext(o: HeapObject): bool {
  return Is<NativeContext>(o);
}

@export
macro IsNumber(o: Object): bool {
  return Is<Number>(o);
}

@export
macro IsPrivateSymbol(o: HeapObject): bool {
  return Is<PrivateSymbol>(o);
}

@export
macro IsPromiseCapability(o: HeapObject): bool {
  return Is<PromiseCapability>(o);
}

@export
macro IsPromiseFulfillReactionJobTask(o: HeapObject): bool {
  return Is<PromiseFulfillReactionJobTask>(o);
}

@export
macro IsPromiseReaction(o: HeapObject): bool {
  return Is<PromiseReaction>(o);
}

@export
macro IsPromiseRejectReactionJobTask(o: HeapObject): bool {
  return Is<PromiseRejectReactionJobTask>(o);
}

@export
macro IsSharedFunctionInfo(o: HeapObject): bool {
  return Is<SharedFunctionInfo>(o);
}

@export
macro IsSymbol(o: HeapObject): bool {
  return Is<Symbol>(o);
115 116
}

Tobias Tebbi's avatar
Tobias Tebbi committed
117 118 119 120 121 122 123 124 125 126 127 128
extern macro TaggedToHeapObject(Object): HeapObject
    labels CastError;
extern macro TaggedToSmi(Object): Smi
    labels CastError;
extern macro TaggedToPositiveSmi(Object): PositiveSmi
    labels CastError;
extern macro TaggedToDirectString(Object): DirectString
    labels CastError;
extern macro HeapObjectToCallable(HeapObject): Callable
    labels CastError;
extern macro HeapObjectToConstructor(HeapObject): Constructor
    labels CastError;
129 130 131
extern macro HeapObjectToJSFunctionWithPrototypeSlot(HeapObject):
    JSFunctionWithPrototypeSlot
    labels CastError;
Tobias Tebbi's avatar
Tobias Tebbi committed
132 133 134 135 136 137

macro Cast<A : type extends WeakHeapObject>(o: A|Object): A labels CastError {
  if (!IsWeakOrCleared(o)) goto CastError;
  return %RawDownCast<A>(o);
}

138 139 140 141 142 143 144 145 146 147 148
macro Cast<A : type extends Object>(implicit context: Context)(o: MaybeObject):
    A labels CastError {
  typeswitch (o) {
    case (WeakHeapObject): {
      goto CastError;
    }
    case (o: Object): {
      return Cast<A>(o) otherwise CastError;
    }
  }
}
Tobias Tebbi's avatar
Tobias Tebbi committed
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177

Cast<Undefined>(o: MaybeObject): Undefined labels CastError {
  if (TaggedNotEqual(o, Undefined)) goto CastError;
  return %RawDownCast<Undefined>(o);
}

macro Cast<A : type extends Object>(implicit context: Context)(o: Object): A
    labels CastError {
  return Cast<A>(TaggedToHeapObject(o) otherwise CastError)
      otherwise CastError;
}

Cast<Smi>(o: Object): Smi
    labels CastError {
  return TaggedToSmi(o) otherwise CastError;
}

Cast<PositiveSmi>(o: Object): PositiveSmi
    labels CastError {
  return TaggedToPositiveSmi(o) otherwise CastError;
}

Cast<Zero>(o: Object): Zero labels CastError {
  if (TaggedEqual(o, SmiConstant(0))) return %RawDownCast<Zero>(o);
  goto CastError;
}

Cast<Number>(o: Object): Number
    labels CastError {
178 179 180 181 182 183 184 185 186 187 188
  typeswitch (o) {
    case (s: Smi): {
      return s;
    }
    case (n: HeapNumber): {
      return n;
    }
    case (Object): {
      goto CastError;
    }
  }
Tobias Tebbi's avatar
Tobias Tebbi committed
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
}

Cast<Undefined>(o: Object): Undefined
    labels CastError {
  const o: MaybeObject = o;
  return Cast<Undefined>(o) otherwise CastError;
}

Cast<Numeric>(o: Object): Numeric labels CastError {
  typeswitch (o) {
    case (o: Number): {
      return o;
    }
    case (o: BigInt): {
      return o;
    }
    case (HeapObject): {
      goto CastError;
    }
  }
}

Cast<TheHole>(o: Object): TheHole labels CastError {
  if (o == TheHole) return %RawDownCast<TheHole>(o);
  goto CastError;
}

Cast<TheHole>(o: HeapObject): TheHole labels CastError {
  const o: Object = o;
  return Cast<TheHole>(o) otherwise CastError;
}

Cast<True>(o: Object): True labels CastError {
  if (o == True) return %RawDownCast<True>(o);
  goto CastError;
}

Cast<True>(o: HeapObject): True labels CastError {
  const o: Object = o;
  return Cast<True>(o) otherwise CastError;
}

Cast<False>(o: Object): False labels CastError {
  if (o == False) return %RawDownCast<False>(o);
  goto CastError;
}

Cast<False>(o: HeapObject): False labels CastError {
  const o: Object = o;
  return Cast<False>(o) otherwise CastError;
}

Cast<Boolean>(o: Object): Boolean labels CastError {
  typeswitch (o) {
    case (o: True): {
      return o;
    }
    case (o: False): {
      return o;
    }
    case (Object): {
      goto CastError;
    }
  }
}

Cast<Boolean>(o: HeapObject): Boolean labels CastError {
  const o: Object = o;
  return Cast<Boolean>(o) otherwise CastError;
}

// TODO(tebbi): These trivial casts for union types should be generated
// automatically.

Cast<JSPrimitive>(o: Object): JSPrimitive labels CastError {
  typeswitch (o) {
    case (o: Numeric): {
      return o;
    }
    case (o: String): {
      return o;
    }
    case (o: Symbol): {
      return o;
    }
    case (o: Boolean): {
      return o;
    }
    case (o: Undefined): {
      return o;
    }
    case (o: Null): {
      return o;
    }
    case (Object): {
      goto CastError;
    }
  }
}

Cast<JSAny>(o: Object): JSAny labels CastError {
  typeswitch (o) {
    case (o: JSPrimitive): {
      return o;
    }
    case (o: JSReceiver): {
      return o;
    }
    case (Object): {
      goto CastError;
    }
  }
}

Cast<JSAny|TheHole>(o: Object): JSAny|TheHole labels CastError {
  typeswitch (o) {
    case (o: JSAny): {
      return o;
    }
    case (o: TheHole): {
      return o;
    }
    case (Object): {
      goto CastError;
    }
  }
}

Cast<Number|TheHole>(o: Object): Number|TheHole labels CastError {
  typeswitch (o) {
    case (o: Number): {
      return o;
    }
    case (o: TheHole): {
      return o;
    }
    case (Object): {
      goto CastError;
    }
  }
}

macro Cast<A : type extends HeapObject>(o: HeapObject): A
    labels CastError;

Cast<HeapObject>(o: HeapObject): HeapObject
labels _CastError {
  return o;
}

Cast<Null>(o: HeapObject): Null
    labels CastError {
  if (o != Null) goto CastError;
  return %RawDownCast<Null>(o);
}

Cast<Undefined>(o: HeapObject): Undefined
    labels CastError {
  const o: MaybeObject = o;
  return Cast<Undefined>(o) otherwise CastError;
}

351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
Cast<EmptyFixedArray>(o: Object): EmptyFixedArray
    labels CastError {
  if (o != kEmptyFixedArray) goto CastError;
  return %RawDownCast<EmptyFixedArray>(o);
}
Cast<EmptyFixedArray>(o: HeapObject): EmptyFixedArray
    labels CastError {
  const o: Object = o;
  return Cast<EmptyFixedArray>(o) otherwise CastError;
}

Cast<(FixedDoubleArray | EmptyFixedArray)>(o: HeapObject): FixedDoubleArray|
    EmptyFixedArray labels CastError {
  typeswitch (o) {
    case (o: EmptyFixedArray): {
      return o;
    }
    case (o: FixedDoubleArray): {
      return o;
    }
    case (HeapObject): {
      goto CastError;
    }
  }
}

Tobias Tebbi's avatar
Tobias Tebbi committed
377 378 379 380 381 382 383 384 385 386 387 388
Cast<Callable>(o: HeapObject): Callable
    labels CastError {
  return HeapObjectToCallable(o) otherwise CastError;
}

Cast<Undefined|Callable>(o: HeapObject): Undefined|Callable
    labels CastError {
  if (o == Undefined) return Undefined;
  return HeapObjectToCallable(o) otherwise CastError;
}

macro Cast<T : type extends Symbol>(o: Symbol): T labels CastError;
389 390 391
Cast<PublicSymbol>(s: Symbol): PublicSymbol labels CastError {
  if (s.flags.is_private) goto CastError;
  return %RawDownCast<PublicSymbol>(s);
Tobias Tebbi's avatar
Tobias Tebbi committed
392
}
393 394
Cast<PrivateSymbol>(s: Symbol): PrivateSymbol labels CastError {
  if (s.flags.is_private) return %RawDownCast<PrivateSymbol>(s);
Tobias Tebbi's avatar
Tobias Tebbi committed
395 396 397
  goto CastError;
}
Cast<PublicSymbol>(o: HeapObject): PublicSymbol labels CastError {
398 399
  const s = Cast<Symbol>(o) otherwise CastError;
  return Cast<PublicSymbol>(s) otherwise CastError;
Tobias Tebbi's avatar
Tobias Tebbi committed
400 401
}
Cast<PrivateSymbol>(o: HeapObject): PrivateSymbol labels CastError {
402 403
  const s = Cast<Symbol>(o) otherwise CastError;
  return Cast<PrivateSymbol>(s) otherwise CastError;
Tobias Tebbi's avatar
Tobias Tebbi committed
404 405 406 407 408 409 410 411 412 413 414 415
}

Cast<DirectString>(o: HeapObject): DirectString
    labels CastError {
  return TaggedToDirectString(o) otherwise CastError;
}

Cast<Constructor>(o: HeapObject): Constructor
    labels CastError {
  return HeapObjectToConstructor(o) otherwise CastError;
}

416 417 418 419 420
Cast<JSFunctionWithPrototypeSlot>(o: HeapObject): JSFunctionWithPrototypeSlot
    labels CastError {
  return HeapObjectToJSFunctionWithPrototypeSlot(o) otherwise CastError;
}

Tobias Tebbi's avatar
Tobias Tebbi committed
421 422 423 424 425
Cast<BigInt>(o: HeapObject): BigInt labels CastError {
  if (IsBigInt(o)) return %RawDownCast<BigInt>(o);
  goto CastError;
}

426
Cast<JSRegExpResult>(implicit context: Context)(o: HeapObject): JSRegExpResult
Tobias Tebbi's avatar
Tobias Tebbi committed
427
    labels CastError {
428
  if (regexp::IsRegExpResult(o)) return %RawDownCast<JSRegExpResult>(o);
Tobias Tebbi's avatar
Tobias Tebbi committed
429 430 431
  goto CastError;
}

432 433
Cast<JSSloppyArgumentsObject>(implicit context: Context)(o: HeapObject):
    JSSloppyArgumentsObject
Tobias Tebbi's avatar
Tobias Tebbi committed
434 435
    labels CastError {
  const map: Map = o.map;
436 437 438
  if (IsFastAliasedArgumentsMap(map) || IsSloppyArgumentsMap(map) ||
      IsSlowAliasedArgumentsMap(map)) {
    return %RawDownCast<JSSloppyArgumentsObject>(o);
Tobias Tebbi's avatar
Tobias Tebbi committed
439
  }
440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
  goto CastError;
}

Cast<JSStrictArgumentsObject>(implicit context: Context)(o: HeapObject):
    JSStrictArgumentsObject
    labels CastError {
  const map: Map = o.map;
  if (!IsStrictArgumentsMap(map)) goto CastError;
  return %RawDownCast<JSStrictArgumentsObject>(o);
}

Cast<JSArgumentsObjectWithLength>(implicit context: Context)(o: HeapObject):
    JSArgumentsObjectWithLength
    labels CastError {
  typeswitch (o) {
    case (o: JSStrictArgumentsObject): {
      return o;
    }
    case (o: JSSloppyArgumentsObject): {
      return o;
    }
    case (HeapObject): {
      goto CastError;
    }
Tobias Tebbi's avatar
Tobias Tebbi committed
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
  }
}

Cast<FastJSRegExp>(implicit context: Context)(o: HeapObject): FastJSRegExp
    labels CastError {
  // TODO(jgruber): Remove or redesign this. There is no single 'fast' regexp,
  // the conditions to make a regexp object fast differ based on the callsite.
  // For now, run the strict variant since replace (the only current callsite)
  // accesses flag getters.
  if (regexp::IsFastRegExpStrict(o)) {
    return %RawDownCast<FastJSRegExp>(o);
  }
  goto CastError;
}

Cast<FastJSArray>(implicit context: Context)(o: HeapObject): FastJSArray
    labels CastError {
  if (IsForceSlowPath()) goto CastError;

483
  if (!Is<JSArray>(o)) goto CastError;
Tobias Tebbi's avatar
Tobias Tebbi committed
484 485

  // Bailout if receiver has slow elements.
486
  const map: Map = o.map;
Tobias Tebbi's avatar
Tobias Tebbi committed
487 488 489 490 491 492 493 494 495 496 497 498 499
  const elementsKind: ElementsKind = LoadMapElementsKind(map);
  if (!IsFastElementsKind(elementsKind)) goto CastError;

  // Verify that our prototype is the initial array prototype.
  if (!IsPrototypeInitialArrayPrototype(map)) goto CastError;

  if (IsNoElementsProtectorCellInvalid()) goto CastError;
  return %RawDownCast<FastJSArray>(o);
}

Cast<FastJSArrayForRead>(implicit context: Context)(o: HeapObject):
    FastJSArrayForRead
    labels CastError {
500
  if (!Is<JSArray>(o)) goto CastError;
Tobias Tebbi's avatar
Tobias Tebbi committed
501 502

  // Bailout if receiver has slow elements.
503
  const map: Map = o.map;
Tobias Tebbi's avatar
Tobias Tebbi committed
504 505
  const elementsKind: ElementsKind = LoadMapElementsKind(map);
  if (!IsElementsKindLessThanOrEqual(
506
          elementsKind, ElementsKind::LAST_ANY_NONEXTENSIBLE_ELEMENTS_KIND))
Tobias Tebbi's avatar
Tobias Tebbi committed
507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539
    goto CastError;

  // Verify that our prototype is the initial array prototype.
  if (!IsPrototypeInitialArrayPrototype(map)) goto CastError;

  if (IsNoElementsProtectorCellInvalid()) goto CastError;
  return %RawDownCast<FastJSArrayForRead>(o);
}

Cast<FastJSArrayForCopy>(implicit context: Context)(o: HeapObject):
    FastJSArrayForCopy
    labels CastError {
  if (IsArraySpeciesProtectorCellInvalid()) goto CastError;
  const a = Cast<FastJSArray>(o) otherwise CastError;
  return %RawDownCast<FastJSArrayForCopy>(a);
}

Cast<FastJSArrayWithNoCustomIteration>(implicit context: Context)(
    o: HeapObject): FastJSArrayWithNoCustomIteration
    labels CastError {
  if (IsArrayIteratorProtectorCellInvalid()) goto CastError;
  const a = Cast<FastJSArray>(o) otherwise CastError;
  return %RawDownCast<FastJSArrayWithNoCustomIteration>(a);
}

Cast<FastJSArrayForReadWithNoCustomIteration>(implicit context: Context)(
    o: HeapObject): FastJSArrayForReadWithNoCustomIteration
    labels CastError {
  if (IsArrayIteratorProtectorCellInvalid()) goto CastError;
  const a = Cast<FastJSArrayForRead>(o) otherwise CastError;
  return %RawDownCast<FastJSArrayForReadWithNoCustomIteration>(a);
}

540 541 542 543 544
Cast<SeqOneByteString>(o: HeapObject): SeqOneByteString labels CastError {
  if (!IsSeqOneByteString(o)) goto CastError;
  return %RawDownCast<SeqOneByteString>(o);
}

Tobias Tebbi's avatar
Tobias Tebbi committed
545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
Cast<JSReceiver|Null>(o: HeapObject): JSReceiver|Null
    labels CastError {
  typeswitch (o) {
    case (o: Null): {
      return o;
    }
    case (o: JSReceiver): {
      return o;
    }
    case (HeapObject): {
      goto CastError;
    }
  }
}

Cast<Smi|PromiseReaction>(o: Object): Smi|PromiseReaction labels CastError {
  typeswitch (o) {
    case (o: Smi): {
      return o;
    }
    case (o: PromiseReaction): {
      return o;
    }
    case (Object): {
      goto CastError;
    }
  }
}

Cast<String|Callable>(implicit context: Context)(o: Object): String|
    Callable labels CastError {
  typeswitch (o) {
    case (o: String): {
      return o;
    }
    case (o: Callable): {
      return o;
    }
    case (Object): {
      goto CastError;
    }
  }
}

Cast<Zero|PromiseReaction>(implicit context: Context)(o: Object): Zero|
    PromiseReaction labels CastError {
  typeswitch (o) {
    case (o: Zero): {
      return o;
    }
    case (o: PromiseReaction): {
      return o;
    }
    case (Object): {
      goto CastError;
    }
  }
}

604 605 606 607 608 609 610 611 612 613 614 615 616 617 618
Cast<JSFunction|JSBoundFunction>(implicit context: Context)(o: Object):
    JSFunction|JSBoundFunction labels CastError {
  typeswitch (o) {
    case (o: JSFunction): {
      return o;
    }
    case (o: JSBoundFunction): {
      return o;
    }
    case (Object): {
      goto CastError;
    }
  }
}

619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647
Cast<FixedArray|Undefined>(o: HeapObject): FixedArray|
    Undefined labels CastError {
  typeswitch (o) {
    case (o: Undefined): {
      return o;
    }
    case (o: FixedArray): {
      return o;
    }
    case (Object): {
      goto CastError;
    }
  }
}

Cast<JSProxy|Null>(o: HeapObject): JSProxy|Null labels CastError {
  typeswitch (o) {
    case (o: Null): {
      return o;
    }
    case (o: JSProxy): {
      return o;
    }
    case (Object): {
      goto CastError;
    }
  }
}

648 649 650 651
macro Is<A : type extends Object, B : type extends Object>(
    implicit context: Context)(o: B): bool {
  Cast<A>(o) otherwise return false;
  return true;
Tobias Tebbi's avatar
Tobias Tebbi committed
652 653
}

654 655 656 657
macro UnsafeCast<A : type extends Object>(implicit context: Context)(o: Object):
    A {
  assert(Is<A>(o));
  return %RawDownCast<A>(o);
Tobias Tebbi's avatar
Tobias Tebbi committed
658
}
659

660 661
macro UnsafeConstCast<T: type>(r: const &T):&T {
  return %RawDownCast<&T>(r);
662 663
}

664 665 666 667 668
UnsafeCast<RegExpMatchInfo>(implicit context: Context)(o: Object):
    RegExpMatchInfo {
  assert(Is<FixedArray>(o));
  return %RawDownCast<RegExpMatchInfo>(o);
}
669 670 671 672 673

macro CastOrDefault<T: type, Arg: type, Default: type>(
    implicit context: Context)(x: Arg, default: Default): T|Default {
  return Cast<T>(x) otherwise return default;
}