systems.pas 40 KB

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