]> git.xonotic.org Git - xonotic/gmqcc.git/blob - exec.h
Moving typedefs and defines from exec.c into an exec.h header
[xonotic/gmqcc.git] / exec.h
1 /*
2  * Copyright (C) 2012
3  *     Wolfgang Bumiller
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of
6  * this software and associated documentation files (the "Software"), to deal in
7  * the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9  * of the Software, and to permit persons to whom the Software is furnished to do
10  * so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21  * SOFTWARE.
22  */
23
24 #ifndef GMQCC_EXEC_HDR
25 #define GMQCC_EXEC_HDR
26
27 /* darkplaces has (or will have) a 64 bit prog loader
28  * where the 32 bit qc program is autoconverted on load.
29  * Since we may want to support that as well, let's redefine
30  * float and int here.
31  */
32 typedef float   qcfloat;
33 typedef int32_t qcint;
34
35 typedef union {
36     qcint   _int;
37     qcint    string;
38     qcint    function;
39     qcint    edict;
40     qcfloat _float;
41     qcfloat vector[3];
42     qcint   ivector[3];
43 } qcany;
44
45 typedef char qcfloat_size_is_correct [sizeof(qcfloat) == 4 ?1:-1];
46 typedef char qcint_size_is_correct   [sizeof(int)     == 4 ?1:-1];
47
48 typedef struct {
49     uint32_t offset;
50     uint32_t length;
51 } prog_section;
52
53 typedef struct {
54     uint32_t     version;
55     uint16_t     crc16;
56     uint16_t     skip;
57
58     prog_section statements;
59     prog_section defs;
60     prog_section fields;
61     prog_section functions;
62     prog_section strings;
63     prog_section globals;
64     uint32_t     entfield;
65 } prog_header;
66
67 typedef prog_section_both      prog_def;
68 typedef prog_section_function  prog_function;
69 typedef prog_section_statement prog_statement;
70
71 enum {
72     VMERR_OK,
73     VMERR_TEMPSTRING_ALLOC,
74
75     VMERR_END
76 };
77
78 #define JUMPS_DEFAULT 1000000
79
80 #define VMXF_DEFAULT 0x0000
81 #define VMXF_TRACE   0x0001
82 #define VMXF_PROFILE 0x0002
83
84 struct qc_program_s;
85
86 typedef int (*prog_builtin)(struct qc_program_s *prog);
87
88 typedef struct {
89     qcint          stmt;
90     size_t         localsp;
91     prog_function *function;
92 } qc_exec_stack;
93
94 typedef struct qc_program_s {
95     char           *filename;
96
97     MEM_VECTOR_MAKE(prog_statement, code);
98     MEM_VECTOR_MAKE(prog_def,       defs);
99     MEM_VECTOR_MAKE(prog_def,       fields);
100     MEM_VECTOR_MAKE(prog_function,  functions);
101     MEM_VECTOR_MAKE(char,           strings);
102     MEM_VECTOR_MAKE(qcint,          globals);
103     MEM_VECTOR_MAKE(qcint,          entitydata);
104
105     size_t tempstring_start;
106     size_t tempstring_at;
107
108     qcint  vmerror;
109
110     MEM_VECTOR_MAKE(size_t, profile);
111
112     MEM_VECTOR_MAKE(prog_builtin, builtins);
113
114     /* size_t ip; */
115     qcint  entities;
116     size_t entityfields;
117     bool   allowworldwrites;
118
119     MEM_VECTOR_MAKE(qcint,         localstack);
120     MEM_VECTOR_MAKE(qc_exec_stack, stack);
121     size_t statement;
122
123     int    argc; /* current arg count for debugging */
124 } qc_program;
125 MEM_VEC_FUNCTIONS(qc_program, prog_statement, code)
126 MEM_VEC_FUNCTIONS(qc_program, prog_def,       defs)
127 MEM_VEC_FUNCTIONS(qc_program, prog_def,       fields)
128 MEM_VEC_FUNCTIONS(qc_program, prog_function,  functions)
129 MEM_VEC_FUNCTIONS(qc_program, char,           strings)
130 _MEM_VEC_FUN_APPEND(qc_program, char, strings)
131 _MEM_VEC_FUN_RESIZE(qc_program, char, strings)
132 MEM_VEC_FUNCTIONS(qc_program, qcint,          globals)
133 MEM_VEC_FUNCTIONS(qc_program, qcint,          entitydata)
134
135 MEM_VEC_FUNCTIONS(qc_program,   qcint, localstack)
136 _MEM_VEC_FUN_APPEND(qc_program, qcint, localstack)
137 _MEM_VEC_FUN_RESIZE(qc_program, qcint, localstack)
138 MEM_VEC_FUNCTIONS(qc_program,   qc_exec_stack, stack)
139
140 MEM_VEC_FUNCTIONS(qc_program,   size_t, profile)
141 _MEM_VEC_FUN_RESIZE(qc_program, size_t, profile)
142
143 qc_program* prog_load(const char *filename);
144 void        prog_delete(qc_program *prog);
145
146 #endif /* GMQCC_EXEC_HDR */