aasm.pas 21 KB

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