1987-03-10 17:51:10 +00:00
|
|
|
/*
|
|
|
|
* (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
|
|
|
|
* See the copyright notice in the ACK home directory, in the file "Copyright".
|
|
|
|
*/
|
1986-03-10 13:07:55 +00:00
|
|
|
/* $Header$ */
|
|
|
|
/* MAIN PROGRAM */
|
|
|
|
|
1986-09-24 13:53:16 +00:00
|
|
|
#include "nofloat.h"
|
1986-03-25 10:39:23 +00:00
|
|
|
#include <system.h>
|
1986-03-10 13:07:55 +00:00
|
|
|
#include "nopp.h"
|
|
|
|
#include "target_sizes.h"
|
|
|
|
#include "debug.h"
|
|
|
|
#include "use_tmp.h"
|
|
|
|
#include "inputtype.h"
|
|
|
|
#include "input.h"
|
|
|
|
#include "level.h"
|
|
|
|
#include "idf.h"
|
|
|
|
#include "arith.h"
|
|
|
|
#include "type.h"
|
1986-03-10 15:10:56 +00:00
|
|
|
#include "declar.h"
|
1986-03-10 13:07:55 +00:00
|
|
|
#include "tokenname.h"
|
|
|
|
#include "Lpars.h"
|
|
|
|
#include "LLlex.h"
|
1987-01-24 00:25:56 +00:00
|
|
|
#include <alloc.h>
|
1986-03-10 13:07:55 +00:00
|
|
|
#include "specials.h"
|
1987-03-25 23:14:43 +00:00
|
|
|
#include "noRoption.h"
|
|
|
|
#include "nocross.h"
|
|
|
|
#include "sizes.h"
|
|
|
|
#include "align.h"
|
1986-03-10 13:07:55 +00:00
|
|
|
|
|
|
|
extern struct tokenname tkidf[], tkother[];
|
|
|
|
extern char *symbol2str();
|
|
|
|
char options[128]; /* one for every char */
|
|
|
|
|
|
|
|
#ifndef NOPP
|
|
|
|
int inc_pos = 1; /* place where next -I goes */
|
1987-05-11 14:08:58 +00:00
|
|
|
int inc_total = 0;
|
|
|
|
int inc_max;
|
|
|
|
char **inctable;
|
1986-03-10 13:07:55 +00:00
|
|
|
|
1986-12-04 16:29:44 +00:00
|
|
|
extern char *getwdir();
|
1986-03-10 13:07:55 +00:00
|
|
|
#endif NOPP
|
|
|
|
|
|
|
|
struct sp_id special_ids[] = {
|
|
|
|
{"setjmp", SP_SETJMP}, /* non-local goto's are registered */
|
|
|
|
{0, 0}
|
|
|
|
};
|
|
|
|
|
1987-03-25 23:14:43 +00:00
|
|
|
#ifndef NOCROSS
|
1986-03-10 13:07:55 +00:00
|
|
|
arith
|
|
|
|
short_size = SZ_SHORT,
|
|
|
|
word_size = SZ_WORD,
|
|
|
|
dword_size = (2 * SZ_WORD),
|
|
|
|
int_size = SZ_INT,
|
|
|
|
long_size = SZ_LONG,
|
1986-09-12 09:16:07 +00:00
|
|
|
#ifndef NOFLOAT
|
1986-03-10 13:07:55 +00:00
|
|
|
float_size = SZ_FLOAT,
|
|
|
|
double_size = SZ_DOUBLE,
|
1986-09-12 09:16:07 +00:00
|
|
|
#endif NOFLOAT
|
1986-03-10 13:07:55 +00:00
|
|
|
pointer_size = SZ_POINTER;
|
|
|
|
|
|
|
|
int
|
|
|
|
short_align = AL_SHORT,
|
|
|
|
word_align = AL_WORD,
|
|
|
|
int_align = AL_INT,
|
|
|
|
long_align = AL_LONG,
|
1986-09-12 09:16:07 +00:00
|
|
|
#ifndef NOFLOAT
|
1986-03-10 13:07:55 +00:00
|
|
|
float_align = AL_FLOAT,
|
|
|
|
double_align = AL_DOUBLE,
|
1986-09-12 09:16:07 +00:00
|
|
|
#endif NOFLOAT
|
1986-03-10 13:07:55 +00:00
|
|
|
pointer_align = AL_POINTER,
|
|
|
|
struct_align = AL_STRUCT,
|
|
|
|
union_align = AL_UNION;
|
1987-03-25 23:14:43 +00:00
|
|
|
#endif NOCROSS
|
1986-03-10 13:07:55 +00:00
|
|
|
|
|
|
|
#ifndef NOPP
|
|
|
|
arith ifval; /* ifval will contain the result of the #if expression */
|
|
|
|
#endif NOPP
|
|
|
|
|
|
|
|
char *prog_name;
|
|
|
|
|
|
|
|
main(argc, argv)
|
|
|
|
char *argv[];
|
|
|
|
{
|
|
|
|
/* parse and interpret the command line options */
|
|
|
|
prog_name = argv[0];
|
|
|
|
|
|
|
|
init_hmask();
|
|
|
|
#ifndef NOPP
|
1987-05-11 14:08:58 +00:00
|
|
|
inctable = (char **) Malloc(10 * sizeof(char *));
|
|
|
|
inctable[0] = ".";
|
|
|
|
inctable[1] = "/usr/include";
|
|
|
|
inctable[2] = 0;
|
|
|
|
inc_total = 2;
|
|
|
|
inc_max = 10;
|
|
|
|
|
1986-03-10 13:07:55 +00:00
|
|
|
init_pp(); /* initialise the preprocessor macros */
|
|
|
|
#endif NOPP
|
|
|
|
|
|
|
|
/* Note: source file "-" indicates that the source is supplied
|
1986-12-04 16:29:44 +00:00
|
|
|
as standard input. This is only allowed if INP_READ_IN_ONE is
|
1986-03-10 13:07:55 +00:00
|
|
|
not defined!
|
|
|
|
*/
|
1986-12-04 16:29:44 +00:00
|
|
|
#ifdef INP_READ_IN_ONE
|
1986-04-28 09:56:33 +00:00
|
|
|
while (argc > 1 && *argv[1] == '-')
|
1986-12-04 16:29:44 +00:00
|
|
|
#else INP_READ_IN_ONE
|
1986-04-28 09:56:33 +00:00
|
|
|
while (argc > 1 && *argv[1] == '-' && argv[1][1] != '\0')
|
1986-12-04 16:29:44 +00:00
|
|
|
#endif INP_READ_IN_ONE
|
1986-04-28 09:56:33 +00:00
|
|
|
{
|
1986-03-10 13:07:55 +00:00
|
|
|
char *par = &argv[1][1];
|
|
|
|
|
|
|
|
do_option(par);
|
|
|
|
argc--, argv++;
|
|
|
|
}
|
|
|
|
compile(argc - 1, &argv[1]);
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
hash_stat();
|
|
|
|
#endif DEBUG
|
|
|
|
|
1987-03-05 15:22:05 +00:00
|
|
|
exit(err_occurred);
|
1986-03-10 13:07:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
char *source = 0;
|
|
|
|
|
|
|
|
char *nmlist = 0;
|
|
|
|
|
|
|
|
compile(argc, argv)
|
|
|
|
char *argv[];
|
|
|
|
{
|
1986-12-04 16:29:44 +00:00
|
|
|
char *result;
|
1987-02-09 23:19:42 +00:00
|
|
|
register char *destination = 0;
|
1986-07-29 11:46:00 +00:00
|
|
|
|
1987-02-09 23:19:42 +00:00
|
|
|
#ifdef DEBUG
|
1986-03-10 13:07:55 +00:00
|
|
|
#ifndef NOPP
|
1987-02-09 23:19:42 +00:00
|
|
|
int pp_only = options['E'] || options['P'] || options['C'];
|
1986-03-10 13:07:55 +00:00
|
|
|
#endif NOPP
|
1987-02-09 23:19:42 +00:00
|
|
|
#endif
|
1986-03-10 13:07:55 +00:00
|
|
|
|
|
|
|
switch (argc) {
|
|
|
|
case 1:
|
1987-02-09 23:19:42 +00:00
|
|
|
#ifdef DEBUG
|
1986-03-10 13:07:55 +00:00
|
|
|
#ifndef NOPP
|
|
|
|
if (!pp_only)
|
|
|
|
#endif NOPP
|
1987-02-09 23:19:42 +00:00
|
|
|
#endif
|
1986-03-10 13:07:55 +00:00
|
|
|
fatal("%s: destination file not specified", prog_name);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
destination = argv[1];
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
nmlist = argv[2];
|
|
|
|
destination = argv[1];
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fatal("use: %s source destination [namelist]", prog_name);
|
|
|
|
break;
|
|
|
|
}
|
1987-03-20 08:30:31 +00:00
|
|
|
if (strcmp(argv[0], "-"))
|
|
|
|
FileName = source = argv[0];
|
|
|
|
else {
|
|
|
|
source = 0;
|
|
|
|
FileName = "standard input";
|
|
|
|
}
|
1986-03-10 13:07:55 +00:00
|
|
|
|
1986-12-01 10:00:23 +00:00
|
|
|
if (destination && strcmp(destination, "-") == 0)
|
1986-03-25 10:39:23 +00:00
|
|
|
destination = 0;
|
1986-12-04 16:29:44 +00:00
|
|
|
if (!InsertFile(source, (char **) 0, &result)) /* read the source file */
|
1987-03-20 08:30:31 +00:00
|
|
|
fatal("%s: no source file %s\n", prog_name, FileName);
|
1987-03-10 17:51:10 +00:00
|
|
|
File_Inserted = 1;
|
1986-03-10 13:07:55 +00:00
|
|
|
init();
|
1986-12-04 16:29:44 +00:00
|
|
|
LineNumber = 0;
|
|
|
|
#ifndef NOPP
|
|
|
|
WorkingDir = getwdir(source);
|
|
|
|
#endif NOPP
|
1986-03-10 13:07:55 +00:00
|
|
|
PushLex();
|
|
|
|
|
1987-02-09 23:19:42 +00:00
|
|
|
#ifdef DEBUG
|
1986-03-10 13:07:55 +00:00
|
|
|
#ifndef NOPP
|
1986-03-25 10:39:23 +00:00
|
|
|
if (pp_only) /* run the preprocessor as if it is stand-alone */
|
1986-03-10 13:07:55 +00:00
|
|
|
preprocess();
|
1987-02-09 23:19:42 +00:00
|
|
|
else
|
1986-03-10 13:07:55 +00:00
|
|
|
#endif NOPP
|
1987-02-09 23:19:42 +00:00
|
|
|
#endif DEBUG
|
|
|
|
{
|
1986-03-10 13:07:55 +00:00
|
|
|
|
1987-02-09 23:19:42 +00:00
|
|
|
init_code(destination);
|
1986-03-10 13:07:55 +00:00
|
|
|
|
|
|
|
/* compile the source text */
|
|
|
|
C_program();
|
|
|
|
|
|
|
|
#ifdef USE_TMP
|
1987-07-16 13:27:37 +00:00
|
|
|
prepend_scopes();
|
1986-03-10 13:07:55 +00:00
|
|
|
#endif USE_TMP
|
1987-07-16 13:27:37 +00:00
|
|
|
end_code();
|
1986-03-10 13:07:55 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
1986-07-18 21:10:42 +00:00
|
|
|
if (options['u']) {
|
|
|
|
unstack_level(); /* unstack L_GLOBAL */
|
|
|
|
}
|
1986-03-10 13:07:55 +00:00
|
|
|
if (options['f'] || options['t'])
|
|
|
|
dumpidftab("end of main", options['f'] ? 0 : 0);
|
|
|
|
#endif DEBUG
|
|
|
|
}
|
|
|
|
PopLex();
|
|
|
|
}
|
|
|
|
|
|
|
|
init()
|
|
|
|
{
|
|
|
|
init_cst(); /* initialize variables of "cstoper.c" */
|
|
|
|
reserve(tkidf); /* mark the C reserved words as such */
|
|
|
|
init_specials(special_ids); /* mark special ids as such */
|
|
|
|
|
1987-03-25 23:14:43 +00:00
|
|
|
#ifndef NOROPTION
|
1986-03-10 13:07:55 +00:00
|
|
|
if (options['R'])
|
|
|
|
reserve(tkother);
|
1987-03-25 23:14:43 +00:00
|
|
|
#endif
|
1986-03-10 13:07:55 +00:00
|
|
|
|
|
|
|
char_type = standard_type(CHAR, 0, 1, (arith)1);
|
|
|
|
uchar_type = standard_type(CHAR, UNSIGNED, 1, (arith)1);
|
|
|
|
|
|
|
|
short_type = standard_type(SHORT, 0, short_align, short_size);
|
|
|
|
ushort_type = standard_type(SHORT, UNSIGNED, short_align, short_size);
|
|
|
|
|
|
|
|
/* Treat type `word' as `int', having its own size and
|
|
|
|
alignment requirements.
|
|
|
|
This type is transparent to the user.
|
|
|
|
*/
|
|
|
|
word_type = standard_type(INT, 0, word_align, word_size);
|
|
|
|
uword_type = standard_type(INT, UNSIGNED, word_align, word_size);
|
|
|
|
|
|
|
|
int_type = standard_type(INT, 0, int_align, int_size);
|
|
|
|
uint_type = standard_type(INT, UNSIGNED, int_align, int_size);
|
|
|
|
|
|
|
|
long_type = standard_type(LONG, 0, long_align, long_size);
|
|
|
|
ulong_type = standard_type(LONG, UNSIGNED, long_align, long_size);
|
|
|
|
|
1986-09-12 09:16:07 +00:00
|
|
|
#ifndef NOFLOAT
|
1986-03-10 13:07:55 +00:00
|
|
|
float_type = standard_type(FLOAT, 0, float_align, float_size);
|
|
|
|
double_type = standard_type(DOUBLE, 0, double_align, double_size);
|
1986-09-12 09:16:07 +00:00
|
|
|
#endif NOFLOAT
|
1986-12-01 10:00:23 +00:00
|
|
|
void_type = standard_type(VOID, 0, 1, (arith)0);
|
1986-03-10 13:07:55 +00:00
|
|
|
label_type = standard_type(LABEL, 0, 0, (arith)0);
|
|
|
|
error_type = standard_type(ERRONEOUS, 0, 1, (arith)1);
|
|
|
|
|
|
|
|
/* Pointer Arithmetic type: all arithmetics concerning
|
|
|
|
pointers is supposed to be performed in the
|
|
|
|
pointer arithmetic type which is equal to either
|
|
|
|
int_type or long_type, depending on the pointer_size
|
|
|
|
*/
|
|
|
|
if (pointer_size == word_size)
|
|
|
|
pa_type = word_type;
|
|
|
|
else
|
|
|
|
if (pointer_size == short_size)
|
|
|
|
pa_type = short_type;
|
|
|
|
else
|
|
|
|
if (pointer_size == int_size)
|
|
|
|
pa_type = int_type;
|
|
|
|
else
|
|
|
|
if (pointer_size == long_size)
|
|
|
|
pa_type = long_type;
|
|
|
|
else
|
|
|
|
fatal("pointer size incompatible with any integral size");
|
1986-05-28 08:40:06 +00:00
|
|
|
|
|
|
|
if (int_size != word_size)
|
|
|
|
fatal("int_size and word_size are not equal");
|
1986-03-10 13:07:55 +00:00
|
|
|
if (short_size > int_size || int_size > long_size)
|
|
|
|
fatal("sizes of short/int/long decreasing");
|
|
|
|
|
|
|
|
/* Build a type for function returning int, RM 13 */
|
|
|
|
funint_type = construct_type(FUNCTION, int_type, (arith)0);
|
|
|
|
string_type = construct_type(POINTER, char_type, (arith)0);
|
|
|
|
|
|
|
|
/* Define the standard type identifiers. */
|
|
|
|
add_def(str2idf("char"), TYPEDEF, char_type, L_UNIVERSAL);
|
|
|
|
add_def(str2idf("int"), TYPEDEF, int_type, L_UNIVERSAL);
|
1986-09-12 09:16:07 +00:00
|
|
|
#ifndef NOFLOAT
|
1986-03-10 13:07:55 +00:00
|
|
|
add_def(str2idf("float"), TYPEDEF, float_type, L_UNIVERSAL);
|
|
|
|
add_def(str2idf("double"), TYPEDEF, double_type, L_UNIVERSAL);
|
1986-09-12 09:16:07 +00:00
|
|
|
#endif NOFLOAT
|
1986-03-10 13:07:55 +00:00
|
|
|
add_def(str2idf("void"), TYPEDEF, void_type, L_UNIVERSAL);
|
|
|
|
stack_level();
|
|
|
|
}
|
|
|
|
|
|
|
|
init_specials(si)
|
1986-09-24 13:53:16 +00:00
|
|
|
register struct sp_id *si;
|
1986-03-10 13:07:55 +00:00
|
|
|
{
|
|
|
|
while (si->si_identifier) {
|
|
|
|
struct idf *idf = str2idf(si->si_identifier);
|
|
|
|
|
|
|
|
if (idf->id_special)
|
|
|
|
fatal("maximum identifier length insufficient");
|
|
|
|
idf->id_special = si->si_flag;
|
|
|
|
si++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1987-02-09 23:19:42 +00:00
|
|
|
#ifdef DEBUG
|
1986-03-10 13:07:55 +00:00
|
|
|
#ifndef NOPP
|
|
|
|
preprocess()
|
|
|
|
{
|
|
|
|
/* preprocess() is the "stand-alone" preprocessor which
|
|
|
|
consecutively calls the lexical analyzer LLlex() to get
|
|
|
|
the tokens and prints them in a suitable way.
|
|
|
|
*/
|
|
|
|
static unsigned int lastlineno = 0;
|
|
|
|
static char *lastfilenm = "";
|
|
|
|
|
|
|
|
while (LLlex() != EOI) {
|
|
|
|
if (lastlineno != dot.tk_line) {
|
|
|
|
if (strcmp(lastfilenm, dot.tk_file) == 0) {
|
|
|
|
if (dot.tk_line - lastlineno <= 1) {
|
|
|
|
lastlineno++;
|
1986-04-03 14:32:56 +00:00
|
|
|
print("\n");
|
1986-03-10 13:07:55 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
lastlineno = dot.tk_line;
|
|
|
|
if (!options['P'])
|
1986-04-03 14:32:56 +00:00
|
|
|
print("\n#line %ld \"%s\"\n",
|
1986-03-17 17:47:04 +00:00
|
|
|
lastlineno,
|
|
|
|
lastfilenm
|
|
|
|
);
|
1986-03-10 13:07:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
lastfilenm = dot.tk_file;
|
|
|
|
lastlineno = dot.tk_line;
|
|
|
|
if (!options['P'])
|
1986-04-03 14:32:56 +00:00
|
|
|
print("\n#line %ld \"%s\"\n",
|
1986-03-10 13:07:55 +00:00
|
|
|
lastlineno, lastfilenm);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if (strcmp(lastfilenm, dot.tk_file) != 0) {
|
|
|
|
lastfilenm = dot.tk_file;
|
|
|
|
if (!options['P'])
|
1986-04-03 14:32:56 +00:00
|
|
|
print("\n#line %ld \"%s\"\n",
|
1986-03-10 13:07:55 +00:00
|
|
|
lastlineno, lastfilenm);
|
|
|
|
}
|
|
|
|
switch (DOT) {
|
|
|
|
case IDENTIFIER:
|
|
|
|
case TYPE_IDENTIFIER:
|
1986-04-03 14:32:56 +00:00
|
|
|
print("%s ", dot.tk_idf->id_text);
|
1986-03-10 13:07:55 +00:00
|
|
|
break;
|
|
|
|
case STRING:
|
1986-03-27 11:01:12 +00:00
|
|
|
{
|
1986-04-03 14:32:56 +00:00
|
|
|
char sbuf[1024]; /* a transient buffer */
|
1986-03-27 11:01:12 +00:00
|
|
|
char *bts2str();
|
|
|
|
|
1986-04-03 14:32:56 +00:00
|
|
|
print("\"%s\" ", bts2str(dot.tk_bts, dot.tk_len, sbuf));
|
1986-03-10 13:07:55 +00:00
|
|
|
break;
|
1986-03-27 11:01:12 +00:00
|
|
|
}
|
1986-03-10 13:07:55 +00:00
|
|
|
case INTEGER:
|
1986-04-03 14:32:56 +00:00
|
|
|
print("%ld ", dot.tk_ival);
|
1986-03-10 13:07:55 +00:00
|
|
|
break;
|
1986-09-12 09:16:07 +00:00
|
|
|
#ifndef NOFLOAT
|
1986-03-10 13:07:55 +00:00
|
|
|
case FLOATING:
|
1986-04-03 14:32:56 +00:00
|
|
|
print("%s ", dot.tk_fval);
|
1986-03-10 13:07:55 +00:00
|
|
|
break;
|
1986-09-12 09:16:07 +00:00
|
|
|
#endif NOFLOAT
|
1986-03-10 13:07:55 +00:00
|
|
|
case EOI:
|
|
|
|
case EOF:
|
|
|
|
return;
|
|
|
|
default: /* very expensive... */
|
1986-04-03 14:32:56 +00:00
|
|
|
print("%s ", symbol2str(DOT));
|
1986-03-10 13:07:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif NOPP
|
1987-02-09 23:19:42 +00:00
|
|
|
#endif DEBUG
|
1986-03-10 13:07:55 +00:00
|
|
|
|
1987-03-20 08:30:31 +00:00
|
|
|
No_Mem()
|
|
|
|
{
|
|
|
|
fatal("out of memory");
|
|
|
|
}
|
|
|
|
|
|
|
|
C_failed()
|
|
|
|
{
|
|
|
|
fatal("write failed");
|
|
|
|
}
|