program.hh 3.95 KB
Newer Older
phlo's avatar
phlo committed
1 2 3
#ifndef PROGRAM_HH_
#define PROGRAM_HH_

4
#include <istream>
5
#include <set>
6
#include <unordered_map>
7
#include <vector>
phlo's avatar
phlo committed
8

9
#include "common.hh"
10
#include "instruction.hh"
11

phlo's avatar
phlo committed
12 13
namespace ConcuBinE {

phlo's avatar
phlo committed
14 15 16
//==============================================================================
// Program class
//==============================================================================
phlo's avatar
phlo committed
17

phlo's avatar
phlo committed
18
class Program : public std::vector<Instruction>
phlo's avatar
phlo committed
19
{
phlo's avatar
phlo committed
20 21
public: //======================================================================

phlo's avatar
phlo committed
22
  //----------------------------------------------------------------------------
phlo's avatar
phlo committed
23
  // public member types
phlo's avatar
phlo committed
24 25 26 27
  //----------------------------------------------------------------------------

  struct List : public std::vector<Program>
    {
28
      //------------------------------------------------------------------------
phlo's avatar
phlo committed
29
      // public constructors
30 31
      //------------------------------------------------------------------------

phlo's avatar
phlo committed
32
      // expose constructors from std::vector
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
      //
      using std::vector<Program>::vector;

      // alias for checking if parameter pack is of class T
      //
      template <typename T, typename ... Ts>
      using all_base_of = std::conjunction<std::is_base_of<T, Ts>...>;

      // alias for restricting variadic overloads to parameters of class T
      //
      template <typename T, typename ... Ts>
      using enable_if_all_base_of =
        typename std::enable_if<all_base_of<T, Ts...>::value>::type;

      // variadic copy constructor for arguments of type Program
      //
      template <class ... Ts, typename = enable_if_all_base_of<Program, Ts...>>
      List (const Ts & ... program) : std::vector<Program>()
        {
          reserve(sizeof...(Ts));
          (push_back(program), ...);
        }

      // variadic move constructor for arguments of type Program
      //
      template <class ... Ts, typename = enable_if_all_base_of<Program, Ts...>>
      List (Ts && ... program) : std::vector<Program>()
        {
          reserve(sizeof...(Ts));
          (push_back(std::move(program)), ...);
        }
phlo's avatar
phlo committed
64 65 66
    };

  //----------------------------------------------------------------------------
phlo's avatar
phlo committed
67
  // public constructors
phlo's avatar
phlo committed
68 69
  //----------------------------------------------------------------------------

phlo's avatar
phlo committed
70
  // expose constructors from std::vector
phlo's avatar
phlo committed
71
  //
phlo's avatar
phlo committed
72
  using std::vector<Instruction>::vector;
phlo's avatar
phlo committed
73

phlo's avatar
phlo committed
74
  // parse input stream
phlo's avatar
phlo committed
75
  //
phlo's avatar
phlo committed
76
  Program (std::istream & stream, const std::string & path);
phlo's avatar
phlo committed
77

phlo's avatar
phlo committed
78 79 80 81 82
  //----------------------------------------------------------------------------
  // public member functions
  //----------------------------------------------------------------------------

  // get map of program counters to set of predecessors
phlo's avatar
phlo committed
83
  //
phlo's avatar
phlo committed
84
  // pc -> set of predecessors
phlo's avatar
phlo committed
85
  //
phlo's avatar
phlo committed
86 87 88
  // * checks for unreachable instructions
  //
  std::unordered_map<word_t, std::set<word_t>> predecessors () const;
phlo's avatar
phlo committed
89

phlo's avatar
phlo committed
90
  // get program counter corresponding to a given label
phlo's avatar
phlo committed
91
  //
phlo's avatar
phlo committed
92 93 94
  word_t pc (const std::string & label) const;

  // get label corresponding to a given program counter
phlo's avatar
phlo committed
95
  //
phlo's avatar
phlo committed
96
  std::string label (word_t pc) const;
phlo's avatar
phlo committed
97

phlo's avatar
phlo committed
98
  // print whole program
phlo's avatar
phlo committed
99
  //
phlo's avatar
phlo committed
100 101 102
  std::string print () const;

  // print instruction at a given program counter
phlo's avatar
phlo committed
103
  //
phlo's avatar
phlo committed
104
  std::string print (word_t pc) const;
phlo's avatar
phlo committed
105

phlo's avatar
phlo committed
106
  //----------------------------------------------------------------------------
phlo's avatar
phlo committed
107
  // public data members
phlo's avatar
phlo committed
108
  //----------------------------------------------------------------------------
109

phlo's avatar
phlo committed
110
  // path to program file
phlo's avatar
phlo committed
111
  //
phlo's avatar
phlo committed
112
  std::string path;
phlo's avatar
phlo committed
113

phlo's avatar
phlo committed
114
private: //=====================================================================
phlo's avatar
phlo committed
115

phlo's avatar
phlo committed
116
  //----------------------------------------------------------------------------
phlo's avatar
phlo committed
117
  // private data members
phlo's avatar
phlo committed
118
  //----------------------------------------------------------------------------
phlo's avatar
phlo committed
119

phlo's avatar
phlo committed
120
  // maps program counters to the label referencing it
phlo's avatar
phlo committed
121
  //
phlo's avatar
phlo committed
122
  // pc -> label
phlo's avatar
phlo committed
123
  //
phlo's avatar
phlo committed
124
  std::unordered_map<word_t, std::string> pc_to_label;
phlo's avatar
phlo committed
125

phlo's avatar
phlo committed
126
  // maps labels to the corresponding program counter
phlo's avatar
phlo committed
127
  //
phlo's avatar
phlo committed
128
  // label -> pc
phlo's avatar
phlo committed
129
  //
phlo's avatar
phlo committed
130
  std::unordered_map<std::string, word_t> label_to_pc;
131
};
phlo's avatar
phlo committed
132

phlo's avatar
phlo committed
133 134
} // namespace ConcuBinE

phlo's avatar
phlo committed
135
#endif