/[ascend]/trunk/ascend/compiler/forvars.c
ViewVC logotype

Contents of /trunk/ascend/compiler/forvars.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2781 - (show annotations) (download) (as text)
Fri Jun 20 05:12:07 2014 UTC (4 years, 2 months ago) by jpye
File MIME type: text/x-csrc
File size: 7240 byte(s)
remove RCS comments per warnings from clang 3.4
1 /*
2 * FOR Loop Index Variable Table
3 * by Tom Epperly
4 * Created: 1/14/89
5 * Version: $Revision: 1.11 $
6 * Version control file: $RCSfile: forvars.c,v $
7 * Date last modified: $Date: 1998/02/05 16:36:06 $
8 * Last modified by: $Author: ballan $
9 *
10 * This file is part of the Ascend Language Interpreter.
11 *
12 * Copyright (C) 1990, 1993, 1994 Thomas Guthrie Epperly
13 *
14 * The Ascend Language Interpreter is free software; you can redistribute
15 * it and/or modify it under the terms of the GNU General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
18 *
19 * The Ascend Language Interpreter is distributed in hope that it will be
20 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program. If not, see <http://www.gnu.org/licenses/>.
26 */
27
28 #include <ascend/general/platform.h>
29
30 #include <ascend/general/panic.h>
31 #include <ascend/general/ascMalloc.h>
32 #include <ascend/general/list.h>
33 #include "symtab.h"
34 #include "instance_enum.h"
35 #include "cmpfunc.h"
36 #include "forvars.h"
37 #include "setinstval.h"
38 #include "setinst_io.h"
39
40 #define FVMALLOC ForVarMalloc()
41
42 /* LIFO forvar list. contents of forvar recycle list are only the heads,
43 we do not save the contents of the var when it is destroyed. */
44 struct for_var_t *g_forvar_recycle_list = NULL;
45
46 static struct for_var_t *ForVarMalloc(void)
47 {
48 struct for_var_t *result;
49 if (g_forvar_recycle_list!=NULL) {
50 /* pop off list */
51 result = g_forvar_recycle_list;
52 g_forvar_recycle_list = (struct for_var_t *)g_forvar_recycle_list->name;
53 return result;
54 } else {
55 return ASC_NEW(struct for_var_t);
56 }
57 }
58
59 struct for_table_t *CreateForTable(void)
60 {
61 register struct for_table_t *result;
62 result =gl_create(4L);
63 AssertMemory(result);
64 return result;
65 }
66
67 static FILE *g_forvarfile = NULL;
68 /* writes ' name = value,' for all cases */
69 static void WriteForVar(FILE *fp, struct for_var_t *fv)
70 {
71 if (fv!=NULL){
72 AssertAllocatedMemory(fv,sizeof(struct for_var_t ));
73 switch (fv->t) {
74 case f_set:
75 FPRINTF(fp," %s = [",SCP(fv->name));
76 WriteInstSet(fp,fv->value.sptr);
77 FPRINTF(fp,"],");
78 break;
79 case f_integer:
80 FPRINTF(fp," %s = %lu,",SCP(fv->name),fv->value.ivalue);
81 break;
82 case f_symbol:
83 FPRINTF(fp," %s = %s,",SCP(fv->name),SCP(fv->value.sym_ptr));
84 break;
85 default:
86 FPRINTF(fp," %s = UNDEFINED,",SCP(fv->name));
87 }
88 } else {
89 FPRINTF(fp,"NULL FOR indices!\n");
90 }
91 }
92
93 static
94 void WriteForVarIterate(struct for_var_t *ft)
95 {
96 WriteForVar(g_forvarfile,ft);
97 }
98
99 void WriteForTable(FILE *fp,struct gl_list_t *ft)
100 {
101 if(ft!=NULL && fp!=NULL){
102 g_forvarfile = fp;
103 AssertMemory(ft);
104 gl_iterate(ft,(void (*)(VOIDPTR))WriteForVarIterate);
105 }
106 }
107
108 void DestroyForTable(struct gl_list_t *ft)
109 {
110 if(ft!=NULL){
111 AssertMemory(ft);
112 gl_iterate(ft,(void (*)(VOIDPTR))DestroyForVar);
113 gl_destroy(ft);
114 }
115 }
116
117 unsigned long ActiveForLoops(CONST struct gl_list_t *ft)
118 {
119 assert(ft!=NULL);
120 AssertMemory(ft);
121 return gl_length(ft);
122 }
123
124 void AddLoopVariable(struct gl_list_t *ft, struct for_var_t *var)
125 {
126 assert(ft && var);
127 AssertMemory(ft);
128 AssertMemory(var);
129 gl_append_ptr(ft,(VOIDPTR)var);
130 }
131
132 struct for_var_t *LoopIndex(CONST struct gl_list_t *ft, unsigned long int num)
133 {
134 assert(ft!=NULL);
135 AssertMemory(ft);
136 return (struct for_var_t *)gl_fetch(ft,num);
137 }
138
139 /*
140 * This function is used in FindForVar only. It returns 0 for
141 * an exact match and 1 OTHERWISE, so it is not appropriate
142 * for sorting lists. Forvar names are assumed to come from
143 * the symbol table, as the symchar implies.
144 */
145 static
146 int CmpForVars(struct for_var_t *fv1, struct for_var_t *fv2)
147 {
148 assert(fv1 && fv2);
149 /* return CmpSymchar(fv1->name,fv2->name); */
150 return (fv1->name != fv2->name);
151 }
152
153 struct for_var_t *FindForVar(CONST struct gl_list_t *ft, symchar *name)
154 {
155 unsigned long pos;
156 struct for_var_t match;
157 assert(ft && name);
158 AssertMemory(ft);
159 match.name = name;
160 pos = gl_search(ft,(char *)&match,(CmpFunc)CmpForVars); /* linear */
161 if (pos != 0) {
162 return (struct for_var_t *)gl_fetch(ft,pos);
163 }
164 return NULL;
165 }
166
167 void RemoveForVariable(struct gl_list_t *ft)
168 {
169 unsigned long length;
170 struct for_var_t *fv;
171 length = gl_length(ft);
172 if (length>0){
173 fv = (struct for_var_t *)gl_fetch(ft,length);
174 DestroyForVar(fv);
175 gl_delete(ft,length,0);
176 }
177 }
178
179 /*********************************************************************\
180 Routines to process for_var_t types.
181 \*********************************************************************/
182
183 struct for_var_t *CreateForVar(symchar *name)
184 {
185 register struct for_var_t *result;
186 assert(AscFindSymbol(name)!=NULL);
187 result = FVMALLOC;
188 result->t = f_untyped;
189 result->name = name;
190 AssertAllocatedMemory(result,sizeof(struct for_var_t));
191 return result;
192 }
193
194 void SetForVarType(struct for_var_t *fv, enum for_kind t)
195 {
196 assert(fv && (fv->t==f_untyped));
197 AssertAllocatedMemory(fv,sizeof(struct for_var_t ));
198 fv->t = t;
199 }
200
201 void SetForInteger(struct for_var_t *fv, long int ivalue)
202 {
203 assert(fv && (fv->t==f_integer));
204 AssertAllocatedMemory(fv,sizeof(struct for_var_t ));
205 fv->value.ivalue = ivalue;
206 }
207
208 void SetForSymbol(struct for_var_t *fv, symchar *sym_ptr)
209 {
210 assert(fv && (fv->t==f_symbol));
211 AssertAllocatedMemory(fv,sizeof(struct for_var_t ));
212 assert(AscFindSymbol(sym_ptr)!=NULL);
213 fv->value.sym_ptr = sym_ptr;
214 }
215
216 void SetForSet(struct for_var_t *fv, struct set_t *sptr)
217 {
218 assert(fv && (fv->t == f_set));
219 AssertAllocatedMemory(fv,sizeof(struct for_var_t ));
220 fv->value.sptr = sptr;
221 }
222
223 enum for_kind GetForKind(CONST struct for_var_t *fv)
224 {
225 assert(fv!=NULL);
226 AssertAllocatedMemory(fv,sizeof(struct for_var_t ));
227 return fv->t;
228 }
229
230 symchar *GetForName(CONST struct for_var_t *fv)
231 {
232 assert(fv!=NULL);
233 AssertAllocatedMemory(fv,sizeof(struct for_var_t ));
234 return fv->name;
235 }
236
237 long GetForInteger(CONST struct for_var_t *fv)
238 {
239 assert(fv && (fv->t == f_integer));
240 AssertAllocatedMemory(fv,sizeof(struct for_var_t ));
241 return fv->value.ivalue;
242 }
243
244 symchar *GetForSymbol(CONST struct for_var_t *fv)
245 {
246 assert(fv && (fv->t == f_symbol));
247 AssertAllocatedMemory(fv,sizeof(struct for_var_t ));
248 return fv->value.sym_ptr;
249 }
250
251 CONST struct set_t *GetForSet(CONST struct for_var_t *fv)
252 {
253 assert(fv && (fv->t == f_set));
254 AssertAllocatedMemory(fv,sizeof(struct for_var_t ));
255 return fv->value.sptr;
256 }
257
258 void DestroyForVar(struct for_var_t *fv)
259 {
260 if (fv!=NULL){
261 AssertAllocatedMemory(fv,sizeof(struct for_var_t ));
262 if (fv->t == f_set) DestroySet(fv->value.sptr);
263 /* push on list */
264 fv->name = (symchar *)g_forvar_recycle_list; /* push on stack */
265 g_forvar_recycle_list = fv;
266 }
267 }
268
269 int ClearForVarRecycle(void)
270 {
271 struct for_var_t *tmp;
272 int len=0;
273 while (g_forvar_recycle_list!=NULL) {
274 tmp = g_forvar_recycle_list;
275 g_forvar_recycle_list = (struct for_var_t *)g_forvar_recycle_list->name;
276 ascfree((char *)tmp);
277 len++;
278 }
279 return len;
280 }

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