Implement the missing bits corresponding to .mips_hack_elf_flags.
[oota-llvm.git] / lib / Target / Mips / MCTargetDesc / MipsTargetStreamer.cpp
1 //===-- MipsTargetStreamer.cpp - Mips Target Streamer Methods -------------===//
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 // This file provides Mips specific target streamer methods.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "MipsTargetStreamer.h"
15 #include "MipsMCTargetDesc.h"
16 #include "llvm/MC/MCELF.h"
17 #include "llvm/MC/MCSymbol.h"
18 #include "llvm/MC/MCSubtargetInfo.h"
19 #include "llvm/Support/CommandLine.h"
20 #include "llvm/Support/ELF.h"
21 #include "llvm/Support/ErrorHandling.h"
22 #include "llvm/Support/FormattedStream.h"
23
24 using namespace llvm;
25
26 // Pin vtable to this file.
27 void MipsTargetStreamer::anchor() {}
28
29 MipsTargetStreamer::MipsTargetStreamer(MCStreamer &S) : MCTargetStreamer(S) {}
30
31 MipsTargetAsmStreamer::MipsTargetAsmStreamer(MCStreamer &S,
32                                              formatted_raw_ostream &OS)
33     : MipsTargetStreamer(S), OS(OS) {}
34
35 void MipsTargetAsmStreamer::emitDirectiveSetMicroMips() {
36   OS << "\t.set\tmicromips\n";
37 }
38
39 void MipsTargetAsmStreamer::emitDirectiveSetNoMicroMips() {
40   OS << "\t.set\tnomicromips\n";
41 }
42
43 void MipsTargetAsmStreamer::emitDirectiveSetMips16() {
44   OS << "\t.set\tmips16\n";
45 }
46
47 void MipsTargetAsmStreamer::emitDirectiveSetNoMips16() {
48   OS << "\t.set\tnomips16\n";
49 }
50
51 void MipsTargetAsmStreamer::emitDirectiveSetReorder() {
52   OS << "\t.set\treorder\n";
53 }
54
55 void MipsTargetAsmStreamer::emitDirectiveSetNoReorder() {
56   OS << "\t.set\tnoreorder\n";
57 }
58
59 void MipsTargetAsmStreamer::emitDirectiveSetMacro() {
60   OS << "\t.set\tmacro\n";
61 }
62
63 void MipsTargetAsmStreamer::emitDirectiveSetNoMacro() {
64   OS << "\t.set\tnomacro\n";
65 }
66
67 void MipsTargetAsmStreamer::emitDirectiveSetAt() {
68   OS << "\t.set\tat\n";
69 }
70
71 void MipsTargetAsmStreamer::emitDirectiveSetNoAt() {
72   OS << "\t.set\tnoat\n";
73 }
74
75 void MipsTargetAsmStreamer::emitDirectiveEnd(StringRef Name) {
76   OS << "\t.end\t" << Name << '\n';
77 }
78
79 void MipsTargetAsmStreamer::emitDirectiveEnt(const MCSymbol &Symbol) {
80   OS << "\t.ent\t" << Symbol.getName() << '\n';
81 }
82
83 void MipsTargetAsmStreamer::emitDirectiveAbiCalls() { OS << "\t.abicalls\n"; }
84 void MipsTargetAsmStreamer::emitDirectiveOptionPic0() {
85   OS << "\t.option\tpic0\n";
86 }
87
88 // This part is for ELF object output.
89 MipsTargetELFStreamer::MipsTargetELFStreamer(MCStreamer &S,
90                                              const MCSubtargetInfo &STI)
91     : MipsTargetStreamer(S), MicroMipsEnabled(false) {
92   MCAssembler &MCA = getStreamer().getAssembler();
93   uint64_t Features = STI.getFeatureBits();
94   Triple T(STI.getTargetTriple());
95
96   // Update e_header flags
97   unsigned EFlags = 0;
98
99   // Architecture
100   if (Features & Mips::FeatureMips64r2)
101     EFlags |= ELF::EF_MIPS_ARCH_64R2;
102   else if (Features & Mips::FeatureMips64)
103     EFlags |= ELF::EF_MIPS_ARCH_64;
104   else if (Features & Mips::FeatureMips32r2)
105     EFlags |= ELF::EF_MIPS_ARCH_32R2;
106   else if (Features & Mips::FeatureMips32)
107     EFlags |= ELF::EF_MIPS_ARCH_32;
108
109   if (T.isArch64Bit()) {
110     EFlags |= ELF::EF_MIPS_ABI2;
111   } else {
112     if (Features & Mips::FeatureMips64r2 || Features & Mips::FeatureMips64)
113       EFlags |= ELF::EF_MIPS_32BITMODE;
114
115     // ABI
116     EFlags |= ELF::EF_MIPS_ABI_O32;
117   }
118
119   MCA.setELFHeaderEFlags(EFlags);
120 }
121
122 void MipsTargetELFStreamer::emitLabel(MCSymbol *Symbol) {
123   if (!isMicroMipsEnabled())
124     return;
125   MCSymbolData &Data = getStreamer().getOrCreateSymbolData(Symbol);
126   uint8_t Type = MCELF::GetType(Data);
127   if (Type != ELF::STT_FUNC)
128     return;
129
130   // The "other" values are stored in the last 6 bits of the second byte
131   // The traditional defines for STO values assume the full byte and thus
132   // the shift to pack it.
133   MCELF::setOther(Data, ELF::STO_MIPS_MICROMIPS >> 2);
134 }
135
136 MCELFStreamer &MipsTargetELFStreamer::getStreamer() {
137   return static_cast<MCELFStreamer &>(Streamer);
138 }
139
140 void MipsTargetELFStreamer::emitDirectiveSetMicroMips() {
141   MicroMipsEnabled = true;
142
143   MCAssembler &MCA = getStreamer().getAssembler();
144   unsigned Flags = MCA.getELFHeaderEFlags();
145   Flags |= ELF::EF_MIPS_MICROMIPS;
146   MCA.setELFHeaderEFlags(Flags);
147 }
148
149 void MipsTargetELFStreamer::emitDirectiveSetNoMicroMips() {
150   MicroMipsEnabled = false;
151 }
152
153 void MipsTargetELFStreamer::emitDirectiveSetMips16() {
154   MCAssembler &MCA = getStreamer().getAssembler();
155   unsigned Flags = MCA.getELFHeaderEFlags();
156   Flags |= ELF::EF_MIPS_ARCH_ASE_M16;
157   MCA.setELFHeaderEFlags(Flags);
158 }
159
160 void MipsTargetELFStreamer::emitDirectiveSetNoMips16() {
161   // FIXME: implement.
162 }
163
164 void MipsTargetELFStreamer::emitDirectiveSetReorder() {
165   // FIXME: implement.
166 }
167
168 void MipsTargetELFStreamer::emitDirectiveSetNoReorder() {
169   MCAssembler &MCA = getStreamer().getAssembler();
170   unsigned Flags = MCA.getELFHeaderEFlags();
171   Flags |= ELF::EF_MIPS_NOREORDER;
172   MCA.setELFHeaderEFlags(Flags);
173 }
174
175 void MipsTargetELFStreamer::emitDirectiveSetMacro() {
176   // FIXME: implement.
177 }
178
179 void MipsTargetELFStreamer::emitDirectiveSetNoMacro() {
180   // FIXME: implement.
181 }
182
183 void MipsTargetELFStreamer::emitDirectiveSetAt() {
184   // FIXME: implement.
185 }
186
187 void MipsTargetELFStreamer::emitDirectiveSetNoAt() {
188   // FIXME: implement.
189 }
190
191 void MipsTargetELFStreamer::emitDirectiveEnd(StringRef Name) {
192   // FIXME: implement.
193 }
194
195 void MipsTargetELFStreamer::emitDirectiveEnt(const MCSymbol &Symbol) {
196   // FIXME: implement.
197 }
198
199 void MipsTargetELFStreamer::emitDirectiveAbiCalls() {
200   MCAssembler &MCA = getStreamer().getAssembler();
201   unsigned Flags = MCA.getELFHeaderEFlags();
202   Flags |= ELF::EF_MIPS_CPIC | ELF::EF_MIPS_PIC;
203   MCA.setELFHeaderEFlags(Flags);
204 }
205 void MipsTargetELFStreamer::emitDirectiveOptionPic0() {
206   MCAssembler &MCA = getStreamer().getAssembler();
207   unsigned Flags = MCA.getELFHeaderEFlags();
208   Flags &= ~ELF::EF_MIPS_PIC;
209   MCA.setELFHeaderEFlags(Flags);
210 }