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