blob: 33d72e29f918624f52209b5be4444e9a9f7ed0ac [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
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"
18using namespace llvm;
19
20namespace {
21
22 class MCAsmStreamer : public MCStreamer {
23 raw_ostream &OS;
24
25 MCSection *CurSection;
26
27 public:
28 MCAsmStreamer(MCContext &Context, raw_ostream &_OS)
29 : MCStreamer(Context), OS(_OS) {}
30 ~MCAsmStreamer() {}
31
32 /// @name MCStreamer Interface
33 /// @{
34
35 virtual void SwitchSection(MCSection *Section);
36
37 virtual void EmitLabel(MCSymbol *Symbol);
38
39 virtual void EmitAssignment(MCSymbol *Symbol, const MCValue &Value,
40 bool MakeAbsolute = false);
41
42 virtual void EmitSymbolAttribute(MCSymbol *Symbol, SymbolAttr Attribute);
43
44 virtual void EmitBytes(const char *Data, unsigned Length);
45
46 virtual void EmitValue(const MCValue &Value, unsigned Size);
47
48 virtual void EmitInstruction(const MCInst &Inst);
49
50 virtual void Finish();
51
52 /// @}
53 };
54
55}
56
57/// Allow printing values directly to a raw_ostream.
58inline raw_ostream &operator<<(raw_ostream &os, const MCValue &Value) {
59 if (Value.getSymA()) {
60 os << Value.getSymA()->getName();
61 if (Value.getSymB())
62 os << " - " << Value.getSymB()->getName();
63 if (Value.getCst())
64 os << " + " << Value.getCst();
65 } else {
66 assert(!Value.getSymB() && "Invalid machine code value!");
67 os << Value.getCst();
68 }
69
70 return os;
71}
72
73void MCAsmStreamer::SwitchSection(MCSection *Section) {
74 if (Section != CurSection) {
75 CurSection = Section;
76
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
79 // this stuff though.
80 OS << ".section " << Section->getName() << "\n";
81 }
82}
83
84void 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.
Chris Lattnerc69485e2009-06-24 04:31:49 +000087 //assert(Symbol->getAtom()->getSection() == CurSection &&
88 // "The label for a symbol must match its section!");
Daniel Dunbara11af532009-06-24 01:03:06 +000089 OS << Symbol->getName() << ":\n";
90}
91
92void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCValue &Value,
93 bool MakeAbsolute) {
94 if (MakeAbsolute) {
95 OS << ".set " << Symbol->getName() << ", " << Value << '\n';
96 } else {
97 OS << Symbol->getName() << " = " << Value << '\n';
98 }
99}
100
101void MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
102 SymbolAttr Attribute) {
103 switch (Attribute) {
104 case Global: OS << ".globl"; break;
Daniel Dunbard814b212009-06-24 16:36:52 +0000105 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;
Daniel Dunbara11af532009-06-24 01:03:06 +0000110 case PrivateExtern: OS << ".private_extern"; break;
Daniel Dunbard814b212009-06-24 16:36:52 +0000111 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;
Daniel Dunbara11af532009-06-24 01:03:06 +0000116 }
117
118 OS << ' ' << Symbol->getName() << '\n';
119}
120
121void MCAsmStreamer::EmitBytes(const char *Data, unsigned Length) {
122 for (unsigned i = 0; i != Length; ++i) {
123 OS << ".byte " << (unsigned) Data[i] << '\n';
124 }
125}
126
127void MCAsmStreamer::EmitValue(const MCValue &Value, unsigned Size) {
128 // Need target hooks to know how to print this.
129 switch (Size) {
130 default:
131 assert(0 && "Invalid size for machine code value!");
132 case 1: OS << ".byte"; break;
Daniel Dunbarf5e75a12009-06-24 16:05:35 +0000133 case 2: OS << ".short"; break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000134 case 4: OS << ".long"; break;
135 case 8: OS << ".quad"; break;
136 }
137
138 OS << ' ' << Value << '\n';
139}
140
141void MCAsmStreamer::EmitInstruction(const MCInst &Inst) {
142 // FIXME: Implement.
143 OS << "# FIXME: Implement instruction printing!\n";
144}
145
146void MCAsmStreamer::Finish() {
147 OS.flush();
148}
149
150MCStreamer *llvm::createAsmStreamer(MCContext &Context, raw_ostream &OS) {
151 return new MCAsmStreamer(Context, OS);
152}