Convert some tab stops into spaces.
[oota-llvm.git] / lib / MC / MCContext.cpp
1 //===- lib/MC/MCContext.cpp - Machine Code Context ------------------------===//
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/MCContext.h"
11 #include "llvm/MC/MCAsmInfo.h"
12 #include "llvm/MC/MCSectionMachO.h"
13 #include "llvm/MC/MCSectionELF.h"
14 #include "llvm/MC/MCSectionCOFF.h"
15 #include "llvm/MC/MCSymbol.h"
16 #include "llvm/MC/MCLabel.h"
17 #include "llvm/ADT/SmallString.h"
18 #include "llvm/ADT/Twine.h"
19 using namespace llvm;
20
21 typedef StringMap<const MCSectionMachO*> MachOUniqueMapTy;
22 typedef StringMap<const MCSectionELF*> ELFUniqueMapTy;
23 typedef StringMap<const MCSectionCOFF*> COFFUniqueMapTy;
24
25
26 MCContext::MCContext(const MCAsmInfo &mai) : MAI(mai), NextUniqueID(0) {
27   MachOUniquingMap = 0;
28   ELFUniquingMap = 0;
29   COFFUniquingMap = 0;
30
31   SecureLogFile = getenv("AS_SECURE_LOG_FILE");
32   SecureLog = 0;
33   SecureLogUsed = false;
34 }
35
36 MCContext::~MCContext() {
37   // NOTE: The symbols are all allocated out of a bump pointer allocator,
38   // we don't need to free them here.
39   
40   // If we have the MachO uniquing map, free it.
41   delete (MachOUniqueMapTy*)MachOUniquingMap;
42   delete (ELFUniqueMapTy*)ELFUniquingMap;
43   delete (COFFUniqueMapTy*)COFFUniquingMap;
44
45   // If the stream for the .secure_log_unique directive was created free it.
46   delete (raw_ostream*)SecureLog;
47 }
48
49 //===----------------------------------------------------------------------===//
50 // Symbol Manipulation
51 //===----------------------------------------------------------------------===//
52
53 MCSymbol *MCContext::GetOrCreateSymbol(StringRef Name) {
54   assert(!Name.empty() && "Normal symbols cannot be unnamed!");
55   
56   // Determine whether this is an assembler temporary or normal label.
57   bool isTemporary = Name.startswith(MAI.getPrivateGlobalPrefix());
58   
59   // Do the lookup and get the entire StringMapEntry.  We want access to the
60   // key if we are creating the entry.
61   StringMapEntry<MCSymbol*> &Entry = Symbols.GetOrCreateValue(Name);
62   if (Entry.getValue()) return Entry.getValue();
63
64   // Ok, the entry doesn't already exist.  Have the MCSymbol object itself refer
65   // to the copy of the string that is embedded in the StringMapEntry.
66   MCSymbol *Result = new (*this) MCSymbol(Entry.getKey(), isTemporary);
67   Entry.setValue(Result);
68   return Result; 
69 }
70
71 MCSymbol *MCContext::GetOrCreateSymbol(const Twine &Name) {
72   SmallString<128> NameSV;
73   Name.toVector(NameSV);
74   return GetOrCreateSymbol(NameSV.str());
75 }
76
77 MCSymbol *MCContext::CreateTempSymbol() {
78   return GetOrCreateSymbol(Twine(MAI.getPrivateGlobalPrefix()) +
79                            "tmp" + Twine(NextUniqueID++));
80 }
81
82 unsigned MCContext::NextInstance(int64_t LocalLabelVal) {
83   MCLabel *&Label = Instances[LocalLabelVal];
84   if (!Label)
85     Label = new (*this) MCLabel(0);
86   return Label->incInstance();
87 }
88
89 unsigned MCContext::GetInstance(int64_t LocalLabelVal) {
90   MCLabel *&Label = Instances[LocalLabelVal];
91   if (!Label)
92     Label = new (*this) MCLabel(0);
93   return Label->getInstance();
94 }
95
96 MCSymbol *MCContext::CreateDirectionalLocalSymbol(int64_t LocalLabelVal) {
97   return GetOrCreateSymbol(Twine(MAI.getPrivateGlobalPrefix()) +
98                            Twine(LocalLabelVal) +
99                            "\2" +
100                            Twine(NextInstance(LocalLabelVal)));
101 }
102 MCSymbol *MCContext::GetDirectionalLocalSymbol(int64_t LocalLabelVal,
103                                                int bORf) {
104   return GetOrCreateSymbol(Twine(MAI.getPrivateGlobalPrefix()) +
105                            Twine(LocalLabelVal) +
106                            "\2" +
107                            Twine(GetInstance(LocalLabelVal) + bORf));
108 }
109
110 MCSymbol *MCContext::LookupSymbol(StringRef Name) const {
111   return Symbols.lookup(Name);
112 }
113
114 //===----------------------------------------------------------------------===//
115 // Section Management
116 //===----------------------------------------------------------------------===//
117
118 const MCSectionMachO *MCContext::
119 getMachOSection(StringRef Segment, StringRef Section,
120                 unsigned TypeAndAttributes,
121                 unsigned Reserved2, SectionKind Kind) {
122   
123   // We unique sections by their segment/section pair.  The returned section
124   // may not have the same flags as the requested section, if so this should be
125   // diagnosed by the client as an error.
126   
127   // Create the map if it doesn't already exist.
128   if (MachOUniquingMap == 0)
129     MachOUniquingMap = new MachOUniqueMapTy();
130   MachOUniqueMapTy &Map = *(MachOUniqueMapTy*)MachOUniquingMap;
131   
132   // Form the name to look up.
133   SmallString<64> Name;
134   Name += Segment;
135   Name.push_back(',');
136   Name += Section;
137   
138   // Do the lookup, if we have a hit, return it.
139   const MCSectionMachO *&Entry = Map[Name.str()];
140   if (Entry) return Entry;
141   
142   // Otherwise, return a new section.
143   return Entry = new (*this) MCSectionMachO(Segment, Section, TypeAndAttributes,
144                                             Reserved2, Kind);
145 }
146
147
148 const MCSection *MCContext::
149 getELFSection(StringRef Section, unsigned Type, unsigned Flags,
150               SectionKind Kind, bool IsExplicit) {
151   if (ELFUniquingMap == 0)
152     ELFUniquingMap = new ELFUniqueMapTy();
153   ELFUniqueMapTy &Map = *(ELFUniqueMapTy*)ELFUniquingMap;
154   
155   // Do the lookup, if we have a hit, return it.
156   StringMapEntry<const MCSectionELF*> &Entry = Map.GetOrCreateValue(Section);
157   if (Entry.getValue()) return Entry.getValue();
158   
159   MCSectionELF *Result = new (*this) MCSectionELF(Entry.getKey(), Type, Flags,
160                                                   Kind, IsExplicit);
161   Entry.setValue(Result);
162   return Result;
163 }
164
165 const MCSection *MCContext::getCOFFSection(StringRef Section,
166                                            unsigned Characteristics,
167                                            int Selection,
168                                            SectionKind Kind) {
169   if (COFFUniquingMap == 0)
170     COFFUniquingMap = new COFFUniqueMapTy();
171   COFFUniqueMapTy &Map = *(COFFUniqueMapTy*)COFFUniquingMap;
172   
173   // Do the lookup, if we have a hit, return it.
174   StringMapEntry<const MCSectionCOFF*> &Entry = Map.GetOrCreateValue(Section);
175   if (Entry.getValue()) return Entry.getValue();
176   
177   MCSectionCOFF *Result = new (*this) MCSectionCOFF(Entry.getKey(),
178                                                     Characteristics,
179                                                     Selection, Kind);
180   
181   Entry.setValue(Result);
182   return Result;
183 }