blob: 5a65b8ae380a8af453c8eb683da2fbdb7ad7aeb8 [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"
Chris Lattner7c5b0212009-10-19 22:49:00 +000016#include "llvm/ADT/SmallString.h"
17#include "llvm/ADT/Twine.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000018using namespace llvm;
19
Chris Lattnerf0559e42010-04-08 20:30:37 +000020typedef StringMap<const MCSectionMachO*> MachOUniqueMapTy;
Chris Lattner74aae472010-04-08 21:26:26 +000021typedef StringMap<const MCSectionELF*> ELFUniqueMapTy;
Chris Lattnereb40a0f2010-05-07 17:17:41 +000022typedef StringMap<const MCSectionCOFF*> COFFUniqueMapTy;
Chris Lattnerf0559e42010-04-08 20:30:37 +000023
24
Chris Lattnerc18409a2010-03-11 22:53:35 +000025MCContext::MCContext(const MCAsmInfo &mai) : MAI(mai), NextUniqueID(0) {
Chris Lattnerf0559e42010-04-08 20:30:37 +000026 MachOUniquingMap = 0;
Chris Lattner74aae472010-04-08 21:26:26 +000027 ELFUniquingMap = 0;
Chris Lattnereb40a0f2010-05-07 17:17:41 +000028 COFFUniquingMap = 0;
Daniel Dunbarecc63f82009-06-23 22:01:43 +000029}
30
31MCContext::~MCContext() {
Chris Lattnerf0559e42010-04-08 20:30:37 +000032 // NOTE: The symbols are all allocated out of a bump pointer allocator,
Chris Lattnerc9d31522009-08-13 00:21:53 +000033 // we don't need to free them here.
Chris Lattnerf0559e42010-04-08 20:30:37 +000034
35 // If we have the MachO uniquing map, free it.
36 delete (MachOUniqueMapTy*)MachOUniquingMap;
Chris Lattner74aae472010-04-08 21:26:26 +000037 delete (ELFUniqueMapTy*)ELFUniquingMap;
Chris Lattnereb40a0f2010-05-07 17:17:41 +000038 delete (COFFUniqueMapTy*)COFFUniquingMap;
Daniel Dunbarecc63f82009-06-23 22:01:43 +000039}
40
Chris Lattnerf0559e42010-04-08 20:30:37 +000041//===----------------------------------------------------------------------===//
42// Symbol Manipulation
43//===----------------------------------------------------------------------===//
44
Chris Lattner9b97a732010-03-30 18:10:53 +000045MCSymbol *MCContext::GetOrCreateSymbol(StringRef Name) {
Chris Lattner00685bb2010-03-10 01:29:27 +000046 assert(!Name.empty() && "Normal symbols cannot be unnamed!");
Chris Lattnerc28cc092010-03-15 06:15:35 +000047
Chris Lattner9b97a732010-03-30 18:10:53 +000048 // Determine whether this is an assembler temporary or normal label.
49 bool isTemporary = Name.startswith(MAI.getPrivateGlobalPrefix());
50
Chris Lattnerc28cc092010-03-15 06:15:35 +000051 // Do the lookup and get the entire StringMapEntry. We want access to the
52 // key if we are creating the entry.
53 StringMapEntry<MCSymbol*> &Entry = Symbols.GetOrCreateValue(Name);
54 if (Entry.getValue()) return Entry.getValue();
Chris Lattnerc69485e2009-06-24 04:31:49 +000055
Chris Lattnerc28cc092010-03-15 06:15:35 +000056 // Ok, the entry doesn't already exist. Have the MCSymbol object itself refer
57 // to the copy of the string that is embedded in the StringMapEntry.
58 MCSymbol *Result = new (*this) MCSymbol(Entry.getKey(), isTemporary);
59 Entry.setValue(Result);
60 return Result;
Chris Lattnerc69485e2009-06-24 04:31:49 +000061}
62
Chris Lattner9b97a732010-03-30 18:10:53 +000063MCSymbol *MCContext::GetOrCreateSymbol(const Twine &Name) {
Chris Lattner7c5b0212009-10-19 22:49:00 +000064 SmallString<128> NameSV;
65 Name.toVector(NameSV);
Chris Lattner9b97a732010-03-30 18:10:53 +000066 return GetOrCreateSymbol(NameSV.str());
Chris Lattner7c5b0212009-10-19 22:49:00 +000067}
68
Chris Lattner1d72a762010-03-14 08:23:30 +000069MCSymbol *MCContext::CreateTempSymbol() {
Chris Lattner9b97a732010-03-30 18:10:53 +000070 return GetOrCreateSymbol(Twine(MAI.getPrivateGlobalPrefix()) +
71 "tmp" + Twine(NextUniqueID++));
Chris Lattner1d72a762010-03-14 08:23:30 +000072}
73
Daniel Dunbar2928c832009-11-06 10:58:06 +000074MCSymbol *MCContext::LookupSymbol(StringRef Name) const {
Daniel Dunbarecc63f82009-06-23 22:01:43 +000075 return Symbols.lookup(Name);
76}
Chris Lattnerf0559e42010-04-08 20:30:37 +000077
78//===----------------------------------------------------------------------===//
79// Section Management
80//===----------------------------------------------------------------------===//
81
82const MCSectionMachO *MCContext::
83getMachOSection(StringRef Segment, StringRef Section,
84 unsigned TypeAndAttributes,
85 unsigned Reserved2, SectionKind Kind) {
86
87 // We unique sections by their segment/section pair. The returned section
88 // may not have the same flags as the requested section, if so this should be
89 // diagnosed by the client as an error.
90
91 // Create the map if it doesn't already exist.
92 if (MachOUniquingMap == 0)
93 MachOUniquingMap = new MachOUniqueMapTy();
94 MachOUniqueMapTy &Map = *(MachOUniqueMapTy*)MachOUniquingMap;
95
96 // Form the name to look up.
97 SmallString<64> Name;
98 Name += Segment;
99 Name.push_back(',');
100 Name += Section;
101
102 // Do the lookup, if we have a hit, return it.
103 const MCSectionMachO *&Entry = Map[Name.str()];
104 if (Entry) return Entry;
105
106 // Otherwise, return a new section.
Chris Lattner74aae472010-04-08 21:26:26 +0000107 return Entry = new (*this) MCSectionMachO(Segment, Section, TypeAndAttributes,
108 Reserved2, Kind);
Chris Lattnerf0559e42010-04-08 20:30:37 +0000109}
Chris Lattner74aae472010-04-08 21:26:26 +0000110
111
112const MCSection *MCContext::
113getELFSection(StringRef Section, unsigned Type, unsigned Flags,
114 SectionKind Kind, bool IsExplicit) {
115 if (ELFUniquingMap == 0)
116 ELFUniquingMap = new ELFUniqueMapTy();
117 ELFUniqueMapTy &Map = *(ELFUniqueMapTy*)ELFUniquingMap;
118
119 // Do the lookup, if we have a hit, return it.
120 StringMapEntry<const MCSectionELF*> &Entry = Map.GetOrCreateValue(Section);
121 if (Entry.getValue()) return Entry.getValue();
122
123 MCSectionELF *Result = new (*this) MCSectionELF(Entry.getKey(), Type, Flags,
124 Kind, IsExplicit);
125 Entry.setValue(Result);
126 return Result;
127}
128
Chris Lattner6e5ce282010-05-07 21:49:09 +0000129const MCSection *MCContext::getCOFFSection(StringRef Section,
130 unsigned Characteristics,
131 int Selection,
132 SectionKind Kind) {
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000133 if (COFFUniquingMap == 0)
134 COFFUniquingMap = new COFFUniqueMapTy();
135 COFFUniqueMapTy &Map = *(COFFUniqueMapTy*)COFFUniquingMap;
136
137 // Do the lookup, if we have a hit, return it.
138 StringMapEntry<const MCSectionCOFF*> &Entry = Map.GetOrCreateValue(Section);
139 if (Entry.getValue()) return Entry.getValue();
140
Chris Lattner6e5ce282010-05-07 21:49:09 +0000141 MCSectionCOFF *Result = new (*this) MCSectionCOFF(Entry.getKey(),
142 Characteristics,
143 Selection, Kind);
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000144
145 Entry.setValue(Result);
146 return Result;
147}