aasm.pas 34 KB

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