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

Contents of /trunk/base/generic/compiler/extcall.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 708 - (show annotations) (download) (as text)
Tue Jun 27 07:34:31 2006 UTC (17 years, 11 months ago) by johnpye
File MIME type: text/x-csrc
File size: 6778 byte(s)
Replaced some references to ascmalloc with ASC_NEW_ARRAY
1 /* ASCEND modelling environment
2 Copyright (C) 2006 Carnegie Mellon University
3 Copyright (C) 1990, 1993, 1994 Thomas Guthrie Epperly, Kirk Andre Abbott
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
8 any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
19 *//*
20 by Kirk Andre Abbott
21 Created: Jun 1, 1995.
22 Last in CVS: $Revision: 1.9 $ $Date: 1998/02/24 21:44:42 $ $Author: ballan $
23 */
24
25 #include <utilities/ascConfig.h>
26 #include <utilities/ascMalloc.h>
27 #include <utilities/error.h>
28 #include "compiler.h"
29 #include <general/list.h>
30 #include "fractions.h"
31 #include "dimen.h"
32 #include "functype.h"
33 #include "expr_types.h"
34 #include "extinst.h"
35 #include "mathinst.h"
36 #include "extcall.h"
37
38 struct ExtCallNode *CreateExtCall(struct ExternalFunc *efunc,
39 struct gl_list_t *args,
40 struct Instance *subject,
41 struct Instance *data)
42 {
43 struct ExtCallNode *ext;
44 struct Instance **hndl=NULL;
45 unsigned long pos;
46 int added=0;
47
48 CONSOLE_DEBUG("...");
49
50 ext = ASC_NEW(struct ExtCallNode);
51 ext->efunc = efunc;
52 ext->arglist = args;
53 if (data) {
54 hndl = AddVarToTable(data,&added); /** FIX FIX FIX **/
55 }
56 ext->data = hndl;
57 if (subject) {
58 pos = GetSubjectIndex(args,subject);
59 ext->subject = pos;
60 } else {
61 ext->subject = 0L;
62 }
63 ext->nodestamp = -1;
64 return ext;
65 }
66
67 void DestroyExtCall(struct ExtCallNode *ext, struct Instance *relinst)
68 {
69 struct Instance *ptr;
70 unsigned long len1, c1;
71 unsigned long len2, c2;
72 struct gl_list_t *arglist, *branch;
73
74 if (!ext) return;
75 arglist = ext->arglist;
76 if (arglist) {
77 len1 = gl_length(arglist);
78 for (c1=1;c1<=len1;c1++) {
79 branch = (struct gl_list_t *)gl_fetch(arglist,c1);
80 len2 = gl_length(branch);
81 for (c2=len2;c2>=1;c2--) {
82 if ( (ptr = (struct Instance *)gl_fetch(branch,c2)) !=NULL)
83 RemoveRelation(ptr,relinst);
84 }
85 gl_destroy(branch);
86 }
87 gl_destroy(arglist);
88 arglist = NULL;
89 }
90 }
91
92 struct Instance *GetSubjectInstance(struct gl_list_t *arglist,
93 unsigned long varndx
94 ){
95 struct Instance *arg;
96 struct gl_list_t *branch;
97 unsigned long len1,c=1L,len2,count=0L;
98 long safetycheck;
99
100 if (arglist&&varndx) {
101 len1 = gl_length(arglist);
102 while(c<=len1){
103 branch = (struct gl_list_t *)gl_fetch(arglist,c);
104 if (!branch) return NULL;
105 len2 = gl_length(branch);
106 count += len2;
107 if (count>=varndx){
108 safetycheck = len2-count+varndx;
109 if (safetycheck<=0){
110 ERROR_REPORTER_HERE(ASC_PROG_ERR,"Something really wrong (%s)",__FUNCTION__);
111 return NULL;
112 }
113 arg = (struct Instance *)gl_fetch(branch,(unsigned long)safetycheck);
114 return arg;
115 }
116 c++;
117 }
118 }
119 return NULL;
120 }
121
122 unsigned long GetSubjectIndex(struct gl_list_t *arglist,
123 struct Instance *subject)
124 {
125 unsigned long len1,c1,len2,c2;
126 struct gl_list_t *branch;
127 struct Instance *arg;
128 unsigned long count=0L;
129
130 if (arglist&&subject){
131 len1 = gl_length(arglist);
132 for(c1=1;c1<=len1;c1++){
133 branch = (struct gl_list_t *)gl_fetch(arglist,c1);
134 if (!branch) return 0L; /* error */
135 len2 = gl_length(branch);
136 for(c2=1;c2<=len2;c2++){
137 count++;
138 arg = (struct Instance *)gl_fetch(branch,c2);
139 if (arg==subject)
140 return count;
141 }
142 }
143 return 0L; /*NOTREACHED*/
144 }
145 return 0L;
146 }
147
148 unsigned long CountNumberOfArgs(struct gl_list_t *arglist,
149 unsigned long start, unsigned long end)
150 {
151 unsigned long c,count=0L;
152 struct gl_list_t *branch;
153
154 if (arglist) {
155 assert(start<=end);
156 for (c=start;c<=end;c++){
157 branch = (struct gl_list_t *)gl_fetch(arglist,c);
158 if (!branch) return 0L; /*error*/
159 count += gl_length(branch);
160 }
161 return count;
162 }
163 else
164 return 0L;
165 }
166
167 struct gl_list_t *LinearizeArgList(struct gl_list_t *arglist,
168 unsigned long start, unsigned long end)
169 {
170 struct gl_list_t *result,*branch;
171 struct Instance *arg;
172 unsigned long c1,len2,c2;
173
174 if (arglist){
175 assert(start<=end);
176 result = gl_create(20L);
177 for(c1=start;c1<=end;c1++){
178 branch = (struct gl_list_t *)gl_fetch(arglist,c1);
179 if (!branch){
180 gl_destroy(result);
181 return NULL;
182 }
183 len2 = gl_length(branch);
184 for(c2=1;c2<=len2;c2++){
185 arg = (struct Instance *)gl_fetch(branch,c2);
186 gl_append_ptr(result,(VOIDPTR)arg);
187 }
188 }
189 return result;
190 }
191 return NULL;
192 }
193
194 void DestroySpecialList(struct gl_list_t *list)
195 {
196 unsigned long len,c;
197 struct gl_list_t *branch,*tmp;
198 if (list) {
199 tmp = list;
200 len = gl_length(tmp);
201 for (c=1;c<=len;c++) {
202 branch = (struct gl_list_t *)gl_fetch(tmp,c);
203 if (branch != NULL) {
204 gl_destroy(branch);
205 }
206 }
207 gl_destroy(tmp);
208 list = NULL;
209 }
210 }
211
212 struct gl_list_t *CopySpecialList(struct gl_list_t *list)
213 {
214 unsigned long len1,c1,len2,c2;
215 struct gl_list_t *result,*branch,*tmp;
216 struct Instance *arg;
217 if (list) {
218 len1 = gl_length(list);
219 result = gl_create(len1);
220 for(c1=1;c1<=len1;c1++) {
221 tmp = (struct gl_list_t *)gl_fetch(list,c1);
222 if (tmp) {
223 len2 = gl_length(tmp);
224 branch = gl_create(len2);
225 for (c2=1;c2<=len2;c2++) {
226 arg = (struct Instance *)gl_fetch(tmp,c2);
227 gl_append_ptr(branch,(VOIDPTR)arg);
228 }
229 }
230 else{
231 DestroySpecialList(result);
232 return NULL;
233 }
234 gl_append_ptr(result,(VOIDPTR)branch);
235 }
236 return result;
237 }
238 return NULL;
239 }
240
241 struct ExternalFunc *ExternalCallExtFuncF(struct ExtCallNode *ext)
242 {
243 return ext->efunc;
244 }
245
246 struct gl_list_t *ExternalCallArgListF(struct ExtCallNode *ext)
247 {
248 return ext->arglist;
249 }
250
251 struct Instance *ExternalCallDataInstance(struct ExtCallNode *ext)
252 {
253 struct Instance **hndl;
254 hndl = ext->data;
255 if (hndl)
256 return *hndl;
257 else
258 return NULL;
259 }
260
261 int ExternalCallNodeStampF(struct ExtCallNode *ext)
262 {
263 return ext->nodestamp;
264 }
265
266 void SetExternalCallNodeStamp(struct ExtCallNode *ext,
267 int nodestamp)
268 {
269 if (ext->nodestamp==-1) {
270 ext->nodestamp = nodestamp;
271 }
272 }
273
274 unsigned long ExternalCallVarIndexF(struct ExtCallNode *ext)
275 {
276 return ext->subject;
277 }
278
279 struct Instance *ExternalCallVarInstance(struct ExtCallNode *ext)
280 {
281 struct Instance *i;
282 assert(ext->subject);
283 i = GetSubjectInstance(ext->arglist,ext->subject);
284 assert(i!=NULL);
285 return i;
286 }
287

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