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/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"
22 class MCAsmStreamer : public MCStreamer {
25 MCSection *CurSection;
28 MCAsmStreamer(MCContext &Context, raw_ostream &_OS)
29 : MCStreamer(Context), OS(_OS) {}
32 /// @name MCStreamer Interface
35 virtual void SwitchSection(MCSection *Section);
37 virtual void EmitLabel(MCSymbol *Symbol);
39 virtual void EmitAssignment(MCSymbol *Symbol, const MCValue &Value,
40 bool MakeAbsolute = false);
42 virtual void EmitSymbolAttribute(MCSymbol *Symbol, SymbolAttr Attribute);
44 virtual void EmitBytes(const char *Data, unsigned Length);
46 virtual void EmitValue(const MCValue &Value, unsigned Size);
48 virtual void EmitInstruction(const MCInst &Inst);
50 virtual void Finish();
57 /// Allow printing values directly to a raw_ostream.
58 inline raw_ostream &operator<<(raw_ostream &os, const MCValue &Value) {
59 if (Value.getSymA()) {
60 os << Value.getSymA()->getName();
62 os << " - " << Value.getSymB()->getName();
64 os << " + " << Value.getCst();
66 assert(!Value.getSymB() && "Invalid machine code value!");
73 void MCAsmStreamer::SwitchSection(MCSection *Section) {
74 if (Section != CurSection) {
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
80 OS << ".section " << Section->getName() << "\n";
84 void 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";
92 void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCValue &Value,
95 OS << ".set " << Symbol->getName() << ", " << Value << '\n';
97 OS << Symbol->getName() << " = " << Value << '\n';
101 void MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
102 SymbolAttr Attribute) {
104 case Global: OS << ".globl"; break;
105 case Hidden: OS << ".hidden"; break;
106 case IndirectSymbol: OS << ".indirect_symbol"; break;
107 case Internal: OS << ".internal"; break;
108 case LazyReference: OS << ".lazy_reference"; break;
109 case NoDeadStrip: OS << ".no_dead_strip"; break;
110 case PrivateExtern: OS << ".private_extern"; break;
111 case Protected: OS << ".protected"; break;
112 case Reference: OS << ".reference"; break;
113 case Weak: OS << ".weak"; break;
114 case WeakDefinition: OS << ".weak_definition"; break;
115 case WeakReference: OS << ".weak_reference"; break;
118 OS << ' ' << Symbol->getName() << '\n';
121 void MCAsmStreamer::EmitBytes(const char *Data, unsigned Length) {
122 for (unsigned i = 0; i != Length; ++i) {
123 OS << ".byte " << (unsigned) Data[i] << '\n';
127 void MCAsmStreamer::EmitValue(const MCValue &Value, unsigned Size) {
128 // Need target hooks to know how to print this.
131 assert(0 && "Invalid size for machine code value!");
132 case 1: OS << ".byte"; break;
133 case 2: OS << ".short"; break;
134 case 4: OS << ".long"; break;
135 case 8: OS << ".quad"; break;
138 OS << ' ' << Value << '\n';
141 void MCAsmStreamer::EmitInstruction(const MCInst &Inst) {
143 OS << "# FIXME: Implement instruction printing!\n";
146 void MCAsmStreamer::Finish() {
150 MCStreamer *llvm::createAsmStreamer(MCContext &Context, raw_ostream &OS) {
151 return new MCAsmStreamer(Context, OS);