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
35 #include <libxml/xmlmemory.h>
36 #include <libxml/parser.h>
37 #include <libxml/encoding.h>
43 #include "pseudo_irg.h"
52 # define MY_ENCODING "ISO-8859-1"
54 # define CHECK(ptr,msg) assert (ptr && msg)
56 # define NODE_NAME(n, m) (0 == xmlStrcmp (n->name, (const xmlChar*) #m))
57 # define CHECK_NAME(n, m) assert (0 == xmlStrcmp (n->name, (const xmlChar*) #m))
59 # define NEW(T) (T*)xmalloc(sizeof (T))
62 #define VERBOSE_PRINTING 0
65 # define VERBOSE_PRINT(s) fprintf s
67 # define VERBOSE_PRINT(s)
72 static type_t *types = NULL;
73 static entity_t *entities = NULL;
74 static proc_t *procs = NULL;
75 static module_t *modules = NULL;
78 static module_t *current_module = NULL;
81 /* this is only used inside a VERBOSE_PRINT() call */
82 static const char *effect_string[] = {
95 #endif /* defined VERBOSE_PRINTING */
98 getNodeModuleIdent (xmlNodePtr node)
100 const char *mod_str = (const char*) xmlGetProp (node, BAD_CAST "module");
102 if (NULL == mod_str) {
105 ident *res = new_id_from_str (mod_str);
111 getNodeProcName (xmlNodePtr node)
113 const char *proc_str = (const char*) xmlGetProp (node, BAD_CAST "procname");
120 getNodeClassName (xmlNodePtr node)
122 char *proc_str = (char*) xmlGetProp (node, BAD_CAST "class");
124 return ( (proc_str));
126 # endif /* defined NEEDED */
129 getNodeId (xmlNodePtr node)
131 const char *id_str = (const char*) xmlGetProp (node, BAD_CAST "id");
137 getNodeRefId (xmlNodePtr node)
139 const char *refid_str = (char*) xmlGetProp (node, BAD_CAST "refid");
141 return ((refid_str));
145 getNodeTypeId (xmlNodePtr node)
147 const char *type_str = (char*) xmlGetProp (node, BAD_CAST "type");
153 *getNodeTypeStr (xmlNodePtr node)
155 const char *type_str = (char*) xmlGetProp (node, BAD_CAST "type");
161 getNodeOwnerStr (xmlNodePtr node)
163 const char *owner_str = (char*) xmlGetProp (node, BAD_CAST "owner");
169 *getNodeEntityStr (xmlNodePtr node)
171 const char *ent_str = (char*) xmlGetProp (node, BAD_CAST "ir_entity");
183 type_t *getTypeByIdent (ident *id)
185 type_t *curr = types; // @@@ TODO module -> types
187 while (NULL != curr) {
188 if (id == curr -> type_ident) {
196 # endif /* defined NEEDED */
200 type_t *getTypeById (ident *id)
202 type_t *curr = types; // which ones?
204 while (NULL != curr) {
205 if (id == curr -> id) {
213 # endif /* defined NEEDED */
217 entity_t *getEntityByIdents (ident *name, ident *tp_ident)
219 entity_t *curr = entities; // TODO module -> entities
221 while (NULL != curr) {
222 if ((name == curr -> ent_ident)
223 && (tp_ident == curr -> tp_ident)) {
231 # endif /* defined NEEDED */
234 entity_t *getEntityById (ident *id)
236 entity_t *curr = entities;
238 while (NULL != curr) {
239 if (id == curr -> id) {
250 proc_t *getEffectByName (ident *proc_ident)
252 proc_t *curr_effs = procs;
254 while (NULL != curr_effs) {
255 if (proc_ident == curr_effs -> proc_ident) {
258 curr_effs = curr_effs->next;
263 # endif /* defined NEEDED */
266 xmlNodePtr get_any_valid_child(xmlNodePtr elem)
270 assert(elem && "no element");
271 child = elem -> xmlChildrenNode;
272 while(child && (NODE_NAME (child, comment))) {
273 child = child -> next;
279 xmlNodePtr get_valid_child(xmlNodePtr elem)
283 child = get_any_valid_child(elem);
284 assert(child && "lost child in deep black forest");
289 * parse XML structure and construct an additional structure
292 parseArg (xmlDocPtr doc, xmlNodePtr argelm)
301 CHECK_NAME (argelm, arg);
302 VERBOSE_PRINT ((stdout, "arg node \t0x%08x\n", (int) argelm));
304 id = getNodeId (argelm);
305 VERBOSE_PRINT ((stdout, "arg->id = \"%s\"\n", id));
306 num_str = (char*) xmlGetProp (argelm, BAD_CAST "number");
307 num = atoi (num_str);
308 VERBOSE_PRINT ((stdout, "arg->no = \"%d\"\n", num));
310 typeid = getNodeTypeStr (argelm);
313 arg -> kind = eff_arg;
314 arg -> id = new_id_from_str(id);
315 arg -> effect.arg.num = num;
316 arg -> effect.arg.type_ident = new_id_from_str(typeid);
322 parseValref (xmlDocPtr doc, xmlNodePtr valelm)
328 CHECK_NAME (valelm, valref);
329 VERBOSE_PRINT ((stdout, "valref node \t0x%08x\n", (int) valelm));
331 ref_id = getNodeRefId (valelm);
332 VERBOSE_PRINT ((stdout, "val->refid = \"%s\"\n", ref_id));
334 valref = NEW (eff_t);
335 valref->kind = eff_valref;
336 valref-> id = new_id_from_str(ref_id);
342 parseSelect (xmlDocPtr doc, xmlNodePtr selelm)
344 ident *entity_id = new_id_from_str(getNodeEntityStr (selelm));
347 eff_t *valref = NULL;
348 eff_t *sel = NEW (eff_t);
349 sel->kind = eff_select;
351 CHECK_NAME (selelm, select);
352 VERBOSE_PRINT ((stdout, "select node \t0x%08x\n", (int) selelm));
354 ent = getEntityById (entity_id);
355 assert(ent && "ir_entity not found");
356 VERBOSE_PRINT ((stdout, "select ir_entity %s\n", get_id_str(ent -> ent_ident)));
358 child = selelm->xmlChildrenNode;
361 valref = parseValref (doc, child);
364 sel-> id = valref ? valref-> id : NO_ID;
365 sel-> effect.select.ent = ent;
375 parseLoad (xmlDocPtr doc, xmlNodePtr loadelm)
380 eff_t *load = NEW (eff_t);
381 load->kind = eff_load;
383 CHECK_NAME (loadelm, load);
384 VERBOSE_PRINT ((stdout, "load node \t0x%08x\n", (int) loadelm));
385 id = new_id_from_str(getNodeId (loadelm));
387 child = get_valid_child(loadelm);
388 if(NODE_NAME (child, select)) {
389 sel = parseSelect (doc, child);
390 load-> effect.load.ent = sel-> effect.select.ent;
391 VERBOSE_PRINT ((stdout, "load ir_entity \t%s\n",
392 get_id_str(load -> effect.load.ent -> ent_ident)));
395 sel = parseValref (doc, child);
396 load-> effect.load.ent = NULL;
400 load-> effect.load.ptrrefid = sel-> id;
408 parseStore (xmlDocPtr doc, xmlNodePtr storeelm)
413 eff_t *store = NEW (eff_t);
414 store->kind = eff_store;
416 CHECK_NAME (storeelm, store);
417 VERBOSE_PRINT ((stdout, "store node \t0x%08x\n", (int) storeelm));
419 child = get_valid_child(storeelm);
420 if(NODE_NAME (child, select)) {
421 sel = parseSelect (doc, child);
422 store-> effect.store.ent = sel-> effect.select.ent;
425 sel = parseValref (doc, child);
426 store-> effect.store.ent = NULL;
430 valref = parseValref (doc, child);
432 store-> effect.store.ptrrefid = sel-> id;
433 store-> effect.store.valrefid = valref-> id;
442 parseAlloc (xmlDocPtr doc, xmlNodePtr allocelm)
446 eff_t *alloc = NEW (eff_t); /* ...! */
447 alloc->kind = eff_alloc;
450 CHECK_NAME (allocelm, alloc);
451 VERBOSE_PRINT ((stdout, "alloc node \t0x%08x\n", (int) allocelm));
452 id = new_id_from_str(getNodeId (allocelm));
453 VERBOSE_PRINT ((stdout, "alloc->id = \"%s\"\n", get_id_str(id)));
454 type_id = new_id_from_str(getNodeTypeId (allocelm));
455 VERBOSE_PRINT ((stdout, "alloc->type_id = \"%s\"\n", get_id_str(type_id)));
458 alloc-> effect.alloc.tp_id = type_id;
464 parseCall (xmlDocPtr doc, xmlNodePtr callelm)
471 eff_t *call = NEW (eff_t);
472 call->kind = eff_call;
474 CHECK_NAME (callelm, call);
475 VERBOSE_PRINT ((stdout, "call node \t0x%08x\n", (int) callelm));
476 id = new_id_from_str(getNodeId (callelm));
477 VERBOSE_PRINT ((stdout, "call->id = \"%s\"\n", get_id_str(id)));
479 child = get_valid_child(callelm);
480 if(NODE_NAME (child, select)) {
481 sel = parseSelect (doc, child);
482 call-> effect.call.ent = sel-> effect.select.ent;
485 sel = parseValref (doc, child);
486 call-> effect.call.ent = NULL;
489 arg = child = child->next;
492 while (NULL != child) {
498 call-> effect.call.valrefid = sel-> id;
499 call-> effect.call.n_args = n_args;
500 call-> effect.call.args = NULL;
505 ident **args = (ident**) xmalloc(n_args * sizeof(ident*));
508 while (NULL != arg) {
509 eff_t *valref = parseValref (doc, arg);
510 args [i ++] = valref-> id;
515 call-> effect.call.args = args;
522 parseJoin (xmlDocPtr doc, xmlNodePtr joinelm)
529 eff_t *join = NEW (eff_t);
530 join->kind = eff_join;
532 CHECK_NAME (joinelm, join);
533 VERBOSE_PRINT ((stdout, "join node \t0x%08x\n", (int) joinelm));
534 id = new_id_from_str(getNodeId (joinelm));
535 VERBOSE_PRINT ((stdout, "join->id = \"%s\"\n", get_id_str(id)));
537 child = get_valid_child(joinelm);
540 while (NULL != child) {
545 ins = (ident **) xmalloc (n_ins * sizeof (ident *) );
547 child = get_valid_child(joinelm);
549 while (NULL != child) {
550 eff_t *valref = parseValref (doc, child);
551 ins [i ++] = valref-> id;
557 join-> effect.join.n_ins = n_ins;
558 join-> effect.join.ins = ins;
564 parseUnknown (xmlDocPtr doc, xmlNodePtr unknownelm)
567 eff_t *unknown = NEW (eff_t);
568 unknown->kind = eff_unknown;
571 CHECK_NAME (unknownelm, unknown);
572 VERBOSE_PRINT ((stdout, "unknown node \t0x%08x\n", (int) unknownelm));
573 id = new_id_from_str(getNodeId (unknownelm));
580 parseReturn (xmlDocPtr doc, xmlNodePtr retelm)
583 eff_t *ret = NEW (eff_t);
586 CHECK_NAME (retelm, ret);
587 VERBOSE_PRINT ((stdout, "ret node \t0x%08x\n", (int) retelm));
589 child = get_any_valid_child(retelm);
592 eff_t *valref = parseValref (doc, child);
593 ret-> effect.ret.ret_id = valref-> id;
596 ret-> effect.ret.ret_id = NO_ID;
603 parseRaise (xmlDocPtr doc, xmlNodePtr raiseelm)
608 eff_t *raise = NEW (eff_t);
609 raise->kind = eff_raise;
611 CHECK_NAME (raiseelm, raise);
612 VERBOSE_PRINT ((stdout, "raise node \t0x%08x\n", (int) raiseelm));
613 tp_id = getNodeTypeId (raiseelm);
614 VERBOSE_PRINT ((stdout, "raise->type = \"%s\"\n", tp_id));
615 child = get_valid_child(raiseelm);
617 assert (NULL != child);
619 valref = parseValref (doc, child);
620 raise-> effect.raise.valref = valref-> id;
621 raise-> effect.raise.tp_id = new_id_from_str(tp_id);
632 /** parse a type node and insert it into the list */
634 parseType (xmlDocPtr doc, xmlNodePtr typeelm)
637 const char *tp_id = getNodeId (typeelm);
638 VERBOSE_PRINT ((stdout, "type node \t0x%08x (%s)\n", (int) typeelm, tp_id));
639 VERBOSE_PRINT ((stdout, "type = \"%s\"\n", getNodeTypeStr (typeelm)));
642 type = (type_t*) xmalloc (sizeof (type_t));
643 type -> type_ident = new_id_from_str(getNodeTypeStr (typeelm));
644 type -> id = new_id_from_str(tp_id);
650 /** parse an ir_entity node and insert it into the list */
652 parseEntity (xmlDocPtr doc, xmlNodePtr entelm)
654 entity_t *ent = NEW (entity_t);
658 const char *ent_id = getNodeId (entelm);
659 /* fprintf (stdout, "ir_entity node \t0x%08x (%d)\n", (int) entelm, ent_id); */
660 VERBOSE_PRINT ((stdout, "ent = \"%s.%s\"\n",
661 getNodeTypeStr (entelm),
662 getNodeEntityStr (entelm)));
665 ent -> ent_ident = new_id_from_str (getNodeEntityStr (entelm));
666 ent -> tp_ident = new_id_from_str (getNodeTypeStr (entelm));
667 ent -> owner = new_id_from_str (getNodeOwnerStr (entelm));
668 ent -> id = new_id_from_str(ent_id);
670 ent->prev = entities;
674 /** parse any effect, and turn it into an eff_t (TODO) */
676 parseEffect (xmlDocPtr doc, xmlNodePtr effelm)
679 const char *procname = getNodeProcName (effelm);
680 const char *ownerid = getNodeOwnerStr (effelm);
681 proc_t *curr_effs = NULL;
685 VERBOSE_PRINT ((stdout, "effect for method \"%s\"\n", procname));
687 cur = effelm -> xmlChildrenNode;
688 while (NULL != cur) {
692 VERBOSE_PRINT ((stdout, "has %d effects\n", n_effs));
694 curr_effs = NEW (proc_t);
695 curr_effs -> proc_ident = new_id_from_str(procname);
696 curr_effs -> ownerid = new_id_from_str(ownerid);
697 curr_effs->effs = (eff_t**) xmalloc (n_effs * sizeof (eff_t*));
699 cur = effelm -> xmlChildrenNode;
700 while (NULL != cur) {
703 if (NODE_NAME (cur, arg)) {
704 eff = (eff_t*) parseArg (doc, cur);
705 } else if (NODE_NAME (cur, load)) {
706 eff = (eff_t*) parseLoad (doc, cur);
707 } else if (NODE_NAME (cur, store)) {
708 eff = (eff_t*) parseStore (doc, cur);
709 } else if (NODE_NAME (cur, alloc)) {
710 eff = (eff_t*) parseAlloc (doc, cur);
711 } else if (NODE_NAME (cur, call)) {
712 eff = (eff_t*) parseCall (doc, cur);
713 } else if (NODE_NAME (cur, join)) {
714 eff = (eff_t*) parseJoin (doc, cur);
715 } else if (NODE_NAME (cur, unknown)) {
716 eff = (eff_t*) parseUnknown (doc, cur);
717 } else if (NODE_NAME (cur, ret)) {
718 eff = (eff_t*) parseReturn (doc, cur);
719 } else if (NODE_NAME (cur, raise)) {
720 eff = (eff_t*) parseRaise (doc, cur);
721 } else if (NODE_NAME (cur, comment)) {
725 fprintf (stderr, "wrong element \"%s\"\n", BAD_CAST cur->name);
729 VERBOSE_PRINT ((stdout, "effect %p@%d\n", (void*)eff, i));
730 curr_effs -> effs[i++] = eff;
734 assert((i == n_effs) && "incorrect number of effects");
735 curr_effs -> n_effs = n_effs;
736 curr_effs -> next = procs;
742 int read_extern (const char *filename)
744 /* xmlNsPtr ns = NULL; */ /* no namespace for us */
745 xmlDocPtr doc; /* whole document */
746 xmlNodePtr cur; /* current node */
750 /* i've got no idea what the VERSION cast is all about. voodoo
751 programming at its finest. */
752 LIBXML_TEST_VERSION xmlKeepBlanksDefault (0);
753 VERBOSE_PRINT((stdout, "read file %s\n", filename));
754 doc = xmlParseFile (filename);
758 cur = xmlDocGetRootElement (doc);
759 CHECK (cur, "xmlDocGetRootElement");
761 if (! NODE_NAME (cur, effects)) {
762 fprintf (stderr,"root node \"%s\" != \"effects\"\n", BAD_CAST cur->name);
767 mod_id = getNodeModuleIdent (cur);
768 if (NULL != mod_id) {
769 VERBOSE_PRINT ((stdout, "effects for \"%s\"\n",
770 get_id_str(mod_id)));
773 VERBOSE_PRINT ((stdout, "effects \t0x%08x\n", (int) cur));
777 cur = cur->xmlChildrenNode;
778 while (cur != NULL) {
779 if (NODE_NAME (cur, type)) {
780 parseType (doc, cur);
781 } else if (NODE_NAME (cur, ir_entity)) {
782 parseEntity (doc, cur);
783 } else if (NODE_NAME (cur, effect)) {
784 parseEffect (doc, cur);
785 } else if ((NODE_NAME (cur, comment))) {
788 fprintf (stderr, "wrong element \"%s\"\n", BAD_CAST cur->name);
794 module = NEW(module_t);
795 module -> id = mod_id;
796 module -> types = types;
797 module -> entities = entities;
798 module -> procs = procs;
804 module -> next = modules;
810 /********************************************************************/
813 * free additional structure
816 void freeArg (eff_t *arg)
818 VERBOSE_PRINT ((stdout, "free arg node \t0x%08x\n", (int) arg));
823 void freeValref (eff_t *valref)
825 VERBOSE_PRINT ((stdout, "free valref node \t0x%08x\n", (int) valref));
830 void freeSelect (eff_t *sel)
832 VERBOSE_PRINT ((stdout, "free select node \t0x%08x\n", (int) sel));
837 void freeLoad (eff_t *load)
839 VERBOSE_PRINT ((stdout, "free load node \t0x%08x\n", (int) load));
844 void freeStore (eff_t *store)
846 VERBOSE_PRINT ((stdout, "free store node \t0x%08x\n", (int) store));
851 void freeAlloc (eff_t *alloc)
853 VERBOSE_PRINT ((stdout, "free alloc node \t0x%08x\n", (int) alloc));
858 void freeCall (eff_t *call)
860 VERBOSE_PRINT ((stdout, "free call node \t0x%08x\n", (int) call));
861 free(call -> effect.call.args);
866 void freeJoin (eff_t *join)
868 VERBOSE_PRINT ((stdout, "free join node \t0x%08x\n", (int) join));
869 free(join -> effect.join.ins);
874 void freeUnknown (eff_t *unknown)
876 VERBOSE_PRINT ((stdout, "free unknown node \t0x%08x\n", (int) unknown));
881 void freeReturn (eff_t *ret)
883 VERBOSE_PRINT ((stdout, "free ret node \t0x%08x\n", (int) ret));
888 void freeRaise (eff_t *raise)
890 VERBOSE_PRINT ((stdout, "free raise node \t0x%08x\n", (int) raise));
896 void freeProcEffs(proc_t *proc)
901 VERBOSE_PRINT ((stdout, "free effect for method \"%s\"\n",
902 get_id_str(proc -> proc_ident)));
904 num = proc -> n_effs;
905 for(i = 0; i < num; i++) {
906 switch(proc -> effs[i] -> kind) {
908 freeArg(proc -> effs[i]);
911 freeValref(proc -> effs[i]);
914 freeSelect(proc -> effs[i]);
917 freeLoad(proc -> effs[i]);
920 freeStore(proc -> effs[i]);
923 freeAlloc(proc -> effs[i]);
926 freeCall(proc -> effs[i]);
929 freeUnknown(proc -> effs[i]);
932 freeJoin(proc -> effs[i]);
935 freeRaise(proc -> effs[i]);
938 freeReturn(proc -> effs[i]);
941 assert(0 && "try to free an unknown effect");
950 void freeModuleProcs(module_t *module)
952 proc_t *next_proc, *proc;
954 VERBOSE_PRINT ((stdout, "free procs for module \"%s\"\n",
955 get_id_str(module -> id)));
957 proc = module -> procs;
959 next_proc = proc -> next;
969 module_t *module, *next_module;
973 freeModuleProcs(module);
974 next_module = module -> next;
976 module = next_module;
980 /********************************************************************/
983 type_t *find_type_in_module(module_t *module, ident *typeid)
987 for(type = module -> types; type; type = type -> prev) {
988 VERBOSE_PRINT((stdout, "test typeid %s\n", get_id_str(type -> id)));
989 if(type -> id == typeid) {
990 VERBOSE_PRINT((stdout, "found\n"));
994 VERBOSE_PRINT((stdout, "did not find type id %s\n", get_id_str(typeid)));
998 /********************************************************************/
1000 static void add_value_to_proc(proc_t *proc, eff_t *eff)
1002 eff -> next = proc -> values;
1003 proc -> values = eff;
1007 eff_t *find_valueid_in_proc_effects(ident *id, proc_t *proc)
1011 val = proc -> values;
1013 if(id == val -> id) {
1021 static void create_abstract_return(ir_graph *irg, proc_t *proc, eff_t *eff)
1026 VERBOSE_PRINT((stdout, "create effect:return in %s\n",
1027 get_id_str(proc -> proc_ident)));
1028 if(NO_ID == eff -> effect.ret.ret_id) {
1030 x = new_Return (get_store(), 0, NULL);
1035 /* return one value */
1036 eff_res = find_valueid_in_proc_effects(eff -> effect.ret.ret_id, proc);
1037 assert(eff_res -> firmnode && "firm in effect not set");
1038 in[0] = eff_res -> firmnode;
1039 x = new_Return (get_store(), 1, in);
1041 eff -> firmnode = x;
1043 /* Now we generated all instructions for this block and all its predecessor
1044 * blocks so we can mature it. (There are not too much.) */
1045 mature_immBlock (get_irg_current_block(irg));
1047 /* This adds the in edge of the end block which originates at the return statement.
1048 * The return node passes controlflow to the end block. */
1049 add_immBlock_pred (get_irg_end_block(irg), x);
1053 static void create_abstract_arg(ir_graph *irg, proc_t *proc, eff_t *eff)
1061 VERBOSE_PRINT((stdout, "create effect:arg %d in %s\n",
1062 eff -> effect.arg.num, get_id_str(proc -> proc_ident)));
1063 ent = get_irg_entity(irg);
1064 typ = get_entity_type(ent);
1066 /* read argument eff -> effect.arg.num and place in values list */
1067 num = get_method_n_params(typ);
1068 assert((num >= eff -> effect.arg.num) && "number too big");
1069 typ = get_method_param_type(typ, eff -> effect.arg.num);
1070 mode = get_type_mode(typ);
1072 arg = new_Proj(get_irg_args(irg), mode, eff -> effect.arg.num);
1073 eff -> firmnode = arg;
1075 add_value_to_proc(proc, eff);
1079 static void create_abstract_load(ir_graph *irg, proc_t *proc, eff_t *eff)
1081 ir_node *sel, *load;
1087 VERBOSE_PRINT((stdout, "create load in %s\n",
1088 get_id_str(proc -> proc_ident)));
1090 if(eff -> effect.load.ent) {
1091 ent = eff -> effect.load.ent -> f_ent;
1092 VERBOSE_PRINT((stdout, "load from %s\n", get_entity_name(ent)));
1095 VERBOSE_PRINT((stdout, "store to memory\n"));
1099 addr = find_valueid_in_proc_effects(eff -> effect.load.ptrrefid, proc);
1100 assert(addr && "no address for load");
1101 /* if addr is Unknown, set proper mode */
1102 if(iro_Unknown == get_irn_opcode(addr -> firmnode)) {
1103 set_irn_mode(addr -> firmnode, mode_P);
1107 sel = new_simpleSel(get_store(), addr -> firmnode, ent);
1108 mode = get_type_mode(get_entity_type(ent));
1111 sel = addr -> firmnode;
1114 load = new_Load(get_store(), sel, mode);
1115 set_store(new_Proj(load, mode_M, 0));
1116 eff -> firmnode = new_Proj(load, mode, 2);
1118 add_value_to_proc(proc, eff);
1122 static void create_abstract_store(ir_graph *irg, proc_t *proc, eff_t *eff)
1124 ir_node *sel, *store;
1129 VERBOSE_PRINT((stdout, "create store in %s\n",
1130 get_id_str(proc -> proc_ident)));
1132 if(eff -> effect.store.ent) {
1133 ent = eff -> effect.store.ent -> f_ent;
1134 VERBOSE_PRINT((stdout, "store to ir_entity %s\n", get_entity_name(ent)));
1137 VERBOSE_PRINT((stdout, "store to memory\n"));
1141 addr = find_valueid_in_proc_effects(eff -> effect.store.ptrrefid, proc);
1142 assert(addr && "no address for store");
1143 /* if addr is Unknown, set propper mode */
1144 if(iro_Unknown == get_irn_opcode(addr -> firmnode)) {
1145 set_irn_mode(addr -> firmnode, mode_P);
1148 val = find_valueid_in_proc_effects(eff -> effect.store.valrefid, proc);
1149 assert(val && "no address for store");
1150 /* if addr is Unknown, set propper mode */
1151 if(iro_Unknown == get_irn_opcode(val -> firmnode)) {
1152 set_irn_mode(val -> firmnode, get_type_mode(get_entity_type(ent)));
1156 sel = new_simpleSel(get_store(), addr -> firmnode, ent);
1159 sel = addr -> firmnode;
1161 store = new_Store(get_store(), sel, val -> firmnode);
1162 set_store(new_Proj(store, mode_M, 0));
1163 eff -> firmnode = store;
1167 static void create_abstract_alloc(ir_graph *irg, proc_t *proc, eff_t *eff)
1172 symconst_symbol sym;
1175 VERBOSE_PRINT((stdout, "create alloc in %s\n",
1176 get_id_str(proc -> proc_ident)));
1178 xtype = find_type_in_module(current_module, eff -> effect.alloc.tp_id);
1179 assert(xtype && "type not found");
1180 ftype = xtype -> f_tp;
1183 alloc = new_Alloc(get_store(), new_SymConst(mode_Is, sym, symconst_type_size), ftype,
1185 set_store(new_Proj(alloc, mode_M, 0));
1186 eff -> firmnode = new_Proj(alloc, mode_P, 2);
1188 add_value_to_proc(proc, eff);
1192 static void create_abstract_unknown(ir_graph *irg, proc_t *proc, eff_t *eff)
1197 VERBOSE_PRINT((stdout, "create unknown in %s\n",
1198 get_id_str(proc -> proc_ident)));
1200 unknown = new_Unknown(mode_ANY);
1201 eff -> firmnode = unknown;
1203 add_value_to_proc(proc, eff);
1207 static void create_abstract_call(ir_graph *irg, proc_t *proc, eff_t *eff)
1209 ir_node *sel, *call;
1215 int mik; /* is method somehow known? */
1218 VERBOSE_PRINT((stdout, "create call in %s\n",
1219 get_id_str(proc -> proc_ident)));
1221 if(eff -> effect.call.ent) {
1222 ent = eff -> effect.call.ent -> f_ent;
1223 VERBOSE_PRINT((stdout, "call %s\n", get_entity_name(ent)));
1227 VERBOSE_PRINT((stdout, "call something in memory\n"));
1230 addr = find_valueid_in_proc_effects(eff -> effect.call.valrefid, proc);
1231 assert(addr && "no address for load");
1232 /* if addr is Unknown, set proper mode */
1233 if(iro_Unknown == get_irn_opcode(addr -> firmnode)) {
1234 set_irn_mode(addr -> firmnode, mode_P);
1239 sel = new_simpleSel(get_store(), addr -> firmnode, ent);
1241 mtype = get_entity_type(ent);
1246 sel = addr -> firmnode;
1248 mtype = get_unknown_type();
1253 num = eff -> effect.call.n_args;
1254 VERBOSE_PRINT((stdout, "number of args given: %d\n", num));
1256 VERBOSE_PRINT((stdout, "number of args expected: %d\n",
1257 get_method_n_params(mtype)));
1259 irns = alloca(num * sizeof(ir_node*));
1260 for(i = 0; i < num; i++) {
1261 irns[i] = find_valueid_in_proc_effects(eff -> effect.call.args[i], proc)
1263 if(iro_Unknown == get_irn_opcode(irns[i])) {
1265 set_irn_mode(irns[i], get_type_mode(get_method_param_type(mtype, i)));
1268 set_irn_mode(irns[i], mode_ANY);
1272 call = new_Call(get_store(), sel, num, irns, mtype);
1273 set_store(new_Proj(call, mode_M, 0));
1274 if(mik && (0 != get_method_n_ress(mtype))) {
1275 eff -> firmnode = new_Proj(call,
1276 get_type_mode(get_method_res_type(mtype, 0)),
1278 add_value_to_proc(proc, eff); /* result can be accessed */
1281 eff -> firmnode = NULL; /* result can not be accessed */
1285 static void create_abstract_join (ir_graph *irg, proc_t *proc, eff_t *eff)
1287 ir_node **ins = NULL;
1288 ir_node *unknown = NULL;
1289 ir_node *cond = NULL;
1290 ir_node *block = NULL;
1291 ir_node *c_block = NULL;
1292 ir_node *phi = NULL;
1293 ir_mode *join_md = mode_ANY;
1298 VERBOSE_PRINT((stdout, "create join in %s\n",
1299 get_id_str(proc -> proc_ident)));
1301 assert (eff_join == eff->kind);
1303 n_ins = eff->effect.join.n_ins;
1305 /* seems like current_block is not always mature at this point */
1306 mature_immBlock (get_cur_block ());
1308 block = get_cur_block (); /* remember this so we can put the ProjXs into it */
1310 /* jump based on an unknown condition so all values are possible */
1311 unknown = new_Unknown (mode_Iu);
1312 cond = new_Cond (unknown);
1314 c_block = new_immBlock (); /* for the Phi after the branch(es) */
1316 ins = (ir_node**) xmalloc (n_ins * sizeof (ir_node*));
1317 for (i = 0; i < n_ins; i ++) {
1318 ir_node *projX = NULL;
1319 ir_node *s_block = NULL;
1320 ir_node *jmp = NULL;
1323 /* make sure the projX is in the 'switch' block */
1324 set_cur_block (block);
1325 projX = new_Proj (cond, mode_X, (long) i);
1327 /* this also sets current_block, so the rest of the code ends up there: */
1328 s_block = new_immBlock ();
1330 add_immBlock_pred (s_block, projX);
1331 mature_immBlock (s_block);
1333 in_eff = find_valueid_in_proc_effects (eff->effect.join.ins [i], proc);
1335 ins [i] = in_eff->firmnode;
1337 /* need to find a suitable mode for the Phi node */
1338 if (mode_ANY != get_irn_mode (ins [i])) {
1339 join_md = get_irn_mode (ins [i]);
1343 add_immBlock_pred (c_block, jmp);
1346 set_cur_block (c_block);
1348 phi = new_Phi (n_ins, ins, join_md);
1350 mature_immBlock (c_block);
1351 memset (ins, 0x00, n_ins * sizeof (ir_node*));
1354 eff->firmnode = phi;
1356 add_value_to_proc (proc, eff);
1359 static void create_abstract_raise (ir_graph *irg, proc_t *proc, eff_t *eff)
1361 ir_node *block = NULL;
1362 ir_node *unknown = NULL;
1363 ir_node *cond = NULL;
1365 /* seems like current_block is not always mature at this point */
1366 mature_immBlock (get_cur_block ());
1367 block = get_cur_block (); /* remember this so we can put the ProjXs into it */
1369 /* jump based on an unknown condition so both values are possible */
1370 unknown = new_Unknown (mode_Iu);
1371 cond = new_Cond (unknown);
1373 /* one branch for 'throw-exception' case */
1375 ir_node *projX = new_Proj (cond, mode_X, 1L);
1376 ir_node *b_exc = new_immBlock ();
1377 ir_node *obj = NULL;
1378 ir_node *thrw = NULL;
1379 eff_t *thrw_eff = NULL;
1381 add_immBlock_pred (b_exc, projX);
1383 thrw_eff = find_valueid_in_proc_effects (eff->effect.raise.valref, proc);
1384 obj = thrw_eff->firmnode;
1386 thrw = new_Raise (get_store (), obj);
1387 /* exc-jump to end block */
1388 thrw = new_Proj (thrw, mode_X, 0L);
1390 add_immBlock_pred (get_irg_end_block (irg), thrw);
1391 mature_immBlock (get_cur_block ());
1394 set_cur_block (block); /* back to the first block */
1396 /* one branch for 'non-exception' case */
1398 ir_node *projX = new_Proj (cond, mode_X, 0);
1399 new_immBlock (); /* also sets current_block */
1400 add_immBlock_pred (get_cur_block (), projX);
1401 mature_immBlock (get_cur_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