1 /*  Copyright (C) 1996-1997  Id Software, Inc.
2 
3     This program is free software; you can redistribute it and/or modify
4     it under the terms of the GNU General Public License as published by
5     the Free Software Foundation; either version 2 of the License, or
6     (at your option) any later version.
7 
8     This program is distributed in the hope that it will be useful,
9     but WITHOUT ANY WARRANTY; without even the implied warranty of
10     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11     GNU General Public License for more details.
12 
13     You should have received a copy of the GNU General Public License
14     along with this program; if not, write to the Free Software
15     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 
17     See file, 'COPYING', for details.
18 */
19 
20 // this file is shared by QuakeForge and qfcc
21 #ifndef __pr_comp_h
22 #define __pr_comp_h
23 
24 #include "QF/qtypes.h"
25 
26 typedef int16_t pr_short_t;
27 typedef uint16_t pr_ushort_t;
28 typedef int32_t pr_int_t;
29 typedef uint32_t pr_uint_t;
30 typedef pr_int_t func_t;
31 typedef pr_int_t string_t;
32 typedef pr_uint_t pointer_t;
33 
34 typedef enum {
35 	ev_void,
36 	ev_string,
37 	ev_float,
38 	ev_vector,
39 	ev_entity,
40 	ev_field,
41 	ev_func,
42 	ev_pointer,			// end of v6 types
43 	ev_quat,
44 	ev_integer,
45 	ev_uinteger,
46 	ev_short,			// value is embedded in the opcode
47 
48 	ev_invalid,			// invalid type. used for instruction checking
49 	ev_type_count		// not a type, gives number of types
50 } etype_t;
51 
52 extern int pr_type_size[ev_type_count];
53 extern const char *pr_type_name[ev_type_count];
54 
55 #define	OFS_NULL		0
56 #define	OFS_RETURN		1
57 #define	OFS_PARM0		4		// leave 3 ofs for each parm to hold vectors
58 #define	OFS_PARM1		7
59 #define	OFS_PARM2		10
60 #define	OFS_PARM3		13
61 #define	OFS_PARM4		16
62 #define	OFS_PARM5		19
63 #define	OFS_PARM6		22
64 #define	OFS_PARM7		25
65 #define	RESERVED_OFS	28
66 
67 
68 typedef enum {
69 	OP_DONE,
70 	OP_MUL_F,
71 	OP_MUL_V,
72 	OP_MUL_FV,
73 	OP_MUL_VF,
74 	OP_DIV_F,
75 	OP_ADD_F,
76 	OP_ADD_V,
77 	OP_SUB_F,
78 	OP_SUB_V,
79 
80 	OP_EQ_F,
81 	OP_EQ_V,
82 	OP_EQ_S,
83 	OP_EQ_E,
84 	OP_EQ_FN,
85 
86 	OP_NE_F,
87 	OP_NE_V,
88 	OP_NE_S,
89 	OP_NE_E,
90 	OP_NE_FN,
91 
92 	OP_LE_F,
93 	OP_GE_F,
94 	OP_LT_F,
95 	OP_GT_F,
96 
97 	OP_LOAD_F,
98 	OP_LOAD_V,
99 	OP_LOAD_S,
100 	OP_LOAD_ENT,
101 	OP_LOAD_FLD,
102 	OP_LOAD_FN,
103 
104 	OP_ADDRESS,
105 
106 	OP_STORE_F,
107 	OP_STORE_V,
108 	OP_STORE_S,
109 	OP_STORE_ENT,
110 	OP_STORE_FLD,
111 	OP_STORE_FN,
112 
113 	OP_STOREP_F,
114 	OP_STOREP_V,
115 	OP_STOREP_S,
116 	OP_STOREP_ENT,
117 	OP_STOREP_FLD,
118 	OP_STOREP_FN,
119 
120 	OP_RETURN,
121 	OP_NOT_F,
122 	OP_NOT_V,
123 	OP_NOT_S,
124 	OP_NOT_ENT,
125 	OP_NOT_FN,
126 	OP_IF,
127 	OP_IFNOT,
128 	OP_CALL0,
129 	OP_CALL1,
130 	OP_CALL2,
131 	OP_CALL3,
132 	OP_CALL4,
133 	OP_CALL5,
134 	OP_CALL6,
135 	OP_CALL7,
136 	OP_CALL8,
137 	OP_STATE,
138 	OP_GOTO,
139 	OP_AND,
140 	OP_OR,
141 
142 	OP_BITAND,
143 	OP_BITOR,
144 
145 	OP_ADD_S,
146 	OP_LE_S,
147 	OP_GE_S,
148 	OP_LT_S,
149 	OP_GT_S,
150 
151 	OP_ADD_I,
152 	OP_SUB_I,
153 	OP_MUL_I,
154 	OP_DIV_I,
155 	OP_BITAND_I,
156 	OP_BITOR_I,
157 	OP_GE_I,
158 	OP_LE_I,
159 	OP_GT_I,
160 	OP_LT_I,
161 	OP_AND_I,
162 	OP_OR_I,
163 	OP_NOT_I,
164 	OP_EQ_I,
165 	OP_NE_I,
166 	OP_STORE_I,
167 	OP_STOREP_I,
168 	OP_LOAD_I,
169 
170 	OP_CONV_IF,
171 	OP_CONV_FI,
172 
173 	OP_BITXOR_F,
174 	OP_BITXOR_I,
175 	OP_BITNOT_F,
176 	OP_BITNOT_I,
177 
178 	OP_SHL_F,
179 	OP_SHR_F,
180 	OP_SHL_I,
181 	OP_SHR_I,
182 
183 	OP_MOD_F,
184 	OP_MOD_I,
185 
186 	OP_LOADB_F,
187 	OP_LOADB_V,
188 	OP_LOADB_S,
189 	OP_LOADB_ENT,
190 	OP_LOADB_FLD,
191 	OP_LOADB_FN,
192 	OP_LOADB_I,
193 	OP_LOADB_P,
194 
195 	OP_STOREB_F,
196 	OP_STOREB_V,
197 	OP_STOREB_S,
198 	OP_STOREB_ENT,
199 	OP_STOREB_FLD,
200 	OP_STOREB_FN,
201 	OP_STOREB_I,
202 	OP_STOREB_P,
203 
204 	OP_ADDRESS_VOID,
205 	OP_ADDRESS_F,
206 	OP_ADDRESS_V,
207 	OP_ADDRESS_S,
208 	OP_ADDRESS_ENT,
209 	OP_ADDRESS_FLD,
210 	OP_ADDRESS_FN,
211 	OP_ADDRESS_I,
212 	OP_ADDRESS_P,
213 
214 	OP_LEA,
215 
216 	OP_IFBE,
217 	OP_IFB,
218 	OP_IFAE,
219 	OP_IFA,
220 
221 	OP_JUMP,
222 	OP_JUMPB,
223 
224 	OP_LT_U,
225 	OP_GT_U,
226 	OP_LE_U,
227 	OP_GE_U,
228 
229 	OP_LOADBI_F,
230 	OP_LOADBI_V,
231 	OP_LOADBI_S,
232 	OP_LOADBI_ENT,
233 	OP_LOADBI_FLD,
234 	OP_LOADBI_FN,
235 	OP_LOADBI_I,
236 	OP_LOADBI_P,
237 
238 	OP_STOREBI_F,
239 	OP_STOREBI_V,
240 	OP_STOREBI_S,
241 	OP_STOREBI_ENT,
242 	OP_STOREBI_FLD,
243 	OP_STOREBI_FN,
244 	OP_STOREBI_I,
245 	OP_STOREBI_P,
246 
247 	OP_LEAI,
248 
249 	OP_LOAD_P,
250 	OP_STORE_P,
251 	OP_STOREP_P,
252 	OP_NOT_P,
253 	OP_EQ_P,
254 	OP_NE_P,
255 	OP_LE_P,
256 	OP_GE_P,
257 	OP_LT_P,
258 	OP_GT_P,
259 
260 	OP_MOVEI,
261 	OP_MOVEP,
262 	OP_MOVEPI,
263 
264 	OP_SHR_U,
265 
266 	OP_STATE_F,
267 
268 	OP_ADD_Q,
269 	OP_SUB_Q,
270 	OP_MUL_Q,
271 	OP_MUL_QF,
272 	OP_MUL_FQ,
273 	OP_MUL_QV,
274 	OP_CONJ_Q,
275 	OP_NOT_Q,
276 	OP_EQ_Q,
277 	OP_NE_Q,
278 	OP_STORE_Q,
279 	OP_STOREB_Q,
280 	OP_STOREBI_Q,
281 	OP_STOREP_Q,
282 	OP_LOAD_Q,
283 	OP_LOADB_Q,
284 	OP_LOADBI_Q,
285 	OP_ADDRESS_Q,
286 
287 	OP_RCALL0,
288 	OP_RCALL1,
289 	OP_RCALL2,
290 	OP_RCALL3,
291 	OP_RCALL4,
292 	OP_RCALL5,
293 	OP_RCALL6,
294 	OP_RCALL7,
295 	OP_RCALL8,
296 
297 	OP_RETURN_V,
298 } pr_opcode_e;
299 
300 typedef struct opcode_s {
301 	const char	*name;
302 	const char	*opname;
303 	pr_opcode_e opcode;
304 	qboolean	right_associative;
305 	etype_t		type_a, type_b, type_c;
306 	unsigned int min_version;
307 	const char	*fmt;
308 } opcode_t;
309 
310 extern opcode_t pr_opcodes[];
311 opcode_t *PR_Opcode (pr_short_t opcode);
312 void PR_Opcode_Init (void);
313 
314 typedef struct dstatement_s {
315 	pr_opcode_e op:16;
316 	pr_ushort_t a,b,c;
317 } __attribute__((gcc_struct)) dstatement_t;
318 
319 typedef struct ddef_s {
320 	pr_ushort_t type;			// if DEF_SAVEGLOBGAL bit is set
321 								// the variable needs to be saved in savegames
322 	pr_ushort_t ofs;
323 	pr_int_t    s_name;
324 } ddef_t;
325 
326 #define	DEF_SAVEGLOBAL	(1<<15)
327 
328 #define	MAX_PARMS	8
329 
330 typedef struct dfunction_s {
331 	pr_int_t    first_statement;	// negative numbers are builtins
332 	pr_int_t    parm_start;
333 	pr_int_t    locals;				// total ints of parms + locals
334 
335 	pr_int_t    profile;			// runtime
336 
337 	pr_int_t    s_name;
338 	pr_int_t    s_file;				// source file defined in
339 
340 	pr_int_t    numparms;
341 	uint8_t     parm_size[MAX_PARMS];
342 } dfunction_t;
343 
344 typedef union pr_type_u {
345 	float       float_var;
346 	string_t    string_var;
347 	func_t      func_var;
348 	pr_int_t    entity_var;
349 	float       vector_var[1];	// really 3, but this structure must be 32 bits
350 	float       quat_var[1];	// really 4, but this structure must be 32 bits
351 	pr_int_t    integer_var;
352 	pointer_t   pointer_var;
353 	pr_uint_t   uinteger_var;
354 } pr_type_t;
355 
356 typedef struct pr_va_list_s {
357 	pr_int_t    count;
358 	pointer_t   list;			// pr_type_t
359 } pr_va_list_t;
360 
361 #define PROG_VERSION_ENCODE(a,b,c)	\
362 	( (((0x##a) & 0x0ff) << 24)		\
363 	 |(((0x##b) & 0xfff) << 12)		\
364 	 |(((0x##c) & 0xfff) <<  0) )
365 #define	PROG_ID_VERSION	6
366 #define	PROG_VERSION	PROG_VERSION_ENCODE(0,fff,009)
367 
368 typedef struct dprograms_s {
369 	pr_uint_t   version;
370 	pr_uint_t   crc;			// check of header file
371 
372 	pr_uint_t   ofs_statements;
373 	pr_uint_t   numstatements;	// statement 0 is an error
374 
375 	pr_uint_t   ofs_globaldefs;
376 	pr_uint_t   numglobaldefs;
377 
378 	pr_uint_t   ofs_fielddefs;
379 	pr_uint_t   numfielddefs;
380 
381 	pr_uint_t   ofs_functions;
382 	pr_int_t    numfunctions;	// function 0 is an empty
383 
384 	pr_uint_t   ofs_strings;
385 	pr_int_t    numstrings;		// first string is a null string
386 
387 	pr_uint_t   ofs_globals;
388 	pr_uint_t   numglobals;
389 
390 	pr_uint_t   entityfields;
391 } dprograms_t;
392 
393 #endif // __pr_comp_h
394