assemble.pas 47 KB

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