home *** CD-ROM | disk | FTP | other *** search
- /*
- * 68K/386 32-bit C compiler.
- *
- * copyright (c) 1996, David Lindauer
- *
- * This compiler is intended for educational use. It may not be used
- * for profit without the express written consent of the author.
- *
- * It may be freely redistributed, as long as this notice remains intact
- * and sources are distributed along with any executables derived from them.
- *
- * The author is not responsible for damages, either direct or consequential,
- * that may arise from use of this software.
- *
- * v1.5 August 1996
- * David Lindauer, gclind01@starbase.spd.louisville.edu
- *
- * Credits to Mathew Brandt for original K&R C compiler
- *
- */
- #include <stdio.h>
- #include "expr.h"
- #include "c.h"
- #include "gen.h"
- #include "cglbdec.h"
-
- extern TYP *head, *tail;
- extern char declid[100];
- extern int prm_listfile;
- extern int prm_cplusplus;
-
- extern int goodcode;
-
- TABLE oldlsym;
-
- static int skm_func[] = { closepa,begin,semicolon,0 };
-
- SYM *currentfunc=0;
-
- /* function compilation routines */
- void funcini(void)
- {
- currentfunc = 0;
- }
- void declfuncarg(void)
- { char *names[50]; /* 50 parameters maximum */
- int nparms, poffset, i, inline;
- SYM *sp1, *sp2, *spsave[50], *oldargs=0;
- TYP *temp, *temp1;
- char oldeclare[100];
- char *nm;
- poffset = 0; /* size of return block */
- nparms = 0;
- temp = head;
- temp1 = tail;
- if (!prm_cplusplus) {
- sp2 = gsearch(declid);
- if (sp2) {
- oldargs = sp2->tp->lst.head;
- }
- }
- temp->lst.head = temp->lst.tail = 0;
- if (lastst == kw_void) {
- getsym();
- if (lastst != closepa) {
- backup(lastst);
- lastst = kw_void;
- }
- else {
- temp->lst.head = (SYM *)-1;
- getsym();
- goto verify;
- }
- }
- else
- if (lastst == closepa) {
- getsym();
- goto verify;
- }
- strcpy(oldeclare,declid);
- if(!prm_cplusplus && lastst == id &&
- ((sp1 = search(nm=litlate(lastid),&gsyms)) ==0 || sp1->storage_class != sc_type)
- && ((sp1 = search(nm,&lsyms)) ==0 || sp1->storage_class != sc_type)) {
- /* declare parameters */
- global_flag = 0;
- while(lastst == id) {
- names[nparms++] = nm;
- getsym();
- if( lastst == comma)
- getsym();
- else
- break;
- }
- global_flag = 1;
- needpunc(closepa,skm_func);
- doargdecl(sc_member,0,0,&temp->lst,inline = FALSE); /* declare parameters */
- }
- else {
- doargdecl(sc_member,names,&nparms,&temp->lst,inline = TRUE);
- needpunc(closepa,skm_func);
- }
- strcpy(declid,oldeclare);
- if (temp->btp->type == bt_struct || temp->btp->type == bt_union)
- poffset += 4;
- for(i = 0;i < nparms;++i) {
- if ((sp1 = search(names[i],&temp->lst)) == 0 && !inline)
- sp1 = makeint(litlate(names[i]),&temp->lst);
- if (sp1->tp)
- sp1->tp->uflags |= UF_DEFINED;
- spsave[i] = sp1;
- if( sp1->tp->size < 4 )
- {
- #ifdef i386
- sp1->value.i = poffset;
- #else
- sp1->value.i = poffset + (4 - sp1->tp->size);
- #endif
- poffset += 4;
- }
- else
- {
- sp1->value.i = poffset;
- poffset += sp1->tp->size;
- }
- sp1->storage_class = sc_auto;
- }
- if (!inline) {
- temp->lst.head = temp->lst.tail = 0;
- for (i=0; i < nparms; i++)
- insert(spsave[i],&temp->lst);
- }
- if (prm_cplusplus && temp->lst.head == 0)
- temp->lst.head = (SYM *) -1; /* () is equiv to (void) in cpp */
- verify:
- head = temp;
- tail = temp1;
- if (prm_cplusplus)
- return;
-
- if (oldargs && head->sname) {
- SYM *newargs = head->lst.head;
- while (oldargs && newargs) {
- if (!checktype(oldargs->tp,newargs->tp))
- gensymerror(ERR_ARGMISMATCH,newargs->name);
- if (oldargs == (SYM *)-1 || newargs == (SYM *)-1)
- break;
- oldargs = oldargs->next;
- newargs = newargs->next;
- }
- if (oldargs && oldargs != (SYM *)-1)
- gensymerror(ERR_ARGLENSHORT,head->sname);
- if (newargs && newargs != (SYM *)-1)
- gensymerror(ERR_ARGLENLONG,head->sname);
- }
- if (sp2 && !checktype(sp2->tp,head))
- gensymerror(ERR_FUNCMISMATCH,head->sname);
- }
- void check_funcused(TABLE *oldlsym, TABLE *lsyms)
- {
- /* oldlsym Must BE 0 at the end of a function */
- SYM *sym1;
- if (oldlsym && oldlsym->head == lsyms->head) {
- return;
- }
- sym1 = lsyms->head;
- while (sym1 && (!oldlsym || sym1 != oldlsym->head)) {
- if (sym1->tp->type != bt_func && sym1->tp->type != bt_ifunc
- && sym1->storage_class != sc_type) {
- if (!(sym1->tp->uflags & UF_USED)) {
- if (sym1->storage_class == sc_label) {
- if (!oldlsym)
- gensymerror(ERR_UNUSEDLABEL,sym1->name);
- }
- else gensymerror(ERR_SYMUNUSED,sym1->name);
- }
- if (sym1->tp->uflags & UF_ASSIGNED)
- gensymerror(ERR_SYMASSIGNED,sym1->name);
- if (!oldlsym && sym1->storage_class == sc_ulabel)
- gensymerror(ERR_UNDEFLABEL,sym1->name);
- }
- sym1 = sym1->next;
- }
- }
- void funcbody(SYM *sp)
- /*
- */
- {
- SYM *sp1 = sp->tp->lst.head, *sp2;
- global_flag = 0;
- if (sp1 != (SYM *)-1) {
- while (sp1) {
- if (sp1->name[0] != '*') {
- sp2 = xalloc(sizeof(SYM));
- memcpy(sp2,sp1,sizeof(SYM));
- insert(sp2,&lsyms);
- }
- sp1 = sp1->next;
- }
- }
- currentfunc = sp;
- cseg();
- gen_strlab(sp->name);
- goodcode &= ~GF_RETURN;
- block();
- check_funcused(0,&lsyms);
- if (sp->tp->btp->type != bt_void && !(goodcode &GF_RETURN))
- generror(ERR_FUNCRETVAL,0,0);
- nl();
- currentfunc = 0;
- lc_auto = lc_maxauto = 0;
- if (prm_listfile) {
- fprintf(listFile,"\n\n*** local symbol table ***\n\n");
- list_table(&lsyms,0);
- fprintf(listFile,"\n\n\n");
- }
-
- release_local(); /* release local symbols */
- global_flag = 1;
- }
-
- SYM *makeint(char *name, TABLE *table)
- { SYM *sp;
- TYP *tp;
- global_flag++;
- sp = xalloc(sizeof(SYM));
- tp = xalloc(sizeof(TYP));
- global_flag--;
- tp->type = bt_long;
- tp->size = 4;
- tp->btp = tp->lst.head = 0;
- tp->sname = 0;
- sp->name = name;
- sp->storage_class = sc_auto;
- sp->tp = tp;
- insert(sp,table);
- return sp;
- }
-
- void block(void)
- {
- SNODE *snp3;
- lastst = 0;
- snp3 = snp_line();
- lastst = begin;
- needpunc(begin,0);
- dodecl(sc_auto);
- lc_maxauto = lc_auto;
- cseg();
- if (snp3)
- snp3->next = compound();
- else
- snp3 = compound();
- if (lc_maxauto < lc_auto) {
- lc_maxauto = lc_auto;
- }
- lc_maxauto +=3;
- lc_maxauto &= 0xfffffffcL;
- genfunc(snp3);
- flush_peep();
- }
- void gather_labels(TABLE *oldlsym, TABLE *lsyms)
- {
- if (oldlsym->head == lsyms->head) {
- return;
- }
- else {
- SYM *sp = lsyms->head,*r = oldlsym->head;
- while (sp && sp != r) {
- SYM *q = sp->next;
- if (sp->storage_class == sc_label || sp->storage_class == sc_ulabel) {
- if (!oldlsym->head) {
- oldlsym->head = oldlsym->tail = sp;
- lsyms->tail = lsyms->tail->next = sp;
- }
- else {
- oldlsym->tail->next = sp;
- sp->next = 0;
- oldlsym->tail = sp;
- }
- }
- sp = q;
- }
- if (oldlsym->tail)
- oldlsym->tail->next = 0;
- }
- }
- SNODE *compoundblock(void)
- {
- SNODE *snp;
- TABLE oldoldlsym;
- int oldlcauto = lc_auto;
- oldoldlsym = oldlsym;
- oldlsym = lsyms;
- dodecl(sc_auto);
- if (lc_auto > lc_maxauto) {
- lc_maxauto = lc_auto;
- }
- cseg();
- snp = compound();
- check_funcused(&oldlsym,&lsyms);
- gather_labels(&oldlsym,&lsyms);
- lc_auto = oldlcauto;
- lsyms = oldlsym;
- oldlsym.head = oldoldlsym.head;
- return(snp);
- }