assemble.pas 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676
  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. {$IFDEF USE_SYSUTILS}
  27. sysutils,
  28. {$ELSE USE_SYSUTILS}
  29. strings,
  30. dos,
  31. {$ENDIF USE_SYSUTILS}
  32. systems,globtype,globals,aasmbase,aasmtai,ogbase;
  33. const
  34. { maximum of aasmoutput lists there will be }
  35. maxoutputlists = 10;
  36. { buffer size for writing the .s file }
  37. AsmOutSize=32768;
  38. type
  39. TAssembler=class(TAbstractAssembler)
  40. public
  41. {filenames}
  42. path : pathstr;
  43. name : namestr;
  44. asmfile, { current .s and .o file }
  45. objfile : string;
  46. ppufilename : string;
  47. asmprefix : string;
  48. SmartAsm : boolean;
  49. SmartFilesCount,
  50. SmartHeaderCount : longint;
  51. Constructor Create(smart:boolean);virtual;
  52. Destructor Destroy;override;
  53. procedure NextSmartName(place:tcutplace);
  54. procedure MakeObject;virtual;abstract;
  55. end;
  56. {# This is the base class which should be overriden for each each
  57. assembler writer. It is used to actually assembler a file,
  58. and write the output to the assembler file.
  59. }
  60. TExternalAssembler=class(TAssembler)
  61. private
  62. procedure CreateSmartLinkPath(const s:string);
  63. protected
  64. {outfile}
  65. AsmSize,
  66. AsmStartSize,
  67. outcnt : longint;
  68. outbuf : array[0..AsmOutSize-1] of char;
  69. outfile : file;
  70. ioerror : boolean;
  71. public
  72. {# Returns the complete path and executable name of the assembler
  73. program.
  74. It first tries looking in the UTIL directory if specified,
  75. otherwise it searches in the free pascal binary directory, in
  76. the current working directory and then in the directories
  77. in the $PATH environment.}
  78. Function FindAssembler:string;
  79. {# Actually does the call to the assembler file. Returns false
  80. if the assembling of the file failed.}
  81. Function CallAssembler(const command:string; const para:TCmdStr):Boolean;
  82. Function DoAssemble:boolean;virtual;
  83. Procedure RemoveAsm;
  84. Procedure AsmFlush;
  85. Procedure AsmClear;
  86. {# Write a string to the assembler file }
  87. Procedure AsmWrite(const s:string);
  88. {# Write a string to the assembler file }
  89. Procedure AsmWritePChar(p:pchar);
  90. {# Write a string to the assembler file followed by a new line }
  91. Procedure AsmWriteLn(const s:string);
  92. {# Write a new line to the assembler file }
  93. Procedure AsmLn;
  94. procedure AsmCreate(Aplace:tcutplace);
  95. procedure AsmClose;
  96. {# This routine should be overriden for each assembler, it is used
  97. to actually write the abstract assembler stream to file.}
  98. procedure WriteTree(p:TAAsmoutput);virtual;
  99. {# This routine should be overriden for each assembler, it is used
  100. to actually write all the different abstract assembler streams
  101. by calling for each stream type, the @var(WriteTree) method.}
  102. procedure WriteAsmList;virtual;
  103. public
  104. Constructor Create(smart:boolean);override;
  105. procedure MakeObject;override;
  106. end;
  107. TInternalAssembler=class(TAssembler)
  108. public
  109. constructor create(smart:boolean);override;
  110. destructor destroy;override;
  111. procedure MakeObject;override;
  112. protected
  113. objectdata : TAsmObjectData;
  114. objectoutput : tobjectoutput;
  115. private
  116. { the aasmoutput lists that need to be processed }
  117. lists : byte;
  118. list : array[1..maxoutputlists] of TAAsmoutput;
  119. { current processing }
  120. currlistidx : byte;
  121. currlist : TAAsmoutput;
  122. currpass : byte;
  123. {$ifdef GDB}
  124. n_line : byte; { different types of source lines }
  125. linecount,
  126. includecount : longint;
  127. funcname : tasmsymbol;
  128. stabslastfileinfo : tfileposinfo;
  129. procedure convertstabs(p:pchar);
  130. procedure emitlineinfostabs(nidx,line : longint);
  131. procedure emitstabs(s:string);
  132. procedure WriteFileLineInfo(var fileinfo : tfileposinfo);
  133. procedure StartFileLineInfo;
  134. procedure EndFileLineInfo;
  135. {$endif}
  136. function MaybeNextList(var hp:Tai):boolean;
  137. function TreePass0(hp:Tai):Tai;
  138. function TreePass1(hp:Tai):Tai;
  139. function TreePass2(hp:Tai):Tai;
  140. procedure writetree;
  141. procedure writetreesmart;
  142. end;
  143. TAssemblerClass = class of TAssembler;
  144. Procedure GenerateAsm(smart:boolean);
  145. Procedure OnlyAsm;
  146. procedure RegisterAssembler(const r:tasminfo;c:TAssemblerClass);
  147. procedure InitAssembler;
  148. procedure DoneAssembler;
  149. Implementation
  150. uses
  151. {$ifdef hasunix}
  152. {$ifdef havelinuxrtl10}
  153. linux,
  154. {$else}
  155. unix,
  156. {$endif}
  157. {$endif}
  158. cutils,script,fmodule,verbose,
  159. {$ifdef memdebug}
  160. cclasses,
  161. {$endif memdebug}
  162. {$ifdef GDB}
  163. finput,
  164. gdb,
  165. {$endif GDB}
  166. {$ifdef m68k}
  167. cpuinfo,
  168. {$endif m68k}
  169. aasmcpu
  170. ;
  171. var
  172. CAssembler : array[tasm] of TAssemblerClass;
  173. {*****************************************************************************
  174. TAssembler
  175. *****************************************************************************}
  176. Constructor TAssembler.Create(smart:boolean);
  177. begin
  178. { load start values }
  179. asmfile:=current_module.get_asmfilename;
  180. objfile:=current_module.objfilename^;
  181. name:=Lower(current_module.modulename^);
  182. path:=current_module.outputpath^;
  183. asmprefix := current_module.asmprefix^;
  184. if not assigned(current_module.outputpath) then
  185. ppufilename := ''
  186. else
  187. ppufilename := current_module.ppufilename^;
  188. SmartAsm:=smart;
  189. SmartFilesCount:=0;
  190. SmartHeaderCount:=0;
  191. SmartLinkOFiles.Clear;
  192. end;
  193. Destructor TAssembler.Destroy;
  194. begin
  195. end;
  196. procedure TAssembler.NextSmartName(place:tcutplace);
  197. var
  198. s : string;
  199. begin
  200. inc(SmartFilesCount);
  201. if SmartFilesCount>999999 then
  202. Message(asmw_f_too_many_asm_files);
  203. case place of
  204. cut_begin :
  205. begin
  206. inc(SmartHeaderCount);
  207. s:=asmprefix+tostr(SmartHeaderCount)+'h';
  208. end;
  209. cut_normal :
  210. s:=asmprefix+tostr(SmartHeaderCount)+'s';
  211. cut_end :
  212. s:=asmprefix+tostr(SmartHeaderCount)+'t';
  213. end;
  214. AsmFile:=Path+FixFileName(s+tostr(SmartFilesCount)+target_info.asmext);
  215. ObjFile:=Path+FixFileName(s+tostr(SmartFilesCount)+target_info.objext);
  216. { insert in container so it can be cleared after the linking }
  217. SmartLinkOFiles.Insert(Objfile);
  218. end;
  219. {*****************************************************************************
  220. TExternalAssembler
  221. *****************************************************************************}
  222. Function DoPipe:boolean;
  223. begin
  224. DoPipe:=(cs_asm_pipe in aktglobalswitches) and
  225. not(cs_asm_leave in aktglobalswitches)
  226. and ((aktoutputformat in [as_gas,as_darwin]));
  227. end;
  228. Constructor TExternalAssembler.Create(smart:boolean);
  229. begin
  230. inherited Create(smart);
  231. if SmartAsm then
  232. begin
  233. path:=FixPath(path+FixFileName(name)+target_info.smartext,false);
  234. CreateSmartLinkPath(path);
  235. end;
  236. Outcnt:=0;
  237. end;
  238. procedure TExternalAssembler.CreateSmartLinkPath(const s:string);
  239. var
  240. {$IFDEF USE_SYSUTILS}
  241. dir : TSearchRec;
  242. {$ELSE USE_SYSUTILS}
  243. dir : searchrec;
  244. {$ENDIF USE_SYSUTILS}
  245. hs : string;
  246. begin
  247. if PathExists(s) then
  248. begin
  249. { the path exists, now we clean only all the .o and .s files }
  250. { .o files }
  251. {$IFDEF USE_SYSUTILS}
  252. if findfirst(s+source_info.dirsep+'*'+target_info.objext,faAnyFile,dir) = 0
  253. then repeat
  254. RemoveFile(s+source_info.dirsep+dir.name);
  255. until findnext(dir) <> 0;
  256. {$ELSE USE_SYSUTILS}
  257. findfirst(s+source_info.dirsep+'*'+target_info.objext,anyfile,dir);
  258. while (doserror=0) do
  259. begin
  260. RemoveFile(s+source_info.dirsep+dir.name);
  261. findnext(dir);
  262. end;
  263. {$ENDIF USE_SYSUTILS}
  264. findclose(dir);
  265. { .s files }
  266. {$IFDEF USE_SYSUTILS}
  267. if findfirst(s+source_info.dirsep+'*'+target_info.asmext,faAnyFile,dir) = 0
  268. then repeat
  269. RemoveFile(s+source_info.dirsep+dir.name);
  270. until findnext(dir) <> 0;
  271. {$ELSE USE_SYSUTILS}
  272. findfirst(s+source_info.dirsep+'*'+target_info.asmext,anyfile,dir);
  273. while (doserror=0) do
  274. begin
  275. RemoveFile(s+source_info.dirsep+dir.name);
  276. findnext(dir);
  277. end;
  278. {$ENDIF USE_SYSUTILS}
  279. findclose(dir);
  280. end
  281. else
  282. begin
  283. hs:=s;
  284. if hs[length(hs)] in ['/','\'] then
  285. delete(hs,length(hs),1);
  286. {$I-}
  287. mkdir(hs);
  288. {$I+}
  289. if ioresult<>0 then;
  290. end;
  291. end;
  292. const
  293. lastas : byte=255;
  294. var
  295. LastASBin : pathstr;
  296. Function TExternalAssembler.FindAssembler:string;
  297. var
  298. asfound : boolean;
  299. UtilExe : string;
  300. begin
  301. asfound:=false;
  302. if cs_link_on_target in aktglobalswitches then
  303. begin
  304. { If linking on target, don't add any path PM }
  305. FindAssembler:=utilsprefix+AddExtension(target_asm.asmbin,target_info.exeext);
  306. exit;
  307. end
  308. else
  309. UtilExe:=utilsprefix+AddExtension(target_asm.asmbin,source_info.exeext);
  310. if lastas<>ord(target_asm.id) then
  311. begin
  312. lastas:=ord(target_asm.id);
  313. { is an assembler passed ? }
  314. if utilsdirectory<>'' then
  315. asfound:=FindFile(UtilExe,utilsdirectory,LastASBin);
  316. if not AsFound then
  317. asfound:=FindExe(UtilExe,LastASBin);
  318. if (not asfound) and not(cs_asm_extern in aktglobalswitches) then
  319. begin
  320. Message1(exec_e_assembler_not_found,LastASBin);
  321. aktglobalswitches:=aktglobalswitches+[cs_asm_extern];
  322. end;
  323. if asfound then
  324. Message1(exec_t_using_assembler,LastASBin);
  325. end;
  326. FindAssembler:=LastASBin;
  327. end;
  328. Function TExternalAssembler.CallAssembler(const command:string; const para:TCmdStr):Boolean;
  329. {$IFDEF USE_SYSUTILS}
  330. var
  331. DosExitCode:Integer;
  332. {$ENDIF USE_SYSUTILS}
  333. begin
  334. callassembler:=true;
  335. if not(cs_asm_extern in aktglobalswitches) then
  336. {$IFDEF USE_SYSUTILS}
  337. try
  338. DosExitCode := ExecuteProcess(command,para);
  339. if DosExitCode <>0
  340. then begin
  341. Message1(exec_e_error_while_assembling,tostr(dosexitcode));
  342. callassembler:=false;
  343. end;
  344. except on E:EOSError do
  345. begin
  346. Message1(exec_e_cant_call_assembler,tostr(E.ErrorCode));
  347. aktglobalswitches:=aktglobalswitches+[cs_asm_extern];
  348. callassembler:=false;
  349. end
  350. end
  351. {$ELSE USE_SYSUTILS}
  352. begin
  353. swapvectors;
  354. exec(maybequoted(command),para);
  355. swapvectors;
  356. if (doserror<>0) then
  357. begin
  358. Message1(exec_e_cant_call_assembler,tostr(doserror));
  359. aktglobalswitches:=aktglobalswitches+[cs_asm_extern];
  360. callassembler:=false;
  361. end
  362. else
  363. if (dosexitcode<>0) then
  364. begin
  365. Message1(exec_e_error_while_assembling,tostr(dosexitcode));
  366. callassembler:=false;
  367. end;
  368. end
  369. {$ENDIF USE_SYSUTILS}
  370. else
  371. AsmRes.AddAsmCommand(command,para,name);
  372. end;
  373. procedure TExternalAssembler.RemoveAsm;
  374. var
  375. g : file;
  376. begin
  377. if cs_asm_leave in aktglobalswitches then
  378. exit;
  379. if cs_asm_extern in aktglobalswitches then
  380. AsmRes.AddDeleteCommand(AsmFile)
  381. else
  382. begin
  383. assign(g,AsmFile);
  384. {$I-}
  385. erase(g);
  386. {$I+}
  387. if ioresult<>0 then;
  388. end;
  389. end;
  390. Function TExternalAssembler.DoAssemble:boolean;
  391. var
  392. s : TCmdStr;
  393. begin
  394. DoAssemble:=true;
  395. if DoPipe then
  396. exit;
  397. if not(cs_asm_extern in aktglobalswitches) then
  398. begin
  399. if SmartAsm then
  400. begin
  401. if (SmartFilesCount<=1) then
  402. Message1(exec_i_assembling_smart,name);
  403. end
  404. else
  405. Message1(exec_i_assembling,name);
  406. end;
  407. s:=target_asm.asmcmd;
  408. {$ifdef m68k}
  409. if aktoptprocessor = MC68020 then
  410. s:='-m68020 '+s
  411. else
  412. s:='-m68000 '+s;
  413. {$endif}
  414. if (cs_link_on_target in aktglobalswitches) then
  415. begin
  416. Replace(s,'$ASM',maybequoted(ScriptFixFileName(AsmFile)));
  417. Replace(s,'$OBJ',maybequoted(ScriptFixFileName(ObjFile)));
  418. end
  419. else
  420. begin
  421. Replace(s,'$ASM',maybequoted(AsmFile));
  422. Replace(s,'$OBJ',maybequoted(ObjFile));
  423. end;
  424. if CallAssembler(FindAssembler,s) then
  425. RemoveAsm
  426. else
  427. begin
  428. DoAssemble:=false;
  429. GenerateError;
  430. end;
  431. end;
  432. Procedure TExternalAssembler.AsmFlush;
  433. begin
  434. if outcnt>0 then
  435. begin
  436. { suppress i/o error }
  437. {$i-}
  438. BlockWrite(outfile,outbuf,outcnt);
  439. {$i+}
  440. ioerror:=ioerror or (ioresult<>0);
  441. outcnt:=0;
  442. end;
  443. end;
  444. Procedure TExternalAssembler.AsmClear;
  445. begin
  446. outcnt:=0;
  447. end;
  448. Procedure TExternalAssembler.AsmWrite(const s:string);
  449. begin
  450. if OutCnt+length(s)>=AsmOutSize then
  451. AsmFlush;
  452. Move(s[1],OutBuf[OutCnt],length(s));
  453. inc(OutCnt,length(s));
  454. inc(AsmSize,length(s));
  455. end;
  456. Procedure TExternalAssembler.AsmWriteLn(const s:string);
  457. begin
  458. AsmWrite(s);
  459. AsmLn;
  460. end;
  461. Procedure TExternalAssembler.AsmWritePChar(p:pchar);
  462. var
  463. i,j : longint;
  464. begin
  465. i:=StrLen(p);
  466. j:=i;
  467. while j>0 do
  468. begin
  469. i:=min(j,AsmOutSize);
  470. if OutCnt+i>=AsmOutSize then
  471. AsmFlush;
  472. Move(p[0],OutBuf[OutCnt],i);
  473. inc(OutCnt,i);
  474. inc(AsmSize,i);
  475. dec(j,i);
  476. p:=pchar(@p[i]);
  477. end;
  478. end;
  479. Procedure TExternalAssembler.AsmLn;
  480. begin
  481. if OutCnt>=AsmOutSize-2 then
  482. AsmFlush;
  483. if (cs_link_on_target in aktglobalswitches) then
  484. begin
  485. OutBuf[OutCnt]:=target_info.newline[1];
  486. inc(OutCnt);
  487. inc(AsmSize);
  488. if length(target_info.newline)>1 then
  489. begin
  490. OutBuf[OutCnt]:=target_info.newline[2];
  491. inc(OutCnt);
  492. inc(AsmSize);
  493. end;
  494. end
  495. else
  496. begin
  497. OutBuf[OutCnt]:=source_info.newline[1];
  498. inc(OutCnt);
  499. inc(AsmSize);
  500. if length(source_info.newline)>1 then
  501. begin
  502. OutBuf[OutCnt]:=source_info.newline[2];
  503. inc(OutCnt);
  504. inc(AsmSize);
  505. end;
  506. end;
  507. end;
  508. procedure TExternalAssembler.AsmCreate(Aplace:tcutplace);
  509. begin
  510. if SmartAsm then
  511. NextSmartName(Aplace);
  512. {$ifdef hasunix}
  513. if DoPipe then
  514. begin
  515. Message1(exec_i_assembling_pipe,asmfile);
  516. POpen(outfile,'as -o '+objfile,'W');
  517. end
  518. else
  519. {$endif}
  520. begin
  521. Assign(outfile,asmfile);
  522. {$I-}
  523. Rewrite(outfile,1);
  524. {$I+}
  525. if ioresult<>0 then
  526. begin
  527. ioerror:=true;
  528. Message1(exec_d_cant_create_asmfile,asmfile);
  529. end;
  530. end;
  531. outcnt:=0;
  532. AsmSize:=0;
  533. AsmStartSize:=0;
  534. end;
  535. procedure TExternalAssembler.AsmClose;
  536. var
  537. f : file;
  538. FileAge : longint;
  539. begin
  540. AsmFlush;
  541. {$ifdef hasunix}
  542. if DoPipe then
  543. begin
  544. if PClose(outfile) <> 0 then
  545. GenerateError;
  546. end
  547. else
  548. {$endif}
  549. begin
  550. {Touch Assembler time to ppu time is there is a ppufilename}
  551. if ppufilename<>'' then
  552. begin
  553. Assign(f,ppufilename);
  554. {$I-}
  555. reset(f,1);
  556. {$I+}
  557. if ioresult=0 then
  558. begin
  559. {$IFDEF USE_SYSUTILS}
  560. FileAge := FileGetDate(GetFileHandle(f));
  561. {$ELSE USE_SYSUTILS}
  562. GetFTime(f, FileAge);
  563. {$ENDIF USE_SYSUTILS}
  564. close(f);
  565. reset(outfile,1);
  566. {$IFDEF USE_SYSUTILS}
  567. FileSetDate(GetFileHandle(outFile),FileAge);
  568. {$ELSE USE_SYSUTILS}
  569. SetFTime(f, FileAge);
  570. {$ENDIF USE_SYSUTILS}
  571. end;
  572. end;
  573. close(outfile);
  574. end;
  575. end;
  576. procedure TExternalAssembler.WriteTree(p:TAAsmoutput);
  577. begin
  578. end;
  579. procedure TExternalAssembler.WriteAsmList;
  580. begin
  581. end;
  582. procedure TExternalAssembler.MakeObject;
  583. begin
  584. AsmCreate(cut_normal);
  585. WriteAsmList;
  586. AsmClose;
  587. if not(ioerror) then
  588. DoAssemble;
  589. end;
  590. {*****************************************************************************
  591. TInternalAssembler
  592. *****************************************************************************}
  593. constructor TInternalAssembler.create(smart:boolean);
  594. begin
  595. inherited create(smart);
  596. objectoutput:=nil;
  597. objectdata:=nil;
  598. SmartAsm:=smart;
  599. currpass:=0;
  600. end;
  601. destructor TInternalAssembler.destroy;
  602. {$ifdef MEMDEBUG}
  603. var
  604. d : tmemdebug;
  605. {$endif}
  606. begin
  607. {$ifdef MEMDEBUG}
  608. d := tmemdebug.create(name+' - agbin');
  609. {$endif}
  610. objectdata.free;
  611. objectoutput.free;
  612. {$ifdef MEMDEBUG}
  613. d.free;
  614. {$endif}
  615. end;
  616. {$ifdef GDB}
  617. procedure TInternalAssembler.convertstabs(p:pchar);
  618. var
  619. ofs,
  620. nidx,nother,ii,i,line,j : longint;
  621. code : integer;
  622. hp : pchar;
  623. reloc : boolean;
  624. ps : tasmsymbol;
  625. s : string;
  626. begin
  627. ofs:=0;
  628. reloc:=true;
  629. ps:=nil;
  630. if p[0]='"' then
  631. begin
  632. i:=1;
  633. { we can have \" inside the string !! PM }
  634. while not ((p[i]='"') and (p[i-1]<>'\')) do
  635. inc(i);
  636. p[i]:=#0;
  637. ii:=i;
  638. hp:=@p[1];
  639. s:=StrPas(@P[i+2]);
  640. end
  641. else
  642. begin
  643. hp:=nil;
  644. s:=StrPas(P);
  645. i:=-2; {needed below (PM) }
  646. end;
  647. { When in pass 1 then only alloc and leave }
  648. if currpass=1 then
  649. begin
  650. objectdata.allocstabs(hp);
  651. if assigned(hp) then
  652. p[i]:='"';
  653. exit;
  654. end;
  655. { Parse the rest of the stabs }
  656. if s='' then
  657. internalerror(33000);
  658. j:=pos(',',s);
  659. if j=0 then
  660. internalerror(33001);
  661. Val(Copy(s,1,j-1),nidx,code);
  662. if code<>0 then
  663. internalerror(33002);
  664. i:=i+2+j;
  665. Delete(s,1,j);
  666. j:=pos(',',s);
  667. if (j=0) then
  668. internalerror(33003);
  669. Val(Copy(s,1,j-1),nother,code);
  670. if code<>0 then
  671. internalerror(33004);
  672. i:=i+j;
  673. Delete(s,1,j);
  674. j:=pos(',',s);
  675. if j=0 then
  676. begin
  677. j:=256;
  678. ofs:=-1;
  679. end;
  680. Val(Copy(s,1,j-1),line,code);
  681. if code<>0 then
  682. internalerror(33005);
  683. if ofs=0 then
  684. begin
  685. Delete(s,1,j);
  686. i:=i+j;
  687. Val(s,ofs,code);
  688. if code=0 then
  689. reloc:=false
  690. else
  691. begin
  692. ofs:=0;
  693. s:=strpas(@p[i]);
  694. { handle asmsymbol or
  695. asmsymbol - asmsymbol }
  696. j:=pos(' ',s);
  697. if j=0 then
  698. j:=pos('-',s);
  699. { also try to handle
  700. asmsymbol + constant
  701. or
  702. asmsymbol - constant }
  703. if j=0 then
  704. j:=pos('+',s);
  705. if j<>0 then
  706. begin
  707. Val(Copy(s,j+1,255),ofs,code);
  708. if code<>0 then
  709. ofs:=0
  710. else
  711. { constant reading successful,
  712. avoid further treatment by
  713. setting s[j] to '+' }
  714. s[j]:='+';
  715. end
  716. else
  717. { single asmsymbol }
  718. j:=256;
  719. { the symbol can be external
  720. so we must use newasmsymbol and
  721. not getasmsymbol !! PM }
  722. ps:=objectlibrary.newasmsymbol(copy(s,1,j-1),AB_EXTERNAL,AT_NONE);
  723. if not assigned(ps) then
  724. internalerror(33006)
  725. else
  726. begin
  727. ofs:=ofs+ps.address;
  728. reloc:=true;
  729. objectlibrary.UsedAsmSymbolListInsert(ps);
  730. end;
  731. if (j<256) and (s[j]<>'+') then
  732. begin
  733. i:=i+j;
  734. s:=strpas(@p[i]);
  735. if (s<>'') and (s[1]=' ') then
  736. begin
  737. j:=0;
  738. while (s[j+1]=' ') do
  739. inc(j);
  740. i:=i+j;
  741. s:=strpas(@p[i]);
  742. end;
  743. ps:=objectlibrary.getasmsymbol(s);
  744. if not assigned(ps) then
  745. internalerror(33007)
  746. else
  747. begin
  748. if ps.section<>objectdata.currsec then
  749. internalerror(33008);
  750. ofs:=ofs-ps.address;
  751. reloc:=false;
  752. objectlibrary.UsedAsmSymbolListInsert(ps);
  753. end;
  754. end;
  755. end;
  756. end;
  757. { External references (AB_EXTERNAL and AB_COMMON) need a symbol relocation }
  758. if assigned(ps) and (ps.currbind in [AB_EXTERNAL,AB_COMMON]) then
  759. begin
  760. if currpass=2 then
  761. begin
  762. objectdata.writesymbol(ps);
  763. objectoutput.exportsymbol(ps);
  764. end;
  765. objectdata.writeSymStabs(ofs,hp,ps,nidx,nother,line,reloc)
  766. end
  767. else
  768. objectdata.writeStabs(ofs,hp,nidx,nother,line,reloc);
  769. if assigned(hp) then
  770. p[ii]:='"';
  771. end;
  772. procedure TInternalAssembler.emitlineinfostabs(nidx,line : longint);
  773. begin
  774. if currpass=1 then
  775. begin
  776. objectdata.allocstabs(nil);
  777. exit;
  778. end;
  779. if (nidx=n_textline) and assigned(funcname) and
  780. (target_info.use_function_relative_addresses) then
  781. objectdata.writeStabs(objectdata.currsec.datasize-funcname.address,nil,nidx,0,line,false)
  782. else
  783. objectdata.writeStabs(objectdata.currsec.datasize,nil,nidx,0,line,true);
  784. end;
  785. procedure TInternalAssembler.emitstabs(s:string);
  786. begin
  787. s:=s+#0;
  788. ConvertStabs(@s[1]);
  789. end;
  790. procedure TInternalAssembler.WriteFileLineInfo(var fileinfo : tfileposinfo);
  791. var
  792. curr_n : byte;
  793. hp : tasmsymbol;
  794. infile : tinputfile;
  795. begin
  796. if not ((cs_debuginfo in aktmoduleswitches) or
  797. (cs_gdb_lineinfo in aktglobalswitches)) then
  798. exit;
  799. { file changed ? (must be before line info) }
  800. if (fileinfo.fileindex<>0) and
  801. (stabslastfileinfo.fileindex<>fileinfo.fileindex) then
  802. begin
  803. infile:=current_module.sourcefiles.get_file(fileinfo.fileindex);
  804. if assigned(infile) then
  805. begin
  806. if includecount=0 then
  807. curr_n:=n_sourcefile
  808. else
  809. curr_n:=n_includefile;
  810. { get symbol for this includefile }
  811. hp:=objectlibrary.newasmsymbol('Ltext'+ToStr(IncludeCount),AB_LOCAL,AT_FUNCTION);
  812. if currpass=1 then
  813. begin
  814. objectdata.allocsymbol(currpass,hp,0);
  815. objectlibrary.UsedAsmSymbolListInsert(hp);
  816. end
  817. else
  818. objectdata.writesymbol(hp);
  819. { emit stabs }
  820. if (infile.path^<>'') then
  821. EmitStabs('"'+BsToSlash(FixPath(infile.path^,false))+'",'+tostr(curr_n)+
  822. ',0,0,Ltext'+ToStr(IncludeCount));
  823. EmitStabs('"'+FixFileName(infile.name^)+'",'+tostr(curr_n)+
  824. ',0,0,Ltext'+ToStr(IncludeCount));
  825. inc(includecount);
  826. { force new line info }
  827. stabslastfileinfo.line:=-1;
  828. end;
  829. end;
  830. { line changed ? }
  831. if (stabslastfileinfo.line<>fileinfo.line) and (fileinfo.line<>0) then
  832. emitlineinfostabs(n_line,fileinfo.line);
  833. stabslastfileinfo:=fileinfo;
  834. end;
  835. procedure TInternalAssembler.StartFileLineInfo;
  836. var
  837. fileinfo : tfileposinfo;
  838. begin
  839. FillChar(stabslastfileinfo,sizeof(stabslastfileinfo),0);
  840. n_line:=n_bssline;
  841. funcname:=nil;
  842. linecount:=1;
  843. includecount:=0;
  844. fileinfo.fileindex:=1;
  845. fileinfo.line:=1;
  846. WriteFileLineInfo(fileinfo);
  847. end;
  848. procedure TInternalAssembler.EndFileLineInfo;
  849. var
  850. hp : tasmsymbol;
  851. begin
  852. if not ((cs_debuginfo in aktmoduleswitches) or
  853. (cs_gdb_lineinfo in aktglobalswitches)) then
  854. exit;
  855. objectdata.createsection(sec_code,'',0,[]);
  856. hp:=objectlibrary.newasmsymbol('Letext',AB_LOCAL,AT_FUNCTION);
  857. if currpass=1 then
  858. begin
  859. objectdata.allocsymbol(currpass,hp,0);
  860. objectlibrary.UsedAsmSymbolListInsert(hp);
  861. end
  862. else
  863. objectdata.writesymbol(hp);
  864. EmitStabs('"",'+tostr(n_sourcefile)+',0,0,Letext');
  865. end;
  866. {$endif GDB}
  867. function TInternalAssembler.MaybeNextList(var hp:Tai):boolean;
  868. begin
  869. { maybe end of list }
  870. while not assigned(hp) do
  871. begin
  872. if currlistidx<lists then
  873. begin
  874. inc(currlistidx);
  875. currlist:=list[currlistidx];
  876. hp:=Tai(currList.first);
  877. end
  878. else
  879. begin
  880. MaybeNextList:=false;
  881. exit;
  882. end;
  883. end;
  884. MaybeNextList:=true;
  885. end;
  886. function TInternalAssembler.TreePass0(hp:Tai):Tai;
  887. var
  888. l : longint;
  889. begin
  890. while assigned(hp) do
  891. begin
  892. case hp.typ of
  893. ait_align :
  894. begin
  895. { always use the maximum fillsize in this pass to avoid possible
  896. short jumps to become out of range }
  897. Tai_align(hp).fillsize:=Tai_align(hp).aligntype;
  898. objectdata.alloc(Tai_align(hp).fillsize);
  899. end;
  900. ait_datablock :
  901. begin
  902. l:=used_align(size_2_align(Tai_datablock(hp).size),0,objectdata.currsec.addralign);
  903. if SmartAsm or (not Tai_datablock(hp).is_global) then
  904. begin
  905. objectdata.allocalign(l);
  906. objectdata.alloc(Tai_datablock(hp).size);
  907. end;
  908. end;
  909. ait_real_80bit :
  910. objectdata.alloc(10);
  911. ait_real_64bit :
  912. objectdata.alloc(8);
  913. ait_real_32bit :
  914. objectdata.alloc(4);
  915. ait_comp_64bit :
  916. objectdata.alloc(8);
  917. ait_const_64bit,
  918. ait_const_32bit,
  919. ait_const_16bit,
  920. ait_const_8bit,
  921. ait_const_rva_symbol,
  922. ait_const_indirect_symbol :
  923. objectdata.alloc(tai_const(hp).size);
  924. ait_section:
  925. begin
  926. objectdata.CreateSection(Tai_section(hp).sectype,Tai_section(hp).name^,Tai_section(hp).secalign,[]);
  927. Tai_section(hp).sec:=objectdata.CurrSec;
  928. end;
  929. ait_symbol :
  930. objectdata.allocsymbol(currpass,Tai_symbol(hp).sym,0);
  931. ait_label :
  932. objectdata.allocsymbol(currpass,Tai_label(hp).l,0);
  933. ait_string :
  934. objectdata.alloc(Tai_string(hp).len);
  935. ait_instruction :
  936. begin
  937. {$ifdef i386}
  938. {$ifndef NOAG386BIN}
  939. { reset instructions which could change in pass 2 }
  940. Taicpu(hp).resetpass2;
  941. objectdata.alloc(Taicpu(hp).Pass1(objectdata.currsec.datasize));
  942. {$endif NOAG386BIN}
  943. {$endif i386}
  944. end;
  945. ait_cutobject :
  946. if SmartAsm then
  947. break;
  948. end;
  949. hp:=Tai(hp.next);
  950. end;
  951. TreePass0:=hp;
  952. end;
  953. function TInternalAssembler.TreePass1(hp:Tai):Tai;
  954. var
  955. InlineLevel,
  956. l : longint;
  957. {$ifdef i386}
  958. {$ifndef NOAG386BIN}
  959. i : longint;
  960. {$endif NOAG386BIN}
  961. {$endif i386}
  962. begin
  963. inlinelevel:=0;
  964. while assigned(hp) do
  965. begin
  966. {$ifdef GDB}
  967. { write stabs, no line info for inlined code }
  968. if (inlinelevel=0) and
  969. ((cs_debuginfo in aktmoduleswitches) or
  970. (cs_gdb_lineinfo in aktglobalswitches)) then
  971. begin
  972. if (objectdata.currsec<>nil) and
  973. not(hp.typ in SkipLineInfo) then
  974. WriteFileLineInfo(tailineinfo(hp).fileinfo);
  975. end;
  976. {$endif GDB}
  977. case hp.typ of
  978. ait_align :
  979. begin
  980. { here we must determine the fillsize which is used in pass2 }
  981. Tai_align(hp).fillsize:=align(objectdata.currsec.datasize,Tai_align(hp).aligntype)-
  982. objectdata.currsec.datasize;
  983. objectdata.alloc(Tai_align(hp).fillsize);
  984. end;
  985. ait_datablock :
  986. begin
  987. if not (objectdata.currsec.sectype in [sec_bss,sec_tbss]) then
  988. Message(asmw_e_alloc_data_only_in_bss);
  989. l:=used_align(size_2_align(Tai_datablock(hp).size),0,objectdata.currsec.addralign);
  990. if Tai_datablock(hp).is_global and
  991. not SmartAsm then
  992. begin
  993. objectdata.allocsymbol(currpass,Tai_datablock(hp).sym,Tai_datablock(hp).size);
  994. { force to be common/external, must be after setaddress as that would
  995. set it to AB_GLOBAL }
  996. Tai_datablock(hp).sym.currbind:=AB_COMMON;
  997. end
  998. else
  999. begin
  1000. objectdata.allocalign(l);
  1001. objectdata.allocsymbol(currpass,Tai_datablock(hp).sym,Tai_datablock(hp).size);
  1002. objectdata.alloc(Tai_datablock(hp).size);
  1003. end;
  1004. objectlibrary.UsedAsmSymbolListInsert(Tai_datablock(hp).sym);
  1005. end;
  1006. ait_real_80bit :
  1007. objectdata.alloc(10);
  1008. ait_real_64bit :
  1009. objectdata.alloc(8);
  1010. ait_real_32bit :
  1011. objectdata.alloc(4);
  1012. ait_comp_64bit :
  1013. objectdata.alloc(8);
  1014. ait_const_64bit,
  1015. ait_const_32bit,
  1016. ait_const_16bit,
  1017. ait_const_8bit,
  1018. ait_const_rva_symbol,
  1019. ait_const_indirect_symbol :
  1020. begin
  1021. objectdata.alloc(tai_const(hp).size);
  1022. if assigned(Tai_const(hp).sym) then
  1023. objectlibrary.UsedAsmSymbolListInsert(Tai_const(hp).sym);
  1024. if assigned(Tai_const(hp).endsym) then
  1025. objectlibrary.UsedAsmSymbolListInsert(Tai_const(hp).endsym);
  1026. end;
  1027. ait_section:
  1028. begin
  1029. { use cached value }
  1030. objectdata.setsection(Tai_section(hp).sec);
  1031. {$ifdef GDB}
  1032. case Tai_section(hp).sectype of
  1033. sec_code :
  1034. n_line:=n_textline;
  1035. sec_data :
  1036. n_line:=n_dataline;
  1037. sec_bss :
  1038. n_line:=n_bssline;
  1039. else
  1040. n_line:=n_dataline;
  1041. end;
  1042. stabslastfileinfo.line:=-1;
  1043. {$endif GDB}
  1044. end;
  1045. {$ifdef GDB}
  1046. ait_stabn :
  1047. begin
  1048. if assigned(Tai_stabn(hp).str) then
  1049. convertstabs(Tai_stabn(hp).str);
  1050. end;
  1051. ait_stabs :
  1052. begin
  1053. if assigned(Tai_stabs(hp).str) then
  1054. convertstabs(Tai_stabs(hp).str);
  1055. end;
  1056. ait_stab_function_name :
  1057. begin
  1058. if assigned(Tai_stab_function_name(hp).str) then
  1059. begin
  1060. funcname:=objectlibrary.getasmsymbol(strpas(Tai_stab_function_name(hp).str));
  1061. objectlibrary.UsedAsmSymbolListInsert(funcname);
  1062. end
  1063. else
  1064. funcname:=nil;
  1065. end;
  1066. ait_force_line :
  1067. stabslastfileinfo.line:=0;
  1068. {$endif}
  1069. ait_symbol :
  1070. begin
  1071. objectdata.allocsymbol(currpass,Tai_symbol(hp).sym,0);
  1072. objectlibrary.UsedAsmSymbolListInsert(Tai_symbol(hp).sym);
  1073. end;
  1074. ait_symbol_end :
  1075. begin
  1076. if target_info.system in [system_i386_linux,system_i386_beos] then
  1077. begin
  1078. Tai_symbol_end(hp).sym.size:=objectdata.currsec.datasize-Tai_symbol_end(hp).sym.address;
  1079. objectlibrary.UsedAsmSymbolListInsert(Tai_symbol_end(hp).sym);
  1080. end;
  1081. end;
  1082. ait_label :
  1083. begin
  1084. objectdata.allocsymbol(currpass,Tai_label(hp).l,0);
  1085. objectlibrary.UsedAsmSymbolListInsert(Tai_label(hp).l);
  1086. end;
  1087. ait_string :
  1088. objectdata.alloc(Tai_string(hp).len);
  1089. ait_instruction :
  1090. begin
  1091. {$ifdef i386}
  1092. {$ifndef NOAG386BIN}
  1093. objectdata.alloc(Taicpu(hp).Pass1(objectdata.currsec.datasize));
  1094. { fixup the references }
  1095. for i:=1 to Taicpu(hp).ops do
  1096. begin
  1097. with Taicpu(hp).oper[i-1]^ do
  1098. begin
  1099. case typ of
  1100. top_ref :
  1101. begin
  1102. if assigned(ref^.symbol) then
  1103. objectlibrary.UsedAsmSymbolListInsert(ref^.symbol);
  1104. if assigned(ref^.relsymbol) then
  1105. objectlibrary.UsedAsmSymbolListInsert(ref^.symbol);
  1106. end;
  1107. end;
  1108. end;
  1109. end;
  1110. {$endif NOAG386BIN}
  1111. {$endif i386}
  1112. end;
  1113. ait_direct :
  1114. Message(asmw_f_direct_not_supported);
  1115. ait_cutobject :
  1116. if SmartAsm then
  1117. break;
  1118. ait_marker :
  1119. if tai_marker(hp).kind=InlineStart then
  1120. inc(InlineLevel)
  1121. else if tai_marker(hp).kind=InlineEnd then
  1122. dec(InlineLevel);
  1123. end;
  1124. hp:=Tai(hp.next);
  1125. end;
  1126. TreePass1:=hp;
  1127. end;
  1128. function TInternalAssembler.TreePass2(hp:Tai):Tai;
  1129. var
  1130. fillbuffer : tfillbuffer;
  1131. InlineLevel,
  1132. l : longint;
  1133. v : int64;
  1134. {$ifdef x86}
  1135. co : comp;
  1136. {$endif x86}
  1137. begin
  1138. inlinelevel:=0;
  1139. { main loop }
  1140. while assigned(hp) do
  1141. begin
  1142. {$ifdef GDB}
  1143. { write stabs, no line info for inlined code }
  1144. if (inlinelevel=0) and
  1145. ((cs_debuginfo in aktmoduleswitches) or
  1146. (cs_gdb_lineinfo in aktglobalswitches)) then
  1147. begin
  1148. if (objectdata.currsec<>nil) and
  1149. not(hp.typ in SkipLineInfo) then
  1150. WriteFileLineInfo(tailineinfo(hp).fileinfo);
  1151. end;
  1152. {$endif GDB}
  1153. case hp.typ of
  1154. ait_align :
  1155. begin
  1156. if objectdata.currsec.sectype in [sec_bss,sec_tbss] then
  1157. objectdata.alloc(Tai_align(hp).fillsize)
  1158. else
  1159. objectdata.writebytes(Tai_align(hp).calculatefillbuf(fillbuffer)^,Tai_align(hp).fillsize);
  1160. end;
  1161. ait_section :
  1162. begin
  1163. { use cached value }
  1164. objectdata.setsection(Tai_section(hp).sec);
  1165. {$ifdef GDB}
  1166. case Tai_section(hp).sectype of
  1167. sec_code : n_line:=n_textline;
  1168. sec_data : n_line:=n_dataline;
  1169. sec_bss : n_line:=n_bssline;
  1170. else
  1171. n_line:=n_dataline;
  1172. end;
  1173. stabslastfileinfo.line:=-1;
  1174. {$endif GDB}
  1175. end;
  1176. ait_symbol :
  1177. begin
  1178. objectdata.writesymbol(Tai_symbol(hp).sym);
  1179. objectoutput.exportsymbol(Tai_symbol(hp).sym);
  1180. end;
  1181. ait_datablock :
  1182. begin
  1183. l:=used_align(size_2_align(Tai_datablock(hp).size),0,objectdata.currsec.addralign);
  1184. objectdata.writesymbol(Tai_datablock(hp).sym);
  1185. objectoutput.exportsymbol(Tai_datablock(hp).sym);
  1186. if SmartAsm or (not Tai_datablock(hp).is_global) then
  1187. begin
  1188. objectdata.allocalign(l);
  1189. objectdata.alloc(Tai_datablock(hp).size);
  1190. end;
  1191. end;
  1192. ait_real_80bit :
  1193. objectdata.writebytes(Tai_real_80bit(hp).value,10);
  1194. ait_real_64bit :
  1195. objectdata.writebytes(Tai_real_64bit(hp).value,8);
  1196. ait_real_32bit :
  1197. objectdata.writebytes(Tai_real_32bit(hp).value,4);
  1198. ait_comp_64bit :
  1199. begin
  1200. {$ifdef x86}
  1201. {$ifdef FPC}
  1202. co:=comp(Tai_comp_64bit(hp).value);
  1203. {$else}
  1204. co:=Tai_comp_64bit(hp).value;
  1205. {$endif}
  1206. objectdata.writebytes(co,8);
  1207. {$endif x86}
  1208. end;
  1209. ait_string :
  1210. objectdata.writebytes(Tai_string(hp).str^,Tai_string(hp).len);
  1211. ait_const_64bit,
  1212. ait_const_32bit,
  1213. ait_const_16bit,
  1214. ait_const_8bit :
  1215. begin
  1216. if assigned(tai_const(hp).sym) then
  1217. begin
  1218. if assigned(tai_const(hp).endsym) then
  1219. begin
  1220. if tai_const(hp).endsym.section<>tai_const(hp).sym.section then
  1221. internalerror(200404124);
  1222. v:=tai_const(hp).endsym.address-tai_const(hp).sym.address+Tai_const(hp).value;
  1223. objectdata.writebytes(v,tai_const(hp).size);
  1224. end
  1225. else
  1226. objectdata.writereloc(Tai_const(hp).value,Tai_const(hp).size,Tai_const(hp).sym,RELOC_ABSOLUTE);
  1227. end
  1228. else
  1229. objectdata.writebytes(Tai_const(hp).value,tai_const(hp).size);
  1230. end;
  1231. ait_const_rva_symbol :
  1232. objectdata.writereloc(Tai_const(hp).value,sizeof(aint),Tai_const(hp).sym,RELOC_RVA);
  1233. ait_label :
  1234. begin
  1235. objectdata.writesymbol(Tai_label(hp).l);
  1236. { exporting shouldn't be necessary as labels are local,
  1237. but it's better to be on the safe side (PFV) }
  1238. objectoutput.exportsymbol(Tai_label(hp).l);
  1239. end;
  1240. {$ifdef i386}
  1241. {$ifndef NOAG386BIN}
  1242. ait_instruction :
  1243. Taicpu(hp).Pass2(objectdata);
  1244. {$endif NOAG386BIN}
  1245. {$endif i386}
  1246. {$ifdef GDB}
  1247. ait_stabn :
  1248. convertstabs(Tai_stabn(hp).str);
  1249. ait_stabs :
  1250. convertstabs(Tai_stabs(hp).str);
  1251. ait_stab_function_name :
  1252. if assigned(Tai_stab_function_name(hp).str) then
  1253. funcname:=objectlibrary.getasmsymbol(strpas(Tai_stab_function_name(hp).str))
  1254. else
  1255. funcname:=nil;
  1256. ait_force_line :
  1257. stabslastfileinfo.line:=0;
  1258. {$endif}
  1259. ait_cutobject :
  1260. if SmartAsm then
  1261. break;
  1262. ait_marker :
  1263. if tai_marker(hp).kind=InlineStart then
  1264. inc(InlineLevel)
  1265. else if tai_marker(hp).kind=InlineEnd then
  1266. dec(InlineLevel);
  1267. end;
  1268. hp:=Tai(hp.next);
  1269. end;
  1270. TreePass2:=hp;
  1271. end;
  1272. procedure TInternalAssembler.writetree;
  1273. var
  1274. hp : Tai;
  1275. label
  1276. doexit;
  1277. begin
  1278. objectdata:=objectoutput.newobjectdata(Objfile);
  1279. { reset the asmsymbol list }
  1280. objectlibrary.CreateUsedAsmsymbolList;
  1281. { Pass 0 }
  1282. currpass:=0;
  1283. objectdata.createsection(sec_code,'',0,[]);
  1284. objectdata.beforealloc;
  1285. { start with list 1 }
  1286. currlistidx:=1;
  1287. currlist:=list[currlistidx];
  1288. hp:=Tai(currList.first);
  1289. while assigned(hp) do
  1290. begin
  1291. hp:=TreePass0(hp);
  1292. MaybeNextList(hp);
  1293. end;
  1294. objectdata.afteralloc;
  1295. { leave if errors have occured }
  1296. if errorcount>0 then
  1297. goto doexit;
  1298. { Pass 1 }
  1299. currpass:=1;
  1300. objectdata.resetsections;
  1301. objectdata.beforealloc;
  1302. objectdata.createsection(sec_code,'',0,[]);
  1303. {$ifdef GDB}
  1304. StartFileLineInfo;
  1305. {$endif GDB}
  1306. { start with list 1 }
  1307. currlistidx:=1;
  1308. currlist:=list[currlistidx];
  1309. hp:=Tai(currList.first);
  1310. while assigned(hp) do
  1311. begin
  1312. hp:=TreePass1(hp);
  1313. MaybeNextList(hp);
  1314. end;
  1315. {$ifdef GDB}
  1316. EndFileLineInfo;
  1317. {$endif GDB}
  1318. objectdata.afteralloc;
  1319. { check for undefined labels and reset }
  1320. objectlibrary.UsedAsmSymbolListCheckUndefined;
  1321. { leave if errors have occured }
  1322. if errorcount>0 then
  1323. goto doexit;
  1324. { Pass 2 }
  1325. currpass:=2;
  1326. objectdata.resetsections;
  1327. objectdata.beforewrite;
  1328. objectdata.createsection(sec_code,'',0,[]);
  1329. {$ifdef GDB}
  1330. StartFileLineInfo;
  1331. {$endif GDB}
  1332. { start with list 1 }
  1333. currlistidx:=1;
  1334. currlist:=list[currlistidx];
  1335. hp:=Tai(currList.first);
  1336. while assigned(hp) do
  1337. begin
  1338. hp:=TreePass2(hp);
  1339. MaybeNextList(hp);
  1340. end;
  1341. {$ifdef GDB}
  1342. EndFileLineInfo;
  1343. {$endif GDB}
  1344. objectdata.afterwrite;
  1345. { don't write the .o file if errors have occured }
  1346. if errorcount=0 then
  1347. begin
  1348. { write objectfile }
  1349. objectoutput.startobjectfile(ObjFile);
  1350. objectoutput.writeobjectfile(objectdata);
  1351. objectdata.free;
  1352. objectdata:=nil;
  1353. end;
  1354. doexit:
  1355. { reset the used symbols back, must be after the .o has been
  1356. written }
  1357. objectlibrary.UsedAsmsymbolListReset;
  1358. objectlibrary.DestroyUsedAsmsymbolList;
  1359. end;
  1360. procedure TInternalAssembler.writetreesmart;
  1361. var
  1362. hp : Tai;
  1363. startsectype : TAsmSectionType;
  1364. place: tcutplace;
  1365. begin
  1366. NextSmartName(cut_normal);
  1367. objectdata:=objectoutput.newobjectdata(Objfile);
  1368. startsectype:=sec_code;
  1369. { start with list 1 }
  1370. currlistidx:=1;
  1371. currlist:=list[currlistidx];
  1372. hp:=Tai(currList.first);
  1373. while assigned(hp) do
  1374. begin
  1375. { reset the asmsymbol list }
  1376. objectlibrary.CreateUsedAsmSymbolList;
  1377. { Pass 0 }
  1378. currpass:=0;
  1379. objectdata.resetsections;
  1380. objectdata.beforealloc;
  1381. objectdata.createsection(startsectype,'',0,[]);
  1382. TreePass0(hp);
  1383. objectdata.afteralloc;
  1384. { leave if errors have occured }
  1385. if errorcount>0 then
  1386. exit;
  1387. { Pass 1 }
  1388. currpass:=1;
  1389. objectdata.resetsections;
  1390. objectdata.beforealloc;
  1391. objectdata.createsection(startsectype,'',0,[]);
  1392. {$ifdef GDB}
  1393. StartFileLineInfo;
  1394. {$endif GDB}
  1395. TreePass1(hp);
  1396. {$ifdef GDB}
  1397. EndFileLineInfo;
  1398. {$endif GDB}
  1399. objectdata.afteralloc;
  1400. { check for undefined labels }
  1401. objectlibrary.UsedAsmSymbolListCheckUndefined;
  1402. { leave if errors have occured }
  1403. if errorcount>0 then
  1404. exit;
  1405. { Pass 2 }
  1406. currpass:=2;
  1407. objectoutput.startobjectfile(Objfile);
  1408. objectdata.resetsections;
  1409. objectdata.beforewrite;
  1410. objectdata.createsection(startsectype,'',0,[]);
  1411. {$ifdef GDB}
  1412. StartFileLineInfo;
  1413. {$endif GDB}
  1414. hp:=TreePass2(hp);
  1415. { save section type for next loop, must be done before EndFileLineInfo
  1416. because that changes the section to sec_code }
  1417. startsectype:=objectdata.currsec.sectype;
  1418. {$ifdef GDB}
  1419. EndFileLineInfo;
  1420. {$endif GDB}
  1421. objectdata.afterwrite;
  1422. { leave if errors have occured }
  1423. if errorcount>0 then
  1424. exit;
  1425. { write the current objectfile }
  1426. objectoutput.writeobjectfile(objectdata);
  1427. objectdata.free;
  1428. objectdata:=nil;
  1429. { reset the used symbols back, must be after the .o has been
  1430. written }
  1431. objectlibrary.UsedAsmsymbolListReset;
  1432. objectlibrary.DestroyUsedAsmsymbolList;
  1433. { end of lists? }
  1434. if not MaybeNextList(hp) then
  1435. break;
  1436. { we will start a new objectfile so reset everything }
  1437. { The place can still change in the next while loop, so don't init }
  1438. { the writer yet (JM) }
  1439. if (hp.typ=ait_cutobject) then
  1440. place := Tai_cutobject(hp).place
  1441. else
  1442. place := cut_normal;
  1443. { avoid empty files }
  1444. while assigned(hp) and
  1445. (Tai(hp).typ in [ait_marker,ait_comment,ait_section,ait_cutobject]) do
  1446. begin
  1447. if Tai(hp).typ=ait_section then
  1448. startsectype:=Tai_section(hp).sectype
  1449. else if (Tai(hp).typ=ait_cutobject) then
  1450. place:=Tai_cutobject(hp).place;
  1451. hp:=Tai(hp.next);
  1452. end;
  1453. { there is a problem if startsectype is sec_none !! PM }
  1454. if startsectype=sec_none then
  1455. startsectype:=sec_code;
  1456. if not MaybeNextList(hp) then
  1457. break;
  1458. { start next objectfile }
  1459. NextSmartName(place);
  1460. objectdata:=objectoutput.newobjectdata(Objfile);
  1461. end;
  1462. end;
  1463. procedure TInternalAssembler.MakeObject;
  1464. var to_do:set of Tasmlist;
  1465. i:Tasmlist;
  1466. procedure addlist(p:TAAsmoutput);
  1467. begin
  1468. inc(lists);
  1469. list[lists]:=p;
  1470. end;
  1471. begin
  1472. to_do:=[low(Tasmlist)..high(Tasmlist)];
  1473. if not(cs_debuginfo in aktmoduleswitches) then
  1474. exclude(to_do,debuglist);
  1475. if usedeffileforexports then
  1476. exclude(to_do,exportsection);
  1477. {$warning TODO internal writer support for dwarf}
  1478. exclude(to_do,dwarflist);
  1479. for i:=low(Tasmlist) to high(Tasmlist) do
  1480. if (i in to_do) and (asmlist[i]<>nil) then
  1481. addlist(asmlist[i]);
  1482. if SmartAsm then
  1483. writetreesmart
  1484. else
  1485. writetree;
  1486. end;
  1487. {*****************************************************************************
  1488. Generate Assembler Files Main Procedure
  1489. *****************************************************************************}
  1490. Procedure GenerateAsm(smart:boolean);
  1491. var
  1492. a : TAssembler;
  1493. begin
  1494. if not assigned(CAssembler[target_asm.id]) then
  1495. Message(asmw_f_assembler_output_not_supported);
  1496. a:=CAssembler[target_asm.id].Create(smart);
  1497. a.MakeObject;
  1498. a.Free;
  1499. end;
  1500. Procedure OnlyAsm;
  1501. var
  1502. a : TExternalAssembler;
  1503. begin
  1504. a:=TExternalAssembler.Create(false);
  1505. a.DoAssemble;
  1506. a.Free;
  1507. end;
  1508. {*****************************************************************************
  1509. Init/Done
  1510. *****************************************************************************}
  1511. procedure RegisterAssembler(const r:tasminfo;c:TAssemblerClass);
  1512. var
  1513. t : tasm;
  1514. begin
  1515. t:=r.id;
  1516. if assigned(asminfos[t]) then
  1517. writeln('Warning: Assembler is already registered!')
  1518. else
  1519. Getmem(asminfos[t],sizeof(tasminfo));
  1520. asminfos[t]^:=r;
  1521. CAssembler[t]:=c;
  1522. end;
  1523. procedure InitAssembler;
  1524. begin
  1525. { target_asm is already set by readarguments }
  1526. initoutputformat:=target_asm.id;
  1527. aktoutputformat:=target_asm.id;
  1528. end;
  1529. procedure DoneAssembler;
  1530. begin
  1531. end;
  1532. end.