aasm.pas 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217
  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) or
  798. target_asm.labelprefix_only_inside_procedure then
  799. inherited init('_$'+current_module.modulename^+'$_L'+tostr(labelnr),AB_GLOBAL,AT_DATA)
  800. else
  801. inherited init(target_asm.labelprefix+tostr(labelnr),AB_LOCAL,AT_DATA);
  802. is_set:=false;
  803. is_addr := false;
  804. { write it always }
  805. refs:=1;
  806. end;
  807. constructor tasmlabel.Initaddr;
  808. begin;
  809. Init;
  810. is_addr := true;
  811. end;
  812. function tasmlabel.name:string;
  813. begin
  814. name:=inherited name;
  815. inc(refs);
  816. end;
  817. {*****************************************************************************
  818. AsmSymbolList helpers
  819. *****************************************************************************}
  820. function newasmsymbol(const s : string) : pasmsymbol;
  821. var
  822. hp : pasmsymbol;
  823. begin
  824. hp:=pasmsymbol(asmsymbollist^.search(s));
  825. if not assigned(hp) then
  826. begin
  827. { Not found, insert it as an External }
  828. hp:=new(pasmsymbol,init(s,AB_EXTERNAL,AT_FUNCTION));
  829. asmsymbollist^.insert(hp);
  830. end;
  831. newasmsymbol:=hp;
  832. end;
  833. function newasmsymboltype(const s : string;_bind:TAsmSymBind;_typ:Tasmsymtype) : pasmsymbol;
  834. var
  835. hp : pasmsymbol;
  836. begin
  837. hp:=pasmsymbol(asmsymbollist^.search(s));
  838. if assigned(hp) then
  839. hp^.defbind:=_bind
  840. else
  841. begin
  842. { Not found, insert it as an External }
  843. hp:=new(pasmsymbol,init(s,_bind,_typ));
  844. asmsymbollist^.insert(hp);
  845. end;
  846. newasmsymboltype:=hp;
  847. end;
  848. function getasmsymbol(const s : string) : pasmsymbol;
  849. begin
  850. getasmsymbol:=pasmsymbol(asmsymbollist^.search(s));
  851. end;
  852. { renames an asmsymbol }
  853. function renameasmsymbol(const sold, snew : string):pasmsymbol;
  854. begin
  855. renameasmsymbol:=pasmsymbol(asmsymbollist^.rename(sold,snew));
  856. end;
  857. {*****************************************************************************
  858. Used AsmSymbolList
  859. *****************************************************************************}
  860. procedure CreateUsedAsmSymbolList;
  861. begin
  862. if assigned(usedasmsymbollist) then
  863. internalerror(78455782);
  864. new(usedasmsymbollist,init);
  865. end;
  866. procedure DestroyUsedAsmSymbolList;
  867. begin
  868. dispose(usedasmsymbollist,done);
  869. usedasmsymbollist:=nil;
  870. end;
  871. procedure UsedAsmSymbolListInsert(p:pasmsymbol);
  872. begin
  873. if not p^.inusedlist then
  874. usedasmsymbollist^.insert(p);
  875. p^.inusedlist:=true;
  876. end;
  877. procedure UsedAsmSymbolListReset;
  878. var
  879. hp : pasmsymbol;
  880. begin
  881. hp:=pasmsymbol(usedasmsymbollist^.first);
  882. while assigned(hp) do
  883. begin
  884. with hp^ do
  885. begin
  886. reset;
  887. inusedlist:=false;
  888. end;
  889. hp:=pasmsymbol(hp^.listnext);
  890. end;
  891. end;
  892. procedure UsedAsmSymbolListResetAltSym;
  893. var
  894. hp : pasmsymbol;
  895. begin
  896. hp:=pasmsymbol(usedasmsymbollist^.first);
  897. while assigned(hp) do
  898. begin
  899. with hp^ do
  900. begin
  901. altsymbol:=nil;
  902. inusedlist:=false;
  903. end;
  904. hp:=pasmsymbol(hp^.listnext);
  905. end;
  906. end;
  907. procedure UsedAsmSymbolListCheckUndefined;
  908. var
  909. hp : pasmsymbol;
  910. begin
  911. hp:=pasmsymbol(usedasmsymbollist^.first);
  912. while assigned(hp) do
  913. begin
  914. with hp^ do
  915. begin
  916. if (refs>0) and
  917. (section=Sec_none) and
  918. not(bind in [AB_EXTERNAL,AB_COMMON]) then
  919. Message1(asmw_e_undefined_label,name);
  920. end;
  921. hp:=pasmsymbol(hp^.listnext);
  922. end;
  923. end;
  924. {*****************************************************************************
  925. Label Helpers
  926. *****************************************************************************}
  927. procedure getlabel(var l : pasmlabel);
  928. begin
  929. l:=new(pasmlabel,init);
  930. asmsymbollist^.insert(l);
  931. end;
  932. procedure getdatalabel(var l : pasmlabel);
  933. begin
  934. l:=new(pasmlabel,initdata);
  935. asmsymbollist^.insert(l);
  936. end;
  937. procedure getaddrlabel(var l : pasmlabel);
  938. begin
  939. l:=new(pasmlabel,initaddr);
  940. asmsymbollist^.insert(l);
  941. end;
  942. procedure RegenerateLabel(var l : pasmlabel);
  943. begin
  944. if l^.proclocal then
  945. getlabel(pasmlabel(l^.altsymbol))
  946. else
  947. getdatalabel(pasmlabel(l^.altsymbol));
  948. end;
  949. procedure getlabelnr(var l : longint);
  950. begin
  951. l:=nextlabelnr;
  952. inc(nextlabelnr);
  953. end;
  954. {*****************************************************************************
  955. TAAsmOutput
  956. *****************************************************************************}
  957. function taasmoutput.getlasttaifilepos : pfileposinfo;
  958. begin
  959. if assigned(last) then
  960. getlasttaifilepos:=@tai(last).fileinfo
  961. else
  962. getlasttaifilepos:=nil;
  963. end;
  964. end.
  965. {
  966. $Log$
  967. Revision 1.16 2001-02-20 21:36:39 peter
  968. * tasm/masm fixes merged
  969. Revision 1.15 2000/12/25 00:07:25 peter
  970. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  971. tlinkedlist objects)
  972. Revision 1.14 2000/11/29 00:30:30 florian
  973. * unused units removed from uses clause
  974. * some changes for widestrings
  975. Revision 1.13 2000/09/24 15:06:10 peter
  976. * use defines.inc
  977. Revision 1.12 2000/08/27 20:19:38 peter
  978. * store strings with case in ppu, when an internal symbol is created
  979. a '$' is prefixed so it's not automatic uppercased
  980. Revision 1.11 2000/08/27 16:11:48 peter
  981. * moved some util functions from globals,cobjects to cutils
  982. * splitted files into finput,fmodule
  983. Revision 1.10 2000/08/20 17:38:21 peter
  984. * smartlinking fixed for linux (merged)
  985. Revision 1.9 2000/08/16 18:33:53 peter
  986. * splitted namedobjectitem.next into indexnext and listnext so it
  987. can be used in both lists
  988. * don't allow "word = word" type definitions (merged)
  989. Revision 1.8 2000/08/12 19:14:58 peter
  990. * ELF writer works now also with -g
  991. * ELF writer is default again for linux
  992. Revision 1.7 2000/08/12 15:34:21 peter
  993. + usedasmsymbollist to check and reset only the used symbols (merged)
  994. Revision 1.6 2000/08/09 19:49:44 peter
  995. * packenumfixed things so it compiles with 1.0.0 again
  996. Revision 1.5 2000/08/05 13:25:06 peter
  997. * packenum 1 fixes (merged)
  998. Revision 1.4 2000/07/21 15:14:01 jonas
  999. + added is_addr field for labels, if they are only used for getting the address
  1000. (e.g. for io checks) and corresponding getaddrlabel() procedure
  1001. Revision 1.3 2000/07/13 12:08:24 michael
  1002. + patched to 1.1.0 with former 1.09patch from peter
  1003. Revision 1.2 2000/07/13 11:32:28 michael
  1004. + removed logs
  1005. }