From 46123d359fa8d93a296902a0f830eafe8f31b344 Mon Sep 17 00:00:00 2001 From: Asher Baker Date: Fri, 15 Aug 2014 16:32:00 +0000 Subject: [PATCH 1/3] Add support for Emscripten to the compiler. --- compiler/amxxpc/amxxpc.cpp | 37 ++++++++++++++++++++++++++----------- compiler/amxxpc/amxxpc.h | 3 +++ compiler/amxxpc/sclinux.h | 6 +++++- compiler/libpc300/sc1.c | 6 ++++-- compiler/libpc300/sc2.c | 2 +- compiler/libpc300/sclinux.h | 6 +++++- 6 files changed, 44 insertions(+), 16 deletions(-) diff --git a/compiler/amxxpc/amxxpc.cpp b/compiler/amxxpc/amxxpc.cpp index 0295abae..8dca2ed5 100755 --- a/compiler/amxxpc/amxxpc.cpp +++ b/compiler/amxxpc/amxxpc.cpp @@ -28,40 +28,50 @@ #endif #endif -static PRINTF pc_printf = NULL; - void ReadFileIntoPl(abl *pl, FILE *fp); bool CompressPl(abl *pl); void Pl2Bh(abl *pl, BinPlugin *bh); void WriteBh(BinaryWriter *bw, BinPlugin *bh); +#if defined(EMSCRIPTEN) +extern "C" void Compile32(int argc, char **argv); +extern "C" int pc_printf(const char *message,...); +#else +static PRINTF pc_printf = NULL; +#endif + int main(int argc, char **argv) { struct abl pl32; -#if defined(__linux__) +#if defined(EMSCRIPTEN) + COMPILER sc32 = (COMPILER)Compile32; + //pc_printf = (PRINTF)pc_printf; +#else +# if defined(__linux__) HINSTANCE lib = NULL; if (FileExists("./amxxpc32.so")) lib = dlmount("./amxxpc32.so"); else lib = dlmount("amxxpc32.so"); -#elif defined(__APPLE__) +# elif defined(__APPLE__) HINSTANCE lib = dlmount("amxxpc32.dylib"); -#else +# else HINSTANCE lib = dlmount("amxxpc32.dll"); -#endif +# endif if (!lib) { -#if defined(__linux__) || defined(__APPLE__) +# if defined(__linux__) || defined(__APPLE__) printf("compiler failed to instantiate: %s\n", dlerror()); -#else +# else printf("compiler failed to instantiate: %d\n", GetLastError()); -#endif +# endif exit(0); } COMPILER sc32 = (COMPILER)dlsym(lib, "Compile32"); pc_printf = (PRINTF)dlsym(lib, "pc_printf"); +#endif //EMSCRIPTEN if (!sc32 || !pc_printf) { @@ -73,8 +83,9 @@ int main(int argc, char **argv) exit(0); } - pc_printf("Welcome to the AMX Mod X %s Compiler.\n", SVN_VERSION); - pc_printf("Copyright (c) 1997-2013 ITB CompuPhase, AMX Mod X Team\n\n"); + pc_printf("AMX Mod X Compiler %s\n", SVN_VERSION); + pc_printf("Copyright (c) 1997-2006 ITB CompuPhase\n"); + pc_printf("Copyright (c) 2004-2013 AMX Mod X Team\n\n"); if (argc < 2) { @@ -166,7 +177,9 @@ int main(int argc, char **argv) fclose(fp); unlink(file); pc_printf("Error, failed to write binary\n"); +#if !defined EMSCRIPTEN dlclose(lib); +#endif exit(0); } @@ -176,7 +189,9 @@ int main(int argc, char **argv) pc_printf("Done.\n"); +#if !defined EMSCRIPTEN dlclose(lib); +#endif exit(0); } diff --git a/compiler/amxxpc/amxxpc.h b/compiler/amxxpc/amxxpc.h index c35c324d..9ebfce16 100755 --- a/compiler/amxxpc/amxxpc.h +++ b/compiler/amxxpc/amxxpc.h @@ -10,7 +10,10 @@ #ifndef _AMXXSC_INCLUDE_H #define _AMXXSC_INCLUDE_H +#ifndef SVN_VERSION #include +#endif + #define MAGIC_HEADER2 0x414D5858 #define MAGIC_VERSION 0x0300 diff --git a/compiler/amxxpc/sclinux.h b/compiler/amxxpc/sclinux.h index fb0b116b..6dc17ac2 100755 --- a/compiler/amxxpc/sclinux.h +++ b/compiler/amxxpc/sclinux.h @@ -31,7 +31,11 @@ * For Linux, we must overrule these settings with those defined in glibc. */ #if !defined __BYTE_ORDER -# include +# if defined EMSCRIPTEN +# include +# else +# include +# endif # if defined __APPLE__ # include # define __BYTE_ORDER BYTE_ORDER diff --git a/compiler/libpc300/sc1.c b/compiler/libpc300/sc1.c index d432a4a2..caeb8018 100755 --- a/compiler/libpc300/sc1.c +++ b/compiler/libpc300/sc1.c @@ -683,11 +683,13 @@ cleanup: pc_printf("Header size: %8ld bytes\n", (long)hdrsize); pc_printf("Code size: %8ld bytes\n", (long)code_idx); pc_printf("Data size: %8ld bytes\n", (long)glb_declared*sizeof(cell)); - pc_printf("Stack/heap size: %8ld bytes; ", (long)sc_stksize*sizeof(cell)); + pc_printf("Stack/heap size: %8ld bytes\n", (long)sc_stksize*sizeof(cell)); +#if 0 if (stacksize<0) pc_printf("max. usage is unknown, due to recursion\n"); else if (stacksize>0) pc_printf("estimated max. usage=%ld cells (%ld bytes)\n",stacksize,stacksize*sizeof(cell)); +#endif pc_printf("Total requirements:%8ld bytes\n", (long)hdrsize+(long)code_idx+(long)glb_declared*sizeof(cell)+(long)sc_stksize*sizeof(cell)); } /* if */ if (flag_exceed) @@ -1253,7 +1255,7 @@ static void setconfig(char *root) /* add the default "include" directory */ #if defined __WIN32__ || defined _WIN32 GetModuleFileName(NULL,path,_MAX_PATH); - #elif defined LINUX || defined __FreeBSD__ || defined __OpenBSD__ || defined __APPLE__ + #elif defined ENABLE_BINRELOC && (defined LINUX || defined __FreeBSD__ || defined __OpenBSD__ || defined __APPLE__) /* see www.autopackage.org for the BinReloc module */ ptr = (char *)SELFPATH; if (!ptr) diff --git a/compiler/libpc300/sc2.c b/compiler/libpc300/sc2.c index 42c31c08..07ed0cb3 100755 --- a/compiler/libpc300/sc2.c +++ b/compiler/libpc300/sc2.c @@ -586,7 +586,7 @@ static int htoi(cell *val,const unsigned char *curptr) return (int)(ptr-curptr); } -#if defined __GNUC__ +#if defined __APPLE__ static double pow10(double d) { return pow(10, d); diff --git a/compiler/libpc300/sclinux.h b/compiler/libpc300/sclinux.h index fb0b116b..6dc17ac2 100755 --- a/compiler/libpc300/sclinux.h +++ b/compiler/libpc300/sclinux.h @@ -31,7 +31,11 @@ * For Linux, we must overrule these settings with those defined in glibc. */ #if !defined __BYTE_ORDER -# include +# if defined EMSCRIPTEN +# include +# else +# include +# endif # if defined __APPLE__ # include # define __BYTE_ORDER BYTE_ORDER From f886e69dfbed8315599fb023704b3f32ab9be080 Mon Sep 17 00:00:00 2001 From: Asher Baker Date: Fri, 15 Aug 2014 23:23:11 +0100 Subject: [PATCH 2/3] Fixed linux build. --- compiler/amxxpc/amxxpc.cpp | 1 - compiler/libpc300/AMBuilder | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/amxxpc/amxxpc.cpp b/compiler/amxxpc/amxxpc.cpp index 8dca2ed5..c5734db9 100755 --- a/compiler/amxxpc/amxxpc.cpp +++ b/compiler/amxxpc/amxxpc.cpp @@ -46,7 +46,6 @@ int main(int argc, char **argv) #if defined(EMSCRIPTEN) COMPILER sc32 = (COMPILER)Compile32; - //pc_printf = (PRINTF)pc_printf; #else # if defined(__linux__) HINSTANCE lib = NULL; diff --git a/compiler/libpc300/AMBuilder b/compiler/libpc300/AMBuilder index 87e59f61..3776948c 100644 --- a/compiler/libpc300/AMBuilder +++ b/compiler/libpc300/AMBuilder @@ -13,6 +13,7 @@ binary.compiler.defines += [ 'NO_MAIN', 'PAWNC_DLL', 'HAVE_STDINT_H', + '_GNU_SOURCE', ] binary.sources = [ From 3a43a4114171f9cdc46fae395d21d0282b1b08db Mon Sep 17 00:00:00 2001 From: Asher Baker Date: Fri, 15 Aug 2014 23:46:16 +0100 Subject: [PATCH 3/3] Added back estimated stack usage. --- compiler/libpc300/sc1.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/compiler/libpc300/sc1.c b/compiler/libpc300/sc1.c index caeb8018..d8429f5e 100755 --- a/compiler/libpc300/sc1.c +++ b/compiler/libpc300/sc1.c @@ -684,12 +684,8 @@ cleanup: pc_printf("Code size: %8ld bytes\n", (long)code_idx); pc_printf("Data size: %8ld bytes\n", (long)glb_declared*sizeof(cell)); pc_printf("Stack/heap size: %8ld bytes\n", (long)sc_stksize*sizeof(cell)); -#if 0 - if (stacksize<0) - pc_printf("max. usage is unknown, due to recursion\n"); - else if (stacksize>0) - pc_printf("estimated max. usage=%ld cells (%ld bytes)\n",stacksize,stacksize*sizeof(cell)); -#endif + if (stacksize>0) + pc_printf("Estimated usage: %8ld bytes\n", stacksize*sizeof(cell)); pc_printf("Total requirements:%8ld bytes\n", (long)hdrsize+(long)code_idx+(long)glb_declared*sizeof(cell)+(long)sc_stksize*sizeof(cell)); } /* if */ if (flag_exceed)