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");
99 getNodeClassName (xmlNodePtr node)
101 char *proc_str = (char*) xmlGetProp (node, BAD_CAST "class");
103 return ( (proc_str));
105 # endif /* defined NEEDED */
108 getNodeId (xmlNodePtr node)
110 const char *id_str = (const char*) xmlGetProp (node, BAD_CAST "id");
116 getNodeRefId (xmlNodePtr node)
118 const char *refid_str = (char*) xmlGetProp (node, BAD_CAST "refid");
120 return ((refid_str));
124 getNodeTypeId (xmlNodePtr node)
126 const char *type_str = (char*) xmlGetProp (node, BAD_CAST "type");
132 *getNodeTypeStr (xmlNodePtr node)
134 const char *type_str = (char*) xmlGetProp (node, BAD_CAST "type");
140 getNodeOwnerStr (xmlNodePtr node)
142 const char *owner_str = (char*) xmlGetProp (node, BAD_CAST "owner");
148 *getNodeEntityStr (xmlNodePtr node)
150 const char *ent_str = (char*) xmlGetProp (node, BAD_CAST "entity");
162 type_t *getTypeByIdent (const ident *id)
164 type_t *curr = types; // @@@ TODO module -> types
166 while (NULL != curr) {
167 if (id == curr -> type_ident) {
175 # endif /* defined NEEDED */
179 type_t *getTypeById (const ident *id)
181 type_t *curr = types; // which ones?
183 while (NULL != curr) {
184 if (id == curr -> id) {
192 # endif /* defined NEEDED */
196 entity_t *getEntityByIdents (const ident *name, const ident *tp_ident)
198 entity_t *curr = entities; // TODO module -> entities
200 while (NULL != curr) {
201 if ((name == curr -> ent_ident)
202 && (tp_ident == curr -> tp_ident)) {
210 # endif /* defined NEEDED */
213 entity_t *getEntityById (const ident *id)
215 entity_t *curr = entities;
217 while (NULL != curr) {
218 if (id == curr -> id) {
229 proc_t *getEffectByName (const ident *proc_ident)
231 proc_t *curr_effs = procs;
233 while (NULL != curr_effs) {
234 if (proc_ident == curr_effs -> proc_ident) {
237 curr_effs = curr_effs->next;
242 # endif /* defined NEEDED */
245 xmlNodePtr get_any_valid_child(xmlNodePtr elem)
249 assert(elem && "no element");
250 child = elem -> xmlChildrenNode;
251 while(child && (NODE_NAME (child, comment))) {
252 child = child -> next;
258 xmlNodePtr get_valid_child(xmlNodePtr elem)
262 child = get_any_valid_child(elem);
263 assert(child && "lost child in deep black forest");
268 * parse XML structure and construct an additional structure
271 parseArg (xmlDocPtr doc, xmlNodePtr argelm)
279 CHECK_NAME (argelm, arg);
280 VERBOSE_PRINT ((stdout, "arg node \t0x%08x\n", (int) argelm));
282 id = getNodeId (argelm);
283 VERBOSE_PRINT ((stdout, "arg->id = \"%s\"\n", id));
284 num_str = (char*) xmlGetProp (argelm, BAD_CAST "number");
285 num = atoi (num_str);
286 VERBOSE_PRINT ((stdout, "arg->no = \"%d\"\n", num));
288 typeid = getNodeTypeStr (argelm);
291 arg -> kind = eff_arg;
292 arg -> id = new_id_from_str(id);
293 arg -> effect.arg.num = num;
294 arg -> effect.arg.type_ident = new_id_from_str(typeid);
300 parseValref (xmlDocPtr doc, xmlNodePtr valelm)
305 CHECK_NAME (valelm, valref);
306 VERBOSE_PRINT ((stdout, "valref node \t0x%08x\n", (int) valelm));
308 ref_id = getNodeRefId (valelm);
309 VERBOSE_PRINT ((stdout, "val->refid = \"%s\"\n", ref_id));
311 valref = NEW (eff_t);
312 valref->kind = eff_valref;
313 valref-> id = new_id_from_str(ref_id);
319 parseSelect (xmlDocPtr doc, xmlNodePtr selelm)
321 const ident *entity_id = new_id_from_str(getNodeEntityStr (selelm));
324 eff_t *valref = NULL;
325 eff_t *sel = NEW (eff_t);
326 sel->kind = eff_select;
328 CHECK_NAME (selelm, select);
329 VERBOSE_PRINT ((stdout, "select node \t0x%08x\n", (int) selelm));
331 ent = getEntityById (entity_id);
332 assert(ent && "entity not found");
333 VERBOSE_PRINT ((stdout, "select entity %s\n", get_id_str(ent -> ent_ident)));
335 child = selelm->xmlChildrenNode;
338 valref = parseValref (doc, child);
341 sel-> id = valref ? valref-> id : NO_ID;
342 sel-> effect.select.ent = ent;
352 parseLoad (xmlDocPtr doc, xmlNodePtr loadelm)
357 eff_t *load = NEW (eff_t);
358 load->kind = eff_load;
360 CHECK_NAME (loadelm, load);
361 VERBOSE_PRINT ((stdout, "load node \t0x%08x\n", (int) loadelm));
362 id = new_id_from_str(getNodeId (loadelm));
364 child = get_valid_child(loadelm);
365 if(NODE_NAME (child, select)) {
366 sel = parseSelect (doc, child);
367 load-> effect.load.ent = sel-> effect.select.ent;
368 VERBOSE_PRINT ((stdout, "load entity \t%s\n",
369 get_id_str(load -> effect.load.ent -> ent_ident)));
372 sel = parseValref (doc, child);
373 load-> effect.load.ent = NULL;
377 load-> effect.load.ptrrefid = sel-> id;
385 parseStore (xmlDocPtr doc, xmlNodePtr storeelm)
390 eff_t *store = NEW (eff_t);
391 store->kind = eff_store;
393 CHECK_NAME (storeelm, store);
394 VERBOSE_PRINT ((stdout, "store node \t0x%08x\n", (int) storeelm));
396 child = get_valid_child(storeelm);
397 if(NODE_NAME (child, select)) {
398 sel = parseSelect (doc, child);
399 store-> effect.store.ent = sel-> effect.select.ent;
402 sel = parseValref (doc, child);
403 store-> effect.store.ent = NULL;
407 valref = parseValref (doc, child);
409 store-> effect.store.ptrrefid = sel-> id;
410 store-> effect.store.valrefid = valref-> id;
419 parseAlloc (xmlDocPtr doc, xmlNodePtr allocelm)
422 const ident *type_id;
423 eff_t *alloc = NEW (eff_t); /* ...! */
424 alloc->kind = eff_alloc;
426 CHECK_NAME (allocelm, alloc);
427 VERBOSE_PRINT ((stdout, "alloc node \t0x%08x\n", (int) allocelm));
428 id = new_id_from_str(getNodeId (allocelm));
429 VERBOSE_PRINT ((stdout, "alloc->id = \"%s\"\n", get_id_str(id)));
430 type_id = new_id_from_str(getNodeTypeId (allocelm));
431 VERBOSE_PRINT ((stdout, "alloc->type_id = \"%s\"\n", get_id_str(type_id)));
434 alloc-> effect.alloc.tp_id = type_id;
440 parseCall (xmlDocPtr doc, xmlNodePtr callelm)
447 eff_t *call = NEW (eff_t);
448 call->kind = eff_call;
450 CHECK_NAME (callelm, call);
451 VERBOSE_PRINT ((stdout, "call node \t0x%08x\n", (int) callelm));
452 id = new_id_from_str(getNodeId (callelm));
453 VERBOSE_PRINT ((stdout, "call->id = \"%s\"\n", get_id_str(id)));
455 child = get_valid_child(callelm);
456 if(NODE_NAME (child, select)) {
457 sel = parseSelect (doc, child);
458 call-> effect.call.ent = sel-> effect.select.ent;
461 sel = parseValref (doc, child);
462 call-> effect.call.ent = NULL;
465 arg = child = child->next;
468 while (NULL != child) {
474 call-> effect.call.valrefid = sel-> id;
475 call-> effect.call.n_args = n_args;
476 call-> effect.call.args = NULL;
481 const ident **args = (const ident**) malloc(n_args * sizeof(const ident*));
484 while (NULL != arg) {
485 eff_t *valref = parseValref (doc, arg);
486 args [i ++] = valref-> id;
491 call-> effect.call.args = args;
498 parseJoin (xmlDocPtr doc, xmlNodePtr joinelm)
505 eff_t *join = NEW (eff_t);
506 join->kind = eff_join;
508 CHECK_NAME (joinelm, join);
509 VERBOSE_PRINT ((stdout, "join node \t0x%08x\n", (int) joinelm));
510 id = new_id_from_str(getNodeId (joinelm));
511 VERBOSE_PRINT ((stdout, "join->id = \"%s\"\n", get_id_str(id)));
513 child = get_valid_child(joinelm);
516 while (NULL != child) {
521 ins = (const ident **) malloc (n_ins * sizeof (const ident *) );
523 child = get_valid_child(joinelm);
525 while (NULL != child) {
526 eff_t *valref = parseValref (doc, child);
527 ins [i ++] = valref-> id;
533 join-> effect.join.n_ins = n_ins;
534 join-> effect.join.ins = ins;
540 parseUnknown (xmlDocPtr doc, xmlNodePtr unknownelm)
543 eff_t *unknown = NEW (eff_t);
544 unknown->kind = eff_unknown;
546 CHECK_NAME (unknownelm, unknown);
547 VERBOSE_PRINT ((stdout, "unknown node \t0x%08x\n", (int) unknownelm));
548 id = new_id_from_str(getNodeId (unknownelm));
555 parseReturn (xmlDocPtr doc, xmlNodePtr retelm)
558 eff_t *ret = NEW (eff_t);
561 CHECK_NAME (retelm, ret);
562 VERBOSE_PRINT ((stdout, "ret node \t0x%08x\n", (int) retelm));
564 child = get_any_valid_child(retelm);
567 eff_t *valref = parseValref (doc, child);
568 ret-> effect.ret.ret_id = valref-> id;
571 ret-> effect.ret.ret_id = NO_ID;
578 parseRaise (xmlDocPtr doc, xmlNodePtr raiseelm)
583 eff_t *raise = NEW (eff_t);
584 raise->kind = eff_raise;
586 CHECK_NAME (raiseelm, raise);
587 VERBOSE_PRINT ((stdout, "raise node \t0x%08x\n", (int) raiseelm));
588 tp_id = getNodeTypeId (raiseelm);
589 VERBOSE_PRINT ((stdout, "raise->type = \"%s\"\n", tp_id));
590 child = get_valid_child(raiseelm);
592 assert (NULL != child);
594 valref = parseValref (doc, child);
595 raise-> effect.raise.valref = valref-> id;
596 raise-> effect.raise.tp_id = new_id_from_str(tp_id);
607 /** parse a type node and insert it into the list */
609 parseType (xmlDocPtr doc, xmlNodePtr typeelm)
612 const char *tp_id = getNodeId (typeelm);
613 VERBOSE_PRINT ((stdout, "type node \t0x%08x (%s)\n", (int) typeelm, tp_id));
614 VERBOSE_PRINT ((stdout, "type = \"%s\"\n", getNodeTypeStr (typeelm)));
616 type = (type_t*) malloc (sizeof (type_t));
617 type -> type_ident = new_id_from_str(getNodeTypeStr (typeelm));
618 type -> id = new_id_from_str(tp_id);
624 /** parse an entity node and insert it into the list */
626 parseEntity (xmlDocPtr doc, xmlNodePtr entelm)
628 entity_t *ent = NEW (entity_t);
631 const char *ent_id = getNodeId (entelm);
632 /* fprintf (stdout, "entity node \t0x%08x (%d)\n", (int) entelm, ent_id); */
633 VERBOSE_PRINT ((stdout, "ent = \"%s.%s\"\n",
634 getNodeTypeStr (entelm),
635 getNodeEntityStr (entelm)));
638 ent -> ent_ident = new_id_from_str (getNodeEntityStr (entelm));
639 ent -> tp_ident = new_id_from_str (getNodeTypeStr (entelm));
640 ent -> owner = new_id_from_str (getNodeOwnerStr (entelm));
641 ent -> id = new_id_from_str(ent_id);
643 ent->prev = entities;
647 /** parse any effect, and turn it into an eff_t (TODO) */
649 parseEffect (xmlDocPtr doc, xmlNodePtr effelm)
652 const char *procname = getNodeProcName (effelm);
653 const char *ownerid = getNodeOwnerStr (effelm);
654 proc_t *curr_effs = NULL;
658 VERBOSE_PRINT ((stdout, "effect for method \"%s\"\n", procname));
660 cur = effelm -> xmlChildrenNode;
661 while (NULL != cur) {
665 VERBOSE_PRINT ((stdout, "has %d effects\n", n_effs));
667 curr_effs = NEW (proc_t);
668 curr_effs -> proc_ident = new_id_from_str(procname);
669 curr_effs -> ownerid = new_id_from_str(ownerid);
670 curr_effs->effs = (eff_t**) malloc (n_effs * sizeof (eff_t*));
672 cur = effelm -> xmlChildrenNode;
673 while (NULL != cur) {
676 if (NODE_NAME (cur, arg)) {
677 eff = (eff_t*) parseArg (doc, cur);
678 } else if (NODE_NAME (cur, load)) {
679 eff = (eff_t*) parseLoad (doc, cur);
680 } else if (NODE_NAME (cur, store)) {
681 eff = (eff_t*) parseStore (doc, cur);
682 } else if (NODE_NAME (cur, alloc)) {
683 eff = (eff_t*) parseAlloc (doc, cur);
684 } else if (NODE_NAME (cur, call)) {
685 eff = (eff_t*) parseCall (doc, cur);
686 } else if (NODE_NAME (cur, join)) {
687 eff = (eff_t*) parseJoin (doc, cur);
688 } else if (NODE_NAME (cur, unknown)) {
689 eff = (eff_t*) parseUnknown (doc, cur);
690 } else if (NODE_NAME (cur, ret)) {
691 eff = (eff_t*) parseReturn (doc, cur);
692 } else if (NODE_NAME (cur, raise)) {
693 eff = (eff_t*) parseRaise (doc, cur);
694 } else if (NODE_NAME (cur, comment)) {
698 fprintf (stderr, "wrong element \"%s\"\n", BAD_CAST cur->name);
702 VERBOSE_PRINT ((stdout, "effect %p@%d\n", (void*)eff, i));
703 curr_effs -> effs[i++] = eff;
707 assert((i == n_effs) && "incorrect number of effects");
708 curr_effs -> n_effs = n_effs;
709 curr_effs -> next = procs;
715 void read_extern (const char *filename)
717 /* xmlNsPtr ns = NULL; */ /* no namespace for us */
718 xmlDocPtr doc; /* whole document */
719 xmlNodePtr cur; /* current node */
723 /* i've got no idea what the VERSION cast is all about. voodoo
724 programming at its finest. */
725 LIBXML_TEST_VERSION xmlKeepBlanksDefault (0);
726 VERBOSE_PRINT((stdout, "read file %s\n", filename));
727 doc = xmlParseFile (filename);
728 CHECK (doc, "xmlParseFile");
730 cur = xmlDocGetRootElement (doc);
731 CHECK (cur, "xmlDocGetRootElement");
733 if (! NODE_NAME (cur, effects)) {
734 fprintf (stderr,"root node \"%s\" != \"effects\"\n", BAD_CAST cur->name);
739 mod_id = getNodeModuleIdent (cur);
740 if (NULL != mod_id) {
741 VERBOSE_PRINT ((stdout, "effects for \"%s\"\n",
742 get_id_str(mod_id)));
745 VERBOSE_PRINT ((stdout, "effects \t0x%08x\n", (int) cur));
749 cur = cur->xmlChildrenNode;
750 while (cur != NULL) {
751 if (NODE_NAME (cur, type)) {
752 parseType (doc, cur);
753 } else if (NODE_NAME (cur, entity)) {
754 parseEntity (doc, cur);
755 } else if (NODE_NAME (cur, effect)) {
756 parseEffect (doc, cur);
757 } else if ((NODE_NAME (cur, comment))) {
760 fprintf (stderr, "wrong element \"%s\"\n", BAD_CAST cur->name);
766 module = NEW(module_t);
767 module -> id = mod_id;
768 module -> types = types;
769 module -> entities = entities;
770 module -> procs = procs;
776 module -> next = modules;
780 /********************************************************************/
783 * free additional structure
786 void freeArg (eff_t *arg)
788 VERBOSE_PRINT ((stdout, "free arg node \t0x%08x\n", (int) arg));
794 void freeValref (eff_t *valref)
796 VERBOSE_PRINT ((stdout, "free valref node \t0x%08x\n", (int) valref));
802 void freeSelect (eff_t *sel)
804 VERBOSE_PRINT ((stdout, "free select node \t0x%08x\n", (int) sel));
810 void freeLoad (eff_t *load)
812 VERBOSE_PRINT ((stdout, "free load node \t0x%08x\n", (int) load));
818 void freeStore (eff_t *store)
820 VERBOSE_PRINT ((stdout, "free store node \t0x%08x\n", (int) store));
826 void freeAlloc (eff_t *alloc)
828 VERBOSE_PRINT ((stdout, "free alloc node \t0x%08x\n", (int) alloc));
834 void freeCall (eff_t *call)
836 VERBOSE_PRINT ((stdout, "free call node \t0x%08x\n", (int) call));
837 free(call -> effect.call.args);
843 void freeJoin (eff_t *join)
845 VERBOSE_PRINT ((stdout, "free join node \t0x%08x\n", (int) join));
846 free(join -> effect.join.ins);
852 void freeUnknown (eff_t *unknown)
854 VERBOSE_PRINT ((stdout, "free unknown node \t0x%08x\n", (int) unknown));
860 void freeReturn (eff_t *ret)
862 VERBOSE_PRINT ((stdout, "free ret node \t0x%08x\n", (int) ret));
868 void freeRaise (eff_t *raise)
870 VERBOSE_PRINT ((stdout, "free raise node \t0x%08x\n", (int) raise));
877 void freeProcEffs(proc_t *proc)
882 VERBOSE_PRINT ((stdout, "free effect for method \"%s\"\n",
883 get_id_str(proc -> proc_ident)));
885 num = proc -> n_effs;
886 for(i = 0; i < num; i++) {
887 switch(proc -> effs[i] -> kind) {
889 freeArg(proc -> effs[i]);
892 freeValref(proc -> effs[i]);
895 freeSelect(proc -> effs[i]);
898 freeLoad(proc -> effs[i]);
901 freeStore(proc -> effs[i]);
904 freeAlloc(proc -> effs[i]);
907 freeCall(proc -> effs[i]);
910 freeUnknown(proc -> effs[i]);
913 freeJoin(proc -> effs[i]);
916 freeRaise(proc -> effs[i]);
919 freeReturn(proc -> effs[i]);
922 assert(0 && "try to free an unknown effect");
931 void freeModuleProcs(module_t *module)
933 proc_t *next_proc, *proc;
935 VERBOSE_PRINT ((stdout, "free procs for module \"%s\"\n",
936 get_id_str(module -> id)));
938 proc = module -> procs;
940 next_proc = proc -> next;
950 module_t *module, *next_module;
954 freeModuleProcs(module);
955 next_module = module -> next;
957 module = next_module;
961 /********************************************************************/
964 type_t *find_type_in_module(module_t *module, const ident *typeid)
968 for(type = module -> types; type; type = type -> prev) {
969 VERBOSE_PRINT((stdout, "test typeid %s\n", get_id_str(type -> id)));
970 if(type -> id == typeid) {
971 VERBOSE_PRINT((stdout, "found\n"));
975 VERBOSE_PRINT((stdout, "did not find type id %s\n", get_id_str(typeid)));
979 /********************************************************************/
981 static void add_value_to_proc(proc_t *proc, eff_t *eff)
983 eff -> next = proc -> values;
984 proc -> values = eff;
988 eff_t *find_valueid_in_proc_effects(const ident *id, proc_t *proc)
992 val = proc -> values;
994 if(id == val -> id) {
1002 static void create_abstract_return(ir_graph *irg, proc_t *proc, eff_t *eff)
1007 VERBOSE_PRINT((stdout, "create effect:return in %s\n",
1008 get_id_str(proc -> proc_ident)));
1009 if(NO_ID == eff -> effect.ret.ret_id) {
1011 x = new_Return (get_store(), 0, NULL);
1016 /* return one value */
1017 eff_res = find_valueid_in_proc_effects(eff -> effect.ret.ret_id, proc);
1018 assert(eff_res -> firmnode && "firm in effect not set");
1019 in[0] = eff_res -> firmnode;
1020 x = new_Return (get_store(), 1, in);
1022 eff -> firmnode = x;
1024 /* Now we generated all instructions for this block and all its predecessor
1025 * blocks so we can mature it. (There are not too much.) */
1026 mature_immBlock (get_irg_current_block(irg));
1028 /* This adds the in edge of the end block which originates at the return statement.
1029 * The return node passes controlflow to the end block. */
1030 add_immBlock_pred (get_irg_end_block(irg), x);
1034 static void create_abstract_arg(ir_graph *irg, proc_t *proc, eff_t *eff)
1042 VERBOSE_PRINT((stdout, "create effect:arg %d in %s\n",
1043 eff -> effect.arg.num, get_id_str(proc -> proc_ident)));
1044 ent = get_irg_entity(irg);
1045 typ = get_entity_type(ent);
1047 /* read argument eff -> effect.arg.num and place in values list */
1048 num = get_method_n_params(typ);
1049 assert((num >= eff -> effect.arg.num) && "number too big");
1050 typ = get_method_param_type(typ, eff -> effect.arg.num);
1051 mode = get_type_mode(typ);
1053 arg = new_Proj(get_irg_args(irg), mode, eff -> effect.arg.num);
1054 eff -> firmnode = arg;
1056 add_value_to_proc(proc, eff);
1060 static void create_abstract_load(ir_graph *irg, proc_t *proc, eff_t *eff)
1062 ir_node *sel, *load;
1067 VERBOSE_PRINT((stdout, "create load in %s\n",
1068 get_id_str(proc -> proc_ident)));
1070 if(eff -> effect.load.ent) {
1071 ent = eff -> effect.load.ent -> f_ent;
1072 VERBOSE_PRINT((stdout, "load from %s\n", get_entity_name(ent)));
1075 VERBOSE_PRINT((stdout, "store to memory\n"));
1079 addr = find_valueid_in_proc_effects(eff -> effect.load.ptrrefid, proc);
1080 assert(addr && "no address for load");
1081 /* if addr is Unknown, set proper mode */
1082 if(iro_Unknown == get_irn_opcode(addr -> firmnode)) {
1083 set_irn_mode(addr -> firmnode, mode_P);
1087 sel = new_simpleSel(get_store(), addr -> firmnode, ent);
1088 mode = get_type_mode(get_entity_type(ent));
1091 sel = addr -> firmnode;
1094 load = new_Load(get_store(), sel, mode);
1095 set_store(new_Proj(load, mode_M, 0));
1096 eff -> firmnode = new_Proj(load, mode, 2);
1098 add_value_to_proc(proc, eff);
1102 static void create_abstract_store(ir_graph *irg, proc_t *proc, eff_t *eff)
1104 ir_node *sel, *store;
1108 VERBOSE_PRINT((stdout, "create store in %s\n",
1109 get_id_str(proc -> proc_ident)));
1111 if(eff -> effect.store.ent) {
1112 ent = eff -> effect.store.ent -> f_ent;
1113 VERBOSE_PRINT((stdout, "store to entity %s\n", get_entity_name(ent)));
1116 VERBOSE_PRINT((stdout, "store to memory\n"));
1120 addr = find_valueid_in_proc_effects(eff -> effect.store.ptrrefid, proc);
1121 assert(addr && "no address for store");
1122 /* if addr is Unknown, set propper mode */
1123 if(iro_Unknown == get_irn_opcode(addr -> firmnode)) {
1124 set_irn_mode(addr -> firmnode, mode_P);
1127 val = find_valueid_in_proc_effects(eff -> effect.store.valrefid, proc);
1128 assert(val && "no address for store");
1129 /* if addr is Unknown, set propper mode */
1130 if(iro_Unknown == get_irn_opcode(val -> firmnode)) {
1131 set_irn_mode(val -> firmnode, get_type_mode(get_entity_type(ent)));
1135 sel = new_simpleSel(get_store(), addr -> firmnode, ent);
1138 sel = addr -> firmnode;
1140 store = new_Store(get_store(), sel, val -> firmnode);
1141 set_store(new_Proj(store, mode_M, 0));
1142 eff -> firmnode = store;
1146 static void create_abstract_alloc(ir_graph *irg, proc_t *proc, eff_t *eff)
1151 symconst_symbol sym;
1153 VERBOSE_PRINT((stdout, "create alloc in %s\n",
1154 get_id_str(proc -> proc_ident)));
1156 xtype = find_type_in_module(current_module, eff -> effect.alloc.tp_id);
1157 assert(xtype && "type not found");
1158 ftype = xtype -> f_tp;
1161 alloc = new_Alloc(get_store(), new_SymConst(sym, symconst_size), ftype,
1163 set_store(new_Proj(alloc, mode_M, 0));
1164 eff -> firmnode = new_Proj(alloc, mode_P, 2);
1166 add_value_to_proc(proc, eff);
1170 static void create_abstract_unknown(ir_graph *irg, proc_t *proc, eff_t *eff)
1174 VERBOSE_PRINT((stdout, "create unknown in %s\n",
1175 get_id_str(proc -> proc_ident)));
1177 unknown = new_Unknown(mode_ANY);
1178 eff -> firmnode = unknown;
1180 add_value_to_proc(proc, eff);
1184 static void create_abstract_call(ir_graph *irg, proc_t *proc, eff_t *eff)
1186 ir_node *sel, *call;
1192 int mik; /* is method somehow known? */
1194 VERBOSE_PRINT((stdout, "create call in %s\n",
1195 get_id_str(proc -> proc_ident)));
1197 if(eff -> effect.call.ent) {
1198 ent = eff -> effect.call.ent -> f_ent;
1199 VERBOSE_PRINT((stdout, "call %s\n", get_entity_name(ent)));
1203 VERBOSE_PRINT((stdout, "call something in memory\n"));
1206 addr = find_valueid_in_proc_effects(eff -> effect.call.valrefid, proc);
1207 assert(addr && "no address for load");
1208 /* if addr is Unknown, set propper mode */
1209 if(iro_Unknown == get_irn_opcode(addr -> firmnode)) {
1210 set_irn_mode(addr -> firmnode, mode_P);
1215 sel = new_simpleSel(get_store(), addr -> firmnode, ent);
1217 mtype = get_entity_type(ent);
1222 sel = addr -> firmnode;
1224 mtype = get_unknown_type();
1229 num = eff -> effect.call.n_args;
1230 VERBOSE_PRINT((stdout, "number of args given: %d\n", num));
1232 VERBOSE_PRINT((stdout, "number of args expected: %d\n",
1233 get_method_n_params(mtype)));
1235 irns = alloca(num * sizeof(ir_node*));
1236 for(i = 0; i < num; i++) {
1237 irns[i] = find_valueid_in_proc_effects(eff -> effect.call.args[i], proc)
1239 if(iro_Unknown == get_irn_opcode(irns[i])) {
1241 set_irn_mode(irns[i], get_type_mode(get_method_param_type(mtype, i)));
1244 set_irn_mode(irns[i], mode_ANY);
1248 call = new_Call(get_store(), sel, num, irns, mtype);
1249 set_store(new_Proj(call, mode_M, 0));
1250 if(mik && (0 != get_method_n_ress(mtype))) {
1251 eff -> firmnode = new_Proj(call,
1252 get_type_mode(get_method_res_type(mtype, 0)),
1254 add_value_to_proc(proc, eff); /* result can be accessed */
1257 eff -> firmnode = NULL; /* result can not be accessed */
1261 static void create_abstract_join (ir_graph *irg, proc_t *proc, eff_t *eff)
1263 ir_node **ins = NULL;
1264 ir_node *unknown = NULL;
1265 ir_node *cond = NULL;
1266 ir_node *block = NULL;
1267 ir_node *c_block = NULL;
1268 ir_node *phi = NULL;
1269 ir_mode *join_md = mode_ANY;
1273 VERBOSE_PRINT((stdout, "create join in %s\n",
1274 get_id_str(proc -> proc_ident)));
1276 assert (eff_join == eff->kind);
1278 n_ins = eff->effect.join.n_ins;
1280 /* seems like current_block is not always mature at this point */
1281 mature_immBlock (get_cur_block ());
1283 block = get_cur_block (); /* remember this so we can put the ProjXs into it */
1285 /* jump based on an unknown condition so all values are possible */
1286 unknown = new_Unknown (mode_Iu);
1287 cond = new_Cond (unknown);
1289 c_block = new_immBlock (); /* for the Phi after the branch(es) */
1291 ins = (ir_node**) malloc (n_ins * sizeof (ir_node*));
1292 for (i = 0; i < n_ins; i ++) {
1293 ir_node *projX = NULL;
1294 ir_node *s_block = NULL;
1295 ir_node *jmp = NULL;
1298 /* make sure the projX is in the 'switch' block */
1299 set_cur_block (block);
1300 projX = new_Proj (cond, mode_X, (long) i);
1302 /* this also sets current_block, so the rest of the code ends up there: */
1303 s_block = new_immBlock ();
1305 add_immBlock_pred (s_block, projX);
1306 mature_immBlock (s_block);
1308 in_eff = find_valueid_in_proc_effects (eff->effect.join.ins [i], proc);
1310 ins [i] = in_eff->firmnode;
1312 /* need to find a suitable mode for the Phi node */
1313 if (mode_ANY != get_irn_mode (ins [i])) {
1314 join_md = get_irn_mode (ins [i]);
1318 add_immBlock_pred (c_block, jmp);
1321 set_cur_block (c_block);
1323 phi = new_Phi (n_ins, ins, join_md);
1325 mature_immBlock (c_block);
1326 memset (ins, 0x00, n_ins * sizeof (ir_node*));
1329 eff->firmnode = phi;
1331 add_value_to_proc (proc, eff);
1334 static void create_abstract_raise (ir_graph *irg, proc_t *proc, eff_t *eff)
1336 ir_node *block = NULL;
1337 ir_node *unknown = NULL;
1338 ir_node *cond = NULL;
1340 /* seems like current_block is not always mature at this point */
1341 mature_immBlock (get_cur_block ());
1342 block = get_cur_block (); /* remember this so we can put the ProjXs into it */
1344 /* jump based on an unknown condition so both values are possible */
1345 unknown = new_Unknown (mode_Iu);
1346 cond = new_Cond (unknown);
1348 /* one branch for 'throw-exception' case */
1350 ir_node *projX = new_Proj (cond, mode_X, 1L);
1351 ir_node *b_exc = new_immBlock ();
1352 ir_node *obj = NULL;
1353 ir_node *thrw = NULL;
1354 eff_t *thrw_eff = NULL;
1356 add_immBlock_pred (b_exc, projX);
1358 thrw_eff = find_valueid_in_proc_effects (eff->effect.raise.valref, proc);
1359 obj = thrw_eff->firmnode;
1361 thrw = new_Raise (get_store (), obj);
1362 /* exc-jump to end block */
1363 thrw = new_Proj (thrw, mode_X, 0L);
1365 add_immBlock_pred (get_irg_end_block (irg), thrw);
1366 mature_immBlock (get_cur_block ());
1369 set_cur_block (block); /* back to the first block */
1371 /* one branch for 'non-exception' case */
1373 ir_node *projX = new_Proj (cond, mode_X, 0);
1374 new_immBlock (); /* also sets current_block */
1375 add_immBlock_pred (get_cur_block (), projX);
1376 mature_immBlock (get_cur_block ());
1377 /* continue building in current_block */
1382 static void create_abstract_firm(module_t *module, proc_t *proc, entity *fent)
1389 assert(visibility_external_allocated == get_entity_visibility(fent)
1390 && peculiarity_existent == get_entity_peculiarity(fent)
1391 && "not an abstract entity");
1392 /* create irg in entity */
1393 irg = new_pseudo_ir_graph(fent, 0);
1394 set_irg_inline_property(irg, irg_inline_forbidden);
1396 /* @@@ If the spec says so: */
1397 set_entity_visibility(fent, visibility_local);
1399 VERBOSE_PRINT((stdout, "create effects for %s\n",
1400 get_id_str(proc -> proc_ident)));
1402 /* create effects in irg */
1403 num = proc -> n_effs;
1404 for(i = 0; i < num; i++) {
1405 eff = proc -> effs[i];
1406 VERBOSE_PRINT((stdout,
1407 "create effect \"%s\"\n", effect_string[(int)eff -> kind]));
1408 switch(eff -> kind) {
1410 create_abstract_return(irg, proc, eff);
1413 create_abstract_arg(irg, proc, eff);
1416 create_abstract_load(irg, proc, eff);
1419 create_abstract_store(irg, proc, eff);
1422 create_abstract_unknown(irg, proc, eff);
1425 create_abstract_alloc(irg, proc, eff);
1428 create_abstract_call(irg, proc, eff);
1431 create_abstract_join(irg, proc, eff);
1434 create_abstract_raise(irg, proc, eff);
1437 assert(0 && "effect not implemented");
1442 /* close irg in entity */
1443 /* Now we can mature the end block as all it's predecessors are known. */
1444 mature_immBlock (get_irg_end_block(irg));
1446 /* Verify the graph. Finds some very bad errors in the graph. */
1447 VERBOSE_PRINT((stdout, "verify graph\n"));
1449 VERBOSE_PRINT((stdout, "finalize construction\n"));
1450 finalize_cons (irg);
1453 /********************************************************************/
1455 static void assign_firm_entity(module_t *module, entity_t *xmlent)
1462 VERBOSE_PRINT((stdout, "assign entity %s to typeid %s\n",
1463 get_id_str(xmlent -> ent_ident),
1464 get_id_str(xmlent -> owner)));
1466 typ = find_type_in_module(module, xmlent -> owner);
1467 assert(typ && "class not found in module");
1469 assert(is_class_type(type));
1471 num = get_class_n_members(type);
1473 for(i = 0; i < num; i++) {
1474 ent = get_class_member(type, i);
1475 VERBOSE_PRINT((stdout, "compare entity %s and %s\n",
1476 get_id_str(xmlent -> ent_ident), get_entity_name(ent)));
1478 if(get_entity_ident(ent) == xmlent -> ent_ident) {
1483 assert(ent && "did not find a entity");
1485 xmlent -> f_ent = ent;
1488 /********************************************************************/
1489 /* must be primitive type or class type */
1490 static void assign_firm_type(type_t *xmltype)
1496 VERBOSE_PRINT((stdout, "assign firm type to type %s\n",
1497 get_id_str(xmltype -> type_ident)));
1499 /* is it global type? */
1500 typ = get_glob_type();
1501 if(xmltype -> type_ident == get_type_ident(typ)) {
1503 xmltype -> f_tp = typ;
1504 VERBOSE_PRINT((stdout, "is global type %s\n", get_type_name(typ)));
1506 num = get_irp_n_types();
1507 for(i = 0; i < num; i++) {
1508 typ = get_irp_type(i);
1509 VERBOSE_PRINT((stdout, "test type %s\n", get_type_name(typ)));
1510 if(xmltype -> type_ident == get_type_ident(typ)) {
1511 VERBOSE_PRINT((stdout, "found type %s\n", get_type_name(typ)));
1512 xmltype -> f_tp = typ;
1518 assert(typ && "did not find a type");
1521 /********************************************************************/
1523 void create_abstract_proc_effect(module_t *module, proc_t *proc)
1526 type *class_typ = NULL;
1530 /* find the class of a procedure */
1531 VERBOSE_PRINT((stdout, "do find owner id %s\n", get_id_str(proc -> ownerid)));
1532 type = find_type_in_module(module, proc -> ownerid);
1533 assert(type && "class not found in module");
1535 class_typ = get_glob_type();
1536 VERBOSE_PRINT((stdout, "test type %s\n", get_type_name(class_typ)));
1537 if(type -> type_ident != get_type_ident(class_typ)) {
1538 /* find module as class */
1539 num = get_irp_n_types();
1540 for(i = 0; i < num; i++) {
1541 class_typ = get_irp_type(i);
1542 VERBOSE_PRINT((stdout, "test type %s\n", get_type_name(class_typ)));
1543 if(is_class_type(class_typ)
1544 && (type -> type_ident == get_type_ident(class_typ))) {
1545 /* found class type */
1546 VERBOSE_PRINT((stdout, "found type %s\n", get_type_name(class_typ)));
1553 VERBOSE_PRINT((stdout, "found global type %s\n", get_type_name(class_typ)));
1555 assert(class_typ && "type not found");
1556 assert(is_class_type(class_typ) && "is not a class type");
1557 type -> f_tp = class_typ;
1559 /* find entity for procedure in class */
1560 VERBOSE_PRINT((stdout, "find method %s\n",
1561 get_id_str(proc -> proc_ident)));
1563 num = get_class_n_members(class_typ);
1565 for(i = 0; i < num; i++) {
1566 fent = get_class_member(class_typ, i);
1567 VERBOSE_PRINT((stdout, "test proc %s\n", get_entity_name(fent)));
1568 if(proc -> proc_ident == get_entity_ident(fent)) {
1569 VERBOSE_PRINT((stdout, "found proc %s\n",
1570 get_id_str(proc -> proc_ident)));
1571 /* @@@ TODO check args types - not in xml yet */
1572 /* create Firm stuff */
1573 create_abstract_firm(module, proc, fent);
1583 "method %s not found\nNo effects generated\nCandidates are:\n",
1584 get_id_str(proc -> proc_ident));
1585 for(i = 0; i < num; i++) {
1586 fent = get_class_member(class_typ, i);
1587 fprintf(stderr, "%s\n", get_entity_name(fent));
1589 //assert(fent && "procedure not found in class");
1593 void create_abstract_module(module_t *module)
1599 VERBOSE_PRINT((stdout, "create an abstraction for module %s\n",
1600 get_id_str(module -> id)));
1602 VERBOSE_PRINT((stdout, "--handle types for module\n"));
1603 for(type = module -> types; type; type = type -> prev) {
1604 assign_firm_type(type);
1607 VERBOSE_PRINT((stdout, "--handle entities for module\n"));
1609 for(ent = module -> entities; ent; ent = ent -> prev) {
1610 assign_firm_entity(module, ent);
1613 VERBOSE_PRINT((stdout, "--handle procs for module\n"));
1614 for(proc = module -> procs; proc; proc = proc -> next) {
1615 create_abstract_proc_effect(module, proc);
1620 void create_abstraction(const char *filename)
1624 /* read and parse XML file */
1625 read_extern(filename);
1627 /* finished reading and parsing here */
1628 /* build FIRM graphs */
1631 current_module = module;
1632 create_abstract_module(module);
1633 module = module -> next;
1635 current_module = NULL;
1637 /* free data structures */
1647 void free_abstraction(void) {
1648 int i, n_pseudo_irgs = get_irp_n_pseudo_irgs();
1649 for (i = 0; i < n_pseudo_irgs; ++i) {
1650 ir_graph *p_irg = get_irp_pseudo_irg(i);
1651 set_entity_visibility(get_irg_entity(p_irg), visibility_external_allocated);
1652 // @@@ free_pseudo_ir_graph(p_irg);
1657 /********************************************************************/
1662 * Revision 1.17 2004/11/23 14:17:31 liekweg
1663 * fenced out currently unneeded static functions
1665 * Revision 1.16 2004/11/11 12:24:52 goetz
1668 * Revision 1.15 2004/11/11 09:28:32 goetz
1669 * treat pseudo irgs special
1670 * parse 'local' from xml files
1672 * Revision 1.14 2004/11/10 14:42:00 boesler
1673 * be more helpful if a method does not exist
1675 * Revision 1.13 2004/11/05 14:00:53 liekweg
1678 * Revision 1.12 2004/11/02 14:30:31 liekweg
1679 * fixed multi-input join (thx, Boris) --flo
1681 * Revision 1.11 2004/10/29 18:51:53 liekweg
1684 * Revision 1.10 2004/10/25 13:52:24 boesler
1685 * seperated read.h (public interface) and read_t.h (types)
1687 * Revision 1.9 2004/10/22 13:51:35 boesler
1688 * prohibit inlining of pseudo ir_graphs
1690 * Revision 1.8 2004/10/22 13:13:27 boesler
1691 * replaced char* by idents, minor fix in Firm codegen for call
1693 * Revision 1.7 2004/10/21 15:31:55 boesler
1694 * added lots of stuff:
1695 * - build abstract syntax trees
1696 * - build Firm graphs for many effects, still todos
1698 * Revision 1.5 2004/10/18 12:48:20 liekweg
1701 * Revision 1.4 2004/10/14 11:31:53 liekweg
1704 * Revision 1.3 2004/10/13 13:36:28 rubino
1707 * Revision 1.2 2004/10/11 15:56:09 liekweg
1708 * Cleanup, comments ...
1709 * Added init func --flo
1711 * Revision 1.1 2004/10/11 09:31:06 liekweg
1712 * First Import of XML reading procs --flo