missing new_r_simpleSel() added
[libfirm] / ir / external / read.c
index 5feddc5..be932e8 100644 (file)
 #ifdef HAVE_STDLIB_H
 # include <stdlib.h>
 #endif
-
+#ifdef HAVE_STRING_H
 # include <string.h>
+#endif
+
+#include <assert.h>
+
+#include <libxml/xmlmemory.h>
+#include <libxml/parser.h>
+#include <libxml/encoding.h>
 
 #include "read_t.h"
 #include "read.h"
 #include "irprog.h"
 #include "irgraph.h"
+#include "pseudo_irg.h"
 #include "ircons.h"
 #include "irmode.h"
 #include "irdump.h"
 #include "irvrfy.h"
 #include "type.h"
 #include "tv.h"
+#include "xmalloc.h"
+
+# define MY_ENCODING "ISO-8859-1"
+
+# define CHECK(ptr,msg)     assert (ptr && msg)
+
+# define NODE_NAME(n, m) (0 == xmlStrcmp (n->name, (const xmlChar*) #m))
+# define CHECK_NAME(n, m) assert (0 == xmlStrcmp (n->name, (const xmlChar*) #m))
 
-#define VERBOSE_PRINTING 1
+# define NEW(T)     (T*)xmalloc(sizeof (T))
+
+
+#define VERBOSE_PRINTING 0
 
 #if VERBOSE_PRINTING
 # define VERBOSE_PRINT(s) fprintf s
@@ -57,7 +76,7 @@ static module_t *current_module = NULL;
 
 #if VERBOSE_PRINTING
 /* this is only used inside a VERBOSE_PRINT() call */
-static char *effect_string[] = {
+static const char *effect_string[] = {
   "arg",
   "valref",
   "select",
@@ -93,6 +112,7 @@ getNodeProcName (xmlNodePtr node)
   return (proc_str);
 }
 
+# ifdef NEEDED
 static char*
 getNodeClassName (xmlNodePtr node)
 {
@@ -100,6 +120,7 @@ getNodeClassName (xmlNodePtr node)
   assert (proc_str);
   return ( (proc_str));
 }
+# endif /* defined NEEDED */
 
 static const char*
 getNodeId (xmlNodePtr node)
@@ -154,6 +175,7 @@ static const char
 /*
   was Public Interface
 */
+# ifdef NEEDED
 static
 type_t *getTypeByIdent (const ident *id)
 {
@@ -168,7 +190,9 @@ type_t *getTypeByIdent (const ident *id)
 
   return (NULL);
 }
+# endif /* defined NEEDED */
 
+# ifdef NEEDED
 static
 type_t *getTypeById (const ident *id)
 {
@@ -183,7 +207,9 @@ type_t *getTypeById (const ident *id)
 
   return (NULL);
 }
+# endif /* defined NEEDED */
 
+# ifdef NEEDED
 static
 entity_t *getEntityByIdents (const ident *name, const ident *tp_ident)
 {
@@ -199,6 +225,7 @@ entity_t *getEntityByIdents (const ident *name, const ident *tp_ident)
 
   return (NULL);
 }
+# endif /* defined NEEDED */
 
 static
 entity_t *getEntityById (const ident *id)
@@ -215,6 +242,7 @@ entity_t *getEntityById (const ident *id)
   return (NULL);
 }
 
+# ifdef NEEDED
 static
 proc_t *getEffectByName (const ident *proc_ident)
 {
@@ -229,6 +257,7 @@ proc_t *getEffectByName (const ident *proc_ident)
 
   return (NULL);
 }
+# endif /* defined NEEDED */
 
 static
 xmlNodePtr get_any_valid_child(xmlNodePtr elem)
@@ -467,7 +496,7 @@ parseCall (xmlDocPtr doc, xmlNodePtr callelm)
   free (sel);
 
   if (0 != n_args) {
-    const ident **args = (const ident**) malloc(n_args * sizeof(const ident*));
+    const ident **args = (const ident**) xmalloc(n_args * sizeof(const ident*));
     int i = 0;
 
     while (NULL != arg) {
@@ -507,7 +536,7 @@ parseJoin (xmlDocPtr doc, xmlNodePtr joinelm)
     child = child->next;
   }
 
-  ins = (const ident **) malloc (n_ins * sizeof (const ident *) );
+  ins = (const ident **) xmalloc (n_ins * sizeof (const ident *) );
   i = 0;
   child = get_valid_child(joinelm);
 
@@ -602,7 +631,7 @@ parseType (xmlDocPtr doc, xmlNodePtr typeelm)
   VERBOSE_PRINT ((stdout, "type node \t0x%08x (%s)\n", (int) typeelm, tp_id));
   VERBOSE_PRINT ((stdout, "type = \"%s\"\n", getNodeTypeStr (typeelm)));
 
-  type = (type_t*) malloc (sizeof (type_t));
+  type = (type_t*) xmalloc (sizeof (type_t));
   type -> type_ident = new_id_from_str(getNodeTypeStr (typeelm));
   type -> id         = new_id_from_str(tp_id);
 
@@ -656,7 +685,7 @@ parseEffect (xmlDocPtr doc, xmlNodePtr effelm)
   curr_effs = NEW (proc_t);
   curr_effs -> proc_ident = new_id_from_str(procname);
   curr_effs -> ownerid = new_id_from_str(ownerid);
-  curr_effs->effs = (eff_t**) malloc (n_effs * sizeof (eff_t*));
+  curr_effs->effs = (eff_t**) xmalloc (n_effs * sizeof (eff_t*));
 
   cur = effelm -> xmlChildrenNode;
   while (NULL != cur) {
@@ -701,7 +730,7 @@ parseEffect (xmlDocPtr doc, xmlNodePtr effelm)
 
 
 static
-void read_extern (const char *filename)
+int read_extern (const char *filename)
 {
   /* xmlNsPtr ns = NULL; */           /* no namespace for us */
   xmlDocPtr doc;                /* whole document */
@@ -714,7 +743,8 @@ void read_extern (const char *filename)
   LIBXML_TEST_VERSION xmlKeepBlanksDefault (0);
   VERBOSE_PRINT((stdout, "read file %s\n", filename));
   doc = xmlParseFile (filename);
-  CHECK (doc, "xmlParseFile");
+  if (! doc)
+    return 0;
 
   cur = xmlDocGetRootElement (doc);
   CHECK (cur, "xmlDocGetRootElement");
@@ -764,6 +794,8 @@ void read_extern (const char *filename)
 
   module -> next = modules;
   modules = module;
+
+  return 1;
 }
 
 /********************************************************************/
@@ -1194,7 +1226,7 @@ static void create_abstract_call(ir_graph *irg, proc_t *proc, eff_t *eff)
 
   addr = find_valueid_in_proc_effects(eff -> effect.call.valrefid, proc);
   assert(addr && "no address for load");
-  /* if addr is Unknown, set propper mode */
+  /* if addr is Unknown, set proper mode */
   if(iro_Unknown == get_irn_opcode(addr -> firmnode)) {
     set_irn_mode(addr -> firmnode, mode_P);
   }
@@ -1202,16 +1234,16 @@ static void create_abstract_call(ir_graph *irg, proc_t *proc, eff_t *eff)
   if(ent) {
     /* the address */
     sel = new_simpleSel(get_store(), addr -> firmnode, ent);
-    /* mthod type */
+    /* method type */
     mtype = get_entity_type(ent);
-    mik = true;
+    mik = 1;
   }
   else {
     /* the address */
     sel = addr -> firmnode;
-    /* mthod type */
+    /* method type */
     mtype = get_unknown_type();
-    mik = false;
+    mik = 0;
   }
 
   /* the args */
@@ -1247,67 +1279,13 @@ static void create_abstract_call(ir_graph *irg, proc_t *proc, eff_t *eff)
   }
 }
 
-static void create_abstract_join2 (ir_graph *irg, proc_t *proc, eff_t *eff)
-{
-  ir_node *unknown = NULL;
-  ir_node *cond    = NULL;
-  ir_node *phi     = NULL;
-
-  ir_node *ins   [2];
-  ir_node *block [2];
-  ir_node *projX [2];
-  ir_node *jmp   [2];
-
-  eff_t *in_eff  = NULL;
-  int n_ins = -1;
-
-  VERBOSE_PRINT((stdout, "create join2 in %s\n",
-                 get_id_str(proc -> proc_ident)));
-
-  assert (eff_join == eff->kind);
-
-  n_ins = eff->effect.join.n_ins;
-  assert (2 == n_ins);
-
-  in_eff = find_valueid_in_proc_effects (eff->effect.join.ins [0], proc);
-  ins [0] = in_eff->firmnode;
-  in_eff = find_valueid_in_proc_effects (eff->effect.join.ins [1], proc);
-  ins [1] = in_eff->firmnode;
-
-  unknown = new_Unknown (mode_Iu);
-  cond    = new_Cond (unknown);
-
-  projX [0] = new_Proj (cond, mode_X, 0);
-  projX [1] = new_Proj (cond, mode_X, 1);
-
-  mature_immBlock (get_cur_block ());
-
-  block [0] = new_immBlock ();
-  add_immBlock_pred (block [0], projX [0]);
-  jmp [0] = new_Jmp ();
-  mature_immBlock (block [0]);
-
-  block [1] = new_immBlock ();
-  add_immBlock_pred (block [1], projX [1]);
-  jmp [1] = new_Jmp ();
-  mature_immBlock (block [1]);
-
-  new_Block (2, jmp);
-
-  phi = new_Phi (2, ins, get_irn_mode (ins [0]));
-  VERBOSE_PRINT ((stdout, "%s: phi.nr = %li\n", __FUNCTION__, get_irn_node_nr (phi)));
-
-  eff->firmnode = phi;
-
-  add_value_to_proc (proc, eff);
-}
-
 static void create_abstract_join (ir_graph *irg, proc_t *proc, eff_t *eff)
 {
   ir_node **ins    = NULL;
   ir_node *unknown = NULL;
   ir_node *cond    = NULL;
   ir_node *block   = NULL;
+  ir_node *c_block = NULL;
   ir_node *phi     = NULL;
   ir_mode *join_md = mode_ANY;
   int n_ins = -1;
@@ -1319,42 +1297,53 @@ static void create_abstract_join (ir_graph *irg, proc_t *proc, eff_t *eff)
   assert (eff_join == eff->kind);
 
   n_ins = eff->effect.join.n_ins;
-  /* hmm ... special-case n_ins==2? */
-  assert (2 == n_ins);
 
-  if (2 == n_ins) {
-    create_abstract_join2 (irg, proc, eff);
-    return;
-  }
+  /* seems like current_block is not always mature at this point */
+  mature_immBlock (get_cur_block ());
 
+  block = get_cur_block ();     /* remember this so we can put the ProjXs into it */
+
+  /* jump based on an unknown condition so all values are possible */
   unknown = new_Unknown (mode_Iu);
   cond    = new_Cond (unknown);
-  block   = new_immBlock ();
 
-  ins = (ir_node**) malloc (n_ins * sizeof (ir_node*));
+  c_block   = new_immBlock ();  /* for the Phi after the branch(es) */
+
+  ins = (ir_node**) xmalloc (n_ins * sizeof (ir_node*));
   for (i = 0; i < n_ins; i ++) {
-    ir_node *s_block = new_immBlock ();
-    ir_node *projX   = new_Proj (cond, mode_X, (long) i);
+    ir_node *projX   = NULL;
+    ir_node *s_block = NULL;
     ir_node *jmp     = NULL;
     eff_t *in_eff;
 
+    /* make sure the projX is in the 'switch' block */
+    set_cur_block (block);
+    projX   = new_Proj (cond, mode_X, (long) i);
+
+    /* this also sets current_block, so the rest of the code ends up there: */
+    s_block = new_immBlock ();
+
     add_immBlock_pred (s_block, projX);
     mature_immBlock (s_block);
 
     in_eff = find_valueid_in_proc_effects (eff->effect.join.ins [i], proc);
 
     ins [i] = in_eff->firmnode;
+
+    /* need to find a suitable mode for the Phi node */
     if (mode_ANY != get_irn_mode (ins [i])) {
       join_md = get_irn_mode (ins [i]);
     }
 
     jmp = new_Jmp ();
-    add_immBlock_pred (block, jmp);
+    add_immBlock_pred (c_block, jmp);
   }
 
-  mature_immBlock (block);
-  set_cur_block (block);
+  set_cur_block (c_block);
+
   phi = new_Phi (n_ins, ins, join_md);
+
+  mature_immBlock (c_block);
   memset (ins, 0x00, n_ins * sizeof (ir_node*));
   free (ins);
 
@@ -1363,6 +1352,53 @@ static void create_abstract_join (ir_graph *irg, proc_t *proc, eff_t *eff)
   add_value_to_proc (proc, eff);
 }
 
+static void create_abstract_raise (ir_graph *irg, proc_t *proc, eff_t *eff)
+{
+  ir_node *block   = NULL;
+  ir_node *unknown = NULL;
+  ir_node *cond    = NULL;
+
+  /* seems like current_block is not always mature at this point */
+  mature_immBlock (get_cur_block ());
+  block = get_cur_block ();     /* remember this so we can put the ProjXs into it */
+
+  /* jump based on an unknown condition so both values are possible */
+  unknown = new_Unknown (mode_Iu);
+  cond    = new_Cond (unknown);
+
+  /* one branch for 'throw-exception' case */
+  {
+    ir_node *projX = new_Proj (cond, mode_X, 1L);
+    ir_node *b_exc = new_immBlock ();
+    ir_node *obj   = NULL;
+    ir_node *thrw  = NULL;
+    eff_t *thrw_eff = NULL;
+
+    add_immBlock_pred (b_exc, projX);
+
+    thrw_eff = find_valueid_in_proc_effects (eff->effect.raise.valref, proc);
+    obj = thrw_eff->firmnode;
+
+    thrw = new_Raise (get_store (), obj);
+    /* exc-jump to end block */
+    thrw = new_Proj (thrw, mode_X, 0L);
+
+    add_immBlock_pred (get_irg_end_block (irg), thrw);
+    mature_immBlock (get_cur_block ());
+  }
+
+  set_cur_block (block);     /* back to the first block */
+
+  /* one branch for 'non-exception' case */
+  {
+    ir_node *projX = new_Proj (cond, mode_X, 0);
+    new_immBlock ();            /* also sets current_block */
+    add_immBlock_pred (get_cur_block (), projX);
+    mature_immBlock (get_cur_block ());
+    /* continue building in current_block */
+  }
+
+}
 
 static void create_abstract_firm(module_t *module, proc_t *proc, entity *fent)
 {
@@ -1375,9 +1411,12 @@ static void create_abstract_firm(module_t *module, proc_t *proc, entity *fent)
      && peculiarity_existent == get_entity_peculiarity(fent)
      && "not an abstract entity");
   /* create irg in entity */
-  irg = new_ir_graph(fent, 0);
+  irg = new_pseudo_ir_graph(fent, 0);
   set_irg_inline_property(irg, irg_inline_forbidden);
 
+  /* @@@ If the spec says so: */
+  set_entity_visibility(fent, visibility_local);
+
   VERBOSE_PRINT((stdout, "create effects for %s\n",
          get_id_str(proc -> proc_ident)));
 
@@ -1412,6 +1451,9 @@ static void create_abstract_firm(module_t *module, proc_t *proc, entity *fent)
     case eff_join:
       create_abstract_join(irg, proc, eff);
       break;
+    case eff_raise:
+      create_abstract_raise(irg, proc, eff);
+      break;
     default:
       assert(0 && "effect not implemented");
       break;
@@ -1426,7 +1468,7 @@ static void create_abstract_firm(module_t *module, proc_t *proc, entity *fent)
   VERBOSE_PRINT((stdout, "verify graph\n"));
   irg_vrfy(irg);
   VERBOSE_PRINT((stdout, "finalize construction\n"));
-  finalize_cons (irg);
+  irg_finalize_cons (irg);
 }
 
 /********************************************************************/
@@ -1445,7 +1487,7 @@ static void assign_firm_entity(module_t *module, entity_t *xmlent)
   typ = find_type_in_module(module, xmlent -> owner);
   assert(typ && "class not found in module");
   type = typ -> f_tp;
-  assert(is_class_type(type));
+  assert(is_Class_type(type));
 
   num = get_class_n_members(type);
   ent = NULL;
@@ -1519,11 +1561,11 @@ void create_abstract_proc_effect(module_t *module, proc_t *proc)
     for(i = 0; i < num; i++) {
       class_typ = get_irp_type(i);
       VERBOSE_PRINT((stdout, "test type %s\n", get_type_name(class_typ)));
-      if(is_class_type(class_typ)
-     && (type -> type_ident == get_type_ident(class_typ))) {
-    /* found class type */
-    VERBOSE_PRINT((stdout, "found type %s\n", get_type_name(class_typ)));
-    break;
+      if (is_Class_type(class_typ)
+         && (type -> type_ident == get_type_ident(class_typ))) {
+        /* found class type */
+        VERBOSE_PRINT((stdout, "found type %s\n", get_type_name(class_typ)));
+        break;
       }
       class_typ = NULL;
     }
@@ -1532,7 +1574,7 @@ void create_abstract_proc_effect(module_t *module, proc_t *proc)
     VERBOSE_PRINT((stdout, "found global type %s\n", get_type_name(class_typ)));
   }
   assert(class_typ && "type not found");
-  assert(is_class_type(class_typ) && "is not a class type");
+  assert(is_Class_type(class_typ) && "is not a class type");
   type -> f_tp = class_typ;
 
   /* find entity for procedure in class */
@@ -1550,13 +1592,22 @@ void create_abstract_proc_effect(module_t *module, proc_t *proc)
       /* @@@ TODO check args types - not in xml yet */
       /* create Firm stuff */
       create_abstract_firm(module, proc, fent);
-      break;
+      return;
     }
     else {
       fent = NULL;
     }
   }
-  assert(fent && "procedure not found in class");
+
+  /* fail */
+  fprintf(stderr,
+      "method %s not found\nNo effects generated\nCandidates are:\n",
+      get_id_str(proc -> proc_ident));
+  for(i = 0; i < num; i++) {
+    fent = get_class_member(class_typ, i);
+    fprintf(stderr, "%s\n", get_entity_name(fent));
+  }
+  //assert(fent && "procedure not found in class");
 }
 
 static
@@ -1587,12 +1638,13 @@ void create_abstract_module(module_t *module)
 }
 
 
-void create_abstraction(const char *filename)
+int create_abstraction(const char *filename)
 {
   module_t *module;
 
   /* read and parse XML file */
-  read_extern(filename);
+  if (! read_extern(filename))
+    return 0;
 
   /* finished reading and parsing here */
   /* build FIRM graphs */
@@ -1611,13 +1663,64 @@ void create_abstraction(const char *filename)
   entities = NULL;
   procs = NULL;
   modules = NULL;
+
+  return 1;
+}
+
+
+void free_abstraction(void) {
+  int i, n_pseudo_irgs = get_irp_n_pseudo_irgs();
+  for (i = 0; i < n_pseudo_irgs; ++i) {
+    ir_graph *p_irg = get_irp_pseudo_irg(i);
+    set_entity_visibility(get_irg_entity(p_irg), visibility_external_allocated);
+    // @@@ free_pseudo_ir_graph(p_irg);
+  }
 }
 
+
 /********************************************************************/
 
 \f
 /*
  * $Log$
+ * Revision 1.23  2005/11/18 09:46:50  beck
+ * removed depency of bool type and stdbool.h (not C89)
+ *
+ * Revision 1.22  2005/08/16 10:18:35  beck
+ * create_abstraction() now returns an error code if the file could not
+ * be opened.
+ *
+ * Revision 1.21  2005/03/10 10:05:38  goetz
+ * chanmged method name
+ *
+ * Revision 1.20  2005/01/05 14:28:35  beck
+ * renames all is_x*_type() functions to is_X*_type() to prevent name clash with EDG frontend
+ *
+ * Revision 1.19  2004/12/10 15:14:34  beck
+ * used xmalloc instead of malloc
+ *
+ * Revision 1.18  2004/12/02 16:21:42  beck
+ * fixed config.h include
+ *
+ * Revision 1.17  2004/11/23 14:17:31  liekweg
+ * fenced out currently unneeded static functions
+ *
+ * Revision 1.16  2004/11/11 12:24:52  goetz
+ * fixes
+ *
+ * Revision 1.15  2004/11/11 09:28:32  goetz
+ * treat pseudo irgs special
+ * parse 'local' from xml files
+ *
+ * Revision 1.14  2004/11/10 14:42:00  boesler
+ * be more helpful if a method does not exist
+ *
+ * Revision 1.13  2004/11/05 14:00:53  liekweg
+ * added raise
+ *
+ * Revision 1.12  2004/11/02 14:30:31  liekweg
+ * fixed multi-input join (thx, Boris) --flo
+ *
  * Revision 1.11  2004/10/29 18:51:53  liekweg
  * Added Join
  *