home *** CD-ROM | disk | FTP | other *** search
/ AI Game Programming Wisdom / AIGameProgrammingWisdom.iso / SourceCode / 10 Scripting / 02 Berger / sinterp / VM.H < prev   
Encoding:
C/C++ Source or Header  |  2001-10-16  |  2.8 KB  |  91 lines

  1. #ifndef __VM_H__
  2. #define __VM_H__
  3.  
  4. #include <vector>
  5.  
  6. #include "Opcode.H"
  7.  
  8.  
  9. // The VM class defines a stack-based implementation of an interpreter.  The
  10. // bytecode stream to execute is passed in to this class's constructor.  Once
  11. // the Exec() member function is called, the class will handle executing all
  12. // the specified bytecode stream.  Execution will continue until the entire
  13. // bytecode stream has been process.
  14. class VM {
  15. public:
  16.   VM( const char *stream, size_t size );
  17.   ~VM();
  18.  
  19.   void Exec();
  20.  
  21.  
  22. protected:
  23.   // Below are all of the various opcode handlers.  They are responsible for
  24.   // actually evaluating the bytecode stream.  They will modify the contents
  25.   // of the stack as needed.
  26.  
  27.   bool HandleBinOp( Opcode op );
  28.  
  29.   bool HandlePush( Opcode op );
  30.   bool HandlePop( Opcode op );
  31.   bool HandleDupe( Opcode op );
  32.  
  33.   bool HandleLoad( Opcode op );
  34.   bool HandleStore( Opcode op );
  35.  
  36.   bool HandleJump( Opcode op );
  37.   bool HandleIfZero( Opcode op );
  38.  
  39.  
  40. protected:
  41.   // This function returns the new opcode in the bytecode stream.  This
  42.   // implicitly increments the instruction pointer to point to next
  43.   // opcode.
  44.   Opcode GetNextOpcode();
  45.  
  46.   // Some opcodes contain arguments in the bytecode stream.  This function
  47.   // fetches the opcode's argument and increments the instruction pointer to
  48.   // skip the argument.
  49.   int GetOpcodeArg();
  50.  
  51.   // The opcode handlers call this function to spew some text to stdout along
  52.   // with the current stack contents.  By convention, the given string should
  53.   // not contain a newline, and this function should be called once the opcode
  54.   // handler is finished modifing the stack.
  55.   void Trace( char *fmt, ... );
  56.  
  57.  
  58. protected:
  59.   // These two functions simply hide the stack pushing/poping.  The main
  60.   // reason they exist is to make the std::vector interface a little easier to
  61.   // deal with.  Mainly because it is convient for Pop() to actually the int
  62.   // that was popped off.
  63.   void Push( int arg );
  64.   int Pop();
  65.  
  66.  
  67. private:
  68.   // Define the data member that contains all of the function pointers to the
  69.   // various opcode handlers.  The index into this array is the actual opcode's
  70.   // value.
  71.   typedef bool ( VM::*OpcodeHandler )( Opcode );
  72.   OpcodeHandler m_opHandlers[ Num_Opcode ];
  73.  
  74.   // Define a VM's actual execution stack.  Note that the load and store
  75.   // instructions need to be able to modify arbitrary locations in the stack;
  76.   // therefore, it is more efficient to use a vector than a stack.
  77.   typedef std::vector<int> RuntimeStack;
  78.   RuntimeStack m_stack;
  79.  
  80.   // The instruction pointer points to the next instruction to exectue.
  81.   const char *m_ip;
  82.  
  83.   // These two data members keeps track of the bytecode stream that the VM
  84.   // should execute.
  85.   const char *m_stream;
  86.   size_t m_streamSize;
  87. };
  88.  
  89.  
  90. #endif // __VM_H__
  91.