blob: 11370642530a1947e8a8ca127dae9540b62cacfe [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;
Kevin Enderbyf187ac52010-06-28 21:45:58 +000030
31 SecureLogFile = getenv("AS_SECURE_LOG_FILE");
32 SecureLog = 0;
33 SecureLogUsed = false;
Daniel Dunbarecc63f82009-06-23 22:01:43 +000034}
35
36MCContext::~MCContext() {
Chris Lattnerf0559e42010-04-08 20:30:37 +000037 // NOTE: The symbols are all allocated out of a bump pointer allocator,
Chris Lattnerc9d31522009-08-13 00:21:53 +000038 // we don't need to free them here.
Chris Lattnerf0559e42010-04-08 20:30:37 +000039
40 // If we have the MachO uniquing map, free it.
41 delete (MachOUniqueMapTy*)MachOUniquingMap;
Chris Lattner74aae472010-04-08 21:26:26 +000042 delete (ELFUniqueMapTy*)ELFUniquingMap;
Chris Lattnereb40a0f2010-05-07 17:17:41 +000043 delete (COFFUniqueMapTy*)COFFUniquingMap;
Kevin Enderbyf187ac52010-06-28 21:45:58 +000044
45 // If the stream for the .secure_log_unique directive was created free it.
46 delete (raw_ostream*)SecureLog;
Daniel Dunbarecc63f82009-06-23 22:01:43 +000047}
48
Chris Lattnerf0559e42010-04-08 20:30:37 +000049//===----------------------------------------------------------------------===//
50// Symbol Manipulation
51//===----------------------------------------------------------------------===//
52
Chris Lattner9b97a732010-03-30 18:10:53 +000053MCSymbol *MCContext::GetOrCreateSymbol(StringRef Name) {
Chris Lattner00685bb2010-03-10 01:29:27 +000054 assert(!Name.empty() && "Normal symbols cannot be unnamed!");
Chris Lattnerc28cc092010-03-15 06:15:35 +000055
Chris Lattner9b97a732010-03-30 18:10:53 +000056 // Determine whether this is an assembler temporary or normal label.
57 bool isTemporary = Name.startswith(MAI.getPrivateGlobalPrefix());
58
Chris Lattnerc28cc092010-03-15 06:15:35 +000059 // 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();
Chris Lattnerc69485e2009-06-24 04:31:49 +000063
Chris Lattnerc28cc092010-03-15 06:15:35 +000064 // 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;
Chris Lattnerc69485e2009-06-24 04:31:49 +000069}
70
Chris Lattner9b97a732010-03-30 18:10:53 +000071MCSymbol *MCContext::GetOrCreateSymbol(const Twine &Name) {
Chris Lattner7c5b0212009-10-19 22:49:00 +000072 SmallString<128> NameSV;
73 Name.toVector(NameSV);
Chris Lattner9b97a732010-03-30 18:10:53 +000074 return GetOrCreateSymbol(NameSV.str());
Chris Lattner7c5b0212009-10-19 22:49:00 +000075}
76
Chris Lattner1d72a762010-03-14 08:23:30 +000077MCSymbol *MCContext::CreateTempSymbol() {
Chris Lattner9b97a732010-03-30 18:10:53 +000078 return GetOrCreateSymbol(Twine(MAI.getPrivateGlobalPrefix()) +
79 "tmp" + Twine(NextUniqueID++));
Chris Lattner1d72a762010-03-14 08:23:30 +000080}
81
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +000082unsigned MCContext::NextInstance(int64_t LocalLabelVal) {
Benjamin Kramer47f9a492010-05-18 12:15:34 +000083 MCLabel *&Label = Instances[LocalLabelVal];
84 if (!Label)
85 Label = new (*this) MCLabel(0);
86 return Label->incInstance();
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +000087}
88
89unsigned MCContext::GetInstance(int64_t LocalLabelVal) {
Benjamin Kramer47f9a492010-05-18 12:15:34 +000090 MCLabel *&Label = Instances[LocalLabelVal];
91 if (!Label)
92 Label = new (*this) MCLabel(0);
93 return Label->getInstance();
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +000094}
95
96MCSymbol *MCContext::CreateDirectionalLocalSymbol(int64_t LocalLabelVal) {
97 return GetOrCreateSymbol(Twine(MAI.getPrivateGlobalPrefix()) +
98 Twine(LocalLabelVal) +
99 "\2" +
Duncan Sands34727662010-07-12 08:16:59 +0000100 Twine(NextInstance(LocalLabelVal)));
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000101}
102MCSymbol *MCContext::GetDirectionalLocalSymbol(int64_t LocalLabelVal,
103 int bORf) {
104 return GetOrCreateSymbol(Twine(MAI.getPrivateGlobalPrefix()) +
105 Twine(LocalLabelVal) +
106 "\2" +
Duncan Sands34727662010-07-12 08:16:59 +0000107 Twine(GetInstance(LocalLabelVal) + bORf));
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000108}
109
Daniel Dunbar2928c832009-11-06 10:58:06 +0000110MCSymbol *MCContext::LookupSymbol(StringRef Name) const {
Daniel Dunbarecc63f82009-06-23 22:01:43 +0000111 return Symbols.lookup(Name);
112}
Chris Lattnerf0559e42010-04-08 20:30:37 +0000113
114//===----------------------------------------------------------------------===//
115// Section Management
116//===----------------------------------------------------------------------===//
117
118const MCSectionMachO *MCContext::
119getMachOSection(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.
Chris Lattner74aae472010-04-08 21:26:26 +0000143 return Entry = new (*this) MCSectionMachO(Segment, Section, TypeAndAttributes,
144 Reserved2, Kind);
Chris Lattnerf0559e42010-04-08 20:30:37 +0000145}
Chris Lattner74aae472010-04-08 21:26:26 +0000146
147
148const MCSection *MCContext::
149getELFSection(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
Chris Lattner6e5ce282010-05-07 21:49:09 +0000165const MCSection *MCContext::getCOFFSection(StringRef Section,
166 unsigned Characteristics,
167 int Selection,
168 SectionKind Kind) {
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000169 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
Chris Lattner6e5ce282010-05-07 21:49:09 +0000177 MCSectionCOFF *Result = new (*this) MCSectionCOFF(Entry.getKey(),
178 Characteristics,
179 Selection, Kind);
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000180
181 Entry.setValue(Result);
182 return Result;
183}