systems.pas 38 KB

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