assemble.pas 94 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735
  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. eextended: floatx80;
  987. {$endif}
  988. {$endif cpuextended}
  989. begin
  990. if do_line then
  991. begin
  992. case tai_realconst(hp).realtyp of
  993. aitrealconst_s32bit:
  994. writer.AsmWriteLn(asminfo^.comment+'value: '+single2str(tai_realconst(hp).value.s32val));
  995. aitrealconst_s64bit:
  996. writer.AsmWriteLn(asminfo^.comment+'value: '+double2str(tai_realconst(hp).value.s64val));
  997. {$if defined(cpuextended) and defined(FPC_HAS_TYPE_EXTENDED)}
  998. { can't write full 80 bit floating point constants yet on non-x86 }
  999. aitrealconst_s80bit:
  1000. writer.AsmWriteLn(asminfo^.comment+'value: '+extended2str(tai_realconst(hp).value.s80val));
  1001. {$else}
  1002. {$ifdef FPC_SOFT_FPUX80}
  1003. {$push}{$warn 6018 off} { Unreachable code due to compile time evaluation }
  1004. aitrealconst_s80bit:
  1005. begin
  1006. if sizeof(tai_realconst(hp).value.s80val) = sizeof(double) then
  1007. writer.AsmWriteLn(asminfo^.comment+'value: '+double2str(tai_realconst(hp).value.s80val))
  1008. else if sizeof(tai_realconst(hp).value.s80val) = sizeof(single) then
  1009. writer.AsmWriteLn(asminfo^.comment+'value: '+single2str(tai_realconst(hp).value.s80val))
  1010. else
  1011. internalerror(2017091901);
  1012. end;
  1013. {$pop}
  1014. {$endif}
  1015. {$endif cpuextended}
  1016. aitrealconst_s64comp:
  1017. writer.AsmWriteLn(asminfo^.comment+'value: '+extended2str(tai_realconst(hp).value.s64compval));
  1018. else
  1019. internalerror(2014050604);
  1020. end;
  1021. end;
  1022. writer.AsmWrite(dbdir);
  1023. { generic float writing code: get start address of value, then write
  1024. byte by byte. Can't use fields directly, because e.g ts64comp is
  1025. defined as extended on x86 }
  1026. case tai_realconst(hp).realtyp of
  1027. aitrealconst_s32bit:
  1028. begin
  1029. ssingle:=single(tai_realconst(hp).value.s32val);
  1030. pdata:=@ssingle;
  1031. end;
  1032. aitrealconst_s64bit:
  1033. begin
  1034. ddouble:=double(tai_realconst(hp).value.s64val);
  1035. pdata:=@ddouble;
  1036. end;
  1037. {$if defined(cpuextended) and defined(FPC_HAS_TYPE_EXTENDED)}
  1038. { can't write full 80 bit floating point constants yet on non-x86 }
  1039. aitrealconst_s80bit:
  1040. begin
  1041. eextended:=extended(tai_realconst(hp).value.s80val);
  1042. pdata:=@eextended;
  1043. end;
  1044. {$else}
  1045. {$ifdef FPC_SOFT_FPUX80}
  1046. {$push}{$warn 6018 off} { Unreachable code due to compile time evaluation }
  1047. aitrealconst_s80bit:
  1048. begin
  1049. if sizeof(tai_realconst(hp).value.s80val) = sizeof(double) then
  1050. eextended:=float64_to_floatx80(float64(double(tai_realconst(hp).value.s80val)))
  1051. else if sizeof(tai_realconst(hp).value.s80val) = sizeof(single) then
  1052. eextended:=float32_to_floatx80(float32(single(tai_realconst(hp).value.s80val)))
  1053. else
  1054. internalerror(2017091902);
  1055. pdata:=@eextended;
  1056. end;
  1057. {$pop}
  1058. {$endif}
  1059. {$endif cpuextended}
  1060. aitrealconst_s64comp:
  1061. begin
  1062. ccomp:=comp(tai_realconst(hp).value.s64compval);
  1063. pdata:=@ccomp;
  1064. end;
  1065. else
  1066. internalerror(2014051001);
  1067. end;
  1068. count:=tai_realconst(hp).datasize;
  1069. { write bytes in inverse order if source and target endianess don't
  1070. match }
  1071. if source_info.endian<>target_info.endian then
  1072. begin
  1073. { go from back to front }
  1074. index:=count-1;
  1075. step:=-1;
  1076. end
  1077. else
  1078. begin
  1079. index:=0;
  1080. step:=1;
  1081. end;
  1082. {$ifdef ARM}
  1083. { ARM-specific: low and high dwords of a double may be swapped }
  1084. if tai_realconst(hp).formatoptions=fo_hiloswapped then
  1085. begin
  1086. { only supported for double }
  1087. if tai_realconst(hp).datasize<>8 then
  1088. internalerror(2014050605);
  1089. { switch bit of the index so that the words are written in
  1090. the opposite order }
  1091. swapmask:=4;
  1092. end
  1093. else
  1094. {$endif ARM}
  1095. swapmask:=0;
  1096. repeat
  1097. writer.AsmWrite(tostr(pdata[index xor swapmask]));
  1098. inc(index,step);
  1099. dec(count);
  1100. if count<>0 then
  1101. writer.AsmWrite(',');
  1102. until count=0;
  1103. { padding }
  1104. for count:=tai_realconst(hp).datasize+1 to tai_realconst(hp).savesize do
  1105. writer.AsmWrite(',0');
  1106. writer.AsmLn;
  1107. end;
  1108. function TExternalAssembler.WriteComments(var hp: tai): boolean;
  1109. begin
  1110. result:=true;
  1111. case hp.typ of
  1112. ait_comment :
  1113. Begin
  1114. writer.AsmWrite(asminfo^.comment);
  1115. writer.AsmWritePChar(tai_comment(hp).str);
  1116. writer.AsmLn;
  1117. End;
  1118. ait_regalloc :
  1119. begin
  1120. if (cs_asm_regalloc in current_settings.globalswitches) then
  1121. begin
  1122. writer.AsmWrite(#9+asminfo^.comment+'Register ');
  1123. repeat
  1124. writer.AsmWrite(std_regname(Tai_regalloc(hp).reg));
  1125. if (hp.next=nil) or
  1126. (tai(hp.next).typ<>ait_regalloc) or
  1127. (tai_regalloc(hp.next).ratype<>tai_regalloc(hp).ratype) then
  1128. break;
  1129. hp:=tai(hp.next);
  1130. writer.AsmWrite(',');
  1131. until false;
  1132. writer.AsmWrite(' ');
  1133. writer.AsmWriteLn(regallocstr[tai_regalloc(hp).ratype]);
  1134. end;
  1135. end;
  1136. ait_tempalloc :
  1137. begin
  1138. if (cs_asm_tempalloc in current_settings.globalswitches) then
  1139. WriteTempalloc(tai_tempalloc(hp));
  1140. end;
  1141. ait_varloc:
  1142. begin
  1143. { ait_varloc is present here only when register allocation is not done ( -sr option ) }
  1144. if tai_varloc(hp).newlocationhi<>NR_NO then
  1145. writer.AsmWriteLn(asminfo^.comment+'Var '+tai_varloc(hp).varsym.realname+' located in register '+
  1146. std_regname(tai_varloc(hp).newlocationhi)+':'+std_regname(tai_varloc(hp).newlocation))
  1147. else
  1148. writer.AsmWriteLn(asminfo^.comment+'Var '+tai_varloc(hp).varsym.realname+' located in register '+
  1149. std_regname(tai_varloc(hp).newlocation));
  1150. end;
  1151. else
  1152. result:=false;
  1153. end;
  1154. end;
  1155. procedure TExternalAssembler.WriteTree(p:TAsmList);
  1156. begin
  1157. end;
  1158. procedure TExternalAssembler.WriteAsmList;
  1159. begin
  1160. end;
  1161. procedure TExternalAssembler.MakeObject;
  1162. begin
  1163. writer.AsmCreate(cut_normal);
  1164. FillChar(lastfileinfo, sizeof(lastfileinfo), 0);
  1165. lastfileinfo.line := -1;
  1166. lastinfile := nil;
  1167. lastsectype := sec_none;
  1168. WriteAsmList;
  1169. writer.AsmClose;
  1170. if not(writer.ioerror) then
  1171. DoAssemble;
  1172. end;
  1173. destructor TExternalAssembler.Destroy;
  1174. begin
  1175. if ffreewriter then
  1176. writer.Free;
  1177. inherited;
  1178. end;
  1179. {*****************************************************************************
  1180. TInternalAssembler
  1181. *****************************************************************************}
  1182. constructor TInternalAssembler.Create(info: pasminfo; smart: boolean);
  1183. begin
  1184. inherited;
  1185. ObjOutput:=nil;
  1186. ObjData:=nil;
  1187. SmartAsm:=smart;
  1188. {$ifdef ARM}
  1189. Code16:=current_settings.instructionset=is_thumb;
  1190. {$endif ARM}
  1191. end;
  1192. destructor TInternalAssembler.destroy;
  1193. begin
  1194. if assigned(ObjData) then
  1195. ObjData.free;
  1196. if assigned(ObjOutput) then
  1197. ObjOutput.free;
  1198. end;
  1199. procedure TInternalAssembler.WriteStab(p:pchar);
  1200. function consumecomma(var p:pchar):boolean;
  1201. begin
  1202. while (p^=' ') do
  1203. inc(p);
  1204. result:=(p^=',');
  1205. inc(p);
  1206. end;
  1207. function consumenumber(var p:pchar;out value:longint):boolean;
  1208. var
  1209. hs : string;
  1210. len,
  1211. code : integer;
  1212. begin
  1213. value:=0;
  1214. while (p^=' ') do
  1215. inc(p);
  1216. len:=0;
  1217. while (p^ in ['0'..'9']) do
  1218. begin
  1219. inc(len);
  1220. hs[len]:=p^;
  1221. inc(p);
  1222. end;
  1223. if len>0 then
  1224. begin
  1225. hs[0]:=chr(len);
  1226. val(hs,value,code);
  1227. end
  1228. else
  1229. code:=-1;
  1230. result:=(code=0);
  1231. end;
  1232. function consumeoffset(var p:pchar;out relocsym:tobjsymbol;out value:longint):boolean;
  1233. var
  1234. hs : string;
  1235. len,
  1236. code : integer;
  1237. pstart : pchar;
  1238. sym : tobjsymbol;
  1239. exprvalue : longint;
  1240. gotmin,
  1241. have_first_symbol,
  1242. have_second_symbol,
  1243. dosub : boolean;
  1244. begin
  1245. result:=false;
  1246. value:=0;
  1247. relocsym:=nil;
  1248. gotmin:=false;
  1249. have_first_symbol:=false;
  1250. have_second_symbol:=false;
  1251. repeat
  1252. dosub:=false;
  1253. exprvalue:=0;
  1254. if gotmin then
  1255. begin
  1256. dosub:=true;
  1257. gotmin:=false;
  1258. end;
  1259. while (p^=' ') do
  1260. inc(p);
  1261. case p^ of
  1262. #0 :
  1263. break;
  1264. ' ' :
  1265. inc(p);
  1266. '0'..'9' :
  1267. begin
  1268. len:=0;
  1269. while (p^ in ['0'..'9']) do
  1270. begin
  1271. inc(len);
  1272. hs[len]:=p^;
  1273. inc(p);
  1274. end;
  1275. hs[0]:=chr(len);
  1276. val(hs,exprvalue,code);
  1277. if code<>0 then
  1278. internalerror(200702251);
  1279. end;
  1280. '.','_',
  1281. 'A'..'Z',
  1282. 'a'..'z' :
  1283. begin
  1284. pstart:=p;
  1285. while not(p^ in [#0,' ','-','+']) do
  1286. inc(p);
  1287. len:=p-pstart;
  1288. if len>255 then
  1289. internalerror(200509187);
  1290. hs[0]:=chr(len);
  1291. move(pstart^,hs[1],len);
  1292. sym:=objdata.symbolref(hs);
  1293. { Second symbol? }
  1294. if assigned(relocsym) then
  1295. begin
  1296. if have_second_symbol then
  1297. internalerror(2007032201);
  1298. have_second_symbol:=true;
  1299. if not have_first_symbol then
  1300. internalerror(2007032202);
  1301. { second symbol should substracted to first }
  1302. if not dosub then
  1303. internalerror(2007032203);
  1304. if (relocsym.objsection<>sym.objsection) then
  1305. internalerror(2005091810);
  1306. exprvalue:=relocsym.address-sym.address;
  1307. relocsym:=nil;
  1308. dosub:=false;
  1309. end
  1310. else
  1311. begin
  1312. relocsym:=sym;
  1313. if assigned(sym.objsection) then
  1314. begin
  1315. { first symbol should be + }
  1316. if not have_first_symbol and dosub then
  1317. internalerror(2007032204);
  1318. have_first_symbol:=true;
  1319. end;
  1320. end;
  1321. end;
  1322. '+' :
  1323. begin
  1324. { nothing, by default addition is done }
  1325. inc(p);
  1326. end;
  1327. '-' :
  1328. begin
  1329. gotmin:=true;
  1330. inc(p);
  1331. end;
  1332. else
  1333. internalerror(200509189);
  1334. end;
  1335. if dosub then
  1336. dec(value,exprvalue)
  1337. else
  1338. inc(value,exprvalue);
  1339. until false;
  1340. result:=true;
  1341. end;
  1342. var
  1343. stabstrlen,
  1344. ofs,
  1345. nline,
  1346. nidx,
  1347. nother,
  1348. i : longint;
  1349. stab : TObjStabEntry;
  1350. relocsym : TObjSymbol;
  1351. pstr,
  1352. pcurr,
  1353. pendquote : pchar;
  1354. oldsec : TObjSection;
  1355. begin
  1356. pcurr:=nil;
  1357. pstr:=nil;
  1358. pendquote:=nil;
  1359. relocsym:=nil;
  1360. ofs:=0;
  1361. { Parse string part }
  1362. if (p[0]='"') then
  1363. begin
  1364. pstr:=@p[1];
  1365. { Ignore \" inside the string }
  1366. i:=1;
  1367. while not((p[i]='"') and (p[i-1]<>'\')) and
  1368. (p[i]<>#0) do
  1369. inc(i);
  1370. pendquote:=@p[i];
  1371. pendquote^:=#0;
  1372. pcurr:=@p[i+1];
  1373. if not consumecomma(pcurr) then
  1374. internalerror(200509181);
  1375. end
  1376. else
  1377. pcurr:=p;
  1378. { When in pass 1 then only alloc and leave }
  1379. if ObjData.currpass=1 then
  1380. begin
  1381. ObjData.StabsSec.Alloc(sizeof(TObjStabEntry));
  1382. if assigned(pstr) and (pstr[0]<>#0) then
  1383. ObjData.StabStrSec.Alloc(strlen(pstr)+1);
  1384. end
  1385. else
  1386. begin
  1387. { Stabs format: nidx,nother,nline[,offset] }
  1388. if not consumenumber(pcurr,nidx) then
  1389. internalerror(200509182);
  1390. if not consumecomma(pcurr) then
  1391. internalerror(200509183);
  1392. if not consumenumber(pcurr,nother) then
  1393. internalerror(200509184);
  1394. if not consumecomma(pcurr) then
  1395. internalerror(200509185);
  1396. if not consumenumber(pcurr,nline) then
  1397. internalerror(200509186);
  1398. if consumecomma(pcurr) then
  1399. consumeoffset(pcurr,relocsym,ofs);
  1400. { Generate stab entry }
  1401. if assigned(pstr) and (pstr[0]<>#0) then
  1402. begin
  1403. stabstrlen:=strlen(pstr);
  1404. {$ifdef optimizestabs}
  1405. StabStrEntry:=nil;
  1406. if (nidx=N_SourceFile) or (nidx=N_IncludeFile) then
  1407. begin
  1408. hs:=strpas(pstr);
  1409. StabstrEntry:=StabStrDict.Find(hs);
  1410. if not assigned(StabstrEntry) then
  1411. begin
  1412. StabstrEntry:=TStabStrEntry.Create(hs);
  1413. StabstrEntry:=StabStrSec.Size;
  1414. StabStrDict.Insert(StabstrEntry);
  1415. { generate new stab }
  1416. StabstrEntry:=nil;
  1417. end;
  1418. end;
  1419. if assigned(StabstrEntry) then
  1420. stab.strpos:=StabstrEntry.strpos
  1421. else
  1422. {$endif optimizestabs}
  1423. begin
  1424. stab.strpos:=ObjData.StabStrSec.Size;
  1425. ObjData.StabStrSec.write(pstr^,stabstrlen+1);
  1426. end;
  1427. end
  1428. else
  1429. stab.strpos:=0;
  1430. stab.ntype:=byte(nidx);
  1431. stab.ndesc:=word(nline);
  1432. stab.nother:=byte(nother);
  1433. stab.nvalue:=ofs;
  1434. { Write the stab first without the value field. Then
  1435. write a the value field with relocation }
  1436. oldsec:=ObjData.CurrObjSec;
  1437. ObjData.SetSection(ObjData.StabsSec);
  1438. ObjData.Writebytes(stab,sizeof(TObjStabEntry)-4);
  1439. ObjData.Writereloc(stab.nvalue,4,relocsym,RELOC_ABSOLUTE32);
  1440. ObjData.setsection(oldsec);
  1441. end;
  1442. if assigned(pendquote) then
  1443. pendquote^:='"';
  1444. end;
  1445. function TInternalAssembler.MaybeNextList(var hp:Tai):boolean;
  1446. begin
  1447. { maybe end of list }
  1448. while not assigned(hp) do
  1449. begin
  1450. if currlistidx<lists then
  1451. begin
  1452. inc(currlistidx);
  1453. currlist:=list[currlistidx];
  1454. hp:=Tai(currList.first);
  1455. end
  1456. else
  1457. begin
  1458. MaybeNextList:=false;
  1459. exit;
  1460. end;
  1461. end;
  1462. MaybeNextList:=true;
  1463. end;
  1464. function TInternalAssembler.SetIndirectToSymbol(hp: Tai; const indirectname: string): Boolean;
  1465. var
  1466. objsym : TObjSymbol;
  1467. indsym : TObjSymbol;
  1468. begin
  1469. Result:=
  1470. Assigned(hp) and
  1471. (hp.typ=ait_symbol);
  1472. if not Result then
  1473. Exit;
  1474. objsym:=Objdata.SymbolRef(tai_symbol(hp).sym);
  1475. objsym.size:=0;
  1476. indsym := TObjSymbol(ObjData.ObjSymbolList.Find(indirectname));
  1477. if not Assigned(indsym) then
  1478. begin
  1479. { it's possible that indirect symbol is not present in the list,
  1480. so we must create it as undefined }
  1481. indsym:=ObjData.CObjSymbol.Create(ObjData.ObjSymbolList, indirectname);
  1482. indsym.typ:=AT_NONE;
  1483. indsym.bind:=AB_NONE;
  1484. end;
  1485. objsym.indsymbol:=indsym;
  1486. Result:=true;
  1487. end;
  1488. function TInternalAssembler.TreePass0(hp:Tai):Tai;
  1489. var
  1490. objsym,
  1491. objsymend : TObjSymbol;
  1492. cpu: tcputype;
  1493. eabi_section, TmpSection: TObjSection;
  1494. begin
  1495. while assigned(hp) do
  1496. begin
  1497. case hp.typ of
  1498. ait_align :
  1499. begin
  1500. if tai_align_abstract(hp).aligntype>1 then
  1501. begin
  1502. { always use the maximum fillsize in this pass to avoid possible
  1503. short jumps to become out of range }
  1504. Tai_align_abstract(hp).fillsize:=Tai_align_abstract(hp).aligntype;
  1505. ObjData.alloc(Tai_align_abstract(hp).fillsize);
  1506. { may need to increase alignment of section }
  1507. if tai_align_abstract(hp).aligntype>ObjData.CurrObjSec.secalign then
  1508. ObjData.CurrObjSec.secalign:=tai_align_abstract(hp).aligntype;
  1509. end
  1510. else
  1511. Tai_align_abstract(hp).fillsize:=0;
  1512. end;
  1513. ait_datablock :
  1514. begin
  1515. {$ifdef USE_COMM_IN_BSS}
  1516. if writingpackages and
  1517. Tai_datablock(hp).is_global then
  1518. ObjData.SymbolDefine(Tai_datablock(hp).sym)
  1519. else
  1520. {$endif USE_COMM_IN_BSS}
  1521. begin
  1522. ObjData.allocalign(used_align(size_2_align(Tai_datablock(hp).size),0,ObjData.CurrObjSec.secalign));
  1523. ObjData.SymbolDefine(Tai_datablock(hp).sym);
  1524. ObjData.alloc(Tai_datablock(hp).size);
  1525. end;
  1526. end;
  1527. ait_realconst:
  1528. ObjData.alloc(tai_realconst(hp).savesize);
  1529. ait_const:
  1530. begin
  1531. { if symbols are provided we can calculate the value for relative symbols.
  1532. This is required for length calculation of leb128 constants }
  1533. if assigned(tai_const(hp).sym) then
  1534. begin
  1535. objsym:=Objdata.SymbolRef(tai_const(hp).sym);
  1536. { objsym already defined and there is endsym? }
  1537. if assigned(objsym.objsection) and assigned(tai_const(hp).endsym) then
  1538. begin
  1539. objsymend:=Objdata.SymbolRef(tai_const(hp).endsym);
  1540. { objsymend already defined? }
  1541. if assigned(objsymend.objsection) then
  1542. begin
  1543. if objsymend.objsection<>objsym.objsection then
  1544. begin
  1545. { leb128 relative constants are not relocatable, but other types are,
  1546. given that objsym belongs to the current section. }
  1547. if (Tai_const(hp).consttype in [aitconst_uleb128bit,aitconst_sleb128bit]) or
  1548. (objsym.objsection<>ObjData.CurrObjSec) then
  1549. InternalError(200404124);
  1550. end
  1551. {$push} {$R-}{$Q-}
  1552. else
  1553. Tai_const(hp).value:=objsymend.address-objsym.address+Tai_const(hp).symofs;
  1554. end;
  1555. {$pop}
  1556. end;
  1557. end;
  1558. ObjData.alloc(tai_const(hp).size);
  1559. end;
  1560. ait_directive:
  1561. begin
  1562. case tai_directive(hp).directive of
  1563. asd_indirect_symbol:
  1564. { handled in TreePass1 }
  1565. ;
  1566. asd_lazy_reference:
  1567. begin
  1568. if tai_directive(hp).name='' then
  1569. Internalerror(2009112101);
  1570. objsym:=ObjData.symbolref(tai_directive(hp).name);
  1571. objsym.bind:=AB_LAZY;
  1572. end;
  1573. asd_reference:
  1574. { ignore for now, but should be added}
  1575. ;
  1576. asd_cpu:
  1577. begin
  1578. ObjData.CPUType:=cpu_none;
  1579. for cpu:=low(tcputype) to high(tcputype) do
  1580. if cputypestr[cpu]=tai_directive(hp).name then
  1581. begin
  1582. ObjData.CPUType:=cpu;
  1583. break;
  1584. end;
  1585. end;
  1586. asd_weak_definition:
  1587. begin
  1588. if tai_directive(hp).name='' then
  1589. Internalerror(2022040901);
  1590. objsym:=ObjData.symbolref(tai_directive(hp).name);
  1591. objsym.bind:=AB_WEAK;
  1592. end;
  1593. {$ifdef OMFOBJSUPPORT}
  1594. asd_omf_linnum_line:
  1595. { ignore for now, but should be added}
  1596. ;
  1597. {$endif OMFOBJSUPPORT}
  1598. {$ifdef ARM}
  1599. asd_thumb_func:
  1600. ObjData.ThumbFunc:=true;
  1601. asd_force_thumb:
  1602. begin
  1603. ObjData.ThumbFunc:=true;
  1604. Code16:=true;
  1605. end;
  1606. asd_code:
  1607. begin
  1608. { ai_directive(hp).name can be only 16 or 32, this is checked by the reader }
  1609. ObjData.ThumbFunc:=tai_directive(hp).name='16';
  1610. Code16:=tai_directive(hp).name='16';
  1611. end
  1612. {$endif ARM}
  1613. {$ifdef RISCV}
  1614. asd_option:
  1615. internalerror(2019031701);
  1616. {$endif RISCV}
  1617. else
  1618. internalerror(2010011101);
  1619. end;
  1620. end;
  1621. ait_section:
  1622. begin
  1623. if Tai_section(hp).sectype=sec_user then
  1624. ObjData.CreateSection(Tai_section(hp).sectype,Tai_section(hp).secflags,Tai_section(hp).secprogbits,Tai_section(hp).name^,Tai_section(hp).secorder)
  1625. else
  1626. ObjData.CreateSection(Tai_section(hp).sectype,Tai_section(hp).name^,Tai_section(hp).secorder);
  1627. Tai_section(hp).sec:=ObjData.CurrObjSec;
  1628. end;
  1629. ait_symbol :
  1630. begin
  1631. { needs extra support in the internal assembler }
  1632. { the value is just ignored }
  1633. {if tai_symbol(hp).has_value then
  1634. internalerror(2009090804); ;}
  1635. ObjData.SymbolDefine(Tai_symbol(hp).sym);
  1636. end;
  1637. ait_symbolpair :
  1638. with tai_symbolpair(hp) do
  1639. ObjData.SymbolPairDefine(kind,sym^,value^);
  1640. ait_label :
  1641. ObjData.SymbolDefine(Tai_label(hp).labsym);
  1642. ait_string :
  1643. ObjData.alloc(Tai_string(hp).len);
  1644. ait_instruction :
  1645. begin
  1646. {$ifdef arm}
  1647. if code16 then
  1648. include(taicpu(hp).flags,cf_thumb)
  1649. else
  1650. exclude(taicpu(hp).flags,cf_thumb);
  1651. {$endif arm}
  1652. { reset instructions which could change in pass 2 }
  1653. Taicpu(hp).resetpass2;
  1654. ObjData.alloc(Taicpu(hp).Pass1(ObjData));
  1655. end;
  1656. ait_cutobject :
  1657. if SmartAsm then
  1658. break;
  1659. ait_eabi_attribute :
  1660. begin
  1661. eabi_section:=ObjData.findsection('.ARM.attributes');
  1662. if not(assigned(eabi_section)) then
  1663. begin
  1664. TmpSection:=ObjData.CurrObjSec;
  1665. ObjData.CreateSection(sec_arm_attribute,[],SPB_ARM_ATTRIBUTES,'',secorder_default);
  1666. eabi_section:=ObjData.CurrObjSec;
  1667. ObjData.setsection(TmpSection);
  1668. end;
  1669. if eabi_section.Size=0 then
  1670. eabi_section.alloc(16);
  1671. eabi_section.alloc(LengthUleb128(tai_eabi_attribute(hp).tag));
  1672. case tai_eabi_attribute(hp).eattr_typ of
  1673. eattrtype_dword:
  1674. eabi_section.alloc(LengthUleb128(tai_eabi_attribute(hp).value));
  1675. eattrtype_ntbs:
  1676. if assigned(tai_eabi_attribute(hp).valuestr) then
  1677. eabi_section.alloc(Length(tai_eabi_attribute(hp).valuestr^)+1)
  1678. else
  1679. eabi_section.alloc(1);
  1680. else
  1681. Internalerror(2019100701);
  1682. end;
  1683. end;
  1684. {$ifdef WASM}
  1685. ait_globaltype:
  1686. TWasmObjData(ObjData).DeclareGlobalType(tai_globaltype(hp));
  1687. ait_functype:
  1688. TWasmObjData(ObjData).DeclareFuncType(tai_functype(hp));
  1689. ait_tagtype:
  1690. TWasmObjData(ObjData).DeclareTagType(tai_tagtype(hp));
  1691. ait_export_name:
  1692. TWasmObjData(ObjData).DeclareExportName(tai_export_name(hp));
  1693. ait_import_module:
  1694. TWasmObjData(ObjData).DeclareImportModule(tai_import_module(hp));
  1695. ait_import_name:
  1696. TWasmObjData(ObjData).DeclareImportName(tai_import_name(hp));
  1697. ait_local:
  1698. TWasmObjData(ObjData).DeclareLocal(tai_local(hp));
  1699. {$endif WASM}
  1700. else
  1701. ;
  1702. end;
  1703. hp:=Tai(hp.next);
  1704. end;
  1705. TreePass0:=hp;
  1706. end;
  1707. function TInternalAssembler.TreePass1(hp:Tai):Tai;
  1708. var
  1709. objsym,
  1710. objsymend : TObjSymbol;
  1711. cpu: tcputype;
  1712. eabi_section: TObjSection;
  1713. begin
  1714. while assigned(hp) do
  1715. begin
  1716. case hp.typ of
  1717. ait_align :
  1718. begin
  1719. if tai_align_abstract(hp).aligntype>1 then
  1720. begin
  1721. { here we must determine the fillsize which is used in pass2 }
  1722. Tai_align_abstract(hp).fillsize:=align(ObjData.CurrObjSec.Size,Tai_align_abstract(hp).aligntype)-
  1723. ObjData.CurrObjSec.Size;
  1724. { maximum number of bytes for alignment exeeded? }
  1725. if (Tai_align_abstract(hp).aligntype<>Tai_align_abstract(hp).maxbytes) and
  1726. (Tai_align_abstract(hp).fillsize>Tai_align_abstract(hp).maxbytes) then
  1727. Tai_align_abstract(hp).fillsize:=align(ObjData.CurrObjSec.Size,Byte(Tai_align_abstract(hp).aligntype div 2))-
  1728. ObjData.CurrObjSec.Size;
  1729. ObjData.alloc(Tai_align_abstract(hp).fillsize);
  1730. end;
  1731. end;
  1732. ait_datablock :
  1733. begin
  1734. if (oso_data in ObjData.CurrObjSec.secoptions) and
  1735. not (oso_sparse_data in ObjData.CurrObjSec.secoptions) then
  1736. Message(asmw_e_alloc_data_only_in_bss);
  1737. {$ifdef USE_COMM_IN_BSS}
  1738. if writingpackages and
  1739. Tai_datablock(hp).is_global then
  1740. begin
  1741. objsym:=ObjData.SymbolDefine(Tai_datablock(hp).sym);
  1742. objsym.size:=Tai_datablock(hp).size;
  1743. objsym.bind:=AB_COMMON;
  1744. objsym.alignment:=needtowritealignmentalsoforELF;
  1745. end
  1746. else
  1747. {$endif USE_COMM_IN_BSS}
  1748. begin
  1749. ObjData.allocalign(used_align(size_2_align(Tai_datablock(hp).size),0,ObjData.CurrObjSec.secalign));
  1750. objsym:=ObjData.SymbolDefine(Tai_datablock(hp).sym);
  1751. objsym.size:=Tai_datablock(hp).size;
  1752. ObjData.alloc(Tai_datablock(hp).size);
  1753. end;
  1754. end;
  1755. ait_realconst:
  1756. ObjData.alloc(tai_realconst(hp).savesize);
  1757. ait_const:
  1758. begin
  1759. { Recalculate relative symbols }
  1760. if assigned(tai_const(hp).sym) and
  1761. assigned(tai_const(hp).endsym) then
  1762. begin
  1763. objsym:=Objdata.SymbolRef(tai_const(hp).sym);
  1764. objsymend:=Objdata.SymbolRef(tai_const(hp).endsym);
  1765. if Tai_const(hp).consttype in [aitconst_gottpoff,aitconst_tlsgd,aitconst_tlsdesc] then
  1766. begin
  1767. if objsymend.objsection<>ObjData.CurrObjSec then
  1768. Internalerror(2019092801);
  1769. Tai_const(hp).value:=objsymend.address-ObjData.CurrObjSec.Size+Tai_const(hp).symofs;
  1770. end
  1771. else if objsymend.objsection<>objsym.objsection then
  1772. begin
  1773. if (Tai_const(hp).consttype in [aitconst_uleb128bit,aitconst_sleb128bit]) or
  1774. (objsym.objsection<>ObjData.CurrObjSec) then
  1775. internalerror(200905042);
  1776. end
  1777. {$push} {$R-}{$Q-}
  1778. else
  1779. Tai_const(hp).value:=objsymend.address-objsym.address+Tai_const(hp).symofs;
  1780. end;
  1781. {$pop}
  1782. if (Tai_const(hp).consttype in [aitconst_uleb128bit,aitconst_sleb128bit]) then
  1783. Tai_const(hp).fixsize;
  1784. ObjData.alloc(tai_const(hp).size);
  1785. end;
  1786. ait_section:
  1787. begin
  1788. { use cached value }
  1789. ObjData.setsection(Tai_section(hp).sec);
  1790. end;
  1791. ait_stab :
  1792. begin
  1793. if assigned(Tai_stab(hp).str) then
  1794. WriteStab(Tai_stab(hp).str);
  1795. end;
  1796. ait_symbol :
  1797. ObjData.SymbolDefine(Tai_symbol(hp).sym);
  1798. ait_symbol_end :
  1799. begin
  1800. objsym:=ObjData.SymbolRef(Tai_symbol_end(hp).sym);
  1801. objsym.size:=ObjData.CurrObjSec.Size-objsym.offset;
  1802. end;
  1803. ait_symbolpair:
  1804. with tai_symbolpair(hp) do
  1805. ObjData.SymbolPairDefine(kind,sym^,value^);
  1806. ait_label :
  1807. ObjData.SymbolDefine(Tai_label(hp).labsym);
  1808. ait_string :
  1809. ObjData.alloc(Tai_string(hp).len);
  1810. ait_instruction :
  1811. ObjData.alloc(Taicpu(hp).Pass1(ObjData));
  1812. ait_cutobject :
  1813. if SmartAsm then
  1814. break;
  1815. ait_directive :
  1816. begin
  1817. case tai_directive(hp).directive of
  1818. asd_indirect_symbol:
  1819. if tai_directive(hp).name='' then
  1820. Internalerror(2009101103)
  1821. else if not SetIndirectToSymbol(Tai(hp.Previous), tai_directive(hp).name) then
  1822. Internalerror(2009101102);
  1823. asd_lazy_reference:
  1824. { handled in TreePass0 }
  1825. ;
  1826. asd_reference:
  1827. { ignore for now, but should be added}
  1828. ;
  1829. asd_thumb_func:
  1830. { ignore for now, but should be added}
  1831. ;
  1832. asd_force_thumb:
  1833. { ignore for now, but should be added}
  1834. ;
  1835. asd_code:
  1836. { ignore for now, but should be added}
  1837. ;
  1838. asd_option:
  1839. { ignore for now, but should be added}
  1840. ;
  1841. asd_weak_definition:
  1842. { ignore for now, but should be added}
  1843. ;
  1844. {$ifdef OMFOBJSUPPORT}
  1845. asd_omf_linnum_line:
  1846. { ignore for now, but should be added}
  1847. ;
  1848. {$endif OMFOBJSUPPORT}
  1849. asd_cpu:
  1850. begin
  1851. ObjData.CPUType:=cpu_none;
  1852. for cpu:=low(tcputype) to high(tcputype) do
  1853. if cputypestr[cpu]=tai_directive(hp).name then
  1854. begin
  1855. ObjData.CPUType:=cpu;
  1856. break;
  1857. end;
  1858. end;
  1859. else
  1860. internalerror(2010011102);
  1861. end;
  1862. end;
  1863. ait_eabi_attribute :
  1864. begin
  1865. eabi_section:=ObjData.findsection('.ARM.attributes');
  1866. if not(assigned(eabi_section)) then
  1867. Internalerror(2019100702);
  1868. if eabi_section.Size=0 then
  1869. eabi_section.alloc(16);
  1870. eabi_section.alloc(LengthUleb128(tai_eabi_attribute(hp).tag));
  1871. case tai_eabi_attribute(hp).eattr_typ of
  1872. eattrtype_dword:
  1873. eabi_section.alloc(LengthUleb128(tai_eabi_attribute(hp).value));
  1874. eattrtype_ntbs:
  1875. if assigned(tai_eabi_attribute(hp).valuestr) then
  1876. eabi_section.alloc(Length(tai_eabi_attribute(hp).valuestr^)+1)
  1877. else
  1878. eabi_section.alloc(1);
  1879. else
  1880. Internalerror(2019100703);
  1881. end;
  1882. end;
  1883. else
  1884. ;
  1885. end;
  1886. hp:=Tai(hp.next);
  1887. end;
  1888. TreePass1:=hp;
  1889. end;
  1890. function TInternalAssembler.TreePass2(hp:Tai):Tai;
  1891. var
  1892. fillbuffer : tfillbuffer;
  1893. leblen : byte;
  1894. lebbuf : array[0..63] of byte;
  1895. objsym,
  1896. ref,
  1897. objsymend : TObjSymbol;
  1898. zerobuf : array[0..63] of byte;
  1899. relative_reloc: boolean;
  1900. pdata : pointer;
  1901. ssingle : single;
  1902. ddouble : double;
  1903. {$if defined(cpuextended) and defined(FPC_HAS_TYPE_EXTENDED)}
  1904. eextended : extended;
  1905. {$else}
  1906. {$ifdef FPC_SOFT_FPUX80}
  1907. eextended : floatx80;
  1908. {$endif}
  1909. {$endif}
  1910. ccomp : comp;
  1911. tmp : word;
  1912. cpu: tcputype;
  1913. ddword : dword;
  1914. eabi_section: TObjSection;
  1915. s: String;
  1916. TmpDataPos: TObjSectionOfs;
  1917. begin
  1918. fillchar(zerobuf,sizeof(zerobuf),0);
  1919. fillchar(objsym,sizeof(objsym),0);
  1920. fillchar(objsymend,sizeof(objsymend),0);
  1921. { main loop }
  1922. while assigned(hp) do
  1923. begin
  1924. case hp.typ of
  1925. ait_align :
  1926. begin
  1927. if tai_align_abstract(hp).aligntype>ObjData.CurrObjSec.secalign then
  1928. InternalError(2012072301);
  1929. if oso_data in ObjData.CurrObjSec.secoptions then
  1930. ObjData.writebytes(Tai_align_abstract(hp).calculatefillbuf(fillbuffer,oso_executable in ObjData.CurrObjSec.secoptions)^,
  1931. Tai_align_abstract(hp).fillsize)
  1932. else
  1933. ObjData.alloc(Tai_align_abstract(hp).fillsize);
  1934. end;
  1935. ait_section :
  1936. begin
  1937. { use cached value }
  1938. ObjData.setsection(Tai_section(hp).sec);
  1939. end;
  1940. ait_symbol :
  1941. begin
  1942. ObjOutput.exportsymbol(ObjData.SymbolRef(Tai_symbol(hp).sym));
  1943. end;
  1944. ait_symbol_end :
  1945. begin
  1946. { recalculate size, as some preceding instructions
  1947. could have been changed to smaller size }
  1948. objsym:=ObjData.SymbolRef(Tai_symbol_end(hp).sym);
  1949. objsym.size:=ObjData.CurrObjSec.Size-objsym.offset;
  1950. end;
  1951. ait_datablock :
  1952. begin
  1953. ObjOutput.exportsymbol(ObjData.SymbolRef(Tai_datablock(hp).sym));
  1954. {$ifdef USE_COMM_IN_BSS}
  1955. if not(writingpackages and
  1956. Tai_datablock(hp).is_global) then
  1957. {$endif USE_COMM_IN_BSS}
  1958. begin
  1959. ObjData.allocalign(used_align(size_2_align(Tai_datablock(hp).size),0,ObjData.CurrObjSec.secalign));
  1960. ObjData.alloc(Tai_datablock(hp).size);
  1961. end;
  1962. end;
  1963. ait_realconst:
  1964. begin
  1965. case tai_realconst(hp).realtyp of
  1966. aitrealconst_s32bit:
  1967. begin
  1968. ssingle:=single(tai_realconst(hp).value.s32val);
  1969. pdata:=@ssingle;
  1970. end;
  1971. aitrealconst_s64bit:
  1972. begin
  1973. ddouble:=double(tai_realconst(hp).value.s64val);
  1974. pdata:=@ddouble;
  1975. end;
  1976. {$if defined(cpuextended) and defined(FPC_HAS_TYPE_EXTENDED)}
  1977. { can't write full 80 bit floating point constants yet on non-x86 }
  1978. aitrealconst_s80bit:
  1979. begin
  1980. eextended:=extended(tai_realconst(hp).value.s80val);
  1981. pdata:=@eextended;
  1982. end;
  1983. {$else}
  1984. {$ifdef FPC_SOFT_FPUX80}
  1985. {$push}{$warn 6018 off} { Unreachable code due to compile time evaluation }
  1986. aitrealconst_s80bit:
  1987. begin
  1988. if sizeof(tai_realconst(hp).value.s80val) = sizeof(double) then
  1989. eextended:=float64_to_floatx80(float64(double(tai_realconst(hp).value.s80val)))
  1990. else if sizeof(tai_realconst(hp).value.s80val) = sizeof(single) then
  1991. eextended:=float32_to_floatx80(float32(single(tai_realconst(hp).value.s80val)))
  1992. else
  1993. internalerror(2017091903);
  1994. pdata:=@eextended;
  1995. end;
  1996. {$pop}
  1997. {$endif}
  1998. {$endif cpuextended}
  1999. aitrealconst_s64comp:
  2000. begin
  2001. ccomp:=comp(tai_realconst(hp).value.s64compval);
  2002. pdata:=@ccomp;
  2003. end;
  2004. else
  2005. internalerror(2015030501);
  2006. end;
  2007. ObjData.writebytes(pdata^,tai_realconst(hp).datasize);
  2008. ObjData.writebytes(zerobuf,tai_realconst(hp).savesize-tai_realconst(hp).datasize);
  2009. end;
  2010. ait_string :
  2011. ObjData.writebytes(Tai_string(hp).str^,Tai_string(hp).len);
  2012. ait_const :
  2013. begin
  2014. { Recalculate relative symbols, addresses of forward references
  2015. can be changed in treepass1 }
  2016. relative_reloc:=false;
  2017. if assigned(tai_const(hp).sym) and
  2018. assigned(tai_const(hp).endsym) then
  2019. begin
  2020. objsym:=Objdata.SymbolRef(tai_const(hp).sym);
  2021. objsymend:=Objdata.SymbolRef(tai_const(hp).endsym);
  2022. relative_reloc:=(objsym.objsection<>objsymend.objsection);
  2023. if Tai_const(hp).consttype in [aitconst_gottpoff] then
  2024. begin
  2025. if objsymend.objsection<>ObjData.CurrObjSec then
  2026. Internalerror(2019092802);
  2027. Tai_const(hp).value:=objsymend.address-ObjData.CurrObjSec.Size+Tai_const(hp).symofs;
  2028. end
  2029. else if Tai_const(hp).consttype in [aitconst_tlsgd,aitconst_tlsdesc] then
  2030. begin
  2031. if objsymend.objsection<>ObjData.CurrObjSec then
  2032. Internalerror(2019092803);
  2033. Tai_const(hp).value:=ObjData.CurrObjSec.Size-objsymend.address+Tai_const(hp).symofs;
  2034. end
  2035. else if objsymend.objsection<>objsym.objsection then
  2036. begin
  2037. if (Tai_const(hp).consttype in [aitconst_uleb128bit,aitconst_sleb128bit]) or
  2038. (objsym.objsection<>ObjData.CurrObjSec) then
  2039. internalerror(2019010301);
  2040. end
  2041. else
  2042. {$push} {$R-}{$Q-}
  2043. Tai_const(hp).value:=objsymend.address-objsym.address+Tai_const(hp).symofs;
  2044. end;
  2045. {$pop}
  2046. case tai_const(hp).consttype of
  2047. aitconst_64bit,
  2048. aitconst_32bit,
  2049. aitconst_16bit,
  2050. aitconst_64bit_unaligned,
  2051. aitconst_32bit_unaligned,
  2052. aitconst_16bit_unaligned,
  2053. aitconst_8bit :
  2054. begin
  2055. if assigned(tai_const(hp).sym) and
  2056. not assigned(tai_const(hp).endsym) then
  2057. ObjData.writereloc(Tai_const(hp).symofs,tai_const(hp).size,Objdata.SymbolRef(tai_const(hp).sym),RELOC_ABSOLUTE)
  2058. else if relative_reloc then
  2059. ObjData.writereloc(ObjData.CurrObjSec.size+tai_const(hp).size-objsym.address+tai_const(hp).symofs,tai_const(hp).size,objsymend,RELOC_RELATIVE)
  2060. else
  2061. ObjData.writebytes(Tai_const(hp).value,tai_const(hp).size);
  2062. end;
  2063. aitconst_rva_symbol :
  2064. begin
  2065. { PE32+? }
  2066. if target_info.system in systems_peoptplus then
  2067. ObjData.writereloc(Tai_const(hp).symofs,sizeof(longint),Objdata.SymbolRef(tai_const(hp).sym),RELOC_RVA)
  2068. else
  2069. ObjData.writereloc(Tai_const(hp).symofs,sizeof(pint),Objdata.SymbolRef(tai_const(hp).sym),RELOC_RVA);
  2070. end;
  2071. aitconst_secrel32_symbol :
  2072. begin
  2073. { Required for DWARF2 support under Windows }
  2074. ObjData.writereloc(Tai_const(hp).symofs,sizeof(longint),Objdata.SymbolRef(tai_const(hp).sym),RELOC_SECREL32);
  2075. end;
  2076. {$ifdef i8086}
  2077. aitconst_farptr :
  2078. if assigned(tai_const(hp).sym) and
  2079. not assigned(tai_const(hp).endsym) then
  2080. ObjData.writereloc(Tai_const(hp).symofs,tai_const(hp).size,Objdata.SymbolRef(tai_const(hp).sym),RELOC_FARPTR)
  2081. else if relative_reloc then
  2082. internalerror(2015040601)
  2083. else
  2084. ObjData.writebytes(Tai_const(hp).value,tai_const(hp).size);
  2085. aitconst_seg:
  2086. if assigned(tai_const(hp).sym) and (tai_const(hp).size=2) then
  2087. ObjData.writereloc(0,2,Objdata.SymbolRef(tai_const(hp).sym),RELOC_SEG)
  2088. else
  2089. internalerror(2015110502);
  2090. aitconst_dgroup:
  2091. ObjData.writereloc(0,2,nil,RELOC_DGROUP);
  2092. aitconst_fardataseg:
  2093. ObjData.writereloc(0,2,nil,RELOC_FARDATASEG);
  2094. {$endif i8086}
  2095. {$ifdef arm}
  2096. aitconst_got:
  2097. ObjData.writereloc(Tai_const(hp).symofs,sizeof(longint),Objdata.SymbolRef(tai_const(hp).sym),RELOC_GOT32);
  2098. { aitconst_gottpoff:
  2099. ObjData.writereloc(Tai_const(hp).symofs,sizeof(longint),Objdata.SymbolRef(tai_const(hp).sym),RELOC_TPOFF); }
  2100. aitconst_tpoff:
  2101. ObjData.writereloc(Tai_const(hp).symofs,sizeof(longint),Objdata.SymbolRef(tai_const(hp).sym),RELOC_TPOFF);
  2102. aitconst_tlsgd:
  2103. ObjData.writereloc(Tai_const(hp).symofs,sizeof(longint),Objdata.SymbolRef(tai_const(hp).sym),RELOC_TLSGD);
  2104. aitconst_tlsdesc:
  2105. begin
  2106. { must be a relative symbol, thus value being valid }
  2107. if not(assigned(tai_const(hp).sym)) or not(assigned(tai_const(hp).endsym)) then
  2108. Internalerror(2019092904);
  2109. ObjData.writereloc(Tai_const(hp).value,sizeof(longint),Objdata.SymbolRef(tai_const(hp).sym),RELOC_TLSDESC);
  2110. end;
  2111. {$endif arm}
  2112. aitconst_dtpoff:
  2113. { so far, the size of dtpoff is fixed to 4 bytes }
  2114. ObjData.writereloc(Tai_const(hp).symofs,4,Objdata.SymbolRef(tai_const(hp).sym),RELOC_DTPOFF);
  2115. aitconst_gotoff_symbol:
  2116. ObjData.writereloc(Tai_const(hp).symofs,sizeof(longint),Objdata.SymbolRef(tai_const(hp).sym),RELOC_GOTOFF);
  2117. aitconst_uleb128bit,
  2118. aitconst_sleb128bit :
  2119. begin
  2120. if Tai_const(hp).fixed_size=0 then
  2121. Internalerror(2019030302);
  2122. if tai_const(hp).consttype=aitconst_uleb128bit then
  2123. leblen:=EncodeUleb128(qword(Tai_const(hp).value),lebbuf,Tai_const(hp).fixed_size)
  2124. else
  2125. leblen:=EncodeSleb128(Tai_const(hp).value,lebbuf,Tai_const(hp).fixed_size);
  2126. if leblen<>tai_const(hp).fixed_size then
  2127. internalerror(200709271);
  2128. ObjData.writebytes(lebbuf,leblen);
  2129. end;
  2130. aitconst_darwin_dwarf_delta32,
  2131. aitconst_darwin_dwarf_delta64:
  2132. ObjData.writebytes(Tai_const(hp).value,tai_const(hp).size);
  2133. aitconst_half16bit,
  2134. aitconst_gs:
  2135. begin
  2136. tmp:=Tai_const(hp).value div 2;
  2137. ObjData.writebytes(tmp,2);
  2138. end;
  2139. else
  2140. internalerror(200603254);
  2141. end;
  2142. end;
  2143. ait_label :
  2144. begin
  2145. { exporting shouldn't be necessary as labels are local,
  2146. but it's better to be on the safe side (PFV) }
  2147. ObjOutput.exportsymbol(ObjData.SymbolRef(Tai_label(hp).labsym));
  2148. end;
  2149. ait_instruction :
  2150. Taicpu(hp).Pass2(ObjData);
  2151. ait_stab :
  2152. WriteStab(Tai_stab(hp).str);
  2153. ait_function_name,
  2154. ait_force_line : ;
  2155. ait_cutobject :
  2156. if SmartAsm then
  2157. break;
  2158. ait_directive :
  2159. begin
  2160. case tai_directive(hp).directive of
  2161. asd_weak_definition,
  2162. asd_weak_reference:
  2163. begin
  2164. objsym:=ObjData.symbolref(tai_directive(hp).name);
  2165. if objsym.bind in [AB_EXTERNAL,AB_WEAK_EXTERNAL] then
  2166. objsym.bind:=AB_WEAK_EXTERNAL
  2167. else
  2168. { TODO: should become a weak definition; for now, do
  2169. the same as what was done for ait_weak }
  2170. objsym.bind:=AB_WEAK_EXTERNAL;
  2171. end;
  2172. asd_cpu:
  2173. begin
  2174. ObjData.CPUType:=cpu_none;
  2175. for cpu:=low(tcputype) to high(tcputype) do
  2176. if cputypestr[cpu]=tai_directive(hp).name then
  2177. begin
  2178. ObjData.CPUType:=cpu;
  2179. break;
  2180. end;
  2181. end;
  2182. {$ifdef OMFOBJSUPPORT}
  2183. asd_omf_linnum_line:
  2184. begin
  2185. TOmfObjSection(ObjData.CurrObjSec).LinNumEntries.Add(
  2186. TOmfSubRecord_LINNUM_MsLink_Entry.Create(
  2187. strtoint(tai_directive(hp).name),
  2188. ObjData.CurrObjSec.Size
  2189. ));
  2190. end;
  2191. {$endif OMFOBJSUPPORT}
  2192. else
  2193. ;
  2194. end
  2195. end;
  2196. ait_symbolpair:
  2197. begin
  2198. if tai_symbolpair(hp).kind=spk_set then
  2199. begin
  2200. objsym:=ObjData.symbolref(tai_symbolpair(hp).sym^);
  2201. ref:=objdata.symbolref(tai_symbolpair(hp).value^);
  2202. objsym.offset:=ref.offset;
  2203. objsym.objsection:=ref.objsection;
  2204. {$ifdef arm}
  2205. objsym.ThumbFunc:=ref.ThumbFunc;
  2206. {$endif arm}
  2207. end;
  2208. end;
  2209. {$ifndef DISABLE_WIN64_SEH}
  2210. ait_seh_directive :
  2211. tai_seh_directive(hp).generate_code(objdata);
  2212. {$endif DISABLE_WIN64_SEH}
  2213. ait_eabi_attribute :
  2214. begin
  2215. eabi_section:=ObjData.findsection('.ARM.attributes');
  2216. if not(assigned(eabi_section)) then
  2217. Internalerror(2019100704);
  2218. if eabi_section.Size=0 then
  2219. begin
  2220. s:='A';
  2221. eabi_section.write(s[1],1);
  2222. ddword:=eabi_section.Size-1;
  2223. eabi_section.write(ddword,4);
  2224. s:='aeabi'#0;
  2225. eabi_section.write(s[1],6);
  2226. s:=#1;
  2227. eabi_section.write(s[1],1);
  2228. ddword:=eabi_section.Size-1-4-6-1;
  2229. eabi_section.write(ddword,4);
  2230. end;
  2231. leblen:=EncodeUleb128(tai_eabi_attribute(hp).tag,lebbuf,0);
  2232. eabi_section.write(lebbuf,leblen);
  2233. case tai_eabi_attribute(hp).eattr_typ of
  2234. eattrtype_dword:
  2235. begin
  2236. leblen:=EncodeUleb128(tai_eabi_attribute(hp).value,lebbuf,0);
  2237. eabi_section.write(lebbuf,leblen);
  2238. end;
  2239. eattrtype_ntbs:
  2240. begin
  2241. if assigned(tai_eabi_attribute(hp).valuestr) then
  2242. s:=tai_eabi_attribute(hp).valuestr^+#0
  2243. else
  2244. s:=#0;
  2245. eabi_section.write(s[1],Length(s));
  2246. end
  2247. else
  2248. Internalerror(2019100705);
  2249. end;
  2250. { update size of attributes section, write directly to the dyn. arrays as
  2251. we do not increase the size of section }
  2252. TmpDataPos:=eabi_section.Data.Pos;
  2253. eabi_section.Data.seek(1);
  2254. ddword:=eabi_section.Size-1;
  2255. eabi_section.Data.write(ddword,4);
  2256. eabi_section.Data.seek(12);
  2257. ddword:=eabi_section.Size-1-4-6;
  2258. eabi_section.Data.write(ddword,4);
  2259. eabi_section.Data.Seek(TmpDataPos);
  2260. end;
  2261. else
  2262. ;
  2263. end;
  2264. hp:=Tai(hp.next);
  2265. end;
  2266. TreePass2:=hp;
  2267. end;
  2268. procedure TInternalAssembler.writetree;
  2269. label
  2270. doexit;
  2271. var
  2272. hp : Tai;
  2273. ObjWriter : TObjectWriter;
  2274. begin
  2275. ObjWriter:=TObjectwriter.create;
  2276. ObjOutput:=CObjOutput.Create(ObjWriter);
  2277. ObjData:=ObjOutput.newObjData(ObjFileName);
  2278. { Pass 0 }
  2279. ObjData.currpass:=0;
  2280. ObjData.createsection(sec_code);
  2281. ObjData.beforealloc;
  2282. { start with list 1 }
  2283. currlistidx:=1;
  2284. currlist:=list[currlistidx];
  2285. hp:=Tai(currList.first);
  2286. while assigned(hp) do
  2287. begin
  2288. hp:=TreePass0(hp);
  2289. MaybeNextList(hp);
  2290. end;
  2291. ObjData.afteralloc;
  2292. { leave if errors have occurred }
  2293. if errorcount>0 then
  2294. goto doexit;
  2295. { Pass 1 }
  2296. ObjData.currpass:=1;
  2297. ObjData.resetsections;
  2298. ObjData.beforealloc;
  2299. ObjData.createsection(sec_code);
  2300. { start with list 1 }
  2301. currlistidx:=1;
  2302. currlist:=list[currlistidx];
  2303. hp:=Tai(currList.first);
  2304. while assigned(hp) do
  2305. begin
  2306. hp:=TreePass1(hp);
  2307. MaybeNextList(hp);
  2308. end;
  2309. ObjData.createsection(sec_code);
  2310. ObjData.afteralloc;
  2311. { leave if errors have occurred }
  2312. if errorcount>0 then
  2313. goto doexit;
  2314. { Pass 2 }
  2315. ObjData.currpass:=2;
  2316. ObjData.resetsections;
  2317. ObjData.beforewrite;
  2318. ObjData.createsection(sec_code);
  2319. { start with list 1 }
  2320. currlistidx:=1;
  2321. currlist:=list[currlistidx];
  2322. hp:=Tai(currList.first);
  2323. while assigned(hp) do
  2324. begin
  2325. hp:=TreePass2(hp);
  2326. MaybeNextList(hp);
  2327. end;
  2328. ObjData.createsection(sec_code);
  2329. ObjData.afterwrite;
  2330. { don't write the .o file if errors have occurred }
  2331. if errorcount=0 then
  2332. begin
  2333. { write objectfile }
  2334. ObjOutput.startobjectfile(ObjFileName);
  2335. ObjOutput.writeobjectfile(ObjData);
  2336. end;
  2337. doexit:
  2338. { Cleanup }
  2339. ObjData.free;
  2340. ObjData:=nil;
  2341. ObjWriter.free;
  2342. end;
  2343. procedure TInternalAssembler.writetreesmart;
  2344. var
  2345. hp : Tai;
  2346. startsectype : TAsmSectiontype;
  2347. place: tcutplace;
  2348. ObjWriter : TObjectWriter;
  2349. startsecname: String;
  2350. startsecorder: TAsmSectionOrder;
  2351. begin
  2352. if not(cs_asm_leave in current_settings.globalswitches) and
  2353. not(af_needar in asminfo^.flags) then
  2354. ObjWriter:=CInternalAr.CreateAr(current_module.staticlibfilename)
  2355. else
  2356. ObjWriter:=TObjectwriter.create;
  2357. NextSmartName(cut_normal);
  2358. ObjOutput:=CObjOutput.Create(ObjWriter);
  2359. startsectype:=sec_none;
  2360. startsecname:='';
  2361. startsecorder:=secorder_default;
  2362. { start with list 1 }
  2363. currlistidx:=1;
  2364. currlist:=list[currlistidx];
  2365. hp:=Tai(currList.first);
  2366. while assigned(hp) do
  2367. begin
  2368. ObjData:=ObjOutput.newObjData(ObjFileName);
  2369. { Pass 0 }
  2370. ObjData.currpass:=0;
  2371. ObjData.resetsections;
  2372. ObjData.beforealloc;
  2373. if startsectype<>sec_none then
  2374. ObjData.CreateSection(startsectype,startsecname,startsecorder);
  2375. TreePass0(hp);
  2376. ObjData.afteralloc;
  2377. { leave if errors have occurred }
  2378. if errorcount>0 then
  2379. break;
  2380. { Pass 1 }
  2381. ObjData.currpass:=1;
  2382. ObjData.resetsections;
  2383. ObjData.beforealloc;
  2384. if startsectype<>sec_none then
  2385. ObjData.CreateSection(startsectype,startsecname,startsecorder);
  2386. TreePass1(hp);
  2387. ObjData.afteralloc;
  2388. { leave if errors have occurred }
  2389. if errorcount>0 then
  2390. break;
  2391. { Pass 2 }
  2392. ObjData.currpass:=2;
  2393. ObjOutput.startobjectfile(ObjFileName);
  2394. ObjData.resetsections;
  2395. ObjData.beforewrite;
  2396. if startsectype<>sec_none then
  2397. ObjData.CreateSection(startsectype,startsecname,startsecorder);
  2398. hp:=TreePass2(hp);
  2399. ObjData.afterwrite;
  2400. { leave if errors have occurred }
  2401. if errorcount>0 then
  2402. break;
  2403. { write the current objectfile }
  2404. ObjOutput.writeobjectfile(ObjData);
  2405. ObjData.free;
  2406. ObjData:=nil;
  2407. { end of lists? }
  2408. if not MaybeNextList(hp) then
  2409. break;
  2410. { we will start a new objectfile so reset everything }
  2411. { The place can still change in the next while loop, so don't init }
  2412. { the writer yet (JM) }
  2413. if (hp.typ=ait_cutobject) then
  2414. place := Tai_cutobject(hp).place
  2415. else
  2416. place := cut_normal;
  2417. { avoid empty files }
  2418. startsectype:=sec_none;
  2419. startsecname:='';
  2420. startsecorder:=secorder_default;
  2421. while assigned(hp) and
  2422. (Tai(hp).typ in [ait_marker,ait_comment,ait_section,ait_cutobject]) do
  2423. begin
  2424. if Tai(hp).typ=ait_section then
  2425. begin
  2426. startsectype:=Tai_section(hp).sectype;
  2427. startsecname:=Tai_section(hp).name^;
  2428. startsecorder:=Tai_section(hp).secorder;
  2429. end;
  2430. if (Tai(hp).typ=ait_cutobject) then
  2431. place:=Tai_cutobject(hp).place;
  2432. hp:=Tai(hp.next);
  2433. end;
  2434. if not MaybeNextList(hp) then
  2435. break;
  2436. { start next objectfile }
  2437. NextSmartName(place);
  2438. end;
  2439. ObjData.free;
  2440. ObjData:=nil;
  2441. ObjWriter.free;
  2442. end;
  2443. procedure TInternalAssembler.MakeObject;
  2444. var to_do:set of TasmlistType;
  2445. i:TasmlistType;
  2446. procedure addlist(p:TAsmList);
  2447. begin
  2448. inc(lists);
  2449. list[lists]:=p;
  2450. end;
  2451. begin
  2452. to_do:=[low(Tasmlisttype)..high(Tasmlisttype)];
  2453. if usedeffileforexports then
  2454. exclude(to_do,al_exports);
  2455. if not(tf_section_threadvars in target_info.flags) then
  2456. exclude(to_do,al_threadvars);
  2457. for i:=low(TasmlistType) to high(TasmlistType) do
  2458. if (i in to_do) and (current_asmdata.asmlists[i]<>nil) and
  2459. (not current_asmdata.asmlists[i].empty) then
  2460. addlist(current_asmdata.asmlists[i]);
  2461. if SmartAsm then
  2462. writetreesmart
  2463. else
  2464. writetree;
  2465. end;
  2466. {*****************************************************************************
  2467. Generate Assembler Files Main Procedure
  2468. *****************************************************************************}
  2469. Procedure GenerateAsm(smart:boolean);
  2470. var
  2471. a : TAssembler;
  2472. begin
  2473. if not assigned(CAssembler[target_asm.id]) then
  2474. Message(asmw_f_assembler_output_not_supported);
  2475. a:=CAssembler[target_asm.id].Create(@target_asm,smart);
  2476. a.MakeObject;
  2477. a.Free;
  2478. end;
  2479. function GetExternalGnuAssemblerWithAsmInfoWriter(info: pasminfo; wr: TExternalAssemblerOutputFile): TExternalAssembler;
  2480. var
  2481. asmkind: tasm;
  2482. begin
  2483. for asmkind in [as_gas,as_ggas,as_darwin,as_clang_gas,as_clang_asdarwin] do
  2484. if assigned(asminfos[asmkind]) and
  2485. (target_info.system in asminfos[asmkind]^.supported_targets) then
  2486. begin
  2487. result:=TExternalAssemblerClass(CAssembler[asmkind]).CreateWithWriter(asminfos[asmkind],wr,false,false);
  2488. exit;
  2489. end;
  2490. Internalerror(2015090604);
  2491. end;
  2492. {*****************************************************************************
  2493. Init/Done
  2494. *****************************************************************************}
  2495. procedure RegisterAssembler(const r:tasminfo;c:TAssemblerClass);
  2496. var
  2497. t : tasm;
  2498. begin
  2499. t:=r.id;
  2500. if assigned(asminfos[t]) then
  2501. writeln('Warning: Assembler is already registered!')
  2502. else
  2503. Getmem(asminfos[t],sizeof(tasminfo));
  2504. asminfos[t]^:=r;
  2505. CAssembler[t]:=c;
  2506. end;
  2507. end.