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