blob: f1f5a648bbf0e6dc8ddb91718f1d296b8240b0a0 [file] [log] [blame]
Daniel Dunbara11af532009-06-24 01:03:06 +00001//===- 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
Daniel Dunbara11af532009-06-24 01:03:06 +000012#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"
17using namespace llvm;
18
19namespace {
20
21 class MCAsmStreamer : public MCStreamer {
22 raw_ostream &OS;
23
24 MCSection *CurSection;
25
26 public:
27 MCAsmStreamer(MCContext &Context, raw_ostream &_OS)
28 : MCStreamer(Context), OS(_OS) {}
29 ~MCAsmStreamer() {}
30
31 /// @name MCStreamer Interface
32 /// @{
33
34 virtual void SwitchSection(MCSection *Section);
35
36 virtual void EmitLabel(MCSymbol *Symbol);
37
38 virtual void EmitAssignment(MCSymbol *Symbol, const MCValue &Value,
39 bool MakeAbsolute = false);
40
41 virtual void EmitSymbolAttribute(MCSymbol *Symbol, SymbolAttr Attribute);
42
43 virtual void EmitBytes(const char *Data, unsigned Length);
44
45 virtual void EmitValue(const MCValue &Value, unsigned Size);
46
Daniel Dunbar84a29262009-06-24 19:25:34 +000047 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
48 unsigned ValueSize = 1,
49 unsigned MaxBytesToEmit = 0);
50
51 virtual void EmitValueToOffset(const MCValue &Offset,
52 unsigned char Value = 0);
53
Daniel Dunbara11af532009-06-24 01:03:06 +000054 virtual void EmitInstruction(const MCInst &Inst);
55
56 virtual void Finish();
57
58 /// @}
59 };
60
61}
62
63/// Allow printing values directly to a raw_ostream.
64inline raw_ostream &operator<<(raw_ostream &os, const MCValue &Value) {
65 if (Value.getSymA()) {
66 os << Value.getSymA()->getName();
67 if (Value.getSymB())
68 os << " - " << Value.getSymB()->getName();
69 if (Value.getCst())
70 os << " + " << Value.getCst();
71 } else {
72 assert(!Value.getSymB() && "Invalid machine code value!");
73 os << Value.getCst();
74 }
75
76 return os;
77}
78
79void MCAsmStreamer::SwitchSection(MCSection *Section) {
80 if (Section != CurSection) {
81 CurSection = Section;
82
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
85 // this stuff though.
86 OS << ".section " << Section->getName() << "\n";
87 }
88}
89
90void MCAsmStreamer::EmitLabel(MCSymbol *Symbol) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +000091 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!");
95
Daniel Dunbara11af532009-06-24 01:03:06 +000096 OS << Symbol->getName() << ":\n";
Daniel Dunbar71d259b2009-06-24 17:00:42 +000097 Symbol->setSection(CurSection);
Daniel Dunbara11af532009-06-24 01:03:06 +000098}
99
100void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCValue &Value,
101 bool MakeAbsolute) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000102 assert(!Symbol->getSection() && "Cannot assign to a label!");
103
Daniel Dunbara11af532009-06-24 01:03:06 +0000104 if (MakeAbsolute) {
105 OS << ".set " << Symbol->getName() << ", " << Value << '\n';
106 } else {
107 OS << Symbol->getName() << " = " << Value << '\n';
108 }
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000109
110 getContext().SetSymbolValue(Symbol, Value);
Daniel Dunbara11af532009-06-24 01:03:06 +0000111}
112
113void MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
114 SymbolAttr Attribute) {
115 switch (Attribute) {
116 case Global: OS << ".globl"; break;
Daniel Dunbard814b212009-06-24 16:36:52 +0000117 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;
Daniel Dunbara11af532009-06-24 01:03:06 +0000122 case PrivateExtern: OS << ".private_extern"; break;
Daniel Dunbard814b212009-06-24 16:36:52 +0000123 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;
Daniel Dunbara11af532009-06-24 01:03:06 +0000128 }
129
130 OS << ' ' << Symbol->getName() << '\n';
131}
132
133void MCAsmStreamer::EmitBytes(const char *Data, unsigned Length) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000134 assert(CurSection && "Cannot emit contents before setting section!");
135 for (unsigned i = 0; i != Length; ++i)
Daniel Dunbara11af532009-06-24 01:03:06 +0000136 OS << ".byte " << (unsigned) Data[i] << '\n';
Daniel Dunbara11af532009-06-24 01:03:06 +0000137}
138
139void MCAsmStreamer::EmitValue(const MCValue &Value, unsigned Size) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000140 assert(CurSection && "Cannot emit contents before setting section!");
Daniel Dunbara11af532009-06-24 01:03:06 +0000141 // Need target hooks to know how to print this.
142 switch (Size) {
143 default:
144 assert(0 && "Invalid size for machine code value!");
145 case 1: OS << ".byte"; break;
Daniel Dunbarf5e75a12009-06-24 16:05:35 +0000146 case 2: OS << ".short"; break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000147 case 4: OS << ".long"; break;
148 case 8: OS << ".quad"; break;
149 }
150
151 OS << ' ' << Value << '\n';
152}
153
Daniel Dunbar84a29262009-06-24 19:25:34 +0000154void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
155 unsigned ValueSize,
156 unsigned MaxBytesToEmit) {
157 unsigned Pow2 = Log2_32(ByteAlignment);
158 assert((1U << Pow2) == ByteAlignment && "Invalid alignment!");
159
160 switch (ValueSize) {
161 default:
162 assert(0 && "Invalid size for machine code value!");
163 case 8:
164 assert(0 && "Unsupported alignment size!");
165 case 1: OS << ".p2align"; break;
166 case 2: OS << ".p2alignw"; break;
167 case 4: OS << ".p2alignl"; break;
168 }
169
170 OS << ' ' << Pow2;
171
172 OS << ", " << Value;
173 if (MaxBytesToEmit)
174 OS << ", " << MaxBytesToEmit;
175 OS << '\n';
176}
177
178void 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';
182}
183
Daniel Dunbara11af532009-06-24 01:03:06 +0000184void MCAsmStreamer::EmitInstruction(const MCInst &Inst) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000185 assert(CurSection && "Cannot emit contents before setting section!");
Daniel Dunbara11af532009-06-24 01:03:06 +0000186 // FIXME: Implement.
187 OS << "# FIXME: Implement instruction printing!\n";
188}
189
190void MCAsmStreamer::Finish() {
191 OS.flush();
192}
193
194MCStreamer *llvm::createAsmStreamer(MCContext &Context, raw_ostream &OS) {
195 return new MCAsmStreamer(Context, OS);
196}