blob: 09479c595a9ee0b2312d45b39b10e3180fc75a23 [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"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000011#include "llvm/MC/MCSection.h"
12#include "llvm/MC/MCSymbol.h"
Daniel Dunbarba1da8a2009-06-23 23:39:15 +000013#include "llvm/MC/MCValue.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 Lattnerfcdbf4e2009-07-31 16:43:49 +000018MCContext::MCContext() {
Daniel Dunbarecc63f82009-06-23 22:01:43 +000019}
20
21MCContext::~MCContext() {
Chris Lattnerc9d31522009-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 Dunbarecc63f82009-06-23 22:01:43 +000024}
25
Daniel Dunbarb5261eb2009-07-27 21:22:30 +000026MCSymbol *MCContext::CreateSymbol(const StringRef &Name) {
Daniel Dunbarecc63f82009-06-23 22:01:43 +000027 assert(Name[0] != '\0' && "Normal symbols cannot be unnamed!");
28
29 // Create and bind the symbol, and ensure that names are unique.
30 MCSymbol *&Entry = Symbols[Name];
31 assert(!Entry && "Duplicate symbol definition!");
Daniel Dunbar71d259b2009-06-24 17:00:42 +000032 return Entry = new (*this) MCSymbol(Name, false);
Daniel Dunbarecc63f82009-06-23 22:01:43 +000033}
34
Daniel Dunbarb5261eb2009-07-27 21:22:30 +000035MCSymbol *MCContext::GetOrCreateSymbol(const StringRef &Name) {
Chris Lattnerc69485e2009-06-24 04:31:49 +000036 MCSymbol *&Entry = Symbols[Name];
37 if (Entry) return Entry;
38
Daniel Dunbar71d259b2009-06-24 17:00:42 +000039 return Entry = new (*this) MCSymbol(Name, false);
Chris Lattnerc69485e2009-06-24 04:31:49 +000040}
41
Chris Lattner7c5b0212009-10-19 22:49:00 +000042MCSymbol *MCContext::GetOrCreateSymbol(const Twine &Name) {
43 SmallString<128> NameSV;
44 Name.toVector(NameSV);
45 return GetOrCreateSymbol(NameSV.str());
46}
47
48
Daniel Dunbarb5261eb2009-07-27 21:22:30 +000049MCSymbol *MCContext::CreateTemporarySymbol(const StringRef &Name) {
Daniel Dunbarecc63f82009-06-23 22:01:43 +000050 // If unnamed, just create a symbol.
Daniel Dunbarb5261eb2009-07-27 21:22:30 +000051 if (Name.empty())
Daniel Dunbar71d259b2009-06-24 17:00:42 +000052 new (*this) MCSymbol("", true);
Daniel Dunbarecc63f82009-06-23 22:01:43 +000053
54 // Otherwise create as usual.
55 MCSymbol *&Entry = Symbols[Name];
56 assert(!Entry && "Duplicate symbol definition!");
Daniel Dunbar71d259b2009-06-24 17:00:42 +000057 return Entry = new (*this) MCSymbol(Name, true);
Daniel Dunbarecc63f82009-06-23 22:01:43 +000058}
59
Daniel Dunbarb5261eb2009-07-27 21:22:30 +000060MCSymbol *MCContext::LookupSymbol(const StringRef &Name) const {
Daniel Dunbarecc63f82009-06-23 22:01:43 +000061 return Symbols.lookup(Name);
62}