systems.pas 39 KB

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