changeset 562:0a156c491f81

*** empty log message ***
author kono
date Mon, 09 Jan 2006 20:06:02 +0900
parents de0b0380c461
children 588bb765b301
files Changes Makefile.ia32 mc-parse.c mc-tree.c
diffstat 4 files changed, 281 insertions(+), 133 deletions(-) [+]
line wrap: on
line diff
--- a/Changes	Sat Jan 07 19:46:27 2006 +0900
+++ b/Changes	Mon Jan 09 20:06:02 2006 +0900
@@ -7997,3 +7997,131 @@
 
 なんか、macro の history のcheckをしてないな。
 
+
+Sat Jan  7 20:34:12 JST 2006
+
+
+しかし、ここまで作っておいてなんだけど...
+
+    function(arg)
+    {
+	interface *arg;
+	calling_function(arg)
+    }
+
+でもいいんじゃない? だめ? レジスタ渡しのargがないけどね。
+
+
+extern printf(const char *,...);
+
+struct interface
+{
+    int a;
+    int b;
+};
+
+code f(struct interface a);
+extern code h(struct interface a);
+
+code g(struct interface a)
+{
+    goto h(a);
+}
+
+
+code f(struct interface a)
+{
+    if (a.b) {
+	printf("%d\n",a.a);
+	goto f(a);
+    } else {
+	goto g(a);
+    }
+}
+
+これを以下のように変換するわけだ。
+
+    struct interface
+    {
+	int a;
+	int b;
+    };
+
+    void f(struct interface *a);
+    extern void h(struct interface *a);
+
+    void g(struct interface *a)
+    {
+	h(a);
+    }
+
+
+    void f(struct interface *a)
+    {
+	if (a->b) {
+	    f(a);
+	} else {
+	    g(a);
+	}
+    }
+
+
+	.globl _g
+_g:
+	b L_h$stub
+	.align 2
+	.globl _f
+_f:
+	lwz r0,4(r3)
+	cmpwi cr7,r0,0
+	beq+ cr7,L5
+L8:
+	b L8
+L5:
+	b _g
+
+結構、ちゃんとでるじゃん。でも、printf を混ぜたりすると、結
+構、わけわからんコードが出るね。戻って来るっていう前提があ
+るから、やっぱりレジスタのsaveがきついんだよな。
+
+レジスタの引数渡しは絶対にでないけどね。(とは限らないか?)
+
+引数をすべて同じにするというのが肝なのか。
+
+でも、これだったら比較的簡単にCに変換できるんじゃないか?
+    CbC to C
+ってわけだね。
+
+そういう意味では一種の「プログラミングスタイル」なんだよな。
+
+さすがに、構造体を引数にすると、こんなに簡単なコードは
+出ないみたいだね。ということはポインタにせざるを得ない
+のか。
+
+スタック関係のコードを抑圧出来れば、mc でも悪くないか。
+
+つまり、gcc で、
+    tail recursion しか「無理に」しない制約 (__code)
+を導入して、それを実現するcallを
+    __goto f(hoge);
+にしてやれば良いわけだ。
+
+あと、構造体をコピーしないモードだね。call semantics が違うから
+これは必要なのか。pointer でもいいんだけどさ。違うのはここか。
+
+4.0なら、比較的簡単に実現できるのではないか?
+
+pointer の方がinterfaceの切替えも楽だし。
+
+Mon Jan  9 19:29:06 JST 2006
+
+なんか、-E も -Cc も「ぜんぜん」動きません。mc-tree.c が
+ぼろぼろだからだな。 まぁ、そうだよな。
+
+ここら辺がconverterにするつもりだったんだけど、簡単には出来ない
+かも。まぁ、今は、inline mode があるから、tree を作ってから
+印刷するっていう技もあるけどね。
+
+
+
+
--- a/Makefile.ia32	Sat Jan 07 19:46:27 2006 +0900
+++ b/Makefile.ia32	Mon Jan 09 20:06:02 2006 +0900
@@ -12,7 +12,7 @@
 MFLAGS=$(MFALGS) BASE=$(BASE) STAGE=$(STAGE)
 ARCH=ia32
 MC=mc-$(ARCH)
-# MLIB = -lm
+MLIB = -lm
 PRINTF= # printf.c
 CONVERTER=conv/c.o conv/null.o
 # conv/c2cbc.o conv/cbc2c.o
--- a/mc-parse.c	Sat Jan 07 19:46:27 2006 +0900
+++ b/mc-parse.c	Mon Jan 09 20:06:02 2006 +0900
@@ -165,6 +165,8 @@
 static void top_init();
 static void qualifiers();
 static void attributes();
+static void macro_convert();
+extern void sym_print(int,FILE *);
 
 // current value of constant symbol
 
@@ -218,6 +220,10 @@
 	case 'c':
 	    chk = 1;
 	    break;
+	case 'E':
+	    ac2++;
+	    macro_convert();
+	    break;
 	case 'd':
 	    debug = 1;
 	    break;
@@ -282,6 +288,20 @@
     leave_scope();
 }
 
+static void
+macro_convert()
+{
+    init();
+    top_init();
+    while(getsym(0)) {
+	if (sym==IDENT) {
+	    printf("%s",nptr->nm);
+	} else
+	    sym_print(sym,stdout);
+    }
+    /* NOT REACHED */
+}
+
 /*
     error handler
     when EOF, process next file
@@ -2055,9 +2075,9 @@
 	    //  ({... ; value}) requires delayed handling of lastexp.
 	    //  lastexp will be flushed in checkret(), or used by
 	    //  statement expression.
-	    if (use) 
+	    if (use) {
 		lastexp = expr(0);
-	    if (inmode) {
+	    } else if (inmode) {
 		e = expr(0);
 		parse = list3(ST_COMP,parse,e);
 	    } else {
--- a/mc-tree.c	Sat Jan 07 19:46:27 2006 +0900
+++ b/mc-tree.c	Mon Jan 09 20:06:02 2006 +0900
@@ -24,137 +24,131 @@
 
 static
 tree_node_type tree_nodes[] = {
-    {(-45),"...",""},
-    {(-44),"lmacro",""},
-    {(-43),"fmacro",""},
-    {(-42),"konst",""},
-    {(-41),"defined",""},
-    {(-40),"environment",""},
-    {(-39),"code",""},
-    {(-38),"register",""},
-    {(-37),"void",""},
-    {(-36),"extern",""},
-    {(-35),"short",""},
-    {(-34),"long",""},
-    {(-33),"type",""},
-    {(-32),"sizeof",""},
-    {(-31),"typedef",""},
-    {(-30),"flabel",""},
-    {(-29),"blabel",""},
-    {(-28),"macro",""},
-    {(-27),"string",""},
-    {(-26),"ident",""},
-    {(-25),"field",""},
-    {(-24),"tag",""},
-    {(-23),"reserve",""},
-    {(-22),"default",""},
-    {(-21),"case",""},
-    {(-20),"switch",""},
-    {(-19),"while",""},
-    {(-18),"do",""},
-    {(-17),"for",""},
-    {(-16),"else",""},
-    {(-15),"if",""},
-    {(-14),"continue",""},
-    {(-13),"break",""},
-    {(-12),"return",""},
-    {(-11),"goto",""},
-    {(-10),"static",""},
-    {(-9),"empty",""},
-    {(-8),"function","t"},
-    {(-7),"union",""},
-    {(-6),"struct","vt"},
-    {(-5),"array","tv"},
-    {(-4),"pointer","t"},
-    {(-3),"unsigned",""},
-    {(-2),"char",""},
-    {(-1),"int",""},
+    {DOTS,"...",""},
+    {LMACRO,"lmacro",""},
+    {FMACRO,"fmacro",""},
+    {KONST,"const",""},
+    {DEFINED,"defined",""},
+    {ENVIRONMENT,"environment",""},
+    {CODE,"code",""},
+    {REGISTER,"register",""},
+    {VOID,"void",""},
+    {EXTRN,"extern",""},
+    {SHORT,"short",""},
+    {LONG,"long",""},
+    {TYPE,"type",""},
+    {SIZEOF,"sizeof",""},
+    {TYPEDEF,"typedef",""},
+    {FLABEL,"flabel",""},
+    {BLABEL,"blabel",""},
+    {MACRO,"macro",""},
+    {STRING,"string",""},
+    {IDENT,"ident",""},
+    {FIELD,"field",""},
+    {TAG,"tag",""},
+    {RESERVE,"reserve",""},
+    {DEFAULT,"default",""},
+    {CASE,"case",""},
+    {SWITCH,"switch",""},
+    {WHILE,"while",""},
+    {DO,"do",""},
+    {FOR,"for",""},
+    {ELSE,"else",""},
+    {IF,"if",""},
+    {CONTINUE,"continue",""},
+    {BREAK,"break",""},
+    {RETURN,"return",""},
+    {GOTO,"goto",""},
+    {STATIC,"static",""},
+    {EMPTY,"empty",""},
+    {FUNCTION,"function","t"},
+    {UNION,"union",""},
+    {STRUCT,"struct","vt"},
+    {ARRAY,"array","tv"},
+    {POINTER,"pointer","t"},
+    {UNSIGNED,"unsigned",""},
+    {CHAR,"char",""},
+    {INT,"int",""},
 
-    {1,"gvar","vs"},
-    {2,"rgvar","vs"},
-    {3,"crgvar","vs"},
-    {4,"lvar","v"},
-    {5,"rlvar","v"},
-    {6,"crlvar","v"},
-    {7,"const","v"},
-    {8,"fname","n"},
-    {9,"*","e"},
-    {10,"rindirect","e"},
-    {11,"crindirect","e"},
-    {12,"&","e"},
-    {13,"-","e"},
-    {14,"!","e"},
-    {15,"~","e"},
-    {16,"++",""},
-    {17,"postinc","e"},
-    {18,"preinc","e"},
-    {19,"cpostinc","e"},
-    {20,"cpreinc","e"},
-    {21,"--",""},
-    {22,"cpostdec","e"},
-    {23,"cpredec","e"},
-    {24,"*","ee"},
-    {25,"*","ee"},
-    {26,"/","ee"},
-    {27,"/","ee"},
-    {28,"%","ee"},
-    {29,"%","ee"},
-    {30,"+","ee"},
-    {31,"-","ee"},
-    {32,">>","ee"},
-    {33,">>","ee"},
-    {34,"<<","ee"},
-    {35,"<<","ee"},
-    {36,">","ee"},
-    {37,">","ee"},
-    {38,">=","ee"},
-    {39,">=","ee"},
-    {40,"<","ee"},
-    {41,"<","ee"},
-    {42,"<=","ee"},
-    {43,"<=","ee"},
-    {44,"==","ee"},
-    {45,"!=","ee"},
-    {46,"&","ee"},
-    {47,"^","ee"},
-    {48,"|","ee"},
-    {49,"&&","ee"},
-    {50,"||","ee"},
-    {51,"cond","eee"},
-    {52," = ","ee"},
-    {53," = ","ee"},
-    {54,"assop","eev"},
-    {55,"cassop","eev"},
-    {56,",","ee"},
-    {57,"(",""},
-    {58,")",""},
-    {59,"[",""},
-    {60,"]",""},
-    {61,"{",""},
-    {62,"}",""},
-    {63,":","ee"},
-    {64,";",""},
-    {65,".",""},
-    {66,"->",""},
-    {67,"cname",""},
-    {68,"sass",""},
-    {69,"rstruct",""},
-    {00,"=",""},
-    {AS+24,"*=","ee"},
-    {AS+25,"*=","ee"},
-    {AS+26,"/=","ee"},
-    {AS+27,"/=","ee"},
-    {AS+28,"%=","ee"},
-    {AS+29,"%=","ee"},
-    {AS+30,"+=","ee"},
-    {AS+31,"-=","ee"},
-    {AS+32,">>=","ee"},
-    {AS+33,">>=","ee"},
-    {AS+34,"<<=","ee"},
-    {AS+35,"<<=","ee"},
-    {AS+46,"&=","ee"},
-    {AS+47,"^=","ee"},
-    {AS+48,"|=","ee"},
+    {GVAR,"gvar","vs"},
+    {RGVAR,"rgvar","vs"},
+    {CRGVAR,"crgvar","vs"},
+    {LVAR,"lvar","v"},
+    {RLVAR,"rlvar","v"},
+    {CRLVAR,"crlvar","v"},
+    {CONST,"const","v"},
+    {FNAME,"fname","n"},
+    {MUL,"*","e"},
+    {RINDIRECT,"rindirect","e"},
+    {CRINDIRECT,"crindirect","e"},
+    {BAND,"&","e"},
+    {MINUS,"-","e"},
+    {LNOT,"!","e"},
+    {BNOT,"~","e"},
+    {INC,"++",""},
+    {POSTINC,"postinc","e"},
+    {PREINC,"preinc","e"},
+    {CPOSTINC,"cpostinc","e"},
+    {CPREINC,"cpreinc","e"},
+    {DEC,"--",""},
+    {DIV,"/","ee"},
+    {UDIV,"/","ee"},
+    {MUL,"*","ee"},
+    {UMUL,"*","ee"},
+    {MOD,"%","ee"},
+    {UMOD,"%","ee"},
+    {ADD,"+","ee"},
+    {SUB,"-","ee"},
+    {RSHIFT,">>","ee"},
+    {URSHIFT,">>","ee"},
+    {LSHIFT,"<<","ee"},
+    {ULSHIFT,"<<","ee"},
+    {GT,">","ee"},
+    {UGT,">","ee"},
+    {GE,">=","ee"},
+    {UGE,">=","ee"},
+    {LT,"<","ee"},
+    {ULT,"<","ee"},
+    {LE,"<=","ee"},
+    {ULE,"<=","ee"},
+    {EQ,"==","ee"},
+    {NEQ,"!=","ee"},
+    {BAND,"&","ee"},
+    {EOR,"^","ee"},
+    {BOR,"|","ee"},
+    {LAND,"&&","ee"},
+    {LOR,"||","ee"},
+    {COND,"cond","eee"},
+    {ASSOP,"assop","eev"},
+    {CASSOP,"cassop","eev"},
+    {COMMA,",","ee"},
+    {LPAR,"(",""},
+    {RPAR,")",""},
+    {LBRA,"[",""},
+    {RBRA,"]",""},
+    {LC,"{",""},
+    {RC,"}",""},
+    {COLON,":","ee"},
+    {SM,";",""},
+    {PERIOD,".",""},
+    {ARROW,"->",""},
+    {SASS,"sass",""},
+    {RSTRUCT,"rstruct",""},
+    {AS+MUL,"*=","ee"},
+    {AS+UMUL,"*=","ee"},
+    {AS+DIV,"/=","ee"},
+    {AS+UDIV,"/=","ee"},
+    {AS+MOD,"%=","ee"},
+    {AS+UMOD,"%=","ee"},
+    {AS+ADD,"+=","ee"},
+    {AS+MINUS,"-=","ee"},
+    {AS+RSHIFT,">>=","ee"},
+    {AS+URSHIFT,">>=","ee"},
+    {AS+LSHIFT,"<<=","ee"},
+    {AS+ULSHIFT,"<<=","ee"},
+    {AS+BAND,"&=","ee"},
+    {AS+EOR,"^=","ee"},
+    {AS+BOR,"|=","ee"},
 };
 
 void
@@ -229,6 +223,7 @@
     int last=sizeof(tree_nodes)/sizeof(tree_node_type);
     e2=-1;
     while (first!=last) {
+#if 0
 	e1 = (first+last)/2;
 	if(e2==e1) 
 	    return 0;
@@ -239,6 +234,11 @@
 	    break;
 	else if (tree_nodes[e1].tree_type<e)
 	    first = e1;
+#else
+	if (tree_nodes[first].tree_type==e)
+	    break;
+	first++;
+#endif
     }
     return &tree_nodes[e1];
 }