AERA
init.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 //_/_/ Center for Analysis and Design of Intelligent Agents
13 //_/_/ Reykjavik University, Menntavegur 1, 102 Reykjavik, Iceland
14 //_/_/ http://cadia.ru.is
15 //_/_/
16 //_/_/ Part of this software was developed by Eric Nivel
17 //_/_/ in the HUMANOBS EU research project, which included
18 //_/_/ the following parties:
19 //_/_/
20 //_/_/ Autonomous Systems Laboratory
21 //_/_/ Technical University of Madrid, Spain
22 //_/_/ http://www.aslab.org/
23 //_/_/
24 //_/_/ Communicative Machines
25 //_/_/ Edinburgh, United Kingdom
26 //_/_/ http://www.cmlabs.com/
27 //_/_/
28 //_/_/ Istituto Dalle Molle di Studi sull'Intelligenza Artificiale
29 //_/_/ University of Lugano and SUPSI, Switzerland
30 //_/_/ http://www.idsia.ch/
31 //_/_/
32 //_/_/ Institute of Cognitive Sciences and Technologies
33 //_/_/ Consiglio Nazionale delle Ricerche, Italy
34 //_/_/ http://www.istc.cnr.it/
35 //_/_/
36 //_/_/ Dipartimento di Ingegneria Informatica
37 //_/_/ University of Palermo, Italy
38 //_/_/ http://diid.unipa.it/roboticslab/
39 //_/_/
40 //_/_/
41 //_/_/ --- HUMANOBS Open-Source BSD License, with CADIA Clause v 1.0 ---
42 //_/_/
43 //_/_/ Redistribution and use in source and binary forms, with or without
44 //_/_/ modification, is permitted provided that the following conditions
45 //_/_/ are met:
46 //_/_/ - Redistributions of source code must retain the above copyright
47 //_/_/ and collaboration notice, this list of conditions and the
48 //_/_/ following disclaimer.
49 //_/_/ - Redistributions in binary form must reproduce the above copyright
50 //_/_/ notice, this list of conditions and the following disclaimer
51 //_/_/ in the documentation and/or other materials provided with
52 //_/_/ the distribution.
53 //_/_/
54 //_/_/ - Neither the name of its copyright holders nor the names of its
55 //_/_/ contributors may be used to endorse or promote products
56 //_/_/ derived from this software without specific prior
57 //_/_/ written permission.
58 //_/_/
59 //_/_/ - CADIA Clause: The license granted in and to the software
60 //_/_/ under this agreement is a limited-use license.
61 //_/_/ The software may not be used in furtherance of:
62 //_/_/ (i) intentionally causing bodily injury or severe emotional
63 //_/_/ distress to any person;
64 //_/_/ (ii) invading the personal privacy or violating the human
65 //_/_/ rights of any person; or
66 //_/_/ (iii) committing or preparing for any act of war.
67 //_/_/
68 //_/_/ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
69 //_/_/ CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
70 //_/_/ INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
71 //_/_/ MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
72 //_/_/ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
73 //_/_/ CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
74 //_/_/ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
75 //_/_/ BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
76 //_/_/ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
77 //_/_/ INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
78 //_/_/ WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
79 //_/_/ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
80 //_/_/ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
81 //_/_/ OF SUCH DAMAGE.
82 //_/_/
83 //_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
84 
85 #ifndef init_h
86 #define init_h
87 
88 #include "../submodules/CoreLibrary/CoreLibrary/utils.h"
89 
90 #include "../r_code/list.h"
91 
92 #include "../r_comp/segments.h"
93 #include "../r_comp/compiler.h"
94 #include "../r_comp/preprocessor.h"
95 
96 #include "dll.h"
97 
98 
99 namespace r_exec {
100 
101 // Time base; either Time::Get or network-aware synced time.
102 extern r_exec_dll Timestamp (*Now)();
103 
104 // Loaded once for all.
105 // Results from the compilation of user.classes.replicode.
106 // The latter contains all class definitions and all shared objects (e.g. ontology); does not contain any dynamic (res!=forever) objects.
107 extern r_exec_dll r_comp::Metadata Metadata;
108 extern r_exec_dll r_comp::Image Seed;
109 
110 // A preprocessor and a compiler are maintained throughout the life of the dll to retain, respectively, macros and global references.
111 // Both functions add the compiled object to Seed.code_image.
112 // Source files: use ANSI encoding (not Unicode).
113 bool r_exec_dll Compile(const char *filename, std::string &error);
114 bool r_exec_dll Compile(std::istream &source_code, std::string &error);
115 
116 // Threaded decompiler, for decompiling on the fly asynchronously.
117 // Named objects are referenced but not decompiled.
118 class r_exec_dll TDecompiler :
119  public _Object {
120 private:
121  static const uint32 ObjectsInitialSize = 16;
122  static thread_ret thread_function_call Decompile(void *args);
123 
124  class _Thread :
125  public Thread {
126  };
127 
128  _Thread *thread_;
129  volatile uint32 spawned_;
130 
131  r_code::list<P<r_code::Code> > objects_;
132 
133  uint32 ostream_id_; // 0 is std::cout.
134 
135  std::string header_;
136 public:
137  TDecompiler(uint32 ostream_id, std::string header);
138  ~TDecompiler();
139 
140  void add_object(r_code::Code *object);
141  void add_objects(const r_code::list<P<r_code::Code> > &objects);
142  void add_objects(const std::vector<P<r_code::Code> > &objects);
143  void decompile();
144 };
145 
146 // Spawns an instance of output_window.exe (see output_window project) and opens a pipe between the main process and output_window.
147 // Temporary solution:
148 // (a) not portable,
149 // (b) shall be defined in CoreLibrary instead of here,
150 // (c) the stream pool management (PipeOStream::Open(), PipeOStream::Close() and PipeOStream::Get()) shall be decoupled from this implementation (it's an IDE feature),
151 // (d) PipeOStream shall be based on std::ostringstream instead of std::ostream with a refined std::stringbuf (override sync() to write in the pipe).
152 class r_exec_dll PipeOStream :
153  public std::ostream {
154 private:
155  static std::vector<PipeOStream *> Streams_;
156  static PipeOStream NullStream_;
157 
158 #ifdef WINDOWS
159  HANDLE pipe_read_;
160  HANDLE pipe_write_;
161 
162  void init(); // create one child process and a pipe.
163 #endif
164  PipeOStream();
165 public:
166  static void Open(uint8 count); // open count streams.
167  static void Close(); // close all streams.
168  static PipeOStream &Get(uint8 id); // return NullStream if id is out of range.
169 
170  ~PipeOStream();
171 
172  PipeOStream &operator <<(std::string &s);
173  PipeOStream &operator <<(const char *s);
174 };
175 
183 bool r_exec_dll InitOpcodes(const r_comp::Metadata& metadata);
184 
189 public:
193  virtual void* getFunction(const char* function_name) = 0;
194 };
195 
201 public:
202  SharedFunctionLibrary() : getUserOperatorFunction_(0) {}
203 
204  SharedLibrary* load(const char* file_name) {
205  SharedLibrary* library = sharedLibrary_.load(file_name);
206  getUserOperatorFunction_ = (void* (*)(const char*))sharedLibrary_.getFunction("GetUserOperatorFunction");
207  return library;
208  }
209 
210  void* getFunction(const char* function_name) override {
211  if (getUserOperatorFunction_) {
212  // Try GetUserOperatorFunction first.
213  void* result = getUserOperatorFunction_(function_name);
214  if (result)
215  return result;
216  }
217 
218  // Fall back to searching for the function in the shared library global name space.
219  return sharedLibrary_.getFunction(function_name);
220  }
221 private:
222  SharedLibrary sharedLibrary_;
223  void* (*getUserOperatorFunction_)(const char* function_name);
224 };
225 
226 // Initialize Now, compile userOperatorLibrary, builds the Seed and loads the user-defined operators.
227 // Return false in case of a problem (e.g. file not found, operator not found, etc.).
228 bool r_exec_dll Init(FunctionLibrary* userOperatorLibrary,
229  Timestamp (*time_base)(),
230  const char *seed_path);
231 
232 // Alternate taking a ready-made metadata and seed (will be copied into Metadata and Seed).
233 bool r_exec_dll Init(FunctionLibrary* userOperatorLibrary,
234  Timestamp (*time_base)(),
235  const r_comp::Metadata &metadata,
236  const r_comp::Image &seed);
237 
238 uint16 r_exec_dll GetOpcode(const char *name); // classes, operators and functions.
239 
240 std::string r_exec_dll GetAxiomName(const uint16 index); // for constant objects (ex: self, position, and other axioms).
241 
242 bool r_exec_dll hasUserDefinedOperators(const std::string class_name);
243 
244 bool r_exec_dll hasUserDefinedOperators(const uint16 opcode);
245 }
246 
247 
248 #endif
core::SharedLibrary
Definition: submodules/CoreLibrary/CoreLibrary/utils.h:149
core::Thread
Definition: submodules/CoreLibrary/CoreLibrary/utils.h:159
r_exec::TDecompiler
Definition: init.h:119
core::P< r_code::Code >
r_exec::PipeOStream
Definition: init.h:153
r_code::Code
Definition: r_code/object.h:224
core::_Object
Definition: base.h:131
r_exec::SharedFunctionLibrary::getFunction
void * getFunction(const char *function_name) override
Definition: init.h:210
r_comp::Image
Definition: segments.h:177
r_code::list
Definition: list.h:99
r_comp::Metadata
Definition: segments.h:109
r_exec::FunctionLibrary::getFunction
virtual void * getFunction(const char *function_name)=0
r_exec::FunctionLibrary
Definition: init.h:188
r_exec::SharedFunctionLibrary
Definition: init.h:200