assemble.pas 50 KB

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