systems.pas 36 KB

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