Add ALLOCAN() and ALLOCANZ().
[libfirm] / ir / external / read.c
1 /*
2  * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
6  * This file may be distributed and/or modified under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation and appearing in the file LICENSE.GPL included in the
9  * packaging of this file.
10  *
11  * Licensees holding valid libFirm Professional Edition licenses may use
12  * this file in accordance with the libFirm Commercial License.
13  * Agreement provided with the Software.
14  *
15  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief     Read descriptions of external effects
23  * @author    Florian, Boris Boesler
24  * @date      11.10.2004
25  * @version   $Id$
26  */
27 #include "config.h"
28
29 #include <stdlib.h>
30 #include <string.h>
31 #include <assert.h>
32
33 #include <libxml/xmlmemory.h>
34 #include <libxml/parser.h>
35 #include <libxml/encoding.h>
36
37 #include "read_t.h"
38 #include "read.h"
39 #include "irprog.h"
40 #include "irgraph.h"
41 #include "pseudo_irg.h"
42 #include "ircons.h"
43 #include "irmode.h"
44 #include "irdump.h"
45 #include "irvrfy.h"
46 #include "typerep.h"
47 #include "tv.h"
48 #include "xmalloc.h"
49
50 # define MY_ENCODING "ISO-8859-1"
51
52 # define CHECK(ptr,msg)     assert (ptr && msg)
53
54 # define NODE_NAME(n, m) (0 == xmlStrcmp (n->name, (const xmlChar*) #m))
55 # define CHECK_NAME(n, m) assert (0 == xmlStrcmp (n->name, (const xmlChar*) #m))
56
57
58 #define VERBOSE_PRINTING 0
59
60 #if VERBOSE_PRINTING
61 # define VERBOSE_PRINT(s) fprintf s
62 #else
63 # define VERBOSE_PRINT(s)
64 #endif
65
66 #define NO_ID NULL
67
68 static type_t *types = NULL;
69 static entity_t *entities = NULL;
70 static proc_t *procs = NULL;
71 static module_t *modules = NULL;
72
73 /* @@@ HACK */
74 static module_t *current_module = NULL;
75
76 #if VERBOSE_PRINTING
77 /* this is only used inside a VERBOSE_PRINT() call */
78 static const char *effect_string[] = {
79   "arg",
80   "valref",
81   "select",
82   "load",
83   "store",
84   "alloc",
85   "call",
86   "unknown",
87   "join",
88   "raise",
89   "ret"
90 };
91 #endif /* defined VERBOSE_PRINTING */
92
93 static ident*
94 getNodeModuleIdent (xmlNodePtr node)
95 {
96   const char *mod_str = (const char*) xmlGetProp (node, BAD_CAST "module");
97
98   if (NULL == mod_str) {
99     return (NULL);
100   } else {
101     ident *res = new_id_from_str (mod_str);
102     return (res);
103   }
104 }
105
106 static const char*
107 getNodeProcName (xmlNodePtr node)
108 {
109   const char *proc_str = (const char*) xmlGetProp (node, BAD_CAST "procname");
110   assert (proc_str);
111   return (proc_str);
112 }
113
114 # ifdef NEEDED
115 static char*
116 getNodeClassName (xmlNodePtr node)
117 {
118   char *proc_str = (char*) xmlGetProp (node, BAD_CAST "class");
119   assert (proc_str);
120   return ( (proc_str));
121 }
122 # endif /* defined NEEDED */
123
124 static const char*
125 getNodeId (xmlNodePtr node)
126 {
127   const char *id_str = (const char*) xmlGetProp (node, BAD_CAST "id");
128   assert (id_str);
129   return (id_str);
130 }
131
132 static const char *
133 getNodeRefId (xmlNodePtr node)
134 {
135   const char *refid_str = (char*) xmlGetProp (node, BAD_CAST "refid");
136   assert (refid_str);
137   return ((refid_str));
138 }
139
140 static const char*
141 getNodeTypeId (xmlNodePtr node)
142 {
143   const char *type_str = (char*) xmlGetProp (node, BAD_CAST "type");
144   assert (type_str);
145   return ((type_str));
146 }
147
148 static const char
149 *getNodeTypeStr (xmlNodePtr node)
150 {
151   const char *type_str = (char*) xmlGetProp (node, BAD_CAST "type");
152   assert (type_str);
153   return (type_str);
154 }
155
156 static const char*
157 getNodeOwnerStr (xmlNodePtr node)
158 {
159   const char *owner_str = (char*) xmlGetProp (node, BAD_CAST "owner");
160   assert (owner_str);
161   return (owner_str);
162 }
163
164 static const char
165 *getNodeEntityStr (xmlNodePtr node)
166 {
167   const char *ent_str = (char*) xmlGetProp (node, BAD_CAST "ir_entity");
168   assert (ent_str);
169
170   return (ent_str);
171 }
172
173
174 /*
175   was Public Interface
176 */
177 # ifdef NEEDED
178 static
179 type_t *getTypeByIdent (ident *id)
180 {
181   type_t *curr = types; // @@@ TODO module -> types
182
183   while (NULL != curr) {
184     if (id == curr -> type_ident) {
185       return (curr);
186     }
187     curr = curr->prev;
188   }
189
190   return (NULL);
191 }
192 # endif /* defined NEEDED */
193
194 # ifdef NEEDED
195 static
196 type_t *getTypeById (ident *id)
197 {
198   type_t *curr = types; // which ones?
199
200   while (NULL != curr) {
201     if (id == curr -> id) {
202       return (curr);
203     }
204     curr = curr->prev;
205   }
206
207   return (NULL);
208 }
209 # endif /* defined NEEDED */
210
211 # ifdef NEEDED
212 static
213 entity_t *getEntityByIdents (ident *name, ident *tp_ident)
214 {
215   entity_t *curr = entities; // TODO module -> entities
216
217   while (NULL != curr) {
218     if ((name == curr -> ent_ident)
219     && (tp_ident == curr -> tp_ident)) {
220       return (curr);
221     }
222     curr = curr->prev;
223   }
224
225   return (NULL);
226 }
227 # endif /* defined NEEDED */
228
229 static
230 entity_t *getEntityById (ident *id)
231 {
232   entity_t *curr = entities;
233
234   while (NULL != curr) {
235     if (id == curr -> id) {
236       return (curr);
237     }
238     curr = curr->prev;
239   }
240
241   return (NULL);
242 }
243
244 # ifdef NEEDED
245 static
246 proc_t *getEffectByName (ident *proc_ident)
247 {
248   proc_t *curr_effs = procs;
249
250   while (NULL != curr_effs) {
251     if (proc_ident == curr_effs -> proc_ident) {
252       return (curr_effs);
253     }
254     curr_effs = curr_effs->next;
255   }
256
257   return (NULL);
258 }
259 # endif /* defined NEEDED */
260
261 static
262 xmlNodePtr get_any_valid_child(xmlNodePtr elem)
263 {
264   xmlNodePtr child;
265
266   assert(elem && "no element");
267   child = elem -> xmlChildrenNode;
268   while(child && (NODE_NAME (child, comment))) {
269     child = child -> next;
270   }
271   return(child);
272 }
273
274 static
275 xmlNodePtr get_valid_child(xmlNodePtr elem)
276 {
277   xmlNodePtr child;
278
279   child = get_any_valid_child(elem);
280   assert(child && "lost child in deep black forest");
281   return(child);
282 }
283
284 /*
285  * parse XML structure and construct an additional structure
286  */
287 static eff_t *
288 parseArg (xmlDocPtr doc, xmlNodePtr argelm)
289 {
290   const char *id;
291   const char *typeid;
292   int num;
293   char *num_str;
294   eff_t *arg;
295   (void) doc;
296
297   CHECK_NAME (argelm, arg);
298   VERBOSE_PRINT ((stdout, "arg node \t0x%08x\n", (int) argelm));
299
300   id = getNodeId (argelm);
301   VERBOSE_PRINT ((stdout, "arg->id = \"%s\"\n", id));
302   num_str = (char*) xmlGetProp (argelm, BAD_CAST "number");
303   num = atoi (num_str);
304   VERBOSE_PRINT ((stdout, "arg->no = \"%d\"\n", num));
305
306   typeid = getNodeTypeStr (argelm);
307
308   arg = XMALLOC(eff_t);
309   arg -> kind = eff_arg;
310   arg -> id = new_id_from_str(id);
311   arg -> effect.arg.num = num;
312   arg -> effect.arg.type_ident = new_id_from_str(typeid);
313
314   return (arg);
315 }
316
317 static eff_t*
318 parseValref (xmlDocPtr doc, xmlNodePtr valelm)
319 {
320   const char *ref_id;
321   eff_t *valref;
322   (void) doc;
323
324   CHECK_NAME (valelm, valref);
325   VERBOSE_PRINT ((stdout, "valref node \t0x%08x\n", (int) valelm));
326
327   ref_id = getNodeRefId (valelm);
328   VERBOSE_PRINT ((stdout, "val->refid = \"%s\"\n", ref_id));
329
330   valref = XMALLOC(eff_t);
331   valref->kind = eff_valref;
332   valref-> id = new_id_from_str(ref_id);
333
334   return (valref);
335 }
336
337 static eff_t*
338 parseSelect (xmlDocPtr doc, xmlNodePtr selelm)
339 {
340   ident *entity_id = new_id_from_str(getNodeEntityStr (selelm));
341   entity_t *ent;
342   xmlNodePtr child;
343   eff_t *valref = NULL;
344   eff_t *sel = XMALLOC(eff_t);
345   sel->kind = eff_select;
346
347   CHECK_NAME (selelm, select);
348   VERBOSE_PRINT ((stdout, "select node \t0x%08x\n", (int) selelm));
349
350   ent = getEntityById (entity_id);
351   assert(ent && "ir_entity not found");
352   VERBOSE_PRINT ((stdout, "select ir_entity %s\n", get_id_str(ent -> ent_ident)));
353
354   child = selelm->xmlChildrenNode;
355
356   if (child) {
357     valref = parseValref (doc, child);
358   }
359
360   sel-> id = valref ? valref-> id : NO_ID;
361   sel-> effect.select.ent = ent;
362
363   if (valref) {
364     free (valref);
365   }
366
367   return (sel);
368 }
369
370 static eff_t*
371 parseLoad (xmlDocPtr doc, xmlNodePtr loadelm)
372 {
373   ident *id;
374   xmlNodePtr child;
375   eff_t *sel;
376   eff_t *load = XMALLOC(eff_t);
377   load->kind = eff_load;
378
379   CHECK_NAME (loadelm, load);
380   VERBOSE_PRINT ((stdout, "load node \t0x%08x\n", (int) loadelm));
381   id = new_id_from_str(getNodeId (loadelm));
382
383   child = get_valid_child(loadelm);
384   if(NODE_NAME (child, select)) {
385     sel = parseSelect (doc, child);
386     load-> effect.load.ent = sel-> effect.select.ent;
387     VERBOSE_PRINT ((stdout, "load ir_entity \t%s\n",
388             get_id_str(load -> effect.load.ent -> ent_ident)));
389   }
390   else {
391     sel = parseValref (doc, child);
392     load-> effect.load.ent = NULL;
393   }
394
395   load-> id = id;
396   load-> effect.load.ptrrefid = sel-> id;
397
398   free (sel);
399
400   return (load);
401 }
402
403 static eff_t*
404 parseStore (xmlDocPtr doc, xmlNodePtr storeelm)
405 {
406   xmlNodePtr child;
407   eff_t *sel;
408   eff_t *valref;
409   eff_t *store = XMALLOC(eff_t);
410   store->kind = eff_store;
411
412   CHECK_NAME (storeelm, store);
413   VERBOSE_PRINT ((stdout, "store node \t0x%08x\n", (int) storeelm));
414
415   child = get_valid_child(storeelm);
416   if(NODE_NAME (child, select)) {
417     sel = parseSelect (doc, child);
418     store-> effect.store.ent = sel-> effect.select.ent;
419   }
420   else {
421     sel = parseValref (doc, child);
422     store-> effect.store.ent = NULL;
423   }
424
425   child = child->next;
426   valref = parseValref (doc, child);
427
428   store-> effect.store.ptrrefid = sel-> id;
429   store-> effect.store.valrefid = valref-> id;
430
431   free (sel);
432   free (valref);
433
434   return (store);
435 }
436
437 static eff_t*
438 parseAlloc (xmlDocPtr doc, xmlNodePtr allocelm)
439 {
440   ident *id;
441   ident *type_id;
442   eff_t *alloc = XMALLOC(eff_t); /* ...! */
443   alloc->kind = eff_alloc;
444   (void) doc;
445
446   CHECK_NAME (allocelm, alloc);
447   VERBOSE_PRINT ((stdout, "alloc node \t0x%08x\n", (int) allocelm));
448   id = new_id_from_str(getNodeId (allocelm));
449   VERBOSE_PRINT ((stdout, "alloc->id = \"%s\"\n", get_id_str(id)));
450   type_id = new_id_from_str(getNodeTypeId (allocelm));
451   VERBOSE_PRINT ((stdout, "alloc->type_id = \"%s\"\n", get_id_str(type_id)));
452
453   alloc-> id = id;
454   alloc-> effect.alloc.tp_id = type_id;
455
456   return (alloc);
457 }
458
459 static eff_t*
460 parseCall (xmlDocPtr doc, xmlNodePtr callelm)
461 {
462   ident *id;
463   xmlNodePtr child;
464   eff_t *sel;
465   xmlNodePtr arg;
466   int n_args;
467   eff_t *call = XMALLOC(eff_t);
468   call->kind = eff_call;
469
470   CHECK_NAME (callelm, call);
471   VERBOSE_PRINT ((stdout, "call node \t0x%08x\n", (int) callelm));
472   id = new_id_from_str(getNodeId (callelm));
473   VERBOSE_PRINT ((stdout, "call->id = \"%s\"\n", get_id_str(id)));
474
475   child = get_valid_child(callelm);
476   if(NODE_NAME (child, select)) {
477     sel = parseSelect (doc, child);
478     call-> effect.call.ent = sel-> effect.select.ent;
479   }
480   else {
481     sel = parseValref (doc, child);
482     call-> effect.call.ent = NULL;
483   }
484
485   arg = child = child->next;
486   n_args = 0;
487
488   while (NULL != child) {
489     n_args ++;
490     child = child->next;
491   }
492
493   call-> id = id;
494   call-> effect.call.valrefid = sel-> id;
495   call-> effect.call.n_args = n_args;
496   call-> effect.call.args = NULL;
497
498   free (sel);
499
500   if (0 != n_args) {
501     ident **args = XMALLOCN(ident*, n_args);
502     int i = 0;
503
504     while (NULL != arg) {
505       eff_t *valref = parseValref (doc, arg);
506       args [i ++] = valref-> id;
507       free (valref);
508       arg = arg->next;
509     }
510
511     call-> effect.call.args = args;
512   }
513
514   return (call);
515 }
516
517 static eff_t*
518 parseJoin (xmlDocPtr doc, xmlNodePtr joinelm)
519 {
520   ident *id;
521   int n_ins;
522   ident **ins;
523   int i;
524   xmlNodePtr child;
525   eff_t *join = XMALLOC(eff_t);
526   join->kind = eff_join;
527
528   CHECK_NAME (joinelm, join);
529   VERBOSE_PRINT ((stdout, "join node \t0x%08x\n", (int) joinelm));
530   id = new_id_from_str(getNodeId (joinelm));
531   VERBOSE_PRINT ((stdout, "join->id = \"%s\"\n", get_id_str(id)));
532
533   child = get_valid_child(joinelm);
534   n_ins = 0;
535
536   while (NULL != child) {
537     n_ins ++;
538     child = child->next;
539   }
540
541   ins = XMALLOCN(ident*, n_ins);
542   i = 0;
543   child = get_valid_child(joinelm);
544
545   while (NULL != child) {
546     eff_t *valref = parseValref (doc, child);
547     ins [i ++] = valref-> id;
548     free(valref);
549     child = child->next;
550   }
551
552   join-> id = id;
553   join-> effect.join.n_ins = n_ins;
554   join-> effect.join.ins = ins;
555
556   return (join);
557 }
558
559 static eff_t*
560 parseUnknown (xmlDocPtr doc, xmlNodePtr unknownelm)
561 {
562   ident *id;
563   eff_t *unknown = XMALLOC(eff_t);
564   unknown->kind = eff_unknown;
565   (void) doc;
566
567   CHECK_NAME (unknownelm, unknown);
568   VERBOSE_PRINT ((stdout, "unknown node \t0x%08x\n", (int) unknownelm));
569   id = new_id_from_str(getNodeId (unknownelm));
570   unknown-> id = id;
571
572   return (unknown);
573 }
574
575 static eff_t*
576 parseReturn (xmlDocPtr doc, xmlNodePtr retelm)
577 {
578   xmlNodePtr child;
579   eff_t *ret = XMALLOC(eff_t);
580   ret->kind = eff_ret;
581
582   CHECK_NAME (retelm, ret);
583   VERBOSE_PRINT ((stdout, "ret node \t0x%08x\n", (int) retelm));
584
585   child = get_any_valid_child(retelm);
586
587   if (child) {
588     eff_t *valref = parseValref (doc, child);
589     ret-> effect.ret.ret_id = valref-> id;
590     free (valref);
591   } else {
592     ret-> effect.ret.ret_id = NO_ID;
593   }
594
595   return (ret);
596 }
597
598 static eff_t*
599 parseRaise (xmlDocPtr doc, xmlNodePtr raiseelm)
600 {
601   const char *tp_id;
602   eff_t *valref;
603   xmlNodePtr child;
604   eff_t *raise = XMALLOC(eff_t);
605   raise->kind = eff_raise;
606
607   CHECK_NAME (raiseelm, raise);
608   VERBOSE_PRINT ((stdout, "raise node \t0x%08x\n", (int) raiseelm));
609   tp_id = getNodeTypeId (raiseelm);
610   VERBOSE_PRINT ((stdout, "raise->type = \"%s\"\n", tp_id));
611   child = get_valid_child(raiseelm);
612
613   assert (NULL != child);
614
615   valref = parseValref (doc, child);
616   raise-> effect.raise.valref = valref-> id;
617   raise-> effect.raise.tp_id = new_id_from_str(tp_id);
618   free (valref);
619
620   return (raise);
621 }
622
623
624 /*
625   Types and Entities
626 */
627
628 /** parse a type node and insert it into the list */
629 static void
630 parseType (xmlDocPtr doc, xmlNodePtr typeelm)
631 {
632   type_t *type;
633   const char *tp_id = getNodeId (typeelm);
634   VERBOSE_PRINT ((stdout, "type node \t0x%08x (%s)\n", (int) typeelm, tp_id));
635   VERBOSE_PRINT ((stdout, "type = \"%s\"\n", getNodeTypeStr (typeelm)));
636   (void) doc;
637
638   type = XMALLOC(type_t);
639   type -> type_ident = new_id_from_str(getNodeTypeStr (typeelm));
640   type -> id         = new_id_from_str(tp_id);
641
642   type->prev = types;
643   types = type;
644 }
645
646 /** parse an ir_entity node and insert it into the list */
647 static void
648 parseEntity (xmlDocPtr doc, xmlNodePtr entelm)
649 {
650   entity_t *ent = XMALLOC(entity_t);
651   (void) doc;
652
653   /* parse it */
654   const char *ent_id = getNodeId (entelm);
655   /* fprintf (stdout, "ir_entity node \t0x%08x (%d)\n", (int) entelm, ent_id); */
656   VERBOSE_PRINT ((stdout, "ent  = \"%s.%s\"\n",
657           getNodeTypeStr (entelm),
658           getNodeEntityStr (entelm)));
659
660
661   ent -> ent_ident = new_id_from_str (getNodeEntityStr (entelm));
662   ent -> tp_ident  = new_id_from_str (getNodeTypeStr   (entelm));
663   ent -> owner     = new_id_from_str (getNodeOwnerStr  (entelm));
664   ent -> id = new_id_from_str(ent_id);
665
666   ent->prev = entities;
667   entities = ent;
668 }
669
670 /** parse any effect, and turn it into an eff_t (TODO) */
671 static void
672 parseEffect (xmlDocPtr doc, xmlNodePtr effelm)
673 {
674   xmlNodePtr cur;
675   const char *procname = getNodeProcName (effelm);
676   const char *ownerid = getNodeOwnerStr (effelm);
677   proc_t *curr_effs = NULL;
678   int i = 0;
679   int n_effs = 0;
680
681   VERBOSE_PRINT ((stdout, "effect for method \"%s\"\n", procname));
682
683   cur = effelm -> xmlChildrenNode;
684   while (NULL != cur) {
685     n_effs ++;
686     cur = cur->next;
687   }
688   VERBOSE_PRINT ((stdout, "has %d effects\n", n_effs));
689
690   curr_effs = XMALLOC(proc_t);
691   curr_effs -> proc_ident = new_id_from_str(procname);
692   curr_effs -> ownerid = new_id_from_str(ownerid);
693   curr_effs->effs = XMALLOCN(eff_t*, n_effs);
694
695   cur = effelm -> xmlChildrenNode;
696   while (NULL != cur) {
697     eff_t *eff = NULL;
698
699     if (NODE_NAME (cur, arg)) {
700       eff = (eff_t*) parseArg (doc, cur);
701     } else if (NODE_NAME (cur, load)) {
702       eff = (eff_t*) parseLoad (doc, cur);
703     } else if (NODE_NAME (cur, store)) {
704       eff = (eff_t*) parseStore (doc, cur);
705     } else if (NODE_NAME (cur, alloc)) {
706       eff = (eff_t*) parseAlloc (doc, cur);
707     } else if (NODE_NAME (cur, call)) {
708       eff = (eff_t*) parseCall (doc, cur);
709     } else if (NODE_NAME (cur, join)) {
710       eff = (eff_t*) parseJoin (doc, cur);
711     } else if (NODE_NAME (cur, unknown)) {
712       eff = (eff_t*) parseUnknown (doc, cur);
713     } else if (NODE_NAME (cur, ret)) {
714       eff = (eff_t*) parseReturn (doc, cur);
715     } else if (NODE_NAME (cur, raise)) {
716       eff = (eff_t*) parseRaise (doc, cur);
717     } else if (NODE_NAME (cur, comment)) {
718       /* comment */
719       --n_effs;
720     } else {
721       fprintf (stderr, "wrong element \"%s\"\n", BAD_CAST cur->name);
722       exit (EXIT_FAILURE);
723     }
724     if(eff) {
725       VERBOSE_PRINT ((stdout, "effect %p@%d\n", (void*)eff, i));
726       curr_effs -> effs[i++] = eff;
727     }
728     cur = cur -> next;
729   }
730   assert((i == n_effs) && "incorrect number of effects");
731   curr_effs -> n_effs = n_effs;
732   curr_effs -> next = procs;
733   procs = curr_effs;
734 }
735
736
737 static
738 int read_extern (const char *filename)
739 {
740   /* xmlNsPtr ns = NULL; */           /* no namespace for us */
741   xmlDocPtr doc;                /* whole document */
742   xmlNodePtr cur;               /* current node */
743   ident *mod_id;
744   module_t *module;
745
746   /* i've got no idea what the VERSION cast is all about. voodoo
747      programming at its finest. */
748   LIBXML_TEST_VERSION xmlKeepBlanksDefault (0);
749   VERBOSE_PRINT((stdout, "read file %s\n", filename));
750   doc = xmlParseFile (filename);
751   if (! doc)
752     return 0;
753
754   cur = xmlDocGetRootElement (doc);
755   CHECK (cur, "xmlDocGetRootElement");
756
757   if (! NODE_NAME (cur, effects)) {
758     fprintf (stderr,"root node \"%s\" != \"effects\"\n", BAD_CAST cur->name);
759     xmlFreeDoc (doc);
760     exit (EXIT_FAILURE);
761   }
762
763   mod_id = getNodeModuleIdent (cur);
764   if (NULL != mod_id) {
765     VERBOSE_PRINT ((stdout, "effects for \"%s\"\n",
766             get_id_str(mod_id)));
767   }
768   else {
769     VERBOSE_PRINT ((stdout, "effects \t0x%08x\n", (int) cur));
770   }
771
772   /* parse entities */
773   cur = cur->xmlChildrenNode;
774   while (cur != NULL) {
775     if (NODE_NAME (cur, type)) {
776       parseType (doc, cur);
777     } else if (NODE_NAME (cur, ir_entity)) {
778       parseEntity (doc, cur);
779     } else if (NODE_NAME (cur, effect)) {
780       parseEffect (doc, cur);
781     } else if ((NODE_NAME (cur, comment))) {
782       /* comment */
783     } else {
784       fprintf (stderr, "wrong element \"%s\"\n", BAD_CAST cur->name);
785       exit (EXIT_FAILURE);
786     }
787     cur = cur->next;
788   }
789
790   module = XMALLOC(module_t);
791   module -> id = mod_id;
792   module -> types = types;
793   module -> entities = entities;
794   module -> procs = procs;
795
796   types = NULL;
797   entities = NULL;
798   procs = NULL;
799
800   module -> next = modules;
801   modules = module;
802
803   return 1;
804 }
805
806 /********************************************************************/
807
808 /*
809  * free additional structure
810  */
811 static
812 void freeArg (eff_t *arg)
813 {
814   VERBOSE_PRINT ((stdout, "free arg node \t0x%08x\n", (int) arg));
815   free(arg);
816 }
817
818 static
819 void freeValref (eff_t *valref)
820 {
821   VERBOSE_PRINT ((stdout, "free valref node \t0x%08x\n", (int) valref));
822   free(valref);
823 }
824
825 static
826 void freeSelect (eff_t *sel)
827 {
828   VERBOSE_PRINT ((stdout, "free select node \t0x%08x\n", (int) sel));
829   free(sel);
830 }
831
832 static
833 void freeLoad (eff_t *load)
834 {
835   VERBOSE_PRINT ((stdout, "free load node \t0x%08x\n", (int) load));
836   free (load);
837 }
838
839 static
840 void freeStore (eff_t *store)
841 {
842   VERBOSE_PRINT ((stdout, "free store node \t0x%08x\n", (int) store));
843   free (store);
844 }
845
846 static
847 void freeAlloc (eff_t *alloc)
848 {
849   VERBOSE_PRINT ((stdout, "free alloc node \t0x%08x\n", (int) alloc));
850   free(alloc);
851 }
852
853 static
854 void freeCall (eff_t *call)
855 {
856   VERBOSE_PRINT ((stdout, "free call node \t0x%08x\n", (int) call));
857   free(call -> effect.call.args);
858   free(call);
859 }
860
861 static
862 void freeJoin (eff_t *join)
863 {
864   VERBOSE_PRINT ((stdout, "free join node \t0x%08x\n", (int) join));
865   free(join -> effect.join.ins);
866   free(join);
867 }
868
869 static
870 void freeUnknown (eff_t *unknown)
871 {
872   VERBOSE_PRINT ((stdout, "free unknown node \t0x%08x\n", (int) unknown));
873   free(unknown);
874 }
875
876 static
877 void freeReturn (eff_t *ret)
878 {
879   VERBOSE_PRINT ((stdout, "free ret node \t0x%08x\n", (int) ret));
880   free(ret);
881 }
882
883 static
884 void freeRaise (eff_t *raise)
885 {
886   VERBOSE_PRINT ((stdout, "free raise node \t0x%08x\n", (int) raise));
887   free (raise);
888 }
889
890
891 static
892 void freeProcEffs(proc_t *proc)
893 {
894   int i;
895   int num;
896
897   VERBOSE_PRINT ((stdout, "free effect for method \"%s\"\n",
898           get_id_str(proc -> proc_ident)));
899
900   num = proc -> n_effs;
901   for(i = 0; i < num; i++) {
902     switch(proc -> effs[i] -> kind) {
903     case eff_arg:
904       freeArg(proc -> effs[i]);
905       break;
906     case eff_valref:
907       freeValref(proc -> effs[i]);
908       break;
909     case eff_select:
910       freeSelect(proc -> effs[i]);
911       break;
912     case eff_load:
913       freeLoad(proc -> effs[i]);
914       break;
915     case eff_store:
916       freeStore(proc -> effs[i]);
917       break;
918     case eff_alloc:
919       freeAlloc(proc -> effs[i]);
920       break;
921     case eff_call:
922       freeCall(proc -> effs[i]);
923       break;
924     case eff_unknown:
925       freeUnknown(proc -> effs[i]);
926       break;
927     case eff_join:
928       freeJoin(proc -> effs[i]);
929       break;
930     case eff_raise:
931       freeRaise(proc -> effs[i]);
932       break;
933     case eff_ret:
934       freeReturn(proc -> effs[i]);
935       break;
936     default:
937       assert(0 && "try to free an unknown effect");
938       break;
939     }
940   }
941   free(proc -> effs);
942   proc -> effs = NULL;
943 }
944
945 static
946 void freeModuleProcs(module_t *module)
947 {
948   proc_t *next_proc, *proc;
949
950   VERBOSE_PRINT ((stdout, "free procs for module \"%s\"\n",
951           get_id_str(module -> id)));
952
953   proc = module -> procs;
954   while(proc) {
955     next_proc = proc -> next;
956     freeProcEffs(proc);
957     free(proc);
958     proc = next_proc;
959   }
960 }
961
962 static
963 void free_data(void)
964 {
965   module_t *module, *next_module;
966
967   module = modules;
968   while(module) {
969     freeModuleProcs(module);
970     next_module = module -> next;
971     free(module);
972     module = next_module;
973   }
974 }
975
976 /********************************************************************/
977
978 static
979 type_t *find_type_in_module(module_t *module, ident *typeid)
980 {
981   type_t *type;
982
983   for(type = module -> types; type; type = type -> prev) {
984     VERBOSE_PRINT((stdout, "test typeid %s\n", get_id_str(type -> id)));
985     if(type -> id == typeid) {
986       VERBOSE_PRINT((stdout, "found\n"));
987       return(type);
988     }
989   }
990   VERBOSE_PRINT((stdout, "did not find type id %s\n", get_id_str(typeid)));
991   return(NULL);
992 }
993
994 /********************************************************************/
995
996 static void add_value_to_proc(proc_t *proc, eff_t *eff)
997 {
998   eff -> next = proc -> values;
999   proc -> values = eff;
1000 }
1001
1002
1003 eff_t *find_valueid_in_proc_effects(ident *id, proc_t *proc)
1004 {
1005   eff_t *val;
1006
1007   val = proc -> values;
1008   while(val) {
1009     if(id == val -> id) {
1010       return(val);
1011     }
1012     val = val -> next;
1013   }
1014   return(NULL);
1015 }
1016
1017 static void create_abstract_return(ir_graph *irg, proc_t *proc, eff_t *eff)
1018 {
1019   ir_node *x;
1020   eff_t *eff_res;
1021
1022   VERBOSE_PRINT((stdout, "create effect:return in %s\n",
1023          get_id_str(proc -> proc_ident)));
1024   if(NO_ID == eff -> effect.ret.ret_id) {
1025     /* return void */
1026     x = new_Return (get_store(), 0, NULL);
1027   }
1028   else {
1029     ir_node *in[1];
1030
1031     /* return one value */
1032     eff_res = find_valueid_in_proc_effects(eff -> effect.ret.ret_id, proc);
1033     assert(eff_res -> firmnode && "firm in effect not set");
1034     in[0] = eff_res -> firmnode;
1035     x = new_Return (get_store(), 1, in);
1036   }
1037   eff -> firmnode = x;
1038
1039   /* Now we generated all instructions for this block and all its predecessor
1040    * blocks so we can mature it.  (There are not too much.) */
1041   mature_immBlock (get_irg_current_block(irg));
1042
1043   /* This adds the in edge of the end block which originates at the return statement.
1044    * The return node passes controlflow to the end block.  */
1045   add_immBlock_pred (get_irg_end_block(irg), x);
1046 }
1047
1048
1049 static void create_abstract_arg(ir_graph *irg, proc_t *proc, eff_t *eff)
1050 {
1051   ir_node *arg;
1052   ir_entity *ent;
1053   ir_mode *mode;
1054   ir_type *typ;
1055   int num;
1056
1057   VERBOSE_PRINT((stdout, "create effect:arg %d in %s\n",
1058          eff -> effect.arg.num, get_id_str(proc -> proc_ident)));
1059   ent = get_irg_entity(irg);
1060   typ = get_entity_type(ent);
1061
1062   /* read argument eff -> effect.arg.num and place in values list */
1063   num = get_method_n_params(typ);
1064   assert((num >= eff -> effect.arg.num) && "number too big");
1065   typ = get_method_param_type(typ, eff -> effect.arg.num);
1066   mode = get_type_mode(typ);
1067
1068   arg = new_Proj(get_irg_args(irg), mode, eff -> effect.arg.num);
1069   eff -> firmnode = arg;
1070
1071   add_value_to_proc(proc, eff);
1072 }
1073
1074
1075 static void create_abstract_load(ir_graph *irg, proc_t *proc, eff_t *eff)
1076 {
1077   ir_node *sel, *load;
1078   ir_entity *ent;
1079   ir_mode *mode;
1080   eff_t *addr;
1081   (void) irg;
1082
1083   VERBOSE_PRINT((stdout, "create load in %s\n",
1084          get_id_str(proc -> proc_ident)));
1085
1086   if(eff -> effect.load.ent) {
1087     ent = eff -> effect.load.ent -> f_ent;
1088     VERBOSE_PRINT((stdout, "load from %s\n", get_entity_name(ent)));
1089   }
1090   else {
1091     VERBOSE_PRINT((stdout, "store to memory\n"));
1092     ent = NULL;
1093   }
1094
1095   addr = find_valueid_in_proc_effects(eff -> effect.load.ptrrefid, proc);
1096   assert(addr && "no address for load");
1097   /* if addr is Unknown, set proper mode */
1098   if(iro_Unknown == get_irn_opcode(addr -> firmnode)) {
1099     set_irn_mode(addr -> firmnode, mode_P);
1100   }
1101
1102   if(ent) {
1103     sel = new_simpleSel(get_store(), addr -> firmnode, ent);
1104     mode = get_type_mode(get_entity_type(ent));
1105   }
1106   else {
1107     sel = addr -> firmnode;
1108     mode = mode_ANY;
1109   }
1110   load = new_Load(get_store(), sel, mode);
1111   set_store(new_Proj(load, mode_M, 0));
1112   eff -> firmnode = new_Proj(load, mode, 2);
1113
1114   add_value_to_proc(proc, eff);
1115 }
1116
1117
1118 static void create_abstract_store(ir_graph *irg, proc_t *proc, eff_t *eff)
1119 {
1120   ir_node *sel, *store;
1121   ir_entity *ent;
1122   eff_t *addr, *val;
1123   (void) irg;
1124
1125   VERBOSE_PRINT((stdout, "create store in %s\n",
1126          get_id_str(proc -> proc_ident)));
1127
1128   if(eff -> effect.store.ent) {
1129     ent = eff -> effect.store.ent -> f_ent;
1130     VERBOSE_PRINT((stdout, "store to ir_entity %s\n", get_entity_name(ent)));
1131   }
1132   else {
1133     VERBOSE_PRINT((stdout, "store to memory\n"));
1134     ent = NULL;
1135   }
1136
1137   addr = find_valueid_in_proc_effects(eff -> effect.store.ptrrefid, proc);
1138   assert(addr && "no address for store");
1139   /* if addr is Unknown, set propper mode */
1140   if(iro_Unknown == get_irn_opcode(addr -> firmnode)) {
1141     set_irn_mode(addr -> firmnode, mode_P);
1142   }
1143
1144   val = find_valueid_in_proc_effects(eff -> effect.store.valrefid, proc);
1145   assert(val && "no address for store");
1146   /* if addr is Unknown, set propper mode */
1147   if(iro_Unknown == get_irn_opcode(val -> firmnode)) {
1148     set_irn_mode(val -> firmnode, get_type_mode(get_entity_type(ent)));
1149   }
1150
1151   if(ent) {
1152     sel = new_simpleSel(get_store(), addr -> firmnode, ent);
1153   }
1154   else {
1155     sel = addr -> firmnode;
1156   }
1157   store = new_Store(get_store(), sel, val -> firmnode);
1158   set_store(new_Proj(store, mode_M, 0));
1159   eff -> firmnode = store;
1160 }
1161
1162
1163 static void create_abstract_alloc(ir_graph *irg, proc_t *proc, eff_t *eff)
1164 {
1165   ir_type *ftype;
1166   ir_node *alloc;
1167   type_t *xtype;
1168   symconst_symbol sym;
1169   (void) irg;
1170
1171   VERBOSE_PRINT((stdout, "create alloc in %s\n",
1172          get_id_str(proc -> proc_ident)));
1173
1174   xtype = find_type_in_module(current_module, eff -> effect.alloc.tp_id);
1175   assert(xtype && "type not found");
1176   ftype = xtype -> f_tp;
1177
1178   sym.type_p = ftype;
1179   alloc = new_Alloc(get_store(), new_SymConst(mode_Is, sym, symconst_type_size), ftype,
1180             heap_alloc);
1181   set_store(new_Proj(alloc, mode_M, 0));
1182   eff -> firmnode = new_Proj(alloc, mode_P, 2);
1183
1184   add_value_to_proc(proc, eff);
1185 }
1186
1187
1188 static void create_abstract_unknown(ir_graph *irg, proc_t *proc, eff_t *eff)
1189 {
1190   ir_node *unknown;
1191   (void) irg;
1192
1193   VERBOSE_PRINT((stdout, "create unknown in %s\n",
1194          get_id_str(proc -> proc_ident)));
1195
1196   unknown = new_Unknown(mode_ANY);
1197   eff -> firmnode = unknown;
1198
1199   add_value_to_proc(proc, eff);
1200 }
1201
1202
1203 static void create_abstract_call(ir_graph *irg, proc_t *proc, eff_t *eff)
1204 {
1205   ir_node *sel, *call;
1206   ir_entity *ent;
1207   eff_t *addr;
1208   ir_node **irns;
1209   int i, num;
1210   ir_type *mtype;
1211   int mik; /* is method somehow known? */
1212   (void) irg;
1213
1214   VERBOSE_PRINT((stdout, "create call in %s\n",
1215          get_id_str(proc -> proc_ident)));
1216
1217   if(eff -> effect.call.ent) {
1218     ent = eff -> effect.call.ent -> f_ent;
1219     VERBOSE_PRINT((stdout, "call %s\n", get_entity_name(ent)));
1220   }
1221   else {
1222     ent = NULL;
1223     VERBOSE_PRINT((stdout, "call something in memory\n"));
1224   }
1225
1226   addr = find_valueid_in_proc_effects(eff -> effect.call.valrefid, proc);
1227   assert(addr && "no address for load");
1228   /* if addr is Unknown, set proper mode */
1229   if(iro_Unknown == get_irn_opcode(addr -> firmnode)) {
1230     set_irn_mode(addr -> firmnode, mode_P);
1231   }
1232
1233   if(ent) {
1234     /* the address */
1235     sel = new_simpleSel(get_store(), addr -> firmnode, ent);
1236     /* method type */
1237     mtype = get_entity_type(ent);
1238     mik = 1;
1239   }
1240   else {
1241     /* the address */
1242     sel = addr -> firmnode;
1243     /* method type */
1244     mtype = get_unknown_type();
1245     mik = 0;
1246   }
1247
1248   /* the args */
1249   num = eff -> effect.call.n_args;
1250   VERBOSE_PRINT((stdout, "number of args given: %d\n", num));
1251   if(mik) {
1252     VERBOSE_PRINT((stdout, "number of args expected: %d\n",
1253            get_method_n_params(mtype)));
1254   }
1255   irns = ALLOCAN(ir_node*, num);
1256   for(i = 0; i < num; i++) {
1257     irns[i] = find_valueid_in_proc_effects(eff -> effect.call.args[i], proc)
1258       -> firmnode;
1259     if(iro_Unknown == get_irn_opcode(irns[i])) {
1260       if(mik) {
1261     set_irn_mode(irns[i], get_type_mode(get_method_param_type(mtype, i)));
1262       }
1263       else {
1264     set_irn_mode(irns[i], mode_ANY);
1265       }
1266     }
1267   }
1268   call = new_Call(get_store(), sel, num, irns, mtype);
1269   set_store(new_Proj(call, mode_M, 0));
1270   if(mik && (0 != get_method_n_ress(mtype))) {
1271     eff -> firmnode = new_Proj(call,
1272                    get_type_mode(get_method_res_type(mtype, 0)),
1273                    0);
1274     add_value_to_proc(proc, eff); /* result can be accessed */
1275   }
1276   else {
1277     eff -> firmnode = NULL; /* result can not be accessed */
1278   }
1279 }
1280
1281 static void create_abstract_join (ir_graph *irg, proc_t *proc, eff_t *eff)
1282 {
1283   ir_node **ins    = NULL;
1284   ir_node *unknown = NULL;
1285   ir_node *cond    = NULL;
1286   ir_node *block   = NULL;
1287   ir_node *c_block = NULL;
1288   ir_node *phi     = NULL;
1289   ir_mode *join_md = mode_ANY;
1290   int n_ins = -1;
1291   int i;
1292   (void) irg;
1293
1294   VERBOSE_PRINT((stdout, "create join in %s\n",
1295          get_id_str(proc -> proc_ident)));
1296
1297   assert (eff_join == eff->kind);
1298
1299   n_ins = eff->effect.join.n_ins;
1300
1301   /* seems like current_block is not always mature at this point */
1302   mature_immBlock (get_cur_block ());
1303
1304   block = get_cur_block ();     /* remember this so we can put the ProjXs into it */
1305
1306   /* jump based on an unknown condition so all values are possible */
1307   unknown = new_Unknown (mode_Iu);
1308   cond    = new_Cond (unknown);
1309
1310   c_block   = new_immBlock ();  /* for the Phi after the branch(es) */
1311
1312   ins = XMALLOCN(ir_node*, n_ins);
1313   for (i = 0; i < n_ins; i ++) {
1314     ir_node *projX   = NULL;
1315     ir_node *s_block = NULL;
1316     ir_node *jmp     = NULL;
1317     eff_t *in_eff;
1318
1319     /* make sure the projX is in the 'switch' block */
1320     set_cur_block (block);
1321     projX   = new_Proj (cond, mode_X, (long) i);
1322
1323     /* this also sets current_block, so the rest of the code ends up there: */
1324     s_block = new_immBlock ();
1325
1326     add_immBlock_pred (s_block, projX);
1327     mature_immBlock (s_block);
1328
1329     in_eff = find_valueid_in_proc_effects (eff->effect.join.ins [i], proc);
1330
1331     ins [i] = in_eff->firmnode;
1332
1333     /* need to find a suitable mode for the Phi node */
1334     if (mode_ANY != get_irn_mode (ins [i])) {
1335       join_md = get_irn_mode (ins [i]);
1336     }
1337
1338     jmp = new_Jmp ();
1339     add_immBlock_pred (c_block, jmp);
1340   }
1341
1342   set_cur_block (c_block);
1343
1344   phi = new_Phi (n_ins, ins, join_md);
1345
1346   mature_immBlock (c_block);
1347   free (ins);
1348
1349   eff->firmnode = phi;
1350
1351   add_value_to_proc (proc, eff);
1352 }
1353
1354 static void create_abstract_raise (ir_graph *irg, proc_t *proc, eff_t *eff)
1355 {
1356   ir_node *block   = NULL;
1357   ir_node *unknown = NULL;
1358   ir_node *cond    = NULL;
1359
1360   /* seems like current_block is not always mature at this point */
1361   mature_immBlock (get_cur_block ());
1362   block = get_cur_block ();     /* remember this so we can put the ProjXs into it */
1363
1364   /* jump based on an unknown condition so both values are possible */
1365   unknown = new_Unknown (mode_Iu);
1366   cond    = new_Cond (unknown);
1367
1368   /* one branch for 'throw-exception' case */
1369   {
1370     ir_node *projX = new_Proj (cond, mode_X, 1L);
1371     ir_node *b_exc = new_immBlock ();
1372     ir_node *obj   = NULL;
1373     ir_node *thrw  = NULL;
1374     eff_t *thrw_eff = NULL;
1375
1376     add_immBlock_pred (b_exc, projX);
1377
1378     thrw_eff = find_valueid_in_proc_effects (eff->effect.raise.valref, proc);
1379     obj = thrw_eff->firmnode;
1380
1381     thrw = new_Raise (get_store (), obj);
1382     /* exc-jump to end block */
1383     thrw = new_Proj (thrw, mode_X, 0L);
1384
1385     add_immBlock_pred (get_irg_end_block (irg), thrw);
1386     mature_immBlock (get_cur_block ());
1387   }
1388
1389   set_cur_block (block);     /* back to the first block */
1390
1391   /* one branch for 'non-exception' case */
1392   {
1393     ir_node *projX = new_Proj (cond, mode_X, 0);
1394     new_immBlock ();            /* also sets current_block */
1395     add_immBlock_pred (get_cur_block (), projX);
1396     mature_immBlock (get_cur_block ());
1397     /* continue building in current_block */
1398   }
1399
1400 }
1401
1402 static void create_abstract_firm(module_t *module, proc_t *proc, ir_entity *fent)
1403 {
1404   eff_t *eff;
1405   ir_graph *irg;
1406   int i, num;
1407   (void) module;
1408
1409   /* test ir_entity */
1410   assert(visibility_external_allocated == get_entity_visibility(fent)
1411      && peculiarity_existent == get_entity_peculiarity(fent)
1412      && "not an abstract ir_entity");
1413   /* create irg in ir_entity */
1414   irg = new_pseudo_ir_graph(fent, 0);
1415   set_irg_inline_property(irg, irg_inline_forbidden);
1416
1417   /* @@@ If the spec says so: */
1418   set_entity_visibility(fent, visibility_local);
1419
1420   VERBOSE_PRINT((stdout, "create effects for %s\n",
1421          get_id_str(proc -> proc_ident)));
1422
1423   /* create effects in irg */
1424   num = proc -> n_effs;
1425   for(i = 0; i < num; i++) {
1426     eff = proc -> effs[i];
1427     VERBOSE_PRINT((stdout,
1428            "create effect \"%s\"\n", effect_string[(int)eff -> kind]));
1429     switch(eff -> kind) {
1430     case eff_ret:
1431       create_abstract_return(irg, proc, eff);
1432       break;
1433     case eff_arg:
1434       create_abstract_arg(irg, proc, eff);
1435       break;
1436     case eff_load:
1437       create_abstract_load(irg, proc, eff);
1438       break;
1439     case eff_store:
1440       create_abstract_store(irg, proc, eff);
1441       break;
1442     case eff_unknown:
1443       create_abstract_unknown(irg, proc, eff);
1444       break;
1445     case eff_alloc:
1446       create_abstract_alloc(irg, proc, eff);
1447       break;
1448     case eff_call:
1449       create_abstract_call(irg, proc, eff);
1450       break;
1451     case eff_join:
1452       create_abstract_join(irg, proc, eff);
1453       break;
1454     case eff_raise:
1455       create_abstract_raise(irg, proc, eff);
1456       break;
1457     default:
1458       assert(0 && "effect not implemented");
1459       break;
1460     }
1461   }
1462
1463   /* close irg in ir_entity */
1464   /* Now we can mature the end block as all it's predecessors are known. */
1465   mature_immBlock (get_irg_end_block(irg));
1466
1467   /* Verify the graph.  Finds some very bad errors in the graph. */
1468   VERBOSE_PRINT((stdout, "verify graph\n"));
1469   irg_vrfy(irg);
1470   VERBOSE_PRINT((stdout, "finalize construction\n"));
1471   irg_finalize_cons (irg);
1472 }
1473
1474 /********************************************************************/
1475
1476 static void assign_firm_entity(module_t *module, entity_t *xmlent)
1477 {
1478   int i, num;
1479   type_t *typ;
1480   ir_type *type;
1481   ir_entity *ent;
1482
1483   VERBOSE_PRINT((stdout, "assign ir_entity %s to typeid %s\n",
1484          get_id_str(xmlent -> ent_ident),
1485          get_id_str(xmlent -> owner)));
1486
1487   typ = find_type_in_module(module, xmlent -> owner);
1488   assert(typ && "class not found in module");
1489   type = typ -> f_tp;
1490   assert(is_Class_type(type));
1491
1492   num = get_class_n_members(type);
1493   ent = NULL;
1494   for(i = 0; i < num; i++) {
1495     ent = get_class_member(type, i);
1496     VERBOSE_PRINT((stdout, "compare ir_entity %s and %s\n",
1497            get_id_str(xmlent -> ent_ident), get_entity_name(ent)));
1498
1499     if(get_entity_ident(ent) == xmlent -> ent_ident) {
1500       break;
1501     }
1502     ent = NULL;
1503   }
1504   assert(ent && "did not find a ir_entity");
1505
1506   xmlent -> f_ent = ent;
1507 }
1508
1509 /********************************************************************/
1510 /* must be primitive type or class type */
1511 static void assign_firm_type(type_t *xmltype)
1512 {
1513   int i;
1514   ir_type *typ = NULL;
1515   int num;
1516
1517   VERBOSE_PRINT((stdout, "assign firm type to type %s\n",
1518                  get_id_str(xmltype -> type_ident)));
1519
1520   /* is it global type? */
1521   typ = get_glob_type();
1522   if(xmltype -> type_ident == get_type_ident(typ)) {
1523     /* yes */
1524     xmltype -> f_tp = typ;
1525     VERBOSE_PRINT((stdout, "is global type %s\n", get_type_name(typ)));
1526   } else {
1527     num = get_irp_n_types();
1528     for(i = 0; i < num; i++) {
1529       typ = get_irp_type(i);
1530       VERBOSE_PRINT((stdout, "test type %s\n", get_type_name(typ)));
1531       if(xmltype -> type_ident == get_type_ident(typ)) {
1532         VERBOSE_PRINT((stdout, "found type %s\n", get_type_name(typ)));
1533         xmltype -> f_tp = typ;
1534         break;
1535       }
1536       typ = NULL;
1537     }
1538   }
1539   assert(typ && "did not find a type");
1540 }
1541
1542 /********************************************************************/
1543 static
1544 void create_abstract_proc_effect(module_t *module, proc_t *proc)
1545 {
1546   int i, num;
1547   ir_type *class_typ = NULL;
1548   type_t *type;
1549   ir_entity *fent;
1550
1551   /* find the class of a procedure */
1552   VERBOSE_PRINT((stdout, "do find owner id %s\n", get_id_str(proc -> ownerid)));
1553   type = find_type_in_module(module, proc -> ownerid);
1554   assert(type && "class not found in module");
1555
1556   class_typ = get_glob_type();
1557   VERBOSE_PRINT((stdout, "test type %s\n", get_type_name(class_typ)));
1558   if(type -> type_ident != get_type_ident(class_typ)) {
1559     /* find module as class */
1560     num = get_irp_n_types();
1561     for(i = 0; i < num; i++) {
1562       class_typ = get_irp_type(i);
1563       VERBOSE_PRINT((stdout, "test type %s\n", get_type_name(class_typ)));
1564       if (is_Class_type(class_typ)
1565          && (type -> type_ident == get_type_ident(class_typ))) {
1566         /* found class type */
1567         VERBOSE_PRINT((stdout, "found type %s\n", get_type_name(class_typ)));
1568         break;
1569       }
1570       class_typ = NULL;
1571     }
1572   }
1573   else {
1574     VERBOSE_PRINT((stdout, "found global type %s\n", get_type_name(class_typ)));
1575   }
1576   assert(class_typ && "type not found");
1577   assert(is_Class_type(class_typ) && "is not a class type");
1578   type -> f_tp = class_typ;
1579
1580   /* find ir_entity for procedure in class */
1581   VERBOSE_PRINT((stdout, "find method %s\n",
1582          get_id_str(proc -> proc_ident)));
1583
1584   num = get_class_n_members(class_typ);
1585   fent = NULL;
1586   for(i = 0; i < num; i++) {
1587     fent = get_class_member(class_typ, i);
1588     VERBOSE_PRINT((stdout, "test proc %s\n", get_entity_name(fent)));
1589     if(proc -> proc_ident == get_entity_ident(fent)) {
1590       VERBOSE_PRINT((stdout, "found proc %s\n",
1591              get_id_str(proc -> proc_ident)));
1592       /* @@@ TODO check args types - not in xml yet */
1593       /* create Firm stuff */
1594       create_abstract_firm(module, proc, fent);
1595       return;
1596     }
1597     else {
1598       fent = NULL;
1599     }
1600   }
1601
1602   /* fail */
1603   fprintf(stderr,
1604       "method %s not found\nNo effects generated\nCandidates are:\n",
1605       get_id_str(proc -> proc_ident));
1606   for(i = 0; i < num; i++) {
1607     fent = get_class_member(class_typ, i);
1608     fprintf(stderr, "%s\n", get_entity_name(fent));
1609   }
1610   //assert(fent && "procedure not found in class");
1611 }
1612
1613 static
1614 void create_abstract_module(module_t *module)
1615 {
1616   proc_t *proc;
1617   type_t *type;
1618   entity_t *ent;
1619
1620   VERBOSE_PRINT((stdout, "create an abstraction for module %s\n",
1621          get_id_str(module -> id)));
1622
1623   VERBOSE_PRINT((stdout, "--handle types for module\n"));
1624   for(type = module -> types; type; type = type -> prev) {
1625     assign_firm_type(type);
1626   }
1627
1628   VERBOSE_PRINT((stdout, "--handle entities for module\n"));
1629   /* @@@ TODO */
1630   for(ent = module -> entities; ent; ent = ent -> prev) {
1631     assign_firm_entity(module, ent);
1632   }
1633
1634   VERBOSE_PRINT((stdout, "--handle procs for module\n"));
1635   for(proc = module -> procs; proc; proc = proc -> next) {
1636     create_abstract_proc_effect(module, proc);
1637   }
1638 }
1639
1640
1641 int create_abstraction(const char *filename)
1642 {
1643   module_t *module;
1644
1645   /* read and parse XML file */
1646   if (! read_extern(filename))
1647     return 0;
1648
1649   /* finished reading and parsing here */
1650   /* build FIRM graphs */
1651   module = modules;
1652   while(module) {
1653     current_module = module;
1654     create_abstract_module(module);
1655     module = module -> next;
1656   }
1657   current_module = NULL;
1658
1659   /* free data structures */
1660   free_data();
1661
1662   types = NULL;
1663   entities = NULL;
1664   procs = NULL;
1665   modules = NULL;
1666
1667   return 1;
1668 }
1669
1670
1671 void free_abstraction(void) {
1672   int i, n_pseudo_irgs = get_irp_n_pseudo_irgs();
1673   for (i = 0; i < n_pseudo_irgs; ++i) {
1674     ir_graph *p_irg = get_irp_pseudo_irg(i);
1675     set_entity_visibility(get_irg_entity(p_irg), visibility_external_allocated);
1676     // @@@ free_pseudo_ir_graph(p_irg);
1677   }
1678 }
1679
1680
1681 /********************************************************************/
1682
1683 \f
1684 /*
1685  * $Log$
1686  * Revision 1.27  2007/02/02 12:38:35  matze
1687  * entity is ir_entity now
1688  *
1689  * Revision 1.26  2006/12/15 12:37:40  matze
1690  * fix warnings
1691  *
1692  * Revision 1.25  2006/06/09 11:26:35  firm
1693  * renamed type to ir_type
1694  *
1695  * Revision 1.24  2006/05/29 13:34:49  beck
1696  * renamed symconst_size to symconst_type_size
1697  *
1698  * Revision 1.22  2005/08/16 10:18:35  beck
1699  * create_abstraction() now returns an error code if the file could not
1700  * be opened.
1701  *
1702  * Revision 1.21  2005/03/10 10:05:38  goetz
1703  * chanmged method name
1704  *
1705  * Revision 1.20  2005/01/05 14:28:35  beck
1706  * renames all is_x*_type() functions to is_X*_type() to prevent name clash with EDG frontend
1707  *
1708  * Revision 1.19  2004/12/10 15:14:34  beck
1709  * used xmalloc instead of malloc
1710  *
1711  * Revision 1.18  2004/12/02 16:21:42  beck
1712  * fixed config.h include
1713  *
1714  * Revision 1.17  2004/11/23 14:17:31  liekweg
1715  * fenced out currently unneeded static functions
1716  *
1717  * Revision 1.16  2004/11/11 12:24:52  goetz
1718  * fixes
1719  *
1720  * Revision 1.15  2004/11/11 09:28:32  goetz
1721  * treat pseudo irgs special
1722  * parse 'local' from xml files
1723  *
1724  * Revision 1.14  2004/11/10 14:42:00  boesler
1725  * be more helpful if a method does not exist
1726  *
1727  * Revision 1.13  2004/11/05 14:00:53  liekweg
1728  * added raise
1729  *
1730  * Revision 1.12  2004/11/02 14:30:31  liekweg
1731  * fixed multi-input join (thx, Boris) --flo
1732  *
1733  * Revision 1.11  2004/10/29 18:51:53  liekweg
1734  * Added Join
1735  *
1736  * Revision 1.10  2004/10/25 13:52:24  boesler
1737  * seperated read.h (public interface) and read_t.h (types)
1738  *
1739  * Revision 1.9  2004/10/22 13:51:35  boesler
1740  * prohibit inlining of pseudo ir_graphs
1741  *
1742  * Revision 1.8  2004/10/22 13:13:27  boesler
1743  * replaced char* by idents, minor fix in Firm codegen for call
1744  *
1745  * Revision 1.7  2004/10/21 15:31:55  boesler
1746  * added lots of stuff:
1747  * - build abstract syntax trees
1748  * - build Firm graphs for many effects, still todos
1749  *
1750  * Revision 1.5  2004/10/18 12:48:20  liekweg
1751  * avoid warning
1752  *
1753  * Revision 1.4  2004/10/14 11:31:53  liekweg
1754  * ...
1755  *
1756  * Revision 1.3  2004/10/13 13:36:28  rubino
1757  * fix for strdup
1758  *
1759  * Revision 1.2  2004/10/11 15:56:09  liekweg
1760  * Cleanup, comments ...
1761  * Added init func --flo
1762  *
1763  * Revision 1.1  2004/10/11 09:31:06  liekweg
1764  * First Import of XML reading procs --flo
1765  *
1766  */