summaryrefslogtreecommitdiffstats
path: root/ue4/mycpu/cprogram.h
blob: d8f7d5eb6d3b298ca10b5ab21fb938a8072a75f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/**
 * @module cprogram
 * @author Guenther Neuwirth (0626638), Manuel Mausz (0728348)
 * @brief  CProgram extends std::vector and adds a method for parsing programfile
 * @date   26.05.2009
 */

#ifndef CPROGRAM_H
#define CPROGRAM_H 1

#include <vector>
#include <set>
#include <map>
#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/split.hpp>
#ifdef DEBUG
# include <iostream>
# include <iomanip>
#endif
#include "cinstruction.h"
#include "instructions.h"

/* forward declare CInstruction */
template<class T=CDat<int>, int width=0>
class CInstruction;

/**
 * @class CProgram
 *
 * CProgram extends std::vector and adds a method for parsing
 * programfile. This adds instances of CInstruction to CProgram itself.
 */
template<class T=CDat<int>, int width=0>
class CProgram
 : public std::vector<CInstruction<T> *>
{
  typedef typename std::set<CInstruction<T> *>::iterator setiterator;
  typedef std::vector<CInstruction<T> *> super;
  typedef typename super::iterator iterator;
  using super::begin;
  using super::end;
  using super::size;

  public:
    /**
     * @method CProgram
     * @brief  Default ctor
     * @param  -
     * @return -
     * @globalvars none
     * @exception  none
     * @conditions none
     */
    CProgram();

    /**
     * @method ~CProgram
     * @brief  Default dtor
     * @param  -
     * @return -
     * @globalvars none
     * @exception  none
     * @conditions none
     */
    ~CProgram();

    /**
     * @method getLabels
     * @brief  get reference to labels map
     * @param  -
     * @return reference to labels map
     * @globalvars none
     * @exception  none
     * @conditions none
     */
    const std::map<std::string, unsigned>& getLabels() const
    {
      return m_labels;
    }

    /**
     * @method findLabel
     * @brief  search for label
     * @param  label  name of label to search for
     * @return index of found label in program
     * @globalvars none
     * @exception  std::runtime_error
     * @conditions none
     */
    unsigned findLabel(const std::string& label) const;

    /**
     * @method compile
     * @brief  create instructions from parsing stream
     * @param  in  inputstream to read from
     * @return void
     * @globalvars none
     * @exception  std::runtime_error
     * @conditions none
     */
    void compile(std::istream& in);

#if DEBUG
    /**
     * @method dump
     * @brief  dumps contents to outputstream
     * @param  out  outputstream to write to
     * @return void
     * @globalvars none
     * @exception  none
     * @conditions none
     */
    void dump(std::ostream& out);
#endif

  private:
    /* members */
    /** set of known instructions */
    std::set<CInstruction<T> *> m_instrset;
    std::map<std::string, unsigned> m_labels;
};

/*----------------------------------------------------------------------------*/

template<class T=CDat<int>, int width=0>
CProgram<T>::CProgram()
{
  m_instrset.insert(new CInstructionInc<T>);
  m_instrset.insert(new CInstructionDec<T>);
  m_instrset.insert(new CInstructionAdd<T>);
  m_instrset.insert(new CInstructionSub<T>);
  m_instrset.insert(new CInstructionMul<T>);
  m_instrset.insert(new CInstructionDiv<T>);
  m_instrset.insert(new CInstructionLoad<T>);
  m_instrset.insert(new CInstructionStore<T>);
  m_instrset.insert(new CInstructionTest<T>);
  m_instrset.insert(new CInstructionLabel<T>);
  m_instrset.insert(new CInstructionJumpA<T>);
  m_instrset.insert(new CInstructionJumpZ<T>);
  m_instrset.insert(new CInstructionJumpS<T>);
  m_instrset.insert(new CInstructionWrite<T>);
}

/*----------------------------------------------------------------------------*/

template<class T=CDat<int>, int width=0>
CProgram<T>::~CProgram()
{
  /* free instruction set */
  for (setiterator it = m_instrset.begin(); it != m_instrset.end(); ++it)
    delete *it;

  /* free instruction */
  for (iterator it2 = begin(); it2 != end(); ++it2)
    delete *it2;
}

/*----------------------------------------------------------------------------*/

template<class T=CDat<int>, int width=0>
void CProgram<T>::compile(std::istream& in)
{
  if (!in.good())
    return;

  std::string line;
  unsigned i = 0;
  while (!in.eof() && in.good())
  {
    ++i;

    /* read stream per line */
    std::getline(in, line);
    if (line.empty())
      continue;

    boost::trim(line);
    boost::to_lower(line);

    /* ignore comments */
    if (line.find_first_of('#') == 0)
      continue;

    /* get instruction name */
    size_t pos = line.find_first_of(' ');
    std::string instrname(line.substr(0, pos));

    /* search and create instruction */
    CInstruction<T> *instrptr = NULL;
    setiterator it;
    for (it = m_instrset.begin(); it != m_instrset.end(); ++it)
    {
      if (*(*it) == instrname)
      {
        instrptr = *it;
        break;
      }
    }
    if (instrptr == NULL)
    {
      std::stringstream sstr;
      sstr << "Unknown instruction '" << instrname << "' on line " << i << ".";
      throw std::runtime_error(sstr.str());
    }

    /* create instruction */
    CInstruction<T> *instr = instrptr->factory();

    /* parse instruction parameters */
    std::string params = (pos == std::string::npos) ? "" : line.substr(pos + 1);
    boost::trim(params);
    std::list<std::string> instrparams;
    boost::split(instrparams, params, boost::is_any_of(", \t"), boost::token_compress_on);

    /* let instruction parse the parameters. catch+throw exception */
    try
    {
      /* handle label instruction ourself, but still add a dummy instruction */
      if (instrname == "label")
      {
        if (instrparams.size() != 1)
          throw std::runtime_error("Invalid paramater count - must be 1");
        std::string label(instrparams.front());
        if (label.length() < 2 || label[ label.length() - 1] != ':')
          throw std::runtime_error("Label has invalid syntax");
        m_labels[ label.substr(0, label.length() - 1) ] = size();
      }
      instr->compile(instrparams);
    }
    catch(std::runtime_error& ex)
    {
      std::stringstream sstr;
      sstr << "Unable to compile instruction '" << instrname
           << "' (line " << i << "): " << ex.what();
      throw std::runtime_error(sstr.str());
    }

    push_back(instr);
  }
}

/*----------------------------------------------------------------------------*/

template<class T=CDat<int>, int width=0>
unsigned CProgram<T>::findLabel(const std::string& label) const
{
  std::map<std::string, unsigned>::const_iterator it;
  it = m_labels.find(label);
  if (it == m_labels.end())
    throw std::runtime_error("Unknown label '" + label + "'");
  return it->second;
}

/*----------------------------------------------------------------------------*/

#if DEBUG
template<class T=CDat<int>, int width=0>
void CProgram<T>::dump(std::ostream& out)
{
  out << "[PROGRAM DUMP]" << std::endl;
  unsigned i = 0;
  for(iterator it = begin(); it != end(); ++it)
  {
    out << "[" << std::setw(4) << std::setfill('0') << i << "]  "
        << *(*it) << std::endl;
    ++i;
  }
}
#endif

#endif

/* vim: set et sw=2 ts=2: */