assemble.pas 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652
  1. {
  2. $Id$
  3. Copyright (c) 1998-2004 by Peter Vreman
  4. This unit handles the assemblerfile write and assembler calls of FPC
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. {# @abstract(This unit handles the assembler file write and assembler calls of FPC)
  19. Handles the calls to the actual external assemblers, as well as the generation
  20. of object files for smart linking. Also contains the base class for writing
  21. the assembler statements to file.
  22. }
  23. unit assemble;
  24. {$i fpcdefs.inc}
  25. interface
  26. uses
  27. {$ifdef Delphi}
  28. sysutils,
  29. dmisc,
  30. {$else Delphi}
  31. strings,
  32. dos,
  33. {$endif Delphi}
  34. systems,globtype,globals,aasmbase,aasmtai,ogbase;
  35. const
  36. { maximum of aasmoutput lists there will be }
  37. maxoutputlists = 10;
  38. { buffer size for writing the .s file }
  39. AsmOutSize=32768;
  40. type
  41. TAssembler=class(TAbstractAssembler)
  42. public
  43. {filenames}
  44. path : pathstr;
  45. name : namestr;
  46. asmfile, { current .s and .o file }
  47. objfile : string;
  48. ppufilename : string;
  49. asmprefix : string;
  50. SmartAsm : boolean;
  51. SmartFilesCount,
  52. SmartHeaderCount : longint;
  53. Constructor Create(smart:boolean);virtual;
  54. Destructor Destroy;override;
  55. procedure NextSmartName(place:tcutplace);
  56. procedure MakeObject;virtual;abstract;
  57. end;
  58. {# This is the base class which should be overriden for each each
  59. assembler writer. It is used to actually assembler a file,
  60. and write the output to the assembler file.
  61. }
  62. TExternalAssembler=class(TAssembler)
  63. private
  64. procedure CreateSmartLinkPath(const s:string);
  65. protected
  66. {outfile}
  67. AsmSize,
  68. AsmStartSize,
  69. outcnt : longint;
  70. outbuf : array[0..AsmOutSize-1] of char;
  71. outfile : file;
  72. public
  73. {# Returns the complete path and executable name of the assembler
  74. program.
  75. It first tries looking in the UTIL directory if specified,
  76. otherwise it searches in the free pascal binary directory, in
  77. the current working directory and then in the directories
  78. in the $PATH environment.}
  79. Function FindAssembler:string;
  80. {# Actually does the call to the assembler file. Returns false
  81. if the assembling of the file failed.}
  82. Function CallAssembler(const command,para:string):Boolean;
  83. Function DoAssemble:boolean;virtual;
  84. Procedure RemoveAsm;
  85. Procedure AsmFlush;
  86. Procedure AsmClear;
  87. {# Write a string to the assembler file }
  88. Procedure AsmWrite(const s:string);
  89. {# Write a string to the assembler file }
  90. Procedure AsmWritePChar(p:pchar);
  91. {# Write a string to the assembler file followed by a new line }
  92. Procedure AsmWriteLn(const s:string);
  93. {# Write a new line to the assembler file }
  94. Procedure AsmLn;
  95. procedure AsmCreate(Aplace:tcutplace);
  96. procedure AsmClose;
  97. {# This routine should be overriden for each assembler, it is used
  98. to actually write the abstract assembler stream to file.}
  99. procedure WriteTree(p:TAAsmoutput);virtual;
  100. {# This routine should be overriden for each assembler, it is used
  101. to actually write all the different abstract assembler streams
  102. by calling for each stream type, the @var(WriteTree) method.}
  103. procedure WriteAsmList;virtual;
  104. public
  105. Constructor Create(smart:boolean);override;
  106. procedure MakeObject;override;
  107. end;
  108. TInternalAssembler=class(TAssembler)
  109. public
  110. constructor create(smart:boolean);override;
  111. destructor destroy;override;
  112. procedure MakeObject;override;
  113. protected
  114. objectdata : TAsmObjectData;
  115. objectoutput : tobjectoutput;
  116. private
  117. { the aasmoutput lists that need to be processed }
  118. lists : byte;
  119. list : array[1..maxoutputlists] of TAAsmoutput;
  120. { current processing }
  121. currlistidx : byte;
  122. currlist : TAAsmoutput;
  123. currpass : byte;
  124. {$ifdef GDB}
  125. n_line : byte; { different types of source lines }
  126. linecount,
  127. includecount : longint;
  128. funcname : tasmsymbol;
  129. stabslastfileinfo : tfileposinfo;
  130. procedure convertstabs(p:pchar);
  131. procedure emitlineinfostabs(nidx,line : longint);
  132. procedure emitstabs(s:string);
  133. procedure WriteFileLineInfo(var fileinfo : tfileposinfo);
  134. procedure StartFileLineInfo;
  135. procedure EndFileLineInfo;
  136. {$endif}
  137. function MaybeNextList(var hp:Tai):boolean;
  138. function TreePass0(hp:Tai):Tai;
  139. function TreePass1(hp:Tai):Tai;
  140. function TreePass2(hp:Tai):Tai;
  141. procedure writetree;
  142. procedure writetreesmart;
  143. end;
  144. TAssemblerClass = class of TAssembler;
  145. Procedure GenerateAsm(smart:boolean);
  146. Procedure OnlyAsm;
  147. procedure RegisterAssembler(const r:tasminfo;c:TAssemblerClass);
  148. procedure InitAssembler;
  149. procedure DoneAssembler;
  150. Implementation
  151. uses
  152. {$ifdef hasunix}
  153. {$ifdef havelinuxrtl10}
  154. linux,
  155. {$else}
  156. unix,
  157. {$endif}
  158. {$endif}
  159. cutils,script,fmodule,verbose,
  160. cpuinfo,
  161. {$ifdef memdebug}
  162. cclasses,
  163. {$endif memdebug}
  164. {$ifdef GDB}
  165. finput,
  166. gdb,
  167. {$endif GDB}
  168. {$ifdef m68k}
  169. cpuinfo,
  170. {$endif m68k}
  171. cpubase,aasmcpu
  172. ;
  173. var
  174. CAssembler : array[tasm] of TAssemblerClass;
  175. {*****************************************************************************
  176. TAssembler
  177. *****************************************************************************}
  178. Constructor TAssembler.Create(smart:boolean);
  179. begin
  180. { load start values }
  181. asmfile:=current_module.get_asmfilename;
  182. objfile:=current_module.objfilename^;
  183. name:=Lower(current_module.modulename^);
  184. path:=current_module.outputpath^;
  185. asmprefix := current_module.asmprefix^;
  186. if not assigned(current_module.outputpath) then
  187. ppufilename := ''
  188. else
  189. ppufilename := current_module.ppufilename^;
  190. SmartAsm:=smart;
  191. SmartFilesCount:=0;
  192. SmartHeaderCount:=0;
  193. SmartLinkOFiles.Clear;
  194. end;
  195. Destructor TAssembler.Destroy;
  196. begin
  197. end;
  198. procedure TAssembler.NextSmartName(place:tcutplace);
  199. var
  200. s : string;
  201. begin
  202. inc(SmartFilesCount);
  203. if SmartFilesCount>999999 then
  204. Message(asmw_f_too_many_asm_files);
  205. case place of
  206. cut_begin :
  207. begin
  208. inc(SmartHeaderCount);
  209. s:=asmprefix+tostr(SmartHeaderCount)+'h';
  210. end;
  211. cut_normal :
  212. s:=asmprefix+tostr(SmartHeaderCount)+'s';
  213. cut_end :
  214. s:=asmprefix+tostr(SmartHeaderCount)+'t';
  215. end;
  216. AsmFile:=Path+FixFileName(s+tostr(SmartFilesCount)+target_info.asmext);
  217. ObjFile:=Path+FixFileName(s+tostr(SmartFilesCount)+target_info.objext);
  218. { insert in container so it can be cleared after the linking }
  219. SmartLinkOFiles.Insert(Objfile);
  220. end;
  221. {*****************************************************************************
  222. TExternalAssembler
  223. *****************************************************************************}
  224. Function DoPipe:boolean;
  225. begin
  226. DoPipe:=(cs_asm_pipe in aktglobalswitches) and
  227. not(cs_asm_leave in aktglobalswitches)
  228. and ((aktoutputformat in [as_gas,as_darwin]));
  229. end;
  230. Constructor TExternalAssembler.Create(smart:boolean);
  231. begin
  232. inherited Create(smart);
  233. if SmartAsm then
  234. begin
  235. path:=FixPath(path+FixFileName(name)+target_info.smartext,false);
  236. CreateSmartLinkPath(path);
  237. end;
  238. Outcnt:=0;
  239. end;
  240. procedure TExternalAssembler.CreateSmartLinkPath(const s:string);
  241. var
  242. dir : searchrec;
  243. hs : string;
  244. begin
  245. if PathExists(s) then
  246. begin
  247. { the path exists, now we clean only all the .o and .s files }
  248. { .o files }
  249. findfirst(s+source_info.dirsep+'*'+target_info.objext,anyfile,dir);
  250. while (doserror=0) do
  251. begin
  252. RemoveFile(s+source_info.dirsep+dir.name);
  253. findnext(dir);
  254. end;
  255. findclose(dir);
  256. { .s files }
  257. findfirst(s+source_info.dirsep+'*'+target_info.asmext,anyfile,dir);
  258. while (doserror=0) do
  259. begin
  260. RemoveFile(s+source_info.dirsep+dir.name);
  261. findnext(dir);
  262. end;
  263. findclose(dir);
  264. end
  265. else
  266. begin
  267. hs:=s;
  268. if hs[length(hs)] in ['/','\'] then
  269. delete(hs,length(hs),1);
  270. {$I-}
  271. mkdir(hs);
  272. {$I+}
  273. if ioresult<>0 then;
  274. end;
  275. end;
  276. const
  277. lastas : byte=255;
  278. var
  279. LastASBin : pathstr;
  280. Function TExternalAssembler.FindAssembler:string;
  281. var
  282. asfound : boolean;
  283. UtilExe : string;
  284. begin
  285. asfound:=false;
  286. if cs_link_on_target in aktglobalswitches then
  287. begin
  288. { If linking on target, don't add any path PM }
  289. FindAssembler:=utilsprefix+AddExtension(target_asm.asmbin,target_info.exeext);
  290. exit;
  291. end
  292. else
  293. UtilExe:=utilsprefix+AddExtension(target_asm.asmbin,source_info.exeext);
  294. if lastas<>ord(target_asm.id) then
  295. begin
  296. lastas:=ord(target_asm.id);
  297. { is an assembler passed ? }
  298. if utilsdirectory<>'' then
  299. asfound:=FindFile(UtilExe,utilsdirectory,LastASBin);
  300. if not AsFound then
  301. asfound:=FindExe(UtilExe,LastASBin);
  302. if (not asfound) and not(cs_asm_extern in aktglobalswitches) then
  303. begin
  304. Message1(exec_e_assembler_not_found,LastASBin);
  305. aktglobalswitches:=aktglobalswitches+[cs_asm_extern];
  306. end;
  307. if asfound then
  308. Message1(exec_t_using_assembler,LastASBin);
  309. end;
  310. FindAssembler:=LastASBin;
  311. end;
  312. Function TExternalAssembler.CallAssembler(const command,para:string):Boolean;
  313. begin
  314. callassembler:=true;
  315. if not(cs_asm_extern in aktglobalswitches) then
  316. begin
  317. swapvectors;
  318. exec(command,para);
  319. swapvectors;
  320. if (doserror<>0) then
  321. begin
  322. Message1(exec_e_cant_call_assembler,tostr(doserror));
  323. aktglobalswitches:=aktglobalswitches+[cs_asm_extern];
  324. callassembler:=false;
  325. end
  326. else
  327. if (dosexitcode<>0) then
  328. begin
  329. Message1(exec_e_error_while_assembling,tostr(dosexitcode));
  330. callassembler:=false;
  331. end;
  332. end
  333. else
  334. AsmRes.AddAsmCommand(command,para,name);
  335. end;
  336. procedure TExternalAssembler.RemoveAsm;
  337. var
  338. g : file;
  339. begin
  340. if cs_asm_leave in aktglobalswitches then
  341. exit;
  342. if cs_asm_extern in aktglobalswitches then
  343. AsmRes.AddDeleteCommand(AsmFile)
  344. else
  345. begin
  346. assign(g,AsmFile);
  347. {$I-}
  348. erase(g);
  349. {$I+}
  350. if ioresult<>0 then;
  351. end;
  352. end;
  353. Function TExternalAssembler.DoAssemble:boolean;
  354. var
  355. s : string;
  356. begin
  357. DoAssemble:=true;
  358. if DoPipe then
  359. exit;
  360. if not(cs_asm_extern in aktglobalswitches) then
  361. begin
  362. if SmartAsm then
  363. begin
  364. if (SmartFilesCount<=1) then
  365. Message1(exec_i_assembling_smart,name);
  366. end
  367. else
  368. Message1(exec_i_assembling,name);
  369. end;
  370. s:=target_asm.asmcmd;
  371. {$ifdef m68k}
  372. if aktoptprocessor = MC68020 then
  373. s:='-m68020 '+s
  374. else
  375. s:='-m68000 '+s;
  376. {$endif}
  377. if (cs_link_on_target in aktglobalswitches) then
  378. begin
  379. Replace(s,'$ASM',ScriptFixFileName(AsmFile));
  380. Replace(s,'$OBJ',ScriptFixFileName(ObjFile));
  381. end
  382. else
  383. begin
  384. Replace(s,'$ASM',AsmFile);
  385. Replace(s,'$OBJ',ObjFile);
  386. end;
  387. if CallAssembler(FindAssembler,s) then
  388. RemoveAsm
  389. else
  390. begin
  391. DoAssemble:=false;
  392. GenerateError;
  393. end;
  394. end;
  395. Procedure TExternalAssembler.AsmFlush;
  396. begin
  397. if outcnt>0 then
  398. begin
  399. BlockWrite(outfile,outbuf,outcnt);
  400. outcnt:=0;
  401. end;
  402. end;
  403. Procedure TExternalAssembler.AsmClear;
  404. begin
  405. outcnt:=0;
  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.AsmWriteLn(const s:string);
  416. begin
  417. AsmWrite(s);
  418. AsmLn;
  419. end;
  420. Procedure TExternalAssembler.AsmWritePChar(p:pchar);
  421. var
  422. i,j : longint;
  423. begin
  424. i:=StrLen(p);
  425. j:=i;
  426. while j>0 do
  427. begin
  428. i:=min(j,AsmOutSize);
  429. if OutCnt+i>=AsmOutSize then
  430. AsmFlush;
  431. Move(p[0],OutBuf[OutCnt],i);
  432. inc(OutCnt,i);
  433. inc(AsmSize,i);
  434. dec(j,i);
  435. p:=pchar(@p[i]);
  436. end;
  437. end;
  438. Procedure TExternalAssembler.AsmLn;
  439. begin
  440. if OutCnt>=AsmOutSize-2 then
  441. AsmFlush;
  442. if (cs_link_on_target in aktglobalswitches) then
  443. begin
  444. OutBuf[OutCnt]:=target_info.newline[1];
  445. inc(OutCnt);
  446. inc(AsmSize);
  447. if length(target_info.newline)>1 then
  448. begin
  449. OutBuf[OutCnt]:=target_info.newline[2];
  450. inc(OutCnt);
  451. inc(AsmSize);
  452. end;
  453. end
  454. else
  455. begin
  456. OutBuf[OutCnt]:=source_info.newline[1];
  457. inc(OutCnt);
  458. inc(AsmSize);
  459. if length(source_info.newline)>1 then
  460. begin
  461. OutBuf[OutCnt]:=source_info.newline[2];
  462. inc(OutCnt);
  463. inc(AsmSize);
  464. end;
  465. end;
  466. end;
  467. procedure TExternalAssembler.AsmCreate(Aplace:tcutplace);
  468. begin
  469. if SmartAsm then
  470. NextSmartName(Aplace);
  471. {$ifdef hasunix}
  472. if DoPipe then
  473. begin
  474. Message1(exec_i_assembling_pipe,asmfile);
  475. POpen(outfile,'as -o '+objfile,'W');
  476. end
  477. else
  478. {$endif}
  479. begin
  480. Assign(outfile,asmfile);
  481. {$I-}
  482. Rewrite(outfile,1);
  483. {$I+}
  484. if ioresult<>0 then
  485. Message1(exec_d_cant_create_asmfile,asmfile);
  486. end;
  487. outcnt:=0;
  488. AsmSize:=0;
  489. AsmStartSize:=0;
  490. end;
  491. procedure TExternalAssembler.AsmClose;
  492. var
  493. f : file;
  494. l : longint;
  495. begin
  496. AsmFlush;
  497. {$ifdef hasunix}
  498. if DoPipe then
  499. PClose(outfile)
  500. else
  501. {$endif}
  502. begin
  503. {Touch Assembler time to ppu time is there is a ppufilename}
  504. if ppufilename<>'' then
  505. begin
  506. Assign(f,ppufilename);
  507. {$I-}
  508. reset(f,1);
  509. {$I+}
  510. if ioresult=0 then
  511. begin
  512. getftime(f,l);
  513. close(f);
  514. reset(outfile,1);
  515. setftime(outfile,l);
  516. end;
  517. end;
  518. close(outfile);
  519. end;
  520. end;
  521. procedure TExternalAssembler.WriteTree(p:TAAsmoutput);
  522. begin
  523. end;
  524. procedure TExternalAssembler.WriteAsmList;
  525. begin
  526. end;
  527. procedure TExternalAssembler.MakeObject;
  528. begin
  529. AsmCreate(cut_normal);
  530. WriteAsmList;
  531. AsmClose;
  532. DoAssemble;
  533. end;
  534. {*****************************************************************************
  535. TInternalAssembler
  536. *****************************************************************************}
  537. constructor TInternalAssembler.create(smart:boolean);
  538. begin
  539. inherited create(smart);
  540. objectoutput:=nil;
  541. objectdata:=nil;
  542. SmartAsm:=smart;
  543. currpass:=0;
  544. end;
  545. destructor TInternalAssembler.destroy;
  546. {$ifdef MEMDEBUG}
  547. var
  548. d : tmemdebug;
  549. {$endif}
  550. begin
  551. {$ifdef MEMDEBUG}
  552. d := tmemdebug.create(name+' - agbin');
  553. {$endif}
  554. objectdata.free;
  555. objectoutput.free;
  556. {$ifdef MEMDEBUG}
  557. d.free;
  558. {$endif}
  559. end;
  560. {$ifdef GDB}
  561. procedure TInternalAssembler.convertstabs(p:pchar);
  562. var
  563. ofs,
  564. nidx,nother,ii,i,line,j : longint;
  565. code : integer;
  566. hp : pchar;
  567. reloc : boolean;
  568. ps : tasmsymbol;
  569. s : string;
  570. begin
  571. ofs:=0;
  572. reloc:=true;
  573. ps:=nil;
  574. if p[0]='"' then
  575. begin
  576. i:=1;
  577. { we can have \" inside the string !! PM }
  578. while not ((p[i]='"') and (p[i-1]<>'\')) do
  579. inc(i);
  580. p[i]:=#0;
  581. ii:=i;
  582. hp:=@p[1];
  583. s:=StrPas(@P[i+2]);
  584. end
  585. else
  586. begin
  587. hp:=nil;
  588. s:=StrPas(P);
  589. i:=-2; {needed below (PM) }
  590. end;
  591. { When in pass 1 then only alloc and leave }
  592. if currpass=1 then
  593. begin
  594. objectdata.allocstabs(hp);
  595. if assigned(hp) then
  596. p[i]:='"';
  597. exit;
  598. end;
  599. { Parse the rest of the stabs }
  600. if s='' then
  601. internalerror(33000);
  602. j:=pos(',',s);
  603. if j=0 then
  604. internalerror(33001);
  605. Val(Copy(s,1,j-1),nidx,code);
  606. if code<>0 then
  607. internalerror(33002);
  608. i:=i+2+j;
  609. Delete(s,1,j);
  610. j:=pos(',',s);
  611. if (j=0) then
  612. internalerror(33003);
  613. Val(Copy(s,1,j-1),nother,code);
  614. if code<>0 then
  615. internalerror(33004);
  616. i:=i+j;
  617. Delete(s,1,j);
  618. j:=pos(',',s);
  619. if j=0 then
  620. begin
  621. j:=256;
  622. ofs:=-1;
  623. end;
  624. Val(Copy(s,1,j-1),line,code);
  625. if code<>0 then
  626. internalerror(33005);
  627. if ofs=0 then
  628. begin
  629. Delete(s,1,j);
  630. i:=i+j;
  631. Val(s,ofs,code);
  632. if code=0 then
  633. reloc:=false
  634. else
  635. begin
  636. ofs:=0;
  637. s:=strpas(@p[i]);
  638. { handle asmsymbol or
  639. asmsymbol - asmsymbol }
  640. j:=pos(' ',s);
  641. if j=0 then
  642. j:=pos('-',s);
  643. { also try to handle
  644. asmsymbol + constant
  645. or
  646. asmsymbol - constant }
  647. if j=0 then
  648. j:=pos('+',s);
  649. if j<>0 then
  650. begin
  651. Val(Copy(s,j+1,255),ofs,code);
  652. if code<>0 then
  653. ofs:=0
  654. else
  655. { constant reading successful,
  656. avoid further treatment by
  657. setting s[j] to '+' }
  658. s[j]:='+';
  659. end
  660. else
  661. { single asmsymbol }
  662. j:=256;
  663. { the symbol can be external
  664. so we must use newasmsymbol and
  665. not getasmsymbol !! PM }
  666. ps:=objectlibrary.newasmsymbol(copy(s,1,j-1),AB_EXTERNAL,AT_NONE);
  667. if not assigned(ps) then
  668. internalerror(33006)
  669. else
  670. begin
  671. ofs:=ofs+ps.address;
  672. reloc:=true;
  673. objectlibrary.UsedAsmSymbolListInsert(ps);
  674. end;
  675. if (j<256) and (s[j]<>'+') then
  676. begin
  677. i:=i+j;
  678. s:=strpas(@p[i]);
  679. if (s<>'') and (s[1]=' ') then
  680. begin
  681. j:=0;
  682. while (s[j+1]=' ') do
  683. inc(j);
  684. i:=i+j;
  685. s:=strpas(@p[i]);
  686. end;
  687. ps:=objectlibrary.getasmsymbol(s);
  688. if not assigned(ps) then
  689. internalerror(33007)
  690. else
  691. begin
  692. if ps.section<>objectdata.currsec then
  693. internalerror(33008);
  694. ofs:=ofs-ps.address;
  695. reloc:=false;
  696. objectlibrary.UsedAsmSymbolListInsert(ps);
  697. end;
  698. end;
  699. end;
  700. end;
  701. { external bss need speical handling (PM) }
  702. if assigned(ps) and (ps.section=nil) then
  703. begin
  704. if currpass=2 then
  705. begin
  706. objectdata.writesymbol(ps);
  707. objectoutput.exportsymbol(ps);
  708. end;
  709. objectdata.writeSymStabs(ofs,hp,ps,nidx,nother,line,reloc)
  710. end
  711. else
  712. objectdata.writeStabs(ofs,hp,nidx,nother,line,reloc);
  713. if assigned(hp) then
  714. p[ii]:='"';
  715. end;
  716. procedure TInternalAssembler.emitlineinfostabs(nidx,line : longint);
  717. begin
  718. if currpass=1 then
  719. begin
  720. objectdata.allocstabs(nil);
  721. exit;
  722. end;
  723. if (nidx=n_textline) and assigned(funcname) and
  724. (target_info.use_function_relative_addresses) then
  725. objectdata.writeStabs(objectdata.currsec.datasize-funcname.address,nil,nidx,0,line,false)
  726. else
  727. objectdata.writeStabs(objectdata.currsec.datasize,nil,nidx,0,line,true);
  728. end;
  729. procedure TInternalAssembler.emitstabs(s:string);
  730. begin
  731. s:=s+#0;
  732. ConvertStabs(@s[1]);
  733. end;
  734. procedure TInternalAssembler.WriteFileLineInfo(var fileinfo : tfileposinfo);
  735. var
  736. curr_n : byte;
  737. hp : tasmsymbol;
  738. infile : tinputfile;
  739. begin
  740. if not ((cs_debuginfo in aktmoduleswitches) or
  741. (cs_gdb_lineinfo in aktglobalswitches)) then
  742. exit;
  743. { file changed ? (must be before line info) }
  744. if (fileinfo.fileindex<>0) and
  745. (stabslastfileinfo.fileindex<>fileinfo.fileindex) then
  746. begin
  747. infile:=current_module.sourcefiles.get_file(fileinfo.fileindex);
  748. if assigned(infile) then
  749. begin
  750. if includecount=0 then
  751. curr_n:=n_sourcefile
  752. else
  753. curr_n:=n_includefile;
  754. { get symbol for this includefile }
  755. hp:=objectlibrary.newasmsymbol('Ltext'+ToStr(IncludeCount),AB_LOCAL,AT_FUNCTION);
  756. if currpass=1 then
  757. begin
  758. objectdata.allocsymbol(currpass,hp,0);
  759. objectlibrary.UsedAsmSymbolListInsert(hp);
  760. end
  761. else
  762. objectdata.writesymbol(hp);
  763. { emit stabs }
  764. if (infile.path^<>'') then
  765. EmitStabs('"'+lower(BsToSlash(FixPath(infile.path^,false)))+'",'+tostr(curr_n)+
  766. ',0,0,Ltext'+ToStr(IncludeCount));
  767. EmitStabs('"'+lower(FixFileName(infile.name^))+'",'+tostr(curr_n)+
  768. ',0,0,Ltext'+ToStr(IncludeCount));
  769. inc(includecount);
  770. { force new line info }
  771. stabslastfileinfo.line:=-1;
  772. end;
  773. end;
  774. { line changed ? }
  775. if (stabslastfileinfo.line<>fileinfo.line) and (fileinfo.line<>0) then
  776. emitlineinfostabs(n_line,fileinfo.line);
  777. stabslastfileinfo:=fileinfo;
  778. end;
  779. procedure TInternalAssembler.StartFileLineInfo;
  780. var
  781. fileinfo : tfileposinfo;
  782. begin
  783. FillChar(stabslastfileinfo,sizeof(stabslastfileinfo),0);
  784. n_line:=n_bssline;
  785. funcname:=nil;
  786. linecount:=1;
  787. includecount:=0;
  788. fileinfo.fileindex:=1;
  789. fileinfo.line:=1;
  790. WriteFileLineInfo(fileinfo);
  791. end;
  792. procedure TInternalAssembler.EndFileLineInfo;
  793. var
  794. hp : tasmsymbol;
  795. begin
  796. if not ((cs_debuginfo in aktmoduleswitches) or
  797. (cs_gdb_lineinfo in aktglobalswitches)) then
  798. exit;
  799. objectdata.createsection(sec_code,'',0,[]);
  800. hp:=objectlibrary.newasmsymbol('Letext',AB_LOCAL,AT_FUNCTION);
  801. if currpass=1 then
  802. begin
  803. objectdata.allocsymbol(currpass,hp,0);
  804. objectlibrary.UsedAsmSymbolListInsert(hp);
  805. end
  806. else
  807. objectdata.writesymbol(hp);
  808. EmitStabs('"",'+tostr(n_sourcefile)+',0,0,Letext');
  809. end;
  810. {$endif GDB}
  811. function TInternalAssembler.MaybeNextList(var hp:Tai):boolean;
  812. begin
  813. { maybe end of list }
  814. while not assigned(hp) do
  815. begin
  816. if currlistidx<lists then
  817. begin
  818. inc(currlistidx);
  819. currlist:=list[currlistidx];
  820. hp:=Tai(currList.first);
  821. end
  822. else
  823. begin
  824. MaybeNextList:=false;
  825. exit;
  826. end;
  827. end;
  828. MaybeNextList:=true;
  829. end;
  830. function TInternalAssembler.TreePass0(hp:Tai):Tai;
  831. var
  832. l : longint;
  833. begin
  834. while assigned(hp) do
  835. begin
  836. case hp.typ of
  837. ait_align :
  838. begin
  839. { always use the maximum fillsize in this pass to avoid possible
  840. short jumps to become out of range }
  841. Tai_align(hp).fillsize:=Tai_align(hp).aligntype;
  842. objectdata.alloc(Tai_align(hp).fillsize);
  843. end;
  844. ait_datablock :
  845. begin
  846. l:=used_align(size_2_align(Tai_datablock(hp).size),0,objectdata.currsec.addralign);
  847. if SmartAsm or (not Tai_datablock(hp).is_global) then
  848. begin
  849. objectdata.allocalign(l);
  850. objectdata.alloc(Tai_datablock(hp).size);
  851. end;
  852. end;
  853. ait_real_80bit :
  854. objectdata.alloc(10);
  855. ait_real_64bit :
  856. objectdata.alloc(8);
  857. ait_real_32bit :
  858. objectdata.alloc(4);
  859. ait_comp_64bit :
  860. objectdata.alloc(8);
  861. ait_const_64bit,
  862. ait_const_32bit,
  863. ait_const_16bit,
  864. ait_const_8bit,
  865. ait_const_rva_symbol,
  866. ait_const_indirect_symbol :
  867. objectdata.alloc(tai_const(hp).size);
  868. ait_section:
  869. begin
  870. objectdata.CreateSection(Tai_section(hp).sectype,Tai_section(hp).name^,Tai_section(hp).secalign,[]);
  871. Tai_section(hp).sec:=objectdata.CurrSec;
  872. end;
  873. ait_symbol :
  874. objectdata.allocsymbol(currpass,Tai_symbol(hp).sym,0);
  875. ait_label :
  876. objectdata.allocsymbol(currpass,Tai_label(hp).l,0);
  877. ait_string :
  878. objectdata.alloc(Tai_string(hp).len);
  879. ait_instruction :
  880. begin
  881. {$ifdef i386}
  882. {$ifndef NOAG386BIN}
  883. { reset instructions which could change in pass 2 }
  884. Taicpu(hp).resetpass2;
  885. objectdata.alloc(Taicpu(hp).Pass1(objectdata.currsec.datasize));
  886. {$endif NOAG386BIN}
  887. {$endif i386}
  888. end;
  889. ait_cutobject :
  890. if SmartAsm then
  891. break;
  892. end;
  893. hp:=Tai(hp.next);
  894. end;
  895. TreePass0:=hp;
  896. end;
  897. function TInternalAssembler.TreePass1(hp:Tai):Tai;
  898. var
  899. InlineLevel,
  900. l : longint;
  901. {$ifdef i386}
  902. {$ifndef NOAG386BIN}
  903. i : longint;
  904. {$endif NOAG386BIN}
  905. {$endif i386}
  906. begin
  907. inlinelevel:=0;
  908. while assigned(hp) do
  909. begin
  910. {$ifdef GDB}
  911. { write stabs, no line info for inlined code }
  912. if (inlinelevel=0) and
  913. ((cs_debuginfo in aktmoduleswitches) or
  914. (cs_gdb_lineinfo in aktglobalswitches)) then
  915. begin
  916. if (objectdata.currsec<>nil) and
  917. not(hp.typ in SkipLineInfo) then
  918. WriteFileLineInfo(tailineinfo(hp).fileinfo);
  919. end;
  920. {$endif GDB}
  921. case hp.typ of
  922. ait_align :
  923. begin
  924. { here we must determine the fillsize which is used in pass2 }
  925. Tai_align(hp).fillsize:=align(objectdata.currsec.datasize,Tai_align(hp).aligntype)-
  926. objectdata.currsec.datasize;
  927. objectdata.alloc(Tai_align(hp).fillsize);
  928. end;
  929. ait_datablock :
  930. begin
  931. if objectdata.currsec.sectype<>sec_bss then
  932. Message(asmw_e_alloc_data_only_in_bss);
  933. l:=used_align(size_2_align(Tai_datablock(hp).size),0,objectdata.currsec.addralign);
  934. if Tai_datablock(hp).is_global and
  935. not SmartAsm then
  936. begin
  937. objectdata.allocsymbol(currpass,Tai_datablock(hp).sym,Tai_datablock(hp).size);
  938. { force to be common/external, must be after setaddress as that would
  939. set it to AB_GLOBAL }
  940. Tai_datablock(hp).sym.currbind:=AB_COMMON;
  941. end
  942. else
  943. begin
  944. objectdata.allocalign(l);
  945. objectdata.allocsymbol(currpass,Tai_datablock(hp).sym,Tai_datablock(hp).size);
  946. objectdata.alloc(Tai_datablock(hp).size);
  947. end;
  948. objectlibrary.UsedAsmSymbolListInsert(Tai_datablock(hp).sym);
  949. end;
  950. ait_real_80bit :
  951. objectdata.alloc(10);
  952. ait_real_64bit :
  953. objectdata.alloc(8);
  954. ait_real_32bit :
  955. objectdata.alloc(4);
  956. ait_comp_64bit :
  957. objectdata.alloc(8);
  958. ait_const_64bit,
  959. ait_const_32bit,
  960. ait_const_16bit,
  961. ait_const_8bit,
  962. ait_const_rva_symbol,
  963. ait_const_indirect_symbol :
  964. begin
  965. objectdata.alloc(tai_const(hp).size);
  966. if assigned(Tai_const(hp).sym) then
  967. objectlibrary.UsedAsmSymbolListInsert(Tai_const(hp).sym);
  968. if assigned(Tai_const(hp).endsym) then
  969. objectlibrary.UsedAsmSymbolListInsert(Tai_const(hp).endsym);
  970. end;
  971. ait_section:
  972. begin
  973. { use cached value }
  974. objectdata.setsection(Tai_section(hp).sec);
  975. {$ifdef GDB}
  976. case Tai_section(hp).sectype of
  977. sec_code :
  978. n_line:=n_textline;
  979. sec_data :
  980. n_line:=n_dataline;
  981. sec_bss :
  982. n_line:=n_bssline;
  983. else
  984. n_line:=n_dataline;
  985. end;
  986. stabslastfileinfo.line:=-1;
  987. {$endif GDB}
  988. end;
  989. {$ifdef GDB}
  990. ait_stabn :
  991. begin
  992. if assigned(Tai_stabn(hp).str) then
  993. convertstabs(Tai_stabn(hp).str);
  994. end;
  995. ait_stabs :
  996. begin
  997. if assigned(Tai_stabs(hp).str) then
  998. convertstabs(Tai_stabs(hp).str);
  999. end;
  1000. ait_stab_function_name :
  1001. begin
  1002. if assigned(Tai_stab_function_name(hp).str) then
  1003. begin
  1004. funcname:=objectlibrary.getasmsymbol(strpas(Tai_stab_function_name(hp).str));
  1005. objectlibrary.UsedAsmSymbolListInsert(funcname);
  1006. end
  1007. else
  1008. funcname:=nil;
  1009. end;
  1010. ait_force_line :
  1011. stabslastfileinfo.line:=0;
  1012. {$endif}
  1013. ait_symbol :
  1014. begin
  1015. objectdata.allocsymbol(currpass,Tai_symbol(hp).sym,0);
  1016. objectlibrary.UsedAsmSymbolListInsert(Tai_symbol(hp).sym);
  1017. end;
  1018. ait_symbol_end :
  1019. begin
  1020. if target_info.system in [system_i386_linux,system_i386_beos] then
  1021. begin
  1022. Tai_symbol_end(hp).sym.size:=objectdata.currsec.datasize-Tai_symbol_end(hp).sym.address;
  1023. objectlibrary.UsedAsmSymbolListInsert(Tai_symbol_end(hp).sym);
  1024. end;
  1025. end;
  1026. ait_label :
  1027. begin
  1028. objectdata.allocsymbol(currpass,Tai_label(hp).l,0);
  1029. objectlibrary.UsedAsmSymbolListInsert(Tai_label(hp).l);
  1030. end;
  1031. ait_string :
  1032. objectdata.alloc(Tai_string(hp).len);
  1033. ait_instruction :
  1034. begin
  1035. {$ifdef i386}
  1036. {$ifndef NOAG386BIN}
  1037. objectdata.alloc(Taicpu(hp).Pass1(objectdata.currsec.datasize));
  1038. { fixup the references }
  1039. for i:=1 to Taicpu(hp).ops do
  1040. begin
  1041. with Taicpu(hp).oper[i-1]^ do
  1042. begin
  1043. case typ of
  1044. top_ref :
  1045. begin
  1046. if assigned(ref^.symbol) then
  1047. objectlibrary.UsedAsmSymbolListInsert(ref^.symbol);
  1048. if assigned(ref^.relsymbol) then
  1049. objectlibrary.UsedAsmSymbolListInsert(ref^.symbol);
  1050. end;
  1051. end;
  1052. end;
  1053. end;
  1054. {$endif NOAG386BIN}
  1055. {$endif i386}
  1056. end;
  1057. ait_direct :
  1058. Message(asmw_f_direct_not_supported);
  1059. ait_cutobject :
  1060. if SmartAsm then
  1061. break;
  1062. ait_marker :
  1063. if tai_marker(hp).kind=InlineStart then
  1064. inc(InlineLevel)
  1065. else if tai_marker(hp).kind=InlineEnd then
  1066. dec(InlineLevel);
  1067. end;
  1068. hp:=Tai(hp.next);
  1069. end;
  1070. TreePass1:=hp;
  1071. end;
  1072. function TInternalAssembler.TreePass2(hp:Tai):Tai;
  1073. var
  1074. fillbuffer : tfillbuffer;
  1075. InlineLevel,
  1076. l : longint;
  1077. v : int64;
  1078. {$ifdef x86}
  1079. co : comp;
  1080. {$endif x86}
  1081. begin
  1082. inlinelevel:=0;
  1083. { main loop }
  1084. while assigned(hp) do
  1085. begin
  1086. {$ifdef GDB}
  1087. { write stabs, no line info for inlined code }
  1088. if (inlinelevel=0) and
  1089. ((cs_debuginfo in aktmoduleswitches) or
  1090. (cs_gdb_lineinfo in aktglobalswitches)) then
  1091. begin
  1092. if (objectdata.currsec<>nil) and
  1093. not(hp.typ in SkipLineInfo) then
  1094. WriteFileLineInfo(tailineinfo(hp).fileinfo);
  1095. end;
  1096. {$endif GDB}
  1097. case hp.typ of
  1098. ait_align :
  1099. begin
  1100. if objectdata.currsec.sectype=sec_bss then
  1101. objectdata.alloc(Tai_align(hp).fillsize)
  1102. else
  1103. objectdata.writebytes(Tai_align(hp).calculatefillbuf(fillbuffer)^,Tai_align(hp).fillsize);
  1104. end;
  1105. ait_section :
  1106. begin
  1107. { use cached value }
  1108. objectdata.setsection(Tai_section(hp).sec);
  1109. {$ifdef GDB}
  1110. case Tai_section(hp).sectype of
  1111. sec_code : n_line:=n_textline;
  1112. sec_data : n_line:=n_dataline;
  1113. sec_bss : n_line:=n_bssline;
  1114. else
  1115. n_line:=n_dataline;
  1116. end;
  1117. stabslastfileinfo.line:=-1;
  1118. {$endif GDB}
  1119. end;
  1120. ait_symbol :
  1121. begin
  1122. objectdata.writesymbol(Tai_symbol(hp).sym);
  1123. objectoutput.exportsymbol(Tai_symbol(hp).sym);
  1124. end;
  1125. ait_datablock :
  1126. begin
  1127. l:=used_align(size_2_align(Tai_datablock(hp).size),0,objectdata.currsec.addralign);
  1128. objectdata.writesymbol(Tai_datablock(hp).sym);
  1129. objectoutput.exportsymbol(Tai_datablock(hp).sym);
  1130. if SmartAsm or (not Tai_datablock(hp).is_global) then
  1131. begin
  1132. objectdata.allocalign(l);
  1133. objectdata.alloc(Tai_datablock(hp).size);
  1134. end;
  1135. end;
  1136. ait_real_80bit :
  1137. objectdata.writebytes(Tai_real_80bit(hp).value,10);
  1138. ait_real_64bit :
  1139. objectdata.writebytes(Tai_real_64bit(hp).value,8);
  1140. ait_real_32bit :
  1141. objectdata.writebytes(Tai_real_32bit(hp).value,4);
  1142. ait_comp_64bit :
  1143. begin
  1144. {$ifdef x86}
  1145. {$ifdef FPC}
  1146. co:=comp(Tai_comp_64bit(hp).value);
  1147. {$else}
  1148. co:=Tai_comp_64bit(hp).value;
  1149. {$endif}
  1150. objectdata.writebytes(co,8);
  1151. {$endif x86}
  1152. end;
  1153. ait_string :
  1154. objectdata.writebytes(Tai_string(hp).str^,Tai_string(hp).len);
  1155. ait_const_64bit,
  1156. ait_const_32bit,
  1157. ait_const_16bit,
  1158. ait_const_8bit :
  1159. begin
  1160. if assigned(tai_const(hp).sym) then
  1161. begin
  1162. if assigned(tai_const(hp).endsym) then
  1163. begin
  1164. if tai_const(hp).endsym.section<>tai_const(hp).sym.section then
  1165. internalerror(200404124);
  1166. v:=tai_const(hp).endsym.address-tai_const(hp).sym.address+Tai_const(hp).value;
  1167. objectdata.writebytes(v,tai_const(hp).size);
  1168. end
  1169. else
  1170. objectdata.writereloc(Tai_const(hp).value,Tai_const(hp).size,Tai_const(hp).sym,RELOC_ABSOLUTE);
  1171. end
  1172. else
  1173. objectdata.writebytes(Tai_const(hp).value,tai_const(hp).size);
  1174. end;
  1175. ait_const_rva_symbol :
  1176. objectdata.writereloc(Tai_const(hp).value,sizeof(aint),Tai_const(hp).sym,RELOC_RVA);
  1177. ait_label :
  1178. begin
  1179. objectdata.writesymbol(Tai_label(hp).l);
  1180. { exporting shouldn't be necessary as labels are local,
  1181. but it's better to be on the safe side (PFV) }
  1182. objectoutput.exportsymbol(Tai_label(hp).l);
  1183. end;
  1184. {$ifdef i386}
  1185. {$ifndef NOAG386BIN}
  1186. ait_instruction :
  1187. Taicpu(hp).Pass2(objectdata);
  1188. {$endif NOAG386BIN}
  1189. {$endif i386}
  1190. {$ifdef GDB}
  1191. ait_stabn :
  1192. convertstabs(Tai_stabn(hp).str);
  1193. ait_stabs :
  1194. convertstabs(Tai_stabs(hp).str);
  1195. ait_stab_function_name :
  1196. if assigned(Tai_stab_function_name(hp).str) then
  1197. funcname:=objectlibrary.getasmsymbol(strpas(Tai_stab_function_name(hp).str))
  1198. else
  1199. funcname:=nil;
  1200. ait_force_line :
  1201. stabslastfileinfo.line:=0;
  1202. {$endif}
  1203. ait_cutobject :
  1204. if SmartAsm then
  1205. break;
  1206. ait_marker :
  1207. if tai_marker(hp).kind=InlineStart then
  1208. inc(InlineLevel)
  1209. else if tai_marker(hp).kind=InlineEnd then
  1210. dec(InlineLevel);
  1211. end;
  1212. hp:=Tai(hp.next);
  1213. end;
  1214. TreePass2:=hp;
  1215. end;
  1216. procedure TInternalAssembler.writetree;
  1217. var
  1218. hp : Tai;
  1219. label
  1220. doexit;
  1221. begin
  1222. objectdata:=objectoutput.newobjectdata(Objfile);
  1223. { reset the asmsymbol list }
  1224. objectlibrary.CreateUsedAsmsymbolList;
  1225. { Pass 0 }
  1226. currpass:=0;
  1227. objectdata.createsection(sec_code,'',0,[]);
  1228. objectdata.beforealloc;
  1229. { start with list 1 }
  1230. currlistidx:=1;
  1231. currlist:=list[currlistidx];
  1232. hp:=Tai(currList.first);
  1233. while assigned(hp) do
  1234. begin
  1235. hp:=TreePass0(hp);
  1236. MaybeNextList(hp);
  1237. end;
  1238. objectdata.afteralloc;
  1239. { leave if errors have occured }
  1240. if errorcount>0 then
  1241. goto doexit;
  1242. { Pass 1 }
  1243. currpass:=1;
  1244. objectdata.resetsections;
  1245. objectdata.beforealloc;
  1246. objectdata.createsection(sec_code,'',0,[]);
  1247. {$ifdef GDB}
  1248. StartFileLineInfo;
  1249. {$endif GDB}
  1250. { start with list 1 }
  1251. currlistidx:=1;
  1252. currlist:=list[currlistidx];
  1253. hp:=Tai(currList.first);
  1254. while assigned(hp) do
  1255. begin
  1256. hp:=TreePass1(hp);
  1257. MaybeNextList(hp);
  1258. end;
  1259. {$ifdef GDB}
  1260. EndFileLineInfo;
  1261. {$endif GDB}
  1262. objectdata.afteralloc;
  1263. { check for undefined labels and reset }
  1264. objectlibrary.UsedAsmSymbolListCheckUndefined;
  1265. { leave if errors have occured }
  1266. if errorcount>0 then
  1267. goto doexit;
  1268. { Pass 2 }
  1269. currpass:=2;
  1270. objectdata.resetsections;
  1271. objectdata.beforewrite;
  1272. objectdata.createsection(sec_code,'',0,[]);
  1273. {$ifdef GDB}
  1274. StartFileLineInfo;
  1275. {$endif GDB}
  1276. { start with list 1 }
  1277. currlistidx:=1;
  1278. currlist:=list[currlistidx];
  1279. hp:=Tai(currList.first);
  1280. while assigned(hp) do
  1281. begin
  1282. hp:=TreePass2(hp);
  1283. MaybeNextList(hp);
  1284. end;
  1285. {$ifdef GDB}
  1286. EndFileLineInfo;
  1287. {$endif GDB}
  1288. objectdata.afterwrite;
  1289. { don't write the .o file if errors have occured }
  1290. if errorcount=0 then
  1291. begin
  1292. { write objectfile }
  1293. objectoutput.startobjectfile(ObjFile);
  1294. objectoutput.writeobjectfile(objectdata);
  1295. objectdata.free;
  1296. objectdata:=nil;
  1297. end;
  1298. doexit:
  1299. { reset the used symbols back, must be after the .o has been
  1300. written }
  1301. objectlibrary.UsedAsmsymbolListReset;
  1302. objectlibrary.DestroyUsedAsmsymbolList;
  1303. end;
  1304. procedure TInternalAssembler.writetreesmart;
  1305. var
  1306. hp : Tai;
  1307. startsectype : TAsmSectionType;
  1308. place: tcutplace;
  1309. begin
  1310. NextSmartName(cut_normal);
  1311. objectdata:=objectoutput.newobjectdata(Objfile);
  1312. startsectype:=sec_code;
  1313. { start with list 1 }
  1314. currlistidx:=1;
  1315. currlist:=list[currlistidx];
  1316. hp:=Tai(currList.first);
  1317. while assigned(hp) do
  1318. begin
  1319. { reset the asmsymbol list }
  1320. objectlibrary.CreateUsedAsmSymbolList;
  1321. { Pass 0 }
  1322. currpass:=0;
  1323. objectdata.resetsections;
  1324. objectdata.beforealloc;
  1325. objectdata.createsection(startsectype,'',0,[]);
  1326. TreePass0(hp);
  1327. objectdata.afteralloc;
  1328. { leave if errors have occured }
  1329. if errorcount>0 then
  1330. exit;
  1331. { Pass 1 }
  1332. currpass:=1;
  1333. objectdata.resetsections;
  1334. objectdata.beforealloc;
  1335. objectdata.createsection(startsectype,'',0,[]);
  1336. {$ifdef GDB}
  1337. StartFileLineInfo;
  1338. {$endif GDB}
  1339. TreePass1(hp);
  1340. {$ifdef GDB}
  1341. EndFileLineInfo;
  1342. {$endif GDB}
  1343. objectdata.afteralloc;
  1344. { check for undefined labels }
  1345. objectlibrary.UsedAsmSymbolListCheckUndefined;
  1346. { leave if errors have occured }
  1347. if errorcount>0 then
  1348. exit;
  1349. { Pass 2 }
  1350. currpass:=2;
  1351. objectoutput.startobjectfile(Objfile);
  1352. objectdata.resetsections;
  1353. objectdata.beforewrite;
  1354. objectdata.createsection(startsectype,'',0,[]);
  1355. {$ifdef GDB}
  1356. StartFileLineInfo;
  1357. {$endif GDB}
  1358. hp:=TreePass2(hp);
  1359. { save section type for next loop, must be done before EndFileLineInfo
  1360. because that changes the section to sec_code }
  1361. startsectype:=objectdata.currsec.sectype;
  1362. {$ifdef GDB}
  1363. EndFileLineInfo;
  1364. {$endif GDB}
  1365. objectdata.afterwrite;
  1366. { leave if errors have occured }
  1367. if errorcount>0 then
  1368. exit;
  1369. { write the current objectfile }
  1370. objectoutput.writeobjectfile(objectdata);
  1371. objectdata.free;
  1372. objectdata:=nil;
  1373. { reset the used symbols back, must be after the .o has been
  1374. written }
  1375. objectlibrary.UsedAsmsymbolListReset;
  1376. objectlibrary.DestroyUsedAsmsymbolList;
  1377. { end of lists? }
  1378. if not MaybeNextList(hp) then
  1379. break;
  1380. { we will start a new objectfile so reset everything }
  1381. { The place can still change in the next while loop, so don't init }
  1382. { the writer yet (JM) }
  1383. if (hp.typ=ait_cutobject) then
  1384. place := Tai_cutobject(hp).place
  1385. else
  1386. place := cut_normal;
  1387. { avoid empty files }
  1388. while assigned(hp) and
  1389. (Tai(hp).typ in [ait_marker,ait_comment,ait_section,ait_cutobject]) do
  1390. begin
  1391. if Tai(hp).typ=ait_section then
  1392. startsectype:=Tai_section(hp).sectype
  1393. else if (Tai(hp).typ=ait_cutobject) then
  1394. place:=Tai_cutobject(hp).place;
  1395. hp:=Tai(hp.next);
  1396. end;
  1397. { there is a problem if startsectype is sec_none !! PM }
  1398. if startsectype=sec_none then
  1399. startsectype:=sec_code;
  1400. if not MaybeNextList(hp) then
  1401. break;
  1402. { start next objectfile }
  1403. NextSmartName(place);
  1404. objectdata:=objectoutput.newobjectdata(Objfile);
  1405. end;
  1406. end;
  1407. procedure TInternalAssembler.MakeObject;
  1408. procedure addlist(p:TAAsmoutput);
  1409. begin
  1410. inc(lists);
  1411. list[lists]:=p;
  1412. end;
  1413. begin
  1414. if cs_debuginfo in aktmoduleswitches then
  1415. addlist(debuglist);
  1416. addlist(codesegment);
  1417. addlist(datasegment);
  1418. addlist(consts);
  1419. addlist(rttilist);
  1420. addlist(picdata);
  1421. if assigned(resourcestringlist) then
  1422. addlist(resourcestringlist);
  1423. addlist(bsssegment);
  1424. if assigned(importssection) then
  1425. addlist(importssection);
  1426. if assigned(exportssection) and not UseDeffileForExports then
  1427. addlist(exportssection);
  1428. if assigned(resourcesection) then
  1429. addlist(resourcesection);
  1430. {$warning TODO internal writer support for dwarf}
  1431. {if assigned(dwarflist) then
  1432. addlist(dwarflist);}
  1433. if SmartAsm then
  1434. writetreesmart
  1435. else
  1436. writetree;
  1437. end;
  1438. {*****************************************************************************
  1439. Generate Assembler Files Main Procedure
  1440. *****************************************************************************}
  1441. Procedure GenerateAsm(smart:boolean);
  1442. var
  1443. a : TAssembler;
  1444. begin
  1445. if not assigned(CAssembler[target_asm.id]) then
  1446. Message(asmw_f_assembler_output_not_supported);
  1447. a:=CAssembler[target_asm.id].Create(smart);
  1448. a.MakeObject;
  1449. a.Free;
  1450. end;
  1451. Procedure OnlyAsm;
  1452. var
  1453. a : TExternalAssembler;
  1454. begin
  1455. a:=TExternalAssembler.Create(false);
  1456. a.DoAssemble;
  1457. a.Free;
  1458. end;
  1459. {*****************************************************************************
  1460. Init/Done
  1461. *****************************************************************************}
  1462. procedure RegisterAssembler(const r:tasminfo;c:TAssemblerClass);
  1463. var
  1464. t : tasm;
  1465. begin
  1466. t:=r.id;
  1467. if assigned(asminfos[t]) then
  1468. writeln('Warning: Assembler is already registered!')
  1469. else
  1470. Getmem(asminfos[t],sizeof(tasminfo));
  1471. asminfos[t]^:=r;
  1472. CAssembler[t]:=c;
  1473. end;
  1474. procedure InitAssembler;
  1475. begin
  1476. { target_asm is already set by readarguments }
  1477. initoutputformat:=target_asm.id;
  1478. aktoutputformat:=target_asm.id;
  1479. end;
  1480. procedure DoneAssembler;
  1481. begin
  1482. end;
  1483. end.
  1484. {
  1485. $Log$
  1486. Revision 1.70 2004-07-01 15:42:53 peter
  1487. * fix wrong calculation of .bss section
  1488. Revision 1.69 2004/06/20 08:55:28 florian
  1489. * logs truncated
  1490. Revision 1.68 2004/06/16 20:07:06 florian
  1491. * dwarf branch merged
  1492. Revision 1.67 2004/05/21 22:43:36 peter
  1493. * set correct n_line type when starting new .o file by passing
  1494. the current section type
  1495. Revision 1.66.2.7 2004/05/03 14:59:57 peter
  1496. * no dlltool needed for win32 linking executables
  1497. Revision 1.66.2.6 2004/05/01 16:02:09 peter
  1498. * POINTER_SIZE replaced with sizeof(aint)
  1499. * aint,aword,tconst*int moved to globtype
  1500. Revision 1.66.2.5 2004/04/29 23:30:28 peter
  1501. * fix i386 compiler
  1502. }