AERA
decompiler.h
1 //_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
2 //_/_/
3 //_/_/ AERA
4 //_/_/ Autocatalytic Endogenous Reflective Architecture
5 //_/_/
6 //_/_/ Copyright (c) 2018-2025 Jeff Thompson
7 //_/_/ Copyright (c) 2018-2025 Kristinn R. Thorisson
8 //_/_/ Copyright (c) 2018-2025 Icelandic Institute for Intelligent Machines
9 //_/_/ http://www.iiim.is
10 //_/_/
11 //_/_/ Copyright (c) 2010-2012 Eric Nivel
12 //_/_/ Copyright (c) 2010 Nathaniel Thurston
13 //_/_/ Center for Analysis and Design of Intelligent Agents
14 //_/_/ Reykjavik University, Menntavegur 1, 102 Reykjavik, Iceland
15 //_/_/ http://cadia.ru.is
16 //_/_/
17 //_/_/ Part of this software was developed by Eric Nivel
18 //_/_/ in the HUMANOBS EU research project, which included
19 //_/_/ the following parties:
20 //_/_/
21 //_/_/ Autonomous Systems Laboratory
22 //_/_/ Technical University of Madrid, Spain
23 //_/_/ http://www.aslab.org/
24 //_/_/
25 //_/_/ Communicative Machines
26 //_/_/ Edinburgh, United Kingdom
27 //_/_/ http://www.cmlabs.com/
28 //_/_/
29 //_/_/ Istituto Dalle Molle di Studi sull'Intelligenza Artificiale
30 //_/_/ University of Lugano and SUPSI, Switzerland
31 //_/_/ http://www.idsia.ch/
32 //_/_/
33 //_/_/ Institute of Cognitive Sciences and Technologies
34 //_/_/ Consiglio Nazionale delle Ricerche, Italy
35 //_/_/ http://www.istc.cnr.it/
36 //_/_/
37 //_/_/ Dipartimento di Ingegneria Informatica
38 //_/_/ University of Palermo, Italy
39 //_/_/ http://diid.unipa.it/roboticslab/
40 //_/_/
41 //_/_/
42 //_/_/ --- HUMANOBS Open-Source BSD License, with CADIA Clause v 1.0 ---
43 //_/_/
44 //_/_/ Redistribution and use in source and binary forms, with or without
45 //_/_/ modification, is permitted provided that the following conditions
46 //_/_/ are met:
47 //_/_/ - Redistributions of source code must retain the above copyright
48 //_/_/ and collaboration notice, this list of conditions and the
49 //_/_/ following disclaimer.
50 //_/_/ - Redistributions in binary form must reproduce the above copyright
51 //_/_/ notice, this list of conditions and the following disclaimer
52 //_/_/ in the documentation and/or other materials provided with
53 //_/_/ the distribution.
54 //_/_/
55 //_/_/ - Neither the name of its copyright holders nor the names of its
56 //_/_/ contributors may be used to endorse or promote products
57 //_/_/ derived from this software without specific prior
58 //_/_/ written permission.
59 //_/_/
60 //_/_/ - CADIA Clause: The license granted in and to the software
61 //_/_/ under this agreement is a limited-use license.
62 //_/_/ The software may not be used in furtherance of:
63 //_/_/ (i) intentionally causing bodily injury or severe emotional
64 //_/_/ distress to any person;
65 //_/_/ (ii) invading the personal privacy or violating the human
66 //_/_/ rights of any person; or
67 //_/_/ (iii) committing or preparing for any act of war.
68 //_/_/
69 //_/_/ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
70 //_/_/ CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
71 //_/_/ INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
72 //_/_/ MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
73 //_/_/ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
74 //_/_/ CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
75 //_/_/ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
76 //_/_/ BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
77 //_/_/ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
78 //_/_/ INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
79 //_/_/ WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
80 //_/_/ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
81 //_/_/ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
82 //_/_/ OF SUCH DAMAGE.
83 //_/_/
84 //_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
85 
86 #ifndef decompiler_h
87 #define decompiler_h
88 
89 #include <fstream>
90 #include <sstream>
91 
92 #include "out_stream.h"
93 #include "segments.h"
94 
95 
96 namespace r_comp {
97 
98 class dll_export Decompiler {
99 private:
100  OutStream *out_stream_;
101  uint16 indents_; // in chars.
102  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.
103  bool in_hlp_;
104  bool hlp_postfix_;
105  bool horizontal_set_;
106 
107  r_code::ImageObject *current_object_;
108 
109  r_comp::Metadata *metadata_;
110  r_comp::Image *image_;
111 
112  Timestamp time_reference_;
113 
114  std::unordered_map<uint16, 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.
115  uint16 last_variable_id_;
116  std::string get_variable_name(uint16 index, bool postfix); // associates iptr/vptr indexes to names; inserts them in out_stream_ if necessary; when postfix==true, a trailing ':' is added.
117  std::string get_hlp_variable_name(uint16 index);
118 
119  std::unordered_map<uint16, 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.
120  std::unordered_map<std::string, uint16> object_indices_; // inverted version of the object_names.
121  std::string get_object_name(uint16 index); // retrieves the name of an object.
122 
123  void write_indent(uint16 i);
124  void write_expression_head(uint16 read_index); // decodes the leading atom of an expression.
125  void write_expression_tail(uint16 read_index); // decodes the elements of an expression following the head.
126  void write_set(uint16 read_index, uint16 write_as_view_index = 0);
127  void write_any(uint16 read_index, bool &after_tail_wildcard, uint16 write_as_view_index = 0); // decodes any element in an expression or a set.
128 
129  typedef void (Decompiler::*Renderer)(uint16);
130  r_code::resized_vector<Renderer> renderers_; // indexed by opcodes; when not there, write_expression() is used.
131 
132  // Renderers.
133  void write_expression(uint16 read_index); // default renderer.
134  void write_group(uint16 read_index);
135  void write_marker(uint16 read_index);
136  void write_pgm(uint16 read_index);
137  void write_ipgm(uint16 read_index);
138  void write_icmd(uint16 read_index);
139  void write_cmd(uint16 read_index);
140  void write_fact(uint16 read_index);
141  void write_hlp(uint16 read_index);
142  void write_ihlp(uint16 read_index);
143  void write_view(uint16 read_index, uint16 arity);
144 
145  bool partial_decompilation_; // used when decompiling on-the-fly.
146  bool ignore_named_objects_;
147  std::unordered_set<uint16> named_objects_;
148  std::vector<r_code::SysObject *> imported_objects_; // referenced objects added to the image that were not in the original list of objects to be decompiled.
149 public:
150  Decompiler();
151  ~Decompiler();
152 
153  void init(r_comp::Metadata *metadata);
154  uint32 decompile(r_comp::Image *image,
155  std::ostringstream *stream,
156  Timestamp time_reference,
157  bool ignore_named_objects); // decompiles the whole image; returns the number of objects.
158  uint32 decompile(r_comp::Image *image,
159  std::ostringstream *stream,
160  Timestamp time_reference,
161  std::vector<r_code::SysObject *> &imported_objects,
162  bool include_oid = true, bool include_label = true, bool include_views = true); // idem, ignores named objects if in the imported object list.
163 
173  uint32 decompile_references(r_comp::Image *image, std::unordered_map<uint16, std::string>* object_names = NULL);
174 
184  void decompile_object(
185  uint16 object_index, std::ostringstream *stream, Timestamp time_reference, bool include_oid = true,
186  bool include_label = true, bool include_views = true);
187 
188  void decompile_object(const std::string object_name, std::ostringstream *stream, Timestamp time_reference); // decompiles a single object given its name: use this function to follow references.
189 };
190 }
191 
192 
193 #endif
r_code::resized_vector< Renderer >
r_comp::Decompiler
Definition: decompiler.h:98
r_code::ImageObject
Definition: r_code/object.h:106
r_comp::Image
Definition: segments.h:177
r_comp::OutStream
Definition: out_stream.h:97
r_comp::Metadata
Definition: segments.h:109