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