assemble.pas 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497
  1. {
  2. Copyright (c) 1998-2004 by Peter Vreman
  3. This unit handles the assemblerfile write and assembler calls of FPC
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. {# @abstract(This unit handles the assembler file write and assembler calls of FPC)
  18. Handles the calls to the actual external assemblers, as well as the generation
  19. of object files for smart linking. Also contains the base class for writing
  20. the assembler statements to file.
  21. }
  22. unit assemble;
  23. {$i fpcdefs.inc}
  24. interface
  25. uses
  26. SysUtils,
  27. systems,globtype,globals,aasmbase,aasmtai,aasmdata,ogbase,finput;
  28. const
  29. { maximum of aasmoutput lists there will be }
  30. maxoutputlists = 20;
  31. { buffer size for writing the .s file }
  32. AsmOutSize=32768*4;
  33. type
  34. TAssembler=class(TAbstractAssembler)
  35. public
  36. {filenames}
  37. path : string;
  38. name : string;
  39. AsmFileName, { current .s and .o file }
  40. ObjFileName,
  41. ppufilename : string;
  42. asmprefix : string;
  43. SmartAsm : boolean;
  44. SmartFilesCount,
  45. SmartHeaderCount : longint;
  46. Constructor Create(smart:boolean);virtual;
  47. Destructor Destroy;override;
  48. procedure NextSmartName(place:tcutplace);
  49. procedure MakeObject;virtual;abstract;
  50. end;
  51. {# This is the base class which should be overriden for each each
  52. assembler writer. It is used to actually assembler a file,
  53. and write the output to the assembler file.
  54. }
  55. TExternalAssembler=class(TAssembler)
  56. private
  57. procedure CreateSmartLinkPath(const s:string);
  58. protected
  59. {outfile}
  60. AsmSize,
  61. AsmStartSize,
  62. outcnt : longint;
  63. outbuf : array[0..AsmOutSize-1] of char;
  64. outfile : file;
  65. ioerror : boolean;
  66. {input source info}
  67. lastfileinfo : tfileposinfo;
  68. infile,
  69. lastinfile : tinputfile;
  70. {last section type written}
  71. lastsectype : TAsmSectionType;
  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:string; const para:TCmdStr):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:TAsmList);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. {# Constructs the command line for calling the assembler }
  105. function MakeCmdLine: TCmdStr; virtual;
  106. public
  107. Constructor Create(smart:boolean);override;
  108. procedure MakeObject;override;
  109. end;
  110. TInternalAssembler=class(TAssembler)
  111. private
  112. FCObjOutput : TObjOutputclass;
  113. { the aasmoutput lists that need to be processed }
  114. lists : byte;
  115. list : array[1..maxoutputlists] of TAsmList;
  116. { current processing }
  117. currlistidx : byte;
  118. currlist : TAsmList;
  119. procedure WriteStab(p:pchar);
  120. function MaybeNextList(var hp:Tai):boolean;
  121. function TreePass0(hp:Tai):Tai;
  122. function TreePass1(hp:Tai):Tai;
  123. function TreePass2(hp:Tai):Tai;
  124. procedure writetree;
  125. procedure writetreesmart;
  126. protected
  127. ObjData : TObjData;
  128. ObjOutput : tObjOutput;
  129. property CObjOutput:TObjOutputclass read FCObjOutput write FCObjOutput;
  130. public
  131. constructor create(smart:boolean);override;
  132. destructor destroy;override;
  133. procedure MakeObject;override;
  134. end;
  135. TAssemblerClass = class of TAssembler;
  136. Procedure GenerateAsm(smart:boolean);
  137. Procedure OnlyAsm;
  138. procedure RegisterAssembler(const r:tasminfo;c:TAssemblerClass);
  139. Implementation
  140. uses
  141. {$ifdef hasunix}
  142. unix,
  143. {$endif}
  144. cutils,cfileutl,
  145. {$ifdef memdebug}
  146. cclasses,
  147. {$endif memdebug}
  148. script,fmodule,verbose,
  149. {$if defined(m68k) or defined(arm)}
  150. cpuinfo,
  151. {$endif m68k or arm}
  152. aasmcpu,
  153. owbase,owar
  154. ;
  155. var
  156. CAssembler : array[tasm] of TAssemblerClass;
  157. {*****************************************************************************
  158. TAssembler
  159. *****************************************************************************}
  160. Constructor TAssembler.Create(smart:boolean);
  161. begin
  162. { load start values }
  163. AsmFileName:=current_module.AsmFilename^;
  164. ObjFileName:=current_module.ObjFileName^;
  165. name:=Lower(current_module.modulename^);
  166. path:=current_module.outputpath^;
  167. asmprefix := current_module.asmprefix^;
  168. if not assigned(current_module.outputpath) then
  169. ppufilename := ''
  170. else
  171. ppufilename := current_module.ppufilename^;
  172. SmartAsm:=smart;
  173. SmartFilesCount:=0;
  174. SmartHeaderCount:=0;
  175. SmartLinkOFiles.Clear;
  176. end;
  177. Destructor TAssembler.Destroy;
  178. begin
  179. end;
  180. procedure TAssembler.NextSmartName(place:tcutplace);
  181. var
  182. s : string;
  183. begin
  184. inc(SmartFilesCount);
  185. if SmartFilesCount>999999 then
  186. Message(asmw_f_too_many_asm_files);
  187. case place of
  188. cut_begin :
  189. begin
  190. inc(SmartHeaderCount);
  191. s:=asmprefix+tostr(SmartHeaderCount)+'h';
  192. end;
  193. cut_normal :
  194. s:=asmprefix+tostr(SmartHeaderCount)+'s';
  195. cut_end :
  196. s:=asmprefix+tostr(SmartHeaderCount)+'t';
  197. end;
  198. AsmFileName:=Path+FixFileName(s+tostr(SmartFilesCount)+target_info.asmext);
  199. ObjFileName:=Path+FixFileName(s+tostr(SmartFilesCount)+target_info.objext);
  200. { insert in container so it can be cleared after the linking }
  201. SmartLinkOFiles.Insert(ObjFileName);
  202. end;
  203. {*****************************************************************************
  204. TExternalAssembler
  205. *****************************************************************************}
  206. Function DoPipe:boolean;
  207. begin
  208. DoPipe:=(cs_asm_pipe in current_settings.globalswitches) and
  209. (([cs_asm_leave,cs_link_on_target] * current_settings.globalswitches) = []) and
  210. ((target_asm.id in [as_gas,as_ggas,as_darwin]));
  211. end;
  212. Constructor TExternalAssembler.Create(smart:boolean);
  213. begin
  214. inherited Create(smart);
  215. if SmartAsm then
  216. begin
  217. path:=FixPath(ChangeFileExt(AsmFileName,target_info.smartext),false);
  218. CreateSmartLinkPath(path);
  219. end;
  220. Outcnt:=0;
  221. end;
  222. procedure TExternalAssembler.CreateSmartLinkPath(const s:string);
  223. procedure DeleteFilesWithExt(const AExt:string);
  224. var
  225. dir : TSearchRec;
  226. begin
  227. if findfirst(s+source_info.dirsep+'*'+AExt,faAnyFile,dir) = 0 then
  228. begin
  229. repeat
  230. DeleteFile(s+source_info.dirsep+dir.name);
  231. until findnext(dir) <> 0;
  232. end;
  233. findclose(dir);
  234. end;
  235. var
  236. hs : string;
  237. begin
  238. if PathExists(s,false) then
  239. begin
  240. { the path exists, now we clean only all the .o and .s files }
  241. DeleteFilesWithExt(target_info.objext);
  242. DeleteFilesWithExt(target_info.asmext);
  243. end
  244. else
  245. begin
  246. hs:=s;
  247. if hs[length(hs)] in ['/','\'] then
  248. delete(hs,length(hs),1);
  249. {$I-}
  250. mkdir(hs);
  251. {$I+}
  252. if ioresult<>0 then;
  253. end;
  254. end;
  255. const
  256. lastas : byte=255;
  257. var
  258. LastASBin : TCmdStr;
  259. Function TExternalAssembler.FindAssembler:string;
  260. var
  261. asfound : boolean;
  262. UtilExe : string;
  263. begin
  264. asfound:=false;
  265. if cs_link_on_target in current_settings.globalswitches then
  266. begin
  267. { If linking on target, don't add any path PM }
  268. FindAssembler:=utilsprefix+ChangeFileExt(target_asm.asmbin,target_info.exeext);
  269. exit;
  270. end
  271. else
  272. UtilExe:=utilsprefix+ChangeFileExt(target_asm.asmbin,source_info.exeext);
  273. if lastas<>ord(target_asm.id) then
  274. begin
  275. lastas:=ord(target_asm.id);
  276. { is an assembler passed ? }
  277. if utilsdirectory<>'' then
  278. asfound:=FindFile(UtilExe,utilsdirectory,false,LastASBin);
  279. if not AsFound then
  280. asfound:=FindExe(UtilExe,false,LastASBin);
  281. if (not asfound) and not(cs_asm_extern in current_settings.globalswitches) then
  282. begin
  283. Message1(exec_e_assembler_not_found,LastASBin);
  284. current_settings.globalswitches:=current_settings.globalswitches+[cs_asm_extern];
  285. end;
  286. if asfound then
  287. Message1(exec_t_using_assembler,LastASBin);
  288. end;
  289. FindAssembler:=LastASBin;
  290. end;
  291. Function TExternalAssembler.CallAssembler(const command:string; const para:TCmdStr):Boolean;
  292. var
  293. DosExitCode : Integer;
  294. begin
  295. result:=true;
  296. if (cs_asm_extern in current_settings.globalswitches) then
  297. begin
  298. AsmRes.AddAsmCommand(command,para,name);
  299. exit;
  300. end;
  301. try
  302. FlushOutput;
  303. DosExitCode := ExecuteProcess(command,para);
  304. if DosExitCode <>0
  305. then begin
  306. Message1(exec_e_error_while_assembling,tostr(dosexitcode));
  307. result:=false;
  308. end;
  309. except on E:EOSError do
  310. begin
  311. Message1(exec_e_cant_call_assembler,tostr(E.ErrorCode));
  312. current_settings.globalswitches:=current_settings.globalswitches+[cs_asm_extern];
  313. result:=false;
  314. end;
  315. end;
  316. end;
  317. procedure TExternalAssembler.RemoveAsm;
  318. var
  319. g : file;
  320. begin
  321. if cs_asm_leave in current_settings.globalswitches then
  322. exit;
  323. if cs_asm_extern in current_settings.globalswitches then
  324. AsmRes.AddDeleteCommand(AsmFileName)
  325. else
  326. begin
  327. assign(g,AsmFileName);
  328. {$I-}
  329. erase(g);
  330. {$I+}
  331. if ioresult<>0 then;
  332. end;
  333. end;
  334. Function TExternalAssembler.DoAssemble:boolean;
  335. begin
  336. DoAssemble:=true;
  337. if DoPipe then
  338. exit;
  339. if not(cs_asm_extern in current_settings.globalswitches) then
  340. begin
  341. if SmartAsm then
  342. begin
  343. if (SmartFilesCount<=1) then
  344. Message1(exec_i_assembling_smart,name);
  345. end
  346. else
  347. Message1(exec_i_assembling,name);
  348. end;
  349. if CallAssembler(FindAssembler,MakeCmdLine) then
  350. RemoveAsm
  351. else
  352. begin
  353. DoAssemble:=false;
  354. GenerateError;
  355. end;
  356. end;
  357. Procedure TExternalAssembler.AsmFlush;
  358. begin
  359. if outcnt>0 then
  360. begin
  361. { suppress i/o error }
  362. {$i-}
  363. BlockWrite(outfile,outbuf,outcnt);
  364. {$i+}
  365. ioerror:=ioerror or (ioresult<>0);
  366. outcnt:=0;
  367. end;
  368. end;
  369. Procedure TExternalAssembler.AsmClear;
  370. begin
  371. outcnt:=0;
  372. end;
  373. Procedure TExternalAssembler.AsmWrite(const s:string);
  374. begin
  375. if OutCnt+length(s)>=AsmOutSize then
  376. AsmFlush;
  377. Move(s[1],OutBuf[OutCnt],length(s));
  378. inc(OutCnt,length(s));
  379. inc(AsmSize,length(s));
  380. end;
  381. Procedure TExternalAssembler.AsmWriteLn(const s:string);
  382. begin
  383. AsmWrite(s);
  384. AsmLn;
  385. end;
  386. Procedure TExternalAssembler.AsmWritePChar(p:pchar);
  387. var
  388. i,j : longint;
  389. begin
  390. i:=StrLen(p);
  391. j:=i;
  392. while j>0 do
  393. begin
  394. i:=min(j,AsmOutSize);
  395. if OutCnt+i>=AsmOutSize then
  396. AsmFlush;
  397. Move(p[0],OutBuf[OutCnt],i);
  398. inc(OutCnt,i);
  399. inc(AsmSize,i);
  400. dec(j,i);
  401. p:=pchar(@p[i]);
  402. end;
  403. end;
  404. Procedure TExternalAssembler.AsmLn;
  405. begin
  406. if OutCnt>=AsmOutSize-2 then
  407. AsmFlush;
  408. if (cs_link_on_target in current_settings.globalswitches) then
  409. begin
  410. OutBuf[OutCnt]:=target_info.newline[1];
  411. inc(OutCnt);
  412. inc(AsmSize);
  413. if length(target_info.newline)>1 then
  414. begin
  415. OutBuf[OutCnt]:=target_info.newline[2];
  416. inc(OutCnt);
  417. inc(AsmSize);
  418. end;
  419. end
  420. else
  421. begin
  422. OutBuf[OutCnt]:=source_info.newline[1];
  423. inc(OutCnt);
  424. inc(AsmSize);
  425. if length(source_info.newline)>1 then
  426. begin
  427. OutBuf[OutCnt]:=source_info.newline[2];
  428. inc(OutCnt);
  429. inc(AsmSize);
  430. end;
  431. end;
  432. end;
  433. function TExternalAssembler.MakeCmdLine: TCmdStr;
  434. begin
  435. result:=target_asm.asmcmd;
  436. {$ifdef m68k}
  437. if current_settings.cputype = cpu_MC68020 then
  438. result:='-m68020 '+result
  439. else
  440. result:='-m68000 '+result;
  441. {$endif}
  442. if (cs_link_on_target in current_settings.globalswitches) then
  443. begin
  444. Replace(result,'$ASM',maybequoted(ScriptFixFileName(AsmFileName)));
  445. Replace(result,'$OBJ',maybequoted(ScriptFixFileName(ObjFileName)));
  446. end
  447. else
  448. begin
  449. {$ifdef hasunix}
  450. if DoPipe then
  451. Replace(result,'$ASM','')
  452. else
  453. {$endif}
  454. Replace(result,'$ASM',maybequoted(AsmFileName));
  455. Replace(result,'$OBJ',maybequoted(ObjFileName));
  456. end;
  457. end;
  458. procedure TExternalAssembler.AsmCreate(Aplace:tcutplace);
  459. begin
  460. if SmartAsm then
  461. NextSmartName(Aplace);
  462. {$ifdef hasunix}
  463. if DoPipe then
  464. begin
  465. if SmartAsm then
  466. begin
  467. if (SmartFilesCount<=1) then
  468. Message1(exec_i_assembling_smart,name);
  469. end
  470. else
  471. Message1(exec_i_assembling_pipe,AsmFileName);
  472. POpen(outfile,FindAssembler+' '+MakeCmdLine,'W');
  473. end
  474. else
  475. {$endif}
  476. begin
  477. Assign(outfile,AsmFileName);
  478. {$I-}
  479. Rewrite(outfile,1);
  480. {$I+}
  481. if ioresult<>0 then
  482. begin
  483. ioerror:=true;
  484. Message1(exec_d_cant_create_asmfile,AsmFileName);
  485. end;
  486. end;
  487. outcnt:=0;
  488. AsmSize:=0;
  489. AsmStartSize:=0;
  490. end;
  491. procedure TExternalAssembler.AsmClose;
  492. var
  493. f : file;
  494. FileAge : longint;
  495. begin
  496. AsmFlush;
  497. {$ifdef hasunix}
  498. if DoPipe then
  499. begin
  500. if PClose(outfile) <> 0 then
  501. GenerateError;
  502. end
  503. else
  504. {$endif}
  505. begin
  506. {Touch Assembler time to ppu time is there is a ppufilename}
  507. if ppufilename<>'' then
  508. begin
  509. Assign(f,ppufilename);
  510. {$I-}
  511. reset(f,1);
  512. {$I+}
  513. if ioresult=0 then
  514. begin
  515. FileAge := FileGetDate(GetFileHandle(f));
  516. close(f);
  517. reset(outfile,1);
  518. FileSetDate(GetFileHandle(outFile),FileAge);
  519. end;
  520. end;
  521. close(outfile);
  522. end;
  523. end;
  524. procedure TExternalAssembler.WriteTree(p:TAsmList);
  525. begin
  526. end;
  527. procedure TExternalAssembler.WriteAsmList;
  528. begin
  529. end;
  530. procedure TExternalAssembler.MakeObject;
  531. begin
  532. AsmCreate(cut_normal);
  533. FillChar(lastfileinfo, sizeof(lastfileinfo), 0);
  534. lastfileinfo.line := -1;
  535. lastinfile := nil;
  536. lastsectype := sec_none;
  537. WriteAsmList;
  538. AsmClose;
  539. if not(ioerror) then
  540. DoAssemble;
  541. end;
  542. {*****************************************************************************
  543. TInternalAssembler
  544. *****************************************************************************}
  545. constructor TInternalAssembler.create(smart:boolean);
  546. begin
  547. inherited create(smart);
  548. ObjOutput:=nil;
  549. ObjData:=nil;
  550. SmartAsm:=smart;
  551. end;
  552. destructor TInternalAssembler.destroy;
  553. begin
  554. if assigned(ObjData) then
  555. ObjData.free;
  556. if assigned(ObjOutput) then
  557. ObjOutput.free;
  558. end;
  559. procedure TInternalAssembler.WriteStab(p:pchar);
  560. function consumecomma(var p:pchar):boolean;
  561. begin
  562. while (p^=' ') do
  563. inc(p);
  564. result:=(p^=',');
  565. inc(p);
  566. end;
  567. function consumenumber(var p:pchar;out value:longint):boolean;
  568. var
  569. hs : string;
  570. len,
  571. code : integer;
  572. begin
  573. value:=0;
  574. while (p^=' ') do
  575. inc(p);
  576. len:=0;
  577. while (p^ in ['0'..'9']) do
  578. begin
  579. inc(len);
  580. hs[len]:=p^;
  581. inc(p);
  582. end;
  583. if len>0 then
  584. begin
  585. hs[0]:=chr(len);
  586. val(hs,value,code);
  587. end
  588. else
  589. code:=-1;
  590. result:=(code=0);
  591. end;
  592. function consumeoffset(var p:pchar;out relocsym:tobjsymbol;out value:longint):boolean;
  593. var
  594. hs : string;
  595. len,
  596. code : integer;
  597. pstart : pchar;
  598. sym : tobjsymbol;
  599. exprvalue : longint;
  600. gotmin,
  601. have_first_symbol,
  602. have_second_symbol,
  603. dosub : boolean;
  604. begin
  605. result:=false;
  606. value:=0;
  607. relocsym:=nil;
  608. gotmin:=false;
  609. have_first_symbol:=false;
  610. have_second_symbol:=false;
  611. repeat
  612. dosub:=false;
  613. exprvalue:=0;
  614. if gotmin then
  615. begin
  616. dosub:=true;
  617. gotmin:=false;
  618. end;
  619. while (p^=' ') do
  620. inc(p);
  621. case p^ of
  622. #0 :
  623. break;
  624. ' ' :
  625. inc(p);
  626. '0'..'9' :
  627. begin
  628. len:=0;
  629. while (p^ in ['0'..'9']) do
  630. begin
  631. inc(len);
  632. hs[len]:=p^;
  633. inc(p);
  634. end;
  635. hs[0]:=chr(len);
  636. val(hs,exprvalue,code);
  637. if code<>0 then
  638. internalerror(200702251);
  639. end;
  640. '.','_',
  641. 'A'..'Z',
  642. 'a'..'z' :
  643. begin
  644. pstart:=p;
  645. while not(p^ in [#0,' ','-','+']) do
  646. inc(p);
  647. len:=p-pstart;
  648. if len>255 then
  649. internalerror(200509187);
  650. move(pstart^,hs[1],len);
  651. hs[0]:=chr(len);
  652. sym:=objdata.symbolref(hs);
  653. have_first_symbol:=true;
  654. { Second symbol? }
  655. if assigned(relocsym) then
  656. begin
  657. if have_second_symbol then
  658. internalerror(2007032201);
  659. have_second_symbol:=true;
  660. if not have_first_symbol then
  661. internalerror(2007032202);
  662. { second symbol should substracted to first }
  663. if not dosub then
  664. internalerror(2007032203);
  665. if (relocsym.objsection<>sym.objsection) then
  666. internalerror(2005091810);
  667. exprvalue:=relocsym.address-sym.address;
  668. relocsym:=nil;
  669. dosub:=false;
  670. end
  671. else
  672. begin
  673. relocsym:=sym;
  674. if assigned(sym.objsection) then
  675. begin
  676. { first symbol should be + }
  677. if not have_first_symbol and dosub then
  678. internalerror(2007032204);
  679. have_first_symbol:=true;
  680. end;
  681. end;
  682. end;
  683. '+' :
  684. begin
  685. { nothing, by default addition is done }
  686. inc(p);
  687. end;
  688. '-' :
  689. begin
  690. gotmin:=true;
  691. inc(p);
  692. end;
  693. else
  694. internalerror(200509189);
  695. end;
  696. if dosub then
  697. dec(value,exprvalue)
  698. else
  699. inc(value,exprvalue);
  700. until false;
  701. result:=true;
  702. end;
  703. var
  704. stabstrlen,
  705. ofs,
  706. nline,
  707. nidx,
  708. nother,
  709. i : longint;
  710. stab : TObjStabEntry;
  711. relocsym : TObjSymbol;
  712. pstr,
  713. pcurr,
  714. pendquote : pchar;
  715. oldsec : TObjSection;
  716. begin
  717. pcurr:=nil;
  718. pstr:=nil;
  719. pendquote:=nil;
  720. relocsym:=nil;
  721. ofs:=0;
  722. { Parse string part }
  723. if (p[0]='"') then
  724. begin
  725. pstr:=@p[1];
  726. { Ignore \" inside the string }
  727. i:=1;
  728. while not((p[i]='"') and (p[i-1]<>'\')) and
  729. (p[i]<>#0) do
  730. inc(i);
  731. pendquote:=@p[i];
  732. pendquote^:=#0;
  733. pcurr:=@p[i+1];
  734. if not consumecomma(pcurr) then
  735. internalerror(200509181);
  736. end
  737. else
  738. pcurr:=p;
  739. { When in pass 1 then only alloc and leave }
  740. if ObjData.currpass=1 then
  741. begin
  742. ObjData.StabsSec.Alloc(sizeof(TObjStabEntry));
  743. if assigned(pstr) and (pstr[0]<>#0) then
  744. ObjData.StabStrSec.Alloc(strlen(pstr)+1);
  745. end
  746. else
  747. begin
  748. { Stabs format: nidx,nother,nline[,offset] }
  749. if not consumenumber(pcurr,nidx) then
  750. internalerror(200509182);
  751. if not consumecomma(pcurr) then
  752. internalerror(200509183);
  753. if not consumenumber(pcurr,nother) then
  754. internalerror(200509184);
  755. if not consumecomma(pcurr) then
  756. internalerror(200509185);
  757. if not consumenumber(pcurr,nline) then
  758. internalerror(200509186);
  759. if consumecomma(pcurr) then
  760. consumeoffset(pcurr,relocsym,ofs);
  761. { Generate stab entry }
  762. if assigned(pstr) and (pstr[0]<>#0) then
  763. begin
  764. stabstrlen:=strlen(pstr);
  765. {$ifdef optimizestabs}
  766. StabStrEntry:=nil;
  767. if (nidx=N_SourceFile) or (nidx=N_IncludeFile) then
  768. begin
  769. hs:=strpas(pstr);
  770. StabstrEntry:=StabStrDict.Find(hs);
  771. if not assigned(StabstrEntry) then
  772. begin
  773. StabstrEntry:=TStabStrEntry.Create(hs);
  774. StabstrEntry:=StabStrSec.Size;
  775. StabStrDict.Insert(StabstrEntry);
  776. { generate new stab }
  777. StabstrEntry:=nil;
  778. end;
  779. end;
  780. if assigned(StabstrEntry) then
  781. stab.strpos:=StabstrEntry.strpos
  782. else
  783. {$endif optimizestabs}
  784. begin
  785. stab.strpos:=ObjData.StabStrSec.Size;
  786. ObjData.StabStrSec.write(pstr^,stabstrlen+1);
  787. end;
  788. end
  789. else
  790. stab.strpos:=0;
  791. stab.ntype:=byte(nidx);
  792. stab.ndesc:=word(nline);
  793. stab.nother:=byte(nother);
  794. stab.nvalue:=ofs;
  795. { Write the stab first without the value field. Then
  796. write a the value field with relocation }
  797. oldsec:=ObjData.CurrObjSec;
  798. ObjData.SetSection(ObjData.StabsSec);
  799. ObjData.Writebytes(stab,sizeof(TObjStabEntry)-4);
  800. ObjData.Writereloc(stab.nvalue,4,relocsym,RELOC_ABSOLUTE);
  801. ObjData.setsection(oldsec);
  802. end;
  803. if assigned(pendquote) then
  804. pendquote^:='"';
  805. end;
  806. function TInternalAssembler.MaybeNextList(var hp:Tai):boolean;
  807. begin
  808. { maybe end of list }
  809. while not assigned(hp) do
  810. begin
  811. if currlistidx<lists then
  812. begin
  813. inc(currlistidx);
  814. currlist:=list[currlistidx];
  815. hp:=Tai(currList.first);
  816. end
  817. else
  818. begin
  819. MaybeNextList:=false;
  820. exit;
  821. end;
  822. end;
  823. MaybeNextList:=true;
  824. end;
  825. function TInternalAssembler.TreePass0(hp:Tai):Tai;
  826. var
  827. objsym,
  828. objsymend : TObjSymbol;
  829. begin
  830. while assigned(hp) do
  831. begin
  832. case hp.typ of
  833. ait_align :
  834. begin
  835. if tai_align_abstract(hp).aligntype>1 then
  836. begin
  837. { always use the maximum fillsize in this pass to avoid possible
  838. short jumps to become out of range }
  839. Tai_align_abstract(hp).fillsize:=Tai_align_abstract(hp).aligntype;
  840. ObjData.alloc(Tai_align_abstract(hp).fillsize);
  841. end
  842. else
  843. Tai_align_abstract(hp).fillsize:=0;
  844. end;
  845. ait_datablock :
  846. begin
  847. {$ifdef USE_COMM_IN_BSS}
  848. if writingpackages and
  849. Tai_datablock(hp).is_global then
  850. ObjData.SymbolDefine(Tai_datablock(hp).sym)
  851. else
  852. {$endif USE_COMM_IN_BSS}
  853. begin
  854. ObjData.allocalign(used_align(size_2_align(Tai_datablock(hp).size),0,ObjData.CurrObjSec.secalign));
  855. ObjData.SymbolDefine(Tai_datablock(hp).sym);
  856. ObjData.alloc(Tai_datablock(hp).size);
  857. end;
  858. end;
  859. ait_real_80bit :
  860. ObjData.alloc(10);
  861. ait_real_64bit :
  862. ObjData.alloc(8);
  863. ait_real_32bit :
  864. ObjData.alloc(4);
  865. ait_comp_64bit :
  866. ObjData.alloc(8);
  867. ait_const:
  868. begin
  869. { if symbols are provided we can calculate the value for relative symbols.
  870. This is required for length calculation of leb128 constants }
  871. if assigned(tai_const(hp).sym) then
  872. begin
  873. objsym:=Objdata.SymbolRef(tai_const(hp).sym);
  874. { objsym already defined and there is endsym? }
  875. if assigned(objsym.objsection) and assigned(tai_const(hp).endsym) then
  876. begin
  877. objsymend:=Objdata.SymbolRef(tai_const(hp).endsym);
  878. { objsymend already defined? }
  879. if assigned(objsymend.objsection) then
  880. begin
  881. if objsymend.objsection<>objsym.objsection then
  882. internalerror(200404124);
  883. Tai_const(hp).value:=objsymend.address-objsym.address+Tai_const(hp).symofs;
  884. end;
  885. end;
  886. end;
  887. ObjData.alloc(tai_const(hp).size);
  888. end;
  889. ait_section:
  890. begin
  891. ObjData.CreateSection(Tai_section(hp).sectype,Tai_section(hp).name^,Tai_section(hp).secorder);
  892. Tai_section(hp).sec:=ObjData.CurrObjSec;
  893. end;
  894. ait_symbol :
  895. ObjData.SymbolDefine(Tai_symbol(hp).sym);
  896. ait_label :
  897. ObjData.SymbolDefine(Tai_label(hp).labsym);
  898. ait_string :
  899. ObjData.alloc(Tai_string(hp).len);
  900. ait_instruction :
  901. begin
  902. { reset instructions which could change in pass 2 }
  903. Taicpu(hp).resetpass2;
  904. ObjData.alloc(Taicpu(hp).Pass1(ObjData));
  905. end;
  906. ait_cutobject :
  907. if SmartAsm then
  908. break;
  909. end;
  910. hp:=Tai(hp.next);
  911. end;
  912. TreePass0:=hp;
  913. end;
  914. function TInternalAssembler.TreePass1(hp:Tai):Tai;
  915. var
  916. objsym,
  917. objsymend : TObjSymbol;
  918. begin
  919. while assigned(hp) do
  920. begin
  921. case hp.typ of
  922. ait_align :
  923. begin
  924. if tai_align_abstract(hp).aligntype>1 then
  925. begin
  926. { here we must determine the fillsize which is used in pass2 }
  927. Tai_align_abstract(hp).fillsize:=align(ObjData.CurrObjSec.Size,Tai_align_abstract(hp).aligntype)-
  928. ObjData.CurrObjSec.Size;
  929. ObjData.alloc(Tai_align_abstract(hp).fillsize);
  930. end;
  931. end;
  932. ait_datablock :
  933. begin
  934. if (oso_data in ObjData.CurrObjSec.secoptions) then
  935. Message(asmw_e_alloc_data_only_in_bss);
  936. {$ifdef USE_COMM_IN_BSS}
  937. if writingpackages and
  938. Tai_datablock(hp).is_global then
  939. begin
  940. objsym:=ObjData.SymbolDefine(Tai_datablock(hp).sym);
  941. objsym.size:=Tai_datablock(hp).size;
  942. objsym.bind:=AB_COMMON;
  943. objsym.alignment:=needtowritealignmentalsoforELF;
  944. end
  945. else
  946. {$endif USE_COMM_IN_BSS}
  947. begin
  948. ObjData.allocalign(used_align(size_2_align(Tai_datablock(hp).size),0,ObjData.CurrObjSec.secalign));
  949. objsym:=ObjData.SymbolDefine(Tai_datablock(hp).sym);
  950. objsym.size:=Tai_datablock(hp).size;
  951. ObjData.alloc(Tai_datablock(hp).size);
  952. end;
  953. end;
  954. ait_real_80bit :
  955. ObjData.alloc(10);
  956. ait_real_64bit :
  957. ObjData.alloc(8);
  958. ait_real_32bit :
  959. ObjData.alloc(4);
  960. ait_comp_64bit :
  961. ObjData.alloc(8);
  962. ait_const:
  963. begin
  964. { Recalculate relative symbols }
  965. if assigned(tai_const(hp).sym) and
  966. assigned(tai_const(hp).endsym) then
  967. begin
  968. objsym:=Objdata.SymbolRef(tai_const(hp).sym);
  969. objsymend:=Objdata.SymbolRef(tai_const(hp).endsym);
  970. if objsymend.objsection<>objsym.objsection then
  971. internalerror(200905042);
  972. Tai_const(hp).value:=objsymend.address-objsym.address+Tai_const(hp).symofs;
  973. end;
  974. ObjData.alloc(tai_const(hp).size);
  975. end;
  976. ait_section:
  977. begin
  978. { use cached value }
  979. ObjData.setsection(Tai_section(hp).sec);
  980. end;
  981. ait_stab :
  982. begin
  983. if assigned(Tai_stab(hp).str) then
  984. WriteStab(Tai_stab(hp).str);
  985. end;
  986. ait_symbol :
  987. ObjData.SymbolDefine(Tai_symbol(hp).sym);
  988. ait_symbol_end :
  989. begin
  990. objsym:=ObjData.SymbolRef(Tai_symbol_end(hp).sym);
  991. objsym.size:=ObjData.CurrObjSec.Size-objsym.offset;
  992. end;
  993. ait_label :
  994. ObjData.SymbolDefine(Tai_label(hp).labsym);
  995. ait_string :
  996. ObjData.alloc(Tai_string(hp).len);
  997. ait_instruction :
  998. ObjData.alloc(Taicpu(hp).Pass1(ObjData));
  999. ait_cutobject :
  1000. if SmartAsm then
  1001. break;
  1002. end;
  1003. hp:=Tai(hp.next);
  1004. end;
  1005. TreePass1:=hp;
  1006. end;
  1007. function TInternalAssembler.TreePass2(hp:Tai):Tai;
  1008. var
  1009. fillbuffer : tfillbuffer;
  1010. {$ifdef x86}
  1011. co : comp;
  1012. {$endif x86}
  1013. leblen : byte;
  1014. lebbuf : array[0..63] of byte;
  1015. objsym,
  1016. objsymend : TObjSymbol;
  1017. begin
  1018. { main loop }
  1019. while assigned(hp) do
  1020. begin
  1021. case hp.typ of
  1022. ait_align :
  1023. begin
  1024. if (oso_data in ObjData.CurrObjSec.secoptions) then
  1025. ObjData.writebytes(Tai_align_abstract(hp).calculatefillbuf(fillbuffer)^,Tai_align_abstract(hp).fillsize)
  1026. else
  1027. ObjData.alloc(Tai_align_abstract(hp).fillsize);
  1028. end;
  1029. ait_section :
  1030. begin
  1031. { use cached value }
  1032. ObjData.setsection(Tai_section(hp).sec);
  1033. end;
  1034. ait_symbol :
  1035. begin
  1036. ObjOutput.exportsymbol(ObjData.SymbolRef(Tai_symbol(hp).sym));
  1037. end;
  1038. ait_datablock :
  1039. begin
  1040. ObjOutput.exportsymbol(ObjData.SymbolRef(Tai_datablock(hp).sym));
  1041. {$ifdef USE_COMM_IN_BSS}
  1042. if not(writingpackages and
  1043. Tai_datablock(hp).is_global) then
  1044. {$endif USE_COMM_IN_BSS}
  1045. begin
  1046. ObjData.allocalign(used_align(size_2_align(Tai_datablock(hp).size),0,ObjData.CurrObjSec.secalign));
  1047. ObjData.alloc(Tai_datablock(hp).size);
  1048. end;
  1049. end;
  1050. ait_real_80bit :
  1051. ObjData.writebytes(Tai_real_80bit(hp).value,10);
  1052. ait_real_64bit :
  1053. ObjData.writebytes(Tai_real_64bit(hp).value,8);
  1054. ait_real_32bit :
  1055. ObjData.writebytes(Tai_real_32bit(hp).value,4);
  1056. ait_comp_64bit :
  1057. begin
  1058. {$ifdef x86}
  1059. co:=comp(Tai_comp_64bit(hp).value);
  1060. ObjData.writebytes(co,8);
  1061. {$endif x86}
  1062. end;
  1063. ait_string :
  1064. ObjData.writebytes(Tai_string(hp).str^,Tai_string(hp).len);
  1065. ait_const :
  1066. begin
  1067. { Recalculate relative symbols, addresses of forward references
  1068. can be changed in treepass1 }
  1069. if assigned(tai_const(hp).sym) and
  1070. assigned(tai_const(hp).endsym) then
  1071. begin
  1072. objsym:=Objdata.SymbolRef(tai_const(hp).sym);
  1073. objsymend:=Objdata.SymbolRef(tai_const(hp).endsym);
  1074. Tai_const(hp).value:=objsymend.address-objsym.address+Tai_const(hp).symofs;
  1075. end;
  1076. case tai_const(hp).consttype of
  1077. aitconst_64bit,
  1078. aitconst_32bit,
  1079. aitconst_16bit,
  1080. aitconst_8bit :
  1081. begin
  1082. if assigned(tai_const(hp).sym) and
  1083. not assigned(tai_const(hp).endsym) then
  1084. ObjData.writereloc(Tai_const(hp).symofs,tai_const(hp).size,Objdata.SymbolRef(tai_const(hp).sym),RELOC_ABSOLUTE)
  1085. else
  1086. ObjData.writebytes(Tai_const(hp).value,tai_const(hp).size);
  1087. end;
  1088. aitconst_rva_symbol :
  1089. begin
  1090. { PE32+? }
  1091. if target_info.system=system_x86_64_win64 then
  1092. ObjData.writereloc(Tai_const(hp).symofs,sizeof(longint),Objdata.SymbolRef(tai_const(hp).sym),RELOC_RVA)
  1093. else
  1094. ObjData.writereloc(Tai_const(hp).symofs,sizeof(pint),Objdata.SymbolRef(tai_const(hp).sym),RELOC_RVA);
  1095. end;
  1096. aitconst_secrel32_symbol :
  1097. begin
  1098. { Required for DWARF2 support under Windows }
  1099. ObjData.writereloc(Tai_const(hp).symofs,sizeof(longint),Objdata.SymbolRef(tai_const(hp).sym),RELOC_SECREL32);
  1100. end;
  1101. aitconst_uleb128bit,
  1102. aitconst_sleb128bit :
  1103. begin
  1104. if tai_const(hp).consttype=aitconst_uleb128bit then
  1105. leblen:=EncodeUleb128(qword(Tai_const(hp).value),lebbuf)
  1106. else
  1107. leblen:=EncodeSleb128(Tai_const(hp).value,lebbuf);
  1108. if leblen<>tai_const(hp).size then
  1109. internalerror(200709271);
  1110. ObjData.writebytes(lebbuf,leblen);
  1111. end;
  1112. else
  1113. internalerror(200603254);
  1114. end;
  1115. end;
  1116. ait_label :
  1117. begin
  1118. { exporting shouldn't be necessary as labels are local,
  1119. but it's better to be on the safe side (PFV) }
  1120. ObjOutput.exportsymbol(ObjData.SymbolRef(Tai_label(hp).labsym));
  1121. end;
  1122. ait_instruction :
  1123. Taicpu(hp).Pass2(ObjData);
  1124. ait_stab :
  1125. WriteStab(Tai_stab(hp).str);
  1126. ait_function_name,
  1127. ait_force_line : ;
  1128. ait_cutobject :
  1129. if SmartAsm then
  1130. break;
  1131. end;
  1132. hp:=Tai(hp.next);
  1133. end;
  1134. TreePass2:=hp;
  1135. end;
  1136. procedure TInternalAssembler.writetree;
  1137. label
  1138. doexit;
  1139. var
  1140. hp : Tai;
  1141. ObjWriter : TObjectWriter;
  1142. begin
  1143. ObjWriter:=TObjectwriter.create;
  1144. ObjOutput:=CObjOutput.Create(ObjWriter);
  1145. ObjData:=ObjOutput.newObjData(ObjFileName);
  1146. { Pass 0 }
  1147. ObjData.currpass:=0;
  1148. ObjData.createsection(sec_code);
  1149. ObjData.beforealloc;
  1150. { start with list 1 }
  1151. currlistidx:=1;
  1152. currlist:=list[currlistidx];
  1153. hp:=Tai(currList.first);
  1154. while assigned(hp) do
  1155. begin
  1156. hp:=TreePass0(hp);
  1157. MaybeNextList(hp);
  1158. end;
  1159. ObjData.afteralloc;
  1160. { leave if errors have occured }
  1161. if errorcount>0 then
  1162. goto doexit;
  1163. { Pass 1 }
  1164. ObjData.currpass:=1;
  1165. ObjData.resetsections;
  1166. ObjData.beforealloc;
  1167. ObjData.createsection(sec_code);
  1168. { start with list 1 }
  1169. currlistidx:=1;
  1170. currlist:=list[currlistidx];
  1171. hp:=Tai(currList.first);
  1172. while assigned(hp) do
  1173. begin
  1174. hp:=TreePass1(hp);
  1175. MaybeNextList(hp);
  1176. end;
  1177. ObjData.createsection(sec_code);
  1178. ObjData.afteralloc;
  1179. { leave if errors have occured }
  1180. if errorcount>0 then
  1181. goto doexit;
  1182. { Pass 2 }
  1183. ObjData.currpass:=2;
  1184. ObjData.resetsections;
  1185. ObjData.beforewrite;
  1186. ObjData.createsection(sec_code);
  1187. { start with list 1 }
  1188. currlistidx:=1;
  1189. currlist:=list[currlistidx];
  1190. hp:=Tai(currList.first);
  1191. while assigned(hp) do
  1192. begin
  1193. hp:=TreePass2(hp);
  1194. MaybeNextList(hp);
  1195. end;
  1196. ObjData.createsection(sec_code);
  1197. ObjData.afterwrite;
  1198. { don't write the .o file if errors have occured }
  1199. if errorcount=0 then
  1200. begin
  1201. { write objectfile }
  1202. ObjOutput.startobjectfile(ObjFileName);
  1203. ObjOutput.writeobjectfile(ObjData);
  1204. end;
  1205. doexit:
  1206. { Cleanup }
  1207. ObjData.free;
  1208. ObjData:=nil;
  1209. ObjWriter.free;
  1210. end;
  1211. procedure TInternalAssembler.writetreesmart;
  1212. var
  1213. hp : Tai;
  1214. startsectype : TAsmSectiontype;
  1215. place: tcutplace;
  1216. ObjWriter : TObjectWriter;
  1217. begin
  1218. if not(cs_asm_leave in current_settings.globalswitches) then
  1219. ObjWriter:=TARObjectWriter.create(current_module.staticlibfilename^)
  1220. else
  1221. ObjWriter:=TObjectwriter.create;
  1222. NextSmartName(cut_normal);
  1223. ObjOutput:=CObjOutput.Create(ObjWriter);
  1224. startsectype:=sec_code;
  1225. { start with list 1 }
  1226. currlistidx:=1;
  1227. currlist:=list[currlistidx];
  1228. hp:=Tai(currList.first);
  1229. while assigned(hp) do
  1230. begin
  1231. ObjData:=ObjOutput.newObjData(ObjFileName);
  1232. { Pass 0 }
  1233. ObjData.currpass:=0;
  1234. ObjData.resetsections;
  1235. ObjData.beforealloc;
  1236. ObjData.createsection(startsectype);
  1237. TreePass0(hp);
  1238. ObjData.afteralloc;
  1239. { leave if errors have occured }
  1240. if errorcount>0 then
  1241. break;
  1242. { Pass 1 }
  1243. ObjData.currpass:=1;
  1244. ObjData.resetsections;
  1245. ObjData.beforealloc;
  1246. ObjData.createsection(startsectype);
  1247. TreePass1(hp);
  1248. ObjData.afteralloc;
  1249. { leave if errors have occured }
  1250. if errorcount>0 then
  1251. break;
  1252. { Pass 2 }
  1253. ObjData.currpass:=2;
  1254. ObjOutput.startobjectfile(ObjFileName);
  1255. ObjData.resetsections;
  1256. ObjData.beforewrite;
  1257. ObjData.createsection(startsectype);
  1258. hp:=TreePass2(hp);
  1259. ObjData.afterwrite;
  1260. { leave if errors have occured }
  1261. if errorcount>0 then
  1262. break;
  1263. { write the current objectfile }
  1264. ObjOutput.writeobjectfile(ObjData);
  1265. ObjData.free;
  1266. ObjData:=nil;
  1267. { end of lists? }
  1268. if not MaybeNextList(hp) then
  1269. break;
  1270. { we will start a new objectfile so reset everything }
  1271. { The place can still change in the next while loop, so don't init }
  1272. { the writer yet (JM) }
  1273. if (hp.typ=ait_cutobject) then
  1274. place := Tai_cutobject(hp).place
  1275. else
  1276. place := cut_normal;
  1277. { avoid empty files }
  1278. startsectype:=sec_code;
  1279. while assigned(hp) and
  1280. (Tai(hp).typ in [ait_marker,ait_comment,ait_section,ait_cutobject]) do
  1281. begin
  1282. if Tai(hp).typ=ait_section then
  1283. startsectype:=Tai_section(hp).sectype;
  1284. if (Tai(hp).typ=ait_cutobject) then
  1285. place:=Tai_cutobject(hp).place;
  1286. hp:=Tai(hp.next);
  1287. end;
  1288. if not MaybeNextList(hp) then
  1289. break;
  1290. { start next objectfile }
  1291. NextSmartName(place);
  1292. end;
  1293. ObjData.free;
  1294. ObjData:=nil;
  1295. ObjWriter.free;
  1296. end;
  1297. procedure TInternalAssembler.MakeObject;
  1298. var to_do:set of TasmlistType;
  1299. i:TasmlistType;
  1300. procedure addlist(p:TAsmList);
  1301. begin
  1302. inc(lists);
  1303. list[lists]:=p;
  1304. end;
  1305. begin
  1306. to_do:=[low(Tasmlisttype)..high(Tasmlisttype)];
  1307. if usedeffileforexports then
  1308. exclude(to_do,al_exports);
  1309. if not(tf_section_threadvars in target_info.flags) then
  1310. exclude(to_do,al_threadvars);
  1311. for i:=low(TasmlistType) to high(TasmlistType) do
  1312. if (i in to_do) and (current_asmdata.asmlists[i]<>nil) then
  1313. addlist(current_asmdata.asmlists[i]);
  1314. if SmartAsm then
  1315. writetreesmart
  1316. else
  1317. writetree;
  1318. end;
  1319. {*****************************************************************************
  1320. Generate Assembler Files Main Procedure
  1321. *****************************************************************************}
  1322. Procedure GenerateAsm(smart:boolean);
  1323. var
  1324. a : TAssembler;
  1325. begin
  1326. if not assigned(CAssembler[target_asm.id]) then
  1327. Message(asmw_f_assembler_output_not_supported);
  1328. a:=CAssembler[target_asm.id].Create(smart);
  1329. a.MakeObject;
  1330. a.Free;
  1331. end;
  1332. Procedure OnlyAsm;
  1333. var
  1334. a : TExternalAssembler;
  1335. begin
  1336. a:=TExternalAssembler.Create(false);
  1337. a.DoAssemble;
  1338. a.Free;
  1339. end;
  1340. {*****************************************************************************
  1341. Init/Done
  1342. *****************************************************************************}
  1343. procedure RegisterAssembler(const r:tasminfo;c:TAssemblerClass);
  1344. var
  1345. t : tasm;
  1346. begin
  1347. t:=r.id;
  1348. if assigned(asminfos[t]) then
  1349. writeln('Warning: Assembler is already registered!')
  1350. else
  1351. Getmem(asminfos[t],sizeof(tasminfo));
  1352. asminfos[t]^:=r;
  1353. CAssembler[t]:=c;
  1354. end;
  1355. end.