aasm.pas 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 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,globals;
  22. type
  23. tait = (
  24. ait_none,
  25. ait_direct,
  26. ait_string,
  27. ait_label,
  28. ait_comment,
  29. ait_instruction,
  30. ait_datablock,
  31. ait_symbol,
  32. ait_symbol_end, { needed to calc the size of a symbol }
  33. ait_const_32bit,
  34. ait_const_16bit,
  35. ait_const_8bit,
  36. ait_const_symbol,
  37. ait_real_80bit,
  38. ait_real_64bit,
  39. ait_real_32bit,
  40. ait_comp_64bit,
  41. ait_align,
  42. ait_section,
  43. { the following is only used by the win32 version of the compiler }
  44. { and only the GNU AS Win32 is able to write it }
  45. ait_const_rva,
  46. ait_stabn,
  47. ait_stabs,
  48. ait_force_line,
  49. ait_stab_function_name,
  50. ait_cut, { used to split into tiny assembler files }
  51. ait_regalloc, { for register,temp allocation debugging }
  52. ait_tempalloc,
  53. ait_marker,
  54. { the follow is for the DEC Alpha }
  55. ait_frame,
  56. ait_ent,
  57. {$ifdef m68k}
  58. ait_labeled_instruction,
  59. {$endif m68k}
  60. { never used, makes insertation of new ait_ easier to type }
  61. { lazy guy !!!! ;-) (FK) }
  62. ait_dummy);
  63. tcpuflags = (cf_64bitaddr);
  64. tcpuflagset = set of tcpuflags;
  65. {$ifdef newcg}
  66. const
  67. SkipInstr = [ait_comment, ait_symbol
  68. {$ifdef GDB}
  69. ,ait_stabs, ait_stabn, ait_stab_function_name
  70. {$endif GDB}
  71. ,ait_regalloc, ait_tempalloc
  72. ];
  73. {$endif newcg}
  74. { asm symbol functions }
  75. type
  76. TAsmsymtype=(AS_NONE,AS_EXTERNAL,AS_LOCAL,AS_GLOBAL);
  77. pasmsymbol = ^tasmsymbol;
  78. tasmsymbol = object(tnamedindexobject)
  79. orgtyp,
  80. typ : TAsmsymtype;
  81. proclocal : boolean;
  82. { this need to be incremented with every symbol loading into the
  83. paasmoutput, thus in loadsym/loadref/const_symbol (PFV) }
  84. refs : longint;
  85. { the next fields are filled in the binary writer }
  86. idx : longint;
  87. section : tsection;
  88. address,
  89. size : longint;
  90. { alternate symbol which can be used for 'renaming' needed for
  91. inlining }
  92. altsymbol : pasmsymbol;
  93. constructor init(const s:string;_typ:TAsmsymtype);
  94. procedure reset;
  95. function is_used:boolean;
  96. procedure settyp(t:tasmsymtype);
  97. procedure setaddress(sec:tsection;offset,len:longint);
  98. procedure GenerateAltSymbol;
  99. end;
  100. pasmlabel = ^tasmlabel;
  101. tasmlabel = object(tasmsymbol)
  102. labelnr : longint;
  103. { this is set by the pai_label.init }
  104. is_set : boolean;
  105. constructor init;
  106. constructor initdata;
  107. function name:string;virtual;
  108. end;
  109. pasmsymbollist = ^tasmsymbollist;
  110. tasmsymbollist = object(tdictionary)
  111. end;
  112. { the short name makes typing easier }
  113. pai = ^tai;
  114. tai = object(tlinkedlist_item)
  115. typ : tait;
  116. { pointer to record with optimizer info about this tai object }
  117. optinfo : pointer;
  118. fileinfo : tfileposinfo;
  119. constructor init;
  120. end;
  121. pai_string = ^tai_string;
  122. tai_string = object(tai)
  123. str : pchar;
  124. { extra len so the string can contain an \0 }
  125. len : longint;
  126. constructor init(const _str : string);
  127. constructor init_pchar(_str : pchar);
  128. constructor init_length_pchar(_str : pchar;length : longint);
  129. destructor done;virtual;
  130. end;
  131. { generates a common label }
  132. pai_symbol = ^tai_symbol;
  133. tai_symbol = object(tai)
  134. sym : pasmsymbol;
  135. is_global : boolean;
  136. size : longint;
  137. constructor init(_sym:PAsmSymbol;siz:longint);
  138. constructor initname(const _name : string;siz:longint);
  139. constructor initname_global(const _name : string;siz:longint);
  140. end;
  141. pai_symbol_end = ^tai_symbol_end;
  142. tai_symbol_end = object(tai)
  143. sym : pasmsymbol;
  144. constructor init(_sym:PAsmSymbol);
  145. constructor initname(const _name : string);
  146. end;
  147. pai_label = ^tai_label;
  148. tai_label = object(tai)
  149. l : pasmlabel;
  150. is_global : boolean;
  151. constructor init(_l : pasmlabel);
  152. end;
  153. pai_direct = ^tai_direct;
  154. tai_direct = object(tai)
  155. str : pchar;
  156. constructor init(_str : pchar);
  157. destructor done; virtual;
  158. end;
  159. { to insert a comment into the generated assembler file }
  160. pai_asm_comment = ^tai_asm_comment;
  161. tai_asm_comment = object(tai)
  162. str : pchar;
  163. constructor init(_str : pchar);
  164. destructor done; virtual;
  165. end;
  166. { alignment for operator }
  167. pai_align_abstract = ^tai_align_abstract;
  168. tai_align_abstract = object(tai)
  169. buf : array[0..63] of char; { buf used for fill }
  170. aligntype : byte; { 1 = no align, 2 = word align, 4 = dword align }
  171. fillsize : byte; { real size to fill }
  172. fillop : byte; { value to fill with - optional }
  173. use_op : boolean;
  174. constructor init(b:byte);
  175. constructor init_op(b: byte; _op: byte);
  176. function getfillbuf:pchar;
  177. end;
  178. { Insert a section/segment directive }
  179. pai_section = ^tai_section;
  180. tai_section = object(tai)
  181. sec : tsection;
  182. constructor init(s : tsection);
  183. end;
  184. { generates an uninitializised data block }
  185. pai_datablock = ^tai_datablock;
  186. tai_datablock = object(tai)
  187. sym : pasmsymbol;
  188. size : longint;
  189. is_global : boolean;
  190. constructor init(const _name : string;_size : longint);
  191. constructor init_global(const _name : string;_size : longint);
  192. end;
  193. { generates a long integer (32 bit) }
  194. pai_const = ^tai_const;
  195. tai_const = object(tai)
  196. value : longint;
  197. constructor init_32bit(_value : longint);
  198. constructor init_16bit(_value : word);
  199. constructor init_8bit(_value : byte);
  200. end;
  201. pai_const_symbol = ^tai_const_symbol;
  202. tai_const_symbol = object(tai)
  203. sym : pasmsymbol;
  204. offset : longint;
  205. constructor init(_sym:PAsmSymbol);
  206. constructor init_offset(_sym:PAsmSymbol;ofs:longint);
  207. constructor init_rva(_sym:PAsmSymbol);
  208. constructor initname(const name:string);
  209. constructor initname_offset(const name:string;ofs:longint);
  210. constructor initname_rva(const name:string);
  211. end;
  212. { generates a single (32 bit real) }
  213. pai_real_32bit = ^tai_real_32bit;
  214. tai_real_32bit = object(tai)
  215. value : ts32real;
  216. constructor init(_value : ts32real);
  217. end;
  218. { generates a double (64 bit real) }
  219. pai_real_64bit = ^tai_real_64bit;
  220. tai_real_64bit = object(tai)
  221. value : ts64real;
  222. constructor init(_value : ts64real);
  223. end;
  224. { generates an extended (80 bit real) }
  225. pai_real_80bit = ^tai_real_80bit;
  226. tai_real_80bit = object(tai)
  227. value : ts80real;
  228. constructor init(_value : ts80real);
  229. end;
  230. { generates an comp (integer over 64 bits) }
  231. pai_comp_64bit = ^tai_comp_64bit;
  232. tai_comp_64bit = object(tai)
  233. value : ts64comp;
  234. constructor init(_value : ts64comp);
  235. end;
  236. { insert a cut to split into several smaller files }
  237. tcutplace=(cut_normal,cut_begin,cut_end);
  238. pai_cut = ^tai_cut;
  239. tai_cut = object(tai)
  240. place : tcutplace;
  241. constructor init;
  242. constructor init_begin;
  243. constructor init_end;
  244. end;
  245. TMarker = (NoPropInfoStart, NoPropInfoEnd, AsmBlockStart, AsmBlockEnd);
  246. pai_marker = ^tai_marker;
  247. tai_marker = object(tai)
  248. Kind: TMarker;
  249. Constructor init(_Kind: TMarker);
  250. end;
  251. paitempalloc = ^taitempalloc;
  252. taitempalloc = object(tai)
  253. allocation : boolean;
  254. temppos,
  255. tempsize : longint;
  256. constructor alloc(pos,size:longint);
  257. constructor dealloc(pos,size:longint);
  258. end;
  259. { for each processor define the best precision }
  260. { bestreal is defined in globals }
  261. {$ifdef i386}
  262. const
  263. ait_bestreal = ait_real_80bit;
  264. type
  265. pai_bestreal = pai_real_80bit;
  266. tai_bestreal = tai_real_80bit;
  267. {$endif i386}
  268. {$ifdef m68k}
  269. const
  270. ait_bestreal = ait_real_32bit;
  271. type
  272. pai_bestreal = pai_real_32bit;
  273. tai_bestreal = tai_real_32bit;
  274. {$endif m68k}
  275. paasmoutput = ^taasmoutput;
  276. taasmoutput = object(tlinkedlist)
  277. function getlasttaifilepos : pfileposinfo;
  278. end;
  279. const
  280. { maximum of aasmoutput lists there will be }
  281. maxoutputlists = 10;
  282. var
  283. { temporary lists }
  284. exprasmlist,
  285. { default lists }
  286. datasegment,codesegment,bsssegment,
  287. debuglist,consts,
  288. importssection,exportssection,
  289. resourcesection,rttilist,
  290. resourcestringlist : paasmoutput;
  291. { asm symbol list }
  292. asmsymbollist : pasmsymbollist;
  293. const
  294. nextaltnr : longint = 1;
  295. nextlabelnr : longint = 1;
  296. countlabelref : boolean = true;
  297. { make l as a new label }
  298. procedure getlabel(var l : pasmlabel);
  299. { make l as a new label and flag is_data }
  300. procedure getdatalabel(var l : pasmlabel);
  301. {just get a label number }
  302. procedure getlabelnr(var l : longint);
  303. function newasmsymbol(const s : string) : pasmsymbol;
  304. function newasmsymboltyp(const s : string;_typ:TAsmSymType) : pasmsymbol;
  305. function getasmsymbol(const s : string) : pasmsymbol;
  306. function renameasmsymbol(const sold, snew : string):pasmsymbol;
  307. procedure ResetAsmsymbolList;
  308. procedure ResetAsmSymbolListAltSymbol;
  309. procedure CheckAsmSymbolListUndefined;
  310. implementation
  311. uses
  312. strings,files,verbose;
  313. {****************************************************************************
  314. TAI
  315. ****************************************************************************}
  316. constructor tai.init;
  317. begin
  318. optinfo := nil;
  319. fileinfo:=aktfilepos;
  320. end;
  321. {****************************************************************************
  322. TAI_SECTION
  323. ****************************************************************************}
  324. constructor tai_section.init(s : tsection);
  325. begin
  326. inherited init;
  327. typ:=ait_section;
  328. sec:=s;
  329. end;
  330. {****************************************************************************
  331. TAI_DATABLOCK
  332. ****************************************************************************}
  333. constructor tai_datablock.init(const _name : string;_size : longint);
  334. begin
  335. inherited init;
  336. typ:=ait_datablock;
  337. sym:=newasmsymboltyp(_name,AS_LOCAL);
  338. size:=_size;
  339. is_global:=false;
  340. end;
  341. constructor tai_datablock.init_global(const _name : string;_size : longint);
  342. begin
  343. inherited init;
  344. typ:=ait_datablock;
  345. sym:=newasmsymboltyp(_name,AS_GLOBAL);
  346. size:=_size;
  347. is_global:=true;
  348. end;
  349. {****************************************************************************
  350. TAI_SYMBOL
  351. ****************************************************************************}
  352. constructor tai_symbol.init(_sym:PAsmSymbol;siz:longint);
  353. begin
  354. inherited init;
  355. typ:=ait_symbol;
  356. sym:=_sym;
  357. size:=siz;
  358. is_global:=(sym^.typ=AS_GLOBAL);
  359. end;
  360. constructor tai_symbol.initname(const _name : string;siz:longint);
  361. begin
  362. inherited init;
  363. typ:=ait_symbol;
  364. sym:=newasmsymboltyp(_name,AS_LOCAL);
  365. size:=siz;
  366. is_global:=false;
  367. end;
  368. constructor tai_symbol.initname_global(const _name : string;siz:longint);
  369. begin
  370. inherited init;
  371. typ:=ait_symbol;
  372. sym:=newasmsymboltyp(_name,AS_GLOBAL);
  373. size:=siz;
  374. is_global:=true;
  375. end;
  376. {****************************************************************************
  377. TAI_SYMBOL
  378. ****************************************************************************}
  379. constructor tai_symbol_end.init(_sym:PAsmSymbol);
  380. begin
  381. inherited init;
  382. typ:=ait_symbol_end;
  383. sym:=_sym;
  384. end;
  385. constructor tai_symbol_end.initname(const _name : string);
  386. begin
  387. inherited init;
  388. typ:=ait_symbol_end;
  389. sym:=newasmsymboltyp(_name,AS_GLOBAL);
  390. end;
  391. {****************************************************************************
  392. TAI_CONST
  393. ****************************************************************************}
  394. constructor tai_const.init_32bit(_value : longint);
  395. begin
  396. inherited init;
  397. typ:=ait_const_32bit;
  398. value:=_value;
  399. end;
  400. constructor tai_const.init_16bit(_value : word);
  401. begin
  402. inherited init;
  403. typ:=ait_const_16bit;
  404. value:=_value;
  405. end;
  406. constructor tai_const.init_8bit(_value : byte);
  407. begin
  408. inherited init;
  409. typ:=ait_const_8bit;
  410. value:=_value;
  411. end;
  412. {****************************************************************************
  413. TAI_CONST_SYMBOL_OFFSET
  414. ****************************************************************************}
  415. constructor tai_const_symbol.init(_sym:PAsmSymbol);
  416. begin
  417. inherited init;
  418. typ:=ait_const_symbol;
  419. sym:=_sym;
  420. offset:=0;
  421. { update sym info }
  422. inc(sym^.refs);
  423. end;
  424. constructor tai_const_symbol.init_offset(_sym:PAsmSymbol;ofs:longint);
  425. begin
  426. inherited init;
  427. typ:=ait_const_symbol;
  428. sym:=_sym;
  429. offset:=ofs;
  430. { update sym info }
  431. inc(sym^.refs);
  432. end;
  433. constructor tai_const_symbol.init_rva(_sym:PAsmSymbol);
  434. begin
  435. inherited init;
  436. typ:=ait_const_rva;
  437. sym:=_sym;
  438. offset:=0;
  439. { update sym info }
  440. inc(sym^.refs);
  441. end;
  442. constructor tai_const_symbol.initname(const name:string);
  443. begin
  444. inherited init;
  445. typ:=ait_const_symbol;
  446. sym:=newasmsymbol(name);
  447. offset:=0;
  448. { update sym info }
  449. inc(sym^.refs);
  450. end;
  451. constructor tai_const_symbol.initname_offset(const name:string;ofs:longint);
  452. begin
  453. inherited init;
  454. typ:=ait_const_symbol;
  455. sym:=newasmsymbol(name);
  456. offset:=ofs;
  457. { update sym info }
  458. inc(sym^.refs);
  459. end;
  460. constructor tai_const_symbol.initname_rva(const name:string);
  461. begin
  462. inherited init;
  463. typ:=ait_const_rva;
  464. sym:=newasmsymbol(name);
  465. offset:=0;
  466. { update sym info }
  467. inc(sym^.refs);
  468. end;
  469. {****************************************************************************
  470. TAI_real_32bit
  471. ****************************************************************************}
  472. constructor tai_real_32bit.init(_value : ts32real);
  473. begin
  474. inherited init;
  475. typ:=ait_real_32bit;
  476. value:=_value;
  477. end;
  478. {****************************************************************************
  479. TAI_real_64bit
  480. ****************************************************************************}
  481. constructor tai_real_64bit.init(_value : ts64real);
  482. begin
  483. inherited init;
  484. typ:=ait_real_64bit;
  485. value:=_value;
  486. end;
  487. {****************************************************************************
  488. TAI_real_80bit
  489. ****************************************************************************}
  490. constructor tai_real_80bit.init(_value : ts80real);
  491. begin
  492. inherited init;
  493. typ:=ait_real_80bit;
  494. value:=_value;
  495. end;
  496. {****************************************************************************
  497. Tai_comp_64bit
  498. ****************************************************************************}
  499. constructor tai_comp_64bit.init(_value : ts64comp);
  500. begin
  501. inherited init;
  502. typ:=ait_comp_64bit;
  503. value:=_value;
  504. end;
  505. {****************************************************************************
  506. TAI_STRING
  507. ****************************************************************************}
  508. constructor tai_string.init(const _str : string);
  509. begin
  510. inherited init;
  511. typ:=ait_string;
  512. getmem(str,length(_str)+1);
  513. strpcopy(str,_str);
  514. len:=length(_str);
  515. end;
  516. constructor tai_string.init_pchar(_str : pchar);
  517. begin
  518. inherited init;
  519. typ:=ait_string;
  520. str:=_str;
  521. len:=strlen(_str);
  522. end;
  523. constructor tai_string.init_length_pchar(_str : pchar;length : longint);
  524. begin
  525. inherited init;
  526. typ:=ait_string;
  527. str:=_str;
  528. len:=length;
  529. end;
  530. destructor tai_string.done;
  531. begin
  532. { you can have #0 inside the strings so }
  533. if str<>nil then
  534. freemem(str,len+1);
  535. inherited done;
  536. end;
  537. {****************************************************************************
  538. TAI_LABEL
  539. ****************************************************************************}
  540. constructor tai_label.init(_l : pasmlabel);
  541. begin
  542. inherited init;
  543. typ:=ait_label;
  544. l:=_l;
  545. l^.is_set:=true;
  546. is_global:=(l^.typ=AS_GLOBAL);
  547. end;
  548. {****************************************************************************
  549. TAI_DIRECT
  550. ****************************************************************************}
  551. constructor tai_direct.init(_str : pchar);
  552. begin
  553. inherited init;
  554. typ:=ait_direct;
  555. str:=_str;
  556. end;
  557. destructor tai_direct.done;
  558. begin
  559. strdispose(str);
  560. inherited done;
  561. end;
  562. {****************************************************************************
  563. TAI_ASM_COMMENT comment to be inserted in the assembler file
  564. ****************************************************************************}
  565. constructor tai_asm_comment.init(_str : pchar);
  566. begin
  567. inherited init;
  568. typ:=ait_comment;
  569. str:=_str;
  570. end;
  571. destructor tai_asm_comment.done;
  572. begin
  573. strdispose(str);
  574. inherited done;
  575. end;
  576. {****************************************************************************
  577. TAI_ALIGN
  578. ****************************************************************************}
  579. constructor tai_align_abstract.init(b: byte);
  580. begin
  581. inherited init;
  582. typ:=ait_align;
  583. if b in [1,2,4,8,16,32] then
  584. aligntype := b
  585. else
  586. aligntype := 1;
  587. fillsize:=0;
  588. fillop:=0;
  589. use_op:=false;
  590. end;
  591. constructor tai_align_abstract.init_op(b: byte; _op: byte);
  592. begin
  593. inherited init;
  594. typ:=ait_align;
  595. if b in [1,2,4,8,16,32] then
  596. aligntype := b
  597. else
  598. aligntype := 1;
  599. fillsize:=0;
  600. fillop:=_op;
  601. use_op:=true;
  602. fillchar(buf,sizeof(buf),_op)
  603. end;
  604. function tai_align_abstract.getfillbuf:pchar;
  605. begin
  606. getfillbuf:=@buf;
  607. end;
  608. {****************************************************************************
  609. TAI_CUT
  610. ****************************************************************************}
  611. constructor tai_cut.init;
  612. begin
  613. inherited init;
  614. typ:=ait_cut;
  615. place:=cut_normal;
  616. end;
  617. constructor tai_cut.init_begin;
  618. begin
  619. inherited init;
  620. typ:=ait_cut;
  621. place:=cut_begin;
  622. end;
  623. constructor tai_cut.init_end;
  624. begin
  625. inherited init;
  626. typ:=ait_cut;
  627. place:=cut_end;
  628. end;
  629. {****************************************************************************
  630. Tai_Marker
  631. ****************************************************************************}
  632. Constructor Tai_Marker.Init(_Kind: TMarker);
  633. Begin
  634. Inherited Init;
  635. typ := ait_marker;
  636. Kind := _Kind;
  637. End;
  638. {*****************************************************************************
  639. TaiTempAlloc
  640. *****************************************************************************}
  641. constructor taitempalloc.alloc(pos,size:longint);
  642. begin
  643. inherited init;
  644. typ:=ait_tempalloc;
  645. allocation:=true;
  646. temppos:=pos;
  647. tempsize:=size;
  648. end;
  649. constructor taitempalloc.dealloc(pos,size:longint);
  650. begin
  651. inherited init;
  652. typ:=ait_tempalloc;
  653. allocation:=false;
  654. temppos:=pos;
  655. tempsize:=size;
  656. end;
  657. {*****************************************************************************
  658. AsmSymbol
  659. *****************************************************************************}
  660. constructor tasmsymbol.init(const s:string;_typ:TAsmsymtype);
  661. begin;
  662. inherited initname(s);
  663. reset;
  664. typ:=_typ;
  665. end;
  666. procedure tasmsymbol.GenerateAltSymbol;
  667. begin
  668. if not assigned(altsymbol) then
  669. begin
  670. new(altsymbol,init(name+'_'+tostr(nextaltnr),typ));
  671. { also copy the amount of references }
  672. altsymbol^.refs:=refs;
  673. inc(nextaltnr);
  674. end;
  675. end;
  676. procedure tasmsymbol.reset;
  677. begin
  678. { save the original typ if not done yet }
  679. if orgtyp=AS_NONE then
  680. orgtyp:=typ;
  681. { reset section info }
  682. section:=sec_none;
  683. address:=0;
  684. size:=0;
  685. idx:=-1;
  686. typ:=AS_EXTERNAL;
  687. proclocal:=false;
  688. { mainly used to remove unused labels from the codesegment }
  689. refs:=0;
  690. end;
  691. function tasmsymbol.is_used:boolean;
  692. begin
  693. is_used:=(refs>0);
  694. end;
  695. procedure tasmsymbol.settyp(t:tasmsymtype);
  696. begin
  697. typ:=t;
  698. orgtyp:=t;
  699. end;
  700. procedure tasmsymbol.setaddress(sec:tsection;offset,len:longint);
  701. begin
  702. section:=sec;
  703. address:=offset;
  704. size:=len;
  705. { when the typ was reset to External, set it back to the original
  706. type it got when defined }
  707. if (typ=AS_EXTERNAL) and (orgtyp<>AS_NONE) then
  708. typ:=orgtyp;
  709. end;
  710. {*****************************************************************************
  711. AsmLabel
  712. *****************************************************************************}
  713. constructor tasmlabel.init;
  714. begin;
  715. labelnr:=nextlabelnr;
  716. inc(nextlabelnr);
  717. inherited init(target_asm.labelprefix+tostr(labelnr),AS_LOCAL);
  718. proclocal:=true;
  719. is_set:=false;
  720. end;
  721. constructor tasmlabel.initdata;
  722. begin;
  723. labelnr:=nextlabelnr;
  724. inc(nextlabelnr);
  725. if (cs_create_smart in aktmoduleswitches) then
  726. inherited init('_$'+current_module^.modulename^+'$_L'+tostr(labelnr),AS_GLOBAL)
  727. else
  728. inherited init(target_asm.labelprefix+tostr(labelnr),AS_LOCAL);
  729. is_set:=false;
  730. { write it always }
  731. refs:=1;
  732. end;
  733. function tasmlabel.name:string;
  734. begin
  735. name:=inherited name;
  736. inc(refs);
  737. end;
  738. {*****************************************************************************
  739. AsmSymbolList helpers
  740. *****************************************************************************}
  741. function newasmsymbol(const s : string) : pasmsymbol;
  742. var
  743. hp : pasmsymbol;
  744. begin
  745. hp:=pasmsymbol(asmsymbollist^.search(s));
  746. if assigned(hp) then
  747. begin
  748. newasmsymbol:=hp;
  749. exit;
  750. end;
  751. { Not found, insert it as an External }
  752. hp:=new(pasmsymbol,init(s,AS_EXTERNAL));
  753. asmsymbollist^.insert(hp);
  754. newasmsymbol:=hp;
  755. end;
  756. function newasmsymboltyp(const s : string;_typ:TAsmSymType) : pasmsymbol;
  757. var
  758. hp : pasmsymbol;
  759. begin
  760. hp:=pasmsymbol(asmsymbollist^.search(s));
  761. if assigned(hp) then
  762. begin
  763. hp^.settyp(_typ);
  764. newasmsymboltyp:=hp;
  765. exit;
  766. end;
  767. { Not found, insert it as an External }
  768. hp:=new(pasmsymbol,init(s,_typ));
  769. asmsymbollist^.insert(hp);
  770. newasmsymboltyp:=hp;
  771. end;
  772. function getasmsymbol(const s : string) : pasmsymbol;
  773. begin
  774. getasmsymbol:=pasmsymbol(asmsymbollist^.search(s));
  775. end;
  776. { renames an asmsymbol }
  777. function renameasmsymbol(const sold, snew : string):pasmsymbol;
  778. begin
  779. renameasmsymbol:=pasmsymbol(asmsymbollist^.rename(sold,snew));
  780. end;
  781. procedure ResetAsmSym(p:Pnamedindexobject);{$ifndef FPC}far;{$endif}
  782. begin
  783. pasmsymbol(p)^.reset;
  784. end;
  785. procedure ResetAsmsymbolList;
  786. begin
  787. asmsymbollist^.foreach({$ifndef TP}@{$endif}resetasmsym);
  788. end;
  789. procedure ResetAltSym(p:Pnamedindexobject);{$ifndef FPC}far;{$endif}
  790. begin
  791. pasmsymbol(p)^.altsymbol:=nil;
  792. end;
  793. procedure ResetAsmSymbolListAltSymbol;
  794. begin
  795. asmsymbollist^.foreach({$ifndef TP}@{$endif}resetaltsym);
  796. end;
  797. procedure checkundefinedasmsym(p:Pnamedindexobject);{$ifndef FPC}far;{$endif}
  798. begin
  799. if (pasmsymbol(p)^.refs>0) and
  800. (pasmsymbol(p)^.section=Sec_none) and
  801. (pasmsymbol(p)^.typ<>AS_EXTERNAL) then
  802. Message1(asmw_e_undefined_label,pasmsymbol(p)^.name);
  803. end;
  804. procedure CheckAsmSymbolListUndefined;
  805. begin
  806. asmsymbollist^.foreach({$ifndef TP}@{$endif}checkundefinedasmsym);
  807. end;
  808. {*****************************************************************************
  809. Label Helpers
  810. *****************************************************************************}
  811. procedure getlabel(var l : pasmlabel);
  812. begin
  813. l:=new(pasmlabel,init);
  814. asmsymbollist^.insert(l);
  815. end;
  816. procedure getdatalabel(var l : pasmlabel);
  817. begin
  818. l:=new(pasmlabel,initdata);
  819. asmsymbollist^.insert(l);
  820. end;
  821. procedure RegenerateLabel(var l : pasmlabel);
  822. begin
  823. if l^.proclocal then
  824. getlabel(pasmlabel(l^.altsymbol))
  825. else
  826. getdatalabel(pasmlabel(l^.altsymbol));
  827. end;
  828. procedure getlabelnr(var l : longint);
  829. begin
  830. l:=nextlabelnr;
  831. inc(nextlabelnr);
  832. end;
  833. {*****************************************************************************
  834. TAAsmOutput
  835. *****************************************************************************}
  836. function taasmoutput.getlasttaifilepos : pfileposinfo;
  837. begin
  838. if assigned(last) then
  839. getlasttaifilepos:=@pai(last)^.fileinfo
  840. else
  841. getlasttaifilepos:=nil;
  842. end;
  843. end.
  844. {
  845. $Log$
  846. Revision 1.72 2000-01-13 13:07:05 jonas
  847. * released -dalignreg
  848. * some small fixes to -dnewOptimizations helper procedures
  849. Revision 1.71 2000/01/12 10:38:16 peter
  850. * smartlinking fixes for binary writer
  851. * release alignreg code and moved instruction writing align to cpuasm,
  852. but it doesn't use the specified register yet
  853. Revision 1.70 2000/01/07 01:14:18 peter
  854. * updated copyright to 2000
  855. Revision 1.69 1999/12/22 01:01:46 peter
  856. - removed freelabel()
  857. * added undefined label detection in internal assembler, this prevents
  858. a lot of ld crashes and wrong .o files
  859. * .o files aren't written anymore if errors have occured
  860. * inlining of assembler labels is now correct
  861. Revision 1.68 1999/11/06 14:34:16 peter
  862. * truncated log to 20 revs
  863. Revision 1.67 1999/11/05 16:01:45 jonas
  864. + first implementation of choosing least used register for alignment code
  865. (not yet working, between ifdef alignreg)
  866. Revision 1.66 1999/11/02 15:06:56 peter
  867. * import library fixes for win32
  868. * alignment works again
  869. Revision 1.65 1999/10/27 16:11:27 peter
  870. * insns.dat is used to generate all i386*.inc files
  871. Revision 1.64 1999/09/20 16:38:51 peter
  872. * cs_create_smart instead of cs_smartlink
  873. * -CX is create smartlink
  874. * -CD is create dynamic, but does nothing atm.
  875. Revision 1.63 1999/09/16 23:05:51 florian
  876. * m68k compiler is again compilable (only gas writer, no assembler reader)
  877. Revision 1.62 1999/09/15 20:35:37 florian
  878. * small fix to operator overloading when in MMX mode
  879. + the compiler uses now fldz and fld1 if possible
  880. + some fixes to floating point registers
  881. + some math. functions (arctan, ln, sin, cos, sqrt, sqr, pi) are now inlined
  882. * .... ???
  883. Revision 1.61 1999/09/08 15:01:29 jonas
  884. * some small changes so the noew optimizer is again compilable
  885. Revision 1.60 1999/08/06 15:30:17 florian
  886. + cpu flags added, mainly for the new cg
  887. Revision 1.59 1999/08/05 15:51:01 michael
  888. * Added ait_frame, ait_ent
  889. Revision 1.58 1999/08/04 00:39:56 michael
  890. + Added ait_frame
  891. Revision 1.57 1999/08/02 21:01:41 michael
  892. * Moved toperand type back =(
  893. Revision 1.56 1999/08/02 20:45:47 michael
  894. * Moved toperand type to aasm
  895. Revision 1.55 1999/08/01 23:55:55 michael
  896. * Moved taitempalloc
  897. Revision 1.54 1999/07/29 20:53:55 peter
  898. * write .size also
  899. Revision 1.53 1999/07/22 09:37:28 florian
  900. + resourcestring implemented
  901. + start of longstring support
  902. Revision 1.52 1999/07/03 00:26:01 peter
  903. * ag386bin doesn't destroy the aasmoutput lists anymore
  904. Revision 1.51 1999/06/02 22:43:57 pierre
  905. * previous wrong log corrected
  906. Revision 1.50 1999/06/02 22:25:24 pierre
  907. * changed $ifdef FPC @ into $ifndef TP
  908. Revision 1.49 1999/06/01 14:45:41 peter
  909. * @procvar is now always needed for FPC
  910. Revision 1.48 1999/05/28 09:11:39 peter
  911. * also count ref when asmlabel^.name is used
  912. }