assemble.pas 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792
  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(TObject)
  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 : TRawByteSearchRec;
  252. begin
  253. if findfirst(FixPath(s,false)+'*'+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. if SmartAsm then
  325. AsmRes.AddAsmCommand(command,para,Name+'('+TosTr(SmartFilesCount)+')')
  326. else
  327. AsmRes.AddAsmCommand(command,para,name);
  328. exit;
  329. end;
  330. try
  331. FlushOutput;
  332. DosExitCode:=RequotedExecuteProcess(command,para);
  333. if DosExitCode<>0
  334. then begin
  335. Message1(exec_e_error_while_assembling,tostr(dosexitcode));
  336. result:=false;
  337. end;
  338. except on E:EOSError do
  339. begin
  340. Message1(exec_e_cant_call_assembler,tostr(E.ErrorCode));
  341. current_settings.globalswitches:=current_settings.globalswitches+[cs_asm_extern];
  342. result:=false;
  343. end;
  344. end;
  345. end;
  346. procedure TExternalAssembler.RemoveAsm;
  347. var
  348. g : file;
  349. begin
  350. if cs_asm_leave in current_settings.globalswitches then
  351. exit;
  352. if cs_asm_extern in current_settings.globalswitches then
  353. AsmRes.AddDeleteCommand(AsmFileName)
  354. else
  355. begin
  356. assign(g,AsmFileName);
  357. {$push} {$I-}
  358. erase(g);
  359. {$pop}
  360. if ioresult<>0 then;
  361. end;
  362. end;
  363. Function TExternalAssembler.DoAssemble:boolean;
  364. begin
  365. DoAssemble:=true;
  366. if DoPipe then
  367. exit;
  368. if not(cs_asm_extern in current_settings.globalswitches) then
  369. begin
  370. if SmartAsm then
  371. begin
  372. if (SmartFilesCount<=1) then
  373. Message1(exec_i_assembling_smart,name);
  374. end
  375. else
  376. Message1(exec_i_assembling,name);
  377. end;
  378. if CallAssembler(FindAssembler,MakeCmdLine) then
  379. RemoveAsm
  380. else
  381. begin
  382. DoAssemble:=false;
  383. GenerateError;
  384. end;
  385. end;
  386. Procedure TExternalAssembler.AsmFlush;
  387. begin
  388. if outcnt>0 then
  389. begin
  390. { suppress i/o error }
  391. {$push} {$I-}
  392. BlockWrite(outfile,outbuf,outcnt);
  393. {$pop}
  394. ioerror:=ioerror or (ioresult<>0);
  395. outcnt:=0;
  396. end;
  397. end;
  398. Procedure TExternalAssembler.AsmClear;
  399. begin
  400. outcnt:=0;
  401. end;
  402. Procedure TExternalAssembler.AsmWrite(const c: char);
  403. begin
  404. if OutCnt+1>=AsmOutSize then
  405. AsmFlush;
  406. OutBuf[OutCnt]:=c;
  407. inc(OutCnt);
  408. inc(AsmSize);
  409. end;
  410. Procedure TExternalAssembler.AsmWrite(const s:string);
  411. begin
  412. if OutCnt+length(s)>=AsmOutSize then
  413. AsmFlush;
  414. Move(s[1],OutBuf[OutCnt],length(s));
  415. inc(OutCnt,length(s));
  416. inc(AsmSize,length(s));
  417. end;
  418. Procedure TExternalAssembler.AsmWrite(const s:ansistring);
  419. var
  420. StartIndex, ToWrite: longint;
  421. begin
  422. if s='' then
  423. exit;
  424. if OutCnt+length(s)>=AsmOutSize then
  425. AsmFlush;
  426. StartIndex:=1;
  427. ToWrite:=length(s);
  428. while ToWrite>AsmOutSize do
  429. begin
  430. Move(s[StartIndex],OutBuf[OutCnt],AsmOutSize);
  431. inc(OutCnt,AsmOutSize);
  432. inc(AsmSize,AsmOutSize);
  433. AsmFlush;
  434. inc(StartIndex,AsmOutSize);
  435. dec(ToWrite,AsmOutSize);
  436. end;
  437. Move(s[StartIndex],OutBuf[OutCnt],ToWrite);
  438. inc(OutCnt,ToWrite);
  439. inc(AsmSize,ToWrite);
  440. end;
  441. procedure TExternalAssembler.AsmWriteLn(const c: char);
  442. begin
  443. AsmWrite(c);
  444. AsmLn;
  445. end;
  446. Procedure TExternalAssembler.AsmWriteLn(const s:string);
  447. begin
  448. AsmWrite(s);
  449. AsmLn;
  450. end;
  451. Procedure TExternalAssembler.AsmWriteLn(const s: ansistring);
  452. begin
  453. AsmWrite(s);
  454. AsmLn;
  455. end;
  456. Procedure TExternalAssembler.AsmWritePChar(p:pchar);
  457. var
  458. i,j : longint;
  459. begin
  460. i:=StrLen(p);
  461. j:=i;
  462. while j>0 do
  463. begin
  464. i:=min(j,AsmOutSize);
  465. if OutCnt+i>=AsmOutSize then
  466. AsmFlush;
  467. Move(p[0],OutBuf[OutCnt],i);
  468. inc(OutCnt,i);
  469. inc(AsmSize,i);
  470. dec(j,i);
  471. p:=pchar(@p[i]);
  472. end;
  473. end;
  474. Procedure TExternalAssembler.AsmLn;
  475. begin
  476. if OutCnt>=AsmOutSize-2 then
  477. AsmFlush;
  478. if (cs_link_on_target in current_settings.globalswitches) then
  479. begin
  480. OutBuf[OutCnt]:=target_info.newline[1];
  481. inc(OutCnt);
  482. inc(AsmSize);
  483. if length(target_info.newline)>1 then
  484. begin
  485. OutBuf[OutCnt]:=target_info.newline[2];
  486. inc(OutCnt);
  487. inc(AsmSize);
  488. end;
  489. end
  490. else
  491. begin
  492. OutBuf[OutCnt]:=source_info.newline[1];
  493. inc(OutCnt);
  494. inc(AsmSize);
  495. if length(source_info.newline)>1 then
  496. begin
  497. OutBuf[OutCnt]:=source_info.newline[2];
  498. inc(OutCnt);
  499. inc(AsmSize);
  500. end;
  501. end;
  502. end;
  503. function TExternalAssembler.MakeCmdLine: TCmdStr;
  504. begin
  505. result:=target_asm.asmcmd;
  506. {$ifdef arm}
  507. if (target_info.system=system_arm_darwin) then
  508. Replace(result,'$ARCH',lower(cputypestr[current_settings.cputype]));
  509. {$endif arm}
  510. if (cs_link_on_target in current_settings.globalswitches) then
  511. begin
  512. Replace(result,'$ASM',maybequoted(ScriptFixFileName(AsmFileName)));
  513. Replace(result,'$OBJ',maybequoted(ScriptFixFileName(ObjFileName)));
  514. end
  515. else
  516. begin
  517. {$ifdef hasunix}
  518. if DoPipe then
  519. Replace(result,'$ASM','')
  520. else
  521. {$endif}
  522. Replace(result,'$ASM',maybequoted(AsmFileName));
  523. Replace(result,'$OBJ',maybequoted(ObjFileName));
  524. end;
  525. if (cs_create_pic in current_settings.moduleswitches) then
  526. Replace(result,'$PIC','-KPIC')
  527. else
  528. Replace(result,'$PIC','');
  529. if (cs_asm_source in current_settings.globalswitches) then
  530. Replace(result,'$NOWARN','')
  531. else
  532. Replace(result,'$NOWARN','-W');
  533. Replace(result,'$EXTRAOPT',asmextraopt);
  534. end;
  535. procedure TExternalAssembler.AsmCreate(Aplace:tcutplace);
  536. {$ifdef hasamiga}
  537. var
  538. tempFileName: TPathStr;
  539. {$endif}
  540. begin
  541. if SmartAsm then
  542. NextSmartName(Aplace);
  543. {$ifdef hasamiga}
  544. { on Amiga/MorphOS try to redirect .s files to the T: assign, which is
  545. for temp files, and usually (default setting) located in the RAM: drive.
  546. This highly improves assembling speed for complex projects like the
  547. compiler itself, especially on hardware with slow disk I/O.
  548. Consider this as a poor man's pipe on Amiga, because real pipe handling
  549. would be much more complex and error prone to implement. (KB) }
  550. if (([cs_asm_extern,cs_asm_leave,cs_link_on_target] * current_settings.globalswitches) = []) then
  551. begin
  552. { try to have an unique name for the .s file }
  553. tempFileName:=HexStr(GetProcessID shr 4,7)+ExtractFileName(AsmFileName);
  554. {$ifndef morphos}
  555. { old Amiga RAM: handler only allows filenames up to 30 char }
  556. if Length(tempFileName) < 30 then
  557. {$endif}
  558. AsmFileName:='T:'+tempFileName;
  559. end;
  560. {$endif}
  561. {$ifdef hasunix}
  562. if DoPipe then
  563. begin
  564. if SmartAsm then
  565. begin
  566. if (SmartFilesCount<=1) then
  567. Message1(exec_i_assembling_smart,name);
  568. end
  569. else
  570. Message1(exec_i_assembling_pipe,AsmFileName);
  571. POpen(outfile,maybequoted(FindAssembler)+' '+MakeCmdLine,'W');
  572. end
  573. else
  574. {$endif}
  575. begin
  576. Assign(outfile,AsmFileName);
  577. {$push} {$I-}
  578. Rewrite(outfile,1);
  579. {$pop}
  580. if ioresult<>0 then
  581. begin
  582. ioerror:=true;
  583. Message1(exec_d_cant_create_asmfile,AsmFileName);
  584. end;
  585. end;
  586. outcnt:=0;
  587. AsmSize:=0;
  588. AsmStartSize:=0;
  589. end;
  590. procedure TExternalAssembler.AsmClose;
  591. var
  592. f : file;
  593. FileAge : longint;
  594. begin
  595. AsmFlush;
  596. {$ifdef hasunix}
  597. if DoPipe then
  598. begin
  599. if PClose(outfile) <> 0 then
  600. GenerateError;
  601. end
  602. else
  603. {$endif}
  604. begin
  605. {Touch Assembler time to ppu time is there is a ppufilename}
  606. if ppufilename<>'' then
  607. begin
  608. Assign(f,ppufilename);
  609. {$push} {$I-}
  610. reset(f,1);
  611. {$pop}
  612. if ioresult=0 then
  613. begin
  614. FileAge := FileGetDate(GetFileHandle(f));
  615. close(f);
  616. reset(outfile,1);
  617. FileSetDate(GetFileHandle(outFile),FileAge);
  618. end;
  619. end;
  620. close(outfile);
  621. end;
  622. end;
  623. procedure TExternalAssembler.WriteSourceLine(hp: tailineinfo);
  624. var
  625. module : tmodule;
  626. begin
  627. { load infile }
  628. if (lastfileinfo.moduleindex<>hp.fileinfo.moduleindex) or
  629. (lastfileinfo.fileindex<>hp.fileinfo.fileindex) then
  630. begin
  631. { in case of a generic the module can be different }
  632. if current_module.unit_index=hp.fileinfo.moduleindex then
  633. module:=current_module
  634. else
  635. module:=get_module(hp.fileinfo.moduleindex);
  636. { during the compilation of the system unit there are cases when
  637. the fileinfo contains just zeros => invalid }
  638. if assigned(module) then
  639. infile:=module.sourcefiles.get_file(hp.fileinfo.fileindex)
  640. else
  641. infile:=nil;
  642. if assigned(infile) then
  643. begin
  644. { open only if needed !! }
  645. if (cs_asm_source in current_settings.globalswitches) then
  646. infile.open;
  647. end;
  648. { avoid unnecessary reopens of the same file !! }
  649. lastfileinfo.fileindex:=hp.fileinfo.fileindex;
  650. lastfileinfo.moduleindex:=hp.fileinfo.moduleindex;
  651. { be sure to change line !! }
  652. lastfileinfo.line:=-1;
  653. end;
  654. { write source }
  655. if (cs_asm_source in current_settings.globalswitches) and
  656. assigned(infile) then
  657. begin
  658. if (infile<>lastinfile) then
  659. begin
  660. AsmWriteLn(target_asm.comment+'['+infile.name+']');
  661. if assigned(lastinfile) then
  662. lastinfile.close;
  663. end;
  664. if (hp.fileinfo.line<>lastfileinfo.line) and
  665. (hp.fileinfo.line<infile.maxlinebuf) then
  666. begin
  667. if (hp.fileinfo.line<>0) and
  668. (infile.linebuf^[hp.fileinfo.line]>=0) then
  669. AsmWriteLn(target_asm.comment+'['+tostr(hp.fileinfo.line)+'] '+
  670. fixline(infile.GetLineStr(hp.fileinfo.line)));
  671. { set it to a negative value !
  672. to make that is has been read already !! PM }
  673. if (infile.linebuf^[hp.fileinfo.line]>=0) then
  674. infile.linebuf^[hp.fileinfo.line]:=-infile.linebuf^[hp.fileinfo.line]-1;
  675. end;
  676. end;
  677. lastfileinfo:=hp.fileinfo;
  678. lastinfile:=infile;
  679. end;
  680. procedure TExternalAssembler.WriteTempalloc(hp: tai_tempalloc);
  681. begin
  682. {$ifdef EXTDEBUG}
  683. if assigned(hp.problem) then
  684. AsmWriteLn(target_asm.comment+'Temp '+tostr(hp.temppos)+','+
  685. tostr(hp.tempsize)+' '+hp.problem^)
  686. else
  687. {$endif EXTDEBUG}
  688. AsmWriteLn(target_asm.comment+'Temp '+tostr(hp.temppos)+','+
  689. tostr(hp.tempsize)+' '+tempallocstr[hp.allocation]);
  690. end;
  691. procedure TExternalAssembler.WriteTree(p:TAsmList);
  692. begin
  693. end;
  694. procedure TExternalAssembler.WriteAsmList;
  695. begin
  696. end;
  697. procedure TExternalAssembler.MakeObject;
  698. begin
  699. AsmCreate(cut_normal);
  700. FillChar(lastfileinfo, sizeof(lastfileinfo), 0);
  701. lastfileinfo.line := -1;
  702. lastinfile := nil;
  703. lastsectype := sec_none;
  704. WriteAsmList;
  705. AsmClose;
  706. if not(ioerror) then
  707. DoAssemble;
  708. end;
  709. {*****************************************************************************
  710. TInternalAssembler
  711. *****************************************************************************}
  712. constructor TInternalAssembler.create(smart:boolean);
  713. begin
  714. inherited create(smart);
  715. ObjOutput:=nil;
  716. ObjData:=nil;
  717. SmartAsm:=smart;
  718. end;
  719. destructor TInternalAssembler.destroy;
  720. begin
  721. if assigned(ObjData) then
  722. ObjData.free;
  723. if assigned(ObjOutput) then
  724. ObjOutput.free;
  725. end;
  726. procedure TInternalAssembler.WriteStab(p:pchar);
  727. function consumecomma(var p:pchar):boolean;
  728. begin
  729. while (p^=' ') do
  730. inc(p);
  731. result:=(p^=',');
  732. inc(p);
  733. end;
  734. function consumenumber(var p:pchar;out value:longint):boolean;
  735. var
  736. hs : string;
  737. len,
  738. code : integer;
  739. begin
  740. value:=0;
  741. while (p^=' ') do
  742. inc(p);
  743. len:=0;
  744. while (p^ in ['0'..'9']) do
  745. begin
  746. inc(len);
  747. hs[len]:=p^;
  748. inc(p);
  749. end;
  750. if len>0 then
  751. begin
  752. hs[0]:=chr(len);
  753. val(hs,value,code);
  754. end
  755. else
  756. code:=-1;
  757. result:=(code=0);
  758. end;
  759. function consumeoffset(var p:pchar;out relocsym:tobjsymbol;out value:longint):boolean;
  760. var
  761. hs : string;
  762. len,
  763. code : integer;
  764. pstart : pchar;
  765. sym : tobjsymbol;
  766. exprvalue : longint;
  767. gotmin,
  768. have_first_symbol,
  769. have_second_symbol,
  770. dosub : boolean;
  771. begin
  772. result:=false;
  773. value:=0;
  774. relocsym:=nil;
  775. gotmin:=false;
  776. have_first_symbol:=false;
  777. have_second_symbol:=false;
  778. repeat
  779. dosub:=false;
  780. exprvalue:=0;
  781. if gotmin then
  782. begin
  783. dosub:=true;
  784. gotmin:=false;
  785. end;
  786. while (p^=' ') do
  787. inc(p);
  788. case p^ of
  789. #0 :
  790. break;
  791. ' ' :
  792. inc(p);
  793. '0'..'9' :
  794. begin
  795. len:=0;
  796. while (p^ in ['0'..'9']) do
  797. begin
  798. inc(len);
  799. hs[len]:=p^;
  800. inc(p);
  801. end;
  802. hs[0]:=chr(len);
  803. val(hs,exprvalue,code);
  804. if code<>0 then
  805. internalerror(200702251);
  806. end;
  807. '.','_',
  808. 'A'..'Z',
  809. 'a'..'z' :
  810. begin
  811. pstart:=p;
  812. while not(p^ in [#0,' ','-','+']) do
  813. inc(p);
  814. len:=p-pstart;
  815. if len>255 then
  816. internalerror(200509187);
  817. move(pstart^,hs[1],len);
  818. hs[0]:=chr(len);
  819. sym:=objdata.symbolref(hs);
  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. fillchar(objsym,sizeof(objsym),0);
  1271. fillchar(objsymend,sizeof(objsymend),0);
  1272. { main loop }
  1273. while assigned(hp) do
  1274. begin
  1275. case hp.typ of
  1276. ait_align :
  1277. begin
  1278. if tai_align_abstract(hp).aligntype>ObjData.CurrObjSec.secalign then
  1279. InternalError(2012072301);
  1280. if oso_data in ObjData.CurrObjSec.secoptions then
  1281. ObjData.writebytes(Tai_align_abstract(hp).calculatefillbuf(fillbuffer,oso_executable in ObjData.CurrObjSec.secoptions)^,
  1282. Tai_align_abstract(hp).fillsize)
  1283. else
  1284. ObjData.alloc(Tai_align_abstract(hp).fillsize);
  1285. end;
  1286. ait_section :
  1287. begin
  1288. { use cached value }
  1289. ObjData.setsection(Tai_section(hp).sec);
  1290. end;
  1291. ait_symbol :
  1292. begin
  1293. ObjOutput.exportsymbol(ObjData.SymbolRef(Tai_symbol(hp).sym));
  1294. end;
  1295. ait_symbol_end :
  1296. begin
  1297. { recalculate size, as some preceding instructions
  1298. could have been changed to smaller size }
  1299. objsym:=ObjData.SymbolRef(Tai_symbol_end(hp).sym);
  1300. objsym.size:=ObjData.CurrObjSec.Size-objsym.offset;
  1301. end;
  1302. ait_datablock :
  1303. begin
  1304. ObjOutput.exportsymbol(ObjData.SymbolRef(Tai_datablock(hp).sym));
  1305. {$ifdef USE_COMM_IN_BSS}
  1306. if not(writingpackages and
  1307. Tai_datablock(hp).is_global) then
  1308. {$endif USE_COMM_IN_BSS}
  1309. begin
  1310. ObjData.allocalign(used_align(size_2_align(Tai_datablock(hp).size),0,ObjData.CurrObjSec.secalign));
  1311. ObjData.alloc(Tai_datablock(hp).size);
  1312. end;
  1313. end;
  1314. ait_real_80bit :
  1315. begin
  1316. ObjData.writebytes(Tai_real_80bit(hp).value,10);
  1317. ObjData.writebytes(zerobuf,Tai_real_80bit(hp).savesize-10);
  1318. end;
  1319. ait_real_64bit :
  1320. ObjData.writebytes(Tai_real_64bit(hp).value,8);
  1321. ait_real_32bit :
  1322. ObjData.writebytes(Tai_real_32bit(hp).value,4);
  1323. ait_comp_64bit :
  1324. begin
  1325. {$ifdef x86}
  1326. co:=comp(Tai_comp_64bit(hp).value);
  1327. ObjData.writebytes(co,8);
  1328. {$endif x86}
  1329. end;
  1330. ait_string :
  1331. ObjData.writebytes(Tai_string(hp).str^,Tai_string(hp).len);
  1332. ait_const :
  1333. begin
  1334. { Recalculate relative symbols, addresses of forward references
  1335. can be changed in treepass1 }
  1336. relative_reloc:=false;
  1337. if assigned(tai_const(hp).sym) and
  1338. assigned(tai_const(hp).endsym) then
  1339. begin
  1340. objsym:=Objdata.SymbolRef(tai_const(hp).sym);
  1341. objsymend:=Objdata.SymbolRef(tai_const(hp).endsym);
  1342. relative_reloc:=(objsym.objsection<>objsymend.objsection);
  1343. Tai_const(hp).value:=objsymend.address-objsym.address+Tai_const(hp).symofs;
  1344. end;
  1345. case tai_const(hp).consttype of
  1346. aitconst_64bit,
  1347. aitconst_32bit,
  1348. aitconst_16bit,
  1349. aitconst_64bit_unaligned,
  1350. aitconst_32bit_unaligned,
  1351. aitconst_16bit_unaligned,
  1352. aitconst_8bit :
  1353. begin
  1354. if assigned(tai_const(hp).sym) and
  1355. not assigned(tai_const(hp).endsym) then
  1356. ObjData.writereloc(Tai_const(hp).symofs,tai_const(hp).size,Objdata.SymbolRef(tai_const(hp).sym),RELOC_ABSOLUTE)
  1357. else if relative_reloc then
  1358. ObjData.writereloc(ObjData.CurrObjSec.size+tai_const(hp).size-objsym.address+tai_const(hp).symofs,tai_const(hp).size,objsymend,RELOC_RELATIVE)
  1359. else
  1360. ObjData.writebytes(Tai_const(hp).value,tai_const(hp).size);
  1361. end;
  1362. aitconst_rva_symbol :
  1363. begin
  1364. { PE32+? }
  1365. if target_info.system=system_x86_64_win64 then
  1366. ObjData.writereloc(Tai_const(hp).symofs,sizeof(longint),Objdata.SymbolRef(tai_const(hp).sym),RELOC_RVA)
  1367. else
  1368. ObjData.writereloc(Tai_const(hp).symofs,sizeof(pint),Objdata.SymbolRef(tai_const(hp).sym),RELOC_RVA);
  1369. end;
  1370. aitconst_secrel32_symbol :
  1371. begin
  1372. { Required for DWARF2 support under Windows }
  1373. ObjData.writereloc(Tai_const(hp).symofs,sizeof(longint),Objdata.SymbolRef(tai_const(hp).sym),RELOC_SECREL32);
  1374. end;
  1375. aitconst_gotoff_symbol:
  1376. ObjData.writereloc(Tai_const(hp).symofs,sizeof(longint),Objdata.SymbolRef(tai_const(hp).sym),RELOC_GOTOFF);
  1377. aitconst_uleb128bit,
  1378. aitconst_sleb128bit :
  1379. begin
  1380. if tai_const(hp).consttype=aitconst_uleb128bit then
  1381. leblen:=EncodeUleb128(qword(Tai_const(hp).value),lebbuf)
  1382. else
  1383. leblen:=EncodeSleb128(Tai_const(hp).value,lebbuf);
  1384. if leblen<>tai_const(hp).size then
  1385. internalerror(200709271);
  1386. ObjData.writebytes(lebbuf,leblen);
  1387. end;
  1388. aitconst_darwin_dwarf_delta32,
  1389. aitconst_darwin_dwarf_delta64:
  1390. ObjData.writebytes(Tai_const(hp).value,tai_const(hp).size);
  1391. else
  1392. internalerror(200603254);
  1393. end;
  1394. end;
  1395. ait_label :
  1396. begin
  1397. { exporting shouldn't be necessary as labels are local,
  1398. but it's better to be on the safe side (PFV) }
  1399. ObjOutput.exportsymbol(ObjData.SymbolRef(Tai_label(hp).labsym));
  1400. end;
  1401. ait_instruction :
  1402. Taicpu(hp).Pass2(ObjData);
  1403. ait_stab :
  1404. WriteStab(Tai_stab(hp).str);
  1405. ait_function_name,
  1406. ait_force_line : ;
  1407. ait_cutobject :
  1408. if SmartAsm then
  1409. break;
  1410. {$ifndef DISABLE_WIN64_SEH}
  1411. ait_seh_directive :
  1412. tai_seh_directive(hp).generate_code(objdata);
  1413. {$endif DISABLE_WIN64_SEH}
  1414. end;
  1415. hp:=Tai(hp.next);
  1416. end;
  1417. TreePass2:=hp;
  1418. end;
  1419. procedure TInternalAssembler.writetree;
  1420. label
  1421. doexit;
  1422. var
  1423. hp : Tai;
  1424. ObjWriter : TObjectWriter;
  1425. begin
  1426. ObjWriter:=TObjectwriter.create;
  1427. ObjOutput:=CObjOutput.Create(ObjWriter);
  1428. ObjData:=ObjOutput.newObjData(ObjFileName);
  1429. { Pass 0 }
  1430. ObjData.currpass:=0;
  1431. ObjData.createsection(sec_code);
  1432. ObjData.beforealloc;
  1433. { start with list 1 }
  1434. currlistidx:=1;
  1435. currlist:=list[currlistidx];
  1436. hp:=Tai(currList.first);
  1437. while assigned(hp) do
  1438. begin
  1439. hp:=TreePass0(hp);
  1440. MaybeNextList(hp);
  1441. end;
  1442. ObjData.afteralloc;
  1443. { leave if errors have occured }
  1444. if errorcount>0 then
  1445. goto doexit;
  1446. { Pass 1 }
  1447. ObjData.currpass:=1;
  1448. ObjData.resetsections;
  1449. ObjData.beforealloc;
  1450. ObjData.createsection(sec_code);
  1451. { start with list 1 }
  1452. currlistidx:=1;
  1453. currlist:=list[currlistidx];
  1454. hp:=Tai(currList.first);
  1455. while assigned(hp) do
  1456. begin
  1457. hp:=TreePass1(hp);
  1458. MaybeNextList(hp);
  1459. end;
  1460. ObjData.createsection(sec_code);
  1461. ObjData.afteralloc;
  1462. { leave if errors have occured }
  1463. if errorcount>0 then
  1464. goto doexit;
  1465. { Pass 2 }
  1466. ObjData.currpass:=2;
  1467. ObjData.resetsections;
  1468. ObjData.beforewrite;
  1469. ObjData.createsection(sec_code);
  1470. { start with list 1 }
  1471. currlistidx:=1;
  1472. currlist:=list[currlistidx];
  1473. hp:=Tai(currList.first);
  1474. while assigned(hp) do
  1475. begin
  1476. hp:=TreePass2(hp);
  1477. MaybeNextList(hp);
  1478. end;
  1479. ObjData.createsection(sec_code);
  1480. ObjData.afterwrite;
  1481. { don't write the .o file if errors have occured }
  1482. if errorcount=0 then
  1483. begin
  1484. { write objectfile }
  1485. ObjOutput.startobjectfile(ObjFileName);
  1486. ObjOutput.writeobjectfile(ObjData);
  1487. end;
  1488. doexit:
  1489. { Cleanup }
  1490. ObjData.free;
  1491. ObjData:=nil;
  1492. ObjWriter.free;
  1493. end;
  1494. procedure TInternalAssembler.writetreesmart;
  1495. var
  1496. hp : Tai;
  1497. startsectype : TAsmSectiontype;
  1498. place: tcutplace;
  1499. ObjWriter : TObjectWriter;
  1500. begin
  1501. if not(cs_asm_leave in current_settings.globalswitches) then
  1502. ObjWriter:=TARObjectWriter.create(current_module.staticlibfilename)
  1503. else
  1504. ObjWriter:=TObjectwriter.create;
  1505. NextSmartName(cut_normal);
  1506. ObjOutput:=CObjOutput.Create(ObjWriter);
  1507. startsectype:=sec_code;
  1508. { start with list 1 }
  1509. currlistidx:=1;
  1510. currlist:=list[currlistidx];
  1511. hp:=Tai(currList.first);
  1512. while assigned(hp) do
  1513. begin
  1514. ObjData:=ObjOutput.newObjData(ObjFileName);
  1515. { Pass 0 }
  1516. ObjData.currpass:=0;
  1517. ObjData.resetsections;
  1518. ObjData.beforealloc;
  1519. ObjData.createsection(startsectype);
  1520. TreePass0(hp);
  1521. ObjData.afteralloc;
  1522. { leave if errors have occured }
  1523. if errorcount>0 then
  1524. break;
  1525. { Pass 1 }
  1526. ObjData.currpass:=1;
  1527. ObjData.resetsections;
  1528. ObjData.beforealloc;
  1529. ObjData.createsection(startsectype);
  1530. TreePass1(hp);
  1531. ObjData.afteralloc;
  1532. { leave if errors have occured }
  1533. if errorcount>0 then
  1534. break;
  1535. { Pass 2 }
  1536. ObjData.currpass:=2;
  1537. ObjOutput.startobjectfile(ObjFileName);
  1538. ObjData.resetsections;
  1539. ObjData.beforewrite;
  1540. ObjData.createsection(startsectype);
  1541. hp:=TreePass2(hp);
  1542. ObjData.afterwrite;
  1543. { leave if errors have occured }
  1544. if errorcount>0 then
  1545. break;
  1546. { write the current objectfile }
  1547. ObjOutput.writeobjectfile(ObjData);
  1548. ObjData.free;
  1549. ObjData:=nil;
  1550. { end of lists? }
  1551. if not MaybeNextList(hp) then
  1552. break;
  1553. { we will start a new objectfile so reset everything }
  1554. { The place can still change in the next while loop, so don't init }
  1555. { the writer yet (JM) }
  1556. if (hp.typ=ait_cutobject) then
  1557. place := Tai_cutobject(hp).place
  1558. else
  1559. place := cut_normal;
  1560. { avoid empty files }
  1561. startsectype:=sec_code;
  1562. while assigned(hp) and
  1563. (Tai(hp).typ in [ait_marker,ait_comment,ait_section,ait_cutobject]) do
  1564. begin
  1565. if Tai(hp).typ=ait_section then
  1566. startsectype:=Tai_section(hp).sectype;
  1567. if (Tai(hp).typ=ait_cutobject) then
  1568. place:=Tai_cutobject(hp).place;
  1569. hp:=Tai(hp.next);
  1570. end;
  1571. if not MaybeNextList(hp) then
  1572. break;
  1573. { start next objectfile }
  1574. NextSmartName(place);
  1575. end;
  1576. ObjData.free;
  1577. ObjData:=nil;
  1578. ObjWriter.free;
  1579. end;
  1580. procedure TInternalAssembler.MakeObject;
  1581. var to_do:set of TasmlistType;
  1582. i:TasmlistType;
  1583. procedure addlist(p:TAsmList);
  1584. begin
  1585. inc(lists);
  1586. list[lists]:=p;
  1587. end;
  1588. begin
  1589. to_do:=[low(Tasmlisttype)..high(Tasmlisttype)];
  1590. if usedeffileforexports then
  1591. exclude(to_do,al_exports);
  1592. if not(tf_section_threadvars in target_info.flags) then
  1593. exclude(to_do,al_threadvars);
  1594. for i:=low(TasmlistType) to high(TasmlistType) do
  1595. if (i in to_do) and (current_asmdata.asmlists[i]<>nil) then
  1596. addlist(current_asmdata.asmlists[i]);
  1597. if SmartAsm then
  1598. writetreesmart
  1599. else
  1600. writetree;
  1601. end;
  1602. {*****************************************************************************
  1603. Generate Assembler Files Main Procedure
  1604. *****************************************************************************}
  1605. Procedure GenerateAsm(smart:boolean);
  1606. var
  1607. a : TAssembler;
  1608. begin
  1609. if not assigned(CAssembler[target_asm.id]) then
  1610. Message(asmw_f_assembler_output_not_supported);
  1611. a:=CAssembler[target_asm.id].Create(smart);
  1612. a.MakeObject;
  1613. a.Free;
  1614. end;
  1615. Procedure OnlyAsm;
  1616. var
  1617. a : TExternalAssembler;
  1618. begin
  1619. a:=TExternalAssembler.Create(false);
  1620. a.DoAssemble;
  1621. a.Free;
  1622. end;
  1623. {*****************************************************************************
  1624. Init/Done
  1625. *****************************************************************************}
  1626. procedure RegisterAssembler(const r:tasminfo;c:TAssemblerClass);
  1627. var
  1628. t : tasm;
  1629. begin
  1630. t:=r.id;
  1631. if assigned(asminfos[t]) then
  1632. writeln('Warning: Assembler is already registered!')
  1633. else
  1634. Getmem(asminfos[t],sizeof(tasminfo));
  1635. asminfos[t]^:=r;
  1636. CAssembler[t]:=c;
  1637. end;
  1638. end.