aasm.pas 21 KB

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