assemble.pas 78 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351
  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}
  214. {$endif}
  215. script,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. aitrealconst_s80bit:
  935. begin
  936. if sizeof(tai_realconst(hp).value.s80val) = sizeof(double) then
  937. writer.AsmWriteLn(asminfo^.comment+'value: '+double2str(tai_realconst(hp).value.s80val))
  938. else if sizeof(tai_realconst(hp).value.s80val) = sizeof(single) then
  939. writer.AsmWriteLn(asminfo^.comment+'value: '+single2str(tai_realconst(hp).value.s80val))
  940. else
  941. internalerror(2017091901);
  942. end;
  943. {$endif}
  944. {$endif cpuextended}
  945. aitrealconst_s64comp:
  946. writer.AsmWriteLn(asminfo^.comment+'value: '+extended2str(tai_realconst(hp).value.s64compval));
  947. else
  948. internalerror(2014050604);
  949. end;
  950. end;
  951. writer.AsmWrite(dbdir);
  952. { generic float writing code: get start address of value, then write
  953. byte by byte. Can't use fields directly, because e.g ts64comp is
  954. defined as extended on x86 }
  955. case tai_realconst(hp).realtyp of
  956. aitrealconst_s32bit:
  957. begin
  958. ssingle:=single(tai_realconst(hp).value.s32val);
  959. pdata:=@ssingle;
  960. end;
  961. aitrealconst_s64bit:
  962. begin
  963. ddouble:=double(tai_realconst(hp).value.s64val);
  964. pdata:=@ddouble;
  965. end;
  966. {$if defined(cpuextended) and defined(FPC_HAS_TYPE_EXTENDED)}
  967. { can't write full 80 bit floating point constants yet on non-x86 }
  968. aitrealconst_s80bit:
  969. begin
  970. eextended:=extended(tai_realconst(hp).value.s80val);
  971. pdata:=@eextended;
  972. end;
  973. {$else}
  974. {$ifdef FPC_SOFT_FPUX80}
  975. aitrealconst_s80bit:
  976. begin
  977. if sizeof(tai_realconst(hp).value.s80val) = sizeof(double) then
  978. eextended:=float64_to_floatx80(float64(double(tai_realconst(hp).value.s80val)))
  979. else if sizeof(tai_realconst(hp).value.s80val) = sizeof(single) then
  980. eextended:=float32_to_floatx80(float32(single(tai_realconst(hp).value.s80val)))
  981. else
  982. internalerror(2017091901);
  983. pdata:=@eextended;
  984. end;
  985. {$endif}
  986. {$endif cpuextended}
  987. aitrealconst_s64comp:
  988. begin
  989. ccomp:=comp(tai_realconst(hp).value.s64compval);
  990. pdata:=@ccomp;
  991. end;
  992. else
  993. internalerror(2014051001);
  994. end;
  995. count:=tai_realconst(hp).datasize;
  996. { write bytes in inverse order if source and target endianess don't
  997. match }
  998. if source_info.endian<>target_info.endian then
  999. begin
  1000. { go from back to front }
  1001. index:=count-1;
  1002. step:=-1;
  1003. end
  1004. else
  1005. begin
  1006. index:=0;
  1007. step:=1;
  1008. end;
  1009. {$ifdef ARM}
  1010. { ARM-specific: low and high dwords of a double may be swapped }
  1011. if tai_realconst(hp).formatoptions=fo_hiloswapped then
  1012. begin
  1013. { only supported for double }
  1014. if tai_realconst(hp).datasize<>8 then
  1015. internalerror(2014050605);
  1016. { switch bit of the index so that the words are written in
  1017. the opposite order }
  1018. swapmask:=4;
  1019. end
  1020. else
  1021. {$endif ARM}
  1022. swapmask:=0;
  1023. repeat
  1024. writer.AsmWrite(tostr(pdata[index xor swapmask]));
  1025. inc(index,step);
  1026. dec(count);
  1027. if count<>0 then
  1028. writer.AsmWrite(',');
  1029. until count=0;
  1030. { padding }
  1031. for count:=tai_realconst(hp).datasize+1 to tai_realconst(hp).savesize do
  1032. writer.AsmWrite(',0');
  1033. writer.AsmLn;
  1034. end;
  1035. procedure TExternalAssembler.WriteTree(p:TAsmList);
  1036. begin
  1037. end;
  1038. procedure TExternalAssembler.WriteAsmList;
  1039. begin
  1040. end;
  1041. procedure TExternalAssembler.MakeObject;
  1042. begin
  1043. writer.AsmCreate(cut_normal);
  1044. FillChar(lastfileinfo, sizeof(lastfileinfo), 0);
  1045. lastfileinfo.line := -1;
  1046. lastinfile := nil;
  1047. lastsectype := sec_none;
  1048. WriteAsmList;
  1049. writer.AsmClose;
  1050. if not(writer.ioerror) then
  1051. DoAssemble;
  1052. end;
  1053. destructor TExternalAssembler.Destroy;
  1054. begin
  1055. if ffreewriter then
  1056. writer.Free;
  1057. inherited;
  1058. end;
  1059. {*****************************************************************************
  1060. TInternalAssembler
  1061. *****************************************************************************}
  1062. constructor TInternalAssembler.Create(info: pasminfo; smart: boolean);
  1063. begin
  1064. inherited;
  1065. ObjOutput:=nil;
  1066. ObjData:=nil;
  1067. SmartAsm:=smart;
  1068. end;
  1069. destructor TInternalAssembler.destroy;
  1070. begin
  1071. if assigned(ObjData) then
  1072. ObjData.free;
  1073. if assigned(ObjOutput) then
  1074. ObjOutput.free;
  1075. end;
  1076. procedure TInternalAssembler.WriteStab(p:pchar);
  1077. function consumecomma(var p:pchar):boolean;
  1078. begin
  1079. while (p^=' ') do
  1080. inc(p);
  1081. result:=(p^=',');
  1082. inc(p);
  1083. end;
  1084. function consumenumber(var p:pchar;out value:longint):boolean;
  1085. var
  1086. hs : string;
  1087. len,
  1088. code : integer;
  1089. begin
  1090. value:=0;
  1091. while (p^=' ') do
  1092. inc(p);
  1093. len:=0;
  1094. while (p^ in ['0'..'9']) do
  1095. begin
  1096. inc(len);
  1097. hs[len]:=p^;
  1098. inc(p);
  1099. end;
  1100. if len>0 then
  1101. begin
  1102. hs[0]:=chr(len);
  1103. val(hs,value,code);
  1104. end
  1105. else
  1106. code:=-1;
  1107. result:=(code=0);
  1108. end;
  1109. function consumeoffset(var p:pchar;out relocsym:tobjsymbol;out value:longint):boolean;
  1110. var
  1111. hs : string;
  1112. len,
  1113. code : integer;
  1114. pstart : pchar;
  1115. sym : tobjsymbol;
  1116. exprvalue : longint;
  1117. gotmin,
  1118. have_first_symbol,
  1119. have_second_symbol,
  1120. dosub : boolean;
  1121. begin
  1122. result:=false;
  1123. value:=0;
  1124. relocsym:=nil;
  1125. gotmin:=false;
  1126. have_first_symbol:=false;
  1127. have_second_symbol:=false;
  1128. repeat
  1129. dosub:=false;
  1130. exprvalue:=0;
  1131. if gotmin then
  1132. begin
  1133. dosub:=true;
  1134. gotmin:=false;
  1135. end;
  1136. while (p^=' ') do
  1137. inc(p);
  1138. case p^ of
  1139. #0 :
  1140. break;
  1141. ' ' :
  1142. inc(p);
  1143. '0'..'9' :
  1144. begin
  1145. len:=0;
  1146. while (p^ in ['0'..'9']) do
  1147. begin
  1148. inc(len);
  1149. hs[len]:=p^;
  1150. inc(p);
  1151. end;
  1152. hs[0]:=chr(len);
  1153. val(hs,exprvalue,code);
  1154. if code<>0 then
  1155. internalerror(200702251);
  1156. end;
  1157. '.','_',
  1158. 'A'..'Z',
  1159. 'a'..'z' :
  1160. begin
  1161. pstart:=p;
  1162. while not(p^ in [#0,' ','-','+']) do
  1163. inc(p);
  1164. len:=p-pstart;
  1165. if len>255 then
  1166. internalerror(200509187);
  1167. move(pstart^,hs[1],len);
  1168. hs[0]:=chr(len);
  1169. sym:=objdata.symbolref(hs);
  1170. { Second symbol? }
  1171. if assigned(relocsym) then
  1172. begin
  1173. if have_second_symbol then
  1174. internalerror(2007032201);
  1175. have_second_symbol:=true;
  1176. if not have_first_symbol then
  1177. internalerror(2007032202);
  1178. { second symbol should substracted to first }
  1179. if not dosub then
  1180. internalerror(2007032203);
  1181. if (relocsym.objsection<>sym.objsection) then
  1182. internalerror(2005091810);
  1183. exprvalue:=relocsym.address-sym.address;
  1184. relocsym:=nil;
  1185. dosub:=false;
  1186. end
  1187. else
  1188. begin
  1189. relocsym:=sym;
  1190. if assigned(sym.objsection) then
  1191. begin
  1192. { first symbol should be + }
  1193. if not have_first_symbol and dosub then
  1194. internalerror(2007032204);
  1195. have_first_symbol:=true;
  1196. end;
  1197. end;
  1198. end;
  1199. '+' :
  1200. begin
  1201. { nothing, by default addition is done }
  1202. inc(p);
  1203. end;
  1204. '-' :
  1205. begin
  1206. gotmin:=true;
  1207. inc(p);
  1208. end;
  1209. else
  1210. internalerror(200509189);
  1211. end;
  1212. if dosub then
  1213. dec(value,exprvalue)
  1214. else
  1215. inc(value,exprvalue);
  1216. until false;
  1217. result:=true;
  1218. end;
  1219. var
  1220. stabstrlen,
  1221. ofs,
  1222. nline,
  1223. nidx,
  1224. nother,
  1225. i : longint;
  1226. stab : TObjStabEntry;
  1227. relocsym : TObjSymbol;
  1228. pstr,
  1229. pcurr,
  1230. pendquote : pchar;
  1231. oldsec : TObjSection;
  1232. begin
  1233. pcurr:=nil;
  1234. pstr:=nil;
  1235. pendquote:=nil;
  1236. relocsym:=nil;
  1237. ofs:=0;
  1238. { Parse string part }
  1239. if (p[0]='"') then
  1240. begin
  1241. pstr:=@p[1];
  1242. { Ignore \" inside the string }
  1243. i:=1;
  1244. while not((p[i]='"') and (p[i-1]<>'\')) and
  1245. (p[i]<>#0) do
  1246. inc(i);
  1247. pendquote:=@p[i];
  1248. pendquote^:=#0;
  1249. pcurr:=@p[i+1];
  1250. if not consumecomma(pcurr) then
  1251. internalerror(200509181);
  1252. end
  1253. else
  1254. pcurr:=p;
  1255. { When in pass 1 then only alloc and leave }
  1256. if ObjData.currpass=1 then
  1257. begin
  1258. ObjData.StabsSec.Alloc(sizeof(TObjStabEntry));
  1259. if assigned(pstr) and (pstr[0]<>#0) then
  1260. ObjData.StabStrSec.Alloc(strlen(pstr)+1);
  1261. end
  1262. else
  1263. begin
  1264. { Stabs format: nidx,nother,nline[,offset] }
  1265. if not consumenumber(pcurr,nidx) then
  1266. internalerror(200509182);
  1267. if not consumecomma(pcurr) then
  1268. internalerror(200509183);
  1269. if not consumenumber(pcurr,nother) then
  1270. internalerror(200509184);
  1271. if not consumecomma(pcurr) then
  1272. internalerror(200509185);
  1273. if not consumenumber(pcurr,nline) then
  1274. internalerror(200509186);
  1275. if consumecomma(pcurr) then
  1276. consumeoffset(pcurr,relocsym,ofs);
  1277. { Generate stab entry }
  1278. if assigned(pstr) and (pstr[0]<>#0) then
  1279. begin
  1280. stabstrlen:=strlen(pstr);
  1281. {$ifdef optimizestabs}
  1282. StabStrEntry:=nil;
  1283. if (nidx=N_SourceFile) or (nidx=N_IncludeFile) then
  1284. begin
  1285. hs:=strpas(pstr);
  1286. StabstrEntry:=StabStrDict.Find(hs);
  1287. if not assigned(StabstrEntry) then
  1288. begin
  1289. StabstrEntry:=TStabStrEntry.Create(hs);
  1290. StabstrEntry:=StabStrSec.Size;
  1291. StabStrDict.Insert(StabstrEntry);
  1292. { generate new stab }
  1293. StabstrEntry:=nil;
  1294. end;
  1295. end;
  1296. if assigned(StabstrEntry) then
  1297. stab.strpos:=StabstrEntry.strpos
  1298. else
  1299. {$endif optimizestabs}
  1300. begin
  1301. stab.strpos:=ObjData.StabStrSec.Size;
  1302. ObjData.StabStrSec.write(pstr^,stabstrlen+1);
  1303. end;
  1304. end
  1305. else
  1306. stab.strpos:=0;
  1307. stab.ntype:=byte(nidx);
  1308. stab.ndesc:=word(nline);
  1309. stab.nother:=byte(nother);
  1310. stab.nvalue:=ofs;
  1311. { Write the stab first without the value field. Then
  1312. write a the value field with relocation }
  1313. oldsec:=ObjData.CurrObjSec;
  1314. ObjData.SetSection(ObjData.StabsSec);
  1315. ObjData.Writebytes(stab,sizeof(TObjStabEntry)-4);
  1316. ObjData.Writereloc(stab.nvalue,4,relocsym,RELOC_ABSOLUTE32);
  1317. ObjData.setsection(oldsec);
  1318. end;
  1319. if assigned(pendquote) then
  1320. pendquote^:='"';
  1321. end;
  1322. function TInternalAssembler.MaybeNextList(var hp:Tai):boolean;
  1323. begin
  1324. { maybe end of list }
  1325. while not assigned(hp) do
  1326. begin
  1327. if currlistidx<lists then
  1328. begin
  1329. inc(currlistidx);
  1330. currlist:=list[currlistidx];
  1331. hp:=Tai(currList.first);
  1332. end
  1333. else
  1334. begin
  1335. MaybeNextList:=false;
  1336. exit;
  1337. end;
  1338. end;
  1339. MaybeNextList:=true;
  1340. end;
  1341. function TInternalAssembler.SetIndirectToSymbol(hp: Tai; const indirectname: string): Boolean;
  1342. var
  1343. objsym : TObjSymbol;
  1344. indsym : TObjSymbol;
  1345. begin
  1346. Result:=
  1347. Assigned(hp) and
  1348. (hp.typ=ait_symbol);
  1349. if not Result then
  1350. Exit;
  1351. objsym:=Objdata.SymbolRef(tai_symbol(hp).sym);
  1352. objsym.size:=0;
  1353. indsym := TObjSymbol(ObjData.ObjSymbolList.Find(indirectname));
  1354. if not Assigned(indsym) then
  1355. begin
  1356. { it's possible that indirect symbol is not present in the list,
  1357. so we must create it as undefined }
  1358. indsym:=ObjData.CObjSymbol.Create(ObjData.ObjSymbolList, indirectname);
  1359. indsym.typ:=AT_NONE;
  1360. indsym.bind:=AB_NONE;
  1361. end;
  1362. objsym.indsymbol:=indsym;
  1363. Result:=true;
  1364. end;
  1365. function TInternalAssembler.TreePass0(hp:Tai):Tai;
  1366. var
  1367. objsym,
  1368. objsymend : TObjSymbol;
  1369. cpu: tcputype;
  1370. begin
  1371. while assigned(hp) do
  1372. begin
  1373. case hp.typ of
  1374. ait_align :
  1375. begin
  1376. if tai_align_abstract(hp).aligntype>1 then
  1377. begin
  1378. { always use the maximum fillsize in this pass to avoid possible
  1379. short jumps to become out of range }
  1380. Tai_align_abstract(hp).fillsize:=Tai_align_abstract(hp).aligntype;
  1381. ObjData.alloc(Tai_align_abstract(hp).fillsize);
  1382. { may need to increase alignment of section }
  1383. if tai_align_abstract(hp).aligntype>ObjData.CurrObjSec.secalign then
  1384. ObjData.CurrObjSec.secalign:=tai_align_abstract(hp).aligntype;
  1385. end
  1386. else
  1387. Tai_align_abstract(hp).fillsize:=0;
  1388. end;
  1389. ait_datablock :
  1390. begin
  1391. {$ifdef USE_COMM_IN_BSS}
  1392. if writingpackages and
  1393. Tai_datablock(hp).is_global then
  1394. ObjData.SymbolDefine(Tai_datablock(hp).sym)
  1395. else
  1396. {$endif USE_COMM_IN_BSS}
  1397. begin
  1398. ObjData.allocalign(used_align(size_2_align(Tai_datablock(hp).size),0,ObjData.CurrObjSec.secalign));
  1399. ObjData.SymbolDefine(Tai_datablock(hp).sym);
  1400. ObjData.alloc(Tai_datablock(hp).size);
  1401. end;
  1402. end;
  1403. ait_realconst:
  1404. ObjData.alloc(tai_realconst(hp).savesize);
  1405. ait_const:
  1406. begin
  1407. { if symbols are provided we can calculate the value for relative symbols.
  1408. This is required for length calculation of leb128 constants }
  1409. if assigned(tai_const(hp).sym) then
  1410. begin
  1411. objsym:=Objdata.SymbolRef(tai_const(hp).sym);
  1412. { objsym already defined and there is endsym? }
  1413. if assigned(objsym.objsection) and assigned(tai_const(hp).endsym) then
  1414. begin
  1415. objsymend:=Objdata.SymbolRef(tai_const(hp).endsym);
  1416. { objsymend already defined? }
  1417. if assigned(objsymend.objsection) then
  1418. begin
  1419. if objsymend.objsection<>objsym.objsection then
  1420. begin
  1421. { leb128 relative constants are not relocatable, but other types are,
  1422. given that objsym belongs to the current section. }
  1423. if (Tai_const(hp).consttype in [aitconst_uleb128bit,aitconst_sleb128bit]) or
  1424. (objsym.objsection<>ObjData.CurrObjSec) then
  1425. InternalError(200404124);
  1426. end
  1427. else
  1428. Tai_const(hp).value:=objsymend.address-objsym.address+Tai_const(hp).symofs;
  1429. end;
  1430. end;
  1431. end;
  1432. ObjData.alloc(tai_const(hp).size);
  1433. end;
  1434. ait_directive:
  1435. begin
  1436. case tai_directive(hp).directive of
  1437. asd_indirect_symbol:
  1438. { handled in TreePass1 }
  1439. ;
  1440. asd_lazy_reference:
  1441. begin
  1442. if tai_directive(hp).name='' then
  1443. Internalerror(2009112101);
  1444. objsym:=ObjData.symbolref(tai_directive(hp).name);
  1445. objsym.bind:=AB_LAZY;
  1446. end;
  1447. asd_reference:
  1448. { ignore for now, but should be added}
  1449. ;
  1450. asd_cpu:
  1451. begin
  1452. ObjData.CPUType:=cpu_none;
  1453. for cpu:=low(tcputype) to high(tcputype) do
  1454. if cputypestr[cpu]=tai_directive(hp).name then
  1455. begin
  1456. ObjData.CPUType:=cpu;
  1457. break;
  1458. end;
  1459. end;
  1460. {$ifdef ARM}
  1461. asd_thumb_func:
  1462. ObjData.ThumbFunc:=true;
  1463. asd_code:
  1464. { ai_directive(hp).name can be only 16 or 32, this is checked by the reader }
  1465. ObjData.ThumbFunc:=tai_directive(hp).name='16';
  1466. {$endif ARM}
  1467. else
  1468. internalerror(2010011101);
  1469. end;
  1470. end;
  1471. ait_section:
  1472. begin
  1473. ObjData.CreateSection(Tai_section(hp).sectype,Tai_section(hp).name^,Tai_section(hp).secorder);
  1474. Tai_section(hp).sec:=ObjData.CurrObjSec;
  1475. end;
  1476. ait_symbol :
  1477. begin
  1478. { needs extra support in the internal assembler }
  1479. { the value is just ignored }
  1480. {if tai_symbol(hp).has_value then
  1481. internalerror(2009090804); ;}
  1482. ObjData.SymbolDefine(Tai_symbol(hp).sym);
  1483. end;
  1484. ait_label :
  1485. ObjData.SymbolDefine(Tai_label(hp).labsym);
  1486. ait_string :
  1487. ObjData.alloc(Tai_string(hp).len);
  1488. ait_instruction :
  1489. begin
  1490. { reset instructions which could change in pass 2 }
  1491. Taicpu(hp).resetpass2;
  1492. ObjData.alloc(Taicpu(hp).Pass1(ObjData));
  1493. end;
  1494. ait_cutobject :
  1495. if SmartAsm then
  1496. break;
  1497. end;
  1498. hp:=Tai(hp.next);
  1499. end;
  1500. TreePass0:=hp;
  1501. end;
  1502. function TInternalAssembler.TreePass1(hp:Tai):Tai;
  1503. var
  1504. objsym,
  1505. objsymend : TObjSymbol;
  1506. cpu: tcputype;
  1507. begin
  1508. while assigned(hp) do
  1509. begin
  1510. case hp.typ of
  1511. ait_align :
  1512. begin
  1513. if tai_align_abstract(hp).aligntype>1 then
  1514. begin
  1515. { here we must determine the fillsize which is used in pass2 }
  1516. Tai_align_abstract(hp).fillsize:=align(ObjData.CurrObjSec.Size,Tai_align_abstract(hp).aligntype)-
  1517. ObjData.CurrObjSec.Size;
  1518. ObjData.alloc(Tai_align_abstract(hp).fillsize);
  1519. end;
  1520. end;
  1521. ait_datablock :
  1522. begin
  1523. if (oso_data in ObjData.CurrObjSec.secoptions) and
  1524. not (oso_sparse_data in ObjData.CurrObjSec.secoptions) then
  1525. Message(asmw_e_alloc_data_only_in_bss);
  1526. {$ifdef USE_COMM_IN_BSS}
  1527. if writingpackages and
  1528. Tai_datablock(hp).is_global then
  1529. begin
  1530. objsym:=ObjData.SymbolDefine(Tai_datablock(hp).sym);
  1531. objsym.size:=Tai_datablock(hp).size;
  1532. objsym.bind:=AB_COMMON;
  1533. objsym.alignment:=needtowritealignmentalsoforELF;
  1534. end
  1535. else
  1536. {$endif USE_COMM_IN_BSS}
  1537. begin
  1538. ObjData.allocalign(used_align(size_2_align(Tai_datablock(hp).size),0,ObjData.CurrObjSec.secalign));
  1539. objsym:=ObjData.SymbolDefine(Tai_datablock(hp).sym);
  1540. objsym.size:=Tai_datablock(hp).size;
  1541. ObjData.alloc(Tai_datablock(hp).size);
  1542. end;
  1543. end;
  1544. ait_realconst:
  1545. ObjData.alloc(tai_realconst(hp).savesize);
  1546. ait_const:
  1547. begin
  1548. { Recalculate relative symbols }
  1549. if assigned(tai_const(hp).sym) and
  1550. assigned(tai_const(hp).endsym) then
  1551. begin
  1552. objsym:=Objdata.SymbolRef(tai_const(hp).sym);
  1553. objsymend:=Objdata.SymbolRef(tai_const(hp).endsym);
  1554. if objsymend.objsection<>objsym.objsection then
  1555. begin
  1556. if (Tai_const(hp).consttype in [aitconst_uleb128bit,aitconst_sleb128bit]) or
  1557. (objsym.objsection<>ObjData.CurrObjSec) then
  1558. internalerror(200905042);
  1559. end
  1560. else
  1561. Tai_const(hp).value:=objsymend.address-objsym.address+Tai_const(hp).symofs;
  1562. end;
  1563. ObjData.alloc(tai_const(hp).size);
  1564. end;
  1565. ait_section:
  1566. begin
  1567. { use cached value }
  1568. ObjData.setsection(Tai_section(hp).sec);
  1569. end;
  1570. ait_stab :
  1571. begin
  1572. if assigned(Tai_stab(hp).str) then
  1573. WriteStab(Tai_stab(hp).str);
  1574. end;
  1575. ait_symbol :
  1576. ObjData.SymbolDefine(Tai_symbol(hp).sym);
  1577. ait_symbol_end :
  1578. begin
  1579. objsym:=ObjData.SymbolRef(Tai_symbol_end(hp).sym);
  1580. objsym.size:=ObjData.CurrObjSec.Size-objsym.offset;
  1581. end;
  1582. ait_label :
  1583. ObjData.SymbolDefine(Tai_label(hp).labsym);
  1584. ait_string :
  1585. ObjData.alloc(Tai_string(hp).len);
  1586. ait_instruction :
  1587. ObjData.alloc(Taicpu(hp).Pass1(ObjData));
  1588. ait_cutobject :
  1589. if SmartAsm then
  1590. break;
  1591. ait_directive :
  1592. begin
  1593. case tai_directive(hp).directive of
  1594. asd_indirect_symbol:
  1595. if tai_directive(hp).name='' then
  1596. Internalerror(2009101103)
  1597. else if not SetIndirectToSymbol(Tai(hp.Previous), tai_directive(hp).name) then
  1598. Internalerror(2009101102);
  1599. asd_lazy_reference:
  1600. { handled in TreePass0 }
  1601. ;
  1602. asd_reference:
  1603. { ignore for now, but should be added}
  1604. ;
  1605. asd_thumb_func:
  1606. { ignore for now, but should be added}
  1607. ;
  1608. asd_code:
  1609. { ignore for now, but should be added}
  1610. ;
  1611. asd_cpu:
  1612. begin
  1613. ObjData.CPUType:=cpu_none;
  1614. for cpu:=low(tcputype) to high(tcputype) do
  1615. if cputypestr[cpu]=tai_directive(hp).name then
  1616. begin
  1617. ObjData.CPUType:=cpu;
  1618. break;
  1619. end;
  1620. end;
  1621. else
  1622. internalerror(2010011102);
  1623. end;
  1624. end;
  1625. end;
  1626. hp:=Tai(hp.next);
  1627. end;
  1628. TreePass1:=hp;
  1629. end;
  1630. function TInternalAssembler.TreePass2(hp:Tai):Tai;
  1631. var
  1632. fillbuffer : tfillbuffer;
  1633. leblen : byte;
  1634. lebbuf : array[0..63] of byte;
  1635. objsym,
  1636. ref,
  1637. objsymend : TObjSymbol;
  1638. zerobuf : array[0..63] of byte;
  1639. relative_reloc: boolean;
  1640. pdata : pointer;
  1641. ssingle : single;
  1642. ddouble : double;
  1643. {$if defined(cpuextended) and defined(FPC_HAS_TYPE_EXTENDED)}
  1644. eextended : extended;
  1645. {$else}
  1646. {$ifdef FPC_SOFT_FPUX80}
  1647. eextended : floatx80;
  1648. {$endif}
  1649. {$endif}
  1650. ccomp : comp;
  1651. tmp : word;
  1652. cpu: tcputype;
  1653. begin
  1654. fillchar(zerobuf,sizeof(zerobuf),0);
  1655. fillchar(objsym,sizeof(objsym),0);
  1656. fillchar(objsymend,sizeof(objsymend),0);
  1657. { main loop }
  1658. while assigned(hp) do
  1659. begin
  1660. case hp.typ of
  1661. ait_align :
  1662. begin
  1663. if tai_align_abstract(hp).aligntype>ObjData.CurrObjSec.secalign then
  1664. InternalError(2012072301);
  1665. if oso_data in ObjData.CurrObjSec.secoptions then
  1666. ObjData.writebytes(Tai_align_abstract(hp).calculatefillbuf(fillbuffer,oso_executable in ObjData.CurrObjSec.secoptions)^,
  1667. Tai_align_abstract(hp).fillsize)
  1668. else
  1669. ObjData.alloc(Tai_align_abstract(hp).fillsize);
  1670. end;
  1671. ait_section :
  1672. begin
  1673. { use cached value }
  1674. ObjData.setsection(Tai_section(hp).sec);
  1675. end;
  1676. ait_symbol :
  1677. begin
  1678. ObjOutput.exportsymbol(ObjData.SymbolRef(Tai_symbol(hp).sym));
  1679. end;
  1680. ait_symbol_end :
  1681. begin
  1682. { recalculate size, as some preceding instructions
  1683. could have been changed to smaller size }
  1684. objsym:=ObjData.SymbolRef(Tai_symbol_end(hp).sym);
  1685. objsym.size:=ObjData.CurrObjSec.Size-objsym.offset;
  1686. end;
  1687. ait_datablock :
  1688. begin
  1689. ObjOutput.exportsymbol(ObjData.SymbolRef(Tai_datablock(hp).sym));
  1690. {$ifdef USE_COMM_IN_BSS}
  1691. if not(writingpackages and
  1692. Tai_datablock(hp).is_global) then
  1693. {$endif USE_COMM_IN_BSS}
  1694. begin
  1695. ObjData.allocalign(used_align(size_2_align(Tai_datablock(hp).size),0,ObjData.CurrObjSec.secalign));
  1696. ObjData.alloc(Tai_datablock(hp).size);
  1697. end;
  1698. end;
  1699. ait_realconst:
  1700. begin
  1701. case tai_realconst(hp).realtyp of
  1702. aitrealconst_s32bit:
  1703. begin
  1704. ssingle:=single(tai_realconst(hp).value.s32val);
  1705. pdata:=@ssingle;
  1706. end;
  1707. aitrealconst_s64bit:
  1708. begin
  1709. ddouble:=double(tai_realconst(hp).value.s64val);
  1710. pdata:=@ddouble;
  1711. end;
  1712. {$if defined(cpuextended) and defined(FPC_HAS_TYPE_EXTENDED)}
  1713. { can't write full 80 bit floating point constants yet on non-x86 }
  1714. aitrealconst_s80bit:
  1715. begin
  1716. eextended:=extended(tai_realconst(hp).value.s80val);
  1717. pdata:=@eextended;
  1718. end;
  1719. {$else}
  1720. {$ifdef FPC_SOFT_FPUX80}
  1721. aitrealconst_s80bit:
  1722. begin
  1723. if sizeof(tai_realconst(hp).value.s80val) = sizeof(double) then
  1724. eextended:=float64_to_floatx80(float64(double(tai_realconst(hp).value.s80val)))
  1725. else if sizeof(tai_realconst(hp).value.s80val) = sizeof(single) then
  1726. eextended:=float32_to_floatx80(float32(single(tai_realconst(hp).value.s80val)))
  1727. else
  1728. internalerror(2017091901);
  1729. pdata:=@eextended;
  1730. end;
  1731. {$endif}
  1732. {$endif cpuextended}
  1733. aitrealconst_s64comp:
  1734. begin
  1735. ccomp:=comp(tai_realconst(hp).value.s64compval);
  1736. pdata:=@ccomp;
  1737. end;
  1738. else
  1739. internalerror(2015030501);
  1740. end;
  1741. ObjData.writebytes(pdata^,tai_realconst(hp).datasize);
  1742. ObjData.writebytes(zerobuf,tai_realconst(hp).savesize-tai_realconst(hp).datasize);
  1743. end;
  1744. ait_string :
  1745. ObjData.writebytes(Tai_string(hp).str^,Tai_string(hp).len);
  1746. ait_const :
  1747. begin
  1748. { Recalculate relative symbols, addresses of forward references
  1749. can be changed in treepass1 }
  1750. relative_reloc:=false;
  1751. if assigned(tai_const(hp).sym) and
  1752. assigned(tai_const(hp).endsym) then
  1753. begin
  1754. objsym:=Objdata.SymbolRef(tai_const(hp).sym);
  1755. objsymend:=Objdata.SymbolRef(tai_const(hp).endsym);
  1756. relative_reloc:=(objsym.objsection<>objsymend.objsection);
  1757. Tai_const(hp).value:=objsymend.address-objsym.address+Tai_const(hp).symofs;
  1758. end;
  1759. case tai_const(hp).consttype of
  1760. aitconst_64bit,
  1761. aitconst_32bit,
  1762. aitconst_16bit,
  1763. aitconst_64bit_unaligned,
  1764. aitconst_32bit_unaligned,
  1765. aitconst_16bit_unaligned,
  1766. aitconst_8bit :
  1767. begin
  1768. if assigned(tai_const(hp).sym) and
  1769. not assigned(tai_const(hp).endsym) then
  1770. ObjData.writereloc(Tai_const(hp).symofs,tai_const(hp).size,Objdata.SymbolRef(tai_const(hp).sym),RELOC_ABSOLUTE)
  1771. else if relative_reloc then
  1772. ObjData.writereloc(ObjData.CurrObjSec.size+tai_const(hp).size-objsym.address+tai_const(hp).symofs,tai_const(hp).size,objsymend,RELOC_RELATIVE)
  1773. else
  1774. ObjData.writebytes(Tai_const(hp).value,tai_const(hp).size);
  1775. end;
  1776. aitconst_rva_symbol :
  1777. begin
  1778. { PE32+? }
  1779. if target_info.system=system_x86_64_win64 then
  1780. ObjData.writereloc(Tai_const(hp).symofs,sizeof(longint),Objdata.SymbolRef(tai_const(hp).sym),RELOC_RVA)
  1781. else
  1782. ObjData.writereloc(Tai_const(hp).symofs,sizeof(pint),Objdata.SymbolRef(tai_const(hp).sym),RELOC_RVA);
  1783. end;
  1784. aitconst_secrel32_symbol :
  1785. begin
  1786. { Required for DWARF2 support under Windows }
  1787. ObjData.writereloc(Tai_const(hp).symofs,sizeof(longint),Objdata.SymbolRef(tai_const(hp).sym),RELOC_SECREL32);
  1788. end;
  1789. {$ifdef i8086}
  1790. aitconst_farptr :
  1791. if assigned(tai_const(hp).sym) and
  1792. not assigned(tai_const(hp).endsym) then
  1793. ObjData.writereloc(Tai_const(hp).symofs,tai_const(hp).size,Objdata.SymbolRef(tai_const(hp).sym),RELOC_FARPTR)
  1794. else if relative_reloc then
  1795. internalerror(2015040601)
  1796. else
  1797. ObjData.writebytes(Tai_const(hp).value,tai_const(hp).size);
  1798. aitconst_seg:
  1799. if assigned(tai_const(hp).sym) and (tai_const(hp).size=2) then
  1800. ObjData.writereloc(0,2,Objdata.SymbolRef(tai_const(hp).sym),RELOC_SEG)
  1801. else
  1802. internalerror(2015110502);
  1803. aitconst_dgroup:
  1804. ObjData.writereloc(0,2,nil,RELOC_DGROUP);
  1805. aitconst_fardataseg:
  1806. ObjData.writereloc(0,2,nil,RELOC_FARDATASEG);
  1807. {$endif i8086}
  1808. {$ifdef arm}
  1809. aitconst_got:
  1810. ObjData.writereloc(Tai_const(hp).symofs,sizeof(longint),Objdata.SymbolRef(tai_const(hp).sym),RELOC_GOT32);
  1811. {$endif arm}
  1812. aitconst_gotoff_symbol:
  1813. ObjData.writereloc(Tai_const(hp).symofs,sizeof(longint),Objdata.SymbolRef(tai_const(hp).sym),RELOC_GOTOFF);
  1814. aitconst_uleb128bit,
  1815. aitconst_sleb128bit :
  1816. begin
  1817. if tai_const(hp).consttype=aitconst_uleb128bit then
  1818. leblen:=EncodeUleb128(qword(Tai_const(hp).value),lebbuf)
  1819. else
  1820. leblen:=EncodeSleb128(Tai_const(hp).value,lebbuf);
  1821. if leblen<>tai_const(hp).size then
  1822. internalerror(200709271);
  1823. ObjData.writebytes(lebbuf,leblen);
  1824. end;
  1825. aitconst_darwin_dwarf_delta32,
  1826. aitconst_darwin_dwarf_delta64:
  1827. ObjData.writebytes(Tai_const(hp).value,tai_const(hp).size);
  1828. aitconst_half16bit,
  1829. aitconst_gs:
  1830. begin
  1831. tmp:=Tai_const(hp).value div 2;
  1832. ObjData.writebytes(tmp,2);
  1833. end;
  1834. else
  1835. internalerror(200603254);
  1836. end;
  1837. end;
  1838. ait_label :
  1839. begin
  1840. { exporting shouldn't be necessary as labels are local,
  1841. but it's better to be on the safe side (PFV) }
  1842. ObjOutput.exportsymbol(ObjData.SymbolRef(Tai_label(hp).labsym));
  1843. end;
  1844. ait_instruction :
  1845. Taicpu(hp).Pass2(ObjData);
  1846. ait_stab :
  1847. WriteStab(Tai_stab(hp).str);
  1848. ait_function_name,
  1849. ait_force_line : ;
  1850. ait_cutobject :
  1851. if SmartAsm then
  1852. break;
  1853. ait_directive :
  1854. begin
  1855. case tai_directive(hp).directive of
  1856. asd_weak_definition,
  1857. asd_weak_reference:
  1858. begin
  1859. objsym:=ObjData.symbolref(tai_directive(hp).name);
  1860. if objsym.bind in [AB_EXTERNAL,AB_WEAK_EXTERNAL] then
  1861. objsym.bind:=AB_WEAK_EXTERNAL
  1862. else
  1863. { TODO: should become a weak definition; for now, do
  1864. the same as what was done for ait_weak }
  1865. objsym.bind:=AB_WEAK_EXTERNAL;
  1866. end;
  1867. asd_cpu:
  1868. begin
  1869. ObjData.CPUType:=cpu_none;
  1870. for cpu:=low(tcputype) to high(tcputype) do
  1871. if cputypestr[cpu]=tai_directive(hp).name then
  1872. begin
  1873. ObjData.CPUType:=cpu;
  1874. break;
  1875. end;
  1876. end;
  1877. end
  1878. end;
  1879. ait_symbolpair:
  1880. begin
  1881. if tai_symbolpair(hp).kind=spk_set then
  1882. begin
  1883. objsym:=ObjData.symbolref(tai_symbolpair(hp).sym^);
  1884. ref:=objdata.symbolref(tai_symbolpair(hp).value^);
  1885. objsym.offset:=ref.offset;
  1886. objsym.objsection:=ref.objsection;
  1887. {$ifdef arm}
  1888. objsym.ThumbFunc:=ref.ThumbFunc;
  1889. {$endif arm}
  1890. end;
  1891. end;
  1892. {$ifndef DISABLE_WIN64_SEH}
  1893. ait_seh_directive :
  1894. tai_seh_directive(hp).generate_code(objdata);
  1895. {$endif DISABLE_WIN64_SEH}
  1896. end;
  1897. hp:=Tai(hp.next);
  1898. end;
  1899. TreePass2:=hp;
  1900. end;
  1901. procedure TInternalAssembler.writetree;
  1902. label
  1903. doexit;
  1904. var
  1905. hp : Tai;
  1906. ObjWriter : TObjectWriter;
  1907. begin
  1908. ObjWriter:=TObjectwriter.create;
  1909. ObjOutput:=CObjOutput.Create(ObjWriter);
  1910. ObjData:=ObjOutput.newObjData(ObjFileName);
  1911. { Pass 0 }
  1912. ObjData.currpass:=0;
  1913. ObjData.createsection(sec_code);
  1914. ObjData.beforealloc;
  1915. { start with list 1 }
  1916. currlistidx:=1;
  1917. currlist:=list[currlistidx];
  1918. hp:=Tai(currList.first);
  1919. while assigned(hp) do
  1920. begin
  1921. hp:=TreePass0(hp);
  1922. MaybeNextList(hp);
  1923. end;
  1924. ObjData.afteralloc;
  1925. { leave if errors have occurred }
  1926. if errorcount>0 then
  1927. goto doexit;
  1928. { Pass 1 }
  1929. ObjData.currpass:=1;
  1930. ObjData.resetsections;
  1931. ObjData.beforealloc;
  1932. ObjData.createsection(sec_code);
  1933. { start with list 1 }
  1934. currlistidx:=1;
  1935. currlist:=list[currlistidx];
  1936. hp:=Tai(currList.first);
  1937. while assigned(hp) do
  1938. begin
  1939. hp:=TreePass1(hp);
  1940. MaybeNextList(hp);
  1941. end;
  1942. ObjData.createsection(sec_code);
  1943. ObjData.afteralloc;
  1944. { leave if errors have occurred }
  1945. if errorcount>0 then
  1946. goto doexit;
  1947. { Pass 2 }
  1948. ObjData.currpass:=2;
  1949. ObjData.resetsections;
  1950. ObjData.beforewrite;
  1951. ObjData.createsection(sec_code);
  1952. { start with list 1 }
  1953. currlistidx:=1;
  1954. currlist:=list[currlistidx];
  1955. hp:=Tai(currList.first);
  1956. while assigned(hp) do
  1957. begin
  1958. hp:=TreePass2(hp);
  1959. MaybeNextList(hp);
  1960. end;
  1961. ObjData.createsection(sec_code);
  1962. ObjData.afterwrite;
  1963. { don't write the .o file if errors have occurred }
  1964. if errorcount=0 then
  1965. begin
  1966. { write objectfile }
  1967. ObjOutput.startobjectfile(ObjFileName);
  1968. ObjOutput.writeobjectfile(ObjData);
  1969. end;
  1970. doexit:
  1971. { Cleanup }
  1972. ObjData.free;
  1973. ObjData:=nil;
  1974. ObjWriter.free;
  1975. end;
  1976. procedure TInternalAssembler.writetreesmart;
  1977. var
  1978. hp : Tai;
  1979. startsectype : TAsmSectiontype;
  1980. place: tcutplace;
  1981. ObjWriter : TObjectWriter;
  1982. startsecname: String;
  1983. startsecorder: TAsmSectionOrder;
  1984. begin
  1985. if not(cs_asm_leave in current_settings.globalswitches) and
  1986. not(af_needar in asminfo^.flags) then
  1987. ObjWriter:=CInternalAr.CreateAr(current_module.staticlibfilename)
  1988. else
  1989. ObjWriter:=TObjectwriter.create;
  1990. NextSmartName(cut_normal);
  1991. ObjOutput:=CObjOutput.Create(ObjWriter);
  1992. startsectype:=sec_none;
  1993. startsecname:='';
  1994. startsecorder:=secorder_default;
  1995. { start with list 1 }
  1996. currlistidx:=1;
  1997. currlist:=list[currlistidx];
  1998. hp:=Tai(currList.first);
  1999. while assigned(hp) do
  2000. begin
  2001. ObjData:=ObjOutput.newObjData(ObjFileName);
  2002. { Pass 0 }
  2003. ObjData.currpass:=0;
  2004. ObjData.resetsections;
  2005. ObjData.beforealloc;
  2006. if startsectype<>sec_none then
  2007. ObjData.CreateSection(startsectype,startsecname,startsecorder);
  2008. TreePass0(hp);
  2009. ObjData.afteralloc;
  2010. { leave if errors have occurred }
  2011. if errorcount>0 then
  2012. break;
  2013. { Pass 1 }
  2014. ObjData.currpass:=1;
  2015. ObjData.resetsections;
  2016. ObjData.beforealloc;
  2017. if startsectype<>sec_none then
  2018. ObjData.CreateSection(startsectype,startsecname,startsecorder);
  2019. TreePass1(hp);
  2020. ObjData.afteralloc;
  2021. { leave if errors have occurred }
  2022. if errorcount>0 then
  2023. break;
  2024. { Pass 2 }
  2025. ObjData.currpass:=2;
  2026. ObjOutput.startobjectfile(ObjFileName);
  2027. ObjData.resetsections;
  2028. ObjData.beforewrite;
  2029. if startsectype<>sec_none then
  2030. ObjData.CreateSection(startsectype,startsecname,startsecorder);
  2031. hp:=TreePass2(hp);
  2032. ObjData.afterwrite;
  2033. { leave if errors have occurred }
  2034. if errorcount>0 then
  2035. break;
  2036. { write the current objectfile }
  2037. ObjOutput.writeobjectfile(ObjData);
  2038. ObjData.free;
  2039. ObjData:=nil;
  2040. { end of lists? }
  2041. if not MaybeNextList(hp) then
  2042. break;
  2043. { we will start a new objectfile so reset everything }
  2044. { The place can still change in the next while loop, so don't init }
  2045. { the writer yet (JM) }
  2046. if (hp.typ=ait_cutobject) then
  2047. place := Tai_cutobject(hp).place
  2048. else
  2049. place := cut_normal;
  2050. { avoid empty files }
  2051. startsectype:=sec_none;
  2052. startsecname:='';
  2053. startsecorder:=secorder_default;
  2054. while assigned(hp) and
  2055. (Tai(hp).typ in [ait_marker,ait_comment,ait_section,ait_cutobject]) do
  2056. begin
  2057. if Tai(hp).typ=ait_section then
  2058. begin
  2059. startsectype:=Tai_section(hp).sectype;
  2060. startsecname:=Tai_section(hp).name^;
  2061. startsecorder:=Tai_section(hp).secorder;
  2062. end;
  2063. if (Tai(hp).typ=ait_cutobject) then
  2064. place:=Tai_cutobject(hp).place;
  2065. hp:=Tai(hp.next);
  2066. end;
  2067. if not MaybeNextList(hp) then
  2068. break;
  2069. { start next objectfile }
  2070. NextSmartName(place);
  2071. end;
  2072. ObjData.free;
  2073. ObjData:=nil;
  2074. ObjWriter.free;
  2075. end;
  2076. procedure TInternalAssembler.MakeObject;
  2077. var to_do:set of TasmlistType;
  2078. i:TasmlistType;
  2079. procedure addlist(p:TAsmList);
  2080. begin
  2081. inc(lists);
  2082. list[lists]:=p;
  2083. end;
  2084. begin
  2085. to_do:=[low(Tasmlisttype)..high(Tasmlisttype)];
  2086. if usedeffileforexports then
  2087. exclude(to_do,al_exports);
  2088. if not(tf_section_threadvars in target_info.flags) then
  2089. exclude(to_do,al_threadvars);
  2090. for i:=low(TasmlistType) to high(TasmlistType) do
  2091. if (i in to_do) and (current_asmdata.asmlists[i]<>nil) and
  2092. (not current_asmdata.asmlists[i].empty) then
  2093. addlist(current_asmdata.asmlists[i]);
  2094. if SmartAsm then
  2095. writetreesmart
  2096. else
  2097. writetree;
  2098. end;
  2099. {*****************************************************************************
  2100. Generate Assembler Files Main Procedure
  2101. *****************************************************************************}
  2102. Procedure GenerateAsm(smart:boolean);
  2103. var
  2104. a : TAssembler;
  2105. begin
  2106. if not assigned(CAssembler[target_asm.id]) then
  2107. Message(asmw_f_assembler_output_not_supported);
  2108. a:=CAssembler[target_asm.id].Create(@target_asm,smart);
  2109. a.MakeObject;
  2110. a.Free;
  2111. end;
  2112. function GetExternalGnuAssemblerWithAsmInfoWriter(info: pasminfo; wr: TExternalAssemblerOutputFile): TExternalAssembler;
  2113. var
  2114. asmkind: tasm;
  2115. begin
  2116. for asmkind in [as_gas,as_ggas,as_darwin] do
  2117. if assigned(asminfos[asmkind]) and
  2118. (target_info.system in asminfos[asmkind]^.supported_targets) then
  2119. begin
  2120. result:=TExternalAssemblerClass(CAssembler[asmkind]).CreateWithWriter(asminfos[asmkind],wr,false,false);
  2121. exit;
  2122. end;
  2123. Internalerror(2015090604);
  2124. end;
  2125. {*****************************************************************************
  2126. Init/Done
  2127. *****************************************************************************}
  2128. procedure RegisterAssembler(const r:tasminfo;c:TAssemblerClass);
  2129. var
  2130. t : tasm;
  2131. begin
  2132. t:=r.id;
  2133. if assigned(asminfos[t]) then
  2134. writeln('Warning: Assembler is already registered!')
  2135. else
  2136. Getmem(asminfos[t],sizeof(tasminfo));
  2137. asminfos[t]^:=r;
  2138. CAssembler[t]:=c;
  2139. end;
  2140. end.