aasm.pas 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213
  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. {$i defines.inc}
  20. interface
  21. uses
  22. cutils,cobjects,cclasses,
  23. globtype,globals,systems;
  24. type
  25. tait = (
  26. ait_none,
  27. ait_direct,
  28. ait_string,
  29. ait_label,
  30. ait_comment,
  31. ait_instruction,
  32. ait_datablock,
  33. ait_symbol,
  34. ait_symbol_end, { needed to calc the size of a symbol }
  35. ait_const_32bit,
  36. ait_const_16bit,
  37. ait_const_8bit,
  38. ait_const_symbol,
  39. ait_real_80bit,
  40. ait_real_64bit,
  41. ait_real_32bit,
  42. ait_comp_64bit,
  43. ait_align,
  44. ait_section,
  45. { the following is only used by the win32 version of the compiler }
  46. { and only the GNU AS Win32 is able to write it }
  47. ait_const_rva,
  48. ait_stabn,
  49. ait_stabs,
  50. ait_force_line,
  51. ait_stab_function_name,
  52. ait_cut, { used to split into tiny assembler files }
  53. ait_regalloc, { for register,temp allocation debugging }
  54. ait_tempalloc,
  55. ait_marker,
  56. { the follow is for the DEC Alpha }
  57. ait_frame,
  58. ait_ent,
  59. {$ifdef m68k}
  60. ait_labeled_instruction,
  61. {$endif m68k}
  62. {$ifdef ia64}
  63. ait_bundle,
  64. ait_stop,
  65. {$endif ia64}
  66. { never used, makes insertation of new ait_ easier to type }
  67. { lazy guy !!!! ;-) (FK) }
  68. ait_dummy);
  69. tcpuflags = (cf_64bitaddr);
  70. tcpuflagset = set of tcpuflags;
  71. { ait_* types which don't result in executable code or which don't influence }
  72. { the way the program runs/behaves, but which may be encountered by the }
  73. { optimizer (= if it's sometimes added to the exprasm list). Update if you add }
  74. { a new ait type! }
  75. const
  76. SkipInstr = [ait_comment, ait_symbol,ait_force_line,ait_section
  77. {$ifdef GDB}
  78. ,ait_stabs, ait_stabn, ait_stab_function_name
  79. {$endif GDB}
  80. ,ait_regalloc, ait_tempalloc, ait_symbol_end
  81. ];
  82. { asm symbol functions }
  83. type
  84. TAsmsymbind=(AB_NONE,AB_EXTERNAL,AB_COMMON,AB_LOCAL,AB_GLOBAL);
  85. TAsmsymtype=(AT_NONE,AT_FUNCTION,AT_DATA,AT_SECTION);
  86. pasmsymbol = ^tasmsymbol;
  87. tasmsymbol = object(tnamedindexobject)
  88. defbind,
  89. bind : TAsmsymbind;
  90. typ : TAsmsymtype;
  91. { the next fields are filled in the binary writer }
  92. section : tsection;
  93. idx : longint;
  94. address,
  95. size : longint;
  96. { this need to be incremented with every symbol loading into the
  97. paasmoutput, thus in loadsym/loadref/const_symbol (PFV) }
  98. refs : longint;
  99. { alternate symbol which can be used for 'renaming' needed for
  100. inlining }
  101. altsymbol : pasmsymbol;
  102. { is the symbol local for a procedure/function }
  103. proclocal : boolean;
  104. { is the symbol in the used list }
  105. inusedlist : boolean;
  106. constructor init(const s:string;_bind:TAsmsymbind;_typ:Tasmsymtype);
  107. procedure reset;
  108. function is_used:boolean;
  109. procedure setaddress(sec:tsection;offset,len:longint);
  110. procedure GenerateAltSymbol;
  111. end;
  112. pasmlabel = ^tasmlabel;
  113. tasmlabel = object(tasmsymbol)
  114. {$ifdef PACKENUMFIXED}
  115. { this is set by the tai_label.Init }
  116. is_set,
  117. { is the label only there for getting an address (e.g. for i/o }
  118. { checks -> true) or is it a jump target (false) }
  119. is_addr : boolean;
  120. {$endif}
  121. labelnr : longint;
  122. {$ifndef PACKENUMFIXED}
  123. is_set,
  124. is_addr : boolean;
  125. {$endif}
  126. constructor init;
  127. constructor initdata;
  128. constructor initaddr;
  129. function name:string;virtual;
  130. end;
  131. { the short name makes typing easier }
  132. tai = class(tlinkedlistitem)
  133. {$ifndef PACKENUMFIXED}
  134. typ : tait;
  135. {$endif}
  136. { pointer to record with optimizer info about this tai object }
  137. optinfo : pointer;
  138. fileinfo : tfileposinfo;
  139. {$ifdef PACKENUMFIXED}
  140. { still 3 bytes left after the next field }
  141. typ : tait;
  142. {$endif}
  143. constructor Create;
  144. end;
  145. tai_string = class(tai)
  146. str : pchar;
  147. { extra len so the string can contain an \0 }
  148. len : longint;
  149. constructor Create(const _str : string);
  150. constructor Create_pchar(_str : pchar);
  151. constructor Create_length_pchar(_str : pchar;length : longint);
  152. destructor Destroy;override;
  153. end;
  154. { generates a common label }
  155. tai_symbol = class(tai)
  156. {$ifdef PACKENUMFIXED}
  157. is_global : boolean;
  158. {$endif}
  159. sym : pasmsymbol;
  160. size : longint;
  161. {$ifndef PACKENUMFIXED}
  162. is_global : boolean;
  163. {$endif}
  164. constructor Create(_sym:PAsmSymbol;siz:longint);
  165. constructor Createname(const _name : string;siz:longint);
  166. constructor Createname_global(const _name : string;siz:longint);
  167. constructor Createdataname(const _name : string;siz:longint);
  168. constructor Createdataname_global(const _name : string;siz:longint);
  169. end;
  170. tai_symbol_end = class(tai)
  171. sym : pasmsymbol;
  172. constructor Create(_sym:PAsmSymbol);
  173. constructor Createname(const _name : string);
  174. end;
  175. tai_label = class(tai)
  176. {$ifdef PACKENUMFIXED}
  177. is_global : boolean;
  178. {$endif}
  179. l : pasmlabel;
  180. {$ifndef PACKENUMFIXED}
  181. is_global : boolean;
  182. {$endif}
  183. constructor Create(_l : pasmlabel);
  184. end;
  185. tai_direct = class(tai)
  186. str : pchar;
  187. constructor Create(_str : pchar);
  188. destructor Destroy; override;
  189. end;
  190. { to insert a comment into the generated assembler file }
  191. tai_asm_comment = class(tai)
  192. str : pchar;
  193. constructor Create(_str : pchar);
  194. destructor Destroy; override;
  195. end;
  196. { alignment for operator }
  197. {$ifdef i386}
  198. tai_align_abstract = class(tai)
  199. {$else i386}
  200. tai_align = class(tai)
  201. {$endif i386}
  202. buf : array[0..63] of char; { buf used for fill }
  203. aligntype : byte; { 1 = no align, 2 = word align, 4 = dword align }
  204. fillsize : byte; { real size to fill }
  205. fillop : byte; { value to fill with - optional }
  206. use_op : boolean;
  207. constructor Create(b:byte);
  208. constructor Create_op(b: byte; _op: byte);
  209. function getfillbuf:pchar;
  210. end;
  211. { Insert a section/segment directive }
  212. tai_section = class(tai)
  213. sec : tsection;
  214. constructor Create(s : tsection);
  215. end;
  216. { generates an uninitializised data block }
  217. tai_datablock = class(tai)
  218. {$ifdef PACKENUMFIXED}
  219. is_global : boolean;
  220. {$endif}
  221. sym : pasmsymbol;
  222. size : longint;
  223. {$ifndef PACKENUMFIXED}
  224. is_global : boolean;
  225. {$endif}
  226. constructor Create(const _name : string;_size : longint);
  227. constructor Create_global(const _name : string;_size : longint);
  228. end;
  229. { generates a long integer (32 bit) }
  230. tai_const = class(tai)
  231. value : longint;
  232. constructor Create_32bit(_value : longint);
  233. constructor Create_16bit(_value : word);
  234. constructor Create_8bit(_value : byte);
  235. end;
  236. tai_const_symbol = class(tai)
  237. sym : pasmsymbol;
  238. offset : longint;
  239. constructor Create(_sym:PAsmSymbol);
  240. constructor Create_offset(_sym:PAsmSymbol;ofs:longint);
  241. constructor Create_rva(_sym:PAsmSymbol);
  242. constructor Createname(const name:string);
  243. constructor Createname_offset(const name:string;ofs:longint);
  244. constructor Createname_rva(const name:string);
  245. end;
  246. { generates a single (32 bit real) }
  247. tai_real_32bit = class(tai)
  248. value : ts32real;
  249. constructor Create(_value : ts32real);
  250. end;
  251. { generates a double (64 bit real) }
  252. tai_real_64bit = class(tai)
  253. value : ts64real;
  254. constructor Create(_value : ts64real);
  255. end;
  256. { generates an extended (80 bit real) }
  257. tai_real_80bit = class(tai)
  258. value : ts80real;
  259. constructor Create(_value : ts80real);
  260. end;
  261. { generates an comp (integer over 64 bits) }
  262. tai_comp_64bit = class(tai)
  263. value : ts64comp;
  264. constructor Create(_value : ts64comp);
  265. end;
  266. { insert a cut to split into several smaller files }
  267. tcutplace=(cut_normal,cut_begin,cut_end);
  268. tai_cut = class(tai)
  269. place : tcutplace;
  270. constructor Create;
  271. constructor Create_begin;
  272. constructor Create_end;
  273. end;
  274. TMarker = (NoPropInfoStart, NoPropInfoEnd,
  275. AsmBlockStart, AsmBlockEnd,
  276. InlineStart,InlineEnd
  277. );
  278. tai_marker = class(tai)
  279. Kind: TMarker;
  280. Constructor Create(_Kind: TMarker);
  281. end;
  282. taitempalloc = class(tai)
  283. {$ifdef PACKENUMFIXED}
  284. allocation : boolean;
  285. {$endif}
  286. temppos,
  287. tempsize : longint;
  288. {$ifndef PACKENUMFIXED}
  289. allocation : boolean;
  290. {$endif}
  291. constructor alloc(pos,size:longint);
  292. constructor dealloc(pos,size:longint);
  293. end;
  294. { for each processor define the best precision }
  295. { bestreal is defined in globals }
  296. {$ifdef i386}
  297. const
  298. ait_bestreal = ait_real_80bit;
  299. type
  300. tai_bestreal = tai_real_80bit;
  301. {$endif i386}
  302. {$ifdef m68k}
  303. const
  304. ait_bestreal = ait_real_32bit;
  305. type
  306. tai_bestreal = tai_real_32bit;
  307. {$endif m68k}
  308. taasmoutput = class(tlinkedlist)
  309. function getlasttaifilepos : pfileposinfo;
  310. end;
  311. const
  312. { maximum of aasmoutput lists there will be }
  313. maxoutputlists = 10;
  314. var
  315. { temporary lists }
  316. exprasmlist,
  317. { default lists }
  318. datasegment,codesegment,bsssegment,
  319. debuglist,withdebuglist,consts,
  320. importssection,exportssection,
  321. resourcesection,rttilist,
  322. resourcestringlist : taasmoutput;
  323. { asm symbol list }
  324. asmsymbollist : pdictionary;
  325. usedasmsymbollist : psinglelist;
  326. const
  327. nextaltnr : longint = 1;
  328. nextlabelnr : longint = 1;
  329. countlabelref : boolean = true;
  330. { make l as a new label }
  331. procedure getlabel(var l : pasmlabel);
  332. { make l as a new label and flag is_addr }
  333. procedure getaddrlabel(var l : pasmlabel);
  334. { make l as a new label and flag is_data }
  335. procedure getdatalabel(var l : pasmlabel);
  336. {just get a label number }
  337. procedure getlabelnr(var l : longint);
  338. function newasmsymbol(const s : string) : pasmsymbol;
  339. function newasmsymboltype(const s : string;_bind:TAsmSymBind;_typ:TAsmsymtype) : pasmsymbol;
  340. function getasmsymbol(const s : string) : pasmsymbol;
  341. function renameasmsymbol(const sold, snew : string):pasmsymbol;
  342. procedure CreateUsedAsmSymbolList;
  343. procedure DestroyUsedAsmSymbolList;
  344. procedure UsedAsmSymbolListInsert(p:pasmsymbol);
  345. procedure UsedAsmSymbolListReset;
  346. procedure UsedAsmSymbolListResetAltSym;
  347. procedure UsedAsmSymbolListCheckUndefined;
  348. implementation
  349. uses
  350. {$ifdef delphi}
  351. sysutils,
  352. {$else}
  353. strings,
  354. {$endif}
  355. fmodule,verbose;
  356. {****************************************************************************
  357. TAI
  358. ****************************************************************************}
  359. constructor tai.Create;
  360. begin
  361. optinfo := nil;
  362. fileinfo:=aktfilepos;
  363. end;
  364. {****************************************************************************
  365. TAI_SECTION
  366. ****************************************************************************}
  367. constructor tai_section.Create(s : tsection);
  368. begin
  369. inherited Create;
  370. typ:=ait_section;
  371. sec:=s;
  372. end;
  373. {****************************************************************************
  374. TAI_DATABLOCK
  375. ****************************************************************************}
  376. constructor tai_datablock.Create(const _name : string;_size : longint);
  377. begin
  378. inherited Create;
  379. typ:=ait_datablock;
  380. sym:=newasmsymboltype(_name,AB_LOCAL,AT_DATA);
  381. { keep things aligned }
  382. if _size<=0 then
  383. _size:=4;
  384. size:=_size;
  385. is_global:=false;
  386. end;
  387. constructor tai_datablock.Create_global(const _name : string;_size : longint);
  388. begin
  389. inherited Create;
  390. typ:=ait_datablock;
  391. sym:=newasmsymboltype(_name,AB_GLOBAL,AT_DATA);
  392. { keep things aligned }
  393. if _size<=0 then
  394. _size:=4;
  395. size:=_size;
  396. is_global:=true;
  397. end;
  398. {****************************************************************************
  399. TAI_SYMBOL
  400. ****************************************************************************}
  401. constructor tai_symbol.Create(_sym:PAsmSymbol;siz:longint);
  402. begin
  403. inherited Create;
  404. typ:=ait_symbol;
  405. sym:=_sym;
  406. size:=siz;
  407. is_global:=(sym^.defbind=AB_GLOBAL);
  408. end;
  409. constructor tai_symbol.Createname(const _name : string;siz:longint);
  410. begin
  411. inherited Create;
  412. typ:=ait_symbol;
  413. sym:=newasmsymboltype(_name,AB_LOCAL,AT_FUNCTION);
  414. size:=siz;
  415. is_global:=false;
  416. end;
  417. constructor tai_symbol.Createname_global(const _name : string;siz:longint);
  418. begin
  419. inherited Create;
  420. typ:=ait_symbol;
  421. sym:=newasmsymboltype(_name,AB_GLOBAL,AT_FUNCTION);
  422. size:=siz;
  423. is_global:=true;
  424. end;
  425. constructor tai_symbol.Createdataname(const _name : string;siz:longint);
  426. begin
  427. inherited Create;
  428. typ:=ait_symbol;
  429. sym:=newasmsymboltype(_name,AB_LOCAL,AT_DATA);
  430. size:=siz;
  431. is_global:=false;
  432. end;
  433. constructor tai_symbol.Createdataname_global(const _name : string;siz:longint);
  434. begin
  435. inherited Create;
  436. typ:=ait_symbol;
  437. sym:=newasmsymboltype(_name,AB_GLOBAL,AT_DATA);
  438. size:=siz;
  439. is_global:=true;
  440. end;
  441. {****************************************************************************
  442. TAI_SYMBOL
  443. ****************************************************************************}
  444. constructor tai_symbol_end.Create(_sym:PAsmSymbol);
  445. begin
  446. inherited Create;
  447. typ:=ait_symbol_end;
  448. sym:=_sym;
  449. end;
  450. constructor tai_symbol_end.Createname(const _name : string);
  451. begin
  452. inherited Create;
  453. typ:=ait_symbol_end;
  454. sym:=newasmsymboltype(_name,AB_GLOBAL,AT_NONE);
  455. end;
  456. {****************************************************************************
  457. TAI_CONST
  458. ****************************************************************************}
  459. constructor tai_const.Create_32bit(_value : longint);
  460. begin
  461. inherited Create;
  462. typ:=ait_const_32bit;
  463. value:=_value;
  464. end;
  465. constructor tai_const.Create_16bit(_value : word);
  466. begin
  467. inherited Create;
  468. typ:=ait_const_16bit;
  469. value:=_value;
  470. end;
  471. constructor tai_const.Create_8bit(_value : byte);
  472. begin
  473. inherited Create;
  474. typ:=ait_const_8bit;
  475. value:=_value;
  476. end;
  477. {****************************************************************************
  478. TAI_CONST_SYMBOL_OFFSET
  479. ****************************************************************************}
  480. constructor tai_const_symbol.Create(_sym:PAsmSymbol);
  481. begin
  482. inherited Create;
  483. typ:=ait_const_symbol;
  484. sym:=_sym;
  485. offset:=0;
  486. { update sym info }
  487. inc(sym^.refs);
  488. end;
  489. constructor tai_const_symbol.Create_offset(_sym:PAsmSymbol;ofs:longint);
  490. begin
  491. inherited Create;
  492. typ:=ait_const_symbol;
  493. sym:=_sym;
  494. offset:=ofs;
  495. { update sym info }
  496. inc(sym^.refs);
  497. end;
  498. constructor tai_const_symbol.Create_rva(_sym:PAsmSymbol);
  499. begin
  500. inherited Create;
  501. typ:=ait_const_rva;
  502. sym:=_sym;
  503. offset:=0;
  504. { update sym info }
  505. inc(sym^.refs);
  506. end;
  507. constructor tai_const_symbol.Createname(const name:string);
  508. begin
  509. inherited Create;
  510. typ:=ait_const_symbol;
  511. sym:=newasmsymbol(name);
  512. offset:=0;
  513. { update sym info }
  514. inc(sym^.refs);
  515. end;
  516. constructor tai_const_symbol.Createname_offset(const name:string;ofs:longint);
  517. begin
  518. inherited Create;
  519. typ:=ait_const_symbol;
  520. sym:=newasmsymbol(name);
  521. offset:=ofs;
  522. { update sym info }
  523. inc(sym^.refs);
  524. end;
  525. constructor tai_const_symbol.Createname_rva(const name:string);
  526. begin
  527. inherited Create;
  528. typ:=ait_const_rva;
  529. sym:=newasmsymbol(name);
  530. offset:=0;
  531. { update sym info }
  532. inc(sym^.refs);
  533. end;
  534. {****************************************************************************
  535. TAI_real_32bit
  536. ****************************************************************************}
  537. constructor tai_real_32bit.Create(_value : ts32real);
  538. begin
  539. inherited Create;
  540. typ:=ait_real_32bit;
  541. value:=_value;
  542. end;
  543. {****************************************************************************
  544. TAI_real_64bit
  545. ****************************************************************************}
  546. constructor tai_real_64bit.Create(_value : ts64real);
  547. begin
  548. inherited Create;
  549. typ:=ait_real_64bit;
  550. value:=_value;
  551. end;
  552. {****************************************************************************
  553. TAI_real_80bit
  554. ****************************************************************************}
  555. constructor tai_real_80bit.Create(_value : ts80real);
  556. begin
  557. inherited Create;
  558. typ:=ait_real_80bit;
  559. value:=_value;
  560. end;
  561. {****************************************************************************
  562. Tai_comp_64bit
  563. ****************************************************************************}
  564. constructor tai_comp_64bit.Create(_value : ts64comp);
  565. begin
  566. inherited Create;
  567. typ:=ait_comp_64bit;
  568. value:=_value;
  569. end;
  570. {****************************************************************************
  571. TAI_STRING
  572. ****************************************************************************}
  573. constructor tai_string.Create(const _str : string);
  574. begin
  575. inherited Create;
  576. typ:=ait_string;
  577. getmem(str,length(_str)+1);
  578. strpcopy(str,_str);
  579. len:=length(_str);
  580. end;
  581. constructor tai_string.Create_pchar(_str : pchar);
  582. begin
  583. inherited Create;
  584. typ:=ait_string;
  585. str:=_str;
  586. len:=strlen(_str);
  587. end;
  588. constructor tai_string.Create_length_pchar(_str : pchar;length : longint);
  589. begin
  590. inherited Create;
  591. typ:=ait_string;
  592. str:=_str;
  593. len:=length;
  594. end;
  595. destructor tai_string.destroy;
  596. begin
  597. { you can have #0 inside the strings so }
  598. if str<>nil then
  599. freemem(str,len+1);
  600. inherited Destroy;
  601. end;
  602. {****************************************************************************
  603. TAI_LABEL
  604. ****************************************************************************}
  605. constructor tai_label.create(_l : pasmlabel);
  606. begin
  607. inherited Create;
  608. typ:=ait_label;
  609. l:=_l;
  610. l^.is_set:=true;
  611. is_global:=(l^.defbind=AB_GLOBAL);
  612. end;
  613. {****************************************************************************
  614. TAI_DIRECT
  615. ****************************************************************************}
  616. constructor tai_direct.Create(_str : pchar);
  617. begin
  618. inherited Create;
  619. typ:=ait_direct;
  620. str:=_str;
  621. end;
  622. destructor tai_direct.destroy;
  623. begin
  624. strdispose(str);
  625. inherited Destroy;
  626. end;
  627. {****************************************************************************
  628. TAI_ASM_COMMENT comment to be inserted in the assembler file
  629. ****************************************************************************}
  630. constructor tai_asm_comment.Create(_str : pchar);
  631. begin
  632. inherited Create;
  633. typ:=ait_comment;
  634. str:=_str;
  635. end;
  636. destructor tai_asm_comment.destroy;
  637. begin
  638. strdispose(str);
  639. inherited Destroy;
  640. end;
  641. {****************************************************************************
  642. TAI_ALIGN
  643. ****************************************************************************}
  644. {$ifdef i386}
  645. constructor tai_align_abstract.Create(b: byte);
  646. {$else i386}
  647. constructor tai_align.Create(b: byte);
  648. {$endif i386}
  649. begin
  650. inherited Create;
  651. typ:=ait_align;
  652. if b in [1,2,4,8,16,32] then
  653. aligntype := b
  654. else
  655. aligntype := 1;
  656. fillsize:=0;
  657. fillop:=0;
  658. use_op:=false;
  659. end;
  660. {$ifdef i386}
  661. constructor tai_align_abstract.Create_op(b: byte; _op: byte);
  662. {$else i386}
  663. constructor tai_align.Create_op(b: byte; _op: byte);
  664. {$endif i386}
  665. begin
  666. inherited Create;
  667. typ:=ait_align;
  668. if b in [1,2,4,8,16,32] then
  669. aligntype := b
  670. else
  671. aligntype := 1;
  672. fillsize:=0;
  673. fillop:=_op;
  674. use_op:=true;
  675. fillchar(buf,sizeof(buf),_op)
  676. end;
  677. {$ifdef i386}
  678. function tai_align_abstract.getfillbuf:pchar;
  679. {$else i386}
  680. function tai_align.getfillbuf:pchar;
  681. {$endif i386}
  682. begin
  683. getfillbuf:=@buf;
  684. end;
  685. {****************************************************************************
  686. TAI_CUT
  687. ****************************************************************************}
  688. constructor tai_cut.Create;
  689. begin
  690. inherited Create;
  691. typ:=ait_cut;
  692. place:=cut_normal;
  693. end;
  694. constructor tai_cut.Create_begin;
  695. begin
  696. inherited Create;
  697. typ:=ait_cut;
  698. place:=cut_begin;
  699. end;
  700. constructor tai_cut.Create_end;
  701. begin
  702. inherited Create;
  703. typ:=ait_cut;
  704. place:=cut_end;
  705. end;
  706. {****************************************************************************
  707. Tai_Marker
  708. ****************************************************************************}
  709. Constructor Tai_Marker.Create(_Kind: TMarker);
  710. Begin
  711. Inherited Create;
  712. typ := ait_marker;
  713. Kind := _Kind;
  714. End;
  715. {*****************************************************************************
  716. TaiTempAlloc
  717. *****************************************************************************}
  718. constructor taitempalloc.alloc(pos,size:longint);
  719. begin
  720. inherited Create;
  721. typ:=ait_tempalloc;
  722. allocation:=true;
  723. temppos:=pos;
  724. tempsize:=size;
  725. end;
  726. constructor taitempalloc.dealloc(pos,size:longint);
  727. begin
  728. inherited Create;
  729. typ:=ait_tempalloc;
  730. allocation:=false;
  731. temppos:=pos;
  732. tempsize:=size;
  733. end;
  734. {*****************************************************************************
  735. AsmSymbol
  736. *****************************************************************************}
  737. constructor tasmsymbol.Init(const s:string;_bind:TAsmsymbind;_typ:Tasmsymtype);
  738. begin;
  739. inherited initname(s);
  740. reset;
  741. defbind:=_bind;
  742. typ:=_typ;
  743. inusedlist:=false;
  744. { mainly used to remove unused labels from the codesegment }
  745. refs:=0;
  746. end;
  747. procedure tasmsymbol.GenerateAltSymbol;
  748. begin
  749. if not assigned(altsymbol) then
  750. begin
  751. new(altsymbol,init(name+'_'+tostr(nextaltnr),bind,typ));
  752. { also copy the amount of references }
  753. altsymbol^.refs:=refs;
  754. inc(nextaltnr);
  755. end;
  756. end;
  757. procedure tasmsymbol.reset;
  758. begin
  759. { reset section info }
  760. section:=sec_none;
  761. address:=0;
  762. size:=0;
  763. idx:=-1;
  764. bind:=AB_EXTERNAL;
  765. proclocal:=false;
  766. end;
  767. function tasmsymbol.is_used:boolean;
  768. begin
  769. is_used:=(refs>0);
  770. end;
  771. procedure tasmsymbol.setaddress(sec:tsection;offset,len:longint);
  772. begin
  773. section:=sec;
  774. address:=offset;
  775. size:=len;
  776. { when the bind was reset to External, set it back to the default
  777. bind it got when defined }
  778. if (bind=AB_EXTERNAL) and (defbind<>AB_NONE) then
  779. bind:=defbind;
  780. end;
  781. {*****************************************************************************
  782. AsmLabel
  783. *****************************************************************************}
  784. constructor tasmlabel.Init;
  785. begin;
  786. labelnr:=nextlabelnr;
  787. inc(nextlabelnr);
  788. inherited init(target_asm.labelprefix+tostr(labelnr),AB_LOCAL,AT_FUNCTION);
  789. proclocal:=true;
  790. is_set:=false;
  791. is_addr := false;
  792. end;
  793. constructor tasmlabel.Initdata;
  794. begin;
  795. labelnr:=nextlabelnr;
  796. inc(nextlabelnr);
  797. if (cs_create_smart in aktmoduleswitches) then
  798. inherited init('_$'+current_module.modulename^+'$_L'+tostr(labelnr),AB_GLOBAL,AT_DATA)
  799. else
  800. inherited init(target_asm.labelprefix+tostr(labelnr),AB_LOCAL,AT_DATA);
  801. is_set:=false;
  802. is_addr := false;
  803. { write it always }
  804. refs:=1;
  805. end;
  806. constructor tasmlabel.Initaddr;
  807. begin;
  808. Init;
  809. is_addr := true;
  810. end;
  811. function tasmlabel.name:string;
  812. begin
  813. name:=inherited name;
  814. inc(refs);
  815. end;
  816. {*****************************************************************************
  817. AsmSymbolList helpers
  818. *****************************************************************************}
  819. function newasmsymbol(const s : string) : pasmsymbol;
  820. var
  821. hp : pasmsymbol;
  822. begin
  823. hp:=pasmsymbol(asmsymbollist^.search(s));
  824. if not assigned(hp) then
  825. begin
  826. { Not found, insert it as an External }
  827. hp:=new(pasmsymbol,init(s,AB_EXTERNAL,AT_FUNCTION));
  828. asmsymbollist^.insert(hp);
  829. end;
  830. newasmsymbol:=hp;
  831. end;
  832. function newasmsymboltype(const s : string;_bind:TAsmSymBind;_typ:Tasmsymtype) : pasmsymbol;
  833. var
  834. hp : pasmsymbol;
  835. begin
  836. hp:=pasmsymbol(asmsymbollist^.search(s));
  837. if assigned(hp) then
  838. hp^.defbind:=_bind
  839. else
  840. begin
  841. { Not found, insert it as an External }
  842. hp:=new(pasmsymbol,init(s,_bind,_typ));
  843. asmsymbollist^.insert(hp);
  844. end;
  845. newasmsymboltype:=hp;
  846. end;
  847. function getasmsymbol(const s : string) : pasmsymbol;
  848. begin
  849. getasmsymbol:=pasmsymbol(asmsymbollist^.search(s));
  850. end;
  851. { renames an asmsymbol }
  852. function renameasmsymbol(const sold, snew : string):pasmsymbol;
  853. begin
  854. renameasmsymbol:=pasmsymbol(asmsymbollist^.rename(sold,snew));
  855. end;
  856. {*****************************************************************************
  857. Used AsmSymbolList
  858. *****************************************************************************}
  859. procedure CreateUsedAsmSymbolList;
  860. begin
  861. if assigned(usedasmsymbollist) then
  862. internalerror(78455782);
  863. new(usedasmsymbollist,init);
  864. end;
  865. procedure DestroyUsedAsmSymbolList;
  866. begin
  867. dispose(usedasmsymbollist,done);
  868. usedasmsymbollist:=nil;
  869. end;
  870. procedure UsedAsmSymbolListInsert(p:pasmsymbol);
  871. begin
  872. if not p^.inusedlist then
  873. usedasmsymbollist^.insert(p);
  874. p^.inusedlist:=true;
  875. end;
  876. procedure UsedAsmSymbolListReset;
  877. var
  878. hp : pasmsymbol;
  879. begin
  880. hp:=pasmsymbol(usedasmsymbollist^.first);
  881. while assigned(hp) do
  882. begin
  883. with hp^ do
  884. begin
  885. reset;
  886. inusedlist:=false;
  887. end;
  888. hp:=pasmsymbol(hp^.listnext);
  889. end;
  890. end;
  891. procedure UsedAsmSymbolListResetAltSym;
  892. var
  893. hp : pasmsymbol;
  894. begin
  895. hp:=pasmsymbol(usedasmsymbollist^.first);
  896. while assigned(hp) do
  897. begin
  898. with hp^ do
  899. begin
  900. altsymbol:=nil;
  901. inusedlist:=false;
  902. end;
  903. hp:=pasmsymbol(hp^.listnext);
  904. end;
  905. end;
  906. procedure UsedAsmSymbolListCheckUndefined;
  907. var
  908. hp : pasmsymbol;
  909. begin
  910. hp:=pasmsymbol(usedasmsymbollist^.first);
  911. while assigned(hp) do
  912. begin
  913. with hp^ do
  914. begin
  915. if (refs>0) and
  916. (section=Sec_none) and
  917. not(bind in [AB_EXTERNAL,AB_COMMON]) then
  918. Message1(asmw_e_undefined_label,name);
  919. end;
  920. hp:=pasmsymbol(hp^.listnext);
  921. end;
  922. end;
  923. {*****************************************************************************
  924. Label Helpers
  925. *****************************************************************************}
  926. procedure getlabel(var l : pasmlabel);
  927. begin
  928. l:=new(pasmlabel,init);
  929. asmsymbollist^.insert(l);
  930. end;
  931. procedure getdatalabel(var l : pasmlabel);
  932. begin
  933. l:=new(pasmlabel,initdata);
  934. asmsymbollist^.insert(l);
  935. end;
  936. procedure getaddrlabel(var l : pasmlabel);
  937. begin
  938. l:=new(pasmlabel,initaddr);
  939. asmsymbollist^.insert(l);
  940. end;
  941. procedure RegenerateLabel(var l : pasmlabel);
  942. begin
  943. if l^.proclocal then
  944. getlabel(pasmlabel(l^.altsymbol))
  945. else
  946. getdatalabel(pasmlabel(l^.altsymbol));
  947. end;
  948. procedure getlabelnr(var l : longint);
  949. begin
  950. l:=nextlabelnr;
  951. inc(nextlabelnr);
  952. end;
  953. {*****************************************************************************
  954. TAAsmOutput
  955. *****************************************************************************}
  956. function taasmoutput.getlasttaifilepos : pfileposinfo;
  957. begin
  958. if assigned(last) then
  959. getlasttaifilepos:=@tai(last).fileinfo
  960. else
  961. getlasttaifilepos:=nil;
  962. end;
  963. end.
  964. {
  965. $Log$
  966. Revision 1.15 2000-12-25 00:07:25 peter
  967. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  968. tlinkedlist objects)
  969. Revision 1.14 2000/11/29 00:30:30 florian
  970. * unused units removed from uses clause
  971. * some changes for widestrings
  972. Revision 1.13 2000/09/24 15:06:10 peter
  973. * use defines.inc
  974. Revision 1.12 2000/08/27 20:19:38 peter
  975. * store strings with case in ppu, when an internal symbol is created
  976. a '$' is prefixed so it's not automatic uppercased
  977. Revision 1.11 2000/08/27 16:11:48 peter
  978. * moved some util functions from globals,cobjects to cutils
  979. * splitted files into finput,fmodule
  980. Revision 1.10 2000/08/20 17:38:21 peter
  981. * smartlinking fixed for linux (merged)
  982. Revision 1.9 2000/08/16 18:33:53 peter
  983. * splitted namedobjectitem.next into indexnext and listnext so it
  984. can be used in both lists
  985. * don't allow "word = word" type definitions (merged)
  986. Revision 1.8 2000/08/12 19:14:58 peter
  987. * ELF writer works now also with -g
  988. * ELF writer is default again for linux
  989. Revision 1.7 2000/08/12 15:34:21 peter
  990. + usedasmsymbollist to check and reset only the used symbols (merged)
  991. Revision 1.6 2000/08/09 19:49:44 peter
  992. * packenumfixed things so it compiles with 1.0.0 again
  993. Revision 1.5 2000/08/05 13:25:06 peter
  994. * packenum 1 fixes (merged)
  995. Revision 1.4 2000/07/21 15:14:01 jonas
  996. + added is_addr field for labels, if they are only used for getting the address
  997. (e.g. for io checks) and corresponding getaddrlabel() procedure
  998. Revision 1.3 2000/07/13 12:08:24 michael
  999. + patched to 1.1.0 with former 1.09patch from peter
  1000. Revision 1.2 2000/07/13 11:32:28 michael
  1001. + removed logs
  1002. }