assemble.pas 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760
  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,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(TAbstractAssembler)
  35. public
  36. {filenames}
  37. path : TPathStr;
  38. name : string;
  39. AsmFileName, { current .s and .o file }
  40. ObjFileName,
  41. ppufilename : TPathStr;
  42. asmprefix : string;
  43. SmartAsm : boolean;
  44. SmartFilesCount,
  45. SmartHeaderCount : longint;
  46. Constructor Create(smart:boolean);virtual;
  47. Destructor Destroy;override;
  48. procedure NextSmartName(place:tcutplace);
  49. procedure MakeObject;virtual;abstract;
  50. end;
  51. {# This is the base class which should be overridden for each each
  52. assembler writer. It is used to actually assembler a file,
  53. and write the output to the assembler file.
  54. }
  55. TExternalAssembler=class(TAssembler)
  56. private
  57. procedure CreateSmartLinkPath(const s:string);
  58. protected
  59. {outfile}
  60. AsmSize,
  61. AsmStartSize,
  62. outcnt : longint;
  63. outbuf : array[0..AsmOutSize-1] of char;
  64. outfile : file;
  65. ioerror : boolean;
  66. {input source info}
  67. lastfileinfo : tfileposinfo;
  68. infile,
  69. lastinfile : tinputfile;
  70. {last section type written}
  71. lastsectype : TAsmSectionType;
  72. procedure WriteSourceLine(hp: tailineinfo);
  73. procedure WriteTempalloc(hp: tai_tempalloc);
  74. public
  75. {# Returns the complete path and executable name of the assembler
  76. program.
  77. It first tries looking in the UTIL directory if specified,
  78. otherwise it searches in the free pascal binary directory, in
  79. the current working directory and then in the directories
  80. in the $PATH environment.}
  81. Function FindAssembler:string;
  82. {# Actually does the call to the assembler file. Returns false
  83. if the assembling of the file failed.}
  84. Function CallAssembler(const command:string; const para:TCmdStr):Boolean;
  85. Function DoAssemble:boolean;virtual;
  86. Procedure RemoveAsm;virtual;
  87. Procedure AsmFlush;
  88. Procedure AsmClear;
  89. {# Write a string to the assembler file }
  90. Procedure AsmWrite(const c:char);
  91. Procedure AsmWrite(const s:string);
  92. Procedure AsmWrite(const s:ansistring);
  93. {# Write a string to the assembler file }
  94. Procedure AsmWritePChar(p:pchar);
  95. {# Write a string to the assembler file followed by a new line }
  96. Procedure AsmWriteLn(const c:char);
  97. Procedure AsmWriteLn(const s:string);
  98. Procedure AsmWriteLn(const s:ansistring);
  99. {# Write a new line to the assembler file }
  100. Procedure AsmLn;
  101. procedure AsmCreate(Aplace:tcutplace);
  102. procedure AsmClose;
  103. {# This routine should be overridden for each assembler, it is used
  104. to actually write the abstract assembler stream to file.}
  105. procedure WriteTree(p:TAsmList);virtual;
  106. {# This routine should be overridden for each assembler, it is used
  107. to actually write all the different abstract assembler streams
  108. by calling for each stream type, the @var(WriteTree) method.}
  109. procedure WriteAsmList;virtual;
  110. {# Constructs the command line for calling the assembler }
  111. function MakeCmdLine: TCmdStr; virtual;
  112. public
  113. Constructor Create(smart:boolean);override;
  114. procedure MakeObject;override;
  115. end;
  116. { TInternalAssembler }
  117. TInternalAssembler=class(TAssembler)
  118. private
  119. FCObjOutput : TObjOutputclass;
  120. { the aasmoutput lists that need to be processed }
  121. lists : byte;
  122. list : array[1..maxoutputlists] of TAsmList;
  123. { current processing }
  124. currlistidx : byte;
  125. currlist : TAsmList;
  126. procedure WriteStab(p:pchar);
  127. function MaybeNextList(var hp:Tai):boolean;
  128. function SetIndirectToSymbol(hp: Tai; const indirectname: string): Boolean;
  129. function TreePass0(hp:Tai):Tai;
  130. function TreePass1(hp:Tai):Tai;
  131. function TreePass2(hp:Tai):Tai;
  132. procedure writetree;
  133. procedure writetreesmart;
  134. protected
  135. ObjData : TObjData;
  136. ObjOutput : tObjOutput;
  137. property CObjOutput:TObjOutputclass read FCObjOutput write FCObjOutput;
  138. public
  139. constructor create(smart:boolean);override;
  140. destructor destroy;override;
  141. procedure MakeObject;override;
  142. end;
  143. TAssemblerClass = class of TAssembler;
  144. Procedure GenerateAsm(smart:boolean);
  145. Procedure OnlyAsm;
  146. procedure RegisterAssembler(const r:tasminfo;c:TAssemblerClass);
  147. Implementation
  148. uses
  149. {$ifdef hasunix}
  150. unix,
  151. {$endif}
  152. cutils,cfileutl,
  153. {$ifdef memdebug}
  154. cclasses,
  155. {$endif memdebug}
  156. script,fmodule,verbose,
  157. {$if defined(m68k) or defined(arm)}
  158. cpuinfo,
  159. {$endif m68k or arm}
  160. aasmcpu,
  161. owbase,owar
  162. ;
  163. var
  164. CAssembler : array[tasm] of TAssemblerClass;
  165. function fixline(s:string):string;
  166. {
  167. return s with all leading and ending spaces and tabs removed
  168. }
  169. var
  170. i,j,k : integer;
  171. begin
  172. i:=length(s);
  173. while (i>0) and (s[i] in [#9,' ']) do
  174. dec(i);
  175. j:=1;
  176. while (j<i) and (s[j] in [#9,' ']) do
  177. inc(j);
  178. for k:=j to i do
  179. if s[k] in [#0..#31,#127..#255] then
  180. s[k]:='.';
  181. fixline:=Copy(s,j,i-j+1);
  182. end;
  183. {*****************************************************************************
  184. TAssembler
  185. *****************************************************************************}
  186. Constructor TAssembler.Create(smart:boolean);
  187. begin
  188. { load start values }
  189. AsmFileName:=current_module.AsmFilename;
  190. ObjFileName:=current_module.ObjFileName;
  191. name:=Lower(current_module.modulename^);
  192. path:=current_module.outputpath;
  193. asmprefix := current_module.asmprefix^;
  194. if current_module.outputpath = '' then
  195. ppufilename := ''
  196. else
  197. ppufilename := current_module.ppufilename;
  198. SmartAsm:=smart;
  199. SmartFilesCount:=0;
  200. SmartHeaderCount:=0;
  201. SmartLinkOFiles.Clear;
  202. end;
  203. Destructor TAssembler.Destroy;
  204. begin
  205. end;
  206. procedure TAssembler.NextSmartName(place:tcutplace);
  207. var
  208. s : string;
  209. begin
  210. inc(SmartFilesCount);
  211. if SmartFilesCount>999999 then
  212. Message(asmw_f_too_many_asm_files);
  213. case place of
  214. cut_begin :
  215. begin
  216. inc(SmartHeaderCount);
  217. s:=asmprefix+tostr(SmartHeaderCount)+'h';
  218. end;
  219. cut_normal :
  220. s:=asmprefix+tostr(SmartHeaderCount)+'s';
  221. cut_end :
  222. s:=asmprefix+tostr(SmartHeaderCount)+'t';
  223. end;
  224. AsmFileName:=Path+FixFileName(s+tostr(SmartFilesCount)+target_info.asmext);
  225. ObjFileName:=Path+FixFileName(s+tostr(SmartFilesCount)+target_info.objext);
  226. { insert in container so it can be cleared after the linking }
  227. SmartLinkOFiles.Insert(ObjFileName);
  228. end;
  229. {*****************************************************************************
  230. TExternalAssembler
  231. *****************************************************************************}
  232. Function DoPipe:boolean;
  233. begin
  234. DoPipe:=(cs_asm_pipe in current_settings.globalswitches) and
  235. (([cs_asm_extern,cs_asm_leave,cs_link_on_target] * current_settings.globalswitches) = []) and
  236. ((target_asm.id in [as_gas,as_ggas,as_darwin,as_powerpc_xcoff]));
  237. end;
  238. Constructor TExternalAssembler.Create(smart:boolean);
  239. begin
  240. inherited Create(smart);
  241. if SmartAsm then
  242. begin
  243. path:=FixPath(ChangeFileExt(AsmFileName,target_info.smartext),false);
  244. CreateSmartLinkPath(path);
  245. end;
  246. Outcnt:=0;
  247. end;
  248. procedure TExternalAssembler.CreateSmartLinkPath(const s:string);
  249. procedure DeleteFilesWithExt(const AExt:string);
  250. var
  251. dir : TSearchRec;
  252. begin
  253. if findfirst(s+source_info.dirsep+'*'+AExt,faAnyFile,dir) = 0 then
  254. begin
  255. repeat
  256. DeleteFile(s+source_info.dirsep+dir.name);
  257. until findnext(dir) <> 0;
  258. end;
  259. findclose(dir);
  260. end;
  261. var
  262. hs : string;
  263. begin
  264. if PathExists(s,false) then
  265. begin
  266. { the path exists, now we clean only all the .o and .s files }
  267. DeleteFilesWithExt(target_info.objext);
  268. DeleteFilesWithExt(target_info.asmext);
  269. end
  270. else
  271. begin
  272. hs:=s;
  273. if hs[length(hs)] in ['/','\'] then
  274. delete(hs,length(hs),1);
  275. {$push} {$I-}
  276. mkdir(hs);
  277. {$pop}
  278. if ioresult<>0 then;
  279. end;
  280. end;
  281. const
  282. lastas : byte=255;
  283. var
  284. LastASBin : TCmdStr;
  285. Function TExternalAssembler.FindAssembler:string;
  286. var
  287. asfound : boolean;
  288. UtilExe : string;
  289. begin
  290. asfound:=false;
  291. if cs_link_on_target in current_settings.globalswitches then
  292. begin
  293. { If linking on target, don't add any path PM }
  294. FindAssembler:=utilsprefix+ChangeFileExt(target_asm.asmbin,target_info.exeext);
  295. exit;
  296. end
  297. else
  298. UtilExe:=utilsprefix+ChangeFileExt(target_asm.asmbin,source_info.exeext);
  299. if lastas<>ord(target_asm.id) then
  300. begin
  301. lastas:=ord(target_asm.id);
  302. { is an assembler passed ? }
  303. if utilsdirectory<>'' then
  304. asfound:=FindFile(UtilExe,utilsdirectory,false,LastASBin);
  305. if not AsFound then
  306. asfound:=FindExe(UtilExe,false,LastASBin);
  307. if (not asfound) and not(cs_asm_extern in current_settings.globalswitches) then
  308. begin
  309. Message1(exec_e_assembler_not_found,LastASBin);
  310. current_settings.globalswitches:=current_settings.globalswitches+[cs_asm_extern];
  311. end;
  312. if asfound then
  313. Message1(exec_t_using_assembler,LastASBin);
  314. end;
  315. FindAssembler:=LastASBin;
  316. end;
  317. Function TExternalAssembler.CallAssembler(const command:string; const para:TCmdStr):Boolean;
  318. var
  319. DosExitCode : Integer;
  320. begin
  321. result:=true;
  322. if (cs_asm_extern in current_settings.globalswitches) then
  323. begin
  324. AsmRes.AddAsmCommand(command,para,name);
  325. exit;
  326. end;
  327. try
  328. FlushOutput;
  329. DosExitCode:=RequotedExecuteProcess(command,para);
  330. if DosExitCode<>0
  331. then begin
  332. Message1(exec_e_error_while_assembling,tostr(dosexitcode));
  333. result:=false;
  334. end;
  335. except on E:EOSError do
  336. begin
  337. Message1(exec_e_cant_call_assembler,tostr(E.ErrorCode));
  338. current_settings.globalswitches:=current_settings.globalswitches+[cs_asm_extern];
  339. result:=false;
  340. end;
  341. end;
  342. end;
  343. procedure TExternalAssembler.RemoveAsm;
  344. var
  345. g : file;
  346. begin
  347. if cs_asm_leave in current_settings.globalswitches then
  348. exit;
  349. if cs_asm_extern in current_settings.globalswitches then
  350. AsmRes.AddDeleteCommand(AsmFileName)
  351. else
  352. begin
  353. assign(g,AsmFileName);
  354. {$push} {$I-}
  355. erase(g);
  356. {$pop}
  357. if ioresult<>0 then;
  358. end;
  359. end;
  360. Function TExternalAssembler.DoAssemble:boolean;
  361. begin
  362. DoAssemble:=true;
  363. if DoPipe then
  364. exit;
  365. if not(cs_asm_extern in current_settings.globalswitches) then
  366. begin
  367. if SmartAsm then
  368. begin
  369. if (SmartFilesCount<=1) then
  370. Message1(exec_i_assembling_smart,name);
  371. end
  372. else
  373. Message1(exec_i_assembling,name);
  374. end;
  375. if CallAssembler(FindAssembler,MakeCmdLine) then
  376. RemoveAsm
  377. else
  378. begin
  379. DoAssemble:=false;
  380. GenerateError;
  381. end;
  382. end;
  383. Procedure TExternalAssembler.AsmFlush;
  384. begin
  385. if outcnt>0 then
  386. begin
  387. { suppress i/o error }
  388. {$push} {$I-}
  389. BlockWrite(outfile,outbuf,outcnt);
  390. {$pop}
  391. ioerror:=ioerror or (ioresult<>0);
  392. outcnt:=0;
  393. end;
  394. end;
  395. Procedure TExternalAssembler.AsmClear;
  396. begin
  397. outcnt:=0;
  398. end;
  399. Procedure TExternalAssembler.AsmWrite(const c: char);
  400. begin
  401. if OutCnt+1>=AsmOutSize then
  402. AsmFlush;
  403. OutBuf[OutCnt]:=c;
  404. inc(OutCnt);
  405. inc(AsmSize);
  406. end;
  407. Procedure TExternalAssembler.AsmWrite(const s:string);
  408. begin
  409. if OutCnt+length(s)>=AsmOutSize then
  410. AsmFlush;
  411. Move(s[1],OutBuf[OutCnt],length(s));
  412. inc(OutCnt,length(s));
  413. inc(AsmSize,length(s));
  414. end;
  415. Procedure TExternalAssembler.AsmWrite(const s:ansistring);
  416. var
  417. StartIndex, ToWrite: longint;
  418. begin
  419. if s='' then
  420. exit;
  421. if OutCnt+length(s)>=AsmOutSize then
  422. AsmFlush;
  423. StartIndex:=1;
  424. ToWrite:=length(s);
  425. while ToWrite>AsmOutSize do
  426. begin
  427. Move(s[StartIndex],OutBuf[OutCnt],AsmOutSize);
  428. inc(OutCnt,AsmOutSize);
  429. inc(AsmSize,AsmOutSize);
  430. AsmFlush;
  431. inc(StartIndex,AsmOutSize);
  432. dec(ToWrite,AsmOutSize);
  433. end;
  434. Move(s[StartIndex],OutBuf[OutCnt],ToWrite);
  435. inc(OutCnt,ToWrite);
  436. inc(AsmSize,ToWrite);
  437. end;
  438. procedure TExternalAssembler.AsmWriteLn(const c: char);
  439. begin
  440. AsmWrite(c);
  441. AsmLn;
  442. end;
  443. Procedure TExternalAssembler.AsmWriteLn(const s:string);
  444. begin
  445. AsmWrite(s);
  446. AsmLn;
  447. end;
  448. Procedure TExternalAssembler.AsmWriteLn(const s: ansistring);
  449. begin
  450. AsmWrite(s);
  451. AsmLn;
  452. end;
  453. Procedure TExternalAssembler.AsmWritePChar(p:pchar);
  454. var
  455. i,j : longint;
  456. begin
  457. i:=StrLen(p);
  458. j:=i;
  459. while j>0 do
  460. begin
  461. i:=min(j,AsmOutSize);
  462. if OutCnt+i>=AsmOutSize then
  463. AsmFlush;
  464. Move(p[0],OutBuf[OutCnt],i);
  465. inc(OutCnt,i);
  466. inc(AsmSize,i);
  467. dec(j,i);
  468. p:=pchar(@p[i]);
  469. end;
  470. end;
  471. Procedure TExternalAssembler.AsmLn;
  472. begin
  473. if OutCnt>=AsmOutSize-2 then
  474. AsmFlush;
  475. if (cs_link_on_target in current_settings.globalswitches) then
  476. begin
  477. OutBuf[OutCnt]:=target_info.newline[1];
  478. inc(OutCnt);
  479. inc(AsmSize);
  480. if length(target_info.newline)>1 then
  481. begin
  482. OutBuf[OutCnt]:=target_info.newline[2];
  483. inc(OutCnt);
  484. inc(AsmSize);
  485. end;
  486. end
  487. else
  488. begin
  489. OutBuf[OutCnt]:=source_info.newline[1];
  490. inc(OutCnt);
  491. inc(AsmSize);
  492. if length(source_info.newline)>1 then
  493. begin
  494. OutBuf[OutCnt]:=source_info.newline[2];
  495. inc(OutCnt);
  496. inc(AsmSize);
  497. end;
  498. end;
  499. end;
  500. function TExternalAssembler.MakeCmdLine: TCmdStr;
  501. begin
  502. result:=target_asm.asmcmd;
  503. {$ifdef m68k}
  504. { TODO: use a better approach for this }
  505. case current_settings.cputype of
  506. cpu_MC68000:
  507. result:='-march=68000 '+result;
  508. cpu_MC68020:
  509. result:='-march=68020 '+result;
  510. cpu_Coldfire:
  511. result:='-march=cfv4e '+result;
  512. end;
  513. {$endif}
  514. {$ifdef arm}
  515. if (target_info.system=system_arm_darwin) then
  516. Replace(result,'$ARCH',lower(cputypestr[current_settings.cputype]));
  517. {$endif arm}
  518. if (cs_link_on_target in current_settings.globalswitches) then
  519. begin
  520. Replace(result,'$ASM',maybequoted(ScriptFixFileName(AsmFileName)));
  521. Replace(result,'$OBJ',maybequoted(ScriptFixFileName(ObjFileName)));
  522. end
  523. else
  524. begin
  525. {$ifdef hasunix}
  526. if DoPipe then
  527. Replace(result,'$ASM','')
  528. else
  529. {$endif}
  530. Replace(result,'$ASM',maybequoted(AsmFileName));
  531. Replace(result,'$OBJ',maybequoted(ObjFileName));
  532. end;
  533. if (cs_create_pic in current_settings.moduleswitches) then
  534. Replace(result,'$PIC','-KPIC')
  535. else
  536. Replace(result,'$PIC','');
  537. if (cs_asm_source in current_settings.globalswitches) then
  538. Replace(result,'$NOWARN','')
  539. else
  540. Replace(result,'$NOWARN','-W');
  541. end;
  542. procedure TExternalAssembler.AsmCreate(Aplace:tcutplace);
  543. begin
  544. if SmartAsm then
  545. NextSmartName(Aplace);
  546. {$ifdef hasunix}
  547. if DoPipe then
  548. begin
  549. if SmartAsm then
  550. begin
  551. if (SmartFilesCount<=1) then
  552. Message1(exec_i_assembling_smart,name);
  553. end
  554. else
  555. Message1(exec_i_assembling_pipe,AsmFileName);
  556. POpen(outfile,maybequoted(FindAssembler)+' '+MakeCmdLine,'W');
  557. end
  558. else
  559. {$endif}
  560. begin
  561. Assign(outfile,AsmFileName);
  562. {$push} {$I-}
  563. Rewrite(outfile,1);
  564. {$pop}
  565. if ioresult<>0 then
  566. begin
  567. ioerror:=true;
  568. Message1(exec_d_cant_create_asmfile,AsmFileName);
  569. end;
  570. end;
  571. outcnt:=0;
  572. AsmSize:=0;
  573. AsmStartSize:=0;
  574. end;
  575. procedure TExternalAssembler.AsmClose;
  576. var
  577. f : file;
  578. FileAge : longint;
  579. begin
  580. AsmFlush;
  581. {$ifdef hasunix}
  582. if DoPipe then
  583. begin
  584. if PClose(outfile) <> 0 then
  585. GenerateError;
  586. end
  587. else
  588. {$endif}
  589. begin
  590. {Touch Assembler time to ppu time is there is a ppufilename}
  591. if ppufilename<>'' then
  592. begin
  593. Assign(f,ppufilename);
  594. {$push} {$I-}
  595. reset(f,1);
  596. {$pop}
  597. if ioresult=0 then
  598. begin
  599. FileAge := FileGetDate(GetFileHandle(f));
  600. close(f);
  601. reset(outfile,1);
  602. FileSetDate(GetFileHandle(outFile),FileAge);
  603. end;
  604. end;
  605. close(outfile);
  606. end;
  607. end;
  608. procedure TExternalAssembler.WriteSourceLine(hp: tailineinfo);
  609. begin
  610. { load infile }
  611. if lastfileinfo.fileindex<>hp.fileinfo.fileindex then
  612. begin
  613. infile:=current_module.sourcefiles.get_file(hp.fileinfo.fileindex);
  614. if assigned(infile) then
  615. begin
  616. { open only if needed !! }
  617. if (cs_asm_source in current_settings.globalswitches) then
  618. infile.open;
  619. end;
  620. { avoid unnecessary reopens of the same file !! }
  621. lastfileinfo.fileindex:=hp.fileinfo.fileindex;
  622. { be sure to change line !! }
  623. lastfileinfo.line:=-1;
  624. end;
  625. { write source }
  626. if (cs_asm_source in current_settings.globalswitches) and
  627. assigned(infile) then
  628. begin
  629. if (infile<>lastinfile) then
  630. begin
  631. AsmWriteLn(target_asm.comment+'['+infile.name+']');
  632. if assigned(lastinfile) then
  633. lastinfile.close;
  634. end;
  635. if (hp.fileinfo.line<>lastfileinfo.line) and
  636. (hp.fileinfo.line<infile.maxlinebuf) then
  637. begin
  638. if (hp.fileinfo.line<>0) and
  639. (infile.linebuf^[hp.fileinfo.line]>=0) then
  640. AsmWriteLn(target_asm.comment+'['+tostr(hp.fileinfo.line)+'] '+
  641. fixline(infile.GetLineStr(hp.fileinfo.line)));
  642. { set it to a negative value !
  643. to make that is has been read already !! PM }
  644. if (infile.linebuf^[hp.fileinfo.line]>=0) then
  645. infile.linebuf^[hp.fileinfo.line]:=-infile.linebuf^[hp.fileinfo.line]-1;
  646. end;
  647. end;
  648. lastfileinfo:=hp.fileinfo;
  649. lastinfile:=infile;
  650. end;
  651. procedure TExternalAssembler.WriteTempalloc(hp: tai_tempalloc);
  652. begin
  653. {$ifdef EXTDEBUG}
  654. if assigned(hp.problem) then
  655. AsmWriteLn(target_asm.comment+'Temp '+tostr(hp.temppos)+','+
  656. tostr(hp.tempsize)+' '+hp.problem^)
  657. else
  658. {$endif EXTDEBUG}
  659. AsmWriteLn(target_asm.comment+'Temp '+tostr(hp.temppos)+','+
  660. tostr(hp.tempsize)+' '+tempallocstr[hp.allocation]);
  661. end;
  662. procedure TExternalAssembler.WriteTree(p:TAsmList);
  663. begin
  664. end;
  665. procedure TExternalAssembler.WriteAsmList;
  666. begin
  667. end;
  668. procedure TExternalAssembler.MakeObject;
  669. begin
  670. AsmCreate(cut_normal);
  671. FillChar(lastfileinfo, sizeof(lastfileinfo), 0);
  672. lastfileinfo.line := -1;
  673. lastinfile := nil;
  674. lastsectype := sec_none;
  675. WriteAsmList;
  676. AsmClose;
  677. if not(ioerror) then
  678. DoAssemble;
  679. end;
  680. {*****************************************************************************
  681. TInternalAssembler
  682. *****************************************************************************}
  683. constructor TInternalAssembler.create(smart:boolean);
  684. begin
  685. inherited create(smart);
  686. ObjOutput:=nil;
  687. ObjData:=nil;
  688. SmartAsm:=smart;
  689. end;
  690. destructor TInternalAssembler.destroy;
  691. begin
  692. if assigned(ObjData) then
  693. ObjData.free;
  694. if assigned(ObjOutput) then
  695. ObjOutput.free;
  696. end;
  697. procedure TInternalAssembler.WriteStab(p:pchar);
  698. function consumecomma(var p:pchar):boolean;
  699. begin
  700. while (p^=' ') do
  701. inc(p);
  702. result:=(p^=',');
  703. inc(p);
  704. end;
  705. function consumenumber(var p:pchar;out value:longint):boolean;
  706. var
  707. hs : string;
  708. len,
  709. code : integer;
  710. begin
  711. value:=0;
  712. while (p^=' ') do
  713. inc(p);
  714. len:=0;
  715. while (p^ in ['0'..'9']) do
  716. begin
  717. inc(len);
  718. hs[len]:=p^;
  719. inc(p);
  720. end;
  721. if len>0 then
  722. begin
  723. hs[0]:=chr(len);
  724. val(hs,value,code);
  725. end
  726. else
  727. code:=-1;
  728. result:=(code=0);
  729. end;
  730. function consumeoffset(var p:pchar;out relocsym:tobjsymbol;out value:longint):boolean;
  731. var
  732. hs : string;
  733. len,
  734. code : integer;
  735. pstart : pchar;
  736. sym : tobjsymbol;
  737. exprvalue : longint;
  738. gotmin,
  739. have_first_symbol,
  740. have_second_symbol,
  741. dosub : boolean;
  742. begin
  743. result:=false;
  744. value:=0;
  745. relocsym:=nil;
  746. gotmin:=false;
  747. have_first_symbol:=false;
  748. have_second_symbol:=false;
  749. repeat
  750. dosub:=false;
  751. exprvalue:=0;
  752. if gotmin then
  753. begin
  754. dosub:=true;
  755. gotmin:=false;
  756. end;
  757. while (p^=' ') do
  758. inc(p);
  759. case p^ of
  760. #0 :
  761. break;
  762. ' ' :
  763. inc(p);
  764. '0'..'9' :
  765. begin
  766. len:=0;
  767. while (p^ in ['0'..'9']) do
  768. begin
  769. inc(len);
  770. hs[len]:=p^;
  771. inc(p);
  772. end;
  773. hs[0]:=chr(len);
  774. val(hs,exprvalue,code);
  775. if code<>0 then
  776. internalerror(200702251);
  777. end;
  778. '.','_',
  779. 'A'..'Z',
  780. 'a'..'z' :
  781. begin
  782. pstart:=p;
  783. while not(p^ in [#0,' ','-','+']) do
  784. inc(p);
  785. len:=p-pstart;
  786. if len>255 then
  787. internalerror(200509187);
  788. move(pstart^,hs[1],len);
  789. hs[0]:=chr(len);
  790. sym:=objdata.symbolref(hs);
  791. have_first_symbol:=true;
  792. { Second symbol? }
  793. if assigned(relocsym) then
  794. begin
  795. if have_second_symbol then
  796. internalerror(2007032201);
  797. have_second_symbol:=true;
  798. if not have_first_symbol then
  799. internalerror(2007032202);
  800. { second symbol should substracted to first }
  801. if not dosub then
  802. internalerror(2007032203);
  803. if (relocsym.objsection<>sym.objsection) then
  804. internalerror(2005091810);
  805. exprvalue:=relocsym.address-sym.address;
  806. relocsym:=nil;
  807. dosub:=false;
  808. end
  809. else
  810. begin
  811. relocsym:=sym;
  812. if assigned(sym.objsection) then
  813. begin
  814. { first symbol should be + }
  815. if not have_first_symbol and dosub then
  816. internalerror(2007032204);
  817. have_first_symbol:=true;
  818. end;
  819. end;
  820. end;
  821. '+' :
  822. begin
  823. { nothing, by default addition is done }
  824. inc(p);
  825. end;
  826. '-' :
  827. begin
  828. gotmin:=true;
  829. inc(p);
  830. end;
  831. else
  832. internalerror(200509189);
  833. end;
  834. if dosub then
  835. dec(value,exprvalue)
  836. else
  837. inc(value,exprvalue);
  838. until false;
  839. result:=true;
  840. end;
  841. var
  842. stabstrlen,
  843. ofs,
  844. nline,
  845. nidx,
  846. nother,
  847. i : longint;
  848. stab : TObjStabEntry;
  849. relocsym : TObjSymbol;
  850. pstr,
  851. pcurr,
  852. pendquote : pchar;
  853. oldsec : TObjSection;
  854. begin
  855. pcurr:=nil;
  856. pstr:=nil;
  857. pendquote:=nil;
  858. relocsym:=nil;
  859. ofs:=0;
  860. { Parse string part }
  861. if (p[0]='"') then
  862. begin
  863. pstr:=@p[1];
  864. { Ignore \" inside the string }
  865. i:=1;
  866. while not((p[i]='"') and (p[i-1]<>'\')) and
  867. (p[i]<>#0) do
  868. inc(i);
  869. pendquote:=@p[i];
  870. pendquote^:=#0;
  871. pcurr:=@p[i+1];
  872. if not consumecomma(pcurr) then
  873. internalerror(200509181);
  874. end
  875. else
  876. pcurr:=p;
  877. { When in pass 1 then only alloc and leave }
  878. if ObjData.currpass=1 then
  879. begin
  880. ObjData.StabsSec.Alloc(sizeof(TObjStabEntry));
  881. if assigned(pstr) and (pstr[0]<>#0) then
  882. ObjData.StabStrSec.Alloc(strlen(pstr)+1);
  883. end
  884. else
  885. begin
  886. { Stabs format: nidx,nother,nline[,offset] }
  887. if not consumenumber(pcurr,nidx) then
  888. internalerror(200509182);
  889. if not consumecomma(pcurr) then
  890. internalerror(200509183);
  891. if not consumenumber(pcurr,nother) then
  892. internalerror(200509184);
  893. if not consumecomma(pcurr) then
  894. internalerror(200509185);
  895. if not consumenumber(pcurr,nline) then
  896. internalerror(200509186);
  897. if consumecomma(pcurr) then
  898. consumeoffset(pcurr,relocsym,ofs);
  899. { Generate stab entry }
  900. if assigned(pstr) and (pstr[0]<>#0) then
  901. begin
  902. stabstrlen:=strlen(pstr);
  903. {$ifdef optimizestabs}
  904. StabStrEntry:=nil;
  905. if (nidx=N_SourceFile) or (nidx=N_IncludeFile) then
  906. begin
  907. hs:=strpas(pstr);
  908. StabstrEntry:=StabStrDict.Find(hs);
  909. if not assigned(StabstrEntry) then
  910. begin
  911. StabstrEntry:=TStabStrEntry.Create(hs);
  912. StabstrEntry:=StabStrSec.Size;
  913. StabStrDict.Insert(StabstrEntry);
  914. { generate new stab }
  915. StabstrEntry:=nil;
  916. end;
  917. end;
  918. if assigned(StabstrEntry) then
  919. stab.strpos:=StabstrEntry.strpos
  920. else
  921. {$endif optimizestabs}
  922. begin
  923. stab.strpos:=ObjData.StabStrSec.Size;
  924. ObjData.StabStrSec.write(pstr^,stabstrlen+1);
  925. end;
  926. end
  927. else
  928. stab.strpos:=0;
  929. stab.ntype:=byte(nidx);
  930. stab.ndesc:=word(nline);
  931. stab.nother:=byte(nother);
  932. stab.nvalue:=ofs;
  933. { Write the stab first without the value field. Then
  934. write a the value field with relocation }
  935. oldsec:=ObjData.CurrObjSec;
  936. ObjData.SetSection(ObjData.StabsSec);
  937. ObjData.Writebytes(stab,sizeof(TObjStabEntry)-4);
  938. ObjData.Writereloc(stab.nvalue,4,relocsym,RELOC_ABSOLUTE32);
  939. ObjData.setsection(oldsec);
  940. end;
  941. if assigned(pendquote) then
  942. pendquote^:='"';
  943. end;
  944. function TInternalAssembler.MaybeNextList(var hp:Tai):boolean;
  945. begin
  946. { maybe end of list }
  947. while not assigned(hp) do
  948. begin
  949. if currlistidx<lists then
  950. begin
  951. inc(currlistidx);
  952. currlist:=list[currlistidx];
  953. hp:=Tai(currList.first);
  954. end
  955. else
  956. begin
  957. MaybeNextList:=false;
  958. exit;
  959. end;
  960. end;
  961. MaybeNextList:=true;
  962. end;
  963. function TInternalAssembler.SetIndirectToSymbol(hp: Tai; const indirectname: string): Boolean;
  964. var
  965. objsym : TObjSymbol;
  966. indsym : TObjSymbol;
  967. begin
  968. Result:=
  969. Assigned(hp) and
  970. (hp.typ=ait_symbol);
  971. if not Result then
  972. Exit;
  973. objsym:=Objdata.SymbolRef(tai_symbol(hp).sym);
  974. objsym.size:=0;
  975. indsym := TObjSymbol(ObjData.ObjSymbolList.Find(indirectname));
  976. if not Assigned(indsym) then
  977. begin
  978. { it's possible that indirect symbol is not present in the list,
  979. so we must create it as undefined }
  980. indsym:=TObjSymbol.Create(ObjData.ObjSymbolList, indirectname);
  981. indsym.typ:=AT_NONE;
  982. indsym.bind:=AB_NONE;
  983. end;
  984. objsym.indsymbol:=indsym;
  985. Result:=true;
  986. end;
  987. function TInternalAssembler.TreePass0(hp:Tai):Tai;
  988. var
  989. objsym,
  990. objsymend : TObjSymbol;
  991. begin
  992. while assigned(hp) do
  993. begin
  994. case hp.typ of
  995. ait_align :
  996. begin
  997. if tai_align_abstract(hp).aligntype>1 then
  998. begin
  999. { always use the maximum fillsize in this pass to avoid possible
  1000. short jumps to become out of range }
  1001. Tai_align_abstract(hp).fillsize:=Tai_align_abstract(hp).aligntype;
  1002. ObjData.alloc(Tai_align_abstract(hp).fillsize);
  1003. { may need to increase alignment of section }
  1004. if tai_align_abstract(hp).aligntype>ObjData.CurrObjSec.secalign then
  1005. ObjData.CurrObjSec.secalign:=tai_align_abstract(hp).aligntype;
  1006. end
  1007. else
  1008. Tai_align_abstract(hp).fillsize:=0;
  1009. end;
  1010. ait_datablock :
  1011. begin
  1012. {$ifdef USE_COMM_IN_BSS}
  1013. if writingpackages and
  1014. Tai_datablock(hp).is_global then
  1015. ObjData.SymbolDefine(Tai_datablock(hp).sym)
  1016. else
  1017. {$endif USE_COMM_IN_BSS}
  1018. begin
  1019. ObjData.allocalign(used_align(size_2_align(Tai_datablock(hp).size),0,ObjData.CurrObjSec.secalign));
  1020. ObjData.SymbolDefine(Tai_datablock(hp).sym);
  1021. ObjData.alloc(Tai_datablock(hp).size);
  1022. end;
  1023. end;
  1024. ait_real_80bit :
  1025. ObjData.alloc(tai_real_80bit(hp).savesize);
  1026. ait_real_64bit :
  1027. ObjData.alloc(8);
  1028. ait_real_32bit :
  1029. ObjData.alloc(4);
  1030. ait_comp_64bit :
  1031. ObjData.alloc(8);
  1032. ait_const:
  1033. begin
  1034. { if symbols are provided we can calculate the value for relative symbols.
  1035. This is required for length calculation of leb128 constants }
  1036. if assigned(tai_const(hp).sym) then
  1037. begin
  1038. objsym:=Objdata.SymbolRef(tai_const(hp).sym);
  1039. { objsym already defined and there is endsym? }
  1040. if assigned(objsym.objsection) and assigned(tai_const(hp).endsym) then
  1041. begin
  1042. objsymend:=Objdata.SymbolRef(tai_const(hp).endsym);
  1043. { objsymend already defined? }
  1044. if assigned(objsymend.objsection) then
  1045. begin
  1046. if objsymend.objsection<>objsym.objsection then
  1047. begin
  1048. { leb128 relative constants are not relocatable, but other types are,
  1049. given that objsym belongs to the current section. }
  1050. if (Tai_const(hp).consttype in [aitconst_uleb128bit,aitconst_sleb128bit]) or
  1051. (objsym.objsection<>ObjData.CurrObjSec) then
  1052. InternalError(200404124);
  1053. end
  1054. else
  1055. Tai_const(hp).value:=objsymend.address-objsym.address+Tai_const(hp).symofs;
  1056. end;
  1057. end;
  1058. end;
  1059. ObjData.alloc(tai_const(hp).size);
  1060. end;
  1061. ait_directive:
  1062. begin
  1063. case tai_directive(hp).directive of
  1064. asd_indirect_symbol:
  1065. { handled in TreePass1 }
  1066. ;
  1067. asd_lazy_reference:
  1068. begin
  1069. if tai_directive(hp).name='' then
  1070. Internalerror(2009112101);
  1071. objsym:=ObjData.symbolref(tai_directive(hp).name);
  1072. objsym.bind:=AB_LAZY;
  1073. end;
  1074. asd_reference:
  1075. { ignore for now, but should be added}
  1076. ;
  1077. else
  1078. internalerror(2010011101);
  1079. end;
  1080. end;
  1081. ait_section:
  1082. begin
  1083. ObjData.CreateSection(Tai_section(hp).sectype,Tai_section(hp).name^,Tai_section(hp).secorder);
  1084. Tai_section(hp).sec:=ObjData.CurrObjSec;
  1085. end;
  1086. ait_symbol :
  1087. begin
  1088. { needs extra support in the internal assembler }
  1089. { the value is just ignored }
  1090. {if tai_symbol(hp).has_value then
  1091. internalerror(2009090804); ;}
  1092. ObjData.SymbolDefine(Tai_symbol(hp).sym);
  1093. end;
  1094. ait_label :
  1095. ObjData.SymbolDefine(Tai_label(hp).labsym);
  1096. ait_string :
  1097. ObjData.alloc(Tai_string(hp).len);
  1098. ait_instruction :
  1099. begin
  1100. { reset instructions which could change in pass 2 }
  1101. Taicpu(hp).resetpass2;
  1102. ObjData.alloc(Taicpu(hp).Pass1(ObjData));
  1103. end;
  1104. ait_cutobject :
  1105. if SmartAsm then
  1106. break;
  1107. end;
  1108. hp:=Tai(hp.next);
  1109. end;
  1110. TreePass0:=hp;
  1111. end;
  1112. function TInternalAssembler.TreePass1(hp:Tai):Tai;
  1113. var
  1114. objsym,
  1115. objsymend : TObjSymbol;
  1116. begin
  1117. while assigned(hp) do
  1118. begin
  1119. case hp.typ of
  1120. ait_align :
  1121. begin
  1122. if tai_align_abstract(hp).aligntype>1 then
  1123. begin
  1124. { here we must determine the fillsize which is used in pass2 }
  1125. Tai_align_abstract(hp).fillsize:=align(ObjData.CurrObjSec.Size,Tai_align_abstract(hp).aligntype)-
  1126. ObjData.CurrObjSec.Size;
  1127. ObjData.alloc(Tai_align_abstract(hp).fillsize);
  1128. end;
  1129. end;
  1130. ait_datablock :
  1131. begin
  1132. if (oso_data in ObjData.CurrObjSec.secoptions) then
  1133. Message(asmw_e_alloc_data_only_in_bss);
  1134. {$ifdef USE_COMM_IN_BSS}
  1135. if writingpackages and
  1136. Tai_datablock(hp).is_global then
  1137. begin
  1138. objsym:=ObjData.SymbolDefine(Tai_datablock(hp).sym);
  1139. objsym.size:=Tai_datablock(hp).size;
  1140. objsym.bind:=AB_COMMON;
  1141. objsym.alignment:=needtowritealignmentalsoforELF;
  1142. end
  1143. else
  1144. {$endif USE_COMM_IN_BSS}
  1145. begin
  1146. ObjData.allocalign(used_align(size_2_align(Tai_datablock(hp).size),0,ObjData.CurrObjSec.secalign));
  1147. objsym:=ObjData.SymbolDefine(Tai_datablock(hp).sym);
  1148. objsym.size:=Tai_datablock(hp).size;
  1149. ObjData.alloc(Tai_datablock(hp).size);
  1150. end;
  1151. end;
  1152. ait_real_80bit :
  1153. ObjData.alloc(tai_real_80bit(hp).savesize);
  1154. ait_real_64bit :
  1155. ObjData.alloc(8);
  1156. ait_real_32bit :
  1157. ObjData.alloc(4);
  1158. ait_comp_64bit :
  1159. ObjData.alloc(8);
  1160. ait_const:
  1161. begin
  1162. { Recalculate relative symbols }
  1163. if assigned(tai_const(hp).sym) and
  1164. assigned(tai_const(hp).endsym) then
  1165. begin
  1166. objsym:=Objdata.SymbolRef(tai_const(hp).sym);
  1167. objsymend:=Objdata.SymbolRef(tai_const(hp).endsym);
  1168. if objsymend.objsection<>objsym.objsection then
  1169. begin
  1170. if (Tai_const(hp).consttype in [aitconst_uleb128bit,aitconst_sleb128bit]) or
  1171. (objsym.objsection<>ObjData.CurrObjSec) then
  1172. internalerror(200905042);
  1173. end
  1174. else
  1175. Tai_const(hp).value:=objsymend.address-objsym.address+Tai_const(hp).symofs;
  1176. end;
  1177. ObjData.alloc(tai_const(hp).size);
  1178. end;
  1179. ait_section:
  1180. begin
  1181. { use cached value }
  1182. ObjData.setsection(Tai_section(hp).sec);
  1183. end;
  1184. ait_stab :
  1185. begin
  1186. if assigned(Tai_stab(hp).str) then
  1187. WriteStab(Tai_stab(hp).str);
  1188. end;
  1189. ait_symbol :
  1190. ObjData.SymbolDefine(Tai_symbol(hp).sym);
  1191. ait_symbol_end :
  1192. begin
  1193. objsym:=ObjData.SymbolRef(Tai_symbol_end(hp).sym);
  1194. objsym.size:=ObjData.CurrObjSec.Size-objsym.offset;
  1195. end;
  1196. ait_label :
  1197. ObjData.SymbolDefine(Tai_label(hp).labsym);
  1198. ait_string :
  1199. ObjData.alloc(Tai_string(hp).len);
  1200. ait_instruction :
  1201. ObjData.alloc(Taicpu(hp).Pass1(ObjData));
  1202. ait_cutobject :
  1203. if SmartAsm then
  1204. break;
  1205. ait_directive :
  1206. begin
  1207. case tai_directive(hp).directive of
  1208. asd_indirect_symbol:
  1209. if tai_directive(hp).name='' then
  1210. Internalerror(2009101103)
  1211. else if not SetIndirectToSymbol(Tai(hp.Previous), tai_directive(hp).name) then
  1212. Internalerror(2009101102);
  1213. asd_lazy_reference:
  1214. { handled in TreePass0 }
  1215. ;
  1216. asd_reference:
  1217. { ignore for now, but should be added}
  1218. ;
  1219. else
  1220. internalerror(2010011102);
  1221. end;
  1222. end;
  1223. end;
  1224. hp:=Tai(hp.next);
  1225. end;
  1226. TreePass1:=hp;
  1227. end;
  1228. function TInternalAssembler.TreePass2(hp:Tai):Tai;
  1229. var
  1230. fillbuffer : tfillbuffer;
  1231. {$ifdef x86}
  1232. co : comp;
  1233. {$endif x86}
  1234. leblen : byte;
  1235. lebbuf : array[0..63] of byte;
  1236. objsym,
  1237. objsymend : TObjSymbol;
  1238. zerobuf : array[0..63] of byte;
  1239. relative_reloc: boolean;
  1240. begin
  1241. fillchar(zerobuf,sizeof(zerobuf),0);
  1242. { main loop }
  1243. while assigned(hp) do
  1244. begin
  1245. case hp.typ of
  1246. ait_align :
  1247. begin
  1248. if tai_align_abstract(hp).aligntype>ObjData.CurrObjSec.secalign then
  1249. InternalError(2012072301);
  1250. if oso_data in ObjData.CurrObjSec.secoptions then
  1251. ObjData.writebytes(Tai_align_abstract(hp).calculatefillbuf(fillbuffer,oso_executable in ObjData.CurrObjSec.secoptions)^,
  1252. Tai_align_abstract(hp).fillsize)
  1253. else
  1254. ObjData.alloc(Tai_align_abstract(hp).fillsize);
  1255. end;
  1256. ait_section :
  1257. begin
  1258. { use cached value }
  1259. ObjData.setsection(Tai_section(hp).sec);
  1260. end;
  1261. ait_symbol :
  1262. begin
  1263. ObjOutput.exportsymbol(ObjData.SymbolRef(Tai_symbol(hp).sym));
  1264. end;
  1265. ait_symbol_end :
  1266. begin
  1267. { recalculate size, as some preceding instructions
  1268. could have been changed to smaller size }
  1269. objsym:=ObjData.SymbolRef(Tai_symbol_end(hp).sym);
  1270. objsym.size:=ObjData.CurrObjSec.Size-objsym.offset;
  1271. end;
  1272. ait_datablock :
  1273. begin
  1274. ObjOutput.exportsymbol(ObjData.SymbolRef(Tai_datablock(hp).sym));
  1275. {$ifdef USE_COMM_IN_BSS}
  1276. if not(writingpackages and
  1277. Tai_datablock(hp).is_global) then
  1278. {$endif USE_COMM_IN_BSS}
  1279. begin
  1280. ObjData.allocalign(used_align(size_2_align(Tai_datablock(hp).size),0,ObjData.CurrObjSec.secalign));
  1281. ObjData.alloc(Tai_datablock(hp).size);
  1282. end;
  1283. end;
  1284. ait_real_80bit :
  1285. begin
  1286. ObjData.writebytes(Tai_real_80bit(hp).value,10);
  1287. ObjData.writebytes(zerobuf,Tai_real_80bit(hp).savesize-10);
  1288. end;
  1289. ait_real_64bit :
  1290. ObjData.writebytes(Tai_real_64bit(hp).value,8);
  1291. ait_real_32bit :
  1292. ObjData.writebytes(Tai_real_32bit(hp).value,4);
  1293. ait_comp_64bit :
  1294. begin
  1295. {$ifdef x86}
  1296. co:=comp(Tai_comp_64bit(hp).value);
  1297. ObjData.writebytes(co,8);
  1298. {$endif x86}
  1299. end;
  1300. ait_string :
  1301. ObjData.writebytes(Tai_string(hp).str^,Tai_string(hp).len);
  1302. ait_const :
  1303. begin
  1304. { Recalculate relative symbols, addresses of forward references
  1305. can be changed in treepass1 }
  1306. relative_reloc:=false;
  1307. if assigned(tai_const(hp).sym) and
  1308. assigned(tai_const(hp).endsym) then
  1309. begin
  1310. objsym:=Objdata.SymbolRef(tai_const(hp).sym);
  1311. objsymend:=Objdata.SymbolRef(tai_const(hp).endsym);
  1312. relative_reloc:=(objsym.objsection<>objsymend.objsection);
  1313. Tai_const(hp).value:=objsymend.address-objsym.address+Tai_const(hp).symofs;
  1314. end;
  1315. case tai_const(hp).consttype of
  1316. aitconst_64bit,
  1317. aitconst_32bit,
  1318. aitconst_16bit,
  1319. aitconst_64bit_unaligned,
  1320. aitconst_32bit_unaligned,
  1321. aitconst_16bit_unaligned,
  1322. aitconst_8bit :
  1323. begin
  1324. if assigned(tai_const(hp).sym) and
  1325. not assigned(tai_const(hp).endsym) then
  1326. ObjData.writereloc(Tai_const(hp).symofs,tai_const(hp).size,Objdata.SymbolRef(tai_const(hp).sym),RELOC_ABSOLUTE)
  1327. else if relative_reloc then
  1328. ObjData.writereloc(ObjData.CurrObjSec.size+tai_const(hp).size-objsym.address+tai_const(hp).symofs,tai_const(hp).size,objsymend,RELOC_RELATIVE)
  1329. else
  1330. ObjData.writebytes(Tai_const(hp).value,tai_const(hp).size);
  1331. end;
  1332. aitconst_rva_symbol :
  1333. begin
  1334. { PE32+? }
  1335. if target_info.system=system_x86_64_win64 then
  1336. ObjData.writereloc(Tai_const(hp).symofs,sizeof(longint),Objdata.SymbolRef(tai_const(hp).sym),RELOC_RVA)
  1337. else
  1338. ObjData.writereloc(Tai_const(hp).symofs,sizeof(pint),Objdata.SymbolRef(tai_const(hp).sym),RELOC_RVA);
  1339. end;
  1340. aitconst_secrel32_symbol :
  1341. begin
  1342. { Required for DWARF2 support under Windows }
  1343. ObjData.writereloc(Tai_const(hp).symofs,sizeof(longint),Objdata.SymbolRef(tai_const(hp).sym),RELOC_SECREL32);
  1344. end;
  1345. aitconst_uleb128bit,
  1346. aitconst_sleb128bit :
  1347. begin
  1348. if tai_const(hp).consttype=aitconst_uleb128bit then
  1349. leblen:=EncodeUleb128(qword(Tai_const(hp).value),lebbuf)
  1350. else
  1351. leblen:=EncodeSleb128(Tai_const(hp).value,lebbuf);
  1352. if leblen<>tai_const(hp).size then
  1353. internalerror(200709271);
  1354. ObjData.writebytes(lebbuf,leblen);
  1355. end;
  1356. aitconst_darwin_dwarf_delta32,
  1357. aitconst_darwin_dwarf_delta64:
  1358. ObjData.writebytes(Tai_const(hp).value,tai_const(hp).size);
  1359. else
  1360. internalerror(200603254);
  1361. end;
  1362. end;
  1363. ait_label :
  1364. begin
  1365. { exporting shouldn't be necessary as labels are local,
  1366. but it's better to be on the safe side (PFV) }
  1367. ObjOutput.exportsymbol(ObjData.SymbolRef(Tai_label(hp).labsym));
  1368. end;
  1369. ait_instruction :
  1370. Taicpu(hp).Pass2(ObjData);
  1371. ait_stab :
  1372. WriteStab(Tai_stab(hp).str);
  1373. ait_function_name,
  1374. ait_force_line : ;
  1375. ait_cutobject :
  1376. if SmartAsm then
  1377. break;
  1378. {$ifdef TEST_WIN64_SEH}
  1379. ait_seh_directive :
  1380. tai_seh_directive(hp).generate_code(objdata);
  1381. {$endif TEST_WIN64_SEH}
  1382. end;
  1383. hp:=Tai(hp.next);
  1384. end;
  1385. TreePass2:=hp;
  1386. end;
  1387. procedure TInternalAssembler.writetree;
  1388. label
  1389. doexit;
  1390. var
  1391. hp : Tai;
  1392. ObjWriter : TObjectWriter;
  1393. begin
  1394. ObjWriter:=TObjectwriter.create;
  1395. ObjOutput:=CObjOutput.Create(ObjWriter);
  1396. ObjData:=ObjOutput.newObjData(ObjFileName);
  1397. { Pass 0 }
  1398. ObjData.currpass:=0;
  1399. ObjData.createsection(sec_code);
  1400. ObjData.beforealloc;
  1401. { start with list 1 }
  1402. currlistidx:=1;
  1403. currlist:=list[currlistidx];
  1404. hp:=Tai(currList.first);
  1405. while assigned(hp) do
  1406. begin
  1407. hp:=TreePass0(hp);
  1408. MaybeNextList(hp);
  1409. end;
  1410. ObjData.afteralloc;
  1411. { leave if errors have occured }
  1412. if errorcount>0 then
  1413. goto doexit;
  1414. { Pass 1 }
  1415. ObjData.currpass:=1;
  1416. ObjData.resetsections;
  1417. ObjData.beforealloc;
  1418. ObjData.createsection(sec_code);
  1419. { start with list 1 }
  1420. currlistidx:=1;
  1421. currlist:=list[currlistidx];
  1422. hp:=Tai(currList.first);
  1423. while assigned(hp) do
  1424. begin
  1425. hp:=TreePass1(hp);
  1426. MaybeNextList(hp);
  1427. end;
  1428. ObjData.createsection(sec_code);
  1429. ObjData.afteralloc;
  1430. { leave if errors have occured }
  1431. if errorcount>0 then
  1432. goto doexit;
  1433. { Pass 2 }
  1434. ObjData.currpass:=2;
  1435. ObjData.resetsections;
  1436. ObjData.beforewrite;
  1437. ObjData.createsection(sec_code);
  1438. { start with list 1 }
  1439. currlistidx:=1;
  1440. currlist:=list[currlistidx];
  1441. hp:=Tai(currList.first);
  1442. while assigned(hp) do
  1443. begin
  1444. hp:=TreePass2(hp);
  1445. MaybeNextList(hp);
  1446. end;
  1447. ObjData.createsection(sec_code);
  1448. ObjData.afterwrite;
  1449. { don't write the .o file if errors have occured }
  1450. if errorcount=0 then
  1451. begin
  1452. { write objectfile }
  1453. ObjOutput.startobjectfile(ObjFileName);
  1454. ObjOutput.writeobjectfile(ObjData);
  1455. end;
  1456. doexit:
  1457. { Cleanup }
  1458. ObjData.free;
  1459. ObjData:=nil;
  1460. ObjWriter.free;
  1461. end;
  1462. procedure TInternalAssembler.writetreesmart;
  1463. var
  1464. hp : Tai;
  1465. startsectype : TAsmSectiontype;
  1466. place: tcutplace;
  1467. ObjWriter : TObjectWriter;
  1468. begin
  1469. if not(cs_asm_leave in current_settings.globalswitches) then
  1470. ObjWriter:=TARObjectWriter.create(current_module.staticlibfilename)
  1471. else
  1472. ObjWriter:=TObjectwriter.create;
  1473. NextSmartName(cut_normal);
  1474. ObjOutput:=CObjOutput.Create(ObjWriter);
  1475. startsectype:=sec_code;
  1476. { start with list 1 }
  1477. currlistidx:=1;
  1478. currlist:=list[currlistidx];
  1479. hp:=Tai(currList.first);
  1480. while assigned(hp) do
  1481. begin
  1482. ObjData:=ObjOutput.newObjData(ObjFileName);
  1483. { Pass 0 }
  1484. ObjData.currpass:=0;
  1485. ObjData.resetsections;
  1486. ObjData.beforealloc;
  1487. ObjData.createsection(startsectype);
  1488. TreePass0(hp);
  1489. ObjData.afteralloc;
  1490. { leave if errors have occured }
  1491. if errorcount>0 then
  1492. break;
  1493. { Pass 1 }
  1494. ObjData.currpass:=1;
  1495. ObjData.resetsections;
  1496. ObjData.beforealloc;
  1497. ObjData.createsection(startsectype);
  1498. TreePass1(hp);
  1499. ObjData.afteralloc;
  1500. { leave if errors have occured }
  1501. if errorcount>0 then
  1502. break;
  1503. { Pass 2 }
  1504. ObjData.currpass:=2;
  1505. ObjOutput.startobjectfile(ObjFileName);
  1506. ObjData.resetsections;
  1507. ObjData.beforewrite;
  1508. ObjData.createsection(startsectype);
  1509. hp:=TreePass2(hp);
  1510. ObjData.afterwrite;
  1511. { leave if errors have occured }
  1512. if errorcount>0 then
  1513. break;
  1514. { write the current objectfile }
  1515. ObjOutput.writeobjectfile(ObjData);
  1516. ObjData.free;
  1517. ObjData:=nil;
  1518. { end of lists? }
  1519. if not MaybeNextList(hp) then
  1520. break;
  1521. { we will start a new objectfile so reset everything }
  1522. { The place can still change in the next while loop, so don't init }
  1523. { the writer yet (JM) }
  1524. if (hp.typ=ait_cutobject) then
  1525. place := Tai_cutobject(hp).place
  1526. else
  1527. place := cut_normal;
  1528. { avoid empty files }
  1529. startsectype:=sec_code;
  1530. while assigned(hp) and
  1531. (Tai(hp).typ in [ait_marker,ait_comment,ait_section,ait_cutobject]) do
  1532. begin
  1533. if Tai(hp).typ=ait_section then
  1534. startsectype:=Tai_section(hp).sectype;
  1535. if (Tai(hp).typ=ait_cutobject) then
  1536. place:=Tai_cutobject(hp).place;
  1537. hp:=Tai(hp.next);
  1538. end;
  1539. if not MaybeNextList(hp) then
  1540. break;
  1541. { start next objectfile }
  1542. NextSmartName(place);
  1543. end;
  1544. ObjData.free;
  1545. ObjData:=nil;
  1546. ObjWriter.free;
  1547. end;
  1548. procedure TInternalAssembler.MakeObject;
  1549. var to_do:set of TasmlistType;
  1550. i:TasmlistType;
  1551. procedure addlist(p:TAsmList);
  1552. begin
  1553. inc(lists);
  1554. list[lists]:=p;
  1555. end;
  1556. begin
  1557. to_do:=[low(Tasmlisttype)..high(Tasmlisttype)];
  1558. if usedeffileforexports then
  1559. exclude(to_do,al_exports);
  1560. if not(tf_section_threadvars in target_info.flags) then
  1561. exclude(to_do,al_threadvars);
  1562. for i:=low(TasmlistType) to high(TasmlistType) do
  1563. if (i in to_do) and (current_asmdata.asmlists[i]<>nil) then
  1564. addlist(current_asmdata.asmlists[i]);
  1565. if SmartAsm then
  1566. writetreesmart
  1567. else
  1568. writetree;
  1569. end;
  1570. {*****************************************************************************
  1571. Generate Assembler Files Main Procedure
  1572. *****************************************************************************}
  1573. Procedure GenerateAsm(smart:boolean);
  1574. var
  1575. a : TAssembler;
  1576. begin
  1577. if not assigned(CAssembler[target_asm.id]) then
  1578. Message(asmw_f_assembler_output_not_supported);
  1579. a:=CAssembler[target_asm.id].Create(smart);
  1580. a.MakeObject;
  1581. a.Free;
  1582. end;
  1583. Procedure OnlyAsm;
  1584. var
  1585. a : TExternalAssembler;
  1586. begin
  1587. a:=TExternalAssembler.Create(false);
  1588. a.DoAssemble;
  1589. a.Free;
  1590. end;
  1591. {*****************************************************************************
  1592. Init/Done
  1593. *****************************************************************************}
  1594. procedure RegisterAssembler(const r:tasminfo;c:TAssemblerClass);
  1595. var
  1596. t : tasm;
  1597. begin
  1598. t:=r.id;
  1599. if assigned(asminfos[t]) then
  1600. writeln('Warning: Assembler is already registered!')
  1601. else
  1602. Getmem(asminfos[t],sizeof(tasminfo));
  1603. asminfos[t]^:=r;
  1604. CAssembler[t]:=c;
  1605. end;
  1606. end.