assemble.pas 52 KB

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