assemble.pas 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788
  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 = ord(high(tasmlisttype))+1;
  31. { buffer size for writing the .s file }
  32. AsmOutSize=32768*4;
  33. type
  34. TAssembler=class(TAbstractAssembler)
  35. public
  36. {filenames}
  37. path : TPathStr;
  38. name : string;
  39. AsmFileName, { current .s and .o file }
  40. ObjFileName,
  41. ppufilename : TPathStr;
  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 overridden 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:TPathStr);
  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. procedure WriteSourceLine(hp: tailineinfo);
  73. procedure WriteTempalloc(hp: tai_tempalloc);
  74. public
  75. {# Returns the complete path and executable name of the assembler
  76. program.
  77. It first tries looking in the UTIL directory if specified,
  78. otherwise it searches in the free pascal binary directory, in
  79. the current working directory and then in the directories
  80. in the $PATH environment.}
  81. Function FindAssembler:string;
  82. {# Actually does the call to the assembler file. Returns false
  83. if the assembling of the file failed.}
  84. Function CallAssembler(const command:string; const para:TCmdStr):Boolean;
  85. Function DoAssemble:boolean;virtual;
  86. Procedure RemoveAsm;virtual;
  87. Procedure AsmFlush;
  88. Procedure AsmClear;
  89. {# Write a string to the assembler file }
  90. Procedure AsmWrite(const c:char);
  91. Procedure AsmWrite(const s:string);
  92. Procedure AsmWrite(const s:ansistring);
  93. {# Write a string to the assembler file }
  94. Procedure AsmWritePChar(p:pchar);
  95. {# Write a string to the assembler file followed by a new line }
  96. Procedure AsmWriteLn(const c:char);
  97. Procedure AsmWriteLn(const s:string);
  98. Procedure AsmWriteLn(const s:ansistring);
  99. {# Write a new line to the assembler file }
  100. Procedure AsmLn;
  101. procedure AsmCreate(Aplace:tcutplace);
  102. procedure AsmClose;
  103. {# This routine should be overridden for each assembler, it is used
  104. to actually write the abstract assembler stream to file.}
  105. procedure WriteTree(p:TAsmList);virtual;
  106. {# This routine should be overridden for each assembler, it is used
  107. to actually write all the different abstract assembler streams
  108. by calling for each stream type, the @var(WriteTree) method.}
  109. procedure WriteAsmList;virtual;
  110. {# Constructs the command line for calling the assembler }
  111. function MakeCmdLine: TCmdStr; virtual;
  112. public
  113. Constructor Create(smart:boolean);override;
  114. procedure MakeObject;override;
  115. end;
  116. { TInternalAssembler }
  117. TInternalAssembler=class(TAssembler)
  118. private
  119. FCObjOutput : TObjOutputclass;
  120. { the aasmoutput lists that need to be processed }
  121. lists : byte;
  122. list : array[1..maxoutputlists] of TAsmList;
  123. { current processing }
  124. currlistidx : byte;
  125. currlist : TAsmList;
  126. procedure WriteStab(p:pchar);
  127. function MaybeNextList(var hp:Tai):boolean;
  128. function SetIndirectToSymbol(hp: Tai; const indirectname: string): Boolean;
  129. function TreePass0(hp:Tai):Tai;
  130. function TreePass1(hp:Tai):Tai;
  131. function TreePass2(hp:Tai):Tai;
  132. procedure writetree;
  133. procedure writetreesmart;
  134. protected
  135. ObjData : TObjData;
  136. ObjOutput : tObjOutput;
  137. property CObjOutput:TObjOutputclass read FCObjOutput write FCObjOutput;
  138. public
  139. constructor create(smart:boolean);override;
  140. destructor destroy;override;
  141. procedure MakeObject;override;
  142. end;
  143. TAssemblerClass = class of TAssembler;
  144. Procedure GenerateAsm(smart:boolean);
  145. Procedure OnlyAsm;
  146. procedure RegisterAssembler(const r:tasminfo;c:TAssemblerClass);
  147. Implementation
  148. uses
  149. {$ifdef hasunix}
  150. unix,
  151. {$endif}
  152. cutils,cfileutl,
  153. {$ifdef memdebug}
  154. cclasses,
  155. {$endif memdebug}
  156. script,fmodule,verbose,
  157. {$if defined(m68k) or defined(arm)}
  158. cpuinfo,
  159. {$endif m68k or arm}
  160. aasmcpu,
  161. owbase,owar
  162. ;
  163. var
  164. CAssembler : array[tasm] of TAssemblerClass;
  165. function fixline(s:string):string;
  166. {
  167. return s with all leading and ending spaces and tabs removed
  168. }
  169. var
  170. i,j,k : integer;
  171. begin
  172. i:=length(s);
  173. while (i>0) and (s[i] in [#9,' ']) do
  174. dec(i);
  175. j:=1;
  176. while (j<i) and (s[j] in [#9,' ']) do
  177. inc(j);
  178. for k:=j to i do
  179. if s[k] in [#0..#31,#127..#255] then
  180. s[k]:='.';
  181. fixline:=Copy(s,j,i-j+1);
  182. end;
  183. {*****************************************************************************
  184. TAssembler
  185. *****************************************************************************}
  186. Constructor TAssembler.Create(smart:boolean);
  187. begin
  188. { load start values }
  189. AsmFileName:=current_module.AsmFilename;
  190. ObjFileName:=current_module.ObjFileName;
  191. name:=Lower(current_module.modulename^);
  192. path:=current_module.outputpath;
  193. asmprefix := current_module.asmprefix^;
  194. if current_module.outputpath = '' then
  195. ppufilename := ''
  196. else
  197. ppufilename := current_module.ppufilename;
  198. SmartAsm:=smart;
  199. SmartFilesCount:=0;
  200. SmartHeaderCount:=0;
  201. SmartLinkOFiles.Clear;
  202. end;
  203. Destructor TAssembler.Destroy;
  204. begin
  205. end;
  206. procedure TAssembler.NextSmartName(place:tcutplace);
  207. var
  208. s : string;
  209. begin
  210. inc(SmartFilesCount);
  211. if SmartFilesCount>999999 then
  212. Message(asmw_f_too_many_asm_files);
  213. case place of
  214. cut_begin :
  215. begin
  216. inc(SmartHeaderCount);
  217. s:=asmprefix+tostr(SmartHeaderCount)+'h';
  218. end;
  219. cut_normal :
  220. s:=asmprefix+tostr(SmartHeaderCount)+'s';
  221. cut_end :
  222. s:=asmprefix+tostr(SmartHeaderCount)+'t';
  223. end;
  224. AsmFileName:=Path+FixFileName(s+tostr(SmartFilesCount)+target_info.asmext);
  225. ObjFileName:=Path+FixFileName(s+tostr(SmartFilesCount)+target_info.objext);
  226. { insert in container so it can be cleared after the linking }
  227. SmartLinkOFiles.Insert(ObjFileName);
  228. end;
  229. {*****************************************************************************
  230. TExternalAssembler
  231. *****************************************************************************}
  232. Function DoPipe:boolean;
  233. begin
  234. DoPipe:=(cs_asm_pipe in current_settings.globalswitches) and
  235. (([cs_asm_extern,cs_asm_leave,cs_link_on_target] * current_settings.globalswitches) = []) and
  236. ((target_asm.id in [as_gas,as_ggas,as_darwin,as_powerpc_xcoff]));
  237. end;
  238. Constructor TExternalAssembler.Create(smart:boolean);
  239. begin
  240. inherited Create(smart);
  241. if SmartAsm then
  242. begin
  243. path:=FixPath(ChangeFileExt(AsmFileName,target_info.smartext),false);
  244. CreateSmartLinkPath(path);
  245. end;
  246. Outcnt:=0;
  247. end;
  248. procedure TExternalAssembler.CreateSmartLinkPath(const s:TPathStr);
  249. procedure DeleteFilesWithExt(const AExt:string);
  250. var
  251. dir : TSearchRec;
  252. begin
  253. if findfirst(s+source_info.dirsep+'*'+AExt,faAnyFile,dir) = 0 then
  254. begin
  255. repeat
  256. DeleteFile(s+source_info.dirsep+dir.name);
  257. until findnext(dir) <> 0;
  258. end;
  259. findclose(dir);
  260. end;
  261. var
  262. hs : TPathStr;
  263. begin
  264. if PathExists(s,false) then
  265. begin
  266. { the path exists, now we clean only all the .o and .s files }
  267. DeleteFilesWithExt(target_info.objext);
  268. DeleteFilesWithExt(target_info.asmext);
  269. end
  270. else
  271. begin
  272. hs:=s;
  273. if hs[length(hs)] in ['/','\'] then
  274. delete(hs,length(hs),1);
  275. {$push} {$I-}
  276. mkdir(hs);
  277. {$pop}
  278. if ioresult<>0 then;
  279. end;
  280. end;
  281. const
  282. lastas : byte=255;
  283. var
  284. LastASBin : TCmdStr;
  285. Function TExternalAssembler.FindAssembler:string;
  286. var
  287. asfound : boolean;
  288. UtilExe : string;
  289. begin
  290. asfound:=false;
  291. if cs_link_on_target in current_settings.globalswitches then
  292. begin
  293. { If linking on target, don't add any path PM }
  294. FindAssembler:=utilsprefix+ChangeFileExt(target_asm.asmbin,target_info.exeext);
  295. exit;
  296. end
  297. else
  298. UtilExe:=utilsprefix+ChangeFileExt(target_asm.asmbin,source_info.exeext);
  299. if lastas<>ord(target_asm.id) then
  300. begin
  301. lastas:=ord(target_asm.id);
  302. { is an assembler passed ? }
  303. if utilsdirectory<>'' then
  304. asfound:=FindFile(UtilExe,utilsdirectory,false,LastASBin);
  305. if not AsFound then
  306. asfound:=FindExe(UtilExe,false,LastASBin);
  307. if (not asfound) and not(cs_asm_extern in current_settings.globalswitches) then
  308. begin
  309. Message1(exec_e_assembler_not_found,LastASBin);
  310. current_settings.globalswitches:=current_settings.globalswitches+[cs_asm_extern];
  311. end;
  312. if asfound then
  313. Message1(exec_t_using_assembler,LastASBin);
  314. end;
  315. FindAssembler:=LastASBin;
  316. end;
  317. Function TExternalAssembler.CallAssembler(const command:string; const para:TCmdStr):Boolean;
  318. var
  319. DosExitCode : Integer;
  320. begin
  321. result:=true;
  322. if (cs_asm_extern in current_settings.globalswitches) then
  323. begin
  324. AsmRes.AddAsmCommand(command,para,name);
  325. exit;
  326. end;
  327. try
  328. FlushOutput;
  329. DosExitCode:=RequotedExecuteProcess(command,para);
  330. if DosExitCode<>0
  331. then begin
  332. Message1(exec_e_error_while_assembling,tostr(dosexitcode));
  333. result:=false;
  334. end;
  335. except on E:EOSError do
  336. begin
  337. Message1(exec_e_cant_call_assembler,tostr(E.ErrorCode));
  338. current_settings.globalswitches:=current_settings.globalswitches+[cs_asm_extern];
  339. result:=false;
  340. end;
  341. end;
  342. end;
  343. procedure TExternalAssembler.RemoveAsm;
  344. var
  345. g : file;
  346. begin
  347. if cs_asm_leave in current_settings.globalswitches then
  348. exit;
  349. if cs_asm_extern in current_settings.globalswitches then
  350. AsmRes.AddDeleteCommand(AsmFileName)
  351. else
  352. begin
  353. assign(g,AsmFileName);
  354. {$push} {$I-}
  355. erase(g);
  356. {$pop}
  357. if ioresult<>0 then;
  358. end;
  359. end;
  360. Function TExternalAssembler.DoAssemble:boolean;
  361. begin
  362. DoAssemble:=true;
  363. if DoPipe then
  364. exit;
  365. if not(cs_asm_extern in current_settings.globalswitches) then
  366. begin
  367. if SmartAsm then
  368. begin
  369. if (SmartFilesCount<=1) then
  370. Message1(exec_i_assembling_smart,name);
  371. end
  372. else
  373. Message1(exec_i_assembling,name);
  374. end;
  375. if CallAssembler(FindAssembler,MakeCmdLine) then
  376. RemoveAsm
  377. else
  378. begin
  379. DoAssemble:=false;
  380. GenerateError;
  381. end;
  382. end;
  383. Procedure TExternalAssembler.AsmFlush;
  384. begin
  385. if outcnt>0 then
  386. begin
  387. { suppress i/o error }
  388. {$push} {$I-}
  389. BlockWrite(outfile,outbuf,outcnt);
  390. {$pop}
  391. ioerror:=ioerror or (ioresult<>0);
  392. outcnt:=0;
  393. end;
  394. end;
  395. Procedure TExternalAssembler.AsmClear;
  396. begin
  397. outcnt:=0;
  398. end;
  399. Procedure TExternalAssembler.AsmWrite(const c: char);
  400. begin
  401. if OutCnt+1>=AsmOutSize then
  402. AsmFlush;
  403. OutBuf[OutCnt]:=c;
  404. inc(OutCnt);
  405. inc(AsmSize);
  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.AsmWrite(const s:ansistring);
  416. var
  417. StartIndex, ToWrite: longint;
  418. begin
  419. if s='' then
  420. exit;
  421. if OutCnt+length(s)>=AsmOutSize then
  422. AsmFlush;
  423. StartIndex:=1;
  424. ToWrite:=length(s);
  425. while ToWrite>AsmOutSize do
  426. begin
  427. Move(s[StartIndex],OutBuf[OutCnt],AsmOutSize);
  428. inc(OutCnt,AsmOutSize);
  429. inc(AsmSize,AsmOutSize);
  430. AsmFlush;
  431. inc(StartIndex,AsmOutSize);
  432. dec(ToWrite,AsmOutSize);
  433. end;
  434. Move(s[StartIndex],OutBuf[OutCnt],ToWrite);
  435. inc(OutCnt,ToWrite);
  436. inc(AsmSize,ToWrite);
  437. end;
  438. procedure TExternalAssembler.AsmWriteLn(const c: char);
  439. begin
  440. AsmWrite(c);
  441. AsmLn;
  442. end;
  443. Procedure TExternalAssembler.AsmWriteLn(const s:string);
  444. begin
  445. AsmWrite(s);
  446. AsmLn;
  447. end;
  448. Procedure TExternalAssembler.AsmWriteLn(const s: ansistring);
  449. begin
  450. AsmWrite(s);
  451. AsmLn;
  452. end;
  453. Procedure TExternalAssembler.AsmWritePChar(p:pchar);
  454. var
  455. i,j : longint;
  456. begin
  457. i:=StrLen(p);
  458. j:=i;
  459. while j>0 do
  460. begin
  461. i:=min(j,AsmOutSize);
  462. if OutCnt+i>=AsmOutSize then
  463. AsmFlush;
  464. Move(p[0],OutBuf[OutCnt],i);
  465. inc(OutCnt,i);
  466. inc(AsmSize,i);
  467. dec(j,i);
  468. p:=pchar(@p[i]);
  469. end;
  470. end;
  471. Procedure TExternalAssembler.AsmLn;
  472. begin
  473. if OutCnt>=AsmOutSize-2 then
  474. AsmFlush;
  475. if (cs_link_on_target in current_settings.globalswitches) then
  476. begin
  477. OutBuf[OutCnt]:=target_info.newline[1];
  478. inc(OutCnt);
  479. inc(AsmSize);
  480. if length(target_info.newline)>1 then
  481. begin
  482. OutBuf[OutCnt]:=target_info.newline[2];
  483. inc(OutCnt);
  484. inc(AsmSize);
  485. end;
  486. end
  487. else
  488. begin
  489. OutBuf[OutCnt]:=source_info.newline[1];
  490. inc(OutCnt);
  491. inc(AsmSize);
  492. if length(source_info.newline)>1 then
  493. begin
  494. OutBuf[OutCnt]:=source_info.newline[2];
  495. inc(OutCnt);
  496. inc(AsmSize);
  497. end;
  498. end;
  499. end;
  500. function TExternalAssembler.MakeCmdLine: TCmdStr;
  501. begin
  502. result:=target_asm.asmcmd;
  503. {$ifdef m68k}
  504. { TODO: use a better approach for this }
  505. if (target_info.system=system_m68k_amiga) then
  506. begin
  507. { m68k-amiga has old binutils, which doesn't support -march=* }
  508. case current_settings.cputype of
  509. cpu_MC68000:
  510. result:='-m68000 '+result;
  511. cpu_MC68020:
  512. result:='-m68020 '+result;
  513. { additionally, AmigaOS doesn't work on Coldfire }
  514. end;
  515. end
  516. else
  517. begin
  518. case current_settings.cputype of
  519. cpu_MC68000:
  520. result:='-march=68000 '+result;
  521. cpu_MC68020:
  522. result:='-march=68020 '+result;
  523. cpu_Coldfire:
  524. result:='-march=cfv4e '+result;
  525. end;
  526. end;
  527. {$endif}
  528. {$ifdef arm}
  529. if (target_info.system=system_arm_darwin) then
  530. Replace(result,'$ARCH',lower(cputypestr[current_settings.cputype]));
  531. {$endif arm}
  532. if (cs_link_on_target in current_settings.globalswitches) then
  533. begin
  534. Replace(result,'$ASM',maybequoted(ScriptFixFileName(AsmFileName)));
  535. Replace(result,'$OBJ',maybequoted(ScriptFixFileName(ObjFileName)));
  536. end
  537. else
  538. begin
  539. {$ifdef hasunix}
  540. if DoPipe then
  541. Replace(result,'$ASM','')
  542. else
  543. {$endif}
  544. Replace(result,'$ASM',maybequoted(AsmFileName));
  545. Replace(result,'$OBJ',maybequoted(ObjFileName));
  546. end;
  547. if (cs_create_pic in current_settings.moduleswitches) then
  548. Replace(result,'$PIC','-KPIC')
  549. else
  550. Replace(result,'$PIC','');
  551. if (cs_asm_source in current_settings.globalswitches) then
  552. Replace(result,'$NOWARN','')
  553. else
  554. Replace(result,'$NOWARN','-W');
  555. end;
  556. procedure TExternalAssembler.AsmCreate(Aplace:tcutplace);
  557. begin
  558. if SmartAsm then
  559. NextSmartName(Aplace);
  560. {$ifdef hasunix}
  561. if DoPipe then
  562. begin
  563. if SmartAsm then
  564. begin
  565. if (SmartFilesCount<=1) then
  566. Message1(exec_i_assembling_smart,name);
  567. end
  568. else
  569. Message1(exec_i_assembling_pipe,AsmFileName);
  570. POpen(outfile,maybequoted(FindAssembler)+' '+MakeCmdLine,'W');
  571. end
  572. else
  573. {$endif}
  574. begin
  575. Assign(outfile,AsmFileName);
  576. {$push} {$I-}
  577. Rewrite(outfile,1);
  578. {$pop}
  579. if ioresult<>0 then
  580. begin
  581. ioerror:=true;
  582. Message1(exec_d_cant_create_asmfile,AsmFileName);
  583. end;
  584. end;
  585. outcnt:=0;
  586. AsmSize:=0;
  587. AsmStartSize:=0;
  588. end;
  589. procedure TExternalAssembler.AsmClose;
  590. var
  591. f : file;
  592. FileAge : longint;
  593. begin
  594. AsmFlush;
  595. {$ifdef hasunix}
  596. if DoPipe then
  597. begin
  598. if PClose(outfile) <> 0 then
  599. GenerateError;
  600. end
  601. else
  602. {$endif}
  603. begin
  604. {Touch Assembler time to ppu time is there is a ppufilename}
  605. if ppufilename<>'' then
  606. begin
  607. Assign(f,ppufilename);
  608. {$push} {$I-}
  609. reset(f,1);
  610. {$pop}
  611. if ioresult=0 then
  612. begin
  613. FileAge := FileGetDate(GetFileHandle(f));
  614. close(f);
  615. reset(outfile,1);
  616. FileSetDate(GetFileHandle(outFile),FileAge);
  617. end;
  618. end;
  619. close(outfile);
  620. end;
  621. end;
  622. procedure TExternalAssembler.WriteSourceLine(hp: tailineinfo);
  623. var
  624. module : tmodule;
  625. begin
  626. { load infile }
  627. if (lastfileinfo.moduleindex<>hp.fileinfo.moduleindex) or
  628. (lastfileinfo.fileindex<>hp.fileinfo.fileindex) then
  629. begin
  630. { in case of a generic the module can be different }
  631. if current_module.unit_index=hp.fileinfo.moduleindex then
  632. module:=current_module
  633. else
  634. module:=get_module(hp.fileinfo.moduleindex);
  635. { during the compilation of the system unit there are cases when
  636. the fileinfo contains just zeros => invalid }
  637. if assigned(module) then
  638. infile:=module.sourcefiles.get_file(hp.fileinfo.fileindex)
  639. else
  640. infile:=nil;
  641. if assigned(infile) then
  642. begin
  643. { open only if needed !! }
  644. if (cs_asm_source in current_settings.globalswitches) then
  645. infile.open;
  646. end;
  647. { avoid unnecessary reopens of the same file !! }
  648. lastfileinfo.fileindex:=hp.fileinfo.fileindex;
  649. lastfileinfo.moduleindex:=hp.fileinfo.moduleindex;
  650. { be sure to change line !! }
  651. lastfileinfo.line:=-1;
  652. end;
  653. { write source }
  654. if (cs_asm_source in current_settings.globalswitches) and
  655. assigned(infile) then
  656. begin
  657. if (infile<>lastinfile) then
  658. begin
  659. AsmWriteLn(target_asm.comment+'['+infile.name+']');
  660. if assigned(lastinfile) then
  661. lastinfile.close;
  662. end;
  663. if (hp.fileinfo.line<>lastfileinfo.line) and
  664. (hp.fileinfo.line<infile.maxlinebuf) then
  665. begin
  666. if (hp.fileinfo.line<>0) and
  667. (infile.linebuf^[hp.fileinfo.line]>=0) then
  668. AsmWriteLn(target_asm.comment+'['+tostr(hp.fileinfo.line)+'] '+
  669. fixline(infile.GetLineStr(hp.fileinfo.line)));
  670. { set it to a negative value !
  671. to make that is has been read already !! PM }
  672. if (infile.linebuf^[hp.fileinfo.line]>=0) then
  673. infile.linebuf^[hp.fileinfo.line]:=-infile.linebuf^[hp.fileinfo.line]-1;
  674. end;
  675. end;
  676. lastfileinfo:=hp.fileinfo;
  677. lastinfile:=infile;
  678. end;
  679. procedure TExternalAssembler.WriteTempalloc(hp: tai_tempalloc);
  680. begin
  681. {$ifdef EXTDEBUG}
  682. if assigned(hp.problem) then
  683. AsmWriteLn(target_asm.comment+'Temp '+tostr(hp.temppos)+','+
  684. tostr(hp.tempsize)+' '+hp.problem^)
  685. else
  686. {$endif EXTDEBUG}
  687. AsmWriteLn(target_asm.comment+'Temp '+tostr(hp.temppos)+','+
  688. tostr(hp.tempsize)+' '+tempallocstr[hp.allocation]);
  689. end;
  690. procedure TExternalAssembler.WriteTree(p:TAsmList);
  691. begin
  692. end;
  693. procedure TExternalAssembler.WriteAsmList;
  694. begin
  695. end;
  696. procedure TExternalAssembler.MakeObject;
  697. begin
  698. AsmCreate(cut_normal);
  699. FillChar(lastfileinfo, sizeof(lastfileinfo), 0);
  700. lastfileinfo.line := -1;
  701. lastinfile := nil;
  702. lastsectype := sec_none;
  703. WriteAsmList;
  704. AsmClose;
  705. if not(ioerror) then
  706. DoAssemble;
  707. end;
  708. {*****************************************************************************
  709. TInternalAssembler
  710. *****************************************************************************}
  711. constructor TInternalAssembler.create(smart:boolean);
  712. begin
  713. inherited create(smart);
  714. ObjOutput:=nil;
  715. ObjData:=nil;
  716. SmartAsm:=smart;
  717. end;
  718. destructor TInternalAssembler.destroy;
  719. begin
  720. if assigned(ObjData) then
  721. ObjData.free;
  722. if assigned(ObjOutput) then
  723. ObjOutput.free;
  724. end;
  725. procedure TInternalAssembler.WriteStab(p:pchar);
  726. function consumecomma(var p:pchar):boolean;
  727. begin
  728. while (p^=' ') do
  729. inc(p);
  730. result:=(p^=',');
  731. inc(p);
  732. end;
  733. function consumenumber(var p:pchar;out value:longint):boolean;
  734. var
  735. hs : string;
  736. len,
  737. code : integer;
  738. begin
  739. value:=0;
  740. while (p^=' ') do
  741. inc(p);
  742. len:=0;
  743. while (p^ in ['0'..'9']) do
  744. begin
  745. inc(len);
  746. hs[len]:=p^;
  747. inc(p);
  748. end;
  749. if len>0 then
  750. begin
  751. hs[0]:=chr(len);
  752. val(hs,value,code);
  753. end
  754. else
  755. code:=-1;
  756. result:=(code=0);
  757. end;
  758. function consumeoffset(var p:pchar;out relocsym:tobjsymbol;out value:longint):boolean;
  759. var
  760. hs : string;
  761. len,
  762. code : integer;
  763. pstart : pchar;
  764. sym : tobjsymbol;
  765. exprvalue : longint;
  766. gotmin,
  767. have_first_symbol,
  768. have_second_symbol,
  769. dosub : boolean;
  770. begin
  771. result:=false;
  772. value:=0;
  773. relocsym:=nil;
  774. gotmin:=false;
  775. have_first_symbol:=false;
  776. have_second_symbol:=false;
  777. repeat
  778. dosub:=false;
  779. exprvalue:=0;
  780. if gotmin then
  781. begin
  782. dosub:=true;
  783. gotmin:=false;
  784. end;
  785. while (p^=' ') do
  786. inc(p);
  787. case p^ of
  788. #0 :
  789. break;
  790. ' ' :
  791. inc(p);
  792. '0'..'9' :
  793. begin
  794. len:=0;
  795. while (p^ in ['0'..'9']) do
  796. begin
  797. inc(len);
  798. hs[len]:=p^;
  799. inc(p);
  800. end;
  801. hs[0]:=chr(len);
  802. val(hs,exprvalue,code);
  803. if code<>0 then
  804. internalerror(200702251);
  805. end;
  806. '.','_',
  807. 'A'..'Z',
  808. 'a'..'z' :
  809. begin
  810. pstart:=p;
  811. while not(p^ in [#0,' ','-','+']) do
  812. inc(p);
  813. len:=p-pstart;
  814. if len>255 then
  815. internalerror(200509187);
  816. move(pstart^,hs[1],len);
  817. hs[0]:=chr(len);
  818. sym:=objdata.symbolref(hs);
  819. have_first_symbol:=true;
  820. { Second symbol? }
  821. if assigned(relocsym) then
  822. begin
  823. if have_second_symbol then
  824. internalerror(2007032201);
  825. have_second_symbol:=true;
  826. if not have_first_symbol then
  827. internalerror(2007032202);
  828. { second symbol should substracted to first }
  829. if not dosub then
  830. internalerror(2007032203);
  831. if (relocsym.objsection<>sym.objsection) then
  832. internalerror(2005091810);
  833. exprvalue:=relocsym.address-sym.address;
  834. relocsym:=nil;
  835. dosub:=false;
  836. end
  837. else
  838. begin
  839. relocsym:=sym;
  840. if assigned(sym.objsection) then
  841. begin
  842. { first symbol should be + }
  843. if not have_first_symbol and dosub then
  844. internalerror(2007032204);
  845. have_first_symbol:=true;
  846. end;
  847. end;
  848. end;
  849. '+' :
  850. begin
  851. { nothing, by default addition is done }
  852. inc(p);
  853. end;
  854. '-' :
  855. begin
  856. gotmin:=true;
  857. inc(p);
  858. end;
  859. else
  860. internalerror(200509189);
  861. end;
  862. if dosub then
  863. dec(value,exprvalue)
  864. else
  865. inc(value,exprvalue);
  866. until false;
  867. result:=true;
  868. end;
  869. var
  870. stabstrlen,
  871. ofs,
  872. nline,
  873. nidx,
  874. nother,
  875. i : longint;
  876. stab : TObjStabEntry;
  877. relocsym : TObjSymbol;
  878. pstr,
  879. pcurr,
  880. pendquote : pchar;
  881. oldsec : TObjSection;
  882. begin
  883. pcurr:=nil;
  884. pstr:=nil;
  885. pendquote:=nil;
  886. relocsym:=nil;
  887. ofs:=0;
  888. { Parse string part }
  889. if (p[0]='"') then
  890. begin
  891. pstr:=@p[1];
  892. { Ignore \" inside the string }
  893. i:=1;
  894. while not((p[i]='"') and (p[i-1]<>'\')) and
  895. (p[i]<>#0) do
  896. inc(i);
  897. pendquote:=@p[i];
  898. pendquote^:=#0;
  899. pcurr:=@p[i+1];
  900. if not consumecomma(pcurr) then
  901. internalerror(200509181);
  902. end
  903. else
  904. pcurr:=p;
  905. { When in pass 1 then only alloc and leave }
  906. if ObjData.currpass=1 then
  907. begin
  908. ObjData.StabsSec.Alloc(sizeof(TObjStabEntry));
  909. if assigned(pstr) and (pstr[0]<>#0) then
  910. ObjData.StabStrSec.Alloc(strlen(pstr)+1);
  911. end
  912. else
  913. begin
  914. { Stabs format: nidx,nother,nline[,offset] }
  915. if not consumenumber(pcurr,nidx) then
  916. internalerror(200509182);
  917. if not consumecomma(pcurr) then
  918. internalerror(200509183);
  919. if not consumenumber(pcurr,nother) then
  920. internalerror(200509184);
  921. if not consumecomma(pcurr) then
  922. internalerror(200509185);
  923. if not consumenumber(pcurr,nline) then
  924. internalerror(200509186);
  925. if consumecomma(pcurr) then
  926. consumeoffset(pcurr,relocsym,ofs);
  927. { Generate stab entry }
  928. if assigned(pstr) and (pstr[0]<>#0) then
  929. begin
  930. stabstrlen:=strlen(pstr);
  931. {$ifdef optimizestabs}
  932. StabStrEntry:=nil;
  933. if (nidx=N_SourceFile) or (nidx=N_IncludeFile) then
  934. begin
  935. hs:=strpas(pstr);
  936. StabstrEntry:=StabStrDict.Find(hs);
  937. if not assigned(StabstrEntry) then
  938. begin
  939. StabstrEntry:=TStabStrEntry.Create(hs);
  940. StabstrEntry:=StabStrSec.Size;
  941. StabStrDict.Insert(StabstrEntry);
  942. { generate new stab }
  943. StabstrEntry:=nil;
  944. end;
  945. end;
  946. if assigned(StabstrEntry) then
  947. stab.strpos:=StabstrEntry.strpos
  948. else
  949. {$endif optimizestabs}
  950. begin
  951. stab.strpos:=ObjData.StabStrSec.Size;
  952. ObjData.StabStrSec.write(pstr^,stabstrlen+1);
  953. end;
  954. end
  955. else
  956. stab.strpos:=0;
  957. stab.ntype:=byte(nidx);
  958. stab.ndesc:=word(nline);
  959. stab.nother:=byte(nother);
  960. stab.nvalue:=ofs;
  961. { Write the stab first without the value field. Then
  962. write a the value field with relocation }
  963. oldsec:=ObjData.CurrObjSec;
  964. ObjData.SetSection(ObjData.StabsSec);
  965. ObjData.Writebytes(stab,sizeof(TObjStabEntry)-4);
  966. ObjData.Writereloc(stab.nvalue,4,relocsym,RELOC_ABSOLUTE32);
  967. ObjData.setsection(oldsec);
  968. end;
  969. if assigned(pendquote) then
  970. pendquote^:='"';
  971. end;
  972. function TInternalAssembler.MaybeNextList(var hp:Tai):boolean;
  973. begin
  974. { maybe end of list }
  975. while not assigned(hp) do
  976. begin
  977. if currlistidx<lists then
  978. begin
  979. inc(currlistidx);
  980. currlist:=list[currlistidx];
  981. hp:=Tai(currList.first);
  982. end
  983. else
  984. begin
  985. MaybeNextList:=false;
  986. exit;
  987. end;
  988. end;
  989. MaybeNextList:=true;
  990. end;
  991. function TInternalAssembler.SetIndirectToSymbol(hp: Tai; const indirectname: string): Boolean;
  992. var
  993. objsym : TObjSymbol;
  994. indsym : TObjSymbol;
  995. begin
  996. Result:=
  997. Assigned(hp) and
  998. (hp.typ=ait_symbol);
  999. if not Result then
  1000. Exit;
  1001. objsym:=Objdata.SymbolRef(tai_symbol(hp).sym);
  1002. objsym.size:=0;
  1003. indsym := TObjSymbol(ObjData.ObjSymbolList.Find(indirectname));
  1004. if not Assigned(indsym) then
  1005. begin
  1006. { it's possible that indirect symbol is not present in the list,
  1007. so we must create it as undefined }
  1008. indsym:=TObjSymbol.Create(ObjData.ObjSymbolList, indirectname);
  1009. indsym.typ:=AT_NONE;
  1010. indsym.bind:=AB_NONE;
  1011. end;
  1012. objsym.indsymbol:=indsym;
  1013. Result:=true;
  1014. end;
  1015. function TInternalAssembler.TreePass0(hp:Tai):Tai;
  1016. var
  1017. objsym,
  1018. objsymend : TObjSymbol;
  1019. begin
  1020. while assigned(hp) do
  1021. begin
  1022. case hp.typ of
  1023. ait_align :
  1024. begin
  1025. if tai_align_abstract(hp).aligntype>1 then
  1026. begin
  1027. { always use the maximum fillsize in this pass to avoid possible
  1028. short jumps to become out of range }
  1029. Tai_align_abstract(hp).fillsize:=Tai_align_abstract(hp).aligntype;
  1030. ObjData.alloc(Tai_align_abstract(hp).fillsize);
  1031. { may need to increase alignment of section }
  1032. if tai_align_abstract(hp).aligntype>ObjData.CurrObjSec.secalign then
  1033. ObjData.CurrObjSec.secalign:=tai_align_abstract(hp).aligntype;
  1034. end
  1035. else
  1036. Tai_align_abstract(hp).fillsize:=0;
  1037. end;
  1038. ait_datablock :
  1039. begin
  1040. {$ifdef USE_COMM_IN_BSS}
  1041. if writingpackages and
  1042. Tai_datablock(hp).is_global then
  1043. ObjData.SymbolDefine(Tai_datablock(hp).sym)
  1044. else
  1045. {$endif USE_COMM_IN_BSS}
  1046. begin
  1047. ObjData.allocalign(used_align(size_2_align(Tai_datablock(hp).size),0,ObjData.CurrObjSec.secalign));
  1048. ObjData.SymbolDefine(Tai_datablock(hp).sym);
  1049. ObjData.alloc(Tai_datablock(hp).size);
  1050. end;
  1051. end;
  1052. ait_real_80bit :
  1053. ObjData.alloc(tai_real_80bit(hp).savesize);
  1054. ait_real_64bit :
  1055. ObjData.alloc(8);
  1056. ait_real_32bit :
  1057. ObjData.alloc(4);
  1058. ait_comp_64bit :
  1059. ObjData.alloc(8);
  1060. ait_const:
  1061. begin
  1062. { if symbols are provided we can calculate the value for relative symbols.
  1063. This is required for length calculation of leb128 constants }
  1064. if assigned(tai_const(hp).sym) then
  1065. begin
  1066. objsym:=Objdata.SymbolRef(tai_const(hp).sym);
  1067. { objsym already defined and there is endsym? }
  1068. if assigned(objsym.objsection) and assigned(tai_const(hp).endsym) then
  1069. begin
  1070. objsymend:=Objdata.SymbolRef(tai_const(hp).endsym);
  1071. { objsymend already defined? }
  1072. if assigned(objsymend.objsection) then
  1073. begin
  1074. if objsymend.objsection<>objsym.objsection then
  1075. begin
  1076. { leb128 relative constants are not relocatable, but other types are,
  1077. given that objsym belongs to the current section. }
  1078. if (Tai_const(hp).consttype in [aitconst_uleb128bit,aitconst_sleb128bit]) or
  1079. (objsym.objsection<>ObjData.CurrObjSec) then
  1080. InternalError(200404124);
  1081. end
  1082. else
  1083. Tai_const(hp).value:=objsymend.address-objsym.address+Tai_const(hp).symofs;
  1084. end;
  1085. end;
  1086. end;
  1087. ObjData.alloc(tai_const(hp).size);
  1088. end;
  1089. ait_directive:
  1090. begin
  1091. case tai_directive(hp).directive of
  1092. asd_indirect_symbol:
  1093. { handled in TreePass1 }
  1094. ;
  1095. asd_lazy_reference:
  1096. begin
  1097. if tai_directive(hp).name='' then
  1098. Internalerror(2009112101);
  1099. objsym:=ObjData.symbolref(tai_directive(hp).name);
  1100. objsym.bind:=AB_LAZY;
  1101. end;
  1102. asd_reference:
  1103. { ignore for now, but should be added}
  1104. ;
  1105. else
  1106. internalerror(2010011101);
  1107. end;
  1108. end;
  1109. ait_section:
  1110. begin
  1111. ObjData.CreateSection(Tai_section(hp).sectype,Tai_section(hp).name^,Tai_section(hp).secorder);
  1112. Tai_section(hp).sec:=ObjData.CurrObjSec;
  1113. end;
  1114. ait_symbol :
  1115. begin
  1116. { needs extra support in the internal assembler }
  1117. { the value is just ignored }
  1118. {if tai_symbol(hp).has_value then
  1119. internalerror(2009090804); ;}
  1120. ObjData.SymbolDefine(Tai_symbol(hp).sym);
  1121. end;
  1122. ait_label :
  1123. ObjData.SymbolDefine(Tai_label(hp).labsym);
  1124. ait_string :
  1125. ObjData.alloc(Tai_string(hp).len);
  1126. ait_instruction :
  1127. begin
  1128. { reset instructions which could change in pass 2 }
  1129. Taicpu(hp).resetpass2;
  1130. ObjData.alloc(Taicpu(hp).Pass1(ObjData));
  1131. end;
  1132. ait_cutobject :
  1133. if SmartAsm then
  1134. break;
  1135. end;
  1136. hp:=Tai(hp.next);
  1137. end;
  1138. TreePass0:=hp;
  1139. end;
  1140. function TInternalAssembler.TreePass1(hp:Tai):Tai;
  1141. var
  1142. objsym,
  1143. objsymend : TObjSymbol;
  1144. begin
  1145. while assigned(hp) do
  1146. begin
  1147. case hp.typ of
  1148. ait_align :
  1149. begin
  1150. if tai_align_abstract(hp).aligntype>1 then
  1151. begin
  1152. { here we must determine the fillsize which is used in pass2 }
  1153. Tai_align_abstract(hp).fillsize:=align(ObjData.CurrObjSec.Size,Tai_align_abstract(hp).aligntype)-
  1154. ObjData.CurrObjSec.Size;
  1155. ObjData.alloc(Tai_align_abstract(hp).fillsize);
  1156. end;
  1157. end;
  1158. ait_datablock :
  1159. begin
  1160. if (oso_data in ObjData.CurrObjSec.secoptions) then
  1161. Message(asmw_e_alloc_data_only_in_bss);
  1162. {$ifdef USE_COMM_IN_BSS}
  1163. if writingpackages and
  1164. Tai_datablock(hp).is_global then
  1165. begin
  1166. objsym:=ObjData.SymbolDefine(Tai_datablock(hp).sym);
  1167. objsym.size:=Tai_datablock(hp).size;
  1168. objsym.bind:=AB_COMMON;
  1169. objsym.alignment:=needtowritealignmentalsoforELF;
  1170. end
  1171. else
  1172. {$endif USE_COMM_IN_BSS}
  1173. begin
  1174. ObjData.allocalign(used_align(size_2_align(Tai_datablock(hp).size),0,ObjData.CurrObjSec.secalign));
  1175. objsym:=ObjData.SymbolDefine(Tai_datablock(hp).sym);
  1176. objsym.size:=Tai_datablock(hp).size;
  1177. ObjData.alloc(Tai_datablock(hp).size);
  1178. end;
  1179. end;
  1180. ait_real_80bit :
  1181. ObjData.alloc(tai_real_80bit(hp).savesize);
  1182. ait_real_64bit :
  1183. ObjData.alloc(8);
  1184. ait_real_32bit :
  1185. ObjData.alloc(4);
  1186. ait_comp_64bit :
  1187. ObjData.alloc(8);
  1188. ait_const:
  1189. begin
  1190. { Recalculate relative symbols }
  1191. if assigned(tai_const(hp).sym) and
  1192. assigned(tai_const(hp).endsym) then
  1193. begin
  1194. objsym:=Objdata.SymbolRef(tai_const(hp).sym);
  1195. objsymend:=Objdata.SymbolRef(tai_const(hp).endsym);
  1196. if objsymend.objsection<>objsym.objsection then
  1197. begin
  1198. if (Tai_const(hp).consttype in [aitconst_uleb128bit,aitconst_sleb128bit]) or
  1199. (objsym.objsection<>ObjData.CurrObjSec) then
  1200. internalerror(200905042);
  1201. end
  1202. else
  1203. Tai_const(hp).value:=objsymend.address-objsym.address+Tai_const(hp).symofs;
  1204. end;
  1205. ObjData.alloc(tai_const(hp).size);
  1206. end;
  1207. ait_section:
  1208. begin
  1209. { use cached value }
  1210. ObjData.setsection(Tai_section(hp).sec);
  1211. end;
  1212. ait_stab :
  1213. begin
  1214. if assigned(Tai_stab(hp).str) then
  1215. WriteStab(Tai_stab(hp).str);
  1216. end;
  1217. ait_symbol :
  1218. ObjData.SymbolDefine(Tai_symbol(hp).sym);
  1219. ait_symbol_end :
  1220. begin
  1221. objsym:=ObjData.SymbolRef(Tai_symbol_end(hp).sym);
  1222. objsym.size:=ObjData.CurrObjSec.Size-objsym.offset;
  1223. end;
  1224. ait_label :
  1225. ObjData.SymbolDefine(Tai_label(hp).labsym);
  1226. ait_string :
  1227. ObjData.alloc(Tai_string(hp).len);
  1228. ait_instruction :
  1229. ObjData.alloc(Taicpu(hp).Pass1(ObjData));
  1230. ait_cutobject :
  1231. if SmartAsm then
  1232. break;
  1233. ait_directive :
  1234. begin
  1235. case tai_directive(hp).directive of
  1236. asd_indirect_symbol:
  1237. if tai_directive(hp).name='' then
  1238. Internalerror(2009101103)
  1239. else if not SetIndirectToSymbol(Tai(hp.Previous), tai_directive(hp).name) then
  1240. Internalerror(2009101102);
  1241. asd_lazy_reference:
  1242. { handled in TreePass0 }
  1243. ;
  1244. asd_reference:
  1245. { ignore for now, but should be added}
  1246. ;
  1247. else
  1248. internalerror(2010011102);
  1249. end;
  1250. end;
  1251. end;
  1252. hp:=Tai(hp.next);
  1253. end;
  1254. TreePass1:=hp;
  1255. end;
  1256. function TInternalAssembler.TreePass2(hp:Tai):Tai;
  1257. var
  1258. fillbuffer : tfillbuffer;
  1259. {$ifdef x86}
  1260. co : comp;
  1261. {$endif x86}
  1262. leblen : byte;
  1263. lebbuf : array[0..63] of byte;
  1264. objsym,
  1265. objsymend : TObjSymbol;
  1266. zerobuf : array[0..63] of byte;
  1267. relative_reloc: boolean;
  1268. begin
  1269. fillchar(zerobuf,sizeof(zerobuf),0);
  1270. { main loop }
  1271. while assigned(hp) do
  1272. begin
  1273. case hp.typ of
  1274. ait_align :
  1275. begin
  1276. if tai_align_abstract(hp).aligntype>ObjData.CurrObjSec.secalign then
  1277. InternalError(2012072301);
  1278. if oso_data in ObjData.CurrObjSec.secoptions then
  1279. ObjData.writebytes(Tai_align_abstract(hp).calculatefillbuf(fillbuffer,oso_executable in ObjData.CurrObjSec.secoptions)^,
  1280. Tai_align_abstract(hp).fillsize)
  1281. else
  1282. ObjData.alloc(Tai_align_abstract(hp).fillsize);
  1283. end;
  1284. ait_section :
  1285. begin
  1286. { use cached value }
  1287. ObjData.setsection(Tai_section(hp).sec);
  1288. end;
  1289. ait_symbol :
  1290. begin
  1291. ObjOutput.exportsymbol(ObjData.SymbolRef(Tai_symbol(hp).sym));
  1292. end;
  1293. ait_symbol_end :
  1294. begin
  1295. { recalculate size, as some preceding instructions
  1296. could have been changed to smaller size }
  1297. objsym:=ObjData.SymbolRef(Tai_symbol_end(hp).sym);
  1298. objsym.size:=ObjData.CurrObjSec.Size-objsym.offset;
  1299. end;
  1300. ait_datablock :
  1301. begin
  1302. ObjOutput.exportsymbol(ObjData.SymbolRef(Tai_datablock(hp).sym));
  1303. {$ifdef USE_COMM_IN_BSS}
  1304. if not(writingpackages and
  1305. Tai_datablock(hp).is_global) then
  1306. {$endif USE_COMM_IN_BSS}
  1307. begin
  1308. ObjData.allocalign(used_align(size_2_align(Tai_datablock(hp).size),0,ObjData.CurrObjSec.secalign));
  1309. ObjData.alloc(Tai_datablock(hp).size);
  1310. end;
  1311. end;
  1312. ait_real_80bit :
  1313. begin
  1314. ObjData.writebytes(Tai_real_80bit(hp).value,10);
  1315. ObjData.writebytes(zerobuf,Tai_real_80bit(hp).savesize-10);
  1316. end;
  1317. ait_real_64bit :
  1318. ObjData.writebytes(Tai_real_64bit(hp).value,8);
  1319. ait_real_32bit :
  1320. ObjData.writebytes(Tai_real_32bit(hp).value,4);
  1321. ait_comp_64bit :
  1322. begin
  1323. {$ifdef x86}
  1324. co:=comp(Tai_comp_64bit(hp).value);
  1325. ObjData.writebytes(co,8);
  1326. {$endif x86}
  1327. end;
  1328. ait_string :
  1329. ObjData.writebytes(Tai_string(hp).str^,Tai_string(hp).len);
  1330. ait_const :
  1331. begin
  1332. { Recalculate relative symbols, addresses of forward references
  1333. can be changed in treepass1 }
  1334. relative_reloc:=false;
  1335. if assigned(tai_const(hp).sym) and
  1336. assigned(tai_const(hp).endsym) then
  1337. begin
  1338. objsym:=Objdata.SymbolRef(tai_const(hp).sym);
  1339. objsymend:=Objdata.SymbolRef(tai_const(hp).endsym);
  1340. relative_reloc:=(objsym.objsection<>objsymend.objsection);
  1341. Tai_const(hp).value:=objsymend.address-objsym.address+Tai_const(hp).symofs;
  1342. end;
  1343. case tai_const(hp).consttype of
  1344. aitconst_64bit,
  1345. aitconst_32bit,
  1346. aitconst_16bit,
  1347. aitconst_64bit_unaligned,
  1348. aitconst_32bit_unaligned,
  1349. aitconst_16bit_unaligned,
  1350. aitconst_8bit :
  1351. begin
  1352. if assigned(tai_const(hp).sym) and
  1353. not assigned(tai_const(hp).endsym) then
  1354. ObjData.writereloc(Tai_const(hp).symofs,tai_const(hp).size,Objdata.SymbolRef(tai_const(hp).sym),RELOC_ABSOLUTE)
  1355. else if relative_reloc then
  1356. ObjData.writereloc(ObjData.CurrObjSec.size+tai_const(hp).size-objsym.address+tai_const(hp).symofs,tai_const(hp).size,objsymend,RELOC_RELATIVE)
  1357. else
  1358. ObjData.writebytes(Tai_const(hp).value,tai_const(hp).size);
  1359. end;
  1360. aitconst_rva_symbol :
  1361. begin
  1362. { PE32+? }
  1363. if target_info.system=system_x86_64_win64 then
  1364. ObjData.writereloc(Tai_const(hp).symofs,sizeof(longint),Objdata.SymbolRef(tai_const(hp).sym),RELOC_RVA)
  1365. else
  1366. ObjData.writereloc(Tai_const(hp).symofs,sizeof(pint),Objdata.SymbolRef(tai_const(hp).sym),RELOC_RVA);
  1367. end;
  1368. aitconst_secrel32_symbol :
  1369. begin
  1370. { Required for DWARF2 support under Windows }
  1371. ObjData.writereloc(Tai_const(hp).symofs,sizeof(longint),Objdata.SymbolRef(tai_const(hp).sym),RELOC_SECREL32);
  1372. end;
  1373. aitconst_uleb128bit,
  1374. aitconst_sleb128bit :
  1375. begin
  1376. if tai_const(hp).consttype=aitconst_uleb128bit then
  1377. leblen:=EncodeUleb128(qword(Tai_const(hp).value),lebbuf)
  1378. else
  1379. leblen:=EncodeSleb128(Tai_const(hp).value,lebbuf);
  1380. if leblen<>tai_const(hp).size then
  1381. internalerror(200709271);
  1382. ObjData.writebytes(lebbuf,leblen);
  1383. end;
  1384. aitconst_darwin_dwarf_delta32,
  1385. aitconst_darwin_dwarf_delta64:
  1386. ObjData.writebytes(Tai_const(hp).value,tai_const(hp).size);
  1387. else
  1388. internalerror(200603254);
  1389. end;
  1390. end;
  1391. ait_label :
  1392. begin
  1393. { exporting shouldn't be necessary as labels are local,
  1394. but it's better to be on the safe side (PFV) }
  1395. ObjOutput.exportsymbol(ObjData.SymbolRef(Tai_label(hp).labsym));
  1396. end;
  1397. ait_instruction :
  1398. Taicpu(hp).Pass2(ObjData);
  1399. ait_stab :
  1400. WriteStab(Tai_stab(hp).str);
  1401. ait_function_name,
  1402. ait_force_line : ;
  1403. ait_cutobject :
  1404. if SmartAsm then
  1405. break;
  1406. {$ifndef DISABLE_WIN64_SEH}
  1407. ait_seh_directive :
  1408. tai_seh_directive(hp).generate_code(objdata);
  1409. {$endif DISABLE_WIN64_SEH}
  1410. end;
  1411. hp:=Tai(hp.next);
  1412. end;
  1413. TreePass2:=hp;
  1414. end;
  1415. procedure TInternalAssembler.writetree;
  1416. label
  1417. doexit;
  1418. var
  1419. hp : Tai;
  1420. ObjWriter : TObjectWriter;
  1421. begin
  1422. ObjWriter:=TObjectwriter.create;
  1423. ObjOutput:=CObjOutput.Create(ObjWriter);
  1424. ObjData:=ObjOutput.newObjData(ObjFileName);
  1425. { Pass 0 }
  1426. ObjData.currpass:=0;
  1427. ObjData.createsection(sec_code);
  1428. ObjData.beforealloc;
  1429. { start with list 1 }
  1430. currlistidx:=1;
  1431. currlist:=list[currlistidx];
  1432. hp:=Tai(currList.first);
  1433. while assigned(hp) do
  1434. begin
  1435. hp:=TreePass0(hp);
  1436. MaybeNextList(hp);
  1437. end;
  1438. ObjData.afteralloc;
  1439. { leave if errors have occured }
  1440. if errorcount>0 then
  1441. goto doexit;
  1442. { Pass 1 }
  1443. ObjData.currpass:=1;
  1444. ObjData.resetsections;
  1445. ObjData.beforealloc;
  1446. ObjData.createsection(sec_code);
  1447. { start with list 1 }
  1448. currlistidx:=1;
  1449. currlist:=list[currlistidx];
  1450. hp:=Tai(currList.first);
  1451. while assigned(hp) do
  1452. begin
  1453. hp:=TreePass1(hp);
  1454. MaybeNextList(hp);
  1455. end;
  1456. ObjData.createsection(sec_code);
  1457. ObjData.afteralloc;
  1458. { leave if errors have occured }
  1459. if errorcount>0 then
  1460. goto doexit;
  1461. { Pass 2 }
  1462. ObjData.currpass:=2;
  1463. ObjData.resetsections;
  1464. ObjData.beforewrite;
  1465. ObjData.createsection(sec_code);
  1466. { start with list 1 }
  1467. currlistidx:=1;
  1468. currlist:=list[currlistidx];
  1469. hp:=Tai(currList.first);
  1470. while assigned(hp) do
  1471. begin
  1472. hp:=TreePass2(hp);
  1473. MaybeNextList(hp);
  1474. end;
  1475. ObjData.createsection(sec_code);
  1476. ObjData.afterwrite;
  1477. { don't write the .o file if errors have occured }
  1478. if errorcount=0 then
  1479. begin
  1480. { write objectfile }
  1481. ObjOutput.startobjectfile(ObjFileName);
  1482. ObjOutput.writeobjectfile(ObjData);
  1483. end;
  1484. doexit:
  1485. { Cleanup }
  1486. ObjData.free;
  1487. ObjData:=nil;
  1488. ObjWriter.free;
  1489. end;
  1490. procedure TInternalAssembler.writetreesmart;
  1491. var
  1492. hp : Tai;
  1493. startsectype : TAsmSectiontype;
  1494. place: tcutplace;
  1495. ObjWriter : TObjectWriter;
  1496. begin
  1497. if not(cs_asm_leave in current_settings.globalswitches) then
  1498. ObjWriter:=TARObjectWriter.create(current_module.staticlibfilename)
  1499. else
  1500. ObjWriter:=TObjectwriter.create;
  1501. NextSmartName(cut_normal);
  1502. ObjOutput:=CObjOutput.Create(ObjWriter);
  1503. startsectype:=sec_code;
  1504. { start with list 1 }
  1505. currlistidx:=1;
  1506. currlist:=list[currlistidx];
  1507. hp:=Tai(currList.first);
  1508. while assigned(hp) do
  1509. begin
  1510. ObjData:=ObjOutput.newObjData(ObjFileName);
  1511. { Pass 0 }
  1512. ObjData.currpass:=0;
  1513. ObjData.resetsections;
  1514. ObjData.beforealloc;
  1515. ObjData.createsection(startsectype);
  1516. TreePass0(hp);
  1517. ObjData.afteralloc;
  1518. { leave if errors have occured }
  1519. if errorcount>0 then
  1520. break;
  1521. { Pass 1 }
  1522. ObjData.currpass:=1;
  1523. ObjData.resetsections;
  1524. ObjData.beforealloc;
  1525. ObjData.createsection(startsectype);
  1526. TreePass1(hp);
  1527. ObjData.afteralloc;
  1528. { leave if errors have occured }
  1529. if errorcount>0 then
  1530. break;
  1531. { Pass 2 }
  1532. ObjData.currpass:=2;
  1533. ObjOutput.startobjectfile(ObjFileName);
  1534. ObjData.resetsections;
  1535. ObjData.beforewrite;
  1536. ObjData.createsection(startsectype);
  1537. hp:=TreePass2(hp);
  1538. ObjData.afterwrite;
  1539. { leave if errors have occured }
  1540. if errorcount>0 then
  1541. break;
  1542. { write the current objectfile }
  1543. ObjOutput.writeobjectfile(ObjData);
  1544. ObjData.free;
  1545. ObjData:=nil;
  1546. { end of lists? }
  1547. if not MaybeNextList(hp) then
  1548. break;
  1549. { we will start a new objectfile so reset everything }
  1550. { The place can still change in the next while loop, so don't init }
  1551. { the writer yet (JM) }
  1552. if (hp.typ=ait_cutobject) then
  1553. place := Tai_cutobject(hp).place
  1554. else
  1555. place := cut_normal;
  1556. { avoid empty files }
  1557. startsectype:=sec_code;
  1558. while assigned(hp) and
  1559. (Tai(hp).typ in [ait_marker,ait_comment,ait_section,ait_cutobject]) do
  1560. begin
  1561. if Tai(hp).typ=ait_section then
  1562. startsectype:=Tai_section(hp).sectype;
  1563. if (Tai(hp).typ=ait_cutobject) then
  1564. place:=Tai_cutobject(hp).place;
  1565. hp:=Tai(hp.next);
  1566. end;
  1567. if not MaybeNextList(hp) then
  1568. break;
  1569. { start next objectfile }
  1570. NextSmartName(place);
  1571. end;
  1572. ObjData.free;
  1573. ObjData:=nil;
  1574. ObjWriter.free;
  1575. end;
  1576. procedure TInternalAssembler.MakeObject;
  1577. var to_do:set of TasmlistType;
  1578. i:TasmlistType;
  1579. procedure addlist(p:TAsmList);
  1580. begin
  1581. inc(lists);
  1582. list[lists]:=p;
  1583. end;
  1584. begin
  1585. to_do:=[low(Tasmlisttype)..high(Tasmlisttype)];
  1586. if usedeffileforexports then
  1587. exclude(to_do,al_exports);
  1588. if not(tf_section_threadvars in target_info.flags) then
  1589. exclude(to_do,al_threadvars);
  1590. for i:=low(TasmlistType) to high(TasmlistType) do
  1591. if (i in to_do) and (current_asmdata.asmlists[i]<>nil) then
  1592. addlist(current_asmdata.asmlists[i]);
  1593. if SmartAsm then
  1594. writetreesmart
  1595. else
  1596. writetree;
  1597. end;
  1598. {*****************************************************************************
  1599. Generate Assembler Files Main Procedure
  1600. *****************************************************************************}
  1601. Procedure GenerateAsm(smart:boolean);
  1602. var
  1603. a : TAssembler;
  1604. begin
  1605. if not assigned(CAssembler[target_asm.id]) then
  1606. Message(asmw_f_assembler_output_not_supported);
  1607. a:=CAssembler[target_asm.id].Create(smart);
  1608. a.MakeObject;
  1609. a.Free;
  1610. end;
  1611. Procedure OnlyAsm;
  1612. var
  1613. a : TExternalAssembler;
  1614. begin
  1615. a:=TExternalAssembler.Create(false);
  1616. a.DoAssemble;
  1617. a.Free;
  1618. end;
  1619. {*****************************************************************************
  1620. Init/Done
  1621. *****************************************************************************}
  1622. procedure RegisterAssembler(const r:tasminfo;c:TAssemblerClass);
  1623. var
  1624. t : tasm;
  1625. begin
  1626. t:=r.id;
  1627. if assigned(asminfos[t]) then
  1628. writeln('Warning: Assembler is already registered!')
  1629. else
  1630. Getmem(asminfos[t],sizeof(tasminfo));
  1631. asminfos[t]^:=r;
  1632. CAssembler[t]:=c;
  1633. end;
  1634. end.