view conv/c2cbc.c @ 104:c21aeb12b78b

*** empty log message ***
author kono
date Mon, 17 Mar 2003 20:57:01 +0900
parents 0b068058dd67
children 756ba7c78e4c
line wrap: on
line source

#define EXTERN /**/

#include "mc.h"

static void open(char *);
static void print(char *);
static void close();
static void comment(char *s);

Converter c2cbc_converter = {
    &open,
    &print,
    &close,
    &comment,
};

static FILE *vout;

static void
comment(char *s)
{
}

static void cbcstruct_init(void);
static void init(void);
static void unlist(int e1);
static int unsym(int e1);

static FILE *vout;
static FILE *cbc_fp1;

static int  while_count=0;
static int  for_count=0;
static int  while_sign=0;
static int  for_sign=0;

#define SPSTACK_MAX 300
int cbc_spstack[SPSTACK_MAX];
static int    sp_free=0;

#define FUNBUF_MAX 1000
char *cbc_funstack[FUNBUF_MAX];
static int    fun_free=0;

static void
open(char *s)
{
    char *p=cheapp;
    while((*cheapp++ = *s++)) {
        if (*s=='.') {
            *cheapp++=*s++; *cheapp++='c';
            *cheapp++='b'; *cheapp++='c'; 
	    *cheapp++=0;
            break;
        }
    }
    vout = fopen(p,"w");
    if(!vout) error(-1);

    cheapp=p;
    while((*cheapp++ = *s++)) {
        if (*s=='.') {
            *cheapp++='-'; *cheapp++='s';
            *cheapp++=*s++; *cheapp++='h';
	    *cheapp++=0;
            break;
        }
    }
    cbc_fp1 = fopen(p,"w");
    if(!cbc_fp1) error(-1);

    cbcstruct_init();
    init();
    fprintf(vout,"#include \"%s\"\n",p);
    cheapp=p;
}

static void
print(char *s)
{
    fprintf(vout,"c: %s\n",s);
}

static void
close()
{
    fclose(vout);
}

static void 
cbcstruct_init(void)
{  
    fprintf(cbc_fp1,"typedef char *stack;\n");
    fprintf(cbc_fp1,"EXTERN stack sp;\n");
    fprintf(cbc_fp1,"struct cont_save {\n");
    fprintf(cbc_fp1,"    code (*ret)();\n");
    fprintf(cbc_fp1,"};\n");
}

static void 
init(void)
{
    while_count=0;
    for_count=0;
    while_sign=0;
    for_sign=0;
    sp_free=0;
    fun_free=0;
    cbc_funstack[fun_free++]="main";
}

static void 
unlist(int e1)
{
    int  e2,e3;
    e2=cadr(e1);
    e3=caddr(e1);
    switch(car(e1)){
    case  GT:
        unlist(e2);
        printf(">");
        unlist(e3);
        return;
    case  LT:
        unlist(e2);
        unsym(LT);
        unlist(e3);
        return;
    case  RLVAR:  case  LVAR:  case  GVAR:
        fprintf(vout,"%s",(char  *)e3);
        return;
    case  CONST:
        fprintf(vout,"%d",e2);
        return;
    case  POSTINC:
        if(e3==1){
            unlist(e2);
            fprintf(vout,"++");
            return;
        }
        else  return;
    case  CPOSTINC:
        unlist(e2);
        fprintf(vout,"++");
        return;
    default:
        return;
    }
}

static int 
unsym(int t) /*タイプをプリントする*/
{
    char *symbuf;
    switch(t){
    case -1: symbuf="int"; break;
    case -2: symbuf="char"; break;
    case -3: symbuf="unsigned"; break;
    case -4: symbuf="*"; break;
    case -6: symbuf="struct"; break;
    case -10: symbuf="static"; break;
    case -34: symbuf="long"; break;
    case -35: symbuf="short"; break;
    case -36: symbuf="extern"; break;
    case -37: symbuf="void"; break;
    case CODE: symbuf="code"; break;
    case MUL: symbuf="*"; break;
    case MUL+AS: symbuf="*="; break;
    case BAND: symbuf="&"; break;
    case BAND+AS: symbuf="&="; break;
    case LAND: symbuf="&&"; break;
    case SUB: symbuf="-"; break;
    case SUB+AS: symbuf="-="; break;
    case DEC: symbuf="--"; break;
    case ARROW: symbuf="->"; break;
    case LNOT: symbuf="!"; break;
    case NEQ: symbuf="!="; break;
    case BNOT: symbuf="~"; break;
    case ADD: symbuf="+"; break;
    case ADD+AS: symbuf="+="; break;
    case INC: symbuf="++"; break;
    case MOD: symbuf="%"; break;
    case MOD+AS: symbuf="%="; break;
    case EOR: symbuf="^"; break;
    case EOR+AS: symbuf="^="; break;
    case BOR: symbuf="|"; break;
    case BOR+AS: symbuf="||"; break;
    case ASS: symbuf="="; break;
    case EQ: symbuf="=="; break;
    case RSHIFT: symbuf=">>"; break;
    case RSHIFT+AS: symbuf=">>="; break;
    case GT: symbuf=">"; break;
    case GE: symbuf=">="; break;
    case LSHIFT: symbuf="<<"; break;
    case LSHIFT+AS: symbuf="<<="; break;
    case LT: symbuf="<"; break;
    case LE: symbuf="<="; break;
    case LPAR: symbuf="("; break;
    case RPAR: symbuf=")"; break;
    case LBRA: symbuf="["; break;
    case RBRA: symbuf="]"; break;
    case LC: symbuf="{"; break;
    case RC: symbuf="}"; break;
    case COMMA: symbuf=","; break;
    case COLON: symbuf=":"; break;
    case COND: symbuf="?"; break;
    case DOTS: symbuf="..."; break;
    case PERIOD: symbuf="."; break;
    case DIV: symbuf="/"; break;
    case DIV+AS: symbuf="/="; break;
    default:
      return sym;
    }
  if((mode==GDECL)&&((t==INT)||(t==CHAR))) 
    fprintf(vout,"code ");
  else 
    fprintf(vout,"%s",symbuf);
  if((mode!=GDECL)&&((t==INT)||(t==CHAR)||(t==-3)||(t==-4))) 
        fprintf(cbc_fp1,"    %s",symbuf);
  return sym;
}


/* end */