assemble.pas 52 KB

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