aasm.pas 31 KB

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