blob: 6a6841a48d914e97c404130fd958c66dacdbba4f [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"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000013#include "llvm/MC/MCSymbol.h"
Chris Lattner7c5b0212009-10-19 22:49:00 +000014#include "llvm/ADT/SmallString.h"
15#include "llvm/ADT/Twine.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000016using namespace llvm;
17
Chris Lattnerf0559e42010-04-08 20:30:37 +000018typedef StringMap<const MCSectionMachO*> MachOUniqueMapTy;
19
20
Chris Lattnerc18409a2010-03-11 22:53:35 +000021MCContext::MCContext(const MCAsmInfo &mai) : MAI(mai), NextUniqueID(0) {
Chris Lattnerf0559e42010-04-08 20:30:37 +000022 MachOUniquingMap = 0;
Daniel Dunbarecc63f82009-06-23 22:01:43 +000023}
24
25MCContext::~MCContext() {
Chris Lattnerf0559e42010-04-08 20:30:37 +000026 // NOTE: The symbols are all allocated out of a bump pointer allocator,
Chris Lattnerc9d31522009-08-13 00:21:53 +000027 // we don't need to free them here.
Chris Lattnerf0559e42010-04-08 20:30:37 +000028
29 // If we have the MachO uniquing map, free it.
30 delete (MachOUniqueMapTy*)MachOUniquingMap;
Daniel Dunbarecc63f82009-06-23 22:01:43 +000031}
32
Chris Lattnerf0559e42010-04-08 20:30:37 +000033//===----------------------------------------------------------------------===//
34// Symbol Manipulation
35//===----------------------------------------------------------------------===//
36
Chris Lattner9b97a732010-03-30 18:10:53 +000037MCSymbol *MCContext::GetOrCreateSymbol(StringRef Name) {
Chris Lattner00685bb2010-03-10 01:29:27 +000038 assert(!Name.empty() && "Normal symbols cannot be unnamed!");
Chris Lattnerc28cc092010-03-15 06:15:35 +000039
Chris Lattner9b97a732010-03-30 18:10:53 +000040 // Determine whether this is an assembler temporary or normal label.
41 bool isTemporary = Name.startswith(MAI.getPrivateGlobalPrefix());
42
Chris Lattnerc28cc092010-03-15 06:15:35 +000043 // Do the lookup and get the entire StringMapEntry. We want access to the
44 // key if we are creating the entry.
45 StringMapEntry<MCSymbol*> &Entry = Symbols.GetOrCreateValue(Name);
46 if (Entry.getValue()) return Entry.getValue();
Chris Lattnerc69485e2009-06-24 04:31:49 +000047
Chris Lattnerc28cc092010-03-15 06:15:35 +000048 // Ok, the entry doesn't already exist. Have the MCSymbol object itself refer
49 // to the copy of the string that is embedded in the StringMapEntry.
50 MCSymbol *Result = new (*this) MCSymbol(Entry.getKey(), isTemporary);
51 Entry.setValue(Result);
52 return Result;
Chris Lattnerc69485e2009-06-24 04:31:49 +000053}
54
Chris Lattner9b97a732010-03-30 18:10:53 +000055MCSymbol *MCContext::GetOrCreateSymbol(const Twine &Name) {
Chris Lattner7c5b0212009-10-19 22:49:00 +000056 SmallString<128> NameSV;
57 Name.toVector(NameSV);
Chris Lattner9b97a732010-03-30 18:10:53 +000058 return GetOrCreateSymbol(NameSV.str());
Chris Lattner7c5b0212009-10-19 22:49:00 +000059}
60
Chris Lattner1d72a762010-03-14 08:23:30 +000061MCSymbol *MCContext::CreateTempSymbol() {
Chris Lattner9b97a732010-03-30 18:10:53 +000062 return GetOrCreateSymbol(Twine(MAI.getPrivateGlobalPrefix()) +
63 "tmp" + Twine(NextUniqueID++));
Chris Lattner1d72a762010-03-14 08:23:30 +000064}
65
Daniel Dunbar2928c832009-11-06 10:58:06 +000066MCSymbol *MCContext::LookupSymbol(StringRef Name) const {
Daniel Dunbarecc63f82009-06-23 22:01:43 +000067 return Symbols.lookup(Name);
68}
Chris Lattnerf0559e42010-04-08 20:30:37 +000069
70//===----------------------------------------------------------------------===//
71// Section Management
72//===----------------------------------------------------------------------===//
73
74const MCSectionMachO *MCContext::
75getMachOSection(StringRef Segment, StringRef Section,
76 unsigned TypeAndAttributes,
77 unsigned Reserved2, SectionKind Kind) {
78
79 // We unique sections by their segment/section pair. The returned section
80 // may not have the same flags as the requested section, if so this should be
81 // diagnosed by the client as an error.
82
83 // Create the map if it doesn't already exist.
84 if (MachOUniquingMap == 0)
85 MachOUniquingMap = new MachOUniqueMapTy();
86 MachOUniqueMapTy &Map = *(MachOUniqueMapTy*)MachOUniquingMap;
87
88 // Form the name to look up.
89 SmallString<64> Name;
90 Name += Segment;
91 Name.push_back(',');
92 Name += Section;
93
94 // Do the lookup, if we have a hit, return it.
95 const MCSectionMachO *&Entry = Map[Name.str()];
96 if (Entry) return Entry;
97
98 // Otherwise, return a new section.
99 return Entry = MCSectionMachO::Create(Segment, Section, TypeAndAttributes,
100 Reserved2, Kind, *this);
101}