* device/lib/printfl.c: fix a ds390 bug by making it portable
[fw/sdcc] / device / lib / printfl.c
index 77b4cc4c13c893c1075e2ed1128c1e3e555d1b5b..b863d7a5f5257a5ac0af6fc5da1310c8c21fd330 100644 (file)
@@ -3,24 +3,24 @@
 
     Written By - Sandeep Dutta . sandeep.dutta@usa.net (1999)
 
-    This program is free software; you can redistribute it and/or modify it
-    under the terms of the GNU General Public License as published by the
+    This library is free software; you can redistribute it and/or modify it
+    under the terms of the GNU Library General Public License as published by the
     Free Software Foundation; either version 2, or (at your option) any
     later version.
 
-    This program is distributed in the hope that it will be useful,
+    This library is distributed in the hope that it will be useful,
     but WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-    GNU General Public License for more details.
+    GNU Library General Public License for more details.
 
-    You should have received a copy of the GNU General Public License
+    You should have received a copy of the GNU Library General Public License
     along with this program; if not, write to the Free Software
     Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
     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
 -------------------------------------------------------------------------*/
 
    format     output type       argument-type
      %d        decimal             int
      %ld       decimal             long
-     %hd       decimal             short/char
+     %hd       decimal             char
      %x        hexadecimal         int
      %lx       hexadecimal         long
-     %hx       hexadecimal         short/char
+     %hx       hexadecimal         char
      %o        octal               int
      %lo       octal               long
-     %ho       octal               short/char
-     %c        character           char/short
-     %s        character           _generic pointer
+     %ho       octal               char
+     %c        character           char
+     %s        character           generic pointer
 */
 
 #include <stdarg.h>
 #include <stdio.h>
+#include <stdlib.h>
 #ifndef __ds390
-#include <8051.h> // for the SP
+/* 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  short_flag = 0;
-static bit sign;
+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;
 
@@ -71,24 +86,25 @@ static void pval(void)
        if (!long_flag) {
          lval &= 0x0000ffff;
        }
-        if (short_flag) {
+        if (char_flag) {
          lval &= 0x000000ff;
        }
 
         do
         {
-#if 1
+
+#  if 1
                 if(radix != 16)  ch = (lval % radix) + '0';
-                else ch = "0123456789ABCDEF"[(unsigned short)lval & 0x0f];
+                else ch = "0123456789ABCDEF"[(unsigned char)lval & 0x0f];
                 _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);
 
@@ -102,6 +118,7 @@ static void pval(void)
                 putchar(ch);
         }
 }
+#endif
 
 void printf_small (char * fmt, ... ) reentrant
 {
@@ -111,7 +128,7 @@ void printf_small (char * fmt, ... ) reentrant
 
     for (; *fmt ; fmt++ ) {
         if (*fmt == '%') {
-            long_flag = string_flag = short_flag = 0;
+            long_flag = string_flag = char_flag = 0;
             fmt++ ;
             switch (*fmt) {
             case 'l':
@@ -119,7 +136,7 @@ void printf_small (char * fmt, ... ) reentrant
                 fmt++;
                 break;
             case 'h':
-                short_flag = 1;
+                char_flag = 1;
                 fmt++;
             }
 
@@ -142,7 +159,7 @@ void printf_small (char * fmt, ... ) reentrant
             }
 
             if (string_flag) {
-                str = va_arg(ap, char _generic *);
+                str = va_arg(ap, char *);
                 while (*str) putchar(*str++);
                 continue ;
             }
@@ -150,13 +167,26 @@ void printf_small (char * fmt, ... ) reentrant
             if (long_flag)
                 val = va_arg(ap,long);
             else
-                if (short_flag)
-                    val = va_arg(ap,short);
+                if (char_flag)
+                    val = va_arg(ap,char);
                 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 data buffer[12], c;
+
+              _ltoa (val, buffer, radix);
+              str = buffer;
+              while ((c = *str++) != '\0')
+                putchar (c);
+            }
+#endif
+            else
+              putchar((char)val);
 
         } else
             putchar(*fmt);