systems.pas 39 KB

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