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" |
Michael J. Spencer | 8067adc | 2010-07-19 06:13:10 +0000 | [diff] [blame] | 14 | #include "llvm/MC/MCExpr.h" |
Daniel Dunbar | 8dc68ab | 2010-06-16 20:04:22 +0000 | [diff] [blame] | 15 | using namespace llvm; |
| 16 | |
| 17 | MCObjectStreamer::MCObjectStreamer(MCContext &Context, TargetAsmBackend &TAB, |
| 18 | raw_ostream &_OS, MCCodeEmitter *_Emitter) |
Daniel Dunbar | 83b4671 | 2010-06-16 20:04:25 +0000 | [diff] [blame] | 19 | : MCStreamer(Context), Assembler(new MCAssembler(Context, TAB, |
| 20 | *_Emitter, _OS)), |
| 21 | CurSectionData(0) |
Daniel Dunbar | 8dc68ab | 2010-06-16 20:04:22 +0000 | [diff] [blame] | 22 | { |
| 23 | } |
| 24 | |
| 25 | MCObjectStreamer::~MCObjectStreamer() { |
| 26 | delete Assembler; |
| 27 | } |
Daniel Dunbar | 83b4671 | 2010-06-16 20:04:25 +0000 | [diff] [blame] | 28 | |
Michael J. Spencer | 8067adc | 2010-07-19 06:13:10 +0000 | [diff] [blame] | 29 | MCFragment *MCObjectStreamer::getCurrentFragment() const { |
| 30 | assert(getCurrentSectionData() && "No current section!"); |
| 31 | |
| 32 | if (!getCurrentSectionData()->empty()) |
| 33 | return &getCurrentSectionData()->getFragmentList().back(); |
| 34 | |
| 35 | return 0; |
| 36 | } |
| 37 | |
| 38 | MCDataFragment *MCObjectStreamer::getOrCreateDataFragment() const { |
| 39 | MCDataFragment *F = dyn_cast_or_null<MCDataFragment>(getCurrentFragment()); |
| 40 | if (!F) |
| 41 | F = new MCDataFragment(getCurrentSectionData()); |
| 42 | return F; |
| 43 | } |
| 44 | |
| 45 | const MCExpr *MCObjectStreamer::AddValueSymbols(const MCExpr *Value) { |
| 46 | switch (Value->getKind()) { |
| 47 | case MCExpr::Target: llvm_unreachable("Can't handle target exprs yet!"); |
| 48 | case MCExpr::Constant: |
| 49 | break; |
| 50 | |
| 51 | case MCExpr::Binary: { |
| 52 | const MCBinaryExpr *BE = cast<MCBinaryExpr>(Value); |
| 53 | AddValueSymbols(BE->getLHS()); |
| 54 | AddValueSymbols(BE->getRHS()); |
| 55 | break; |
| 56 | } |
| 57 | |
| 58 | case MCExpr::SymbolRef: |
| 59 | Assembler->getOrCreateSymbolData(cast<MCSymbolRefExpr>(Value)->getSymbol()); |
| 60 | break; |
| 61 | |
| 62 | case MCExpr::Unary: |
| 63 | AddValueSymbols(cast<MCUnaryExpr>(Value)->getSubExpr()); |
| 64 | break; |
| 65 | } |
| 66 | |
| 67 | return Value; |
| 68 | } |
| 69 | |
Daniel Dunbar | 83b4671 | 2010-06-16 20:04:25 +0000 | [diff] [blame] | 70 | void MCObjectStreamer::SwitchSection(const MCSection *Section) { |
| 71 | assert(Section && "Cannot switch to a null section!"); |
| 72 | |
| 73 | // If already in this section, then this is a noop. |
| 74 | if (Section == CurSection) return; |
| 75 | |
| 76 | CurSection = Section; |
| 77 | CurSectionData = &getAssembler().getOrCreateSectionData(*Section); |
| 78 | } |
| 79 | |
| 80 | void MCObjectStreamer::Finish() { |
| 81 | getAssembler().Finish(); |
| 82 | } |