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/MCInst.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), CurSection(0) {}
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 EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
49 unsigned ValueSize = 1,
50 unsigned MaxBytesToEmit = 0);
52 virtual void EmitValueToOffset(const MCValue &Offset,
53 unsigned char Value = 0);
55 virtual void EmitInstruction(const MCInst &Inst);
57 virtual void Finish();
64 /// Allow printing values directly to a raw_ostream.
65 static inline raw_ostream &operator<<(raw_ostream &os, const MCValue &Value) {
66 if (Value.getSymA()) {
67 os << Value.getSymA()->getName();
69 os << " - " << Value.getSymB()->getName();
70 if (Value.getConstant())
71 os << " + " << Value.getConstant();
73 assert(!Value.getSymB() && "Invalid machine code value!");
74 os << Value.getConstant();
80 static inline int64_t truncateToSize(int64_t Value, unsigned Bytes) {
81 assert(Bytes && "Invalid size!");
82 return Value & ((uint64_t) (int64_t) -1 >> (64 - Bytes * 8));
85 static inline MCValue truncateToSize(const MCValue &Value, unsigned Bytes) {
86 return MCValue::get(Value.getSymA(), Value.getSymB(),
87 truncateToSize(Value.getConstant(), Bytes));
90 void MCAsmStreamer::SwitchSection(MCSection *Section) {
91 if (Section != CurSection) {
94 // FIXME: Really we would like the segment, flags, etc. to be separate
95 // values instead of embedded in the name. Not all assemblers understand all
97 OS << ".section " << Section->getName() << "\n";
101 void MCAsmStreamer::EmitLabel(MCSymbol *Symbol) {
102 assert(Symbol->getSection() == 0 && "Cannot emit a symbol twice!");
103 assert(CurSection && "Cannot emit before setting section!");
104 assert(!getContext().GetSymbolValue(Symbol) &&
105 "Cannot emit symbol which was directly assigned to!");
107 OS << Symbol->getName() << ":\n";
108 Symbol->setSection(CurSection);
109 Symbol->setExternal(false);
112 void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCValue &Value,
114 assert(!Symbol->getSection() && "Cannot assign to a label!");
117 OS << ".set " << Symbol->getName() << ", " << Value << '\n';
119 OS << Symbol->getName() << " = " << Value << '\n';
122 getContext().SetSymbolValue(Symbol, Value);
125 void MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
126 SymbolAttr Attribute) {
128 case Global: OS << ".globl"; break;
129 case Hidden: OS << ".hidden"; break;
130 case IndirectSymbol: OS << ".indirect_symbol"; break;
131 case Internal: OS << ".internal"; break;
132 case LazyReference: OS << ".lazy_reference"; break;
133 case NoDeadStrip: OS << ".no_dead_strip"; break;
134 case PrivateExtern: OS << ".private_extern"; break;
135 case Protected: OS << ".protected"; break;
136 case Reference: OS << ".reference"; break;
137 case Weak: OS << ".weak"; break;
138 case WeakDefinition: OS << ".weak_definition"; break;
139 case WeakReference: OS << ".weak_reference"; break;
142 OS << ' ' << Symbol->getName() << '\n';
145 void MCAsmStreamer::EmitBytes(const char *Data, unsigned Length) {
146 assert(CurSection && "Cannot emit contents before setting section!");
147 for (unsigned i = 0; i != Length; ++i)
148 OS << ".byte " << (unsigned) Data[i] << '\n';
151 void MCAsmStreamer::EmitValue(const MCValue &Value, unsigned Size) {
152 assert(CurSection && "Cannot emit contents before setting section!");
153 // Need target hooks to know how to print this.
156 assert(0 && "Invalid size for machine code value!");
157 case 1: OS << ".byte"; break;
158 case 2: OS << ".short"; break;
159 case 4: OS << ".long"; break;
160 case 8: OS << ".quad"; break;
163 OS << ' ' << truncateToSize(Value, Size) << '\n';
166 void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
168 unsigned MaxBytesToEmit) {
169 // Some assemblers don't support .balign, so we always emit as .p2align if
170 // this is a power of two. Otherwise we assume the client knows the target
171 // supports .balign and use that.
172 unsigned Pow2 = Log2_32(ByteAlignment);
173 bool IsPow2 = (1U << Pow2) == ByteAlignment;
177 assert(0 && "Invalid size for machine code value!");
179 assert(0 && "Unsupported alignment size!");
180 case 1: OS << (IsPow2 ? ".p2align" : ".balign"); break;
181 case 2: OS << (IsPow2 ? ".p2alignw" : ".balignw"); break;
182 case 4: OS << (IsPow2 ? ".p2alignl" : ".balignl"); break;
185 OS << ' ' << (IsPow2 ? Pow2 : ByteAlignment);
187 OS << ", " << truncateToSize(Value, ValueSize);
189 OS << ", " << MaxBytesToEmit;
193 void MCAsmStreamer::EmitValueToOffset(const MCValue &Offset,
194 unsigned char Value) {
195 // FIXME: Verify that Offset is associated with the current section.
196 OS << ".org " << Offset << ", " << (unsigned) Value << '\n';
199 static raw_ostream &operator<<(raw_ostream &OS, const MCOperand &Op) {
201 return OS << "reg:" << Op.getReg();
203 return OS << "imm:" << Op.getImm();
205 return OS << "mbblabel:("
206 << Op.getMBBLabelFunction() << ", " << Op.getMBBLabelBlock();
207 assert(Op.isMCValue() && "Invalid operand!");
208 return OS << "val:" << Op.getMCValue();
211 void MCAsmStreamer::EmitInstruction(const MCInst &Inst) {
212 assert(CurSection && "Cannot emit contents before setting section!");
213 // FIXME: Implement proper printing.
215 << "opcode=" << Inst.getOpcode() << ", "
217 for (unsigned i = 0, e = Inst.getNumOperands(); i != e; ++i) {
220 OS << Inst.getOperand(i);
225 void MCAsmStreamer::Finish() {
229 MCStreamer *llvm::createAsmStreamer(MCContext &Context, raw_ostream &OS) {
230 return new MCAsmStreamer(Context, OS);