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