n386con.pas 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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,systems,
  48. cobjects,verbose,globals,
  49. symconst,symdef,aasm,types,
  50. hcodegen,temp_gen,pass_2,
  51. cpubase,cpuasm,
  52. cgai386,tgeni386;
  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 : pai;
  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:=pai(consts^.first);
  86. while assigned(hp1) do
  87. begin
  88. if hp1^.typ=ait_label then
  89. lastlabel:=pai_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 (pai_real_32bit(hp1)^.value=value_real)) or
  96. ((realait=ait_real_64bit) and (pai_real_64bit(hp1)^.value=value_real)) or
  97. ((realait=ait_real_80bit) and (pai_real_80bit(hp1)^.value=value_real)) or
  98. ((realait=ait_comp_64bit) and (pai_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:=pai(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(new(pai_cut,init));
  117. consts^.concat(new(pai_label,init(lastlabel)));
  118. case realait of
  119. ait_real_32bit :
  120. consts^.concat(new(pai_real_32bit,init(value_real)));
  121. ait_real_64bit :
  122. consts^.concat(new(pai_real_64bit,init(value_real)));
  123. ait_real_80bit :
  124. consts^.concat(new(pai_real_80bit,init(value_real)));
  125. ait_comp_64bit :
  126. consts^.concat(new(pai_comp_64bit,init(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(new(pai_cut,init));
  160. consts^.concat(new(pai_label,init(l)));
  161. consts^.concat(new(pai_const,init_32bit(longint(lo(value)))));
  162. consts^.concat(new(pai_const,init_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:=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 : pai;
  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. { tries to found an old entry }
  214. hp1:=pai(consts^.first);
  215. while assigned(hp1) do
  216. begin
  217. if hp1^.typ=ait_label then
  218. lastlabel:=pai_label(hp1)^.l
  219. else
  220. begin
  221. { when changing that code, be careful that }
  222. { you don't use typed consts, which are }
  223. { are also written to consts }
  224. { currently, this is no problem, because }
  225. { typed consts have no leading length or }
  226. { they have no trailing zero }
  227. if (hp1^.typ=ait_string) and (lastlabel<>nil) and
  228. (pai_string(hp1)^.len=mylength) then
  229. begin
  230. same_string:=true;
  231. { if shortstring then check the length byte first and
  232. set the start index to 1 }
  233. if is_shortstring(resulttype) then
  234. begin
  235. if len<>ord(pai_string(hp1)^.str[0]) then
  236. same_string:=false;
  237. j:=1;
  238. end
  239. else
  240. j:=0;
  241. { don't check if the length byte was already wrong }
  242. if same_string then
  243. begin
  244. for i:=0 to len do
  245. begin
  246. if pai_string(hp1)^.str[j]<>value_str[i] then
  247. begin
  248. same_string:=false;
  249. break;
  250. end;
  251. inc(j);
  252. end;
  253. end;
  254. { found ? }
  255. if same_string then
  256. begin
  257. lab_str:=lastlabel;
  258. { create a new entry for ansistrings, but reuse the data }
  259. if (stringtype in [st_ansistring,st_widestring]) then
  260. begin
  261. getdatalabel(l2);
  262. consts^.concat(new(pai_label,init(l2)));
  263. consts^.concat(new(pai_const_symbol,init(lab_str)));
  264. { return the offset of the real string }
  265. lab_str:=l2;
  266. end;
  267. break;
  268. end;
  269. end;
  270. lastlabel:=nil;
  271. end;
  272. hp1:=pai(hp1^.next);
  273. end;
  274. { :-(, we must generate a new entry }
  275. if not assigned(lab_str) then
  276. begin
  277. getdatalabel(lastlabel);
  278. lab_str:=lastlabel;
  279. if (cs_create_smart in aktmoduleswitches) then
  280. consts^.concat(new(pai_cut,init));
  281. consts^.concat(new(pai_label,init(lastlabel)));
  282. { generate an ansi string ? }
  283. case stringtype of
  284. st_ansistring:
  285. begin
  286. { an empty ansi string is nil! }
  287. if len=0 then
  288. consts^.concat(new(pai_const,init_32bit(0)))
  289. else
  290. begin
  291. getdatalabel(l1);
  292. getdatalabel(l2);
  293. consts^.concat(new(pai_label,init(l2)));
  294. consts^.concat(new(pai_const_symbol,init(l1)));
  295. consts^.concat(new(pai_const,init_32bit(len)));
  296. consts^.concat(new(pai_const,init_32bit(len)));
  297. consts^.concat(new(pai_const,init_32bit(-1)));
  298. consts^.concat(new(pai_label,init(l1)));
  299. getmem(pc,len+2);
  300. move(value_str^,pc^,len);
  301. pc[len]:=#0;
  302. { to overcome this problem we set the length explicitly }
  303. { with the ending null char }
  304. consts^.concat(new(pai_string,init_length_pchar(pc,len+1)));
  305. { return the offset of the real string }
  306. lab_str:=l2;
  307. end;
  308. end;
  309. st_shortstring:
  310. begin
  311. { truncate strings larger than 255 chars }
  312. if len>255 then
  313. l:=255
  314. else
  315. l:=len;
  316. { also length and terminating zero }
  317. getmem(pc,l+3);
  318. move(value_str^,pc[1],l+1);
  319. pc[0]:=chr(l);
  320. { to overcome this problem we set the length explicitly }
  321. { with the ending null char }
  322. pc[l+1]:=#0;
  323. consts^.concat(new(pai_string,init_length_pchar(pc,l+2)));
  324. end;
  325. end;
  326. end;
  327. end;
  328. reset_reference(location.reference);
  329. location.reference.symbol:=lab_str;
  330. location.loc:=LOC_MEM;
  331. end;
  332. {*****************************************************************************
  333. TI386SETCONSTNODE
  334. *****************************************************************************}
  335. procedure ti386setconstnode.pass_2;
  336. var
  337. hp1 : pai;
  338. lastlabel : pasmlabel;
  339. i : longint;
  340. neededtyp : tait;
  341. begin
  342. { small sets are loaded as constants }
  343. if psetdef(resulttype)^.settype=smallset then
  344. begin
  345. location.loc:=LOC_MEM;
  346. location.reference.is_immediate:=true;
  347. location.reference.offset:=plongint(value_set)^;
  348. exit;
  349. end;
  350. if psetdef(resulttype)^.settype=smallset then
  351. neededtyp:=ait_const_32bit
  352. else
  353. neededtyp:=ait_const_8bit;
  354. lastlabel:=nil;
  355. { const already used ? }
  356. if not assigned(lab_set) then
  357. begin
  358. { tries to found an old entry }
  359. hp1:=pai(consts^.first);
  360. while assigned(hp1) do
  361. begin
  362. if hp1^.typ=ait_label then
  363. lastlabel:=pai_label(hp1)^.l
  364. else
  365. begin
  366. if (lastlabel<>nil) and (hp1^.typ=neededtyp) then
  367. begin
  368. if (hp1^.typ=ait_const_8bit) then
  369. begin
  370. { compare normal set }
  371. i:=0;
  372. while assigned(hp1) and (i<32) do
  373. begin
  374. if pai_const(hp1)^.value<>value_set^[i] then
  375. break;
  376. inc(i);
  377. hp1:=pai(hp1^.next);
  378. end;
  379. if i=32 then
  380. begin
  381. { found! }
  382. lab_set:=lastlabel;
  383. break;
  384. end;
  385. { leave when the end of consts is reached, so no
  386. hp1^.next is done }
  387. if not assigned(hp1) then
  388. break;
  389. end
  390. else
  391. begin
  392. { compare small set }
  393. if plongint(value_set)^=pai_const(hp1)^.value then
  394. begin
  395. { found! }
  396. lab_set:=lastlabel;
  397. break;
  398. end;
  399. end;
  400. end;
  401. lastlabel:=nil;
  402. end;
  403. hp1:=pai(hp1^.next);
  404. end;
  405. { :-(, we must generate a new entry }
  406. if not assigned(lab_set) then
  407. begin
  408. getdatalabel(lastlabel);
  409. lab_set:=lastlabel;
  410. if (cs_create_smart in aktmoduleswitches) then
  411. consts^.concat(new(pai_cut,init));
  412. consts^.concat(new(pai_label,init(lastlabel)));
  413. if psetdef(resulttype)^.settype=smallset then
  414. begin
  415. move(value_set^,i,sizeof(longint));
  416. consts^.concat(new(pai_const,init_32bit(i)));
  417. end
  418. else
  419. begin
  420. for i:=0 to 31 do
  421. consts^.concat(new(pai_const,init_8bit(value_set^[i])));
  422. end;
  423. end;
  424. end;
  425. reset_reference(location.reference);
  426. location.reference.symbol:=lab_set;
  427. location.loc:=LOC_MEM;
  428. end;
  429. {*****************************************************************************
  430. TI386NILNODE
  431. *****************************************************************************}
  432. procedure ti386nilnode.pass_2;
  433. begin
  434. location.loc:=LOC_MEM;
  435. location.reference.is_immediate:=true;
  436. location.reference.offset:=0;
  437. end;
  438. begin
  439. crealconstnode:=ti386realconstnode;
  440. cfixconstnode:=ti386fixconstnode;
  441. cordconstnode:=ti386ordconstnode;
  442. cpointerconstnode:=ti386pointerconstnode;
  443. cstringconstnode:=ti386stringconstnode;
  444. csetconstnode:=ti386setconstnode;
  445. cnilnode:=ti386nilnode;
  446. end.
  447. {
  448. $Log$
  449. Revision 1.3 2000-11-13 14:44:36 jonas
  450. * fixes so no more range errors with improved range checking code
  451. Revision 1.2 2000/10/31 22:02:56 peter
  452. * symtable splitted, no real code changes
  453. Revision 1.1 2000/10/15 09:33:31 peter
  454. * moved n386*.pas to i386/ cpu_target dir
  455. Revision 1.2 2000/10/14 10:14:48 peter
  456. * moehrendorf oct 2000 rewrite
  457. Revision 1.1 2000/09/28 20:48:52 florian
  458. *** empty log message ***
  459. }