aasm.pas 34 KB

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