@@ -358,8 +358,8 @@ class X86MCCodeEmitter : public MCCodeEmitter {
358
358
359
359
unsigned getX86RegEncoding (const MCInst &MI, unsigned OpNum) const ;
360
360
361
- void emitImmediate (const MCOperand &Disp, SMLoc Loc, unsigned ImmSize ,
362
- unsigned FixupKind , uint64_t StartByte,
361
+ void emitImmediate (const MCOperand &Disp, SMLoc Loc, unsigned FixupKind ,
362
+ bool IsPCRel , uint64_t StartByte,
363
363
SmallVectorImpl<char > &CB,
364
364
SmallVectorImpl<MCFixup> &Fixups, int ImmOffset = 0 ) const ;
365
365
@@ -441,8 +441,6 @@ static bool isDispOrCDisp8(uint64_t TSFlags, int Value, int &ImmOffset) {
441
441
// / instruction with the specified TSFlags.
442
442
static MCFixupKind getImmFixupKind (uint64_t TSFlags) {
443
443
unsigned Size = X86II::getSizeOfImm (TSFlags);
444
- bool isPCRel = X86II::isImmPCRel (TSFlags);
445
-
446
444
if (X86II::isImmSigned (TSFlags)) {
447
445
switch (Size) {
448
446
default :
@@ -455,13 +453,13 @@ static MCFixupKind getImmFixupKind(uint64_t TSFlags) {
455
453
default :
456
454
llvm_unreachable (" Invalid generic fixup size!" );
457
455
case 1 :
458
- return isPCRel ? FK_PCRel_1 : FK_Data_1;
456
+ return FK_Data_1;
459
457
case 2 :
460
- return isPCRel ? FK_PCRel_2 : FK_Data_2;
458
+ return FK_Data_2;
461
459
case 4 :
462
- return isPCRel ? FK_PCRel_4 : FK_Data_4;
460
+ return FK_Data_4;
463
461
case 8 :
464
- return isPCRel ? FK_PCRel_8 : FK_Data_8;
462
+ return FK_Data_8;
465
463
}
466
464
}
467
465
@@ -506,7 +504,8 @@ static bool isPCRel32Branch(const MCInst &MI, const MCInstrInfo &MCII) {
506
504
const MCInstrDesc &Desc = MCII.get (Opcode);
507
505
if ((Opcode != X86::CALL64pcrel32 && Opcode != X86::JMP_4 &&
508
506
Opcode != X86::JCC_4) ||
509
- getImmFixupKind (Desc.TSFlags ) != FK_PCRel_4)
507
+ !(getImmFixupKind (Desc.TSFlags ) == FK_Data_4 &&
508
+ X86II::isImmPCRel (Desc.TSFlags )))
510
509
return false ;
511
510
512
511
unsigned CurOp = X86II::getOperandBias (Desc);
@@ -528,17 +527,29 @@ unsigned X86MCCodeEmitter::getX86RegEncoding(const MCInst &MI,
528
527
}
529
528
530
529
void X86MCCodeEmitter::emitImmediate (const MCOperand &DispOp, SMLoc Loc,
531
- unsigned Size, unsigned FixupKind ,
530
+ unsigned FixupKind, bool PCRel ,
532
531
uint64_t StartByte,
533
532
SmallVectorImpl<char > &CB,
534
533
SmallVectorImpl<MCFixup> &Fixups,
535
534
int ImmOffset) const {
535
+ unsigned Size = 4 ;
536
+ switch (FixupKind) {
537
+ case FK_Data_1:
538
+ Size = 1 ;
539
+ break ;
540
+ case FK_Data_2:
541
+ Size = 2 ;
542
+ break ;
543
+ case FK_Data_8:
544
+ Size = 8 ;
545
+ break ;
546
+ }
536
547
const MCExpr *Expr = nullptr ;
537
548
if (DispOp.isImm ()) {
538
549
// If this is a simple integer displacement that doesn't require a
539
550
// relocation, emit it now.
540
- if (FixupKind != FK_PCRel_1 && FixupKind != FK_PCRel_2 &&
541
- FixupKind != FK_PCRel_4 ) {
551
+ if (!( is_contained ({FK_Data_1, FK_Data_2, FK_Data_4}, FixupKind) &&
552
+ PCRel) ) {
542
553
emitConstant (DispOp.getImm () + ImmOffset, Size, CB);
543
554
return ;
544
555
}
@@ -578,33 +589,13 @@ void X86MCCodeEmitter::emitImmediate(const MCOperand &DispOp, SMLoc Loc,
578
589
579
590
// If the fixup is pc-relative, we need to bias the value to be relative to
580
591
// the start of the field, not the end of the field.
581
- bool PCRel = false ;
582
- switch (FixupKind) {
583
- case FK_PCRel_1:
584
- PCRel = true ;
585
- ImmOffset -= 1 ;
586
- break ;
587
- case FK_PCRel_2:
588
- PCRel = true ;
589
- ImmOffset -= 2 ;
590
- break ;
591
- case FK_PCRel_4:
592
- case X86::reloc_riprel_4byte:
593
- case X86::reloc_riprel_4byte_movq_load:
594
- case X86::reloc_riprel_4byte_movq_load_rex2:
595
- case X86::reloc_riprel_4byte_relax:
596
- case X86::reloc_riprel_4byte_relax_rex:
597
- case X86::reloc_riprel_4byte_relax_rex2:
598
- case X86::reloc_branch_4byte_pcrel:
599
- case X86::reloc_riprel_4byte_relax_evex:
600
- PCRel = true ;
601
- ImmOffset -= 4 ;
592
+ if (PCRel) {
593
+ ImmOffset -= Size;
602
594
// If this is a pc-relative load off _GLOBAL_OFFSET_TABLE_:
603
595
// leaq _GLOBAL_OFFSET_TABLE_(%rip), %r15
604
596
// this needs to be a GOTPC32 relocation.
605
- if (startsWithGlobalOffsetTable (Expr) != GOT_None)
597
+ if (Size == 4 && startsWithGlobalOffsetTable (Expr) != GOT_None)
606
598
FixupKind = X86::reloc_global_offset_table;
607
- break ;
608
599
}
609
600
610
601
if (ImmOffset)
@@ -712,8 +703,8 @@ void X86MCCodeEmitter::emitMemModRMByte(
712
703
? X86II::getSizeOfImm (TSFlags)
713
704
: 0 ;
714
705
715
- emitImmediate (Disp, MI.getLoc (), 4 , MCFixupKind (FixupKind) , StartByte, CB,
716
- Fixups, -ImmSize);
706
+ emitImmediate (Disp, MI.getLoc (), FixupKind, true , StartByte, CB, Fixups ,
707
+ -ImmSize);
717
708
return ;
718
709
}
719
710
@@ -767,7 +758,8 @@ void X86MCCodeEmitter::emitMemModRMByte(
767
758
}
768
759
// Use the [REG]+disp8 form, including for [BP] which cannot be encoded.
769
760
emitByte (modRMByte (1 , RegOpcodeField, RMfield), CB);
770
- emitImmediate (Disp, MI.getLoc (), 1 , FK_Data_1, StartByte, CB, Fixups);
761
+ emitImmediate (Disp, MI.getLoc (), FK_Data_1, false , StartByte, CB,
762
+ Fixups);
771
763
return ;
772
764
}
773
765
// This is the [REG]+disp16 case.
@@ -779,7 +771,7 @@ void X86MCCodeEmitter::emitMemModRMByte(
779
771
}
780
772
781
773
// Emit 16-bit displacement for plain disp16 or [REG]+disp16 cases.
782
- emitImmediate (Disp, MI.getLoc (), 2 , FK_Data_2 , StartByte, CB, Fixups);
774
+ emitImmediate (Disp, MI.getLoc (), FK_Data_2, false , StartByte, CB, Fixups);
783
775
return ;
784
776
}
785
777
@@ -797,7 +789,7 @@ void X86MCCodeEmitter::emitMemModRMByte(
797
789
STI.hasFeature (X86::Is64Bit))) {
798
790
if (!BaseReg) { // [disp32] in X86-32 mode
799
791
emitByte (modRMByte (0 , RegOpcodeField, 5 ), CB);
800
- emitImmediate (Disp, MI.getLoc (), 4 , FK_Data_4 , StartByte, CB, Fixups);
792
+ emitImmediate (Disp, MI.getLoc (), FK_Data_4, false , StartByte, CB, Fixups);
801
793
return ;
802
794
}
803
795
@@ -833,8 +825,8 @@ void X86MCCodeEmitter::emitMemModRMByte(
833
825
int ImmOffset = 0 ;
834
826
if (isDispOrCDisp8 (TSFlags, Disp.getImm (), ImmOffset)) {
835
827
emitByte (modRMByte (1 , RegOpcodeField, BaseRegNo), CB);
836
- emitImmediate (Disp, MI.getLoc (), 1 , FK_Data_1 , StartByte, CB, Fixups ,
837
- ImmOffset);
828
+ emitImmediate (Disp, MI.getLoc (), FK_Data_1, false , StartByte, CB,
829
+ Fixups, ImmOffset);
838
830
return ;
839
831
}
840
832
}
@@ -846,8 +838,8 @@ void X86MCCodeEmitter::emitMemModRMByte(
846
838
unsigned Opcode = MI.getOpcode ();
847
839
unsigned FixupKind = Opcode == X86::MOV32rm ? X86::reloc_signed_4byte_relax
848
840
: X86::reloc_signed_4byte;
849
- emitImmediate (Disp, MI.getLoc (), 4 , MCFixupKind (FixupKind), StartByte, CB ,
850
- Fixups);
841
+ emitImmediate (Disp, MI.getLoc (), MCFixupKind (FixupKind), false , StartByte ,
842
+ CB, Fixups);
851
843
return ;
852
844
}
853
845
@@ -895,11 +887,11 @@ void X86MCCodeEmitter::emitMemModRMByte(
895
887
896
888
// Do we need to output a displacement?
897
889
if (ForceDisp8)
898
- emitImmediate (Disp, MI.getLoc (), 1 , FK_Data_1 , StartByte, CB, Fixups,
890
+ emitImmediate (Disp, MI.getLoc (), FK_Data_1, false , StartByte, CB, Fixups,
899
891
ImmOffset);
900
892
else if (ForceDisp32)
901
- emitImmediate (Disp, MI.getLoc (), 4 , MCFixupKind (X86::reloc_signed_4byte),
902
- StartByte, CB, Fixups);
893
+ emitImmediate (Disp, MI.getLoc (), MCFixupKind (X86::reloc_signed_4byte),
894
+ false , StartByte, CB, Fixups);
903
895
}
904
896
905
897
// / Emit all instruction prefixes.
@@ -1634,33 +1626,29 @@ void X86MCCodeEmitter::encodeInstruction(const MCInst &MI,
1634
1626
break ;
1635
1627
1636
1628
const MCOperand &Op = MI.getOperand (CurOp++);
1637
- emitImmediate (Op, MI.getLoc (), X86II::getSizeOfImm (TSFlags),
1638
- MCFixupKind (X86::reloc_branch_4byte_pcrel), StartByte, CB,
1639
- Fixups);
1629
+ emitImmediate (Op, MI.getLoc (), MCFixupKind (X86::reloc_branch_4byte_pcrel),
1630
+ true , StartByte, CB, Fixups);
1640
1631
break ;
1641
1632
}
1642
1633
case X86II::RawFrmMemOffs:
1643
1634
emitByte (BaseOpcode, CB);
1644
- emitImmediate (MI.getOperand (CurOp++), MI.getLoc (),
1645
- X86II::getSizeOfImm (TSFlags), getImmFixupKind (TSFlags),
1646
- StartByte, CB, Fixups);
1635
+ emitImmediate (MI.getOperand (CurOp++), MI.getLoc (), getImmFixupKind (TSFlags),
1636
+ X86II::isImmPCRel (TSFlags), StartByte, CB, Fixups);
1647
1637
++CurOp; // skip segment operand
1648
1638
break ;
1649
1639
case X86II::RawFrmImm8:
1650
1640
emitByte (BaseOpcode, CB);
1651
- emitImmediate (MI.getOperand (CurOp++), MI.getLoc (),
1652
- X86II::getSizeOfImm (TSFlags), getImmFixupKind (TSFlags),
1641
+ emitImmediate (MI.getOperand (CurOp++), MI.getLoc (), getImmFixupKind (TSFlags),
1642
+ X86II::isImmPCRel (TSFlags), StartByte, CB, Fixups);
1643
+ emitImmediate (MI.getOperand (CurOp++), MI.getLoc (), FK_Data_1, false ,
1653
1644
StartByte, CB, Fixups);
1654
- emitImmediate (MI.getOperand (CurOp++), MI.getLoc (), 1 , FK_Data_1, StartByte,
1655
- CB, Fixups);
1656
1645
break ;
1657
1646
case X86II::RawFrmImm16:
1658
1647
emitByte (BaseOpcode, CB);
1659
- emitImmediate (MI.getOperand (CurOp++), MI.getLoc (),
1660
- X86II::getSizeOfImm (TSFlags), getImmFixupKind (TSFlags),
1648
+ emitImmediate (MI.getOperand (CurOp++), MI.getLoc (), getImmFixupKind (TSFlags),
1649
+ X86II::isImmPCRel (TSFlags), StartByte, CB, Fixups);
1650
+ emitImmediate (MI.getOperand (CurOp++), MI.getLoc (), FK_Data_2, false ,
1661
1651
StartByte, CB, Fixups);
1662
- emitImmediate (MI.getOperand (CurOp++), MI.getLoc (), 2 , FK_Data_2, StartByte,
1663
- CB, Fixups);
1664
1652
break ;
1665
1653
1666
1654
case X86II::AddRegFrm:
@@ -2013,7 +2001,7 @@ void X86MCCodeEmitter::encodeInstruction(const MCInst &MI,
2013
2001
assert (Val < 16 && " Immediate operand value out of range" );
2014
2002
I8RegNum |= Val;
2015
2003
}
2016
- emitImmediate (MCOperand::createImm (I8RegNum), MI.getLoc (), 1 , FK_Data_1 ,
2004
+ emitImmediate (MCOperand::createImm (I8RegNum), MI.getLoc (), FK_Data_1, false ,
2017
2005
StartByte, CB, Fixups);
2018
2006
} else {
2019
2007
// If there is a remaining operand, it must be a trailing immediate. Emit it
@@ -2024,7 +2012,7 @@ void X86MCCodeEmitter::encodeInstruction(const MCInst &MI,
2024
2012
unsigned RemaningOps = NumOps - CurOp - 2 * HasTwoConditionalOps;
2025
2013
while (RemaningOps) {
2026
2014
emitImmediate (MI.getOperand (CurOp++), MI.getLoc (),
2027
- X86II::getSizeOfImm (TSFlags), getImmFixupKind (TSFlags),
2015
+ getImmFixupKind (TSFlags), X86II::isImmPCRel (TSFlags),
2028
2016
StartByte, CB, Fixups);
2029
2017
--RemaningOps;
2030
2018
}
0 commit comments