Changes to avoid compiler warnings.
[libfirm] / ir / tv / tv.c
index 4e8f62d..e0b825f 100644 (file)
@@ -35,6 +35,7 @@
 # include "xprintf.h"
 #include <assert.h>
 #include <limits.h>
+#include <errno.h>
 #include <math.h>
 #include <stdlib.h>
 #include <string.h>
@@ -556,6 +557,27 @@ tarval_B_from_str (const char *s, size_t len)
 }
 
 
+tarval *
+tarval_f_from_str (const char *s, size_t len)
+{
+  tarval *tv;
+  char *buf;
+  char *eptr;
+
+  assert (!BUILDING);
+
+  buf = alloca (len+1);
+  stripcpy (buf, s, len);
+
+  tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
+  tv->mode = mode_f;
+  tv->u.f = (float)strtod (buf, &eptr);
+  assert (eptr == buf+strlen(buf));
+
+  return tarval_identify (tv);
+}
+
+
 tarval *
 tarval_d_from_str (const char *s, size_t len)
 {
@@ -609,6 +631,28 @@ tarval_S_from_str (const char *s, size_t len)
   return tarval_identify (tv);
 }
 
+tarval *tarval_int_from_str (const char *s, size_t len, int base, ir_mode *m) {
+  long val;
+  char *eptr;
+  char *buf;
+
+  assert (mode_is_int(m));
+  assert (!BUILDING);
+
+  buf = alloca (len+1);
+  stripcpy (buf, s, len);
+
+  errno = 0;
+  val = strtol(buf, &eptr, base);    /* strtoll */
+  assert (eptr == buf+strlen(buf));
+  if ((errno == ERANGE)               &&
+      ((m == mode_l) || (m == mode_L))  ) {
+    printf("WARNING: Constant %s not representable. Continuing with %ld.\n",
+          s, val);
+  }
+
+  return tarval_from_long(m, val);
+}
 
 /* Create a tarval with mode `m' and value `i' casted to the type that
    represents such tarvals on host.  The resulting value must be legal
@@ -1632,6 +1676,7 @@ tarval_print (XP_PAR1, const xprintf_info *info ATTRIBUTE((unused)), XP_PARN)
 {
   tarval *val = XP_GETARG (tarval *, 0);
   int printed;
+  char buf[40];
 
   TARVAL_VRFY (val);
 
@@ -1642,10 +1687,11 @@ tarval_print (XP_PAR1, const xprintf_info *info ATTRIBUTE((unused)), XP_PARN)
     break;
 
   case irm_f:                  /* float */
-    printed = XPF1R ("%g", (double)(val->u.f));
+    sprintf (buf, "%1.9e", (float)(val->u.f));
+    printed = XPF1R ("%s", buf);
     break;
   case irm_d:                  /* double */
-    printed = XPF1R ("%g", (double)(val->u.d));
+    printed = XPF1R ("%1.30g", (double)(val->u.d));
     break;
 
   case irm_c:                  /* signed char */
@@ -1653,7 +1699,7 @@ tarval_print (XP_PAR1, const xprintf_info *info ATTRIBUTE((unused)), XP_PARN)
     if (isprint (val->u.chil)) {
       printed = XPF1R ("'%c'", val->u.chil);
     } else {
-      printed = XPF1R ("'\\%03o'", val->u.chil);
+      printed = XPF1R ("0x%x", (unsigned long)val->u.chil);
     }
     break;
 
@@ -1672,7 +1718,10 @@ tarval_print (XP_PAR1, const xprintf_info *info ATTRIBUTE((unused)), XP_PARN)
     if (val->u.p.xname) {
       printed = XPR (val->u.p.xname);
     } else if (val->u.p.ent) {
-      printed = XPF1R ("(%I)", val->u.p.ent->name);
+      if (get_entity_peculiarity(val->u.p.ent) == existent)
+       printed = XPF1R ("&(%I)", get_entity_ld_ident(val->u.p.ent));
+      else
+       printed = XPSR ("(NULL)");
     } else {
       assert (val == tarval_p_void);
       printed = XPSR ("(void)");
@@ -1729,3 +1778,21 @@ get_tv_mode (tarval *tv)
 {
   return tv->mode;
 }
+
+/* Returns the entity if the tv is a pointer to an entity, else
+   returns NULL; */
+entity *get_tv_entity(tarval *tv) {
+  entity *ent = NULL;
+
+  if (tv->mode == mode_p) {
+    if (tv->u.p.xname) {
+      assert(0);
+      /* not an entity */
+    } else if (tv->u.p.ent) {
+      ent = tv->u.p.ent;
+    } else {
+      /* not an entity */
+    }
+  }
+  return ent;
+}