@@ -358,8 +358,8 @@ class X86MCCodeEmitter : public MCCodeEmitter {
358358
359359 unsigned getX86RegEncoding (const MCInst &MI, unsigned OpNum) const ;
360360
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,
363363 SmallVectorImpl<char > &CB,
364364 SmallVectorImpl<MCFixup> &Fixups, int ImmOffset = 0 ) const ;
365365
@@ -441,8 +441,6 @@ static bool isDispOrCDisp8(uint64_t TSFlags, int Value, int &ImmOffset) {
441441// / instruction with the specified TSFlags.
442442static MCFixupKind getImmFixupKind (uint64_t TSFlags) {
443443 unsigned Size = X86II::getSizeOfImm (TSFlags);
444- bool isPCRel = X86II::isImmPCRel (TSFlags);
445-
446444 if (X86II::isImmSigned (TSFlags)) {
447445 switch (Size) {
448446 default :
@@ -455,13 +453,13 @@ static MCFixupKind getImmFixupKind(uint64_t TSFlags) {
455453 default :
456454 llvm_unreachable (" Invalid generic fixup size!" );
457455 case 1 :
458- return isPCRel ? FK_PCRel_1 : FK_Data_1;
456+ return FK_Data_1;
459457 case 2 :
460- return isPCRel ? FK_PCRel_2 : FK_Data_2;
458+ return FK_Data_2;
461459 case 4 :
462- return isPCRel ? FK_PCRel_4 : FK_Data_4;
460+ return FK_Data_4;
463461 case 8 :
464- return isPCRel ? FK_PCRel_8 : FK_Data_8;
462+ return FK_Data_8;
465463 }
466464}
467465
@@ -506,7 +504,8 @@ static bool isPCRel32Branch(const MCInst &MI, const MCInstrInfo &MCII) {
506504 const MCInstrDesc &Desc = MCII.get (Opcode);
507505 if ((Opcode != X86::CALL64pcrel32 && Opcode != X86::JMP_4 &&
508506 Opcode != X86::JCC_4) ||
509- getImmFixupKind (Desc.TSFlags ) != FK_PCRel_4)
507+ !(getImmFixupKind (Desc.TSFlags ) == FK_Data_4 &&
508+ X86II::isImmPCRel (Desc.TSFlags )))
510509 return false ;
511510
512511 unsigned CurOp = X86II::getOperandBias (Desc);
@@ -528,17 +527,29 @@ unsigned X86MCCodeEmitter::getX86RegEncoding(const MCInst &MI,
528527}
529528
530529void X86MCCodeEmitter::emitImmediate (const MCOperand &DispOp, SMLoc Loc,
531- unsigned Size, unsigned FixupKind ,
530+ unsigned FixupKind, bool PCRel ,
532531 uint64_t StartByte,
533532 SmallVectorImpl<char > &CB,
534533 SmallVectorImpl<MCFixup> &Fixups,
535534 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+ }
536547 const MCExpr *Expr = nullptr ;
537548 if (DispOp.isImm ()) {
538549 // If this is a simple integer displacement that doesn't require a
539550 // 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) ) {
542553 emitConstant (DispOp.getImm () + ImmOffset, Size, CB);
543554 return ;
544555 }
@@ -578,33 +589,13 @@ void X86MCCodeEmitter::emitImmediate(const MCOperand &DispOp, SMLoc Loc,
578589
579590 // If the fixup is pc-relative, we need to bias the value to be relative to
580591 // 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;
602594 // If this is a pc-relative load off _GLOBAL_OFFSET_TABLE_:
603595 // leaq _GLOBAL_OFFSET_TABLE_(%rip), %r15
604596 // this needs to be a GOTPC32 relocation.
605- if (startsWithGlobalOffsetTable (Expr) != GOT_None)
597+ if (Size == 4 && startsWithGlobalOffsetTable (Expr) != GOT_None)
606598 FixupKind = X86::reloc_global_offset_table;
607- break ;
608599 }
609600
610601 if (ImmOffset)
@@ -712,8 +703,8 @@ void X86MCCodeEmitter::emitMemModRMByte(
712703 ? X86II::getSizeOfImm (TSFlags)
713704 : 0 ;
714705
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);
717708 return ;
718709 }
719710
@@ -767,7 +758,8 @@ void X86MCCodeEmitter::emitMemModRMByte(
767758 }
768759 // Use the [REG]+disp8 form, including for [BP] which cannot be encoded.
769760 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);
771763 return ;
772764 }
773765 // This is the [REG]+disp16 case.
@@ -779,7 +771,7 @@ void X86MCCodeEmitter::emitMemModRMByte(
779771 }
780772
781773 // 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);
783775 return ;
784776 }
785777
@@ -797,7 +789,7 @@ void X86MCCodeEmitter::emitMemModRMByte(
797789 STI.hasFeature (X86::Is64Bit))) {
798790 if (!BaseReg) { // [disp32] in X86-32 mode
799791 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);
801793 return ;
802794 }
803795
@@ -833,8 +825,8 @@ void X86MCCodeEmitter::emitMemModRMByte(
833825 int ImmOffset = 0 ;
834826 if (isDispOrCDisp8 (TSFlags, Disp.getImm (), ImmOffset)) {
835827 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);
838830 return ;
839831 }
840832 }
@@ -846,8 +838,8 @@ void X86MCCodeEmitter::emitMemModRMByte(
846838 unsigned Opcode = MI.getOpcode ();
847839 unsigned FixupKind = Opcode == X86::MOV32rm ? X86::reloc_signed_4byte_relax
848840 : 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);
851843 return ;
852844 }
853845
@@ -895,11 +887,11 @@ void X86MCCodeEmitter::emitMemModRMByte(
895887
896888 // Do we need to output a displacement?
897889 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,
899891 ImmOffset);
900892 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);
903895}
904896
905897// / Emit all instruction prefixes.
@@ -1634,33 +1626,29 @@ void X86MCCodeEmitter::encodeInstruction(const MCInst &MI,
16341626 break ;
16351627
16361628 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);
16401631 break ;
16411632 }
16421633 case X86II::RawFrmMemOffs:
16431634 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);
16471637 ++CurOp; // skip segment operand
16481638 break ;
16491639 case X86II::RawFrmImm8:
16501640 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 ,
16531644 StartByte, CB, Fixups);
1654- emitImmediate (MI.getOperand (CurOp++), MI.getLoc (), 1 , FK_Data_1, StartByte,
1655- CB, Fixups);
16561645 break ;
16571646 case X86II::RawFrmImm16:
16581647 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 ,
16611651 StartByte, CB, Fixups);
1662- emitImmediate (MI.getOperand (CurOp++), MI.getLoc (), 2 , FK_Data_2, StartByte,
1663- CB, Fixups);
16641652 break ;
16651653
16661654 case X86II::AddRegFrm:
@@ -2013,7 +2001,7 @@ void X86MCCodeEmitter::encodeInstruction(const MCInst &MI,
20132001 assert (Val < 16 && " Immediate operand value out of range" );
20142002 I8RegNum |= Val;
20152003 }
2016- emitImmediate (MCOperand::createImm (I8RegNum), MI.getLoc (), 1 , FK_Data_1 ,
2004+ emitImmediate (MCOperand::createImm (I8RegNum), MI.getLoc (), FK_Data_1, false ,
20172005 StartByte, CB, Fixups);
20182006 } else {
20192007 // If there is a remaining operand, it must be a trailing immediate. Emit it
@@ -2024,7 +2012,7 @@ void X86MCCodeEmitter::encodeInstruction(const MCInst &MI,
20242012 unsigned RemaningOps = NumOps - CurOp - 2 * HasTwoConditionalOps;
20252013 while (RemaningOps) {
20262014 emitImmediate (MI.getOperand (CurOp++), MI.getLoc (),
2027- X86II::getSizeOfImm (TSFlags), getImmFixupKind (TSFlags),
2015+ getImmFixupKind (TSFlags), X86II::isImmPCRel (TSFlags),
20282016 StartByte, CB, Fixups);
20292017 --RemaningOps;
20302018 }
0 commit comments