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 */
33 #include "pseudo_irg.h"
41 #define VERBOSE_PRINTING 0
44 # define VERBOSE_PRINT(s) fprintf s
46 # define VERBOSE_PRINT(s)
51 static type_t *types = NULL;
52 static entity_t *entities = NULL;
53 static proc_t *procs = NULL;
54 static module_t *modules = NULL;
57 static module_t *current_module = NULL;
60 /* this is only used inside a VERBOSE_PRINT() call */
61 static char *effect_string[] = {
74 #endif /* defined VERBOSE_PRINTING */
77 getNodeModuleIdent (xmlNodePtr node)
79 const char *mod_str = (const char*) xmlGetProp (node, BAD_CAST "module");
81 if (NULL == mod_str) {
84 const ident *res = new_id_from_str (mod_str);
90 getNodeProcName (xmlNodePtr node)
92 const char *proc_str = (const char*) xmlGetProp (node, BAD_CAST "procname");
98 getNodeClassName (xmlNodePtr node)
100 char *proc_str = (char*) xmlGetProp (node, BAD_CAST "class");
102 return ( (proc_str));
106 getNodeId (xmlNodePtr node)
108 const char *id_str = (const char*) xmlGetProp (node, BAD_CAST "id");
114 getNodeRefId (xmlNodePtr node)
116 const char *refid_str = (char*) xmlGetProp (node, BAD_CAST "refid");
118 return ((refid_str));
122 getNodeTypeId (xmlNodePtr node)
124 const char *type_str = (char*) xmlGetProp (node, BAD_CAST "type");
130 *getNodeTypeStr (xmlNodePtr node)
132 const char *type_str = (char*) xmlGetProp (node, BAD_CAST "type");
138 getNodeOwnerStr (xmlNodePtr node)
140 const char *owner_str = (char*) xmlGetProp (node, BAD_CAST "owner");
146 *getNodeEntityStr (xmlNodePtr node)
148 const char *ent_str = (char*) xmlGetProp (node, BAD_CAST "entity");
159 type_t *getTypeByIdent (const ident *id)
161 type_t *curr = types; // @@@ TODO module -> types
163 while (NULL != curr) {
164 if (id == curr -> type_ident) {
174 type_t *getTypeById (const ident *id)
176 type_t *curr = types; // which ones?
178 while (NULL != curr) {
179 if (id == curr -> id) {
189 entity_t *getEntityByIdents (const ident *name, const ident *tp_ident)
191 entity_t *curr = entities; // TODO module -> entities
193 while (NULL != curr) {
194 if ((name == curr -> ent_ident)
195 && (tp_ident == curr -> tp_ident)) {
205 entity_t *getEntityById (const ident *id)
207 entity_t *curr = entities;
209 while (NULL != curr) {
210 if (id == curr -> id) {
220 proc_t *getEffectByName (const ident *proc_ident)
222 proc_t *curr_effs = procs;
224 while (NULL != curr_effs) {
225 if (proc_ident == curr_effs -> proc_ident) {
228 curr_effs = curr_effs->next;
235 xmlNodePtr get_any_valid_child(xmlNodePtr elem)
239 assert(elem && "no element");
240 child = elem -> xmlChildrenNode;
241 while(child && (NODE_NAME (child, comment))) {
242 child = child -> next;
248 xmlNodePtr get_valid_child(xmlNodePtr elem)
252 child = get_any_valid_child(elem);
253 assert(child && "lost child in deep black forest");
258 * parse XML structure and construct an additional structure
261 parseArg (xmlDocPtr doc, xmlNodePtr argelm)
269 CHECK_NAME (argelm, arg);
270 VERBOSE_PRINT ((stdout, "arg node \t0x%08x\n", (int) argelm));
272 id = getNodeId (argelm);
273 VERBOSE_PRINT ((stdout, "arg->id = \"%s\"\n", id));
274 num_str = (char*) xmlGetProp (argelm, BAD_CAST "number");
275 num = atoi (num_str);
276 VERBOSE_PRINT ((stdout, "arg->no = \"%d\"\n", num));
278 typeid = getNodeTypeStr (argelm);
281 arg -> kind = eff_arg;
282 arg -> id = new_id_from_str(id);
283 arg -> effect.arg.num = num;
284 arg -> effect.arg.type_ident = new_id_from_str(typeid);
290 parseValref (xmlDocPtr doc, xmlNodePtr valelm)
295 CHECK_NAME (valelm, valref);
296 VERBOSE_PRINT ((stdout, "valref node \t0x%08x\n", (int) valelm));
298 ref_id = getNodeRefId (valelm);
299 VERBOSE_PRINT ((stdout, "val->refid = \"%s\"\n", ref_id));
301 valref = NEW (eff_t);
302 valref->kind = eff_valref;
303 valref-> id = new_id_from_str(ref_id);
309 parseSelect (xmlDocPtr doc, xmlNodePtr selelm)
311 const ident *entity_id = new_id_from_str(getNodeEntityStr (selelm));
314 eff_t *valref = NULL;
315 eff_t *sel = NEW (eff_t);
316 sel->kind = eff_select;
318 CHECK_NAME (selelm, select);
319 VERBOSE_PRINT ((stdout, "select node \t0x%08x\n", (int) selelm));
321 ent = getEntityById (entity_id);
322 assert(ent && "entity not found");
323 VERBOSE_PRINT ((stdout, "select entity %s\n", get_id_str(ent -> ent_ident)));
325 child = selelm->xmlChildrenNode;
328 valref = parseValref (doc, child);
331 sel-> id = valref ? valref-> id : NO_ID;
332 sel-> effect.select.ent = ent;
342 parseLoad (xmlDocPtr doc, xmlNodePtr loadelm)
347 eff_t *load = NEW (eff_t);
348 load->kind = eff_load;
350 CHECK_NAME (loadelm, load);
351 VERBOSE_PRINT ((stdout, "load node \t0x%08x\n", (int) loadelm));
352 id = new_id_from_str(getNodeId (loadelm));
354 child = get_valid_child(loadelm);
355 if(NODE_NAME (child, select)) {
356 sel = parseSelect (doc, child);
357 load-> effect.load.ent = sel-> effect.select.ent;
358 VERBOSE_PRINT ((stdout, "load entity \t%s\n",
359 get_id_str(load -> effect.load.ent -> ent_ident)));
362 sel = parseValref (doc, child);
363 load-> effect.load.ent = NULL;
367 load-> effect.load.ptrrefid = sel-> id;
375 parseStore (xmlDocPtr doc, xmlNodePtr storeelm)
380 eff_t *store = NEW (eff_t);
381 store->kind = eff_store;
383 CHECK_NAME (storeelm, store);
384 VERBOSE_PRINT ((stdout, "store node \t0x%08x\n", (int) storeelm));
386 child = get_valid_child(storeelm);
387 if(NODE_NAME (child, select)) {
388 sel = parseSelect (doc, child);
389 store-> effect.store.ent = sel-> effect.select.ent;
392 sel = parseValref (doc, child);
393 store-> effect.store.ent = NULL;
397 valref = parseValref (doc, child);
399 store-> effect.store.ptrrefid = sel-> id;
400 store-> effect.store.valrefid = valref-> id;
409 parseAlloc (xmlDocPtr doc, xmlNodePtr allocelm)
412 const ident *type_id;
413 eff_t *alloc = NEW (eff_t); /* ...! */
414 alloc->kind = eff_alloc;
416 CHECK_NAME (allocelm, alloc);
417 VERBOSE_PRINT ((stdout, "alloc node \t0x%08x\n", (int) allocelm));
418 id = new_id_from_str(getNodeId (allocelm));
419 VERBOSE_PRINT ((stdout, "alloc->id = \"%s\"\n", get_id_str(id)));
420 type_id = new_id_from_str(getNodeTypeId (allocelm));
421 VERBOSE_PRINT ((stdout, "alloc->type_id = \"%s\"\n", get_id_str(type_id)));
424 alloc-> effect.alloc.tp_id = type_id;
430 parseCall (xmlDocPtr doc, xmlNodePtr callelm)
437 eff_t *call = NEW (eff_t);
438 call->kind = eff_call;
440 CHECK_NAME (callelm, call);
441 VERBOSE_PRINT ((stdout, "call node \t0x%08x\n", (int) callelm));
442 id = new_id_from_str(getNodeId (callelm));
443 VERBOSE_PRINT ((stdout, "call->id = \"%s\"\n", get_id_str(id)));
445 child = get_valid_child(callelm);
446 if(NODE_NAME (child, select)) {
447 sel = parseSelect (doc, child);
448 call-> effect.call.ent = sel-> effect.select.ent;
451 sel = parseValref (doc, child);
452 call-> effect.call.ent = NULL;
455 arg = child = child->next;
458 while (NULL != child) {
464 call-> effect.call.valrefid = sel-> id;
465 call-> effect.call.n_args = n_args;
466 call-> effect.call.args = NULL;
471 const ident **args = (const ident**) malloc(n_args * sizeof(const ident*));
474 while (NULL != arg) {
475 eff_t *valref = parseValref (doc, arg);
476 args [i ++] = valref-> id;
481 call-> effect.call.args = args;
488 parseJoin (xmlDocPtr doc, xmlNodePtr joinelm)
495 eff_t *join = NEW (eff_t);
496 join->kind = eff_join;
498 CHECK_NAME (joinelm, join);
499 VERBOSE_PRINT ((stdout, "join node \t0x%08x\n", (int) joinelm));
500 id = new_id_from_str(getNodeId (joinelm));
501 VERBOSE_PRINT ((stdout, "join->id = \"%s\"\n", get_id_str(id)));
503 child = get_valid_child(joinelm);
506 while (NULL != child) {
511 ins = (const ident **) malloc (n_ins * sizeof (const ident *) );
513 child = get_valid_child(joinelm);
515 while (NULL != child) {
516 eff_t *valref = parseValref (doc, child);
517 ins [i ++] = valref-> id;
523 join-> effect.join.n_ins = n_ins;
524 join-> effect.join.ins = ins;
530 parseUnknown (xmlDocPtr doc, xmlNodePtr unknownelm)
533 eff_t *unknown = NEW (eff_t);
534 unknown->kind = eff_unknown;
536 CHECK_NAME (unknownelm, unknown);
537 VERBOSE_PRINT ((stdout, "unknown node \t0x%08x\n", (int) unknownelm));
538 id = new_id_from_str(getNodeId (unknownelm));
545 parseReturn (xmlDocPtr doc, xmlNodePtr retelm)
548 eff_t *ret = NEW (eff_t);
551 CHECK_NAME (retelm, ret);
552 VERBOSE_PRINT ((stdout, "ret node \t0x%08x\n", (int) retelm));
554 child = get_any_valid_child(retelm);
557 eff_t *valref = parseValref (doc, child);
558 ret-> effect.ret.ret_id = valref-> id;
561 ret-> effect.ret.ret_id = NO_ID;
568 parseRaise (xmlDocPtr doc, xmlNodePtr raiseelm)
573 eff_t *raise = NEW (eff_t);
574 raise->kind = eff_raise;
576 CHECK_NAME (raiseelm, raise);
577 VERBOSE_PRINT ((stdout, "raise node \t0x%08x\n", (int) raiseelm));
578 tp_id = getNodeTypeId (raiseelm);
579 VERBOSE_PRINT ((stdout, "raise->type = \"%s\"\n", tp_id));
580 child = get_valid_child(raiseelm);
582 assert (NULL != child);
584 valref = parseValref (doc, child);
585 raise-> effect.raise.valref = valref-> id;
586 raise-> effect.raise.tp_id = new_id_from_str(tp_id);
597 /** parse a type node and insert it into the list */
599 parseType (xmlDocPtr doc, xmlNodePtr typeelm)
602 const char *tp_id = getNodeId (typeelm);
603 VERBOSE_PRINT ((stdout, "type node \t0x%08x (%s)\n", (int) typeelm, tp_id));
604 VERBOSE_PRINT ((stdout, "type = \"%s\"\n", getNodeTypeStr (typeelm)));
606 type = (type_t*) malloc (sizeof (type_t));
607 type -> type_ident = new_id_from_str(getNodeTypeStr (typeelm));
608 type -> id = new_id_from_str(tp_id);
614 /** parse an entity node and insert it into the list */
616 parseEntity (xmlDocPtr doc, xmlNodePtr entelm)
618 entity_t *ent = NEW (entity_t);
621 const char *ent_id = getNodeId (entelm);
622 /* fprintf (stdout, "entity node \t0x%08x (%d)\n", (int) entelm, ent_id); */
623 VERBOSE_PRINT ((stdout, "ent = \"%s.%s\"\n",
624 getNodeTypeStr (entelm),
625 getNodeEntityStr (entelm)));
628 ent -> ent_ident = new_id_from_str (getNodeEntityStr (entelm));
629 ent -> tp_ident = new_id_from_str (getNodeTypeStr (entelm));
630 ent -> owner = new_id_from_str (getNodeOwnerStr (entelm));
631 ent -> id = new_id_from_str(ent_id);
633 ent->prev = entities;
637 /** parse any effect, and turn it into an eff_t (TODO) */
639 parseEffect (xmlDocPtr doc, xmlNodePtr effelm)
642 const char *procname = getNodeProcName (effelm);
643 const char *ownerid = getNodeOwnerStr (effelm);
644 proc_t *curr_effs = NULL;
648 VERBOSE_PRINT ((stdout, "effect for method \"%s\"\n", procname));
650 cur = effelm -> xmlChildrenNode;
651 while (NULL != cur) {
655 VERBOSE_PRINT ((stdout, "has %d effects\n", n_effs));
657 curr_effs = NEW (proc_t);
658 curr_effs -> proc_ident = new_id_from_str(procname);
659 curr_effs -> ownerid = new_id_from_str(ownerid);
660 curr_effs->effs = (eff_t**) malloc (n_effs * sizeof (eff_t*));
662 cur = effelm -> xmlChildrenNode;
663 while (NULL != cur) {
666 if (NODE_NAME (cur, arg)) {
667 eff = (eff_t*) parseArg (doc, cur);
668 } else if (NODE_NAME (cur, load)) {
669 eff = (eff_t*) parseLoad (doc, cur);
670 } else if (NODE_NAME (cur, store)) {
671 eff = (eff_t*) parseStore (doc, cur);
672 } else if (NODE_NAME (cur, alloc)) {
673 eff = (eff_t*) parseAlloc (doc, cur);
674 } else if (NODE_NAME (cur, call)) {
675 eff = (eff_t*) parseCall (doc, cur);
676 } else if (NODE_NAME (cur, join)) {
677 eff = (eff_t*) parseJoin (doc, cur);
678 } else if (NODE_NAME (cur, unknown)) {
679 eff = (eff_t*) parseUnknown (doc, cur);
680 } else if (NODE_NAME (cur, ret)) {
681 eff = (eff_t*) parseReturn (doc, cur);
682 } else if (NODE_NAME (cur, raise)) {
683 eff = (eff_t*) parseRaise (doc, cur);
684 } else if (NODE_NAME (cur, comment)) {
688 fprintf (stderr, "wrong element \"%s\"\n", BAD_CAST cur->name);
692 VERBOSE_PRINT ((stdout, "effect %p@%d\n", (void*)eff, i));
693 curr_effs -> effs[i++] = eff;
697 assert((i == n_effs) && "incorrect number of effects");
698 curr_effs -> n_effs = n_effs;
699 curr_effs -> next = procs;
705 void read_extern (const char *filename)
707 /* xmlNsPtr ns = NULL; */ /* no namespace for us */
708 xmlDocPtr doc; /* whole document */
709 xmlNodePtr cur; /* current node */
713 /* i've got no idea what the VERSION cast is all about. voodoo
714 programming at its finest. */
715 LIBXML_TEST_VERSION xmlKeepBlanksDefault (0);
716 VERBOSE_PRINT((stdout, "read file %s\n", filename));
717 doc = xmlParseFile (filename);
718 CHECK (doc, "xmlParseFile");
720 cur = xmlDocGetRootElement (doc);
721 CHECK (cur, "xmlDocGetRootElement");
723 if (! NODE_NAME (cur, effects)) {
724 fprintf (stderr,"root node \"%s\" != \"effects\"\n", BAD_CAST cur->name);
729 mod_id = getNodeModuleIdent (cur);
730 if (NULL != mod_id) {
731 VERBOSE_PRINT ((stdout, "effects for \"%s\"\n",
732 get_id_str(mod_id)));
735 VERBOSE_PRINT ((stdout, "effects \t0x%08x\n", (int) cur));
739 cur = cur->xmlChildrenNode;
740 while (cur != NULL) {
741 if (NODE_NAME (cur, type)) {
742 parseType (doc, cur);
743 } else if (NODE_NAME (cur, entity)) {
744 parseEntity (doc, cur);
745 } else if (NODE_NAME (cur, effect)) {
746 parseEffect (doc, cur);
747 } else if ((NODE_NAME (cur, comment))) {
750 fprintf (stderr, "wrong element \"%s\"\n", BAD_CAST cur->name);
756 module = NEW(module_t);
757 module -> id = mod_id;
758 module -> types = types;
759 module -> entities = entities;
760 module -> procs = procs;
766 module -> next = modules;
770 /********************************************************************/
773 * free additional structure
776 void freeArg (eff_t *arg)
778 VERBOSE_PRINT ((stdout, "free arg node \t0x%08x\n", (int) arg));
784 void freeValref (eff_t *valref)
786 VERBOSE_PRINT ((stdout, "free valref node \t0x%08x\n", (int) valref));
792 void freeSelect (eff_t *sel)
794 VERBOSE_PRINT ((stdout, "free select node \t0x%08x\n", (int) sel));
800 void freeLoad (eff_t *load)
802 VERBOSE_PRINT ((stdout, "free load node \t0x%08x\n", (int) load));
808 void freeStore (eff_t *store)
810 VERBOSE_PRINT ((stdout, "free store node \t0x%08x\n", (int) store));
816 void freeAlloc (eff_t *alloc)
818 VERBOSE_PRINT ((stdout, "free alloc node \t0x%08x\n", (int) alloc));
824 void freeCall (eff_t *call)
826 VERBOSE_PRINT ((stdout, "free call node \t0x%08x\n", (int) call));
827 free(call -> effect.call.args);
833 void freeJoin (eff_t *join)
835 VERBOSE_PRINT ((stdout, "free join node \t0x%08x\n", (int) join));
836 free(join -> effect.join.ins);
842 void freeUnknown (eff_t *unknown)
844 VERBOSE_PRINT ((stdout, "free unknown node \t0x%08x\n", (int) unknown));
850 void freeReturn (eff_t *ret)
852 VERBOSE_PRINT ((stdout, "free ret node \t0x%08x\n", (int) ret));
858 void freeRaise (eff_t *raise)
860 VERBOSE_PRINT ((stdout, "free raise node \t0x%08x\n", (int) raise));
867 void freeProcEffs(proc_t *proc)
872 VERBOSE_PRINT ((stdout, "free effect for method \"%s\"\n",
873 get_id_str(proc -> proc_ident)));
875 num = proc -> n_effs;
876 for(i = 0; i < num; i++) {
877 switch(proc -> effs[i] -> kind) {
879 freeArg(proc -> effs[i]);
882 freeValref(proc -> effs[i]);
885 freeSelect(proc -> effs[i]);
888 freeLoad(proc -> effs[i]);
891 freeStore(proc -> effs[i]);
894 freeAlloc(proc -> effs[i]);
897 freeCall(proc -> effs[i]);
900 freeUnknown(proc -> effs[i]);
903 freeJoin(proc -> effs[i]);
906 freeRaise(proc -> effs[i]);
909 freeReturn(proc -> effs[i]);
912 assert(0 && "try to free an unknown effect");
921 void freeModuleProcs(module_t *module)
923 proc_t *next_proc, *proc;
925 VERBOSE_PRINT ((stdout, "free procs for module \"%s\"\n",
926 get_id_str(module -> id)));
928 proc = module -> procs;
930 next_proc = proc -> next;
940 module_t *module, *next_module;
944 freeModuleProcs(module);
945 next_module = module -> next;
947 module = next_module;
951 /********************************************************************/
954 type_t *find_type_in_module(module_t *module, const ident *typeid)
958 for(type = module -> types; type; type = type -> prev) {
959 VERBOSE_PRINT((stdout, "test typeid %s\n", get_id_str(type -> id)));
960 if(type -> id == typeid) {
961 VERBOSE_PRINT((stdout, "found\n"));
965 VERBOSE_PRINT((stdout, "did not find type id %s\n", get_id_str(typeid)));
969 /********************************************************************/
971 static void add_value_to_proc(proc_t *proc, eff_t *eff)
973 eff -> next = proc -> values;
974 proc -> values = eff;
978 eff_t *find_valueid_in_proc_effects(const ident *id, proc_t *proc)
982 val = proc -> values;
984 if(id == val -> id) {
992 static void create_abstract_return(ir_graph *irg, proc_t *proc, eff_t *eff)
997 VERBOSE_PRINT((stdout, "create effect:return in %s\n",
998 get_id_str(proc -> proc_ident)));
999 if(NO_ID == eff -> effect.ret.ret_id) {
1001 x = new_Return (get_store(), 0, NULL);
1006 /* return one value */
1007 eff_res = find_valueid_in_proc_effects(eff -> effect.ret.ret_id, proc);
1008 assert(eff_res -> firmnode && "firm in effect not set");
1009 in[0] = eff_res -> firmnode;
1010 x = new_Return (get_store(), 1, in);
1012 eff -> firmnode = x;
1014 /* Now we generated all instructions for this block and all its predecessor
1015 * blocks so we can mature it. (There are not too much.) */
1016 mature_immBlock (get_irg_current_block(irg));
1018 /* This adds the in edge of the end block which originates at the return statement.
1019 * The return node passes controlflow to the end block. */
1020 add_immBlock_pred (get_irg_end_block(irg), x);
1024 static void create_abstract_arg(ir_graph *irg, proc_t *proc, eff_t *eff)
1032 VERBOSE_PRINT((stdout, "create effect:arg %d in %s\n",
1033 eff -> effect.arg.num, get_id_str(proc -> proc_ident)));
1034 ent = get_irg_entity(irg);
1035 typ = get_entity_type(ent);
1037 /* read argument eff -> effect.arg.num and place in values list */
1038 num = get_method_n_params(typ);
1039 assert((num >= eff -> effect.arg.num) && "number too big");
1040 typ = get_method_param_type(typ, eff -> effect.arg.num);
1041 mode = get_type_mode(typ);
1043 arg = new_Proj(get_irg_args(irg), mode, eff -> effect.arg.num);
1044 eff -> firmnode = arg;
1046 add_value_to_proc(proc, eff);
1050 static void create_abstract_load(ir_graph *irg, proc_t *proc, eff_t *eff)
1052 ir_node *sel, *load;
1057 VERBOSE_PRINT((stdout, "create load in %s\n",
1058 get_id_str(proc -> proc_ident)));
1060 if(eff -> effect.load.ent) {
1061 ent = eff -> effect.load.ent -> f_ent;
1062 VERBOSE_PRINT((stdout, "load from %s\n", get_entity_name(ent)));
1065 VERBOSE_PRINT((stdout, "store to memory\n"));
1069 addr = find_valueid_in_proc_effects(eff -> effect.load.ptrrefid, proc);
1070 assert(addr && "no address for load");
1071 /* if addr is Unknown, set proper mode */
1072 if(iro_Unknown == get_irn_opcode(addr -> firmnode)) {
1073 set_irn_mode(addr -> firmnode, mode_P);
1077 sel = new_simpleSel(get_store(), addr -> firmnode, ent);
1078 mode = get_type_mode(get_entity_type(ent));
1081 sel = addr -> firmnode;
1084 load = new_Load(get_store(), sel, mode);
1085 set_store(new_Proj(load, mode_M, 0));
1086 eff -> firmnode = new_Proj(load, mode, 2);
1088 add_value_to_proc(proc, eff);
1092 static void create_abstract_store(ir_graph *irg, proc_t *proc, eff_t *eff)
1094 ir_node *sel, *store;
1098 VERBOSE_PRINT((stdout, "create store in %s\n",
1099 get_id_str(proc -> proc_ident)));
1101 if(eff -> effect.store.ent) {
1102 ent = eff -> effect.store.ent -> f_ent;
1103 VERBOSE_PRINT((stdout, "store to entity %s\n", get_entity_name(ent)));
1106 VERBOSE_PRINT((stdout, "store to memory\n"));
1110 addr = find_valueid_in_proc_effects(eff -> effect.store.ptrrefid, proc);
1111 assert(addr && "no address for store");
1112 /* if addr is Unknown, set propper mode */
1113 if(iro_Unknown == get_irn_opcode(addr -> firmnode)) {
1114 set_irn_mode(addr -> firmnode, mode_P);
1117 val = find_valueid_in_proc_effects(eff -> effect.store.valrefid, proc);
1118 assert(val && "no address for store");
1119 /* if addr is Unknown, set propper mode */
1120 if(iro_Unknown == get_irn_opcode(val -> firmnode)) {
1121 set_irn_mode(val -> firmnode, get_type_mode(get_entity_type(ent)));
1125 sel = new_simpleSel(get_store(), addr -> firmnode, ent);
1128 sel = addr -> firmnode;
1130 store = new_Store(get_store(), sel, val -> firmnode);
1131 set_store(new_Proj(store, mode_M, 0));
1132 eff -> firmnode = store;
1136 static void create_abstract_alloc(ir_graph *irg, proc_t *proc, eff_t *eff)
1141 symconst_symbol sym;
1143 VERBOSE_PRINT((stdout, "create alloc in %s\n",
1144 get_id_str(proc -> proc_ident)));
1146 xtype = find_type_in_module(current_module, eff -> effect.alloc.tp_id);
1147 assert(xtype && "type not found");
1148 ftype = xtype -> f_tp;
1151 alloc = new_Alloc(get_store(), new_SymConst(sym, symconst_size), ftype,
1153 set_store(new_Proj(alloc, mode_M, 0));
1154 eff -> firmnode = new_Proj(alloc, mode_P, 2);
1156 add_value_to_proc(proc, eff);
1160 static void create_abstract_unknown(ir_graph *irg, proc_t *proc, eff_t *eff)
1164 VERBOSE_PRINT((stdout, "create unknown in %s\n",
1165 get_id_str(proc -> proc_ident)));
1167 unknown = new_Unknown(mode_ANY);
1168 eff -> firmnode = unknown;
1170 add_value_to_proc(proc, eff);
1174 static void create_abstract_call(ir_graph *irg, proc_t *proc, eff_t *eff)
1176 ir_node *sel, *call;
1182 int mik; /* is method somehow known? */
1184 VERBOSE_PRINT((stdout, "create call in %s\n",
1185 get_id_str(proc -> proc_ident)));
1187 if(eff -> effect.call.ent) {
1188 ent = eff -> effect.call.ent -> f_ent;
1189 VERBOSE_PRINT((stdout, "call %s\n", get_entity_name(ent)));
1193 VERBOSE_PRINT((stdout, "call something in memory\n"));
1196 addr = find_valueid_in_proc_effects(eff -> effect.call.valrefid, proc);
1197 assert(addr && "no address for load");
1198 /* if addr is Unknown, set propper mode */
1199 if(iro_Unknown == get_irn_opcode(addr -> firmnode)) {
1200 set_irn_mode(addr -> firmnode, mode_P);
1205 sel = new_simpleSel(get_store(), addr -> firmnode, ent);
1207 mtype = get_entity_type(ent);
1212 sel = addr -> firmnode;
1214 mtype = get_unknown_type();
1219 num = eff -> effect.call.n_args;
1220 VERBOSE_PRINT((stdout, "number of args given: %d\n", num));
1222 VERBOSE_PRINT((stdout, "number of args expected: %d\n",
1223 get_method_n_params(mtype)));
1225 irns = alloca(num * sizeof(ir_node*));
1226 for(i = 0; i < num; i++) {
1227 irns[i] = find_valueid_in_proc_effects(eff -> effect.call.args[i], proc)
1229 if(iro_Unknown == get_irn_opcode(irns[i])) {
1231 set_irn_mode(irns[i], get_type_mode(get_method_param_type(mtype, i)));
1234 set_irn_mode(irns[i], mode_ANY);
1238 call = new_Call(get_store(), sel, num, irns, mtype);
1239 set_store(new_Proj(call, mode_M, 0));
1240 if(mik && (0 != get_method_n_ress(mtype))) {
1241 eff -> firmnode = new_Proj(call,
1242 get_type_mode(get_method_res_type(mtype, 0)),
1244 add_value_to_proc(proc, eff); /* result can be accessed */
1247 eff -> firmnode = NULL; /* result can not be accessed */
1251 static void create_abstract_join (ir_graph *irg, proc_t *proc, eff_t *eff)
1253 ir_node **ins = NULL;
1254 ir_node *unknown = NULL;
1255 ir_node *cond = NULL;
1256 ir_node *block = NULL;
1257 ir_node *c_block = NULL;
1258 ir_node *phi = NULL;
1259 ir_mode *join_md = mode_ANY;
1263 VERBOSE_PRINT((stdout, "create join in %s\n",
1264 get_id_str(proc -> proc_ident)));
1266 assert (eff_join == eff->kind);
1268 n_ins = eff->effect.join.n_ins;
1270 /* seems like current_block is not always mature at this point */
1271 mature_immBlock (get_cur_block ());
1273 block = get_cur_block (); /* remember this so we can put the ProjXs into it */
1275 /* jump based on an unknown condition so all values are possible */
1276 unknown = new_Unknown (mode_Iu);
1277 cond = new_Cond (unknown);
1279 c_block = new_immBlock (); /* for the Phi after the branch(es) */
1281 ins = (ir_node**) malloc (n_ins * sizeof (ir_node*));
1282 for (i = 0; i < n_ins; i ++) {
1283 ir_node *projX = NULL;
1284 ir_node *s_block = NULL;
1285 ir_node *jmp = NULL;
1288 /* make sure the projX is in the 'switch' block */
1289 set_cur_block (block);
1290 projX = new_Proj (cond, mode_X, (long) i);
1292 /* this also sets current_block, so the rest of the code ends up there: */
1293 s_block = new_immBlock ();
1295 add_immBlock_pred (s_block, projX);
1296 mature_immBlock (s_block);
1298 in_eff = find_valueid_in_proc_effects (eff->effect.join.ins [i], proc);
1300 ins [i] = in_eff->firmnode;
1302 /* need to find a suitable mode for the Phi node */
1303 if (mode_ANY != get_irn_mode (ins [i])) {
1304 join_md = get_irn_mode (ins [i]);
1308 add_immBlock_pred (c_block, jmp);
1311 set_cur_block (c_block);
1313 phi = new_Phi (n_ins, ins, join_md);
1315 mature_immBlock (c_block);
1316 memset (ins, 0x00, n_ins * sizeof (ir_node*));
1319 eff->firmnode = phi;
1321 add_value_to_proc (proc, eff);
1324 static void create_abstract_raise (ir_graph *irg, proc_t *proc, eff_t *eff)
1326 ir_node *block = NULL;
1327 ir_node *unknown = NULL;
1328 ir_node *cond = NULL;
1330 /* seems like current_block is not always mature at this point */
1331 mature_immBlock (get_cur_block ());
1332 block = get_cur_block (); /* remember this so we can put the ProjXs into it */
1334 /* jump based on an unknown condition so both values are possible */
1335 unknown = new_Unknown (mode_Iu);
1336 cond = new_Cond (unknown);
1338 /* one branch for 'throw-exception' case */
1340 ir_node *projX = new_Proj (cond, mode_X, 1L);
1341 ir_node *b_exc = new_immBlock ();
1342 ir_node *obj = NULL;
1343 ir_node *thrw = NULL;
1344 eff_t *thrw_eff = NULL;
1346 add_immBlock_pred (b_exc, projX);
1348 thrw_eff = find_valueid_in_proc_effects (eff->effect.raise.valref, proc);
1349 obj = thrw_eff->firmnode;
1351 thrw = new_Raise (get_store (), obj);
1352 /* exc-jump to end block */
1353 thrw = new_Proj (thrw, mode_X, 0L);
1355 add_immBlock_pred (get_irg_end_block (irg), thrw);
1356 mature_immBlock (get_cur_block ());
1359 set_cur_block (block); /* back to the first block */
1361 /* one branch for 'non-exception' case */
1363 ir_node *projX = new_Proj (cond, mode_X, 0);
1364 new_immBlock (); /* also sets current_block */
1365 add_immBlock_pred (get_cur_block (), projX);
1366 mature_immBlock (get_cur_block ());
1367 /* continue building in current_block */
1372 static void create_abstract_firm(module_t *module, proc_t *proc, entity *fent)
1379 assert(visibility_external_allocated == get_entity_visibility(fent)
1380 && peculiarity_existent == get_entity_peculiarity(fent)
1381 && "not an abstract entity");
1382 /* create irg in entity */
1383 irg = new_pseudo_ir_graph(fent, 0);
1384 set_irg_inline_property(irg, irg_inline_forbidden);
1386 /* @@@ If the spec says so: */
1387 set_entity_visibility(fent, visibility_local);
1389 VERBOSE_PRINT((stdout, "create effects for %s\n",
1390 get_id_str(proc -> proc_ident)));
1392 /* create effects in irg */
1393 num = proc -> n_effs;
1394 for(i = 0; i < num; i++) {
1395 eff = proc -> effs[i];
1396 VERBOSE_PRINT((stdout,
1397 "create effect \"%s\"\n", effect_string[(int)eff -> kind]));
1398 switch(eff -> kind) {
1400 create_abstract_return(irg, proc, eff);
1403 create_abstract_arg(irg, proc, eff);
1406 create_abstract_load(irg, proc, eff);
1409 create_abstract_store(irg, proc, eff);
1412 create_abstract_unknown(irg, proc, eff);
1415 create_abstract_alloc(irg, proc, eff);
1418 create_abstract_call(irg, proc, eff);
1421 create_abstract_join(irg, proc, eff);
1424 create_abstract_raise(irg, proc, eff);
1427 assert(0 && "effect not implemented");
1432 /* close irg in entity */
1433 /* Now we can mature the end block as all it's predecessors are known. */
1434 mature_immBlock (get_irg_end_block(irg));
1436 /* Verify the graph. Finds some very bad errors in the graph. */
1437 VERBOSE_PRINT((stdout, "verify graph\n"));
1439 VERBOSE_PRINT((stdout, "finalize construction\n"));
1440 finalize_cons (irg);
1443 /********************************************************************/
1445 static void assign_firm_entity(module_t *module, entity_t *xmlent)
1452 VERBOSE_PRINT((stdout, "assign entity %s to typeid %s\n",
1453 get_id_str(xmlent -> ent_ident),
1454 get_id_str(xmlent -> owner)));
1456 typ = find_type_in_module(module, xmlent -> owner);
1457 assert(typ && "class not found in module");
1459 assert(is_class_type(type));
1461 num = get_class_n_members(type);
1463 for(i = 0; i < num; i++) {
1464 ent = get_class_member(type, i);
1465 VERBOSE_PRINT((stdout, "compare entity %s and %s\n",
1466 get_id_str(xmlent -> ent_ident), get_entity_name(ent)));
1468 if(get_entity_ident(ent) == xmlent -> ent_ident) {
1473 assert(ent && "did not find a entity");
1475 xmlent -> f_ent = ent;
1478 /********************************************************************/
1479 /* must be primitive type or class type */
1480 static void assign_firm_type(type_t *xmltype)
1486 VERBOSE_PRINT((stdout, "assign firm type to type %s\n",
1487 get_id_str(xmltype -> type_ident)));
1489 /* is it global type? */
1490 typ = get_glob_type();
1491 if(xmltype -> type_ident == get_type_ident(typ)) {
1493 xmltype -> f_tp = typ;
1494 VERBOSE_PRINT((stdout, "is global type %s\n", get_type_name(typ)));
1496 num = get_irp_n_types();
1497 for(i = 0; i < num; i++) {
1498 typ = get_irp_type(i);
1499 VERBOSE_PRINT((stdout, "test type %s\n", get_type_name(typ)));
1500 if(xmltype -> type_ident == get_type_ident(typ)) {
1501 VERBOSE_PRINT((stdout, "found type %s\n", get_type_name(typ)));
1502 xmltype -> f_tp = typ;
1508 assert(typ && "did not find a type");
1511 /********************************************************************/
1513 void create_abstract_proc_effect(module_t *module, proc_t *proc)
1516 type *class_typ = NULL;
1520 /* find the class of a procedure */
1521 VERBOSE_PRINT((stdout, "do find owner id %s\n", get_id_str(proc -> ownerid)));
1522 type = find_type_in_module(module, proc -> ownerid);
1523 assert(type && "class not found in module");
1525 class_typ = get_glob_type();
1526 VERBOSE_PRINT((stdout, "test type %s\n", get_type_name(class_typ)));
1527 if(type -> type_ident != get_type_ident(class_typ)) {
1528 /* find module as class */
1529 num = get_irp_n_types();
1530 for(i = 0; i < num; i++) {
1531 class_typ = get_irp_type(i);
1532 VERBOSE_PRINT((stdout, "test type %s\n", get_type_name(class_typ)));
1533 if(is_class_type(class_typ)
1534 && (type -> type_ident == get_type_ident(class_typ))) {
1535 /* found class type */
1536 VERBOSE_PRINT((stdout, "found type %s\n", get_type_name(class_typ)));
1543 VERBOSE_PRINT((stdout, "found global type %s\n", get_type_name(class_typ)));
1545 assert(class_typ && "type not found");
1546 assert(is_class_type(class_typ) && "is not a class type");
1547 type -> f_tp = class_typ;
1549 /* find entity for procedure in class */
1550 VERBOSE_PRINT((stdout, "find method %s\n",
1551 get_id_str(proc -> proc_ident)));
1553 num = get_class_n_members(class_typ);
1555 for(i = 0; i < num; i++) {
1556 fent = get_class_member(class_typ, i);
1557 VERBOSE_PRINT((stdout, "test proc %s\n", get_entity_name(fent)));
1558 if(proc -> proc_ident == get_entity_ident(fent)) {
1559 VERBOSE_PRINT((stdout, "found proc %s\n",
1560 get_id_str(proc -> proc_ident)));
1561 /* @@@ TODO check args types - not in xml yet */
1562 /* create Firm stuff */
1563 create_abstract_firm(module, proc, fent);
1573 "method %s not found\nNo effects generated\nCandidates are:\n",
1574 get_id_str(proc -> proc_ident));
1575 for(i = 0; i < num; i++) {
1576 fent = get_class_member(class_typ, i);
1577 fprintf(stderr, "%s\n", get_entity_name(fent));
1579 //assert(fent && "procedure not found in class");
1583 void create_abstract_module(module_t *module)
1589 VERBOSE_PRINT((stdout, "create an abstraction for module %s\n",
1590 get_id_str(module -> id)));
1592 VERBOSE_PRINT((stdout, "--handle types for module\n"));
1593 for(type = module -> types; type; type = type -> prev) {
1594 assign_firm_type(type);
1597 VERBOSE_PRINT((stdout, "--handle entities for module\n"));
1599 for(ent = module -> entities; ent; ent = ent -> prev) {
1600 assign_firm_entity(module, ent);
1603 VERBOSE_PRINT((stdout, "--handle procs for module\n"));
1604 for(proc = module -> procs; proc; proc = proc -> next) {
1605 create_abstract_proc_effect(module, proc);
1610 void create_abstraction(const char *filename)
1614 /* read and parse XML file */
1615 read_extern(filename);
1617 /* finished reading and parsing here */
1618 /* build FIRM graphs */
1621 current_module = module;
1622 create_abstract_module(module);
1623 module = module -> next;
1625 current_module = NULL;
1627 /* free data structures */
1637 void free_abstraction(void) {
1638 int i, n_pseudo_irgs = get_irp_n_pseudo_irgs();
1639 for (i = 0; i < n_pseudo_irgs; ++i) {
1640 ir_graph *p_irg = get_irp_pseudo_irg(i);
1641 set_entity_visibility(get_irg_entity(p_irg), visibility_external_allocated);
1642 // @@@ free_pseudo_ir_graph(p_irg);
1647 /********************************************************************/
1652 * Revision 1.16 2004/11/11 12:24:52 goetz
1655 * Revision 1.15 2004/11/11 09:28:32 goetz
1656 * treat pseudo irgs special
1657 * parse 'local' from xml files
1659 * Revision 1.14 2004/11/10 14:42:00 boesler
1660 * be more helpful if a method does not exist
1662 * Revision 1.13 2004/11/05 14:00:53 liekweg
1665 * Revision 1.12 2004/11/02 14:30:31 liekweg
1666 * fixed multi-input join (thx, Boris) --flo
1668 * Revision 1.11 2004/10/29 18:51:53 liekweg
1671 * Revision 1.10 2004/10/25 13:52:24 boesler
1672 * seperated read.h (public interface) and read_t.h (types)
1674 * Revision 1.9 2004/10/22 13:51:35 boesler
1675 * prohibit inlining of pseudo ir_graphs
1677 * Revision 1.8 2004/10/22 13:13:27 boesler
1678 * replaced char* by idents, minor fix in Firm codegen for call
1680 * Revision 1.7 2004/10/21 15:31:55 boesler
1681 * added lots of stuff:
1682 * - build abstract syntax trees
1683 * - build Firm graphs for many effects, still todos
1685 * Revision 1.5 2004/10/18 12:48:20 liekweg
1688 * Revision 1.4 2004/10/14 11:31:53 liekweg
1691 * Revision 1.3 2004/10/13 13:36:28 rubino
1694 * Revision 1.2 2004/10/11 15:56:09 liekweg
1695 * Cleanup, comments ...
1696 * Added init func --flo
1698 * Revision 1.1 2004/10/11 09:31:06 liekweg
1699 * First Import of XML reading procs --flo