assemble.pas 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479
  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. {$ifdef m68k}
  150. cpuinfo,
  151. {$endif m68k}
  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. if Tai_datablock(hp).is_global then
  848. ObjData.SymbolDefine(Tai_datablock(hp).sym)
  849. else
  850. begin
  851. ObjData.allocalign(used_align(size_2_align(Tai_datablock(hp).size),0,ObjData.CurrObjSec.secalign));
  852. ObjData.SymbolDefine(Tai_datablock(hp).sym);
  853. ObjData.alloc(Tai_datablock(hp).size);
  854. end;
  855. end;
  856. ait_real_80bit :
  857. ObjData.alloc(10);
  858. ait_real_64bit :
  859. ObjData.alloc(8);
  860. ait_real_32bit :
  861. ObjData.alloc(4);
  862. ait_comp_64bit :
  863. ObjData.alloc(8);
  864. ait_const:
  865. begin
  866. { if symbols are provided we can calculate the value for relative symbols.
  867. This is required for length calculation of leb128 constants }
  868. if assigned(tai_const(hp).sym) then
  869. begin
  870. objsym:=Objdata.SymbolRef(tai_const(hp).sym);
  871. if assigned(tai_const(hp).endsym) then
  872. begin
  873. objsymend:=Objdata.SymbolRef(tai_const(hp).endsym);
  874. if objsymend.objsection<>objsym.objsection then
  875. internalerror(200404124);
  876. Tai_const(hp).value:=objsymend.address-objsym.address+Tai_const(hp).symofs;
  877. end;
  878. end;
  879. ObjData.alloc(tai_const(hp).size);
  880. end;
  881. ait_section:
  882. begin
  883. ObjData.CreateSection(Tai_section(hp).sectype,Tai_section(hp).name^,Tai_section(hp).secorder);
  884. Tai_section(hp).sec:=ObjData.CurrObjSec;
  885. end;
  886. ait_symbol :
  887. ObjData.SymbolDefine(Tai_symbol(hp).sym);
  888. ait_label :
  889. ObjData.SymbolDefine(Tai_label(hp).labsym);
  890. ait_string :
  891. ObjData.alloc(Tai_string(hp).len);
  892. ait_instruction :
  893. begin
  894. { reset instructions which could change in pass 2 }
  895. Taicpu(hp).resetpass2;
  896. ObjData.alloc(Taicpu(hp).Pass1(ObjData));
  897. end;
  898. ait_cutobject :
  899. if SmartAsm then
  900. break;
  901. end;
  902. hp:=Tai(hp.next);
  903. end;
  904. TreePass0:=hp;
  905. end;
  906. function TInternalAssembler.TreePass1(hp:Tai):Tai;
  907. var
  908. objsym,
  909. objsymend : TObjSymbol;
  910. begin
  911. while assigned(hp) do
  912. begin
  913. case hp.typ of
  914. ait_align :
  915. begin
  916. if tai_align_abstract(hp).aligntype>1 then
  917. begin
  918. { here we must determine the fillsize which is used in pass2 }
  919. Tai_align_abstract(hp).fillsize:=align(ObjData.CurrObjSec.Size,Tai_align_abstract(hp).aligntype)-
  920. ObjData.CurrObjSec.Size;
  921. ObjData.alloc(Tai_align_abstract(hp).fillsize);
  922. end;
  923. end;
  924. ait_datablock :
  925. begin
  926. if (oso_data in ObjData.CurrObjSec.secoptions) then
  927. Message(asmw_e_alloc_data_only_in_bss);
  928. if Tai_datablock(hp).is_global then
  929. begin
  930. objsym:=ObjData.SymbolDefine(Tai_datablock(hp).sym);
  931. objsym.size:=Tai_datablock(hp).size;
  932. objsym.bind:=AB_COMMON;
  933. end
  934. else
  935. begin
  936. ObjData.allocalign(used_align(size_2_align(Tai_datablock(hp).size),0,ObjData.CurrObjSec.secalign));
  937. objsym:=ObjData.SymbolDefine(Tai_datablock(hp).sym);
  938. objsym.size:=Tai_datablock(hp).size;
  939. ObjData.alloc(Tai_datablock(hp).size);
  940. end;
  941. end;
  942. ait_real_80bit :
  943. ObjData.alloc(10);
  944. ait_real_64bit :
  945. ObjData.alloc(8);
  946. ait_real_32bit :
  947. ObjData.alloc(4);
  948. ait_comp_64bit :
  949. ObjData.alloc(8);
  950. ait_const:
  951. begin
  952. { Recalculate relative symbols, all checks are done in treepass0 }
  953. if assigned(tai_const(hp).sym) and
  954. assigned(tai_const(hp).endsym) then
  955. begin
  956. objsym:=Objdata.SymbolRef(tai_const(hp).sym);
  957. objsymend:=Objdata.SymbolRef(tai_const(hp).endsym);
  958. Tai_const(hp).value:=objsymend.address-objsym.address+Tai_const(hp).symofs;
  959. end;
  960. ObjData.alloc(tai_const(hp).size);
  961. end;
  962. ait_section:
  963. begin
  964. { use cached value }
  965. ObjData.setsection(Tai_section(hp).sec);
  966. end;
  967. ait_stab :
  968. begin
  969. if assigned(Tai_stab(hp).str) then
  970. WriteStab(Tai_stab(hp).str);
  971. end;
  972. ait_symbol :
  973. ObjData.SymbolDefine(Tai_symbol(hp).sym);
  974. ait_symbol_end :
  975. begin
  976. objsym:=ObjData.SymbolRef(Tai_symbol_end(hp).sym);
  977. objsym.size:=ObjData.CurrObjSec.Size-objsym.offset;
  978. end;
  979. ait_label :
  980. ObjData.SymbolDefine(Tai_label(hp).labsym);
  981. ait_string :
  982. ObjData.alloc(Tai_string(hp).len);
  983. ait_instruction :
  984. ObjData.alloc(Taicpu(hp).Pass1(ObjData));
  985. ait_cutobject :
  986. if SmartAsm then
  987. break;
  988. end;
  989. hp:=Tai(hp.next);
  990. end;
  991. TreePass1:=hp;
  992. end;
  993. function TInternalAssembler.TreePass2(hp:Tai):Tai;
  994. var
  995. fillbuffer : tfillbuffer;
  996. {$ifdef x86}
  997. co : comp;
  998. {$endif x86}
  999. leblen : byte;
  1000. lebbuf : array[0..63] of byte;
  1001. objsym,
  1002. objsymend : TObjSymbol;
  1003. begin
  1004. { main loop }
  1005. while assigned(hp) do
  1006. begin
  1007. case hp.typ of
  1008. ait_align :
  1009. begin
  1010. if (oso_data in ObjData.CurrObjSec.secoptions) then
  1011. ObjData.writebytes(Tai_align_abstract(hp).calculatefillbuf(fillbuffer)^,Tai_align_abstract(hp).fillsize)
  1012. else
  1013. ObjData.alloc(Tai_align_abstract(hp).fillsize);
  1014. end;
  1015. ait_section :
  1016. begin
  1017. { use cached value }
  1018. ObjData.setsection(Tai_section(hp).sec);
  1019. end;
  1020. ait_symbol :
  1021. begin
  1022. ObjOutput.exportsymbol(ObjData.SymbolRef(Tai_symbol(hp).sym));
  1023. end;
  1024. ait_datablock :
  1025. begin
  1026. ObjOutput.exportsymbol(ObjData.SymbolRef(Tai_datablock(hp).sym));
  1027. if not Tai_datablock(hp).is_global then
  1028. begin
  1029. ObjData.allocalign(used_align(size_2_align(Tai_datablock(hp).size),0,ObjData.CurrObjSec.secalign));
  1030. ObjData.alloc(Tai_datablock(hp).size);
  1031. end;
  1032. end;
  1033. ait_real_80bit :
  1034. ObjData.writebytes(Tai_real_80bit(hp).value,10);
  1035. ait_real_64bit :
  1036. ObjData.writebytes(Tai_real_64bit(hp).value,8);
  1037. ait_real_32bit :
  1038. ObjData.writebytes(Tai_real_32bit(hp).value,4);
  1039. ait_comp_64bit :
  1040. begin
  1041. {$ifdef x86}
  1042. co:=comp(Tai_comp_64bit(hp).value);
  1043. ObjData.writebytes(co,8);
  1044. {$endif x86}
  1045. end;
  1046. ait_string :
  1047. ObjData.writebytes(Tai_string(hp).str^,Tai_string(hp).len);
  1048. ait_const :
  1049. begin
  1050. { Recalculate relative symbols, addresses of forward references
  1051. can be changed in treepass1 }
  1052. if assigned(tai_const(hp).sym) and
  1053. assigned(tai_const(hp).endsym) then
  1054. begin
  1055. objsym:=Objdata.SymbolRef(tai_const(hp).sym);
  1056. objsymend:=Objdata.SymbolRef(tai_const(hp).endsym);
  1057. Tai_const(hp).value:=objsymend.address-objsym.address+Tai_const(hp).symofs;
  1058. end;
  1059. case tai_const(hp).consttype of
  1060. aitconst_64bit,
  1061. aitconst_32bit,
  1062. aitconst_16bit,
  1063. aitconst_8bit :
  1064. begin
  1065. if assigned(tai_const(hp).sym) and
  1066. not assigned(tai_const(hp).endsym) then
  1067. ObjData.writereloc(Tai_const(hp).symofs,tai_const(hp).size,Objdata.SymbolRef(tai_const(hp).sym),RELOC_ABSOLUTE)
  1068. else
  1069. ObjData.writebytes(Tai_const(hp).value,tai_const(hp).size);
  1070. end;
  1071. aitconst_rva_symbol :
  1072. begin
  1073. { PE32+? }
  1074. if target_info.system=system_x86_64_win64 then
  1075. ObjData.writereloc(Tai_const(hp).symofs,sizeof(longint),Objdata.SymbolRef(tai_const(hp).sym),RELOC_RVA)
  1076. else
  1077. ObjData.writereloc(Tai_const(hp).symofs,sizeof(pint),Objdata.SymbolRef(tai_const(hp).sym),RELOC_RVA);
  1078. end;
  1079. aitconst_secrel32_symbol :
  1080. begin
  1081. { Required for DWARF2 support under Windows }
  1082. ObjData.writereloc(Tai_const(hp).symofs,sizeof(longint),Objdata.SymbolRef(tai_const(hp).sym),RELOC_SECREL32);
  1083. end;
  1084. aitconst_uleb128bit,
  1085. aitconst_sleb128bit :
  1086. begin
  1087. if tai_const(hp).consttype=aitconst_uleb128bit then
  1088. leblen:=EncodeUleb128(qword(Tai_const(hp).value),lebbuf)
  1089. else
  1090. leblen:=EncodeSleb128(Tai_const(hp).value,lebbuf);
  1091. if leblen<>tai_const(hp).size then
  1092. internalerror(200709271);
  1093. ObjData.writebytes(lebbuf,leblen);
  1094. end;
  1095. else
  1096. internalerror(200603254);
  1097. end;
  1098. end;
  1099. ait_label :
  1100. begin
  1101. { exporting shouldn't be necessary as labels are local,
  1102. but it's better to be on the safe side (PFV) }
  1103. ObjOutput.exportsymbol(ObjData.SymbolRef(Tai_label(hp).labsym));
  1104. end;
  1105. ait_instruction :
  1106. Taicpu(hp).Pass2(ObjData);
  1107. ait_stab :
  1108. WriteStab(Tai_stab(hp).str);
  1109. ait_function_name,
  1110. ait_force_line : ;
  1111. ait_cutobject :
  1112. if SmartAsm then
  1113. break;
  1114. end;
  1115. hp:=Tai(hp.next);
  1116. end;
  1117. TreePass2:=hp;
  1118. end;
  1119. procedure TInternalAssembler.writetree;
  1120. label
  1121. doexit;
  1122. var
  1123. hp : Tai;
  1124. ObjWriter : TObjectWriter;
  1125. begin
  1126. ObjWriter:=TObjectwriter.create;
  1127. ObjOutput:=CObjOutput.Create(ObjWriter);
  1128. ObjData:=ObjOutput.newObjData(ObjFileName);
  1129. { Pass 0 }
  1130. ObjData.currpass:=0;
  1131. ObjData.createsection(sec_code);
  1132. ObjData.beforealloc;
  1133. { start with list 1 }
  1134. currlistidx:=1;
  1135. currlist:=list[currlistidx];
  1136. hp:=Tai(currList.first);
  1137. while assigned(hp) do
  1138. begin
  1139. hp:=TreePass0(hp);
  1140. MaybeNextList(hp);
  1141. end;
  1142. ObjData.afteralloc;
  1143. { leave if errors have occured }
  1144. if errorcount>0 then
  1145. goto doexit;
  1146. { Pass 1 }
  1147. ObjData.currpass:=1;
  1148. ObjData.resetsections;
  1149. ObjData.beforealloc;
  1150. ObjData.createsection(sec_code);
  1151. { start with list 1 }
  1152. currlistidx:=1;
  1153. currlist:=list[currlistidx];
  1154. hp:=Tai(currList.first);
  1155. while assigned(hp) do
  1156. begin
  1157. hp:=TreePass1(hp);
  1158. MaybeNextList(hp);
  1159. end;
  1160. ObjData.createsection(sec_code);
  1161. ObjData.afteralloc;
  1162. { leave if errors have occured }
  1163. if errorcount>0 then
  1164. goto doexit;
  1165. { Pass 2 }
  1166. ObjData.currpass:=2;
  1167. ObjData.resetsections;
  1168. ObjData.beforewrite;
  1169. ObjData.createsection(sec_code);
  1170. { start with list 1 }
  1171. currlistidx:=1;
  1172. currlist:=list[currlistidx];
  1173. hp:=Tai(currList.first);
  1174. while assigned(hp) do
  1175. begin
  1176. hp:=TreePass2(hp);
  1177. MaybeNextList(hp);
  1178. end;
  1179. ObjData.createsection(sec_code);
  1180. ObjData.afterwrite;
  1181. { don't write the .o file if errors have occured }
  1182. if errorcount=0 then
  1183. begin
  1184. { write objectfile }
  1185. ObjOutput.startobjectfile(ObjFileName);
  1186. ObjOutput.writeobjectfile(ObjData);
  1187. end;
  1188. doexit:
  1189. { Cleanup }
  1190. ObjData.free;
  1191. ObjData:=nil;
  1192. ObjWriter.free;
  1193. end;
  1194. procedure TInternalAssembler.writetreesmart;
  1195. var
  1196. hp : Tai;
  1197. startsectype : TAsmSectiontype;
  1198. place: tcutplace;
  1199. ObjWriter : TObjectWriter;
  1200. begin
  1201. if not(cs_asm_leave in current_settings.globalswitches) then
  1202. ObjWriter:=TARObjectWriter.create(current_module.staticlibfilename^)
  1203. else
  1204. ObjWriter:=TObjectwriter.create;
  1205. NextSmartName(cut_normal);
  1206. ObjOutput:=CObjOutput.Create(ObjWriter);
  1207. startsectype:=sec_code;
  1208. { start with list 1 }
  1209. currlistidx:=1;
  1210. currlist:=list[currlistidx];
  1211. hp:=Tai(currList.first);
  1212. while assigned(hp) do
  1213. begin
  1214. ObjData:=ObjOutput.newObjData(ObjFileName);
  1215. { Pass 0 }
  1216. ObjData.currpass:=0;
  1217. ObjData.resetsections;
  1218. ObjData.beforealloc;
  1219. ObjData.createsection(startsectype);
  1220. TreePass0(hp);
  1221. ObjData.afteralloc;
  1222. { leave if errors have occured }
  1223. if errorcount>0 then
  1224. break;
  1225. { Pass 1 }
  1226. ObjData.currpass:=1;
  1227. ObjData.resetsections;
  1228. ObjData.beforealloc;
  1229. ObjData.createsection(startsectype);
  1230. TreePass1(hp);
  1231. ObjData.afteralloc;
  1232. { leave if errors have occured }
  1233. if errorcount>0 then
  1234. break;
  1235. { Pass 2 }
  1236. ObjData.currpass:=2;
  1237. ObjOutput.startobjectfile(ObjFileName);
  1238. ObjData.resetsections;
  1239. ObjData.beforewrite;
  1240. ObjData.createsection(startsectype);
  1241. hp:=TreePass2(hp);
  1242. ObjData.afterwrite;
  1243. { leave if errors have occured }
  1244. if errorcount>0 then
  1245. break;
  1246. { write the current objectfile }
  1247. ObjOutput.writeobjectfile(ObjData);
  1248. ObjData.free;
  1249. ObjData:=nil;
  1250. { end of lists? }
  1251. if not MaybeNextList(hp) then
  1252. break;
  1253. { we will start a new objectfile so reset everything }
  1254. { The place can still change in the next while loop, so don't init }
  1255. { the writer yet (JM) }
  1256. if (hp.typ=ait_cutobject) then
  1257. place := Tai_cutobject(hp).place
  1258. else
  1259. place := cut_normal;
  1260. { avoid empty files }
  1261. startsectype:=sec_code;
  1262. while assigned(hp) and
  1263. (Tai(hp).typ in [ait_marker,ait_comment,ait_section,ait_cutobject]) do
  1264. begin
  1265. if Tai(hp).typ=ait_section then
  1266. startsectype:=Tai_section(hp).sectype;
  1267. if (Tai(hp).typ=ait_cutobject) then
  1268. place:=Tai_cutobject(hp).place;
  1269. hp:=Tai(hp.next);
  1270. end;
  1271. if not MaybeNextList(hp) then
  1272. break;
  1273. { start next objectfile }
  1274. NextSmartName(place);
  1275. end;
  1276. ObjData.free;
  1277. ObjData:=nil;
  1278. ObjWriter.free;
  1279. end;
  1280. procedure TInternalAssembler.MakeObject;
  1281. var to_do:set of TasmlistType;
  1282. i:TasmlistType;
  1283. procedure addlist(p:TAsmList);
  1284. begin
  1285. inc(lists);
  1286. list[lists]:=p;
  1287. end;
  1288. begin
  1289. to_do:=[low(Tasmlisttype)..high(Tasmlisttype)];
  1290. if usedeffileforexports then
  1291. exclude(to_do,al_exports);
  1292. if not(tf_section_threadvars in target_info.flags) then
  1293. exclude(to_do,al_threadvars);
  1294. for i:=low(TasmlistType) to high(TasmlistType) do
  1295. if (i in to_do) and (current_asmdata.asmlists[i]<>nil) then
  1296. addlist(current_asmdata.asmlists[i]);
  1297. if SmartAsm then
  1298. writetreesmart
  1299. else
  1300. writetree;
  1301. end;
  1302. {*****************************************************************************
  1303. Generate Assembler Files Main Procedure
  1304. *****************************************************************************}
  1305. Procedure GenerateAsm(smart:boolean);
  1306. var
  1307. a : TAssembler;
  1308. begin
  1309. if not assigned(CAssembler[target_asm.id]) then
  1310. Message(asmw_f_assembler_output_not_supported);
  1311. a:=CAssembler[target_asm.id].Create(smart);
  1312. a.MakeObject;
  1313. a.Free;
  1314. end;
  1315. Procedure OnlyAsm;
  1316. var
  1317. a : TExternalAssembler;
  1318. begin
  1319. a:=TExternalAssembler.Create(false);
  1320. a.DoAssemble;
  1321. a.Free;
  1322. end;
  1323. {*****************************************************************************
  1324. Init/Done
  1325. *****************************************************************************}
  1326. procedure RegisterAssembler(const r:tasminfo;c:TAssemblerClass);
  1327. var
  1328. t : tasm;
  1329. begin
  1330. t:=r.id;
  1331. if assigned(asminfos[t]) then
  1332. writeln('Warning: Assembler is already registered!')
  1333. else
  1334. Getmem(asminfos[t],sizeof(tasminfo));
  1335. asminfos[t]^:=r;
  1336. CAssembler[t]:=c;
  1337. end;
  1338. end.