n386con.pas 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  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(pcompilerwidestring(value_str)^.data[i]));
  320. { return the offset of the real string }
  321. lab_str:=l2;
  322. end;
  323. end;
  324. st_shortstring:
  325. begin
  326. { truncate strings larger than 255 chars }
  327. if len>255 then
  328. l:=255
  329. else
  330. l:=len;
  331. { also length and terminating zero }
  332. getmem(pc,l+3);
  333. move(value_str^,pc[1],l+1);
  334. pc[0]:=chr(l);
  335. { to overcome this problem we set the length explicitly }
  336. { with the ending null char }
  337. pc[l+1]:=#0;
  338. Consts.concat(Tai_string.Create_length_pchar(pc,l+2));
  339. end;
  340. end;
  341. end;
  342. end;
  343. reset_reference(location.reference);
  344. location.reference.symbol:=lab_str;
  345. location.loc:=LOC_MEM;
  346. end;
  347. {*****************************************************************************
  348. TI386SETCONSTNODE
  349. *****************************************************************************}
  350. procedure ti386setconstnode.pass_2;
  351. var
  352. hp1 : tai;
  353. lastlabel : tasmlabel;
  354. i : longint;
  355. neededtyp : tait;
  356. begin
  357. { small sets are loaded as constants }
  358. if tsetdef(resulttype.def).settype=smallset then
  359. begin
  360. location.loc:=LOC_MEM;
  361. location.reference.is_immediate:=true;
  362. location.reference.offset:=plongint(value_set)^;
  363. exit;
  364. end;
  365. if tsetdef(resulttype.def).settype=smallset then
  366. neededtyp:=ait_const_32bit
  367. else
  368. neededtyp:=ait_const_8bit;
  369. lastlabel:=nil;
  370. { const already used ? }
  371. if not assigned(lab_set) then
  372. begin
  373. { tries to found an old entry }
  374. hp1:=tai(Consts.first);
  375. while assigned(hp1) do
  376. begin
  377. if hp1.typ=ait_label then
  378. lastlabel:=tai_label(hp1).l
  379. else
  380. begin
  381. if (lastlabel<>nil) and (hp1.typ=neededtyp) then
  382. begin
  383. if (hp1.typ=ait_const_8bit) then
  384. begin
  385. { compare normal set }
  386. i:=0;
  387. while assigned(hp1) and (i<32) do
  388. begin
  389. if tai_const(hp1).value<>value_set^[i] then
  390. break;
  391. inc(i);
  392. hp1:=tai(hp1.next);
  393. end;
  394. if i=32 then
  395. begin
  396. { found! }
  397. lab_set:=lastlabel;
  398. break;
  399. end;
  400. { leave when the end of consts is reached, so no
  401. hp1.next is done }
  402. if not assigned(hp1) then
  403. break;
  404. end
  405. else
  406. begin
  407. { compare small set }
  408. if plongint(value_set)^=tai_const(hp1).value then
  409. begin
  410. { found! }
  411. lab_set:=lastlabel;
  412. break;
  413. end;
  414. end;
  415. end;
  416. lastlabel:=nil;
  417. end;
  418. hp1:=tai(hp1.next);
  419. end;
  420. { :-(, we must generate a new entry }
  421. if not assigned(lab_set) then
  422. begin
  423. getdatalabel(lastlabel);
  424. lab_set:=lastlabel;
  425. if (cs_create_smart in aktmoduleswitches) then
  426. Consts.concat(Tai_cut.Create);
  427. Consts.concat(Tai_label.Create(lastlabel));
  428. if tsetdef(resulttype.def).settype=smallset then
  429. begin
  430. move(value_set^,i,sizeof(longint));
  431. Consts.concat(Tai_const.Create_32bit(i));
  432. end
  433. else
  434. begin
  435. for i:=0 to 31 do
  436. Consts.concat(Tai_const.Create_8bit(value_set^[i]));
  437. end;
  438. end;
  439. end;
  440. reset_reference(location.reference);
  441. location.reference.symbol:=lab_set;
  442. location.loc:=LOC_MEM;
  443. end;
  444. {*****************************************************************************
  445. TI386NILNODE
  446. *****************************************************************************}
  447. procedure ti386nilnode.pass_2;
  448. begin
  449. location.loc:=LOC_MEM;
  450. location.reference.is_immediate:=true;
  451. location.reference.offset:=0;
  452. end;
  453. begin
  454. crealconstnode:=ti386realconstnode;
  455. cordconstnode:=ti386ordconstnode;
  456. cpointerconstnode:=ti386pointerconstnode;
  457. cstringconstnode:=ti386stringconstnode;
  458. csetconstnode:=ti386setconstnode;
  459. cnilnode:=ti386nilnode;
  460. end.
  461. {
  462. $Log$
  463. Revision 1.9 2001-07-08 21:00:18 peter
  464. * various widestring updates, it works now mostly without charset
  465. mapping supported
  466. Revision 1.8 2001/04/13 01:22:18 peter
  467. * symtable change to classes
  468. * range check generation and errors fixed, make cycle DEBUG=1 works
  469. * memory leaks fixed
  470. Revision 1.7 2001/04/02 21:20:37 peter
  471. * resulttype rewrite
  472. Revision 1.6 2000/12/25 00:07:32 peter
  473. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  474. tlinkedlist objects)
  475. Revision 1.5 2000/11/29 00:30:47 florian
  476. * unused units removed from uses clause
  477. * some changes for widestrings
  478. Revision 1.4 2000/11/20 15:31:38 jonas
  479. * longint typecast to assignment of constant to offset field
  480. Revision 1.3 2000/11/13 14:44:36 jonas
  481. * fixes so no more range errors with improved range checking code
  482. Revision 1.2 2000/10/31 22:02:56 peter
  483. * symtable splitted, no real code changes
  484. Revision 1.1 2000/10/15 09:33:31 peter
  485. * moved n386*.pas to i386/ cpu_target dir
  486. Revision 1.2 2000/10/14 10:14:48 peter
  487. * moehrendorf oct 2000 rewrite
  488. Revision 1.1 2000/09/28 20:48:52 florian
  489. *** empty log message ***
  490. }