PPCXCOFFStreamer.cpp 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. //===-------- PPCXCOFFStreamer.cpp - XCOFF Object Output ------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This is a custom MCXCOFFStreamer for PowerPC.
  10. //
  11. // The purpose of the custom XCOFF streamer is to allow us to intercept
  12. // instructions as they are being emitted and align all 8 byte instructions
  13. // to a 64 byte boundary if required (by adding a 4 byte nop). This is important
  14. // because 8 byte instructions are not allowed to cross 64 byte boundaries
  15. // and by aligning anything that is within 4 bytes of the boundary we can
  16. // guarantee that the 8 byte instructions do not cross that boundary.
  17. //
  18. //===----------------------------------------------------------------------===//
  19. #include "PPCXCOFFStreamer.h"
  20. #include "PPCMCCodeEmitter.h"
  21. #include "llvm/BinaryFormat/XCOFF.h"
  22. #include "llvm/MC/MCAsmBackend.h"
  23. #include "llvm/MC/MCCodeEmitter.h"
  24. #include "llvm/MC/MCDirectives.h"
  25. #include "llvm/MC/MCObjectWriter.h"
  26. #include "llvm/MC/MCSectionXCOFF.h"
  27. #include "llvm/MC/MCSymbolXCOFF.h"
  28. #include "llvm/MC/TargetRegistry.h"
  29. using namespace llvm;
  30. PPCXCOFFStreamer::PPCXCOFFStreamer(MCContext &Context,
  31. std::unique_ptr<MCAsmBackend> MAB,
  32. std::unique_ptr<MCObjectWriter> OW,
  33. std::unique_ptr<MCCodeEmitter> Emitter)
  34. : MCXCOFFStreamer(Context, std::move(MAB), std::move(OW),
  35. std::move(Emitter)) {}
  36. void PPCXCOFFStreamer::emitPrefixedInstruction(const MCInst &Inst,
  37. const MCSubtargetInfo &STI) {
  38. // Prefixed instructions must not cross a 64-byte boundary (i.e. prefix is
  39. // before the boundary and the remaining 4-bytes are after the boundary). In
  40. // order to achieve this, a nop is added prior to any such boundary-crossing
  41. // prefixed instruction. Align to 64 bytes if possible but add a maximum of 4
  42. // bytes when trying to do that. If alignment requires adding more than 4
  43. // bytes then the instruction won't be aligned.
  44. emitCodeAlignment(64, &STI, 4);
  45. // Emit the instruction.
  46. // Since the previous emit created a new fragment then adding this instruction
  47. // also forces the addition of a new fragment. Inst is now the first
  48. // instruction in that new fragment.
  49. MCXCOFFStreamer::emitInstruction(Inst, STI);
  50. }
  51. void PPCXCOFFStreamer::emitInstruction(const MCInst &Inst,
  52. const MCSubtargetInfo &STI) {
  53. PPCMCCodeEmitter *Emitter =
  54. static_cast<PPCMCCodeEmitter *>(getAssembler().getEmitterPtr());
  55. // Special handling is only for prefixed instructions.
  56. if (!Emitter->isPrefixedInstruction(Inst)) {
  57. MCXCOFFStreamer::emitInstruction(Inst, STI);
  58. return;
  59. }
  60. emitPrefixedInstruction(Inst, STI);
  61. }
  62. MCXCOFFStreamer *
  63. llvm::createPPCXCOFFStreamer(MCContext &Context,
  64. std::unique_ptr<MCAsmBackend> MAB,
  65. std::unique_ptr<MCObjectWriter> OW,
  66. std::unique_ptr<MCCodeEmitter> Emitter) {
  67. return new PPCXCOFFStreamer(Context, std::move(MAB), std::move(OW),
  68. std::move(Emitter));
  69. }