blob: e02cbc74622dfc95d9b0b8c6ce060de37e8836e9 [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"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000012#include "llvm/MC/MCSection.h"
13#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 Lattnerc18409a2010-03-11 22:53:35 +000018MCContext::MCContext(const MCAsmInfo &mai) : MAI(mai), NextUniqueID(0) {
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
Chris Lattner9b97a732010-03-30 18:10:53 +000026MCSymbol *MCContext::GetOrCreateSymbol(StringRef Name) {
Chris Lattner00685bb2010-03-10 01:29:27 +000027 assert(!Name.empty() && "Normal symbols cannot be unnamed!");
Chris Lattnerc28cc092010-03-15 06:15:35 +000028
Chris Lattner9b97a732010-03-30 18:10:53 +000029 // Determine whether this is an assembler temporary or normal label.
30 bool isTemporary = Name.startswith(MAI.getPrivateGlobalPrefix());
31
Chris Lattnerc28cc092010-03-15 06:15:35 +000032 // Do the lookup and get the entire StringMapEntry. We want access to the
33 // key if we are creating the entry.
34 StringMapEntry<MCSymbol*> &Entry = Symbols.GetOrCreateValue(Name);
35 if (Entry.getValue()) return Entry.getValue();
Chris Lattnerc69485e2009-06-24 04:31:49 +000036
Chris Lattnerc28cc092010-03-15 06:15:35 +000037 // Ok, the entry doesn't already exist. Have the MCSymbol object itself refer
38 // to the copy of the string that is embedded in the StringMapEntry.
39 MCSymbol *Result = new (*this) MCSymbol(Entry.getKey(), isTemporary);
40 Entry.setValue(Result);
41 return Result;
Chris Lattnerc69485e2009-06-24 04:31:49 +000042}
43
Chris Lattner9b97a732010-03-30 18:10:53 +000044MCSymbol *MCContext::GetOrCreateSymbol(const Twine &Name) {
Chris Lattner7c5b0212009-10-19 22:49:00 +000045 SmallString<128> NameSV;
46 Name.toVector(NameSV);
Chris Lattner9b97a732010-03-30 18:10:53 +000047 return GetOrCreateSymbol(NameSV.str());
Chris Lattner7c5b0212009-10-19 22:49:00 +000048}
49
Chris Lattner1d72a762010-03-14 08:23:30 +000050MCSymbol *MCContext::CreateTempSymbol() {
Chris Lattner9b97a732010-03-30 18:10:53 +000051 return GetOrCreateSymbol(Twine(MAI.getPrivateGlobalPrefix()) +
52 "tmp" + Twine(NextUniqueID++));
Chris Lattner1d72a762010-03-14 08:23:30 +000053}
54
Daniel Dunbar2928c832009-11-06 10:58:06 +000055MCSymbol *MCContext::LookupSymbol(StringRef Name) const {
Daniel Dunbarecc63f82009-06-23 22:01:43 +000056 return Symbols.lookup(Name);
57}