-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
global.h
417 lines (334 loc) · 8.82 KB
/
global.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
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/*
* This file global.h is part of L1vm.
*
* (c) Copyright Stefan Pietzonke (jay-t@gmx.net), 2022
*
* L1vm is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* L1vm is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with L1vm. If not, see <http://www.gnu.org/licenses/>.
*/
// global.h
#if ! _GNU_SOURCE
#define _GNU_SOURCE 1
#endif
// OS type messsage
#if __CYGWIN__
#pragma message ("OS type: CYGWIN")
#endif
#if __MACH__
#pragma message ("OS type: macOS")
#endif
#if __linux__
#pragma message ("OS type: Linux")
#endif
#if __OpenBSD__
#pragma message ("OS type: OpenBSD")
#endif
#if __FreeBSD__
#pragma message ("OS type: FreeBSD")
#endif
#if __HAIKU__
#pragma message ("OS type: Haiku")
#endif
// set __linux__ on Cygwin, macOS, OpenBSD and FreeBSD
#if __CYGWIN__ || __MACH__ || __OpenBSD__ || __FreeBSD__ || __NetBSD__ || ___HAIKU__
#define __linux__ 1
#endif
// new for parse-infix.c compiler
#include <sys/types.h>
#include <regex.h>
// ------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <signal.h>
#include <stdint.h>
#include <ctype.h>
#include <errno.h>
#include <string.h>
#if __linux__
#include <dlfcn.h>
#endif
#if _WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
#include <Windows.h>
#endif
#include <inttypes.h>
#include <unistd.h>
#include <sys/time.h>
#include <pthread.h>
#include <sched.h>
#include <math.h>
#include <time.h>
#include <assert.h>
#include <limits.h>
// unicode support
#include <wchar.h>
#include <locale.h>
// include user settings file
#include "settings.h"
// internal settings ==========================================================
// handle with care!
// The values you can try on an machine with low RAM, are shown in the comments below!
// This should it make possible to run the L1VM in just a few MB of RAM!
// max input string len
#define MAXINPUT 512
// stacksize in bytes
//#define STACKSIZE 64000
#define STACKSIZE 4096
// code labels name len
#define LABELLEN 64
#if LOW_RAM
#define MODULES 32
#define MODULES_MAXFUNC 256
// Jump call stack, for jsr, jsra: how many jsr and jsra MAXSUBJUMPS can be done without a rts (return) call.
// If you want to save RAM you can set this value to 256!
#define MAXSUBJUMPS 256 // 256 on low RAM
// max number of code labels, and their name length:
#define MAXLABELS 1024 // 1024 on low RAM
//
// max number of Brackets objects
#define MAXOBJECTS 1024
#define MAXLINELEN 256 // MAX LENGTH for strlen_safe string length
#define MAXSTRLEN 256 // MAX LENGTH for maximum string size for static allocation
#else
#define MODULES 1024
#define MODULES_MAXFUNC 4096
// Jump call stack, for jsr, jsra: how many jsr and jsra MAXSUBJUMPS can be done without a rts (return) call.
// If you want to save RAM you can set this value to 256!
#define MAXSUBJUMPS 40960
// max number of code labels:
#define MAXLABELS 40960
// max number of Brackets objects
#define MAXOBJECTS 40960
#define MAXLINELEN 512 // MAX LENGTH for strlen_safe string length, was 4096
#define MAXSTRLEN 4096 // MAX LENGTH for maximum string size for static allocation
#endif
// internal settings end ======================================================
// info strings:
#define COPYRIGHT_STR " 2024/winter (C) 2017-2024 Stefan Pietzonke - software research"
#define VM_VERSION_STR "3.0.0 " // version number
#define MOTTO_STR "let it snow!"
// no user defined definitions below this section! ============================
#if MAXCPUCORES == 0
#error "global.h: ERROR MAXCPUCORES is 0!"
#endif
#if MATH_LIMITS_DOUBLE_FULL == 1
#if MATH_LIMITS == 0
#error "global.h: ERROR YOU HAVE TO DEFINE MATH_LIMITS TO 1 IN ORDER TO USE MATH_LIMITS_DOUBLE_FULL!"
#endif
#endif
// BOUNDSCHECK now always must be set!
#if BOUNDSCHECK == 0
#error "BOUNDSCHECK always must be set to 1!"
#endif
typedef unsigned char U1; /* UBYTE */
typedef int16_t S2; /* INT */
typedef uint16_t U2; /* UINT */
typedef int32_t S4; /* LONGINT */
typedef long long S8; /* 64 bit long */
typedef double F8; /* DOUBLE */
/* set alignment for Android ARM */
#if DO_ALIGNMENT
#define ALIGN __attribute__ ((aligned(8)))
// #pragma message ("ALIGNMENT FOR 64 bit ON")
#else
#define ALIGN
#endif
#define TRUE 1
#define FALSE 0
// machine
#define MAXREG 256 // registers (integer and double float)
#define MAXARGS 64
#define MAXBRACKETLEVEL 64
#define MAXEXPRESSION 32 // expressions per bracket level
#define MAXLINES 4096
#define REM_SB "//"
#define ASM_SB "(ASM)"
#define ASM_END_SB "(ASM_END)"
// data types UNKNOWN (0) -> x
#define UNKNOWN 7
#define BYTE 8
#define WORD 9
#define DOUBLEWORD 10
#define QUADWORD 11
#define DOUBLEFLOAT 12
#define STACK_BYTE 0
#define STACK_QUADWORD 1
#define STACK_DOUBLEFLOAT 2
#define STACK_UNSET 3
#define STRING 13
// translate.h:
#define INTEGER 14
#define DOUBLE 15
#define RUNNING 1
#define STOP 0
// ERROR codes returned by VM
#define ERR_FILE_OK 0
#define ERR_FILE_OPEN -1
#define ERR_FILE_CLOSE -2
#define ERR_FILE_READ -3
#define ERR_FILE_WRITE -4
#define ERR_FILE_NUMBER -5
#define ERR_FILE_EOF -6
#define ERR_FILE_FPOS -7
// for time functions
// struct tm *tm;
struct threaddata
{
U1 *sp; // stack pointer of mother thread
U1 *sp_top; // stack pointer top of mother thread
U1 *sp_bottom; // stack botttom of mother thread
U1 *sp_thread; // stack pointer of new thread
U1 *sp_top_thread;
U1 *sp_bottom_thread;
S8 ep_startpos ALIGN; // code startpos for new thread
pthread_t id; // thread ID
U1 status; // thread status
U1 *data; // own thread data
};
struct t_var
{
U1 type;
U1 digitstr[MAXSTRLEN];
U1 digitstr_type;
U1 base;
};
struct data_info
{
U1 name[MAXSTRLEN];
S8 offset ALIGN;
S8 size ALIGN;
S8 end ALIGN;
S8 type_size ALIGN;
U1 type;
// compiler
U1 value_str[MAXSTRLEN];
U1 type_str[2];
U1 constant; // set to one if variable is constant
};
// compiler, set variables as unused/used
// to get defined but unused variables
struct data_info_var
{
U1 used;
};
#define MAXOPCODES 61
#if ! JIT_COMPILER
struct opcode
{
U1 op[16];
S2 args;
U1 type[4];
};
#endif
// compiler:
struct translate
{
U1 op[32];
S2 args;
U1 type[4];
U1 assemb_op;
};
struct label
{
U1 name[LABELLEN];
S8 pos;
};
struct call_label
{
U1 name[LABELLEN];
S8 pos;
};
// shell arguments
#define MAXSHELLARGS 32
#define MAXSHELLARGLEN 1048576
// This is type of function we will generate
// for JIT-compiler
typedef void (*Func)(void);
struct JIT_code
{
Func fn;
U1 used;
};
// ranges for variable assign checks
// store the variable name and the min/max variable names
struct range
{
U1 varname[MAXSTRLEN];
U1 min[MAXSTRLEN];
U1 max[MAXSTRLEN];
};
// 61 opcodes
#define PUSHB 0
#define PUSHW 1
#define PUSHDW 2
#define PUSHQW 3
#define PUSHD 4
#define PULLB 5
#define PULLW 6
#define PULLDW 7
#define PULLQW 8
#define PULLD 9
#define ADDI 10 // in JIT-Compiler
#define SUBI 11 //
#define MULI 12 //
#define DIVI 13 //
#define ADDD 14 // in JIT-compiler
#define SUBD 15 //
#define MULD 16 //
#define DIVD 17 //
#define SMULI 18
#define SDIVI 19
#define ANDI 20 // in JIT-compiler
#define ORI 21 // in JIT-compiler
#define BANDI 22 // in JIT-compiler
#define BORI 23 // in JIT-compiler
#define BXORI 24 // in JIT-compiler
#define MODI 25
#define EQI 26 // in JIT-compiler
#define NEQI 27 //
#define GRI 28 //
#define LSI 29 //
#define GREQI 30 //
#define LSEQI 31 //
#define EQD 32 // in JIT-Compiler
#define NEQD 33 //
#define GRD 34 //
#define LSD 35 //
#define GREQD 36 //
#define LSEQD 37 //
#define JMP 38 // in JIT-compiler
#define JMPI 39 //
#define STPUSHB 40
#define STPOPB 41
#define STPUSHI 42
#define STPOPI 43
#define STPUSHD 44
#define STPOPD 45
#define LOADA 46
#define LOADD 47
#define INTR0 48
#define INTR1 49
// superopcodes for counter loops
#define INCLSIJMPI 50
#define DECGRIJMPI 51
#define MOVI 52 // in JIT_compiler
#define MOVD 53 //
#define LOADL 54
#define JMPA 55
#define JSR 56
#define JSRA 57
#define RTS 58
#define LOAD 59
#define NOTI 60