aasm.pas 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195
  1. {
  2. $Id$
  3. Copyright (c) 1996-98 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. unit aasm;
  19. interface
  20. uses
  21. globtype,systems,cobjects,files,globals;
  22. type
  23. { Types of operand }
  24. toptype=(top_none,top_reg,top_ref,top_const,top_symbol);
  25. toper=record
  26. ot : longint;
  27. case typ : toptype of
  28. top_none : ();
  29. top_reg : (reg:tregister);
  30. top_ref : (ref:preference);
  31. top_const : (val:longint);
  32. top_symbol : (sym:pasmsymbol;symofs:longint);
  33. end;
  34. tait = (
  35. ait_none,
  36. ait_direct,
  37. ait_string,
  38. ait_label,
  39. ait_comment,
  40. ait_instruction,
  41. ait_datablock,
  42. ait_symbol,
  43. ait_symbol_end, { needed to calc the size of a symbol }
  44. ait_const_32bit,
  45. ait_const_16bit,
  46. ait_const_8bit,
  47. ait_const_symbol,
  48. ait_real_80bit,
  49. ait_real_64bit,
  50. ait_real_32bit,
  51. ait_comp_64bit,
  52. ait_align,
  53. ait_section,
  54. { the following is only used by the win32 version of the compiler }
  55. { and only the GNU AS Win32 is able to write it }
  56. ait_const_rva,
  57. ait_stabn,
  58. ait_stabs,
  59. ait_force_line,
  60. ait_stab_function_name,
  61. ait_cut, { used to split into tiny assembler files }
  62. ait_regalloc, { for register,temp allocation debugging }
  63. ait_tempalloc,
  64. ait_marker,
  65. { never used, makes insertation of new ait_ easier to type }
  66. ait_dummy);
  67. { asm symbol functions }
  68. type
  69. TAsmsymtype=(AS_EXTERNAL,AS_LOCAL,AS_GLOBAL);
  70. pasmsymbol = ^tasmsymbol;
  71. tasmsymbol = object(tnamedindexobject)
  72. typ : TAsmsymtype;
  73. { this need te incremented with every symbol loading into the
  74. paasmoutput, thus in loadsym/loadref/const_symbol (PFV) }
  75. refs : longint;
  76. { the next fields are filled in the binary writer }
  77. idx : longint;
  78. section : tsection;
  79. address,
  80. size : longint;
  81. constructor init(const s:string;_typ:TAsmsymtype);
  82. procedure reset;
  83. function is_used:boolean;
  84. procedure setaddress(sec:tsection;offset,len:longint);
  85. end;
  86. pasmlabel = ^tasmlabel;
  87. tasmlabel = object(tasmsymbol)
  88. labelnr : longint;
  89. { this is set by the pai_label.init }
  90. is_set : boolean;
  91. constructor init;
  92. constructor initdata;
  93. function name:string;virtual;
  94. end;
  95. pasmsymbollist = ^tasmsymbollist;
  96. tasmsymbollist = object(tdictionary)
  97. end;
  98. { the short name makes typing easier }
  99. pai = ^tai;
  100. tai = object(tlinkedlist_item)
  101. typ : tait;
  102. { pointer to record with optimizer info about this tai object }
  103. optinfo : pointer;
  104. fileinfo : tfileposinfo;
  105. constructor init;
  106. end;
  107. pai_string = ^tai_string;
  108. tai_string = object(tai)
  109. str : pchar;
  110. { extra len so the string can contain an \0 }
  111. len : longint;
  112. constructor init(const _str : string);
  113. constructor init_pchar(_str : pchar);
  114. constructor init_length_pchar(_str : pchar;length : longint);
  115. destructor done;virtual;
  116. end;
  117. { generates a common label }
  118. pai_symbol = ^tai_symbol;
  119. tai_symbol = object(tai)
  120. sym : pasmsymbol;
  121. is_global : boolean;
  122. size : longint;
  123. constructor init(_sym:PAsmSymbol;siz:longint);
  124. constructor initname(const _name : string;siz:longint);
  125. constructor initname_global(const _name : string;siz:longint);
  126. end;
  127. pai_symbol_end = ^tai_symbol_end;
  128. tai_symbol_end = object(tai)
  129. sym : pasmsymbol;
  130. constructor init(_sym:PAsmSymbol);
  131. constructor initname(const _name : string);
  132. end;
  133. pai_label = ^tai_label;
  134. tai_label = object(tai)
  135. l : pasmlabel;
  136. is_global : boolean;
  137. constructor init(_l : pasmlabel);
  138. end;
  139. pai_direct = ^tai_direct;
  140. tai_direct = object(tai)
  141. str : pchar;
  142. constructor init(_str : pchar);
  143. destructor done; virtual;
  144. end;
  145. { to insert a comment into the generated assembler file }
  146. pai_asm_comment = ^tai_asm_comment;
  147. tai_asm_comment = object(tai)
  148. str : pchar;
  149. constructor init(_str : pchar);
  150. destructor done; virtual;
  151. end;
  152. { alignment for operator }
  153. pai_align = ^tai_align;
  154. tai_align = object(tai)
  155. aligntype : byte; { 1 = no align, 2 = word align, 4 = dword align }
  156. fillsize : byte; { real size to fill }
  157. fillop : byte; { value to fill with - optional }
  158. use_op : boolean;
  159. constructor init(b:byte);
  160. constructor init_op(b: byte; _op: byte);
  161. end;
  162. { Insert a section/segment directive }
  163. pai_section = ^tai_section;
  164. tai_section = object(tai)
  165. sec : tsection;
  166. constructor init(s : tsection);
  167. end;
  168. { generates an uninitializised data block }
  169. pai_datablock = ^tai_datablock;
  170. tai_datablock = object(tai)
  171. sym : pasmsymbol;
  172. size : longint;
  173. is_global : boolean;
  174. constructor init(const _name : string;_size : longint);
  175. constructor init_global(const _name : string;_size : longint);
  176. end;
  177. { generates a long integer (32 bit) }
  178. pai_const = ^tai_const;
  179. tai_const = object(tai)
  180. value : longint;
  181. constructor init_32bit(_value : longint);
  182. constructor init_16bit(_value : word);
  183. constructor init_8bit(_value : byte);
  184. end;
  185. pai_const_symbol = ^tai_const_symbol;
  186. tai_const_symbol = object(tai)
  187. sym : pasmsymbol;
  188. offset : longint;
  189. constructor init(_sym:PAsmSymbol);
  190. constructor init_offset(_sym:PAsmSymbol;ofs:longint);
  191. constructor init_rva(_sym:PAsmSymbol);
  192. constructor initname(const name:string);
  193. constructor initname_offset(const name:string;ofs:longint);
  194. constructor initname_rva(const name:string);
  195. end;
  196. { generates a single (32 bit real) }
  197. pai_real_32bit = ^tai_real_32bit;
  198. tai_real_32bit = object(tai)
  199. value : ts32real;
  200. constructor init(_value : ts32real);
  201. end;
  202. { generates a double (64 bit real) }
  203. pai_real_64bit = ^tai_real_64bit;
  204. tai_real_64bit = object(tai)
  205. value : ts64real;
  206. constructor init(_value : ts64real);
  207. end;
  208. { generates an extended (80 bit real) }
  209. pai_real_80bit = ^tai_real_80bit;
  210. tai_real_80bit = object(tai)
  211. value : ts80real;
  212. constructor init(_value : ts80real);
  213. end;
  214. { generates an comp (integer over 64 bits) }
  215. pai_comp_64bit = ^tai_comp_64bit;
  216. tai_comp_64bit = object(tai)
  217. value : ts64comp;
  218. constructor init(_value : ts64comp);
  219. end;
  220. { insert a cut to split into several smaller files }
  221. pai_cut = ^tai_cut;
  222. tai_cut = object(tai)
  223. endname : boolean;
  224. constructor init;
  225. constructor init_end;
  226. end;
  227. TMarker = (NoPropInfoStart, NoPropInfoEnd, AsmBlockStart, AsmBlockEnd);
  228. pai_marker = ^tai_marker;
  229. tai_marker = object(tai)
  230. Kind: TMarker;
  231. Constructor init(_Kind: TMarker);
  232. end;
  233. paitempalloc = ^taitempalloc;
  234. taitempalloc = object(tai)
  235. allocation : boolean;
  236. temppos,
  237. tempsize : longint;
  238. constructor alloc(pos,size:longint);
  239. constructor dealloc(pos,size:longint);
  240. end;
  241. { for each processor define the best precision }
  242. { bestreal is defined in globals }
  243. {$ifdef i386}
  244. const
  245. ait_bestreal = ait_real_80bit;
  246. type
  247. pai_bestreal = pai_real_80bit;
  248. tai_bestreal = tai_real_80bit;
  249. {$endif i386}
  250. {$ifdef m68k}
  251. const
  252. ait_bestreal = ait_real_32bit;
  253. type
  254. pai_bestreal = pai_real_32bit;
  255. tai_bestreal = tai_real_32bit;
  256. {$endif m68k}
  257. paasmoutput = ^taasmoutput;
  258. taasmoutput = object(tlinkedlist)
  259. function getlasttaifilepos : pfileposinfo;
  260. end;
  261. const
  262. { maximum of aasmoutput lists there will be }
  263. maxoutputlists = 10;
  264. var
  265. { temporary lists }
  266. exprasmlist,
  267. { default lists }
  268. datasegment,codesegment,bsssegment,
  269. debuglist,consts,
  270. importssection,exportssection,
  271. resourcesection,rttilist,
  272. resourcestringlist : paasmoutput;
  273. { asm symbol list }
  274. asmsymbollist : pasmsymbollist;
  275. const
  276. nextlabelnr : longint = 1;
  277. countlabelref : boolean = true;
  278. { make l as a new label }
  279. procedure getlabel(var l : pasmlabel);
  280. { make l as a new label and flag is_data }
  281. procedure getdatalabel(var l : pasmlabel);
  282. { free a label }
  283. procedure freelabel(var l : pasmlabel);
  284. {just get a label number }
  285. procedure getlabelnr(var l : longint);
  286. function newasmsymbol(const s : string) : pasmsymbol;
  287. function newasmsymboltyp(const s : string;_typ:TAsmSymType) : pasmsymbol;
  288. function getasmsymbol(const s : string) : pasmsymbol;
  289. function renameasmsymbol(const sold, snew : string):pasmsymbol;
  290. procedure ResetAsmsymbolList;
  291. implementation
  292. uses
  293. strings,verbose;
  294. {****************************************************************************
  295. TAI
  296. ****************************************************************************}
  297. constructor tai.init;
  298. begin
  299. optinfo := nil;
  300. fileinfo:=aktfilepos;
  301. end;
  302. {****************************************************************************
  303. TAI_SECTION
  304. ****************************************************************************}
  305. constructor tai_section.init(s : tsection);
  306. begin
  307. inherited init;
  308. typ:=ait_section;
  309. sec:=s;
  310. end;
  311. {****************************************************************************
  312. TAI_DATABLOCK
  313. ****************************************************************************}
  314. constructor tai_datablock.init(const _name : string;_size : longint);
  315. begin
  316. inherited init;
  317. typ:=ait_datablock;
  318. sym:=newasmsymboltyp(_name,AS_LOCAL);
  319. size:=_size;
  320. is_global:=false;
  321. end;
  322. constructor tai_datablock.init_global(const _name : string;_size : longint);
  323. begin
  324. inherited init;
  325. typ:=ait_datablock;
  326. sym:=newasmsymboltyp(_name,AS_GLOBAL);
  327. size:=_size;
  328. is_global:=true;
  329. end;
  330. {****************************************************************************
  331. TAI_SYMBOL
  332. ****************************************************************************}
  333. constructor tai_symbol.init(_sym:PAsmSymbol;siz:longint);
  334. begin
  335. inherited init;
  336. typ:=ait_symbol;
  337. sym:=_sym;
  338. size:=siz;
  339. is_global:=(sym^.typ=AS_GLOBAL);
  340. end;
  341. constructor tai_symbol.initname(const _name : string;siz:longint);
  342. begin
  343. inherited init;
  344. typ:=ait_symbol;
  345. sym:=newasmsymboltyp(_name,AS_LOCAL);
  346. size:=siz;
  347. is_global:=false;
  348. end;
  349. constructor tai_symbol.initname_global(const _name : string;siz:longint);
  350. begin
  351. inherited init;
  352. typ:=ait_symbol;
  353. sym:=newasmsymboltyp(_name,AS_GLOBAL);
  354. size:=siz;
  355. is_global:=true;
  356. end;
  357. {****************************************************************************
  358. TAI_SYMBOL
  359. ****************************************************************************}
  360. constructor tai_symbol_end.init(_sym:PAsmSymbol);
  361. begin
  362. inherited init;
  363. typ:=ait_symbol_end;
  364. sym:=_sym;
  365. end;
  366. constructor tai_symbol_end.initname(const _name : string);
  367. begin
  368. inherited init;
  369. typ:=ait_symbol_end;
  370. sym:=newasmsymboltyp(_name,AS_GLOBAL);
  371. end;
  372. {****************************************************************************
  373. TAI_CONST
  374. ****************************************************************************}
  375. constructor tai_const.init_32bit(_value : longint);
  376. begin
  377. inherited init;
  378. typ:=ait_const_32bit;
  379. value:=_value;
  380. end;
  381. constructor tai_const.init_16bit(_value : word);
  382. begin
  383. inherited init;
  384. typ:=ait_const_16bit;
  385. value:=_value;
  386. end;
  387. constructor tai_const.init_8bit(_value : byte);
  388. begin
  389. inherited init;
  390. typ:=ait_const_8bit;
  391. value:=_value;
  392. end;
  393. {****************************************************************************
  394. TAI_CONST_SYMBOL_OFFSET
  395. ****************************************************************************}
  396. constructor tai_const_symbol.init(_sym:PAsmSymbol);
  397. begin
  398. inherited init;
  399. typ:=ait_const_symbol;
  400. sym:=_sym;
  401. offset:=0;
  402. { update sym info }
  403. inc(sym^.refs);
  404. end;
  405. constructor tai_const_symbol.init_offset(_sym:PAsmSymbol;ofs:longint);
  406. begin
  407. inherited init;
  408. typ:=ait_const_symbol;
  409. sym:=_sym;
  410. offset:=ofs;
  411. { update sym info }
  412. inc(sym^.refs);
  413. end;
  414. constructor tai_const_symbol.init_rva(_sym:PAsmSymbol);
  415. begin
  416. inherited init;
  417. typ:=ait_const_rva;
  418. sym:=_sym;
  419. offset:=0;
  420. { update sym info }
  421. inc(sym^.refs);
  422. end;
  423. constructor tai_const_symbol.initname(const name:string);
  424. begin
  425. inherited init;
  426. typ:=ait_const_symbol;
  427. sym:=newasmsymbol(name);
  428. offset:=0;
  429. { update sym info }
  430. inc(sym^.refs);
  431. end;
  432. constructor tai_const_symbol.initname_offset(const name:string;ofs:longint);
  433. begin
  434. inherited init;
  435. typ:=ait_const_symbol;
  436. sym:=newasmsymbol(name);
  437. offset:=ofs;
  438. { update sym info }
  439. inc(sym^.refs);
  440. end;
  441. constructor tai_const_symbol.initname_rva(const name:string);
  442. begin
  443. inherited init;
  444. typ:=ait_const_rva;
  445. sym:=newasmsymbol(name);
  446. offset:=0;
  447. { update sym info }
  448. inc(sym^.refs);
  449. end;
  450. {****************************************************************************
  451. TAI_real_32bit
  452. ****************************************************************************}
  453. constructor tai_real_32bit.init(_value : ts32real);
  454. begin
  455. inherited init;
  456. typ:=ait_real_32bit;
  457. value:=_value;
  458. end;
  459. {****************************************************************************
  460. TAI_real_64bit
  461. ****************************************************************************}
  462. constructor tai_real_64bit.init(_value : ts64real);
  463. begin
  464. inherited init;
  465. typ:=ait_real_64bit;
  466. value:=_value;
  467. end;
  468. {****************************************************************************
  469. TAI_real_80bit
  470. ****************************************************************************}
  471. constructor tai_real_80bit.init(_value : ts80real);
  472. begin
  473. inherited init;
  474. typ:=ait_real_80bit;
  475. value:=_value;
  476. end;
  477. {****************************************************************************
  478. Tai_comp_64bit
  479. ****************************************************************************}
  480. constructor tai_comp_64bit.init(_value : ts64comp);
  481. begin
  482. inherited init;
  483. typ:=ait_comp_64bit;
  484. value:=_value;
  485. end;
  486. {****************************************************************************
  487. TAI_STRING
  488. ****************************************************************************}
  489. constructor tai_string.init(const _str : string);
  490. begin
  491. inherited init;
  492. typ:=ait_string;
  493. getmem(str,length(_str)+1);
  494. strpcopy(str,_str);
  495. len:=length(_str);
  496. end;
  497. constructor tai_string.init_pchar(_str : pchar);
  498. begin
  499. inherited init;
  500. typ:=ait_string;
  501. str:=_str;
  502. len:=strlen(_str);
  503. end;
  504. constructor tai_string.init_length_pchar(_str : pchar;length : longint);
  505. begin
  506. inherited init;
  507. typ:=ait_string;
  508. str:=_str;
  509. len:=length;
  510. end;
  511. destructor tai_string.done;
  512. begin
  513. { you can have #0 inside the strings so }
  514. if str<>nil then
  515. freemem(str,len+1);
  516. inherited done;
  517. end;
  518. {****************************************************************************
  519. TAI_LABEL
  520. ****************************************************************************}
  521. constructor tai_label.init(_l : pasmlabel);
  522. begin
  523. inherited init;
  524. typ:=ait_label;
  525. l:=_l;
  526. l^.is_set:=true;
  527. is_global:=(l^.typ=AS_GLOBAL);
  528. end;
  529. {****************************************************************************
  530. TAI_DIRECT
  531. ****************************************************************************}
  532. constructor tai_direct.init(_str : pchar);
  533. begin
  534. inherited init;
  535. typ:=ait_direct;
  536. str:=_str;
  537. end;
  538. destructor tai_direct.done;
  539. begin
  540. strdispose(str);
  541. inherited done;
  542. end;
  543. {****************************************************************************
  544. TAI_ASM_COMMENT comment to be inserted in the assembler file
  545. ****************************************************************************}
  546. constructor tai_asm_comment.init(_str : pchar);
  547. begin
  548. inherited init;
  549. typ:=ait_comment;
  550. str:=_str;
  551. end;
  552. destructor tai_asm_comment.done;
  553. begin
  554. strdispose(str);
  555. inherited done;
  556. end;
  557. {****************************************************************************
  558. TAI_ALIGN
  559. ****************************************************************************}
  560. constructor tai_align.init(b: byte);
  561. begin
  562. inherited init;
  563. typ:=ait_align;
  564. if b in [1,2,4,8,16] then
  565. aligntype := b
  566. else
  567. aligntype := 1;
  568. fillsize:=0;
  569. fillop:=0;
  570. use_op:=false;
  571. end;
  572. constructor tai_align.init_op(b: byte; _op: byte);
  573. begin
  574. inherited init;
  575. typ:=ait_align;
  576. if b in [1,2,4,8,16] then
  577. aligntype := b
  578. else
  579. aligntype := 1;
  580. fillsize:=0;
  581. fillop:=_op;
  582. use_op:=true;
  583. end;
  584. {****************************************************************************
  585. TAI_CUT
  586. ****************************************************************************}
  587. constructor tai_cut.init;
  588. begin
  589. inherited init;
  590. typ:=ait_cut;
  591. endname:=false;
  592. end;
  593. constructor tai_cut.init_end;
  594. begin
  595. inherited init;
  596. typ:=ait_cut;
  597. endname:=true;
  598. end;
  599. {****************************************************************************
  600. Tai_Marker
  601. ****************************************************************************}
  602. Constructor Tai_Marker.Init(_Kind: TMarker);
  603. Begin
  604. Inherited Init;
  605. typ := ait_marker;
  606. Kind := _Kind;
  607. End;
  608. {*****************************************************************************
  609. TaiTempAlloc
  610. *****************************************************************************}
  611. constructor taitempalloc.alloc(pos,size:longint);
  612. begin
  613. inherited init;
  614. typ:=ait_tempalloc;
  615. allocation:=true;
  616. temppos:=pos;
  617. tempsize:=size;
  618. end;
  619. constructor taitempalloc.dealloc(pos,size:longint);
  620. begin
  621. inherited init;
  622. typ:=ait_tempalloc;
  623. allocation:=false;
  624. temppos:=pos;
  625. tempsize:=size;
  626. end;
  627. {*****************************************************************************
  628. AsmSymbol
  629. *****************************************************************************}
  630. constructor tasmsymbol.init(const s:string;_typ:TAsmsymtype);
  631. begin;
  632. inherited initname(s);
  633. reset;
  634. typ:=_typ;
  635. end;
  636. procedure tasmsymbol.reset;
  637. begin
  638. section:=sec_none;
  639. address:=0;
  640. size:=0;
  641. idx:=-1;
  642. typ:=AS_EXTERNAL;
  643. { mainly used to remove unused labels from the codesegment }
  644. refs:=0;
  645. end;
  646. function tasmsymbol.is_used:boolean;
  647. begin
  648. is_used:=(refs>0);
  649. end;
  650. procedure tasmsymbol.setaddress(sec:tsection;offset,len:longint);
  651. begin
  652. section:=sec;
  653. address:=offset;
  654. size:=len;
  655. end;
  656. {*****************************************************************************
  657. AsmLabel
  658. *****************************************************************************}
  659. constructor tasmlabel.init;
  660. begin;
  661. labelnr:=nextlabelnr;
  662. inc(nextlabelnr);
  663. inherited init(target_asm.labelprefix+tostr(labelnr),AS_LOCAL);
  664. is_set:=false;
  665. end;
  666. constructor tasmlabel.initdata;
  667. begin;
  668. labelnr:=nextlabelnr;
  669. inc(nextlabelnr);
  670. if (cs_smartlink in aktmoduleswitches) then
  671. inherited init('_$'+current_module^.modulename^+'$_L'+tostr(labelnr),AS_GLOBAL)
  672. else
  673. inherited init(target_asm.labelprefix+tostr(labelnr),AS_LOCAL);
  674. is_set:=false;
  675. { write it always }
  676. refs:=1;
  677. end;
  678. function tasmlabel.name:string;
  679. begin
  680. name:=inherited name;
  681. inc(refs);
  682. end;
  683. {*****************************************************************************
  684. AsmSymbolList helpers
  685. *****************************************************************************}
  686. function newasmsymbol(const s : string) : pasmsymbol;
  687. var
  688. hp : pasmsymbol;
  689. begin
  690. hp:=pasmsymbol(asmsymbollist^.search(s));
  691. if assigned(hp) then
  692. begin
  693. newasmsymbol:=hp;
  694. exit;
  695. end;
  696. { Not found, insert it as an External }
  697. hp:=new(pasmsymbol,init(s,AS_EXTERNAL));
  698. asmsymbollist^.insert(hp);
  699. newasmsymbol:=hp;
  700. end;
  701. function newasmsymboltyp(const s : string;_typ:TAsmSymType) : pasmsymbol;
  702. var
  703. hp : pasmsymbol;
  704. begin
  705. hp:=pasmsymbol(asmsymbollist^.search(s));
  706. if assigned(hp) then
  707. begin
  708. hp^.typ:=_typ;
  709. newasmsymboltyp:=hp;
  710. exit;
  711. end;
  712. { Not found, insert it as an External }
  713. hp:=new(pasmsymbol,init(s,_typ));
  714. asmsymbollist^.insert(hp);
  715. newasmsymboltyp:=hp;
  716. end;
  717. function getasmsymbol(const s : string) : pasmsymbol;
  718. begin
  719. getasmsymbol:=pasmsymbol(asmsymbollist^.search(s));
  720. end;
  721. { renames an asmsymbol }
  722. function renameasmsymbol(const sold, snew : string):pasmsymbol;
  723. {$ifdef nodictonaryrename}
  724. var
  725. hpold,hpnew : pasmsymbol;
  726. begin
  727. hpnew:=pasmsymbol(asmsymbollist^.search(snew));
  728. if assigned(hpnew) then
  729. internalerror(405405);
  730. hpold:=pasmsymbol(asmsymbollist^.search(sold));
  731. if not assigned(hpold) then
  732. internalerror(405406);
  733. hpnew:=new(pasmsymbol,init(sold));
  734. { replace the old one }
  735. { WARNING this heavily depends on the
  736. feature that tdictionnary.insert does not delete
  737. the tree element that it replaces !! }
  738. asmsymbollist^.replace_existing:=true;
  739. asmsymbollist^.insert(hpnew);
  740. asmsymbollist^.replace_existing:=false;
  741. { restore the tree }
  742. hpnew^.left:=hpold^.left;
  743. hpnew^.right:=hpold^.right;
  744. stringdispose(hpold^._name);
  745. hpold^._name:=stringdup(snew);
  746. hpold^.speedvalue:=getspeedvalue(snew);
  747. { now reinsert it at right location !! }
  748. asmsymbollist^.insert(hpold);
  749. renameasmsymbol:=hpold;
  750. end;
  751. {$else}
  752. begin
  753. renameasmsymbol:=pasmsymbol(asmsymbollist^.rename(sold,snew));
  754. end;
  755. {$endif}
  756. procedure ResetAsmSym(p:Pnamedindexobject);{$ifndef FPC}far;{$endif}
  757. begin
  758. pasmsymbol(p)^.reset;
  759. end;
  760. procedure ResetAsmsymbolList;
  761. begin
  762. asmsymbollist^.foreach({$ifndef TP}@{$endif}resetasmsym);
  763. end;
  764. {*****************************************************************************
  765. Label Helpers
  766. *****************************************************************************}
  767. procedure getlabel(var l : pasmlabel);
  768. begin
  769. l:=new(pasmlabel,init);
  770. asmsymbollist^.insert(l);
  771. end;
  772. procedure getdatalabel(var l : pasmlabel);
  773. begin
  774. l:=new(pasmlabel,initdata);
  775. asmsymbollist^.insert(l);
  776. end;
  777. procedure freelabel(var l : pasmlabel);
  778. begin
  779. { nothing to do, the dispose of the asmsymbollist will do it }
  780. l:=nil;
  781. end;
  782. procedure getlabelnr(var l : longint);
  783. begin
  784. l:=nextlabelnr;
  785. inc(nextlabelnr);
  786. end;
  787. {*****************************************************************************
  788. TAAsmOutput
  789. *****************************************************************************}
  790. function taasmoutput.getlasttaifilepos : pfileposinfo;
  791. begin
  792. if assigned(last) then
  793. getlasttaifilepos:=@pai(last)^.fileinfo
  794. else
  795. getlasttaifilepos:=nil;
  796. end;
  797. end.
  798. {
  799. $Log$
  800. Revision 1.56 1999-08-02 20:45:47 michael
  801. * Moved toperand type to aasm
  802. Revision 1.55 1999/08/01 23:55:55 michael
  803. * Moved taitempalloc
  804. Revision 1.54 1999/07/29 20:53:55 peter
  805. * write .size also
  806. Revision 1.53 1999/07/22 09:37:28 florian
  807. + resourcestring implemented
  808. + start of longstring support
  809. Revision 1.52 1999/07/03 00:26:01 peter
  810. * ag386bin doesn't destroy the aasmoutput lists anymore
  811. Revision 1.51 1999/06/02 22:43:57 pierre
  812. * previous wrong log corrected
  813. Revision 1.50 1999/06/02 22:25:24 pierre
  814. * changed $ifdef FPC @ into $ifndef TP
  815. Revision 1.49 1999/06/01 14:45:41 peter
  816. * @procvar is now always needed for FPC
  817. Revision 1.48 1999/05/28 09:11:39 peter
  818. * also count ref when asmlabel^.name is used
  819. Revision 1.47 1999/05/27 19:43:55 peter
  820. * removed oldasm
  821. * plabel -> pasmlabel
  822. * -a switches to source writing automaticly
  823. * assembler readers OOPed
  824. * asmsymbol automaticly external
  825. * jumptables and other label fixes for asm readers
  826. Revision 1.46 1999/05/21 13:54:38 peter
  827. * NEWLAB for label as symbol
  828. Revision 1.45 1999/05/20 22:18:51 pierre
  829. * fix from Peter for double bug reported 20/05/1999
  830. Revision 1.44 1999/05/12 00:19:34 peter
  831. * removed R_DEFAULT_SEG
  832. * uniform float names
  833. Revision 1.43 1999/05/08 20:38:02 jonas
  834. * seperate OPTimizer INFO pointer field in tai object
  835. Revision 1.42 1999/05/06 09:05:05 peter
  836. * generic write_float and str_float
  837. * fixed constant float conversions
  838. Revision 1.41 1999/05/02 22:41:46 peter
  839. * moved section names to systems
  840. * fixed nasm,intel writer
  841. Revision 1.40 1999/04/21 09:43:28 peter
  842. * storenumber works
  843. * fixed some typos in double_checksum
  844. + incompatible types type1 and type2 message (with storenumber)
  845. Revision 1.39 1999/04/16 11:49:36 peter
  846. + tempalloc
  847. + -at to show temp alloc info in .s file
  848. Revision 1.38 1999/04/14 09:14:44 peter
  849. * first things to store the symbol/def number in the ppu
  850. Revision 1.37 1999/03/10 13:25:42 pierre
  851. section order changed to get closer output from coff writer
  852. Revision 1.36 1999/03/08 14:51:04 peter
  853. + smartlinking for ag386bin
  854. Revision 1.35 1999/03/05 13:09:48 peter
  855. * first things for tai_cut support for ag386bin
  856. Revision 1.34 1999/03/03 11:59:27 pierre
  857. + getasmsymbol to search for existing assembler symbol only
  858. Revision 1.33 1999/03/02 02:56:08 peter
  859. + stabs support for binary writers
  860. * more fixes and missing updates from the previous commit :(
  861. Revision 1.32 1999/03/01 13:31:59 pierre
  862. * external used before implemented problem fixed
  863. Revision 1.31 1999/02/25 21:02:16 peter
  864. * ag386bin updates
  865. + coff writer
  866. Revision 1.30 1999/02/17 10:16:24 peter
  867. * small fixes for the binary writer
  868. Revision 1.29 1998/12/29 18:48:24 jonas
  869. + optimize pascal code surrounding assembler blocks
  870. Revision 1.28 1998/12/16 00:27:16 peter
  871. * removed some obsolete version checks
  872. Revision 1.27 1998/12/11 00:02:37 peter
  873. + globtype,tokens,version unit splitted from globals
  874. Revision 1.26 1998/12/01 23:36:31 pierre
  875. * zero padded alignment was buggy
  876. Revision 1.25 1998/11/30 09:42:52 pierre
  877. * some range check bugs fixed (still not working !)
  878. + added DLL writing support for win32 (also accepts variables)
  879. + TempAnsi for code that could be used for Temporary ansi strings
  880. handling
  881. Revision 1.24 1998/11/12 11:19:30 pierre
  882. * fix for first line of function break
  883. Revision 1.23 1998/10/14 15:56:37 pierre
  884. * all references to comp suppressed for m68k
  885. Revision 1.22 1998/10/12 12:20:38 pierre
  886. + added tai_const_symbol_offset
  887. for r : pointer = @var.field;
  888. * better message for different arg names on implementation
  889. of function
  890. Revision 1.21 1998/10/08 17:17:07 pierre
  891. * current_module old scanner tagged as invalid if unit is recompiled
  892. + added ppheap for better info on tracegetmem of heaptrc
  893. (adds line column and file index)
  894. * several memory leaks removed ith help of heaptrc !!
  895. Revision 1.20 1998/10/06 17:16:31 pierre
  896. * some memory leaks fixed (thanks to Peter for heaptrc !)
  897. Revision 1.19 1998/10/01 20:19:11 jonas
  898. + ait_marker support
  899. Revision 1.18 1998/09/20 17:11:25 jonas
  900. * released REGALLOC
  901. Revision 1.17 1998/09/07 18:33:31 peter
  902. + smartlinking for win95 imports
  903. Revision 1.16 1998/09/03 17:08:37 pierre
  904. * better lines for stabs
  905. (no scroll back to if before else part
  906. no return to case line at jump outside case)
  907. + source lines also if not in order
  908. Revision 1.15 1998/08/11 15:31:36 peter
  909. * write extended to ppu file
  910. * new version 0.99.7
  911. Revision 1.14 1998/08/10 23:56:03 peter
  912. * fixed extended writing
  913. Revision 1.13 1998/08/10 14:49:33 peter
  914. + localswitches, moduleswitches, globalswitches splitting
  915. Revision 1.12 1998/07/14 14:46:36 peter
  916. * released NEWINPUT
  917. Revision 1.11 1998/07/07 11:19:50 peter
  918. + NEWINPUT for a better inputfile and scanner object
  919. Revision 1.10 1998/06/08 22:59:41 peter
  920. * smartlinking works for win32
  921. * some defines to exclude some compiler parts
  922. Revision 1.9 1998/06/04 23:51:26 peter
  923. * m68k compiles
  924. + .def file creation moved to gendef.pas so it could also be used
  925. for win32
  926. Revision 1.8 1998/05/23 01:20:53 peter
  927. + aktasmmode, aktoptprocessor, aktoutputformat
  928. + smartlink per module $SMARTLINK-/+ (like MMX) and moved to aktswitches
  929. + $LIBNAME to set the library name where the unit will be put in
  930. * splitted cgi386 a bit (codeseg to large for bp7)
  931. * nasm, tasm works again. nasm moved to ag386nsm.pas
  932. Revision 1.7 1998/05/07 00:16:59 peter
  933. * smartlinking for sets
  934. + consts labels are now concated/generated in hcodegen
  935. * moved some cpu code to cga and some none cpu depended code from cga
  936. to tree and hcodegen and cleanup of hcodegen
  937. * assembling .. output reduced for smartlinking ;)
  938. Revision 1.6 1998/05/06 18:36:53 peter
  939. * tai_section extended with code,data,bss sections and enumerated type
  940. * ident 'compiled by FPC' moved to pmodules
  941. * small fix for smartlink
  942. Revision 1.5 1998/05/01 07:43:52 florian
  943. + basics for rtti implemented
  944. + switch $m (generate rtti for published sections)
  945. Revision 1.4 1998/04/29 10:33:40 pierre
  946. + added some code for ansistring (not complete nor working yet)
  947. * corrected operator overloading
  948. * corrected nasm output
  949. + started inline procedures
  950. + added starstarn : use ** for exponentiation (^ gave problems)
  951. + started UseTokenInfo cond to get accurate positions
  952. Revision 1.3 1998/04/27 23:10:27 peter
  953. + new scanner
  954. * $makelib -> if smartlink
  955. * small filename fixes pmodule.setfilename
  956. * moved import from files.pas -> import.pas
  957. Revision 1.2 1998/04/09 15:46:37 florian
  958. + register allocation tracing stuff added
  959. }