systems.pas 34 KB

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