blob: 73d6f046dc24a2c52d5db819432db2b566a206f8 [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
47 virtual void EmitInstruction(const MCInst &Inst);
48
49 virtual void Finish();
50
51 /// @}
52 };
53
54}
55
56/// Allow printing values directly to a raw_ostream.
57inline raw_ostream &operator<<(raw_ostream &os, const MCValue &Value) {
58 if (Value.getSymA()) {
59 os << Value.getSymA()->getName();
60 if (Value.getSymB())
61 os << " - " << Value.getSymB()->getName();
62 if (Value.getCst())
63 os << " + " << Value.getCst();
64 } else {
65 assert(!Value.getSymB() && "Invalid machine code value!");
66 os << Value.getCst();
67 }
68
69 return os;
70}
71
72void MCAsmStreamer::SwitchSection(MCSection *Section) {
73 if (Section != CurSection) {
74 CurSection = Section;
75
76 // FIXME: Really we would like the segment, flags, etc. to be separate
77 // values instead of embedded in the name. Not all assemblers understand all
78 // this stuff though.
79 OS << ".section " << Section->getName() << "\n";
80 }
81}
82
83void MCAsmStreamer::EmitLabel(MCSymbol *Symbol) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +000084 assert(Symbol->getSection() == 0 && "Cannot emit a symbol twice!");
85 assert(CurSection && "Cannot emit before setting section!");
86 assert(!getContext().GetSymbolValue(Symbol) &&
87 "Cannot emit symbol which was directly assigned to!");
88
Daniel Dunbara11af532009-06-24 01:03:06 +000089 OS << Symbol->getName() << ":\n";
Daniel Dunbar71d259b2009-06-24 17:00:42 +000090 Symbol->setSection(CurSection);
Daniel Dunbara11af532009-06-24 01:03:06 +000091}
92
93void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCValue &Value,
94 bool MakeAbsolute) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +000095 assert(!Symbol->getSection() && "Cannot assign to a label!");
96
Daniel Dunbara11af532009-06-24 01:03:06 +000097 if (MakeAbsolute) {
98 OS << ".set " << Symbol->getName() << ", " << Value << '\n';
99 } else {
100 OS << Symbol->getName() << " = " << Value << '\n';
101 }
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000102
103 getContext().SetSymbolValue(Symbol, Value);
Daniel Dunbara11af532009-06-24 01:03:06 +0000104}
105
106void MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
107 SymbolAttr Attribute) {
108 switch (Attribute) {
109 case Global: OS << ".globl"; break;
Daniel Dunbard814b212009-06-24 16:36:52 +0000110 case Hidden: OS << ".hidden"; break;
111 case IndirectSymbol: OS << ".indirect_symbol"; break;
112 case Internal: OS << ".internal"; break;
113 case LazyReference: OS << ".lazy_reference"; break;
114 case NoDeadStrip: OS << ".no_dead_strip"; break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000115 case PrivateExtern: OS << ".private_extern"; break;
Daniel Dunbard814b212009-06-24 16:36:52 +0000116 case Protected: OS << ".protected"; break;
117 case Reference: OS << ".reference"; break;
118 case Weak: OS << ".weak"; break;
119 case WeakDefinition: OS << ".weak_definition"; break;
120 case WeakReference: OS << ".weak_reference"; break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000121 }
122
123 OS << ' ' << Symbol->getName() << '\n';
124}
125
126void MCAsmStreamer::EmitBytes(const char *Data, unsigned Length) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000127 assert(CurSection && "Cannot emit contents before setting section!");
128 for (unsigned i = 0; i != Length; ++i)
Daniel Dunbara11af532009-06-24 01:03:06 +0000129 OS << ".byte " << (unsigned) Data[i] << '\n';
Daniel Dunbara11af532009-06-24 01:03:06 +0000130}
131
132void MCAsmStreamer::EmitValue(const MCValue &Value, unsigned Size) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000133 assert(CurSection && "Cannot emit contents before setting section!");
Daniel Dunbara11af532009-06-24 01:03:06 +0000134 // Need target hooks to know how to print this.
135 switch (Size) {
136 default:
137 assert(0 && "Invalid size for machine code value!");
138 case 1: OS << ".byte"; break;
Daniel Dunbarf5e75a12009-06-24 16:05:35 +0000139 case 2: OS << ".short"; break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000140 case 4: OS << ".long"; break;
141 case 8: OS << ".quad"; break;
142 }
143
144 OS << ' ' << Value << '\n';
145}
146
147void MCAsmStreamer::EmitInstruction(const MCInst &Inst) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000148 assert(CurSection && "Cannot emit contents before setting section!");
Daniel Dunbara11af532009-06-24 01:03:06 +0000149 // FIXME: Implement.
150 OS << "# FIXME: Implement instruction printing!\n";
151}
152
153void MCAsmStreamer::Finish() {
154 OS.flush();
155}
156
157MCStreamer *llvm::createAsmStreamer(MCContext &Context, raw_ostream &OS) {
158 return new MCAsmStreamer(Context, OS);
159}