1 2 #include "cmdlib.h" 3 #include <stdio.h> 4 #include <setjmp.h> 5 6 7 // offsets are allways multiplied by 4 before using 8 typedef int gofs_t; // offset in global data block 9 typedef struct function_s function_t; 10 11 typedef enum {ev_void, ev_string, ev_float, ev_vector, ev_entity, ev_field, ev_function, ev_pointer, ev_bad = -1} etype_t; 12 13 14 #define MAX_PARMS 8 15 16 17 typedef struct statement_s 18 { 19 unsigned short op; 20 short a,b,c; 21 } dstatement_t; 22 23 typedef struct 24 { 25 unsigned short type; // if DEF_SAVEGLOBGAL bit is set 26 // the variable needs to be saved in savegames 27 unsigned short ofs; 28 int s_name; 29 } ddef_t; 30 #define DEF_SAVEGLOBGAL (1<<15) 31 32 #define MAX_PARMS 8 33 34 typedef struct 35 { 36 int first_statement; // negative numbers are builtins 37 int parm_start; 38 int locals; // total ints of parms + locals 39 40 int profile; // runtime 41 42 int s_name; 43 int s_file; // source file defined in 44 45 int numparms; 46 byte parm_size[MAX_PARMS]; 47 } dfunction_t; 48 49 50 #define PROG_VERSION 6 51 typedef struct 52 { 53 int version; 54 int crc; // check of header file 55 56 int ofs_statements; 57 int numstatements; // statement 0 is an error 58 59 int ofs_globaldefs; 60 int numglobaldefs; 61 62 int ofs_fielddefs; 63 int numfielddefs; 64 65 int ofs_functions; 66 int numfunctions; // function 0 is an empty 67 68 int ofs_strings; 69 int numstrings; // first string is a null string 70 71 int ofs_globals; 72 int numglobals; 73 74 int entityfields; 75 } dprograms_t; 76 77 78 79 80 //============================================================================ 81 82 // pr_loc.h -- program local defs 83 84 #define MAX_ERRORS 10 85 86 #define MAX_NAME 64 // chars long 87 88 #define MAX_REGS 16384 89 90 //============================================================================= 91 typedef struct type_s 92 { 93 etype_t type; 94 struct def_s *def; // a def that points to this type 95 struct type_s *next; 96 // function types are more complex 97 struct type_s *aux_type; // return type or field type 98 int num_parms; // -1 = variable args 99 struct type_s *parm_types[MAX_PARMS]; // only [num_parms] allocated 100 //char parm_names[MAX_PARMS][MAX_NAME]; 101 } type_t; 102 103 extern char *typenames[]; 104 105 106 typedef struct def_s 107 { 108 type_t *type; 109 char *name; 110 struct def_s *next; 111 gofs_t ofs; 112 struct def_s *scope; // function the var was defined in, or NULL 113 int constant; // 1 when a declaration included "= immediate" 114 int save; 115 etype_t cast; // used for type casting 116 char *s_file; // used for reclaration info 117 int line; // " " 118 // FrikaC: newtest 119 120 struct def_s *tempnext; 121 // FrikaC: newtest end 122 } def_t; 123 124 125 #define OFS_NULL 0 126 #define OFS_RETURN 1 127 #define OFS_PARM0 4 // leave 3 ofs for each parm to hold vectors 128 #define OFS_PARM1 7 129 #define OFS_PARM2 10 130 #define OFS_PARM3 13 131 #define OFS_PARM4 16 132 #define RESERVED_OFS 28 133 134 135 136 137 #define G_FLOAT(o) (pr_globals[o]) 138 #define G_INT(o) (*(int *)&pr_globals[o]) 139 #define G_VECTOR(o) (&pr_globals[o]) 140 #define G_STRING(o) (strings + *(string_t *)&pr_globals[o]) 141 #define G_FUNCTION(o) (*(func_t *)&pr_globals[o]) 142 143 //============================================================================= 144 145 #define MAX_STRINGS 500000 146 #define MAX_GLOBALS 16384 147 #define MAX_FIELDS 1024 148 #define MAX_STATEMENTS 65536 149 #define MAX_FUNCTIONS 8192 150 151 #define MAX_SOUNDS 1024 152 #define MAX_MODELS 1024 153 #define MAX_FILES 1024 154 #define MAX_DATA_PATH 64 155 //============================================================================= 156 typedef int func_t; 157 typedef int string_t; 158 159 typedef union eval_s 160 { 161 string_t string; 162 float _float; 163 float vector[3]; 164 func_t function; 165 int _int; 166 union eval_s *ptr; 167 } eval_t; 168 169 extern int type_size[8]; 170 extern def_t *def_for_type[8]; 171 172 extern type_t type_void, type_string, type_float, type_vector, type_entity, type_field, type_function, type_pointer, type_floatfield; 173 174 extern def_t def_void, def_string, def_float, def_vector, def_entity, def_field, def_function, def_pointer; 175 176 struct function_s 177 { 178 int builtin; // if non 0, call an internal function 179 int code; // first statement 180 char *file; // source file with definition 181 int file_line; 182 struct def_s *def; 183 int parm_ofs[MAX_PARMS]; // allways contiguous, right? 184 }; 185 186 187 // 188 // output generated by prog parsing 189 // 190 typedef struct 191 { 192 char *memory; 193 int max_memory; 194 int current_memory; 195 type_t *types; 196 197 def_t def_head; // unused head of linked list 198 def_t *def_tail; // add new defs after this and move it 199 200 int size_fields; 201 } pr_info_t; 202 203 typedef struct 204 { 205 char *name; 206 char *opname; 207 float priority; 208 boolean right_associative; 209 def_t *type_a, *type_b, *type_c; 210 } opcode_t; 211 212 extern char strings[MAX_STRINGS]; 213 extern int strofs; 214 215 extern dstatement_t statements[MAX_STATEMENTS]; 216 extern int numstatements; 217 extern int statement_linenums[MAX_STATEMENTS]; 218 219 extern dfunction_t functions[MAX_FUNCTIONS]; 220 extern int numfunctions; 221 222 extern float pr_globals[MAX_REGS]; 223 extern int numpr_globals; 224 225 extern char pr_immediate_string[2048]; 226 extern int pr_immediate_strlen; 227 228 extern char precache_sounds[MAX_SOUNDS][MAX_DATA_PATH]; 229 extern int precache_sounds_block[MAX_SOUNDS]; 230 extern int numsounds; 231 232 extern char precache_models[MAX_MODELS][MAX_DATA_PATH]; 233 extern int precache_models_block[MAX_SOUNDS]; 234 extern int nummodels; 235 236 extern char precache_files[MAX_FILES][MAX_DATA_PATH]; 237 extern int precache_files_block[MAX_SOUNDS]; 238 extern int numfiles; 239 240 241 // actual opcodes 242 enum { 243 OP2_DONE, 244 OP2_MUL_F, 245 OP2_MUL_V, 246 OP2_MUL_FV, 247 OP2_MUL_VF, 248 OP2_DIV_F, 249 OP2_ADD_F, 250 OP2_ADD_V, 251 OP2_SUB_F, 252 OP2_SUB_V, 253 254 OP2_EQ_F, 255 OP2_EQ_V, 256 OP2_EQ_S, 257 OP2_EQ_E, 258 OP2_EQ_FNC, 259 260 OP2_NE_F, 261 OP2_NE_V, 262 OP2_NE_S, 263 OP2_NE_E, 264 OP2_NE_FNC, 265 266 OP2_LE, 267 OP2_GE, 268 OP2_LT, 269 OP2_GT, 270 271 OP2_LOAD_F, 272 OP2_LOAD_V, 273 OP2_LOAD_S, 274 OP2_LOAD_ENT, 275 OP2_LOAD_FLD, 276 OP2_LOAD_FNC, 277 278 OP2_ADDRESS, 279 280 OP2_STORE_F, 281 OP2_STORE_V, 282 OP2_STORE_S, 283 OP2_STORE_ENT, 284 OP2_STORE_FLD, 285 OP2_STORE_FNC, 286 287 OP2_STOREP_F, 288 OP2_STOREP_V, 289 OP2_STOREP_S, 290 OP2_STOREP_ENT, 291 OP2_STOREP_FLD, 292 OP2_STOREP_FNC, 293 294 OP2_RETURN, 295 OP2_NOT_F, 296 OP2_NOT_V, 297 OP2_NOT_S, 298 OP2_NOT_ENT, 299 OP2_NOT_FNC, 300 OP2_IF, 301 OP2_IFNOT, 302 OP2_CALL0, 303 OP2_CALL1, 304 OP2_CALL2, 305 OP2_CALL3, 306 OP2_CALL4, 307 OP2_CALL5, 308 OP2_CALL6, 309 OP2_CALL7, 310 OP2_CALL8, 311 OP2_STATE, 312 OP2_GOTO, 313 OP2_AND, 314 OP2_OR, 315 316 OP2_BITAND, 317 OP2_BITOR 318 }; 319 320