assemble.pas 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673
  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,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. if (cs_link_on_target in aktglobalswitches) then
  374. begin
  375. Replace(s,'$ASM',ScriptFixFileName(AsmFile));
  376. Replace(s,'$OBJ',ScriptFixFileName(ObjFile));
  377. end
  378. else
  379. begin
  380. Replace(s,'$ASM',AsmFile);
  381. Replace(s,'$OBJ',ObjFile);
  382. end;
  383. if CallAssembler(FindAssembler,s) then
  384. RemoveAsm
  385. else
  386. begin
  387. DoAssemble:=false;
  388. GenerateError;
  389. end;
  390. end;
  391. Procedure TExternalAssembler.AsmFlush;
  392. begin
  393. if outcnt>0 then
  394. begin
  395. BlockWrite(outfile,outbuf,outcnt);
  396. outcnt:=0;
  397. end;
  398. end;
  399. Procedure TExternalAssembler.AsmClear;
  400. begin
  401. outcnt:=0;
  402. end;
  403. Procedure TExternalAssembler.AsmWrite(const s:string);
  404. begin
  405. if OutCnt+length(s)>=AsmOutSize then
  406. AsmFlush;
  407. Move(s[1],OutBuf[OutCnt],length(s));
  408. inc(OutCnt,length(s));
  409. inc(AsmSize,length(s));
  410. end;
  411. Procedure TExternalAssembler.AsmWriteLn(const s:string);
  412. begin
  413. AsmWrite(s);
  414. AsmLn;
  415. end;
  416. Procedure TExternalAssembler.AsmWritePChar(p:pchar);
  417. var
  418. i,j : longint;
  419. begin
  420. i:=StrLen(p);
  421. j:=i;
  422. while j>0 do
  423. begin
  424. i:=min(j,AsmOutSize);
  425. if OutCnt+i>=AsmOutSize then
  426. AsmFlush;
  427. Move(p[0],OutBuf[OutCnt],i);
  428. inc(OutCnt,i);
  429. inc(AsmSize,i);
  430. dec(j,i);
  431. p:=pchar(@p[i]);
  432. end;
  433. end;
  434. Procedure TExternalAssembler.AsmLn;
  435. begin
  436. if OutCnt>=AsmOutSize-2 then
  437. AsmFlush;
  438. OutBuf[OutCnt]:=target_info.newline[1];
  439. inc(OutCnt);
  440. inc(AsmSize);
  441. if length(target_info.newline)>1 then
  442. begin
  443. OutBuf[OutCnt]:=target_info.newline[2];
  444. inc(OutCnt);
  445. inc(AsmSize);
  446. end;
  447. end;
  448. procedure TExternalAssembler.AsmCreate(Aplace:tcutplace);
  449. begin
  450. if SmartAsm then
  451. NextSmartName(Aplace);
  452. {$ifdef unix}
  453. if DoPipe then
  454. begin
  455. Message1(exec_i_assembling_pipe,asmfile);
  456. POpen(outfile,'as -o '+objfile,'W');
  457. end
  458. else
  459. {$endif}
  460. begin
  461. Assign(outfile,asmfile);
  462. {$I-}
  463. Rewrite(outfile,1);
  464. {$I+}
  465. if ioresult<>0 then
  466. Message1(exec_d_cant_create_asmfile,asmfile);
  467. end;
  468. outcnt:=0;
  469. AsmSize:=0;
  470. AsmStartSize:=0;
  471. end;
  472. procedure TExternalAssembler.AsmClose;
  473. var
  474. f : file;
  475. l : longint;
  476. begin
  477. AsmFlush;
  478. {$ifdef unix}
  479. if DoPipe then
  480. PClose(outfile)
  481. else
  482. {$endif}
  483. begin
  484. {Touch Assembler time to ppu time is there is a ppufilename}
  485. if ppufilename<>'' then
  486. begin
  487. Assign(f,ppufilename);
  488. {$I-}
  489. reset(f,1);
  490. {$I+}
  491. if ioresult=0 then
  492. begin
  493. getftime(f,l);
  494. close(f);
  495. reset(outfile,1);
  496. setftime(outfile,l);
  497. end;
  498. end;
  499. close(outfile);
  500. end;
  501. end;
  502. procedure TExternalAssembler.WriteTree(p:TAAsmoutput);
  503. begin
  504. end;
  505. procedure TExternalAssembler.WriteAsmList;
  506. begin
  507. end;
  508. procedure TExternalAssembler.MakeObject;
  509. begin
  510. AsmCreate(cut_normal);
  511. WriteAsmList;
  512. AsmClose;
  513. DoAssemble;
  514. end;
  515. {*****************************************************************************
  516. TInternalAssembler
  517. *****************************************************************************}
  518. constructor TInternalAssembler.create(smart:boolean);
  519. begin
  520. inherited create(smart);
  521. objectoutput:=nil;
  522. objectdata:=nil;
  523. objectalloc:=TAsmObjectAlloc.create;
  524. SmartAsm:=smart;
  525. currpass:=0;
  526. end;
  527. destructor TInternalAssembler.destroy;
  528. {$ifdef MEMDEBUG}
  529. var
  530. d : tmemdebug;
  531. {$endif}
  532. begin
  533. {$ifdef MEMDEBUG}
  534. d := tmemdebug.create('agbin');
  535. {$endif}
  536. objectdata.free;
  537. objectoutput.free;
  538. objectalloc.free;
  539. {$ifdef MEMDEBUG}
  540. d.free;
  541. {$endif}
  542. end;
  543. {$ifdef GDB}
  544. procedure TInternalAssembler.convertstabs(p:pchar);
  545. var
  546. ofs,
  547. nidx,nother,ii,i,line,j : longint;
  548. code : integer;
  549. hp : pchar;
  550. reloc : boolean;
  551. sec : TSection;
  552. ps : tasmsymbol;
  553. s : string;
  554. begin
  555. ofs:=0;
  556. reloc:=true;
  557. ps:=nil;
  558. sec:=sec_none;
  559. if p[0]='"' then
  560. begin
  561. i:=1;
  562. { we can have \" inside the string !! PM }
  563. while not ((p[i]='"') and (p[i-1]<>'\')) do
  564. inc(i);
  565. p[i]:=#0;
  566. ii:=i;
  567. hp:=@p[1];
  568. s:=StrPas(@P[i+2]);
  569. end
  570. else
  571. begin
  572. hp:=nil;
  573. s:=StrPas(P);
  574. i:=-2; {needed below (PM) }
  575. end;
  576. { When in pass 1 then only alloc and leave }
  577. if currpass=1 then
  578. begin
  579. objectalloc.staballoc(hp);
  580. if assigned(hp) then
  581. p[i]:='"';
  582. exit;
  583. end;
  584. { Parse the rest of the stabs }
  585. if s='' then
  586. internalerror(33000);
  587. j:=pos(',',s);
  588. if j=0 then
  589. internalerror(33001);
  590. Val(Copy(s,1,j-1),nidx,code);
  591. if code<>0 then
  592. internalerror(33002);
  593. i:=i+2+j;
  594. Delete(s,1,j);
  595. j:=pos(',',s);
  596. if (j=0) then
  597. internalerror(33003);
  598. Val(Copy(s,1,j-1),nother,code);
  599. if code<>0 then
  600. internalerror(33004);
  601. i:=i+j;
  602. Delete(s,1,j);
  603. j:=pos(',',s);
  604. if j=0 then
  605. begin
  606. j:=256;
  607. ofs:=-1;
  608. end;
  609. Val(Copy(s,1,j-1),line,code);
  610. if code<>0 then
  611. internalerror(33005);
  612. if ofs=0 then
  613. begin
  614. Delete(s,1,j);
  615. i:=i+j;
  616. Val(s,ofs,code);
  617. if code=0 then
  618. reloc:=false
  619. else
  620. begin
  621. ofs:=0;
  622. s:=strpas(@p[i]);
  623. { handle asmsymbol or
  624. asmsymbol - asmsymbol }
  625. j:=pos(' ',s);
  626. if j=0 then
  627. j:=pos('-',s);
  628. { single asmsymbol }
  629. if j=0 then
  630. j:=256;
  631. { the symbol can be external
  632. so we must use newasmsymbol and
  633. not getasmsymbol !! PM }
  634. ps:=objectlibrary.newasmsymbol(copy(s,1,j-1));
  635. if not assigned(ps) then
  636. internalerror(33006)
  637. else
  638. begin
  639. sec:=ps.section;
  640. ofs:=ps.address;
  641. reloc:=true;
  642. objectlibrary.UsedAsmSymbolListInsert(ps);
  643. end;
  644. if j<256 then
  645. begin
  646. i:=i+j;
  647. s:=strpas(@p[i]);
  648. if (s<>'') and (s[1]=' ') then
  649. begin
  650. j:=0;
  651. while (s[j+1]=' ') do
  652. inc(j);
  653. i:=i+j;
  654. s:=strpas(@p[i]);
  655. end;
  656. ps:=objectlibrary.getasmsymbol(s);
  657. if not assigned(ps) then
  658. internalerror(33007)
  659. else
  660. begin
  661. if ps.section<>sec then
  662. internalerror(33008);
  663. ofs:=ofs-ps.address;
  664. reloc:=false;
  665. objectlibrary.UsedAsmSymbolListInsert(ps);
  666. end;
  667. end;
  668. end;
  669. end;
  670. { external bss need speical handling (PM) }
  671. if assigned(ps) and (ps.section=sec_none) then
  672. begin
  673. if currpass=2 then
  674. begin
  675. objectdata.writesymbol(ps);
  676. objectoutput.exportsymbol(ps);
  677. end;
  678. objectdata.writeSymStabs(sec,ofs,hp,ps,nidx,nother,line,reloc)
  679. end
  680. else
  681. objectdata.writeStabs(sec,ofs,hp,nidx,nother,line,reloc);
  682. if assigned(hp) then
  683. p[ii]:='"';
  684. end;
  685. procedure TInternalAssembler.emitlineinfostabs(nidx,line : longint);
  686. var
  687. sec : TSection;
  688. begin
  689. if currpass=1 then
  690. begin
  691. objectalloc.staballoc(nil);
  692. exit;
  693. end;
  694. if (nidx=n_textline) and assigned(funcname) and
  695. (target_info.use_function_relative_addresses) then
  696. objectdata.writeStabs(sec_code,objectdata.sectionsize(sec_code)-funcname.address,
  697. nil,nidx,0,line,false)
  698. else
  699. begin
  700. if nidx=n_textline then
  701. sec:=sec_code
  702. else if nidx=n_dataline then
  703. sec:=sec_data
  704. else
  705. sec:=sec_bss;
  706. objectdata.writeStabs(sec,objectdata.sectionsize(sec),
  707. nil,nidx,0,line,true);
  708. end;
  709. end;
  710. procedure TInternalAssembler.emitstabs(s:string);
  711. begin
  712. s:=s+#0;
  713. ConvertStabs(@s[1]);
  714. end;
  715. procedure TInternalAssembler.WriteFileLineInfo(var fileinfo : tfileposinfo);
  716. var
  717. curr_n : byte;
  718. hp : tasmsymbol;
  719. infile : tinputfile;
  720. begin
  721. if not ((cs_debuginfo in aktmoduleswitches) or
  722. (cs_gdb_lineinfo in aktglobalswitches)) then
  723. exit;
  724. { file changed ? (must be before line info) }
  725. if (fileinfo.fileindex<>0) and
  726. (stabslastfileinfo.fileindex<>fileinfo.fileindex) then
  727. begin
  728. infile:=current_module.sourcefiles.get_file(fileinfo.fileindex);
  729. if includecount=0 then
  730. curr_n:=n_sourcefile
  731. else
  732. curr_n:=n_includefile;
  733. { get symbol for this includefile }
  734. hp:=objectlibrary.newasmsymboltype('Ltext'+ToStr(IncludeCount),AB_LOCAL,AT_FUNCTION);
  735. if currpass=1 then
  736. begin
  737. hp.setaddress(currpass,objectalloc.currsec,objectalloc.sectionsize,0);
  738. objectlibrary.UsedAsmSymbolListInsert(hp);
  739. end
  740. else
  741. objectdata.writesymbol(hp);
  742. { emit stabs }
  743. if (infile.path^<>'') then
  744. EmitStabs('"'+lower(BsToSlash(FixPath(infile.path^,false)))+'",'+tostr(curr_n)+
  745. ',0,0,Ltext'+ToStr(IncludeCount));
  746. EmitStabs('"'+lower(FixFileName(infile.name^))+'",'+tostr(curr_n)+
  747. ',0,0,Ltext'+ToStr(IncludeCount));
  748. inc(includecount);
  749. end;
  750. { line changed ? }
  751. if (stabslastfileinfo.line<>fileinfo.line) and (fileinfo.line<>0) then
  752. emitlineinfostabs(n_line,fileinfo.line);
  753. stabslastfileinfo:=fileinfo;
  754. end;
  755. procedure TInternalAssembler.StartFileLineInfo;
  756. var
  757. fileinfo : tfileposinfo;
  758. begin
  759. FillChar(stabslastfileinfo,sizeof(stabslastfileinfo),0);
  760. n_line:=n_textline;
  761. funcname:=nil;
  762. linecount:=1;
  763. includecount:=0;
  764. fileinfo.fileindex:=1;
  765. fileinfo.line:=1;
  766. WriteFileLineInfo(fileinfo);
  767. end;
  768. procedure TInternalAssembler.EndFileLineInfo;
  769. var
  770. hp : tasmsymbol;
  771. store_sec : TSection;
  772. begin
  773. if not ((cs_debuginfo in aktmoduleswitches) or
  774. (cs_gdb_lineinfo in aktglobalswitches)) then
  775. exit;
  776. store_sec:=objectalloc.currsec;
  777. objectalloc.seTSection(sec_code);
  778. hp:=objectlibrary.newasmsymboltype('Letext',AB_LOCAL,AT_FUNCTION);
  779. if currpass=1 then
  780. begin
  781. hp.setaddress(currpass,objectalloc.currsec,objectalloc.sectionsize,0);
  782. objectlibrary.UsedAsmSymbolListInsert(hp);
  783. end
  784. else
  785. objectdata.writesymbol(hp);
  786. EmitStabs('"",'+tostr(n_sourcefile)+',0,0,Letext');
  787. objectalloc.seTSection(store_sec);
  788. end;
  789. {$endif GDB}
  790. function TInternalAssembler.MaybeNextList(var hp:Tai):boolean;
  791. begin
  792. { maybe end of list }
  793. while not assigned(hp) do
  794. begin
  795. if currlistidx<lists then
  796. begin
  797. inc(currlistidx);
  798. currlist:=list[currlistidx];
  799. hp:=Tai(currList.first);
  800. end
  801. else
  802. begin
  803. MaybeNextList:=false;
  804. exit;
  805. end;
  806. end;
  807. MaybeNextList:=true;
  808. end;
  809. function TInternalAssembler.TreePass0(hp:Tai):Tai;
  810. var
  811. l : longint;
  812. begin
  813. while assigned(hp) do
  814. begin
  815. case hp.typ of
  816. ait_align :
  817. begin
  818. { always use the maximum fillsize in this pass to avoid possible
  819. short jumps to become out of range }
  820. Tai_align(hp).fillsize:=Tai_align(hp).aligntype;
  821. objectalloc.sectionalloc(Tai_align(hp).fillsize);
  822. end;
  823. ait_datablock :
  824. begin
  825. if not SmartAsm then
  826. begin
  827. if not Tai_datablock(hp).is_global then
  828. begin
  829. l:=Tai_datablock(hp).size;
  830. if l>2 then
  831. objectalloc.sectionalign(4)
  832. else if l>1 then
  833. objectalloc.sectionalign(2);
  834. objectalloc.sectionalloc(Tai_datablock(hp).size);
  835. end;
  836. end
  837. else
  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. ait_const_32bit :
  848. objectalloc.sectionalloc(4);
  849. ait_const_16bit :
  850. objectalloc.sectionalloc(2);
  851. ait_const_8bit :
  852. objectalloc.sectionalloc(1);
  853. ait_real_80bit :
  854. objectalloc.sectionalloc(10);
  855. ait_real_64bit :
  856. objectalloc.sectionalloc(8);
  857. ait_real_32bit :
  858. objectalloc.sectionalloc(4);
  859. ait_comp_64bit :
  860. objectalloc.sectionalloc(8);
  861. ait_const_rva,
  862. ait_const_symbol :
  863. objectalloc.sectionalloc(4);
  864. ait_section:
  865. objectalloc.seTSection(Tai_section(hp).sec);
  866. ait_symbol :
  867. Tai_symbol(hp).sym.setaddress(currpass,objectalloc.currsec,objectalloc.sectionsize,0);
  868. ait_label :
  869. Tai_label(hp).l.setaddress(currpass,objectalloc.currsec,objectalloc.sectionsize,0);
  870. ait_string :
  871. objectalloc.sectionalloc(Tai_string(hp).len);
  872. ait_instruction :
  873. begin
  874. {$ifdef i386}
  875. {$ifndef NOAG386BIN}
  876. { reset instructions which could change in pass 2 }
  877. Taicpu(hp).resetpass2;
  878. objectalloc.sectionalloc(Taicpu(hp).Pass1(objectalloc.sectionsize));
  879. {$endif NOAG386BIN}
  880. {$endif i386}
  881. end;
  882. ait_cut :
  883. if SmartAsm then
  884. break;
  885. end;
  886. hp:=Tai(hp.next);
  887. end;
  888. TreePass0:=hp;
  889. end;
  890. function TInternalAssembler.TreePass1(hp:Tai):Tai;
  891. var
  892. InlineLevel,
  893. i,l : longint;
  894. begin
  895. inlinelevel:=0;
  896. while assigned(hp) do
  897. begin
  898. {$ifdef GDB}
  899. { write stabs, no line info for inlined code }
  900. if (inlinelevel=0) and
  901. ((cs_debuginfo in aktmoduleswitches) or
  902. (cs_gdb_lineinfo in aktglobalswitches)) then
  903. begin
  904. if (objectalloc.currsec<>sec_none) and
  905. not(hp.typ in [
  906. ait_label,
  907. ait_regalloc,ait_tempalloc,
  908. ait_stabn,ait_stabs,ait_section,
  909. ait_cut,ait_marker,ait_align,ait_stab_function_name]) then
  910. WriteFileLineInfo(hp.fileinfo);
  911. end;
  912. {$endif GDB}
  913. case hp.typ of
  914. ait_align :
  915. begin
  916. { here we must determine the fillsize which is used in pass2 }
  917. Tai_align(hp).fillsize:=align(objectalloc.sectionsize,Tai_align(hp).aligntype)-
  918. objectalloc.sectionsize;
  919. objectalloc.sectionalloc(Tai_align(hp).fillsize);
  920. end;
  921. ait_datablock :
  922. begin
  923. if objectalloc.currsec<>sec_bss then
  924. Message(asmw_e_alloc_data_only_in_bss);
  925. if not SmartAsm then
  926. begin
  927. if Tai_datablock(hp).is_global then
  928. begin
  929. Tai_datablock(hp).sym.setaddress(currpass,sec_none,Tai_datablock(hp).size,Tai_datablock(hp).size);
  930. { force to be common/external, must be after setaddress as that would
  931. set it to AS_GLOBAL }
  932. Tai_datablock(hp).sym.currbind:=AB_COMMON;
  933. end
  934. else
  935. begin
  936. l:=Tai_datablock(hp).size;
  937. if l>2 then
  938. objectalloc.sectionalign(4)
  939. else if l>1 then
  940. objectalloc.sectionalign(2);
  941. Tai_datablock(hp).sym.setaddress(currpass,objectalloc.currsec,objectalloc.sectionsize,
  942. Tai_datablock(hp).size);
  943. objectalloc.sectionalloc(Tai_datablock(hp).size);
  944. end;
  945. end
  946. else
  947. begin
  948. l:=Tai_datablock(hp).size;
  949. if l>2 then
  950. objectalloc.sectionalign(4)
  951. else if l>1 then
  952. objectalloc.sectionalign(2);
  953. Tai_datablock(hp).sym.setaddress(currpass,objectalloc.currsec,objectalloc.sectionsize,Tai_datablock(hp).size);
  954. objectalloc.sectionalloc(Tai_datablock(hp).size);
  955. end;
  956. objectlibrary.UsedAsmSymbolListInsert(Tai_datablock(hp).sym);
  957. end;
  958. ait_const_32bit :
  959. objectalloc.sectionalloc(4);
  960. ait_const_16bit :
  961. objectalloc.sectionalloc(2);
  962. ait_const_8bit :
  963. objectalloc.sectionalloc(1);
  964. ait_real_80bit :
  965. objectalloc.sectionalloc(10);
  966. ait_real_64bit :
  967. objectalloc.sectionalloc(8);
  968. ait_real_32bit :
  969. objectalloc.sectionalloc(4);
  970. ait_comp_64bit :
  971. objectalloc.sectionalloc(8);
  972. ait_const_rva,
  973. ait_const_symbol :
  974. begin
  975. objectalloc.sectionalloc(4);
  976. objectlibrary.UsedAsmSymbolListInsert(Tai_const_symbol(hp).sym);
  977. end;
  978. ait_section:
  979. begin
  980. objectalloc.seTSection(Tai_section(hp).sec);
  981. {$ifdef GDB}
  982. case Tai_section(hp).sec of
  983. sec_code : n_line:=n_textline;
  984. sec_data : n_line:=n_dataline;
  985. sec_bss : n_line:=n_bssline;
  986. else
  987. n_line:=n_dataline;
  988. end;
  989. stabslastfileinfo.line:=-1;
  990. {$endif GDB}
  991. end;
  992. {$ifdef GDB}
  993. ait_stabn :
  994. convertstabs(Tai_stabn(hp).str);
  995. ait_stabs :
  996. convertstabs(Tai_stabs(hp).str);
  997. ait_stab_function_name :
  998. begin
  999. if assigned(Tai_stab_function_name(hp).str) then
  1000. begin
  1001. funcname:=objectlibrary.getasmsymbol(strpas(Tai_stab_function_name(hp).str));
  1002. objectlibrary.UsedAsmSymbolListInsert(funcname);
  1003. end
  1004. else
  1005. funcname:=nil;
  1006. end;
  1007. ait_force_line :
  1008. stabslastfileinfo.line:=0;
  1009. {$endif}
  1010. ait_symbol :
  1011. begin
  1012. Tai_symbol(hp).sym.setaddress(currpass,objectalloc.currsec,objectalloc.sectionsize,0);
  1013. objectlibrary.UsedAsmSymbolListInsert(Tai_symbol(hp).sym);
  1014. end;
  1015. ait_symbol_end :
  1016. begin
  1017. if target_info.system in [system_i386_linux,system_i386_beos] then
  1018. begin
  1019. Tai_symbol_end(hp).sym.size:=objectalloc.sectionsize-Tai_symbol_end(hp).sym.address;
  1020. objectlibrary.UsedAsmSymbolListInsert(Tai_symbol_end(hp).sym);
  1021. end;
  1022. end;
  1023. ait_label :
  1024. begin
  1025. Tai_label(hp).l.setaddress(currpass,objectalloc.currsec,objectalloc.sectionsize,0);
  1026. objectlibrary.UsedAsmSymbolListInsert(Tai_label(hp).l);
  1027. end;
  1028. ait_string :
  1029. objectalloc.sectionalloc(Tai_string(hp).len);
  1030. ait_instruction :
  1031. begin
  1032. {$ifdef i386}
  1033. {$ifndef NOAG386BIN}
  1034. objectalloc.sectionalloc(Taicpu(hp).Pass1(objectalloc.sectionsize));
  1035. { fixup the references }
  1036. for i:=1 to Taicpu(hp).ops do
  1037. begin
  1038. with Taicpu(hp).oper[i-1] do
  1039. begin
  1040. case typ of
  1041. top_ref :
  1042. begin
  1043. if assigned(ref^.symbol) then
  1044. objectlibrary.UsedAsmSymbolListInsert(ref^.symbol);
  1045. end;
  1046. top_symbol :
  1047. begin
  1048. objectlibrary.UsedAsmSymbolListInsert(sym);
  1049. end;
  1050. end;
  1051. end;
  1052. end;
  1053. {$endif NOAG386BIN}
  1054. {$endif i386}
  1055. end;
  1056. ait_direct :
  1057. Message(asmw_f_direct_not_supported);
  1058. ait_cut :
  1059. if SmartAsm then
  1060. break;
  1061. ait_marker :
  1062. if tai_marker(hp).kind=InlineStart then
  1063. inc(InlineLevel)
  1064. else if tai_marker(hp).kind=InlineEnd then
  1065. dec(InlineLevel);
  1066. end;
  1067. hp:=Tai(hp.next);
  1068. end;
  1069. TreePass1:=hp;
  1070. end;
  1071. function TInternalAssembler.TreePass2(hp:Tai):Tai;
  1072. var
  1073. InlineLevel,
  1074. l : longint;
  1075. {$ifdef i386}
  1076. co : comp;
  1077. {$endif i386}
  1078. begin
  1079. inlinelevel:=0;
  1080. { main loop }
  1081. while assigned(hp) do
  1082. begin
  1083. {$ifdef GDB}
  1084. { write stabs, no line info for inlined code }
  1085. if (inlinelevel=0) and
  1086. ((cs_debuginfo in aktmoduleswitches) or
  1087. (cs_gdb_lineinfo in aktglobalswitches)) then
  1088. begin
  1089. if (objectdata.currsec<>sec_none) and
  1090. not(hp.typ in [
  1091. ait_label,
  1092. ait_regalloc,ait_tempalloc,
  1093. ait_stabn,ait_stabs,ait_section,
  1094. ait_cut,ait_marker,ait_align,ait_stab_function_name]) then
  1095. WriteFileLineInfo(hp.fileinfo);
  1096. end;
  1097. {$endif GDB}
  1098. case hp.typ of
  1099. ait_align :
  1100. begin
  1101. if objectdata.currsec=sec_bss then
  1102. objectdata.alloc(Tai_align(hp).fillsize)
  1103. else
  1104. objectdata.writebytes(Tai_align(hp).getfillbuf^,Tai_align(hp).fillsize);
  1105. end;
  1106. ait_section :
  1107. begin
  1108. objectdata.defaulTSection(Tai_section(hp).sec);
  1109. {$ifdef GDB}
  1110. case Tai_section(hp).sec of
  1111. sec_code : n_line:=n_textline;
  1112. sec_data : n_line:=n_dataline;
  1113. sec_bss : n_line:=n_bssline;
  1114. else
  1115. n_line:=n_dataline;
  1116. end;
  1117. stabslastfileinfo.line:=-1;
  1118. {$endif GDB}
  1119. end;
  1120. ait_symbol :
  1121. begin
  1122. objectdata.writesymbol(Tai_symbol(hp).sym);
  1123. objectoutput.exportsymbol(Tai_symbol(hp).sym);
  1124. end;
  1125. ait_datablock :
  1126. begin
  1127. objectdata.writesymbol(Tai_datablock(hp).sym);
  1128. objectoutput.exportsymbol(Tai_datablock(hp).sym);
  1129. if SmartAsm or (not Tai_datablock(hp).is_global) then
  1130. begin
  1131. l:=Tai_datablock(hp).size;
  1132. if l>2 then
  1133. objectdata.allocalign(4)
  1134. else if l>1 then
  1135. objectdata.allocalign(2);
  1136. objectdata.alloc(Tai_datablock(hp).size);
  1137. end;
  1138. end;
  1139. ait_const_32bit :
  1140. objectdata.writebytes(Tai_const(hp).value,4);
  1141. ait_const_16bit :
  1142. objectdata.writebytes(Tai_const(hp).value,2);
  1143. ait_const_8bit :
  1144. objectdata.writebytes(Tai_const(hp).value,1);
  1145. ait_real_80bit :
  1146. objectdata.writebytes(Tai_real_80bit(hp).value,10);
  1147. ait_real_64bit :
  1148. objectdata.writebytes(Tai_real_64bit(hp).value,8);
  1149. ait_real_32bit :
  1150. objectdata.writebytes(Tai_real_32bit(hp).value,4);
  1151. ait_comp_64bit :
  1152. begin
  1153. {$ifdef i386}
  1154. {$ifdef FPC}
  1155. co:=comp(Tai_comp_64bit(hp).value);
  1156. {$else}
  1157. co:=Tai_comp_64bit(hp).value;
  1158. {$endif}
  1159. objectdata.writebytes(co,8);
  1160. {$endif i386}
  1161. end;
  1162. ait_string :
  1163. objectdata.writebytes(Tai_string(hp).str^,Tai_string(hp).len);
  1164. ait_const_rva :
  1165. objectdata.writereloc(Tai_const_symbol(hp).offset,4,
  1166. Tai_const_symbol(hp).sym,RELOC_RVA);
  1167. ait_const_symbol :
  1168. objectdata.writereloc(Tai_const_symbol(hp).offset,4,
  1169. Tai_const_symbol(hp).sym,RELOC_ABSOLUTE);
  1170. ait_label :
  1171. begin
  1172. objectdata.writesymbol(Tai_label(hp).l);
  1173. { exporting shouldn't be necessary as labels are local,
  1174. but it's better to be on the safe side (PFV) }
  1175. objectoutput.exportsymbol(Tai_label(hp).l);
  1176. end;
  1177. {$ifdef i386}
  1178. {$ifndef NOAG386BIN}
  1179. ait_instruction :
  1180. Taicpu(hp).Pass2(objectdata);
  1181. {$endif NOAG386BIN}
  1182. {$endif i386}
  1183. {$ifdef GDB}
  1184. ait_stabn :
  1185. convertstabs(Tai_stabn(hp).str);
  1186. ait_stabs :
  1187. convertstabs(Tai_stabs(hp).str);
  1188. ait_stab_function_name :
  1189. if assigned(Tai_stab_function_name(hp).str) then
  1190. funcname:=objectlibrary.getasmsymbol(strpas(Tai_stab_function_name(hp).str))
  1191. else
  1192. funcname:=nil;
  1193. ait_force_line :
  1194. stabslastfileinfo.line:=0;
  1195. {$endif}
  1196. ait_cut :
  1197. if SmartAsm then
  1198. break;
  1199. ait_marker :
  1200. if tai_marker(hp).kind=InlineStart then
  1201. inc(InlineLevel)
  1202. else if tai_marker(hp).kind=InlineEnd then
  1203. dec(InlineLevel);
  1204. end;
  1205. hp:=Tai(hp.next);
  1206. end;
  1207. TreePass2:=hp;
  1208. end;
  1209. procedure TInternalAssembler.writetree;
  1210. var
  1211. hp : Tai;
  1212. label
  1213. doexit;
  1214. begin
  1215. objectalloc.reseTSections;
  1216. objectalloc.seTSection(sec_code);
  1217. objectdata:=objectoutput.newobjectdata(Objfile);
  1218. objectdata.defaulTSection(sec_code);
  1219. { reset the asmsymbol list }
  1220. objectlibrary.CreateUsedAsmsymbolList;
  1221. {$ifdef MULTIPASS}
  1222. { Pass 0 }
  1223. currpass:=0;
  1224. objectalloc.seTSection(sec_code);
  1225. { start with list 1 }
  1226. currlistidx:=1;
  1227. currlist:=list[currlistidx];
  1228. hp:=Tai(currList.first);
  1229. while assigned(hp) do
  1230. begin
  1231. hp:=TreePass0(hp);
  1232. MaybeNextList(hp);
  1233. end;
  1234. { leave if errors have occured }
  1235. if errorcount>0 then
  1236. goto doexit;
  1237. {$endif}
  1238. { Pass 1 }
  1239. currpass:=1;
  1240. objectalloc.reseTSections;
  1241. objectalloc.seTSection(sec_code);
  1242. {$ifdef GDB}
  1243. StartFileLineInfo;
  1244. {$endif GDB}
  1245. { start with list 1 }
  1246. currlistidx:=1;
  1247. currlist:=list[currlistidx];
  1248. hp:=Tai(currList.first);
  1249. while assigned(hp) do
  1250. begin
  1251. hp:=TreePass1(hp);
  1252. MaybeNextList(hp);
  1253. end;
  1254. {$ifdef GDB}
  1255. EndFileLineInfo;
  1256. {$endif GDB}
  1257. { check for undefined labels and reset }
  1258. objectlibrary.UsedAsmSymbolListCheckUndefined;
  1259. { set section sizes }
  1260. objectdata.seTSectionsizes(objectalloc.secsize);
  1261. { leave if errors have occured }
  1262. if errorcount>0 then
  1263. goto doexit;
  1264. { Pass 2 }
  1265. currpass:=2;
  1266. {$ifdef GDB}
  1267. StartFileLineInfo;
  1268. {$endif GDB}
  1269. { start with list 1 }
  1270. currlistidx:=1;
  1271. currlist:=list[currlistidx];
  1272. hp:=Tai(currList.first);
  1273. while assigned(hp) do
  1274. begin
  1275. hp:=TreePass2(hp);
  1276. MaybeNextList(hp);
  1277. end;
  1278. {$ifdef GDB}
  1279. EndFileLineInfo;
  1280. {$endif GDB}
  1281. { don't write the .o file if errors have occured }
  1282. if errorcount=0 then
  1283. begin
  1284. { write objectfile }
  1285. objectoutput.startobjectfile(ObjFile);
  1286. objectoutput.writeobjectfile(objectdata);
  1287. objectdata.free;
  1288. objectdata:=nil;
  1289. end;
  1290. doexit:
  1291. { reset the used symbols back, must be after the .o has been
  1292. written }
  1293. objectlibrary.UsedAsmsymbolListReset;
  1294. objectlibrary.DestroyUsedAsmsymbolList;
  1295. end;
  1296. procedure TInternalAssembler.writetreesmart;
  1297. var
  1298. hp : Tai;
  1299. starTSec : TSection;
  1300. place: tcutplace;
  1301. begin
  1302. objectalloc.reseTSections;
  1303. objectalloc.seTSection(sec_code);
  1304. NextSmartName(cut_normal);
  1305. objectdata:=objectoutput.newobjectdata(Objfile);
  1306. objectdata.defaulTSection(sec_code);
  1307. starTSec:=sec_code;
  1308. { start with list 1 }
  1309. currlistidx:=1;
  1310. currlist:=list[currlistidx];
  1311. hp:=Tai(currList.first);
  1312. while assigned(hp) do
  1313. begin
  1314. { reset the asmsymbol list }
  1315. objectlibrary.CreateUsedAsmSymbolList;
  1316. {$ifdef MULTIPASS}
  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. {$endif MULTIPASS}
  1326. { Pass 1 }
  1327. currpass:=1;
  1328. objectalloc.reseTSections;
  1329. objectalloc.seTSection(starTSec);
  1330. {$ifdef GDB}
  1331. StartFileLineInfo;
  1332. {$endif GDB}
  1333. TreePass1(hp);
  1334. {$ifdef GDB}
  1335. EndFileLineInfo;
  1336. {$endif GDB}
  1337. { check for undefined labels }
  1338. objectlibrary.UsedAsmSymbolListCheckUndefined;
  1339. { set section sizes }
  1340. objectdata.seTSectionsizes(objectalloc.secsize);
  1341. { leave if errors have occured }
  1342. if errorcount>0 then
  1343. exit;
  1344. { Pass 2 }
  1345. currpass:=2;
  1346. objectoutput.startobjectfile(Objfile);
  1347. objectdata.defaulTSection(starTSec);
  1348. {$ifdef GDB}
  1349. StartFileLineInfo;
  1350. {$endif GDB}
  1351. hp:=TreePass2(hp);
  1352. {$ifdef GDB}
  1353. EndFileLineInfo;
  1354. {$endif GDB}
  1355. { leave if errors have occured }
  1356. if errorcount>0 then
  1357. exit;
  1358. { write the current objectfile }
  1359. objectoutput.writeobjectfile(objectdata);
  1360. objectdata.free;
  1361. objectdata:=nil;
  1362. { reset the used symbols back, must be after the .o has been
  1363. written }
  1364. objectlibrary.UsedAsmsymbolListReset;
  1365. objectlibrary.DestroyUsedAsmsymbolList;
  1366. { end of lists? }
  1367. if not MaybeNextList(hp) then
  1368. break;
  1369. { save section for next loop }
  1370. { this leads to a problem if starTSec is sec_none !! PM }
  1371. starTSec:=objectalloc.currsec;
  1372. { we will start a new objectfile so reset everything }
  1373. { The place can still change in the next while loop, so don't init }
  1374. { the writer yet (JM) }
  1375. if (hp.typ=ait_cut) then
  1376. place := Tai_cut(hp).place
  1377. else
  1378. place := cut_normal;
  1379. { avoid empty files }
  1380. while assigned(hp) and
  1381. (Tai(hp).typ in [ait_marker,ait_comment,ait_section,ait_cut]) do
  1382. begin
  1383. if Tai(hp).typ=ait_section then
  1384. starTSec:=Tai_section(hp).sec
  1385. else if (Tai(hp).typ=ait_cut) then
  1386. place := Tai_cut(hp).place;
  1387. hp:=Tai(hp.next);
  1388. end;
  1389. if not MaybeNextList(hp) then
  1390. break;
  1391. { start next objectfile }
  1392. NextSmartName(place);
  1393. objectdata:=objectoutput.newobjectdata(Objfile);
  1394. { there is a problem if starTSec is sec_none !! PM }
  1395. if starTSec=sec_none then
  1396. starTSec:=sec_code;
  1397. end;
  1398. end;
  1399. procedure TInternalAssembler.MakeObject;
  1400. procedure addlist(p:TAAsmoutput);
  1401. begin
  1402. inc(lists);
  1403. list[lists]:=p;
  1404. end;
  1405. begin
  1406. if cs_debuginfo in aktmoduleswitches then
  1407. addlist(debuglist);
  1408. addlist(codesegment);
  1409. addlist(datasegment);
  1410. addlist(consts);
  1411. addlist(rttilist);
  1412. if assigned(resourcestringlist) then
  1413. addlist(resourcestringlist);
  1414. addlist(bsssegment);
  1415. if assigned(importssection) then
  1416. addlist(importssection);
  1417. if assigned(exportssection) and not UseDeffileForExport then
  1418. addlist(exportssection);
  1419. if assigned(resourcesection) then
  1420. addlist(resourcesection);
  1421. if SmartAsm then
  1422. writetreesmart
  1423. else
  1424. writetree;
  1425. end;
  1426. {*****************************************************************************
  1427. Generate Assembler Files Main Procedure
  1428. *****************************************************************************}
  1429. Procedure GenerateAsm(smart:boolean);
  1430. var
  1431. a : TAssembler;
  1432. begin
  1433. if not assigned(CAssembler[target_asm.id]) then
  1434. Message(asmw_f_assembler_output_not_supported);
  1435. a:=CAssembler[target_asm.id].Create(smart);
  1436. a.MakeObject;
  1437. a.Free;
  1438. end;
  1439. Procedure OnlyAsm;
  1440. var
  1441. a : TExternalAssembler;
  1442. begin
  1443. a:=TExternalAssembler.Create(false);
  1444. a.DoAssemble;
  1445. a.Free;
  1446. end;
  1447. {*****************************************************************************
  1448. Init/Done
  1449. *****************************************************************************}
  1450. procedure RegisterAssembler(const r:tasminfo;c:TAssemblerClass);
  1451. var
  1452. t : tasm;
  1453. begin
  1454. t:=r.id;
  1455. if assigned(asminfos[t]) then
  1456. writeln('Warning: Assembler is already registered!')
  1457. else
  1458. Getmem(asminfos[t],sizeof(tasminfo));
  1459. asminfos[t]^:=r;
  1460. CAssembler[t]:=c;
  1461. end;
  1462. procedure InitAssembler;
  1463. begin
  1464. { target_asm is already set by readarguments }
  1465. initoutputformat:=target_asm.id;
  1466. aktoutputformat:=target_asm.id;
  1467. end;
  1468. procedure DoneAssembler;
  1469. begin
  1470. end;
  1471. end.
  1472. {
  1473. $Log$
  1474. Revision 1.43 2002-08-20 16:55:38 peter
  1475. * don't write (stabs)line info when inlining a procedure
  1476. Revision 1.42 2002/08/12 15:08:39 carl
  1477. + stab register indexes for powerpc (moved from gdb to cpubase)
  1478. + tprocessor enumeration moved to cpuinfo
  1479. + linker in target_info is now a class
  1480. * many many updates for m68k (will soon start to compile)
  1481. - removed some ifdef or correct them for correct cpu
  1482. Revision 1.41 2002/08/11 14:32:26 peter
  1483. * renamed current_library to objectlibrary
  1484. Revision 1.40 2002/08/11 13:24:10 peter
  1485. * saving of asmsymbols in ppu supported
  1486. * asmsymbollist global is removed and moved into a new class
  1487. tasmlibrarydata that will hold the info of a .a file which
  1488. corresponds with a single module. Added librarydata to tmodule
  1489. to keep the library info stored for the module. In the future the
  1490. objectfiles will also be stored to the tasmlibrarydata class
  1491. * all getlabel/newasmsymbol and friends are moved to the new class
  1492. Revision 1.39 2002/07/26 21:15:37 florian
  1493. * rewrote the system handling
  1494. Revision 1.38 2002/07/10 07:24:40 jonas
  1495. * memory leak fixes from Sergey Korshunoff
  1496. Revision 1.37 2002/07/01 18:46:21 peter
  1497. * internal linker
  1498. * reorganized aasm layer
  1499. Revision 1.36 2002/05/18 13:34:05 peter
  1500. * readded missing revisions
  1501. Revision 1.35 2002/05/16 19:46:35 carl
  1502. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  1503. + try to fix temp allocation (still in ifdef)
  1504. + generic constructor calls
  1505. + start of tassembler / tmodulebase class cleanup
  1506. Revision 1.33 2002/04/10 08:07:55 jonas
  1507. * fix for the ie9999 under Linux (patch from Peter)
  1508. Revision 1.32 2002/04/07 13:19:14 carl
  1509. + more documentation
  1510. Revision 1.31 2002/04/04 19:05:54 peter
  1511. * removed unused units
  1512. * use tlocation.size in cg.a_*loc*() routines
  1513. Revision 1.30 2002/04/02 17:11:27 peter
  1514. * tlocation,treference update
  1515. * LOC_CONSTANT added for better constant handling
  1516. * secondadd splitted in multiple routines
  1517. * location_force_reg added for loading a location to a register
  1518. of a specified size
  1519. * secondassignment parses now first the right and then the left node
  1520. (this is compatible with Kylix). This saves a lot of push/pop especially
  1521. with string operations
  1522. * adapted some routines to use the new cg methods
  1523. }