2
0

systems.pas 37 KB

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