summaryrefslogtreecommitdiffstats
path: root/scilab/modules/intersci/src/exe/crerhs.c
blob: 8e80ee6001ceffd63449254c38037d5ff3ff6aeb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
/*
 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
 * Copyright (C) 2000-2008 - INRIA
 *
 * This file must be used under the terms of the CeCILL.
 * This source file is licensed as described in the file COPYING, which
 * you should have received as part of this distribution.  The terms
 * are also available at
 * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
 *
 */

#include <stdlib.h>
#include "intersci-n.h"
#include "crerhs.h"

/*****************************************************************
 * For each possible data type we have here a function
 * which generate the code for <<Creating>> a variable
 * and checking some properties
 *     if needed check if the variable is square
 *     if needed check if some dimensions must be of fixed sizes
 * All the possible Getfunction are stored in a function table
 ******************************************************************/

/**************************************************
 * A FINIR :
 * le code qui est ici est utilise a deux fins
 * Creer des objets ds le stack Scilab
 * et construire en meme temps
 * la sequence d'appel pour
 * le Fortran
 * Il faudrait sans doute couper cela en deux
 * et surtout voir si l'on ne peux pas simplifier
 * le tout :
 *   i.e associer a une variable
 *   des formats qui indiquent comment
 *   on accede aux champs importants pour elle
 *   + un numero unique de variable
 *   un numero ds le stacl etc......
 **************************************************/

/******************************************************
 * the functions in the following table must follow  the
 * order given by the type list defined in  intersci-n.h
 * The correct ordering is checked when using this table.
 * (see intersci.c)
 ********************************************************/

CreRhsTab CRERHSTAB[] = {
  {DIMFOREXT,CreDIMFOREXT},
  {COLUMN,CreCOLUMN},
  {LIST,CreANY},
  {TLIST,CreANY},
  {MATRIX,CreMATRIX},
  {POLYNOM,CreVECTOR},
  {ROW,CreVECTOR},
  {SCALAR,CreSCALAR},
  {SEQUENCE,CreANY},
  {STRING,CreSTRING},
  {WORK,CreVECTOR},
  {EMPTY,CreANY},
  {ANY,CreANY},
  {VECTOR,CreVECTOR},
  {STRINGMAT,CreSTRINGMAT},
  {SCIMPOINTER,CrePOINTER},
  {IMATRIX,CreIMATRIX},
  {SCISMPOINTER,CrePOINTER},
  {SCILPOINTER,CrePOINTER},
  {BMATRIX,CreBMATRIX},
  {SCIBPOINTER,CrePOINTER},
  {SCIOPOINTER,CrePOINTER},
  {SPARSE,CreSPARSE}
};



extern  int indent ; /* incremental counter for code indentation */
extern  int pass;  /* flag for couting pass on code generation */

static  char str[MAXNAM];
static char str1[MAXNAM];
static char str2[MAXNAM];
static char str3[MAXNAM];
static char str4[MAXNAM];

/***********************************************
 * Matrix  XXXXX OK
 ***********************************************/

void CreMATRIX(f,var)
     FILE *f;
     VARPTR var;
{
  WriteCallRestCheck(f,var,"mm",1,0) ;
  GetDim(str3,var->el[1]);
  WriteCallRestCheck(f,var,"nn",0,0) ;
  GetDim(str4,var->el[0]);
  if ( str3[0] == '&' || str3[0] == '(')
    strcpy(str2,str3);
  else
    sprintf(str2,"&%s",str3);
  if ( str4[0] == '&' || str4[0] == '(')
    strcpy(str1,str4);
  else
    sprintf(str1,"&%s",str4);
  CreCommon(f,var);
}

void CreCommon(f,var)
     FILE *f;
     VARPTR var;
{
  if (var->for_type == EXTERNAL )
    {
      Fprintf(f,indent,"/* external variable named %s (xxe%d) */\n",var->name,var->stack_position);
      AddDeclare1(DEC_INT,"me%d",var->stack_position);
      AddDeclare1(DEC_INT,"ne%d",var->stack_position);
       if ( strncmp(var->fexternal,"cintf",4)==0 )
	 AddDeclare1(DEC_IPTR,"le%d",var->stack_position);
       else if ( strncmp(var->fexternal,"cboolf",5)==0 )
	 AddDeclare1(DEC_IPTR,"le%d",var->stack_position);
       else if ( strncmp(var->fexternal,"cdoublef",7)==0 )
	 AddDeclare1(DEC_DPTR,"le%d",var->stack_position);
       else if (strncmp(var->fexternal,"ccharf",5)==0)
	 AddDeclare1(DEC_CPTR,"le%d",var->stack_position);
       else if (strncmp(var->fexternal,"cfloatf",6)==0 )
	 AddDeclare1(DEC_RPTR,"le%d",var->stack_position);
       else
	 AddDeclare1(DEC_UL,"le%d",var->stack_position);
       ChangeForName2(var,"&le%d",var->stack_position);
       /** List case not considered here **/
    }
   else
     {
       char *lstr1,*lstr2;
       if ( strncmp(str1,"&istk(",6)==0 || strncmp(str1,"&cstk(",6)==0 || strncmp(str1,"&rstk(",6)==0
	    || strncmp(str1,"&stk(",5)==0 ) lstr1 = str1+1;
       else
	 lstr1=str1;
       if ( strncmp(str2,"&istk(",6)==0 || strncmp(str2,"&cstk(",6)==0 || strncmp(str2,"&rstk(",6)==0
	    || strncmp(str2,"&stk(",5)==0 ) lstr2 = str2+1;
       else
	 lstr2=str2;

       Fprintf(f,indent,"CreateVar(%d,\"%s\",%s,%s,&l%d);/* named: %s */\n",
	       var->stack_position,
	       SGetForTypeAbrev(var),
	       lstr1,lstr2,var->stack_position,
	       var->name);
       AddDeclare1(DEC_INT,"l%d",var->stack_position);
       ChangeForName2(var,"%s(l%d)",SGetForTypeStack(var),
		      var->stack_position);
     }
}

/***********************************************
 * String   XXXXX OK
 ***********************************************/

void CreSTRING(f,var)
     FILE *f;
     VARPTR var;
{
  if (var->for_type != CHAR && var->for_type != EXTERNAL )
    {
      printf("incompatibility between the type %s and FORTRAN type %s for variable \"%s\"\n",
	     SGetSciType(STRING),SGetForType(var->for_type),var->name);
      exit(1);
    }
  AddDeclare1(DEC_INT,"n%d",var->stack_position);
  AddDeclare1(DEC_INIT,"n%d=1",var->stack_position);
  sprintf(str2,"&n%d",var->stack_position);
  WriteCallRestCheck(f,var,"nn",0,0) ;
  GetDim(str1,var->el[0]);
  CreCommon(f,var);
}



/***********************************************
 * Boolean matrix  XXXXX OK
 ***********************************************/

void CreBMATRIX(f,var)
     FILE *f;
     VARPTR var;
{
  if (var->for_type == INT ) var->for_type = BOOLEAN;
  if (var->for_type != BOOLEAN && var->for_type != EXTERNAL )
    {
      printf("incompatibility between the type %s and FORTRAN type %s for variable \"%s\"\n",
	     SGetSciType(var->type),SGetForType(var->for_type),var->name);
      exit(1);
    }
  CreMATRIX(f,var);
}

/***********************************************
 * variable which are dimensions
 * (of Scilab variables or external)
 ***********************************************/

void CreDIMFOREXT(f,var)
     FILE *f;
     VARPTR var;
{
  if (var->nfor_name == 0 && var->for_type != PREDEF)
    {
      printf("dimension variable \"%s\" is not defined\n",var->name);
      exit(1);
    }
  switch (var->for_type)
    {
    case PREDEF:
    case 'C':
      if ( strcmp(var->name,"rhs") == 0)
	{
	  AddDeclare(DEC_INT,"rhs");
	  Fprintf(f,indent,"rhs=Rhs;\n");
	  sprintf(str,"&rhs");
	}
      else
	{
	  sprintf(str,"&%s",var->name);
	}
      ChangeForName1(var,str);
      break;
    case 0:
    case INT:
      sprintf(str,"&%s",var->for_name[0]);
      if ( ~isdigit(str[1]))
	{
	  ChangeForName1(var,str);
	}
      else
	{
	  Fprintf(f,indent,"loc%s= (int) %s;\n",
		  var->for_name[0],var->for_name[0]);
	  sprintf(str,"loc%s",var->for_name[0]);
	  AddDeclare(DEC_INT,str);
	  sprintf(str,"&loc%s",var->for_name[0]);
	  ChangeForName1(var,str);
	}
      break;
    case DOUBLE:
      Fprintf(f,indent,"loc%s= (double) %s;\n",
	      var->for_name[0],var->for_name[0]);
      sprintf(str,"loc%s",var->for_name[0]);
      AddDeclare(DEC_DOUBLE,str);
      sprintf(str,"&loc%s",var->for_name[0]);
      ChangeForName1(var,str);
      break;
    case REAL:
      Fprintf(f,indent,"loc%s= (float) %s;\n",var->for_name[0],var->for_name[0]);
      sprintf(str,"loc%s",var->for_name[0]);
      AddDeclare(DEC_REAL,str);
      sprintf(str,"&loc%s",var->for_name[0]);
      ChangeForName1(var,str);
      break;
    case CHAR:
    case CSTRINGV:
      printf("a dimension variable cannot have FORTRAN type \"%s\"\n",
	     SGetForType(var->for_type));
      exit(1);
      break;
    }
}

/***********************************************
 * Common code for COLUMN, ROW, WORK,POLYNOM,VECTOR
 ***********************************************/

void CreVECTOR(f,var)
     FILE *f;
     VARPTR var;
{
  char *lstr1 ;
  WriteCallRestCheck(f,var,"nn",0,0) ;
  GetDim(str1,var->el[0]);
  AddDeclare1(DEC_INT,"un=1");
  AddDeclare1(DEC_INT,"mn%d",var->stack_position);
  lstr1 = ( str1[0]== '&' )? str1+1: str1;
  /** peut-etre un pb si str1 contient des stk istk .. XXXXXX **/
  if ( strncmp(lstr1,"stk",3) ==0 || strncmp(lstr1,"istk",4)==0
       || strncmp(lstr1,"rstk",4)==0 || strncmp(lstr1,"cstk",4)==0 )
    sprintf(str2,"(mn%d=*%s,&mn%d)",var->stack_position,
	    lstr1,var->stack_position);
  else
    sprintf(str2,"(mn%d=%s,&mn%d)",var->stack_position,
	    lstr1,var->stack_position);
  strcpy(str1,"(un=1,&un)");
  CreCommon(f,var);
}

void CreCOLUMN(f,var)
     FILE *f;
     VARPTR var;
{
  char *lstr1 ;
  WriteCallRestCheck(f,var,"nn",0,0) ;
  GetDim(str1,var->el[0]);
  AddDeclare1(DEC_INT,"un=1");
  AddDeclare1(DEC_INT,"mn%d",var->stack_position);
  lstr1 = ( str1[0]== '&' )? str1+1: str1;
  /** peut-etre un pb si str1 contient des stk istk .. XXXXXX **/
  if ( strncmp(lstr1,"stk",3) ==0 || strncmp(lstr1,"istk",4)==0
       || strncmp(lstr1,"rstk",4)==0 || strncmp(lstr1,"cstk",4)==0 )
    sprintf(str2,"(mn%d=*%s,&mn%d)",var->stack_position,
	    lstr1,var->stack_position);
  else
    sprintf(str2,"(mn%d=%s,&mn%d)",var->stack_position,
	    lstr1,var->stack_position);
  strcpy(str1,str2);
  strcpy(str2,"(un=1,&un)");
  CreCommon(f,var);
}


/***********************************************
 * Sparse OK
 ***********************************************/

void CreSPARSE(f,var)
     FILE *f;
     VARPTR var;
{
  WriteCallRestCheck(f,var,"mm",1,0) ;
  GetDim(str2,var->el[1]);
  WriteCallRestCheck(f,var,"nn",0,0) ;
  GetDim(str1,var->el[0]);
  if (var->for_type == EXTERNAL)
    {
      AddDeclare1(DEC_SPARSEPTR,"S%d",var->stack_position);
      ChangeForName2(var,"&S%d",var->stack_position);
      /** List case not considered here **/
    }
  else
    {
      VARPTR m,n;
      int origm,orign;
      m= variables[var->el[0]-1];
      n= variables[var->el[1]-1];
      /* here we must create a sparse variable
       * but str1 and str2 is not enough to create the matrix
       * we copy the argument which gives the size
       */
      origm = ( m->nfor_name == 0)? -1 : m->for_name_orig[0];
      orign = ( n->nfor_name == 0)? -1 : n->for_name_orig[0];
      if ( origm != orign )
	{
	  fprintf(stderr,"A local sparse matrix can only be built as a copy of a sparse entry\n");
	  exit(1);
	}
      AddDeclare1(DEC_SPARSE,"S%d",var->stack_position);
      Fprintf(f,indent,"CreateVarFromPtr(%d,\"s\",&%s,&%s,&S%d);\n",
	      var->stack_position,
	      str1,str2,origm);
      Fprintf(f,indent,"GetRhsVar(%d,\"s\",&%s,&%s,&S%d);\n",
	      var->stack_position,
	      str1,str2,var->stack_position);
      ChangeForName2(var,"&S%d",var->stack_position);
    }
}


/***********************************************
 * Complex Matrix OK
 ***********************************************/

void CreIMATRIX(f,var)
     FILE *f;
     VARPTR var;
{
  WriteCallRestCheck(f,var,"it",2,0) ;
  GetDim(str3,var->el[2]);
  WriteCallRestCheck(f,var,"mm",1,0) ;
  GetDim(str2,var->el[1]);
  WriteCallRestCheck(f,var,"nn",0,0) ;
  GetDim(str1,var->el[0]);
  if (var->for_type == EXTERNAL)
    {
      AddDeclare1(DEC_UL,"ler%d",var->stack_position);
      AddDeclare1(DEC_UL,"lec%d",var->stack_position);
      AddDeclare1(DEC_INT,"ite%d",var->stack_position);
      ChangeForName2(var,"&ler%d,&lec%d,&ite%d",var->stack_position,var->stack_position,var->stack_position);
      /** List case not considered here **/
    }
  else
    {
      Fprintf(f,indent,"CreateCVar(%d,\"%s\",&%s,&%s,&%s,&lr%d,&lc%d);\n",
	      var->stack_position,SGetForTypeAbrev(var),
	      str3,str1,str2,var->stack_position,var->stack_position);
      AddDeclare1(DEC_INT,"lr%d",var->stack_position);
      AddDeclare1(DEC_INT,"lc%d",var->stack_position);
      AddDeclare1(DEC_INT,"it%d",var->stack_position);
      ChangeForName2(var,"%s(lr%d),%s(lc%d),&it%d",
		     SGetForTypeStack(var),
		     var->stack_position,
		     SGetForTypeStack(var),
		     var->stack_position,var->stack_position);
    }
}


/***********************************************
 * Pointers
 ***********************************************/

void CrePOINTER(f,var)
     FILE *f;
     VARPTR var;
{
  if (var->for_type == EXTERNAL)
    {
      AddDeclare1(DEC_UL,"le%d",var->stack_position);
      ChangeForName2(var,"&le%d",var->stack_position);
      /** List case not considered here **/
    }
  else
    {
      Fprintf(f,indent,"CreateOpointer(%d,&lr%d);\n",
	      var->stack_position,var->stack_position);
      AddDeclare1(DEC_INT,"lr%d",var->stack_position);
      ChangeForName2(var,"stk(lr%d)",
		     var->stack_position);
    }
}


/***********************************************
 * STRINGMAT
 ***********************************************/

void CreSTRINGMAT(f,var)
     FILE *f;
     VARPTR var;
{
  if (var->for_type == EXTERNAL || var->for_type == CSTRINGV )
    {
      /* for external or cstringv parameters, unknown formal dimensions
	 can be used */
      WriteCallRestCheck(f,var,"nsmm",0,1) ;
      WriteCallRestCheck(f,var,"nsnn",1,1) ;
      sprintf(str,"nsmm%d",var->stack_position);
      AddForName1(var->el[0],str,NULL,var->stack_position);
      sprintf(str,"nsnn%d",var->stack_position);
      AddForName1(var->el[1],str,NULL,var->stack_position);
      AddDeclare1(DEC_SMAT,"Str%d",var->stack_position);
      AddDeclare1(DEC_INT,"nsmm%d",var->stack_position);
      AddDeclare1(DEC_INT,"nsnn%d",var->stack_position);
      sprintf(str,"&Str%d",var->stack_position);
      ChangeForName1(var,str);
    }
  else
    {
      /** XXXX dimensions should be specified **/
      fprintf(stderr,"WARNING : your code contains a specification\n");
      fprintf(stderr," not fully implemented in intersci\n");
      WriteCallRestCheck(f,var,"mm",0,0) ;
      WriteCallRestCheck(f,var,"nn",1,0) ;
      AddDeclare(DEC_LOGICAL,"cresmatafaire");
      Fprintf(f,indent,"if(.not.cresmatafaire(fname,top-rhs+%d,lr%d)) return\n",var->stack_position,var->stack_position);
      sprintf(str,"stk(lr%d)",var->stack_position);
      ChangeForName1(var,str);
    }
}


/***********************************************
 * Scalar
 ***********************************************/

void CreSCALAR(f,var)
     FILE *f;
     VARPTR var;
{
  strcpy(str1,"&un");
  strcpy(str2,"&un");
  AddDeclare(DEC_INT,"un=1");
  CreCommon(f,var);
}

/***********************************************
 ** Common code for LIST TLIST SEQUENCE ANY **
 ***********************************************/

void CreANY(f,var)
     FILE *f;
     VARPTR var;
{
  printf("work or output FORTRAN argument cannot have\n");
  printf("  type \"ANY\", \"LIST\", \"TLIST\" or \"SEQUENCE\"\n");
  exit(1);
}

/***********************************************
 * Utility function for WriteCallRest
 * when flag==1 we acccept undefined dimensions
 * this is used with stringmat/Cstringv
 * where dimensions and space are allocated inside
 * the interfaced function and returned
 * to the interface
 *******************************************/

void WriteCallRestCheck(FILE *f,VARPTR var,char *name,int iel,int flag)
{
  char sdim[MAXNAM];
  char lstr[MAXNAM];
  if (variables[var->el[iel]-1]->nfor_name == 0)
    {
      strcpy(lstr,variables[var->el[iel]-1]->name);
      if (isdigit(lstr[0]) == 0)
	{
	  if ( variables[var->el[iel]-1]->is_sciarg == 1)
	    {
	      /* dimension of FORTRAN argument is a SCILAB argument */
	      sprintf(sdim,"%s%d",name,var->stack_position);
	      Fprintf(f,indent,"%s= (int) *stk(lr%d));\n",sdim,
		      variables[var->el[iel]-1]->stack_position);
	      AddForName1(var->el[iel],sdim,NULL,var->stack_position);
	    }
	  else if ( flag != 1)
	    {
	      printf("dimension variable \"%s\" is not defined\n",
		     variables[var->el[iel]-1]->name);
	      exit(1);
	    }
	}
      else
	{
	  sprintf(sdim,"%s%d",name,var->stack_position);
	  Fprintf(f,indent,"%s=%s;\n",sdim,lstr);
	  AddForName1(var->el[iel],sdim,NULL,var->stack_position);
	}
    }
}

/** to be finished : Forname2Int is not finished fo C  **/

void GetDim(char *lstr,IVAR ivar)
{
  char *s;
  s=Forname2Int(variables[ivar-1],0);
  if ( strncmp(s,"stk",3)==0 || strncmp(s,"istk",4)==0
       || strncmp(s,"sstk",4)==0 || strncmp(s,"stk",3)==0 ||
       strncmp(s,"cstk",4)==0 )
    {
      strcpy(lstr,s);
      return ;
    }
  else
    {
      AddDeclare1(DEC_INT,s);
      strcpy(lstr,s);
    }
}