replicode
pgm_overlay.h
Go to the documentation of this file.
1 // pgm_overlay.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 pgm_overlay_h
32 #define pgm_overlay_h
33 
34 #include "overlay.h"
35 
36 
37 namespace r_exec {
38 
39 class PGMController;
40 class InputLessPGMController;
41 class IPGMContext;
42 
43 // Overlays for input-less programs.
44 // Base class for other programs (with inputs, anti-programs).
46  public Overlay {
47  friend class PGMController;
48  friend class InputLessPGMController;
49  friend class IPGMContext;
50 protected:
51  std::vector<P<Code> > productions; // receives the results of ins, inj and eje; views are retrieved (fvw) or built (reduction) in the value array.
52 
53  bool evaluate(uint16_t index); // evaluates the pgm_code at the specified index.
54 
55  virtual Code *get_mk_rdx(uint16_t &extent_index) const;
56 
57  void patch_tpl_args(); // no views in tpl args; patches the ptn skeleton's first atom with IPGM_PTR with an index in the ipgm arg set; patches wildcards with similar IPGM_PTRs.
58  void patch_tpl_code(uint16_t pgm_code_index, uint16_t ipgm_code_index); // to recurse.
59  virtual void patch_input_code(uint16_t pgm_code_index, uint16_t input_index, uint16_t input_code_index, int16_t parent_index = -1); // defined in PGMOverlay.
60 
63 public:
64  virtual ~InputLessPGMOverlay();
65 
66  virtual void reset(); // reset to original state (pristine copy of the pgm code and empty value set).
67 
68  bool inject_productions(); // return true upon successful evaluation; no existence check in simulation mode.
69 };
70 
71 // Overlay with inputs.
72 // Several ReductionCores can attempt to reduce the same overlay simultaneously (each with a different input).
74  public InputLessPGMOverlay {
75  friend class PGMController;
76  friend class IPGMContext;
77 private:
79  uint64_t birth_time; // used for ipgms: overlays older than ipgm->tsc are killed; birth_time set to the time of the first match, 0 if no match occurred.
80 protected:
81  r_code::list<uint16_t> input_pattern_indices; // stores the input patterns still waiting for a match: will be plucked upon each successful match.
82  std::vector<P<r_code::View> > input_views; // copies of the inputs; vector updated at each successful match.
83 
84  typedef enum {
85  SUCCESS = 0,
86  FAILURE = 1,
87  IMPOSSIBLE = 3 // when the input's class does not even match the object class in the pattern's skeleton.
88  } MatchResult;
89 
90  MatchResult match(r_exec::View *input, uint16_t &input_index); // delegates to _match; input_index is set to the index of the pattern that matched the input.
91  bool check_guards(); // return true upon successful evaluation.
92 
93  MatchResult _match(r_exec::View *input, uint16_t pattern_index); // delegates to __match.
94  MatchResult __match(r_exec::View *input, uint16_t pattern_index); // return SUCCESS upon a successful match, IMPOSSIBLE if the input is not of the right class, FAILURE otherwise.
95 
96  Code *dereference_in_ptr(Atom a);
97  void patch_input_code(uint16_t pgm_code_index, uint16_t input_index, uint16_t input_code_index, int16_t parent_index = -1);
98 
99  virtual Code *get_mk_rdx(uint16_t &extent_index) const;
100 
101  void init();
102 
104  PGMOverlay(PGMOverlay *original, uint16_t last_input_index, uint16_t value_commit_index); // copy from the original and rollback.
105 public:
106  virtual ~PGMOverlay();
107 
108  void reset();
109 
110  virtual Overlay *reduce(r_exec::View *input); // called upon the processing of a reduction job.
111 
112  r_code::Code *getInputObject(uint16_t i) const;
113  r_code::View *getInputView(uint16_t i) const;
114 
116  return birth_time;
117  }
118 
119  bool is_invalidated();
120 };
121 
122 // Several ReductionCores can attempt to reduce the same overlay simultaneously (each with a different input).
123 // In addition, ReductionCores and signalling jobs can attempt to inject productions concurrently.
124 // Usues the same mk.rdx as for InputLessPGMOverlays.
126  public PGMOverlay {
127  friend class AntiPGMController;
128 private:
130  AntiPGMOverlay(AntiPGMOverlay *original, uint16_t last_input_index, uint16_t value_limit);
131 public:
132  ~AntiPGMOverlay();
133 
134  Overlay *reduce(r_exec::View *input); // called upon the processing of a reduction job.
135 };
136 }
137 
138 #endif
std::vector< P< Code > > productions
Definition: pgm_overlay.h:51
uint64_t birth_time
Definition: pgm_overlay.h:79
Definition: pgm_overlay.h:73
Definition: overlay.h:116
#define dll_export
Definition: dll.h:44
std::vector< P< r_code::View > > input_views
Definition: pgm_overlay.h:82
Definition: pgm_overlay.h:45
Definition: _context.cpp:34
Definition: pgm_controller.h:68
MatchResult
Definition: binding_map.h:168
Definition: context.h:47
bool match(const IPGMContext &input, const IPGMContext &pattern)
Definition: context.cpp:452
Definition: pgm_controller.h:54
Definition: view.h:47
r_code::list< uint16_t > input_pattern_indices
Definition: pgm_overlay.h:81
uint64_t get_birth_time() const
Definition: pgm_overlay.h:115
Definition: atom.h:45
Definition: pgm_overlay.h:125
Definition: pgm_controller.h:82
Definition: object.h:100
Definition: object.h:172
Definition: overlay.h:50
void reduce(const IPGMContext &context, const IPGMContext &input_set, const IPGMContext &section, std::vector< uint16_t > &input_indices, std::vector< uint16_t > &production_indices)
Definition: context.cpp:523
bool is_volatile
Definition: pgm_overlay.h:78