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