File: pf_guts.h

package info (click to toggle)
pforth 21-11
  • links: PTS
  • area: main
  • in suites: lenny, squeeze, wheezy
  • size: 820 kB
  • ctags: 873
  • sloc: ansic: 5,050; makefile: 102
file content (565 lines) | stat: -rw-r--r-- 14,620 bytes parent folder | download | duplicates (5)
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
/* @(#) pf_guts.h 98/01/28 1.4 */
#ifndef _pf_guts_h
#define _pf_guts_h

/***************************************************************
** Include file for PForth, a Forth based on 'C'
**
** Author: Phil Burk
** Copyright 1994 3DO, Phil Burk, Larry Polansky, Devid Rosenboom
**
** The pForth software code is dedicated to the public domain,
** and any third party may reproduce, distribute and modify
** the pForth software code or any derivative works thereof
** without any compensation or license.  The pForth software
** code is provided on an "as is" basis without any warranty
** of any kind, including, without limitation, the implied
** warranties of merchantability and fitness for a particular
** purpose and their equivalents under the laws of any jurisdiction.
**
***************************************************************/

/*
** PFORTH_VERSION changes when PForth is modified and released.
** See README file for version info.
*/
#define PFORTH_VERSION "21"

/*
** PFORTH_FILE_VERSION changes when incompatible changes are made
** in the ".dic" file format.
**
** FV3 - 950225 - Use ABS_TO_CODEREL for CodePtr. See file "pf_save.c".
** FV4 - 950309 - Added NameSize and CodeSize to pfSaveForth().
** FV5 - 950316 - Added Floats and reserved words.
** FV6 - 961213 - Added ID_LOCAL_PLUSSTORE, ID_COLON_P, etc.
** FV7 - 971203 - Added ID_FILL, (1LOCAL@),  etc., ran out of reserved, resorted.
** FV8 - 980818 - Added Endian flag.
*/
#define PF_FILE_VERSION (8)   /* Bump this whenever primitives added. */
#define PF_EARLIEST_FILE_VERSION (8)  /* earliest one still compatible */

/***************************************************************
** Sizes and other constants
***************************************************************/

#define TIB_SIZE (256)

#ifndef FALSE
	#define FALSE (0)
#endif
#ifndef TRUE
	#define TRUE (1)
#endif

#define FFALSE (0)
#define FTRUE (-1)
#define BLANK (' ')

#define FLAG_PRECEDENCE (0x80)
#define FLAG_IMMEDIATE  (0x40)
#define FLAG_SMUDGE     (0x20)
#define MASK_NAME_SIZE  (0x1F)

/* Debug TRACE flags */
#define TRACE_INNER     (0x0002)
#define TRACE_COMPILE   (0x0004)
#define TRACE_SPECIAL   (0x0008)

/* Numeric types returned by NUMBER? */
#define NUM_TYPE_BAD    (0)
#define NUM_TYPE_SINGLE (1)
#define NUM_TYPE_DOUBLE (2)
#define NUM_TYPE_FLOAT  (3)

#define CREATE_BODY_OFFSET  (3*sizeof(cell))

/***************************************************************
** Primitive Token IDS
** Do NOT change the order of these IDs or dictionary files will break!
***************************************************************/
enum cforth_primitive_ids
{
	ID_EXIT = 0,  /* ID_EXIT must always be zero. */
/* Do NOT change the order of these IDs or dictionary files will break! */
	ID_1MINUS,
	ID_1PLUS,
	ID_2DUP,
	ID_2LITERAL,
	ID_2LITERAL_P,
	ID_2MINUS,
	ID_2OVER,
	ID_2PLUS,
	ID_2SWAP,
	ID_2_R_FETCH,
	ID_2_R_FROM,
	ID_2_TO_R,
	ID_ACCEPT,
	ID_ALITERAL,
	ID_ALITERAL_P,
	ID_ALLOCATE,
	ID_AND,
	ID_ARSHIFT,
	ID_BAIL,
	ID_BODY_OFFSET,
	ID_BRANCH,
	ID_BYE,
	ID_CALL_C,
	ID_CFETCH,
	ID_CMOVE,
	ID_CMOVE_UP,
	ID_COLON,
	ID_COLON_P,
	ID_COMPARE,
	ID_COMP_EQUAL,
	ID_COMP_GREATERTHAN,
	ID_COMP_LESSTHAN,
	ID_COMP_NOT_EQUAL,
	ID_COMP_U_GREATERTHAN,
	ID_COMP_U_LESSTHAN,
	ID_COMP_ZERO_EQUAL,
	ID_COMP_ZERO_GREATERTHAN,
	ID_COMP_ZERO_LESSTHAN,
	ID_COMP_ZERO_NOT_EQUAL,
	ID_CR,
	ID_CREATE,
	ID_CREATE_P,
	ID_CSTORE,
	ID_DEFER,
	ID_DEFER_P,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	ID_OR,
	ID_OVER,
	ID_PICK,
	ID_PLUS,
	ID_PLUSLOOP_P,
	ID_PLUS_STORE,
	ID_QDO_P,
	ID_QDUP,
	ID_QTERMINAL,
	ID_QUIT_P,
	ID_REFILL,
	ID_RESIZE,
	ID_RESTORE_INPUT,
	ID_ROLL,
	ID_ROT,
	ID_RP_FETCH,
	ID_RP_STORE,
	ID_RSHIFT,
	ID_R_DROP,
	ID_R_FETCH,
	ID_R_FROM,
	ID_SAVE_FORTH_P,
	ID_SAVE_INPUT,
	ID_SCAN,
	ID_SEMICOLON,
	ID_SKIP,
	ID_SOURCE,
	ID_SOURCE_ID,
	ID_SOURCE_ID_POP,
	ID_SOURCE_ID_PUSH,
	ID_SOURCE_SET,
	ID_SP_FETCH,
	ID_SP_STORE,
	ID_STORE,
	ID_SWAP,
	ID_TEST1,
	ID_TEST2,
	ID_TEST3,
	ID_TICK,
	ID_TIMES,
	ID_TO_R,
	ID_TYPE,
	ID_TYPE_P,
	ID_VAR_BASE,
	ID_VAR_CODE_BASE,
	ID_VAR_CODE_LIMIT,
	ID_VAR_CONTEXT,
	ID_VAR_DP,
	ID_VAR_ECHO,
	ID_VAR_HEADERS_BASE,
	ID_VAR_HEADERS_LIMIT,
	ID_VAR_HEADERS_PTR,
	ID_VAR_NUM_TIB,
	ID_VAR_OUT,
	ID_VAR_RETURN_CODE,
	ID_VAR_SOURCE_ID,
	ID_VAR_STATE,
	ID_VAR_TO_IN,
	ID_VAR_TRACE_FLAGS,
	ID_VAR_TRACE_LEVEL,
	ID_VAR_TRACE_STACK,
	ID_VLIST,
	ID_WORD,
	ID_WORD_FETCH,
	ID_WORD_STORE,
	ID_XOR,
	ID_ZERO_BRANCH,
/* If you add a word here, take away one reserved word below. */
#ifdef PF_SUPPORT_FP
/* Only reserve space if we are adding FP so that we can detect
** unsupported primitives when loading dictionary.
*/
	ID_RESERVED01,
	ID_RESERVED02,
	ID_RESERVED03,
	ID_RESERVED04,
	ID_RESERVED05,
	ID_RESERVED06,
	ID_RESERVED07,
	ID_RESERVED08,
	ID_RESERVED09,
	ID_RESERVED10,
	ID_RESERVED11,
	ID_RESERVED12,
	ID_RESERVED13,
	ID_RESERVED14,
	ID_RESERVED15,
	ID_RESERVED16,
	ID_RESERVED17,
	ID_RESERVED18,
	ID_RESERVED19,
	ID_RESERVED20,
	ID_FP_D_TO_F,
	ID_FP_FSTORE,
	ID_FP_FTIMES,
	ID_FP_FPLUS,
	ID_FP_FMINUS,
	ID_FP_FSLASH,
	ID_FP_F_ZERO_LESS_THAN,
	ID_FP_F_ZERO_EQUALS,
	ID_FP_F_LESS_THAN,
	ID_FP_F_TO_D,
	ID_FP_FFETCH,
	ID_FP_FDEPTH,
	ID_FP_FDROP,
	ID_FP_FDUP,
	ID_FP_FLITERAL,
	ID_FP_FLITERAL_P,
	ID_FP_FLOAT_PLUS,
	ID_FP_FLOATS,
	ID_FP_FLOOR,
	ID_FP_FMAX,
	ID_FP_FMIN,
	ID_FP_FNEGATE,
	ID_FP_FOVER,
	ID_FP_FROT,
	ID_FP_FROUND,
	ID_FP_FSWAP,
	ID_FP_FSTAR_STAR,
	ID_FP_FABS,
	ID_FP_FACOS,
	ID_FP_FACOSH,
	ID_FP_FALOG,
	ID_FP_FASIN,
	ID_FP_FASINH,
	ID_FP_FATAN,
	ID_FP_FATAN2,
	ID_FP_FATANH,
	ID_FP_FCOS,
	ID_FP_FCOSH,
	ID_FP_FLN,
	ID_FP_FLNP1,
	ID_FP_FLOG,
	ID_FP_FSIN,
	ID_FP_FSINCOS,
	ID_FP_FSINH,
	ID_FP_FSQRT,
	ID_FP_FTAN,
	ID_FP_FTANH,
	ID_FP_FPICK,
#endif
/* Add new IDs by replacing reserved IDs or extending FP routines. */
/* Do NOT change the order of these IDs or dictionary files will break! */
	NUM_PRIMITIVES     /* This must always be LAST */
};


/***************************************************************
** Structures
***************************************************************/
#define CFTD_FLAG_GO	(0x0001)
/* This flag is true when ABORTing to cause the 'C' code to unravel. */
#define CFTD_FLAG_ABORT	(0x0002)

typedef struct cfTaskData
{
	cell   *td_StackPtr;       /* Primary data stack */
	cell   *td_StackBase;
	cell   *td_StackLimit;
	cell   *td_ReturnPtr;      /* Return stack */
	cell   *td_ReturnBase;
	cell   *td_ReturnLimit;
#ifdef PF_SUPPORT_FP
	PF_FLOAT  *td_FloatStackPtr;
	PF_FLOAT  *td_FloatStackBase;
	PF_FLOAT  *td_FloatStackLimit;
#endif
	cell   *td_InsPtr;          /* Instruction pointer, "PC" */
	cell    td_Flags;
	FileStream   *td_InputStream;
/* Terminal. */
	char    td_TIB[TIB_SIZE];   /* Buffer for terminal input. */
	cell    td_IN;              /* Index into Source */
	cell    td_SourceNum;       /* #TIB after REFILL */
	char   *td_SourcePtr;       /* Pointer to TIB or other source. */
	int32   td_LineNumber;      /* Incremented on every refill. */
	cell    td_OUT;             /* Current output column. */
} cfTaskData;

typedef struct pfNode
{
	struct pfNode *n_Next;
	struct pfNode *n_Prev;
} pfNode;

/* Structure of header entry in dictionary. These will be stored in dictionary specific endian format*/
typedef struct cfNameLinks
{
	cell       cfnl_PreviousName;   /* name relative address of previous */
	ExecToken  cfnl_ExecToken;      /* Execution token for word. */
/* Followed by variable length name field. */
} cfNameLinks;

#define PF_DICF_ALLOCATED_SEGMENTS  ( 0x0001)
typedef struct cfDictionary
{
	pfNode	dic_Node;
	uint32  dic_Flags;
/* Headers contain pointers to names and dictionary. */
	uint8	*dic_HeaderBaseUnaligned;
	uint8	*dic_HeaderBase;
	union
	{
		cell	*Cell;
		uint8	*Byte;
	} dic_HeaderPtr;
	uint8	*dic_HeaderLimit;
/* Code segment contains tokenized code and data. */
	uint8	*dic_CodeBaseUnaligned;
	uint8	*dic_CodeBase;
	union
	{
		cell	*Cell;
		uint8	*Byte;
	} dic_CodePtr;
	uint8	*dic_CodeLimit;
} cfDictionary;

/* Save state of include when nesting files. */
typedef struct IncludeFrame
{
	FileStream   *inf_FileID;
	int32         inf_LineNumber;
	int32         inf_SourceNum;
	int32         inf_IN;
	char          inf_SaveTIB[TIB_SIZE];
} IncludeFrame;

#define MAX_INCLUDE_DEPTH (8)

/***************************************************************
** Prototypes
***************************************************************/

#ifdef __cplusplus
extern "C" {
#endif

void pfExecuteToken( ExecToken XT );

#ifdef __cplusplus
}
#endif

/***************************************************************
** External Globals
***************************************************************/
extern cfTaskData   *gCurrentTask;
extern cfDictionary *gCurrentDictionary;
extern char          gScratch[TIB_SIZE];
extern int32         gNumPrimitives;

extern ExecToken     gLocalCompiler_XT;      /* CFA of (LOCAL) compiler. */

#define DEPTH_AT_COLON_INVALID (-100)
extern int32         gDepthAtColon;

/* Global variables. */
extern char *gVarContext;    /* Points to last name field. */
extern cell  gVarState;      /* 1 if compiling. */
extern cell  gVarBase;       /* Numeric Base. */
extern cell  gVarEcho;       /* Echo input from file. */
extern cell  gVarEchoAccept; /* Echo input from ACCEPT. */
extern cell  gVarTraceLevel;
extern cell  gVarTraceStack;
extern cell  gVarTraceFlags;
extern cell  gVarQuiet;	     /* Suppress unnecessary messages, OK, etc. */
extern cell  gVarReturnCode; /* Returned to caller of Forth, eg. UNIX shell. */

/***************************************************************
** Macros
***************************************************************/

/* Endian specific macros for creating target dictionaries for machines with
** different endian-ness.
*/
#if defined(PF_BIG_ENDIAN_DIC)
#define WRITE_FLOAT_DIC             WriteFloatBigEndian
#define WRITE_LONG_DIC(addr,data)   WriteLongBigEndian((uint32 *)(addr),(uint32)(data))
#define WRITE_SHORT_DIC(addr,data)  WriteShortBigEndian((uint16 *)(addr),(uint16)(data))
#define READ_FLOAT_DIC              ReadFloatBigEndian
#define READ_LONG_DIC(addr)         ReadLongBigEndian((uint32 *)(addr))
#define READ_SHORT_DIC(addr)        ReadShortBigEndian((uint16 *)(addr))
#elif defined(PF_LITTLE_ENDIAN_DIC)
#define WRITE_FLOAT_DIC             WriteFloatLittleEndian
#define WRITE_LONG_DIC(addr,data)   WriteLongLittleEndian((uint32 *)(addr),(uint32)(data))
#define WRITE_SHORT_DIC(addr,data)  WriteShortLittleEndian((uint16 *)(addr),(uint16)(data))
#define READ_FLOAT_DIC              ReadFloatLittleEndian
#define READ_LONG_DIC(addr)         ReadLongLittleEndian((uint32 *)(addr))
#define READ_SHORT_DIC(addr)        ReadShortLittleEndian((uint16 *)(addr))
#else
#define WRITE_FLOAT_DIC(addr,data)  { *((PF_FLOAT *)(addr)) = (PF_FLOAT)(data); }
#define WRITE_LONG_DIC(addr,data)   { *((int32 *)(addr)) = (int32)(data); }
#define WRITE_SHORT_DIC(addr,data)  { *((int16 *)(addr)) = (int16)(data); }
#define READ_FLOAT_DIC(addr)        ( *((PF_FLOAT *)(addr)) )
#define READ_LONG_DIC(addr)         ( *((int32 *)(addr)) )
#define READ_SHORT_DIC(addr)        ( *((int16 *)(addr)) )
#endif

#define HEADER_HERE (gCurrentDictionary->dic_HeaderPtr.Cell)
#define CODE_HERE (gCurrentDictionary->dic_CodePtr.Cell)
#define CODE_COMMA( N ) WRITE_LONG_DIC(CODE_HERE++,(N))
#define NAME_BASE (gCurrentDictionary->dic_HeaderBase)
#define CODE_BASE (gCurrentDictionary->dic_CodeBase)
#define NAME_SIZE (gCurrentDictionary->dic_HeaderLimit - gCurrentDictionary->dic_HeaderBase)
#define CODE_SIZE (gCurrentDictionary->dic_CodeLimit - gCurrentDictionary->dic_CodeBase)

#define IN_CODE_DIC(addr) ( ( ((uint8 *)(addr)) >= gCurrentDictionary->dic_CodeBase)   && ( ((uint8 *)(addr)) < gCurrentDictionary->dic_CodeLimit) )
#define IN_NAME_DIC(addr) ( ( ((uint8 *)(addr)) >= gCurrentDictionary->dic_HeaderBase) && ( ((uint8 *)(addr)) < gCurrentDictionary->dic_HeaderLimit) )
#define IN_DICS(addr) (IN_CODE_DIC(addr) || IN_NAME_DIC(addr))

/* Address conversion */
#define ABS_TO_NAMEREL( a ) ((int32)  (((uint8 *) a) - NAME_BASE ))
#define ABS_TO_CODEREL( a ) ((int32)  (((uint8 *) a) - CODE_BASE ))
#define NAMEREL_TO_ABS( a ) ((char *) (((int32) a) + NAME_BASE))
#define CODEREL_TO_ABS( a ) ((cell *) (((int32) a) + CODE_BASE))

/* The check for >0 is only needed for CLONE testing. !!! */
#define IsTokenPrimitive(xt) ((xt<gNumPrimitives) && (xt>=0))

#define SET_ABORT { gCurrentTask->td_Flags |= CFTD_FLAG_ABORT; }
#define CLEAR_ABORT { gCurrentTask->td_Flags &= ~CFTD_FLAG_ABORT; }
#define CHECK_ABORT (gCurrentTask->td_Flags & CFTD_FLAG_ABORT)

#define FREE_VAR(v) { if (v) { pfFreeMem(v); v = NULL; } }

#define DATA_STACK_DEPTH (gCurrentTask->td_StackBase - gCurrentTask->td_StackPtr)
#define DROP_DATA_STACK (gCurrentTask->td_StackPtr++)
#define POP_DATA_STACK (*gCurrentTask->td_StackPtr++)
#define PUSH_DATA_STACK(x) {*(--(gCurrentTask->td_StackPtr)) = (cell) x; }

/* Force Quad alignment. */
#define QUADUP(x) (((x)+3)&~3)

#define MIN(a,b)  ( ((a)<(b)) ? (a) : (b) )
#define MAX(a,b)  ( ((a)>(b)) ? (a) : (b) )


#ifndef TOUCH
	#define TOUCH(argument) ((void)argument)
#endif

/***************************************************************
** I/O related macros
***************************************************************/

#define EMIT(c)  ioEmit(c)
#define EMIT_CR  EMIT('\n');

#define DBUG(x) /* PRT(x) */
#define DBUGX(x) /* DBUG(x) */

#define MSG(cs)   pfMessage(cs)
#define ERR(x)    MSG(x)

#define MSG_NUM_D(msg,num) { MSG(msg); ffDot((int32) num); EMIT_CR; }
#define MSG_NUM_H(msg,num) { MSG(msg); ffDotHex((int32) num); EMIT_CR; }

#endif  /* _pf_guts_h */