Merged changes from sfall 4.2.1 modderspack:

* Added -m<macro[=val]> and -I<path> options
* Changed the search order for Include files for the parser DLL.
* Removed embedded mcpp from Win2k build.
This commit is contained in:
NovaRain
2019-11-14 12:14:46 +08:00
parent 9b63b1886a
commit d93e812f9f
7 changed files with 639 additions and 566 deletions
+51 -17
View File
@@ -7,10 +7,15 @@
#include "parse.h"
#include <io.h>
#ifdef BUILDING_DLL
int optimize = 0;
#else
int optimize = 1; // default for exe
#endif
int noinputwait = 0;
int warnings = 1;
int backwardcompat = 0;
int optimize = 0;
int debug = 0;
int preprocess_fullpath = 0;
int dumpTree = 0;
@@ -32,14 +37,19 @@ FILE *parseroutput;
static void PrintLogo() {
parseOutput("Startreck scripting language compiler (Fallout 2 sfall edition 4.2)\n\n"
#ifndef WIN2K
"Preprocessing handled by mcpp 2.7.2\n"
"Copyright (c) 1998, 2002-2008 Kiyoshi Matsui <kmatsui@t3.rim.or.jp>\n"
"All rights reserved.\n\n");
"All rights reserved.\n\n"
#else
"Win2K/Lite version (no built-in preprocessor)\n\n"
#endif
);
}
extern int warn_level; //the mcpp warning level
extern int mcpp_lib_main(FILE *fin, FILE *fout, const char* in_file, const char* dir);
//extern void set_a_dir(const char * dirname);
extern int mcpp_lib_main(FILE *fin, FILE *fout, const char* in_file, const char* dir, const char* def, const char* include_dir);
extern void mcpp_add_include_dir(char*);
#ifndef BUILDING_DLL
int main(int argc, char **argv)
@@ -52,6 +62,8 @@ int main(int argc, char **argv)
int preprocess=0;
int onlypreprocess=0;
char* includeDir = NULL, *defMacro = NULL;
if (argc < 2) {
PrintLogo();
parseOutput("Usage: compile {switches} filename [-o outputname] [filename [..]]\n");
@@ -59,13 +71,19 @@ int main(int argc, char **argv)
parseOutput(" -n no warnings\n");
parseOutput(" -b use backward compatibility mode\n");
parseOutput(" -l no logo\n");
#ifndef WIN2K
parseOutput(" -p preprocess\n");
parseOutput(" -P preprocess only. (Don't generate .int)\n");
parseOutput(" -F write full file paths in #line directives\n");
parseOutput(" -O<level> optimize (0 - none, 1 - only remove unreferenced globals, 2 - full, 3 - full+experimental, don't use!)\n");
#endif
parseOutput(" -O<level> optimize (0 - none, 1 - only remove unreferenced globals (default), 2 - full, 3 - full+experimental, don't use!)\n");
parseOutput(" -d show debug info\n");
parseOutput(" -s enable short-circuit evaluation for boolean operators (AND, OR)\n");
parseOutput(" -D dump abstract syntax tree after optimizations\n");
#ifndef WIN2K
parseOutput(" -m<macro[=val]> define a macro named \"macro\" for conditional compilation\n");
parseOutput(" -I<path> specify an additional directory to search for include files\n");
#endif
return 1;
}
@@ -91,9 +109,10 @@ int main(int argc, char **argv)
nologo=1;
break;
case 'O':
if (strlen(argv[1]) == 2) optimize = 2;
if (strlen(argv[1]) == 2) optimize = 2; // full
else optimize = atoi(&argv[1][2]);
break;
#ifndef WIN2K
case 'P':
onlypreprocess = 1;
case 'p':
@@ -102,12 +121,24 @@ int main(int argc, char **argv)
case 'F':
preprocess_fullpath = 1;
break;
#endif
case 'D':
dumpTree = 1;
break;
case 's':
shortCircuit = 1;
break;
#ifndef WIN2K
case 'm':
defMacro = &argv[1][2];
break;
case 'I':
if (!includeDir)
includeDir = &argv[1][2];
else
mcpp_add_include_dir(&argv[1][2]);
break;
#endif
default:
parseOutput("Unknown option %c\n", argv[1][1]);
}
@@ -115,7 +146,7 @@ int main(int argc, char **argv)
argv++;
argc--;
}
// disabled - Fakels
/*if(backwardcompat&&(optimize||preprocess)) {
parseOutput("Invalid option combination; cannot run preprocess or optimization passes in backward compatibility mode\n");
return -1;
@@ -124,7 +155,10 @@ int main(int argc, char **argv)
if(!nologo) PrintLogo();
compilerErrorTotal = 0;
#ifndef WIN2K
if (defMacro) parseOutput("Define macro: %s\n", defMacro);
if (includeDir) parseOutput("Set include directory: %s\n", includeDir);
#endif
while(argv[1]) {
file = argv[1];
argv++;
@@ -173,7 +207,7 @@ int main(int argc, char **argv)
}
}
}
#ifndef WIN2K
if(preprocess) {
FILE *newfile;
unsigned int letters;
@@ -190,7 +224,7 @@ int main(int argc, char **argv)
newfile=fopen(tmpbuf, "w+DT");
//#endif
}
if(mcpp_lib_main(foo.file, newfile, buf.name, buf.name)) {
if(mcpp_lib_main(foo.file, newfile, buf.name, buf.name, defMacro, includeDir)) {
parseOutput("*** An error occured during preprocessing of %s ***\n", buf.name);
return 1;
}
@@ -198,6 +232,7 @@ int main(int argc, char **argv)
rewind(newfile);
foo.file=newfile;
}
#endif
if(!onlypreprocess) {
parse(&foo, name);
freeCurrentProgram();
@@ -214,8 +249,7 @@ int main(int argc, char **argv)
getchar();
return 1;
}
}
else {
} else {
parseOutput("Warning: %s not found\n", file);
}
}
@@ -226,8 +260,8 @@ int main(int argc, char **argv)
#ifdef BUILDING_DLL
static int inited=0;
int _stdcall parse_main(const char *filePath, const char* origPath, const char* dir, int backMode) {
// old parser
int _stdcall parse_main(const char *filePath, const char* origPath, const char* dir/*, const char* def, const char* include_dir, int backMode*/) {
InputStream foo;
char tmpbuf[260];
//char cwd[1024];
@@ -239,12 +273,12 @@ int _stdcall parse_main(const char *filePath, const char* origPath, const char*
FreeFileNames();
inited=0;
}
/*
if (backMode) {
backwardcompat = 1;
lexClear();
}
*/
foo.name=AddFileName(origPath);
foo.file = fopen(filePath, "r");
@@ -258,7 +292,7 @@ int _stdcall parse_main(const char *filePath, const char* origPath, const char*
compilerErrorTotal = 0;
compilerSyntaxError = 0;
preprocess_fullpath = 1;
if (mcpp_lib_main(foo.file, newfile, origPath, dir)) {
if (mcpp_lib_main(foo.file, newfile, origPath, dir, NULL, NULL)) { // prevent crash in old Script Editor - NR
fclose(foo.file);
fclose(newfile);
if (parseroutput)
+1 -1
View File
@@ -454,7 +454,7 @@ extern int mcpp_fputs(const char *s, OUTDEST od);
extern int mcpp_fprintf( OUTDEST od, const char *format, ...);
/* system.c */
extern void do_options();
extern void do_options(const char* dir, const char* def, const char* include_dir, const char* second_dir);
/* Process command line args */
extern void init_sys_macro( void);
/* Define system-specific macro */
+10 -2
View File
@@ -250,6 +250,12 @@ static void devide_line( char * out);
static void put_a_line( char * out);
/* Put out the processed line */
static char* second_include_dir = NULL;
void mcpp_add_include_dir(char* dir) /* added - Fakels */
{
second_include_dir = dir;
}
static void init_main( void)
/* Initialize global variables on re-entering. */
{
@@ -274,7 +280,9 @@ int mcpp_lib_main
FILE *fin,
FILE *fout,
const char* in_file,
const char* dir
const char* dir,
const char* def,
const char* include_dir
)
{
const char * stdin_name = "<stdin>";
@@ -303,7 +311,7 @@ int mcpp_lib_main
inc_dirp = &null; /* Initialize to current (null) directory */
cur_fname = cur_fullname = "(predefined)"; /* For predefined macros */
init_defines(); /* Predefine macros */
do_options(dir); /* Command line options */
do_options(dir, def, include_dir, second_include_dir); /* Command line options */
init_sys_macro(); /* Initialize system-specific macros */
add_file( fp_in, NULL, in_file, in_file, FALSE);
+27 -5
View File
@@ -221,7 +221,10 @@ void init_system( void)
#define OPTLISTLEN 80
void do_options(
const char* dir
const char* dir,
const char* def,
const char* include_dir,
const char* second_dir
)
/*
* Process command line arguments, called only at MCPP startup.
@@ -245,8 +248,11 @@ void do_options(
sprintf( cur_work_dir + strlen( cur_work_dir), "%c%c", PATH_DELIM, EOS);
/* Append trailing path-delimiter */
if (dir)
set_a_dir(dir);
if (dir) set_a_dir(dir);
if (include_dir) set_a_dir(include_dir);
if (second_dir) set_a_dir(second_dir);
/* define macros */
if (def) def_list[def_cnt++] = (char*)def;
/* Check consistency of specified options, set some variables */
chk_opts( sflag, trad);
@@ -898,7 +904,13 @@ static int open_include(
has_dir = has_dir_src || has_dir_fname
|| (**(infile->dirp) != EOS);
}
#ifdef BUILDING_DLL /* Reordered search include files for DLL (Fakels) */
if (full_path) {
if (open_file( &null, NULL, filename, FALSE, FALSE, FALSE))
return TRUE;
return FALSE;
}
#else
if ((searchlocal && ((search_rule & CURRENT) || !has_dir)) || full_path) {
/*
* Look in local directory first.
@@ -910,7 +922,7 @@ static int open_include(
if (full_path)
return FALSE;
}
#endif
if (searchlocal && (search_rule & SOURCE) && has_dir) {
/*
* Look in local directory of source file.
@@ -924,6 +936,16 @@ static int open_include(
if (search_dir( filename, searchlocal, next))
return TRUE;
#ifdef BUILDING_DLL /* Reordered search include files for DLL (Fakels) */
if ((searchlocal && ((search_rule & CURRENT) || !has_dir))) {
/*
* Look in local directory.
* Try to open filename relative to the "current directory".
*/
if (open_file( &null, NULL, filename, searchlocal, FALSE, FALSE))
return TRUE;
}
#endif
return FALSE;
}
View File
+26 -13
View File
@@ -192,11 +192,11 @@ static void PerformConstOp(const Value* in1, const Value* in2, Value* out, int o
F_IOP(T_GREATER_EQUAL, >=)
case T_AND:
out->type = V_INT;
out->intData=fd1!=0.0 && fd2!=0.0;
out->intData = (fd1 != 0.0) && (fd2 != 0.0);
break;
case T_OR:
out->type = V_INT;
out->intData=fd1!=0.0 || fd2!=0.0;
out->intData = (fd1 != 0.0) || (fd2 != 0.0);
break;
case T_BWAND:
out->type = V_INT;
@@ -247,8 +247,8 @@ static int ConstantFolding(NodeList* _nodes) {
matched = 1;
PerformConstOp(&nodes[i - 2].value, &nodes[i - 1].value, &nodes[i].value, token, &nodes[i]);
nodes[i].token = T_CONSTANT;
RemoveNodes(_nodes, i-2, 2);
i -= 2;
RemoveNodes(_nodes, i, 2);
}/* AND/OR were changed in the tree
else if (token == T_AND || token == T_OR) {
if ((nodes[i - 1].token == T_CONSTANT && nodes[i - 1].value.type != V_STRING) || (nodes[i - 2].token == T_CONSTANT && nodes[i - 2].value.type != V_STRING)) {
@@ -326,10 +326,17 @@ static int* FindAssignmentsInBlock(const Node* nodes, const Variable* vars, int
assert(nodes->token == T_START_STATEMENT);
while (statementdepth) {
switch (nodes->token) {
case T_START_STATEMENT: statementdepth++; break;
case T_END_STATEMENT: statementdepth--; break;
case T_ASSIGN: case T_ASSIGN_ADD: case T_ASSIGN_SUB:
case T_ASSIGN_MUL: case T_ASSIGN_DIV:
case T_START_STATEMENT:
statementdepth++;
break;
case T_END_STATEMENT:
statementdepth--;
break;
case T_ASSIGN:
case T_ASSIGN_ADD:
case T_ASSIGN_SUB:
case T_ASSIGN_MUL:
case T_ASSIGN_DIV:
if (nodes[-1].token == T_SYMBOL && (var = LookupVariable(&nodes[-1])) != -1) {
results[var] = 1;
}
@@ -341,8 +348,13 @@ static int* FindAssignmentsInBlock(const Node* nodes, const Variable* vars, int
}
static int ConstantPropagateBlock(Node* nodes, int *_i, Variable* vars, Value* values, int varCount) {
int i=_i?*_i:0, token, var, matched=0, blockdepth=1, blockbegin, blockend, j;
int i = _i ? *_i : 0,
matched = 0,
blockdepth = 1,
token, var, blockbegin, blockend, j;
assert(nodes->token == T_BEGIN || nodes->token == T_START_STATEMENT);
if (nodes[i].token == T_BEGIN) {
blockbegin = T_BEGIN;
blockend = T_END;
@@ -350,6 +362,7 @@ static int ConstantPropagateBlock(Node* nodes, int *_i, Variable* vars, Value* v
blockbegin = T_START_STATEMENT;
blockend = T_END_STATEMENT;
}
i++;
while (1) {
token = nodes[i].token;
@@ -359,7 +372,8 @@ static int ConstantPropagateBlock(Node* nodes, int *_i, Variable* vars, Value* v
} else if (token == T_ASSIGN || token == T_ASSIGN_ADD ||token == T_ASSIGN_SUB || token == T_ASSIGN_MUL || token == T_ASSIGN_DIV) {
if ((var = LookupVariable(&nodes[i - 1])) != -1) {
if (nodes[i + 1].token == T_START_EXPRESSION && nodes[i + 2].token == T_CONSTANT && nodes[i + 3].token == T_END_EXPRESSION) {
if(token==T_ASSIGN) values[var]=nodes[i+2].value;
if (token == T_ASSIGN)
values[var] = nodes[i + 2].value;
else if (values[var].type != -1) {
switch (token) {
case T_ASSIGN_ADD: token = '+'; break;
@@ -764,8 +778,8 @@ int IsProtectedProc(const char* c) {
Protect("combat_is_starting_p_proc");
Protect("combat_is_over_p_proc");
Protect("node998");
Protect("node999");
//Protect("node998");
//Protect("node999");
return 0;
}
static int __once = 0;
@@ -859,7 +873,7 @@ static void UpdateProcedureReferences(Procedure* procs, int count) {
int i, j, matched = 1;
Node* node;
for (i = 1; i < count; i++) {
if(IsProtectedProc(currprogram->namelist + procs[i].name) || procs[i].type&(P_TIMED|P_CONDITIONAL|P_EXPORT|P_CRITICAL)) procs[i].uses=1;
if (IsProtectedProc(currprogram->namelist + procs[i].name) || procs[i].type & (P_TIMED | P_CONDITIONAL | P_EXPORT/* | P_CRITICAL*/)) procs[i].uses = 1;
//else if (procs[i].type & P_IMPORT) procs[i].uses = 2;
else procs[i].uses = 0;
}
@@ -1125,4 +1139,3 @@ void optimizeTree(Program *prog) {
}
CompressNamelist(prog);
}
+2 -6
View File
@@ -124,7 +124,7 @@
<ClCompile>
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<MinimalRebuild>true</MinimalRebuild>
<MinimalRebuild>false</MinimalRebuild>
<BasicRuntimeChecks>EnableFastChecks</BasicRuntimeChecks>
<RuntimeLibrary>MultiThreadedDebugDLL</RuntimeLibrary>
<PrecompiledHeader>
@@ -145,10 +145,6 @@
<ModuleDefinitionFile>
</ModuleDefinitionFile>
</Link>
<PostBuildEvent>
<Command>
</Command>
</PostBuildEvent>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
@@ -191,7 +187,7 @@
<ClCompile>
<Optimization>Full</Optimization>
<WholeProgramOptimization>true</WholeProgramOptimization>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;_CRT_SECURE_NO_WARNINGS;WIN2K;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
<PrecompiledHeader>
</PrecompiledHeader>