ncgcon.pas 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 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. temp_gen,
  52. cpubase,
  53. tgcpu;
  54. {*****************************************************************************
  55. TCGREALCONSTNODE
  56. *****************************************************************************}
  57. procedure tcgrealconstnode.pass_2;
  58. { I suppose the parser/pass_1 must make sure the generated real }
  59. { constants are actually supported by the target processor? (JM) }
  60. const
  61. floattype2ait:array[tfloattype] of tait=
  62. (ait_real_32bit,ait_real_64bit,ait_real_80bit,ait_comp_64bit);
  63. var
  64. hp1 : tai;
  65. lastlabel : tasmlabel;
  66. realait : tait;
  67. begin
  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. reset_reference(location.reference);
  122. location.reference.symbol:=lab_real;
  123. location.loc:=LOC_MEM;
  124. end;
  125. {*****************************************************************************
  126. TCGORDCONSTNODE
  127. *****************************************************************************}
  128. procedure tcgordconstnode.pass_2;
  129. var
  130. l : tasmlabel;
  131. begin
  132. location.loc:=LOC_MEM;
  133. { still needs to be made more generic (and optimal), this is for }
  134. { when Peter implements LOC_ORDCONST (JM) }
  135. if is_64bitint(resulttype.def) then
  136. begin
  137. getdatalabel(l);
  138. if (cs_create_smart in aktmoduleswitches) then
  139. Consts.concat(Tai_cut.Create);
  140. Consts.concat(Tai_label.Create(l));
  141. Consts.concat(Tai_const.Create_32bit(longint(lo(value))));
  142. Consts.concat(Tai_const.Create_32bit(longint(hi(value))));
  143. reset_reference(location.reference);
  144. location.reference.symbol:=l;
  145. end
  146. else
  147. begin
  148. { non int64 const. behaves as a memory reference }
  149. location.reference.is_immediate:=true;
  150. location.reference.offset:=longint(value);
  151. end;
  152. end;
  153. {*****************************************************************************
  154. TCGPOINTERCONSTNODE
  155. *****************************************************************************}
  156. procedure tcgpointerconstnode.pass_2;
  157. begin
  158. { an integer const. behaves as a memory reference }
  159. location.loc:=LOC_MEM;
  160. location.reference.is_immediate:=true;
  161. location.reference.offset:=longint(value);
  162. end;
  163. {*****************************************************************************
  164. TCGSTRINGCONSTNODE
  165. *****************************************************************************}
  166. procedure tcgstringconstnode.pass_2;
  167. var
  168. hp1 : tai;
  169. l1,l2,
  170. lastlabel : tasmlabel;
  171. pc : pchar;
  172. same_string : boolean;
  173. l,j,
  174. i,mylength : longint;
  175. begin
  176. { for empty ansistrings we could return a constant 0 }
  177. if (is_ansistring(resulttype.def) or
  178. is_widestring(resulttype.def)) and
  179. (len=0) then
  180. begin
  181. location.loc:=LOC_MEM;
  182. location.reference.is_immediate:=true;
  183. location.reference.offset:=0;
  184. exit;
  185. end;
  186. { const already used ? }
  187. lastlabel:=nil;
  188. if not assigned(lab_str) then
  189. begin
  190. if is_shortstring(resulttype.def) then
  191. mylength:=len+2
  192. else
  193. mylength:=len+1;
  194. { widestrings can't be reused yet }
  195. if not(is_widestring(resulttype.def)) then
  196. begin
  197. { tries to found an old entry }
  198. hp1:=tai(Consts.first);
  199. while assigned(hp1) do
  200. begin
  201. if hp1.typ=ait_label then
  202. lastlabel:=tai_label(hp1).l
  203. else
  204. begin
  205. { when changing that code, be careful that }
  206. { you don't use typed consts, which are }
  207. { are also written to consts }
  208. { currently, this is no problem, because }
  209. { typed consts have no leading length or }
  210. { they have no trailing zero }
  211. if (hp1.typ=ait_string) and (lastlabel<>nil) and
  212. (tai_string(hp1).len=mylength) then
  213. begin
  214. same_string:=true;
  215. { if shortstring then check the length byte first and
  216. set the start index to 1 }
  217. if is_shortstring(resulttype.def) then
  218. begin
  219. if len<>ord(tai_string(hp1).str[0]) then
  220. same_string:=false;
  221. j:=1;
  222. end
  223. else
  224. j:=0;
  225. { don't check if the length byte was already wrong }
  226. if same_string then
  227. begin
  228. for i:=0 to len do
  229. begin
  230. if tai_string(hp1).str[j]<>value_str[i] then
  231. begin
  232. same_string:=false;
  233. break;
  234. end;
  235. inc(j);
  236. end;
  237. end;
  238. { found ? }
  239. if same_string then
  240. begin
  241. lab_str:=lastlabel;
  242. { create a new entry for ansistrings, but reuse the data }
  243. if (st_type in [st_ansistring,st_widestring]) then
  244. begin
  245. getdatalabel(l2);
  246. Consts.concat(Tai_label.Create(l2));
  247. Consts.concat(Tai_const_symbol.Create(lab_str));
  248. { return the offset of the real string }
  249. lab_str:=l2;
  250. end;
  251. break;
  252. end;
  253. end;
  254. lastlabel:=nil;
  255. end;
  256. hp1:=tai(hp1.next);
  257. end;
  258. end;
  259. { :-(, we must generate a new entry }
  260. if not assigned(lab_str) then
  261. begin
  262. getdatalabel(lastlabel);
  263. lab_str:=lastlabel;
  264. if (cs_create_smart in aktmoduleswitches) then
  265. Consts.concat(Tai_cut.Create);
  266. Consts.concat(Tai_label.Create(lastlabel));
  267. { generate an ansi string ? }
  268. case st_type of
  269. st_ansistring:
  270. begin
  271. { an empty ansi string is nil! }
  272. if len=0 then
  273. Consts.concat(Tai_const.Create_32bit(0))
  274. else
  275. begin
  276. getdatalabel(l1);
  277. getdatalabel(l2);
  278. Consts.concat(Tai_label.Create(l2));
  279. Consts.concat(Tai_const_symbol.Create(l1));
  280. Consts.concat(Tai_const.Create_32bit(len));
  281. Consts.concat(Tai_const.Create_32bit(len));
  282. Consts.concat(Tai_const.Create_32bit(-1));
  283. Consts.concat(Tai_label.Create(l1));
  284. getmem(pc,len+2);
  285. move(value_str^,pc^,len);
  286. pc[len]:=#0;
  287. { to overcome this problem we set the length explicitly }
  288. { with the ending null char }
  289. Consts.concat(Tai_string.Create_length_pchar(pc,len+1));
  290. { return the offset of the real string }
  291. lab_str:=l2;
  292. end;
  293. end;
  294. st_widestring:
  295. begin
  296. { an empty wide string is nil! }
  297. if len=0 then
  298. Consts.concat(Tai_const.Create_32bit(0))
  299. else
  300. begin
  301. getdatalabel(l1);
  302. getdatalabel(l2);
  303. Consts.concat(Tai_label.Create(l2));
  304. Consts.concat(Tai_const_symbol.Create(l1));
  305. { we use always UTF-16 coding for constants }
  306. { at least for now }
  307. { Consts.concat(Tai_const.Create_8bit(2)); }
  308. Consts.concat(Tai_const.Create_32bit(len));
  309. Consts.concat(Tai_const.Create_32bit(len));
  310. Consts.concat(Tai_const.Create_32bit(-1));
  311. Consts.concat(Tai_label.Create(l1));
  312. for i:=0 to len-1 do
  313. Consts.concat(Tai_const.Create_16bit(pcompilerwidestring(value_str)^.data[i]));
  314. { return the offset of the real string }
  315. lab_str:=l2;
  316. end;
  317. end;
  318. st_shortstring:
  319. begin
  320. { truncate strings larger than 255 chars }
  321. if len>255 then
  322. l:=255
  323. else
  324. l:=len;
  325. { also length and terminating zero }
  326. getmem(pc,l+3);
  327. move(value_str^,pc[1],l+1);
  328. pc[0]:=chr(l);
  329. { to overcome this problem we set the length explicitly }
  330. { with the ending null char }
  331. pc[l+1]:=#0;
  332. Consts.concat(Tai_string.Create_length_pchar(pc,l+2));
  333. end;
  334. end;
  335. end;
  336. end;
  337. reset_reference(location.reference);
  338. location.reference.symbol:=lab_str;
  339. location.loc:=LOC_MEM;
  340. end;
  341. {*****************************************************************************
  342. TCGSETCONSTNODE
  343. *****************************************************************************}
  344. procedure tcgsetconstnode.pass_2;
  345. var
  346. hp1 : tai;
  347. lastlabel : tasmlabel;
  348. i : longint;
  349. neededtyp : tait;
  350. begin
  351. { small sets are loaded as constants }
  352. if tsetdef(resulttype.def).settype=smallset then
  353. begin
  354. location.loc:=LOC_MEM;
  355. location.reference.is_immediate:=true;
  356. location.reference.offset:=plongint(value_set)^;
  357. exit;
  358. end;
  359. neededtyp:=ait_const_8bit;
  360. lastlabel:=nil;
  361. { const already used ? }
  362. if not assigned(lab_set) then
  363. begin
  364. { tries to found an old entry }
  365. hp1:=tai(Consts.first);
  366. while assigned(hp1) do
  367. begin
  368. if hp1.typ=ait_label then
  369. lastlabel:=tai_label(hp1).l
  370. else
  371. begin
  372. if (lastlabel<>nil) and (hp1.typ=neededtyp) then
  373. begin
  374. if (hp1.typ=ait_const_8bit) then
  375. begin
  376. { compare normal set }
  377. i:=0;
  378. while assigned(hp1) and (i<32) do
  379. begin
  380. if tai_const(hp1).value<>value_set^[i] then
  381. break;
  382. inc(i);
  383. hp1:=tai(hp1.next);
  384. end;
  385. if i=32 then
  386. begin
  387. { found! }
  388. lab_set:=lastlabel;
  389. break;
  390. end;
  391. { leave when the end of consts is reached, so no
  392. hp1.next is done }
  393. if not assigned(hp1) then
  394. break;
  395. end
  396. else
  397. begin
  398. { compare small set }
  399. if plongint(value_set)^=tai_const(hp1).value then
  400. begin
  401. { found! }
  402. lab_set:=lastlabel;
  403. break;
  404. end;
  405. end;
  406. end;
  407. lastlabel:=nil;
  408. end;
  409. hp1:=tai(hp1.next);
  410. end;
  411. { :-(, we must generate a new entry }
  412. if not assigned(lab_set) then
  413. begin
  414. getdatalabel(lastlabel);
  415. lab_set:=lastlabel;
  416. if (cs_create_smart in aktmoduleswitches) then
  417. Consts.concat(Tai_cut.Create);
  418. Consts.concat(Tai_label.Create(lastlabel));
  419. if tsetdef(resulttype.def).settype=smallset then
  420. begin
  421. move(value_set^,i,sizeof(longint));
  422. Consts.concat(Tai_const.Create_32bit(i));
  423. end
  424. else
  425. begin
  426. for i:=0 to 31 do
  427. Consts.concat(Tai_const.Create_8bit(value_set^[i]));
  428. end;
  429. end;
  430. end;
  431. reset_reference(location.reference);
  432. location.reference.symbol:=lab_set;
  433. location.loc:=LOC_MEM;
  434. end;
  435. {*****************************************************************************
  436. TCGNILNODE
  437. *****************************************************************************}
  438. procedure tcgnilnode.pass_2;
  439. begin
  440. location.loc:=LOC_MEM;
  441. location.reference.is_immediate:=true;
  442. location.reference.offset:=0;
  443. end;
  444. {*****************************************************************************
  445. TCGPOINTERCONSTNODE
  446. *****************************************************************************}
  447. procedure tcgguidconstnode.pass_2;
  448. var
  449. tmplabel : TAsmLabel;
  450. i : integer;
  451. begin
  452. location.loc:=LOC_MEM;
  453. { label for GUID }
  454. getdatalabel(tmplabel);
  455. consts.concat(Tai_label.Create(tmplabel));
  456. consts.concat(Tai_const.Create_32bit(value.D1));
  457. consts.concat(Tai_const.Create_16bit(value.D2));
  458. consts.concat(Tai_const.Create_16bit(value.D3));
  459. for i:=Low(value.D4) to High(value.D4) do
  460. consts.concat(Tai_const.Create_8bit(value.D4[i]));
  461. reset_reference(location.reference);
  462. location.reference.symbol:=tmplabel;
  463. end;
  464. begin
  465. crealconstnode:=tcgrealconstnode;
  466. cordconstnode:=tcgordconstnode;
  467. cpointerconstnode:=tcgpointerconstnode;
  468. cstringconstnode:=tcgstringconstnode;
  469. csetconstnode:=tcgsetconstnode;
  470. cnilnode:=tcgnilnode;
  471. cguidconstnode:=tcgguidconstnode;
  472. end.
  473. {
  474. $Log$
  475. Revision 1.3 2001-12-31 09:52:02 jonas
  476. * empty widestrings can also be optimized to the constant '0'
  477. Revision 1.2 2001/10/20 19:28:37 peter
  478. * interface 2 guid support
  479. * guid constants support
  480. Revision 1.1 2001/09/30 16:17:17 jonas
  481. * made most constant and mem handling processor independent
  482. }