blob: dc757bb0bffa341e8a4ce249d01aa41a284e884e [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"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000014#include "llvm/MC/MCSymbol.h"
Chris Lattner7c5b0212009-10-19 22:49:00 +000015#include "llvm/ADT/SmallString.h"
16#include "llvm/ADT/Twine.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000017using namespace llvm;
18
Chris Lattnerf0559e42010-04-08 20:30:37 +000019typedef StringMap<const MCSectionMachO*> MachOUniqueMapTy;
Chris Lattner74aae472010-04-08 21:26:26 +000020typedef StringMap<const MCSectionELF*> ELFUniqueMapTy;
Chris Lattnerf0559e42010-04-08 20:30:37 +000021
22
Chris Lattnerc18409a2010-03-11 22:53:35 +000023MCContext::MCContext(const MCAsmInfo &mai) : MAI(mai), NextUniqueID(0) {
Chris Lattnerf0559e42010-04-08 20:30:37 +000024 MachOUniquingMap = 0;
Chris Lattner74aae472010-04-08 21:26:26 +000025 ELFUniquingMap = 0;
Daniel Dunbarecc63f82009-06-23 22:01:43 +000026}
27
28MCContext::~MCContext() {
Chris Lattnerf0559e42010-04-08 20:30:37 +000029 // NOTE: The symbols are all allocated out of a bump pointer allocator,
Chris Lattnerc9d31522009-08-13 00:21:53 +000030 // we don't need to free them here.
Chris Lattnerf0559e42010-04-08 20:30:37 +000031
32 // If we have the MachO uniquing map, free it.
33 delete (MachOUniqueMapTy*)MachOUniquingMap;
Chris Lattner74aae472010-04-08 21:26:26 +000034 delete (ELFUniqueMapTy*)ELFUniquingMap;
Daniel Dunbarecc63f82009-06-23 22:01:43 +000035}
36
Chris Lattnerf0559e42010-04-08 20:30:37 +000037//===----------------------------------------------------------------------===//
38// Symbol Manipulation
39//===----------------------------------------------------------------------===//
40
Chris Lattner9b97a732010-03-30 18:10:53 +000041MCSymbol *MCContext::GetOrCreateSymbol(StringRef Name) {
Chris Lattner00685bb2010-03-10 01:29:27 +000042 assert(!Name.empty() && "Normal symbols cannot be unnamed!");
Chris Lattnerc28cc092010-03-15 06:15:35 +000043
Chris Lattner9b97a732010-03-30 18:10:53 +000044 // Determine whether this is an assembler temporary or normal label.
45 bool isTemporary = Name.startswith(MAI.getPrivateGlobalPrefix());
46
Chris Lattnerc28cc092010-03-15 06:15:35 +000047 // Do the lookup and get the entire StringMapEntry. We want access to the
48 // key if we are creating the entry.
49 StringMapEntry<MCSymbol*> &Entry = Symbols.GetOrCreateValue(Name);
50 if (Entry.getValue()) return Entry.getValue();
Chris Lattnerc69485e2009-06-24 04:31:49 +000051
Chris Lattnerc28cc092010-03-15 06:15:35 +000052 // Ok, the entry doesn't already exist. Have the MCSymbol object itself refer
53 // to the copy of the string that is embedded in the StringMapEntry.
54 MCSymbol *Result = new (*this) MCSymbol(Entry.getKey(), isTemporary);
55 Entry.setValue(Result);
56 return Result;
Chris Lattnerc69485e2009-06-24 04:31:49 +000057}
58
Chris Lattner9b97a732010-03-30 18:10:53 +000059MCSymbol *MCContext::GetOrCreateSymbol(const Twine &Name) {
Chris Lattner7c5b0212009-10-19 22:49:00 +000060 SmallString<128> NameSV;
61 Name.toVector(NameSV);
Chris Lattner9b97a732010-03-30 18:10:53 +000062 return GetOrCreateSymbol(NameSV.str());
Chris Lattner7c5b0212009-10-19 22:49:00 +000063}
64
Chris Lattner1d72a762010-03-14 08:23:30 +000065MCSymbol *MCContext::CreateTempSymbol() {
Chris Lattner9b97a732010-03-30 18:10:53 +000066 return GetOrCreateSymbol(Twine(MAI.getPrivateGlobalPrefix()) +
67 "tmp" + Twine(NextUniqueID++));
Chris Lattner1d72a762010-03-14 08:23:30 +000068}
69
Daniel Dunbar2928c832009-11-06 10:58:06 +000070MCSymbol *MCContext::LookupSymbol(StringRef Name) const {
Daniel Dunbarecc63f82009-06-23 22:01:43 +000071 return Symbols.lookup(Name);
72}
Chris Lattnerf0559e42010-04-08 20:30:37 +000073
74//===----------------------------------------------------------------------===//
75// Section Management
76//===----------------------------------------------------------------------===//
77
78const MCSectionMachO *MCContext::
79getMachOSection(StringRef Segment, StringRef Section,
80 unsigned TypeAndAttributes,
81 unsigned Reserved2, SectionKind Kind) {
82
83 // We unique sections by their segment/section pair. The returned section
84 // may not have the same flags as the requested section, if so this should be
85 // diagnosed by the client as an error.
86
87 // Create the map if it doesn't already exist.
88 if (MachOUniquingMap == 0)
89 MachOUniquingMap = new MachOUniqueMapTy();
90 MachOUniqueMapTy &Map = *(MachOUniqueMapTy*)MachOUniquingMap;
91
92 // Form the name to look up.
93 SmallString<64> Name;
94 Name += Segment;
95 Name.push_back(',');
96 Name += Section;
97
98 // Do the lookup, if we have a hit, return it.
99 const MCSectionMachO *&Entry = Map[Name.str()];
100 if (Entry) return Entry;
101
102 // Otherwise, return a new section.
Chris Lattner74aae472010-04-08 21:26:26 +0000103 return Entry = new (*this) MCSectionMachO(Segment, Section, TypeAndAttributes,
104 Reserved2, Kind);
Chris Lattnerf0559e42010-04-08 20:30:37 +0000105}
Chris Lattner74aae472010-04-08 21:26:26 +0000106
107
108const MCSection *MCContext::
109getELFSection(StringRef Section, unsigned Type, unsigned Flags,
110 SectionKind Kind, bool IsExplicit) {
111 if (ELFUniquingMap == 0)
112 ELFUniquingMap = new ELFUniqueMapTy();
113 ELFUniqueMapTy &Map = *(ELFUniqueMapTy*)ELFUniquingMap;
114
115 // Do the lookup, if we have a hit, return it.
116 StringMapEntry<const MCSectionELF*> &Entry = Map.GetOrCreateValue(Section);
117 if (Entry.getValue()) return Entry.getValue();
118
119 MCSectionELF *Result = new (*this) MCSectionELF(Entry.getKey(), Type, Flags,
120 Kind, IsExplicit);
121 Entry.setValue(Result);
122 return Result;
123}
124
125