1 /* @(#) pf_guts.h 98/01/28 1.4 */
5 /***************************************************************
6 ** Include file for PForth, a Forth based on 'C'
9 ** Copyright 1994 3DO, Phil Burk, Larry Polansky, David Rosenboom
11 ** Permission to use, copy, modify, and/or distribute this
12 ** software for any purpose with or without fee is hereby granted.
14 ** THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
15 ** WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
16 ** WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
17 ** THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
18 ** CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
19 ** FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
20 ** CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
21 ** OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 ***************************************************************/
26 ** PFORTH_VERSION changes when PForth is modified and released.
27 ** See README file for version info.
29 #define PFORTH_VERSION "28"
32 ** PFORTH_FILE_VERSION changes when incompatible changes are made
33 ** in the ".dic" file format.
35 ** FV3 - 950225 - Use ABS_TO_CODEREL for CodePtr. See file "pf_save.c".
36 ** FV4 - 950309 - Added NameSize and CodeSize to pfSaveForth().
37 ** FV5 - 950316 - Added Floats and reserved words.
38 ** FV6 - 961213 - Added ID_LOCAL_PLUSSTORE, ID_COLON_P, etc.
39 ** FV7 - 971203 - Added ID_FILL, (1LOCAL@), etc., ran out of reserved, resorted.
40 ** FV8 - 980818 - Added Endian flag.
41 ** FV9 - 20100503 - Added support for 64-bit CELL.
42 ** FV10 - 20170103 - Added ID_FILE_FLUSH ID_FILE_RENAME ID_FILE_RESIZE
44 #define PF_FILE_VERSION (10) /* Bump this whenever primitives added. */
45 #define PF_EARLIEST_FILE_VERSION (9) /* earliest one still compatible */
47 /***************************************************************
48 ** Sizes and other constants
49 ***************************************************************/
51 #define TIB_SIZE (256)
64 #define FLAG_PRECEDENCE (0x80)
65 #define FLAG_IMMEDIATE (0x40)
66 #define FLAG_SMUDGE (0x20)
67 #define MASK_NAME_SIZE (0x1F)
69 /* Debug TRACE flags */
70 #define TRACE_INNER (0x0002)
71 #define TRACE_COMPILE (0x0004)
72 #define TRACE_SPECIAL (0x0008)
74 /* Numeric types returned by NUMBER? */
75 #define NUM_TYPE_BAD (0)
76 #define NUM_TYPE_SINGLE (1)
77 #define NUM_TYPE_DOUBLE (2)
78 #define NUM_TYPE_FLOAT (3)
80 #define CREATE_BODY_OFFSET (3*sizeof(cell_t))
82 /***************************************************************
83 ** Primitive Token IDS
84 ** Do NOT change the order of these IDs or dictionary files will break!
85 ***************************************************************/
86 enum cforth_primitive_ids
88 ID_EXIT = 0, /* ID_EXIT must always be zero. */
89 /* Do NOT change the order of these IDs or dictionary files will break! */
123 ID_COMP_U_GREATERTHAN,
126 ID_COMP_ZERO_GREATERTHAN,
127 ID_COMP_ZERO_LESSTHAN,
128 ID_COMP_ZERO_NOT_EQUAL,
223 ID_SOURCE_LINE_NUMBER_FETCH, /* used to be ID_RESTORE_INPUT */
233 ID_SOURCE_LINE_NUMBER_STORE, /* used to be ID_SAVE_INPUT */
261 ID_VAR_HEADERS_LIMIT,
283 /* Added to support 64 bit operation. */
288 ID_FILE_FLUSH, /* FLUSH-FILE */
289 ID_FILE_RENAME, /* (RENAME-FILE) */
290 ID_FILE_RESIZE, /* RESIZE-FILE */
291 ID_SLEEP_P, /* (SLEEP) V2.0.0 */
292 /* If you add a word here, take away one reserved word below. */
294 /* Only reserve space if we are adding FP so that we can detect
295 ** unsupported primitives when loading dictionary.
312 ID_FP_F_ZERO_LESS_THAN,
355 /* Add new IDs by replacing reserved IDs or extending FP routines. */
356 /* Do NOT change the order of these IDs or dictionary files will break! */
357 NUM_PRIMITIVES /* This must always be LAST */
362 /***************************************************************
364 ***************************************************************/
365 /* ANSI standard definitions needed by pForth */
366 #define THROW_ABORT (-1)
367 #define THROW_ABORT_QUOTE (-2)
368 #define THROW_STACK_OVERFLOW (-3)
369 #define THROW_STACK_UNDERFLOW (-4)
370 #define THROW_UNDEFINED_WORD (-13)
371 #define THROW_EXECUTING (-14)
372 #define THROW_PAIRS (-22)
373 #define THROW_FLOAT_STACK_UNDERFLOW ( -45)
374 #define THROW_QUIT (-56)
375 #define THROW_FLUSH_FILE (-68)
376 #define THROW_RESIZE_FILE (-74)
378 /* THROW codes unique to pForth */
379 #define THROW_BYE (-256) /* Exit program. */
380 #define THROW_SEMICOLON (-257) /* Error detected at ; */
381 #define THROW_DEFERRED (-258) /* Not a deferred word. Used in system.fth */
383 /***************************************************************
385 ***************************************************************/
387 typedef struct pfTaskData_s
389 cell_t *td_StackPtr; /* Primary data stack */
390 cell_t *td_StackBase;
391 cell_t *td_StackLimit;
392 cell_t *td_ReturnPtr; /* Return stack */
393 cell_t *td_ReturnBase;
394 cell_t *td_ReturnLimit;
396 PF_FLOAT *td_FloatStackPtr;
397 PF_FLOAT *td_FloatStackBase;
398 PF_FLOAT *td_FloatStackLimit;
400 cell_t *td_InsPtr; /* Instruction pointer, "PC" */
401 FileStream *td_InputStream;
403 char td_TIB[TIB_SIZE]; /* Buffer for terminal input. */
404 cell_t td_IN; /* Index into Source */
405 cell_t td_SourceNum; /* #TIB after REFILL */
406 char *td_SourcePtr; /* Pointer to TIB or other source. */
407 cell_t td_LineNumber; /* Incremented on every refill. */
408 cell_t td_OUT; /* Current output column. */
411 typedef struct pfNode
413 struct pfNode *n_Next;
414 struct pfNode *n_Prev;
417 /* Structure of header entry in dictionary. These will be stored in dictionary specific endian format*/
418 typedef struct cfNameLinks
420 cell_t cfnl_PreviousName; /* name relative address of previous */
421 ExecToken cfnl_ExecToken; /* Execution token for word. */
422 /* Followed by variable length name field. */
425 #define PF_DICF_ALLOCATED_SEGMENTS ( 0x0001)
426 typedef struct pfDictionary_s
430 /* Headers contain pointers to names and dictionary. */
432 ucell_t dic_HeaderBaseUnaligned;
434 ucell_t dic_HeaderBase;
435 ucell_t dic_HeaderPtr;
436 ucell_t dic_HeaderLimit;
437 /* Code segment contains tokenized code and data. */
438 ucell_t dic_CodeBaseUnaligned;
439 ucell_t dic_CodeBase;
445 ucell_t dic_CodeLimit;
448 /* Save state of include when nesting files. */
449 typedef struct IncludeFrame
451 FileStream *inf_FileID;
452 cell_t inf_LineNumber;
453 cell_t inf_SourceNum;
455 char inf_SaveTIB[TIB_SIZE];
458 #define MAX_INCLUDE_DEPTH (16)
460 /***************************************************************
462 ***************************************************************/
468 ThrowCode pfCatch( ExecToken XT );
474 /***************************************************************
476 ***************************************************************/
477 extern pfTaskData_t *gCurrentTask;
478 extern pfDictionary_t *gCurrentDictionary;
479 extern char gScratch[TIB_SIZE];
480 extern cell_t gNumPrimitives;
482 extern ExecToken gLocalCompiler_XT; /* CFA of (LOCAL) compiler. */
483 extern ExecToken gNumberQ_XT; /* XT of NUMBER? */
484 extern ExecToken gQuitP_XT; /* XT of (QUIT) */
485 extern ExecToken gAcceptP_XT; /* XT of ACCEPT */
487 #define DEPTH_AT_COLON_INVALID (-100)
488 extern cell_t gDepthAtColon;
490 /* Global variables. */
491 extern cell_t gVarContext; /* Points to last name field. */
492 extern cell_t gVarState; /* 1 if compiling. */
493 extern cell_t gVarBase; /* Numeric Base. */
494 extern cell_t gVarEcho; /* Echo input from file. */
495 extern cell_t gVarEchoAccept; /* Echo input from ACCEPT. */
496 extern cell_t gVarTraceLevel;
497 extern cell_t gVarTraceStack;
498 extern cell_t gVarTraceFlags;
499 extern cell_t gVarQuiet; /* Suppress unnecessary messages, OK, etc. */
500 extern cell_t gVarReturnCode; /* Returned to caller of Forth, eg. UNIX shell. */
502 extern IncludeFrame gIncludeStack[MAX_INCLUDE_DEPTH];
503 extern cell_t gIncludeIndex;
504 /***************************************************************
506 ***************************************************************/
509 /* Endian specific macros for creating target dictionaries for machines with
511 ** different endian-ness.
515 #if defined(PF_BIG_ENDIAN_DIC)
517 #define WRITE_FLOAT_DIC WriteFloatBigEndian
518 #define WRITE_CELL_DIC(addr,data) WriteCellBigEndian((uint8_t *)(addr),(ucell_t)(data))
519 #define WRITE_SHORT_DIC(addr,data) Write16BigEndian((uint8_t *)(addr),(uint16_t)(data))
520 #define READ_FLOAT_DIC ReadFloatBigEndian
521 #define READ_CELL_DIC(addr) ReadCellBigEndian((const uint8_t *)(addr))
522 #define READ_SHORT_DIC(addr) Read16BigEndian((const uint8_t *)(addr))
524 #elif defined(PF_LITTLE_ENDIAN_DIC)
526 #define WRITE_FLOAT_DIC WriteFloatLittleEndian
527 #define WRITE_CELL_DIC(addr,data) WriteCellLittleEndian((uint8_t *)(addr),(ucell_t)(data))
528 #define WRITE_SHORT_DIC(addr,data) Write16LittleEndian((uint8_t *)(addr),(uint16_t)(data))
529 #define READ_FLOAT_DIC ReadFloatLittleEndian
530 #define READ_CELL_DIC(addr) ReadCellLittleEndian((const uint8_t *)(addr))
531 #define READ_SHORT_DIC(addr) Read16LittleEndian((const uint8_t *)(addr))
535 #define WRITE_FLOAT_DIC(addr,data) { *((PF_FLOAT *)(addr)) = (PF_FLOAT)(data); }
536 #define WRITE_CELL_DIC(addr,data) { *((cell_t *)(addr)) = (cell_t)(data); }
537 #define WRITE_SHORT_DIC(addr,data) { *((int16_t *)(addr)) = (int16_t)(data); }
538 #define READ_FLOAT_DIC(addr) ( *((PF_FLOAT *)(addr)) )
539 #define READ_CELL_DIC(addr) ( *((const ucell_t *)(addr)) )
540 #define READ_SHORT_DIC(addr) ( *((const uint16_t *)(addr)) )
545 #define HEADER_HERE (gCurrentDictionary->dic_HeaderPtr.Cell)
546 #define CODE_HERE (gCurrentDictionary->dic_CodePtr.Cell)
547 #define CODE_COMMA( N ) WRITE_CELL_DIC(CODE_HERE++,(N))
548 #define NAME_BASE (gCurrentDictionary->dic_HeaderBase)
549 #define CODE_BASE (gCurrentDictionary->dic_CodeBase)
550 #define NAME_SIZE (gCurrentDictionary->dic_HeaderLimit - gCurrentDictionary->dic_HeaderBase)
551 #define CODE_SIZE (gCurrentDictionary->dic_CodeLimit - gCurrentDictionary->dic_CodeBase)
553 #define IN_CODE_DIC(addr) ( ( ((uint8_t *)(addr)) >= gCurrentDictionary->dic_CodeBase) && ( ((uint8_t *)(addr)) < gCurrentDictionary->dic_CodeLimit) )
555 #define IN_NAME_DIC(addr) ( ( ((uint8_t *)(addr)) >= gCurrentDictionary->dic_HeaderBase) && ( ((uint8_t *)(addr)) < gCurrentDictionary->dic_HeaderLimit) )
556 #define IN_DICS(addr) (IN_CODE_DIC(addr) || IN_NAME_DIC(addr))
558 /* Address conversion */
559 #define ABS_TO_NAMEREL( a ) ((cell_t) (((ucell_t) a) - NAME_BASE ))
560 #define ABS_TO_CODEREL( a ) ((cell_t) (((ucell_t) a) - CODE_BASE ))
561 #define NAMEREL_TO_ABS( a ) ((ucell_t) (((cell_t) a) + NAME_BASE))
562 #define CODEREL_TO_ABS( a ) ((ucell_t) (((cell_t) a) + CODE_BASE))
564 /* The check for >0 is only needed for CLONE testing. !!! */
565 #define IsTokenPrimitive(xt) ((xt<gNumPrimitives) && (xt>=0))
567 #define FREE_VAR(v) { if (v) { pfFreeMem((void *)(v)); v = 0; } }
569 #define DATA_STACK_DEPTH (gCurrentTask->td_StackBase - gCurrentTask->td_StackPtr)
570 #define DROP_DATA_STACK (gCurrentTask->td_StackPtr++)
571 #define POP_DATA_STACK (*gCurrentTask->td_StackPtr++)
572 #define PUSH_DATA_STACK(x) {*(--(gCurrentTask->td_StackPtr)) = (cell_t) x; }
574 /* Force Quad alignment. */
575 #define QUADUP(x) (((x)+3)&~3)
578 #define MIN(a,b) ( ((a)<(b)) ? (a) : (b) )
581 #define MAX(a,b) ( ((a)>(b)) ? (a) : (b) )
585 #define TOUCH(argument) ((void)argument)
588 /***************************************************************
589 ** I/O related macros
590 ***************************************************************/
592 #define EMIT(c) ioEmit(c)
593 #define EMIT_CR EMIT('\n');
595 #define MSG(cs) pfMessage(cs)
596 #define ERR(x) MSG(x)
598 #define DBUG(x) /* PRT(x) */
599 #define DBUGX(x) /* DBUG(x) */
601 #define MSG_NUM_D(msg,num) { MSG(msg); ffDot((cell_t) num); EMIT_CR; }
602 #define MSG_NUM_H(msg,num) { MSG(msg); ffDotHex((cell_t) num); EMIT_CR; }
604 #define DBUG_NUM_D(msg,num) { pfDebugMessage(msg); pfDebugPrintDecimalNumber((cell_t) num); pfDebugMessage("\n"); }
606 #endif /* _pf_guts_h */