assemble.pas 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689
  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. {$ifdef i386}
  509. const format_option='--32';
  510. {$else}{$ifdef x86_64}
  511. const format_option='--64';
  512. {$else}
  513. const format_option='';
  514. {$endif}{$endif}
  515. procedure TExternalAssembler.AsmCreate(Aplace:tcutplace);
  516. begin
  517. if SmartAsm then
  518. NextSmartName(Aplace);
  519. {$ifdef hasunix}
  520. if DoPipe then
  521. begin
  522. Message1(exec_i_assembling_pipe,asmfile);
  523. POpen(outfile,'as '+format_option+' -o '+objfile,'W');
  524. end
  525. else
  526. {$endif}
  527. begin
  528. Assign(outfile,asmfile);
  529. {$I-}
  530. Rewrite(outfile,1);
  531. {$I+}
  532. if ioresult<>0 then
  533. begin
  534. ioerror:=true;
  535. Message1(exec_d_cant_create_asmfile,asmfile);
  536. end;
  537. end;
  538. outcnt:=0;
  539. AsmSize:=0;
  540. AsmStartSize:=0;
  541. end;
  542. procedure TExternalAssembler.AsmClose;
  543. var
  544. f : file;
  545. FileAge : longint;
  546. begin
  547. AsmFlush;
  548. {$ifdef hasunix}
  549. if DoPipe then
  550. begin
  551. if PClose(outfile) <> 0 then
  552. GenerateError;
  553. end
  554. else
  555. {$endif}
  556. begin
  557. {Touch Assembler time to ppu time is there is a ppufilename}
  558. if ppufilename<>'' then
  559. begin
  560. Assign(f,ppufilename);
  561. {$I-}
  562. reset(f,1);
  563. {$I+}
  564. if ioresult=0 then
  565. begin
  566. {$IFDEF USE_SYSUTILS}
  567. FileAge := FileGetDate(GetFileHandle(f));
  568. {$ELSE USE_SYSUTILS}
  569. GetFTime(f, FileAge);
  570. {$ENDIF USE_SYSUTILS}
  571. close(f);
  572. reset(outfile,1);
  573. {$IFDEF USE_SYSUTILS}
  574. FileSetDate(GetFileHandle(outFile),FileAge);
  575. {$ELSE USE_SYSUTILS}
  576. SetFTime(f, FileAge);
  577. {$ENDIF USE_SYSUTILS}
  578. end;
  579. end;
  580. close(outfile);
  581. end;
  582. end;
  583. procedure TExternalAssembler.WriteTree(p:TAAsmoutput);
  584. begin
  585. end;
  586. procedure TExternalAssembler.WriteAsmList;
  587. begin
  588. end;
  589. procedure TExternalAssembler.MakeObject;
  590. begin
  591. AsmCreate(cut_normal);
  592. WriteAsmList;
  593. AsmClose;
  594. if not(ioerror) then
  595. DoAssemble;
  596. end;
  597. {*****************************************************************************
  598. TInternalAssembler
  599. *****************************************************************************}
  600. constructor TInternalAssembler.create(smart:boolean);
  601. begin
  602. inherited create(smart);
  603. objectoutput:=nil;
  604. objectdata:=nil;
  605. SmartAsm:=smart;
  606. currpass:=0;
  607. end;
  608. destructor TInternalAssembler.destroy;
  609. {$ifdef MEMDEBUG}
  610. var
  611. d : tmemdebug;
  612. {$endif}
  613. begin
  614. {$ifdef MEMDEBUG}
  615. d := tmemdebug.create(name+' - agbin');
  616. {$endif}
  617. objectdata.free;
  618. objectoutput.free;
  619. {$ifdef MEMDEBUG}
  620. d.free;
  621. {$endif}
  622. end;
  623. {$ifdef GDB}
  624. procedure TInternalAssembler.convertstabs(p:pchar);
  625. var
  626. ofs,
  627. nidx,nother,ii,i,line,j : longint;
  628. code : integer;
  629. hp : pchar;
  630. reloc : boolean;
  631. ps : tasmsymbol;
  632. s : string;
  633. begin
  634. ofs:=0;
  635. reloc:=true;
  636. ps:=nil;
  637. if p[0]='"' then
  638. begin
  639. i:=1;
  640. { we can have \" inside the string !! PM }
  641. while not ((p[i]='"') and (p[i-1]<>'\')) do
  642. inc(i);
  643. p[i]:=#0;
  644. ii:=i;
  645. hp:=@p[1];
  646. s:=StrPas(@P[i+2]);
  647. end
  648. else
  649. begin
  650. hp:=nil;
  651. s:=StrPas(P);
  652. i:=-2; {needed below (PM) }
  653. end;
  654. { When in pass 1 then only alloc and leave }
  655. if currpass=1 then
  656. begin
  657. objectdata.allocstabs(hp);
  658. if assigned(hp) then
  659. p[i]:='"';
  660. exit;
  661. end;
  662. { Parse the rest of the stabs }
  663. if s='' then
  664. internalerror(33000);
  665. j:=pos(',',s);
  666. if j=0 then
  667. internalerror(33001);
  668. Val(Copy(s,1,j-1),nidx,code);
  669. if code<>0 then
  670. internalerror(33002);
  671. i:=i+2+j;
  672. Delete(s,1,j);
  673. j:=pos(',',s);
  674. if (j=0) then
  675. internalerror(33003);
  676. Val(Copy(s,1,j-1),nother,code);
  677. if code<>0 then
  678. internalerror(33004);
  679. i:=i+j;
  680. Delete(s,1,j);
  681. j:=pos(',',s);
  682. if j=0 then
  683. begin
  684. j:=256;
  685. ofs:=-1;
  686. end;
  687. Val(Copy(s,1,j-1),line,code);
  688. if code<>0 then
  689. internalerror(33005);
  690. if ofs=0 then
  691. begin
  692. Delete(s,1,j);
  693. i:=i+j;
  694. Val(s,ofs,code);
  695. if code=0 then
  696. reloc:=false
  697. else
  698. begin
  699. ofs:=0;
  700. s:=strpas(@p[i]);
  701. { handle asmsymbol or
  702. asmsymbol - asmsymbol }
  703. j:=pos(' ',s);
  704. if j=0 then
  705. j:=pos('-',s);
  706. { also try to handle
  707. asmsymbol + constant
  708. or
  709. asmsymbol - constant }
  710. if j=0 then
  711. j:=pos('+',s);
  712. if j<>0 then
  713. begin
  714. Val(Copy(s,j+1,255),ofs,code);
  715. if code<>0 then
  716. ofs:=0
  717. else
  718. { constant reading successful,
  719. avoid further treatment by
  720. setting s[j] to '+' }
  721. s[j]:='+';
  722. end
  723. else
  724. { single asmsymbol }
  725. j:=256;
  726. { the symbol can be external
  727. so we must use newasmsymbol and
  728. not getasmsymbol !! PM }
  729. ps:=objectlibrary.newasmsymbol(copy(s,1,j-1),AB_EXTERNAL,AT_NONE);
  730. if not assigned(ps) then
  731. internalerror(33006)
  732. else
  733. begin
  734. ofs:=ofs+ps.address;
  735. reloc:=true;
  736. objectlibrary.UsedAsmSymbolListInsert(ps);
  737. end;
  738. if (j<256) and (s[j]<>'+') then
  739. begin
  740. i:=i+j;
  741. s:=strpas(@p[i]);
  742. if (s<>'') and (s[1]=' ') then
  743. begin
  744. j:=0;
  745. while (s[j+1]=' ') do
  746. inc(j);
  747. i:=i+j;
  748. s:=strpas(@p[i]);
  749. end;
  750. ps:=objectlibrary.getasmsymbol(s);
  751. if not assigned(ps) then
  752. internalerror(33007)
  753. else
  754. begin
  755. if ps.section<>objectdata.currsec then
  756. internalerror(33008);
  757. ofs:=ofs-ps.address;
  758. reloc:=false;
  759. objectlibrary.UsedAsmSymbolListInsert(ps);
  760. end;
  761. end;
  762. end;
  763. end;
  764. { External references (AB_EXTERNAL and AB_COMMON) need a symbol relocation }
  765. if assigned(ps) and (ps.currbind in [AB_EXTERNAL,AB_COMMON]) then
  766. begin
  767. if currpass=2 then
  768. begin
  769. objectdata.writesymbol(ps);
  770. objectoutput.exportsymbol(ps);
  771. end;
  772. objectdata.writeSymStabs(ofs,hp,ps,nidx,nother,line,reloc)
  773. end
  774. else
  775. objectdata.writeStabs(ofs,hp,nidx,nother,line,reloc);
  776. if assigned(hp) then
  777. p[ii]:='"';
  778. end;
  779. procedure TInternalAssembler.emitlineinfostabs(nidx,line : longint);
  780. begin
  781. if currpass=1 then
  782. begin
  783. objectdata.allocstabs(nil);
  784. exit;
  785. end;
  786. if (nidx=n_textline) and assigned(funcname) and
  787. (target_info.use_function_relative_addresses) then
  788. objectdata.writeStabs(objectdata.currsec.datasize-funcname.address,nil,nidx,0,line,false)
  789. else
  790. objectdata.writeStabs(objectdata.currsec.datasize,nil,nidx,0,line,true);
  791. end;
  792. procedure TInternalAssembler.emitstabs(s:string);
  793. begin
  794. s:=s+#0;
  795. ConvertStabs(@s[1]);
  796. end;
  797. procedure TInternalAssembler.WriteFileLineInfo(var fileinfo : tfileposinfo);
  798. var
  799. curr_n : byte;
  800. hp : tasmsymbol;
  801. infile : tinputfile;
  802. begin
  803. if not ((cs_debuginfo in aktmoduleswitches) or
  804. (cs_gdb_lineinfo in aktglobalswitches)) then
  805. exit;
  806. { file changed ? (must be before line info) }
  807. if (fileinfo.fileindex<>0) and
  808. (stabslastfileinfo.fileindex<>fileinfo.fileindex) then
  809. begin
  810. infile:=current_module.sourcefiles.get_file(fileinfo.fileindex);
  811. if assigned(infile) then
  812. begin
  813. if includecount=0 then
  814. curr_n:=n_sourcefile
  815. else
  816. curr_n:=n_includefile;
  817. { get symbol for this includefile }
  818. hp:=objectlibrary.newasmsymbol('Ltext'+ToStr(IncludeCount),AB_LOCAL,AT_FUNCTION);
  819. if currpass=1 then
  820. begin
  821. objectdata.allocsymbol(currpass,hp,0);
  822. objectlibrary.UsedAsmSymbolListInsert(hp);
  823. end
  824. else
  825. objectdata.writesymbol(hp);
  826. { emit stabs }
  827. if (infile.path^<>'') then
  828. EmitStabs('"'+BsToSlash(FixPath(infile.path^,false))+'",'+tostr(curr_n)+
  829. ',0,0,Ltext'+ToStr(IncludeCount));
  830. EmitStabs('"'+FixFileName(infile.name^)+'",'+tostr(curr_n)+
  831. ',0,0,Ltext'+ToStr(IncludeCount));
  832. inc(includecount);
  833. { force new line info }
  834. stabslastfileinfo.line:=-1;
  835. end;
  836. end;
  837. { line changed ? }
  838. if (stabslastfileinfo.line<>fileinfo.line) and (fileinfo.line<>0) then
  839. emitlineinfostabs(n_line,fileinfo.line);
  840. stabslastfileinfo:=fileinfo;
  841. end;
  842. procedure TInternalAssembler.StartFileLineInfo;
  843. var
  844. fileinfo : tfileposinfo;
  845. begin
  846. FillChar(stabslastfileinfo,sizeof(stabslastfileinfo),0);
  847. n_line:=n_bssline;
  848. funcname:=nil;
  849. linecount:=1;
  850. includecount:=0;
  851. fileinfo.fileindex:=1;
  852. fileinfo.line:=1;
  853. WriteFileLineInfo(fileinfo);
  854. end;
  855. procedure TInternalAssembler.EndFileLineInfo;
  856. var
  857. hp : tasmsymbol;
  858. begin
  859. if not ((cs_debuginfo in aktmoduleswitches) or
  860. (cs_gdb_lineinfo in aktglobalswitches)) then
  861. exit;
  862. objectdata.createsection(sec_code,'',0,[]);
  863. hp:=objectlibrary.newasmsymbol('Letext',AB_LOCAL,AT_FUNCTION);
  864. if currpass=1 then
  865. begin
  866. objectdata.allocsymbol(currpass,hp,0);
  867. objectlibrary.UsedAsmSymbolListInsert(hp);
  868. end
  869. else
  870. objectdata.writesymbol(hp);
  871. EmitStabs('"",'+tostr(n_sourcefile)+',0,0,Letext');
  872. end;
  873. {$endif GDB}
  874. function TInternalAssembler.MaybeNextList(var hp:Tai):boolean;
  875. begin
  876. { maybe end of list }
  877. while not assigned(hp) do
  878. begin
  879. if currlistidx<lists then
  880. begin
  881. inc(currlistidx);
  882. currlist:=list[currlistidx];
  883. hp:=Tai(currList.first);
  884. end
  885. else
  886. begin
  887. MaybeNextList:=false;
  888. exit;
  889. end;
  890. end;
  891. MaybeNextList:=true;
  892. end;
  893. function TInternalAssembler.TreePass0(hp:Tai):Tai;
  894. var
  895. l : longint;
  896. begin
  897. while assigned(hp) do
  898. begin
  899. case hp.typ of
  900. ait_align :
  901. begin
  902. { always use the maximum fillsize in this pass to avoid possible
  903. short jumps to become out of range }
  904. Tai_align(hp).fillsize:=Tai_align(hp).aligntype;
  905. objectdata.alloc(Tai_align(hp).fillsize);
  906. end;
  907. ait_datablock :
  908. begin
  909. l:=used_align(size_2_align(Tai_datablock(hp).size),0,objectdata.currsec.addralign);
  910. if SmartAsm or (not Tai_datablock(hp).is_global) then
  911. begin
  912. objectdata.allocalign(l);
  913. objectdata.alloc(Tai_datablock(hp).size);
  914. end;
  915. end;
  916. ait_real_80bit :
  917. objectdata.alloc(10);
  918. ait_real_64bit :
  919. objectdata.alloc(8);
  920. ait_real_32bit :
  921. objectdata.alloc(4);
  922. ait_comp_64bit :
  923. objectdata.alloc(8);
  924. ait_const_64bit,
  925. ait_const_32bit,
  926. ait_const_16bit,
  927. ait_const_8bit,
  928. ait_const_rva_symbol,
  929. ait_const_indirect_symbol :
  930. objectdata.alloc(tai_const(hp).size);
  931. ait_section:
  932. begin
  933. objectdata.CreateSection(Tai_section(hp).sectype,Tai_section(hp).name^,Tai_section(hp).secalign,[]);
  934. Tai_section(hp).sec:=objectdata.CurrSec;
  935. end;
  936. ait_symbol :
  937. objectdata.allocsymbol(currpass,Tai_symbol(hp).sym,0);
  938. ait_label :
  939. objectdata.allocsymbol(currpass,Tai_label(hp).l,0);
  940. ait_string :
  941. objectdata.alloc(Tai_string(hp).len);
  942. ait_instruction :
  943. begin
  944. {$ifdef i386}
  945. {$ifndef NOAG386BIN}
  946. { reset instructions which could change in pass 2 }
  947. Taicpu(hp).resetpass2;
  948. objectdata.alloc(Taicpu(hp).Pass1(objectdata.currsec.datasize));
  949. {$endif NOAG386BIN}
  950. {$endif i386}
  951. end;
  952. ait_cutobject :
  953. if SmartAsm then
  954. break;
  955. end;
  956. hp:=Tai(hp.next);
  957. end;
  958. TreePass0:=hp;
  959. end;
  960. function TInternalAssembler.TreePass1(hp:Tai):Tai;
  961. var
  962. InlineLevel,
  963. l : longint;
  964. {$ifdef i386}
  965. {$ifndef NOAG386BIN}
  966. i : longint;
  967. {$endif NOAG386BIN}
  968. {$endif i386}
  969. begin
  970. inlinelevel:=0;
  971. while assigned(hp) do
  972. begin
  973. {$ifdef GDB}
  974. { write stabs, no line info for inlined code }
  975. if (inlinelevel=0) and
  976. ((cs_debuginfo in aktmoduleswitches) or
  977. (cs_gdb_lineinfo in aktglobalswitches)) then
  978. begin
  979. if (objectdata.currsec<>nil) and
  980. not(hp.typ in SkipLineInfo) then
  981. WriteFileLineInfo(tailineinfo(hp).fileinfo);
  982. end;
  983. {$endif GDB}
  984. case hp.typ of
  985. ait_align :
  986. begin
  987. { here we must determine the fillsize which is used in pass2 }
  988. Tai_align(hp).fillsize:=align(objectdata.currsec.datasize,Tai_align(hp).aligntype)-
  989. objectdata.currsec.datasize;
  990. objectdata.alloc(Tai_align(hp).fillsize);
  991. end;
  992. ait_datablock :
  993. begin
  994. if objectdata.currsec.sectype<>sec_bss then
  995. Message(asmw_e_alloc_data_only_in_bss);
  996. l:=used_align(size_2_align(Tai_datablock(hp).size),0,objectdata.currsec.addralign);
  997. if Tai_datablock(hp).is_global and
  998. not SmartAsm then
  999. begin
  1000. objectdata.allocsymbol(currpass,Tai_datablock(hp).sym,Tai_datablock(hp).size);
  1001. { force to be common/external, must be after setaddress as that would
  1002. set it to AB_GLOBAL }
  1003. Tai_datablock(hp).sym.currbind:=AB_COMMON;
  1004. end
  1005. else
  1006. begin
  1007. objectdata.allocalign(l);
  1008. objectdata.allocsymbol(currpass,Tai_datablock(hp).sym,Tai_datablock(hp).size);
  1009. objectdata.alloc(Tai_datablock(hp).size);
  1010. end;
  1011. objectlibrary.UsedAsmSymbolListInsert(Tai_datablock(hp).sym);
  1012. end;
  1013. ait_real_80bit :
  1014. objectdata.alloc(10);
  1015. ait_real_64bit :
  1016. objectdata.alloc(8);
  1017. ait_real_32bit :
  1018. objectdata.alloc(4);
  1019. ait_comp_64bit :
  1020. objectdata.alloc(8);
  1021. ait_const_64bit,
  1022. ait_const_32bit,
  1023. ait_const_16bit,
  1024. ait_const_8bit,
  1025. ait_const_rva_symbol,
  1026. ait_const_indirect_symbol :
  1027. begin
  1028. objectdata.alloc(tai_const(hp).size);
  1029. if assigned(Tai_const(hp).sym) then
  1030. objectlibrary.UsedAsmSymbolListInsert(Tai_const(hp).sym);
  1031. if assigned(Tai_const(hp).endsym) then
  1032. objectlibrary.UsedAsmSymbolListInsert(Tai_const(hp).endsym);
  1033. end;
  1034. ait_section:
  1035. begin
  1036. { use cached value }
  1037. objectdata.setsection(Tai_section(hp).sec);
  1038. {$ifdef GDB}
  1039. case Tai_section(hp).sectype of
  1040. sec_code :
  1041. n_line:=n_textline;
  1042. sec_data :
  1043. n_line:=n_dataline;
  1044. sec_bss :
  1045. n_line:=n_bssline;
  1046. else
  1047. n_line:=n_dataline;
  1048. end;
  1049. stabslastfileinfo.line:=-1;
  1050. {$endif GDB}
  1051. end;
  1052. {$ifdef GDB}
  1053. ait_stabn :
  1054. begin
  1055. if assigned(Tai_stabn(hp).str) then
  1056. convertstabs(Tai_stabn(hp).str);
  1057. end;
  1058. ait_stabs :
  1059. begin
  1060. if assigned(Tai_stabs(hp).str) then
  1061. convertstabs(Tai_stabs(hp).str);
  1062. end;
  1063. ait_stab_function_name :
  1064. begin
  1065. if assigned(Tai_stab_function_name(hp).str) then
  1066. begin
  1067. funcname:=objectlibrary.getasmsymbol(strpas(Tai_stab_function_name(hp).str));
  1068. objectlibrary.UsedAsmSymbolListInsert(funcname);
  1069. end
  1070. else
  1071. funcname:=nil;
  1072. end;
  1073. ait_force_line :
  1074. stabslastfileinfo.line:=0;
  1075. {$endif}
  1076. ait_symbol :
  1077. begin
  1078. objectdata.allocsymbol(currpass,Tai_symbol(hp).sym,0);
  1079. objectlibrary.UsedAsmSymbolListInsert(Tai_symbol(hp).sym);
  1080. end;
  1081. ait_symbol_end :
  1082. begin
  1083. if target_info.system in [system_i386_linux,system_i386_beos] then
  1084. begin
  1085. Tai_symbol_end(hp).sym.size:=objectdata.currsec.datasize-Tai_symbol_end(hp).sym.address;
  1086. objectlibrary.UsedAsmSymbolListInsert(Tai_symbol_end(hp).sym);
  1087. end;
  1088. end;
  1089. ait_label :
  1090. begin
  1091. objectdata.allocsymbol(currpass,Tai_label(hp).l,0);
  1092. objectlibrary.UsedAsmSymbolListInsert(Tai_label(hp).l);
  1093. end;
  1094. ait_string :
  1095. objectdata.alloc(Tai_string(hp).len);
  1096. ait_instruction :
  1097. begin
  1098. {$ifdef i386}
  1099. {$ifndef NOAG386BIN}
  1100. objectdata.alloc(Taicpu(hp).Pass1(objectdata.currsec.datasize));
  1101. { fixup the references }
  1102. for i:=1 to Taicpu(hp).ops do
  1103. begin
  1104. with Taicpu(hp).oper[i-1]^ do
  1105. begin
  1106. case typ of
  1107. top_ref :
  1108. begin
  1109. if assigned(ref^.symbol) then
  1110. objectlibrary.UsedAsmSymbolListInsert(ref^.symbol);
  1111. if assigned(ref^.relsymbol) then
  1112. objectlibrary.UsedAsmSymbolListInsert(ref^.symbol);
  1113. end;
  1114. end;
  1115. end;
  1116. end;
  1117. {$endif NOAG386BIN}
  1118. {$endif i386}
  1119. end;
  1120. ait_direct :
  1121. Message(asmw_f_direct_not_supported);
  1122. ait_cutobject :
  1123. if SmartAsm then
  1124. break;
  1125. ait_marker :
  1126. if tai_marker(hp).kind=InlineStart then
  1127. inc(InlineLevel)
  1128. else if tai_marker(hp).kind=InlineEnd then
  1129. dec(InlineLevel);
  1130. end;
  1131. hp:=Tai(hp.next);
  1132. end;
  1133. TreePass1:=hp;
  1134. end;
  1135. function TInternalAssembler.TreePass2(hp:Tai):Tai;
  1136. var
  1137. fillbuffer : tfillbuffer;
  1138. InlineLevel,
  1139. l : longint;
  1140. v : int64;
  1141. {$ifdef x86}
  1142. co : comp;
  1143. {$endif x86}
  1144. begin
  1145. inlinelevel:=0;
  1146. { main loop }
  1147. while assigned(hp) do
  1148. begin
  1149. {$ifdef GDB}
  1150. { write stabs, no line info for inlined code }
  1151. if (inlinelevel=0) and
  1152. ((cs_debuginfo in aktmoduleswitches) or
  1153. (cs_gdb_lineinfo in aktglobalswitches)) then
  1154. begin
  1155. if (objectdata.currsec<>nil) and
  1156. not(hp.typ in SkipLineInfo) then
  1157. WriteFileLineInfo(tailineinfo(hp).fileinfo);
  1158. end;
  1159. {$endif GDB}
  1160. case hp.typ of
  1161. ait_align :
  1162. begin
  1163. if objectdata.currsec.sectype=sec_bss then
  1164. objectdata.alloc(Tai_align(hp).fillsize)
  1165. else
  1166. objectdata.writebytes(Tai_align(hp).calculatefillbuf(fillbuffer)^,Tai_align(hp).fillsize);
  1167. end;
  1168. ait_section :
  1169. begin
  1170. { use cached value }
  1171. objectdata.setsection(Tai_section(hp).sec);
  1172. {$ifdef GDB}
  1173. case Tai_section(hp).sectype of
  1174. sec_code : n_line:=n_textline;
  1175. sec_data : n_line:=n_dataline;
  1176. sec_bss : n_line:=n_bssline;
  1177. else
  1178. n_line:=n_dataline;
  1179. end;
  1180. stabslastfileinfo.line:=-1;
  1181. {$endif GDB}
  1182. end;
  1183. ait_symbol :
  1184. begin
  1185. objectdata.writesymbol(Tai_symbol(hp).sym);
  1186. objectoutput.exportsymbol(Tai_symbol(hp).sym);
  1187. end;
  1188. ait_datablock :
  1189. begin
  1190. l:=used_align(size_2_align(Tai_datablock(hp).size),0,objectdata.currsec.addralign);
  1191. objectdata.writesymbol(Tai_datablock(hp).sym);
  1192. objectoutput.exportsymbol(Tai_datablock(hp).sym);
  1193. if SmartAsm or (not Tai_datablock(hp).is_global) then
  1194. begin
  1195. objectdata.allocalign(l);
  1196. objectdata.alloc(Tai_datablock(hp).size);
  1197. end;
  1198. end;
  1199. ait_real_80bit :
  1200. objectdata.writebytes(Tai_real_80bit(hp).value,10);
  1201. ait_real_64bit :
  1202. objectdata.writebytes(Tai_real_64bit(hp).value,8);
  1203. ait_real_32bit :
  1204. objectdata.writebytes(Tai_real_32bit(hp).value,4);
  1205. ait_comp_64bit :
  1206. begin
  1207. {$ifdef x86}
  1208. {$ifdef FPC}
  1209. co:=comp(Tai_comp_64bit(hp).value);
  1210. {$else}
  1211. co:=Tai_comp_64bit(hp).value;
  1212. {$endif}
  1213. objectdata.writebytes(co,8);
  1214. {$endif x86}
  1215. end;
  1216. ait_string :
  1217. objectdata.writebytes(Tai_string(hp).str^,Tai_string(hp).len);
  1218. ait_const_64bit,
  1219. ait_const_32bit,
  1220. ait_const_16bit,
  1221. ait_const_8bit :
  1222. begin
  1223. if assigned(tai_const(hp).sym) then
  1224. begin
  1225. if assigned(tai_const(hp).endsym) then
  1226. begin
  1227. if tai_const(hp).endsym.section<>tai_const(hp).sym.section then
  1228. internalerror(200404124);
  1229. v:=tai_const(hp).endsym.address-tai_const(hp).sym.address+Tai_const(hp).value;
  1230. objectdata.writebytes(v,tai_const(hp).size);
  1231. end
  1232. else
  1233. objectdata.writereloc(Tai_const(hp).value,Tai_const(hp).size,Tai_const(hp).sym,RELOC_ABSOLUTE);
  1234. end
  1235. else
  1236. objectdata.writebytes(Tai_const(hp).value,tai_const(hp).size);
  1237. end;
  1238. ait_const_rva_symbol :
  1239. objectdata.writereloc(Tai_const(hp).value,sizeof(aint),Tai_const(hp).sym,RELOC_RVA);
  1240. ait_label :
  1241. begin
  1242. objectdata.writesymbol(Tai_label(hp).l);
  1243. { exporting shouldn't be necessary as labels are local,
  1244. but it's better to be on the safe side (PFV) }
  1245. objectoutput.exportsymbol(Tai_label(hp).l);
  1246. end;
  1247. {$ifdef i386}
  1248. {$ifndef NOAG386BIN}
  1249. ait_instruction :
  1250. Taicpu(hp).Pass2(objectdata);
  1251. {$endif NOAG386BIN}
  1252. {$endif i386}
  1253. {$ifdef GDB}
  1254. ait_stabn :
  1255. convertstabs(Tai_stabn(hp).str);
  1256. ait_stabs :
  1257. convertstabs(Tai_stabs(hp).str);
  1258. ait_stab_function_name :
  1259. if assigned(Tai_stab_function_name(hp).str) then
  1260. funcname:=objectlibrary.getasmsymbol(strpas(Tai_stab_function_name(hp).str))
  1261. else
  1262. funcname:=nil;
  1263. ait_force_line :
  1264. stabslastfileinfo.line:=0;
  1265. {$endif}
  1266. ait_cutobject :
  1267. if SmartAsm then
  1268. break;
  1269. ait_marker :
  1270. if tai_marker(hp).kind=InlineStart then
  1271. inc(InlineLevel)
  1272. else if tai_marker(hp).kind=InlineEnd then
  1273. dec(InlineLevel);
  1274. end;
  1275. hp:=Tai(hp.next);
  1276. end;
  1277. TreePass2:=hp;
  1278. end;
  1279. procedure TInternalAssembler.writetree;
  1280. var
  1281. hp : Tai;
  1282. label
  1283. doexit;
  1284. begin
  1285. objectdata:=objectoutput.newobjectdata(Objfile);
  1286. { reset the asmsymbol list }
  1287. objectlibrary.CreateUsedAsmsymbolList;
  1288. { Pass 0 }
  1289. currpass:=0;
  1290. objectdata.createsection(sec_code,'',0,[]);
  1291. objectdata.beforealloc;
  1292. { start with list 1 }
  1293. currlistidx:=1;
  1294. currlist:=list[currlistidx];
  1295. hp:=Tai(currList.first);
  1296. while assigned(hp) do
  1297. begin
  1298. hp:=TreePass0(hp);
  1299. MaybeNextList(hp);
  1300. end;
  1301. objectdata.afteralloc;
  1302. { leave if errors have occured }
  1303. if errorcount>0 then
  1304. goto doexit;
  1305. { Pass 1 }
  1306. currpass:=1;
  1307. objectdata.resetsections;
  1308. objectdata.beforealloc;
  1309. objectdata.createsection(sec_code,'',0,[]);
  1310. {$ifdef GDB}
  1311. StartFileLineInfo;
  1312. {$endif GDB}
  1313. { start with list 1 }
  1314. currlistidx:=1;
  1315. currlist:=list[currlistidx];
  1316. hp:=Tai(currList.first);
  1317. while assigned(hp) do
  1318. begin
  1319. hp:=TreePass1(hp);
  1320. MaybeNextList(hp);
  1321. end;
  1322. {$ifdef GDB}
  1323. EndFileLineInfo;
  1324. {$endif GDB}
  1325. objectdata.afteralloc;
  1326. { check for undefined labels and reset }
  1327. objectlibrary.UsedAsmSymbolListCheckUndefined;
  1328. { leave if errors have occured }
  1329. if errorcount>0 then
  1330. goto doexit;
  1331. { Pass 2 }
  1332. currpass:=2;
  1333. objectdata.resetsections;
  1334. objectdata.beforewrite;
  1335. objectdata.createsection(sec_code,'',0,[]);
  1336. {$ifdef GDB}
  1337. StartFileLineInfo;
  1338. {$endif GDB}
  1339. { start with list 1 }
  1340. currlistidx:=1;
  1341. currlist:=list[currlistidx];
  1342. hp:=Tai(currList.first);
  1343. while assigned(hp) do
  1344. begin
  1345. hp:=TreePass2(hp);
  1346. MaybeNextList(hp);
  1347. end;
  1348. {$ifdef GDB}
  1349. EndFileLineInfo;
  1350. {$endif GDB}
  1351. objectdata.afterwrite;
  1352. { don't write the .o file if errors have occured }
  1353. if errorcount=0 then
  1354. begin
  1355. { write objectfile }
  1356. objectoutput.startobjectfile(ObjFile);
  1357. objectoutput.writeobjectfile(objectdata);
  1358. objectdata.free;
  1359. objectdata:=nil;
  1360. end;
  1361. doexit:
  1362. { reset the used symbols back, must be after the .o has been
  1363. written }
  1364. objectlibrary.UsedAsmsymbolListReset;
  1365. objectlibrary.DestroyUsedAsmsymbolList;
  1366. end;
  1367. procedure TInternalAssembler.writetreesmart;
  1368. var
  1369. hp : Tai;
  1370. startsectype : TAsmSectionType;
  1371. place: tcutplace;
  1372. begin
  1373. NextSmartName(cut_normal);
  1374. objectdata:=objectoutput.newobjectdata(Objfile);
  1375. startsectype:=sec_code;
  1376. { start with list 1 }
  1377. currlistidx:=1;
  1378. currlist:=list[currlistidx];
  1379. hp:=Tai(currList.first);
  1380. while assigned(hp) do
  1381. begin
  1382. { reset the asmsymbol list }
  1383. objectlibrary.CreateUsedAsmSymbolList;
  1384. { Pass 0 }
  1385. currpass:=0;
  1386. objectdata.resetsections;
  1387. objectdata.beforealloc;
  1388. objectdata.createsection(startsectype,'',0,[]);
  1389. TreePass0(hp);
  1390. objectdata.afteralloc;
  1391. { leave if errors have occured }
  1392. if errorcount>0 then
  1393. exit;
  1394. { Pass 1 }
  1395. currpass:=1;
  1396. objectdata.resetsections;
  1397. objectdata.beforealloc;
  1398. objectdata.createsection(startsectype,'',0,[]);
  1399. {$ifdef GDB}
  1400. StartFileLineInfo;
  1401. {$endif GDB}
  1402. TreePass1(hp);
  1403. {$ifdef GDB}
  1404. EndFileLineInfo;
  1405. {$endif GDB}
  1406. objectdata.afteralloc;
  1407. { check for undefined labels }
  1408. objectlibrary.UsedAsmSymbolListCheckUndefined;
  1409. { leave if errors have occured }
  1410. if errorcount>0 then
  1411. exit;
  1412. { Pass 2 }
  1413. currpass:=2;
  1414. objectoutput.startobjectfile(Objfile);
  1415. objectdata.resetsections;
  1416. objectdata.beforewrite;
  1417. objectdata.createsection(startsectype,'',0,[]);
  1418. {$ifdef GDB}
  1419. StartFileLineInfo;
  1420. {$endif GDB}
  1421. hp:=TreePass2(hp);
  1422. { save section type for next loop, must be done before EndFileLineInfo
  1423. because that changes the section to sec_code }
  1424. startsectype:=objectdata.currsec.sectype;
  1425. {$ifdef GDB}
  1426. EndFileLineInfo;
  1427. {$endif GDB}
  1428. objectdata.afterwrite;
  1429. { leave if errors have occured }
  1430. if errorcount>0 then
  1431. exit;
  1432. { write the current objectfile }
  1433. objectoutput.writeobjectfile(objectdata);
  1434. objectdata.free;
  1435. objectdata:=nil;
  1436. { reset the used symbols back, must be after the .o has been
  1437. written }
  1438. objectlibrary.UsedAsmsymbolListReset;
  1439. objectlibrary.DestroyUsedAsmsymbolList;
  1440. { end of lists? }
  1441. if not MaybeNextList(hp) then
  1442. break;
  1443. { we will start a new objectfile so reset everything }
  1444. { The place can still change in the next while loop, so don't init }
  1445. { the writer yet (JM) }
  1446. if (hp.typ=ait_cutobject) then
  1447. place := Tai_cutobject(hp).place
  1448. else
  1449. place := cut_normal;
  1450. { avoid empty files }
  1451. while assigned(hp) and
  1452. (Tai(hp).typ in [ait_marker,ait_comment,ait_section,ait_cutobject]) do
  1453. begin
  1454. if Tai(hp).typ=ait_section then
  1455. startsectype:=Tai_section(hp).sectype
  1456. else if (Tai(hp).typ=ait_cutobject) then
  1457. place:=Tai_cutobject(hp).place;
  1458. hp:=Tai(hp.next);
  1459. end;
  1460. { there is a problem if startsectype is sec_none !! PM }
  1461. if startsectype=sec_none then
  1462. startsectype:=sec_code;
  1463. if not MaybeNextList(hp) then
  1464. break;
  1465. { start next objectfile }
  1466. NextSmartName(place);
  1467. objectdata:=objectoutput.newobjectdata(Objfile);
  1468. end;
  1469. end;
  1470. procedure TInternalAssembler.MakeObject;
  1471. procedure addlist(p:TAAsmoutput);
  1472. begin
  1473. inc(lists);
  1474. list[lists]:=p;
  1475. end;
  1476. begin
  1477. if cs_debuginfo in aktmoduleswitches then
  1478. addlist(debuglist);
  1479. addlist(codesegment);
  1480. addlist(datasegment);
  1481. addlist(consts);
  1482. addlist(rttilist);
  1483. addlist(picdata);
  1484. if assigned(resourcestringlist) then
  1485. addlist(resourcestringlist);
  1486. addlist(bsssegment);
  1487. if assigned(importssection) then
  1488. addlist(importssection);
  1489. if assigned(exportssection) and not UseDeffileForExports then
  1490. addlist(exportssection);
  1491. if assigned(resourcesection) then
  1492. addlist(resourcesection);
  1493. {$warning TODO internal writer support for dwarf}
  1494. {if assigned(dwarflist) then
  1495. addlist(dwarflist);}
  1496. if SmartAsm then
  1497. writetreesmart
  1498. else
  1499. writetree;
  1500. end;
  1501. {*****************************************************************************
  1502. Generate Assembler Files Main Procedure
  1503. *****************************************************************************}
  1504. Procedure GenerateAsm(smart:boolean);
  1505. var
  1506. a : TAssembler;
  1507. begin
  1508. if not assigned(CAssembler[target_asm.id]) then
  1509. Message(asmw_f_assembler_output_not_supported);
  1510. a:=CAssembler[target_asm.id].Create(smart);
  1511. a.MakeObject;
  1512. a.Free;
  1513. end;
  1514. Procedure OnlyAsm;
  1515. var
  1516. a : TExternalAssembler;
  1517. begin
  1518. a:=TExternalAssembler.Create(false);
  1519. a.DoAssemble;
  1520. a.Free;
  1521. end;
  1522. {*****************************************************************************
  1523. Init/Done
  1524. *****************************************************************************}
  1525. procedure RegisterAssembler(const r:tasminfo;c:TAssemblerClass);
  1526. var
  1527. t : tasm;
  1528. begin
  1529. t:=r.id;
  1530. if assigned(asminfos[t]) then
  1531. writeln('Warning: Assembler is already registered!')
  1532. else
  1533. Getmem(asminfos[t],sizeof(tasminfo));
  1534. asminfos[t]^:=r;
  1535. CAssembler[t]:=c;
  1536. end;
  1537. procedure InitAssembler;
  1538. begin
  1539. { target_asm is already set by readarguments }
  1540. initoutputformat:=target_asm.id;
  1541. aktoutputformat:=target_asm.id;
  1542. end;
  1543. procedure DoneAssembler;
  1544. begin
  1545. end;
  1546. end.