summaryrefslogtreecommitdiff
path: root/graphics/asymptote/program.h
blob: d16c12d4ad7e1f5e2ac7f3b6b010406f1d7b46e4 (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
/*****
 * program.h
 * Tom Prince
 *
 * The list of instructions used by the virtual machine.
 *****/

#ifndef PROGRAM_H
#define PROGRAM_H

#include <cstddef> // for ptrdiff_t

#include "common.h"
#include "inst.h"

using std::ptrdiff_t;

namespace vm {
struct inst;

class program : public gc {
public:
  class label;
  program();
  void encode(inst i);
  label begin();
  label end();
  inst &back();
  void pop_back();
private:
  friend class label;
  typedef mem::vector<inst> code_t;
  code_t code;
  inst& operator[](size_t);
};

class program::label
{
public: // interface
  label() : where(0), code() {}
public: //interface
  label& operator++();
  label& operator--();
  bool defined() const;
  bool operator==(const label& right) const;
  bool operator!=(const label& right) const;
  inst& operator*() const;
  inst* operator->() const;
  friend ptrdiff_t offset(const label& left,
                          const label& right);
private:
  label (size_t where, program* code)
    : where(where), code(code) {}
  size_t where;
  program* code;
  friend class program;
};

// Prints one instruction (including arguments).
void printInst(std::ostream& out, const program::label& code,
               const program::label& base);

// Prints code until a ret opcode is printed.
void print(std::ostream& out, program *base);

// Inline forwarding functions for vm::program
inline program::program()
  : code() {}
inline program::label program::end()
{ return label(code.size(), this); }
inline program::label program::begin()
{ return label(0, this); }
inline inst& program::back()
{ return code.back(); }
inline void program::pop_back()
{ return code.pop_back(); }
inline void program::encode(inst i)
{ code.push_back(i); }
inline inst& program::operator[](size_t n)
{ return code[n]; }
inline program::label& program::label::operator++()
{ ++where; return *this; }
inline program::label& program::label::operator--()
{ --where; return *this; }
inline bool program::label::defined() const
{ return (code != 0); }
inline bool program::label::operator==(const label& right) const
{ return (code == right.code) && (where == right.where); }
inline bool program::label::operator!=(const label& right) const
{ return !(*this == right); }
inline inst& program::label::operator*() const
{ return (*code)[where]; }
inline inst* program::label::operator->() const
{ return &**this; }
inline ptrdiff_t offset(const program::label& left,
                        const program::label& right)
{ return right.where - left.where; }

} // namespace vm

#endif // PROGRAM_H