aasm.pas 23 KB

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