assemble.pas 62 KB

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