aasm.pas 21 KB

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