1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/graphite2/src/direct_machine.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,117 @@ 1.4 +/* GRAPHITE2 LICENSING 1.5 + 1.6 + Copyright 2010, SIL International 1.7 + All rights reserved. 1.8 + 1.9 + This library is free software; you can redistribute it and/or modify 1.10 + it under the terms of the GNU Lesser General Public License as published 1.11 + by the Free Software Foundation; either version 2.1 of License, or 1.12 + (at your option) any later version. 1.13 + 1.14 + This program is distributed in the hope that it will be useful, 1.15 + but WITHOUT ANY WARRANTY; without even the implied warranty of 1.16 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 1.17 + Lesser General Public License for more details. 1.18 + 1.19 + You should also have received a copy of the GNU Lesser General Public 1.20 + License along with this library in the file named "LICENSE". 1.21 + If not, write to the Free Software Foundation, 51 Franklin Street, 1.22 + Suite 500, Boston, MA 02110-1335, USA or visit their web page on the 1.23 + internet at http://www.fsf.org/licenses/lgpl.html. 1.24 + 1.25 +Alternatively, the contents of this file may be used under the terms of the 1.26 +Mozilla Public License (http://mozilla.org/MPL) or the GNU General Public 1.27 +License, as published by the Free Software Foundation, either version 2 1.28 +of the License or (at your option) any later version. 1.29 +*/ 1.30 +// This direct threaded interpreter implmentation for machine.h 1.31 +// Author: Tim Eves 1.32 + 1.33 +// Build either this interpreter or the call_machine implementation. 1.34 +// The direct threaded interpreter is relies upon a gcc feature called 1.35 +// labels-as-values so is only portable to compilers that support the 1.36 +// extension (gcc only as far as I know) however it should build on any 1.37 +// architecture gcc supports. 1.38 +// This is twice as fast as the call threaded model and is likely faster on 1.39 +// inorder processors with short pipelines and little branch prediction such 1.40 +// as the ARM and possibly Atom chips. 1.41 + 1.42 + 1.43 +#include <cassert> 1.44 +#include <cstring> 1.45 +#include "inc/Machine.h" 1.46 +#include "inc/Segment.h" 1.47 +#include "inc/Slot.h" 1.48 +#include "inc/Rule.h" 1.49 + 1.50 +#define STARTOP(name) name: { 1.51 +#define ENDOP }; goto *((sp - sb)/Machine::STACK_MAX ? &&end : *++ip); 1.52 +#define EXIT(status) { push(status); goto end; } 1.53 + 1.54 +#define do_(name) &&name 1.55 + 1.56 + 1.57 +using namespace graphite2; 1.58 +using namespace vm; 1.59 + 1.60 +namespace { 1.61 + 1.62 +const void * direct_run(const bool get_table_mode, 1.63 + const instr * program, 1.64 + const byte * data, 1.65 + Machine::stack_t * stack, 1.66 + slotref * & __map, 1.67 + SlotMap * __smap=0) 1.68 +{ 1.69 + // We need to define and return to opcode table from within this function 1.70 + // other inorder to take the addresses of the instruction bodies. 1.71 + #include "inc/opcode_table.h" 1.72 + if (get_table_mode) 1.73 + return opcode_table; 1.74 + 1.75 + // Declare virtual machine registers 1.76 + const instr * ip = program; 1.77 + const byte * dp = data; 1.78 + Machine::stack_t * sp = stack + Machine::STACK_GUARD, 1.79 + * const sb = sp; 1.80 + SlotMap & smap = *__smap; 1.81 + Segment & seg = smap.segment; 1.82 + slotref is = *__map, 1.83 + * map = __map, 1.84 + * const mapb = smap.begin()+smap.context(); 1.85 + int8 flags = 0; 1.86 + 1.87 + // start the program 1.88 + goto **ip; 1.89 + 1.90 + // Pull in the opcode definitions 1.91 + #include "inc/opcodes.h" 1.92 + 1.93 + end: 1.94 + __map = map; 1.95 + *__map = is; 1.96 + return sp; 1.97 +} 1.98 + 1.99 +} 1.100 + 1.101 +const opcode_t * Machine::getOpcodeTable() throw() 1.102 +{ 1.103 + slotref * dummy; 1.104 + return static_cast<const opcode_t *>(direct_run(true, 0, 0, 0, dummy, 0)); 1.105 +} 1.106 + 1.107 + 1.108 +Machine::stack_t Machine::run(const instr * program, 1.109 + const byte * data, 1.110 + slotref * & is) 1.111 +{ 1.112 + assert(program != 0); 1.113 + 1.114 + const stack_t *sp = static_cast<const stack_t *>( 1.115 + direct_run(false, program, data, _stack, is, &_map)); 1.116 + const stack_t ret = sp == _stack+STACK_GUARD+1 ? *sp-- : 0; 1.117 + check_final_stack(sp); 1.118 + return ret; 1.119 +} 1.120 +