aasm.pas 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827
  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. { the following is only used by the win32 version of the compiler }
  49. { and only the GNU AS Win32 is able to write it }
  50. ait_section,
  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. pai_section = ^tai_section;
  144. tai_section = object(tai)
  145. name : pstring;
  146. constructor init(const s : string);
  147. destructor done;virtual;
  148. end;
  149. pai_datablock = ^tai_datablock;
  150. { generates an uninitilizised data block }
  151. tai_datablock = object(tai)
  152. size : longint;
  153. name : pchar;
  154. is_global : boolean;
  155. constructor init(const _name : string;_size : longint);
  156. constructor init_global(const _name : string;_size : longint);
  157. destructor done; virtual;
  158. end;
  159. pai_const = ^tai_const;
  160. { generates a long integer (32 bit) }
  161. tai_const = object(tai)
  162. value : longint;
  163. constructor init_32bit(_value : longint);
  164. constructor init_16bit(_value : word);
  165. constructor init_8bit(_value : byte);
  166. constructor init_symbol(p : pchar);
  167. constructor init_rva(p : pchar);
  168. destructor done;virtual;
  169. end;
  170. pai_double = ^tai_double;
  171. { generates a double (64 bit real) }
  172. tai_double = object(tai)
  173. value : double;
  174. constructor init(_value : double);
  175. end;
  176. pai_comp = ^tai_comp;
  177. { generates an comp (integer over 64 bits) }
  178. tai_comp = object(tai)
  179. value : bestreal;
  180. constructor init(_value : bestreal);
  181. { usefull for 64 bits apps, maybe later }
  182. constructor init_comp(_value : comp);
  183. end;
  184. pai_single = ^tai_single;
  185. { generates a single (32 bit real) }
  186. tai_single = object(tai)
  187. value : single;
  188. constructor init(_value : single);
  189. end;
  190. pai_extended = ^tai_extended;
  191. { generates an extended (80 bit real) }
  192. { for version above v0_9_8 }
  193. { creates a double otherwise }
  194. tai_extended = object(tai)
  195. value : bestreal;
  196. constructor init(_value : bestreal);
  197. end;
  198. pai_cut = ^tai_cut;
  199. tai_cut = object(tai)
  200. constructor init;
  201. end;
  202. { for each processor define the best precision }
  203. { bestreal is defined in globals }
  204. {$ifdef i386}
  205. {$ifdef ver_above0_9_8}
  206. const
  207. ait_bestreal = ait_real_extended;
  208. type
  209. pai_bestreal = pai_extended;
  210. tai_bestreal = tai_extended;
  211. {$else ver_above0_9_8}
  212. const
  213. ait_bestreal = ait_real_64bit;
  214. type
  215. pai_bestreal = pai_double;
  216. tai_bestreal = tai_double;
  217. {$endif ver_above0_9_8}
  218. {$endif i386}
  219. {$ifdef m68k}
  220. const
  221. ait_bestreal = ait_real_32bit;
  222. type
  223. pai_bestreal = pai_single;
  224. tai_bestreal = tai_single;
  225. {$endif m68k}
  226. paasmoutput = ^taasmoutput;
  227. taasmoutput = tlinkedlist;
  228. var
  229. datasegment,codesegment,bsssegment,
  230. internals,externals,debuglist,consts,importssection,
  231. exportssection,resourcesection : paasmoutput;
  232. { external symbols without repetition }
  233. function search_assembler_symbol(pl : paasmoutput;const _name : string;exttype : texternal_typ) : pai_external;
  234. procedure concat_external(const _name : string;exttype : texternal_typ);
  235. procedure concat_internal(const _name : string;exttype : texternal_typ);
  236. implementation
  237. uses strings,verbose;
  238. {****************************************************************************
  239. TAI
  240. ****************************************************************************}
  241. constructor tai.init;
  242. begin
  243. {$ifdef GDB}
  244. infile:=pointer(current_module^.current_inputfile);
  245. if assigned(infile) then
  246. line:=current_module^.current_inputfile^.line_no;
  247. {$endif GDB}
  248. end;
  249. {****************************************************************************
  250. TAI_SECTION
  251. ****************************************************************************}
  252. constructor tai_section.init(const s : string);
  253. begin
  254. inherited init;
  255. typ:=ait_section;
  256. name:=stringdup(s);
  257. end;
  258. destructor tai_section.done;
  259. begin
  260. stringdispose(name);
  261. inherited done;
  262. end;
  263. {****************************************************************************
  264. TAI_DATABLOCK
  265. ****************************************************************************}
  266. constructor tai_datablock.init(const _name : string;_size : longint);
  267. begin
  268. inherited init;
  269. typ:=ait_datablock;
  270. name:=strpnew(_name);
  271. concat_internal(_name,EXT_ANY);
  272. size:=_size;
  273. is_global:=false;
  274. end;
  275. constructor tai_datablock.init_global(const _name : string;_size : longint);
  276. begin
  277. inherited init;
  278. typ:=ait_datablock;
  279. name:=strpnew(_name);
  280. concat_internal(_name,EXT_ANY);
  281. size:=_size;
  282. is_global:=true;
  283. end;
  284. destructor tai_datablock.done;
  285. begin
  286. strdispose(name);
  287. inherited done;
  288. end;
  289. {****************************************************************************
  290. TAI_SYMBOL
  291. ****************************************************************************}
  292. constructor tai_symbol.init(const _name : string);
  293. begin
  294. inherited init;
  295. typ:=ait_symbol;
  296. name:=strpnew(_name);
  297. concat_internal(_name,EXT_ANY);
  298. is_global:=false;
  299. end;
  300. constructor tai_symbol.init_global(const _name : string);
  301. begin
  302. inherited init;
  303. typ:=ait_symbol;
  304. name:=strpnew(_name);
  305. concat_internal(_name,EXT_ANY);
  306. is_global:=true;
  307. end;
  308. destructor tai_symbol.done;
  309. begin
  310. strdispose(name);
  311. inherited done;
  312. end;
  313. {****************************************************************************
  314. TAI_EXTERNAL
  315. ****************************************************************************}
  316. constructor tai_external.init(const _name : string;exttype : texternal_typ);
  317. begin
  318. inherited init;
  319. typ:=ait_external;
  320. exttyp:=exttype;
  321. name:=strpnew(_name);
  322. end;
  323. destructor tai_external.done;
  324. begin
  325. strdispose(name);
  326. inherited done;
  327. end;
  328. function search_assembler_symbol(pl : paasmoutput;const _name : string;exttype : texternal_typ) : pai_external;
  329. var
  330. p : pai;
  331. begin
  332. search_assembler_symbol:=nil;
  333. if pl=nil then
  334. internalerror(2001)
  335. else
  336. begin
  337. p:=pai(pl^.first);
  338. while (p<>nil) and
  339. (p<>pai(pl^.last)) do
  340. { if we get the same name with a different typ }
  341. { there is probably an error }
  342. if (p^.typ=ait_external) and
  343. ((exttype=EXT_ANY) or (pai_external(p)^.exttyp=exttype)) and
  344. (strpas(pai_external(p)^.name)=_name) then
  345. begin
  346. search_assembler_symbol:=pai_external(p);
  347. exit;
  348. end
  349. else
  350. p:=pai(p^.next);
  351. if (p<>nil) and
  352. (p^.typ=ait_external) and
  353. (pai_external(p)^.exttyp=exttype) and
  354. (strpas(pai_external(p)^.name)=_name) then
  355. begin
  356. search_assembler_symbol:=pai_external(p);
  357. exit;
  358. end;
  359. end;
  360. end;
  361. { insert each need external only once }
  362. procedure concat_external(const _name : string;exttype : texternal_typ);
  363. var
  364. p : pai_external;
  365. begin
  366. p:=search_assembler_symbol(externals,_name,exttype);
  367. if p=nil then
  368. externals^.concat(new(pai_external,init(_name,exttype)));
  369. end;
  370. { insert each need external only once }
  371. procedure concat_internal(const _name : string;exttype : texternal_typ);
  372. var
  373. p : pai_external;
  374. begin
  375. p:=search_assembler_symbol(internals,_name,exttype);
  376. if p=nil then
  377. internals^.concat(new(pai_external,init(_name,exttype)));
  378. end;
  379. {****************************************************************************
  380. TAI_CONST
  381. ****************************************************************************}
  382. constructor tai_const.init_32bit(_value : longint);
  383. begin
  384. inherited init;
  385. typ:=ait_const_32bit;
  386. value:=_value;
  387. end;
  388. constructor tai_const.init_16bit(_value : word);
  389. begin
  390. inherited init;
  391. typ:=ait_const_16bit;
  392. value:=_value;
  393. end;
  394. constructor tai_const.init_8bit(_value : byte);
  395. begin
  396. inherited init;
  397. typ:=ait_const_8bit;
  398. value:=_value;
  399. end;
  400. constructor tai_const.init_symbol(p : pchar);
  401. begin
  402. inherited init;
  403. typ:=ait_const_symbol;
  404. value:=longint(p);
  405. end;
  406. constructor tai_const.init_rva(p : pchar);
  407. begin
  408. inherited init;
  409. typ:=ait_const_rva;
  410. value:=longint(p);
  411. end;
  412. destructor tai_const.done;
  413. begin
  414. if typ=ait_const_symbol then
  415. strdispose(pchar(value));
  416. inherited done;
  417. end;
  418. {****************************************************************************
  419. TAI_DOUBLE
  420. ****************************************************************************}
  421. constructor tai_double.init(_value : double);
  422. begin
  423. inherited init;
  424. typ:=ait_real_64bit;
  425. value:=_value;
  426. end;
  427. {****************************************************************************
  428. TAI_SINGLE
  429. ****************************************************************************}
  430. constructor tai_single.init(_value : single);
  431. begin
  432. inherited init;
  433. typ:=ait_real_32bit;
  434. value:=_value;
  435. end;
  436. {****************************************************************************
  437. TAI_EXTENDED
  438. ****************************************************************************}
  439. constructor tai_extended.init(_value : bestreal);
  440. begin
  441. inherited init;
  442. typ:=ait_real_extended;
  443. value:=_value;
  444. end;
  445. {****************************************************************************
  446. TAI_COMP
  447. ****************************************************************************}
  448. constructor tai_comp.init(_value : bestreal);
  449. begin
  450. inherited init;
  451. typ:=ait_comp;
  452. value:=_value;
  453. end;
  454. constructor tai_comp.init_comp(_value : comp);
  455. begin
  456. inherited init;
  457. typ:=ait_comp;
  458. value:=_value;
  459. end;
  460. {****************************************************************************
  461. TAI_STRING
  462. ****************************************************************************}
  463. constructor tai_string.init(const _str : string);
  464. begin
  465. inherited init;
  466. typ:=ait_string;
  467. getmem(str,length(_str)+1);
  468. strpcopy(str,_str);
  469. len:=length(_str);
  470. end;
  471. constructor tai_string.init_pchar(_str : pchar);
  472. begin
  473. inherited init;
  474. typ:=ait_string;
  475. str:=_str;
  476. len:=strlen(_str);
  477. end;
  478. constructor tai_string.init_length_pchar(_str : pchar;length : longint);
  479. begin
  480. inherited init;
  481. typ:=ait_string;
  482. str:=_str;
  483. len:=length;
  484. end;
  485. destructor tai_string.done;
  486. begin
  487. { you can have #0 inside the strings so }
  488. if str<>nil then
  489. freemem(str,len+1);
  490. inherited done;
  491. end;
  492. {****************************************************************************
  493. TAI_LABEL
  494. ****************************************************************************}
  495. constructor tai_label.init(_l : plabel);
  496. begin
  497. inherited init;
  498. typ:=ait_label;
  499. l:=_l;
  500. l^.is_set:=true;
  501. { suggestion of JM:
  502. inc(l^.refcount); }
  503. end;
  504. destructor tai_label.done;
  505. begin
  506. { suggestion of JM:
  507. dec(l^.refcount); }
  508. if (l^.is_used) then
  509. l^.is_set:=false
  510. else dispose(l);
  511. inherited done;
  512. end;
  513. {****************************************************************************
  514. TAI_DIRECT
  515. ****************************************************************************}
  516. constructor tai_direct.init(_str : pchar);
  517. begin
  518. inherited init;
  519. typ:=ait_direct;
  520. str:=_str;
  521. end;
  522. destructor tai_direct.done;
  523. begin
  524. strdispose(str);
  525. inherited done;
  526. end;
  527. {****************************************************************************
  528. TAI_ASM_COMMENT
  529. comment to be inserted in the assembler file
  530. ****************************************************************************}
  531. constructor tai_asm_comment.init(_str : pchar);
  532. begin
  533. inherited init;
  534. typ:=ait_comment;
  535. str:=_str;
  536. end;
  537. destructor tai_asm_comment.done;
  538. begin
  539. strdispose(str);
  540. inherited done;
  541. end;
  542. {****************************************************************************
  543. TAI_ALIGN
  544. ****************************************************************************}
  545. constructor tai_align.init(b: byte);
  546. begin
  547. inherited init;
  548. typ:=ait_align;
  549. if b in [1,2,4,8,16] then
  550. aligntype := b
  551. else
  552. aligntype := 1;
  553. op:=0;
  554. end;
  555. constructor tai_align.init_op(b: byte; use_op: byte);
  556. begin
  557. inherited init;
  558. typ:=ait_align;
  559. if b in [1,2,4,8,16] then
  560. aligntype := b
  561. else
  562. aligntype := 1;
  563. op:=use_op;
  564. end;
  565. destructor tai_align.done;
  566. begin
  567. inherited done;
  568. end;
  569. {****************************************************************************
  570. TAI_CUT
  571. ****************************************************************************}
  572. constructor tai_cut.init;
  573. begin
  574. inherited init;
  575. typ:=ait_cut;
  576. end;
  577. end.
  578. {
  579. $Log$
  580. Revision 1.4 1998-04-29 10:33:40 pierre
  581. + added some code for ansistring (not complete nor working yet)
  582. * corrected operator overloading
  583. * corrected nasm output
  584. + started inline procedures
  585. + added starstarn : use ** for exponentiation (^ gave problems)
  586. + started UseTokenInfo cond to get accurate positions
  587. Revision 1.3 1998/04/27 23:10:27 peter
  588. + new scanner
  589. * $makelib -> if smartlink
  590. * small filename fixes pmodule.setfilename
  591. * moved import from files.pas -> import.pas
  592. Revision 1.2 1998/04/09 15:46:37 florian
  593. + register allocation tracing stuff added
  594. Revision 1.1.1.1 1998/03/25 11:18:16 root
  595. * Restored version
  596. Revision 1.18 1998/03/10 16:27:36 pierre
  597. * better line info in stabs debug
  598. * symtabletype and lexlevel separated into two fields of tsymtable
  599. + ifdef MAKELIB for direct library output, not complete
  600. + ifdef CHAINPROCSYMS for overloaded seach across units, not fully
  601. working
  602. + ifdef TESTFUNCRET for setting func result in underfunction, not
  603. working
  604. Revision 1.17 1998/03/10 01:17:13 peter
  605. * all files have the same header
  606. * messages are fully implemented, EXTDEBUG uses Comment()
  607. + AG... files for the Assembler generation
  608. Revision 1.16 1998/03/02 01:47:56 peter
  609. * renamed target_DOS to target_GO32V1
  610. + new verbose system, merged old errors and verbose units into one new
  611. verbose.pas, so errors.pas is obsolete
  612. Revision 1.15 1998/02/28 14:43:46 florian
  613. * final implemenation of win32 imports
  614. * extended tai_align to allow 8 and 16 byte aligns
  615. Revision 1.14 1998/02/28 00:20:20 florian
  616. * more changes to get import libs for Win32 working
  617. Revision 1.13 1998/02/27 22:27:50 florian
  618. + win_targ unit
  619. + support of sections
  620. + new asmlists: sections, exports and resource
  621. Revision 1.12 1998/02/24 00:19:08 peter
  622. * makefile works again (btw. linux does like any char after a \ )
  623. * removed circular unit with assemble and files
  624. * fixed a sigsegv in pexpr
  625. * pmodule init unit/program is the almost the same, merged them
  626. Revision 1.11 1998/02/13 10:34:29 daniel
  627. * Made Motorola version compilable.
  628. * Fixed optimizer
  629. Revision 1.10 1998/02/06 23:08:31 florian
  630. + endian to targetinfo and sourceinfo added
  631. + endian independed writing of ppu file (reading missed), a PPU file
  632. is written with the target endian
  633. Revision 1.9 1998/01/11 04:14:30 carl
  634. + correct floating point support for m68k
  635. Revision 1.6 1997/12/09 13:18:34 carl
  636. + added pai_align abstract object (required for m68k)
  637. + renamed ait_real_s80bit --> ait_real_extended
  638. Revision 1.5 1997/12/01 18:14:32 pierre
  639. * fixes a bug in nasm output due to my previous changes
  640. Revision 1.3 1997/11/28 18:14:17 pierre
  641. working version with several bug fixes
  642. Revision 1.2 1997/11/28 14:26:18 florian
  643. Fixed some bugs
  644. Revision 1.1.1.1 1997/11/27 08:32:50 michael
  645. FPC Compiler CVS start
  646. Pre-CVS log:
  647. FK Florian Klaempfl
  648. PM Pierre Muller
  649. + feature added
  650. - removed
  651. * bug fixed or changed
  652. History:
  653. 30th september 1996:
  654. + unit started
  655. 13th november 1997:
  656. + added pai_single and pai_extended (PM)
  657. 14th november 1997:
  658. + added bestreal type and pai_bestreal
  659. to store all real consts with best precision (PM)
  660. has a drawback for GDB that does not know extended !! (PM)
  661. }