ncgcon.pas 19 KB

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