aasm.pas 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180
  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. systems,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_80bit,
  44. ait_real_64bit,
  45. ait_real_32bit,
  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, { for register,temp allocation debugging }
  59. ait_tempalloc,
  60. ait_marker,
  61. { never used, makes insertation of new ait_ easier to type }
  62. ait_dummy);
  63. { asm symbol functions }
  64. type
  65. TAsmsymtype=(AS_EXTERNAL,AS_LOCAL,AS_GLOBAL);
  66. pasmsymbol = ^tasmsymbol;
  67. tasmsymbol = object(tnamedindexobject)
  68. idx : longint;
  69. section : tsection;
  70. address,
  71. size : longint;
  72. typ : TAsmsymtype;
  73. constructor init(const s:string);
  74. procedure reset;
  75. procedure setaddress(sec:tsection;offset,len:longint);
  76. end;
  77. pasmsymbollist = ^tasmsymbollist;
  78. tasmsymbollist = object(tdictionary)
  79. end;
  80. { the short name makes typing easier }
  81. pai = ^tai;
  82. tai = object(tlinkedlist_item)
  83. typ : tait;
  84. {pointer to record with optimizer info about this tai object}
  85. optinfo: pointer;
  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. sym : pasmsymbol; { filled in pass1 of ag386bin }
  135. constructor init(_l : plabel);
  136. destructor done; virtual;
  137. procedure setaddress(offset:longint);
  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(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_80bit;
  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 getasmsymbol(const s : string) : pasmsymbol;
  272. function renameasmsymbol(const sold, snew : string) : pasmsymbol;
  273. procedure ResetAsmsymbolList;
  274. { label functions }
  275. const
  276. nextlabelnr : longint = 1;
  277. countlabelref : boolean = true;
  278. { convert label to string}
  279. function lab2str(l : plabel) : string;
  280. { make l as a new label }
  281. procedure getlabel(var l : plabel);
  282. { make l as a new label and flag is_data }
  283. procedure getdatalabel(var l : plabel);
  284. { frees the label if unused }
  285. procedure freelabel(var l : plabel);
  286. { make a new zero label }
  287. procedure getzerolabel(var l : plabel);
  288. { reset a label to a zero label }
  289. procedure setzerolabel(var l : plabel);
  290. {just get a label number }
  291. procedure getlabelnr(var l : longint);
  292. implementation
  293. uses
  294. strings,verbose,globtype;
  295. {****************************************************************************
  296. TAI
  297. ****************************************************************************}
  298. constructor tai.init;
  299. begin
  300. optinfo := nil;
  301. fileinfo:=aktfilepos;
  302. end;
  303. {****************************************************************************
  304. TAI_SECTION
  305. ****************************************************************************}
  306. constructor tai_section.init(s : tsection);
  307. begin
  308. inherited init;
  309. typ:=ait_section;
  310. sec:=s;
  311. end;
  312. {****************************************************************************
  313. TAI_DATABLOCK
  314. ****************************************************************************}
  315. constructor tai_datablock.init(const _name : string;_size : longint);
  316. begin
  317. inherited init;
  318. typ:=ait_datablock;
  319. sym:=newasmsymbol(_name);
  320. concat_internal(_name,EXT_ANY);
  321. size:=_size;
  322. is_global:=false;
  323. end;
  324. constructor tai_datablock.init_global(const _name : string;_size : longint);
  325. begin
  326. inherited init;
  327. typ:=ait_datablock;
  328. sym:=newasmsymbol(_name);
  329. concat_internal(_name,EXT_ANY);
  330. size:=_size;
  331. is_global:=true;
  332. end;
  333. {****************************************************************************
  334. TAI_SYMBOL
  335. ****************************************************************************}
  336. constructor tai_symbol.init(const _name : string);
  337. begin
  338. inherited init;
  339. typ:=ait_symbol;
  340. sym:=newasmsymbol(_name);
  341. concat_internal(_name,EXT_ANY);
  342. is_global:=false;
  343. end;
  344. constructor tai_symbol.init_global(const _name : string);
  345. begin
  346. inherited init;
  347. typ:=ait_symbol;
  348. sym:=newasmsymbol(_name);
  349. concat_internal(_name,EXT_ANY);
  350. is_global:=true;
  351. end;
  352. {****************************************************************************
  353. TAI_EXTERNAL
  354. ****************************************************************************}
  355. constructor tai_external.init(_sym:pasmsymbol;exttype : texternal_typ);
  356. begin
  357. inherited init;
  358. typ:=ait_external;
  359. exttyp:=exttype;
  360. sym:=_sym;
  361. end;
  362. {****************************************************************************
  363. TAI_CONST
  364. ****************************************************************************}
  365. constructor tai_const.init_32bit(_value : longint);
  366. begin
  367. inherited init;
  368. typ:=ait_const_32bit;
  369. value:=_value;
  370. end;
  371. constructor tai_const.init_16bit(_value : word);
  372. begin
  373. inherited init;
  374. typ:=ait_const_16bit;
  375. value:=_value;
  376. end;
  377. constructor tai_const.init_8bit(_value : byte);
  378. begin
  379. inherited init;
  380. typ:=ait_const_8bit;
  381. value:=_value;
  382. end;
  383. {****************************************************************************
  384. TAI_CONST_SYMBOL_OFFSET
  385. ****************************************************************************}
  386. constructor tai_const_symbol.init(const name:string);
  387. begin
  388. inherited init;
  389. typ:=ait_const_symbol;
  390. sym:=newasmsymbol(name);
  391. offset:=0;
  392. end;
  393. constructor tai_const_symbol.init_offset(const name:string;ofs:longint);
  394. begin
  395. inherited init;
  396. typ:=ait_const_symbol;
  397. sym:=newasmsymbol(name);
  398. offset:=ofs;
  399. end;
  400. constructor tai_const_symbol.init_rva(const name:string);
  401. begin
  402. inherited init;
  403. typ:=ait_const_rva;
  404. sym:=newasmsymbol(name);
  405. offset:=0;
  406. end;
  407. {****************************************************************************
  408. TAI_DOUBLE
  409. ****************************************************************************}
  410. constructor tai_double.init(_value : double);
  411. begin
  412. inherited init;
  413. typ:=ait_real_64bit;
  414. value:=_value;
  415. end;
  416. {****************************************************************************
  417. TAI_SINGLE
  418. ****************************************************************************}
  419. constructor tai_single.init(_value : single);
  420. begin
  421. inherited init;
  422. typ:=ait_real_32bit;
  423. value:=_value;
  424. end;
  425. {****************************************************************************
  426. TAI_EXTENDED
  427. ****************************************************************************}
  428. constructor tai_extended.init(_value : bestreal);
  429. begin
  430. inherited init;
  431. typ:=ait_real_80bit;
  432. value:=_value;
  433. end;
  434. {****************************************************************************
  435. TAI_COMP
  436. ****************************************************************************}
  437. constructor tai_comp.init(_value : bestreal);
  438. begin
  439. inherited init;
  440. typ:=ait_comp;
  441. value:=_value;
  442. end;
  443. {$ifdef i386}
  444. constructor tai_comp.init_comp(_value : comp);
  445. begin
  446. inherited init;
  447. typ:=ait_comp;
  448. value:=_value;
  449. end;
  450. {$endif i386}
  451. {****************************************************************************
  452. TAI_STRING
  453. ****************************************************************************}
  454. constructor tai_string.init(const _str : string);
  455. begin
  456. inherited init;
  457. typ:=ait_string;
  458. getmem(str,length(_str)+1);
  459. strpcopy(str,_str);
  460. len:=length(_str);
  461. end;
  462. constructor tai_string.init_pchar(_str : pchar);
  463. begin
  464. inherited init;
  465. typ:=ait_string;
  466. str:=_str;
  467. len:=strlen(_str);
  468. end;
  469. constructor tai_string.init_length_pchar(_str : pchar;length : longint);
  470. begin
  471. inherited init;
  472. typ:=ait_string;
  473. str:=_str;
  474. len:=length;
  475. end;
  476. destructor tai_string.done;
  477. begin
  478. { you can have #0 inside the strings so }
  479. if str<>nil then
  480. freemem(str,len+1);
  481. inherited done;
  482. end;
  483. {****************************************************************************
  484. TAI_LABEL
  485. ****************************************************************************}
  486. constructor tai_label.init(_l : plabel);
  487. begin
  488. inherited init;
  489. typ:=ait_label;
  490. l:=_l;
  491. sym:=nil;
  492. l^.is_set:=true;
  493. end;
  494. destructor tai_label.done;
  495. begin
  496. if (l^.refcount>0) then
  497. { can now be disposed by a tai_labeled instruction !! }
  498. l^.is_set:=false
  499. else
  500. dispose(l);
  501. inherited done;
  502. end;
  503. procedure tai_label.setaddress(offset:longint);
  504. begin
  505. l^.address:=offset;
  506. end;
  507. {****************************************************************************
  508. TAI_DIRECT
  509. ****************************************************************************}
  510. constructor tai_direct.init(_str : pchar);
  511. begin
  512. inherited init;
  513. typ:=ait_direct;
  514. str:=_str;
  515. end;
  516. destructor tai_direct.done;
  517. begin
  518. strdispose(str);
  519. inherited done;
  520. end;
  521. {****************************************************************************
  522. TAI_ASM_COMMENT comment to be inserted in the assembler file
  523. ****************************************************************************}
  524. constructor tai_asm_comment.init(_str : pchar);
  525. begin
  526. inherited init;
  527. typ:=ait_comment;
  528. str:=_str;
  529. end;
  530. destructor tai_asm_comment.done;
  531. begin
  532. strdispose(str);
  533. inherited done;
  534. end;
  535. {****************************************************************************
  536. TAI_ALIGN
  537. ****************************************************************************}
  538. constructor tai_align.init(b: byte);
  539. begin
  540. inherited init;
  541. typ:=ait_align;
  542. if b in [1,2,4,8,16] then
  543. aligntype := b
  544. else
  545. aligntype := 1;
  546. fillsize:=0;
  547. fillop:=0;
  548. use_op:=false;
  549. end;
  550. constructor tai_align.init_op(b: byte; _op: byte);
  551. begin
  552. inherited init;
  553. typ:=ait_align;
  554. if b in [1,2,4,8,16] then
  555. aligntype := b
  556. else
  557. aligntype := 1;
  558. fillsize:=0;
  559. fillop:=_op;
  560. use_op:=true;
  561. end;
  562. {****************************************************************************
  563. TAI_CUT
  564. ****************************************************************************}
  565. constructor tai_cut.init;
  566. begin
  567. inherited init;
  568. typ:=ait_cut;
  569. endname:=false;
  570. end;
  571. constructor tai_cut.init_end;
  572. begin
  573. inherited init;
  574. typ:=ait_cut;
  575. endname:=true;
  576. end;
  577. {****************************************************************************
  578. Tai_Marker
  579. ****************************************************************************}
  580. Constructor Tai_Marker.Init(_Kind: TMarker);
  581. Begin
  582. Inherited Init;
  583. typ := ait_marker;
  584. Kind := _Kind;
  585. End;
  586. {*****************************************************************************
  587. External Helpers
  588. *****************************************************************************}
  589. function search_assembler_symbol(pl : paasmoutput;const _name : string;exttype : texternal_typ) : pai_external;
  590. var
  591. p : pai;
  592. begin
  593. search_assembler_symbol:=nil;
  594. if pl=nil then
  595. internalerror(2001)
  596. else
  597. begin
  598. p:=pai(pl^.first);
  599. while (p<>nil) and
  600. (p<>pai(pl^.last)) do
  601. { if we get the same name with a different typ }
  602. { there is probably an error }
  603. if (p^.typ=ait_external) and
  604. ((exttype=EXT_ANY) or (pai_external(p)^.exttyp=exttype)) and
  605. (pai_external(p)^.sym^.name=_name) then
  606. begin
  607. search_assembler_symbol:=pai_external(p);
  608. exit;
  609. end
  610. else
  611. p:=pai(p^.next);
  612. if (p<>nil) and
  613. (p^.typ=ait_external) and
  614. (pai_external(p)^.exttyp=exttype) and
  615. (pai_external(p)^.sym^.name=_name) then
  616. begin
  617. search_assembler_symbol:=pai_external(p);
  618. exit;
  619. end;
  620. end;
  621. end;
  622. { insert each need external only once }
  623. procedure concat_external(const _name : string;exttype : texternal_typ);
  624. var
  625. hp : pasmsymbol;
  626. begin
  627. if not target_asm.externals then
  628. exit;
  629. { insert in symbollist }
  630. hp:=newasmsymbol(_name);
  631. { insert in externals }
  632. if search_assembler_symbol(externals,_name,exttype)=nil then
  633. externals^.concat(new(pai_external,init(hp,exttype)));
  634. end;
  635. { insert each need internal only once }
  636. procedure concat_internal(const _name : string;exttype : texternal_typ);
  637. var
  638. hp : pasmsymbol;
  639. begin
  640. if not target_asm.externals then
  641. exit;
  642. { insert in symbollist }
  643. hp:=newasmsymbol(_name);
  644. { insert in externals }
  645. if search_assembler_symbol(internals,_name,exttype)=nil then
  646. internals^.concat(new(pai_external,init(hp,exttype)));
  647. end;
  648. {*****************************************************************************
  649. AsmSymbol
  650. *****************************************************************************}
  651. constructor tasmsymbol.init(const s:string);
  652. begin;
  653. inherited initname(s);
  654. reset;
  655. end;
  656. procedure tasmsymbol.reset;
  657. begin
  658. section:=sec_none;
  659. address:=0;
  660. size:=0;
  661. idx:=-1;
  662. typ:=AS_EXTERNAL;
  663. end;
  664. procedure tasmsymbol.setaddress(sec:tsection;offset,len:longint);
  665. begin
  666. section:=sec;
  667. address:=offset;
  668. size:=len;
  669. end;
  670. { generates an help record for constants }
  671. function newasmsymbol(const s : string) : pasmsymbol;
  672. var
  673. hp : pasmsymbol;
  674. begin
  675. hp:=pasmsymbol(asmsymbollist^.search(s));
  676. if assigned(hp) then
  677. begin
  678. newasmsymbol:=hp;
  679. exit;
  680. end;
  681. hp:=new(pasmsymbol,init(s));
  682. asmsymbollist^.insert(hp);
  683. newasmsymbol:=hp;
  684. end;
  685. { returns nil if non existing symbol }
  686. function getasmsymbol(const s : string) : pasmsymbol;
  687. begin
  688. getasmsymbol:=pasmsymbol(asmsymbollist^.search(s));
  689. end;
  690. { renames an asmsymbol }
  691. function renameasmsymbol(const sold, snew : string) : pasmsymbol;
  692. var
  693. hpold,hpnew : pasmsymbol;
  694. begin
  695. hpnew:=pasmsymbol(asmsymbollist^.search(snew));
  696. if assigned(hpnew) then
  697. internalerror(405405);
  698. hpold:=pasmsymbol(asmsymbollist^.search(sold));
  699. if not assigned(hpold) then
  700. internalerror(405406);
  701. hpnew:=new(pasmsymbol,init(sold));
  702. { replace the old one }
  703. { WARNING this heavily depends on the
  704. feature that tdictionnary.insert does not delete
  705. the tree element that it replaces !! }
  706. asmsymbollist^.replace_existing:=true;
  707. asmsymbollist^.insert(hpnew);
  708. asmsymbollist^.replace_existing:=false;
  709. { restore the tree }
  710. hpnew^.left:=hpold^.left;
  711. hpnew^.right:=hpold^.right;
  712. stringdispose(hpold^._name);
  713. hpold^._name:=stringdup(snew);
  714. hpold^.speedvalue:=getspeedvalue(snew);
  715. { now reinsert it at right location !! }
  716. asmsymbollist^.insert(hpold);
  717. renameasmsymbol:=hpold;
  718. end;
  719. procedure ResetAsmSym(p:Pnamedindexobject);{$ifndef FPC}far;{$endif}
  720. begin
  721. pasmsymbol(p)^.reset;
  722. end;
  723. procedure ResetAsmsymbolList;
  724. begin
  725. {$ifdef tp}
  726. asmsymbollist^.foreach(resetasmsym);
  727. {$else}
  728. asmsymbollist^.foreach(@resetasmsym);
  729. {$endif}
  730. end;
  731. {*****************************************************************************
  732. Label Helpers
  733. *****************************************************************************}
  734. function lab2str(l : plabel) : string;
  735. begin
  736. if (l=nil) or (l^.nb=0) then
  737. begin
  738. {$ifdef EXTDEBUG}
  739. lab2str:='ILLEGAL'
  740. {$else EXTDEBUG}
  741. internalerror(2000);
  742. {$endif EXTDEBUG}
  743. end
  744. else
  745. begin
  746. if (l^.is_data) and (cs_smartlink in aktmoduleswitches) then
  747. lab2str:='_$'+current_module^.modulename^+'$_L'+tostr(l^.nb)
  748. else
  749. lab2str:=target_asm.labelprefix+tostr(l^.nb);
  750. end;
  751. { inside the WriteTree we must not count the refs PM }
  752. {$ifndef HEAPTRC}
  753. if countlabelref then
  754. inc(l^.refcount);
  755. {$endif HEAPTRC}
  756. l^.is_symbol:=true;
  757. l^.is_used:=true;
  758. end;
  759. procedure getlabel(var l : plabel);
  760. begin
  761. new(l);
  762. l^.nb:=nextlabelnr;
  763. l^.is_used:=false;
  764. l^.is_set:=false;
  765. l^.is_data:=false;
  766. l^.is_symbol:=false;
  767. l^.address:=-1;
  768. l^.refcount:=0;
  769. inc(nextlabelnr);
  770. end;
  771. procedure getdatalabel(var l : plabel);
  772. begin
  773. new(l);
  774. l^.nb:=nextlabelnr;
  775. l^.is_used:=false;
  776. l^.is_set:=false;
  777. l^.is_data:=true;
  778. l^.is_symbol:=false;
  779. l^.address:=-1;
  780. l^.refcount:=0;
  781. inc(nextlabelnr);
  782. end;
  783. procedure freelabel(var l : plabel);
  784. begin
  785. if (l<>nil) and (not l^.is_set) and (not l^.is_used) then
  786. dispose(l);
  787. l:=nil;
  788. end;
  789. procedure setzerolabel(var l : plabel);
  790. begin
  791. with l^ do
  792. begin
  793. nb:=0;
  794. is_used:=false;
  795. is_set:=false;
  796. is_data:=false;
  797. is_symbol:=false;
  798. address:=-1;
  799. refcount:=0;
  800. end;
  801. end;
  802. procedure getzerolabel(var l : plabel);
  803. begin
  804. new(l);
  805. l^.nb:=0;
  806. l^.is_used:=false;
  807. l^.is_set:=false;
  808. l^.is_data:=false;
  809. l^.is_symbol:=false;
  810. l^.address:=-1;
  811. l^.refcount:=0;
  812. end;
  813. procedure getlabelnr(var l : longint);
  814. begin
  815. l:=nextlabelnr;
  816. inc(nextlabelnr);
  817. end;
  818. {*****************************************************************************
  819. TAAsmOutput
  820. *****************************************************************************}
  821. function taasmoutput.getlasttaifilepos : pfileposinfo;
  822. begin
  823. if assigned(last) then
  824. getlasttaifilepos:=@pai(last)^.fileinfo
  825. else
  826. getlasttaifilepos:=nil;
  827. end;
  828. end.
  829. {
  830. $Log$
  831. Revision 1.43 1999-05-08 20:38:02 jonas
  832. * seperate OPTimizer INFO pointer field in tai object
  833. Revision 1.42 1999/05/06 09:05:05 peter
  834. * generic write_float and str_float
  835. * fixed constant float conversions
  836. Revision 1.41 1999/05/02 22:41:46 peter
  837. * moved section names to systems
  838. * fixed nasm,intel writer
  839. Revision 1.40 1999/04/21 09:43:28 peter
  840. * storenumber works
  841. * fixed some typos in double_checksum
  842. + incompatible types type1 and type2 message (with storenumber)
  843. Revision 1.39 1999/04/16 11:49:36 peter
  844. + tempalloc
  845. + -at to show temp alloc info in .s file
  846. Revision 1.38 1999/04/14 09:14:44 peter
  847. * first things to store the symbol/def number in the ppu
  848. Revision 1.37 1999/03/10 13:25:42 pierre
  849. section order changed to get closer output from coff writer
  850. Revision 1.36 1999/03/08 14:51:04 peter
  851. + smartlinking for ag386bin
  852. Revision 1.35 1999/03/05 13:09:48 peter
  853. * first things for tai_cut support for ag386bin
  854. Revision 1.34 1999/03/03 11:59:27 pierre
  855. + getasmsymbol to search for existing assembler symbol only
  856. Revision 1.33 1999/03/02 02:56:08 peter
  857. + stabs support for binary writers
  858. * more fixes and missing updates from the previous commit :(
  859. Revision 1.32 1999/03/01 13:31:59 pierre
  860. * external used before implemented problem fixed
  861. Revision 1.31 1999/02/25 21:02:16 peter
  862. * ag386bin updates
  863. + coff writer
  864. Revision 1.30 1999/02/17 10:16:24 peter
  865. * small fixes for the binary writer
  866. Revision 1.29 1998/12/29 18:48:24 jonas
  867. + optimize pascal code surrounding assembler blocks
  868. Revision 1.28 1998/12/16 00:27:16 peter
  869. * removed some obsolete version checks
  870. Revision 1.27 1998/12/11 00:02:37 peter
  871. + globtype,tokens,version unit splitted from globals
  872. Revision 1.26 1998/12/01 23:36:31 pierre
  873. * zero padded alignment was buggy
  874. Revision 1.25 1998/11/30 09:42:52 pierre
  875. * some range check bugs fixed (still not working !)
  876. + added DLL writing support for win32 (also accepts variables)
  877. + TempAnsi for code that could be used for Temporary ansi strings
  878. handling
  879. Revision 1.24 1998/11/12 11:19:30 pierre
  880. * fix for first line of function break
  881. Revision 1.23 1998/10/14 15:56:37 pierre
  882. * all references to comp suppressed for m68k
  883. Revision 1.22 1998/10/12 12:20:38 pierre
  884. + added tai_const_symbol_offset
  885. for r : pointer = @var.field;
  886. * better message for different arg names on implementation
  887. of function
  888. Revision 1.21 1998/10/08 17:17:07 pierre
  889. * current_module old scanner tagged as invalid if unit is recompiled
  890. + added ppheap for better info on tracegetmem of heaptrc
  891. (adds line column and file index)
  892. * several memory leaks removed ith help of heaptrc !!
  893. Revision 1.20 1998/10/06 17:16:31 pierre
  894. * some memory leaks fixed (thanks to Peter for heaptrc !)
  895. Revision 1.19 1998/10/01 20:19:11 jonas
  896. + ait_marker support
  897. Revision 1.18 1998/09/20 17:11:25 jonas
  898. * released REGALLOC
  899. Revision 1.17 1998/09/07 18:33:31 peter
  900. + smartlinking for win95 imports
  901. Revision 1.16 1998/09/03 17:08:37 pierre
  902. * better lines for stabs
  903. (no scroll back to if before else part
  904. no return to case line at jump outside case)
  905. + source lines also if not in order
  906. Revision 1.15 1998/08/11 15:31:36 peter
  907. * write extended to ppu file
  908. * new version 0.99.7
  909. Revision 1.14 1998/08/10 23:56:03 peter
  910. * fixed extended writing
  911. Revision 1.13 1998/08/10 14:49:33 peter
  912. + localswitches, moduleswitches, globalswitches splitting
  913. Revision 1.12 1998/07/14 14:46:36 peter
  914. * released NEWINPUT
  915. Revision 1.11 1998/07/07 11:19:50 peter
  916. + NEWINPUT for a better inputfile and scanner object
  917. Revision 1.10 1998/06/08 22:59:41 peter
  918. * smartlinking works for win32
  919. * some defines to exclude some compiler parts
  920. Revision 1.9 1998/06/04 23:51:26 peter
  921. * m68k compiles
  922. + .def file creation moved to gendef.pas so it could also be used
  923. for win32
  924. Revision 1.8 1998/05/23 01:20:53 peter
  925. + aktasmmode, aktoptprocessor, aktoutputformat
  926. + smartlink per module $SMARTLINK-/+ (like MMX) and moved to aktswitches
  927. + $LIBNAME to set the library name where the unit will be put in
  928. * splitted cgi386 a bit (codeseg to large for bp7)
  929. * nasm, tasm works again. nasm moved to ag386nsm.pas
  930. Revision 1.7 1998/05/07 00:16:59 peter
  931. * smartlinking for sets
  932. + consts labels are now concated/generated in hcodegen
  933. * moved some cpu code to cga and some none cpu depended code from cga
  934. to tree and hcodegen and cleanup of hcodegen
  935. * assembling .. output reduced for smartlinking ;)
  936. Revision 1.6 1998/05/06 18:36:53 peter
  937. * tai_section extended with code,data,bss sections and enumerated type
  938. * ident 'compiled by FPC' moved to pmodules
  939. * small fix for smartlink
  940. Revision 1.5 1998/05/01 07:43:52 florian
  941. + basics for rtti implemented
  942. + switch $m (generate rtti for published sections)
  943. Revision 1.4 1998/04/29 10:33:40 pierre
  944. + added some code for ansistring (not complete nor working yet)
  945. * corrected operator overloading
  946. * corrected nasm output
  947. + started inline procedures
  948. + added starstarn : use ** for exponentiation (^ gave problems)
  949. + started UseTokenInfo cond to get accurate positions
  950. Revision 1.3 1998/04/27 23:10:27 peter
  951. + new scanner
  952. * $makelib -> if smartlink
  953. * small filename fixes pmodule.setfilename
  954. * moved import from files.pas -> import.pas
  955. Revision 1.2 1998/04/09 15:46:37 florian
  956. + register allocation tracing stuff added
  957. }