#include "vbc.h"

int endok=1;
int line,errors;

char *multname[]={"","s"};
void raus(void)
/*  Beendet das Programm                                            */
{
    if(DEBUG) printf("raus()");
    if(!endok) printf("unexpected end of file\n");
    if(errors) printf("%d error%s found!\n",errors,multname[errors>1]);
    while(nesting>=0) leave_block();
    if(in) fclose(in);
    if(out) fclose(out);
    if(ic1) fclose(ic1);
    if(ic2) fclose(ic2);
    if(endok&&!errors) exit(EXIT_SUCCESS); else exit(EXIT_FAILURE);
}
void translation_unit(void)
/*  bearbeitet translation_unit                                     */
/*  hier z.Z. nur provisorisch                                      */
{
    while(1){
        killsp();
        if(!isalpha(*s)&&*s!='_'){
            if(*s!=EOF) error(0);
            raus();
        }
        endok=0;
        var_declaration();
        endok=1;
    }
}

void dontwarn(char *p)
/*  schaltet flags fuer Meldung auf DONTWARN    */
{
    int i;
    if(*p!='=') error(4,"-dontwarn");
    i=atoi(p+1);
    if(i>=err_num) error(159,i);
    if(err_out[i].flags&(ANSIV|FATAL)) error(160,i);
    err_out[i].flags|=DONTWARN;
}

extern char *copyright;

int main(int argc,char *argv[])
{
    int i,j,fname=0;
    c_flags_val[9].f=dontwarn;
    for(i=1;i<argc;i++){
        if(*argv[i]!='-'){  /*  kein Flag   */
            if(fname){
                error(1);
            }else fname=i;
        }else{
            int flag=0;
            for(j=0;j<MAXCF;j++){
                size_t l;
                if(!c_flags_name[j]) continue;
                l=strlen(c_flags_name[j]);
                if(l>0&&!strncmp(argv[i]+1,c_flags_name[j],l)){
                    flag=1;
                    if((c_flags[j]&(USEDFLAG|FUNCFLAG))==USEDFLAG){error(2,argv[i]);break;}
                    c_flags[j]|=USEDFLAG;
                    if(c_flags[j]&STRINGFLAG){
                        if(argv[i][l+1]!='='){error(3,argv[i]);}
                        c_flags_val[j].p=&argv[i][l+2];
                    }
                    if(c_flags[j]&VALFLAG){
                        if(argv[i][l+1]!='='){error(4,argv[i]);}
                        c_flags_val[j].l=atol(&argv[i][l+2]);
                    }
                    if(c_flags[j]&FUNCFLAG) c_flags_val[j].f(&argv[i][l+1]);
                }
            }
            for(j=0;j<MAXGF;j++){
                size_t l;
                if(!g_flags_name[j]) continue;
                l=strlen(g_flags_name[j]);
                if(l>0&&!strncmp(argv[i]+1,g_flags_name[j],l)){
                    flag=1;
                    if((g_flags[j]&(USEDFLAG|FUNCFLAG))==USEDFLAG){error(2,argv[i]);break;}
                    g_flags[j]|=USEDFLAG;
                    if(g_flags[j]&STRINGFLAG){
                        if(argv[i][l+1]!='='){error(3,argv[i]);}
                        g_flags_val[j].p=&argv[i][l+2];
                    }
                    if(g_flags[j]&VALFLAG){
                        if(argv[i][l+1]!='='){error(4,argv[i]);}
                        g_flags_val[j].l=atol(&argv[i][l+2]);
                    }
                    if(g_flags[j]&FUNCFLAG) g_flags_val[j].f(&argv[i][l+1]);
                }
            }
            if(!flag){error(5,argv[i]);}
        }
    }
    if(!(c_flags[6]&USEDFLAG)) printf(copyright);
    if(!(c_flags[8]&USEDFLAG)) c_flags_val[8].l=10; /* max. Fehlerzahl */
    if(!fname){error(6);}
    inname=argv[fname];
    if(!init_cg()) exit(EXIT_FAILURE);
    in=fopen(inname,"r");
    if(!in) {error(7,inname);}
    out=open_out(inname,"asm");
    if(!out){fclose(in);exit(EXIT_FAILURE);}
    if(c_flags[2]&USEDFLAG) ic1=open_out(inname,"ic1");
    if(c_flags[3]&USEDFLAG) ic2=open_out(inname,"ic2");
    if(c_flags[4]&USEDFLAG) DEBUG=c_flags_val[4].l; else DEBUG=0;
    switch_count=0;break_label=0;
    *string=0;s=string;line=0;
    killsp();
    nesting=-1;enter_block();
    translation_unit();
}

void prd(FILE *o,struct Typ *p)
/* Gibt einen Typ auf dem Bildschirm aus    */
{
    int f;
    if(!p) {fprintf(o,"leerer Typ ");return;}
    f=p->flags;
    fprintf(o,"(Sizeof=%d,flags=%d)",szof(p),f);
    if(f&UNCOMPLETE) {fprintf(o,"Unkompletter Typ ");f&=~UNCOMPLETE;}
    if(f&CONST) {fprintf(o,"const ");f&=~CONST;}
    if(f&VOLATILE) {fprintf(o,"volatile ");f&=~VOLATILE;}
    if(f&UNSIGNED) {fprintf(o,"unsigned ");f&=~UNSIGNED;}
    if(f==FUNKT) {fprintf(o,"Funktion mit Parametern (");
                  prl(o,p->exact);
                  fprintf(o,") und Rueckgabe ");prd(o,p->next);return;}
    if(f==STRUCT){fprintf(o,"Struktur mit Komponenten {");
                  if(p->flags&UNCOMPLETE) fprintf(o,"%s} ",(char *)p->exact);
                   else {prl(o,p->exact);fprintf(o,"} ");}
                  return;
    }
    if(f==UNION) {fprintf(o,"Union mit Komponenten {");
                  if(p->flags&UNCOMPLETE) fprintf(o,"%s} ",(char *)p->exact);
                   else {prl(o,p->exact);fprintf(o,"} ");return;}
    }
    if(f==POINTER) {fprintf(o,"Zeiger auf ");prd(o,p->next);return;}
    if(f==ARRAY) {fprintf(o,"Array [%d] von ",p->size);prd(o,p->next);return;}
    fprintf(o,"%s",typname[f]);
}
void prl(FILE *o,struct struct_declaration *p)
/* Gibt eine struct_declaration auf dem Bildschirm aus */
{
    int i;
    for(i=0;i<p->count;i++) {fprintf(o,"(%d,%s) ",i,p->sl[i].identifier); prd(o,p->sl[i].styp);}
}
void freetyp(struct Typ *p)
/* Gibt eine Typ-Liste frei, aber keine struct_declaration oder so */
{
    int f;struct Typ *merk;
    if(DEBUG&8){printf("freetyp: ");prd(stdout,p);printf("\n");}
    while(p){
        merk=p->next;
        f=p->flags&15;
        if(merk&&f!=ARRAY&&f!=POINTER&&f!=FUNKT){ierror(0);return;}
        free(p);
        p=merk;
    }
}
void cpbez(char *m)
/* kopiert den naechsten Bezeichner von s nach m */
{
    if(DEBUG&128) printf("Before cpbez:%s\n",s);
    while(isalpha(*s)||isnum(*s)||*s=='_') *m++=*s++;
    *m++=0;
    if(DEBUG&128) printf("After cpbez:%s\n",s);
}
void cpnum(char *m)
/* kopiert die naechste int-Zahl von s nach m   */
/* muss noch erheblich erweiter werden          */
{
    if(DEBUG&128) printf("Before cpnum:%s\n",s);
    while(isnum(*s)) *m++=*s++;
    *m++=0;
    if(DEBUG&128) printf("After cpnum:%s\n",s);

}
void killsp(void)
/*  Ueberspringt Fuellzeichen                   */
{
    if(DEBUG&128) printf("Before killsp:%s\n",s);
    if(*s==EOF) raus();
    while(*s==' '||*s=='\t'||*s=='\n'||*s=='\r'){
/*        if(*s=='\n') {line++;if(DEBUG&1) printf("Line %d\n",line);}*/
        s++;
    }
    if(*s==0){
        do{
            if(!fgets(string,MAXINPUT,in)){
                /*raus();*/
                s=string;*s=EOF;
                return;
            }else{line++;if(DEBUG&1) printf("Line %d\n",line);}
            s=string;
        }while(*s=='#');
        killsp();
    }
    if(DEBUG&128) printf("After killsp:%s\n",s);
}
int szof(struct Typ *t)
/*  liefert die benoetigte Groesse eines Typs in Bytes      */
/*  maschinenabhaengig                                      */
{
    int i=t->flags,j,size,m,f;
    /* wofuer was das hier gedacht ?    */
/*    if(type_uncomplete(t)) return(0);*/
    if(i&UNCOMPLETE) return(0);
    if((i&15)==POINTER) return(4);
    if((i&15)==ARRAY) return((t->size)*szof(t->next));
    if((i&15)==UNION){
        for(j=0,size=0;j<t->exact->count;j++){
            m=szof(t->exact->sl[j].styp);
            if(m==0) return(0);
            if(m>size) size=m;
        }
        return(((size+align[UNION]-1)/align[UNION])*align[UNION]); /* align */
    }
    if((i&15)==STRUCT){
        for(j=0,size=0;j<t->exact->count;j++){
            m=szof(t->exact->sl[j].styp);
            if(m==0) return(0);
            f=(t->exact->sl[j].styp->flags)&15;
            size=((size+align[f]-1)/align[f])*align[f];
            size+=m;
        }
        return(((size+align[STRUCT]-1)/align[STRUCT])*align[STRUCT]);
    }
    if(DEBUG&2) printf("sizeof(%d)=%d\n",i&15,sizetab[i&15]);
    return(sizetab[i&15]);
}
void enter_block(void)
/*  Setzt Zeiger/Struckturen bei Eintritt in neuen Block    */
{
    if(nesting>=MAXN){error(9,nesting);return;}
    nesting++;
    if(DEBUG&1) printf("enter block %d\n",nesting);
    first_ilist[nesting]=last_ilist[nesting]=0;
    first_sd[nesting]=last_sd[nesting]=0;
    first_si[nesting]=last_si[nesting]=0;
    first_var[nesting]=last_var[nesting]=0;
    if(nesting==1){
        first_llist=last_llist=0;
        first_clist=last_clist=0;
        merk_varf=merk_varl=0;
        merk_ilistf=merk_ilistl=0;
        merk_sif=merk_sil=0;
        merk_sdf=merk_sdl=0;
        afterlabel=0;
    }
}
void leave_block(void)
/*  Setzt Zeiger/Struckturen bei Verlassen eines Blocks     */
{
    if(nesting<0){error(10);return;}
    if(DEBUG&1) printf("leave block %d\n",nesting);
    if(nesting>0){
        if(merk_varl) merk_varl->next=first_var[nesting]; else merk_varf=first_var[nesting];
        if(last_var[nesting]) merk_varl=last_var[nesting];
        if(merk_sil) merk_sil->next=first_si[nesting]; else merk_sif=first_si[nesting];
        if(last_si[nesting]) merk_sil=last_si[nesting];
        if(merk_sdl) merk_sdl->next=first_sd[nesting]; else merk_sdf=first_sd[nesting];
        if(last_sd[nesting]) merk_sdl=last_sd[nesting];
        if(merk_ilistl) merk_ilistl->next=first_ilist[nesting]; else merk_ilistf=first_ilist[nesting];
        if(last_ilist[nesting]) merk_ilistl=last_ilist[nesting];
    }
    if(nesting==1){
        if(merk_varf) gen_vars(merk_varf);
        if(first_llist) free_llist(first_llist);
        if(first_clist) free_clist(first_clist);
        if(merk_varf) free_var(merk_varf);
        if(merk_sif) free_si(merk_sif);
        if(merk_sdf) free_sd(merk_sdf);
        if(merk_ilistf) free_ilist(merk_ilistf);
    }
    if(nesting==0){
        if(first_var[0]) gen_vars(first_var[0]);
        if(first_var[0]) free_var(first_var[0]);
        if(first_sd[0]) free_sd(first_sd[0]);
        if(first_si[0]) free_si(first_si[0]);
        if(first_ilist[0]) free_ilist(first_ilist[0]);
    }
    nesting--;
}
void pra(FILE *f,struct argument_list *p)
/*  Gibt argument_list umgekehrt auf Bildschirm aus             */
{
    if(p->next){ pra(f,p->next);fprintf(f,",");}
    if(p->arg) pre(f,p->arg);
}
void pre(FILE *f,np p)
/*  Gibt expression auf Bildschirm aus                          */
{
    int c;
    c=p->flags;
    if(p->sidefx) fprintf(f,"/");
    if(p->lvalue) fprintf(f,"|");
    if(c==CALL){fprintf(f,"call-function(");pre(f,p->left);fprintf(f,")(");
                if(p->alist) pra(f,p->alist);
                fprintf(f,")");return;}
    if(c==CAST){fprintf(f,"cast(");pre(f,p->left);
                fprintf(f,"->");prd(f,p->ntyp);
                fprintf(f,")");return;}
    if(c==MEMBER){if(p->identifier) fprintf(f,".%s",p->identifier);return;}
    if(c==IDENTIFIER){if(p->identifier) fprintf(f,"%s",p->identifier);
        fprintf(f,"+");printval(f,&p->val,LONG,1); return;}
    fprintf(f,"%s(",ename[c]);
    if(p->left) pre(f,p->left);
    if(p->right){
        fprintf(f,",");
        pre(f,p->right);
    }
    fprintf(f,")");
    if(c==CEXPR||c==PCEXPR){fprintf(f,"(value="); printval(f,&p->val,p->ntyp->flags,1); fprintf(f,")");}
}
void printval(FILE *f,union atyps *p,int t,int verbose)
/*  Gibt atyps aus                                      */
{
    if(t==CHAR){if(verbose)fprintf(f,"C");vlong=zc2zl(p->vchar);printzl(f,vlong);}
    if(t==(UNSIGNED|CHAR)){if(verbose)fprintf(f,"UC");vulong=zuc2zul(p->vuchar);printzul(f,vulong);}
    if(t==SHORT){if(verbose)fprintf(f,"S");vlong=zs2zl(p->vshort);printzl(f,vlong);}
    if(t==(UNSIGNED|SHORT)){if(verbose) fprintf(f,"US");vulong=zus2zul(p->vushort);printzul(f,vulong);}
    if(t==FLOAT){if(verbose)fprintf(f,"F");vdouble=zf2zd(p->vfloat);printzd(f,vdouble);}
    if(t==DOUBLE){if(verbose)fprintf(f,"D");printzd(f,p->vdouble);}
    if(t==INT){if(verbose)fprintf(f,"I");vlong=zi2zl(p->vint);printzl(f,vlong);}
    if(t==LONG){if(verbose)fprintf(f,"L");printzl(f,p->vlong);}
    if(t==(UNSIGNED|INT)){if(verbose)fprintf(f,"UI");vulong=zui2zul(p->vuint);printzul(f,vulong);}
    if(t==(UNSIGNED|LONG)){if(verbose)fprintf(f,"UL");printzul(f,p->vulong);}
    /*  das hier ist nicht wirklich portabel    */
    if(t==POINTER){if(verbose)fprintf(f,"P");vulong=zp2zul(p->vpointer);printzul(f,vulong);}
}
void pric2(FILE *f,struct IC *p)
/*  Gibt ein IC aus */
{
    if(p->next&&p->next->prev!=p) {ierror(0);exit(EXIT_FAILURE);}
    if(p->code>=LABEL&&p->code<=BRA){
        if(p->code==LABEL) fprintf(f,"L%d",p->typf);
        else fprintf(f,"\t\t%s L%d",ename[p->code],p->typf);
    }else{
        fprintf(f,"\t\t%s ",ename[p->code]);
        if(p->typf&UNSIGNED) fprintf(f,"unsigned ");
        if(p->typf) fprintf(f,"%s ",typname[p->typf&15]);
        probj(f,&p->q1,p->typf,1);
        if(p->q2.flags){fprintf(f,",");probj(f,&p->q2,p->typf,1);}
        if(p->z.flags){fprintf(f,"->");probj(f,&p->z,p->typf,1);}
        if(p->code==ASSIGN||p->code==PUSH||p->code==POP) fprintf(f," size=%d",p->q2.reg);
    }
    fprintf(f,"\n");
}
void pric(FILE *f,struct IC *p)
/*  Gibt IC-Liste auf dem Bildschirm aus                */
{
    while(p){
        pric2(f,p);
        p=p->next;
    }
}

char *errstr[]={"","warning","error"};

void error(int errn,...)
/*  Behandelt Ausgaben wie Fehler und Meldungen */
{
    int type=0;
    va_list vl;
    if(err_out[errn].flags&DONTWARN) return;
    va_start(vl,errn);
    if(err_out[errn].flags&WARNING) type=1;
    if(err_out[errn].flags&ERROR) type=2;
    if(c_flags[7]&USEDFLAG){
        if(err_out[errn].flags&ANSIV) type=2; else type=0;
    }
    if(type){
        {if(*string==EOF) printf(">EOF\n"); else printf(">%s",string);}
        printf("%s %d in line %d of %s: ",errstr[type],errn,line,inname);
    }
    if(type||(err_out[errn].flags&MESSAGE)){
        vprintf(err_out[errn].text,vl);
        printf("\n");
    }
    va_end(vl);
    if(type==2){
        errors++;
        if(c_flags_val[8].l&&c_flags_val[8].l<=errors)
            {printf("Maximum number of errors reached!\n");raus();}
    }
    if(err_out[errn].flags&FATAL){printf("aborting...\n");raus();}
}
void printzl(FILE *f,zlong x)
/*  Konvertiert zlong nach ASCII                        */
/*  basiert noch einigermassen auf                      */
/*  Zweierkomplementdarstellung (d.h. -MIN>MAX)         */
/*  Ausserdem muss max(abs(long))<=max(unsigned long)   */
{
    zlong zl;zulong zul;
    zl=l2zl(0L);
    if(zlleq(x,zl)&&!zleq(x)){
        fprintf(f,"-");zl=zul2zl(t_max[LONG]);
        if(zlleq(x,zlneg(zl))&&!zleqto(x,zlneg(zl))){
        /*  aufpassen, da -x evtl. >LONG_MAX    */
            zul=t_max[LONG];
            x=zladd(x,zl);
        } else zul=ul2zul(0UL);
        x=zlneg(x);
        vulong=zl2zul(x);
        zul=zuladd(zul,vulong);
    }else zul=zl2zul(x);
    printzul(f,zul);
}
void printzul(FILE *f,zulong x)
/*  Konvertiert zulong nach ASCII                       */
{
    zulong zul;unsigned long l;
    if(DEBUG&64) printf("printzul:%lu\n",x);
    zul=ul2zul(10UL);
    if(!zuleq(zuldiv(x,zul))) printzul(f,zuldiv(x,zul));
    zul=zulmod(x,zul);l=zul2ul(zul);
    fprintf(f,"%c",(int)(l+'0'));
}
void printzd(FILE *f,zdouble x)
/*  Konvertiert zdouble nach ASCII, noch nicht fertig   */
{
    fprintf(f,"sorry, floating point not yet");
}
FILE *open_out(char *name,char *ext)
/*  Haengt ext an name an und versucht diese File als output zu oeffnen */
{
    char *s,*p;FILE *f;
    s=(char *)mymalloc(strlen(name)+strlen(ext)+1);
    strcpy(s,name);
    p=s+strlen(s);
    while(p>=s){
        if(*p=='.'){*p=0;break;}
        p--;
    }
    strcat(s,".");
    strcat(s,ext);
    f=fopen(s,"w");
    if(!f) printf("Couldn't open <%s> for output!\n",s);
    free(s);
    return(f);
}
void *mymalloc(size_t size)
/*  Belegt Speicher mit Abfrage     */
{
    void *p;
    if(!(p=malloc(size))){
        error(12);
        raus();
    }
    return(p);
}

