systems.pas 33 KB

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