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