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