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
40 #include <libxml/xmlmemory.h>
41 #include <libxml/parser.h>
42 #include <libxml/encoding.h>
48 #include "pseudo_irg.h"
57 # define MY_ENCODING "ISO-8859-1"
59 # define CHECK(ptr,msg) assert (ptr && msg)
61 # define NODE_NAME(n, m) (0 == xmlStrcmp (n->name, (const xmlChar*) #m))
62 # define CHECK_NAME(n, m) assert (0 == xmlStrcmp (n->name, (const xmlChar*) #m))
64 # define NEW(T) (T*)xmalloc(sizeof (T))
67 #define VERBOSE_PRINTING 0
70 # define VERBOSE_PRINT(s) fprintf s
72 # define VERBOSE_PRINT(s)
77 static type_t *types = NULL;
78 static entity_t *entities = NULL;
79 static proc_t *procs = NULL;
80 static module_t *modules = NULL;
83 static module_t *current_module = NULL;
86 /* this is only used inside a VERBOSE_PRINT() call */
87 static const char *effect_string[] = {
100 #endif /* defined VERBOSE_PRINTING */
103 getNodeModuleIdent (xmlNodePtr node)
105 const char *mod_str = (const char*) xmlGetProp (node, BAD_CAST "module");
107 if (NULL == mod_str) {
110 ident *res = new_id_from_str (mod_str);
116 getNodeProcName (xmlNodePtr node)
118 const char *proc_str = (const char*) xmlGetProp (node, BAD_CAST "procname");
125 getNodeClassName (xmlNodePtr node)
127 char *proc_str = (char*) xmlGetProp (node, BAD_CAST "class");
129 return ( (proc_str));
131 # endif /* defined NEEDED */
134 getNodeId (xmlNodePtr node)
136 const char *id_str = (const char*) xmlGetProp (node, BAD_CAST "id");
142 getNodeRefId (xmlNodePtr node)
144 const char *refid_str = (char*) xmlGetProp (node, BAD_CAST "refid");
146 return ((refid_str));
150 getNodeTypeId (xmlNodePtr node)
152 const char *type_str = (char*) xmlGetProp (node, BAD_CAST "type");
158 *getNodeTypeStr (xmlNodePtr node)
160 const char *type_str = (char*) xmlGetProp (node, BAD_CAST "type");
166 getNodeOwnerStr (xmlNodePtr node)
168 const char *owner_str = (char*) xmlGetProp (node, BAD_CAST "owner");
174 *getNodeEntityStr (xmlNodePtr node)
176 const char *ent_str = (char*) xmlGetProp (node, BAD_CAST "ir_entity");
188 type_t *getTypeByIdent (ident *id)
190 type_t *curr = types; // @@@ TODO module -> types
192 while (NULL != curr) {
193 if (id == curr -> type_ident) {
201 # endif /* defined NEEDED */
205 type_t *getTypeById (ident *id)
207 type_t *curr = types; // which ones?
209 while (NULL != curr) {
210 if (id == curr -> id) {
218 # endif /* defined NEEDED */
222 entity_t *getEntityByIdents (ident *name, ident *tp_ident)
224 entity_t *curr = entities; // TODO module -> entities
226 while (NULL != curr) {
227 if ((name == curr -> ent_ident)
228 && (tp_ident == curr -> tp_ident)) {
236 # endif /* defined NEEDED */
239 entity_t *getEntityById (ident *id)
241 entity_t *curr = entities;
243 while (NULL != curr) {
244 if (id == curr -> id) {
255 proc_t *getEffectByName (ident *proc_ident)
257 proc_t *curr_effs = procs;
259 while (NULL != curr_effs) {
260 if (proc_ident == curr_effs -> proc_ident) {
263 curr_effs = curr_effs->next;
268 # endif /* defined NEEDED */
271 xmlNodePtr get_any_valid_child(xmlNodePtr elem)
275 assert(elem && "no element");
276 child = elem -> xmlChildrenNode;
277 while(child && (NODE_NAME (child, comment))) {
278 child = child -> next;
284 xmlNodePtr get_valid_child(xmlNodePtr elem)
288 child = get_any_valid_child(elem);
289 assert(child && "lost child in deep black forest");
294 * parse XML structure and construct an additional structure
297 parseArg (xmlDocPtr doc, xmlNodePtr argelm)
305 CHECK_NAME (argelm, arg);
306 VERBOSE_PRINT ((stdout, "arg node \t0x%08x\n", (int) argelm));
308 id = getNodeId (argelm);
309 VERBOSE_PRINT ((stdout, "arg->id = \"%s\"\n", id));
310 num_str = (char*) xmlGetProp (argelm, BAD_CAST "number");
311 num = atoi (num_str);
312 VERBOSE_PRINT ((stdout, "arg->no = \"%d\"\n", num));
314 typeid = getNodeTypeStr (argelm);
317 arg -> kind = eff_arg;
318 arg -> id = new_id_from_str(id);
319 arg -> effect.arg.num = num;
320 arg -> effect.arg.type_ident = new_id_from_str(typeid);
326 parseValref (xmlDocPtr doc, xmlNodePtr valelm)
331 CHECK_NAME (valelm, valref);
332 VERBOSE_PRINT ((stdout, "valref node \t0x%08x\n", (int) valelm));
334 ref_id = getNodeRefId (valelm);
335 VERBOSE_PRINT ((stdout, "val->refid = \"%s\"\n", ref_id));
337 valref = NEW (eff_t);
338 valref->kind = eff_valref;
339 valref-> id = new_id_from_str(ref_id);
345 parseSelect (xmlDocPtr doc, xmlNodePtr selelm)
347 ident *entity_id = new_id_from_str(getNodeEntityStr (selelm));
350 eff_t *valref = NULL;
351 eff_t *sel = NEW (eff_t);
352 sel->kind = eff_select;
354 CHECK_NAME (selelm, select);
355 VERBOSE_PRINT ((stdout, "select node \t0x%08x\n", (int) selelm));
357 ent = getEntityById (entity_id);
358 assert(ent && "ir_entity not found");
359 VERBOSE_PRINT ((stdout, "select ir_entity %s\n", get_id_str(ent -> ent_ident)));
361 child = selelm->xmlChildrenNode;
364 valref = parseValref (doc, child);
367 sel-> id = valref ? valref-> id : NO_ID;
368 sel-> effect.select.ent = ent;
378 parseLoad (xmlDocPtr doc, xmlNodePtr loadelm)
383 eff_t *load = NEW (eff_t);
384 load->kind = eff_load;
386 CHECK_NAME (loadelm, load);
387 VERBOSE_PRINT ((stdout, "load node \t0x%08x\n", (int) loadelm));
388 id = new_id_from_str(getNodeId (loadelm));
390 child = get_valid_child(loadelm);
391 if(NODE_NAME (child, select)) {
392 sel = parseSelect (doc, child);
393 load-> effect.load.ent = sel-> effect.select.ent;
394 VERBOSE_PRINT ((stdout, "load ir_entity \t%s\n",
395 get_id_str(load -> effect.load.ent -> ent_ident)));
398 sel = parseValref (doc, child);
399 load-> effect.load.ent = NULL;
403 load-> effect.load.ptrrefid = sel-> id;
411 parseStore (xmlDocPtr doc, xmlNodePtr storeelm)
416 eff_t *store = NEW (eff_t);
417 store->kind = eff_store;
419 CHECK_NAME (storeelm, store);
420 VERBOSE_PRINT ((stdout, "store node \t0x%08x\n", (int) storeelm));
422 child = get_valid_child(storeelm);
423 if(NODE_NAME (child, select)) {
424 sel = parseSelect (doc, child);
425 store-> effect.store.ent = sel-> effect.select.ent;
428 sel = parseValref (doc, child);
429 store-> effect.store.ent = NULL;
433 valref = parseValref (doc, child);
435 store-> effect.store.ptrrefid = sel-> id;
436 store-> effect.store.valrefid = valref-> id;
445 parseAlloc (xmlDocPtr doc, xmlNodePtr allocelm)
449 eff_t *alloc = NEW (eff_t); /* ...! */
450 alloc->kind = eff_alloc;
452 CHECK_NAME (allocelm, alloc);
453 VERBOSE_PRINT ((stdout, "alloc node \t0x%08x\n", (int) allocelm));
454 id = new_id_from_str(getNodeId (allocelm));
455 VERBOSE_PRINT ((stdout, "alloc->id = \"%s\"\n", get_id_str(id)));
456 type_id = new_id_from_str(getNodeTypeId (allocelm));
457 VERBOSE_PRINT ((stdout, "alloc->type_id = \"%s\"\n", get_id_str(type_id)));
460 alloc-> effect.alloc.tp_id = type_id;
466 parseCall (xmlDocPtr doc, xmlNodePtr callelm)
473 eff_t *call = NEW (eff_t);
474 call->kind = eff_call;
476 CHECK_NAME (callelm, call);
477 VERBOSE_PRINT ((stdout, "call node \t0x%08x\n", (int) callelm));
478 id = new_id_from_str(getNodeId (callelm));
479 VERBOSE_PRINT ((stdout, "call->id = \"%s\"\n", get_id_str(id)));
481 child = get_valid_child(callelm);
482 if(NODE_NAME (child, select)) {
483 sel = parseSelect (doc, child);
484 call-> effect.call.ent = sel-> effect.select.ent;
487 sel = parseValref (doc, child);
488 call-> effect.call.ent = NULL;
491 arg = child = child->next;
494 while (NULL != child) {
500 call-> effect.call.valrefid = sel-> id;
501 call-> effect.call.n_args = n_args;
502 call-> effect.call.args = NULL;
507 ident **args = (ident**) xmalloc(n_args * sizeof(ident*));
510 while (NULL != arg) {
511 eff_t *valref = parseValref (doc, arg);
512 args [i ++] = valref-> id;
517 call-> effect.call.args = args;
524 parseJoin (xmlDocPtr doc, xmlNodePtr joinelm)
531 eff_t *join = NEW (eff_t);
532 join->kind = eff_join;
534 CHECK_NAME (joinelm, join);
535 VERBOSE_PRINT ((stdout, "join node \t0x%08x\n", (int) joinelm));
536 id = new_id_from_str(getNodeId (joinelm));
537 VERBOSE_PRINT ((stdout, "join->id = \"%s\"\n", get_id_str(id)));
539 child = get_valid_child(joinelm);
542 while (NULL != child) {
547 ins = (ident **) xmalloc (n_ins * sizeof (ident *) );
549 child = get_valid_child(joinelm);
551 while (NULL != child) {
552 eff_t *valref = parseValref (doc, child);
553 ins [i ++] = valref-> id;
559 join-> effect.join.n_ins = n_ins;
560 join-> effect.join.ins = ins;
566 parseUnknown (xmlDocPtr doc, xmlNodePtr unknownelm)
569 eff_t *unknown = NEW (eff_t);
570 unknown->kind = eff_unknown;
572 CHECK_NAME (unknownelm, unknown);
573 VERBOSE_PRINT ((stdout, "unknown node \t0x%08x\n", (int) unknownelm));
574 id = new_id_from_str(getNodeId (unknownelm));
581 parseReturn (xmlDocPtr doc, xmlNodePtr retelm)
584 eff_t *ret = NEW (eff_t);
587 CHECK_NAME (retelm, ret);
588 VERBOSE_PRINT ((stdout, "ret node \t0x%08x\n", (int) retelm));
590 child = get_any_valid_child(retelm);
593 eff_t *valref = parseValref (doc, child);
594 ret-> effect.ret.ret_id = valref-> id;
597 ret-> effect.ret.ret_id = NO_ID;
604 parseRaise (xmlDocPtr doc, xmlNodePtr raiseelm)
609 eff_t *raise = NEW (eff_t);
610 raise->kind = eff_raise;
612 CHECK_NAME (raiseelm, raise);
613 VERBOSE_PRINT ((stdout, "raise node \t0x%08x\n", (int) raiseelm));
614 tp_id = getNodeTypeId (raiseelm);
615 VERBOSE_PRINT ((stdout, "raise->type = \"%s\"\n", tp_id));
616 child = get_valid_child(raiseelm);
618 assert (NULL != child);
620 valref = parseValref (doc, child);
621 raise-> effect.raise.valref = valref-> id;
622 raise-> effect.raise.tp_id = new_id_from_str(tp_id);
633 /** parse a type node and insert it into the list */
635 parseType (xmlDocPtr doc, xmlNodePtr typeelm)
638 const char *tp_id = getNodeId (typeelm);
639 VERBOSE_PRINT ((stdout, "type node \t0x%08x (%s)\n", (int) typeelm, tp_id));
640 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);
657 const char *ent_id = getNodeId (entelm);
658 /* fprintf (stdout, "ir_entity node \t0x%08x (%d)\n", (int) entelm, ent_id); */
659 VERBOSE_PRINT ((stdout, "ent = \"%s.%s\"\n",
660 getNodeTypeStr (entelm),
661 getNodeEntityStr (entelm)));
664 ent -> ent_ident = new_id_from_str (getNodeEntityStr (entelm));
665 ent -> tp_ident = new_id_from_str (getNodeTypeStr (entelm));
666 ent -> owner = new_id_from_str (getNodeOwnerStr (entelm));
667 ent -> id = new_id_from_str(ent_id);
669 ent->prev = entities;
673 /** parse any effect, and turn it into an eff_t (TODO) */
675 parseEffect (xmlDocPtr doc, xmlNodePtr effelm)
678 const char *procname = getNodeProcName (effelm);
679 const char *ownerid = getNodeOwnerStr (effelm);
680 proc_t *curr_effs = NULL;
684 VERBOSE_PRINT ((stdout, "effect for method \"%s\"\n", procname));
686 cur = effelm -> xmlChildrenNode;
687 while (NULL != cur) {
691 VERBOSE_PRINT ((stdout, "has %d effects\n", n_effs));
693 curr_effs = NEW (proc_t);
694 curr_effs -> proc_ident = new_id_from_str(procname);
695 curr_effs -> ownerid = new_id_from_str(ownerid);
696 curr_effs->effs = (eff_t**) xmalloc (n_effs * sizeof (eff_t*));
698 cur = effelm -> xmlChildrenNode;
699 while (NULL != cur) {
702 if (NODE_NAME (cur, arg)) {
703 eff = (eff_t*) parseArg (doc, cur);
704 } else if (NODE_NAME (cur, load)) {
705 eff = (eff_t*) parseLoad (doc, cur);
706 } else if (NODE_NAME (cur, store)) {
707 eff = (eff_t*) parseStore (doc, cur);
708 } else if (NODE_NAME (cur, alloc)) {
709 eff = (eff_t*) parseAlloc (doc, cur);
710 } else if (NODE_NAME (cur, call)) {
711 eff = (eff_t*) parseCall (doc, cur);
712 } else if (NODE_NAME (cur, join)) {
713 eff = (eff_t*) parseJoin (doc, cur);
714 } else if (NODE_NAME (cur, unknown)) {
715 eff = (eff_t*) parseUnknown (doc, cur);
716 } else if (NODE_NAME (cur, ret)) {
717 eff = (eff_t*) parseReturn (doc, cur);
718 } else if (NODE_NAME (cur, raise)) {
719 eff = (eff_t*) parseRaise (doc, cur);
720 } else if (NODE_NAME (cur, comment)) {
724 fprintf (stderr, "wrong element \"%s\"\n", BAD_CAST cur->name);
728 VERBOSE_PRINT ((stdout, "effect %p@%d\n", (void*)eff, i));
729 curr_effs -> effs[i++] = eff;
733 assert((i == n_effs) && "incorrect number of effects");
734 curr_effs -> n_effs = n_effs;
735 curr_effs -> next = procs;
741 int read_extern (const char *filename)
743 /* xmlNsPtr ns = NULL; */ /* no namespace for us */
744 xmlDocPtr doc; /* whole document */
745 xmlNodePtr cur; /* current node */
749 /* i've got no idea what the VERSION cast is all about. voodoo
750 programming at its finest. */
751 LIBXML_TEST_VERSION xmlKeepBlanksDefault (0);
752 VERBOSE_PRINT((stdout, "read file %s\n", filename));
753 doc = xmlParseFile (filename);
757 cur = xmlDocGetRootElement (doc);
758 CHECK (cur, "xmlDocGetRootElement");
760 if (! NODE_NAME (cur, effects)) {
761 fprintf (stderr,"root node \"%s\" != \"effects\"\n", BAD_CAST cur->name);
766 mod_id = getNodeModuleIdent (cur);
767 if (NULL != mod_id) {
768 VERBOSE_PRINT ((stdout, "effects for \"%s\"\n",
769 get_id_str(mod_id)));
772 VERBOSE_PRINT ((stdout, "effects \t0x%08x\n", (int) cur));
776 cur = cur->xmlChildrenNode;
777 while (cur != NULL) {
778 if (NODE_NAME (cur, type)) {
779 parseType (doc, cur);
780 } else if (NODE_NAME (cur, ir_entity)) {
781 parseEntity (doc, cur);
782 } else if (NODE_NAME (cur, effect)) {
783 parseEffect (doc, cur);
784 } else if ((NODE_NAME (cur, comment))) {
787 fprintf (stderr, "wrong element \"%s\"\n", BAD_CAST cur->name);
793 module = NEW(module_t);
794 module -> id = mod_id;
795 module -> types = types;
796 module -> entities = entities;
797 module -> procs = procs;
803 module -> next = modules;
809 /********************************************************************/
812 * free additional structure
815 void freeArg (eff_t *arg)
817 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));
831 void freeSelect (eff_t *sel)
833 VERBOSE_PRINT ((stdout, "free select node \t0x%08x\n", (int) sel));
839 void freeLoad (eff_t *load)
841 VERBOSE_PRINT ((stdout, "free load node \t0x%08x\n", (int) load));
847 void freeStore (eff_t *store)
849 VERBOSE_PRINT ((stdout, "free store node \t0x%08x\n", (int) store));
855 void freeAlloc (eff_t *alloc)
857 VERBOSE_PRINT ((stdout, "free alloc node \t0x%08x\n", (int) alloc));
863 void freeCall (eff_t *call)
865 VERBOSE_PRINT ((stdout, "free call node \t0x%08x\n", (int) call));
866 free(call -> effect.call.args);
872 void freeJoin (eff_t *join)
874 VERBOSE_PRINT ((stdout, "free join node \t0x%08x\n", (int) join));
875 free(join -> effect.join.ins);
881 void freeUnknown (eff_t *unknown)
883 VERBOSE_PRINT ((stdout, "free unknown node \t0x%08x\n", (int) unknown));
889 void freeReturn (eff_t *ret)
891 VERBOSE_PRINT ((stdout, "free ret node \t0x%08x\n", (int) ret));
897 void freeRaise (eff_t *raise)
899 VERBOSE_PRINT ((stdout, "free raise node \t0x%08x\n", (int) raise));
906 void freeProcEffs(proc_t *proc)
911 VERBOSE_PRINT ((stdout, "free effect for method \"%s\"\n",
912 get_id_str(proc -> proc_ident)));
914 num = proc -> n_effs;
915 for(i = 0; i < num; i++) {
916 switch(proc -> effs[i] -> kind) {
918 freeArg(proc -> effs[i]);
921 freeValref(proc -> effs[i]);
924 freeSelect(proc -> effs[i]);
927 freeLoad(proc -> effs[i]);
930 freeStore(proc -> effs[i]);
933 freeAlloc(proc -> effs[i]);
936 freeCall(proc -> effs[i]);
939 freeUnknown(proc -> effs[i]);
942 freeJoin(proc -> effs[i]);
945 freeRaise(proc -> effs[i]);
948 freeReturn(proc -> effs[i]);
951 assert(0 && "try to free an unknown effect");
960 void freeModuleProcs(module_t *module)
962 proc_t *next_proc, *proc;
964 VERBOSE_PRINT ((stdout, "free procs for module \"%s\"\n",
965 get_id_str(module -> id)));
967 proc = module -> procs;
969 next_proc = proc -> next;
979 module_t *module, *next_module;
983 freeModuleProcs(module);
984 next_module = module -> next;
986 module = next_module;
990 /********************************************************************/
993 type_t *find_type_in_module(module_t *module, ident *typeid)
997 for(type = module -> types; type; type = type -> prev) {
998 VERBOSE_PRINT((stdout, "test typeid %s\n", get_id_str(type -> id)));
999 if(type -> id == typeid) {
1000 VERBOSE_PRINT((stdout, "found\n"));
1004 VERBOSE_PRINT((stdout, "did not find type id %s\n", get_id_str(typeid)));
1008 /********************************************************************/
1010 static void add_value_to_proc(proc_t *proc, eff_t *eff)
1012 eff -> next = proc -> values;
1013 proc -> values = eff;
1017 eff_t *find_valueid_in_proc_effects(ident *id, proc_t *proc)
1021 val = proc -> values;
1023 if(id == val -> id) {
1031 static void create_abstract_return(ir_graph *irg, proc_t *proc, eff_t *eff)
1036 VERBOSE_PRINT((stdout, "create effect:return in %s\n",
1037 get_id_str(proc -> proc_ident)));
1038 if(NO_ID == eff -> effect.ret.ret_id) {
1040 x = new_Return (get_store(), 0, NULL);
1045 /* return one value */
1046 eff_res = find_valueid_in_proc_effects(eff -> effect.ret.ret_id, proc);
1047 assert(eff_res -> firmnode && "firm in effect not set");
1048 in[0] = eff_res -> firmnode;
1049 x = new_Return (get_store(), 1, in);
1051 eff -> firmnode = x;
1053 /* Now we generated all instructions for this block and all its predecessor
1054 * blocks so we can mature it. (There are not too much.) */
1055 mature_immBlock (get_irg_current_block(irg));
1057 /* This adds the in edge of the end block which originates at the return statement.
1058 * The return node passes controlflow to the end block. */
1059 add_immBlock_pred (get_irg_end_block(irg), x);
1063 static void create_abstract_arg(ir_graph *irg, proc_t *proc, eff_t *eff)
1071 VERBOSE_PRINT((stdout, "create effect:arg %d in %s\n",
1072 eff -> effect.arg.num, get_id_str(proc -> proc_ident)));
1073 ent = get_irg_entity(irg);
1074 typ = get_entity_type(ent);
1076 /* read argument eff -> effect.arg.num and place in values list */
1077 num = get_method_n_params(typ);
1078 assert((num >= eff -> effect.arg.num) && "number too big");
1079 typ = get_method_param_type(typ, eff -> effect.arg.num);
1080 mode = get_type_mode(typ);
1082 arg = new_Proj(get_irg_args(irg), mode, eff -> effect.arg.num);
1083 eff -> firmnode = arg;
1085 add_value_to_proc(proc, eff);
1089 static void create_abstract_load(ir_graph *irg, proc_t *proc, eff_t *eff)
1091 ir_node *sel, *load;
1096 VERBOSE_PRINT((stdout, "create load in %s\n",
1097 get_id_str(proc -> proc_ident)));
1099 if(eff -> effect.load.ent) {
1100 ent = eff -> effect.load.ent -> f_ent;
1101 VERBOSE_PRINT((stdout, "load from %s\n", get_entity_name(ent)));
1104 VERBOSE_PRINT((stdout, "store to memory\n"));
1108 addr = find_valueid_in_proc_effects(eff -> effect.load.ptrrefid, proc);
1109 assert(addr && "no address for load");
1110 /* if addr is Unknown, set proper mode */
1111 if(iro_Unknown == get_irn_opcode(addr -> firmnode)) {
1112 set_irn_mode(addr -> firmnode, mode_P);
1116 sel = new_simpleSel(get_store(), addr -> firmnode, ent);
1117 mode = get_type_mode(get_entity_type(ent));
1120 sel = addr -> firmnode;
1123 load = new_Load(get_store(), sel, mode);
1124 set_store(new_Proj(load, mode_M, 0));
1125 eff -> firmnode = new_Proj(load, mode, 2);
1127 add_value_to_proc(proc, eff);
1131 static void create_abstract_store(ir_graph *irg, proc_t *proc, eff_t *eff)
1133 ir_node *sel, *store;
1137 VERBOSE_PRINT((stdout, "create store in %s\n",
1138 get_id_str(proc -> proc_ident)));
1140 if(eff -> effect.store.ent) {
1141 ent = eff -> effect.store.ent -> f_ent;
1142 VERBOSE_PRINT((stdout, "store to ir_entity %s\n", get_entity_name(ent)));
1145 VERBOSE_PRINT((stdout, "store to memory\n"));
1149 addr = find_valueid_in_proc_effects(eff -> effect.store.ptrrefid, proc);
1150 assert(addr && "no address for store");
1151 /* if addr is Unknown, set propper mode */
1152 if(iro_Unknown == get_irn_opcode(addr -> firmnode)) {
1153 set_irn_mode(addr -> firmnode, mode_P);
1156 val = find_valueid_in_proc_effects(eff -> effect.store.valrefid, proc);
1157 assert(val && "no address for store");
1158 /* if addr is Unknown, set propper mode */
1159 if(iro_Unknown == get_irn_opcode(val -> firmnode)) {
1160 set_irn_mode(val -> firmnode, get_type_mode(get_entity_type(ent)));
1164 sel = new_simpleSel(get_store(), addr -> firmnode, ent);
1167 sel = addr -> firmnode;
1169 store = new_Store(get_store(), sel, val -> firmnode);
1170 set_store(new_Proj(store, mode_M, 0));
1171 eff -> firmnode = store;
1175 static void create_abstract_alloc(ir_graph *irg, proc_t *proc, eff_t *eff)
1180 symconst_symbol sym;
1182 VERBOSE_PRINT((stdout, "create alloc in %s\n",
1183 get_id_str(proc -> proc_ident)));
1185 xtype = find_type_in_module(current_module, eff -> effect.alloc.tp_id);
1186 assert(xtype && "type not found");
1187 ftype = xtype -> f_tp;
1190 alloc = new_Alloc(get_store(), new_SymConst(sym, symconst_type_size), ftype,
1192 set_store(new_Proj(alloc, mode_M, 0));
1193 eff -> firmnode = new_Proj(alloc, mode_P, 2);
1195 add_value_to_proc(proc, eff);
1199 static void create_abstract_unknown(ir_graph *irg, proc_t *proc, eff_t *eff)
1203 VERBOSE_PRINT((stdout, "create unknown in %s\n",
1204 get_id_str(proc -> proc_ident)));
1206 unknown = new_Unknown(mode_ANY);
1207 eff -> firmnode = unknown;
1209 add_value_to_proc(proc, eff);
1213 static void create_abstract_call(ir_graph *irg, proc_t *proc, eff_t *eff)
1215 ir_node *sel, *call;
1221 int mik; /* is method somehow known? */
1223 VERBOSE_PRINT((stdout, "create call in %s\n",
1224 get_id_str(proc -> proc_ident)));
1226 if(eff -> effect.call.ent) {
1227 ent = eff -> effect.call.ent -> f_ent;
1228 VERBOSE_PRINT((stdout, "call %s\n", get_entity_name(ent)));
1232 VERBOSE_PRINT((stdout, "call something in memory\n"));
1235 addr = find_valueid_in_proc_effects(eff -> effect.call.valrefid, proc);
1236 assert(addr && "no address for load");
1237 /* if addr is Unknown, set proper mode */
1238 if(iro_Unknown == get_irn_opcode(addr -> firmnode)) {
1239 set_irn_mode(addr -> firmnode, mode_P);
1244 sel = new_simpleSel(get_store(), addr -> firmnode, ent);
1246 mtype = get_entity_type(ent);
1251 sel = addr -> firmnode;
1253 mtype = get_unknown_type();
1258 num = eff -> effect.call.n_args;
1259 VERBOSE_PRINT((stdout, "number of args given: %d\n", num));
1261 VERBOSE_PRINT((stdout, "number of args expected: %d\n",
1262 get_method_n_params(mtype)));
1264 irns = alloca(num * sizeof(ir_node*));
1265 for(i = 0; i < num; i++) {
1266 irns[i] = find_valueid_in_proc_effects(eff -> effect.call.args[i], proc)
1268 if(iro_Unknown == get_irn_opcode(irns[i])) {
1270 set_irn_mode(irns[i], get_type_mode(get_method_param_type(mtype, i)));
1273 set_irn_mode(irns[i], mode_ANY);
1277 call = new_Call(get_store(), sel, num, irns, mtype);
1278 set_store(new_Proj(call, mode_M, 0));
1279 if(mik && (0 != get_method_n_ress(mtype))) {
1280 eff -> firmnode = new_Proj(call,
1281 get_type_mode(get_method_res_type(mtype, 0)),
1283 add_value_to_proc(proc, eff); /* result can be accessed */
1286 eff -> firmnode = NULL; /* result can not be accessed */
1290 static void create_abstract_join (ir_graph *irg, proc_t *proc, eff_t *eff)
1292 ir_node **ins = NULL;
1293 ir_node *unknown = NULL;
1294 ir_node *cond = NULL;
1295 ir_node *block = NULL;
1296 ir_node *c_block = NULL;
1297 ir_node *phi = NULL;
1298 ir_mode *join_md = mode_ANY;
1302 VERBOSE_PRINT((stdout, "create join in %s\n",
1303 get_id_str(proc -> proc_ident)));
1305 assert (eff_join == eff->kind);
1307 n_ins = eff->effect.join.n_ins;
1309 /* seems like current_block is not always mature at this point */
1310 mature_immBlock (get_cur_block ());
1312 block = get_cur_block (); /* remember this so we can put the ProjXs into it */
1314 /* jump based on an unknown condition so all values are possible */
1315 unknown = new_Unknown (mode_Iu);
1316 cond = new_Cond (unknown);
1318 c_block = new_immBlock (); /* for the Phi after the branch(es) */
1320 ins = (ir_node**) xmalloc (n_ins * sizeof (ir_node*));
1321 for (i = 0; i < n_ins; i ++) {
1322 ir_node *projX = NULL;
1323 ir_node *s_block = NULL;
1324 ir_node *jmp = NULL;
1327 /* make sure the projX is in the 'switch' block */
1328 set_cur_block (block);
1329 projX = new_Proj (cond, mode_X, (long) i);
1331 /* this also sets current_block, so the rest of the code ends up there: */
1332 s_block = new_immBlock ();
1334 add_immBlock_pred (s_block, projX);
1335 mature_immBlock (s_block);
1337 in_eff = find_valueid_in_proc_effects (eff->effect.join.ins [i], proc);
1339 ins [i] = in_eff->firmnode;
1341 /* need to find a suitable mode for the Phi node */
1342 if (mode_ANY != get_irn_mode (ins [i])) {
1343 join_md = get_irn_mode (ins [i]);
1347 add_immBlock_pred (c_block, jmp);
1350 set_cur_block (c_block);
1352 phi = new_Phi (n_ins, ins, join_md);
1354 mature_immBlock (c_block);
1355 memset (ins, 0x00, n_ins * sizeof (ir_node*));
1358 eff->firmnode = phi;
1360 add_value_to_proc (proc, eff);
1363 static void create_abstract_raise (ir_graph *irg, proc_t *proc, eff_t *eff)
1365 ir_node *block = NULL;
1366 ir_node *unknown = NULL;
1367 ir_node *cond = NULL;
1369 /* seems like current_block is not always mature at this point */
1370 mature_immBlock (get_cur_block ());
1371 block = get_cur_block (); /* remember this so we can put the ProjXs into it */
1373 /* jump based on an unknown condition so both values are possible */
1374 unknown = new_Unknown (mode_Iu);
1375 cond = new_Cond (unknown);
1377 /* one branch for 'throw-exception' case */
1379 ir_node *projX = new_Proj (cond, mode_X, 1L);
1380 ir_node *b_exc = new_immBlock ();
1381 ir_node *obj = NULL;
1382 ir_node *thrw = NULL;
1383 eff_t *thrw_eff = NULL;
1385 add_immBlock_pred (b_exc, projX);
1387 thrw_eff = find_valueid_in_proc_effects (eff->effect.raise.valref, proc);
1388 obj = thrw_eff->firmnode;
1390 thrw = new_Raise (get_store (), obj);
1391 /* exc-jump to end block */
1392 thrw = new_Proj (thrw, mode_X, 0L);
1394 add_immBlock_pred (get_irg_end_block (irg), thrw);
1395 mature_immBlock (get_cur_block ());
1398 set_cur_block (block); /* back to the first block */
1400 /* one branch for 'non-exception' case */
1402 ir_node *projX = new_Proj (cond, mode_X, 0);
1403 new_immBlock (); /* also sets current_block */
1404 add_immBlock_pred (get_cur_block (), projX);
1405 mature_immBlock (get_cur_block ());
1406 /* continue building in current_block */
1411 static void create_abstract_firm(module_t *module, proc_t *proc, ir_entity *fent)
1417 /* test ir_entity */
1418 assert(visibility_external_allocated == get_entity_visibility(fent)
1419 && peculiarity_existent == get_entity_peculiarity(fent)
1420 && "not an abstract ir_entity");
1421 /* create irg in ir_entity */
1422 irg = new_pseudo_ir_graph(fent, 0);
1423 set_irg_inline_property(irg, irg_inline_forbidden);
1425 /* @@@ If the spec says so: */
1426 set_entity_visibility(fent, visibility_local);
1428 VERBOSE_PRINT((stdout, "create effects for %s\n",
1429 get_id_str(proc -> proc_ident)));
1431 /* create effects in irg */
1432 num = proc -> n_effs;
1433 for(i = 0; i < num; i++) {
1434 eff = proc -> effs[i];
1435 VERBOSE_PRINT((stdout,
1436 "create effect \"%s\"\n", effect_string[(int)eff -> kind]));
1437 switch(eff -> kind) {
1439 create_abstract_return(irg, proc, eff);
1442 create_abstract_arg(irg, proc, eff);
1445 create_abstract_load(irg, proc, eff);
1448 create_abstract_store(irg, proc, eff);
1451 create_abstract_unknown(irg, proc, eff);
1454 create_abstract_alloc(irg, proc, eff);
1457 create_abstract_call(irg, proc, eff);
1460 create_abstract_join(irg, proc, eff);
1463 create_abstract_raise(irg, proc, eff);
1466 assert(0 && "effect not implemented");
1471 /* close irg in ir_entity */
1472 /* Now we can mature the end block as all it's predecessors are known. */
1473 mature_immBlock (get_irg_end_block(irg));
1475 /* Verify the graph. Finds some very bad errors in the graph. */
1476 VERBOSE_PRINT((stdout, "verify graph\n"));
1478 VERBOSE_PRINT((stdout, "finalize construction\n"));
1479 irg_finalize_cons (irg);
1482 /********************************************************************/
1484 static void assign_firm_entity(module_t *module, entity_t *xmlent)
1491 VERBOSE_PRINT((stdout, "assign ir_entity %s to typeid %s\n",
1492 get_id_str(xmlent -> ent_ident),
1493 get_id_str(xmlent -> owner)));
1495 typ = find_type_in_module(module, xmlent -> owner);
1496 assert(typ && "class not found in module");
1498 assert(is_Class_type(type));
1500 num = get_class_n_members(type);
1502 for(i = 0; i < num; i++) {
1503 ent = get_class_member(type, i);
1504 VERBOSE_PRINT((stdout, "compare ir_entity %s and %s\n",
1505 get_id_str(xmlent -> ent_ident), get_entity_name(ent)));
1507 if(get_entity_ident(ent) == xmlent -> ent_ident) {
1512 assert(ent && "did not find a ir_entity");
1514 xmlent -> f_ent = ent;
1517 /********************************************************************/
1518 /* must be primitive type or class type */
1519 static void assign_firm_type(type_t *xmltype)
1522 ir_type *typ = NULL;
1525 VERBOSE_PRINT((stdout, "assign firm type to type %s\n",
1526 get_id_str(xmltype -> type_ident)));
1528 /* is it global type? */
1529 typ = get_glob_type();
1530 if(xmltype -> type_ident == get_type_ident(typ)) {
1532 xmltype -> f_tp = typ;
1533 VERBOSE_PRINT((stdout, "is global type %s\n", get_type_name(typ)));
1535 num = get_irp_n_types();
1536 for(i = 0; i < num; i++) {
1537 typ = get_irp_type(i);
1538 VERBOSE_PRINT((stdout, "test type %s\n", get_type_name(typ)));
1539 if(xmltype -> type_ident == get_type_ident(typ)) {
1540 VERBOSE_PRINT((stdout, "found type %s\n", get_type_name(typ)));
1541 xmltype -> f_tp = typ;
1547 assert(typ && "did not find a type");
1550 /********************************************************************/
1552 void create_abstract_proc_effect(module_t *module, proc_t *proc)
1555 ir_type *class_typ = NULL;
1559 /* find the class of a procedure */
1560 VERBOSE_PRINT((stdout, "do find owner id %s\n", get_id_str(proc -> ownerid)));
1561 type = find_type_in_module(module, proc -> ownerid);
1562 assert(type && "class not found in module");
1564 class_typ = get_glob_type();
1565 VERBOSE_PRINT((stdout, "test type %s\n", get_type_name(class_typ)));
1566 if(type -> type_ident != get_type_ident(class_typ)) {
1567 /* find module as class */
1568 num = get_irp_n_types();
1569 for(i = 0; i < num; i++) {
1570 class_typ = get_irp_type(i);
1571 VERBOSE_PRINT((stdout, "test type %s\n", get_type_name(class_typ)));
1572 if (is_Class_type(class_typ)
1573 && (type -> type_ident == get_type_ident(class_typ))) {
1574 /* found class type */
1575 VERBOSE_PRINT((stdout, "found type %s\n", get_type_name(class_typ)));
1582 VERBOSE_PRINT((stdout, "found global type %s\n", get_type_name(class_typ)));
1584 assert(class_typ && "type not found");
1585 assert(is_Class_type(class_typ) && "is not a class type");
1586 type -> f_tp = class_typ;
1588 /* find ir_entity for procedure in class */
1589 VERBOSE_PRINT((stdout, "find method %s\n",
1590 get_id_str(proc -> proc_ident)));
1592 num = get_class_n_members(class_typ);
1594 for(i = 0; i < num; i++) {
1595 fent = get_class_member(class_typ, i);
1596 VERBOSE_PRINT((stdout, "test proc %s\n", get_entity_name(fent)));
1597 if(proc -> proc_ident == get_entity_ident(fent)) {
1598 VERBOSE_PRINT((stdout, "found proc %s\n",
1599 get_id_str(proc -> proc_ident)));
1600 /* @@@ TODO check args types - not in xml yet */
1601 /* create Firm stuff */
1602 create_abstract_firm(module, proc, fent);
1612 "method %s not found\nNo effects generated\nCandidates are:\n",
1613 get_id_str(proc -> proc_ident));
1614 for(i = 0; i < num; i++) {
1615 fent = get_class_member(class_typ, i);
1616 fprintf(stderr, "%s\n", get_entity_name(fent));
1618 //assert(fent && "procedure not found in class");
1622 void create_abstract_module(module_t *module)
1628 VERBOSE_PRINT((stdout, "create an abstraction for module %s\n",
1629 get_id_str(module -> id)));
1631 VERBOSE_PRINT((stdout, "--handle types for module\n"));
1632 for(type = module -> types; type; type = type -> prev) {
1633 assign_firm_type(type);
1636 VERBOSE_PRINT((stdout, "--handle entities for module\n"));
1638 for(ent = module -> entities; ent; ent = ent -> prev) {
1639 assign_firm_entity(module, ent);
1642 VERBOSE_PRINT((stdout, "--handle procs for module\n"));
1643 for(proc = module -> procs; proc; proc = proc -> next) {
1644 create_abstract_proc_effect(module, proc);
1649 int create_abstraction(const char *filename)
1653 /* read and parse XML file */
1654 if (! read_extern(filename))
1657 /* finished reading and parsing here */
1658 /* build FIRM graphs */
1661 current_module = module;
1662 create_abstract_module(module);
1663 module = module -> next;
1665 current_module = NULL;
1667 /* free data structures */
1679 void free_abstraction(void) {
1680 int i, n_pseudo_irgs = get_irp_n_pseudo_irgs();
1681 for (i = 0; i < n_pseudo_irgs; ++i) {
1682 ir_graph *p_irg = get_irp_pseudo_irg(i);
1683 set_entity_visibility(get_irg_entity(p_irg), visibility_external_allocated);
1684 // @@@ free_pseudo_ir_graph(p_irg);
1689 /********************************************************************/
1694 * Revision 1.27 2007/02/02 12:38:35 matze
1695 * entity is ir_entity now
1697 * Revision 1.26 2006/12/15 12:37:40 matze
1700 * Revision 1.25 2006/06/09 11:26:35 firm
1701 * renamed type to ir_type
1703 * Revision 1.24 2006/05/29 13:34:49 beck
1704 * renamed symconst_size to symconst_type_size
1706 * Revision 1.22 2005/08/16 10:18:35 beck
1707 * create_abstraction() now returns an error code if the file could not
1710 * Revision 1.21 2005/03/10 10:05:38 goetz
1711 * chanmged method name
1713 * Revision 1.20 2005/01/05 14:28:35 beck
1714 * renames all is_x*_type() functions to is_X*_type() to prevent name clash with EDG frontend
1716 * Revision 1.19 2004/12/10 15:14:34 beck
1717 * used xmalloc instead of malloc
1719 * Revision 1.18 2004/12/02 16:21:42 beck
1720 * fixed config.h include
1722 * Revision 1.17 2004/11/23 14:17:31 liekweg
1723 * fenced out currently unneeded static functions
1725 * Revision 1.16 2004/11/11 12:24:52 goetz
1728 * Revision 1.15 2004/11/11 09:28:32 goetz
1729 * treat pseudo irgs special
1730 * parse 'local' from xml files
1732 * Revision 1.14 2004/11/10 14:42:00 boesler
1733 * be more helpful if a method does not exist
1735 * Revision 1.13 2004/11/05 14:00:53 liekweg
1738 * Revision 1.12 2004/11/02 14:30:31 liekweg
1739 * fixed multi-input join (thx, Boris) --flo
1741 * Revision 1.11 2004/10/29 18:51:53 liekweg
1744 * Revision 1.10 2004/10/25 13:52:24 boesler
1745 * seperated read.h (public interface) and read_t.h (types)
1747 * Revision 1.9 2004/10/22 13:51:35 boesler
1748 * prohibit inlining of pseudo ir_graphs
1750 * Revision 1.8 2004/10/22 13:13:27 boesler
1751 * replaced char* by idents, minor fix in Firm codegen for call
1753 * Revision 1.7 2004/10/21 15:31:55 boesler
1754 * added lots of stuff:
1755 * - build abstract syntax trees
1756 * - build Firm graphs for many effects, still todos
1758 * Revision 1.5 2004/10/18 12:48:20 liekweg
1761 * Revision 1.4 2004/10/14 11:31:53 liekweg
1764 * Revision 1.3 2004/10/13 13:36:28 rubino
1767 * Revision 1.2 2004/10/11 15:56:09 liekweg
1768 * Cleanup, comments ...
1769 * Added init func --flo
1771 * Revision 1.1 2004/10/11 09:31:06 liekweg
1772 * First Import of XML reading procs --flo