mirror of
https://github.com/DarkPlacesEngine/gmqcc.git
synced 2025-03-25 03:51:26 +00:00
ensure all data is LE format, cleanups, etc.
This commit is contained in:
parent
32b804864e
commit
082caf61bd
4 changed files with 135 additions and 102 deletions
25
assembler.c
25
assembler.c
|
@ -161,7 +161,7 @@ void asm_clear() {
|
|||
mem_d(assembly_constants_data[i].name);
|
||||
mem_d(assembly_constants_data);
|
||||
}
|
||||
|
||||
|
||||
void asm_parse(FILE *fp) {
|
||||
char *data = NULL;
|
||||
char *skip = NULL;
|
||||
|
@ -187,7 +187,7 @@ void asm_parse(FILE *fp) {
|
|||
exit (1); \
|
||||
} else { \
|
||||
size_t offset_code = code_statements_elements+1; \
|
||||
size_t offset_chars = code_strings_elements +1; \
|
||||
size_t offset_chars = code_chars_elements +1; \
|
||||
size_t offset_globals = code_globals_elements +1; \
|
||||
size_t offset_functions = code_functions_elements +1; \
|
||||
size_t offset_fields = code_fields_elements +1; \
|
||||
|
@ -201,7 +201,7 @@ void asm_parse(FILE *fp) {
|
|||
(void)offset_fields; \
|
||||
(void)offset_defs; \
|
||||
assembly_constants_add((globals){ \
|
||||
.name = util_strdup(skip), \
|
||||
.name = util_strdup("empty"), \
|
||||
.offset = offset_globals \
|
||||
}); \
|
||||
} \
|
||||
|
@ -210,6 +210,7 @@ void asm_parse(FILE *fp) {
|
|||
|
||||
/* FLOAT */
|
||||
DECLTYPE(asm_keys[0], {
|
||||
//util_debug("ASM", "Constant FLOAT\n");
|
||||
code_defs_add((prog_section_def){
|
||||
.type = TYPE_FLOAT,
|
||||
.offset = offset_globals, /* global table */
|
||||
|
@ -219,7 +220,9 @@ void asm_parse(FILE *fp) {
|
|||
code_globals_add(*(int*)&f);
|
||||
|
||||
});
|
||||
/* VECTOR */
|
||||
DECLTYPE(asm_keys[1], {
|
||||
//util_debug("ASM", "Constant VECTOR\n");
|
||||
code_defs_add((prog_section_def){
|
||||
.type = TYPE_FLOAT,
|
||||
.offset = offset_globals, /* global table */
|
||||
|
@ -232,19 +235,21 @@ void asm_parse(FILE *fp) {
|
|||
code_globals_add(*(int*)&f2);
|
||||
code_globals_add(*(int*)&f3);
|
||||
});
|
||||
/* ENTITY */ DECLTYPE(asm_keys[2], {});
|
||||
/* FIELD */ DECLTYPE(asm_keys[3], {});
|
||||
/* ENTITY */ DECLTYPE(asm_keys[2], {util_debug("ASM", "Constant ENTITY\n");});
|
||||
/* FIELD */ DECLTYPE(asm_keys[3], {util_debug("ASM", "Constant FIELD\n");});
|
||||
/* STRING */
|
||||
DECLTYPE(asm_keys[4], {
|
||||
//util_debug("ASM", "Constant STRING\n");
|
||||
code_defs_add((prog_section_def){
|
||||
.type = TYPE_STRING,
|
||||
.offset = offset_globals, /* offset to offset in string table (for data)*/
|
||||
.name = offset_chars /* location of name in string table (for name)*/
|
||||
});
|
||||
code_strings_add('h');
|
||||
code_strings_add("hello world");
|
||||
});
|
||||
/* FUNCTION */
|
||||
DECLTYPE(asm_keys[5], {
|
||||
//util_debug("ASM", "Constant FUNCTION\n");
|
||||
/* TODO: parse */
|
||||
if (state != ASM_NULL) {
|
||||
printf("%li: Error unfinished function block, expected DONE or RETURN\n", line);
|
||||
|
@ -305,9 +310,9 @@ void asm_parse(FILE *fp) {
|
|||
i ++;
|
||||
}
|
||||
|
||||
util_debug("ASM", "Operand 1: %s\n", util_strrnl(op[0]));
|
||||
util_debug("ASM", "Operand 2: %s\n", util_strrnl(op[1]));
|
||||
util_debug("ASM", "Operand 3: %s\n", util_strrnl(op[2]));
|
||||
// util_debug("ASM", "Operand 1: %s\n", util_strrnl(op[0]));
|
||||
// util_debug("ASM", "Operand 2: %s\n", util_strrnl(op[1]));
|
||||
// util_debug("ASM", "Operand 3: %s\n", util_strrnl(op[2]));
|
||||
|
||||
/* check */
|
||||
if (i != o) {
|
||||
|
@ -331,5 +336,5 @@ void asm_parse(FILE *fp) {
|
|||
mem_d(data);
|
||||
line ++;
|
||||
}
|
||||
asm_clear();
|
||||
asm_clear();
|
||||
}
|
||||
|
|
132
code.c
132
code.c
|
@ -68,17 +68,26 @@ typedef struct {
|
|||
* code_globals_allocated -- size of the array allocated
|
||||
* code_globals_add(T) -- add element (returns -1 on error)
|
||||
*
|
||||
* code_strings_data -- raw char* array
|
||||
* code_strings_elements -- number of elements
|
||||
* code_strings_allocated -- size of the array allocated
|
||||
* code_strings_add(T) -- add element (returns -1 on error)
|
||||
* code_chars_data -- raw char* array
|
||||
* code_chars_elements -- number of elements
|
||||
* code_chars_allocated -- size of the array allocated
|
||||
* code_chars_add(T) -- add element (returns -1 on error)
|
||||
*/
|
||||
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_strings );
|
||||
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() {
|
||||
/*
|
||||
|
@ -91,30 +100,19 @@ void code_init() {
|
|||
for(i = 0; i < 28; i++)
|
||||
code_globals_add(0);
|
||||
|
||||
code_strings_add ('\0');
|
||||
code_chars_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; \
|
||||
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");
|
||||
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_globals_add(1); /* m_init */
|
||||
code_globals_add(2); /* print */
|
||||
|
@ -142,13 +140,21 @@ void code_write() {
|
|||
code_header.version = 6;
|
||||
code_header.crc16 = 0; /* TODO: */
|
||||
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.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_chars_elements };
|
||||
code_header.entfield = 0; /* TODO: */
|
||||
|
||||
/* ensure all data is in LE format */
|
||||
util_endianswap(&code_header, sizeof(prog_header), 1);
|
||||
util_endianswap(code_statements_data, sizeof(prog_section_statement), code_statements_elements);
|
||||
util_endianswap(code_defs_data, sizeof(prog_section_def), code_defs_elements);
|
||||
util_endianswap(code_fields_data, sizeof(prog_section_field), code_fields_elements);
|
||||
util_endianswap(code_functions_data, sizeof(prog_section_function), code_functions_elements);
|
||||
util_endianswap(code_globals_data, sizeof(int), code_globals_elements);
|
||||
|
||||
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);
|
||||
|
@ -156,41 +162,41 @@ void code_write() {
|
|||
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(int) *code_globals_elements, fp);
|
||||
fwrite(code_strings_data, 1, 1 *code_strings_elements, fp);
|
||||
fwrite(code_chars_data, 1, 1 *code_chars_elements, fp);
|
||||
|
||||
mem_d(code_statements_data);
|
||||
mem_d(code_defs_data);
|
||||
mem_d(code_fields_data);
|
||||
mem_d(code_functions_data);
|
||||
mem_d(code_globals_data);
|
||||
mem_d(code_strings_data);
|
||||
util_debug("GEN","header:\n");
|
||||
util_debug("GEN"," version: = %d\n", code_header.version );
|
||||
util_debug("GEN"," crc16: = %d\n", code_header.crc16 );
|
||||
util_debug("GEN"," entfield: = %d\n", code_header.entfield);
|
||||
util_debug("GEN"," statements = {.offset = % 8d, .length = % 8d}\n", code_header.statements.offset, code_header.statements.length);
|
||||
util_debug("GEN"," defs = {.offset = % 8d, .length = % 8d}\n", code_header.defs .offset, code_header.defs .length);
|
||||
util_debug("GEN"," fields = {.offset = % 8d, .length = % 8d}\n", code_header.fields .offset, code_header.fields .length);
|
||||
util_debug("GEN"," functions = {.offset = % 8d, .length = % 8d}\n", code_header.functions .offset, code_header.functions .length);
|
||||
util_debug("GEN"," globals = {.offset = % 8d, .length = % 8d}\n", code_header.globals .offset, code_header.globals .length);
|
||||
util_debug("GEN"," strings = {.offset = % 8d, .length = % 8d}\n", code_header.strings .offset, code_header.strings .length);
|
||||
|
||||
util_debug("GEN","wrote program.dat:\n\
|
||||
version: = %d\n\
|
||||
crc16: = %d\n\
|
||||
entfield: = %d\n\
|
||||
statements {.offset = % 8d, .length = % 8d}\n\
|
||||
defs {.offset = % 8d, .length = % 8d}\n\
|
||||
fields {.offset = % 8d, .length = % 8d}\n\
|
||||
functions {.offset = % 8d, .length = % 8d}\n\
|
||||
globals {.offset = % 8d, .length = % 8d}\n\
|
||||
strings {.offset = % 8d, .length = % 8d}\n",
|
||||
code_header.version,
|
||||
code_header.crc16,
|
||||
code_header.entfield,
|
||||
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
|
||||
);
|
||||
/* WRITE out all functions */
|
||||
util_debug("GEN", "functions:\n");
|
||||
size_t i = 0;
|
||||
for (; i < code_functions_elements; i++) {
|
||||
util_debug("GEN", " {.entry =% 5d, .firstlocal =% 5d, .locals =% 5d, .profile =% 5d, .name =% 5d, .file =% 5d, .nargs =% 5d, .argsize =%0X }\n",
|
||||
code_functions_data[i].entry,
|
||||
code_functions_data[i].firstlocal,
|
||||
code_functions_data[i].locals,
|
||||
code_functions_data[i].profile,
|
||||
code_functions_data[i].name,
|
||||
code_functions_data[i].file,
|
||||
code_functions_data[i].nargs,
|
||||
*((int32_t*)&code_functions_data[i].argsize)
|
||||
);
|
||||
}
|
||||
|
||||
mem_d(code_statements_data);
|
||||
mem_d(code_defs_data);
|
||||
mem_d(code_fields_data);
|
||||
mem_d(code_functions_data);
|
||||
mem_d(code_globals_data);
|
||||
mem_d(code_chars_data);
|
||||
|
||||
fclose(fp);
|
||||
}
|
||||
|
|
32
gmqcc.h
32
gmqcc.h
|
@ -156,15 +156,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 *);
|
||||
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);
|
||||
|
||||
#ifdef NOTRACK
|
||||
# define mem_a(x) malloc(x)
|
||||
|
@ -368,16 +369,17 @@ enum {
|
|||
* VECTOR_MAKE(prog_section_field, code_fields );
|
||||
* VECTOR_MAKE(prog_section_function, code_functions );
|
||||
* VECTOR_MAKE(int, code_globals );
|
||||
* VECTOR_MAKE(char, code_strings );
|
||||
* 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_strings_add (char);
|
||||
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_strings_add (const char *); /* function wrapping code_chars_add */
|
||||
extern long code_statements_elements;
|
||||
extern long code_strings_elements;
|
||||
extern long code_chars_elements;
|
||||
extern long code_globals_elements;
|
||||
extern long code_functions_elements;
|
||||
extern long code_fields_elements;
|
||||
|
|
48
util.c
48
util.c
|
@ -71,32 +71,28 @@ void util_meminfo() {
|
|||
);
|
||||
}
|
||||
|
||||
#ifndef mem_d
|
||||
#define mem_d(x) util_memory_d((x), __LINE__, __FILE__)
|
||||
#endif
|
||||
#ifndef mem_a
|
||||
#define mem_a(x) util_memory_a((x), __LINE__, __FILE__)
|
||||
#endif
|
||||
//#ifndef mem_d
|
||||
//#define mem_d(x) util_memory_d((x), __LINE__, __FILE__)
|
||||
//#endif
|
||||
//#ifndef mem_a
|
||||
//#define mem_a(x) util_memory_a((x), __LINE__, __FILE__)
|
||||
//#endif
|
||||
|
||||
/*
|
||||
* Some string utility functions, because strdup uses malloc, and we want
|
||||
* to track all memory (without replacing malloc).
|
||||
*/
|
||||
char *util_strdup(const char *s) {
|
||||
size_t len;
|
||||
char *ptr;
|
||||
size_t len = 0;
|
||||
char *ptr = NULL;
|
||||
|
||||
if (!s)
|
||||
return NULL;
|
||||
|
||||
len = strlen(s);
|
||||
ptr = mem_a (len+1);
|
||||
|
||||
if (ptr && len) {
|
||||
if ((len = strlen(s)) && (ptr = mem_a(len+1))) {
|
||||
memcpy(ptr, s, len);
|
||||
ptr[len] = '\0';
|
||||
}
|
||||
|
||||
return ptr;
|
||||
}
|
||||
|
||||
|
@ -142,12 +138,36 @@ void util_debug(const char *area, const char *ms, ...) {
|
|||
va_start(va, ms);
|
||||
fprintf (stdout, "DEBUG: ");
|
||||
fputc ('[', stdout);
|
||||
fprintf(stdout, "%s",area);
|
||||
fprintf(stdout, "%s", area);
|
||||
fputs ("] ", stdout);
|
||||
vfprintf(stdout, ms, va);
|
||||
va_end (va);
|
||||
}
|
||||
|
||||
/*
|
||||
* Endianess swapping, all data must be stored little-endian. This
|
||||
* reorders by stride and length, much nicer than other functions for
|
||||
* certian-sized types like short or int.
|
||||
*/
|
||||
void util_endianswap(void *m, int s, int l) {
|
||||
size_t w = 0;
|
||||
size_t i = 0;
|
||||
|
||||
/* ignore if we're already LE */
|
||||
if(*((char *)&s))
|
||||
return;
|
||||
|
||||
for(; w < l; w++) {
|
||||
for(; i < s << 1; i++) {
|
||||
unsigned char *p = (unsigned char *)m+w*s;
|
||||
unsigned char t = p[i];
|
||||
p[i] = p[s-i-1];
|
||||
p[s-i-1] = t;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Implements libc getline for systems that don't have it, which is
|
||||
* assmed all. This works the same as getline().
|
||||
|
|
Loading…
Reference in a new issue