aasm.pas 31 KB

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