]> git.xonotic.org Git - xonotic/gmqcc.git/blob - exec.h
tidying up, using the original typenames from code.c, renamed JUMPS_DEFAULT to VM_JUM...
[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(qcint)   == 4 ?1:-1];
47
48 enum {
49     VMERR_OK,
50     VMERR_TEMPSTRING_ALLOC,
51
52     VMERR_END
53 };
54
55 #define VM_JUMPS_DEFAULT 1000000
56
57 /* execute-flags */
58 #define VMXF_DEFAULT 0x0000     /* default flags - nothing */
59 #define VMXF_TRACE   0x0001     /* trace: print statements before executing */
60 #define VMXF_PROFILE 0x0002     /* profile: increment the profile counters */
61
62 struct qc_program_s;
63
64 typedef int (*prog_builtin)(struct qc_program_s *prog);
65
66 typedef struct {
67     qcint                  stmt;
68     size_t                 localsp;
69     prog_section_function *function;
70 } qc_exec_stack;
71
72 typedef struct qc_program_s {
73     char           *filename;
74
75     MEM_VECTOR_MAKE(prog_section_statement, code);
76     MEM_VECTOR_MAKE(prog_section_def,       defs);
77     MEM_VECTOR_MAKE(prog_section_def,       fields);
78     MEM_VECTOR_MAKE(prog_section_function,  functions);
79     MEM_VECTOR_MAKE(char,                   strings);
80     MEM_VECTOR_MAKE(qcint,                  globals);
81     MEM_VECTOR_MAKE(qcint,                  entitydata);
82
83     size_t tempstring_start;
84     size_t tempstring_at;
85
86     qcint  vmerror;
87
88     MEM_VECTOR_MAKE(size_t, profile);
89
90     MEM_VECTOR_MAKE(prog_builtin, builtins);
91
92     /* size_t ip; */
93     qcint  entities;
94     size_t entityfields;
95     bool   allowworldwrites;
96
97     MEM_VECTOR_MAKE(qcint,         localstack);
98     MEM_VECTOR_MAKE(qc_exec_stack, stack);
99     size_t statement;
100
101     int    argc; /* current arg count for debugging */
102 } qc_program;
103 MEM_VEC_FUNCTIONS(qc_program,   prog_section_statement, code)
104 MEM_VEC_FUNCTIONS(qc_program,   prog_section_def,       defs)
105 MEM_VEC_FUNCTIONS(qc_program,   prog_section_def,       fields)
106 MEM_VEC_FUNCTIONS(qc_program,   prog_section_function,  functions)
107 MEM_VEC_FUNCTIONS(qc_program,   char,                   strings)
108 _MEM_VEC_FUN_APPEND(qc_program, char,                   strings)
109 _MEM_VEC_FUN_RESIZE(qc_program, char,                   strings)
110 MEM_VEC_FUNCTIONS(qc_program,   qcint,                  globals)
111 MEM_VEC_FUNCTIONS(qc_program,   qcint,                  entitydata)
112
113 MEM_VEC_FUNCTIONS(qc_program,   qcint,         localstack)
114 _MEM_VEC_FUN_APPEND(qc_program, qcint,         localstack)
115 _MEM_VEC_FUN_RESIZE(qc_program, qcint,         localstack)
116 MEM_VEC_FUNCTIONS(qc_program,   qc_exec_stack, stack)
117
118 MEM_VEC_FUNCTIONS(qc_program,   size_t, profile)
119 _MEM_VEC_FUN_RESIZE(qc_program, size_t, profile)
120
121 qc_program* prog_load(const char *filename);
122 void        prog_delete(qc_program *prog);
123
124 bool prog_exec(qc_program *prog, prog_section_function *func, size_t flags, long maxjumps);
125
126 char*             prog_getstring (qc_program *prog, qcint str);
127 prog_section_def* prog_entfield  (qc_program *prog, qcint off);
128 prog_section_def* prog_getdef    (qc_program *prog, qcint off);
129 qcany*            prog_getedict  (qc_program *prog, qcint e);
130 qcint             prog_tempstring(qc_program *prog, const char *_str);
131
132 #endif /* GMQCC_EXEC_HDR */