1 /*============================================================================= 2 Copyright (c) 2001-2014 Joel de Guzman 3 4 Distributed under the Boost Software License, Version 1.0. (See accompanying 5 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 6 =============================================================================*/ 7 #include "vm.hpp" 8 9 namespace client 10 { execute(std::vector<int> const & code)11 void vmachine::execute(std::vector<int> const& code) 12 { 13 auto pc = code.begin(); 14 auto locals = stack.begin(); 15 stack_ptr = stack.begin(); 16 17 while (pc != code.end()) 18 { 19 switch (*pc++) 20 { 21 case op_neg: 22 stack_ptr[-1] = -stack_ptr[-1]; 23 break; 24 25 case op_add: 26 --stack_ptr; 27 stack_ptr[-1] += stack_ptr[0]; 28 break; 29 30 case op_sub: 31 --stack_ptr; 32 stack_ptr[-1] -= stack_ptr[0]; 33 break; 34 35 case op_mul: 36 --stack_ptr; 37 stack_ptr[-1] *= stack_ptr[0]; 38 break; 39 40 case op_div: 41 --stack_ptr; 42 stack_ptr[-1] /= stack_ptr[0]; 43 break; 44 45 case op_load: 46 *stack_ptr++ = locals[*pc++]; 47 break; 48 49 case op_store: 50 --stack_ptr; 51 locals[*pc++] = stack_ptr[0]; 52 break; 53 54 case op_int: 55 *stack_ptr++ = *pc++; 56 break; 57 58 case op_stk_adj: 59 stack_ptr = stack.begin() + *pc++; 60 break; 61 } 62 } 63 } 64 } 65