* as/z80/z80mch.c: fixed bug #1704376: missing as-z80 errors
[fw/sdcc] / device / lib / printfl.c
index 0754fb79032ca11a26b47cdd68d119ac399f1778..a5d2211176f6cd9a12256f22fd874ab66e8fa7a4 100644 (file)
@@ -20,7 +20,7 @@
     In other words, you are welcome to use, share and improve this program.
     You are forbidden to forbid anyone else to use, share and improve
     what you give them.   Help stamp out software-hoarding!
-    
+
     2001060401: Improved by was@icb.snz.chel.su
 -------------------------------------------------------------------------*/
 
 
 #include <stdarg.h>
 #include <stdio.h>
+#include <stdlib.h>
 #ifndef __ds390
 /* just for the SP */
 #include <8051.h>
 #endif
 
-static data volatile char ch;
-static data char radix ;
-static bit  long_flag = 0;
-static bit  string_flag =0;
-static bit  char_flag = 0;
-static bit sign;
-static char * data str ;
-static data long val;
+static __data char radix ;
+static __bit  long_flag = 0;
+static __bit  string_flag =0;
+static __bit  char_flag = 0;
+static char * __data str ;
+static __data long val;
+
+/* This great loop fails with the ds390 port (2003-01-13).
+
+   At the beginning resp. end of the loop the compiler inserts a "push ar2"
+   resp. "pop ar2", which badly interfers with the push/pop in the source.
+
+   Library functions should be rock solid and portable. There's an _ltoa in
+   the library, so let's use it and don't reinvent the wheel.
+
+   Bernhard
+*/
+
+#if NICE_LIFO_IMPLEMENTATION_BUT_NOT_PORTABLE
+static __data volatile char ch;
+static __bit sign;
 
 static void pval(void)
 {
-        char sp;
+        volatile char sp;
         unsigned long lval;
         sp = SP;
 
@@ -78,33 +92,35 @@ static void pval(void)
 
         do
         {
-#if 1
+
+#  if 1
                 if(radix != 16)  ch = (lval % radix) + '0';
                 else ch = "0123456789ABCDEF"[(unsigned char)lval & 0x0f];
-                _asm push _ch _endasm;
+                __asm push _ch __endasm;
                 lval /= radix;
-#else
+#  else
                // This only looks more efficient, but isn't. see the .map
-                ch = (lval % radix) + '0'; 
-                if (ch>'9') ch+=7; 
-                _asm push _ch _endasm; 
+                ch = (lval % radix) + '0';
+                if (ch>'9') ch+=7;
+                __asm push _ch __endasm;
                 lval /= radix;
-#endif
+#  endif
         }
         while (lval);
 
         if (sign) {
                 ch = '-';
-                _asm push _ch _endasm;
+                __asm push _ch __endasm;
         }
 
         while (sp != SP) {
-                _asm pop _ch _endasm;
+                __asm pop _ch __endasm;
                 putchar(ch);
         }
 }
+#endif
 
-void printf_small (char * fmt, ... ) reentrant
+void printf_small (char * fmt, ... ) __reentrant
 {
     va_list ap ;
 
@@ -156,8 +172,25 @@ void printf_small (char * fmt, ... ) reentrant
                 else
                     val = va_arg(ap,int);
 
+#if NICE_LIFO_IMPLEMENTATION_BUT_NOT_PORTABLE
             if (radix) pval();
-            else putchar((char)val);
+#else
+            if (radix)
+            {
+              static char __idata buffer[12]; /* 37777777777(oct) */
+              char __idata * stri;
+
+              _ltoa (val, buffer, radix);
+              stri = buffer;
+              while (*stri)
+                {
+                  putchar (*stri);
+                  stri++;
+                }
+            }
+#endif
+            else
+              putchar((char)val);
 
         } else
             putchar(*fmt);