aasm.pas 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206
  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. {$ifdef FPC}
  20. {$ifdef PACKENUMFIXED}
  21. {$PACKENUM 1}
  22. {$endif}
  23. {$endif}
  24. interface
  25. uses
  26. globtype,systems,cobjects,globals;
  27. type
  28. tait = (
  29. ait_none,
  30. ait_direct,
  31. ait_string,
  32. ait_label,
  33. ait_comment,
  34. ait_instruction,
  35. ait_datablock,
  36. ait_symbol,
  37. ait_symbol_end, { needed to calc the size of a symbol }
  38. ait_const_32bit,
  39. ait_const_16bit,
  40. ait_const_8bit,
  41. ait_const_symbol,
  42. ait_real_80bit,
  43. ait_real_64bit,
  44. ait_real_32bit,
  45. ait_comp_64bit,
  46. ait_align,
  47. ait_section,
  48. { the following is only used by the win32 version of the compiler }
  49. { and only the GNU AS Win32 is able to write it }
  50. ait_const_rva,
  51. ait_stabn,
  52. ait_stabs,
  53. ait_force_line,
  54. ait_stab_function_name,
  55. ait_cut, { used to split into tiny assembler files }
  56. ait_regalloc, { for register,temp allocation debugging }
  57. ait_tempalloc,
  58. ait_marker,
  59. { the follow is for the DEC Alpha }
  60. ait_frame,
  61. ait_ent,
  62. {$ifdef m68k}
  63. ait_labeled_instruction,
  64. {$endif m68k}
  65. { never used, makes insertation of new ait_ easier to type }
  66. { lazy guy !!!! ;-) (FK) }
  67. ait_dummy);
  68. tcpuflags = (cf_64bitaddr);
  69. tcpuflagset = set of tcpuflags;
  70. { ait_* types which don't result in executable code or which don't influence }
  71. { the way the program runs/behaves, but which may be encountered by the }
  72. { optimizer (= if it's sometimes added to the exprasm list). Update if you add }
  73. { a new ait type! }
  74. const
  75. SkipInstr = [ait_comment, ait_symbol,ait_force_line,ait_section
  76. {$ifdef GDB}
  77. ,ait_stabs, ait_stabn, ait_stab_function_name
  78. {$endif GDB}
  79. ,ait_regalloc, ait_tempalloc, ait_symbol_end
  80. ];
  81. { asm symbol functions }
  82. type
  83. TAsmsymbind=(AB_NONE,AB_EXTERNAL,AB_COMMON,AB_LOCAL,AB_GLOBAL);
  84. TAsmsymtype=(AT_NONE,AT_FUNCTION,AT_DATA,AT_SECTION);
  85. pasmsymbol = ^tasmsymbol;
  86. tasmsymbol = object(tnamedindexobject)
  87. defbind,
  88. bind : TAsmsymbind;
  89. typ : TAsmsymtype;
  90. { the next fields are filled in the binary writer }
  91. section : tsection;
  92. idx : longint;
  93. address,
  94. size : longint;
  95. { this need to be incremented with every symbol loading into the
  96. paasmoutput, thus in loadsym/loadref/const_symbol (PFV) }
  97. refs : longint;
  98. { alternate symbol which can be used for 'renaming' needed for
  99. inlining }
  100. altsymbol : pasmsymbol;
  101. { is the symbol local for a procedure/function }
  102. proclocal : boolean;
  103. { is the symbol in the used list }
  104. inusedlist : boolean;
  105. constructor init(const s:string;_bind:TAsmsymbind;_typ:Tasmsymtype);
  106. procedure reset;
  107. function is_used:boolean;
  108. procedure setaddress(sec:tsection;offset,len:longint);
  109. procedure GenerateAltSymbol;
  110. end;
  111. pasmlabel = ^tasmlabel;
  112. tasmlabel = object(tasmsymbol)
  113. {$ifdef PACKENUMFIXED}
  114. { this is set by the pai_label.init }
  115. is_set,
  116. { is the label only there for getting an address (e.g. for i/o }
  117. { checks -> true) or is it a jump target (false) }
  118. is_addr : boolean;
  119. {$endif}
  120. labelnr : longint;
  121. {$ifndef PACKENUMFIXED}
  122. is_set,
  123. is_addr : boolean;
  124. {$endif}
  125. constructor init;
  126. constructor initdata;
  127. constructor initaddr;
  128. function name:string;virtual;
  129. end;
  130. { the short name makes typing easier }
  131. pai = ^tai;
  132. tai = object(tlinkedlist_item)
  133. {$ifndef PACKENUMFIXED}
  134. typ : tait;
  135. {$endif}
  136. { pointer to record with optimizer info about this tai object }
  137. optinfo : pointer;
  138. fileinfo : tfileposinfo;
  139. {$ifdef PACKENUMFIXED}
  140. { still 3 bytes left after the next field }
  141. typ : tait;
  142. {$endif}
  143. constructor init;
  144. end;
  145. pai_string = ^tai_string;
  146. tai_string = object(tai)
  147. str : pchar;
  148. { extra len so the string can contain an \0 }
  149. len : longint;
  150. constructor init(const _str : string);
  151. constructor init_pchar(_str : pchar);
  152. constructor init_length_pchar(_str : pchar;length : longint);
  153. destructor done;virtual;
  154. end;
  155. { generates a common label }
  156. pai_symbol = ^tai_symbol;
  157. tai_symbol = object(tai)
  158. {$ifdef PACKENUMFIXED}
  159. is_global : boolean;
  160. {$endif}
  161. sym : pasmsymbol;
  162. size : longint;
  163. {$ifndef PACKENUMFIXED}
  164. is_global : boolean;
  165. {$endif}
  166. constructor init(_sym:PAsmSymbol;siz:longint);
  167. constructor initname(const _name : string;siz:longint);
  168. constructor initname_global(const _name : string;siz:longint);
  169. constructor initdataname(const _name : string;siz:longint);
  170. constructor initdataname_global(const _name : string;siz:longint);
  171. end;
  172. pai_symbol_end = ^tai_symbol_end;
  173. tai_symbol_end = object(tai)
  174. sym : pasmsymbol;
  175. constructor init(_sym:PAsmSymbol);
  176. constructor initname(const _name : string);
  177. end;
  178. pai_label = ^tai_label;
  179. tai_label = object(tai)
  180. {$ifdef PACKENUMFIXED}
  181. is_global : boolean;
  182. {$endif}
  183. l : pasmlabel;
  184. {$ifndef PACKENUMFIXED}
  185. is_global : boolean;
  186. {$endif}
  187. constructor init(_l : pasmlabel);
  188. end;
  189. pai_direct = ^tai_direct;
  190. tai_direct = object(tai)
  191. str : pchar;
  192. constructor init(_str : pchar);
  193. destructor done; virtual;
  194. end;
  195. { to insert a comment into the generated assembler file }
  196. pai_asm_comment = ^tai_asm_comment;
  197. tai_asm_comment = object(tai)
  198. str : pchar;
  199. constructor init(_str : pchar);
  200. destructor done; virtual;
  201. end;
  202. { alignment for operator }
  203. {$ifdef i386}
  204. pai_align_abstract = ^tai_align_abstract;
  205. tai_align_abstract = object(tai)
  206. {$else i386}
  207. pai_align = ^tai_align;
  208. tai_align = object(tai)
  209. {$endif i386}
  210. buf : array[0..63] of char; { buf used for fill }
  211. aligntype : byte; { 1 = no align, 2 = word align, 4 = dword align }
  212. fillsize : byte; { real size to fill }
  213. fillop : byte; { value to fill with - optional }
  214. use_op : boolean;
  215. constructor init(b:byte);
  216. constructor init_op(b: byte; _op: byte);
  217. function getfillbuf:pchar;
  218. end;
  219. { Insert a section/segment directive }
  220. pai_section = ^tai_section;
  221. tai_section = object(tai)
  222. sec : tsection;
  223. constructor init(s : tsection);
  224. end;
  225. { generates an uninitializised data block }
  226. pai_datablock = ^tai_datablock;
  227. tai_datablock = object(tai)
  228. {$ifdef PACKENUMFIXED}
  229. is_global : boolean;
  230. {$endif}
  231. sym : pasmsymbol;
  232. size : longint;
  233. {$ifndef PACKENUMFIXED}
  234. is_global : boolean;
  235. {$endif}
  236. constructor init(const _name : string;_size : longint);
  237. constructor init_global(const _name : string;_size : longint);
  238. end;
  239. { generates a long integer (32 bit) }
  240. pai_const = ^tai_const;
  241. tai_const = object(tai)
  242. value : longint;
  243. constructor init_32bit(_value : longint);
  244. constructor init_16bit(_value : word);
  245. constructor init_8bit(_value : byte);
  246. end;
  247. pai_const_symbol = ^tai_const_symbol;
  248. tai_const_symbol = object(tai)
  249. sym : pasmsymbol;
  250. offset : longint;
  251. constructor init(_sym:PAsmSymbol);
  252. constructor init_offset(_sym:PAsmSymbol;ofs:longint);
  253. constructor init_rva(_sym:PAsmSymbol);
  254. constructor initname(const name:string);
  255. constructor initname_offset(const name:string;ofs:longint);
  256. constructor initname_rva(const name:string);
  257. end;
  258. { generates a single (32 bit real) }
  259. pai_real_32bit = ^tai_real_32bit;
  260. tai_real_32bit = object(tai)
  261. value : ts32real;
  262. constructor init(_value : ts32real);
  263. end;
  264. { generates a double (64 bit real) }
  265. pai_real_64bit = ^tai_real_64bit;
  266. tai_real_64bit = object(tai)
  267. value : ts64real;
  268. constructor init(_value : ts64real);
  269. end;
  270. { generates an extended (80 bit real) }
  271. pai_real_80bit = ^tai_real_80bit;
  272. tai_real_80bit = object(tai)
  273. value : ts80real;
  274. constructor init(_value : ts80real);
  275. end;
  276. { generates an comp (integer over 64 bits) }
  277. pai_comp_64bit = ^tai_comp_64bit;
  278. tai_comp_64bit = object(tai)
  279. value : ts64comp;
  280. constructor init(_value : ts64comp);
  281. end;
  282. { insert a cut to split into several smaller files }
  283. tcutplace=(cut_normal,cut_begin,cut_end);
  284. pai_cut = ^tai_cut;
  285. tai_cut = object(tai)
  286. place : tcutplace;
  287. constructor init;
  288. constructor init_begin;
  289. constructor init_end;
  290. end;
  291. TMarker = (NoPropInfoStart, NoPropInfoEnd,
  292. AsmBlockStart, AsmBlockEnd,
  293. InlineStart,InlineEnd);
  294. pai_marker = ^tai_marker;
  295. tai_marker = object(tai)
  296. Kind: TMarker;
  297. Constructor init(_Kind: TMarker);
  298. end;
  299. paitempalloc = ^taitempalloc;
  300. taitempalloc = object(tai)
  301. {$ifdef PACKENUMFIXED}
  302. allocation : boolean;
  303. {$endif}
  304. temppos,
  305. tempsize : longint;
  306. {$ifndef PACKENUMFIXED}
  307. allocation : boolean;
  308. {$endif}
  309. constructor alloc(pos,size:longint);
  310. constructor dealloc(pos,size:longint);
  311. end;
  312. { for each processor define the best precision }
  313. { bestreal is defined in globals }
  314. {$ifdef i386}
  315. const
  316. ait_bestreal = ait_real_80bit;
  317. type
  318. pai_bestreal = pai_real_80bit;
  319. tai_bestreal = tai_real_80bit;
  320. {$endif i386}
  321. {$ifdef m68k}
  322. const
  323. ait_bestreal = ait_real_32bit;
  324. type
  325. pai_bestreal = pai_real_32bit;
  326. tai_bestreal = tai_real_32bit;
  327. {$endif m68k}
  328. paasmoutput = ^taasmoutput;
  329. taasmoutput = object(tlinkedlist)
  330. function getlasttaifilepos : pfileposinfo;
  331. end;
  332. const
  333. { maximum of aasmoutput lists there will be }
  334. maxoutputlists = 10;
  335. var
  336. { temporary lists }
  337. exprasmlist,
  338. { default lists }
  339. datasegment,codesegment,bsssegment,
  340. debuglist,withdebuglist,consts,
  341. importssection,exportssection,
  342. resourcesection,rttilist,
  343. resourcestringlist : paasmoutput;
  344. { asm symbol list }
  345. asmsymbollist : pdictionary;
  346. usedasmsymbollist : psinglelist;
  347. const
  348. nextaltnr : longint = 1;
  349. nextlabelnr : longint = 1;
  350. countlabelref : boolean = true;
  351. { make l as a new label }
  352. procedure getlabel(var l : pasmlabel);
  353. { make l as a new label and flag is_addr }
  354. procedure getaddrlabel(var l : pasmlabel);
  355. { make l as a new label and flag is_data }
  356. procedure getdatalabel(var l : pasmlabel);
  357. {just get a label number }
  358. procedure getlabelnr(var l : longint);
  359. function newasmsymbol(const s : string) : pasmsymbol;
  360. function newasmsymboltype(const s : string;_bind:TAsmSymBind;_typ:TAsmsymtype) : pasmsymbol;
  361. function getasmsymbol(const s : string) : pasmsymbol;
  362. function renameasmsymbol(const sold, snew : string):pasmsymbol;
  363. procedure InitUsedAsmSymbolList;
  364. procedure DoneUsedAsmSymbolList;
  365. procedure UsedAsmSymbolListInsert(p:pasmsymbol);
  366. procedure UsedAsmSymbolListReset;
  367. procedure UsedAsmSymbolListResetAltSym;
  368. procedure UsedAsmSymbolListCheckUndefined;
  369. implementation
  370. uses
  371. strings,files,verbose;
  372. {****************************************************************************
  373. TAI
  374. ****************************************************************************}
  375. constructor tai.init;
  376. begin
  377. optinfo := nil;
  378. fileinfo:=aktfilepos;
  379. end;
  380. {****************************************************************************
  381. TAI_SECTION
  382. ****************************************************************************}
  383. constructor tai_section.init(s : tsection);
  384. begin
  385. inherited init;
  386. typ:=ait_section;
  387. sec:=s;
  388. end;
  389. {****************************************************************************
  390. TAI_DATABLOCK
  391. ****************************************************************************}
  392. constructor tai_datablock.init(const _name : string;_size : longint);
  393. begin
  394. inherited init;
  395. typ:=ait_datablock;
  396. sym:=newasmsymboltype(_name,AB_LOCAL,AT_DATA);
  397. { keep things aligned }
  398. if _size<=0 then
  399. _size:=4;
  400. size:=_size;
  401. is_global:=false;
  402. end;
  403. constructor tai_datablock.init_global(const _name : string;_size : longint);
  404. begin
  405. inherited init;
  406. typ:=ait_datablock;
  407. sym:=newasmsymboltype(_name,AB_GLOBAL,AT_DATA);
  408. { keep things aligned }
  409. if _size<=0 then
  410. _size:=4;
  411. size:=_size;
  412. is_global:=true;
  413. end;
  414. {****************************************************************************
  415. TAI_SYMBOL
  416. ****************************************************************************}
  417. constructor tai_symbol.init(_sym:PAsmSymbol;siz:longint);
  418. begin
  419. inherited init;
  420. typ:=ait_symbol;
  421. sym:=_sym;
  422. size:=siz;
  423. is_global:=(sym^.bind=AB_GLOBAL);
  424. end;
  425. constructor tai_symbol.initname(const _name : string;siz:longint);
  426. begin
  427. inherited init;
  428. typ:=ait_symbol;
  429. sym:=newasmsymboltype(_name,AB_LOCAL,AT_FUNCTION);
  430. size:=siz;
  431. is_global:=false;
  432. end;
  433. constructor tai_symbol.initname_global(const _name : string;siz:longint);
  434. begin
  435. inherited init;
  436. typ:=ait_symbol;
  437. sym:=newasmsymboltype(_name,AB_GLOBAL,AT_FUNCTION);
  438. size:=siz;
  439. is_global:=true;
  440. end;
  441. constructor tai_symbol.initdataname(const _name : string;siz:longint);
  442. begin
  443. inherited init;
  444. typ:=ait_symbol;
  445. sym:=newasmsymboltype(_name,AB_LOCAL,AT_DATA);
  446. size:=siz;
  447. is_global:=false;
  448. end;
  449. constructor tai_symbol.initdataname_global(const _name : string;siz:longint);
  450. begin
  451. inherited init;
  452. typ:=ait_symbol;
  453. sym:=newasmsymboltype(_name,AB_GLOBAL,AT_DATA);
  454. size:=siz;
  455. is_global:=true;
  456. end;
  457. {****************************************************************************
  458. TAI_SYMBOL
  459. ****************************************************************************}
  460. constructor tai_symbol_end.init(_sym:PAsmSymbol);
  461. begin
  462. inherited init;
  463. typ:=ait_symbol_end;
  464. sym:=_sym;
  465. end;
  466. constructor tai_symbol_end.initname(const _name : string);
  467. begin
  468. inherited init;
  469. typ:=ait_symbol_end;
  470. sym:=newasmsymboltype(_name,AB_GLOBAL,AT_NONE);
  471. end;
  472. {****************************************************************************
  473. TAI_CONST
  474. ****************************************************************************}
  475. constructor tai_const.init_32bit(_value : longint);
  476. begin
  477. inherited init;
  478. typ:=ait_const_32bit;
  479. value:=_value;
  480. end;
  481. constructor tai_const.init_16bit(_value : word);
  482. begin
  483. inherited init;
  484. typ:=ait_const_16bit;
  485. value:=_value;
  486. end;
  487. constructor tai_const.init_8bit(_value : byte);
  488. begin
  489. inherited init;
  490. typ:=ait_const_8bit;
  491. value:=_value;
  492. end;
  493. {****************************************************************************
  494. TAI_CONST_SYMBOL_OFFSET
  495. ****************************************************************************}
  496. constructor tai_const_symbol.init(_sym:PAsmSymbol);
  497. begin
  498. inherited init;
  499. typ:=ait_const_symbol;
  500. sym:=_sym;
  501. offset:=0;
  502. { update sym info }
  503. inc(sym^.refs);
  504. end;
  505. constructor tai_const_symbol.init_offset(_sym:PAsmSymbol;ofs:longint);
  506. begin
  507. inherited init;
  508. typ:=ait_const_symbol;
  509. sym:=_sym;
  510. offset:=ofs;
  511. { update sym info }
  512. inc(sym^.refs);
  513. end;
  514. constructor tai_const_symbol.init_rva(_sym:PAsmSymbol);
  515. begin
  516. inherited init;
  517. typ:=ait_const_rva;
  518. sym:=_sym;
  519. offset:=0;
  520. { update sym info }
  521. inc(sym^.refs);
  522. end;
  523. constructor tai_const_symbol.initname(const name:string);
  524. begin
  525. inherited init;
  526. typ:=ait_const_symbol;
  527. sym:=newasmsymbol(name);
  528. offset:=0;
  529. { update sym info }
  530. inc(sym^.refs);
  531. end;
  532. constructor tai_const_symbol.initname_offset(const name:string;ofs:longint);
  533. begin
  534. inherited init;
  535. typ:=ait_const_symbol;
  536. sym:=newasmsymbol(name);
  537. offset:=ofs;
  538. { update sym info }
  539. inc(sym^.refs);
  540. end;
  541. constructor tai_const_symbol.initname_rva(const name:string);
  542. begin
  543. inherited init;
  544. typ:=ait_const_rva;
  545. sym:=newasmsymbol(name);
  546. offset:=0;
  547. { update sym info }
  548. inc(sym^.refs);
  549. end;
  550. {****************************************************************************
  551. TAI_real_32bit
  552. ****************************************************************************}
  553. constructor tai_real_32bit.init(_value : ts32real);
  554. begin
  555. inherited init;
  556. typ:=ait_real_32bit;
  557. value:=_value;
  558. end;
  559. {****************************************************************************
  560. TAI_real_64bit
  561. ****************************************************************************}
  562. constructor tai_real_64bit.init(_value : ts64real);
  563. begin
  564. inherited init;
  565. typ:=ait_real_64bit;
  566. value:=_value;
  567. end;
  568. {****************************************************************************
  569. TAI_real_80bit
  570. ****************************************************************************}
  571. constructor tai_real_80bit.init(_value : ts80real);
  572. begin
  573. inherited init;
  574. typ:=ait_real_80bit;
  575. value:=_value;
  576. end;
  577. {****************************************************************************
  578. Tai_comp_64bit
  579. ****************************************************************************}
  580. constructor tai_comp_64bit.init(_value : ts64comp);
  581. begin
  582. inherited init;
  583. typ:=ait_comp_64bit;
  584. value:=_value;
  585. end;
  586. {****************************************************************************
  587. TAI_STRING
  588. ****************************************************************************}
  589. constructor tai_string.init(const _str : string);
  590. begin
  591. inherited init;
  592. typ:=ait_string;
  593. getmem(str,length(_str)+1);
  594. strpcopy(str,_str);
  595. len:=length(_str);
  596. end;
  597. constructor tai_string.init_pchar(_str : pchar);
  598. begin
  599. inherited init;
  600. typ:=ait_string;
  601. str:=_str;
  602. len:=strlen(_str);
  603. end;
  604. constructor tai_string.init_length_pchar(_str : pchar;length : longint);
  605. begin
  606. inherited init;
  607. typ:=ait_string;
  608. str:=_str;
  609. len:=length;
  610. end;
  611. destructor tai_string.done;
  612. begin
  613. { you can have #0 inside the strings so }
  614. if str<>nil then
  615. freemem(str,len+1);
  616. inherited done;
  617. end;
  618. {****************************************************************************
  619. TAI_LABEL
  620. ****************************************************************************}
  621. constructor tai_label.init(_l : pasmlabel);
  622. begin
  623. inherited init;
  624. typ:=ait_label;
  625. l:=_l;
  626. l^.is_set:=true;
  627. is_global:=(l^.bind=AB_GLOBAL);
  628. end;
  629. {****************************************************************************
  630. TAI_DIRECT
  631. ****************************************************************************}
  632. constructor tai_direct.init(_str : pchar);
  633. begin
  634. inherited init;
  635. typ:=ait_direct;
  636. str:=_str;
  637. end;
  638. destructor tai_direct.done;
  639. begin
  640. strdispose(str);
  641. inherited done;
  642. end;
  643. {****************************************************************************
  644. TAI_ASM_COMMENT comment to be inserted in the assembler file
  645. ****************************************************************************}
  646. constructor tai_asm_comment.init(_str : pchar);
  647. begin
  648. inherited init;
  649. typ:=ait_comment;
  650. str:=_str;
  651. end;
  652. destructor tai_asm_comment.done;
  653. begin
  654. strdispose(str);
  655. inherited done;
  656. end;
  657. {****************************************************************************
  658. TAI_ALIGN
  659. ****************************************************************************}
  660. {$ifdef i386}
  661. constructor tai_align_abstract.init(b: byte);
  662. {$else i386}
  663. constructor tai_align.init(b: byte);
  664. {$endif i386}
  665. begin
  666. inherited init;
  667. typ:=ait_align;
  668. if b in [1,2,4,8,16,32] then
  669. aligntype := b
  670. else
  671. aligntype := 1;
  672. fillsize:=0;
  673. fillop:=0;
  674. use_op:=false;
  675. end;
  676. {$ifdef i386}
  677. constructor tai_align_abstract.init_op(b: byte; _op: byte);
  678. {$else i386}
  679. constructor tai_align.init_op(b: byte; _op: byte);
  680. {$endif i386}
  681. begin
  682. inherited init;
  683. typ:=ait_align;
  684. if b in [1,2,4,8,16,32] then
  685. aligntype := b
  686. else
  687. aligntype := 1;
  688. fillsize:=0;
  689. fillop:=_op;
  690. use_op:=true;
  691. fillchar(buf,sizeof(buf),_op)
  692. end;
  693. {$ifdef i386}
  694. function tai_align_abstract.getfillbuf:pchar;
  695. {$else i386}
  696. function tai_align.getfillbuf:pchar;
  697. {$endif i386}
  698. begin
  699. getfillbuf:=@buf;
  700. end;
  701. {****************************************************************************
  702. TAI_CUT
  703. ****************************************************************************}
  704. constructor tai_cut.init;
  705. begin
  706. inherited init;
  707. typ:=ait_cut;
  708. place:=cut_normal;
  709. end;
  710. constructor tai_cut.init_begin;
  711. begin
  712. inherited init;
  713. typ:=ait_cut;
  714. place:=cut_begin;
  715. end;
  716. constructor tai_cut.init_end;
  717. begin
  718. inherited init;
  719. typ:=ait_cut;
  720. place:=cut_end;
  721. end;
  722. {****************************************************************************
  723. Tai_Marker
  724. ****************************************************************************}
  725. Constructor Tai_Marker.Init(_Kind: TMarker);
  726. Begin
  727. Inherited Init;
  728. typ := ait_marker;
  729. Kind := _Kind;
  730. End;
  731. {*****************************************************************************
  732. TaiTempAlloc
  733. *****************************************************************************}
  734. constructor taitempalloc.alloc(pos,size:longint);
  735. begin
  736. inherited init;
  737. typ:=ait_tempalloc;
  738. allocation:=true;
  739. temppos:=pos;
  740. tempsize:=size;
  741. end;
  742. constructor taitempalloc.dealloc(pos,size:longint);
  743. begin
  744. inherited init;
  745. typ:=ait_tempalloc;
  746. allocation:=false;
  747. temppos:=pos;
  748. tempsize:=size;
  749. end;
  750. {*****************************************************************************
  751. AsmSymbol
  752. *****************************************************************************}
  753. constructor tasmsymbol.init(const s:string;_bind:TAsmsymbind;_typ:Tasmsymtype);
  754. begin;
  755. inherited initname(s);
  756. reset;
  757. defbind:=_bind;
  758. typ:=_typ;
  759. inusedlist:=false;
  760. { mainly used to remove unused labels from the codesegment }
  761. refs:=0;
  762. end;
  763. procedure tasmsymbol.GenerateAltSymbol;
  764. begin
  765. if not assigned(altsymbol) then
  766. begin
  767. new(altsymbol,init(name+'_'+tostr(nextaltnr),bind,typ));
  768. { also copy the amount of references }
  769. altsymbol^.refs:=refs;
  770. inc(nextaltnr);
  771. end;
  772. end;
  773. procedure tasmsymbol.reset;
  774. begin
  775. { reset section info }
  776. section:=sec_none;
  777. address:=0;
  778. size:=0;
  779. idx:=-1;
  780. bind:=AB_EXTERNAL;
  781. proclocal:=false;
  782. end;
  783. function tasmsymbol.is_used:boolean;
  784. begin
  785. is_used:=(refs>0);
  786. end;
  787. procedure tasmsymbol.setaddress(sec:tsection;offset,len:longint);
  788. begin
  789. section:=sec;
  790. address:=offset;
  791. size:=len;
  792. { when the bind was reset to External, set it back to the default
  793. bind it got when defined }
  794. if (bind=AB_EXTERNAL) and (defbind<>AB_NONE) then
  795. bind:=defbind;
  796. end;
  797. {*****************************************************************************
  798. AsmLabel
  799. *****************************************************************************}
  800. constructor tasmlabel.init;
  801. begin;
  802. labelnr:=nextlabelnr;
  803. inc(nextlabelnr);
  804. inherited init(target_asm.labelprefix+tostr(labelnr),AB_LOCAL,AT_FUNCTION);
  805. proclocal:=true;
  806. is_set:=false;
  807. is_addr := false;
  808. end;
  809. constructor tasmlabel.initdata;
  810. begin;
  811. labelnr:=nextlabelnr;
  812. inc(nextlabelnr);
  813. if (cs_create_smart in aktmoduleswitches) then
  814. inherited init('_$'+current_module^.modulename^+'$_L'+tostr(labelnr),AB_GLOBAL,AT_DATA)
  815. else
  816. inherited init(target_asm.labelprefix+tostr(labelnr),AB_LOCAL,AT_DATA);
  817. is_set:=false;
  818. is_addr := false;
  819. { write it always }
  820. refs:=1;
  821. end;
  822. constructor tasmlabel.initaddr;
  823. begin;
  824. init;
  825. is_addr := true;
  826. end;
  827. function tasmlabel.name:string;
  828. begin
  829. name:=inherited name;
  830. inc(refs);
  831. end;
  832. {*****************************************************************************
  833. AsmSymbolList helpers
  834. *****************************************************************************}
  835. function newasmsymbol(const s : string) : pasmsymbol;
  836. var
  837. hp : pasmsymbol;
  838. begin
  839. hp:=pasmsymbol(asmsymbollist^.search(s));
  840. if not assigned(hp) then
  841. begin
  842. { Not found, insert it as an External }
  843. hp:=new(pasmsymbol,init(s,AB_EXTERNAL,AT_FUNCTION));
  844. asmsymbollist^.insert(hp);
  845. end;
  846. newasmsymbol:=hp;
  847. end;
  848. function newasmsymboltype(const s : string;_bind:TAsmSymBind;_typ:Tasmsymtype) : pasmsymbol;
  849. var
  850. hp : pasmsymbol;
  851. begin
  852. hp:=pasmsymbol(asmsymbollist^.search(s));
  853. if assigned(hp) then
  854. hp^.defbind:=_bind
  855. else
  856. begin
  857. { Not found, insert it as an External }
  858. hp:=new(pasmsymbol,init(s,_bind,_typ));
  859. asmsymbollist^.insert(hp);
  860. end;
  861. newasmsymboltype:=hp;
  862. end;
  863. function getasmsymbol(const s : string) : pasmsymbol;
  864. begin
  865. getasmsymbol:=pasmsymbol(asmsymbollist^.search(s));
  866. end;
  867. { renames an asmsymbol }
  868. function renameasmsymbol(const sold, snew : string):pasmsymbol;
  869. begin
  870. renameasmsymbol:=pasmsymbol(asmsymbollist^.rename(sold,snew));
  871. end;
  872. {*****************************************************************************
  873. Used AsmSymbolList
  874. *****************************************************************************}
  875. procedure InitUsedAsmSymbolList;
  876. begin
  877. if assigned(usedasmsymbollist) then
  878. internalerror(78455782);
  879. new(usedasmsymbollist,init);
  880. usedasmsymbollist^.noclear:=true;
  881. end;
  882. procedure DoneUsedAsmSymbolList;
  883. begin
  884. dispose(usedasmsymbollist,done);
  885. usedasmsymbollist:=nil;
  886. end;
  887. procedure UsedAsmSymbolListInsert(p:pasmsymbol);
  888. begin
  889. if not p^.inusedlist then
  890. usedasmsymbollist^.insert(p);
  891. p^.inusedlist:=true;
  892. end;
  893. procedure UsedAsmSymbolListReset;
  894. var
  895. hp : pasmsymbol;
  896. begin
  897. hp:=pasmsymbol(usedasmsymbollist^.first);
  898. while assigned(hp) do
  899. begin
  900. with hp^ do
  901. begin
  902. reset;
  903. inusedlist:=false;
  904. end;
  905. hp:=pasmsymbol(hp^.next);
  906. end;
  907. end;
  908. procedure UsedAsmSymbolListResetAltSym;
  909. var
  910. hp : pasmsymbol;
  911. begin
  912. hp:=pasmsymbol(usedasmsymbollist^.first);
  913. while assigned(hp) do
  914. begin
  915. with hp^ do
  916. begin
  917. altsymbol:=nil;
  918. inusedlist:=false;
  919. end;
  920. hp:=pasmsymbol(hp^.next);
  921. end;
  922. end;
  923. procedure UsedAsmSymbolListCheckUndefined;
  924. var
  925. hp : pasmsymbol;
  926. begin
  927. hp:=pasmsymbol(usedasmsymbollist^.first);
  928. while assigned(hp) do
  929. begin
  930. with hp^ do
  931. begin
  932. if (refs>0) and
  933. (section=Sec_none) and
  934. not(bind in [AB_EXTERNAL,AB_COMMON]) then
  935. Message1(asmw_e_undefined_label,name);
  936. end;
  937. hp:=pasmsymbol(hp^.next);
  938. end;
  939. end;
  940. {*****************************************************************************
  941. Label Helpers
  942. *****************************************************************************}
  943. procedure getlabel(var l : pasmlabel);
  944. begin
  945. l:=new(pasmlabel,init);
  946. asmsymbollist^.insert(l);
  947. end;
  948. procedure getdatalabel(var l : pasmlabel);
  949. begin
  950. l:=new(pasmlabel,initdata);
  951. asmsymbollist^.insert(l);
  952. end;
  953. procedure getaddrlabel(var l : pasmlabel);
  954. begin
  955. l:=new(pasmlabel,initaddr);
  956. asmsymbollist^.insert(l);
  957. end;
  958. procedure RegenerateLabel(var l : pasmlabel);
  959. begin
  960. if l^.proclocal then
  961. getlabel(pasmlabel(l^.altsymbol))
  962. else
  963. getdatalabel(pasmlabel(l^.altsymbol));
  964. end;
  965. procedure getlabelnr(var l : longint);
  966. begin
  967. l:=nextlabelnr;
  968. inc(nextlabelnr);
  969. end;
  970. {*****************************************************************************
  971. TAAsmOutput
  972. *****************************************************************************}
  973. function taasmoutput.getlasttaifilepos : pfileposinfo;
  974. begin
  975. if assigned(last) then
  976. getlasttaifilepos:=@pai(last)^.fileinfo
  977. else
  978. getlasttaifilepos:=nil;
  979. end;
  980. end.
  981. {
  982. $Log$
  983. Revision 1.8 2000-08-12 19:14:58 peter
  984. * ELF writer works now also with -g
  985. * ELF writer is default again for linux
  986. Revision 1.7 2000/08/12 15:34:21 peter
  987. + usedasmsymbollist to check and reset only the used symbols (merged)
  988. Revision 1.6 2000/08/09 19:49:44 peter
  989. * packenumfixed things so it compiles with 1.0.0 again
  990. Revision 1.5 2000/08/05 13:25:06 peter
  991. * packenum 1 fixes (merged)
  992. Revision 1.4 2000/07/21 15:14:01 jonas
  993. + added is_addr field for labels, if they are only used for getting the address
  994. (e.g. for io checks) and corresponding getaddrlabel() procedure
  995. Revision 1.3 2000/07/13 12:08:24 michael
  996. + patched to 1.1.0 with former 1.09patch from peter
  997. Revision 1.2 2000/07/13 11:32:28 michael
  998. + removed logs
  999. }