Computer Laboratory

Transition.cpp
Go to the documentation of this file.
1 
2 /*
3  * Copyright (c) 2013 Jonathan Anderson
4  * All rights reserved.
5  *
6  * This software was developed by SRI International and the University of
7  * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237)
8  * ("CTSRD"), as part of the DARPA CRASH research programme.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in the
17  * documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include "Debug.h"
33 #include "Protocol.h"
34 #include "State.h"
35 #include "Transition.h"
36 
37 #include "tesla.pb.h"
38 
39 #include <llvm/ADT/Twine.h>
40 
41 #include <sstream>
42 
43 using namespace llvm;
44 using std::string;
45 
46 namespace tesla {
47 
48 
49 void Transition::Create(State& From, State& To, TransitionVector& Transitions,
50  bool Init, bool Cleanup) {
51  OwningPtr<Transition> T(new NullTransition(From, To, Init, Cleanup));
52  Register(T, From, To, Transitions);
53 }
54 
55 void Transition::Create(State& From, State& To, const NowEvent& Ev,
57  TransitionVector& Transitions, bool Init, bool Cleanup) {
58 
59  ReferenceVector Refs(Automaton.argument().data(),
60  Automaton.argument_size());
61 
62  OwningPtr<Transition> T(new NowTransition(From, To, Ev, Refs, Init, Cleanup));
63  Register(T, From, To, Transitions);
64 }
65 
66 void Transition::Create(State& From, State& To, const FunctionEvent& Ev,
67  TransitionVector& Transitions, bool Init, bool Cleanup,
68  bool OutOfScope) {
69 
70  OwningPtr<Transition> T(
71  new FnTransition(From, To, Ev, Init, Cleanup, OutOfScope));
72 
73  Register(T, From, To, Transitions);
74 }
75 
76 void Transition::Create(State& From, State& To, const FieldAssignment& A,
77  TransitionVector& Transitions, bool Init, bool Cleanup,
78  bool OutOfScope) {
79 
80  OwningPtr<Transition> T(
81  new FieldAssignTransition(From, To, A, Init, Cleanup, OutOfScope));
82 
83  Register(T, From, To, Transitions);
84 }
85 
86 void Transition::CreateSubAutomaton(State& From, State& To,
87  const Identifier& ID,
88  TransitionVector& Transitions) {
89 
90  OwningPtr<Transition> T(new SubAutomatonTransition(From, To, ID));
91  Register(T, From, To, Transitions);
92 }
93 
94 void Transition::Copy(State &From, State& To, const Transition* Other,
95  TransitionVector& Transitions, bool OutOfScope) {
96 
97  OwningPtr<Transition> New;
98  bool Init = Other->RequiresInit();
99  bool Cleanup = Other->RequiresCleanup();
100 
101  OutOfScope |= Other->OutOfScope;
102  assert(!Init || !OutOfScope);
103 
104  switch (Other->getKind()) {
105  case Null:
106  assert(!OutOfScope);
107  return;
108 
109  case Now: {
110  assert(!OutOfScope);
111  auto O = cast<NowTransition>(Other);
112  New.reset(new NowTransition(From, To, O->Ev, O->Refs, Init, Cleanup));
113  break;
114  }
115 
116  case Fn:
117  New.reset(new FnTransition(From, To, cast<FnTransition>(Other)->Ev,
118  Init, Cleanup, OutOfScope));
119  break;
120 
121  case FieldAssign:
122  New.reset(new FieldAssignTransition(
123  From, To, cast<FieldAssignTransition>(Other)->Assign,
124  Init, Cleanup, OutOfScope));
125  break;
126 
127  case SubAutomaton:
128  assert(!OutOfScope);
129  New.reset(new SubAutomatonTransition(From, To,
130  cast<SubAutomatonTransition>(Other)->ID));
131  break;
132  }
133 
134  assert(New);
135  Register(New, From, To, Transitions);
136 }
137 
138 void Transition::Register(OwningPtr<Transition>& T, State& From, State& To,
139  TransitionVector& Transitions) {
140 
141  Transitions.push_back(T.get());
142  debugs("tesla.automata.transitions") << "registered " << T->String() << "\n";
143 
144  // We should never try to update the start state's references.
145  assert(To.ID() != 0);
146 
147  // Update the state we're pointing to with the references it should
148  // know about thus far in the execution of the automaton.
149  To.UpdateReferences(*T.get());
150  From.AddTransition(T);
151 }
152 
153 void Transition::GroupClasses(const TransitionVector& Ungrouped,
154  TransitionSets& EquivalenceClasses) {
155 
156  auto& Out = debugs("tesla.automata.transitions.equivalence");
157  Out << "grouping transitions:\n";
158 
159  for (auto *T : Ungrouped) {
160  Out << " " << T->String() << "\n";
161 
162  bool FoundEquivalent = false;
163  for (auto& Set : EquivalenceClasses) {
164  auto *Head = *Set.begin();
165  if (T->EquivalentTo(*Head)) {
166  assert(Head->EquivalentTo(*T));
167  FoundEquivalent = true;
168  Set.insert(T);
169  break;
170  }
171  }
172 
173  if (!FoundEquivalent) {
174  SmallPtrSet<const Transition*, 4> New;
175  New.insert(T);
176  EquivalenceClasses.push_back(New);
177  }
178  }
179 
180  Out << "equivalence classes:\n";
181  for (auto& EquivClass : EquivalenceClasses) {
182  bool Head = true;
183  for (const Transition *T : EquivClass) {
184  Out << (Head ? " " : " == ") << T->String() << "\n";
185  Head = false;
186  }
187  }
188 }
189 
190 
191 void Transition::ReferencesThusFar(OwningArrayPtr<const Argument*>& Args,
192  ReferenceVector& Ref) const {
193 
194  // Put this transition's *variable* references in var-index order.
195  SmallVector<const Argument*, 4> MyRefs;
196  for (auto Arg : this->Arguments())
197  if (Arg && Arg->type() == Argument::Variable) {
198  size_t i = Arg->index();
199 
200  if (MyRefs.size() <= i)
201  MyRefs.resize(i + 1);
202 
203  MyRefs[i] = Arg;
204  }
205 
206  auto& FromRefs = From.References();
207  const size_t Size = FromRefs.size();
208 
209  auto Arguments = new const Argument*[Size];
210  for (size_t i = 0; i < Size; i++) {
211  if ((MyRefs.size() > i) && MyRefs[i])
212  Arguments[i] = MyRefs[i];
213 
214  else if ((FromRefs.size() > i) && FromRefs[i])
215  Arguments[i] = FromRefs[i];
216 
217  else
218  Arguments[i] = NULL;
219  }
220 
221  Args.reset(Arguments);
222  Ref = ReferenceVector(Arguments, Size);
223 }
224 
225 
226 SmallVector<const Argument*,4> Transition::NewArguments() const {
227  auto OldArgs(From.References());
228  auto TransArgs(Arguments());
229 
230  SmallVector<const Argument*,4> NewArgs(TransArgs.size());
231  for (size_t i = 0; i < NewArgs.size(); i++)
232  if ((OldArgs.size() <= i) || (OldArgs[i] == NULL))
233  NewArgs[i] = TransArgs[i];
234 
235  return NewArgs;
236 }
237 
238 
239 int Transition::NewArgMask() const {
240  auto NewArgs(NewArguments());
241  int Mask = 0;
242 
243  for (int i = 0; i < NewArgs.size(); i++)
244  if ((NewArgs[i] != NULL) && (NewArgs[i]->type() == Argument::Variable))
245  Mask += (1 << i);
246 
247  return Mask;
248 }
249 
250 
251 string Transition::String() const {
252  string NewArgs;
253  for (auto A : NewArguments())
254  if ((A != NULL) && (A->type() == Argument::Variable))
255  NewArgs += " " + ShortName(A);
256 
257  string Special =
258  string(RequiresInit() ? "<<init>>" : "")
259  + (RequiresCleanup() ? "<<cleanup>>" : "")
260  ;
261 
262  return (Twine()
263  + "--("
264  + ShortLabel()
265  + (NewArgs.empty() ? "" : ":" + NewArgs)
266  + (Special.empty() ? "" : " " + Special)
267  + ")-->("
268  + Twine(To.ID())
269  + ")"
270  ).str();
271 }
272 
273 
274 const ReferenceVector FnTransition::Arguments() const {
275  const Argument* const *Args = Ev.argument().data();
276  size_t Len = Ev.argument_size();
277 
278  return ReferenceVector(Args, Len);
279 }
280 
281 string FnTransition::ShortLabel() const {
282  std::stringstream ss;
283  ss << Ev.function().name() << "(";
284 
285  for (int i = 0; i < Ev.argument_size(); i++)
286  ss
287  << ShortName(&Ev.argument(i))
288  << ((i < Ev.argument_size() - 1) ? "," : "");
289 
290  ss << ")";
291 
292  if (Ev.has_expectedreturnvalue()) {
293  assert(Ev.direction() == FunctionEvent::Exit);
294  ss << " == " << ShortName(&Ev.expectedreturnvalue());
295  } else
296  ss << ": " << FunctionEvent::Direction_Name(Ev.direction());
297 
298  return ss.str();
299 }
300 
301 string FnTransition::DotLabel() const {
302  std::stringstream ss;
303  ss << Ev.function().name() << "(";
304 
305  for (int i = 0; i < Ev.argument_size(); i++)
306  ss
307  << DotName(&Ev.argument(i))
308  << ((i < Ev.argument_size() - 1) ? "," : "");
309 
310  ss << ")";
311 
312  if (Ev.has_expectedreturnvalue()) {
313  assert(Ev.direction() == FunctionEvent::Exit);
314  ss << " == " << DotName(&Ev.expectedreturnvalue());
315  } else
316  ss << "\\n("
317  << FunctionEvent::Direction_Name(Ev.direction())
318  << ")"
319  ;
320 
321  return ss.str();
322 }
323 
324 
325 FieldAssignTransition::FieldAssignTransition(const State& From, const State& To,
326  const FieldAssignment& A,
327  bool Init, bool Cleanup,
328  bool OutOfScope)
329  : Transition(From, To, Init, Cleanup, OutOfScope), Assign(A),
330  ReferencedVariables(new const Argument*[2]),
331  Refs(ReferencedVariables.get(), 2)
332 {
333  ReferencedVariables[0] = &Assign.field().base();
334  ReferencedVariables[1] = &Assign.value();
335 }
336 
338  return (Twine()
339  + ShortName(&Assign.field().base())
340  + "."
341  + Assign.field().name()
342  + " "
343  + OpString(Assign.operation())
344  + " "
345  + ShortName(&Assign.value())
346  ).str();
347 }
348 
350  return (Twine()
351  + "struct " + Assign.field().type() + ":\\l"
352  + ShortName(&Assign.field().base())
353  + "."
354  + Assign.field().name()
355  + " "
356  + OpString(Assign.operation())
357  + " "
358  + ShortName(&Assign.value())
359  ).str();
360 }
361 
362 const char* FieldAssignTransition::OpString(FieldAssignment::AssignType T) {
363  switch (T) {
364  case FieldAssignment::SimpleAssign: return "=";
365  case FieldAssignment::PlusEqual: return "+=";
366  case FieldAssignment::MinusEqual: return "-=";
367  }
368 }
369 
370 
372  // TODO: actually find sub-automaton!
373  return ReferenceVector();
374 }
375 
376 } // namespace tesla
377