]> git.xonotic.org Git - xonotic/gmqcc.git/blob - code.c
Fix memory leak
[xonotic/gmqcc.git] / code.c
1 /*
2  * Copyright (C) 2012 
3  *      Dale Weiler
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 #include <stdint.h>
24 #include <stdlib.h>
25 #include "gmqcc.h"
26
27 typedef struct {
28         uint16_t opcode;
29         
30         /* operand 1 */
31         union {
32                 int16_t  s1; /* signed   */
33                 uint16_t u1; /* unsigned */
34         };
35         /* operand 2 */
36         union {
37                 int16_t  s2; /* signed   */
38                 uint16_t u2; /* unsigned */
39         };
40         /* operand 3 */
41         union {
42                 int16_t  s3; /* signed   */
43                 uint16_t u3; /* unsigned */
44         };
45         
46         /*
47          * This is the same as the structure in darkplaces
48          * {
49          *     unsigned short op;
50          *     short          a,b,c;
51          * }
52          * But this one is more sane to work with, and the
53          * type sizes are guranteed.
54          */
55 } prog_section_statement;
56
57 typedef struct {
58         /* The type is (I assume)
59          * 0 = ev_void
60          * 1 = ev_string
61          * 2 = ev_float
62          * 3 = ev_vector
63          * 4 = ev_entity
64          * 5 = ev_field
65          * 6 = ev_function
66          * 7 = ev_pointer
67          * 8 = ev_bad    (is this right for uint16_t type?)
68          */
69         uint16_t type;
70         uint16_t offset;     /* offset in file? (what about length)   */
71         uint32_t name;       /* offset in string table? (confused :() */
72 } prog_section_both;
73
74 /*
75  * var and field use the same structure.  But lets not use the same
76  * name just for safety reasons?  (still castable ...).
77  */
78 typedef prog_section_both prog_section_var;
79 typedef prog_section_both prog_section_field;
80
81 typedef struct {
82         int32_t   entry;      /* in statement table for instructions  */
83         uint32_t  args;       /* What is this?                        */
84         uint32_t  locals;     /* Total ints of params + locals        */
85         uint32_t  profile;    /* What is this?                        */
86         uint32_t  name;       /* name of function in string table     */
87         uint32_t  nargs;      /* number of arguments                  */
88         uint8_t   argsize[8]; /* size of arguments (keep 8 always?)   */
89 } prog_section_function;
90
91 typedef struct {
92         uint32_t offset;      /* Offset in file of where data begins  */
93         uint32_t length;      /* Length of section (how many of)      */
94 } prog_section;
95
96 typedef struct {
97         uint32_t     version;      /* Program version (6)     */
98         uint32_t     crc16;        /* What is this?           */
99         prog_section statements;   /* prog_section_statement  */
100         prog_section vars;         /* prog_section_var        */
101         prog_section fields;       /* prog_section_field      */
102         prog_section functions;    /* prog_section_function   */
103         prog_section strings;      /* What is this?           */
104         prog_section globals;      /* What is this?           */
105         uint32_t     entfield;     /* Number of entity fields */
106 } prog_header;
107
108 /*
109  * The macros below expand to a typesafe vector implementation, which
110  * can be viewed in gmqcc.h
111  * 
112  * code_statements_data      -- raw prog_section_statement array
113  * code_statements_elements  -- number of elements
114  * code_statements_allocated -- size of the array allocated
115  * code_statements_add(T)    -- add element (returns -1 on error)
116  * 
117  * code_vars_data            -- raw prog_section_var array
118  * code_vars_elements        -- number of elements
119  * code_vars_allocated       -- size of the array allocated
120  * code_vars_add(T)          -- add element (returns -1 on error)
121  * 
122  * code_fields_data          -- raw prog_section_field array
123  * code_fields_elements      -- number of elements
124  * code_fields_allocated     -- size of the array allocated
125  * code_fields_add(T)        -- add element (returns -1 on error)
126  *
127  * code_functions_data       -- raw prog_section_function array
128  * code_functions_elements   -- number of elements
129  * code_functions_allocated  -- size of the array allocated
130  * code_functions_add(T)     -- add element (returns -1 on error)
131  * 
132  * code_globals_data         -- raw prog_section_var array
133  * code_globals_elements     -- number of elements
134  * code_globals_allocated    -- size of the array allocated
135  * code_globals_add(T)       -- add element (returns -1 on error)
136  * 
137  * code_strings_data         -- raw char* array
138  * code_strings_elements     -- number of elements
139  * code_strings_allocated    -- size of the array allocated
140  * code_strings_add(T)       -- add element (returns -1 on error)
141  */
142 VECTOR_MAKE(prog_section_statement, code_statements);
143 VECTOR_MAKE(prog_section_var,       code_vars      );
144 VECTOR_MAKE(prog_section_field,     code_fields    );
145 VECTOR_MAKE(prog_section_function,  code_functions );
146 VECTOR_MAKE(prog_section_var,       code_globals   );
147 VECTOR_MAKE(char*,                  code_strings   );
148
149 /* program header */
150 prog_header code_header;
151 void code_write() {
152         code_header.version    = 6;
153         code_header.crc16      = 0; /* TODO: */
154         code_header.statements = (prog_section){sizeof(prog_header),                                                          code_statements_elements };
155         code_header.vars       = (prog_section){sizeof(prog_header)+sizeof(prog_section_statement)*code_statements_elements,  code_vars_elements       };
156         code_header.fields     = (prog_section){sizeof(prog_header)+sizeof(prog_section_var)      *code_vars_elements,        code_fields_elements     };
157         code_header.functions  = (prog_section){sizeof(prog_header)+sizeof(prog_section_field)    *code_fields_elements,      code_functions_elements  };
158         code_header.globals    = (prog_section){sizeof(prog_header)+sizeof(prog_section_function) *code_functions_elements,   code_globals_elements    };
159         /* how, I think I don't have strings figured out yet :| */
160         code_header.entfield   = 0; /* TODO: */
161         
162         #if 0 /* is this right? */
163         fwrite(&code_header,         1, sizeof(prog_header), fp);
164         fwrite(code_statements_data, 1, sizeof(prog_section_statement)*code_statements_elements, fp);
165         fwrite(code_vars_data,       1, sizeof(prog_section_var)      *code_vars_elements,       fp);
166         fwrite(code_fields_data,     1, sizeof(prog_section_field)    *code_fields_elements,     fp);
167         fwrite(code_functions_data,  1, sizeof(prog_section_function) *code_functions_elements,  fp);
168         fwrite(code_globals_data,    1, sizeof(prog_section_var)      *code_globals_elements,    fp);
169         #endif
170         
171         free(code_statements_data);
172         free(code_vars_data);
173         free(code_fields_data);
174         free(code_functions_data);
175         free(code_globals_data);
176         free(code_strings_data);
177 }