1 //===- lib/MC/MCAsmStreamer.cpp - Text Assembly Output --------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/MC/MCStreamer.h"
12 #include "llvm/MC/MCContext.h"
13 #include "llvm/MC/MCSection.h"
14 #include "llvm/MC/MCSymbol.h"
15 #include "llvm/MC/MCValue.h"
16 #include "llvm/Support/raw_ostream.h"
21 class MCAsmStreamer : public MCStreamer {
24 MCSection *CurSection;
27 MCAsmStreamer(MCContext &Context, raw_ostream &_OS)
28 : MCStreamer(Context), OS(_OS) {}
31 /// @name MCStreamer Interface
34 virtual void SwitchSection(MCSection *Section);
36 virtual void EmitLabel(MCSymbol *Symbol);
38 virtual void EmitAssignment(MCSymbol *Symbol, const MCValue &Value,
39 bool MakeAbsolute = false);
41 virtual void EmitSymbolAttribute(MCSymbol *Symbol, SymbolAttr Attribute);
43 virtual void EmitBytes(const char *Data, unsigned Length);
45 virtual void EmitValue(const MCValue &Value, unsigned Size);
47 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
48 unsigned ValueSize = 1,
49 unsigned MaxBytesToEmit = 0);
51 virtual void EmitValueToOffset(const MCValue &Offset,
52 unsigned char Value = 0);
54 virtual void EmitInstruction(const MCInst &Inst);
56 virtual void Finish();
63 /// Allow printing values directly to a raw_ostream.
64 inline raw_ostream &operator<<(raw_ostream &os, const MCValue &Value) {
65 if (Value.getSymA()) {
66 os << Value.getSymA()->getName();
68 os << " - " << Value.getSymB()->getName();
70 os << " + " << Value.getCst();
72 assert(!Value.getSymB() && "Invalid machine code value!");
79 void MCAsmStreamer::SwitchSection(MCSection *Section) {
80 if (Section != CurSection) {
83 // FIXME: Really we would like the segment, flags, etc. to be separate
84 // values instead of embedded in the name. Not all assemblers understand all
86 OS << ".section " << Section->getName() << "\n";
90 void MCAsmStreamer::EmitLabel(MCSymbol *Symbol) {
91 assert(Symbol->getSection() == 0 && "Cannot emit a symbol twice!");
92 assert(CurSection && "Cannot emit before setting section!");
93 assert(!getContext().GetSymbolValue(Symbol) &&
94 "Cannot emit symbol which was directly assigned to!");
96 OS << Symbol->getName() << ":\n";
97 Symbol->setSection(CurSection);
100 void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCValue &Value,
102 assert(!Symbol->getSection() && "Cannot assign to a label!");
105 OS << ".set " << Symbol->getName() << ", " << Value << '\n';
107 OS << Symbol->getName() << " = " << Value << '\n';
110 getContext().SetSymbolValue(Symbol, Value);
113 void MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
114 SymbolAttr Attribute) {
116 case Global: OS << ".globl"; break;
117 case Hidden: OS << ".hidden"; break;
118 case IndirectSymbol: OS << ".indirect_symbol"; break;
119 case Internal: OS << ".internal"; break;
120 case LazyReference: OS << ".lazy_reference"; break;
121 case NoDeadStrip: OS << ".no_dead_strip"; break;
122 case PrivateExtern: OS << ".private_extern"; break;
123 case Protected: OS << ".protected"; break;
124 case Reference: OS << ".reference"; break;
125 case Weak: OS << ".weak"; break;
126 case WeakDefinition: OS << ".weak_definition"; break;
127 case WeakReference: OS << ".weak_reference"; break;
130 OS << ' ' << Symbol->getName() << '\n';
133 void MCAsmStreamer::EmitBytes(const char *Data, unsigned Length) {
134 assert(CurSection && "Cannot emit contents before setting section!");
135 for (unsigned i = 0; i != Length; ++i)
136 OS << ".byte " << (unsigned) Data[i] << '\n';
139 void MCAsmStreamer::EmitValue(const MCValue &Value, unsigned Size) {
140 assert(CurSection && "Cannot emit contents before setting section!");
141 // Need target hooks to know how to print this.
144 assert(0 && "Invalid size for machine code value!");
145 case 1: OS << ".byte"; break;
146 case 2: OS << ".short"; break;
147 case 4: OS << ".long"; break;
148 case 8: OS << ".quad"; break;
151 OS << ' ' << Value << '\n';
154 void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
156 unsigned MaxBytesToEmit) {
157 unsigned Pow2 = Log2_32(ByteAlignment);
158 assert((1U << Pow2) == ByteAlignment && "Invalid alignment!");
162 assert(0 && "Invalid size for machine code value!");
164 assert(0 && "Unsupported alignment size!");
165 case 1: OS << ".p2align"; break;
166 case 2: OS << ".p2alignw"; break;
167 case 4: OS << ".p2alignl"; break;
174 OS << ", " << MaxBytesToEmit;
178 void MCAsmStreamer::EmitValueToOffset(const MCValue &Offset,
179 unsigned char Value) {
180 // FIXME: Verify that Offset is associated with the current section.
181 OS << ".org " << Offset << ", " << (unsigned) Value << '\n';
184 void MCAsmStreamer::EmitInstruction(const MCInst &Inst) {
185 assert(CurSection && "Cannot emit contents before setting section!");
187 OS << "# FIXME: Implement instruction printing!\n";
190 void MCAsmStreamer::Finish() {
194 MCStreamer *llvm::createAsmStreamer(MCContext &Context, raw_ostream &OS) {
195 return new MCAsmStreamer(Context, OS);