aasmbase.pas 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. This unit implements an abstract asmoutput class for all processor types
  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 implements an abstract asm output class for all processor types)
  18. This unit implements an abstract assembler output class for all processors, these
  19. are then overriden for each assembler writer to actually write the data in these
  20. classes to an assembler file.
  21. }
  22. unit aasmbase;
  23. {$i fpcdefs.inc}
  24. interface
  25. uses
  26. cutils,cclasses,
  27. globtype,globals,systems
  28. ;
  29. type
  30. TAsmSection = class;
  31. TAsmObjectData = class;
  32. TAsmsymbind=(AB_NONE,AB_EXTERNAL,AB_COMMON,AB_LOCAL,AB_GLOBAL);
  33. TAsmsymtype=(AT_NONE,AT_FUNCTION,AT_DATA,AT_SECTION);
  34. TAsmRelocationType = (RELOC_ABSOLUTE,RELOC_RELATIVE,RELOC_RVA);
  35. TAsmSectionType=(sec_none,
  36. sec_code,sec_data,sec_rodata,sec_bss,
  37. sec_common, { used for executable creation }
  38. sec_custom, { custom section, no prefix }
  39. { stabs }
  40. sec_stab,sec_stabstr,
  41. { win32 }
  42. sec_idata2,sec_idata4,sec_idata5,sec_idata6,sec_idata7,sec_edata,
  43. { C++ exception handling unwinding (uses dwarf) }
  44. sec_eh_frame,
  45. { dwarf }
  46. sec_debug_frame,
  47. { ELF resources }
  48. sec_fpc
  49. );
  50. TAsmSectionOption = (aso_alloconly,aso_executable);
  51. TAsmSectionOptions = set of TAsmSectionOption;
  52. TAsmSymbol = class(TNamedIndexItem)
  53. private
  54. { this need to be incremented with every symbol loading into the
  55. paasmoutput, thus in loadsym/loadref/const_symbol (PFV) }
  56. refs : longint;
  57. public
  58. defbind,
  59. currbind : TAsmsymbind;
  60. typ : TAsmsymtype;
  61. { the next fields are filled in the binary writer }
  62. section : TAsmSection;
  63. address,
  64. size : aint;
  65. { Alternate symbol which can be used for 'renaming' needed for
  66. inlining }
  67. altsymbol : tasmsymbol;
  68. { pointer to objectdata that is the owner of this symbol }
  69. owner : tasmobjectdata;
  70. { Is the symbol in the used list }
  71. inusedlist : boolean;
  72. { assembler pass label is set, used for detecting multiple labels }
  73. pass : byte;
  74. ppuidx : longint;
  75. constructor create(const s:string;_bind:TAsmsymbind;_typ:Tasmsymtype);
  76. procedure reset;
  77. function is_used:boolean;
  78. procedure increfs;
  79. procedure decrefs;
  80. function getrefs: longint;
  81. procedure setaddress(_pass:byte;sec:TAsmSection;offset,len:aint);
  82. end;
  83. TAsmLabel = class(TAsmSymbol)
  84. { this is set by the tai_label.Init }
  85. is_set,
  86. { is the label only there for getting an address (e.g. for i/o }
  87. { checks -> true) or is it a jump target (false) }
  88. is_addr : boolean;
  89. labelnr : longint;
  90. constructor create(nr:longint);
  91. constructor createdata(const modulename:string;nr:longint);
  92. constructor createaddr(nr:longint);
  93. function getname:string;override;
  94. end;
  95. TAsmRelocation = class(TLinkedListItem)
  96. address,
  97. orgsize : aint; { original size of the symbol to relocate, required for COFF }
  98. symbol : TAsmSymbol;
  99. section : TAsmSection; { only used if symbol=nil }
  100. typ : TAsmRelocationType;
  101. constructor CreateSymbol(Aaddress:aint;s:Tasmsymbol;Atyp:TAsmRelocationType);
  102. constructor CreateSymbolSize(Aaddress:aint;s:Tasmsymbol;Aorgsize:aint;Atyp:TAsmRelocationType);
  103. constructor CreateSection(Aaddress:aint;sec:TAsmSection;Atyp:TAsmRelocationType);
  104. end;
  105. TAsmSection = class(TNamedIndexItem)
  106. owner : TAsmObjectData;
  107. secoptions : TAsmSectionOptions;
  108. sectype : TAsmSectionType;
  109. secsymidx : longint; { index for the section in symtab }
  110. addralign : longint; { alignment of the section }
  111. { size of the data and in the file }
  112. dataalignbytes : longint;
  113. data : TDynamicArray;
  114. datasize,
  115. datapos : aint;
  116. { size and position in memory }
  117. memsize,
  118. mempos : aint;
  119. { relocation }
  120. relocations : TLinkedList;
  121. constructor create(const Aname:string;Atype:TAsmSectionType;Aalign:longint;Aoptions:TAsmSectionOptions);virtual;
  122. destructor destroy;override;
  123. function write(const d;l:aint):aint;
  124. function writestr(const s:string):aint;
  125. procedure writealign(l:longint);
  126. function aligneddatasize:aint;
  127. procedure setdatapos(var dpos:aint);
  128. procedure alignsection;
  129. procedure alloc(l:aint);
  130. procedure addsymreloc(ofs:aint;p:tasmsymbol;relative:TAsmRelocationType);
  131. procedure addsectionreloc(ofs:aint;sec:TAsmSection;relative:TAsmRelocationType);
  132. procedure fixuprelocs;virtual;
  133. end;
  134. TAsmSectionClass = class of TAsmSection;
  135. TAsmObjectData = class(TLinkedListItem)
  136. private
  137. FName : string{$ifndef VER1_9_4}[80]{$endif};
  138. FCurrSec : TAsmSection;
  139. FSects : TDictionary;
  140. FCAsmSection : TAsmSectionClass;
  141. { Symbols that will be defined in this object file }
  142. FSymbols : TIndexArray;
  143. { Special info sections that are written to during object generation }
  144. FStabsRecSize : longint;
  145. FStabsSec,
  146. FStabStrSec : TAsmSection;
  147. procedure section_reset(p:tnamedindexitem;arg:pointer);
  148. procedure section_fixuprelocs(p:tnamedindexitem;arg:pointer);
  149. protected
  150. property StabsRecSize:longint read FStabsRecSize write FStabsRecSize;
  151. property StabsSec:TAsmSection read FStabsSec write FStabsSec;
  152. property StabStrSec:TAsmSection read FStabStrSec write FStabStrSec;
  153. property CAsmSection:TAsmSectionClass read FCAsmSection write FCAsmSection;
  154. public
  155. constructor create(const n:string);virtual;
  156. destructor destroy;override;
  157. function sectionname(atype:tasmsectiontype;const aname:string):string;virtual;
  158. function createsection(atype:tasmsectiontype;const aname:string;aalign:longint;aoptions:TAsmSectionOptions):tasmsection;virtual;
  159. procedure setsection(asec:tasmsection);
  160. procedure alloc(len:aint);
  161. procedure allocalign(len:longint);
  162. procedure allocstabs(p:pchar);
  163. procedure allocsymbol(currpass:byte;p:tasmsymbol;len:aint);
  164. procedure writebytes(var data;len:aint);
  165. procedure writereloc(data,len:aint;p:tasmsymbol;relative:TAsmRelocationType);virtual;abstract;
  166. procedure writesymbol(p:tasmsymbol);virtual;abstract;
  167. procedure writestabs(offset:aint;p:pchar;nidx,nother,line:longint;reloc:boolean);virtual;abstract;
  168. procedure writesymstabs(offset:aint;p:pchar;ps:tasmsymbol;nidx,nother,line:longint;reloc:boolean);virtual;abstract;
  169. procedure beforealloc;virtual;
  170. procedure beforewrite;virtual;
  171. procedure afteralloc;virtual;
  172. procedure afterwrite;virtual;
  173. procedure resetsections;
  174. procedure fixuprelocs;
  175. property Name:string{$ifndef VER1_9_4}[80]{$endif} read FName;
  176. property CurrSec:TAsmSection read FCurrSec;
  177. property Symbols:TindexArray read FSymbols;
  178. property Sects:TDictionary read FSects;
  179. end;
  180. TAsmObjectDataClass = class of TAsmObjectData;
  181. tasmsymbolidxarr = array[0..($7fffffff div sizeof(pointer))-1] of tasmsymbol;
  182. pasmsymbolidxarr = ^tasmsymbolidxarr;
  183. TAsmLibraryData = class(TLinkedListItem)
  184. private
  185. nextaltnr : longint;
  186. nextlabelnr : longint;
  187. public
  188. name,
  189. realname : string[80];
  190. symbolsearch : tdictionary; { contains ALL assembler symbols }
  191. usedasmsymbollist : tsinglelist;
  192. { ppu }
  193. asmsymbolppuidx : longint;
  194. asmsymbolidx : pasmsymbolidxarr; { used for translating ppu index->asmsymbol }
  195. constructor create(const n:string);
  196. destructor destroy;override;
  197. procedure Freeasmsymbolidx;
  198. procedure DerefAsmsymbol(var s:tasmsymbol);
  199. { asmsymbol }
  200. function newasmsymbol(const s : string;_bind:TAsmSymBind;_typ:TAsmsymtype) : tasmsymbol;
  201. function getasmsymbol(const s : string) : tasmsymbol;
  202. function renameasmsymbol(const sold, snew : string):tasmsymbol;
  203. function newasmlabel(nr:longint;is_addr,is_data:boolean) : tasmlabel;
  204. {# create a new assembler label }
  205. procedure getlabel(var l : tasmlabel);
  206. { make l as a new label and flag is_addr }
  207. procedure getaddrlabel(var l : tasmlabel);
  208. { make l as a new label and flag is_data }
  209. procedure getdatalabel(var l : tasmlabel);
  210. {# return a label number }
  211. procedure getlabelnr(var l : longint);
  212. procedure CreateUsedAsmSymbolList;
  213. procedure DestroyUsedAsmSymbolList;
  214. procedure UsedAsmSymbolListInsert(p:tasmsymbol);
  215. { generate an alternative (duplicate) symbol }
  216. procedure GenerateAltSymbol(p:tasmsymbol);
  217. { reset alternative symbol information }
  218. procedure UsedAsmSymbolListResetAltSym;
  219. procedure UsedAsmSymbolListReset;
  220. procedure UsedAsmSymbolListCheckUndefined;
  221. end;
  222. var
  223. objectlibrary : tasmlibrarydata;
  224. implementation
  225. uses
  226. strings,
  227. verbose;
  228. const
  229. symbolsgrow = 100;
  230. {*****************************************************************************
  231. TAsmSymbol
  232. *****************************************************************************}
  233. constructor tasmsymbol.create(const s:string;_bind:TAsmsymbind;_typ:Tasmsymtype);
  234. begin;
  235. inherited createname(s);
  236. reset;
  237. defbind:=_bind;
  238. typ:=_typ;
  239. inusedlist:=false;
  240. pass:=255;
  241. ppuidx:=-1;
  242. { mainly used to remove unused labels from the codesegment }
  243. refs:=0;
  244. end;
  245. procedure tasmsymbol.reset;
  246. begin
  247. { reset section info }
  248. section:=nil;
  249. address:=0;
  250. size:=0;
  251. indexnr:=-1;
  252. pass:=255;
  253. currbind:=AB_EXTERNAL;
  254. altsymbol:=nil;
  255. { taiowner:=nil;}
  256. end;
  257. function tasmsymbol.is_used:boolean;
  258. begin
  259. is_used:=(refs>0);
  260. end;
  261. procedure tasmsymbol.increfs;
  262. begin
  263. inc(refs);
  264. end;
  265. procedure tasmsymbol.decrefs;
  266. begin
  267. dec(refs);
  268. if refs<0 then
  269. internalerror(200211121);
  270. end;
  271. function tasmsymbol.getrefs: longint;
  272. begin
  273. getrefs := refs;
  274. end;
  275. procedure tasmsymbol.setaddress(_pass:byte;sec:TAsmSection;offset,len:aint);
  276. begin
  277. if (_pass=pass) then
  278. begin
  279. Message1(asmw_e_duplicate_label,name);
  280. exit;
  281. end;
  282. pass:=_pass;
  283. section:=sec;
  284. address:=offset;
  285. size:=len;
  286. { when the bind was reset to External, set it back to the default
  287. bind it got when defined }
  288. if (currbind=AB_EXTERNAL) and (defbind<>AB_NONE) then
  289. currbind:=defbind;
  290. end;
  291. {*****************************************************************************
  292. TAsmLabel
  293. *****************************************************************************}
  294. constructor tasmlabel.create(nr:longint);
  295. begin;
  296. labelnr:=nr;
  297. inherited create(target_asm.labelprefix+tostr(labelnr),AB_LOCAL,AT_FUNCTION);
  298. is_set:=false;
  299. is_addr := false;
  300. end;
  301. constructor tasmlabel.createdata(const modulename:string;nr:longint);
  302. begin;
  303. labelnr:=nr;
  304. inherited create('_$'+modulename+'$_L'+tostr(labelnr),AB_GLOBAL,AT_DATA);
  305. is_set:=false;
  306. is_addr := false;
  307. { write it always }
  308. increfs;
  309. end;
  310. constructor tasmlabel.createaddr(nr:longint);
  311. begin;
  312. self.create(nr);
  313. is_addr := true;
  314. end;
  315. function tasmlabel.getname:string;
  316. begin
  317. getname:=inherited getname;
  318. increfs;
  319. end;
  320. {****************************************************************************
  321. TAsmRelocation
  322. ****************************************************************************}
  323. constructor TAsmRelocation.CreateSymbol(Aaddress:aint;s:Tasmsymbol;Atyp:TAsmRelocationType);
  324. begin
  325. Address:=Aaddress;
  326. Symbol:=s;
  327. OrgSize:=0;
  328. Section:=nil;
  329. Typ:=Atyp;
  330. end;
  331. constructor TAsmRelocation.CreateSymbolSize(Aaddress:aint;s:Tasmsymbol;Aorgsize:aint;Atyp:TAsmRelocationType);
  332. begin
  333. Address:=Aaddress;
  334. Symbol:=s;
  335. OrgSize:=Aorgsize;
  336. Section:=nil;
  337. Typ:=Atyp;
  338. end;
  339. constructor TAsmRelocation.CreateSection(Aaddress:aint;sec:TAsmSection;Atyp:TAsmRelocationType);
  340. begin
  341. Address:=Aaddress;
  342. Symbol:=nil;
  343. OrgSize:=0;
  344. Section:=sec;
  345. Typ:=Atyp;
  346. end;
  347. {****************************************************************************
  348. TAsmSection
  349. ****************************************************************************}
  350. constructor TAsmSection.create(const Aname:string;Atype:TAsmSectionType;Aalign:longint;Aoptions:TAsmSectionOptions);
  351. begin
  352. inherited createname(Aname);
  353. sectype:=Atype;
  354. name:=Aname;
  355. secoptions:=Aoptions;
  356. secsymidx:=0;
  357. addralign:=Aalign;
  358. { data }
  359. datasize:=0;
  360. datapos:=0;
  361. if (aso_alloconly in aoptions) then
  362. data:=nil
  363. else
  364. Data:=TDynamicArray.Create(8192);
  365. { memory }
  366. mempos:=0;
  367. memsize:=0;
  368. { relocation }
  369. relocations:=TLinkedList.Create;
  370. end;
  371. destructor TAsmSection.destroy;
  372. begin
  373. if assigned(Data) then
  374. Data.Free;
  375. relocations.free;
  376. end;
  377. function TAsmSection.write(const d;l:aint):aint;
  378. begin
  379. write:=datasize;
  380. if assigned(Data) then
  381. Data.write(d,l);
  382. inc(datasize,l);
  383. end;
  384. function TAsmSection.writestr(const s:string):aint;
  385. begin
  386. writestr:=datasize;
  387. if assigned(Data) then
  388. Data.write(s[1],length(s));
  389. inc(datasize,length(s));
  390. end;
  391. procedure TAsmSection.writealign(l:longint);
  392. var
  393. i : longint;
  394. empty : array[0..63] of char;
  395. begin
  396. { no alignment needed for 0 or 1 }
  397. if l<=1 then
  398. exit;
  399. i:=datasize mod l;
  400. if i>0 then
  401. begin
  402. if assigned(data) then
  403. begin
  404. fillchar(empty,sizeof(empty),0);
  405. Data.write(empty,l-i);
  406. end;
  407. inc(datasize,l-i);
  408. end;
  409. end;
  410. function TAsmSection.aligneddatasize:aint;
  411. begin
  412. aligneddatasize:=align(datasize,addralign);
  413. end;
  414. procedure TAsmSection.setdatapos(var dpos:aint);
  415. var
  416. alignedpos : aint;
  417. begin
  418. { get aligned datapos }
  419. alignedpos:=align(dpos,addralign);
  420. dataalignbytes:=alignedpos-dpos;
  421. datapos:=alignedpos;
  422. { update datapos }
  423. dpos:=datapos+aligneddatasize;
  424. end;
  425. procedure TAsmSection.alignsection;
  426. begin
  427. writealign(addralign);
  428. end;
  429. procedure TAsmSection.alloc(l:aint);
  430. begin
  431. inc(datasize,l);
  432. end;
  433. procedure TAsmSection.addsymreloc(ofs:aint;p:tasmsymbol;relative:TAsmRelocationType);
  434. var
  435. r : TAsmRelocation;
  436. begin
  437. r:=TAsmRelocation.Create;
  438. r.address:=ofs;
  439. r.orgsize:=0;
  440. r.symbol:=p;
  441. r.section:=nil;
  442. r.typ:=relative;
  443. relocations.concat(r);
  444. end;
  445. procedure TAsmSection.addsectionreloc(ofs:aint;sec:TAsmSection;relative:TAsmRelocationType);
  446. var
  447. r : TAsmRelocation;
  448. begin
  449. r:=TAsmRelocation.Create;
  450. r.address:=ofs;
  451. r.symbol:=nil;
  452. r.orgsize:=0;
  453. r.section:=sec;
  454. r.typ:=relative;
  455. relocations.concat(r);
  456. end;
  457. procedure TAsmSection.fixuprelocs;
  458. begin
  459. end;
  460. {****************************************************************************
  461. TAsmObjectData
  462. ****************************************************************************}
  463. constructor TAsmObjectData.create(const n:string);
  464. begin
  465. inherited create;
  466. FName:=n;
  467. { sections }
  468. FSects:=tdictionary.create;
  469. FStabsRecSize:=1;
  470. FStabsSec:=nil;
  471. FStabStrSec:=nil;
  472. { symbols }
  473. FSymbols:=tindexarray.create(symbolsgrow);
  474. FSymbols.noclear:=true;
  475. { section class type for creating of new sections }
  476. FCAsmSection:=TAsmSection;
  477. end;
  478. destructor TAsmObjectData.destroy;
  479. begin
  480. FSects.free;
  481. FSymbols.free;
  482. end;
  483. function TAsmObjectData.sectionname(atype:tasmsectiontype;const aname:string):string;
  484. const
  485. secnames : array[tasmsectiontype] of string[12] = ('',
  486. 'code','data','rodata','bss',
  487. 'common',
  488. 'note',
  489. 'stab','stabstr',
  490. 'idata2','idata4','idata5','idata6','idata7','edata',
  491. 'eh_frame',
  492. 'debug_frame',
  493. 'fpc'
  494. );
  495. begin
  496. if aname<>'' then
  497. result:=secnames[atype]+'.'+aname
  498. else
  499. result:=secnames[atype];
  500. end;
  501. function TAsmObjectData.createsection(atype:tasmsectiontype;const aname:string;aalign:longint;aoptions:TAsmSectionOptions):TAsmSection;
  502. var
  503. secname : string;
  504. begin
  505. secname:=sectionname(atype,aname);
  506. result:=TasmSection(FSects.search(secname));
  507. if not assigned(result) then
  508. begin
  509. {$warning TODO make alloconly configurable}
  510. if atype=sec_bss then
  511. include(aoptions,aso_alloconly);
  512. result:=CAsmSection.create(secname,atype,aalign,aoptions);
  513. FSects.Insert(result);
  514. result.owner:=self;
  515. end;
  516. FCurrSec:=result;
  517. end;
  518. procedure TAsmObjectData.setsection(asec:tasmsection);
  519. begin
  520. if asec.owner<>self then
  521. internalerror(200403041);
  522. FCurrSec:=asec;
  523. end;
  524. procedure TAsmObjectData.writebytes(var data;len:aint);
  525. begin
  526. if not assigned(currsec) then
  527. internalerror(200402251);
  528. currsec.write(data,len);
  529. end;
  530. procedure TAsmObjectData.alloc(len:aint);
  531. begin
  532. if not assigned(currsec) then
  533. internalerror(200402252);
  534. currsec.alloc(len);
  535. end;
  536. procedure TAsmObjectData.allocalign(len:longint);
  537. var
  538. modulo : aint;
  539. begin
  540. if not assigned(currsec) then
  541. internalerror(200402253);
  542. modulo:=currsec.datasize mod len;
  543. if modulo > 0 then
  544. currsec.alloc(len-modulo);
  545. end;
  546. procedure TAsmObjectData.allocsymbol(currpass:byte;p:tasmsymbol;len:aint);
  547. begin
  548. p.setaddress(currpass,currsec,currsec.datasize,len);
  549. end;
  550. procedure TAsmObjectData.allocstabs(p:pchar);
  551. begin
  552. if not(assigned(FStabsSec) and assigned(FStabStrSec)) then
  553. internalerror(200402254);
  554. FStabsSec.alloc(FStabsRecSize);
  555. if assigned(p) and (p[0]<>#0) then
  556. FStabStrSec.alloc(strlen(p)+1);
  557. end;
  558. procedure TAsmObjectData.section_reset(p:tnamedindexitem;arg:pointer);
  559. begin
  560. with tasmsection(p) do
  561. begin
  562. datasize:=0;
  563. datapos:=0;
  564. end;
  565. end;
  566. procedure TAsmObjectData.section_fixuprelocs(p:tnamedindexitem;arg:pointer);
  567. begin
  568. tasmsection(p).fixuprelocs;
  569. end;
  570. procedure TAsmObjectData.beforealloc;
  571. begin
  572. end;
  573. procedure TAsmObjectData.beforewrite;
  574. begin
  575. end;
  576. procedure TAsmObjectData.afteralloc;
  577. begin
  578. end;
  579. procedure TAsmObjectData.afterwrite;
  580. begin
  581. end;
  582. procedure TAsmObjectData.resetsections;
  583. begin
  584. FSects.foreach(@section_reset,nil);
  585. end;
  586. procedure TAsmObjectData.fixuprelocs;
  587. begin
  588. FSects.foreach(@section_fixuprelocs,nil);
  589. end;
  590. {****************************************************************************
  591. TAsmLibraryData
  592. ****************************************************************************}
  593. constructor TAsmLibraryData.create(const n:string);
  594. begin
  595. inherited create;
  596. realname:=n;
  597. name:=upper(n);
  598. { symbols }
  599. symbolsearch:=tdictionary.create;
  600. symbolsearch.usehash;
  601. { labels }
  602. nextaltnr:=1;
  603. nextlabelnr:=1;
  604. { ppu }
  605. asmsymbolppuidx:=0;
  606. asmsymbolidx:=nil;
  607. end;
  608. destructor TAsmLibraryData.destroy;
  609. begin
  610. symbolsearch.free;
  611. Freeasmsymbolidx;
  612. end;
  613. procedure TAsmLibraryData.Freeasmsymbolidx;
  614. begin
  615. if assigned(asmsymbolidx) then
  616. begin
  617. Freemem(asmsymbolidx);
  618. asmsymbolidx:=nil;
  619. end;
  620. end;
  621. procedure TAsmLibraryData.DerefAsmsymbol(var s:tasmsymbol);
  622. begin
  623. if assigned(s) then
  624. begin
  625. if not assigned(asmsymbolidx) then
  626. internalerror(200208072);
  627. if (ptrint(pointer(s))<1) or (ptrint(pointer(s))>asmsymbolppuidx) then
  628. internalerror(200208073);
  629. s:=asmsymbolidx^[ptrint(pointer(s))-1];
  630. end;
  631. end;
  632. function TAsmLibraryData.newasmsymbol(const s : string;_bind:TAsmSymBind;_typ:Tasmsymtype) : tasmsymbol;
  633. var
  634. hp : tasmsymbol;
  635. begin
  636. hp:=tasmsymbol(symbolsearch.search(s));
  637. if assigned(hp) then
  638. begin
  639. {$IFDEF EXTDEBUG}
  640. if (_typ <> AT_NONE) and
  641. (hp.typ <> _typ) and
  642. not(cs_compilesystem in aktmoduleswitches) and
  643. (target_info.system <> system_powerpc_darwin) then
  644. begin
  645. //Writeln('Error symbol '+hp.name+' type is ',Ord(_typ),', should be ',Ord(hp.typ));
  646. InternalError(2004031501);
  647. end;
  648. {$ENDIF}
  649. if (_bind<>AB_EXTERNAL) then
  650. hp.defbind:=_bind
  651. end
  652. else
  653. begin
  654. { Not found, insert it. }
  655. hp:=tasmsymbol.create(s,_bind,_typ);
  656. symbolsearch.insert(hp);
  657. end;
  658. newasmsymbol:=hp;
  659. end;
  660. function TAsmLibraryData.getasmsymbol(const s : string) : tasmsymbol;
  661. begin
  662. getasmsymbol:=tasmsymbol(symbolsearch.search(s));
  663. end;
  664. function TAsmLibraryData.renameasmsymbol(const sold, snew : string):tasmsymbol;
  665. begin
  666. renameasmsymbol:=tasmsymbol(symbolsearch.rename(sold,snew));
  667. end;
  668. procedure TAsmLibraryData.CreateUsedAsmSymbolList;
  669. begin
  670. if assigned(usedasmsymbollist) then
  671. internalerror(78455782);
  672. usedasmsymbollist:=TSingleList.create;
  673. end;
  674. procedure TAsmLibraryData.DestroyUsedAsmSymbolList;
  675. begin
  676. usedasmsymbollist.destroy;
  677. usedasmsymbollist:=nil;
  678. end;
  679. procedure TAsmLibraryData.UsedAsmSymbolListInsert(p:tasmsymbol);
  680. begin
  681. if not p.inusedlist then
  682. usedasmsymbollist.insert(p);
  683. p.inusedlist:=true;
  684. end;
  685. procedure TAsmLibraryData.GenerateAltSymbol(p:tasmsymbol);
  686. begin
  687. if not assigned(p.altsymbol) then
  688. begin
  689. p.altsymbol:=tasmsymbol.create(p.name+'_'+tostr(nextaltnr),p.defbind,p.typ);
  690. symbolsearch.insert(p.altsymbol);
  691. { add also the original sym to the usedasmsymbollist,
  692. that list is used to reset the altsymbol }
  693. if not p.inusedlist then
  694. usedasmsymbollist.insert(p);
  695. p.inusedlist:=true;
  696. end;
  697. end;
  698. procedure TAsmLibraryData.UsedAsmSymbolListReset;
  699. var
  700. hp : tasmsymbol;
  701. begin
  702. hp:=tasmsymbol(usedasmsymbollist.first);
  703. while assigned(hp) do
  704. begin
  705. with hp do
  706. begin
  707. reset;
  708. inusedlist:=false;
  709. end;
  710. hp:=tasmsymbol(hp.listnext);
  711. end;
  712. end;
  713. procedure TAsmLibraryData.UsedAsmSymbolListResetAltSym;
  714. var
  715. hp : tasmsymbol;
  716. begin
  717. hp:=tasmsymbol(usedasmsymbollist.first);
  718. inc(nextaltnr);
  719. while assigned(hp) do
  720. begin
  721. with hp do
  722. begin
  723. altsymbol:=nil;
  724. inusedlist:=false;
  725. end;
  726. hp:=tasmsymbol(hp.listnext);
  727. end;
  728. end;
  729. procedure TAsmLibraryData.UsedAsmSymbolListCheckUndefined;
  730. var
  731. hp : tasmsymbol;
  732. begin
  733. hp:=tasmsymbol(usedasmsymbollist.first);
  734. while assigned(hp) do
  735. begin
  736. with hp do
  737. begin
  738. if is_used and
  739. (section=nil) and
  740. not(currbind in [AB_EXTERNAL,AB_COMMON]) then
  741. Message1(asmw_e_undefined_label,name);
  742. end;
  743. hp:=tasmsymbol(hp.listnext);
  744. end;
  745. end;
  746. function TAsmLibraryData.newasmlabel(nr:longint;is_addr,is_data:boolean) : tasmlabel;
  747. var
  748. hp : tasmlabel;
  749. begin
  750. if is_addr then
  751. hp:=tasmlabel.createaddr(nr)
  752. else if is_data then
  753. hp:=tasmlabel.createdata(name,nr)
  754. else
  755. hp:=tasmlabel.create(nr);
  756. symbolsearch.insert(hp);
  757. newasmlabel:=hp;
  758. end;
  759. procedure TAsmLibraryData.getlabel(var l : tasmlabel);
  760. begin
  761. l:=tasmlabel.create(nextlabelnr);
  762. inc(nextlabelnr);
  763. symbolsearch.insert(l);
  764. end;
  765. procedure TAsmLibraryData.getdatalabel(var l : tasmlabel);
  766. begin
  767. l:=tasmlabel.createdata(name,nextlabelnr);
  768. inc(nextlabelnr);
  769. symbolsearch.insert(l);
  770. end;
  771. procedure TAsmLibraryData.getaddrlabel(var l : tasmlabel);
  772. begin
  773. l:=tasmlabel.createaddr(nextlabelnr);
  774. inc(nextlabelnr);
  775. symbolsearch.insert(l);
  776. end;
  777. procedure TAsmLibraryData.getlabelnr(var l : longint);
  778. begin
  779. l:=nextlabelnr;
  780. inc(nextlabelnr);
  781. end;
  782. end.