assemble.pas 72 KB

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