system.pp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2004-2006 by Karoly Balogh
  4. System unit for AmigaOS 3.x/4.x
  5. Uses parts of the Free Pascal 1.0.x for Commodore Amiga/68k port
  6. by Carl Eric Codere and Nils Sjoholm
  7. See the file COPYING.FPC, included in this distribution,
  8. for details about the copyright.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. **********************************************************************}
  13. unit System;
  14. interface
  15. {$define FPC_IS_SYSTEM}
  16. {$define FPC_ANSI_TEXTFILEREC}
  17. {$ifdef cpum68k}
  18. {$define FPC_SYSTEM_HAS_BACKTRACESTR}
  19. {$define FPC_SYSTEM_NO_VERBOSE_THREADERROR}
  20. {$define FPC_SYSTEM_NO_VERBOSE_UNICODEERROR}
  21. {$endif}
  22. {$if defined(AMIGA_V1_0_ONLY) or defined(AMIGA_V1_2_ONLY)}
  23. {$define AMIGA_LEGACY}
  24. {$endif}
  25. {$if defined(AMIGA_LEGACY) or defined(AMIGA_USE_OSHEAP)}
  26. {$define FPC_AMIGA_USE_OSHEAP}
  27. {$endif}
  28. {$ifdef FPC_AMIGA_USE_OSHEAP}
  29. {$define HAS_MEMORYMANAGER}
  30. {$endif FPC_AMIGA_USE_OSHEAP}
  31. {$I systemh.inc}
  32. {$I osdebugh.inc}
  33. {$if defined(cpum68k) and defined(fpusoft)}
  34. {$define fpc_softfpu_interface}
  35. {$i softfpu.pp}
  36. {$undef fpc_softfpu_interface}
  37. {$endif defined(cpum68k) and defined(fpusoft)}
  38. const
  39. {$if defined(AMIGA_V1_0_ONLY)}
  40. AMIGA_OS_MINVERSION = 0;
  41. {$else}
  42. {$if defined(AMIGA_V1_2_ONLY)}
  43. AMIGA_OS_MINVERSION = 33;
  44. {$else}
  45. {$if defined(AMIGA_V2_0_ONLY)}
  46. AMIGA_OS_MINVERSION = 37;
  47. {$else}
  48. {$ifndef cpupowerpc}
  49. AMIGA_OS_MINVERSION = 39;
  50. {$else}
  51. AMIGA_OS_MINVERSION = 50;
  52. {$endif}
  53. {$endif}
  54. {$endif}
  55. {$endif}
  56. const
  57. LineEnding = #10;
  58. LFNSupport = True;
  59. DirectorySeparator = '/';
  60. DriveSeparator = ':';
  61. ExtensionSeparator = '.';
  62. PathSeparator = ';';
  63. AllowDirectorySeparators : set of AnsiChar = ['\','/'];
  64. AllowDriveSeparators : set of AnsiChar = [':'];
  65. maxExitCode = 255;
  66. MaxPathLen = 256;
  67. AllFilesMask = '#?';
  68. const
  69. UnusedHandle : LongInt = -1;
  70. StdInputHandle : LongInt = 0;
  71. StdOutputHandle : LongInt = 0;
  72. StdErrorHandle : LongInt = 0;
  73. FileNameCaseSensitive : Boolean = False;
  74. FileNameCasePreserving: boolean = True;
  75. CtrlZMarksEOF: boolean = false; (* #26 not considered as end of file *)
  76. sLineBreak = LineEnding;
  77. DefaultTextLineBreakStyle : TTextLineBreakStyle = tlbsLF;
  78. BreakOn : Boolean = True;
  79. { FIX ME: remove the kludge required by amunits package, which needs a huge rework }
  80. var
  81. AOS_ExecBase : Pointer; external name '_ExecBase';
  82. _ExecBase: Pointer; external name '_ExecBase'; { amunits compatibility kludge }
  83. AOS_DOSBase : Pointer; public name '_DOSBase'; { the "public" part is amunits compatibility kludge }
  84. _DOSBase: Pointer; external name '_DOSBase'; { amunits compatibility kludge }
  85. AOS_UtilityBase: Pointer; public name '_UtilityBase'; { the "public" part is amunits compatibility kludge }
  86. _UtilityBase: Pointer; external name '_UtilityBase'; { amunits compatibility kludge }
  87. AOS_IntuitionBase: Pointer; public name '_IntuitionBase'; { amunits compatibility kludge }
  88. _IntuitionBase: Pointer; external name '_IntuitionBase'; { amunits compatibility kludge }
  89. {$IFDEF AMIGAOS4}
  90. {$WARNING iExec, iDOS and iUtility should be typed pointer later}
  91. var
  92. IExec : Pointer; external name '_IExec';
  93. IDOS : Pointer;
  94. IUtility : Pointer;
  95. {$ENDIF}
  96. ASYS_heapPool : Pointer; { pointer for the OS pool for growing the heap }
  97. ASYS_heapSemaphore: Pointer; { 68k OS from 3.x has no MEMF_SEM_PROTECTED for pools, have to do it ourselves }
  98. ASYS_fileSemaphore: Pointer; { mutex semaphore for filelist access arbitration }
  99. ASYS_origDir : LongInt; { original directory on startup }
  100. AOS_wbMsg : Pointer; public name '_WBenchMsg'; { the "public" part is amunits compatibility kludge }
  101. _WBenchMsg : Pointer; external name '_WBenchMsg'; { amunits compatibility kludge }
  102. AOS_ConName : PAnsiChar ='CON:10/30/620/100/FPC Console Output/AUTO/CLOSE/WAIT';
  103. AOS_ConHandle: LongInt;
  104. argc: LongInt;
  105. argv: PPAnsiChar;
  106. envp: PPAnsiChar;
  107. implementation
  108. {$if defined(cpum68k) and defined(fpusoft)}
  109. {$define fpc_softfpu_implementation}
  110. {$define softfpu_compiler_mul32to64}
  111. {$define softfpu_inline}
  112. {$i softfpu.pp}
  113. {$undef fpc_softfpu_implementation}
  114. { we get these functions and types from the softfpu code }
  115. {$define FPC_SYSTEM_HAS_float64}
  116. {$define FPC_SYSTEM_HAS_float32}
  117. {$define FPC_SYSTEM_HAS_flag}
  118. {$define FPC_SYSTEM_HAS_extractFloat64Frac0}
  119. {$define FPC_SYSTEM_HAS_extractFloat64Frac1}
  120. {$define FPC_SYSTEM_HAS_extractFloat64Exp}
  121. {$define FPC_SYSTEM_HAS_extractFloat64Sign}
  122. {$define FPC_SYSTEM_HAS_ExtractFloat32Frac}
  123. {$define FPC_SYSTEM_HAS_extractFloat32Exp}
  124. {$define FPC_SYSTEM_HAS_extractFloat32Sign}
  125. {$endif defined(cpum68k) and defined(fpusoft)}
  126. {$ifdef FPC_SYSTEM_HAS_BACKTRACESTR}
  127. var
  128. _start: byte; external name '_start';
  129. { __text_size is provided by the linker }
  130. __text_size: ptruint; external name '___text_size';
  131. var
  132. codestart: pointer;
  133. codeend: pointer;
  134. {$endif FPC_SYSTEM_HAS_BACKTRACESTR}
  135. {$I system.inc}
  136. {$ifdef FPC_AMIGA_USE_OSHEAP}
  137. {$i osheap.inc}
  138. {$endif FPC_AMIGA_USE_OSHEAP}
  139. {$I osdebug.inc}
  140. {$IFDEF AMIGAOS4}
  141. // Required to allow opening of utility library interface...
  142. {$include utilf.inc}
  143. {$ENDIF}
  144. {$IFDEF ASYS_FPC_FILEDEBUG}
  145. {$WARNING Compiling with file debug enabled!}
  146. {$ENDIF}
  147. {$IFDEF ASYS_FPC_MEMDEBUG}
  148. {$WARNING Compiling with memory debug enabled!}
  149. {$ENDIF}
  150. type
  151. PWBArg = ^TWBArg;
  152. TWBArg = record
  153. wa_Lock : LongInt; { a lock descriptor }
  154. wa_Name : PAnsiChar; { a string relative to that lock }
  155. end;
  156. WBArgList = array[1..MaxInt] of TWBArg; { Only 1..smNumArgs are valid }
  157. PWBArgList = ^WBArgList;
  158. PWBStartup = ^TWBStartup;
  159. TWBStartup = record
  160. sm_Message : TMessage; { a standard message structure }
  161. sm_Process : Pointer; { the process descriptor for you }
  162. sm_Segment : Pointer; { a descriptor for your code }
  163. sm_NumArgs : Longint; { the number of elements in ArgList }
  164. sm_ToolWindow : Pointer; { description of window }
  165. sm_ArgList : PWBArgList; { the arguments themselves }
  166. end;
  167. {*****************************************************************************
  168. Misc. System Dependent Functions
  169. *****************************************************************************}
  170. procedure haltproc(e:longint);cdecl;external name '_haltproc';
  171. procedure System_exit;
  172. var
  173. oldDirLock: LongInt;
  174. begin
  175. { Dispose the thread init/exit chains }
  176. CleanupThreadProcChain(threadInitProcList);
  177. CleanupThreadProcChain(threadExitProcList);
  178. { We must remove the CTRL-C FLAG here because halt }
  179. { may call I/O routines, which in turn might call }
  180. { halt, so a recursive stack crash }
  181. if BreakOn then begin
  182. if (SetSignal(0,0) and SIGBREAKF_CTRL_C)<>0 then
  183. SetSignal(0,SIGBREAKF_CTRL_C);
  184. end;
  185. { Closing opened files }
  186. CloseList(ASYS_fileList);
  187. { Changing back to original directory if changed }
  188. if ASYS_origDir<>0 then begin
  189. oldDirLock:=CurrentDir(ASYS_origDir);
  190. { unlock our lock if its safe, so we won't leak the lock }
  191. if (oldDirLock<>0) and (oldDirLock<>ASYS_origDir) then
  192. Unlock(oldDirLock);
  193. end;
  194. {$IFDEF AMIGAOS4}
  195. if iDOS<>nil then DropInterface(iDOS);
  196. if iUtility<>nil then DropInterface(iUtility);
  197. {$ENDIF}
  198. if AOS_IntuitionBase<>nil then CloseLibrary(AOS_IntuitionBase); { amunits kludge }
  199. if AOS_UtilityBase<>nil then CloseLibrary(AOS_UtilityBase);
  200. if AOS_DOSBase<>nil then CloseLibrary(AOS_DOSBase);
  201. if ASYS_heapPool<>nil then DeletePool(ASYS_heapPool);
  202. { If in Workbench mode, replying WBMsg }
  203. if AOS_wbMsg<>nil then begin
  204. Forbid;
  205. ReplyMsg(AOS_wbMsg);
  206. end;
  207. haltproc(ExitCode);
  208. end;
  209. {*****************************************************************************
  210. Parameterhandling
  211. as include in amicommon
  212. *****************************************************************************}
  213. {$I paramhandling.inc}
  214. {*****************************************************************************
  215. Randomize
  216. *****************************************************************************}
  217. { set randseed to a new pseudo random value }
  218. procedure randomize;
  219. var tmpTime: TDateStamp;
  220. begin
  221. DateStamp(@tmpTime);
  222. randseed:=tmpTime.ds_tick;
  223. end;
  224. {$IFDEF FPC_AMIGA_USE_OSHEAP}
  225. var
  226. { generated by the compiler based on the $MEMORY directive }
  227. heapsize : PtrInt; external name '__heapsize';
  228. {$ENDIF FPC_AMIGA_USE_OSHEAP}
  229. { AmigaOS specific startup }
  230. procedure SysInitAmigaOS;
  231. var self: PProcess;
  232. begin
  233. self:=PProcess(FindTask(nil));
  234. if self^.pr_CLI=0 then begin
  235. { if we're running from Ambient/Workbench, we catch its message }
  236. WaitPort(@self^.pr_MsgPort);
  237. AOS_wbMsg:=GetMsg(@self^.pr_MsgPort);
  238. end;
  239. AOS_DOSBase:=OpenLibrary('dos.library',AMIGA_OS_MINVERSION);
  240. if AOS_DOSBase=nil then Halt(1);
  241. {$ifndef AMIGA_LEGACY}
  242. AOS_UtilityBase:=OpenLibrary('utility.library',AMIGA_OS_MINVERSION);
  243. if AOS_UtilityBase=nil then Halt(1);
  244. {$endif}
  245. AOS_IntuitionBase:=OpenLibrary('intuition.library',AMIGA_OS_MINVERSION); { amunits support kludge }
  246. if AOS_IntuitionBase=nil then Halt(1);
  247. {$IFDEF AMIGAOS4}
  248. { Open main interfaces on OS4 }
  249. iDOS := GetInterface(AOS_DOSBase,'main',1,nil);
  250. iUtility := GetInterface(AOS_UtilityBase,'main',1,nil);
  251. {$ENDIF}
  252. { Creating the memory pool for growing heap }
  253. {$IFNDEF FPC_AMIGA_USE_OSHEAP}
  254. ASYS_heapPool:=CreatePool(MEMF_ANY,growheapsize2,growheapsize1);
  255. {$ELSE FPC_AMIGA_USE_OSHEAP}
  256. ASYS_heapPool:=CreatePool(MEMF_ANY,min(heapsize,1024),min(heapsize div 2,1024));
  257. {$ENDIF FPC_AMIGA_USE_OSHEAP}
  258. if ASYS_heapPool=nil then Halt(1);
  259. ASYS_heapSemaphore:=AllocPooled(ASYS_heapPool,sizeof(TSignalSemaphore));
  260. if ASYS_heapSemaphore = nil then Halt(1);
  261. InitSemaphore(ASYS_heapSemaphore);
  262. { Initialize semaphore for filelist access arbitration }
  263. ASYS_fileSemaphore:=AllocPooled(ASYS_heapPool,sizeof(TSignalSemaphore));
  264. if ASYS_fileSemaphore = nil then Halt(1);
  265. InitSemaphore(ASYS_fileSemaphore);
  266. if AOS_wbMsg=nil then begin
  267. StdInputHandle:=dosInput;
  268. StdOutputHandle:=dosOutput;
  269. StdErrorHandle:=StdOutputHandle;
  270. end else begin
  271. AOS_ConHandle:=Open(AOS_ConName,MODE_OLDFILE);
  272. if AOS_ConHandle<>0 then begin
  273. StdInputHandle:=AOS_ConHandle;
  274. StdOutputHandle:=AOS_ConHandle;
  275. StdErrorHandle:=AOS_ConHandle;
  276. end else
  277. Halt(1);
  278. end;
  279. end;
  280. procedure SysInitStdIO;
  281. begin
  282. OpenStdIO(Input,fmInput,StdInputHandle);
  283. OpenStdIO(Output,fmOutput,StdOutputHandle);
  284. OpenStdIO(ErrOutput,fmOutput,StdErrorHandle);
  285. {$ifndef FPC_STDOUT_TRUE_ALIAS}
  286. OpenStdIO(StdOut,fmOutput,StdOutputHandle);
  287. OpenStdIO(StdErr,fmOutput,StdErrorHandle);
  288. {$endif FPC_STDOUT_TRUE_ALIAS}
  289. end;
  290. function GetProcessID: SizeUInt;
  291. begin
  292. GetProcessID:=SizeUInt(FindTask(NIL));
  293. end;
  294. function CheckInitialStkLen(stklen : SizeUInt) : SizeUInt;
  295. begin
  296. result := stklen;
  297. end;
  298. begin
  299. IsConsole := TRUE;
  300. StackLength := CheckInitialStkLen(InitialStkLen);
  301. StackBottom := StackTop - StackLength;
  302. {$ifdef FPC_SYSTEM_HAS_BACKTRACESTR}
  303. codestart := @_start;
  304. codeend := pointer(ptruint(@_start) + ptruint(@__text_size));
  305. {$endif FPC_SYSTEM_HAS_BACKTRACESTR}
  306. { OS specific startup }
  307. AOS_wbMsg:=nil;
  308. ASYS_origDir:=0;
  309. ASYS_fileList:=nil;
  310. envp:=nil;
  311. SysInitAmigaOS;
  312. { Set up signals handlers }
  313. // InstallSignals;
  314. {$ifndef FPC_AMIGA_USE_OSHEAP}
  315. { Setup heap }
  316. InitHeap;
  317. {$endif FPC_AMIGA_USE_OSHEAP}
  318. SysInitExceptions;
  319. {$ifdef cpum68k}
  320. fpc_cpucodeinit;
  321. {$endif}
  322. initunicodestringmanager;
  323. { Setup stdin, stdout and stderr }
  324. SysInitStdIO;
  325. { Reset IO Error }
  326. InOutRes:=0;
  327. { Arguments }
  328. GenerateArgs;
  329. InitSystemThreads;
  330. {$ifdef FPC_HAS_FEATURE_DYNLIBS}
  331. InitSystemDynLibs;
  332. {$endif}
  333. end.