2 * Copyright (C) 1995-2008 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
22 * @brief Read descriptions of external effects
23 * @author Florian, Boris Boesler
33 #include <libxml/xmlmemory.h>
34 #include <libxml/parser.h>
35 #include <libxml/encoding.h>
41 #include "pseudo_irg.h"
50 # define MY_ENCODING "ISO-8859-1"
52 # define CHECK(ptr,msg) assert (ptr && msg)
54 # define NODE_NAME(n, m) (0 == xmlStrcmp (n->name, (const xmlChar*) #m))
55 # define CHECK_NAME(n, m) assert (0 == xmlStrcmp (n->name, (const xmlChar*) #m))
58 #define VERBOSE_PRINTING 0
61 # define VERBOSE_PRINT(s) fprintf s
63 # define VERBOSE_PRINT(s)
68 static type_t *types = NULL;
69 static entity_t *entities = NULL;
70 static proc_t *procs = NULL;
71 static module_t *modules = NULL;
74 static module_t *current_module = NULL;
77 /* this is only used inside a VERBOSE_PRINT() call */
78 static const char *effect_string[] = {
91 #endif /* defined VERBOSE_PRINTING */
94 getNodeModuleIdent (xmlNodePtr node)
96 const char *mod_str = (const char*) xmlGetProp (node, BAD_CAST "module");
98 if (NULL == mod_str) {
101 ident *res = new_id_from_str (mod_str);
107 getNodeProcName (xmlNodePtr node)
109 const char *proc_str = (const char*) xmlGetProp (node, BAD_CAST "procname");
116 getNodeClassName (xmlNodePtr node)
118 char *proc_str = (char*) xmlGetProp (node, BAD_CAST "class");
120 return ( (proc_str));
122 # endif /* defined NEEDED */
125 getNodeId (xmlNodePtr node)
127 const char *id_str = (const char*) xmlGetProp (node, BAD_CAST "id");
133 getNodeRefId (xmlNodePtr node)
135 const char *refid_str = (char*) xmlGetProp (node, BAD_CAST "refid");
137 return ((refid_str));
141 getNodeTypeId (xmlNodePtr node)
143 const char *type_str = (char*) xmlGetProp (node, BAD_CAST "type");
149 *getNodeTypeStr (xmlNodePtr node)
151 const char *type_str = (char*) xmlGetProp (node, BAD_CAST "type");
157 getNodeOwnerStr (xmlNodePtr node)
159 const char *owner_str = (char*) xmlGetProp (node, BAD_CAST "owner");
165 *getNodeEntityStr (xmlNodePtr node)
167 const char *ent_str = (char*) xmlGetProp (node, BAD_CAST "ir_entity");
179 type_t *getTypeByIdent (ident *id)
181 type_t *curr = types; // @@@ TODO module -> types
183 while (NULL != curr) {
184 if (id == curr -> type_ident) {
192 # endif /* defined NEEDED */
196 type_t *getTypeById (ident *id)
198 type_t *curr = types; // which ones?
200 while (NULL != curr) {
201 if (id == curr -> id) {
209 # endif /* defined NEEDED */
213 entity_t *getEntityByIdents (ident *name, ident *tp_ident)
215 entity_t *curr = entities; // TODO module -> entities
217 while (NULL != curr) {
218 if ((name == curr -> ent_ident)
219 && (tp_ident == curr -> tp_ident)) {
227 # endif /* defined NEEDED */
230 entity_t *getEntityById (ident *id)
232 entity_t *curr = entities;
234 while (NULL != curr) {
235 if (id == curr -> id) {
246 proc_t *getEffectByName (ident *proc_ident)
248 proc_t *curr_effs = procs;
250 while (NULL != curr_effs) {
251 if (proc_ident == curr_effs -> proc_ident) {
254 curr_effs = curr_effs->next;
259 # endif /* defined NEEDED */
262 xmlNodePtr get_any_valid_child(xmlNodePtr elem)
266 assert(elem && "no element");
267 child = elem -> xmlChildrenNode;
268 while(child && (NODE_NAME (child, comment))) {
269 child = child -> next;
275 xmlNodePtr get_valid_child(xmlNodePtr elem)
279 child = get_any_valid_child(elem);
280 assert(child && "lost child in deep black forest");
285 * parse XML structure and construct an additional structure
288 parseArg (xmlDocPtr doc, xmlNodePtr argelm)
297 CHECK_NAME (argelm, arg);
298 VERBOSE_PRINT ((stdout, "arg node \t0x%08x\n", (int) argelm));
300 id = getNodeId (argelm);
301 VERBOSE_PRINT ((stdout, "arg->id = \"%s\"\n", id));
302 num_str = (char*) xmlGetProp (argelm, BAD_CAST "number");
303 num = atoi (num_str);
304 VERBOSE_PRINT ((stdout, "arg->no = \"%d\"\n", num));
306 typeid = getNodeTypeStr (argelm);
308 arg = XMALLOC(eff_t);
309 arg -> kind = eff_arg;
310 arg -> id = new_id_from_str(id);
311 arg -> effect.arg.num = num;
312 arg -> effect.arg.type_ident = new_id_from_str(typeid);
318 parseValref (xmlDocPtr doc, xmlNodePtr valelm)
324 CHECK_NAME (valelm, valref);
325 VERBOSE_PRINT ((stdout, "valref node \t0x%08x\n", (int) valelm));
327 ref_id = getNodeRefId (valelm);
328 VERBOSE_PRINT ((stdout, "val->refid = \"%s\"\n", ref_id));
330 valref = XMALLOC(eff_t);
331 valref->kind = eff_valref;
332 valref-> id = new_id_from_str(ref_id);
338 parseSelect (xmlDocPtr doc, xmlNodePtr selelm)
340 ident *entity_id = new_id_from_str(getNodeEntityStr (selelm));
343 eff_t *valref = NULL;
344 eff_t *sel = XMALLOC(eff_t);
345 sel->kind = eff_select;
347 CHECK_NAME (selelm, select);
348 VERBOSE_PRINT ((stdout, "select node \t0x%08x\n", (int) selelm));
350 ent = getEntityById (entity_id);
351 assert(ent && "ir_entity not found");
352 VERBOSE_PRINT ((stdout, "select ir_entity %s\n", get_id_str(ent -> ent_ident)));
354 child = selelm->xmlChildrenNode;
357 valref = parseValref (doc, child);
360 sel-> id = valref ? valref-> id : NO_ID;
361 sel-> effect.select.ent = ent;
371 parseLoad (xmlDocPtr doc, xmlNodePtr loadelm)
376 eff_t *load = XMALLOC(eff_t);
377 load->kind = eff_load;
379 CHECK_NAME (loadelm, load);
380 VERBOSE_PRINT ((stdout, "load node \t0x%08x\n", (int) loadelm));
381 id = new_id_from_str(getNodeId (loadelm));
383 child = get_valid_child(loadelm);
384 if(NODE_NAME (child, select)) {
385 sel = parseSelect (doc, child);
386 load-> effect.load.ent = sel-> effect.select.ent;
387 VERBOSE_PRINT ((stdout, "load ir_entity \t%s\n",
388 get_id_str(load -> effect.load.ent -> ent_ident)));
391 sel = parseValref (doc, child);
392 load-> effect.load.ent = NULL;
396 load-> effect.load.ptrrefid = sel-> id;
404 parseStore (xmlDocPtr doc, xmlNodePtr storeelm)
409 eff_t *store = XMALLOC(eff_t);
410 store->kind = eff_store;
412 CHECK_NAME (storeelm, store);
413 VERBOSE_PRINT ((stdout, "store node \t0x%08x\n", (int) storeelm));
415 child = get_valid_child(storeelm);
416 if(NODE_NAME (child, select)) {
417 sel = parseSelect (doc, child);
418 store-> effect.store.ent = sel-> effect.select.ent;
421 sel = parseValref (doc, child);
422 store-> effect.store.ent = NULL;
426 valref = parseValref (doc, child);
428 store-> effect.store.ptrrefid = sel-> id;
429 store-> effect.store.valrefid = valref-> id;
438 parseAlloc (xmlDocPtr doc, xmlNodePtr allocelm)
442 eff_t *alloc = XMALLOC(eff_t); /* ...! */
443 alloc->kind = eff_alloc;
446 CHECK_NAME (allocelm, alloc);
447 VERBOSE_PRINT ((stdout, "alloc node \t0x%08x\n", (int) allocelm));
448 id = new_id_from_str(getNodeId (allocelm));
449 VERBOSE_PRINT ((stdout, "alloc->id = \"%s\"\n", get_id_str(id)));
450 type_id = new_id_from_str(getNodeTypeId (allocelm));
451 VERBOSE_PRINT ((stdout, "alloc->type_id = \"%s\"\n", get_id_str(type_id)));
454 alloc-> effect.alloc.tp_id = type_id;
460 parseCall (xmlDocPtr doc, xmlNodePtr callelm)
467 eff_t *call = XMALLOC(eff_t);
468 call->kind = eff_call;
470 CHECK_NAME (callelm, call);
471 VERBOSE_PRINT ((stdout, "call node \t0x%08x\n", (int) callelm));
472 id = new_id_from_str(getNodeId (callelm));
473 VERBOSE_PRINT ((stdout, "call->id = \"%s\"\n", get_id_str(id)));
475 child = get_valid_child(callelm);
476 if(NODE_NAME (child, select)) {
477 sel = parseSelect (doc, child);
478 call-> effect.call.ent = sel-> effect.select.ent;
481 sel = parseValref (doc, child);
482 call-> effect.call.ent = NULL;
485 arg = child = child->next;
488 while (NULL != child) {
494 call-> effect.call.valrefid = sel-> id;
495 call-> effect.call.n_args = n_args;
496 call-> effect.call.args = NULL;
501 ident **args = XMALLOCN(ident*, n_args);
504 while (NULL != arg) {
505 eff_t *valref = parseValref (doc, arg);
506 args [i ++] = valref-> id;
511 call-> effect.call.args = args;
518 parseJoin (xmlDocPtr doc, xmlNodePtr joinelm)
525 eff_t *join = XMALLOC(eff_t);
526 join->kind = eff_join;
528 CHECK_NAME (joinelm, join);
529 VERBOSE_PRINT ((stdout, "join node \t0x%08x\n", (int) joinelm));
530 id = new_id_from_str(getNodeId (joinelm));
531 VERBOSE_PRINT ((stdout, "join->id = \"%s\"\n", get_id_str(id)));
533 child = get_valid_child(joinelm);
536 while (NULL != child) {
541 ins = XMALLOCN(ident*, n_ins);
543 child = get_valid_child(joinelm);
545 while (NULL != child) {
546 eff_t *valref = parseValref (doc, child);
547 ins [i ++] = valref-> id;
553 join-> effect.join.n_ins = n_ins;
554 join-> effect.join.ins = ins;
560 parseUnknown (xmlDocPtr doc, xmlNodePtr unknownelm)
563 eff_t *unknown = XMALLOC(eff_t);
564 unknown->kind = eff_unknown;
567 CHECK_NAME (unknownelm, unknown);
568 VERBOSE_PRINT ((stdout, "unknown node \t0x%08x\n", (int) unknownelm));
569 id = new_id_from_str(getNodeId (unknownelm));
576 parseReturn (xmlDocPtr doc, xmlNodePtr retelm)
579 eff_t *ret = XMALLOC(eff_t);
582 CHECK_NAME (retelm, ret);
583 VERBOSE_PRINT ((stdout, "ret node \t0x%08x\n", (int) retelm));
585 child = get_any_valid_child(retelm);
588 eff_t *valref = parseValref (doc, child);
589 ret-> effect.ret.ret_id = valref-> id;
592 ret-> effect.ret.ret_id = NO_ID;
599 parseRaise (xmlDocPtr doc, xmlNodePtr raiseelm)
604 eff_t *raise = XMALLOC(eff_t);
605 raise->kind = eff_raise;
607 CHECK_NAME (raiseelm, raise);
608 VERBOSE_PRINT ((stdout, "raise node \t0x%08x\n", (int) raiseelm));
609 tp_id = getNodeTypeId (raiseelm);
610 VERBOSE_PRINT ((stdout, "raise->type = \"%s\"\n", tp_id));
611 child = get_valid_child(raiseelm);
613 assert (NULL != child);
615 valref = parseValref (doc, child);
616 raise-> effect.raise.valref = valref-> id;
617 raise-> effect.raise.tp_id = new_id_from_str(tp_id);
628 /** parse a type node and insert it into the list */
630 parseType (xmlDocPtr doc, xmlNodePtr typeelm)
633 const char *tp_id = getNodeId (typeelm);
634 VERBOSE_PRINT ((stdout, "type node \t0x%08x (%s)\n", (int) typeelm, tp_id));
635 VERBOSE_PRINT ((stdout, "type = \"%s\"\n", getNodeTypeStr (typeelm)));
638 type = XMALLOC(type_t);
639 type -> type_ident = new_id_from_str(getNodeTypeStr (typeelm));
640 type -> id = new_id_from_str(tp_id);
646 /** parse an ir_entity node and insert it into the list */
648 parseEntity (xmlDocPtr doc, xmlNodePtr entelm)
650 entity_t *ent = XMALLOC(entity_t);
654 const char *ent_id = getNodeId (entelm);
655 /* fprintf (stdout, "ir_entity node \t0x%08x (%d)\n", (int) entelm, ent_id); */
656 VERBOSE_PRINT ((stdout, "ent = \"%s.%s\"\n",
657 getNodeTypeStr (entelm),
658 getNodeEntityStr (entelm)));
661 ent -> ent_ident = new_id_from_str (getNodeEntityStr (entelm));
662 ent -> tp_ident = new_id_from_str (getNodeTypeStr (entelm));
663 ent -> owner = new_id_from_str (getNodeOwnerStr (entelm));
664 ent -> id = new_id_from_str(ent_id);
666 ent->prev = entities;
670 /** parse any effect, and turn it into an eff_t (TODO) */
672 parseEffect (xmlDocPtr doc, xmlNodePtr effelm)
675 const char *procname = getNodeProcName (effelm);
676 const char *ownerid = getNodeOwnerStr (effelm);
677 proc_t *curr_effs = NULL;
681 VERBOSE_PRINT ((stdout, "effect for method \"%s\"\n", procname));
683 cur = effelm -> xmlChildrenNode;
684 while (NULL != cur) {
688 VERBOSE_PRINT ((stdout, "has %d effects\n", n_effs));
690 curr_effs = XMALLOC(proc_t);
691 curr_effs -> proc_ident = new_id_from_str(procname);
692 curr_effs -> ownerid = new_id_from_str(ownerid);
693 curr_effs->effs = XMALLOCN(eff_t*, n_effs);
695 cur = effelm -> xmlChildrenNode;
696 while (NULL != cur) {
699 if (NODE_NAME (cur, arg)) {
700 eff = (eff_t*) parseArg (doc, cur);
701 } else if (NODE_NAME (cur, load)) {
702 eff = (eff_t*) parseLoad (doc, cur);
703 } else if (NODE_NAME (cur, store)) {
704 eff = (eff_t*) parseStore (doc, cur);
705 } else if (NODE_NAME (cur, alloc)) {
706 eff = (eff_t*) parseAlloc (doc, cur);
707 } else if (NODE_NAME (cur, call)) {
708 eff = (eff_t*) parseCall (doc, cur);
709 } else if (NODE_NAME (cur, join)) {
710 eff = (eff_t*) parseJoin (doc, cur);
711 } else if (NODE_NAME (cur, unknown)) {
712 eff = (eff_t*) parseUnknown (doc, cur);
713 } else if (NODE_NAME (cur, ret)) {
714 eff = (eff_t*) parseReturn (doc, cur);
715 } else if (NODE_NAME (cur, raise)) {
716 eff = (eff_t*) parseRaise (doc, cur);
717 } else if (NODE_NAME (cur, comment)) {
721 fprintf (stderr, "wrong element \"%s\"\n", BAD_CAST cur->name);
725 VERBOSE_PRINT ((stdout, "effect %p@%d\n", (void*)eff, i));
726 curr_effs -> effs[i++] = eff;
730 assert((i == n_effs) && "incorrect number of effects");
731 curr_effs -> n_effs = n_effs;
732 curr_effs -> next = procs;
738 int read_extern (const char *filename)
740 /* xmlNsPtr ns = NULL; */ /* no namespace for us */
741 xmlDocPtr doc; /* whole document */
742 xmlNodePtr cur; /* current node */
746 /* i've got no idea what the VERSION cast is all about. voodoo
747 programming at its finest. */
748 LIBXML_TEST_VERSION xmlKeepBlanksDefault (0);
749 VERBOSE_PRINT((stdout, "read file %s\n", filename));
750 doc = xmlParseFile (filename);
754 cur = xmlDocGetRootElement (doc);
755 CHECK (cur, "xmlDocGetRootElement");
757 if (! NODE_NAME (cur, effects)) {
758 fprintf (stderr,"root node \"%s\" != \"effects\"\n", BAD_CAST cur->name);
763 mod_id = getNodeModuleIdent (cur);
764 if (NULL != mod_id) {
765 VERBOSE_PRINT ((stdout, "effects for \"%s\"\n",
766 get_id_str(mod_id)));
769 VERBOSE_PRINT ((stdout, "effects \t0x%08x\n", (int) cur));
773 cur = cur->xmlChildrenNode;
774 while (cur != NULL) {
775 if (NODE_NAME (cur, type)) {
776 parseType (doc, cur);
777 } else if (NODE_NAME (cur, ir_entity)) {
778 parseEntity (doc, cur);
779 } else if (NODE_NAME (cur, effect)) {
780 parseEffect (doc, cur);
781 } else if ((NODE_NAME (cur, comment))) {
784 fprintf (stderr, "wrong element \"%s\"\n", BAD_CAST cur->name);
790 module = XMALLOC(module_t);
791 module -> id = mod_id;
792 module -> types = types;
793 module -> entities = entities;
794 module -> procs = procs;
800 module -> next = modules;
806 /********************************************************************/
809 * free additional structure
812 void freeArg (eff_t *arg)
814 VERBOSE_PRINT ((stdout, "free arg node \t0x%08x\n", (int) arg));
819 void freeValref (eff_t *valref)
821 VERBOSE_PRINT ((stdout, "free valref node \t0x%08x\n", (int) valref));
826 void freeSelect (eff_t *sel)
828 VERBOSE_PRINT ((stdout, "free select node \t0x%08x\n", (int) sel));
833 void freeLoad (eff_t *load)
835 VERBOSE_PRINT ((stdout, "free load node \t0x%08x\n", (int) load));
840 void freeStore (eff_t *store)
842 VERBOSE_PRINT ((stdout, "free store node \t0x%08x\n", (int) store));
847 void freeAlloc (eff_t *alloc)
849 VERBOSE_PRINT ((stdout, "free alloc node \t0x%08x\n", (int) alloc));
854 void freeCall (eff_t *call)
856 VERBOSE_PRINT ((stdout, "free call node \t0x%08x\n", (int) call));
857 free(call -> effect.call.args);
862 void freeJoin (eff_t *join)
864 VERBOSE_PRINT ((stdout, "free join node \t0x%08x\n", (int) join));
865 free(join -> effect.join.ins);
870 void freeUnknown (eff_t *unknown)
872 VERBOSE_PRINT ((stdout, "free unknown node \t0x%08x\n", (int) unknown));
877 void freeReturn (eff_t *ret)
879 VERBOSE_PRINT ((stdout, "free ret node \t0x%08x\n", (int) ret));
884 void freeRaise (eff_t *raise)
886 VERBOSE_PRINT ((stdout, "free raise node \t0x%08x\n", (int) raise));
892 void freeProcEffs(proc_t *proc)
897 VERBOSE_PRINT ((stdout, "free effect for method \"%s\"\n",
898 get_id_str(proc -> proc_ident)));
900 num = proc -> n_effs;
901 for(i = 0; i < num; i++) {
902 switch(proc -> effs[i] -> kind) {
904 freeArg(proc -> effs[i]);
907 freeValref(proc -> effs[i]);
910 freeSelect(proc -> effs[i]);
913 freeLoad(proc -> effs[i]);
916 freeStore(proc -> effs[i]);
919 freeAlloc(proc -> effs[i]);
922 freeCall(proc -> effs[i]);
925 freeUnknown(proc -> effs[i]);
928 freeJoin(proc -> effs[i]);
931 freeRaise(proc -> effs[i]);
934 freeReturn(proc -> effs[i]);
937 assert(0 && "try to free an unknown effect");
946 void freeModuleProcs(module_t *module)
948 proc_t *next_proc, *proc;
950 VERBOSE_PRINT ((stdout, "free procs for module \"%s\"\n",
951 get_id_str(module -> id)));
953 proc = module -> procs;
955 next_proc = proc -> next;
965 module_t *module, *next_module;
969 freeModuleProcs(module);
970 next_module = module -> next;
972 module = next_module;
976 /********************************************************************/
979 type_t *find_type_in_module(module_t *module, ident *typeid)
983 for(type = module -> types; type; type = type -> prev) {
984 VERBOSE_PRINT((stdout, "test typeid %s\n", get_id_str(type -> id)));
985 if(type -> id == typeid) {
986 VERBOSE_PRINT((stdout, "found\n"));
990 VERBOSE_PRINT((stdout, "did not find type id %s\n", get_id_str(typeid)));
994 /********************************************************************/
996 static void add_value_to_proc(proc_t *proc, eff_t *eff)
998 eff -> next = proc -> values;
999 proc -> values = eff;
1003 eff_t *find_valueid_in_proc_effects(ident *id, proc_t *proc)
1007 val = proc -> values;
1009 if(id == val -> id) {
1017 static void create_abstract_return(ir_graph *irg, proc_t *proc, eff_t *eff)
1022 VERBOSE_PRINT((stdout, "create effect:return in %s\n",
1023 get_id_str(proc -> proc_ident)));
1024 if(NO_ID == eff -> effect.ret.ret_id) {
1026 x = new_Return (get_store(), 0, NULL);
1031 /* return one value */
1032 eff_res = find_valueid_in_proc_effects(eff -> effect.ret.ret_id, proc);
1033 assert(eff_res -> firmnode && "firm in effect not set");
1034 in[0] = eff_res -> firmnode;
1035 x = new_Return (get_store(), 1, in);
1037 eff -> firmnode = x;
1039 /* Now we generated all instructions for this block and all its predecessor
1040 * blocks so we can mature it. (There are not too much.) */
1041 mature_immBlock (get_irg_current_block(irg));
1043 /* This adds the in edge of the end block which originates at the return statement.
1044 * The return node passes controlflow to the end block. */
1045 add_immBlock_pred (get_irg_end_block(irg), x);
1049 static void create_abstract_arg(ir_graph *irg, proc_t *proc, eff_t *eff)
1057 VERBOSE_PRINT((stdout, "create effect:arg %d in %s\n",
1058 eff -> effect.arg.num, get_id_str(proc -> proc_ident)));
1059 ent = get_irg_entity(irg);
1060 typ = get_entity_type(ent);
1062 /* read argument eff -> effect.arg.num and place in values list */
1063 num = get_method_n_params(typ);
1064 assert((num >= eff -> effect.arg.num) && "number too big");
1065 typ = get_method_param_type(typ, eff -> effect.arg.num);
1066 mode = get_type_mode(typ);
1068 arg = new_Proj(get_irg_args(irg), mode, eff -> effect.arg.num);
1069 eff -> firmnode = arg;
1071 add_value_to_proc(proc, eff);
1075 static void create_abstract_load(ir_graph *irg, proc_t *proc, eff_t *eff)
1077 ir_node *sel, *load;
1083 VERBOSE_PRINT((stdout, "create load in %s\n",
1084 get_id_str(proc -> proc_ident)));
1086 if(eff -> effect.load.ent) {
1087 ent = eff -> effect.load.ent -> f_ent;
1088 VERBOSE_PRINT((stdout, "load from %s\n", get_entity_name(ent)));
1091 VERBOSE_PRINT((stdout, "store to memory\n"));
1095 addr = find_valueid_in_proc_effects(eff -> effect.load.ptrrefid, proc);
1096 assert(addr && "no address for load");
1097 /* if addr is Unknown, set proper mode */
1098 if(iro_Unknown == get_irn_opcode(addr -> firmnode)) {
1099 set_irn_mode(addr -> firmnode, mode_P);
1103 sel = new_simpleSel(get_store(), addr -> firmnode, ent);
1104 mode = get_type_mode(get_entity_type(ent));
1107 sel = addr -> firmnode;
1110 load = new_Load(get_store(), sel, mode);
1111 set_store(new_Proj(load, mode_M, 0));
1112 eff -> firmnode = new_Proj(load, mode, 2);
1114 add_value_to_proc(proc, eff);
1118 static void create_abstract_store(ir_graph *irg, proc_t *proc, eff_t *eff)
1120 ir_node *sel, *store;
1125 VERBOSE_PRINT((stdout, "create store in %s\n",
1126 get_id_str(proc -> proc_ident)));
1128 if(eff -> effect.store.ent) {
1129 ent = eff -> effect.store.ent -> f_ent;
1130 VERBOSE_PRINT((stdout, "store to ir_entity %s\n", get_entity_name(ent)));
1133 VERBOSE_PRINT((stdout, "store to memory\n"));
1137 addr = find_valueid_in_proc_effects(eff -> effect.store.ptrrefid, proc);
1138 assert(addr && "no address for store");
1139 /* if addr is Unknown, set propper mode */
1140 if(iro_Unknown == get_irn_opcode(addr -> firmnode)) {
1141 set_irn_mode(addr -> firmnode, mode_P);
1144 val = find_valueid_in_proc_effects(eff -> effect.store.valrefid, proc);
1145 assert(val && "no address for store");
1146 /* if addr is Unknown, set propper mode */
1147 if(iro_Unknown == get_irn_opcode(val -> firmnode)) {
1148 set_irn_mode(val -> firmnode, get_type_mode(get_entity_type(ent)));
1152 sel = new_simpleSel(get_store(), addr -> firmnode, ent);
1155 sel = addr -> firmnode;
1157 store = new_Store(get_store(), sel, val -> firmnode);
1158 set_store(new_Proj(store, mode_M, 0));
1159 eff -> firmnode = store;
1163 static void create_abstract_alloc(ir_graph *irg, proc_t *proc, eff_t *eff)
1168 symconst_symbol sym;
1171 VERBOSE_PRINT((stdout, "create alloc in %s\n",
1172 get_id_str(proc -> proc_ident)));
1174 xtype = find_type_in_module(current_module, eff -> effect.alloc.tp_id);
1175 assert(xtype && "type not found");
1176 ftype = xtype -> f_tp;
1179 alloc = new_Alloc(get_store(), new_SymConst(mode_Is, sym, symconst_type_size), ftype,
1181 set_store(new_Proj(alloc, mode_M, 0));
1182 eff -> firmnode = new_Proj(alloc, mode_P, 2);
1184 add_value_to_proc(proc, eff);
1188 static void create_abstract_unknown(ir_graph *irg, proc_t *proc, eff_t *eff)
1193 VERBOSE_PRINT((stdout, "create unknown in %s\n",
1194 get_id_str(proc -> proc_ident)));
1196 unknown = new_Unknown(mode_ANY);
1197 eff -> firmnode = unknown;
1199 add_value_to_proc(proc, eff);
1203 static void create_abstract_call(ir_graph *irg, proc_t *proc, eff_t *eff)
1205 ir_node *sel, *call;
1211 int mik; /* is method somehow known? */
1214 VERBOSE_PRINT((stdout, "create call in %s\n",
1215 get_id_str(proc -> proc_ident)));
1217 if(eff -> effect.call.ent) {
1218 ent = eff -> effect.call.ent -> f_ent;
1219 VERBOSE_PRINT((stdout, "call %s\n", get_entity_name(ent)));
1223 VERBOSE_PRINT((stdout, "call something in memory\n"));
1226 addr = find_valueid_in_proc_effects(eff -> effect.call.valrefid, proc);
1227 assert(addr && "no address for load");
1228 /* if addr is Unknown, set proper mode */
1229 if(iro_Unknown == get_irn_opcode(addr -> firmnode)) {
1230 set_irn_mode(addr -> firmnode, mode_P);
1235 sel = new_simpleSel(get_store(), addr -> firmnode, ent);
1237 mtype = get_entity_type(ent);
1242 sel = addr -> firmnode;
1244 mtype = get_unknown_type();
1249 num = eff -> effect.call.n_args;
1250 VERBOSE_PRINT((stdout, "number of args given: %d\n", num));
1252 VERBOSE_PRINT((stdout, "number of args expected: %d\n",
1253 get_method_n_params(mtype)));
1255 irns = ALLOCAN(ir_node*, num);
1256 for(i = 0; i < num; i++) {
1257 irns[i] = find_valueid_in_proc_effects(eff -> effect.call.args[i], proc)
1259 if(iro_Unknown == get_irn_opcode(irns[i])) {
1261 set_irn_mode(irns[i], get_type_mode(get_method_param_type(mtype, i)));
1264 set_irn_mode(irns[i], mode_ANY);
1268 call = new_Call(get_store(), sel, num, irns, mtype);
1269 set_store(new_Proj(call, mode_M, 0));
1270 if(mik && (0 != get_method_n_ress(mtype))) {
1271 eff -> firmnode = new_Proj(call,
1272 get_type_mode(get_method_res_type(mtype, 0)),
1274 add_value_to_proc(proc, eff); /* result can be accessed */
1277 eff -> firmnode = NULL; /* result can not be accessed */
1281 static void create_abstract_join (ir_graph *irg, proc_t *proc, eff_t *eff)
1283 ir_node **ins = NULL;
1284 ir_node *unknown = NULL;
1285 ir_node *cond = NULL;
1286 ir_node *block = NULL;
1287 ir_node *c_block = NULL;
1288 ir_node *phi = NULL;
1289 ir_mode *join_md = mode_ANY;
1294 VERBOSE_PRINT((stdout, "create join in %s\n",
1295 get_id_str(proc -> proc_ident)));
1297 assert (eff_join == eff->kind);
1299 n_ins = eff->effect.join.n_ins;
1301 /* seems like current_block is not always mature at this point */
1302 mature_immBlock (get_cur_block ());
1304 block = get_cur_block (); /* remember this so we can put the ProjXs into it */
1306 /* jump based on an unknown condition so all values are possible */
1307 unknown = new_Unknown (mode_Iu);
1308 cond = new_Cond (unknown);
1310 c_block = new_immBlock (); /* for the Phi after the branch(es) */
1311 set_cur_block(c_block);
1313 ins = XMALLOCN(ir_node*, n_ins);
1314 for (i = 0; i < n_ins; i ++) {
1315 ir_node *projX = NULL;
1316 ir_node *s_block = NULL;
1317 ir_node *jmp = NULL;
1320 /* make sure the projX is in the 'switch' block */
1321 set_cur_block (block);
1322 projX = new_Proj (cond, mode_X, (long) i);
1324 /* this also sets current_block, so the rest of the code ends up there: */
1325 s_block = new_immBlock ();
1326 set_cur_block(s_block);
1328 add_immBlock_pred (s_block, projX);
1329 mature_immBlock (s_block);
1331 in_eff = find_valueid_in_proc_effects (eff->effect.join.ins [i], proc);
1333 ins [i] = in_eff->firmnode;
1335 /* need to find a suitable mode for the Phi node */
1336 if (mode_ANY != get_irn_mode (ins [i])) {
1337 join_md = get_irn_mode (ins [i]);
1341 add_immBlock_pred (c_block, jmp);
1344 set_cur_block (c_block);
1346 phi = new_Phi (n_ins, ins, join_md);
1348 mature_immBlock (c_block);
1351 eff->firmnode = phi;
1353 add_value_to_proc (proc, eff);
1356 static void create_abstract_raise (ir_graph *irg, proc_t *proc, eff_t *eff)
1358 ir_node *block = NULL;
1359 ir_node *unknown = NULL;
1360 ir_node *cond = NULL;
1362 /* seems like current_block is not always mature at this point */
1363 mature_immBlock (get_cur_block ());
1364 block = get_cur_block (); /* remember this so we can put the ProjXs into it */
1366 /* jump based on an unknown condition so both values are possible */
1367 unknown = new_Unknown (mode_Iu);
1368 cond = new_Cond (unknown);
1370 /* one branch for 'throw-exception' case */
1372 ir_node *projX = new_Proj (cond, mode_X, 1L);
1373 ir_node *b_exc = new_immBlock ();
1374 ir_node *obj = NULL;
1375 ir_node *thrw = NULL;
1376 eff_t *thrw_eff = NULL;
1378 set_cur_block(b_exc);
1379 add_immBlock_pred (b_exc, projX);
1381 thrw_eff = find_valueid_in_proc_effects (eff->effect.raise.valref, proc);
1382 obj = thrw_eff->firmnode;
1384 thrw = new_Raise (get_store (), obj);
1385 /* exc-jump to end block */
1386 thrw = new_Proj (thrw, mode_X, 0L);
1388 add_immBlock_pred (get_irg_end_block (irg), thrw);
1389 mature_immBlock (get_cur_block ());
1392 set_cur_block (block); /* back to the first block */
1394 /* one branch for 'non-exception' case */
1397 ir_node *projX = new_Proj (cond, mode_X, 0);
1398 block = new_immBlock ();
1399 set_cur_block(block);
1400 add_immBlock_pred (block, projX);
1401 mature_immBlock (block);
1402 /* continue building in current_block */
1407 static void create_abstract_firm(module_t *module, proc_t *proc, ir_entity *fent)
1414 /* test ir_entity */
1415 assert(visibility_external_allocated == get_entity_visibility(fent)
1416 && peculiarity_existent == get_entity_peculiarity(fent)
1417 && "not an abstract ir_entity");
1418 /* create irg in ir_entity */
1419 irg = new_pseudo_ir_graph(fent, 0);
1420 set_irg_inline_property(irg, irg_inline_forbidden);
1422 /* @@@ If the spec says so: */
1423 set_entity_visibility(fent, visibility_local);
1425 VERBOSE_PRINT((stdout, "create effects for %s\n",
1426 get_id_str(proc -> proc_ident)));
1428 /* create effects in irg */
1429 num = proc -> n_effs;
1430 for(i = 0; i < num; i++) {
1431 eff = proc -> effs[i];
1432 VERBOSE_PRINT((stdout,
1433 "create effect \"%s\"\n", effect_string[(int)eff -> kind]));
1434 switch(eff -> kind) {
1436 create_abstract_return(irg, proc, eff);
1439 create_abstract_arg(irg, proc, eff);
1442 create_abstract_load(irg, proc, eff);
1445 create_abstract_store(irg, proc, eff);
1448 create_abstract_unknown(irg, proc, eff);
1451 create_abstract_alloc(irg, proc, eff);
1454 create_abstract_call(irg, proc, eff);
1457 create_abstract_join(irg, proc, eff);
1460 create_abstract_raise(irg, proc, eff);
1463 assert(0 && "effect not implemented");
1468 /* close irg in ir_entity */
1469 /* Now we can mature the end block as all it's predecessors are known. */
1470 mature_immBlock (get_irg_end_block(irg));
1472 /* Verify the graph. Finds some very bad errors in the graph. */
1473 VERBOSE_PRINT((stdout, "verify graph\n"));
1475 VERBOSE_PRINT((stdout, "finalize construction\n"));
1476 irg_finalize_cons (irg);
1479 /********************************************************************/
1481 static void assign_firm_entity(module_t *module, entity_t *xmlent)
1488 VERBOSE_PRINT((stdout, "assign ir_entity %s to typeid %s\n",
1489 get_id_str(xmlent -> ent_ident),
1490 get_id_str(xmlent -> owner)));
1492 typ = find_type_in_module(module, xmlent -> owner);
1493 assert(typ && "class not found in module");
1495 assert(is_Class_type(type));
1497 num = get_class_n_members(type);
1499 for(i = 0; i < num; i++) {
1500 ent = get_class_member(type, i);
1501 VERBOSE_PRINT((stdout, "compare ir_entity %s and %s\n",
1502 get_id_str(xmlent -> ent_ident), get_entity_name(ent)));
1504 if(get_entity_ident(ent) == xmlent -> ent_ident) {
1509 assert(ent && "did not find a ir_entity");
1511 xmlent -> f_ent = ent;
1514 /********************************************************************/
1515 /* must be primitive type or class type */
1516 static void assign_firm_type(type_t *xmltype)
1519 ir_type *typ = NULL;
1522 VERBOSE_PRINT((stdout, "assign firm type to type %s\n",
1523 get_id_str(xmltype -> type_ident)));
1525 /* is it global type? */
1526 typ = get_glob_type();
1527 if(xmltype -> type_ident == get_type_ident(typ)) {
1529 xmltype -> f_tp = typ;
1530 VERBOSE_PRINT((stdout, "is global type %s\n", get_type_name(typ)));
1532 num = get_irp_n_types();
1533 for(i = 0; i < num; i++) {
1534 typ = get_irp_type(i);
1535 VERBOSE_PRINT((stdout, "test type %s\n", get_type_name(typ)));
1536 if(xmltype -> type_ident == get_type_ident(typ)) {
1537 VERBOSE_PRINT((stdout, "found type %s\n", get_type_name(typ)));
1538 xmltype -> f_tp = typ;
1544 assert(typ && "did not find a type");
1547 /********************************************************************/
1549 void create_abstract_proc_effect(module_t *module, proc_t *proc)
1552 ir_type *class_typ = NULL;
1556 /* find the class of a procedure */
1557 VERBOSE_PRINT((stdout, "do find owner id %s\n", get_id_str(proc -> ownerid)));
1558 type = find_type_in_module(module, proc -> ownerid);
1559 assert(type && "class not found in module");
1561 class_typ = get_glob_type();
1562 VERBOSE_PRINT((stdout, "test type %s\n", get_type_name(class_typ)));
1563 if(type -> type_ident != get_type_ident(class_typ)) {
1564 /* find module as class */
1565 num = get_irp_n_types();
1566 for(i = 0; i < num; i++) {
1567 class_typ = get_irp_type(i);
1568 VERBOSE_PRINT((stdout, "test type %s\n", get_type_name(class_typ)));
1569 if (is_Class_type(class_typ)
1570 && (type -> type_ident == get_type_ident(class_typ))) {
1571 /* found class type */
1572 VERBOSE_PRINT((stdout, "found type %s\n", get_type_name(class_typ)));
1579 VERBOSE_PRINT((stdout, "found global type %s\n", get_type_name(class_typ)));
1581 assert(class_typ && "type not found");
1582 assert(is_Class_type(class_typ) && "is not a class type");
1583 type -> f_tp = class_typ;
1585 /* find ir_entity for procedure in class */
1586 VERBOSE_PRINT((stdout, "find method %s\n",
1587 get_id_str(proc -> proc_ident)));
1589 num = get_class_n_members(class_typ);
1591 for(i = 0; i < num; i++) {
1592 fent = get_class_member(class_typ, i);
1593 VERBOSE_PRINT((stdout, "test proc %s\n", get_entity_name(fent)));
1594 if(proc -> proc_ident == get_entity_ident(fent)) {
1595 VERBOSE_PRINT((stdout, "found proc %s\n",
1596 get_id_str(proc -> proc_ident)));
1597 /* @@@ TODO check args types - not in xml yet */
1598 /* create Firm stuff */
1599 create_abstract_firm(module, proc, fent);
1609 "method %s not found\nNo effects generated\nCandidates are:\n",
1610 get_id_str(proc -> proc_ident));
1611 for(i = 0; i < num; i++) {
1612 fent = get_class_member(class_typ, i);
1613 fprintf(stderr, "%s\n", get_entity_name(fent));
1615 //assert(fent && "procedure not found in class");
1619 void create_abstract_module(module_t *module)
1625 VERBOSE_PRINT((stdout, "create an abstraction for module %s\n",
1626 get_id_str(module -> id)));
1628 VERBOSE_PRINT((stdout, "--handle types for module\n"));
1629 for(type = module -> types; type; type = type -> prev) {
1630 assign_firm_type(type);
1633 VERBOSE_PRINT((stdout, "--handle entities for module\n"));
1635 for(ent = module -> entities; ent; ent = ent -> prev) {
1636 assign_firm_entity(module, ent);
1639 VERBOSE_PRINT((stdout, "--handle procs for module\n"));
1640 for(proc = module -> procs; proc; proc = proc -> next) {
1641 create_abstract_proc_effect(module, proc);
1646 int create_abstraction(const char *filename)
1650 /* read and parse XML file */
1651 if (! read_extern(filename))
1654 /* finished reading and parsing here */
1655 /* build FIRM graphs */
1658 current_module = module;
1659 create_abstract_module(module);
1660 module = module -> next;
1662 current_module = NULL;
1664 /* free data structures */
1676 void free_abstraction(void) {
1677 int i, n_pseudo_irgs = get_irp_n_pseudo_irgs();
1678 for (i = 0; i < n_pseudo_irgs; ++i) {
1679 ir_graph *p_irg = get_irp_pseudo_irg(i);
1680 set_entity_visibility(get_irg_entity(p_irg), visibility_external_allocated);
1681 // @@@ free_pseudo_ir_graph(p_irg);
1686 /********************************************************************/
1691 * Revision 1.27 2007/02/02 12:38:35 matze
1692 * entity is ir_entity now
1694 * Revision 1.26 2006/12/15 12:37:40 matze
1697 * Revision 1.25 2006/06/09 11:26:35 firm
1698 * renamed type to ir_type
1700 * Revision 1.24 2006/05/29 13:34:49 beck
1701 * renamed symconst_size to symconst_type_size
1703 * Revision 1.22 2005/08/16 10:18:35 beck
1704 * create_abstraction() now returns an error code if the file could not
1707 * Revision 1.21 2005/03/10 10:05:38 goetz
1708 * chanmged method name
1710 * Revision 1.20 2005/01/05 14:28:35 beck
1711 * renames all is_x*_type() functions to is_X*_type() to prevent name clash with EDG frontend
1713 * Revision 1.19 2004/12/10 15:14:34 beck
1714 * used xmalloc instead of malloc
1716 * Revision 1.18 2004/12/02 16:21:42 beck
1717 * fixed config.h include
1719 * Revision 1.17 2004/11/23 14:17:31 liekweg
1720 * fenced out currently unneeded static functions
1722 * Revision 1.16 2004/11/11 12:24:52 goetz
1725 * Revision 1.15 2004/11/11 09:28:32 goetz
1726 * treat pseudo irgs special
1727 * parse 'local' from xml files
1729 * Revision 1.14 2004/11/10 14:42:00 boesler
1730 * be more helpful if a method does not exist
1732 * Revision 1.13 2004/11/05 14:00:53 liekweg
1735 * Revision 1.12 2004/11/02 14:30:31 liekweg
1736 * fixed multi-input join (thx, Boris) --flo
1738 * Revision 1.11 2004/10/29 18:51:53 liekweg
1741 * Revision 1.10 2004/10/25 13:52:24 boesler
1742 * seperated read.h (public interface) and read_t.h (types)
1744 * Revision 1.9 2004/10/22 13:51:35 boesler
1745 * prohibit inlining of pseudo ir_graphs
1747 * Revision 1.8 2004/10/22 13:13:27 boesler
1748 * replaced char* by idents, minor fix in Firm codegen for call
1750 * Revision 1.7 2004/10/21 15:31:55 boesler
1751 * added lots of stuff:
1752 * - build abstract syntax trees
1753 * - build Firm graphs for many effects, still todos
1755 * Revision 1.5 2004/10/18 12:48:20 liekweg
1758 * Revision 1.4 2004/10/14 11:31:53 liekweg
1761 * Revision 1.3 2004/10/13 13:36:28 rubino
1764 * Revision 1.2 2004/10/11 15:56:09 liekweg
1765 * Cleanup, comments ...
1766 * Added init func --flo
1768 * Revision 1.1 2004/10/11 09:31:06 liekweg
1769 * First Import of XML reading procs --flo