5 * File name: ir/external/read.c
6 * Purpose: Read descriptions of external effects
8 * Modified by: Boris Boesler
11 * Copyright: (c) 1999-2004 Universität Karlsruhe
12 * Licence: This file is protected by GPL - GNU GENERAL PUBLIC LICENSE.
19 /* get prototype for alloca somehow */
40 #define VERBOSE_PRINTING 0
43 # define VERBOSE_PRINT(s) fprintf s
45 # define VERBOSE_PRINT(s)
50 static type_t *types = NULL;
51 static entity_t *entities = NULL;
52 static proc_t *procs = NULL;
53 static module_t *modules = NULL;
56 static module_t *current_module = NULL;
59 /* this is only used inside a VERBOSE_PRINT() call */
60 static char *effect_string[] = {
73 #endif /* defined VERBOSE_PRINTING */
76 getNodeModuleIdent (xmlNodePtr node)
78 const char *mod_str = (const char*) xmlGetProp (node, BAD_CAST "module");
80 if (NULL == mod_str) {
83 const ident *res = new_id_from_str (mod_str);
89 getNodeProcName (xmlNodePtr node)
91 const char *proc_str = (const char*) xmlGetProp (node, BAD_CAST "procname");
97 getNodeClassName (xmlNodePtr node)
99 char *proc_str = (char*) xmlGetProp (node, BAD_CAST "class");
101 return ( (proc_str));
105 getNodeId (xmlNodePtr node)
107 const char *id_str = (const char*) xmlGetProp (node, BAD_CAST "id");
113 getNodeRefId (xmlNodePtr node)
115 const char *refid_str = (char*) xmlGetProp (node, BAD_CAST "refid");
117 return ((refid_str));
121 getNodeTypeId (xmlNodePtr node)
123 const char *type_str = (char*) xmlGetProp (node, BAD_CAST "type");
129 *getNodeTypeStr (xmlNodePtr node)
131 const char *type_str = (char*) xmlGetProp (node, BAD_CAST "type");
137 getNodeOwnerStr (xmlNodePtr node)
139 const char *owner_str = (char*) xmlGetProp (node, BAD_CAST "owner");
145 *getNodeEntityStr (xmlNodePtr node)
147 const char *ent_str = (char*) xmlGetProp (node, BAD_CAST "entity");
158 type_t *getTypeByIdent (const ident *id)
160 type_t *curr = types; // @@@ TODO module -> types
162 while (NULL != curr) {
163 if (id == curr -> type_ident) {
173 type_t *getTypeById (const ident *id)
175 type_t *curr = types; // which ones?
177 while (NULL != curr) {
178 if (id == curr -> id) {
188 entity_t *getEntityByIdents (const ident *name, const ident *tp_ident)
190 entity_t *curr = entities; // TODO module -> entities
192 while (NULL != curr) {
193 if ((name == curr -> ent_ident)
194 && (tp_ident == curr -> tp_ident)) {
204 entity_t *getEntityById (const ident *id)
206 entity_t *curr = entities;
208 while (NULL != curr) {
209 if (id == curr -> id) {
219 proc_t *getEffectByName (const ident *proc_ident)
221 proc_t *curr_effs = procs;
223 while (NULL != curr_effs) {
224 if (proc_ident == curr_effs -> proc_ident) {
227 curr_effs = curr_effs->next;
234 xmlNodePtr get_any_valid_child(xmlNodePtr elem)
238 assert(elem && "no element");
239 child = elem -> xmlChildrenNode;
240 while(child && (NODE_NAME (child, comment))) {
241 child = child -> next;
247 xmlNodePtr get_valid_child(xmlNodePtr elem)
251 child = get_any_valid_child(elem);
252 assert(child && "lost child in deep black forest");
257 * parse XML structure and construct an additional structure
260 parseArg (xmlDocPtr doc, xmlNodePtr argelm)
268 CHECK_NAME (argelm, arg);
269 VERBOSE_PRINT ((stdout, "arg node \t0x%08x\n", (int) argelm));
271 id = getNodeId (argelm);
272 VERBOSE_PRINT ((stdout, "arg->id = \"%s\"\n", id));
273 num_str = (char*) xmlGetProp (argelm, BAD_CAST "number");
274 num = atoi (num_str);
275 VERBOSE_PRINT ((stdout, "arg->no = \"%d\"\n", num));
277 typeid = getNodeTypeStr (argelm);
280 arg -> kind = eff_arg;
281 arg -> id = new_id_from_str(id);
282 arg -> effect.arg.num = num;
283 arg -> effect.arg.type_ident = new_id_from_str(typeid);
289 parseValref (xmlDocPtr doc, xmlNodePtr valelm)
294 CHECK_NAME (valelm, valref);
295 VERBOSE_PRINT ((stdout, "valref node \t0x%08x\n", (int) valelm));
297 ref_id = getNodeRefId (valelm);
298 VERBOSE_PRINT ((stdout, "val->refid = \"%s\"\n", ref_id));
300 valref = NEW (eff_t);
301 valref->kind = eff_valref;
302 valref-> id = new_id_from_str(ref_id);
308 parseSelect (xmlDocPtr doc, xmlNodePtr selelm)
310 const ident *entity_id = new_id_from_str(getNodeEntityStr (selelm));
313 eff_t *valref = NULL;
314 eff_t *sel = NEW (eff_t);
315 sel->kind = eff_select;
317 CHECK_NAME (selelm, select);
318 VERBOSE_PRINT ((stdout, "select node \t0x%08x\n", (int) selelm));
320 ent = getEntityById (entity_id);
321 assert(ent && "entity not found");
322 VERBOSE_PRINT ((stdout, "select entity %s\n", get_id_str(ent -> ent_ident)));
324 child = selelm->xmlChildrenNode;
327 valref = parseValref (doc, child);
330 sel-> id = valref ? valref-> id : NO_ID;
331 sel-> effect.select.ent = ent;
341 parseLoad (xmlDocPtr doc, xmlNodePtr loadelm)
346 eff_t *load = NEW (eff_t);
347 load->kind = eff_load;
349 CHECK_NAME (loadelm, load);
350 VERBOSE_PRINT ((stdout, "load node \t0x%08x\n", (int) loadelm));
351 id = new_id_from_str(getNodeId (loadelm));
353 child = get_valid_child(loadelm);
354 if(NODE_NAME (child, select)) {
355 sel = parseSelect (doc, child);
356 load-> effect.load.ent = sel-> effect.select.ent;
357 VERBOSE_PRINT ((stdout, "load entity \t%s\n",
358 get_id_str(load -> effect.load.ent -> ent_ident)));
361 sel = parseValref (doc, child);
362 load-> effect.load.ent = NULL;
366 load-> effect.load.ptrrefid = sel-> id;
374 parseStore (xmlDocPtr doc, xmlNodePtr storeelm)
379 eff_t *store = NEW (eff_t);
380 store->kind = eff_store;
382 CHECK_NAME (storeelm, store);
383 VERBOSE_PRINT ((stdout, "store node \t0x%08x\n", (int) storeelm));
385 child = get_valid_child(storeelm);
386 if(NODE_NAME (child, select)) {
387 sel = parseSelect (doc, child);
388 store-> effect.store.ent = sel-> effect.select.ent;
391 sel = parseValref (doc, child);
392 store-> effect.store.ent = NULL;
396 valref = parseValref (doc, child);
398 store-> effect.store.ptrrefid = sel-> id;
399 store-> effect.store.valrefid = valref-> id;
408 parseAlloc (xmlDocPtr doc, xmlNodePtr allocelm)
411 const ident *type_id;
412 eff_t *alloc = NEW (eff_t); /* ...! */
413 alloc->kind = eff_alloc;
415 CHECK_NAME (allocelm, alloc);
416 VERBOSE_PRINT ((stdout, "alloc node \t0x%08x\n", (int) allocelm));
417 id = new_id_from_str(getNodeId (allocelm));
418 VERBOSE_PRINT ((stdout, "alloc->id = \"%s\"\n", get_id_str(id)));
419 type_id = new_id_from_str(getNodeTypeId (allocelm));
420 VERBOSE_PRINT ((stdout, "alloc->type_id = \"%s\"\n", get_id_str(type_id)));
423 alloc-> effect.alloc.tp_id = type_id;
429 parseCall (xmlDocPtr doc, xmlNodePtr callelm)
436 eff_t *call = NEW (eff_t);
437 call->kind = eff_call;
439 CHECK_NAME (callelm, call);
440 VERBOSE_PRINT ((stdout, "call node \t0x%08x\n", (int) callelm));
441 id = new_id_from_str(getNodeId (callelm));
442 VERBOSE_PRINT ((stdout, "call->id = \"%s\"\n", get_id_str(id)));
444 child = get_valid_child(callelm);
445 if(NODE_NAME (child, select)) {
446 sel = parseSelect (doc, child);
447 call-> effect.call.ent = sel-> effect.select.ent;
450 sel = parseValref (doc, child);
451 call-> effect.call.ent = NULL;
454 arg = child = child->next;
457 while (NULL != child) {
463 call-> effect.call.valrefid = sel-> id;
464 call-> effect.call.n_args = n_args;
465 call-> effect.call.args = NULL;
470 const ident **args = (const ident**) malloc(n_args * sizeof(const ident*));
473 while (NULL != arg) {
474 eff_t *valref = parseValref (doc, arg);
475 args [i ++] = valref-> id;
480 call-> effect.call.args = args;
487 parseJoin (xmlDocPtr doc, xmlNodePtr joinelm)
494 eff_t *join = NEW (eff_t);
495 join->kind = eff_join;
497 CHECK_NAME (joinelm, join);
498 VERBOSE_PRINT ((stdout, "join node \t0x%08x\n", (int) joinelm));
499 id = new_id_from_str(getNodeId (joinelm));
500 VERBOSE_PRINT ((stdout, "join->id = \"%s\"\n", get_id_str(id)));
502 child = get_valid_child(joinelm);
505 while (NULL != child) {
510 ins = (const ident **) malloc (n_ins * sizeof (const ident *) );
512 child = get_valid_child(joinelm);
514 while (NULL != child) {
515 eff_t *valref = parseValref (doc, child);
516 ins [i ++] = valref-> id;
522 join-> effect.join.n_ins = n_ins;
523 join-> effect.join.ins = ins;
529 parseUnknown (xmlDocPtr doc, xmlNodePtr unknownelm)
532 eff_t *unknown = NEW (eff_t);
533 unknown->kind = eff_unknown;
535 CHECK_NAME (unknownelm, unknown);
536 VERBOSE_PRINT ((stdout, "unknown node \t0x%08x\n", (int) unknownelm));
537 id = new_id_from_str(getNodeId (unknownelm));
544 parseReturn (xmlDocPtr doc, xmlNodePtr retelm)
547 eff_t *ret = NEW (eff_t);
550 CHECK_NAME (retelm, ret);
551 VERBOSE_PRINT ((stdout, "ret node \t0x%08x\n", (int) retelm));
553 child = get_any_valid_child(retelm);
556 eff_t *valref = parseValref (doc, child);
557 ret-> effect.ret.ret_id = valref-> id;
560 ret-> effect.ret.ret_id = NO_ID;
567 parseRaise (xmlDocPtr doc, xmlNodePtr raiseelm)
572 eff_t *raise = NEW (eff_t);
573 raise->kind = eff_raise;
575 CHECK_NAME (raiseelm, raise);
576 VERBOSE_PRINT ((stdout, "raise node \t0x%08x\n", (int) raiseelm));
577 tp_id = getNodeTypeId (raiseelm);
578 VERBOSE_PRINT ((stdout, "raise->type = \"%s\"\n", tp_id));
579 child = get_valid_child(raiseelm);
581 assert (NULL != child);
583 valref = parseValref (doc, child);
584 raise-> effect.raise.valref = valref-> id;
585 raise-> effect.raise.tp_id = new_id_from_str(tp_id);
596 /** parse a type node and insert it into the list */
598 parseType (xmlDocPtr doc, xmlNodePtr typeelm)
601 const char *tp_id = getNodeId (typeelm);
602 VERBOSE_PRINT ((stdout, "type node \t0x%08x (%s)\n", (int) typeelm, tp_id));
603 VERBOSE_PRINT ((stdout, "type = \"%s\"\n", getNodeTypeStr (typeelm)));
605 type = (type_t*) malloc (sizeof (type_t));
606 type -> type_ident = new_id_from_str(getNodeTypeStr (typeelm));
607 type -> id = new_id_from_str(tp_id);
613 /** parse an entity node and insert it into the list */
615 parseEntity (xmlDocPtr doc, xmlNodePtr entelm)
617 entity_t *ent = NEW (entity_t);
620 const char *ent_id = getNodeId (entelm);
621 /* fprintf (stdout, "entity node \t0x%08x (%d)\n", (int) entelm, ent_id); */
622 VERBOSE_PRINT ((stdout, "ent = \"%s.%s\"\n",
623 getNodeTypeStr (entelm),
624 getNodeEntityStr (entelm)));
627 ent -> ent_ident = new_id_from_str (getNodeEntityStr (entelm));
628 ent -> tp_ident = new_id_from_str (getNodeTypeStr (entelm));
629 ent -> owner = new_id_from_str (getNodeOwnerStr (entelm));
630 ent -> id = new_id_from_str(ent_id);
632 ent->prev = entities;
636 /** parse any effect, and turn it into an eff_t (TODO) */
638 parseEffect (xmlDocPtr doc, xmlNodePtr effelm)
641 const char *procname = getNodeProcName (effelm);
642 const char *ownerid = getNodeOwnerStr (effelm);
643 proc_t *curr_effs = NULL;
647 VERBOSE_PRINT ((stdout, "effect for method \"%s\"\n", procname));
649 cur = effelm -> xmlChildrenNode;
650 while (NULL != cur) {
654 VERBOSE_PRINT ((stdout, "has %d effects\n", n_effs));
656 curr_effs = NEW (proc_t);
657 curr_effs -> proc_ident = new_id_from_str(procname);
658 curr_effs -> ownerid = new_id_from_str(ownerid);
659 curr_effs->effs = (eff_t**) malloc (n_effs * sizeof (eff_t*));
661 cur = effelm -> xmlChildrenNode;
662 while (NULL != cur) {
665 if (NODE_NAME (cur, arg)) {
666 eff = (eff_t*) parseArg (doc, cur);
667 } else if (NODE_NAME (cur, load)) {
668 eff = (eff_t*) parseLoad (doc, cur);
669 } else if (NODE_NAME (cur, store)) {
670 eff = (eff_t*) parseStore (doc, cur);
671 } else if (NODE_NAME (cur, alloc)) {
672 eff = (eff_t*) parseAlloc (doc, cur);
673 } else if (NODE_NAME (cur, call)) {
674 eff = (eff_t*) parseCall (doc, cur);
675 } else if (NODE_NAME (cur, join)) {
676 eff = (eff_t*) parseJoin (doc, cur);
677 } else if (NODE_NAME (cur, unknown)) {
678 eff = (eff_t*) parseUnknown (doc, cur);
679 } else if (NODE_NAME (cur, ret)) {
680 eff = (eff_t*) parseReturn (doc, cur);
681 } else if (NODE_NAME (cur, raise)) {
682 eff = (eff_t*) parseRaise (doc, cur);
683 } else if (NODE_NAME (cur, comment)) {
687 fprintf (stderr, "wrong element \"%s\"\n", BAD_CAST cur->name);
691 VERBOSE_PRINT ((stdout, "effect %p@%d\n", (void*)eff, i));
692 curr_effs -> effs[i++] = eff;
696 assert((i == n_effs) && "incorrect number of effects");
697 curr_effs -> n_effs = n_effs;
698 curr_effs -> next = procs;
704 void read_extern (const char *filename)
706 /* xmlNsPtr ns = NULL; */ /* no namespace for us */
707 xmlDocPtr doc; /* whole document */
708 xmlNodePtr cur; /* current node */
712 /* i've got no idea what the VERSION cast is all about. voodoo
713 programming at its finest. */
714 LIBXML_TEST_VERSION xmlKeepBlanksDefault (0);
715 VERBOSE_PRINT((stdout, "read file %s\n", filename));
716 doc = xmlParseFile (filename);
717 CHECK (doc, "xmlParseFile");
719 cur = xmlDocGetRootElement (doc);
720 CHECK (cur, "xmlDocGetRootElement");
722 if (! NODE_NAME (cur, effects)) {
723 fprintf (stderr,"root node \"%s\" != \"effects\"\n", BAD_CAST cur->name);
728 mod_id = getNodeModuleIdent (cur);
729 if (NULL != mod_id) {
730 VERBOSE_PRINT ((stdout, "effects for \"%s\"\n",
731 get_id_str(mod_id)));
734 VERBOSE_PRINT ((stdout, "effects \t0x%08x\n", (int) cur));
738 cur = cur->xmlChildrenNode;
739 while (cur != NULL) {
740 if (NODE_NAME (cur, type)) {
741 parseType (doc, cur);
742 } else if (NODE_NAME (cur, entity)) {
743 parseEntity (doc, cur);
744 } else if (NODE_NAME (cur, effect)) {
745 parseEffect (doc, cur);
746 } else if ((NODE_NAME (cur, comment))) {
749 fprintf (stderr, "wrong element \"%s\"\n", BAD_CAST cur->name);
755 module = NEW(module_t);
756 module -> id = mod_id;
757 module -> types = types;
758 module -> entities = entities;
759 module -> procs = procs;
765 module -> next = modules;
769 /********************************************************************/
772 * free additional structure
775 void freeArg (eff_t *arg)
777 VERBOSE_PRINT ((stdout, "free arg node \t0x%08x\n", (int) arg));
783 void freeValref (eff_t *valref)
785 VERBOSE_PRINT ((stdout, "free valref node \t0x%08x\n", (int) valref));
791 void freeSelect (eff_t *sel)
793 VERBOSE_PRINT ((stdout, "free select node \t0x%08x\n", (int) sel));
799 void freeLoad (eff_t *load)
801 VERBOSE_PRINT ((stdout, "free load node \t0x%08x\n", (int) load));
807 void freeStore (eff_t *store)
809 VERBOSE_PRINT ((stdout, "free store node \t0x%08x\n", (int) store));
815 void freeAlloc (eff_t *alloc)
817 VERBOSE_PRINT ((stdout, "free alloc node \t0x%08x\n", (int) alloc));
823 void freeCall (eff_t *call)
825 VERBOSE_PRINT ((stdout, "free call node \t0x%08x\n", (int) call));
826 free(call -> effect.call.args);
832 void freeJoin (eff_t *join)
834 VERBOSE_PRINT ((stdout, "free join node \t0x%08x\n", (int) join));
835 free(join -> effect.join.ins);
841 void freeUnknown (eff_t *unknown)
843 VERBOSE_PRINT ((stdout, "free unknown node \t0x%08x\n", (int) unknown));
849 void freeReturn (eff_t *ret)
851 VERBOSE_PRINT ((stdout, "free ret node \t0x%08x\n", (int) ret));
857 void freeRaise (eff_t *raise)
859 VERBOSE_PRINT ((stdout, "free raise node \t0x%08x\n", (int) raise));
866 void freeProcEffs(proc_t *proc)
871 VERBOSE_PRINT ((stdout, "free effect for method \"%s\"\n",
872 get_id_str(proc -> proc_ident)));
874 num = proc -> n_effs;
875 for(i = 0; i < num; i++) {
876 switch(proc -> effs[i] -> kind) {
878 freeArg(proc -> effs[i]);
881 freeValref(proc -> effs[i]);
884 freeSelect(proc -> effs[i]);
887 freeLoad(proc -> effs[i]);
890 freeStore(proc -> effs[i]);
893 freeAlloc(proc -> effs[i]);
896 freeCall(proc -> effs[i]);
899 freeUnknown(proc -> effs[i]);
902 freeJoin(proc -> effs[i]);
905 freeRaise(proc -> effs[i]);
908 freeReturn(proc -> effs[i]);
911 assert(0 && "try to free an unknown effect");
920 void freeModuleProcs(module_t *module)
922 proc_t *next_proc, *proc;
924 VERBOSE_PRINT ((stdout, "free procs for module \"%s\"\n",
925 get_id_str(module -> id)));
927 proc = module -> procs;
929 next_proc = proc -> next;
939 module_t *module, *next_module;
943 freeModuleProcs(module);
944 next_module = module -> next;
946 module = next_module;
950 /********************************************************************/
953 type_t *find_type_in_module(module_t *module, const ident *typeid)
957 for(type = module -> types; type; type = type -> prev) {
958 VERBOSE_PRINT((stdout, "test typeid %s\n", get_id_str(type -> id)));
959 if(type -> id == typeid) {
960 VERBOSE_PRINT((stdout, "found\n"));
964 VERBOSE_PRINT((stdout, "did not find type id %s\n", get_id_str(typeid)));
968 /********************************************************************/
970 static void add_value_to_proc(proc_t *proc, eff_t *eff)
972 eff -> next = proc -> values;
973 proc -> values = eff;
977 eff_t *find_valueid_in_proc_effects(const ident *id, proc_t *proc)
981 val = proc -> values;
983 if(id == val -> id) {
991 static void create_abstract_return(ir_graph *irg, proc_t *proc, eff_t *eff)
996 VERBOSE_PRINT((stdout, "create effect:return in %s\n",
997 get_id_str(proc -> proc_ident)));
998 if(NO_ID == eff -> effect.ret.ret_id) {
1000 x = new_Return (get_store(), 0, NULL);
1005 /* return one value */
1006 eff_res = find_valueid_in_proc_effects(eff -> effect.ret.ret_id, proc);
1007 assert(eff_res -> firmnode && "firm in effect not set");
1008 in[0] = eff_res -> firmnode;
1009 x = new_Return (get_store(), 1, in);
1011 eff -> firmnode = x;
1013 /* Now we generated all instructions for this block and all its predecessor
1014 * blocks so we can mature it. (There are not too much.) */
1015 mature_immBlock (get_irg_current_block(irg));
1017 /* This adds the in edge of the end block which originates at the return statement.
1018 * The return node passes controlflow to the end block. */
1019 add_immBlock_pred (get_irg_end_block(irg), x);
1023 static void create_abstract_arg(ir_graph *irg, proc_t *proc, eff_t *eff)
1031 VERBOSE_PRINT((stdout, "create effect:arg %d in %s\n",
1032 eff -> effect.arg.num, get_id_str(proc -> proc_ident)));
1033 ent = get_irg_entity(irg);
1034 typ = get_entity_type(ent);
1036 /* read argument eff -> effect.arg.num and place in values list */
1037 num = get_method_n_params(typ);
1038 assert((num >= eff -> effect.arg.num) && "number too big");
1039 typ = get_method_param_type(typ, eff -> effect.arg.num);
1040 mode = get_type_mode(typ);
1042 arg = new_Proj(get_irg_args(irg), mode, eff -> effect.arg.num);
1043 eff -> firmnode = arg;
1045 add_value_to_proc(proc, eff);
1049 static void create_abstract_load(ir_graph *irg, proc_t *proc, eff_t *eff)
1051 ir_node *sel, *load;
1056 VERBOSE_PRINT((stdout, "create load in %s\n",
1057 get_id_str(proc -> proc_ident)));
1059 if(eff -> effect.load.ent) {
1060 ent = eff -> effect.load.ent -> f_ent;
1061 VERBOSE_PRINT((stdout, "load from %s\n", get_entity_name(ent)));
1064 VERBOSE_PRINT((stdout, "store to memory\n"));
1068 addr = find_valueid_in_proc_effects(eff -> effect.load.ptrrefid, proc);
1069 assert(addr && "no address for load");
1070 /* if addr is Unknown, set proper mode */
1071 if(iro_Unknown == get_irn_opcode(addr -> firmnode)) {
1072 set_irn_mode(addr -> firmnode, mode_P);
1076 sel = new_simpleSel(get_store(), addr -> firmnode, ent);
1077 mode = get_type_mode(get_entity_type(ent));
1080 sel = addr -> firmnode;
1083 load = new_Load(get_store(), sel, mode);
1084 set_store(new_Proj(load, mode_M, 0));
1085 eff -> firmnode = new_Proj(load, mode, 2);
1087 add_value_to_proc(proc, eff);
1091 static void create_abstract_store(ir_graph *irg, proc_t *proc, eff_t *eff)
1093 ir_node *sel, *store;
1097 VERBOSE_PRINT((stdout, "create store in %s\n",
1098 get_id_str(proc -> proc_ident)));
1100 if(eff -> effect.store.ent) {
1101 ent = eff -> effect.store.ent -> f_ent;
1102 VERBOSE_PRINT((stdout, "store to entity %s\n", get_entity_name(ent)));
1105 VERBOSE_PRINT((stdout, "store to memory\n"));
1109 addr = find_valueid_in_proc_effects(eff -> effect.store.ptrrefid, proc);
1110 assert(addr && "no address for store");
1111 /* if addr is Unknown, set propper mode */
1112 if(iro_Unknown == get_irn_opcode(addr -> firmnode)) {
1113 set_irn_mode(addr -> firmnode, mode_P);
1116 val = find_valueid_in_proc_effects(eff -> effect.store.valrefid, proc);
1117 assert(val && "no address for store");
1118 /* if addr is Unknown, set propper mode */
1119 if(iro_Unknown == get_irn_opcode(val -> firmnode)) {
1120 set_irn_mode(val -> firmnode, get_type_mode(get_entity_type(ent)));
1124 sel = new_simpleSel(get_store(), addr -> firmnode, ent);
1127 sel = addr -> firmnode;
1129 store = new_Store(get_store(), sel, val -> firmnode);
1130 set_store(new_Proj(store, mode_M, 0));
1131 eff -> firmnode = store;
1135 static void create_abstract_alloc(ir_graph *irg, proc_t *proc, eff_t *eff)
1140 symconst_symbol sym;
1142 VERBOSE_PRINT((stdout, "create alloc in %s\n",
1143 get_id_str(proc -> proc_ident)));
1145 xtype = find_type_in_module(current_module, eff -> effect.alloc.tp_id);
1146 assert(xtype && "type not found");
1147 ftype = xtype -> f_tp;
1150 alloc = new_Alloc(get_store(), new_SymConst(sym, symconst_size), ftype,
1152 set_store(new_Proj(alloc, mode_M, 0));
1153 eff -> firmnode = new_Proj(alloc, mode_P, 2);
1155 add_value_to_proc(proc, eff);
1159 static void create_abstract_unknown(ir_graph *irg, proc_t *proc, eff_t *eff)
1163 VERBOSE_PRINT((stdout, "create unknown in %s\n",
1164 get_id_str(proc -> proc_ident)));
1166 unknown = new_Unknown(mode_ANY);
1167 eff -> firmnode = unknown;
1169 add_value_to_proc(proc, eff);
1173 static void create_abstract_call(ir_graph *irg, proc_t *proc, eff_t *eff)
1175 ir_node *sel, *call;
1181 int mik; /* is method somehow known? */
1183 VERBOSE_PRINT((stdout, "create call in %s\n",
1184 get_id_str(proc -> proc_ident)));
1186 if(eff -> effect.call.ent) {
1187 ent = eff -> effect.call.ent -> f_ent;
1188 VERBOSE_PRINT((stdout, "call %s\n", get_entity_name(ent)));
1192 VERBOSE_PRINT((stdout, "call something in memory\n"));
1195 addr = find_valueid_in_proc_effects(eff -> effect.call.valrefid, proc);
1196 assert(addr && "no address for load");
1197 /* if addr is Unknown, set propper mode */
1198 if(iro_Unknown == get_irn_opcode(addr -> firmnode)) {
1199 set_irn_mode(addr -> firmnode, mode_P);
1204 sel = new_simpleSel(get_store(), addr -> firmnode, ent);
1206 mtype = get_entity_type(ent);
1211 sel = addr -> firmnode;
1213 mtype = get_unknown_type();
1218 num = eff -> effect.call.n_args;
1219 VERBOSE_PRINT((stdout, "number of args given: %d\n", num));
1221 VERBOSE_PRINT((stdout, "number of args expected: %d\n",
1222 get_method_n_params(mtype)));
1224 irns = alloca(num * sizeof(ir_node*));
1225 for(i = 0; i < num; i++) {
1226 irns[i] = find_valueid_in_proc_effects(eff -> effect.call.args[i], proc)
1228 if(iro_Unknown == get_irn_opcode(irns[i])) {
1230 set_irn_mode(irns[i], get_type_mode(get_method_param_type(mtype, i)));
1233 set_irn_mode(irns[i], mode_ANY);
1237 call = new_Call(get_store(), sel, num, irns, mtype);
1238 set_store(new_Proj(call, mode_M, 0));
1239 if(mik && (0 != get_method_n_ress(mtype))) {
1240 eff -> firmnode = new_Proj(call,
1241 get_type_mode(get_method_res_type(mtype, 0)),
1243 add_value_to_proc(proc, eff); /* result can be accessed */
1246 eff -> firmnode = NULL; /* result can not be accessed */
1250 static void create_abstract_join (ir_graph *irg, proc_t *proc, eff_t *eff)
1252 ir_node **ins = NULL;
1253 ir_node *unknown = NULL;
1254 ir_node *cond = NULL;
1255 ir_node *block = NULL;
1256 ir_node *c_block = NULL;
1257 ir_node *phi = NULL;
1258 ir_mode *join_md = mode_ANY;
1262 VERBOSE_PRINT((stdout, "create join in %s\n",
1263 get_id_str(proc -> proc_ident)));
1265 assert (eff_join == eff->kind);
1267 n_ins = eff->effect.join.n_ins;
1269 /* seems like current_block is not always mature at this point */
1270 mature_immBlock (get_cur_block ());
1272 block = get_cur_block (); /* remember this so we can put the ProjXs into it */
1274 /* jump based on an unknown condition so all values are possible */
1275 unknown = new_Unknown (mode_Iu);
1276 cond = new_Cond (unknown);
1278 c_block = new_immBlock (); /* for the Phi after the branch(es) */
1280 ins = (ir_node**) malloc (n_ins * sizeof (ir_node*));
1281 for (i = 0; i < n_ins; i ++) {
1282 ir_node *projX = NULL;
1283 ir_node *s_block = NULL;
1284 ir_node *jmp = NULL;
1287 /* make sure the projX is in the 'switch' block */
1288 set_cur_block (block);
1289 projX = new_Proj (cond, mode_X, (long) i);
1291 /* this also sets current_block, so the rest of the code ends up there: */
1292 s_block = new_immBlock ();
1294 add_immBlock_pred (s_block, projX);
1295 mature_immBlock (s_block);
1297 in_eff = find_valueid_in_proc_effects (eff->effect.join.ins [i], proc);
1299 ins [i] = in_eff->firmnode;
1301 /* need to find a suitable mode for the Phi node */
1302 if (mode_ANY != get_irn_mode (ins [i])) {
1303 join_md = get_irn_mode (ins [i]);
1307 add_immBlock_pred (c_block, jmp);
1310 set_cur_block (c_block);
1312 phi = new_Phi (n_ins, ins, join_md);
1314 mature_immBlock (c_block);
1315 memset (ins, 0x00, n_ins * sizeof (ir_node*));
1318 eff->firmnode = phi;
1320 add_value_to_proc (proc, eff);
1323 static void create_abstract_raise (ir_graph *irg, proc_t *proc, eff_t *eff)
1325 ir_node *block = NULL;
1326 ir_node *unknown = NULL;
1327 ir_node *cond = NULL;
1329 /* seems like current_block is not always mature at this point */
1330 mature_immBlock (get_cur_block ());
1331 block = get_cur_block (); /* remember this so we can put the ProjXs into it */
1333 /* jump based on an unknown condition so both values are possible */
1334 unknown = new_Unknown (mode_Iu);
1335 cond = new_Cond (unknown);
1337 /* one branch for 'throw-exception' case */
1339 ir_node *projX = new_Proj (cond, mode_X, 1L);
1340 ir_node *b_exc = new_immBlock ();
1341 ir_node *obj = NULL;
1342 ir_node *thrw = NULL;
1343 eff_t *thrw_eff = NULL;
1345 add_immBlock_pred (b_exc, projX);
1347 thrw_eff = find_valueid_in_proc_effects (eff->effect.raise.valref, proc);
1348 obj = thrw_eff->firmnode;
1350 thrw = new_Raise (get_store (), obj);
1351 /* exc-jump to end block */
1352 thrw = new_Proj (thrw, mode_X, 0L);
1354 add_immBlock_pred (get_irg_end_block (irg), thrw);
1355 mature_immBlock (get_cur_block ());
1358 set_cur_block (block); /* back to the first block */
1360 /* one branch for 'non-exception' case */
1362 ir_node *projX = new_Proj (cond, mode_X, 0);
1363 new_immBlock (); /* also sets current_block */
1364 add_immBlock_pred (get_cur_block (), projX);
1365 mature_immBlock (get_cur_block ());
1366 /* continue building in current_block */
1371 static void create_abstract_firm(module_t *module, proc_t *proc, entity *fent)
1378 assert(visibility_external_allocated == get_entity_visibility(fent)
1379 && peculiarity_existent == get_entity_peculiarity(fent)
1380 && "not an abstract entity");
1381 /* create irg in entity */
1382 irg = new_ir_graph(fent, 0);
1383 set_irg_inline_property(irg, irg_inline_forbidden);
1385 VERBOSE_PRINT((stdout, "create effects for %s\n",
1386 get_id_str(proc -> proc_ident)));
1388 /* create effects in irg */
1389 num = proc -> n_effs;
1390 for(i = 0; i < num; i++) {
1391 eff = proc -> effs[i];
1392 VERBOSE_PRINT((stdout,
1393 "create effect \"%s\"\n", effect_string[(int)eff -> kind]));
1394 switch(eff -> kind) {
1396 create_abstract_return(irg, proc, eff);
1399 create_abstract_arg(irg, proc, eff);
1402 create_abstract_load(irg, proc, eff);
1405 create_abstract_store(irg, proc, eff);
1408 create_abstract_unknown(irg, proc, eff);
1411 create_abstract_alloc(irg, proc, eff);
1414 create_abstract_call(irg, proc, eff);
1417 create_abstract_join(irg, proc, eff);
1420 create_abstract_raise(irg, proc, eff);
1423 assert(0 && "effect not implemented");
1428 /* close irg in entity */
1429 /* Now we can mature the end block as all it's predecessors are known. */
1430 mature_immBlock (get_irg_end_block(irg));
1432 /* Verify the graph. Finds some very bad errors in the graph. */
1433 VERBOSE_PRINT((stdout, "verify graph\n"));
1435 VERBOSE_PRINT((stdout, "finalize construction\n"));
1436 finalize_cons (irg);
1439 /********************************************************************/
1441 static void assign_firm_entity(module_t *module, entity_t *xmlent)
1448 VERBOSE_PRINT((stdout, "assign entity %s to typeid %s\n",
1449 get_id_str(xmlent -> ent_ident),
1450 get_id_str(xmlent -> owner)));
1452 typ = find_type_in_module(module, xmlent -> owner);
1453 assert(typ && "class not found in module");
1455 assert(is_class_type(type));
1457 num = get_class_n_members(type);
1459 for(i = 0; i < num; i++) {
1460 ent = get_class_member(type, i);
1461 VERBOSE_PRINT((stdout, "compare entity %s and %s\n",
1462 get_id_str(xmlent -> ent_ident), get_entity_name(ent)));
1464 if(get_entity_ident(ent) == xmlent -> ent_ident) {
1469 assert(ent && "did not find a entity");
1471 xmlent -> f_ent = ent;
1474 /********************************************************************/
1475 /* must be primitive type or class type */
1476 static void assign_firm_type(type_t *xmltype)
1482 VERBOSE_PRINT((stdout, "assign firm type to type %s\n",
1483 get_id_str(xmltype -> type_ident)));
1485 /* is it global type? */
1486 typ = get_glob_type();
1487 if(xmltype -> type_ident == get_type_ident(typ)) {
1489 xmltype -> f_tp = typ;
1490 VERBOSE_PRINT((stdout, "is global type %s\n", get_type_name(typ)));
1492 num = get_irp_n_types();
1493 for(i = 0; i < num; i++) {
1494 typ = get_irp_type(i);
1495 VERBOSE_PRINT((stdout, "test type %s\n", get_type_name(typ)));
1496 if(xmltype -> type_ident == get_type_ident(typ)) {
1497 VERBOSE_PRINT((stdout, "found type %s\n", get_type_name(typ)));
1498 xmltype -> f_tp = typ;
1504 assert(typ && "did not find a type");
1507 /********************************************************************/
1509 void create_abstract_proc_effect(module_t *module, proc_t *proc)
1512 type *class_typ = NULL;
1516 /* find the class of a procedure */
1517 VERBOSE_PRINT((stdout, "do find owner id %s\n", get_id_str(proc -> ownerid)));
1518 type = find_type_in_module(module, proc -> ownerid);
1519 assert(type && "class not found in module");
1521 class_typ = get_glob_type();
1522 VERBOSE_PRINT((stdout, "test type %s\n", get_type_name(class_typ)));
1523 if(type -> type_ident != get_type_ident(class_typ)) {
1524 /* find module as class */
1525 num = get_irp_n_types();
1526 for(i = 0; i < num; i++) {
1527 class_typ = get_irp_type(i);
1528 VERBOSE_PRINT((stdout, "test type %s\n", get_type_name(class_typ)));
1529 if(is_class_type(class_typ)
1530 && (type -> type_ident == get_type_ident(class_typ))) {
1531 /* found class type */
1532 VERBOSE_PRINT((stdout, "found type %s\n", get_type_name(class_typ)));
1539 VERBOSE_PRINT((stdout, "found global type %s\n", get_type_name(class_typ)));
1541 assert(class_typ && "type not found");
1542 assert(is_class_type(class_typ) && "is not a class type");
1543 type -> f_tp = class_typ;
1545 /* find entity for procedure in class */
1546 VERBOSE_PRINT((stdout, "find method %s\n",
1547 get_id_str(proc -> proc_ident)));
1549 num = get_class_n_members(class_typ);
1551 for(i = 0; i < num; i++) {
1552 fent = get_class_member(class_typ, i);
1553 VERBOSE_PRINT((stdout, "test proc %s\n", get_entity_name(fent)));
1554 if(proc -> proc_ident == get_entity_ident(fent)) {
1555 VERBOSE_PRINT((stdout, "found proc %s\n",
1556 get_id_str(proc -> proc_ident)));
1557 /* @@@ TODO check args types - not in xml yet */
1558 /* create Firm stuff */
1559 create_abstract_firm(module, proc, fent);
1569 "method %s not found\nNo effects generated\nCandidates are:\n",
1570 get_id_str(proc -> proc_ident));
1571 for(i = 0; i < num; i++) {
1572 fent = get_class_member(class_typ, i);
1573 fprintf(stderr, "%s\n", get_entity_name(fent));
1575 //assert(fent && "procedure not found in class");
1579 void create_abstract_module(module_t *module)
1585 VERBOSE_PRINT((stdout, "create an abstraction for module %s\n",
1586 get_id_str(module -> id)));
1588 VERBOSE_PRINT((stdout, "--handle types for module\n"));
1589 for(type = module -> types; type; type = type -> prev) {
1590 assign_firm_type(type);
1593 VERBOSE_PRINT((stdout, "--handle entities for module\n"));
1595 for(ent = module -> entities; ent; ent = ent -> prev) {
1596 assign_firm_entity(module, ent);
1599 VERBOSE_PRINT((stdout, "--handle procs for module\n"));
1600 for(proc = module -> procs; proc; proc = proc -> next) {
1601 create_abstract_proc_effect(module, proc);
1606 void create_abstraction(const char *filename)
1610 /* read and parse XML file */
1611 read_extern(filename);
1613 /* finished reading and parsing here */
1614 /* build FIRM graphs */
1617 current_module = module;
1618 create_abstract_module(module);
1619 module = module -> next;
1621 current_module = NULL;
1623 /* free data structures */
1632 /********************************************************************/
1637 * Revision 1.14 2004/11/10 14:42:00 boesler
1638 * be more helpful if a method does not exist
1640 * Revision 1.13 2004/11/05 14:00:53 liekweg
1643 * Revision 1.12 2004/11/02 14:30:31 liekweg
1644 * fixed multi-input join (thx, Boris) --flo
1646 * Revision 1.11 2004/10/29 18:51:53 liekweg
1649 * Revision 1.10 2004/10/25 13:52:24 boesler
1650 * seperated read.h (public interface) and read_t.h (types)
1652 * Revision 1.9 2004/10/22 13:51:35 boesler
1653 * prohibit inlining of pseudo ir_graphs
1655 * Revision 1.8 2004/10/22 13:13:27 boesler
1656 * replaced char* by idents, minor fix in Firm codegen for call
1658 * Revision 1.7 2004/10/21 15:31:55 boesler
1659 * added lots of stuff:
1660 * - build abstract syntax trees
1661 * - build Firm graphs for many effects, still todos
1663 * Revision 1.5 2004/10/18 12:48:20 liekweg
1666 * Revision 1.4 2004/10/14 11:31:53 liekweg
1669 * Revision 1.3 2004/10/13 13:36:28 rubino
1672 * Revision 1.2 2004/10/11 15:56:09 liekweg
1673 * Cleanup, comments ...
1674 * Added init func --flo
1676 * Revision 1.1 2004/10/11 09:31:06 liekweg
1677 * First Import of XML reading procs --flo