blob: ae9a30a95f4815cc8e09977e865747b28d6b9583 [file] [log] [blame]
Daniel Dunbarbadeace2009-06-23 23:39:15 +00001//===- lib/MC/MCContext.cpp - Machine Code Context ------------------------===//
Daniel Dunbarca29e4d2009-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 Lattnerd3691dd2010-03-11 22:56:10 +000011#include "llvm/MC/MCAsmInfo.h"
Daniel Dunbarca29e4d2009-06-23 22:01:43 +000012#include "llvm/MC/MCSection.h"
13#include "llvm/MC/MCSymbol.h"
Chris Lattner86dfd732009-10-19 22:49:00 +000014#include "llvm/ADT/SmallString.h"
15#include "llvm/ADT/Twine.h"
Daniel Dunbarca29e4d2009-06-23 22:01:43 +000016using namespace llvm;
17
Chris Lattner768ea2a2010-03-11 22:53:35 +000018MCContext::MCContext(const MCAsmInfo &mai) : MAI(mai), NextUniqueID(0) {
Daniel Dunbarca29e4d2009-06-23 22:01:43 +000019}
20
21MCContext::~MCContext() {
Chris Lattnerb6913162009-08-13 00:21:53 +000022 // NOTE: The sections are all allocated out of a bump pointer allocator,
23 // we don't need to free them here.
Daniel Dunbarca29e4d2009-06-23 22:01:43 +000024}
25
Daniel Dunbarad36e8a2009-11-06 10:58:06 +000026MCSymbol *MCContext::GetOrCreateSymbol(StringRef Name) {
Chris Lattnerb973ea82010-03-10 01:29:27 +000027 assert(!Name.empty() && "Normal symbols cannot be unnamed!");
Chris Lattner3f5738d2009-06-24 04:31:49 +000028 MCSymbol *&Entry = Symbols[Name];
29 if (Entry) return Entry;
30
Daniel Dunbarf782ebc2009-06-24 17:00:42 +000031 return Entry = new (*this) MCSymbol(Name, false);
Chris Lattner3f5738d2009-06-24 04:31:49 +000032}
33
Chris Lattner86dfd732009-10-19 22:49:00 +000034MCSymbol *MCContext::GetOrCreateSymbol(const Twine &Name) {
35 SmallString<128> NameSV;
36 Name.toVector(NameSV);
37 return GetOrCreateSymbol(NameSV.str());
38}
39
Chris Lattner073d8172010-03-14 08:23:30 +000040MCSymbol *MCContext::CreateTempSymbol() {
41 return GetOrCreateTemporarySymbol(Twine(MAI.getPrivateGlobalPrefix()) +
42 "tmp" + Twine(NextUniqueID++));
43}
44
Chris Lattner86dfd732009-10-19 22:49:00 +000045
Chris Lattnerb973ea82010-03-10 01:29:27 +000046MCSymbol *MCContext::GetOrCreateTemporarySymbol(StringRef Name) {
Chris Lattnerd3691dd2010-03-11 22:56:10 +000047 // If there is no name, create a new anonymous symbol.
Chris Lattner073d8172010-03-14 08:23:30 +000048 // FIXME: Remove this. This form of the method should always take a name.
Chris Lattnerd3691dd2010-03-11 22:56:10 +000049 if (Name.empty())
50 return GetOrCreateTemporarySymbol(Twine(MAI.getPrivateGlobalPrefix()) +
51 "tmp" + Twine(NextUniqueID++));
52
Daniel Dunbarca29e4d2009-06-23 22:01:43 +000053 // Otherwise create as usual.
54 MCSymbol *&Entry = Symbols[Name];
Chris Lattnerb973ea82010-03-10 01:29:27 +000055 if (Entry) return Entry;
Daniel Dunbarf782ebc2009-06-24 17:00:42 +000056 return Entry = new (*this) MCSymbol(Name, true);
Daniel Dunbarca29e4d2009-06-23 22:01:43 +000057}
58
Chris Lattnerb973ea82010-03-10 01:29:27 +000059MCSymbol *MCContext::GetOrCreateTemporarySymbol(const Twine &Name) {
60 SmallString<128> NameSV;
61 Name.toVector(NameSV);
62 return GetOrCreateTemporarySymbol(NameSV.str());
63}
64
65
Daniel Dunbarad36e8a2009-11-06 10:58:06 +000066MCSymbol *MCContext::LookupSymbol(StringRef Name) const {
Daniel Dunbarca29e4d2009-06-23 22:01:43 +000067 return Symbols.lookup(Name);
68}