assemble.pas 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654
  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. i,l : longint;
  893. begin
  894. while assigned(hp) do
  895. begin
  896. {$ifdef GDB}
  897. { write stabs }
  898. if ((cs_debuginfo in aktmoduleswitches) or
  899. (cs_gdb_lineinfo in aktglobalswitches)) then
  900. begin
  901. if (objectalloc.currsec<>sec_none) and
  902. not(hp.typ in [
  903. ait_label,
  904. ait_regalloc,ait_tempalloc,
  905. ait_stabn,ait_stabs,ait_section,
  906. ait_cut,ait_marker,ait_align,ait_stab_function_name]) then
  907. WriteFileLineInfo(hp.fileinfo);
  908. end;
  909. {$endif GDB}
  910. case hp.typ of
  911. ait_align :
  912. begin
  913. { here we must determine the fillsize which is used in pass2 }
  914. Tai_align(hp).fillsize:=align(objectalloc.sectionsize,Tai_align(hp).aligntype)-
  915. objectalloc.sectionsize;
  916. objectalloc.sectionalloc(Tai_align(hp).fillsize);
  917. end;
  918. ait_datablock :
  919. begin
  920. if objectalloc.currsec<>sec_bss then
  921. Message(asmw_e_alloc_data_only_in_bss);
  922. if not SmartAsm then
  923. begin
  924. if Tai_datablock(hp).is_global then
  925. begin
  926. Tai_datablock(hp).sym.setaddress(currpass,sec_none,Tai_datablock(hp).size,Tai_datablock(hp).size);
  927. { force to be common/external, must be after setaddress as that would
  928. set it to AS_GLOBAL }
  929. Tai_datablock(hp).sym.currbind:=AB_COMMON;
  930. end
  931. else
  932. begin
  933. l:=Tai_datablock(hp).size;
  934. if l>2 then
  935. objectalloc.sectionalign(4)
  936. else if l>1 then
  937. objectalloc.sectionalign(2);
  938. Tai_datablock(hp).sym.setaddress(currpass,objectalloc.currsec,objectalloc.sectionsize,
  939. Tai_datablock(hp).size);
  940. objectalloc.sectionalloc(Tai_datablock(hp).size);
  941. end;
  942. end
  943. else
  944. begin
  945. l:=Tai_datablock(hp).size;
  946. if l>2 then
  947. objectalloc.sectionalign(4)
  948. else if l>1 then
  949. objectalloc.sectionalign(2);
  950. Tai_datablock(hp).sym.setaddress(currpass,objectalloc.currsec,objectalloc.sectionsize,Tai_datablock(hp).size);
  951. objectalloc.sectionalloc(Tai_datablock(hp).size);
  952. end;
  953. objectlibrary.UsedAsmSymbolListInsert(Tai_datablock(hp).sym);
  954. end;
  955. ait_const_32bit :
  956. objectalloc.sectionalloc(4);
  957. ait_const_16bit :
  958. objectalloc.sectionalloc(2);
  959. ait_const_8bit :
  960. objectalloc.sectionalloc(1);
  961. ait_real_80bit :
  962. objectalloc.sectionalloc(10);
  963. ait_real_64bit :
  964. objectalloc.sectionalloc(8);
  965. ait_real_32bit :
  966. objectalloc.sectionalloc(4);
  967. ait_comp_64bit :
  968. objectalloc.sectionalloc(8);
  969. ait_const_rva,
  970. ait_const_symbol :
  971. begin
  972. objectalloc.sectionalloc(4);
  973. objectlibrary.UsedAsmSymbolListInsert(Tai_const_symbol(hp).sym);
  974. end;
  975. ait_section:
  976. begin
  977. objectalloc.seTSection(Tai_section(hp).sec);
  978. {$ifdef GDB}
  979. case Tai_section(hp).sec of
  980. sec_code : n_line:=n_textline;
  981. sec_data : n_line:=n_dataline;
  982. sec_bss : n_line:=n_bssline;
  983. else
  984. n_line:=n_dataline;
  985. end;
  986. stabslastfileinfo.line:=-1;
  987. {$endif GDB}
  988. end;
  989. {$ifdef GDB}
  990. ait_stabn :
  991. convertstabs(Tai_stabn(hp).str);
  992. ait_stabs :
  993. convertstabs(Tai_stabs(hp).str);
  994. ait_stab_function_name :
  995. begin
  996. if assigned(Tai_stab_function_name(hp).str) then
  997. begin
  998. funcname:=objectlibrary.getasmsymbol(strpas(Tai_stab_function_name(hp).str));
  999. objectlibrary.UsedAsmSymbolListInsert(funcname);
  1000. end
  1001. else
  1002. funcname:=nil;
  1003. end;
  1004. ait_force_line :
  1005. stabslastfileinfo.line:=0;
  1006. {$endif}
  1007. ait_symbol :
  1008. begin
  1009. Tai_symbol(hp).sym.setaddress(currpass,objectalloc.currsec,objectalloc.sectionsize,0);
  1010. objectlibrary.UsedAsmSymbolListInsert(Tai_symbol(hp).sym);
  1011. end;
  1012. ait_symbol_end :
  1013. begin
  1014. if target_info.system in [system_i386_linux,system_i386_beos] then
  1015. begin
  1016. Tai_symbol_end(hp).sym.size:=objectalloc.sectionsize-Tai_symbol_end(hp).sym.address;
  1017. objectlibrary.UsedAsmSymbolListInsert(Tai_symbol_end(hp).sym);
  1018. end;
  1019. end;
  1020. ait_label :
  1021. begin
  1022. Tai_label(hp).l.setaddress(currpass,objectalloc.currsec,objectalloc.sectionsize,0);
  1023. objectlibrary.UsedAsmSymbolListInsert(Tai_label(hp).l);
  1024. end;
  1025. ait_string :
  1026. objectalloc.sectionalloc(Tai_string(hp).len);
  1027. ait_instruction :
  1028. begin
  1029. {$ifdef i386}
  1030. {$ifndef NOAG386BIN}
  1031. objectalloc.sectionalloc(Taicpu(hp).Pass1(objectalloc.sectionsize));
  1032. { fixup the references }
  1033. for i:=1 to Taicpu(hp).ops do
  1034. begin
  1035. with Taicpu(hp).oper[i-1] do
  1036. begin
  1037. case typ of
  1038. top_ref :
  1039. begin
  1040. if assigned(ref^.symbol) then
  1041. objectlibrary.UsedAsmSymbolListInsert(ref^.symbol);
  1042. end;
  1043. top_symbol :
  1044. begin
  1045. objectlibrary.UsedAsmSymbolListInsert(sym);
  1046. end;
  1047. end;
  1048. end;
  1049. end;
  1050. {$endif NOAG386BIN}
  1051. {$endif i386}
  1052. end;
  1053. ait_direct :
  1054. Message(asmw_f_direct_not_supported);
  1055. ait_cut :
  1056. if SmartAsm then
  1057. break;
  1058. end;
  1059. hp:=Tai(hp.next);
  1060. end;
  1061. TreePass1:=hp;
  1062. end;
  1063. function TInternalAssembler.TreePass2(hp:Tai):Tai;
  1064. var
  1065. l : longint;
  1066. {$ifdef i386}
  1067. co : comp;
  1068. {$endif i386}
  1069. begin
  1070. { main loop }
  1071. while assigned(hp) do
  1072. begin
  1073. {$ifdef GDB}
  1074. { write stabs }
  1075. if ((cs_debuginfo in aktmoduleswitches) or
  1076. (cs_gdb_lineinfo in aktglobalswitches)) then
  1077. begin
  1078. if (objectdata.currsec<>sec_none) and
  1079. not(hp.typ in [
  1080. ait_label,
  1081. ait_regalloc,ait_tempalloc,
  1082. ait_stabn,ait_stabs,ait_section,
  1083. ait_cut,ait_marker,ait_align,ait_stab_function_name]) then
  1084. WriteFileLineInfo(hp.fileinfo);
  1085. end;
  1086. {$endif GDB}
  1087. case hp.typ of
  1088. ait_align :
  1089. begin
  1090. if objectdata.currsec=sec_bss then
  1091. objectdata.alloc(Tai_align(hp).fillsize)
  1092. else
  1093. objectdata.writebytes(Tai_align(hp).getfillbuf^,Tai_align(hp).fillsize);
  1094. end;
  1095. ait_section :
  1096. begin
  1097. objectdata.defaulTSection(Tai_section(hp).sec);
  1098. {$ifdef GDB}
  1099. case Tai_section(hp).sec of
  1100. sec_code : n_line:=n_textline;
  1101. sec_data : n_line:=n_dataline;
  1102. sec_bss : n_line:=n_bssline;
  1103. else
  1104. n_line:=n_dataline;
  1105. end;
  1106. stabslastfileinfo.line:=-1;
  1107. {$endif GDB}
  1108. end;
  1109. ait_symbol :
  1110. begin
  1111. objectdata.writesymbol(Tai_symbol(hp).sym);
  1112. objectoutput.exportsymbol(Tai_symbol(hp).sym);
  1113. end;
  1114. ait_datablock :
  1115. begin
  1116. objectdata.writesymbol(Tai_datablock(hp).sym);
  1117. objectoutput.exportsymbol(Tai_datablock(hp).sym);
  1118. if SmartAsm or (not Tai_datablock(hp).is_global) then
  1119. begin
  1120. l:=Tai_datablock(hp).size;
  1121. if l>2 then
  1122. objectdata.allocalign(4)
  1123. else if l>1 then
  1124. objectdata.allocalign(2);
  1125. objectdata.alloc(Tai_datablock(hp).size);
  1126. end;
  1127. end;
  1128. ait_const_32bit :
  1129. objectdata.writebytes(Tai_const(hp).value,4);
  1130. ait_const_16bit :
  1131. objectdata.writebytes(Tai_const(hp).value,2);
  1132. ait_const_8bit :
  1133. objectdata.writebytes(Tai_const(hp).value,1);
  1134. ait_real_80bit :
  1135. objectdata.writebytes(Tai_real_80bit(hp).value,10);
  1136. ait_real_64bit :
  1137. objectdata.writebytes(Tai_real_64bit(hp).value,8);
  1138. ait_real_32bit :
  1139. objectdata.writebytes(Tai_real_32bit(hp).value,4);
  1140. ait_comp_64bit :
  1141. begin
  1142. {$ifdef i386}
  1143. {$ifdef FPC}
  1144. co:=comp(Tai_comp_64bit(hp).value);
  1145. {$else}
  1146. co:=Tai_comp_64bit(hp).value;
  1147. {$endif}
  1148. objectdata.writebytes(co,8);
  1149. {$endif i386}
  1150. end;
  1151. ait_string :
  1152. objectdata.writebytes(Tai_string(hp).str^,Tai_string(hp).len);
  1153. ait_const_rva :
  1154. objectdata.writereloc(Tai_const_symbol(hp).offset,4,
  1155. Tai_const_symbol(hp).sym,RELOC_RVA);
  1156. ait_const_symbol :
  1157. objectdata.writereloc(Tai_const_symbol(hp).offset,4,
  1158. Tai_const_symbol(hp).sym,RELOC_ABSOLUTE);
  1159. ait_label :
  1160. begin
  1161. objectdata.writesymbol(Tai_label(hp).l);
  1162. { exporting shouldn't be necessary as labels are local,
  1163. but it's better to be on the safe side (PFV) }
  1164. objectoutput.exportsymbol(Tai_label(hp).l);
  1165. end;
  1166. {$ifdef i386}
  1167. {$ifndef NOAG386BIN}
  1168. ait_instruction :
  1169. Taicpu(hp).Pass2(objectdata);
  1170. {$endif NOAG386BIN}
  1171. {$endif i386}
  1172. {$ifdef GDB}
  1173. ait_stabn :
  1174. convertstabs(Tai_stabn(hp).str);
  1175. ait_stabs :
  1176. convertstabs(Tai_stabs(hp).str);
  1177. ait_stab_function_name :
  1178. if assigned(Tai_stab_function_name(hp).str) then
  1179. funcname:=objectlibrary.getasmsymbol(strpas(Tai_stab_function_name(hp).str))
  1180. else
  1181. funcname:=nil;
  1182. ait_force_line :
  1183. stabslastfileinfo.line:=0;
  1184. {$endif}
  1185. ait_cut :
  1186. if SmartAsm then
  1187. break;
  1188. end;
  1189. hp:=Tai(hp.next);
  1190. end;
  1191. TreePass2:=hp;
  1192. end;
  1193. procedure TInternalAssembler.writetree;
  1194. var
  1195. hp : Tai;
  1196. label
  1197. doexit;
  1198. begin
  1199. objectalloc.reseTSections;
  1200. objectalloc.seTSection(sec_code);
  1201. objectdata:=objectoutput.newobjectdata(Objfile);
  1202. objectdata.defaulTSection(sec_code);
  1203. { reset the asmsymbol list }
  1204. objectlibrary.CreateUsedAsmsymbolList;
  1205. {$ifdef MULTIPASS}
  1206. { Pass 0 }
  1207. currpass:=0;
  1208. objectalloc.seTSection(sec_code);
  1209. { start with list 1 }
  1210. currlistidx:=1;
  1211. currlist:=list[currlistidx];
  1212. hp:=Tai(currList.first);
  1213. while assigned(hp) do
  1214. begin
  1215. hp:=TreePass0(hp);
  1216. MaybeNextList(hp);
  1217. end;
  1218. { leave if errors have occured }
  1219. if errorcount>0 then
  1220. goto doexit;
  1221. {$endif}
  1222. { Pass 1 }
  1223. currpass:=1;
  1224. objectalloc.reseTSections;
  1225. objectalloc.seTSection(sec_code);
  1226. {$ifdef GDB}
  1227. StartFileLineInfo;
  1228. {$endif GDB}
  1229. { start with list 1 }
  1230. currlistidx:=1;
  1231. currlist:=list[currlistidx];
  1232. hp:=Tai(currList.first);
  1233. while assigned(hp) do
  1234. begin
  1235. hp:=TreePass1(hp);
  1236. MaybeNextList(hp);
  1237. end;
  1238. {$ifdef GDB}
  1239. EndFileLineInfo;
  1240. {$endif GDB}
  1241. { check for undefined labels and reset }
  1242. objectlibrary.UsedAsmSymbolListCheckUndefined;
  1243. { set section sizes }
  1244. objectdata.seTSectionsizes(objectalloc.secsize);
  1245. { leave if errors have occured }
  1246. if errorcount>0 then
  1247. goto doexit;
  1248. { Pass 2 }
  1249. currpass:=2;
  1250. {$ifdef GDB}
  1251. StartFileLineInfo;
  1252. {$endif GDB}
  1253. { start with list 1 }
  1254. currlistidx:=1;
  1255. currlist:=list[currlistidx];
  1256. hp:=Tai(currList.first);
  1257. while assigned(hp) do
  1258. begin
  1259. hp:=TreePass2(hp);
  1260. MaybeNextList(hp);
  1261. end;
  1262. {$ifdef GDB}
  1263. EndFileLineInfo;
  1264. {$endif GDB}
  1265. { don't write the .o file if errors have occured }
  1266. if errorcount=0 then
  1267. begin
  1268. { write objectfile }
  1269. objectoutput.startobjectfile(ObjFile);
  1270. objectoutput.writeobjectfile(objectdata);
  1271. objectdata.free;
  1272. objectdata:=nil;
  1273. end;
  1274. doexit:
  1275. { reset the used symbols back, must be after the .o has been
  1276. written }
  1277. objectlibrary.UsedAsmsymbolListReset;
  1278. objectlibrary.DestroyUsedAsmsymbolList;
  1279. end;
  1280. procedure TInternalAssembler.writetreesmart;
  1281. var
  1282. hp : Tai;
  1283. starTSec : TSection;
  1284. place: tcutplace;
  1285. begin
  1286. objectalloc.reseTSections;
  1287. objectalloc.seTSection(sec_code);
  1288. NextSmartName(cut_normal);
  1289. objectdata:=objectoutput.newobjectdata(Objfile);
  1290. objectdata.defaulTSection(sec_code);
  1291. starTSec:=sec_code;
  1292. { start with list 1 }
  1293. currlistidx:=1;
  1294. currlist:=list[currlistidx];
  1295. hp:=Tai(currList.first);
  1296. while assigned(hp) do
  1297. begin
  1298. { reset the asmsymbol list }
  1299. objectlibrary.CreateUsedAsmSymbolList;
  1300. {$ifdef MULTIPASS}
  1301. { Pass 0 }
  1302. currpass:=0;
  1303. objectalloc.reseTSections;
  1304. objectalloc.seTSection(starTSec);
  1305. TreePass0(hp);
  1306. { leave if errors have occured }
  1307. if errorcount>0 then
  1308. exit;
  1309. {$endif MULTIPASS}
  1310. { Pass 1 }
  1311. currpass:=1;
  1312. objectalloc.reseTSections;
  1313. objectalloc.seTSection(starTSec);
  1314. {$ifdef GDB}
  1315. StartFileLineInfo;
  1316. {$endif GDB}
  1317. TreePass1(hp);
  1318. {$ifdef GDB}
  1319. EndFileLineInfo;
  1320. {$endif GDB}
  1321. { check for undefined labels }
  1322. objectlibrary.UsedAsmSymbolListCheckUndefined;
  1323. { set section sizes }
  1324. objectdata.seTSectionsizes(objectalloc.secsize);
  1325. { leave if errors have occured }
  1326. if errorcount>0 then
  1327. exit;
  1328. { Pass 2 }
  1329. currpass:=2;
  1330. objectoutput.startobjectfile(Objfile);
  1331. objectdata.defaulTSection(starTSec);
  1332. {$ifdef GDB}
  1333. StartFileLineInfo;
  1334. {$endif GDB}
  1335. hp:=TreePass2(hp);
  1336. {$ifdef GDB}
  1337. EndFileLineInfo;
  1338. {$endif GDB}
  1339. { leave if errors have occured }
  1340. if errorcount>0 then
  1341. exit;
  1342. { write the current objectfile }
  1343. objectoutput.writeobjectfile(objectdata);
  1344. objectdata.free;
  1345. objectdata:=nil;
  1346. { reset the used symbols back, must be after the .o has been
  1347. written }
  1348. objectlibrary.UsedAsmsymbolListReset;
  1349. objectlibrary.DestroyUsedAsmsymbolList;
  1350. { end of lists? }
  1351. if not MaybeNextList(hp) then
  1352. break;
  1353. { save section for next loop }
  1354. { this leads to a problem if starTSec is sec_none !! PM }
  1355. starTSec:=objectalloc.currsec;
  1356. { we will start a new objectfile so reset everything }
  1357. { The place can still change in the next while loop, so don't init }
  1358. { the writer yet (JM) }
  1359. if (hp.typ=ait_cut) then
  1360. place := Tai_cut(hp).place
  1361. else
  1362. place := cut_normal;
  1363. { avoid empty files }
  1364. while assigned(hp) and
  1365. (Tai(hp).typ in [ait_marker,ait_comment,ait_section,ait_cut]) do
  1366. begin
  1367. if Tai(hp).typ=ait_section then
  1368. starTSec:=Tai_section(hp).sec
  1369. else if (Tai(hp).typ=ait_cut) then
  1370. place := Tai_cut(hp).place;
  1371. hp:=Tai(hp.next);
  1372. end;
  1373. if not MaybeNextList(hp) then
  1374. break;
  1375. { start next objectfile }
  1376. NextSmartName(place);
  1377. objectdata:=objectoutput.newobjectdata(Objfile);
  1378. { there is a problem if starTSec is sec_none !! PM }
  1379. if starTSec=sec_none then
  1380. starTSec:=sec_code;
  1381. end;
  1382. end;
  1383. procedure TInternalAssembler.MakeObject;
  1384. procedure addlist(p:TAAsmoutput);
  1385. begin
  1386. inc(lists);
  1387. list[lists]:=p;
  1388. end;
  1389. begin
  1390. if cs_debuginfo in aktmoduleswitches then
  1391. addlist(debuglist);
  1392. addlist(codesegment);
  1393. addlist(datasegment);
  1394. addlist(consts);
  1395. addlist(rttilist);
  1396. if assigned(resourcestringlist) then
  1397. addlist(resourcestringlist);
  1398. addlist(bsssegment);
  1399. if assigned(importssection) then
  1400. addlist(importssection);
  1401. if assigned(exportssection) and not UseDeffileForExport then
  1402. addlist(exportssection);
  1403. if assigned(resourcesection) then
  1404. addlist(resourcesection);
  1405. if SmartAsm then
  1406. writetreesmart
  1407. else
  1408. writetree;
  1409. end;
  1410. {*****************************************************************************
  1411. Generate Assembler Files Main Procedure
  1412. *****************************************************************************}
  1413. Procedure GenerateAsm(smart:boolean);
  1414. var
  1415. a : TAssembler;
  1416. begin
  1417. if not assigned(CAssembler[target_asm.id]) then
  1418. Message(asmw_f_assembler_output_not_supported);
  1419. a:=CAssembler[target_asm.id].Create(smart);
  1420. a.MakeObject;
  1421. a.Free;
  1422. end;
  1423. Procedure OnlyAsm;
  1424. var
  1425. a : TExternalAssembler;
  1426. begin
  1427. a:=TExternalAssembler.Create(false);
  1428. a.DoAssemble;
  1429. a.Free;
  1430. end;
  1431. {*****************************************************************************
  1432. Init/Done
  1433. *****************************************************************************}
  1434. procedure RegisterAssembler(const r:tasminfo;c:TAssemblerClass);
  1435. var
  1436. t : tasm;
  1437. begin
  1438. t:=r.id;
  1439. if assigned(asminfos[t]) then
  1440. writeln('Warning: Assembler is already registered!')
  1441. else
  1442. Getmem(asminfos[t],sizeof(tasminfo));
  1443. asminfos[t]^:=r;
  1444. CAssembler[t]:=c;
  1445. end;
  1446. procedure InitAssembler;
  1447. begin
  1448. { target_asm is already set by readarguments }
  1449. initoutputformat:=target_asm.id;
  1450. aktoutputformat:=target_asm.id;
  1451. end;
  1452. procedure DoneAssembler;
  1453. begin
  1454. end;
  1455. end.
  1456. {
  1457. $Log$
  1458. Revision 1.42 2002-08-12 15:08:39 carl
  1459. + stab register indexes for powerpc (moved from gdb to cpubase)
  1460. + tprocessor enumeration moved to cpuinfo
  1461. + linker in target_info is now a class
  1462. * many many updates for m68k (will soon start to compile)
  1463. - removed some ifdef or correct them for correct cpu
  1464. Revision 1.41 2002/08/11 14:32:26 peter
  1465. * renamed current_library to objectlibrary
  1466. Revision 1.40 2002/08/11 13:24:10 peter
  1467. * saving of asmsymbols in ppu supported
  1468. * asmsymbollist global is removed and moved into a new class
  1469. tasmlibrarydata that will hold the info of a .a file which
  1470. corresponds with a single module. Added librarydata to tmodule
  1471. to keep the library info stored for the module. In the future the
  1472. objectfiles will also be stored to the tasmlibrarydata class
  1473. * all getlabel/newasmsymbol and friends are moved to the new class
  1474. Revision 1.39 2002/07/26 21:15:37 florian
  1475. * rewrote the system handling
  1476. Revision 1.38 2002/07/10 07:24:40 jonas
  1477. * memory leak fixes from Sergey Korshunoff
  1478. Revision 1.37 2002/07/01 18:46:21 peter
  1479. * internal linker
  1480. * reorganized aasm layer
  1481. Revision 1.36 2002/05/18 13:34:05 peter
  1482. * readded missing revisions
  1483. Revision 1.35 2002/05/16 19:46:35 carl
  1484. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  1485. + try to fix temp allocation (still in ifdef)
  1486. + generic constructor calls
  1487. + start of tassembler / tmodulebase class cleanup
  1488. Revision 1.33 2002/04/10 08:07:55 jonas
  1489. * fix for the ie9999 under Linux (patch from Peter)
  1490. Revision 1.32 2002/04/07 13:19:14 carl
  1491. + more documentation
  1492. Revision 1.31 2002/04/04 19:05:54 peter
  1493. * removed unused units
  1494. * use tlocation.size in cg.a_*loc*() routines
  1495. Revision 1.30 2002/04/02 17:11:27 peter
  1496. * tlocation,treference update
  1497. * LOC_CONSTANT added for better constant handling
  1498. * secondadd splitted in multiple routines
  1499. * location_force_reg added for loading a location to a register
  1500. of a specified size
  1501. * secondassignment parses now first the right and then the left node
  1502. (this is compatible with Kylix). This saves a lot of push/pop especially
  1503. with string operations
  1504. * adapted some routines to use the new cg methods
  1505. }