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 Weak: OS << ".weak"; break;
106 case PrivateExtern: OS << ".private_extern"; break;
109 OS << ' ' << Symbol->getName() << '\n';
112 void MCAsmStreamer::EmitBytes(const char *Data, unsigned Length) {
113 for (unsigned i = 0; i != Length; ++i) {
114 OS << ".byte " << (unsigned) Data[i] << '\n';
118 void MCAsmStreamer::EmitValue(const MCValue &Value, unsigned Size) {
119 // Need target hooks to know how to print this.
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;
129 OS << ' ' << Value << '\n';
132 void MCAsmStreamer::EmitInstruction(const MCInst &Inst) {
134 OS << "# FIXME: Implement instruction printing!\n";
137 void MCAsmStreamer::Finish() {
141 MCStreamer *llvm::createAsmStreamer(MCContext &Context, raw_ostream &OS) {
142 return new MCAsmStreamer(Context, OS);