systems.pas 36 KB

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