systems.pas 31 KB

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