aasm.pas 31 KB

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