systems.pas 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132
  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. const
  231. { alias for supported_target field in tasminfo }
  232. system_any = system_none;
  233. systems_wince = [system_arm_wince,system_i386_wince];
  234. systems_android = [system_arm_android, system_aarch64_android, system_i386_android, system_x86_64_android, system_mipsel_android];
  235. systems_linux = [system_i386_linux,system_x86_64_linux,system_powerpc_linux,system_powerpc64_linux,
  236. system_arm_linux,system_sparc_linux,system_sparc64_linux,system_m68k_linux,
  237. system_x86_6432_linux,system_mipseb_linux,system_mipsel_linux,system_aarch64_linux,
  238. system_riscv32_linux,system_riscv64_linux];
  239. systems_dragonfly = [system_x86_64_dragonfly];
  240. systems_freebsd = [system_i386_freebsd,
  241. system_x86_64_freebsd];
  242. systems_netbsd = [system_i386_netbsd,
  243. system_m68k_netbsd,
  244. system_powerpc_netbsd,
  245. system_x86_64_netbsd,
  246. system_arm_netbsd];
  247. systems_openbsd = [system_i386_openbsd,
  248. system_x86_64_openbsd];
  249. systems_bsd = systems_freebsd + systems_netbsd + systems_openbsd + systems_dragonfly;
  250. systems_aix = [system_powerpc_aix,system_powerpc64_aix];
  251. { all real windows systems, no cripple ones like win16, wince, wdosx et. al. }
  252. systems_windows = [system_i386_win32,system_x86_64_win64];
  253. { all windows systems }
  254. systems_all_windows = [system_i386_win32,system_x86_64_win64,
  255. system_arm_wince,system_i386_wince,
  256. system_i8086_win16];
  257. { all darwin systems }
  258. systems_darwin = [system_powerpc_darwin,system_i386_darwin,
  259. system_powerpc64_darwin,system_x86_64_darwin,
  260. system_arm_darwin,system_i386_iphonesim,
  261. system_aarch64_darwin,system_x86_64_iphonesim];
  262. {all solaris systems }
  263. systems_solaris = [system_sparc_solaris, system_i386_solaris,
  264. system_x86_64_solaris];
  265. { all embedded systems }
  266. systems_embedded = [system_i386_embedded,system_m68k_embedded,
  267. system_powerpc_embedded,
  268. system_sparc_embedded,obsolete_system_vm_embedded,
  269. obsolete_system_ia64_embedded,system_x86_64_embedded,
  270. system_mips_embedded,system_arm_embedded,
  271. system_powerpc64_embedded,system_avr_embedded,
  272. system_jvm_java32,system_mipseb_embedded,system_mipsel_embedded,
  273. system_i8086_embedded,system_riscv32_embedded,system_riscv64_embedded,
  274. system_xtensa_embedded];
  275. { all FreeRTOS systems }
  276. systems_freertos = [system_xtensa_freertos];
  277. { all systems that allow section directive }
  278. systems_allow_section = systems_embedded;
  279. { systems that uses dotted function names as descriptors }
  280. systems_dotted_function_names = [system_powerpc64_linux]+systems_aix;
  281. systems_allow_section_no_semicolon = systems_allow_section
  282. {$ifndef DISABLE_TLS_DIRECTORY}
  283. + systems_windows
  284. {$endif not DISABLE_TLS_DIRECTORY}
  285. ;
  286. { systems that allow external far variables }
  287. systems_allow_external_far_var = [system_i8086_msdos,system_i8086_win16,system_i8086_embedded];
  288. { all symbian systems }
  289. systems_symbian = [system_i386_symbian,system_arm_symbian];
  290. { all classic Mac OS targets }
  291. systems_macos = [system_m68k_macos,system_powerpc_macos];
  292. { all OS/2 targets }
  293. systems_os2 = [system_i386_OS2,system_i386_emx];
  294. { AROS systems }
  295. systems_aros = [system_i386_aros,system_x86_64_aros,system_arm_aros];
  296. { all amiga like systems }
  297. systems_amigalike = [system_m68k_amiga,system_powerpc_morphos,system_powerpc_amiga]+systems_aros;
  298. { all native nt systems }
  299. systems_nativent = [system_i386_nativent];
  300. { systems supporting Objective-C }
  301. systems_objc_supported = systems_darwin;
  302. { systems using the non-fragile Objective-C ABI }
  303. systems_objc_nfabi = [system_powerpc64_darwin,system_x86_64_darwin,system_arm_darwin,system_i386_iphonesim,system_aarch64_darwin,system_x86_64_iphonesim];
  304. { systems supporting "blocks" }
  305. systems_blocks_supported = systems_darwin;
  306. { all systems supporting exports from programs or units }
  307. systems_unit_program_exports = [system_i386_win32,
  308. system_i386_wdosx,
  309. system_i386_Netware,
  310. system_i386_netwlibc,
  311. system_arm_wince,
  312. system_x86_64_win64,
  313. system_i8086_win16]+systems_linux+systems_android;
  314. { all systems that reference symbols in other binaries using indirect imports }
  315. systems_indirect_var_imports = systems_all_windows+[system_i386_nativent];
  316. { all systems that support indirect entry information }
  317. systems_indirect_entry_information = systems_darwin+[system_i386_win32,system_x86_64_win64,system_x86_64_linux];
  318. { all systems for which weak linking has been tested/is supported }
  319. systems_weak_linking = systems_darwin + systems_solaris + systems_linux + systems_android + systems_openbsd + systems_freebsd;
  320. systems_internal_sysinit = [system_i386_win32,system_x86_64_win64,
  321. system_i386_linux,system_powerpc64_linux,system_sparc64_linux,system_x86_64_linux,
  322. system_m68k_atari,system_m68k_palmos,
  323. system_i386_haiku,system_x86_64_haiku,
  324. system_i386_openbsd,system_x86_64_openbsd,
  325. system_riscv32_linux,system_riscv64_linux
  326. ]+systems_darwin+systems_amigalike;
  327. { all systems that use garbage collection for reference-counted types }
  328. systems_garbage_collected_managed_types = [
  329. system_jvm_java32,
  330. system_jvm_android32
  331. ];
  332. { all systems that use a managed vm (-> no real pointers, internal VMT
  333. format, ...) }
  334. systems_managed_vm = [
  335. system_jvm_java32,
  336. system_jvm_android32
  337. ];
  338. { all systems based on the JVM }
  339. systems_jvm = [
  340. system_jvm_java32,
  341. system_jvm_android32
  342. ];
  343. { all systems where typed constants have to be translated into node
  344. trees that initialise the data instead of into data sections }
  345. systems_typed_constants_node_init = [
  346. system_jvm_java32,
  347. system_jvm_android32
  348. ];
  349. { all systems that don't use a built-in framepointer for accessing nested
  350. variables, but emulate it by wrapping nested variables in records
  351. whose address is passed around }
  352. systems_fpnestedstruct = [
  353. {$ifndef llvm}
  354. system_jvm_java32,
  355. system_jvm_android32
  356. {$else not llvm}
  357. low(tsystem)..high(tsystem)
  358. {$endif not llvm}
  359. ];
  360. { all systems where a value parameter passed by reference must be copied
  361. on the caller side rather than on the callee side }
  362. systems_caller_copy_addr_value_para = [system_aarch64_darwin,system_aarch64_linux];
  363. { pointer checking (requires special code in FPC_CHECKPOINTER,
  364. and can never work for libc-based targets or any other program
  365. linking to an external library)
  366. }
  367. systems_support_checkpointer = systems_linux
  368. + [system_i386_win32]
  369. + [system_i386_GO32V2]
  370. + [system_i386_os2]
  371. + [system_i386_beos,system_i386_haiku]
  372. + [system_powerpc_morphos];
  373. cpu2str : array[TSystemCpu] of string[10] =
  374. ('','i386','m68k','alpha','powerpc','sparc','vm','ia64','x86_64',
  375. 'mips','arm', 'powerpc64', 'avr', 'mipsel','jvm', 'i8086',
  376. 'aarch64', 'wasm', 'sparc64', 'riscv32', 'riscv64', 'xtensa');
  377. abiinfo : array[tabi] of tabiinfo = (
  378. (name: 'DEFAULT'; supported: true),
  379. (name: 'SYSV' ; supported:{$if defined(powerpc) or defined(powerpc64)}true{$else}false{$endif}),
  380. (name: 'AIX' ; supported:{$if defined(powerpc) or defined(powerpc64)}true{$else}false{$endif}),
  381. (name: 'DARWIN' ; supported:{$if defined(powerpc) or defined(powerpc64)}true{$else}false{$endif}),
  382. (name: 'ELFV2' ; supported:{$if defined(powerpc64)}true{$else}false{$endif}),
  383. (name: 'EABI' ; supported:{$ifdef FPC_ARMEL}true{$else}false{$endif}),
  384. (name: 'ARMEB' ; supported:{$ifdef FPC_ARMEB}true{$else}false{$endif}),
  385. (name: 'EABIHF' ; supported:{$ifdef FPC_ARMHF}true{$else}false{$endif}),
  386. (name: 'OLDWIN32GNU'; supported:{$ifdef I386}true{$else}false{$endif}),
  387. (name: 'AARCH64IOS'; supported:{$ifdef aarch64}true{$else}false{$endif}),
  388. (name: 'RISCVHF'; supported:{$if defined(riscv32) or defined(riscv64)}true{$else}false{$endif}),
  389. (name: 'LINUX386_SYSV'; supported:{$if defined(i386)}true{$else}false{$endif}),
  390. (name: 'WINDOWED'; supported:{$if defined(xtensa)}true{$else}false{$endif}),
  391. (name: 'CALL0'; supported:{$if defined(xtensa)}true{$else}false{$endif})
  392. );
  393. { x86 asm modes with an Intel-style syntax }
  394. asmmodes_x86_intel = [
  395. {$ifdef i8086}
  396. asmmode_standard,
  397. {$endif i8086}
  398. asmmode_i8086_intel,
  399. asmmode_i386_intel,
  400. asmmode_x86_64_intel
  401. ];
  402. { x86 asm modes with an AT&T-style syntax }
  403. asmmodes_x86_att = [
  404. {$if defined(i386) or defined(x86_64)}
  405. asmmode_standard,
  406. {$endif}
  407. asmmode_i8086_att,
  408. asmmode_i386_att,
  409. asmmode_x86_64_att,
  410. asmmode_x86_64_gas
  411. ];
  412. var
  413. targetinfos : array[tsystem] of psysteminfo;
  414. arinfos : array[tar] of parinfo;
  415. resinfos : array[tres] of presinfo;
  416. asminfos : array[tasm] of pasminfo;
  417. dbginfos : array[tdbg] of pdbginfo;
  418. source_info : tsysteminfo;
  419. target_cpu : tsystemcpu;
  420. target_info : tsysteminfo;
  421. target_asm : tasminfo;
  422. target_ar : tarinfo;
  423. target_res : tresinfo;
  424. target_dbg : tdbginfo;
  425. target_cpu_string,
  426. target_os_string : string[12]; { for rtl/<X>/,fcl/<X>/, etc. }
  427. target_full_string : string[24];
  428. function set_target(t:tsystem):boolean;
  429. function set_target_asm(t:tasm):boolean;
  430. function set_target_ar(t:tar):boolean;
  431. function set_target_res(t:tres):boolean;
  432. function set_target_dbg(t:tdbg):boolean;
  433. function find_system_by_string(const s : string) : tsystem;
  434. function find_asm_by_string(const s : string) : tasm;
  435. function find_dbg_by_string(const s : string) : tdbg;
  436. procedure set_source_info(const ti : tsysteminfo);
  437. function UpdateAlignment(var d:talignmentinfo;const s:talignmentinfo) : boolean;
  438. procedure RegisterTarget(const r:tsysteminfo);
  439. procedure RegisterRes(const r:tresinfo; rcf : TAbstractResourceFileClass);
  440. procedure RegisterAr(const r:tarinfo);
  441. procedure InitSystems;
  442. {$ifdef FreeBSD}
  443. function GetOSRelDate:Longint;
  444. {$endif}
  445. implementation
  446. uses
  447. cutils{$ifdef FreeBSD},SysCtl,BaseUnix{$endif};
  448. {****************************************************************************
  449. OS runtime version detection utility routine
  450. ****************************************************************************}
  451. {$ifdef FreeBSD}
  452. function GetOSRelDate:Longint;
  453. { FPSysCtl first argument was of type pchar
  454. up to commit 35566 from 2017/03/11
  455. and corrected to pcint in that commit.
  456. But the following code needs to work with
  457. both old 3.0.X definition and new definition using pcint type.
  458. Problem solved using a special type called
  459. FPSysCtlFirstArgType. }
  460. {$if defined(VER3_0_0) or defined(VER3_0_2)}
  461. type
  462. FPSysCtlFirstArgType = PChar;
  463. {$else}
  464. type
  465. FPSysCtlFirstArgType = pcint;
  466. {$endif}
  467. var
  468. mib : array[0..1] of cint;
  469. rval : cint;
  470. len : size_t;
  471. i : longint;
  472. v : longint;
  473. oerrno : cint;
  474. S : AnsiString;
  475. Begin
  476. s:='ab';
  477. SetLength(S,50);
  478. mib[0] := CTL_KERN;
  479. mib[1] := KERN_OSRELDATE;
  480. len := 4;
  481. oerrno:= fpgeterrno;
  482. if (FPsysctl(FPSysCtlFirstArgType(@mib), 2, pchar(@v), @len, NIL, 0) = -1) Then
  483. Begin
  484. if (fpgeterrno = ESysENOMEM) Then
  485. fpseterrno(oerrno);
  486. GetOSRelDate:=0;
  487. End
  488. else
  489. GetOSRelDate:=v;
  490. End;
  491. {$endif}
  492. {****************************************************************************
  493. Target setting
  494. ****************************************************************************}
  495. function set_target(t:tsystem):boolean;
  496. begin
  497. set_target:=false;
  498. if assigned(targetinfos[t]) then
  499. begin
  500. target_info:=targetinfos[t]^;
  501. set_target_asm(target_info.assem);
  502. set_target_ar(target_info.ar);
  503. set_target_res(target_info.res);
  504. set_target_dbg(target_info.dbg);
  505. target_cpu:=target_info.cpu;
  506. target_os_string:=lower(target_info.shortname);
  507. target_cpu_string:=cpu2str[target_cpu];
  508. target_full_string:=target_cpu_string+'-'+target_os_string;
  509. set_target:=true;
  510. exit;
  511. end;
  512. end;
  513. function set_target_asm(t:tasm):boolean;
  514. begin
  515. set_target_asm:=false;
  516. if assigned(asminfos[t]) and
  517. ((target_info.system in asminfos[t]^.supported_targets) or
  518. (system_any in asminfos[t]^.supported_targets)) then
  519. begin
  520. target_asm:=asminfos[t]^;
  521. set_target_asm:=true;
  522. exit;
  523. end;
  524. end;
  525. function set_target_ar(t:tar):boolean;
  526. begin
  527. result:=false;
  528. if assigned(arinfos[t]) then
  529. begin
  530. target_ar:=arinfos[t]^;
  531. result:=true;
  532. exit;
  533. end;
  534. end;
  535. function set_target_res(t:tres):boolean;
  536. begin
  537. result:=false;
  538. if assigned(resinfos[t]) then
  539. begin
  540. target_res:=resinfos[t]^;
  541. result:=true;
  542. exit;
  543. end
  544. else
  545. FillByte(target_res,sizeof(target_res),0);
  546. end;
  547. function set_target_dbg(t:tdbg):boolean;
  548. begin
  549. result:=false;
  550. { no debugging support for llvm yet }
  551. {$ifndef llvm}
  552. if assigned(dbginfos[t]) then
  553. begin
  554. target_dbg:=dbginfos[t]^;
  555. result:=true;
  556. exit;
  557. end;
  558. {$endif}
  559. end;
  560. function find_system_by_string(const s : string) : tsystem;
  561. var
  562. hs : string;
  563. t : tsystem;
  564. begin
  565. result:=system_none;
  566. hs:=upper(s);
  567. for t:=low(tsystem) to high(tsystem) do
  568. if assigned(targetinfos[t]) and
  569. (upper(targetinfos[t]^.shortname)=hs) then
  570. begin
  571. result:=t;
  572. exit;
  573. end;
  574. end;
  575. function find_asm_by_string(const s : string) : tasm;
  576. var
  577. hs : string;
  578. t : tasm;
  579. begin
  580. result:=as_none;
  581. hs:=upper(s);
  582. for t:=low(tasm) to high(tasm) do
  583. if assigned(asminfos[t]) and
  584. (asminfos[t]^.idtxt=hs) then
  585. begin
  586. result:=t;
  587. exit;
  588. end;
  589. end;
  590. function find_dbg_by_string(const s : string) : tdbg;
  591. var
  592. hs : string;
  593. t : tdbg;
  594. begin
  595. result:=dbg_none;
  596. hs:=upper(s);
  597. for t:=low(tdbg) to high(tdbg) do
  598. if assigned(dbginfos[t]) and
  599. (dbginfos[t]^.idtxt=hs) then
  600. begin
  601. result:=t;
  602. exit;
  603. end;
  604. end;
  605. function UpdateAlignment(var d:talignmentinfo;const s:talignmentinfo) : boolean;
  606. begin
  607. result:=true;
  608. with d do
  609. begin
  610. if (s.procalign in [1,2,4,8,16,32,64,128]) or (s.procalign=256) then
  611. procalign:=s.procalign
  612. else if s.procalign<>0 then
  613. result:=false;
  614. if (s.loopalign in [1,2,4,8,16,32,64,128]) or (s.loopalign=256) then
  615. loopalign:=s.loopalign
  616. else if s.loopalign<>0 then
  617. result:=false;
  618. if (s.jumpalign in [1,2,4,8,16,32,64,128]) or (s.jumpalign=256) then
  619. jumpalign:=s.jumpalign
  620. else if s.jumpalign<>0 then
  621. result:=false;
  622. if (s.coalescealign in [1,2,4,8,16,32,64,128]) or (s.coalescealign=256) then
  623. coalescealign:=s.coalescealign
  624. else if s.coalescealign<>0 then
  625. result:=false;
  626. if s.jumpalignskipmax>0 then
  627. jumpalignskipmax:=s.jumpalignskipmax;
  628. if s.coalescealign>0 then
  629. coalescealignskipmax:=s.coalescealignskipmax;
  630. { general update rules:
  631. minimum: if higher then update
  632. maximum: if lower then update or if undefined then update }
  633. if s.constalignmin>constalignmin then
  634. constalignmin:=s.constalignmin;
  635. if (constalignmax=0) or
  636. ((s.constalignmax>0) and (s.constalignmax<constalignmax)) then
  637. constalignmax:=s.constalignmax;
  638. if s.varalignmin>varalignmin then
  639. varalignmin:=s.varalignmin;
  640. if (varalignmax=0) or
  641. ((s.varalignmax>0) and (s.varalignmax<varalignmax)) then
  642. varalignmax:=s.varalignmax;
  643. if s.localalignmin>localalignmin then
  644. localalignmin:=s.localalignmin;
  645. if (localalignmax=0) or
  646. ((s.localalignmax>0) and (s.localalignmax<localalignmax)) then
  647. localalignmax:=s.localalignmax;
  648. if s.recordalignmin>recordalignmin then
  649. recordalignmin:=s.recordalignmin;
  650. if (recordalignmax=0) or
  651. ((s.recordalignmax>0) and (s.recordalignmax<recordalignmax)) then
  652. recordalignmax:=s.recordalignmax;
  653. if (maxCrecordalign=0) or
  654. ((s.maxCrecordalign>0) and (s.maxCrecordalign<maxCrecordalign)) then
  655. maxCrecordalign:=s.maxCrecordalign;
  656. end;
  657. end;
  658. {****************************************************************************
  659. Target registration
  660. ****************************************************************************}
  661. procedure RegisterTarget(const r:tsysteminfo);
  662. var
  663. t : tsystem;
  664. begin
  665. t:=r.system;
  666. if assigned(targetinfos[t]) then
  667. writeln('Warning: Target is already registered!')
  668. else
  669. new(targetinfos[t]);
  670. targetinfos[t]^:=r;
  671. end;
  672. procedure RegisterRes(const r:tresinfo; rcf : TAbstractResourceFileClass);
  673. var
  674. t : tres;
  675. begin
  676. t:=r.id;
  677. if not assigned(resinfos[t]) then
  678. new(resinfos[t]);
  679. resinfos[t]^:=r;
  680. resinfos[t]^.resourcefileclass:=rcf;
  681. end;
  682. procedure RegisterAr(const r:tarinfo);
  683. var
  684. t : tar;
  685. begin
  686. t:=r.id;
  687. if assigned(arinfos[t]) then
  688. writeln('Warning: ar is already registered!')
  689. else
  690. new(arinfos[t]);
  691. arinfos[t]^:=r;
  692. end;
  693. procedure DeregisterInfos;
  694. var
  695. assem : tasm;
  696. target : tsystem;
  697. ar : tar;
  698. res : tres;
  699. dbg : tdbg;
  700. begin
  701. for target:=low(tsystem) to high(tsystem) do
  702. if assigned(targetinfos[target]) then
  703. begin
  704. freemem(targetinfos[target],sizeof(tsysteminfo));
  705. targetinfos[target]:=nil;
  706. end;
  707. for assem:=low(tasm) to high(tasm) do
  708. if assigned(asminfos[assem]) then
  709. begin
  710. freemem(asminfos[assem],sizeof(tasminfo));
  711. asminfos[assem]:=nil;
  712. end;
  713. for ar:=low(tar) to high(tar) do
  714. if assigned(arinfos[ar]) then
  715. begin
  716. freemem(arinfos[ar],sizeof(tarinfo));
  717. arinfos[ar]:=nil;
  718. end;
  719. for res:=low(tres) to high(tres) do
  720. if assigned(resinfos[res]) then
  721. begin
  722. freemem(resinfos[res],sizeof(tresinfo));
  723. resinfos[res]:=nil;
  724. end;
  725. for dbg:=low(tdbg) to high(tdbg) do
  726. if assigned(dbginfos[dbg]) then
  727. begin
  728. freemem(dbginfos[dbg],sizeof(tdbginfo));
  729. dbginfos[dbg]:=nil;
  730. end;
  731. end;
  732. {****************************************************************************
  733. Initialization of default target
  734. ****************************************************************************}
  735. procedure default_target(t:tsystem);
  736. begin
  737. set_target(t);
  738. if source_info.name='' then
  739. source_info:=target_info;
  740. end;
  741. procedure set_source_info(const ti : tsysteminfo);
  742. begin
  743. { can't use message() here (PFV) }
  744. if source_info.name<>'' then
  745. Writeln('Warning: Source OS Redefined!');
  746. source_info:=ti;
  747. end;
  748. procedure InitSystems;
  749. begin
  750. { Now default target, this is dependent on the target cpu define,
  751. when the define is the same as the source cpu then we use the source
  752. os, else we pick a default }
  753. {$ifdef i386}
  754. {$ifdef cpui386}
  755. default_target(source_info.system);
  756. {$define default_target_set}
  757. {$else cpui386}
  758. {$ifdef linux}
  759. default_target(system_i386_linux);
  760. {$define default_target_set}
  761. {$endif}
  762. {$ifdef freebsd}
  763. default_target(system_i386_freebsd);
  764. {$define default_target_set}
  765. {$endif}
  766. {$ifdef openbsd}
  767. default_target(system_i386_openbsd);
  768. {$define default_target_set}
  769. {$endif}
  770. {$ifdef netbsd}
  771. default_target(system_i386_netbsd);
  772. {$define default_target_set}
  773. {$endif}
  774. {$ifdef darwin}
  775. default_target(system_i386_darwin);
  776. {$define default_target_set}
  777. {$endif}
  778. {$ifdef android}
  779. {$define default_target_set}
  780. default_target(system_i386_android);
  781. {$endif}
  782. {$ifdef solaris}
  783. {$define default_target_set}
  784. default_target(system_i386_solaris);
  785. {$endif}
  786. {$endif cpui386}
  787. { default is linux }
  788. {$ifndef default_target_set}
  789. default_target(system_i386_linux);
  790. {$endif default_target_set}
  791. {$endif i386}
  792. {$ifdef x86_64}
  793. {$ifdef cpux86_64}
  794. default_target(source_info.system);
  795. {$define default_target_set}
  796. {$else cpux86_64}
  797. {$ifdef MSWindows}
  798. default_target(system_x86_64_win64);
  799. {$define default_target_set}
  800. {$endif}
  801. {$ifdef linux}
  802. default_target(system_x86_64_linux);
  803. {$define default_target_set}
  804. {$endif}
  805. {$ifdef dragonfly}
  806. default_target(system_x86_64_dragonfly);
  807. {$define default_target_set}
  808. {$endif}
  809. {$ifdef freebsd}
  810. default_target(system_x86_64_freebsd);
  811. {$define default_target_set}
  812. {$endif}
  813. {$ifdef openbsd}
  814. default_target(system_x86_64_openbsd);
  815. {$define default_target_set}
  816. {$endif}
  817. {$ifdef netbsd}
  818. default_target(system_x86_64_netbsd);
  819. {$define default_target_set}
  820. {$endif}
  821. {$ifdef solaris}
  822. default_target(system_x86_64_solaris);
  823. {$define default_target_set}
  824. {$endif}
  825. {$ifdef darwin}
  826. default_target(system_x86_64_darwin);
  827. {$define default_target_set}
  828. {$endif}
  829. {$endif cpux86_64}
  830. { default is linux }
  831. {$ifndef default_target_set}
  832. default_target(system_x86_64_linux);
  833. {$endif default_target_set}
  834. {$endif x86_64}
  835. {$ifdef m68k}
  836. {$ifdef cpu68}
  837. default_target(source_info.system);
  838. {$else cpu68}
  839. default_target(system_m68k_linux);
  840. {$endif cpu68}
  841. {$endif m68k}
  842. {$ifdef powerpc}
  843. {$ifdef cpupowerpc32}
  844. default_target(source_info.system);
  845. {$define default_target_set}
  846. {$else cpupowerpc}
  847. {$ifdef linux}
  848. default_target(system_powerpc_linux);
  849. {$define default_target_set}
  850. {$endif}
  851. {$ifdef darwin}
  852. default_target(system_powerpc_darwin);
  853. {$define default_target_set}
  854. {$endif}
  855. {$endif cpupowerpc}
  856. {$ifdef aix}
  857. default_target(system_powerpc_aix);
  858. {$define default_target_set}
  859. {$endif}
  860. {$ifdef android}
  861. {$define default_target_set}
  862. default_target(system_x86_64_android);
  863. {$endif}
  864. {$ifndef default_target_set}
  865. default_target(system_powerpc_linux);
  866. {$endif default_target_set}
  867. {$endif powerpc}
  868. {$ifdef POWERPC64}
  869. {$ifdef cpupowerpc64}
  870. default_target(source_info.system);
  871. {$define default_target_set}
  872. {$else cpupowerpc64}
  873. {$ifdef darwin}
  874. default_target(system_powerpc64_darwin);
  875. {$define default_target_set}
  876. {$endif}
  877. {$ifdef linux}
  878. default_target(system_powerpc64_linux);
  879. {$define default_target_set}
  880. {$endif}
  881. {$ifdef aix}
  882. default_target(system_powerpc64_aix);
  883. {$define default_target_set}
  884. {$endif}
  885. {$endif cpupowerpc64}
  886. {$ifndef default_target_set}
  887. default_target(system_powerpc64_linux);
  888. {$define default_target_set}
  889. {$endif}
  890. {$endif POWERPC64}
  891. {$ifdef sparc}
  892. {$ifdef cpusparc}
  893. default_target(source_info.system);
  894. {$else cpusparc}
  895. {$ifdef solaris}
  896. {$define default_target_set}
  897. default_target(system_sparc_solaris);
  898. {$endif}
  899. {$ifndef default_target_set}
  900. default_target(system_sparc_linux);
  901. {$endif ndef default_target_set}
  902. {$endif cpusparc}
  903. {$endif sparc}
  904. {$ifdef sparc64}
  905. {$ifdef cpusparc64}
  906. default_target(source_info.system);
  907. {$else cpusparc64}
  908. {$ifdef solaris}
  909. {$define default_target_set}
  910. default_target(system_sparc64_solaris);
  911. {$endif}
  912. {$ifndef default_target_set}
  913. default_target(system_sparc64_linux);
  914. {$endif ndef default_target_set}
  915. {$endif cpusparc64}
  916. {$endif sparc64}
  917. {$ifdef arm}
  918. {$ifdef cpuarm}
  919. default_target(source_info.system);
  920. {$else cpuarm}
  921. {$ifdef WINDOWS}
  922. {$define default_target_set}
  923. default_target(system_arm_wince);
  924. {$endif}
  925. {$ifdef linux}
  926. {$define default_target_set}
  927. default_target(system_arm_linux);
  928. {$endif}
  929. {$ifdef netbsd}
  930. {$define default_target_set}
  931. default_target(system_arm_netbsd);
  932. {$endif}
  933. {$ifdef android}
  934. {$define default_target_set}
  935. default_target(system_arm_android);
  936. {$endif}
  937. {$ifdef darwin}
  938. {$define default_target_set}
  939. default_target(system_arm_darwin);
  940. {$endif}
  941. {$ifndef default_target_set}
  942. default_target(system_arm_linux);
  943. {$define default_target_set}
  944. {$endif}
  945. {$endif cpuarm}
  946. {$endif arm}
  947. {$ifdef avr}
  948. default_target(system_avr_embedded);
  949. {$endif avr}
  950. {$ifdef mips}
  951. {$ifdef mipsel}
  952. {$ifdef cpumipsel}
  953. default_target(source_info.system);
  954. {$else cpumipsel}
  955. default_target(system_mipsel_linux);
  956. {$endif cpumipsel}
  957. {$else mipsel}
  958. default_target(system_mipseb_linux);
  959. {$endif mipsel}
  960. {$endif mips}
  961. {$ifdef jvm}
  962. default_target(system_jvm_java32);
  963. {$endif jvm}
  964. {$ifdef i8086}
  965. default_target(system_i8086_msdos);
  966. {$endif i8086}
  967. {$ifdef aarch64}
  968. {$ifdef cpuaarch64}
  969. default_target(source_info.system);
  970. {$else cpuaarch64}
  971. {$ifdef darwin}
  972. {$define default_target_set}
  973. default_target(system_aarch64_darwin);
  974. {$endif darwin}
  975. {$ifdef android}
  976. {$define default_target_set}
  977. default_target(system_aarch64_android);
  978. {$endif android}
  979. {$ifndef default_target_set}
  980. default_target(system_aarch64_linux);
  981. {$define default_target_set}
  982. {$endif}
  983. {$endif cpuaarch64}
  984. {$endif aarch64}
  985. {$ifdef wasm}
  986. default_target(system_wasm_wasm32);
  987. {$endif}
  988. {$ifdef riscv32}
  989. default_target(system_riscv32_linux);
  990. {$endif riscv32}
  991. {$ifdef riscv64}
  992. default_target(system_riscv64_linux);
  993. {$endif riscv64}
  994. {$ifdef xtensa}
  995. default_target(system_xtensa_embedded);
  996. {$endif xtensa}
  997. end;
  998. initialization
  999. source_info.name:='';
  1000. finalization
  1001. DeregisterInfos;
  1002. end.