replicode
decompiler.h
Go to the documentation of this file.
1 // decompiler.h
2 //
3 // Author: Eric Nivel
4 //
5 // BSD license:
6 // Copyright (c) 2010, Eric Nivel
7 // All rights reserved.
8 // Redistribution and use in source and binary forms, with or without
9 // modification, are permitted provided that the following conditions are met:
10 //
11 // - Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 // - Redistributions in binary form must reproduce the above copyright
14 // notice, this list of conditions and the following disclaimer in the
15 // documentation and/or other materials provided with the distribution.
16 // - Neither the name of Eric Nivel nor the
17 // names of their contributors may be used to endorse or promote products
18 // derived from this software without specific prior written permission.
19 //
20 // THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
21 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 // DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
24 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 #ifndef decompiler_h
32 #define decompiler_h
33 
34 #include <fstream>
35 #include <sstream>
36 
37 #include "out_stream.h"
38 #include "segments.h"
39 
40 
41 namespace r_comp {
42 
44 private:
46  uint16_t indents; // in chars.
47  bool closing_set; // set after writing the last element of a set: any element in an expression finding closing_set will indent and set closing_set to false.
48  bool in_hlp;
51 
53 
56 
57  uint64_t time_offset; // 0 means no offset.
58 
59  std::unordered_map<uint16_t, std::string> variable_names; // in the form vxxx where xxx is an integer representing the order of referencing of the variable/label in the code.
60  uint16_t last_variable_id;
61  std::string get_variable_name(uint16_t index, bool postfix); // associates iptr/vptr indexes to names; inserts them in out_stream if necessary; when postfix==true, a trailing ':' is added.
62  std::string get_hlp_variable_name(uint16_t index);
63 
64  std::unordered_map<uint16_t, std::string> object_names; // in the form class_namexxx where xxx is an integer representing the order of appearence of the object in the image; or: user-defined names when they are provided.
65  std::unordered_map<std::string, uint16_t> object_indices; // inverted version of the object_names.
66  std::string get_object_name(uint16_t index); // retrieves the name of an object.
67 
68  void write_indent(uint16_t i);
69  void write_expression_head(uint16_t read_index); // decodes the leading atom of an expression.
70  void write_expression_tail(uint16_t read_index, bool apply_time_offset, bool vertical = false); // decodes the elements of an expression following the head.
71  void write_set(uint16_t read_index, bool aply_time_offset, uint16_t write_as_view_index = 0);
72  void write_any(uint16_t read_index, bool &after_tail_wildcard, bool apply_time_offset, uint16_t write_as_view_index = 0); // decodes any element in an expression or a set.
73 
74  typedef void (Decompiler::*Renderer)(uint16_t);
75  r_code::vector<Renderer> renderers; // indexed by opcodes; when not there, write_expression() is used.
76 
77 // Renderers.
78  void write_expression(uint16_t read_index); // default renderer.
79  void write_group(uint16_t read_index);
80  void write_marker(uint16_t read_index);
81  void write_pgm(uint16_t read_index);
82  void write_ipgm(uint16_t read_index);
83  void write_icmd(uint16_t read_index);
84  void write_cmd(uint16_t read_index);
85  void write_fact(uint16_t read_index);
86  void write_hlp(uint16_t read_index);
87  void write_ihlp(uint16_t read_index);
88  void write_view(uint16_t read_index, uint16_t arity);
89 
90  bool partial_decompilation; // used when decompiling on-the-fly.
92  std::unordered_set<uint16_t> named_objects;
93  std::vector<SysObject *> imported_objects; // referenced objects added to the image that were not in the original list of objects to be decompiled.
94 public:
95  Decompiler();
96  ~Decompiler();
97 
98  void init(r_comp::Metadata *metadata);
99  uint64_t decompile(r_comp::Image *image,
100  std::ostringstream *stream,
101  uint64_t time_offset,
102  bool ignore_named_objects); // decompiles the whole image; returns the number of objects.
103  uint64_t decompile(r_comp::Image *image,
104  std::ostringstream *stream,
105  uint64_t time_offset,
106  std::vector<SysObject *> &imported_objects); // idem, ignores named objects if in the imported object list.
107  uint64_t decompile_references(r_comp::Image *image); // initialize a reference table so that objects can be decompiled individually; returns the number of objects.
108  void decompile_object(uint16_t object_index, std::ostringstream *stream, uint64_t time_offset); // decompiles a single object; object_index is the position of the object in the vector returned by Image::getObject.
109  void decompile_object(const std::string object_name, std::ostringstream *stream, uint64_t time_offset); // decompiles a single object given its name: use this function to follow references.
110 };
111 }
112 
113 
114 #endif
Definition: segments.h:119
std::unordered_map< std::string, uint16_t > object_indices
Definition: decompiler.h:65
ImageObject * current_object
Definition: decompiler.h:52
r_comp::Metadata * metadata
Definition: decompiler.h:54
#define dll_export
Definition: dll.h:44
r_comp::Image * image
Definition: decompiler.h:55
std::unordered_set< uint16_t > named_objects
Definition: decompiler.h:92
std::unordered_map< uint16_t, std::string > variable_names
Definition: decompiler.h:59
std::vector< SysObject * > imported_objects
Definition: decompiler.h:93
Definition: object.h:49
bool ignore_named_objects
Definition: decompiler.h:91
Definition: segments.h:59
uint64_t time_offset
Definition: decompiler.h:57
bool closing_set
Definition: decompiler.h:47
Definition: decompiler.h:43
r_code::vector< Renderer > renderers
Definition: decompiler.h:75
bool in_hlp
Definition: decompiler.h:48
std::unordered_map< uint16_t, std::string > object_names
Definition: decompiler.h:64
bool hlp_postfix
Definition: decompiler.h:49
OutStream * out_stream
Definition: decompiler.h:45
bool partial_decompilation
Definition: decompiler.h:90
Definition: image.h:41
bool horizontal_set
Definition: decompiler.h:50
uint16_t indents
Definition: decompiler.h:46
uint16_t last_variable_id
Definition: decompiler.h:60
Definition: out_stream.h:45