Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/thirdparty/graphite/src/call_machine.cpp
9902 views
1
// SPDX-License-Identifier: MIT OR MPL-2.0 OR LGPL-2.1-or-later OR GPL-2.0-or-later
2
// Copyright 2010, SIL International, All rights reserved.
3
4
// This call threaded interpreter implmentation for machine.h
5
// Author: Tim Eves
6
7
// Build either this interpreter or the direct_machine implementation.
8
// The call threaded interpreter is portable across compilers and
9
// architectures as well as being useful to debug (you can set breakpoints on
10
// opcodes) but is slower that the direct threaded interpreter by a factor of 2
11
12
#include <cassert>
13
#include <cstring>
14
#include <graphite2/Segment.h>
15
#include "inc/Machine.h"
16
#include "inc/Segment.h"
17
#include "inc/Slot.h"
18
#include "inc/Rule.h"
19
20
// Disable the unused parameter warning as th compiler is mistaken since dp
21
// is always updated (even if by 0) on every opcode.
22
#ifdef __GNUC__
23
#pragma GCC diagnostic ignored "-Wunused-parameter"
24
#endif
25
26
#define registers const byte * & dp, vm::Machine::stack_t * & sp, \
27
vm::Machine::stack_t * const sb, regbank & reg
28
29
// These are required by opcodes.h and should not be changed
30
#define STARTOP(name) bool name(registers) REGPARM(4);\
31
bool name(registers) {
32
#define ENDOP return (sp - sb)/Machine::STACK_MAX==0; \
33
}
34
35
#define EXIT(status) { push(status); return false; }
36
37
// This is required by opcode_table.h
38
#define do_(name) instr(name)
39
40
41
using namespace graphite2;
42
using namespace vm;
43
44
struct regbank {
45
slotref is;
46
slotref * map;
47
SlotMap & smap;
48
slotref * const map_base;
49
const instr * & ip;
50
uint8 direction;
51
int8 flags;
52
Machine::status_t & status;
53
};
54
55
typedef bool (* ip_t)(registers);
56
57
// Pull in the opcode definitions
58
// We pull these into a private namespace so these otherwise common names dont
59
// pollute the toplevel namespace.
60
namespace {
61
#define smap reg.smap
62
#define seg smap.segment
63
#define is reg.is
64
#define ip reg.ip
65
#define map reg.map
66
#define mapb reg.map_base
67
#define flags reg.flags
68
#define dir reg.direction
69
#define status reg.status
70
71
#include "inc/opcodes.h"
72
73
#undef smap
74
#undef seg
75
#undef is
76
#undef ip
77
#undef map
78
#undef mapb
79
#undef flags
80
#undef dir
81
}
82
83
Machine::stack_t Machine::run(const instr * program,
84
const byte * data,
85
slotref * & map)
86
87
{
88
assert(program != 0);
89
90
// Declare virtual machine registers
91
const instr * ip = program-1;
92
const byte * dp = data;
93
stack_t * sp = _stack + Machine::STACK_GUARD,
94
* const sb = sp;
95
regbank reg = {*map, map, _map, _map.begin()+_map.context(), ip, _map.dir(), 0, _status};
96
97
// Run the program
98
while ((reinterpret_cast<ip_t>(*++ip))(dp, sp, sb, reg)) {}
99
const stack_t ret = sp == _stack+STACK_GUARD+1 ? *sp-- : 0;
100
101
check_final_stack(sp);
102
map = reg.map;
103
*map = reg.is;
104
return ret;
105
}
106
107
// Pull in the opcode table
108
namespace {
109
#include "inc/opcode_table.h"
110
}
111
112
const opcode_t * Machine::getOpcodeTable() throw()
113
{
114
return opcode_table;
115
}
116
117