n386con.pas 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 by Florian Klaempfl
  4. Generate i386 assembler for constants
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit n386con;
  19. {$i defines.inc}
  20. interface
  21. uses
  22. node,ncon;
  23. type
  24. ti386realconstnode = class(trealconstnode)
  25. function pass_1 : tnode;override;
  26. procedure pass_2;override;
  27. end;
  28. ti386ordconstnode = class(tordconstnode)
  29. procedure pass_2;override;
  30. end;
  31. ti386pointerconstnode = class(tpointerconstnode)
  32. procedure pass_2;override;
  33. end;
  34. ti386stringconstnode = class(tstringconstnode)
  35. procedure pass_2;override;
  36. end;
  37. ti386setconstnode = class(tsetconstnode)
  38. procedure pass_2;override;
  39. end;
  40. ti386nilnode = 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. cga,tgcpu;
  51. {*****************************************************************************
  52. TI386REALCONSTNODE
  53. *****************************************************************************}
  54. function ti386realconstnode.pass_1 : tnode;
  55. begin
  56. result:=nil;
  57. if (value_real=1.0) or (value_real=0.0) then
  58. begin
  59. location.loc:=LOC_FPU;
  60. registersfpu:=1;
  61. end
  62. else
  63. location.loc:=LOC_MEM;
  64. end;
  65. procedure ti386realconstnode.pass_2;
  66. const
  67. floattype2ait:array[tfloattype] of tait=
  68. (ait_real_32bit,ait_real_64bit,ait_real_80bit,ait_comp_64bit);
  69. var
  70. hp1 : tai;
  71. lastlabel : tasmlabel;
  72. realait : tait;
  73. begin
  74. if (value_real=1.0) then
  75. begin
  76. emit_none(A_FLD1,S_NO);
  77. location.loc:=LOC_FPU;
  78. inc(fpuvaroffset);
  79. end
  80. else if (value_real=0.0) then
  81. begin
  82. emit_none(A_FLDZ,S_NO);
  83. location.loc:=LOC_FPU;
  84. inc(fpuvaroffset);
  85. end
  86. else
  87. begin
  88. lastlabel:=nil;
  89. realait:=floattype2ait[tfloatdef(resulttype.def).typ];
  90. { const already used ? }
  91. if not assigned(lab_real) then
  92. begin
  93. { tries to find an old entry }
  94. hp1:=tai(Consts.first);
  95. while assigned(hp1) do
  96. begin
  97. if hp1.typ=ait_label then
  98. lastlabel:=tai_label(hp1).l
  99. else
  100. begin
  101. if (hp1.typ=realait) and (lastlabel<>nil) then
  102. begin
  103. if(
  104. ((realait=ait_real_32bit) and (tai_real_32bit(hp1).value=value_real)) or
  105. ((realait=ait_real_64bit) and (tai_real_64bit(hp1).value=value_real)) or
  106. ((realait=ait_real_80bit) and (tai_real_80bit(hp1).value=value_real)) or
  107. ((realait=ait_comp_64bit) and (tai_comp_64bit(hp1).value=value_real))
  108. ) then
  109. begin
  110. { found! }
  111. lab_real:=lastlabel;
  112. break;
  113. end;
  114. end;
  115. lastlabel:=nil;
  116. end;
  117. hp1:=tai(hp1.next);
  118. end;
  119. { :-(, we must generate a new entry }
  120. if not assigned(lab_real) then
  121. begin
  122. getdatalabel(lastlabel);
  123. lab_real:=lastlabel;
  124. if (cs_create_smart in aktmoduleswitches) then
  125. Consts.concat(Tai_cut.Create);
  126. Consts.concat(Tai_label.Create(lastlabel));
  127. case realait of
  128. ait_real_32bit :
  129. Consts.concat(Tai_real_32bit.Create(value_real));
  130. ait_real_64bit :
  131. Consts.concat(Tai_real_64bit.Create(value_real));
  132. ait_real_80bit :
  133. Consts.concat(Tai_real_80bit.Create(value_real));
  134. ait_comp_64bit :
  135. Consts.concat(Tai_comp_64bit.Create(value_real));
  136. else
  137. internalerror(10120);
  138. end;
  139. end;
  140. end;
  141. reset_reference(location.reference);
  142. location.reference.symbol:=lab_real;
  143. location.loc:=LOC_MEM;
  144. end;
  145. end;
  146. {*****************************************************************************
  147. TI386ORDCONSTNODE
  148. *****************************************************************************}
  149. procedure ti386ordconstnode.pass_2;
  150. var
  151. l : tasmlabel;
  152. begin
  153. location.loc:=LOC_MEM;
  154. if is_64bitint(resulttype.def) then
  155. begin
  156. getdatalabel(l);
  157. if (cs_create_smart in aktmoduleswitches) then
  158. Consts.concat(Tai_cut.Create);
  159. Consts.concat(Tai_label.Create(l));
  160. Consts.concat(Tai_const.Create_32bit(longint(lo(value))));
  161. Consts.concat(Tai_const.Create_32bit(longint(hi(value))));
  162. reset_reference(location.reference);
  163. location.reference.symbol:=l;
  164. end
  165. else
  166. begin
  167. { non int64 const. behaves as a memory reference }
  168. location.reference.is_immediate:=true;
  169. location.reference.offset:=longint(value);
  170. end;
  171. end;
  172. {*****************************************************************************
  173. TI386POINTERCONSTNODE
  174. *****************************************************************************}
  175. procedure ti386pointerconstnode.pass_2;
  176. begin
  177. { an integer const. behaves as a memory reference }
  178. location.loc:=LOC_MEM;
  179. location.reference.is_immediate:=true;
  180. location.reference.offset:=longint(value);
  181. end;
  182. {*****************************************************************************
  183. Ti386STRINGCONSTNODE
  184. *****************************************************************************}
  185. procedure ti386stringconstnode.pass_2;
  186. var
  187. hp1 : tai;
  188. l1,l2,
  189. lastlabel : tasmlabel;
  190. pc : pchar;
  191. same_string : boolean;
  192. l,j,
  193. i,mylength : longint;
  194. begin
  195. { for empty ansistrings we could return a constant 0 }
  196. if is_ansistring(resulttype.def) and
  197. (len=0) then
  198. begin
  199. location.loc:=LOC_MEM;
  200. location.reference.is_immediate:=true;
  201. location.reference.offset:=0;
  202. exit;
  203. end;
  204. { const already used ? }
  205. lastlabel:=nil;
  206. if not assigned(lab_str) then
  207. begin
  208. if is_shortstring(resulttype.def) then
  209. mylength:=len+2
  210. else
  211. mylength:=len+1;
  212. { widestrings can't be reused yet }
  213. if not(is_widestring(resulttype.def)) then
  214. begin
  215. { tries to found an old entry }
  216. hp1:=tai(Consts.first);
  217. while assigned(hp1) do
  218. begin
  219. if hp1.typ=ait_label then
  220. lastlabel:=tai_label(hp1).l
  221. else
  222. begin
  223. { when changing that code, be careful that }
  224. { you don't use typed consts, which are }
  225. { are also written to consts }
  226. { currently, this is no problem, because }
  227. { typed consts have no leading length or }
  228. { they have no trailing zero }
  229. if (hp1.typ=ait_string) and (lastlabel<>nil) and
  230. (tai_string(hp1).len=mylength) then
  231. begin
  232. same_string:=true;
  233. { if shortstring then check the length byte first and
  234. set the start index to 1 }
  235. if is_shortstring(resulttype.def) then
  236. begin
  237. if len<>ord(tai_string(hp1).str[0]) then
  238. same_string:=false;
  239. j:=1;
  240. end
  241. else
  242. j:=0;
  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. { create a new entry for ansistrings, but reuse the data }
  261. if (st_type in [st_ansistring,st_widestring]) then
  262. begin
  263. getdatalabel(l2);
  264. Consts.concat(Tai_label.Create(l2));
  265. Consts.concat(Tai_const_symbol.Create(lab_str));
  266. { return the offset of the real string }
  267. lab_str:=l2;
  268. end;
  269. break;
  270. end;
  271. end;
  272. lastlabel:=nil;
  273. end;
  274. hp1:=tai(hp1.next);
  275. end;
  276. end;
  277. { :-(, we must generate a new entry }
  278. if not assigned(lab_str) then
  279. begin
  280. getdatalabel(lastlabel);
  281. lab_str:=lastlabel;
  282. if (cs_create_smart in aktmoduleswitches) then
  283. Consts.concat(Tai_cut.Create);
  284. Consts.concat(Tai_label.Create(lastlabel));
  285. { generate an ansi string ? }
  286. case st_type of
  287. st_ansistring:
  288. begin
  289. { an empty ansi string is nil! }
  290. if len=0 then
  291. Consts.concat(Tai_const.Create_32bit(0))
  292. else
  293. begin
  294. getdatalabel(l1);
  295. getdatalabel(l2);
  296. Consts.concat(Tai_label.Create(l2));
  297. Consts.concat(Tai_const_symbol.Create(l1));
  298. Consts.concat(Tai_const.Create_32bit(len));
  299. Consts.concat(Tai_const.Create_32bit(len));
  300. Consts.concat(Tai_const.Create_32bit(-1));
  301. Consts.concat(Tai_label.Create(l1));
  302. getmem(pc,len+2);
  303. move(value_str^,pc^,len);
  304. pc[len]:=#0;
  305. { to overcome this problem we set the length explicitly }
  306. { with the ending null char }
  307. Consts.concat(Tai_string.Create_length_pchar(pc,len+1));
  308. { return the offset of the real string }
  309. lab_str:=l2;
  310. end;
  311. end;
  312. st_widestring:
  313. begin
  314. { an empty wide string is nil! }
  315. if len=0 then
  316. Consts.concat(Tai_const.Create_32bit(0))
  317. else
  318. begin
  319. getdatalabel(l1);
  320. getdatalabel(l2);
  321. Consts.concat(Tai_label.Create(l2));
  322. Consts.concat(Tai_const_symbol.Create(l1));
  323. { we use always UTF-16 coding for constants }
  324. { at least for now }
  325. { Consts.concat(Tai_const.Create_8bit(2)); }
  326. Consts.concat(Tai_const.Create_32bit(len));
  327. Consts.concat(Tai_const.Create_32bit(len));
  328. Consts.concat(Tai_const.Create_32bit(-1));
  329. Consts.concat(Tai_label.Create(l1));
  330. for i:=0 to len-1 do
  331. Consts.concat(Tai_const.Create_16bit(pcompilerwidestring(value_str)^.data[i]));
  332. { return the offset of the real string }
  333. lab_str:=l2;
  334. end;
  335. end;
  336. st_shortstring:
  337. begin
  338. { truncate strings larger than 255 chars }
  339. if len>255 then
  340. l:=255
  341. else
  342. l:=len;
  343. { also length and terminating zero }
  344. getmem(pc,l+3);
  345. move(value_str^,pc[1],l+1);
  346. pc[0]:=chr(l);
  347. { to overcome this problem we set the length explicitly }
  348. { with the ending null char }
  349. pc[l+1]:=#0;
  350. Consts.concat(Tai_string.Create_length_pchar(pc,l+2));
  351. end;
  352. end;
  353. end;
  354. end;
  355. reset_reference(location.reference);
  356. location.reference.symbol:=lab_str;
  357. location.loc:=LOC_MEM;
  358. end;
  359. {*****************************************************************************
  360. TI386SETCONSTNODE
  361. *****************************************************************************}
  362. procedure ti386setconstnode.pass_2;
  363. var
  364. hp1 : tai;
  365. lastlabel : tasmlabel;
  366. i : longint;
  367. neededtyp : tait;
  368. begin
  369. { small sets are loaded as constants }
  370. if tsetdef(resulttype.def).settype=smallset then
  371. begin
  372. location.loc:=LOC_MEM;
  373. location.reference.is_immediate:=true;
  374. location.reference.offset:=plongint(value_set)^;
  375. exit;
  376. end;
  377. if tsetdef(resulttype.def).settype=smallset then
  378. neededtyp:=ait_const_32bit
  379. else
  380. neededtyp:=ait_const_8bit;
  381. lastlabel:=nil;
  382. { const already used ? }
  383. if not assigned(lab_set) then
  384. begin
  385. { tries to found an old entry }
  386. hp1:=tai(Consts.first);
  387. while assigned(hp1) do
  388. begin
  389. if hp1.typ=ait_label then
  390. lastlabel:=tai_label(hp1).l
  391. else
  392. begin
  393. if (lastlabel<>nil) and (hp1.typ=neededtyp) then
  394. begin
  395. if (hp1.typ=ait_const_8bit) then
  396. begin
  397. { compare normal set }
  398. i:=0;
  399. while assigned(hp1) and (i<32) do
  400. begin
  401. if tai_const(hp1).value<>value_set^[i] then
  402. break;
  403. inc(i);
  404. hp1:=tai(hp1.next);
  405. end;
  406. if i=32 then
  407. begin
  408. { found! }
  409. lab_set:=lastlabel;
  410. break;
  411. end;
  412. { leave when the end of consts is reached, so no
  413. hp1.next is done }
  414. if not assigned(hp1) then
  415. break;
  416. end
  417. else
  418. begin
  419. { compare small set }
  420. if plongint(value_set)^=tai_const(hp1).value then
  421. begin
  422. { found! }
  423. lab_set:=lastlabel;
  424. break;
  425. end;
  426. end;
  427. end;
  428. lastlabel:=nil;
  429. end;
  430. hp1:=tai(hp1.next);
  431. end;
  432. { :-(, we must generate a new entry }
  433. if not assigned(lab_set) then
  434. begin
  435. getdatalabel(lastlabel);
  436. lab_set:=lastlabel;
  437. if (cs_create_smart in aktmoduleswitches) then
  438. Consts.concat(Tai_cut.Create);
  439. Consts.concat(Tai_label.Create(lastlabel));
  440. if tsetdef(resulttype.def).settype=smallset then
  441. begin
  442. move(value_set^,i,sizeof(longint));
  443. Consts.concat(Tai_const.Create_32bit(i));
  444. end
  445. else
  446. begin
  447. for i:=0 to 31 do
  448. Consts.concat(Tai_const.Create_8bit(value_set^[i]));
  449. end;
  450. end;
  451. end;
  452. reset_reference(location.reference);
  453. location.reference.symbol:=lab_set;
  454. location.loc:=LOC_MEM;
  455. end;
  456. {*****************************************************************************
  457. TI386NILNODE
  458. *****************************************************************************}
  459. procedure ti386nilnode.pass_2;
  460. begin
  461. location.loc:=LOC_MEM;
  462. location.reference.is_immediate:=true;
  463. location.reference.offset:=0;
  464. end;
  465. begin
  466. crealconstnode:=ti386realconstnode;
  467. cordconstnode:=ti386ordconstnode;
  468. cpointerconstnode:=ti386pointerconstnode;
  469. cstringconstnode:=ti386stringconstnode;
  470. csetconstnode:=ti386setconstnode;
  471. cnilnode:=ti386nilnode;
  472. end.
  473. {
  474. $Log$
  475. Revision 1.10 2001-08-26 13:36:57 florian
  476. * some cg reorganisation
  477. * some PPC updates
  478. Revision 1.9 2001/07/08 21:00:18 peter
  479. * various widestring updates, it works now mostly without charset
  480. mapping supported
  481. Revision 1.8 2001/04/13 01:22:18 peter
  482. * symtable change to classes
  483. * range check generation and errors fixed, make cycle DEBUG=1 works
  484. * memory leaks fixed
  485. Revision 1.7 2001/04/02 21:20:37 peter
  486. * resulttype rewrite
  487. Revision 1.6 2000/12/25 00:07:32 peter
  488. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  489. tlinkedlist objects)
  490. Revision 1.5 2000/11/29 00:30:47 florian
  491. * unused units removed from uses clause
  492. * some changes for widestrings
  493. Revision 1.4 2000/11/20 15:31:38 jonas
  494. * longint typecast to assignment of constant to offset field
  495. Revision 1.3 2000/11/13 14:44:36 jonas
  496. * fixes so no more range errors with improved range checking code
  497. Revision 1.2 2000/10/31 22:02:56 peter
  498. * symtable splitted, no real code changes
  499. Revision 1.1 2000/10/15 09:33:31 peter
  500. * moved n386*.pas to i386/ cpu_target dir
  501. Revision 1.2 2000/10/14 10:14:48 peter
  502. * moehrendorf oct 2000 rewrite
  503. Revision 1.1 2000/09/28 20:48:52 florian
  504. *** empty log message ***
  505. }