assemble.pas 95 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782
  1. {
  2. Copyright (c) 1998-2004 by Peter Vreman
  3. This unit handles the assemblerfile write and assembler calls of FPC
  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. {# @abstract(This unit handles the assembler file write and assembler calls of FPC)
  18. Handles the calls to the actual external assemblers, as well as the generation
  19. of object files for smart linking. Also contains the base class for writing
  20. the assembler statements to file.
  21. }
  22. unit assemble;
  23. {$i fpcdefs.inc}
  24. interface
  25. uses
  26. SysUtils,
  27. systems,globtype,globals,aasmbase,aasmtai,aasmdata,ogbase,owbase,finput;
  28. const
  29. { maximum of aasmoutput lists there will be }
  30. maxoutputlists = ord(high(tasmlisttype))+1;
  31. { buffer size for writing the .s file }
  32. AsmOutSize=32768*4;
  33. type
  34. TAssembler=class(TObject)
  35. public
  36. {assembler info}
  37. asminfo : pasminfo;
  38. {filenames}
  39. path : TPathStr;
  40. name : string;
  41. AsmFileName, { current .s and .o file }
  42. ObjFileName,
  43. ppufilename : TPathStr;
  44. asmprefix : string;
  45. SmartAsm : boolean;
  46. SmartFilesCount,
  47. SmartHeaderCount : longint;
  48. Constructor Create(info: pasminfo; smart:boolean);virtual;
  49. Destructor Destroy;override;
  50. procedure NextSmartName(place:tcutplace);
  51. procedure MakeObject;virtual;abstract;
  52. end;
  53. TExternalAssembler = class;
  54. IExternalAssemblerOutputFileDecorator=interface
  55. function LinePrefix: AnsiString;
  56. function LinePostfix: AnsiString;
  57. function LineFilter(const s: AnsiString): AnsiString;
  58. function LineEnding(const deflineending: ShortString): ShortString;
  59. end;
  60. TExternalAssemblerOutputFile=class
  61. private
  62. fdecorator: IExternalAssemblerOutputFileDecorator;
  63. protected
  64. owner: TExternalAssembler;
  65. {outfile}
  66. AsmSize,
  67. AsmStartSize,
  68. outcnt : longint;
  69. outbuf : array[0..AsmOutSize-1] of char;
  70. outfile : file;
  71. fioerror : boolean;
  72. linestart: boolean;
  73. Procedure AsmClear;
  74. Procedure MaybeAddLinePrefix;
  75. Procedure MaybeAddLinePostfix;
  76. Procedure AsmWriteAnsiStringUnfiltered(const s: ansistring);
  77. public
  78. Constructor Create(_owner: TExternalAssembler);
  79. Procedure RemoveAsm;virtual;
  80. Procedure AsmFlush;
  81. { mark the current output as the "empty" state (i.e., it only contains
  82. headers/directives etc }
  83. Procedure MarkEmpty;
  84. { clears the assembler output if nothing was added since it was marked
  85. as empty, and returns whether it was empty }
  86. function ClearIfEmpty: boolean;
  87. { these routines will write the filtered version of their argument
  88. according to the current decorator }
  89. procedure AsmWriteFiltered(const c:char);
  90. procedure AsmWriteFiltered(const s:string);
  91. procedure AsmWriteFiltered(const s:ansistring);
  92. procedure AsmWriteFiltered(p:pchar; len: longint);
  93. {# Write a string to the assembler file }
  94. Procedure AsmWrite(const c:char);
  95. Procedure AsmWrite(const s:string);
  96. Procedure AsmWrite(const s:ansistring);
  97. {# Write a string to the assembler file }
  98. Procedure AsmWritePChar(p:pchar);
  99. {# Write a string to the assembler file followed by a new line }
  100. Procedure AsmWriteLn(const c:char);
  101. Procedure AsmWriteLn(const s:string);
  102. Procedure AsmWriteLn(const s:ansistring);
  103. {# Write a new line to the assembler file }
  104. Procedure AsmLn; virtual;
  105. procedure AsmCreate(Aplace:tcutplace);
  106. procedure AsmClose;
  107. property ioerror: boolean read fioerror;
  108. property decorator: IExternalAssemblerOutputFileDecorator read fdecorator write fdecorator;
  109. end;
  110. {# This is the base class which should be overridden for each each
  111. assembler writer. It is used to actually assembler a file,
  112. and write the output to the assembler file.
  113. }
  114. TExternalAssembler=class(TAssembler)
  115. private
  116. { output writer }
  117. fwriter: TExternalAssemblerOutputFile;
  118. ffreewriter: boolean;
  119. procedure CreateSmartLinkPath(const s:TPathStr);
  120. protected
  121. {input source info}
  122. lastfileinfo : tfileposinfo;
  123. infile,
  124. lastinfile : tinputfile;
  125. {last section type written}
  126. lastsectype : TAsmSectionType;
  127. procedure ResetSourceLines;
  128. procedure WriteSourceLine(hp: tailineinfo);
  129. procedure WriteTempalloc(hp: tai_tempalloc);
  130. procedure WriteRealConstAsBytes(hp: tai_realconst; const dbdir: string; do_line: boolean);
  131. function WriteComments(var hp: tai): boolean;
  132. function single2str(d : single) : string; virtual;
  133. function double2str(d : double) : string; virtual;
  134. function extended2str(e : extended) : string; virtual;
  135. function sleb128tostr(a : int64) : string;
  136. function uleb128tostr(a : qword) : string;
  137. Function DoPipe:boolean; virtual;
  138. function CreateNewAsmWriter: TExternalAssemblerOutputFile; virtual;
  139. {# Return true if the external assembler should run again }
  140. function RerunAssembler: boolean; virtual;
  141. public
  142. {# Returns the complete path and executable name of the assembler
  143. program.
  144. It first tries looking in the UTIL directory if specified,
  145. otherwise it searches in the free pascal binary directory, in
  146. the current working directory and then in the directories
  147. in the $PATH environment.}
  148. Function FindAssembler:string;
  149. {# Actually does the call to the assembler file. Returns false
  150. if the assembling of the file failed.}
  151. Function CallAssembler(const command:string; const para:TCmdStr):Boolean;
  152. Function DoAssemble:boolean;virtual;
  153. {# This routine should be overridden for each assembler, it is used
  154. to actually write the abstract assembler stream to file.}
  155. procedure WriteTree(p:TAsmList);virtual;
  156. {# This routine should be overridden for each assembler, it is used
  157. to actually write all the different abstract assembler streams
  158. by calling for each stream type, the @var(WriteTree) method.}
  159. procedure WriteAsmList;virtual;
  160. {# Constructs the command line for calling the assembler }
  161. function MakeCmdLine: TCmdStr; virtual;
  162. public
  163. Constructor Create(info: pasminfo; smart: boolean); override; final;
  164. Constructor CreateWithWriter(info: pasminfo; wr: TExternalAssemblerOutputFile; freewriter, smart: boolean); virtual;
  165. procedure MakeObject;override;
  166. destructor Destroy; override;
  167. property writer: TExternalAssemblerOutputFile read fwriter;
  168. end;
  169. TExternalAssemblerClass = class of TExternalAssembler;
  170. { TInternalAssembler }
  171. TInternalAssembler=class(TAssembler)
  172. private
  173. {$ifdef ARM}
  174. { true, if thumb instructions are generated }
  175. Code16 : Boolean;
  176. {$endif ARM}
  177. FCObjOutput : TObjOutputclass;
  178. FCInternalAr : TObjectWriterClass;
  179. { the aasmoutput lists that need to be processed }
  180. lists : byte;
  181. list : array[1..maxoutputlists] of TAsmList;
  182. { current processing }
  183. currlistidx : byte;
  184. currlist : TAsmList;
  185. procedure WriteStab(p:pchar);
  186. function MaybeNextList(var hp:Tai):boolean;
  187. function SetIndirectToSymbol(hp: Tai; const indirectname: string): Boolean;
  188. function TreePass0(hp:Tai):Tai;
  189. function TreePass1(hp:Tai):Tai;
  190. function TreePass2(hp:Tai):Tai;
  191. procedure writetree;
  192. procedure writetreesmart;
  193. protected
  194. ObjData : TObjData;
  195. ObjOutput : tObjOutput;
  196. property CObjOutput:TObjOutputclass read FCObjOutput write FCObjOutput;
  197. property CInternalAr : TObjectWriterClass read FCInternalAr write FCInternalAr;
  198. public
  199. constructor Create(info: pasminfo; smart: boolean);override;
  200. destructor destroy;override;
  201. procedure MakeObject;override;
  202. end;
  203. TAssemblerClass = class of TAssembler;
  204. Procedure GenerateAsm(smart:boolean);
  205. { get an instance of an external GNU-style assembler that is compatible
  206. with the current target, reusing an existing writer. Used by the LLVM
  207. target to write inline assembler }
  208. function GetExternalGnuAssemblerWithAsmInfoWriter(info: pasminfo; wr: TExternalAssemblerOutputFile): TExternalAssembler;
  209. procedure RegisterAssembler(const r:tasminfo;c:TAssemblerClass);
  210. Implementation
  211. uses
  212. {$ifdef hasunix}
  213. unix,
  214. {$endif}
  215. cutils,cfileutl,
  216. {$ifdef memdebug}
  217. cclasses,
  218. {$endif memdebug}
  219. {$ifdef OMFOBJSUPPORT}
  220. omfbase,
  221. ogomf,
  222. {$endif OMFOBJSUPPORT}
  223. {$if defined(cpuextended) and defined(FPC_HAS_TYPE_EXTENDED)}
  224. {$else}
  225. {$ifdef FPC_SOFT_FPUX80}
  226. sfpux80,
  227. {$endif FPC_SOFT_FPUX80}
  228. {$endif}
  229. {$ifdef WASM}
  230. ogwasm,
  231. {$endif WASM}
  232. cscript,fmodule,verbose,
  233. cpubase,cpuinfo,triplet,
  234. aasmcpu;
  235. var
  236. CAssembler : array[tasm] of TAssemblerClass;
  237. function fixline(const s:string):string;
  238. {
  239. return s with all leading and ending spaces and tabs removed
  240. }
  241. var
  242. i,j,k : integer;
  243. begin
  244. i:=length(s);
  245. while (i>0) and (s[i] in [#9,' ']) do
  246. dec(i);
  247. j:=1;
  248. while (j<i) and (s[j] in [#9,' ']) do
  249. inc(j);
  250. result := Copy(s, j, i - j + 1);
  251. for k:=1 to length(result) do
  252. if result[k] in [#0..#31,#127..#255] then
  253. result[k]:='.';
  254. end;
  255. {*****************************************************************************
  256. TAssembler
  257. *****************************************************************************}
  258. Constructor TAssembler.Create(info: pasminfo; smart: boolean);
  259. begin
  260. asminfo:=info;
  261. { load start values }
  262. AsmFileName:=current_module.AsmFilename;
  263. ObjFileName:=current_module.ObjFileName;
  264. name:=Lower(current_module.modulename^);
  265. path:=current_module.outputpath;
  266. asmprefix := current_module.asmprefix^;
  267. if current_module.outputpath = '' then
  268. ppufilename := ''
  269. else
  270. ppufilename := current_module.ppufilename;
  271. SmartAsm:=smart;
  272. SmartFilesCount:=0;
  273. SmartHeaderCount:=0;
  274. SmartLinkOFiles.Clear;
  275. end;
  276. Destructor TAssembler.Destroy;
  277. begin
  278. end;
  279. procedure TAssembler.NextSmartName(place:tcutplace);
  280. var
  281. s : string;
  282. begin
  283. inc(SmartFilesCount);
  284. if SmartFilesCount>999999 then
  285. Message(asmw_f_too_many_asm_files);
  286. case place of
  287. cut_begin :
  288. begin
  289. inc(SmartHeaderCount);
  290. s:=asmprefix+tostr(SmartHeaderCount)+'h';
  291. end;
  292. cut_normal :
  293. s:=asmprefix+tostr(SmartHeaderCount)+'s';
  294. cut_end :
  295. s:=asmprefix+tostr(SmartHeaderCount)+'t';
  296. end;
  297. AsmFileName:=Path+FixFileName(s+tostr(SmartFilesCount)+target_info.asmext);
  298. ObjFileName:=Path+FixFileName(s+tostr(SmartFilesCount)+target_info.objext);
  299. { insert in container so it can be cleared after the linking }
  300. SmartLinkOFiles.Insert(ObjFileName);
  301. end;
  302. {*****************************************************************************
  303. TAssemblerOutputFile
  304. *****************************************************************************}
  305. procedure TExternalAssemblerOutputFile.RemoveAsm;
  306. var
  307. g : file;
  308. begin
  309. if cs_asm_leave in current_settings.globalswitches then
  310. exit;
  311. if cs_asm_extern in current_settings.globalswitches then
  312. AsmRes.AddDeleteCommand(owner.AsmFileName)
  313. else
  314. begin
  315. assign(g,owner.AsmFileName);
  316. {$push} {$I-}
  317. erase(g);
  318. {$pop}
  319. if ioresult<>0 then;
  320. end;
  321. end;
  322. Procedure TExternalAssemblerOutputFile.AsmFlush;
  323. begin
  324. if outcnt>0 then
  325. begin
  326. { suppress i/o error }
  327. {$push} {$I-}
  328. BlockWrite(outfile,outbuf,outcnt);
  329. {$pop}
  330. fioerror:=fioerror or (ioresult<>0);
  331. outcnt:=0;
  332. end;
  333. end;
  334. procedure TExternalAssemblerOutputFile.MarkEmpty;
  335. begin
  336. AsmStartSize:=AsmSize
  337. end;
  338. function TExternalAssemblerOutputFile.ClearIfEmpty: boolean;
  339. begin
  340. result:=AsmSize=AsmStartSize;
  341. if result then
  342. AsmClear;
  343. end;
  344. procedure TExternalAssemblerOutputFile.AsmWriteFiltered(const c: char);
  345. begin
  346. MaybeAddLinePrefix;
  347. AsmWriteAnsiStringUnfiltered(decorator.LineFilter(c));
  348. end;
  349. procedure TExternalAssemblerOutputFile.AsmWriteFiltered(const s: string);
  350. begin
  351. MaybeAddLinePrefix;
  352. AsmWriteAnsiStringUnfiltered(decorator.LineFilter(s));
  353. end;
  354. procedure TExternalAssemblerOutputFile.AsmWriteFiltered(const s: ansistring);
  355. begin
  356. MaybeAddLinePrefix;
  357. AsmWriteAnsiStringUnfiltered(decorator.LineFilter(s));
  358. end;
  359. procedure TExternalAssemblerOutputFile.AsmWriteFiltered(p: pchar; len: longint);
  360. var
  361. s: ansistring;
  362. begin
  363. MaybeAddLinePrefix;
  364. s:='';
  365. setlength(s,len);
  366. move(p^,s[1],len);
  367. AsmWriteAnsiStringUnfiltered(decorator.LineFilter(s));
  368. end;
  369. Procedure TExternalAssemblerOutputFile.AsmClear;
  370. begin
  371. outcnt:=0;
  372. end;
  373. procedure TExternalAssemblerOutputFile.MaybeAddLinePrefix;
  374. begin
  375. if assigned(decorator) and
  376. linestart then
  377. begin
  378. AsmWriteAnsiStringUnfiltered(decorator.LinePrefix);
  379. linestart:=false;
  380. end;
  381. end;
  382. procedure TExternalAssemblerOutputFile.MaybeAddLinePostfix;
  383. begin
  384. if assigned(decorator) and
  385. not linestart then
  386. begin
  387. AsmWriteAnsiStringUnfiltered(decorator.LinePostfix);
  388. linestart:=true;
  389. end;
  390. end;
  391. procedure TExternalAssemblerOutputFile.AsmWriteAnsiStringUnfiltered(const s: ansistring);
  392. var
  393. StartIndex, ToWrite: longint;
  394. begin
  395. if s='' then
  396. exit;
  397. if OutCnt+length(s)>=AsmOutSize then
  398. AsmFlush;
  399. StartIndex:=1;
  400. ToWrite:=length(s);
  401. while ToWrite>AsmOutSize do
  402. begin
  403. Move(s[StartIndex],OutBuf[OutCnt],AsmOutSize);
  404. inc(OutCnt,AsmOutSize);
  405. inc(AsmSize,AsmOutSize);
  406. AsmFlush;
  407. inc(StartIndex,AsmOutSize);
  408. dec(ToWrite,AsmOutSize);
  409. end;
  410. Move(s[StartIndex],OutBuf[OutCnt],ToWrite);
  411. inc(OutCnt,ToWrite);
  412. inc(AsmSize,ToWrite);
  413. end;
  414. constructor TExternalAssemblerOutputFile.Create(_owner: TExternalAssembler);
  415. begin
  416. owner:=_owner;
  417. linestart:=true;
  418. end;
  419. Procedure TExternalAssemblerOutputFile.AsmWrite(const c: char);
  420. begin
  421. if assigned(decorator) then
  422. AsmWriteFiltered(c)
  423. else
  424. begin
  425. if OutCnt+1>=AsmOutSize then
  426. AsmFlush;
  427. OutBuf[OutCnt]:=c;
  428. inc(OutCnt);
  429. inc(AsmSize);
  430. end;
  431. end;
  432. Procedure TExternalAssemblerOutputFile.AsmWrite(const s:string);
  433. begin
  434. if s='' then
  435. exit;
  436. if assigned(decorator) then
  437. AsmWriteFiltered(s)
  438. else
  439. begin
  440. if OutCnt+length(s)>=AsmOutSize then
  441. AsmFlush;
  442. Move(s[1],OutBuf[OutCnt],length(s));
  443. inc(OutCnt,length(s));
  444. inc(AsmSize,length(s));
  445. end;
  446. end;
  447. Procedure TExternalAssemblerOutputFile.AsmWrite(const s:ansistring);
  448. begin
  449. if s='' then
  450. exit;
  451. if assigned(decorator) then
  452. AsmWriteFiltered(s)
  453. else
  454. AsmWriteAnsiStringUnfiltered(s);
  455. end;
  456. procedure TExternalAssemblerOutputFile.AsmWriteLn(const c: char);
  457. begin
  458. AsmWrite(c);
  459. AsmLn;
  460. end;
  461. Procedure TExternalAssemblerOutputFile.AsmWriteLn(const s:string);
  462. begin
  463. AsmWrite(s);
  464. AsmLn;
  465. end;
  466. Procedure TExternalAssemblerOutputFile.AsmWriteLn(const s: ansistring);
  467. begin
  468. AsmWrite(s);
  469. AsmLn;
  470. end;
  471. Procedure TExternalAssemblerOutputFile.AsmWritePChar(p:pchar);
  472. var
  473. i,j : longint;
  474. begin
  475. i:=StrLen(p);
  476. if i=0 then
  477. exit;
  478. if assigned(decorator) then
  479. AsmWriteFiltered(p,i)
  480. else
  481. begin
  482. j:=i;
  483. while j>0 do
  484. begin
  485. i:=min(j,AsmOutSize);
  486. if OutCnt+i>=AsmOutSize then
  487. AsmFlush;
  488. Move(p[0],OutBuf[OutCnt],i);
  489. inc(OutCnt,i);
  490. inc(AsmSize,i);
  491. dec(j,i);
  492. p:=pchar(@p[i]);
  493. end;
  494. end;
  495. end;
  496. Procedure TExternalAssemblerOutputFile.AsmLn;
  497. var
  498. newline: pshortstring;
  499. newlineres: shortstring;
  500. index: longint;
  501. begin
  502. MaybeAddLinePostfix;
  503. if (cs_assemble_on_target in current_settings.globalswitches) then
  504. newline:=@target_info.newline
  505. else
  506. newline:=@source_info.newline;
  507. if assigned(decorator) then
  508. begin
  509. newlineres:=decorator.LineEnding(newline^);
  510. newline:=@newlineres;
  511. end;
  512. if OutCnt>=AsmOutSize-length(newline^) then
  513. AsmFlush;
  514. index:=1;
  515. repeat
  516. OutBuf[OutCnt]:=newline^[index];
  517. inc(OutCnt);
  518. inc(AsmSize);
  519. inc(index);
  520. until index>length(newline^);
  521. end;
  522. procedure TExternalAssemblerOutputFile.AsmCreate(Aplace:tcutplace);
  523. {$ifdef hasamiga}
  524. var
  525. tempFileName: TPathStr;
  526. {$endif}
  527. begin
  528. if owner.SmartAsm then
  529. owner.NextSmartName(Aplace);
  530. {$ifdef hasamiga}
  531. { on Amiga/MorphOS try to redirect .s files to the T: assign, which is
  532. for temp files, and usually (default setting) located in the RAM: drive.
  533. This highly improves assembling speed for complex projects like the
  534. compiler itself, especially on hardware with slow disk I/O.
  535. Consider this as a poor man's pipe on Amiga, because real pipe handling
  536. would be much more complex and error prone to implement. (KB) }
  537. if (([cs_asm_extern,cs_asm_leave,cs_assemble_on_target] * current_settings.globalswitches) = []) then
  538. begin
  539. { try to have an unique name for the .s file }
  540. tempFileName:=HexStr(GetProcessID shr 4,7)+ExtractFileName(owner.AsmFileName);
  541. {$ifndef morphos}
  542. { old Amiga RAM: handler only allows filenames up to 30 char }
  543. if Length(tempFileName) < 30 then
  544. {$endif}
  545. owner.AsmFileName:='T:'+tempFileName;
  546. end;
  547. {$endif}
  548. {$ifdef hasunix}
  549. if owner.DoPipe then
  550. begin
  551. if owner.SmartAsm then
  552. begin
  553. if (owner.SmartFilesCount<=1) then
  554. Message1(exec_i_assembling_smart,owner.name);
  555. end
  556. else
  557. Message1(exec_i_assembling_pipe,owner.AsmFileName);
  558. if checkverbosity(V_Executable) then
  559. comment(V_Executable,'Executing "'+maybequoted(owner.FindAssembler)+'" with command line "'+
  560. owner.MakeCmdLine+'"');
  561. POpen(outfile,maybequoted(owner.FindAssembler)+' '+owner.MakeCmdLine,'W');
  562. end
  563. else
  564. {$endif}
  565. begin
  566. Assign(outfile,owner.AsmFileName);
  567. {$push} {$I-}
  568. Rewrite(outfile,1);
  569. {$pop}
  570. if ioresult<>0 then
  571. begin
  572. fioerror:=true;
  573. Message1(exec_d_cant_create_asmfile,owner.AsmFileName);
  574. end;
  575. end;
  576. outcnt:=0;
  577. AsmSize:=0;
  578. AsmStartSize:=0;
  579. end;
  580. procedure TExternalAssemblerOutputFile.AsmClose;
  581. var
  582. f : file;
  583. FileAge : longint;
  584. begin
  585. AsmFlush;
  586. {$ifdef hasunix}
  587. if owner.DoPipe then
  588. begin
  589. if PClose(outfile) <> 0 then
  590. GenerateError;
  591. end
  592. else
  593. {$endif}
  594. begin
  595. {Touch Assembler time to ppu time is there is a ppufilename}
  596. if owner.ppufilename<>'' then
  597. begin
  598. Assign(f,owner.ppufilename);
  599. {$push} {$I-}
  600. reset(f,1);
  601. {$pop}
  602. if ioresult=0 then
  603. begin
  604. FileAge := FileGetDate(GetFileHandle(f));
  605. close(f);
  606. reset(outfile,1);
  607. FileSetDate(GetFileHandle(outFile),FileAge);
  608. end;
  609. end;
  610. close(outfile);
  611. end;
  612. end;
  613. {*****************************************************************************
  614. TExternalAssembler
  615. *****************************************************************************}
  616. function TExternalAssembler.single2str(d : single) : string;
  617. var
  618. hs : string;
  619. begin
  620. str(d,hs);
  621. { replace space with + }
  622. if hs[1]=' ' then
  623. hs[1]:='+';
  624. single2str:='0d'+hs
  625. end;
  626. function TExternalAssembler.double2str(d : double) : string;
  627. var
  628. hs : string;
  629. begin
  630. str(d,hs);
  631. { replace space with + }
  632. if hs[1]=' ' then
  633. hs[1]:='+';
  634. double2str:='0d'+hs
  635. end;
  636. function TExternalAssembler.extended2str(e : extended) : string;
  637. var
  638. hs : string;
  639. begin
  640. str(e,hs);
  641. { replace space with + }
  642. if hs[1]=' ' then
  643. hs[1]:='+';
  644. extended2str:='0d'+hs
  645. end;
  646. function TExternalAssembler.sleb128tostr(a: int64): string;
  647. var
  648. i,len : longint;
  649. buf : array[0..31] of byte;
  650. begin
  651. result:='';
  652. len:=EncodeSleb128(a,buf,0);
  653. for i:=0 to len-1 do
  654. begin
  655. if (i > 0) then
  656. result:=result+',';
  657. result:=result+tostr(buf[i]);
  658. end;
  659. end;
  660. function TExternalAssembler.uleb128tostr(a: qword): string;
  661. var
  662. i,len : longint;
  663. buf : array[0..31] of byte;
  664. begin
  665. result:='';
  666. len:=EncodeUleb128(a,buf,0);
  667. for i:=0 to len-1 do
  668. begin
  669. if (i > 0) then
  670. result:=result+',';
  671. result:=result+tostr(buf[i]);
  672. end;
  673. end;
  674. Function TExternalAssembler.DoPipe:boolean;
  675. begin
  676. {$ifdef hasunix}
  677. DoPipe:=(cs_asm_pipe in current_settings.globalswitches) and
  678. (([cs_asm_extern,cs_asm_leave,cs_assemble_on_target] * current_settings.globalswitches) = []) and
  679. ((asminfo^.id in [as_gas,as_ggas,as_darwin,as_powerpc_xcoff,as_clang_gas,as_clang_llvm,as_clang_llvm_darwin,as_solaris_as,as_clang_asdarwin]));
  680. {$else hasunix}
  681. DoPipe:=false;
  682. {$endif}
  683. end;
  684. function TExternalAssembler.CreateNewAsmWriter: TExternalAssemblerOutputFile;
  685. begin
  686. result:=TExternalAssemblerOutputFile.Create(self);
  687. end;
  688. Constructor TExternalAssembler.Create(info: pasminfo; smart: boolean);
  689. begin
  690. CreateWithWriter(info,CreateNewAsmWriter,true,smart);
  691. end;
  692. constructor TExternalAssembler.CreateWithWriter(info: pasminfo; wr: TExternalAssemblerOutputFile; freewriter,smart: boolean);
  693. begin
  694. inherited Create(info,smart);
  695. fwriter:=wr;
  696. ffreewriter:=freewriter;
  697. if SmartAsm then
  698. begin
  699. path:=FixPath(ChangeFileExt(AsmFileName,target_info.smartext),false);
  700. CreateSmartLinkPath(path);
  701. end;
  702. end;
  703. procedure TExternalAssembler.CreateSmartLinkPath(const s:TPathStr);
  704. procedure DeleteFilesWithExt(const AExt:string);
  705. var
  706. dir : TRawByteSearchRec;
  707. begin
  708. if findfirst(FixPath(s,false)+'*'+AExt,faAnyFile,dir) = 0 then
  709. begin
  710. repeat
  711. DeleteFile(s+source_info.dirsep+dir.name);
  712. until findnext(dir) <> 0;
  713. end;
  714. findclose(dir);
  715. end;
  716. var
  717. hs : TPathStr;
  718. begin
  719. if PathExists(s,false) then
  720. begin
  721. { the path exists, now we clean only all the .o and .s files }
  722. DeleteFilesWithExt(target_info.objext);
  723. DeleteFilesWithExt(target_info.asmext);
  724. end
  725. else
  726. begin
  727. hs:=s;
  728. if hs[length(hs)] in ['/','\'] then
  729. delete(hs,length(hs),1);
  730. {$push} {$I-}
  731. mkdir(hs);
  732. {$pop}
  733. if ioresult<>0 then;
  734. end;
  735. end;
  736. const
  737. lastas : byte=255;
  738. var
  739. LastASBin : TCmdStr;
  740. Function TExternalAssembler.FindAssembler:string;
  741. var
  742. asfound : boolean;
  743. UtilExe : string;
  744. asmbin : TCmdStr;
  745. begin
  746. asfound:=false;
  747. asmbin:=asminfo^.asmbin;
  748. if (af_llvm in asminfo^.flags) then
  749. asmbin:=asmbin+llvmutilssuffix;
  750. if cs_assemble_on_target in current_settings.globalswitches then
  751. begin
  752. { If assembling on target, don't add any path PM }
  753. FindAssembler:=utilsprefix+ChangeFileExt(asmbin,target_info.exeext);
  754. exit;
  755. end
  756. else
  757. UtilExe:=utilsprefix+ChangeFileExt(asmbin,source_info.exeext);
  758. if lastas<>ord(asminfo^.id) then
  759. begin
  760. lastas:=ord(asminfo^.id);
  761. { is an assembler passed ? }
  762. if utilsdirectory<>'' then
  763. asfound:=FindFile(UtilExe,utilsdirectory,false,LastASBin);
  764. if not AsFound then
  765. asfound:=FindExe(UtilExe,false,LastASBin);
  766. if (not asfound) and not(cs_asm_extern in current_settings.globalswitches) then
  767. begin
  768. Message1(exec_e_assembler_not_found,LastASBin);
  769. current_settings.globalswitches:=current_settings.globalswitches+[cs_asm_extern];
  770. end;
  771. if asfound then
  772. Message1(exec_t_using_assembler,LastASBin);
  773. end;
  774. FindAssembler:=LastASBin;
  775. end;
  776. Function TExternalAssembler.CallAssembler(const command:string; const para:TCmdStr):Boolean;
  777. var
  778. DosExitCode : Integer;
  779. begin
  780. result:=true;
  781. if (cs_asm_extern in current_settings.globalswitches) then
  782. begin
  783. if SmartAsm then
  784. AsmRes.AddAsmCommand(command,para,Name+'('+TosTr(SmartFilesCount)+')')
  785. else
  786. AsmRes.AddAsmCommand(command,para,name);
  787. exit;
  788. end;
  789. try
  790. FlushOutput;
  791. DosExitCode:=RequotedExecuteProcess(command,para);
  792. if DosExitCode<>0
  793. then begin
  794. Message1(exec_e_error_while_assembling,tostr(dosexitcode));
  795. result:=false;
  796. end;
  797. except on E:EOSError do
  798. begin
  799. Message1(exec_e_cant_call_assembler,tostr(E.ErrorCode));
  800. current_settings.globalswitches:=current_settings.globalswitches+[cs_asm_extern];
  801. result:=false;
  802. end;
  803. end;
  804. end;
  805. Function TExternalAssembler.DoAssemble:boolean;
  806. begin
  807. result:=true;
  808. if DoPipe then
  809. exit;
  810. if not(cs_asm_extern in current_settings.globalswitches) then
  811. begin
  812. if SmartAsm then
  813. begin
  814. if (SmartFilesCount<=1) then
  815. Message1(exec_i_assembling_smart,name);
  816. end
  817. else
  818. Message1(exec_i_assembling,name);
  819. end;
  820. repeat
  821. result:=CallAssembler(FindAssembler,MakeCmdLine)
  822. until not(result) or not RerunAssembler;
  823. if result then
  824. writer.RemoveAsm
  825. else
  826. GenerateError;
  827. end;
  828. function TExternalAssembler.MakeCmdLine: TCmdStr;
  829. function section_high_bound:longint;
  830. var
  831. alt : tasmlisttype;
  832. begin
  833. result:=0;
  834. for alt:=low(tasmlisttype) to high(tasmlisttype) do
  835. result:=result+current_asmdata.asmlists[alt].section_count;
  836. end;
  837. const
  838. min_big_obj_section_count = $7fff;
  839. begin
  840. result:=asminfo^.asmcmd;
  841. if af_llvm in target_asm.flags then
  842. Replace(result,'$TRIPLET',targettriplet(triplet_llvm))
  843. {$ifdef arm}
  844. else if (target_info.system=system_arm_ios) then
  845. Replace(result,'$ARCH',lower(cputypestr[current_settings.cputype]))
  846. {$endif arm}
  847. ;
  848. if (cs_assemble_on_target in current_settings.globalswitches) then
  849. begin
  850. Replace(result,'$ASM',maybequoted(ScriptFixFileName(AsmFileName)));
  851. Replace(result,'$OBJ',maybequoted(ScriptFixFileName(ObjFileName)));
  852. end
  853. else
  854. begin
  855. {$ifdef hasunix}
  856. if DoPipe then
  857. if not(asminfo^.id in [as_clang_gas,as_clang_asdarwin,as_clang_llvm,as_clang_llvm_darwin]) then
  858. Replace(result,'$ASM','')
  859. else
  860. Replace(result,'$ASM','-')
  861. else
  862. {$endif}
  863. Replace(result,'$ASM',maybequoted(AsmFileName));
  864. Replace(result,'$OBJ',maybequoted(ObjFileName));
  865. end;
  866. if (cs_create_pic in current_settings.moduleswitches) then
  867. Replace(result,'$PIC','-KPIC')
  868. else
  869. Replace(result,'$PIC','');
  870. if (cs_asm_source in current_settings.globalswitches) then
  871. Replace(result,'$NOWARN','')
  872. else
  873. Replace(result,'$NOWARN','-W');
  874. if target_info.endian=endian_little then
  875. Replace(result,'$ENDIAN','-mlittle')
  876. else
  877. Replace(result,'$ENDIAN','-mbig');
  878. { as we don't keep track of the amount of sections we created we simply
  879. enable Big Obj COFF files always for targets that need them }
  880. if (cs_asm_pre_binutils_2_25 in current_settings.globalswitches) or
  881. not (target_info.system in systems_all_windows+systems_nativent-[system_i8086_win16]) or
  882. (section_high_bound<min_big_obj_section_count) then
  883. Replace(result,'$BIGOBJ','')
  884. else
  885. Replace(result,'$BIGOBJ','-mbig-obj');
  886. Replace(result,'$EXTRAOPT',asmextraopt);
  887. end;
  888. function TExternalAssembler.RerunAssembler: boolean;
  889. begin
  890. result:=false;
  891. end;
  892. procedure TExternalAssembler.ResetSourceLines;
  893. procedure DoReset(f:tinputfile);
  894. var
  895. i : longint;
  896. begin
  897. if not assigned(f) then
  898. exit;
  899. for i:=0 to f.maxlinebuf-1 do
  900. if f.linebuf^[i]<0 then
  901. f.linebuf^[i]:=-f.linebuf^[i]-1;
  902. end;
  903. begin
  904. DoReset(infile);
  905. DoReset(lastinfile);
  906. end;
  907. procedure TExternalAssembler.WriteSourceLine(hp: tailineinfo);
  908. var
  909. module : tmodule;
  910. begin
  911. { load infile }
  912. if (lastfileinfo.moduleindex<>hp.fileinfo.moduleindex) or
  913. (lastfileinfo.fileindex<>hp.fileinfo.fileindex) then
  914. begin
  915. { in case of a generic the module can be different }
  916. if current_module.unit_index=hp.fileinfo.moduleindex then
  917. module:=current_module
  918. else
  919. module:=get_module(hp.fileinfo.moduleindex);
  920. { during the compilation of the system unit there are cases when
  921. the fileinfo contains just zeros => invalid }
  922. if assigned(module) then
  923. infile:=module.sourcefiles.get_file(hp.fileinfo.fileindex)
  924. else
  925. infile:=nil;
  926. if assigned(infile) then
  927. begin
  928. { open only if needed !! }
  929. if (cs_asm_source in current_settings.globalswitches) then
  930. infile.open;
  931. end;
  932. { avoid unnecessary reopens of the same file !! }
  933. lastfileinfo.fileindex:=hp.fileinfo.fileindex;
  934. lastfileinfo.moduleindex:=hp.fileinfo.moduleindex;
  935. { be sure to change line !! }
  936. lastfileinfo.line:=-1;
  937. end;
  938. { write source }
  939. if (cs_asm_source in current_settings.globalswitches) and
  940. assigned(infile) then
  941. begin
  942. if (infile<>lastinfile) then
  943. begin
  944. writer.AsmWriteLn(asminfo^.comment+'['+infile.name+']');
  945. if assigned(lastinfile) then
  946. lastinfile.close;
  947. end;
  948. if (hp.fileinfo.line<>lastfileinfo.line) and
  949. (hp.fileinfo.line<infile.maxlinebuf) then
  950. begin
  951. if (hp.fileinfo.line<>0) and
  952. (infile.linebuf^[hp.fileinfo.line]>=0) then
  953. writer.AsmWriteLn(asminfo^.comment+'['+tostr(hp.fileinfo.line)+'] '+
  954. fixline(infile.GetLineStr(hp.fileinfo.line)));
  955. { set it to a negative value !
  956. to make that is has been read already !! PM }
  957. if (infile.linebuf^[hp.fileinfo.line]>=0) then
  958. infile.linebuf^[hp.fileinfo.line]:=-infile.linebuf^[hp.fileinfo.line]-1;
  959. end;
  960. end;
  961. lastfileinfo:=hp.fileinfo;
  962. lastinfile:=infile;
  963. end;
  964. procedure TExternalAssembler.WriteTempalloc(hp: tai_tempalloc);
  965. begin
  966. {$ifdef EXTDEBUG}
  967. if assigned(hp.problem) then
  968. writer.AsmWriteLn(asminfo^.comment+'Temp '+tostr(hp.temppos)+','+
  969. tostr(hp.tempsize)+' '+hp.problem^)
  970. else
  971. {$endif EXTDEBUG}
  972. writer.AsmWriteLn(asminfo^.comment+'Temp '+tostr(hp.temppos)+','+
  973. tostr(hp.tempsize)+' '+tempallocstr[hp.allocation]);
  974. end;
  975. procedure TExternalAssembler.WriteRealConstAsBytes(hp: tai_realconst; const dbdir: string; do_line: boolean);
  976. var
  977. pdata: pbyte;
  978. index, step, swapmask, count: longint;
  979. ssingle: single;
  980. ddouble: double;
  981. ccomp: comp;
  982. {$if defined(cpuextended) and defined(FPC_HAS_TYPE_EXTENDED)}
  983. eextended: extended;
  984. {$else}
  985. {$ifdef FPC_SOFT_FPUX80}
  986. f32 : float32;
  987. f64 : float64;
  988. eextended: floatx80;
  989. {$endif}
  990. {$endif cpuextended}
  991. begin
  992. if do_line then
  993. begin
  994. case tai_realconst(hp).realtyp of
  995. aitrealconst_s32bit:
  996. writer.AsmWriteLn(asminfo^.comment+'value: '+single2str(tai_realconst(hp).value.s32val));
  997. aitrealconst_s64bit:
  998. writer.AsmWriteLn(asminfo^.comment+'value: '+double2str(tai_realconst(hp).value.s64val));
  999. {$if defined(cpuextended) and defined(FPC_HAS_TYPE_EXTENDED)}
  1000. { can't write full 80 bit floating point constants yet on non-x86 }
  1001. aitrealconst_s80bit:
  1002. writer.AsmWriteLn(asminfo^.comment+'value: '+extended2str(tai_realconst(hp).value.s80val));
  1003. {$else}
  1004. {$ifdef FPC_SOFT_FPUX80}
  1005. {$push}{$warn 6018 off} { Unreachable code due to compile time evaluation }
  1006. aitrealconst_s80bit:
  1007. begin
  1008. if sizeof(tai_realconst(hp).value.s80val) = sizeof(double) then
  1009. writer.AsmWriteLn(asminfo^.comment+'value: '+double2str(tai_realconst(hp).value.s80val))
  1010. else if sizeof(tai_realconst(hp).value.s80val) = sizeof(single) then
  1011. writer.AsmWriteLn(asminfo^.comment+'value: '+single2str(tai_realconst(hp).value.s80val))
  1012. else
  1013. internalerror(2017091901);
  1014. end;
  1015. {$pop}
  1016. {$endif}
  1017. {$endif cpuextended}
  1018. aitrealconst_s64comp:
  1019. writer.AsmWriteLn(asminfo^.comment+'value: '+extended2str(tai_realconst(hp).value.s64compval));
  1020. else
  1021. internalerror(2014050604);
  1022. end;
  1023. end;
  1024. writer.AsmWrite(dbdir);
  1025. { generic float writing code: get start address of value, then write
  1026. byte by byte. Can't use fields directly, because e.g ts64comp is
  1027. defined as extended on x86 }
  1028. case tai_realconst(hp).realtyp of
  1029. aitrealconst_s32bit:
  1030. begin
  1031. ssingle:=single(tai_realconst(hp).value.s32val);
  1032. pdata:=@ssingle;
  1033. end;
  1034. aitrealconst_s64bit:
  1035. begin
  1036. ddouble:=double(tai_realconst(hp).value.s64val);
  1037. pdata:=@ddouble;
  1038. end;
  1039. {$if defined(cpuextended) and defined(FPC_HAS_TYPE_EXTENDED)}
  1040. { can't write full 80 bit floating point constants yet on non-x86 }
  1041. aitrealconst_s80bit:
  1042. begin
  1043. eextended:=extended(tai_realconst(hp).value.s80val);
  1044. pdata:=@eextended;
  1045. end;
  1046. {$else}
  1047. {$ifdef FPC_SOFT_FPUX80}
  1048. {$push}{$warn 6018 off} { Unreachable code due to compile time evaluation }
  1049. aitrealconst_s80bit:
  1050. begin
  1051. if sizeof(tai_realconst(hp).value.s80val) = sizeof(double) then
  1052. begin
  1053. f64:=float64(double(tai_realconst(hp).value.s80val));
  1054. if float64_is_signaling_nan(f64)<>0 then
  1055. begin
  1056. f64.low := 0;
  1057. f64.high := longword($fff80000);
  1058. end;
  1059. eextended:=float64_to_floatx80(f64);
  1060. end
  1061. else if sizeof(tai_realconst(hp).value.s80val) = sizeof(single) then
  1062. begin
  1063. f32:=float32(single(tai_realconst(hp).value.s80val));
  1064. if float32_is_signaling_nan(f32)<>0 then
  1065. begin
  1066. f32 := longword($ffc00000);
  1067. end;
  1068. eextended:=float32_to_floatx80(f32);
  1069. end
  1070. else
  1071. internalerror(2017091902);
  1072. pdata:=@eextended;
  1073. end;
  1074. {$pop}
  1075. {$endif}
  1076. {$endif cpuextended}
  1077. aitrealconst_s64comp:
  1078. begin
  1079. ccomp:=comp(tai_realconst(hp).value.s64compval);
  1080. pdata:=@ccomp;
  1081. end;
  1082. else
  1083. internalerror(2014051001);
  1084. end;
  1085. count:=tai_realconst(hp).datasize;
  1086. { write bytes in inverse order if source and target endianess don't
  1087. match }
  1088. if source_info.endian<>target_info.endian then
  1089. begin
  1090. { go from back to front }
  1091. index:=count-1;
  1092. step:=-1;
  1093. end
  1094. else
  1095. begin
  1096. index:=0;
  1097. step:=1;
  1098. end;
  1099. {$ifdef ARM}
  1100. { ARM-specific: low and high dwords of a double may be swapped }
  1101. if tai_realconst(hp).formatoptions=fo_hiloswapped then
  1102. begin
  1103. { only supported for double }
  1104. if tai_realconst(hp).datasize<>8 then
  1105. internalerror(2014050605);
  1106. { switch bit of the index so that the words are written in
  1107. the opposite order }
  1108. swapmask:=4;
  1109. end
  1110. else
  1111. {$endif ARM}
  1112. swapmask:=0;
  1113. repeat
  1114. writer.AsmWrite(tostr(pdata[index xor swapmask]));
  1115. inc(index,step);
  1116. dec(count);
  1117. if count<>0 then
  1118. writer.AsmWrite(',');
  1119. until count=0;
  1120. { padding }
  1121. for count:=tai_realconst(hp).datasize+1 to tai_realconst(hp).savesize do
  1122. writer.AsmWrite(',0');
  1123. writer.AsmLn;
  1124. end;
  1125. function TExternalAssembler.WriteComments(var hp: tai): boolean;
  1126. begin
  1127. result:=true;
  1128. case hp.typ of
  1129. ait_comment :
  1130. Begin
  1131. writer.AsmWrite(asminfo^.comment);
  1132. writer.AsmWritePChar(tai_comment(hp).str);
  1133. writer.AsmLn;
  1134. End;
  1135. ait_regalloc :
  1136. begin
  1137. if (cs_asm_regalloc in current_settings.globalswitches) then
  1138. begin
  1139. writer.AsmWrite(#9+asminfo^.comment+'Register ');
  1140. repeat
  1141. writer.AsmWrite(std_regname(Tai_regalloc(hp).reg));
  1142. if (hp.next=nil) or
  1143. (tai(hp.next).typ<>ait_regalloc) or
  1144. (tai_regalloc(hp.next).ratype<>tai_regalloc(hp).ratype) then
  1145. break;
  1146. hp:=tai(hp.next);
  1147. writer.AsmWrite(',');
  1148. until false;
  1149. writer.AsmWrite(' ');
  1150. writer.AsmWriteLn(regallocstr[tai_regalloc(hp).ratype]);
  1151. end;
  1152. end;
  1153. ait_tempalloc :
  1154. begin
  1155. if (cs_asm_tempalloc in current_settings.globalswitches) then
  1156. WriteTempalloc(tai_tempalloc(hp));
  1157. end;
  1158. ait_varloc:
  1159. begin
  1160. { ait_varloc is present here only when register allocation is not done ( -sr option ) }
  1161. if tai_varloc(hp).newlocationhi<>NR_NO then
  1162. writer.AsmWriteLn(asminfo^.comment+'Var '+tai_varloc(hp).varsym.realname+' located in register '+
  1163. std_regname(tai_varloc(hp).newlocationhi)+':'+std_regname(tai_varloc(hp).newlocation))
  1164. else
  1165. writer.AsmWriteLn(asminfo^.comment+'Var '+tai_varloc(hp).varsym.realname+' located in register '+
  1166. std_regname(tai_varloc(hp).newlocation));
  1167. end;
  1168. else
  1169. result:=false;
  1170. end;
  1171. end;
  1172. procedure TExternalAssembler.WriteTree(p:TAsmList);
  1173. begin
  1174. end;
  1175. procedure TExternalAssembler.WriteAsmList;
  1176. begin
  1177. end;
  1178. procedure TExternalAssembler.MakeObject;
  1179. begin
  1180. writer.AsmCreate(cut_normal);
  1181. FillChar(lastfileinfo, sizeof(lastfileinfo), 0);
  1182. lastfileinfo.line := -1;
  1183. lastinfile := nil;
  1184. lastsectype := sec_none;
  1185. WriteAsmList;
  1186. writer.AsmClose;
  1187. if not(writer.ioerror) then
  1188. DoAssemble;
  1189. end;
  1190. destructor TExternalAssembler.Destroy;
  1191. begin
  1192. if ffreewriter then
  1193. writer.Free;
  1194. inherited;
  1195. end;
  1196. {*****************************************************************************
  1197. TInternalAssembler
  1198. *****************************************************************************}
  1199. constructor TInternalAssembler.Create(info: pasminfo; smart: boolean);
  1200. begin
  1201. inherited;
  1202. ObjOutput:=nil;
  1203. ObjData:=nil;
  1204. SmartAsm:=smart;
  1205. {$ifdef ARM}
  1206. Code16:=current_settings.instructionset=is_thumb;
  1207. {$endif ARM}
  1208. end;
  1209. destructor TInternalAssembler.destroy;
  1210. begin
  1211. if assigned(ObjData) then
  1212. ObjData.free;
  1213. if assigned(ObjOutput) then
  1214. ObjOutput.free;
  1215. end;
  1216. procedure TInternalAssembler.WriteStab(p:pchar);
  1217. function consumecomma(var p:pchar):boolean;
  1218. begin
  1219. while (p^=' ') do
  1220. inc(p);
  1221. result:=(p^=',');
  1222. inc(p);
  1223. end;
  1224. function consumenumber(var p:pchar;out value:longint):boolean;
  1225. var
  1226. hs : string;
  1227. len,
  1228. code : integer;
  1229. begin
  1230. value:=0;
  1231. while (p^=' ') do
  1232. inc(p);
  1233. len:=0;
  1234. while (p^ in ['0'..'9']) do
  1235. begin
  1236. inc(len);
  1237. hs[len]:=p^;
  1238. inc(p);
  1239. end;
  1240. if len>0 then
  1241. begin
  1242. hs[0]:=chr(len);
  1243. val(hs,value,code);
  1244. end
  1245. else
  1246. code:=-1;
  1247. result:=(code=0);
  1248. end;
  1249. function consumeoffset(var p:pchar;out relocsym:tobjsymbol;out value:longint):boolean;
  1250. var
  1251. hs : string;
  1252. len,
  1253. code : integer;
  1254. pstart : pchar;
  1255. sym : tobjsymbol;
  1256. exprvalue : longint;
  1257. gotmin,
  1258. have_first_symbol,
  1259. have_second_symbol,
  1260. dosub : boolean;
  1261. begin
  1262. result:=false;
  1263. value:=0;
  1264. relocsym:=nil;
  1265. gotmin:=false;
  1266. have_first_symbol:=false;
  1267. have_second_symbol:=false;
  1268. repeat
  1269. dosub:=false;
  1270. exprvalue:=0;
  1271. if gotmin then
  1272. begin
  1273. dosub:=true;
  1274. gotmin:=false;
  1275. end;
  1276. while (p^=' ') do
  1277. inc(p);
  1278. case p^ of
  1279. #0 :
  1280. break;
  1281. ' ' :
  1282. inc(p);
  1283. '0'..'9' :
  1284. begin
  1285. len:=0;
  1286. while (p^ in ['0'..'9']) do
  1287. begin
  1288. inc(len);
  1289. hs[len]:=p^;
  1290. inc(p);
  1291. end;
  1292. hs[0]:=chr(len);
  1293. val(hs,exprvalue,code);
  1294. if code<>0 then
  1295. internalerror(200702251);
  1296. end;
  1297. '.','_',
  1298. 'A'..'Z',
  1299. 'a'..'z' :
  1300. begin
  1301. pstart:=p;
  1302. while not(p^ in [#0,' ','-','+']) do
  1303. inc(p);
  1304. len:=p-pstart;
  1305. if len>255 then
  1306. internalerror(200509187);
  1307. hs[0]:=chr(len);
  1308. move(pstart^,hs[1],len);
  1309. sym:=objdata.symbolref(hs);
  1310. { Second symbol? }
  1311. if assigned(relocsym) then
  1312. begin
  1313. if have_second_symbol then
  1314. internalerror(2007032201);
  1315. have_second_symbol:=true;
  1316. if not have_first_symbol then
  1317. internalerror(2007032202);
  1318. { second symbol should substracted to first }
  1319. if not dosub then
  1320. internalerror(2007032203);
  1321. if (relocsym.objsection<>sym.objsection) then
  1322. internalerror(2005091810);
  1323. exprvalue:=relocsym.address-sym.address;
  1324. relocsym:=nil;
  1325. dosub:=false;
  1326. end
  1327. else
  1328. begin
  1329. relocsym:=sym;
  1330. if assigned(sym.objsection) then
  1331. begin
  1332. { first symbol should be + }
  1333. if not have_first_symbol and dosub then
  1334. internalerror(2007032204);
  1335. have_first_symbol:=true;
  1336. end;
  1337. end;
  1338. end;
  1339. '+' :
  1340. begin
  1341. { nothing, by default addition is done }
  1342. inc(p);
  1343. end;
  1344. '-' :
  1345. begin
  1346. gotmin:=true;
  1347. inc(p);
  1348. end;
  1349. else
  1350. internalerror(200509189);
  1351. end;
  1352. if dosub then
  1353. dec(value,exprvalue)
  1354. else
  1355. inc(value,exprvalue);
  1356. until false;
  1357. result:=true;
  1358. end;
  1359. var
  1360. stabstrlen,
  1361. ofs,
  1362. nline,
  1363. nidx,
  1364. nother,
  1365. i : longint;
  1366. stab : TObjStabEntry;
  1367. relocsym : TObjSymbol;
  1368. pstr,
  1369. pcurr,
  1370. pendquote : pchar;
  1371. oldsec : TObjSection;
  1372. begin
  1373. pcurr:=nil;
  1374. pstr:=nil;
  1375. pendquote:=nil;
  1376. relocsym:=nil;
  1377. ofs:=0;
  1378. { Parse string part }
  1379. if (p[0]='"') then
  1380. begin
  1381. pstr:=@p[1];
  1382. { Ignore \" inside the string }
  1383. i:=1;
  1384. while not((p[i]='"') and (p[i-1]<>'\')) and
  1385. (p[i]<>#0) do
  1386. inc(i);
  1387. pendquote:=@p[i];
  1388. pendquote^:=#0;
  1389. pcurr:=@p[i+1];
  1390. if not consumecomma(pcurr) then
  1391. internalerror(200509181);
  1392. end
  1393. else
  1394. pcurr:=p;
  1395. { When in pass 1 then only alloc and leave }
  1396. if ObjData.currpass=1 then
  1397. begin
  1398. ObjData.StabsSec.Alloc(sizeof(TObjStabEntry));
  1399. if assigned(pstr) and (pstr[0]<>#0) then
  1400. ObjData.StabStrSec.Alloc(strlen(pstr)+1);
  1401. end
  1402. else
  1403. begin
  1404. { Stabs format: nidx,nother,nline[,offset] }
  1405. if not consumenumber(pcurr,nidx) then
  1406. internalerror(200509182);
  1407. if not consumecomma(pcurr) then
  1408. internalerror(200509183);
  1409. if not consumenumber(pcurr,nother) then
  1410. internalerror(200509184);
  1411. if not consumecomma(pcurr) then
  1412. internalerror(200509185);
  1413. if not consumenumber(pcurr,nline) then
  1414. internalerror(200509186);
  1415. if consumecomma(pcurr) then
  1416. consumeoffset(pcurr,relocsym,ofs);
  1417. { Generate stab entry }
  1418. if assigned(pstr) and (pstr[0]<>#0) then
  1419. begin
  1420. stabstrlen:=strlen(pstr);
  1421. {$ifdef optimizestabs}
  1422. StabStrEntry:=nil;
  1423. if (nidx=N_SourceFile) or (nidx=N_IncludeFile) then
  1424. begin
  1425. hs:=strpas(pstr);
  1426. StabstrEntry:=StabStrDict.Find(hs);
  1427. if not assigned(StabstrEntry) then
  1428. begin
  1429. StabstrEntry:=TStabStrEntry.Create(hs);
  1430. StabstrEntry:=StabStrSec.Size;
  1431. StabStrDict.Insert(StabstrEntry);
  1432. { generate new stab }
  1433. StabstrEntry:=nil;
  1434. end;
  1435. end;
  1436. if assigned(StabstrEntry) then
  1437. stab.strpos:=StabstrEntry.strpos
  1438. else
  1439. {$endif optimizestabs}
  1440. begin
  1441. stab.strpos:=ObjData.StabStrSec.Size;
  1442. ObjData.StabStrSec.write(pstr^,stabstrlen+1);
  1443. end;
  1444. end
  1445. else
  1446. stab.strpos:=0;
  1447. stab.ntype:=byte(nidx);
  1448. stab.ndesc:=word(nline);
  1449. stab.nother:=byte(nother);
  1450. stab.nvalue:=ofs;
  1451. { Write the stab first without the value field. Then
  1452. write a the value field with relocation }
  1453. oldsec:=ObjData.CurrObjSec;
  1454. ObjData.SetSection(ObjData.StabsSec);
  1455. MaybeSwapStab(stab);
  1456. ObjData.Writebytes(stab,sizeof(TObjStabEntry)-4);
  1457. ObjData.Writereloc(stab.nvalue,4,relocsym,RELOC_ABSOLUTE32);
  1458. ObjData.setsection(oldsec);
  1459. end;
  1460. if assigned(pendquote) then
  1461. pendquote^:='"';
  1462. end;
  1463. function TInternalAssembler.MaybeNextList(var hp:Tai):boolean;
  1464. begin
  1465. { maybe end of list }
  1466. while not assigned(hp) do
  1467. begin
  1468. if currlistidx<lists then
  1469. begin
  1470. inc(currlistidx);
  1471. currlist:=list[currlistidx];
  1472. hp:=Tai(currList.first);
  1473. end
  1474. else
  1475. begin
  1476. MaybeNextList:=false;
  1477. exit;
  1478. end;
  1479. end;
  1480. MaybeNextList:=true;
  1481. end;
  1482. function TInternalAssembler.SetIndirectToSymbol(hp: Tai; const indirectname: string): Boolean;
  1483. var
  1484. objsym : TObjSymbol;
  1485. indsym : TObjSymbol;
  1486. begin
  1487. Result:=
  1488. Assigned(hp) and
  1489. (hp.typ=ait_symbol);
  1490. if not Result then
  1491. Exit;
  1492. objsym:=Objdata.SymbolRef(tai_symbol(hp).sym);
  1493. objsym.size:=0;
  1494. indsym := TObjSymbol(ObjData.ObjSymbolList.Find(indirectname));
  1495. if not Assigned(indsym) then
  1496. begin
  1497. { it's possible that indirect symbol is not present in the list,
  1498. so we must create it as undefined }
  1499. indsym:=ObjData.CObjSymbol.Create(ObjData.ObjSymbolList, indirectname);
  1500. indsym.typ:=AT_NONE;
  1501. indsym.bind:=AB_NONE;
  1502. end;
  1503. objsym.indsymbol:=indsym;
  1504. Result:=true;
  1505. end;
  1506. function TInternalAssembler.TreePass0(hp:Tai):Tai;
  1507. var
  1508. objsym,
  1509. objsymend : TObjSymbol;
  1510. cpu: tcputype;
  1511. eabi_section, TmpSection: TObjSection;
  1512. begin
  1513. while assigned(hp) do
  1514. begin
  1515. {$ifdef DEBUG}
  1516. if not(hp.typ in SkipLineInfo) then
  1517. current_filepos:=tailineinfo(hp).fileinfo;
  1518. {$endif DEBUG}
  1519. case hp.typ of
  1520. ait_align :
  1521. begin
  1522. if tai_align_abstract(hp).aligntype>1 then
  1523. begin
  1524. { always use the maximum fillsize in this pass to avoid possible
  1525. short jumps to become out of range }
  1526. Tai_align_abstract(hp).fillsize:=Tai_align_abstract(hp).aligntype;
  1527. ObjData.alloc(Tai_align_abstract(hp).fillsize);
  1528. { may need to increase alignment of section }
  1529. if tai_align_abstract(hp).aligntype>ObjData.CurrObjSec.secalign then
  1530. ObjData.CurrObjSec.secalign:=tai_align_abstract(hp).aligntype;
  1531. end
  1532. else
  1533. Tai_align_abstract(hp).fillsize:=0;
  1534. end;
  1535. ait_datablock :
  1536. begin
  1537. {$ifdef USE_COMM_IN_BSS}
  1538. if writingpackages and
  1539. Tai_datablock(hp).is_global then
  1540. ObjData.SymbolDefine(Tai_datablock(hp).sym)
  1541. else
  1542. {$endif USE_COMM_IN_BSS}
  1543. begin
  1544. ObjData.allocalign(used_align(size_2_align(Tai_datablock(hp).size),0,ObjData.CurrObjSec.secalign));
  1545. ObjData.SymbolDefine(Tai_datablock(hp).sym);
  1546. ObjData.alloc(Tai_datablock(hp).size);
  1547. end;
  1548. end;
  1549. ait_realconst:
  1550. ObjData.alloc(tai_realconst(hp).savesize);
  1551. ait_const:
  1552. begin
  1553. { if symbols are provided we can calculate the value for relative symbols.
  1554. This is required for length calculation of leb128 constants }
  1555. if assigned(tai_const(hp).sym) then
  1556. begin
  1557. objsym:=Objdata.SymbolRef(tai_const(hp).sym);
  1558. { objsym already defined and there is endsym? }
  1559. if assigned(objsym.objsection) and assigned(tai_const(hp).endsym) then
  1560. begin
  1561. objsymend:=Objdata.SymbolRef(tai_const(hp).endsym);
  1562. { objsymend already defined? }
  1563. if assigned(objsymend.objsection) then
  1564. begin
  1565. if objsymend.objsection<>objsym.objsection then
  1566. begin
  1567. { leb128 relative constants are not relocatable, but other types are,
  1568. given that objsym belongs to the current section. }
  1569. if (Tai_const(hp).consttype in [aitconst_uleb128bit,aitconst_sleb128bit]) or
  1570. (objsym.objsection<>ObjData.CurrObjSec) then
  1571. InternalError(200404124);
  1572. end
  1573. {$push} {$R-}{$Q-}
  1574. else
  1575. Tai_const(hp).value:=objsymend.address-objsym.address+Tai_const(hp).symofs;
  1576. end;
  1577. {$pop}
  1578. end;
  1579. end;
  1580. ObjData.alloc(tai_const(hp).size);
  1581. end;
  1582. ait_directive:
  1583. begin
  1584. case tai_directive(hp).directive of
  1585. asd_indirect_symbol:
  1586. { handled in TreePass1 }
  1587. ;
  1588. asd_lazy_reference:
  1589. begin
  1590. if tai_directive(hp).name='' then
  1591. Internalerror(2009112101);
  1592. objsym:=ObjData.symbolref(tai_directive(hp).name);
  1593. objsym.bind:=AB_LAZY;
  1594. end;
  1595. asd_reference:
  1596. { ignore for now, but should be added}
  1597. ;
  1598. asd_cpu:
  1599. begin
  1600. ObjData.CPUType:=cpu_none;
  1601. for cpu:=low(tcputype) to high(tcputype) do
  1602. if cputypestr[cpu]=tai_directive(hp).name then
  1603. begin
  1604. ObjData.CPUType:=cpu;
  1605. break;
  1606. end;
  1607. end;
  1608. asd_weak_definition:
  1609. begin
  1610. if tai_directive(hp).name='' then
  1611. Internalerror(2022040901);
  1612. objsym:=ObjData.symbolref(tai_directive(hp).name);
  1613. objsym.bind:=AB_WEAK;
  1614. end;
  1615. {$ifdef OMFOBJSUPPORT}
  1616. asd_omf_linnum_line:
  1617. { ignore for now, but should be added}
  1618. ;
  1619. {$endif OMFOBJSUPPORT}
  1620. {$ifdef ARM}
  1621. asd_thumb_func:
  1622. ObjData.ThumbFunc:=true;
  1623. asd_force_thumb:
  1624. begin
  1625. ObjData.ThumbFunc:=true;
  1626. Code16:=true;
  1627. end;
  1628. asd_code:
  1629. begin
  1630. { ai_directive(hp).name can be only 16 or 32, this is checked by the reader }
  1631. ObjData.ThumbFunc:=tai_directive(hp).name='16';
  1632. Code16:=tai_directive(hp).name='16';
  1633. end
  1634. {$endif ARM}
  1635. {$ifdef RISCV}
  1636. asd_option:
  1637. internalerror(2019031701);
  1638. {$endif RISCV}
  1639. else
  1640. internalerror(2010011101);
  1641. end;
  1642. end;
  1643. ait_section:
  1644. begin
  1645. if Tai_section(hp).sectype=sec_user then
  1646. ObjData.CreateSection(Tai_section(hp).sectype,Tai_section(hp).secflags,Tai_section(hp).secprogbits,Tai_section(hp).name^,Tai_section(hp).secorder)
  1647. else
  1648. ObjData.CreateSection(Tai_section(hp).sectype,Tai_section(hp).name^,Tai_section(hp).secorder);
  1649. Tai_section(hp).sec:=ObjData.CurrObjSec;
  1650. end;
  1651. ait_symbol :
  1652. begin
  1653. { needs extra support in the internal assembler }
  1654. { the value is just ignored }
  1655. {if tai_symbol(hp).has_value then
  1656. internalerror(2009090804); ;}
  1657. ObjData.SymbolDefine(Tai_symbol(hp).sym);
  1658. end;
  1659. ait_symbolpair :
  1660. with tai_symbolpair(hp) do
  1661. ObjData.SymbolPairDefine(kind,sym^,value^);
  1662. ait_label :
  1663. ObjData.SymbolDefine(Tai_label(hp).labsym);
  1664. ait_string :
  1665. ObjData.alloc(Tai_string(hp).len);
  1666. ait_instruction :
  1667. begin
  1668. {$ifdef arm}
  1669. if code16 then
  1670. include(taicpu(hp).flags,cf_thumb)
  1671. else
  1672. exclude(taicpu(hp).flags,cf_thumb);
  1673. {$endif arm}
  1674. { reset instructions which could change in pass 2 }
  1675. Taicpu(hp).resetpass2;
  1676. ObjData.alloc(Taicpu(hp).Pass1(ObjData));
  1677. end;
  1678. ait_cutobject :
  1679. if SmartAsm then
  1680. break;
  1681. ait_eabi_attribute :
  1682. begin
  1683. eabi_section:=ObjData.findsection('.ARM.attributes');
  1684. if not(assigned(eabi_section)) then
  1685. begin
  1686. TmpSection:=ObjData.CurrObjSec;
  1687. ObjData.CreateSection(sec_arm_attribute,[],SPB_ARM_ATTRIBUTES,'',secorder_default);
  1688. eabi_section:=ObjData.CurrObjSec;
  1689. ObjData.setsection(TmpSection);
  1690. end;
  1691. if eabi_section.Size=0 then
  1692. eabi_section.alloc(16);
  1693. eabi_section.alloc(LengthUleb128(tai_eabi_attribute(hp).tag));
  1694. case tai_eabi_attribute(hp).eattr_typ of
  1695. eattrtype_dword:
  1696. eabi_section.alloc(LengthUleb128(tai_eabi_attribute(hp).value));
  1697. eattrtype_ntbs:
  1698. if assigned(tai_eabi_attribute(hp).valuestr) then
  1699. eabi_section.alloc(Length(tai_eabi_attribute(hp).valuestr^)+1)
  1700. else
  1701. eabi_section.alloc(1);
  1702. else
  1703. Internalerror(2019100701);
  1704. end;
  1705. end;
  1706. {$ifdef WASM}
  1707. ait_globaltype:
  1708. TWasmObjData(ObjData).DeclareGlobalType(tai_globaltype(hp));
  1709. ait_functype:
  1710. TWasmObjData(ObjData).DeclareFuncType(tai_functype(hp));
  1711. ait_tagtype:
  1712. TWasmObjData(ObjData).DeclareTagType(tai_tagtype(hp));
  1713. ait_export_name:
  1714. TWasmObjData(ObjData).DeclareExportName(tai_export_name(hp));
  1715. ait_import_module:
  1716. TWasmObjData(ObjData).DeclareImportModule(tai_import_module(hp));
  1717. ait_import_name:
  1718. TWasmObjData(ObjData).DeclareImportName(tai_import_name(hp));
  1719. ait_local:
  1720. TWasmObjData(ObjData).DeclareLocal(tai_local(hp));
  1721. {$endif WASM}
  1722. else
  1723. ;
  1724. end;
  1725. hp:=Tai(hp.next);
  1726. end;
  1727. TreePass0:=hp;
  1728. end;
  1729. function TInternalAssembler.TreePass1(hp:Tai):Tai;
  1730. var
  1731. objsym,
  1732. objsymend : TObjSymbol;
  1733. cpu: tcputype;
  1734. eabi_section: TObjSection;
  1735. begin
  1736. while assigned(hp) do
  1737. begin
  1738. {$ifdef DEBUG}
  1739. if not(hp.typ in SkipLineInfo) then
  1740. current_filepos:=tailineinfo(hp).fileinfo;
  1741. {$endif DEBUG}
  1742. case hp.typ of
  1743. ait_align :
  1744. begin
  1745. if tai_align_abstract(hp).aligntype>1 then
  1746. begin
  1747. { here we must determine the fillsize which is used in pass2 }
  1748. Tai_align_abstract(hp).fillsize:=align(ObjData.CurrObjSec.Size,Tai_align_abstract(hp).aligntype)-
  1749. ObjData.CurrObjSec.Size;
  1750. { maximum number of bytes for alignment exeeded? }
  1751. if (Tai_align_abstract(hp).aligntype<>Tai_align_abstract(hp).maxbytes) and
  1752. (Tai_align_abstract(hp).fillsize>Tai_align_abstract(hp).maxbytes) then
  1753. Tai_align_abstract(hp).fillsize:=align(ObjData.CurrObjSec.Size,Byte(Tai_align_abstract(hp).aligntype div 2))-
  1754. ObjData.CurrObjSec.Size;
  1755. ObjData.alloc(Tai_align_abstract(hp).fillsize);
  1756. end;
  1757. end;
  1758. ait_datablock :
  1759. begin
  1760. if (oso_data in ObjData.CurrObjSec.secoptions) and
  1761. not (oso_sparse_data in ObjData.CurrObjSec.secoptions) then
  1762. Message(asmw_e_alloc_data_only_in_bss);
  1763. {$ifdef USE_COMM_IN_BSS}
  1764. if writingpackages and
  1765. Tai_datablock(hp).is_global then
  1766. begin
  1767. objsym:=ObjData.SymbolDefine(Tai_datablock(hp).sym);
  1768. objsym.size:=Tai_datablock(hp).size;
  1769. objsym.bind:=AB_COMMON;
  1770. objsym.alignment:=needtowritealignmentalsoforELF;
  1771. end
  1772. else
  1773. {$endif USE_COMM_IN_BSS}
  1774. begin
  1775. ObjData.allocalign(used_align(size_2_align(Tai_datablock(hp).size),0,ObjData.CurrObjSec.secalign));
  1776. objsym:=ObjData.SymbolDefine(Tai_datablock(hp).sym);
  1777. objsym.size:=Tai_datablock(hp).size;
  1778. ObjData.alloc(Tai_datablock(hp).size);
  1779. end;
  1780. end;
  1781. ait_realconst:
  1782. ObjData.alloc(tai_realconst(hp).savesize);
  1783. ait_const:
  1784. begin
  1785. { Recalculate relative symbols }
  1786. if assigned(tai_const(hp).sym) and
  1787. assigned(tai_const(hp).endsym) then
  1788. begin
  1789. objsym:=Objdata.SymbolRef(tai_const(hp).sym);
  1790. objsymend:=Objdata.SymbolRef(tai_const(hp).endsym);
  1791. if Tai_const(hp).consttype in [aitconst_gottpoff,aitconst_tlsgd,aitconst_tlsdesc] then
  1792. begin
  1793. if objsymend.objsection<>ObjData.CurrObjSec then
  1794. Internalerror(2019092801);
  1795. Tai_const(hp).value:=objsymend.address-ObjData.CurrObjSec.Size+Tai_const(hp).symofs;
  1796. end
  1797. else if objsymend.objsection<>objsym.objsection then
  1798. begin
  1799. if (Tai_const(hp).consttype in [aitconst_uleb128bit,aitconst_sleb128bit]) or
  1800. (objsym.objsection<>ObjData.CurrObjSec) then
  1801. internalerror(200905042);
  1802. end
  1803. {$push} {$R-}{$Q-}
  1804. else
  1805. Tai_const(hp).value:=objsymend.address-objsym.address+Tai_const(hp).symofs;
  1806. end;
  1807. {$pop}
  1808. if (Tai_const(hp).consttype in [aitconst_uleb128bit,aitconst_sleb128bit]) then
  1809. Tai_const(hp).fixsize;
  1810. ObjData.alloc(tai_const(hp).size);
  1811. end;
  1812. ait_section:
  1813. begin
  1814. { use cached value }
  1815. ObjData.setsection(Tai_section(hp).sec);
  1816. end;
  1817. ait_stab :
  1818. begin
  1819. if assigned(Tai_stab(hp).str) then
  1820. WriteStab(Tai_stab(hp).str);
  1821. end;
  1822. ait_symbol :
  1823. ObjData.SymbolDefine(Tai_symbol(hp).sym);
  1824. ait_symbol_end :
  1825. begin
  1826. objsym:=ObjData.SymbolRef(Tai_symbol_end(hp).sym);
  1827. objsym.size:=ObjData.CurrObjSec.Size-objsym.offset;
  1828. end;
  1829. ait_symbolpair:
  1830. with tai_symbolpair(hp) do
  1831. ObjData.SymbolPairDefine(kind,sym^,value^);
  1832. ait_label :
  1833. ObjData.SymbolDefine(Tai_label(hp).labsym);
  1834. ait_string :
  1835. ObjData.alloc(Tai_string(hp).len);
  1836. ait_instruction :
  1837. ObjData.alloc(Taicpu(hp).Pass1(ObjData));
  1838. ait_cutobject :
  1839. if SmartAsm then
  1840. break;
  1841. ait_directive :
  1842. begin
  1843. case tai_directive(hp).directive of
  1844. asd_indirect_symbol:
  1845. if tai_directive(hp).name='' then
  1846. Internalerror(2009101103)
  1847. else if not SetIndirectToSymbol(Tai(hp.Previous), tai_directive(hp).name) then
  1848. Internalerror(2009101102);
  1849. asd_lazy_reference:
  1850. { handled in TreePass0 }
  1851. ;
  1852. asd_reference:
  1853. { ignore for now, but should be added}
  1854. ;
  1855. asd_thumb_func:
  1856. { ignore for now, but should be added}
  1857. ;
  1858. asd_force_thumb:
  1859. { ignore for now, but should be added}
  1860. ;
  1861. asd_code:
  1862. { ignore for now, but should be added}
  1863. ;
  1864. asd_option:
  1865. { ignore for now, but should be added}
  1866. ;
  1867. asd_weak_definition:
  1868. { ignore for now, but should be added}
  1869. ;
  1870. {$ifdef OMFOBJSUPPORT}
  1871. asd_omf_linnum_line:
  1872. { ignore for now, but should be added}
  1873. ;
  1874. {$endif OMFOBJSUPPORT}
  1875. asd_cpu:
  1876. begin
  1877. ObjData.CPUType:=cpu_none;
  1878. for cpu:=low(tcputype) to high(tcputype) do
  1879. if cputypestr[cpu]=tai_directive(hp).name then
  1880. begin
  1881. ObjData.CPUType:=cpu;
  1882. break;
  1883. end;
  1884. end;
  1885. else
  1886. internalerror(2010011102);
  1887. end;
  1888. end;
  1889. ait_eabi_attribute :
  1890. begin
  1891. eabi_section:=ObjData.findsection('.ARM.attributes');
  1892. if not(assigned(eabi_section)) then
  1893. Internalerror(2019100702);
  1894. if eabi_section.Size=0 then
  1895. eabi_section.alloc(16);
  1896. eabi_section.alloc(LengthUleb128(tai_eabi_attribute(hp).tag));
  1897. case tai_eabi_attribute(hp).eattr_typ of
  1898. eattrtype_dword:
  1899. eabi_section.alloc(LengthUleb128(tai_eabi_attribute(hp).value));
  1900. eattrtype_ntbs:
  1901. if assigned(tai_eabi_attribute(hp).valuestr) then
  1902. eabi_section.alloc(Length(tai_eabi_attribute(hp).valuestr^)+1)
  1903. else
  1904. eabi_section.alloc(1);
  1905. else
  1906. Internalerror(2019100703);
  1907. end;
  1908. end;
  1909. else
  1910. ;
  1911. end;
  1912. hp:=Tai(hp.next);
  1913. end;
  1914. TreePass1:=hp;
  1915. end;
  1916. function TInternalAssembler.TreePass2(hp:Tai):Tai;
  1917. var
  1918. fillbuffer : tfillbuffer;
  1919. leblen : byte;
  1920. lebbuf : array[0..63] of byte;
  1921. objsym,
  1922. ref,
  1923. objsymend : TObjSymbol;
  1924. zerobuf : array[0..63] of byte;
  1925. relative_reloc: boolean;
  1926. pdata : pointer;
  1927. ssingle : single;
  1928. ddouble : double;
  1929. {$if defined(cpuextended) and defined(FPC_HAS_TYPE_EXTENDED)}
  1930. eextended : extended;
  1931. {$else}
  1932. {$ifdef FPC_SOFT_FPUX80}
  1933. f32 : float32;
  1934. f64 : float64;
  1935. eextended : floatx80;
  1936. {$endif}
  1937. {$endif}
  1938. ccomp : comp;
  1939. tmp : word;
  1940. cpu: tcputype;
  1941. ddword : dword;
  1942. eabi_section: TObjSection;
  1943. s: String;
  1944. TmpDataPos: TObjSectionOfs;
  1945. begin
  1946. fillchar(zerobuf,sizeof(zerobuf),0);
  1947. fillchar(objsym,sizeof(objsym),0);
  1948. fillchar(objsymend,sizeof(objsymend),0);
  1949. { main loop }
  1950. while assigned(hp) do
  1951. begin
  1952. {$ifdef DEBUG}
  1953. if not(hp.typ in SkipLineInfo) then
  1954. current_filepos:=tailineinfo(hp).fileinfo;
  1955. {$endif DEBUG}
  1956. case hp.typ of
  1957. ait_align :
  1958. begin
  1959. if tai_align_abstract(hp).aligntype>ObjData.CurrObjSec.secalign then
  1960. InternalError(2012072301);
  1961. if oso_data in ObjData.CurrObjSec.secoptions then
  1962. ObjData.writebytes(Tai_align_abstract(hp).calculatefillbuf(fillbuffer,oso_executable in ObjData.CurrObjSec.secoptions)^,
  1963. Tai_align_abstract(hp).fillsize)
  1964. else
  1965. ObjData.alloc(Tai_align_abstract(hp).fillsize);
  1966. end;
  1967. ait_section :
  1968. begin
  1969. { use cached value }
  1970. ObjData.setsection(Tai_section(hp).sec);
  1971. end;
  1972. ait_symbol :
  1973. begin
  1974. ObjOutput.exportsymbol(ObjData.SymbolRef(Tai_symbol(hp).sym));
  1975. end;
  1976. ait_symbol_end :
  1977. begin
  1978. { recalculate size, as some preceding instructions
  1979. could have been changed to smaller size }
  1980. objsym:=ObjData.SymbolRef(Tai_symbol_end(hp).sym);
  1981. objsym.size:=ObjData.CurrObjSec.Size-objsym.offset;
  1982. end;
  1983. ait_datablock :
  1984. begin
  1985. ObjOutput.exportsymbol(ObjData.SymbolRef(Tai_datablock(hp).sym));
  1986. {$ifdef USE_COMM_IN_BSS}
  1987. if not(writingpackages and
  1988. Tai_datablock(hp).is_global) then
  1989. {$endif USE_COMM_IN_BSS}
  1990. begin
  1991. ObjData.allocalign(used_align(size_2_align(Tai_datablock(hp).size),0,ObjData.CurrObjSec.secalign));
  1992. ObjData.alloc(Tai_datablock(hp).size);
  1993. end;
  1994. end;
  1995. ait_realconst:
  1996. begin
  1997. case tai_realconst(hp).realtyp of
  1998. aitrealconst_s32bit:
  1999. begin
  2000. ssingle:=single(tai_realconst(hp).value.s32val);
  2001. pdata:=@ssingle;
  2002. end;
  2003. aitrealconst_s64bit:
  2004. begin
  2005. ddouble:=double(tai_realconst(hp).value.s64val);
  2006. pdata:=@ddouble;
  2007. end;
  2008. {$if defined(cpuextended) and defined(FPC_HAS_TYPE_EXTENDED)}
  2009. { can't write full 80 bit floating point constants yet on non-x86 }
  2010. aitrealconst_s80bit:
  2011. begin
  2012. eextended:=extended(tai_realconst(hp).value.s80val);
  2013. pdata:=@eextended;
  2014. end;
  2015. {$else}
  2016. {$ifdef FPC_SOFT_FPUX80}
  2017. {$push}{$warn 6018 off} { Unreachable code due to compile time evaluation }
  2018. aitrealconst_s80bit:
  2019. begin
  2020. if sizeof(tai_realconst(hp).value.s80val) = sizeof(double) then
  2021. begin
  2022. f64:=float64(double(tai_realconst(hp).value.s80val));
  2023. if float64_is_signaling_nan(f64)<>0 then
  2024. begin
  2025. f64.low := 0;
  2026. f64.high := longword($fff80000);
  2027. end;
  2028. eextended:=float64_to_floatx80(f64);
  2029. end
  2030. else if sizeof(tai_realconst(hp).value.s80val) = sizeof(single) then
  2031. begin
  2032. f32:=float32(single(tai_realconst(hp).value.s80val));
  2033. if float32_is_signaling_nan(f32)<>0 then
  2034. begin
  2035. f32 := longword($ffc00000);
  2036. end;
  2037. eextended:=float32_to_floatx80(f32);
  2038. end
  2039. else
  2040. internalerror(2017091903);
  2041. pdata:=@eextended;
  2042. end;
  2043. {$pop}
  2044. {$endif}
  2045. {$endif cpuextended}
  2046. aitrealconst_s64comp:
  2047. begin
  2048. ccomp:=comp(tai_realconst(hp).value.s64compval);
  2049. pdata:=@ccomp;
  2050. end;
  2051. else
  2052. internalerror(2015030501);
  2053. end;
  2054. ObjData.writebytes(pdata^,tai_realconst(hp).datasize);
  2055. ObjData.writebytes(zerobuf,tai_realconst(hp).savesize-tai_realconst(hp).datasize);
  2056. end;
  2057. ait_string :
  2058. ObjData.writebytes(Tai_string(hp).str^,Tai_string(hp).len);
  2059. ait_const :
  2060. begin
  2061. { Recalculate relative symbols, addresses of forward references
  2062. can be changed in treepass1 }
  2063. relative_reloc:=false;
  2064. if assigned(tai_const(hp).sym) and
  2065. assigned(tai_const(hp).endsym) then
  2066. begin
  2067. objsym:=Objdata.SymbolRef(tai_const(hp).sym);
  2068. objsymend:=Objdata.SymbolRef(tai_const(hp).endsym);
  2069. relative_reloc:=(objsym.objsection<>objsymend.objsection);
  2070. if Tai_const(hp).consttype in [aitconst_gottpoff] then
  2071. begin
  2072. if objsymend.objsection<>ObjData.CurrObjSec then
  2073. Internalerror(2019092802);
  2074. Tai_const(hp).value:=objsymend.address-ObjData.CurrObjSec.Size+Tai_const(hp).symofs;
  2075. end
  2076. else if Tai_const(hp).consttype in [aitconst_tlsgd,aitconst_tlsdesc] then
  2077. begin
  2078. if objsymend.objsection<>ObjData.CurrObjSec then
  2079. Internalerror(2019092803);
  2080. Tai_const(hp).value:=ObjData.CurrObjSec.Size-objsymend.address+Tai_const(hp).symofs;
  2081. end
  2082. else if objsymend.objsection<>objsym.objsection then
  2083. begin
  2084. if (Tai_const(hp).consttype in [aitconst_uleb128bit,aitconst_sleb128bit]) or
  2085. (objsym.objsection<>ObjData.CurrObjSec) then
  2086. internalerror(2019010301);
  2087. end
  2088. else
  2089. {$push} {$R-}{$Q-}
  2090. Tai_const(hp).value:=objsymend.address-objsym.address+Tai_const(hp).symofs;
  2091. end;
  2092. {$pop}
  2093. case tai_const(hp).consttype of
  2094. aitconst_64bit,
  2095. aitconst_32bit,
  2096. aitconst_16bit,
  2097. aitconst_64bit_unaligned,
  2098. aitconst_32bit_unaligned,
  2099. aitconst_16bit_unaligned,
  2100. aitconst_8bit :
  2101. begin
  2102. if assigned(tai_const(hp).sym) and
  2103. not assigned(tai_const(hp).endsym) then
  2104. ObjData.writereloc(Tai_const(hp).symofs,tai_const(hp).size,Objdata.SymbolRef(tai_const(hp).sym),RELOC_ABSOLUTE)
  2105. else if relative_reloc then
  2106. ObjData.writereloc(ObjData.CurrObjSec.size+tai_const(hp).size-objsym.address+tai_const(hp).symofs,tai_const(hp).size,objsymend,RELOC_RELATIVE)
  2107. else
  2108. ObjData.writebytes(Tai_const(hp).value,tai_const(hp).size);
  2109. end;
  2110. aitconst_rva_symbol :
  2111. begin
  2112. { PE32+? }
  2113. if target_info.system in systems_peoptplus then
  2114. ObjData.writereloc(Tai_const(hp).symofs,sizeof(longint),Objdata.SymbolRef(tai_const(hp).sym),RELOC_RVA)
  2115. else
  2116. ObjData.writereloc(Tai_const(hp).symofs,sizeof(pint),Objdata.SymbolRef(tai_const(hp).sym),RELOC_RVA);
  2117. end;
  2118. aitconst_secrel32_symbol :
  2119. begin
  2120. { Required for DWARF2 support under Windows }
  2121. ObjData.writereloc(Tai_const(hp).symofs,sizeof(longint),Objdata.SymbolRef(tai_const(hp).sym),RELOC_SECREL32);
  2122. end;
  2123. {$ifdef i8086}
  2124. aitconst_farptr :
  2125. if assigned(tai_const(hp).sym) and
  2126. not assigned(tai_const(hp).endsym) then
  2127. ObjData.writereloc(Tai_const(hp).symofs,tai_const(hp).size,Objdata.SymbolRef(tai_const(hp).sym),RELOC_FARPTR)
  2128. else if relative_reloc then
  2129. internalerror(2015040601)
  2130. else
  2131. ObjData.writebytes(Tai_const(hp).value,tai_const(hp).size);
  2132. aitconst_seg:
  2133. if assigned(tai_const(hp).sym) and (tai_const(hp).size=2) then
  2134. ObjData.writereloc(0,2,Objdata.SymbolRef(tai_const(hp).sym),RELOC_SEG)
  2135. else
  2136. internalerror(2015110502);
  2137. aitconst_dgroup:
  2138. ObjData.writereloc(0,2,nil,RELOC_DGROUP);
  2139. aitconst_fardataseg:
  2140. ObjData.writereloc(0,2,nil,RELOC_FARDATASEG);
  2141. {$endif i8086}
  2142. {$ifdef arm}
  2143. aitconst_got:
  2144. ObjData.writereloc(Tai_const(hp).symofs,sizeof(longint),Objdata.SymbolRef(tai_const(hp).sym),RELOC_GOT32);
  2145. { aitconst_gottpoff:
  2146. ObjData.writereloc(Tai_const(hp).symofs,sizeof(longint),Objdata.SymbolRef(tai_const(hp).sym),RELOC_TPOFF); }
  2147. aitconst_tpoff:
  2148. ObjData.writereloc(Tai_const(hp).symofs,sizeof(longint),Objdata.SymbolRef(tai_const(hp).sym),RELOC_TPOFF);
  2149. aitconst_tlsgd:
  2150. ObjData.writereloc(Tai_const(hp).symofs,sizeof(longint),Objdata.SymbolRef(tai_const(hp).sym),RELOC_TLSGD);
  2151. aitconst_tlsdesc:
  2152. begin
  2153. { must be a relative symbol, thus value being valid }
  2154. if not(assigned(tai_const(hp).sym)) or not(assigned(tai_const(hp).endsym)) then
  2155. Internalerror(2019092904);
  2156. ObjData.writereloc(Tai_const(hp).value,sizeof(longint),Objdata.SymbolRef(tai_const(hp).sym),RELOC_TLSDESC);
  2157. end;
  2158. {$endif arm}
  2159. aitconst_dtpoff:
  2160. { so far, the size of dtpoff is fixed to 4 bytes }
  2161. ObjData.writereloc(Tai_const(hp).symofs,4,Objdata.SymbolRef(tai_const(hp).sym),RELOC_DTPOFF);
  2162. aitconst_gotoff_symbol:
  2163. ObjData.writereloc(Tai_const(hp).symofs,sizeof(longint),Objdata.SymbolRef(tai_const(hp).sym),RELOC_GOTOFF);
  2164. aitconst_uleb128bit,
  2165. aitconst_sleb128bit :
  2166. begin
  2167. if Tai_const(hp).fixed_size=0 then
  2168. Internalerror(2019030302);
  2169. if tai_const(hp).consttype=aitconst_uleb128bit then
  2170. leblen:=EncodeUleb128(qword(Tai_const(hp).value),lebbuf,Tai_const(hp).fixed_size)
  2171. else
  2172. leblen:=EncodeSleb128(Tai_const(hp).value,lebbuf,Tai_const(hp).fixed_size);
  2173. if leblen<>tai_const(hp).fixed_size then
  2174. internalerror(200709271);
  2175. ObjData.writebytes(lebbuf,leblen);
  2176. end;
  2177. aitconst_darwin_dwarf_delta32,
  2178. aitconst_darwin_dwarf_delta64:
  2179. ObjData.writebytes(Tai_const(hp).value,tai_const(hp).size);
  2180. aitconst_half16bit,
  2181. aitconst_gs:
  2182. begin
  2183. tmp:=Tai_const(hp).value div 2;
  2184. ObjData.writebytes(tmp,2);
  2185. end;
  2186. else
  2187. internalerror(200603254);
  2188. end;
  2189. end;
  2190. ait_label :
  2191. begin
  2192. { exporting shouldn't be necessary as labels are local,
  2193. but it's better to be on the safe side (PFV) }
  2194. ObjOutput.exportsymbol(ObjData.SymbolRef(Tai_label(hp).labsym));
  2195. end;
  2196. ait_instruction :
  2197. Taicpu(hp).Pass2(ObjData);
  2198. ait_stab :
  2199. WriteStab(Tai_stab(hp).str);
  2200. ait_function_name,
  2201. ait_force_line : ;
  2202. ait_cutobject :
  2203. if SmartAsm then
  2204. break;
  2205. ait_directive :
  2206. begin
  2207. case tai_directive(hp).directive of
  2208. asd_weak_definition,
  2209. asd_weak_reference:
  2210. begin
  2211. objsym:=ObjData.symbolref(tai_directive(hp).name);
  2212. if objsym.bind in [AB_EXTERNAL,AB_WEAK_EXTERNAL] then
  2213. objsym.bind:=AB_WEAK_EXTERNAL
  2214. else
  2215. { TODO: should become a weak definition; for now, do
  2216. the same as what was done for ait_weak }
  2217. objsym.bind:=AB_WEAK_EXTERNAL;
  2218. end;
  2219. asd_cpu:
  2220. begin
  2221. ObjData.CPUType:=cpu_none;
  2222. for cpu:=low(tcputype) to high(tcputype) do
  2223. if cputypestr[cpu]=tai_directive(hp).name then
  2224. begin
  2225. ObjData.CPUType:=cpu;
  2226. break;
  2227. end;
  2228. end;
  2229. {$ifdef OMFOBJSUPPORT}
  2230. asd_omf_linnum_line:
  2231. begin
  2232. TOmfObjSection(ObjData.CurrObjSec).LinNumEntries.Add(
  2233. TOmfSubRecord_LINNUM_MsLink_Entry.Create(
  2234. strtoint(tai_directive(hp).name),
  2235. ObjData.CurrObjSec.Size
  2236. ));
  2237. end;
  2238. {$endif OMFOBJSUPPORT}
  2239. else
  2240. ;
  2241. end
  2242. end;
  2243. ait_symbolpair:
  2244. begin
  2245. if tai_symbolpair(hp).kind=spk_set then
  2246. begin
  2247. objsym:=ObjData.symbolref(tai_symbolpair(hp).sym^);
  2248. ref:=objdata.symbolref(tai_symbolpair(hp).value^);
  2249. objsym.offset:=ref.offset;
  2250. objsym.objsection:=ref.objsection;
  2251. {$ifdef arm}
  2252. objsym.ThumbFunc:=ref.ThumbFunc;
  2253. {$endif arm}
  2254. end;
  2255. end;
  2256. {$ifndef DISABLE_WIN64_SEH}
  2257. ait_seh_directive :
  2258. tai_seh_directive(hp).generate_code(objdata);
  2259. {$endif DISABLE_WIN64_SEH}
  2260. ait_eabi_attribute :
  2261. begin
  2262. eabi_section:=ObjData.findsection('.ARM.attributes');
  2263. if not(assigned(eabi_section)) then
  2264. Internalerror(2019100704);
  2265. if eabi_section.Size=0 then
  2266. begin
  2267. s:='A';
  2268. eabi_section.write(s[1],1);
  2269. ddword:=eabi_section.Size-1;
  2270. eabi_section.write(ddword,4);
  2271. s:='aeabi'#0;
  2272. eabi_section.write(s[1],6);
  2273. s:=#1;
  2274. eabi_section.write(s[1],1);
  2275. ddword:=eabi_section.Size-1-4-6-1;
  2276. eabi_section.write(ddword,4);
  2277. end;
  2278. leblen:=EncodeUleb128(tai_eabi_attribute(hp).tag,lebbuf,0);
  2279. eabi_section.write(lebbuf,leblen);
  2280. case tai_eabi_attribute(hp).eattr_typ of
  2281. eattrtype_dword:
  2282. begin
  2283. leblen:=EncodeUleb128(tai_eabi_attribute(hp).value,lebbuf,0);
  2284. eabi_section.write(lebbuf,leblen);
  2285. end;
  2286. eattrtype_ntbs:
  2287. begin
  2288. if assigned(tai_eabi_attribute(hp).valuestr) then
  2289. s:=tai_eabi_attribute(hp).valuestr^+#0
  2290. else
  2291. s:=#0;
  2292. eabi_section.write(s[1],Length(s));
  2293. end
  2294. else
  2295. Internalerror(2019100705);
  2296. end;
  2297. { update size of attributes section, write directly to the dyn. arrays as
  2298. we do not increase the size of section }
  2299. TmpDataPos:=eabi_section.Data.Pos;
  2300. eabi_section.Data.seek(1);
  2301. ddword:=eabi_section.Size-1;
  2302. eabi_section.Data.write(ddword,4);
  2303. eabi_section.Data.seek(12);
  2304. ddword:=eabi_section.Size-1-4-6;
  2305. eabi_section.Data.write(ddword,4);
  2306. eabi_section.Data.Seek(TmpDataPos);
  2307. end;
  2308. else
  2309. ;
  2310. end;
  2311. hp:=Tai(hp.next);
  2312. end;
  2313. TreePass2:=hp;
  2314. end;
  2315. procedure TInternalAssembler.writetree;
  2316. label
  2317. doexit;
  2318. var
  2319. hp : Tai;
  2320. ObjWriter : TObjectWriter;
  2321. begin
  2322. ObjWriter:=TObjectwriter.create;
  2323. ObjOutput:=CObjOutput.Create(ObjWriter);
  2324. ObjData:=ObjOutput.newObjData(ObjFileName);
  2325. { Pass 0 }
  2326. ObjData.currpass:=0;
  2327. ObjData.createsection(sec_code);
  2328. ObjData.beforealloc;
  2329. { start with list 1 }
  2330. currlistidx:=1;
  2331. currlist:=list[currlistidx];
  2332. hp:=Tai(currList.first);
  2333. while assigned(hp) do
  2334. begin
  2335. hp:=TreePass0(hp);
  2336. MaybeNextList(hp);
  2337. end;
  2338. ObjData.afteralloc;
  2339. { leave if errors have occurred }
  2340. if errorcount>0 then
  2341. goto doexit;
  2342. { Pass 1 }
  2343. ObjData.currpass:=1;
  2344. ObjData.resetsections;
  2345. ObjData.beforealloc;
  2346. ObjData.createsection(sec_code);
  2347. { start with list 1 }
  2348. currlistidx:=1;
  2349. currlist:=list[currlistidx];
  2350. hp:=Tai(currList.first);
  2351. while assigned(hp) do
  2352. begin
  2353. hp:=TreePass1(hp);
  2354. MaybeNextList(hp);
  2355. end;
  2356. ObjData.createsection(sec_code);
  2357. ObjData.afteralloc;
  2358. { leave if errors have occurred }
  2359. if errorcount>0 then
  2360. goto doexit;
  2361. { Pass 2 }
  2362. ObjData.currpass:=2;
  2363. ObjData.resetsections;
  2364. ObjData.beforewrite;
  2365. ObjData.createsection(sec_code);
  2366. { start with list 1 }
  2367. currlistidx:=1;
  2368. currlist:=list[currlistidx];
  2369. hp:=Tai(currList.first);
  2370. while assigned(hp) do
  2371. begin
  2372. hp:=TreePass2(hp);
  2373. MaybeNextList(hp);
  2374. end;
  2375. ObjData.createsection(sec_code);
  2376. ObjData.afterwrite;
  2377. { don't write the .o file if errors have occurred }
  2378. if errorcount=0 then
  2379. begin
  2380. { write objectfile }
  2381. ObjOutput.startobjectfile(ObjFileName);
  2382. ObjOutput.writeobjectfile(ObjData);
  2383. end;
  2384. doexit:
  2385. { Cleanup }
  2386. ObjData.free;
  2387. ObjData:=nil;
  2388. ObjWriter.free;
  2389. end;
  2390. procedure TInternalAssembler.writetreesmart;
  2391. var
  2392. hp : Tai;
  2393. startsectype : TAsmSectiontype;
  2394. place: tcutplace;
  2395. ObjWriter : TObjectWriter;
  2396. startsecname: String;
  2397. startsecorder: TAsmSectionOrder;
  2398. begin
  2399. if not(cs_asm_leave in current_settings.globalswitches) and
  2400. not(af_needar in asminfo^.flags) then
  2401. ObjWriter:=CInternalAr.CreateAr(current_module.staticlibfilename)
  2402. else
  2403. ObjWriter:=TObjectwriter.create;
  2404. NextSmartName(cut_normal);
  2405. ObjOutput:=CObjOutput.Create(ObjWriter);
  2406. startsectype:=sec_none;
  2407. startsecname:='';
  2408. startsecorder:=secorder_default;
  2409. { start with list 1 }
  2410. currlistidx:=1;
  2411. currlist:=list[currlistidx];
  2412. hp:=Tai(currList.first);
  2413. while assigned(hp) do
  2414. begin
  2415. ObjData:=ObjOutput.newObjData(ObjFileName);
  2416. { Pass 0 }
  2417. ObjData.currpass:=0;
  2418. ObjData.resetsections;
  2419. ObjData.beforealloc;
  2420. if startsectype<>sec_none then
  2421. ObjData.CreateSection(startsectype,startsecname,startsecorder);
  2422. TreePass0(hp);
  2423. ObjData.afteralloc;
  2424. { leave if errors have occurred }
  2425. if errorcount>0 then
  2426. break;
  2427. { Pass 1 }
  2428. ObjData.currpass:=1;
  2429. ObjData.resetsections;
  2430. ObjData.beforealloc;
  2431. if startsectype<>sec_none then
  2432. ObjData.CreateSection(startsectype,startsecname,startsecorder);
  2433. TreePass1(hp);
  2434. ObjData.afteralloc;
  2435. { leave if errors have occurred }
  2436. if errorcount>0 then
  2437. break;
  2438. { Pass 2 }
  2439. ObjData.currpass:=2;
  2440. ObjOutput.startobjectfile(ObjFileName);
  2441. ObjData.resetsections;
  2442. ObjData.beforewrite;
  2443. if startsectype<>sec_none then
  2444. ObjData.CreateSection(startsectype,startsecname,startsecorder);
  2445. hp:=TreePass2(hp);
  2446. ObjData.afterwrite;
  2447. { leave if errors have occurred }
  2448. if errorcount>0 then
  2449. break;
  2450. { write the current objectfile }
  2451. ObjOutput.writeobjectfile(ObjData);
  2452. ObjData.free;
  2453. ObjData:=nil;
  2454. { end of lists? }
  2455. if not MaybeNextList(hp) then
  2456. break;
  2457. { we will start a new objectfile so reset everything }
  2458. { The place can still change in the next while loop, so don't init }
  2459. { the writer yet (JM) }
  2460. if (hp.typ=ait_cutobject) then
  2461. place := Tai_cutobject(hp).place
  2462. else
  2463. place := cut_normal;
  2464. { avoid empty files }
  2465. startsectype:=sec_none;
  2466. startsecname:='';
  2467. startsecorder:=secorder_default;
  2468. while assigned(hp) and
  2469. (Tai(hp).typ in [ait_marker,ait_comment,ait_section,ait_cutobject]) do
  2470. begin
  2471. if Tai(hp).typ=ait_section then
  2472. begin
  2473. startsectype:=Tai_section(hp).sectype;
  2474. startsecname:=Tai_section(hp).name^;
  2475. startsecorder:=Tai_section(hp).secorder;
  2476. end;
  2477. if (Tai(hp).typ=ait_cutobject) then
  2478. place:=Tai_cutobject(hp).place;
  2479. hp:=Tai(hp.next);
  2480. end;
  2481. if not MaybeNextList(hp) then
  2482. break;
  2483. { start next objectfile }
  2484. NextSmartName(place);
  2485. end;
  2486. ObjData.free;
  2487. ObjData:=nil;
  2488. ObjWriter.free;
  2489. end;
  2490. procedure TInternalAssembler.MakeObject;
  2491. var to_do:set of TasmlistType;
  2492. i:TasmlistType;
  2493. procedure addlist(p:TAsmList);
  2494. begin
  2495. inc(lists);
  2496. list[lists]:=p;
  2497. end;
  2498. begin
  2499. to_do:=[low(Tasmlisttype)..high(Tasmlisttype)];
  2500. if usedeffileforexports then
  2501. exclude(to_do,al_exports);
  2502. if not(tf_section_threadvars in target_info.flags) then
  2503. exclude(to_do,al_threadvars);
  2504. for i:=low(TasmlistType) to high(TasmlistType) do
  2505. if (i in to_do) and (current_asmdata.asmlists[i]<>nil) and
  2506. (not current_asmdata.asmlists[i].empty) then
  2507. addlist(current_asmdata.asmlists[i]);
  2508. if SmartAsm then
  2509. writetreesmart
  2510. else
  2511. writetree;
  2512. end;
  2513. {*****************************************************************************
  2514. Generate Assembler Files Main Procedure
  2515. *****************************************************************************}
  2516. Procedure GenerateAsm(smart:boolean);
  2517. var
  2518. a : TAssembler;
  2519. begin
  2520. if not assigned(CAssembler[target_asm.id]) then
  2521. Message(asmw_f_assembler_output_not_supported);
  2522. a:=CAssembler[target_asm.id].Create(@target_asm,smart);
  2523. a.MakeObject;
  2524. a.Free;
  2525. end;
  2526. function GetExternalGnuAssemblerWithAsmInfoWriter(info: pasminfo; wr: TExternalAssemblerOutputFile): TExternalAssembler;
  2527. var
  2528. asmkind: tasm;
  2529. begin
  2530. for asmkind in [as_gas,as_ggas,as_darwin,as_clang_gas,as_clang_asdarwin] do
  2531. if assigned(asminfos[asmkind]) and
  2532. (target_info.system in asminfos[asmkind]^.supported_targets) then
  2533. begin
  2534. result:=TExternalAssemblerClass(CAssembler[asmkind]).CreateWithWriter(asminfos[asmkind],wr,false,false);
  2535. exit;
  2536. end;
  2537. Internalerror(2015090604);
  2538. end;
  2539. {*****************************************************************************
  2540. Init/Done
  2541. *****************************************************************************}
  2542. procedure RegisterAssembler(const r:tasminfo;c:TAssemblerClass);
  2543. var
  2544. t : tasm;
  2545. begin
  2546. t:=r.id;
  2547. if assigned(asminfos[t]) then
  2548. writeln('Warning: Assembler is already registered!')
  2549. else
  2550. Getmem(asminfos[t],sizeof(tasminfo));
  2551. asminfos[t]^:=r;
  2552. CAssembler[t]:=c;
  2553. end;
  2554. end.