From 25d629083f93f37b57761e1d7fa77f87ef9e41ad Mon Sep 17 00:00:00 2001 From: David Anderson Date: Sun, 24 Jul 2005 20:00:55 +0000 Subject: [PATCH] Initial import of Pawn (Small 3.0) --- compiler/libpc300/CMakeLists.txt | 52 + compiler/libpc300/amx.h | 439 +++ compiler/libpc300/amxdbg.h | 172 + compiler/libpc300/dllmain.c | 79 + compiler/libpc300/libpawnc.c | 334 ++ compiler/libpc300/libpawnc.def | 8 + compiler/libpc300/libpawnc.lbc | 5 + compiler/libpc300/libpawnc.rc | 62 + compiler/libpc300/libpc300.sln | 27 + compiler/libpc300/libpc300.vcproj | 276 ++ compiler/libpc300/osdefs.h | 94 + compiler/libpc300/pawn.ico | Bin 0 -> 8478 bytes compiler/libpc300/pawncc.c | 30 + compiler/libpc300/pawncc.def | 2 + compiler/libpc300/sc.h | 780 ++++ compiler/libpc300/sc1.c | 5555 +++++++++++++++++++++++++++++ compiler/libpc300/sc2.c | 2707 ++++++++++++++ compiler/libpc300/sc3.c | 2338 ++++++++++++ compiler/libpc300/sc4.c | 1304 +++++++ compiler/libpc300/sc5.c | 217 ++ compiler/libpc300/sc5.scp | 326 ++ compiler/libpc300/sc6.c | 1162 ++++++ compiler/libpc300/sc7.c | 631 ++++ compiler/libpc300/sc7.scp | 1613 +++++++++ compiler/libpc300/scexpand.c | 68 + compiler/libpc300/sci18n.c | 428 +++ compiler/libpc300/sclist.c | 459 +++ compiler/libpc300/scmemfil.c | 339 ++ compiler/libpc300/scpack.c | 450 +++ compiler/libpc300/scstate.c | 347 ++ compiler/libpc300/scstub.c | 29 + compiler/libpc300/scvars.c | 108 + 32 files changed, 20441 insertions(+) create mode 100755 compiler/libpc300/CMakeLists.txt create mode 100755 compiler/libpc300/amx.h create mode 100755 compiler/libpc300/amxdbg.h create mode 100755 compiler/libpc300/dllmain.c create mode 100755 compiler/libpc300/libpawnc.c create mode 100755 compiler/libpc300/libpawnc.def create mode 100755 compiler/libpc300/libpawnc.lbc create mode 100755 compiler/libpc300/libpawnc.rc create mode 100755 compiler/libpc300/libpc300.sln create mode 100755 compiler/libpc300/libpc300.vcproj create mode 100755 compiler/libpc300/osdefs.h create mode 100755 compiler/libpc300/pawn.ico create mode 100755 compiler/libpc300/pawncc.c create mode 100755 compiler/libpc300/pawncc.def create mode 100755 compiler/libpc300/sc.h create mode 100755 compiler/libpc300/sc1.c create mode 100755 compiler/libpc300/sc2.c create mode 100755 compiler/libpc300/sc3.c create mode 100755 compiler/libpc300/sc4.c create mode 100755 compiler/libpc300/sc5.c create mode 100755 compiler/libpc300/sc5.scp create mode 100755 compiler/libpc300/sc6.c create mode 100755 compiler/libpc300/sc7.c create mode 100755 compiler/libpc300/sc7.scp create mode 100755 compiler/libpc300/scexpand.c create mode 100755 compiler/libpc300/sci18n.c create mode 100755 compiler/libpc300/sclist.c create mode 100755 compiler/libpc300/scmemfil.c create mode 100755 compiler/libpc300/scpack.c create mode 100755 compiler/libpc300/scstate.c create mode 100755 compiler/libpc300/scstub.c create mode 100755 compiler/libpc300/scvars.c diff --git a/compiler/libpc300/CMakeLists.txt b/compiler/libpc300/CMakeLists.txt new file mode 100755 index 00000000..c57f21f4 --- /dev/null +++ b/compiler/libpc300/CMakeLists.txt @@ -0,0 +1,52 @@ +#build file for CMake, see http://www.cmake.org/ + +PROJECT(pawnc) + +# check for optional include files +INCLUDE(${CMAKE_ROOT}/Modules/CheckIncludeFile.cmake) +CHECK_INCLUDE_FILE("unistd.h" HAVE_UNISTD_H) +IF(HAVE_UNISTD_H) + ADD_DEFINITIONS(-DHAVE_UNISTD_H) +ENDIF(HAVE_UNISTD_H) +CHECK_INCLUDE_FILE("inttypes.h" HAVE_INTTYPES_H) +IF(HAVE_INTTYPES_H) + ADD_DEFINITIONS(-DHAVE_INTTYPES_H) +ENDIF(HAVE_INTTYPES_H) +CHECK_INCLUDE_FILE("stdint.h" HAVE_STDINT_H) +IF(HAVE_STDINT_H) + ADD_DEFINITIONS(-DHAVE_STDINT_H) +ENDIF(HAVE_STDINT_H) +CHECK_INCLUDE_FILE("alloca.h" HAVE_ALLOCA_H) +IF(HAVE_ALLOCA_H) + ADD_DEFINITIONS(-DHAVE_ALLOCA_H) +ENDIF(HAVE_ALLOCA_H) + +IF(UNIX) + ADD_DEFINITIONS(-DLINUX -DENABLE_BINRELOC -g) + INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/../linux) +ENDIF(UNIX) + +# The Pawn compiler shared library +SET(PAWNC_SRCS sc1.c sc2.c sc3.c sc4.c sc5.c sc6.c sc7.c + scexpand.c sci18n.c sclist.c scmemfil.c scstate.c scvars.c + libpawnc.c) +SET_SOURCE_FILES_PROPERTIES(sc1.c COMPILE_FLAGS -DNO_MAIN) +IF(WIN32) + SET(PAWNC_SRCS ${PAWNC_SRCS} libpawnc.def libpawnc.rc) + SET_SOURCE_FILES_PROPERTIES(libpawnc.c COMPILE_FLAGS -DPAWNC_DLL) +ENDIF(WIN32) +IF(UNIX) + SET(PAWNC_SRCS ${PAWNC_SRCS} ${CMAKE_CURRENT_SOURCE_DIR}/../linux/prefix.c) +ENDIF(UNIX) +ADD_LIBRARY(pawnc SHARED ${PAWNC_SRCS}) +IF(UNIX) + ADD_CUSTOM_COMMAND(TARGET pawnc POST_BUILD COMMAND strip ARGS -K pc_compile -K pc_addconstant -K pc_addtag -K pc_enablewarning libpawnc.so) +ENDIF(UNIX) + +# The Pawn compiler driver (console program) +SET(PAWNCC_SRCS pawncc.c) +IF(WIN32) + SET(PAWNCC_SRCS ${PAWNCC_SRCS} pawncc.def libpawnc.rc) +ENDIF(WIN32) +ADD_EXECUTABLE(pawncc ${PAWNCC_SRCS}) +TARGET_LINK_LIBRARIES(pawncc pawnc) diff --git a/compiler/libpc300/amx.h b/compiler/libpc300/amx.h new file mode 100755 index 00000000..cf194750 --- /dev/null +++ b/compiler/libpc300/amx.h @@ -0,0 +1,439 @@ +/* Pawn Abstract Machine (for the Pawn language) + * + * Copyright (c) ITB CompuPhase, 1997-2005 + * + * This software is provided "as-is", without any express or implied warranty. + * In no event will the authors be held liable for any damages arising from + * the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software in + * a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * 3. This notice may not be removed or altered from any source distribution. + * + * Version: $Id$ + */ + +#if defined FREEBSD && !defined __FreeBSD__ + #define __FreeBSD__ +#endif +#if defined LINUX || defined __FreeBSD__ || defined __OpenBSD__ + #include +#endif + +#ifndef AMX_H_INCLUDED +#define AMX_H_INCLUDED + +#if defined HAVE_STDINT_H + #include +#else + #if defined __LCC__ || defined __DMC__ || defined LINUX + #if defined HAVE_INTTYPES_H + #include + #else + #include + #endif + #elif !defined __STDC_VERSION__ || __STDC_VERSION__ < 199901L + /* The ISO C99 defines the int16_t and int_32t types. If the compiler got + * here, these types are probably undefined. + */ + #if defined __MACH__ + #include + typedef unsigned short int uint16_t; + typedef unsigned long int uint32_t; + #elif defined __FreeBSD__ + #include + #else + typedef short int int16_t; + typedef unsigned short int uint16_t; + #if defined SN_TARGET_PS2 + typedef int int32_t; + typedef unsigned int uint32_t; + #else + typedef long int int32_t; + typedef unsigned long int uint32_t; + #endif + #if defined __WIN32__ || defined _WIN32 || defined WIN32 + typedef __int64 int64_t; + typedef unsigned __int64 uint64_t; + #define HAVE_I64 + #elif defined __GNUC__ + typedef long long int64_t; + typedef unsigned long long uint64_t; + #define HAVE_I64 + #endif + #endif + #endif + #define HAVE_STDINT_H +#endif +#if defined _LP64 || defined WIN64 || defined _WIN64 + #if !defined __64BIT__ + #define __64BIT__ + #endif +#endif + +#if HAVE_ALLOCA_H + #include +#endif +#if defined __WIN32__ || defined _WIN32 || defined WIN32 /* || defined __MSDOS__ */ + #if !defined alloca + #define alloca(n) _alloca(n) + #endif +#endif + +#if !defined arraysize + #define arraysize(array) (sizeof(array) / sizeof((array)[0])) +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +#if defined PAWN_DLL + #if !defined AMX_NATIVE_CALL + #define AMX_NATIVE_CALL __stdcall + #endif + #if !defined AMXAPI + #define AMXAPI __stdcall + #endif +#endif + +/* calling convention for native functions */ +#if !defined AMX_NATIVE_CALL + #define AMX_NATIVE_CALL +#endif +/* calling convention for all interface functions and callback functions */ +#if !defined AMXAPI + #if defined STDECL + #define AMXAPI __stdcall + #elif defined CDECL + #define AMXAPI __cdecl + #elif defined GCC_HASCLASSVISIBILITY + #define AMXAPI __attribute__ ((visibility("default"))) + #else + #define AMXAPI + #endif +#endif +#if !defined AMXEXPORT + #define AMXEXPORT +#endif + +/* File format version Required AMX version + * 0 (original version) 0 + * 1 (opcodes JUMP.pri, SWITCH and CASETBL) 1 + * 2 (compressed files) 2 + * 3 (public variables) 2 + * 4 (opcodes SWAP.pri/alt and PUSHADDR) 4 + * 5 (tagnames table) 4 + * 6 (reformatted header) 6 + * 7 (name table, opcodes SYMTAG & SYSREQ.D) 7 + * 8 (opcode STMT, renewed debug interface) 8 + */ +#define CUR_FILE_VERSION 8 /* current file version; also the current AMX version */ +#define MIN_FILE_VERSION 6 /* lowest supported file format version for the current AMX version */ +#define MIN_AMX_VERSION 8 /* minimum AMX version needed to support the current file format */ + +#if !defined PAWN_CELL_SIZE + #define PAWN_CELL_SIZE 32 /* by default, use 32-bit cells */ +#endif +#if PAWN_CELL_SIZE==16 + typedef uint16_t ucell; + typedef int16_t cell; +#elif PAWN_CELL_SIZE==32 + typedef uint32_t ucell; + typedef int32_t cell; +#elif PAWN_CELL_SIZE==64 + typedef uint64_t ucell; + typedef int64_t cell; +#else + #error Unsupported cell size (PAWN_CELL_SIZE) +#endif + +#define UNPACKEDMAX ((1L << (sizeof(cell)-1)*8) - 1) +#define UNLIMITED (~1u >> 1) + +struct tagAMX; +typedef cell (AMX_NATIVE_CALL *AMX_NATIVE)(struct tagAMX *amx, cell *params); +typedef int (AMXAPI *AMX_CALLBACK)(struct tagAMX *amx, cell index, + cell *result, cell *params); +typedef int (AMXAPI *AMX_DEBUG)(struct tagAMX *amx); +#if !defined _FAR + #define _FAR +#endif + +#if defined _MSC_VER + #pragma warning(disable:4103) /* disable warning message 4103 that complains + * about pragma pack in a header file */ + #pragma warning(disable:4100) /* "'%$S' : unreferenced formal parameter" */ +#endif + +/* Some compilers do not support the #pragma align, which should be fine. Some + * compilers give a warning on unknown #pragmas, which is not so fine... + */ +#if (defined SN_TARGET_PS2 || defined __GNUC__) && !defined AMX_NO_ALIGN + #define AMX_NO_ALIGN +#endif + +#if defined __GNUC__ + #define PACKED __attribute__((packed)) +#else + #define PACKED +#endif + +#if !defined AMX_NO_ALIGN + #if defined LINUX || defined __FreeBSD__ + #pragma pack(1) /* structures must be packed (byte-aligned) */ + #elif defined MACOS && defined __MWERKS__ + #pragma options align=mac68k + #else + #pragma pack(push) + #pragma pack(1) /* structures must be packed (byte-aligned) */ + #if defined __TURBOC__ + #pragma option -a- /* "pack" pragma for older Borland compilers */ + #endif + #endif +#endif + +typedef struct tagAMX_NATIVE_INFO { + const char _FAR *name PACKED; + AMX_NATIVE func PACKED; +} PACKED AMX_NATIVE_INFO; + +#define AMX_USERNUM 4 +#define sEXPMAX 19 /* maximum name length for file version <= 6 */ +#define sNAMEMAX 31 /* maximum name length of symbol name */ + +typedef struct tagAMX_FUNCSTUB { + ucell address PACKED; + char name[sEXPMAX+1] PACKED; +} PACKED AMX_FUNCSTUB; + +typedef struct tagFUNCSTUBNT { + ucell address PACKED; + uint32_t nameofs PACKED; +} PACKED AMX_FUNCSTUBNT; + +/* The AMX structure is the internal structure for many functions. Not all + * fields are valid at all times; many fields are cached in local variables. + */ +typedef struct tagAMX { + unsigned char _FAR *base PACKED; /* points to the AMX header plus the code, optionally also the data */ + unsigned char _FAR *data PACKED; /* points to separate data+stack+heap, may be NULL */ + AMX_CALLBACK callback PACKED; + AMX_DEBUG debug PACKED; /* debug callback */ + /* for external functions a few registers must be accessible from the outside */ + cell cip PACKED; /* instruction pointer: relative to base + amxhdr->cod */ + cell frm PACKED; /* stack frame base: relative to base + amxhdr->dat */ + cell hea PACKED; /* top of the heap: relative to base + amxhdr->dat */ + cell hlw PACKED; /* bottom of the heap: relative to base + amxhdr->dat */ + cell stk PACKED; /* stack pointer: relative to base + amxhdr->dat */ + cell stp PACKED; /* top of the stack: relative to base + amxhdr->dat */ + int flags PACKED; /* current status, see amx_Flags() */ + /* user data */ + long usertags[AMX_USERNUM] PACKED; + void _FAR *userdata[AMX_USERNUM] PACKED; + /* native functions can raise an error */ + int error PACKED; + /* passing parameters requires a "count" field */ + int paramcount; + /* the sleep opcode needs to store the full AMX status */ + cell pri PACKED; + cell alt PACKED; + cell reset_stk PACKED; + cell reset_hea PACKED; + cell sysreq_d PACKED; /* relocated address/value for the SYSREQ.D opcode */ + #if defined JIT + /* support variables for the JIT */ + int reloc_size PACKED; /* required temporary buffer for relocations */ + long code_size PACKED; /* estimated memory footprint of the native code */ + #endif +} PACKED AMX; + +/* The AMX_HEADER structure is both the memory format as the file format. The + * structure is used internaly. + */ +typedef struct tagAMX_HEADER { + int32_t size PACKED; /* size of the "file" */ + uint16_t magic PACKED; /* signature */ + char file_version PACKED; /* file format version */ + char amx_version PACKED; /* required version of the AMX */ + int16_t flags PACKED; + int16_t defsize PACKED; /* size of a definition record */ + int32_t cod PACKED; /* initial value of COD - code block */ + int32_t dat PACKED; /* initial value of DAT - data block */ + int32_t hea PACKED; /* initial value of HEA - start of the heap */ + int32_t stp PACKED; /* initial value of STP - stack top */ + int32_t cip PACKED; /* initial value of CIP - the instruction pointer */ + int32_t publics PACKED; /* offset to the "public functions" table */ + int32_t natives PACKED; /* offset to the "native functions" table */ + int32_t libraries PACKED; /* offset to the table of libraries */ + int32_t pubvars PACKED; /* the "public variables" table */ + int32_t tags PACKED; /* the "public tagnames" table */ + int32_t nametable PACKED; /* name table */ +} PACKED AMX_HEADER; + +#if PAWN_CELL_SIZE==16 + #define AMX_MAGIC 0xf1e2 +#elif PAWN_CELL_SIZE==32 + #define AMX_MAGIC 0xf1e0 +#elif PAWN_CELL_SIZE==64 + #define AMX_MAGIC 0xf1e1 +#endif + +enum { + AMX_ERR_NONE, + /* reserve the first 15 error codes for exit codes of the abstract machine */ + AMX_ERR_EXIT, /* forced exit */ + AMX_ERR_ASSERT, /* assertion failed */ + AMX_ERR_STACKERR, /* stack/heap collision */ + AMX_ERR_BOUNDS, /* index out of bounds */ + AMX_ERR_MEMACCESS, /* invalid memory access */ + AMX_ERR_INVINSTR, /* invalid instruction */ + AMX_ERR_STACKLOW, /* stack underflow */ + AMX_ERR_HEAPLOW, /* heap underflow */ + AMX_ERR_CALLBACK, /* no callback, or invalid callback */ + AMX_ERR_NATIVE, /* native function failed */ + AMX_ERR_DIVIDE, /* divide by zero */ + AMX_ERR_SLEEP, /* go into sleepmode - code can be restarted */ + AMX_ERR_INVSTATE, /* invalid state for this access */ + + AMX_ERR_MEMORY = 16, /* out of memory */ + AMX_ERR_FORMAT, /* invalid file format */ + AMX_ERR_VERSION, /* file is for a newer version of the AMX */ + AMX_ERR_NOTFOUND, /* function not found */ + AMX_ERR_INDEX, /* invalid index parameter (bad entry point) */ + AMX_ERR_DEBUG, /* debugger cannot run */ + AMX_ERR_INIT, /* AMX not initialized (or doubly initialized) */ + AMX_ERR_USERDATA, /* unable to set user data field (table full) */ + AMX_ERR_INIT_JIT, /* cannot initialize the JIT */ + AMX_ERR_PARAMS, /* parameter error */ + AMX_ERR_DOMAIN, /* domain error, expression result does not fit in range */ + AMX_ERR_GENERAL, /* general error (unknown or unspecific error) */ +}; + +/* AMX_FLAG_CHAR16 0x01 no longer used */ +#define AMX_FLAG_DEBUG 0x02 /* symbolic info. available */ +#define AMX_FLAG_COMPACT 0x04 /* compact encoding */ +#define AMX_FLAG_BYTEOPC 0x08 /* opcode is a byte (not a cell) */ +#define AMX_FLAG_NOCHECKS 0x10 /* no array bounds checking; no STMT opcode */ +#define AMX_FLAG_NTVREG 0x1000 /* all native functions are registered */ +#define AMX_FLAG_JITC 0x2000 /* abstract machine is JIT compiled */ +#define AMX_FLAG_BROWSE 0x4000 /* busy browsing */ +#define AMX_FLAG_RELOC 0x8000 /* jump/call addresses relocated */ + +#define AMX_EXEC_MAIN -1 /* start at program entry point */ +#define AMX_EXEC_CONT -2 /* continue from last address */ + +#define AMX_USERTAG(a,b,c,d) ((a) | ((b)<<8) | ((long)(c)<<16) | ((long)(d)<<24)) + +#if !defined AMX_COMPACTMARGIN + #define AMX_COMPACTMARGIN 64 +#endif + +/* for native functions that use floating point parameters, the following + * two macros are convenient for casting a "cell" into a "float" type _without_ + * changing the bit pattern + */ +#if PAWN_CELL_SIZE==32 + #define amx_ftoc(f) ( * ((cell*)&f) ) /* float to cell */ + #define amx_ctof(c) ( * ((float*)&c) ) /* cell to float */ +#elif PAWN_CELL_SIZE==64 + #define amx_ftoc(f) ( * ((cell*)&f) ) /* float to cell */ + #define amx_ctof(c) ( * ((double*)&c) ) /* cell to float */ +#else + #error Unsupported cell size +#endif + +#define amx_StrParam(amx,param,result) \ + do { \ + cell *amx_cstr_; int amx_length_; \ + amx_GetAddr((amx), (param), &amx_cstr_); \ + amx_StrLen(amx_cstr_, &amx_length_); \ + if (amx_length_ > 0 && \ + ((result) = (void*)alloca((amx_length_ + 1) * sizeof(*(result)))) != NULL) \ + amx_GetString((char*)(result), amx_cstr_, sizeof(*(result))>1, amx_length_); \ + else (result) = NULL; \ + } while (0) + +uint16_t * AMXAPI amx_Align16(uint16_t *v); +uint32_t * AMXAPI amx_Align32(uint32_t *v); +#if defined _I64_MAX || defined HAVE_I64 + uint64_t * AMXAPI amx_Align64(uint64_t *v); +#endif +int AMXAPI amx_Allot(AMX *amx, int cells, cell *amx_addr, cell **phys_addr); +int AMXAPI amx_Callback(AMX *amx, cell index, cell *result, cell *params); +int AMXAPI amx_Cleanup(AMX *amx); +int AMXAPI amx_Clone(AMX *amxClone, AMX *amxSource, void *data); +int AMXAPI amx_Exec(AMX *amx, cell *retval, int index); +int AMXAPI amx_FindNative(AMX *amx, const char *name, int *index); +int AMXAPI amx_FindPublic(AMX *amx, const char *funcname, int *index); +int AMXAPI amx_FindPubVar(AMX *amx, const char *varname, cell *amx_addr); +int AMXAPI amx_FindTagId(AMX *amx, cell tag_id, char *tagname); +int AMXAPI amx_Flags(AMX *amx,uint16_t *flags); +int AMXAPI amx_GetAddr(AMX *amx,cell amx_addr,cell **phys_addr); +int AMXAPI amx_GetNative(AMX *amx, int index, char *funcname); +int AMXAPI amx_GetPublic(AMX *amx, int index, char *funcname); +int AMXAPI amx_GetPubVar(AMX *amx, int index, char *varname, cell *amx_addr); +int AMXAPI amx_GetString(char *dest,const cell *source, int use_wchar, size_t size); +int AMXAPI amx_GetTag(AMX *amx, int index, char *tagname, cell *tag_id); +int AMXAPI amx_GetUserData(AMX *amx, long tag, void **ptr); +int AMXAPI amx_Init(AMX *amx, void *program); +int AMXAPI amx_InitJIT(AMX *amx, void *reloc_table, void *native_code); +int AMXAPI amx_MemInfo(AMX *amx, long *codesize, long *datasize, long *stackheap); +int AMXAPI amx_NameLength(AMX *amx, int *length); +AMX_NATIVE_INFO * AMXAPI amx_NativeInfo(const char *name, AMX_NATIVE func); +int AMXAPI amx_NumNatives(AMX *amx, int *number); +int AMXAPI amx_NumPublics(AMX *amx, int *number); +int AMXAPI amx_NumPubVars(AMX *amx, int *number); +int AMXAPI amx_NumTags(AMX *amx, int *number); +int AMXAPI amx_Push(AMX *amx, cell value); +int AMXAPI amx_PushArray(AMX *amx, cell *amx_addr, cell **phys_addr, const cell array[], int numcells); +int AMXAPI amx_PushString(AMX *amx, cell *amx_addr, cell **phys_addr, const char *string, int pack, int use_wchar); +int AMXAPI amx_RaiseError(AMX *amx, int error); +int AMXAPI amx_Register(AMX *amx, const AMX_NATIVE_INFO *nativelist, int number); +int AMXAPI amx_Release(AMX *amx, cell amx_addr); +int AMXAPI amx_SetCallback(AMX *amx, AMX_CALLBACK callback); +int AMXAPI amx_SetDebugHook(AMX *amx, AMX_DEBUG debug); +int AMXAPI amx_SetString(cell *dest, const char *source, int pack, int use_wchar, size_t size); +int AMXAPI amx_SetUserData(AMX *amx, long tag, void *ptr); +int AMXAPI amx_StrLen(const cell *cstring, int *length); +int AMXAPI amx_UTF8Check(const char *string, int *length); +int AMXAPI amx_UTF8Get(const char *string, const char **endptr, cell *value); +int AMXAPI amx_UTF8Len(const cell *cstr, int *length); +int AMXAPI amx_UTF8Put(char *string, char **endptr, int maxchars, cell value); + +#if PAWN_CELL_SIZE==16 + #define amx_AlignCell(v) amx_Align16(v) +#elif PAWN_CELL_SIZE==32 + #define amx_AlignCell(v) amx_Align32(v) +#elif PAWN_CELL_SIZE==64 && (defined _I64_MAX || defined HAVE_I64) + #define amx_AlignCell(v) amx_Align64(v) +#else + #error Unsupported cell size +#endif + +#define amx_RegisterFunc(amx, name, func) \ + amx_Register((amx), amx_NativeInfo((name),(func)), 1); + +#if !defined AMX_NO_ALIGN + #if defined LINUX || defined __FreeBSD__ + #pragma pack() /* reset default packing */ + #elif defined MACOS && defined __MWERKS__ + #pragma options align=reset + #else + #pragma pack(pop) /* reset previous packing */ + #endif +#endif + +#ifdef __cplusplus +} +#endif + +#endif /* AMX_H_INCLUDED */ diff --git a/compiler/libpc300/amxdbg.h b/compiler/libpc300/amxdbg.h new file mode 100755 index 00000000..9f9258c4 --- /dev/null +++ b/compiler/libpc300/amxdbg.h @@ -0,0 +1,172 @@ +/* Abstract Machine for the Pawn compiler, debugger support + * + * This file contains extra definitions that are convenient for debugger + * support. + * + * Copyright (c) ITB CompuPhase, 2005 + * + * This software is provided "as-is", without any express or implied warranty. + * In no event will the authors be held liable for any damages arising from + * the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software in + * a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * 3. This notice may not be removed or altered from any source distribution. + * + * Version: $Id$ + */ + +#ifndef AMXDBG_H_INCLUDED +#define AMXDBG_H_INCLUDED + +#ifndef AMX_H_INCLUDED + #include "amx.h" +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +/* Some compilers do not support the #pragma align, which should be fine. Some + * compilers give a warning on unknown #pragmas, which is not so fine... + */ +#if defined SN_TARGET_PS2 || defined __GNUC__ + #define AMX_NO_ALIGN +#endif + +#if defined __GNUC__ + #define PACKED __attribute__((packed)) +#else + #define PACKED +#endif + +#if !defined AMX_NO_ALIGN + #if defined LINUX || defined __FreeBSD__ + #pragma pack(1) /* structures must be packed (byte-aligned) */ + #elif defined MACOS && defined __MWERKS__ + #pragma options align=mac68k + #else + #pragma pack(push) + #pragma pack(1) /* structures must be packed (byte-aligned) */ + #if defined __TURBOC__ + #pragma option -a- /* "pack" pragma for older Borland compilers */ + #endif + #endif +#endif + +typedef struct tagAMX_DBG_HDR { + int32_t size PACKED; /* size of the debug information chunk */ + uint16_t magic PACKED; /* signature, must be 0xf1ef */ + char file_version PACKED; /* file format version */ + char amx_version PACKED; /* required version of the AMX */ + int16_t flags PACKED; /* currently unused */ + int16_t files PACKED; /* number of entries in the "file" table */ + int16_t lines PACKED; /* number of entries in the "line" table */ + int16_t symbols PACKED; /* number of entries in the "symbol" table */ + int16_t tags PACKED; /* number of entries in the "tag" table */ + int16_t automatons PACKED; /* number of entries in the "automaton" table */ + int16_t states PACKED; /* number of entries in the "state" table */ +} AMX_DBG_HDR PACKED; +#define AMX_DBG_MAGIC 0xf1ef + +typedef struct tagAMX_DBG_FILE { + ucell address PACKED; /* address in the code segment where generated code (for this file) starts */ + const char name[1] PACKED; /* ASCII string, zero-terminated */ +} AMX_DBG_FILE PACKED; + +typedef struct tagAMX_DBG_LINE { + ucell address PACKED; /* address in the code segment where generated code (for this line) starts */ + int32_t line PACKED; /* line number */ +} AMX_DBG_LINE PACKED; + +typedef struct tagAMX_DBG_SYMBOL { + ucell address PACKED; /* address in the data segment or relative to the frame */ + int16_t tag PACKED; /* tag for the symbol */ + ucell codestart PACKED; /* address in the code segment from which this symbol is valid (in scope) */ + ucell codeend PACKED; /* address in the code segment until which this symbol is valid (in scope) */ + char ident PACKED; /* kind of symbol (function/variable) */ + char vclass PACKED; /* class of symbol (global/local) */ + int16_t dim PACKED; /* number of dimensions */ + const char name[1] PACKED; /* ASCII string, zero-terminated */ +} AMX_DBG_SYMBOL PACKED; + +typedef struct tagAMX_DBG_SYMDIM { + int16_t tag PACKED; /* tag for the array dimension */ + ucell size PACKED; /* size of the array dimension */ +} AMX_DBG_SYMDIM PACKED; + +typedef struct tagAMX_DBG_TAG { + int16_t tag PACKED; /* tag id */ + const char name[1] PACKED; /* ASCII string, zero-terminated */ +} AMX_DBG_TAG PACKED; + +typedef struct tagAMX_DBG_MACHINE { + int16_t automaton PACKED; /* automaton id */ + ucell address PACKED; /* address of state variable */ + const char name[1] PACKED; /* ASCII string, zero-terminated */ +} AMX_DBG_MACHINE PACKED; + +typedef struct tagAMX_DBG_STATE { + int16_t state PACKED; /* state id */ + int16_t automaton PACKED; /* automaton id */ + const char name[1] PACKED; /* ASCII string, zero-terminated */ +} AMX_DBG_STATE PACKED; + +typedef struct tagAMX_DBG { + AMX_DBG_HDR _FAR *hdr PACKED; /* points to the AMX_DBG header */ + AMX_DBG_FILE _FAR **filetbl PACKED; + AMX_DBG_LINE _FAR *linetbl PACKED; + AMX_DBG_SYMBOL _FAR **symboltbl PACKED; + AMX_DBG_TAG _FAR **tagtbl PACKED; + AMX_DBG_MACHINE _FAR **automatontbl PACKED; + AMX_DBG_STATE _FAR **statetbl PACKED; +} AMX_DBG PACKED; + +#if !defined iVARIABLE + #define iVARIABLE 1 /* cell that has an address and that can be fetched directly (lvalue) */ + #define iREFERENCE 2 /* iVARIABLE, but must be dereferenced */ + #define iARRAY 3 + #define iREFARRAY 4 /* an array passed by reference (i.e. a pointer) */ + #define iFUNCTN 9 +#endif + + +int AMXAPI dbg_FreeInfo(AMX_DBG *amxdbg); +int AMXAPI dbg_LoadInfo(AMX_DBG *amxdbg, FILE *fp); + +int AMXAPI dbg_LookupFile(AMX_DBG *amxdbg, ucell address, const char **filename); +int AMXAPI dbg_LookupFunction(AMX_DBG *amxdbg, ucell address, const char **funcname); +int AMXAPI dbg_LookupLine(AMX_DBG *amxdbg, ucell address, long *line); + +int AMXAPI dbg_GetFunctionAddress(AMX_DBG *amxdbg, const char *funcname, const char *filename, ucell *address); +int AMXAPI dbg_GetLineAddress(AMX_DBG *amxdbg, long line, const char *filename, ucell *address); +int AMXAPI dbg_GetAutomatonName(AMX_DBG *amxdbg, int automaton, const char **name); +int AMXAPI dbg_GetStateName(AMX_DBG *amxdbg, int state, const char **name); +int AMXAPI dbg_GetTagName(AMX_DBG *amxdbg, int tag, const char **name); +int AMXAPI dbg_GetVariable(AMX_DBG *amxdbg, const char *symname, ucell scopeaddr, const AMX_DBG_SYMBOL **sym); +int AMXAPI dbg_GetArrayDim(AMX_DBG *amxdbg, const AMX_DBG_SYMBOL *sym, const AMX_DBG_SYMDIM **symdim); + + +#if !defined AMX_NO_ALIGN + #if defined LINUX || defined __FreeBSD__ + #pragma pack() /* reset default packing */ + #elif defined MACOS && defined __MWERKS__ + #pragma options align=reset + #else + #pragma pack(pop) /* reset previous packing */ + #endif +#endif + +#ifdef __cplusplus +} +#endif + +#endif /* AMXDBG_H_INCLUDED */ diff --git a/compiler/libpc300/dllmain.c b/compiler/libpc300/dllmain.c new file mode 100755 index 00000000..a694d2bc --- /dev/null +++ b/compiler/libpc300/dllmain.c @@ -0,0 +1,79 @@ +/* DLL support functions for dynamically loadable extension libraries. + * + * Copyright (c) ITB CompuPhase, 2004-2005 + * + * This software is provided "as-is", without any express or implied warranty. + * In no event will the authors be held liable for any damages arising from + * the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software in + * a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * 3. This notice may not be removed or altered from any source distribution. + * + * Version: $Id$ + */ + +#if defined _UNICODE || defined __UNICODE__ || defined UNICODE +# if !defined UNICODE /* for Windows */ +# define UNICODE +# endif +# if !defined _UNICODE /* for C library */ +# define _UNICODE +# endif +#endif + +#include +#include + +#if !defined UNUSED_PARAM + #define UNUSED_PARAM(p) ((void)(p)) +#endif + +HINSTANCE hinstDLL; + +/* Especially Watcom C/C++ does not like DLLs that do not have a LibMain() + * set. Apparently, the start address is not set well, and some required + * initializations are not done. + */ +#if defined __WIN32__ || defined _WIN32 || defined WIN32 + + BOOL WINAPI DllMain(HINSTANCE hinst, DWORD dwReason, LPVOID lpRes) + { + UNUSED_PARAM(lpRes); + switch (dwReason) { + case DLL_PROCESS_ATTACH: + hinstDLL=hinst; + break; + case DLL_PROCESS_DETACH: + break; + } /* switch */ + return TRUE; + } + +#else + + int FAR PASCAL LibMain(HINSTANCE hinst, WORD wDataSeg, WORD wHeapSize, LPSTR lpszCmdLine) + { + UNUSED_PARAM(wDataSeg); + UNUSED_PARAM(wHeapSize); + UNUSED_PARAM(lpszCmdLine); + hinstDLL=hinst; + return 1; + } + + int FAR PASCAL _export WEP(int param) + { + UNUSED_PARAM(param); + return 1; + } + +#endif /* __WIN32__ */ + diff --git a/compiler/libpc300/libpawnc.c b/compiler/libpc300/libpawnc.c new file mode 100755 index 00000000..13fcc8e8 --- /dev/null +++ b/compiler/libpc300/libpawnc.c @@ -0,0 +1,334 @@ +/* LIBPAWNC.C + * + * A "glue file" for building the Pawn compiler as a DLL or shared library. + * + * Copyright (c) ITB CompuPhase, 2000-2005 + * + * This software is provided "as-is", without any express or implied warranty. + * In no event will the authors be held liable for any damages arising from + * the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software in + * a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * 3. This notice may not be removed or altered from any source distribution. + * + * Version: $Id$ + */ +#include +#include +#include +#include +#include "sc.h" + +#if defined PAWNC_DLL + +# include "dllmain.c" + +# define MAX_ARGS 100 +# if !defined UNUSED_PARAM +# define UNUSED_PARAM(p) ((void)(p)) +# endif + + static char *argv[MAX_ARGS]; + static int argc; + + LPSTR dll_skipwhite(LPSTR ptr) + { + assert(ptr!=NULL); + while (*ptr<=' ' && *ptr!='\0') + ptr++; + return ptr; + } + +#if PAWN_CELL_SIZE==32 +#define EXCOMPILER Compile32 +#else +#define EXCOMPILER Compile64 +#endif + +# if defined __WIN32__ || defined _WIN32 || defined WIN32 || defined __NT__ + __declspec (dllexport) + void EXCOMPILER(HWND hwnd, HINSTANCE hinst, LPSTR lpCommandLine, int nCmdShow) +# else + void extern EXCOMPILER(HWND hwnd, HINSTANCE hinst, LPSTR lpCommandLine, int nCmdShow) +# endif + { + char RootPath[_MAX_PATH]; + LPSTR ptr; + + /* RUNDLL32 may have passed us a HWND and a HINSTANCE, but we can hardly + * trust these. They may not contain values that we can use. + */ + + /* the root path in argv[0] */ + GetModuleFileName(hinstDLL, RootPath, sizeof RootPath); + argv[argc++]=RootPath; + + /* all other options */ + assert(lpCommandLine!=NULL); + ptr=dll_skipwhite(lpCommandLine); + while (*ptr!='\0') { + if (*ptr=='"') { + argv[argc++]=ptr+1; + while (*ptr!='"' && *ptr!='\0') + ptr++; + } else { + argv[argc++]=ptr; + while (*ptr>' ') + ptr++; + } /* if */ + if (*ptr!='\0') + *ptr++='\0'; + ptr=dll_skipwhite(ptr); + } /* while */ + pc_compile(argc,argv); + UNUSED_PARAM(hwnd); + UNUSED_PARAM(hinst); + UNUSED_PARAM(nCmdShow); + } + +#else /* PAWNC_DLL */ + + +#endif /* PAWNC_DLL */ + + +/* pc_printf() + * Called for general purpose "console" output. This function prints general + * purpose messages; errors go through pc_error(). The function is modelled + * after printf(). + */ +int pc_printf(const char *message,...) +{ +#if PAWN_CELL_SIZE==32 + int ret; + va_list argptr; + + va_start(argptr,message); + ret=vprintf(message,argptr); + va_end(argptr); + + return ret; +#else + return 1; +#endif +} + +/* pc_error() + * Called for producing error output. + * number the error number (as documented in the manual) + * message a string describing the error with embedded %d and %s tokens + * filename the name of the file currently being parsed + * firstline the line number at which the expression started on which + * the error was found, or -1 if there is no "starting line" + * lastline the line number at which the error was detected + * argptr a pointer to the first of a series of arguments (for macro + * "va_arg") + * Return: + * If the function returns 0, the parser attempts to continue compilation. + * On a non-zero return value, the parser aborts. + */ +int pc_error(int number,char *message,char *filename,int firstline,int lastline,va_list argptr) +{ +#if PAWN_CELL_SIZE==32 +static char *prefix[3]={ "error", "fatal error", "warning" }; + + if (number!=0) { + char *pre; + + pre=prefix[number/100]; + if (firstline>=0) + fprintf(stderr,"%s(%d -- %d) : %s %03d: ",filename,firstline,lastline,pre,number); + else + fprintf(stderr,"%s(%d) : %s %03d: ",filename,lastline,pre,number); + } /* if */ + vfprintf(stderr,message,argptr); + fflush(stderr); +#endif + return 0; +} + +/* pc_opensrc() + * Opens a source file (or include file) for reading. The "file" does not have + * to be a physical file, one might compile from memory. + * filename the name of the "file" to read from + * Return: + * The function must return a pointer, which is used as a "magic cookie" to + * all I/O functions. When failing to open the file for reading, the + * function must return NULL. + * Note: + * Several "source files" may be open at the same time. Specifically, one + * file can be open for reading and another for writing. + */ +void *pc_opensrc(char *filename) +{ + return fopen(filename,"rt"); +} + +/* pc_createsrc() + * Creates/overwrites a source file for writing. The "file" does not have + * to be a physical file, one might compile from memory. + * filename the name of the "file" to create + * Return: + * The function must return a pointer, which is used as a "magic cookie" to + * all I/O functions. When failing to open the file for reading, the + * function must return NULL. + * Note: + * Several "source files" may be open at the same time. Specifically, one + * file can be open for reading and another for writing. + */ +void *pc_createsrc(char *filename) +{ + return fopen(filename,"wt"); +} + +/* pc_closesrc() + * Closes a source file (or include file). The "handle" parameter has the + * value that pc_opensrc() returned in an earlier call. + */ +void pc_closesrc(void *handle) +{ + assert(handle!=NULL); + fclose((FILE*)handle); +} + +/* pc_resetsrc() + * "position" may only hold a pointer that was previously obtained from + * pc_getpossrc() + */ +void pc_resetsrc(void *handle,void *position) +{ + assert(handle!=NULL); + fsetpos((FILE*)handle,(fpos_t *)position); +} + +/* pc_readsrc() + * Reads a single line from the source file (or up to a maximum number of + * characters if the line in the input file is too long). + */ +char *pc_readsrc(void *handle,unsigned char *target,int maxchars) +{ + return fgets((char*)target,maxchars,(FILE*)handle); +} + +/* pc_writesrc() + * Writes to to the source file. There is no automatic line ending; to end a + * line, write a "\n". + */ +int pc_writesrc(void *handle,unsigned char *source) +{ + return fputs((char*)source,(FILE*)handle) >= 0; +} + +void *pc_getpossrc(void *handle) +{ + static fpos_t lastpos; /* may need to have a LIFO stack of such positions */ + + fgetpos((FILE*)handle,&lastpos); + return &lastpos; +} + +int pc_eofsrc(void *handle) +{ + return feof((FILE*)handle); +} + +/* should return a pointer, which is used as a "magic cookie" to all I/O + * functions; return NULL for failure + */ +void *pc_openasm(char *filename) +{ + #if defined __MSDOS__ || defined SC_LIGHT + return fopen(filename,"w+t"); + #else + return mfcreate(filename); + #endif +} + +void pc_closeasm(void *handle, int deletefile) +{ + #if defined __MSDOS__ || defined SC_LIGHT + if (handle!=NULL) + fclose((FILE*)handle); + if (deletefile) + remove(outfname); + #else + if (handle!=NULL) { + if (!deletefile) + mfdump((MEMFILE*)handle); + mfclose((MEMFILE*)handle); + } /* if */ + #endif +} + +void pc_resetasm(void *handle) +{ + assert(handle!=NULL); + #if defined __MSDOS__ || defined SC_LIGHT + fflush((FILE*)handle); + fseek((FILE*)handle,0,SEEK_SET); + #else + mfseek((MEMFILE*)handle,0,SEEK_SET); + #endif +} + +int pc_writeasm(void *handle,char *string) +{ + #if defined __MSDOS__ || defined SC_LIGHT + return fputs(string,(FILE*)handle) >= 0; + #else + return mfputs((MEMFILE*)handle,string); + #endif +} + +char *pc_readasm(void *handle, char *string, int maxchars) +{ + #if defined __MSDOS__ || defined SC_LIGHT + return fgets(string,maxchars,(FILE*)handle); + #else + return mfgets((MEMFILE*)handle,string,maxchars); + #endif +} + +/* Should return a pointer, which is used as a "magic cookie" to all I/O + * functions; return NULL for failure. + */ +void *pc_openbin(char *filename) +{ + return fopen(filename,"wb"); +} + +void pc_closebin(void *handle,int deletefile) +{ + fclose((FILE*)handle); + if (deletefile) + remove(binfname); +} + +/* pc_resetbin() + * Can seek to any location in the file. + * The offset is always from the start of the file. + */ +void pc_resetbin(void *handle,long offset) +{ + fflush((FILE*)handle); + fseek((FILE*)handle,offset,SEEK_SET); +} + +int pc_writebin(void *handle,void *buffer,int size) +{ + return (int)fwrite(buffer,1,size,(FILE*)handle) == size; +} + +long pc_lengthbin(void *handle) +{ + return ftell((FILE*)handle); +} diff --git a/compiler/libpc300/libpawnc.def b/compiler/libpc300/libpawnc.def new file mode 100755 index 00000000..b4fe172c --- /dev/null +++ b/compiler/libpc300/libpawnc.def @@ -0,0 +1,8 @@ +LIBRARY PAWNC +DESCRIPTION 'Pawn compiler' + +EXPORTS Compile + pc_compile + pc_addconstant + pc_addtag + pc_enablewarning diff --git a/compiler/libpc300/libpawnc.lbc b/compiler/libpc300/libpawnc.lbc new file mode 100755 index 00000000..c8fab17d --- /dev/null +++ b/compiler/libpc300/libpawnc.lbc @@ -0,0 +1,5 @@ +++_Compile@16 .libpawnc .Compile +++pc_compile .libpawnc .pc_compile +++pc_addconstant .libpawnc .pc_addconstant +++pc_addtag .libpawnc .pc_addtag +++pc_enablewarning .libpawnc .pc_enablewarning \ No newline at end of file diff --git a/compiler/libpc300/libpawnc.rc b/compiler/libpc300/libpawnc.rc new file mode 100755 index 00000000..5cc4d271 --- /dev/null +++ b/compiler/libpc300/libpawnc.rc @@ -0,0 +1,62 @@ +#include +#if defined WIN32 || defined _WIN32 || defined __WIN32__ +# include +#else +# include +#endif + +AppIcon ICON "pawn.ico" + +/* Version information + * + * All strings MUST have an explicit \0. See the Windows SDK documentation + * for details on version information and the VERSIONINFO structure. + */ +#define VERSION 3 +#define REVISION 0 +#define BUILD 0 +#define VERSIONSTR "3.0.0\0" +#if defined STAND_ALONE +#define VERSIONNAME "pawncc.exe\0" +#define VERSIONDESCRIPTION "Pawn Compiler\0" +#define VERSIONPRODUCTNAME "pawncc\0" +#else +#define VERSIONNAME "libpawnc.dll\0" +#define VERSIONDESCRIPTION "Pawn Compiler library\0" +#define VERSIONPRODUCTNAME "libpawnc\0" +#endif +#define VERSIONCOMPANYNAME "ITB CompuPhase\0" +#define VERSIONCOPYRIGHT "Copyright \251 1998-2005 ITB CompuPhase\0" + +VS_VERSION_INFO VERSIONINFO +FILEVERSION VERSION, REVISION, BUILD, 0 +PRODUCTVERSION VERSION, REVISION, BUILD, 0 +FILEFLAGSMASK 0x0000003FL +FILEFLAGS 0 +#if defined(WIN32) + FILEOS VOS__WINDOWS32 +#else + FILEOS VOS__WINDOWS16 +#endif +FILETYPE VFT_DLL +BEGIN + BLOCK "StringFileInfo" + BEGIN + BLOCK "040904E4" + BEGIN + VALUE "CompanyName", VERSIONCOMPANYNAME + VALUE "FileDescription", VERSIONDESCRIPTION + VALUE "FileVersion", VERSIONSTR + VALUE "InternalName", VERSIONNAME + VALUE "LegalCopyright", VERSIONCOPYRIGHT + VALUE "OriginalFilename", VERSIONNAME + VALUE "ProductName", VERSIONPRODUCTNAME + VALUE "ProductVersion", VERSIONSTR + END + END + + BLOCK "VarFileInfo" + BEGIN + VALUE "Translation", 0x409, 1252 + END +END diff --git a/compiler/libpc300/libpc300.sln b/compiler/libpc300/libpc300.sln new file mode 100755 index 00000000..9ceb4d8b --- /dev/null +++ b/compiler/libpc300/libpc300.sln @@ -0,0 +1,27 @@ +Microsoft Visual Studio Solution File, Format Version 8.00 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "libpc300", "libpc300.vcproj", "{19B72687-080B-437A-917A-12AEB0031635}" + ProjectSection(ProjectDependencies) = postProject + EndProjectSection +EndProject +Global + GlobalSection(SolutionConfiguration) = preSolution + Debug = Debug + Release = Release + Release32 = Release32 + Release64 = Release64 + EndGlobalSection + GlobalSection(ProjectConfiguration) = postSolution + {19B72687-080B-437A-917A-12AEB0031635}.Debug.ActiveCfg = Release|Win32 + {19B72687-080B-437A-917A-12AEB0031635}.Debug.Build.0 = Release|Win32 + {19B72687-080B-437A-917A-12AEB0031635}.Release.ActiveCfg = Release|Win32 + {19B72687-080B-437A-917A-12AEB0031635}.Release.Build.0 = Release|Win32 + {19B72687-080B-437A-917A-12AEB0031635}.Release32.ActiveCfg = Release32|Win32 + {19B72687-080B-437A-917A-12AEB0031635}.Release32.Build.0 = Release32|Win32 + {19B72687-080B-437A-917A-12AEB0031635}.Release64.ActiveCfg = Release64|Win32 + {19B72687-080B-437A-917A-12AEB0031635}.Release64.Build.0 = Release64|Win32 + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + EndGlobalSection + GlobalSection(ExtensibilityAddIns) = postSolution + EndGlobalSection +EndGlobal diff --git a/compiler/libpc300/libpc300.vcproj b/compiler/libpc300/libpc300.vcproj new file mode 100755 index 00000000..87880631 --- /dev/null +++ b/compiler/libpc300/libpc300.vcproj @@ -0,0 +1,276 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/compiler/libpc300/osdefs.h b/compiler/libpc300/osdefs.h new file mode 100755 index 00000000..ef0e068d --- /dev/null +++ b/compiler/libpc300/osdefs.h @@ -0,0 +1,94 @@ +/* __MSDOS__ set when compiling for DOS (not Windows) + * _Windows set when compiling for any version of Microsoft Windows + * __WIN32__ set when compiling for Windows95 or WindowsNT (32 bit mode) + * __32BIT__ set when compiling in 32-bit "flat" mode (DOS or Windows) + * + * Copyright 1998-2005, ITB CompuPhase, The Netherlands. + * info@compuphase.com. + */ + +#ifndef _OSDEFS_H +#define _OSDEFS_H + +/* Every compiler uses different "default" macros to indicate the mode + * it is in. Throughout the source, we use the Borland C++ macros, so + * the macros of Watcom C/C++ and Microsoft Visual C/C++ are mapped to + * those of Borland C++. + */ +#if defined(__WATCOMC__) +# if defined(__WINDOWS__) || defined(__NT__) +# define _Windows 1 +# endif +# ifdef __386__ +# define __32BIT__ 1 +# endif +# if defined(_Windows) && defined(__32BIT__) +# define __WIN32__ 1 +# endif +#elif defined(_MSC_VER) +# if defined(_WINDOWS) || defined(_WIN32) +# define _Windows 1 +# endif +# ifdef _WIN32 +# define __WIN32__ 1 +# define __32BIT__ 1 +# endif +#endif + +#if defined __FreeBSD__ + #include +#elif defined LINUX + #include +#endif + +/* Linux NOW has these */ +#if !defined BIG_ENDIAN + #define BIG_ENDIAN 4321 +#endif +#if !defined LITTLE_ENDIAN + #define LITTLE_ENDIAN 1234 +#endif + +/* educated guess, BYTE_ORDER is undefined, i386 is common => little endian */ +#if !defined BYTE_ORDER + #if defined UCLINUX + #define BYTE_ORDER BIG_ENDIAN + #else + #define BYTE_ORDER LITTLE_ENDIAN + #endif +#endif + +#if defined __MSDOS__ || defined __WIN32__ || defined _Windows + #define DIRSEP_CHAR '\\' +#elif defined macintosh + #define DIRSEP_CHAR ':' +#else + #define DIRSEP_CHAR '/' /* directory separator character */ +#endif + +/* _MAX_PATH is sometimes called differently and it may be in limits.h instead + * stdio.h. + */ +#if !defined _MAX_PATH + /* not defined, perhaps stdio.h was not included */ + #include + #if !defined _MAX_PATH + /* still undefined, try a common alternative name */ + #if defined MAX_PATH + #define _MAX_PATH MAX_PATH + #else + /* no _MAX_PATH and no MAX_PATH, perhaps it is in limits.h */ + #include + #if defined PATH_MAX + #define _MAX_PATH PATH_MAX + #elif defined _POSIX_PATH_MAX + #define _MAX_PATH _POSIX_PATH_MAX + #else + /* everything failed, actually we have a problem here... */ + #define _MAX_PATH 1024 + #endif + #endif + #endif +#endif + +#endif /* _OSDEFS_H */ diff --git a/compiler/libpc300/pawn.ico b/compiler/libpc300/pawn.ico new file mode 100755 index 0000000000000000000000000000000000000000..7a6ab60bc5be72862ee6fee5e3bd4585a6644f36 GIT binary patch literal 8478 zcmeI1J!~6C7RN`X0-1jOasId_?ko&&fT6+xS*394Ykk$=3c7X~x^Y9`0H!do zU5Z1HN;p`{=BPq$&)d5&1?24JqK^}mD=Z*^=YId$U22!&QoJ_M$p4+M_ujmDGn|?C zY|N7R#N4}QLi%CNm_Jc!G}8KCJ~8GaZH=tq%Ozvn7slMWmG=K|)tJAotDZ9a`-(At z{Jk+>(k+pH21^DarFxo8f4gen)6ELqtg^eiYj#Jw=KFt+&GPcHxq9`gSzTQ<&1TbF zzkc1^xN*bWxpT+dy?fU@di2OVeE86`S}k+`{(bZ8*)#J@a`{jUh0sF9Qga{z2~dLt z4n!aU9*D>S2O^LF55#DJ0})7o2NGt10})7oSKU(w34kK}Ild7{fCo~*0tX_H01u>; z1r9_Y0Uk(E3mk|*0z8oN7B~=r1bASXkOL7&fCmU?WEcJ-z6tO^leWNt2qeG*8Nvbw zB9H(X#sUW-kN^*4EDIu#01sq53nGvJ&xi?M88O13`b%V7S%x~4j2%Hi8^*T4iHDslm->rW7>Dp%>D(KSYTU|S<<~Y9H>4gMKU}!g6jn!&~>nXLqDM!1_ar9Z`03QPV7AbH@CFu7n=~qPG zkK?%Cha>0&{b?orIHiUm4h%tjYmSLh`eA(tLnozfwJXIN6HA%VXaI4mi7HCm4b@4i z^jsj#=-buiL`>XIiCay`1IIjdj5&e4t)(JiOE~7tR%&*7@vu%BjpBH#=i2kdn?AEm z(_2lY-|KLy%}McFC6Tgbe0rVM9BSs9r#|b;9GJ{pdQ*?Xh6tKy#!u#x(>n-5Nb~jr z)G$czAfKCcku(T9wC5I4Cdlg{f2+gRSwuQC+I1av#093ANZaZ4CB60Sq~(HwbZ^bw z_+$}bZ{9lzudfb*Gj@RaD}) z=)>_3eFeo-bDdM2Y4qZ$_UdYVI2cs;KHmR$jdzguAN3?6u|f}&kBzyxNS@Q@`L7v2 z`8VRv^IU#jpU(4s->r$w``wjtp6QwEJITj4Q@Ta-TyE7kdAnIk*F{Bwv22Jl9)b;-@ZAnUT6`<8$e!;x%6SscZ)Mi1Hip8p-9gUHYl`lD||+PI-Gz2fnEdQ#u9TLbBOHPM`jHeB)h0XL0$+j2eY8wvM(V1Kk8GoY{#3b)o3awbNH622m-o?cKW=)ma&+)y zfB)d{c&uY!Ivsw!zV6(5yVYp44}PBRHK%EaQ^7ZE`8` zmYZptyuXj9Ubx2d=1Kkm>&-BIslQTtsQQE&=E(1FDIRowZ{+gB(pSgR#@ZPy{;buL zh$o@Da)!qWp9GTGQ#cB-cu~xbSFyvjzmYL}iUaN75HFq0KaPnX5N;?x#qzn64@dtZ ze!(b(wo;Mk^C;fxm)c5|z52QML2%EzxX5yMi;(*~4Y}NMd*z}Fa($;eUDWkZ0-2AM8fMoHg+Orq*V!GLeM( zMk9Og&8iQ#Z8LqniN3aJH}u+?B&!EvZBng$jzmVxQ*CoN+}_q}FHNdLkyjaT6Zw_BT&6^Jd!x`T4bQX{MQx*?jp~gvZ9@Owr|Jc5XZ16`(s)ewY&tN|S*%NVaRAGI zxSyE|b+ +#include +#include +#if defined __BORLANDC__ && defined _Windows && !(defined __32BIT__ || defined __WIN32__) + /* setjmp() and longjmp() not well supported in 16-bit windows */ + #include + typedef int jmp_buf[9]; + #define setjmp(b) Catch(b) + #define longjmp(b,e) Throw(b,e) +#else + #include +#endif +#include "osdefs.h" +#include "amx.h" + +/* Note: the "cell" and "ucell" types are defined in AMX.H */ + +#define PUBLIC_CHAR '@' /* character that defines a function "public" */ +#define CTRL_CHAR '^' /* default control character */ +#define sCHARBITS 8 /* size of a packed character */ + +#define sDIMEN_MAX 3 /* maximum number of array dimensions */ +#define sLINEMAX 511 /* input line length (in characters) */ +#define sCOMP_STACK 32 /* maximum nesting of #if .. #endif sections */ +#define sDEF_LITMAX 500 /* initial size of the literal pool, in "cells" */ +#define sDEF_AMXSTACK 4096 /* default stack size for AMX files */ +#define PREPROC_TERM '\x7f'/* termination character for preprocessor expressions (the "DEL" code) */ +#define sDEF_PREFIX "default.inc" /* default prefix filename */ + +typedef union { + void *pv; /* e.g. a name */ + int i; +} stkitem; /* type of items stored on the compiler stack */ + +typedef struct s_arginfo { /* function argument info */ + char name[sNAMEMAX+1]; + char ident; /* iVARIABLE, iREFERENCE, iREFARRAY or iVARARGS */ + char usage; /* uCONST */ + int *tags; /* argument tag id. list */ + int numtags; /* number of tags in the tag list */ + int dim[sDIMEN_MAX]; + int idxtag[sDIMEN_MAX]; + int numdim; /* number of dimensions */ + unsigned char hasdefault; /* bit0: is there a default value? bit6: "tagof"; bit7: "sizeof" */ + union { + cell val; /* default value */ + struct { + char *symname; /* name of another symbol */ + short level; /* indirection level for that symbol */ + } size; /* used for "sizeof" default value */ + struct { + cell *data; /* values of default array */ + int size; /* complete length of default array */ + int arraysize; /* size to reserve on the heap */ + cell addr; /* address of the default array in the data segment */ + } array; + } defvalue; /* default value, or pointer to default array */ + int defvalue_tag; /* tag of the default value */ +} arginfo; + +/* Equate table, tagname table, library table */ +typedef struct s_constvalue { + struct s_constvalue *next; + char name[sNAMEMAX+1]; + cell value; + short index; /* index level, for constants referring to array sizes/tags + * automaton id. for states and automatons */ +} constvalue; + +/* Symbol table format + * + * The symbol name read from the input file is stored in "name", the + * value of "addr" is written to the output file. The address in "addr" + * depends on the class of the symbol: + * global offset into the data segment + * local offset relative to the stack frame + * label generated hexadecimal number + * function offset into code segment + */ +typedef struct s_symbol { + struct s_symbol *next; + struct s_symbol *parent; /* hierarchical types (multi-dimensional arrays) */ + char name[sNAMEMAX+1]; + uint32_t hash; /* value derived from name, for quicker searching */ + cell addr; /* address or offset (or value for constant, index for native function) */ + cell codeaddr; /* address (in the code segment) where the symbol declaration starts */ + char vclass; /* sLOCAL if "addr" refers to a local symbol */ + char ident; /* see below for possible values */ + char usage; /* see below for possible values */ + int compound; /* compound level (braces nesting level) */ + int tag; /* tagname id */ + int fieldtag; /* enumeration fields, where a size is attached to the field */ + union { + int declared; /* label: how many local variables are declared */ + int idxtag; /* array & enum: tag of array indices or the enum item */ + constvalue *lib; /* native function: library it is part of */ + long stacksize; /* normal/public function: stack requirements */ + } x; /* 'x' for 'extra' */ + union { + arginfo *arglist; /* types of all parameters for functions */ + constvalue *enumlist;/* list of names for the "root" of an enumeration */ + struct { + cell length; /* arrays: length (size) */ + short level; /* number of dimensions below this level */ + } array; + } dim; /* for 'dimension', both functions and arrays */ + constvalue *states; /* list of state function addresses */ + int fnumber; /* static global variables: file number in which the declaration is visible */ + struct s_symbol **refer; /* referrer list, functions that "use" this symbol */ + int numrefers; /* number of entries in the referrer list */ + char *documentation; /* optional documentation string */ +} symbol; + + +/* Possible entries for "ident". These are used in the "symbol", "value" + * and arginfo structures. Not every constant is valid for every use. + * In an argument list, the list is terminated with a "zero" ident; labels + * cannot be passed as function arguments, so the value 0 is overloaded. + */ +#define iLABEL 0 +#define iVARIABLE 1 /* cell that has an address and that can be fetched directly (lvalue) */ +#define iREFERENCE 2 /* iVARIABLE, but must be dereferenced */ +#define iARRAY 3 +#define iREFARRAY 4 /* an array passed by reference (i.e. a pointer) */ +#define iARRAYCELL 5 /* array element, cell that must be fetched indirectly */ +#define iARRAYCHAR 6 /* array element, character from cell from array */ +#define iEXPRESSION 7 /* expression result, has no address (rvalue) */ +#define iCONSTEXPR 8 /* constant expression (or constant symbol) */ +#define iFUNCTN 9 +#define iREFFUNC 10 +#define iVARARGS 11 /* function specified ... as argument(s) */ + +/* Possible entries for "usage" + * + * This byte is used as a serie of bits, the syntax is different for + * functions and other symbols: + * + * VARIABLE + * bits: 0 (uDEFINE) the variable is defined in the source file + * 1 (uREAD) the variable is "read" (accessed) in the source file + * 2 (uWRITTEN) the variable is altered (assigned a value) + * 3 (uCONST) the variable is constant (may not be assigned to) + * 4 (uPUBLIC) the variable is public + * 6 (uSTOCK) the variable is discardable (without warning) + * + * FUNCTION + * bits: 0 (uDEFINE) the function is defined ("implemented") in the source file + * 1 (uREAD) the function is invoked in the source file + * 2 (uRETVALUE) the function returns a value (or should return a value) + * 3 (uPROTOTYPED) the function was prototyped + * 4 (uPUBLIC) the function is public + * 5 (uNATIVE) the function is native + * 6 (uSTOCK) the function is discardable (without warning) + * 7 (uMISSING) the function is not implemented in this source file + * + * CONSTANT + * bits: 0 (uDEFINE) the symbol is defined in the source file + * 1 (uREAD) the constant is "read" (accessed) in the source file + * 2 (uWRITTEN) redundant, but may be set for constants passed by reference + * 3 (uPREDEF) the constant is pre-defined and should be kept between passes + * 5 (uENUMROOT) the constant is the "root" of an enumeration + * 6 (uENUMFIELD) the constant is a field in a named enumeration + */ +#define uDEFINE 0x01 +#define uREAD 0x02 +#define uWRITTEN 0x04 +#define uRETVALUE 0x04 /* function returns (or should return) a value */ +#define uCONST 0x08 +#define uPROTOTYPED 0x08 +#define uPREDEF 0x08 /* constant is pre-defined */ +#define uPUBLIC 0x10 +#define uNATIVE 0x20 +#define uENUMROOT 0x20 +#define uSTOCK 0x40 +#define uENUMFIELD 0x40 +#define uMISSING 0x80 +/* uRETNONE is not stored in the "usage" field of a symbol. It is + * used during parsing a function, to detect a mix of "return;" and + * "return value;" in a few special cases. + */ +#define uRETNONE 0x10 + +#define uTAGOF 0x40 /* set in the "hasdefault" field of the arginfo struct */ +#define uSIZEOF 0x80 /* set in the "hasdefault" field of the arginfo struct */ + +#define uMAINFUNC "main" +#define uENTRYFUNC "entry" + +#define sGLOBAL 0 /* global/local variable/constant class */ +#define sLOCAL 1 +#define sSTATIC 2 /* global life, local scope */ + +typedef struct s_value { + symbol *sym; /* symbol in symbol table, NULL for (constant) expression */ + cell constval; /* value of the constant expression (if ident==iCONSTEXPR) + * also used for the size of a literal array */ + int tag; /* tagname id (of the expression) */ + char ident; /* iCONSTEXPR, iVARIABLE, iARRAY, iARRAYCELL, + * iEXPRESSION or iREFERENCE */ + char boolresult; /* boolean result for relational operators */ + cell *arrayidx; /* last used array indices, for checking self assignment */ +} value; + +/* "while" statement queue (also used for "for" and "do - while" loops) */ +enum { + wqBRK, /* used to restore stack for "break" */ + wqCONT, /* used to restore stack for "continue" */ + wqLOOP, /* loop start label number */ + wqEXIT, /* loop exit label number (jump if false) */ + /* --- */ + wqSIZE /* "while queue" size */ +}; +#define wqTABSZ (24*wqSIZE) /* 24 nested loop statements */ + +enum { + statIDLE, /* not compiling yet */ + statFIRST, /* first pass */ + statWRITE, /* writing output */ + statSKIP, /* skipping output */ +}; + +typedef struct s_stringlist { + struct s_stringlist *next; + char *line; +} stringlist; + +typedef struct s_stringpair { + struct s_stringpair *next; + char *first; + char *second; + int matchlength; +} stringpair; + +/* macros for code generation */ +#define opcodes(n) ((n)*sizeof(cell)) /* opcode size */ +#define opargs(n) ((n)*sizeof(cell)) /* size of typical argument */ + +/* Tokens recognized by lex() + * Some of these constants are assigned as well to the variable "lastst" + */ +#define tFIRST 256 /* value of first multi-character operator */ +#define tMIDDLE 280 /* value of last multi-character operator */ +#define tLAST 325 /* value of last multi-character match-able token */ +/* multi-character operators */ +#define taMULT 256 /* *= */ +#define taDIV 257 /* /= */ +#define taMOD 258 /* %= */ +#define taADD 259 /* += */ +#define taSUB 260 /* -= */ +#define taSHL 261 /* <<= */ +#define taSHRU 262 /* >>>= */ +#define taSHR 263 /* >>= */ +#define taAND 264 /* &= */ +#define taXOR 265 /* ^= */ +#define taOR 266 /* |= */ +#define tlOR 267 /* || */ +#define tlAND 268 /* && */ +#define tlEQ 269 /* == */ +#define tlNE 270 /* != */ +#define tlLE 271 /* <= */ +#define tlGE 272 /* >= */ +#define tSHL 273 /* << */ +#define tSHRU 274 /* >>> */ +#define tSHR 275 /* >> */ +#define tINC 276 /* ++ */ +#define tDEC 277 /* -- */ +#define tELLIPS 278 /* ... */ +#define tDBLDOT 279 /* .. */ +#define tDBLCOLON 280 /* :: */ +/* reserved words (statements) */ +#define tASSERT 281 +#define tBREAK 282 +#define tCASE 283 +#define tCHAR 284 +#define tCONST 285 +#define tCONTINUE 286 +#define tDEFAULT 287 +#define tDEFINED 288 +#define tDO 289 +#define tELSE 290 +#define tENUM 291 +#define tEXIT 292 +#define tFOR 293 +#define tFORWARD 294 +#define tGOTO 295 +#define tIF 296 +#define tNATIVE 297 +#define tNEW 298 +#define tOPERATOR 299 +#define tPUBLIC 300 +#define tRETURN 301 +#define tSIZEOF 302 +#define tSLEEP 303 +#define tSTATE 304 +#define tSTATIC 305 +#define tSTOCK 306 +#define tSWITCH 307 +#define tTAGOF 308 +#define tWHILE 309 +/* compiler directives */ +#define tpASSERT 310 /* #assert */ +#define tpDEFINE 311 +#define tpELSE 312 /* #else */ +#define tpELSEIF 313 /* #elseif */ +#define tpEMIT 314 +#define tpENDIF 315 +#define tpENDINPUT 316 +#define tpENDSCRPT 317 +#define tpERROR 318 +#define tpFILE 319 +#define tpIF 320 /* #if */ +#define tINCLUDE 321 +#define tpLINE 322 +#define tpPRAGMA 323 +#define tpTRYINCLUDE 324 +#define tpUNDEF 325 +/* semicolon is a special case, because it can be optional */ +#define tTERM 326 /* semicolon or newline */ +#define tENDEXPR 327 /* forced end of expression */ +/* other recognized tokens */ +#define tNUMBER 328 /* integer number */ +#define tRATIONAL 329 /* rational number */ +#define tSYMBOL 330 +#define tLABEL 331 +#define tSTRING 332 +#define tEXPR 333 /* for assigment to "lastst" only */ + +/* (reversed) evaluation of staging buffer */ +#define sSTARTREORDER 0x01 +#define sENDREORDER 0x02 +#define sEXPRSTART 0x80 /* top bit set, rest is free */ +#define sMAXARGS 127 /* relates to the bit pattern of sEXPRSTART */ + +#define sDOCSEP 0x01 /* to separate documentation comments between functions */ + +/* codes for ffabort() */ +#define xEXIT 1 /* exit code in PRI */ +#define xASSERTION 2 /* abort caused by failing assertion */ +#define xSTACKERROR 3 /* stack/heap overflow */ +#define xBOUNDSERROR 4 /* array index out of bounds */ +#define xMEMACCESS 5 /* data access error */ +#define xINVINSTR 6 /* invalid instruction */ +#define xSTACKUNDERFLOW 7 /* stack underflow */ +#define xHEAPUNDERFLOW 8 /* heap underflow */ +#define xCALLBACKERR 9 /* no, or invalid, callback */ +#define xSLEEP 12 /* sleep, exit code in PRI, tag in ALT */ + +/* Miscellaneous */ +#if !defined TRUE + #define FALSE 0 + #define TRUE 1 +#endif +#define sIN_CSEG 1 /* if parsing CODE */ +#define sIN_DSEG 2 /* if parsing DATA */ +#define sCHKBOUNDS 1 /* bit position in "debug" variable: check bounds */ +#define sSYMBOLIC 2 /* bit position in "debug" variable: symbolic info */ +#define sNOOPTIMIZE 4 /* bit position in "debug" variable: no optimization */ +#define sRESET 0 /* reset error flag */ +#define sFORCESET 1 /* force error flag on */ +#define sEXPRMARK 2 /* mark start of expression */ +#define sEXPRRELEASE 3 /* mark end of expression */ + +typedef enum s_regid { + sPRI, /* indicates the primary register */ + sALT, /* indicates the secundary register */ +} regid; + +typedef enum s_optmark { + sEXPR, /* end of expression (for expressions that form a statement) */ + sPARM, /* end of parameter (in a function parameter list) */ + sLDECL, /* start of local declaration (variable) */ +} optmark; + +#if INT_MAX<0x8000u + #define PUBLICTAG 0x8000u + #define FIXEDTAG 0x4000u +#else + #define PUBLICTAG 0x80000000Lu + #define FIXEDTAG 0x40000000Lu +#endif +#define TAGMASK (~PUBLICTAG) + + +/* interface functions */ +#if defined __cplusplus + extern "C" { +#endif + +/* + * Functions you call from the "driver" program + */ +int pc_compile(int argc, char **argv); +int pc_addconstant(char *name,cell value,int tag); +int pc_addtag(char *name); +int pc_enablewarning(int number,int enable); + +/* + * Functions called from the compiler (to be implemented by you) + */ + +/* general console output */ +int pc_printf(const char *message,...); + +/* error report function */ +int pc_error(int number,char *message,char *filename,int firstline,int lastline,va_list argptr); + +/* input from source file */ +void *pc_opensrc(char *filename); /* reading only */ +void *pc_createsrc(char *filename); +void pc_closesrc(void *handle); /* never delete */ +void pc_resetsrc(void *handle,void *position); /* reset to a position marked earlier */ +char *pc_readsrc(void *handle,unsigned char *target,int maxchars); +int pc_writesrc(void *handle,unsigned char *source); +void *pc_getpossrc(void *handle); /* mark the current position */ +int pc_eofsrc(void *handle); + +/* output to intermediate (.ASM) file */ +void *pc_openasm(char *filename); /* read/write */ +void pc_closeasm(void *handle,int deletefile); +void pc_resetasm(void *handle); +int pc_writeasm(void *handle,char *str); +char *pc_readasm(void *handle,char *target,int maxchars); + +/* output to binary (.AMX) file */ +void *pc_openbin(char *filename); +void pc_closebin(void *handle,int deletefile); +void pc_resetbin(void *handle,long offset); +int pc_writebin(void *handle,void *buffer,int size); +long pc_lengthbin(void *handle); /* return the length of the file */ + +#if defined __cplusplus + } +#endif + + +/* by default, functions and variables used in throughout the compiler + * files are "external" + */ +#if !defined SC_FUNC + #define SC_FUNC +#endif +#if !defined SC_VDECL + #define SC_VDECL extern +#endif +#if !defined SC_VDEFINE + #define SC_VDEFINE +#endif + +/* function prototypes in SC1.C */ +SC_FUNC void set_extension(char *filename,char *extension,int force); +SC_FUNC symbol *fetchfunc(char *name,int tag); +SC_FUNC char *operator_symname(char *symname,char *opername,int tag1,int tag2,int numtags,int resulttag); +SC_FUNC char *funcdisplayname(char *dest,char *funcname); +SC_FUNC int constexpr(cell *val,int *tag,symbol **symptr); +SC_FUNC constvalue *append_constval(constvalue *table,const char *name,cell val,short index); +SC_FUNC constvalue *find_constval(constvalue *table,char *name,short index); +SC_FUNC void delete_consttable(constvalue *table); +SC_FUNC symbol *add_constant(char *name,cell val,int vclass,int tag); +SC_FUNC void exporttag(int tag); +SC_FUNC void sc_attachdocumentation(symbol *sym); + +/* function prototypes in SC2.C */ +#define PUSHSTK_P(v) { stkitem s_; s_.pv=(v); pushstk(s_); } +#define PUSHSTK_I(v) { stkitem s_; s_.i=(v); pushstk(s_); } +#define POPSTK_P() (popstk().pv) +#define POPSTK_I() (popstk().i) +SC_FUNC void pushstk(stkitem val); +SC_FUNC stkitem popstk(void); +SC_FUNC void clearstk(void); +SC_FUNC int plungequalifiedfile(char *name); /* explicit path included */ +SC_FUNC int plungefile(char *name,int try_currentpath,int try_includepaths); /* search through "include" paths */ +SC_FUNC void preprocess(void); +SC_FUNC void lexinit(void); +SC_FUNC int lex(cell *lexvalue,char **lexsym); +SC_FUNC void lexpush(void); +SC_FUNC void lexclr(int clreol); +SC_FUNC int matchtoken(int token); +SC_FUNC int tokeninfo(cell *val,char **str); +SC_FUNC int needtoken(int token); +SC_FUNC void litadd(cell value); +SC_FUNC void litinsert(cell value,int pos); +SC_FUNC int alphanum(char c); +SC_FUNC int ishex(char c); +SC_FUNC void delete_symbol(symbol *root,symbol *sym); +SC_FUNC void delete_symbols(symbol *root,int level,int del_labels,int delete_functions); +SC_FUNC int refer_symbol(symbol *entry,symbol *bywhom); +SC_FUNC void markusage(symbol *sym,int usage); +SC_FUNC uint32_t namehash(const char *name); +SC_FUNC symbol *findglb(const char *name); +SC_FUNC symbol *findloc(const char *name); +SC_FUNC symbol *findconst(const char *name); +SC_FUNC symbol *finddepend(const symbol *parent); +SC_FUNC symbol *addsym(const char *name,cell addr,int ident,int vclass,int tag, + int usage); +SC_FUNC symbol *addvariable(const char *name,cell addr,int ident,int vclass,int tag, + int dim[],int numdim,int idxtag[]); +SC_FUNC int getlabel(void); +SC_FUNC char *itoh(ucell val); + +/* function prototypes in SC3.C */ +SC_FUNC int check_userop(void (*oper)(void),int tag1,int tag2,int numparam, + value *lval,int *resulttag); +SC_FUNC int matchtag(int formaltag,int actualtag,int allowcoerce); +SC_FUNC int expression(cell *val,int *tag,symbol **symptr,int chkfuncresult); + +/* function prototypes in SC4.C */ +SC_FUNC void writeleader(symbol *root); +SC_FUNC void writetrailer(void); +SC_FUNC void begcseg(void); +SC_FUNC void begdseg(void); +SC_FUNC void setline(int chkbounds); +SC_FUNC void setfiledirect(char *name); +SC_FUNC void setlinedirect(int line); +SC_FUNC void setlabel(int index); +SC_FUNC void markexpr(optmark type,const char *name,cell offset); +SC_FUNC void startfunc(char *fname); +SC_FUNC void endfunc(void); +SC_FUNC void alignframe(int numbytes); +SC_FUNC void rvalue(value *lval); +SC_FUNC void address(symbol *ptr,regid reg); +SC_FUNC void store(value *lval); +SC_FUNC void storereg(cell address,regid reg); +SC_FUNC void memcopy(cell size); +SC_FUNC void copyarray(symbol *sym,cell size); +SC_FUNC void fillarray(symbol *sym,cell size,cell value); +SC_FUNC void ldconst(cell val,regid reg); +SC_FUNC void moveto1(void); +SC_FUNC void pushreg(regid reg); +SC_FUNC void pushval(cell val); +SC_FUNC void popreg(regid reg); +SC_FUNC void swap1(void); +SC_FUNC void ffswitch(int label); +SC_FUNC void ffcase(cell value,char *labelname,int newtable); +SC_FUNC void ffcall(symbol *sym,const char *label,int numargs); +SC_FUNC void ffret(void); +SC_FUNC void ffabort(int reason); +SC_FUNC void ffbounds(cell size); +SC_FUNC void jumplabel(int number); +SC_FUNC void defstorage(void); +SC_FUNC void modstk(int delta); +SC_FUNC void setstk(cell value); +SC_FUNC void modheap(int delta); +SC_FUNC void setheap_pri(void); +SC_FUNC void setheap(cell value); +SC_FUNC void cell2addr(void); +SC_FUNC void cell2addr_alt(void); +SC_FUNC void addr2cell(void); +SC_FUNC void char2addr(void); +SC_FUNC void charalign(void); +SC_FUNC void addconst(cell value); + +/* Code generation functions for arithmetic operators. + * + * Syntax: o[u|s|b]_name + * | | | +--- name of operator + * | | +----- underscore + * | +--------- "u"nsigned operator, "s"igned operator or "b"oth + * +------------- "o"perator + */ +SC_FUNC void os_mult(void); /* multiplication (signed) */ +SC_FUNC void os_div(void); /* division (signed) */ +SC_FUNC void os_mod(void); /* modulus (signed) */ +SC_FUNC void ob_add(void); /* addition */ +SC_FUNC void ob_sub(void); /* subtraction */ +SC_FUNC void ob_sal(void); /* shift left (arithmetic) */ +SC_FUNC void os_sar(void); /* shift right (arithmetic, signed) */ +SC_FUNC void ou_sar(void); /* shift right (logical, unsigned) */ +SC_FUNC void ob_or(void); /* bitwise or */ +SC_FUNC void ob_xor(void); /* bitwise xor */ +SC_FUNC void ob_and(void); /* bitwise and */ +SC_FUNC void ob_eq(void); /* equality */ +SC_FUNC void ob_ne(void); /* inequality */ +SC_FUNC void relop_prefix(void); +SC_FUNC void relop_suffix(void); +SC_FUNC void os_le(void); /* less or equal (signed) */ +SC_FUNC void os_ge(void); /* greater or equal (signed) */ +SC_FUNC void os_lt(void); /* less (signed) */ +SC_FUNC void os_gt(void); /* greater (signed) */ + +SC_FUNC void lneg(void); +SC_FUNC void neg(void); +SC_FUNC void invert(void); +SC_FUNC void nooperation(void); +SC_FUNC void inc(value *lval); +SC_FUNC void dec(value *lval); +SC_FUNC void jmp_ne0(int number); +SC_FUNC void jmp_eq0(int number); +SC_FUNC void outval(cell val,int newline); + +/* function prototypes in SC5.C */ +SC_FUNC int error(int number,...); +SC_FUNC void errorset(int code); + +/* function prototypes in SC6.C */ +SC_FUNC int assemble(FILE *fout,FILE *fin); + +/* function prototypes in SC7.C */ +SC_FUNC void stgbuffer_cleanup(void); +SC_FUNC void stgmark(char mark); +SC_FUNC void stgwrite(const char *st); +SC_FUNC void stgout(int index); +SC_FUNC void stgdel(int index,cell code_index); +SC_FUNC int stgget(int *index,cell *code_index); +SC_FUNC void stgset(int onoff); +SC_FUNC int phopt_init(void); +SC_FUNC int phopt_cleanup(void); + +/* function prototypes in SCLIST.C */ +SC_FUNC char* duplicatestring(const char* sourcestring); +SC_FUNC stringpair *insert_alias(char *name,char *alias); +SC_FUNC stringpair *find_alias(char *name); +SC_FUNC int lookup_alias(char *target,char *name); +SC_FUNC void delete_aliastable(void); +SC_FUNC stringlist *insert_path(char *path); +SC_FUNC char *get_path(int index); +SC_FUNC void delete_pathtable(void); +SC_FUNC stringpair *insert_subst(char *pattern,char *substitution,int prefixlen); +SC_FUNC int get_subst(int index,char **pattern,char **substitution); +SC_FUNC stringpair *find_subst(char *name,int length); +SC_FUNC int delete_subst(char *name,int length); +SC_FUNC void delete_substtable(void); +SC_FUNC stringlist *insert_sourcefile(char *string); +SC_FUNC char *get_sourcefile(int index); +SC_FUNC void delete_sourcefiletable(void); +SC_FUNC stringlist *insert_docstring(char *string); +SC_FUNC char *get_docstring(int index); +SC_FUNC void delete_docstring(int index); +SC_FUNC void delete_docstringtable(void); +SC_FUNC stringlist *insert_autolist(char *string); +SC_FUNC char *get_autolist(int index); +SC_FUNC void delete_autolisttable(void); +SC_FUNC stringlist *insert_dbgfile(const char *filename); +SC_FUNC stringlist *insert_dbgline(int linenr); +SC_FUNC stringlist *insert_dbgsymbol(symbol *sym); +SC_FUNC char *get_dbgstring(int index); +SC_FUNC void delete_dbgstringtable(void); + +/* function prototypes in SCMEMFILE.C */ +#if !defined tMEMFILE + typedef unsigned char MEMFILE; + #define tMEMFILE 1 +#endif +MEMFILE *mfcreate(char *filename); +void mfclose(MEMFILE *mf); +int mfdump(MEMFILE *mf); +long mflength(MEMFILE *mf); +long mfseek(MEMFILE *mf,long offset,int whence); +unsigned int mfwrite(MEMFILE *mf,unsigned char *buffer,unsigned int size); +unsigned int mfread(MEMFILE *mf,unsigned char *buffer,unsigned int size); +char *mfgets(MEMFILE *mf,char *string,unsigned int size); +int mfputs(MEMFILE *mf,char *string); + +/* function prototypes in SCI18N.C */ +#define MAXCODEPAGE 12 +SC_FUNC int cp_path(const char *root,const char *directory); +SC_FUNC int cp_set(const char *name); +SC_FUNC cell cp_translate(const unsigned char *string,const unsigned char **endptr); +SC_FUNC cell get_utf8_char(const unsigned char *string,const unsigned char **endptr); +SC_FUNC int scan_utf8(FILE *fp,const char *filename); + +/* function prototypes in SCSTATE.C */ +SC_FUNC constvalue *automaton_add(const char *name); +SC_FUNC constvalue *automaton_find(const char *name); +SC_FUNC constvalue *automaton_findid(int id); +SC_FUNC constvalue *state_add(const char *name,int fsa_id); +SC_FUNC constvalue *state_find(const char *name,int fsa_id); +SC_FUNC constvalue *state_findid(int id); +SC_FUNC void state_buildlist(int **list,int *listsize,int *count,int stateid); +SC_FUNC int state_addlist(int *list,int count,int fsa_id); +SC_FUNC void state_deletetable(void); +SC_FUNC int state_getfsa(int listid); +SC_FUNC int state_count(int listid); +SC_FUNC int state_inlist(int listid,int state); +SC_FUNC int state_listitem(int listid,int index); +SC_FUNC void state_conflict(symbol *root); + +/* external variables (defined in scvars.c) */ +#if !defined SC_SKIP_VDECL +SC_VDECL symbol loctab; /* local symbol table */ +SC_VDECL symbol glbtab; /* global symbol table */ +SC_VDECL cell *litq; /* the literal queue */ +SC_VDECL unsigned char pline[]; /* the line read from the input file */ +SC_VDECL const unsigned char *lptr;/* points to the current position in "pline" */ +SC_VDECL constvalue tagname_tab;/* tagname table */ +SC_VDECL constvalue libname_tab;/* library table (#pragma library "..." syntax) */ +SC_VDECL constvalue *curlibrary;/* current library */ +SC_VDECL int pc_addlibtable; /* is the library table added to the AMX file? */ +SC_VDECL symbol *curfunc; /* pointer to current function */ +SC_VDECL char *inpfname; /* name of the file currently read from */ +SC_VDECL char outfname[]; /* intermediate (assembler) file name */ +SC_VDECL char binfname[]; /* binary file name */ +SC_VDECL char errfname[]; /* error file name */ +SC_VDECL char sc_ctrlchar; /* the control character (or escape character) */ +SC_VDECL char sc_ctrlchar_org;/* the default control character */ +SC_VDECL int litidx; /* index to literal table */ +SC_VDECL int litmax; /* current size of the literal table */ +SC_VDECL int stgidx; /* index to the staging buffer */ +SC_VDECL int sc_labnum; /* number of (internal) labels */ +SC_VDECL int staging; /* true if staging output */ +SC_VDECL cell declared; /* number of local cells declared */ +SC_VDECL cell glb_declared; /* number of global cells declared */ +SC_VDECL cell code_idx; /* number of bytes with generated code */ +SC_VDECL int ntv_funcid; /* incremental number of native function */ +SC_VDECL int errnum; /* number of errors */ +SC_VDECL int warnnum; /* number of warnings */ +SC_VDECL int sc_debug; /* debug/optimization options (bit field) */ +SC_VDECL int sc_packstr; /* strings are packed by default? */ +SC_VDECL int sc_asmfile; /* create .ASM file? */ +SC_VDECL int sc_listing; /* create .LST file? */ +SC_VDECL int sc_compress; /* compress bytecode? */ +SC_VDECL int sc_needsemicolon;/* semicolon required to terminate expressions? */ +SC_VDECL int sc_dataalign; /* data alignment value */ +SC_VDECL int sc_alignnext; /* must frame of the next function be aligned? */ +SC_VDECL int pc_docexpr; /* must expression be attached to documentation comment? */ +SC_VDECL int curseg; /* 1 if currently parsing CODE, 2 if parsing DATA */ +SC_VDECL cell sc_stksize; /* stack size */ +SC_VDECL cell sc_amxlimit; /* abstract machine size limit */ +SC_VDECL int freading; /* is there an input file ready for reading? */ +SC_VDECL int fline; /* the line number in the current file */ +SC_VDECL short fnumber; /* number of files in the file table (debugging) */ +SC_VDECL short fcurrent; /* current file being processed (debugging) */ +SC_VDECL short sc_intest; /* true if inside a test */ +SC_VDECL int sideeffect; /* true if an expression causes a side-effect */ +SC_VDECL int stmtindent; /* current indent of the statement */ +SC_VDECL int indent_nowarn; /* skip warning "217 loose indentation" */ +SC_VDECL int sc_tabsize; /* number of spaces that a TAB represents */ +SC_VDECL short sc_allowtags; /* allow/detect tagnames in lex() */ +SC_VDECL int sc_status; /* read/write status */ +SC_VDECL int sc_rationaltag; /* tag for rational numbers */ +SC_VDECL int rational_digits; /* number of fractional digits */ +SC_VDECL int sc_allowproccall;/* allow/detect tagnames in lex() */ +SC_VDECL short sc_is_utf8; /* is this source file in UTF-8 encoding */ + +SC_VDECL constvalue sc_automaton_tab; /* automaton table */ +SC_VDECL constvalue sc_state_tab; /* state table */ + +SC_VDECL FILE *inpf; /* file read from (source or include) */ +SC_VDECL FILE *inpf_org; /* main source file */ +SC_VDECL FILE *outf; /* file written to */ + +SC_VDECL jmp_buf errbuf; /* target of longjmp() on a fatal error */ + +#if !defined SC_LIGHT + SC_VDECL int sc_makereport; /* generate a cross-reference report */ +#endif + +#endif /* SC_SKIP_VDECL */ + +#endif /* SC_H_INCLUDED */ diff --git a/compiler/libpc300/sc1.c b/compiler/libpc300/sc1.c new file mode 100755 index 00000000..3aff2ed6 --- /dev/null +++ b/compiler/libpc300/sc1.c @@ -0,0 +1,5555 @@ +/* Pawn compiler + * + * Function and variable definition and declaration, statement parser. + * + * Copyright (c) ITB CompuPhase, 1997-2005 + * + * This software is provided "as-is", without any express or implied warranty. + * In no event will the authors be held liable for any damages arising from + * the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software in + * a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * 3. This notice may not be removed or altered from any source distribution. + * + * Version: $Id$ + */ +#include +#include +#include +#include +#include +#include +#include + +#if defined __WIN32__ || defined _WIN32 || defined __MSDOS__ + #include + #include +#endif + +#if defined LINUX || defined __FreeBSD__ || defined __OpenBSD__ + #include + #include /* from BinReloc, see www.autopackage.org */ +#endif + +#if defined FORTIFY + #include "fortify.h" +#endif + +#if defined __BORLANDC__ || defined __WATCOMC__ + #include + static unsigned total_drives; /* dummy variable */ + #define dos_setdrive(i) _dos_setdrive(i,&total_drives) +#elif defined _MSC_VER && defined _WIN32 + #include /* for _chdrive() */ + #define dos_setdrive(i) _chdrive(i) +#endif +#if defined __BORLANDC__ + #include /* for chdir() */ +#elif defined __WATCOMC__ + #include /* for chdir() */ +#endif +#if defined __WIN32__ || defined _WIN32 || defined _Windows + #include +#endif + +#include "sc.h" +#define VERSION_STR "3.0.3367" +#define VERSION_INT 0x300 + +static void resetglobals(void); +static void initglobals(void); +static void setopt(int argc,char **argv,char *oname,char *ename,char *pname, + char *rname,char *codepage); +static void setconfig(char *root); +static void setcaption(void); +static void about(void); +static void setconstants(void); +static void parse(void); +static void dumplits(void); +static void dumpzero(int count); +static void declfuncvar(int fpublic,int fstatic,int fstock,int fconst); +static void declglb(char *firstname,int firsttag,int fpublic,int fstatic, + int stock,int fconst); +static int declloc(int fstatic); +static void decl_const(int table); +static void decl_enum(int table); +static cell needsub(int *tag,constvalue **enumroot); +static void initials(int ident,int tag,cell *size,int dim[],int numdim, + constvalue *enumroot); +static cell initarray(int ident,int tag,int dim[],int numdim,int cur, + int startlit,int counteddim[],constvalue *lastdim, + constvalue *enumroot,int *errorfound); +static cell initvector(int ident,int tag,cell size,int fillzero, + constvalue *enumroot,int *errorfound); +static cell init(int ident,int *tag,int *errorfound); +static void funcstub(int native); +static int newfunc(char *firstname,int firsttag,int fpublic,int fstatic,int stock); +static int declargs(symbol *sym); +static void doarg(char *name,int ident,int offset,int tags[],int numtags, + int fpublic,int fconst,arginfo *arg); +static void make_report(symbol *root,FILE *log,char *sourcefile); +static void reduce_referrers(symbol *root); +static long max_stacksize(symbol *root); +static int testsymbols(symbol *root,int level,int testlabs,int testconst); +static void destructsymbols(symbol *root,int level); +static constvalue *find_constval_byval(constvalue *table,cell val); +static void statement(int *lastindent,int allow_decl); +static void compound(int stmt_sameline); +static int doexpr(int comma,int chkeffect,int allowarray,int mark_endexpr, + int *tag,symbol **symptr,int chkfuncresult); +static void doassert(void); +static void doexit(void); +static void test(int label,int parens,int invert); +static void doif(void); +static void dowhile(void); +static void dodo(void); +static void dofor(void); +static void doswitch(void); +static void dogoto(void); +static void dolabel(void); +static symbol *fetchlab(char *name); +static void doreturn(void); +static void dobreak(void); +static void docont(void); +static void dosleep(void); +static void dostate(void); +static void addwhile(int *ptr); +static void delwhile(void); +static int *readwhile(void); + +static int lastst = 0; /* last executed statement type */ +static int nestlevel = 0; /* number of active (open) compound statements */ +static int rettype = 0; /* the type that a "return" expression should have */ +static int skipinput = 0; /* number of lines to skip from the first input file */ +static int optproccall = TRUE; /* support "procedure call" */ +static int verbosity = 1; /* verbosity level, 0=quiet, 1=normal, 2=verbose */ +static int sc_reparse = 0; /* needs 3th parse because of changed prototypes? */ +static int sc_parsenum = 0; /* number of the extra parses */ +static int wq[wqTABSZ]; /* "while queue", internal stack for nested loops */ +static int *wqptr; /* pointer to next entry */ +#if !defined SC_LIGHT + static char *sc_documentation=NULL;/* main documentation */ +#endif +#if defined __WIN32__ || defined _WIN32 || defined _Windows + static HWND hwndFinish = 0; +#endif + +#if !defined NO_MAIN + +#if defined __TURBOC__ && !defined __32BIT__ + extern unsigned int _stklen = 0x2000; +#endif + +int main(int argc, char *argv[]) +{ + return pc_compile(argc,argv); +} + +/* pc_printf() + * Called for general purpose "console" output. This function prints general + * purpose messages; errors go through pc_error(). The function is modelled + * after printf(). + */ +int pc_printf(const char *message,...) +{ + int ret; + va_list argptr; + + va_start(argptr,message); + ret=vprintf(message,argptr); + va_end(argptr); + + return ret; +} + +/* pc_error() + * Called for producing error output. + * number the error number (as documented in the manual) + * message a string describing the error with embedded %d and %s tokens + * filename the name of the file currently being parsed + * firstline the line number at which the expression started on which + * the error was found, or -1 if there is no "starting line" + * lastline the line number at which the error was detected + * argptr a pointer to the first of a series of arguments (for macro + * "va_arg") + * Return: + * If the function returns 0, the parser attempts to continue compilation. + * On a non-zero return value, the parser aborts. + */ +int pc_error(int number,char *message,char *filename,int firstline,int lastline,va_list argptr) +{ +static char *prefix[3]={ "error", "fatal error", "warning" }; + + if (number!=0) { + char *pre; + + pre=prefix[number/100]; + if (firstline>=0) + fprintf(stderr,"%s(%d -- %d) : %s %03d: ",filename,firstline,lastline,pre,number); + else + fprintf(stderr,"%s(%d) : %s %03d: ",filename,lastline,pre,number); + } /* if */ + vfprintf(stderr,message,argptr); + fflush(stderr); + return 0; +} + +/* pc_opensrc() + * Opens a source file (or include file) for reading. The "file" does not have + * to be a physical file, one might compile from memory. + * filename the name of the "file" to read from + * Return: + * The function must return a pointer, which is used as a "magic cookie" to + * all I/O functions. When failing to open the file for reading, the + * function must return NULL. + * Note: + * Several "source files" may be open at the same time. Specifically, one + * file can be open for reading and another for writing. + */ +void *pc_opensrc(char *filename) +{ + return fopen(filename,"r"); +} + +/* pc_createsrc() + * Creates/overwrites a source file for writing. The "file" does not have + * to be a physical file, one might compile from memory. + * filename the name of the "file" to create + * Return: + * The function must return a pointer, which is used as a "magic cookie" to + * all I/O functions. When failing to open the file for reading, the + * function must return NULL. + * Note: + * Several "source files" may be open at the same time. Specifically, one + * file can be open for reading and another for writing. + */ +void *pc_createsrc(char *filename) +{ + return fopen(filename,"w"); +} + +/* pc_closesrc() + * Closes a source file (or include file). The "handle" parameter has the + * value that pc_opensrc() returned in an earlier call. + */ +void pc_closesrc(void *handle) +{ + assert(handle!=NULL); + fclose((FILE*)handle); +} + +/* pc_resetsrc() + * "position" may only hold a pointer that was previously obtained from + * pc_getpossrc() + */ +void pc_resetsrc(void *handle,void *position) +{ + assert(handle!=NULL); + fsetpos((FILE*)handle,(fpos_t *)position); +} + +/* pc_readsrc() + * Reads a single line from the source file (or up to a maximum number of + * characters if the line in the input file is too long). + */ +char *pc_readsrc(void *handle,unsigned char *target,int maxchars) +{ + return fgets((char*)target,maxchars,(FILE*)handle); +} + +/* pc_writesrc() + * Writes to to the source file. There is no automatic line ending; to end a + * line, write a "\n". + */ +int pc_writesrc(void *handle,unsigned char *source) +{ + return fputs((char*)source,(FILE*)handle) >= 0; +} + +void *pc_getpossrc(void *handle) +{ + static fpos_t lastpos; /* may need to have a LIFO stack of such positions */ + + fgetpos((FILE*)handle,&lastpos); + return &lastpos; +} + +int pc_eofsrc(void *handle) +{ + return feof((FILE*)handle); +} + +/* should return a pointer, which is used as a "magic cookie" to all I/O + * functions; return NULL for failure + */ +void *pc_openasm(char *filename) +{ + #if defined __MSDOS__ || defined SC_LIGHT + return fopen(filename,"w+"); + #else + return mfcreate(filename); + #endif +} + +void pc_closeasm(void *handle, int deletefile) +{ + #if defined __MSDOS__ || defined SC_LIGHT + if (handle!=NULL) + fclose((FILE*)handle); + if (deletefile) + remove(outfname); + #else + if (handle!=NULL) { + if (!deletefile) + mfdump((MEMFILE*)handle); + mfclose((MEMFILE*)handle); + } /* if */ + #endif +} + +void pc_resetasm(void *handle) +{ + assert(handle!=NULL); + #if defined __MSDOS__ || defined SC_LIGHT + fflush((FILE*)handle); + fseek((FILE*)handle,0,SEEK_SET); + #else + mfseek((MEMFILE*)handle,0,SEEK_SET); + #endif +} + +int pc_writeasm(void *handle,char *string) +{ + #if defined __MSDOS__ || defined SC_LIGHT + return fputs(string,(FILE*)handle) >= 0; + #else + return mfputs((MEMFILE*)handle,string); + #endif +} + +char *pc_readasm(void *handle, char *string, int maxchars) +{ + #if defined __MSDOS__ || defined SC_LIGHT + return fgets(string,maxchars,(FILE*)handle); + #else + return mfgets((MEMFILE*)handle,string,maxchars); + #endif +} + +/* Should return a pointer, which is used as a "magic cookie" to all I/O + * functions; return NULL for failure. + */ +void *pc_openbin(char *filename) +{ + return fopen(filename,"wb"); +} + +void pc_closebin(void *handle,int deletefile) +{ + fclose((FILE*)handle); + if (deletefile) + remove(binfname); +} + +/* pc_resetbin() + * Can seek to any location in the file. + * The offset is always from the start of the file. + */ +void pc_resetbin(void *handle,long offset) +{ + fflush((FILE*)handle); + fseek((FILE*)handle,offset,SEEK_SET); +} + +int pc_writebin(void *handle,void *buffer,int size) +{ + return (int)fwrite(buffer,1,size,(FILE*)handle) == size; +} + +long pc_lengthbin(void *handle) +{ + return ftell((FILE*)handle); +} + +#endif /* !defined NO_MAIN */ + + +/* "main" of the compiler + */ +#if defined __cplusplus + extern "C" +#endif +int pc_compile(int argc, char *argv[]) +{ + int entry,i,jmpcode; + int retcode; + char incfname[_MAX_PATH]; + char reportname[_MAX_PATH]; + char codepage[MAXCODEPAGE+1]; + FILE *binf; + void *inpfmark; + int lcl_packstr,lcl_needsemicolon,lcl_tabsize; + #if !defined SC_LIGHT + int hdrsize; + #endif + + /* set global variables to their initial value */ + binf=NULL; + initglobals(); + errorset(sRESET); + errorset(sEXPRRELEASE); + lexinit(); + + /* make sure that we clean up on a fatal error; do this before the first + * call to error(). */ + if ((jmpcode=setjmp(errbuf))!=0) + goto cleanup; + + /* allocate memory for fixed tables */ + inpfname=(char*)malloc(_MAX_PATH); + if (inpfname==NULL) + error(103); /* insufficient memory */ + litq=(cell*)malloc(litmax*sizeof(cell)); + if (litq==NULL) + error(103); /* insufficient memory */ + if (!phopt_init()) + error(103); /* insufficient memory */ + + setopt(argc,argv,outfname,errfname,incfname,reportname,codepage); + /* set output names that depend on the input name */ + if (sc_listing) + set_extension(outfname,".lst",TRUE); + else + set_extension(outfname,".asm",TRUE); + strcpy(binfname,outfname); + set_extension(binfname,".amx",TRUE); + if (strlen(errfname)!=0) + remove(errfname); /* delete file on startup */ + else if (verbosity>0) + setcaption(); + setconfig(argv[0]); /* the path to the include and codepage files */ + sc_ctrlchar_org=sc_ctrlchar; + lcl_packstr=sc_packstr; + lcl_needsemicolon=sc_needsemicolon; + lcl_tabsize=sc_tabsize; + #if !defined NO_CODEPAGE + if (!cp_set(codepage)) /* set codepage */ + error(108); /* codepage mapping file not found */ + #endif + /* optionally create a temporary input file that is a collection of all + * input files + */ + assert(get_sourcefile(0)!=NULL); /* there must be at least one source file */ + if (get_sourcefile(1)!=NULL) { + /* there are at least two or more source files */ + char *tname,*sname; + FILE *ftmp,*fsrc; + int fidx; + #if defined __WIN32__ || defined _WIN32 + tname=_tempnam(NULL,"pawn"); + #elif defined __MSDOS__ || defined _Windows + tname=tempnam(NULL,"pawn"); + #elif defined(MACOS) && !defined(__MACH__) + /* tempnam is not supported for the Macintosh CFM build. */ + error(104,get_sourcefile(1)); + tname=NULL; + sname=NULL; + #else + tname=tempnam(NULL,"pawn"); + #endif + ftmp=(FILE*)pc_createsrc(tname); + for (fidx=0; (sname=get_sourcefile(fidx))!=NULL; fidx++) { + unsigned char tstring[128]; + fsrc=(FILE*)pc_opensrc(sname); + if (fsrc==NULL) + error(100,sname); + pc_writesrc(ftmp,(unsigned char*)"#file "); + pc_writesrc(ftmp,(unsigned char*)sname); + pc_writesrc(ftmp,(unsigned char*)"\n"); + while (!pc_eofsrc(fsrc)) { + pc_readsrc(fsrc,tstring,sizeof tstring); + pc_writesrc(ftmp,tstring); + } /* while */ + pc_closesrc(fsrc); + } /* for */ + pc_closesrc(ftmp); + strcpy(inpfname,tname); + free(tname); + } else { + strcpy(inpfname,get_sourcefile(0)); + } /* if */ + inpf_org=(FILE*)pc_opensrc(inpfname); + if (inpf_org==NULL) + error(100,inpfname); + freading=TRUE; + outf=(FILE*)pc_openasm(outfname); /* first write to assembler file (may be temporary) */ + if (outf==NULL) + error(101,outfname); + /* immediately open the binary file, for other programs to check */ + if (sc_asmfile || sc_listing) { + binf=NULL; + } else { + binf=(FILE*)pc_openbin(binfname); + if (binf==NULL) + error(101,binfname); + } /* if */ + setconstants(); /* set predefined constants and tagnames */ + for (i=0; i0) { + if (strcmp(incfname,sDEF_PREFIX)==0) { + plungefile(incfname,FALSE,TRUE); /* parse "default.inc" */ + } else { + if (!plungequalifiedfile(incfname)) /* parse "prefix" include file */ + error(100,incfname); /* cannot read from ... (fatal error) */ + } /* if */ + } /* if */ + preprocess(); /* fetch first line */ + parse(); /* process all input */ + sc_parsenum++; + } while (sc_reparse); + + /* second (or third) pass */ + sc_status=statWRITE; /* set, to enable warnings */ + state_conflict(&glbtab); + + /* write a report, if requested */ + #if !defined SC_LIGHT + if (sc_makereport) { + FILE *frep=stdout; + if (strlen(reportname)>0) + frep=fopen(reportname,"wb"); /* avoid translation of \n to \r\n in DOS/Windows */ + if (frep!=NULL) { + make_report(&glbtab,frep,get_sourcefile(0)); + if (strlen(reportname)>0) + fclose(frep); + } /* if */ + if (sc_documentation!=NULL) { + free(sc_documentation); + sc_documentation=NULL; + } /* if */ + } /* if */ + #endif + if (sc_listing) + goto cleanup; + + /* ??? for re-parsing the listing file instead of the original source + * file (and doing preprocessing twice): + * - close input file, close listing file + * - re-open listing file for reading (inpf) + * - open assembler file (outf) + */ + + /* reset "defined" flag of all functions and global variables */ + reduce_referrers(&glbtab); + delete_symbols(&glbtab,0,TRUE,FALSE); + #if !defined NO_DEFINE + delete_substtable(); + #endif + resetglobals(); + sc_ctrlchar=sc_ctrlchar_org; + sc_packstr=lcl_packstr; + sc_needsemicolon=lcl_needsemicolon; + sc_tabsize=lcl_tabsize; + errorset(sRESET); + /* reset the source file */ + inpf=inpf_org; + freading=TRUE; + pc_resetsrc(inpf,inpfmark); /* reset file position */ + fline=skipinput; /* reset line number */ + lexinit(); /* clear internal flags of lex() */ + sc_status=statWRITE; /* allow to write --this variable was reset by resetglobals() */ + writeleader(&glbtab); + insert_dbgfile(inpfname); + if (strlen(incfname)>0) { + if (strcmp(incfname,sDEF_PREFIX)==0) + plungefile(incfname,FALSE,TRUE); /* parse "default.inc" (again) */ + else + plungequalifiedfile(incfname); /* parse implicit include file (again) */ + } /* if */ + preprocess(); /* fetch first line */ + parse(); /* process all input */ + /* inpf is already closed when readline() attempts to pop of a file */ + writetrailer(); /* write remaining stuff */ + + entry=testsymbols(&glbtab,0,TRUE,FALSE); /* test for unused or undefined + * functions and variables */ + if (!entry) + error(13); /* no entry point (no public functions) */ + +cleanup: + if (inpf!=NULL) /* main source file is not closed, do it now */ + pc_closesrc(inpf); + /* write the binary file (the file is already open) */ + if (!(sc_asmfile || sc_listing) && errnum==0 && jmpcode==0) { + assert(binf!=NULL); + pc_resetasm(outf); /* flush and loop back, for reading */ + #if !defined SC_LIGHT + hdrsize= + #endif + assemble(binf,outf); /* assembler file is now input */ + } /* if */ + if (outf!=NULL) { + pc_closeasm(outf,!(sc_asmfile || sc_listing)); + outf=NULL; + } /* if */ + if (binf!=NULL) { + pc_closebin(binf,errnum!=0); + binf=NULL; + } /* if */ + + #if !defined SC_LIGHT + if (errnum==0 && strlen(errfname)==0) { + long stacksize=max_stacksize(&glbtab); + int flag_exceed=0; + if (sc_amxlimit > 0 && (long)(hdrsize+code_idx+glb_declared*sizeof(cell)+sc_stksize*sizeof(cell)) >= sc_amxlimit) + flag_exceed=1; + if ((sc_debug & sSYMBOLIC)!=0 || verbosity>=2 || stacksize+32>=(long)sc_stksize || flag_exceed) { + 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)); + 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)); + 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) + error(106,sc_amxlimit); /* this causes a jump back to label "cleanup" */ + } /* if */ + #endif + + if (inpfname!=NULL) { + if (get_sourcefile(1)!=NULL) + remove(inpfname); /* the "input file" was in fact a temporary file */ + free(inpfname); + } /* if */ + if (litq!=NULL) + free(litq); + phopt_cleanup(); + stgbuffer_cleanup(); + clearstk(); + assert(jmpcode!=0 || loctab.next==NULL);/* on normal flow, local symbols + * should already have been deleted */ + delete_symbols(&loctab,0,TRUE,TRUE); /* delete local variables if not yet + * done (i.e. on a fatal error) */ + delete_symbols(&glbtab,0,TRUE,TRUE); + delete_consttable(&tagname_tab); + delete_consttable(&libname_tab); + delete_consttable(&sc_automaton_tab); + delete_consttable(&sc_state_tab); + state_deletetable(); + delete_aliastable(); + delete_pathtable(); + delete_sourcefiletable(); + delete_dbgstringtable(); + #if !defined NO_DEFINE + delete_substtable(); + #endif + #if !defined SC_LIGHT + delete_docstringtable(); + if (sc_documentation!=NULL) + free(sc_documentation); + #endif + delete_autolisttable(); + if (errnum!=0) { + if (strlen(errfname)==0) + pc_printf("\n%d Error%s.\n",errnum,(errnum>1) ? "s" : ""); + retcode=2; + } else if (warnnum!=0){ + if (strlen(errfname)==0) + pc_printf("\n%d Warning%s.\n",warnnum,(warnnum>1) ? "s" : ""); + retcode=1; + } else { + retcode=jmpcode; + if (retcode==0 && verbosity>=2) + pc_printf("\nDone.\n"); + } /* if */ + #if defined __WIN32__ || defined _WIN32 || defined _Windows + if (IsWindow(hwndFinish)) + PostMessage(hwndFinish,RegisterWindowMessage("PawnNotify"),retcode,0L); + #endif + #if defined FORTIFY + Fortify_ListAllMemory(); + #endif + return retcode; +} + +#if defined __cplusplus + extern "C" +#endif +int pc_addconstant(char *name,cell value,int tag) +{ + errorset(sFORCESET); /* make sure error engine is silenced */ + sc_status=statIDLE; + add_constant(name,value,sGLOBAL,tag); + return 1; +} + +#if defined __cplusplus + extern "C" +#endif +int pc_addtag(char *name) +{ + cell val; + constvalue *ptr; + int last,tag; + + if (name==NULL) { + /* no tagname was given, check for one */ + if (lex(&val,&name)!=tLABEL) { + lexpush(); + return 0; /* untagged */ + } /* if */ + } /* if */ + + assert(strchr(name,':')==NULL); /* colon should already have been stripped */ + last=0; + ptr=tagname_tab.next; + while (ptr!=NULL) { + tag=(int)(ptr->value & TAGMASK); + if (strcmp(name,ptr->name)==0) + return tag; /* tagname is known, return its sequence number */ + tag &= (int)~FIXEDTAG; + if (tag>last) + last=tag; + ptr=ptr->next; + } /* while */ + + /* tagname currently unknown, add it */ + tag=last+1; /* guaranteed not to exist already */ + if (isupper(*name)) + tag |= (int)FIXEDTAG; + append_constval(&tagname_tab,name,(cell)tag,0); + return tag; +} + +static void resetglobals(void) +{ + /* reset the subset of global variables that is modified by the first pass */ + curfunc=NULL; /* pointer to current function */ + lastst=0; /* last executed statement type */ + nestlevel=0; /* number of active (open) compound statements */ + rettype=0; /* the type that a "return" expression should have */ + litidx=0; /* index to literal table */ + stgidx=0; /* index to the staging buffer */ + sc_labnum=0; /* top value of (internal) labels */ + staging=0; /* true if staging output */ + declared=0; /* number of local cells declared */ + glb_declared=0; /* number of global cells declared */ + code_idx=0; /* number of bytes with generated code */ + ntv_funcid=0; /* incremental number of native function */ + curseg=0; /* 1 if currently parsing CODE, 2 if parsing DATA */ + freading=FALSE; /* no input file ready yet */ + fline=0; /* the line number in the current file */ + fnumber=0; /* the file number in the file table (debugging) */ + fcurrent=0; /* current file being processed (debugging) */ + sc_intest=FALSE; /* true if inside a test */ + sideeffect=0; /* true if an expression causes a side-effect */ + stmtindent=0; /* current indent of the statement */ + indent_nowarn=FALSE; /* do not skip warning "217 loose indentation" */ + sc_allowtags=TRUE; /* allow/detect tagnames */ + sc_status=statIDLE; + sc_allowproccall=FALSE; + pc_addlibtable=TRUE; /* by default, add a "library table" to the output file */ + sc_alignnext=FALSE; + pc_docexpr=FALSE; +} + +static void initglobals(void) +{ + resetglobals(); + + sc_asmfile=FALSE; /* do not create .ASM file */ + sc_listing=FALSE; /* do not create .LST file */ + skipinput=0; /* number of lines to skip from the first input file */ + sc_ctrlchar=CTRL_CHAR;/* the escape character */ + litmax=sDEF_LITMAX; /* current size of the literal table */ + errnum=0; /* number of errors */ + warnnum=0; /* number of warnings */ + optproccall=TRUE; /* support "procedure call" */ + verbosity=1; /* verbosity level, no copyright banner */ + sc_debug=sCHKBOUNDS|sSYMBOLIC; /* by default: bounds checking+assertions */ + sc_packstr=TRUE; /* strings are unpacked by default */ + sc_compress=FALSE; + sc_needsemicolon=FALSE;/* semicolon required to terminate expressions? */ + sc_dataalign=sizeof(cell); + sc_stksize=sDEF_AMXSTACK;/* default stack size */ + sc_amxlimit=0; /* no limit on size of the abstract machine */ + sc_tabsize=8; /* assume a TAB is 8 spaces */ + sc_rationaltag=0; /* assume no support for rational numbers */ + rational_digits=0; /* number of fractional digits */ + + outfname[0]='\0'; /* output file name */ + errfname[0]='\0'; /* error file name */ + inpf=NULL; /* file read from */ + inpfname=NULL; /* pointer to name of the file currently read from */ + outf=NULL; /* file written to */ + litq=NULL; /* the literal queue */ + glbtab.next=NULL; /* clear global variables/constants table */ + loctab.next=NULL; /* " local " / " " */ + tagname_tab.next=NULL;/* tagname table */ + libname_tab.next=NULL;/* library table (#pragma library "..." syntax) */ + + pline[0]='\0'; /* the line read from the input file */ + lptr=NULL; /* points to the current position in "pline" */ + curlibrary=NULL; /* current library */ + inpf_org=NULL; /* main source file */ + + wqptr=wq; /* initialize while queue pointer */ + +#if !defined SC_LIGHT + sc_documentation=NULL; + sc_makereport=FALSE; /* do not generate a cross-reference report */ +#endif +} + +/* set_extension + * Set the default extension, or force an extension. To erase the + * extension of a filename, set "extension" to an empty string. + */ +SC_FUNC void set_extension(char *filename,char *extension,int force) +{ + char *ptr; + + assert(extension!=NULL && (*extension=='\0' || *extension=='.')); + assert(filename!=NULL); + ptr=strrchr(filename,'.'); + if (ptr!=NULL) { + /* ignore extension on a directory or at the start of the filename */ + if (strchr(ptr,DIRSEP_CHAR)!=NULL || ptr==filename || *(ptr-1)==DIRSEP_CHAR) + ptr=NULL; + } /* if */ + if (force && ptr!=NULL) + *ptr='\0'; /* set zero terminator at the position of the period */ + if (force || ptr==NULL) + strcat(filename,extension); +} + +static const char *option_value(const char *optptr) +{ + return (*(optptr+1)=='=' || *(optptr+1)==':') ? optptr+2 : optptr+1; +} + +static int toggle_option(const char *optptr, int option) +{ + switch (*option_value(optptr)) { + case '\0': + option=!option; + break; + case '-': + option=FALSE; + break; + case '+': + option=TRUE; + break; + default: + about(); + } /* switch */ + return option; +} + +/* Parsing command line options is indirectly recursive: parseoptions() + * calls parserespf() to handle options in a a response file and + * parserespf() calls parseoptions() at its turn after having created + * an "option list" from the contents of the file. + */ +static void parserespf(char *filename,char *oname,char *ename,char *pname, + char *rname, char *codepage); + +static void parseoptions(int argc,char **argv,char *oname,char *ename,char *pname, + char *rname, char *codepage) +{ + char str[_MAX_PATH],*name; + const char *ptr; + int arg,i,isoption; + + for (arg=1; arg 2 + sc_compress=toggle_option(ptr,sc_compress); + #else + about(); + #endif + break; + case 'c': + strncpy(codepage,option_value(ptr),MAXCODEPAGE); /* set name of codepage */ + codepage[MAXCODEPAGE]='\0'; + break; +#if defined dos_setdrive + case 'D': /* set active directory */ + ptr=option_value(ptr); + if (ptr[1]==':') + dos_setdrive(toupper(*ptr)-'A'+1); /* set active drive */ + chdir(ptr); + break; +#endif + case 'd': + switch (*option_value(ptr)) { + case '0': + sc_debug=0; + break; + case '1': + sc_debug=sCHKBOUNDS; /* assertions and bounds checking */ + break; + case '2': + sc_debug=sCHKBOUNDS | sSYMBOLIC; /* also symbolic info */ + break; + case '3': + sc_debug=sCHKBOUNDS | sSYMBOLIC | sNOOPTIMIZE; + /* also avoid peephole optimization */ + break; + default: + about(); + } /* switch */ + break; + case 'e': + strncpy(ename,option_value(ptr),_MAX_PATH); /* set name of error file */ + ename[_MAX_PATH-1]='\0'; + break; +#if defined __WIN32__ || defined _WIN32 || defined _Windows + case 'H': + hwndFinish=(HWND)atoi(option_value(ptr)); + if (!IsWindow(hwndFinish)) + hwndFinish=(HWND)0; + break; +#endif + case 'i': + strncpy(str,option_value(ptr),sizeof str); /* set name of include directory */ + str[sizeof(str)-1]='\0'; + i=strlen(str); + if (i>0) { + if (str[i-1]!=DIRSEP_CHAR) { + str[i]=DIRSEP_CHAR; + str[i+1]='\0'; + } /* if */ + insert_path(str); + } /* if */ + break; + case 'l': + if (*(ptr+1)!='\0') + about(); + sc_listing=TRUE; /* skip second pass & code generation */ + break; + case 'o': + strncpy(oname,option_value(ptr),_MAX_PATH); /* set name of (binary) output file */ + oname[_MAX_PATH-1]='\0'; + break; + case 'p': + strncpy(pname,option_value(ptr),_MAX_PATH); /* set name of implicit include file */ + pname[_MAX_PATH-1]='\0'; + break; +#if !defined SC_LIGHT + case 'r': + strncpy(rname,option_value(ptr),_MAX_PATH); /* set name of report file */ + rname[_MAX_PATH-1]='\0'; + sc_makereport=TRUE; + if (strlen(rname)>0) { + set_extension(rname,".xml",FALSE); + } else if ((name=get_sourcefile(0))!=NULL) { + assert(strlen(rname)==0); + assert(strlen(name)<_MAX_PATH); + if ((ptr=strrchr(name,DIRSEP_CHAR))!=NULL) + ptr++; /* strip path */ + else + ptr=name; + assert(strlen(ptr)<_MAX_PATH); + strcpy(rname,ptr); + set_extension(rname,".xml",TRUE); + } /* if */ + break; +#endif + case 'S': + i=atoi(option_value(ptr)); + if (i>64) + sc_stksize=(cell)i; /* stack size has minimum size */ + else + about(); + break; + case 's': + skipinput=atoi(option_value(ptr)); + break; + case 't': + sc_tabsize=atoi(option_value(ptr)); + break; + case 'v': + verbosity= isdigit(*option_value(ptr)) ? atoi(option_value(ptr)) : 2; + break; + case 'w': + i=(int)strtol(option_value(ptr),(char **)&ptr,10); + if (*ptr=='-') + pc_enablewarning(i,0); + else if (*ptr=='+') + pc_enablewarning(i,1); + else if (*ptr=='\0') + pc_enablewarning(i,2); + break; + case 'X': + i=atoi(option_value(ptr)); + if (i>64) + sc_amxlimit=(cell)i; /* abstract machine size has minimum size */ + else + about(); + break; + case '\\': /* use \ instead for escape characters */ + sc_ctrlchar='\\'; + break; + case '^': /* use ^ instead for escape characters */ + sc_ctrlchar='^'; + break; + case ';': + sc_needsemicolon=toggle_option(ptr,sc_needsemicolon); + break; + case '(': + optproccall=!toggle_option(ptr,!optproccall); + break; + default: /* wrong option */ + about(); + } /* switch */ + } else if (argv[arg][0]=='@') { + #if !defined SC_LIGHT + parserespf(&argv[arg][1],oname,ename,pname,rname,codepage); + #endif + } else if ((ptr=strchr(argv[arg],'='))!=NULL) { + i=(int)(ptr-argv[arg]); + if (i>sNAMEMAX) { + i=sNAMEMAX; + error(200,argv[arg],sNAMEMAX); /* symbol too long, truncated to sNAMEMAX chars */ + } /* if */ + strncpy(str,argv[arg],i); + str[i]='\0'; /* str holds symbol name */ + i=atoi(ptr+1); + add_constant(str,i,sGLOBAL,0); + } else { + strncpy(str,argv[arg],sizeof(str)-5); /* -5 because default extension is 4 characters */ + str[sizeof(str)-5]='\0'; + set_extension(str,".p",FALSE); + insert_sourcefile(str); + /* The output name is the first input name with a different extension, + * but it is stored in a different directory + */ + if (strlen(oname)==0) { + if ((ptr=strrchr(str,DIRSEP_CHAR))!=NULL) + ptr++; /* strip path */ + else + ptr=str; + assert(strlen(ptr)<_MAX_PATH); + strcpy(oname,ptr); + } /* if */ + set_extension(oname,".asm",TRUE); +#if !defined SC_LIGHT + if (sc_makereport && strlen(rname)==0) { + if ((ptr=strrchr(str,DIRSEP_CHAR))!=NULL) + ptr++; /* strip path */ + else + ptr=str; + assert(strlen(ptr)<_MAX_PATH); + strcpy(rname,ptr); + set_extension(rname,".xml",TRUE); + } /* if */ +#endif + } /* if */ + } /* for */ +} + +#if !defined SC_LIGHT +static void parserespf(char *filename,char *oname,char *ename,char *pname, + char *rname,char *codepage) +{ +#define MAX_OPTIONS 100 + FILE *fp; + char *string, *ptr, **argv; + int argc; + long size; + + if ((fp=fopen(filename,"r"))==NULL) + error(100,filename); /* error reading input file */ + /* load the complete file into memory */ + fseek(fp,0L,SEEK_END); + size=ftell(fp); + fseek(fp,0L,SEEK_SET); + assert(size [filename...] [options]\n\n"); + pc_printf("Options:\n"); + pc_printf(" -A alignment in bytes of the data segment and the stack\n"); + pc_printf(" -a output assembler code\n"); +#if AMX_COMPACTMARGIN > 2 + pc_printf(" -C[+/-] compact encoding for output file (default=%c)\n", sc_compress ? '+' : '-'); +#endif + pc_printf(" -c codepage name or number; e.g. 1252 for Windows Latin-1\n"); +#if defined dos_setdrive + pc_printf(" -Dpath active directory path\n"); +#endif + pc_printf(" -d0 no symbolic information, no run-time checks\n"); + pc_printf(" -d1 [default] run-time checks, no symbolic information\n"); + pc_printf(" -d2 full debug information and dynamic checking\n"); + pc_printf(" -d3 full debug information, dynamic checking, no optimization\n"); + pc_printf(" -e set name of error file (quiet compile)\n"); +#if defined __WIN32__ || defined _WIN32 || defined _Windows + pc_printf(" -H window handle to send a notification message on finish\n"); +#endif + pc_printf(" -i path for include files\n"); + pc_printf(" -l create list file (preprocess only)\n"); + pc_printf(" -o set base name of (P-code) output file\n"); + pc_printf(" -p set name of \"prefix\" file\n"); +#if !defined SC_LIGHT + pc_printf(" -r[name] write cross reference report to console or to specified file\n"); +#endif + pc_printf(" -S stack/heap size in cells (default=%d)\n",(int)sc_stksize); + pc_printf(" -s skip lines from the input file\n"); + pc_printf(" -t TAB indent size (in character positions, default=%d)\n",sc_tabsize); + pc_printf(" -v verbosity level; 0=quiet, 1=normal, 2=verbose (default=%d)\n",verbosity); + pc_printf(" -w disable a specific warning by its number\n"); + pc_printf(" -X abstract machine size limit in bytes\n"); + pc_printf(" -\\ use '\\' for escape characters\n"); + pc_printf(" -^ use '^' for escape characters\n"); + pc_printf(" -;[+/-] require a semicolon to end each statement (default=%c)\n", sc_needsemicolon ? '+' : '-'); + pc_printf(" -([+/-] require parantheses for function invocation (default=%c)\n", optproccall ? '-' : '+'); + pc_printf(" sym=val define constant \"sym\" with value \"val\"\n"); + pc_printf(" sym= define constant \"sym\" with value 0\n"); +#if defined __WIN32__ || defined _WIN32 || defined _Windows || defined __MSDOS__ + pc_printf("\nOptions may start with a dash or a slash; the options \"-d0\" and \"/d0\" are\n"); + pc_printf("equivalent.\n"); +#endif + pc_printf("\nOptions with a value may optionally separate the value from the option letter\n"); + pc_printf("with a colon (\":\") or an equal sign (\"=\"). That is, the options \"-d0\", \"-d=0\"\n"); + pc_printf("and \"-d:0\" are all equivalent.\n"); + } /* if */ + longjmp(errbuf,3); /* user abort */ +} + +static void setconstants(void) +{ + int debug; + + assert(sc_status==statIDLE); + append_constval(&tagname_tab,"_",0,0);/* "untagged" */ + append_constval(&tagname_tab,"bool",1,0); + + add_constant("true",1,sGLOBAL,1); /* boolean flags */ + add_constant("false",0,sGLOBAL,1); + add_constant("EOS",0,sGLOBAL,0); /* End Of String, or '\0' */ + #if PAWN_CELL_SIZE==16 + add_constant("cellbits",16,sGLOBAL,0); + #if defined _I16_MAX + add_constant("cellmax",_I16_MAX,sGLOBAL,0); + add_constant("cellmin",_I16_MIN,sGLOBAL,0); + #else + add_constant("cellmax",SHRT_MAX,sGLOBAL,0); + add_constant("cellmin",SHRT_MIN,sGLOBAL,0); + #endif + #elif PAWN_CELL_SIZE==32 + add_constant("cellbits",32,sGLOBAL,0); + #if defined _I32_MAX + add_constant("cellmax",_I32_MAX,sGLOBAL,0); + add_constant("cellmin",_I32_MIN,sGLOBAL,0); + #else + add_constant("cellmax",LONG_MAX,sGLOBAL,0); + add_constant("cellmin",LONG_MIN,sGLOBAL,0); + #endif + #elif PAWN_CELL_SIZE==64 + add_constant("cellbits",64,sGLOBAL,0); + add_constant("cellmax",_I64_MAX,sGLOBAL,0); + add_constant("cellmin",_I64_MIN,sGLOBAL,0); + #else + #error Unsupported cell size + #endif + add_constant("charbits",sCHARBITS,sGLOBAL,0); + add_constant("charmin",0,sGLOBAL,0); + add_constant("charmax",~(-1 << sCHARBITS) - 1,sGLOBAL,0); + add_constant("ucharmax",(1 << (sizeof(cell)-1)*8)-1,sGLOBAL,0); + + add_constant("__Pawn",VERSION_INT,sGLOBAL,0); + + debug=0; + if ((sc_debug & (sCHKBOUNDS | sSYMBOLIC))==(sCHKBOUNDS | sSYMBOLIC)) + debug=2; + else if ((sc_debug & sCHKBOUNDS)==sCHKBOUNDS) + debug=1; + add_constant("debug",debug,sGLOBAL,0); + + append_constval(&sc_automaton_tab,"",0,0); /* anonymous automaton */ +} + +static int getclassspec(int initialtok,int *fpublic,int *fstatic,int *fstock,int *fconst) +{ + int tok,err; + cell val; + char *str; + + assert(fconst!=NULL); + assert(fstock!=NULL); + assert(fstatic!=NULL); + assert(fpublic!=NULL); + *fconst=FALSE; + *fstock=FALSE; + *fstatic=FALSE; + *fpublic=FALSE; + switch (initialtok) { + case tCONST: + *fconst=TRUE; + break; + case tSTOCK: + *fstock=TRUE; + break; + case tSTATIC: + *fstatic=TRUE; + break; + case tPUBLIC: + *fpublic=TRUE; + break; + } /* switch */ + + err=0; + do { + tok=lex(&val,&str); /* read in (new) token */ + switch (tok) { + case tCONST: + if (*fconst) + err=42; /* invalid combination of class specifiers */ + *fconst=TRUE; + break; + case tSTOCK: + if (*fstock) + err=42; /* invalid combination of class specifiers */ + *fstock=TRUE; + break; + case tSTATIC: + if (*fstatic) + err=42; /* invalid combination of class specifiers */ + *fstatic=TRUE; + break; + case tPUBLIC: + if (*fpublic) + err=42; /* invalid combination of class specifiers */ + *fpublic=TRUE; + break; + default: + lexpush(); + tok=0; /* force break out of loop */ + } /* switch */ + } while (tok && err==0); + + /* extra checks */ + if (*fstatic && *fpublic) { + err=42; /* invalid combination of class specifiers */ + *fstatic=*fpublic=FALSE; + } /* if */ + + if (err) + error(err); + return err==0; +} + +/* parse - process all input text + * + * At this level, only static declarations and function definitions are legal. + */ +static void parse(void) +{ + int tok,fconst,fstock,fstatic,fpublic; + cell val; + char *str; + + while (freading){ + /* first try whether a declaration possibly is native or public */ + tok=lex(&val,&str); /* read in (new) token */ + switch (tok) { + case 0: + /* ignore zero's */ + break; + case tNEW: + if (getclassspec(tok,&fpublic,&fstatic,&fstock,&fconst)) + declglb(NULL,0,fpublic,fstatic,fstock,fconst); + break; + case tSTATIC: + /* This can be a static function or a static global variable; we know + * which of the two as soon as we have parsed up to the point where an + * opening paranthesis of a function would be expected. To back out after + * deciding it was a declaration of a static variable after all, we have + * to store the symbol name and tag. + */ + if (getclassspec(tok,&fpublic,&fstatic,&fstock,&fconst)) { + assert(!fpublic); + declfuncvar(fpublic,fstatic,fstock,fconst); + } /* if */ + break; + case tCONST: + decl_const(sGLOBAL); + break; + case tENUM: + decl_enum(sGLOBAL); + break; + case tPUBLIC: + /* This can be a public function or a public variable; see the comment + * above (for static functions/variables) for details. + */ + if (getclassspec(tok,&fpublic,&fstatic,&fstock,&fconst)) { + assert(!fstatic); + declfuncvar(fpublic,fstatic,fstock,fconst); + } /* if */ + break; + case tSTOCK: + /* This can be a stock function or a stock *global*) variable; see the + * comment above (for static functions/variables) for details. + */ + if (getclassspec(tok,&fpublic,&fstatic,&fstock,&fconst)) { + assert(fstock); + declfuncvar(fpublic,fstatic,fstock,fconst); + } /* if */ + break; + case tLABEL: + case tSYMBOL: + case tOPERATOR: + lexpush(); + if (!newfunc(NULL,-1,FALSE,FALSE,FALSE)) { + error(10); /* illegal function or declaration */ + lexclr(TRUE); /* drop the rest of the line */ + litidx=0; /* drop the literal queue too */ + } /* if */ + break; + case tNATIVE: + funcstub(TRUE); /* create a dummy function */ + break; + case tFORWARD: + funcstub(FALSE); + break; + case '}': + error(54); /* unmatched closing brace */ + break; + case '{': + error(55); /* start of function body without function header */ + break; + default: + if (freading) { + error(10); /* illegal function or declaration */ + lexclr(TRUE); /* drop the rest of the line */ + litidx=0; /* drop any literal arrays (strings) */ + } /* if */ + } /* switch */ + } /* while */ +} + +/* dumplits + * + * Dump the literal pool (strings etc.) + * + * Global references: litidx (referred to only) + */ +static void dumplits(void) +{ + int j,k; + + k=0; + while (k=litidx) + stgwrite("\n"); /* force a newline after 10 dumps */ + /* Note: stgwrite() buffers a line until it is complete. It recognizes + * the end of line as a sequence of "\n\0", so something like "\n\t" + * so should not be passed to stgwrite(). + */ + } /* while */ + } /* while */ +} + +/* dumpzero + * + * Dump zero's for default initial values + */ +static void dumpzero(int count) +{ + int i; + + if (count<=0) + return; + assert(curseg==2); + defstorage(); + i=0; + while (count-- > 0) { + outval(0, FALSE); + i=(i+1) % 16; + stgwrite((i==0 || count==0) ? "\n" : " "); + if (i==0 && count>0) + defstorage(); + } /* while */ +} + +static void aligndata(int numbytes) +{ + assert(numbytes % sizeof(cell) == 0); /* alignment must be a multiple of + * the cell size */ + assert(numbytes!=0); + + if ((((glb_declared+litidx)*sizeof(cell)) % numbytes)!=0) { + while ((((glb_declared+litidx)*sizeof(cell)) % numbytes)!=0) + litadd(0); + } /* if */ + +} + +#if !defined SC_LIGHT +/* sc_attachdocumentation() + * appends documentation comments to the passed-in symbol, or to a global + * string if "sym" is NULL. + */ +void sc_attachdocumentation(symbol *sym) +{ + int line; + size_t length; + char *str,*doc; + + if (!sc_makereport || sc_status!=statFIRST || sc_parsenum>0) { + /* just clear the entire table */ + delete_docstringtable(); + return; + } /* if */ + /* in the case of state functions, multiple documentation sections may + * appear; we should concatenate these + */ + assert(sym==NULL || sym->documentation==NULL || sym->states!=NULL); + + /* first check the size */ + length=0; + for (line=0; (str=get_docstring(line))!=NULL && *str!=sDOCSEP; line++) { + if (length>0) + length++; /* count 1 extra for a separating space */ + length+=strlen(str); + } /* for */ + if (sym==NULL && sc_documentation!=NULL) { + length += strlen(sc_documentation) + 1 + 4; /* plus 4 for "

" */ + assert(length>strlen(sc_documentation)); + } /* if */ + + if (length>0) { + /* allocate memory for the documentation */ + if (sym!=NULL && sym->documentation!=NULL) + length+=strlen(sym->documentation) + 1 + 4;/* plus 4 for "

" */ + doc=(char*)malloc((length+1)*sizeof(char)); + if (doc!=NULL) { + /* initialize string or concatenate */ + if (sym==NULL && sc_documentation!=NULL) { + strcpy(doc,sc_documentation); + strcat(doc,"

"); + } else if (sym!=NULL && sym->documentation!=NULL) { + strcpy(doc,sym->documentation); + strcat(doc,"

"); + free(sym->documentation); + sym->documentation=NULL; + } else { + doc[0]='\0'; + } /* if */ + /* collect all documentation */ + while ((str=get_docstring(0))!=NULL && *str!=sDOCSEP) { + if (doc[0]!='\0') + strcat(doc," "); + strcat(doc,str); + delete_docstring(0); + } /* while */ + if (str!=NULL) { + /* also delete the separator */ + assert(*str==sDOCSEP); + delete_docstring(0); + } /* if */ + if (sym!=NULL) { + assert(sym->documentation==NULL); + sym->documentation=doc; + } else { + if (sc_documentation!=NULL) + free(sc_documentation); + sc_documentation=doc; + } /* if */ + } /* if */ + } else { + /* delete an empty separator, if present */ + if ((str=get_docstring(0))!=NULL && *str==sDOCSEP) + delete_docstring(0); + } /* if */ +} + +static void insert_docstring_separator(void) +{ + char sep[2]={sDOCSEP,'\0'}; + insert_docstring(sep); +} +#else + #define sc_attachdocumentation(s) (void)(s) + #define insert_docstring_separator() +#endif + +static void declfuncvar(int fpublic,int fstatic,int fstock,int fconst) +{ + char name[sNAMEMAX+11]; + int tok,tag; + char *str; + cell val; + int invalidfunc; + + tag=pc_addtag(NULL); + tok=lex(&val,&str); + /* if we arrived here, this may not be a declaration of a native function + * or variable + */ + if (tok==tNATIVE) { + error(42); /* invalid combination of class specifiers */ + return; + } /* if */ + + if (tok!=tSYMBOL && tok!=tOPERATOR) { + lexpush(); + needtoken(tSYMBOL); + lexclr(TRUE); /* drop the rest of the line */ + litidx=0; /* drop the literal queue too */ + return; + } /* if */ + if (tok==tOPERATOR) { + lexpush(); /* push "operator" keyword back (for later analysis) */ + if (!newfunc(NULL,tag,fpublic,fstatic,fstock)) { + error(10); /* illegal function or declaration */ + lexclr(TRUE); /* drop the rest of the line */ + litidx=0; /* drop the literal queue too */ + } /* if */ + } else { + /* so tok is tSYMBOL */ + assert(strlen(str)<=sNAMEMAX); + strcpy(name,str); + /* only variables can be "const" or both "public" and "stock" */ + invalidfunc= fconst || (fpublic && fstock); + if (invalidfunc || !newfunc(name,tag,fpublic,fstatic,fstock)) { + /* if not a function, try a global variable */ + declglb(name,tag,fpublic,fstatic,fstock,fconst); + } /* if */ + } /* if */ +} + +/* declglb - declare global symbols + * + * Declare a static (global) variable. Global variables are stored in + * the DATA segment. + * + * global references: glb_declared (altered) + */ +static void declglb(char *firstname,int firsttag,int fpublic,int fstatic,int fstock,int fconst) +{ + int ident,tag,ispublic; + int idxtag[sDIMEN_MAX]; + char name[sNAMEMAX+1]; + cell val,size,cidx; + char *str; + int dim[sDIMEN_MAX]; + int numdim; + short filenum; + symbol *sym; + constvalue *enumroot; + #if !defined NDEBUG + cell glbdecl=0; + #endif + + assert(!fpublic || !fstatic); /* may not both be set */ + insert_docstring_separator(); /* see comment in newfunc() */ + filenum=fcurrent; /* save file number at the start of the declaration */ + do { + size=1; /* single size (no array) */ + numdim=0; /* no dimensions */ + ident=iVARIABLE; + if (firstname!=NULL) { + assert(strlen(firstname)<=sNAMEMAX); + strcpy(name,firstname); /* save symbol name */ + tag=firsttag; + firstname=NULL; + } else { + tag=pc_addtag(NULL); + if (lex(&val,&str)!=tSYMBOL) /* read in (new) token */ + error(20,str); /* invalid symbol name */ + assert(strlen(str)<=sNAMEMAX); + strcpy(name,str); /* save symbol name */ + } /* if */ + sym=findglb(name); + if (sym==NULL) + sym=findconst(name); + if (sym!=NULL && (sym->usage & uDEFINE)!=0) + error(21,name); /* symbol already defined */ + ispublic=fpublic; + if (name[0]==PUBLIC_CHAR) { + ispublic=TRUE; /* implicitly public variable */ + assert(!fstatic); + } /* if */ + while (matchtoken('[')) { + ident=iARRAY; + if (numdim == sDIMEN_MAX) { + error(53); /* exceeding maximum number of dimensions */ + return; + } /* if */ + size=needsub(&idxtag[numdim],&enumroot); /* get size; size==0 for "var[]" */ + #if INT_MAX < LONG_MAX + if (size > INT_MAX) + error(105); /* overflow, exceeding capacity */ + #endif + if (ispublic) + error(56,name); /* arrays cannot be public */ + dim[numdim++]=(int)size; + } /* while */ + /* if this variable is never used (which can be detected only in the + * second stage), shut off code generation + */ + cidx=0; /* only to avoid a compiler warning */ + if (sc_status==statWRITE && sym!=NULL && (sym->usage & (uREAD | uWRITTEN))==0) { + sc_status=statSKIP; + cidx=code_idx; + #if !defined NDEBUG + glbdecl=glb_declared; + #endif + } /* if */ + begdseg(); /* real (initialized) data in data segment */ + assert(litidx==0); /* literal queue should be empty */ + if (sc_alignnext) { + litidx=0; + aligndata(sc_dataalign); + dumplits(); /* dump the literal queue */ + sc_alignnext=FALSE; + litidx=0; /* global initial data is dumped, so restart at zero */ + } /* if */ + assert(litidx==0); /* literal queue should be empty (again) */ + initials(ident,tag,&size,dim,numdim,enumroot);/* stores values in the literal queue */ + assert(size>=litidx); + if (numdim==1) + dim[0]=(int)size; + dumplits(); /* dump the literal queue */ + dumpzero((int)size-litidx); + litidx=0; + if (sym==NULL) { /* define only if not yet defined */ + sym=addvariable(name,sizeof(cell)*glb_declared,ident,sGLOBAL,tag, + dim,numdim,idxtag); + } else { /* if declared but not yet defined, adjust the variable's address */ + sym->addr=sizeof(cell)*glb_declared; + sym->codeaddr=code_idx; + sym->usage|=uDEFINE; + } /* if */ + if (ispublic) + sym->usage|=uPUBLIC; + if (fconst) + sym->usage|=uCONST; + if (fstock) + sym->usage|=uSTOCK; + if (fstatic) + sym->fnumber=filenum; + sc_attachdocumentation(sym);/* attach any documenation to the variable */ + if (sc_status==statSKIP) { + sc_status=statWRITE; + code_idx=cidx; + assert(glb_declared==glbdecl); + } else { + glb_declared+=(int)size; /* add total number of cells */ + } /* if */ + } while (matchtoken(',')); /* enddo */ /* more? */ + needtoken(tTERM); /* if not comma, must be semicolumn */ +} + +/* declloc - declare local symbols + * + * Declare local (automatic) variables. Since these variables are relative + * to the STACK, there is no switch to the DATA segment. These variables + * cannot be initialized either. + * + * global references: declared (altered) + * funcstatus (referred to only) + */ +static int declloc(int fstatic) +{ + int ident,tag; + int idxtag[sDIMEN_MAX]; + char name[sNAMEMAX+1]; + symbol *sym; + constvalue *enumroot; + cell val,size; + char *str; + value lval = {0}; + int cur_lit=0; + int dim[sDIMEN_MAX]; + int numdim; + int fconst; + int staging_start; + + fconst=matchtoken(tCONST); + do { + ident=iVARIABLE; + size=1; + numdim=0; /* no dimensions */ + tag=pc_addtag(NULL); + if (lex(&val,&str)!=tSYMBOL) /* read in (new) token */ + error(20,str); /* invalid symbol name */ + assert(strlen(str)<=sNAMEMAX); + strcpy(name,str); /* save symbol name */ + if (name[0]==PUBLIC_CHAR) + error(56,name); /* local variables cannot be public */ + /* Note: block locals may be named identical to locals at higher + * compound blocks (as with standard C); so we must check (and add) + * the "nesting level" of local variables to verify the + * multi-definition of symbols. + */ + if ((sym=findloc(name))!=NULL && sym->compound==nestlevel) + error(21,name); /* symbol already defined */ + /* Although valid, a local variable whose name is equal to that + * of a global variable or to that of a local variable at a lower + * level might indicate a bug. + */ + if ((sym=findloc(name))!=NULL && sym->compound!=nestlevel || findglb(name)!=NULL) + error(219,name); /* variable shadows another symbol */ + while (matchtoken('[')){ + ident=iARRAY; + if (numdim == sDIMEN_MAX) { + error(53); /* exceeding maximum number of dimensions */ + return ident; + } /* if */ + size=needsub(&idxtag[numdim],&enumroot); /* get size; size==0 for "var[]" */ + #if INT_MAX < LONG_MAX + if (size > INT_MAX) + error(105); /* overflow, exceeding capacity */ + #endif + dim[numdim++]=(int)size; + } /* while */ + if (ident==iARRAY || fstatic) { + if (sc_alignnext) { + aligndata(sc_dataalign); + sc_alignnext=FALSE; + } /* if */ + cur_lit=litidx; /* save current index in the literal table */ + initials(ident,tag,&size,dim,numdim,enumroot); + if (size==0) + return ident; /* error message already given */ + if (numdim==1) + dim[0]=(int)size; + } /* if */ + /* reserve memory (on the stack) for the variable */ + if (fstatic) { + /* write zeros for uninitialized fields */ + while (litidxusage & uNATIVE)==0); + if (curfunc->x.stacksizex.stacksize=declared+1; /* +1 for PROC opcode */ + } /* if */ + /* now that we have reserved memory for the variable, we can proceed + * to initialize it */ + assert(sym!=NULL); /* we declared it, it must be there */ + sym->compound=nestlevel; /* for multiple declaration/shadowing check */ + if (fconst) + sym->usage|=uCONST; + if (!fstatic) { /* static variables already initialized */ + if (ident==iVARIABLE) { + /* simple variable, also supports initialization */ + int ctag = tag; /* set to "tag" by default */ + int explicit_init=FALSE;/* is the variable explicitly initialized? */ + if (matchtoken('=')) { + doexpr(FALSE,FALSE,FALSE,FALSE,&ctag,NULL,TRUE); + explicit_init=TRUE; + } else { + ldconst(0,sPRI); /* uninitialized variable, set to zero */ + } /* if */ + /* now try to save the value (still in PRI) in the variable */ + lval.sym=sym; + lval.ident=iVARIABLE; + lval.constval=0; + lval.tag=tag; + check_userop(NULL,ctag,lval.tag,2,NULL,&ctag); + store(&lval); + markexpr(sEXPR,NULL,0); /* full expression ends after the store */ + assert(staging); /* end staging phase (optimize expression) */ + stgout(staging_start); + stgset(FALSE); + if (!matchtag(tag,ctag,TRUE)) + error(213); /* tag mismatch */ + /* if the variable was not explicitly initialized, reset the + * "uWRITTEN" flag that store() set */ + if (!explicit_init) + sym->usage &= ~uWRITTEN; + } else { + /* an array */ + assert(cur_lit>=0 && cur_lit<=litidx && litidx<=litmax); + /* if the array is not completely filled, set all values to zero first */ + assert(size>0 && size>=sym->dim.array.length); + assert(numdim>1 || size==sym->dim.array.length); + if (litidx-cur_lit < size) + fillarray(sym,size*sizeof(cell),0); + if (cur_lit=0 && cur<=numdim); + if (cur==numdim) + return 0; + return dim[cur]+(dim[cur]*calc_arraysize(dim,numdim,cur+1)); +} + +static cell adjust_indirectiontables(int dim[],int numdim,int cur,cell increment, + int startlit,constvalue *lastdim,int *skipdim) +{ +static int base; + int d; + cell accum; + + assert(cur>=0 && cur=0); + assert(cur>0 && startlit==-1 || startlit>=0 && startlit<=litidx); + if (cur==0) + base=startlit; + if (cur==numdim-1) + return 0; + /* 2 or more dimensions left, fill in an indirection vector */ + assert(dim[cur]>0); + if (dim[cur+1]>0) { + for (d=0; dnext; d<*skipdim; d++,ld=ld->next) { + assert(ld!=NULL); + } /* for */ + for (d=0; dname,NULL,16)==d); + litq[base++]=(dim[cur]+accum+increment) * sizeof(cell); + accum+=ld->value-1; + *skipdim+=1; + ld=ld->next; + } /* for */ + } /* if */ + /* create the indirection tables for the lower level */ + if (cur+2=dim[cur]) { + error(18); /* initialization data exceeds array size */ + break; + } /* if */ + if (cur+20) { + if (idxcounteddim[cur]) + error(18); /* initialization data exceeds declared size */ + } /* if */ + counteddim[cur]=idx; + + return totalsize+dim[cur]; /* size of sub-arrays + indirection vector */ +} + +/* initvector + * Initialize a single dimensional array + */ +static cell initvector(int ident,int tag,cell size,int fillzero, + constvalue *enumroot,int *errorfound) +{ + cell prev1=0,prev2=0; + int ellips=FALSE; + int curlit=litidx; + int rtag,ctag; + + assert(ident==iARRAY || ident==iREFARRAY); + if (matchtoken('{')) { + constvalue *enumfield=(enumroot!=NULL) ? enumroot->next : NULL; + do { + int fieldlit=litidx; + int matchbrace,i; + if (matchtoken('}')) { /* to allow for trailing ',' after the initialization */ + lexpush(); + break; + } /* if */ + if ((ellips=matchtoken(tELLIPS))!=0) + break; + /* for enumeration fields, allow another level of braces ("{...}") */ + matchbrace=0; /* preset */ + ellips=0; + if (enumfield!=NULL) + matchbrace=matchtoken('{'); + for ( ;; ) { + prev2=prev1; + prev1=init(ident,&ctag,errorfound); + if (!matchbrace) + break; + if ((ellips=matchtoken(tELLIPS))!=0) + break; + if (!matchtoken(',')) { + needtoken('}'); + break; + } /* for */ + } /* for */ + /* if this array is based on an enumeration, fill the "field" up with + * zeros, and toggle the tag + */ + if (enumroot!=NULL && enumfield==NULL) + error(227); /* more initiallers than enum fields */ + rtag=tag; /* preset, may be overridden by enum field tag */ + if (enumfield!=NULL) { + cell step; + symbol *symfield=findconst(enumfield->name); + assert(symfield!=NULL); + assert(fieldlitsymfield->dim.array.length) + error(228); /* length of initialler exceeds size of the enum field */ + if (ellips) { + step=prev1-prev2; + } else { + step=0; + prev1=0; + } /* if */ + for (i=litidx-fieldlit; idim.array.length; i++) { + prev1+=step; + litadd(prev1); + } /* for */ + rtag=symfield->x.idxtag; /* set the expected tag to the index tag */ + enumfield=enumfield->next; + } /* if */ + if (!matchtag(rtag,ctag,TRUE)) + error(213); /* tag mismatch */ + } while (matchtoken(',')); /* do */ + needtoken('}'); + } else { + init(ident,&ctag,errorfound); + if (!matchtag(tag,ctag,TRUE)) + error(213); /* tagname mismatch */ + } /* if */ + /* fill up the literal queue with a series */ + if (ellips) { + cell step=((litidx-curlit)==1) ? (cell)0 : prev1-prev2; + if (size==0 || (litidx-curlit)==0) + error(41); /* invalid ellipsis, array size unknown */ + else if ((litidx-curlit)==(int)size) + error(18); /* initialisation data exceeds declared size */ + while ((litidx-curlit)<(int)size) { + prev1+=step; + litadd(prev1); + } /* while */ + } /* if */ + if (fillzero && size>0) { + while ((litidx-curlit)<(int)size) + litadd(0); + } /* if */ + if (size==0) { + size=litidx-curlit; /* number of elements defined */ + } else if (litidx-curlit>(int)size) { /* e.g. "myvar[3]={1,2,3,4};" */ + error(18); /* initialisation data exceeds declared size */ + litidx=(int)size+curlit; /* avoid overflow in memory moves */ + } /* if */ + return size; +} + +/* init + * + * Evaluate one initializer. + */ +static cell init(int ident,int *tag,int *errorfound) +{ + cell i = 0; + + if (matchtoken(tSTRING)){ + /* lex() automatically stores strings in the literal table (and + * increases "litidx") */ + if (ident==iVARIABLE) { + error(6); /* must be assigned to an array */ + litidx=1; /* reset literal queue */ + } /* if */ + *tag=0; + } else if (constexpr(&i,tag,NULL)){ + litadd(i); /* store expression result in literal table */ + } else { + if (errorfound!=NULL) + *errorfound=TRUE; + } /* if */ + return i; +} + +/* needsub + * + * Get required array size + */ +static cell needsub(int *tag,constvalue **enumroot) +{ + cell val; + symbol *sym; + + assert(tag!=NULL); + *tag=0; + if (enumroot!=NULL) + *enumroot=NULL; /* preset */ + if (matchtoken(']')) /* we have already seen "[" */ + return 0; /* zero size (like "char msg[]") */ + + constexpr(&val,tag,&sym); /* get value (must be constant expression) */ + if (val<0) { + error(9); /* negative array size is invalid; assumed zero */ + val=0; + } /* if */ + needtoken(']'); + + if (enumroot!=NULL) { + /* get the field list for an enumeration */ + assert(*enumroot==NULL);/* should have been preset */ + assert(sym==NULL || sym->ident==iCONSTEXPR); + if (sym!=NULL && (sym->usage & uENUMROOT)==uENUMROOT) { + assert(sym->dim.enumlist!=NULL); + *enumroot=sym->dim.enumlist; + } /* if */ + } /* if */ + + return val; /* return array size */ +} + +/* decl_const - declare a single constant + * + */ +static void decl_const(int vclass) +{ + char constname[sNAMEMAX+1]; + cell val; + char *str; + int tag,exprtag; + int symbolline; + symbol *sym; + + insert_docstring_separator(); /* see comment in newfunc() */ + tag=pc_addtag(NULL); + if (lex(&val,&str)!=tSYMBOL) /* read in (new) token */ + error(20,str); /* invalid symbol name */ + symbolline=fline; /* save line where symbol was found */ + strcpy(constname,str); /* save symbol name */ + needtoken('='); + constexpr(&val,&exprtag,NULL);/* get value */ + needtoken(tTERM); + /* add_constant() checks for duplicate definitions */ + if (!matchtag(tag,exprtag,FALSE)) { + /* temporarily reset the line number to where the symbol was defined */ + int orgfline=fline; + fline=symbolline; + error(213); /* tagname mismatch */ + fline=orgfline; + } /* if */ + sym=add_constant(constname,val,vclass,tag); + if (sym!=NULL) + sc_attachdocumentation(sym);/* attach any documenation to the function */ +} + +/* decl_enum - declare enumerated constants + * + */ +static void decl_enum(int vclass) +{ + char enumname[sNAMEMAX+1],constname[sNAMEMAX+1]; + cell val,value,size; + char *str; + int tok,tag,explicittag; + cell increment,multiplier; + constvalue *enumroot; + symbol *enumsym; + + /* get an explicit tag, if any (we need to remember whether an explicit + * tag was passed, even if that explicit tag was "_:", so we cannot call + * pc_addtag() here + */ + if (lex(&val,&str)==tLABEL) { + tag=pc_addtag(str); + explicittag=TRUE; + } else { + lexpush(); + tag=0; + explicittag=FALSE; + } /* if */ + + /* get optional enum name (also serves as a tag if no explicit tag was set) */ + if (lex(&val,&str)==tSYMBOL) { /* read in (new) token */ + strcpy(enumname,str); /* save enum name (last constant) */ + if (!explicittag) + tag=pc_addtag(enumname); + } else { + lexpush(); /* analyze again */ + enumname[0]='\0'; + } /* if */ + + /* get increment and multiplier */ + increment=1; + multiplier=1; + if (matchtoken('(')) { + if (matchtoken(taADD)) { + constexpr(&increment,NULL,NULL); + } else if (matchtoken(taMULT)) { + constexpr(&multiplier,NULL,NULL); + } else if (matchtoken(taSHL)) { + constexpr(&val,NULL,NULL); + while (val-->0) + multiplier*=2; + } /* if */ + needtoken(')'); + } /* if */ + + if (strlen(enumname)>0) { + /* already create the root symbol, so the fields can have it as their "parent" */ + enumsym=add_constant(enumname,0,vclass,tag); + if (enumsym!=NULL) + enumsym->usage |= uENUMROOT; + /* start a new list for the element names */ + if ((enumroot=(constvalue*)malloc(sizeof(constvalue)))==NULL) + error(103); /* insufficient memory (fatal error) */ + memset(enumroot,0,sizeof(constvalue)); + } else { + enumsym=NULL; + enumroot=NULL; + } /* if */ + + needtoken('{'); + /* go through all constants */ + value=0; /* default starting value */ + do { + int idxtag,fieldtag; + symbol *sym; + if (matchtoken('}')) { /* quick exit if '}' follows ',' */ + lexpush(); + break; + } /* if */ + idxtag=pc_addtag(NULL); /* optional explicit item tag */ + tok=lex(&val,&str); /* read in (new) token */ + if (tok!=tSYMBOL) + error(20,str); /* invalid symbol name */ + strcpy(constname,str); /* save symbol name */ + size=increment; /* default increment of 'val' */ + if (matchtoken('[')) { + constexpr(&size,&fieldtag,NULL); /* get size */ + needtoken(']'); + } /* if */ + if (matchtoken('=')) + constexpr(&value,NULL,NULL); /* get value */ + /* add_constant() checks whether a variable (global or local) or + * a constant with the same name already exists + */ + sym=add_constant(constname,value,vclass,tag); + if (sym==NULL) + continue; /* error message already given */ + /* set the item tag and the item size, for use in indexing arrays */ + sym->x.idxtag=idxtag; + sym->fieldtag=fieldtag; + sym->dim.array.length=size; + sym->dim.array.level=0; + sym->parent=enumsym; + /* add the constant to a separate list as well */ + if (enumroot!=NULL) { + sym->usage |= uENUMFIELD; + append_constval(enumroot,constname,value,0); + } /* if */ + if (multiplier==1) + value+=size; + else + value*=size*multiplier; + } while (matchtoken(',')); + needtoken('}'); /* terminates the constant list */ + matchtoken(';'); /* eat an optional ; */ + + /* set the enum name to the "next" value (typically the last value plus one) */ + if (enumsym!=NULL) { + assert((enumsym->usage & uENUMROOT)!=0); + enumsym->addr=value; + /* assign the constant list */ + assert(enumroot!=NULL); + enumsym->dim.enumlist=enumroot; + sc_attachdocumentation(enumsym); /* attach any documenation to the enumeration */ + } /* if */ +} + +static int getstates(const char *funcname) +{ + char fsaname[sNAMEMAX+1],statename[sNAMEMAX+1]; + cell val; + char *str; + constvalue *automaton; + constvalue *state; + int fsa,islabel; + int *list; + int count,listsize,state_id; + + if (!matchtoken('<')) + return 0; + if (matchtoken('>')) + return -1; /* special construct: all other states (fall-back) */ + + count=0; + listsize=0; + list=NULL; + fsa=-1; + + do { + if (!(islabel=matchtoken(tLABEL)) && !needtoken(tSYMBOL)) + break; + tokeninfo(&val,&str); + assert(strlen(str)=0 && automaton->index!=fsa) + error(83,funcname); /* multiple automatons for a single function */ + fsa=automaton->index; + } /* if */ + state=state_add(statename,fsa); + /* add this state to the state combination list (it will be attached to the + * automaton later) */ + state_buildlist(&list,&listsize,&count,(int)state->value); + } while (matchtoken(',')); + needtoken('>'); + + if (count>0) { + assert(automaton!=NULL); + assert(fsa>=0); + state_id=state_addlist(list,count,fsa); + assert(state_id>0); + } else { + /* error is already given */ + state_id=0; + } /* if */ + if (list!=NULL) + free(list); + + return state_id; +} + +static void attachstatelist(symbol *sym, int state_id) +{ + assert(sym!=NULL); + if ((sym->usage & uDEFINE)!=0 && (sym->states==NULL || state_id==0)) + error(21,sym->name); /* function already defined, either without states or the current definition has no states */ + + if (state_id!=0) { + /* add the state list id */ + constvalue *stateptr; + if (sym->states==NULL) { + if ((sym->states=(constvalue*)malloc(sizeof(constvalue)))==NULL) + error(103); /* insufficient memory (fatal error) */ + memset(sym->states,0,sizeof(constvalue)); + } /* if */ + /* see whether the id already exists (add new state only if it does not + * yet exist + */ + assert(sym->states!=NULL); + for (stateptr=sym->states->next; stateptr!=NULL && stateptr->index!=state_id; stateptr=stateptr->next) + /* nothing */; + assert(state_id<=SHRT_MAX); + if (stateptr==NULL) + append_constval(sym->states,"",code_idx,(short)state_id); + else if (stateptr->value==0) + stateptr->value=code_idx; + else + error(84,sym->name); + /* also check for another conflicting situation: a fallback function + * without any states + */ + if (state_id==-1 && sc_status!=statFIRST) { + /* in the second round, all states should have been accumulated */ + assert(sym->states!=NULL); + for (stateptr=sym->states->next; stateptr!=NULL && stateptr->index==-1; stateptr=stateptr->next) + /* nothing */; + if (stateptr==NULL) + error(85,sym->name); /* no states are defined for this function */ + } /* if */ + } /* if */ +} + +/* + * Finds a function in the global symbol table or creates a new entry. + * It does some basic processing and error checking. + */ +SC_FUNC symbol *fetchfunc(char *name,int tag) +{ + symbol *sym; + + if ((sym=findglb(name))!=0) { /* already in symbol table? */ + if (sym->ident!=iFUNCTN) { + error(21,name); /* yes, but not as a function */ + return NULL; /* make sure the old symbol is not damaged */ + } else if ((sym->usage & uNATIVE)!=0) { + error(21,name); /* yes, and it is a native */ + } /* if */ + assert(sym->vclass==sGLOBAL); + if ((sym->usage & uPROTOTYPED)!=0 && sym->tag!=tag) + error(25); /* mismatch from earlier prototype */ + if ((sym->usage & uDEFINE)==0) { + /* as long as the function stays undefined, update the address and the tag */ + if (sym->states==NULL) + sym->addr=code_idx; + sym->tag=tag; + } /* if */ + } else { + /* don't set the "uDEFINE" flag; it may be a prototype */ + sym=addsym(name,code_idx,iFUNCTN,sGLOBAL,tag,0); + assert(sym!=NULL); /* fatal error 103 must be given on error */ + /* assume no arguments */ + sym->dim.arglist=(arginfo*)malloc(1*sizeof(arginfo)); + sym->dim.arglist[0].ident=0; + /* set library ID to NULL (only for native functions) */ + sym->x.lib=NULL; + /* set the required stack size to zero (only for non-native functions) */ + sym->x.stacksize=1; /* 1 for PROC opcode */ + } /* if */ + + return sym; +} + +/* This routine adds symbolic information for each argument. + */ +static void define_args(void) +{ + symbol *sym; + + /* At this point, no local variables have been declared. All + * local symbols are function arguments. + */ + sym=loctab.next; + while (sym!=NULL) { + assert(sym->ident!=iLABEL); + assert(sym->vclass==sLOCAL); + markexpr(sLDECL,sym->name,sym->addr); /* mark for better optimization */ + sym=sym->next; + } /* while */ +} + +static int operatorname(char *name) +{ + int opertok; + char *str; + cell val; + + assert(name!=NULL); + + /* check the operator */ + opertok=lex(&val,&str); + switch (opertok) { + case '+': + case '-': + case '*': + case '/': + case '%': + case '>': + case '<': + case '!': + case '~': + case '=': + name[0]=(char)opertok; + name[1]='\0'; + break; + case tINC: + strcpy(name,"++"); + break; + case tDEC: + strcpy(name,"--"); + break; + case tlEQ: + strcpy(name,"=="); + break; + case tlNE: + strcpy(name,"!="); + break; + case tlLE: + strcpy(name,"<="); + break; + case tlGE: + strcpy(name,">="); + break; + default: + name[0]='\0'; + error(7); /* operator cannot be redefined (or bad operator name) */ + return 0; + } /* switch */ + + return opertok; +} + +static int operatoradjust(int opertok,symbol *sym,char *opername,int resulttag) +{ + int tags[2]={0,0}; + int count=0; + arginfo *arg; + char tmpname[sNAMEMAX+1]; + symbol *oldsym; + + if (opertok==0) + return TRUE; + + assert(sym!=NULL && sym->ident==iFUNCTN && sym->dim.arglist!=NULL); + /* count arguments and save (first two) tags */ + while (arg=&sym->dim.arglist[count], arg->ident!=0) { + if (count<2) { + if (arg->numtags>1) + error(65,count+1); /* function argument may only have a single tag */ + else if (arg->numtags==1) + tags[count]=arg->tags[0]; + } /* if */ + if (opertok=='~' && count==0) { + if (arg->ident!=iREFARRAY) + error(73,arg->name);/* must be an array argument */ + } else { + if (arg->ident!=iVARIABLE) + error(66,arg->name);/* must be non-reference argument */ + } /* if */ + if (arg->hasdefault) + error(59,arg->name); /* arguments of an operator may not have a default value */ + count++; + } /* while */ + + /* for '!', '++' and '--', count must be 1 + * for '-', count may be 1 or 2 + * for '=', count must be 1, and the resulttag is also important + * for all other (binary) operators and the special '~' operator, count must be 2 + */ + switch (opertok) { + case '!': + case '=': + case tINC: + case tDEC: + if (count!=1) + error(62); /* number or placement of the operands does not fit the operator */ + break; + case '-': + if (count!=1 && count!=2) + error(62); /* number or placement of the operands does not fit the operator */ + break; + default: + if (count!=2) + error(62); /* number or placement of the operands does not fit the operator */ + } /* switch */ + + if (tags[0]==0 && (opertok!='=' && tags[1]==0 || opertok=='=' && resulttag==0)) + error(64); /* cannot change predefined operators */ + + /* change the operator name */ + assert(strlen(opername)>0); + operator_symname(tmpname,opername,tags[0],tags[1],count,resulttag); + if ((oldsym=findglb(tmpname))!=NULL) { + int i; + if ((oldsym->usage & uDEFINE)!=0) { + char errname[2*sNAMEMAX+16]; + funcdisplayname(errname,tmpname); + error(21,errname); /* symbol already defined */ + } /* if */ + sym->usage|=oldsym->usage; /* copy flags from the previous definition */ + for (i=0; inumrefers; i++) + if (oldsym->refer[i]!=NULL) + refer_symbol(sym,oldsym->refer[i]); + delete_symbol(&glbtab,oldsym); + } /* if */ + strcpy(sym->name,tmpname); + sym->hash=namehash(sym->name);/* calculate new hash */ + + /* operators should return a value, except the '~' operator */ + if (opertok!='~') + sym->usage |= uRETVALUE; + + return TRUE; +} + +static int check_operatortag(int opertok,int resulttag,char *opername) +{ + assert(opername!=NULL && strlen(opername)>0); + switch (opertok) { + case '!': + case '<': + case '>': + case tlEQ: + case tlNE: + case tlLE: + case tlGE: + if (resulttag!=pc_addtag("bool")) { + error(63,opername,"bool:"); /* operator X requires a "bool:" result tag */ + return FALSE; + } /* if */ + break; + case '~': + if (resulttag!=0) { + error(63,opername,"_:"); /* operator "~" requires a "_:" result tag */ + return FALSE; + } /* if */ + break; + } /* switch */ + return TRUE; +} + +static char *tag2str(char *dest,int tag) +{ + tag &= TAGMASK; + assert(tag>=0); + sprintf(dest,"0%x",tag); + return isdigit(dest[1]) ? &dest[1] : dest; +} + +SC_FUNC char *operator_symname(char *symname,char *opername,int tag1,int tag2,int numtags,int resulttag) +{ + char tagstr1[10], tagstr2[10]; + int opertok; + + assert(numtags>=1 && numtags<=2); + opertok= (opername[1]=='\0') ? opername[0] : 0; + if (opertok=='=') + sprintf(symname,"%s%s%s",tag2str(tagstr1,resulttag),opername,tag2str(tagstr2,tag1)); + else if (numtags==1 || opertok=='~') + sprintf(symname,"%s%s",opername,tag2str(tagstr1,tag1)); + else + sprintf(symname,"%s%s%s",tag2str(tagstr1,tag1),opername,tag2str(tagstr2,tag2)); + return symname; +} + +static int parse_funcname(char *fname,int *tag1,int *tag2,char *opname) +{ + char *ptr,*name; + int unary; + + /* tags are only positive, so if the function name starts with a '-', + * the operator is an unary '-' or '--' operator. + */ + if (*fname=='-') { + *tag1=0; + unary=TRUE; + ptr=fname; + } else { + *tag1=(int)strtol(fname,&ptr,16); + unary= ptr==fname; /* unary operator if it doesn't start with a tag name */ + } /* if */ + assert(!unary || *tag1==0); + assert(*ptr!='\0'); + for (name=opname; !isdigit(*ptr); ) + *name++ = *ptr++; + *name='\0'; + *tag2=(int)strtol(ptr,NULL,16); + return unary; +} + +static constvalue *find_tag_byval(int tag) +{ + constvalue *tagsym; + tagsym=find_constval_byval(&tagname_tab,tag & ~PUBLICTAG); + if (tagsym==NULL) + tagsym=find_constval_byval(&tagname_tab,tag | PUBLICTAG); + return tagsym; +} + +SC_FUNC char *funcdisplayname(char *dest,char *funcname) +{ + int tags[2]; + char opname[10]; + constvalue *tagsym[2]; + int unary; + + if (isalpha(*funcname) || *funcname=='_' || *funcname==PUBLIC_CHAR || *funcname=='\0') { + if (dest!=funcname) + strcpy(dest,funcname); + return dest; + } /* if */ + + unary=parse_funcname(funcname,&tags[0],&tags[1],opname); + tagsym[1]=find_tag_byval(tags[1]); + assert(tagsym[1]!=NULL); + if (unary) { + sprintf(dest,"operator%s(%s:)",opname,tagsym[1]->name); + } else { + tagsym[0]=find_tag_byval(tags[0]); + assert(tagsym[0]!=NULL); + /* special case: the assignment operator has the return value as the 2nd tag */ + if (opname[0]=='=' && opname[1]=='\0') + sprintf(dest,"%s:operator%s(%s:)",tagsym[0]->name,opname,tagsym[1]->name); + else + sprintf(dest,"operator%s(%s:,%s:)",opname,tagsym[0]->name,tagsym[1]->name); + } /* if */ + return dest; +} + +static void funcstub(int native) +{ + int tok,tag; + char *str; + cell val,size; + char symbolname[sNAMEMAX+1]; + int idxtag[sDIMEN_MAX]; + int dim[sDIMEN_MAX]; + int numdim; + symbol *sym,*sub; + int opertok; + + opertok=0; + lastst=0; + litidx=0; /* clear the literal pool */ + assert(loctab.next==NULL); /* local symbol table should be empty */ + + tag=pc_addtag(NULL); /* get the tag of the return value */ + numdim=0; + while (matchtoken('[')) { + /* the function returns an array, get this tag for the index and the array + * dimensions + */ + if (numdim == sDIMEN_MAX) { + error(53); /* exceeding maximum number of dimensions */ + return; + } /* if */ + size=needsub(&idxtag[numdim],NULL); /* get size; size==0 for "var[]" */ + if (size==0) + error(9); /* invalid array size */ + #if INT_MAX < LONG_MAX + if (size > INT_MAX) + error(105); /* overflow, exceeding capacity */ + #endif + dim[numdim++]=(int)size; + } /* while */ + + tok=lex(&val,&str); + if (native) { + if (tok==tPUBLIC || tok==tSTOCK || tok==tSTATIC || tok==tSYMBOL && *str==PUBLIC_CHAR) + error(42); /* invalid combination of class specifiers */ + } else { + if (tok==tPUBLIC || tok==tSTOCK || tok==tSTATIC) + tok=lex(&val,&str); + } /* if */ + + if (tok==tOPERATOR) { + opertok=operatorname(symbolname); + if (opertok==0) + return; /* error message already given */ + check_operatortag(opertok,tag,symbolname); + } else { + if (tok!=tSYMBOL && freading) { + error(10); /* illegal function or declaration */ + return; + } /* if */ + strcpy(symbolname,str); + } /* if */ + needtoken('('); /* only functions may be native/forward */ + + sym=fetchfunc(symbolname,tag);/* get a pointer to the function entry */ + if (sym==NULL) + return; + if (native) { + sym->usage=(char)(uNATIVE | uRETVALUE | uDEFINE | (sym->usage & uPROTOTYPED)); + sym->x.lib=curlibrary; + } /* if */ + + declargs(sym); + /* "declargs()" found the ")" */ + sc_attachdocumentation(sym); /* attach any documenation to the function */ + if (!operatoradjust(opertok,sym,symbolname,tag)) + sym->usage &= ~uDEFINE; + + if (getstates(symbolname)!=0) { + if (native || opertok!=0) + error(82); /* native functions and operators may not have states */ + else + error(231); /* ignoring state specifications on forward declarations */ + } /* if */ + + /* for a native operator, also need to specify an "exported" function name; + * for a native function, this is optional + */ + if (native) { + if (opertok!=0) { + needtoken('='); + lexpush(); /* push back, for matchtoken() to retrieve again */ + } /* if */ + if (matchtoken('=')) { + /* allow number or symbol */ + if (matchtoken(tSYMBOL)) { + tokeninfo(&val,&str); + insert_alias(sym->name,str); + } else { + constexpr(&val,NULL,NULL); + sym->addr=val; + /* At the moment, I have assumed that this syntax is only valid if + * val < 0. To properly mix "normal" native functions and indexed + * native functions, one should use negative indices anyway. + * Special code for a negative index in sym->addr exists in SC4.C + * (ffcall()) and in SC6.C (the loops for counting the number of native + * variables and for writing them). + */ + } /* if */ + } /* if */ + } /* if */ + needtoken(tTERM); + + /* attach the array to the function symbol */ + if (numdim>0) { + assert(sym!=NULL); + sub=addvariable(symbolname,0,iARRAY,sGLOBAL,tag,dim,numdim,idxtag); + sub->parent=sym; + } /* if */ + + litidx=0; /* clear the literal pool */ + delete_symbols(&loctab,0,TRUE,TRUE);/* clear local variables queue */ +} + +/* newfunc - begin a function + * + * This routine is called from "parse" and tries to make a function + * out of the following text + * + * Global references: funcstatus,lastst,litidx + * rettype (altered) + * curfunc (altered) + * declared (altered) + * glb_declared (altered) + * sc_alignnext (altered) + */ +static int newfunc(char *firstname,int firsttag,int fpublic,int fstatic,int stock) +{ + symbol *sym; + int argcnt,tok,tag,funcline; + int opertok,opererror; + char symbolname[sNAMEMAX+1]; + char *str; + cell val,cidx,glbdecl; + short filenum; + int state_id; + + assert(litidx==0); /* literal queue should be empty */ + litidx=0; /* clear the literal pool (should already be empty) */ + opertok=0; + lastst=0; /* no statement yet */ + cidx=0; /* just to avoid compiler warnings */ + glbdecl=0; + assert(loctab.next==NULL); /* local symbol table should be empty */ + filenum=fcurrent; /* save file number at the start of the declaration */ + + if (firstname!=NULL) { + assert(strlen(firstname)<=sNAMEMAX); + strcpy(symbolname,firstname); /* save symbol name */ + tag=firsttag; + } else { + tag= (firsttag>=0) ? firsttag : pc_addtag(NULL); + tok=lex(&val,&str); + assert(!fpublic); + if (tok==tNATIVE || tok==tPUBLIC && stock) + error(42); /* invalid combination of class specifiers */ + if (tok==tOPERATOR) { + opertok=operatorname(symbolname); + if (opertok==0) + return TRUE; /* error message already given */ + check_operatortag(opertok,tag,symbolname); + } else { + if (tok!=tSYMBOL && freading) { + error(20,str); /* invalid symbol name */ + return FALSE; + } /* if */ + assert(strlen(str)<=sNAMEMAX); + strcpy(symbolname,str); + } /* if */ + } /* if */ + /* check whether this is a function or a variable declaration */ + if (!matchtoken('(')) + return FALSE; + /* so it is a function, proceed */ + funcline=fline; /* save line at which the function is defined */ + if (symbolname[0]==PUBLIC_CHAR) { + fpublic=TRUE; /* implicitly public function */ + if (stock) + error(42); /* invalid combination of class specifiers */ + } /* if */ + sym=fetchfunc(symbolname,tag);/* get a pointer to the function entry */ + if (sym==NULL || (sym->usage & uNATIVE)!=0) + return TRUE; /* it was recognized as a function declaration, but not as a valid one */ + if (fpublic) + sym->usage|=uPUBLIC; + if (fstatic) + sym->fnumber=filenum; + /* if the function was used before being declared, and it has a tag for the + * result, add a third pass (as second "skimming" parse) because the function + * result may have been used with user-defined operators, which have now + * been incorrectly flagged (as the return tag was unknown at the time of + * the call) + */ + if ((sym->usage & (uPROTOTYPED | uREAD))==uREAD && sym->tag!=0) { + int curstatus=sc_status; + sc_status=statWRITE; /* temporarily set status to WRITE, so the warning isn't blocked */ + error(208); + sc_status=curstatus; + sc_reparse=TRUE; /* must add another pass to "initial scan" phase */ + } /* if */ + /* declare all arguments */ + argcnt=declargs(sym); + opererror=!operatoradjust(opertok,sym,symbolname,tag); + if (strcmp(symbolname,uMAINFUNC)==0 || strcmp(symbolname,uENTRYFUNC)==0) { + if (argcnt>0) + error(5); /* "main()" and "entry()" functions may not have any arguments */ + sym->usage|=uREAD; /* "main()" is the program's entry point: always used */ + } /* if */ + state_id=getstates(symbolname); + if (opertok!=0 && state_id>0) + error(82); /* operators may not have states */ + attachstatelist(sym,state_id); + /* "declargs()" found the ")"; if a ";" appears after this, it was a + * prototype */ + if (matchtoken(';')) { + if (!sc_needsemicolon) + error(218); /* old style prototypes used with optional semicolumns */ + delete_symbols(&loctab,0,TRUE,TRUE); /* prototype is done; forget everything */ + return TRUE; + } /* if */ + /* so it is not a prototype, proceed */ + /* if this is a function that is not referred to (this can only be detected + * in the second stage), shut code generation off */ + if (sc_status==statWRITE && (sym->usage & uREAD)==0) { + sc_status=statSKIP; + cidx=code_idx; + glbdecl=glb_declared; + } /* if */ + begcseg(); + sym->usage|=uDEFINE; /* set the definition flag */ + if (fpublic) + sym->usage|=uREAD; /* public functions are always "used" */ + if (stock) + sym->usage|=uSTOCK; + if (opertok!=0 && opererror) + sym->usage &= ~uDEFINE; + /* if the function has states, dump the label to the start of the function */ + if (state_id!=0) { + constvalue *ptr=sym->states->next; + while (ptr!=NULL) { + assert(sc_status!=statWRITE || strlen(ptr->name)>0); + if (ptr->index==state_id) { + setlabel((int)strtol(ptr->name,NULL,16)); + break; + } /* if */ + ptr=ptr->next; + } /* while */ + } /* if */ + startfunc(sym->name); /* creates stack frame */ + insert_dbgline(funcline); + setline(FALSE); + if (sc_alignnext) { + alignframe(sc_dataalign); + sc_alignnext=FALSE; + } /* if */ + declared=0; /* number of local cells */ + rettype=(sym->usage & uRETVALUE); /* set "return type" variable */ + curfunc=sym; + define_args(); /* add the symbolic info for the function arguments */ + #if !defined SC_LIGHT + if (matchtoken('{')) { + lexpush(); + } else { + /* Insert a separator so that comments following the statement will not + * be attached to this function; they should be attached to the next + * function. This is not a problem for functions having a compound block, + * because the closing brace is an explicit "end token" for the function. + * With single statement functions, the preprocessor may overread the + * source code before the parser determines an "end of statement". + */ + insert_docstring_separator(); + } /* if */ + #endif + statement(NULL,FALSE); + if ((rettype & uRETVALUE)!=0) + sym->usage|=uRETVALUE; + if (declared!=0) { + /* This happens only in a very special (and useless) case, where a function + * has only a single statement in its body (no compound block) and that + * statement declares a new variable + */ + modstk((int)declared*sizeof(cell)); /* remove all local variables */ + declared=0; + } /* if */ + if ((lastst!=tRETURN) && (lastst!=tGOTO)){ + ldconst(0,sPRI); + ffret(); + if ((sym->usage & uRETVALUE)!=0) { + char symname[2*sNAMEMAX+16]; /* allow space for user defined operators */ + funcdisplayname(symname,sym->name); + error(209,symname); /* function should return a value */ + } /* if */ + } /* if */ + endfunc(); + sym->codeaddr=code_idx; + sc_attachdocumentation(sym); /* attach collected documenation to the function */ + if (litidx) { /* if there are literals defined */ + glb_declared+=litidx; + begdseg(); /* flip to DATA segment */ + dumplits(); /* dump literal strings */ + litidx=0; + } /* if */ + testsymbols(&loctab,0,TRUE,TRUE); /* test for unused arguments and labels */ + delete_symbols(&loctab,0,TRUE,TRUE); /* clear local variables queue */ + assert(loctab.next==NULL); + curfunc=NULL; + if (sc_status==statSKIP) { + sc_status=statWRITE; + code_idx=cidx; + glb_declared=glbdecl; + } /* if */ + return TRUE; +} + +static int argcompare(arginfo *a1,arginfo *a2) +{ + int result,level,i; + + result= strcmp(a1->name,a2->name)==0; /* name */ + if (result) + result= a1->ident==a2->ident; /* type/class */ + if (result) + result= a1->usage==a2->usage; /* "const" flag */ + if (result) + result= a1->numtags==a2->numtags; /* tags (number and names) */ + for (i=0; result && inumtags; i++) + result= a1->tags[i]==a2->tags[i]; + if (result) + result= a1->numdim==a2->numdim; /* array dimensions & index tags */ + for (level=0; result && levelnumdim; level++) + result= a1->dim[level]==a2->dim[level]; + for (level=0; result && levelnumdim; level++) + result= a1->idxtag[level]==a2->idxtag[level]; + if (result) + result= a1->hasdefault==a2->hasdefault; /* availability of default value */ + if (a1->hasdefault) { + if (a1->ident==iREFARRAY) { + if (result) + result= a1->defvalue.array.size==a2->defvalue.array.size; + if (result) + result= a1->defvalue.array.arraysize==a2->defvalue.array.arraysize; + /* ??? should also check contents of the default array (these troubles + * go away in a 2-pass compiler that forbids double declarations, but + * Pawn currently does not forbid them) */ + } else { + if (result) { + if ((a1->hasdefault & uSIZEOF)!=0 || (a1->hasdefault & uTAGOF)!=0) + result= a1->hasdefault==a2->hasdefault + && strcmp(a1->defvalue.size.symname,a2->defvalue.size.symname)==0 + && a1->defvalue.size.level==a2->defvalue.size.level; + else + result= a1->defvalue.val==a2->defvalue.val; + } /* if */ + } /* if */ + if (result) + result= a1->defvalue_tag==a2->defvalue_tag; + } /* if */ + return result; +} + +/* declargs() + * + * This routine adds an entry in the local symbol table for each argument + * found in the argument list. It returns the number of arguments. + */ +static int declargs(symbol *sym) +{ + #define MAXTAGS 16 + char *ptr; + int argcnt,oldargcnt,tok,tags[MAXTAGS],numtags; + cell val; + arginfo arg, *arglist; + char name[sNAMEMAX+1]; + int ident,fpublic,fconst; + int idx; + + /* if the function is already defined earlier, get the number of arguments + * of the existing definition + */ + oldargcnt=0; + if ((sym->usage & uPROTOTYPED)!=0) + while (sym->dim.arglist[oldargcnt].ident!=0) + oldargcnt++; + argcnt=0; /* zero aruments up to now */ + ident=iVARIABLE; + numtags=0; + fconst=FALSE; + fpublic= (sym->usage & uPUBLIC)!=0; + /* the '(' parantheses has already been parsed */ + if (!matchtoken(')')){ + do { /* there are arguments; process them */ + /* any legal name increases argument count (and stack offset) */ + tok=lex(&val,&ptr); + switch (tok) { + case 0: + /* nothing */ + break; + case '&': + if (ident!=iVARIABLE || numtags>0) + error(1,"-identifier-","&"); + ident=iREFERENCE; + break; + case tCONST: + if (ident!=iVARIABLE || numtags>0) + error(1,"-identifier-","const"); + fconst=TRUE; + break; + case tLABEL: + if (numtags>0) + error(1,"-identifier-","-tagname-"); + tags[0]=pc_addtag(ptr); + numtags=1; + break; + case '{': + if (numtags>0) + error(1,"-identifier-","-tagname-"); + numtags=0; + while (numtags=sMAXARGS) + error(45); /* too many function arguments */ + strcpy(name,ptr); /* save symbol name */ + if (name[0]==PUBLIC_CHAR) + error(56,name); /* function arguments cannot be public */ + if (numtags==0) + tags[numtags++]=0; /* default tag */ + /* Stack layout: + * base + 0*sizeof(cell) == previous "base" + * base + 1*sizeof(cell) == function return address + * base + 2*sizeof(cell) == number of arguments + * base + 3*sizeof(cell) == first argument of the function + * So the offset of each argument is "(argcnt+3) * sizeof(cell)". + */ + doarg(name,ident,(argcnt+3)*sizeof(cell),tags,numtags,fpublic,fconst,&arg); + if (fpublic && arg.hasdefault) + error(59,name); /* arguments of a public function may not have a default value */ + if ((sym->usage & uPROTOTYPED)==0) { + /* redimension the argument list, add the entry */ + sym->dim.arglist=(arginfo*)realloc(sym->dim.arglist,(argcnt+2)*sizeof(arginfo)); + if (sym->dim.arglist==0) + error(103); /* insufficient memory */ + memset(&sym->dim.arglist[argcnt+1],0,sizeof(arginfo)); /* keep the list terminated */ + sym->dim.arglist[argcnt]=arg; + } else { + /* check the argument with the earlier definition */ + if (argcnt>oldargcnt || !argcompare(&sym->dim.arglist[argcnt],&arg)) + error(25); /* function definition does not match prototype */ + /* may need to free default array argument and the tag list */ + if (arg.ident==iREFARRAY && arg.hasdefault) + free(arg.defvalue.array.data); + else if (arg.ident==iVARIABLE + && ((arg.hasdefault & uSIZEOF)!=0 || (arg.hasdefault & uTAGOF)!=0)) + free(arg.defvalue.size.symname); + free(arg.tags); + } /* if */ + argcnt++; + ident=iVARIABLE; + numtags=0; + fconst=FALSE; + break; + case tELLIPS: + if (ident!=iVARIABLE) + error(10); /* illegal function or declaration */ + if (numtags==0) + tags[numtags++]=0; /* default tag */ + if ((sym->usage & uPROTOTYPED)==0) { + /* redimension the argument list, add the entry iVARARGS */ + sym->dim.arglist=(arginfo*)realloc(sym->dim.arglist,(argcnt+2)*sizeof(arginfo)); + if (sym->dim.arglist==0) + error(103); /* insufficient memory */ + memset(&sym->dim.arglist[argcnt+1],0,sizeof(arginfo)); /* keep the list terminated */ + sym->dim.arglist[argcnt].ident=iVARARGS; + sym->dim.arglist[argcnt].hasdefault=FALSE; + sym->dim.arglist[argcnt].defvalue.val=0; + sym->dim.arglist[argcnt].defvalue_tag=0; + sym->dim.arglist[argcnt].numtags=numtags; + sym->dim.arglist[argcnt].tags=(int*)malloc(numtags*sizeof tags[0]); + if (sym->dim.arglist[argcnt].tags==NULL) + error(103); /* insufficient memory */ + memcpy(sym->dim.arglist[argcnt].tags,tags,numtags*sizeof tags[0]); + } else { + if (argcnt>oldargcnt || sym->dim.arglist[argcnt].ident!=iVARARGS) + error(25); /* function definition does not match prototype */ + } /* if */ + argcnt++; + break; + default: + error(10); /* illegal function or declaration */ + } /* switch */ + } while (tok=='&' || tok==tLABEL || tok==tCONST + || tok!=tELLIPS && matchtoken(',')); /* more? */ + /* if the next token is not ",", it should be ")" */ + needtoken(')'); + } /* if */ + /* resolve any "sizeof" arguments (now that all arguments are known) */ + assert(sym->dim.arglist!=NULL); + arglist=sym->dim.arglist; + for (idx=0; idx=argcnt) { + error(17,ptr); /* undefined symbol */ + } else { + assert(arglist[idx].defvalue.size.symname!=NULL); + /* check the level against the number of dimensions */ + if (arglist[idx].defvalue.size.level>0 + && arglist[idx].defvalue.size.level>=arglist[altidx].numdim) + error(28,arglist[idx].name); /* invalid subscript */ + /* check the type of the argument whose size to take; for a iVARIABLE + * or a iREFERENCE, this is always 1 (so the code is redundant) + */ + assert(arglist[altidx].ident!=iVARARGS); + if (arglist[altidx].ident!=iREFARRAY && (arglist[idx].hasdefault & uSIZEOF)!=0) { + if ((arglist[idx].hasdefault & uTAGOF)!=0) { + error(81,arglist[idx].name); /* cannot take "tagof" an indexed array */ + } else { + assert(arglist[altidx].ident==iVARIABLE || arglist[altidx].ident==iREFERENCE); + error(223,ptr); /* redundant sizeof */ + } /* if */ + } /* if */ + } /* if */ + } /* if */ + } /* for */ + + sym->usage|=uPROTOTYPED; + errorset(sRESET); /* reset error flag (clear the "panic mode")*/ + return argcnt; +} + +/* doarg - declare one argument type + * + * this routine is called from "declargs()" and adds an entry in the local + * symbol table for one argument. + * + * "fpublic" indicates whether the function for this argument list is public. + * The arguments themselves are never public. + */ +static void doarg(char *name,int ident,int offset,int tags[],int numtags, + int fpublic,int fconst,arginfo *arg) +{ + symbol *argsym; + constvalue *enumroot; + cell size; + + strcpy(arg->name,name); + arg->hasdefault=FALSE; /* preset (most common case) */ + arg->defvalue.val=0; /* clear */ + arg->defvalue_tag=0; + arg->numdim=0; + if (matchtoken('[')) { + if (ident==iREFERENCE) + error(67,name); /* illegal declaration ("&name[]" is unsupported) */ + do { + if (arg->numdim == sDIMEN_MAX) { + error(53); /* exceeding maximum number of dimensions */ + return; + } /* if */ + size=needsub(&arg->idxtag[arg->numdim],&enumroot);/* may be zero here, it is a pointer anyway */ + #if INT_MAX < LONG_MAX + if (size > INT_MAX) + error(105); /* overflow, exceeding capacity */ + #endif + arg->dim[arg->numdim]=(int)size; + arg->numdim+=1; + } while (matchtoken('[')); + ident=iREFARRAY; /* "reference to array" (is a pointer) */ + if (matchtoken('=')) { + lexpush(); /* initials() needs the "=" token again */ + assert(litidx==0); /* at the start of a function, this is reset */ + assert(numtags>0); + initials(ident,tags[0],&size,arg->dim,arg->numdim,enumroot); + assert(size>=litidx); + /* allocate memory to hold the initial values */ + arg->defvalue.array.data=(cell *)malloc(litidx*sizeof(cell)); + if (arg->defvalue.array.data!=NULL) { + int i; + memcpy(arg->defvalue.array.data,litq,litidx*sizeof(cell)); + arg->hasdefault=TRUE; /* argument has default value */ + arg->defvalue.array.size=litidx; + arg->defvalue.array.addr=-1; + /* calulate size to reserve on the heap */ + arg->defvalue.array.arraysize=1; + for (i=0; inumdim; i++) + arg->defvalue.array.arraysize*=arg->dim[i]; + if (arg->defvalue.array.arraysize < arg->defvalue.array.size) + arg->defvalue.array.arraysize = arg->defvalue.array.size; + } /* if */ + litidx=0; /* reset */ + } /* if */ + } else { + if (matchtoken('=')) { + unsigned char size_tag_token; + assert(ident==iVARIABLE || ident==iREFERENCE); + arg->hasdefault=TRUE; /* argument has a default value */ + size_tag_token=(unsigned char)(matchtoken(tSIZEOF) ? uSIZEOF : 0); + if (size_tag_token==0) + size_tag_token=(unsigned char)(matchtoken(tTAGOF) ? uTAGOF : 0); + if (size_tag_token!=0) { + int paranthese; + if (ident==iREFERENCE) + error(66,name); /* argument may not be a reference */ + paranthese=0; + while (matchtoken('(')) + paranthese++; + if (needtoken(tSYMBOL)) { + /* save the name of the argument whose size id to take */ + char *name; + cell val; + tokeninfo(&val,&name); + if ((arg->defvalue.size.symname=duplicatestring(name)) == NULL) + error(103); /* insufficient memory */ + arg->defvalue.size.level=0; + if (size_tag_token==uSIZEOF) { + while (matchtoken('[')) { + arg->defvalue.size.level+=(short)1; + needtoken(']'); + } /* while */ + } /* if */ + if (ident==iVARIABLE) /* make sure we set this only if not a reference */ + arg->hasdefault |= size_tag_token; /* uSIZEOF or uTAGOF */ + } /* if */ + while (paranthese--) + needtoken(')'); + } else { + constexpr(&arg->defvalue.val,&arg->defvalue_tag,NULL); + assert(numtags>0); + if (!matchtag(tags[0],arg->defvalue_tag,TRUE)) + error(213); /* tagname mismatch */ + } /* if */ + } /* if */ + } /* if */ + arg->ident=(char)ident; + arg->usage=(char)(fconst ? uCONST : 0); + arg->numtags=numtags; + arg->tags=(int*)malloc(numtags*sizeof tags[0]); + if (arg->tags==NULL) + error(103); /* insufficient memory */ + memcpy(arg->tags,tags,numtags*sizeof tags[0]); + argsym=findloc(name); + if (argsym!=NULL) { + error(21,name); /* symbol already defined */ + } else { + if ((argsym=findglb(name))!=NULL && argsym->ident!=iFUNCTN) + error(219,name); /* variable shadows another symbol */ + /* add details of type and address */ + assert(numtags>0); + argsym=addvariable(name,offset,ident,sLOCAL,tags[0], + arg->dim,arg->numdim,arg->idxtag); + argsym->compound=0; + if (ident==iREFERENCE) + argsym->usage|=uREAD; /* because references are passed back */ + if (fpublic) + argsym->usage|=uREAD; /* arguments of public functions are always "used" */ + if (fconst) + argsym->usage|=uCONST; + } /* if */ +} + +static int count_referrers(symbol *entry) +{ + int i,count; + + count=0; + for (i=0; inumrefers; i++) + if (entry->refer[i]!=NULL) + count++; + return count; +} + +#if !defined SC_LIGHT +static int find_xmltag(char *source,char *xmltag,char *xmlparam,char *xmlvalue, + char **outer_start,int *outer_length, + char **inner_start,int *inner_length) +{ + char *ptr,*inner_end; + int xmltag_len,xmlparam_len,xmlvalue_len; + int match; + + assert(source!=NULL); + assert(xmltag!=NULL); + assert(outer_start!=NULL); + assert(outer_length!=NULL); + assert(inner_start!=NULL); + assert(inner_length!=NULL); + + /* both NULL or both non-NULL */ + assert(xmlvalue!=NULL && xmlparam!=NULL || xmlvalue==NULL && xmlparam==NULL); + + xmltag_len=strlen(xmltag); + xmlparam_len= (xmlparam!=NULL) ? strlen(xmlparam) : 0; + xmlvalue_len= (xmlvalue!=NULL) ? strlen(xmlvalue) : 0; + ptr=source; + /* find an opening '<' */ + while ((ptr=strchr(ptr,'<'))!=NULL) { + *outer_start=ptr; /* be optimistic... */ + match=FALSE; /* ...and pessimistic at the same time */ + ptr++; /* skip '<' */ + while (*ptr!='\0' && *ptr<=' ') + ptr++; /* skip white space */ + if (strncmp(ptr,xmltag,xmltag_len)==0 && (*(ptr+xmltag_len)<=' ' || *(ptr+xmltag_len)=='>')) { + /* xml tag found, optionally check the parameter */ + ptr+=xmltag_len; + while (*ptr!='\0' && *ptr<=' ') + ptr++; /* skip white space */ + if (xmlparam!=NULL) { + if (strncmp(ptr,xmlparam,xmlparam_len)==0 && (*(ptr+xmlparam_len)<=' ' || *(ptr+xmlparam_len)=='=')) { + ptr+=xmlparam_len; + while (*ptr!='\0' && *ptr<=' ') + ptr++; /* skip white space */ + if (*ptr=='=') { + ptr++; /* skip '=' */ + while (*ptr!='\0' && *ptr<=' ') + ptr++; /* skip white space */ + if (*ptr=='"' || *ptr=='\'') + ptr++; /* skip " or ' */ + assert(xmlvalue!=NULL); + if (strncmp(ptr,xmlvalue,xmlvalue_len)==0 + && (*(ptr+xmlvalue_len)<=' ' + || *(ptr+xmlvalue_len)=='>' + || *(ptr+xmlvalue_len)=='"' + || *(ptr+xmlvalue_len)=='\'')) + match=TRUE; /* found it */ + } /* if */ + } /* if */ + } else { + match=TRUE; /* don't check the parameter */ + } /* if */ + } /* if */ + if (match) { + /* now find the end of the opening tag */ + while (*ptr!='\0' && *ptr!='>') + ptr++; + if (*ptr=='>') + ptr++; + while (*ptr!='\0' && *ptr<=' ') + ptr++; /* skip white space */ + *inner_start=ptr; + /* find the start of the closing tag (assume no nesting) */ + while ((ptr=strchr(ptr,'<'))!=NULL) { + inner_end=ptr; + ptr++; /* skip '<' */ + while (*ptr!='\0' && *ptr<=' ') + ptr++; /* skip white space */ + if (*ptr=='/') { + ptr++; /* skip / */ + while (*ptr!='\0' && *ptr<=' ') + ptr++; /* skip white space */ + if (strncmp(ptr,xmltag,xmltag_len)==0 && (*(ptr+xmltag_len)<=' ' || *(ptr+xmltag_len)=='>')) { + /* find the end of the closing tag */ + while (*ptr!='\0' && *ptr!='>') + ptr++; + if (*ptr=='>') + ptr++; + /* set the lengths of the inner and outer segment */ + assert(*inner_start!=NULL); + *inner_length=(int)(inner_end-*inner_start); + assert(*outer_start!=NULL); + *outer_length=(int)(ptr-*outer_start); + break; /* break out of the loop */ + } /* if */ + } /* if */ + } /* while */ + return TRUE; + } /* if */ + } /* while */ + return FALSE; /* not found */ +} + +static char *xmlencode(char *dest,char *source) +{ + char temp[2*sNAMEMAX+20],*ptr; + + /* replace < by < and such; normally, such a symbol occurs at most once in + * a symbol name (e.g. "operator<") + */ + ptr=temp; + while (*source!='\0') { + switch (*source) { + case '<': + strcpy(ptr,"<"); + ptr+=4; + break; + case '>': + strcpy(ptr,">"); + ptr+=4; + break; + case '&': + strcpy(ptr,"&"); + ptr+=5; + break; + default: + *ptr++=*source; + } /* switch */ + source++; + } /* while */ + *ptr='\0'; + strcpy(dest,temp); + return dest; +} + +static void make_report(symbol *root,FILE *log,char *sourcefile) +{ + char symname[2*sNAMEMAX+20]; + int i,arg; + symbol *sym,*ref; + constvalue *tagsym; + constvalue *enumroot; + char *ptr; + + /* the XML header */ + fprintf(log,"\n"); + fprintf(log,"\n"); + fprintf(log,"\n",sourcefile); + ptr=strrchr(sourcefile,DIRSEP_CHAR); + if (ptr!=NULL) + ptr++; + else + ptr=sourcefile; + fprintf(log,"\t\n\t\t%s\n\t\n",ptr); + + /* attach the global documentation, if any */ + if (sc_documentation!=NULL) { + fprintf(log,"\n\t\n"); + fprintf(log,"\t\n\t\t"); + fputs(sc_documentation,log); + fprintf(log,"\n\t\n\n"); + } /* if */ + + /* use multiple passes to print constants variables and functions in + * separate sections + */ + fprintf(log,"\t\n"); + + fprintf(log,"\n\t\t\n"); + for (sym=root->next; sym!=NULL; sym=sym->next) { + if (sym->parent!=NULL) + continue; /* hierarchical data type */ + assert(sym->ident==iCONSTEXPR || sym->ident==iVARIABLE + || sym->ident==iARRAY || sym->ident==iFUNCTN); + if (sym->ident!=iCONSTEXPR || (sym->usage & uENUMROOT)==0) + continue; + if ((sym->usage & uREAD)==0) + continue; + fprintf(log,"\t\t\n",funcdisplayname(symname,sym->name),sym->addr); + if (sym->tag!=0) { + tagsym=find_tag_byval(sym->tag); + assert(tagsym!=NULL); + fprintf(log,"\t\t\t\n",tagsym->name); + } /* if */ + /* browse through all fields */ + if ((enumroot=sym->dim.enumlist)!=NULL) { + enumroot=enumroot->next; /* skip root */ + while (enumroot!=NULL) { + fprintf(log,"\t\t\t\n",funcdisplayname(symname,enumroot->name),enumroot->value); + /* find the constant with this name and get the tag */ + ref=findglb(enumroot->name); + if (ref!=NULL) { + if (ref->x.idxtag!=0) { + tagsym=find_tag_byval(ref->x.idxtag); + assert(tagsym!=NULL); + fprintf(log,"\t\t\t\t\n",tagsym->name); + } /* if */ + if (ref->dim.array.length!=1) + fprintf(log,"\t\t\t\t\n",(long)ref->dim.array.length); + } /* if */ + fprintf(log,"\t\t\t\n"); + enumroot=enumroot->next; + } /* while */ + } /* if */ + assert(sym->refer!=NULL); + for (i=0; inumrefers; i++) { + if ((ref=sym->refer[i])!=NULL) + fprintf(log,"\t\t\t\n",xmlencode(symname,funcdisplayname(symname,ref->name))); + } /* for */ + if (sym->documentation!=NULL) + fprintf(log,"\t\t\t%s\n",sym->documentation); + fprintf(log,"\t\t\n"); + } /* for */ + + fprintf(log,"\n\t\t\n"); + for (sym=root->next; sym!=NULL; sym=sym->next) { + if (sym->parent!=NULL) + continue; /* hierarchical data type */ + assert(sym->ident==iCONSTEXPR || sym->ident==iVARIABLE + || sym->ident==iARRAY || sym->ident==iFUNCTN); + if (sym->ident!=iCONSTEXPR) + continue; + if ((sym->usage & uREAD)==0 || (sym->usage & (uENUMFIELD | uENUMROOT))!=0) + continue; + fprintf(log,"\t\t\n",funcdisplayname(symname,sym->name),sym->addr); + if (sym->tag!=0) { + tagsym=find_tag_byval(sym->tag); + assert(tagsym!=NULL); + fprintf(log,"\t\t\t\n",tagsym->name); + } /* if */ + assert(sym->refer!=NULL); + for (i=0; inumrefers; i++) { + if ((ref=sym->refer[i])!=NULL) + fprintf(log,"\t\t\t\n",xmlencode(symname,funcdisplayname(symname,ref->name))); + } /* for */ + if (sym->documentation!=NULL) + fprintf(log,"\t\t\t%s\n",sym->documentation); + fprintf(log,"\t\t\n"); + } /* for */ + + fprintf(log,"\n\t\t\n"); + for (sym=root->next; sym!=NULL; sym=sym->next) { + if (sym->parent!=NULL) + continue; /* hierarchical data type */ + if (sym->ident!=iVARIABLE && sym->ident!=iARRAY) + continue; + fprintf(log,"\t\t\n",funcdisplayname(symname,sym->name)); + if (sym->tag!=0) { + tagsym=find_tag_byval(sym->tag); + assert(tagsym!=NULL); + fprintf(log,"\t\t\t\n",tagsym->name); + } /* if */ + assert(sym->refer!=NULL); + if ((sym->usage & uPUBLIC)!=0) + fprintf(log,"\t\t\t\n"); + for (i=0; inumrefers; i++) { + if ((ref=sym->refer[i])!=NULL) + fprintf(log,"\t\t\t\n",xmlencode(symname,funcdisplayname(symname,ref->name))); + } /* for */ + if (sym->documentation!=NULL) + fprintf(log,"\t\t\t%s\n",sym->documentation); + fprintf(log,"\t\t\n"); + } /* for */ + + fprintf(log,"\n\t\t\n"); + for (sym=root->next; sym!=NULL; sym=sym->next) { + if (sym->parent!=NULL) + continue; /* hierarchical data type */ + if (sym->ident!=iFUNCTN) + continue; + if ((sym->usage & (uREAD | uNATIVE))==uNATIVE) + continue; /* unused native function */ + funcdisplayname(symname,sym->name); + xmlencode(symname,symname); + fprintf(log,"\t\tdim.arglist!=NULL); + for (arg=0; sym->dim.arglist[arg].ident!=0; arg++) { + int dim; + if (arg>0) + fprintf(log,", "); + switch (sym->dim.arglist[arg].ident) { + case iVARIABLE: + fprintf(log,"%s",sym->dim.arglist[arg].name); + break; + case iREFERENCE: + fprintf(log,"&%s",sym->dim.arglist[arg].name); + break; + case iREFARRAY: + fprintf(log,"%s",sym->dim.arglist[arg].name); + for (dim=0; dimdim.arglist[arg].numdim;dim++) + fprintf(log,"[]"); + break; + case iVARARGS: + fprintf(log,"..."); + break; + } /* switch */ + } /* for */ + /* ??? should also print an "array return" size */ + fprintf(log,")\">\n"); + if (sym->tag!=0) { + tagsym=find_tag_byval(sym->tag); + assert(tagsym!=NULL); + fprintf(log,"\t\t\t\n",tagsym->name); + } /* if */ + /* check whether this function is called from the outside */ + if ((sym->usage & uNATIVE)!=0) + fprintf(log,"\t\t\t\n"); + if ((sym->usage & uPUBLIC)!=0) + fprintf(log,"\t\t\t\n"); + if (strcmp(sym->name,uMAINFUNC)==0 || strcmp(sym->name,uENTRYFUNC)==0) + fprintf(log,"\t\t\t\n"); + if ((sym->usage & uNATIVE)==0) + fprintf(log,"\t\t\t\n",(long)sym->x.stacksize); + if (sym->states!=NULL) { + constvalue *stlist=sym->states->next; + assert(stlist!=NULL); /* there should be at least one state item */ + while (stlist!=NULL && stlist->index==-1) + stlist=stlist->next; + assert(stlist!=NULL); /* state id should be found */ + i=state_getfsa(stlist->index); + assert(i>=0); /* automaton 0 exists */ + stlist=automaton_findid(i); + assert(stlist!=NULL); /* automaton should be found */ + fprintf(log,"\t\t\t\n", strlen(stlist->name)>0 ? stlist->name : "(anonymous)"); + //??? dump state decision table + } /* if */ + assert(sym->refer!=NULL); + for (i=0; inumrefers; i++) + if ((ref=sym->refer[i])!=NULL) + fprintf(log,"\t\t\t\n",xmlencode(symname,funcdisplayname(symname,ref->name))); + /* print all symbols that are required for this function to compile */ + for (ref=root->next; ref!=NULL; ref=ref->next) { + if (ref==sym) + continue; + for (i=0; inumrefers; i++) + if (ref->refer[i]==sym) + fprintf(log,"\t\t\t\n",xmlencode(symname,funcdisplayname(symname,ref->name))); + } /* for */ + /* print parameter list, with tag & const information, plus descriptions */ + assert(sym->dim.arglist!=NULL); + for (arg=0; sym->dim.arglist[arg].ident!=0; arg++) { + int dim,paraminfo; + char *outer_start,*inner_start; + int outer_length,inner_length; + if (sym->dim.arglist[arg].ident==iVARARGS) + fprintf(log,"\t\t\t\n"); + else + fprintf(log,"\t\t\t\n",sym->dim.arglist[arg].name); + /* print the tag name(s) for each parameter */ + assert(sym->dim.arglist[arg].numtags>0); + assert(sym->dim.arglist[arg].tags!=NULL); + paraminfo=(sym->dim.arglist[arg].numtags>1 || sym->dim.arglist[arg].tags[0]!=0) + || sym->dim.arglist[arg].ident==iREFERENCE + || sym->dim.arglist[arg].ident==iREFARRAY; + if (paraminfo) + fprintf(log,"\t\t\t\t"); + if (sym->dim.arglist[arg].numtags>1 || sym->dim.arglist[arg].tags[0]!=0) { + assert(paraminfo); + if (sym->dim.arglist[arg].numtags>1) + fprintf(log," {"); + for (i=0; idim.arglist[arg].numtags; i++) { + if (i>0) + fprintf(log,","); + tagsym=find_tag_byval(sym->dim.arglist[arg].tags[i]); + assert(tagsym!=NULL); + fprintf(log,"%s",tagsym->name); + } /* for */ + if (sym->dim.arglist[arg].numtags>1) + fprintf(log,"}"); + } /* if */ + switch (sym->dim.arglist[arg].ident) { + case iREFERENCE: + fprintf(log," &"); + break; + case iREFARRAY: + fprintf(log," "); + for (dim=0; dimdim.arglist[arg].numdim; dim++) { + if (sym->dim.arglist[arg].dim[dim]==0) { + fprintf(log,"[]"); + } else { + //??? find index tag + fprintf(log,"[%d]",sym->dim.arglist[arg].dim[dim]); + } /* if */ + } /* for */ + break; + } /* switch */ + if (paraminfo) + fprintf(log," \n"); + /* print the user description of the parameter (parse through + * sym->documentation) + */ + if (sym->documentation!=NULL + && find_xmltag(sym->documentation, "param", "name", sym->dim.arglist[arg].name, + &outer_start, &outer_length, &inner_start, &inner_length)) + { + char *tail; + fprintf(log,"\t\t\t\t%.*s\n",inner_length,inner_start); + /* delete from documentation string */ + tail=outer_start+outer_length; + memmove(outer_start,tail,strlen(tail)+1); + } /* if */ + fprintf(log,"\t\t\t\n"); + } /* for */ + if (sym->documentation!=NULL) + fprintf(log,"\t\t\t%s\n",sym->documentation); + fprintf(log,"\t\t\n"); + } /* for */ + + fprintf(log,"\n\t\n"); + fprintf(log,"\n"); +} +#endif + +/* Every symbol has a referrer list, that contains the functions that use + * the symbol. Now, if function "apple" is accessed by functions "banana" and + * "citron", but neither function "banana" nor "citron" are used by anyone + * else, then, by inference, function "apple" is not used either. + */ +static void reduce_referrers(symbol *root) +{ + int i,restart; + symbol *sym,*ref; + + do { + restart=0; + for (sym=root->next; sym!=NULL; sym=sym->next) { + if (sym->parent!=NULL) + continue; /* hierarchical data type */ + if (sym->ident==iFUNCTN + && (sym->usage & uNATIVE)==0 + && (sym->usage & uPUBLIC)==0 && strcmp(sym->name,uMAINFUNC)!=0 && strcmp(sym->name,uENTRYFUNC)!=0 + && count_referrers(sym)==0) + { + sym->usage&=~(uREAD | uWRITTEN); /* erase usage bits if there is no referrer */ + /* find all symbols that are referred by this symbol */ + for (ref=root->next; ref!=NULL; ref=ref->next) { + if (ref->parent!=NULL) + continue; /* hierarchical data type */ + assert(ref->refer!=NULL); + for (i=0; inumrefers && ref->refer[i]!=sym; i++) + /* nothing */; + if (inumrefers) { + assert(ref->refer[i]==sym); + ref->refer[i]=NULL; + restart++; + } /* if */ + } /* for */ + } else if ((sym->ident==iVARIABLE || sym->ident==iARRAY) + && (sym->usage & uPUBLIC)==0 + && sym->parent==NULL + && count_referrers(sym)==0) + { + sym->usage&=~(uREAD | uWRITTEN); /* erase usage bits if there is no referrer */ + } /* if */ + } /* for */ + /* after removing a symbol, check whether more can be removed */ + } while (restart>0); +} + +#if !defined SC_LIGHT +static long max_stacksize_recurse(symbol *sym,long basesize,int *pubfuncparams) +{ + long size,maxsize; + int i; + + assert(sym!=NULL); + assert(sym->ident==iFUNCTN); + assert((sym->usage & uNATIVE)==0); + /* recursion detection */ + if (sym->compound==0) + return -1; /* this function was processed already -> recursion */ + sym->compound=0; + + maxsize=sym->x.stacksize; + for (i=0; inumrefers; i++) { + if (sym->refer[i]!=NULL) { + assert(sym->refer[i]->ident==iFUNCTN); + assert((sym->refer[i]->usage & uNATIVE)==0); /* a native function cannot refer to a user-function */ + size=max_stacksize_recurse(sym->refer[i],sym->x.stacksize,pubfuncparams); + if (size<0) + return size; /* recursion was detected, quit */ + if (maxsizeusage & uPUBLIC)!=0) { + /* Find out how many parameters a public function has, then see if this + * is bigger than some maximum + */ + arginfo *arg=sym->dim.arglist; + int count=0; + assert(arg!=0); + while (arg->ident!=0) { + count++; + arg++; + } /* while */ + assert(pubfuncparams!=0); + if (count>*pubfuncparams) + *pubfuncparams=count; + } /* if */ + + return maxsize+basesize; +} + +static long max_stacksize(symbol *root) +{ + /* Loop over all non-native functions. For each function, loop + * over all of its referrers, accumulating the stack requirements. + * Detect (indirect) recursion with a "mark-and-sweep" algorithm. + * I (mis-)use the "compound" field of the symbol structure for + * the marker, as this field is unused for functions. + * + * Note that the stack is shared with the heap. A host application + * may "eat" cells from the heap as well, through amx_Allot(). The + * stack requirements are thus only an estimate. + */ + long size,maxsize; + int maxparams; + symbol *sym; + + #if !defined NDEBUG + for (sym=root->next; sym!=NULL; sym=sym->next) + if (sym->ident==iFUNCTN) + assert(sym->compound==0); + #endif + + maxsize=0; + maxparams=0; + for (sym=root->next; sym!=NULL; sym=sym->next) { + symbol *tmpsym; + /* drop out if this is not a user-implemented function */ + if (sym->ident!=iFUNCTN || (sym->usage & uNATIVE)!=0) + continue; + /* set a "mark" on all functions */ + for (tmpsym=root->next; tmpsym!=NULL; tmpsym=tmpsym->next) + if (tmpsym->ident==iFUNCTN) + tmpsym->compound=1; + /* accumulate stack size for this symbol */ + size=max_stacksize_recurse(sym,0L,&maxparams); + if (size<0) + return size; /* recursion was detected */ + if (maxsizenext; sym!=NULL; sym=sym->next) + if (sym->ident==iFUNCTN) + sym->compound=0; + + maxsize++; /* +1 because a zero cell is always pushed on top + * of the stack to catch stack overwrites */ + return maxsize+(maxparams+1);/* +1 because # of parameters is always pushed on entry */ +} +#endif + +/* testsymbols - test for unused local or global variables + * + * "Public" functions are excluded from the check, since these + * may be exported to other object modules. + * Labels are excluded from the check if the argument 'testlabs' + * is 0. Thus, labels are not tested until the end of the function. + * Constants may also be excluded (convenient for global constants). + * + * When the nesting level drops below "level", the check stops. + * + * The function returns whether there is an "entry" point for the file. + * This flag will only be 1 when browsing the global symbol table. + */ +static int testsymbols(symbol *root,int level,int testlabs,int testconst) +{ + char symname[2*sNAMEMAX+16]; + int entry=FALSE; + + symbol *sym=root->next; + while (sym!=NULL && sym->compound>=level) { + switch (sym->ident) { + case iLABEL: + if (testlabs) { + if ((sym->usage & uDEFINE)==0) + error(19,sym->name); /* not a label: ... */ + else if ((sym->usage & uREAD)==0) + error(203,sym->name); /* symbol isn't used: ... */ + } /* if */ + break; + case iFUNCTN: + if ((sym->usage & (uDEFINE | uREAD | uNATIVE | uSTOCK))==uDEFINE) { + funcdisplayname(symname,sym->name); + if (strlen(symname)>0) + error(203,symname); /* symbol isn't used ... (and not native/stock) */ + } /* if */ + if ((sym->usage & uPUBLIC)!=0 || strcmp(sym->name,uMAINFUNC)==0) + entry=TRUE; /* there is an entry point */ + /* also mark the function to the debug information */ + if ((sym->usage & uREAD)!=0 && (sym->usage & uNATIVE)==0) + insert_dbgsymbol(sym); + break; + case iCONSTEXPR: + if (testconst && (sym->usage & uREAD)==0) + error(203,sym->name); /* symbol isn't used: ... */ + break; + default: + /* a variable */ + if (sym->parent!=NULL) + break; /* hierarchical data type */ + if ((sym->usage & (uWRITTEN | uREAD | uSTOCK))==0) + error(203,sym->name); /* symbol isn't used (and not stock) */ + else if ((sym->usage & (uREAD | uSTOCK | uPUBLIC))==0) + error(204,sym->name); /* value assigned to symbol is never used */ +#if 0 // ??? not sure whether it is a good idea to force people use "const" + else if ((sym->usage & (uWRITTEN | uPUBLIC | uCONST))==0 && sym->ident==iREFARRAY) + error(214,sym->name); /* make array argument "const" */ +#endif + /* also mark the variable (local or global) to the debug information */ + if ((sym->usage & (uWRITTEN | uREAD))!=0 && (sym->usage & uNATIVE)==0) + insert_dbgsymbol(sym); + } /* if */ + sym=sym->next; + } /* while */ + + return entry; +} + +static cell calc_array_datasize(symbol *sym, cell *offset) +{ + cell length; + + assert(sym!=NULL); + assert(sym->ident==iARRAY || sym->ident==iREFARRAY); + length=sym->dim.array.length; + if (sym->dim.array.level > 0) { + cell sublength=calc_array_datasize(finddepend(sym),offset); + if (offset!=NULL) + *offset=length*(*offset+sizeof(cell)); + if (sublength>0) + length*=length*sublength; + else + length=0; + } else { + if (offset!=NULL) + *offset=0; + } /* if */ + return length; +} + +static void destructsymbols(symbol *root,int level) +{ + cell offset=0; + int savepri=FALSE; + symbol *sym=root->next; + while (sym!=NULL && sym->compound>=level) { + if (sym->ident==iVARIABLE || sym->ident==iARRAY) { + char symbolname[16]; + symbol *opsym; + cell elements; + /* check that the '~' operator is defined for this tag */ + operator_symname(symbolname,"~",sym->tag,0,1,0); + if ((opsym=findglb(symbolname))!=NULL) { + /* save PRI, in case of a return statment */ + if (!savepri) { + pushreg(sPRI); /* right-hand operand is in PRI */ + savepri=TRUE; + } /* if */ + /* if the variable is an array, get the number of elements */ + if (sym->ident==iARRAY) { + elements=calc_array_datasize(sym,&offset); + /* "elements" can be zero when the variable is declared like + * new mytag: myvar[2][] = { {1, 2}, {3, 4} } + * one should declare all dimensions! + */ + if (elements==0) + error(46,sym->name); /* array size is unknown */ + } else { + elements=1; + offset=0; + } /* if */ + pushval(elements); + /* call the '~' operator */ + address(sym,sPRI); + addconst(offset); /* add offset to array data to the address */ + pushreg(sPRI); + pushval(2*sizeof(cell));/* 2 parameters */ + assert(opsym->ident==iFUNCTN); + ffcall(opsym,NULL,1); + if (sc_status!=statSKIP) + markusage(opsym,uREAD); /* do not mark as "used" when this call itself is skipped */ + if ((opsym->usage & uNATIVE)!=0 && opsym->x.lib!=NULL) + opsym->x.lib->value += 1; /* increment "usage count" of the library */ + } /* if */ + } /* if */ + sym=sym->next; + } /* while */ + /* restore PRI, if it was saved */ + if (savepri) + popreg(sPRI); +} + +static constvalue *insert_constval(constvalue *prev,constvalue *next,const char *name,cell val, + short index) +{ + constvalue *cur; + + if ((cur=(constvalue*)malloc(sizeof(constvalue)))==NULL) + error(103); /* insufficient memory (fatal error) */ + memset(cur,0,sizeof(constvalue)); + if (name!=NULL) { + assert(strlen(name)name,name); + } /* if */ + cur->value=val; + cur->index=index; + cur->next=next; + prev->next=cur; + return cur; +} + +SC_FUNC constvalue *append_constval(constvalue *table,const char *name,cell val,short index) +{ + constvalue *cur,*prev; + + /* find the end of the constant table */ + for (prev=table, cur=table->next; cur!=NULL; prev=cur, cur=cur->next) + /* nothing */; + return insert_constval(prev,NULL,name,val,index); +} + +SC_FUNC constvalue *find_constval(constvalue *table,char *name,short index) +{ + constvalue *ptr = table->next; + + while (ptr!=NULL) { + if (strcmp(name,ptr->name)==0 && ptr->index==index) + return ptr; + ptr=ptr->next; + } /* while */ + return NULL; +} + +static constvalue *find_constval_byval(constvalue *table,cell val) +{ + constvalue *ptr = table->next; + + while (ptr!=NULL) { + if (ptr->value==val) + return ptr; + ptr=ptr->next; + } /* while */ + return NULL; +} + +#if 0 /* never used */ +static int delete_constval(constvalue *table,char *name) +{ + constvalue *prev = table; + constvalue *cur = prev->next; + + while (cur!=NULL) { + if (strcmp(name,cur->name)==0) { + prev->next=cur->next; + free(cur); + return TRUE; + } /* if */ + prev=cur; + cur=cur->next; + } /* while */ + return FALSE; +} +#endif + +SC_FUNC void delete_consttable(constvalue *table) +{ + constvalue *cur=table->next, *next; + + while (cur!=NULL) { + next=cur->next; + free(cur); + cur=next; + } /* while */ + memset(table,0,sizeof(constvalue)); +} + +/* add_constant + * + * Adds a symbol to the #define symbol table. Returns NULL on failure. + */ +SC_FUNC symbol *add_constant(char *name,cell val,int vclass,int tag) +{ + symbol *sym; + + /* Test whether a global or local symbol with the same name exists. Since + * constants are stored in the symbols table, this also finds previously + * defind constants. */ + sym=findglb(name); + if (!sym) + sym=findloc(name); + if (sym) { + /* silently ignore redefinitions of constants with the same value */ + if (sym->ident==iCONSTEXPR) { + if (sym->addr!=val) + error(201,name); /* redefinition of constant (different value) */ + } else { + error(21,name); /* symbol already defined */ + return NULL; + } /* if */ + return sym; + } /* if */ + + /* constant doesn't exist yet, an entry must be created */ + sym=addsym(name,val,iCONSTEXPR,vclass,tag,uDEFINE); + assert(sym!=NULL); /* fatal error 103 must be given on error */ + if (sc_status == statIDLE) + sym->usage |= uPREDEF; + return sym; +} + +/* statement - The Statement Parser + * + * This routine is called whenever the parser needs to know what statement + * it encounters (i.e. whenever program syntax requires a statement). + */ +static void statement(int *lastindent,int allow_decl) +{ + int tok; + cell val; + char *st; + + if (!freading) { + error(36); /* empty statement */ + return; + } /* if */ + errorset(sRESET); + + tok=lex(&val,&st); + if (tok!='{') { + insert_dbgline(fline); + setline(TRUE); + } /* if */ + /* lex() has set stmtindent */ + if (lastindent!=NULL && tok!=tLABEL) { + if (*lastindent>=0 && *lastindent!=stmtindent && !indent_nowarn && sc_tabsize>0) + error(217); /* loose indentation */ + *lastindent=stmtindent; + indent_nowarn=FALSE; /* if warning was blocked, re-enable it */ + } /* if */ + switch (tok) { + case 0: + /* nothing */ + break; + case tNEW: + if (allow_decl) { + declloc(FALSE); + lastst=tNEW; + } else { + error(3); /* declaration only valid in a block */ + } /* if */ + break; + case tSTATIC: + if (allow_decl) { + declloc(TRUE); + lastst=tNEW; + } else { + error(3); /* declaration only valid in a block */ + } /* if */ + break; + case '{': + tok=fline; + if (!matchtoken('}')) /* {} is the empty statement */ + compound(tok==fline); + /* lastst (for "last statement") does not change */ + break; + case ';': + error(36); /* empty statement */ + break; + case tIF: + doif(); + lastst=tIF; + break; + case tWHILE: + dowhile(); + lastst=tWHILE; + break; + case tDO: + dodo(); + lastst=tDO; + break; + case tFOR: + dofor(); + lastst=tFOR; + break; + case tSWITCH: + doswitch(); + lastst=tSWITCH; + break; + case tCASE: + case tDEFAULT: + error(14); /* not in switch */ + break; + case tGOTO: + dogoto(); + lastst=tGOTO; + break; + case tLABEL: + dolabel(); + lastst=tLABEL; + break; + case tRETURN: + doreturn(); + lastst=tRETURN; + break; + case tBREAK: + dobreak(); + lastst=tBREAK; + break; + case tCONTINUE: + docont(); + lastst=tCONTINUE; + break; + case tEXIT: + doexit(); + lastst=tEXIT; + break; + case tASSERT: + doassert(); + lastst=tASSERT; + break; + case tSLEEP: + dosleep(); + lastst=tSLEEP; + break; + case tSTATE: + dostate(); + lastst=tSTATE; + break; + case tCONST: + decl_const(sLOCAL); + break; + case tENUM: + decl_enum(sLOCAL); + break; + default: /* non-empty expression */ + sc_allowproccall=optproccall; + lexpush(); /* analyze token later */ + doexpr(TRUE,TRUE,TRUE,TRUE,NULL,NULL,FALSE); + needtoken(tTERM); + lastst=tEXPR; + sc_allowproccall=FALSE; + } /* switch */ +} + +static void compound(int stmt_sameline) +{ + int indent=-1; + cell save_decl=declared; + int count_stmt=0; + + /* if there is more text on this line, we should adjust the statement indent */ + if (stmt_sameline) { + int i; + const unsigned char *p=lptr; + /* go back to the opening brace */ + while (*p!='{') { + assert(p>pline); + p--; + } /* while */ + assert(*p=='{'); /* it should be found */ + /* go forward, skipping white-space */ + p++; + while (*p<=' ' && *p!='\0') + p++; + assert(*p!='\0'); /* a token should be found */ + stmtindent=0; + for (i=0; i<(int)(p-pline); i++) + if (pline[i]=='\t' && sc_tabsize>0) + stmtindent += (int)(sc_tabsize - (stmtindent+sc_tabsize) % sc_tabsize); + else + stmtindent++; + } /* if */ + + nestlevel+=1; /* increase compound statement level */ + while (matchtoken('}')==0){ /* repeat until compound statement is closed */ + if (!freading){ + needtoken('}'); /* gives error: "expected token }" */ + break; + } else { + if (count_stmt>0 && (lastst==tRETURN || lastst==tBREAK || lastst==tCONTINUE)) + error(225); /* unreachable code */ + statement(&indent,TRUE); /* do a statement */ + count_stmt++; + } /* if */ + } /* while */ + if (lastst!=tRETURN) + destructsymbols(&loctab,nestlevel); + if (lastst!=tRETURN && lastst!=tGOTO) + modstk((int)(declared-save_decl)*sizeof(cell)); /* delete local variable space */ + testsymbols(&loctab,nestlevel,FALSE,TRUE); /* look for unused block locals */ + declared=save_decl; + delete_symbols(&loctab,nestlevel,FALSE,TRUE); /* erase local symbols, but + * retain block local labels + * (within the function) */ + nestlevel-=1; /* decrease compound statement level */ +} + +/* doexpr + * + * Global references: stgidx (referred to only) + */ +static int doexpr(int comma,int chkeffect,int allowarray,int mark_endexpr, + int *tag,symbol **symptr,int chkfuncresult) +{ + int index,ident; + int localstaging=FALSE; + cell val; + + if (!staging) { + stgset(TRUE); /* start stage-buffering */ + localstaging=TRUE; + assert(stgidx==0); + } /* if */ + index=stgidx; + errorset(sEXPRMARK); + do { + /* on second round through, mark the end of the previous expression */ + if (index!=stgidx) + markexpr(sEXPR,NULL,0); + sideeffect=FALSE; + ident=expression(&val,tag,symptr,chkfuncresult); + if (!allowarray && (ident==iARRAY || ident==iREFARRAY)) + error(33,"-unknown-"); /* array must be indexed */ + if (chkeffect && !sideeffect) + error(215); /* expression has no effect */ + sc_allowproccall=FALSE; /* cannot use "procedure call" syntax anymore */ + } while (comma && matchtoken(',')); /* more? */ + if (mark_endexpr) + markexpr(sEXPR,NULL,0); /* optionally, mark the end of the expression */ + errorset(sEXPRRELEASE); + if (localstaging) { + stgout(index); + stgset(FALSE); /* stop staging */ + } /* if */ + return ident; +} + +/* constexpr + */ +SC_FUNC int constexpr(cell *val,int *tag,symbol **symptr) +{ + int ident,index; + cell cidx; + + stgset(TRUE); /* start stage-buffering */ + stgget(&index,&cidx); /* mark position in code generator */ + errorset(sEXPRMARK); + ident=expression(val,tag,symptr,FALSE); + stgdel(index,cidx); /* scratch generated code */ + stgset(FALSE); /* stop stage-buffering */ + if (ident!=iCONSTEXPR) { + error(8); /* must be constant expression */ + if (val!=NULL) + *val=0; + if (tag!=NULL) + *tag=0; + if (symptr!=NULL) + *symptr=NULL; + } /* if */ + errorset(sEXPRRELEASE); + return (ident==iCONSTEXPR); +} + +/* test + * + * In the case a "simple assignment" operator ("=") is used within a test, + * the warning "possibly unintended assignment" is displayed. This routine + * sets the global variable "sc_intest" to true, it is restored upon termination. + * In the case the assignment was intended, use parantheses around the + * expression to avoid the warning; primary() sets "sc_intest" to 0. + * + * Global references: sc_intest (altered, but restored upon termination) + */ +static void test(int label,int parens,int invert) +{ + int index,tok; + cell cidx; + int ident,tag; + cell constval; + symbol *sym; + int localstaging=FALSE; + + if (!staging) { + stgset(TRUE); /* start staging */ + localstaging=TRUE; + #if !defined NDEBUG + stgget(&index,&cidx); /* should start at zero if started locally */ + assert(index==0); + #endif + } /* if */ + + PUSHSTK_I(sc_intest); + sc_intest=TRUE; + if (parens) + needtoken('('); + do { + stgget(&index,&cidx); /* mark position (of last expression) in + * code generator */ + ident=expression(&constval,&tag,&sym,TRUE); + tok=matchtoken(','); + if (tok) + markexpr(sEXPR,NULL,0); + } while (tok); /* do */ + if (parens) + needtoken(')'); + if (ident==iARRAY || ident==iREFARRAY) { + char *ptr=(sym->name!=NULL) ? sym->name : "-unknown-"; + error(33,ptr); /* array must be indexed */ + } /* if */ + if (ident==iCONSTEXPR) { /* constant expression */ + sc_intest=(short)POPSTK_I();/* restore stack */ + stgdel(index,cidx); + if (constval) { /* code always executed */ + error(206); /* redundant test: always non-zero */ + } else { + error(205); /* redundant code: never executed */ + jumplabel(label); + } /* if */ + if (localstaging) { + stgout(0); /* write "jumplabel" code */ + stgset(FALSE); /* stop staging */ + } /* if */ + return; + } /* if */ + if (tag!=0 && tag!=pc_addtag("bool")) + if (check_userop(lneg,tag,0,1,NULL,&tag)) + invert= !invert; /* user-defined ! operator inverted result */ + if (invert) + jmp_ne0(label); /* jump to label if true (different from 0) */ + else + jmp_eq0(label); /* jump to label if false (equal to 0) */ + markexpr(sEXPR,NULL,0); /* end expression (give optimizer a chance) */ + sc_intest=(short)POPSTK_I(); /* double typecast to avoid warning with Microsoft C */ + if (localstaging) { + stgout(0); /* output queue from the very beginning (see + * assert() when localstaging is set to TRUE) */ + stgset(FALSE); /* stop staging */ + } /* if */ +} + +static void doif(void) +{ + int flab1,flab2; + int ifindent; + + ifindent=stmtindent; /* save the indent of the "if" instruction */ + flab1=getlabel(); /* get label number for false branch */ + test(flab1,TRUE,FALSE); /* get expression, branch to flab1 if false */ + statement(NULL,FALSE); /* if true, do a statement */ + if (matchtoken(tELSE)==0){ /* if...else ? */ + setlabel(flab1); /* no, simple if..., print false label */ + } else { + /* to avoid the "dangling else" error, we want a warning if the "else" + * has a lower indent than the matching "if" */ + if (stmtindent0) + error(217); /* loose indentation */ + flab2=getlabel(); + if ((lastst!=tRETURN) && (lastst!=tGOTO)) + jumplabel(flab2); + setlabel(flab1); /* print false label */ + statement(NULL,FALSE); /* do "else" clause */ + setlabel(flab2); /* print true label */ + } /* endif */ +} + +static void dowhile(void) +{ + int wq[wqSIZE]; /* allocate local queue */ + + addwhile(wq); /* add entry to queue for "break" */ + setlabel(wq[wqLOOP]); /* loop label */ + /* The debugger uses the "line" opcode to be able to "break" out of + * a loop. To make sure that each loop has a line opcode, even for the + * tiniest loop, set it below the top of the loop + */ + setline(TRUE); + test(wq[wqEXIT],TRUE,FALSE); /* branch to wq[wqEXIT] if false */ + statement(NULL,FALSE); /* if so, do a statement */ + jumplabel(wq[wqLOOP]); /* and loop to "while" start */ + setlabel(wq[wqEXIT]); /* exit label */ + delwhile(); /* delete queue entry */ +} + +/* + * Note that "continue" will in this case not jump to the top of the loop, but + * to the end: just before the TRUE-or-FALSE testing code. + */ +static void dodo(void) +{ + int wq[wqSIZE],top; + + addwhile(wq); /* see "dowhile" for more info */ + top=getlabel(); /* make a label first */ + setlabel(top); /* loop label */ + statement(NULL,FALSE); + needtoken(tWHILE); + setlabel(wq[wqLOOP]); /* "continue" always jumps to WQLOOP. */ + setline(TRUE); + test(wq[wqEXIT],TRUE,FALSE); + jumplabel(top); + setlabel(wq[wqEXIT]); + delwhile(); + needtoken(tTERM); +} + +static void dofor(void) +{ + int wq[wqSIZE],skiplab; + cell save_decl; + int save_nestlevel,index; + int *ptr; + + save_decl=declared; + save_nestlevel=nestlevel; + + addwhile(wq); + skiplab=getlabel(); + needtoken('('); + if (matchtoken(';')==0) { + /* new variable declarations are allowed here */ + if (matchtoken(tNEW)) { + /* The variable in expr1 of the for loop is at a + * 'compound statement' level of it own. + */ + nestlevel++; + declloc(FALSE); /* declare local variable */ + } else { + doexpr(TRUE,TRUE,TRUE,TRUE,NULL,NULL,FALSE); /* expression 1 */ + needtoken(';'); + } /* if */ + } /* if */ + /* Adjust the "declared" field in the "while queue", in case that + * local variables were declared in the first expression of the + * "for" loop. These are deleted in separately, so a "break" or a "continue" + * must ignore these fields. + */ + ptr=readwhile(); + assert(ptr!=NULL); + ptr[wqBRK]=(int)declared; + ptr[wqCONT]=(int)declared; + jumplabel(skiplab); /* skip expression 3 1st time */ + setlabel(wq[wqLOOP]); /* "continue" goes to this label: expr3 */ + setline(TRUE); + /* Expressions 2 and 3 are reversed in the generated code: expression 3 + * precedes expression 2. When parsing, the code is buffered and marks for + * the start of each expression are insterted in the buffer. + */ + assert(!staging); + stgset(TRUE); /* start staging */ + assert(stgidx==0); + index=stgidx; + stgmark(sSTARTREORDER); + stgmark((char)(sEXPRSTART+0)); /* mark start of 2nd expression in stage */ + setlabel(skiplab); /* jump to this point after 1st expression */ + if (matchtoken(';')==0) { + test(wq[wqEXIT],FALSE,FALSE); /* expression 2 (jump to wq[wqEXIT] if false) */ + needtoken(';'); + } /* if */ + stgmark((char)(sEXPRSTART+1)); /* mark start of 3th expression in stage */ + if (matchtoken(')')==0) { + doexpr(TRUE,TRUE,TRUE,TRUE,NULL,NULL,FALSE); /* expression 3 */ + needtoken(')'); + } /* if */ + stgmark(sENDREORDER); /* mark end of reversed evaluation */ + stgout(index); + stgset(FALSE); /* stop staging */ + statement(NULL,FALSE); + jumplabel(wq[wqLOOP]); + setlabel(wq[wqEXIT]); + delwhile(); + + assert(nestlevel>=save_nestlevel); + if (nestlevel>save_nestlevel) { + /* Clean up the space and the symbol table for the local + * variable in "expr1". + */ + destructsymbols(&loctab,nestlevel); + modstk((int)(declared-save_decl)*sizeof(cell)); + testsymbols(&loctab,nestlevel,FALSE,TRUE); /* look for unused block locals */ + declared=save_decl; + delete_symbols(&loctab,nestlevel,FALSE,TRUE); + nestlevel=save_nestlevel; /* reset 'compound statement' nesting level */ + } /* if */ +} + +/* The switch statement is incompatible with its C sibling: + * 1. the cases are not drop through + * 2. only one instruction may appear below each case, use a compound + * instruction to execute multiple instructions + * 3. the "case" keyword accepts a comma separated list of values to + * match, it also accepts a range using the syntax "1 .. 4" + * + * SWITCH param + * PRI = expression result + * param = table offset (code segment) + * + */ +static void doswitch(void) +{ + int lbl_table,lbl_exit,lbl_case; + int tok,swdefault,casecount; + cell val; + char *str; + constvalue caselist = { NULL, "", 0, 0}; /* case list starts empty */ + constvalue *cse,*csp; + char labelname[sNAMEMAX+1]; + + needtoken('('); + doexpr(TRUE,FALSE,FALSE,FALSE,NULL,NULL,TRUE);/* evaluate switch expression */ + needtoken(')'); + /* generate the code for the switch statement, the label is the address + * of the case table (to be generated later). + */ + lbl_table=getlabel(); + lbl_case=0; /* just to avoid a compiler warning */ + ffswitch(lbl_table); + + needtoken('{'); + lbl_exit=getlabel(); /* get label number for jumping out of switch */ + swdefault=FALSE; + casecount=0; + do { + tok=lex(&val,&str); /* read in (new) token */ + switch (tok) { + case tCASE: + if (swdefault!=FALSE) + error(15); /* "default" case must be last in switch statement */ + lbl_case=getlabel(); + PUSHSTK_I(sc_allowtags); + sc_allowtags=FALSE; /* do not allow tagnames here */ + do { + casecount++; + + /* ??? enforce/document that, in a switch, a statement cannot start + * with a label. Then, you can search for: + * * the first semicolon (marks the end of a statement) + * * an opening brace (marks the start of a compound statement) + * and search for the right-most colon before that statement + * Now, by replacing the ':' by a special COLON token, you can + * parse all expressions until that special token. + */ + + constexpr(&val,NULL,NULL); + /* Search the insertion point (the table is kept in sorted order, so + * that advanced abstract machines can sift the case table with a + * binary search). Check for duplicate case values at the same time. + */ + for (csp=&caselist, cse=caselist.next; + cse!=NULL && cse->valuenext) + /* nothing */; + if (cse!=NULL && cse->value==val) + error(40,val); /* duplicate "case" label */ + /* Since the label is stored as a string in the "constvalue", the + * size of an identifier must be at least 8, as there are 8 + * hexadecimal digits in a 32-bit number. + */ + #if sNAMEMAX < 8 + #error Length of identifier (sNAMEMAX) too small. + #endif + assert(csp!=NULL); + assert(csp->next==cse); + insert_constval(csp,cse,itoh(lbl_case),val,0); + if (matchtoken(tDBLDOT)) { + cell end; + constexpr(&end,NULL,NULL); + if (end<=val) + error(50); /* invalid range */ + while (++val<=end) { + casecount++; + /* find the new insertion point */ + for (csp=&caselist, cse=caselist.next; + cse!=NULL && cse->valuenext) + /* nothing */; + if (cse!=NULL && cse->value==val) + error(40,val); /* duplicate "case" label */ + assert(csp!=NULL); + assert(csp->next==cse); + insert_constval(csp,cse,itoh(lbl_case),val,0); + } /* if */ + } /* if */ + } while (matchtoken(',')); + needtoken(':'); /* ':' ends the case */ + sc_allowtags=(short)POPSTK_I(); /* reset */ + setlabel(lbl_case); + statement(NULL,FALSE); + jumplabel(lbl_exit); + break; + case tDEFAULT: + if (swdefault!=FALSE) + error(16); /* multiple defaults in switch */ + lbl_case=getlabel(); + setlabel(lbl_case); + needtoken(':'); + swdefault=TRUE; + statement(NULL,FALSE); + /* Jump to lbl_exit, even thouh this is the last clause in the + * switch, because the jump table is generated between the last + * clause of the switch and the exit label. + */ + jumplabel(lbl_exit); + break; + case '}': + /* nothing, but avoid dropping into "default" */ + break; + default: + error(2); + indent_nowarn=TRUE; /* disable this check */ + tok='}'; /* break out of the loop after an error */ + } /* switch */ + } while (tok!='}'); + + #if !defined NDEBUG + /* verify that the case table is sorted (unfortunatly, duplicates can + * occur; there really shouldn't be duplicate cases, but the compiler + * may not crash or drop into an assertion for a user error). */ + for (cse=caselist.next; cse!=NULL && cse->next!=NULL; cse=cse->next) + assert(cse->value <= cse->next->value); + #endif + /* generate the table here, before lbl_exit (general jump target) */ + setlabel(lbl_table); + assert(swdefault==FALSE || swdefault==TRUE); + if (swdefault==FALSE) { + /* store lbl_exit as the "none-matched" label in the switch table */ + strcpy(labelname,itoh(lbl_exit)); + } else { + /* lbl_case holds the label of the "default" clause */ + strcpy(labelname,itoh(lbl_case)); + } /* if */ + ffcase(casecount,labelname,TRUE); + /* generate the rest of the table */ + for (cse=caselist.next; cse!=NULL; cse=cse->next) + ffcase(cse->value,cse->name,FALSE); + + setlabel(lbl_exit); + delete_consttable(&caselist); /* clear list of case labels */ +} + +static void doassert(void) +{ + int flab1,index; + cell cidx; + + if ((sc_debug & sCHKBOUNDS)!=0) { + flab1=getlabel(); /* get label number for "OK" branch */ + test(flab1,FALSE,TRUE); /* get expression and branch to flab1 if true */ + insert_dbgline(fline); /* make sure we can find the correct line number */ + ffabort(xASSERTION); + setlabel(flab1); + } else { + stgset(TRUE); /* start staging */ + stgget(&index,&cidx); /* mark position in code generator */ + do { + expression(NULL,NULL,NULL,FALSE); + stgdel(index,cidx); /* just scrap the code */ + } while (matchtoken(',')); + stgset(FALSE); /* stop staging */ + } /* if */ + needtoken(tTERM); +} + +static void dogoto(void) +{ + char *st; + cell val; + symbol *sym; + + if (lex(&val,&st)==tSYMBOL) { + sym=fetchlab(st); + jumplabel((int)sym->addr); + sym->usage|=uREAD; /* set "uREAD" bit */ + // ??? if the label is defined (check sym->usage & uDEFINE), check + // sym->compound (nesting level of the label) against nestlevel; + // if sym->compound < nestlevel, call the destructor operator + } else { + error(20,st); /* illegal symbol name */ + } /* if */ + needtoken(tTERM); +} + +static void dolabel(void) +{ + char *st; + cell val; + symbol *sym; + + tokeninfo(&val,&st); /* retrieve label name again */ + if (find_constval(&tagname_tab,st,0)!=NULL) + error(221,st); /* label name shadows tagname */ + sym=fetchlab(st); + setlabel((int)sym->addr); + /* since one can jump around variable declarations or out of compound + * blocks, the stack must be manually adjusted + */ + setstk(-declared*sizeof(cell)); + sym->usage|=uDEFINE; /* label is now defined */ +} + +/* fetchlab + * + * Finds a label from the (local) symbol table or adds one to it. + * Labels are local in scope. + * + * Note: The "_usage" bit is set to zero. The routines that call "fetchlab()" + * must set this bit accordingly. + */ +static symbol *fetchlab(char *name) +{ + symbol *sym; + + sym=findloc(name); /* labels are local in scope */ + if (sym){ + if (sym->ident!=iLABEL) + error(19,sym->name); /* not a label: ... */ + } else { + sym=addsym(name,getlabel(),iLABEL,sLOCAL,0,0); + assert(sym!=NULL); /* fatal error 103 must be given on error */ + sym->x.declared=(int)declared; + sym->compound=nestlevel; + } /* if */ + return sym; +} + +/* doreturn + * + * Global references: rettype (altered) + */ +static void doreturn(void) +{ + int tag,ident; + int level; + symbol *sym,*sub; + + if (!matchtoken(tTERM)) { + /* "return " */ + if ((rettype & uRETNONE)!=0) + error(78); /* mix "return;" and "return value;" */ + ident=doexpr(TRUE,FALSE,TRUE,TRUE,&tag,&sym,TRUE); + needtoken(tTERM); + /* see if this function already has a sub type (an array attached) */ + sub=finddepend(curfunc); + assert(sub==NULL || sub->ident==iREFARRAY); + if ((rettype & uRETVALUE)!=0) { + int retarray=(ident==iARRAY || ident==iREFARRAY); + /* there was an earlier "return" statement in this function */ + if (sub==NULL && retarray || sub!=NULL && !retarray) + error(79); /* mixing "return array;" and "return value;" */ + } /* if */ + rettype|=uRETVALUE; /* function returns a value */ + /* check tagname with function tagname */ + assert(curfunc!=NULL); + if (!matchtag(curfunc->tag,tag,TRUE)) + error(213); /* tagname mismatch */ + if (ident==iARRAY || ident==iREFARRAY) { + int dim[sDIMEN_MAX],numdim; + cell arraysize; + assert(sym!=NULL); + if (sub!=NULL) { + assert(sub->ident==iREFARRAY); + /* this function has an array attached already; check that the current + * "return" statement returns exactly the same array + */ + level=sym->dim.array.level; + if (sub->dim.array.level!=level) { + error(48); /* array dimensions must match */ + } else { + for (numdim=0; numdim<=level; numdim++) { + dim[numdim]=(int)sub->dim.array.length; + if (sym->dim.array.length!=dim[numdim]) + error(47); /* array sizes must match */ + if (numdimdim.array.level; + for (numdim=0; numdim<=level; numdim++) { + dim[numdim]=(int)sub->dim.array.length; + idxtag[numdim]=sub->x.idxtag; + if (numdimname); + } /* for */ + /* the address of the array is stored in a hidden parameter; the address + * of this parameter is 1 + the number of parameters (times the size of + * a cell) + the size of the stack frame and the return address + * base + 0*sizeof(cell) == previous "base" + * base + 1*sizeof(cell) == function return address + * base + 2*sizeof(cell) == number of arguments + * base + 3*sizeof(cell) == first argument of the function + * ... + * base + ((n-1)+3)*sizeof(cell) == last argument of the function + * base + (n+3)*sizeof(cell) == hidden parameter with array address + */ + assert(curfunc!=NULL); + assert(curfunc->dim.arglist!=NULL); + for (argcount=0; curfunc->dim.arglist[argcount].ident!=0; argcount++) + /* nothing */; + sub=addvariable(curfunc->name,(argcount+3)*sizeof(cell),iREFARRAY,sGLOBAL,curfunc->tag,dim,numdim,idxtag); + sub->parent=curfunc; + } /* if */ + /* get the hidden parameter, copy the array (the array is on the heap; + * it stays on the heap for the moment, and it is removed -usually- at + * the end of the expression/statement, see expression() in SC3.C) + */ + address(sub,sALT); /* ALT = destination */ + arraysize=calc_arraysize(dim,numdim,0); + memcopy(arraysize*sizeof(cell)); /* source already in PRI */ + /* moveto1(); is not necessary, callfunction() does a popreg() */ + } /* if */ + } else { + /* this return statement contains no expression */ + ldconst(0,sPRI); + if ((rettype & uRETVALUE)!=0) { + char symname[2*sNAMEMAX+16]; /* allow space for user defined operators */ + assert(curfunc!=NULL); + funcdisplayname(symname,curfunc->name); + error(209,symname); /* function should return a value */ + } /* if */ + rettype|=uRETNONE; /* function does not return anything */ + } /* if */ + destructsymbols(&loctab,0); /* call destructor for *all* locals */ + modstk((int)declared*sizeof(cell)); /* end of function, remove *all* + * local variables */ + ffret(); +} + +static void dobreak(void) +{ + int *ptr; + + ptr=readwhile(); /* readwhile() gives an error if not in loop */ + needtoken(tTERM); + if (ptr==NULL) + return; + destructsymbols(&loctab,nestlevel); + modstk(((int)declared-ptr[wqBRK])*sizeof(cell)); + jumplabel(ptr[wqEXIT]); +} + +static void docont(void) +{ + int *ptr; + + ptr=readwhile(); /* readwhile() gives an error if not in loop */ + needtoken(tTERM); + if (ptr==NULL) + return; + destructsymbols(&loctab,nestlevel); + modstk(((int)declared-ptr[wqCONT])*sizeof(cell)); + jumplabel(ptr[wqLOOP]); +} + +SC_FUNC void exporttag(int tag) +{ + /* find the tag by value in the table, then set the top bit to mark it + * "public" + */ + if (tag!=0 && (tag & PUBLICTAG)==0) { + constvalue *ptr; + for (ptr=tagname_tab.next; ptr!=NULL && tag!=(int)(ptr->value & TAGMASK); ptr=ptr->next) + /* nothing */; + if (ptr!=NULL) + ptr->value |= PUBLICTAG; + } /* if */ +} + +static void doexit(void) +{ + int tag=0; + + if (matchtoken(tTERM)==0){ + doexpr(TRUE,FALSE,FALSE,TRUE,&tag,NULL,TRUE); + needtoken(tTERM); + } else { + ldconst(0,sPRI); + } /* if */ + ldconst(tag,sALT); + exporttag(tag); + destructsymbols(&loctab,0); /* call destructor for *all* locals */ + ffabort(xEXIT); +} + +static void dosleep(void) +{ + int tag=0; + + if (matchtoken(tTERM)==0){ + doexpr(TRUE,FALSE,FALSE,TRUE,&tag,NULL,TRUE); + needtoken(tTERM); + } else { + ldconst(0,sPRI); + } /* if */ + ldconst(tag,sALT); + exporttag(tag); + ffabort(xSLEEP); +} + +static void dostate(void) +{ + char name[sNAMEMAX+1]; + cell val; + char *str; + constvalue *automaton; + constvalue *state; + constvalue *stlist; + int fsa,flabel; + int islabel; + symbol *sym; + #if !defined SC_LIGHT + int length,index,listid,listindex,stateindex; + char *doc; + #endif + + /* check for an optional condition */ + if (matchtoken('(')) { + flabel=getlabel(); /* get label number for "false" branch */ + pc_docexpr=TRUE; /* attach expression as a documentation string */ + test(flabel,FALSE,FALSE); /* get expression, branch to flabel if false */ + pc_docexpr=FALSE; + needtoken(')'); + } else { + flabel=-1; + } /* if */ + + fsa=0; + + if (!(islabel=matchtoken(tLABEL)) && !needtoken(tSYMBOL)) { + delete_autolisttable(); + return; + } /* if */ + tokeninfo(&val,&str); + assert(strlen(str)index; + if (!needtoken(tSYMBOL)) { + delete_autolisttable(); + return; + } /* if */ + tokeninfo(&val,&str); + assert(strlen(str)name; + if (*fsaname=='\0') + fsaname="

"; + error(87,name,fsaname); /* unknown state for automaton */ + } else { + ldconst(state->value,sPRI); + storereg(automaton->value,sPRI); + /* find the optional entry() function for the state */ + sym=findglb(uENTRYFUNC); + if (sc_status==statWRITE && sym!=NULL && sym->ident==iFUNCTN && sym->states!=NULL) { + for (stlist=sym->states->next; stlist!=NULL; stlist=stlist->next) { + assert(strlen(stlist->name)!=0); + if (state_getfsa(stlist->index)==fsa && state_inlist(stlist->index,(int)state->value)) + break; /* found! */ + } /* for */ + assert(stlist==NULL || state_inlist(stlist->index,state->value)); + if (stlist!=NULL) { + /* the label to jump to is in stlist->name */ + ffcall(sym,stlist->name,0); + } /* if */ + } /* if */ + } /* if */ + } /* if */ + needtoken(tTERM); + + if (flabel>=0) + setlabel(flabel); /* condition was false, jump around the state switch */ + + #if !defined SC_LIGHT + /* mark for documentation */ + if (sc_status==statFIRST) { + /* get the last list id attached to the function, this contains the source states */ + assert(curfunc!=NULL); + if (curfunc->states!=NULL) { + stlist=curfunc->states->next; + assert(stlist!=NULL); + while (stlist->next!=NULL) + stlist=stlist->next; + listid=stlist->index; + } else { + listid=-1; + } /* if */ + listindex=0; + length=strlen(name)+70; /* +70 for the fixed part "\n" */ + /* see if there are any condition strings to attach */ + for (index=0; (str=get_autolist(index))!=NULL; index++) + length+=strlen(str); + if ((doc=(char*)malloc(length*sizeof(char)))!=NULL) { + do { + sprintf(doc,"=0) { + /* get the source state */ + stateindex=state_listitem(listid,listindex); + state=state_findid(stateindex); + assert(state!=NULL); + sprintf(doc+strlen(doc)," source=\"%s\"",state->name); + } /* if */ + if (get_autolist(0)!=NULL) { + /* add the condition */ + strcat(doc," condition=\""); + for (index=0; (str=get_autolist(index))!=NULL; index++) { + /* remove the ')' token that may be appended before detecting that the expression has ended */ + if (*str!=')' || *(str+1)!='\0' || get_autolist(index+1)!=NULL) + strcat(doc,str); + } /* for */ + strcat(doc,"\""); + } /* if */ + strcat(doc,"/>\n"); + insert_docstring(doc); + } while (listid>=0 && ++listindex=(wq+wqTABSZ-wqSIZE)) + error(102,"loop table"); /* loop table overflow (too many active loops)*/ + k=0; + while (kwq) + wqptr-=wqSIZE; +} + +static int *readwhile(void) +{ + if (wqptr<=wq){ + error(24); /* out of context */ + return NULL; + } else { + return (wqptr-wqSIZE); + } /* if */ +} + diff --git a/compiler/libpc300/sc2.c b/compiler/libpc300/sc2.c new file mode 100755 index 00000000..2fc52a32 --- /dev/null +++ b/compiler/libpc300/sc2.c @@ -0,0 +1,2707 @@ +/* Pawn compiler - File input, preprocessing and lexical analysis functions + * + * Copyright (c) ITB CompuPhase, 1997-2005 + * + * This software is provided "as-is", without any express or implied warranty. + * In no event will the authors be held liable for any damages arising from + * the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software in + * a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * 3. This notice may not be removed or altered from any source distribution. + * + * Version: $Id$ + */ +#include +#include +#include +#include +#include +#include +#include "sc.h" +#if defined LINUX || defined __FreeBSD__ || defined __OpenBSD__ + #include +#endif + +#if defined FORTIFY + #include "fortify.h" +#endif + +/* flags for litchar() */ +#define RAWMODE 1 +#define UTF8MODE 2 +static cell litchar(const unsigned char **lptr,int flags); + +static void substallpatterns(unsigned char *line,int buffersize); +static int match(char *st,int end); +static int alpha(char c); + +#define SKIPMODE 1 /* bit field in "#if" stack */ +#define PARSEMODE 2 /* bit field in "#if" stack */ +#define HANDLED_ELSE 4 /* bit field in "#if" stack */ +#define SKIPPING (skiplevel>0 && (ifstack[skiplevel-1] & SKIPMODE)==SKIPMODE) + +static short icomment; /* currently in multiline comment? */ +static char ifstack[sCOMP_STACK]; /* "#if" stack */ +static short iflevel; /* nesting level if #if/#else/#endif */ +static short skiplevel; /* level at which we started skipping (including nested #if .. #endif) */ +static unsigned char term_expr[] = ""; +static int listline=-1; /* "current line" for the list file */ + + +/* pushstk & popstk + * + * Uses a LIFO stack to store information. The stack is used by doinclude(), + * doswitch() (to hold the state of "swactive") and some other routines. + * + * Porting note: I made the bold assumption that an integer will not be + * larger than a pointer (it may be smaller). That is, the stack element + * is typedef'ed as a pointer type, but I also store integers on it. See + * SC.H for "stkitem" + * + * Global references: stack,stkidx,stktop (private to pushstk(), popstk() + * and clearstk()) + */ +static stkitem *stack=NULL; +static int stkidx=0,stktop=0; + +SC_FUNC void pushstk(stkitem val) +{ + assert(stkidx<=stktop); + if (stkidx==stktop) { + stkitem *newstack; + int newsize= (stktop==0) ? 16 : 2*stktop; + /* try to resize the stack */ + assert(newsize>stktop); + newstack=(stkitem*)malloc(newsize*sizeof(stkitem)); + if (newstack==NULL) + error(102,"parser stack"); /* stack overflow (recursive include?) */ + /* swap the stacks */ + memcpy(newstack,stack,stkidx*sizeof(stkitem)); + free(stack); + stack=newstack; + stktop=newsize; + } /* if */ + assert(stkidx'); /* termination character */ + lptr++; + while (*lptr<=' ' && *lptr!='\0') /* skip whitespace after quote */ + lptr++; + } else { + c='\0'; + } /* if */ + + i=0; + while (*lptr!=c && *lptr!='\0' && i0 && name[i-1]<=' ') + i--; /* strip trailing whitespace */ + assert(i>=0 && i are only read from the list of include directories. + */ + result=plungefile(name,(c!='>'),TRUE); + if (!result && !silent) + error(100,name); /* cannot read from ... (fatal error) */ +} + +/* readline + * + * Reads in a new line from the input file pointed to by "inpf". readline() + * concatenates lines that end with a \ with the next line. If no more data + * can be read from the file, readline() attempts to pop off the previous file + * from the stack. If that fails too, it sets "freading" to 0. + * + * Global references: inpf,fline,inpfname,freading,icomment (altered) + */ +static void readline(unsigned char *line) +{ + int i,num,cont; + unsigned char *ptr; + + if (lptr==term_expr) + return; + num=sLINEMAX; + cont=FALSE; + do { + if (inpf==NULL || pc_eofsrc(inpf)) { + if (cont) + error(49); /* invalid line continuation */ + if (inpf!=NULL && inpf!=inpf_org) + pc_closesrc(inpf); + i=POPSTK_I(); + if (i==-1) { /* All's done; popstk() returns "stack is empty" */ + freading=FALSE; + *line='\0'; + /* when there is nothing more to read, the #if/#else stack should + * be empty and we should not be in a comment + */ + assert(iflevel>=0); + if (iflevel>0) + error(1,"#endif","-end of file-"); + else if (icomment!=0) + error(1,"*/","-end of file-"); + return; + } /* if */ + fline=i; + fcurrent=(short)POPSTK_I(); + icomment=(short)POPSTK_I(); + sc_is_utf8=(short)POPSTK_I(); + iflevel=(short)POPSTK_I(); + skiplevel=iflevel; /* this condition held before including the file */ + assert(!SKIPPING); /* idem ditto */ + curlibrary=(constvalue *)POPSTK_P(); + free(inpfname); /* return memory allocated for the include file name */ + inpfname=(char *)POPSTK_P(); + inpf=(FILE *)POPSTK_P(); + insert_dbgfile(inpfname); + setfiledirect(inpfname); + listline=-1; /* force a #line directive when changing the file */ + } /* if */ + + if (pc_readsrc(inpf,line,num)==NULL) { + *line='\0'; /* delete line */ + cont=FALSE; + } else { + /* check whether to erase leading spaces */ + if (cont) { + unsigned char *ptr=line; + while (*ptr<=' ' && *ptr!='\0') + ptr++; + if (ptr!=line) + memmove(line,ptr,strlen((char*)ptr)+1); + } /* if */ + cont=FALSE; + /* check whether a full line was read */ + if (strchr((char*)line,'\n')==NULL && !pc_eofsrc(inpf)) + error(75); /* line too long */ + /* check if the next line must be concatenated to this line */ + if ((ptr=(unsigned char*)strchr((char*)line,'\n'))==NULL) + ptr=(unsigned char*)strchr((char*)line,'\r'); + if (ptr!=NULL && ptr>line) { + assert(*(ptr+1)=='\0'); /* '\n' or '\r' should be last in the string */ + while (ptr>line && *ptr<=' ') + ptr--; /* skip trailing whitespace */ + if (*ptr=='\\') { + cont=TRUE; + /* set '\a' at the position of '\\' to make it possible to check + * for a line continuation in a single line comment (error 49) + */ + *ptr++='\a'; + *ptr='\0'; /* erase '\n' (and any trailing whitespace) */ + } /* if */ + } /* if */ + num-=strlen((char*)line); + line+=strlen((char*)line); + } /* if */ + fline+=1; + } while (num>=0 && cont); +} + +/* stripcom + * + * Replaces all comments from the line by space characters. It updates + * a global variable ("icomment") for multiline comments. + * + * This routine also supports the C++ extension for single line comments. + * These comments are started with "//" and end at the end of the line. + * + * The function also detects (and manages) "documentation comments". The + * global variable "icomment" is set to 2 for documentation comments. + * + * Global references: icomment (private to "stripcom") + */ +static void stripcom(unsigned char *line) +{ + char c; + #if !defined SC_LIGHT + #define COMMENT_LIMIT 100 + #define COMMENT_MARGIN 40 /* length of the longest word */ + char comment[COMMENT_LIMIT+COMMENT_MARGIN]; + int commentidx=0; + int skipstar=TRUE; + static int prev_singleline=FALSE; + int singleline=prev_singleline; + + prev_singleline=FALSE; /* preset */ + #endif + + while (*line){ + if (icomment!=0) { + if (*line=='*' && *(line+1)=='/') { + #if !defined SC_LIGHT + if (icomment==2) { + assert(commentidx0) + insert_docstring(comment); + } /* if */ + #endif + icomment=0; /* comment has ended */ + *line=' '; /* replace '*' and '/' characters by spaces */ + *(line+1)=' '; + line+=2; + } else { + if (*line=='/' && *(line+1)=='*') + error(216); /* nested comment */ + #if !defined SC_LIGHT + /* collect the comment characters in a string */ + if (icomment==2) { + if (skipstar && (*line!='\0' && *line<=' ' || *line=='*')) { + /* ignore leading whitespace and '*' characters */ + } else if (commentidxCOMMENT_LIMIT && *line!='\0' && *line<=' ') { + comment[commentidx]='\0'; + insert_docstring(comment); + commentidx=0; + } /* if */ + skipstar=FALSE; + } /* if */ + } /* if */ + #endif + *line=' '; /* replace comments by spaces */ + line+=1; + } /* if */ + } else { + if (*line=='/' && *(line+1)=='*'){ + icomment=1; /* start comment */ + #if !defined SC_LIGHT + /* there must be two "*" behind the slash and then white space */ + if (*(line+2)=='*' && *(line+3)<=' ') { + /* if we are not in a function, we must attach the previous block + * to the global documentation + */ + if (curfunc==NULL && get_docstring(0)!=NULL) + sc_attachdocumentation(NULL); + icomment=2; /* documentation comment */ + } /* if */ + commentidx=0; + skipstar=TRUE; + #endif + *line=' '; /* replace '/' and '*' characters by spaces */ + *(line+1)=' '; + line+=2; + if (icomment==2) + *line++=' '; + } else if (*line=='/' && *(line+1)=='/'){ /* comment to end of line */ + if (strchr((char*)line,'\a')!=NULL) + error(49); /* invalid line continuation */ + #if !defined SC_LIGHT + if (*(line+2)=='/' && *(line+3)<=' ') { + /* documentation comment */ + char *str=(char*)line+3; + char *end; + while (*str<=' ' && *str!='\0') + str++; /* skip leading whitespace */ + if ((end=strrchr(str,'\n'))!=NULL) + *end='\0';/* erase trailing '\n' */ + /* if there is a disjunct block, we may need to attach the previous + * block to the global documentation + */ + if (!singleline && curfunc==NULL && get_docstring(0)!=NULL) + sc_attachdocumentation(NULL); + insert_docstring(str); + prev_singleline=TRUE; + } /* if */ + #endif + *line++='\n'; /* put "newline" at first slash */ + *line='\0'; /* put "zero-terminator" at second slash */ + } else { + if (*line=='\"' || *line=='\''){ /* leave literals unaltered */ + c=*line; /* ending quote, single or double */ + line+=1; + while ((*line!=c || *(line-1)==sc_ctrlchar) && *line!='\0') + line+=1; + line+=1; /* skip final quote */ + } else { + line+=1; + } /* if */ + } /* if */ + } /* if */ + } /* while */ + #if !defined SC_LIGHT + if (icomment==2) { + assert(commentidx0) + insert_docstring(comment); + } /* if */ + #endif +} + +/* btoi + * + * Attempts to interpret a numeric symbol as a boolean value. On success + * it returns the number of characters processed (so the line pointer can be + * adjusted) and the value is stored in "val". Otherwise it returns 0 and + * "val" is garbage. + * + * A boolean value must start with "0b" + */ +static int btoi(cell *val,const unsigned char *curptr) +{ + const unsigned char *ptr; + + *val=0; + ptr=curptr; + if (*ptr=='0' && *(ptr+1)=='b') { + ptr+=2; + while (*ptr=='0' || *ptr=='1' || *ptr=='_') { + if (*ptr!='_') + *val=(*val<<1) | (*ptr-'0'); + ptr++; + } /* while */ + } else { + return 0; + } /* if */ + if (alphanum(*ptr)) /* number must be delimited by non-alphanumeric char */ + return 0; + else + return (int)(ptr-curptr); +} + +/* dtoi + * + * Attempts to interpret a numeric symbol as a decimal value. On success + * it returns the number of characters processed and the value is stored in + * "val". Otherwise it returns 0 and "val" is garbage. + */ +static int dtoi(cell *val,const unsigned char *curptr) +{ + const unsigned char *ptr; + + *val=0; + ptr=curptr; + if (!isdigit(*ptr)) /* should start with digit */ + return 0; + while (isdigit(*ptr) || *ptr=='_') { + if (*ptr!='_') + *val=(*val*10)+(*ptr-'0'); + ptr++; + } /* while */ + if (alphanum(*ptr)) /* number must be delimited by non-alphanumerical */ + return 0; + if (*ptr=='.' && isdigit(*(ptr+1))) + return 0; /* but a fractional part must not be present */ + return (int)(ptr-curptr); +} + +/* htoi + * + * Attempts to interpret a numeric symbol as a hexadecimal value. On + * success it returns the number of characters processed and the value is + * stored in "val". Otherwise it return 0 and "val" is garbage. + */ +static int htoi(cell *val,const unsigned char *curptr) +{ + const unsigned char *ptr; + + *val=0; + ptr=curptr; + if (!isdigit(*ptr)) /* should start with digit */ + return 0; + if (*ptr=='0' && *(ptr+1)=='x') { /* C style hexadecimal notation */ + ptr+=2; + while (ishex(*ptr) || *ptr=='_') { + if (*ptr!='_') { + assert(ishex(*ptr)); + *val= *val<<4; + if (isdigit(*ptr)) + *val+= (*ptr-'0'); + else + *val+= (tolower(*ptr)-'a'+10); + } /* if */ + ptr++; + } /* while */ + } else { + return 0; + } /* if */ + if (alphanum(*ptr)) + return 0; + else + return (int)(ptr-curptr); +} + +#if defined __GNUC__ +static double pow10(int value) +{ + double res=1.0; + while (value>=4) { + res*=10000.0; + value-=5; + } /* while */ + while (value>=2) { + res*=100.0; + value-=2; + } /* while */ + while (value>=1) { + res*=10.0; + value-=1; + } /* while */ + return res; +} +#endif + +/* ftoi + * + * Attempts to interpret a numeric symbol as a rational number, either as + * IEEE 754 single/double precision floating point or as a fixed point integer. + * On success it returns the number of characters processed and the value is + * stored in "val". Otherwise it returns 0 and "val" is unchanged. + * + * Pawn has stricter definition for rational numbers than most: + * o the value must start with a digit; ".5" is not a valid number, you + * should write "0.5" + * o a period must appear in the value, even if an exponent is given; "2e3" + * is not a valid number, you should write "2.0e3" + * o at least one digit must follow the period; "6." is not a valid number, + * you should write "6.0" + */ +static int ftoi(cell *val,const unsigned char *curptr) +{ + const unsigned char *ptr; + double fnum,ffrac,fmult; + unsigned long dnum,dbase; + int i, ignore; + + assert(rational_digits>=0 && rational_digits<9); + for (i=0,dbase=1; i0 && !ignore) { + error(222); /* number of digits exceeds rational number precision */ + ignore=TRUE; + } /* if */ + } /* if */ + ptr++; + } /* while */ + fnum += ffrac*fmult; /* form the number so far */ + if (*ptr=='e') { /* optional fractional part */ + int exp,sign; + ptr++; + if (*ptr=='-') { + sign=-1; + ptr++; + } else { + sign=1; + } /* if */ + if (!isdigit(*ptr)) /* 'e' should be followed by a digit */ + return 0; + exp=0; + while (isdigit(*ptr)) { + exp=(exp*10)+(*ptr-'0'); + ptr++; + } /* while */ + #if defined __GNUC__ + fmult=pow10(exp*sign); + #else + fmult=pow(10,exp*sign); + #endif + fnum *= fmult; + dnum *= (unsigned long)(fmult+0.5); + } /* if */ + + /* decide how to store the number */ + if (sc_rationaltag==0) { + error(70); /* rational number support was not enabled */ + *val=0; + } else if (rational_digits==0) { + /* floating point */ + #if PAWN_CELL_SIZE==32 + float value=(float)fnum; + *val=*((cell *)&value); + #if !defined NDEBUG + /* I assume that the C/C++ compiler stores "float" values in IEEE 754 + * format (as mandated in the ANSI standard). Test this assumption + * anyway. + * Note: problems have been reported with GCC 3.2.x, version 3.3.x works. + */ + { float test1 = 0.0, test2 = 50.0, test3 = -50.0; + uint32_t bit = 1; + /* test 0.0 == all bits 0 */ + assert(*(uint32_t*)&test1==0x00000000L); + /* test sign & magnitude format */ + assert(((*(uint32_t*)&test2) ^ (*(uint32_t*)&test3)) == (bit << (PAWN_CELL_SIZE-1))); + /* test a known value */ + assert(*(uint32_t*)&test2==0x42480000L); + } + #endif + #elif PAWN_CELL_SIZE==64 + *val=*((cell *)&fnum); + #if !defined NDEBUG + /* I assume that the C/C++ compiler stores "double" values in IEEE 754 + * format (as mandated in the ANSI standard). + */ + { float test1 = 0.0, test2 = 50.0, test3 = -50.0; + uint64_t bit = 1; + /* test 0.0 == all bits 0 */ + assert(*(uint64_t*)&test1==0x00000000L); + /* test sign & magnitude format */ + assert(((*(uint64_t*)&test2) ^ (*(uint64_t*)&test3)) == (bit << (PAWN_CELL_SIZE-1))); + } + #endif + #else + #error Unsupported cell size + #endif + } else { + /* fixed point */ + *val=(cell)dnum; + } /* if */ + + return (int)(ptr-curptr); +} + +/* number + * + * Reads in a number (binary, decimal or hexadecimal). It returns the number + * of characters processed or 0 if the symbol couldn't be interpreted as a + * number (in this case the argument "val" remains unchanged). This routine + * relies on the 'early dropout' implementation of the logical or (||) + * operator. + * + * Note: the routine doesn't check for a sign (+ or -). The - is checked + * for at "hier2()" (in fact, it is viewed as an operator, not as a + * sign) and the + is invalid (as in K&R C, and unlike ANSI C). + */ +static int number(cell *val,const unsigned char *curptr) +{ + int i; + cell value; + + if ((i=btoi(&value,curptr))!=0 /* binary? */ + || (i=htoi(&value,curptr))!=0 /* hexadecimal? */ + || (i=dtoi(&value,curptr))!=0) /* decimal? */ + { + *val=value; + return i; + } else { + return 0; /* else not a number */ + } /* if */ +} + +static void chrcat(char *str,char chr) +{ + str=strchr(str,'\0'); + *str++=chr; + *str='\0'; +} + +static int preproc_expr(cell *val,int *tag) +{ + int result; + int index; + cell code_index; + char *term; + + /* Disable staging; it should be disabled already because + * expressions may not be cut off half-way between conditional + * compilations. Reset the staging index, but keep the code + * index. + */ + if (stgget(&index,&code_index)) { + error(57); /* unfinished expression */ + stgdel(0,code_index); + stgset(FALSE); + } /* if */ + assert((lptr-pline)<(int)strlen((char*)pline)); /* lptr must point inside the string */ + #if !defined NO_DEFINE + /* preprocess the string */ + substallpatterns(pline,sLINEMAX); + assert((lptr-pline)<(int)strlen((char*)pline)); /* lptr must STILL point inside the string */ + #endif + /* append a special symbol to the string, so the expression + * analyzer won't try to read a next line when it encounters + * an end-of-line + */ + assert(strlen((char*)pline)=0); + if (iflevel>=sCOMP_STACK) + error(102,"Conditional compilation stack"); /* table overflow */ + iflevel++; + if (SKIPPING) + break; /* break out of switch */ + skiplevel=iflevel; + preproc_expr(&val,NULL); /* get value (or 0 on error) */ + ifstack[iflevel-1]=(char)(val ? PARSEMODE : SKIPMODE); + check_empty(lptr); + break; + case tpELSE: + case tpELSEIF: + ret=CMD_IF; + assert(iflevel>=0); + if (iflevel==0) { + error(26); /* no matching #if */ + errorset(sRESET); + } else { + /* check for earlier #else */ + if ((ifstack[iflevel-1] & HANDLED_ELSE)==HANDLED_ELSE) { + if (tok==tpELSEIF) + error(61); /* #elseif directive may not follow an #else */ + else + error(60); /* multiple #else directives between #if ... #endif */ + errorset(sRESET); + } else { + assert(iflevel>0); + /* if there has been a "parse mode" on this level, set "skip mode", + * otherwise, clear "skip mode" + */ + if ((ifstack[iflevel-1] & PARSEMODE)==PARSEMODE) { + /* there has been a parse mode already on this level, so skip the rest */ + ifstack[iflevel-1] |= (char)SKIPMODE; + } else { + /* previous conditions were all FALSE */ + if (tok==tpELSEIF) { + /* get new expression */ + preproc_expr(&val,NULL); /* get value (or 0 on error) */ + ifstack[iflevel-1]=(char)(val ? PARSEMODE : SKIPMODE); + } else { + /* a simple #else, clear skip mode */ + ifstack[iflevel-1] &= (char)~SKIPMODE; + } /* if */ + } /* if */ + } /* if */ + } /* if */ + check_empty(lptr); + break; + case tpENDIF: + ret=CMD_IF; + if (iflevel==0){ + error(26); /* no matching "#if" */ + errorset(sRESET); + } else { + iflevel--; + if (iflevel0) { + free(inpfname); + inpfname=duplicatestring(pathname); + if (inpfname==NULL) + error(103); /* insufficient memory */ + } /* if */ + } /* if */ + check_empty(lptr); + break; + case tpLINE: + if (!SKIPPING) { + if (lex(&val,&str)!=tNUMBER) + error(8); /* invalid/non-constant expression */ + fline=(int)val; + } /* if */ + check_empty(lptr); + break; + case tpASSERT: + if (!SKIPPING && (sc_debug & sCHKBOUNDS)!=0) { + for (str=(char*)lptr; *str<=' ' && *str!='\0'; str++) + /* nothing */; /* save start of expression */ + preproc_expr(&val,NULL); /* get constant expression (or 0 on error) */ + if (!val) + error(110,str); /* assertion failed */ + check_empty(lptr); + } /* if */ + break; + case tpPRAGMA: + if (!SKIPPING) { + if (lex(&val,&str)==tSYMBOL) { + if (strcmp(str,"amxlimit")==0) { + preproc_expr(&sc_amxlimit,NULL); + } else if (strcmp(str,"codepage")==0) { + char name[sNAMEMAX+1]; + while (*lptr<=' ' && *lptr!='\0') + lptr++; + if (*lptr=='"') { + lptr=getstring((unsigned char*)name,sizeof name,lptr); + } else { + int i; + for (i=0; i9) { + error(68); /* invalid rational number precision */ + digits=0; + } /* if */ + if (*lptr==')') + lptr++; + } /* if */ + /* add the tag (make it public) and check the values */ + i=pc_addtag(name); + exporttag(i); + if (sc_rationaltag==0 || (sc_rationaltag==i && rational_digits==(int)digits)) { + sc_rationaltag=i; + rational_digits=(int)digits; + } else { + error(69); /* rational number format already set, can only be set once */ + } /* if */ + } else if (strcmp(str,"semicolon")==0) { + cell val; + preproc_expr(&val,NULL); + sc_needsemicolon=(int)val; + } else if (strcmp(str,"tabsize")==0) { + cell val; + preproc_expr(&val,NULL); + sc_tabsize=(int)val; + } else if (strcmp(str,"align")==0) { + sc_alignnext=TRUE; + } else if (strcmp(str,"unused")==0) { + char name[sNAMEMAX+1]; + int i,comma; + symbol *sym; + do { + /* get the name */ + while (*lptr<=' ' && *lptr!='\0') + lptr++; + for (i=0; iusage |= uREAD; + if (sym->ident==iVARIABLE || sym->ident==iREFERENCE + || sym->ident==iARRAY || sym->ident==iREFARRAY) + sym->usage |= uWRITTEN; + } else { + error(17,name); /* undefined symbol */ + } /* if */ + /* see if a comma follows the name */ + while (*lptr<=' ' && *lptr!='\0') + lptr++; + comma= (*lptr==','); + if (comma) + lptr++; + } while (comma); + } else { + error(207); /* unknown #pragma */ + } /* if */ + } else { + error(207); /* unknown #pragma */ + } /* if */ + check_empty(lptr); + } /* if */ + break; + case tpENDINPUT: + case tpENDSCRPT: + if (!SKIPPING) { + check_empty(lptr); + assert(inpf!=NULL); + if (inpf!=inpf_org) + pc_closesrc(inpf); + inpf=NULL; + } /* if */ + break; +#if !defined NOEMIT + case tpEMIT: { + /* write opcode to output file */ + char name[40]; + int i; + while (*lptr<=' ' && *lptr!='\0') + lptr++; + for (i=0; i<40 && (isalpha(*lptr) || *lptr=='.'); i++,lptr++) + name[i]=(char)tolower(*lptr); + name[i]='\0'; + stgwrite("\t"); + stgwrite(name); + stgwrite(" "); + code_idx+=opcodes(1); + /* write parameter (if any) */ + while (*lptr<=' ' && *lptr!='\0') + lptr++; + if (*lptr!='\0') { + symbol *sym; + tok=lex(&val,&str); + switch (tok) { + case tNUMBER: + case tRATIONAL: + outval(val,FALSE); + code_idx+=opargs(1); + break; + case tSYMBOL: + sym=findloc(str); + if (sym==NULL) + sym=findglb(str); + if (sym==NULL || sym->ident!=iFUNCTN && sym->ident!=iREFFUNC && (sym->usage & uDEFINE)==0) { + error(17,str); /* undefined symbol */ + } else { + outval(sym->addr,FALSE); + /* mark symbol as "used", unknown whether for read or write */ + markusage(sym,uREAD | uWRITTEN); + code_idx+=opargs(1); + } /* if */ + break; + default: { + char s2[20]; + extern char *sc_tokens[];/* forward declaration */ + if (tok<256) + sprintf(s2,"%c",(char)tok); + else + strcpy(s2,sc_tokens[tok-tFIRST]); + error(1,sc_tokens[tSYMBOL-tFIRST],s2); + break; + } /* case */ + } /* switch */ + } /* if */ + stgwrite("\n"); + check_empty(lptr); + break; + } /* case */ +#endif +#if !defined NO_DEFINE + case tpDEFINE: { + ret=CMD_DEFINE; + if (!SKIPPING) { + char *pattern,*substitution; + const unsigned char *start,*end; + int count,prefixlen; + stringpair *def; + /* find the pattern to match */ + while (*lptr<=' ' && *lptr!='\0') + lptr++; + start=lptr; /* save starting point of the match pattern */ + count=0; + while (*lptr>' ' && *lptr!='\0') { + litchar(&lptr,0); /* litchar() advances "lptr" and handles escape characters */ + count++; + } /* while */ + end=lptr; + /* check pattern to match */ + if (!isalpha(*start) && *start!='_') { + error(74); /* pattern must start with an alphabetic character */ + break; + } /* if */ + /* store matched pattern */ + pattern=(char*)malloc(count+1); + if (pattern==NULL) + error(103); /* insufficient memory */ + lptr=start; + count=0; + while (lptr!=end) { + assert(lptr=2 && isdigit(pattern[count-1]) && pattern[count-2]=='%') + pattern[count-2]='\0'; + /* find substitution string */ + while (*lptr<=' ' && *lptr!='\0') + lptr++; + start=lptr; /* save starting point of the match pattern */ + count=0; + end=NULL; + while (*lptr!='\0') { + /* keep position of the start of trailing whitespace */ + if (*lptr<=' ') { + if (end==NULL) + end=lptr; + } else { + end=NULL; + } /* if */ + count++; + lptr++; + } /* while */ + if (end==NULL) + end=lptr; + /* store matched substitution */ + substitution=(char*)malloc(count+1); /* +1 for '\0' */ + if (substitution==NULL) + error(103); /* insufficient memory */ + lptr=start; + count=0; + while (lptr!=end) { + assert(lptr0); + if ((def=find_subst(pattern,prefixlen))!=NULL) { + if (strcmp(def->first,pattern)!=0 || strcmp(def->second,substitution)!=0) + error(201,pattern); /* redefinition of macro (non-identical) */ + delete_subst(pattern,prefixlen); + } /* if */ + /* add the pattern/substitution pair to the list */ + assert(strlen(pattern)>0); + insert_subst(pattern,substitution,prefixlen); + free(pattern); + free(substitution); + } /* if */ + break; + } /* case */ + case tpUNDEF: + if (!SKIPPING) { + if (lex(&val,&str)==tSYMBOL) { + if (!delete_subst(str,strlen(str))) + error(17,str); /* undefined symbol */ + } else { + error(20,str); /* invalid symbol name */ + } /* if */ + check_empty(lptr); + } /* if */ + break; +#endif + case tpERROR: + while (*lptr<=' ' && *lptr!='\0') + lptr++; + error(111,lptr); /* user error */ + break; + default: + error(31); /* unknown compiler directive */ + ret=SKIPPING ? CMD_CONDFALSE : CMD_NONE; /* process as normal line */ + } /* switch */ + return ret; +} + +#if !defined NO_DEFINE +static int is_startstring(const unsigned char *string) +{ + if (*string=='\"' || *string=='\'') + return TRUE; /* "..." */ + + if (*string=='!') { + string++; + if (*string=='\"' || *string=='\'') + return TRUE; /* !"..." */ + if (*string==sc_ctrlchar) { + string++; + if (*string=='\"' || *string=='\'') + return TRUE; /* !\"..." */ + } /* if */ + } else if (*string==sc_ctrlchar) { + string++; + if (*string=='\"' || *string=='\'') + return TRUE; /* \"..." */ + if (*string=='!') { + string++; + if (*string=='\"' || *string=='\'') + return TRUE; /* \!"..." */ + } /* if */ + } /* if */ + + return FALSE; +} + +static const unsigned char *skipstring(const unsigned char *string) +{ + char endquote; + int flags=0; + + while (*string=='!' || *string==sc_ctrlchar) { + if (*string==sc_ctrlchar) + flags=RAWMODE; + string++; + } /* while */ + + endquote=*string; + assert(endquote=='"' || endquote=='\''); + string++; /* skip open quote */ + while (*string!=endquote && *string!='\0') + litchar(&string,flags); + return string; +} + +static const unsigned char *skippgroup(const unsigned char *string) +{ + int nest=0; + char open=*string; + char close; + + switch (open) { + case '(': + close=')'; + break; + case '{': + close='}'; + break; + case '[': + close=']'; + break; + case '<': + close='>'; + break; + default: + assert(0); + close='\0'; /* only to avoid a compiler warning */ + }/* switch */ + + string++; + while (*string!=close || nest>0) { + if (*string==open) + nest++; + else if (*string==close) + nest--; + else if (is_startstring(string)) + string=skipstring(string); + if (*string=='\0') + break; + string++; + } /* while */ + return string; +} + +static char *strdel(char *str,size_t len) +{ + size_t length=strlen(str); + if (len>length) + len=length; + memmove(str, str+len, length-len+1); /* include EOS byte */ + return str; +} + +static char *strins(char *dest,char *src,size_t srclen) +{ + size_t destlen=strlen(dest); + assert(srclen<=strlen(src)); + memmove(dest+srclen, dest, destlen+1);/* include EOS byte */ + memcpy(dest, src, srclen); + return dest; +} + +static int substpattern(unsigned char *line,size_t buffersize,char *pattern,char *substitution) +{ + int prefixlen; + const unsigned char *p,*s,*e; + unsigned char *args[10]; + int match,arg,len; + + memset(args,0,sizeof args); + + /* check the length of the prefix */ + for (prefixlen=0,s=(unsigned char*)pattern; isalpha(*s) || isdigit(*s) || *s=='_'; prefixlen++,s++) + /* nothing */; + assert(prefixlen>0); + assert(strncmp((char*)line,pattern,prefixlen)==0); + + /* pattern prefix matches; match the rest of the pattern, gather + * the parameters + */ + s=line+prefixlen; + p=(unsigned char*)pattern+prefixlen; + match=TRUE; /* so far, pattern matches */ + while (match && *s!='\0' && *p!='\0') { + if (*p=='%') { + p++; /* skip '%' */ + if (isdigit(*p)) { + arg=*p-'0'; + assert(arg>=0 && arg<=9); + p++; /* skip parameter id */ + assert(*p!='\0'); + /* match the source string up to the character after the digit + * (skipping strings in the process + */ + e=s; + while (*e!=*p && *e!='\0' && *e!='\n') { + if (is_startstring(e)) /* skip strings */ + e=skipstring(e); + else if (strchr("({[",*e)!=NULL) /* skip parenthized groups */ + e=skippgroup(e); + if (*e!='\0') + e++; /* skip non-alphapetic character (or closing quote of + * a string, or the closing paranthese of a group) */ + } /* while */ + /* store the parameter (overrule any earlier) */ + if (args[arg]!=NULL) + free(args[arg]); + len=(int)(e-s); + args[arg]=(unsigned char*)malloc(len+1); + if (args[arg]==NULL) + error(103); /* insufficient memory */ + strncpy((char*)args[arg],(char*)s,len); + args[arg][len]='\0'; + /* character behind the pattern was matched too */ + if (*e==*p) { + s=e+1; + } else if (*e=='\n' && *p==';' && *(p+1)=='\0' && !sc_needsemicolon) { + s=e; /* allow a trailing ; in the pattern match to end of line */ + } else { + assert(*e=='\0' || *e=='\n'); + match=FALSE; + s=e; + } /* if */ + p++; + } else { + match=FALSE; + } /* if */ + } else if (*p==';' && *(p+1)=='\0' && !sc_needsemicolon) { + /* source may be ';' or end of the line */ + while (*s<=' ' && *s!='\0') + s++; /* skip white space */ + if (*s!=';' && *s!='\0') + match=FALSE; + p++; /* skip the semicolon in the pattern */ + } else { + cell ch; + /* skip whitespace between two non-alphanumeric characters, except + * for two identical symbols + */ + assert((char*)p>pattern); + if (!alphanum(*p) && *(p-1)!=*p) + while (*s<=' ' && *s!='\0') + s++; /* skip white space */ + ch=litchar(&p,0); /* this increments "p" */ + if (*s!=ch) + match=FALSE; + else + s++; /* this character matches */ + } /* if */ + } /* while */ + + if (match && *p=='\0') { + /* if the last character to match is an alphanumeric character, the + * current character in the source may not be alphanumeric + */ + assert(p>(unsigned char*)pattern); + if (alphanum(*(p-1)) && alphanum(*s)) + match=FALSE; + } /* if */ + + if (match) { + /* calculate the length of the substituted string */ + for (e=(unsigned char*)substitution,len=0; *e!='\0'; e++) { + if (*e=='%' && isdigit(*(e+1))) { + arg=*(e+1)-'0'; + assert(arg>=0 && arg<=9); + if (args[arg]!=NULL) + len+=strlen((char*)args[arg]); + e++; /* skip %, digit is skipped later */ + } else { + len++; + } /* if */ + } /* for */ + /* check length of the string after substitution */ + if (strlen((char*)line) + len - (int)(s-line) > buffersize) { + error(75); /* line too long */ + } else { + /* substitute pattern */ + strdel((char*)line,(int)(s-line)); + for (e=(unsigned char*)substitution,s=line; *e!='\0'; e++) { + if (*e=='%' && isdigit(*(e+1))) { + arg=*(e+1)-'0'; + assert(arg>=0 && arg<=9); + if (args[arg]!=NULL) { + strins((char*)s,(char*)args[arg],strlen((char*)args[arg])); + s+=strlen((char*)args[arg]); + } /* if */ + e++; /* skip %, digit is skipped later */ + } else { + strins((char*)s,(char*)e,1); + s++; + } /* if */ + } /* for */ + } /* if */ + } /* if */ + + for (arg=0; arg<10; arg++) + if (args[arg]!=NULL) + free(args[arg]); + + return match; +} + +static void substallpatterns(unsigned char *line,int buffersize) +{ + unsigned char *start, *end; + int prefixlen; + stringpair *subst; + + start=line; + while (*start!='\0') { + /* find the start of a prefix (skip all non-alphabetic characters), + * also skip strings + */ + while (!isalpha(*start) && *start!='_' && *start!='\0') { + /* skip strings */ + if (is_startstring(start)) { + start=(unsigned char *)skipstring(start); + if (*start=='\0') + break; /* abort loop on error */ + } /* if */ + start++; /* skip non-alphapetic character (or closing quote of a string) */ + } /* while */ + if (*start=='\0') + break; /* abort loop on error */ + /* if matching the operator "defined", skip it plus the symbol behind it */ + if (strncmp((char*)start,"defined",7)==0 && *(start+7)<=' ') { + start+=7; /* skip "defined" */ + /* skip white space & parantheses */ + while (*start<=' ' && *start!='\0' || *start=='(') + start++; + /* skip the symbol behind it */ + while (isalpha(*start) || isdigit(*start) || *start=='_') + start++; + /* drop back into the main loop */ + continue; + } /* if */ + /* get the prefix (length), look for a matching definition */ + prefixlen=0; + end=start; + while (isalpha(*end) || isdigit(*end) || *end=='_') { + prefixlen++; + end++; + } /* while */ + assert(prefixlen>0); + subst=find_subst((char*)start,prefixlen); + if (subst!=NULL) { + /* properly match the pattern and substitute */ + if (!substpattern(start,buffersize-(int)(start-line),subst->first,subst->second)) + start=end; /* match failed, skip this prefix */ + /* match succeeded: do not update "start", because the substitution text + * may be matched by other macros + */ + } else { + start=end; /* no macro with this prefix, skip this prefix */ + } /* if */ + } /* while */ +} +#endif + +/* preprocess + * + * Reads a line by readline() into "pline" and performs basic preprocessing: + * deleting comments, skipping lines with false "#if.." code and recognizing + * other compiler directives. There is an indirect recursion: lex() calls + * preprocess() if a new line must be read, preprocess() calls command(), + * which at his turn calls lex() to identify the token. + * + * Global references: lptr (altered) + * pline (altered) + * freading (referred to only) + */ +SC_FUNC void preprocess(void) +{ + int iscommand; + + if (!freading) + return; + do { + readline(pline); + stripcom(pline); /* ??? no need for this when reading back from list file (in the second pass) */ + lptr=pline; /* set "line pointer" to start of the parsing buffer */ + iscommand=command(); + if (iscommand!=CMD_NONE) + errorset(sRESET); /* reset error flag ("panic mode") on empty line or directive */ + #if !defined NO_DEFINE + if (iscommand==CMD_NONE) { + assert(lptr!=term_expr); + substallpatterns(pline,sLINEMAX); + lptr=pline; /* reset "line pointer" to start of the parsing buffer */ + } /* if */ + #endif + if (sc_status==statFIRST && sc_listing && freading + && (iscommand==CMD_NONE || iscommand==CMD_EMPTYLINE || iscommand==CMD_DIRECTIVE)) + { + listline++; + if (fline!=listline) { + listline=fline; + setlinedirect(fline); + } /* if */ + if (iscommand==CMD_EMPTYLINE) + pc_writeasm(outf,"\n"); + else + pc_writeasm(outf,(char*)pline); + } /* if */ + } while (iscommand!=CMD_NONE && iscommand!=CMD_TERM && freading); /* enddo */ +} + +static const unsigned char *unpackedstring(const unsigned char *lptr,int flags) +{ + while (*lptr!='\"' && *lptr!='\0') { + if (*lptr=='\a') { /* ignore '\a' (which was inserted at a line concatenation) */ + lptr++; + continue; + } /* if */ + litadd(litchar(&lptr,flags | UTF8MODE)); /* litchar() alters "lptr" */ + } /* while */ + litadd(0); /* terminate string */ + return lptr; +} + +static const unsigned char *packedstring(const unsigned char *lptr,int flags) +{ + int i; + ucell val,c; + + i=sizeof(ucell)-(sCHARBITS/8); /* start at most significant byte */ + val=0; + while (*lptr!='\"' && *lptr!='\0') { + if (*lptr=='\a') { /* ignore '\a' (which was inserted at a line concatenation) */ + lptr++; + continue; + } /* if */ + c=litchar(&lptr,flags); /* litchar() alters "lptr" */ + if (c>=(ucell)(1 << sCHARBITS)) + error(43); /* character constant exceeds range */ + val |= (c << 8*i); + if (i==0) { + litadd(val); + val=0; + } /* if */ + i=(i+sizeof(ucell)-(sCHARBITS/8)) % sizeof(ucell); + } /* if */ + /* save last code; make sure there is at least one terminating zero character */ + if (i!=(int)(sizeof(ucell)-(sCHARBITS/8))) + litadd(val); /* at least one zero character in "val" */ + else + litadd(0); /* add full cell of zeros */ + return lptr; +} + +/* lex(lexvalue,lexsym) Lexical Analysis + * + * lex() first deletes leading white space, then checks for multi-character + * operators, keywords (including most compiler directives), numbers, + * labels, symbols and literals (literal characters are converted to a number + * and are returned as such). If every check fails, the line must contain + * a single-character operator. So, lex() returns this character. In the other + * case (something did match), lex() returns the number of the token. All + * these tokens have been assigned numbers above 255. + * + * Some tokens have "attributes": + * tNUMBER the value of the number is return in "lexvalue". + * tRATIONAL the value is in IEEE 754 encoding or in fixed point + * encoding in "lexvalue". + * tSYMBOL the first sNAMEMAX characters of the symbol are + * stored in a buffer, a pointer to this buffer is + * returned in "lexsym". + * tLABEL the first sNAMEMAX characters of the label are + * stored in a buffer, a pointer to this buffer is + * returned in "lexsym". + * tSTRING the string is stored in the literal pool, the index + * in the literal pool to this string is stored in + * "lexvalue". + * + * lex() stores all information (the token found and possibly its attribute) + * in global variables. This allows a token to be examined twice. If "_pushed" + * is true, this information is returned. + * + * Global references: lptr (altered) + * fline (referred to only) + * litidx (referred to only) + * _lextok, _lexval, _lexstr + * _pushed + */ + +static int _pushed; +static int _lextok; +static cell _lexval; +static char _lexstr[sLINEMAX+1]; +static int _lexnewline; + +SC_FUNC void lexinit(void) +{ + stkidx=0; /* index for pushstk() and popstk() */ + iflevel=0; /* preprocessor: nesting of "#if" is currently 0 */ + skiplevel=0; /* preprocessor: not currently skipping */ + icomment=0; /* currently not in a multiline comment */ + _pushed=FALSE; /* no token pushed back into lex */ + _lexnewline=FALSE; +} + +char *sc_tokens[] = { + "*=", "/=", "%=", "+=", "-=", "<<=", ">>>=", ">>=", "&=", "^=", "|=", + "||", "&&", "==", "!=", "<=", ">=", "<<", ">>>", ">>", "++", "--", + "...", "..", "::", + "assert", "break", "case", "char", "const", "continue", "default", + "defined", "do", "else", "enum", "exit", "for", "forward", "goto", + "if", "native", "new", "operator", "public", "return", "sizeof", + "sleep", "state", "static", "stock", "switch", "tagof", "while", + "#assert", "#define", "#else", "#elseif", "#emit", "#endif", "#endinput", + "#endscript", "#error", "#file", "#if", "#include", "#line", "#pragma", + "#tryinclude", "#undef", + ";", ";", "-integer value-", "-rational value-", "-identifier-", + "-label-", "-string-" + }; + +SC_FUNC int lex(cell *lexvalue,char **lexsym) +{ + int i,toolong,newline,stringflags; + char **tokptr; + const unsigned char *starttoken; + + if (_pushed) { + _pushed=FALSE; /* reset "_pushed" flag */ + *lexvalue=_lexval; + *lexsym=_lexstr; + return _lextok; + } /* if */ + + _lextok=0; /* preset all values */ + _lexval=0; + _lexstr[0]='\0'; + *lexvalue=_lexval; + *lexsym=_lexstr; + _lexnewline=FALSE; + if (!freading) + return 0; + + newline= (lptr==pline); /* does lptr point to start of line buffer */ + while (*lptr<=' ') { /* delete leading white space */ + if (*lptr=='\0') { + preprocess(); /* preprocess resets "lptr" */ + if (!freading) + return 0; + if (lptr==term_expr) /* special sequence to terminate a pending expression */ + return (_lextok=tENDEXPR); + _lexnewline=TRUE; /* set this after preprocess(), because + * preprocess() calls lex() recursively */ + newline=TRUE; + } else { + lptr+=1; + } /* if */ + } /* while */ + if (newline) { + stmtindent=0; + for (i=0; i<(int)(lptr-pline); i++) + if (pline[i]=='\t' && sc_tabsize>0) + stmtindent += (int)(sc_tabsize - (stmtindent+sc_tabsize) % sc_tabsize); + else + stmtindent++; + } /* if */ + + i=tFIRST; + tokptr=sc_tokens; + while (i<=tMIDDLE) { /* match multi-character operators */ + if (*lptr==**tokptr && match(*tokptr,FALSE)) { + _lextok=i; + if (pc_docexpr) /* optionally concatenate to documentation string */ + insert_autolist(*tokptr); + return _lextok; + } /* if */ + i+=1; + tokptr+=1; + } /* while */ + while (i<=tLAST) { /* match reserved words and compiler directives */ + if (*lptr==**tokptr && match(*tokptr,TRUE)) { + _lextok=i; + errorset(sRESET); /* reset error flag (clear the "panic mode")*/ + if (pc_docexpr) /* optionally concatenate to documentation string */ + insert_autolist(*tokptr); + return _lextok; + } /* if */ + i+=1; + tokptr+=1; + } /* while */ + + starttoken=lptr; /* save start pointer (for concatenating to documentation string) */ + if ((i=number(&_lexval,lptr))!=0) { /* number */ + _lextok=tNUMBER; + *lexvalue=_lexval; + lptr+=i; + } else if ((i=ftoi(&_lexval,lptr))!=0) { + _lextok=tRATIONAL; + *lexvalue=_lexval; + lptr+=i; + } else if (alpha(*lptr)) { /* symbol or label */ + /* Note: only sNAMEMAX characters are significant. The compiler + * generates a warning if a symbol exceeds this length. + */ + _lextok=tSYMBOL; + i=0; + toolong=0; + while (alphanum(*lptr)){ + _lexstr[i]=*lptr; + lptr+=1; + if (i=litmax) { + cell *p; + + litmax+=sDEF_LITMAX; + p=(cell *)realloc(litq,litmax*sizeof(cell)); + if (p==NULL) + error(102,"literal table"); /* literal table overflow (fatal error) */ + litq=p; + } /* if */ +} + +/* litadd + * + * Adds a value at the end of the literal queue. The literal queue is used + * for literal strings used in functions and for initializing array variables. + * + * Global references: litidx (altered) + * litq (altered) + */ +SC_FUNC void litadd(cell value) +{ + chk_grow_litq(); + assert(litidx=0 && pos<=litidx); + memmove(litq+(pos+1),litq+pos,(litidx-pos)*sizeof(cell)); + litidx++; + litq[pos]=value; +} + +/* litchar + * + * Return current literal character and increase the pointer to point + * just behind this literal character. + * + * Note: standard "escape sequences" are suported, but the backslash may be + * replaced by another character; the syntax '\ddd' is supported, + * but ddd must be decimal! + */ +static cell litchar(const unsigned char **lptr,int flags) +{ + cell c=0; + const unsigned char *cptr; + + cptr=*lptr; + if ((flags & RAWMODE)!=0 || *cptr!=sc_ctrlchar) { /* no escape character */ + #if !defined NO_UTF8 + if (sc_is_utf8 && (flags & UTF8MODE)!=0) { + c=get_utf8_char(cptr,&cptr); + assert(c>=0); /* file was already scanned for conformance to UTF-8 */ + } else { + #endif + #if !defined NO_CODEPAGE + c=cp_translate(cptr,&cptr); + #else + c=*cptr; + cptr+=1; + #endif + #if !defined NO_UTF8 + } /* if */ + #endif + } else { + cptr+=1; + if (*cptr==sc_ctrlchar) { + c=*cptr; /* \\ == \ (the escape character itself) */ + cptr+=1; + } else { + switch (*cptr) { + case 'a': /* \a == audible alarm */ + c=7; + cptr+=1; + break; + case 'b': /* \b == backspace */ + c=8; + cptr+=1; + break; + case 'e': /* \e == escape */ + c=27; + cptr+=1; + break; + case 'f': /* \f == form feed */ + c=12; + cptr+=1; + break; + case 'n': /* \n == NewLine character */ + c=10; + cptr+=1; + break; + case 'r': /* \r == carriage return */ + c=13; + cptr+=1; + break; + case 't': /* \t == horizontal TAB */ + c=9; + cptr+=1; + break; + case 'v': /* \v == vertical TAB */ + c=11; + cptr+=1; + break; + case 'x': + cptr+=1; + c=0; + while (ishex(*cptr)) { + if (isdigit(*cptr)) + c=(c<<4)+(*cptr-'0'); + else + c=(c<<4)+(tolower(*cptr)-'a'+10); + cptr++; + } /* while */ + if (*cptr==';') + cptr++; /* swallow a trailing ';' */ + break; + case '\'': /* \' == ' (single quote) */ + case '"': /* \" == " (single quote) */ + case '%': /* \% == % (percent) */ + c=*cptr; + cptr+=1; + break; + default: + if (isdigit(*cptr)) { /* \ddd */ + c=0; + while (*cptr>='0' && *cptr<='9') /* decimal! */ + c=c*10 + *cptr++ - '0'; + if (*cptr==';') + cptr++; /* swallow a trailing ';' */ + } else { + error(27); /* invalid character constant */ + } /* if */ + } /* switch */ + } /* if */ + } /* if */ + *lptr=cptr; + assert(c>=0); + return c; +} + +/* alpha + * + * Test if character "c" is alphabetic ("a".."z"), an underscore ("_") + * or an "at" sign ("@"). The "@" is an extension to standard C. + */ +static int alpha(char c) +{ + return (isalpha(c) || c=='_' || c==PUBLIC_CHAR); +} + +/* alphanum + * + * Test if character "c" is alphanumeric ("a".."z", "0".."9", "_" or "@") + */ +SC_FUNC int alphanum(char c) +{ + return (alpha(c) || isdigit(c)); +} + +/* ishex + * + * Test if character "c" is a hexadecimal digit ("0".."9" or "a".."f"). + */ +SC_FUNC int ishex(char c) +{ + return (c>='0' && c<='9') || (c>='a' && c<='f') || (c>='A' && c<='F'); +} + +/* The local variable table must be searched backwards, so that the deepest + * nesting of local variables is searched first. The simplest way to do + * this is to insert all new items at the head of the list. + * In the global list, the symbols are kept in sorted order, so that the + * public functions are written in sorted order. + */ +static symbol *add_symbol(symbol *root,symbol *entry,int sort) +{ + symbol *newsym; + + if (sort) + while (root->next!=NULL && strcmp(entry->name,root->next->name)>0) + root=root->next; + + if ((newsym=(symbol *)malloc(sizeof(symbol)))==NULL) { + error(103); + return NULL; + } /* if */ + memcpy(newsym,entry,sizeof(symbol)); + newsym->next=root->next; + root->next=newsym; + return newsym; +} + +static void free_symbol(symbol *sym) +{ + arginfo *arg; + + /* free all sub-symbol allocated memory blocks, depending on the + * kind of the symbol + */ + assert(sym!=NULL); + if (sym->ident==iFUNCTN) { + /* run through the argument list; "default array" arguments + * must be freed explicitly; the tag list must also be freed */ + assert(sym->dim.arglist!=NULL); + for (arg=sym->dim.arglist; arg->ident!=0; arg++) { + if (arg->ident==iREFARRAY && arg->hasdefault) + free(arg->defvalue.array.data); + else if (arg->ident==iVARIABLE + && ((arg->hasdefault & uSIZEOF)!=0 || (arg->hasdefault & uTAGOF)!=0)) + free(arg->defvalue.size.symname); + assert(arg->tags!=NULL); + free(arg->tags); + } /* for */ + free(sym->dim.arglist); + if (sym->states!=NULL) { + delete_consttable(sym->states); + free(sym->states); + } /* if */ + } else if (sym->ident==iCONSTEXPR && (sym->usage & uENUMROOT)==uENUMROOT) { + /* free the constant list of an enum root */ + assert(sym->dim.enumlist!=NULL); + delete_consttable(sym->dim.enumlist); + free(sym->dim.enumlist); + } /* if */ + assert(sym->refer!=NULL); + free(sym->refer); + if (sym->documentation!=NULL) + free(sym->documentation); + free(sym); +} + +SC_FUNC void delete_symbol(symbol *root,symbol *sym) +{ + /* find the symbol and its predecessor + * (this function assumes that you will never delete a symbol that is not + * in the table pointed at by "root") + */ + assert(root!=sym); + while (root->next!=sym) { + root=root->next; + assert(root!=NULL); + } /* while */ + + /* unlink it, then free it */ + root->next=sym->next; + free_symbol(sym); +} + +SC_FUNC void delete_symbols(symbol *root,int level,int delete_labels,int delete_functions) +{ + symbol *sym,*parent_sym; + constvalue *stateptr; + int mustdelete; + + /* erase only the symbols with a deeper nesting level than the + * specified nesting level */ + while (root->next!=NULL) { + sym=root->next; + if (sym->compoundident) { + case iLABEL: + mustdelete=delete_labels; + break; + case iVARIABLE: + case iARRAY: + /* do not delete global variables if functions are preserved */ + mustdelete=delete_functions; + break; + case iREFERENCE: + /* always delete references (only exist as function parameters) */ + mustdelete=TRUE; + break; + case iREFARRAY: + /* a global iREFARRAY symbol is the return value of a function: delete + * this only if "globals" must be deleted; other iREFARRAY instances + * (locals) are also deleted + */ + mustdelete=delete_functions; + for (parent_sym=sym->parent; parent_sym!=NULL && parent_sym->ident!=iFUNCTN; parent_sym=parent_sym->parent) + assert(parent_sym->ident==iREFARRAY); + assert(parent_sym==NULL || (parent_sym->ident==iFUNCTN && parent_sym->parent==NULL)); + if (parent_sym==NULL || parent_sym->ident!=iFUNCTN) + mustdelete=TRUE; + break; + case iCONSTEXPR: + /* delete constants, except predefined constants */ + mustdelete=delete_functions || (sym->usage & uPREDEF)==0; + break; + case iFUNCTN: + /* optionally preserve globals (variables & functions), but NOT native functions */ + mustdelete=delete_functions || (sym->usage & uNATIVE)!=0; + assert(sym->parent==NULL); + break; + case iARRAYCELL: + case iARRAYCHAR: + case iEXPRESSION: + case iVARARGS: + default: + assert(0); + break; + } /* switch */ + if (mustdelete) { + root->next=sym->next; + free_symbol(sym); + } else { + /* if the function was prototyped, but not implemented in this source, + * mark it as such, so that its use can be flagged + */ + if (sym->ident==iFUNCTN && (sym->usage & uDEFINE)==0) + sym->usage |= uMISSING; + if (sym->ident==iFUNCTN || sym->ident==iVARIABLE || sym->ident==iARRAY) + sym->usage &= ~uDEFINE; /* clear "defined" flag */ + /* set all states as "undefined" too */ + if (sym->states!=NULL) + for (stateptr=sym->states->next; stateptr!=NULL; stateptr=stateptr->next) + stateptr->value=0; + /* for user defined operators, also remove the "prototyped" flag, as + * user-defined operators *must* be declared before use + */ + if (sym->ident==iFUNCTN && !isalpha(*sym->name) && *sym->name!='_' && *sym->name!=PUBLIC_CHAR) + sym->usage &= ~uPROTOTYPED; + root=sym; /* skip the symbol */ + } /* if */ + } /* if */ +} + +/* The purpose of the hash is to reduce the frequency of a "name" + * comparison (which is costly). There is little interest in avoiding + * clusters in similar names, which is why this function is plain simple. + */ +SC_FUNC uint32_t namehash(const char *name) +{ + const unsigned char *ptr=(const unsigned char *)name; + int len=strlen(name); + if (len==0) + return 0L; + assert(len<256); + return (len<<24Lu) + (ptr[0]<<16Lu) + (ptr[len-1]<<8Lu) + (ptr[len>>1Lu]); +} + +static symbol *find_symbol(const symbol *root,const char *name,int fnumber,int includechildren) +{ + symbol *ptr=root->next; + unsigned long hash=namehash(name); + while (ptr!=NULL) { + if (hash==ptr->hash && strcmp(name,ptr->name)==0 + && (ptr->parent==NULL || includechildren) + && (ptr->fnumber<0 || ptr->fnumber==fnumber)) + return ptr; + ptr=ptr->next; + } /* while */ + return NULL; +} + +static symbol *find_symbol_child(const symbol *root,const symbol *sym) +{ + symbol *ptr=root->next; + while (ptr!=NULL) { + if (ptr->parent==sym) + return ptr; + ptr=ptr->next; + } /* while */ + return NULL; +} + +/* Adds "bywhom" to the list of referrers of "entry". Typically, + * bywhom will be the function that uses a variable or that calls + * the function. + */ +SC_FUNC int refer_symbol(symbol *entry,symbol *bywhom) +{ + int count; + + assert(bywhom!=NULL); /* it makes no sense to add a "void" referrer */ + assert(entry!=NULL); + assert(entry->refer!=NULL); + + /* see if it is already there */ + for (count=0; countnumrefers && entry->refer[count]!=bywhom; count++) + /* nothing */; + if (countnumrefers) { + assert(entry->refer[count]==bywhom); + return TRUE; + } /* if */ + + /* see if there is an empty spot in the referrer list */ + for (count=0; countnumrefers && entry->refer[count]!=NULL; count++) + /* nothing */; + assert(count <= entry->numrefers); + if (count==entry->numrefers) { + symbol **refer; + int newsize=2*entry->numrefers; + assert(newsize>0); + /* grow the referrer list */ + refer=(symbol**)realloc(entry->refer,newsize*sizeof(symbol*)); + if (refer==NULL) + return FALSE; /* insufficient memory */ + /* initialize the new entries */ + entry->refer=refer; + for (count=entry->numrefers; countrefer[count]=NULL; + count=entry->numrefers; /* first empty spot */ + entry->numrefers=newsize; + } /* if */ + + /* add the referrer */ + assert(entry->refer[count]==NULL); + entry->refer[count]=bywhom; + return TRUE; +} + +SC_FUNC void markusage(symbol *sym,int usage) +{ + sym->usage |= (char)usage; + /* check if (global) reference must be added to the symbol */ + if ((usage & (uREAD | uWRITTEN))!=0) { + /* only do this for global symbols */ + if (sym->vclass==sGLOBAL) { + /* "curfunc" should always be valid, since statements may not occurs + * outside functions; in the case of syntax errors, however, the + * compiler may arrive through this function + */ + if (curfunc!=NULL) + refer_symbol(sym,curfunc); + } /* if */ + } /* if */ +} + + +/* findglb + * + * Returns a pointer to the global symbol (if found) or NULL (if not found) + */ +SC_FUNC symbol *findglb(const char *name) +{ + return find_symbol(&glbtab,name,fcurrent,FALSE); +} + +/* findloc + * + * Returns a pointer to the local symbol (if found) or NULL (if not found). + * See add_symbol() how the deepest nesting level is searched first. + */ +SC_FUNC symbol *findloc(const char *name) +{ + return find_symbol(&loctab,name,-1,FALSE); +} + +SC_FUNC symbol *findconst(const char *name) +{ + symbol *sym; + + sym=find_symbol(&loctab,name,-1,TRUE); /* try local symbols first */ + if (sym==NULL || sym->ident!=iCONSTEXPR) /* not found, or not a constant */ + sym=find_symbol(&glbtab,name,fcurrent,TRUE); + if (sym==NULL || sym->ident!=iCONSTEXPR) + return NULL; + assert(sym->parent==NULL || (sym->usage & uENUMFIELD)!=0); + /* ^^^ constants have no hierarchy, but enumeration fields may have a parent */ + return sym; +} + +SC_FUNC symbol *finddepend(const symbol *parent) +{ + symbol *sym; + + sym=find_symbol_child(&loctab,parent); /* try local symbols first */ + if (sym==NULL) /* not found */ + sym=find_symbol_child(&glbtab,parent); + return sym; +} + +/* addsym + * + * Adds a symbol to the symbol table (either global or local variables, + * or global and local constants). + */ +SC_FUNC symbol *addsym(const char *name,cell addr,int ident,int vclass,int tag,int usage) +{ + symbol entry, **refer; + + /* global variables/constants/functions may only be defined once */ + assert(!(ident==iFUNCTN || ident==iCONSTEXPR) || vclass!=sGLOBAL || findglb(name)==NULL); + /* labels may only be defined once */ + assert(ident!=iLABEL || findloc(name)==NULL); + + /* create an empty referrer list */ + if ((refer=(symbol**)malloc(sizeof(symbol*)))==NULL) { + error(103); /* insufficient memory */ + return NULL; + } /* if */ + *refer=NULL; + + /* first fill in the entry */ + strcpy(entry.name,name); + entry.hash=namehash(name); + entry.addr=addr; + entry.codeaddr=code_idx; + entry.vclass=(char)vclass; + entry.ident=(char)ident; + entry.tag=tag; + entry.usage=(char)usage; + entry.compound=0; /* may be overridden later */ + entry.states=NULL; + entry.fnumber=-1; /* assume global visibility (ignored for local symbols) */ + entry.numrefers=1; + entry.refer=refer; + entry.parent=NULL; + entry.fieldtag=0; + entry.documentation=NULL; + + /* then insert it in the list */ + if (vclass==sGLOBAL) + return add_symbol(&glbtab,&entry,TRUE); + else + return add_symbol(&loctab,&entry,FALSE); +} + +SC_FUNC symbol *addvariable(const char *name,cell addr,int ident,int vclass,int tag, + int dim[],int numdim,int idxtag[]) +{ + symbol *sym; + + /* global variables may only be defined once + * One complication is that functions returning arrays declare an array + * with the same name as the function, so the assertion must allow for + * this special case. + */ + assert(vclass!=sGLOBAL || (sym=findglb(name))==NULL || (sym->usage & uDEFINE)==0 + || sym->ident==iFUNCTN && sym==curfunc); + + if (ident==iARRAY || ident==iREFARRAY) { + symbol *parent=NULL,*top; + int level; + sym=NULL; /* to avoid a compiler warning */ + for (level=0; leveldim.array.length=dim[level]; + top->dim.array.level=(short)(numdim-level-1); + top->x.idxtag=idxtag[level]; + top->parent=parent; + parent=top; + if (level==0) + sym=top; + } /* for */ + } else { + sym=addsym(name,addr,ident,vclass,tag,uDEFINE); + } /* if */ + return sym; +} + +/* getlabel + * + * Returns te next internal label number. The global variable sc_labnum is + * initialized to zero. + */ +SC_FUNC int getlabel(void) +{ + return sc_labnum++; +} + +/* itoh + * + * Converts a number to a hexadecimal string and returns a pointer to that + * string. This function is NOT re-entrant. + */ +SC_FUNC char *itoh(ucell val) +{ +static char itohstr[30]; + char *ptr; + int i,nibble[16]; /* a 64-bit hexadecimal cell has 16 nibbles */ + int max; + + #if PAWN_CELL_SIZE==16 + max=4; + #elif PAWN_CELL_SIZE==32 + max=8; + #elif PAWN_CELL_SIZE==64 + max=16; + #else + #error Unsupported cell size + #endif + ptr=itohstr; + for (i=0; i>=4; + } /* endfor */ + i=max-1; + while (nibble[i]==0 && i>0) /* search for highest non-zero nibble */ + i-=1; + while (i>=0){ + if (nibble[i]>=10) + *ptr++=(char)('a'+(nibble[i]-10)); + else + *ptr++=(char)('0'+nibble[i]); + i-=1; + } /* while */ + *ptr='\0'; /* and a zero-terminator */ + return itohstr; +} + diff --git a/compiler/libpc300/sc3.c b/compiler/libpc300/sc3.c new file mode 100755 index 00000000..dbc28e44 --- /dev/null +++ b/compiler/libpc300/sc3.c @@ -0,0 +1,2338 @@ +/* Pawn compiler - Recursive descend expresion parser + * + * Copyright (c) ITB CompuPhase, 1997-2005 + * + * This software is provided "as-is", without any express or implied warranty. + * In no event will the authors be held liable for any damages arising from + * the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software in + * a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * 3. This notice may not be removed or altered from any source distribution. + * + * Version: $Id$ + */ +#include +#include +#include /* for _MAX_PATH */ +#include +#if defined FORTIFY + #include "fortify.h" +#endif +#include "sc.h" + +static int skim(int *opstr,void (*testfunc)(int),int dropval,int endval, + int (*hier)(value*),value *lval); +static void dropout(int lvalue,void (*testfunc)(int val),int exit1,value *lval); +static int plnge(int *opstr,int opoff,int (*hier)(value *lval),value *lval, + char *forcetag,int chkbitwise); +static int plnge1(int (*hier)(value *lval),value *lval); +static void plnge2(void (*oper)(void), + int (*hier)(value *lval), + value *lval1,value *lval2); +static cell calc(cell left,void (*oper)(),cell right,char *boolresult); +static int hier14(value *lval); +static int hier13(value *lval); +static int hier12(value *lval); +static int hier11(value *lval); +static int hier10(value *lval); +static int hier9(value *lval); +static int hier8(value *lval); +static int hier7(value *lval); +static int hier6(value *lval); +static int hier5(value *lval); +static int hier4(value *lval); +static int hier3(value *lval); +static int hier2(value *lval); +static int hier1(value *lval1); +static int primary(value *lval); +static void clear_value(value *lval); +static void callfunction(symbol *sym,value *lval_result,int matchparanthesis); +static int dbltest(void (*oper)(),value *lval1,value *lval2); +static int commutative(void (*oper)()); +static int constant(value *lval); + +static char lastsymbol[sNAMEMAX+1]; /* name of last function/variable */ +static int bitwise_opercount; /* count of bitwise operators in an expression */ +static int decl_heap=0; + +/* Function addresses of binary operators for signed operations */ +static void (*op1[17])(void) = { + os_mult,os_div,os_mod, /* hier3, index 0 */ + ob_add,ob_sub, /* hier4, index 3 */ + ob_sal,os_sar,ou_sar, /* hier5, index 5 */ + ob_and, /* hier6, index 8 */ + ob_xor, /* hier7, index 9 */ + ob_or, /* hier8, index 10 */ + os_le,os_ge,os_lt,os_gt, /* hier9, index 11 */ + ob_eq,ob_ne, /* hier10, index 15 */ +}; +/* These two functions are defined because the functions inc() and dec() in + * SC4.C have a different prototype than the other code generation functions. + * The arrays for user-defined functions use the function pointers for + * identifying what kind of operation is requested; these functions must all + * have the same prototype. As inc() and dec() are special cases already, it + * is simplest to add two "do-nothing" functions. + */ +static void user_inc(void) {} +static void user_dec(void) {} + +/* + * Searches for a binary operator a list of operators. The list is stored in + * the array "list". The last entry in the list should be set to 0. + * + * The index of an operator in "list" (if found) is returned in "opidx". If + * no operator is found, nextop() returns 0. + * + * If an operator is found in the expression, it cannot be used in a function + * call with omitted parantheses. Mark this... + * + * Global references: sc_allowproccall (modified) + */ +static int nextop(int *opidx,int *list) +{ + *opidx=0; + while (*list){ + if (matchtoken(*list)){ + sc_allowproccall=FALSE; + return TRUE; /* found! */ + } else { + list+=1; + *opidx+=1; + } /* if */ + } /* while */ + return FALSE; /* entire list scanned, nothing found */ +} + +SC_FUNC int check_userop(void (*oper)(void),int tag1,int tag2,int numparam, + value *lval,int *resulttag) +{ +static char *binoperstr[] = { "*", "/", "%", "+", "-", "", "", "", + "", "", "", "<=", ">=", "<", ">", "==", "!=" }; +static int binoper_savepri[] = { FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, + FALSE, FALSE, FALSE, FALSE, FALSE, + TRUE, TRUE, TRUE, TRUE, FALSE, FALSE }; +static char *unoperstr[] = { "!", "-", "++", "--" }; +static void (*unopers[])(void) = { lneg, neg, user_inc, user_dec }; + char opername[4] = "", symbolname[sNAMEMAX+1]; + int i,swapparams,savepri,savealt; + int paramspassed; + symbol *sym; + + /* since user-defined operators on untagged operands are forbidden, we have + * a quick exit. + */ + assert(numparam==1 || numparam==2); + if (tag1==0 && (numparam==1 || tag2==0)) + return FALSE; + + savepri=savealt=FALSE; + /* find the name with the operator */ + if (numparam==2) { + if (oper==NULL) { + /* assignment operator: a special case */ + strcpy(opername,"="); + if (lval!=NULL && (lval->ident==iARRAYCELL || lval->ident==iARRAYCHAR)) + savealt=TRUE; + } else { + assert( (sizeof binoperstr / sizeof binoperstr[0]) == (sizeof op1 / sizeof op1[0]) ); + for (i=0; iusage & uDEFINE)==0*/) { /* ??? should not check uDEFINE; first pass clears these bits */ + /* check for commutative operators */ + if (tag1==tag2 || oper==NULL || !commutative(oper)) + return FALSE; /* not commutative, cannot swap operands */ + /* if arrived here, the operator is commutative and the tags are different, + * swap tags and try again + */ + assert(numparam==2); /* commutative operator must be a binary operator */ + operator_symname(symbolname,opername,tag2,tag1,numparam,tag1); + swapparams=TRUE; + sym=findglb(symbolname); + if (sym==NULL /*|| (sym->usage & uDEFINE)==0*/) + return FALSE; + } /* if */ + + /* check existance and the proper declaration of this function */ + if ((sym->usage & uMISSING)!=0 || (sym->usage & uPROTOTYPED)==0) { + char symname[2*sNAMEMAX+16]; /* allow space for user defined operators */ + funcdisplayname(symname,sym->name); + if ((sym->usage & uMISSING)!=0) + error(4,symname); /* function not defined */ + if ((sym->usage & uPROTOTYPED)==0) + error(71,symname); /* operator must be declared before use */ + } /* if */ + + /* we don't want to use the redefined operator in the function that + * redefines the operator itself, otherwise the snippet below gives + * an unexpected recursion: + * fixed:operator+(fixed:a, fixed:b) + * return a + b + */ + if (sym==curfunc) + return FALSE; + + /* for increment and decrement operators, the symbol must first be loaded + * (and stored back afterwards) + */ + if (oper==user_inc || oper==user_dec) { + assert(!savepri); + assert(lval!=NULL); + if (lval->ident==iARRAYCELL || lval->ident==iARRAYCHAR) + pushreg(sPRI); /* save current address in PRI */ + rvalue(lval); /* get the symbol's value in PRI */ + } /* if */ + + assert(!savepri || !savealt); /* either one MAY be set, but not both */ + if (savepri) { + /* the chained comparison operators require that the ALT register is + * unmodified, so we save it here; actually, we save PRI because the normal + * instruction sequence (without user operator) swaps PRI and ALT + */ + pushreg(sPRI); /* right-hand operand is in PRI */ + } else if (savealt) { + /* for the assignment operator, ALT may contain an address at which the + * result must be stored; this address must be preserved accross the + * call + */ + assert(lval!=NULL); /* this was checked earlier */ + assert(lval->ident==iARRAYCELL || lval->ident==iARRAYCHAR); /* checked earlier */ + pushreg(sALT); + } /* if */ + + /* push parameters, call the function */ + paramspassed= (oper==NULL) ? 1 : numparam; + switch (paramspassed) { + case 1: + pushreg(sPRI); + break; + case 2: + /* note that 1) a function expects that the parameters are pushed + * in reversed order, and 2) the left operand is in the secondary register + * and the right operand is in the primary register */ + if (swapparams) { + pushreg(sALT); + pushreg(sPRI); + } else { + pushreg(sPRI); + pushreg(sALT); + } /* if */ + break; + default: + assert(0); + } /* switch */ + markexpr(sPARM,NULL,0); /* mark the end of a sub-expression */ + pushval((cell)paramspassed*sizeof(cell)); + assert(sym->ident==iFUNCTN); + ffcall(sym,NULL,paramspassed); + if (sc_status!=statSKIP) + markusage(sym,uREAD); /* do not mark as "used" when this call itself is skipped */ + if ((sym->usage & uNATIVE)!=0 && sym->x.lib!=NULL) + sym->x.lib->value += 1; /* increment "usage count" of the library */ + sideeffect=TRUE; /* assume functions carry out a side-effect */ + assert(resulttag!=NULL); + *resulttag=sym->tag; /* save tag of the called function */ + + if (savepri || savealt) + popreg(sALT); /* restore the saved PRI/ALT that into ALT */ + if (oper==user_inc || oper==user_dec) { + assert(lval!=NULL); + if (lval->ident==iARRAYCELL || lval->ident==iARRAYCHAR) + popreg(sALT); /* restore address (in ALT) */ + store(lval); /* store PRI in the symbol */ + moveto1(); /* make sure PRI is restored on exit */ + } /* if */ + return TRUE; +} + +SC_FUNC int matchtag(int formaltag,int actualtag,int allowcoerce) +{ + if (formaltag!=actualtag) { + /* if the formal tag is zero and the actual tag is not "fixed", the actual + * tag is "coerced" to zero + */ + if (!allowcoerce || formaltag!=0 || (actualtag & FIXEDTAG)!=0) + return FALSE; + } /* if */ + return TRUE; +} + +/* + * The AMX pseudo-processor has no direct support for logical (boolean) + * operations. These have to be done via comparing and jumping. Since we are + * already jumping through the code, we might as well implement an "early + * drop-out" evaluation (also called "short-circuit"). This conforms to + * standard C: + * + * expr1 || expr2 expr2 will only be evaluated if expr1 is false. + * expr1 && expr2 expr2 will only be evaluated if expr1 is true. + * + * expr1 || expr2 && expr3 expr2 will only be evaluated if expr1 is false + * and expr3 will only be evaluated if expr1 is + * false and expr2 is true. + * + * Code generation for the last example proceeds thus: + * + * evaluate expr1 + * operator || found + * jump to "l1" if result of expr1 not equal to 0 + * evaluate expr2 + * -> operator && found; skip to higher level in hierarchy diagram + * jump to "l2" if result of expr2 equal to 0 + * evaluate expr3 + * jump to "l2" if result of expr3 equal to 0 + * set expression result to 1 (true) + * jump to "l3" + * l2: set expression result to 0 (false) + * l3: + * <- drop back to previous hierarchy level + * jump to "l1" if result of expr2 && expr3 not equal to 0 + * set expression result to 0 (false) + * jump to "l4" + * l1: set expression result to 1 (true) + * l4: + * + */ + +/* Skim over terms adjoining || and && operators + * dropval The value of the expression after "dropping out". An "or" drops + * out when the left hand is TRUE, so dropval must be 1 on "or" + * expressions. + * endval The value of the expression when no expression drops out. In an + * "or" expression, this happens when both the left hand and the + * right hand are FALSE, so endval must be 0 for "or" expressions. + */ +static int skim(int *opstr,void (*testfunc)(int),int dropval,int endval, + int (*hier)(value*),value *lval) +{ + int lvalue,hits,droplab,endlab,opidx; + int allconst; + cell constval; + int index; + cell cidx; + + stgget(&index,&cidx); /* mark position in code generator */ + hits=FALSE; /* no logical operators "hit" yet */ + allconst=TRUE; /* assume all values "const" */ + constval=0; + droplab=0; /* to avoid a compiler warning */ + for ( ;; ) { + lvalue=plnge1(hier,lval); /* evaluate left expression */ + + allconst= allconst && (lval->ident==iCONSTEXPR); + if (allconst) { + if (hits) { + /* one operator was already found */ + if (testfunc==jmp_ne0) + lval->constval= lval->constval || constval; + else + lval->constval= lval->constval && constval; + } /* if */ + constval=lval->constval; /* save result accumulated so far */ + } /* if */ + + if (nextop(&opidx,opstr)) { + if (!hits) { + /* this is the first operator in the list */ + hits=TRUE; + droplab=getlabel(); + } /* if */ + dropout(lvalue,testfunc,droplab,lval); + } else if (hits) { /* no (more) identical operators */ + dropout(lvalue,testfunc,droplab,lval); /* found at least one operator! */ + ldconst(endval,sPRI); + jumplabel(endlab=getlabel()); + setlabel(droplab); + ldconst(dropval,sPRI); + setlabel(endlab); + lval->sym=NULL; + lval->tag=pc_addtag("bool"); /* force tag to be "bool" */ + if (allconst) { + lval->ident=iCONSTEXPR; + lval->constval=constval; + stgdel(index,cidx); /* scratch generated code and calculate */ + } else { + lval->ident=iEXPRESSION; + lval->constval=0; + } /* if */ + return FALSE; + } else { + return lvalue; /* none of the operators in "opstr" were found */ + } /* if */ + + } /* while */ +} + +/* + * Reads into the primary register the variable pointed to by lval if + * plunging through the hierarchy levels detected an lvalue. Otherwise + * if a constant was detected, it is loaded. If there is no constant and + * no lvalue, the primary register must already contain the expression + * result. + * + * After that, the compare routines "jmp_ne0" or "jmp_eq0" are called, which + * compare the primary register against 0, and jump to the "early drop-out" + * label "exit1" if the condition is true. + */ +static void dropout(int lvalue,void (*testfunc)(int val),int exit1,value *lval) +{ + if (lvalue) + rvalue(lval); + else if (lval->ident==iCONSTEXPR) + ldconst(lval->constval,sPRI); + (*testfunc)(exit1); +} + +static void checkfunction(value *lval) +{ + symbol *sym=lval->sym; + + if (sym==NULL || (sym->ident!=iFUNCTN && sym->ident!=iREFFUNC)) + return; /* no known symbol, or not a function result */ + + if ((sym->usage & uDEFINE)!=0) { + /* function is defined, can now check the return value (but make an + * exception for directly recursive functions) + */ + if (sym!=curfunc && (sym->usage & uRETVALUE)==0) { + char symname[2*sNAMEMAX+16]; /* allow space for user defined operators */ + funcdisplayname(symname,sym->name); + error(209,symname); /* function should return a value */ + } /* if */ + } else { + /* function not yet defined, set */ + sym->usage|=uRETVALUE; /* make sure that a future implementation of + * the function uses "return " */ + } /* if */ +} + +/* + * Plunge to a lower level + */ +static int plnge(int *opstr,int opoff,int (*hier)(value *lval),value *lval, + char *forcetag,int chkbitwise) +{ + int lvalue,opidx; + int count; + value lval2 = {0}; + + lvalue=plnge1(hier,lval); + if (nextop(&opidx,opstr)==0) + return lvalue; /* no operator in "opstr" found */ + if (lvalue) + rvalue(lval); + count=0; + do { + if (chkbitwise && count++>0 && bitwise_opercount!=0) + error(212); + opidx+=opoff; /* add offset to index returned by nextop() */ + plnge2(op1[opidx],hier,lval,&lval2); + if (op1[opidx]==ob_and || op1[opidx]==ob_or) + bitwise_opercount++; + if (forcetag!=NULL) + lval->tag=pc_addtag(forcetag); + } while (nextop(&opidx,opstr)); /* do */ + return FALSE; /* result of expression is not an lvalue */ +} + +/* plnge_rel + * + * Binary plunge to lower level; this is very simular to plnge, but + * it has special code generation sequences for chained operations. + */ +static int plnge_rel(int *opstr,int opoff,int (*hier)(value *lval),value *lval) +{ + int lvalue,opidx; + value lval2 = {0}; /* intialize, to avoid a compiler warning */ + int count; + + /* this function should only be called for relational operators */ + assert(op1[opoff]==os_le); + lvalue=plnge1(hier,lval); + if (nextop(&opidx,opstr)==0) + return lvalue; /* no operator in "opstr" found */ + if (lvalue) + rvalue(lval); + count=0; + lval->boolresult=TRUE; + do { + /* same check as in plnge(), but "chkbitwise" is always TRUE */ + if (count>0 && bitwise_opercount!=0) + error(212); + if (count>0) { + relop_prefix(); + *lval=lval2; /* copy right hand expression of the previous iteration */ + } /* if */ + opidx+=opoff; + plnge2(op1[opidx],hier,lval,&lval2); + if (count++>0) + relop_suffix(); + } while (nextop(&opidx,opstr)); /* enddo */ + lval->constval=lval->boolresult; + lval->tag=pc_addtag("bool"); /* force tag to be "bool" */ + return FALSE; /* result of expression is not an lvalue */ +} + +/* plnge1 + * + * Unary plunge to lower level + * Called by: skim(), plnge(), plnge2(), plnge_rel(), hier14() and hier13() + */ +static int plnge1(int (*hier)(value *lval),value *lval) +{ + int lvalue,index; + cell cidx; + + stgget(&index,&cidx); /* mark position in code generator */ + lvalue=(*hier)(lval); + if (lval->ident==iCONSTEXPR) + stgdel(index,cidx); /* load constant later */ + return lvalue; +} + +/* plnge2 + * + * Binary plunge to lower level + * Called by: plnge(), plnge_rel(), hier14() and hier1() + */ +static void plnge2(void (*oper)(void), + int (*hier)(value *lval), + value *lval1,value *lval2) +{ + int index; + cell cidx; + + stgget(&index,&cidx); /* mark position in code generator */ + if (lval1->ident==iCONSTEXPR) { /* constant on left side; it is not yet loaded */ + if (plnge1(hier,lval2)) + rvalue(lval2); /* load lvalue now */ + else if (lval2->ident==iCONSTEXPR) + ldconst(lval2->constval<constval<ident==iCONSTEXPR) { /* constant on right side */ + if (commutative(oper)) { /* test for commutative operators */ + value lvaltmp = {0}; + stgdel(index,cidx); /* scratch pushreg() and constant fetch (then + * fetch the constant again */ + ldconst(lval2->constval<constval<ident==iARRAY || lval1->ident==iREFARRAY) { + char *ptr=(lval1->sym!=NULL) ? lval1->sym->name : "-unknown-"; + error(33,ptr); /* array must be indexed */ + } else if (lval2->ident==iARRAY || lval2->ident==iREFARRAY) { + char *ptr=(lval2->sym!=NULL) ? lval2->sym->name : "-unknown-"; + error(33,ptr); /* array must be indexed */ + } /* if */ + /* ??? ^^^ should do same kind of error checking with functions */ + + /* check whether an "operator" function is defined for the tag names + * (a constant expression cannot be optimized in that case) + */ + if (check_userop(oper,lval1->tag,lval2->tag,2,NULL,&lval1->tag)) { + lval1->ident=iEXPRESSION; + lval1->constval=0; + } else if (lval1->ident==iCONSTEXPR && lval2->ident==iCONSTEXPR) { + /* only constant expression if both constant */ + stgdel(index,cidx); /* scratch generated code and calculate */ + if (!matchtag(lval1->tag,lval2->tag,FALSE)) + error(213); /* tagname mismatch */ + lval1->constval=calc(lval1->constval,oper,lval2->constval,&lval1->boolresult); + } else { + if (!matchtag(lval1->tag,lval2->tag,FALSE)) + error(213); /* tagname mismatch */ + (*oper)(); /* do the (signed) operation */ + lval1->ident=iEXPRESSION; + } /* if */ + } /* if */ +} + +static cell truemodulus(cell a,cell b) +{ + return (a % b + b) % b; +} + +static cell calc(cell left,void (*oper)(),cell right,char *boolresult) +{ + if (oper==ob_or) + return (left | right); + else if (oper==ob_xor) + return (left ^ right); + else if (oper==ob_and) + return (left & right); + else if (oper==ob_eq) + return (left == right); + else if (oper==ob_ne) + return (left != right); + else if (oper==os_le) + return *boolresult &= (char)(left <= right), right; + else if (oper==os_ge) + return *boolresult &= (char)(left >= right), right; + else if (oper==os_lt) + return *boolresult &= (char)(left < right), right; + else if (oper==os_gt) + return *boolresult &= (char)(left > right), right; + else if (oper==os_sar) + return (left >> (int)right); + else if (oper==ou_sar) + return ((ucell)left >> (ucell)right); + else if (oper==ob_sal) + return ((ucell)left << (int)right); + else if (oper==ob_add) + return (left + right); + else if (oper==ob_sub) + return (left - right); + else if (oper==os_mult) + return (left * right); + else if (oper==os_div) + return (left - truemodulus(left,right)) / right; + else if (oper==os_mod) + return truemodulus(left,right); + else + error(29); /* invalid expression, assumed 0 (this should never occur) */ + return 0; +} + +SC_FUNC int expression(cell *val,int *tag,symbol **symptr,int chkfuncresult) +{ + int locheap=decl_heap; + value lval={0}; + + if (hier14(&lval)) + rvalue(&lval); + /* scrap any arrays left on the heap */ + assert(decl_heap>=locheap); + modheap((locheap-decl_heap)*sizeof(cell)); /* remove heap space, so negative delta */ + decl_heap=locheap; + + if (lval.ident==iCONSTEXPR && val!=NULL) /* constant expression */ + *val=lval.constval; + if (tag!=NULL) + *tag=lval.tag; + if (symptr!=NULL) + *symptr=lval.sym; + if (chkfuncresult) + checkfunction(&lval); + return lval.ident; +} + +static cell array_totalsize(symbol *sym) +{ + cell length; + + assert(sym!=NULL); + assert(sym->ident==iARRAY || sym->ident==iREFARRAY); + length=sym->dim.array.length; + if (sym->dim.array.level > 0) { + cell sublength=array_totalsize(finddepend(sym)); + if (sublength>0) + length=length+length*sublength; + else + length=0; + } /* if */ + return length; +} + +static cell array_levelsize(symbol *sym,int level) +{ + assert(sym!=NULL); + assert(sym->ident==iARRAY || sym->ident==iREFARRAY); + assert(level <= sym->dim.array.level); + while (level-- > 0) { + sym=finddepend(sym); + assert(sym!=NULL); + } /* if */ + return sym->dim.array.length; +} + +/* hier14 + * + * Lowest hierarchy level (except for the , operator). + * + * Global references: sc_intest (reffered to only) + * sc_allowproccall (modified) + */ +static int hier14(value *lval1) +{ + int lvalue; + value lval2 = {0},lval3 = {0}; + void (*oper)(void); + int tok,level,i; + cell val; + char *st; + int bwcount,leftarray; + cell arrayidx1[sDIMEN_MAX],arrayidx2[sDIMEN_MAX]; /* last used array indices */ + cell *org_arrayidx; + + bwcount=bitwise_opercount; + bitwise_opercount=0; + /* initialize the index arrays with unlikely constant indices; note that + * these indices will only be changed when the array is indexed with a + * constant, and that negative array indices are invalid (so actually, any + * negative value would do). + */ + for (i=0; iarrayidx; /* save current pointer, to reset later */ + if (lval1->arrayidx==NULL) + lval1->arrayidx=arrayidx1; + lvalue=plnge1(hier13,lval1); + if (lval1->ident!=iARRAYCELL && lval1->ident!=iARRAYCHAR) + lval1->arrayidx=NULL; + if (lval1->ident==iCONSTEXPR) /* load constant here */ + ldconst(lval1->constval,sPRI); + tok=lex(&val,&st); + switch (tok) { + case taOR: + oper=ob_or; + break; + case taXOR: + oper=ob_xor; + break; + case taAND: + oper=ob_and; + break; + case taADD: + oper=ob_add; + break; + case taSUB: + oper=ob_sub; + break; + case taMULT: + oper=os_mult; + break; + case taDIV: + oper=os_div; + break; + case taMOD: + oper=os_mod; + break; + case taSHRU: + oper=ou_sar; + break; + case taSHR: + oper=os_sar; + break; + case taSHL: + oper=ob_sal; + break; + case '=': /* simple assignment */ + oper=NULL; + if (sc_intest) + error(211); /* possibly unintended assignment */ + break; + default: + lexpush(); + bitwise_opercount=bwcount; + lval1->arrayidx=org_arrayidx; /* restore array index pointer */ + return lvalue; + } /* switch */ + + /* if we get here, it was an assignment; first check a few special cases + * and then the general */ + if (lval1->ident==iARRAYCHAR) { + /* special case, assignment to packed character in a cell is permitted */ + lvalue=TRUE; + } else if (lval1->ident==iARRAY || lval1->ident==iREFARRAY) { + /* array assignment is permitted too (with restrictions) */ + if (oper) + return error(23); /* array assignment must be simple assigment */ + assert(lval1->sym!=NULL); + if (array_totalsize(lval1->sym)==0) + return error(46,lval1->sym->name); /* unknown array size */ + lvalue=TRUE; + } /* if */ + + /* operand on left side of assignment must be lvalue */ + if (!lvalue) + return error(22); /* must be lvalue */ + /* may not change "constant" parameters */ + assert(lval1->sym!=NULL); + if ((lval1->sym->usage & uCONST)!=0) + return error(22); /* assignment to const argument */ + sc_allowproccall=FALSE; /* may no longer use "procedure call" syntax */ + + lval3=*lval1; /* save symbol to enable storage of expresion result */ + lval1->arrayidx=org_arrayidx; /* restore array index pointer */ + if (lval1->ident==iARRAYCELL || lval1->ident==iARRAYCHAR + || lval1->ident==iARRAY || lval1->ident==iREFARRAY) + { + /* if indirect fetch: save PRI (cell address) */ + if (oper) { + pushreg(sPRI); + rvalue(lval1); + } /* if */ + lval2.arrayidx=arrayidx2; + plnge2(oper,hier14,lval1,&lval2); + if (lval2.ident!=iARRAYCELL && lval2.ident!=iARRAYCHAR) + lval2.arrayidx=NULL; + if (oper) + popreg(sALT); + if (!oper && lval3.arrayidx!=NULL && lval2.arrayidx!=NULL + && lval3.ident==lval2.ident && lval3.sym==lval2.sym) + { + int same=TRUE; + assert(lval2.arrayidx==arrayidx2); + for (i=0; iname); /* self-assignment */ + } /* if */ + } else { + if (oper){ + rvalue(lval1); + plnge2(oper,hier14,lval1,&lval2); + } else { + /* if direct fetch and simple assignment: no "push" + * and "pop" needed -> call hier14() directly, */ + if (hier14(&lval2)) + rvalue(&lval2); /* instead of plnge2(). */ + else if (lval2.ident==iVARIABLE) + lval2.ident=iEXPRESSION;/* mark as "rvalue" if it is not an "lvalue" */ + checkfunction(&lval2); + /* check whether lval2 and lval3 (old lval1) refer to the same variable */ + if (lval2.ident==iVARIABLE && lval3.ident==lval2.ident && lval3.sym==lval2.sym) { + assert(lval3.sym!=NULL); + error(226,lval3.sym->name); /* self-assignment */ + } /* if */ + } /* if */ + } /* if */ + /* Array elements are sometimes considered as sub-arrays --when the + * array index is an enumeration field and the enumeration size is greater + * than 1. If the expression on the right side of the assignment is a cell, + * or if an operation is in effect, this does not apply. + */ + leftarray= lval3.ident==iARRAY || lval3.ident==iREFARRAY + || ((lval3.ident==iARRAYCELL || lval3.ident==iARRAYCHAR) + && lval3.constval>1 && lval3.sym->dim.array.level==0 + && !oper && (lval2.ident==iARRAY || lval2.ident==iREFARRAY)); + if (leftarray) { + /* Left operand is an array, right operand should be an array variable + * of the same size and the same dimension, an array literal (of the + * same size) or a literal string. For single-dimensional arrays without + * tag for the index, it is permitted to assign a smaller array into a + * larger one (without warning). This is to make it easier to work with + * strings. + */ + int exactmatch=TRUE; + int idxtag=0; + int ltlength=(int)lval3.sym->dim.array.length; + if ((lval3.ident==iARRAYCELL || lval3.ident==iARRAYCHAR) + && lval3.constval>0 && lval3.sym->dim.array.level==0) + { + ltlength=(int)lval3.constval; + } /* if */ + if (lval2.ident!=iARRAY && lval2.ident!=iREFARRAY + && (lval2.sym==NULL || lval2.constval<=0)) + error(33,lval3.sym->name); /* array must be indexed */ + if (lval2.sym!=NULL) { + if (lval2.constval==0) { + val=lval2.sym->dim.array.length;/* array variable */ + } else { + val=lval2.constval; + if (lval2.sym->dim.array.level!=0) + error(28,lval2.sym->name); + } /* if */ + level=lval2.sym->dim.array.level; + idxtag=lval2.sym->x.idxtag; + if (level==0 && idxtag==0 && lval3.sym->x.idxtag==0) + exactmatch=FALSE; + } else { + val=lval2.constval; /* literal array */ + level=0; + /* If val is negative, it means that lval2 is a literal string. + * The string array size may be smaller than the destination + * array, provided that the destination array does not have an + * index tag. + */ + if (val<0) { + val=-val; + if (lval3.sym->x.idxtag==0) + exactmatch=FALSE; + } /* if */ + } /* if */ + if (lval3.sym->dim.array.level!=level) + return error(48); /* array dimensions must match */ + else if (ltlengthval || val==0) + return error(47); /* array sizes must match */ + else if (lval3.ident!=iARRAYCELL && !matchtag(lval3.sym->x.idxtag,idxtag,TRUE)) + error(229,(lval2.sym!=NULL) ? lval2.sym->name : lval3.sym->name); /* index tag mismatch */ + if (level>0) { + /* check the sizes of all sublevels too */ + symbol *sym1 = lval3.sym; + symbol *sym2 = lval2.sym; + int i; + assert(sym1!=NULL && sym2!=NULL); + /* ^^^ sym2 must be valid, because only variables can be + * multi-dimensional (there are no multi-dimensional literals), + * sym1 must be valid because it must be an lvalue + */ + assert(exactmatch); + for (i=0; idim.array.length!=sym2->dim.array.length) + error(47); /* array sizes must match */ + else if (!matchtag(sym1->x.idxtag,sym2->x.idxtag,TRUE)) + error(229,sym2->name); /* index tag mismatch */ + } /* for */ + /* get the total size in cells of the multi-dimensional array */ + val=array_totalsize(lval3.sym); + assert(val>0); /* already checked */ + } /* if */ + } else { + /* left operand is not an array, right operand should then not be either */ + if (lval2.ident==iARRAY || lval2.ident==iREFARRAY) + error(6); /* must be assigned to an array */ + } /* if */ + if (leftarray) { + memcopy(val*sizeof(cell)); + } else { + check_userop(NULL,lval2.tag,lval3.tag,2,&lval3,&lval2.tag); + store(&lval3); /* now, store the expression result */ + } /* if */ + if (!oper && !matchtag(lval3.tag,lval2.tag,TRUE)) + error(213); /* tagname mismatch (if "oper", warning already given in plunge2()) */ + if (lval3.sym) + markusage(lval3.sym,uWRITTEN); + sideeffect=TRUE; + bitwise_opercount=bwcount; + lval1->ident=iEXPRESSION; + return FALSE; /* expression result is never an lvalue */ +} + +static int hier13(value *lval) +{ + int lvalue=plnge1(hier12,lval); + if (matchtoken('?')) { + int flab1=getlabel(); + int flab2=getlabel(); + value lval2 = {0}; + int array1,array2; + + if (lvalue) { + rvalue(lval); + } else if (lval->ident==iCONSTEXPR) { + ldconst(lval->constval,sPRI); + error(lval->constval ? 206 : 205); /* redundant test */ + } /* if */ + jmp_eq0(flab1); /* go to second expression if primary register==0 */ + PUSHSTK_I(sc_allowtags); + sc_allowtags=FALSE; /* do not allow tagnames here (colon is a special token) */ + if (hier13(lval)) + rvalue(lval); + if (lval->ident==iCONSTEXPR) /* load constant here */ + ldconst(lval->constval,sPRI); + sc_allowtags=(short)POPSTK_I(); /* restore */ + jumplabel(flab2); + setlabel(flab1); + needtoken(':'); + if (hier13(&lval2)) + rvalue(&lval2); + if (lval2.ident==iCONSTEXPR) /* load constant here */ + ldconst(lval2.constval,sPRI); + array1= (lval->ident==iARRAY || lval->ident==iREFARRAY); + array2= (lval2.ident==iARRAY || lval2.ident==iREFARRAY); + if (array1 && !array2) { + char *ptr=(lval->sym->name!=NULL) ? lval->sym->name : "-unknown-"; + error(33,ptr); /* array must be indexed */ + } else if (!array1 && array2) { + char *ptr=(lval2.sym->name!=NULL) ? lval2.sym->name : "-unknown-"; + error(33,ptr); /* array must be indexed */ + } /* if */ + /* ??? if both are arrays, should check dimensions */ + if (!matchtag(lval->tag,lval2.tag,FALSE)) + error(213); /* tagname mismatch ('true' and 'false' expressions) */ + setlabel(flab2); + if (lval->ident==iARRAY) + lval->ident=iREFARRAY; /* iARRAY becomes iREFARRAY */ + else if (lval->ident!=iREFARRAY) + lval->ident=iEXPRESSION; /* iREFARRAY stays iREFARRAY, rest becomes iEXPRESSION */ + return FALSE; /* conditional expression is no lvalue */ + } else { + return lvalue; + } /* if */ +} + +/* the order of the operators in these lists is important and must be + * the same as the order of the operators in the array "op1" + */ +static int list3[] = {'*','/','%',0}; +static int list4[] = {'+','-',0}; +static int list5[] = {tSHL,tSHR,tSHRU,0}; +static int list6[] = {'&',0}; +static int list7[] = {'^',0}; +static int list8[] = {'|',0}; +static int list9[] = {tlLE,tlGE,'<','>',0}; +static int list10[] = {tlEQ,tlNE,0}; +static int list11[] = {tlAND,0}; +static int list12[] = {tlOR,0}; + +static int hier12(value *lval) +{ + return skim(list12,jmp_ne0,1,0,hier11,lval); +} + +static int hier11(value *lval) +{ + return skim(list11,jmp_eq0,0,1,hier10,lval); +} + +static int hier10(value *lval) +{ /* ==, != */ + return plnge(list10,15,hier9,lval,"bool",TRUE); +} /* ^ this variable is the starting index in the op1[] + * array of the operators of this hierarchy level */ + +static int hier9(value *lval) +{ /* <=, >=, <, > */ + return plnge_rel(list9,11,hier8,lval); +} + +static int hier8(value *lval) +{ /* | */ + return plnge(list8,10,hier7,lval,NULL,FALSE); +} + +static int hier7(value *lval) +{ /* ^ */ + return plnge(list7,9,hier6,lval,NULL,FALSE); +} + +static int hier6(value *lval) +{ /* & */ + return plnge(list6,8,hier5,lval,NULL,FALSE); +} + +static int hier5(value *lval) +{ /* <<, >>, >>> */ + return plnge(list5,5,hier4,lval,NULL,FALSE); +} + +static int hier4(value *lval) +{ /* +, - */ + return plnge(list4,3,hier3,lval,NULL,FALSE); +} + +static int hier3(value *lval) +{ /* *, /, % */ + return plnge(list3,0,hier2,lval,NULL,FALSE); +} + +static int hier2(value *lval) +{ + int lvalue,tok; + int tag,paranthese; + cell val; + char *st; + symbol *sym; + int saveresult; + + tok=lex(&val,&st); + switch (tok) { + case tINC: /* ++lval */ + if (!hier2(lval)) + return error(22); /* must be lvalue */ + assert(lval->sym!=NULL); + if ((lval->sym->usage & uCONST)!=0) + return error(22); /* assignment to const argument */ + if (!check_userop(user_inc,lval->tag,0,1,lval,&lval->tag)) + inc(lval); /* increase variable first */ + rvalue(lval); /* and read the result into PRI */ + sideeffect=TRUE; + return FALSE; /* result is no longer lvalue */ + case tDEC: /* --lval */ + if (!hier2(lval)) + return error(22); /* must be lvalue */ + assert(lval->sym!=NULL); + if ((lval->sym->usage & uCONST)!=0) + return error(22); /* assignment to const argument */ + if (!check_userop(user_dec,lval->tag,0,1,lval,&lval->tag)) + dec(lval); /* decrease variable first */ + rvalue(lval); /* and read the result into PRI */ + sideeffect=TRUE; + return FALSE; /* result is no longer lvalue */ + case '~': /* ~ (one's complement) */ + if (hier2(lval)) + rvalue(lval); + invert(); /* bitwise NOT */ + lval->constval=~lval->constval; + return FALSE; + case '!': /* ! (logical negate) */ + if (hier2(lval)) + rvalue(lval); + if (check_userop(lneg,lval->tag,0,1,NULL,&lval->tag)) { + lval->ident=iEXPRESSION; + lval->constval=0; + } else { + lneg(); /* 0 -> 1, !0 -> 0 */ + lval->constval=!lval->constval; + lval->tag=pc_addtag("bool"); + } /* if */ + return FALSE; + case '-': /* unary - (two's complement) */ + if (hier2(lval)) + rvalue(lval); + /* make a special check for a constant expression with the tag of a + * rational number, so that we can simple swap the sign of that constant. + */ + if (lval->ident==iCONSTEXPR && lval->tag==sc_rationaltag && sc_rationaltag!=0) { + if (rational_digits==0) { + #if PAWN_CELL_SIZE==32 + float *f = (float *)&lval->constval; + #elif PAWN_CELL_SIZE==64 + double *f = (double *)&lval->constval; + #else + #error Unsupported cell size + #endif + *f= - *f; /* this modifies lval->constval */ + } else { + /* the negation of a fixed point number is just an integer negation */ + lval->constval=-lval->constval; + } /* if */ + } else if (check_userop(neg,lval->tag,0,1,NULL,&lval->tag)) { + lval->ident=iEXPRESSION; + lval->constval=0; + } else { + neg(); /* arithmic negation */ + lval->constval=-lval->constval; + } /* if */ + return FALSE; + case tLABEL: /* tagname override */ + tag=pc_addtag(st); + lvalue=hier2(lval); + lval->tag=tag; + return lvalue; + case tDEFINED: + paranthese=0; + while (matchtoken('(')) + paranthese++; + tok=lex(&val,&st); + if (tok!=tSYMBOL) + return error(20,st); /* illegal symbol name */ + sym=findloc(st); + if (sym==NULL) + sym=findglb(st); + if (sym!=NULL && sym->ident!=iFUNCTN && sym->ident!=iREFFUNC && (sym->usage & uDEFINE)==0) + sym=NULL; /* symbol is not a function, it is in the table, but not "defined" */ + val= (sym!=NULL); + if (!val && find_subst(st,strlen(st))!=NULL) + val=1; + clear_value(lval); + lval->ident=iCONSTEXPR; + lval->constval= val; + ldconst(lval->constval,sPRI); + while (paranthese--) + needtoken(')'); + return FALSE; + case tSIZEOF: + paranthese=0; + while (matchtoken('(')) + paranthese++; + tok=lex(&val,&st); + if (tok!=tSYMBOL) + return error(20,st); /* illegal symbol name */ + sym=findloc(st); + if (sym==NULL) + sym=findglb(st); + if (sym==NULL) + return error(17,st); /* undefined symbol */ + if (sym->ident==iCONSTEXPR) + error(39); /* constant symbol has no size */ + else if (sym->ident==iFUNCTN || sym->ident==iREFFUNC) + error(72); /* "function" symbol has no size */ + else if ((sym->usage & uDEFINE)==0) + return error(17,st); /* undefined symbol (symbol is in the table, but it is "used" only) */ + clear_value(lval); + lval->ident=iCONSTEXPR; + lval->constval=1; /* preset */ + if (sym->ident==iARRAY || sym->ident==iREFARRAY) { + int level; + symbol *idxsym; + for (level=0; matchtoken('['); level++) { + idxsym=NULL; + if (level==sym->dim.array.level && matchtoken(tSYMBOL)) { + char *idxname; + tokeninfo(&val,&idxname); + if ((idxsym=findconst(idxname))==NULL) + error(80,idxname); /* unknown symbol, or non-constant */ + } /* if */ + needtoken(']'); + } /* for */ + if (level>sym->dim.array.level+1) + error(28,sym->name); /* invalid subscript */ + else if (level==sym->dim.array.level+1) + lval->constval= (idxsym!=NULL && idxsym->dim.array.length>0) ? idxsym->dim.array.length : 1; + else + lval->constval=array_levelsize(sym,level); + if (lval->constval==0 && strchr((char *)lptr,PREPROC_TERM)==NULL) + error(224,st); /* indeterminate array size in "sizeof" expression */ + } /* if */ + ldconst(lval->constval,sPRI); + while (paranthese--) + needtoken(')'); + return FALSE; + case tTAGOF: + paranthese=0; + while (matchtoken('(')) + paranthese++; + tok=lex(&val,&st); + if (tok!=tSYMBOL && tok!=tLABEL) + return error(20,st); /* illegal symbol name */ + if (tok==tLABEL) { + constvalue *tagsym=find_constval(&tagname_tab,st,0); + tag=(int)((tagsym!=NULL) ? tagsym->value : 0); + } else { + sym=findloc(st); + if (sym==NULL) + sym=findglb(st); + if (sym==NULL) + return error(17,st); /* undefined symbol */ + if ((sym->usage & uDEFINE)==0) + return error(17,st); /* undefined symbol (symbol is in the table, but it is "used" only) */ + tag=sym->tag; + } /* if */ + if (sym->ident==iARRAY || sym->ident==iREFARRAY) { + int level; + symbol *idxsym; + for (level=0; matchtoken('['); level++) { + idxsym=NULL; + if (level==sym->dim.array.level && matchtoken(tSYMBOL)) { + char *idxname; + tokeninfo(&val,&idxname); + if ((idxsym=findconst(idxname))==NULL) + error(80,idxname); /* unknown symbol, or non-constant */ + } /* if */ + needtoken(']'); + } /* for */ + if (level>sym->dim.array.level+1) + error(28,sym->name); /* invalid subscript */ + else if (level==sym->dim.array.level+1 && idxsym!=NULL) + tag= idxsym->x.idxtag; + } /* if */ + exporttag(tag); + clear_value(lval); + lval->ident=iCONSTEXPR; + lval->constval=tag; + ldconst(lval->constval,sPRI); + while (paranthese--) + needtoken(')'); + return FALSE; + default: + lexpush(); + lvalue=hier1(lval); + /* check for postfix operators */ + if (matchtoken(';')) { + /* Found a ';', do not look further for postfix operators */ + lexpush(); /* push ';' back after successful match */ + return lvalue; + } else if (matchtoken(tTERM)) { + /* Found a newline that ends a statement (this is the case when + * semicolons are optional). Note that an explicit semicolon was + * handled above. This case is similar, except that the token must + * not be pushed back. + */ + return lvalue; + } else { + tok=lex(&val,&st); + switch (tok) { + case tINC: /* lval++ */ + if (!lvalue) + return error(22); /* must be lvalue */ + assert(lval->sym!=NULL); + if ((lval->sym->usage & uCONST)!=0) + return error(22); /* assignment to const argument */ + /* on incrementing array cells, the address in PRI must be saved for + * incremening the value, whereas the current value must be in PRI + * on exit. + */ + saveresult= (lval->ident==iARRAYCELL || lval->ident==iARRAYCHAR); + if (saveresult) + pushreg(sPRI); /* save address in PRI */ + rvalue(lval); /* read current value into PRI */ + if (saveresult) + swap1(); /* save PRI on the stack, restore address in PRI */ + if (!check_userop(user_inc,lval->tag,0,1,lval,&lval->tag)) + inc(lval); /* increase variable afterwards */ + if (saveresult) + popreg(sPRI); /* restore PRI (result of rvalue()) */ + sideeffect=TRUE; + return FALSE; /* result is no longer lvalue */ + case tDEC: /* lval-- */ + if (!lvalue) + return error(22); /* must be lvalue */ + assert(lval->sym!=NULL); + if ((lval->sym->usage & uCONST)!=0) + return error(22); /* assignment to const argument */ + saveresult= (lval->ident==iARRAYCELL || lval->ident==iARRAYCHAR); + if (saveresult) + pushreg(sPRI); /* save address in PRI */ + rvalue(lval); /* read current value into PRI */ + if (saveresult) + swap1(); /* save PRI on the stack, restore address in PRI */ + if (!check_userop(user_dec,lval->tag,0,1,lval,&lval->tag)) + dec(lval); /* decrease variable afterwards */ + if (saveresult) + popreg(sPRI); /* restore PRI (result of rvalue()) */ + sideeffect=TRUE; + return FALSE; + case tCHAR: /* char (compute required # of cells */ + if (lval->ident==iCONSTEXPR) { + lval->constval *= sCHARBITS/8; /* from char to bytes */ + lval->constval = (lval->constval + sizeof(cell)-1) / sizeof(cell); + } else { + if (lvalue) + rvalue(lval); /* fetch value if not already in PRI */ + char2addr(); /* from characters to bytes */ + addconst(sizeof(cell)-1); /* make sure the value is rounded up */ + addr2cell(); /* truncate to number of cells */ + } /* if */ + return FALSE; + default: + lexpush(); + return lvalue; + } /* switch */ + } /* if */ + } /* switch */ +} + +/* hier1 + * + * The highest hierarchy level: it looks for pointer and array indices + * and function calls. + * Generates code to fetch a pointer value if it is indexed and code to + * add to the pointer value or the array address (the address is already + * read at primary()). It also generates code to fetch a function address + * if that hasn't already been done at primary() (check lval[4]) and calls + * callfunction() to call the function. + */ +static int hier1(value *lval1) +{ + int lvalue,index,tok,symtok; + cell val,cidx; + value lval2 = {0}; + char *st; + char close; + symbol *sym; + symbol dummysymbol,*cursym; /* for changing the index tags in case of enumerated pseudo-arrays */ + + lvalue=primary(lval1); + symtok=tokeninfo(&val,&st); /* get token read by primary() */ + cursym=lval1->sym; +restart: + sym=cursym; + if (matchtoken('[') || matchtoken('{') || matchtoken('(')) { + tok=tokeninfo(&val,&st); /* get token read by matchtoken() */ + if (sym==NULL && symtok!=tSYMBOL) { + /* we do not have a valid symbol and we appear not to have read a valid + * symbol name (so it is unlikely that we would have read a name of an + * undefined symbol) */ + error(29); /* expression error, assumed 0 */ + lexpush(); /* analyse '(', '{' or '[' again later */ + return FALSE; + } /* if */ + if (tok=='[' || tok=='{') { /* subscript */ + close = (char)((tok=='[') ? ']' : '}'); + if (sym==NULL) { /* sym==NULL if lval is a constant or a literal */ + error(28,""); /* cannot subscript */ + needtoken(close); + return FALSE; + } else if (sym->ident!=iARRAY && sym->ident!=iREFARRAY){ + error(28,sym->name); /* cannot subscript, variable is not an array */ + needtoken(close); + return FALSE; + } else if (sym->dim.array.level>0 && close!=']') { + error(51); /* invalid subscript, must use [ ] */ + needtoken(close); + return FALSE; + } /* if */ + stgget(&index,&cidx); /* mark position in code generator */ + pushreg(sPRI); /* save base address of the array */ + if (hier14(&lval2)) /* create expression for the array index */ + rvalue(&lval2); + if (lval2.ident==iARRAY || lval2.ident==iREFARRAY) + error(33,lval2.sym->name); /* array must be indexed */ + needtoken(close); + if (!matchtag(sym->x.idxtag,lval2.tag,TRUE)) + error(213); + if (lval2.ident==iCONSTEXPR) { /* constant expression */ + stgdel(index,cidx); /* scratch generated code */ + if (lval1->arrayidx!=NULL) { /* keep constant index, for checking */ + assert(sym->dim.array.level>=0 && sym->dim.array.levelarrayidx[sym->dim.array.level]=lval2.constval; + } /* if */ + if (close==']') { + /* normal array index */ + if (lval2.constval<0 || sym->dim.array.length!=0 && sym->dim.array.length<=lval2.constval) + error(32,sym->name); /* array index out of bounds */ + if (lval2.constval!=0) { + /* don't add offsets for zero subscripts */ + #if PAWN_CELL_SIZE==16 + ldconst(lval2.constval<<1,sALT); + #elif PAWN_CELL_SIZE==32 + ldconst(lval2.constval<<2,sALT); + #elif PAWN_CELL_SIZE==64 + ldconst(lval2.constval<<3,sALT); + #else + #error Unsupported cell size + #endif + ob_add(); + } /* if */ + } else { + /* character index */ + if (lval2.constval<0 || sym->dim.array.length!=0 + && sym->dim.array.length*((8*sizeof(cell))/sCHARBITS)<=(ucell)lval2.constval) + error(32,sym->name); /* array index out of bounds */ + if (lval2.constval!=0) { + /* don't add offsets for zero subscripts */ + #if sCHARBITS==16 + ldconst(lval2.constval<<1,sALT);/* 16-bit character */ + #else + ldconst(lval2.constval,sALT); /* 8-bit character */ + #endif + ob_add(); + } /* if */ + charalign(); /* align character index into array */ + } /* if */ + /* if the array index is a field from an enumeration, get the tag name + * from the field and save the size of the field too. + */ + assert(lval2.sym==NULL || lval2.sym->dim.array.level==0); + if (lval2.sym!=NULL && lval2.sym->dim.array.length>0 && sym->dim.array.level==0) { + lval1->tag=lval2.sym->x.idxtag; + lval1->constval=lval2.sym->dim.array.length; + } /* if */ + } else { + /* array index is not constant */ + lval1->arrayidx=NULL; /* reset, so won't be checked */ + if (close==']') { + if (sym->dim.array.length!=0) + ffbounds(sym->dim.array.length-1); /* run time check for array bounds */ + cell2addr(); /* normal array index */ + } else { + if (sym->dim.array.length!=0) + ffbounds(sym->dim.array.length*(32/sCHARBITS)-1); + char2addr(); /* character array index */ + } /* if */ + popreg(sALT); + ob_add(); /* base address was popped into secondary register */ + if (close!=']') + charalign(); /* align character index into array */ + } /* if */ + /* the indexed item may be another array (multi-dimensional arrays) */ + assert(cursym==sym && sym!=NULL); /* should still be set */ + if (sym->dim.array.level>0) { + assert(close==']'); /* checked earlier */ + assert(cursym==lval1->sym); + /* read the offset to the subarray and add it to the current address */ + lval1->ident=iARRAYCELL; + pushreg(sPRI); /* the optimizer makes this to a MOVE.alt */ + rvalue(lval1); + popreg(sALT); + ob_add(); + /* adjust the "value" structure and find the referenced array */ + lval1->ident=iREFARRAY; + lval1->sym=finddepend(sym); + assert(lval1->sym!=NULL); + assert(lval1->sym->dim.array.level==sym->dim.array.level-1); + cursym=lval1->sym; + /* try to parse subsequent array indices */ + lvalue=FALSE; /* for now, a iREFARRAY is no lvalue */ + goto restart; + } /* if */ + assert(sym->dim.array.level==0); + /* set type to fetch... INDIRECTLY */ + lval1->ident= (char)((close==']') ? iARRAYCELL : iARRAYCHAR); + /* if the array index is a field from an enumeration, get the tag name + * from the field and save the size of the field too. Otherwise, the + * tag is the one from the array symbol. + */ + if (lval2.ident==iCONSTEXPR && lval2.sym!=NULL + && lval2.sym->dim.array.length>0 && sym->dim.array.level==0) + { + lval1->tag=lval2.sym->x.idxtag; + lval1->constval=lval2.sym->dim.array.length; + if (lval2.tag==sym->x.idxtag && lval1->constval>1 && matchtoken('[')) { + /* an array indexed with an enumeration field may be considered a sub-array */ + lexpush(); + lvalue=FALSE; /* for now, a iREFARRAY is no lvalue */ + lval1->ident=iREFARRAY; + /* initialize a dummy symbol, which is a copy of the current symbol, + * but with an adjusted index tag + */ + assert(sym!=NULL); + dummysymbol=*sym; + /* get the tag of the root of the enumeration */ + assert(lval2.sym!=NULL); + dummysymbol.x.idxtag=lval2.sym->fieldtag; + dummysymbol.dim.array.length=lval2.sym->dim.array.length; + cursym=&dummysymbol; + /* recurse */ + goto restart; + } /* if */ + } else { + assert(sym!=NULL); + if (cursym!=&dummysymbol) + lval1->tag=sym->tag; + lval1->constval=0; + } /* if */ + /* a cell in an array is an lvalue, a character in an array is not + * always a *valid* lvalue */ + return TRUE; + } else { /* tok=='(' -> function(...) */ + assert(tok=='('); + if (sym==NULL + || (sym->ident!=iFUNCTN && sym->ident!=iREFFUNC)) + { + if (sym==NULL && sc_status==statFIRST) { + /* could be a "use before declaration"; in that case, create a stub + * function so that the usage can be marked. + */ + sym=fetchfunc(lastsymbol,0); + if (sym==NULL) + error(103); /* insufficient memory */ + markusage(sym,uREAD); + } else { + return error(12); /* invalid function call */ + } /* if */ + } else if ((sym->usage & uMISSING)!=0) { + char symname[2*sNAMEMAX+16]; /* allow space for user defined operators */ + funcdisplayname(symname,sym->name); + error(4,symname); /* function not defined */ + } /* if */ + callfunction(sym,lval1,TRUE); + return FALSE; /* result of function call is no lvalue */ + } /* if */ + } /* if */ + if (sym!=NULL && lval1->ident==iFUNCTN) { + assert(sym->ident==iFUNCTN); + if (sc_allowproccall) { + callfunction(sym,lval1,FALSE); + } else { + lval1->sym=NULL; + lval1->ident=iEXPRESSION; + lval1->constval=0; + lval1->tag=0; + error(76); /* invalid function call, or syntax error */ + } /* if */ + return FALSE; + } /* if */ + return lvalue; +} + +/* primary + * + * Returns 1 if the operand is an lvalue (everything except arrays, functions + * constants and -of course- errors). + * Generates code to fetch the address of arrays. Code for constants is + * already generated by constant(). + * This routine first clears the entire lval array (all fields are set to 0). + * + * Global references: sc_intest (may be altered, but restored upon termination) + */ +static int primary(value *lval) +{ + char *st; + int lvalue,tok; + cell val; + symbol *sym; + + if (matchtoken('(')){ /* sub-expression - (expression,...) */ + PUSHSTK_I(sc_intest); + PUSHSTK_I(sc_allowtags); + + sc_intest=FALSE; /* no longer in "test" expression */ + sc_allowtags=TRUE; /* allow tagnames to be used in parenthesized expressions */ + sc_allowproccall=FALSE; + do + lvalue=hier14(lval); + while (matchtoken(',')); + needtoken(')'); + lexclr(FALSE); /* clear lex() push-back, it should have been + * cleared already by needtoken() */ + sc_allowtags=(short)POPSTK_I(); + sc_intest=(short)POPSTK_I(); + return lvalue; + } /* if */ + + clear_value(lval); /* clear lval */ + tok=lex(&val,&st); + if (tok==tSYMBOL) { + /* lastsymbol is char[sNAMEMAX+1], lex() should have truncated any symbol + * to sNAMEMAX significant characters */ + assert(strlen(st)ident==iLABEL) { + error(29); /* expression error, assumed 0 */ + ldconst(0,sPRI); /* load 0 */ + return FALSE; /* return 0 for labels (expression error) */ + } /* if */ + lval->sym=sym; + lval->ident=sym->ident; + lval->tag=sym->tag; + if (sym->ident==iARRAY || sym->ident==iREFARRAY) { + address(sym,sPRI); /* get starting address in primary register */ + return FALSE; /* return 0 for array (not lvalue) */ + } else { + return TRUE; /* return 1 if lvalue (not label or array) */ + } /* if */ + } /* if */ + /* now try a global variable */ + if ((sym=findglb(st))!=0) { + if (sym->ident==iFUNCTN || sym->ident==iREFFUNC) { + /* if the function is only in the table because it was inserted as a + * stub in the first pass (i.e. it was "used" but never declared or + * implemented, issue an error + */ + if ((sym->usage & uPROTOTYPED)==0) + error(17,st); + } else { + if ((sym->usage & uDEFINE)==0) + error(17,st); + lval->sym=sym; + lval->ident=sym->ident; + lval->tag=sym->tag; + if (sym->ident==iARRAY || sym->ident==iREFARRAY) { + address(sym,sPRI); /* get starting address in primary register */ + return FALSE; /* return 0 for array (not lvalue) */ + } else { + return TRUE; /* return 1 if lvalue (not function or array) */ + } /* if */ + } /* if */ + } else { + if (!sc_allowproccall) + return error(17,st); /* undefined symbol */ + /* an unknown symbol, but used in a way compatible with the "procedure + * call" syntax. So assume that the symbol refers to a function. + */ + assert(sc_status==statFIRST); + sym=fetchfunc(st,0); + if (sym==NULL) + error(103); /* insufficient memory */ + } /* if */ + assert(sym!=NULL); + assert(sym->ident==iFUNCTN || sym->ident==iREFFUNC); + lval->sym=sym; + lval->ident=sym->ident; + lval->tag=sym->tag; + return FALSE; /* return 0 for function (not an lvalue) */ + } /* if */ + lexpush(); /* push the token, it is analyzed by constant() */ + if (constant(lval)==0) { + error(29); /* expression error, assumed 0 */ + ldconst(0,sPRI); /* load 0 */ + } /* if */ + return FALSE; /* return 0 for constants (or errors) */ +} + +static void clear_value(value *lval) +{ + lval->sym=NULL; + lval->constval=0L; + lval->tag=0; + lval->ident=0; + lval->boolresult=FALSE; + /* do not clear lval->arrayidx, it is preset in hier14() */ +} + +static void setdefarray(cell *string,cell size,cell array_sz,cell *dataaddr,int fconst) +{ + /* The routine must copy the default array data onto the heap, as to avoid + * that a function can change the default value. An optimization is that + * the default array data is "dumped" into the data segment only once (on the + * first use). + */ + assert(string!=NULL); + assert(size>0); + /* check whether to dump the default array */ + assert(dataaddr!=NULL); + if (sc_status==statWRITE && *dataaddr<0) { + int i; + *dataaddr=(litidx+glb_declared)*sizeof(cell); + for (i=0; i=size); + modheap((int)array_sz*sizeof(cell)); + /* ??? should perhaps fill with zeros first */ + memcopy(size*sizeof(cell)); + moveto1(); + } /* if */ +} + +static int findnamedarg(arginfo *arg,char *name) +{ + int i; + + for (i=0; arg[i].ident!=0 && arg[i].ident!=iVARARGS; i++) + if (strcmp(arg[i].name,name)==0) + return i; + return -1; +} + +static int checktag(int tags[],int numtags,int exprtag) +{ + int i; + + assert(tags!=0); + assert(numtags>0); + for (i=0; iident=iEXPRESSION; /* preset, may be changed later */ + lval_result->constval=0; + lval_result->tag=sym->tag; + /* check whether this is a function that returns an array */ + symret=finddepend(sym); + assert(symret==NULL || symret->ident==iREFARRAY); + if (symret!=NULL) { + int retsize; + /* allocate space on the heap for the array, and pass the pointer to the + * reserved memory block as a hidden parameter + */ + retsize=(int)array_totalsize(symret); + assert(retsize>0); + modheap(retsize*sizeof(cell));/* address is in ALT */ + pushreg(sALT); /* pass ALT as the last (hidden) parameter */ + decl_heap+=retsize; + /* also mark the ident of the result as "array" */ + lval_result->ident=iREFARRAY; + lval_result->sym=symret; + } /* if */ + locheap=decl_heap; + + nesting++; + assert(nest_stkusage>=0); + #if !defined NDEBUG + if (nesting==1) + assert(nest_stkusage==0); + #endif + + /* run through the arguments */ + arg=sym->dim.arglist; + assert(arg!=NULL); + stgmark(sSTARTREORDER); + memset(arglist,ARG_UNHANDLED,sizeof arglist); + if (matchparanthesis) { + /* Opening brace was already parsed, if closing brace follows, this + * call passes no parameters. + */ + close=matchtoken(')'); + } else { + /* When we find an end of line here, it may be a function call passing + * no parameters, or it may be that the first parameter is on a line + * below. But as a parameter can be anything, this is difficult to check. + * The only simple check that we have is the use of "named parameters". + */ + close=matchtoken(tTERM); + if (close) { + close=!matchtoken('.'); + if (!close) + lexpush(); /* reset the '.' */ + } /* if */ + } /* if */ + if (!close) { + do { + if (matchtoken('.')) { + namedparams=TRUE; + if (needtoken(tSYMBOL)) + tokeninfo(&lexval,&lexstr); + else + lexstr=""; + argpos=findnamedarg(arg,lexstr); + if (argpos<0) { + error(17,lexstr); /* undefined symbol */ + break; /* exit loop, argpos is invalid */ + } /* if */ + needtoken('='); + argidx=argpos; + } else { + if (namedparams) + error(44); /* positional parameters must precede named parameters */ + argpos=nargs; + } /* if */ + /* the number of arguments this was already checked at the declaration + * of the function; check it again for functions with a variable + * argument list + */ + if (argpos>=sMAXARGS) + error(45); /* too many function arguments */ + stgmark((char)(sEXPRSTART+argpos));/* mark beginning of new expression in stage */ + if (arglist[argpos]!=ARG_UNHANDLED) + error(58); /* argument already set */ + if (matchtoken('_')) { + arglist[argpos]=ARG_IGNORED; /* flag argument as "present, but ignored" */ + if (arg[argidx].ident==0 || arg[argidx].ident==iVARARGS) { + error(202); /* argument count mismatch */ + } else if (!arg[argidx].hasdefault) { + error(34,nargs+1); /* argument has no default value */ + } /* if */ + if (arg[argidx].ident!=0 && arg[argidx].ident!=iVARARGS) + argidx++; + /* The rest of the code to handle default values is at the bottom + * of this routine where default values for unspecified parameters + * are (also) handled. Note that above, the argument is flagged as + * ARG_IGNORED. + */ + } else { + arglist[argpos]=ARG_DONE; /* flag argument as "present" */ + lvalue=hier14(&lval); + switch (arg[argidx].ident) { + case 0: + error(202); /* argument count mismatch */ + break; + case iVARARGS: + /* always pass by reference */ + if (lval.ident==iVARIABLE || lval.ident==iREFERENCE) { + assert(lval.sym!=NULL); + if ((lval.sym->usage & uCONST)!=0 && (arg[argidx].usage & uCONST)==0) { + /* treat a "const" variable passed to a function with a non-const + * "variable argument list" as a constant here */ + assert(lvalue); + rvalue(&lval); /* get value in PRI */ + setheap_pri(); /* address of the value on the heap in PRI */ + heapalloc++; + nest_stkusage++; + } else if (lvalue) { + address(lval.sym,sPRI); + } else { + setheap_pri(); /* address of the value on the heap in PRI */ + heapalloc++; + nest_stkusage++; + } /* if */ + } else if (lval.ident==iCONSTEXPR || lval.ident==iEXPRESSION + || lval.ident==iARRAYCHAR) + { + /* fetch value if needed */ + if (lval.ident==iARRAYCHAR) + rvalue(&lval); + /* allocate a cell on the heap and store the + * value (already in PRI) there */ + setheap_pri(); /* address of the value on the heap in PRI */ + heapalloc++; + nest_stkusage++; + } /* if */ + /* ??? handle const array passed by reference */ + /* otherwise, the address is already in PRI */ + if (lval.sym!=NULL) + markusage(lval.sym,uWRITTEN); + if (!checktag(arg[argidx].tags,arg[argidx].numtags,lval.tag)) + error(213); + break; + case iVARIABLE: + if (lval.ident==iLABEL || lval.ident==iFUNCTN || lval.ident==iREFFUNC + || lval.ident==iARRAY || lval.ident==iREFARRAY) + error(35,argidx+1); /* argument type mismatch */ + if (lvalue) + rvalue(&lval); /* get value (direct or indirect) */ + /* otherwise, the expression result is already in PRI */ + assert(arg[argidx].numtags>0); + check_userop(NULL,lval.tag,arg[argidx].tags[0],2,NULL,&lval.tag); + if (!checktag(arg[argidx].tags,arg[argidx].numtags,lval.tag)) + error(213); + argidx++; /* argument done */ + break; + case iREFERENCE: + if (!lvalue || lval.ident==iARRAYCHAR) + error(35,argidx+1); /* argument type mismatch */ + if (lval.sym!=NULL && (lval.sym->usage & uCONST)!=0 && (arg[argidx].usage & uCONST)==0) + error(35,argidx+1); /* argument type mismatch */ + if (lval.ident==iVARIABLE || lval.ident==iREFERENCE) { + if (lvalue) { + assert(lval.sym!=NULL); + address(lval.sym,sPRI); + } else { + setheap_pri(); /* address of the value on the heap in PRI */ + heapalloc++; + nest_stkusage++; + } /* if */ + } /* if */ + /* otherwise, the address is already in PRI */ + if (!checktag(arg[argidx].tags,arg[argidx].numtags,lval.tag)) + error(213); + argidx++; /* argument done */ + if (lval.sym!=NULL) + markusage(lval.sym,uWRITTEN); + break; + case iREFARRAY: + if (lval.ident!=iARRAY && lval.ident!=iREFARRAY + && lval.ident!=iARRAYCELL) + { + error(35,argidx+1); /* argument type mismatch */ + break; + } /* if */ + if (lval.sym!=NULL && (lval.sym->usage & uCONST)!=0 && (arg[argidx].usage & uCONST)==0) + error(35,argidx+1); /* argument type mismatch */ + /* Verify that the dimensions match with those in arg[argidx]. + * A literal array always has a single dimension. + * An iARRAYCELL parameter is also assumed to have a single dimension. + */ + if (lval.sym==NULL || lval.ident==iARRAYCELL) { + if (arg[argidx].numdim!=1) { + error(48); /* array dimensions must match */ + } else if (arg[argidx].dim[0]!=0) { + assert(arg[argidx].dim[0]>0); + if (lval.ident==iARRAYCELL) { + error(47); /* array sizes must match */ + } else { + assert(lval.constval!=0); /* literal array must have a size */ + /* A literal array must have exactly the same size as the + * function argument; a literal string may be smaller than + * the function argument. + */ + if (lval.constval>0 && arg[argidx].dim[0]!=lval.constval + || lval.constval<0 && arg[argidx].dim[0] < -lval.constval) + error(47); /* array sizes must match */ + } /* if */ + } /* if */ + if (lval.ident!=iARRAYCELL) { + /* save array size, for default values with uSIZEOF flag */ + cell array_sz=lval.constval; + assert(array_sz!=0);/* literal array must have a size */ + if (array_sz<0) + array_sz= -array_sz; + append_constval(&arrayszlst,arg[argidx].name,array_sz,0); + } /* if */ + } else { + symbol *sym=lval.sym; + short level=0; + assert(sym!=NULL); + if (sym->dim.array.level+1!=arg[argidx].numdim) + error(48); /* array dimensions must match */ + /* the lengths for all dimensions must match, unless the dimension + * length was defined at zero (which means "undefined") + */ + while (sym->dim.array.level>0) { + assert(leveldim.array.length!=arg[argidx].dim[level]) + error(47); /* array sizes must match */ + else if (!matchtag(arg[argidx].idxtag[level],sym->x.idxtag,TRUE)) + error(229,sym->name); /* index tag mismatch */ + append_constval(&arrayszlst,arg[argidx].name,sym->dim.array.length,level); + sym=finddepend(sym); + assert(sym!=NULL); + level++; + } /* if */ + /* the last dimension is checked too, again, unless it is zero */ + assert(leveldim.array.length!=arg[argidx].dim[level]) + error(47); /* array sizes must match */ + else if (!matchtag(arg[argidx].idxtag[level],sym->x.idxtag,TRUE)) + error(229,sym->name); /* index tag mismatch */ + append_constval(&arrayszlst,arg[argidx].name,sym->dim.array.length,level); + } /* if */ + /* address already in PRI */ + if (!checktag(arg[argidx].tags,arg[argidx].numtags,lval.tag)) + error(213); + // ??? set uWRITTEN? + argidx++; /* argument done */ + break; + } /* switch */ + pushreg(sPRI); /* store the function argument on the stack */ + markexpr(sPARM,NULL,0); /* mark the end of a sub-expression */ + nest_stkusage++; + } /* if */ + assert(arglist[argpos]!=ARG_UNHANDLED); + nargs++; + if (matchparanthesis) { + close=matchtoken(')'); + if (!close) /* if not paranthese... */ + if (!needtoken(',')) /* ...should be comma... */ + break; /* ...but abort loop if neither */ + } else { + close=!matchtoken(','); + if (close) { /* if not comma... */ + if (needtoken(tTERM)==1)/* ...must be end of statement */ + lexpush(); /* push again, because end of statement is analised later */ + } /* if */ + } /* if */ + } while (!close && freading && !matchtoken(tENDEXPR)); /* do */ + } /* if */ + /* check remaining function arguments (they may have default values) */ + for (argidx=0; arg[argidx].ident!=0 && arg[argidx].ident!=iVARARGS; argidx++) { + if (arglist[argidx]==ARG_DONE) + continue; /* already seen and handled this argument */ + /* in this first stage, we also skip the arguments with uSIZEOF and uTAGOF; + * these are handled last + */ + if ((arg[argidx].hasdefault & uSIZEOF)!=0 || (arg[argidx].hasdefault & uTAGOF)!=0) { + assert(arg[argidx].ident==iVARIABLE); + continue; + } /* if */ + stgmark((char)(sEXPRSTART+argidx));/* mark beginning of new expression in stage */ + if (arg[argidx].hasdefault) { + if (arg[argidx].ident==iREFARRAY) { + short level; + setdefarray(arg[argidx].defvalue.array.data, + arg[argidx].defvalue.array.size, + arg[argidx].defvalue.array.arraysize, + &arg[argidx].defvalue.array.addr, + (arg[argidx].usage & uCONST)!=0); + if ((arg[argidx].usage & uCONST)==0) { + heapalloc+=arg[argidx].defvalue.array.arraysize; + nest_stkusage+=arg[argidx].defvalue.array.arraysize; + } /* if */ + /* keep the lengths of all dimensions of a multi-dimensional default array */ + assert(arg[argidx].numdim>0); + if (arg[argidx].numdim==1) { + append_constval(&arrayszlst,arg[argidx].name,arg[argidx].defvalue.array.arraysize,0); + } else { + for (level=0; level0); + check_userop(NULL,arg[argidx].defvalue_tag,arg[argidx].tags[0],2,NULL,&dummytag); + assert(dummytag==arg[argidx].tags[0]); + } /* if */ + pushreg(sPRI); /* store the function argument on the stack */ + markexpr(sPARM,NULL,0); /* mark the end of a sub-expression */ + nest_stkusage++; + } else { + error(202,argidx); /* argument count mismatch */ + } /* if */ + if (arglist[argidx]==ARG_UNHANDLED) + nargs++; + arglist[argidx]=ARG_DONE; + } /* for */ + /* now a second loop to catch the arguments with default values that are + * the "sizeof" or "tagof" of other arguments + */ + for (argidx=0; arg[argidx].ident!=0 && arg[argidx].ident!=iVARARGS; argidx++) { + constvalue *asz; + cell array_sz; + if (arglist[argidx]==ARG_DONE) + continue; /* already seen and handled this argument */ + stgmark((char)(sEXPRSTART+argidx));/* mark beginning of new expression in stage */ + assert(arg[argidx].ident==iVARIABLE); /* if "sizeof", must be single cell */ + /* if unseen, must be "sizeof" or "tagof" */ + assert((arg[argidx].hasdefault & uSIZEOF)!=0 || (arg[argidx].hasdefault & uTAGOF)!=0); + if ((arg[argidx].hasdefault & uSIZEOF)!=0) { + /* find the argument; if it isn't found, the argument's default value + * was a "sizeof" of a non-array (a warning for this was already given + * when declaring the function) + */ + asz=find_constval(&arrayszlst,arg[argidx].defvalue.size.symname, + arg[argidx].defvalue.size.level); + if (asz!=NULL) { + array_sz=asz->value; + if (array_sz==0) + error(224,arg[argidx].name); /* indeterminate array size in "sizeof" expression */ + } else { + array_sz=1; + } /* if */ + } else { + symbol *sym; + assert((arg[argidx].hasdefault & uTAGOF)!=0); + sym=findloc(arg[argidx].defvalue.size.symname); + if (sym==NULL) + sym=findglb(arg[argidx].defvalue.size.symname); + array_sz=(sym!=NULL) ? sym->tag : 0; + exporttag((int)array_sz); + } /* if */ + ldconst(array_sz,sPRI); + pushreg(sPRI); /* store the function argument on the stack */ + markexpr(sPARM,NULL,0); + nest_stkusage++; + if (arglist[argidx]==ARG_UNHANDLED) + nargs++; + arglist[argidx]=ARG_DONE; + } /* for */ + stgmark(sENDREORDER); /* mark end of reversed evaluation */ + pushval((cell)nargs*sizeof(cell)); + nest_stkusage++; + ffcall(sym,NULL,nargs); + if (sc_status!=statSKIP) + markusage(sym,uREAD); /* do not mark as "used" when this call itself is skipped */ + if ((sym->usage & uNATIVE)!=0 &&sym->x.lib!=NULL) + sym->x.lib->value += 1; /* increment "usage count" of the library */ + modheap(-heapalloc*sizeof(cell)); + if (symret!=NULL) + popreg(sPRI); /* pop hidden parameter as function result */ + sideeffect=TRUE; /* assume functions carry out a side-effect */ + sc_allowproccall=FALSE; + delete_consttable(&arrayszlst); /* clear list of array sizes */ + + /* maintain max. amount of memory used */ + { + long totalsize; + totalsize=declared+decl_heap+1; /* local variables & return value size, + * +1 for PROC opcode */ + if (lval_result->ident==iREFARRAY) + totalsize++; /* add hidden parameter (on the stack) */ + if ((sym->usage & uNATIVE)==0) + totalsize++; /* add "call" opcode */ + totalsize+=nest_stkusage; + assert(curfunc!=NULL); + if (curfunc->x.stacksizex.stacksize=totalsize; + nest_stkusage-=nargs+heapalloc+1; /* stack/heap space, +1 for argcount param */ + /* if there is a syntax error in the script, the stack calculation is + * probably incorrect; but we may not allow it to drop below zero + */ + if (nest_stkusage<0) + nest_stkusage=0; + } + + /* scrap any arrays left on the heap, with the exception of the array that + * this function has as a result (in other words, scrap all arrays on the + * heap that caused by expressions in the function arguments) + */ + assert(decl_heap>=locheap); + modheap((locheap-decl_heap)*sizeof(cell)); /* remove heap space, so negative delta */ + decl_heap=locheap; + nesting--; +} + +/* dbltest + * + * Returns a non-zero value if lval1 an array and lval2 is not an array and + * the operation is addition or subtraction. + * + * Returns the "shift" count (1 for 16-bit, 2 for 32-bit) to align a cell + * to an array offset. + */ +static int dbltest(void (*oper)(),value *lval1,value *lval2) +{ + if ((oper!=ob_add) && (oper!=ob_sub)) + return 0; + if (lval1->ident!=iARRAY) + return 0; + if (lval2->ident==iARRAY) + return 0; + return sizeof(cell)/2; /* 1 for 16-bit, 2 for 32-bit */ +} + +/* commutative + * + * Test whether an operator is commutative, i.e. x oper y == y oper x. + * Commutative operators are: + (addition) + * * (multiplication) + * == (equality) + * != (inequality) + * & (bitwise and) + * ^ (bitwise xor) + * | (bitwise or) + * + * If in an expression, code for the left operand has been generated and + * the right operand is a constant and the operator is commutative, the + * precautionary "push" of the primary register is scrapped and the constant + * is read into the secondary register immediately. + */ +static int commutative(void (*oper)()) +{ + return oper==ob_add || oper==os_mult + || oper==ob_eq || oper==ob_ne + || oper==ob_and || oper==ob_xor || oper==ob_or; +} + +/* constant + * + * Generates code to fetch a number, a literal character (which is returned + * by lex() as a number as well) or a literal string (lex() stores the + * strings in the literal queue). If the operand was a number, it is stored + * in lval->constval. + * + * The function returns 1 if the token was a constant or a string, 0 + * otherwise. + */ +static int constant(value *lval) +{ + int tok,index,ident; + cell val,item,cidx; + char *st; + symbol *sym; + + tok=lex(&val,&st); + if (tok==tSYMBOL && (sym=findconst(st))!=0){ + lval->constval=sym->addr; + ldconst(lval->constval,sPRI); + lval->ident=iCONSTEXPR; + lval->tag=sym->tag; + lval->sym=sym; + markusage(sym,uREAD); + } else if (tok==tNUMBER) { + lval->constval=val; + ldconst(lval->constval,sPRI); + lval->ident=iCONSTEXPR; + } else if (tok==tRATIONAL) { + lval->constval=val; + ldconst(lval->constval,sPRI); + lval->ident=iCONSTEXPR; + lval->tag=sc_rationaltag; + } else if (tok==tSTRING) { + /* lex() stores starting index of string in the literal table in 'val' */ + ldconst((val+glb_declared)*sizeof(cell),sPRI); + lval->ident=iARRAY; /* pretend this is a global array */ + lval->constval=val-litidx; /* constval == the negative value of the + * size of the literal array; using a negative + * value distinguishes between literal arrays + * and literal strings (this was done for + * array assignment). */ + } else if (tok=='{') { + int tag,lasttag=-1; + val=litidx; + do { + /* cannot call constexpr() here, because "staging" is already turned + * on at this point */ + assert(staging); + stgget(&index,&cidx); /* mark position in code generator */ + ident=expression(&item,&tag,NULL,FALSE); + stgdel(index,cidx); /* scratch generated code */ + if (ident!=iCONSTEXPR) + error(8); /* must be constant expression */ + if (lasttag<0) + lasttag=tag; + else if (!matchtag(lasttag,tag,FALSE)) + error(213); /* tagname mismatch */ + litadd(item); /* store expression result in literal table */ + } while (matchtoken(',')); + needtoken('}'); + ldconst((val+glb_declared)*sizeof(cell),sPRI); + lval->ident=iARRAY; /* pretend this is a global array */ + lval->constval=litidx-val; /* constval == the size of the literal array */ + } else { + return FALSE; /* no, it cannot be interpreted as a constant */ + } /* if */ + return TRUE; /* yes, it was a constant value */ +} + diff --git a/compiler/libpc300/sc4.c b/compiler/libpc300/sc4.c new file mode 100755 index 00000000..14d46636 --- /dev/null +++ b/compiler/libpc300/sc4.c @@ -0,0 +1,1304 @@ +/* Pawn compiler - code generation (unoptimized "assembler" code) + * + * Copyright (c) ITB CompuPhase, 1997-2005 + * + * This software is provided "as-is", without any express or implied warranty. + * In no event will the authors be held liable for any damages arising from + * the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software in + * a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * 3. This notice may not be removed or altered from any source distribution. + * + * Version: $Id$ + */ +#include +#include +#include +#include /* for _MAX_PATH */ +#include +#if defined FORTIFY + #include "fortify.h" +#endif +#include "sc.h" + +/* When a subroutine returns to address 0, the AMX must halt. In earlier + * releases, the RET and RETN opcodes checked for the special case 0 address. + * Today, the compiler simply generates a HALT instruction at address 0. So + * a subroutine can savely return to 0, and then encounter a HALT. + */ +SC_FUNC void writeleader(symbol *root) +{ + int lbl_nostate,lbl_table; + int statecount; + symbol *sym; + constvalue *fsa, *state, *stlist; + int fsa_id,listid; + char lbl_default[sNAMEMAX+1]; + + assert(code_idx==0); + + begcseg(); + stgwrite(";program exit point\n"); + stgwrite("\thalt 0\n"); + code_idx+=opcodes(1)+opargs(1); /* calculate code length */ + + /* check whether there are any functions that have states */ + for (sym=root->next; sym!=NULL; sym=sym->next) + if (sym->ident==iFUNCTN && (sym->usage & uREAD)!=0 && sym->states!=NULL) + break; + if (sym==NULL) + return; /* no function has states, nothing to do next */ + + /* generate an error function that is called for an undefined state */ + stgwrite("\n;exit point for functions called from the wrong state\n"); + lbl_nostate=getlabel(); + setlabel(lbl_nostate); + stgwrite("\thalt "); + outval(AMX_ERR_INVSTATE,TRUE); + code_idx+=opcodes(1)+opargs(1); /* calculate code length */ + + /* write the "state-selectors" table with all automatons (update the + * automatons structure too, as we are now assigning the address to + * each automaton state-selector variable) + */ + assert(glb_declared==0); + begdseg(); + for (fsa=sc_automaton_tab.next; fsa!=NULL; fsa=fsa->next) { + defstorage(); + stgwrite("0\t; automaton "); + if (strlen(fsa->name)==0) + stgwrite("(anonymous)"); + else + stgwrite(fsa->name); + stgwrite("\n"); + fsa->value=glb_declared*sizeof(cell); + glb_declared++; + } /* for */ + + /* write stubs and jump tables for all state functions */ + begcseg(); + for (sym=root->next; sym!=NULL; sym=sym->next) { + if (sym->ident==iFUNCTN && (sym->usage & uREAD)!=0 && sym->states!=NULL) { + stlist=sym->states->next; + assert(stlist!=NULL); /* there should be at least one state item */ + listid=stlist->index; + assert(listid==-1 || listid>0); + if (listid==-1 && stlist->next!=NULL) { + /* first index is the "fallback", take the next one (if available) */ + stlist=stlist->next; + listid=stlist->index; + } /* if */ + if (listid==-1) { + /* first index is the fallback, there is no second... */ + strcpy(stlist->name,"0"); /* insert dummy label number */ + /* this is an error, but we postpone adding the error message until the + * function definition + */ + continue; + } /* if */ + /* generate label numbers for all statelist ids */ + for (stlist=sym->states->next; stlist!=NULL; stlist=stlist->next) { + assert(strlen(stlist->name)==0); + strcpy(stlist->name,itoh(getlabel())); + } /* for */ + if (strcmp(sym->name,uENTRYFUNC)==0) + continue; /* do not generate stubs for this special function */ + sym->addr=code_idx; /* fix the function address now */ + /* get automaton id for this function */ + assert(listid>0); + fsa_id=state_getfsa(listid); + assert(fsa_id>=0); /* automaton 0 exists */ + fsa=automaton_findid(fsa_id); + /* count the number of states actually used; at the sane time, check + * whether there is a default state function + */ + statecount=0; + strcpy(lbl_default,itoh(lbl_nostate)); + for (stlist=sym->states->next; stlist!=NULL; stlist=stlist->next) { + if (stlist->index==-1) { + assert(strlen(stlist->name)name); + } else { + statecount+=state_count(stlist->index); + } /* if */ + } /* for */ + /* generate a stub entry for the functions */ + stgwrite("\tload.pri "); + outval(fsa->value,FALSE); + stgwrite("\t; "); + stgwrite(sym->name); + stgwrite("\n"); + code_idx+=opcodes(1)+opargs(1); /* calculate code length */ + lbl_table=getlabel(); + ffswitch(lbl_table); + /* generate the jump table */ + setlabel(lbl_table); + ffcase(statecount,lbl_default,TRUE); + for (state=sc_state_tab.next; state!=NULL; state=state->next) { + if (state->index==fsa_id) { + /* find the label for this list id */ + for (stlist=sym->states->next; stlist!=NULL; stlist=stlist->next) { + if (stlist->index!=-1 && state_inlist(stlist->index,(int)state->value)) { + ffcase(state->value,stlist->name,FALSE); + break; + } /* if */ + } /* for */ + if (stlist==NULL && strtol(lbl_default,NULL,16)==lbl_nostate) + error(230,state->name,sym->name); /* unimplemented state, no fallback */ + } /* if (state belongs to automaton of function) */ + } /* for (state) */ + stgwrite("\n"); + } /* if (is function, used & having states) */ + } /* for (sym) */ +} + +/* writetrailer + * Not much left of this once important function. + * + * Global references: sc_stksize (referred to only) + * sc_dataalign (referred to only) + * code_idx (altered) + * glb_declared (altered) + */ +SC_FUNC void writetrailer(void) +{ + assert(sc_dataalign % opcodes(1) == 0); /* alignment must be a multiple of + * the opcode size */ + assert(sc_dataalign!=0); + + /* pad code to align data segment */ + if ((code_idx % sc_dataalign)!=0) { + begcseg(); + while ((code_idx % sc_dataalign)!=0) + nooperation(); + } /* if */ + + /* pad data segment to align the stack and the heap */ + assert(litidx==0); /* literal queue should have been emptied */ + assert(sc_dataalign % sizeof(cell) == 0); + if (((glb_declared*sizeof(cell)) % sc_dataalign)!=0) { + begdseg(); + defstorage(); + while (((glb_declared*sizeof(cell)) % sc_dataalign)!=0) { + stgwrite("0 "); + glb_declared++; + } /* while */ + } /* if */ + + stgwrite("\nSTKSIZE "); /* write stack size (align stack top) */ + outval(sc_stksize - (sc_stksize % sc_dataalign), TRUE); +} + +/* + * Start (or restart) the CODE segment. + * + * In fact, the code and data segment specifiers are purely informational; + * the "DUMP" instruction itself already specifies that the following values + * should go to the data segment. All otherinstructions go to the code + * segment. + * + * Global references: curseg + */ +SC_FUNC void begcseg(void) +{ + if (curseg!=sIN_CSEG) { + stgwrite("\n"); + stgwrite("CODE\t; "); + outval(code_idx,TRUE); + curseg=sIN_CSEG; + } /* endif */ +} + +/* + * Start (or restart) the DATA segment. + * + * Global references: curseg + */ +SC_FUNC void begdseg(void) +{ + if (curseg!=sIN_DSEG) { + stgwrite("\n"); + stgwrite("DATA\t; "); + outval(glb_declared-litidx,TRUE); + curseg=sIN_DSEG; + } /* if */ +} + +SC_FUNC void setline(int chkbounds) +{ + if ((sc_debug & sSYMBOLIC)!=0 || chkbounds && (sc_debug & sCHKBOUNDS)!=0) { + /* generate a "break" (start statement) opcode rather than a "line" opcode + * because earlier versions of Small/Pawn have an incompatible version of the + * line opcode + */ + stgwrite("\tbreak\t; "); + outval(code_idx,TRUE); + code_idx+=opcodes(1); + } /* if */ +} + +SC_FUNC void setfiledirect(char *name) +{ + if (sc_status==statFIRST && sc_listing) { + assert(name!=NULL); + pc_writeasm(outf,"#file "); + pc_writeasm(outf,name); + pc_writeasm(outf,"\n"); + } /* if */ +} + +SC_FUNC void setlinedirect(int line) +{ + if (sc_status==statFIRST && sc_listing) { + char string[40]; + sprintf(string,"#line %d\n",line); + pc_writeasm(outf,string); + } /* if */ +} + +/* setlabel + * + * Post a code label (specified as a number), on a new line. + */ +SC_FUNC void setlabel(int number) +{ + assert(number>=0); + stgwrite("l."); + stgwrite((char *)itoh(number)); + /* To assist verification of the assembled code, put the address of the + * label as a comment. However, labels that occur inside an expression + * may move (through optimization or through re-ordering). So write the + * address only if it is known to accurate. + */ + if (!staging) { + stgwrite("\t\t; "); + outval(code_idx,FALSE); + } /* if */ + stgwrite("\n"); +} + +/* Write a token that signifies the start or end of an expression or special + * statement. This allows several simple optimizations by the peephole + * optimizer. + */ +SC_FUNC void markexpr(optmark type,const char *name,cell offset) +{ + switch (type) { + case sEXPR: + stgwrite("\t;$exp\n"); + break; + case sPARM: + stgwrite("\t;$par\n"); + break; + case sLDECL: + assert(name!=NULL); + stgwrite("\t;$lcl "); + stgwrite(name); + stgwrite(" "); + outval(offset,TRUE); + break; + default: + assert(0); + } /* switch */ +} + +/* startfunc - declare a CODE entry point (function start) + * + * Global references: funcstatus (referred to only) + */ +SC_FUNC void startfunc(char *fname) +{ + stgwrite("\tproc"); + if (sc_asmfile) { + char symname[2*sNAMEMAX+16]; + funcdisplayname(symname,fname); + stgwrite("\t; "); + stgwrite(symname); + } /* if */ + stgwrite("\n"); + code_idx+=opcodes(1); +} + +/* endfunc + * + * Declare a CODE ending point (function end) + */ +SC_FUNC void endfunc(void) +{ + stgwrite("\n"); /* skip a line */ +} + +/* alignframe + * + * Aligns the frame (and the stack) of the current function to a multiple + * of the specified byte count. Two caveats: the alignment ("numbytes") should + * be a power of 2, and this alignment must be done right after the frame + * is set up (before the first variable is declared) + */ +SC_FUNC void alignframe(int numbytes) +{ + #if !defined NDEBUG + /* "numbytes" should be a power of 2 for this code to work */ + int i,count=0; + for (i=0; isym; + if (lval->ident==iARRAYCELL) { + /* indirect fetch, address already in PRI */ + stgwrite("\tload.i\n"); + code_idx+=opcodes(1); + } else if (lval->ident==iARRAYCHAR) { + /* indirect fetch of a character from a pack, address already in PRI */ + stgwrite("\tlodb.i "); + outval(sCHARBITS/8,TRUE); /* read one or two bytes */ + code_idx+=opcodes(1)+opargs(1); + } else if (lval->ident==iREFERENCE) { + /* indirect fetch, but address not yet in PRI */ + assert(sym!=NULL); + assert(sym->vclass==sLOCAL);/* global references don't exist in Pawn */ + if (sym->vclass==sLOCAL) + stgwrite("\tlref.s.pri "); + else + stgwrite("\tlref.pri "); + outval(sym->addr,TRUE); + markusage(sym,uREAD); + code_idx+=opcodes(1)+opargs(1); + } else { + /* direct or stack relative fetch */ + assert(sym!=NULL); + if (sym->vclass==sLOCAL) + stgwrite("\tload.s.pri "); + else + stgwrite("\tload.pri "); + outval(sym->addr,TRUE); + markusage(sym,uREAD); + code_idx+=opcodes(1)+opargs(1); + } /* if */ +} + +/* Get the address of a symbol into the primary or alternate register (used + * for arrays, and for passing arguments by reference). + */ +SC_FUNC void address(symbol *sym,regid reg) +{ + assert(sym!=NULL); + assert(reg==sPRI || reg==sALT); + /* the symbol can be a local array, a global array, or an array + * that is passed by reference. + */ + if (sym->ident==iREFARRAY || sym->ident==iREFERENCE) { + /* reference to a variable or to an array; currently this is + * always a local variable */ + switch (reg) { + case sPRI: + stgwrite("\tload.s.pri "); + break; + case sALT: + stgwrite("\tload.s.alt "); + break; + } /* switch */ + } else { + /* a local array or local variable */ + switch (reg) { + case sPRI: + if (sym->vclass==sLOCAL) + stgwrite("\taddr.pri "); + else + stgwrite("\tconst.pri "); + break; + case sALT: + if (sym->vclass==sLOCAL) + stgwrite("\taddr.alt "); + else + stgwrite("\tconst.alt "); + break; + } /* switch */ + } /* if */ + outval(sym->addr,TRUE); + markusage(sym,uREAD); + code_idx+=opcodes(1)+opargs(1); +} + +/* store + * + * Saves the contents of "primary" into a memory cell, either directly + * or indirectly (at the address given in the alternate register). + */ +SC_FUNC void store(value *lval) +{ + symbol *sym; + + sym=lval->sym; + if (lval->ident==iARRAYCELL) { + /* store at address in ALT */ + stgwrite("\tstor.i\n"); + code_idx+=opcodes(1); + } else if (lval->ident==iARRAYCHAR) { + /* store at address in ALT */ + stgwrite("\tstrb.i "); + outval(sCHARBITS/8,TRUE); /* write one or two bytes */ + code_idx+=opcodes(1)+opargs(1); + } else if (lval->ident==iREFERENCE) { + assert(sym!=NULL); + if (sym->vclass==sLOCAL) + stgwrite("\tsref.s.pri "); + else + stgwrite("\tsref.pri "); + outval(sym->addr,TRUE); + code_idx+=opcodes(1)+opargs(1); + } else { + assert(sym!=NULL); + markusage(sym,uWRITTEN); + if (sym->vclass==sLOCAL) + stgwrite("\tstor.s.pri "); + else + stgwrite("\tstor.pri "); + outval(sym->addr,TRUE); + code_idx+=opcodes(1)+opargs(1); + } /* if */ +} + +SC_FUNC void storereg(cell address,regid reg) +{ + assert(reg==sPRI || reg==sALT); + if (reg==sPRI) + stgwrite("\tstor.pri "); + else + stgwrite("\tstor.alt "); + outval(address,TRUE); + code_idx+=opcodes(1)+opargs(1); +} + +/* source must in PRI, destination address in ALT. The "size" + * parameter is in bytes, not cells. + */ +SC_FUNC void memcopy(cell size) +{ + stgwrite("\tmovs "); + outval(size,TRUE); + + code_idx+=opcodes(1)+opargs(1); +} + +/* Address of the source must already have been loaded in PRI + * "size" is the size in bytes (not cells). + */ +SC_FUNC void copyarray(symbol *sym,cell size) +{ + assert(sym!=NULL); + /* the symbol can be a local array, a global array, or an array + * that is passed by reference. + */ + if (sym->ident==iREFARRAY) { + /* reference to an array; currently this is always a local variable */ + assert(sym->vclass==sLOCAL); /* symbol must be stack relative */ + stgwrite("\tload.s.alt "); + } else { + /* a local or global array */ + if (sym->vclass==sLOCAL) + stgwrite("\taddr.alt "); + else + stgwrite("\tconst.alt "); + } /* if */ + outval(sym->addr,TRUE); + markusage(sym,uWRITTEN); + + code_idx+=opcodes(1)+opargs(1); + memcopy(size); +} + +SC_FUNC void fillarray(symbol *sym,cell size,cell value) +{ + ldconst(value,sPRI); /* load value in PRI */ + + assert(sym!=NULL); + /* the symbol can be a local array, a global array, or an array + * that is passed by reference. + */ + if (sym->ident==iREFARRAY) { + /* reference to an array; currently this is always a local variable */ + assert(sym->vclass==sLOCAL); /* symbol must be stack relative */ + stgwrite("\tload.s.alt "); + } else { + /* a local or global array */ + if (sym->vclass==sLOCAL) + stgwrite("\taddr.alt "); + else + stgwrite("\tconst.alt "); + } /* if */ + outval(sym->addr,TRUE); + markusage(sym,uWRITTEN); + + assert(size>0); + stgwrite("\tfill "); + outval(size,TRUE); + + code_idx+=opcodes(2)+opargs(2); +} + +/* Instruction to get an immediate value into the primary or the alternate + * register + */ +SC_FUNC void ldconst(cell val,regid reg) +{ + assert(reg==sPRI || reg==sALT); + switch (reg) { + case sPRI: + if (val==0) { + stgwrite("\tzero.pri\n"); + code_idx+=opcodes(1); + } else { + stgwrite("\tconst.pri "); + outval(val, TRUE); + code_idx+=opcodes(1)+opargs(1); + } /* if */ + break; + case sALT: + if (val==0) { + stgwrite("\tzero.alt\n"); + code_idx+=opcodes(1); + } else { + stgwrite("\tconst.alt "); + outval(val, TRUE); + code_idx+=opcodes(1)+opargs(1); + } /* if */ + break; + } /* switch */ +} + +/* Copy value in alternate register to the primary register */ +SC_FUNC void moveto1(void) +{ + stgwrite("\tmove.pri\n"); + code_idx+=opcodes(1)+opargs(0); +} + +/* Push primary or the alternate register onto the stack + */ +SC_FUNC void pushreg(regid reg) +{ + assert(reg==sPRI || reg==sALT); + switch (reg) { + case sPRI: + stgwrite("\tpush.pri\n"); + break; + case sALT: + stgwrite("\tpush.alt\n"); + break; + } /* switch */ + code_idx+=opcodes(1); +} + +/* + * Push a constant value onto the stack + */ +SC_FUNC void pushval(cell val) +{ + stgwrite("\tpush.c "); + outval(val, TRUE); + code_idx+=opcodes(1)+opargs(1); +} + +/* Pop stack into the primary or the alternate register + */ +SC_FUNC void popreg(regid reg) +{ + assert(reg==sPRI || reg==sALT); + switch (reg) { + case sPRI: + stgwrite("\tpop.pri\n"); + break; + case sALT: + stgwrite("\tpop.alt\n"); + break; + } /* switch */ + code_idx+=opcodes(1); +} + +/* + * swap the top-of-stack with the value in primary register + */ +SC_FUNC void swap1(void) +{ + stgwrite("\tswap.pri\n"); + code_idx+=opcodes(1); +} + +/* Switch statements + * The "switch" statement generates a "case" table using the "CASE" opcode. + * The case table contains a list of records, each record holds a comparison + * value and a label to branch to on a match. The very first record is an + * exception: it holds the size of the table (excluding the first record) and + * the label to branch to when none of the values in the case table match. + * The case table is sorted on the comparison value. This allows more advanced + * abstract machines to sift the case table with a binary search. + */ +SC_FUNC void ffswitch(int label) +{ + stgwrite("\tswitch "); + outval(label,TRUE); /* the label is the address of the case table */ + code_idx+=opcodes(1)+opargs(1); +} + +SC_FUNC void ffcase(cell value,char *labelname,int newtable) +{ + if (newtable) { + stgwrite("\tcasetbl\n"); + code_idx+=opcodes(1); + } /* if */ + stgwrite("\tcase "); + outval(value,FALSE); + stgwrite(" "); + stgwrite(labelname); + stgwrite("\n"); + code_idx+=opcodes(0)+opargs(2); +} + +/* + * Call specified function + */ +SC_FUNC void ffcall(symbol *sym,const char *label,int numargs) +{ + char symname[2*sNAMEMAX+16]; + + assert(sym!=NULL); + assert(sym->ident==iFUNCTN); + if (sc_asmfile) + funcdisplayname(symname,sym->name); + if ((sym->usage & uNATIVE)!=0) { + /* reserve a SYSREQ id if called for the first time */ + assert(label==NULL); + if (sc_status==statWRITE && (sym->usage & uREAD)==0 && sym->addr>=0) + sym->addr=ntv_funcid++; + stgwrite("\tsysreq.c "); + outval(sym->addr,FALSE); + if (sc_asmfile) { + stgwrite("\t; "); + stgwrite(symname); + } /* if */ + stgwrite("\n\tstack "); + outval((numargs+1)*sizeof(cell), TRUE); + code_idx+=opcodes(2)+opargs(2); + } else { + /* normal function */ + stgwrite("\tcall "); + if (label!=NULL) { + stgwrite("l."); + stgwrite(label); + } else { + stgwrite(sym->name); + } /* if */ + if (sc_asmfile + && (label!=NULL || !isalpha(sym->name[0]) && sym->name[0]!='_' && sym->name[0]!=sc_ctrlchar)) + { + stgwrite("\t; "); + stgwrite(symname); + } /* if */ + stgwrite("\n"); + code_idx+=opcodes(1)+opargs(1); + } /* if */ +} + +/* Return from function + * + * Global references: funcstatus (referred to only) + */ +SC_FUNC void ffret(void) +{ + stgwrite("\tretn\n"); + code_idx+=opcodes(1); +} + +SC_FUNC void ffabort(int reason) +{ + stgwrite("\thalt "); + outval(reason,TRUE); + code_idx+=opcodes(1)+opargs(1); +} + +SC_FUNC void ffbounds(cell size) +{ + if ((sc_debug & sCHKBOUNDS)!=0) { + stgwrite("\tbounds "); + outval(size,TRUE); + code_idx+=opcodes(1)+opargs(1); + } /* if */ +} + +/* + * Jump to local label number (the number is converted to a name) + */ +SC_FUNC void jumplabel(int number) +{ + stgwrite("\tjump "); + outval(number,TRUE); + code_idx+=opcodes(1)+opargs(1); +} + +/* + * Define storage (global and static variables) + */ +SC_FUNC void defstorage(void) +{ + stgwrite("dump "); +} + +/* + * Inclrement/decrement stack pointer. Note that this routine does + * nothing if the delta is zero. + */ +SC_FUNC void modstk(int delta) +{ + if (delta) { + stgwrite("\tstack "); + outval(delta, TRUE); + code_idx+=opcodes(1)+opargs(1); + } /* if */ +} + +/* set the stack to a hard offset from the frame */ +SC_FUNC void setstk(cell value) +{ + stgwrite("\tlctrl 5\n"); /* get FRM in PRI */ + assert(value<=0); /* STK should always become <= FRM */ + if (value<0) { + stgwrite("\tadd.c "); + outval(value, TRUE); /* add (negative) offset */ + code_idx+=opcodes(1)+opargs(1); + // ??? write zeros in the space between STK and the value in PRI (the new stk) + // get value of STK in ALT + // zero PRI + // need new FILL opcode that takes a variable size + } /* if */ + stgwrite("\tsctrl 4\n"); /* store in STK */ + code_idx+=opcodes(2)+opargs(2); +} + +SC_FUNC void modheap(int delta) +{ + if (delta) { + stgwrite("\theap "); + outval(delta, TRUE); + code_idx+=opcodes(1)+opargs(1); + } /* if */ +} + +SC_FUNC void setheap_pri(void) +{ + stgwrite("\theap "); /* ALT = HEA++ */ + outval(sizeof(cell), TRUE); + stgwrite("\tstor.i\n"); /* store PRI (default value) at address ALT */ + stgwrite("\tmove.pri\n"); /* move ALT to PRI: PRI contains the address */ + code_idx+=opcodes(3)+opargs(1); +} + +SC_FUNC void setheap(cell value) +{ + stgwrite("\tconst.pri "); /* load default value in PRI */ + outval(value, TRUE); + code_idx+=opcodes(1)+opargs(1); + setheap_pri(); +} + +/* + * Convert a cell number to a "byte" address; i.e. double or quadruple + * the primary register. + */ +SC_FUNC void cell2addr(void) +{ + #if PAWN_CELL_SIZE==16 + stgwrite("\tshl.c.pri 1\n"); + #elif PAWN_CELL_SIZE==32 + stgwrite("\tshl.c.pri 2\n"); + #elif PAWN_CELL_SIZE==64 + stgwrite("\tshl.c.pri 3\n"); + #else + #error Unsupported cell size + #endif + code_idx+=opcodes(1)+opargs(1); +} + +/* + * Double or quadruple the alternate register. + */ +SC_FUNC void cell2addr_alt(void) +{ + #if PAWN_CELL_SIZE==16 + stgwrite("\tshl.c.alt 1\n"); + #elif PAWN_CELL_SIZE==32 + stgwrite("\tshl.c.alt 2\n"); + #elif PAWN_CELL_SIZE==64 + stgwrite("\tshl.c.alt 3\n"); + #else + #error Unsupported cell size + #endif + code_idx+=opcodes(1)+opargs(1); +} + +/* + * Convert "distance of addresses" to "number of cells" in between. + * Or convert a number of packed characters to the number of cells (with + * truncation). + */ +SC_FUNC void addr2cell(void) +{ + #if PAWN_CELL_SIZE==16 + stgwrite("\tshr.c.pri 1\n"); + #elif PAWN_CELL_SIZE==32 + stgwrite("\tshr.c.pri 2\n"); + #elif PAWN_CELL_SIZE==64 + stgwrite("\tshr.c.pri 3\n"); + #else + #error Unsupported cell size + #endif + code_idx+=opcodes(1)+opargs(1); +} + +/* Convert from character index to byte address. This routine does + * nothing if a character has the size of a byte. + */ +SC_FUNC void char2addr(void) +{ + #if sCHARBITS==16 + stgwrite("\tshl.c.pri 1\n"); + code_idx+=opcodes(1)+opargs(1); + #endif +} + +/* Align PRI (which should hold a character index) to an address. + * The first character in a "pack" occupies the highest bits of + * the cell. This is at the lower memory address on Big Endian + * computers and on the higher address on Little Endian computers. + * The ALIGN.pri/alt instructions must solve this machine dependence; + * that is, on Big Endian computers, ALIGN.pri/alt shuold do nothing + * and on Little Endian computers they should toggle the address. + */ +SC_FUNC void charalign(void) +{ + stgwrite("\talign.pri "); + outval(sCHARBITS/8,TRUE); + code_idx+=opcodes(1)+opargs(1); +} + +/* + * Add a constant to the primary register. + */ +SC_FUNC void addconst(cell value) +{ + if (value!=0) { + stgwrite("\tadd.c "); + outval(value,TRUE); + code_idx+=opcodes(1)+opargs(1); + } /* if */ +} + +/* + * signed multiply of primary and secundairy registers (result in primary) + */ +SC_FUNC void os_mult(void) +{ + stgwrite("\tsmul\n"); + code_idx+=opcodes(1); +} + +/* + * signed divide of alternate register by primary register (quotient in + * primary; remainder in alternate) + */ +SC_FUNC void os_div(void) +{ + stgwrite("\tsdiv.alt\n"); + code_idx+=opcodes(1); +} + +/* + * modulus of (alternate % primary), result in primary (signed) + */ +SC_FUNC void os_mod(void) +{ + stgwrite("\tsdiv.alt\n"); + stgwrite("\tmove.pri\n"); /* move ALT to PRI */ + code_idx+=opcodes(2); +} + +/* + * Add primary and alternate registers (result in primary). + */ +SC_FUNC void ob_add(void) +{ + stgwrite("\tadd\n"); + code_idx+=opcodes(1); +} + +/* + * subtract primary register from alternate register (result in primary) + */ +SC_FUNC void ob_sub(void) +{ + stgwrite("\tsub.alt\n"); + code_idx+=opcodes(1); +} + +/* + * arithmic shift left alternate register the number of bits + * given in the primary register (result in primary). + * There is no need for a "logical shift left" routine, since + * logical shift left is identical to arithmic shift left. + */ +SC_FUNC void ob_sal(void) +{ + stgwrite("\txchg\n"); + stgwrite("\tshl\n"); + code_idx+=opcodes(2); +} + +/* + * arithmic shift right alternate register the number of bits + * given in the primary register (result in primary). + */ +SC_FUNC void os_sar(void) +{ + stgwrite("\txchg\n"); + stgwrite("\tsshr\n"); + code_idx+=opcodes(2); +} + +/* + * logical (unsigned) shift right of the alternate register by the + * number of bits given in the primary register (result in primary). + */ +SC_FUNC void ou_sar(void) +{ + stgwrite("\txchg\n"); + stgwrite("\tshr\n"); + code_idx+=opcodes(2); +} + +/* + * inclusive "or" of primary and alternate registers (result in primary) + */ +SC_FUNC void ob_or(void) +{ + stgwrite("\tor\n"); + code_idx+=opcodes(1); +} + +/* + * "exclusive or" of primary and alternate registers (result in primary) + */ +SC_FUNC void ob_xor(void) +{ + stgwrite("\txor\n"); + code_idx+=opcodes(1); +} + +/* + * "and" of primary and secundairy registers (result in primary) + */ +SC_FUNC void ob_and(void) +{ + stgwrite("\tand\n"); + code_idx+=opcodes(1); +} + +/* + * test ALT==PRI; result in primary register (1 or 0). + */ +SC_FUNC void ob_eq(void) +{ + stgwrite("\teq\n"); + code_idx+=opcodes(1); +} + +/* + * test ALT!=PRI + */ +SC_FUNC void ob_ne(void) +{ + stgwrite("\tneq\n"); + code_idx+=opcodes(1); +} + +/* The abstract machine defines the relational instructions so that PRI is + * on the left side and ALT on the right side of the operator. For example, + * SLESS sets PRI to either 1 or 0 depending on whether the expression + * "PRI < ALT" is true. + * + * The compiler generates comparisons with ALT on the left side of the + * relational operator and PRI on the right side. The XCHG instruction + * prefixing the relational operators resets this. We leave it to the + * peephole optimizer to choose more compact instructions where possible. + */ + +/* Relational operator prefix for chained relational expressions. The + * "suffix" code restores the stack. + * For chained relational operators, the goal is to keep the comparison + * result "so far" in PRI and the value of the most recent operand in + * ALT, ready for a next comparison. + * The "prefix" instruction pushed the comparison result (PRI) onto the + * stack and moves the value of ALT into PRI. If there is a next comparison, + * PRI can now serve as the "left" operand of the relational operator. + */ +SC_FUNC void relop_prefix(void) +{ + stgwrite("\tpush.pri\n"); + stgwrite("\tmove.pri\n"); + code_idx+=opcodes(2); +} + +SC_FUNC void relop_suffix(void) +{ + stgwrite("\tswap.alt\n"); + stgwrite("\tand\n"); + stgwrite("\tpop.alt\n"); + code_idx+=opcodes(3); +} + +/* + * test ALTPRI (signed) + */ +SC_FUNC void os_gt(void) +{ + stgwrite("\txchg\n"); + stgwrite("\tsgrtr\n"); + code_idx+=opcodes(2); +} + +/* + * test ALT>=PRI (signed) + */ +SC_FUNC void os_ge(void) +{ + stgwrite("\txchg\n"); + stgwrite("\tsgeq\n"); + code_idx+=opcodes(2); +} + +/* + * logical negation of primary register + */ +SC_FUNC void lneg(void) +{ + stgwrite("\tnot\n"); + code_idx+=opcodes(1); +} + +/* + * two's complement primary register + */ +SC_FUNC void neg(void) +{ + stgwrite("\tneg\n"); + code_idx+=opcodes(1); +} + +/* + * one's complement of primary register + */ +SC_FUNC void invert(void) +{ + stgwrite("\tinvert\n"); + code_idx+=opcodes(1); +} + +/* + * nop + */ +SC_FUNC void nooperation(void) +{ + stgwrite("\tnop\n"); + code_idx+=opcodes(1); +} + + +/* increment symbol + */ +SC_FUNC void inc(value *lval) +{ + symbol *sym; + + sym=lval->sym; + if (lval->ident==iARRAYCELL) { + /* indirect increment, address already in PRI */ + stgwrite("\tinc.i\n"); + code_idx+=opcodes(1); + } else if (lval->ident==iARRAYCHAR) { + /* indirect increment of single character, address already in PRI */ + stgwrite("\tpush.pri\n"); + stgwrite("\tpush.alt\n"); + stgwrite("\tmove.alt\n"); /* copy address */ + stgwrite("\tlodb.i "); /* read from PRI into PRI */ + outval(sCHARBITS/8,TRUE); /* read one or two bytes */ + stgwrite("\tinc.pri\n"); + stgwrite("\tstrb.i "); /* write PRI to ALT */ + outval(sCHARBITS/8,TRUE); /* write one or two bytes */ + stgwrite("\tpop.alt\n"); + stgwrite("\tpop.pri\n"); + code_idx+=opcodes(8)+opargs(2); + } else if (lval->ident==iREFERENCE) { + assert(sym!=NULL); + stgwrite("\tpush.pri\n"); + /* load dereferenced value */ + assert(sym->vclass==sLOCAL); /* global references don't exist in Pawn */ + if (sym->vclass==sLOCAL) + stgwrite("\tlref.s.pri "); + else + stgwrite("\tlref.pri "); + outval(sym->addr,TRUE); + /* increment */ + stgwrite("\tinc.pri\n"); + /* store dereferenced value */ + if (sym->vclass==sLOCAL) + stgwrite("\tsref.s.pri "); + else + stgwrite("\tsref.pri "); + outval(sym->addr,TRUE); + stgwrite("\tpop.pri\n"); + code_idx+=opcodes(5)+opargs(2); + } else { + /* local or global variable */ + assert(sym!=NULL); + if (sym->vclass==sLOCAL) + stgwrite("\tinc.s "); + else + stgwrite("\tinc "); + outval(sym->addr,TRUE); + code_idx+=opcodes(1)+opargs(1); + } /* if */ +} + +/* decrement symbol + * + * in case of an integer pointer, the symbol must be incremented by 2. + */ +SC_FUNC void dec(value *lval) +{ + symbol *sym; + + sym=lval->sym; + if (lval->ident==iARRAYCELL) { + /* indirect decrement, address already in PRI */ + stgwrite("\tdec.i\n"); + code_idx+=opcodes(1); + } else if (lval->ident==iARRAYCHAR) { + /* indirect decrement of single character, address already in PRI */ + stgwrite("\tpush.pri\n"); + stgwrite("\tpush.alt\n"); + stgwrite("\tmove.alt\n"); /* copy address */ + stgwrite("\tlodb.i "); /* read from PRI into PRI */ + outval(sCHARBITS/8,TRUE); /* read one or two bytes */ + stgwrite("\tdec.pri\n"); + stgwrite("\tstrb.i "); /* write PRI to ALT */ + outval(sCHARBITS/8,TRUE); /* write one or two bytes */ + stgwrite("\tpop.alt\n"); + stgwrite("\tpop.pri\n"); + code_idx+=opcodes(8)+opargs(2); + } else if (lval->ident==iREFERENCE) { + assert(sym!=NULL); + stgwrite("\tpush.pri\n"); + /* load dereferenced value */ + assert(sym->vclass==sLOCAL); /* global references don't exist in Pawn */ + if (sym->vclass==sLOCAL) + stgwrite("\tlref.s.pri "); + else + stgwrite("\tlref.pri "); + outval(sym->addr,TRUE); + /* decrement */ + stgwrite("\tdec.pri\n"); + /* store dereferenced value */ + if (sym->vclass==sLOCAL) + stgwrite("\tsref.s.pri "); + else + stgwrite("\tsref.pri "); + outval(sym->addr,TRUE); + stgwrite("\tpop.pri\n"); + code_idx+=opcodes(5)+opargs(2); + } else { + /* local or global variable */ + assert(sym!=NULL); + if (sym->vclass==sLOCAL) + stgwrite("\tdec.s "); + else + stgwrite("\tdec "); + outval(sym->addr,TRUE); + code_idx+=opcodes(1)+opargs(1); + } /* if */ +} + +/* + * Jumps to "label" if PRI != 0 + */ +SC_FUNC void jmp_ne0(int number) +{ + stgwrite("\tjnz "); + outval(number,TRUE); + code_idx+=opcodes(1)+opargs(1); +} + +/* + * Jumps to "label" if PRI == 0 + */ +SC_FUNC void jmp_eq0(int number) +{ + stgwrite("\tjzer "); + outval(number,TRUE); + code_idx+=opcodes(1)+opargs(1); +} + +/* write a value in hexadecimal; optionally adds a newline */ +SC_FUNC void outval(cell val,int newline) +{ + stgwrite(itoh(val)); + if (newline) + stgwrite("\n"); +} diff --git a/compiler/libpc300/sc5.c b/compiler/libpc300/sc5.c new file mode 100755 index 00000000..eb5eef45 --- /dev/null +++ b/compiler/libpc300/sc5.c @@ -0,0 +1,217 @@ +/* Pawn compiler - Error message system + * In fact a very simple system, using only 'panic mode'. + * + * Copyright (c) ITB CompuPhase, 1997-2005 + * + * This software is provided "as-is", without any express or implied warranty. + * In no event will the authors be held liable for any damages arising from + * the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software in + * a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * 3. This notice may not be removed or altered from any source distribution. + * + * Version: $Id$ + */ +#include +#if defined __WIN32__ || defined _WIN32 || defined __MSDOS__ + #include +#endif +#if defined LINUX || defined __GNUC__ + #include +#endif +#include +#include +#include /* ANSI standardized variable argument list functions */ +#include +#if defined FORTIFY + #include "fortify.h" +#endif +#include "sc.h" + +#if defined _MSC_VER + #pragma warning(push) + #pragma warning(disable:4125) /* decimal digit terminates octal escape sequence */ +#endif + +#include "sc5.scp" + +#if defined _MSC_VER + #pragma warning(pop) +#endif + +#define NUM_WARNINGS (sizeof warnmsg / sizeof warnmsg[0]) +static unsigned char warndisable[(NUM_WARNINGS + 7) / 8]; /* 8 flags in a char */ + +static int errflag; +static int errstart; /* line number at which the instruction started */ + +/* error + * + * Outputs an error message (note: msg is passed optionally). + * If an error is found, the variable "errflag" is set and subsequent + * errors are ignored until lex() finds a semicolumn or a keyword + * (lex() resets "errflag" in that case). + * + * Global references: inpfname (reffered to only) + * fline (reffered to only) + * fcurrent (reffered to only) + * errflag (altered) + */ +SC_FUNC int error(int number,...) +{ +static char *prefix[3]={ "error", "fatal error", "warning" }; +static int lastline,errorcount; +static short lastfile; + char *msg,*pre; + va_list argptr; + char string[128]; + + /* errflag is reset on each semicolon. + * In a two-pass compiler, an error should not be reported twice. Therefore + * the error reporting is enabled only in the second pass (and only when + * actually producing output). Fatal errors may never be ignored. + */ + if ((errflag || sc_status!=statWRITE) && (number<100 || number>=200)) + return 0; + + /* also check for disabled warnings */ + if (number>=200) { + int index=(number-200)/8; + int mask=1 << ((number-200)%8); + if ((warndisable[index] & mask)!=0) + return 0; + } /* if */ + + if (number<100){ + msg=errmsg[number-1]; + pre=prefix[0]; + errflag=TRUE; /* set errflag (skip rest of erroneous expression) */ + errnum++; + } else if (number<200){ + msg=fatalmsg[number-100]; + pre=prefix[1]; + errnum++; /* a fatal error also counts as an error */ + } else { + msg=warnmsg[number-200]; + pre=prefix[2]; + warnnum++; + } /* if */ + + strexpand(string,(unsigned char *)msg,sizeof string,SCPACK_TABLE); + + assert(errstart<=fline); + va_start(argptr,number); + if (strlen(errfname)==0) { + int start= (errstart==fline) ? -1 : errstart; + if (pc_error(number,string,inpfname,start,fline,argptr)) { + if (outf!=NULL) { + pc_closeasm(outf,TRUE); + outf=NULL; + } /* if */ + longjmp(errbuf,3); /* user abort */ + } /* if */ + } else { + FILE *fp=fopen(errfname,"a"); + if (fp!=NULL) { + if (errstart>=0 && errstart!=fline) + fprintf(fp,"%s(%d -- %d) : %s %03d: ",inpfname,errstart,fline,pre,number); + else + fprintf(fp,"%s(%d) : %s %03d: ",inpfname,fline,pre,number); + vfprintf(fp,string,argptr); + fclose(fp); + } /* if */ + } /* if */ + va_end(argptr); + + if (number>=100 && number<200 || errnum>25){ + if (strlen(errfname)==0) { + va_start(argptr,number); + pc_error(0,"\nCompilation aborted.",NULL,0,0,argptr); + va_end(argptr); + } /* if */ + if (outf!=NULL) { + pc_closeasm(outf,TRUE); + outf=NULL; + } /* if */ + longjmp(errbuf,2); /* fatal error, quit */ + } /* if */ + + /* check whether we are seeing many errors on the same line */ + if ((errstart<0 && lastline!=fline) || lastlinefline || fcurrent!=lastfile) + errorcount=0; + lastline=fline; + lastfile=fcurrent; + if (number<200) + errorcount++; + if (errorcount>=3) + error(107); /* too many error/warning messages on one line */ + + return 0; +} + +SC_FUNC void errorset(int code) +{ + switch (code) { + case sRESET: + errflag=FALSE; /* start reporting errors */ + break; + case sFORCESET: + errflag=TRUE; /* stop reporting errors */ + break; + case sEXPRMARK: + errstart=fline; /* save start line number */ + break; + case sEXPRRELEASE: + errstart=-1; /* forget start line number */ + break; + } /* switch */ +} + +/* sc_enablewarning() + * Enables or disables a warning (errors cannot be disabled). + * Initially all warnings are enabled. The compiler does this by setting bits + * for the *disabled* warnings and relying on the array to be zero-initialized. + * + * Parameter enable can be: + * o 0 for disable + * o 1 for enable + * o 2 for toggle + */ +int pc_enablewarning(int number,int enable) +{ + int index; + unsigned char mask; + + if (number<200) + return FALSE; /* errors and fatal errors cannot be disabled */ + number -= 200; + if (number>=NUM_WARNINGS) + return FALSE; + + index=number/8; + mask=(unsigned char)(1 << (number%8)); + switch (enable) { + case 0: + warndisable[index] |= mask; + break; + case 1: + warndisable[index] &= (unsigned char)~mask; + break; + case 2: + warndisable[index] ^= mask; + break; + } /* switch */ + + return TRUE; +} + +#undef SCPACK_TABLE diff --git a/compiler/libpc300/sc5.scp b/compiler/libpc300/sc5.scp new file mode 100755 index 00000000..8c5fcae9 --- /dev/null +++ b/compiler/libpc300/sc5.scp @@ -0,0 +1,326 @@ +/* Pawn compiler - Error message strings (plain and compressed formats) + * + * Copyright (c) ITB CompuPhase, 2000-2005 + * + * This software is provided "as-is", without any express or implied warranty. + * In no event will the authors be held liable for any damages arising from + * the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software in + * a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * 3. This notice may not be removed or altered from any source distribution. + * + * Version: $Id$ + */ + +SC_FUNC int strexpand(char *dest, unsigned char *source, int maxlen, unsigned char pairtable[128][2]); + +#define SCPACK_TABLE errstr_table +/*-*SCPACK start of pair table, do not change or remove this line */ +unsigned char errstr_table[][2] = { + {101,32}, {111,110}, {116,32}, {105,110}, {97,114}, {116,105}, {100,32}, {115,32}, {101,110}, {101,114}, {97,108}, {133,129}, {37,115}, {34,140}, {141,34}, {117,110}, + {115,105}, {110,111}, {114,101}, {121,32}, {97,110}, {111,114}, {97,116}, {109,136}, {100,101}, {139,32}, {115,116}, {101,134}, {109,98}, {41,10}, {118,138}, {114,97}, + {143,99}, {145,130}, {32,142}, {102,160}, {117,115}, {98,108}, {111,108}, {97,32}, {115,144}, {163,153}, {105,134}, {132,159}, {149,32}, {158,170}, {115,121}, {117,151}, + {174,156}, {103,32}, {176,166}, {103,175}, {131,173}, {132,179}, {116,104}, {101,120}, {137,32}, {101,100}, {111,102}, {99,104}, {105,135}, {171,147}, {152,102}, {116,111}, + {186,32}, {97,165}, {99,129}, {181,130}, {101,10}, {115,10}, {112,146}, {164,130}, {98,128}, {162,10}, {44,32}, {109,199}, {109,97}, {193,128}, {34,32}, {129,32}, + {116,97}, {100,105}, {144,122}, {190,131}, {118,132}, {101,135}, {154,150}, {102,105}, {212,105}, {101,108}, {117,108}, {99,111}, {109,150}, {58,201}, {216,205}, {132,97}, + {119,105}, {111,112}, {97,115}, {225,137}, {194,154}, {178,162}, {110,32}, {138,32}, {148,32}, {198,168}, {131,177}, {203,200}, {104,97}, {183,233}, {108,111}, {109,112}, + {148,130}, {108,128}, {229,157}, {142,157}, {40,242}, {110,117}, {118,128}, {116,137}, {116,10}, {102,149}, {227,150}, {101,151}, {245,156}, {111,32}, {145,32} +}; +/*-*SCPACK end of pair table, do not change or remove this line */ + +static char *errmsg[] = { +#ifdef SCPACK +/*001*/ "expected token: \"%s\", but found \"%s\"\n", +/*002*/ "only a single statement (or expression) can follow each \"case\"\n", +/*003*/ "declaration of a local variable must appear in a compound block\n", +/*004*/ "function \"%s\" is not implemented\n", +/*005*/ "function may not have arguments\n", +/*006*/ "must be assigned to an array\n", +/*007*/ "operator cannot be redefined\n", +/*008*/ "must be a constant expression; assumed zero\n", +/*009*/ "invalid array size (negative or zero)\n", +/*010*/ "invalid function or declaration\n", +/*011*/ "invalid outside functions\n", +/*012*/ "invalid function call, not a valid address\n", +/*013*/ "no entry point (no public functions)\n", +/*014*/ "invalid statement; not in switch\n", +/*015*/ "\"default\" case must be the last case in switch statement\n", +/*016*/ "multiple defaults in \"switch\"\n", +/*017*/ "undefined symbol \"%s\"\n", +/*018*/ "initialization data exceeds declared size\n", +/*019*/ "not a label: \"%s\"\n", +/*020*/ "invalid symbol name \"%s\"\n", +/*021*/ "symbol already defined: \"%s\"\n", +/*022*/ "must be lvalue (non-constant)\n", +/*023*/ "array assignment must be simple assignment\n", +/*024*/ "\"break\" or \"continue\" is out of context\n", +/*025*/ "function heading differs from prototype\n", +/*026*/ "no matching \"#if...\"\n", +/*027*/ "invalid character constant\n", +/*028*/ "invalid subscript (not an array or too many subscripts): \"%s\"\n", +/*029*/ "invalid expression, assumed zero\n", +/*030*/ "compound statement not closed at the end of file\n", +/*031*/ "unknown directive\n", +/*032*/ "array index out of bounds (variable \"%s\")\n", +/*033*/ "array must be indexed (variable \"%s\")\n", +/*034*/ "argument does not have a default value (argument %d)\n", +/*035*/ "argument type mismatch (argument %d)\n", +/*036*/ "empty statement\n", +/*037*/ "invalid string (possibly non-terminated string)\n", +/*038*/ "extra characters on line\n", +/*039*/ "constant symbol has no size\n", +/*040*/ "duplicate \"case\" label (value %d)\n", +/*041*/ "invalid ellipsis, array size is not known\n", +/*042*/ "invalid combination of class specifiers\n", +/*043*/ "character constant exceeds range for packed string\n", +/*044*/ "positional parameters must precede all named parameters\n", +/*045*/ "too many function arguments\n", +/*046*/ "unknown array size (variable \"%s\")\n", +/*047*/ "array sizes do not match, or destination array is too small\n", +/*048*/ "array dimensions do not match\n", +/*049*/ "invalid line continuation\n", +/*050*/ "invalid range\n", +/*051*/ "invalid subscript, use \"[ ]\" operators on major dimensions\n", +/*052*/ "multi-dimensional arrays must be fully initialized\n", +/*053*/ "exceeding maximum number of dimensions\n", +/*054*/ "unmatched closing brace\n", +/*055*/ "start of function body without function header\n", +/*056*/ "arrays, local variables and function arguments cannot be public (variable \"%s\")\n", +/*057*/ "unfinished expression before compiler directive\n", +/*058*/ "duplicate argument; same argument is passed twice\n", +/*059*/ "function argument may not have a default value (variable \"%s\")\n", +/*060*/ "multiple \"#else\" directives between \"#if ... #endif\"\n", +/*061*/ "\"#elseif\" directive follows an \"#else\" directive\n", +/*062*/ "number of operands does not fit the operator\n", +/*063*/ "function result tag of operator \"%s\" must be \"%s\"\n", +/*064*/ "cannot change predefined operators\n", +/*065*/ "function argument may only have a single tag (argument %d)\n", +/*066*/ "function argument may not be a reference argument or an array (argument \"%s\")\n", +/*067*/ "variable cannot be both a reference and an array (variable \"%s\")\n", +/*068*/ "invalid rational number precision in #pragma\n", +/*069*/ "rational number format already defined\n", +/*070*/ "rational number support was not enabled\n", +/*071*/ "user-defined operator must be declared before use (function \"%s\")\n", +/*072*/ "\"sizeof\" operator is invalid on \"function\" symbols\n", +/*073*/ "function argument must be an array (argument \"%s\")\n", +/*074*/ "#define pattern must start with an alphabetic character\n", +/*075*/ "input line too long (after substitutions)\n", +/*076*/ "syntax error in the expression, or invalid function call\n", +/*077*/ "malformed UTF-8 encoding, or corrupted file: %s\n", +/*078*/ "function uses both \"return\" and \"return \"\n", +/*079*/ "inconsistent return types (array & non-array)\n", +/*080*/ "unknown symbol, or not a constant symbol (symbol \"%s\")\n", +/*081*/ "cannot take a tag as a default value for an indexed array parameter (symbol \"%s\")\n", +/*082*/ "user-defined operators and native functions may not have states\n", +/*083*/ "a function may only belong to a single automaton (symbol \"%s\")\n", +/*084*/ "state conflict: one of the states is already assigned to another implementation (symbol \"%s\")\n", +/*085*/ "no states are defined for function \"%s\"\n", +/*086*/ "unknown automaton \"%s\"\n", +/*087*/ "unknown state \"%s\" for automaton \"%s\"\n" +#else + "\267pect\233\277k\210:\242\312bu\202fo\217\206\216\012", + "\201l\223\247s\203g\361\326\373\202(\254\355\201) c\350f\246\356w ea\273 \042c\342e\042\012", + "\230cl\337\231\300\247\356c\347\336\313appe\204 \203 \247\333\357o\217\206\245ock\012", + "\251\216 \274\241i\357l\373t\271\012", + "\251\314\223\241\354\366\265t\305", + "\353a\250gn\233\277 \350\253y\012", + "\372\254c\224\241\310\222\323\271\012", + "\353\247\344\360\355\201; \342sum\233z\211o\012", + "\264\275\322\200(nega\205\366\254z\211o\235", + "\264\251\254\230cl\337\213\012", + "\264out\220d\200\243\213\305", + "\264\251c\212l\312\241\247\255add\222s\305", + "\376\210tr\223po\203\202(\376pu\245ic \243\213s\235", + "\264\326\373t; \241\203 s\340t\273\012", + "\042\276a\332t\316c\342\200\353\266\200l\342\202c\342\200\203 s\340t\273 \326\373\370", + "m\332\205p\361\276a\332t\207\203 \042s\340t\273\042\012", + "\217\323\233\262\311", + "\203i\205\212iza\231d\226\247\267ce\271\207\230cl\204\233\322\304", + "\241\247lab\331\335", + "\264\262 nam\200\216\012", + "\262 \212\222ad\223\323\271\335", + "\353l\236u\200(n\201-\344\224t\235", + "\275a\250gn\227\202\353\220\357\361a\250gn\227\370", + "\042b\222ak\316\254\042\302t\203ue\316\274ou\202\300\302t\267\370", + "\251head\352\321ff\211\207from pro\277typ\304", + "\376\334\273\352\042#if...\042\012", + "\264\273\337ct\270\344\224\370", + "\264subscrip\202(\241\350\275\254\277\375m\224\223subscripts)\335", + "\264\355\201\312\342sum\233z\211o\012", + "\333\357o\217\206\326\373\202\241c\356s\233a\202\266\200\210\206\300\327l\304", + "\217k\221w\346\321\222c\205v\304", + "\275\203\230x ou\202\300bo\217d\207(\336\363", + "\275\353\203\230x\233(\336\363", + "\303do\325\241\354\366\247\276a\332\202\236u\200(\303%d\235", + "\303typ\200mis\334\273 (\303%d\235", + "e\357t\223\326\373\370", + "\264\232r\352(po\250\245\223n\201-\367m\203\226\233\232r\203g\235", + "\267t\237 \273\337c\367\207\317l\203\304", + "\344\360\262 \354\207\376\322\304", + "duplic\226\200\042c\342e\316lab\331 (\236u\200%d\235", + "\264\331lip\220s\312\275\322\200\274\241k\221wn\012", + "\264\333\234\203a\231\300cl\342\207speci\327\211\305", + "\273\337ct\270\344\360\267ce\271\207r\224g\200f\254pack\233\232r\203g\012", + "po\220\213\347p\337me\367\207\313\306c\271\200\212l nam\233p\337me\367\305", + "\277\375m\224\223\251\265t\305", + "\217k\221w\346\275\322\200(\336\363", + "\275\322\325d\375\241\334\273\312\254\230\232\203a\231\275\274\277\375sm\212l\012", + "\275\321\227\220\201\207d\375\241\334\273\012", + "\264l\203\200\302t\203ua\213\012", + "\264r\224g\304", + "\264subscript\312\244\200\042[ ]\316\372\225\207\317\314j\254\321\227\220\201\305", + "m\332\205-\321\227\220\201\347\253y\207\353f\332l\223\203i\205\212iz\271\012", + "\267ce\271\352\314ximum \374\270\300\321\227\220\201\305", + "\217\334\273\233c\356s\352b\237c\304", + "\232\204\202\300\251bod\223\340\266ou\202\251head\211\012", + "\253ys\312\356c\347\330\301\325\224\206\251\265t\207c\224\241\310pu\245ic (\336\363", + "\217f\203ish\233\355\317be\371\200\333\357il\270\321\222c\205v\304", + "duplic\226\200\265t; sam\200\303\274p\342s\233t\340c\304", + "\251\303\314\223\241\354\366\247\276a\332\202\236u\200(\336\363", + "m\332\205p\361\042#\331se\316\321\222c\205v\325betwe\210 \042#if ... #\210\321f\042\012", + "\042#\331seif\316\321\222c\205\366f\246\356w\207\350\042#\331se\316\321\222c\205v\304", + "\374\270\300\343\224d\207do\325\241\327\202\266\200\372\225\012", + "\251\222s\332\202\320\261\300\372\225\242 \353\216\012", + "c\224\241\273\224g\200\306\323\233\372\225\305", + "\251\303\314\223\201l\223\354\366\247s\203g\361\320\261(\303%d\235", + "\251\303\314\223\241\310\247\222f\211\210c\200\303\254\350\275(\303\363", + "\336c\224\241\310bo\266 \247\222f\211\210c\200\224\206\350\275(\336\363", + "\264\237\213\347\374\270\306ci\220\317\203 #p\237g\314\012", + "\237\213\347\374\270\371\314\202\212\222ad\223\323\271\012", + "\237\213\347\374\270supp\225\202wa\207\241\210\301\271\012", + "\244\211-\323\233\372\254\353\230cl\204\233be\371\200\244\200(\251\363", + "\042\322e\272\316\372\254\274\264\317\042\243\213\316\262\305", + "\251\303\353\350\275(\303\363", + "#\323\200p\226\367\346\313\232\204\202\340\266 \350\212p\354be\205c \273\337c\367\012", + "\203pu\202l\203\200\277\375l\201\261(aft\270subs\205tu\213s\235", + "\256n\320x \211r\254\203 \266\200\355\201\312\254\264\251c\212l\012", + "m\212\371m\233UTF-8 \210\333d\203g\312\254c\225rupt\233\327le: \214\012", + "\251\244\325bo\266 \042\222turn\316\224\206\042\222tur\346<\236ue>\042\012", + "\203\302\220\232\210\202\222tur\346typ\325(\275& n\201-\253y\235", + "\217k\221w\346\262\312\254\241\247\344\360\262 \364", + "c\224\241\320k\200\247\320\261a\207\247\276a\332\202\236u\200f\254\350\203\230x\233\275p\337met\270\364", + "\244\211-\323\233\372\225\207\224\206na\205\366\243\213\207\314\223\241\354\366\326e\305", + "\247\251\314\223\201l\223b\331\201\261\277 \247s\203g\361au\277\334\317\364", + "\326\200\302flict: \201\200\300\266\200\326\325\274\212\222ad\223a\250gn\233\277 a\221\266\270i\357l\373\320\231\364", + "\376\326\325\204\200\323\233f\254\251\216\012", + "\217k\221w\346au\277\334\201\311", + "\217k\221w\346\326\200\216 f\254au\277\334\201\311" +#endif + }; + +static char *fatalmsg[] = { +#ifdef SCPACK +/*100*/ "cannot read from file: \"%s\"\n", +/*101*/ "cannot write to file: \"%s\"\n", +/*102*/ "table overflow: \"%s\"\n", + /* table can be: loop table + * literal table + * staging buffer + * option table (response file) + * peephole optimizer table + */ +/*103*/ "insufficient memory\n", +/*104*/ "invalid assembler instruction \"%s\"\n", +/*105*/ "numeric overflow, exceeding capacity\n", +/*106*/ "compiled script exceeds the maximum memory size (%ld bytes)\n", +/*107*/ "too many error messages on one line\n", +/*108*/ "codepage mapping file not found\n", +/*109*/ "invalid path: \"%s\"\n", +/*110*/ "assertion failed: %s\n", +/*111*/ "user error: %s\n", +#else + "c\224\241\222a\206from \327le\335", + "c\224\241writ\200\277 \327le\335", + "t\315ov\211f\356w\335", + "\203suf\327ci\210\202mem\225y\012", + "\264\342se\234l\270\203\232ruc\231\216\012", + "\365m\211ic ov\211f\356w\312\267ce\271\352capacity\012", + "\333\357il\233scrip\202\267ce\271\207\266\200\314ximum mem\225\223\322\200(%l\206bytes\235", + "\277\375m\224\223\211r\254messag\325\317\201\200l\203\304", + "\333\230pag\200\314pp\352\327\361\241fo\217d\012", + "\264p\226h\335", + "\342s\211\231fail\271: \214\012", + "\244\270\211r\225: \214\012" +#endif + }; + +static char *warnmsg[] = { +#ifdef SCPACK +/*200*/ "symbol \"%s\" is truncated to %d characters\n", +/*201*/ "redefinition of constant/macro (symbol \"%s\")\n", +/*202*/ "number of arguments does not match definition\n", +/*203*/ "symbol is never used: \"%s\"\n", +/*204*/ "symbol is assigned a value that is never used: \"%s\"\n", +/*205*/ "redundant code: constant expression is zero\n", +/*206*/ "redundant test: constant expression is non-zero\n", +/*207*/ "unknown #pragma\n", +/*208*/ "function with tag result used before definition, forcing reparse\n", +/*209*/ "function \"%s\" should return a value\n", +/*210*/ "possible use of symbol before initialization: \"%s\"\n", +/*211*/ "possibly unintended assignment\n", +/*212*/ "possibly unintended bitwise operation\n", +/*213*/ "tag mismatch\n", +/*214*/ "possibly a \"const\" array argument was intended: \"%s\"\n", +/*215*/ "expression has no effect\n", +/*216*/ "nested comment\n", +/*217*/ "loose indentation\n", +/*218*/ "old style prototypes used with optional semicolumns\n", +/*219*/ "local variable \"%s\" shadows a variable at a preceding level\n", +/*220*/ "expression with tag override must appear between parentheses\n", +/*221*/ "label name \"%s\" shadows tag name\n", +/*222*/ "number of digits exceeds rational number precision\n", +/*223*/ "redundant \"sizeof\": argument size is always 1 (symbol \"%s\")\n", +/*224*/ "indeterminate array size in \"sizeof\" expression (symbol \"%s\")\n", +/*225*/ "unreachable code\n", +/*226*/ "a variable is assigned to itself (symbol \"%s\")\n", +/*227*/ "more initiallers than enum fields\n", +/*228*/ "length of initialler exceeds size of the enum field\n", +/*229*/ "index tag mismatch (symbol \"%s\")\n", +/*230*/ "no implementation for state \"%s\" / function \"%s\", no fall-back\n", +/*231*/ "state specification on forward declaration is ignored\n", +/*232*/ "output file is written, but with compact encoding disabled\n" +#else + "\345 \274tr\240\226\233\277 %\206\273\337c\367\305", + "\222\323i\231\300\344\224t/\314cr\375\364", + "\374\270\300\265t\207do\325\241\334\273 \323i\213\012", + "\262 \274nev\270\244\271\335", + "\262 \274a\250gn\233\247\236u\200\266a\202\274nev\270\244\271\335", + "\222d\217d\360\333\230: \344\360\355\317\274z\211o\012", + "\222d\217d\360te\232: \344\360\355\317\274n\201-z\211o\012", + "\217k\221w\346#p\237g\314\012", + "\251\340\266 \320\261\222s\332\202\244\233be\371\200\323i\213\312\371c\352\222p\204s\304", + "\251\216 sho\332\206\222tur\346\247\236u\304", + "po\250\245\200\244\200\300\262 be\371\200\203i\205\212iza\213\335", + "po\250\245\223\217\203t\210\230\206a\250gn\227\370", + "po\250\245\223\217\203t\210\230\206bit\340s\200\343a\213\012", + "\320\261mis\334\273\012", + "po\250\245\223\247\042\344\316\275\303wa\207\203t\210\230d\335", + "\355\317\354\207\376effec\370", + "ne\232\233\333m\227\370", + "\356os\200\203d\210\320\213\012", + "\246\206\232y\361pro\277typ\325\244\233\340\266 \341\213\347semic\246umn\305", + "\356c\347\336\216 s\354dow\207\247\336a\202\247\306c\271\352lev\331\012", + "\355\317\340\266 \320\261ov\211rid\200\313appe\204 betwe\210 p\204\210\266ese\305", + "lab\331 nam\200\216 s\354dow\207\320\261nam\304", + "\374\270\300\321git\207\267ce\271\207\237\213\347\374\270\306ci\220\201\012", + "\222d\217d\360\042\322e\272\042: \303\322\200\274\212way\2071 \364", + "\203\230\367m\203\226\200\275\322\200\203 \042\322e\272\316\355\317\364", + "\217\222a\273\315\333\230\012", + "\247\336\274a\250gn\233\277 its\331f \364", + "m\225\200\203i\205\212l\211\207\266\350\210um \327\331d\305", + "l\210g\266 \300\203i\205\212l\270\267ce\271\207\322\200\300\266\200\210um \327\331d\012", + "\203\230x \320\261mis\334\273 \364", + "\376i\357l\373\320\231f\254\326\200\216 / \251\216\312\376f\212l-back\012", + "\326\200speci\327ca\231\317\371w\204\206\230cl\337\231\274ig\221\222d\012", + "outpu\202\327\361\274writt\210\312bu\202\340\266 \333\357ac\202\210\333d\352\321s\301\271\012" +#endif + }; diff --git a/compiler/libpc300/sc6.c b/compiler/libpc300/sc6.c new file mode 100755 index 00000000..65110180 --- /dev/null +++ b/compiler/libpc300/sc6.c @@ -0,0 +1,1162 @@ +/* Pawn compiler - Binary code generation (the "assembler") + * + * Copyright (c) ITB CompuPhase, 1997-2005 + * + * This software is provided "as-is", without any express or implied warranty. + * In no event will the authors be held liable for any damages arising from + * the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software in + * a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * 3. This notice may not be removed or altered from any source distribution. + * + * Version: $Id$ + */ +#include +#include +#include /* for macro max() */ +#include +#include +#if defined FORTIFY + #include "fortify.h" +#endif +#include "sc.h" +#include "amxdbg.h" +#if defined LINUX || defined __FreeBSD__ || defined __OpenBSD__ + #include +#endif + + +static void append_dbginfo(FILE *fout); + + +typedef cell (*OPCODE_PROC)(FILE *fbin,char *params,cell opcode); + +typedef struct { + cell opcode; + char *name; + int segment; /* sIN_CSEG=parse in cseg, sIN_DSEG=parse in dseg */ + OPCODE_PROC func; +} OPCODE; + +static cell codeindex; /* similar to "code_idx" */ +static cell *lbltab; /* label table */ +static int writeerror; +static int bytes_in, bytes_out; +static jmp_buf compact_err; + +/* apparently, strtol() does not work correctly on very large (unsigned) + * hexadecimal values */ +static ucell hex2long(char *s,char **n) +{ + ucell result=0L; + int negate=FALSE; + int digit; + + /* ignore leading whitespace */ + while (*s==' ' || *s=='\t') + s++; + + /* allow a negation sign to create the two's complement of numbers */ + if (*s=='-') { + negate=TRUE; + s++; + } /* if */ + + assert((*s>='0' && *s<='9') || (*s>='a' && *s<='f') || (*s>='a' && *s<='f')); + for ( ;; ) { + if (*s>='0' && *s<='9') + digit=*s-'0'; + else if (*s>='a' && *s<='f') + digit=*s-'a' + 10; + else if (*s>='A' && *s<='F') + digit=*s-'A' + 10; + else + break; /* probably whitespace */ + result=(result<<4) | digit; + s++; + } /* for */ + if (n!=NULL) + *n=s; + if (negate) + result=(~result)+1; /* take two's complement of the result */ + return (ucell)result; +} + +static ucell getparam(char *s,char **n) +{ + ucell result=0; + for ( ;; ) { + result+=hex2long(s,&s); + if (*s!='+') + break; + s++; + } /* for */ + if (n!=NULL) + *n=s; + return result; +} + +#if BYTE_ORDER==BIG_ENDIAN +static uint16_t *align16(uint16_t *v) +{ + unsigned char *s = (unsigned char *)v; + unsigned char t; + + /* swap two bytes */ + t=s[0]; + s[0]=s[1]; + s[1]=t; + return v; +} + +static uint32_t *align32(uint32_t *v) +{ + unsigned char *s = (unsigned char *)v; + unsigned char t; + + /* swap outer two bytes */ + t=s[0]; + s[0]=s[3]; + s[3]=t; + /* swap inner two bytes */ + t=s[1]; + s[1]=s[2]; + s[2]=t; + return v; +} + +#if PAWN_CELL_SIZE>=64 +static uint64_t *align64(uint64_t *v) +{ + unsigned char *s = (unsigned char *)v; + unsigned char t; + + t=s[0]; + s[0]=s[7]; + s[7]=t; + + t=s[1]; + s[1]=s[6]; + s[6]=t; + + t=s[2]; + s[2]=s[5]; + s[5]=t; + + t=s[3]; + s[3]=s[4]; + s[4]=t; + + return v; +} +#endif + + #if PAWN_CELL_SIZE==16 + #define aligncell(v) align16(v) + #elif PAWN_CELL_SIZE==32 + #define aligncell(v) align32(v) + #elif PAWN_CELL_SIZE==64 + #define aligncell(v) align64(v) + #endif +#else + #define align16(v) (v) + #define align32(v) (v) + #define aligncell(v) (v) +#endif + +static char *skipwhitespace(char *str) +{ + while (isspace(*str)) + str++; + return str; +} + +static char *stripcomment(char *str) +{ + char *ptr=strchr(str,';'); + if (ptr!=NULL) { + *ptr++='\n'; /* terminate the line, but leave the '\n' */ + *ptr='\0'; + } /* if */ + return str; +} + +static void write_encoded(FILE *fbin,ucell *c,int num) +{ + #if PAWN_CELL_SIZE == 16 + #define ENC_MAX 3 /* a 16-bit cell is encoded in max. 3 bytes */ + #define ENC_MASK 0x03 /* after 2x7 bits, 2 bits remain to make 16 bits */ + #elif PAWN_CELL_SIZE == 32 + #define ENC_MAX 5 /* a 32-bit cell is encoded in max. 5 bytes */ + #define ENC_MASK 0x0f /* after 4x7 bits, 4 bits remain to make 32 bits */ + #elif PAWN_CELL_SIZE == 64 + #define ENC_MAX 10 /* a 32-bit cell is encoded in max. 10 bytes */ + #define ENC_MASK 0x01 /* after 9x7 bits, 1 bit remains to make 64 bits */ + #endif + + assert(fbin!=NULL); + while (num-->0) { + if (sc_compress) { + ucell p=(ucell)*c; + unsigned char t[ENC_MAX]; + unsigned char code; + int index; + for (index=0; index>=7; + } /* for */ + /* skip leading zeros */ + while (index>1 && t[index-1]==0 && (t[index-2] & 0x40)==0) + index--; + /* skip leading -1s */ + if (index==ENC_MAX && t[index-1]==ENC_MASK && (t[index-2] & 0x40)!=0) + index--; + while (index>1 && t[index-1]==0x7f && (t[index-2] & 0x40)!=0) + index--; + /* write high byte first, write continuation bits */ + assert(index>0); + while (index-->0) { + code=(unsigned char)((index==0) ? t[index] : (t[index]|0x80)); + writeerror |= !pc_writebin(fbin,&code,1); + bytes_out++; + } /* while */ + bytes_in+=sizeof *c; + assert(AMX_COMPACTMARGIN>2); + if (bytes_out-bytes_in>=AMX_COMPACTMARGIN-2) + longjmp(compact_err,1); + } else { + assert((pc_lengthbin(fbin) % sizeof(cell)) == 0); + writeerror |= !pc_writebin(fbin,aligncell(c),sizeof *c); + } /* if */ + c++; + } /* while */ +} + +#if defined __BORLANDC__ || defined __WATCOMC__ + #pragma argsused +#endif +static cell noop(FILE *fbin,char *params,cell opcode) +{ + return 0; +} + +#if defined __BORLANDC__ || defined __WATCOMC__ + #pragma argsused +#endif +static cell parm0(FILE *fbin,char *params,cell opcode) +{ + if (fbin!=NULL) + write_encoded(fbin,(ucell*)&opcode,1); + return opcodes(1); +} + +static cell parm1(FILE *fbin,char *params,cell opcode) +{ + ucell p=getparam(params,NULL); + if (fbin!=NULL) { + write_encoded(fbin,(ucell*)&opcode,1); + write_encoded(fbin,&p,1); + } /* if */ + return opcodes(1)+opargs(1); +} + +#if defined __BORLANDC__ || defined __WATCOMC__ + #pragma argsused +#endif +static cell do_dump(FILE *fbin,char *params,cell opcode) +{ + ucell p; + int num = 0; + + while (*params!='\0') { + p=getparam(params,¶ms); + if (fbin!=NULL) + write_encoded(fbin,&p,1); + num++; + while (isspace(*params)) + params++; + } /* while */ + return num*sizeof(cell); +} + +static cell do_call(FILE *fbin,char *params,cell opcode) +{ + char name[sNAMEMAX+1]; + int i; + symbol *sym; + ucell p; + + for (i=0; !isspace(*params); i++,params++) { + assert(*params!='\0'); + assert(i=0 && iident==iFUNCTN || sym->ident==iREFFUNC); + assert(sym->vclass==sGLOBAL); + p=sym->addr; + } /* if */ + + if (fbin!=NULL) { + write_encoded(fbin,(ucell*)&opcode,1); + write_encoded(fbin,&p,1); + } /* if */ + return opcodes(1)+opargs(1); +} + +static cell do_jump(FILE *fbin,char *params,cell opcode) +{ + int i; + ucell p; + + i=(int)hex2long(params,NULL); + assert(i>=0 && i=0 && i=0 && i=MAX_INSTR_LEN) + return 0; + strncpy(str,instr,maxlen); + str[maxlen]='\0'; /* make sure the string is zero terminated */ + /* look up the instruction with a binary search + * the assembler is case insensitive to instructions (but case sensitive + * to symbols) + */ + low=1; /* entry 0 is reserved (for "not found") */ + high=(sizeof opcodelist / sizeof opcodelist[0])-1; + while (low0) + low=mid+1; + else + high=mid; + } /* while */ + + assert(low==high); + if (stricmp(str,opcodelist[low].name)==0) + return low; /* found */ + return 0; /* not found, return special index */ +} + +SC_FUNC int assemble(FILE *fout,FILE *fin) +{ + AMX_HEADER hdr; + AMX_FUNCSTUBNT func; + int numpublics,numnatives,numlibraries,numpubvars,numtags,padding; + long nametablesize,nameofs; + #if PAWN_CELL_SIZE > 32 + char line[512]; + #else + char line[256]; + #endif + char *instr,*params; + int i,pass,size; + int16_t count; + symbol *sym, **nativelist; + constvalue *constptr; + cell mainaddr; + + /* if compression failed, restart the assembly with compaction switched off */ + if (setjmp(compact_err)!=0) { + assert(sc_compress); /* cannot arrive here if compact encoding was disabled */ + sc_compress=FALSE; + pc_resetbin(fout,0); + error(232); /* disabled compact encoding */ + } /* if */ + + #if !defined NDEBUG + /* verify that the opcode list is sorted (skip entry 1; it is reserved + * for a non-existant opcode) + */ + assert(opcodelist[1].name!=NULL); + for (i=2; i<(sizeof opcodelist / sizeof opcodelist[0]); i++) { + assert(opcodelist[i].name!=NULL); + assert(stricmp(opcodelist[i].name,opcodelist[i-1].name)>0); + } /* for */ + #endif + + writeerror=FALSE; + nametablesize=sizeof(int16_t); + numpublics=0; + numnatives=0; + numpubvars=0; + mainaddr=-1; + /* count number of public and native functions and public variables */ + for (sym=glbtab.next; sym!=NULL; sym=sym->next) { + int match=0; + if (sym->ident==iFUNCTN) { + if ((sym->usage & uNATIVE)!=0 && (sym->usage & uREAD)!=0 && sym->addr>=0) + match=++numnatives; + if ((sym->usage & uPUBLIC)!=0 && (sym->usage & uDEFINE)!=0) + match=++numpublics; + if (strcmp(sym->name,uMAINFUNC)==0) { + assert(sym->vclass==sGLOBAL); + mainaddr=sym->addr; + } /* if */ + } else if (sym->ident==iVARIABLE) { + if ((sym->usage & uPUBLIC)!=0 && (sym->usage & (uREAD | uWRITTEN))!=0) + match=++numpubvars; + } /* if */ + if (match) { + char alias[sNAMEMAX+1]; + assert(sym!=NULL); + if ((sym->usage & uNATIVE)==0 || !lookup_alias(alias,sym->name)) { + assert(strlen(sym->name)<=sNAMEMAX); + strcpy(alias,sym->name); + } /* if */ + nametablesize+=strlen(alias)+1; + } /* if */ + } /* for */ + assert(numnatives==ntv_funcid); + + /* count number of libraries */ + numlibraries=0; + if (pc_addlibtable) { + for (constptr=libname_tab.next; constptr!=NULL; constptr=constptr->next) { + if (constptr->value>0) { + assert(strlen(constptr->name)>0); + numlibraries++; + nametablesize+=strlen(constptr->name)+1; + } /* if */ + } /* for */ + } /* if */ + + /* count number of public tags */ + numtags=0; + for (constptr=tagname_tab.next; constptr!=NULL; constptr=constptr->next) { + if ((constptr->value & PUBLICTAG)!=0) { + assert(strlen(constptr->name)>0); + numtags++; + nametablesize+=strlen(constptr->name)+1; + } /* if */ + } /* for */ + + /* pad the header to sc_dataalign + * => thereby the code segment is aligned + * => since the code segment is padded to a sc_dataalign boundary, the data segment is aligned + * => and thereby the stack top is aligned too + */ + assert(sc_dataalign!=0); + padding= (int)(sc_dataalign - (sizeof hdr + nametablesize) % sc_dataalign); + if (padding==sc_dataalign) + padding=0; + + /* write the abstract machine header */ + memset(&hdr, 0, sizeof hdr); + hdr.magic=(unsigned short)AMX_MAGIC; + hdr.file_version=CUR_FILE_VERSION; + hdr.amx_version=MIN_AMX_VERSION; + hdr.flags=(short)(sc_debug & sSYMBOLIC); + if (sc_compress) + hdr.flags|=AMX_FLAG_COMPACT; + if (sc_debug==0) + hdr.flags|=AMX_FLAG_NOCHECKS; + hdr.defsize=sizeof(AMX_FUNCSTUBNT); + hdr.publics=sizeof hdr; /* public table starts right after the header */ + hdr.natives=hdr.publics + numpublics*sizeof(AMX_FUNCSTUBNT); + hdr.libraries=hdr.natives + numnatives*sizeof(AMX_FUNCSTUBNT); + hdr.pubvars=hdr.libraries + numlibraries*sizeof(AMX_FUNCSTUBNT); + hdr.tags=hdr.pubvars + numpubvars*sizeof(AMX_FUNCSTUBNT); + hdr.nametable=hdr.tags + numtags*sizeof(AMX_FUNCSTUBNT); + hdr.cod=hdr.nametable + nametablesize + padding; + hdr.dat=hdr.cod + code_idx; + hdr.hea=hdr.dat + glb_declared*sizeof(cell); + hdr.stp=hdr.hea + sc_stksize*sizeof(cell); + hdr.cip=mainaddr; + hdr.size=hdr.hea; /* preset, this is incorrect in case of compressed output */ + pc_writebin(fout,&hdr,sizeof hdr); + + /* dump zeros up to the rest of the header, so that we can easily "seek" */ + for (nameofs=sizeof hdr; nameofsnext) { + if (sym->ident==iFUNCTN + && (sym->usage & uPUBLIC)!=0 && (sym->usage & uDEFINE)!=0) + { + assert(sym->vclass==sGLOBAL); + func.address=sym->addr; + func.nameofs=nameofs; + #if BYTE_ORDER==BIG_ENDIAN + align32(&func.address); + align32(&func.nameofs); + #endif + pc_resetbin(fout,hdr.publics+count*sizeof(AMX_FUNCSTUBNT)); + pc_writebin(fout,&func,sizeof func); + pc_resetbin(fout,nameofs); + pc_writebin(fout,sym->name,strlen(sym->name)+1); + nameofs+=strlen(sym->name)+1; + count++; + } /* if */ + } /* for */ + + /* write the natives table */ + /* The native functions must be written in sorted order. (They are + * sorted on their "id", not on their name). A nested loop to find + * each successive function would be an O(n^2) operation. But we + * do not really need to sort, because the native function id's + * are sequential and there are no duplicates. So we first walk + * through the complete symbol list and store a pointer to every + * native function of interest in a temporary table, where its id + * serves as the index in the table. Now we can walk the table and + * have all native functions in sorted order. + */ + if (numnatives>0) { + nativelist=(symbol **)malloc(numnatives*sizeof(symbol *)); + if (nativelist==NULL) + error(103); /* insufficient memory */ + #if !defined NDEBUG + memset(nativelist,0,numnatives*sizeof(symbol *)); /* for NULL checking */ + #endif + for (sym=glbtab.next; sym!=NULL; sym=sym->next) { + if (sym->ident==iFUNCTN && (sym->usage & uNATIVE)!=0 && (sym->usage & uREAD)!=0 && sym->addr>=0) { + assert(sym->addr < numnatives); + nativelist[(int)sym->addr]=sym; + } /* if */ + } /* for */ + count=0; + for (i=0; iname)) { + assert(strlen(sym->name)<=sNAMEMAX); + strcpy(alias,sym->name); + } /* if */ + assert(sym->vclass==sGLOBAL); + func.address=0; + func.nameofs=nameofs; + #if BYTE_ORDER==BIG_ENDIAN + align32(&func.address); + align32(&func.nameofs); + #endif + pc_resetbin(fout,hdr.natives+count*sizeof(AMX_FUNCSTUBNT)); + pc_writebin(fout,&func,sizeof func); + pc_resetbin(fout,nameofs); + pc_writebin(fout,alias,strlen(alias)+1); + nameofs+=strlen(alias)+1; + count++; + } /* for */ + free(nativelist); + } /* if */ + + /* write the libraries table */ + if (pc_addlibtable) { + count=0; + for (constptr=libname_tab.next; constptr!=NULL; constptr=constptr->next) { + if (constptr->value>0) { + assert(strlen(constptr->name)>0); + func.address=0; + func.nameofs=nameofs; + #if BYTE_ORDER==BIG_ENDIAN + align32(&func.address); + align32(&func.nameofs); + #endif + pc_resetbin(fout,hdr.libraries+count*sizeof(AMX_FUNCSTUBNT)); + pc_writebin(fout,&func,sizeof func); + pc_resetbin(fout,nameofs); + pc_writebin(fout,constptr->name,strlen(constptr->name)+1); + nameofs+=strlen(constptr->name)+1; + count++; + } /* if */ + } /* for */ + } /* if */ + + /* write the public variables table */ + count=0; + for (sym=glbtab.next; sym!=NULL; sym=sym->next) { + if (sym->ident==iVARIABLE && (sym->usage & uPUBLIC)!=0 && (sym->usage & (uREAD | uWRITTEN))!=0) { + assert((sym->usage & uDEFINE)!=0); + assert(sym->vclass==sGLOBAL); + func.address=sym->addr; + func.nameofs=nameofs; + #if BYTE_ORDER==BIG_ENDIAN + align32(&func.address); + align32(&func.nameofs); + #endif + pc_resetbin(fout,hdr.pubvars+count*sizeof(AMX_FUNCSTUBNT)); + pc_writebin(fout,&func,sizeof func); + pc_resetbin(fout,nameofs); + pc_writebin(fout,sym->name,strlen(sym->name)+1); + nameofs+=strlen(sym->name)+1; + count++; + } /* if */ + } /* for */ + + /* write the public tagnames table */ + count=0; + for (constptr=tagname_tab.next; constptr!=NULL; constptr=constptr->next) { + if ((constptr->value & PUBLICTAG)!=0) { + assert(strlen(constptr->name)>0); + func.address=constptr->value & TAGMASK; + func.nameofs=nameofs; + #if BYTE_ORDER==BIG_ENDIAN + align32(&func.address); + align32(&func.nameofs); + #endif + pc_resetbin(fout,hdr.tags+count*sizeof(AMX_FUNCSTUBNT)); + pc_writebin(fout,&func,sizeof func); + pc_resetbin(fout,nameofs); + pc_writebin(fout,constptr->name,strlen(constptr->name)+1); + nameofs+=strlen(constptr->name)+1; + count++; + } /* if */ + } /* for */ + + /* write the "maximum name length" field in the name table */ + assert(nameofs==hdr.nametable+nametablesize); + pc_resetbin(fout,hdr.nametable); + count=sNAMEMAX; + #if BYTE_ORDER==BIG_ENDIAN + align16(&count); + #endif + pc_writebin(fout,&count,sizeof count); + pc_resetbin(fout,hdr.cod); + + /* First pass: relocate all labels */ + /* This pass is necessary because the code addresses of labels is only known + * after the peephole optimization flag. Labels can occur inside expressions + * (e.g. the conditional operator), which are optimized. + */ + lbltab=NULL; + if (sc_labnum>0) { + /* only very short programs have zero labels; no first pass is needed + * if there are no labels */ + lbltab=(cell *)malloc(sc_labnum*sizeof(cell)); + if (lbltab==NULL) + error(103); /* insufficient memory */ + codeindex=0; + pc_resetasm(fin); + while (pc_readasm(fin,line,sizeof line)!=NULL) { + stripcomment(line); + instr=skipwhitespace(line); + /* ignore empty lines */ + if (*instr=='\0') + continue; + if (tolower(*instr)=='l' && *(instr+1)=='.') { + int lindex=(int)hex2long(instr+2,NULL); + assert(lindex>=0 && lindexinstr); + i=findopcode(instr,(int)(params-instr)); + if (opcodelist[i].name==NULL) { + *params='\0'; + error(104,instr); /* invalid assembler instruction */ + } /* if */ + if (opcodelist[i].segment==sIN_CSEG) + codeindex+=opcodelist[i].func(NULL,skipwhitespace(params),opcodelist[i].opcode); + } /* if */ + } /* while */ + } /* if */ + + /* Second pass (actually 2 more passes, one for all code and one for all data) */ + bytes_in=0; + bytes_out=0; + for (pass=sIN_CSEG; pass<=sIN_DSEG; pass++) { + pc_resetasm(fin); + while (pc_readasm(fin,line,sizeof line)!=NULL) { + stripcomment(line); + instr=skipwhitespace(line); + /* ignore empty lines and labels (labels have a special syntax, so these + * must be parsed separately) */ + if (*instr=='\0' || tolower(*instr)=='l' && *(instr+1)=='.') + continue; + /* get to the end of the instruction (make use of the '\n' that fgets() + * added at the end of the line; this way we will *always* drop on a + * whitespace character) */ + for (params=instr; *params!='\0' && !isspace(*params); params++) + /* nothing */; + assert(params>instr); + i=findopcode(instr,(int)(params-instr)); + assert(opcodelist[i].name!=NULL); + if (opcodelist[i].segment==pass) + opcodelist[i].func(fout,skipwhitespace(params),opcodelist[i].opcode); + } /* while */ + } /* for */ + if (bytes_out-bytes_in>0) + error(106); /* compression buffer overflow */ + + if (lbltab!=NULL) { + free(lbltab); + #if !defined NDEBUG + lbltab=NULL; + #endif + } /* if */ + + if (sc_compress) + hdr.size=pc_lengthbin(fout);/* get this value before appending debug info */ + if (!writeerror && (sc_debug & sSYMBOLIC)!=0) + append_dbginfo(fout); /* optionally append debug file */ + + if (writeerror) + error(101,"disk full"); + + /* adjust the header */ + size=(int)hdr.cod; /* save, the value in the header may be swapped */ + #if BYTE_ORDER==BIG_ENDIAN + align32(&hdr.size); + align16(&hdr.magic); + align16(&hdr.flags); + align16(&hdr.defsize); + align32(&hdr.publics); + align32(&hdr.natives); + align32(&hdr.libraries); + align32(&hdr.pubvars); + align32(&hdr.tags); + align32(&hdr.nametable); + align32(&hdr.cod); + align32(&hdr.dat); + align32(&hdr.hea); + align32(&hdr.stp); + align32(&hdr.cip); + #endif + pc_resetbin(fout,0); + pc_writebin(fout,&hdr,sizeof hdr); + + /* return the size of the header (including name tables, but excluding code + * or data sections) + */ + return size; +} + +static void append_dbginfo(FILE *fout) +{ + AMX_DBG_HDR dbghdr; + AMX_DBG_LINE dbgline; + AMX_DBG_SYMBOL dbgsym; + AMX_DBG_SYMDIM dbgidxtag[sDIMEN_MAX]; + int index,dim; + char *str,*prevstr,*name,*prevname; + ucell codeidx,previdx; + constvalue *constptr; + char symname[2*sNAMEMAX+16]; + int16_t id1,id2; + ucell address; + + /* header with general information */ + memset(&dbghdr, 0, sizeof dbghdr); + dbghdr.size=sizeof dbghdr; + dbghdr.magic=AMX_DBG_MAGIC; + dbghdr.file_version=CUR_FILE_VERSION; + dbghdr.amx_version=MIN_AMX_VERSION; + + /* first pass: collect the number of items in various tables */ + + /* file table */ + previdx=0; + prevstr=NULL; + prevname=NULL; + for (index=0; (str=get_dbgstring(index))!=NULL; index++) { + assert(str!=NULL); + assert(str[0]!='\0' && str[1]==':'); + if (str[0]=='F') { + codeidx=hex2long(str+2,&name); + if (codeidx!=previdx) { + if (prevstr!=NULL) { + assert(prevname!=NULL); + dbghdr.files++; + dbghdr.size+=sizeof(cell)+strlen(prevname)+1; + } /* if */ + previdx=codeidx; + } /* if */ + prevstr=str; + prevname=skipwhitespace(name); + } /* if */ + } /* for */ + if (prevstr!=NULL) { + assert(prevname!=NULL); + dbghdr.files++; + dbghdr.size+=sizeof(cell)+strlen(prevname)+1; + } /* if */ + + /* line number table */ + for (index=0; (str=get_dbgstring(index))!=NULL; index++) { + assert(str!=NULL); + assert(str[0]!='\0' && str[1]==':'); + if (str[0]=='L') { + dbghdr.lines++; + dbghdr.size+=sizeof(AMX_DBG_LINE); + } /* if */ + } /* for */ + + /* symbol table */ + for (index=0; (str=get_dbgstring(index))!=NULL; index++) { + assert(str!=NULL); + assert(str[0]!='\0' && str[1]==':'); + if (str[0]=='S') { + dbghdr.symbols++; + name=strchr(str+2,':'); + assert(name!=NULL); + dbghdr.size+=sizeof(AMX_DBG_SYMBOL)+strlen(skipwhitespace(name+1)); + if ((prevstr=strchr(name,'['))!=NULL) + while ((prevstr=strchr(prevstr+1,':'))!=NULL) + dbghdr.size+=sizeof(AMX_DBG_SYMDIM); + } /* if */ + } /* for */ + + /* tag table */ + for (constptr=tagname_tab.next; constptr!=NULL; constptr=constptr->next) { + assert(strlen(constptr->name)>0); + dbghdr.tags++; + dbghdr.size+=sizeof(AMX_DBG_TAG)+strlen(constptr->name); + } /* for */ + + /* automaton table */ + for (constptr=sc_automaton_tab.next; constptr!=NULL; constptr=constptr->next) { + assert(constptr->index==0 && strlen(constptr->name)==0 || strlen(constptr->name)>0); + dbghdr.automatons++; + dbghdr.size+=sizeof(AMX_DBG_MACHINE)+strlen(constptr->name); + } /* for */ + + /* state table */ + for (constptr=sc_state_tab.next; constptr!=NULL; constptr=constptr->next) { + assert(strlen(constptr->name)>0); + dbghdr.states++; + dbghdr.size+=sizeof(AMX_DBG_STATE)+strlen(constptr->name); + } /* for */ + + + /* pass 2: generate the tables */ + + writeerror |= !pc_writebin(fout,&dbghdr,sizeof dbghdr); + + /* file table */ + previdx=0; + prevstr=NULL; + prevname=NULL; + for (index=0; (str=get_dbgstring(index))!=NULL; index++) { + assert(str!=NULL); + assert(str[0]!='\0' && str[1]==':'); + if (str[0]=='F') { + codeidx=hex2long(str+2,&name); + if (codeidx!=previdx) { + if (prevstr!=NULL) { + assert(prevname!=NULL); + writeerror |= !pc_writebin(fout,&previdx,sizeof previdx); + writeerror |= !pc_writebin(fout,prevname,strlen(prevname)+1); + } /* if */ + previdx=codeidx; + } /* if */ + prevstr=str; + prevname=skipwhitespace(name); + } /* if */ + } /* for */ + if (prevstr!=NULL) { + assert(prevname!=NULL); + writeerror |= !pc_writebin(fout,&previdx,sizeof previdx); + writeerror |= !pc_writebin(fout,prevname,strlen(prevname)+1); + } /* if */ + + /* line number table */ + for (index=0; (str=get_dbgstring(index))!=NULL; index++) { + assert(str!=NULL); + assert(str[0]!='\0' && str[1]==':'); + if (str[0]=='L') { + dbgline.address=hex2long(str+2,&str); + dbgline.line=(int32_t)hex2long(str,NULL); + writeerror |= !pc_writebin(fout,&dbgline,sizeof dbgline); + } /* if */ + } /* for */ + + /* symbol table */ + for (index=0; (str=get_dbgstring(index))!=NULL; index++) { + assert(str!=NULL); + assert(str[0]!='\0' && str[1]==':'); + if (str[0]=='S') { + dbgsym.address=hex2long(str+2,&str); + dbgsym.tag=(int16_t)hex2long(str,&str); + str=skipwhitespace(str); + assert(*str==':'); + name=skipwhitespace(str+1); + str=strchr(name,' '); + assert(str!=NULL); + assert((int)(str-name)next) { + assert(strlen(constptr->name)>0); + id1=(int16_t)(constptr->value & TAGMASK); + writeerror |= !pc_writebin(fout,&id1,sizeof id1); + writeerror |= !pc_writebin(fout,constptr->name,strlen(constptr->name)+1); + } /* for */ + + /* automaton table */ + for (constptr=sc_automaton_tab.next; constptr!=NULL; constptr=constptr->next) { + assert(constptr->index==0 && strlen(constptr->name)==0 || strlen(constptr->name)>0); + id1=(int16_t)constptr->index; + address=(ucell)constptr->value; + writeerror |= !pc_writebin(fout,&id1,sizeof id1); + writeerror |= !pc_writebin(fout,&address,sizeof address); + writeerror |= !pc_writebin(fout,constptr->name,strlen(constptr->name)+1); + } /* for */ + + /* state table */ + for (constptr=sc_state_tab.next; constptr!=NULL; constptr=constptr->next) { + assert(strlen(constptr->name)>0); + id1=(int16_t)constptr->value; + id2=(int16_t)constptr->index; + address=(ucell)constptr->value; + writeerror |= !pc_writebin(fout,&id1,sizeof id1); + writeerror |= !pc_writebin(fout,&id2,sizeof id2); + writeerror |= !pc_writebin(fout,constptr->name,strlen(constptr->name)+1); + } /* for */ + + delete_dbgstringtable(); +} diff --git a/compiler/libpc300/sc7.c b/compiler/libpc300/sc7.c new file mode 100755 index 00000000..33a972e5 --- /dev/null +++ b/compiler/libpc300/sc7.c @@ -0,0 +1,631 @@ +/* Pawn compiler - Staging buffer and optimizer + * + * The staging buffer + * ------------------ + * The staging buffer allows buffered output of generated code, deletion + * of redundant code, optimization by a tinkering process and reversing + * the ouput of evaluated expressions (which is used for the reversed + * evaluation of arguments in functions). + * Initially, stgwrite() writes to the file directly, but after a call to + * stgset(TRUE), output is redirected to the buffer. After a call to + * stgset(FALSE), stgwrite()'s output is directed to the file again. Thus + * only one routine is used for writing to the output, which can be + * buffered output or direct output. + * + * staging buffer variables: stgbuf - the buffer + * stgidx - current index in the staging buffer + * staging - if true, write to the staging buffer; + * if false, write to file directly. + * + * Copyright (c) ITB CompuPhase, 1997-2005 + * + * This software is provided "as-is", without any express or implied warranty. + * In no event will the authors be held liable for any damages arising from + * the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software in + * a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * 3. This notice may not be removed or altered from any source distribution. + * + * Version: $Id$ + */ +#include +#include +#include /* for atoi() */ +#include +#include +#if defined FORTIFY + #include "fortify.h" +#endif +#include "sc.h" + +#if defined _MSC_VER + #pragma warning(push) + #pragma warning(disable:4125) /* decimal digit terminates octal escape sequence */ +#endif + +#include "sc7.scp" + +#if defined _MSC_VER + #pragma warning(pop) +#endif + +static void stgstring(char *start,char *end); +static void stgopt(char *start,char *end); + + +#define sSTG_GROW 512 +#define sSTG_MAX 20480 + +static char *stgbuf = NULL; +static int stgmax = 0; /* current size of the staging buffer */ + +#define CHECK_STGBUFFER(index) if ((int)(index)>=stgmax) grow_stgbuffer((index)+1) + +static void grow_stgbuffer(int requiredsize) +{ + char *p; + int clear = stgbuf==NULL; /* if previously none, empty buffer explicitly */ + + assert(stgmaxsSTG_MAX) + error(102,"staging buffer"); /* staging buffer overflow (fatal error) */ + stgmax=requiredsize+sSTG_GROW; + if (stgbuf!=NULL) + p=(char *)realloc(stgbuf,stgmax*sizeof(char)); + else + p=(char *)malloc(stgmax*sizeof(char)); + if (p==NULL) + error(102,"staging buffer"); /* staging buffer overflow (fatal error) */ + stgbuf=p; + if (clear) + *stgbuf='\0'; +} + +SC_FUNC void stgbuffer_cleanup(void) +{ + if (stgbuf!=NULL) { + free(stgbuf); + stgbuf=NULL; + stgmax=0; + } /* if */ +} + +/* the variables "stgidx" and "staging" are declared in "scvars.c" */ + +/* stgmark + * + * Copies a mark into the staging buffer. At this moment there are three + * possible marks: + * sSTARTREORDER identifies the beginning of a series of expression + * strings that must be written to the output file in + * reordered order + * sENDREORDER identifies the end of 'reverse evaluation' + * sEXPRSTART + idx only valid within a block that is evaluated in + * reordered order, it identifies the start of an + * expression; the "idx" value is the argument position + * + * Global references: stgidx (altered) + * stgbuf (altered) + * staging (referred to only) + */ +SC_FUNC void stgmark(char mark) +{ + if (staging) { + CHECK_STGBUFFER(stgidx); + stgbuf[stgidx++]=mark; + } /* if */ +} + +static int filewrite(char *str) +{ + if (sc_status==statWRITE) + return pc_writeasm(outf,str); + return TRUE; +} + +/* stgwrite + * + * Writes the string "st" to the staging buffer or to the output file. In the + * case of writing to the staging buffer, the terminating byte of zero is + * copied too, but... the optimizer can only work on complete lines (not on + * fractions of it. Therefore if the string is staged, if the last character + * written to the buffer is a '\0' and the previous-to-last is not a '\n', + * the string is concatenated to the last string in the buffer (the '\0' is + * overwritten). This also means an '\n' used in the middle of a string isn't + * recognized and could give wrong results with the optimizer. + * Even when writing to the output file directly, all strings are buffered + * until a whole line is complete. + * + * Global references: stgidx (altered) + * stgbuf (altered) + * staging (referred to only) + */ +SC_FUNC void stgwrite(const char *st) +{ + int len; + + CHECK_STGBUFFER(0); + if (staging) { + if (stgidx>=2 && stgbuf[stgidx-1]=='\0' && stgbuf[stgidx-2]!='\n') + stgidx-=1; /* overwrite last '\0' */ + while (*st!='\0') { /* copy to staging buffer */ + CHECK_STGBUFFER(stgidx); + stgbuf[stgidx++]=*st++; + } /* while */ + CHECK_STGBUFFER(stgidx); + stgbuf[stgidx++]='\0'; + } else { + CHECK_STGBUFFER(strlen(stgbuf)+strlen(st)+1); + strcat(stgbuf,st); + len=strlen(stgbuf); + if (len>0 && stgbuf[len-1]=='\n') { + filewrite(stgbuf); + stgbuf[0]='\0'; + } /* if */ + } /* if */ +} + +/* stgout + * + * Writes the staging buffer to the output file via stgstring() (for + * reversing expressions in the buffer) and stgopt() (for optimizing). It + * resets "stgidx". + * + * Global references: stgidx (altered) + * stgbuf (referred to only) + * staging (referred to only) + */ +SC_FUNC void stgout(int index) +{ + if (!staging) + return; + stgstring(&stgbuf[index],&stgbuf[stgidx]); + stgidx=index; +} + +typedef struct { + char *start,*end; +} argstack; + +/* stgstring + * + * Analyses whether code strings should be output to the file as they appear + * in the staging buffer or whether portions of it should be re-ordered. + * Re-ordering takes place in function argument lists; Pawn passes arguments + * to functions from right to left. When arguments are "named" rather than + * positional, the order in the source stream is indeterminate. + * This function calls itself recursively in case it needs to re-order code + * strings, and it uses a private stack (or list) to mark the start and the + * end of expressions in their correct (reversed) order. + * In any case, stgstring() sends a block as large as possible to the + * optimizer stgopt(). + * + * In "reorder" mode, each set of code strings must start with the token + * sEXPRSTART, even the first. If the token sSTARTREORDER is represented + * by '[', sENDREORDER by ']' and sEXPRSTART by '|' the following applies: + * '[]...' valid, but useless; no output + * '[|...] valid, but useless; only one string + * '[|...|...] valid and usefull + * '[...|...] invalid, first string doesn't start with '|' + * '[|...|] invalid + */ +static void stgstring(char *start,char *end) +{ + char *ptr; + int nest,argc,arg; + argstack *stack; + + while (start=0) + stack[arg].end=start-1; /* finish previous argument */ + arg=(unsigned char)*start - sEXPRSTART; + stack[arg].start=start+1; + if (arg>=argc) + argc=arg+1; + } /* if */ + start++; + } else { + start+=strlen(start)+1; + } /* if */ + } /* switch */ + } while (nest); /* enddo */ + if (arg>=0) + stack[arg].end=start-1; /* finish previous argument */ + while (argc>0) { + argc--; + stgstring(stack[argc].start,stack[argc].end); + } /* while */ + free(stack); + } else { + ptr=start; + while (ptr0) + filewrite(stgbuf); + } /* if */ + stgbuf[0]='\0'; +} + +/* phopt_init + * Initialize all sequence strings of the peehole optimizer. The strings + * are embedded in the .EXE file in compressed format, here we expand + * them (and allocate memory for the sequences). + */ +static SEQUENCE *sequences; + +SC_FUNC int phopt_init(void) +{ + int number, i, len; + char str[160]; + + /* count number of sequences */ + for (number=0; sequences_cmp[number].find!=NULL; number++) + /* nothing */; + number++; /* include an item for the NULL terminator */ + + if ((sequences=(SEQUENCE*)malloc(number * sizeof(SEQUENCE)))==NULL) + return FALSE; + + /* pre-initialize all to NULL (in case of failure) */ + for (i=0; i (PAWN_CELL_SIZE/4) * MAX_OPT_CAT + #define MAX_ALIAS sNAMEMAX +#else + #define MAX_ALIAS (PAWN_CELL_SIZE/4) * MAX_OPT_CAT +#endif + +static int matchsequence(char *start,char *end,char *pattern, + char symbols[MAX_OPT_VARS][MAX_ALIAS+1], + int *match_length) +{ + int var,i; + char str[MAX_ALIAS+1]; + char *start_org=start; + cell value; + char *ptr; + + *match_length=0; + for (var=0; var=end) + return FALSE; + switch (*pattern) { + case '%': /* new "symbol" */ + pattern++; + assert(isdigit(*pattern)); + var=atoi(pattern) - 1; + assert(var>=0 && var=0 && var=0 && var0) { /* delete a section */ + memmove(dest,dest+offset,dest_length-offset); + memset(dest+dest_length-offset,0xcc,offset); /* not needed, but for cleanlyness */ + } else if (offset<0) { /* insert a section */ + memmove(dest-offset, dest, dest_length); + } /* if */ + memcpy(dest, replace, repl_length); +} + +/* stgopt + * + * Optimizes the staging buffer by checking for series of instructions that + * can be coded more compact. The routine expects the lines in the staging + * buffer to be separated with '\n' and '\0' characters. + * + * The longest sequences should probably be checked first. + */ + +static void stgopt(char *start,char *end) +{ + char symbols[MAX_OPT_VARS][MAX_ALIAS+1]; + int seq,match_length,repl_length; + int matches; + char *debut=start; + + assert(sequences!=NULL); + /* do not match anything if debug-level is maximum */ + if ((sc_debug & sNOOPTIMIZE)==0 && sc_status==statWRITE) { + do { + matches=0; + start=debut; + while (start=0); + if (matchsequence(start,end,sequences[seq].find,symbols,&match_length)) { + char *replace=replacesequence(sequences[seq].replace,symbols,&repl_length); + /* If the replacement is bigger than the original section, we may need + * to "grow" the staging buffer. This is quite complex, due to the + * re-ordering of expressions that can also happen in the staging + * buffer. In addition, it should not happen: the peephole optimizer + * must replace sequences with *shorter* sequences, not longer ones. + * So, I simply forbid sequences that are longer than the ones they + * are meant to replace. + */ + assert(match_length>=repl_length); + if (match_length>=repl_length) { + strreplace(start,replace,match_length,repl_length,(int)(end-start)); + end-=match_length-repl_length; + free(replace); + code_idx-=sequences[seq].savesize; + seq=0; /* restart search for matches */ + matches++; + } else { + /* actually, we should never get here (match_length0); + } /* if ((sc_debug & sNOOPTIMIZE)==0 && sc_status==statWRITE) */ + + for (start=debut; start ;$lcl + * stack -4 push.c + * const.pri ;$exp + * stor.s.pri - + * ;$exp - + */ + { + #ifdef SCPACK + ";$lcl %1 %2!stack -4!const.pri %3!stor.s.pri %2!;$exp!", + ";$lcl %1 %2!push.c %3!;$exp!", + #else + "\231lcl\204\216\227ack -\331\237\203\257\227or\220\226\244", + "\231lcl\204\216\321\257\244", + #endif + seqsize(3,3) - seqsize(1,1) + }, + { + #ifdef SCPACK + ";$lcl %1 %2!stack -4!zero.pri!stor.s.pri %2!;$exp!", + ";$lcl %1 %2!push.c 0!;$exp!", + #else + "\231lcl\204\216\227ack -\331\353\227or\220\226\244", + "\231lcl\204\216\321 0!\244", + #endif + seqsize(3,2) - seqsize(1,1) + }, + /* During a calculation, the intermediate result must sometimes + * be moved from PRI to ALT, like in: + * push.pri move.alt + * load.s.pri n1 load.s.pri n1 + * pop.alt - + * + * The above also accurs for "load.pri" and for "const.pri", + * so add another two cases. + */ + { + #ifdef SCPACK + "push.pri!load.s.pri %1!pop.alt!", + "move.alt!load.s.pri %1!", + #else + "\240\325\234", + "\351\217\325", + #endif + seqsize(3,1) - seqsize(2,1) + }, + { + #ifdef SCPACK + "push.pri!load.pri %1!pop.alt!", + "move.alt!load.pri %1!", + #else + "\240\315\234", + "\351\217\315", + #endif + seqsize(3,1) - seqsize(2,1) + }, + { + #ifdef SCPACK + "push.pri!const.pri %1!pop.alt!", + "move.alt!const.pri %1!", + #else + "\240\316\234", + "\351\217\316", + #endif + seqsize(3,1) - seqsize(2,1) + }, + { + #ifdef SCPACK + "push.pri!zero.pri!pop.alt!", + "move.alt!zero.pri!", + #else + "\240\353\234", + "\351\217\353", + #endif + seqsize(3,0) - seqsize(2,0) + }, + /* saving PRI and then loading from its address + * occurs when indexing a multi-dimensional array + */ + { + #ifdef SCPACK + "push.pri!load.i!pop.alt!", + "move.alt!load.i!", + #else + "\240\213\341\234", + "\351\217\213\341", + #endif + seqsize(3,0) - seqsize(2,0) + }, + /* An even simpler PUSH/POP optimization (occurs in + * switch statements): + * push.pri move.alt + * pop.alt - + */ + { + #ifdef SCPACK + "push.pri!pop.alt!", + "move.alt!", + #else + "\240\234", + "\351\217", + #endif + seqsize(2,0) - seqsize(1,0) + }, + /* And what to think of this PUSH/POP sequence, which occurs + * due to the support for user-defined assignment operator): + * push.alt - + * pop.alt - + */ +//??? +//{ +// #ifdef SCPACK +// "push.alt!pop.alt!", +// ";$", /* SCPACK cannot handle empty strings */ +// #else +// "\225\237", +// "\353", +// #endif +// seqsize(2,0) - seqsize(0,0) +//}, + /* Functions with many parameters with the same default + * value have sequences like: + * push.c n1 const.pri n1 + * ;$par push.r.pri n2 ; where n2 is the number of pushes + * push.c n1 ;$par + * ;$par - + * push.c n1 - + * ;$par - + * etc. etc. + * The shortest matched sequence is 3, because a sequence of two can also be + * optimized as two "push.c n1" instructions. + * => this optimization does not work, because the argument re-ordering in + * a function call causes each argument to be optimized individually + */ +//{ +// #ifdef SCPACK +// "const.pri %1!push.pri!;$par!const.pri %1!push.pri!;$par!const.pri %1!push.pri!;$par!const.pri %1!push.pri!;$par!const.pri %1!push.pri!;$par!", +// "const.pri %1!push.r.pri 5!;$par!", +// #else +// "\327\327\254", +// "\352\221.r\2745!", +// #endif +// seqsize(10,5) - seqsize(2,2) +//}, +//{ +// #ifdef SCPACK +// "const.pri %1!push.pri!;$par!const.pri %1!push.pri!;$par!const.pri %1!push.pri!;$par!const.pri %1!push.pri!;$par!", +// "const.pri %1!push.r.pri 4!;$par!", +// #else +// "\327\327", +// "\352\221.r\274\326", +// #endif +// seqsize(8,4) - seqsize(2,2) +//}, +//{ +// #ifdef SCPACK +// "const.pri %1!push.pri!;$par!const.pri %1!push.pri!;$par!const.pri %1!push.pri!;$par!", +// "const.pri %1!push.r.pri 3!;$par!", +// #else +// "\327\254", +// "\352\221.r\274\247", +// #endif +// seqsize(6,3) - seqsize(2,2) +//}, + /* User-defined operators first load the operands into registers and + * then have them pushed onto the stack. This can give rise to sequences + * like: + * const.pri n1 push.c n1 + * const.alt n2 push.c n2 + * push.pri - + * push.alt - + * A similar sequence occurs with the two PUSH.pri/alt instructions inverted. + * The first, second, or both CONST.pri/alt instructions can also be + * LOAD.pri/alt. + * This gives 2 x 4 cases. + */ + { + #ifdef SCPACK + "const.pri %1!const.alt %2!push.pri!push.alt!", + "push.c %1!push.c %2!", + #else + "\316\237\311\240\352", + "\321\205\321\216", + #endif + seqsize(4,2) - seqsize(2,2) + }, + { + #ifdef SCPACK + "const.pri %1!const.alt %2!push.alt!push.pri!", + "push.c %2!push.c %1!", + #else + "\316\237\311\352\240", + "\321\216\321\205", + #endif + seqsize(4,2) - seqsize(2,2) + }, + { + #ifdef SCPACK + "const.pri %1!load.alt %2!push.pri!push.alt!", + "push.c %1!push %2!", + #else + "\316\213\311\240\352", + "\321\205\222\216", + #endif + seqsize(4,2) - seqsize(2,2) + }, + { + #ifdef SCPACK + "const.pri %1!load.alt %2!push.alt!push.pri!", + "push %2!push.c %1!", + #else + "\316\213\311\352\240", + "\222\216\321\205", + #endif + seqsize(4,2) - seqsize(2,2) + }, + { + #ifdef SCPACK + "load.pri %1!const.alt %2!push.pri!push.alt!", + "push %1!push.c %2!", + #else + "\315\237\311\240\352", + "\222\205\321\216", + #endif + seqsize(4,2) - seqsize(2,2) + }, + { + #ifdef SCPACK + "load.pri %1!const.alt %2!push.alt!push.pri!", + "push.c %2!push %1!", + #else + "\315\237\311\352\240", + "\321\216\222\205", + #endif + seqsize(4,2) - seqsize(2,2) + }, + { + #ifdef SCPACK + "load.pri %1!load.alt %2!push.pri!push.alt!", + "push %1!push %2!", + #else + "\315\213\311\240\352", + "\222\205\222\216", + #endif + seqsize(4,2) - seqsize(2,2) + }, + { + #ifdef SCPACK + "load.pri %1!load.alt %2!push.alt!push.pri!", + "push %2!push %1!", + #else + "\315\213\311\352\240", + "\222\216\222\205", + #endif + seqsize(4,2) - seqsize(2,2) + }, + /* Function calls (parameters are passed on the stack) + * load.s.pri n1 push.s n1 + * push.pri - + * -------------------------------------- + * load.pri n1 push n1 + * push.pri - + * -------------------------------------- + * const.pri n1 push.c n1 + * push.pri - + * -------------------------------------- + * zero.pri push.c 0 + * push.pri - + * -------------------------------------- + * addr.pri n1 pushaddr n1 + * push.pri - + * + * However, PRI must not be needed after this instruction + * if this shortcut is used. Check for the ;$par comment. + */ + { + #ifdef SCPACK + "load.s.pri %1!push.pri!;$par!", + "push.s %1!;$par!", + #else + "\224\255\347", + "\222\220\205\347", + #endif + seqsize(2,1) - seqsize(1,1) + }, + { + #ifdef SCPACK + "load.pri %1!push.pri!;$par!", + "push %1!;$par!", + #else + "\213\255\347", + "\222\205\347", + #endif + seqsize(2,1) - seqsize(1,1) + }, + { + #ifdef SCPACK + "const.pri %1!push.pri!;$par!", + "push.c %1!;$par!", + #else + "\237\255\347", + "\321\205\347", + #endif + seqsize(2,1) - seqsize(1,1) + }, + { + #ifdef SCPACK + "zero.pri!push.pri!;$par!", + "push.c 0!;$par!", + #else + "\353\240\347", + "\321 0!\347", + #endif + seqsize(2,0) - seqsize(1,1) + }, + { + #ifdef SCPACK + "addr.pri %1!push.pri!;$par!", + "pushaddr %1!;$par!", + #else + "\252\255\347", + "\222\252\205\347", + #endif + seqsize(2,1) - seqsize(1,1) + }, + /* References with a default value generate new cells on the heap + * dynamically. That code often ends with: + * move.pri push.alt + * push.pri - + */ + { + #ifdef SCPACK + "move.pri!push.pri!", + "push.alt!", + #else + "\351\230\240", + "\352", + #endif + seqsize(2,0) - seqsize(1,0) + }, + /* Simple arithmetic operations on constants. Noteworthy is the + * subtraction of a constant, since it is converted to the addition + * of the inverse value. + * const.alt n1 add.c n1 + * add - + * -------------------------------------- + * const.alt n1 add.c -n1 + * sub - + * -------------------------------------- + * const.alt n1 smul.c n1 + * smul - + * -------------------------------------- + * const.alt n1 eq.c.pri n1 + * eq - + */ + { + #ifdef SCPACK + "const.alt %1!add!", + "add.c %1!", + #else + "\361\270", + "\376\205", + #endif + seqsize(2,1) - seqsize(1,1) + }, + { + #ifdef SCPACK + "const.alt %1!sub!", + "add.c -%1!", + #else + "\361sub!", + "\376 -%1!", + #endif + seqsize(2,1) - seqsize(1,1) + }, + { + #ifdef SCPACK + "const.alt %1!smul!", + "smul.c %1!", + #else + "\361smul!", + "smu\271\205", + #endif + seqsize(2,1) - seqsize(1,1) + }, + { + #ifdef SCPACK + "const.alt %1!eq!", + "eq.c.pri %1!", + #else + "\361\265", + "\254\247\223", + #endif + seqsize(2,1) - seqsize(1,1) + }, + /* Some operations use the alternative subtraction operation --these + * can also be optimized. + * const.pri n1 load.s.pri n2 + * load.s.alt n2 add.c -n1 + * sub.alt - + * -------------------------------------- + * const.pri n1 load.pri n2 + * load.alt n2 add.c -n1 + * sub.alt - + */ + { + #ifdef SCPACK + "const.pri %1!load.s.alt %2!sub.alt!", + "load.s.pri %2!add.c -%1!", + #else + "\316\224\311sub\217", + "\245\376 -%1!", + #endif + seqsize(3,2) - seqsize(2,2) + }, + { + #ifdef SCPACK + "const.pri %1!load.alt %2!sub.alt!", + "load.pri %2!add.c -%1!", + #else + "\316\213\311sub\217", + "\317\376 -%1!", + #endif + seqsize(3,2) - seqsize(2,2) + }, + /* With arrays indexed with constants that come from enumerations, it happens + * multiple add.c opcodes follow in sequence. + * add.c n1 add.c n1+n2 + * add.c n2 - + */ + { + #ifdef SCPACK + "add.c %1!add.c %2!", + "add.c %1+%2!", + #else + "\376\205\376\216", + "\376\204+%\214", + #endif + seqsize(2,2) - seqsize(1,1) + }, + /* Compare and jump + * eq jneq n1 + * jzer n1 - + * -------------------------------------- + * eq jeq n1 + * jnz n1 - + * -------------------------------------- + * neq jeq n1 + * jzer n1 - + * -------------------------------------- + * neq jneq n1 + * jnz n1 - + * Compares followed by jzer occur much more + * often than compares followed with jnz. So we + * take the easy route here. + * less jgeq n1 + * jzer n1 - + * -------------------------------------- + * leq jgrtr n1 + * jzer n1 - + * -------------------------------------- + * grtr jleq n1 + * jzer n1 - + * -------------------------------------- + * geq jless n1 + * jzer n1 - + * -------------------------------------- + * sless jsgeq n1 + * jzer n1 - + * -------------------------------------- + * sleq jsgrtr n1 + * jzer n1 - + * -------------------------------------- + * sgrtr jsleq n1 + * jzer n1 - + * -------------------------------------- + * sgeq jsless n1 + * jzer n1 - + */ + { + #ifdef SCPACK + "eq!jzer %1!", + "jneq %1!", + #else + "\265\305", + "jn\326", + #endif + seqsize(2,1) - seqsize(1,1) + }, + { + #ifdef SCPACK + "eq!jnz %1!", + "jeq %1!", + #else + "\265jnz\205", + "j\326", + #endif + seqsize(2,1) - seqsize(1,1) + }, + { + #ifdef SCPACK + "neq!jzer %1!", + "jeq %1!", + #else + "n\265\305", + "j\326", + #endif + seqsize(2,1) - seqsize(1,1) + }, + { + #ifdef SCPACK + "neq!jnz %1!", + "jneq %1!", + #else + "n\265jnz\205", + "jn\326", + #endif + seqsize(2,1) - seqsize(1,1) + }, + { + #ifdef SCPACK + "less!jzer %1!", + "jgeq %1!", + #else + "l\323!\305", + "jg\326", + #endif + seqsize(2,1) - seqsize(1,1) + }, + { + #ifdef SCPACK + "leq!jzer %1!", + "jgrtr %1!", + #else + "l\265\305", + "jg\324r\205", + #endif + seqsize(2,1) - seqsize(1,1) + }, + { + #ifdef SCPACK + "grtr!jzer %1!", + "jleq %1!", + #else + "g\324\306\305", + "jl\326", + #endif + seqsize(2,1) - seqsize(1,1) + }, + { + #ifdef SCPACK + "geq!jzer %1!", + "jless %1!", + #else + "g\265\305", + "jl\323\205", + #endif + seqsize(2,1) - seqsize(1,1) + }, + { + #ifdef SCPACK + "sless!jzer %1!", + "jsgeq %1!", + #else + "\360\305", + "j\302\326", + #endif + seqsize(2,1) - seqsize(1,1) + }, + { + #ifdef SCPACK + "sleq!jzer %1!", + "jsgrtr %1!", + #else + "\363\305", + "j\342r\205", + #endif + seqsize(2,1) - seqsize(1,1) + }, + { + #ifdef SCPACK + "sgrtr!jzer %1!", + "jsleq %1!", + #else + "\365\305", + "j\303\326", + #endif + seqsize(2,1) - seqsize(1,1) + }, + { + #ifdef SCPACK + "sgeq!jzer %1!", + "jsless %1!", + #else + "\362\305", + "j\340\205", + #endif + seqsize(2,1) - seqsize(1,1) + }, + /* Test for zero (common case, especially for strings) + * E.g. the test expression of: "for (i=0; str{i}!=0; ++i)" + * + * zero.alt jzer n1 + * jeq n1 - + * -------------------------------------- + * zero.alt jnz n1 + * jneq n1 - + */ + { + #ifdef SCPACK + "zero.alt!jeq %1!", + "jzer %1!", + #else + "\314\217j\326", + "\305", + #endif + seqsize(2,1) - seqsize(1,1) + }, + { + #ifdef SCPACK + "zero.alt!jneq %1!", + "jnz %1!", + #else + "\314\217jn\326", + "jnz\205", + #endif + seqsize(2,1) - seqsize(1,1) + }, + /* Incrementing and decrementing leaves a value in + * in PRI which may not be used (for example, as the + * third expression in a "for" loop). + * inc n1 inc n1 ; ++n + * load.pri n1 ;$exp + * ;$exp - + * -------------------------------------- + * load.pri n1 inc n1 ; n++, e.g. "for (n=0; n<10; n++)" + * inc n1 ;$exp + * ;$exp - + * Plus the varieties for stack relative increments + * and decrements. + */ + { + #ifdef SCPACK + "inc %1!load.pri %1!;$exp!", + "inc %1!;$exp!", + #else + "\373c\205\315\244", + "\373c\261", + #endif + seqsize(2,2) - seqsize(1,1) + }, + { + #ifdef SCPACK + "load.pri %1!inc %1!;$exp!", + "inc %1!;$exp!", + #else + "\315\373c\261", + "\373c\261", + #endif + seqsize(2,2) - seqsize(1,1) + }, + { + #ifdef SCPACK + "inc.s %1!load.s.pri %1!;$exp!", + "inc.s %1!;$exp!", + #else + "\373\354\205\325\244", + "\373\354\261", + #endif + seqsize(2,2) - seqsize(1,1) + }, + { + #ifdef SCPACK + "load.s.pri %1!inc.s %1!;$exp!", + "inc.s %1!;$exp!", + #else + "\325\373\354\261", + "\373\354\261", + #endif + seqsize(2,2) - seqsize(1,1) + }, + { + #ifdef SCPACK + "dec %1!load.pri %1!;$exp!", + "dec %1!;$exp!", + #else + "\371c\205\315\244", + "\371c\261", + #endif + seqsize(2,2) - seqsize(1,1) + }, + { + #ifdef SCPACK + "load.pri %1!dec %1!;$exp!", + "dec %1!;$exp!", + #else + "\315\371c\261", + "\371c\261", + #endif + seqsize(2,2) - seqsize(1,1) + }, + { + #ifdef SCPACK + "dec.s %1!load.s.pri %1!;$exp!", + "dec.s %1!;$exp!", + #else + "\371\354\205\325\244", + "\371\354\261", + #endif + seqsize(2,2) - seqsize(1,1) + }, + { + #ifdef SCPACK + "load.s.pri %1!dec.s %1!;$exp!", + "dec.s %1!;$exp!", + #else + "\325\371\354\261", + "\371\354\261", + #endif + seqsize(2,2) - seqsize(1,1) + }, + /* ??? the same (increments and decrements) for references */ + /* Loading the constant zero has a special opcode. + * When storing zero in memory, the value of PRI must not be later on. + * const.pri 0 zero n1 + * stor.pri n1 ;$exp + * ;$exp - + * -------------------------------------- + * const.pri 0 zero.s n1 + * stor.s.pri n1 ;$exp + * ;$exp - + * -------------------------------------- + * zero.pri zero n1 + * stor.pri n1 ;$exp + * ;$exp - + * -------------------------------------- + * zero.pri zero.s n1 + * stor.s.pri n1 ;$exp + * ;$exp - + * -------------------------------------- + * const.pri 0 zero.pri + * -------------------------------------- + * const.alt 0 zero.alt + * The last two alternatives save more memory than they save + * time, but anyway... + */ + { + #ifdef SCPACK + "const.pri 0!stor.pri %1!;$exp!", + "zero %1!;$exp!", + #else + "\237\203 0!\227or\223\244", + "\314\261", + #endif + seqsize(2,2) - seqsize(1,1) + }, + { + #ifdef SCPACK + "const.pri 0!stor.s.pri %1!;$exp!", + "zero.s %1!;$exp!", + #else + "\237\203 0!\227or\220\223\244", + "\314\220\261", + #endif + seqsize(2,2) - seqsize(1,1) + }, + { + #ifdef SCPACK + "zero.pri!stor.pri %1!;$exp!", + "zero %1!;$exp!", + #else + "\353\227or\223\244", + "\314\261", + #endif + seqsize(2,1) - seqsize(1,1) + }, + { + #ifdef SCPACK + "zero.pri!stor.s.pri %1!;$exp!", + "zero.s %1!;$exp!", + #else + "\353\227or\220\223\244", + "\314\220\261", + #endif + seqsize(2,1) - seqsize(1,1) + }, + { + #ifdef SCPACK + "const.pri 0!", + "zero.pri!", + #else + "\237\203 0!", + "\353", + #endif + seqsize(1,1) - seqsize(1,0) + }, + { + #ifdef SCPACK + "const.alt 0!", + "zero.alt!", + #else + "\237\211 0!", + "\314\217", + #endif + seqsize(1,1) - seqsize(1,0) + }, + /* ----- */ + { NULL, NULL, 0 } +}; diff --git a/compiler/libpc300/scexpand.c b/compiler/libpc300/scexpand.c new file mode 100755 index 00000000..e0d6a9e7 --- /dev/null +++ b/compiler/libpc300/scexpand.c @@ -0,0 +1,68 @@ +/* expand.c -- Byte Pair Encoding decompression */ +/* Copyright 1996 Philip Gage */ + +/* Byte Pair Compression appeared in the September 1997 + * issue of C/C++ Users Journal. The original source code + * may still be found at the web site of the magazine + * (www.cuj.com). + * + * The decompressor has been modified by me (Thiadmer + * Riemersma) to accept a string as input, instead of a + * complete file. + */ +#include +#include +#include "sc.h" + +#define STACKSIZE 16 + +SC_FUNC int strexpand(char *dest, unsigned char *source, int maxlen, unsigned char pairtable[128][2]) +{ + unsigned char stack[STACKSIZE]; + short c, top = 0; + int len; + + assert(maxlen > 0); + len = 1; /* already 1 byte for '\0' */ + for (;;) { + + /* Pop byte from stack or read byte from the input string */ + if (top) + c = stack[--top]; + else if ((c = *(unsigned char *)source++) == '\0') + break; + + /* Push pair on stack or output byte to the output string */ + if (c > 127) { + assert(top+2 <= STACKSIZE); + stack[top++] = pairtable[c-128][1]; + stack[top++] = pairtable[c-128][0]; + } + else { + len++; + if (maxlen > 1) { /* reserve one byte for the '\0' */ + *dest++ = (char)c; + maxlen--; + } + } + } + *dest = '\0'; + return len; /* return number of bytes decoded */ +} + +#if 0 /*for testing*/ +#include "sc5.scp" + +int main (int argc, char **argv) +{ + int i; + char str[128]; + + for (i=0; i<58; i++) { + strexpand(str, errmsg[i], sizeof str, SCPACK_TABLE); + printf("%s", str); + } /* for */ + return 0; +} +#endif + diff --git a/compiler/libpc300/sci18n.c b/compiler/libpc300/sci18n.c new file mode 100755 index 00000000..13486071 --- /dev/null +++ b/compiler/libpc300/sci18n.c @@ -0,0 +1,428 @@ +/* Codepage translation to Unicode, and UTF-8 support + * + * The translation is based on codepage mapping files that are distributed + * by the Unicode consortium, see ftp://ftp.unicode.org/Public/MAPPINGS/. + * + * Character sets with a maximum of 256 codes are translated via a lookup + * table (these are Single-Byte Character Sets). Character sets like Shift-JIS + * with single-byte characters and multi-byte characters (introduced by a + * leader byte) are split into two tables: the 256-entry lookup table for + * the single-byte characters and an extended table for the multi-byte + * characters. The extended table is allocated dynamically; the lookup table + * is allocated statically, so loading SBCS tables cannot fail (if the tables + * themselves are valid, of course). + * + * Copyright (c) ITB CompuPhase, 2004-2005 + * + * This software is provided "as-is", without any express or implied warranty. + * In no event will the authors be held liable for any damages arising from + * the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software in + * a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * 3. This notice may not be removed or altered from any source distribution. + * + * Version: $Id$ + */ +#include +#include +#include +#include +#include +#include "sc.h" + +#if !defined TRUE + #define FALSE 0 + #define TRUE 1 +#endif +#if !defined _MAX_PATH + #define _MAX_PATH 250 +#endif +#if !defined DIRSEP_CHAR + #if defined LINUX || defined __FreeBSD__ || defined __OpenBSD__ + #define DIRSEP_CHAR '/' + #elif defined macintosh + #define DIRSEP_CHAR ':' + #else + #define DIRSEP_CHAR '\\' + #endif +#endif + +#if !defined ELEMENTS + #define ELEMENTS(array) (sizeof(array) / sizeof(array[0])) +#endif + +#if !defined NO_CODEPAGE + +#if !defined MAXCODEPAGE + #define MAXCODEPAGE 12 /* typically "cp" + 4 digits + ".txt" */ +#endif +#define INVALID 0xffffu /* 0xffff and 0xfffe are invalid Unicode characters */ +#define LEADBYTE 0xfffeu + +struct wordpair { + unsigned short index; + wchar_t code; +}; +static char cprootpath[_MAX_PATH] = { DIRSEP_CHAR, '\0' }; +static wchar_t bytetable[256]; +static struct wordpair *wordtable = NULL; +static unsigned wordtablesize = 0; +static unsigned wordtabletop = 0; + + +/* read in a line delimited by '\r' or '\n'; do NOT store the '\r' or '\n' into + * the string and ignore empty lines + * returns 1 for success and 0 for failure + */ +static int cp_readline(FILE *fp,char *string,size_t size) +{ + size_t count=0; + int c; + assert(size>1); + while ((c=fgetc(fp))!=EOF && count0) /* '\r' or '\n' ends a string */ + break; + /* if count==0, the line started with a '\r' or '\n', or perhaps line + * ends in the file are '\r\n' and we read and stopped on the '\r' of + * the preceding line + */ + } else { + string[count++]=(char)c; + } /* if */ + } /* while */ + string[count]='\0'; + return count>0; +} + +/* cp_path() sets the directory where all codepage files must be found (if + * the parameter to cp_set() specifies a full path, that is used instead). + * The path is specified into two parts: root and directory; the full path + * for the codepage direcory is just the concatenation of the two, with a + * directory separator in between. The directory is given in two parts, + * because often a program already retrieves its "home" directory and the + * codepages are most conveniently stored in a subdirectory of this home + * directory. + */ +SC_FUNC int cp_path(const char *root, const char *directory) +{ + size_t len1,len2; + int add_slash1,add_slash2; + + len1= (root!=NULL) ? strlen(root) : 0; + add_slash1= (len1==0 || root[len1-1]!=DIRSEP_CHAR); + len2= (directory!=NULL) ? strlen(directory) : 0; + add_slash2= (len2>0 && root[len2-1]!=DIRSEP_CHAR); + if (len1+add_slash1+len2+add_slash2>=(_MAX_PATH-MAXCODEPAGE)) + return FALSE; /* full filename may not fit */ + if (root!=NULL) + strcpy(cprootpath,root); + if (add_slash1) { + assert(len1==0 || cprootpath[len1]=='\0'); + cprootpath[len1]=DIRSEP_CHAR; + cprootpath[len1+1]='\0'; + } /* if */ + if (directory!=NULL) + strcat(cprootpath,directory); + if (add_slash2) { + assert(cprootpath[len1+add_slash1+len2]=='\0'); + cprootpath[len1+add_slash1+len2]=DIRSEP_CHAR; + cprootpath[len1+add_slash1+len2+1]='\0'; + } /* if */ + cp_set(NULL); /* start with a "linear" table (no translation) */ + return TRUE; +} + +/* cp_set() loads a codepage from a file. The name parameter may be a + * filename (including a full path) or it may be a partial codepage name. + * If the name parameter is NULL, the codepage is cleared to be a "linear" + * table (no translation). + * The following files are attempted to open (where specifies the + * value of the parameter): + * + * / + * /.txt + * /cp + * /cp.txt + */ +SC_FUNC int cp_set(const char *name) +{ + char filename[_MAX_PATH]; + FILE *fp=NULL; + unsigned index; + + /* for name==NULL, set up an identity table */ + if (name==NULL || *name=='\0') { + if (wordtable!=NULL) { + free(wordtable); + wordtable=NULL; + wordtablesize=0; + wordtabletop=0; + } /* if */ + for (index=0; indexMAXCODEPAGE) + return 0; + assert(strlen(name)+strlen(cprootpath)<_MAX_PATH); + strcpy(filename,cprootpath); + strcat(filename,name); + fp=fopen(filename,"rt"); + } /* if */ + if (fp==NULL) { + /* try opening the file in the "root path" for codepages, with a ".txt" extension */ + if (strlen(name)+4>=MAXCODEPAGE) + return 0; + assert(strlen(filename)+4<_MAX_PATH); + strcat(filename,".txt"); + fp=fopen(filename,"rt"); + } /* if */ + if (fp==NULL) { + /* try opening the file in the "root path" for codepages, with "cp" prefixed before the name */ + if (strlen(name)+2>MAXCODEPAGE) + return 0; + assert(2+strlen(name)+strlen(cprootpath)<_MAX_PATH); + strcpy(filename,cprootpath); + strcat(filename,"cp"); + strcat(filename,name); + fp=fopen(filename,"rt"); + } /* if */ + if (fp==NULL) { + /* try opening the file in the "root path" for codepages, with "cp" prefixed an ".txt" appended */ + if (strlen(name)+2+4>MAXCODEPAGE) + return 0; + assert(strlen(filename)+4<_MAX_PATH); + strcat(filename,".txt"); + fp=fopen(filename,"rt"); + } /* if */ + if (fp==NULL) + return FALSE; /* all failed */ + + /* clear the tables */ + for (index=0; index0 && wordtable!=NULL); + if (wordtable!=NULL) { + free(wordtable); + wordtable=NULL; + wordtablesize=0; + wordtabletop=0; + } /* if */ + + /* read in the table */ + while (cp_readline(fp,filename,sizeof filename)) { + char *ptr; + if ((ptr=strchr(filename,'#'))!=NULL) + *ptr='\0'; /* strip of comment */ + for (ptr=filename; *ptr>0 && *ptr<' '; ptr++) + /* nothing */; /* skip leading whitespace */ + if (*ptr!='\0') { + /* content on line */ + unsigned code=LEADBYTE; + int num=sscanf(ptr,"%i %i",&index,&code); + /* if sscanf() returns 1 and the index is in range 0..255, then the + * code is a DBCS lead byte; if sscanf() returns 2 and index>=256, this + * is a double byte pair (lead byte + follower) + */ + if (num>=1 && index<256) { + bytetable[index]=(wchar_t)code; + } else if (num==2 && index>=256 && index=wordtablesize) { + /* grow the list */ + int newsize; + struct wordpair *newblock; + newsize= (wordtablesize==0) ? 128 : 2*wordtablesize; + newblock=(struct wordpair *)malloc(newsize*sizeof(*wordtable)); + if (newblock!=NULL) { + memcpy(newblock,wordtable,wordtabletop*sizeof(*wordtable)); + free(wordtable); + wordtable=newblock; + wordtablesize=newsize; + } /* if */ + } /* if */ + if (wordtabletop0 && (unsigned)wordtable[pos-1].index>index) { + wordtable[pos]=wordtable[pos-1]; + pos--; + } /* while */ + wordtable[pos].index=(unsigned short)index; + wordtable[pos].code=(wchar_t)code; + } /* if */ + } /* if */ + } /* if */ + } /* while */ + + fclose(fp); + return TRUE; +} + +SC_FUNC cell cp_translate(const unsigned char *string,const unsigned char **endptr) +{ + wchar_t result; + + result=bytetable[*string++]; + /* check whether this is a leader code */ + if ((unsigned)result==LEADBYTE && wordtable!=NULL) { + /* look up the code via binary search */ + int low,high,mid; + unsigned short index=(unsigned short)(((*(string-1)) << 8) | *string); + string++; + assert(wordtabletop>0); + low=0; + high=wordtabletop-1; + while (lowwordtable[mid].index) + low=mid+1; + else + high=mid; + } /* while */ + assert(low==high); + if (wordtable[low].index==index) + result=wordtable[low].code; + } /* if */ + + if (endptr!=NULL) + *endptr=string; + return (cell)result; +} + +#endif /* NO_CODEPAGE */ + +#if !defined NO_UTF8 +SC_FUNC cell get_utf8_char(const unsigned char *string,const unsigned char **endptr) +{ + int follow=0; + long lowmark=0; + unsigned char ch; + cell result=0; + + if (endptr!=NULL) + *endptr=string; + + for ( ;; ) { + ch=*string++; + + if (follow>0 && (ch & 0xc0)==0x80) { + /* leader code is active, combine with earlier code */ + result=(result << 6) | (ch & 0x3f); + if (--follow==0) { + /* encoding a character in more bytes than is strictly needed, + * is not really valid UTF-8; we are strict here to increase + * the chance of heuristic dectection of non-UTF-8 text + * (JAVA writes zero bytes as a 2-byte code UTF-8, which is invalid) + */ + if (result=0xd800 && result<=0xdfff || result==0xfffe || result==0xffff) + return -1; + } /* if */ + break; + } else if (follow==0 && (ch & 0x80)==0x80) { + /* UTF-8 leader code */ + if ((ch & 0xe0)==0xc0) { + /* 110xxxxx 10xxxxxx */ + follow=1; + lowmark=0x80L; + result=ch & 0x1f; + } else if ((ch & 0xf0)==0xe0) { + /* 1110xxxx 10xxxxxx 10xxxxxx (16 bits, BMP plane) */ + follow=2; + lowmark=0x800L; + result=ch & 0x0f; + } else if ((ch & 0xf8)==0xf0) { + /* 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */ + follow=3; + lowmark=0x10000L; + result=ch & 0x07; + } else if ((ch & 0xfc)==0xf8) { + /* 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx */ + follow=4; + lowmark=0x200000L; + result=ch & 0x03; + } else if ((ch & 0xfe)==0xfc) { + /* 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx (32 bits) */ + follow=5; + lowmark=0x4000000L; + result=ch & 0x01; + } else { + /* this is invalid UTF-8 */ + return -1; + } /* if */ + } else if (follow==0 && (ch & 0x80)==0x00) { + /* 0xxxxxxx (US-ASCII) */ + result=ch; + break; + } else { + /* this is invalid UTF-8 */ + return -1; + } /* if */ + + } /* for */ + + if (endptr!=NULL) + *endptr=string; + return result; +} +#endif + +SC_FUNC int scan_utf8(FILE *fp,const char *filename) +{ + #if defined NO_UTF8 + return 0; + #else + void *resetpos=pc_getpossrc(fp); + int utf8=TRUE; + int firstchar=TRUE,bom_found=FALSE; + const unsigned char *ptr; + + while (utf8 && pc_readsrc(fp,pline,sLINEMAX)!=NULL) { + ptr=pline; + if (firstchar) { + /* check whether the very first character on the very first line + * starts with a BYTE order mark + */ + cell c=get_utf8_char(ptr,&ptr); + bom_found= (c==0xfeff); + utf8= (c>=0); + firstchar=FALSE; + } /* if */ + while (utf8 && *ptr!='\0') + utf8= (get_utf8_char(ptr,&ptr)>=0); + } /* while */ + pc_resetsrc(fp,resetpos); + if (bom_found) { + unsigned char bom[3]; + if (!utf8) + error(77,filename); /* malformed UTF-8 encoding */ + pc_readsrc(fp,bom,3); + assert(bom[0]==0xef && bom[1]==0xbb && bom[2]==0xbf); + } /* if */ + return utf8; + #endif /* NO_UTF8 */ +} diff --git a/compiler/libpc300/sclist.c b/compiler/libpc300/sclist.c new file mode 100755 index 00000000..9744537b --- /dev/null +++ b/compiler/libpc300/sclist.c @@ -0,0 +1,459 @@ +/* Pawn compiler - maintenance of various lists + * + * o Name list (aliases) + * o Include path list + * o Macro defintions (text substitutions) + * + * Copyright (c) ITB CompuPhase, 2001-2005 + * + * This software is provided "as-is", without any express or implied warranty. + * In no event will the authors be held liable for any damages arising from + * the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software in + * a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * 3. This notice may not be removed or altered from any source distribution. + * + * Version: $Id$ + */ +#include +#include +#include +#include "sc.h" + +#if defined FORTIFY + #include "fortify.h" +#endif + +/* a "private" implementation of strdup(), so that porting + * to other memory allocators becomes easier. + * By Søren Hannibal. + */ +SC_FUNC char* duplicatestring(const char* sourcestring) +{ + char* result=(char*)malloc(strlen(sourcestring)+1); + strcpy(result,sourcestring); + return result; +} + + +static stringpair *insert_stringpair(stringpair *root,char *first,char *second,int matchlength) +{ + stringpair *cur,*pred; + + assert(root!=NULL); + assert(first!=NULL); + assert(second!=NULL); + /* create a new node, and check whether all is okay */ + if ((cur=(stringpair*)malloc(sizeof(stringpair)))==NULL) + return NULL; + cur->first=duplicatestring(first); + cur->second=duplicatestring(second); + cur->matchlength=matchlength; + if (cur->first==NULL || cur->second==NULL) { + if (cur->first!=NULL) + free(cur->first); + if (cur->second!=NULL) + free(cur->second); + free(cur); + return NULL; + } /* if */ + /* link the node to the tree, find the position */ + for (pred=root; pred->next!=NULL && strcmp(pred->next->first,first)<0; pred=pred->next) + /* nothing */; + cur->next=pred->next; + pred->next=cur; + return cur; +} + +static void delete_stringpairtable(stringpair *root) +{ + stringpair *cur, *next; + + assert(root!=NULL); + cur=root->next; + while (cur!=NULL) { + next=cur->next; + assert(cur->first!=NULL); + assert(cur->second!=NULL); + free(cur->first); + free(cur->second); + free(cur); + cur=next; + } /* while */ + memset(root,0,sizeof(stringpair)); +} + +static stringpair *find_stringpair(stringpair *cur,char *first,int matchlength) +{ + int result=0; + + assert(matchlength>0); /* the function cannot handle zero-length comparison */ + assert(first!=NULL); + while (cur!=NULL && result<=0) { + result=(int)*cur->first - (int)*first; + if (result==0 && matchlength==cur->matchlength) { + result=strncmp(cur->first,first,matchlength); + if (result==0) + return cur; + } /* if */ + cur=cur->next; + } /* while */ + return NULL; +} + +static int delete_stringpair(stringpair *root,stringpair *item) +{ + stringpair *cur; + + assert(root!=NULL); + cur=root; + while (cur->next!=NULL) { + if (cur->next==item) { + cur->next=item->next; /* unlink from list */ + assert(item->first!=NULL); + assert(item->second!=NULL); + free(item->first); + free(item->second); + free(item); + return TRUE; + } /* if */ + cur=cur->next; + } /* while */ + return FALSE; +} + +/* ----- string list functions ----------------------------------- */ +static stringlist *insert_string(stringlist *root,char *string) +{ + stringlist *cur; + + assert(string!=NULL); + if ((cur=(stringlist*)malloc(sizeof(stringlist)))==NULL) + error(103); /* insufficient memory (fatal error) */ + if ((cur->line=duplicatestring(string))==NULL) + error(103); /* insufficient memory (fatal error) */ + /* insert as "last" */ + assert(root!=NULL); + while (root->next!=NULL) + root=root->next; + cur->next=root->next; + root->next=cur; + return cur; +} + +static char *get_string(stringlist *root,int index) +{ + stringlist *cur; + + assert(root!=NULL); + cur=root->next; + while (cur!=NULL && index-->0) + cur=cur->next; + if (cur!=NULL) { + assert(cur->line!=NULL); + return cur->line; + } /* if */ + return NULL; +} + +static int delete_string(stringlist *root,int index) +{ + stringlist *cur,*item; + + assert(root!=NULL); + for (cur=root; cur->next!=NULL && index>0; cur=cur->next,index--) + /* nothing */; + if (cur->next!=NULL) { + item=cur->next; + cur->next=item->next; /* unlink from list */ + assert(item->line!=NULL); + free(item->line); + free(item); + return TRUE; + } /* if */ + return FALSE; +} + +SC_FUNC void delete_stringtable(stringlist *root) +{ + stringlist *cur,*next; + + assert(root!=NULL); + cur=root->next; + while (cur!=NULL) { + next=cur->next; + assert(cur->line!=NULL); + free(cur->line); + free(cur); + cur=next; + } /* while */ + memset(root,0,sizeof(stringlist)); +} + + +/* ----- alias table --------------------------------------------- */ +static stringpair alias_tab = {NULL, NULL, NULL}; /* alias table */ + +SC_FUNC stringpair *insert_alias(char *name,char *alias) +{ + stringpair *cur; + + assert(name!=NULL); + assert(strlen(name)<=sNAMEMAX); + assert(alias!=NULL); + assert(strlen(alias)<=sNAMEMAX); + if ((cur=insert_stringpair(&alias_tab,name,alias,strlen(name)))==NULL) + error(103); /* insufficient memory (fatal error) */ + return cur; +} + +SC_FUNC int lookup_alias(char *target,char *name) +{ + stringpair *cur=find_stringpair(alias_tab.next,name,strlen(name)); + if (cur!=NULL) { + assert(strlen(cur->second)<=sNAMEMAX); + strcpy(target,cur->second); + } /* if */ + return cur!=NULL; +} + +SC_FUNC void delete_aliastable(void) +{ + delete_stringpairtable(&alias_tab); +} + +/* ----- include paths list -------------------------------------- */ +static stringlist includepaths = {NULL, NULL}; /* directory list for include files */ + +SC_FUNC stringlist *insert_path(char *path) +{ + return insert_string(&includepaths,path); +} + +SC_FUNC char *get_path(int index) +{ + return get_string(&includepaths,index); +} + +SC_FUNC void delete_pathtable(void) +{ + delete_stringtable(&includepaths); + assert(includepaths.next==NULL); +} + + +/* ----- text substitution patterns ------------------------------ */ +#if !defined NO_DEFINE + +static stringpair substpair = { NULL, NULL, NULL}; /* list of substitution pairs */ + +static stringpair *substindex['z'-'A'+1]; /* quick index to first character */ +static void adjustindex(char c) +{ + stringpair *cur; + assert(c>='A' && c<='Z' || c>='a' && c<='z' || c=='_'); + assert('A'<'_' && '_'<'z'); + + for (cur=substpair.next; cur!=NULL && cur->first[0]!=c; cur=cur->next) + /* nothing */; + substindex[(int)c-'A']=cur; +} + +SC_FUNC stringpair *insert_subst(char *pattern,char *substitution,int prefixlen) +{ + stringpair *cur; + + assert(pattern!=NULL); + assert(substitution!=NULL); + if ((cur=insert_stringpair(&substpair,pattern,substitution,prefixlen))==NULL) + error(103); /* insufficient memory (fatal error) */ + adjustindex(*pattern); + return cur; +} + +SC_FUNC stringpair *find_subst(char *name,int length) +{ + stringpair *item; + assert(name!=NULL); + assert(length>0); + assert(*name>='A' && *name<='Z' || *name>='a' && *name<='z' || *name=='_'); + item=substindex[(int)*name-'A']; + if (item!=NULL) + item=find_stringpair(item,name,length); + return item; +} + +SC_FUNC int delete_subst(char *name,int length) +{ + stringpair *item; + assert(name!=NULL); + assert(length>0); + assert(*name>='A' && *name<='Z' || *name>='a' && *name<='z' || *name=='_'); + item=substindex[(int)*name-'A']; + if (item!=NULL) + item=find_stringpair(item,name,length); + if (item==NULL) + return FALSE; + delete_stringpair(&substpair,item); + adjustindex(*name); + return TRUE; +} + +SC_FUNC void delete_substtable(void) +{ + int i; + delete_stringpairtable(&substpair); + for (i=0; i0) + linenr--; /* line numbers are zero-based in the debug information */ + sprintf(string,"L:%08lx %04x",(long)code_idx,linenr); + return insert_string(&dbgstrings,string); + } /* if */ + return NULL; +} + +SC_FUNC stringlist *insert_dbgsymbol(symbol *sym) +{ + if (sc_status==statWRITE && (sc_debug & sSYMBOLIC)!=0) { + char string[2*sNAMEMAX+128]; + char symname[2*sNAMEMAX+16]; + #if !defined NDEBUG + int count=0; + #endif + + funcdisplayname(symname,sym->name); + /* address tag:name codestart codeend ident vclass [tag:dim ...] */ + if (sym->ident==iFUNCTN) + sprintf(string,"S:%08lx %x:%s %08lx %08lx %x %x",sym->addr,sym->tag, + symname,sym->addr,sym->codeaddr,sym->ident,sym->vclass); + else + sprintf(string,"S:%08lx %x:%s %08lx %08lx %x %x",sym->addr,sym->tag, + symname,sym->codeaddr,code_idx,sym->ident,sym->vclass); + if (sym->ident==iARRAY || sym->ident==iREFARRAY) { + symbol *sub; + strcat(string," [ "); + for (sub=sym; sub!=NULL; sub=finddepend(sub)) { + assert(sub->dim.array.level==count++); + sprintf(string+strlen(string),"%x:%x ",sub->x.idxtag,sub->dim.array.length); + } /* for */ + strcat(string,"]"); + } /* if */ + + return insert_string(&dbgstrings,string); + } /* if */ + return NULL; +} + +SC_FUNC char *get_dbgstring(int index) +{ + return get_string(&dbgstrings,index); +} + +SC_FUNC void delete_dbgstringtable(void) +{ + delete_stringtable(&dbgstrings); + assert(dbgstrings.next==NULL); +} diff --git a/compiler/libpc300/scmemfil.c b/compiler/libpc300/scmemfil.c new file mode 100755 index 00000000..4ad146da --- /dev/null +++ b/compiler/libpc300/scmemfil.c @@ -0,0 +1,339 @@ +/* Pawn compiler + * + * Routines to maintain a "text file" in memory. + * + * Copyright (c) ITB CompuPhase, 2003-2005 + * + * This software is provided 'as-is', without any express or implied warranty. + * In no event will the authors be held liable for any damages arising from the + * use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software in + * a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any source distribution. + * + * Version: $Id$ + */ + +#include +#include +#include +#include + +#if defined FORTIFY + #include "fortify.h" +#endif + + +#define BUFFERSIZE 512u + +/* For every block, except the first: + * buffer points to a block that is BUFFERSIZE long that holds the data + * bufpos is the "used size" of the block + * For the first block: + * buffer points to the "file name" + * bufpos is the current "file pointer" + */ +typedef struct tagMEMFILE { + struct tagMEMFILE *next; + unsigned char *buffer; + long bufpos; +} MEMFILE; +#define tMEMFILE 1 + +#include "sc.h" + + +MEMFILE *mfcreate(char *filename) +{ + MEMFILE *mf; + + /* create a first block that only holds the name */ + mf=(MEMFILE*)malloc(sizeof(MEMFILE)); + if (mf==NULL) + return NULL; + memset(mf,0,sizeof(MEMFILE)); + mf->buffer=(unsigned char*)strdup(filename); + if (mf->buffer==NULL) { + free(mf); + return NULL; + } /* if */ + return mf; +} + +void mfclose(MEMFILE *mf) +{ + MEMFILE *next; + + assert(mf!=NULL); + while (mf!=NULL) { + next=mf->next; + assert(mf->buffer!=NULL); + free(mf->buffer); + free(mf); + mf=next; + } /* while */ +} + +int mfdump(MEMFILE *mf) +{ + FILE *fp; + int okay; + + assert(mf!=NULL); + /* create the file */ + fp=fopen((char*)mf->buffer,"wb"); + if (fp==NULL) + return 0; + + okay=1; + mf=mf->next; + while (mf!=NULL) { + assert(mf->buffer!=NULL); + /* all blocks except the last should be fully filled */ + assert(mf->next==NULL || (unsigned long)mf->bufpos==BUFFERSIZE); + okay=okay && fwrite(mf->buffer,1,(size_t)mf->bufpos,fp)==(size_t)mf->bufpos; + mf=mf->next; + } /* while */ + + fclose(fp); + return okay; +} + +long mflength(MEMFILE *mf) +{ + long length; + + assert(mf!=NULL); + /* find the size of the memory file */ + length=0L; + mf=mf->next; /* skip initial block */ + while (mf!=NULL) { + assert(mf->next==NULL || (unsigned long)mf->bufpos==BUFFERSIZE); + length+=mf->bufpos; + mf=mf->next; + } /* while */ + + return length; +} + +long mfseek(MEMFILE *mf,long offset,int whence) +{ + long length; + + assert(mf!=NULL); + if (mf->next==NULL) + return 0L; /* early exit: not a single byte in the file */ + + /* find the size of the memory file */ + length=mflength(mf); + + /* convert the offset to an absolute position */ + switch (whence) { + case SEEK_SET: + break; + case SEEK_CUR: + offset+=mf->bufpos; + break; + case SEEK_END: + assert(offset<=0); + offset+=length; + break; + } /* switch */ + + /* clamp to the file length limit */ + if (offset<0) + offset=0; + else if (offset>length) + offset=length; + + /* set new position and return it */ + mf->bufpos=offset; + return offset; +} + +unsigned int mfwrite(MEMFILE *mf,unsigned char *buffer,unsigned int size) +{ + long length; + long numblocks; + int blockpos,blocksize; + unsigned int bytes; + MEMFILE *block; + + assert(mf!=NULL); + + /* see whether more memory must be allocated */ + length=mflength(mf); + assert(mf->bufpos>=0 && mf->bufpos<=length); + numblocks=(length+BUFFERSIZE-1)/BUFFERSIZE; /* # allocated blocks */ + while (mf->bufpos+size>numblocks*BUFFERSIZE) { + /* append a block */ + MEMFILE *last; + block=(MEMFILE*)malloc(sizeof(MEMFILE)); + if (block==NULL) + return 0; + memset(block,0,sizeof(MEMFILE)); + block->buffer=(unsigned char*)malloc(BUFFERSIZE); + if (block->buffer==NULL) { + free(block); + return 0; + } /* if */ + for (last=mf; last->next!=NULL; last=last->next) + /* nothing */; + assert(last!=NULL); + assert(last->next==NULL); + last->next=block; + numblocks++; + } /* while */ + + if (size==0) + return 0; + + /* find the block to start writing to */ + numblocks=mf->bufpos/BUFFERSIZE; /* # blocks to skip */ + block=mf->next; + while (numblocks-->0) { + assert(block!=NULL); + block=block->next; + } /* while */ + assert(block!=NULL); + + /* copy into memory */ + bytes=0; + blockpos=(int)(mf->bufpos % BUFFERSIZE); + do { + blocksize=BUFFERSIZE-blockpos; + assert(blocksize>=0); + if ((unsigned int)blocksize>size) + blocksize=size; + + assert(block!=NULL); + memcpy(block->buffer+blockpos,buffer,blocksize); + buffer+=blocksize; + size-=blocksize; + bytes+=blocksize; + + if (blockpos+blocksize>block->bufpos) + block->bufpos=blockpos+blocksize; + assert(block->bufpos>=0 && (unsigned long)block->bufpos<=BUFFERSIZE); + block=block->next; + blockpos=0; + } while (size>0); + + /* adjust file pointer */ + mf->bufpos+=bytes; + + return bytes; +} + +unsigned int mfread(MEMFILE *mf,unsigned char *buffer,unsigned int size) +{ + long length; + long numblocks; + int blockpos,blocksize; + unsigned int bytes; + MEMFILE *block; + + assert(mf!=NULL); + + /* adjust the size to read */ + length=mflength(mf); + assert(mf->bufpos>=0 && mf->bufpos<=length); + if (mf->bufpos+size>(unsigned long)length) + size=(int)(length-mf->bufpos); + assert(mf->bufpos+size<=(unsigned long)length); + if (size==0) + return 0; + + /* find the block to start reading from */ + numblocks=mf->bufpos/BUFFERSIZE; /* # blocks to skip */ + block=mf->next; + while (numblocks-->0) { + assert(block!=NULL); + block=block->next; + } /* while */ + assert(block!=NULL); + + /* copy out of memory */ + bytes=0; + blockpos=(int)(mf->bufpos % BUFFERSIZE); + do { + blocksize=BUFFERSIZE-blockpos; + if ((unsigned int)blocksize>size) + blocksize=size; + + assert(block!=NULL); + assert(block->bufpos>=0 && (unsigned long)block->bufpos<=BUFFERSIZE); + assert(blockpos+blocksize<=block->bufpos); + memcpy(buffer,block->buffer+blockpos,blocksize); + buffer+=blocksize; + size-=blocksize; + bytes+=blocksize; + + block=block->next; + blockpos=0; + } while (size>0); + + /* adjust file pointer */ + mf->bufpos+=bytes; + + return bytes; +} + +char *mfgets(MEMFILE *mf,char *string,unsigned int size) +{ + char *ptr; + unsigned int read; + long seek; + + assert(mf!=NULL); + + read=mfread(mf,(unsigned char *)string,size); + if (read==0) + return NULL; + seek=0L; + + /* make sure that the string is zero-terminated */ + assert(read<=size); + if (read +#include +#include +#include +#include + +#if UINT_MAX > 0xFFFFU + #define MAXSIZE 1024*1024L +#else + #define MAXSIZE UINT_MAX /* Input file buffer size */ +#endif +#define HASHSIZE 8192 /* Hash table size, power of 2 */ +#define THRESHOLD 3 /* Increase for speed, min 3 */ + +#define START_TOKEN "#ifdef SCPACK" /* start reading the buffer here */ +#define NAME_TOKEN "#define SCPACK_TABLE" +#define SEP_TOKEN "#define SCPACK_SEPARATOR" +#define TERM_TOKEN "#define SCPACK_TERMINATOR" +#define TEMPFILE "~SCPACK.TMP" +static char tablename[32+1] = "scpack_table"; +static char separator[16]=","; +static char terminator[16]=""; + +int compress(unsigned char *buffer, unsigned buffersize, unsigned char pairtable[128][2]) +{ + unsigned char *left, *right, *count; + unsigned char a, b, bestcount; + unsigned i, j, index, bestindex, code=128; + + /* Dynamically allocate buffers and check for errors */ + left = (unsigned char *)malloc(HASHSIZE); + right = (unsigned char *)malloc(HASHSIZE); + count = (unsigned char *)malloc(HASHSIZE); + if (left==NULL || right==NULL || count==NULL) { + printf("Error allocating memory\n"); + exit(1); + } + + /* Check for errors */ + for (i=0; i 127) { + printf("This program works only on text files (7-bit ASCII)\n"); + exit(1); + } + + memset(pairtable, 0, 128*2*sizeof(char)); + + do { /* Replace frequent pairs with bytes 128..255 */ + + /* Enter counts of all byte pairs into hash table */ + memset(count,0,HASHSIZE); + for (i=0; i bestcount) { + bestcount = count[i]; + bestindex = i; + } + } + + /* Compress if enough occurrences of pair */ + if (bestcount >= THRESHOLD) { + + /* Add pair to table using code as index */ + a = pairtable[code-128][0] = left[bestindex]; + b = pairtable[code-128][1] = right[bestindex]; + + /* Replace all pair occurrences with unused byte */ + for (i=0, j=0; i= 128 || *bufptr == '"' || *bufptr == '\\') + fprintf(output, "\\%03o", *bufptr); + else + fprintf(output, "%c", *bufptr); + bufptr++; + } /* while */ + fprintf(output, "\""); + needseparator = 1; + bufptr++; /* skip '\0' */ + } /* while */ + fprintf(output, "%s\n",terminator); + bufptr++; + + /* skip the input file until the #endif section */ + while (fgets(str,sizeof str,input)!=NULL) { + if (strmatch(str,"#endif",NULL)) { + fprintf(output,"%s",str); + break; /* done */ + } /* if */ + } /* while */ + } /* while - !feof(input) */ +} + +static void usage(void) +{ + printf("Usage: scpack [output file]\n"); + exit(1); +} + +int main(int argc, char **argv) +{ + FILE *in, *out; + unsigned char *buffer; + unsigned buffersize, orgbuffersize; + unsigned char pairtable[128][2]; + + if (argc < 2 || argc > 3) + usage(); + if ((in=fopen(argv[1],"rt"))==NULL) { + printf("SCPACK: error opening input %s\n",argv[1]); + usage(); + } /* if */ + if (argc == 2) { + if ((out=fopen(TEMPFILE,"wt"))==NULL) { + printf("SCPACK: error opening temporary file %s\n",TEMPFILE); + usage(); + } /* if */ + } else { + if ((out=fopen(argv[2],"wt"))==NULL) { + printf("SCPACK: error opening output file %s\n",argv[2]); + usage(); + } /* if */ + } /* if */ + + buffer = (unsigned char *)malloc(MAXSIZE); + if (buffer == NULL) { + printf("SCPACK: error allocating memory\n"); + return 1; + } /* if */ + /* 1. read the buffer + * 2. compress the buffer + * 3. copy the file, insert the compressed buffer + */ + buffersize = readbuffer(in, buffer); + orgbuffersize = buffersize; + if (buffersize > 0) { + buffersize = compress(buffer, buffersize, pairtable); + writefile(in, out, buffer, buffersize, pairtable); + printf("SCPACK: compression ratio: %ld%% (%d -> %d)\n", + 100L-(100L*buffersize)/orgbuffersize, orgbuffersize, buffersize); + } else { + printf("SCPACK: no SCPACK section found, nothing to do\n"); + } /* if */ + fclose(out); + fclose(in); + /* let the new file replace the old file */ + if (buffersize == 0) { + if (argc == 2) + remove(TEMPFILE); + else + remove(argv[2]); + } else if (argc == 2) { + remove(argv[1]); + rename(TEMPFILE,argv[1]); + } /* if */ + return 0; +} diff --git a/compiler/libpc300/scstate.c b/compiler/libpc300/scstate.c new file mode 100755 index 00000000..7daf7d61 --- /dev/null +++ b/compiler/libpc300/scstate.c @@ -0,0 +1,347 @@ +/* Pawn compiler + * + * Machine and state maintenance. + * + * Three lists are maintained here: + * - A list of automatons (state machines): these hold a name, a unique id + * (in the "index" field) and the memory address of a cell that holds the + * current state of the automaton (in the "value" field). + * - A list of states for each automaton: a name, an automaton id (in the + * "index" field) and a unique id for the state (unique in the automaton; + * states belonging to different automatons may have the same id). + * - A list of state combinations. Each function may belong to a set of states. + * This list assigns a unique id to the combination of the automaton and all + * states. + * + * For a function that has states, there is a fourth list, which is attached + * to the "symbol" structure. This list contains the code label (in the "name" + * field), the id of the state combinations (the state list id; it is stored + * in the "index" field) and the code address at which the function starts. + * The latter is currently unused. + * + * At the start of the compiled code, a set of stub functions is generated. + * Each stub function looks up the value of the "state selector" value for the + * automaton, and goes with a "switch" instruction to the start address of the + * function. This happens in SC4.C. + * + * + * Copyright (c) ITB CompuPhase, 2005 + * + * This software is provided "as-is", without any express or implied warranty. + * In no event will the authors be held liable for any damages arising from + * the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software in + * a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * 3. This notice may not be removed or altered from any source distribution. + * + * Version: $Id$ + */ +#include +#include +#include +#include +#include +#include "sc.h" +#if defined LINUX || defined __FreeBSD__ || defined __OpenBSD__ + #include +#endif + +#if defined FORTIFY + #include "fortify.h" +#endif + +typedef struct s_statelist { + struct s_statelist *next; + int *states; /* list of states in this combination */ + int numstates; /* number of items in the above list */ + int fsa; /* automaton id */ + int listid; /* unique id for this combination list */ +} statelist; + +static statelist statelist_tab = { NULL, NULL, 0, 0, 0}; /* state combinations table */ + + +static constvalue *find_automaton(const char *name,int *last) +{ + constvalue *ptr; + + assert(last!=NULL); + *last=0; + ptr=sc_automaton_tab.next; + while (ptr!=NULL) { + if (strcmp(name,ptr->name)==0) + return ptr; + if (ptr->index>*last) + *last=ptr->index; + ptr=ptr->next; + } /* while */ + return NULL; +} + +SC_FUNC constvalue *automaton_add(const char *name) +{ + constvalue *ptr; + int last; + + assert(strlen(name)name)); + ptr=find_automaton(name,&last); + if (ptr==NULL) { + assert(last+1 <= SHRT_MAX); + ptr=append_constval(&sc_automaton_tab,name,(cell)0,(short)(last+1)); + /* for every new automaton, create an anonymous (invalid) state */ + state_add("",last+1); + } /* if */ + return ptr; +} + +SC_FUNC constvalue *automaton_find(const char *name) +{ + int last; + return find_automaton(name,&last); +} + +SC_FUNC constvalue *automaton_findid(int id) +{ + constvalue *ptr; + for (ptr=sc_automaton_tab.next; ptr!=NULL && ptr->index!=id; ptr=ptr->next) + /* nothing */; + return ptr; +} + + +static constvalue *find_state(const char *name,int fsa,int *last) +{ + constvalue *ptr; + + assert(last!=NULL); + *last=0; + ptr=sc_state_tab.next; + while (ptr!=NULL) { + if (ptr->index==fsa) { + if (strcmp(name,ptr->name)==0) + return ptr; + if ((int)ptr->value>*last) + *last=(int)ptr->value; + } /* if */ + ptr=ptr->next; + } /* while */ + return NULL; +} + +SC_FUNC constvalue *state_add(const char *name,int fsa) +{ + constvalue *ptr; + int last; + + assert(strlen(name)name)); + ptr=find_state(name,fsa,&last); + if (ptr==NULL) { + assert(fsa <= SHRT_MAX); + ptr=append_constval(&sc_state_tab,name,(cell)(last+1),(short)fsa); + } /* if */ + return ptr; +} + +SC_FUNC constvalue *state_find(const char *name,int fsa_id) +{ + int last; /* dummy */ + return find_state(name,fsa_id,&last); +} + +SC_FUNC constvalue *state_findid(int id) +{ + constvalue *ptr; + for (ptr=sc_state_tab.next; ptr!=NULL && ptr->value!=id; ptr=ptr->next) + /* nothing */; + return ptr; +} + +SC_FUNC void state_buildlist(int **list,int *listsize,int *count,int stateid) +{ + int idx; + + assert(list!=NULL); + assert(listsize!=NULL); + assert(*listsize>=0); + assert(count!=NULL); + assert(*count>=0); + assert(*count<=*listsize); + + if (*count==*listsize) { + /* To avoid constantly calling malloc(), the list is grown by 4 states at + * a time. + */ + *listsize+=4; + *list=(int*)realloc(*list,*listsize*sizeof(int)); + if (*list==NULL) + error(103); /* insufficient memory */ + } /* if */ + + /* find the insertion point (the list has to stay sorted) */ + for (idx=0; idx<*count && *list[idx]0); + assert(last!=NULL); + *last=0; + ptr=statelist_tab.next; + while (ptr!=NULL) { + if (ptr->listid>*last) + *last=ptr->listid; + if (ptr->fsa==fsa && ptr->numstates==count) { + /* compare all states */ + for (i=0; istates[i]==list[i]; i++) + /* nothing */; + if (i==count) + return ptr; + } /* if */ + ptr=ptr->next; + } /* while */ + return NULL; +} + +static statelist *state_getlist_ptr(int listid) +{ + statelist *ptr; + + assert(listid>0); + for (ptr=statelist_tab.next; ptr!=NULL && ptr->listid!=listid; ptr=ptr->next) + /* nothing */; + return ptr; +} + +SC_FUNC int state_addlist(int *list,int count,int fsa) +{ + statelist *ptr; + int last; + + assert(list!=NULL); + assert(count>0); + ptr=state_findlist(list,count,fsa,&last); + if (ptr==NULL) { + if ((ptr=(statelist*)malloc(sizeof(statelist)))==NULL) + error(103); /* insufficient memory */ + if ((ptr->states=(int*)malloc(count*sizeof(int)))==NULL) { + free(ptr); + error(103); /* insufficient memory */ + } /* if */ + memcpy(ptr->states,list,count*sizeof(int)); + ptr->numstates=count; + ptr->fsa=fsa; + ptr->listid=last+1; + ptr->next=statelist_tab.next; + statelist_tab.next=ptr; + } /* if */ + assert(ptr!=NULL); + return ptr->listid; +} + +SC_FUNC void state_deletetable(void) +{ + statelist *ptr; + + while (statelist_tab.next!=NULL) { + ptr=statelist_tab.next; + /* unlink first */ + statelist_tab.next=ptr->next; + /* then delete */ + assert(ptr->states!=NULL); + free(ptr->states); + free(ptr); + } /* while */ +} + +SC_FUNC int state_getfsa(int listid) +{ + statelist *ptr=state_getlist_ptr(listid); + return (ptr!=NULL) ? ptr->fsa : -1; /* fsa 0 exists */ +} + +SC_FUNC int state_count(int listid) +{ + statelist *ptr=state_getlist_ptr(listid); + if (ptr==NULL) + return 0; /* unknown list, no states in it */ + return ptr->numstates; +} + +SC_FUNC int state_inlist(int listid,int state) +{ + statelist *ptr; + int i; + + ptr=state_getlist_ptr(listid); + if (ptr==NULL) + return FALSE; /* unknown list, state not in it */ + for (i=0; inumstates; i++) + if (ptr->states[i]==state) + return TRUE; + return FALSE; +} + +SC_FUNC int state_listitem(int listid,int index) +{ + statelist *ptr; + + ptr=state_getlist_ptr(listid); + assert(ptr!=NULL); + assert(index>=0 && indexnumstates); + return ptr->states[index]; +} + +/* This function searches whether one of the states in the list of statelist id's + * of a symbol exists in any other statelist id's of the same function; it also + * verifies that all definitions of the symbol are in the same automaton. + */ +SC_FUNC void state_conflict(symbol *root) +{ + statelist *psrc,*ptgt; + constvalue *srcptr,*tgtptr; + int s,t; + symbol *sym; + + assert(root!=NULL); + for (sym=root->next; sym!=NULL; sym=sym->next) { + if (sym->parent!=NULL || sym->ident!=iFUNCTN) + continue; /* hierarchical data type or no function */ + if (sym->states==NULL) + continue; /* this function has no states */ + for (srcptr=sym->states->next; srcptr!=NULL; srcptr=srcptr->next) { + if (srcptr->index==-1) + continue; /* state list id -1 is a special case */ + psrc=state_getlist_ptr(srcptr->index); + assert(psrc!=NULL); + for (tgtptr=srcptr->next; tgtptr!=NULL; tgtptr=tgtptr->next) { + if (tgtptr->index==-1) + continue; /* state list id -1 is a special case */ + ptgt=state_getlist_ptr(tgtptr->index); + assert(ptgt!=NULL); + if (psrc->fsa!=ptgt->fsa && strcmp(sym->name,uENTRYFUNC)!=0) + error(83,sym->name); /* this function is part of another machine */ + for (s=0; snumstates; s++) + for (t=0; tnumstates; t++) + if (psrc->states[s]==ptgt->states[t]) + error(84,sym->name); /* state conflict */ + } /* for (tgtptr) */ + } /* for (srcptr) */ + } /* for (sym) */ +} diff --git a/compiler/libpc300/scstub.c b/compiler/libpc300/scstub.c new file mode 100755 index 00000000..db07520b --- /dev/null +++ b/compiler/libpc300/scstub.c @@ -0,0 +1,29 @@ +#include +#include +#include + +static char filename[] = "pccdos.exe"; + +int main(int argc, char *argv[]) +{ + int result; + + /* build a command line to pass on to the "DOS" program */ + char path[80], *ptr; + strcpy(path,argv[0]); + ptr=strrchr(path,'\\'); + if (ptr==NULL) + ptr=strchr(path,':'); + if (ptr==NULL) { + strcpy(path,filename); + } else { + strcpy(ptr+1,filename); + } /* if */ + + /* launch the DOS version of the tool */ + result=execv(path,argv); + if (result==-1) + printf("Error launching '%s'\n",path); + return result; +} + diff --git a/compiler/libpc300/scvars.c b/compiler/libpc300/scvars.c new file mode 100755 index 00000000..87edb8d5 --- /dev/null +++ b/compiler/libpc300/scvars.c @@ -0,0 +1,108 @@ +/* Pawn compiler + * + * Global (cross-module) variables. + * + * Copyright (c) ITB CompuPhase, 1997-2005 + * + * This software is provided "as-is", without any express or implied warranty. + * In no event will the authors be held liable for any damages arising from + * the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software in + * a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * 3. This notice may not be removed or altered from any source distribution. + * + * Version: $Id$ + */ +#include +#include /* for _MAX_PATH */ +#include "sc.h" + +/* global variables + * + * All global variables that are shared amongst the compiler files are + * declared here. + */ +SC_VDEFINE symbol loctab; /* local symbol table */ +SC_VDEFINE symbol glbtab; /* global symbol table */ +SC_VDEFINE cell *litq; /* the literal queue */ +SC_VDEFINE unsigned char pline[sLINEMAX+1]; /* the line read from the input file */ +SC_VDEFINE const unsigned char *lptr; /* points to the current position in "pline" */ +SC_VDEFINE constvalue tagname_tab = { NULL, "", 0, 0}; /* tagname table */ +SC_VDEFINE constvalue libname_tab = { NULL, "", 0, 0}; /* library table (#pragma library "..." syntax) */ +SC_VDEFINE constvalue *curlibrary = NULL; /* current library */ +SC_VDEFINE int pc_addlibtable = TRUE; /* is the library table added to the AMX file? */ +SC_VDEFINE symbol *curfunc; /* pointer to current function */ +SC_VDEFINE char *inpfname; /* pointer to name of the file currently read from */ +SC_VDEFINE char outfname[_MAX_PATH]; /* intermediate (assembler) file name */ +SC_VDEFINE char binfname[_MAX_PATH]; /* binary file name */ +SC_VDEFINE char errfname[_MAX_PATH]; /* error file name */ +SC_VDEFINE char sc_ctrlchar = CTRL_CHAR; /* the control character (or escape character)*/ +SC_VDEFINE char sc_ctrlchar_org = CTRL_CHAR;/* the default control character */ +SC_VDEFINE int litidx = 0; /* index to literal table */ +SC_VDEFINE int litmax = sDEF_LITMAX; /* current size of the literal table */ +SC_VDEFINE int stgidx = 0; /* index to the staging buffer */ +SC_VDEFINE int sc_labnum = 0; /* number of (internal) labels */ +SC_VDEFINE int staging = 0; /* true if staging output */ +SC_VDEFINE cell declared = 0; /* number of local cells declared */ +SC_VDEFINE cell glb_declared=0; /* number of global cells declared */ +SC_VDEFINE cell code_idx = 0; /* number of bytes with generated code */ +SC_VDEFINE int ntv_funcid= 0; /* incremental number of native function */ +SC_VDEFINE int errnum = 0; /* number of errors */ +SC_VDEFINE int warnnum = 0; /* number of warnings */ +SC_VDEFINE int sc_debug = sCHKBOUNDS; /* by default: bounds checking+assertions */ +SC_VDEFINE int sc_packstr= FALSE; /* strings are packed by default? */ +SC_VDEFINE int sc_asmfile= FALSE; /* create .ASM file? */ +SC_VDEFINE int sc_listing= FALSE; /* create .LST file? */ +SC_VDEFINE int sc_compress=TRUE; /* compress bytecode? */ +SC_VDEFINE int sc_needsemicolon=TRUE;/* semicolon required to terminate expressions? */ +SC_VDEFINE int sc_dataalign=sizeof(cell);/* data alignment value */ +SC_VDEFINE int sc_alignnext=FALSE; /* must frame of the next function be aligned? */ +SC_VDEFINE int pc_docexpr=FALSE; /* must expression be attached to documentation comment? */ +SC_VDEFINE int curseg = 0; /* 1 if currently parsing CODE, 2 if parsing DATA */ +SC_VDEFINE cell sc_stksize=sDEF_AMXSTACK;/* default stack size */ +SC_VDEFINE cell sc_amxlimit=0; /* default abstract machine size limit = none */ +SC_VDEFINE int freading = FALSE; /* Is there an input file ready for reading? */ +SC_VDEFINE int fline = 0; /* the line number in the current file */ +SC_VDEFINE short fnumber = 0; /* the file number in the file table (debugging) */ +SC_VDEFINE short fcurrent= 0; /* current file being processed (debugging) */ +SC_VDEFINE short sc_intest=FALSE; /* true if inside a test */ +SC_VDEFINE int sideeffect= 0; /* true if an expression causes a side-effect */ +SC_VDEFINE int stmtindent= 0; /* current indent of the statement */ +SC_VDEFINE int indent_nowarn=FALSE;/* skip warning "217 loose indentation" */ +SC_VDEFINE int sc_tabsize=8; /* number of spaces that a TAB represents */ +SC_VDEFINE short sc_allowtags=TRUE; /* allow/detect tagnames in lex() */ +SC_VDEFINE int sc_status; /* read/write status */ +SC_VDEFINE int sc_rationaltag=0; /* tag for rational numbers */ +SC_VDEFINE int rational_digits=0; /* number of fractional digits */ +SC_VDEFINE int sc_allowproccall=0; /* allow/detect tagnames in lex() */ +SC_VDEFINE short sc_is_utf8=FALSE; /* is this source file in UTF-8 encoding */ + +SC_VDEFINE constvalue sc_automaton_tab = { NULL, "", 0, 0}; /* automaton table */ +SC_VDEFINE constvalue sc_state_tab = { NULL, "", 0, 0}; /* state table */ + +SC_VDEFINE FILE *inpf = NULL; /* file read from (source or include) */ +SC_VDEFINE FILE *inpf_org= NULL; /* main source file */ +SC_VDEFINE FILE *outf = NULL; /* (intermediate) text file written to */ + +SC_VDEFINE jmp_buf errbuf; + +#if !defined SC_LIGHT + SC_VDEFINE int sc_makereport=FALSE; /* generate a cross-reference report */ +#endif + +#if defined __WATCOMC__ && !defined NDEBUG + /* Watcom's CVPACK dislikes .OBJ files without functions */ + static int dummyfunc(void) + { + return 0; + } +#endif