Daniel Dunbar | 8dc68ab | 2010-06-16 20:04:22 +0000 | [diff] [blame] | 1 | //===- lib/MC/MCObjectStreamer.cpp - Object File MCStreamer Interface -----===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "llvm/MC/MCObjectStreamer.h" |
| 11 | |
Michael J. Spencer | 8067adc | 2010-07-19 06:13:10 +0000 | [diff] [blame] | 12 | #include "llvm/Support/ErrorHandling.h" |
Daniel Dunbar | 8dc68ab | 2010-06-16 20:04:22 +0000 | [diff] [blame] | 13 | #include "llvm/MC/MCAssembler.h" |
Benjamin Kramer | 1abcd06 | 2010-07-29 17:48:06 +0000 | [diff] [blame] | 14 | #include "llvm/MC/MCCodeEmitter.h" |
Michael J. Spencer | 8067adc | 2010-07-19 06:13:10 +0000 | [diff] [blame] | 15 | #include "llvm/MC/MCExpr.h" |
Benjamin Kramer | 1abcd06 | 2010-07-29 17:48:06 +0000 | [diff] [blame] | 16 | #include "llvm/Target/TargetAsmBackend.h" |
Daniel Dunbar | 8dc68ab | 2010-06-16 20:04:22 +0000 | [diff] [blame] | 17 | using namespace llvm; |
| 18 | |
| 19 | MCObjectStreamer::MCObjectStreamer(MCContext &Context, TargetAsmBackend &TAB, |
Rafael Espindola | 59ff3c9 | 2010-09-22 22:27:05 +0000 | [diff] [blame] | 20 | raw_ostream &_OS, MCCodeEmitter *_Emitter, |
| 21 | bool _PadSectionToAlignment) |
Daniel Dunbar | 83b4671 | 2010-06-16 20:04:25 +0000 | [diff] [blame] | 22 | : MCStreamer(Context), Assembler(new MCAssembler(Context, TAB, |
Rafael Espindola | 59ff3c9 | 2010-09-22 22:27:05 +0000 | [diff] [blame] | 23 | *_Emitter, |
| 24 | _PadSectionToAlignment, |
| 25 | _OS)), |
Daniel Dunbar | 83b4671 | 2010-06-16 20:04:25 +0000 | [diff] [blame] | 26 | CurSectionData(0) |
Daniel Dunbar | 8dc68ab | 2010-06-16 20:04:22 +0000 | [diff] [blame] | 27 | { |
| 28 | } |
| 29 | |
| 30 | MCObjectStreamer::~MCObjectStreamer() { |
Benjamin Kramer | 1abcd06 | 2010-07-29 17:48:06 +0000 | [diff] [blame] | 31 | delete &Assembler->getBackend(); |
| 32 | delete &Assembler->getEmitter(); |
Daniel Dunbar | 8dc68ab | 2010-06-16 20:04:22 +0000 | [diff] [blame] | 33 | delete Assembler; |
| 34 | } |
Daniel Dunbar | 83b4671 | 2010-06-16 20:04:25 +0000 | [diff] [blame] | 35 | |
Michael J. Spencer | 8067adc | 2010-07-19 06:13:10 +0000 | [diff] [blame] | 36 | MCFragment *MCObjectStreamer::getCurrentFragment() const { |
| 37 | assert(getCurrentSectionData() && "No current section!"); |
| 38 | |
| 39 | if (!getCurrentSectionData()->empty()) |
| 40 | return &getCurrentSectionData()->getFragmentList().back(); |
| 41 | |
| 42 | return 0; |
| 43 | } |
| 44 | |
| 45 | MCDataFragment *MCObjectStreamer::getOrCreateDataFragment() const { |
| 46 | MCDataFragment *F = dyn_cast_or_null<MCDataFragment>(getCurrentFragment()); |
| 47 | if (!F) |
| 48 | F = new MCDataFragment(getCurrentSectionData()); |
| 49 | return F; |
| 50 | } |
| 51 | |
| 52 | const MCExpr *MCObjectStreamer::AddValueSymbols(const MCExpr *Value) { |
| 53 | switch (Value->getKind()) { |
| 54 | case MCExpr::Target: llvm_unreachable("Can't handle target exprs yet!"); |
| 55 | case MCExpr::Constant: |
| 56 | break; |
| 57 | |
| 58 | case MCExpr::Binary: { |
| 59 | const MCBinaryExpr *BE = cast<MCBinaryExpr>(Value); |
| 60 | AddValueSymbols(BE->getLHS()); |
| 61 | AddValueSymbols(BE->getRHS()); |
| 62 | break; |
| 63 | } |
| 64 | |
| 65 | case MCExpr::SymbolRef: |
| 66 | Assembler->getOrCreateSymbolData(cast<MCSymbolRefExpr>(Value)->getSymbol()); |
| 67 | break; |
| 68 | |
| 69 | case MCExpr::Unary: |
| 70 | AddValueSymbols(cast<MCUnaryExpr>(Value)->getSubExpr()); |
| 71 | break; |
| 72 | } |
| 73 | |
| 74 | return Value; |
| 75 | } |
| 76 | |
Rafael Espindola | 484291c | 2010-11-01 14:28:48 +0000 | [diff] [blame] | 77 | void MCObjectStreamer::EmitWeakReference(MCSymbol *Alias, |
| 78 | const MCSymbol *Symbol) { |
| 79 | report_fatal_error("This file format doesn't support weak aliases."); |
| 80 | } |
| 81 | |
Daniel Dunbar | 83b4671 | 2010-06-16 20:04:25 +0000 | [diff] [blame] | 82 | void MCObjectStreamer::SwitchSection(const MCSection *Section) { |
| 83 | assert(Section && "Cannot switch to a null section!"); |
| 84 | |
| 85 | // If already in this section, then this is a noop. |
| 86 | if (Section == CurSection) return; |
| 87 | |
Benjamin Kramer | 1674b0b | 2010-09-02 18:53:37 +0000 | [diff] [blame] | 88 | PrevSection = CurSection; |
Daniel Dunbar | 83b4671 | 2010-06-16 20:04:25 +0000 | [diff] [blame] | 89 | CurSection = Section; |
| 90 | CurSectionData = &getAssembler().getOrCreateSectionData(*Section); |
| 91 | } |
| 92 | |
| 93 | void MCObjectStreamer::Finish() { |
| 94 | getAssembler().Finish(); |
| 95 | } |