home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / amiga / programm / utility / gcc21_92.lha / gcc / lib / crt0.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-20  |  5.2 KB  |  195 lines

  1. /* revamped version by Loren J. Rittle. Thanks Loren !! */
  2.  
  3. /* I guess I should start to put my sources into RCS........ */
  4.  
  5. #include <exec/types.h>
  6. #include <exec/libraries.h>
  7. #include <exec/execbase.h>
  8. #include <inline/exec.h>
  9. #include <libraries/dosextens.h>
  10. #include <limits.h>
  11.  
  12. #include <sys/syscall.h>
  13.  
  14. /* get the current revision number. Version control is automatically done by
  15.  * OpenLibrary(), I just have to check the revision number 
  16.  */
  17. #include "../library/version.h"
  18.  
  19. #define MSTRING(x) STRING(x)
  20. #define STRING(x) #x
  21.  
  22. struct Library *ixemulbase;
  23.  
  24. static int start_stdio();
  25.  
  26. /*
  27.  * Have to take care.. I may not use any library functions in this file,
  28.  * since they are exactly then used, when the library itself couldn't be
  29.  * opened...
  30.  */
  31.  
  32. extern int main();
  33. extern int expand_cmd_line;
  34. extern char *default_wb_window;
  35. extern int errno;
  36. extern char **environ;
  37. extern char *_ctype_;
  38. extern int sys_nerr;
  39. extern struct Library *SysBase, *DOSBase;
  40. extern struct __sFILE **__sF;
  41. static int ENTRY();
  42. static int exec_entry();
  43.  
  44. asm("
  45.     .text
  46.  
  47.     jmp    pc@(_ENTRY-.+2)    | by default jump to normal AmigaDOS startup
  48.  
  49.     | this is a struct exec, for now only OMAGIC is supported
  50.     .globl    exec
  51. exec:
  52.     .word    0        | a_mid
  53.     .word    0407        | a_magic = OMAGIC
  54.     | until gld knows about this, the following fields are not filled out.
  55.     | the relocation fields will be used to provide self-relocating programs
  56.     | that are then residentable
  57.     .long    0        | a_text
  58.     .long    0        | a_data
  59.     .long    0        | a_bss
  60.     .long    0        | a_syms
  61.     .long    _exec_entry    | a_entry
  62.     .long    0        | a_trsize
  63.     .long    0        | a_drsize
  64.  
  65.     | word alignement is guaranteed
  66. ");
  67.  
  68. static int
  69. exec_entry (struct Library *ixembase, int argc, char *argv[], char *env[])
  70. {
  71.   ixemulbase = ixembase;
  72.   environ = env;
  73.   return ix_exec_entry (argc, argv, env, &errno, start_stdio);
  74. }
  75.  
  76. /* this thing is best done with sprintf else, but it has to work without the
  77.  * library as well ;-(
  78.  */
  79. __inline static char *
  80. itoa (int num)
  81. {
  82.   short snum = num;
  83.  
  84.   /* how large can a long get...?? */
  85.   /* Answer (by ljr): best method to use (in terms of portability)
  86.      involves number theory.  The exact number of decimal digits
  87.      needed to store a given number of binary digits is
  88.  
  89.     ceiling ( number_of_binary_digits * log(2) / log(10) )
  90.      or
  91.     ceiling ( number_of_binary_digits * 0.301029996 )
  92.  
  93.      Since sizeof evaluates to the number of bytes a given type takes 
  94.      instead of the number of bits, we need to multiply sizeof (type) by
  95.      CHAR_BIT to obtain the number of bits.  Since an array size specifier
  96.      needs to be integer type, we multiply by 302 and divide by 1000 instead
  97.      of multiplying by 0.301029996.  Finally, we add 1 for the null terminator
  98.      and 1 because we want the ceiling of the function instead of the floor.
  99.      Funny thing about this whole affair is that you really wanted to know
  100.      the size a short could expand to be and not a long...  :-) I know
  101.      comments get out of date, etc.  The nice thing about this method is
  102.      that the size of the array is picked at compile time based upon the
  103.      number of bytes really needed by the local C implementation. */
  104.   static char buf[sizeof snum * CHAR_BIT * 302 / 1000 + 1 + 1];
  105.   char *cp;
  106.   
  107.   buf[sizeof buf - 1] = 0;
  108.   for (cp = &buf[sizeof buf - 1]; snum; snum /= 10) 
  109.     *--cp = (snum % 10) + '0';
  110.  
  111.   return cp;
  112. }
  113.  
  114. __inline static char *
  115. pstrcpy (char *start, char *arg)
  116. {
  117.   while (*start++=*arg++) ;
  118.   return start-1;
  119. }
  120.  
  121. __inline static char *
  122. build_warn (char *t1, int num1)
  123. {
  124.   static char buf[255];
  125.   char *cp;
  126.   
  127.   cp = pstrcpy (buf, t1);
  128.   cp = pstrcpy (cp, itoa (num1));
  129.  
  130.   return buf;
  131. }
  132.  
  133. static int
  134. ENTRY (void)
  135. {
  136.   register unsigned char *rega0  asm("a0");
  137.   register unsigned long  regd0  asm("d0");
  138.  
  139.   UBYTE *aline = rega0;
  140.   ULONG alen = regd0;
  141.  
  142.   ixemulbase = OpenLibrary ("ixemul.library", IX_VERSION);
  143.   if (ixemulbase)
  144.     {
  145.       int res;
  146.  
  147.       /* just warn, in case the user tries to run program which might require
  148.        * more functions than are currently available under this revision. */
  149.       if (ixemulbase->lib_Version == IX_VERSION &&
  150.       ixemulbase->lib_Revision < IX_REVISION)
  151.     /* don't need to block signals, they are blocked until after 
  152.          * ix_startup() in current releases */
  153.     __request_msg (build_warn ("ixemul.library warning: needed revision "
  154.                      MSTRING (IX_REVISION) ", current revision ",
  155.                             ixemulbase->lib_Revision), "Continue");
  156.  
  157.       res = ix_startup (aline, alen, 
  158.                 expand_cmd_line, default_wb_window, start_stdio, &errno);
  159.  
  160.       CloseLibrary (ixemulbase);
  161.  
  162.       return res;
  163.     }
  164.   else
  165.     {
  166.       struct Process *me = (struct Process *)((*(struct ExecBase **)4)->ThisTask);
  167.  
  168.       __request_msg ("Need at least version " MSTRING (IX_VERSION)
  169.              " of ixemul.library.", "Abort");
  170.       
  171.       /* quickly deal with the WB startup message, as the library couldn't do
  172.        * this for us. Nothing at all is done that isn't necessary to just shutup
  173.        * workbench..*/
  174.       if (! me->pr_CLI)
  175.         {
  176.       Forbid (); 
  177.       ReplyMsg ((WaitPort (& me->pr_MsgPort), GetMsg (& me->pr_MsgPort)));
  178.     }
  179.       
  180.       return 20;
  181.     }
  182. }
  183.  
  184. int
  185. start_stdio (int a1, int a2, int a3)
  186. {
  187.   int res;
  188.  
  189.   /* more to follow ;-) */
  190.   ix_get_vars2 (5, &_ctype_, &sys_nerr, &SysBase, &DOSBase, &__sF);
  191.  
  192.   res = main (a1, a2, a3);
  193.   return res;
  194. }
  195.