#ifndef GMQCC_HDR
#define GMQCC_HDR
+#include <vector>
+#include <string>
+#include <utility>
+#include <memory>
+#include <map>
+using std::move;
#include <stdarg.h>
#include <stddef.h>
#include <stdlib.h>
* us to use the array [] to access individual elements from the vector
* opposed to using set/get methods.
*/
-typedef struct {
+struct vector_t {
size_t allocated;
size_t used;
/* can be extended now! whoot */
-} vector_t;
+};
/* hidden interface */
void _util_vec_grow(void **a, size_t i, size_t s);
/* exposed interface */
#define vec_meta(A) ((vector_t*)(((char *)(A)) - sizeof(vector_t)))
-#define vec_free(A) ((void)((A) ? (_util_vec_delete((void *)(A)), (A) = NULL) : 0))
+#define vec_free(A) ((void)((A) ? (_util_vec_delete((void *)(A)), (A) = nullptr) : 0))
#define vec_push(A,V) (GMQCC_VEC_WILLGROW((A),1), (A)[vec_meta(A)->used++] = (V))
#define vec_size(A) ((A) ? vec_meta(A)->used : 0)
#define vec_add(A,N) (GMQCC_VEC_WILLGROW((A),(N)), vec_meta(A)->used += (N), &(A)[vec_meta(A)->used-(N)])
/* code.c */
/* Note: if you change the order, fix type_sizeof in ir.c */
-enum {
+enum qc_type {
TYPE_VOID ,
TYPE_STRING ,
TYPE_FLOAT ,
extern const uint16_t type_ne_instr [TYPE_COUNT];
extern const uint16_t type_not_instr [TYPE_COUNT];
-typedef struct {
+struct prog_section_t {
uint32_t offset; /* Offset in file of where data begins */
uint32_t length; /* Length of section (how many of) */
-} prog_section_t;
+};
-typedef struct {
+struct prog_header_t {
uint32_t version; /* Program version (6) */
uint16_t crc16;
uint16_t skip;
prog_section_t strings;
prog_section_t globals;
uint32_t entfield; /* Number of entity fields */
-} prog_header_t;
+};
/*
* Each paramater incerements by 3 since vector types hold
#define OFS_PARM6 (OFS_PARM5 +3)
#define OFS_PARM7 (OFS_PARM6 +3)
-typedef union {
+union operand_t {
int16_t s1;
uint16_t u1;
-} operand_t;
+};
-typedef struct {
+struct prog_section_statement_t {
uint16_t opcode;
operand_t o1;
operand_t o2;
operand_t o3;
-} prog_section_statement_t;
+};
-typedef struct {
+struct prog_section_both_t {
/*
* The types:
* 0 = ev_void
uint16_t type;
uint16_t offset;
uint32_t name;
-} prog_section_both_t;
+};
typedef prog_section_both_t prog_section_def_t;
typedef prog_section_both_t prog_section_field_t;
#define DEF_SAVEGLOBAL (1<<15)
#define DEF_TYPEMASK ((1<<15)-1)
-typedef struct {
+struct prog_section_function_t {
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 file; /* file of the source file */
int32_t nargs; /* number of arguments */
uint8_t argsize[8]; /* size of arguments (keep 8 always?) */
-} prog_section_function_t;
+};
/*
* Instructions
/* TODO: elide */
extern const char *util_instr_str[VINSTR_END];
-void util_swap_header (prog_header_t *code_header);
-void util_swap_statements (prog_section_statement_t *statements);
-void util_swap_defs_fields(prog_section_both_t *section);
-void util_swap_functions (prog_section_function_t *functions);
-void util_swap_globals (int32_t *globals);
+void util_swap_header(prog_header_t &code_header);
+void util_swap_statements(std::vector<prog_section_statement_t> &statements);
+void util_swap_defs_fields(std::vector<prog_section_both_t> §ion);
+void util_swap_functions(std::vector<prog_section_function_t> &functions);
+void util_swap_globals(std::vector<int32_t> &globals);
-typedef float qcfloat_t;
-typedef int32_t qcint_t;
+typedef float qcfloat_t;
+typedef int32_t qcint_t;
typedef uint32_t qcuint_t;
struct code_t {
- prog_section_statement_t *statements;
- int *linenums;
- int *columnnums;
- prog_section_def_t *defs;
- prog_section_field_t *fields;
- prog_section_function_t *functions;
- int *globals;
- char *chars;
- uint16_t crc;
- uint32_t entfields;
- ht string_cache;
- qcint_t string_cached_empty;
+ void* operator new(std::size_t);
+ void operator delete(void*);
+ code_t();
+ ~code_t();
+ std::vector<prog_section_statement_t> statements;
+ std::vector<int> linenums;
+ std::vector<int> columnnums;
+ std::vector<prog_section_def_t> defs;
+ std::vector<prog_section_field_t> fields;
+ std::vector<prog_section_function_t> functions;
+ std::vector<int> globals;
+ std::vector<char> chars;
+ uint16_t crc = 0;
+ uint32_t entfields = 0;
+ ht string_cache;
+ qcint_t string_cached_empty = 0;
};
/*
*/
struct lex_ctx_t {
const char *file;
- size_t line;
- size_t column;
+ size_t line;
+ size_t column;
};
/*
extern size_t compile_Werrors;
extern size_t compile_warnings;
-void /********/ compile_error (lex_ctx_t ctx, /*LVL_ERROR*/ const char *msg, ...);
+void /********/ compile_error_ (lex_ctx_t ctx, /*LVL_ERROR*/ const char *msg, ...);
void /********/ vcompile_error (lex_ctx_t ctx, /*LVL_ERROR*/ const char *msg, va_list ap);
-bool GMQCC_WARN compile_warning (lex_ctx_t ctx, int warntype, const char *fmt, ...);
+bool GMQCC_WARN compile_warning_(lex_ctx_t ctx, int warntype, const char *fmt, ...);
bool GMQCC_WARN vcompile_warning(lex_ctx_t ctx, int warntype, const char *fmt, va_list ap);
void compile_show_werrors(void);
+template <typename T>
+inline constexpr const T formatNormalize(const T argument) { return argument; }
+
+inline const char *formatNormalize(const std::string &argument) {
+ return argument.c_str();
+}
+
+template<typename... Ts>
+inline bool GMQCC_WARN compile_warning(lex_ctx_t ctx, int warntype, const char *fmt, const Ts&... ts) {
+ return compile_warning_(ctx, warntype, fmt, formatNormalize(ts)...);
+}
+template<typename... Ts>
+inline void /********/ compile_error (lex_ctx_t ctx, /*LVL_ERROR*/ const char *msg, const Ts&... ts) {
+ return compile_error_(ctx, msg, formatNormalize(ts)...);
+}
+
/* ir.c */
/* TODO: cleanup */
-enum store_types {
+enum store_type {
store_global,
store_local, /* local, assignable for now, should get promoted later */
store_param, /* parameters, they are locals with a fixed position */
store_return /* unassignable, at OFS_RETURN */
};
-typedef struct {
+struct vec3_t {
qcfloat_t x, y, z;
-} vec3_t;
+};
/* exec.c */
#define VMXF_TRACE 0x0001 /* trace: print statements before executing */
#define VMXF_PROFILE 0x0002 /* profile: increment the profile counters */
-struct qc_program_s;
-typedef int (*prog_builtin_t)(struct qc_program_s *prog);
+typedef struct qc_program qc_program_t;
+typedef int (*prog_builtin_t)(qc_program_t *prog);
-typedef struct {
- qcint_t stmt;
- size_t localsp;
+struct qc_exec_stack_t {
+ qcint_t stmt;
+ size_t localsp;
prog_section_function_t *function;
-} qc_exec_stack_t;
+};
+
+struct qc_program {
+ qc_program() = delete;
+ qc_program(const char *name, uint16_t crc, size_t entfields);
-typedef struct qc_program_s {
- char *filename;
- prog_section_statement_t *code;
- prog_section_def_t *defs;
- prog_section_def_t *fields;
- prog_section_function_t *functions;
- char *strings;
- qcint_t *globals;
- qcint_t *entitydata;
- bool *entitypool;
+ std::string filename;
+ std::vector<prog_section_statement_t> code;
+ std::vector<prog_section_def_t> defs;
+ std::vector<prog_section_def_t> fields;
+ std::vector<prog_section_function_t> functions;
+ std::vector<char> strings;
+ std::vector<qcint_t> globals;
+ std::vector<qcint_t> entitydata;
+ std::vector<bool> entitypool;
- const char* *function_stack;
+ std::vector<const char*> function_stack;
uint16_t crc16;
qcint_t vmerror;
- size_t *profile;
+ std::vector<size_t> profile;
prog_builtin_t *builtins;
size_t builtins_count;
size_t entityfields;
bool allowworldwrites;
- qcint_t *localstack;
- qc_exec_stack_t *stack;
+ std::vector<qcint_t> localstack;
+ std::vector<qc_exec_stack_t> stack;
size_t statement;
size_t xflags;
} cached_globals;
bool supports_state; /* is INSTR_STATE supported? */
-} qc_program_t;
+};
qc_program_t* prog_load (const char *filename, bool ignoreversion);
void prog_delete (qc_program_t *prog);
bool parser_compile_file(parser_t *parser, const char *);
bool parser_compile_string(parser_t *parser, const char *, const char *, size_t);
bool parser_finish(parser_t *parser, const char *);
-void parser_cleanup(parser_t *parser);
/* ftepp.c */
struct ftepp_t;
/* Helpers to allow for a whole lot of flags. Otherwise we'd limit
* to 32 or 64 -f options...
*/
-typedef struct {
+struct longbit {
size_t idx; /* index into an array of 32 bit words */
uint8_t bit; /* bit index for the 8 bit group idx points to */
-} longbit;
+};
#define LONGBIT(bit) { ((bit)/32), ((bit)%32) }
#define LONGBIT_SET(B, I) ((B).idx = (I)/32, (B).bit = ((I)%32))
#else
int utf8_to(utf8ch_t *, const unsigned char *, size_t);
/* opts.c */
-typedef struct {
+struct opts_flag_def_t {
const char *name;
longbit bit;
-} opts_flag_def_t;
+};
bool opts_setflag (const char *, bool);
bool opts_setwarn (const char *, bool);
extern unsigned int opts_optimizationcount[COUNT_OPTIMIZATIONS];
/* other options: */
-typedef enum {
+enum {
COMPILER_QCC, /* circa QuakeC */
COMPILER_FTEQCC, /* fteqcc QuakeC */
COMPILER_QCCX, /* qccx QuakeC */
COMPILER_GMQCC /* this QuakeC */
-} opts_std_t;
+};
-typedef struct {
+struct opt_value_t {
union {
- bool b;
+ bool b;
uint16_t u16;
uint32_t u32;
-
union {
- char *p;
+ char *p;
const char *c;
} str;
} data;
-
bool allocated;
-} opt_value_t;
-
+};
-typedef struct {
+struct opts_cmd_t {
opt_value_t options [OPTION_COUNT];
uint32_t flags [1 + (COUNT_FLAGS / 32)];
uint32_t warn [1 + (COUNT_WARNINGS / 32)];
uint32_t werror_backup[1 + (COUNT_WARNINGS / 32)];
uint32_t optimization [1 + (COUNT_OPTIMIZATIONS / 32)];
bool optimizeoff; /* True when -O0 */
-} opts_cmd_t;
+};
extern opts_cmd_t opts;