2 * File: ConversionController.h
4 * Copyright (c) Freescale Semiconductor, Inc. All rights reserved.
5 * See included license file for license details.
7 #if !defined(_ConversionController_h_)
8 #define _ConversionController_h_
14 #include <smart_ptr.h>
15 #include <ElftosbLexer.h>
16 #include <ElftosbAST.h>
17 #include "EvalContext.h"
19 #include "SourceFile.h"
20 #include "Operation.h"
21 #include "DataSource.h"
22 #include "DataTarget.h"
23 #include "OutputSection.h"
24 #include "BootImage.h"
25 #include "OptionDictionary.h"
26 #include "BootImageGenerator.h"
32 * \brief Manages the entire elftosb file conversion process.
34 * Instances of this class are intended to be used only once. There is no
35 * way to reset an instance once it has started or completed a conversion.
36 * Thus the run() method is not reentrant. State information is stored in
37 * the object during the conversion process.
39 * Two things need to be done before the conversion can be started. The
40 * command file path has to be set with the setCommandFilePath() method,
41 * and the paths of any externally provided (i.e., from the command line)
42 * files need to be added with addExternalFilePath(). Once these tasks
43 * are completed, the run() method can be called to parse and execute the
44 * command file. After run() returns, pass an instance of
45 * BootImageGenerator to the generateOutput() method in order to get
46 * an instance of BootImage that can be written to the output file.
48 class ConversionController : public OptionDictionary, public EvalContext::SourceFileManager
51 //! \brief Default constructor.
52 ConversionController();
54 //! \brief Destructor.
55 virtual ~ConversionController();
59 //! \brief Specify the command file that controls the conversion process.
60 void setCommandFilePath(const std::string & path);
62 //! \brief Specify the path of a file provided by the user from outside the command file.
63 void addExternalFilePath(const std::string & path);
68 //! \brief Process input files.
71 //! \brief Uses a BootImageGenerator object to create the final output boot image.
72 BootImage * generateOutput(BootImageGenerator * generator);
75 //! \name SourceFileManager interface
77 //! \brief Returns true if a source file with the name \a name exists.
78 virtual bool hasSourceFile(const std::string & name);
80 //! \brief Gets the requested source file.
81 virtual SourceFile * getSourceFile(const std::string & name);
83 //! \brief Returns the default source file, or NULL if none is set.
84 virtual SourceFile * getDefaultSourceFile();
87 //! \brief Returns a reference to the context used for expression evaluation.
88 inline EvalContext & getEvalContext() { return m_context; }
91 //! \name AST processing
93 void parseCommandFile();
94 void processOptions(ListASTNode * options);
95 void processConstants(ListASTNode * constants);
96 void processSources(ListASTNode * sources);
97 void processSections(ListASTNode * sections);
98 OutputSection * convertDataSection(DataSectionContentsASTNode * dataSection, uint32_t sectionID, OptionDictionary * optionsDict);
101 //! \name Statement conversion
103 OperationSequence * convertStatementList(ListASTNode * statements);
104 OperationSequence * convertOneStatement(StatementASTNode * statement);
105 OperationSequence * convertLoadStatement(LoadStatementASTNode * statement);
106 OperationSequence * convertCallStatement(CallStatementASTNode * statement);
107 OperationSequence * convertFromStatement(FromStatementASTNode * statement);
108 OperationSequence * convertModeStatement(ModeStatementASTNode * statement);
109 OperationSequence * convertIfStatement(IfStatementASTNode * statement);
110 void handleMessageStatement(MessageStatementASTNode * statement);
115 Value * convertAssignmentNodeToValue(ASTNode * node, std::string & ident);
116 SourceFile * getSourceFromName(std::string * sourceName, int line);
117 DataSource * createSourceFromNode(ASTNode * dataNode);
118 DataTarget * createTargetFromNode(ASTNode * targetNode);
119 std::string * substituteVariables(const std::string * message);
120 DataSource * createIVTDataSource(IVTConstASTNode * ivtNode);
125 void testLexer(ElftosbLexer & lexer);
126 void printIntConstExpr(const std::string & ident, IntConstExprASTNode * expr);
130 typedef std::map<std::string, SourceFile*> source_map_t; //!< Map from source name to object.
131 typedef std::vector<std::string> path_vector_t; //!< List of file paths.
132 typedef std::vector<OutputSection*> section_vector_t; //!< List of output sections.
133 typedef std::vector<std::string> source_name_vector_t; //!< List of source names.
135 smart_ptr<std::string> m_commandFilePath; //!< Path to command file.
136 smart_ptr<CommandFileASTNode> m_ast; //!< Root of the abstract syntax tree.
137 EvalContext m_context; //!< Evaluation context for expressions.
138 source_map_t m_sources; //!< Map of source names to file objects.
139 path_vector_t m_externPaths; //!< Paths provided on the command line by the user.
140 SourceFile * m_defaultSource; //!< Source to use when one isn't provided.
141 section_vector_t m_outputSections; //!< List of output sections the user wants.
142 source_name_vector_t m_failedSources; //!< List of sources that failed to open successfully.
145 //! \brief Whether to support HAB keywords during parsing.
147 //! This is a standalone global solely so that the bison-generated parser code can get to it
148 //! as simply as possible.
149 extern bool g_enableHABSupport;
151 }; // namespace elftosb
153 #endif // _ConversionController_h_