ScummVM API documentation
decompiler.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #ifndef STARK_TOOLS_SCRIPT_H
23 #define STARK_TOOLS_SCRIPT_H
24 
25 #include "common/str.h"
26 
27 #include "engines/stark/resources/script.h"
28 
29 namespace Stark {
30 
31 namespace Resources {
32 class Script;
33 }
34 
35 namespace Tools {
36 
37 class Block;
38 class CFGCommand;
39 class DefinitionRegistry;
40 struct ControlStructure;
41 struct ASTBlock;
42 struct ASTNode;
43 struct ASTCondition;
44 struct ASTLoop;
45 
46 class Decompiler {
47 public:
49  ~Decompiler();
50 
51  void printCommands() const;
52  void printBlocks() const;
53  void printDecompiled();
54 
55  Common::String getError() const;
56 
57 private:
58  // Command control flow graph construction
59  bool checkCommands();
60  void linkCommandBranches();
61  CFGCommand *findEntryPoint();
62 
63  // Block control flow graph construction
64  void buildBlocks();
65  void buildBlocks(Block *block, CFGCommand *command);
66  Block *buildBranchBlocks(CFGCommand *command);
67 
68  // Control flow analysis
69  void analyseControlFlow();
70  void detectInfiniteLoop();
71  void detectWhile();
72  void detectIf();
73 
74  // AST generation
75  ASTNode *buildAST();
76  void buildASTFromBlock(ASTBlock *parent, Block *block, Block *stopBlock);
77  ASTCondition *buildASTConditionFromBlock(ASTNode *parent, Block *block);
78  ASTLoop *buildASTLoopFromBlock(ASTNode *parent, Block *block);
79 
80  // AST verification
81  void verifyAST();
82  bool verifyCommandInAST(CFGCommand *cfgCommand);
83  bool verifyCommandSuccessorInAST(CFGCommand *cfgCommand, CFGCommand *cfgSuccessor, ASTNode *astSuccessor, const char *successorType);
84 
85  Common::String _error;
86 
88  CFGCommand *_entryPoint;
89 
90  Common::Array<Block *> _blocks;
91  Common::Array<ControlStructure *> _controlStructures;
92 
93  ASTNode *_astHead;
94  Common::Array<Block *> _visitedInfiniteLoopStarts;
95  Common::Array<Block *> _visitedBlocks;
96 
97  DefinitionRegistry *_definitionRegistry;
98 };
99 
100 } // End of namespace Tools
101 } // End of namespace Stark
102 
103 #endif // STARK_TOOLS_SCRIPT_H
Definition: abstractsyntaxtree.h:107
Definition: str.h:59
Definition: command.h:153
Definition: decompiler.h:46
Definition: array.h:52
Definition: console.h:27
Definition: abstractsyntaxtree.h:39
Definition: abstractsyntaxtree.h:128
Definition: abstractsyntaxtree.h:85
Definition: command.h:94
Definition: block.h:40
Definition: script.h:45