systems.pas 37 KB

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