ncgcon.pas 20 KB

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