/* SGI big endian MIPSes running IRIX 5.x and 6.x.

   The default memory model is -o32, because further work is need in the
   lcc code generator to produce -n32 code, and no work at all has yet
   been done to support the -64 model.  However, code is in place here
   to support all three memory models:

	Define the symbol MM_O32 to select -o32 as the default.

	Define the symbol MM_N32 to select -n32 as the default.

	Define the symbol MM_64 to enable recognition of the -64
	option; otherwise -64 is diagnosed as an error.

   If none of these symbols is defined, then MM_O32 is assumed.

   WARNING: If new elements are added to any of the as[] or ld[]
   initializers, corresponding elements must be added in the MM_N32,
   MM_64, and MM_O32 branches, because option() updates elements in
   those arrays at run time.
 */

#include <lcc-conf.h>

#include <string.h>

#if !(defined(MM_O32) || defined(MM_N32) || defined(MM_64))
#define MM_O32				/* default memory model is -o32 */
#endif

static char rcsid[] = "$Id: irix.c,v 1.1.1.1 2002/04/21 13:25:10 peter Exp $";

#ifndef LOCALDIR
#define LOCALDIR "/usr/local/"
#endif

#ifndef LCCDIR
#define LCCDIR LOCALDIR "lib/lcc/"
#endif

#ifndef LIBDIR
#define LIBDIR "/usr/lib/"
#endif

extern char *concat(char *, char *);

#if defined(OS_MAJOR_LEVEL) && ((OS_MAJOR_LEVEL + 0) == 5) /* IRIX 5.x: (-32 -mips1) */
char *suffixes[] = { ".c", ".i", ".s", ".o", ".out", 0 };
char inputs[256] = "";
char *cpp[] = { "/usr/lib/cpp", "-D__STDC__=1",
	"-DLANGUAGE_C",
	"-DMIPSEB",
	"-DSYSTYPE_SVR4",
	"-D_CFE",
	"-D_LANGUAGE_C",
	"-D_MIPSEB",
	"-D_MIPS_FPSET=16",
	"-D_MIPS_ISA=_MIPS_ISA_MIPS1",
	"-D_MIPS_SIM=_MIPS_SIM_ABI32",
	"-D_MIPS_SZINT=32",
	"-D_MIPS_SZLONG=32",
	"-D_MIPS_SZPTR=32",
	"-D_SGI_SOURCE",
	"-D_SVR4_SOURCE",
	"-D_SYSTYPE_SVR4",
	"-D__host_mips",
	"-D__mips=1",
	"-D__sgi",
	"-D__unix",
	"-Dhost_mips",
	"-Dmips",
	"-Dsgi",
	"-Dunix",
	"$1", "$2", "$3", 0 };
char *com[] =  { LCCDIR "rcc", "-target=mips/irix", "$1", "$2", "$3", "", 0 };
char *include[] = { "-I" LCCDIR "include", "-I/usr/local/include",
	"-I/usr/include", 0 };
char *as[] = { "/usr/bin/as", "-32", "-o", "$3", "$1", "-nocpp", "-KPIC", "$2", 0 };
char *ld[] = {
	/* 0 */	"/usr/bin/ld",
		"-require_dynamic_link",
		"_rld_new_interface",
		"-32",
	/* 4 */	"-elf",
		"-_SYSTYPE_SVR4",
		"-Wx,-G",
		"0",
	/* 8 */	"-g0",
		"-KPIC",
		"-dont_warn_unused",
		"-o",
	/* 12*/	"$3",
		LIBDIR "crt1.o",
		"-L" LOCALDIR "lib",
		"$1",
	/* 16*/	"$2",
		"",
		"-L" LCCDIR,
		"-llcc",
	/* 20*/	"-lc",
		"-lm",
		LIBDIR "crtn.o",
		0
};
int option(char *arg) {
	if (strncmp(arg, "-lccdir=", 8) == 0) {
		cpp[0] = concat(&arg[8], "/cpp");
		include[0] = concat("-I", concat(&arg[8], "/include"));
		com[0] = concat(&arg[8], "/rcc");
		ld[18] = concat("-L", &arg[8]);
	} else if (strncmp(arg, "-as=", 4) == 0) {
		as[0] = concat(&arg[4], "");
	} else if (strncmp(arg, "-cpp=", 5) == 0) {
		cpp[0] = concat(&arg[5], "");
	} else if (strncmp(arg, "-ld=", 4) == 0) {
		ld[0] = concat(&arg[4], "");
	} else if (strncmp(arg, "-rcc=", 5) == 0) {
		com[0] = concat(&arg[5],"");
	} else if (strcmp(arg, "-g") == 0)
		;
	else if (strcmp(arg, "-p") == 0)
		ld[13] = LIBDIR "mcrt1.o";
	else if (strcmp(arg, "-b") == 0)
		;
	else if (strcmp(arg, "-dynamic") == 0)
		;			/* default is dynamic */
	else if (strcmp(arg, "-static") == 0)
		;
	else
		return 0;
	return 1;
}
#else  /* assume IRIX6 */

char *suffixes[] = { ".c", ".i", ".s", ".o", ".out", 0 };

char inputs[256] = "";

char *com[] =  { LCCDIR "rcc", "-target=mips/irix", "$1", "$2", "$3", "", 0 };

char *include[] = {
	"-I" LCCDIR "include",
	"-I/usr/local/include",
	"-I/usr/include",
	0 };

#if defined(MM_N32)
char *as[] = { "/usr/bin/as",
	"-n32",
	"-o",
	"$3",
	"$1",
	"-nocpp",
	"-KPIC",
	"$2",
	0 };

char *cpp[] = { "/usr/lib/cpp",		/* derived from "c89 -n32 -E foo.c" on SGI IRIX 6.5 */
	"-D__STDC__=1",
	"-DLANGUAGE_C",
	"-DMIPSEB",
	"-D_ABIN32=2",
	"-D_LANGUAGE_C",
	"-D_LONGLONG",
	"-D_MIPSEB",
	"-D_MIPS_FPSET=32",
	"-D_MIPS_ISA=3",
	"-D_MIPS_SIM=_ABIN32",
	"-D_MIPS_SZINT=32",
	"-D_MIPS_SZLONG=32",
	"-D_MIPS_SZPTR=32",
	"-D_MODERN_C",
	"-D_PIC",
	"-D_SGI_SOURCE",
	"-D_SIZE_INT=32",
	"-D_SIZE_LONG=32",
	"-D_SIZE_PTR=32",
	"-D_SVR4_SOURCE",
	"-D_SYSTYPE_SVR4",
	"-D__DSO__",
	"-D__EXTENSIONS__",
	"-D__INLINE_INTRINSICS",
	"-D__MATH_HAS_NO_SIDE_EFFECTS",
	"-D__host_mips",
	"-D__mips=3",
	"-D__sgi",
	"-D__unix",
	"-Dhost_mips",
	"-Dmips",
	"-Dsgi",
	"-Dunix",
	/* "-Ac=xansi", */
	/* "-Amachine(mips)", */
	/* "-Asystem(unix)", */
	/* "-D_COMPILER_VERSION=730", */
	"$1", "$2", "$3", 0 };

char *ld[] = {
	/* 0 */	"/usr/lib32/cmplrs/ld32",
		"-call_shared",
		"-no_unresolved",
		"-transitive_link",
	/* 4 */	"-elf",
		"-_SYSTYPE_SVR4",
		"-show",
		"-mips3",
	/* 8 */	"",				/* -g: -g2 */
		"-n32",
		"",				/* user-supplied -Lxxx */
		"-L" LOCALDIR,
	/* 12*/	"-L/usr/lib32/mips3/r4000",
		"-L/usr/lib32/mips3",
		"-L/usr/lib32",
		"/usr/lib32/mips3/crt1.o",
	/* 16*/	"-o",
		"$3",
		"$1",
		"$2",
	/* 20*/	"-dont_warn_unused",
		"-Bdynamic",
		"",				/* user-supplied -lxxx */
		"-L" LCCDIR,
	/* 24*/	"-llcc",
		"-lc",
		"-lm",
		"/usr/lib32/mips3/crtn.o",
	/* 28*/	"-warn_unused",
		0
};
#elif defined(MM_64)
char *as[] = { "/usr/bin/as",
	"-64",
	"-o",
	"$3",
	"$1",
	"-nocpp",
	"-KPIC",
	"$2",
	0 };

char *cpp[] = { "/usr/lib/cpp",		/* derived from "c89 -64 -E foo.c" on SGI IRIX 6.5 */
	"-D__STDC__=1",
	"-DLANGUAGE_C",
	"-DMIPSEB",
	"-D_ABI64=3",
	"-D_LANGUAGE_C",
	"-D_LONGLONG",
	"-D_MIPSEB",
	"-D_MIPS_FPSET=32",
	"-D_MIPS_ISA=3",
	"-D_MIPS_SIM=_ABI64",
	"-D_MIPS_SZINT=32",
	"-D_MIPS_SZLONG=64",
	"-D_MIPS_SZPTR=64",
	"-D_MODERN_C",
	"-D_PIC",
	"-D_SGI_SOURCE",
	"-D_SIZE_INT=32",
	"-D_SIZE_LONG=64",
	"-D_SIZE_PTR=64",
	"-D_SVR4_SOURCE",
	"-D_SYSTYPE_SVR4",
	"-D__DSO__",
	"-D__EXTENSIONS__",
	"-D__INLINE_INTRINSICS",
	"-D__MATH_HAS_NO_SIDE_EFFECTS",
	"-D__host_mips",
	"-D__mips=3",
	"-D__sgi",
	"-D__unix",
	"-Dhost_mips",
	"-Dmips",
	"-Dsgi",
	"-Dunix",
	/* "-Ac=xansi", */
	/* "-Amachine(mips)", */
	/* "-Asystem(unix)", */
	/* "-D_COMPILER_VERSION=730", */
	"$1", "$2", "$3", 0 };

char *ld[] = {
	/* 0 */	"/usr/lib32/cmplrs/ld64",
		"-call_shared",
		"-no_unresolved",
		"-transitive_link",
	/* 4 */	"-elf",
		"-_SYSTYPE_SVR4",
		"-show",
		"-mips3",
	/* 8 */	"",				/* -g: -g2 */
		"-64",
		"",				/* user-supplied -Lxxx */
		"-L" LOCALDIR,
	/* 12*/	"-L/usr/lib64/mips3/r4000",
		"-L/usr/lib64/mips3",
		"-L/usr/lib64",
		"/usr/lib64/mips3/crt1.o",
	/* 16*/	"-o",
		"$3",
		"$1",
		"$2",
	/* 20*/	"-dont_warn_unused",
		"-Bdynamic",
		"",				/* user-supplied -lxxx */
		"-L" LCCDIR,
	/* 24*/	"-llcc",
		"-lc",
		"-lm",
		"/usr/lib64/mips3/crtn.o",
	/* 28*/	"-warn_unused",
		0
};
#elif defined(MM_O32)  /* must be MM_O32: -o32 memory model */
char *as[] = { "/usr/bin/as",
	"-o32",
	"-o",
	"$3",
	"$1",
	"-nocpp",
	"-KPIC",
	"$2",
	0 };

char *cpp[] = { "/usr/lib/cpp",		/* derived from "c89 -o32 -E foo.c" on SGI IRIX 6.5 */
	"-D__STDC__=1",
	"-DLANGUAGE_C",
	"-DMIPSEB",
	"-DSYSTYPE_SVR4",
	"-D_ABIO32=1",
	"-D_CFE",
	"-D_LANGUAGE_C",
	"-D_LONGLONG",
	"-D_MIPSEB",
	"-D_MIPS_FPSET=16",
	"-D_MIPS_ISA=2",
	"-D_MIPS_SIM=_ABIO32",
	"-D_MIPS_SZINT=32",
	"-D_MIPS_SZLONG=32",
	"-D_MIPS_SZPTR=32",
	"-D_MODERN_C",
	"-D_PIC",
	"-D_SGI_SOURCE",
	"-D_SIZE_INT=32",
	"-D_SIZE_LONG=32",
	"-D_SIZE_PTR=32",
	"-D_SVR4_SOURCE",
	"-D_SYSTYPE_SVR4",
	"-D__DSO__",
	"-D__EXTENSIONS__",
	"-D__INLINE_INTRINSICS",
	"-D__host_mips",
	"-D__mips=2",
	"-D__sgi",
	"-D__unix",
	"-Dhost_mips",
	"-Dmips",
	"-Dsgi",
	"-Dunix",
	"$1", "$2", "$3", 0 };

char *ld[] = {
	/* 0 */	"/usr/lib/ld",
		"-call_shared",
		"-no_unresolved",
		"-transitive_link",
	/* 4 */	"-elf",
		"-_SYSTYPE_SVR4",
		"-show",
		"-mips3",
	/* 8 */	"",				/* -g: -g2 */
		"-o32",
		"",				/* user-supplied -Lxxx */
		"-L" LOCALDIR,
	/* 12*/	"",
		"",
		"-L/usr/lib",
		"/usr/lib/crt1.o",
	/* 16*/	"-o",
		"$3",
		"$1",
		"$2",
	/* 20*/	"-dont_warn_unused",
		"-Bdynamic",
		"",				/* user-supplied -lxxx */
		"-L" LCCDIR,
	/* 24*/	"-llcc",
		"-lc",
		"-lm",
		"/usr/lib/crtn.o",
	/* 28*/	"-warn_unused",
		0
};
#else
#error One of memory models MM_N32, MM_O32, or MM_64 must be defined at compile time.
#endif
int option(char *arg) {
	if (strncmp(arg, "-lccdir=", 8) == 0) { /* override LCCDIR */
		cpp[0] = concat(&arg[8], "/cpp");
		include[0] = concat("-I", concat(&arg[8], "/include"));
		com[0] = concat(&arg[8], "/rcc");
		ld[22] = concat("-L", &arg[8]);
	} else if (strncmp(arg, "-as=", 4) == 0) {
		as[0] = concat(&arg[4], "");
	} else if (strncmp(arg, "-cpp=", 5) == 0) {
		cpp[0] = concat(&arg[5], "");
	} else if (strncmp(arg, "-ld=", 4) == 0) {
		ld[0] = concat(&arg[4], "");
	} else if (strncmp(arg, "-rcc=", 5) == 0) {
		com[0] = concat(&arg[5],"");
	} else if (strcmp(arg, "-g") == 0) {
		ld[8] = "-g2";
	} else if (strcmp(arg, "-p") == 0) /* -p replaced by speedshop(1) in IRIX 6 */
		;
	else if (strcmp(arg, "-pg") == 0) /* -pg replaced by speedshop(1) in IRIX 6 */
		;
	else if (strcmp(arg, "-b") == 0)
		;
	else if (strcmp(arg, "-dynamic") == 0)
		;			/* default is dynamic */
	else if (strcmp(arg, "-32") == 0) {
		ld[0] = "/usr/lib/ld";
		as[1] = ld[9] = "-32";
		ld[12] = "";		/* empty */
		ld[13] = "";		/* empty */
		ld[14] = "-L/usr/lib/";
		ld[15] = "/usr/lib/crt1.o";
		ld[27] = "/usr/lib/crtn.o";
	}
	else if (strcmp(arg, "-o32") == 0) {
		ld[0] = "/usr/lib/ld";
		as[1] = ld[9] = "-o32";
		ld[12] = "";		/* empty */
		ld[13] = "";		/* empty */
		ld[14] = "-L/usr/lib/";
		ld[15] = "/usr/lib/crt1.o";
		ld[27] = "/usr/lib/crtn.o";
	}
	else if (strcmp(arg, "-n32") == 0) {
		ld[0] = "/usr/lib32/cmplrs/ld32";
		as[1] = ld[9] = "-n32";
		ld[12] = "-L/usr/lib32/mips3/r4000";
		ld[13] = "-L/usr/lib32/mips3";
		ld[14] = "-L/usr/lib32";
		ld[15] = "/usr/lib32/mips3/crt1.o";
		ld[27] = "/usr/lib32/mips3/crtn.o";
	}
	else if (strcmp(arg, "-64") == 0) {
#if defined(MM_64)
	    /* These settings are correct for the 64-bit world, but rcc also
	       needs to generate 64-bit instructions (it does not, yet), and we
	       need a 64-bit version of liblcc.a (which requires changes to
	       ../../Makefile.in to compile 64-bit object files to make liblcc64.a). */
		ld[0] = "/usr/lib32/cmplrs/ld64";
		as[1] = ld[9] = "-64";
		ld[12] = "-L/usr/lib64/mips3/r4000";
		ld[13] = "-L/usr/lib64/mips3";
		ld[14] = "-L/usr/lib64";
		ld[15] = "/usr/lib64/mips3/crt1.o";
		ld[27] = "/usr/lib64/mips3/crtn.o";
#else
		/* Until 64-bit support is ready, diagnose -64 as an error */
		return 0;
#endif
	}
	else if (strcmp(arg, "-static") == 0)
	{
	    /* The IRIX 6.5 compilers accept a -non_shared (== -static) option,
		but call /usr/lib32/cmplrs/ld32 with references to
		nonexistant files:

			/usr/lib32/mips3/r4000/nonshared
			/usr/lib32/mips3/nonshared
			/usr/lib32/nonshared
			/usr/lib32/nonshared/crt1.o
			/usr/lib32/nonshared/crtn.o

		We therefore conclude that static linking is in fact
		not supported under this O/S release. */
		;
	}
	else
		return 0;
	return 1;
}
#endif
