assemble.pas 50 KB

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