From 608aedb5005e68202d3d10a69aa375501fc9cd9b Mon Sep 17 00:00:00 2001 From: Nunuhara Cabbage Date: Fri, 14 Feb 2020 16:08:32 -0800 Subject: [PATCH] Fix various issues with MSYS2/mingw build --- include/system4.h | 6 ++++++ include/system4/ain.h | 2 +- include/system4/instructions.h | 2 ++ src/ain.c | 2 +- src/aindump/aindump.c | 11 ++++++----- src/aindump/dasm.c | 10 +++++----- src/aindump/guess_filenames.c | 2 +- src/ainedit/ainedit.c | 1 + src/ainedit/repack.c | 2 +- src/ald.c | 14 +++++++++++++- src/file.c | 11 +++++++++-- src/instructions.c | 6 ++++++ src/system4.c | 2 ++ 13 files changed, 54 insertions(+), 17 deletions(-) diff --git a/include/system4.h b/include/system4.h index a33aa8a..69fbdad 100644 --- a/include/system4.h +++ b/include/system4.h @@ -87,4 +87,10 @@ char *unix_path(const char *path); char *gamedir_path(const char *path); char *savedir_path(const char *filename); +#ifdef _WIN32 +#define SIZE_T_FMT "I" +#else +#define SIZE_T_FMT "z" +#endif + #endif /* SYSTEM4_H */ diff --git a/include/system4/ain.h b/include/system4/ain.h index d05e64d..ea1e5b4 100644 --- a/include/system4/ain.h +++ b/include/system4/ain.h @@ -147,7 +147,7 @@ struct ain_variable { }; struct ain_function { - int32_t address; + ain_addr_t address; char *name; bool is_label; struct ain_type return_type; diff --git a/include/system4/instructions.h b/include/system4/instructions.h index eb03444..f251667 100644 --- a/include/system4/instructions.h +++ b/include/system4/instructions.h @@ -378,6 +378,8 @@ struct instruction { struct instruction instructions[NR_OPCODES]; +void initialize_instructions(void); + static const_pure inline int32_t instruction_width(enum opcode opcode) { return 2 + instructions[opcode].nr_args * 4; diff --git a/src/ain.c b/src/ain.c index 0cc7693..6d41191 100644 --- a/src/ain.c +++ b/src/ain.c @@ -631,7 +631,7 @@ static struct ain_function_type *read_function_types(struct ain_reader *r, int c do { \ const struct instruction *instr; \ for (size_t addr = start; addr < ain->code_size; addr += instruction_width(instr->opcode)) { \ - int16_t _fei_opcode = LittleEndian_getW(ain->code, addr); \ + uint16_t _fei_opcode = LittleEndian_getW(ain->code, addr); \ if (_fei_opcode >= NR_OPCODES) \ ERROR("Unknown/invalid opcode: %u", _fei_opcode); \ instr = &instructions[_fei_opcode]; \ diff --git a/src/aindump/aindump.c b/src/aindump/aindump.c index 3cc15d2..7e59bca 100644 --- a/src/aindump/aindump.c +++ b/src/aindump/aindump.c @@ -393,21 +393,21 @@ static void ain_audit(FILE *f, struct ain *ain) uint16_t opcode = LittleEndian_getW(ain->code, addr); const struct instruction *instr = &instructions[opcode]; if (opcode >= NR_OPCODES) { - ERROR("0x%08lx: Invalid/unknown opcode: %x", opcode); + ERROR("0x%08" SIZE_T_FMT "x: Invalid/unknown opcode: %x", opcode); } if (!instr->implemented) { - fprintf(f, "0x%08lx: %s (unimplemented instruction)\n", addr, instr->name); + fprintf(f, "0x%08" SIZE_T_FMT "x: %s (unimplemented instruction)\n", addr, instr->name); } if (opcode == CALLSYS) { uint32_t syscode = LittleEndian_getDW(ain->code, addr + 2); if (syscode >= NR_SYSCALLS) { - ERROR("0x%08lx: CALLSYS system.(0x%x)\n", addr, syscode); + ERROR("0x%08" SIZE_T_FMT "x: CALLSYS system.(0x%x)\n", addr, syscode); } const char * const name = syscalls[syscode].name; if (!name) { - fprintf(f, "0x%08lx: CALLSYS system.(0x%x)\n", addr, syscode); + fprintf(f, "0x%08" SIZE_T_FMT "x: CALLSYS system.(0x%x)\n", addr, syscode); } else if (!syscalls[syscode].implemented) { - fprintf(f, "0x%08lx: CALLSYS %s (unimplemented system call)\n", addr, name); + fprintf(f, "0x%08" SIZE_T_FMT "x: CALLSYS %s (unimplemented system call)\n", addr, name); } } @@ -490,6 +490,7 @@ enum { int main(int argc, char *argv[]) { + initialize_instructions(); bool decrypt = false; char *output_file = NULL; FILE *output = stdout; diff --git a/src/aindump/dasm.c b/src/aindump/dasm.c index 37a22a8..fcca0f7 100644 --- a/src/aindump/dasm.c +++ b/src/aindump/dasm.c @@ -318,7 +318,7 @@ static void print_argument(struct dasm_state *dasm, int32_t arg, enum instructio switch (type) { case T_INT: case T_SWITCH: - fprintf(dasm->out, "0x%x", arg); + fprintf(dasm->out, "%d", arg); break; case T_FLOAT: fprintf(dasm->out, "%f", arg_to_float(arg)); @@ -469,7 +469,7 @@ static void dasm_leave_function(struct dasm_state *dasm) static void print_instruction(struct dasm_state *dasm, const struct instruction *instr) { if (dasm->flags & DASM_RAW) - fprintf(dasm->out, "0x%08lX:\t", dasm->addr); + fprintf(dasm->out, "0x%08" SIZE_T_FMT "X:\t", dasm->addr); switch (instr->opcode) { case FUNC: @@ -503,7 +503,7 @@ static const struct instruction *get_instruction(struct dasm_state *dasm) static void print_switch_case(struct dasm_state *dasm, struct ain_switch_case *c) { - fprintf(dasm->out, ".CASE %ld:%ld ", c->parent - dasm->ain->switches, c - c->parent->cases); + fprintf(dasm->out, ".CASE %" SIZE_T_FMT "d:%" SIZE_T_FMT "d ", c->parent - dasm->ain->switches, c - c->parent->cases); switch (c->parent->case_type) { case AIN_SWITCH_INT: fprintf(dasm->out, "%d", c->value); @@ -527,7 +527,7 @@ static void print_switch_case(struct dasm_state *dasm, struct ain_switch_case *c static char *genlabel(size_t addr) { char name[64]; - snprintf(name, 64, "0x%lx", addr); + snprintf(name, 64, "0x%" SIZE_T_FMT "x", addr); return strdup(name); } @@ -589,7 +589,7 @@ void disassemble_ain(FILE *out, struct ain *ain, unsigned int flags) print_switch_case(&dasm, t->switch_case); break; case JMP_DEFAULT: - fprintf(dasm.out, ".DEFAULT %ld\n", t->switch_default - dasm.ain->switches); + fprintf(dasm.out, ".DEFAULT %" SIZE_T_FMT "d\n", t->switch_default - dasm.ain->switches); break; } } diff --git a/src/aindump/guess_filenames.c b/src/aindump/guess_filenames.c index 038b932..97c6a06 100644 --- a/src/aindump/guess_filenames.c +++ b/src/aindump/guess_filenames.c @@ -42,7 +42,7 @@ static size_t get_prefix_length(char *a, char *b) static char *guess_itoa(possibly_unused struct guesser_state *state, size_t n) { char buf[512]; - snprintf(buf, 512, "%lu", n); + snprintf(buf, 512, "%" SIZE_T_FMT "u", n); return strdup(buf); } diff --git a/src/ainedit/ainedit.c b/src/ainedit/ainedit.c index 32f6693..12fb98b 100644 --- a/src/ainedit/ainedit.c +++ b/src/ainedit/ainedit.c @@ -51,6 +51,7 @@ enum { int main(int argc, char *argv[]) { + initialize_instructions(); struct ain *ain; int err = AIN_SUCCESS; const char *code_file = NULL; diff --git a/src/ainedit/repack.c b/src/ainedit/repack.c index dc43aea..d9021ec 100644 --- a/src/ainedit/repack.c +++ b/src/ainedit/repack.c @@ -427,7 +427,7 @@ void ain_write(const char *filename, struct ain *ain) else buf = ain_compress(buf, &len); - FILE *out = fopen(filename, "w"); + FILE *out = fopen(filename, "wb"); if (!out) ERROR("Failed to open '%s': %s", filename, strerror(errno)); if (fwrite(buf, len, 1, out) != 1) diff --git a/src/ald.c b/src/ald.c index f044979..72d3dd1 100644 --- a/src/ald.c +++ b/src/ald.c @@ -23,11 +23,19 @@ #include #include #include -#include #include #include "little_endian.h" +#include "system4.h" #include "system4/ald.h" +#ifdef _WIN32 +#define mmap(...) (ERROR("mmap not supported on Windows"), NULL) +#define munmap(...) ERROR("munmap not supported on Windows") +#define MAP_FAILED 0 +#else +#include +#endif + static const char *errtab[ALD_MAX_ERROR] = { [ALD_SUCCESS] = "Success", [ALD_FILE_ERROR] = "Error opening ALD file", @@ -258,6 +266,10 @@ struct ald_archive *ald_open(char **files, int count, int flags, int *error) struct ald_archive *ar = calloc(1, sizeof(struct ald_archive)); bool gotmap = false; +#ifdef _WIN32 + flags &= ~ALD_MMAP; +#endif + for (int i = 0; i < count; i++) { // XXX: why allow this? if (!files[i]) diff --git a/src/file.c b/src/file.c index b2097fa..0611152 100644 --- a/src/file.c +++ b/src/file.c @@ -60,6 +60,13 @@ bool file_exists(const char *path) return access(path, F_OK) != -1; } +#if (defined(_WIN32) || defined(__WIN32__)) +#include +#define make_dir(path, mode) _mkdir(path) +#else +#define make_dir(path, mode) mkdir(path, mode) +#endif + // Adapted from http://stackoverflow.com/a/2336245/119527 int mkdir_p(const char *path) { @@ -82,7 +89,7 @@ int mkdir_p(const char *path) // Temporarily truncate *p = '\0'; - if (mkdir(_path, S_IRWXU) != 0) { + if (make_dir(_path, S_IRWXU) != 0) { if (errno != EEXIST) return -1; } @@ -91,7 +98,7 @@ int mkdir_p(const char *path) } } - if (mkdir(_path, S_IRWXU) != 0) { + if (make_dir(_path, S_IRWXU) != 0) { if (errno != EEXIST) return -1; } diff --git a/src/instructions.c b/src/instructions.c index e04a338..6890163 100644 --- a/src/instructions.c +++ b/src/instructions.c @@ -378,3 +378,9 @@ struct instruction instructions[NR_OPCODES] = { TODO ( OP_0X104, 0 ), TODO ( OP_0X105, 1, T_STRUCT ), }; + +void initialize_instructions(void) +{ + // this is a dumb hack to force linking/initialization of the above + // arrays on Windows. +} diff --git a/src/system4.c b/src/system4.c index a1906c4..bf28e57 100644 --- a/src/system4.c +++ b/src/system4.c @@ -26,6 +26,7 @@ #include "system4.h" #include "system4/ain.h" #include "system4/ald.h" +#include "system4/instructions.h" #include "system4/utfsjis.h" #include "vm.h" @@ -196,6 +197,7 @@ static void config_init(void) int main(int argc, char *argv[]) { + initialize_instructions(); size_t len; char *ainfile; int err = AIN_SUCCESS;