2
0

globals.pas 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. This unit implements some support functions and global variables
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit globals;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. {$ifdef windows}
  22. windows,
  23. {$endif}
  24. {$ifdef os2}
  25. dos,
  26. {$endif os2}
  27. {$ifdef hasunix}
  28. Baseunix,unix,
  29. {$endif}
  30. {$IFNDEF USE_FAKE_SYSUTILS}
  31. sysutils,
  32. {$ELSE}
  33. fksysutl,
  34. {$ENDIF}
  35. { comphook pulls in sysutils anyways }
  36. cutils,cclasses,cfileutl,
  37. cpuinfo,
  38. {$if defined(LLVM) or defined(GENERIC_CPU)}
  39. llvminfo,
  40. {$endif LLVM or GENERIC_CPU}
  41. globtype,version,versioncmp,systems;
  42. const
  43. delphimodeswitches =
  44. [m_delphi,m_class,m_objpas,m_result,m_string_pchar,
  45. m_pointer_2_procedure,m_autoderef,m_tp_procvar,m_initfinal,m_default_ansistring,
  46. m_out,m_default_para,m_duplicate_names,m_hintdirective,
  47. m_property,m_default_inline,m_except,m_advanced_records,
  48. m_array_operators,m_prefixed_attributes,m_underscoreisseparator,
  49. m_function_references,m_anonymous_functions];
  50. delphiunicodemodeswitches = delphimodeswitches + [m_systemcodepage,m_default_unicodestring];
  51. fpcmodeswitches =
  52. [m_fpc,m_string_pchar,m_nested_comment,m_repeat_forward,
  53. m_cvar_support,m_initfinal,m_hintdirective,
  54. m_property,m_default_inline];
  55. objfpcmodeswitches =
  56. [m_objfpc,m_fpc,m_class,m_objpas,m_result,m_string_pchar,m_nested_comment,
  57. m_repeat_forward,m_cvar_support,m_initfinal,m_out,m_default_para,m_hintdirective,
  58. m_property,m_default_inline,m_except];
  59. tpmodeswitches =
  60. [m_tp7,m_tp_procvar,m_duplicate_names];
  61. {$ifdef gpc_mode}
  62. gpcmodeswitches =
  63. [m_gpc,m_tp_procvar];
  64. {$endif}
  65. macmodeswitches =
  66. [m_mac,m_cvar_support,m_mac_procvar,m_nested_procvars,m_non_local_goto,m_isolike_unary_minus,m_default_inline];
  67. isomodeswitches =
  68. [m_iso,m_tp_procvar,m_duplicate_names,m_nested_procvars,m_non_local_goto,m_isolike_unary_minus,m_isolike_io,
  69. m_isolike_program_para,
  70. m_isolike_mod];
  71. extpasmodeswitches =
  72. [m_extpas,m_tp_procvar,m_duplicate_names,m_nested_procvars,m_non_local_goto,m_isolike_unary_minus,m_isolike_io,
  73. m_isolike_program_para,
  74. m_isolike_mod];
  75. { maximum nesting of routines }
  76. maxnesting = 32;
  77. { Filenames and extensions }
  78. sourceext = '.pp';
  79. pasext = '.pas';
  80. pext = '.p';
  81. treelogfilename = 'tree.log';
  82. {$if defined(CPUARM) and defined(FPUFPA)}
  83. MathQNaN : tcompdoublerec = (bytes : (0,0,252,255,0,0,0,0));
  84. MathInf : tcompdoublerec = (bytes : (0,0,240,127,0,0,0,0));
  85. MathNegInf : tcompdoublerec = (bytes : (0,0,240,255,0,0,0,0));
  86. MathPi : tcompdoublerec = (bytes : (251,33,9,64,24,45,68,84));
  87. {$else}
  88. {$ifdef FPC_LITTLE_ENDIAN}
  89. MathQNaN : tcompdoublerec = (bytes : (0,0,0,0,0,0,252,255));
  90. MathInf : tcompdoublerec = (bytes : (0,0,0,0,0,0,240,127));
  91. MathNegInf : tcompdoublerec = (bytes : (0,0,0,0,0,0,240,255));
  92. MathPi : tcompdoublerec = (bytes : (24,45,68,84,251,33,9,64));
  93. MathPiExtended : tcompextendedrec = (bytes : (53,194,104,33,162,218,15,201,0,64));
  94. {$else FPC_LITTLE_ENDIAN}
  95. MathQNaN : tcompdoublerec = (bytes : (255,252,0,0,0,0,0,0));
  96. MathInf : tcompdoublerec = (bytes : (127,240,0,0,0,0,0,0));
  97. MathNegInf : tcompdoublerec = (bytes : (255,240,0,0,0,0,0,0));
  98. MathPi : tcompdoublerec = (bytes : (64,9,33,251,84,68,45,24));
  99. MathPiExtended : tcompextendedrec = (bytes : (64,0,201,15,218,162,33,104,194,53));
  100. {$endif FPC_LITTLE_ENDIAN}
  101. {$endif}
  102. CP_UTF8 = 65001;
  103. CP_UTF16LE = 1200;
  104. CP_UTF16BE = 1201;
  105. CP_NONE = 65535;
  106. { by default no local variable trashing }
  107. localvartrashing: longint = -1;
  108. nroftrashvalues = 4;
  109. trashintvalues: array[0..nroftrashvalues-1] of int64 = ($5555555555555555,$AAAAAAAAAAAAAAAA,$EFEFEFEFEFEFEFEF,0);
  110. { Verbosity constants }
  111. Const
  112. { Levels }
  113. V_None = $0;
  114. V_Fatal = $1;
  115. V_Error = $2;
  116. V_Normal = $4; { doesn't show a text like Error: }
  117. V_Warning = $8;
  118. V_Note = $10;
  119. V_Hint = $20;
  120. V_LineInfoMask = $fff;
  121. { From here by default no line info }
  122. V_Info = $1000;
  123. V_Status = $2000;
  124. V_Used = $4000;
  125. V_Tried = $8000;
  126. V_Conditional = $10000;
  127. V_Debug = $20000;
  128. V_Executable = $40000;
  129. V_TimeStamps = $80000;
  130. V_LevelMask = $ffffff;
  131. V_All = V_LevelMask;
  132. V_Default = V_Fatal + V_Error + V_Normal;
  133. { Flags }
  134. V_LineInfo = $10000000;
  135. V_Parallel = $20000000;
  136. type
  137. { this is written to ppus during token recording for generics,
  138. it used to required to be packed,
  139. but this requirement is now obsolete,
  140. as the fields are written one by one. PM 2012-06-13 }
  141. tsettings = record
  142. alignment : talignmentinfo;
  143. globalswitches : tglobalswitches;
  144. targetswitches : ttargetswitches;
  145. moduleswitches : tmoduleswitches;
  146. localswitches : tlocalswitches;
  147. modeswitches : tmodeswitches;
  148. optimizerswitches : toptimizerswitches;
  149. { generate information necessary to perform these wpo's during a subsequent compilation }
  150. genwpoptimizerswitches: twpoptimizerswitches;
  151. { perform these wpo's using information generated during a previous compilation }
  152. dowpoptimizerswitches: twpoptimizerswitches;
  153. debugswitches : tdebugswitches;
  154. { 0: old behaviour for sets <=256 elements
  155. >0: round to this size }
  156. setalloc,
  157. packenum : shortint;
  158. packrecords : shortint;
  159. maxfpuregisters : shortint;
  160. cputype,
  161. optimizecputype,
  162. asmcputype : tcputype;
  163. fputype : tfputype;
  164. asmmode : tasmmode;
  165. interfacetype : tinterfacetypes;
  166. defproccall : tproccalloption;
  167. sourcecodepage : tstringencoding;
  168. minfpconstprec : tfloattype;
  169. disabledircache : boolean;
  170. tlsmodel : ttlsmodel;
  171. { CPU targets with microcontroller support can add a controller specific unit }
  172. controllertype : tcontrollertype;
  173. { WARNING: this pointer cannot be written as such in record token }
  174. pmessage : pmessagestaterecord;
  175. {$if defined(generic_cpu)}
  176. case byte of
  177. {$endif}
  178. {$if defined(i8086) or defined(generic_cpu)}
  179. {$ifdef generic_cpu} 1:({$endif}
  180. x86memorymodel : tx86memorymodel;
  181. {$ifdef generic_cpu} );{$endif}
  182. {$endif defined(i8086) or defined(generic_cpu)}
  183. {$if defined(ARM) or defined(generic_cpu)}
  184. {$ifdef generic_cpu} 2:({$endif}
  185. instructionset : tinstructionset;
  186. {$ifdef generic_cpu} );{$endif}
  187. {$endif defined(ARM) or defined(generic_cpu)}
  188. {$if defined(LLVM) or defined(GENERIC_CPU)}
  189. {$ifdef generic_cpu} 3:({$endif}
  190. llvmversion: tllvmversion;
  191. {$ifdef generic_cpu} );{$endif}
  192. {$endif defined(LLVM) or defined(GENERIC_CPU)}
  193. end;
  194. const
  195. LinkMapWeightDefault = 1000;
  196. type
  197. TLinkRec = record
  198. Key : AnsiString;
  199. Value : AnsiString; // key expands to valuelist "value"
  200. Weight: longint;
  201. end;
  202. TLinkStrMap = class
  203. private
  204. itemcnt : longint;
  205. fmap : Array Of TLinkRec;
  206. function Lookup(const key:Ansistring):longint;
  207. function getlinkrec(i:longint):TLinkRec;
  208. public
  209. procedure Add(const key:ansistring;const value:AnsiString='';weight:longint=LinkMapWeightDefault);
  210. procedure addseries(const keys:AnsiString;weight:longint=LinkMapWeightDefault);
  211. function AddDep(const keyvalue:String):boolean;
  212. function AddWeight(const keyvalue:String):boolean;
  213. procedure SetValue(const key:AnsiString;Weight:Integer);
  214. procedure SortonWeight;
  215. function Find(const key:AnsiString):AnsiString;
  216. procedure Expand(src:TCmdStrList;dest: TLinkStrMap);
  217. procedure UpdateWeights(Weightmap:TLinkStrMap);
  218. constructor Create;
  219. property count : longint read itemcnt;
  220. property items[I:longint]:TLinkRec read getlinkrec; default;
  221. end;
  222. tpendingstateflag = (
  223. psf_alignment_changed,
  224. psf_verbosity_full_switched,
  225. psf_local_switches_changed,
  226. psf_packenum_changed,
  227. psf_packrecords_changed,
  228. psf_setalloc_changed
  229. );
  230. tpendingstateflags = set of tpendingstateflag;
  231. tpendingstate = record
  232. nextverbositystr : shortstring;
  233. nextlocalswitches : tlocalswitches;
  234. nextverbosityfullswitch: longint;
  235. nextcallingstr : shortstring;
  236. nextmessagerecord : pmessagestaterecord;
  237. nextalignment : talignmentinfo;
  238. nextpackenum : shortint;
  239. nextpackrecords : shortint;
  240. nextsetalloc : shortint;
  241. flags : tpendingstateflags;
  242. end;
  243. var
  244. { specified inputfile }
  245. inputfilepath : string;
  246. inputfilename : string;
  247. { specified outputfile with -o parameter }
  248. outputfilename : string;
  249. outputprefix : pshortstring;
  250. outputsuffix : pshortstring;
  251. { selected subtarget }
  252. subtarget : string;
  253. { specified with -FE or -FU }
  254. outputexedir : TPathStr;
  255. outputunitdir : TPathStr;
  256. { specified with -FW and -Fw }
  257. wpofeedbackinput,
  258. wpofeedbackoutput : TPathStr;
  259. {$if defined(XTENSA) or defined(RISCV32)}
  260. { specified with -Ff }
  261. idfpath : TPathStr;
  262. { specified with }
  263. idf_version : longint;
  264. {$endif defined(XTENSA) or defined(RISCV32)}
  265. { external assembler extra option }
  266. asmextraopt : string;
  267. { things specified with parameters }
  268. paralinkoptions : TCmdStr;
  269. paradynamiclinker : string;
  270. paraprintnodetree : byte;
  271. {$ifdef PREPROCWRITE}
  272. parapreprocess : boolean;
  273. {$endif PREPROCWRITE}
  274. printnodefile : text;
  275. { typical cross compiling params}
  276. { directory where the utils can be found (options -FD) }
  277. utilsdirectory : TPathStr;
  278. { targetname specific prefix used by these utils (options -XP<path>) }
  279. utilsprefix : TCmdStr;
  280. { Suffix for LLVM utilities, e.g. '-7' for clang-7 }
  281. llvmutilssuffix : TCmdStr;
  282. cshared : boolean; { pass --shared to ld to link C libs shared}
  283. Dontlinkstdlibpath: Boolean; { Don't add std paths to linkpath}
  284. rlinkpath : TCmdStr; { rpath-link linkdir override}
  285. sysrootpath : TCmdStr; { target system root to search dyn linker }
  286. { some flags for global compiler switches }
  287. do_build,
  288. do_release,
  289. do_make : boolean;
  290. timestr,
  291. datestr : string;
  292. { Path to ppc }
  293. exepath : TPathStr;
  294. { Path to unicode charmap/collation binaries }
  295. unicodepath : TPathStr;
  296. { path for searching units, different paths can be separated by ; }
  297. librarysearchpath,
  298. unitsearchpath,
  299. objectsearchpath,
  300. includesearchpath,
  301. frameworksearchpath : TSearchPathList;
  302. packagesearchpath : TSearchPathList;
  303. { list of default namespaces }
  304. namespacelist : TCmdStrList;
  305. // During scanning/parsing, a module may not yet be available.
  306. // Scanner checks first current_namespacelist, then local_namespacelist
  307. premodule_namespacelist, // always set: used as long as current_namespacelist is not correctly set.
  308. current_namespacelist : TCmdStrList; // Set when parsing module to the current module's namespace.
  309. { contains tpackageentry entries }
  310. packagelist : TFPHashList;
  311. autoloadunits : string;
  312. { linking }
  313. usewindowapi : boolean;
  314. description : string;
  315. SetPEFlagsSetExplicity,
  316. SetPEOptFlagsSetExplicity,
  317. SetPEOSVersionSetExplicitely,
  318. SetPESubSysVersionSetExplicitely,
  319. SetPEUserVersionSetExplicitely,
  320. ImageBaseSetExplicity,
  321. MinStackSizeSetExplicity,
  322. MaxStackSizeSetExplicity,
  323. DescriptionSetExplicity : boolean;
  324. dllversion : string;
  325. dllmajor,
  326. dllminor,
  327. dllrevision : word; { revision only for netware }
  328. { win pe }
  329. peosversionminor,
  330. peosversionmajor,
  331. pesubsysversionminor,
  332. pesubsysversionmajor,
  333. peuserversionminor,
  334. peuserversionmajor : word;
  335. peoptflags,
  336. peflags : longint;
  337. minstacksize,
  338. maxstacksize,
  339. imagebase : puint;
  340. UseDeffileForExports : boolean;
  341. UseDeffileForExportsSetExplicitly : boolean;
  342. GenerateImportSection,
  343. GenerateImportSectionSetExplicitly,
  344. RelocSection : boolean;
  345. MacOSXVersionMin,
  346. iPhoneOSVersionMin: tversion;
  347. RelocSectionSetExplicitly : boolean;
  348. current_tokenpos, { position of the last token }
  349. current_filepos : tfileposinfo; { current position }
  350. nwscreenname : string;
  351. nwthreadname : string;
  352. nwcopyright : string;
  353. codegenerror : boolean; { true if there is an error reported }
  354. exception_raised : boolean; { true if there is an exception reported }
  355. block_type : tblock_type; { type of currently parsed block }
  356. exceptblockcounter : integer; { each except block gets a unique number check gotos }
  357. current_exceptblock : integer; { the exceptblock number of the current block (0 if none) }
  358. LinkLibraryAliases : TLinkStrMap;
  359. LinkLibraryOrder : TLinkStrMap;
  360. init_settings,
  361. current_settings : tsettings;
  362. pendingstate : tpendingstate;
  363. { Memory sizes }
  364. heapsize,
  365. maxheapsize,
  366. stacksize : longint;
  367. {$Ifdef EXTDEBUG}
  368. { parameter switches }
  369. debugstop : boolean;
  370. {$EndIf EXTDEBUG}
  371. { Application type (platform specific)
  372. see globtype.pas for description }
  373. apptype : tapptype;
  374. features : tfeatures;
  375. { prefix added to automatically generated setters/getters. If empty,
  376. no getters/setters will be automatically generated except if required
  377. for visibility reasons (but in that case the names will be mangled so
  378. they are unique) }
  379. prop_auto_getter_prefix,
  380. prop_auto_setter_prefix : string;
  381. cgbackend: tcgbackend;
  382. const
  383. Inside_asm_statement : boolean = false;
  384. global_unit_count : word = 0;
  385. { for error info in pp.pas }
  386. parser_current_file : string = '';
  387. {$if defined(m68k) or defined(arm)}
  388. { PalmOS resources }
  389. palmos_applicationname : string = 'FPC Application';
  390. palmos_applicationid : string[4] = 'FPCA';
  391. {$endif defined(m68k) or defined(arm)}
  392. {$if defined(m68k)}
  393. { Atari Specific }
  394. ataritos_exe_flags: dword = 7;
  395. ataritos_exe_format: string = 'ataritos';
  396. { Sinclair QL specific }
  397. sinclairql_metadata_format: string[4] = 'QHDR';
  398. sinclairql_vlink_experimental: boolean = true; { temporary }
  399. {$endif defined(m68k)}
  400. { default name of the C-style "main" procedure of the library/program }
  401. { (this will be prefixed with the target_info.cprefix) }
  402. defaultmainaliasname = 'main';
  403. mainaliasname : string = defaultmainaliasname;
  404. custom_attribute_suffix = 'ATTRIBUTE';
  405. LTOExt: TCmdStr = '';
  406. const
  407. default_settings : TSettings = (
  408. alignment : (
  409. procalign : 0;
  410. loopalign : 0;
  411. jumpalign : 0;
  412. jumpalignskipmax : 0;
  413. coalescealign : 0;
  414. coalescealignskipmax: 0;
  415. constalignmin : 0;
  416. constalignmax : 0;
  417. varalignmin : 0;
  418. varalignmax : 0;
  419. localalignmin : 0;
  420. localalignmax : 0;
  421. recordalignmin : 0;
  422. recordalignmax : 0;
  423. maxCrecordalign : 0;
  424. );
  425. globalswitches : [cs_check_unit_name,cs_link_static];
  426. targetswitches : [];
  427. moduleswitches : [cs_extsyntax,cs_implicit_exceptions];
  428. localswitches : [cs_check_io,cs_typed_const_writable,cs_pointermath,cs_imported_data{$ifdef i8086},cs_force_far_calls{$endif}];
  429. modeswitches : fpcmodeswitches;
  430. optimizerswitches : [];
  431. genwpoptimizerswitches : [];
  432. dowpoptimizerswitches : [];
  433. {$ifdef i8086}
  434. debugswitches : [ds_dwarf_sets,ds_dwarf_omf_linnum];
  435. {$else i8086}
  436. debugswitches : [ds_dwarf_sets];
  437. {$endif i8086}
  438. setalloc : 0;
  439. packenum : 4;
  440. {$ifdef i8086}
  441. packrecords : 1;
  442. {$else i8086}
  443. packrecords : 0;
  444. {$endif i8086}
  445. maxfpuregisters : 0;
  446. { Note: GENERIC_CPU is used together with generic subdirectory to
  447. be able to compile some of the units without any real CPU.
  448. This is used to generate a CPU independant PPUDUMP utility. PM }
  449. {$ifdef GENERIC_CPU}
  450. cputype : cpu_none;
  451. optimizecputype : cpu_none;
  452. asmcputype : cpu_none;
  453. fputype : fpu_none;
  454. {$else not GENERIC_CPU}
  455. {$ifdef i386}
  456. cputype : cpu_Pentium2;
  457. optimizecputype : cpu_Pentium3;
  458. asmcputype : cpu_none;
  459. fputype : fpu_x87;
  460. {$endif i386}
  461. {$ifdef m68k}
  462. cputype : cpu_MC68020;
  463. optimizecputype : cpu_MC68020;
  464. asmcputype : cpu_none;
  465. fputype : fpu_soft;
  466. {$endif m68k}
  467. {$ifdef powerpc}
  468. cputype : cpu_PPC604;
  469. optimizecputype : cpu_ppc7400;
  470. asmcputype : cpu_none;
  471. fputype : fpu_standard;
  472. {$endif powerpc}
  473. {$ifdef POWERPC64}
  474. cputype : cpu_PPC970;
  475. optimizecputype : cpu_ppc970;
  476. asmcputype : cpu_none;
  477. fputype : fpu_standard;
  478. {$endif POWERPC64}
  479. {$ifdef sparc}
  480. cputype : cpu_SPARC_V9;
  481. optimizecputype : cpu_SPARC_V9;
  482. asmcputype : cpu_none;
  483. fputype : fpu_hard;
  484. {$endif sparc}
  485. {$ifdef sparc64}
  486. cputype : cpu_SPARC_V9;
  487. optimizecputype : cpu_SPARC_V9;
  488. asmcputype : cpu_none;
  489. fputype : fpu_hard;
  490. {$endif sparc64}
  491. {$ifdef arm}
  492. cputype : cpu_armv4;
  493. optimizecputype : cpu_armv4;
  494. asmcputype : cpu_none;
  495. fputype : fpu_fpa;
  496. {$endif arm}
  497. {$ifdef x86_64}
  498. cputype : cpu_athlon64;
  499. optimizecputype : cpu_athlon64;
  500. asmcputype : cpu_none;
  501. fputype : fpu_sse64;
  502. {$endif x86_64}
  503. {$ifdef avr}
  504. cputype : cpuinfo.cpu_avr5;
  505. optimizecputype : cpuinfo.cpu_avr5;
  506. asmcputype : cpu_none;
  507. fputype : fpu_none;
  508. {$endif avr}
  509. {$ifdef mips}
  510. {$ifdef mips64}
  511. cputype : cpu_mips3;
  512. optimizecputype : cpu_mips3;
  513. asmcputype : cpu_none;
  514. fputype : fpu_mips3;
  515. {$else mips64}
  516. cputype : cpu_mips2;
  517. optimizecputype : cpu_mips2;
  518. asmcputype : cpu_none;
  519. fputype : fpu_mips2;
  520. {$endif mips64}
  521. {$endif mips}
  522. {$ifdef jvm}
  523. cputype : cpu_none;
  524. optimizecputype : cpu_none;
  525. asmcputype : cpu_none;
  526. fputype : fpu_standard;
  527. {$endif jvm}
  528. {$ifdef aarch64}
  529. cputype : cpu_armv8;
  530. optimizecputype : cpu_armv8;
  531. asmcputype : cpu_none;
  532. fputype : fpu_vfp;
  533. {$endif aarch64}
  534. {$ifdef i8086}
  535. cputype : cpu_8086;
  536. optimizecputype : cpu_8086;
  537. { Use cpu_none by default,
  538. because using cpu_8086 by default means
  539. that we reject any instruction above bare 8086 instruction set
  540. for all assembler code PM }
  541. asmcputype : cpu_none;
  542. fputype : fpu_x87;
  543. {$endif i8086}
  544. {$ifdef riscv32}
  545. cputype : cpu_rv32ima;
  546. optimizecputype : cpu_rv32ima;
  547. asmcputype : cpu_none;
  548. fputype : fpu_fd;
  549. {$endif riscv32}
  550. {$ifdef riscv64}
  551. cputype : cpu_rv64imac;
  552. optimizecputype : cpu_rv64imac;
  553. asmcputype : cpu_none;
  554. fputype : fpu_fd;
  555. {$endif riscv64}
  556. {$ifdef xtensa}
  557. cputype : cpu_none;
  558. optimizecputype : cpu_none;
  559. asmcputype : cpu_none;
  560. fputype : fpu_none;
  561. {$endif xtensa}
  562. {$ifdef z80}
  563. cputype : cpu_zilog_z80;
  564. optimizecputype : cpu_zilog_z80;
  565. { Use cpu_none by default,
  566. because using cpu_8086 by default means
  567. that we reject any instruction above bare 8086 instruction set
  568. for all assembler code PM }
  569. asmcputype : cpu_none;
  570. fputype : fpu_soft;
  571. {$endif z80}
  572. {$ifdef wasm}
  573. cputype : cpu_none;
  574. optimizecputype : cpu_none;
  575. asmcputype : cpu_none;
  576. fputype : fpu_standard;
  577. {$endif wasm}
  578. {$ifdef loongarch64}
  579. cputype : cpu_3a;
  580. optimizecputype : cpu_3a;
  581. asmcputype : cpu_none;
  582. fputype : fpu_fd;
  583. {$endif loongarch64}
  584. {$endif not GENERIC_CPU}
  585. asmmode : asmmode_standard;
  586. {$ifndef jvm}
  587. interfacetype : it_interfacecom;
  588. {$else jvm}
  589. interfacetype : it_interfacejava;
  590. {$endif jvm}
  591. defproccall : pocall_default;
  592. sourcecodepage : 28591;
  593. minfpconstprec : s32real;
  594. disabledircache : false;
  595. tlsmodel : tlsm_none;
  596. controllertype : ct_none;
  597. pmessage : nil;
  598. {$if defined(i8086) or defined(GENERIC_CPU)}
  599. x86memorymodel : mm_small;
  600. {$endif defined(i8086) or defined(GENERIC_CPU)}
  601. {$if defined(ARM)}
  602. instructionset : is_arm;
  603. {$endif defined(ARM)}
  604. {$if defined(LLVM) and not defined(GENERIC_CPU)}
  605. llvmversion : llvmver_7_0;
  606. {$endif defined(LLVM) and not defined(GENERIC_CPU)}
  607. );
  608. var
  609. starttime : real;
  610. startsystime : TSystemTime;
  611. function getdatestr:string;
  612. function gettimestr:string;
  613. function filetimestring( t : longint) : string;
  614. function getrealtime(const st: TSystemTime) : real;
  615. function getrealtime : real;
  616. procedure DefaultReplacements(var s:ansistring; substitute_env_variables:boolean=true);
  617. function GetEnvPChar(const envname:ansistring):pchar;
  618. procedure FreeEnvPChar(p:pchar);
  619. function is_number_float(d : double) : boolean;
  620. { discern +0.0 and -0.0 }
  621. function get_real_sign(r: bestreal): longint;
  622. procedure InitGlobals;
  623. procedure DoneGlobals;
  624. procedure register_initdone_proc(init,done:tprocedure);
  625. function string2guid(const s: string; var GUID: TGUID): boolean;
  626. function guid2string(const GUID: TGUID): string;
  627. function SetAktProcCall(const s:string; var a:tproccalloption):boolean;
  628. function Setabitype(const s:string;var a:tabi):boolean;
  629. function Setoptimizecputype(const s:string;var a:tcputype):boolean;
  630. function Setcputype(const s:string;var a:tsettings):boolean;
  631. function SetFpuType(const s:string;var a:tfputype):boolean;
  632. function SetControllerType(const s:string;var a:tcontrollertype):boolean;
  633. function HandleFeature(const s : string) : boolean;
  634. function SetMinFPConstPrec(const s: string; var a: tfloattype) : boolean;
  635. {# Routine to get the required alignment for size of data, which will
  636. be placed in bss segment, according to the current alignment requirements }
  637. function size_2_align(len : asizeuint) : longint;
  638. function var_align(want_align: longint): shortint;
  639. function var_align_size(siz: asizeuint): shortint;
  640. {# Routine to get the required alignment for size of data, which will
  641. be placed in data/const segment, according to the current alignment requirements }
  642. function const_align(want_align: longint): shortint;
  643. function const_align_size(siz: asizeuint): shortint;
  644. {$ifdef ARM}
  645. function is_double_hilo_swapped: boolean;{$ifdef USEINLINE}inline;{$endif}
  646. {$endif ARM}
  647. function floating_point_range_check_error : boolean;
  648. function use_dotted_functions: boolean;
  649. { hide Sysutils.ExecuteProcess in units using this one after SysUtils}
  650. const
  651. ExecuteProcess = 'Do not use' deprecated 'Use cfileutil.RequotedExecuteProcess instead, ExecuteProcess cannot deal with single quotes as used by Unix command lines';
  652. var
  653. AllowedFilenameTransFormations : tfilenametransformations = AllTransformations;
  654. implementation
  655. {$if defined(macos)}
  656. uses
  657. macutils;
  658. {$elseif defined(mswindows)}
  659. uses
  660. windirs;
  661. {$endif}
  662. {****************************************************************************
  663. TLinkStrMap
  664. ****************************************************************************}
  665. Constructor TLinkStrMap.create;
  666. begin
  667. inherited;
  668. itemcnt:=0;
  669. end;
  670. procedure TLinkStrMap.Add(const key:ansistring;const value:AnsiString='';weight:longint=LinkMapWeightDefault);
  671. begin
  672. if lookup(key)<>-1 Then
  673. exit;
  674. if itemcnt<=length(fmap) Then
  675. setlength(fmap,itemcnt+10);
  676. fmap[itemcnt].key:=key;
  677. fmap[itemcnt].value:=value;
  678. fmap[itemcnt].weight:=weight;
  679. inc(itemcnt);
  680. end;
  681. function TLinkStrMap.AddDep(const keyvalue:String):boolean;
  682. var
  683. i : Longint;
  684. begin
  685. AddDep:=false;
  686. i:=pos('=',keyvalue);
  687. if i=0 then
  688. exit;
  689. Add(Copy(KeyValue,1,i-1),Copy(KeyValue,i+1,length(KeyValue)-i));
  690. AddDep:=True;
  691. end;
  692. function TLinkStrMap.AddWeight(const keyvalue:String):boolean;
  693. var
  694. i,j : Longint;
  695. Code : Word;
  696. s : AnsiString;
  697. begin
  698. AddWeight:=false;
  699. i:=pos('=',keyvalue);
  700. if i=0 then
  701. exit;
  702. s:=Copy(KeyValue,i+1,length(KeyValue)-i);
  703. val(s,j,code);
  704. if code=0 Then
  705. begin
  706. Add(Copy(KeyValue,1,i-1),'',j);
  707. AddWeight:=True;
  708. end;
  709. end;
  710. procedure TLinkStrMap.addseries(const keys:AnsiString;weight:longint);
  711. var
  712. i,j,k : longint;
  713. begin
  714. k:=length(keys);
  715. i:=1;
  716. while i<=k do
  717. begin
  718. j:=i;
  719. while (i<=k) and (keys[i]<>',') do
  720. inc(i);
  721. add(copy(keys,j,i-j),'',weight);
  722. inc(i);
  723. end;
  724. end;
  725. procedure TLinkStrMap.SetValue(const Key:Ansistring;weight:Integer);
  726. var
  727. j : longint;
  728. begin
  729. j:=lookup(key);
  730. if j<>-1 then
  731. fmap[j].weight:=weight;
  732. end;
  733. function TLinkStrMap.find(const key:Ansistring):Ansistring;
  734. var
  735. j : longint;
  736. begin
  737. find:='';
  738. j:=lookup(key);
  739. if j<>-1 then
  740. find:=fmap[j].value;
  741. end;
  742. function TLinkStrMap.lookup(const key:Ansistring):longint;
  743. var
  744. i : longint;
  745. begin
  746. lookup:=-1;
  747. i:=0;
  748. while (i<itemcnt) and (fmap[i].key<>key) do
  749. inc(i);
  750. if i<>itemcnt then
  751. lookup:=i;
  752. end;
  753. procedure TLinkStrMap.SortOnWeight;
  754. var
  755. i, j : longint;
  756. m : TLinkRec;
  757. begin
  758. if itemcnt <2 then exit;
  759. for i:=0 to itemcnt-1 do
  760. for j:=i+1 to itemcnt-1 do
  761. begin
  762. if fmap[i].weight>fmap[j].weight Then
  763. begin
  764. m:=fmap[i];
  765. fmap[i]:=fmap[j];
  766. fmap[j]:=m;
  767. end;
  768. end;
  769. end;
  770. function TLinkStrMap.getlinkrec(i:longint):TLinkRec;
  771. begin
  772. result:=fmap[i];
  773. end;
  774. procedure TLinkStrMap.Expand(Src:TCmdStrList;Dest:TLinkStrMap);
  775. // expands every thing in Src to Dest for linkorder purposes.
  776. var
  777. r : longint;
  778. LibN : TCmdStr;
  779. begin
  780. while not src.empty do
  781. begin
  782. LibN:=src.getfirst;
  783. r:=lookup (LibN);
  784. if r=-1 then
  785. dest.add(LibN)
  786. else
  787. dest.addseries(fmap[r].value);
  788. end;
  789. end;
  790. procedure TLinkStrMap.UpdateWeights(Weightmap:TLinkStrMap);
  791. var
  792. l,r : longint;
  793. begin
  794. for l := 0 to itemcnt-1 do
  795. begin
  796. r:=weightmap.lookup (fmap[l].key);
  797. if r<>-1 then
  798. fmap[l].weight:=weightmap[r].weight;
  799. end;
  800. end;
  801. {****************************************************************************
  802. Time Handling
  803. ****************************************************************************}
  804. Function L0(l:longint):string;
  805. {
  806. return the string of value l, if l<10 then insert a zero, so
  807. the string is always at least 2 chars '01','02',etc
  808. }
  809. var
  810. s : string;
  811. begin
  812. Str(l,s);
  813. if l<10 then
  814. s:='0'+s;
  815. L0:=s;
  816. end;
  817. function gettimestr:string;
  818. {
  819. get the current time in a string HH:MM:SS
  820. }
  821. var
  822. st: TSystemTime;
  823. begin
  824. GetLocalTime(st);
  825. gettimestr:=L0(st.Hour)+':'+L0(st.Minute)+':'+L0(st.Second);
  826. end;
  827. function getdatestr:string;
  828. {
  829. get the current date in a string YY/MM/DD
  830. }
  831. var
  832. st: TSystemTime;
  833. begin
  834. GetLocalTime(st);
  835. getdatestr:=L0(st.Year)+'/'+L0(st.Month)+'/'+L0(st.Day);
  836. end;
  837. function filetimestring( t : longint) : string;
  838. {
  839. convert dos datetime t to a string YY/MM/DD HH:MM:SS
  840. }
  841. var
  842. DT : TDateTime;
  843. hsec : word;
  844. Year,Month,Day: Word;
  845. hour,min,sec : word;
  846. begin
  847. if t=-1 then
  848. begin
  849. Result := 'Not Found';
  850. exit;
  851. end;
  852. DT := FileDateToDateTime(t);
  853. DecodeTime(DT,hour,min,sec,hsec);
  854. DecodeDate(DT,year,month,day);
  855. Result := L0(Year)+'/'+L0(Month)+'/'+L0(Day)+' '+L0(Hour)+':'+L0(min)+':'+L0(sec);
  856. end;
  857. function getrealtime(const st: TSystemTime) : real;
  858. begin
  859. result := st.Hour*3600.0 + st.Minute*60.0 + st.Second + st.MilliSecond/1000.0;
  860. end;
  861. function getrealtime : real;
  862. var
  863. st:TSystemTime;
  864. begin
  865. GetLocalTime(st);
  866. result:=getrealtime(st);
  867. end;
  868. {****************************************************************************
  869. Default Macro Handling
  870. ****************************************************************************}
  871. procedure DefaultReplacements(var s:ansistring; substitute_env_variables:boolean=true);
  872. {$ifdef mswindows}
  873. procedure ReplaceSpecialFolder(const MacroName: string; const ID: integer);
  874. begin
  875. // Only try to receive the special folders (and thus dynamically
  876. // load shfolder.dll) when that's needed.
  877. if pos(MacroName,s)>0 then
  878. Replace(s,MacroName,GetWindowsSpecialDir(ID));
  879. end;
  880. {$endif mswindows}
  881. {$ifdef openbsd}
  882. function GetOpenBSDLocalBase: ansistring;
  883. var
  884. envvalue: pchar;
  885. begin
  886. envvalue := GetEnvPChar('LOCALBASE');
  887. if assigned(envvalue) then
  888. Result:=envvalue
  889. else
  890. Result:='/usr/local';
  891. FreeEnvPChar(envvalue);
  892. end;
  893. function GetOpenBSDX11Base: ansistring;
  894. var
  895. envvalue: pchar;
  896. begin
  897. envvalue := GetEnvPChar('X11BASE');
  898. if assigned(envvalue) then
  899. Result:=envvalue
  900. else
  901. Result:='/usr/X11R6';
  902. FreeEnvPChar(envvalue);
  903. end;
  904. {$endif openbsd}
  905. var
  906. envstr: string;
  907. envvalue: pchar;
  908. i: integer;
  909. begin
  910. { Replace some macros }
  911. Replace(s,'$FPCVERSION',version_string);
  912. Replace(s,'$FPCFULLVERSION',full_version_string);
  913. Replace(s,'$FPCDATE',date_string);
  914. Replace(s,'$FPCCPU',target_cpu_string);
  915. Replace(s,'$FPCOS',target_os_string);
  916. Replace(s,'$FPCBINDIR',exepath);
  917. if (tf_use_8_3 in Source_Info.Flags) or
  918. (tf_use_8_3 in Target_Info.Flags) then
  919. Replace(s,'$FPCTARGET',target_os_string)
  920. else if subtarget<>'' then
  921. Replace(s,'$FPCTARGET',target_full_string+'-'+lower(subtarget))
  922. else
  923. Replace(s,'$FPCTARGET',target_full_string);
  924. Replace(s,'$FPCSUBARCH',lower(cputypestr[init_settings.cputype]));
  925. Replace(s,'$FPCABI',lower(abiinfo[target_info.abi].name));
  926. {$ifdef i8086}
  927. Replace(s,'$FPCMEMORYMODEL',lower(x86memorymodelstr[init_settings.x86memorymodel]));
  928. {$else i8086}
  929. Replace(s,'$FPCMEMORYMODEL','flat');
  930. {$endif i8086}
  931. {$ifdef mswindows}
  932. ReplaceSpecialFolder('$LOCAL_APPDATA',CSIDL_LOCAL_APPDATA);
  933. ReplaceSpecialFolder('$APPDATA',CSIDL_APPDATA);
  934. ReplaceSpecialFolder('$COMMON_APPDATA',CSIDL_COMMON_APPDATA);
  935. ReplaceSpecialFolder('$PERSONAL',CSIDL_PERSONAL);
  936. ReplaceSpecialFolder('$PROGRAM_FILES',CSIDL_PROGRAM_FILES);
  937. ReplaceSpecialFolder('$PROGRAM_FILES_COMMON',CSIDL_PROGRAM_FILES_COMMON);
  938. ReplaceSpecialFolder('$PROFILE',CSIDL_PROFILE);
  939. {$endif mswindows}
  940. {$ifdef openbsd}
  941. Replace(s,'$OPENBSD_LOCALBASE',GetOpenBSDLocalBase);
  942. Replace(s,'$OPENBSD_X11BASE',GetOpenBSDX11Base);
  943. {$endif openbsd}
  944. if not substitute_env_variables then
  945. exit;
  946. { Replace environment variables between dollar signs }
  947. i := pos('$',s);
  948. while i>0 do
  949. begin
  950. envstr:=copy(s,i+1,length(s)-i);
  951. i:=pos('$',envstr);
  952. if i>0 then
  953. begin
  954. envstr := copy(envstr,1,i-1);
  955. envvalue := GetEnvPChar(envstr);
  956. if assigned(envvalue) then
  957. begin
  958. Replace(s,'$'+envstr+'$',envvalue);
  959. // Look if there is another env.var in the string
  960. i:=pos('$',s);
  961. end
  962. else
  963. // if the env.var is not set, do not replace the env.variable
  964. // and stop looking for more env.var within the string
  965. i := 0;
  966. FreeEnvPChar(envvalue);
  967. end;
  968. end;
  969. end;
  970. {****************************************************************************
  971. OS Dependent things
  972. ****************************************************************************}
  973. function GetEnvPChar(const envname:ansistring):pchar;
  974. {$ifdef mswindows}
  975. var
  976. s : string;
  977. i,len : longint;
  978. hp,p,p2 : pchar;
  979. {$endif}
  980. begin
  981. {$ifdef hasunix}
  982. GetEnvPchar:=BaseUnix.fpGetEnv(pansichar(envname));
  983. {$define GETENVOK}
  984. {$endif}
  985. {$ifdef mswindows}
  986. GetEnvPchar:=nil;
  987. p:=GetEnvironmentStringsA;
  988. hp:=p;
  989. while hp^<>#0 do
  990. begin
  991. s:=strpas(hp);
  992. i:=pos('=',s);
  993. len:=strlen(hp);
  994. if upper(copy(s,1,i-1))=upper(envname) then
  995. begin
  996. GetMem(p2,len-length(envname));
  997. Move(hp[i],p2^,len-length(envname));
  998. GetEnvPchar:=p2;
  999. break;
  1000. end;
  1001. { next string entry}
  1002. hp:=hp+len+1;
  1003. end;
  1004. FreeEnvironmentStrings(p);
  1005. {$define GETENVOK}
  1006. {$endif}
  1007. {$ifdef os2}
  1008. GetEnvPChar := Dos.GetEnvPChar (EnvName);
  1009. {$define GETENVOK}
  1010. {$endif}
  1011. {$ifdef GETENVOK}
  1012. {$undef GETENVOK}
  1013. {$else}
  1014. GetEnvPchar:=StrPNew(GetEnvironmentVariable(envname));
  1015. if (length(GetEnvPChar)=0) then
  1016. begin
  1017. FreeEnvPChar(GetEnvPChar);
  1018. GetEnvPChar:=nil;
  1019. end;
  1020. {$endif}
  1021. end;
  1022. procedure FreeEnvPChar(p:pchar);
  1023. begin
  1024. {$ifndef hasunix}
  1025. {$ifndef os2}
  1026. freemem(p);
  1027. {$endif}
  1028. {$endif}
  1029. end;
  1030. function is_number_float(d : double) : boolean;
  1031. var
  1032. bytearray : array[0..7] of byte;
  1033. begin
  1034. move(d,bytearray,8);
  1035. { only 1.1 save, 1.0.x will use always little endian }
  1036. {$ifdef FPC_BIG_ENDIAN}
  1037. result:=((bytearray[0] and $7f)<>$7f) or ((bytearray[1] and $f0)<>$f0);
  1038. {$else FPC_BIG_ENDIAN}
  1039. result:=((bytearray[7] and $7f)<>$7f) or ((bytearray[6] and $f0)<>$f0);
  1040. {$endif FPC_BIG_ENDIAN}
  1041. end;
  1042. function get_real_sign(r: bestreal): longint;
  1043. var
  1044. p: pbyte;
  1045. begin
  1046. p := pbyte(@r);
  1047. {$ifdef FPUARM_HAS_FPA}
  1048. inc(p,4);
  1049. {$else}
  1050. {$ifdef FPC_LITTLE_ENDIAN}
  1051. inc(p,sizeof(r)-1);
  1052. {$endif}
  1053. {$endif}
  1054. if (p^ and $80) = 0 then
  1055. result := 1
  1056. else
  1057. result := -1;
  1058. end;
  1059. function convertdoublerec(d : tcompdoublerec) : tcompdoublerec;{$ifdef USEINLINE}inline;{$endif}
  1060. {$ifdef CPUARM}
  1061. var
  1062. i : longint;
  1063. begin
  1064. for i:=0 to 3 do
  1065. begin
  1066. result.bytes[i+4]:=d.bytes[i];
  1067. result.bytes[i]:=d.bytes[i+4];
  1068. end;
  1069. {$else CPUARM}
  1070. begin
  1071. result:=d;
  1072. {$endif CPUARM}
  1073. end;
  1074. { '('D1:'00000000-'D2:'0000-'D3:'0000-'D4:'0000-000000000000)' }
  1075. function string2guid(const s: string; var GUID: TGUID): boolean;
  1076. function ishexstr(const hs: string): boolean;
  1077. var
  1078. i: integer;
  1079. begin
  1080. ishexstr:=false;
  1081. for i:=1 to Length(hs) do begin
  1082. if not (hs[i] in ['0'..'9','A'..'F','a'..'f']) then
  1083. exit;
  1084. end;
  1085. ishexstr:=true;
  1086. end;
  1087. function hexstr2longint(const hexs: string): longint;
  1088. var
  1089. i: integer;
  1090. rl: longint;
  1091. begin
  1092. rl:=0;
  1093. for i:=1 to length(hexs) do begin
  1094. rl:=rl shl 4;
  1095. case hexs[i] of
  1096. '0'..'9' : inc(rl,ord(hexs[i])-ord('0'));
  1097. 'A'..'F' : inc(rl,ord(hexs[i])-ord('A')+10);
  1098. 'a'..'f' : inc(rl,ord(hexs[i])-ord('a')+10);
  1099. end
  1100. end;
  1101. hexstr2longint:=rl;
  1102. end;
  1103. var
  1104. i: integer;
  1105. begin
  1106. if (Length(s)=38) and (s[1]='{') and (s[38]='}') and
  1107. (s[10]='-') and (s[15]='-') and (s[20]='-') and (s[25]='-') and
  1108. ishexstr(copy(s,2,8)) and ishexstr(copy(s,11,4)) and
  1109. ishexstr(copy(s,16,4)) and ishexstr(copy(s,21,4)) and
  1110. ishexstr(copy(s,26,12)) then begin
  1111. GUID.D1:=dword(hexstr2longint(copy(s,2,8)));
  1112. { these values are arealdy in the correct range (4 chars = word) }
  1113. GUID.D2:=word(hexstr2longint(copy(s,11,4)));
  1114. GUID.D3:=word(hexstr2longint(copy(s,16,4)));
  1115. for i:=0 to 1 do
  1116. GUID.D4[i]:=byte(hexstr2longint(copy(s,21+i*2,2)));
  1117. for i:=2 to 7 do
  1118. GUID.D4[i]:=byte(hexstr2longint(copy(s,22+i*2,2)));
  1119. string2guid:=true;
  1120. end
  1121. else if (length(s)=0) then
  1122. begin
  1123. FillChar(GUID,SizeOf(GUID),0);
  1124. string2guid:=true;
  1125. end
  1126. else
  1127. string2guid:=false;
  1128. end;
  1129. function guid2string(const GUID: TGUID): string;
  1130. begin
  1131. guid2string:=
  1132. '{'+hexstr(GUID.D1,8)+
  1133. '-'+hexstr(GUID.D2,4)+
  1134. '-'+hexstr(GUID.D3,4)+
  1135. '-'+hexstr(GUID.D4[0],2)+hexstr(GUID.D4[1],2)+
  1136. '-'+hexstr(GUID.D4[2],2)+hexstr(GUID.D4[3],2)+
  1137. hexstr(GUID.D4[4],2)+hexstr(GUID.D4[5],2)+
  1138. hexstr(GUID.D4[6],2)+hexstr(GUID.D4[7],2)+
  1139. '}';
  1140. end;
  1141. function SetAktProcCall(const s:string; var a:tproccalloption):boolean;
  1142. const
  1143. DefProcCallName : array[tproccalloption] of string[16] = ('',
  1144. 'CDECL',
  1145. 'CPPDECL',
  1146. 'FAR16',
  1147. 'OLDFPCCALL',
  1148. '', { internproc }
  1149. '', { syscall }
  1150. 'PASCAL',
  1151. 'REGISTER',
  1152. 'SAFECALL',
  1153. 'STDCALL',
  1154. 'SOFTFLOAT',
  1155. 'MWPASCAL',
  1156. 'INTERRUPT',
  1157. 'HARDFLOAT',
  1158. 'SYSV_ABI_DEFAULT',
  1159. 'SYSV_ABI_CDECL',
  1160. 'MS_ABI_DEFAULT',
  1161. 'MS_ABI_CDECL',
  1162. 'VECTORCALL'
  1163. );
  1164. var
  1165. t : tproccalloption;
  1166. hs : string;
  1167. begin
  1168. result:=false;
  1169. if (s = '') then
  1170. exit;
  1171. hs:=upper(s);
  1172. if (hs = 'DEFAULT') then
  1173. begin
  1174. a := pocall_default;
  1175. result := true;
  1176. exit;
  1177. end;
  1178. for t:=low(tproccalloption) to high(tproccalloption) do
  1179. if DefProcCallName[t]=hs then
  1180. begin
  1181. a:=t;
  1182. result:=true;
  1183. break;
  1184. end;
  1185. end;
  1186. function Setabitype(const s:string;var a:tabi):boolean;
  1187. var
  1188. t : tabi;
  1189. hs : string;
  1190. begin
  1191. result:=false;
  1192. hs:=Upper(s);
  1193. for t:=low(t) to high(t) do
  1194. if abiinfo[t].supported and
  1195. (abiinfo[t].name=hs) then
  1196. begin
  1197. a:=t;
  1198. { abi_old_win32_gnu is a win32 i386 specific "feature" }
  1199. if (t<>abi_old_win32_gnu) or (target_info.system=system_i386_win32) then
  1200. result:=true;
  1201. break;
  1202. end;
  1203. end;
  1204. function Setoptimizecputype(const s:string;var a:tcputype):boolean;
  1205. var
  1206. t : tcputype;
  1207. hs : string;
  1208. begin
  1209. result:=false;
  1210. hs:=Upper(s);
  1211. for t:=low(tcputype) to high(tcputype) do
  1212. if cputypestr[t]=hs then
  1213. begin
  1214. a:=t;
  1215. result:=true;
  1216. break;
  1217. end;
  1218. end;
  1219. function Setcputype(const s:string;var a:tsettings):boolean;
  1220. var
  1221. t : tcputype;
  1222. hs : string;
  1223. begin
  1224. result:=false;
  1225. hs:=Upper(s);
  1226. for t:=low(tcputype) to high(tcputype) do
  1227. if cputypestr[t]=hs then
  1228. begin
  1229. a.cputype:=t;
  1230. result:=true;
  1231. break;
  1232. end;
  1233. {$ifdef arm}
  1234. { set default instruction set for arm }
  1235. if result then
  1236. begin
  1237. if a.cputype in [cpu_armv6m,cpu_armv6t2,cpu_armv7m,cpu_armv7em] then
  1238. a.instructionset:=is_thumb
  1239. else
  1240. a.instructionset:=is_arm;
  1241. end;
  1242. {$endif arm}
  1243. end;
  1244. function SetFpuType(const s:string;var a:tfputype):boolean;
  1245. var
  1246. t : tfputype;
  1247. begin
  1248. result:=false;
  1249. for t:=low(tfputype) to high(tfputype) do
  1250. if fputypestr[t]=s then
  1251. begin
  1252. a:=t;
  1253. result:=true;
  1254. break;
  1255. end;
  1256. end;
  1257. function SetControllerType(const s:string;var a:tcontrollertype):boolean;
  1258. var
  1259. t : tcontrollertype;
  1260. hs : string;
  1261. begin
  1262. { The following check allows to reduce amount of code for platforms }
  1263. { not supporting microcontrollers due to evaluation at compile time. }
  1264. {$PUSH}
  1265. {$WARN 6018 OFF} (* Unreachable code due to compile time evaluation *)
  1266. if ControllerSupport then
  1267. begin
  1268. result:=false;
  1269. hs:=Upper(s);
  1270. for t:=low(tcontrollertype) to high(tcontrollertype) do
  1271. if embedded_controllers[t].controllertypestr=hs then
  1272. begin
  1273. a:=t;
  1274. result:=true;
  1275. break;
  1276. end;
  1277. end
  1278. else
  1279. begin
  1280. a := ct_none;
  1281. Result := true;
  1282. end;
  1283. {$POP}
  1284. end;
  1285. function HandleFeature(const s : string) : boolean;
  1286. var
  1287. i : tfeature;
  1288. begin
  1289. result:=true;
  1290. for i:=low(tfeature) to high(tfeature) do
  1291. if s=featurestr[i] then
  1292. begin
  1293. include(features,i);
  1294. exit;
  1295. end;
  1296. { Also support -Sfnoheap to exclude heap }
  1297. if Copy(S,1,2)='NO' then
  1298. for i:=low(tfeature) to high(tfeature) do
  1299. if s='NO'+featurestr[i] then
  1300. begin
  1301. exclude(features,i);
  1302. exit;
  1303. end;
  1304. result:=false;
  1305. end;
  1306. function SetMinFPConstPrec(const s: string; var a: tfloattype) : boolean;
  1307. var
  1308. value, error: longint;
  1309. begin
  1310. if (upper(s)='DEFAULT') then
  1311. begin
  1312. a:=s32real;
  1313. result:=true;
  1314. exit;
  1315. end;
  1316. result:=false;
  1317. val(s,value,error);
  1318. if (error<>0) then
  1319. exit;
  1320. case value of
  1321. 32: a:=s32real;
  1322. 64: a:=s64real;
  1323. { adding support for 80 bit here is tricky, since we can't really }
  1324. { check whether the target cpu+OS actually supports it }
  1325. else
  1326. exit;
  1327. end;
  1328. result:=true;
  1329. end;
  1330. function size_2_align(len : asizeuint) : longint;
  1331. begin
  1332. if len>16 then
  1333. size_2_align:=32
  1334. else if len>8 then
  1335. size_2_align:=16
  1336. else if len>4 then
  1337. size_2_align:=8
  1338. else if len>2 then
  1339. size_2_align:=4
  1340. else if len>1 then
  1341. size_2_align:=2
  1342. else
  1343. size_2_align:=1;
  1344. end;
  1345. function var_align(want_align: longint): shortint;
  1346. begin
  1347. var_align := used_align(want_align,current_settings.alignment.varalignmin,current_settings.alignment.varalignmax);
  1348. end;
  1349. function var_align_size(siz: asizeuint): shortint;
  1350. begin
  1351. siz := size_2_align(siz);
  1352. var_align_size := var_align(siz);
  1353. end;
  1354. function const_align(want_align: longint): shortint;
  1355. begin
  1356. const_align := used_align(want_align,current_settings.alignment.constalignmin,current_settings.alignment.constalignmax);
  1357. end;
  1358. function const_align_size(siz: asizeuint): shortint;
  1359. begin
  1360. siz := size_2_align(siz);
  1361. const_align_size := const_align(siz);
  1362. end;
  1363. {$ifdef ARM}
  1364. function is_double_hilo_swapped: boolean;{$ifdef USEINLINE}inline;{$endif}
  1365. begin
  1366. result := (current_settings.fputype in [fpu_fpa,fpu_fpa10,fpu_fpa11]) and
  1367. not(cs_fp_emulation in current_settings.moduleswitches);
  1368. {$ifdef FPC_DOUBLE_HILO_SWAPPED}
  1369. { inverse result if compiler was compiled with swapped hilo already }
  1370. result := not result;
  1371. {$endif FPC_DOUBLE_HILO_SWAPPED}
  1372. end;
  1373. {$endif ARM}
  1374. function floating_point_range_check_error : boolean;
  1375. begin
  1376. result:=cs_ieee_errors in current_settings.localswitches;
  1377. end;
  1378. function use_dotted_functions: boolean;
  1379. begin
  1380. result:=
  1381. (target_info.system in systems_dotted_function_names) and
  1382. (target_info.abi<>abi_powerpc_elfv2);
  1383. end;
  1384. {****************************************************************************
  1385. Init
  1386. ****************************************************************************}
  1387. {$ifdef unix}
  1388. {$define need_path_search}
  1389. {$endif unix}
  1390. {$ifdef os2}
  1391. {$define need_path_search}
  1392. {$endif os2}
  1393. {$ifdef macos}
  1394. {$define need_path_search}
  1395. {$endif macos}
  1396. procedure get_exepath;
  1397. var
  1398. localExepath : TCmdStr;
  1399. exeName:TCmdStr;
  1400. {$ifdef need_path_search}
  1401. hs1 : TPathStr;
  1402. {$endif need_path_search}
  1403. begin
  1404. localexepath:=GetEnvironmentVariable('PPC_EXEC_PATH');
  1405. exeName := '';
  1406. if localexepath='' then
  1407. begin
  1408. exeName := FixFileName(system.paramstr(0));
  1409. localexepath := ExtractFilePath(exeName);
  1410. end;
  1411. {$ifdef need_path_search}
  1412. if localexepath='' then
  1413. begin
  1414. hs1 := ExtractFileName(exeName);
  1415. hs1 := ChangeFileExt(hs1,source_info.exeext);
  1416. {$ifdef macos}
  1417. FindFile(hs1,GetEnvironmentVariable('Commands'),false,localExepath);
  1418. {$else macos}
  1419. FindFile(hs1,GetEnvironmentVariable('PATH'),false,localExepath);
  1420. {$endif macos}
  1421. localExepath:=ExtractFilePath(localExepath);
  1422. end;
  1423. {$endif need_path_search}
  1424. exepath:=FixPath(localExepath,false);
  1425. end;
  1426. type
  1427. tinitdoneentry=record
  1428. init:tprocedure;
  1429. done:tprocedure;
  1430. end;
  1431. pinitdoneentry=^tinitdoneentry;
  1432. const
  1433. initdoneprocs : TFPList = nil;
  1434. procedure allocinitdoneprocs;
  1435. begin
  1436. { Avoid double initialization }
  1437. if assigned(initdoneprocs) then
  1438. exit;
  1439. initdoneprocs:=tfplist.create;
  1440. end;
  1441. procedure register_initdone_proc(init,done:tprocedure);
  1442. var
  1443. entry : pinitdoneentry;
  1444. begin
  1445. new(entry);
  1446. entry^.init:=init;
  1447. entry^.done:=done;
  1448. { Do not rely on the fact that
  1449. globals unit initialization code
  1450. has already been executed.
  1451. Unit initialization order is too
  1452. uncertian for that. PM }
  1453. if not assigned(initdoneprocs) then
  1454. allocinitdoneprocs;
  1455. initdoneprocs.add(entry);
  1456. end;
  1457. procedure callinitprocs;
  1458. var
  1459. i : longint;
  1460. begin
  1461. if not assigned(initdoneprocs) then
  1462. exit;
  1463. for i:=0 to initdoneprocs.count-1 do
  1464. with pinitdoneentry(initdoneprocs[i])^ do
  1465. if assigned(init) then
  1466. init();
  1467. end;
  1468. procedure calldoneprocs;
  1469. var
  1470. i : longint;
  1471. begin
  1472. if not assigned(initdoneprocs) then
  1473. exit;
  1474. for i:=0 to initdoneprocs.count-1 do
  1475. with pinitdoneentry(initdoneprocs[i])^ do
  1476. if assigned(done) then
  1477. done();
  1478. end;
  1479. procedure freeinitdoneprocs;
  1480. var
  1481. i : longint;
  1482. begin
  1483. if not assigned(initdoneprocs) then
  1484. exit;
  1485. for i:=0 to initdoneprocs.count-1 do
  1486. dispose(pinitdoneentry(initdoneprocs[i]));
  1487. initdoneprocs.free;
  1488. { Reset variable, to be on the safe side }
  1489. initdoneprocs:=nil;
  1490. end;
  1491. procedure DoneGlobals;
  1492. begin
  1493. calldoneprocs;
  1494. librarysearchpath.Free;
  1495. unitsearchpath.Free;
  1496. objectsearchpath.Free;
  1497. includesearchpath.Free;
  1498. frameworksearchpath.Free;
  1499. LinkLibraryAliases.Free;
  1500. LinkLibraryOrder.Free;
  1501. packagesearchpath.Free;
  1502. namespacelist.Free;
  1503. premodule_namespacelist.Free;
  1504. current_namespacelist:=Nil;
  1505. end;
  1506. procedure InitGlobals;
  1507. begin
  1508. get_exepath;
  1509. { reset globals }
  1510. do_build:=false;
  1511. do_release:=false;
  1512. do_make:=true;
  1513. codegenerror:=false;
  1514. { Output }
  1515. OutputFileName:='';
  1516. OutputPrefix:=Nil;
  1517. OutputSuffix:=Nil;
  1518. OutputExeDir:='';
  1519. OutputUnitDir:='';
  1520. { Utils directory }
  1521. utilsdirectory:='';
  1522. utilsprefix:='';
  1523. llvmutilssuffix:='';
  1524. cshared:=false;
  1525. rlinkpath:='';
  1526. sysrootpath:='';
  1527. {$if defined(XTENSA) or defined(RISCV32)}
  1528. idfpath:='';
  1529. {$endif defined(XTENSA) or defined(RISCV32)}
  1530. { Search Paths }
  1531. unicodepath:='';
  1532. librarysearchpath:=TSearchPathList.Create;
  1533. unitsearchpath:=TSearchPathList.Create;
  1534. includesearchpath:=TSearchPathList.Create;
  1535. objectsearchpath:=TSearchPathList.Create;
  1536. frameworksearchpath:=TSearchPathList.Create;
  1537. packagesearchpath:=TSearchPathList.Create;
  1538. namespacelist:=TCmdStrList.Create;
  1539. premodule_namespacelist:=TCmdStrList.Create;
  1540. current_namespacelist:=Nil;
  1541. { Def file }
  1542. usewindowapi:=false;
  1543. description:='Compiled by FPC '+version_string+' - '+target_cpu_string;
  1544. DescriptionSetExplicity:=false;
  1545. SetPEFlagsSetExplicity:=false;
  1546. SetPEOptFlagsSetExplicity:=false;
  1547. SetPEOSVersionSetExplicitely:=false;
  1548. SetPESubSysVersionSetExplicitely:=false;
  1549. SetPEUserVersionSetExplicitely:=false;
  1550. ImageBaseSetExplicity:=false;
  1551. MinStackSizeSetExplicity:=false;
  1552. MaxStackSizeSetExplicity:=false;
  1553. dllversion:='';
  1554. dllmajor:=1;
  1555. dllminor:=0;
  1556. dllrevision:=0;
  1557. nwscreenname := '';
  1558. nwthreadname := '';
  1559. nwcopyright := '';
  1560. UseDeffileForExports:=false;
  1561. UseDeffileForExportsSetExplicitly:=false;
  1562. GenerateImportSection:=false;
  1563. RelocSection:=false;
  1564. RelocSectionSetExplicitly:=false;
  1565. MacOSXVersionMin.invalidate;
  1566. iPhoneOSVersionMin.invalidate;
  1567. { memory sizes, will be overridden by parameter or default for target
  1568. in options or init_parser }
  1569. stacksize:=0;
  1570. { not initialized yet }
  1571. apptype:=app_cui;
  1572. { Init values }
  1573. init_settings:=default_settings;
  1574. if init_settings.optimizecputype=cpu_none then
  1575. init_settings.optimizecputype:=init_settings.cputype;
  1576. LinkLibraryAliases :=TLinkStrMap.Create;
  1577. LinkLibraryOrder :=TLinkStrMap.Create;
  1578. { enable all features by default }
  1579. features:=[low(Tfeature)..high(Tfeature)];
  1580. callinitprocs;
  1581. end;
  1582. initialization
  1583. allocinitdoneprocs;
  1584. {$ifdef LLVM}
  1585. cgbackend:=cg_llvm;
  1586. {$else}
  1587. cgbackend:=cg_fpc;
  1588. {$endif}
  1589. finalization
  1590. freeinitdoneprocs;
  1591. end.