AERA
atom.h
1 //_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
2 //_/_/
3 //_/_/ AERA
4 //_/_/ Autocatalytic Endogenous Reflective Architecture
5 //_/_/
6 //_/_/ Copyright (c) 2018-2023 Jeff Thompson
7 //_/_/ Copyright (c) 2018-2023 Kristinn R. Thorisson
8 //_/_/ Copyright (c) 2018-2023 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 r_code_atom_h
87 #define r_code_atom_h
88 
89 #include <string>
90 #include <unordered_map>
91 #include <set>
92 #include "../submodules/CoreLibrary/CoreLibrary/types.h"
93 
94 #undef THIS
95 
96 using namespace core;
97 
98 namespace r_code {
99 
100 // Opcodes on 12 bits.
101 // Indices on 12 bits.
102 // Element count on 8 bits.
103 // To define bigger constructs (e.g. large matrices), define hooks to RAM (and derive classes from Object).
104 class dll_export Atom {
105 public:
112  class dll_export TraceContext {
113  public:
114  uint8 members_to_go_;
115  uint8 timestamp_data_;
116  uint8 duration_data_;
117  uint8 string_data_;
118  uint8 char_count_;
119  uint64 int64_high_;
120 
121  TraceContext();
122  void write_indents(std::ostream& out);
123  };
124 
125  typedef enum {
126  NIL = 0x80,
127  BOOLEAN_ = 0x81, // Spell with underbar to distinguish from Windows BOOLEAN.
128  WILDCARD = 0x82,
129  T_WILDCARD = 0x83,
130  I_PTR = 0x84, // internal pointer.
131  R_PTR = 0x85, // reference pointer.
132  VL_PTR = 0x86, // binding map value pointer.
133  IPGM_PTR = 0x87, // r_exec internal: index of data of a tpl arg held by an ipgm.
134  IN_OBJ_PTR = 0x88, // r_exec internal: index of data held by an input object.
135  VALUE_PTR = 0x89, // r_exec internal: index of data held by the overlay's value array.
136  PROD_PTR = 0x8A, // r_exec internal: index of data held by the overlay's production array.
137  OUT_OBJ_PTR = 0x8B, // r_exec internal: index of data held by a newly produced object.
138  D_IN_OBJ_PTR = 0x8C, // r_exec internal: index of data held by an object referenced by an input object.
139  ASSIGN_PTR = 0x8D, // r_exec internal: index of a hlp variable and to be assigned index of an expression that produces the value.
140  CODE_VL_PTR = 0x8E, // pointer to a value at an index in the same code array.
141  THIS = 0x90, // this pointer.
142  VIEW = 0x91,
143  MKS = 0x92,
144  VWS = 0x93,
145  NODE = 0xA0,
146  DEVICE = 0xA1,
147  DEVICE_FUNCTION = 0xA2,
148  C_PTR = 0xC0, // chain pointer.
149  SET = 0xC1,
150  S_SET = 0xC2, // structured set.
151  OBJECT = 0xC3,
152  MARKER = 0xC4,
153  OPERATOR = 0xC5,
154  STRING = 0xC6,
155  TIMESTAMP = 0xC7,
156  GROUP = 0xC8,
157  INSTANTIATED_PROGRAM = 0xC9,
158  INSTANTIATED_CPP_PROGRAM = 0xCA,
159  INSTANTIATED_INPUT_LESS_PROGRAM = 0xCB,
160  INSTANTIATED_ANTI_PROGRAM = 0xCC,
161  COMPOSITE_STATE = 0xCD,
162  MODEL = 0xCE,
163  NULL_PROGRAM = 0xCF,
164  DURATION = 0xD0
165  }Type;
166 
167  // encoders
168  static Atom Float(float32 f); // IEEE 754 32 bits encoding; shifted by 1 to the right (loss of precison).
169  static Atom PlusInfinity();
170  static Atom MinusInfinity();
171  static Atom UndefinedFloat();
172  static Atom Nil();
173  static Atom Boolean(bool value);
174  static Atom UndefinedBoolean();
175  static Atom Wildcard(uint16 opcode = 0x00);
176  static Atom TailWildcard();
177  static Atom IPointer(uint16 index);
178  static Atom VLPointer(uint16 index);
179  static Atom RPointer(uint16 index);
180  static Atom IPGMPointer(uint16 index);
181  static Atom InObjPointer(uint8 input_index, uint16 index); // input_index: index of the input view; index: index of data in the object's code.
182  static Atom DInObjPointer(uint8 relative_index, uint16 index); // relative_index: index of an in-obj-ptr in the program's (patched) code; index: index of data in the referenced object code.
183  static Atom OutObjPointer(uint16 index);
184  static Atom ValuePointer(uint16 index);
185  static Atom ProductionPointer(uint16 index);
186  static Atom AssignmentPointer(uint8 variable_index, uint16 index);
187  static Atom CodeVLPointer(uint16 index, uint16 cast_opcode = 0x0FFF);
188  static Atom This();
189  static Atom View();
190  static Atom Mks();
191  static Atom Vws();
192  static Atom SSet(uint16 opcode, uint8 element_count);
193  static Atom Set(uint8 element_count);
194  static Atom CPointer(uint8 element_count);
195  static Atom Object(uint16 opcode, uint8 arity);
196  static Atom Marker(uint16 opcode, uint8 arity);
197  static Atom Operator(uint16 opcode, uint8 arity);
198  static Atom Node(uint8 node_id);
199  static Atom UndefinedNode();
200  static Atom Device(uint8 node_id, uint8 class_id, uint8 dev_id);
201  static Atom UndefinedDevice();
202  static Atom DeviceFunction(uint16 opcode);
203  static Atom UndefinedDeviceFunction();
204  static Atom String(uint8 character_count);
205  static Atom UndefinedString();
206  static Atom Timestamp();
207  static Atom UndefinedTimestamp();
208  static Atom Duration();
209  static Atom InstantiatedProgram(uint16 opcode, uint8 arity);
210  static Atom Group(uint16 opcode, uint8 arity);
211  static Atom InstantiatedCPPProgram(uint16 opcode, uint8 arity);
212  static Atom InstantiatedAntiProgram(uint16 opcode, uint8 arity);
213  static Atom InstantiatedInputLessProgram(uint16 opcode, uint8 arity);
214  static Atom CompositeState(uint16 opcode, uint8 arity);
215  static Atom Model(uint16 opcode, uint8 arity);
216 
217  static Atom NullProgram(bool take_past_inputs);
218  static Atom RawPointer(void *pointer);
219 
220  Atom(uint32 a = 0xFFFFFFFF);
221  ~Atom();
222 
223  Atom &operator =(const Atom& a);
224  bool operator ==(const Atom& a) const;
225  bool operator !=(const Atom& a) const;
226  bool operator !() const;
227  operator size_t () const;
228 
229  uint32 atom_;
230 
231  // decoders
232  bool isUndefined() const;
233  uint8 getDescriptor() const;
234  bool isStructural() const;
235  bool isFloat() const;
236  bool readsAsNil() const; // returns true for all undefined values.
237  float32 asFloat() const;
238  bool asBoolean() const;
239  bool isBooleanTrue() const;
240  bool isBooleanFalse() const;
241  uint16 asIndex() const; // applicable to internal, view, reference,
242  // and value pointers.
243  uint8 asInputIndex() const; // applicable to IN_OBJ_PTR.
244  uint8 asRelativeIndex() const; // applicable to D_IN_OBJ_PTR.
245  uint16 asOpcode() const;
246  uint16 asCastOpcode() const; // applicable to CODE_VL_PTR.
247  uint8 getNodeID() const; // applicable to nodes and devices.
248  uint8 getClassID() const; // applicable to devices.
249  uint8 getDeviceID() const; // applicable to devices.
250  uint8 asAssignmentIndex() const;
251  uint8 getAtomCount() const; // arity of operators and
252  // objects/markers/structured sets,
253  // number of atoms in pointers chains,
254  // number of blocks of characters in
255  // strings.
256 
257  bool takesPastInputs() const; // applicable to NULL_PROGRAM.
258  template<class C> C *asRawPointer() const { return (C *)atom_; }
259 
260  void trace(TraceContext& context, std::ostream& out) const;
261 };
262 
269 bool __declspec(dllexport) SetOpcodeNames(const std::unordered_map<uint16, std::set<std::string>>& opcode_names);
270 
277 std::string GetOpcodeName(uint16 opcode);
278 }
279 
280 
281 #include "atom.inline.cpp"
282 
283 
284 #endif
r_code::Atom::TraceContext
Definition: atom.h:112
r_code::Atom
Definition: atom.h:104
core::String
Definition: submodules/CoreLibrary/CoreLibrary/utils.h:314