/[ascend]/trunk/base/generic/compiler/exprs.c
ViewVC logotype

Diff of /trunk/base/generic/compiler/exprs.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 668 by johnpye, Fri Mar 31 08:52:57 2006 UTC revision 669 by johnpye, Wed Jun 21 07:00:45 2006 UTC
# Line 1  Line 1 
1  /*  /*  ASCEND modelling environment
2   *  Expression Implementation      Copyright (C) 2006 Carnegie Mellon University
3   *  by Tom Epperly      Copyright (C) 1990, 1993, 1994 Thomas Guthrie Epperly
4   *  Version: $Revision: 1.13 $                            
5   *  Version control file: $RCSfile: exprs.c,v $      This program is free software; you can redistribute it and/or modify
6   *  Date last modified: $Date: 1998/02/05 16:35:58 $      it under the terms of the GNU General Public License as published by
7   *  Last modified by: $Author: ballan $      the Free Software Foundation; either version 2, or (at your option)
8   *      any later version.
9   *  This file is part of the Ascend Language Interpreter.  
10   *      This program is distributed in the hope that it will be useful,
11   *  Copyright (C) 1990, 1993, 1994 Thomas Guthrie Epperly      but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   *  The Ascend Language Interpreter is free software; you can redistribute      GNU General Public License for more details.
14   *  it and/or modify it under the terms of the GNU General Public License as  
15   *  published by the Free Software Foundation; either version 2 of the      You should have received a copy of the GNU General Public License
16   *  License, or (at your option) any later version.      along with this program; if not, write to the Free Software
17   *      Foundation, Inc., 59 Temple Place - Suite 330,
18   *  The Ascend Language Interpreter is distributed in hope that it will be      Boston, MA 02111-1307, USA.
19   *  useful, but WITHOUT ANY WARRANTY; without even the implied warranty of  *//**
20   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU      @file
21   *  General Public License for more details.      Expression Module
22   *  
23   *  You should have received a copy of the GNU General Public License along with      Requires:
24   *  the program; if not, write to the Free Software Foundation, Inc., 675      #include "utilities/ascConfig.h"
25   *  Mass Ave, Cambridge, MA 02139 USA.  Check the file named COPYING.      #include "fractions.h"
26   */      #include "compiler.h"
27        #include "dimen.h"
28        #include "expr_types.h"
29    *//*
30        by Tom Epperly
31        Last in CVS: $Revision: 1.13 $ $Date: 1998/02/05 16:35:58 $ $Author: ballan $
32    */
33    
34    #include <stdio.h>
35    #include <assert.h>
36    
 #include<stdio.h>  
 #include<assert.h>  
37  #include <utilities/ascConfig.h>  #include <utilities/ascConfig.h>
38  #include <utilities/ascPanic.h>  #include <utilities/ascPanic.h>
39  #include "compiler.h"  #include "compiler.h"
# Line 37  Line 44 
44  #include "fractions.h"  #include "fractions.h"
45  #include "dimen.h"  #include "dimen.h"
46  #include "functype.h"  #include "functype.h"
47  #include "types.h"  #include "expr_types.h"
48  #include "func.h"  #include "func.h"
49  #include "name.h"  #include "name.h"
50  #include "sets.h"  #include "sets.h"
# Line 50  Line 57 
57  static CONST char ExpressionID[] = "$Id: exprs.c,v 1.13 1998/02/05 16:35:58 ballan Exp $";  static CONST char ExpressionID[] = "$Id: exprs.c,v 1.13 1998/02/05 16:35:58 ballan Exp $";
58  #endif  #endif
59    
60    /*------------------------------------------------------------------------------
61      MEMORY USAGE
62    
63      Using 'pool' or else regular malloc...
64    */
65    
66  #ifdef ASC_NO_POOL  #ifdef ASC_NO_POOL
67  #define EXPRSUSESPOOL FALSE  #define EXPRSUSESPOOL FALSE
68  #else  #else
69  #define EXPRSUSESPOOL TRUE  #define EXPRSUSESPOOL TRUE
70  #endif  #endif
71    
72  #if EXPRSUSESPOOL /* FALSE means hacking ascCompiler.c */  #if EXPRSUSESPOOL /* using 'g_exprs_pool' for memory management */
73    /** global for our memory manager */
74  static pool_store_t g_exprs_pool = NULL;  static pool_store_t g_exprs_pool = NULL;
75  /* global for our memory manager */      /* aim for 4096 chunks including malloc overhead */
76  /* aim for 4096 chunks including malloc overhead */  # define EMP_LEN 10
77  #define EMP_LEN 10  # if (SIZEOF_VOID_P == 8)
78  #if (SIZEOF_VOID_P == 8)  #  define EMP_WID 63
79  #define EMP_WID 63  # else
80  #else  #  define EMP_WID 127
81  #define EMP_WID 127  # endif
82  #endif      /* retune rpwid if the size of struct name changes */
83  /* retune rpwid if the size of struct name changes */  # define EMP_ELT_SIZE (sizeof(struct Expr))
84  #define EMP_ELT_SIZE (sizeof(struct Expr))  # define EMP_MORE_ELTS 10
85  #define EMP_MORE_ELTS 10  /**< Number of slots filled if more elements needed.
86  /*      So if the pool grows, it grows by EMP_MORE_ELTS*EMP_WID elements at a time. */
87   *  Number of slots filled if more elements needed.  # define EMP_MORE_BARS 500
88   *  So if the pool grows, it grows by EMP_MORE_ELTS*EMP_WID elements at a time.  /**< This is the number of pool bar slots to add during expansion.
  */  
 #define EMP_MORE_BARS 500  
 /* This is the number of pool bar slots to add during expansion.  
89     not all the slots will be filled immediately. */     not all the slots will be filled immediately. */
90    
91  /* This function is called at compiler startup time and destroy at shutdown. */  /**
92        This function is called at compiler startup time and destroy at shutdown.
93    */
94  void exprs_init_pool(void) {  void exprs_init_pool(void) {
95    if (g_exprs_pool != NULL ) {    if (g_exprs_pool != NULL ) {
96      Asc_Panic(2, NULL, "ERROR: exprs_init_pool called twice.\n");      Asc_Panic(2, NULL, "ERROR: exprs_init_pool called twice.\n");
# Line 105  void exprs_report_pool() Line 118  void exprs_report_pool()
118    pool_print_store(ASCERR,g_exprs_pool,0);    pool_print_store(ASCERR,g_exprs_pool,0);
119  }  }
120    
121  #define EPMALLOC ((struct Expr *)(pool_get_element(g_exprs_pool)))  # define EPMALLOC \
122  /* get a token. Token is the size of the struct struct Expr */      ((struct Expr *)(pool_get_element(g_exprs_pool)))
123  #define EPFREE(p) (pool_free_element(g_exprs_pool,((void *)p)))  /**< get a token. Token is the size of the struct struct Expr */
124  /* return a struct Expr */  
125    # define EPFREE(p) \
126        (pool_free_element(g_exprs_pool,((void *)p)))
127    /**< return a struct Expr */
128    
129  #else  # define EXPR_CHECK_MEMORY(VAR) \
130        AssertMemory(VAR)
131    
132    #else /* not using 'g_exprs_pool'... */
133    
 #define EPFREE(p) ascfree(p)  
 #define EPMALLOC (struct Expr *)ascmalloc(sizeof(struct Expr))  
134  void exprs_init_pool(void) {}  void exprs_init_pool(void) {}
135  void exprs_destroy_pool(void) {}  void exprs_destroy_pool(void) {}
136  void exprs_report_pool(void)  void exprs_report_pool(void){
137  {      FPRINTF(ASCERR,"ExprsPool not used at all\n");
     FPRINTF(ASCERR,"ExprsPool not used at all\n");  
138  }  }
139    
140    # define EPFREE(p) \
141        ASC_FREE(p)
142    
143    # define EPMALLOC \
144        ASC_NEW(struct Expr)
145    
146    # define EXPR_CHECK_MEMORY(VAR) \
147        AssertAllocatedMemory(result,sizeof(struct Expr))
148    
149  #endif  #endif
150    
151    
152    # define EXPR_NEW(VAR,TYPE) \
153        VAR = EPMALLOC; \
154        VAR->t = TYPE; \
155        VAR->next = NULL
156    
157    /*------------------------------------------------------------------------------
158      CREATION ROUTINES
159    */
160    
161  struct Expr *CreateVarExpr(struct Name *n)  struct Expr *CreateVarExpr(struct Name *n)
162  {  {
163    register struct Expr *result;    register struct Expr *result;
164    assert(n!=NULL);    assert(n!=NULL);
165    AssertMemory(n);    AssertMemory(n);
166    result = EPMALLOC;    EXPR_NEW(result,e_var);
   result->t = e_var;  
167    result->v.nptr = n;    result->v.nptr = n;
168    result->next = NULL;    EXPR_CHECK_MEMORY(result);
169  #if EXPRSUSESPOOL  
170    AssertMemory(result);    return result;
171  #else  }
172    AssertAllocatedMemory(result,sizeof(struct Expr));  
173  #endif  /*
174        As with var, store pointer to the name, but give it a special type 'e_diff'.
175    */
176    struct Expr *CreateDiffExpr(struct Name *n){
177      register struct Expr *result;
178      ERROR_REPORTER_HERE(ASC_PROG_ERR,"not yet implemented");
179      EXPR_NEW(result,e_diff);
180      result->v.nptr = n;
181      EXPR_CHECK_MEMORY(result);
182    return result;    return result;
183  }  }
184    
# Line 154  struct Expr *CreateOpExpr(enum Expr_enum Line 195  struct Expr *CreateOpExpr(enum Expr_enum
195  {  {
196    register struct Expr *result;    register struct Expr *result;
197    assert((t!=e_var)&&(t!=e_func)&&(t!=e_int)&&(t!=e_real)&&(t!=e_zero));    assert((t!=e_var)&&(t!=e_func)&&(t!=e_int)&&(t!=e_real)&&(t!=e_zero));
198    result = EPMALLOC;    EXPR_NEW(result,t);
   result->t = t;  
   result->next = NULL;  
199  #if EXPRSUSESPOOL  #if EXPRSUSESPOOL
200    AssertMemory(result);    AssertMemory(result);
201  #else  #else
# Line 169  struct Expr *CreateSatisfiedExpr(struct Line 208  struct Expr *CreateSatisfiedExpr(struct
208                                   CONST dim_type *dims)                                   CONST dim_type *dims)
209  {  {
210    register struct Expr *result;    register struct Expr *result;
211    result = EPMALLOC;    EXPR_NEW(result,e_satisfied);
   result->t = e_satisfied ;  
212    result->v.se.sen =  n;    result->v.se.sen =  n;
213    result->v.se.ser.rvalue = tol;    result->v.se.ser.rvalue = tol;
214    result->v.se.ser.dimensions = dims;    result->v.se.ser.dimensions = dims;
215    result->next = NULL;    EXPR_CHECK_MEMORY(result);
 #if EXPRSUSESPOOL  
   AssertMemory(result);  
 #else  
   AssertAllocatedMemory(result,sizeof(struct Expr));  
 #endif  
216    return result;    return result;
217  }  }
218    
219  struct Expr *CreateFuncExpr(CONST struct Func *f)  struct Expr *CreateFuncExpr(CONST struct Func *f)
220  {  {
221    register struct Expr *result;    register struct Expr *result;
222    result = EPMALLOC;    EXPR_NEW(result,e_func);
   result->t = e_func;  
223    result->v.fptr = f;    result->v.fptr = f;
224    result->next = NULL;    EXPR_CHECK_MEMORY(result);
 #if EXPRSUSESPOOL  
   AssertMemory(result);  
 #else  
   AssertAllocatedMemory(result,sizeof(struct Expr));  
 #endif  
225    return result;    return result;
226  }  }
227    
228  struct Expr *CreateIntExpr(long int i)  struct Expr *CreateIntExpr(long int i)
229  {  {
230    register struct Expr *result;    register struct Expr *result;
231    result = EPMALLOC;    EXPR_NEW(result,e_int);
   result->t = e_int;  
232    result->v.ivalue = i;    result->v.ivalue = i;
233    result->next = NULL;    EXPR_CHECK_MEMORY(result);
 #if EXPRSUSESPOOL  
   AssertMemory(result);  
 #else  
   AssertAllocatedMemory(result,sizeof(struct Expr));  
 #endif  
234    return result;    return result;
235  }  }
236    
237  struct Expr *CreateRealExpr(double r, CONST dim_type *dims)  struct Expr *CreateRealExpr(double r, CONST dim_type *dims)
238  {  {
239    register struct Expr *result;    register struct Expr *result;
240    result = EPMALLOC;    EXPR_NEW(result,e_real);
   result->t = e_real;  
241    result->v.r.rvalue = r;    result->v.r.rvalue = r;
242    result->v.r.dimensions = dims;    result->v.r.dimensions = dims;
243    result->next = NULL;    EXPR_CHECK_MEMORY(result);
 #if EXPRSUSESPOOL  
   AssertMemory(result);  
 #else  
   AssertAllocatedMemory(result,sizeof(struct Expr));  
 #endif  
244    return result;    return result;
245  }  }
246    
247  struct Expr *CreateTrueExpr(void)  struct Expr *CreateTrueExpr(void)
248  {  {
249    register struct Expr *result;    register struct Expr *result;
250    result = EPMALLOC;    EXPR_NEW(result,e_boolean);
   result->t = e_boolean;  
251    result->v.bvalue = 1;    result->v.bvalue = 1;
252    result->next = NULL;    EXPR_CHECK_MEMORY(result);
 #if EXPRSUSESPOOL  
   AssertMemory(result);  
 #else  
   AssertAllocatedMemory(result,sizeof(struct Expr));  
 #endif  
253    return result;    return result;
254  }  }
255    
256  struct Expr *CreateFalseExpr(void)  struct Expr *CreateFalseExpr(void)
257  {  {
258    register struct Expr *result;    register struct Expr *result;
259    result = EPMALLOC;    EXPR_NEW(result,e_boolean);
   result->t = e_boolean;  
260    result->v.bvalue = 0;    result->v.bvalue = 0;
261    result->next = NULL;    EXPR_CHECK_MEMORY(result);
 #if EXPRSUSESPOOL  
   AssertMemory(result);  
 #else  
   AssertAllocatedMemory(result,sizeof(struct Expr));  
 #endif  
262    return result;    return result;
263  }  }
264    
265  struct Expr *CreateAnyExpr(void)  struct Expr *CreateAnyExpr(void)
266  {  {
267    register struct Expr *result;    register struct Expr *result;
268    result = EPMALLOC;    EXPR_NEW(result,e_boolean);
   result->t = e_boolean;  
269    result->v.bvalue = 2;    result->v.bvalue = 2;
270    result->next = NULL;    EXPR_CHECK_MEMORY(result);
 #if EXPRSUSESPOOL  
   AssertMemory(result);  
 #else  
   AssertAllocatedMemory(result,sizeof(struct Expr));  
 #endif  
271    return result;    return result;
272  }  }
273    
274  struct Expr *CreateSetExpr(struct Set *set)  struct Expr *CreateSetExpr(struct Set *set)
275  {  {
276    register struct Expr *result;    register struct Expr *result;
277    result = EPMALLOC;    EXPR_NEW(result,e_set);
   result->t = e_set;  
278    result->v.s = set;    result->v.s = set;
279    result->next = NULL;    EXPR_CHECK_MEMORY(result);
 #if EXPRSUSESPOOL  
   AssertMemory(result);  
 #else  
   AssertAllocatedMemory(result,sizeof(struct Expr));  
 #endif  
280    return result;    return result;
281  }  }
282    
# Line 294  struct Expr *CreateSymbolExpr(symchar *s Line 285  struct Expr *CreateSymbolExpr(symchar *s
285  {  {
286    register struct Expr *result;    register struct Expr *result;
287    assert(AscFindSymbol(sym)!=NULL);    assert(AscFindSymbol(sym)!=NULL);
288    result = EPMALLOC;    EXPR_NEW(result,e_symbol);
   result->t = e_symbol;  
289    result->v.sym_ptr = sym;    result->v.sym_ptr = sym;
290    result->next = NULL;    EXPR_CHECK_MEMORY(result);
 #if EXPRSUSESPOOL  
   AssertMemory(result);  
 #else  
   AssertAllocatedMemory(result,sizeof(struct Expr));  
 #endif  
291    return result;    return result;
292  }  }
293    
294  struct Expr *CreateQStringExpr(CONST char *qstr)  struct Expr *CreateQStringExpr(CONST char *qstr)
295  {  {
296    register struct Expr *result;    register struct Expr *result;
297    result = EPMALLOC;    EXPR_NEW(result,e_qstring);
   result->t = e_qstring;  
298    result->v.sym_ptr = (symchar *)qstr; /* qstr really not symbol */    result->v.sym_ptr = (symchar *)qstr; /* qstr really not symbol */
299    result->next = NULL;    EXPR_CHECK_MEMORY(result);
 #if EXPRSUSESPOOL  
   AssertMemory(result);  
 #else  
   AssertAllocatedMemory(result,sizeof(struct Expr));  
 #endif  
300    return result;    return result;
301  }  }
302    
303  struct Expr *CreateBuiltin(enum Expr_enum t, struct Set *set)  struct Expr *CreateBuiltin(enum Expr_enum t, struct Set *set)
304  {  {
305    register struct Expr *result;    register struct Expr *result;
306    result = EPMALLOC;    EXPR_NEW(result,t);
   result->t = t;  
307    result->v.s = set;    result->v.s = set;
308    result->next = NULL;    EXPR_CHECK_MEMORY(result);
 #if EXPRSUSESPOOL  
   AssertMemory(result);  
 #else  
   AssertAllocatedMemory(result,sizeof(struct Expr));  
 #endif  
309    return result;    return result;
310  }  }
311    
312    /*------------------------------------------------------------------------------
313      MANIPULATION ROUTINES
314    */
315    
316  void LinkExprs(struct Expr *cur, struct Expr *next)  void LinkExprs(struct Expr *cur, struct Expr *next)
317  {  {
# Line 543  void DestroyExprList(struct Expr *e) Line 519  void DestroyExprList(struct Expr *e)
519      AssertMemory(ep);      AssertMemory(ep);
520      next = ep->next;      next = ep->next;
521      switch(ep->t) {      switch(ep->t) {
522      case e_var: DestroyName(ep->v.nptr); break;          case e_var:
523      case e_set:              DestroyName(ep->v.nptr);
524      case e_card:              break;
525      case e_choice:          case e_set:
526      case e_sum:          case e_card:
527      case e_prod:          case e_choice:
528      case e_union:          case e_sum:
529      case e_inter:          case e_prod:
530        DestroySetList(ep->v.s);          case e_union:
531        break;          case e_inter:
532      default: break;              DestroySetList(ep->v.s);
533                break;
534            default:
535                break;
536      }      }
537      EPFREE((char *)ep);      EPFREE((char *)ep);
538      ep = next;      ep = next;

Legend:
Removed from v.668  
changed lines
  Added in v.669

john.pye@anu.edu.au
ViewVC Help
Powered by ViewVC 1.1.22