aasm.pas 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  1. {
  2. $Id$
  3. Copyright (c) 1996-98 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. interface
  20. uses
  21. cobjects,files,globals;
  22. {$I version.inc}
  23. type
  24. {$ifdef klaempfl}
  25. {$ifdef ver0_9_2}
  26. extended = double;
  27. {$endif ver0_9_2}
  28. {$endif klaempfl}
  29. tait = (
  30. ait_string,
  31. ait_label,
  32. ait_direct,
  33. ait_labeled_instruction,
  34. ait_comment,
  35. ait_instruction,
  36. ait_datablock,
  37. ait_symbol,
  38. ait_const_32bit,
  39. ait_const_symbol,
  40. ait_const_16bit,
  41. ait_const_8bit,
  42. ait_real_64bit,
  43. ait_real_32bit,
  44. ait_real_extended,
  45. ait_comp,
  46. ait_external,
  47. ait_align,
  48. ait_section,
  49. { the following is only used by the win32 version of the compiler }
  50. { and only the GNU AS Win32 is able to write it }
  51. ait_const_rva,
  52. {$ifdef GDB}
  53. ait_stabn,
  54. ait_stabs,
  55. ait_stab_function_name,
  56. {$endif GDB}
  57. ait_cut, { used to split into tiny assembler files }
  58. {$ifdef REGALLOC}
  59. ait_regalloc,
  60. ait_regdealloc,
  61. {$endif REGALLOC}
  62. { never used, makes insertation of new ait_ easier to type }
  63. ait_dummy);
  64. type
  65. { the short name makes typing easier }
  66. pai = ^tai;
  67. tai = object(tlinkedlist_item)
  68. typ : tait;
  69. line : longint;
  70. infile : pinputfile;
  71. constructor init;
  72. end;
  73. pai_string = ^tai_string;
  74. tai_string = object(tai)
  75. str : pchar;
  76. { extra len so the string can contain an \0 }
  77. len : longint;
  78. constructor init(const _str : string);
  79. constructor init_pchar(_str : pchar);
  80. constructor init_length_pchar(_str : pchar;length : longint);
  81. destructor done;virtual;
  82. end;
  83. pai_symbol = ^tai_symbol;
  84. { generates a common label }
  85. tai_symbol = object(tai)
  86. name : pchar;
  87. is_global : boolean;
  88. constructor init(const _name : string);
  89. constructor init_global(const _name : string);
  90. destructor done;virtual;
  91. end;
  92. { external types defined for TASM }
  93. { EXT_ANY for search purposes }
  94. texternal_typ = (EXT_ANY,EXT_NEAR, EXT_FAR, EXT_PROC, EXT_BYTE,
  95. EXT_WORD, EXT_DWORD, EXT_CODEPTR, EXT_DATAPTR,
  96. EXT_FWORD, EXT_PWORD, EXT_QWORD, EXT_TBYTE, EXT_ABS);
  97. pai_external = ^tai_external;
  98. { generates an symbol which is marked as external }
  99. tai_external = object(tai)
  100. name : pchar;
  101. exttyp : texternal_typ;
  102. constructor init(const _name : string;exttype : texternal_typ);
  103. destructor done; virtual;
  104. end;
  105. { simple temporary label }
  106. pai_label = ^tai_label;
  107. { type for a temporary label }
  108. { test if used for dispose of unnecessary labels }
  109. tlabel = record
  110. nb : longint;
  111. is_used : boolean;
  112. is_set : boolean;
  113. refcount : word;
  114. end;
  115. plabel = ^tlabel;
  116. tai_label = object(tai)
  117. l : plabel;
  118. constructor init(_l : plabel);
  119. destructor done; virtual;
  120. end;
  121. pai_direct = ^tai_direct;
  122. tai_direct = object(tai)
  123. str : pchar;
  124. constructor init(_str : pchar);
  125. destructor done; virtual;
  126. end;
  127. { to insert a comment into the generated assembler file }
  128. pai_asm_comment = ^tai_asm_comment;
  129. tai_asm_comment = object(tai)
  130. str : pchar;
  131. constructor init(_str : pchar);
  132. destructor done; virtual;
  133. end;
  134. { alignment for operator }
  135. pai_align = ^tai_align;
  136. tai_align = object(tai)
  137. aligntype: byte; { 1 = no align, 2 = word align, 4 = dword align }
  138. op: byte; { value to fill with - optional }
  139. constructor init(b:byte);
  140. constructor init_op(b: byte; use_op: byte);
  141. destructor done;virtual;
  142. end;
  143. { Insert a section/segment directive }
  144. tsection=(sec_none,sec_code,sec_data,sec_bss,sec_idata);
  145. pai_section = ^tai_section;
  146. tai_section = object(tai)
  147. sec : tsection;
  148. idataidx : longint;
  149. constructor init(s : tsection);
  150. constructor init_idata(i:longint);
  151. destructor done;virtual;
  152. end;
  153. pai_datablock = ^tai_datablock;
  154. { generates an uninitilizised data block }
  155. tai_datablock = object(tai)
  156. size : longint;
  157. name : pchar;
  158. is_global : boolean;
  159. constructor init(const _name : string;_size : longint);
  160. constructor init_global(const _name : string;_size : longint);
  161. destructor done; virtual;
  162. end;
  163. pai_const = ^tai_const;
  164. { generates a long integer (32 bit) }
  165. tai_const = object(tai)
  166. value : longint;
  167. constructor init_32bit(_value : longint);
  168. constructor init_16bit(_value : word);
  169. constructor init_8bit(_value : byte);
  170. constructor init_symbol(p : pchar);
  171. constructor init_rva(p : pchar);
  172. destructor done;virtual;
  173. end;
  174. pai_double = ^tai_double;
  175. { generates a double (64 bit real) }
  176. tai_double = object(tai)
  177. value : double;
  178. constructor init(_value : double);
  179. end;
  180. pai_comp = ^tai_comp;
  181. { generates an comp (integer over 64 bits) }
  182. tai_comp = object(tai)
  183. value : bestreal;
  184. constructor init(_value : bestreal);
  185. { usefull for 64 bits apps, maybe later }
  186. constructor init_comp(_value : comp);
  187. end;
  188. pai_single = ^tai_single;
  189. { generates a single (32 bit real) }
  190. tai_single = object(tai)
  191. value : single;
  192. constructor init(_value : single);
  193. end;
  194. pai_extended = ^tai_extended;
  195. { generates an extended (80 bit real) }
  196. { for version above v0_9_8 }
  197. { creates a double otherwise }
  198. tai_extended = object(tai)
  199. value : bestreal;
  200. constructor init(_value : bestreal);
  201. end;
  202. pai_cut = ^tai_cut;
  203. tai_cut = object(tai)
  204. constructor init;
  205. end;
  206. { for each processor define the best precision }
  207. { bestreal is defined in globals }
  208. {$ifdef i386}
  209. {$ifdef ver_above0_9_8}
  210. const
  211. ait_bestreal = ait_real_extended;
  212. type
  213. pai_bestreal = pai_extended;
  214. tai_bestreal = tai_extended;
  215. {$else ver_above0_9_8}
  216. const
  217. ait_bestreal = ait_real_64bit;
  218. type
  219. pai_bestreal = pai_double;
  220. tai_bestreal = tai_double;
  221. {$endif ver_above0_9_8}
  222. {$endif i386}
  223. {$ifdef m68k}
  224. const
  225. ait_bestreal = ait_real_32bit;
  226. type
  227. pai_bestreal = pai_single;
  228. tai_bestreal = tai_single;
  229. {$endif m68k}
  230. paasmoutput = ^taasmoutput;
  231. taasmoutput = tlinkedlist;
  232. var
  233. datasegment,codesegment,bsssegment,
  234. internals,externals,debuglist,consts,importssection,
  235. exportssection,resourcesection,rttilist : paasmoutput;
  236. { external symbols without repetition }
  237. function search_assembler_symbol(pl : paasmoutput;const _name : string;exttype : texternal_typ) : pai_external;
  238. procedure concat_external(const _name : string;exttype : texternal_typ);
  239. procedure concat_internal(const _name : string;exttype : texternal_typ);
  240. implementation
  241. uses strings,verbose;
  242. {****************************************************************************
  243. TAI
  244. ****************************************************************************}
  245. constructor tai.init;
  246. begin
  247. {$ifdef GDB}
  248. infile:=pointer(current_module^.current_inputfile);
  249. if assigned(infile) then
  250. line:=current_module^.current_inputfile^.line_no;
  251. {$endif GDB}
  252. end;
  253. {****************************************************************************
  254. TAI_SECTION
  255. ****************************************************************************}
  256. constructor tai_section.init(s : tsection);
  257. begin
  258. inherited init;
  259. typ:=ait_section;
  260. sec:=s;
  261. idataidx:=0;
  262. end;
  263. constructor tai_section.init_idata(i:longint);
  264. begin
  265. inherited init;
  266. typ:=ait_section;
  267. sec:=sec_idata;
  268. idataidx:=i;
  269. end;
  270. destructor tai_section.done;
  271. begin
  272. inherited done;
  273. end;
  274. {****************************************************************************
  275. TAI_DATABLOCK
  276. ****************************************************************************}
  277. constructor tai_datablock.init(const _name : string;_size : longint);
  278. begin
  279. inherited init;
  280. typ:=ait_datablock;
  281. name:=strpnew(_name);
  282. concat_internal(_name,EXT_ANY);
  283. size:=_size;
  284. is_global:=false;
  285. end;
  286. constructor tai_datablock.init_global(const _name : string;_size : longint);
  287. begin
  288. inherited init;
  289. typ:=ait_datablock;
  290. name:=strpnew(_name);
  291. concat_internal(_name,EXT_ANY);
  292. size:=_size;
  293. is_global:=true;
  294. end;
  295. destructor tai_datablock.done;
  296. begin
  297. strdispose(name);
  298. inherited done;
  299. end;
  300. {****************************************************************************
  301. TAI_SYMBOL
  302. ****************************************************************************}
  303. constructor tai_symbol.init(const _name : string);
  304. begin
  305. inherited init;
  306. typ:=ait_symbol;
  307. name:=strpnew(_name);
  308. concat_internal(_name,EXT_ANY);
  309. is_global:=false;
  310. end;
  311. constructor tai_symbol.init_global(const _name : string);
  312. begin
  313. inherited init;
  314. typ:=ait_symbol;
  315. name:=strpnew(_name);
  316. concat_internal(_name,EXT_ANY);
  317. is_global:=true;
  318. end;
  319. destructor tai_symbol.done;
  320. begin
  321. strdispose(name);
  322. inherited done;
  323. end;
  324. {****************************************************************************
  325. TAI_EXTERNAL
  326. ****************************************************************************}
  327. constructor tai_external.init(const _name : string;exttype : texternal_typ);
  328. begin
  329. inherited init;
  330. typ:=ait_external;
  331. exttyp:=exttype;
  332. name:=strpnew(_name);
  333. end;
  334. destructor tai_external.done;
  335. begin
  336. strdispose(name);
  337. inherited done;
  338. end;
  339. function search_assembler_symbol(pl : paasmoutput;const _name : string;exttype : texternal_typ) : pai_external;
  340. var
  341. p : pai;
  342. begin
  343. search_assembler_symbol:=nil;
  344. if pl=nil then
  345. internalerror(2001)
  346. else
  347. begin
  348. p:=pai(pl^.first);
  349. while (p<>nil) and
  350. (p<>pai(pl^.last)) do
  351. { if we get the same name with a different typ }
  352. { there is probably an error }
  353. if (p^.typ=ait_external) and
  354. ((exttype=EXT_ANY) or (pai_external(p)^.exttyp=exttype)) and
  355. (strpas(pai_external(p)^.name)=_name) then
  356. begin
  357. search_assembler_symbol:=pai_external(p);
  358. exit;
  359. end
  360. else
  361. p:=pai(p^.next);
  362. if (p<>nil) and
  363. (p^.typ=ait_external) and
  364. (pai_external(p)^.exttyp=exttype) and
  365. (strpas(pai_external(p)^.name)=_name) then
  366. begin
  367. search_assembler_symbol:=pai_external(p);
  368. exit;
  369. end;
  370. end;
  371. end;
  372. { insert each need external only once }
  373. procedure concat_external(const _name : string;exttype : texternal_typ);
  374. var
  375. p : pai_external;
  376. begin
  377. p:=search_assembler_symbol(externals,_name,exttype);
  378. if p=nil then
  379. externals^.concat(new(pai_external,init(_name,exttype)));
  380. end;
  381. { insert each need external only once }
  382. procedure concat_internal(const _name : string;exttype : texternal_typ);
  383. var
  384. p : pai_external;
  385. begin
  386. p:=search_assembler_symbol(internals,_name,exttype);
  387. if p=nil then
  388. internals^.concat(new(pai_external,init(_name,exttype)));
  389. end;
  390. {****************************************************************************
  391. TAI_CONST
  392. ****************************************************************************}
  393. constructor tai_const.init_32bit(_value : longint);
  394. begin
  395. inherited init;
  396. typ:=ait_const_32bit;
  397. value:=_value;
  398. end;
  399. constructor tai_const.init_16bit(_value : word);
  400. begin
  401. inherited init;
  402. typ:=ait_const_16bit;
  403. value:=_value;
  404. end;
  405. constructor tai_const.init_8bit(_value : byte);
  406. begin
  407. inherited init;
  408. typ:=ait_const_8bit;
  409. value:=_value;
  410. end;
  411. constructor tai_const.init_symbol(p : pchar);
  412. begin
  413. inherited init;
  414. typ:=ait_const_symbol;
  415. value:=longint(p);
  416. end;
  417. constructor tai_const.init_rva(p : pchar);
  418. begin
  419. inherited init;
  420. typ:=ait_const_rva;
  421. value:=longint(p);
  422. end;
  423. destructor tai_const.done;
  424. begin
  425. if typ=ait_const_symbol then
  426. strdispose(pchar(value));
  427. inherited done;
  428. end;
  429. {****************************************************************************
  430. TAI_DOUBLE
  431. ****************************************************************************}
  432. constructor tai_double.init(_value : double);
  433. begin
  434. inherited init;
  435. typ:=ait_real_64bit;
  436. value:=_value;
  437. end;
  438. {****************************************************************************
  439. TAI_SINGLE
  440. ****************************************************************************}
  441. constructor tai_single.init(_value : single);
  442. begin
  443. inherited init;
  444. typ:=ait_real_32bit;
  445. value:=_value;
  446. end;
  447. {****************************************************************************
  448. TAI_EXTENDED
  449. ****************************************************************************}
  450. constructor tai_extended.init(_value : bestreal);
  451. begin
  452. inherited init;
  453. typ:=ait_real_extended;
  454. value:=_value;
  455. end;
  456. {****************************************************************************
  457. TAI_COMP
  458. ****************************************************************************}
  459. constructor tai_comp.init(_value : bestreal);
  460. begin
  461. inherited init;
  462. typ:=ait_comp;
  463. value:=_value;
  464. end;
  465. constructor tai_comp.init_comp(_value : comp);
  466. begin
  467. inherited init;
  468. typ:=ait_comp;
  469. value:=_value;
  470. end;
  471. {****************************************************************************
  472. TAI_STRING
  473. ****************************************************************************}
  474. constructor tai_string.init(const _str : string);
  475. begin
  476. inherited init;
  477. typ:=ait_string;
  478. getmem(str,length(_str)+1);
  479. strpcopy(str,_str);
  480. len:=length(_str);
  481. end;
  482. constructor tai_string.init_pchar(_str : pchar);
  483. begin
  484. inherited init;
  485. typ:=ait_string;
  486. str:=_str;
  487. len:=strlen(_str);
  488. end;
  489. constructor tai_string.init_length_pchar(_str : pchar;length : longint);
  490. begin
  491. inherited init;
  492. typ:=ait_string;
  493. str:=_str;
  494. len:=length;
  495. end;
  496. destructor tai_string.done;
  497. begin
  498. { you can have #0 inside the strings so }
  499. if str<>nil then
  500. freemem(str,len+1);
  501. inherited done;
  502. end;
  503. {****************************************************************************
  504. TAI_LABEL
  505. ****************************************************************************}
  506. constructor tai_label.init(_l : plabel);
  507. begin
  508. inherited init;
  509. typ:=ait_label;
  510. l:=_l;
  511. l^.is_set:=true;
  512. { suggestion of JM:
  513. inc(l^.refcount); }
  514. end;
  515. destructor tai_label.done;
  516. begin
  517. { suggestion of JM:
  518. dec(l^.refcount); }
  519. if (l^.is_used) then
  520. l^.is_set:=false
  521. else dispose(l);
  522. inherited done;
  523. end;
  524. {****************************************************************************
  525. TAI_DIRECT
  526. ****************************************************************************}
  527. constructor tai_direct.init(_str : pchar);
  528. begin
  529. inherited init;
  530. typ:=ait_direct;
  531. str:=_str;
  532. end;
  533. destructor tai_direct.done;
  534. begin
  535. strdispose(str);
  536. inherited done;
  537. end;
  538. {****************************************************************************
  539. TAI_ASM_COMMENT
  540. comment to be inserted in the assembler file
  541. ****************************************************************************}
  542. constructor tai_asm_comment.init(_str : pchar);
  543. begin
  544. inherited init;
  545. typ:=ait_comment;
  546. str:=_str;
  547. end;
  548. destructor tai_asm_comment.done;
  549. begin
  550. strdispose(str);
  551. inherited done;
  552. end;
  553. {****************************************************************************
  554. TAI_ALIGN
  555. ****************************************************************************}
  556. constructor tai_align.init(b: byte);
  557. begin
  558. inherited init;
  559. typ:=ait_align;
  560. if b in [1,2,4,8,16] then
  561. aligntype := b
  562. else
  563. aligntype := 1;
  564. op:=0;
  565. end;
  566. constructor tai_align.init_op(b: byte; use_op: byte);
  567. begin
  568. inherited init;
  569. typ:=ait_align;
  570. if b in [1,2,4,8,16] then
  571. aligntype := b
  572. else
  573. aligntype := 1;
  574. op:=use_op;
  575. end;
  576. destructor tai_align.done;
  577. begin
  578. inherited done;
  579. end;
  580. {****************************************************************************
  581. TAI_CUT
  582. ****************************************************************************}
  583. constructor tai_cut.init;
  584. begin
  585. inherited init;
  586. typ:=ait_cut;
  587. end;
  588. end.
  589. {
  590. $Log$
  591. Revision 1.6 1998-05-06 18:36:53 peter
  592. * tai_section extended with code,data,bss sections and enumerated type
  593. * ident 'compiled by FPC' moved to pmodules
  594. * small fix for smartlink
  595. Revision 1.5 1998/05/01 07:43:52 florian
  596. + basics for rtti implemented
  597. + switch $m (generate rtti for published sections)
  598. Revision 1.4 1998/04/29 10:33:40 pierre
  599. + added some code for ansistring (not complete nor working yet)
  600. * corrected operator overloading
  601. * corrected nasm output
  602. + started inline procedures
  603. + added starstarn : use ** for exponentiation (^ gave problems)
  604. + started UseTokenInfo cond to get accurate positions
  605. Revision 1.3 1998/04/27 23:10:27 peter
  606. + new scanner
  607. * $makelib -> if smartlink
  608. * small filename fixes pmodule.setfilename
  609. * moved import from files.pas -> import.pas
  610. Revision 1.2 1998/04/09 15:46:37 florian
  611. + register allocation tracing stuff added
  612. }