systems.pas 31 KB

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