aasmbase.pas 29 KB

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