home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 432b.lha / CClib.library_V3.0 / gcc / crt0.asm < prev    next >
Assembly Source File  |  1990-11-10  |  8KB  |  335 lines

  1. *crt0.asm
  2. *   Copyright (C) 1990 Paul Gittings.
  3. *
  4. * This file is part of aMIGA gARGANTUAN c cOMPILER (agcc).
  5. *
  6. * agcc is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 1, or (at your option)
  9. * any later version.
  10. *
  11. * agcc is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with agcc; see the file COPYING.  If not, write to
  18. * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  19. *
  20. *
  21. *    A startup/__main routine for use with agcc and CCLib.library
  22. *    ctr0.o        should be used if floating point is required.
  23. *    ctr0_no_fp.o    should be used when an application does not use
  24. *            floating point.
  25. *    The following libraries are loaded:
  26. *        Dos.library
  27. *        CCLib.library
  28. *        MathIeeeDoubBas.library
  29. *    
  30. *    This routine is based on: 
  31. *    Robert Albrecht's _main.c and
  32. *    Bryce Nesbitt's BothStartup.asm 
  33. *
  34. *    Paul Gittings    21/2/1990
  35. *    ACSnet          paulg@tplrd.tpl.oz
  36. *    snail        172 Union St.
  37. *            Erskineville 2043
  38. *            NSW
  39. *            Australia
  40. *
  41. ******* Included Files *************************************************
  42.  
  43. ;    NOLIST
  44. ;    INCLUDE "exec/types.i"
  45.  
  46.  
  47. ;    IFND EXEC_ABLES_I
  48. ;    INCLUDE "exec/ables.i"
  49. ;    ENDC
  50.     
  51. FORBID    MACRO
  52.     IFC    '\1',''
  53.     ADDQ.B    #1,TDNestCnt(A6)
  54.     ENDC
  55.     IFNC    '\1',''
  56.     MOVE.L    4,\1
  57.     ADDQ.B    #1,TDNestCnt(\1)
  58.     ENDC
  59.     ENDM
  60.  
  61. ;    IFND EXEC_EXECBASE_I
  62. ;    INCLUDE "exec/execbase.i"
  63. ;    ENDC
  64.  
  65. ThisTask    EQU    $114
  66. TDNestCnt    EQU    $127
  67.  
  68. ;    IFND LIBRARIES_DOS_I
  69. ;    INCLUDE "libraries/dos.i"
  70. ;    ENDC
  71.  
  72. ;    IFND LIBRARIES_DOESEXTENS_I
  73. ;    INCLUDE "libraries/dosextens.i"
  74. ;    ENDC
  75.  
  76. pr_CLI        EQU    $0ac
  77. pr_MsgPort    EQU    $05c
  78.  
  79. ;
  80. ;       Offsets into user data
  81. ;
  82. ud_argc        EQU     96
  83. ud_argv        EQU    100
  84.  
  85. ******* Imported *******************************************************
  86.  
  87.     xref    _main            ; C code entry point
  88.  
  89. ;    cclib routines.
  90.  
  91.     xref    _ClearSTDIO
  92.     xref    _SetupSTDIO
  93.     xref    _GetSTDIO
  94.  
  95. callsys macro
  96.     xref    _LVO\1
  97.     jsr    _LVO\1(a6)
  98.     endm
  99.  
  100. ******* Exported *******************************************************
  101.  
  102.     xdef    _SysBase
  103.     xdef    _DOSBase
  104.     xdef    _MathIeeeDoubBasBase
  105.     xdef    _CCLibBase
  106.  
  107.     xdef    _errno
  108.     xdef    _stdin
  109.     xdef    _stdout
  110.     xdef    _stderr
  111.     xdef    __math
  112.     xdef    _blocksize
  113.     xdef    _exit_fcn
  114.     xdef    _type
  115.  
  116.     xdef    _exit            ; standard C exit function
  117.     xdef    __exit
  118.  
  119. ************************************************************************
  120. *
  121. *    Standard Program Entry Point
  122. *
  123. *    If called from CLI then a0 will point to a buffer containing the
  124. *    command line but not including the command name. Also, d0 will
  125. *    contain the length of this line.
  126. *
  127. *
  128. ;
  129. ;a3 - frame pointer
  130. ;a4 - ThisTask
  131. dosCmdLen    equr    d6
  132. dosCmdBuf    equr    d5
  133. ;
  134. startup:    move.l    d0,dosCmdLen
  135.         move.l    a0,dosCmdBuf
  136.         lea.l    ss,a3        ; set up "frame pointer"
  137.         clr.l    (a3)+        ; _errno
  138.         clr.l    (a3)+        ; _exit_fcn
  139.         move.l    sp,(a3)+    ; _initialSP
  140.  
  141.     ;------ get Exec's library base pointer:
  142.         move.l    4,a6        ; get _AbsExecBase
  143.         move.l    a6,(a3)+    ; _SysBase
  144.  
  145.     ;------ Open the DOS library:
  146.  
  147.         clr.l    d0
  148.         lea.l    DOSName(pc),A1
  149.         callsys OpenLibrary
  150.         tst.l    d0
  151.         beq    bailout
  152.         move.l    d0,(a3)+    ; _DOSBase
  153.  
  154.     ;------ Open Math Ieee Double library:
  155.  
  156.         clr.l    d0
  157.         lea.l    MathIeeeName(pc),a1
  158.         callsys    OpenLibrary
  159.         tst.l    d0
  160.         beq    bailout
  161.         move.l    d0,(a3)+    ; _MathIeeeDoubBasBase
  162.  
  163.     ;------ Open CCLib library:
  164.  
  165.         clr.l    d0
  166.         lea.l    CCLibName(pc),a1
  167.         callsys    OpenLibrary
  168.         tst.l    d0
  169.         beq    bailout
  170.         move.l    d0,(a3)+    ; _CCLibBase
  171.  
  172.     ;------ get the address of our task
  173.         move.l    ThisTask(a6),a4
  174.  
  175.     ;------ were we started from the Workbench
  176.         tst.l    pr_CLI(A4)
  177.         beq.s    fromWorkbench
  178.  
  179.     ;------ No, then we were started by CLI.
  180.     ;------ so clear out workbench msg pointer
  181.         clr.l    (a3)+        ; returnMsg
  182.         bra.s    skipwbstartup
  183.  
  184. ;
  185. ;------- Do some Workbench specific startup work
  186. ;
  187.  
  188.     ;------ we are now set up.  wait for a message from our starter
  189. fromWorkbench:    ;[a4=ThisTask]
  190. nostartupmsg:
  191.         lea.l    pr_MsgPort(A4),a0    ; our process base
  192.         callsys WaitPort
  193.     ;------ Get the message and save in returnMsg
  194.         lea.l    pr_MsgPort(A4),a0    ; our process base
  195.         callsys GetMsg
  196.         tst.l    d0
  197.         beq.s    nostartupmsg    ; ROM kernal manual says we could
  198.                     ; get a signal but no msg...
  199.  
  200.         move.l    d0,(a3)+     ; returnMsg
  201.  
  202. skipwbstartup:
  203.         clr.l    (a3)+        ; _stdin
  204.         clr.l    (a3)+        ; _stdout
  205.         clr.l    (a3)+        ; _stderr
  206.         clr.l    (a3)+        ; _type
  207.         clr.l    (a3)        ; _blocksize
  208.         lea.l    ss,a3
  209. ;
  210. ;  SetupSTDIO(&stdin, &stdout, &stderr, &errno, &blocksize, &type,
  211. ;        MathIeeeDoubBasBase, dosCmdLen, dosCmdBuf, returnMsg,
  212. ;        _exit)
  213. ;
  214.  
  215.         pea    __exit
  216.         move.l    returnMsg-ss(a3),-(sp)
  217.         move.l    dosCmdBuf,-(SP)
  218.         move.l    dosCmdLen,-(SP)
  219.         move.l    _MathIeeeDoubBasBase-ss(a3),-(sp)
  220.         pea    _type-ss(a3)
  221.         pea    _blocksize-ss(a3)
  222.         pea    _errno-ss(a3)
  223.         pea    _stderr-ss(a3)
  224.         pea    _stdout-ss(a3)
  225.         pea    _stdin-ss(a3)
  226.         jsr    _SetupSTDIO
  227.  
  228.         add.w    #44,sp
  229.         tst.l    d0
  230.         beq.s    bailout
  231.  
  232.     ;------ get pointer to the block of user data (ud) belonging to
  233.     ;------ this task.
  234.  
  235.         jsr    _GetSTDIO    ; put ud into d0
  236.         tst.l    d0
  237.         beq.s    bailout
  238.  
  239.     ;------ push argv and argc from the ud onto the stack
  240.     ;------ and call main()
  241.         move.l    d0,a1
  242.         move.l  ud_argv(a1),-(sp)    ; put ud->_argv onto stack
  243.         clr.l    d0
  244.         move.w    ud_argc(a1),d0    ; get ud->_argc and
  245.         move.l    d0,-(sp)    ; push to stack
  246.  
  247.         jsr _main        ; hand over to C main routine.
  248.  
  249.         clr.l    _errno        ; Successful return code
  250.         bra.s    exit2
  251.  
  252.     ;------ get here if something went wrong during startup.
  253. bailout:
  254.         move.l    #$DEADBEEF,_errno
  255.         bra.s    exit2
  256.  
  257. ************************************************************************
  258. *
  259. *    C Program exit(returncode) Function
  260. *
  261. *
  262. ; a1 - general
  263. ; a3 - Frame pointer
  264. ; a6 - Exec Base pointer
  265. _exit:
  266. __exit:
  267.         move.l    4(SP),_errno     ; save error code
  268. exit2:        lea.l    ss,a3         ; set "frame pointer" back up
  269.  
  270.     ;------ check for an exit function set up by call to atexit().
  271.     ;------ If there is one execute it.
  272.  
  273.         move.l    _exit_fcn-ss(a3),a1
  274.         cmp.l    #0,a1
  275.         beq.s    no_atexit_fcn
  276.         jsr    (a1)
  277.  
  278. no_atexit_fcn:
  279.         move.l    _initialSP-ss(a3),SP    ; restore stack pointer
  280.  
  281.         move.l    4,a6
  282.  
  283.     ;------ If MathIeeeDoubBas.library is open close it
  284.         move.l    _MathIeeeDoubBasBase-ss(a3),a1
  285.         cmp.l    #0,a1
  286.         beq.s    no_MIDB
  287.         callsys CloseLibrary
  288. no_MIDB:
  289.     ;------ If CCLib.library is open close it
  290.         move.l    _CCLibBase-ss(a3),a2
  291.         cmp.l    #0,a2
  292.         beq.s    no_CCLib
  293.     ;------ close all files free memory 
  294.         jsr _ClearSTDIO
  295.         move.l    a2,a1
  296.         callsys CloseLibrary
  297.  
  298. no_CCLib:
  299.     ;------ If DOS library is open close it
  300.         move.l    _DOSBase-ss(a3),a1
  301.         cmp.l    #0,a1
  302.         beq.s    exitToDOS
  303.         callsys CloseLibrary
  304.  
  305. exitToDOS:
  306.         move.l    _errno-ss(a3),d0    ; save return code
  307.         rts
  308. ******* DATA ***********************************************************
  309.  
  310. DOSName:    dc.b 'dos.library',0
  311.         EVEN
  312. MathIeeeName:    dc.b 'mathieeedoubbas.library',0
  313.         EVEN
  314. CCLibName:    dc.b 'CClib.library',0
  315.         EVEN
  316. __math        dc.w 1    ; Set to indicate MathIEEEDoubBas is loaded.
  317.         EVEN
  318.         BSS    b.crt0
  319. ss            ; placemarker
  320. _errno        ds.l 1    ; error number from OS routines
  321. _exit_fcn    ds.l 1    ; exit function ptr. setup by call to atexit
  322. _initialSP    ds.l 1    ; initial stack pointer
  323. _SysBase    ds.l 1    ; exec library base pointer
  324. _DOSBase    ds.l 1    ; dos library base pointer
  325. _MathIeeeDoubBasBase    ds.l 1    ; Math Ieee Double library base pointer
  326. _CCLibBase    ds.l 1    ; CCLib library base pointer
  327. returnMsg    ds.l 1    ; Workbench message, or zero for CLI startup
  328. _stdin        ds.l 1    ;       STANDARD 
  329. _stdout     ds.l 1    ;            I/O FILE
  330. _stderr     ds.l 1    ;            POINTERS
  331. _type        ds.l 1
  332. _blocksize    ds.l 1    ; blocksize global variable
  333.  
  334.         END
  335.