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