changeset 597:756ba7c78e4c

converter fix.
author kono
date Mon, 23 Jan 2006 14:09:31 +0900
parents 94d3a8c1b3e8
children e29ae2dab3c0
files conv/c.c conv/c2cbc.c conv/c2cbc.h
diffstat 3 files changed, 499 insertions(+), 194 deletions(-) [+]
line wrap: on
line diff
--- a/conv/c.c	Mon Jan 23 14:09:20 2006 +0900
+++ b/conv/c.c	Mon Jan 23 14:09:31 2006 +0900
@@ -3,6 +3,7 @@
 #include <stdio.h>
 #include "mc.h"
 #include "mc-parse.h"
+#include "mc-codegen.h"
 
 #include "conv/conv.h"
 #include "conv/convdef.h"
@@ -84,7 +85,7 @@
 	args=cadr(args);
 	if(args) fprintf(vout,",");
     }
-    fprintf(vout,")\n{");
+    fprintf(vout,")");
 }
 
 void
@@ -199,6 +200,7 @@
     args=n->dsp;
     while(args) {
 	n=(NMTBL *)caddr(args);
+	if (n==&str_ret) { args=cadr(args); continue; }
 	type_print(n->ty,n,vout);
 	args=cadr(args);
 	if(args) {
--- a/conv/c2cbc.c	Mon Jan 23 14:09:20 2006 +0900
+++ b/conv/c2cbc.c	Mon Jan 23 14:09:31 2006 +0900
@@ -1,224 +1,447 @@
 #define EXTERN /**/
 
+#include <stdio.h>
 #include "mc.h"
-
-static void open(char *);
-static void print(char *);
-static void close();
-static void comment(char *s);
+#include "mc-parse.h"
+#include "mc-codegen.h"
 
-Converter c2cbc_converter = {
-    &open,
-    &print,
-    &close,
-    &comment,
-};
+#include "conv/conv.h"
+#include "conv/convdef.h"
+#include "conv/c2cbc.h"
 
-static FILE *vout;
+extern void type_print(int type,NMTBL *nptr,FILE *out);
+extern void type_print1(int type,NMTBL *nptr,FILE *out,int cont);
+extern void sym_print(int type,FILE *out);
+
+static FILE *vout,*svout;
+static FILE *null;
+static FILE *hout;
 
 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)
+comment_(int c)
 {
-    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;
+    fprintf(vout,"%c",c);
 }
 
 static void
-print(char *s)
+open_(char *s)
+{
+    char *p;
+    p = make_filename_with_extension(s,"cbc.c");
+#if 1
+    vout = fopen(p,"w");
+#else
+    vout = stderr;p=p;
+#endif
+    p = make_filename_with_extension(s,"cbc.h");
+    hout = fopen(p,"w");
+    null=fopen("/dev/null","w");
+    if (!vout) error(-1);
+}
+
+static void
+conv_()
+{
+    vout=svout;
+}
+
+static void
+noconv_(int c)
+{
+    svout=vout;
+    vout=c?null:vout;
+}
+
+static void
+print_(char *s)
 {
     fprintf(vout,"c: %s\n",s);
 }
 
 static void
-close()
+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");
+void
+case_begin_(int cases,int def){
+    fprintf(vout,"case ");
+}
+
+void
+case_(int cases,int def){
+    if (def)
+	fprintf(vout,"default:");
+    else
+	fprintf(vout,"%d:",car(cases));
+}
+
+void
+code_(NMTBL *n){
+    int args;
+    args=n->dsp;
+    fprintf(vout,"code %s(",n->nm);
+    while(args) {
+	n=(NMTBL *)caddr(args);
+	type_print(n->ty,n,vout);
+	args=cadr(args);
+	if(args) fprintf(vout,",");
+    }
+    fprintf(vout,")");
+}
+
+void
+code_end_(){
+    fprintf(vout,"}");
+}
+
+void
+comma_(){
+    fprintf(vout,",");
+}
+
+void
+cond_(){
+
 }
 
-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";
+void
+cond1_(){
+    fprintf(vout,"?");
+}
+
+void
+cond2_(){
+    fprintf(vout,":");
+}
+
+void
+cond_end_(){
+
+}
+
+void
+decl_data_begin_(){
+
+}
+
+void
+decl_data_end_(){
+
+}
+
+void
+def_(NMTBL *n){
+}
+
+void
+dowhile_(){
+    fprintf(vout,"do");
+}
+
+void
+dowhile_cond_(){
+    fprintf(vout,"while(");
+}
+
+void
+dowhile_end_(){
+    fprintf(vout,");");
+}
+
+int
+error_(int n){
+    return 0;
 }
 
-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;
+void
+extern_(){
+    fprintf(vout,"extern ");
+}
+
+void
+for_(){
+    fprintf(vout,"for(");
+}
+
+void
+for1_(){
+    fprintf(vout,";");
+}
+
+void
+for2_(){
+    fprintf(vout,";");
+}
+
+void
+for_body_(){
+    fprintf(vout,")");
+}
+
+void
+for_end_(){
+
+}
+
+void
+funcall_(int type){
+    fprintf(vout,"(");
+}
+
+void
+funcall_args_(){
+    fprintf(vout,")");
+}
+
+void
+function_(NMTBL *n,int cont){
+    int args;
+    type_print1(cadr(n->ty),0,vout,cont);
+    fprintf(vout,"%s(",n->nm);
+    args=n->dsp;
+    while(args) {
+	n=(NMTBL *)caddr(args);
+	if (n==&str_ret) {
+	    args=cadr(args); continue;
+	}
+	type_print(n->ty,n,vout);
+	args=cadr(args);
+	if(args) {
+	    /* if(caddr(args)==(int)&str_ret) break; */
+	    fprintf(vout,",");
+	}
     }
+    fprintf(vout,")");
+}
+
+void
+function_end_(){
+
+}
+
+void
+goto_(){
+    fprintf(vout,"goto ");
+}
+
+void
+goto_label_(){
+
+}
+
+void
+id_(int sym,NMTBL *nptr){
+    char *p = nptr->nm;
+    for(;*p && *p!='.';p++) fputc(*p,vout);
+}
+
+void
+string_(char *s,int len){
+    int c;
+    fprintf(vout,"\"");
+    while((c=*s++)) {
+	if(c=='\n') fprintf(vout,"\\n");
+	else if(c=='\r') fprintf(vout,"\\r");
+	else if(c=='\t') fprintf(vout,"\\t");
+	else if(c=='\e') fprintf(vout,"\\e");
+	else if(c=='"') fprintf(vout,"\\\"");
+	else if(c=='\\') fprintf(vout,"\\\\");
+	else if(!(' '<=c&&c<=0x7f)) fprintf(vout,"\\%03o",c);
+	else fprintf(vout,"%c",c);
+    }
+    fprintf(vout,"\"");
 }
 
-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;
+void
+const_(int symval){
+    fprintf(vout,"%d",symval);
+}
+
+void
+return_f_(){
+    fprintf(vout,"return");
+}
+
+void
+defined_(char *s){
+    fprintf(vout,"defined(%s)",s);
+}
+
+void
+environment_(){
+    fprintf(vout,"environment");
+}
+
+void
+if_(){
+    fprintf(vout,"if (");
+}
+
+void
+if_else_(){
+    fprintf(vout," else ");
+}
+
+void
+if_endif_(){
+}
+
+void
+if_then_(){
+    fprintf(vout,")");
+}
+
+void
+jump_(int env){
+
+}
+
+void
+label_(){
+    fprintf(vout,"%s:",nptr->nm);
+}
+
+void
+lpar_(){
+    fprintf(vout,"(");
+}
+
+void
+rpar_(){
+    fprintf(vout,")");
+}
+
+void
+lbra_(int sym){
+    fprintf(vout,"[");
+}
+
+void
+rbra_(int sym){
+    fprintf(vout,"]");
+}
+
+void
+lc_(){
+    fprintf(vout,"{");
+}
+
+void
+rc_(){
+    fprintf(vout,"}");
+}
+
+void
+localvar_end_(){
+
+}
+
+void
+op_(int sym){
+    sym_print(sym,vout);
+}
+
+void
+postfix_(int sym){
+    sym_print(sym,vout);
 }
 
+void
+prefix_(int sym){
+    sym_print(sym,vout);
+}
+
+void
+register_(){
+    fprintf(vout,"register ");
+}
+
+void
+return_(){
+    fprintf(vout,"return ");
+}
+
+void
+return_end_(){
+    fprintf(vout,";");
+}
+
+void
+return_type_(int t,NMTBL *nptr,int cont){
+    type_print1(t,nptr,vout,cont);
+}
+
+void
+sm_(){
+    fprintf(vout,";");
+}
+
+void
+static_(){
+    fprintf(vout,"static ");
+}
+
+void
+switch_(){
+    fprintf(vout,"switch (");
+}
+
+void
+switch_body_(){
+    fprintf(vout,")");
+}
+
+void
+switch_end_(){
+}
+
+void
+typedef_(){
+    fprintf(vout,"typedef ");
+}
+
+void
+type_(int t){
+    type_print(t,0,vout);
+}
+
+void
+sdecl_(int s)
+{
+    fprintf(vout,s==UNION?"union ":"struct ");
+}
+
+void
+while_(){
+    fprintf(vout,"while (");
+}
+
+void
+while_body_(){
+    fprintf(vout,")");
+}
+
+void
+while_end_(){
+}
+
+void
+decl_data_()
+{
+}
+
+void
+break_()
+{
+    fprintf(vout,"break;");
+}
+
+void
+continue_()
+{
+    fprintf(vout,"continue;");
+}
 
 /* end */
--- a/conv/c2cbc.h	Mon Jan 23 14:09:20 2006 +0900
+++ b/conv/c2cbc.h	Mon Jan 23 14:09:31 2006 +0900
@@ -1,2 +1,82 @@
+/* Do not edit this file. This is automatically generated. */
 
 extern Converter c2cbc_converter;
+
+
+
+Converter c2cbc_converter = {
+    &conv_,
+    &noconv_,
+    &comment_,
+    &open_,
+    &print_,
+    &close_,
+    &case_,
+    &case_begin_,
+    &code_,
+    &code_end_,
+    &comma_,
+    &cond_,
+    &cond1_,
+    &cond2_,
+    &cond_end_,
+    &decl_data_begin_,
+    &decl_data_end_,
+    &def_,
+    &dowhile_,
+    &dowhile_cond_,
+    &dowhile_end_,
+    &error_,
+    &extern_,
+    &for_,
+    &for1_,
+    &for2_,
+    &for_body_,
+    &for_end_,
+    &funcall_,
+    &funcall_args_,
+    &function_,
+    &function_end_,
+    &goto_,
+    &goto_label_,
+    &id_,
+    &string_,
+    &const_,
+    &return_f_,
+    &defined_,
+    &environment_,
+    &if_,
+    &if_else_,
+    &if_endif_,
+    &if_then_,
+    &jump_,
+    &label_,
+    &lbra_,
+    &lc_,
+    &localvar_end_,
+    &lpar_,
+    &op_,
+    &postfix_,
+    &prefix_,
+    &rbra_,
+    &rc_,
+    &register_,
+    &return_,
+    &return_end_,
+    &return_type_,
+    &rpar_,
+    &sm_,
+    &static_,
+    &switch_,
+    &switch_body_,
+    &switch_end_,
+    &typedef_,
+    &type_,
+    &while_,
+    &while_body_,
+    &while_end_,
+    &decl_data_,
+    &break_,
+    &continue_,
+    &sdecl_,
+};