blob: 53ffc9409a6459e242bbf634a539a2a689c1f079 [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) {
Benjamin Kramer47f9a492010-05-18 12:15:34 +000076 MCLabel *&Label = Instances[LocalLabelVal];
77 if (!Label)
78 Label = new (*this) MCLabel(0);
79 return Label->incInstance();
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +000080}
81
82unsigned MCContext::GetInstance(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->getInstance();
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +000087}
88
89MCSymbol *MCContext::CreateDirectionalLocalSymbol(int64_t LocalLabelVal) {
90 return GetOrCreateSymbol(Twine(MAI.getPrivateGlobalPrefix()) +
91 Twine(LocalLabelVal) +
92 "\2" +
93 Twine(NextInstance(LocalLabelVal)));
94}
95MCSymbol *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
Daniel Dunbar2928c832009-11-06 10:58:06 +0000103MCSymbol *MCContext::LookupSymbol(StringRef Name) const {
Daniel Dunbarecc63f82009-06-23 22:01:43 +0000104 return Symbols.lookup(Name);
105}
Chris Lattnerf0559e42010-04-08 20:30:37 +0000106
107//===----------------------------------------------------------------------===//
108// Section Management
109//===----------------------------------------------------------------------===//
110
111const MCSectionMachO *MCContext::
112getMachOSection(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.
Chris Lattner74aae472010-04-08 21:26:26 +0000136 return Entry = new (*this) MCSectionMachO(Segment, Section, TypeAndAttributes,
137 Reserved2, Kind);
Chris Lattnerf0559e42010-04-08 20:30:37 +0000138}
Chris Lattner74aae472010-04-08 21:26:26 +0000139
140
141const MCSection *MCContext::
142getELFSection(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
Chris Lattner6e5ce282010-05-07 21:49:09 +0000158const MCSection *MCContext::getCOFFSection(StringRef Section,
159 unsigned Characteristics,
160 int Selection,
161 SectionKind Kind) {
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000162 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
Chris Lattner6e5ce282010-05-07 21:49:09 +0000170 MCSectionCOFF *Result = new (*this) MCSectionCOFF(Entry.getKey(),
171 Characteristics,
172 Selection, Kind);
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000173
174 Entry.setValue(Result);
175 return Result;
176}