aasm.pas 34 KB

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