Jan Voung | ec27073 | 2015-01-12 17:00:22 -0800 | [diff] [blame] | 1 | //===- subzero/src/IceFixups.cpp - Implementation of Assembler Fixups -----===// |
| 2 | // |
| 3 | // The Subzero Code Generator |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 9 | /// |
| 10 | /// \file |
Jim Stichnoth | 92a6e5b | 2015-12-02 16:52:44 -0800 | [diff] [blame] | 11 | /// \brief Implements the AssemblerFixup class, a very basic target-independent |
| 12 | /// representation of a fixup or relocation. |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 13 | /// |
Jan Voung | ec27073 | 2015-01-12 17:00:22 -0800 | [diff] [blame] | 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #include "IceFixups.h" |
John Porto | 67f8de9 | 2015-06-25 10:14:17 -0700 | [diff] [blame] | 17 | |
Jan Voung | ec27073 | 2015-01-12 17:00:22 -0800 | [diff] [blame] | 18 | #include "IceOperand.h" |
| 19 | |
| 20 | namespace Ice { |
| 21 | |
Jan Voung | f644a4b | 2015-03-19 11:57:52 -0700 | [diff] [blame] | 22 | const Constant *AssemblerFixup::NullSymbol = nullptr; |
| 23 | |
Jan Voung | ec27073 | 2015-01-12 17:00:22 -0800 | [diff] [blame] | 24 | RelocOffsetT AssemblerFixup::offset() const { |
Jan Voung | f644a4b | 2015-03-19 11:57:52 -0700 | [diff] [blame] | 25 | if (isNullSymbol()) |
John Porto | d1bd1d3 | 2016-01-26 11:44:01 -0800 | [diff] [blame] | 26 | return addend_; |
Jim Stichnoth | 3e32400 | 2016-03-08 16:18:40 -0800 | [diff] [blame] | 27 | if (!ValueIsSymbol) { |
| 28 | if (const auto *CR = llvm::dyn_cast<ConstantRelocatable>(ConstValue)) |
| 29 | return CR->getOffset() + addend_; |
| 30 | } |
John Porto | d1bd1d3 | 2016-01-26 11:44:01 -0800 | [diff] [blame] | 31 | return addend_; |
Jan Voung | ec27073 | 2015-01-12 17:00:22 -0800 | [diff] [blame] | 32 | } |
| 33 | |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 34 | GlobalString AssemblerFixup::symbol() const { |
Jim Stichnoth | 3e32400 | 2016-03-08 16:18:40 -0800 | [diff] [blame] | 35 | assert(!isNullSymbol()); |
| 36 | assert(!ValueIsSymbol); |
| 37 | const Constant *C = ConstValue; |
| 38 | if (const auto *CR = llvm::dyn_cast<ConstantRelocatable>(C)) { |
| 39 | return CR->getName(); |
| 40 | } |
| 41 | // NOTE: currently only float/doubles are put into constant pools. In the |
| 42 | // future we may put integers as well. |
| 43 | assert(llvm::isa<ConstantFloat>(C) || llvm::isa<ConstantDouble>(C)); |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 44 | return C->getLabelName(); |
Jan Voung | ec27073 | 2015-01-12 17:00:22 -0800 | [diff] [blame] | 45 | } |
| 46 | |
Karl Schimpf | e4289e2 | 2015-10-27 15:16:27 -0700 | [diff] [blame] | 47 | size_t AssemblerFixup::emit(GlobalContext *Ctx, const Assembler &Asm) const { |
Karl Schimpf | 2fee2a2 | 2015-10-22 08:19:26 -0700 | [diff] [blame] | 48 | static constexpr const size_t FixupSize = 4; |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 49 | if (!BuildDefs::dump()) |
Karl Schimpf | 2fee2a2 | 2015-10-22 08:19:26 -0700 | [diff] [blame] | 50 | return FixupSize; |
Jan Voung | ec27073 | 2015-01-12 17:00:22 -0800 | [diff] [blame] | 51 | Ostream &Str = Ctx->getStrEmit(); |
Karl Schimpf | 2fee2a2 | 2015-10-22 08:19:26 -0700 | [diff] [blame] | 52 | Str << "\t.long "; |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 53 | std::string Symbol; |
John Porto | 6e8d3fa | 2016-02-04 10:35:20 -0800 | [diff] [blame] | 54 | if (isNullSymbol()) { |
Jim Stichnoth | 927f7cc | 2015-03-19 23:23:00 -0700 | [diff] [blame] | 55 | Str << "__Sz_AbsoluteZero"; |
John Porto | 6e8d3fa | 2016-02-04 10:35:20 -0800 | [diff] [blame] | 56 | } else { |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 57 | Symbol = symbol().toString(); |
John Porto | 6e8d3fa | 2016-02-04 10:35:20 -0800 | [diff] [blame] | 58 | Str << Symbol; |
Jim Stichnoth | 3e32400 | 2016-03-08 16:18:40 -0800 | [diff] [blame] | 59 | assert(!ValueIsSymbol); |
| 60 | if (const auto *CR = llvm::dyn_cast<ConstantRelocatable>(ConstValue)) { |
Karl Schimpf | d469994 | 2016-04-02 09:55:31 -0700 | [diff] [blame] | 61 | if (!Asm.fixupIsPCRel(kind()) && getFlags().getUseNonsfi() && |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 62 | CR->getName().toString() != GlobalOffsetTable) { |
Jim Stichnoth | 3e32400 | 2016-03-08 16:18:40 -0800 | [diff] [blame] | 63 | Str << "@GOTOFF"; |
| 64 | } |
| 65 | } |
John Porto | 6e8d3fa | 2016-02-04 10:35:20 -0800 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | assert(Asm.load<RelocOffsetT>(position()) == 0); |
| 69 | |
| 70 | RelocOffsetT Offset = offset(); |
| 71 | if (Offset != 0) { |
| 72 | if (Offset > 0) { |
| 73 | Str << " + " << Offset; |
| 74 | } else { |
| 75 | assert(Offset != std::numeric_limits<RelocOffsetT>::lowest()); |
| 76 | Str << " - " << -Offset; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | // We need to emit the '- .' for PCRel fixups. Even if the relocation kind() |
| 81 | // is not PCRel, we emit the '- .' for the _GLOBAL_OFFSET_TABLE_. |
| 82 | // TODO(jpp): create fixups wrt the GOT with the right fixup kind. |
| 83 | if (Asm.fixupIsPCRel(kind()) || Symbol == GlobalOffsetTable) |
Karl Schimpf | 2fee2a2 | 2015-10-22 08:19:26 -0700 | [diff] [blame] | 84 | Str << " - ."; |
| 85 | Str << "\n"; |
| 86 | return FixupSize; |
| 87 | } |
| 88 | |
John Porto | 6e8d3fa | 2016-02-04 10:35:20 -0800 | [diff] [blame] | 89 | void AssemblerFixup::emitOffset(Assembler *Asm) const { |
| 90 | Asm->store(position(), offset()); |
| 91 | } |
| 92 | |
Karl Schimpf | e4289e2 | 2015-10-27 15:16:27 -0700 | [diff] [blame] | 93 | size_t AssemblerTextFixup::emit(GlobalContext *Ctx, const Assembler &) const { |
Karl Schimpf | 2fee2a2 | 2015-10-22 08:19:26 -0700 | [diff] [blame] | 94 | Ctx->getStrEmit() << Message << "\n"; |
| 95 | return NumBytes; |
Jan Voung | ec27073 | 2015-01-12 17:00:22 -0800 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | } // end of namespace Ice |