OpenASIP  2.0
Public Member Functions | Private Member Functions | Private Attributes | List of all members
Assembler Class Reference

#include <Assembler.hh>

Collaboration diagram for Assembler:
Collaboration graph

Public Member Functions

 Assembler (TPEF::BinaryStream &assemblerFile, TTAMachine::Machine &assemblerMachine)
 
TPEF::Binarycompile ()
 
const std::set< CompilerMessage > & warnings () const
 
const CompilerMessageerror () const
 
void addWarning (UValue lineNumber, std::string errorMessage)
 
const TTAMachine::MachinetargetMachine () const
 

Private Member Functions

void readAssemblerCode ()
 
std::string codeLine (UValue lineNumber) const
 

Private Attributes

TPEF::BinaryStreamstream_
 File containing file to assemble. More...
 
TTAMachine::Machinemach_
 Machine for assembled TPEF. More...
 
CompilerMessage error_
 Last thrown error message. More...
 
AssemblyParserDiagnostic parserDiagnostic_
 Parser messages. More...
 
std::shared_ptr< std::string > asmCode_
 String containing code to compile. More...
 

Detailed Description

TCE Assembler user interface.

Definition at line 55 of file Assembler.hh.

Constructor & Destructor Documentation

◆ Assembler()

Assembler::Assembler ( TPEF::BinaryStream assemblerFile,
TTAMachine::Machine assemblerMachine 
)

Constructor.

Parameters
assemblerFileStream containing file to assemble.
assemblerMachineMachine to use for running compiled binary.

Definition at line 55 of file Assembler.cc.

57  :
58  stream_(assemblerFile), mach_(&assemblerMachine) {
59 }

Member Function Documentation

◆ addWarning()

void Assembler::addWarning ( UValue  lineNumber,
std::string  errorMessage 
)

Adds warning message for client to see.

Parameters
lineNumberLine number of assembly code, where error accured.
errorMessageWarning message.

Definition at line 140 of file Assembler.cc.

140  {
141  parserDiagnostic_.addWarning(lineNumber, errorMessage);
142 }

References AssemblyParserDiagnostic::addWarning(), and parserDiagnostic_.

Here is the call graph for this function:

◆ codeLine()

std::string Assembler::codeLine ( UValue  lineNumber) const
private

Definition at line 196 of file Assembler.cc.

196  {
197  return parserDiagnostic_.codeLine(lineNumber);
198 }

References AssemblyParserDiagnostic::codeLine(), and parserDiagnostic_.

Referenced by compile().

Here is the call graph for this function:

◆ compile()

Binary * Assembler::compile ( )

Compiles assembler and returns TPEF hierarchy.

Returns
Compiled TPEF.
Exceptions
CompileErrorIf there was any problems during the compilation.

Definition at line 68 of file Assembler.cc.

68  {
69  // read binary stream to string
71 
72  // clear old warnings from previous compilation
74 
75  Binary* newBinary = new Binary();
76 
77  // this one does the actual parsing work
78  AssemblerParser parser(*newBinary, *mach_, &parserDiagnostic_);
79 
80  try {
81  try {
82  if (!parser.compile(*asmCode_)) {
83 
85  __FILE__, __LINE__, __func__, "Syntax error.");
86 
87  error.setCodeFileLineNumber(parser.errorLine());
88 
89  throw error;
90  }
91 
92  // ObjectAllreadyExists and IllegalMachine exceptions.
93  } catch (Exception& e) {
94 
96  __FILE__, __LINE__, __func__, e.errorMessage());
97 
98  error.setCause(e);
99  error.setCodeFileLineNumber(parser.errorLine());
100 
101  throw error;
102  }
103 
104  parser.finalize(mach_->isLittleEndian());
105 
106  } catch (CompileError& e) {
107 
111 
112  // clean up internals of creators and managers and delete
113  // partial binary
114  parser.cleanup();
115 
116  delete newBinary;
117  newBinary = NULL;
118 
120  __FILE__, __LINE__, __func__,
121  "Error in line " + Conversion::toString(error_.lineNumber) +
122  ": " + error_.assemblerLine + "\nMessage: " + error_.message);
123 
124  error.setCause(e);
125  error.setCodeFileLineNumber(error_.lineNumber);
126 
127  throw error;
128  }
129 
130  return newBinary;
131 }

References __func__, asmCode_, CompilerMessage::assemblerLine, AssemblerParser::cleanup(), CompileError::codeFileLineNumber(), codeLine(), AssemblerParser::compile(), error(), error_, AssemblerParser::errorLine(), Exception::errorMessage(), AssemblerParser::finalize(), TTAMachine::Machine::isLittleEndian(), CompilerMessage::lineNumber, mach_, CompilerMessage::message, parserDiagnostic_, readAssemblerCode(), AssemblyParserDiagnostic::reset(), and Conversion::toString().

Referenced by main().

Here is the call graph for this function:

◆ error()

const CompilerMessage & Assembler::error ( ) const

Returns last thrown error message..

Returns
Error that halted compilation.

Definition at line 165 of file Assembler.cc.

165  {
166  return error_;
167 }

References error_.

Referenced by compile().

◆ readAssemblerCode()

void Assembler::readAssemblerCode ( )
private

Reads assembly code from binary stream to a string.

Definition at line 173 of file Assembler.cc.

173  {
174  unsigned int streamPosition = stream_.readPosition();
175 
176  asmCode_ = std::make_shared<std::string>();
177 
178  // read file to string and mark line start indexes.
179  for (unsigned int i = streamPosition; i < stream_.sizeOfFile(); i++) {
180  Byte readByte = stream_.readByte();
181  *asmCode_ += readByte;
182  }
183 
184  // restore stream position...
185  stream_.setReadPosition(streamPosition);
186 }

References asmCode_, TPEF::BinaryStream::readByte(), TPEF::BinaryStream::readPosition(), TPEF::BinaryStream::setReadPosition(), TPEF::BinaryStream::sizeOfFile(), and stream_.

Referenced by compile().

Here is the call graph for this function:

◆ targetMachine()

const TTAMachine::Machine & Assembler::targetMachine ( ) const

Returns the target machine of the assembler.

Returns
The target machine.

Definition at line 150 of file Assembler.cc.

150  {
151  return *mach_;
152 }

References mach_.

◆ warnings()

const std::set< CompilerMessage > & Assembler::warnings ( ) const

Definition at line 155 of file Assembler.cc.

155  {
156  return parserDiagnostic_.warnings();
157 }

References parserDiagnostic_, and AssemblyParserDiagnostic::warnings().

Referenced by main().

Here is the call graph for this function:

Member Data Documentation

◆ asmCode_

std::shared_ptr<std::string> Assembler::asmCode_
private

String containing code to compile.

Definition at line 89 of file Assembler.hh.

Referenced by compile(), and readAssemblerCode().

◆ error_

CompilerMessage Assembler::error_
private

Last thrown error message.

Definition at line 83 of file Assembler.hh.

Referenced by compile(), and error().

◆ mach_

TTAMachine::Machine* Assembler::mach_
private

Machine for assembled TPEF.

Definition at line 80 of file Assembler.hh.

Referenced by compile(), and targetMachine().

◆ parserDiagnostic_

AssemblyParserDiagnostic Assembler::parserDiagnostic_
private

Parser messages.

Definition at line 86 of file Assembler.hh.

Referenced by addWarning(), codeLine(), compile(), and warnings().

◆ stream_

TPEF::BinaryStream& Assembler::stream_
private

File containing file to assemble.

Definition at line 77 of file Assembler.hh.

Referenced by readAssemblerCode().


The documentation for this class was generated from the following files:
AssemblerParser
Definition: AssemblerParser.hh:331
Assembler::error
const CompilerMessage & error() const
Definition: Assembler.cc:165
TPEF::BinaryStream::readPosition
unsigned int readPosition()
Definition: BinaryStream.cc:561
TPEF::BinaryStream::setReadPosition
void setReadPosition(unsigned int position)
Definition: BinaryStream.cc:629
TPEF::Binary
Definition: Binary.hh:49
Assembler::stream_
TPEF::BinaryStream & stream_
File containing file to assemble.
Definition: Assembler.hh:77
CompileError
Definition: Exception.hh:1019
CompilerMessage::message
std::string message
Message.
Definition: AssemblyParserDiagnostic.hh:47
CompilerMessage::assemblerLine
std::string assemblerLine
Assembly code line number.
Definition: AssemblyParserDiagnostic.hh:48
Byte
unsigned char Byte
Definition: BaseType.hh:116
CompileError::codeFileLineNumber
int codeFileLineNumber()
Definition: Exception.cc:1489
Conversion::toString
static std::string toString(const T &source)
Assembler::parserDiagnostic_
AssemblyParserDiagnostic parserDiagnostic_
Parser messages.
Definition: Assembler.hh:86
TTAMachine::Machine::isLittleEndian
bool isLittleEndian() const
Definition: Machine.hh:258
CompilerMessage::lineNumber
UValue lineNumber
Message generation line number.
Definition: AssemblyParserDiagnostic.hh:49
__func__
#define __func__
Definition: Application.hh:67
TPEF::BinaryStream::readByte
Byte readByte()
Definition: BinaryStream.cc:120
Exception
Definition: Exception.hh:54
Assembler::asmCode_
std::shared_ptr< std::string > asmCode_
String containing code to compile.
Definition: Assembler.hh:89
Exception::errorMessage
std::string errorMessage() const
Definition: Exception.cc:123
Assembler::mach_
TTAMachine::Machine * mach_
Machine for assembled TPEF.
Definition: Assembler.hh:80
AssemblyParserDiagnostic::codeLine
std::string codeLine(UValue lineNumber) const
Definition: AssemblyParserDiagnostic.cc:96
Assembler::codeLine
std::string codeLine(UValue lineNumber) const
Definition: Assembler.cc:196
Assembler::readAssemblerCode
void readAssemblerCode()
Definition: Assembler.cc:173
TPEF::BinaryStream::sizeOfFile
unsigned int sizeOfFile()
Definition: BinaryStream.cc:764
AssemblyParserDiagnostic::addWarning
void addWarning(UValue lineNumber, const std::string &message)
Definition: AssemblyParserDiagnostic.cc:56
AssemblyParserDiagnostic::reset
void reset(std::shared_ptr< const std::string > assemblyText)
Definition: AssemblyParserDiagnostic.cc:40
Assembler::error_
CompilerMessage error_
Last thrown error message.
Definition: Assembler.hh:83
AssemblyParserDiagnostic::warnings
const std::set< CompilerMessage > & warnings() const
Definition: AssemblyParserDiagnostic.hh:78