-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathquattro_istruzioni_ffi.c
353 lines (290 loc) · 10.5 KB
/
quattro_istruzioni_ffi.c
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
//@+leo-ver=4-thin
//@+node:leonardoce.20090629082550.69:@thin quattro_istruzioni_ffi.c
//@@language c
//@@tabwidth -4
//@+others
//@+node:leonardoce.20090629082550.70:int
#include <stdio.h>
#include <string.h>
#include "quattro_istruzioni.h"
#include "quattro_debug.h"
#ifdef OS_WINDOWS
#include <windows.h>
#endif
#ifdef OS_LINUX
#include <dlfcn.h>
#endif
typedef int (*genericProc)();
ForthVm *currentVm;
void Instruction_CMalloc(ForthVm *self) {
/* ( u -- addr ) */
if( self->dataStackPtr < 1 ) {
ForthVm_Error(self, "Data stack underflow.");
return;
}
int len=self->dataStack[self->dataStackPtr-1];
void *newMem = (void *)malloc(len+1);
self->dataStack[self->dataStackPtr-1]=(int)newMem;
}
//@-node:leonardoce.20090629082550.70:int
//@+node:leonardoce.20090629082550.71:Instruction_CFree
void Instruction_CFree(ForthVm *self) {
/* ( addr -- ) */
if( self->dataStackPtr < 1 ) {
ForthVm_Error(self, "Data stack underflow.");
return;
}
self->dataStackPtr--;
int addr=self->dataStack[self->dataStackPtr];
free((void *)addr);
}
//@-node:leonardoce.20090629082550.71:Instruction_CFree
//@+node:leonardoce.20090629082550.72:Instruction_CMemStore
void Instruction_CMemStore(ForthVm *self) {
/* ( data c-addr -- ) */
if( self->dataStackPtr < 2 ) {
ForthVm_Error(self, "Data stack underflow.");
return;
}
char *addr=(char *)self->dataStack[self->dataStackPtr-1];
int data=self->dataStack[self->dataStackPtr-2];
self->dataStackPtr-=2;
*addr=(char)data;
}
//@-node:leonardoce.20090629082550.72:Instruction_CMemStore
//@+node:leonardoce.20090629082550.73:Instruction_CMemCellStore
void Instruction_CMemCellStore(ForthVm *self) {
/* ( data c-addr -- ) */
if( self->dataStackPtr < 2 ) {
ForthVm_Error(self, "Data stack underflow.");
return;
}
int *addr=(int *)self->dataStack[self->dataStackPtr-1];
int data=self->dataStack[self->dataStackPtr-2];
self->dataStackPtr-=2;
*addr=(int)data;
}
//@-node:leonardoce.20090629082550.73:Instruction_CMemCellStore
//@+node:leonardoce.20090629082550.74:Instruction_CMemFetch
void Instruction_CMemFetch(ForthVm *self) {
/* ( c-addr -- c ) */
if( self->dataStackPtr < 1 ) {
ForthVm_Error(self, "Data stack overflow.");
return;
}
char *addr=(char *)self->dataStack[self->dataStackPtr-1];
self->dataStack[self->dataStackPtr-1]=*addr;
}
//@-node:leonardoce.20090629082550.74:Instruction_CMemFetch
//@+node:leonardoce.20090629082550.75:Instruction_CMemCellFetch
void Instruction_CMemCellFetch(ForthVm *self) {
/* ( c-addr -- c ) */
if( self->dataStackPtr < 1 ) {
ForthVm_Error(self, "Data stack overflow.");
return;
}
int *addr=(int *)self->dataStack[self->dataStackPtr-1];
self->dataStack[self->dataStackPtr-1]=*addr;
}
//@-node:leonardoce.20090629082550.75:Instruction_CMemCellFetch
//@+node:leonardoce.20090629082550.76:Instruction_CLoadLibrary
void Instruction_CLoadLibrary(ForthVm *self) {
/* ( libname -- modaddress ) */
if( self->dataStackPtr < 1 ) {
ForthVm_Error(self, "Data stack underflow.");
return;
}
int result;
#ifdef OS_WINDOWS
result=(int)LoadLibrary((char *)self->dataStack[self->dataStackPtr-1]);
#endif
#ifdef OS_LINUX
result=(int)dlopen((char *)self->dataStack[self->dataStackPtr-1], RTLD_LAZY);
#endif
self->dataStack[self->dataStackPtr-1]=result;
}
//@-node:leonardoce.20090629082550.76:Instruction_CLoadLibrary
//@+node:leonardoce.20090629082550.77:Instruction_CFreeLibrary
void Instruction_CFreeLibrary(ForthVm *self) {
/* ( modaddress -- ) */
if( self->dataStackPtr < 1 ) {
ForthVm_Error(self, "Data stack underflow.");
return;
}
int result;
#ifdef OS_WINDOWS
result=(int)FreeLibrary((void *)self->dataStack[self->dataStackPtr-1]);
#endif
#ifdef OS_LINUX
result=(int)dlclose((void *)self->dataStack[self->dataStackPtr-1]);
#endif
self->dataStackPtr--;
}
//@-node:leonardoce.20090629082550.77:Instruction_CFreeLibrary
//@+node:leonardoce.20090629082550.78:Instruction_CGetProcAddress
void Instruction_CGetProcAddress(ForthVm *self) {
/* ( libaddr procname -- ) */
if( self->dataStackPtr < 2 ) {
ForthVm_Error(self, "Data stack underflow.");
return;
}
int result;
#ifdef OS_WINDOWS
result=(int)GetProcAddress((void *)self->dataStack[self->dataStackPtr-2], (char *)(void *)self->dataStack[self->dataStackPtr-1]);
#endif
#ifdef OS_LINUX
result=(int)dlsym((void *)self->dataStack[self->dataStackPtr-2], (char *)(void *)self->dataStack[self->dataStackPtr-1]);
#endif
self->dataStackPtr--;
self->dataStack[self->dataStackPtr-1]=result;
}
//@-node:leonardoce.20090629082550.78:Instruction_CGetProcAddress
//@+node:leonardoce.20090629082550.79:Instruction_CCall
void Instruction_CCall(ForthVm *self) {
/* ( arg*n n procaddr -- result ) */
if( self->dataStackPtr < 2 ) {
ForthVm_Error(self, "Data stack underflow.");
return;
}
int procaddr = self->dataStack[self->dataStackPtr-1];
int argscount = self->dataStack[self->dataStackPtr-2];
int result = 0;
if( self->dataStackPtr < (2+argscount) ) {
ForthVm_Error(self, "Data stack underflow.");
return;
}
genericProc myProc = (genericProc) procaddr;
if( myProc != NULL ) {
if( argscount == 0 ) {
result = myProc( );
} else if( argscount == 1 ) {
result = myProc( self->dataStack[self->dataStackPtr-3] );
} else if( argscount == 2 ) {
result = myProc( self->dataStack[self->dataStackPtr-4], self->dataStack[self->dataStackPtr-3] );
} else if( argscount == 3 ) {
result = myProc( self->dataStack[self->dataStackPtr-5], self->dataStack[self->dataStackPtr-4], self->dataStack[self->dataStackPtr-3] );
} else if( argscount == 4 ) {
result = myProc( self->dataStack[self->dataStackPtr-6], self->dataStack[self->dataStackPtr-5], self->dataStack[self->dataStackPtr-4], self->dataStack[self->dataStackPtr-3] );
} else if( argscount == 5 ) {
result = myProc( self->dataStack[self->dataStackPtr-7], self->dataStack[self->dataStackPtr-6], self->dataStack[self->dataStackPtr-5], self->dataStack[self->dataStackPtr-4], self->dataStack[self->dataStackPtr-3] );
} else if( argscount == 6 ) {
result = myProc( self->dataStack[self->dataStackPtr-8], self->dataStack[self->dataStackPtr-7], self->dataStack[self->dataStackPtr-6], self->dataStack[self->dataStackPtr-5], self->dataStack[self->dataStackPtr-4], self->dataStack[self->dataStackPtr-3] );
} else {
ForthVm_Error(self, "Call not supported.");
return;
}
}
self->dataStackPtr-=2+argscount;
self->dataStackPtr++;
self->dataStack[self->dataStackPtr-1] = result;
}
//@-node:leonardoce.20090629082550.79:Instruction_CCall
//@+node:leonardoce.20090629082550.80:Instruction_AddrToC
void Instruction_AddrToC(ForthVm *self) {
// ( addr -- caddr )
if( self->dataStackPtr < 1 ) {
ForthVm_Error(self, "Data stack underflow.");
return;
}
self->dataStack[self->dataStackPtr-1]+=(int)&self->dataSpace;
}
//@-node:leonardoce.20090629082550.80:Instruction_AddrToC
//@+node:leonardoce.20090629082550.81:Instruction_OsLinux
void Instruction_OsLinux(ForthVm *self) {
// ( -- flag )
if( self->dataStackPtr >= MAX_DATA_STACK ) {
ForthVm_Error(self, "Data stack underflow.");
return;
}
int result=0;
#ifdef OS_LINUX
result=1;
#endif
self->dataStack[self->dataStackPtr]=result;
self->dataStackPtr++;
}
//@-node:leonardoce.20090629082550.81:Instruction_OsLinux
//@+node:leonardoce.20090629082550.82:Instruction_OsWindows
void Instruction_OsWindows(ForthVm *self) {
// ( -- flag )
if( self->dataStackPtr >= MAX_DATA_STACK ) {
ForthVm_Error(self, "Data stack underflow.");
return;
}
int result=0;
#ifdef OS_WINDOWS
result=1;
#endif
self->dataStack[self->dataStackPtr]=result;
self->dataStackPtr++;
}
//@-node:leonardoce.20090629082550.82:Instruction_OsWindows
//@+node:leonardoce.20090629082550.83:cdeclOneParams
int cdeclOneParams(int p0) {
ForthVm *self = currentVm;
int oldPtr = self->instructionPtr;
self->instructionPtr=p0;
ForthVm_Run(self);
self->instructionPtr=oldPtr;
self->exitFlag=FALSE;
self->retStackPtr--;
return 0;
}
//@-node:leonardoce.20090629082550.83:cdeclOneParams
//@+node:leonardoce.20090629082550.84:cdeclTwoParams
int cdeclTwoParams(int p0, int p1) {
ForthVm *self = currentVm;
if( self->dataStackPtr >= MAX_DATA_STACK ) {
ForthVm_Error(self, "Data stack underflow.");
return 0;
}
self->dataStack[self->dataStackPtr++] = p1;
int oldPtr = self->instructionPtr;
self->instructionPtr=p0;
ForthVm_Run(self);
self->instructionPtr=oldPtr;
self->exitFlag=FALSE;
self->retStackPtr--;
return 0;
}
//@-node:leonardoce.20090629082550.84:cdeclTwoParams
//@+node:leonardoce.20090629082550.85:Instruction_GetCallback
void Instruction_GetCallback(ForthVm *self) {
// ( nargs -- addr )
if( self->dataStackPtr < 1 ) {
ForthVm_Error(self, "Data stack underflow.");
return;
}
currentVm = self;
int nargs = self->dataStack[self->dataStackPtr-1];
if( nargs == 1 ) {
self->dataStack[self->dataStackPtr-1]=(int)&cdeclOneParams;
} else if( nargs == 1 ) {
self->dataStack[self->dataStackPtr-2]=(int)&cdeclTwoParams;
} else {
ForthVm_Error(self, "Unsupported callback");
return;
}
}
//@-node:leonardoce.20090629082550.85:Instruction_GetCallback
//@+node:leonardoce.20090629082550.86:ForthVm_RegistraIstruzioniFfi
void ForthVm_RegistraIstruzioniFfi(ForthVm *self) {
ForthVm_RegisterNativeInstruction(self, "cmalloc", Instruction_CMalloc);
ForthVm_RegisterNativeInstruction(self, "cfree", Instruction_CFree);
ForthVm_RegisterNativeInstruction(self, "cmem!", Instruction_CMemStore);
ForthVm_RegisterNativeInstruction(self, "cmem@", Instruction_CMemFetch);
ForthVm_RegisterNativeInstruction(self, "cmemcell!", Instruction_CMemCellStore);
ForthVm_RegisterNativeInstruction(self, "cmemcell@", Instruction_CMemCellFetch);
ForthVm_RegisterNativeInstruction(self, "cloadlibrary", Instruction_CLoadLibrary);
ForthVm_RegisterNativeInstruction(self, "cfreelibrary", Instruction_CFreeLibrary);
ForthVm_RegisterNativeInstruction(self, "cprocaddress", Instruction_CGetProcAddress);
ForthVm_RegisterNativeInstruction(self, "ccall", Instruction_CCall);
ForthVm_RegisterNativeInstruction(self, "addr>c", Instruction_AddrToC);
ForthVm_RegisterNativeInstruction(self, "oswindows", Instruction_OsWindows);
ForthVm_RegisterNativeInstruction(self, "oslinux", Instruction_OsLinux);
ForthVm_RegisterNativeInstruction(self, "getcallback", Instruction_GetCallback);
}
//@-node:leonardoce.20090629082550.86:ForthVm_RegistraIstruzioniFfi
//@-others
//@-node:leonardoce.20090629082550.69:@thin quattro_istruzioni_ffi.c
//@-leo