aasm.pas 33 KB

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