systems.pas 34 KB

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