ncgcon.pas 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  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 defines.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,aasm,types,
  51. cpuinfo,cpubase,
  52. cginfo,cgbase,tgobj,rgobj;
  53. {*****************************************************************************
  54. TCGREALCONSTNODE
  55. *****************************************************************************}
  56. procedure tcgrealconstnode.pass_2;
  57. { I suppose the parser/pass_1 must make sure the generated real }
  58. { constants are actually supported by the target processor? (JM) }
  59. const
  60. floattype2ait:array[tfloattype] of tait=
  61. (ait_real_32bit,ait_real_64bit,ait_real_80bit,ait_comp_64bit);
  62. var
  63. hp1 : tai;
  64. lastlabel : tasmlabel;
  65. realait : tait;
  66. begin
  67. location_reset(location,LOC_CREFERENCE,def_cgsize(resulttype.def));
  68. lastlabel:=nil;
  69. realait:=floattype2ait[tfloatdef(resulttype.def).typ];
  70. { const already used ? }
  71. if not assigned(lab_real) then
  72. begin
  73. { tries to find an old entry }
  74. hp1:=tai(Consts.first);
  75. while assigned(hp1) do
  76. begin
  77. if hp1.typ=ait_label then
  78. lastlabel:=tai_label(hp1).l
  79. else
  80. begin
  81. if (hp1.typ=realait) and (lastlabel<>nil) then
  82. begin
  83. if(
  84. ((realait=ait_real_32bit) and (tai_real_32bit(hp1).value=value_real)) or
  85. ((realait=ait_real_64bit) and (tai_real_64bit(hp1).value=value_real)) or
  86. ((realait=ait_real_80bit) and (tai_real_80bit(hp1).value=value_real)) or
  87. ((realait=ait_comp_64bit) and (tai_comp_64bit(hp1).value=value_real))
  88. ) then
  89. begin
  90. { found! }
  91. lab_real:=lastlabel;
  92. break;
  93. end;
  94. end;
  95. lastlabel:=nil;
  96. end;
  97. hp1:=tai(hp1.next);
  98. end;
  99. { :-(, we must generate a new entry }
  100. if not assigned(lab_real) then
  101. begin
  102. getdatalabel(lastlabel);
  103. lab_real:=lastlabel;
  104. if (cs_create_smart in aktmoduleswitches) then
  105. Consts.concat(Tai_cut.Create);
  106. Consts.concat(Tai_label.Create(lastlabel));
  107. case realait of
  108. ait_real_32bit :
  109. Consts.concat(Tai_real_32bit.Create(value_real));
  110. ait_real_64bit :
  111. Consts.concat(Tai_real_64bit.Create(value_real));
  112. ait_real_80bit :
  113. Consts.concat(Tai_real_80bit.Create(value_real));
  114. ait_comp_64bit :
  115. Consts.concat(Tai_comp_64bit.Create(value_real));
  116. else
  117. internalerror(10120);
  118. end;
  119. end;
  120. end;
  121. location.reference.symbol:=lab_real;
  122. end;
  123. {*****************************************************************************
  124. TCGORDCONSTNODE
  125. *****************************************************************************}
  126. procedure tcgordconstnode.pass_2;
  127. begin
  128. location_reset(location,LOC_CONSTANT,def_cgsize(resulttype.def));
  129. location.valuelow:=AWord(value);
  130. location.valuehigh:=AWord(value shr 32);
  131. end;
  132. {*****************************************************************************
  133. TCGPOINTERCONSTNODE
  134. *****************************************************************************}
  135. procedure tcgpointerconstnode.pass_2;
  136. begin
  137. { an integer const. behaves as a memory reference }
  138. location_reset(location,LOC_CONSTANT,OS_ADDR);
  139. location.value:=AWord(value);
  140. end;
  141. {*****************************************************************************
  142. TCGSTRINGCONSTNODE
  143. *****************************************************************************}
  144. procedure tcgstringconstnode.pass_2;
  145. var
  146. hp1,hp2 : tai;
  147. l1,l2,
  148. lastlabel : tasmlabel;
  149. lastlabelhp : tai;
  150. pc : pchar;
  151. same_string : boolean;
  152. l,j,
  153. i,mylength : longint;
  154. begin
  155. { for empty ansistrings we could return a constant 0 }
  156. if (st_type in [st_ansistring,st_widestring]) and
  157. (len=0) then
  158. begin
  159. location_reset(location,LOC_CONSTANT,OS_ADDR);
  160. location.value:=0;
  161. exit;
  162. end;
  163. { return a constant reference in memory }
  164. location_reset(location,LOC_CREFERENCE,def_cgsize(resulttype.def));
  165. { const already used ? }
  166. lastlabel:=nil;
  167. lastlabelhp:=nil;
  168. if not assigned(lab_str) then
  169. begin
  170. if is_shortstring(resulttype.def) then
  171. mylength:=len+2
  172. else
  173. mylength:=len+1;
  174. { widestrings can't be reused yet }
  175. if not(is_widestring(resulttype.def)) then
  176. begin
  177. { tries to found an old entry }
  178. hp1:=tai(Consts.first);
  179. while assigned(hp1) do
  180. begin
  181. if hp1.typ=ait_label then
  182. begin
  183. lastlabel:=tai_label(hp1).l;
  184. lastlabelhp:=hp1;
  185. end
  186. else
  187. begin
  188. { when changing that code, be careful that }
  189. { you don't use typed consts, which are }
  190. { are also written to consts }
  191. { currently, this is no problem, because }
  192. { typed consts have no leading length or }
  193. { they have no trailing zero }
  194. if (hp1.typ=ait_string) and (lastlabel<>nil) and
  195. (tai_string(hp1).len=mylength) then
  196. begin
  197. same_string:=true;
  198. { if shortstring then check the length byte first and
  199. set the start index to 1 }
  200. case st_type of
  201. st_shortstring:
  202. begin
  203. if len=ord(tai_string(hp1).str[0]) then
  204. j:=1
  205. else
  206. same_string:=false;
  207. end;
  208. st_ansistring,
  209. st_widestring :
  210. begin
  211. { before the string the following sequence must be found:
  212. <label>
  213. constsymbol <datalabel>
  214. const32 <len>
  215. const32 <len>
  216. const32 -1
  217. we must then return <label> to reuse
  218. }
  219. hp2:=tai(lastlabelhp.previous);
  220. if assigned(hp2) and
  221. (hp2.typ=ait_const_32bit) and
  222. (tai_const(hp2).value=-1) and
  223. assigned(hp2.previous) and
  224. (tai(hp2.previous).typ=ait_const_32bit) and
  225. (tai_const(hp2.previous).value=len) and
  226. assigned(hp2.previous.previous) and
  227. (tai(hp2.previous.previous).typ=ait_const_32bit) and
  228. (tai_const(hp2.previous.previous).value=len) and
  229. assigned(hp2.previous.previous.previous) and
  230. (tai(hp2.previous.previous.previous).typ=ait_const_symbol) and
  231. assigned(hp2.previous.previous.previous.previous) and
  232. (tai(hp2.previous.previous.previous.previous).typ=ait_label) then
  233. begin
  234. lastlabel:=tai_label(hp2.previous.previous.previous.previous).l;
  235. j:=0;
  236. end
  237. else
  238. same_string:=false;
  239. end;
  240. else
  241. same_string:=false;
  242. end;
  243. { don't check if the length byte was already wrong }
  244. if same_string then
  245. begin
  246. for i:=0 to len do
  247. begin
  248. if tai_string(hp1).str[j]<>value_str[i] then
  249. begin
  250. same_string:=false;
  251. break;
  252. end;
  253. inc(j);
  254. end;
  255. end;
  256. { found ? }
  257. if same_string then
  258. begin
  259. lab_str:=lastlabel;
  260. break;
  261. end;
  262. end;
  263. lastlabel:=nil;
  264. end;
  265. hp1:=tai(hp1.next);
  266. end;
  267. end;
  268. { :-(, we must generate a new entry }
  269. if not assigned(lab_str) then
  270. begin
  271. getdatalabel(lastlabel);
  272. lab_str:=lastlabel;
  273. if (cs_create_smart in aktmoduleswitches) then
  274. Consts.concat(Tai_cut.Create);
  275. Consts.concat(Tai_label.Create(lastlabel));
  276. { generate an ansi string ? }
  277. case st_type of
  278. st_ansistring:
  279. begin
  280. { an empty ansi string is nil! }
  281. if len=0 then
  282. Consts.concat(Tai_const.Create_32bit(0))
  283. else
  284. begin
  285. getdatalabel(l1);
  286. getdatalabel(l2);
  287. Consts.concat(Tai_label.Create(l2));
  288. Consts.concat(Tai_const_symbol.Create(l1));
  289. Consts.concat(Tai_const.Create_32bit(len));
  290. Consts.concat(Tai_const.Create_32bit(len));
  291. Consts.concat(Tai_const.Create_32bit(-1));
  292. Consts.concat(Tai_label.Create(l1));
  293. getmem(pc,len+2);
  294. move(value_str^,pc^,len);
  295. pc[len]:=#0;
  296. { to overcome this problem we set the length explicitly }
  297. { with the ending null char }
  298. Consts.concat(Tai_string.Create_length_pchar(pc,len+1));
  299. { return the offset of the real string }
  300. lab_str:=l2;
  301. end;
  302. end;
  303. st_widestring:
  304. begin
  305. { an empty wide string is nil! }
  306. if len=0 then
  307. Consts.concat(Tai_const.Create_32bit(0))
  308. else
  309. begin
  310. getdatalabel(l1);
  311. getdatalabel(l2);
  312. Consts.concat(Tai_label.Create(l2));
  313. Consts.concat(Tai_const_symbol.Create(l1));
  314. { we use always UTF-16 coding for constants }
  315. { at least for now }
  316. { Consts.concat(Tai_const.Create_8bit(2)); }
  317. Consts.concat(Tai_const.Create_32bit(len));
  318. Consts.concat(Tai_const.Create_32bit(len));
  319. Consts.concat(Tai_const.Create_32bit(-1));
  320. Consts.concat(Tai_label.Create(l1));
  321. for i:=0 to len-1 do
  322. Consts.concat(Tai_const.Create_16bit(pcompilerwidestring(value_str)^.data[i]));
  323. { return the offset of the real string }
  324. lab_str:=l2;
  325. end;
  326. end;
  327. st_shortstring:
  328. begin
  329. { truncate strings larger than 255 chars }
  330. if len>255 then
  331. l:=255
  332. else
  333. l:=len;
  334. { also length and terminating zero }
  335. getmem(pc,l+3);
  336. move(value_str^,pc[1],l+1);
  337. pc[0]:=chr(l);
  338. { to overcome this problem we set the length explicitly }
  339. { with the ending null char }
  340. pc[l+1]:=#0;
  341. Consts.concat(Tai_string.Create_length_pchar(pc,l+2));
  342. end;
  343. end;
  344. end;
  345. end;
  346. location.reference.symbol:=lab_str;
  347. end;
  348. {*****************************************************************************
  349. TCGSETCONSTNODE
  350. *****************************************************************************}
  351. procedure tcgsetconstnode.pass_2;
  352. var
  353. hp1 : tai;
  354. lastlabel : tasmlabel;
  355. i : longint;
  356. neededtyp : tait;
  357. begin
  358. { small sets are loaded as constants }
  359. if tsetdef(resulttype.def).settype=smallset then
  360. begin
  361. location_reset(location,LOC_CONSTANT,OS_32);
  362. location.value:=PAWord(value_set)^;
  363. exit;
  364. end;
  365. location_reset(location,LOC_CREFERENCE,OS_NO);
  366. neededtyp:=ait_const_8bit;
  367. lastlabel:=nil;
  368. { const already used ? }
  369. if not assigned(lab_set) then
  370. begin
  371. { tries to found an old entry }
  372. hp1:=tai(Consts.first);
  373. while assigned(hp1) do
  374. begin
  375. if hp1.typ=ait_label then
  376. lastlabel:=tai_label(hp1).l
  377. else
  378. begin
  379. if (lastlabel<>nil) and (hp1.typ=neededtyp) then
  380. begin
  381. if (hp1.typ=ait_const_8bit) then
  382. begin
  383. { compare normal set }
  384. i:=0;
  385. while assigned(hp1) and (i<32) do
  386. begin
  387. if tai_const(hp1).value<>value_set^[i] then
  388. break;
  389. inc(i);
  390. hp1:=tai(hp1.next);
  391. end;
  392. if i=32 then
  393. begin
  394. { found! }
  395. lab_set:=lastlabel;
  396. break;
  397. end;
  398. { leave when the end of consts is reached, so no
  399. hp1.next is done }
  400. if not assigned(hp1) then
  401. break;
  402. end
  403. else
  404. begin
  405. { compare small set }
  406. if plongint(value_set)^=tai_const(hp1).value then
  407. begin
  408. { found! }
  409. lab_set:=lastlabel;
  410. break;
  411. end;
  412. end;
  413. end;
  414. lastlabel:=nil;
  415. end;
  416. hp1:=tai(hp1.next);
  417. end;
  418. { :-(, we must generate a new entry }
  419. if not assigned(lab_set) then
  420. begin
  421. getdatalabel(lastlabel);
  422. lab_set:=lastlabel;
  423. if (cs_create_smart in aktmoduleswitches) then
  424. Consts.concat(Tai_cut.Create);
  425. Consts.concat(Tai_label.Create(lastlabel));
  426. if tsetdef(resulttype.def).settype=smallset then
  427. begin
  428. move(value_set^,i,sizeof(longint));
  429. Consts.concat(Tai_const.Create_32bit(i));
  430. end
  431. else
  432. begin
  433. for i:=0 to 31 do
  434. Consts.concat(Tai_const.Create_8bit(value_set^[i]));
  435. end;
  436. end;
  437. end;
  438. location.reference.symbol:=lab_set;
  439. end;
  440. {*****************************************************************************
  441. TCGNILNODE
  442. *****************************************************************************}
  443. procedure tcgnilnode.pass_2;
  444. begin
  445. location_reset(location,LOC_CONSTANT,OS_ADDR);
  446. location.value:=0;
  447. end;
  448. {*****************************************************************************
  449. TCGPOINTERCONSTNODE
  450. *****************************************************************************}
  451. procedure tcgguidconstnode.pass_2;
  452. var
  453. tmplabel : TAsmLabel;
  454. i : integer;
  455. begin
  456. location_reset(location,LOC_CREFERENCE,OS_NO);
  457. { label for GUID }
  458. getdatalabel(tmplabel);
  459. consts.concat(Tai_label.Create(tmplabel));
  460. consts.concat(Tai_const.Create_32bit(value.D1));
  461. consts.concat(Tai_const.Create_16bit(value.D2));
  462. consts.concat(Tai_const.Create_16bit(value.D3));
  463. for i:=Low(value.D4) to High(value.D4) do
  464. consts.concat(Tai_const.Create_8bit(value.D4[i]));
  465. location.reference.symbol:=tmplabel;
  466. end;
  467. begin
  468. crealconstnode:=tcgrealconstnode;
  469. cordconstnode:=tcgordconstnode;
  470. cpointerconstnode:=tcgpointerconstnode;
  471. cstringconstnode:=tcgstringconstnode;
  472. csetconstnode:=tcgsetconstnode;
  473. cnilnode:=tcgnilnode;
  474. cguidconstnode:=tcgguidconstnode;
  475. end.
  476. {
  477. $Log$
  478. Revision 1.8 2002-05-14 19:27:33 peter
  479. * fixed reusing of ansistrings
  480. Revision 1.7 2002/04/04 19:05:57 peter
  481. * removed unused units
  482. * use tlocation.size in cg.a_*loc*() routines
  483. Revision 1.6 2002/04/02 17:11:28 peter
  484. * tlocation,treference update
  485. * LOC_CONSTANT added for better constant handling
  486. * secondadd splitted in multiple routines
  487. * location_force_reg added for loading a location to a register
  488. of a specified size
  489. * secondassignment parses now first the right and then the left node
  490. (this is compatible with Kylix). This saves a lot of push/pop especially
  491. with string operations
  492. * adapted some routines to use the new cg methods
  493. Revision 1.5 2002/03/31 20:26:34 jonas
  494. + a_loadfpu_* and a_loadmm_* methods in tcg
  495. * register allocation is now handled by a class and is mostly processor
  496. independent (+rgobj.pas and i386/rgcpu.pas)
  497. * temp allocation is now handled by a class (+tgobj.pas, -i386\tgcpu.pas)
  498. * some small improvements and fixes to the optimizer
  499. * some register allocation fixes
  500. * some fpuvaroffset fixes in the unary minus node
  501. * push/popusedregisters is now called rg.save/restoreusedregisters and
  502. (for i386) uses temps instead of push/pop's when using -Op3 (that code is
  503. also better optimizable)
  504. * fixed and optimized register saving/restoring for new/dispose nodes
  505. * LOC_FPU locations now also require their "register" field to be set to
  506. R_ST, not R_ST0 (the latter is used for LOC_CFPUREGISTER locations only)
  507. - list field removed of the tnode class because it's not used currently
  508. and can cause hard-to-find bugs
  509. Revision 1.4 2002/02/26 09:12:39 jonas
  510. * fixed problem when compiling the compiler with Delphi (reported by
  511. "Luc Langlois" <[email protected]>) (lo/hi don't work as in FPC
  512. when used with int64's under Delphi)
  513. }