home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / amiga / programm / utility / gcc21_92.lha / gcc / lib / bcrt0.c next >
Encoding:
C/C++ Source or Header  |  1992-04-20  |  7.0 KB  |  267 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. #include <sys/exec.h>
  14.  
  15. /* get the current revision number. Version control is automatically done by
  16.  * OpenLibrary(), I just have to check the revision number 
  17.  */
  18. #include "../library/version.h"
  19.  
  20. #undef DEBUG
  21. #ifdef DEBUG
  22. #define DP(a) kprintf a
  23. static int inline __geta4() {int res;asm ("movel a4,%0" : "=g" (res));return res;}
  24. #else
  25. #define DP(a)
  26. #endif
  27.  
  28. #define MSTRING(x) STRING(x)
  29. #define STRING(x) #x
  30.  
  31. struct Library *ixemulbase;
  32.  
  33. static int start_stdio();
  34.  
  35. /*
  36.  * Have to take care.. I may not use any library functions in this file,
  37.  * since they are exactly then used, when the library itself couldn't be
  38.  * opened...
  39.  */
  40.  
  41. extern int main();
  42. extern int expand_cmd_line;
  43. extern char *default_wb_window;
  44. extern int errno;
  45. extern char **environ;
  46. extern char *_ctype_;
  47. extern int sys_nerr;
  48. extern struct Library *SysBase, *DOSBase;
  49. extern struct __sFILE **__sF;
  50. static int ENTRY();
  51. static int exec_entry();
  52.  
  53. asm("
  54.     .text
  55.  
  56.     jmp    pc@(_ENTRY-.+2)    | by default jump to normal AmigaDOS startup
  57.  
  58.     | this is a struct exec, for now only OMAGIC is supported
  59.     .globl    exec
  60. exec:
  61.     .word    ___machtype    | a_mid
  62.     .word    0407        | a_magic = OMAGIC
  63.     | until gld knows about this, the following fields are not filled out.
  64.     | the relocation fields will be used to provide self-relocating programs
  65.     | that are then residentable
  66.     .long    ___text_size    | a_text
  67.     .long    ___data_size    | a_data
  68.     .long    ___bss_size    | a_bss
  69.     .long    0        | a_syms
  70.     .long    _exec_entry    | a_entry
  71.     .long    0        | a_trsize
  72.     .long    0        | a_drsize
  73.  
  74.     | word alignement is guaranteed
  75. ");
  76.  
  77. extern struct exec exec asm ("exec");
  78. extern int _sdata(), __a4_init(), __datadata_relocs();
  79. extern int __data_size, __bss_size;
  80.  
  81. #ifdef RCRT0
  82. /* have to do this this way, or it is done base-relative.. */
  83. static inline int dbsize() 
  84. {
  85.   int res;
  86.   asm ("movel #___data_size,%0; addl #___bss_size,%0" : "=r" (res));
  87.   return res;
  88. }
  89.  
  90. static void inline
  91. ix_resident (void *base, int num, int a4, int size, void *relocs)
  92. {
  93.   typedef void (*func)(int, int, int, void *);
  94.  
  95.   ((func)((int)base - 6*(SYS_ix_resident + 4))) (num, a4, size, relocs);
  96. }
  97.  
  98. #else
  99. static void inline
  100. ix_resident (void *base, int num, int a4)
  101. {
  102.   typedef void (*func)(int, int);
  103.  
  104.   ((func)((int)base - 6*(SYS_ix_resident + 4))) (num, a4);
  105. }
  106. #endif
  107.  
  108. static int
  109. exec_entry (struct Library *ixembase, int argc, char *argv[], char *env[])
  110. {
  111.   register int a4;
  112.   /* needed, so that data can be accessed. ix_resident might change this
  113.      again afterwards */
  114.   asm volatile ("lea    ___a4_init,a4" : "=r" (a4) : "0" (a4));
  115.   asm volatile ("movel    a4,%0" : "=r" (a4) : "0" (a4));
  116.  
  117. #ifdef RCRT0
  118.   ix_resident (ixembase, 4, a4, dbsize(), __datadata_relocs);
  119. #else
  120.   ix_resident (ixembase, 2, a4);
  121. #endif
  122.   ixemulbase = ixembase;
  123.   environ = env;
  124.   return ix_exec_entry (argc, argv, env, &errno, start_stdio);
  125. }
  126.  
  127. /* this thing is best done with sprintf else, but it has to work without the
  128.  * library as well ;-(
  129.  */
  130. __inline static char *
  131. itoa (int num)
  132. {
  133.   short snum = num;
  134.  
  135.   /* how large can a long get...?? */
  136.   /* Answer (by ljr): best method to use (in terms of portability)
  137.      involves number theory.  The exact number of decimal digits
  138.      needed to store a given number of binary digits is
  139.  
  140.     ceiling ( number_of_binary_digits * log(2) / log(10) )
  141.      or
  142.     ceiling ( number_of_binary_digits * 0.301029996 )
  143.  
  144.      Since sizeof evaluates to the number of bytes a given type takes 
  145.      instead of the number of bits, we need to multiply sizeof (type) by
  146.      CHAR_BIT to obtain the number of bits.  Since an array size specifier
  147.      needs to be integer type, we multiply by 302 and divide by 1000 instead
  148.      of multiplying by 0.301029996.  Finally, we add 1 for the null terminator
  149.      and 1 because we want the ceiling of the function instead of the floor.
  150.      Funny thing about this whole affair is that you really wanted to know
  151.      the size a short could expand to be and not a long...  :-) I know
  152.      comments get out of date, etc.  The nice thing about this method is
  153.      that the size of the array is picked at compile time based upon the
  154.      number of bytes really needed by the local C implementation. */
  155.   static char buf[sizeof snum * CHAR_BIT * 302 / 1000 + 1 + 1];
  156.   char *cp;
  157.   
  158.   buf[sizeof buf - 1] = 0;
  159.   for (cp = &buf[sizeof buf - 1]; snum; snum /= 10) 
  160.     *--cp = (snum % 10) + '0';
  161.  
  162.   return cp;
  163. }
  164.  
  165. __inline static char *
  166. pstrcpy (char *start, char *arg)
  167. {
  168.   while (*start++=*arg++) ;
  169.   return start-1;
  170. }
  171.  
  172. __inline static char *
  173. build_warn (char *t1, int num1)
  174. {
  175.   static char buf[255];
  176.   char *cp;
  177.   
  178.   cp = pstrcpy (buf, t1);
  179.   cp = pstrcpy (cp, itoa (num1));
  180.  
  181.   return buf;
  182. }
  183.  
  184. static int
  185. ENTRY (void)
  186. {
  187.   register unsigned char *rega0  asm("a0");
  188.   register unsigned long  regd0  asm("d0");
  189.   register int a4;
  190.  
  191.   UBYTE *aline = rega0;
  192.   ULONG alen = regd0;
  193.   struct Library *ibase;
  194.  
  195.   /* needed, so that data can be accessed. ix_resident() might change this
  196.      again afterwards */
  197.   asm volatile ("lea    ___a4_init,a4" : "=r" (a4) : "0" (a4));
  198.   asm volatile ("movel a4,%0" : "=r" (a4) : "0" (a4));
  199.  
  200. DP(("ENTRY: a4 = $%lx\n", __geta4()));
  201.  
  202.   ibase = OpenLibrary ("ixemul.library", IX_VERSION);
  203.   if (ibase)
  204.     {
  205.       int res;
  206.  
  207.       /* just warn, in case the user tries to run program which might require
  208.        * more functions than are currently available under this revision. */
  209.       if (ibase->lib_Version == IX_VERSION &&
  210.       ibase->lib_Revision < IX_REVISION)
  211.     /* don't need to block signals, they are blocked until after 
  212.          * ix_startup() in current releases */
  213.     __request_msg (build_warn ("ixemul.library warning: needed revision "
  214.                      MSTRING (IX_REVISION) ", current revision ",
  215.                             ibase->lib_Revision), "Continue");
  216.  
  217. #ifdef RCRT0
  218.       ix_resident (ibase, 4, a4, dbsize(), __datadata_relocs);
  219. #else
  220.       ix_resident (ibase, 2, a4);
  221. #endif
  222.  
  223. DP(("ix_resident: a4 = $%lx\n", __geta4()));
  224.  
  225.       ixemulbase = ibase;
  226.  
  227.       res = ix_startup (aline, alen, 
  228.                 expand_cmd_line, default_wb_window, start_stdio, &errno);
  229.  
  230.       CloseLibrary (ixemulbase);
  231.  
  232.       return res;
  233.     }
  234.   else
  235.     {
  236.       struct Process *me = (struct Process *)((*(struct ExecBase **)4)->ThisTask);
  237.  
  238.       __request_msg ("Need at least version " MSTRING (IX_VERSION)
  239.              " of ixemul.library.", "Abort");
  240.       
  241.       /* quickly deal with the WB startup message, as the library couldn't do
  242.        * this for us. Nothing at all is done that isn't necessary to just shutup
  243.        * workbench..*/
  244.       if (! me->pr_CLI)
  245.         {
  246.       Forbid (); 
  247.       ReplyMsg ((WaitPort (& me->pr_MsgPort), GetMsg (& me->pr_MsgPort)));
  248.     }
  249.       
  250.       return 20;
  251.     }
  252. }
  253.  
  254. int
  255. start_stdio (int a1, int a2, int a3)
  256. {
  257.   int res;
  258.  
  259. DP(("start_stdio1: a4 = $%lx\n", __geta4()));
  260.  
  261.   /* more to follow ;-) */
  262.   ix_get_vars2 (5, &_ctype_, &sys_nerr, &SysBase, &DOSBase, &__sF);
  263.  
  264.   res = main (a1, a2, a3);
  265.   return res;
  266. }
  267.