assemble.pas 76 KB

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