MCAsmStreamer.cpp revision a11af531ec48ad84f790b9511f003ac5c934a999
1//===- lib/MC/MCAsmStreamer.cpp - Text Assembly Output --------------------===//
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/MCStreamer.h"
11
12#include "llvm/MC/MCAtom.h"
13#include "llvm/MC/MCContext.h"
14#include "llvm/MC/MCSection.h"
15#include "llvm/MC/MCSymbol.h"
16#include "llvm/MC/MCValue.h"
17#include "llvm/Support/raw_ostream.h"
18using namespace llvm;
19
20namespace {
21
22  class MCAsmStreamer : public MCStreamer {
23    raw_ostream &OS;
24
25    MCSection *CurSection;
26
27  public:
28    MCAsmStreamer(MCContext &Context, raw_ostream &_OS)
29      : MCStreamer(Context), OS(_OS) {}
30    ~MCAsmStreamer() {}
31
32    /// @name MCStreamer Interface
33    /// @{
34
35    virtual void SwitchSection(MCSection *Section);
36
37    virtual void EmitLabel(MCSymbol *Symbol);
38
39    virtual void EmitAssignment(MCSymbol *Symbol, const MCValue &Value,
40                                bool MakeAbsolute = false);
41
42    virtual void EmitSymbolAttribute(MCSymbol *Symbol, SymbolAttr Attribute);
43
44    virtual void EmitBytes(const char *Data, unsigned Length);
45
46    virtual void EmitValue(const MCValue &Value, unsigned Size);
47
48    virtual void EmitInstruction(const MCInst &Inst);
49
50    virtual void Finish();
51
52    /// @}
53  };
54
55}
56
57/// Allow printing values directly to a raw_ostream.
58inline raw_ostream &operator<<(raw_ostream &os, const MCValue &Value) {
59  if (Value.getSymA()) {
60    os << Value.getSymA()->getName();
61    if (Value.getSymB())
62      os << " - " << Value.getSymB()->getName();
63    if (Value.getCst())
64      os << " + " << Value.getCst();
65  } else {
66    assert(!Value.getSymB() && "Invalid machine code value!");
67    os << Value.getCst();
68  }
69
70  return os;
71}
72
73void MCAsmStreamer::SwitchSection(MCSection *Section) {
74  if (Section != CurSection) {
75    CurSection = Section;
76
77    // FIXME: Really we would like the segment, flags, etc. to be separate
78    // values instead of embedded in the name. Not all assemblers understand all
79    // this stuff though.
80    OS << ".section " << Section->getName() << "\n";
81  }
82}
83
84void MCAsmStreamer::EmitLabel(MCSymbol *Symbol) {
85  // FIXME: We need to enforce that we aren't printing atoms which are more
86  // complicated than the assembler understands.
87  assert(Symbol->getAtom()->getSection() == CurSection &&
88         "The label for a symbol must match its section!");
89  OS << Symbol->getName() << ":\n";
90}
91
92void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCValue &Value,
93                                   bool MakeAbsolute) {
94  if (MakeAbsolute) {
95    OS << ".set " << Symbol->getName() << ", " << Value << '\n';
96  } else {
97    OS << Symbol->getName() << " = " << Value << '\n';
98  }
99}
100
101void MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
102                                        SymbolAttr Attribute) {
103  switch (Attribute) {
104  case Global: OS << ".globl"; break;
105  case Weak: OS << ".weak"; break;
106  case PrivateExtern: OS << ".private_extern"; break;
107  }
108
109  OS << ' ' << Symbol->getName() << '\n';
110}
111
112void MCAsmStreamer::EmitBytes(const char *Data, unsigned Length) {
113  for (unsigned i = 0; i != Length; ++i) {
114    OS << ".byte " << (unsigned) Data[i] << '\n';
115  }
116}
117
118void MCAsmStreamer::EmitValue(const MCValue &Value, unsigned Size) {
119  // Need target hooks to know how to print this.
120  switch (Size) {
121  default:
122    assert(0 && "Invalid size for machine code value!");
123  case 1: OS << ".byte"; break;
124  case 2: OS << ".hword"; break;
125  case 4: OS << ".long"; break;
126  case 8: OS << ".quad"; break;
127  }
128
129  OS << ' ' << Value << '\n';
130}
131
132void MCAsmStreamer::EmitInstruction(const MCInst &Inst) {
133  // FIXME: Implement.
134  OS << "# FIXME: Implement instruction printing!\n";
135}
136
137void MCAsmStreamer::Finish() {
138  OS.flush();
139}
140
141MCStreamer *llvm::createAsmStreamer(MCContext &Context, raw_ostream &OS) {
142  return new MCAsmStreamer(Context, OS);
143}
144