aasm.pas 30 KB

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