assemble.pas 94 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747
  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. {$ifdef DEBUG}
  1498. if not(hp.typ in SkipLineInfo) then
  1499. current_filepos:=tailineinfo(hp).fileinfo;
  1500. {$endif DEBUG}
  1501. case hp.typ of
  1502. ait_align :
  1503. begin
  1504. if tai_align_abstract(hp).aligntype>1 then
  1505. begin
  1506. { always use the maximum fillsize in this pass to avoid possible
  1507. short jumps to become out of range }
  1508. Tai_align_abstract(hp).fillsize:=Tai_align_abstract(hp).aligntype;
  1509. ObjData.alloc(Tai_align_abstract(hp).fillsize);
  1510. { may need to increase alignment of section }
  1511. if tai_align_abstract(hp).aligntype>ObjData.CurrObjSec.secalign then
  1512. ObjData.CurrObjSec.secalign:=tai_align_abstract(hp).aligntype;
  1513. end
  1514. else
  1515. Tai_align_abstract(hp).fillsize:=0;
  1516. end;
  1517. ait_datablock :
  1518. begin
  1519. {$ifdef USE_COMM_IN_BSS}
  1520. if writingpackages and
  1521. Tai_datablock(hp).is_global then
  1522. ObjData.SymbolDefine(Tai_datablock(hp).sym)
  1523. else
  1524. {$endif USE_COMM_IN_BSS}
  1525. begin
  1526. ObjData.allocalign(used_align(size_2_align(Tai_datablock(hp).size),0,ObjData.CurrObjSec.secalign));
  1527. ObjData.SymbolDefine(Tai_datablock(hp).sym);
  1528. ObjData.alloc(Tai_datablock(hp).size);
  1529. end;
  1530. end;
  1531. ait_realconst:
  1532. ObjData.alloc(tai_realconst(hp).savesize);
  1533. ait_const:
  1534. begin
  1535. { if symbols are provided we can calculate the value for relative symbols.
  1536. This is required for length calculation of leb128 constants }
  1537. if assigned(tai_const(hp).sym) then
  1538. begin
  1539. objsym:=Objdata.SymbolRef(tai_const(hp).sym);
  1540. { objsym already defined and there is endsym? }
  1541. if assigned(objsym.objsection) and assigned(tai_const(hp).endsym) then
  1542. begin
  1543. objsymend:=Objdata.SymbolRef(tai_const(hp).endsym);
  1544. { objsymend already defined? }
  1545. if assigned(objsymend.objsection) then
  1546. begin
  1547. if objsymend.objsection<>objsym.objsection then
  1548. begin
  1549. { leb128 relative constants are not relocatable, but other types are,
  1550. given that objsym belongs to the current section. }
  1551. if (Tai_const(hp).consttype in [aitconst_uleb128bit,aitconst_sleb128bit]) or
  1552. (objsym.objsection<>ObjData.CurrObjSec) then
  1553. InternalError(200404124);
  1554. end
  1555. {$push} {$R-}{$Q-}
  1556. else
  1557. Tai_const(hp).value:=objsymend.address-objsym.address+Tai_const(hp).symofs;
  1558. end;
  1559. {$pop}
  1560. end;
  1561. end;
  1562. ObjData.alloc(tai_const(hp).size);
  1563. end;
  1564. ait_directive:
  1565. begin
  1566. case tai_directive(hp).directive of
  1567. asd_indirect_symbol:
  1568. { handled in TreePass1 }
  1569. ;
  1570. asd_lazy_reference:
  1571. begin
  1572. if tai_directive(hp).name='' then
  1573. Internalerror(2009112101);
  1574. objsym:=ObjData.symbolref(tai_directive(hp).name);
  1575. objsym.bind:=AB_LAZY;
  1576. end;
  1577. asd_reference:
  1578. { ignore for now, but should be added}
  1579. ;
  1580. asd_cpu:
  1581. begin
  1582. ObjData.CPUType:=cpu_none;
  1583. for cpu:=low(tcputype) to high(tcputype) do
  1584. if cputypestr[cpu]=tai_directive(hp).name then
  1585. begin
  1586. ObjData.CPUType:=cpu;
  1587. break;
  1588. end;
  1589. end;
  1590. asd_weak_definition:
  1591. begin
  1592. if tai_directive(hp).name='' then
  1593. Internalerror(2022040901);
  1594. objsym:=ObjData.symbolref(tai_directive(hp).name);
  1595. objsym.bind:=AB_WEAK;
  1596. end;
  1597. {$ifdef OMFOBJSUPPORT}
  1598. asd_omf_linnum_line:
  1599. { ignore for now, but should be added}
  1600. ;
  1601. {$endif OMFOBJSUPPORT}
  1602. {$ifdef ARM}
  1603. asd_thumb_func:
  1604. ObjData.ThumbFunc:=true;
  1605. asd_force_thumb:
  1606. begin
  1607. ObjData.ThumbFunc:=true;
  1608. Code16:=true;
  1609. end;
  1610. asd_code:
  1611. begin
  1612. { ai_directive(hp).name can be only 16 or 32, this is checked by the reader }
  1613. ObjData.ThumbFunc:=tai_directive(hp).name='16';
  1614. Code16:=tai_directive(hp).name='16';
  1615. end
  1616. {$endif ARM}
  1617. {$ifdef RISCV}
  1618. asd_option:
  1619. internalerror(2019031701);
  1620. {$endif RISCV}
  1621. else
  1622. internalerror(2010011101);
  1623. end;
  1624. end;
  1625. ait_section:
  1626. begin
  1627. if Tai_section(hp).sectype=sec_user then
  1628. ObjData.CreateSection(Tai_section(hp).sectype,Tai_section(hp).secflags,Tai_section(hp).secprogbits,Tai_section(hp).name^,Tai_section(hp).secorder)
  1629. else
  1630. ObjData.CreateSection(Tai_section(hp).sectype,Tai_section(hp).name^,Tai_section(hp).secorder);
  1631. Tai_section(hp).sec:=ObjData.CurrObjSec;
  1632. end;
  1633. ait_symbol :
  1634. begin
  1635. { needs extra support in the internal assembler }
  1636. { the value is just ignored }
  1637. {if tai_symbol(hp).has_value then
  1638. internalerror(2009090804); ;}
  1639. ObjData.SymbolDefine(Tai_symbol(hp).sym);
  1640. end;
  1641. ait_symbolpair :
  1642. with tai_symbolpair(hp) do
  1643. ObjData.SymbolPairDefine(kind,sym^,value^);
  1644. ait_label :
  1645. ObjData.SymbolDefine(Tai_label(hp).labsym);
  1646. ait_string :
  1647. ObjData.alloc(Tai_string(hp).len);
  1648. ait_instruction :
  1649. begin
  1650. {$ifdef arm}
  1651. if code16 then
  1652. include(taicpu(hp).flags,cf_thumb)
  1653. else
  1654. exclude(taicpu(hp).flags,cf_thumb);
  1655. {$endif arm}
  1656. { reset instructions which could change in pass 2 }
  1657. Taicpu(hp).resetpass2;
  1658. ObjData.alloc(Taicpu(hp).Pass1(ObjData));
  1659. end;
  1660. ait_cutobject :
  1661. if SmartAsm then
  1662. break;
  1663. ait_eabi_attribute :
  1664. begin
  1665. eabi_section:=ObjData.findsection('.ARM.attributes');
  1666. if not(assigned(eabi_section)) then
  1667. begin
  1668. TmpSection:=ObjData.CurrObjSec;
  1669. ObjData.CreateSection(sec_arm_attribute,[],SPB_ARM_ATTRIBUTES,'',secorder_default);
  1670. eabi_section:=ObjData.CurrObjSec;
  1671. ObjData.setsection(TmpSection);
  1672. end;
  1673. if eabi_section.Size=0 then
  1674. eabi_section.alloc(16);
  1675. eabi_section.alloc(LengthUleb128(tai_eabi_attribute(hp).tag));
  1676. case tai_eabi_attribute(hp).eattr_typ of
  1677. eattrtype_dword:
  1678. eabi_section.alloc(LengthUleb128(tai_eabi_attribute(hp).value));
  1679. eattrtype_ntbs:
  1680. if assigned(tai_eabi_attribute(hp).valuestr) then
  1681. eabi_section.alloc(Length(tai_eabi_attribute(hp).valuestr^)+1)
  1682. else
  1683. eabi_section.alloc(1);
  1684. else
  1685. Internalerror(2019100701);
  1686. end;
  1687. end;
  1688. {$ifdef WASM}
  1689. ait_globaltype:
  1690. TWasmObjData(ObjData).DeclareGlobalType(tai_globaltype(hp));
  1691. ait_functype:
  1692. TWasmObjData(ObjData).DeclareFuncType(tai_functype(hp));
  1693. ait_tagtype:
  1694. TWasmObjData(ObjData).DeclareTagType(tai_tagtype(hp));
  1695. ait_export_name:
  1696. TWasmObjData(ObjData).DeclareExportName(tai_export_name(hp));
  1697. ait_import_module:
  1698. TWasmObjData(ObjData).DeclareImportModule(tai_import_module(hp));
  1699. ait_import_name:
  1700. TWasmObjData(ObjData).DeclareImportName(tai_import_name(hp));
  1701. ait_local:
  1702. TWasmObjData(ObjData).DeclareLocal(tai_local(hp));
  1703. {$endif WASM}
  1704. else
  1705. ;
  1706. end;
  1707. hp:=Tai(hp.next);
  1708. end;
  1709. TreePass0:=hp;
  1710. end;
  1711. function TInternalAssembler.TreePass1(hp:Tai):Tai;
  1712. var
  1713. objsym,
  1714. objsymend : TObjSymbol;
  1715. cpu: tcputype;
  1716. eabi_section: TObjSection;
  1717. begin
  1718. while assigned(hp) do
  1719. begin
  1720. {$ifdef DEBUG}
  1721. if not(hp.typ in SkipLineInfo) then
  1722. current_filepos:=tailineinfo(hp).fileinfo;
  1723. {$endif DEBUG}
  1724. case hp.typ of
  1725. ait_align :
  1726. begin
  1727. if tai_align_abstract(hp).aligntype>1 then
  1728. begin
  1729. { here we must determine the fillsize which is used in pass2 }
  1730. Tai_align_abstract(hp).fillsize:=align(ObjData.CurrObjSec.Size,Tai_align_abstract(hp).aligntype)-
  1731. ObjData.CurrObjSec.Size;
  1732. { maximum number of bytes for alignment exeeded? }
  1733. if (Tai_align_abstract(hp).aligntype<>Tai_align_abstract(hp).maxbytes) and
  1734. (Tai_align_abstract(hp).fillsize>Tai_align_abstract(hp).maxbytes) then
  1735. Tai_align_abstract(hp).fillsize:=align(ObjData.CurrObjSec.Size,Byte(Tai_align_abstract(hp).aligntype div 2))-
  1736. ObjData.CurrObjSec.Size;
  1737. ObjData.alloc(Tai_align_abstract(hp).fillsize);
  1738. end;
  1739. end;
  1740. ait_datablock :
  1741. begin
  1742. if (oso_data in ObjData.CurrObjSec.secoptions) and
  1743. not (oso_sparse_data in ObjData.CurrObjSec.secoptions) then
  1744. Message(asmw_e_alloc_data_only_in_bss);
  1745. {$ifdef USE_COMM_IN_BSS}
  1746. if writingpackages and
  1747. Tai_datablock(hp).is_global then
  1748. begin
  1749. objsym:=ObjData.SymbolDefine(Tai_datablock(hp).sym);
  1750. objsym.size:=Tai_datablock(hp).size;
  1751. objsym.bind:=AB_COMMON;
  1752. objsym.alignment:=needtowritealignmentalsoforELF;
  1753. end
  1754. else
  1755. {$endif USE_COMM_IN_BSS}
  1756. begin
  1757. ObjData.allocalign(used_align(size_2_align(Tai_datablock(hp).size),0,ObjData.CurrObjSec.secalign));
  1758. objsym:=ObjData.SymbolDefine(Tai_datablock(hp).sym);
  1759. objsym.size:=Tai_datablock(hp).size;
  1760. ObjData.alloc(Tai_datablock(hp).size);
  1761. end;
  1762. end;
  1763. ait_realconst:
  1764. ObjData.alloc(tai_realconst(hp).savesize);
  1765. ait_const:
  1766. begin
  1767. { Recalculate relative symbols }
  1768. if assigned(tai_const(hp).sym) and
  1769. assigned(tai_const(hp).endsym) then
  1770. begin
  1771. objsym:=Objdata.SymbolRef(tai_const(hp).sym);
  1772. objsymend:=Objdata.SymbolRef(tai_const(hp).endsym);
  1773. if Tai_const(hp).consttype in [aitconst_gottpoff,aitconst_tlsgd,aitconst_tlsdesc] then
  1774. begin
  1775. if objsymend.objsection<>ObjData.CurrObjSec then
  1776. Internalerror(2019092801);
  1777. Tai_const(hp).value:=objsymend.address-ObjData.CurrObjSec.Size+Tai_const(hp).symofs;
  1778. end
  1779. else if objsymend.objsection<>objsym.objsection then
  1780. begin
  1781. if (Tai_const(hp).consttype in [aitconst_uleb128bit,aitconst_sleb128bit]) or
  1782. (objsym.objsection<>ObjData.CurrObjSec) then
  1783. internalerror(200905042);
  1784. end
  1785. {$push} {$R-}{$Q-}
  1786. else
  1787. Tai_const(hp).value:=objsymend.address-objsym.address+Tai_const(hp).symofs;
  1788. end;
  1789. {$pop}
  1790. if (Tai_const(hp).consttype in [aitconst_uleb128bit,aitconst_sleb128bit]) then
  1791. Tai_const(hp).fixsize;
  1792. ObjData.alloc(tai_const(hp).size);
  1793. end;
  1794. ait_section:
  1795. begin
  1796. { use cached value }
  1797. ObjData.setsection(Tai_section(hp).sec);
  1798. end;
  1799. ait_stab :
  1800. begin
  1801. if assigned(Tai_stab(hp).str) then
  1802. WriteStab(Tai_stab(hp).str);
  1803. end;
  1804. ait_symbol :
  1805. ObjData.SymbolDefine(Tai_symbol(hp).sym);
  1806. ait_symbol_end :
  1807. begin
  1808. objsym:=ObjData.SymbolRef(Tai_symbol_end(hp).sym);
  1809. objsym.size:=ObjData.CurrObjSec.Size-objsym.offset;
  1810. end;
  1811. ait_symbolpair:
  1812. with tai_symbolpair(hp) do
  1813. ObjData.SymbolPairDefine(kind,sym^,value^);
  1814. ait_label :
  1815. ObjData.SymbolDefine(Tai_label(hp).labsym);
  1816. ait_string :
  1817. ObjData.alloc(Tai_string(hp).len);
  1818. ait_instruction :
  1819. ObjData.alloc(Taicpu(hp).Pass1(ObjData));
  1820. ait_cutobject :
  1821. if SmartAsm then
  1822. break;
  1823. ait_directive :
  1824. begin
  1825. case tai_directive(hp).directive of
  1826. asd_indirect_symbol:
  1827. if tai_directive(hp).name='' then
  1828. Internalerror(2009101103)
  1829. else if not SetIndirectToSymbol(Tai(hp.Previous), tai_directive(hp).name) then
  1830. Internalerror(2009101102);
  1831. asd_lazy_reference:
  1832. { handled in TreePass0 }
  1833. ;
  1834. asd_reference:
  1835. { ignore for now, but should be added}
  1836. ;
  1837. asd_thumb_func:
  1838. { ignore for now, but should be added}
  1839. ;
  1840. asd_force_thumb:
  1841. { ignore for now, but should be added}
  1842. ;
  1843. asd_code:
  1844. { ignore for now, but should be added}
  1845. ;
  1846. asd_option:
  1847. { ignore for now, but should be added}
  1848. ;
  1849. asd_weak_definition:
  1850. { ignore for now, but should be added}
  1851. ;
  1852. {$ifdef OMFOBJSUPPORT}
  1853. asd_omf_linnum_line:
  1854. { ignore for now, but should be added}
  1855. ;
  1856. {$endif OMFOBJSUPPORT}
  1857. asd_cpu:
  1858. begin
  1859. ObjData.CPUType:=cpu_none;
  1860. for cpu:=low(tcputype) to high(tcputype) do
  1861. if cputypestr[cpu]=tai_directive(hp).name then
  1862. begin
  1863. ObjData.CPUType:=cpu;
  1864. break;
  1865. end;
  1866. end;
  1867. else
  1868. internalerror(2010011102);
  1869. end;
  1870. end;
  1871. ait_eabi_attribute :
  1872. begin
  1873. eabi_section:=ObjData.findsection('.ARM.attributes');
  1874. if not(assigned(eabi_section)) then
  1875. Internalerror(2019100702);
  1876. if eabi_section.Size=0 then
  1877. eabi_section.alloc(16);
  1878. eabi_section.alloc(LengthUleb128(tai_eabi_attribute(hp).tag));
  1879. case tai_eabi_attribute(hp).eattr_typ of
  1880. eattrtype_dword:
  1881. eabi_section.alloc(LengthUleb128(tai_eabi_attribute(hp).value));
  1882. eattrtype_ntbs:
  1883. if assigned(tai_eabi_attribute(hp).valuestr) then
  1884. eabi_section.alloc(Length(tai_eabi_attribute(hp).valuestr^)+1)
  1885. else
  1886. eabi_section.alloc(1);
  1887. else
  1888. Internalerror(2019100703);
  1889. end;
  1890. end;
  1891. else
  1892. ;
  1893. end;
  1894. hp:=Tai(hp.next);
  1895. end;
  1896. TreePass1:=hp;
  1897. end;
  1898. function TInternalAssembler.TreePass2(hp:Tai):Tai;
  1899. var
  1900. fillbuffer : tfillbuffer;
  1901. leblen : byte;
  1902. lebbuf : array[0..63] of byte;
  1903. objsym,
  1904. ref,
  1905. objsymend : TObjSymbol;
  1906. zerobuf : array[0..63] of byte;
  1907. relative_reloc: boolean;
  1908. pdata : pointer;
  1909. ssingle : single;
  1910. ddouble : double;
  1911. {$if defined(cpuextended) and defined(FPC_HAS_TYPE_EXTENDED)}
  1912. eextended : extended;
  1913. {$else}
  1914. {$ifdef FPC_SOFT_FPUX80}
  1915. eextended : floatx80;
  1916. {$endif}
  1917. {$endif}
  1918. ccomp : comp;
  1919. tmp : word;
  1920. cpu: tcputype;
  1921. ddword : dword;
  1922. eabi_section: TObjSection;
  1923. s: String;
  1924. TmpDataPos: TObjSectionOfs;
  1925. begin
  1926. fillchar(zerobuf,sizeof(zerobuf),0);
  1927. fillchar(objsym,sizeof(objsym),0);
  1928. fillchar(objsymend,sizeof(objsymend),0);
  1929. { main loop }
  1930. while assigned(hp) do
  1931. begin
  1932. {$ifdef DEBUG}
  1933. if not(hp.typ in SkipLineInfo) then
  1934. current_filepos:=tailineinfo(hp).fileinfo;
  1935. {$endif DEBUG}
  1936. case hp.typ of
  1937. ait_align :
  1938. begin
  1939. if tai_align_abstract(hp).aligntype>ObjData.CurrObjSec.secalign then
  1940. InternalError(2012072301);
  1941. if oso_data in ObjData.CurrObjSec.secoptions then
  1942. ObjData.writebytes(Tai_align_abstract(hp).calculatefillbuf(fillbuffer,oso_executable in ObjData.CurrObjSec.secoptions)^,
  1943. Tai_align_abstract(hp).fillsize)
  1944. else
  1945. ObjData.alloc(Tai_align_abstract(hp).fillsize);
  1946. end;
  1947. ait_section :
  1948. begin
  1949. { use cached value }
  1950. ObjData.setsection(Tai_section(hp).sec);
  1951. end;
  1952. ait_symbol :
  1953. begin
  1954. ObjOutput.exportsymbol(ObjData.SymbolRef(Tai_symbol(hp).sym));
  1955. end;
  1956. ait_symbol_end :
  1957. begin
  1958. { recalculate size, as some preceding instructions
  1959. could have been changed to smaller size }
  1960. objsym:=ObjData.SymbolRef(Tai_symbol_end(hp).sym);
  1961. objsym.size:=ObjData.CurrObjSec.Size-objsym.offset;
  1962. end;
  1963. ait_datablock :
  1964. begin
  1965. ObjOutput.exportsymbol(ObjData.SymbolRef(Tai_datablock(hp).sym));
  1966. {$ifdef USE_COMM_IN_BSS}
  1967. if not(writingpackages and
  1968. Tai_datablock(hp).is_global) then
  1969. {$endif USE_COMM_IN_BSS}
  1970. begin
  1971. ObjData.allocalign(used_align(size_2_align(Tai_datablock(hp).size),0,ObjData.CurrObjSec.secalign));
  1972. ObjData.alloc(Tai_datablock(hp).size);
  1973. end;
  1974. end;
  1975. ait_realconst:
  1976. begin
  1977. case tai_realconst(hp).realtyp of
  1978. aitrealconst_s32bit:
  1979. begin
  1980. ssingle:=single(tai_realconst(hp).value.s32val);
  1981. pdata:=@ssingle;
  1982. end;
  1983. aitrealconst_s64bit:
  1984. begin
  1985. ddouble:=double(tai_realconst(hp).value.s64val);
  1986. pdata:=@ddouble;
  1987. end;
  1988. {$if defined(cpuextended) and defined(FPC_HAS_TYPE_EXTENDED)}
  1989. { can't write full 80 bit floating point constants yet on non-x86 }
  1990. aitrealconst_s80bit:
  1991. begin
  1992. eextended:=extended(tai_realconst(hp).value.s80val);
  1993. pdata:=@eextended;
  1994. end;
  1995. {$else}
  1996. {$ifdef FPC_SOFT_FPUX80}
  1997. {$push}{$warn 6018 off} { Unreachable code due to compile time evaluation }
  1998. aitrealconst_s80bit:
  1999. begin
  2000. if sizeof(tai_realconst(hp).value.s80val) = sizeof(double) then
  2001. eextended:=float64_to_floatx80(float64(double(tai_realconst(hp).value.s80val)))
  2002. else if sizeof(tai_realconst(hp).value.s80val) = sizeof(single) then
  2003. eextended:=float32_to_floatx80(float32(single(tai_realconst(hp).value.s80val)))
  2004. else
  2005. internalerror(2017091903);
  2006. pdata:=@eextended;
  2007. end;
  2008. {$pop}
  2009. {$endif}
  2010. {$endif cpuextended}
  2011. aitrealconst_s64comp:
  2012. begin
  2013. ccomp:=comp(tai_realconst(hp).value.s64compval);
  2014. pdata:=@ccomp;
  2015. end;
  2016. else
  2017. internalerror(2015030501);
  2018. end;
  2019. ObjData.writebytes(pdata^,tai_realconst(hp).datasize);
  2020. ObjData.writebytes(zerobuf,tai_realconst(hp).savesize-tai_realconst(hp).datasize);
  2021. end;
  2022. ait_string :
  2023. ObjData.writebytes(Tai_string(hp).str^,Tai_string(hp).len);
  2024. ait_const :
  2025. begin
  2026. { Recalculate relative symbols, addresses of forward references
  2027. can be changed in treepass1 }
  2028. relative_reloc:=false;
  2029. if assigned(tai_const(hp).sym) and
  2030. assigned(tai_const(hp).endsym) then
  2031. begin
  2032. objsym:=Objdata.SymbolRef(tai_const(hp).sym);
  2033. objsymend:=Objdata.SymbolRef(tai_const(hp).endsym);
  2034. relative_reloc:=(objsym.objsection<>objsymend.objsection);
  2035. if Tai_const(hp).consttype in [aitconst_gottpoff] then
  2036. begin
  2037. if objsymend.objsection<>ObjData.CurrObjSec then
  2038. Internalerror(2019092802);
  2039. Tai_const(hp).value:=objsymend.address-ObjData.CurrObjSec.Size+Tai_const(hp).symofs;
  2040. end
  2041. else if Tai_const(hp).consttype in [aitconst_tlsgd,aitconst_tlsdesc] then
  2042. begin
  2043. if objsymend.objsection<>ObjData.CurrObjSec then
  2044. Internalerror(2019092803);
  2045. Tai_const(hp).value:=ObjData.CurrObjSec.Size-objsymend.address+Tai_const(hp).symofs;
  2046. end
  2047. else if objsymend.objsection<>objsym.objsection then
  2048. begin
  2049. if (Tai_const(hp).consttype in [aitconst_uleb128bit,aitconst_sleb128bit]) or
  2050. (objsym.objsection<>ObjData.CurrObjSec) then
  2051. internalerror(2019010301);
  2052. end
  2053. else
  2054. {$push} {$R-}{$Q-}
  2055. Tai_const(hp).value:=objsymend.address-objsym.address+Tai_const(hp).symofs;
  2056. end;
  2057. {$pop}
  2058. case tai_const(hp).consttype of
  2059. aitconst_64bit,
  2060. aitconst_32bit,
  2061. aitconst_16bit,
  2062. aitconst_64bit_unaligned,
  2063. aitconst_32bit_unaligned,
  2064. aitconst_16bit_unaligned,
  2065. aitconst_8bit :
  2066. begin
  2067. if assigned(tai_const(hp).sym) and
  2068. not assigned(tai_const(hp).endsym) then
  2069. ObjData.writereloc(Tai_const(hp).symofs,tai_const(hp).size,Objdata.SymbolRef(tai_const(hp).sym),RELOC_ABSOLUTE)
  2070. else if relative_reloc then
  2071. ObjData.writereloc(ObjData.CurrObjSec.size+tai_const(hp).size-objsym.address+tai_const(hp).symofs,tai_const(hp).size,objsymend,RELOC_RELATIVE)
  2072. else
  2073. ObjData.writebytes(Tai_const(hp).value,tai_const(hp).size);
  2074. end;
  2075. aitconst_rva_symbol :
  2076. begin
  2077. { PE32+? }
  2078. if target_info.system in systems_peoptplus then
  2079. ObjData.writereloc(Tai_const(hp).symofs,sizeof(longint),Objdata.SymbolRef(tai_const(hp).sym),RELOC_RVA)
  2080. else
  2081. ObjData.writereloc(Tai_const(hp).symofs,sizeof(pint),Objdata.SymbolRef(tai_const(hp).sym),RELOC_RVA);
  2082. end;
  2083. aitconst_secrel32_symbol :
  2084. begin
  2085. { Required for DWARF2 support under Windows }
  2086. ObjData.writereloc(Tai_const(hp).symofs,sizeof(longint),Objdata.SymbolRef(tai_const(hp).sym),RELOC_SECREL32);
  2087. end;
  2088. {$ifdef i8086}
  2089. aitconst_farptr :
  2090. if assigned(tai_const(hp).sym) and
  2091. not assigned(tai_const(hp).endsym) then
  2092. ObjData.writereloc(Tai_const(hp).symofs,tai_const(hp).size,Objdata.SymbolRef(tai_const(hp).sym),RELOC_FARPTR)
  2093. else if relative_reloc then
  2094. internalerror(2015040601)
  2095. else
  2096. ObjData.writebytes(Tai_const(hp).value,tai_const(hp).size);
  2097. aitconst_seg:
  2098. if assigned(tai_const(hp).sym) and (tai_const(hp).size=2) then
  2099. ObjData.writereloc(0,2,Objdata.SymbolRef(tai_const(hp).sym),RELOC_SEG)
  2100. else
  2101. internalerror(2015110502);
  2102. aitconst_dgroup:
  2103. ObjData.writereloc(0,2,nil,RELOC_DGROUP);
  2104. aitconst_fardataseg:
  2105. ObjData.writereloc(0,2,nil,RELOC_FARDATASEG);
  2106. {$endif i8086}
  2107. {$ifdef arm}
  2108. aitconst_got:
  2109. ObjData.writereloc(Tai_const(hp).symofs,sizeof(longint),Objdata.SymbolRef(tai_const(hp).sym),RELOC_GOT32);
  2110. { aitconst_gottpoff:
  2111. ObjData.writereloc(Tai_const(hp).symofs,sizeof(longint),Objdata.SymbolRef(tai_const(hp).sym),RELOC_TPOFF); }
  2112. aitconst_tpoff:
  2113. ObjData.writereloc(Tai_const(hp).symofs,sizeof(longint),Objdata.SymbolRef(tai_const(hp).sym),RELOC_TPOFF);
  2114. aitconst_tlsgd:
  2115. ObjData.writereloc(Tai_const(hp).symofs,sizeof(longint),Objdata.SymbolRef(tai_const(hp).sym),RELOC_TLSGD);
  2116. aitconst_tlsdesc:
  2117. begin
  2118. { must be a relative symbol, thus value being valid }
  2119. if not(assigned(tai_const(hp).sym)) or not(assigned(tai_const(hp).endsym)) then
  2120. Internalerror(2019092904);
  2121. ObjData.writereloc(Tai_const(hp).value,sizeof(longint),Objdata.SymbolRef(tai_const(hp).sym),RELOC_TLSDESC);
  2122. end;
  2123. {$endif arm}
  2124. aitconst_dtpoff:
  2125. { so far, the size of dtpoff is fixed to 4 bytes }
  2126. ObjData.writereloc(Tai_const(hp).symofs,4,Objdata.SymbolRef(tai_const(hp).sym),RELOC_DTPOFF);
  2127. aitconst_gotoff_symbol:
  2128. ObjData.writereloc(Tai_const(hp).symofs,sizeof(longint),Objdata.SymbolRef(tai_const(hp).sym),RELOC_GOTOFF);
  2129. aitconst_uleb128bit,
  2130. aitconst_sleb128bit :
  2131. begin
  2132. if Tai_const(hp).fixed_size=0 then
  2133. Internalerror(2019030302);
  2134. if tai_const(hp).consttype=aitconst_uleb128bit then
  2135. leblen:=EncodeUleb128(qword(Tai_const(hp).value),lebbuf,Tai_const(hp).fixed_size)
  2136. else
  2137. leblen:=EncodeSleb128(Tai_const(hp).value,lebbuf,Tai_const(hp).fixed_size);
  2138. if leblen<>tai_const(hp).fixed_size then
  2139. internalerror(200709271);
  2140. ObjData.writebytes(lebbuf,leblen);
  2141. end;
  2142. aitconst_darwin_dwarf_delta32,
  2143. aitconst_darwin_dwarf_delta64:
  2144. ObjData.writebytes(Tai_const(hp).value,tai_const(hp).size);
  2145. aitconst_half16bit,
  2146. aitconst_gs:
  2147. begin
  2148. tmp:=Tai_const(hp).value div 2;
  2149. ObjData.writebytes(tmp,2);
  2150. end;
  2151. else
  2152. internalerror(200603254);
  2153. end;
  2154. end;
  2155. ait_label :
  2156. begin
  2157. { exporting shouldn't be necessary as labels are local,
  2158. but it's better to be on the safe side (PFV) }
  2159. ObjOutput.exportsymbol(ObjData.SymbolRef(Tai_label(hp).labsym));
  2160. end;
  2161. ait_instruction :
  2162. Taicpu(hp).Pass2(ObjData);
  2163. ait_stab :
  2164. WriteStab(Tai_stab(hp).str);
  2165. ait_function_name,
  2166. ait_force_line : ;
  2167. ait_cutobject :
  2168. if SmartAsm then
  2169. break;
  2170. ait_directive :
  2171. begin
  2172. case tai_directive(hp).directive of
  2173. asd_weak_definition,
  2174. asd_weak_reference:
  2175. begin
  2176. objsym:=ObjData.symbolref(tai_directive(hp).name);
  2177. if objsym.bind in [AB_EXTERNAL,AB_WEAK_EXTERNAL] then
  2178. objsym.bind:=AB_WEAK_EXTERNAL
  2179. else
  2180. { TODO: should become a weak definition; for now, do
  2181. the same as what was done for ait_weak }
  2182. objsym.bind:=AB_WEAK_EXTERNAL;
  2183. end;
  2184. asd_cpu:
  2185. begin
  2186. ObjData.CPUType:=cpu_none;
  2187. for cpu:=low(tcputype) to high(tcputype) do
  2188. if cputypestr[cpu]=tai_directive(hp).name then
  2189. begin
  2190. ObjData.CPUType:=cpu;
  2191. break;
  2192. end;
  2193. end;
  2194. {$ifdef OMFOBJSUPPORT}
  2195. asd_omf_linnum_line:
  2196. begin
  2197. TOmfObjSection(ObjData.CurrObjSec).LinNumEntries.Add(
  2198. TOmfSubRecord_LINNUM_MsLink_Entry.Create(
  2199. strtoint(tai_directive(hp).name),
  2200. ObjData.CurrObjSec.Size
  2201. ));
  2202. end;
  2203. {$endif OMFOBJSUPPORT}
  2204. else
  2205. ;
  2206. end
  2207. end;
  2208. ait_symbolpair:
  2209. begin
  2210. if tai_symbolpair(hp).kind=spk_set then
  2211. begin
  2212. objsym:=ObjData.symbolref(tai_symbolpair(hp).sym^);
  2213. ref:=objdata.symbolref(tai_symbolpair(hp).value^);
  2214. objsym.offset:=ref.offset;
  2215. objsym.objsection:=ref.objsection;
  2216. {$ifdef arm}
  2217. objsym.ThumbFunc:=ref.ThumbFunc;
  2218. {$endif arm}
  2219. end;
  2220. end;
  2221. {$ifndef DISABLE_WIN64_SEH}
  2222. ait_seh_directive :
  2223. tai_seh_directive(hp).generate_code(objdata);
  2224. {$endif DISABLE_WIN64_SEH}
  2225. ait_eabi_attribute :
  2226. begin
  2227. eabi_section:=ObjData.findsection('.ARM.attributes');
  2228. if not(assigned(eabi_section)) then
  2229. Internalerror(2019100704);
  2230. if eabi_section.Size=0 then
  2231. begin
  2232. s:='A';
  2233. eabi_section.write(s[1],1);
  2234. ddword:=eabi_section.Size-1;
  2235. eabi_section.write(ddword,4);
  2236. s:='aeabi'#0;
  2237. eabi_section.write(s[1],6);
  2238. s:=#1;
  2239. eabi_section.write(s[1],1);
  2240. ddword:=eabi_section.Size-1-4-6-1;
  2241. eabi_section.write(ddword,4);
  2242. end;
  2243. leblen:=EncodeUleb128(tai_eabi_attribute(hp).tag,lebbuf,0);
  2244. eabi_section.write(lebbuf,leblen);
  2245. case tai_eabi_attribute(hp).eattr_typ of
  2246. eattrtype_dword:
  2247. begin
  2248. leblen:=EncodeUleb128(tai_eabi_attribute(hp).value,lebbuf,0);
  2249. eabi_section.write(lebbuf,leblen);
  2250. end;
  2251. eattrtype_ntbs:
  2252. begin
  2253. if assigned(tai_eabi_attribute(hp).valuestr) then
  2254. s:=tai_eabi_attribute(hp).valuestr^+#0
  2255. else
  2256. s:=#0;
  2257. eabi_section.write(s[1],Length(s));
  2258. end
  2259. else
  2260. Internalerror(2019100705);
  2261. end;
  2262. { update size of attributes section, write directly to the dyn. arrays as
  2263. we do not increase the size of section }
  2264. TmpDataPos:=eabi_section.Data.Pos;
  2265. eabi_section.Data.seek(1);
  2266. ddword:=eabi_section.Size-1;
  2267. eabi_section.Data.write(ddword,4);
  2268. eabi_section.Data.seek(12);
  2269. ddword:=eabi_section.Size-1-4-6;
  2270. eabi_section.Data.write(ddword,4);
  2271. eabi_section.Data.Seek(TmpDataPos);
  2272. end;
  2273. else
  2274. ;
  2275. end;
  2276. hp:=Tai(hp.next);
  2277. end;
  2278. TreePass2:=hp;
  2279. end;
  2280. procedure TInternalAssembler.writetree;
  2281. label
  2282. doexit;
  2283. var
  2284. hp : Tai;
  2285. ObjWriter : TObjectWriter;
  2286. begin
  2287. ObjWriter:=TObjectwriter.create;
  2288. ObjOutput:=CObjOutput.Create(ObjWriter);
  2289. ObjData:=ObjOutput.newObjData(ObjFileName);
  2290. { Pass 0 }
  2291. ObjData.currpass:=0;
  2292. ObjData.createsection(sec_code);
  2293. ObjData.beforealloc;
  2294. { start with list 1 }
  2295. currlistidx:=1;
  2296. currlist:=list[currlistidx];
  2297. hp:=Tai(currList.first);
  2298. while assigned(hp) do
  2299. begin
  2300. hp:=TreePass0(hp);
  2301. MaybeNextList(hp);
  2302. end;
  2303. ObjData.afteralloc;
  2304. { leave if errors have occurred }
  2305. if errorcount>0 then
  2306. goto doexit;
  2307. { Pass 1 }
  2308. ObjData.currpass:=1;
  2309. ObjData.resetsections;
  2310. ObjData.beforealloc;
  2311. ObjData.createsection(sec_code);
  2312. { start with list 1 }
  2313. currlistidx:=1;
  2314. currlist:=list[currlistidx];
  2315. hp:=Tai(currList.first);
  2316. while assigned(hp) do
  2317. begin
  2318. hp:=TreePass1(hp);
  2319. MaybeNextList(hp);
  2320. end;
  2321. ObjData.createsection(sec_code);
  2322. ObjData.afteralloc;
  2323. { leave if errors have occurred }
  2324. if errorcount>0 then
  2325. goto doexit;
  2326. { Pass 2 }
  2327. ObjData.currpass:=2;
  2328. ObjData.resetsections;
  2329. ObjData.beforewrite;
  2330. ObjData.createsection(sec_code);
  2331. { start with list 1 }
  2332. currlistidx:=1;
  2333. currlist:=list[currlistidx];
  2334. hp:=Tai(currList.first);
  2335. while assigned(hp) do
  2336. begin
  2337. hp:=TreePass2(hp);
  2338. MaybeNextList(hp);
  2339. end;
  2340. ObjData.createsection(sec_code);
  2341. ObjData.afterwrite;
  2342. { don't write the .o file if errors have occurred }
  2343. if errorcount=0 then
  2344. begin
  2345. { write objectfile }
  2346. ObjOutput.startobjectfile(ObjFileName);
  2347. ObjOutput.writeobjectfile(ObjData);
  2348. end;
  2349. doexit:
  2350. { Cleanup }
  2351. ObjData.free;
  2352. ObjData:=nil;
  2353. ObjWriter.free;
  2354. end;
  2355. procedure TInternalAssembler.writetreesmart;
  2356. var
  2357. hp : Tai;
  2358. startsectype : TAsmSectiontype;
  2359. place: tcutplace;
  2360. ObjWriter : TObjectWriter;
  2361. startsecname: String;
  2362. startsecorder: TAsmSectionOrder;
  2363. begin
  2364. if not(cs_asm_leave in current_settings.globalswitches) and
  2365. not(af_needar in asminfo^.flags) then
  2366. ObjWriter:=CInternalAr.CreateAr(current_module.staticlibfilename)
  2367. else
  2368. ObjWriter:=TObjectwriter.create;
  2369. NextSmartName(cut_normal);
  2370. ObjOutput:=CObjOutput.Create(ObjWriter);
  2371. startsectype:=sec_none;
  2372. startsecname:='';
  2373. startsecorder:=secorder_default;
  2374. { start with list 1 }
  2375. currlistidx:=1;
  2376. currlist:=list[currlistidx];
  2377. hp:=Tai(currList.first);
  2378. while assigned(hp) do
  2379. begin
  2380. ObjData:=ObjOutput.newObjData(ObjFileName);
  2381. { Pass 0 }
  2382. ObjData.currpass:=0;
  2383. ObjData.resetsections;
  2384. ObjData.beforealloc;
  2385. if startsectype<>sec_none then
  2386. ObjData.CreateSection(startsectype,startsecname,startsecorder);
  2387. TreePass0(hp);
  2388. ObjData.afteralloc;
  2389. { leave if errors have occurred }
  2390. if errorcount>0 then
  2391. break;
  2392. { Pass 1 }
  2393. ObjData.currpass:=1;
  2394. ObjData.resetsections;
  2395. ObjData.beforealloc;
  2396. if startsectype<>sec_none then
  2397. ObjData.CreateSection(startsectype,startsecname,startsecorder);
  2398. TreePass1(hp);
  2399. ObjData.afteralloc;
  2400. { leave if errors have occurred }
  2401. if errorcount>0 then
  2402. break;
  2403. { Pass 2 }
  2404. ObjData.currpass:=2;
  2405. ObjOutput.startobjectfile(ObjFileName);
  2406. ObjData.resetsections;
  2407. ObjData.beforewrite;
  2408. if startsectype<>sec_none then
  2409. ObjData.CreateSection(startsectype,startsecname,startsecorder);
  2410. hp:=TreePass2(hp);
  2411. ObjData.afterwrite;
  2412. { leave if errors have occurred }
  2413. if errorcount>0 then
  2414. break;
  2415. { write the current objectfile }
  2416. ObjOutput.writeobjectfile(ObjData);
  2417. ObjData.free;
  2418. ObjData:=nil;
  2419. { end of lists? }
  2420. if not MaybeNextList(hp) then
  2421. break;
  2422. { we will start a new objectfile so reset everything }
  2423. { The place can still change in the next while loop, so don't init }
  2424. { the writer yet (JM) }
  2425. if (hp.typ=ait_cutobject) then
  2426. place := Tai_cutobject(hp).place
  2427. else
  2428. place := cut_normal;
  2429. { avoid empty files }
  2430. startsectype:=sec_none;
  2431. startsecname:='';
  2432. startsecorder:=secorder_default;
  2433. while assigned(hp) and
  2434. (Tai(hp).typ in [ait_marker,ait_comment,ait_section,ait_cutobject]) do
  2435. begin
  2436. if Tai(hp).typ=ait_section then
  2437. begin
  2438. startsectype:=Tai_section(hp).sectype;
  2439. startsecname:=Tai_section(hp).name^;
  2440. startsecorder:=Tai_section(hp).secorder;
  2441. end;
  2442. if (Tai(hp).typ=ait_cutobject) then
  2443. place:=Tai_cutobject(hp).place;
  2444. hp:=Tai(hp.next);
  2445. end;
  2446. if not MaybeNextList(hp) then
  2447. break;
  2448. { start next objectfile }
  2449. NextSmartName(place);
  2450. end;
  2451. ObjData.free;
  2452. ObjData:=nil;
  2453. ObjWriter.free;
  2454. end;
  2455. procedure TInternalAssembler.MakeObject;
  2456. var to_do:set of TasmlistType;
  2457. i:TasmlistType;
  2458. procedure addlist(p:TAsmList);
  2459. begin
  2460. inc(lists);
  2461. list[lists]:=p;
  2462. end;
  2463. begin
  2464. to_do:=[low(Tasmlisttype)..high(Tasmlisttype)];
  2465. if usedeffileforexports then
  2466. exclude(to_do,al_exports);
  2467. if not(tf_section_threadvars in target_info.flags) then
  2468. exclude(to_do,al_threadvars);
  2469. for i:=low(TasmlistType) to high(TasmlistType) do
  2470. if (i in to_do) and (current_asmdata.asmlists[i]<>nil) and
  2471. (not current_asmdata.asmlists[i].empty) then
  2472. addlist(current_asmdata.asmlists[i]);
  2473. if SmartAsm then
  2474. writetreesmart
  2475. else
  2476. writetree;
  2477. end;
  2478. {*****************************************************************************
  2479. Generate Assembler Files Main Procedure
  2480. *****************************************************************************}
  2481. Procedure GenerateAsm(smart:boolean);
  2482. var
  2483. a : TAssembler;
  2484. begin
  2485. if not assigned(CAssembler[target_asm.id]) then
  2486. Message(asmw_f_assembler_output_not_supported);
  2487. a:=CAssembler[target_asm.id].Create(@target_asm,smart);
  2488. a.MakeObject;
  2489. a.Free;
  2490. end;
  2491. function GetExternalGnuAssemblerWithAsmInfoWriter(info: pasminfo; wr: TExternalAssemblerOutputFile): TExternalAssembler;
  2492. var
  2493. asmkind: tasm;
  2494. begin
  2495. for asmkind in [as_gas,as_ggas,as_darwin,as_clang_gas,as_clang_asdarwin] do
  2496. if assigned(asminfos[asmkind]) and
  2497. (target_info.system in asminfos[asmkind]^.supported_targets) then
  2498. begin
  2499. result:=TExternalAssemblerClass(CAssembler[asmkind]).CreateWithWriter(asminfos[asmkind],wr,false,false);
  2500. exit;
  2501. end;
  2502. Internalerror(2015090604);
  2503. end;
  2504. {*****************************************************************************
  2505. Init/Done
  2506. *****************************************************************************}
  2507. procedure RegisterAssembler(const r:tasminfo;c:TAssemblerClass);
  2508. var
  2509. t : tasm;
  2510. begin
  2511. t:=r.id;
  2512. if assigned(asminfos[t]) then
  2513. writeln('Warning: Assembler is already registered!')
  2514. else
  2515. Getmem(asminfos[t],sizeof(tasminfo));
  2516. asminfos[t]^:=r;
  2517. CAssembler[t]:=c;
  2518. end;
  2519. end.