]> git.xonotic.org Git - xonotic/gmqcc.git/blobdiff - code.c
Fixes to README
[xonotic/gmqcc.git] / code.c
diff --git a/code.c b/code.c
index b796b1319937b8a5276cc43263738d18d9eda082..c2ae9c8bf41f811e3917545bc18045e52e54c3f7 100644 (file)
--- a/code.c
+++ b/code.c
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  * SOFTWARE.
  */
-#include <stdint.h>
-#include <stdlib.h>
 #include "gmqcc.h"
 
-typedef struct {
-       uint16_t opcode;
-       
-       /* operand 1 */
-       union {
-               int16_t  s1; /* signed   */
-               uint16_t u1; /* unsigned */
-       };
-       /* operand 2 */
-       union {
-               int16_t  s2; /* signed   */
-               uint16_t u2; /* unsigned */
-       };
-       /* operand 3 */
-       union {
-               int16_t  s3; /* signed   */
-               uint16_t u3; /* unsigned */
-       };
-       
-       /*
-        * This is the same as the structure in darkplaces
-        * {
-        *     unsigned short op;
-        *     short          a,b,c;
-        * }
-        * But this one is more sane to work with, and the
-        * type sizes are guranteed.
-        */
-} prog_section_statement;
-
-typedef struct {
-       /* The type is (I assume)
-        * 0 = ev_void
-        * 1 = ev_string
-        * 2 = ev_float
-        * 3 = ev_vector
-        * 4 = ev_entity
-        * 5 = ev_field
-        * 6 = ev_function
-        * 7 = ev_pointer
-        * 8 = ev_bad    (is this right for uint16_t type?)
-        */
-       uint16_t type;
-       uint16_t offset;     /* offset in file? (what about length)   */
-       uint32_t name;       /* offset in string table? (confused :() */
-} prog_section_both;
-
-/*
- * var and field use the same structure.  But lets not use the same
- * name just for safety reasons?  (still castable ...).
- */
-typedef prog_section_both prog_section_def;
-typedef prog_section_both prog_section_field;
-
-typedef struct {
-       int32_t   entry;      /* in statement table for instructions  */
-       uint32_t  firstlocal; /* First local in local table           */
-       uint32_t  locals;     /* Total ints of params + locals        */
-       uint32_t  profile;    /* Always zero (engine uses this)       */
-       uint32_t  name;       /* name of function in string table     */
-       uint32_t  file;       /* file of the source file              */
-       uint32_t  nargs;      /* number of arguments                  */
-       uint8_t   argsize[8]; /* size of arguments (keep 8 always?)   */
-} prog_section_function;
-
 typedef struct {
        uint32_t offset;      /* Offset in file of where data begins  */
        uint32_t length;      /* Length of section (how many of)      */
@@ -147,17 +80,26 @@ VECTOR_MAKE(prog_section_function,  code_functions );
 VECTOR_MAKE(int,                    code_globals   );
 VECTOR_MAKE(char,                   code_strings   );
 
-/* program header */
-prog_header code_header;
-void code_write() {
-       
-       #if 0
-       /* Add test program */
-       code_strings_add('\0');
-       
-               const char *X;
-               size_t size = sizeof(X);
-               size_t iter = 0;
+void code_init() {
+       /*
+        * The way progs.dat is suppose to work is odd, there needs to be
+        * some null (empty) statements, functions, and 28 globals
+        */
+       prog_section_function  empty_function  = {0,0,0,0,0,0,0,{0}};
+       prog_section_statement empty_statement = {0,{0},{0},{0}};
+       int i;
+       for(i = 0; i < 28; i++)
+               code_globals_add(0);
+               
+       code_strings_add   ('\0');
+       code_functions_add (empty_function);
+       code_statements_add(empty_statement);
+}
+
+void code_test() {
+       const char *X;
+       size_t size = sizeof(X);
+       size_t iter = 0;
        
        #define FOO(Y) \
                X = Y; \
@@ -174,10 +116,6 @@ void code_write() {
        FOO("m_toggle");
        FOO("m_shutdown");
        
-       int i;
-       for (i=0; i<28; i++)
-               code_globals_add(0); /* 28 empty */
-       
        code_globals_add(1);  /* m_init */
        code_globals_add(2);  /* print  */
        code_globals_add(14); /* hello world in string table */
@@ -187,7 +125,6 @@ void code_write() {
        code_defs_add((prog_section_def){.type=TYPE_FUNCTION,.offset=29/*globals[29]*/, .name=8 }); /* print  */
        code_defs_add((prog_section_def){.type=TYPE_STRING,  .offset=30/*globals[30]*/, .name=14}); /*hello_world*/
        
-       code_functions_add((prog_section_function){0,  0, 0, 0, .name=0, 0, 0, {0}}); /* NULL */
        code_functions_add((prog_section_function){1,  0, 0, 0, .name=1, 0, 0, {0}}); /* m_init */
        code_functions_add((prog_section_function){-4, 0, 0, 0, .name=8, 0, 0, {0}}); /* print  */
        code_functions_add((prog_section_function){0,  0, 0, 0, .name=14+13,        0,0, {0}}); /* m_keydown */
@@ -195,11 +132,13 @@ void code_write() {
        code_functions_add((prog_section_function){0,  0, 0, 0, .name=14+13+10+7,   0,0, {0}});
        code_functions_add((prog_section_function){0,  0, 0, 0, .name=14+13+10+7+9, 0,0, {0}});
        
-       code_statements_add((prog_section_statement){0, 0, 0, 0}); /* NULL */
-       code_statements_add((prog_section_statement){INSTR_STORE_F, 30/*30 is hello_world */, OFS_PARM0, 0});
-       code_statements_add((prog_section_statement){INSTR_CALL1,   29/*29 is print       */, 0,         0});
-       code_statements_add((prog_section_statement){INSTR_RETURN,  0,                        0,         0});
-       
+       code_statements_add((prog_section_statement){INSTR_STORE_F, {30}/*30 is hello_world */, {OFS_PARM0}, {0}});
+       code_statements_add((prog_section_statement){INSTR_CALL1,   {29}/*29 is print       */, {0},         {0}});
+       code_statements_add((prog_section_statement){INSTR_RETURN,  {0},                        {0},         {0}});
+}
+
+void code_write() {
+       prog_header code_header={0};
        code_header.version    = 6;
        code_header.crc16      = 0; /* TODO: */
        code_header.statements = (prog_section){sizeof(prog_header), code_statements_elements };
@@ -209,10 +148,7 @@ void code_write() {
        code_header.globals    = (prog_section){code_header.functions.offset  + sizeof(prog_section_function) *code_functions_elements,   code_globals_elements    };
        code_header.strings    = (prog_section){code_header.globals.offset    + sizeof(int)                   *code_globals_elements,     code_strings_elements    };
        code_header.entfield   = 0; /* TODO: */
-       #endif
-
-
-       /* write out everything one SHOT! */
+       
        FILE *fp = fopen("program.dat", "wb");
        fwrite(&code_header,         1, sizeof(prog_header), fp);
        fwrite(code_statements_data, 1, sizeof(prog_section_statement)*code_statements_elements, fp);
@@ -229,5 +165,50 @@ void code_write() {
        free(code_globals_data);
        free(code_strings_data);
        
+       util_debug("CODE","wrote program.dat\n\
+    version:    = %d\n\
+    crc16:      = %d\n\
+    statements {\n\
+        .offset = %d\n\
+        .length = %d\n\
+    }\n\
+    defs       {\n\
+        .offset = %d\n\
+        .length = %d\n\
+    }\n\
+    fields     {\n\
+        .offset = %d\n\
+        .length = %d\n\
+    }\n\
+    functions  {\n\
+        .offset = %d\n\
+        .length = %d\n\
+    }\n\
+    globals    {\n\
+        .offset = %d\n\
+        .length = %d\n\
+    }\n\
+    strings    {\n\
+        .offset = %d\n\
+        .length = %d\n\
+    }\n\
+    entfield:   = %d\n",
+               code_header.version,
+               code_header.crc16,
+               code_header.statements.offset,
+               code_header.statements.length,
+               code_header.defs.offset,
+               code_header.defs.length,
+               code_header.fields.offset,
+               code_header.fields.length,
+               code_header.functions.offset,
+               code_header.functions.length,
+               code_header.strings.offset,
+               code_header.strings.length,
+               code_header.globals.offset,
+               code_header.globals.length,
+               code_header.entfield
+       );
+       
        fclose(fp);
 }