n386con.pas 21 KB

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