@@ -376,6 +376,82 @@ static Error getAddrSpace(StringRef R, unsigned &AddrSpace) {
376
376
return Error::success ();
377
377
}
378
378
379
+ Error DataLayout::parsePrimitiveSpec (StringRef Spec) {
380
+ // [ifv]<size>:<abi>[:<pref>]
381
+ SmallVector<StringRef, 3 > Components;
382
+ char Specifier = Spec.front ();
383
+ assert (Specifier == ' i' || Specifier == ' f' || Specifier == ' v' );
384
+ Spec.drop_front ().split (Components, ' :' );
385
+
386
+ if (Components.size () < 2 || Components.size () > 3 )
387
+ return createSpecFormatError (Twine (Specifier) + " <size>:<abi>[:<pref>]" );
388
+
389
+ // Size. Required, cannot be zero.
390
+ unsigned BitWidth;
391
+ if (Error Err = parseSize (Components[0 ], BitWidth))
392
+ return Err;
393
+
394
+ // ABI alignment.
395
+ Align ABIAlign;
396
+ if (Error Err = parseAlignment (Components[1 ], ABIAlign, " ABI" ))
397
+ return Err;
398
+
399
+ if (Specifier == ' i' && BitWidth == 8 && ABIAlign != 1 )
400
+ return createStringError (" i8 must be 8-bit aligned" );
401
+
402
+ // Preferred alignment. Optional, defaults to the ABI alignment.
403
+ Align PrefAlign = ABIAlign;
404
+ if (Components.size () > 2 )
405
+ if (Error Err = parseAlignment (Components[2 ], PrefAlign, " preferred" ))
406
+ return Err;
407
+
408
+ if (PrefAlign < ABIAlign)
409
+ return createStringError (
410
+ " preferred alignment cannot be less than the ABI alignment" );
411
+
412
+ setPrimitiveSpec (Specifier, BitWidth, ABIAlign, PrefAlign);
413
+ return Error::success ();
414
+ }
415
+
416
+ Error DataLayout::parseAggregateSpec (StringRef Spec) {
417
+ // a<size>:<abi>[:<pref>]
418
+ SmallVector<StringRef, 3 > Components;
419
+ assert (Spec.front () == ' a' );
420
+ Spec.drop_front ().split (Components, ' :' );
421
+
422
+ if (Components.size () < 2 || Components.size () > 3 )
423
+ return createSpecFormatError (" a:<abi>[:<pref>]" );
424
+
425
+ // According to LangRef, <size> component must be absent altogether.
426
+ // For backward compatibility, allow it to be specified, but require
427
+ // it to be zero.
428
+ if (!Components[0 ].empty ()) {
429
+ unsigned BitWidth;
430
+ if (!to_integer (Components[0 ], BitWidth, 10 ) || BitWidth != 0 )
431
+ return createStringError (" size must be zero" );
432
+ }
433
+
434
+ // ABI alignment. Required. Can be zero, meaning use one byte alignment.
435
+ Align ABIAlign;
436
+ if (Error Err =
437
+ parseAlignment (Components[1 ], ABIAlign, " ABI" , /* AllowZero=*/ true ))
438
+ return Err;
439
+
440
+ // Preferred alignment. Optional, defaults to the ABI alignment.
441
+ Align PrefAlign = ABIAlign;
442
+ if (Components.size () > 2 )
443
+ if (Error Err = parseAlignment (Components[2 ], PrefAlign, " preferred" ))
444
+ return Err;
445
+
446
+ if (PrefAlign < ABIAlign)
447
+ return createStringError (
448
+ " preferred alignment cannot be less than the ABI alignment" );
449
+
450
+ StructABIAlignment = ABIAlign;
451
+ StructPrefAlignment = PrefAlign;
452
+ return Error::success ();
453
+ }
454
+
379
455
Error DataLayout::parsePointerSpec (StringRef Spec) {
380
456
// p[<n>]:<size>:<abi>[:<pref>[:<idx>]]
381
457
SmallVector<StringRef, 5 > Components;
@@ -451,6 +527,12 @@ Error DataLayout::parseSpecification(StringRef Spec) {
451
527
assert (!Spec.empty () && " Empty specification is handled by the caller" );
452
528
char Specifier = Spec.front ();
453
529
530
+ if (Specifier == ' i' || Specifier == ' f' || Specifier == ' v' )
531
+ return parsePrimitiveSpec (Spec);
532
+
533
+ if (Specifier == ' a' )
534
+ return parseAggregateSpec (Spec);
535
+
454
536
if (Specifier == ' p' )
455
537
return parsePointerSpec (Spec);
456
538
@@ -477,78 +559,6 @@ Error DataLayout::parseSpecification(StringRef Spec) {
477
559
case ' e' :
478
560
BigEndian = false ;
479
561
break ;
480
- case ' i' :
481
- case ' v' :
482
- case ' f' :
483
- case ' a' : {
484
- TypeSpecifier Specifier;
485
- switch (SpecifierChar) {
486
- default :
487
- llvm_unreachable (" Unexpected specifier!" );
488
- case ' i' :
489
- Specifier = TypeSpecifier::Integer;
490
- break ;
491
- case ' v' :
492
- Specifier = TypeSpecifier::Vector;
493
- break ;
494
- case ' f' :
495
- Specifier = TypeSpecifier::Float;
496
- break ;
497
- case ' a' :
498
- Specifier = TypeSpecifier::Aggregate;
499
- break ;
500
- }
501
-
502
- // Bit size.
503
- unsigned Size = 0 ;
504
- if (!Tok.empty ())
505
- if (Error Err = getInt (Tok, Size))
506
- return Err;
507
-
508
- if (Specifier == TypeSpecifier::Aggregate && Size != 0 )
509
- return reportError (" Sized aggregate specification in datalayout string" );
510
-
511
- // ABI alignment.
512
- if (Rest.empty ())
513
- return reportError (
514
- " Missing alignment specification in datalayout string" );
515
- if (Error Err = ::split (Rest, ' :' , Split))
516
- return Err;
517
- unsigned ABIAlign;
518
- if (Error Err = getIntInBytes (Tok, ABIAlign))
519
- return Err;
520
- if (Specifier != TypeSpecifier::Aggregate && !ABIAlign)
521
- return reportError (
522
- " ABI alignment specification must be >0 for non-aggregate types" );
523
-
524
- if (!isUInt<16 >(ABIAlign))
525
- return reportError (" Invalid ABI alignment, must be a 16bit integer" );
526
- if (ABIAlign != 0 && !isPowerOf2_64 (ABIAlign))
527
- return reportError (" Invalid ABI alignment, must be a power of 2" );
528
- if (Specifier == TypeSpecifier::Integer && Size == 8 && ABIAlign != 1 )
529
- return reportError (" Invalid ABI alignment, i8 must be naturally aligned" );
530
-
531
- // Preferred alignment.
532
- unsigned PrefAlign = ABIAlign;
533
- if (!Rest.empty ()) {
534
- if (Error Err = ::split (Rest, ' :' , Split))
535
- return Err;
536
- if (Error Err = getIntInBytes (Tok, PrefAlign))
537
- return Err;
538
- }
539
-
540
- if (!isUInt<16 >(PrefAlign))
541
- return reportError (
542
- " Invalid preferred alignment, must be a 16bit integer" );
543
- if (PrefAlign != 0 && !isPowerOf2_64 (PrefAlign))
544
- return reportError (" Invalid preferred alignment, must be a power of 2" );
545
-
546
- if (Error Err = setPrimitiveSpec (Specifier, Size, assumeAligned (ABIAlign),
547
- assumeAligned (PrefAlign)))
548
- return Err;
549
-
550
- break ;
551
- }
552
562
case ' n' : // Native integer types.
553
563
while (true ) {
554
564
unsigned Width;
@@ -668,32 +678,19 @@ Error DataLayout::parseLayoutString(StringRef LayoutString) {
668
678
return Error::success ();
669
679
}
670
680
671
- Error DataLayout::setPrimitiveSpec (TypeSpecifier Specifier, uint32_t BitWidth,
672
- Align ABIAlign, Align PrefAlign) {
673
- // AlignmentsTy::ABIAlign and AlignmentsTy::PrefAlign were once stored as
674
- // uint16_t, it is unclear if there are requirements for alignment to be less
675
- // than 2^16 other than storage. In the meantime we leave the restriction as
676
- // an assert. See D67400 for context.
677
- assert (Log2 (ABIAlign) < 16 && Log2 (PrefAlign) < 16 && " Alignment too big" );
678
- if (!isUInt<24 >(BitWidth))
679
- return reportError (" Invalid bit width, must be a 24-bit integer" );
680
- if (PrefAlign < ABIAlign)
681
- return reportError (
682
- " Preferred alignment cannot be less than the ABI alignment" );
683
-
681
+ void DataLayout::setPrimitiveSpec (char Specifier, uint32_t BitWidth,
682
+ Align ABIAlign, Align PrefAlign) {
684
683
SmallVectorImpl<PrimitiveSpec> *Specs;
685
684
switch (Specifier) {
686
- case TypeSpecifier::Aggregate:
687
- StructABIAlignment = ABIAlign;
688
- StructPrefAlignment = PrefAlign;
689
- return Error::success ();
690
- case TypeSpecifier::Integer:
685
+ default :
686
+ llvm_unreachable (" Unexpected specifier" );
687
+ case ' i' :
691
688
Specs = &IntSpecs;
692
689
break ;
693
- case TypeSpecifier::Float :
690
+ case ' f ' :
694
691
Specs = &FloatSpecs;
695
692
break ;
696
- case TypeSpecifier::Vector :
693
+ case ' v ' :
697
694
Specs = &VectorSpecs;
698
695
break ;
699
696
}
@@ -707,7 +704,6 @@ Error DataLayout::setPrimitiveSpec(TypeSpecifier Specifier, uint32_t BitWidth,
707
704
// Insert before I to keep the vector sorted.
708
705
Specs->insert (I, PrimitiveSpec{BitWidth, ABIAlign, PrefAlign});
709
706
}
710
- return Error::success ();
711
707
}
712
708
713
709
const DataLayout::PointerSpec &
0 commit comments