assemble.pas 78 KB

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