assemble.pas 79 KB

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