OpenASIP 2.2
Loading...
Searching...
No Matches
Classes | Public Member Functions | Private Types | Private Member Functions | Private Attributes | List of all members
LabelManager Class Reference

#include <LabelManager.hh>

Collaboration diagram for LabelManager:
Collaboration graph

Classes

struct  InternalLabel
 
struct  InternalRelocation
 

Public Member Functions

 LabelManager (TPEF::Binary &bin, MachineResourceManager &resources, AssemblyParserDiagnostic *parent)
 
 ~LabelManager ()
 
UValue value (std::string &name)
 
TPEF::ASpaceElementaSpaceElement (std::string &labelName)
 
std::string aSpaceName (std::string &labelName)
 
void addLabel (TPEF::ASpaceElement &aSpace, std::string &name, UValue value)
 
void removeLabel (std::string &name)
 
void addProcedure (std::string &name, UValue value)
 
void setGlobal (std::string &labelName)
 
void addRelocation (TPEF::Section &locationSect, TPEF::SectionElement &location, TPEF::ASpaceElement &dstASpace, UValue destination, UValue bitWidth)
 
void clearLastRelocations ()
 
void commitLastRelocations ()
 
void finalize ()
 
void cleanup ()
 
UValue resolveExpressionValue (UValue asmLineNumber, LiteralOrExpression &litOrExpr)
 

Private Types

typedef std::map< std::string, InternalLabel * > SymbolMap
 

Private Member Functions

TPEF::SectionfindSectionByAddress (InternalLabel *currLabel)
 
TPEF::SymbolElementcreateSymbolWithReference (InternalLabel *currLabel, TPEF::Section *ownerSection)
 
void writeProcedureSymbols (TPEF::CodeSection *codeSect, TPEF::SymbolSection *symbolSect, TPEF::StringSection *strings)
 
TPEF::RelocSectionfindOrCreateRelocationSection (InternalRelocation *currReloc, TPEF::SymbolSection *symbolSect)
 
TPEF::SectionfindRelocationDstSection (InternalRelocation *currReloc)
 

Private Attributes

SymbolMap labels_
 Contains stored symbols by their name.
 
std::set< InternalRelocation * > relocs_
 Added relocations.
 
std::set< InternalRelocation * > uncommittedRelocs_
 Uncommitted relocations.
 
std::set< std::string > globals_
 Names of global symbols.
 
std::map< std::string, UValueprocedures_
 Names and addresses of procedure starts.
 
TPEF::Binarybin_
 For finding sections containing relocated elements.
 
MachineResourceManagerresources_
 Common resources of TPEF.
 

Detailed Description

Keeps track of labels, symbols and relocations.

After all label and relocation information if added, class knows how to write SymbolSection and RelocationSectios to TPEF.

Definition at line 64 of file LabelManager.hh.

Member Typedef Documentation

◆ SymbolMap

typedef std::map<std::string, InternalLabel*> LabelManager::SymbolMap
private

Definition at line 148 of file LabelManager.hh.

Constructor & Destructor Documentation

◆ LabelManager()

LabelManager::LabelManager ( TPEF::Binary bin,
MachineResourceManager resources,
AssemblyParserDiagnostic parent 
)

Constructor.

Parameters
binBinary containing data and code sections of relocations.
resourcesResource manager for getting TPEF resources.
parentAssembler root class.

Definition at line 83 of file LabelManager.cc.

86 :
87 bin_(bin), resources_(resources) {
88}
TPEF::Binary & bin_
For finding sections containing relocated elements.
MachineResourceManager & resources_
Common resources of TPEF.

◆ ~LabelManager()

LabelManager::~LabelManager ( )

Destructor.

Definition at line 93 of file LabelManager.cc.

93 {
95
96 while (!relocs_.empty()) {
97 delete *relocs_.begin();
98 relocs_.erase(relocs_.begin());
99 }
100}
SymbolMap labels_
Contains stored symbols by their name.
std::set< InternalRelocation * > relocs_
Added relocations.
static void deleteAllValues(MapType &aMap)

References MapTools::deleteAllValues(), labels_, and relocs_.

Here is the call graph for this function:

Member Function Documentation

◆ addLabel()

void LabelManager::addLabel ( TPEF::ASpaceElement aSpace,
std::string &  name,
UValue  value 
)

Adds a label to manager.

Parameters
aSpaceAddress space of the label to add.
nameName of the label.
valueValue for the label.
Exceptions
ObjectAlreadyExistsLabel by the same name already exists.

Definition at line 160 of file LabelManager.cc.

160 {
161 if(MapTools::containsKey(labels_, name)) {
163 __FILE__, __LINE__, __func__,
164 "Multiply defined lablel: " + name);
165 }
166
167 InternalLabel *newElem = new InternalLabel();
168
169 newElem->name = name;
170 newElem->aSpace = &aSpace;
171 newElem->value = value;
172
173 labels_[name] = newElem;
174}
#define __func__
UValue value(std::string &name)
static bool containsKey(const MapType &aMap, const KeyType &aKey)

References __func__, LabelManager::InternalLabel::aSpace, MapTools::containsKey(), labels_, LabelManager::InternalLabel::name, value(), and LabelManager::InternalLabel::value.

Referenced by AddLabelActor::operator()(), and DataSectionCreator::resolveDataAreaSizesAndLabelAddresses().

Here is the call graph for this function:

◆ addProcedure()

void LabelManager::addProcedure ( std::string &  name,
UValue  address 
)

Adds a procedure to manager.

Parameters
nameName of the procedure
addressStart address of the procedure.
Exceptions
ObjectAlreadyExistsProcedure with a same name is already added.

Definition at line 201 of file LabelManager.cc.

201 {
204 __FILE__, __LINE__, __func__,
205 "Multiply defined procedure: " + name);
206 }
207
208 procedures_[name] = address;
209}
std::map< std::string, UValue > procedures_
Names and addresses of procedure starts.

References __func__, MapTools::containsKey(), and procedures_.

Referenced by AddProcedureActor::operator()().

Here is the call graph for this function:

◆ addRelocation()

void LabelManager::addRelocation ( TPEF::Section locationSect,
TPEF::SectionElement location,
TPEF::ASpaceElement dstASpace,
UValue  destination,
UValue  bitWidth 
)

Adds relocated immediate or data chunk.

Parameters
locationSectSection where relocated element is stored.
locationImmediate or Chunk containing value to relocate.
dstASpaceDestination address space of relocation.
destinationDestination address of relocation.
bitWidthSize of the relocated field.

Definition at line 231 of file LabelManager.cc.

236 {
237
238 InternalRelocation *newElem = new InternalRelocation();
239
240 newElem->locationSect = &locationSect;
241 newElem->location = &location;
242 newElem->dstASpace = &dstASpace;
243 newElem->destination = destination;
244 newElem->bitWidth = bitWidth;
245
246 uncommittedRelocs_.insert(newElem);
247}
std::set< InternalRelocation * > uncommittedRelocs_
Uncommitted relocations.

References LabelManager::InternalRelocation::bitWidth, LabelManager::InternalRelocation::destination, LabelManager::InternalRelocation::dstASpace, LabelManager::InternalRelocation::location, LabelManager::InternalRelocation::locationSect, and uncommittedRelocs_.

Referenced by CodeSectionCreator::finalize(), and DataSectionCreator::writeDataLineToTPEF().

◆ aSpaceElement()

ASpaceElement & LabelManager::aSpaceElement ( std::string &  labelName)

Returns a TPEF address space element for a label by name.

Parameters
labelNameName of the requested label.
Returns
Address space of the label.
Exceptions
SymbolNotFoundIf there is not symbol for requested name.

Definition at line 129 of file LabelManager.cc.

129 {
130 if(!MapTools::containsKey(labels_, labelName)) {
131 throw SymbolNotFound(
132 __FILE__, __LINE__, __func__,
133 "Can't find address address space for label: " + labelName);
134 }
135
136 return *labels_[labelName]->aSpace;
137}

References __func__, MapTools::containsKey(), and labels_.

Referenced by aSpaceName(), CodeSectionCreator::finalize(), and DataSectionCreator::writeDataLineToTPEF().

Here is the call graph for this function:

◆ aSpaceName()

std::string LabelManager::aSpaceName ( std::string &  labelName)

Returns the name of the address space for a label.

Parameters
labelNameName of the requested label.
Returns
Name of the address space the label belongs in.
Exceptions
SymbolNotFoundIf there is not symbol for requested name.

Definition at line 147 of file LabelManager.cc.

147 {
148 return TPEFTools::addressSpaceName(bin_, aSpaceElement(labelName));
149}
TPEF::ASpaceElement & aSpaceElement(std::string &labelName)

References aSpaceElement(), and bin_.

Referenced by CodeSectionCreator::finalize().

Here is the call graph for this function:

◆ cleanup()

void LabelManager::cleanup ( )

Frees all internally allocated data.

Definition at line 263 of file LabelManager.cc.

263 {
267 globals_.clear();
268 procedures_.clear();
269}
std::set< std::string > globals_
Names of global symbols.
static void deleteAllItems(SequenceType &aSequence)

References SequenceTools::deleteAllItems(), MapTools::deleteAllValues(), globals_, labels_, procedures_, relocs_, and uncommittedRelocs_.

Referenced by AssemblerParser::cleanup(), and finalize().

Here is the call graph for this function:

◆ clearLastRelocations()

void LabelManager::clearLastRelocations ( )

Removes relocations that are added but not committed to manager.

Used for cleaning up in error cases.

Definition at line 255 of file LabelManager.cc.

References SequenceTools::deleteAllItems(), and uncommittedRelocs_.

Referenced by CodeSectionCreator::finalize(), and DataSectionCreator::finalize().

Here is the call graph for this function:

◆ commitLastRelocations()

void LabelManager::commitLastRelocations ( )

Adds permanently relocations that are added but not yet committed to manager.

Definition at line 276 of file LabelManager.cc.

276 {
277
278 // add last relocations for writing down
279 for (std::set<InternalRelocation*>::iterator iter =
280 uncommittedRelocs_.begin();
281 iter != uncommittedRelocs_.end(); iter++) {
282
283 relocs_.insert(*iter);
284 }
285
286 uncommittedRelocs_.clear();
287}

References relocs_, and uncommittedRelocs_.

Referenced by CodeSectionCreator::finalize(), and DataSectionCreator::finalize().

◆ createSymbolWithReference()

SymbolElement * LabelManager::createSymbolWithReference ( InternalLabel currLabel,
TPEF::Section ownerSection 
)
private

Creates new TPEF SymbolElement and sets reference field of symbol.

Parameters
currLabelLabel which for symbol element is created.
ownerSectionSection who contains element referred by label.
Returns
Dynamically allocated TPEF symbol with reference field set.

Definition at line 507 of file LabelManager.cc.

509 {
510
511 SymbolElement *symbol = NULL;
512
513 if (ownerSection->isCodeSection()) {
514 CodeSymElement *codeSym = new CodeSymElement();
515 symbol = codeSym;
516
517 CodeSection *codeSection =
518 dynamic_cast<CodeSection*>(ownerSection);
519
520 codeSym->setReference(
521 &codeSection->instruction(currLabel->value));
522
523 } else {
524 DataSymElement *dataSym = new DataSymElement();
525 symbol = dataSym;
526
527 UDataSection *uDataSection =
528 dynamic_cast<UDataSection*>(ownerSection);
529
530 dataSym->setReference(
531 uDataSection->chunk(
532 uDataSection->MAUsToBytes(
533 currLabel->value - ownerSection->startingAddress())));
534 }
535
536 return symbol;
537}
InstructionElement & instruction(Word index) const
void setReference(InstructionElement *aReference)
void setReference(Chunk *aReference)
virtual Chunk * chunk(SectionOffset offset) const
Definition Section.cc:212
virtual Word MAUsToBytes(Word mauCount) const
Definition Section.cc:320
AddressImage startingAddress() const
virtual bool isCodeSection() const
Definition Section.hh:143

References TPEF::RawSection::chunk(), TPEF::CodeSection::instruction(), TPEF::Section::isCodeSection(), TPEF::RawSection::MAUsToBytes(), TPEF::DataSymElement::setReference(), TPEF::CodeSymElement::setReference(), TPEF::Section::startingAddress(), and LabelManager::InternalLabel::value.

Referenced by finalize().

Here is the call graph for this function:

◆ finalize()

void LabelManager::finalize ( )

Creates symbol and relocation tables corresponding to input information.

All destinations must exist in TPEF before this method can be executed, otherwise finalize will throw exception. If exception is thrown finalize method will not do any changes to TPEF. When error that caused the exception is fixed, method can be re-executed and it continues from the same phase until the end.

Exceptions
CompileErrorThere was problems during the resolving.

Definition at line 301 of file LabelManager.cc.

301 {
302 SymbolSection* symbolSect = NULL;
303
304 // write labels
306 NoTypeSymElement* undefSymbol = NULL;
307
308 // create always symbol section with undefined symbol
309 symbolSect = dynamic_cast<SymbolSection*>(
310 Section::createSection(Section::ST_SYMTAB));
311
312 try {
314
315 symbolSect->setLink(strings);
316
317 undefSymbol = new NoTypeSymElement();
318 undefSymbol->setName(strings->string2Chunk(""));
319 symbolSect->addElement(undefSymbol);
320
321 // write code and data labels
322 for (SymbolMap::iterator iter = labels_.begin();
323 iter != labels_.end(); iter++) {
324
325 InternalLabel *currLabel = (*iter).second;
326
327 Section *ownerSection = findSectionByAddress(currLabel);
328 assert(ownerSection != NULL);
329
330 SymbolElement *symbol =
331 createSymbolWithReference(currLabel, ownerSection);
332 assert(symbol != NULL);
333
334 symbol->setSection(ownerSection);
335 symbol->setName(strings->string2Chunk(currLabel->name));
336 symbol->setAbsolute(false);
337
338 // check if global
339 if (ContainerTools::containsValue(globals_, currLabel->name)) {
340 symbol->setBinding(SymbolElement::STB_GLOBAL);
341 globals_.erase(currLabel->name);
342 } else {
343 symbol->setBinding(SymbolElement::STB_LOCAL);
344 }
345
346 symbolSect->addElement(symbol);
347 }
348
349 // if there is global definition without corresponding label
350 if (!globals_.empty()) {
351 throw CompileError(
352 __FILE__, __LINE__, __func__,
353 "Can't set label to be global. " +
354 *(globals_.begin()) + " is undefined.");
355 }
356
357 // write procedure symbols
358 if (bin_.sectionCount(Section::ST_CODE) != 0) {
359
360 CodeSection *codeSect =
361 dynamic_cast<CodeSection*>(
362 bin_.section(Section::ST_CODE, 0));
363
364 assert(codeSect != NULL);
365 writeProcedureSymbols(codeSect, symbolSect, strings);
366
367 } else if (procedures_.size() != 0) {
368 throw CompileError(
369 __FILE__, __LINE__, __func__,
370 "Invalid procedure declaration: " +
371 (*procedures_.begin()).first);
372 }
373
374 // write relocations
375 for (std::set<InternalRelocation*>::iterator iter = relocs_.begin();
376 iter != relocs_.end(); iter++) {
377
378 InternalRelocation *currReloc = *iter;
379
380 // find relocation section for corresponding location section
381 RelocSection* relocSect =
382 findOrCreateRelocationSection(currReloc, symbolSect);
383 assert(relocSect != NULL);
384
385 // find destination section
386 Section* dstSect = findRelocationDstSection(currReloc);
387
388 if (dstSect == NULL) {
389 throw CompileError(
390 __FILE__, __LINE__, __func__,
391 "Can't find destination section for address: "
392 + Conversion::toString(currReloc->destination) +
393 " of adress space: " +
394 TPEFTools::addressSpaceName(bin_, *currReloc->dstASpace));
395 }
396
397 RelocElement *newReloc = new RelocElement();
398
399 newReloc->setLocation(currReloc->location);
400
401 Word dstIndex =
402 currReloc->destination - dstSect->startingAddress();
403
404 // set destination element
405 if (dstSect->isCodeSection()) {
406 CodeSection* codeSect =
407 dynamic_cast<CodeSection*>(dstSect);
408
409 newReloc->setDestination(&codeSect->instruction(dstIndex));
410
411 } else if (dstSect->type() == Section::ST_DATA ||
412 dstSect->type() == Section::ST_LEDATA ||
413 dstSect->type() == Section::ST_UDATA) {
414
415 UDataSection* uDataSect =
416 dynamic_cast<UDataSection*>(dstSect);
417
418 Word byteOffset = uDataSect->MAUsToBytes(dstIndex);
419
420 newReloc->setDestination(uDataSect->chunk(byteOffset));
421
422 } else {
423 abortWithError("Unknown dst section of relocation.");
424 }
425
426 newReloc->setASpace(currReloc->dstASpace);
427 newReloc->setType(RelocElement::RT_SELF);
428 newReloc->setChunked(false);
429 newReloc->setSymbol(undefSymbol);
430 newReloc->setSize(currReloc->bitWidth);
431
432 relocSect->addElement(newReloc);
433 }
434
435 } catch(CompileError& e) {
436 delete symbolSect;
437 throw e;
438 }
439
440 bin_.addSection(symbolSect);
441
442 cleanup();
443}
#define abortWithError(message)
#define assert(condition)
static bool containsValue(const ContainerType &aContainer, const ElementType &aKey)
static std::string toString(const T &source)
TPEF::SymbolElement * createSymbolWithReference(InternalLabel *currLabel, TPEF::Section *ownerSection)
TPEF::RelocSection * findOrCreateRelocationSection(InternalRelocation *currReloc, TPEF::SymbolSection *symbolSect)
TPEF::Section * findSectionByAddress(InternalLabel *currLabel)
void writeProcedureSymbols(TPEF::CodeSection *codeSect, TPEF::SymbolSection *symbolSect, TPEF::StringSection *strings)
TPEF::Section * findRelocationDstSection(InternalRelocation *currReloc)
TPEF::StringSection * stringSection()
TPEF::ASpaceElement * undefinedAddressSpace()
void addSection(Section *section)
Word sectionCount() const
Section * section(Word index) const
void setASpace(ASpaceElement *anASpace)
void setLocation(SectionElement *aLocation)
void setSize(Byte aSize)
void setSymbol(SymbolElement *aSymbol)
void setChunked(bool isChunked)
void setType(RelocType aType)
void setDestination(SectionElement *aDestination)
virtual void addElement(SectionElement *element)
Definition Section.cc:133
void setLink(const ReferenceManager::SafePointer *aLink)
void setASpace(const ReferenceManager::SafePointer *addrSpace)
virtual SectionType type() const =0
Returns SectioType of actual section instance.
Chunk * string2Chunk(const std::string &str)
void setAbsolute(bool anAbsoluteness)
void setName(Chunk *aName)
void setBinding(SymbolBinding aBinding)
void setSection(Section *aSect)

References __func__, abortWithError, TPEF::Section::addElement(), TPEF::Binary::addSection(), assert, bin_, LabelManager::InternalRelocation::bitWidth, TPEF::RawSection::chunk(), cleanup(), ContainerTools::containsValue(), createSymbolWithReference(), LabelManager::InternalRelocation::destination, LabelManager::InternalRelocation::dstASpace, findOrCreateRelocationSection(), findRelocationDstSection(), findSectionByAddress(), globals_, TPEF::CodeSection::instruction(), TPEF::Section::isCodeSection(), labels_, LabelManager::InternalRelocation::location, TPEF::RawSection::MAUsToBytes(), LabelManager::InternalLabel::name, procedures_, relocs_, resources_, TPEF::Binary::section(), TPEF::Binary::sectionCount(), TPEF::SymbolElement::setAbsolute(), TPEF::RelocElement::setASpace(), TPEF::Section::setASpace(), TPEF::SymbolElement::setBinding(), TPEF::RelocElement::setChunked(), TPEF::RelocElement::setDestination(), TPEF::Section::setLink(), TPEF::RelocElement::setLocation(), TPEF::SymbolElement::setName(), TPEF::SymbolElement::setSection(), TPEF::RelocElement::setSize(), TPEF::RelocElement::setSymbol(), TPEF::RelocElement::setType(), TPEF::Section::startingAddress(), TPEF::StringSection::string2Chunk(), MachineResourceManager::stringSection(), Conversion::toString(), TPEF::Section::type(), MachineResourceManager::undefinedAddressSpace(), and writeProcedureSymbols().

Referenced by AssemblerParser::finalize().

Here is the call graph for this function:

◆ findOrCreateRelocationSection()

RelocSection * LabelManager::findOrCreateRelocationSection ( InternalRelocation currReloc,
TPEF::SymbolSection symbolSect 
)
private

Finds or creates relocation section for relocation.

Parameters
currRelocRelocation whose section is looked for.
symbolSectStrings for created section.
Returns
Suitable relocation section for requested relocation.

Definition at line 590 of file LabelManager.cc.

591 {
592
593 RelocSection* relocSect = NULL;
594
595 for (Word j = 0; j < bin_.sectionCount(Section::ST_RELOC); j++) {
596 RelocSection* currSect =
597 dynamic_cast<RelocSection*>(
598 bin_.section(Section::ST_RELOC, j));
599
600 if (currSect->referencedSection() ==
601 currReloc->locationSect) {
602 return currSect;
603 }
604 }
605
606 // none found, create new reloc section
607 if (relocSect == NULL) {
608 relocSect = dynamic_cast<RelocSection*>(
609 Section::createSection(Section::ST_RELOC));
610
611 relocSect->setReferencedSection(currReloc->locationSect);
613 relocSect->setLink(symbolSect);
614
615 bin_.addSection(relocSect);
616 }
617
618 return relocSect;
619}
void setReferencedSection(Section *section)
Section * referencedSection() const

References TPEF::Binary::addSection(), bin_, LabelManager::InternalRelocation::locationSect, TPEF::RelocSection::referencedSection(), resources_, TPEF::Binary::section(), TPEF::Binary::sectionCount(), TPEF::Section::setASpace(), TPEF::Section::setLink(), TPEF::RelocSection::setReferencedSection(), and MachineResourceManager::undefinedAddressSpace().

Referenced by finalize().

Here is the call graph for this function:

◆ findRelocationDstSection()

Section * LabelManager::findRelocationDstSection ( InternalRelocation currReloc)
private

Returns destination section of the relocation.

Parameters
currRelocRelocation whose destination is wanted.
Returns
Destination section of the relocation.

Definition at line 628 of file LabelManager.cc.

628 {
629
630 for (Word j = 0; j < bin_.sectionCount(); j++) {
631 Section* currSect = bin_.section(j);
632
633 // check address space and starting address
634 if (currSect->aSpace() == currReloc->dstASpace &&
635 currSect->startingAddress() <= currReloc->destination) {
636
637 if (currSect->isCodeSection()) {
638 CodeSection* codeSect = dynamic_cast<CodeSection*>(currSect);
639
640 // check that section is enough long
641 if (codeSect->instructionCount() +
642 codeSect->startingAddress() > currReloc->destination) {
643
644 return currSect;
645 }
646
647 } else if (currSect->type() == Section::ST_DATA ||
648 currSect->type() == Section::ST_LEDATA ||
649 currSect->type() == Section::ST_UDATA) {
650
651 UDataSection* uDataSect =
652 dynamic_cast<UDataSection*>(currSect);
653
654 // check that section is enough long
655 if (uDataSect->lengthInMAUs() +
656 uDataSect->startingAddress() > currReloc->destination) {
657
658 return currSect;
659 }
660 }
661 }
662 }
663
664 return NULL;
665}
Word instructionCount() const
virtual Word lengthInMAUs() const
Definition Section.cc:285
ASpaceElement * aSpace() const

References TPEF::Section::aSpace(), bin_, LabelManager::InternalRelocation::destination, LabelManager::InternalRelocation::dstASpace, TPEF::CodeSection::instructionCount(), TPEF::Section::isCodeSection(), TPEF::RawSection::lengthInMAUs(), TPEF::Binary::section(), TPEF::Binary::sectionCount(), TPEF::Section::startingAddress(), and TPEF::Section::type().

Referenced by finalize().

Here is the call graph for this function:

◆ findSectionByAddress()

Section * LabelManager::findSectionByAddress ( InternalLabel currLabel)
private

Returns section that contains the address stored in label.

Parameters
currLabelLabel whose destination section is returned.
Returns
Section that contains the address stored in label.

Definition at line 452 of file LabelManager.cc.

452 {
453
454 // find section by address space
455 for (Word i = 0; i < bin_.sectionCount(); i++) {
456 Section *currSect = bin_.section(i);
457
458 if (currSect->aSpace() == currLabel->aSpace) {
459
460 // address must be found from section
461 if (currSect->type() == Section::ST_DATA ||
462 currSect->type() == Section::ST_LEDATA ||
463 currSect->type() == Section::ST_UDATA) {
464
465 UDataSection* sect =
466 dynamic_cast<UDataSection*>(currSect);
467
468 assert(sect != NULL);
469
470 if (currLabel->value >= sect->startingAddress() &&
471 currLabel->value - sect->startingAddress() <
472 sect->lengthInMAUs()) {
473
474 return currSect;
475 }
476
477 } else if (currSect->isCodeSection()) {
478
479 CodeSection* sect = dynamic_cast<CodeSection*>(currSect);
480
481 assert(sect != NULL);
482
483 if (currLabel->value >= sect->startingAddress() &&
484 currLabel->value - sect->startingAddress() <
485 sect->instructionCount()) {
486
487 return currSect;
488 }
489
490 } else {
491 abortWithError("Label must point to data or code section.");
492 }
493 }
494
495 }
496 return NULL;
497}

References abortWithError, LabelManager::InternalLabel::aSpace, TPEF::Section::aSpace(), assert, bin_, TPEF::CodeSection::instructionCount(), TPEF::Section::isCodeSection(), TPEF::RawSection::lengthInMAUs(), TPEF::Binary::section(), TPEF::Binary::sectionCount(), TPEF::Section::startingAddress(), TPEF::Section::type(), and LabelManager::InternalLabel::value.

Referenced by finalize().

Here is the call graph for this function:

◆ removeLabel()

void LabelManager::removeLabel ( std::string &  name)

Removes a label from manager.

Parameters
nameName of the label.
Exceptions
SymbolNotFoundLabel is not found.

Definition at line 183 of file LabelManager.cc.

183 {
184 if(!MapTools::containsKey(labels_, name)) {
185 throw SymbolNotFound(
186 __FILE__, __LINE__, __func__,
187 "Can't find label: " + name);
188 }
189
190 labels_.erase(name);
191}

References __func__, MapTools::containsKey(), and labels_.

Referenced by DataSectionCreator::finalize().

Here is the call graph for this function:

◆ resolveExpressionValue()

UValue LabelManager::resolveExpressionValue ( UValue  asmLineNumber,
LiteralOrExpression litOrExpr 
)

Resolves value of label expression.

Parameters
asmLineNumberCurrent assembly code line number error information.
litOrExprExpression whose value to resolve.
labelsLabel manager for resolving label values.
Returns
Value of expression.
Exceptions
CompileErrorThere was error with resolving value.

Definition at line 678 of file LabelManager.cc.

679 {
680 UValue retVal = 0;
681
682 try {
683 retVal = value(litOrExpr.expression.label);
684
685 } catch (SymbolNotFound& e) {
686 CompileError error(
687 __FILE__, __LINE__, __func__,
688 e.errorMessage());
689
690 error.setCause(e);
691 error.setCodeFileLineNumber(asmLineNumber);
692
693 throw error;
694 }
695
696 if (litOrExpr.expression.hasOffset) {
697 if (litOrExpr.expression.isMinus) {
698 retVal -= litOrExpr.expression.offset;
699 } else {
700 retVal += litOrExpr.expression.offset;
701 }
702 }
703
704 // check if value matches with label + offset
705 if(litOrExpr.expression.hasValue) {
706 if (litOrExpr.expression.value != retVal) {
707
708 CompileError error(
709 __FILE__, __LINE__, __func__,
710 "Defined expression value (" +
712 ") does not match with resolved (" +
713 Conversion::toString(retVal) +
714 ") one.");
715
716 error.setCodeFileLineNumber(asmLineNumber);
717
718 throw error;
719 }
720 }
721
722 return retVal;
723}
unsigned long UValue
std::string errorMessage() const
Definition Exception.cc:123
bool hasOffset
Is offset defined.
bool isMinus
Is offset minus.
UValue value
Resolved value.
bool hasValue
Is resolved value defined in struct.
std::string label
Name of the label.
UValue offset
Value of offset.
Expression expression
If expression the expression, Otherwise not used.

References __func__, Exception::errorMessage(), LiteralOrExpression::expression, Expression::hasOffset, Expression::hasValue, Expression::isMinus, Expression::label, Expression::offset, Exception::setCause(), CompileError::setCodeFileLineNumber(), Conversion::toString(), value(), and Expression::value.

Referenced by CodeSectionCreator::addAnnotationes(), CodeSectionCreator::finalize(), and DataSectionCreator::writeDataLineToTPEF().

Here is the call graph for this function:

◆ setGlobal()

void LabelManager::setGlobal ( std::string &  labelName)

Set's label to be global label.

Parameters
labelNameName of label to set global.

Definition at line 217 of file LabelManager.cc.

217 {
218 globals_.insert(labelName);
219}

References globals_.

Referenced by SetGlobalActor::operator()().

◆ value()

UValue LabelManager::value ( std::string &  name)

Returns value of a label by name.

Parameters
nameName of the requested label.
Returns
Value of the label.
Exceptions
SymbolNotFoundIf there is not symbol for requested name.

Definition at line 110 of file LabelManager.cc.

110 {
111 if(!MapTools::containsKey(labels_, name)) {
112
113 throw SymbolNotFound(
114 __FILE__, __LINE__, __func__,
115 "Can't find value for label: " + name);
116 }
117
118 return labels_[name]->value;
119}

References __func__, MapTools::containsKey(), and labels_.

Referenced by addLabel(), and resolveExpressionValue().

Here is the call graph for this function:

◆ writeProcedureSymbols()

void LabelManager::writeProcedureSymbols ( TPEF::CodeSection codeSect,
TPEF::SymbolSection symbolSect,
TPEF::StringSection strings 
)
private

Writes all procedure symbols of code section to symbol section.

Parameters
codeSectSection whose symbols are written.
symbolSectSection where to symbols are written.
stringSectiong where symbol strings are stored.
Exceptions
CompileErrorProcedure declaration is is not in code section.

Definition at line 548 of file LabelManager.cc.

549 {
550 for (std::map<std::string, UValue>::iterator
551 iter = procedures_.begin();
552 iter != procedures_.end(); iter++) {
553
554 std::string name = (*iter).first;
555 UValue address = (*iter).second;
556
557 if (address >=
558 codeSect->instructionCount() +
559 codeSect->startingAddress()) {
560
561 throw CompileError(
562 __FILE__, __LINE__, __func__,
563 "Invalid procedure declaration: " +
564 name);
565 }
566
567 ProcedSymElement *procedSym = new ProcedSymElement();
568
569 procedSym->setReference(
570 &codeSect->instruction(
571 address - codeSect->startingAddress()));
572
573 procedSym->setSection(codeSect);
574 procedSym->setName(strings->string2Chunk(name));
575 procedSym->setAbsolute(false);
576 procedSym->setBinding(SymbolElement::STB_LOCAL);
577
578 symbolSect->addElement(procedSym);
579 }
580}

References __func__, TPEF::Section::addElement(), TPEF::CodeSection::instruction(), TPEF::CodeSection::instructionCount(), procedures_, TPEF::SymbolElement::setAbsolute(), TPEF::SymbolElement::setBinding(), TPEF::SymbolElement::setName(), TPEF::CodeSymElement::setReference(), TPEF::SymbolElement::setSection(), TPEF::Section::startingAddress(), and TPEF::StringSection::string2Chunk().

Referenced by finalize().

Here is the call graph for this function:

Member Data Documentation

◆ bin_

TPEF::Binary& LabelManager::bin_
private

For finding sections containing relocated elements.

Definition at line 166 of file LabelManager.hh.

Referenced by aSpaceName(), finalize(), findOrCreateRelocationSection(), findRelocationDstSection(), and findSectionByAddress().

◆ globals_

std::set<std::string> LabelManager::globals_
private

Names of global symbols.

Definition at line 160 of file LabelManager.hh.

Referenced by cleanup(), finalize(), and setGlobal().

◆ labels_

SymbolMap LabelManager::labels_
private

Contains stored symbols by their name.

Definition at line 151 of file LabelManager.hh.

Referenced by addLabel(), aSpaceElement(), cleanup(), finalize(), removeLabel(), value(), and ~LabelManager().

◆ procedures_

std::map<std::string, UValue> LabelManager::procedures_
private

Names and addresses of procedure starts.

Definition at line 163 of file LabelManager.hh.

Referenced by addProcedure(), cleanup(), finalize(), and writeProcedureSymbols().

◆ relocs_

std::set<InternalRelocation*> LabelManager::relocs_
private

Added relocations.

Definition at line 154 of file LabelManager.hh.

Referenced by cleanup(), commitLastRelocations(), finalize(), and ~LabelManager().

◆ resources_

MachineResourceManager& LabelManager::resources_
private

Common resources of TPEF.

Definition at line 169 of file LabelManager.hh.

Referenced by finalize(), and findOrCreateRelocationSection().

◆ uncommittedRelocs_

std::set<InternalRelocation*> LabelManager::uncommittedRelocs_
private

Uncommitted relocations.

Definition at line 157 of file LabelManager.hh.

Referenced by addRelocation(), cleanup(), clearLastRelocations(), and commitLastRelocations().


The documentation for this class was generated from the following files: