|
1 /* GRAPHITE2 LICENSING |
|
2 |
|
3 Copyright 2010, SIL International |
|
4 All rights reserved. |
|
5 |
|
6 This library is free software; you can redistribute it and/or modify |
|
7 it under the terms of the GNU Lesser General Public License as published |
|
8 by the Free Software Foundation; either version 2.1 of License, or |
|
9 (at your option) any later version. |
|
10 |
|
11 This program is distributed in the hope that it will be useful, |
|
12 but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
14 Lesser General Public License for more details. |
|
15 |
|
16 You should also have received a copy of the GNU Lesser General Public |
|
17 License along with this library in the file named "LICENSE". |
|
18 If not, write to the Free Software Foundation, 51 Franklin Street, |
|
19 Suite 500, Boston, MA 02110-1335, USA or visit their web page on the |
|
20 internet at http://www.fsf.org/licenses/lgpl.html. |
|
21 |
|
22 Alternatively, the contents of this file may be used under the terms of the |
|
23 Mozilla Public License (http://mozilla.org/MPL) or the GNU General Public |
|
24 License, as published by the Free Software Foundation, either version 2 |
|
25 of the License or (at your option) any later version. |
|
26 */ |
|
27 // This direct threaded interpreter implmentation for machine.h |
|
28 // Author: Tim Eves |
|
29 |
|
30 // Build either this interpreter or the call_machine implementation. |
|
31 // The direct threaded interpreter is relies upon a gcc feature called |
|
32 // labels-as-values so is only portable to compilers that support the |
|
33 // extension (gcc only as far as I know) however it should build on any |
|
34 // architecture gcc supports. |
|
35 // This is twice as fast as the call threaded model and is likely faster on |
|
36 // inorder processors with short pipelines and little branch prediction such |
|
37 // as the ARM and possibly Atom chips. |
|
38 |
|
39 |
|
40 #include <cassert> |
|
41 #include <cstring> |
|
42 #include "inc/Machine.h" |
|
43 #include "inc/Segment.h" |
|
44 #include "inc/Slot.h" |
|
45 #include "inc/Rule.h" |
|
46 |
|
47 #define STARTOP(name) name: { |
|
48 #define ENDOP }; goto *((sp - sb)/Machine::STACK_MAX ? &&end : *++ip); |
|
49 #define EXIT(status) { push(status); goto end; } |
|
50 |
|
51 #define do_(name) &&name |
|
52 |
|
53 |
|
54 using namespace graphite2; |
|
55 using namespace vm; |
|
56 |
|
57 namespace { |
|
58 |
|
59 const void * direct_run(const bool get_table_mode, |
|
60 const instr * program, |
|
61 const byte * data, |
|
62 Machine::stack_t * stack, |
|
63 slotref * & __map, |
|
64 SlotMap * __smap=0) |
|
65 { |
|
66 // We need to define and return to opcode table from within this function |
|
67 // other inorder to take the addresses of the instruction bodies. |
|
68 #include "inc/opcode_table.h" |
|
69 if (get_table_mode) |
|
70 return opcode_table; |
|
71 |
|
72 // Declare virtual machine registers |
|
73 const instr * ip = program; |
|
74 const byte * dp = data; |
|
75 Machine::stack_t * sp = stack + Machine::STACK_GUARD, |
|
76 * const sb = sp; |
|
77 SlotMap & smap = *__smap; |
|
78 Segment & seg = smap.segment; |
|
79 slotref is = *__map, |
|
80 * map = __map, |
|
81 * const mapb = smap.begin()+smap.context(); |
|
82 int8 flags = 0; |
|
83 |
|
84 // start the program |
|
85 goto **ip; |
|
86 |
|
87 // Pull in the opcode definitions |
|
88 #include "inc/opcodes.h" |
|
89 |
|
90 end: |
|
91 __map = map; |
|
92 *__map = is; |
|
93 return sp; |
|
94 } |
|
95 |
|
96 } |
|
97 |
|
98 const opcode_t * Machine::getOpcodeTable() throw() |
|
99 { |
|
100 slotref * dummy; |
|
101 return static_cast<const opcode_t *>(direct_run(true, 0, 0, 0, dummy, 0)); |
|
102 } |
|
103 |
|
104 |
|
105 Machine::stack_t Machine::run(const instr * program, |
|
106 const byte * data, |
|
107 slotref * & is) |
|
108 { |
|
109 assert(program != 0); |
|
110 |
|
111 const stack_t *sp = static_cast<const stack_t *>( |
|
112 direct_run(false, program, data, _stack, is, &_map)); |
|
113 const stack_t ret = sp == _stack+STACK_GUARD+1 ? *sp-- : 0; |
|
114 check_final_stack(sp); |
|
115 return ret; |
|
116 } |
|
117 |