getrhs.c

Go to the documentation of this file.
00001 #include <stdlib.h>
00002 #include "intersci-n.h"
00003 #include "getrhs.h"
00004 
00005 /*****************************************************************
00006  * For each possible data type we have here a function 
00007  * [1] which generate the code for <<Getting>> a variable 
00008  * and checking some properties 
00009  *     if needed check if the variable is square 
00010  *     if needed check if some dimensions must be of fixed sizes
00011  * [2] generate the code for type convertion if needed
00012  * [3] computes the string for the C or Fortran calling sequence 
00013  *     which is stored in the variable structure 
00014  * All the possible Getfunction are stored in a function table 
00015  ******************************************************************/
00016 
00017 /******************************************************
00018  * the functions in the following table must follow  the 
00019  * order given by the type list defined in  intersci-n.h 
00020  * The correct ordering is checked when using this table.
00021  * (see intersci.c) 
00022  ********************************************************/
00023 
00024 GetRhsTab RHSTAB[] = {
00025   {DIMFOREXT,GetDIMFOREXT},
00026   {COLUMN,GetCOLUMN},
00027   {LIST,GetLIST},
00028   {TLIST,GetTLIST},
00029   {MATRIX,GetMATRIX},
00030   {POLYNOM,GetPOLYNOM},
00031   {ROW,GetROW},
00032   {SCALAR,GetSCALAR},
00033   {SEQUENCE,GetSEQUENCE},
00034   {STRING,GetSTRING},
00035   {WORK,GetWORK},
00036   {EMPTY,GetEMPTY},
00037   {ANY,GetANY},
00038   {VECTOR,GetVECTOR},
00039   {STRINGMAT,GetSTRINGMAT},
00040   {SCIMPOINTER,GetPOINTER},
00041   {IMATRIX,GetIMATRIX},
00042   {SCISMPOINTER,GetPOINTER},
00043   {SCILPOINTER,GetPOINTER},
00044   {BMATRIX,GetBMATRIX},
00045   {SCIBPOINTER,GetPOINTER},
00046   {SCIOPOINTER,GetPOINTER},
00047   {SPARSE,GetSPARSE}
00048 };
00049 
00050 extern  int indent ; /* incremental counter for code indentation */
00051 extern  int pass;  /* flag for couting pass on code generation */
00052 
00053 static  char str[MAXNAM];
00054 static  char str1[MAXNAM];
00055 static  char str2[MAXNAM];
00056 
00057 /***********************************************
00058  * Matrix OK
00059  *   flag is used for optional variables 
00060  *   f(..... x=val) 
00061  ***********************************************/
00062 
00063 void GetMATRIX(f,var,flag) 
00064      FILE *f;   VARPTR var ;int flag;
00065 {
00066   GetCom(f,var,flag);
00068   CheckSquare(f,var,str1,str2); 
00069   Check(f,var,0);
00070   Check(f,var,1);
00071 }
00072 
00075 void GetCom(f,var,flag) 
00076      FILE *f;   VARPTR var ;int flag;
00077 {
00078   static char C[]="GetRhsVar(%s,\"%s\",&m%d,&n%d,&l%d);\n";
00079   static char LC[]="GetListRhsVar(%s,%d,\"%s\",&m%s,&n%s,&l%s);\n";
00080   int i1= var->stack_position;
00081   if ( flag == 1 ) 
00082     sprintf(str2,"k");
00083   else
00084     sprintf(str2,"%d",i1);
00085   if (var->list_el ==0 )
00086     {
00088       sprintf(str1,"%d",i1);
00089       Fprintf(f,indent,C,str2,SGetForTypeAbrev(var),i1,i1,i1);
00090       /* Adding the calling sequence in the for_names  */ 
00091       ChangeForName2(var,"%s(l%s)",SGetForTypeStack(var),str1);
00092     }
00093   else
00094     {
00096       sprintf(str1,"%de%d",i1,var->list_el);
00097       Fprintf(f,indent,LC,str2,var->list_el,SGetForTypeAbrev(var),str1,str1,str1,str1);
00098       /* Adding the calling sequence in the for_names  */ 
00099       ChangeForName2(var,"%s(l%s)",SGetForTypeStack(var),str1);
00100     }
00101   AddDeclare1(DEC_INT,"m%s",str1);
00102   AddDeclare1(DEC_INT,"n%s",str1);
00103   AddDeclare1(DEC_INT,"l%s",str1);
00104 }
00105 
00106 
00107 
00108 
00109 /***********************************************
00110  * STRING : OK
00111  ***********************************************/
00112   
00113 void GetSTRING(f,var,flag) FILE *f;   VARPTR var ;int flag;
00114 {
00115   if (var->for_type != CHAR) 
00116     {
00117       printf("incompatibility between the type %s and FORTRAN type %s for variable \"%s\"\n",
00118              SGetSciType(STRING),SGetForType(var->for_type),var->name);
00119       exit(1);
00120     }
00121   GetCom(f,var,flag);
00122   Check(f,var,0);
00123 }
00124 
00125 /***********************************************
00126  * Boolean matrix OK
00127  ***********************************************/
00128 
00129 
00130 void GetBMATRIX(f,var,flag) 
00131      FILE *f;   VARPTR var ;int flag;
00132 {
00133   if (var->for_type != INT && var->for_type != BOOLEAN)
00134     {
00135       printf("incompatibility between the type %s and FORTRAN type %s for variable \"%s\"\n",
00136              SGetSciType(var->type),SGetForType(var->for_type),var->name);
00137       exit(1);
00138     }
00139   var->for_type = BOOLEAN;
00140   GetCom(f,var,flag);
00142   CheckSquare(f,var,str1,str2); 
00143   Check(f,var,0);
00144   Check(f,var,1);
00145 }
00146 
00147 /***********************************************
00148  * Complex Matrix 
00149  ***********************************************/
00150 
00151 void GetIMATRIX(f,var,flag) 
00152      FILE *f;   VARPTR var ;int flag;
00153 {
00154   static char C[]="GetRhsCVar(%s,\"%s\",&it%d,&m%d,&n%d,&lr%d,&lc%d);\n";
00155   static char LC[]="GetListRhsCVar(%s,%d,\"%s\",&it%s,&m%s,&n%s,&lr%s,&lc%s,&lar%s,&lac%s);\n";
00156   int i1= var->stack_position;
00157   if ( flag == 1 ) 
00158     sprintf(str2,"k");
00159   else
00160     sprintf(str2,"%d",i1);
00161   if (var->list_el ==0 )
00162     {
00164       sprintf(str1,"%d",i1);
00165       Fprintf(f,indent,C,str2,SGetForTypeAbrev(var),
00166               i1,i1,i1,i1,i1);
00167       /* Adding the calling sequence in the for_names  */ 
00168       ChangeForName2(var,"%s(lr%s),%s(lc%s),&it%s",
00169                      SGetForTypeStack(var),str1,
00170                      SGetForTypeStack(var),str1,str1);
00171     }
00172   else
00173     {
00175       sprintf(str1,"%de%d",i1,var->list_el);
00176       AddDeclare1(DEC_INT,"lar%s",str1);
00177       AddDeclare1(DEC_INT,"lac%s",str1);
00178       Fprintf(f,indent,LC,str2,var->list_el,SGetForTypeAbrev(var),
00179               str1,str1,str1,str1,str1,str1,str1);
00180       /* Adding the calling sequence in the for_names  */ 
00181       ChangeForName2(var,"%s(lr%s),%s(lc%s),&it%s",
00182                      SGetForTypeStack(var),str1,
00183                      SGetForTypeStack(var),str1,str1);
00184     }
00185   
00186   AddDeclare1(DEC_INT,"m%s",str1);
00187   AddDeclare1(DEC_INT,"n%s",str1);
00188   AddDeclare1(DEC_INT,"lr%s",str1);
00189   AddDeclare1(DEC_INT,"lc%s",str1);
00190   AddDeclare1(DEC_INT,"it%s",str1);
00191 
00193   CheckSquare(f,var,str1,str2); 
00194   Check(f,var,0);
00195   Check(f,var,1);
00196 }
00197 
00198 
00199 /***********************************************
00200  * Sparse Matrix 
00201  ***********************************************/
00202 
00203 void GetSPARSE(f,var,flag) 
00204      FILE *f;   VARPTR var ;int flag;
00205 {
00206   static char C[]="GetRhsVar(%s,\"s\",&m%d,&n%d,&S%d);\n";
00207   static char LC[]="GetListRhsVar(%s,%d,\"s\",&m%s,&n%s,&S%s);\n";
00208   int i1= var->stack_position;
00209   if ( flag == 1 ) 
00210     sprintf(str2,"k");
00211   else
00212     sprintf(str2,"%d",i1);
00213   if (var->list_el ==0 )
00214     {
00216       sprintf(str1,"%d",i1);
00217       Fprintf(f,indent,C,str2, i1,i1,i1);
00218       /* Adding the calling sequence in the for_names  */ 
00219       ChangeForName2(var,"&S%d",i1);
00220     }
00221   else
00222     {
00224       sprintf(str1,"%de%d",i1,var->list_el);
00225       Fprintf(f,indent,LC,str2,var->list_el, str1,str1,str1);
00226       /* Adding the calling sequence in the for_names  */ 
00227       ChangeForName2(var,"&S%s",str1);
00228     }
00229 
00230   AddDeclare1(DEC_INT,"m%s",str1);
00231   AddDeclare1(DEC_INT,"n%s",str1);
00232   AddDeclare1(DEC_SPARSE,"S%s",str1);
00233 
00235   CheckSquare(f,var,str1,str2); 
00236   Check(f,var,0);
00237   Check(f,var,1);
00238 }
00239 
00240 
00241 
00242 
00243 /***********************************************
00244  * Stringmat 
00245  ***********************************************/
00246 
00247 void GetSTRINGMAT(f,var,flag) FILE *f;   VARPTR var ;int flag;
00248 {
00249   int i1= var->stack_position;
00250   if ( flag == 1 ) 
00251     sprintf(str2,"k");
00252   else
00253     sprintf(str2,"%d",i1);
00254   if (var->list_el ==0 )
00255     {
00256       AddDeclare1(DEC_SMAT,"Str%d",i1);
00257       AddDeclare1(DEC_INT,"m%d",i1);
00258       AddDeclare1(DEC_INT,"n%d",i1);
00259       Fprintf(f,indent,"GetRhsVar(%s,\"S\",&m%d,&n%d,&Str%d);\n",str2,i1,i1,i1);
00260       sprintf(str,"&Str%d",i1);
00261       ChangeForName1(var,str);
00262     }
00263   else
00264     {
00265       sprintf(str1,"%de%d",i1,var->list_el);
00266       AddDeclare1(DEC_SMAT,"Str%s",str1);
00267       AddDeclare1(DEC_INT,"m%s",str1);
00268       AddDeclare1(DEC_INT,"n%s",str1);
00269       Fprintf(f,indent,"GetListRhsVar(%s,%d,\"S\",&m%s,&n%s,&Str%s);\n",str2,var->list_el,
00270               str1,str1,str1);
00271       sprintf(str,"&Str%s",str1);
00272       ChangeForName1(var,str);
00273     }
00274   /* square matrix */
00275   CheckSquare(f,var,str1,str2); 
00276   Check(f,var,0);
00277   Check(f,var,1);
00278   if (var->for_type != CSTRINGV) 
00279     {
00280       printf("incompatibility between the type %s and FORTRAN type %s for variable \"%s\"\n",
00281              SGetSciType(STRINGMAT),SGetForType(var->for_type),var->name);
00282       exit(1);
00283     }
00284 }
00285 
00286 /***********************************************
00287  * Row 
00288  ***********************************************/
00289 
00290 void GetROW(f,var,flag) FILE *f;   VARPTR var ;int flag;
00291 {
00292   int i1= var->stack_position;
00293   GetCom(f,var,flag);
00294   Check(f,var,0);
00295   if (var->list_el ==0 )
00296     {
00297       Fprintf(f,indent,"CheckRow(%d,m%d,n%d);\n",i1,i1,i1);
00298       Fprintf(f,indent,"mn%d=m%d*n%d;\n",i1,i1,i1);
00299       AddDeclare1(DEC_INT,"mn%d",i1);
00300     }
00301   else 
00302     {
00303       sprintf(str1,"%de%d",i1,var->list_el);
00304       Fprintf(f,indent,"CheckListRow(%d,%d,m%s,n%s);\n",i1,var->list_el,str1,str1);
00305       Fprintf(f,indent,"l%dmn%d=m%s*n%s;\n",i1,var->list_el,str1,str1);
00306       AddDeclare1(DEC_INT,"l%dmn%d",i1,var->list_el);
00307     }
00308 }
00309 
00310 
00311 /***********************************************
00312  * Column 
00313  ***********************************************/
00314 
00315 void GetCOLUMN(f,var,flag) FILE *f;   VARPTR var ;int flag;
00316 {
00317   int i1= var->stack_position;
00318   GetCom(f,var,flag);
00319   Check(f,var,0);
00320   if (var->list_el ==0 )
00321     {
00322       Fprintf(f,indent,"CheckColumn(%d,m%d,n%d);\n",i1,i1,i1);
00323       Fprintf(f,indent,"mn%d=m%d*n%d;\n",i1,i1,i1);
00324       AddDeclare1(DEC_INT,"mn%d",i1);
00325     }
00326   else 
00327     {
00328       sprintf(str1,"%de%d",i1,var->list_el);
00329       Fprintf(f,indent,"CheckListColumn(%d,%d,m%s,n%s);\n",i1,var->list_el,str1,str1);
00330       Fprintf(f,indent,"l%dmn%d=m%s*n%s;\n",i1,var->list_el,str1,str1);
00331       AddDeclare1(DEC_INT,"l%dmn%d",i1,var->list_el);
00332     }
00333 }
00334 
00335 /***********************************************
00336  * Vector 
00337  ***********************************************/
00338 
00339 void GetVECTOR(f,var,flag) FILE *f;   VARPTR var ;int flag;
00340 {
00341   int i1= var->stack_position;
00342   GetCom(f,var,flag);
00343   Check(f,var,0);
00344   if (var->list_el ==0 )
00345     {
00346       Fprintf(f,indent,"CheckVector(%d,m%d,n%d);\n",i1,i1,i1);
00347       Fprintf(f,indent,"mn%d=m%d*n%d;\n",i1,i1,i1);
00348       AddDeclare1(DEC_INT,"mn%d",i1);
00349     }
00350   else 
00351     {
00352       sprintf(str1,"%de%d",i1,var->list_el);
00353       Fprintf(f,indent,"CheckListVector(%d,%d,m%s,n%s);\n",i1,var->list_el,str1,str1);
00354       Fprintf(f,indent,"l%dmn%d=m%s*n%s;\n",i1,var->list_el,str1,str1);
00355       AddDeclare1(DEC_INT,"l%dmn%d",i1,var->list_el);
00356     }
00357 }
00358 
00359 /***********************************************
00360  * Polynom 
00361  ***********************************************/
00362 
00363 void GetPOLYNOM(f,var,flag) FILE *f;   VARPTR var ;int flag;
00364 {
00365   int i1= var->stack_position;
00366   if (var->list_el ==0 )
00367     {
00368       sprintf(str1,"%d",i1);
00369       AddDeclare(DEC_LOGICAL,"getonepoly");
00370       AddDeclare(DEC_CHAR,str);
00371       Fprintf(f,indent,"if(.not.getonepoly(fname,top,top-rhs+%d,it%d,m%d,namelr%d,namellr%d,lr%d,lc%d)\n",i1,i1,i1,i1,i1,i1,i1);
00372     }
00373   else
00374     {
00375       sprintf(str1,"%de%d",i1,var->list_el);
00376       AddDeclare(DEC_LOGICAL,"getlistpoly");
00377       Fprintf(f,indent,"if(.not.getlistpoly(fname,topk,top-rhs+%d,%d,it%s,m%s,n%s,name%s,\n",
00378               i1,var->list_el,str1,str1,str1,str1);
00379       Fprintf(f,indent,"$     namel%s,ilp%s,lr%s,lc%s)\n",str1,str1,str1,str1);
00380     }
00381   Check(f,var,0);
00382   /* Convertion */ 
00383   switch (var->for_type) 
00384     {
00385     case INT:
00386       Fprintf(f,indent,"call entier(n%s,stk(lr%s),istk(iadr(lr%s)))\n",
00387               str1,str1,str1);
00388       sprintf(str,"istk(iadr(lr%s))",str1);
00389       ChangeForName1(var,str);
00390       break;
00391     case REAL:
00392       Fprintf(f,indent,"call simple(n%s,stk(lr%s),stk(lr%s))\n",
00393               str1,str1,str1);
00394       sprintf(str,"stk(lr%s)",str1);
00395       ChangeForName1(var,str);
00396       break;
00397     case DOUBLE:
00398       sprintf(str,"stk(lr%s)",str1);
00399       ChangeForName1(var,str);
00400       break;
00401     default: 
00402       printf("incompatibility between Scilab and Fortran type for variable \"%s\"\n",
00403              var->name);
00404       exit(1);
00405     }
00406 }
00407 
00408 /***********************************************
00409  * Scalar 
00410  ***********************************************/
00411 
00412 void GetSCALAR(f,var,flag) FILE *f;   VARPTR var ;int flag;
00413 {
00414   int i1= var->stack_position;
00415   GetCom(f,var,flag);
00416   /* Check(f,var,0); */
00417   if (var->list_el ==0 )
00418     {
00419       Fprintf(f,indent,"CheckScalar(%d,m%d,n%d);\n",i1,i1,i1);
00420     }
00421   else 
00422     {
00423       sprintf(str1,"%de%d",i1,var->list_el);
00424       Fprintf(f,indent,"CheckListScalar(%d,%d,m%s,n%s);\n",i1,var->list_el,str1,str1);
00425     }
00426 }
00427 
00428 /***********************************************
00429  * Pointers 
00430  ***********************************************/
00431 
00432 void GetPOINTER(f,var,flag) 
00433      FILE *f;   VARPTR var ;int flag;
00434 {
00435   static char C[]="GetRhsOPointer(%s,&lr%d);\n";
00436   int i1= var->stack_position;
00437   if ( flag == 1 ) 
00438     sprintf(str2,"k");
00439   else
00440     sprintf(str2,"%d",i1);
00441   sprintf(str1,"%d",i1);
00442   if (var->list_el ==0 )
00443     {
00445       sprintf(str1,"%d",i1);
00446       Fprintf(f,indent,C,str2,i1);
00447       /* Adding the calling sequence in the for_names  */ 
00448       ChangeForName2(var,"stk(lr%s)",str1);
00449     }
00450   else
00451     {
00452       fprintf(stderr,"Wrong type opointer inside a list \n");
00453       exit(1);
00454     }
00455   AddDeclare1(DEC_INT,"lr%s",str1);
00456   
00457 }
00458 
00459 
00460 void GetANY(f,var,flag) FILE *f;   VARPTR var ;int flag;{
00461   fprintf(stderr,"Wrong type in Get function \n");
00462   exit(1);
00463 }
00464 
00465 void GetLIST(f,var,flag) FILE *f;   VARPTR var ;int flag;{
00466   fprintf(stderr,"Wrong type in Get function \n");
00467   exit(1);
00468 }
00469 
00470 void GetTLIST(f,var,flag) FILE *f;   VARPTR var ;int flag;{
00471   fprintf(stderr,"Wrong type in Get function \n");
00472   exit(1);
00473 }
00474 
00475 void GetSEQUENCE(f,var,flag) FILE *f;   VARPTR var ;int flag;
00476 {  
00477   fprintf(stderr,"Wrong type in Get function \n");
00478   exit(1);
00479 }
00480 
00481 void GetEMPTY(f,var,flag)  FILE *f;   VARPTR var ;int flag;
00482 {  
00483   fprintf(stderr,"Wrong type in Get function \n");
00484   exit(1);
00485 }
00486 
00487 void GetWORK(f,var,flag)  FILE *f;   VARPTR var ;int flag;
00488 {  
00489   fprintf(stderr,"Wrong type in Get function \n");
00490   exit(1);
00491 }
00492 
00493 
00494 void GetDIMFOREXT(f,var,flag)  FILE *f;   VARPTR var ;int flag;
00495 {  
00496   fprintf(stderr,"Wrong type in Get function \n");
00497   exit(1);
00498 }
00499 
00500 /***************************************
00501  * Utility function for the Getfunctions
00502  * Check for fixed sized dimensions 
00503  ***************************************/
00504 
00505 void Check(f,var,nel)
00506      FILE *f;
00507      int nel;
00508      VARPTR var;
00509 {
00510   VARPTR var1 = variables[var->el[nel]-1];
00511   if ( var1->nfor_name == 0) 
00512     {
00513       fprintf(stderr,"Pb with element number %d [%s] of variable %s\n",
00514               nel+1, var1->name, var->name);
00515       return;
00516     }
00517   if (isdigit(var1->name[0]) != 0) 
00518     {
00519       /* the dimension of the variable is a constant integer */
00520       if ( strcmp(var1->for_name[0],var1->name) != 0) 
00521         {
00522           if (var->list_el ==0 )
00523             {
00524               Fprintf(f,indent,"CheckOneDim(%d,%d,%s,%s);\n",
00525                       var->stack_position,
00526                       nel+1,
00527                       var1->for_name[0],var1->name);
00528             }
00529           else
00530             {
00531               Fprintf(f,indent,"CheckListOneDim(%s,%d,%d,%s,%s);\n",
00532                       str2,
00533                       var->list_el,
00534                       nel+1,
00535                       var1->for_name[0],var1->name);
00536             }
00537         }
00538     }
00539 }
00540 
00541 
00542 
00543 
00544 void CheckSquare(FILE *f,VARPTR var,char *str1_,char *str2_)
00545 {
00546   if (var->el[0] != var->el[1]) return ;
00547   if (var->list_el ==0 )
00548     {
00549       Fprintf(f,indent,"CheckSquare(%d,m%s,n%s);\n", var->stack_position,str1_,str1_);
00550     }
00551   else 
00552     {
00553       Fprintf(f,indent,"CheckListSquare(%s,%d,m%s,n%s);\n",str2_, var->list_el,str1_,str1_);
00554     }  
00555 }
00556 
00557 
00558 
00559 
00560 
00561 
00562 

Generated on Sun Mar 4 15:03:57 2007 for Scilab [trunk] by  doxygen 1.5.1