bytecode.h 11.2 KB
Newer Older
kollo's avatar
kollo committed
1 2 3 4 5 6 7 8
/* BYTECODE.H (c) Markus Hoffmann  */

/* This file is part of X11BASIC, the basic interpreter for Unix/X
 * ============================================================
 * X11BASIC is free software and comes with NO WARRANTY - read the file
 * COPYING for details
 */

kollo's avatar
kollo committed
9
#define BC_STACKLEN 256
kollo's avatar
kollo committed
10

kollo's avatar
kollo committed
11
#define BC_VERSION 0x1252   /* Version 1.25 release 2*/
kollo's avatar
kollo committed
12 13 14

typedef struct {
  unsigned char BRAs;       /* DC_BRAs */
kollo's avatar
kollo committed
15
  unsigned char offs;       /* sizeof(BYTECODE_HEADER)-2*/
kollo's avatar
kollo committed
16
  unsigned short rsv;       /*  not used */
17 18 19 20 21 22 23 24
  uint32_t textseglen; /* size of the text/code segment */
  uint32_t rodataseglen; /* size of the readonly data segment */
  uint32_t sdataseglen; /* size of the data segment for DATA statements*/
  uint32_t dataseglen;   /* size of the data segment */
  uint32_t bssseglen;    /* storage space for variables */
  uint32_t symbolseglen; /* size of the symbol table segment */
  uint32_t stringseglen; /* size of the string segment */
  uint32_t relseglen;    /* size of the relocation data */
kollo's avatar
kollo committed
25
  unsigned short flags;       /* not used */
kollo's avatar
kollo committed
26
  unsigned short version;     /* version, currently=0x117 */
kollo's avatar
kollo committed
27 28 29
} BYTECODE_HEADER;


kollo's avatar
kollo committed
30 31
#define EXE_REL 1     /* If relocation is needed */

kollo's avatar
kollo committed
32
typedef struct {
kollo's avatar
kollo committed
33 34
  unsigned char typ;      /* Typ of the symbol */
  unsigned char subtyp;
kollo's avatar
kollo committed
35
  unsigned short rsv;       /*  not used */
36 37
  uint32_t name;      /* Pointer to Symbol string table */
  uint32_t adr;       /* address */
kollo's avatar
kollo committed
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
} BYTECODE_SYMBOL;

/* Symbol types */

#define STT_NOTYPE  0
#define STT_OBJECT  1
#define STT_FUNC    2
#define STT_LABEL   3
#define STT_DATAPTR 4


/* Bitmuster fuer die Kommandos, so codiert, dass die anzahl der folgenden
   parameter-bytes mit drin codiert ist:
   
   00000000
         ^^----0,1,2,4 folgende Bytes
*/
/* Gruppe 0: Ausnahmen und Interrupts */

#define BC_BRK     0x00   /* 00000000  --- BRK Breakpoint, this will cause illegal instruction error */
#define BC_INT1    0x01   /* 00000001  --- Interrupt 1 */
kollo's avatar
kollo committed
59 60 61 62 63 64 65
#define BC_INT2    0x02   /* 00000010  --- Interrupt 2 */
#define BC_INT3    0x03   /* 00000011  --- Interrupt 3 */
#define BC_INT4    0x04   /* 00000100  --- Interrupt 4 */
#define BC_INT5    0x05   /* 00000101  --- Interrupt 5 */
#define BC_INT6    0x06   /* 00000110  --- Interrupt 6 */
#define BC_INT7    0x07   /* 00000111  --- Interrupt 7 */

kollo's avatar
kollo committed
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
#define BC_TRAP0   0x08   /* 00001000  --- TRAP0 */
#define BC_TRAP1   0x09   /* 00001001  --- TRAP1 */
#define BC_TRAP2   0x0a   /* 00001010  --- TRAP2 */
#define BC_TRAP3   0x0b   /* 00001011  --- TRAP3 */
#define BC_TRAP4   0x0c   /* 00001100  --- TRAP4 */
#define BC_TRAP5   0x0d   /* 00001101  --- TRAP5 */
#define BC_TRAP6   0x0e   /* 00001110  --- TRAP6 */
#define BC_TRAP7   0x0f   /* 00001111  --- TRAP7 */

/* Gruppe 1: Programmlauf-Kontrolle */

#define BC_HALT    0x10 /*   00010000  --- HLT Halt */
#define BC_BRAs    0x11 /*   00010001  --- BRA.s  very short Branch */
#define BC_BRA     0x12 /*   00010010  --- BRA short Branch */
#define BC_JMP     0x13 /*   00010011  --- JMP Long Jump */
#define BC_RTS     0x14 /*   00010100  --- RTS Return from Subroutine */
                        /*   00010101  --- */
#define BC_BSR     0x16 /*   00010110  --- BSR Branch to Subroutine */
#define BC_JSR     0x17 /*   00010111  --- JSR jump to Subroutine */
kollo's avatar
kollo committed
85
#define BC_BLKSTART 0x18 /*   00011000  --- Save context */
kollo's avatar
kollo committed
86 87 88
#define BC_BEQs    0x19 /*   00011001  --- BEQ.s short Branch if 0 */
#define BC_BEQ     0x1a /*   00011010  --- BEQ   short Branch if 0 */
#define BC_JEQ     0x1b /*   00011011  --- JEQ jump if 0 */
kollo's avatar
kollo committed
89
#define BC_BLKEND  0x1c /*   00011100  --- Restore context */
kollo's avatar
kollo committed
90 91 92 93 94 95
                        /*   00011101  ---  */
                        /*   00011110  ---  */
#define BC_RESTORE 0x1f /*   00011111  --- RESTORE Restore datapointer */

/* Gruppe 2: Operand Stack Manipulation Operators 0 in 1 out */

kollo's avatar
kollo committed
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
#define BC_PUSHLEER 0x20 /*     --- PUSHLEER Push an empty parameter */

#define BC_PUSHCOMM 0x22 /*     --- push command  */

#define BC_COUNT    0x24 /*     --- COUNT the number of elements on stack */
#define BC_PUSHSYS  0x25 /*     --- PUSHSYS push system variable */
#define BC_PUSHFUNC 0x26 /*     --- push function */
#define BC_PUSHPROC 0x27 /*     --- push procedure label */
#define BC_PUSHK    0x28 /*     ---  Push a keyword by index */
#define BC_PUSHSSYS 0x29 /*     --- PUSHSSYS push system string variable */
#define BC_PUSHSFUNC 0x2a /*    --- push string function */
#define BC_PUSHLABEL 0x2b /*    --- push a label  */
#define BC_PUSHX     0x2c /*    ---  Push a keyword */
#define BC_PUSHASYS 0x2d /*     --- PUSHASYS */
#define BC_PUSHAFUNC 0x2e /*    --- push array function */
#define BC_PUSHA     0x2f /*    --- push an array constant */
kollo's avatar
kollo committed
112 113

/* Gruppe 3: Operand Stack Manipulation Operators 0 in 1 out */
kollo's avatar
kollo committed
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128

#define BC_PUSH0    0x30 /*   ---  PUSH0 push a zero (int 0) */
#define BC_PUSH1    0x31 /*   ---  Push a int 1 */
#define BC_PUSH2    0x32 /*   ---  Push a int 2 */
#define BC_PUSHM1   0x33 /*   ---  Push a int -1 */
#define BC_PUSHB    0x34 /*   ---  Push a byte int constant */
#define BC_PUSHW    0x35 /*   ---  Push a short int */

#define BC_PUSHI    0x39 /*   ---  Push an int constant  */
#define BC_PUSHF    0x3a /*   ---  Push a double constant */
#define BC_PUSHAI   0x3b /*   ---  Push a big int constant */
#define BC_PUSHAF   0x3c /*   ---  Push a big float constant */
#define BC_PUSHC    0x3d /*   ---  Push a complex constant */
#define BC_PUSHAC   0x3e /*   ---  Push a big complex constant */
#define BC_PUSHS    0x3f /*   ---  Push a string constant */
kollo's avatar
kollo committed
129 130 131 132



/* Gruppe 4: Operatoren (unitaer 1 in 1 out) */
kollo's avatar
kollo committed
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
#define BC_NOT      0x40 /*     --- NOT big int*/
#define BC_NOTi     0x41 /*     --- NOT int */
#define BC_NEG      0x44 /*     --- NEG */

#define BC_I2FILE   0x46 /*     --- cast int --> file#  */
#define BC_F2C      0x47 /*     --- cast  float --> complex */
#define BC_I2F      0x48 /*     --- cast int --> float  */
#define BC_X2I      0x49 /*     --- cast to int  */
#define BC_X2F      0x4a /*     --- cast to float */
#define BC_X2AI     0x4b /*     --- cast to big int  */
#define BC_X2AF     0x4c /*     --- cast to big float */
#define BC_X2C      0x4d /*     --- cast to complex */
#define BC_X2AC     0x4e /*     --- cast to big complex */



kollo's avatar
kollo committed
149 150 151 152 153 154
/* Gruppe 5: Operatoren (unitaer 1 in 2 out) */
#define BC_DUP      0x50 /*   01010000  --- DUP */
                         /*   01010100  ---  */
                         /*   01011000  ---  */
                         /*   01011100  ---  */
/* Gruppe 6: Operatoren (binaer 2 in 1 out) Arithmetic and Math Operators  */
kollo's avatar
kollo committed
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
#define BC_ADD      0x60 /*      --- ADD */
#define BC_ADDi     0x61 /*      --- ADD int */
#define BC_ADDf     0x62 /*      --- ADD floats */
#define BC_ADDc     0x63 /*      --- ADD complexs */
#define BC_ADDs     0x64 /*      --- ADD strings */
#define BC_SUB      0x65 /*      --- SUB */
#define BC_SUBi     0x66 /*      --- SUB int */
#define BC_SUBf     0x67 /*      --- SUB floats */
#define BC_SUBc     0x68 /*      --- SUB complexs */
#define BC_MUL      0x69 /*      --- MUL */
#define BC_MULi     0x6a /*      --- MUL int */
#define BC_MULf     0x6b /*      --- MUL float */
#define BC_MULc     0x6c /*      --- MUL complexs */
#define BC_DIV      0x6d /*      --- DIV */
#define BC_DIVf     0x6e /*      --- DIV floats*/
#define BC_DIVc     0x6f /*      --- DIV complexs*/

/* Gruppe 7: Operatoren (binaeout)  Arithmetic and Math Operators */
#define BC_AND      0x70 /*     --- AND big int*/
#define BC_ANDi     0x71 /*     --- AND int */
#define BC_OR       0x74 /*     --- OR */
#define BC_ORi      0x75 /*     --- OR int */
#define BC_POW      0x78 /*     --- POW */
#define BC_MOD      0x7c /*     --- MOD */
kollo's avatar
kollo committed
179 180

/* Gruppe 8: Operatoren (binaer 2 in 1 out) Logic Math Operators */
kollo's avatar
kollo committed
181 182 183 184 185
#define BC_XOR      0x80 /*      --- XOR big int */
#define BC_XORi     0x81 /*      --- XOR int */
#define BC_EQUAL    0x84 /*      --- EQUAL */
#define BC_LESS     0x88 /*      --- LESS */
#define BC_GREATER  0x8c /*      --- GREATER */
kollo's avatar
kollo committed
186 187 188 189 190 191
/* Gruppe 9: Operatoren (2 in 2 out) */
#define BC_EXCH     0x90 /*    10010000  --- EXCHG   */

/* Gruppe 10: Operatoren (x in 0 out)   */
#define BC_POP      0xa0 /*    10100000  --- POP */
#define BC_CLEAR    0xa4 /*    10100100  --- CLEAR remove all elements from stack*/
kollo's avatar
kollo committed
192
#define BC_LOCAL    0xa6 /*    10100110  --- LOCAL */
kollo's avatar
kollo committed
193
#define BC_NOOP     0xa8 /*    10101000  --- NOOP */
kollo's avatar
kollo committed
194

kollo's avatar
kollo committed
195
#define BC_ZUWEISc  0xab /*    10101011  --- ZUWEIS complex */
kollo's avatar
kollo committed
196 197
#define BC_ZUWEISf  0xac /*    10101100  --- ZUWEIS float */
#define BC_ZUWEISi  0xad /*    10101101  --- ZUWEIS integer */
kollo's avatar
kollo committed
198
#define BC_ZUWEIS   0xae /*    10101110  --- ZUWEIS */
kollo's avatar
kollo committed
199 200 201
#define BC_COMMENT  0xaf /*    10101111  --- Comment */
   
/* Gruppe 11: Variablen Management */
kollo's avatar
kollo committed
202
#define BC_PUSHVVI    0xb0 /*    10110000  --- */
kollo's avatar
kollo committed
203 204 205 206 207 208
#define BC_ZUWEISINDEX 0xb3 /*    10110011  --- */
#define BC_EVAL     0xb4 /*    10110100  --- EVAL */
#define BC_PUSHV    0xb8 /*    10111000  --- PUSHV */
#define BC_PUSHVV   0xbc /*    10111100  --- PUSHVV */
#define BC_PUSHARRAYELEM 0xbf /* 10111111  --- PUSHARRAYELEM */

kollo's avatar
kollo committed
209 210 211
/* Gruppe 12: Memory Management */
#define BC_LOADi    0xc3 /*    11000011  --- push integer from address*/
#define BC_LOADf    0xc7 /*    11000111  --- push float from address */
kollo's avatar
kollo committed
212
#define BC_LOADc    0xc8 /*    11001000  --- push complex from address */
kollo's avatar
kollo committed
213 214 215 216 217 218
#define BC_LOADs    0xcb /*    11001011  --- push string from address */
#define BC_LOADa    0xcf /*    11001111  --- push array from address */

/* Gruppe 13: Memory Management */
#define BC_SAVEi    0xd3 /*    11010011  --- store integer from address*/
#define BC_SAVEf    0xd7 /*    11010111  --- store float from address */
kollo's avatar
kollo committed
219
#define BC_SAVEc    0xd8 /*    11011000  --- store complex from address */
kollo's avatar
kollo committed
220 221 222
#define BC_SAVEs    0xdb /*    11011011  --- store string from address */
#define BC_SAVEa    0xdf /*    11011111  --- store array from address */

kollo's avatar
kollo committed
223 224
/* Gruppe 14: Frei */
/* Gruppe 15: Frei */
kollo's avatar
kollo committed
225

kollo's avatar
kollo committed
226
#define BC_FALSE  BC_PUSH0     /* Push a logical FALSE */
kollo's avatar
kollo committed
227
#define BC_TRUE   BC_PUSHM1     /* Push a logical TRUE */
kollo's avatar
kollo committed
228

kollo's avatar
kollo committed
229 230 231 232
//#define BC_PUSHUSERFUNC '@'
//#define BC_COPY  'C'
//#define BC_INDEX 'I'     /* Duplicate arbitrary element */
//#define BC_ROLL  'R'     /* Roll n elements up j times */
kollo's avatar
kollo committed
233

kollo's avatar
kollo committed
234
/* Interpretierungen */
kollo's avatar
kollo committed
235

kollo's avatar
kollo committed
236
//#define BC_LINE        1
kollo's avatar
kollo committed
237 238 239

/* Protos */

kollo's avatar
kollo committed
240
PARAMETER *virtual_machine(STRING, int, int *, const PARAMETER *, int);
kollo's avatar
kollo committed
241
void compile(int verbose);
kollo's avatar
kollo committed
242 243
int bc_parser(const char *funktion);
int add_rodata(char *data,int len);
kollo's avatar
kollo committed
244 245

#if 0
kollo's avatar
kollo committed
246 247 248 249 250 251 252 253 254 255 256 257 258 259
int vm_add(PARAMETER *sp);
int vm_sub(PARAMETER *sp);
int vm_mul(PARAMETER *sp);
int vm_pow(PARAMETER *sp);
int vm_mod(PARAMETER *sp);
int vm_equal(PARAMETER *sp);
int vm_greater(PARAMETER *sp);
int vm_less(PARAMETER *sp);
int vm_sysvar(PARAMETER *sp,int n);
int vm_ssysvar(PARAMETER *sp,int n);
int vm_asysvar(PARAMETER *sp,int n);
int vm_dup(PARAMETER *sp);
int vm_exch(PARAMETER *sp);
int vm_neg(PARAMETER *sp);
kollo's avatar
kollo committed
260

kollo's avatar
kollo committed
261 262 263
void cast_to_real(PARAMETER *sp);
void cast_to_int(PARAMETER *sp);
int vm_sfunc(PARAMETER *sp,int i, int anzarg);
264
int vm_afunc(PARAMETER *sp,int i, int anzarg);
kollo's avatar
kollo committed
265 266 267 268 269 270 271 272 273 274
int vm_func(PARAMETER *sp,int i, int anzarg);
int vm_comm(PARAMETER *sp,int i, int anzarg);
int vm_pushvv(int vnr,PARAMETER *sp);
int vm_zuweis(int vnr,PARAMETER *sp);
void zuweis_v_parameter(VARIABLE *v,PARAMETER *p);
int vm_zuweisindex(int vnr,PARAMETER *sp,int dim);
int vm_pusharrayelem(int vnr,PARAMETER *sp, int dim);
int vm_pushv(int vnr,PARAMETER *sp);
void  push_v(PARAMETER *p, VARIABLE *v);
int vm_eval(PARAMETER *sp);
kollo's avatar
kollo committed
275
#endif