aasm.pas 34 KB

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