replicode
init.h
Go to the documentation of this file.
1 // init.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 init_h
32 #define init_h
33 
34 #include "r_code/list.h"
35 
36 #include "r_comp/segments.h"
37 #include "r_comp/compiler.h"
38 #include "r_comp/preprocessor.h"
39 
40 #include "CoreLibrary/dll.h"
41 
42 #include <thread>
43 
44 namespace r_exec {
45 
46 // Time base; either Time::Get or network-aware synced time.
47 extern dll_export uint64_t(*Now)();
48 
49 // Loaded once for all.
50 // Results from the compilation of user.classes.replicode.
51 // The latter contains all class definitions and all shared objects (e.g. ontology); does not contain any dynamic (res!=forever) objects.
52 //extern dll_export r_comp::Metadata Metadata;
53 //extern dll_export r_comp::Image Seed;
56 
57 // A preprocessor and a compiler are maintained throughout the life of the dll to retain, respectively, macros and global references.
58 // Both functions add the compiled object to Seed.code_image.
59 // Source files: use ANSI encoding (not Unicode).
60 bool Compile(const char* filename, string& error, bool compile_metadata = false);
61 
62 // Threaded decompiler, for decompiling on the fly asynchronously.
63 // Named objects are referenced but not decompiled.
65  public _Object {
66 private:
67  static const uint64_t ObjectsInitialSize = 16;
68  void decompile();
69 
70 
71  uint64_t ostream_id; // 0 is std::cout.
72  std::string header;
74  std::thread *_thread;
75  volatile uint64_t spawned;
76 
77 public:
78  TDecompiler(uint64_t ostream_id, std::string header);
79  ~TDecompiler();
80 
81  void add_object(Code *object);
82  void add_objects(const r_code::list<P<Code> > &objects);
83  void add_objects(const std::vector<P<Code> > &objects);
84  void runDecompiler();
85 };
86 
87 // Spawns an instance of output_window.exe (see output_window project) and opens a pipe between the main process and output_window.
88 // Temporary solution:
89 // (a) not portable,
90 // (b) shall be defined in CoreLibrary instead of here,
91 // (c) the stream pool management (PipeOStream::Open(), PipeOStream::Close() and PipeOStream::Get()) shall be decoupled from this implementation (it's an IDE feature),
92 // (d) PipeOStream shall be based on std::ostringstream instead of std::ostream with a refined std::stringbuf (override sync() to write in the pipe).
93 #if defined(WIN32) || defined(WIN64)
94 class dll_export PipeOStream:
95  public std::ostream {
96 private:
97  static std::vector<PipeOStream *> Streams;
98  static PipeOStream NullStream;
99 
100  int m_pipeRead[2];
101  int m_pipeWrite[2];
102 //HANDLE pipe_read;
103 //HANDLE pipe_write;
104 
105  void init(); // create one child process and a pipe.
106  PipeOStream();
107 public:
108  static void Open(uint8_t count); // open count streams.
109  static void Close(); // close all streams.
110  static PipeOStream &Get(uint8_t id); // return NullStream if id is out of range.
111 
112  ~PipeOStream();
113 
114  PipeOStream &operator <<(std::string &s);
115  PipeOStream &operator <<(const char *s);
116 };
117 #endif
118 
119 // Initialize Now, compile user.classes.replicode, builds the Seed and loads the user-defined operators.
120 // Return false in case of a problem (e.g. file not found, operator not found, etc.).
121 bool dll_export Init(const char *user_operator_library_path,
122  uint64_t(*time_base)(),
123  const char *seed_path);
124 
125 // Alternate taking a ready-made metadata and seed (will be copied into Metadata and Seed).
126 bool dll_export Init(const char *user_operator_library_path,
127  uint64_t(*time_base)(),
128  const r_comp::Metadata &metadata,
129  const r_comp::Image &seed);
130 
131 uint16_t dll_export GetOpcode(const char *name); // classes, operators and functions.
132 
133 std::string dll_export GetAxiomName(const uint16_t index); // for constant objects (ex: self, position, and other axioms).
134 }
135 
136 
137 #endif
Definition: segments.h:119
std::ostream & operator<<(std::ostream &os, RepliStruct *structure)
Definition: replistruct.cpp:718
Definition: base.h:75
r_code::list< P< Code > > objects
Definition: init.h:73
bool Init(const char *user_operator_library_path, uint64_t(*time_base)())
Definition: init.cpp:261
#define dll_export
Definition: dll.h:44
Definition: _context.cpp:34
uint16_t GetOpcode(const char *name)
Definition: init.cpp:509
uint64_t ostream_id
Definition: init.h:71
Definition: segments.h:59
r_comp::Image * getSeed()
Definition: init.cpp:57
r_comp::Metadata * getMetadata()
Definition: init.cpp:51
dll_export uint64_t(* Now)()
Definition: init.cpp:49
Definition: list.h:42
bool Compile(const char *filename, std::string &error, bool compile_metadata)
Definition: init.cpp:73
Definition: object.h:172
volatile uint64_t spawned
Definition: init.h:75
std::string GetAxiomName(const uint16_t index)
Definition: init.cpp:517
std::string header
Definition: init.h:72
Definition: init.h:64
std::thread * _thread
Definition: init.h:74