aasmbase.pas 27 KB

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