assemble.pas 78 KB

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