+/*
+ * The symbols below are created by the following
+ * expanded macros:
+ *
+ * VECTOR_MAKE(prog_section_statement, code_statements);
+ * VECTOR_MAKE(prog_section_def, code_defs );
+ * VECTOR_MAKE(prog_section_field, code_fields );
+ * VECTOR_MAKE(prog_section_function, code_functions );
+ * VECTOR_MAKE(int, code_globals );
+ * VECTOR_MAKE(char, code_chars );
+ */
+int code_statements_add(prog_section_statement);
+int code_defs_add (prog_section_def);
+int code_fields_add (prog_section_field);
+int code_functions_add (prog_section_function);
+int code_globals_add (int);
+int code_chars_add (char);
+int code_statements_put(prog_section_statement*, size_t);
+int code_defs_put (prog_section_def*, size_t);
+int code_fields_put (prog_section_field*, size_t);
+int code_functions_put (prog_section_function*, size_t);
+int code_globals_put (int*, size_t);
+int code_chars_put (char*, size_t);
+extern long code_statements_elements;
+extern long code_chars_elements;
+extern long code_globals_elements;
+extern long code_functions_elements;
+extern long code_fields_elements;
+extern long code_defs_elements;
+
+/*
+ * code_write -- writes out the compiled file
+ * code_init -- prepares the code file
+ */
+void code_write ();
+void code_init ();
+
+//===================================================================
+//========================= assembler.c =============================
+//===================================================================
+static const struct {
+ const char *m; /* menomic */
+ const size_t o; /* operands */
+ const size_t l; /* menomic len */
+} const asm_instr[] = {
+ [INSTR_DONE] = { "DONE" , 1, 4 },
+ [INSTR_MUL_F] = { "MUL_F" , 3, 5 },
+ [INSTR_MUL_V] = { "MUL_V" , 3, 5 },
+ [INSTR_MUL_FV] = { "MUL_FV" , 3, 6 },
+ [INSTR_MUL_VF] = { "MUL_VF" , 3, 6 },
+ [INSTR_DIV_F] = { "DIV" , 0, 3 },
+ [INSTR_ADD_F] = { "ADD_F" , 3, 5 },
+ [INSTR_ADD_V] = { "ADD_V" , 3, 5 },
+ [INSTR_SUB_F] = { "SUB_F" , 3, 5 },
+ [INSTR_SUB_V] = { "DUB_V" , 3, 5 },
+ [INSTR_EQ_F] = { "EQ_F" , 0, 4 },
+ [INSTR_EQ_V] = { "EQ_V" , 0, 4 },
+ [INSTR_EQ_S] = { "EQ_S" , 0, 4 },
+ [INSTR_EQ_E] = { "EQ_E" , 0, 4 },
+ [INSTR_EQ_FNC] = { "ES_FNC" , 0, 6 },
+ [INSTR_NE_F] = { "NE_F" , 0, 4 },
+ [INSTR_NE_V] = { "NE_V" , 0, 4 },
+ [INSTR_NE_S] = { "NE_S" , 0, 4 },
+ [INSTR_NE_E] = { "NE_E" , 0, 4 },
+ [INSTR_NE_FNC] = { "NE_FNC" , 0, 6 },
+ [INSTR_LE] = { "LE" , 0, 2 },
+ [INSTR_GE] = { "GE" , 0, 2 },
+ [INSTR_LT] = { "LT" , 0, 2 },
+ [INSTR_GT] = { "GT" , 0, 2 },
+ [INSTR_LOAD_F] = { "FIELD_F" , 0, 7 },
+ [INSTR_LOAD_V] = { "FIELD_V" , 0, 7 },
+ [INSTR_LOAD_S] = { "FIELD_S" , 0, 7 },
+ [INSTR_LOAD_ENT] = { "FIELD_ENT" , 0, 9 },
+ [INSTR_LOAD_FLD] = { "FIELD_FLD" , 0, 9 },
+ [INSTR_LOAD_FNC] = { "FIELD_FNC" , 0, 9 },
+ [INSTR_ADDRESS] = { "ADDRESS" , 0, 7 },
+ [INSTR_STORE_F] = { "STORE_F" , 0, 7 },
+ [INSTR_STORE_V] = { "STORE_V" , 0, 7 },
+ [INSTR_STORE_S] = { "STORE_S" , 0, 7 },
+ [INSTR_STORE_ENT] = { "STORE_ENT" , 0, 9 },
+ [INSTR_STORE_FLD] = { "STORE_FLD" , 0, 9 },
+ [INSTR_STORE_FNC] = { "STORE_FNC" , 0, 9 },
+ [INSTR_STOREP_F] = { "STOREP_F" , 0, 8 },
+ [INSTR_STOREP_V] = { "STOREP_V" , 0, 8 },
+ [INSTR_STOREP_S] = { "STOREP_S" , 0, 8 },
+ [INSTR_STOREP_ENT] = { "STOREP_ENT", 0, 10},
+ [INSTR_STOREP_FLD] = { "STOREP_FLD", 0, 10},
+ [INSTR_STOREP_FNC] = { "STOREP_FNC", 0, 10},
+ [INSTR_RETURN] = { "RETURN" , 0, 6 },
+ [INSTR_NOT_F] = { "NOT_F" , 0, 5 },
+ [INSTR_NOT_V] = { "NOT_V" , 0, 5 },
+ [INSTR_NOT_S] = { "NOT_S" , 0, 5 },
+ [INSTR_NOT_ENT] = { "NOT_ENT" , 0, 7 },
+ [INSTR_NOT_FNC] = { "NOT_FNC" , 0, 7 },
+ [INSTR_IF] = { "IF" , 0, 2 },
+ [INSTR_IFNOT] = { "IFNOT" , 0, 5 },
+ [INSTR_CALL0] = { "CALL0" , 0, 5 },
+ [INSTR_CALL1] = { "CALL1" , 0, 5 },
+ [INSTR_CALL2] = { "CALL2" , 0, 5 },
+ [INSTR_CALL3] = { "CALL3" , 0, 5 },
+ [INSTR_CALL4] = { "CALL4" , 0, 5 },
+ [INSTR_CALL5] = { "CALL5" , 0, 5 },
+ [INSTR_CALL6] = { "CALL6" , 0, 5 },
+ [INSTR_CALL7] = { "CALL7" , 0, 5 },
+ [INSTR_CALL8] = { "CALL8" , 0, 5 },
+ [INSTR_STATE] = { "STATE" , 0, 5 },
+ [INSTR_GOTO] = { "GOTO" , 0, 4 },
+ [INSTR_AND] = { "AND" , 0, 3 },
+ [INSTR_OR] = { "OR" , 0, 2 },
+ [INSTR_BITAND] = { "BITAND" , 0, 6 },
+ [INSTR_BITOR] = { "BITOR" , 0, 5 }
+};
+
+void asm_init (const char *, FILE **);
+void asm_close(FILE *);
+void asm_parse(FILE *);
+//======================================================================
+//============================= main.c =================================
+//======================================================================
+enum {
+ COMPILER_QCC, /* circa QuakeC */
+ COMPILER_FTEQCC, /* fteqcc QuakeC */
+ COMPILER_QCCX, /* qccx QuakeC */
+ COMPILER_GMQCC /* this QuakeC */
+};
+extern bool opts_debug;
+extern bool opts_memchk;
+extern bool opts_darkplaces_stringtablebug;
+extern bool opts_omit_nullcode;
+extern int opts_compiler;
+//======================================================================
+//============================= ast.c ==================================
+//======================================================================
+#define MEM_VECTOR_PROTO(Towner, Tmem, mem) \
+ bool GMQCC_WARN Towner##_##mem##_add(Towner*, Tmem); \
+ bool GMQCC_WARN Towner##_##mem##_remove(Towner*, size_t)
+
+#define MEM_VECTOR_PROTO_ALL(Towner, Tmem, mem) \
+ MEM_VECTOR_PROTO(Towner, Tmem, mem); \
+ bool GMQCC_WARN Towner##_##mem##_find(Towner*, Tmem, size_t*); \
+ void Towner##_##mem##_clear(Towner*)
+
+#define MEM_VECTOR_MAKE(Twhat, name) \
+ Twhat *name; \
+ size_t name##_count; \
+ size_t name##_alloc
+
+#define _MEM_VEC_FUN_ADD(Tself, Twhat, mem) \
+bool GMQCC_WARN Tself##_##mem##_add(Tself *self, Twhat f) \
+{ \
+ Twhat *reall; \
+ if (self->mem##_count == self->mem##_alloc) { \
+ if (!self->mem##_alloc) { \
+ self->mem##_alloc = 16; \
+ } else { \
+ self->mem##_alloc *= 2; \
+ } \
+ reall = (Twhat*)mem_a(sizeof(Twhat) * self->mem##_alloc); \
+ if (!reall) { \
+ return false; \
+ } \
+ memcpy(reall, self->mem, sizeof(Twhat) * self->mem##_count); \
+ mem_d(self->mem); \
+ self->mem = reall; \
+ } \
+ self->mem[self->mem##_count++] = f; \
+ return true; \
+}
+
+#define _MEM_VEC_FUN_REMOVE(Tself, Twhat, mem) \
+bool GMQCC_WARN Tself##_##mem##_remove(Tself *self, size_t idx) \
+{ \
+ size_t i; \
+ Twhat *reall; \
+ if (idx >= self->mem##_count) { \
+ return true; /* huh... */ \
+ } \
+ for (i = idx; i < self->mem##_count-1; ++i) { \
+ self->mem[i] = self->mem[i+1]; \
+ } \
+ self->mem##_count--; \
+ if (self->mem##_count < self->mem##_count/2) { \
+ self->mem##_alloc /= 2; \
+ reall = (Twhat*)mem_a(sizeof(Twhat) * self->mem##_count); \
+ if (!reall) { \
+ return false; \
+ } \
+ memcpy(reall, self->mem, sizeof(Twhat) * self->mem##_count); \
+ mem_d(self->mem); \
+ self->mem = reall; \
+ } \
+ return true; \
+}
+
+#define _MEM_VEC_FUN_FIND(Tself, Twhat, mem) \
+bool GMQCC_WARN Tself##_##mem##_find(Tself *self, Twhat obj, size_t *idx) \
+{ \
+ size_t i; \
+ for (i = 0; i < self->mem##_count; ++i) { \
+ if (self->mem[i] == obj) { \
+ if (idx) { \
+ *idx = i; \
+ } \
+ return true; \
+ } \
+ } \
+ return false; \
+}
+
+#define _MEM_VEC_FUN_CLEAR(Tself, mem) \
+void Tself##_##mem##_clear(Tself *self) \
+{ \
+ if (!self->mem) \
+ return; \
+ mem_d((void*) self->mem); \
+ self->mem = NULL; \
+ self->mem##_count = 0; \
+ self->mem##_alloc = 0; \
+}
+
+#define MEM_VECTOR_CLEAR(owner, mem) \
+ if ((owner)->mem) \
+ mem_d((void*)((owner)->mem)); \
+ (owner)->mem = NULL; \
+ (owner)->mem##_count = 0; \
+ (owner)->mem##_alloc = 0
+
+#define MEM_VECTOR_INIT(owner, mem) \
+{ \
+ (owner)->mem = NULL; \
+ (owner)->mem##_count = 0; \
+ (owner)->mem##_alloc = 0; \
+}
+
+#define MEM_VEC_FUNCTIONS(Tself, Twhat, mem) \
+_MEM_VEC_FUN_REMOVE(Tself, Twhat, mem) \
+_MEM_VEC_FUN_ADD(Tself, Twhat, mem)
+
+#define MEM_VEC_FUNCTIONS_ALL(Tself, Twhat, mem) \
+MEM_VEC_FUNCTIONS(Tself, Twhat, mem) \
+_MEM_VEC_FUN_CLEAR(Tself, mem) \
+_MEM_VEC_FUN_FIND(Tself, Twhat, mem)
+
+enum store_types {
+ store_global,
+ store_local, /* local, assignable for now, should get promoted later */
+ store_value, /* unassignable */
+};
+
+typedef struct {
+ float x, y, z;
+} vector;
+
+/*
+ * A shallow copy of a lex_file to remember where which ast node
+ * came from.
+ */
+typedef struct {
+ const char *file;
+ size_t line;
+} lex_ctx;