aasm.pas 35 KB

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