]> git.rm.cloudns.org Git - xonotic/gmqcc.git/commitdiff
It does hello world
authorDale Weiler <killfieldengine@gmail.com>
Wed, 11 Apr 2012 12:40:17 +0000 (08:40 -0400)
committerDale Weiler <killfieldengine@gmail.com>
Wed, 11 Apr 2012 12:40:17 +0000 (08:40 -0400)
code.c
gmqcc.h
main.c

diff --git a/code.c b/code.c
index ecea1dd587787ca3b3dd6cb383a611f059ed1e63..329730154e9896933f1ff09b48db2af97510d51e 100644 (file)
--- a/code.c
+++ b/code.c
@@ -75,15 +75,16 @@ typedef struct {
  * 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_var;
+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  args;       /* What is this?                        */
+       uint32_t  firstlocal; /* First local in local table           */
        uint32_t  locals;     /* Total ints of params + locals        */
-       uint32_t  profile;    /* What is this?                        */
+       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;
@@ -97,7 +98,7 @@ typedef struct {
        uint32_t     version;      /* Program version (6)     */
        uint32_t     crc16;        /* What is this?           */
        prog_section statements;   /* prog_section_statement  */
-       prog_section vars;         /* prog_section_var        */
+       prog_section defs;         /* prog_section_def        */
        prog_section fields;       /* prog_section_field      */
        prog_section functions;    /* prog_section_function   */
        prog_section strings;      /* What is this?           */
@@ -129,7 +130,7 @@ typedef struct {
  * code_functions_allocated  -- size of the array allocated
  * code_functions_add(T)     -- add element (returns -1 on error)
  * 
- * code_globals_data         -- raw prog_section_var array
+ * code_globals_data         -- raw prog_section_def array
  * code_globals_elements     -- number of elements
  * code_globals_allocated    -- size of the array allocated
  * code_globals_add(T)       -- add element (returns -1 on error)
@@ -140,55 +141,90 @@ typedef struct {
  * code_strings_add(T)       -- add element (returns -1 on error)
  */
 VECTOR_MAKE(prog_section_statement, code_statements);
-VECTOR_MAKE(prog_section_var,       code_vars      );
+VECTOR_MAKE(prog_section_def,       code_defs      );
 VECTOR_MAKE(prog_section_field,     code_fields    );
 VECTOR_MAKE(prog_section_function,  code_functions );
-VECTOR_MAKE(prog_section_var,       code_globals   );
-VECTOR_MAKE(char*,                  code_strings   );
+VECTOR_MAKE(int,                    code_globals   );
+VECTOR_MAKE(char                  code_strings   );
 
 /* program header */
 prog_header code_header;
 void code_write() {
        
        /* Add test program */
-       code_strings_add(NULL);           /* from my understanding str 0 = NULL (always!) */
-       code_strings_add("test program"); /* whoo a test program :3                       */
-       code_statements_add((prog_section_statement){INSTR_ADD_F, 1, 2, OFS_RETURN});
-       code_statements_add((prog_section_statement){INSTR_DONE,  0, 0, 0});
-       code_functions_add ((prog_section_function) {
-               .entry   = 0,
-               .args    = 0,
-               .locals  = 0,
-               .profile = 0,
-               .name    = 1, /*0 in string table is NULL always */
-               .nargs   = 0, /* CALL0 (no args) */
-               .argsize = (uint8_t*){0,0,0,0,0,0,0,0}
-       });
+       code_strings_add('\0');
+       
+               const char *X;
+               size_t size = sizeof(X);
+               size_t iter = 0;
+       
+       #define FOO(Y) \
+               X = Y; \
+               size = sizeof(Y); \
+               for (iter=0; iter < size; iter++) { \
+                       code_strings_add(X[iter]);    \
+               }
+               
+       FOO("m_init");
+       FOO("print");
+       FOO("hello world\n");
+       FOO("m_keydown");
+       FOO("m_draw");
+       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 */
+       
+       /* now the defs */
+       code_defs_add((prog_section_def){.type=TYPE_VOID,    .offset=28/*globals[28]*/, .name=1 }); /* m_init */
+       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){-2, 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 */
+       code_functions_add((prog_section_function){0,  0, 0, 0, .name=14+13+10,     0,0, {0}});
+       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_header.version    = 6;
        code_header.crc16      = 0; /* TODO: */
-       code_header.statements = (prog_section){sizeof(prog_header),                                                          code_statements_elements };
-       code_header.vars       = (prog_section){sizeof(prog_header)+sizeof(prog_section_statement)*code_statements_elements,  code_vars_elements       };
-       code_header.fields     = (prog_section){sizeof(prog_header)+sizeof(prog_section_var)      *code_vars_elements,        code_fields_elements     };
-       code_header.functions  = (prog_section){sizeof(prog_header)+sizeof(prog_section_field)    *code_fields_elements,      code_functions_elements  };
-       code_header.globals    = (prog_section){sizeof(prog_header)+sizeof(prog_section_function) *code_functions_elements,   code_globals_elements    };
-       /* how, I think I don't have strings figured out yet :| */
+       code_header.statements = (prog_section){sizeof(prog_header), code_statements_elements };
+       code_header.defs       = (prog_section){code_header.statements.offset + sizeof(prog_section_statement)*code_statements_elements,  code_defs_elements       };
+       code_header.fields     = (prog_section){code_header.defs.offset       + sizeof(prog_section_def)      *code_defs_elements,        code_fields_elements     };
+       code_header.functions  = (prog_section){code_header.fields.offset     + sizeof(prog_section_field)    *code_fields_elements,      code_functions_elements  };
+       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: */
        
-       #if 0 /* is this right? */
+
+       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);
-       fwrite(code_vars_data,       1, sizeof(prog_section_var)      *code_vars_elements,       fp);
+       fwrite(code_defs_data,       1, sizeof(prog_section_def)      *code_defs_elements,       fp);
        fwrite(code_fields_data,     1, sizeof(prog_section_field)    *code_fields_elements,     fp);
        fwrite(code_functions_data,  1, sizeof(prog_section_function) *code_functions_elements,  fp);
-       fwrite(code_globals_data,    1, sizeof(prog_section_var)      *code_globals_elements,    fp);
-       fwrite(
-       #endif
+       fwrite(code_globals_data,    1, sizeof(int)                   *code_globals_elements,    fp);
+       fwrite(code_strings_data,    1, 1                             *code_strings_elements,    fp);
        
        free(code_statements_data);
-       free(code_vars_data);
+       free(code_defs_data);
        free(code_fields_data);
        free(code_functions_data);
        free(code_globals_data);
        free(code_strings_data);
+       
+       fclose(fp);
 }
diff --git a/gmqcc.h b/gmqcc.h
index 26f0d1041b57de252364a209d79f86f4b16b86dd..e95e42860f729b1b92e18484df2039aea09cdcc1 100644 (file)
--- a/gmqcc.h
+++ b/gmqcc.h
@@ -178,69 +178,84 @@ char *util_strdup  (const char *);
  * These are the external instructions supported by the interperter
  * this is what things compile to (from the C code).
  */
-#define INSTR_DONE      0
-// math
-#define INSTR_MUL_F     1
-#define INSTR_MUL_V     2
-#define INSTR_MUL_FV    3
-#define INSTR_MUL_VF    4
-#define INSTR_DIV_F     5
-#define INSTR_ADD_F     6
-#define INSTR_ADD_V     7
-#define INSTR_SUB_F     8
-#define INSTR_SUB_V     9
-// compare
-#define INSTR_EQ_F      10
-#define INSTR_EQ_V      11
-#define INSTR_EQ_S      12
-#define INSTR_EQ_E      13
-#define INSTR_EQ_FNC    14
-#define INSTR_NE_F      15
-#define INSTR_NE_V      16
-#define INSTR_NE_S      17
-#define INSTR_NE_E      18
-#define INSTR_NE_FNC    19
-// multi compare
-#define INSTR_LE        20
-#define INSTR_GE        21
-#define INSTR_LT        22
-#define INSTR_GT        23
-// load and store
-#define INSTR_LOAD_F    24
-#define INSTR_LOAD_V    25
-#define INSTR_LOAD_S    26
-#define INSTR_LOAD_ENT  27
-#define INSTR_LOAD_FLD  28
-#define INSTR_LOAD_FNC  29
-#define INSTR_STORE_F   31
-#define INSTR_STORE_V   32
-#define INSTR_STORE_S   33
-#define INSTR_STORE_ENT 34
-#define INSTR_STORE_FLD 35
-#define INSTR_STORE_FNC 36
-// others
-#define INSTR_ADDRESS   30
-#define INSTR_RETURN    37
-#define INSTR_NOT_F     38
-#define INSTR_NOT_V     39
-#define INSTR_NOT_S     40
-#define INSTR_NOT_ENT   41
-#define INSTR_NOT_FNC   42
-#define INSTR_IF        43
-#define INSTR_IFNOT     44
-#define INSTR_CALL0     45
-#define INSTR_CALL1     46
-#define INSTR_CALL2     47
-#define INSTR_CALL3     48
-#define INSTR_CALL4     49
-#define INSTR_CALL5     50
-#define INSTR_CALL6     51
-#define INSTR_CALL7     52
-#define INSTR_CALL8     53
-#define INSTR_STATE     54
-#define INSTR_GOTO      55
-#define INSTR_AND       56
-#define INSTR_OR        57
-#define INSTR_BITAND    59
-#define INSTR_BITOR     60
+enum {
+       INSTR_DONE,
+       INSTR_MUL_F,
+       INSTR_MUL_V,
+       INSTR_MUL_FV,
+       INSTR_MUL_VF,
+       INSTR_DIV_F,
+       INSTR_ADD_F,
+       INSTR_ADD_V,
+       INSTR_SUB_F,
+       INSTR_SUB_V,
+       
+       INSTR_EQ_F,
+       INSTR_EQ_V,
+       INSTR_EQ_S,
+       INSTR_EQ_E,
+       INSTR_EQ_FNC,
+       
+       INSTR_NE_F,
+       INSTR_NE_V,
+       INSTR_NE_S,
+       INSTR_NE_E,
+       INSTR_NE_FNC,
+       
+       INSTR_LE,
+       INSTR_GE,
+       INSTR_LT,
+       INSTR_GT,
+
+       INSTR_LOAD_F,
+       INSTR_LOAD_V,
+       INSTR_LOAD_S,
+       INSTR_LOAD_ENT,
+       INSTR_LOAD_FLD,
+       INSTR_LOAD_FNC,
+
+       INSTR_ADDRESS,
+
+       INSTR_STORE_F,
+       INSTR_STORE_V,
+       INSTR_STORE_S,
+       INSTR_STORE_ENT,
+       INSTR_STORE_FLD,
+       INSTR_STORE_FNC,
+
+       INSTR_STOREP_F,
+       INSTR_STOREP_V,
+       INSTR_STOREP_S,
+       INSTR_STOREP_ENT,
+       INSTR_STOREP_FLD,
+       INSTR_STOREP_FNC,
+
+       INSTR_RETURN,
+       INSTR_NOT_F,
+       INSTR_NOT_V,
+       INSTR_NOT_S,
+       INSTR_NOT_ENT,
+       INSTR_NOT_FNC,
+       INSTR_IF,
+       INSTR_IFNOT,
+       INSTR_CALL0,
+       INSTR_CALL1,
+       INSTR_CALL2,
+       INSTR_CALL3,
+       INSTR_CALL4,
+       INSTR_CALL5,
+       INSTR_CALL6,
+       INSTR_CALL7,
+       INSTR_CALL8,
+       INSTR_STATE,
+       INSTR_GOTO,
+       INSTR_AND,
+       INSTR_OR,
+       
+       INSTR_BITAND,
+       INSTR_BITOR
+};
+
+
+void code_write();
 #endif
diff --git a/main.c b/main.c
index 94e4e82d38ae696ae484d43653e15d536ac338d5..8aa73a0ad4b1ef9f89463cc0a97064f1d57c04b1 100644 (file)
--- a/main.c
+++ b/main.c
@@ -44,5 +44,7 @@ int main(int argc, char **argv) {
                parse_tree(lex);
                lex_close (lex);
        }
+       
+       code_write();
        return 0;
 }