systems.pas 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141
  1. {
  2. Copyright (c) 1998-2008 by Florian Klaempfl
  3. This unit contains information about the target systems supported
  4. (these are not processor specific)
  5. This program is free software; you can redistribute it and/or modify
  6. iu under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  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. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge- MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit systems;
  19. {$i fpcdefs.inc}
  20. interface
  21. {$i systems.inc}
  22. {*****************************************************************************
  23. Structures
  24. *****************************************************************************}
  25. type
  26. TAbstractResourceFile = class
  27. constructor create(const fn : ansistring);virtual;abstract;
  28. end;
  29. TAbstractResourceFileClass = class of TAbstractResourceFile;
  30. palignmentinfo = ^talignmentinfo;
  31. { this is written to ppus during token recording for generics so it must be packed }
  32. talignmentinfo = packed record
  33. procalign,
  34. loopalign,
  35. { alignment for labels after unconditional jumps, this must be a power of two }
  36. jumpalign,
  37. { max. alignment for labels after unconditional jumps:
  38. the compiler tries to align jumpalign, however, to do so it inserts at maximum jumpalignskipmax bytes or uses
  39. the next smaller power of two of jumpalign }
  40. jumpalignskipmax,
  41. { alignment for labels where two flows of the program flow coalesce, this must be a power of two }
  42. coalescealign,
  43. { max. alignment for labels where two flows of the program flow coalesce
  44. the compiler tries to align to coalescealign, however, to do so it inserts at maximum coalescealignskipmax bytes or uses
  45. the next smaller power of two of coalescealign }
  46. coalescealignskipmax,
  47. constalignmin,
  48. constalignmax,
  49. varalignmin,
  50. varalignmax,
  51. localalignmin,
  52. localalignmax,
  53. recordalignmin,
  54. recordalignmax,
  55. maxCrecordalign : longint;
  56. end;
  57. tasmflags = (af_none
  58. ,af_outputbinary
  59. ,af_needar
  60. ,af_smartlink_sections
  61. ,af_labelprefix_only_inside_procedure
  62. ,af_supports_dwarf
  63. ,af_no_debug
  64. ,af_stabs_use_function_absolute_addresses
  65. ,af_no_stabs
  66. ,af_llvm
  67. );
  68. pasminfo = ^tasminfo;
  69. tasminfo = record
  70. id : tasm;
  71. idtxt : string[12];
  72. asmbin : string[16];
  73. asmcmd : string[70];
  74. supported_targets : set of tsystem;
  75. flags : set of tasmflags;
  76. labelprefix : string[3];
  77. comment : string[3];
  78. { set to '$' if that character is allowed in symbol names, otherwise
  79. to alternate character by which '$' should be replaced }
  80. dollarsign : char;
  81. end;
  82. parinfo = ^tarinfo;
  83. tarinfo = record
  84. id : tar;
  85. addfilecmd : string[10];
  86. arfirstcmd : string[50];
  87. arcmd : string[50];
  88. arfinishcmd : string[10];
  89. end;
  90. presinfo = ^tresinfo;
  91. tresinfo = record
  92. id : tres;
  93. { Compiler for resource (.rc or .res) to obj }
  94. resbin : string[10];
  95. rescmd : string[50];
  96. { Optional compiler for resource script (.rc) to binary resource (.res). }
  97. { If it is not provided resbin and rescmd will be used. }
  98. rcbin : string[10];
  99. rccmd : string[50];
  100. resourcefileclass : TAbstractResourceFileClass;
  101. resflags : set of tresinfoflags;
  102. end;
  103. pdbginfo = ^tdbginfo;
  104. tdbginfo = record
  105. id : tdbg;
  106. idtxt : string[12];
  107. end;
  108. tsystemflags = (tf_none,
  109. tf_under_development,
  110. tf_need_export,
  111. tf_needs_isconsole,
  112. tf_code_small,
  113. tf_static_reg_based,
  114. tf_needs_symbol_size,
  115. tf_smartlink_sections,
  116. tf_smartlink_library,
  117. tf_needs_dwarf_cfi,
  118. tf_use_8_3,
  119. tf_pic_uses_got,
  120. tf_library_needs_pic,
  121. tf_needs_symbol_type,
  122. tf_section_threadvars,
  123. tf_files_case_sensitive,
  124. tf_files_case_aware,
  125. tf_p_ext_support,
  126. tf_has_dllscanner,
  127. tf_use_function_relative_addresses,
  128. tf_winlikewidestring,
  129. tf_dwarf_relative_addresses, // use offsets where the Dwarf spec requires this instead of absolute addresses (the latter is needed by Linux binutils)
  130. tf_dwarf_only_local_labels, // only use local labels inside the Dwarf debug_info section (needed for e.g. Darwin)
  131. tf_requires_proper_alignment,
  132. tf_no_pic_supported,
  133. tf_pic_default,
  134. { the os does some kind of stack checking and it can be converted into a rte 202 }
  135. tf_no_generic_stackcheck,
  136. tf_emit_stklen, // Means that the compiler should emit a _stklen variable with the stack size, even if tf_no_generic_stackcheck is specified
  137. tf_has_winlike_resources,
  138. tf_safecall_clearstack, // With this flag set, after safecall calls the caller cleans up the stack
  139. tf_safecall_exceptions, // Exceptions in safecall calls are not raised, but passed to the caller as an ordinal (hresult) in the function result.
  140. // The original result (if it exists) is passed as an extra parameter
  141. tf_no_backquote_support,
  142. { do not generate an object file when smartlinking is turned on,
  143. this is usefull for architectures which require a small code footprint }
  144. tf_no_objectfiles_when_smartlinking,
  145. { indicates that the default value of the ts_cld target switch is 'on' for this target }
  146. tf_cld,
  147. { indicates that the default value of the ts_x86_far_procs_push_odd_bp target switch is 'on' for this target }
  148. tf_x86_far_procs_push_odd_bp,
  149. { indicates that this target can use dynamic packages otherwise an
  150. error will be generated if a package file is compiled }
  151. tf_supports_packages,
  152. { use PSABI/Dwarf-based "zero cost" exception handling }
  153. tf_use_psabieh,
  154. { use high level cfi directives to generate call frame information }
  155. tf_use_hlcfi,
  156. { supports symbol order file (to ensure symbols in vectorised sections are kept in the correct order) }
  157. tf_supports_symbolorderfile,
  158. { supports hidden/private extern symbols: visible across object files, but local/private in exe/library }
  159. tf_supports_hidden_symbols,
  160. { units are initialized by direct calls and not table driven,
  161. in particular for a small amount of units, this results in smaller
  162. executables }
  163. tf_init_final_units_by_calls
  164. );
  165. psysteminfo = ^tsysteminfo;
  166. { using packed causes bus errors on processors which require alignment }
  167. tsysteminfo = record
  168. system : tsystem;
  169. name : string[34];
  170. shortname : string[9];
  171. flags : set of tsystemflags;
  172. cpu : tsystemcpu;
  173. unit_env : string[16];
  174. extradefines : string[40];
  175. exeext,
  176. defext,
  177. scriptext,
  178. smartext,
  179. unitext,
  180. unitlibext,
  181. asmext : string[4];
  182. objext : string[6];
  183. resext : string[4];
  184. resobjext : string[7];
  185. sharedlibext : string[10];
  186. staticlibext,
  187. staticlibprefix : string[4];
  188. sharedlibprefix : string[4];
  189. sharedClibext : string[10];
  190. staticClibext,
  191. staticClibprefix : string[4];
  192. sharedClibprefix : string[4];
  193. importlibprefix : string[10];
  194. importlibext : string[4];
  195. Cprefix : string[2];
  196. newline : string[2];
  197. dirsep : char;
  198. assem : tasm;
  199. assemextern : tasm; { external assembler, used by -a }
  200. link : tlink;
  201. linkextern : tlink; { external linker, used by -s }
  202. ar : tar;
  203. res : tres;
  204. dbg : tdbg;
  205. script : tscripttype;
  206. endian : tendian;
  207. alignment : talignmentinfo;
  208. {
  209. Offset from the argument pointer register to the first
  210. argument's address. On some machines it may depend on
  211. the data type of the function.
  212. (see also FIRST_PARM_OFFSET in GCC source)
  213. }
  214. first_parm_offset : longint;
  215. stacksize : longint;
  216. { stack alignment }
  217. stackalign : byte;
  218. abi : tabi;
  219. { llvm -- varies wildly in length and is empty for many targets ->
  220. ansistring instead of shortstring; tsysteminfo records aren't
  221. copied very often anyway. These strings come from the file
  222. lib/Basic/Targets.cpp in the clang (cfe 3.3) source tree, sometimes
  223. adapted to match our (custom) stack alignment requirements }
  224. llvmdatalayout: ansistring;
  225. end;
  226. tabiinfo = record
  227. name: string[13];
  228. supported: boolean;
  229. end;
  230. {$push}
  231. {$j-}
  232. const
  233. { alias for supported_target field in tasminfo }
  234. system_any = system_none;
  235. systems_wince = [system_arm_wince,system_i386_wince];
  236. systems_android = [system_arm_android, system_aarch64_android, system_i386_android, system_x86_64_android, system_mipsel_android];
  237. systems_linux = [system_i386_linux,system_x86_64_linux,system_powerpc_linux,system_powerpc64_linux,
  238. system_arm_linux,system_sparc_linux,system_sparc64_linux,system_m68k_linux,
  239. system_x86_6432_linux,system_mipseb_linux,system_mipsel_linux,system_aarch64_linux,
  240. system_riscv32_linux,system_riscv64_linux,system_xtensa_linux];
  241. systems_dragonfly = [system_x86_64_dragonfly];
  242. systems_freebsd = [system_i386_freebsd,
  243. system_x86_64_freebsd];
  244. systems_netbsd = [system_i386_netbsd,
  245. system_m68k_netbsd,
  246. system_powerpc_netbsd,
  247. system_x86_64_netbsd,
  248. system_arm_netbsd];
  249. systems_openbsd = [system_i386_openbsd,
  250. system_x86_64_openbsd];
  251. systems_bsd = systems_freebsd + systems_netbsd + systems_openbsd + systems_dragonfly;
  252. systems_aix = [system_powerpc_aix,system_powerpc64_aix];
  253. { all real windows systems, no cripple ones like win16, wince, wdosx et. al. }
  254. systems_windows = [system_i386_win32,system_x86_64_win64];
  255. { all windows systems }
  256. systems_all_windows = [system_i386_win32,system_x86_64_win64,
  257. system_arm_wince,system_i386_wince,
  258. system_i8086_win16];
  259. { all darwin systems }
  260. systems_darwin = [system_powerpc_darwin,system_i386_darwin,
  261. system_powerpc64_darwin,system_x86_64_darwin,
  262. system_arm_darwin,system_i386_iphonesim,
  263. system_aarch64_darwin,system_x86_64_iphonesim];
  264. {all solaris systems }
  265. systems_solaris = [system_sparc_solaris, system_i386_solaris,
  266. system_x86_64_solaris];
  267. { all embedded systems }
  268. systems_embedded = [system_i386_embedded,system_m68k_embedded,
  269. system_powerpc_embedded,
  270. system_sparc_embedded,obsolete_system_vm_embedded,
  271. obsolete_system_ia64_embedded,system_x86_64_embedded,
  272. system_mips_embedded,system_arm_embedded,
  273. system_powerpc64_embedded,system_avr_embedded,
  274. system_jvm_java32,system_mipseb_embedded,system_mipsel_embedded,
  275. system_i8086_embedded,system_riscv32_embedded,system_riscv64_embedded,
  276. system_xtensa_embedded];
  277. { all FreeRTOS systems }
  278. systems_freertos = [system_xtensa_freertos];
  279. { all systems that allow section directive }
  280. systems_allow_section = systems_embedded;
  281. { systems that uses dotted function names as descriptors }
  282. systems_dotted_function_names = [system_powerpc64_linux]+systems_aix;
  283. systems_allow_section_no_semicolon = systems_allow_section
  284. {$ifndef DISABLE_TLS_DIRECTORY}
  285. + systems_windows
  286. {$endif not DISABLE_TLS_DIRECTORY}
  287. ;
  288. { systems that allow external far variables }
  289. systems_allow_external_far_var = [system_i8086_msdos,system_i8086_win16,system_i8086_embedded];
  290. { all symbian systems }
  291. systems_symbian = [system_i386_symbian,system_arm_symbian];
  292. { all classic Mac OS targets }
  293. systems_macos = [system_m68k_macos,system_powerpc_macos];
  294. { all OS/2 targets }
  295. systems_os2 = [system_i386_OS2,system_i386_emx];
  296. { AROS systems }
  297. systems_aros = [system_i386_aros,system_x86_64_aros,system_arm_aros];
  298. { all amiga like systems }
  299. systems_amigalike = [system_m68k_amiga,system_powerpc_morphos,system_powerpc_amiga]+systems_aros;
  300. { all native nt systems }
  301. systems_nativent = [system_i386_nativent];
  302. { systems supporting Objective-C }
  303. systems_objc_supported = systems_darwin;
  304. { systems using the non-fragile Objective-C ABI }
  305. systems_objc_nfabi = [system_powerpc64_darwin,system_x86_64_darwin,system_arm_darwin,system_i386_iphonesim,system_aarch64_darwin,system_x86_64_iphonesim];
  306. { systems supporting "blocks" }
  307. systems_blocks_supported = systems_darwin;
  308. { all systems supporting exports from programs or units }
  309. systems_unit_program_exports = [system_i386_win32,
  310. system_i386_wdosx,
  311. system_i386_Netware,
  312. system_i386_netwlibc,
  313. system_arm_wince,
  314. system_x86_64_win64,
  315. system_i8086_win16]+systems_linux+systems_android;
  316. { all systems that reference symbols in other binaries using indirect imports }
  317. systems_indirect_var_imports = systems_all_windows+[system_i386_nativent];
  318. { all systems that support indirect entry information }
  319. systems_indirect_entry_information = systems_darwin+[system_i386_win32,system_x86_64_win64,system_x86_64_linux];
  320. { all systems for which weak linking has been tested/is supported }
  321. systems_weak_linking = systems_darwin + systems_solaris + systems_linux + systems_android + systems_openbsd + systems_freebsd;
  322. systems_internal_sysinit = [system_i386_win32,system_x86_64_win64,
  323. system_i386_linux,system_powerpc64_linux,system_sparc64_linux,system_x86_64_linux,
  324. system_xtensa_linux,
  325. system_m68k_atari,system_m68k_palmos,
  326. system_i386_haiku,system_x86_64_haiku,
  327. system_i386_openbsd,system_x86_64_openbsd,
  328. system_riscv32_linux,system_riscv64_linux
  329. ]+systems_darwin+systems_amigalike;
  330. { all systems that use garbage collection for reference-counted types }
  331. systems_garbage_collected_managed_types = [
  332. system_jvm_java32,
  333. system_jvm_android32
  334. ];
  335. { all systems that use a managed vm (-> no real pointers, internal VMT
  336. format, ...) }
  337. systems_managed_vm = [
  338. system_jvm_java32,
  339. system_jvm_android32
  340. ];
  341. { all systems based on the JVM }
  342. systems_jvm = [
  343. system_jvm_java32,
  344. system_jvm_android32
  345. ];
  346. { all systems where typed constants have to be translated into node
  347. trees that initialise the data instead of into data sections }
  348. systems_typed_constants_node_init = [
  349. system_jvm_java32,
  350. system_jvm_android32
  351. ];
  352. { all systems that don't use a built-in framepointer for accessing nested
  353. variables, but emulate it by wrapping nested variables in records
  354. whose address is passed around }
  355. systems_fpnestedstruct = [
  356. {$ifndef llvm}
  357. system_jvm_java32,
  358. system_jvm_android32
  359. {$else not llvm}
  360. low(tsystem)..high(tsystem)
  361. {$endif not llvm}
  362. ];
  363. { all systems where a value parameter passed by reference must be copied
  364. on the caller side rather than on the callee side }
  365. systems_caller_copy_addr_value_para = [system_aarch64_darwin,system_aarch64_linux];
  366. { pointer checking (requires special code in FPC_CHECKPOINTER,
  367. and can never work for libc-based targets or any other program
  368. linking to an external library)
  369. }
  370. systems_support_checkpointer = systems_linux
  371. + [system_i386_win32]
  372. + [system_i386_GO32V2]
  373. + [system_i386_os2]
  374. + [system_i386_beos,system_i386_haiku]
  375. + [system_powerpc_morphos];
  376. cpu2str : array[TSystemCpu] of string[10] =
  377. ('','i386','m68k','alpha','powerpc','sparc','vm','ia64','x86_64',
  378. 'mips','arm', 'powerpc64', 'avr', 'mipsel','jvm', 'i8086',
  379. 'aarch64', 'wasm', 'sparc64', 'riscv32', 'riscv64', 'xtensa');
  380. abiinfo : array[tabi] of tabiinfo = (
  381. (name: 'DEFAULT'; supported: true),
  382. (name: 'SYSV' ; supported:{$if defined(powerpc) or defined(powerpc64)}true{$else}false{$endif}),
  383. (name: 'AIX' ; supported:{$if defined(powerpc) or defined(powerpc64)}true{$else}false{$endif}),
  384. (name: 'DARWIN' ; supported:{$if defined(powerpc) or defined(powerpc64)}true{$else}false{$endif}),
  385. (name: 'ELFV2' ; supported:{$if defined(powerpc64)}true{$else}false{$endif}),
  386. (name: 'EABI' ; supported:{$if defined(arm)}true{$else}false{$endif}),
  387. (name: 'ARMEB' ; supported:{$ifdef FPC_ARMEB}true{$else}false{$endif}),
  388. (name: 'EABIHF' ; supported:{$if defined(arm)}true{$else}false{$endif}),
  389. (name: 'OLDWIN32GNU'; supported:{$ifdef I386}true{$else}false{$endif}),
  390. (name: 'AARCH64IOS'; supported:{$ifdef aarch64}true{$else}false{$endif}),
  391. (name: 'RISCVHF'; supported:{$if defined(riscv32) or defined(riscv64)}true{$else}false{$endif}),
  392. (name: 'LINUX386_SYSV'; supported:{$if defined(i386)}true{$else}false{$endif}),
  393. (name: 'WINDOWED'; supported:{$if defined(xtensa)}true{$else}false{$endif}),
  394. (name: 'CALL0'; supported:{$if defined(xtensa)}true{$else}false{$endif})
  395. );
  396. cgbackend2str: array[tcgbackend] of ansistring = (
  397. 'FPC',
  398. 'LLVM'
  399. );
  400. { x86 asm modes with an Intel-style syntax }
  401. asmmodes_x86_intel = [
  402. {$ifdef i8086}
  403. asmmode_standard,
  404. {$endif i8086}
  405. asmmode_i8086_intel,
  406. asmmode_i386_intel,
  407. asmmode_x86_64_intel
  408. ];
  409. { x86 asm modes with an AT&T-style syntax }
  410. asmmodes_x86_att = [
  411. {$if defined(i386) or defined(x86_64)}
  412. asmmode_standard,
  413. {$endif}
  414. asmmode_i8086_att,
  415. asmmode_i386_att,
  416. asmmode_x86_64_att,
  417. asmmode_x86_64_gas
  418. ];
  419. {$pop}
  420. var
  421. targetinfos : array[tsystem] of psysteminfo;
  422. arinfos : array[tar] of parinfo;
  423. resinfos : array[tres] of presinfo;
  424. asminfos : array[tasm] of pasminfo;
  425. dbginfos : array[tdbg] of pdbginfo;
  426. source_info : tsysteminfo;
  427. target_cpu : tsystemcpu;
  428. target_info : tsysteminfo;
  429. target_asm : tasminfo;
  430. target_ar : tarinfo;
  431. target_res : tresinfo;
  432. target_dbg : tdbginfo;
  433. target_cpu_string,
  434. target_os_string : string[12]; { for rtl/<X>/,fcl/<X>/, etc. }
  435. target_full_string : string[24];
  436. function set_target(t:tsystem):boolean;
  437. function set_target_asm(t:tasm):boolean;
  438. function set_target_ar(t:tar):boolean;
  439. function set_target_res(t:tres):boolean;
  440. function set_target_dbg(t:tdbg):boolean;
  441. function find_system_by_string(const s : string) : tsystem;
  442. function find_asm_by_string(const s : string) : tasm;
  443. function find_dbg_by_string(const s : string) : tdbg;
  444. procedure set_source_info(const ti : tsysteminfo);
  445. function UpdateAlignment(var d:talignmentinfo;const s:talignmentinfo) : boolean;
  446. procedure RegisterTarget(const r:tsysteminfo);
  447. procedure RegisterRes(const r:tresinfo; rcf : TAbstractResourceFileClass);
  448. procedure RegisterAr(const r:tarinfo);
  449. procedure InitSystems;
  450. {$ifdef FreeBSD}
  451. function GetOSRelDate:Longint;
  452. {$endif}
  453. implementation
  454. uses
  455. cutils{$ifdef FreeBSD},SysCtl,BaseUnix{$endif};
  456. {****************************************************************************
  457. OS runtime version detection utility routine
  458. ****************************************************************************}
  459. {$ifdef FreeBSD}
  460. function GetOSRelDate:Longint;
  461. { FPSysCtl first argument was of type pchar
  462. up to commit 35566 from 2017/03/11
  463. and corrected to pcint in that commit.
  464. But the following code needs to work with
  465. both old 3.0.X definition and new definition using pcint type.
  466. Problem solved using a special type called
  467. FPSysCtlFirstArgType. }
  468. {$if defined(VER3_0_0) or defined(VER3_0_2)}
  469. type
  470. FPSysCtlFirstArgType = PChar;
  471. {$else}
  472. type
  473. FPSysCtlFirstArgType = pcint;
  474. {$endif}
  475. var
  476. mib : array[0..1] of cint;
  477. rval : cint;
  478. len : size_t;
  479. i : longint;
  480. v : longint;
  481. oerrno : cint;
  482. S : AnsiString;
  483. Begin
  484. s:='ab';
  485. SetLength(S,50);
  486. mib[0] := CTL_KERN;
  487. mib[1] := KERN_OSRELDATE;
  488. len := 4;
  489. oerrno:= fpgeterrno;
  490. if (FPsysctl(FPSysCtlFirstArgType(@mib), 2, pchar(@v), @len, NIL, 0) = -1) Then
  491. Begin
  492. if (fpgeterrno = ESysENOMEM) Then
  493. fpseterrno(oerrno);
  494. GetOSRelDate:=0;
  495. End
  496. else
  497. GetOSRelDate:=v;
  498. End;
  499. {$endif}
  500. {****************************************************************************
  501. Target setting
  502. ****************************************************************************}
  503. function set_target(t:tsystem):boolean;
  504. begin
  505. set_target:=false;
  506. if assigned(targetinfos[t]) then
  507. begin
  508. target_info:=targetinfos[t]^;
  509. set_target_asm(target_info.assem);
  510. set_target_ar(target_info.ar);
  511. set_target_res(target_info.res);
  512. set_target_dbg(target_info.dbg);
  513. target_cpu:=target_info.cpu;
  514. target_os_string:=lower(target_info.shortname);
  515. target_cpu_string:=cpu2str[target_cpu];
  516. target_full_string:=target_cpu_string+'-'+target_os_string;
  517. set_target:=true;
  518. exit;
  519. end;
  520. end;
  521. function set_target_asm(t:tasm):boolean;
  522. begin
  523. set_target_asm:=false;
  524. if assigned(asminfos[t]) and
  525. ((target_info.system in asminfos[t]^.supported_targets) or
  526. (system_any in asminfos[t]^.supported_targets)) then
  527. begin
  528. target_asm:=asminfos[t]^;
  529. set_target_asm:=true;
  530. exit;
  531. end;
  532. end;
  533. function set_target_ar(t:tar):boolean;
  534. begin
  535. result:=false;
  536. if assigned(arinfos[t]) then
  537. begin
  538. target_ar:=arinfos[t]^;
  539. result:=true;
  540. exit;
  541. end;
  542. end;
  543. function set_target_res(t:tres):boolean;
  544. begin
  545. result:=false;
  546. if assigned(resinfos[t]) then
  547. begin
  548. target_res:=resinfos[t]^;
  549. result:=true;
  550. exit;
  551. end
  552. else
  553. FillByte(target_res,sizeof(target_res),0);
  554. end;
  555. function set_target_dbg(t:tdbg):boolean;
  556. begin
  557. result:=false;
  558. { no debugging support for llvm yet }
  559. {$ifndef llvm}
  560. if assigned(dbginfos[t]) then
  561. begin
  562. target_dbg:=dbginfos[t]^;
  563. result:=true;
  564. exit;
  565. end;
  566. {$endif}
  567. end;
  568. function find_system_by_string(const s : string) : tsystem;
  569. var
  570. hs : string;
  571. t : tsystem;
  572. begin
  573. result:=system_none;
  574. hs:=upper(s);
  575. for t:=low(tsystem) to high(tsystem) do
  576. if assigned(targetinfos[t]) and
  577. (upper(targetinfos[t]^.shortname)=hs) then
  578. begin
  579. result:=t;
  580. exit;
  581. end;
  582. end;
  583. function find_asm_by_string(const s : string) : tasm;
  584. var
  585. hs : string;
  586. t : tasm;
  587. begin
  588. result:=as_none;
  589. hs:=upper(s);
  590. for t:=low(tasm) to high(tasm) do
  591. if assigned(asminfos[t]) and
  592. (asminfos[t]^.idtxt=hs) then
  593. begin
  594. result:=t;
  595. exit;
  596. end;
  597. end;
  598. function find_dbg_by_string(const s : string) : tdbg;
  599. var
  600. hs : string;
  601. t : tdbg;
  602. begin
  603. result:=dbg_none;
  604. hs:=upper(s);
  605. for t:=low(tdbg) to high(tdbg) do
  606. if assigned(dbginfos[t]) and
  607. (dbginfos[t]^.idtxt=hs) then
  608. begin
  609. result:=t;
  610. exit;
  611. end;
  612. end;
  613. function UpdateAlignment(var d:talignmentinfo;const s:talignmentinfo) : boolean;
  614. begin
  615. result:=true;
  616. with d do
  617. begin
  618. if (s.procalign in [1,2,4,8,16,32,64,128]) or (s.procalign=256) then
  619. procalign:=s.procalign
  620. else if s.procalign<>0 then
  621. result:=false;
  622. if (s.loopalign in [1,2,4,8,16,32,64,128]) or (s.loopalign=256) then
  623. loopalign:=s.loopalign
  624. else if s.loopalign<>0 then
  625. result:=false;
  626. if (s.jumpalign in [1,2,4,8,16,32,64,128]) or (s.jumpalign=256) then
  627. jumpalign:=s.jumpalign
  628. else if s.jumpalign<>0 then
  629. result:=false;
  630. if (s.coalescealign in [1,2,4,8,16,32,64,128]) or (s.coalescealign=256) then
  631. coalescealign:=s.coalescealign
  632. else if s.coalescealign<>0 then
  633. result:=false;
  634. if s.jumpalignskipmax>0 then
  635. jumpalignskipmax:=s.jumpalignskipmax;
  636. if s.coalescealign>0 then
  637. coalescealignskipmax:=s.coalescealignskipmax;
  638. { general update rules:
  639. minimum: if higher then update
  640. maximum: if lower then update or if undefined then update }
  641. if s.constalignmin>constalignmin then
  642. constalignmin:=s.constalignmin;
  643. if (constalignmax=0) or
  644. ((s.constalignmax>0) and (s.constalignmax<constalignmax)) then
  645. constalignmax:=s.constalignmax;
  646. if s.varalignmin>varalignmin then
  647. varalignmin:=s.varalignmin;
  648. if (varalignmax=0) or
  649. ((s.varalignmax>0) and (s.varalignmax<varalignmax)) then
  650. varalignmax:=s.varalignmax;
  651. if s.localalignmin>localalignmin then
  652. localalignmin:=s.localalignmin;
  653. if (localalignmax=0) or
  654. ((s.localalignmax>0) and (s.localalignmax<localalignmax)) then
  655. localalignmax:=s.localalignmax;
  656. if s.recordalignmin>recordalignmin then
  657. recordalignmin:=s.recordalignmin;
  658. if (recordalignmax=0) or
  659. ((s.recordalignmax>0) and (s.recordalignmax<recordalignmax)) then
  660. recordalignmax:=s.recordalignmax;
  661. if (maxCrecordalign=0) or
  662. ((s.maxCrecordalign>0) and (s.maxCrecordalign<maxCrecordalign)) then
  663. maxCrecordalign:=s.maxCrecordalign;
  664. end;
  665. end;
  666. {****************************************************************************
  667. Target registration
  668. ****************************************************************************}
  669. procedure RegisterTarget(const r:tsysteminfo);
  670. var
  671. t : tsystem;
  672. begin
  673. t:=r.system;
  674. if assigned(targetinfos[t]) then
  675. writeln('Warning: Target is already registered!')
  676. else
  677. new(targetinfos[t]);
  678. targetinfos[t]^:=r;
  679. end;
  680. procedure RegisterRes(const r:tresinfo; rcf : TAbstractResourceFileClass);
  681. var
  682. t : tres;
  683. begin
  684. t:=r.id;
  685. if not assigned(resinfos[t]) then
  686. new(resinfos[t]);
  687. resinfos[t]^:=r;
  688. resinfos[t]^.resourcefileclass:=rcf;
  689. end;
  690. procedure RegisterAr(const r:tarinfo);
  691. var
  692. t : tar;
  693. begin
  694. t:=r.id;
  695. if assigned(arinfos[t]) then
  696. writeln('Warning: ar is already registered!')
  697. else
  698. new(arinfos[t]);
  699. arinfos[t]^:=r;
  700. end;
  701. procedure DeregisterInfos;
  702. var
  703. assem : tasm;
  704. target : tsystem;
  705. ar : tar;
  706. res : tres;
  707. dbg : tdbg;
  708. begin
  709. for target:=low(tsystem) to high(tsystem) do
  710. if assigned(targetinfos[target]) then
  711. begin
  712. freemem(targetinfos[target],sizeof(tsysteminfo));
  713. targetinfos[target]:=nil;
  714. end;
  715. for assem:=low(tasm) to high(tasm) do
  716. if assigned(asminfos[assem]) then
  717. begin
  718. freemem(asminfos[assem],sizeof(tasminfo));
  719. asminfos[assem]:=nil;
  720. end;
  721. for ar:=low(tar) to high(tar) do
  722. if assigned(arinfos[ar]) then
  723. begin
  724. freemem(arinfos[ar],sizeof(tarinfo));
  725. arinfos[ar]:=nil;
  726. end;
  727. for res:=low(tres) to high(tres) do
  728. if assigned(resinfos[res]) then
  729. begin
  730. freemem(resinfos[res],sizeof(tresinfo));
  731. resinfos[res]:=nil;
  732. end;
  733. for dbg:=low(tdbg) to high(tdbg) do
  734. if assigned(dbginfos[dbg]) then
  735. begin
  736. freemem(dbginfos[dbg],sizeof(tdbginfo));
  737. dbginfos[dbg]:=nil;
  738. end;
  739. end;
  740. {****************************************************************************
  741. Initialization of default target
  742. ****************************************************************************}
  743. procedure default_target(t:tsystem);
  744. begin
  745. set_target(t);
  746. if source_info.name='' then
  747. source_info:=target_info;
  748. end;
  749. procedure set_source_info(const ti : tsysteminfo);
  750. begin
  751. { can't use message() here (PFV) }
  752. if source_info.name<>'' then
  753. Writeln('Warning: Source OS Redefined!');
  754. source_info:=ti;
  755. end;
  756. procedure InitSystems;
  757. begin
  758. { Now default target, this is dependent on the target cpu define,
  759. when the define is the same as the source cpu then we use the source
  760. os, else we pick a default }
  761. {$ifdef i386}
  762. {$ifdef cpui386}
  763. default_target(source_info.system);
  764. {$define default_target_set}
  765. {$else cpui386}
  766. {$ifdef linux}
  767. default_target(system_i386_linux);
  768. {$define default_target_set}
  769. {$endif}
  770. {$ifdef freebsd}
  771. default_target(system_i386_freebsd);
  772. {$define default_target_set}
  773. {$endif}
  774. {$ifdef openbsd}
  775. default_target(system_i386_openbsd);
  776. {$define default_target_set}
  777. {$endif}
  778. {$ifdef netbsd}
  779. default_target(system_i386_netbsd);
  780. {$define default_target_set}
  781. {$endif}
  782. {$ifdef darwin}
  783. default_target(system_i386_darwin);
  784. {$define default_target_set}
  785. {$endif}
  786. {$ifdef android}
  787. {$define default_target_set}
  788. default_target(system_i386_android);
  789. {$endif}
  790. {$ifdef solaris}
  791. {$define default_target_set}
  792. default_target(system_i386_solaris);
  793. {$endif}
  794. {$endif cpui386}
  795. { default is linux }
  796. {$ifndef default_target_set}
  797. default_target(system_i386_linux);
  798. {$endif default_target_set}
  799. {$endif i386}
  800. {$ifdef x86_64}
  801. {$ifdef cpux86_64}
  802. default_target(source_info.system);
  803. {$define default_target_set}
  804. {$else cpux86_64}
  805. {$ifdef MSWindows}
  806. default_target(system_x86_64_win64);
  807. {$define default_target_set}
  808. {$endif}
  809. {$ifdef linux}
  810. default_target(system_x86_64_linux);
  811. {$define default_target_set}
  812. {$endif}
  813. {$ifdef dragonfly}
  814. default_target(system_x86_64_dragonfly);
  815. {$define default_target_set}
  816. {$endif}
  817. {$ifdef freebsd}
  818. default_target(system_x86_64_freebsd);
  819. {$define default_target_set}
  820. {$endif}
  821. {$ifdef openbsd}
  822. default_target(system_x86_64_openbsd);
  823. {$define default_target_set}
  824. {$endif}
  825. {$ifdef netbsd}
  826. default_target(system_x86_64_netbsd);
  827. {$define default_target_set}
  828. {$endif}
  829. {$ifdef solaris}
  830. default_target(system_x86_64_solaris);
  831. {$define default_target_set}
  832. {$endif}
  833. {$ifdef darwin}
  834. default_target(system_x86_64_darwin);
  835. {$define default_target_set}
  836. {$endif}
  837. {$endif cpux86_64}
  838. { default is linux }
  839. {$ifndef default_target_set}
  840. default_target(system_x86_64_linux);
  841. {$endif default_target_set}
  842. {$endif x86_64}
  843. {$ifdef m68k}
  844. {$ifdef cpu68}
  845. default_target(source_info.system);
  846. {$else cpu68}
  847. default_target(system_m68k_linux);
  848. {$endif cpu68}
  849. {$endif m68k}
  850. {$ifdef powerpc}
  851. {$ifdef cpupowerpc32}
  852. default_target(source_info.system);
  853. {$define default_target_set}
  854. {$else cpupowerpc}
  855. {$ifdef linux}
  856. default_target(system_powerpc_linux);
  857. {$define default_target_set}
  858. {$endif}
  859. {$ifdef darwin}
  860. default_target(system_powerpc_darwin);
  861. {$define default_target_set}
  862. {$endif}
  863. {$endif cpupowerpc}
  864. {$ifdef aix}
  865. default_target(system_powerpc_aix);
  866. {$define default_target_set}
  867. {$endif}
  868. {$ifdef android}
  869. {$define default_target_set}
  870. default_target(system_x86_64_android);
  871. {$endif}
  872. {$ifndef default_target_set}
  873. default_target(system_powerpc_linux);
  874. {$endif default_target_set}
  875. {$endif powerpc}
  876. {$ifdef POWERPC64}
  877. {$ifdef cpupowerpc64}
  878. default_target(source_info.system);
  879. {$define default_target_set}
  880. {$else cpupowerpc64}
  881. {$ifdef darwin}
  882. default_target(system_powerpc64_darwin);
  883. {$define default_target_set}
  884. {$endif}
  885. {$ifdef linux}
  886. default_target(system_powerpc64_linux);
  887. {$define default_target_set}
  888. {$endif}
  889. {$ifdef aix}
  890. default_target(system_powerpc64_aix);
  891. {$define default_target_set}
  892. {$endif}
  893. {$endif cpupowerpc64}
  894. {$ifndef default_target_set}
  895. default_target(system_powerpc64_linux);
  896. {$define default_target_set}
  897. {$endif}
  898. {$endif POWERPC64}
  899. {$ifdef sparc}
  900. {$ifdef cpusparc}
  901. default_target(source_info.system);
  902. {$else cpusparc}
  903. {$ifdef solaris}
  904. {$define default_target_set}
  905. default_target(system_sparc_solaris);
  906. {$endif}
  907. {$ifndef default_target_set}
  908. default_target(system_sparc_linux);
  909. {$endif ndef default_target_set}
  910. {$endif cpusparc}
  911. {$endif sparc}
  912. {$ifdef sparc64}
  913. {$ifdef cpusparc64}
  914. default_target(source_info.system);
  915. {$else cpusparc64}
  916. {$ifdef solaris}
  917. {$define default_target_set}
  918. default_target(system_sparc64_solaris);
  919. {$endif}
  920. {$ifndef default_target_set}
  921. default_target(system_sparc64_linux);
  922. {$endif ndef default_target_set}
  923. {$endif cpusparc64}
  924. {$endif sparc64}
  925. {$ifdef arm}
  926. {$ifdef cpuarm}
  927. default_target(source_info.system);
  928. {$else cpuarm}
  929. {$ifdef WINDOWS}
  930. {$define default_target_set}
  931. default_target(system_arm_wince);
  932. {$endif}
  933. {$ifdef linux}
  934. {$define default_target_set}
  935. default_target(system_arm_linux);
  936. {$endif}
  937. {$ifdef netbsd}
  938. {$define default_target_set}
  939. default_target(system_arm_netbsd);
  940. {$endif}
  941. {$ifdef android}
  942. {$define default_target_set}
  943. default_target(system_arm_android);
  944. {$endif}
  945. {$ifdef darwin}
  946. {$define default_target_set}
  947. default_target(system_arm_darwin);
  948. {$endif}
  949. {$ifndef default_target_set}
  950. default_target(system_arm_linux);
  951. {$define default_target_set}
  952. {$endif}
  953. {$endif cpuarm}
  954. {$endif arm}
  955. {$ifdef avr}
  956. default_target(system_avr_embedded);
  957. {$endif avr}
  958. {$ifdef mips}
  959. {$ifdef mipsel}
  960. {$ifdef cpumipsel}
  961. default_target(source_info.system);
  962. {$else cpumipsel}
  963. default_target(system_mipsel_linux);
  964. {$endif cpumipsel}
  965. {$else mipsel}
  966. default_target(system_mipseb_linux);
  967. {$endif mipsel}
  968. {$endif mips}
  969. {$ifdef jvm}
  970. default_target(system_jvm_java32);
  971. {$endif jvm}
  972. {$ifdef i8086}
  973. default_target(system_i8086_msdos);
  974. {$endif i8086}
  975. {$ifdef aarch64}
  976. {$ifdef cpuaarch64}
  977. default_target(source_info.system);
  978. {$else cpuaarch64}
  979. {$ifdef darwin}
  980. {$define default_target_set}
  981. default_target(system_aarch64_darwin);
  982. {$endif darwin}
  983. {$ifdef android}
  984. {$define default_target_set}
  985. default_target(system_aarch64_android);
  986. {$endif android}
  987. {$ifndef default_target_set}
  988. default_target(system_aarch64_linux);
  989. {$define default_target_set}
  990. {$endif}
  991. {$endif cpuaarch64}
  992. {$endif aarch64}
  993. {$ifdef wasm}
  994. default_target(system_wasm_wasm32);
  995. {$endif}
  996. {$ifdef riscv32}
  997. default_target(system_riscv32_linux);
  998. {$endif riscv32}
  999. {$ifdef riscv64}
  1000. default_target(system_riscv64_linux);
  1001. {$endif riscv64}
  1002. {$ifdef xtensa}
  1003. default_target(system_xtensa_embedded);
  1004. {$endif xtensa}
  1005. end;
  1006. initialization
  1007. source_info.name:='';
  1008. finalization
  1009. DeregisterInfos;
  1010. end.