systems.pas 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034
  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. { all embedded systems }
  383. systems_embedded = [system_i386_embedded,system_m68k_embedded,
  384. system_alpha_embedded,system_powerpc_embedded,
  385. system_sparc_embedded,system_vm_embedded,
  386. system_iA64_embedded,system_x86_64_embedded,
  387. system_mips_embedded,system_arm_embedded,
  388. system_powerpc64_embedded];
  389. { all symbian systems }
  390. systems_symbian = [system_i386_symbian,system_arm_symbian];
  391. { all classic Mac OS targets }
  392. systems_macos = [system_m68k_Mac,system_powerpc_Macos];
  393. { all OS/2 targets }
  394. systems_os2 = [system_i386_OS2,system_i386_emx];
  395. { all native nt systems }
  396. systems_nativent = [system_i386_nativent];
  397. { systems supporting Objective-C }
  398. systems_objc_supported = systems_darwin;
  399. { systems using the non-fragile Objective-C ABI }
  400. systems_objc_nfabi = [system_powerpc64_darwin,system_x86_64_darwin,system_arm_darwin,system_i386_iphonesim];
  401. { all systems supporting exports from programs or units }
  402. systems_unit_program_exports = [system_i386_win32,
  403. system_i386_wdosx,
  404. system_i386_Netware,
  405. system_i386_netwlibc,
  406. system_arm_wince,
  407. system_x86_64_win64,
  408. system_ia64_win64]+systems_linux;
  409. { all systems for which weak linking has been tested/is supported }
  410. systems_weak_linking = systems_darwin + systems_solaris;
  411. systems_internal_sysinit = [system_i386_linux,system_i386_win32];
  412. systems_interrupt_table = [{system_arm_embedded}];
  413. { all systems for which istack must be at a 16 byte boundary
  414. when calling a function }
  415. systems_need_16_byte_stack_alignment = [
  416. system_i386_darwin,
  417. system_i386_iphonesim,
  418. system_x86_64_darwin,
  419. system_x86_64_win64,
  420. system_x86_64_linux,
  421. system_x86_64_freebsd,
  422. system_x86_64_solaris];
  423. cpu2str : array[TSystemCpu] of string[10] =
  424. ('','i386','m68k','alpha','powerpc','sparc','vm','ia64','x86_64',
  425. 'mips','arm', 'powerpc64', 'avr', 'mipsel');
  426. abi2str : array[tabi] of string[10] =
  427. ('DEFAULT','SYSV','AIX','EABI','ARMEB');
  428. var
  429. targetinfos : array[tsystem] of psysteminfo;
  430. arinfos : array[tar] of parinfo;
  431. resinfos : array[tres] of presinfo;
  432. asminfos : array[tasm] of pasminfo;
  433. dbginfos : array[tdbg] of pdbginfo;
  434. source_info : tsysteminfo;
  435. target_cpu : tsystemcpu;
  436. target_info : tsysteminfo;
  437. target_asm : tasminfo;
  438. target_ar : tarinfo;
  439. target_res : tresinfo;
  440. target_dbg : tdbginfo;
  441. target_cpu_string,
  442. target_os_string : string[12]; { for rtl/<X>/,fcl/<X>/, etc. }
  443. target_full_string : string[24];
  444. function set_target(t:tsystem):boolean;
  445. function set_target_asm(t:tasm):boolean;
  446. function set_target_ar(t:tar):boolean;
  447. function set_target_res(t:tres):boolean;
  448. function set_target_dbg(t:tdbg):boolean;
  449. function find_system_by_string(const s : string) : tsystem;
  450. function find_asm_by_string(const s : string) : tasm;
  451. function find_dbg_by_string(const s : string) : tdbg;
  452. procedure set_source_info(const ti : tsysteminfo);
  453. function UpdateAlignment(var d:talignmentinfo;const s:talignmentinfo) : boolean;
  454. procedure RegisterTarget(const r:tsysteminfo);
  455. procedure RegisterRes(const r:tresinfo; rcf : TAbstractResourceFileClass);
  456. procedure RegisterAr(const r:tarinfo);
  457. { Register the external linker. This routine is called to setup the
  458. class to use for the linker. It returns the tsysteminfo structure
  459. updated with the correct linker class for external linking.
  460. }
  461. procedure RegisterExternalLinker(var system_info: tsysteminfo; c:TAbstractLinkerClass);
  462. { Register the internal linker. This routine is called to setup the
  463. class to use for the linker. It returns the tsysteminfo structure
  464. updated with the correct linker class for internal linking.
  465. If internal linking is not supported, this class can be set
  466. to nil.
  467. }
  468. procedure RegisterInternalLinker(var system_info : tsysteminfo; c:TAbstractLinkerClass);
  469. procedure InitSystems;
  470. {$ifdef FreeBSD}
  471. function GetOSRelDate:Longint;
  472. {$endif}
  473. implementation
  474. uses
  475. cutils{$ifdef FreeBSD},SysCtl,BaseUnix{$endif};
  476. {****************************************************************************
  477. OS runtime version detection utility routine
  478. ****************************************************************************}
  479. {$ifdef FreeBSD}
  480. function GetOSRelDate:Longint;
  481. var
  482. mib : array[0..1] of cint;
  483. rval : cint;
  484. len : size_t;
  485. i : longint;
  486. v : longint;
  487. oerrno : cint;
  488. S : AnsiString;
  489. Begin
  490. s:='ab';
  491. SetLength(S,50);
  492. mib[0] := CTL_KERN;
  493. mib[1] := KERN_OSRELDATE;
  494. len := 4;
  495. oerrno:= fpgeterrno;
  496. if (FPsysctl(pChar(@mib), 2, pchar(@v), @len, NIL, 0) = -1) Then
  497. Begin
  498. if (fpgeterrno = ESysENOMEM) Then
  499. fpseterrno(oerrno);
  500. GetOSRelDate:=0;
  501. End
  502. else
  503. GetOSRelDate:=v;
  504. End;
  505. {$endif}
  506. {****************************************************************************
  507. Target setting
  508. ****************************************************************************}
  509. function set_target(t:tsystem):boolean;
  510. begin
  511. set_target:=false;
  512. if assigned(targetinfos[t]) then
  513. begin
  514. target_info:=targetinfos[t]^;
  515. set_target_asm(target_info.assem);
  516. set_target_ar(target_info.ar);
  517. set_target_res(target_info.res);
  518. set_target_dbg(target_info.dbg);
  519. target_cpu:=target_info.cpu;
  520. target_os_string:=lower(target_info.shortname);
  521. target_cpu_string:=cpu2str[target_cpu];
  522. target_full_string:=target_cpu_string+'-'+target_os_string;
  523. set_target:=true;
  524. exit;
  525. end;
  526. end;
  527. function set_target_asm(t:tasm):boolean;
  528. begin
  529. set_target_asm:=false;
  530. if assigned(asminfos[t]) and
  531. ((target_info.system in asminfos[t]^.supported_targets) or
  532. (system_any in asminfos[t]^.supported_targets)) then
  533. begin
  534. target_asm:=asminfos[t]^;
  535. set_target_asm:=true;
  536. exit;
  537. end;
  538. end;
  539. function set_target_ar(t:tar):boolean;
  540. begin
  541. result:=false;
  542. if assigned(arinfos[t]) then
  543. begin
  544. target_ar:=arinfos[t]^;
  545. result:=true;
  546. exit;
  547. end;
  548. end;
  549. function set_target_res(t:tres):boolean;
  550. begin
  551. result:=false;
  552. if assigned(resinfos[t]) then
  553. begin
  554. target_res:=resinfos[t]^;
  555. result:=true;
  556. exit;
  557. end
  558. else
  559. FillByte(target_res,sizeof(target_res),0);
  560. end;
  561. function set_target_dbg(t:tdbg):boolean;
  562. begin
  563. result:=false;
  564. if assigned(dbginfos[t]) then
  565. begin
  566. target_dbg:=dbginfos[t]^;
  567. result:=true;
  568. exit;
  569. end;
  570. end;
  571. function find_system_by_string(const s : string) : tsystem;
  572. var
  573. hs : string;
  574. t : tsystem;
  575. begin
  576. result:=system_none;
  577. hs:=upper(s);
  578. for t:=low(tsystem) to high(tsystem) do
  579. if assigned(targetinfos[t]) and
  580. (upper(targetinfos[t]^.shortname)=hs) then
  581. begin
  582. result:=t;
  583. exit;
  584. end;
  585. end;
  586. function find_asm_by_string(const s : string) : tasm;
  587. var
  588. hs : string;
  589. t : tasm;
  590. begin
  591. result:=as_none;
  592. hs:=upper(s);
  593. for t:=low(tasm) to high(tasm) do
  594. if assigned(asminfos[t]) and
  595. (asminfos[t]^.idtxt=hs) then
  596. begin
  597. result:=t;
  598. exit;
  599. end;
  600. end;
  601. function find_dbg_by_string(const s : string) : tdbg;
  602. var
  603. hs : string;
  604. t : tdbg;
  605. begin
  606. result:=dbg_none;
  607. hs:=upper(s);
  608. for t:=low(tdbg) to high(tdbg) do
  609. if assigned(dbginfos[t]) and
  610. (dbginfos[t]^.idtxt=hs) then
  611. begin
  612. result:=t;
  613. exit;
  614. end;
  615. end;
  616. function UpdateAlignment(var d:talignmentinfo;const s:talignmentinfo) : boolean;
  617. begin
  618. result:=true;
  619. with d do
  620. begin
  621. if (s.procalign in [1,2,4,8,16,32,64,128]) or (s.procalign=256) then
  622. procalign:=s.procalign
  623. else if s.procalign<>0 then
  624. result:=false;
  625. if (s.loopalign in [1,2,4,8,16,32,64,128]) or (s.loopalign=256) then
  626. loopalign:=s.loopalign
  627. else if s.loopalign<>0 then
  628. result:=false;
  629. if (s.jumpalign in [1,2,4,8,16,32,64,128]) or (s.jumpalign=256) then
  630. jumpalign:=s.jumpalign
  631. else if s.jumpalign<>0 then
  632. result:=false;
  633. { general update rules:
  634. minimum: if higher then update
  635. maximum: if lower then update or if undefined then update }
  636. if s.constalignmin>constalignmin then
  637. constalignmin:=s.constalignmin;
  638. if (constalignmax=0) or
  639. ((s.constalignmax>0) and (s.constalignmax<constalignmax)) then
  640. constalignmax:=s.constalignmax;
  641. if s.varalignmin>varalignmin then
  642. varalignmin:=s.varalignmin;
  643. if (varalignmax=0) or
  644. ((s.varalignmax>0) and (s.varalignmax<varalignmax)) then
  645. varalignmax:=s.varalignmax;
  646. if s.localalignmin>localalignmin then
  647. localalignmin:=s.localalignmin;
  648. if (localalignmax=0) or
  649. ((s.localalignmax>0) and (s.localalignmax<localalignmax)) then
  650. localalignmax:=s.localalignmax;
  651. if s.recordalignmin>recordalignmin then
  652. recordalignmin:=s.recordalignmin;
  653. if (recordalignmax=0) or
  654. ((s.recordalignmax>0) and (s.recordalignmax<recordalignmax)) then
  655. recordalignmax:=s.recordalignmax;
  656. if (maxCrecordalign=0) or
  657. ((s.maxCrecordalign>0) and (s.maxCrecordalign<maxCrecordalign)) then
  658. maxCrecordalign:=s.maxCrecordalign;
  659. end;
  660. end;
  661. {****************************************************************************
  662. Target registration
  663. ****************************************************************************}
  664. procedure RegisterTarget(const r:tsysteminfo);
  665. var
  666. t : tsystem;
  667. begin
  668. t:=r.system;
  669. if assigned(targetinfos[t]) then
  670. writeln('Warning: Target is already registered!')
  671. else
  672. Getmem(targetinfos[t],sizeof(tsysteminfo));
  673. targetinfos[t]^:=r;
  674. end;
  675. procedure RegisterRes(const r:tresinfo; rcf : TAbstractResourceFileClass);
  676. var
  677. t : tres;
  678. begin
  679. t:=r.id;
  680. if not assigned(resinfos[t]) then
  681. Getmem(resinfos[t],sizeof(tresinfo));
  682. resinfos[t]^:=r;
  683. resinfos[t]^.resourcefileclass:=rcf;
  684. end;
  685. procedure RegisterAr(const r:tarinfo);
  686. var
  687. t : tar;
  688. begin
  689. t:=r.id;
  690. if assigned(arinfos[t]) then
  691. writeln('Warning: ar is already registered!')
  692. else
  693. Getmem(arinfos[t],sizeof(tarinfo));
  694. arinfos[t]^:=r;
  695. end;
  696. procedure RegisterExternalLinker(var system_info: tsysteminfo; c:TAbstractLinkerClass);
  697. begin
  698. system_info.linkextern := c;
  699. end;
  700. procedure RegisterInternalLinker(var system_info : tsysteminfo; c:TAbstractLinkerClass);
  701. begin
  702. system_info.link := c;
  703. end;
  704. procedure DeregisterInfos;
  705. var
  706. assem : tasm;
  707. target : tsystem;
  708. ar : tar;
  709. res : tres;
  710. dbg : tdbg;
  711. begin
  712. for target:=low(tsystem) to high(tsystem) do
  713. if assigned(targetinfos[target]) then
  714. begin
  715. freemem(targetinfos[target],sizeof(tsysteminfo));
  716. targetinfos[target]:=nil;
  717. end;
  718. for assem:=low(tasm) to high(tasm) do
  719. if assigned(asminfos[assem]) then
  720. begin
  721. freemem(asminfos[assem],sizeof(tasminfo));
  722. asminfos[assem]:=nil;
  723. end;
  724. for ar:=low(tar) to high(tar) do
  725. if assigned(arinfos[ar]) then
  726. begin
  727. freemem(arinfos[ar],sizeof(tarinfo));
  728. arinfos[ar]:=nil;
  729. end;
  730. for res:=low(tres) to high(tres) do
  731. if assigned(resinfos[res]) then
  732. begin
  733. freemem(resinfos[res],sizeof(tresinfo));
  734. resinfos[res]:=nil;
  735. end;
  736. for dbg:=low(tdbg) to high(tdbg) do
  737. if assigned(dbginfos[dbg]) then
  738. begin
  739. freemem(dbginfos[dbg],sizeof(tdbginfo));
  740. dbginfos[dbg]:=nil;
  741. end;
  742. end;
  743. {****************************************************************************
  744. Initialization of default target
  745. ****************************************************************************}
  746. procedure default_target(t:tsystem);
  747. begin
  748. set_target(t);
  749. if source_info.name='' then
  750. source_info:=target_info;
  751. end;
  752. procedure set_source_info(const ti : tsysteminfo);
  753. begin
  754. { can't use message() here (PFV) }
  755. if source_info.name<>'' then
  756. Writeln('Warning: Source OS Redefined!');
  757. source_info:=ti;
  758. end;
  759. procedure InitSystems;
  760. begin
  761. { Now default target, this is dependent on the target cpu define,
  762. when the define is the same as the source cpu then we use the source
  763. os, else we pick a default }
  764. {$ifdef i386}
  765. {$ifdef cpu86}
  766. default_target(source_info.system);
  767. {$define default_target_set}
  768. {$else cpu86}
  769. {$ifdef linux}
  770. default_target(system_i386_linux);
  771. {$define default_target_set}
  772. {$endif}
  773. {$ifdef freebsd}
  774. default_target(system_i386_freebsd);
  775. {$define default_target_set}
  776. {$endif}
  777. {$ifdef darwin}
  778. default_target(system_i386_darwin);
  779. {$define default_target_set}
  780. {$endif}
  781. {$endif cpu86}
  782. { default is linux }
  783. {$ifndef default_target_set}
  784. default_target(system_i386_linux);
  785. {$endif default_target_set}
  786. {$endif i386}
  787. {$ifdef x86_64}
  788. {$ifdef cpux86_64}
  789. default_target(source_info.system);
  790. {$define default_target_set}
  791. {$else cpux86_64}
  792. {$ifdef MSWindows}
  793. default_target(system_x86_64_win64);
  794. {$define default_target_set}
  795. {$endif}
  796. {$ifdef linux}
  797. default_target(system_x86_64_linux);
  798. {$define default_target_set}
  799. {$endif}
  800. {$ifdef freebsd}
  801. default_target(system_x86_64_freebsd);
  802. {$define default_target_set}
  803. {$endif}
  804. {$ifdef solaris}
  805. default_target(system_x86_64_solaris);
  806. {$define default_target_set}
  807. {$endif}
  808. {$ifdef darwin}
  809. default_target(system_x86_64_darwin);
  810. {$define default_target_set}
  811. {$endif}
  812. {$endif cpux86_64}
  813. { default is linux }
  814. {$ifndef default_target_set}
  815. default_target(system_x86_64_linux);
  816. {$endif default_target_set}
  817. {$endif x86_64}
  818. {$ifdef m68k}
  819. {$ifdef cpu68}
  820. default_target(source_info.target);
  821. {$else cpu68}
  822. default_target(system_m68k_linux);
  823. {$endif cpu68}
  824. {$endif m68k}
  825. {$ifdef alpha}
  826. {$ifdef cpualpha}
  827. default_target(source_info.system);
  828. {$else cpualpha}
  829. default_target(system_alpha_linux);
  830. {$endif cpualpha}
  831. {$endif alpha}
  832. {$ifdef powerpc}
  833. {$ifdef cpupowerpc}
  834. default_target(source_info.system);
  835. {$define default_target_set}
  836. {$else cpupowerpc}
  837. {$ifdef linux}
  838. default_target(system_powerpc_linux);
  839. {$define default_target_set}
  840. {$endif}
  841. {$ifdef darwin}
  842. default_target(system_powerpc_darwin);
  843. {$define default_target_set}
  844. {$endif}
  845. {$endif cpupowerpc}
  846. {$ifndef default_target_set}
  847. default_target(system_powerpc_linux);
  848. {$endif default_target_set}
  849. {$endif powerpc}
  850. {$ifdef POWERPC64}
  851. {$ifdef cpupowerpc64}
  852. default_target(source_info.system);
  853. {$define default_target_set}
  854. {$else cpupowerpc64}
  855. {$ifdef darwin}
  856. default_target(system_powerpc64_darwin);
  857. {$define default_target_set}
  858. {$endif}
  859. {$ifdef linux}
  860. default_target(system_powerpc64_linux);
  861. {$define default_target_set}
  862. {$endif}
  863. {$endif cpupowerpc64}
  864. {$ifndef default_target_set}
  865. default_target(system_powerpc64_linux);
  866. {$define default_target_set}
  867. {$endif}
  868. {$endif POWERPC64}
  869. {$ifdef sparc}
  870. {$ifdef cpusparc}
  871. default_target(source_info.system);
  872. {$else cpusparc}
  873. default_target(system_sparc_linux);
  874. {$endif cpusparc}
  875. {$endif sparc}
  876. {$ifdef arm}
  877. {$ifdef cpuarm}
  878. default_target(source_info.system);
  879. {$else cpuarm}
  880. {$ifdef WINDOWS}
  881. {$define default_target_set}
  882. default_target(system_arm_wince);
  883. {$endif}
  884. {$ifdef linux}
  885. {$define default_target_set}
  886. default_target(system_arm_linux);
  887. {$endif}
  888. {$ifdef darwin}
  889. {$define default_target_set}
  890. default_target(system_arm_darwin);
  891. {$endif}
  892. {$ifndef default_target_set}
  893. default_target(system_arm_linux);
  894. {$define default_target_set}
  895. {$endif}
  896. {$endif cpuarm}
  897. {$endif arm}
  898. {$ifdef avr}
  899. default_target(system_avr_embedded);
  900. {$endif avr}
  901. {$ifdef mips}
  902. {$ifdef mipsel}
  903. default_target(system_mipsel_linux);
  904. {$else mipsel}
  905. default_target(system_mips_linux);
  906. {$endif mipsel}
  907. {$endif mips}
  908. end;
  909. initialization
  910. source_info.name:='';
  911. finalization
  912. DeregisterInfos;
  913. end.