2 * Copyright (C) 1995-2007 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
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.
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.
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
25 * File name: ir/external/read.c
26 * Purpose: Read descriptions of external effects
28 * Modified by: Boris Boesler
31 * Copyright: (c) 1999-2004 Universität Karlsruhe
47 #include <libxml/xmlmemory.h>
48 #include <libxml/parser.h>
49 #include <libxml/encoding.h>
55 #include "pseudo_irg.h"
64 # define MY_ENCODING "ISO-8859-1"
66 # define CHECK(ptr,msg) assert (ptr && msg)
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))
71 # define NEW(T) (T*)xmalloc(sizeof (T))
74 #define VERBOSE_PRINTING 0
77 # define VERBOSE_PRINT(s) fprintf s
79 # define VERBOSE_PRINT(s)
84 static type_t *types = NULL;
85 static entity_t *entities = NULL;
86 static proc_t *procs = NULL;
87 static module_t *modules = NULL;
90 static module_t *current_module = NULL;
93 /* this is only used inside a VERBOSE_PRINT() call */
94 static const char *effect_string[] = {
107 #endif /* defined VERBOSE_PRINTING */
110 getNodeModuleIdent (xmlNodePtr node)
112 const char *mod_str = (const char*) xmlGetProp (node, BAD_CAST "module");
114 if (NULL == mod_str) {
117 ident *res = new_id_from_str (mod_str);
123 getNodeProcName (xmlNodePtr node)
125 const char *proc_str = (const char*) xmlGetProp (node, BAD_CAST "procname");
132 getNodeClassName (xmlNodePtr node)
134 char *proc_str = (char*) xmlGetProp (node, BAD_CAST "class");
136 return ( (proc_str));
138 # endif /* defined NEEDED */
141 getNodeId (xmlNodePtr node)
143 const char *id_str = (const char*) xmlGetProp (node, BAD_CAST "id");
149 getNodeRefId (xmlNodePtr node)
151 const char *refid_str = (char*) xmlGetProp (node, BAD_CAST "refid");
153 return ((refid_str));
157 getNodeTypeId (xmlNodePtr node)
159 const char *type_str = (char*) xmlGetProp (node, BAD_CAST "type");
165 *getNodeTypeStr (xmlNodePtr node)
167 const char *type_str = (char*) xmlGetProp (node, BAD_CAST "type");
173 getNodeOwnerStr (xmlNodePtr node)
175 const char *owner_str = (char*) xmlGetProp (node, BAD_CAST "owner");
181 *getNodeEntityStr (xmlNodePtr node)
183 const char *ent_str = (char*) xmlGetProp (node, BAD_CAST "ir_entity");
195 type_t *getTypeByIdent (ident *id)
197 type_t *curr = types; // @@@ TODO module -> types
199 while (NULL != curr) {
200 if (id == curr -> type_ident) {
208 # endif /* defined NEEDED */
212 type_t *getTypeById (ident *id)
214 type_t *curr = types; // which ones?
216 while (NULL != curr) {
217 if (id == curr -> id) {
225 # endif /* defined NEEDED */
229 entity_t *getEntityByIdents (ident *name, ident *tp_ident)
231 entity_t *curr = entities; // TODO module -> entities
233 while (NULL != curr) {
234 if ((name == curr -> ent_ident)
235 && (tp_ident == curr -> tp_ident)) {
243 # endif /* defined NEEDED */
246 entity_t *getEntityById (ident *id)
248 entity_t *curr = entities;
250 while (NULL != curr) {
251 if (id == curr -> id) {
262 proc_t *getEffectByName (ident *proc_ident)
264 proc_t *curr_effs = procs;
266 while (NULL != curr_effs) {
267 if (proc_ident == curr_effs -> proc_ident) {
270 curr_effs = curr_effs->next;
275 # endif /* defined NEEDED */
278 xmlNodePtr get_any_valid_child(xmlNodePtr elem)
282 assert(elem && "no element");
283 child = elem -> xmlChildrenNode;
284 while(child && (NODE_NAME (child, comment))) {
285 child = child -> next;
291 xmlNodePtr get_valid_child(xmlNodePtr elem)
295 child = get_any_valid_child(elem);
296 assert(child && "lost child in deep black forest");
301 * parse XML structure and construct an additional structure
304 parseArg (xmlDocPtr doc, xmlNodePtr argelm)
312 CHECK_NAME (argelm, arg);
313 VERBOSE_PRINT ((stdout, "arg node \t0x%08x\n", (int) argelm));
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));
321 typeid = getNodeTypeStr (argelm);
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);
333 parseValref (xmlDocPtr doc, xmlNodePtr valelm)
338 CHECK_NAME (valelm, valref);
339 VERBOSE_PRINT ((stdout, "valref node \t0x%08x\n", (int) valelm));
341 ref_id = getNodeRefId (valelm);
342 VERBOSE_PRINT ((stdout, "val->refid = \"%s\"\n", ref_id));
344 valref = NEW (eff_t);
345 valref->kind = eff_valref;
346 valref-> id = new_id_from_str(ref_id);
352 parseSelect (xmlDocPtr doc, xmlNodePtr selelm)
354 ident *entity_id = new_id_from_str(getNodeEntityStr (selelm));
357 eff_t *valref = NULL;
358 eff_t *sel = NEW (eff_t);
359 sel->kind = eff_select;
361 CHECK_NAME (selelm, select);
362 VERBOSE_PRINT ((stdout, "select node \t0x%08x\n", (int) selelm));
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)));
368 child = selelm->xmlChildrenNode;
371 valref = parseValref (doc, child);
374 sel-> id = valref ? valref-> id : NO_ID;
375 sel-> effect.select.ent = ent;
385 parseLoad (xmlDocPtr doc, xmlNodePtr loadelm)
390 eff_t *load = NEW (eff_t);
391 load->kind = eff_load;
393 CHECK_NAME (loadelm, load);
394 VERBOSE_PRINT ((stdout, "load node \t0x%08x\n", (int) loadelm));
395 id = new_id_from_str(getNodeId (loadelm));
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)));
405 sel = parseValref (doc, child);
406 load-> effect.load.ent = NULL;
410 load-> effect.load.ptrrefid = sel-> id;
418 parseStore (xmlDocPtr doc, xmlNodePtr storeelm)
423 eff_t *store = NEW (eff_t);
424 store->kind = eff_store;
426 CHECK_NAME (storeelm, store);
427 VERBOSE_PRINT ((stdout, "store node \t0x%08x\n", (int) storeelm));
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;
435 sel = parseValref (doc, child);
436 store-> effect.store.ent = NULL;
440 valref = parseValref (doc, child);
442 store-> effect.store.ptrrefid = sel-> id;
443 store-> effect.store.valrefid = valref-> id;
452 parseAlloc (xmlDocPtr doc, xmlNodePtr allocelm)
456 eff_t *alloc = NEW (eff_t); /* ...! */
457 alloc->kind = eff_alloc;
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)));
467 alloc-> effect.alloc.tp_id = type_id;
473 parseCall (xmlDocPtr doc, xmlNodePtr callelm)
480 eff_t *call = NEW (eff_t);
481 call->kind = eff_call;
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)));
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;
494 sel = parseValref (doc, child);
495 call-> effect.call.ent = NULL;
498 arg = child = child->next;
501 while (NULL != child) {
507 call-> effect.call.valrefid = sel-> id;
508 call-> effect.call.n_args = n_args;
509 call-> effect.call.args = NULL;
514 ident **args = (ident**) xmalloc(n_args * sizeof(ident*));
517 while (NULL != arg) {
518 eff_t *valref = parseValref (doc, arg);
519 args [i ++] = valref-> id;
524 call-> effect.call.args = args;
531 parseJoin (xmlDocPtr doc, xmlNodePtr joinelm)
538 eff_t *join = NEW (eff_t);
539 join->kind = eff_join;
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)));
546 child = get_valid_child(joinelm);
549 while (NULL != child) {
554 ins = (ident **) xmalloc (n_ins * sizeof (ident *) );
556 child = get_valid_child(joinelm);
558 while (NULL != child) {
559 eff_t *valref = parseValref (doc, child);
560 ins [i ++] = valref-> id;
566 join-> effect.join.n_ins = n_ins;
567 join-> effect.join.ins = ins;
573 parseUnknown (xmlDocPtr doc, xmlNodePtr unknownelm)
576 eff_t *unknown = NEW (eff_t);
577 unknown->kind = eff_unknown;
579 CHECK_NAME (unknownelm, unknown);
580 VERBOSE_PRINT ((stdout, "unknown node \t0x%08x\n", (int) unknownelm));
581 id = new_id_from_str(getNodeId (unknownelm));
588 parseReturn (xmlDocPtr doc, xmlNodePtr retelm)
591 eff_t *ret = NEW (eff_t);
594 CHECK_NAME (retelm, ret);
595 VERBOSE_PRINT ((stdout, "ret node \t0x%08x\n", (int) retelm));
597 child = get_any_valid_child(retelm);
600 eff_t *valref = parseValref (doc, child);
601 ret-> effect.ret.ret_id = valref-> id;
604 ret-> effect.ret.ret_id = NO_ID;
611 parseRaise (xmlDocPtr doc, xmlNodePtr raiseelm)
616 eff_t *raise = NEW (eff_t);
617 raise->kind = eff_raise;
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);
625 assert (NULL != child);
627 valref = parseValref (doc, child);
628 raise-> effect.raise.valref = valref-> id;
629 raise-> effect.raise.tp_id = new_id_from_str(tp_id);
640 /** parse a type node and insert it into the list */
642 parseType (xmlDocPtr doc, xmlNodePtr typeelm)
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)));
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);
657 /** parse an ir_entity node and insert it into the list */
659 parseEntity (xmlDocPtr doc, xmlNodePtr entelm)
661 entity_t *ent = NEW (entity_t);
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)));
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);
676 ent->prev = entities;
680 /** parse any effect, and turn it into an eff_t (TODO) */
682 parseEffect (xmlDocPtr doc, xmlNodePtr effelm)
685 const char *procname = getNodeProcName (effelm);
686 const char *ownerid = getNodeOwnerStr (effelm);
687 proc_t *curr_effs = NULL;
691 VERBOSE_PRINT ((stdout, "effect for method \"%s\"\n", procname));
693 cur = effelm -> xmlChildrenNode;
694 while (NULL != cur) {
698 VERBOSE_PRINT ((stdout, "has %d effects\n", n_effs));
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*));
705 cur = effelm -> xmlChildrenNode;
706 while (NULL != cur) {
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)) {
731 fprintf (stderr, "wrong element \"%s\"\n", BAD_CAST cur->name);
735 VERBOSE_PRINT ((stdout, "effect %p@%d\n", (void*)eff, i));
736 curr_effs -> effs[i++] = eff;
740 assert((i == n_effs) && "incorrect number of effects");
741 curr_effs -> n_effs = n_effs;
742 curr_effs -> next = procs;
748 int read_extern (const char *filename)
750 /* xmlNsPtr ns = NULL; */ /* no namespace for us */
751 xmlDocPtr doc; /* whole document */
752 xmlNodePtr cur; /* current node */
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);
764 cur = xmlDocGetRootElement (doc);
765 CHECK (cur, "xmlDocGetRootElement");
767 if (! NODE_NAME (cur, effects)) {
768 fprintf (stderr,"root node \"%s\" != \"effects\"\n", BAD_CAST cur->name);
773 mod_id = getNodeModuleIdent (cur);
774 if (NULL != mod_id) {
775 VERBOSE_PRINT ((stdout, "effects for \"%s\"\n",
776 get_id_str(mod_id)));
779 VERBOSE_PRINT ((stdout, "effects \t0x%08x\n", (int) cur));
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))) {
794 fprintf (stderr, "wrong element \"%s\"\n", BAD_CAST cur->name);
800 module = NEW(module_t);
801 module -> id = mod_id;
802 module -> types = types;
803 module -> entities = entities;
804 module -> procs = procs;
810 module -> next = modules;
816 /********************************************************************/
819 * free additional structure
822 void freeArg (eff_t *arg)
824 VERBOSE_PRINT ((stdout, "free arg node \t0x%08x\n", (int) arg));
830 void freeValref (eff_t *valref)
832 VERBOSE_PRINT ((stdout, "free valref node \t0x%08x\n", (int) valref));
838 void freeSelect (eff_t *sel)
840 VERBOSE_PRINT ((stdout, "free select node \t0x%08x\n", (int) sel));
846 void freeLoad (eff_t *load)
848 VERBOSE_PRINT ((stdout, "free load node \t0x%08x\n", (int) load));
854 void freeStore (eff_t *store)
856 VERBOSE_PRINT ((stdout, "free store node \t0x%08x\n", (int) store));
862 void freeAlloc (eff_t *alloc)
864 VERBOSE_PRINT ((stdout, "free alloc node \t0x%08x\n", (int) alloc));
870 void freeCall (eff_t *call)
872 VERBOSE_PRINT ((stdout, "free call node \t0x%08x\n", (int) call));
873 free(call -> effect.call.args);
879 void freeJoin (eff_t *join)
881 VERBOSE_PRINT ((stdout, "free join node \t0x%08x\n", (int) join));
882 free(join -> effect.join.ins);
888 void freeUnknown (eff_t *unknown)
890 VERBOSE_PRINT ((stdout, "free unknown node \t0x%08x\n", (int) unknown));
896 void freeReturn (eff_t *ret)
898 VERBOSE_PRINT ((stdout, "free ret node \t0x%08x\n", (int) ret));
904 void freeRaise (eff_t *raise)
906 VERBOSE_PRINT ((stdout, "free raise node \t0x%08x\n", (int) raise));
913 void freeProcEffs(proc_t *proc)
918 VERBOSE_PRINT ((stdout, "free effect for method \"%s\"\n",
919 get_id_str(proc -> proc_ident)));
921 num = proc -> n_effs;
922 for(i = 0; i < num; i++) {
923 switch(proc -> effs[i] -> kind) {
925 freeArg(proc -> effs[i]);
928 freeValref(proc -> effs[i]);
931 freeSelect(proc -> effs[i]);
934 freeLoad(proc -> effs[i]);
937 freeStore(proc -> effs[i]);
940 freeAlloc(proc -> effs[i]);
943 freeCall(proc -> effs[i]);
946 freeUnknown(proc -> effs[i]);
949 freeJoin(proc -> effs[i]);
952 freeRaise(proc -> effs[i]);
955 freeReturn(proc -> effs[i]);
958 assert(0 && "try to free an unknown effect");
967 void freeModuleProcs(module_t *module)
969 proc_t *next_proc, *proc;
971 VERBOSE_PRINT ((stdout, "free procs for module \"%s\"\n",
972 get_id_str(module -> id)));
974 proc = module -> procs;
976 next_proc = proc -> next;
986 module_t *module, *next_module;
990 freeModuleProcs(module);
991 next_module = module -> next;
993 module = next_module;
997 /********************************************************************/
1000 type_t *find_type_in_module(module_t *module, ident *typeid)
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"));
1011 VERBOSE_PRINT((stdout, "did not find type id %s\n", get_id_str(typeid)));
1015 /********************************************************************/
1017 static void add_value_to_proc(proc_t *proc, eff_t *eff)
1019 eff -> next = proc -> values;
1020 proc -> values = eff;
1024 eff_t *find_valueid_in_proc_effects(ident *id, proc_t *proc)
1028 val = proc -> values;
1030 if(id == val -> id) {
1038 static void create_abstract_return(ir_graph *irg, proc_t *proc, eff_t *eff)
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) {
1047 x = new_Return (get_store(), 0, NULL);
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);
1058 eff -> firmnode = x;
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));
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);
1070 static void create_abstract_arg(ir_graph *irg, proc_t *proc, eff_t *eff)
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);
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);
1089 arg = new_Proj(get_irg_args(irg), mode, eff -> effect.arg.num);
1090 eff -> firmnode = arg;
1092 add_value_to_proc(proc, eff);
1096 static void create_abstract_load(ir_graph *irg, proc_t *proc, eff_t *eff)
1098 ir_node *sel, *load;
1103 VERBOSE_PRINT((stdout, "create load in %s\n",
1104 get_id_str(proc -> proc_ident)));
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)));
1111 VERBOSE_PRINT((stdout, "store to memory\n"));
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);
1123 sel = new_simpleSel(get_store(), addr -> firmnode, ent);
1124 mode = get_type_mode(get_entity_type(ent));
1127 sel = addr -> firmnode;
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);
1134 add_value_to_proc(proc, eff);
1138 static void create_abstract_store(ir_graph *irg, proc_t *proc, eff_t *eff)
1140 ir_node *sel, *store;
1144 VERBOSE_PRINT((stdout, "create store in %s\n",
1145 get_id_str(proc -> proc_ident)));
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)));
1152 VERBOSE_PRINT((stdout, "store to memory\n"));
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);
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)));
1171 sel = new_simpleSel(get_store(), addr -> firmnode, ent);
1174 sel = addr -> firmnode;
1176 store = new_Store(get_store(), sel, val -> firmnode);
1177 set_store(new_Proj(store, mode_M, 0));
1178 eff -> firmnode = store;
1182 static void create_abstract_alloc(ir_graph *irg, proc_t *proc, eff_t *eff)
1187 symconst_symbol sym;
1189 VERBOSE_PRINT((stdout, "create alloc in %s\n",
1190 get_id_str(proc -> proc_ident)));
1192 xtype = find_type_in_module(current_module, eff -> effect.alloc.tp_id);
1193 assert(xtype && "type not found");
1194 ftype = xtype -> f_tp;
1197 alloc = new_Alloc(get_store(), new_SymConst(sym, symconst_type_size), ftype,
1199 set_store(new_Proj(alloc, mode_M, 0));
1200 eff -> firmnode = new_Proj(alloc, mode_P, 2);
1202 add_value_to_proc(proc, eff);
1206 static void create_abstract_unknown(ir_graph *irg, proc_t *proc, eff_t *eff)
1210 VERBOSE_PRINT((stdout, "create unknown in %s\n",
1211 get_id_str(proc -> proc_ident)));
1213 unknown = new_Unknown(mode_ANY);
1214 eff -> firmnode = unknown;
1216 add_value_to_proc(proc, eff);
1220 static void create_abstract_call(ir_graph *irg, proc_t *proc, eff_t *eff)
1222 ir_node *sel, *call;
1228 int mik; /* is method somehow known? */
1230 VERBOSE_PRINT((stdout, "create call in %s\n",
1231 get_id_str(proc -> proc_ident)));
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)));
1239 VERBOSE_PRINT((stdout, "call something in memory\n"));
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);
1251 sel = new_simpleSel(get_store(), addr -> firmnode, ent);
1253 mtype = get_entity_type(ent);
1258 sel = addr -> firmnode;
1260 mtype = get_unknown_type();
1265 num = eff -> effect.call.n_args;
1266 VERBOSE_PRINT((stdout, "number of args given: %d\n", num));
1268 VERBOSE_PRINT((stdout, "number of args expected: %d\n",
1269 get_method_n_params(mtype)));
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)
1275 if(iro_Unknown == get_irn_opcode(irns[i])) {
1277 set_irn_mode(irns[i], get_type_mode(get_method_param_type(mtype, i)));
1280 set_irn_mode(irns[i], mode_ANY);
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)),
1290 add_value_to_proc(proc, eff); /* result can be accessed */
1293 eff -> firmnode = NULL; /* result can not be accessed */
1297 static void create_abstract_join (ir_graph *irg, proc_t *proc, eff_t *eff)
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;
1309 VERBOSE_PRINT((stdout, "create join in %s\n",
1310 get_id_str(proc -> proc_ident)));
1312 assert (eff_join == eff->kind);
1314 n_ins = eff->effect.join.n_ins;
1316 /* seems like current_block is not always mature at this point */
1317 mature_immBlock (get_cur_block ());
1319 block = get_cur_block (); /* remember this so we can put the ProjXs into it */
1321 /* jump based on an unknown condition so all values are possible */
1322 unknown = new_Unknown (mode_Iu);
1323 cond = new_Cond (unknown);
1325 c_block = new_immBlock (); /* for the Phi after the branch(es) */
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;
1334 /* make sure the projX is in the 'switch' block */
1335 set_cur_block (block);
1336 projX = new_Proj (cond, mode_X, (long) i);
1338 /* this also sets current_block, so the rest of the code ends up there: */
1339 s_block = new_immBlock ();
1341 add_immBlock_pred (s_block, projX);
1342 mature_immBlock (s_block);
1344 in_eff = find_valueid_in_proc_effects (eff->effect.join.ins [i], proc);
1346 ins [i] = in_eff->firmnode;
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]);
1354 add_immBlock_pred (c_block, jmp);
1357 set_cur_block (c_block);
1359 phi = new_Phi (n_ins, ins, join_md);
1361 mature_immBlock (c_block);
1362 memset (ins, 0x00, n_ins * sizeof (ir_node*));
1365 eff->firmnode = phi;
1367 add_value_to_proc (proc, eff);
1370 static void create_abstract_raise (ir_graph *irg, proc_t *proc, eff_t *eff)
1372 ir_node *block = NULL;
1373 ir_node *unknown = NULL;
1374 ir_node *cond = NULL;
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 */
1380 /* jump based on an unknown condition so both values are possible */
1381 unknown = new_Unknown (mode_Iu);
1382 cond = new_Cond (unknown);
1384 /* one branch for 'throw-exception' case */
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;
1392 add_immBlock_pred (b_exc, projX);
1394 thrw_eff = find_valueid_in_proc_effects (eff->effect.raise.valref, proc);
1395 obj = thrw_eff->firmnode;
1397 thrw = new_Raise (get_store (), obj);
1398 /* exc-jump to end block */
1399 thrw = new_Proj (thrw, mode_X, 0L);
1401 add_immBlock_pred (get_irg_end_block (irg), thrw);
1402 mature_immBlock (get_cur_block ());
1405 set_cur_block (block); /* back to the first block */
1407 /* one branch for 'non-exception' case */
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 */
1418 static void create_abstract_firm(module_t *module, proc_t *proc, ir_entity *fent)
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);
1432 /* @@@ If the spec says so: */
1433 set_entity_visibility(fent, visibility_local);
1435 VERBOSE_PRINT((stdout, "create effects for %s\n",
1436 get_id_str(proc -> proc_ident)));
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) {
1446 create_abstract_return(irg, proc, eff);
1449 create_abstract_arg(irg, proc, eff);
1452 create_abstract_load(irg, proc, eff);
1455 create_abstract_store(irg, proc, eff);
1458 create_abstract_unknown(irg, proc, eff);
1461 create_abstract_alloc(irg, proc, eff);
1464 create_abstract_call(irg, proc, eff);
1467 create_abstract_join(irg, proc, eff);
1470 create_abstract_raise(irg, proc, eff);
1473 assert(0 && "effect not implemented");
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));
1482 /* Verify the graph. Finds some very bad errors in the graph. */
1483 VERBOSE_PRINT((stdout, "verify graph\n"));
1485 VERBOSE_PRINT((stdout, "finalize construction\n"));
1486 irg_finalize_cons (irg);
1489 /********************************************************************/
1491 static void assign_firm_entity(module_t *module, entity_t *xmlent)
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)));
1502 typ = find_type_in_module(module, xmlent -> owner);
1503 assert(typ && "class not found in module");
1505 assert(is_Class_type(type));
1507 num = get_class_n_members(type);
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)));
1514 if(get_entity_ident(ent) == xmlent -> ent_ident) {
1519 assert(ent && "did not find a ir_entity");
1521 xmlent -> f_ent = ent;
1524 /********************************************************************/
1525 /* must be primitive type or class type */
1526 static void assign_firm_type(type_t *xmltype)
1529 ir_type *typ = NULL;
1532 VERBOSE_PRINT((stdout, "assign firm type to type %s\n",
1533 get_id_str(xmltype -> type_ident)));
1535 /* is it global type? */
1536 typ = get_glob_type();
1537 if(xmltype -> type_ident == get_type_ident(typ)) {
1539 xmltype -> f_tp = typ;
1540 VERBOSE_PRINT((stdout, "is global type %s\n", get_type_name(typ)));
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;
1554 assert(typ && "did not find a type");
1557 /********************************************************************/
1559 void create_abstract_proc_effect(module_t *module, proc_t *proc)
1562 ir_type *class_typ = NULL;
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");
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)));
1589 VERBOSE_PRINT((stdout, "found global type %s\n", get_type_name(class_typ)));
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;
1595 /* find ir_entity for procedure in class */
1596 VERBOSE_PRINT((stdout, "find method %s\n",
1597 get_id_str(proc -> proc_ident)));
1599 num = get_class_n_members(class_typ);
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);
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));
1625 //assert(fent && "procedure not found in class");
1629 void create_abstract_module(module_t *module)
1635 VERBOSE_PRINT((stdout, "create an abstraction for module %s\n",
1636 get_id_str(module -> id)));
1638 VERBOSE_PRINT((stdout, "--handle types for module\n"));
1639 for(type = module -> types; type; type = type -> prev) {
1640 assign_firm_type(type);
1643 VERBOSE_PRINT((stdout, "--handle entities for module\n"));
1645 for(ent = module -> entities; ent; ent = ent -> prev) {
1646 assign_firm_entity(module, ent);
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);
1656 int create_abstraction(const char *filename)
1660 /* read and parse XML file */
1661 if (! read_extern(filename))
1664 /* finished reading and parsing here */
1665 /* build FIRM graphs */
1668 current_module = module;
1669 create_abstract_module(module);
1670 module = module -> next;
1672 current_module = NULL;
1674 /* free data structures */
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);
1696 /********************************************************************/
1701 * Revision 1.27 2007/02/02 12:38:35 matze
1702 * entity is ir_entity now
1704 * Revision 1.26 2006/12/15 12:37:40 matze
1707 * Revision 1.25 2006/06/09 11:26:35 firm
1708 * renamed type to ir_type
1710 * Revision 1.24 2006/05/29 13:34:49 beck
1711 * renamed symconst_size to symconst_type_size
1713 * Revision 1.22 2005/08/16 10:18:35 beck
1714 * create_abstraction() now returns an error code if the file could not
1717 * Revision 1.21 2005/03/10 10:05:38 goetz
1718 * chanmged method name
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
1723 * Revision 1.19 2004/12/10 15:14:34 beck
1724 * used xmalloc instead of malloc
1726 * Revision 1.18 2004/12/02 16:21:42 beck
1727 * fixed config.h include
1729 * Revision 1.17 2004/11/23 14:17:31 liekweg
1730 * fenced out currently unneeded static functions
1732 * Revision 1.16 2004/11/11 12:24:52 goetz
1735 * Revision 1.15 2004/11/11 09:28:32 goetz
1736 * treat pseudo irgs special
1737 * parse 'local' from xml files
1739 * Revision 1.14 2004/11/10 14:42:00 boesler
1740 * be more helpful if a method does not exist
1742 * Revision 1.13 2004/11/05 14:00:53 liekweg
1745 * Revision 1.12 2004/11/02 14:30:31 liekweg
1746 * fixed multi-input join (thx, Boris) --flo
1748 * Revision 1.11 2004/10/29 18:51:53 liekweg
1751 * Revision 1.10 2004/10/25 13:52:24 boesler
1752 * seperated read.h (public interface) and read_t.h (types)
1754 * Revision 1.9 2004/10/22 13:51:35 boesler
1755 * prohibit inlining of pseudo ir_graphs
1757 * Revision 1.8 2004/10/22 13:13:27 boesler
1758 * replaced char* by idents, minor fix in Firm codegen for call
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
1765 * Revision 1.5 2004/10/18 12:48:20 liekweg
1768 * Revision 1.4 2004/10/14 11:31:53 liekweg
1771 * Revision 1.3 2004/10/13 13:36:28 rubino
1774 * Revision 1.2 2004/10/11 15:56:09 liekweg
1775 * Cleanup, comments ...
1776 * Added init func --flo
1778 * Revision 1.1 2004/10/11 09:31:06 liekweg
1779 * First Import of XML reading procs --flo