systems.pas 35 KB

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