/
Sapphire/bin/
Sapphire/db/
Sapphire/db/OLC_rooms/
Sapphire/db/abi/
Sapphire/db/em_src/
Sapphire/db/helps/
Sapphire/db/helps/emman/ifunc/
Sapphire/db/npcs/Tatt/
Sapphire/db/objects/Tatt/
Sapphire/db/q_data/
Sapphire/db/rooms/Tatt/
Sapphire/doc/
Sapphire/doc/em/
Sapphire/etc/
Sapphire/src/abic/
Sapphire/src/areacon/
Sapphire/src/client/
Sapphire/src/embc/
Sapphire/src/emi/
Sapphire/src/emi/test/
Sapphire/src/include/
Sapphire/src/sapphire/em/
Sapphire/src/tcon/
/*
 * Copyright (C) 1995-1997 Christopher D. Granz
 *
 * This header may not be removed.
 *
 * Refer to the file "License" included in this package for further
 * information and before using any of the following.
 */

#ifndef __EMERALD_H__
#define __EMERALD_H__

#include "sapphire.h"


/*
 * Defines
 */
#define EM_MAGIC_NUMBER                                         0x7B041D
#define EM_LOADER_VERSION                                              8

/*
 * Instruction byte codes.
 */
#define INSTR_END                                                      0
#define INSTR_NEW_INT                                                  1
#define INSTR_NEW_FLOAT                                                2
#define INSTR_NEW_STRING                                               3
#define INSTR_NEW_OBJECT                                               4
#define INSTR_NEW_ARRAY                                                5
#define INSTR_PUSH_ZERO                                                6
#define INSTR_PUSH_ONE                                                 7
#define INSTR_PUSH_INT1                                                8
#define INSTR_PUSH_INT2                                                9
#define INSTR_PUSH_INT4                                               10
#define INSTR_PUSH_FLOAT4                                             11
#define INSTR_PUSH_FLOAT8                                             12
#define INSTR_PUSH_STRING                                             13
#define INSTR_PUSH_ARRAY                                              14
#define INSTR_PUSH_GLOBAL                                             15
#define INSTR_PUSH_LOCAL                                              16
#define INSTR_PUSH_INDEX                                              17
#define INSTR_POP                                                     18
#define INSTR_CLEAR_GLOBAL                            /* Not used. */ 19
#define INSTR_CLEAR_LOCAL                             /* Not used. */ 20
#define INSTR_ASSIGN_GLOBAL                                           21
#define INSTR_ASSIGN_LOCAL                                            22
#define INSTR_ASSIGN_INDEX                                            23
#define INSTR_MULTIPLY                                                24
#define INSTR_DIVIDE                                                  25
#define INSTR_MODULUS                                                 26
#define INSTR_ADD                                                     27
#define INSTR_SUBTRACT                                                28
#define INSTR_LSHIFT                                                  29
#define INSTR_RSHIFT                                                  30
#define INSTR_AND                                                     31
#define INSTR_XOR                                                     32
#define INSTR_OR                                                      33
#define INSTR_ONES_COMPLEMENT                                         34
#define INSTR_JUMP                                                    35
#define INSTR_CAST                                                    36
#define INSTR_COMPARE                                                 37
#define INSTR_CALL_BUILTIN_FUNC                                       38
#define INSTR_CALL_FUNC                                               39
#define INSTR_RETURN                                  /* Not used. */ 40

/*
 * Byte codes translated by the linker.
 */
#define TRANS_GLOBAL_VAR                                              75
#define TRANS_BUILTIN_FUNC                                            76
#define TRANS_FUNC                                                    77

/*
 * Macros for adding instructions to a byte instruction stream.
 */
#define ADD_INSTR_I1( p, i )                            ( *(p)++ = (i) )

#define ADD_INSTR_I2( p, i )                                           \
          {                                                            \
              *( (i2 *) (p) )             = (i2) (i);                  \
              (p)                        += sizeof( i2 );              \
          }

#define ADD_INSTR_I4( p, i )                                           \
          {                                                            \
              *( (i4 *) (p) )             = (i4) (i);                  \
              (p)                        += sizeof( i4 );              \
          }

#define ADD_INSTR_F4( p, i )                                           \
          {                                                            \
              *( (f4 *) (p) )             = (f4) (i);                  \
              (p)                        += sizeof( f4 );              \
          }

#define ADD_INSTR_F8( p, i )                                           \
          {                                                            \
              *( (f8 *) (p) )             = (f8) (i);                  \
              (p)                        += sizeof( f8 );              \
          }

#define COND_EQUAL                                                     1
#define COND_NOT_EQUAL                                                 2
#define COND_GREATER                                                   3
#define COND_LESS                                                      4
#define COND_GREATER_OR_EQUAL                                          5
#define COND_LESS_OR_EQUAL                                             6

#define TYPE_INVALID                                                   1
#define TYPE_VOID                                                      2
#define TYPE_INT                                                       3
#define TYPE_FLOAT                                                     4
#define TYPE_STRING                                                    5
#define TYPE_OBJECT                                                    6
#define TYPE_ARRAY                                                     7

#define OBJ_TYPE_FILE                                                  1
#define OBJ_TYPE_TERM                                                  2
#define OBJ_TYPE_CHAR                                                  3

#define FFLAG_PRIVATE                                               0x01

#define MAX_ARRAY_SIZE                                              8192

#define em_setup_abort( )                          setjmp( jbAbortJump )
#define em_abort( )                            longjmp( jbAbortJump, 1 )


/*
 * Type defines
 */
typedef short int                                                    i2;
typedef long int                                                     i4;
typedef float                                                        f4;
typedef double                                                       f8;

typedef struct em_func                                          EM_FUNC;
typedef struct em_value                                        EM_VALUE;
typedef struct em_string                                      EM_STRING;
typedef struct em_array                                        EM_ARRAY;
typedef struct em_object                                      EM_OBJECT;


/*
 * Structures
 */
struct em_func
{
    short int                /* Used at boot time only. */ siFileNumber;
    char *                                                        pName;
    intt                                                       iNumArgs;
    intt *                                                    pArgTypes;
    int *                                                  pArgArrayDim;
    intt *                                               pArgArrayTypes;
    intt                                                     iNumReturn;
    intt *                                                 pReturnTypes;
    int *                                               pReturnArrayDim;
    intt *                                            pReturnArrayTypes;
    flags                                                    fFuncFlags;
    unsigned long                                            ulCodeSize;
    byte *                                                 pInstrStream;
    STRUCT_BOOL                                            ( bDynamic );
};

struct em_value
{
    intt                                                          iType;

    union
    {
        long                                                       lInt;
        double                                                   dFloat;
        EM_STRING *                                             pString;
        EM_OBJECT *                                             pObject;
        EM_VALUE *                                               pMixed;
        EM_ARRAY *                                               pArray;
    } u;
};

struct em_string
{
    short                                                       sLength;
    char *                                                      pString;
};

struct em_object
{
    int                                                     iObjectType;
    void *                                                  pRealObject;
};

struct em_array
{
    short                                                        sCount;
    short                                                         sSize;
    EM_VALUE *                                                pElements;
};

struct em_extern_func_type
{
    void ( *pFunc )                                            ( void );
    char *                                                        pName;
    intt                                                       iNumArgs;
    intt                                                  iArgTypes[16];
    intt                                                     iNumReturn;
    intt                                                iReturnTypes[8];
};

struct _open_file
{
    struct _open_file *                                           pNext;
    intt                                                          iMode;
    FILE *                                                        pFile;
};


#ifdef SERVER

/*
 * Prototypes
 */

/*
 * emerald.c
 */
int              func_new                                      ( void );
int              em_load_obj_file                      ( char *, bool );
int              em_unload_func                                 ( int );
void             em_translate                                  ( void );
byte *           bct                                         ( byte * );
int              em_find_func                                ( char * );
int              em_call_func                                   ( int );

/*
 * interp.c
 */
void             stack_init                                    ( void );
void             stack_free                                    ( void );
void             var_stack_push                 ( register EM_VALUE * );
void             var_stack_pop                   ( register short int );
void             var_stack_reset                               ( void );
bool             var_stack_overflow                            ( void );
void             interp_stack_push              ( register EM_VALUE * );
void             interp_stack_pop                ( register short int );
bool             interp_stack_overflow                         ( void );
void             init_value                     ( register EM_VALUE * );
void             delete_value                   ( register EM_VALUE * );
byte *           interpret_instruction                       ( byte * );

/*
 * array.c
 */
EM_ARRAY *       array_alloc                        ( register size_t );
void             array_free                     ( register EM_ARRAY * );
EM_ARRAY *       array_copy                     ( register EM_ARRAY * );

/*
 * error.c
 */
void             print_error                                   ( void );
void             em_set_timer                                   ( int );
void             em_clear_timer                                ( void );
void             error_check                                   ( void );
byte *           bcec                                        ( byte * );

/*
 * builtin.c
 */
void             em_get_values                            ( long, ... );
void             _lalloc                                       ( void );
void             _lallocm                                      ( void );
void             _lnullobj                                     ( void );
void             _lobjtype                                     ( void );
void             _lvstacksize                                  ( void );
void             _listacksize                                  ( void );
void             _lnumbfuncargs                                ( void );
void             _lnumfuncargs                                 ( void );
void             _lopentfile                                   ( void );
void             _lclosefile                                   ( void );
void             _lcoredump                                    ( void );

/*
 * ifunc.c
 */
void             _ione_arg                                     ( void );
void             _istr_to_int                                  ( void );
void             _imud_name                                    ( void );
void             _iget_plr_obj                                 ( void );
void             _iget_term_obj                                ( void );
void             _inext_char_obj                               ( void );
void             _inext_term_obj                               ( void );
void             _iwrite_to_char                               ( void );
void             _iwrite_to_term                               ( void );
void             _isetup_string_pager                          ( void );
void             _ifinish_string_pager                         ( void );
void             _ipage_string                                 ( void );


/*
 * Globals
 */
extern char                                                 cErrorBuf[];
extern int                                                     iEmError;
extern jmp_buf                                              jbAbortJump;

extern const struct em_extern_func_type                emefCFuncTable[];

extern EM_FUNC **                                               ppFuncs;
extern short int                                         siTopFuncIndex;

#endif /* SERVER */
#endif /* __EMERALD_H__ */


/*
 * End of emerald.h
 */