n386con.pas 21 KB

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