Update of /cvsroot/monetdb/MonetDB5/src/compiler
In directory sc8-pr-cvs16.sourceforge.net:/tmp/cvs-serv19378/compiler

Modified Files:
        mal_compiler.mx 
Log Message:
A few patches for the mal compiler


Index: mal_compiler.mx
===================================================================
RCS file: /cvsroot/monetdb/MonetDB5/src/compiler/mal_compiler.mx,v
retrieving revision 1.16
retrieving revision 1.17
diff -u -d -r1.16 -r1.17
--- mal_compiler.mx     1 Jun 2007 06:48:27 -0000       1.16
+++ mal_compiler.mx     4 Oct 2007 20:11:09 -0000       1.17
@@ -17,16 +17,16 @@
 @f mal_compiler
 @a M. L. Kersten
 @+ MAL compilation
-One of the last resorts to gain performance is to
-compile the MAL into C, followed by compilation
-and dynamic linkage.
-The facility is offered as one of the optimization
-steps.
+Compilation of a bunch of MAL procedures can be used
+to remove the overhead of the interpreter and reduce
+the footprint.
+It is particularly useful in embedded and static
+applications.
 
-The current translation scheme is chosen for
+The current translation scheme is choosen for
 simplicity and is particularly aimed at
-removing the MAL interpreter overhead.
-To best result is most likely obtained after
+removing the MAL interpreter overhead only.
+The best result is most likely obtained after
 the other MAL optimizers have performed their job.
 This may involve code in-lining steps specifically
 geared at producing C later on.
@@ -37,14 +37,15 @@
 
 @example
 function tst903();
-    b:= new(:void,:lng);
-    barrier (go,i):= newRange(0:lng);
-        k:= rand();
-        l:= lng(k);
-        insert(b,nil,l);
-        redo (go,i):= nextElement(1:lng,1000000:lng);
+    b:= bat.new(:void,:lng);
+    barrier (go,i):= language.newRange(0:lng);
+        k:= mmath.rand();
+        l:= calc.lng(k);
+        bat.insert(b,nil,l);
+        redo (go,i):= language.nextElement(1:lng,1000000:lng);
     exit (go,i);
 end tst903;
+mal.compiler("user","tst903","mal01");
 @end example
 
 The C code is derived in a few steps. First the variables
@@ -58,29 +59,30 @@
 Of course the resulting code is not optimal, e.g.
 the following portion for the example is produced:
 @example
-str MCC_Admin_tst903(void *tst903)
+str MCCuser_tst903(void *tst903)
 {
-        BAT *b= 0; /* bat[:void,:lng] */
-        void T2 = 0;
-        lng T3 = 0;
+        BID *b= 0; /* bat[:void,:lng] */
+        void *V2= 0; /* void */
+        lng V3 = 0;
         bit go = 0;
         lng i = 0;
-        lng T6 = 0;
+        lng V6 = 0;
         int k = 0;
         lng l = 0;
-        void T10 = nil;
-        lng T11 = 1;
-        lng T12 = 1000000;
-
-        if( Xmsg = CMDBATnew(&b,&T2,&T3) ) return Xmsg;
-        if( Xmsg = RNGnewRange_lng(&go,&i,&T6) ) return Xmsg;
-        if( !( go == 0 ||go== bit_nil) ) goto EXIT_7;
+        void *V9= 0; /* void */
+        void *V10= 0; /* void */
+        lng V11 = 1;
+        lng V12 = 1000000;
+        str Xmsg = MAL_SUCCEED;
+        if( Xmsg = CMDBATnew(&b,&V2,&V3) ) return Xmsg;
+        if( Xmsg = RNGnewRange_lng(&go,&i,&V6) ) return Xmsg;
+        if(  go == 0 ||go== bit_nil ) goto EXIT_7;
 BARRIER_3:
-        if( Xmsg = CALCrandint(&k) ) return Xmsg;
+        if( Xmsg = MATHrandint(&k) ) return Xmsg;
         if( Xmsg = CALCint2lng(&l,&k) ) return Xmsg;
-        if( Xmsg = BKCinsert_bun(&T9,&b,&T10,&l) ) return Xmsg;
-        if( Xmsg = RNGnextElement_lng(&go,&i,&T11,&T12) ) return Xmsg;
-        if(  go == 0 ||go== bit_nil ) goto BARRIER_3;
+        if( Xmsg = BKCinsert_bun(&V9,&b,&V10,&l) ) return Xmsg;
+        if( Xmsg = RNGnextElement_lng(&go,&i,&V11,&V12) ) return Xmsg;
+        if( !( go == 0 ||go== bit_nil) ) goto BARRIER_3;
 EXIT_7: ;
 }
 @end example
@@ -98,7 +100,7 @@
 executable.
 @mal
 module compiler;
-pattern MALtoC(mod:str, fcn:str):void
+pattern MALtoC(mod:str, fcn:str, alias:str):void
 address MCdynamicCompiler
 comment "MAL to C compiler for functions";
 @{
@@ -134,13 +136,12 @@
        "/* MAL to C compiler\n",
        "   Copyright (c) 2001-2007, CWI.\n",
        "   All rights reserved.\n",
-       " \n",
-       "   The current version does not yet\n",
-       "   support THREAD blocks\n",
        "*/\n",
+       "#include \"mal_config.h\"\n",
        "#include \"mal.h\"\n",
+       "#include \"mal_interpreter.h\"\n",
        "#include \"mal_function.h\"\n",
-       "#define BAT int\n",    /* the interface works on BAT ids */
+       "#define BID int\n",    /* the interface works on BAT ids */
        0
 };
 void
@@ -148,7 +149,6 @@
 {
        if (isTmpVar(mb, i))
                stream_printf(f, "T%d", mb->var[i]->tmpindex);
-
        else
                stream_printf(f, "%s", mb->var[i]->name);
 }
@@ -191,7 +191,7 @@
        if (strcmp(tpe, "void") == 0) {
                stream_printf(f, "void *");
        } else if (isaBatType(getVarType(mb, i))) {
-               stream_printf(f, "BAT *");
+               stream_printf(f, "int *");
        } else {
                stream_printf(f, "%s *", tpe);
        }
@@ -236,7 +236,7 @@
                        mccVar(f, mb, i);
                        stream_printf(f, "= 0; /* %s */\n", tpe);
                } else if (isaBatType(getVarType(mb, i))) {
-                       stream_printf(f, "\tBAT *");
+                       stream_printf(f, "\tBID *");
                        mccVar(f, mb, i);
                        stream_printf(f, "= 0; /* %s */\n", tpe);
                } else {
@@ -315,6 +315,24 @@
 CATCH blocks. If they do not exist, then any exception
 terminates the function. Otherwise, we jump to the first one.
 @c
+void
+mccCall(stream *f, MalBlkPtr mb, InstrPtr p, int *catch, int *ctop){
+       int j;
+       if (p->blk && p->blk->binding) {
+               stream_printf(f, "\tif( Xmsg = ");
+               stream_printf(f, "%s(", p->blk->binding);
+               mccArg(f, mb, getArg(p, 0));
+               for (j = 1; j < p->argc; j++) {
+                       stream_printf(f, ",");
+                       mccArg(f, mb, getArg(p, j));
+               }
+               stream_printf(f, ") )");
+               if (*ctop > 0)
+                       stream_printf(f, " goto CATCH_%d;\n", catch[--*ctop]);
+               else
+                       stream_printf(f, " return Xmsg;\n");
+       }
+}
 int
 mccInstruction(stream *f, MalBlkPtr mb, InstrPtr p, int i, int *catch, int 
*ctop)
 {
@@ -332,20 +350,7 @@
                return errors;
        }
 
-       if (p->blk && p->blk->binding) {
-               stream_printf(f, "\tif( Xmsg = ");
-               stream_printf(f, "%s(", p->blk->binding);
-               mccArg(f, mb, getArg(p, 0));
-               for (j = 1; j < p->argc; j++) {
-                       stream_printf(f, ",");
-                       mccArg(f, mb, getArg(p, j));
-               }
-               stream_printf(f, ") )");
-               if (*ctop > 0)
-                       stream_printf(f, " goto CATCH_%d;\n", catch[--*ctop]);
-               else
-                       stream_printf(f, " return Xmsg;\n");
-       }
+       mccCall(f,mb,p,catch,ctop);
        if (p->barrier)
                switch (p->barrier) {
                case BARRIERsymbol:
@@ -409,6 +414,39 @@
 }
 
 void
+mccJoinPath(stream *f, MalBlkPtr mb, InstrPtr p){
+       int i;
+       (void)mb;
+       stream_printf(f,"\t{ BID *j%d[]={",getArg(p,0));
+       mccArg(f, mb, getArg(p, p->retc));
+       for(i=p->retc+1;i<p->argc; i++){
+               stream_printf(f,",");
+               mccArg(f, mb, getArg(p, i));
+       }
+       stream_printf(f,"};\t");
+       mccVar(f, mb, getArg(p,0));
+       stream_printf(f,"= ALGjoinPathBody(%d,&j%d); ", p->argc-p->retc, 
getArg(p,0));
+       stream_printf(f,"};\n");
+}
+
+void
+mccProject(stream *f, MalBlkPtr mb, InstrPtr p, int *catch, int *ctop){
+       int j;
+       stream_printf(f, "\tif( Xmsg = ALGprojectCstBody(");
+       mccArg(f, mb, getArg(p, 0));
+       for (j = 1; j < p->argc; j++) {
+               stream_printf(f, ",");
+               mccArg(f, mb, getArg(p, j));
+       }
+       stream_printf(f,",%d",getArgType(mb,p,2));
+       stream_printf(f, ") )");
+       if (*ctop > 0)
+               stream_printf(f, " goto CATCH_%d;\n", catch[--*ctop]);
+       else
+               stream_printf(f, " return Xmsg;\n");
+}
+
+void
 mccBody(stream *f, MalBlkPtr mb)
 {
        int i;
@@ -440,6 +478,18 @@
                                mccMultiplex(f, mb, p);
                                continue;
                        }
+                       if (getModuleId(p) && getFunctionId(p) && 
+                               idcmp(getModuleId(p), "algebra") == 0 && 
+                               idcmp(getFunctionId(p), "joinPath") == 0) {
+                               mccJoinPath(f, mb, p);
+                               continue;
+                       }
+                       if (getModuleId(p) && getFunctionId(p) && 
+                               idcmp(getModuleId(p), "algebra") == 0 && 
+                               idcmp(getFunctionId(p), "project") == 0) {
+                               mccProject(f, mb, p, catch, &ctop);
+                               continue;
+                       }
                        GDKwarning("call to %s.%s can not be handled 
correctly\n", 
                                getModuleId(p), getFunctionId(p));
                        errors++;
@@ -463,13 +513,12 @@
 @c
 
 str
-mccGenerate(MalBlkPtr mb)
+mccGenerate(MalBlkPtr mb, str alias)
 {
        char buf[1024];
        stream *f;
 
-       snprintf(buf, 1024, "%s/MCC%s_%s.c", monet_cwd, 
-                       getModuleId(getInstrPtr(mb, 0)), 
getFunctionId(getInstrPtr(mb, 0)));
+       snprintf(buf, 1024, "%s/%s.c", monet_cwd, alias);
        f = open_wastream(buf);
        if (f == NULL)
                throw(IO, "optimizer.MCcompiler", "Could not access file");
@@ -490,32 +539,26 @@
 {
        Module s;
        Symbol t;
-       int j;
+       str alias;
        str nme, fcn, msg = MAL_SUCCEED;
 
        (void) mb;
        printf("Calling the dynamic compiler\n");
        nme = *(str *) getArgReference(stk, p, 1);
        fcn = *(str *) getArgReference(stk, p, 2);
+       alias = *(str *) getArgReference(stk, p, 3);
 
 #ifdef DEBUG_MAL_COMPILER
        printf("MCdynamicCompiler: %s.%s\n", nme, fcn);
 #endif
 
        s = findModule(MCgetClient()->nspace, nme);
-       for (; s; s = s->outer)
-               if (strcmp(s->name, nme) == 0 && s->subscope) {
-                       j = getSubScope(fcn);
-                       for (t = s->subscope[j]; t != NULL; t = t->peer)
-                               if (t->def->errors == 0) {
-                                       if (getSignature(t)->token == 
FUNCTIONsymbol && 
-                                               idcmp(fcn, 
getFunctionId(getSignature(t))) == 0)
-                                               /* call macro expansion */
-                                               mccGenerate(t->def);
-                                       if (msg)
-                                               return msg;
-                               }
-               }
+       if( s){
+               t= findSymbolInModule(s,fcn);
+               if(t== 0)
+                       throw(MAL,"mal_compiler","Could not find function");
+               msg= mccGenerate(t->def,alias);
+       }
        return msg;
 }
 


-------------------------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc.
Still grepping through log files to find problems?  Stop.
Now Search log events and configuration files using AJAX and a browser.
Download your FREE copy of Splunk now >> http://get.splunk.com/
_______________________________________________
Monetdb-checkins mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/monetdb-checkins

Reply via email to