1 /*
2  * File:  ConversionController.h
3  *
4  * Copyright (c) Freescale Semiconductor, Inc. All rights reserved.
5  * See included license file for license details.
6  */
7 #if !defined(_ConversionController_h_)
8 #define _ConversionController_h_
9 
10 #include <iostream>
11 #include <fstream>
12 #include <string>
13 #include <stdexcept>
14 #include <smart_ptr.h>
15 #include <ElftosbLexer.h>
16 #include <ElftosbAST.h>
17 #include "EvalContext.h"
18 #include "Value.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"
27 
28 namespace elftosb
29 {
30 
31 /*!
32  * \brief Manages the entire elftosb file conversion process.
33  *
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.
38  *
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.
47  */
48 class ConversionController : public OptionDictionary, public EvalContext::SourceFileManager
49 {
50 public:
51           //! \brief Default constructor.
52           ConversionController();
53 
54           //! \brief Destructor.
55           virtual ~ConversionController();
56 
57           //! \name Paths
58           //@{
59           //! \brief Specify the command file that controls the conversion process.
60           void setCommandFilePath(const std::string & path);
61 
62           //! \brief Specify the path of a file provided by the user from outside the command file.
63           void addExternalFilePath(const std::string & path);
64           //@}
65 
66           //! \name Conversion
67           //@{
68           //! \brief Process input files.
69           void run();
70 
71           //! \brief Uses a BootImageGenerator object to create the final output boot image.
72           BootImage * generateOutput(BootImageGenerator * generator);
73           //@}
74 
75           //! \name SourceFileManager interface
76           //@{
77           //! \brief Returns true if a source file with the name \a name exists.
78           virtual bool hasSourceFile(const std::string & name);
79 
80           //! \brief Gets the requested source file.
81           virtual SourceFile * getSourceFile(const std::string & name);
82 
83           //! \brief Returns the default source file, or NULL if none is set.
84           virtual SourceFile * getDefaultSourceFile();
85           //@}
86 
87           //! \brief Returns a reference to the context used for expression evaluation.
getEvalContext()88           inline EvalContext & getEvalContext() { return m_context; }
89 
90 protected:
91           //! \name AST processing
92           //@{
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);
99           //@}
100 
101           //! \name Statement conversion
102           //@{
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);
111           //@}
112 
113           //! \name Utilities
114           //@{
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);
121           //@}
122 
123           //! \name Debugging
124           //@{
125           void testLexer(ElftosbLexer & lexer);
126           void printIntConstExpr(const std::string & ident, IntConstExprASTNode * expr);
127           //@}
128 
129 protected:
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.
134 
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.
143 };
144 
145 //! \brief Whether to support HAB keywords during parsing.
146 //!
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;
150 
151 }; // namespace elftosb
152 
153 #endif // _ConversionController_h_
154