mirror of
https://github.com/DarkPlacesEngine/gmqcc.git
synced 2025-02-21 10:51:10 +00:00
Merge branch 'master' into blub/ast-and-ir-merging
-> Todo: take over bool from qbool
This commit is contained in:
commit
234567cb9f
5 changed files with 117 additions and 62 deletions
3
asm.c
3
asm.c
|
@ -86,6 +86,7 @@ int asm_parsetype(const char *key, char **skip, long line) {
|
|||
exit(1);
|
||||
}
|
||||
*skip += keylen+1;
|
||||
/* skip whitespace */
|
||||
while (**skip == ' ' || **skip == '\t')
|
||||
(*skip)++;
|
||||
|
||||
|
@ -139,7 +140,7 @@ void asm_parse(FILE *fp) {
|
|||
.nargs = 0,
|
||||
.argsize = {0}
|
||||
});
|
||||
code_strings_add(skip);
|
||||
code_chars_put(skip, strlen(skip));
|
||||
};
|
||||
|
||||
#if 0
|
||||
|
|
23
code.c
23
code.c
|
@ -83,15 +83,6 @@ VECTOR_MAKE(prog_section_function, code_functions );
|
|||
VECTOR_MAKE(int, code_globals );
|
||||
VECTOR_MAKE(char, code_chars );
|
||||
|
||||
int code_strings_add(const char *src) {
|
||||
size_t size = strlen(src);
|
||||
size_t iter = 0;
|
||||
while (iter < size)
|
||||
code_chars_add(src[iter++]);
|
||||
code_chars_add('\0');
|
||||
return code_chars_elements;
|
||||
}
|
||||
|
||||
void code_init() {
|
||||
/* omit creation of null code */
|
||||
if (opts_omit_nullcode)
|
||||
|
@ -113,13 +104,13 @@ void code_init() {
|
|||
}
|
||||
|
||||
void code_test() {
|
||||
code_strings_add("m_init");
|
||||
code_strings_add("print");
|
||||
code_strings_add("hello world\n");
|
||||
code_strings_add("m_keydown");
|
||||
code_strings_add("m_draw");
|
||||
code_strings_add("m_toggle");
|
||||
code_strings_add("m_shutdown");
|
||||
code_chars_put("m_init", 0x6);
|
||||
code_chars_put("print", 0x5);
|
||||
code_chars_put("hello world\n", 0xC);
|
||||
code_chars_put("m_keydown", 0x9);
|
||||
code_chars_put("m_draw", 0x6);
|
||||
code_chars_put("m_toggle", 0x8);
|
||||
code_chars_put("m_shutdown", 0xA);
|
||||
|
||||
code_globals_add(1); /* m_init */
|
||||
code_globals_add(2); /* print */
|
||||
|
|
117
gmqcc.h
117
gmqcc.h
|
@ -27,6 +27,30 @@
|
|||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <ctype.h>
|
||||
|
||||
/*
|
||||
* We cannoy rely on C99 at all, since compilers like MSVC
|
||||
* simply don't support it. We define our own boolean type
|
||||
* as a result (since we cannot include <stdbool.h>). For
|
||||
* compilers that are in 1999 mode (C99 compliant) we can use
|
||||
* the language keyword _Bool which can allow for better code
|
||||
* on GCC and GCC-like compilers, opposed to `int`.
|
||||
*/
|
||||
#ifndef __cplusplus
|
||||
# ifdef false
|
||||
# undef false
|
||||
# endif /* !false */
|
||||
# ifdef true
|
||||
# undef true
|
||||
# endif /* !true */
|
||||
# define false (0)
|
||||
# define true (1)
|
||||
# define bool _Bool
|
||||
# if __STDC_VERSION__ < 199901L && __GNUC__ < 3
|
||||
typedef int _Bool
|
||||
# endif
|
||||
# endif /* !__cplusplus */
|
||||
|
||||
/*
|
||||
* stdint.h and inttypes.h -less subset
|
||||
* for systems that don't have it, which we must
|
||||
|
@ -160,16 +184,16 @@ int typedef_add (struct lex_file *file, const char *, const char *);
|
|||
//===================================================================
|
||||
//=========================== util.c ================================
|
||||
//===================================================================
|
||||
void *util_memory_a (unsigned int, unsigned int, const char *);
|
||||
void util_memory_d (void *, unsigned int, const char *);
|
||||
void util_meminfo ();
|
||||
void *util_memory_a (unsigned int, unsigned int, const char *);
|
||||
void util_memory_d (void *, unsigned int, const char *);
|
||||
void util_meminfo ();
|
||||
|
||||
char *util_strdup (const char *);
|
||||
char *util_strrq (char *);
|
||||
char *util_strrnl (char *);
|
||||
void util_debug (const char *, const char *, ...);
|
||||
int util_getline (char **, size_t *, FILE *);
|
||||
void util_endianswap(void *, int, int);
|
||||
char *util_strdup (const char *);
|
||||
char *util_strrq (char *);
|
||||
char *util_strrnl (char *);
|
||||
void util_debug (const char *, const char *, ...);
|
||||
int util_getline (char **, size_t *, FILE *);
|
||||
void util_endianswap (void *, int, int);
|
||||
|
||||
uint32_t util_crc32(const char *, int, register const short);
|
||||
|
||||
|
@ -181,29 +205,44 @@ uint32_t util_crc32(const char *, int, register const short);
|
|||
# define mem_d(x) util_memory_d((x), __LINE__, __FILE__)
|
||||
#endif
|
||||
|
||||
#define VECTOR_MAKE(T,N) \
|
||||
T* N##_data = NULL; \
|
||||
long N##_elements = 0; \
|
||||
long N##_allocated = 0; \
|
||||
int N##_add(T element) { \
|
||||
if (N##_elements == N##_allocated) { \
|
||||
if (N##_allocated == 0) { \
|
||||
N##_allocated = 12; \
|
||||
} else { \
|
||||
N##_allocated *= 2; \
|
||||
} \
|
||||
void *temp = mem_a(N##_allocated * sizeof(T)); \
|
||||
if (!temp) { \
|
||||
mem_d(temp); \
|
||||
return -1; \
|
||||
} \
|
||||
memcpy(temp, N##_data, (N##_elements * sizeof(T))); \
|
||||
mem_d(N##_data); \
|
||||
N##_data = (T*)temp; \
|
||||
} \
|
||||
N##_data[N##_elements] = element; \
|
||||
return N##_elements++; \
|
||||
/* Builds vector type (usefull for inside structures) */
|
||||
#define VECTOR_TYPE(T,N) \
|
||||
T* N##_data = NULL; \
|
||||
long N##_elements = 0; \
|
||||
long N##_allocated = 0
|
||||
/* Builds vector add */
|
||||
#define VECTOR_CORE(T,N) \
|
||||
int N##_add(T element) { \
|
||||
if (N##_elements == N##_allocated) { \
|
||||
if (N##_allocated == 0) { \
|
||||
N##_allocated = 12; \
|
||||
} else { \
|
||||
N##_allocated *= 2; \
|
||||
} \
|
||||
void *temp = mem_a(N##_allocated * sizeof(T)); \
|
||||
if (!temp) { \
|
||||
mem_d(temp); \
|
||||
return -1; \
|
||||
} \
|
||||
memcpy(temp, N##_data, (N##_elements * sizeof(T))); \
|
||||
mem_d(N##_data); \
|
||||
N##_data = (T*)temp; \
|
||||
} \
|
||||
N##_data[N##_elements] = element; \
|
||||
return N##_elements++; \
|
||||
} \
|
||||
int N##_put(T* elements, size_t len) { \
|
||||
len --; \
|
||||
elements--; \
|
||||
while (N##_add(*++elements) != -1 && len--); \
|
||||
return N##_elements; \
|
||||
}
|
||||
/* Builds a full vector inspot */
|
||||
#define VECTOR_MAKE(T,N) \
|
||||
VECTOR_TYPE(T,N); \
|
||||
VECTOR_CORE(T,N)
|
||||
/* Builds a vector add function pointer for inside structures */
|
||||
#define VECTOR_IMPL(T,N) int (*N##_add)(T)
|
||||
|
||||
//===================================================================
|
||||
//=========================== code.c ================================
|
||||
|
@ -392,7 +431,12 @@ 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_strings_add (const char *); /* function wrapping code_chars_add */
|
||||
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;
|
||||
|
@ -495,8 +539,9 @@ enum {
|
|||
COMPILER_QCCX, /* qccx QuakeC */
|
||||
COMPILER_GMQCC /* this QuakeC */
|
||||
};
|
||||
extern int opts_debug;
|
||||
extern int opts_memchk;
|
||||
extern int opts_darkplaces_stringtablebug;
|
||||
extern int opts_omit_nullcode;
|
||||
extern bool opts_debug;
|
||||
extern bool opts_memchk;
|
||||
extern bool opts_darkplaces_stringtablebug;
|
||||
extern bool opts_omit_nullcode;
|
||||
extern int opts_compiler;
|
||||
#endif
|
||||
|
|
18
main.c
18
main.c
|
@ -26,11 +26,11 @@ typedef struct { char *name, type; } argitem;
|
|||
VECTOR_MAKE(argitem, items);
|
||||
|
||||
/* global options */
|
||||
int opts_debug = 0;
|
||||
int opts_memchk = 0;
|
||||
int opts_compiler = COMPILER_GMQCC;
|
||||
int opts_darkplaces_stringtablebug = 0;
|
||||
int opts_omit_nullcode = 0;
|
||||
bool opts_debug = false;
|
||||
bool opts_memchk = false;
|
||||
bool opts_darkplaces_stringtablebug = false;
|
||||
bool opts_omit_nullcode = false;
|
||||
int opts_compiler = COMPILER_GMQCC;
|
||||
|
||||
static const int usage(const char *const app) {
|
||||
printf("usage:\n");
|
||||
|
@ -73,8 +73,8 @@ int main(int argc, char **argv) {
|
|||
case 'a': items_add((argitem){util_strdup(&argv[1][2]), 1}); break; /* assemble */
|
||||
case 'i': items_add((argitem){util_strdup(&argv[1][2]), 2}); break; /* includes */
|
||||
default:
|
||||
if (!strncmp(&argv[1][1], "debug" , 5)) { opts_debug = 1; break; }
|
||||
if (!strncmp(&argv[1][1], "memchk", 6)) { opts_memchk = 1; break; }
|
||||
if (!strncmp(&argv[1][1], "debug" , 5)) { opts_debug = true; break; }
|
||||
if (!strncmp(&argv[1][1], "memchk", 6)) { opts_memchk = true; break; }
|
||||
if (!strncmp(&argv[1][1], "help", 4)) {
|
||||
return usage(app);
|
||||
break;
|
||||
|
@ -95,11 +95,11 @@ int main(int argc, char **argv) {
|
|||
|
||||
/* code specific switches */
|
||||
if (!strcmp(&argv[1][1], "fdarkplaces-stringtablebug")) {
|
||||
opts_darkplaces_stringtablebug = 1;
|
||||
opts_darkplaces_stringtablebug = true;
|
||||
break;
|
||||
}
|
||||
if (!strcmp(&argv[1][1], "fomit-nullcode")) {
|
||||
opts_omit_nullcode = 1;
|
||||
opts_omit_nullcode = true;
|
||||
break;
|
||||
}
|
||||
return usage(app);
|
||||
|
|
18
util.c
18
util.c
|
@ -335,3 +335,21 @@ int util_getline(char **lineptr, size_t *n, FILE *stream) {
|
|||
*pos = '\0';
|
||||
return (ret = pos - *lineptr);
|
||||
}
|
||||
|
||||
/*
|
||||
* Strechy string buffer (for easy string creation) -- this is fast, just
|
||||
* say no to strict aliasing.
|
||||
*/
|
||||
//#define util_stringbuf_add(a,v) ((((a)==0 ||((int*)(a)-2)[1]+(1)>=((int*)(a)-2)[0])?util_stringbuf_grow((void**)&(a),(1),sizeof(*(a))):0),(a)[((int*)(a)-2)[1]++]=(v))
|
||||
//#define util_stringbuf_len(a) ((a)? ((int*)(a)-2)[1]:0)
|
||||
//#define util_stringbuf_del(a) ((a)?free (((int*)(a)-2)),0:0)
|
||||
void *util_stringbuf_grow(void **a, int in, int it) {
|
||||
int m = *a ? 2 * ((int*)(*a)-2)[0]+in : in+1;
|
||||
void *p = realloc(*a ? ((int*)(*a)-2) : 0, it * m + sizeof(int)*2);
|
||||
if (p) {
|
||||
if (!*a) ((int*)p)[1] = 0;
|
||||
*a = (void*)((int*)p+2);
|
||||
((int*)(*a)-2)[0] = m;
|
||||
}
|
||||
return *a;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue