systems.pas 36 KB

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