-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathquattro_vm.h
240 lines (208 loc) · 6.46 KB
/
quattro_vm.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
//@+leo-ver=4-thin
//@+node:leonardoce.20090629082550.191:@thin quattro_vm.h
//@@language c
#ifndef __QUATTROVM_H
#define __QUATTROVM_H
#include "quattro_parser.h"
#include "utils.h"
#include "config.h"
#define longtobool(x) (((x)!=0)?1:0)
#define FORTHCODEADDRESS_NULL 0
#define FORTHDATAADDRESS_NULL 0
#define MAX_WORD_LISTS 64
#define MAX_CODE_SPACE 4096
#define MAX_DATA_SPACE 1048576
#define MAX_DATA_STACK 96
#define MAX_RET_STACK 96
#define MAX_NATIVE_INSTRUCTIONS 256
//@+others
//@+node:leonardoce.20090629082550.232:Dati VM
#define LEOFORTH_VERSION "4Forth 0.1 (06/2009)"
//@nonl
//@-node:leonardoce.20090629082550.232:Dati VM
//@+node:leonardoce.20090629082550.213:ForthWordList
/****c* forth/ForthWordList
* NAME
* ForthWordList
* FUNCTION
* Incapsula una wordlist del forth ovvero associa il nome di una parola
* a un indirizzo univoco nel settore codice del dizionario forth.
* Le parole vengono create come parole non immediate. Possono essere marcate
* immediate attraverso la "MarkImmediate"
* SOURCE
*/
typedef struct ForthWordList ForthWordList;
struct ForthWordList {
DynamicVector *wordNames;
DynamicVector *wordAddrs;
DynamicVector *wordIsImmediate;
};
ForthWordList *ForthWordList_New();
ForthWordList *ForthWordList_NewFromStream(InputStream *in);
void ForthWordList_SaveOnFile(ForthWordList* self, OutputStream *out);
void ForthWordList_Delete(ForthWordList* self);
void ForthWordList_Assoc(ForthWordList* self, const char *wordName, ForthInstructionAddress addr);
void ForthWordList_MarkImmediate(ForthWordList* self, const char *wordName);
int ForthWordList_IsImmediate(ForthWordList* self, const char *wordName);
ForthInstructionAddress ForthWordList_Find(ForthWordList* self, const char *wordName);
void ForthWordList_SetCount(ForthWordList *self, int c);
int ForthWordList_Count(ForthWordList *self);
/****/
//@-node:leonardoce.20090629082550.213:ForthWordList
//@+node:leonardoce.20090629082550.214:ForthInstruction
/****c* forth/ForthInstruction
* NAME
* ForthInstruction
* FUNCTION
* Questa struttura rappresenta una istruzione forth gli stack e
* le aree di memoria. Inoltre a questo la struttura ha un riferimento
* ad un parser che viene utilizzato per prelevare le parole.
* SOURCE
*/
typedef enum ForthInstructionType ForthInstructionType;
enum ForthInstructionType {
NOP_INSTRUCTION,
NATIVE_INSTRUCTION,
RET_INSTRUCTION,
CALL_INSTRUCTION,
DEBUGGER_INSTRUCTION,
DATAPUSH_INSTRUCTION,
STOP_VM,
JUMP_INSTRUCTION,
JFALSE_INSTRUCTION,
RET_FROM_INSTRUCTION,
TO_RET_INSTRUCTION,
RET_FETCH_INSTRUCTION
};
typedef struct ForthInstruction ForthInstruction;
struct ForthInstruction {
ForthInstructionType instructionType;
int instructionDest;
};
/****/
//@-node:leonardoce.20090629082550.214:ForthInstruction
//@+node:leonardoce.20090629082550.215:ForthVm
/****c* forth/ForthVm
* NAME
* ForthVM
* METHODS
* ForthVm_InitForthWordList
* ForthVm_AllocateForthInstruction
* ForthVm_Run
* ForthVm_RegisterNativeInstruction
* FUNCTION
* Questa classe rappresenta la macchina virtuale forth
* SOURCE
*/
typedef enum ForthVm_State ForthVm_State;
enum ForthVm_State {
VM_INTERPRETER,
VM_COMPILER
};
typedef struct ForthVm ForthVm;
typedef void (*NativeInstruction)(ForthVm *self);
struct ForthVm {
/* word lists */
DynamicVector *wordLists;
int compilerWordListIdx;
ForthWordList *forthWordList;
int searchOrder[MAX_WORD_LISTS];
int searchOrderLen;
char *lastDefinedWord;
/* native instructions */
NativeInstruction nativeInstructionTable[MAX_NATIVE_INSTRUCTIONS];
const char *nativeInstructionTableDesc[MAX_NATIVE_INSTRUCTIONS];
int nativeInstructionPtr;
/* debug info */
DynamicVector *debugInfo;
/* memory spaces */
ForthInstruction codeSpace[MAX_CODE_SPACE];
ForthInstructionAddress hereCodePtr;
ForthInstructionAddress instructionPtr;
ForthStream *parser;
char dataSpace[MAX_DATA_SPACE];
ForthDataAddress hereDataPtr;
/* stacks */
ForthCell dataStack[MAX_DATA_STACK];
int dataStackPtr;
ForthCell retStack[MAX_DATA_STACK];
int retStackPtr;
/* state */
ForthVm_State internalState;
int traceEnabled;
int traceMaxWidth;
int wordTraceEnabled;
int exitFlag;
};
ForthVm *ForthVm_New();
ForthVm *ForthVm_NewFromFile(InputStream *in);
void ForthVm_SaveOnFile(ForthVm *self, OutputStream *out);
void ForthVm_Error(ForthVm *self, const char *msg);
void ForthVm_Output(ForthVm *self, const char *msg);
void ForthVm_Delete(ForthVm *self);
void ForthVm_DumpDataStack(ForthVm *self);
void ForthVm_DumpRetStack(ForthVm *self);
ForthInstructionAddress ForthVm_Lookup(ForthVm *self, const char *wordName);
void ForthVm_SetParser(ForthVm *self, ForthStream *value);
ForthStream *ForthVm_GetParser(ForthVm *self);
void ForthVm_Feed(ForthVm *self);
ForthInstructionAddress ForthVm_IsImmediate(ForthVm *self, const char *wordName);
void ForthVm_MarkImmediate(ForthVm *self, const char *wordName);
const char *ForthVm_ReverseLookup(ForthVm *self, ForthInstructionAddress addr);
/****/
/****m* forth/ForthVm_InitForthWordList
* NAME
* ForthVm_InitForthWordList
* FUNCTION
* Inizializza la wordlist principale del forth
* SOURCE
*/
void ForthVm_InitForthWordList(ForthVm *self);
/****/
/****m* forth/ForthVm_AllocateForthInstruction
* NAME
* ForthVm_AllocateForthInstruction
* FUNCTION
* Alloca spazio per una nuova istruzione forth
* NOTES
* Se non c'e' piu' spazio disponibile la vm abortisce.
* SOURCE
*/
ForthInstructionAddress ForthVm_AllocateForthInstruction(ForthVm *self);
/****/
/****m* forth/ForthVm_Run
* NAME
* ForthVm_Run
* FUNCTION
* Esegue la macchina virtuale finche' non trova una istruzione
* di stop
* SOURCE
*/
void ForthVm_Run(ForthVm* self);
/****/
/****m* forth/ForthVm_RegisterNativeInstruction
* NAME
* ForthVm_RegisterNativeInstruction
* FUNCTION
* Compila una chiamata all'istruzione nativa specificata
* nei parametri e inserisce, nella forth wordlist, il
* richiamo al codice compilato.
* SOURCE
*/
void ForthVm_RegisterNativeInstruction(ForthVm* self, const char *wordName, NativeInstruction instr);
/****/
/****m* forth/ForthVm_LookupNativeId
* NAME
* ForthVm_LookupNativeId
* FUNCTION
* Ritorna l'identificativo nativo, da usarsi all'interno
* del bytecode, di una certa funzione che implementa una primitiva
* SOURCE
*/
int ForthVm_LookupNativeId(ForthVm *self, NativeInstruction ni);
/****/
//@-node:leonardoce.20090629082550.215:ForthVm
//@-others
#endif
//@-node:leonardoce.20090629082550.191:@thin quattro_vm.h
//@-leo