assemble.pas 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763
  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(TObject)
  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:TPathStr);
  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:TPathStr);
  249. procedure DeleteFilesWithExt(const AExt:string);
  250. var
  251. dir : TRawByteSearchRec;
  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 : TPathStr;
  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 arm}
  504. if (target_info.system=system_arm_darwin) then
  505. Replace(result,'$ARCH',lower(cputypestr[current_settings.cputype]));
  506. {$endif arm}
  507. if (cs_link_on_target in current_settings.globalswitches) then
  508. begin
  509. Replace(result,'$ASM',maybequoted(ScriptFixFileName(AsmFileName)));
  510. Replace(result,'$OBJ',maybequoted(ScriptFixFileName(ObjFileName)));
  511. end
  512. else
  513. begin
  514. {$ifdef hasunix}
  515. if DoPipe then
  516. Replace(result,'$ASM','')
  517. else
  518. {$endif}
  519. Replace(result,'$ASM',maybequoted(AsmFileName));
  520. Replace(result,'$OBJ',maybequoted(ObjFileName));
  521. end;
  522. if (cs_create_pic in current_settings.moduleswitches) then
  523. Replace(result,'$PIC','-KPIC')
  524. else
  525. Replace(result,'$PIC','');
  526. if (cs_asm_source in current_settings.globalswitches) then
  527. Replace(result,'$NOWARN','')
  528. else
  529. Replace(result,'$NOWARN','-W');
  530. end;
  531. procedure TExternalAssembler.AsmCreate(Aplace:tcutplace);
  532. begin
  533. if SmartAsm then
  534. NextSmartName(Aplace);
  535. {$ifdef hasunix}
  536. if DoPipe then
  537. begin
  538. if SmartAsm then
  539. begin
  540. if (SmartFilesCount<=1) then
  541. Message1(exec_i_assembling_smart,name);
  542. end
  543. else
  544. Message1(exec_i_assembling_pipe,AsmFileName);
  545. POpen(outfile,maybequoted(FindAssembler)+' '+MakeCmdLine,'W');
  546. end
  547. else
  548. {$endif}
  549. begin
  550. Assign(outfile,AsmFileName);
  551. {$push} {$I-}
  552. Rewrite(outfile,1);
  553. {$pop}
  554. if ioresult<>0 then
  555. begin
  556. ioerror:=true;
  557. Message1(exec_d_cant_create_asmfile,AsmFileName);
  558. end;
  559. end;
  560. outcnt:=0;
  561. AsmSize:=0;
  562. AsmStartSize:=0;
  563. end;
  564. procedure TExternalAssembler.AsmClose;
  565. var
  566. f : file;
  567. FileAge : longint;
  568. begin
  569. AsmFlush;
  570. {$ifdef hasunix}
  571. if DoPipe then
  572. begin
  573. if PClose(outfile) <> 0 then
  574. GenerateError;
  575. end
  576. else
  577. {$endif}
  578. begin
  579. {Touch Assembler time to ppu time is there is a ppufilename}
  580. if ppufilename<>'' then
  581. begin
  582. Assign(f,ppufilename);
  583. {$push} {$I-}
  584. reset(f,1);
  585. {$pop}
  586. if ioresult=0 then
  587. begin
  588. FileAge := FileGetDate(GetFileHandle(f));
  589. close(f);
  590. reset(outfile,1);
  591. FileSetDate(GetFileHandle(outFile),FileAge);
  592. end;
  593. end;
  594. close(outfile);
  595. end;
  596. end;
  597. procedure TExternalAssembler.WriteSourceLine(hp: tailineinfo);
  598. var
  599. module : tmodule;
  600. begin
  601. { load infile }
  602. if (lastfileinfo.moduleindex<>hp.fileinfo.moduleindex) or
  603. (lastfileinfo.fileindex<>hp.fileinfo.fileindex) then
  604. begin
  605. { in case of a generic the module can be different }
  606. if current_module.unit_index=hp.fileinfo.moduleindex then
  607. module:=current_module
  608. else
  609. module:=get_module(hp.fileinfo.moduleindex);
  610. { during the compilation of the system unit there are cases when
  611. the fileinfo contains just zeros => invalid }
  612. if assigned(module) then
  613. infile:=module.sourcefiles.get_file(hp.fileinfo.fileindex)
  614. else
  615. infile:=nil;
  616. if assigned(infile) then
  617. begin
  618. { open only if needed !! }
  619. if (cs_asm_source in current_settings.globalswitches) then
  620. infile.open;
  621. end;
  622. { avoid unnecessary reopens of the same file !! }
  623. lastfileinfo.fileindex:=hp.fileinfo.fileindex;
  624. lastfileinfo.moduleindex:=hp.fileinfo.moduleindex;
  625. { be sure to change line !! }
  626. lastfileinfo.line:=-1;
  627. end;
  628. { write source }
  629. if (cs_asm_source in current_settings.globalswitches) and
  630. assigned(infile) then
  631. begin
  632. if (infile<>lastinfile) then
  633. begin
  634. AsmWriteLn(target_asm.comment+'['+infile.name+']');
  635. if assigned(lastinfile) then
  636. lastinfile.close;
  637. end;
  638. if (hp.fileinfo.line<>lastfileinfo.line) and
  639. (hp.fileinfo.line<infile.maxlinebuf) then
  640. begin
  641. if (hp.fileinfo.line<>0) and
  642. (infile.linebuf^[hp.fileinfo.line]>=0) then
  643. AsmWriteLn(target_asm.comment+'['+tostr(hp.fileinfo.line)+'] '+
  644. fixline(infile.GetLineStr(hp.fileinfo.line)));
  645. { set it to a negative value !
  646. to make that is has been read already !! PM }
  647. if (infile.linebuf^[hp.fileinfo.line]>=0) then
  648. infile.linebuf^[hp.fileinfo.line]:=-infile.linebuf^[hp.fileinfo.line]-1;
  649. end;
  650. end;
  651. lastfileinfo:=hp.fileinfo;
  652. lastinfile:=infile;
  653. end;
  654. procedure TExternalAssembler.WriteTempalloc(hp: tai_tempalloc);
  655. begin
  656. {$ifdef EXTDEBUG}
  657. if assigned(hp.problem) then
  658. AsmWriteLn(target_asm.comment+'Temp '+tostr(hp.temppos)+','+
  659. tostr(hp.tempsize)+' '+hp.problem^)
  660. else
  661. {$endif EXTDEBUG}
  662. AsmWriteLn(target_asm.comment+'Temp '+tostr(hp.temppos)+','+
  663. tostr(hp.tempsize)+' '+tempallocstr[hp.allocation]);
  664. end;
  665. procedure TExternalAssembler.WriteTree(p:TAsmList);
  666. begin
  667. end;
  668. procedure TExternalAssembler.WriteAsmList;
  669. begin
  670. end;
  671. procedure TExternalAssembler.MakeObject;
  672. begin
  673. AsmCreate(cut_normal);
  674. FillChar(lastfileinfo, sizeof(lastfileinfo), 0);
  675. lastfileinfo.line := -1;
  676. lastinfile := nil;
  677. lastsectype := sec_none;
  678. WriteAsmList;
  679. AsmClose;
  680. if not(ioerror) then
  681. DoAssemble;
  682. end;
  683. {*****************************************************************************
  684. TInternalAssembler
  685. *****************************************************************************}
  686. constructor TInternalAssembler.create(smart:boolean);
  687. begin
  688. inherited create(smart);
  689. ObjOutput:=nil;
  690. ObjData:=nil;
  691. SmartAsm:=smart;
  692. end;
  693. destructor TInternalAssembler.destroy;
  694. begin
  695. if assigned(ObjData) then
  696. ObjData.free;
  697. if assigned(ObjOutput) then
  698. ObjOutput.free;
  699. end;
  700. procedure TInternalAssembler.WriteStab(p:pchar);
  701. function consumecomma(var p:pchar):boolean;
  702. begin
  703. while (p^=' ') do
  704. inc(p);
  705. result:=(p^=',');
  706. inc(p);
  707. end;
  708. function consumenumber(var p:pchar;out value:longint):boolean;
  709. var
  710. hs : string;
  711. len,
  712. code : integer;
  713. begin
  714. value:=0;
  715. while (p^=' ') do
  716. inc(p);
  717. len:=0;
  718. while (p^ in ['0'..'9']) do
  719. begin
  720. inc(len);
  721. hs[len]:=p^;
  722. inc(p);
  723. end;
  724. if len>0 then
  725. begin
  726. hs[0]:=chr(len);
  727. val(hs,value,code);
  728. end
  729. else
  730. code:=-1;
  731. result:=(code=0);
  732. end;
  733. function consumeoffset(var p:pchar;out relocsym:tobjsymbol;out value:longint):boolean;
  734. var
  735. hs : string;
  736. len,
  737. code : integer;
  738. pstart : pchar;
  739. sym : tobjsymbol;
  740. exprvalue : longint;
  741. gotmin,
  742. have_first_symbol,
  743. have_second_symbol,
  744. dosub : boolean;
  745. begin
  746. result:=false;
  747. value:=0;
  748. relocsym:=nil;
  749. gotmin:=false;
  750. have_first_symbol:=false;
  751. have_second_symbol:=false;
  752. repeat
  753. dosub:=false;
  754. exprvalue:=0;
  755. if gotmin then
  756. begin
  757. dosub:=true;
  758. gotmin:=false;
  759. end;
  760. while (p^=' ') do
  761. inc(p);
  762. case p^ of
  763. #0 :
  764. break;
  765. ' ' :
  766. inc(p);
  767. '0'..'9' :
  768. begin
  769. len:=0;
  770. while (p^ in ['0'..'9']) do
  771. begin
  772. inc(len);
  773. hs[len]:=p^;
  774. inc(p);
  775. end;
  776. hs[0]:=chr(len);
  777. val(hs,exprvalue,code);
  778. if code<>0 then
  779. internalerror(200702251);
  780. end;
  781. '.','_',
  782. 'A'..'Z',
  783. 'a'..'z' :
  784. begin
  785. pstart:=p;
  786. while not(p^ in [#0,' ','-','+']) do
  787. inc(p);
  788. len:=p-pstart;
  789. if len>255 then
  790. internalerror(200509187);
  791. move(pstart^,hs[1],len);
  792. hs[0]:=chr(len);
  793. sym:=objdata.symbolref(hs);
  794. have_first_symbol:=true;
  795. { Second symbol? }
  796. if assigned(relocsym) then
  797. begin
  798. if have_second_symbol then
  799. internalerror(2007032201);
  800. have_second_symbol:=true;
  801. if not have_first_symbol then
  802. internalerror(2007032202);
  803. { second symbol should substracted to first }
  804. if not dosub then
  805. internalerror(2007032203);
  806. if (relocsym.objsection<>sym.objsection) then
  807. internalerror(2005091810);
  808. exprvalue:=relocsym.address-sym.address;
  809. relocsym:=nil;
  810. dosub:=false;
  811. end
  812. else
  813. begin
  814. relocsym:=sym;
  815. if assigned(sym.objsection) then
  816. begin
  817. { first symbol should be + }
  818. if not have_first_symbol and dosub then
  819. internalerror(2007032204);
  820. have_first_symbol:=true;
  821. end;
  822. end;
  823. end;
  824. '+' :
  825. begin
  826. { nothing, by default addition is done }
  827. inc(p);
  828. end;
  829. '-' :
  830. begin
  831. gotmin:=true;
  832. inc(p);
  833. end;
  834. else
  835. internalerror(200509189);
  836. end;
  837. if dosub then
  838. dec(value,exprvalue)
  839. else
  840. inc(value,exprvalue);
  841. until false;
  842. result:=true;
  843. end;
  844. var
  845. stabstrlen,
  846. ofs,
  847. nline,
  848. nidx,
  849. nother,
  850. i : longint;
  851. stab : TObjStabEntry;
  852. relocsym : TObjSymbol;
  853. pstr,
  854. pcurr,
  855. pendquote : pchar;
  856. oldsec : TObjSection;
  857. begin
  858. pcurr:=nil;
  859. pstr:=nil;
  860. pendquote:=nil;
  861. relocsym:=nil;
  862. ofs:=0;
  863. { Parse string part }
  864. if (p[0]='"') then
  865. begin
  866. pstr:=@p[1];
  867. { Ignore \" inside the string }
  868. i:=1;
  869. while not((p[i]='"') and (p[i-1]<>'\')) and
  870. (p[i]<>#0) do
  871. inc(i);
  872. pendquote:=@p[i];
  873. pendquote^:=#0;
  874. pcurr:=@p[i+1];
  875. if not consumecomma(pcurr) then
  876. internalerror(200509181);
  877. end
  878. else
  879. pcurr:=p;
  880. { When in pass 1 then only alloc and leave }
  881. if ObjData.currpass=1 then
  882. begin
  883. ObjData.StabsSec.Alloc(sizeof(TObjStabEntry));
  884. if assigned(pstr) and (pstr[0]<>#0) then
  885. ObjData.StabStrSec.Alloc(strlen(pstr)+1);
  886. end
  887. else
  888. begin
  889. { Stabs format: nidx,nother,nline[,offset] }
  890. if not consumenumber(pcurr,nidx) then
  891. internalerror(200509182);
  892. if not consumecomma(pcurr) then
  893. internalerror(200509183);
  894. if not consumenumber(pcurr,nother) then
  895. internalerror(200509184);
  896. if not consumecomma(pcurr) then
  897. internalerror(200509185);
  898. if not consumenumber(pcurr,nline) then
  899. internalerror(200509186);
  900. if consumecomma(pcurr) then
  901. consumeoffset(pcurr,relocsym,ofs);
  902. { Generate stab entry }
  903. if assigned(pstr) and (pstr[0]<>#0) then
  904. begin
  905. stabstrlen:=strlen(pstr);
  906. {$ifdef optimizestabs}
  907. StabStrEntry:=nil;
  908. if (nidx=N_SourceFile) or (nidx=N_IncludeFile) then
  909. begin
  910. hs:=strpas(pstr);
  911. StabstrEntry:=StabStrDict.Find(hs);
  912. if not assigned(StabstrEntry) then
  913. begin
  914. StabstrEntry:=TStabStrEntry.Create(hs);
  915. StabstrEntry:=StabStrSec.Size;
  916. StabStrDict.Insert(StabstrEntry);
  917. { generate new stab }
  918. StabstrEntry:=nil;
  919. end;
  920. end;
  921. if assigned(StabstrEntry) then
  922. stab.strpos:=StabstrEntry.strpos
  923. else
  924. {$endif optimizestabs}
  925. begin
  926. stab.strpos:=ObjData.StabStrSec.Size;
  927. ObjData.StabStrSec.write(pstr^,stabstrlen+1);
  928. end;
  929. end
  930. else
  931. stab.strpos:=0;
  932. stab.ntype:=byte(nidx);
  933. stab.ndesc:=word(nline);
  934. stab.nother:=byte(nother);
  935. stab.nvalue:=ofs;
  936. { Write the stab first without the value field. Then
  937. write a the value field with relocation }
  938. oldsec:=ObjData.CurrObjSec;
  939. ObjData.SetSection(ObjData.StabsSec);
  940. ObjData.Writebytes(stab,sizeof(TObjStabEntry)-4);
  941. ObjData.Writereloc(stab.nvalue,4,relocsym,RELOC_ABSOLUTE32);
  942. ObjData.setsection(oldsec);
  943. end;
  944. if assigned(pendquote) then
  945. pendquote^:='"';
  946. end;
  947. function TInternalAssembler.MaybeNextList(var hp:Tai):boolean;
  948. begin
  949. { maybe end of list }
  950. while not assigned(hp) do
  951. begin
  952. if currlistidx<lists then
  953. begin
  954. inc(currlistidx);
  955. currlist:=list[currlistidx];
  956. hp:=Tai(currList.first);
  957. end
  958. else
  959. begin
  960. MaybeNextList:=false;
  961. exit;
  962. end;
  963. end;
  964. MaybeNextList:=true;
  965. end;
  966. function TInternalAssembler.SetIndirectToSymbol(hp: Tai; const indirectname: string): Boolean;
  967. var
  968. objsym : TObjSymbol;
  969. indsym : TObjSymbol;
  970. begin
  971. Result:=
  972. Assigned(hp) and
  973. (hp.typ=ait_symbol);
  974. if not Result then
  975. Exit;
  976. objsym:=Objdata.SymbolRef(tai_symbol(hp).sym);
  977. objsym.size:=0;
  978. indsym := TObjSymbol(ObjData.ObjSymbolList.Find(indirectname));
  979. if not Assigned(indsym) then
  980. begin
  981. { it's possible that indirect symbol is not present in the list,
  982. so we must create it as undefined }
  983. indsym:=TObjSymbol.Create(ObjData.ObjSymbolList, indirectname);
  984. indsym.typ:=AT_NONE;
  985. indsym.bind:=AB_NONE;
  986. end;
  987. objsym.indsymbol:=indsym;
  988. Result:=true;
  989. end;
  990. function TInternalAssembler.TreePass0(hp:Tai):Tai;
  991. var
  992. objsym,
  993. objsymend : TObjSymbol;
  994. begin
  995. while assigned(hp) do
  996. begin
  997. case hp.typ of
  998. ait_align :
  999. begin
  1000. if tai_align_abstract(hp).aligntype>1 then
  1001. begin
  1002. { always use the maximum fillsize in this pass to avoid possible
  1003. short jumps to become out of range }
  1004. Tai_align_abstract(hp).fillsize:=Tai_align_abstract(hp).aligntype;
  1005. ObjData.alloc(Tai_align_abstract(hp).fillsize);
  1006. { may need to increase alignment of section }
  1007. if tai_align_abstract(hp).aligntype>ObjData.CurrObjSec.secalign then
  1008. ObjData.CurrObjSec.secalign:=tai_align_abstract(hp).aligntype;
  1009. end
  1010. else
  1011. Tai_align_abstract(hp).fillsize:=0;
  1012. end;
  1013. ait_datablock :
  1014. begin
  1015. {$ifdef USE_COMM_IN_BSS}
  1016. if writingpackages and
  1017. Tai_datablock(hp).is_global then
  1018. ObjData.SymbolDefine(Tai_datablock(hp).sym)
  1019. else
  1020. {$endif USE_COMM_IN_BSS}
  1021. begin
  1022. ObjData.allocalign(used_align(size_2_align(Tai_datablock(hp).size),0,ObjData.CurrObjSec.secalign));
  1023. ObjData.SymbolDefine(Tai_datablock(hp).sym);
  1024. ObjData.alloc(Tai_datablock(hp).size);
  1025. end;
  1026. end;
  1027. ait_real_80bit :
  1028. ObjData.alloc(tai_real_80bit(hp).savesize);
  1029. ait_real_64bit :
  1030. ObjData.alloc(8);
  1031. ait_real_32bit :
  1032. ObjData.alloc(4);
  1033. ait_comp_64bit :
  1034. ObjData.alloc(8);
  1035. ait_const:
  1036. begin
  1037. { if symbols are provided we can calculate the value for relative symbols.
  1038. This is required for length calculation of leb128 constants }
  1039. if assigned(tai_const(hp).sym) then
  1040. begin
  1041. objsym:=Objdata.SymbolRef(tai_const(hp).sym);
  1042. { objsym already defined and there is endsym? }
  1043. if assigned(objsym.objsection) and assigned(tai_const(hp).endsym) then
  1044. begin
  1045. objsymend:=Objdata.SymbolRef(tai_const(hp).endsym);
  1046. { objsymend already defined? }
  1047. if assigned(objsymend.objsection) then
  1048. begin
  1049. if objsymend.objsection<>objsym.objsection then
  1050. begin
  1051. { leb128 relative constants are not relocatable, but other types are,
  1052. given that objsym belongs to the current section. }
  1053. if (Tai_const(hp).consttype in [aitconst_uleb128bit,aitconst_sleb128bit]) or
  1054. (objsym.objsection<>ObjData.CurrObjSec) then
  1055. InternalError(200404124);
  1056. end
  1057. else
  1058. Tai_const(hp).value:=objsymend.address-objsym.address+Tai_const(hp).symofs;
  1059. end;
  1060. end;
  1061. end;
  1062. ObjData.alloc(tai_const(hp).size);
  1063. end;
  1064. ait_directive:
  1065. begin
  1066. case tai_directive(hp).directive of
  1067. asd_indirect_symbol:
  1068. { handled in TreePass1 }
  1069. ;
  1070. asd_lazy_reference:
  1071. begin
  1072. if tai_directive(hp).name='' then
  1073. Internalerror(2009112101);
  1074. objsym:=ObjData.symbolref(tai_directive(hp).name);
  1075. objsym.bind:=AB_LAZY;
  1076. end;
  1077. asd_reference:
  1078. { ignore for now, but should be added}
  1079. ;
  1080. else
  1081. internalerror(2010011101);
  1082. end;
  1083. end;
  1084. ait_section:
  1085. begin
  1086. ObjData.CreateSection(Tai_section(hp).sectype,Tai_section(hp).name^,Tai_section(hp).secorder);
  1087. Tai_section(hp).sec:=ObjData.CurrObjSec;
  1088. end;
  1089. ait_symbol :
  1090. begin
  1091. { needs extra support in the internal assembler }
  1092. { the value is just ignored }
  1093. {if tai_symbol(hp).has_value then
  1094. internalerror(2009090804); ;}
  1095. ObjData.SymbolDefine(Tai_symbol(hp).sym);
  1096. end;
  1097. ait_label :
  1098. ObjData.SymbolDefine(Tai_label(hp).labsym);
  1099. ait_string :
  1100. ObjData.alloc(Tai_string(hp).len);
  1101. ait_instruction :
  1102. begin
  1103. { reset instructions which could change in pass 2 }
  1104. Taicpu(hp).resetpass2;
  1105. ObjData.alloc(Taicpu(hp).Pass1(ObjData));
  1106. end;
  1107. ait_cutobject :
  1108. if SmartAsm then
  1109. break;
  1110. end;
  1111. hp:=Tai(hp.next);
  1112. end;
  1113. TreePass0:=hp;
  1114. end;
  1115. function TInternalAssembler.TreePass1(hp:Tai):Tai;
  1116. var
  1117. objsym,
  1118. objsymend : TObjSymbol;
  1119. begin
  1120. while assigned(hp) do
  1121. begin
  1122. case hp.typ of
  1123. ait_align :
  1124. begin
  1125. if tai_align_abstract(hp).aligntype>1 then
  1126. begin
  1127. { here we must determine the fillsize which is used in pass2 }
  1128. Tai_align_abstract(hp).fillsize:=align(ObjData.CurrObjSec.Size,Tai_align_abstract(hp).aligntype)-
  1129. ObjData.CurrObjSec.Size;
  1130. ObjData.alloc(Tai_align_abstract(hp).fillsize);
  1131. end;
  1132. end;
  1133. ait_datablock :
  1134. begin
  1135. if (oso_data in ObjData.CurrObjSec.secoptions) then
  1136. Message(asmw_e_alloc_data_only_in_bss);
  1137. {$ifdef USE_COMM_IN_BSS}
  1138. if writingpackages and
  1139. Tai_datablock(hp).is_global then
  1140. begin
  1141. objsym:=ObjData.SymbolDefine(Tai_datablock(hp).sym);
  1142. objsym.size:=Tai_datablock(hp).size;
  1143. objsym.bind:=AB_COMMON;
  1144. objsym.alignment:=needtowritealignmentalsoforELF;
  1145. end
  1146. else
  1147. {$endif USE_COMM_IN_BSS}
  1148. begin
  1149. ObjData.allocalign(used_align(size_2_align(Tai_datablock(hp).size),0,ObjData.CurrObjSec.secalign));
  1150. objsym:=ObjData.SymbolDefine(Tai_datablock(hp).sym);
  1151. objsym.size:=Tai_datablock(hp).size;
  1152. ObjData.alloc(Tai_datablock(hp).size);
  1153. end;
  1154. end;
  1155. ait_real_80bit :
  1156. ObjData.alloc(tai_real_80bit(hp).savesize);
  1157. ait_real_64bit :
  1158. ObjData.alloc(8);
  1159. ait_real_32bit :
  1160. ObjData.alloc(4);
  1161. ait_comp_64bit :
  1162. ObjData.alloc(8);
  1163. ait_const:
  1164. begin
  1165. { Recalculate relative symbols }
  1166. if assigned(tai_const(hp).sym) and
  1167. assigned(tai_const(hp).endsym) then
  1168. begin
  1169. objsym:=Objdata.SymbolRef(tai_const(hp).sym);
  1170. objsymend:=Objdata.SymbolRef(tai_const(hp).endsym);
  1171. if objsymend.objsection<>objsym.objsection then
  1172. begin
  1173. if (Tai_const(hp).consttype in [aitconst_uleb128bit,aitconst_sleb128bit]) or
  1174. (objsym.objsection<>ObjData.CurrObjSec) then
  1175. internalerror(200905042);
  1176. end
  1177. else
  1178. Tai_const(hp).value:=objsymend.address-objsym.address+Tai_const(hp).symofs;
  1179. end;
  1180. ObjData.alloc(tai_const(hp).size);
  1181. end;
  1182. ait_section:
  1183. begin
  1184. { use cached value }
  1185. ObjData.setsection(Tai_section(hp).sec);
  1186. end;
  1187. ait_stab :
  1188. begin
  1189. if assigned(Tai_stab(hp).str) then
  1190. WriteStab(Tai_stab(hp).str);
  1191. end;
  1192. ait_symbol :
  1193. ObjData.SymbolDefine(Tai_symbol(hp).sym);
  1194. ait_symbol_end :
  1195. begin
  1196. objsym:=ObjData.SymbolRef(Tai_symbol_end(hp).sym);
  1197. objsym.size:=ObjData.CurrObjSec.Size-objsym.offset;
  1198. end;
  1199. ait_label :
  1200. ObjData.SymbolDefine(Tai_label(hp).labsym);
  1201. ait_string :
  1202. ObjData.alloc(Tai_string(hp).len);
  1203. ait_instruction :
  1204. ObjData.alloc(Taicpu(hp).Pass1(ObjData));
  1205. ait_cutobject :
  1206. if SmartAsm then
  1207. break;
  1208. ait_directive :
  1209. begin
  1210. case tai_directive(hp).directive of
  1211. asd_indirect_symbol:
  1212. if tai_directive(hp).name='' then
  1213. Internalerror(2009101103)
  1214. else if not SetIndirectToSymbol(Tai(hp.Previous), tai_directive(hp).name) then
  1215. Internalerror(2009101102);
  1216. asd_lazy_reference:
  1217. { handled in TreePass0 }
  1218. ;
  1219. asd_reference:
  1220. { ignore for now, but should be added}
  1221. ;
  1222. else
  1223. internalerror(2010011102);
  1224. end;
  1225. end;
  1226. end;
  1227. hp:=Tai(hp.next);
  1228. end;
  1229. TreePass1:=hp;
  1230. end;
  1231. function TInternalAssembler.TreePass2(hp:Tai):Tai;
  1232. var
  1233. fillbuffer : tfillbuffer;
  1234. {$ifdef x86}
  1235. co : comp;
  1236. {$endif x86}
  1237. leblen : byte;
  1238. lebbuf : array[0..63] of byte;
  1239. objsym,
  1240. objsymend : TObjSymbol;
  1241. zerobuf : array[0..63] of byte;
  1242. relative_reloc: boolean;
  1243. begin
  1244. fillchar(zerobuf,sizeof(zerobuf),0);
  1245. { main loop }
  1246. while assigned(hp) do
  1247. begin
  1248. case hp.typ of
  1249. ait_align :
  1250. begin
  1251. if tai_align_abstract(hp).aligntype>ObjData.CurrObjSec.secalign then
  1252. InternalError(2012072301);
  1253. if oso_data in ObjData.CurrObjSec.secoptions then
  1254. ObjData.writebytes(Tai_align_abstract(hp).calculatefillbuf(fillbuffer,oso_executable in ObjData.CurrObjSec.secoptions)^,
  1255. Tai_align_abstract(hp).fillsize)
  1256. else
  1257. ObjData.alloc(Tai_align_abstract(hp).fillsize);
  1258. end;
  1259. ait_section :
  1260. begin
  1261. { use cached value }
  1262. ObjData.setsection(Tai_section(hp).sec);
  1263. end;
  1264. ait_symbol :
  1265. begin
  1266. ObjOutput.exportsymbol(ObjData.SymbolRef(Tai_symbol(hp).sym));
  1267. end;
  1268. ait_symbol_end :
  1269. begin
  1270. { recalculate size, as some preceding instructions
  1271. could have been changed to smaller size }
  1272. objsym:=ObjData.SymbolRef(Tai_symbol_end(hp).sym);
  1273. objsym.size:=ObjData.CurrObjSec.Size-objsym.offset;
  1274. end;
  1275. ait_datablock :
  1276. begin
  1277. ObjOutput.exportsymbol(ObjData.SymbolRef(Tai_datablock(hp).sym));
  1278. {$ifdef USE_COMM_IN_BSS}
  1279. if not(writingpackages and
  1280. Tai_datablock(hp).is_global) then
  1281. {$endif USE_COMM_IN_BSS}
  1282. begin
  1283. ObjData.allocalign(used_align(size_2_align(Tai_datablock(hp).size),0,ObjData.CurrObjSec.secalign));
  1284. ObjData.alloc(Tai_datablock(hp).size);
  1285. end;
  1286. end;
  1287. ait_real_80bit :
  1288. begin
  1289. ObjData.writebytes(Tai_real_80bit(hp).value,10);
  1290. ObjData.writebytes(zerobuf,Tai_real_80bit(hp).savesize-10);
  1291. end;
  1292. ait_real_64bit :
  1293. ObjData.writebytes(Tai_real_64bit(hp).value,8);
  1294. ait_real_32bit :
  1295. ObjData.writebytes(Tai_real_32bit(hp).value,4);
  1296. ait_comp_64bit :
  1297. begin
  1298. {$ifdef x86}
  1299. co:=comp(Tai_comp_64bit(hp).value);
  1300. ObjData.writebytes(co,8);
  1301. {$endif x86}
  1302. end;
  1303. ait_string :
  1304. ObjData.writebytes(Tai_string(hp).str^,Tai_string(hp).len);
  1305. ait_const :
  1306. begin
  1307. { Recalculate relative symbols, addresses of forward references
  1308. can be changed in treepass1 }
  1309. relative_reloc:=false;
  1310. if assigned(tai_const(hp).sym) and
  1311. assigned(tai_const(hp).endsym) then
  1312. begin
  1313. objsym:=Objdata.SymbolRef(tai_const(hp).sym);
  1314. objsymend:=Objdata.SymbolRef(tai_const(hp).endsym);
  1315. relative_reloc:=(objsym.objsection<>objsymend.objsection);
  1316. Tai_const(hp).value:=objsymend.address-objsym.address+Tai_const(hp).symofs;
  1317. end;
  1318. case tai_const(hp).consttype of
  1319. aitconst_64bit,
  1320. aitconst_32bit,
  1321. aitconst_16bit,
  1322. aitconst_64bit_unaligned,
  1323. aitconst_32bit_unaligned,
  1324. aitconst_16bit_unaligned,
  1325. aitconst_8bit :
  1326. begin
  1327. if assigned(tai_const(hp).sym) and
  1328. not assigned(tai_const(hp).endsym) then
  1329. ObjData.writereloc(Tai_const(hp).symofs,tai_const(hp).size,Objdata.SymbolRef(tai_const(hp).sym),RELOC_ABSOLUTE)
  1330. else if relative_reloc then
  1331. ObjData.writereloc(ObjData.CurrObjSec.size+tai_const(hp).size-objsym.address+tai_const(hp).symofs,tai_const(hp).size,objsymend,RELOC_RELATIVE)
  1332. else
  1333. ObjData.writebytes(Tai_const(hp).value,tai_const(hp).size);
  1334. end;
  1335. aitconst_rva_symbol :
  1336. begin
  1337. { PE32+? }
  1338. if target_info.system=system_x86_64_win64 then
  1339. ObjData.writereloc(Tai_const(hp).symofs,sizeof(longint),Objdata.SymbolRef(tai_const(hp).sym),RELOC_RVA)
  1340. else
  1341. ObjData.writereloc(Tai_const(hp).symofs,sizeof(pint),Objdata.SymbolRef(tai_const(hp).sym),RELOC_RVA);
  1342. end;
  1343. aitconst_secrel32_symbol :
  1344. begin
  1345. { Required for DWARF2 support under Windows }
  1346. ObjData.writereloc(Tai_const(hp).symofs,sizeof(longint),Objdata.SymbolRef(tai_const(hp).sym),RELOC_SECREL32);
  1347. end;
  1348. aitconst_uleb128bit,
  1349. aitconst_sleb128bit :
  1350. begin
  1351. if tai_const(hp).consttype=aitconst_uleb128bit then
  1352. leblen:=EncodeUleb128(qword(Tai_const(hp).value),lebbuf)
  1353. else
  1354. leblen:=EncodeSleb128(Tai_const(hp).value,lebbuf);
  1355. if leblen<>tai_const(hp).size then
  1356. internalerror(200709271);
  1357. ObjData.writebytes(lebbuf,leblen);
  1358. end;
  1359. aitconst_darwin_dwarf_delta32,
  1360. aitconst_darwin_dwarf_delta64:
  1361. ObjData.writebytes(Tai_const(hp).value,tai_const(hp).size);
  1362. else
  1363. internalerror(200603254);
  1364. end;
  1365. end;
  1366. ait_label :
  1367. begin
  1368. { exporting shouldn't be necessary as labels are local,
  1369. but it's better to be on the safe side (PFV) }
  1370. ObjOutput.exportsymbol(ObjData.SymbolRef(Tai_label(hp).labsym));
  1371. end;
  1372. ait_instruction :
  1373. Taicpu(hp).Pass2(ObjData);
  1374. ait_stab :
  1375. WriteStab(Tai_stab(hp).str);
  1376. ait_function_name,
  1377. ait_force_line : ;
  1378. ait_cutobject :
  1379. if SmartAsm then
  1380. break;
  1381. {$ifndef DISABLE_WIN64_SEH}
  1382. ait_seh_directive :
  1383. tai_seh_directive(hp).generate_code(objdata);
  1384. {$endif DISABLE_WIN64_SEH}
  1385. end;
  1386. hp:=Tai(hp.next);
  1387. end;
  1388. TreePass2:=hp;
  1389. end;
  1390. procedure TInternalAssembler.writetree;
  1391. label
  1392. doexit;
  1393. var
  1394. hp : Tai;
  1395. ObjWriter : TObjectWriter;
  1396. begin
  1397. ObjWriter:=TObjectwriter.create;
  1398. ObjOutput:=CObjOutput.Create(ObjWriter);
  1399. ObjData:=ObjOutput.newObjData(ObjFileName);
  1400. { Pass 0 }
  1401. ObjData.currpass:=0;
  1402. ObjData.createsection(sec_code);
  1403. ObjData.beforealloc;
  1404. { start with list 1 }
  1405. currlistidx:=1;
  1406. currlist:=list[currlistidx];
  1407. hp:=Tai(currList.first);
  1408. while assigned(hp) do
  1409. begin
  1410. hp:=TreePass0(hp);
  1411. MaybeNextList(hp);
  1412. end;
  1413. ObjData.afteralloc;
  1414. { leave if errors have occured }
  1415. if errorcount>0 then
  1416. goto doexit;
  1417. { Pass 1 }
  1418. ObjData.currpass:=1;
  1419. ObjData.resetsections;
  1420. ObjData.beforealloc;
  1421. ObjData.createsection(sec_code);
  1422. { start with list 1 }
  1423. currlistidx:=1;
  1424. currlist:=list[currlistidx];
  1425. hp:=Tai(currList.first);
  1426. while assigned(hp) do
  1427. begin
  1428. hp:=TreePass1(hp);
  1429. MaybeNextList(hp);
  1430. end;
  1431. ObjData.createsection(sec_code);
  1432. ObjData.afteralloc;
  1433. { leave if errors have occured }
  1434. if errorcount>0 then
  1435. goto doexit;
  1436. { Pass 2 }
  1437. ObjData.currpass:=2;
  1438. ObjData.resetsections;
  1439. ObjData.beforewrite;
  1440. ObjData.createsection(sec_code);
  1441. { start with list 1 }
  1442. currlistidx:=1;
  1443. currlist:=list[currlistidx];
  1444. hp:=Tai(currList.first);
  1445. while assigned(hp) do
  1446. begin
  1447. hp:=TreePass2(hp);
  1448. MaybeNextList(hp);
  1449. end;
  1450. ObjData.createsection(sec_code);
  1451. ObjData.afterwrite;
  1452. { don't write the .o file if errors have occured }
  1453. if errorcount=0 then
  1454. begin
  1455. { write objectfile }
  1456. ObjOutput.startobjectfile(ObjFileName);
  1457. ObjOutput.writeobjectfile(ObjData);
  1458. end;
  1459. doexit:
  1460. { Cleanup }
  1461. ObjData.free;
  1462. ObjData:=nil;
  1463. ObjWriter.free;
  1464. end;
  1465. procedure TInternalAssembler.writetreesmart;
  1466. var
  1467. hp : Tai;
  1468. startsectype : TAsmSectiontype;
  1469. place: tcutplace;
  1470. ObjWriter : TObjectWriter;
  1471. begin
  1472. if not(cs_asm_leave in current_settings.globalswitches) then
  1473. ObjWriter:=TARObjectWriter.create(current_module.staticlibfilename)
  1474. else
  1475. ObjWriter:=TObjectwriter.create;
  1476. NextSmartName(cut_normal);
  1477. ObjOutput:=CObjOutput.Create(ObjWriter);
  1478. startsectype:=sec_code;
  1479. { start with list 1 }
  1480. currlistidx:=1;
  1481. currlist:=list[currlistidx];
  1482. hp:=Tai(currList.first);
  1483. while assigned(hp) do
  1484. begin
  1485. ObjData:=ObjOutput.newObjData(ObjFileName);
  1486. { Pass 0 }
  1487. ObjData.currpass:=0;
  1488. ObjData.resetsections;
  1489. ObjData.beforealloc;
  1490. ObjData.createsection(startsectype);
  1491. TreePass0(hp);
  1492. ObjData.afteralloc;
  1493. { leave if errors have occured }
  1494. if errorcount>0 then
  1495. break;
  1496. { Pass 1 }
  1497. ObjData.currpass:=1;
  1498. ObjData.resetsections;
  1499. ObjData.beforealloc;
  1500. ObjData.createsection(startsectype);
  1501. TreePass1(hp);
  1502. ObjData.afteralloc;
  1503. { leave if errors have occured }
  1504. if errorcount>0 then
  1505. break;
  1506. { Pass 2 }
  1507. ObjData.currpass:=2;
  1508. ObjOutput.startobjectfile(ObjFileName);
  1509. ObjData.resetsections;
  1510. ObjData.beforewrite;
  1511. ObjData.createsection(startsectype);
  1512. hp:=TreePass2(hp);
  1513. ObjData.afterwrite;
  1514. { leave if errors have occured }
  1515. if errorcount>0 then
  1516. break;
  1517. { write the current objectfile }
  1518. ObjOutput.writeobjectfile(ObjData);
  1519. ObjData.free;
  1520. ObjData:=nil;
  1521. { end of lists? }
  1522. if not MaybeNextList(hp) then
  1523. break;
  1524. { we will start a new objectfile so reset everything }
  1525. { The place can still change in the next while loop, so don't init }
  1526. { the writer yet (JM) }
  1527. if (hp.typ=ait_cutobject) then
  1528. place := Tai_cutobject(hp).place
  1529. else
  1530. place := cut_normal;
  1531. { avoid empty files }
  1532. startsectype:=sec_code;
  1533. while assigned(hp) and
  1534. (Tai(hp).typ in [ait_marker,ait_comment,ait_section,ait_cutobject]) do
  1535. begin
  1536. if Tai(hp).typ=ait_section then
  1537. startsectype:=Tai_section(hp).sectype;
  1538. if (Tai(hp).typ=ait_cutobject) then
  1539. place:=Tai_cutobject(hp).place;
  1540. hp:=Tai(hp.next);
  1541. end;
  1542. if not MaybeNextList(hp) then
  1543. break;
  1544. { start next objectfile }
  1545. NextSmartName(place);
  1546. end;
  1547. ObjData.free;
  1548. ObjData:=nil;
  1549. ObjWriter.free;
  1550. end;
  1551. procedure TInternalAssembler.MakeObject;
  1552. var to_do:set of TasmlistType;
  1553. i:TasmlistType;
  1554. procedure addlist(p:TAsmList);
  1555. begin
  1556. inc(lists);
  1557. list[lists]:=p;
  1558. end;
  1559. begin
  1560. to_do:=[low(Tasmlisttype)..high(Tasmlisttype)];
  1561. if usedeffileforexports then
  1562. exclude(to_do,al_exports);
  1563. if not(tf_section_threadvars in target_info.flags) then
  1564. exclude(to_do,al_threadvars);
  1565. for i:=low(TasmlistType) to high(TasmlistType) do
  1566. if (i in to_do) and (current_asmdata.asmlists[i]<>nil) then
  1567. addlist(current_asmdata.asmlists[i]);
  1568. if SmartAsm then
  1569. writetreesmart
  1570. else
  1571. writetree;
  1572. end;
  1573. {*****************************************************************************
  1574. Generate Assembler Files Main Procedure
  1575. *****************************************************************************}
  1576. Procedure GenerateAsm(smart:boolean);
  1577. var
  1578. a : TAssembler;
  1579. begin
  1580. if not assigned(CAssembler[target_asm.id]) then
  1581. Message(asmw_f_assembler_output_not_supported);
  1582. a:=CAssembler[target_asm.id].Create(smart);
  1583. a.MakeObject;
  1584. a.Free;
  1585. end;
  1586. Procedure OnlyAsm;
  1587. var
  1588. a : TExternalAssembler;
  1589. begin
  1590. a:=TExternalAssembler.Create(false);
  1591. a.DoAssemble;
  1592. a.Free;
  1593. end;
  1594. {*****************************************************************************
  1595. Init/Done
  1596. *****************************************************************************}
  1597. procedure RegisterAssembler(const r:tasminfo;c:TAssemblerClass);
  1598. var
  1599. t : tasm;
  1600. begin
  1601. t:=r.id;
  1602. if assigned(asminfos[t]) then
  1603. writeln('Warning: Assembler is already registered!')
  1604. else
  1605. Getmem(asminfos[t],sizeof(tasminfo));
  1606. asminfos[t]^:=r;
  1607. CAssembler[t]:=c;
  1608. end;
  1609. end.