/[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 710 - (show annotations) (download) (as text)
Thu Jun 29 08:53:37 2006 UTC (18 years ago) by johnpye
File MIME type: text/x-csrc
File size: 7470 byte(s)
Added my so-called 'quick fix' to external relation processing.
Still need to pursue corruption of efunc->etype pointer, for some
reason.
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 int i,n;
50 struct Instance *inst;
51 /* char *tmp; */
52 n = gl_length(args);
53 for(i = 1; i < n; ++i){
54 inst = (struct Instance *)gl_fetch(args,i);
55 /* tmp = WriteInstanceNameString(inst, NULL); */
56 CONSOLE_DEBUG("Argument %d: (%p)", i, inst);
57 /* ASC_FREE(tmp) */
58
59 }
60
61 ext = ASC_NEW(struct ExtCallNode);
62 CONSOLE_DEBUG("ASSIGNING efunc = %p TO ExtCallNode %p",efunc,ext);
63 ext->efunc = efunc;
64 ext->arglist = args;
65 if(data){
66 hndl = AddVarToTable(data,&added); /** FIX FIX FIX **/
67 }
68 ext->data = hndl;
69
70 if(subject){
71 pos = GetSubjectIndex(args,subject);
72 ext->subject = pos;
73 }else{
74 ext->subject = 0L;
75 }
76
77 ext->nodestamp = -1;
78 return ext;
79 }
80
81 void DestroyExtCall(struct ExtCallNode *ext, struct Instance *relinst){
82 struct Instance *ptr;
83 unsigned long len1, c1;
84 unsigned long len2, c2;
85 struct gl_list_t *arglist, *branch;
86
87 if (!ext) return;
88 arglist = ext->arglist;
89 if (arglist) {
90 len1 = gl_length(arglist);
91 for (c1=1;c1<=len1;c1++) {
92 branch = (struct gl_list_t *)gl_fetch(arglist,c1);
93 len2 = gl_length(branch);
94 for (c2=len2;c2>=1;c2--) {
95 if ( (ptr = (struct Instance *)gl_fetch(branch,c2)) !=NULL)
96 RemoveRelation(ptr,relinst);
97 }
98 gl_destroy(branch);
99 }
100 gl_destroy(arglist);
101 arglist = NULL;
102 }
103 }
104
105 struct Instance *GetSubjectInstance(struct gl_list_t *arglist,
106 unsigned long varndx
107 ){
108 struct Instance *arg;
109 struct gl_list_t *branch;
110 unsigned long len1,c=1L,len2,count=0L;
111 long safetycheck;
112
113 if (arglist&&varndx) {
114 len1 = gl_length(arglist);
115 while(c<=len1){
116 branch = (struct gl_list_t *)gl_fetch(arglist,c);
117 if (!branch) return NULL;
118 len2 = gl_length(branch);
119 count += len2;
120 if (count>=varndx){
121 safetycheck = len2-count+varndx;
122 if (safetycheck<=0){
123 ERROR_REPORTER_HERE(ASC_PROG_ERR,"Something really wrong (%s)",__FUNCTION__);
124 return NULL;
125 }
126 arg = (struct Instance *)gl_fetch(branch,(unsigned long)safetycheck);
127 return arg;
128 }
129 c++;
130 }
131 }
132 return NULL;
133 }
134
135 unsigned long GetSubjectIndex(struct gl_list_t *arglist,
136 struct Instance *subject
137 ){
138 unsigned long len1,c1,len2,c2;
139 struct gl_list_t *branch;
140 struct Instance *arg;
141 unsigned long count=0L;
142
143 if (arglist&&subject){
144 len1 = gl_length(arglist);
145 for(c1=1;c1<=len1;c1++){
146 branch = (struct gl_list_t *)gl_fetch(arglist,c1);
147 if(branch==NULL){
148 ERROR_REPORTER_HERE(ASC_PROG_ERR,"Found null branch");
149 return 0L; /* error */
150 }
151 len2 = gl_length(branch);
152 for(c2=1;c2<=len2;c2++){
153 count++;
154 arg = (struct Instance *)gl_fetch(branch,c2);
155 if (arg==subject){
156 return count;
157 }
158 }
159 }
160 ERROR_REPORTER_HERE(ASC_PROG_ERR,"Reached impossible place");
161 return 0L; /*NOTREACHED*/
162 }
163 return 0L;
164 }
165
166 unsigned long CountNumberOfArgs(struct gl_list_t *arglist,
167 unsigned long start, unsigned long end
168 ){
169 unsigned long c,count=0L;
170 struct gl_list_t *branch;
171
172 if(arglist){
173 assert(start<=end);
174 for (c=start;c<=end;c++){
175 branch = (struct gl_list_t *)gl_fetch(arglist,c);
176 if(branch==NULL){
177 ERROR_REPORTER_HERE(ASC_PROG_ERR,"Found null branch");
178 return 0L; /*error*/
179 }
180 count += gl_length(branch);
181 }
182 return count;
183 }
184 return 0L;
185 }
186
187 struct gl_list_t *LinearizeArgList(struct gl_list_t *arglist,
188 unsigned long start, unsigned long end
189 ){
190 struct gl_list_t *result,*branch;
191 struct Instance *arg;
192 unsigned long c1,len2,c2;
193
194 if (arglist){
195 assert(start<=end);
196 result = gl_create(20L);
197 for(c1=start;c1<=end;c1++){
198 branch = (struct gl_list_t *)gl_fetch(arglist,c1);
199 if (!branch){
200 gl_destroy(result);
201 return NULL;
202 }
203 len2 = gl_length(branch);
204 for(c2=1;c2<=len2;c2++){
205 arg = (struct Instance *)gl_fetch(branch,c2);
206 gl_append_ptr(result,(VOIDPTR)arg);
207 }
208 }
209 return result;
210 }
211 return NULL;
212 }
213
214 void DestroySpecialList(struct gl_list_t *list)
215 {
216 unsigned long len,c;
217 struct gl_list_t *branch,*tmp;
218 if (list) {
219 tmp = list;
220 len = gl_length(tmp);
221 for (c=1;c<=len;c++) {
222 branch = (struct gl_list_t *)gl_fetch(tmp,c);
223 if (branch != NULL) {
224 gl_destroy(branch);
225 }
226 }
227 gl_destroy(tmp);
228 list = NULL;
229 }
230 }
231
232 struct gl_list_t *CopySpecialList(struct gl_list_t *list){
233 unsigned long len1,c1,len2,c2;
234 struct gl_list_t *result,*branch,*tmp;
235 struct Instance *arg;
236 if (list) {
237 len1 = gl_length(list);
238 result = gl_create(len1);
239 for(c1=1;c1<=len1;c1++) {
240 tmp = (struct gl_list_t *)gl_fetch(list,c1);
241 if (tmp) {
242 len2 = gl_length(tmp);
243 branch = gl_create(len2);
244 for (c2=1;c2<=len2;c2++) {
245 arg = (struct Instance *)gl_fetch(tmp,c2);
246 gl_append_ptr(branch,(VOIDPTR)arg);
247 }
248 }
249 else{
250 DestroySpecialList(result);
251 return NULL;
252 }
253 gl_append_ptr(result,(VOIDPTR)branch);
254 }
255 return result;
256 }
257 return NULL;
258 }
259
260 struct ExternalFunc *ExternalCallExtFuncF(struct ExtCallNode *ext){
261 return ext->efunc;
262 }
263
264 struct gl_list_t *ExternalCallArgListF(struct ExtCallNode *ext){
265 return ext->arglist;
266 }
267
268 struct Instance *ExternalCallDataInstance(struct ExtCallNode *ext){
269 struct Instance **hndl;
270 hndl = ext->data;
271 if(hndl!=NULL){
272 /*
273 ExtCallNode::data is an array of pointers to Instance
274 structures. We are here returning the first pointer from that array.
275 */
276 return *hndl;
277 }else{
278 return NULL;
279 }
280 }
281
282 int ExternalCallNodeStampF(struct ExtCallNode *ext){
283 return ext->nodestamp;
284 }
285
286 void SetExternalCallNodeStamp(struct ExtCallNode *ext, int nodestamp){
287 if (ext->nodestamp==-1) {
288 ext->nodestamp = nodestamp;
289 }
290 }
291
292 unsigned long ExternalCallVarIndexF(struct ExtCallNode *ext){
293 return ext->subject;
294 }
295
296 struct Instance *ExternalCallVarInstance(struct ExtCallNode *ext){
297 struct Instance *i;
298 assert(ext->subject);
299 i = GetSubjectInstance(ext->arglist,ext->subject);
300 assert(i!=NULL);
301 return i;
302 }
303

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