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 ** The pForth software code is dedicated to the public domain,
12 ** and any third party may reproduce, distribute and modify
13 ** the pForth software code or any derivative works thereof
14 ** without any compensation or license. The pForth software
15 ** code is provided on an "as is" basis without any warranty
16 ** of any kind, including, without limitation, the implied
17 ** warranties of merchantability and fitness for a particular
18 ** purpose and their equivalents under the laws of any jurisdiction.
20 ***************************************************************/
23 ** PFORTH_VERSION changes when PForth is modified and released.
24 ** See README file for version info.
26 #define PFORTH_VERSION "27"
29 ** PFORTH_FILE_VERSION changes when incompatible changes are made
30 ** in the ".dic" file format.
32 ** FV3 - 950225 - Use ABS_TO_CODEREL for CodePtr. See file "pf_save.c".
33 ** FV4 - 950309 - Added NameSize and CodeSize to pfSaveForth().
34 ** FV5 - 950316 - Added Floats and reserved words.
35 ** FV6 - 961213 - Added ID_LOCAL_PLUSSTORE, ID_COLON_P, etc.
36 ** FV7 - 971203 - Added ID_FILL, (1LOCAL@), etc., ran out of reserved, resorted.
37 ** FV8 - 980818 - Added Endian flag.
38 ** FV9 - 20100503 - Added support for 64-bit CELL.
40 #define PF_FILE_VERSION (9) /* Bump this whenever primitives added. */
41 #define PF_EARLIEST_FILE_VERSION (9) /* earliest one still compatible */
43 /***************************************************************
44 ** Sizes and other constants
45 ***************************************************************/
47 #define TIB_SIZE (256)
60 #define FLAG_PRECEDENCE (0x80)
61 #define FLAG_IMMEDIATE (0x40)
62 #define FLAG_SMUDGE (0x20)
63 #define MASK_NAME_SIZE (0x1F)
65 /* Debug TRACE flags */
66 #define TRACE_INNER (0x0002)
67 #define TRACE_COMPILE (0x0004)
68 #define TRACE_SPECIAL (0x0008)
70 /* Numeric types returned by NUMBER? */
71 #define NUM_TYPE_BAD (0)
72 #define NUM_TYPE_SINGLE (1)
73 #define NUM_TYPE_DOUBLE (2)
74 #define NUM_TYPE_FLOAT (3)
76 #define CREATE_BODY_OFFSET (3*sizeof(cell_t))
78 /***************************************************************
79 ** Primitive Token IDS
80 ** Do NOT change the order of these IDs or dictionary files will break!
81 ***************************************************************/
82 enum cforth_primitive_ids
84 ID_EXIT = 0, /* ID_EXIT must always be zero. */
85 /* Do NOT change the order of these IDs or dictionary files will break! */
119 ID_COMP_U_GREATERTHAN,
122 ID_COMP_ZERO_GREATERTHAN,
123 ID_COMP_ZERO_LESSTHAN,
124 ID_COMP_ZERO_NOT_EQUAL,
219 ID_SOURCE_LINE_NUMBER_FETCH, /* used to be ID_RESTORE_INPUT */
229 ID_SOURCE_LINE_NUMBER_STORE, /* used to be ID_SAVE_INPUT */
257 ID_VAR_HEADERS_LIMIT,
279 /* Added to support 64 bit operation. */
284 /* If you add a word here, take away one reserved word below. */
286 /* Only reserve space if we are adding FP so that we can detect
287 ** unsupported primitives when loading dictionary.
308 ID_FP_F_ZERO_LESS_THAN,
351 /* Add new IDs by replacing reserved IDs or extending FP routines. */
352 /* Do NOT change the order of these IDs or dictionary files will break! */
353 NUM_PRIMITIVES /* This must always be LAST */
358 /***************************************************************
360 ***************************************************************/
361 /* ANSI standard definitions needed by pForth */
362 #define THROW_ABORT (-1)
363 #define THROW_ABORT_QUOTE (-2)
364 #define THROW_STACK_OVERFLOW (-3)
365 #define THROW_STACK_UNDERFLOW (-4)
366 #define THROW_UNDEFINED_WORD (-13)
367 #define THROW_EXECUTING (-14)
368 #define THROW_PAIRS (-22)
369 #define THROW_FLOAT_STACK_UNDERFLOW ( -45)
370 #define THROW_QUIT (-56)
372 /* THROW codes unique to pForth */
373 #define THROW_BYE (-256) /* Exit program. */
374 #define THROW_SEMICOLON (-257) /* Error detected at ; */
375 #define THROW_DEFERRED (-258) /* Not a deferred word. Used in system.fth */
377 /***************************************************************
379 ***************************************************************/
381 typedef struct pfTaskData_s
383 cell_t *td_StackPtr; /* Primary data stack */
384 cell_t *td_StackBase;
385 cell_t *td_StackLimit;
386 cell_t *td_ReturnPtr; /* Return stack */
387 cell_t *td_ReturnBase;
388 cell_t *td_ReturnLimit;
390 PF_FLOAT *td_FloatStackPtr;
391 PF_FLOAT *td_FloatStackBase;
392 PF_FLOAT *td_FloatStackLimit;
394 cell_t *td_InsPtr; /* Instruction pointer, "PC" */
395 FileStream *td_InputStream;
397 char td_TIB[TIB_SIZE]; /* Buffer for terminal input. */
398 cell_t td_IN; /* Index into Source */
399 cell_t td_SourceNum; /* #TIB after REFILL */
400 char *td_SourcePtr; /* Pointer to TIB or other source. */
401 cell_t td_LineNumber; /* Incremented on every refill. */
402 cell_t td_OUT; /* Current output column. */
405 typedef struct pfNode
407 struct pfNode *n_Next;
408 struct pfNode *n_Prev;
411 /* Structure of header entry in dictionary. These will be stored in dictionary specific endian format*/
412 typedef struct cfNameLinks
414 cell_t cfnl_PreviousName; /* name relative address of previous */
415 ExecToken cfnl_ExecToken; /* Execution token for word. */
416 /* Followed by variable length name field. */
419 #define PF_DICF_ALLOCATED_SEGMENTS ( 0x0001)
420 typedef struct pfDictionary_s
424 /* Headers contain pointers to names and dictionary. */
426 ucell_t dic_HeaderBaseUnaligned;
428 ucell_t dic_HeaderBase;
429 ucell_t dic_HeaderPtr;
430 ucell_t dic_HeaderLimit;
431 /* Code segment contains tokenized code and data. */
432 ucell_t dic_CodeBaseUnaligned;
433 ucell_t dic_CodeBase;
439 ucell_t dic_CodeLimit;
442 /* Save state of include when nesting files. */
443 typedef struct IncludeFrame
445 FileStream *inf_FileID;
446 cell_t inf_LineNumber;
447 cell_t inf_SourceNum;
449 char inf_SaveTIB[TIB_SIZE];
452 #define MAX_INCLUDE_DEPTH (16)
454 /***************************************************************
456 ***************************************************************/
462 int pfCatch( ExecToken XT );
468 /***************************************************************
470 ***************************************************************/
471 extern pfTaskData_t *gCurrentTask;
472 extern pfDictionary_t *gCurrentDictionary;
473 extern char gScratch[TIB_SIZE];
474 extern cell_t gNumPrimitives;
476 extern ExecToken gLocalCompiler_XT; /* CFA of (LOCAL) compiler. */
477 extern ExecToken gNumberQ_XT; /* XT of NUMBER? */
478 extern ExecToken gQuitP_XT; /* XT of (QUIT) */
479 extern ExecToken gAcceptP_XT; /* XT of ACCEPT */
481 #define DEPTH_AT_COLON_INVALID (-100)
482 extern cell_t gDepthAtColon;
484 /* Global variables. */
485 extern cell_t gVarContext; /* Points to last name field. */
486 extern cell_t gVarState; /* 1 if compiling. */
487 extern cell_t gVarBase; /* Numeric Base. */
488 extern cell_t gVarEcho; /* Echo input from file. */
489 extern cell_t gVarEchoAccept; /* Echo input from ACCEPT. */
490 extern cell_t gVarTraceLevel;
491 extern cell_t gVarTraceStack;
492 extern cell_t gVarTraceFlags;
493 extern cell_t gVarQuiet; /* Suppress unnecessary messages, OK, etc. */
494 extern cell_t gVarReturnCode; /* Returned to caller of Forth, eg. UNIX shell. */
496 extern IncludeFrame gIncludeStack[MAX_INCLUDE_DEPTH];
497 extern cell_t gIncludeIndex;
498 /***************************************************************
500 ***************************************************************/
503 /* Endian specific macros for creating target dictionaries for machines with
505 ** different endian-ness.
509 #if defined(PF_BIG_ENDIAN_DIC)
511 #define WRITE_FLOAT_DIC WriteFloatBigEndian
512 #define WRITE_CELL_DIC(addr,data) WriteCellBigEndian((uint8_t *)(addr),(ucell_t)(data))
513 #define WRITE_SHORT_DIC(addr,data) Write16BigEndian((uint8_t *)(addr),(uint16_t)(data))
514 #define READ_FLOAT_DIC ReadFloatBigEndian
515 #define READ_CELL_DIC(addr) ReadCellBigEndian((const uint8_t *)(addr))
516 #define READ_SHORT_DIC(addr) Read16BigEndian((const uint8_t *)(addr))
518 #elif defined(PF_LITTLE_ENDIAN_DIC)
520 #define WRITE_FLOAT_DIC WriteFloatLittleEndian
521 #define WRITE_CELL_DIC(addr,data) WriteCellLittleEndian((uint8_t *)(addr),(ucell_t)(data))
522 #define WRITE_SHORT_DIC(addr,data) Write16LittleEndian((uint8_t *)(addr),(uint16_t)(data))
523 #define READ_FLOAT_DIC ReadFloatLittleEndian
524 #define READ_CELL_DIC(addr) ReadCellLittleEndian((const uint8_t *)(addr))
525 #define READ_SHORT_DIC(addr) Read16LittleEndian((const uint8_t *)(addr))
529 #define WRITE_FLOAT_DIC(addr,data) { *((PF_FLOAT *)(addr)) = (PF_FLOAT)(data); }
530 #define WRITE_CELL_DIC(addr,data) { *((cell_t *)(addr)) = (cell_t)(data); }
531 #define WRITE_SHORT_DIC(addr,data) { *((int16_t *)(addr)) = (int16_t)(data); }
532 #define READ_FLOAT_DIC(addr) ( *((PF_FLOAT *)(addr)) )
533 #define READ_CELL_DIC(addr) ( *((const ucell_t *)(addr)) )
534 #define READ_SHORT_DIC(addr) ( *((const uint16_t *)(addr)) )
539 #define HEADER_HERE (gCurrentDictionary->dic_HeaderPtr.Cell)
540 #define CODE_HERE (gCurrentDictionary->dic_CodePtr.Cell)
541 #define CODE_COMMA( N ) WRITE_CELL_DIC(CODE_HERE++,(N))
542 #define NAME_BASE (gCurrentDictionary->dic_HeaderBase)
543 #define CODE_BASE (gCurrentDictionary->dic_CodeBase)
544 #define NAME_SIZE (gCurrentDictionary->dic_HeaderLimit - gCurrentDictionary->dic_HeaderBase)
545 #define CODE_SIZE (gCurrentDictionary->dic_CodeLimit - gCurrentDictionary->dic_CodeBase)
547 #define IN_CODE_DIC(addr) ( ( ((uint8_t *)(addr)) >= gCurrentDictionary->dic_CodeBase) && ( ((uint8_t *)(addr)) < gCurrentDictionary->dic_CodeLimit) )
549 #define IN_NAME_DIC(addr) ( ( ((uint8_t *)(addr)) >= gCurrentDictionary->dic_HeaderBase) && ( ((uint8_t *)(addr)) < gCurrentDictionary->dic_HeaderLimit) )
550 #define IN_DICS(addr) (IN_CODE_DIC(addr) || IN_NAME_DIC(addr))
552 /* Address conversion */
553 #define ABS_TO_NAMEREL( a ) ((cell_t) (((ucell_t) a) - NAME_BASE ))
554 #define ABS_TO_CODEREL( a ) ((cell_t) (((ucell_t) a) - CODE_BASE ))
555 #define NAMEREL_TO_ABS( a ) ((ucell_t) (((cell_t) a) + NAME_BASE))
556 #define CODEREL_TO_ABS( a ) ((ucell_t) (((cell_t) a) + CODE_BASE))
558 /* The check for >0 is only needed for CLONE testing. !!! */
559 #define IsTokenPrimitive(xt) ((xt<gNumPrimitives) && (xt>=0))
561 #define FREE_VAR(v) { if (v) { pfFreeMem((void *)(v)); v = 0; } }
563 #define DATA_STACK_DEPTH (gCurrentTask->td_StackBase - gCurrentTask->td_StackPtr)
564 #define DROP_DATA_STACK (gCurrentTask->td_StackPtr++)
565 #define POP_DATA_STACK (*gCurrentTask->td_StackPtr++)
566 #define PUSH_DATA_STACK(x) {*(--(gCurrentTask->td_StackPtr)) = (cell_t) x; }
568 /* Force Quad alignment. */
569 #define QUADUP(x) (((x)+3)&~3)
571 #define MIN(a,b) ( ((a)<(b)) ? (a) : (b) )
572 #define MAX(a,b) ( ((a)>(b)) ? (a) : (b) )
576 #define TOUCH(argument) ((void)argument)
579 /***************************************************************
580 ** I/O related macros
581 ***************************************************************/
583 #define EMIT(c) ioEmit(c)
584 #define EMIT_CR EMIT('\n');
586 #define MSG(cs) pfMessage(cs)
587 #define ERR(x) MSG(x)
589 #define DBUG(x) /* PRT(x) */
590 #define DBUGX(x) /* DBUG(x) */
592 #define MSG_NUM_D(msg,num) { MSG(msg); ffDot((cell_t) num); EMIT_CR; }
593 #define MSG_NUM_H(msg,num) { MSG(msg); ffDotHex((cell_t) num); EMIT_CR; }
595 #define DBUG_NUM_D(msg,num) { pfDebugMessage(msg); pfDebugPrintDecimalNumber((cell_t) num); pfDebugMessage("\n"); }
597 #endif /* _pf_guts_h */