systems.pas 36 KB

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