systems.pas 38 KB

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