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
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)
300 CHECK_NAME (argelm, arg);
301 VERBOSE_PRINT ((stdout, "arg node \t0x%08x\n", (int) argelm));
303 id = getNodeId (argelm);
304 VERBOSE_PRINT ((stdout, "arg->id = \"%s\"\n", id));
305 num_str = (char*) xmlGetProp (argelm, BAD_CAST "number");
306 num = atoi (num_str);
307 VERBOSE_PRINT ((stdout, "arg->no = \"%d\"\n", num));
309 typeid = getNodeTypeStr (argelm);
312 arg -> kind = eff_arg;
313 arg -> id = new_id_from_str(id);
314 arg -> effect.arg.num = num;
315 arg -> effect.arg.type_ident = new_id_from_str(typeid);
321 parseValref (xmlDocPtr doc, xmlNodePtr valelm)
326 CHECK_NAME (valelm, valref);
327 VERBOSE_PRINT ((stdout, "valref node \t0x%08x\n", (int) valelm));
329 ref_id = getNodeRefId (valelm);
330 VERBOSE_PRINT ((stdout, "val->refid = \"%s\"\n", ref_id));
332 valref = NEW (eff_t);
333 valref->kind = eff_valref;
334 valref-> id = new_id_from_str(ref_id);
340 parseSelect (xmlDocPtr doc, xmlNodePtr selelm)
342 ident *entity_id = new_id_from_str(getNodeEntityStr (selelm));
345 eff_t *valref = NULL;
346 eff_t *sel = NEW (eff_t);
347 sel->kind = eff_select;
349 CHECK_NAME (selelm, select);
350 VERBOSE_PRINT ((stdout, "select node \t0x%08x\n", (int) selelm));
352 ent = getEntityById (entity_id);
353 assert(ent && "ir_entity not found");
354 VERBOSE_PRINT ((stdout, "select ir_entity %s\n", get_id_str(ent -> ent_ident)));
356 child = selelm->xmlChildrenNode;
359 valref = parseValref (doc, child);
362 sel-> id = valref ? valref-> id : NO_ID;
363 sel-> effect.select.ent = ent;
373 parseLoad (xmlDocPtr doc, xmlNodePtr loadelm)
378 eff_t *load = NEW (eff_t);
379 load->kind = eff_load;
381 CHECK_NAME (loadelm, load);
382 VERBOSE_PRINT ((stdout, "load node \t0x%08x\n", (int) loadelm));
383 id = new_id_from_str(getNodeId (loadelm));
385 child = get_valid_child(loadelm);
386 if(NODE_NAME (child, select)) {
387 sel = parseSelect (doc, child);
388 load-> effect.load.ent = sel-> effect.select.ent;
389 VERBOSE_PRINT ((stdout, "load ir_entity \t%s\n",
390 get_id_str(load -> effect.load.ent -> ent_ident)));
393 sel = parseValref (doc, child);
394 load-> effect.load.ent = NULL;
398 load-> effect.load.ptrrefid = sel-> id;
406 parseStore (xmlDocPtr doc, xmlNodePtr storeelm)
411 eff_t *store = NEW (eff_t);
412 store->kind = eff_store;
414 CHECK_NAME (storeelm, store);
415 VERBOSE_PRINT ((stdout, "store node \t0x%08x\n", (int) storeelm));
417 child = get_valid_child(storeelm);
418 if(NODE_NAME (child, select)) {
419 sel = parseSelect (doc, child);
420 store-> effect.store.ent = sel-> effect.select.ent;
423 sel = parseValref (doc, child);
424 store-> effect.store.ent = NULL;
428 valref = parseValref (doc, child);
430 store-> effect.store.ptrrefid = sel-> id;
431 store-> effect.store.valrefid = valref-> id;
440 parseAlloc (xmlDocPtr doc, xmlNodePtr allocelm)
444 eff_t *alloc = NEW (eff_t); /* ...! */
445 alloc->kind = eff_alloc;
447 CHECK_NAME (allocelm, alloc);
448 VERBOSE_PRINT ((stdout, "alloc node \t0x%08x\n", (int) allocelm));
449 id = new_id_from_str(getNodeId (allocelm));
450 VERBOSE_PRINT ((stdout, "alloc->id = \"%s\"\n", get_id_str(id)));
451 type_id = new_id_from_str(getNodeTypeId (allocelm));
452 VERBOSE_PRINT ((stdout, "alloc->type_id = \"%s\"\n", get_id_str(type_id)));
455 alloc-> effect.alloc.tp_id = type_id;
461 parseCall (xmlDocPtr doc, xmlNodePtr callelm)
468 eff_t *call = NEW (eff_t);
469 call->kind = eff_call;
471 CHECK_NAME (callelm, call);
472 VERBOSE_PRINT ((stdout, "call node \t0x%08x\n", (int) callelm));
473 id = new_id_from_str(getNodeId (callelm));
474 VERBOSE_PRINT ((stdout, "call->id = \"%s\"\n", get_id_str(id)));
476 child = get_valid_child(callelm);
477 if(NODE_NAME (child, select)) {
478 sel = parseSelect (doc, child);
479 call-> effect.call.ent = sel-> effect.select.ent;
482 sel = parseValref (doc, child);
483 call-> effect.call.ent = NULL;
486 arg = child = child->next;
489 while (NULL != child) {
495 call-> effect.call.valrefid = sel-> id;
496 call-> effect.call.n_args = n_args;
497 call-> effect.call.args = NULL;
502 ident **args = (ident**) xmalloc(n_args * sizeof(ident*));
505 while (NULL != arg) {
506 eff_t *valref = parseValref (doc, arg);
507 args [i ++] = valref-> id;
512 call-> effect.call.args = args;
519 parseJoin (xmlDocPtr doc, xmlNodePtr joinelm)
526 eff_t *join = NEW (eff_t);
527 join->kind = eff_join;
529 CHECK_NAME (joinelm, join);
530 VERBOSE_PRINT ((stdout, "join node \t0x%08x\n", (int) joinelm));
531 id = new_id_from_str(getNodeId (joinelm));
532 VERBOSE_PRINT ((stdout, "join->id = \"%s\"\n", get_id_str(id)));
534 child = get_valid_child(joinelm);
537 while (NULL != child) {
542 ins = (ident **) xmalloc (n_ins * sizeof (ident *) );
544 child = get_valid_child(joinelm);
546 while (NULL != child) {
547 eff_t *valref = parseValref (doc, child);
548 ins [i ++] = valref-> id;
554 join-> effect.join.n_ins = n_ins;
555 join-> effect.join.ins = ins;
561 parseUnknown (xmlDocPtr doc, xmlNodePtr unknownelm)
564 eff_t *unknown = NEW (eff_t);
565 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 = NEW (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 = NEW (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)));
637 type = (type_t*) xmalloc (sizeof (type_t));
638 type -> type_ident = new_id_from_str(getNodeTypeStr (typeelm));
639 type -> id = new_id_from_str(tp_id);
645 /** parse an ir_entity node and insert it into the list */
647 parseEntity (xmlDocPtr doc, xmlNodePtr entelm)
649 entity_t *ent = NEW (entity_t);
652 const char *ent_id = getNodeId (entelm);
653 /* fprintf (stdout, "ir_entity node \t0x%08x (%d)\n", (int) entelm, ent_id); */
654 VERBOSE_PRINT ((stdout, "ent = \"%s.%s\"\n",
655 getNodeTypeStr (entelm),
656 getNodeEntityStr (entelm)));
659 ent -> ent_ident = new_id_from_str (getNodeEntityStr (entelm));
660 ent -> tp_ident = new_id_from_str (getNodeTypeStr (entelm));
661 ent -> owner = new_id_from_str (getNodeOwnerStr (entelm));
662 ent -> id = new_id_from_str(ent_id);
664 ent->prev = entities;
668 /** parse any effect, and turn it into an eff_t (TODO) */
670 parseEffect (xmlDocPtr doc, xmlNodePtr effelm)
673 const char *procname = getNodeProcName (effelm);
674 const char *ownerid = getNodeOwnerStr (effelm);
675 proc_t *curr_effs = NULL;
679 VERBOSE_PRINT ((stdout, "effect for method \"%s\"\n", procname));
681 cur = effelm -> xmlChildrenNode;
682 while (NULL != cur) {
686 VERBOSE_PRINT ((stdout, "has %d effects\n", n_effs));
688 curr_effs = NEW (proc_t);
689 curr_effs -> proc_ident = new_id_from_str(procname);
690 curr_effs -> ownerid = new_id_from_str(ownerid);
691 curr_effs->effs = (eff_t**) xmalloc (n_effs * sizeof (eff_t*));
693 cur = effelm -> xmlChildrenNode;
694 while (NULL != cur) {
697 if (NODE_NAME (cur, arg)) {
698 eff = (eff_t*) parseArg (doc, cur);
699 } else if (NODE_NAME (cur, load)) {
700 eff = (eff_t*) parseLoad (doc, cur);
701 } else if (NODE_NAME (cur, store)) {
702 eff = (eff_t*) parseStore (doc, cur);
703 } else if (NODE_NAME (cur, alloc)) {
704 eff = (eff_t*) parseAlloc (doc, cur);
705 } else if (NODE_NAME (cur, call)) {
706 eff = (eff_t*) parseCall (doc, cur);
707 } else if (NODE_NAME (cur, join)) {
708 eff = (eff_t*) parseJoin (doc, cur);
709 } else if (NODE_NAME (cur, unknown)) {
710 eff = (eff_t*) parseUnknown (doc, cur);
711 } else if (NODE_NAME (cur, ret)) {
712 eff = (eff_t*) parseReturn (doc, cur);
713 } else if (NODE_NAME (cur, raise)) {
714 eff = (eff_t*) parseRaise (doc, cur);
715 } else if (NODE_NAME (cur, comment)) {
719 fprintf (stderr, "wrong element \"%s\"\n", BAD_CAST cur->name);
723 VERBOSE_PRINT ((stdout, "effect %p@%d\n", (void*)eff, i));
724 curr_effs -> effs[i++] = eff;
728 assert((i == n_effs) && "incorrect number of effects");
729 curr_effs -> n_effs = n_effs;
730 curr_effs -> next = procs;
736 int read_extern (const char *filename)
738 /* xmlNsPtr ns = NULL; */ /* no namespace for us */
739 xmlDocPtr doc; /* whole document */
740 xmlNodePtr cur; /* current node */
744 /* i've got no idea what the VERSION cast is all about. voodoo
745 programming at its finest. */
746 LIBXML_TEST_VERSION xmlKeepBlanksDefault (0);
747 VERBOSE_PRINT((stdout, "read file %s\n", filename));
748 doc = xmlParseFile (filename);
752 cur = xmlDocGetRootElement (doc);
753 CHECK (cur, "xmlDocGetRootElement");
755 if (! NODE_NAME (cur, effects)) {
756 fprintf (stderr,"root node \"%s\" != \"effects\"\n", BAD_CAST cur->name);
761 mod_id = getNodeModuleIdent (cur);
762 if (NULL != mod_id) {
763 VERBOSE_PRINT ((stdout, "effects for \"%s\"\n",
764 get_id_str(mod_id)));
767 VERBOSE_PRINT ((stdout, "effects \t0x%08x\n", (int) cur));
771 cur = cur->xmlChildrenNode;
772 while (cur != NULL) {
773 if (NODE_NAME (cur, type)) {
774 parseType (doc, cur);
775 } else if (NODE_NAME (cur, ir_entity)) {
776 parseEntity (doc, cur);
777 } else if (NODE_NAME (cur, effect)) {
778 parseEffect (doc, cur);
779 } else if ((NODE_NAME (cur, comment))) {
782 fprintf (stderr, "wrong element \"%s\"\n", BAD_CAST cur->name);
788 module = NEW(module_t);
789 module -> id = mod_id;
790 module -> types = types;
791 module -> entities = entities;
792 module -> procs = procs;
798 module -> next = modules;
804 /********************************************************************/
807 * free additional structure
810 void freeArg (eff_t *arg)
812 VERBOSE_PRINT ((stdout, "free arg node \t0x%08x\n", (int) arg));
818 void freeValref (eff_t *valref)
820 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));
834 void freeLoad (eff_t *load)
836 VERBOSE_PRINT ((stdout, "free load node \t0x%08x\n", (int) load));
842 void freeStore (eff_t *store)
844 VERBOSE_PRINT ((stdout, "free store node \t0x%08x\n", (int) store));
850 void freeAlloc (eff_t *alloc)
852 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);
867 void freeJoin (eff_t *join)
869 VERBOSE_PRINT ((stdout, "free join node \t0x%08x\n", (int) join));
870 free(join -> effect.join.ins);
876 void freeUnknown (eff_t *unknown)
878 VERBOSE_PRINT ((stdout, "free unknown node \t0x%08x\n", (int) unknown));
884 void freeReturn (eff_t *ret)
886 VERBOSE_PRINT ((stdout, "free ret node \t0x%08x\n", (int) ret));
892 void freeRaise (eff_t *raise)
894 VERBOSE_PRINT ((stdout, "free raise node \t0x%08x\n", (int) raise));
901 void freeProcEffs(proc_t *proc)
906 VERBOSE_PRINT ((stdout, "free effect for method \"%s\"\n",
907 get_id_str(proc -> proc_ident)));
909 num = proc -> n_effs;
910 for(i = 0; i < num; i++) {
911 switch(proc -> effs[i] -> kind) {
913 freeArg(proc -> effs[i]);
916 freeValref(proc -> effs[i]);
919 freeSelect(proc -> effs[i]);
922 freeLoad(proc -> effs[i]);
925 freeStore(proc -> effs[i]);
928 freeAlloc(proc -> effs[i]);
931 freeCall(proc -> effs[i]);
934 freeUnknown(proc -> effs[i]);
937 freeJoin(proc -> effs[i]);
940 freeRaise(proc -> effs[i]);
943 freeReturn(proc -> effs[i]);
946 assert(0 && "try to free an unknown effect");
955 void freeModuleProcs(module_t *module)
957 proc_t *next_proc, *proc;
959 VERBOSE_PRINT ((stdout, "free procs for module \"%s\"\n",
960 get_id_str(module -> id)));
962 proc = module -> procs;
964 next_proc = proc -> next;
974 module_t *module, *next_module;
978 freeModuleProcs(module);
979 next_module = module -> next;
981 module = next_module;
985 /********************************************************************/
988 type_t *find_type_in_module(module_t *module, ident *typeid)
992 for(type = module -> types; type; type = type -> prev) {
993 VERBOSE_PRINT((stdout, "test typeid %s\n", get_id_str(type -> id)));
994 if(type -> id == typeid) {
995 VERBOSE_PRINT((stdout, "found\n"));
999 VERBOSE_PRINT((stdout, "did not find type id %s\n", get_id_str(typeid)));
1003 /********************************************************************/
1005 static void add_value_to_proc(proc_t *proc, eff_t *eff)
1007 eff -> next = proc -> values;
1008 proc -> values = eff;
1012 eff_t *find_valueid_in_proc_effects(ident *id, proc_t *proc)
1016 val = proc -> values;
1018 if(id == val -> id) {
1026 static void create_abstract_return(ir_graph *irg, proc_t *proc, eff_t *eff)
1031 VERBOSE_PRINT((stdout, "create effect:return in %s\n",
1032 get_id_str(proc -> proc_ident)));
1033 if(NO_ID == eff -> effect.ret.ret_id) {
1035 x = new_Return (get_store(), 0, NULL);
1040 /* return one value */
1041 eff_res = find_valueid_in_proc_effects(eff -> effect.ret.ret_id, proc);
1042 assert(eff_res -> firmnode && "firm in effect not set");
1043 in[0] = eff_res -> firmnode;
1044 x = new_Return (get_store(), 1, in);
1046 eff -> firmnode = x;
1048 /* Now we generated all instructions for this block and all its predecessor
1049 * blocks so we can mature it. (There are not too much.) */
1050 mature_immBlock (get_irg_current_block(irg));
1052 /* This adds the in edge of the end block which originates at the return statement.
1053 * The return node passes controlflow to the end block. */
1054 add_immBlock_pred (get_irg_end_block(irg), x);
1058 static void create_abstract_arg(ir_graph *irg, proc_t *proc, eff_t *eff)
1066 VERBOSE_PRINT((stdout, "create effect:arg %d in %s\n",
1067 eff -> effect.arg.num, get_id_str(proc -> proc_ident)));
1068 ent = get_irg_entity(irg);
1069 typ = get_entity_type(ent);
1071 /* read argument eff -> effect.arg.num and place in values list */
1072 num = get_method_n_params(typ);
1073 assert((num >= eff -> effect.arg.num) && "number too big");
1074 typ = get_method_param_type(typ, eff -> effect.arg.num);
1075 mode = get_type_mode(typ);
1077 arg = new_Proj(get_irg_args(irg), mode, eff -> effect.arg.num);
1078 eff -> firmnode = arg;
1080 add_value_to_proc(proc, eff);
1084 static void create_abstract_load(ir_graph *irg, proc_t *proc, eff_t *eff)
1086 ir_node *sel, *load;
1091 VERBOSE_PRINT((stdout, "create load in %s\n",
1092 get_id_str(proc -> proc_ident)));
1094 if(eff -> effect.load.ent) {
1095 ent = eff -> effect.load.ent -> f_ent;
1096 VERBOSE_PRINT((stdout, "load from %s\n", get_entity_name(ent)));
1099 VERBOSE_PRINT((stdout, "store to memory\n"));
1103 addr = find_valueid_in_proc_effects(eff -> effect.load.ptrrefid, proc);
1104 assert(addr && "no address for load");
1105 /* if addr is Unknown, set proper mode */
1106 if(iro_Unknown == get_irn_opcode(addr -> firmnode)) {
1107 set_irn_mode(addr -> firmnode, mode_P);
1111 sel = new_simpleSel(get_store(), addr -> firmnode, ent);
1112 mode = get_type_mode(get_entity_type(ent));
1115 sel = addr -> firmnode;
1118 load = new_Load(get_store(), sel, mode);
1119 set_store(new_Proj(load, mode_M, 0));
1120 eff -> firmnode = new_Proj(load, mode, 2);
1122 add_value_to_proc(proc, eff);
1126 static void create_abstract_store(ir_graph *irg, proc_t *proc, eff_t *eff)
1128 ir_node *sel, *store;
1132 VERBOSE_PRINT((stdout, "create store in %s\n",
1133 get_id_str(proc -> proc_ident)));
1135 if(eff -> effect.store.ent) {
1136 ent = eff -> effect.store.ent -> f_ent;
1137 VERBOSE_PRINT((stdout, "store to ir_entity %s\n", get_entity_name(ent)));
1140 VERBOSE_PRINT((stdout, "store to memory\n"));
1144 addr = find_valueid_in_proc_effects(eff -> effect.store.ptrrefid, proc);
1145 assert(addr && "no address for store");
1146 /* if addr is Unknown, set propper mode */
1147 if(iro_Unknown == get_irn_opcode(addr -> firmnode)) {
1148 set_irn_mode(addr -> firmnode, mode_P);
1151 val = find_valueid_in_proc_effects(eff -> effect.store.valrefid, proc);
1152 assert(val && "no address for store");
1153 /* if addr is Unknown, set propper mode */
1154 if(iro_Unknown == get_irn_opcode(val -> firmnode)) {
1155 set_irn_mode(val -> firmnode, get_type_mode(get_entity_type(ent)));
1159 sel = new_simpleSel(get_store(), addr -> firmnode, ent);
1162 sel = addr -> firmnode;
1164 store = new_Store(get_store(), sel, val -> firmnode);
1165 set_store(new_Proj(store, mode_M, 0));
1166 eff -> firmnode = store;
1170 static void create_abstract_alloc(ir_graph *irg, proc_t *proc, eff_t *eff)
1175 symconst_symbol sym;
1177 VERBOSE_PRINT((stdout, "create alloc in %s\n",
1178 get_id_str(proc -> proc_ident)));
1180 xtype = find_type_in_module(current_module, eff -> effect.alloc.tp_id);
1181 assert(xtype && "type not found");
1182 ftype = xtype -> f_tp;
1185 alloc = new_Alloc(get_store(), new_SymConst(sym, symconst_type_size), ftype,
1187 set_store(new_Proj(alloc, mode_M, 0));
1188 eff -> firmnode = new_Proj(alloc, mode_P, 2);
1190 add_value_to_proc(proc, eff);
1194 static void create_abstract_unknown(ir_graph *irg, proc_t *proc, eff_t *eff)
1198 VERBOSE_PRINT((stdout, "create unknown in %s\n",
1199 get_id_str(proc -> proc_ident)));
1201 unknown = new_Unknown(mode_ANY);
1202 eff -> firmnode = unknown;
1204 add_value_to_proc(proc, eff);
1208 static void create_abstract_call(ir_graph *irg, proc_t *proc, eff_t *eff)
1210 ir_node *sel, *call;
1216 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;
1297 VERBOSE_PRINT((stdout, "create join in %s\n",
1298 get_id_str(proc -> proc_ident)));
1300 assert (eff_join == eff->kind);
1302 n_ins = eff->effect.join.n_ins;
1304 /* seems like current_block is not always mature at this point */
1305 mature_immBlock (get_cur_block ());
1307 block = get_cur_block (); /* remember this so we can put the ProjXs into it */
1309 /* jump based on an unknown condition so all values are possible */
1310 unknown = new_Unknown (mode_Iu);
1311 cond = new_Cond (unknown);
1313 c_block = new_immBlock (); /* for the Phi after the branch(es) */
1315 ins = (ir_node**) xmalloc (n_ins * sizeof (ir_node*));
1316 for (i = 0; i < n_ins; i ++) {
1317 ir_node *projX = NULL;
1318 ir_node *s_block = NULL;
1319 ir_node *jmp = NULL;
1322 /* make sure the projX is in the 'switch' block */
1323 set_cur_block (block);
1324 projX = new_Proj (cond, mode_X, (long) i);
1326 /* this also sets current_block, so the rest of the code ends up there: */
1327 s_block = new_immBlock ();
1329 add_immBlock_pred (s_block, projX);
1330 mature_immBlock (s_block);
1332 in_eff = find_valueid_in_proc_effects (eff->effect.join.ins [i], proc);
1334 ins [i] = in_eff->firmnode;
1336 /* need to find a suitable mode for the Phi node */
1337 if (mode_ANY != get_irn_mode (ins [i])) {
1338 join_md = get_irn_mode (ins [i]);
1342 add_immBlock_pred (c_block, jmp);
1345 set_cur_block (c_block);
1347 phi = new_Phi (n_ins, ins, join_md);
1349 mature_immBlock (c_block);
1350 memset (ins, 0x00, n_ins * sizeof (ir_node*));
1353 eff->firmnode = phi;
1355 add_value_to_proc (proc, eff);
1358 static void create_abstract_raise (ir_graph *irg, proc_t *proc, eff_t *eff)
1360 ir_node *block = NULL;
1361 ir_node *unknown = NULL;
1362 ir_node *cond = NULL;
1364 /* seems like current_block is not always mature at this point */
1365 mature_immBlock (get_cur_block ());
1366 block = get_cur_block (); /* remember this so we can put the ProjXs into it */
1368 /* jump based on an unknown condition so both values are possible */
1369 unknown = new_Unknown (mode_Iu);
1370 cond = new_Cond (unknown);
1372 /* one branch for 'throw-exception' case */
1374 ir_node *projX = new_Proj (cond, mode_X, 1L);
1375 ir_node *b_exc = new_immBlock ();
1376 ir_node *obj = NULL;
1377 ir_node *thrw = NULL;
1378 eff_t *thrw_eff = NULL;
1380 add_immBlock_pred (b_exc, projX);
1382 thrw_eff = find_valueid_in_proc_effects (eff->effect.raise.valref, proc);
1383 obj = thrw_eff->firmnode;
1385 thrw = new_Raise (get_store (), obj);
1386 /* exc-jump to end block */
1387 thrw = new_Proj (thrw, mode_X, 0L);
1389 add_immBlock_pred (get_irg_end_block (irg), thrw);
1390 mature_immBlock (get_cur_block ());
1393 set_cur_block (block); /* back to the first block */
1395 /* one branch for 'non-exception' case */
1397 ir_node *projX = new_Proj (cond, mode_X, 0);
1398 new_immBlock (); /* also sets current_block */
1399 add_immBlock_pred (get_cur_block (), projX);
1400 mature_immBlock (get_cur_block ());
1401 /* continue building in current_block */
1406 static void create_abstract_firm(module_t *module, proc_t *proc, ir_entity *fent)
1412 /* test ir_entity */
1413 assert(visibility_external_allocated == get_entity_visibility(fent)
1414 && peculiarity_existent == get_entity_peculiarity(fent)
1415 && "not an abstract ir_entity");
1416 /* create irg in ir_entity */
1417 irg = new_pseudo_ir_graph(fent, 0);
1418 set_irg_inline_property(irg, irg_inline_forbidden);
1420 /* @@@ If the spec says so: */
1421 set_entity_visibility(fent, visibility_local);
1423 VERBOSE_PRINT((stdout, "create effects for %s\n",
1424 get_id_str(proc -> proc_ident)));
1426 /* create effects in irg */
1427 num = proc -> n_effs;
1428 for(i = 0; i < num; i++) {
1429 eff = proc -> effs[i];
1430 VERBOSE_PRINT((stdout,
1431 "create effect \"%s\"\n", effect_string[(int)eff -> kind]));
1432 switch(eff -> kind) {
1434 create_abstract_return(irg, proc, eff);
1437 create_abstract_arg(irg, proc, eff);
1440 create_abstract_load(irg, proc, eff);
1443 create_abstract_store(irg, proc, eff);
1446 create_abstract_unknown(irg, proc, eff);
1449 create_abstract_alloc(irg, proc, eff);
1452 create_abstract_call(irg, proc, eff);
1455 create_abstract_join(irg, proc, eff);
1458 create_abstract_raise(irg, proc, eff);
1461 assert(0 && "effect not implemented");
1466 /* close irg in ir_entity */
1467 /* Now we can mature the end block as all it's predecessors are known. */
1468 mature_immBlock (get_irg_end_block(irg));
1470 /* Verify the graph. Finds some very bad errors in the graph. */
1471 VERBOSE_PRINT((stdout, "verify graph\n"));
1473 VERBOSE_PRINT((stdout, "finalize construction\n"));
1474 irg_finalize_cons (irg);
1477 /********************************************************************/
1479 static void assign_firm_entity(module_t *module, entity_t *xmlent)
1486 VERBOSE_PRINT((stdout, "assign ir_entity %s to typeid %s\n",
1487 get_id_str(xmlent -> ent_ident),
1488 get_id_str(xmlent -> owner)));
1490 typ = find_type_in_module(module, xmlent -> owner);
1491 assert(typ && "class not found in module");
1493 assert(is_Class_type(type));
1495 num = get_class_n_members(type);
1497 for(i = 0; i < num; i++) {
1498 ent = get_class_member(type, i);
1499 VERBOSE_PRINT((stdout, "compare ir_entity %s and %s\n",
1500 get_id_str(xmlent -> ent_ident), get_entity_name(ent)));
1502 if(get_entity_ident(ent) == xmlent -> ent_ident) {
1507 assert(ent && "did not find a ir_entity");
1509 xmlent -> f_ent = ent;
1512 /********************************************************************/
1513 /* must be primitive type or class type */
1514 static void assign_firm_type(type_t *xmltype)
1517 ir_type *typ = NULL;
1520 VERBOSE_PRINT((stdout, "assign firm type to type %s\n",
1521 get_id_str(xmltype -> type_ident)));
1523 /* is it global type? */
1524 typ = get_glob_type();
1525 if(xmltype -> type_ident == get_type_ident(typ)) {
1527 xmltype -> f_tp = typ;
1528 VERBOSE_PRINT((stdout, "is global type %s\n", get_type_name(typ)));
1530 num = get_irp_n_types();
1531 for(i = 0; i < num; i++) {
1532 typ = get_irp_type(i);
1533 VERBOSE_PRINT((stdout, "test type %s\n", get_type_name(typ)));
1534 if(xmltype -> type_ident == get_type_ident(typ)) {
1535 VERBOSE_PRINT((stdout, "found type %s\n", get_type_name(typ)));
1536 xmltype -> f_tp = typ;
1542 assert(typ && "did not find a type");
1545 /********************************************************************/
1547 void create_abstract_proc_effect(module_t *module, proc_t *proc)
1550 ir_type *class_typ = NULL;
1554 /* find the class of a procedure */
1555 VERBOSE_PRINT((stdout, "do find owner id %s\n", get_id_str(proc -> ownerid)));
1556 type = find_type_in_module(module, proc -> ownerid);
1557 assert(type && "class not found in module");
1559 class_typ = get_glob_type();
1560 VERBOSE_PRINT((stdout, "test type %s\n", get_type_name(class_typ)));
1561 if(type -> type_ident != get_type_ident(class_typ)) {
1562 /* find module as class */
1563 num = get_irp_n_types();
1564 for(i = 0; i < num; i++) {
1565 class_typ = get_irp_type(i);
1566 VERBOSE_PRINT((stdout, "test type %s\n", get_type_name(class_typ)));
1567 if (is_Class_type(class_typ)
1568 && (type -> type_ident == get_type_ident(class_typ))) {
1569 /* found class type */
1570 VERBOSE_PRINT((stdout, "found type %s\n", get_type_name(class_typ)));
1577 VERBOSE_PRINT((stdout, "found global type %s\n", get_type_name(class_typ)));
1579 assert(class_typ && "type not found");
1580 assert(is_Class_type(class_typ) && "is not a class type");
1581 type -> f_tp = class_typ;
1583 /* find ir_entity for procedure in class */
1584 VERBOSE_PRINT((stdout, "find method %s\n",
1585 get_id_str(proc -> proc_ident)));
1587 num = get_class_n_members(class_typ);
1589 for(i = 0; i < num; i++) {
1590 fent = get_class_member(class_typ, i);
1591 VERBOSE_PRINT((stdout, "test proc %s\n", get_entity_name(fent)));
1592 if(proc -> proc_ident == get_entity_ident(fent)) {
1593 VERBOSE_PRINT((stdout, "found proc %s\n",
1594 get_id_str(proc -> proc_ident)));
1595 /* @@@ TODO check args types - not in xml yet */
1596 /* create Firm stuff */
1597 create_abstract_firm(module, proc, fent);
1607 "method %s not found\nNo effects generated\nCandidates are:\n",
1608 get_id_str(proc -> proc_ident));
1609 for(i = 0; i < num; i++) {
1610 fent = get_class_member(class_typ, i);
1611 fprintf(stderr, "%s\n", get_entity_name(fent));
1613 //assert(fent && "procedure not found in class");
1617 void create_abstract_module(module_t *module)
1623 VERBOSE_PRINT((stdout, "create an abstraction for module %s\n",
1624 get_id_str(module -> id)));
1626 VERBOSE_PRINT((stdout, "--handle types for module\n"));
1627 for(type = module -> types; type; type = type -> prev) {
1628 assign_firm_type(type);
1631 VERBOSE_PRINT((stdout, "--handle entities for module\n"));
1633 for(ent = module -> entities; ent; ent = ent -> prev) {
1634 assign_firm_entity(module, ent);
1637 VERBOSE_PRINT((stdout, "--handle procs for module\n"));
1638 for(proc = module -> procs; proc; proc = proc -> next) {
1639 create_abstract_proc_effect(module, proc);
1644 int create_abstraction(const char *filename)
1648 /* read and parse XML file */
1649 if (! read_extern(filename))
1652 /* finished reading and parsing here */
1653 /* build FIRM graphs */
1656 current_module = module;
1657 create_abstract_module(module);
1658 module = module -> next;
1660 current_module = NULL;
1662 /* free data structures */
1674 void free_abstraction(void) {
1675 int i, n_pseudo_irgs = get_irp_n_pseudo_irgs();
1676 for (i = 0; i < n_pseudo_irgs; ++i) {
1677 ir_graph *p_irg = get_irp_pseudo_irg(i);
1678 set_entity_visibility(get_irg_entity(p_irg), visibility_external_allocated);
1679 // @@@ free_pseudo_ir_graph(p_irg);
1684 /********************************************************************/
1689 * Revision 1.27 2007/02/02 12:38:35 matze
1690 * entity is ir_entity now
1692 * Revision 1.26 2006/12/15 12:37:40 matze
1695 * Revision 1.25 2006/06/09 11:26:35 firm
1696 * renamed type to ir_type
1698 * Revision 1.24 2006/05/29 13:34:49 beck
1699 * renamed symconst_size to symconst_type_size
1701 * Revision 1.22 2005/08/16 10:18:35 beck
1702 * create_abstraction() now returns an error code if the file could not
1705 * Revision 1.21 2005/03/10 10:05:38 goetz
1706 * chanmged method name
1708 * Revision 1.20 2005/01/05 14:28:35 beck
1709 * renames all is_x*_type() functions to is_X*_type() to prevent name clash with EDG frontend
1711 * Revision 1.19 2004/12/10 15:14:34 beck
1712 * used xmalloc instead of malloc
1714 * Revision 1.18 2004/12/02 16:21:42 beck
1715 * fixed config.h include
1717 * Revision 1.17 2004/11/23 14:17:31 liekweg
1718 * fenced out currently unneeded static functions
1720 * Revision 1.16 2004/11/11 12:24:52 goetz
1723 * Revision 1.15 2004/11/11 09:28:32 goetz
1724 * treat pseudo irgs special
1725 * parse 'local' from xml files
1727 * Revision 1.14 2004/11/10 14:42:00 boesler
1728 * be more helpful if a method does not exist
1730 * Revision 1.13 2004/11/05 14:00:53 liekweg
1733 * Revision 1.12 2004/11/02 14:30:31 liekweg
1734 * fixed multi-input join (thx, Boris) --flo
1736 * Revision 1.11 2004/10/29 18:51:53 liekweg
1739 * Revision 1.10 2004/10/25 13:52:24 boesler
1740 * seperated read.h (public interface) and read_t.h (types)
1742 * Revision 1.9 2004/10/22 13:51:35 boesler
1743 * prohibit inlining of pseudo ir_graphs
1745 * Revision 1.8 2004/10/22 13:13:27 boesler
1746 * replaced char* by idents, minor fix in Firm codegen for call
1748 * Revision 1.7 2004/10/21 15:31:55 boesler
1749 * added lots of stuff:
1750 * - build abstract syntax trees
1751 * - build Firm graphs for many effects, still todos
1753 * Revision 1.5 2004/10/18 12:48:20 liekweg
1756 * Revision 1.4 2004/10/14 11:31:53 liekweg
1759 * Revision 1.3 2004/10/13 13:36:28 rubino
1762 * Revision 1.2 2004/10/11 15:56:09 liekweg
1763 * Cleanup, comments ...
1764 * Added init func --flo
1766 * Revision 1.1 2004/10/11 09:31:06 liekweg
1767 * First Import of XML reading procs --flo