si_prc.pp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2020 by Karoly Balogh
  4. System Entry point for the Sinclair QL
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. unit si_prc;
  12. interface
  13. implementation
  14. {$i qdosfuncs.inc}
  15. var
  16. stacktop: pointer;
  17. stackorig: pointer;
  18. setjmpbuf: jmp_buf;
  19. stklen: longint; external name '__stklen';
  20. binstart: pointer; external name '_stext';
  21. binend: pointer; external name '_etext';
  22. procedure PascalMain; external name 'PASCALMAIN';
  23. { this function must be the first in this unit which contains code }
  24. {$OPTIMIZATION OFF}
  25. function _FPC_proc_start: longint; cdecl; public name '_start';
  26. var
  27. newstack: pointer;
  28. begin
  29. _FPC_proc_start:=0;
  30. asm
  31. move.l d7,-(sp)
  32. { relocation code }
  33. { get our actual position in RAM }
  34. lea.l binstart(pc),a0
  35. move.l a0,d0
  36. { get an offset to the end of the binary. this depends on the
  37. fact that at this point the binary is not relocated yet }
  38. lea.l binend,a1
  39. add.l d0,a1
  40. { first item in the relocation table is the number of relocs }
  41. move.l (a1),d7
  42. beq @noreloc
  43. { zero out the number of relocs in RAM, so if our code is
  44. called again, without reload, it won't relocate itself twice }
  45. move.l #0,(a1)+
  46. @relocloop:
  47. { we read the offsets and relocate them }
  48. move.l (a1)+,d1
  49. add.l d0,(a0,d1)
  50. subq.l #1,d7
  51. bne @relocloop
  52. @noreloc:
  53. move.l (sp)+,d7
  54. { save the original stack pointer }
  55. move.l a7,stackorig
  56. end;
  57. newstack:=mt_alchp(stklen,nil,-1);
  58. if not assigned(newstack) then
  59. _FPC_proc_start:=ERR_OM
  60. else
  61. begin
  62. asm
  63. move.l newstack,sp
  64. end;
  65. if setjmp(setjmpbuf) = 0 then
  66. PascalMain;
  67. asm
  68. move.l stackorig,sp
  69. end;
  70. mt_rechp(newstack);
  71. end;
  72. end;
  73. procedure _FPC_proc_halt(_ExitCode: longint); public name '_haltproc';
  74. begin
  75. longjmp(setjmpbuf,1);
  76. end;
  77. end.