Example #1
0
File: goto.c Project: z88dk/z88dk
SYMBOL* addgotosym(char* sname)
{
    char sname2[NAMESIZE * 3];
    strcpy(sname2, "00goto_");
    strcat(sname2, currfn->name);
    strcat(sname2, "_");
    strcat(sname2, sname);
    return (addglb(sname2, type_void, ID_GOTOLABEL, 0, 0, 0));
}
Example #2
0
TAG_SYMBOL *
defstruct (char *sname, int storage, int is_struct)
{
    int itag ;                              /* index of tag in tag symbol table */
    char nam[20];                           /* Dummy name */
    TAG_SYMBOL *tag = NULL;

    if ( tagptr >= ENDTAG ) {
        error(E_STROV) ;
    }

    if ( sname && sname[0] == 0 )
        sname = NULL;
   
    if ( sname && (tag = findtag(sname) ) ) {
        if ( tag->weak == 0 ) {
            if ( rcmatch('{') )
                multidef();
            else
                return tag;
        }
        itag = tag - tagtab;
    }

    /* No tag defined for this, so leave it alone */
    if ( tag == NULL ) {
        tag = tagptr++;
        itag = tag - tagtab ;
        sprintf(nam,"0st%d",itag);
        if ( sname == NULL )
            sname = nam;
        strcpy(tag->name,sname);
        tag->size = 0;
        tag->ptr = tag->end = membptr ;      /* Set so no member searches done.. */
        dummy_sym[NTYPE+1+itag] = addglb(nam,POINTER,STRUCT,0,STATIK,0,itag) ;
        tag->weak = 1;
    }

    if ( rcmatch('{' ) ) {        
        /* increment tagptr to add tag to table */
        tag->ptr = membptr;
        tag->weak = 0;
    
    
        needchar('{') ;
        while ( dodeclare(storage, tag, is_struct) )
            ;
        needchar('}') ;
        tag->end = membptr ;
    }
    return tag ;
}
Example #3
0
void defenum(char *sname, char storage)
{
    SYMBOL  *ptr;
    char    name[NAMEMAX];
    long    value;
    /* Add it into the symbol table, we do not need to keep track of the
     * tag because we treat enums as constants
     */
    addglb(sname, ENUM, CINT, 0, storage, 0, 0);
    value=0;        /* initial constant */
    needchar('{');
    do {
        if (symname(name)==0)
            illname(name);
        if ( cmatch('=') )
            constexpr(&value,1);
        ptr=addglb(name,VARIABLE,ENUM,0,STATIK,0,0);
        ptr->size=value;
        value++;

    } while (cmatch(','));
    needchar('}');
}
Example #4
0
void
#endif

setup_sym()
{
        defmac("Z80") ;
        defmac("SMALL_C") ;
        /* dummy symbols for pointers to char, int, double */
        /* note that the symbol names are not valid C variables */
        dummy_sym[0] = 0 ;
        dummy_sym[CCHAR] = addglb("0ch", POINTER, CCHAR, 0, STATIK, 0,0) ;
        dummy_sym[CINT] = addglb("0int", POINTER, CINT, 0, STATIK, 0,0) ;
        dummy_sym[DOUBLE] = addglb("0dbl", POINTER,DOUBLE,0,STATIK, 0,0) ;
        dummy_sym[LONG] = addglb("0lng", POINTER, LONG, 0, STATIK, 0,0) ;
        dummy_sym[CPTR] = addglb("0cpt", POINTER, CPTR, 0, STATIK, 0,0) ;
        dummy_sym[VOID] = addglb("0vd", POINTER, VOID, 0 , STATIK, 0,0) ;
}
Example #5
0
int main (int argc, char *argv[])
{
	char *p, *pp, *bp;
	char **oldargv = argv;
	char **link_lib;
	long smacptr;
	int first = 1;
	char *asmdefs_global_end;

	macptr = 0;
	ctext = 0;
	argc--; argv++;
	errs = 0;
	sflag = 0;
	cdflag = 0;
	verboseflag = 0;
	startup_incl = 0;
	optimize = 2;	/* -O2 by default */
	overlayflag = 0;
	asmdefs[0] = '\0';

	while ((p = *argv++)) {
		if (*p == '-') {
			while (*++p) switch (*p) {
				case 't': case 'T':
					ctext = 1;
					break;

				case 'c':
					if ((*(p + 1) == 'd')) {
						cdflag = 1;	/* pass '-cd' to assembler */
						p++;
						break;
					}
					else {
						usage(oldargv[0]);
						break;
					}

				case 's':
					if (strncmp(p, "scd", 3) == 0) {
						cdflag = 2;	/* pass '-scd' to assembler */
						p += 2;
						break;
					}
					else if (strncmp(p, "sgx", 3) == 0) {
						strcat(asmdefs, "_SGX = 1\n");
						defmac("_SGX");
						p += 2;
						break;
					}
				/* fallthrough */
				case 'S':
					sflag = 1;
					break;

				/* defines to pass to assembler */
				case 'a':
					if (strncmp(p, "acd", 3) == 0) {
						cdflag = 2;	/* pass '-scd' to assembler */
						strcat(asmdefs, "_AC = 1\n");
						defmac("_AC");
						p += 2;
						break;
					}
				/* fallthrough */
				case 'A':
					bp = ++p;
					if (!*p) usage(oldargv[0]);
					while (*p && *p != '=') p++;
					strncat(asmdefs, bp, (p - bp));
/*					if (*p == '=') *p = '\t'; */
					bp = ++p;
					strcat(asmdefs, "\t= ");
					if (*bp == '\0')
						strcat(asmdefs, "1\n");
					else {
						strcat(asmdefs, bp);
						strcat(asmdefs, "\n");
					}
					break;


				case 'v':
					verboseflag++;
					if (verboseflag > 1)
						ctext = 1;		/* "C" code in asm output */
					break;

				case 'd': case 'D':
					bp = ++p;
					if (!*p) usage(oldargv[0]);
					while (*p && *p != '=') p++;
					if (*p == '=') *p = '\t';
					while (*p) p++;
					p--;
					defmac(bp);
					break;

				case 'o':
					if (strncmp(p, "over", 4) == 0) {
						overlayflag = 1;
						if (strncmp(p, "overlay", 7) == 0)
							p += 6;
						else
							p += 3;
					}
					else {
						bp = ++p;
						while (*p && *p != ' ' && *p != '\t')
							p++;
						memcpy(user_outfile, bp, p - bp);
						user_outfile[p - bp] = 0;
						p--;
					}
					break;
				case 'O':
					/* David, made -O equal to -O2
					 * I'm too lazy to tape -O2 each time :)
					 */
					if (!p[1]) optimize = 2;
					else optimize = atoi(++p);
					break;

				case 'f':
					p++;
					if (!strcmp(p, "no-recursive")) {
						user_norecurse = 1;
						p += 11;
					}
					else if (!strcmp(p, "recursive")) {
						user_norecurse = 0;
						p += 8;
					}
					else if (!strcmp(p, "no-short-enums")) {
						user_short_enums = 0;
						p += 13;
					}
					else if (!strcmp(p, "short-enums")) {
						user_short_enums = 1;
						p += 10;
					}
					else
						goto unknown_option;
					break;

				case 'l':
					bp = ++p;
					while (*p && *p != ' ' && *p != '\t')
						p++;
					link_libs = realloc(link_libs, (link_lib_ptr + 2) * sizeof(*link_libs));
					link_libs[link_lib_ptr] = malloc(p - bp + 1);
					memcpy(link_libs[link_lib_ptr], bp, p - bp);
					link_libs[link_lib_ptr][p - bp] = 0;
					strcat(asmdefs, "LINK_");
					strcat(asmdefs, link_libs[link_lib_ptr]);
					strcat(asmdefs, "\t= 1\n");
					link_libs[++link_lib_ptr] = 0;
					p--;
					break;

				case 'm':
					if (!strcmp(p + 1, "small")) {
						strcat(asmdefs, "SMALL\t= 1\n");
						p += 5;
					}
					else {
unknown_option:
						fprintf(stderr, "unknown option %s\n", p);
						exit(1);
					}
					break;

				default:
					usage(oldargv[0]);
				}
		}
		else {
			infiles = realloc(infiles, (infile_ptr + 2) * sizeof(*infiles));
			infiles[infile_ptr++] = p;
			infiles[infile_ptr] = 0;
		}
	}

	smacptr = macptr;
	if (!infiles)
		usage(oldargv[0]);
	printf(HUC_VERSION);
	printf("\n");
	init_path();
	/* Remember the first file, it will be used as the base for the
	   output file name unless there is a user-specified outfile. */
	p = pp = infiles[0];
	/* Labels count is not reset for each file because labels are
	   global and conflicts would arise. */
	nxtlab = 0;
	link_lib = link_libs;
	infile_ptr = 1;
	/* Remember where the global assembler defines end so we can
	   reset to that point for each file. */
	/* XXX: Even if we don't repeat the local asm defines, they
	   are still defined because we compile everything into one
	   assembly file. */
	asmdefs_global_end = asmdefs + strlen(asmdefs);
	while (p) {
		errfile = 0;
		/* Truncate asm defines to the point where global
		   defines end. */
		asmdefs_global_end[0] = 0;
		if (extension(p) == 'c' || extension(p) == 'C') {
			glbptr = STARTGLB;
			locptr = STARTLOC;
			wsptr = ws;
			inclsp =
			iflevel =
			skiplevel =
			swstp =
			litptr =
			stkp =
			errcnt =
			ncmp =
			lastst =
			quote[1] =
			const_nb =
			line_number = 0;
			macptr = smacptr;
			input2 = NULL;
			quote[0] = '"';
			cmode = 1;
			glbflag = 1;
			litlab = getlabel();
			member_table_index = 0;
			memset(member_table, 0, sizeof(member_table));
			tag_table_index = 0;
			norecurse = user_norecurse;
			typedef_ptr = 0;
			enum_ptr = 0;
			enum_type_ptr = 0;
			memset(fastcall_tbl, 0, sizeof(fastcall_tbl));
			defpragma();

			/* Macros and globals have to be reset for each
			   file, so we have to define the defaults all over
			   each time. */
			defmac("__end\t__memory");
			addglb("__memory", ARRAY, CCHAR, 0, EXTERN, 0);
			addglb("stack", ARRAY, CCHAR, 0, EXTERN, 0);
			rglbptr = glbptr;
			addglb("etext", ARRAY, CCHAR, 0, EXTERN, 0);
			addglb("edata", ARRAY, CCHAR, 0, EXTERN, 0);
			/* PCE specific externs */
			addglb("font_base", VARIABLE, CINT, 0, EXTERN, 0);
			addglb_far("vdc", CINT);
			addglb_far("vram", CCHAR);
			/* end specific externs */
			defmac("huc6280\t1");
			defmac("huc\t1");

			if (cdflag == 1)
				defmac("_CD\t1");
			else if (cdflag == 2)
				defmac("_SCD\t1");
			else
				defmac("_ROM\t1");

			if (overlayflag == 1)
				defmac("_OVERLAY\t1");

//			initmac();
			/*
			 *	compiler body
			 */
			if (!openin(p))
				exit(1);
			if (first && !openout())
				exit(1);
			if (first)
				header();
			asmdefines();
//			gtext ();
			parse();
			fclose(input);
//			gdata ();
			dumplits();
			dumpglbs();
			errorsummary();
//			trailer ();
			pl("");
			errs = errs || errfile;
		}
		else {
			fputs("Don't understand file ", stderr);
			fputs(p, stderr);
			fputc('\n', stderr);
			exit(1);
		}
		p = infiles[infile_ptr];
		if (!p && link_lib && *link_lib) {
			/* No more command-line files, continue with
			   libraries. */
			p = lib_to_file(*link_lib);
			if (!p) {
				fprintf(stderr, "cannot find library %s\n", *link_lib);
				exit(1);
			}
			link_lib++;
		}
		else
			infile_ptr++;
		first = 0;
	}
	dumpfinal();
	fclose(output);
	if (!errs && !sflag) {
		if (user_outfile[0])
			errs = errs || assemble(user_outfile);
		else
			errs = errs || assemble(pp);
	}
	exit(errs != 0);
}
Example #6
0
newfunc ()
{
        char    n[NAMESIZE], *ptr;
        fexitlab = getlabel();

        if (!symname (n) ) {
                error ("illegal function or declaration");
                kill ();
                return;
        }
        if (ptr = findglb (n)) {
                if (ptr[IDENT] != FUNCTION)
                        multidef (n);
                else if (ptr[OFFSET] == FUNCTION)
                        multidef (n);
                else
                        ptr[OFFSET] = FUNCTION;
        } else
                addglb (n, FUNCTION, CINT, FUNCTION, PUBLIC);
        prologue ();
        if (!match ("("))
                error ("missing open paren");
        prefix ();
        outstr (n);
        col ();
        nl ();
        locptr = STARTLOC;
        argstk = 0;
        while (!match (")")) {
                if (symname (n)) {
                        if (findloc (n))
                                multidef (n);
                        else {
                                addloc (n, 0, 0, argstk, AUTO);
                                argstk = argstk + intsize();
                        }
                } else {
                        error ("illegal argument name");
                        junk ();
                }
                blanks ();
                if (!streq (line + lptr, ")")) {
                        if (!match (","))
                                error ("expected comma");
                }
                if (endst ())
                        break;
        }
        stkp = 0;
        argtop = argstk;
        while (argstk) {
                if (amatch ("register", 8)) {
                        if (amatch("char", 4))
                                getarg(CCHAR);
                        else if (amatch ("int", 3))
                                getarg(CINT);
                        else
                                getarg(CINT);
                        ns();
                } else if (amatch ("char", 4)) {
                        getarg (CCHAR);
                        ns ();
                } else if (amatch ("int", 3)) {
                        getarg (CINT);
                        ns ();
                } else {
                        error ("wrong number args");
                        break;
                }
        }
        statement(YES);
        printlabel(fexitlab);
        col();
        nl();
        modstk (0);
        gret ();
        stkp = 0;
        locptr = STARTLOC;

}
Example #7
0
SYMBOL *
#endif

AddFuncCode(char *n, char type, char ident, char sign,char zfar, int storage, int more, char check,char simple,TAG_SYMBOL *otag, long *addr)
{
    unsigned char tvalue;           /* Used to hold protot value */
    char    typ;                    /* Temporary type */
    int     itag;

    itag=0;
    if (otag)
        itag=otag-tagtab;       /* tag number */

    lastst = 0;                     /* no last statement */
    locptr = STARTLOC ;             /* deallocate all locals */
    fnstart = lineno ;              /* remember where fn began */
    /*
     * Do some elementary checking before hand..
     */
    if (zfar && ident!=FUNCTIONP) { zfar=NO; warning(W_FAR); }
    if ( ( currfn=findglb(n) ) ) {
        /* already in symbol table ? */
        if ( currfn->ident != FUNCTION && currfn->ident != FUNCTIONP ) {
            /* already variable by that name */
            multidef();
        }
        else if ( currfn->offset.i == FUNCTION && !currfn->prototyped) {
            /* already function by that name */
            multidef();
        }
        else {
            /* we have what was earlier assumed to be a function */

            if (currfn->storage == EXTERNAL && currfn->flags&LIBRARY ) {
                /* Overwriting a lib function, is that what you wanted?!? Handy for
                 * compiling the library though!! Change type to local static to prevent
                 * being dumped in the scope list..
                 */
				if (makelib || storage == LSTATIC ) {
                    currfn->storage=LSTATIC;
				} else {
					currfn->storage=LIBOVER;
				} 
                currfn->offset.i=FUNCTION;
            } else {
                /*
                 * I'm not sure what *exactly* I was trying to achieve here djm 25/2/00
                 */
                if (currfn->storage != EXTERNAL && ( (currfn->flags&LIBRARY) != LIBRARY) ) {
                    currfn->flags&=(~LIBRARY);
                    currfn->size = 0;
                }
                currfn->offset.i = FUNCTION ;
                currfn->storage = storage;
            }
        }
    }
    /* if not in table, define as a function now */
    else {  
        typ=type;
        if (ident == FUNCTIONP) typ=(zfar ? CPTR : CINT );

        currfn = addglb(n, FUNCTION,  typ, FUNCTION, storage, more, 0);
        currfn->size=0;
        currfn->prototyped=0;
        currfn->flags= (sign&UNSIGNED) | (zfar&FARPTR);
        if (type == STRUCT) currfn->tagarg[0]=itag;
        /*
         *      Set our function prototype - what we are!
         *      args[0] is free for use
         */
        currfn->args[0]=CalcArgValue(type, ident, currfn->flags);

    }
    tvalue=CalcArgValue(type,ident,((sign&UNSIGNED) | (zfar&FARPTR)) );
    if ( currfn->args[0] != tvalue || (type==STRUCT && currfn->tagarg[0] != itag  )  ){
        char buffer[120];
        warning(W_DIFFTYPE);
        warning(W_DIFFTYPE2,ExpandArgValue(currfn->args[0],buffer,currfn->tagarg[0]));
        warning(W_DIFFTYPE3,ExpandArgValue(tvalue,buffer,itag) );

    }

    /* we had better see open paren for args... */
    if ( check && (cmatch('(') == 0) )
        error(E_PAREN);


    locptr = STARTLOC ;             /* "clear" local symbol table */
    undeclared = 0 ;                /* init arg count */


    /* Check to see if we are doing ANSI fn defs - must be a better way of
     * doing this! (Have an array and check by that?)           
     */
    if (CheckANSI()) {
        return( dofnansi(currfn, addr) ); /* So we can pass back result */
    }
    DoFnKR(currfn,simple);
    return(0);
}
Example #8
0
void
do_inc_ex(int type)
{
	int end;
	int i;
	INTPTR_T j;
	int num;
	int nb_tile;
	char label[NAMESIZE];
	char label2[NAMESIZE];
	char str[NAMESIZE+32];
	struct {
		char fname[FILENAMESIZE];
		INTPTR_T  arg[5];
	} tiles[16];

	if(!match("(")) {
		error("missing '('");
		kill_line();
		return;
	}

	readstr(); /* read the label name */
	strcpy(label, litq2);
	strcpy(label2, litq2);
	strcpy(str, "__data__");
	for(i = (int)strlen(label2), j = 0; i < NAMEMAX; i++)
		label2[i] = str[j++];
	label2[i] = '\0';
	addglb(label2, ARRAY, CINT, 0, EXTERN);
	addglb(label, ARRAY, CINT, 0, EXTERN);

	if(!match(",")) {
		error("comma missing");
		kill_line();
		return;
	}

	end = 0;
	num = 0;
	nb_tile = 0;
	while (!end) {
		// if (match("\\"));
	    if(!readqstr()) {
			error("not a file name");
			kill_line();
			return;
		}
		if(!match(",")) {
			error("comma missing");
			kill_line();
			return;
		}
		strcpy(tiles[num].fname, litq2);

		for (i = 0; i < 5; i++) {
			// if (match("\\"));
			if(!number(&tiles[num].arg[i])) {
				error("not a number");
				kill_line();
				return;
			}
			if (match(")")) {
				if (i == 4) {
					kill_line();
					end = 1;
					break;
				}
				else {
					error("arg missing");
					kill_line();
					return;
				}
			}
			if(!match(",")) {
				error("comma missing");
				kill_line();
				return;
			}
			while((ch() == ' ') || (ch() == '\t'))
				gch();
			if (ch() == '\0') {
				error("arg missing");
				kill_line();
				return;
			}
		}
		nb_tile += tiles[num].arg[2] * tiles[num].arg[3];
		num++;
		if (num == 16) {
			if(!end) {
				error("too many args (max 16 files)");
				kill_line();
				return;
			}
		}
	}

	/* create const array to hold extra infos */
	new_const();
	const_val[const_val_idx++] = const_data_idx;	/* number of tile */
	sprintf(str, "%i", nb_tile);
	add_buffer(str, '(');
	const_data[const_data_idx++] = '\0';
	const_val[const_val_idx++] = const_data_idx;	/* tile size */
	sprintf(str, "%i", type);
	add_buffer(str, '(');
	const_data[const_data_idx++] = '\0';
	const_val[const_val_idx++] = const_data_idx;	/* tile bank */
	sprintf(str, "BANK(_%s)", label2);
	add_buffer(str, '(');
	const_data[const_data_idx++] = '\0';
	const_val[const_val_idx++] = const_data_idx;	/* tile addr */
	sprintf(str, "     _%s", label2);
	add_buffer(str, '(');
	const_data[const_data_idx++] = '\0';
	const_val[const_val_idx++] = -(litptr + 1024);	/* pal idx table addr */
	add_const(CINT);

	/* create pal idx table */
	for(i = 0; i < num; i++) {
		j = tiles[i].arg[2] * tiles[i].arg[3];
		while (j) {
			j--;
			if (litptr < LITMAX)
				litq[litptr++] = (tiles[i].arg[4] << 4);
		}
	}

	/* dump incchr/tile cmds */
	ol(".data");
	if (type == 8)
		ol(".dw $0800");
	else
		ol(".dw $1000");
	prefix();
	outstr(label2);
	outstr(":\n");
	for(i = 0; i < num; i++) {
		if (type == 8)
			ot(".incchr \"");
		else
			ot(".inctile \"");
		outstr(tiles[i].fname);
		outstr("\"");
		for (j = 0; j < 4; j++) {
			outstr(",");
			outdec(tiles[i].arg[j]);
		}
		newl();
	}
	ol(".code");
	kill_line();
}