blob: 5cc72e8e9f555162763198f37c20b48cc1d3d4bd [file] [log] [blame]
Daniel Dunbarba1da8a2009-06-23 23:39:15 +00001//===- lib/MC/MCContext.cpp - Machine Code Context ------------------------===//
Daniel Dunbarecc63f82009-06-23 22:01:43 +00002//
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"
Chris Lattner1a5c28f2010-03-11 22:56:10 +000011#include "llvm/MC/MCAsmInfo.h"
Chris Lattnerf0559e42010-04-08 20:30:37 +000012#include "llvm/MC/MCSectionMachO.h"
Chris Lattner74aae472010-04-08 21:26:26 +000013#include "llvm/MC/MCSectionELF.h"
Chris Lattnereb40a0f2010-05-07 17:17:41 +000014#include "llvm/MC/MCSectionCOFF.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000015#include "llvm/MC/MCSymbol.h"
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +000016#include "llvm/MC/MCLabel.h"
Chris Lattner7c5b0212009-10-19 22:49:00 +000017#include "llvm/ADT/SmallString.h"
18#include "llvm/ADT/Twine.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000019using namespace llvm;
20
Chris Lattnerf0559e42010-04-08 20:30:37 +000021typedef StringMap<const MCSectionMachO*> MachOUniqueMapTy;
Chris Lattner74aae472010-04-08 21:26:26 +000022typedef StringMap<const MCSectionELF*> ELFUniqueMapTy;
Chris Lattnereb40a0f2010-05-07 17:17:41 +000023typedef StringMap<const MCSectionCOFF*> COFFUniqueMapTy;
Chris Lattnerf0559e42010-04-08 20:30:37 +000024
25
Chris Lattnerc18409a2010-03-11 22:53:35 +000026MCContext::MCContext(const MCAsmInfo &mai) : MAI(mai), NextUniqueID(0) {
Chris Lattnerf0559e42010-04-08 20:30:37 +000027 MachOUniquingMap = 0;
Chris Lattner74aae472010-04-08 21:26:26 +000028 ELFUniquingMap = 0;
Chris Lattnereb40a0f2010-05-07 17:17:41 +000029 COFFUniquingMap = 0;
Daniel Dunbarecc63f82009-06-23 22:01:43 +000030}
31
32MCContext::~MCContext() {
Chris Lattnerf0559e42010-04-08 20:30:37 +000033 // NOTE: The symbols are all allocated out of a bump pointer allocator,
Chris Lattnerc9d31522009-08-13 00:21:53 +000034 // we don't need to free them here.
Chris Lattnerf0559e42010-04-08 20:30:37 +000035
36 // If we have the MachO uniquing map, free it.
37 delete (MachOUniqueMapTy*)MachOUniquingMap;
Chris Lattner74aae472010-04-08 21:26:26 +000038 delete (ELFUniqueMapTy*)ELFUniquingMap;
Chris Lattnereb40a0f2010-05-07 17:17:41 +000039 delete (COFFUniqueMapTy*)COFFUniquingMap;
Daniel Dunbarecc63f82009-06-23 22:01:43 +000040}
41
Chris Lattnerf0559e42010-04-08 20:30:37 +000042//===----------------------------------------------------------------------===//
43// Symbol Manipulation
44//===----------------------------------------------------------------------===//
45
Chris Lattner9b97a732010-03-30 18:10:53 +000046MCSymbol *MCContext::GetOrCreateSymbol(StringRef Name) {
Chris Lattner00685bb2010-03-10 01:29:27 +000047 assert(!Name.empty() && "Normal symbols cannot be unnamed!");
Chris Lattnerc28cc092010-03-15 06:15:35 +000048
Chris Lattner9b97a732010-03-30 18:10:53 +000049 // Determine whether this is an assembler temporary or normal label.
50 bool isTemporary = Name.startswith(MAI.getPrivateGlobalPrefix());
51
Chris Lattnerc28cc092010-03-15 06:15:35 +000052 // 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();
Chris Lattnerc69485e2009-06-24 04:31:49 +000056
Chris Lattnerc28cc092010-03-15 06:15:35 +000057 // 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;
Chris Lattnerc69485e2009-06-24 04:31:49 +000062}
63
Chris Lattner9b97a732010-03-30 18:10:53 +000064MCSymbol *MCContext::GetOrCreateSymbol(const Twine &Name) {
Chris Lattner7c5b0212009-10-19 22:49:00 +000065 SmallString<128> NameSV;
66 Name.toVector(NameSV);
Chris Lattner9b97a732010-03-30 18:10:53 +000067 return GetOrCreateSymbol(NameSV.str());
Chris Lattner7c5b0212009-10-19 22:49:00 +000068}
69
Chris Lattner1d72a762010-03-14 08:23:30 +000070MCSymbol *MCContext::CreateTempSymbol() {
Chris Lattner9b97a732010-03-30 18:10:53 +000071 return GetOrCreateSymbol(Twine(MAI.getPrivateGlobalPrefix()) +
72 "tmp" + Twine(NextUniqueID++));
Chris Lattner1d72a762010-03-14 08:23:30 +000073}
74
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +000075unsigned MCContext::NextInstance(int64_t LocalLabelVal) {
76 unsigned Instance;
77 MCLabel *Label;
78 Label = Instances[LocalLabelVal];
79 if (Label) {
80 Instance = Label->incInstance();
81 }
82 else {
83 Instance = 1;
84 Label = new MCLabel(Instance);
85 Instances[LocalLabelVal] = Label;
86 }
87 return Instance;
88}
89
90unsigned MCContext::GetInstance(int64_t LocalLabelVal) {
91 int Instance;
92 MCLabel *Label;
93 Label = Instances[LocalLabelVal];
94 if (Label) {
95 Instance = Label->getInstance();
96 }
97 else {
98 Instance = 0;
99 Label = new MCLabel(Instance);
100 Instances[LocalLabelVal] = Label;
101 }
102 return Instance;
103}
104
105MCSymbol *MCContext::CreateDirectionalLocalSymbol(int64_t LocalLabelVal) {
106 return GetOrCreateSymbol(Twine(MAI.getPrivateGlobalPrefix()) +
107 Twine(LocalLabelVal) +
108 "\2" +
109 Twine(NextInstance(LocalLabelVal)));
110}
111MCSymbol *MCContext::GetDirectionalLocalSymbol(int64_t LocalLabelVal,
112 int bORf) {
113 return GetOrCreateSymbol(Twine(MAI.getPrivateGlobalPrefix()) +
114 Twine(LocalLabelVal) +
115 "\2" +
116 Twine(GetInstance(LocalLabelVal) + bORf));
117}
118
Daniel Dunbar2928c832009-11-06 10:58:06 +0000119MCSymbol *MCContext::LookupSymbol(StringRef Name) const {
Daniel Dunbarecc63f82009-06-23 22:01:43 +0000120 return Symbols.lookup(Name);
121}
Chris Lattnerf0559e42010-04-08 20:30:37 +0000122
123//===----------------------------------------------------------------------===//
124// Section Management
125//===----------------------------------------------------------------------===//
126
127const MCSectionMachO *MCContext::
128getMachOSection(StringRef Segment, StringRef Section,
129 unsigned TypeAndAttributes,
130 unsigned Reserved2, SectionKind Kind) {
131
132 // We unique sections by their segment/section pair. The returned section
133 // may not have the same flags as the requested section, if so this should be
134 // diagnosed by the client as an error.
135
136 // Create the map if it doesn't already exist.
137 if (MachOUniquingMap == 0)
138 MachOUniquingMap = new MachOUniqueMapTy();
139 MachOUniqueMapTy &Map = *(MachOUniqueMapTy*)MachOUniquingMap;
140
141 // Form the name to look up.
142 SmallString<64> Name;
143 Name += Segment;
144 Name.push_back(',');
145 Name += Section;
146
147 // Do the lookup, if we have a hit, return it.
148 const MCSectionMachO *&Entry = Map[Name.str()];
149 if (Entry) return Entry;
150
151 // Otherwise, return a new section.
Chris Lattner74aae472010-04-08 21:26:26 +0000152 return Entry = new (*this) MCSectionMachO(Segment, Section, TypeAndAttributes,
153 Reserved2, Kind);
Chris Lattnerf0559e42010-04-08 20:30:37 +0000154}
Chris Lattner74aae472010-04-08 21:26:26 +0000155
156
157const MCSection *MCContext::
158getELFSection(StringRef Section, unsigned Type, unsigned Flags,
159 SectionKind Kind, bool IsExplicit) {
160 if (ELFUniquingMap == 0)
161 ELFUniquingMap = new ELFUniqueMapTy();
162 ELFUniqueMapTy &Map = *(ELFUniqueMapTy*)ELFUniquingMap;
163
164 // Do the lookup, if we have a hit, return it.
165 StringMapEntry<const MCSectionELF*> &Entry = Map.GetOrCreateValue(Section);
166 if (Entry.getValue()) return Entry.getValue();
167
168 MCSectionELF *Result = new (*this) MCSectionELF(Entry.getKey(), Type, Flags,
169 Kind, IsExplicit);
170 Entry.setValue(Result);
171 return Result;
172}
173
Chris Lattner6e5ce282010-05-07 21:49:09 +0000174const MCSection *MCContext::getCOFFSection(StringRef Section,
175 unsigned Characteristics,
176 int Selection,
177 SectionKind Kind) {
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000178 if (COFFUniquingMap == 0)
179 COFFUniquingMap = new COFFUniqueMapTy();
180 COFFUniqueMapTy &Map = *(COFFUniqueMapTy*)COFFUniquingMap;
181
182 // Do the lookup, if we have a hit, return it.
183 StringMapEntry<const MCSectionCOFF*> &Entry = Map.GetOrCreateValue(Section);
184 if (Entry.getValue()) return Entry.getValue();
185
Chris Lattner6e5ce282010-05-07 21:49:09 +0000186 MCSectionCOFF *Result = new (*this) MCSectionCOFF(Entry.getKey(),
187 Characteristics,
188 Selection, Kind);
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000189
190 Entry.setValue(Result);
191 return Result;
192}