ncgcon.pas 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. Generate assembler for constant nodes which are the same for
  5. all (most) processors
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. ****************************************************************************
  18. }
  19. unit ncgcon;
  20. {$i fpcdefs.inc}
  21. interface
  22. uses
  23. node,ncon;
  24. type
  25. tcgrealconstnode = class(trealconstnode)
  26. procedure pass_2;override;
  27. end;
  28. tcgordconstnode = class(tordconstnode)
  29. procedure pass_2;override;
  30. end;
  31. tcgpointerconstnode = class(tpointerconstnode)
  32. procedure pass_2;override;
  33. end;
  34. tcgstringconstnode = class(tstringconstnode)
  35. procedure pass_2;override;
  36. end;
  37. tcgsetconstnode = class(tsetconstnode)
  38. procedure pass_2;override;
  39. end;
  40. tcgnilnode = class(tnilnode)
  41. procedure pass_2;override;
  42. end;
  43. tcgguidconstnode = class(tguidconstnode)
  44. procedure pass_2;override;
  45. end;
  46. implementation
  47. uses
  48. globtype,widestr,systems,
  49. verbose,globals,
  50. symconst,symdef,aasmbase,aasmtai,aasmcpu,defutil,
  51. cpuinfo,cpubase,
  52. cginfo,cgbase,tgobj,rgobj
  53. {$ifdef delphi}
  54. ,dmisc
  55. {$endif}
  56. ;
  57. {*****************************************************************************
  58. TCGREALCONSTNODE
  59. *****************************************************************************}
  60. procedure tcgrealconstnode.pass_2;
  61. { I suppose the parser/pass_1 must make sure the generated real }
  62. { constants are actually supported by the target processor? (JM) }
  63. const
  64. floattype2ait:array[tfloattype] of taitype=
  65. (ait_real_32bit,ait_real_64bit,ait_real_80bit,ait_comp_64bit,ait_comp_64bit);
  66. var
  67. hp1 : tai;
  68. lastlabel : tasmlabel;
  69. realait : taitype;
  70. begin
  71. location_reset(location,LOC_CREFERENCE,def_cgsize(resulttype.def));
  72. lastlabel:=nil;
  73. realait:=floattype2ait[tfloatdef(resulttype.def).typ];
  74. { const already used ? }
  75. if not assigned(lab_real) then
  76. begin
  77. { tries to find an old entry }
  78. hp1:=tai(Consts.first);
  79. while assigned(hp1) do
  80. begin
  81. if hp1.typ=ait_label then
  82. lastlabel:=tai_label(hp1).l
  83. else
  84. begin
  85. if (hp1.typ=realait) and (lastlabel<>nil) then
  86. begin
  87. if(
  88. ((realait=ait_real_32bit) and (tai_real_32bit(hp1).value=value_real)) or
  89. ((realait=ait_real_64bit) and (tai_real_64bit(hp1).value=value_real)) or
  90. ((realait=ait_real_80bit) and (tai_real_80bit(hp1).value=value_real)) or
  91. ((realait=ait_comp_64bit) and (tai_comp_64bit(hp1).value=value_real))
  92. ) then
  93. begin
  94. { found! }
  95. lab_real:=lastlabel;
  96. break;
  97. end;
  98. end;
  99. lastlabel:=nil;
  100. end;
  101. hp1:=tai(hp1.next);
  102. end;
  103. { :-(, we must generate a new entry }
  104. if not assigned(lab_real) then
  105. begin
  106. objectlibrary.getdatalabel(lastlabel);
  107. lab_real:=lastlabel;
  108. if (cs_create_smart in aktmoduleswitches) then
  109. Consts.concat(Tai_cut.Create);
  110. consts.concat(tai_align.create(const_align(4)));
  111. Consts.concat(Tai_label.Create(lastlabel));
  112. case realait of
  113. ait_real_32bit :
  114. Consts.concat(Tai_real_32bit.Create(ts32real(value_real)));
  115. ait_real_64bit :
  116. Consts.concat(Tai_real_64bit.Create(ts64real(value_real)));
  117. ait_real_80bit :
  118. Consts.concat(Tai_real_80bit.Create(value_real));
  119. ait_comp_64bit :
  120. Consts.concat(Tai_comp_64bit.Create(value_real));
  121. else
  122. internalerror(10120);
  123. end;
  124. end;
  125. end;
  126. location.reference.symbol:=lab_real;
  127. end;
  128. {*****************************************************************************
  129. TCGORDCONSTNODE
  130. *****************************************************************************}
  131. procedure tcgordconstnode.pass_2;
  132. begin
  133. location_reset(location,LOC_CONSTANT,def_cgsize(resulttype.def));
  134. location.valueqword:=TConstExprUInt(value);
  135. end;
  136. {*****************************************************************************
  137. TCGPOINTERCONSTNODE
  138. *****************************************************************************}
  139. procedure tcgpointerconstnode.pass_2;
  140. begin
  141. { an integer const. behaves as a memory reference }
  142. location_reset(location,LOC_CONSTANT,OS_ADDR);
  143. location.value:=AWord(value);
  144. end;
  145. {*****************************************************************************
  146. TCGSTRINGCONSTNODE
  147. *****************************************************************************}
  148. procedure tcgstringconstnode.pass_2;
  149. var
  150. hp1,hp2 : tai;
  151. l1,l2,
  152. lastlabel : tasmlabel;
  153. lastlabelhp : tai;
  154. pc : pchar;
  155. same_string : boolean;
  156. l,j,
  157. i,mylength : longint;
  158. begin
  159. { for empty ansistrings we could return a constant 0 }
  160. if (st_type in [st_ansistring,st_widestring]) and
  161. (len=0) then
  162. begin
  163. location_reset(location,LOC_CONSTANT,OS_ADDR);
  164. location.value:=0;
  165. exit;
  166. end;
  167. { return a constant reference in memory }
  168. location_reset(location,LOC_CREFERENCE,def_cgsize(resulttype.def));
  169. { const already used ? }
  170. lastlabel:=nil;
  171. lastlabelhp:=nil;
  172. if not assigned(lab_str) then
  173. begin
  174. if is_shortstring(resulttype.def) then
  175. mylength:=len+2
  176. else
  177. mylength:=len+1;
  178. { widestrings can't be reused yet }
  179. if not(is_widestring(resulttype.def)) then
  180. begin
  181. { tries to found an old entry }
  182. hp1:=tai(Consts.first);
  183. while assigned(hp1) do
  184. begin
  185. if hp1.typ=ait_label then
  186. begin
  187. lastlabel:=tai_label(hp1).l;
  188. lastlabelhp:=hp1;
  189. end
  190. else
  191. begin
  192. { when changing that code, be careful that }
  193. { you don't use typed consts, which are }
  194. { are also written to consts }
  195. { currently, this is no problem, because }
  196. { typed consts have no leading length or }
  197. { they have no trailing zero }
  198. if (hp1.typ=ait_string) and (lastlabel<>nil) and
  199. (tai_string(hp1).len=mylength) then
  200. begin
  201. same_string:=true;
  202. { if shortstring then check the length byte first and
  203. set the start index to 1 }
  204. case st_type of
  205. st_shortstring:
  206. begin
  207. if len=ord(tai_string(hp1).str[0]) then
  208. j:=1
  209. else
  210. same_string:=false;
  211. end;
  212. st_ansistring,
  213. st_widestring :
  214. begin
  215. { before the string the following sequence must be found:
  216. <label>
  217. constsymbol <datalabel>
  218. const32 <len>
  219. const32 <len>
  220. const32 -1
  221. we must then return <label> to reuse
  222. }
  223. hp2:=tai(lastlabelhp.previous);
  224. if assigned(hp2) and
  225. (hp2.typ=ait_const_32bit) and
  226. (tai_const(hp2).value=-1) and
  227. assigned(hp2.previous) and
  228. (tai(hp2.previous).typ=ait_const_32bit) and
  229. (tai_const(hp2.previous).value=len) and
  230. assigned(hp2.previous.previous) and
  231. (tai(hp2.previous.previous).typ=ait_const_32bit) and
  232. (tai_const(hp2.previous.previous).value=len) and
  233. assigned(hp2.previous.previous.previous) and
  234. (tai(hp2.previous.previous.previous).typ=ait_const_symbol) and
  235. assigned(hp2.previous.previous.previous.previous) and
  236. (tai(hp2.previous.previous.previous.previous).typ=ait_label) then
  237. begin
  238. lastlabel:=tai_label(hp2.previous.previous.previous.previous).l;
  239. j:=0;
  240. end
  241. else
  242. same_string:=false;
  243. end;
  244. else
  245. same_string:=false;
  246. end;
  247. { don't check if the length byte was already wrong }
  248. if same_string then
  249. begin
  250. for i:=0 to len do
  251. begin
  252. if tai_string(hp1).str[j]<>value_str[i] then
  253. begin
  254. same_string:=false;
  255. break;
  256. end;
  257. inc(j);
  258. end;
  259. end;
  260. { found ? }
  261. if same_string then
  262. begin
  263. lab_str:=lastlabel;
  264. break;
  265. end;
  266. end;
  267. lastlabel:=nil;
  268. end;
  269. hp1:=tai(hp1.next);
  270. end;
  271. end;
  272. { :-(, we must generate a new entry }
  273. if not assigned(lab_str) then
  274. begin
  275. objectlibrary.getdatalabel(lastlabel);
  276. lab_str:=lastlabel;
  277. if (cs_create_smart in aktmoduleswitches) then
  278. Consts.concat(Tai_cut.Create);
  279. consts.concat(tai_align.create(const_align(4)));
  280. Consts.concat(Tai_label.Create(lastlabel));
  281. { generate an ansi string ? }
  282. case st_type of
  283. st_ansistring:
  284. begin
  285. { an empty ansi string is nil! }
  286. if len=0 then
  287. Consts.concat(Tai_const.Create_32bit(0))
  288. else
  289. begin
  290. objectlibrary.getdatalabel(l1);
  291. objectlibrary.getdatalabel(l2);
  292. Consts.concat(Tai_label.Create(l2));
  293. Consts.concat(Tai_const_symbol.Create(l1));
  294. Consts.concat(Tai_const.Create_32bit(len));
  295. Consts.concat(Tai_const.Create_32bit(len));
  296. Consts.concat(Tai_const.Create_32bit(-1));
  297. Consts.concat(Tai_label.Create(l1));
  298. getmem(pc,len+2);
  299. move(value_str^,pc^,len);
  300. pc[len]:=#0;
  301. { to overcome this problem we set the length explicitly }
  302. { with the ending null char }
  303. Consts.concat(Tai_string.Create_length_pchar(pc,len+1));
  304. { return the offset of the real string }
  305. lab_str:=l2;
  306. end;
  307. end;
  308. st_widestring:
  309. begin
  310. { an empty wide string is nil! }
  311. if len=0 then
  312. Consts.concat(Tai_const.Create_32bit(0))
  313. else
  314. begin
  315. objectlibrary.getdatalabel(l1);
  316. objectlibrary.getdatalabel(l2);
  317. Consts.concat(Tai_label.Create(l2));
  318. Consts.concat(Tai_const_symbol.Create(l1));
  319. { we use always UTF-16 coding for constants }
  320. { at least for now }
  321. { Consts.concat(Tai_const.Create_8bit(2)); }
  322. Consts.concat(Tai_const.Create_32bit(len));
  323. Consts.concat(Tai_const.Create_32bit(len));
  324. Consts.concat(Tai_const.Create_32bit(-1));
  325. Consts.concat(Tai_label.Create(l1));
  326. for i:=0 to len-1 do
  327. Consts.concat(Tai_const.Create_16bit(pcompilerwidestring(value_str)^.data[i]));
  328. { return the offset of the real string }
  329. lab_str:=l2;
  330. end;
  331. end;
  332. st_shortstring:
  333. begin
  334. { truncate strings larger than 255 chars }
  335. if len>255 then
  336. l:=255
  337. else
  338. l:=len;
  339. { also length and terminating zero }
  340. getmem(pc,l+3);
  341. move(value_str^,pc[1],l+1);
  342. pc[0]:=chr(l);
  343. { to overcome this problem we set the length explicitly }
  344. { with the ending null char }
  345. pc[l+1]:=#0;
  346. Consts.concat(Tai_string.Create_length_pchar(pc,l+2));
  347. end;
  348. end;
  349. end;
  350. end;
  351. location.reference.symbol:=lab_str;
  352. end;
  353. {*****************************************************************************
  354. TCGSETCONSTNODE
  355. *****************************************************************************}
  356. procedure tcgsetconstnode.pass_2;
  357. var
  358. hp1 : tai;
  359. lastlabel : tasmlabel;
  360. i : longint;
  361. neededtyp : taitype;
  362. type
  363. setbytes=array[0..31] of byte;
  364. Psetbytes=^setbytes;
  365. begin
  366. { small sets are loaded as constants }
  367. if tsetdef(resulttype.def).settype=smallset then
  368. begin
  369. location_reset(location,LOC_CONSTANT,OS_32);
  370. location.value:=PAWord(value_set)^;
  371. exit;
  372. end;
  373. location_reset(location,LOC_CREFERENCE,OS_NO);
  374. neededtyp:=ait_const_8bit;
  375. lastlabel:=nil;
  376. { const already used ? }
  377. if not assigned(lab_set) then
  378. begin
  379. { tries to found an old entry }
  380. hp1:=tai(Consts.first);
  381. while assigned(hp1) do
  382. begin
  383. if hp1.typ=ait_label then
  384. lastlabel:=tai_label(hp1).l
  385. else
  386. begin
  387. if (lastlabel<>nil) and (hp1.typ=neededtyp) then
  388. begin
  389. if (hp1.typ=ait_const_8bit) then
  390. begin
  391. { compare normal set }
  392. i:=0;
  393. while assigned(hp1) and (i<32) do
  394. begin
  395. {$ifdef oldset}
  396. if tai_const(hp1).value<>value_set^[i] then
  397. {$else}
  398. if tai_const(hp1).value<>Psetbytes(value_set)^[i] then
  399. {$endif}
  400. break;
  401. inc(i);
  402. hp1:=tai(hp1.next);
  403. end;
  404. if i=32 then
  405. begin
  406. { found! }
  407. lab_set:=lastlabel;
  408. break;
  409. end;
  410. { leave when the end of consts is reached, so no
  411. hp1.next is done }
  412. if not assigned(hp1) then
  413. break;
  414. end
  415. else
  416. begin
  417. { compare small set }
  418. if plongint(value_set)^=tai_const(hp1).value then
  419. begin
  420. { found! }
  421. lab_set:=lastlabel;
  422. break;
  423. end;
  424. end;
  425. end;
  426. lastlabel:=nil;
  427. end;
  428. hp1:=tai(hp1.next);
  429. end;
  430. { :-(, we must generate a new entry }
  431. if not assigned(lab_set) then
  432. begin
  433. objectlibrary.getdatalabel(lastlabel);
  434. lab_set:=lastlabel;
  435. if (cs_create_smart in aktmoduleswitches) then
  436. Consts.concat(Tai_cut.Create);
  437. consts.concat(tai_align.create(const_align(4)));
  438. Consts.concat(Tai_label.Create(lastlabel));
  439. if tsetdef(resulttype.def).settype=smallset then
  440. begin
  441. move(value_set^,i,sizeof(longint));
  442. Consts.concat(Tai_const.Create_32bit(i));
  443. end
  444. else
  445. begin
  446. for i:=0 to 31 do
  447. Consts.concat(Tai_const.Create_8bit(Psetbytes(value_set)^[i]));
  448. end;
  449. end;
  450. end;
  451. location.reference.symbol:=lab_set;
  452. end;
  453. {*****************************************************************************
  454. TCGNILNODE
  455. *****************************************************************************}
  456. procedure tcgnilnode.pass_2;
  457. begin
  458. location_reset(location,LOC_CONSTANT,OS_ADDR);
  459. location.value:=0;
  460. end;
  461. {*****************************************************************************
  462. TCGPOINTERCONSTNODE
  463. *****************************************************************************}
  464. procedure tcgguidconstnode.pass_2;
  465. var
  466. tmplabel : TAsmLabel;
  467. i : integer;
  468. begin
  469. location_reset(location,LOC_CREFERENCE,OS_NO);
  470. { label for GUID }
  471. objectlibrary.getdatalabel(tmplabel);
  472. consts.concat(tai_align.create(const_align(16)));
  473. consts.concat(Tai_label.Create(tmplabel));
  474. consts.concat(Tai_const.Create_32bit(value.D1));
  475. consts.concat(Tai_const.Create_16bit(value.D2));
  476. consts.concat(Tai_const.Create_16bit(value.D3));
  477. for i:=Low(value.D4) to High(value.D4) do
  478. consts.concat(Tai_const.Create_8bit(value.D4[i]));
  479. location.reference.symbol:=tmplabel;
  480. end;
  481. begin
  482. crealconstnode:=tcgrealconstnode;
  483. cordconstnode:=tcgordconstnode;
  484. cpointerconstnode:=tcgpointerconstnode;
  485. cstringconstnode:=tcgstringconstnode;
  486. csetconstnode:=tcgsetconstnode;
  487. cnilnode:=tcgnilnode;
  488. cguidconstnode:=tcgguidconstnode;
  489. end.
  490. {
  491. $Log$
  492. Revision 1.24 2002-12-07 14:10:21 carl
  493. * fix warnings by adding explicit typecasts
  494. Revision 1.23 2002/11/25 17:43:17 peter
  495. * splitted defbase in defutil,symutil,defcmp
  496. * merged isconvertable and is_equal into compare_defs(_ext)
  497. * made operator search faster by walking the list only once
  498. Revision 1.22 2002/11/09 15:36:50 carl
  499. * align all constants correctly (default of 4 size for real type constants)
  500. Revision 1.21 2002/10/06 21:01:50 peter
  501. * use tconstexpruint instead of qword
  502. Revision 1.20 2002/10/05 12:43:25 carl
  503. * fixes for Delphi 6 compilation
  504. (warning : Some features do not work under Delphi)
  505. Revision 1.19 2002/08/18 20:06:23 peter
  506. * inlining is now also allowed in interface
  507. * renamed write/load to ppuwrite/ppuload
  508. * tnode storing in ppu
  509. * nld,ncon,nbas are already updated for storing in ppu
  510. Revision 1.18 2002/08/11 14:32:26 peter
  511. * renamed current_library to objectlibrary
  512. Revision 1.17 2002/08/11 13:24:11 peter
  513. * saving of asmsymbols in ppu supported
  514. * asmsymbollist global is removed and moved into a new class
  515. tasmlibrarydata that will hold the info of a .a file which
  516. corresponds with a single module. Added librarydata to tmodule
  517. to keep the library info stored for the module. In the future the
  518. objectfiles will also be stored to the tasmlibrarydata class
  519. * all getlabel/newasmsymbol and friends are moved to the new class
  520. Revision 1.16 2002/08/10 17:15:06 jonas
  521. * endianess fix
  522. Revision 1.15 2002/07/23 12:34:30 daniel
  523. * Readded old set code. To use it define 'oldset'. Activated by default
  524. for ppc.
  525. Revision 1.14 2002/07/22 11:48:04 daniel
  526. * Sets are now internally sets.
  527. Revision 1.13 2002/07/20 11:57:53 florian
  528. * types.pas renamed to defbase.pas because D6 contains a types
  529. unit so this would conflicts if D6 programms are compiled
  530. + Willamette/SSE2 instructions to assembler added
  531. Revision 1.12 2002/07/01 18:46:22 peter
  532. * internal linker
  533. * reorganized aasm layer
  534. Revision 1.11 2002/07/01 16:23:53 peter
  535. * cg64 patch
  536. * basics for currency
  537. * asnode updates for class and interface (not finished)
  538. Revision 1.10 2002/05/18 13:34:09 peter
  539. * readded missing revisions
  540. Revision 1.9 2002/05/16 19:46:37 carl
  541. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  542. + try to fix temp allocation (still in ifdef)
  543. + generic constructor calls
  544. + start of tassembler / tmodulebase class cleanup
  545. Revision 1.7 2002/04/04 19:05:57 peter
  546. * removed unused units
  547. * use tlocation.size in cg.a_*loc*() routines
  548. Revision 1.6 2002/04/02 17:11:28 peter
  549. * tlocation,treference update
  550. * LOC_CONSTANT added for better constant handling
  551. * secondadd splitted in multiple routines
  552. * location_force_reg added for loading a location to a register
  553. of a specified size
  554. * secondassignment parses now first the right and then the left node
  555. (this is compatible with Kylix). This saves a lot of push/pop especially
  556. with string operations
  557. * adapted some routines to use the new cg methods
  558. Revision 1.5 2002/03/31 20:26:34 jonas
  559. + a_loadfpu_* and a_loadmm_* methods in tcg
  560. * register allocation is now handled by a class and is mostly processor
  561. independent (+rgobj.pas and i386/rgcpu.pas)
  562. * temp allocation is now handled by a class (+tgobj.pas, -i386\tgcpu.pas)
  563. * some small improvements and fixes to the optimizer
  564. * some register allocation fixes
  565. * some fpuvaroffset fixes in the unary minus node
  566. * push/popusedregisters is now called rg.save/restoreusedregisters and
  567. (for i386) uses temps instead of push/pop's when using -Op3 (that code is
  568. also better optimizable)
  569. * fixed and optimized register saving/restoring for new/dispose nodes
  570. * LOC_FPU locations now also require their "register" field to be set to
  571. R_ST, not R_ST0 (the latter is used for LOC_CFPUREGISTER locations only)
  572. - list field removed of the tnode class because it's not used currently
  573. and can cause hard-to-find bugs
  574. Revision 1.4 2002/02/26 09:12:39 jonas
  575. * fixed problem when compiling the compiler with Delphi (reported by
  576. "Luc Langlois" <[email protected]>) (lo/hi don't work as in FPC
  577. when used with int64's under Delphi)
  578. }