aasm.pas 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152
  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.76 2000-02-03 23:01:45 peter
  853. * fixed smartlinking
  854. Revision 1.75 2000/01/28 15:15:31 jonas
  855. * moved skipinstr from daopt386 to aasm
  856. * fixed crashing bug with -dreplacereg in csopt386.pas
  857. Revision 1.74 2000/01/23 16:31:38 peter
  858. * fixed uninited asmsymbol.typ var
  859. Revision 1.73 2000/01/19 22:53:57 florian
  860. * empty records/objects would generate static data of size 0 which is optimized away, tai_datablock
  861. checks now the size and sets it to a value > 0
  862. Revision 1.72 2000/01/13 13:07:05 jonas
  863. * released -dalignreg
  864. * some small fixes to -dnewOptimizations helper procedures
  865. Revision 1.71 2000/01/12 10:38:16 peter
  866. * smartlinking fixes for binary writer
  867. * release alignreg code and moved instruction writing align to cpuasm,
  868. but it doesn't use the specified register yet
  869. Revision 1.70 2000/01/07 01:14:18 peter
  870. * updated copyright to 2000
  871. Revision 1.69 1999/12/22 01:01:46 peter
  872. - removed freelabel()
  873. * added undefined label detection in internal assembler, this prevents
  874. a lot of ld crashes and wrong .o files
  875. * .o files aren't written anymore if errors have occured
  876. * inlining of assembler labels is now correct
  877. Revision 1.68 1999/11/06 14:34:16 peter
  878. * truncated log to 20 revs
  879. Revision 1.67 1999/11/05 16:01:45 jonas
  880. + first implementation of choosing least used register for alignment code
  881. (not yet working, between ifdef alignreg)
  882. Revision 1.66 1999/11/02 15:06:56 peter
  883. * import library fixes for win32
  884. * alignment works again
  885. Revision 1.65 1999/10/27 16:11:27 peter
  886. * insns.dat is used to generate all i386*.inc files
  887. Revision 1.64 1999/09/20 16:38:51 peter
  888. * cs_create_smart instead of cs_smartlink
  889. * -CX is create smartlink
  890. * -CD is create dynamic, but does nothing atm.
  891. Revision 1.63 1999/09/16 23:05:51 florian
  892. * m68k compiler is again compilable (only gas writer, no assembler reader)
  893. Revision 1.62 1999/09/15 20:35:37 florian
  894. * small fix to operator overloading when in MMX mode
  895. + the compiler uses now fldz and fld1 if possible
  896. + some fixes to floating point registers
  897. + some math. functions (arctan, ln, sin, cos, sqrt, sqr, pi) are now inlined
  898. * .... ???
  899. Revision 1.61 1999/09/08 15:01:29 jonas
  900. * some small changes so the noew optimizer is again compilable
  901. Revision 1.60 1999/08/06 15:30:17 florian
  902. + cpu flags added, mainly for the new cg
  903. Revision 1.59 1999/08/05 15:51:01 michael
  904. * Added ait_frame, ait_ent
  905. Revision 1.58 1999/08/04 00:39:56 michael
  906. + Added ait_frame
  907. Revision 1.57 1999/08/02 21:01:41 michael
  908. * Moved toperand type back =(
  909. Revision 1.56 1999/08/02 20:45:47 michael
  910. * Moved toperand type to aasm
  911. Revision 1.55 1999/08/01 23:55:55 michael
  912. * Moved taitempalloc
  913. Revision 1.54 1999/07/29 20:53:55 peter
  914. * write .size also
  915. Revision 1.53 1999/07/22 09:37:28 florian
  916. + resourcestring implemented
  917. + start of longstring support
  918. Revision 1.52 1999/07/03 00:26:01 peter
  919. * ag386bin doesn't destroy the aasmoutput lists anymore
  920. Revision 1.51 1999/06/02 22:43:57 pierre
  921. * previous wrong log corrected
  922. Revision 1.50 1999/06/02 22:25:24 pierre
  923. * changed $ifdef FPC @ into $ifndef TP
  924. Revision 1.49 1999/06/01 14:45:41 peter
  925. * @procvar is now always needed for FPC
  926. Revision 1.48 1999/05/28 09:11:39 peter
  927. * also count ref when asmlabel^.name is used
  928. }