systems.pas 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020
  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. type
  22. tendian = (endian_little,endian_big);
  23. (*
  24. IMPORTANT NOTE:
  25. The value of this enumeration is stored in PPU files.
  26. Therefore adding new CPU targets should not change the
  27. values of the pre-existing targets. (CEC)
  28. FURTHERMORE : Make sure that this branch values, are
  29. consistant with the main branch version always.
  30. *)
  31. tsystemcpu=
  32. (
  33. cpu_no, { 0 }
  34. cpu_i386, { 1 }
  35. cpu_m68k, { 2 }
  36. cpu_alpha, { 3 }
  37. cpu_powerpc, { 4 }
  38. cpu_sparc, { 5 }
  39. cpu_vm, { 6 }
  40. cpu_iA64, { 7 }
  41. cpu_x86_64, { 8 }
  42. cpu_mips, { 9 }
  43. cpu_arm, { 10 }
  44. cpu_powerpc64, { 11 }
  45. cpu_avr, { 12 }
  46. cpu_mipsel { 13 }
  47. );
  48. tasmmode= (asmmode_none
  49. { standard assembler (cpu dependant) with full parsing }
  50. ,asmmode_standard
  51. ,asmmode_i386_att
  52. ,asmmode_i386_intel
  53. ,asmmode_ppc_gas
  54. ,asmmode_ppc_motorola
  55. ,asmmode_arm_gas
  56. ,asmmode_sparc_gas
  57. ,asmmode_x86_64_gas
  58. ,asmmode_m68k_mot
  59. ,asmmode_x86_64_intel
  60. ,asmmode_x86_64_att
  61. ,asmmode_avr_gas
  62. );
  63. (* IMPORTANT NOTE:
  64. the integer value of this enum is stored in PPU
  65. files to recognize the target, so if you add new targets
  66. allways add them at end PM
  67. FURTHERMORE : Make sure that this branch values are
  68. consistant with the main branch version always. (CEC)
  69. *)
  70. type
  71. tsystem =
  72. (
  73. system_none, { 0 }
  74. obsolete_system_i386_GO32V1,{ 1 }
  75. system_i386_GO32V2, { 2 }
  76. system_i386_linux, { 3 }
  77. system_i386_OS2, { 4 }
  78. system_i386_Win32, { 5 }
  79. system_i386_freebsd, { 6 }
  80. system_m68k_Amiga, { 7 }
  81. system_m68k_Atari, { 8 }
  82. system_m68k_Mac, { 9 }
  83. system_m68k_linux, { 10 }
  84. system_m68k_PalmOS, { 11 }
  85. system_alpha_linux, { 12 }
  86. system_powerpc_linux, { 13 }
  87. system_powerpc_macos, { 14 }
  88. system_i386_solaris, { 15 }
  89. system_i386_beos, { 16 }
  90. system_i386_netbsd, { 17 }
  91. system_m68k_netbsd, { 18 }
  92. system_i386_Netware, { 19 }
  93. system_i386_qnx, { 20 }
  94. system_i386_wdosx, { 21 }
  95. system_sparc_solaris, { 22 }
  96. system_sparc_linux, { 23 }
  97. system_i386_openbsd, { 24 }
  98. system_m68k_openbsd, { 25 }
  99. system_x86_64_linux, { 26 }
  100. system_powerpc_darwin, { 27 }
  101. system_i386_EMX, { 28 }
  102. system_powerpc_netbsd, { 29 }
  103. system_powerpc_openbsd, { 30 }
  104. system_arm_linux, { 31 }
  105. system_i386_watcom, { 32 }
  106. system_powerpc_MorphOS, { 33 }
  107. system_x86_64_freebsd, { 34 }
  108. system_i386_netwlibc, { 35 }
  109. system_powerpc_Amiga, { 36 }
  110. system_x86_64_win64, { 37 }
  111. system_arm_wince, { 38 }
  112. system_ia64_win64, { 39 }
  113. system_i386_wince, { 40 }
  114. system_x86_6432_linux, { 41 }
  115. system_arm_gba, { 42 }
  116. system_powerpc64_linux, { 43 }
  117. system_i386_darwin, { 44 }
  118. system_arm_palmos, { 45 }
  119. system_powerpc64_darwin, { 46 }
  120. system_arm_nds, { 47 }
  121. system_i386_embedded, { 48 }
  122. system_m68k_embedded, { 49 }
  123. system_alpha_embedded, { 50 }
  124. system_powerpc_embedded, { 51 }
  125. system_sparc_embedded, { 52 }
  126. system_vm_embedded, { 53 }
  127. system_iA64_embedded, { 54 }
  128. system_x86_64_embedded, { 55 }
  129. system_mips_embedded, { 56 }
  130. system_arm_embedded, { 57 }
  131. system_powerpc64_embedded, { 58 }
  132. system_i386_symbian, { 59 }
  133. system_arm_symbian, { 60 }
  134. system_x86_64_darwin, { 61 }
  135. system_avr_embedded, { 62 }
  136. system_i386_haiku, { 63 }
  137. system_arm_darwin, { 64 }
  138. system_x86_64_solaris, { 65 }
  139. system_mips_linux, { 66 }
  140. system_mipsel_linux, { 67 }
  141. system_i386_nativent, { 68 }
  142. system_i386_iphonesim, { 69 }
  143. system_powerpc_wii { 70 }
  144. );
  145. type
  146. tasm = (as_none
  147. ,as_gas { standard gnu assembler }
  148. ,as_i386_as_aout
  149. ,as_i386_nasmcoff
  150. ,as_i386_nasmwin32
  151. ,as_i386_nasmwdosx
  152. ,as_i386_nasmelf
  153. ,as_i386_nasmobj
  154. ,as_i386_nasmbeos
  155. ,as_i386_tasm
  156. ,as_i386_masm
  157. ,as_i386_wasm
  158. ,as_i386_coff
  159. ,as_i386_pecoff
  160. ,as_i386_elf32
  161. ,as_i386_pecoffwdosx
  162. ,as_m68k_mit
  163. ,as_powerpc_mpw
  164. ,as_darwin
  165. ,as_i386_macho
  166. ,as_x86_64_masm
  167. ,as_x86_64_pecoff
  168. ,as_i386_pecoffwince
  169. ,as_arm_pecoffwince
  170. ,as_x86_64_elf64
  171. ,as_sparc_elf32
  172. ,as_ggas { gnu assembler called "gas" instead of "as" }
  173. ,as_i386_nasmhaiku
  174. ,as_powerpc_vasm
  175. ,as_i386_nlmcoff
  176. );
  177. tar = (ar_none
  178. ,ar_gnu_ar
  179. ,ar_mpw_ar
  180. ,ar_gnu_ar_scripted
  181. ,ar_gnu_gar
  182. );
  183. tres = (res_none
  184. ,res_gnu_windres,res_watcom_wrc_os2
  185. ,res_m68k_palmos,res_m68k_mpw
  186. ,res_powerpc_mpw,res_elf
  187. ,res_win64_gorc, res_macho, res_ext
  188. );
  189. tresinfoflags = (res_external_file,res_arch_in_file_name
  190. ,res_single_file);
  191. tdbg = (dbg_none
  192. ,dbg_stabs,dbg_dwarf2,dbg_dwarf3,dbg_dwarf4
  193. );
  194. tscripttype = (script_none
  195. ,script_dos,script_unix,script_amiga,
  196. script_mpw
  197. );
  198. tabi = (abi_default
  199. ,abi_powerpc_sysv,abi_powerpc_aix
  200. ,abi_eabi,abi_armeb
  201. );
  202. {*****************************************************************************
  203. Structures
  204. *****************************************************************************}
  205. type
  206. { Abstract linker class which is implemented in link module }
  207. TAbstractLinker = class
  208. end;
  209. TAbstractLinkerClass = class of TAbstractLinker;
  210. { Abstract assembler class which is implemented in assemble module }
  211. TAbstractAssembler = class
  212. end;
  213. TAbstractAssemblerClass = class of TAbstractAssembler;
  214. TAbstractResourceFile = class
  215. constructor create(const fn : ansistring);virtual;abstract;
  216. end;
  217. TAbstractResourceFileClass = class of TAbstractResourceFile;
  218. palignmentinfo = ^talignmentinfo;
  219. { this is written to ppus during token recording for generics so it must be packed }
  220. talignmentinfo = packed record
  221. procalign,
  222. loopalign,
  223. jumpalign,
  224. constalignmin,
  225. constalignmax,
  226. varalignmin,
  227. varalignmax,
  228. localalignmin,
  229. localalignmax,
  230. recordalignmin,
  231. recordalignmax,
  232. maxCrecordalign : longint;
  233. end;
  234. tasmflags = (af_none,
  235. af_outputbinary,af_allowdirect,
  236. af_needar,af_smartlink_sections,
  237. af_labelprefix_only_inside_procedure,
  238. af_supports_dwarf,
  239. af_no_debug,
  240. af_stabs_use_function_absolute_addresses
  241. );
  242. pasminfo = ^tasminfo;
  243. tasminfo = record
  244. id : tasm;
  245. idtxt : string[12];
  246. asmbin : string[8];
  247. asmcmd : string[50];
  248. supported_targets : set of tsystem;
  249. flags : set of tasmflags;
  250. labelprefix : string[3];
  251. comment : string[3];
  252. end;
  253. parinfo = ^tarinfo;
  254. tarinfo = record
  255. id : tar;
  256. arcmd : string[50];
  257. arfinishcmd : string[10];
  258. end;
  259. presinfo = ^tresinfo;
  260. tresinfo = record
  261. id : tres;
  262. { Compiler for resource (.rc or .res) to obj }
  263. resbin : string[10];
  264. rescmd : string[50];
  265. { Optional compiler for resource script (.rc) to binary resource (.res). }
  266. { If it is not provided resbin and rescmd will be used. }
  267. rcbin : string[10];
  268. rccmd : string[50];
  269. resourcefileclass : TAbstractResourceFileClass;
  270. resflags : set of tresinfoflags;
  271. end;
  272. pdbginfo = ^tdbginfo;
  273. tdbginfo = record
  274. id : tdbg;
  275. idtxt : string[12];
  276. end;
  277. tsystemflags = (tf_none,
  278. tf_under_development,
  279. tf_need_export,
  280. tf_needs_isconsole,
  281. tf_code_small,
  282. tf_static_reg_based,
  283. tf_needs_symbol_size,
  284. tf_smartlink_sections,
  285. tf_smartlink_library,
  286. tf_needs_dwarf_cfi,
  287. tf_use_8_3,
  288. tf_pic_uses_got,
  289. tf_library_needs_pic,
  290. tf_needs_symbol_type,
  291. tf_section_threadvars,
  292. tf_files_case_sensitive,
  293. tf_files_case_aware,
  294. tf_p_ext_support,
  295. tf_has_dllscanner,
  296. tf_use_function_relative_addresses,
  297. tf_winlikewidestring,
  298. tf_dwarf_relative_addresses, // use offsets where the Dwarf spec requires this instead of absolute addresses (the latter is needed by Linux binutils)
  299. tf_dwarf_only_local_labels, // only use local labels inside the Dwarf debug_info section (needed for e.g. Darwin)
  300. tf_requires_proper_alignment,
  301. tf_no_pic_supported,
  302. tf_pic_default,
  303. { the os does some kind of stack checking and it can be converted into a rte 202 }
  304. tf_no_generic_stackcheck,
  305. tf_has_winlike_resources,
  306. tf_safecall_clearstack, // With this flag set, after safecall calls the caller cleans up the stack
  307. tf_safecall_exceptions // Exceptions in safecall calls are not raised, but passed to the caller as an ordinal (hresult) in the function result.
  308. // The original result (if it exists) is passed as an extra parameter
  309. );
  310. psysteminfo = ^tsysteminfo;
  311. { using packed causes bus errors on processors which require alignment }
  312. tsysteminfo = record
  313. system : tsystem;
  314. name : string[34];
  315. shortname : string[9];
  316. flags : set of tsystemflags;
  317. cpu : tsystemcpu;
  318. unit_env : string[16];
  319. extradefines : string[40];
  320. exeext,
  321. defext,
  322. scriptext,
  323. smartext,
  324. unitext,
  325. unitlibext,
  326. asmext,
  327. objext,
  328. resext : string[4];
  329. resobjext : string[7];
  330. sharedlibext : string[10];
  331. staticlibext,
  332. staticlibprefix : string[4];
  333. sharedlibprefix : string[4];
  334. sharedClibext : string[10];
  335. staticClibext,
  336. staticClibprefix : string[4];
  337. sharedClibprefix : string[4];
  338. importlibprefix : string[10];
  339. importlibext : string[4];
  340. Cprefix : string[2];
  341. newline : string[2];
  342. dirsep : char;
  343. assem : tasm;
  344. assemextern : tasm; { external assembler, used by -a }
  345. link : tabstractlinkerclass;
  346. linkextern : tabstractlinkerclass; { external linker, used by -s }
  347. ar : tar;
  348. res : tres;
  349. dbg : tdbg;
  350. script : tscripttype;
  351. endian : tendian;
  352. alignment : talignmentinfo;
  353. {
  354. Offset from the argument pointer register to the first
  355. argument's address. On some machines it may depend on
  356. the data type of the function.
  357. (see also FIRST_PARM_OFFSET in GCC source)
  358. }
  359. first_parm_offset : longint;
  360. stacksize : longint;
  361. abi : tabi;
  362. end;
  363. const
  364. { alias for supported_target field in tasminfo }
  365. system_any = system_none;
  366. systems_wince = [system_arm_wince,system_i386_wince];
  367. systems_linux = [system_i386_linux,system_x86_64_linux,system_powerpc_linux,system_powerpc64_linux,
  368. system_arm_linux,system_sparc_linux,system_alpha_linux,system_m68k_linux,
  369. system_x86_6432_linux,system_mips_linux,system_mipsel_linux];
  370. { all real windows systems, no cripple ones like wince, wdosx et. al. }
  371. systems_windows = [system_i386_win32,system_x86_64_win64,system_ia64_win64];
  372. { all windows systems }
  373. systems_all_windows = [system_i386_win32,system_x86_64_win64,system_ia64_win64,
  374. system_arm_wince,system_i386_wince];
  375. { all darwin systems }
  376. systems_darwin = [system_powerpc_darwin,system_i386_darwin,
  377. system_powerpc64_darwin,system_x86_64_darwin,
  378. system_arm_darwin,system_i386_iphonesim];
  379. {all solaris systems }
  380. systems_solaris = [system_sparc_solaris, system_i386_solaris,
  381. system_x86_64_solaris];
  382. { systems supporting Objective-C }
  383. systems_objc_supported = systems_darwin;
  384. { systems using the non-fragile Objective-C ABI }
  385. systems_objc_nfabi = [system_powerpc64_darwin,system_x86_64_darwin,system_arm_darwin,system_i386_iphonesim];
  386. { all embedded systems }
  387. systems_embedded = [system_i386_embedded,system_m68k_embedded,
  388. system_alpha_embedded,system_powerpc_embedded,
  389. system_sparc_embedded,system_vm_embedded,
  390. system_iA64_embedded,system_x86_64_embedded,
  391. system_mips_embedded,system_arm_embedded,
  392. system_powerpc64_embedded];
  393. { all systems supporting exports from programs or units }
  394. systems_unit_program_exports = [system_i386_win32,
  395. system_i386_wdosx,
  396. system_i386_Netware,
  397. system_i386_netwlibc,
  398. system_arm_wince,
  399. system_x86_64_win64,
  400. system_ia64_win64]+systems_linux;
  401. { all systems for which weak linking has been tested/is supported }
  402. systems_weak_linking = systems_darwin + systems_solaris;
  403. systems_internal_sysinit = [system_i386_linux,system_i386_win32];
  404. systems_interrupt_table = [{system_arm_embedded}];
  405. { all symbian systems }
  406. systems_symbian = [system_i386_symbian,system_arm_symbian];
  407. { all native nt systems }
  408. systems_nativent = [system_i386_nativent];
  409. { all systems for which istack must be at a 16 byte boundary
  410. when calling a function }
  411. systems_need_16_byte_stack_alignment = [
  412. system_i386_darwin,
  413. system_i386_iphonesim,
  414. system_x86_64_darwin,
  415. system_x86_64_win64,
  416. system_x86_64_linux,
  417. system_x86_64_freebsd,
  418. system_x86_64_solaris];
  419. cpu2str : array[TSystemCpu] of string[10] =
  420. ('','i386','m68k','alpha','powerpc','sparc','vm','ia64','x86_64',
  421. 'mips','arm', 'powerpc64', 'avr', 'mipsel');
  422. abi2str : array[tabi] of string[10] =
  423. ('DEFAULT','SYSV','AIX','EABI','ARMEB');
  424. var
  425. targetinfos : array[tsystem] of psysteminfo;
  426. arinfos : array[tar] of parinfo;
  427. resinfos : array[tres] of presinfo;
  428. asminfos : array[tasm] of pasminfo;
  429. dbginfos : array[tdbg] of pdbginfo;
  430. source_info : tsysteminfo;
  431. target_cpu : tsystemcpu;
  432. target_info : tsysteminfo;
  433. target_asm : tasminfo;
  434. target_ar : tarinfo;
  435. target_res : tresinfo;
  436. target_dbg : tdbginfo;
  437. target_cpu_string,
  438. target_os_string : string[12]; { for rtl/<X>/,fcl/<X>/, etc. }
  439. target_full_string : string[24];
  440. function set_target(t:tsystem):boolean;
  441. function set_target_asm(t:tasm):boolean;
  442. function set_target_ar(t:tar):boolean;
  443. function set_target_res(t:tres):boolean;
  444. function set_target_dbg(t:tdbg):boolean;
  445. function find_system_by_string(const s : string) : tsystem;
  446. function find_asm_by_string(const s : string) : tasm;
  447. function find_dbg_by_string(const s : string) : tdbg;
  448. procedure set_source_info(const ti : tsysteminfo);
  449. procedure UpdateAlignment(var d:talignmentinfo;const s:talignmentinfo);
  450. procedure RegisterTarget(const r:tsysteminfo);
  451. procedure RegisterRes(const r:tresinfo; rcf : TAbstractResourceFileClass);
  452. procedure RegisterAr(const r:tarinfo);
  453. { Register the external linker. This routine is called to setup the
  454. class to use for the linker. It returns the tsysteminfo structure
  455. updated with the correct linker class for external linking.
  456. }
  457. procedure RegisterExternalLinker(var system_info: tsysteminfo; c:TAbstractLinkerClass);
  458. { Register the internal linker. This routine is called to setup the
  459. class to use for the linker. It returns the tsysteminfo structure
  460. updated with the correct linker class for internal linking.
  461. If internal linking is not supported, this class can be set
  462. to nil.
  463. }
  464. procedure RegisterInternalLinker(var system_info : tsysteminfo; c:TAbstractLinkerClass);
  465. procedure InitSystems;
  466. {$ifdef FreeBSD}
  467. function GetOSRelDate:Longint;
  468. {$endif}
  469. implementation
  470. uses
  471. cutils{$ifdef FreeBSD},SysCtl,BaseUnix{$endif};
  472. {****************************************************************************
  473. OS runtime version detection utility routine
  474. ****************************************************************************}
  475. {$ifdef FreeBSD}
  476. function GetOSRelDate:Longint;
  477. var
  478. mib : array[0..1] of cint;
  479. rval : cint;
  480. len : size_t;
  481. i : longint;
  482. v : longint;
  483. oerrno : cint;
  484. S : AnsiString;
  485. Begin
  486. s:='ab';
  487. SetLength(S,50);
  488. mib[0] := CTL_KERN;
  489. mib[1] := KERN_OSRELDATE;
  490. len := 4;
  491. oerrno:= fpgeterrno;
  492. if (FPsysctl(pChar(@mib), 2, pchar(@v), @len, NIL, 0) = -1) Then
  493. Begin
  494. if (fpgeterrno = ESysENOMEM) Then
  495. fpseterrno(oerrno);
  496. GetOSRelDate:=0;
  497. End
  498. else
  499. GetOSRelDate:=v;
  500. End;
  501. {$endif}
  502. {****************************************************************************
  503. Target setting
  504. ****************************************************************************}
  505. function set_target(t:tsystem):boolean;
  506. begin
  507. set_target:=false;
  508. if assigned(targetinfos[t]) then
  509. begin
  510. target_info:=targetinfos[t]^;
  511. set_target_asm(target_info.assem);
  512. set_target_ar(target_info.ar);
  513. set_target_res(target_info.res);
  514. set_target_dbg(target_info.dbg);
  515. target_cpu:=target_info.cpu;
  516. target_os_string:=lower(target_info.shortname);
  517. target_cpu_string:=cpu2str[target_cpu];
  518. target_full_string:=target_cpu_string+'-'+target_os_string;
  519. set_target:=true;
  520. exit;
  521. end;
  522. end;
  523. function set_target_asm(t:tasm):boolean;
  524. begin
  525. set_target_asm:=false;
  526. if assigned(asminfos[t]) and
  527. ((target_info.system in asminfos[t]^.supported_targets) or
  528. (system_any in asminfos[t]^.supported_targets)) then
  529. begin
  530. target_asm:=asminfos[t]^;
  531. set_target_asm:=true;
  532. exit;
  533. end;
  534. end;
  535. function set_target_ar(t:tar):boolean;
  536. begin
  537. result:=false;
  538. if assigned(arinfos[t]) then
  539. begin
  540. target_ar:=arinfos[t]^;
  541. result:=true;
  542. exit;
  543. end;
  544. end;
  545. function set_target_res(t:tres):boolean;
  546. begin
  547. result:=false;
  548. if assigned(resinfos[t]) then
  549. begin
  550. target_res:=resinfos[t]^;
  551. result:=true;
  552. exit;
  553. end
  554. else
  555. FillByte(target_res,sizeof(target_res),0);
  556. end;
  557. function set_target_dbg(t:tdbg):boolean;
  558. begin
  559. result:=false;
  560. if assigned(dbginfos[t]) then
  561. begin
  562. target_dbg:=dbginfos[t]^;
  563. result:=true;
  564. exit;
  565. end;
  566. end;
  567. function find_system_by_string(const s : string) : tsystem;
  568. var
  569. hs : string;
  570. t : tsystem;
  571. begin
  572. result:=system_none;
  573. hs:=upper(s);
  574. for t:=low(tsystem) to high(tsystem) do
  575. if assigned(targetinfos[t]) and
  576. (upper(targetinfos[t]^.shortname)=hs) then
  577. begin
  578. result:=t;
  579. exit;
  580. end;
  581. end;
  582. function find_asm_by_string(const s : string) : tasm;
  583. var
  584. hs : string;
  585. t : tasm;
  586. begin
  587. result:=as_none;
  588. hs:=upper(s);
  589. for t:=low(tasm) to high(tasm) do
  590. if assigned(asminfos[t]) and
  591. (asminfos[t]^.idtxt=hs) then
  592. begin
  593. result:=t;
  594. exit;
  595. end;
  596. end;
  597. function find_dbg_by_string(const s : string) : tdbg;
  598. var
  599. hs : string;
  600. t : tdbg;
  601. begin
  602. result:=dbg_none;
  603. hs:=upper(s);
  604. for t:=low(tdbg) to high(tdbg) do
  605. if assigned(dbginfos[t]) and
  606. (dbginfos[t]^.idtxt=hs) then
  607. begin
  608. result:=t;
  609. exit;
  610. end;
  611. end;
  612. procedure UpdateAlignment(var d:talignmentinfo;const s:talignmentinfo);
  613. begin
  614. with d do
  615. begin
  616. { general update rules:
  617. minimum: if higher then update
  618. maximum: if lower then update or if undefined then update }
  619. if s.procalign>procalign then
  620. procalign:=s.procalign;
  621. if s.loopalign>loopalign then
  622. loopalign:=s.loopalign;
  623. if s.jumpalign>jumpalign then
  624. jumpalign:=s.jumpalign;
  625. if s.constalignmin>constalignmin then
  626. constalignmin:=s.constalignmin;
  627. if (constalignmax=0) or
  628. ((s.constalignmax>0) and (s.constalignmax<constalignmax)) then
  629. constalignmax:=s.constalignmax;
  630. if s.varalignmin>varalignmin then
  631. varalignmin:=s.varalignmin;
  632. if (varalignmax=0) or
  633. ((s.varalignmax>0) and (s.varalignmax<varalignmax)) then
  634. varalignmax:=s.varalignmax;
  635. if s.localalignmin>localalignmin then
  636. localalignmin:=s.localalignmin;
  637. if (localalignmax=0) or
  638. ((s.localalignmax>0) and (s.localalignmax<localalignmax)) then
  639. localalignmax:=s.localalignmax;
  640. if s.recordalignmin>recordalignmin then
  641. recordalignmin:=s.recordalignmin;
  642. if (recordalignmax=0) or
  643. ((s.recordalignmax>0) and (s.recordalignmax<recordalignmax)) then
  644. recordalignmax:=s.recordalignmax;
  645. if (maxCrecordalign=0) or
  646. ((s.maxCrecordalign>0) and (s.maxCrecordalign<maxCrecordalign)) then
  647. maxCrecordalign:=s.maxCrecordalign;
  648. end;
  649. end;
  650. {****************************************************************************
  651. Target registration
  652. ****************************************************************************}
  653. procedure RegisterTarget(const r:tsysteminfo);
  654. var
  655. t : tsystem;
  656. begin
  657. t:=r.system;
  658. if assigned(targetinfos[t]) then
  659. writeln('Warning: Target is already registered!')
  660. else
  661. Getmem(targetinfos[t],sizeof(tsysteminfo));
  662. targetinfos[t]^:=r;
  663. end;
  664. procedure RegisterRes(const r:tresinfo; rcf : TAbstractResourceFileClass);
  665. var
  666. t : tres;
  667. begin
  668. t:=r.id;
  669. if not assigned(resinfos[t]) then
  670. Getmem(resinfos[t],sizeof(tresinfo));
  671. resinfos[t]^:=r;
  672. resinfos[t]^.resourcefileclass:=rcf;
  673. end;
  674. procedure RegisterAr(const r:tarinfo);
  675. var
  676. t : tar;
  677. begin
  678. t:=r.id;
  679. if assigned(arinfos[t]) then
  680. writeln('Warning: ar is already registered!')
  681. else
  682. Getmem(arinfos[t],sizeof(tarinfo));
  683. arinfos[t]^:=r;
  684. end;
  685. procedure RegisterExternalLinker(var system_info: tsysteminfo; c:TAbstractLinkerClass);
  686. begin
  687. system_info.linkextern := c;
  688. end;
  689. procedure RegisterInternalLinker(var system_info : tsysteminfo; c:TAbstractLinkerClass);
  690. begin
  691. system_info.link := c;
  692. end;
  693. procedure DeregisterInfos;
  694. var
  695. assem : tasm;
  696. target : tsystem;
  697. ar : tar;
  698. res : tres;
  699. dbg : tdbg;
  700. begin
  701. for target:=low(tsystem) to high(tsystem) do
  702. if assigned(targetinfos[target]) then
  703. begin
  704. freemem(targetinfos[target],sizeof(tsysteminfo));
  705. targetinfos[target]:=nil;
  706. end;
  707. for assem:=low(tasm) to high(tasm) do
  708. if assigned(asminfos[assem]) then
  709. begin
  710. freemem(asminfos[assem],sizeof(tasminfo));
  711. asminfos[assem]:=nil;
  712. end;
  713. for ar:=low(tar) to high(tar) do
  714. if assigned(arinfos[ar]) then
  715. begin
  716. freemem(arinfos[ar],sizeof(tarinfo));
  717. arinfos[ar]:=nil;
  718. end;
  719. for res:=low(tres) to high(tres) do
  720. if assigned(resinfos[res]) then
  721. begin
  722. freemem(resinfos[res],sizeof(tresinfo));
  723. resinfos[res]:=nil;
  724. end;
  725. for dbg:=low(tdbg) to high(tdbg) do
  726. if assigned(dbginfos[dbg]) then
  727. begin
  728. freemem(dbginfos[dbg],sizeof(tdbginfo));
  729. dbginfos[dbg]:=nil;
  730. end;
  731. end;
  732. {****************************************************************************
  733. Initialization of default target
  734. ****************************************************************************}
  735. procedure default_target(t:tsystem);
  736. begin
  737. set_target(t);
  738. if source_info.name='' then
  739. source_info:=target_info;
  740. end;
  741. procedure set_source_info(const ti : tsysteminfo);
  742. begin
  743. { can't use message() here (PFV) }
  744. if source_info.name<>'' then
  745. Writeln('Warning: Source OS Redefined!');
  746. source_info:=ti;
  747. end;
  748. procedure InitSystems;
  749. begin
  750. { Now default target, this is dependent on the target cpu define,
  751. when the define is the same as the source cpu then we use the source
  752. os, else we pick a default }
  753. {$ifdef i386}
  754. {$ifdef cpu86}
  755. default_target(source_info.system);
  756. {$define default_target_set}
  757. {$else cpu86}
  758. {$ifdef linux}
  759. default_target(system_i386_linux);
  760. {$define default_target_set}
  761. {$endif}
  762. {$ifdef freebsd}
  763. default_target(system_i386_freebsd);
  764. {$define default_target_set}
  765. {$endif}
  766. {$ifdef darwin}
  767. default_target(system_i386_darwin);
  768. {$define default_target_set}
  769. {$endif}
  770. {$endif cpu86}
  771. { default is linux }
  772. {$ifndef default_target_set}
  773. default_target(system_i386_linux);
  774. {$endif default_target_set}
  775. {$endif i386}
  776. {$ifdef x86_64}
  777. {$ifdef cpux86_64}
  778. default_target(source_info.system);
  779. {$define default_target_set}
  780. {$else cpux86_64}
  781. {$ifdef MSWindows}
  782. default_target(system_x86_64_win64);
  783. {$define default_target_set}
  784. {$endif}
  785. {$ifdef linux}
  786. default_target(system_x86_64_linux);
  787. {$define default_target_set}
  788. {$endif}
  789. {$ifdef freebsd}
  790. default_target(system_x86_64_freebsd);
  791. {$define default_target_set}
  792. {$endif}
  793. {$ifdef solaris}
  794. default_target(system_x86_64_solaris);
  795. {$define default_target_set}
  796. {$endif}
  797. {$ifdef darwin}
  798. default_target(system_x86_64_darwin);
  799. {$define default_target_set}
  800. {$endif}
  801. {$endif cpux86_64}
  802. { default is linux }
  803. {$ifndef default_target_set}
  804. default_target(system_x86_64_linux);
  805. {$endif default_target_set}
  806. {$endif x86_64}
  807. {$ifdef m68k}
  808. {$ifdef cpu68}
  809. default_target(source_info.target);
  810. {$else cpu68}
  811. default_target(system_m68k_linux);
  812. {$endif cpu68}
  813. {$endif m68k}
  814. {$ifdef alpha}
  815. {$ifdef cpualpha}
  816. default_target(source_info.system);
  817. {$else cpualpha}
  818. default_target(system_alpha_linux);
  819. {$endif cpualpha}
  820. {$endif alpha}
  821. {$ifdef powerpc}
  822. {$ifdef cpupowerpc}
  823. default_target(source_info.system);
  824. {$define default_target_set}
  825. {$else cpupowerpc}
  826. {$ifdef linux}
  827. default_target(system_powerpc_linux);
  828. {$define default_target_set}
  829. {$endif}
  830. {$ifdef darwin}
  831. default_target(system_powerpc_darwin);
  832. {$define default_target_set}
  833. {$endif}
  834. {$endif cpupowerpc}
  835. {$ifndef default_target_set}
  836. default_target(system_powerpc_linux);
  837. {$endif default_target_set}
  838. {$endif powerpc}
  839. {$ifdef POWERPC64}
  840. {$ifdef cpupowerpc64}
  841. default_target(source_info.system);
  842. {$define default_target_set}
  843. {$else cpupowerpc64}
  844. {$ifdef darwin}
  845. default_target(system_powerpc64_darwin);
  846. {$define default_target_set}
  847. {$endif}
  848. {$ifdef linux}
  849. default_target(system_powerpc64_linux);
  850. {$define default_target_set}
  851. {$endif}
  852. {$endif cpupowerpc64}
  853. {$ifndef default_target_set}
  854. default_target(system_powerpc64_linux);
  855. {$define default_target_set}
  856. {$endif}
  857. {$endif POWERPC64}
  858. {$ifdef sparc}
  859. {$ifdef cpusparc}
  860. default_target(source_info.system);
  861. {$else cpusparc}
  862. default_target(system_sparc_linux);
  863. {$endif cpusparc}
  864. {$endif sparc}
  865. {$ifdef arm}
  866. {$ifdef cpuarm}
  867. default_target(source_info.system);
  868. {$else cpuarm}
  869. {$ifdef WINDOWS}
  870. {$define default_target_set}
  871. default_target(system_arm_wince);
  872. {$endif}
  873. {$ifdef linux}
  874. {$define default_target_set}
  875. default_target(system_arm_linux);
  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. default_target(system_mipsel_linux);
  893. {$else mipsel}
  894. default_target(system_mips_linux);
  895. {$endif mipsel}
  896. {$endif mips}
  897. end;
  898. initialization
  899. source_info.name:='';
  900. finalization
  901. DeregisterInfos;
  902. end.