Wolframe, 0.0.3

programCode.hpp
Go to the documentation of this file.
1 /************************************************************************
2 
3  Copyright (C) 2011 - 2014 Project Wolframe.
4  All rights reserved.
5 
6  This file is part of Project Wolframe.
7 
8  Commercial Usage
9  Licensees holding valid Project Wolframe Commercial licenses may
10  use this file in accordance with the Project Wolframe
11  Commercial License Agreement provided with the Software or,
12  alternatively, in accordance with the terms contained
13  in a written agreement between the licensee and Project Wolframe.
14 
15  GNU General Public License Usage
16  Alternatively, you can redistribute this file and/or modify it
17  under the terms of the GNU General Public License as published by
18  the Free Software Foundation, either version 3 of the License, or
19  (at your option) any later version.
20 
21  Wolframe is distributed in the hope that it will be useful,
22  but WITHOUT ANY WARRANTY; without even the implied warranty of
23  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24  GNU General Public License for more details.
25 
26  You should have received a copy of the GNU General Public License
27  along with Wolframe. If not, see <http://www.gnu.org/licenses/>.
28 
29  If you have questions regarding the use of this file, please contact
30  Project Wolframe.
31 
32 ************************************************************************/
35 #ifndef _DATABASE_VIRTUAL_MACHINE_PROGRAM_CODE_HPP_INCLUDED
36 #define _DATABASE_VIRTUAL_MACHINE_PROGRAM_CODE_HPP_INCLUDED
38 #include <vector>
39 #include <cstdlib>
40 #include <limits>
41 #include <boost/shared_ptr.hpp>
42 
43 namespace _Wolframe {
44 namespace db {
45 namespace vm {
46 
50 {
51 public:
56 
57 public:
62  :m_ar(o.m_ar){}
63 
66  void add( const Instruction& instr)
67  {
68  m_ar.push_back( instr);
70  {
71  throw std::runtime_error( "program code size out of range");
72  }
73  }
78  {
79  if (adr >= m_ar.size()) throw std::runtime_error( "address out of bounds");
80  return m_ar[ adr];
81  }
85  const Instruction& operator[]( const Address& adr) const
86  {
87  if (adr >= m_ar.size()) throw std::runtime_error( "address out of bounds");
88  return m_ar[ adr];
89  }
90 
93  Address size() const
94  {
95  return (Address)m_ar.size();
96  }
97 
103  ProgramCode& operator()( CondCode cond, OpCode opcode, unsigned int arg=0)
104  {
105  add( InstructionSet::instruction( cond, opcode, arg));
106  return *this;
107  }
112  ProgramCode& operator()( OpCode opcode, unsigned int arg=0)
113  {
115  add( InstructionSet::instruction( cond, opcode, arg));
116  return *this;
117  }
118 
121  void append( const ProgramCode& prg)
122  {
123  m_ar.insert( m_ar.end(), prg.begin(), prg.end());
124  }
125 
129  Instruction get( Address& ip) const
130  {
132  return m_ar.at(ip);
133  }
134 
135  typedef std::vector<Instruction>::const_iterator const_iterator;
136  typedef std::vector<Instruction>::iterator iterator;
137 
139  const_iterator begin() const {return m_ar.begin();}
141  const_iterator end() const {return m_ar.end();}
143  iterator begin() {return m_ar.begin();}
145  iterator end() {return m_ar.end();}
147  const_iterator at( const Address& adr) const {return m_ar.begin() + adr;}
149  iterator at( const Address& adr) {return m_ar.begin() + adr;}
150 
152  void printRaw( std::ostream& out) const
153  {
155  }
156 
157 private:
158  std::vector<Instruction> m_ar; //< array of instructions
159 };
160 
161 }}}//namespace
162 #endif
163 
InstructionSet::Address Address
Definition: programCode.hpp:55
ProgramCode & operator()(OpCode opcode, unsigned int arg=0)
More readable program instruction definition operator (no condition -> always executed) ...
Definition: programCode.hpp:112
Definition: instructionSet.hpp:307
Instruction & operator[](const Address &adr)
Get an instruction reference by address (instruction index starting with 0)
Definition: programCode.hpp:77
iterator begin()
Get the program start iterator.
Definition: programCode.hpp:143
InstructionSet::Instruction Instruction
Definition: programCode.hpp:52
Defines the instruction set of the virtual machine defining database transactions.
void append(const ProgramCode &prg)
Append the code of another program without joining argument sets.
Definition: programCode.hpp:121
InstructionSet::CondCode CondCode
Definition: programCode.hpp:53
Definition: instructionSet.hpp:58
static void printProgramRaw(std::ostream &out, const std::vector< Instruction > &prg)
Print the program code without any symbolic information that is not available here.
Definition: instructionSet.hpp:368
void add(const Instruction &instr)
Add one instruction.
Definition: programCode.hpp:66
CondCode
Enumeration of conditional codes.
Definition: instructionSet.hpp:305
const_iterator at(const Address &adr) const
Get an iterator by address (instruction pointer)
Definition: programCode.hpp:147
InstructionSet::OpCode OpCode
Definition: programCode.hpp:54
boost::uint32_t Address
Address in program code.
Definition: instructionSet.hpp:326
void printRaw(std::ostream &out) const
Print the program code without any symbolic information that is not available here.
Definition: programCode.hpp:152
boost::uint32_t Instruction
Instruction.
Definition: instructionSet.hpp:328
ProgramCode(const ProgramCode &o)
Copy constructor.
Definition: programCode.hpp:61
const_iterator begin() const
Get the program start iterator.
Definition: programCode.hpp:139
Address size() const
Get size of the program code in instructions.
Definition: programCode.hpp:93
std::vector< Instruction >::const_iterator const_iterator
Definition: programCode.hpp:135
static Instruction instruction(CondCode cond, OpCode opcode, unsigned int arg=0)
Build an instruction from its parts.
Definition: instructionSet.hpp:335
iterator end()
Get the program end iterator.
Definition: programCode.hpp:145
std::vector< Instruction >::iterator iterator
Definition: programCode.hpp:136
const Instruction & operator[](const Address &adr) const
Get an instruction const reference by address (instruction index starting with 0) ...
Definition: programCode.hpp:85
const_iterator end() const
Get the program end iterator.
Definition: programCode.hpp:141
std::vector< Instruction > m_ar
Definition: programCode.hpp:158
The structure for the program code as array of instructions.
Definition: programCode.hpp:49
ProgramCode & operator()(CondCode cond, OpCode opcode, unsigned int arg=0)
More readable program instruction definition operator.
Definition: programCode.hpp:103
ProgramCode()
Default constructor.
Definition: programCode.hpp:59
OpCode
Implemented operation codes of the VM.
Definition: instructionSet.hpp:55
iterator at(const Address &adr)
Get an iterator by address (instruction pointer)
Definition: programCode.hpp:149