aasm.pas 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217
  1. {
  2. $Id$
  3. Copyright (c) 1996-98 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. interface
  20. uses
  21. globtype,systems,cobjects,files,globals;
  22. type
  23. tait = (
  24. ait_none,
  25. ait_direct,
  26. ait_string,
  27. ait_label,
  28. ait_comment,
  29. ait_instruction,
  30. ait_datablock,
  31. ait_symbol,
  32. ait_symbol_end, { needed to calc the size of a symbol }
  33. ait_const_32bit,
  34. ait_const_16bit,
  35. ait_const_8bit,
  36. ait_const_symbol,
  37. ait_real_80bit,
  38. ait_real_64bit,
  39. ait_real_32bit,
  40. ait_comp_64bit,
  41. ait_align,
  42. ait_section,
  43. { the following is only used by the win32 version of the compiler }
  44. { and only the GNU AS Win32 is able to write it }
  45. ait_const_rva,
  46. ait_stabn,
  47. ait_stabs,
  48. ait_force_line,
  49. ait_stab_function_name,
  50. ait_cut, { used to split into tiny assembler files }
  51. ait_regalloc, { for register,temp allocation debugging }
  52. ait_tempalloc,
  53. ait_marker,
  54. { the follow is for the DEC Alpha }
  55. ait_frame,
  56. ait_ent,
  57. { never used, makes insertation of new ait_ easier to type }
  58. { lazy guy !!!! ;-) (FK) }
  59. ait_dummy);
  60. tcpuflags = (cf_64bitaddr);
  61. tcpuflagset = set of tcpuflags;
  62. {$ifdef newcg}
  63. const
  64. SkipInstr = [ait_comment, ait_align, ait_symbol
  65. {$ifdef GDB}
  66. ,ait_stabs, ait_stabn, ait_stab_function_name
  67. {$endif GDB}
  68. ,ait_regalloc, ait_tempalloc
  69. ];
  70. {$endif newcg}
  71. { asm symbol functions }
  72. type
  73. TAsmsymtype=(AS_EXTERNAL,AS_LOCAL,AS_GLOBAL);
  74. pasmsymbol = ^tasmsymbol;
  75. tasmsymbol = object(tnamedindexobject)
  76. typ : TAsmsymtype;
  77. { this need te incremented with every symbol loading into the
  78. paasmoutput, thus in loadsym/loadref/const_symbol (PFV) }
  79. refs : longint;
  80. { the next fields are filled in the binary writer }
  81. idx : longint;
  82. section : tsection;
  83. address,
  84. size : longint;
  85. constructor init(const s:string;_typ:TAsmsymtype);
  86. procedure reset;
  87. function is_used:boolean;
  88. procedure setaddress(sec:tsection;offset,len:longint);
  89. end;
  90. pasmlabel = ^tasmlabel;
  91. tasmlabel = object(tasmsymbol)
  92. labelnr : longint;
  93. { this is set by the pai_label.init }
  94. is_set : boolean;
  95. constructor init;
  96. constructor initdata;
  97. function name:string;virtual;
  98. end;
  99. pasmsymbollist = ^tasmsymbollist;
  100. tasmsymbollist = object(tdictionary)
  101. end;
  102. { the short name makes typing easier }
  103. pai = ^tai;
  104. tai = object(tlinkedlist_item)
  105. typ : tait;
  106. { pointer to record with optimizer info about this tai object }
  107. optinfo : pointer;
  108. fileinfo : tfileposinfo;
  109. constructor init;
  110. end;
  111. pai_string = ^tai_string;
  112. tai_string = object(tai)
  113. str : pchar;
  114. { extra len so the string can contain an \0 }
  115. len : longint;
  116. constructor init(const _str : string);
  117. constructor init_pchar(_str : pchar);
  118. constructor init_length_pchar(_str : pchar;length : longint);
  119. destructor done;virtual;
  120. end;
  121. { generates a common label }
  122. pai_symbol = ^tai_symbol;
  123. tai_symbol = object(tai)
  124. sym : pasmsymbol;
  125. is_global : boolean;
  126. size : longint;
  127. constructor init(_sym:PAsmSymbol;siz:longint);
  128. constructor initname(const _name : string;siz:longint);
  129. constructor initname_global(const _name : string;siz:longint);
  130. end;
  131. pai_symbol_end = ^tai_symbol_end;
  132. tai_symbol_end = object(tai)
  133. sym : pasmsymbol;
  134. constructor init(_sym:PAsmSymbol);
  135. constructor initname(const _name : string);
  136. end;
  137. pai_label = ^tai_label;
  138. tai_label = object(tai)
  139. l : pasmlabel;
  140. is_global : boolean;
  141. constructor init(_l : pasmlabel);
  142. end;
  143. pai_direct = ^tai_direct;
  144. tai_direct = object(tai)
  145. str : pchar;
  146. constructor init(_str : pchar);
  147. destructor done; virtual;
  148. end;
  149. { to insert a comment into the generated assembler file }
  150. pai_asm_comment = ^tai_asm_comment;
  151. tai_asm_comment = object(tai)
  152. str : pchar;
  153. constructor init(_str : pchar);
  154. destructor done; virtual;
  155. end;
  156. { alignment for operator }
  157. pai_align = ^tai_align;
  158. tai_align = object(tai)
  159. aligntype : byte; { 1 = no align, 2 = word align, 4 = dword align }
  160. fillsize : byte; { real size to fill }
  161. fillop : byte; { value to fill with - optional }
  162. use_op : boolean;
  163. constructor init(b:byte);
  164. constructor init_op(b: byte; _op: byte);
  165. end;
  166. { Insert a section/segment directive }
  167. pai_section = ^tai_section;
  168. tai_section = object(tai)
  169. sec : tsection;
  170. constructor init(s : tsection);
  171. end;
  172. { generates an uninitializised data block }
  173. pai_datablock = ^tai_datablock;
  174. tai_datablock = object(tai)
  175. sym : pasmsymbol;
  176. size : longint;
  177. is_global : boolean;
  178. constructor init(const _name : string;_size : longint);
  179. constructor init_global(const _name : string;_size : longint);
  180. end;
  181. { generates a long integer (32 bit) }
  182. pai_const = ^tai_const;
  183. tai_const = object(tai)
  184. value : longint;
  185. constructor init_32bit(_value : longint);
  186. constructor init_16bit(_value : word);
  187. constructor init_8bit(_value : byte);
  188. end;
  189. pai_const_symbol = ^tai_const_symbol;
  190. tai_const_symbol = object(tai)
  191. sym : pasmsymbol;
  192. offset : longint;
  193. constructor init(_sym:PAsmSymbol);
  194. constructor init_offset(_sym:PAsmSymbol;ofs:longint);
  195. constructor init_rva(_sym:PAsmSymbol);
  196. constructor initname(const name:string);
  197. constructor initname_offset(const name:string;ofs:longint);
  198. constructor initname_rva(const name:string);
  199. end;
  200. { generates a single (32 bit real) }
  201. pai_real_32bit = ^tai_real_32bit;
  202. tai_real_32bit = object(tai)
  203. value : ts32real;
  204. constructor init(_value : ts32real);
  205. end;
  206. { generates a double (64 bit real) }
  207. pai_real_64bit = ^tai_real_64bit;
  208. tai_real_64bit = object(tai)
  209. value : ts64real;
  210. constructor init(_value : ts64real);
  211. end;
  212. { generates an extended (80 bit real) }
  213. pai_real_80bit = ^tai_real_80bit;
  214. tai_real_80bit = object(tai)
  215. value : ts80real;
  216. constructor init(_value : ts80real);
  217. end;
  218. { generates an comp (integer over 64 bits) }
  219. pai_comp_64bit = ^tai_comp_64bit;
  220. tai_comp_64bit = object(tai)
  221. value : ts64comp;
  222. constructor init(_value : ts64comp);
  223. end;
  224. { insert a cut to split into several smaller files }
  225. pai_cut = ^tai_cut;
  226. tai_cut = object(tai)
  227. endname : boolean;
  228. constructor init;
  229. constructor init_end;
  230. end;
  231. TMarker = (NoPropInfoStart, NoPropInfoEnd, AsmBlockStart, AsmBlockEnd);
  232. pai_marker = ^tai_marker;
  233. tai_marker = object(tai)
  234. Kind: TMarker;
  235. Constructor init(_Kind: TMarker);
  236. end;
  237. paitempalloc = ^taitempalloc;
  238. taitempalloc = object(tai)
  239. allocation : boolean;
  240. temppos,
  241. tempsize : longint;
  242. constructor alloc(pos,size:longint);
  243. constructor dealloc(pos,size:longint);
  244. end;
  245. { for each processor define the best precision }
  246. { bestreal is defined in globals }
  247. {$ifdef i386}
  248. const
  249. ait_bestreal = ait_real_80bit;
  250. type
  251. pai_bestreal = pai_real_80bit;
  252. tai_bestreal = tai_real_80bit;
  253. {$endif i386}
  254. {$ifdef m68k}
  255. const
  256. ait_bestreal = ait_real_32bit;
  257. type
  258. pai_bestreal = pai_real_32bit;
  259. tai_bestreal = tai_real_32bit;
  260. {$endif m68k}
  261. paasmoutput = ^taasmoutput;
  262. taasmoutput = object(tlinkedlist)
  263. function getlasttaifilepos : pfileposinfo;
  264. end;
  265. const
  266. { maximum of aasmoutput lists there will be }
  267. maxoutputlists = 10;
  268. var
  269. { temporary lists }
  270. exprasmlist,
  271. { default lists }
  272. datasegment,codesegment,bsssegment,
  273. debuglist,consts,
  274. importssection,exportssection,
  275. resourcesection,rttilist,
  276. resourcestringlist : paasmoutput;
  277. { asm symbol list }
  278. asmsymbollist : pasmsymbollist;
  279. const
  280. nextlabelnr : longint = 1;
  281. countlabelref : boolean = true;
  282. { make l as a new label }
  283. procedure getlabel(var l : pasmlabel);
  284. { make l as a new label and flag is_data }
  285. procedure getdatalabel(var l : pasmlabel);
  286. { free a label }
  287. procedure freelabel(var l : pasmlabel);
  288. {just get a label number }
  289. procedure getlabelnr(var l : longint);
  290. function newasmsymbol(const s : string) : pasmsymbol;
  291. function newasmsymboltyp(const s : string;_typ:TAsmSymType) : pasmsymbol;
  292. function getasmsymbol(const s : string) : pasmsymbol;
  293. function renameasmsymbol(const sold, snew : string):pasmsymbol;
  294. procedure ResetAsmsymbolList;
  295. implementation
  296. uses
  297. strings,verbose;
  298. {****************************************************************************
  299. TAI
  300. ****************************************************************************}
  301. constructor tai.init;
  302. begin
  303. optinfo := nil;
  304. fileinfo:=aktfilepos;
  305. end;
  306. {****************************************************************************
  307. TAI_SECTION
  308. ****************************************************************************}
  309. constructor tai_section.init(s : tsection);
  310. begin
  311. inherited init;
  312. typ:=ait_section;
  313. sec:=s;
  314. end;
  315. {****************************************************************************
  316. TAI_DATABLOCK
  317. ****************************************************************************}
  318. constructor tai_datablock.init(const _name : string;_size : longint);
  319. begin
  320. inherited init;
  321. typ:=ait_datablock;
  322. sym:=newasmsymboltyp(_name,AS_LOCAL);
  323. size:=_size;
  324. is_global:=false;
  325. end;
  326. constructor tai_datablock.init_global(const _name : string;_size : longint);
  327. begin
  328. inherited init;
  329. typ:=ait_datablock;
  330. sym:=newasmsymboltyp(_name,AS_GLOBAL);
  331. size:=_size;
  332. is_global:=true;
  333. end;
  334. {****************************************************************************
  335. TAI_SYMBOL
  336. ****************************************************************************}
  337. constructor tai_symbol.init(_sym:PAsmSymbol;siz:longint);
  338. begin
  339. inherited init;
  340. typ:=ait_symbol;
  341. sym:=_sym;
  342. size:=siz;
  343. is_global:=(sym^.typ=AS_GLOBAL);
  344. end;
  345. constructor tai_symbol.initname(const _name : string;siz:longint);
  346. begin
  347. inherited init;
  348. typ:=ait_symbol;
  349. sym:=newasmsymboltyp(_name,AS_LOCAL);
  350. size:=siz;
  351. is_global:=false;
  352. end;
  353. constructor tai_symbol.initname_global(const _name : string;siz:longint);
  354. begin
  355. inherited init;
  356. typ:=ait_symbol;
  357. sym:=newasmsymboltyp(_name,AS_GLOBAL);
  358. size:=siz;
  359. is_global:=true;
  360. end;
  361. {****************************************************************************
  362. TAI_SYMBOL
  363. ****************************************************************************}
  364. constructor tai_symbol_end.init(_sym:PAsmSymbol);
  365. begin
  366. inherited init;
  367. typ:=ait_symbol_end;
  368. sym:=_sym;
  369. end;
  370. constructor tai_symbol_end.initname(const _name : string);
  371. begin
  372. inherited init;
  373. typ:=ait_symbol_end;
  374. sym:=newasmsymboltyp(_name,AS_GLOBAL);
  375. end;
  376. {****************************************************************************
  377. TAI_CONST
  378. ****************************************************************************}
  379. constructor tai_const.init_32bit(_value : longint);
  380. begin
  381. inherited init;
  382. typ:=ait_const_32bit;
  383. value:=_value;
  384. end;
  385. constructor tai_const.init_16bit(_value : word);
  386. begin
  387. inherited init;
  388. typ:=ait_const_16bit;
  389. value:=_value;
  390. end;
  391. constructor tai_const.init_8bit(_value : byte);
  392. begin
  393. inherited init;
  394. typ:=ait_const_8bit;
  395. value:=_value;
  396. end;
  397. {****************************************************************************
  398. TAI_CONST_SYMBOL_OFFSET
  399. ****************************************************************************}
  400. constructor tai_const_symbol.init(_sym:PAsmSymbol);
  401. begin
  402. inherited init;
  403. typ:=ait_const_symbol;
  404. sym:=_sym;
  405. offset:=0;
  406. { update sym info }
  407. inc(sym^.refs);
  408. end;
  409. constructor tai_const_symbol.init_offset(_sym:PAsmSymbol;ofs:longint);
  410. begin
  411. inherited init;
  412. typ:=ait_const_symbol;
  413. sym:=_sym;
  414. offset:=ofs;
  415. { update sym info }
  416. inc(sym^.refs);
  417. end;
  418. constructor tai_const_symbol.init_rva(_sym:PAsmSymbol);
  419. begin
  420. inherited init;
  421. typ:=ait_const_rva;
  422. sym:=_sym;
  423. offset:=0;
  424. { update sym info }
  425. inc(sym^.refs);
  426. end;
  427. constructor tai_const_symbol.initname(const name:string);
  428. begin
  429. inherited init;
  430. typ:=ait_const_symbol;
  431. sym:=newasmsymbol(name);
  432. offset:=0;
  433. { update sym info }
  434. inc(sym^.refs);
  435. end;
  436. constructor tai_const_symbol.initname_offset(const name:string;ofs:longint);
  437. begin
  438. inherited init;
  439. typ:=ait_const_symbol;
  440. sym:=newasmsymbol(name);
  441. offset:=ofs;
  442. { update sym info }
  443. inc(sym^.refs);
  444. end;
  445. constructor tai_const_symbol.initname_rva(const name:string);
  446. begin
  447. inherited init;
  448. typ:=ait_const_rva;
  449. sym:=newasmsymbol(name);
  450. offset:=0;
  451. { update sym info }
  452. inc(sym^.refs);
  453. end;
  454. {****************************************************************************
  455. TAI_real_32bit
  456. ****************************************************************************}
  457. constructor tai_real_32bit.init(_value : ts32real);
  458. begin
  459. inherited init;
  460. typ:=ait_real_32bit;
  461. value:=_value;
  462. end;
  463. {****************************************************************************
  464. TAI_real_64bit
  465. ****************************************************************************}
  466. constructor tai_real_64bit.init(_value : ts64real);
  467. begin
  468. inherited init;
  469. typ:=ait_real_64bit;
  470. value:=_value;
  471. end;
  472. {****************************************************************************
  473. TAI_real_80bit
  474. ****************************************************************************}
  475. constructor tai_real_80bit.init(_value : ts80real);
  476. begin
  477. inherited init;
  478. typ:=ait_real_80bit;
  479. value:=_value;
  480. end;
  481. {****************************************************************************
  482. Tai_comp_64bit
  483. ****************************************************************************}
  484. constructor tai_comp_64bit.init(_value : ts64comp);
  485. begin
  486. inherited init;
  487. typ:=ait_comp_64bit;
  488. value:=_value;
  489. end;
  490. {****************************************************************************
  491. TAI_STRING
  492. ****************************************************************************}
  493. constructor tai_string.init(const _str : string);
  494. begin
  495. inherited init;
  496. typ:=ait_string;
  497. getmem(str,length(_str)+1);
  498. strpcopy(str,_str);
  499. len:=length(_str);
  500. end;
  501. constructor tai_string.init_pchar(_str : pchar);
  502. begin
  503. inherited init;
  504. typ:=ait_string;
  505. str:=_str;
  506. len:=strlen(_str);
  507. end;
  508. constructor tai_string.init_length_pchar(_str : pchar;length : longint);
  509. begin
  510. inherited init;
  511. typ:=ait_string;
  512. str:=_str;
  513. len:=length;
  514. end;
  515. destructor tai_string.done;
  516. begin
  517. { you can have #0 inside the strings so }
  518. if str<>nil then
  519. freemem(str,len+1);
  520. inherited done;
  521. end;
  522. {****************************************************************************
  523. TAI_LABEL
  524. ****************************************************************************}
  525. constructor tai_label.init(_l : pasmlabel);
  526. begin
  527. inherited init;
  528. typ:=ait_label;
  529. l:=_l;
  530. l^.is_set:=true;
  531. is_global:=(l^.typ=AS_GLOBAL);
  532. end;
  533. {****************************************************************************
  534. TAI_DIRECT
  535. ****************************************************************************}
  536. constructor tai_direct.init(_str : pchar);
  537. begin
  538. inherited init;
  539. typ:=ait_direct;
  540. str:=_str;
  541. end;
  542. destructor tai_direct.done;
  543. begin
  544. strdispose(str);
  545. inherited done;
  546. end;
  547. {****************************************************************************
  548. TAI_ASM_COMMENT comment to be inserted in the assembler file
  549. ****************************************************************************}
  550. constructor tai_asm_comment.init(_str : pchar);
  551. begin
  552. inherited init;
  553. typ:=ait_comment;
  554. str:=_str;
  555. end;
  556. destructor tai_asm_comment.done;
  557. begin
  558. strdispose(str);
  559. inherited done;
  560. end;
  561. {****************************************************************************
  562. TAI_ALIGN
  563. ****************************************************************************}
  564. constructor tai_align.init(b: byte);
  565. begin
  566. inherited init;
  567. typ:=ait_align;
  568. if b in [1,2,4,8,16] then
  569. aligntype := b
  570. else
  571. aligntype := 1;
  572. fillsize:=0;
  573. fillop:=0;
  574. use_op:=false;
  575. end;
  576. constructor tai_align.init_op(b: byte; _op: byte);
  577. begin
  578. inherited init;
  579. typ:=ait_align;
  580. if b in [1,2,4,8,16] then
  581. aligntype := b
  582. else
  583. aligntype := 1;
  584. fillsize:=0;
  585. fillop:=_op;
  586. use_op:=true;
  587. end;
  588. {****************************************************************************
  589. TAI_CUT
  590. ****************************************************************************}
  591. constructor tai_cut.init;
  592. begin
  593. inherited init;
  594. typ:=ait_cut;
  595. endname:=false;
  596. end;
  597. constructor tai_cut.init_end;
  598. begin
  599. inherited init;
  600. typ:=ait_cut;
  601. endname:=true;
  602. end;
  603. {****************************************************************************
  604. Tai_Marker
  605. ****************************************************************************}
  606. Constructor Tai_Marker.Init(_Kind: TMarker);
  607. Begin
  608. Inherited Init;
  609. typ := ait_marker;
  610. Kind := _Kind;
  611. End;
  612. {*****************************************************************************
  613. TaiTempAlloc
  614. *****************************************************************************}
  615. constructor taitempalloc.alloc(pos,size:longint);
  616. begin
  617. inherited init;
  618. typ:=ait_tempalloc;
  619. allocation:=true;
  620. temppos:=pos;
  621. tempsize:=size;
  622. end;
  623. constructor taitempalloc.dealloc(pos,size:longint);
  624. begin
  625. inherited init;
  626. typ:=ait_tempalloc;
  627. allocation:=false;
  628. temppos:=pos;
  629. tempsize:=size;
  630. end;
  631. {*****************************************************************************
  632. AsmSymbol
  633. *****************************************************************************}
  634. constructor tasmsymbol.init(const s:string;_typ:TAsmsymtype);
  635. begin;
  636. inherited initname(s);
  637. reset;
  638. typ:=_typ;
  639. end;
  640. procedure tasmsymbol.reset;
  641. begin
  642. section:=sec_none;
  643. address:=0;
  644. size:=0;
  645. idx:=-1;
  646. typ:=AS_EXTERNAL;
  647. { mainly used to remove unused labels from the codesegment }
  648. refs:=0;
  649. end;
  650. function tasmsymbol.is_used:boolean;
  651. begin
  652. is_used:=(refs>0);
  653. end;
  654. procedure tasmsymbol.setaddress(sec:tsection;offset,len:longint);
  655. begin
  656. section:=sec;
  657. address:=offset;
  658. size:=len;
  659. end;
  660. {*****************************************************************************
  661. AsmLabel
  662. *****************************************************************************}
  663. constructor tasmlabel.init;
  664. begin;
  665. labelnr:=nextlabelnr;
  666. inc(nextlabelnr);
  667. inherited init(target_asm.labelprefix+tostr(labelnr),AS_LOCAL);
  668. is_set:=false;
  669. end;
  670. constructor tasmlabel.initdata;
  671. begin;
  672. labelnr:=nextlabelnr;
  673. inc(nextlabelnr);
  674. if (cs_smartlink in aktmoduleswitches) then
  675. inherited init('_$'+current_module^.modulename^+'$_L'+tostr(labelnr),AS_GLOBAL)
  676. else
  677. inherited init(target_asm.labelprefix+tostr(labelnr),AS_LOCAL);
  678. is_set:=false;
  679. { write it always }
  680. refs:=1;
  681. end;
  682. function tasmlabel.name:string;
  683. begin
  684. name:=inherited name;
  685. inc(refs);
  686. end;
  687. {*****************************************************************************
  688. AsmSymbolList helpers
  689. *****************************************************************************}
  690. function newasmsymbol(const s : string) : pasmsymbol;
  691. var
  692. hp : pasmsymbol;
  693. begin
  694. hp:=pasmsymbol(asmsymbollist^.search(s));
  695. if assigned(hp) then
  696. begin
  697. newasmsymbol:=hp;
  698. exit;
  699. end;
  700. { Not found, insert it as an External }
  701. hp:=new(pasmsymbol,init(s,AS_EXTERNAL));
  702. asmsymbollist^.insert(hp);
  703. newasmsymbol:=hp;
  704. end;
  705. function newasmsymboltyp(const s : string;_typ:TAsmSymType) : pasmsymbol;
  706. var
  707. hp : pasmsymbol;
  708. begin
  709. hp:=pasmsymbol(asmsymbollist^.search(s));
  710. if assigned(hp) then
  711. begin
  712. hp^.typ:=_typ;
  713. newasmsymboltyp:=hp;
  714. exit;
  715. end;
  716. { Not found, insert it as an External }
  717. hp:=new(pasmsymbol,init(s,_typ));
  718. asmsymbollist^.insert(hp);
  719. newasmsymboltyp:=hp;
  720. end;
  721. function getasmsymbol(const s : string) : pasmsymbol;
  722. begin
  723. getasmsymbol:=pasmsymbol(asmsymbollist^.search(s));
  724. end;
  725. { renames an asmsymbol }
  726. function renameasmsymbol(const sold, snew : string):pasmsymbol;
  727. {$ifdef nodictonaryrename}
  728. var
  729. hpold,hpnew : pasmsymbol;
  730. begin
  731. hpnew:=pasmsymbol(asmsymbollist^.search(snew));
  732. if assigned(hpnew) then
  733. internalerror(405405);
  734. hpold:=pasmsymbol(asmsymbollist^.search(sold));
  735. if not assigned(hpold) then
  736. internalerror(405406);
  737. hpnew:=new(pasmsymbol,init(sold));
  738. { replace the old one }
  739. { WARNING this heavily depends on the
  740. feature that tdictionnary.insert does not delete
  741. the tree element that it replaces !! }
  742. asmsymbollist^.replace_existing:=true;
  743. asmsymbollist^.insert(hpnew);
  744. asmsymbollist^.replace_existing:=false;
  745. { restore the tree }
  746. hpnew^.left:=hpold^.left;
  747. hpnew^.right:=hpold^.right;
  748. stringdispose(hpold^._name);
  749. hpold^._name:=stringdup(snew);
  750. hpold^.speedvalue:=getspeedvalue(snew);
  751. { now reinsert it at right location !! }
  752. asmsymbollist^.insert(hpold);
  753. renameasmsymbol:=hpold;
  754. end;
  755. {$else}
  756. begin
  757. renameasmsymbol:=pasmsymbol(asmsymbollist^.rename(sold,snew));
  758. end;
  759. {$endif}
  760. procedure ResetAsmSym(p:Pnamedindexobject);{$ifndef FPC}far;{$endif}
  761. begin
  762. pasmsymbol(p)^.reset;
  763. end;
  764. procedure ResetAsmsymbolList;
  765. begin
  766. asmsymbollist^.foreach({$ifndef TP}@{$endif}resetasmsym);
  767. end;
  768. {*****************************************************************************
  769. Label Helpers
  770. *****************************************************************************}
  771. procedure getlabel(var l : pasmlabel);
  772. begin
  773. l:=new(pasmlabel,init);
  774. asmsymbollist^.insert(l);
  775. end;
  776. procedure getdatalabel(var l : pasmlabel);
  777. begin
  778. l:=new(pasmlabel,initdata);
  779. asmsymbollist^.insert(l);
  780. end;
  781. procedure freelabel(var l : pasmlabel);
  782. begin
  783. { nothing to do, the dispose of the asmsymbollist will do it }
  784. l:=nil;
  785. end;
  786. procedure getlabelnr(var l : longint);
  787. begin
  788. l:=nextlabelnr;
  789. inc(nextlabelnr);
  790. end;
  791. {*****************************************************************************
  792. TAAsmOutput
  793. *****************************************************************************}
  794. function taasmoutput.getlasttaifilepos : pfileposinfo;
  795. begin
  796. if assigned(last) then
  797. getlasttaifilepos:=@pai(last)^.fileinfo
  798. else
  799. getlasttaifilepos:=nil;
  800. end;
  801. end.
  802. {
  803. $Log$
  804. Revision 1.61 1999-09-08 15:01:29 jonas
  805. * some small changes so the noew optimizer is again compilable
  806. Revision 1.60 1999/08/06 15:30:17 florian
  807. + cpu flags added, mainly for the new cg
  808. Revision 1.59 1999/08/05 15:51:01 michael
  809. * Added ait_frame, ait_ent
  810. Revision 1.58 1999/08/04 00:39:56 michael
  811. + Added ait_frame
  812. Revision 1.57 1999/08/02 21:01:41 michael
  813. * Moved toperand type back =(
  814. Revision 1.56 1999/08/02 20:45:47 michael
  815. * Moved toperand type to aasm
  816. Revision 1.55 1999/08/01 23:55:55 michael
  817. * Moved taitempalloc
  818. Revision 1.54 1999/07/29 20:53:55 peter
  819. * write .size also
  820. Revision 1.53 1999/07/22 09:37:28 florian
  821. + resourcestring implemented
  822. + start of longstring support
  823. Revision 1.52 1999/07/03 00:26:01 peter
  824. * ag386bin doesn't destroy the aasmoutput lists anymore
  825. Revision 1.51 1999/06/02 22:43:57 pierre
  826. * previous wrong log corrected
  827. Revision 1.50 1999/06/02 22:25:24 pierre
  828. * changed $ifdef FPC @ into $ifndef TP
  829. Revision 1.49 1999/06/01 14:45:41 peter
  830. * @procvar is now always needed for FPC
  831. Revision 1.48 1999/05/28 09:11:39 peter
  832. * also count ref when asmlabel^.name is used
  833. Revision 1.47 1999/05/27 19:43:55 peter
  834. * removed oldasm
  835. * plabel -> pasmlabel
  836. * -a switches to source writing automaticly
  837. * assembler readers OOPed
  838. * asmsymbol automaticly external
  839. * jumptables and other label fixes for asm readers
  840. Revision 1.46 1999/05/21 13:54:38 peter
  841. * NEWLAB for label as symbol
  842. Revision 1.45 1999/05/20 22:18:51 pierre
  843. * fix from Peter for double bug reported 20/05/1999
  844. Revision 1.44 1999/05/12 00:19:34 peter
  845. * removed R_DEFAULT_SEG
  846. * uniform float names
  847. Revision 1.43 1999/05/08 20:38:02 jonas
  848. * seperate OPTimizer INFO pointer field in tai object
  849. Revision 1.42 1999/05/06 09:05:05 peter
  850. * generic write_float and str_float
  851. * fixed constant float conversions
  852. Revision 1.41 1999/05/02 22:41:46 peter
  853. * moved section names to systems
  854. * fixed nasm,intel writer
  855. Revision 1.40 1999/04/21 09:43:28 peter
  856. * storenumber works
  857. * fixed some typos in double_checksum
  858. + incompatible types type1 and type2 message (with storenumber)
  859. Revision 1.39 1999/04/16 11:49:36 peter
  860. + tempalloc
  861. + -at to show temp alloc info in .s file
  862. Revision 1.38 1999/04/14 09:14:44 peter
  863. * first things to store the symbol/def number in the ppu
  864. Revision 1.37 1999/03/10 13:25:42 pierre
  865. section order changed to get closer output from coff writer
  866. Revision 1.36 1999/03/08 14:51:04 peter
  867. + smartlinking for ag386bin
  868. Revision 1.35 1999/03/05 13:09:48 peter
  869. * first things for tai_cut support for ag386bin
  870. Revision 1.34 1999/03/03 11:59:27 pierre
  871. + getasmsymbol to search for existing assembler symbol only
  872. Revision 1.33 1999/03/02 02:56:08 peter
  873. + stabs support for binary writers
  874. * more fixes and missing updates from the previous commit :(
  875. Revision 1.32 1999/03/01 13:31:59 pierre
  876. * external used before implemented problem fixed
  877. Revision 1.31 1999/02/25 21:02:16 peter
  878. * ag386bin updates
  879. + coff writer
  880. Revision 1.30 1999/02/17 10:16:24 peter
  881. * small fixes for the binary writer
  882. Revision 1.29 1998/12/29 18:48:24 jonas
  883. + optimize pascal code surrounding assembler blocks
  884. Revision 1.28 1998/12/16 00:27:16 peter
  885. * removed some obsolete version checks
  886. Revision 1.27 1998/12/11 00:02:37 peter
  887. + globtype,tokens,version unit splitted from globals
  888. Revision 1.26 1998/12/01 23:36:31 pierre
  889. * zero padded alignment was buggy
  890. Revision 1.25 1998/11/30 09:42:52 pierre
  891. * some range check bugs fixed (still not working !)
  892. + added DLL writing support for win32 (also accepts variables)
  893. + TempAnsi for code that could be used for Temporary ansi strings
  894. handling
  895. Revision 1.24 1998/11/12 11:19:30 pierre
  896. * fix for first line of function break
  897. Revision 1.23 1998/10/14 15:56:37 pierre
  898. * all references to comp suppressed for m68k
  899. Revision 1.22 1998/10/12 12:20:38 pierre
  900. + added tai_const_symbol_offset
  901. for r : pointer = @var.field;
  902. * better message for different arg names on implementation
  903. of function
  904. Revision 1.21 1998/10/08 17:17:07 pierre
  905. * current_module old scanner tagged as invalid if unit is recompiled
  906. + added ppheap for better info on tracegetmem of heaptrc
  907. (adds line column and file index)
  908. * several memory leaks removed ith help of heaptrc !!
  909. Revision 1.20 1998/10/06 17:16:31 pierre
  910. * some memory leaks fixed (thanks to Peter for heaptrc !)
  911. Revision 1.19 1998/10/01 20:19:11 jonas
  912. + ait_marker support
  913. Revision 1.18 1998/09/20 17:11:25 jonas
  914. * released REGALLOC
  915. Revision 1.17 1998/09/07 18:33:31 peter
  916. + smartlinking for win95 imports
  917. Revision 1.16 1998/09/03 17:08:37 pierre
  918. * better lines for stabs
  919. (no scroll back to if before else part
  920. no return to case line at jump outside case)
  921. + source lines also if not in order
  922. Revision 1.15 1998/08/11 15:31:36 peter
  923. * write extended to ppu file
  924. * new version 0.99.7
  925. Revision 1.14 1998/08/10 23:56:03 peter
  926. * fixed extended writing
  927. Revision 1.13 1998/08/10 14:49:33 peter
  928. + localswitches, moduleswitches, globalswitches splitting
  929. Revision 1.12 1998/07/14 14:46:36 peter
  930. * released NEWINPUT
  931. Revision 1.11 1998/07/07 11:19:50 peter
  932. + NEWINPUT for a better inputfile and scanner object
  933. Revision 1.10 1998/06/08 22:59:41 peter
  934. * smartlinking works for win32
  935. * some defines to exclude some compiler parts
  936. Revision 1.9 1998/06/04 23:51:26 peter
  937. * m68k compiles
  938. + .def file creation moved to gendef.pas so it could also be used
  939. for win32
  940. Revision 1.8 1998/05/23 01:20:53 peter
  941. + aktasmmode, aktoptprocessor, aktoutputformat
  942. + smartlink per module $SMARTLINK-/+ (like MMX) and moved to aktswitches
  943. + $LIBNAME to set the library name where the unit will be put in
  944. * splitted cgi386 a bit (codeseg to large for bp7)
  945. * nasm, tasm works again. nasm moved to ag386nsm.pas
  946. Revision 1.7 1998/05/07 00:16:59 peter
  947. * smartlinking for sets
  948. + consts labels are now concated/generated in hcodegen
  949. * moved some cpu code to cga and some none cpu depended code from cga
  950. to tree and hcodegen and cleanup of hcodegen
  951. * assembling .. output reduced for smartlinking ;)
  952. Revision 1.6 1998/05/06 18:36:53 peter
  953. * tai_section extended with code,data,bss sections and enumerated type
  954. * ident 'compiled by FPC' moved to pmodules
  955. * small fix for smartlink
  956. Revision 1.5 1998/05/01 07:43:52 florian
  957. + basics for rtti implemented
  958. + switch $m (generate rtti for published sections)
  959. Revision 1.4 1998/04/29 10:33:40 pierre
  960. + added some code for ansistring (not complete nor working yet)
  961. * corrected operator overloading
  962. * corrected nasm output
  963. + started inline procedures
  964. + added starstarn : use ** for exponentiation (^ gave problems)
  965. + started UseTokenInfo cond to get accurate positions
  966. Revision 1.3 1998/04/27 23:10:27 peter
  967. + new scanner
  968. * $makelib -> if smartlink
  969. * small filename fixes pmodule.setfilename
  970. * moved import from files.pas -> import.pas
  971. Revision 1.2 1998/04/09 15:46:37 florian
  972. + register allocation tracing stuff added
  973. }