systems.pas 37 KB

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