aasm.pas 31 KB

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