cg386con.pas 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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 cg386con;
  19. {$i defines.inc}
  20. interface
  21. uses
  22. tree;
  23. procedure secondrealconst(var p : ptree);
  24. procedure secondfixconst(var p : ptree);
  25. procedure secondordconst(var p : ptree);
  26. procedure secondpointerconst(var p : ptree);
  27. procedure secondstringconst(var p : ptree);
  28. procedure secondsetconst(var p : ptree);
  29. procedure secondniln(var p : ptree);
  30. implementation
  31. uses
  32. globtype,systems,
  33. cobjects,verbose,globals,
  34. symconst,symtable,aasm,types,
  35. hcodegen,temp_gen,pass_2,
  36. cpubase,cpuasm,
  37. cgai386,tgeni386;
  38. {*****************************************************************************
  39. SecondRealConst
  40. *****************************************************************************}
  41. procedure secondrealconst(var p : ptree);
  42. const
  43. floattype2ait:array[tfloattype] of tait=
  44. (ait_real_32bit,ait_real_64bit,ait_real_80bit,ait_comp_64bit,ait_none,ait_none);
  45. var
  46. hp1 : pai;
  47. lastlabel : pasmlabel;
  48. realait : tait;
  49. begin
  50. if (p^.value_real=1.0) then
  51. begin
  52. emit_none(A_FLD1,S_NO);
  53. p^.location.loc:=LOC_FPU;
  54. inc(fpuvaroffset);
  55. end
  56. else if (p^.value_real=0.0) then
  57. begin
  58. emit_none(A_FLDZ,S_NO);
  59. p^.location.loc:=LOC_FPU;
  60. inc(fpuvaroffset);
  61. end
  62. else
  63. begin
  64. lastlabel:=nil;
  65. realait:=floattype2ait[pfloatdef(p^.resulttype)^.typ];
  66. { const already used ? }
  67. if not assigned(p^.lab_real) then
  68. begin
  69. { tries to find an old entry }
  70. hp1:=pai(consts^.first);
  71. while assigned(hp1) do
  72. begin
  73. if hp1^.typ=ait_label then
  74. lastlabel:=pai_label(hp1)^.l
  75. else
  76. begin
  77. if (hp1^.typ=realait) and (lastlabel<>nil) then
  78. begin
  79. if(
  80. ((realait=ait_real_32bit) and (pai_real_32bit(hp1)^.value=p^.value_real)) or
  81. ((realait=ait_real_64bit) and (pai_real_64bit(hp1)^.value=p^.value_real)) or
  82. ((realait=ait_real_80bit) and (pai_real_80bit(hp1)^.value=p^.value_real)) or
  83. ((realait=ait_comp_64bit) and (pai_comp_64bit(hp1)^.value=p^.value_real))
  84. ) then
  85. begin
  86. { found! }
  87. p^.lab_real:=lastlabel;
  88. break;
  89. end;
  90. end;
  91. lastlabel:=nil;
  92. end;
  93. hp1:=pai(hp1^.next);
  94. end;
  95. { :-(, we must generate a new entry }
  96. if not assigned(p^.lab_real) then
  97. begin
  98. getdatalabel(lastlabel);
  99. p^.lab_real:=lastlabel;
  100. if (cs_create_smart in aktmoduleswitches) then
  101. consts^.concat(new(pai_cut,init));
  102. consts^.concat(new(pai_label,init(lastlabel)));
  103. case realait of
  104. ait_real_32bit :
  105. consts^.concat(new(pai_real_32bit,init(p^.value_real)));
  106. ait_real_64bit :
  107. consts^.concat(new(pai_real_64bit,init(p^.value_real)));
  108. ait_real_80bit :
  109. consts^.concat(new(pai_real_80bit,init(p^.value_real)));
  110. ait_comp_64bit :
  111. consts^.concat(new(pai_comp_64bit,init(p^.value_real)));
  112. else
  113. internalerror(10120);
  114. end;
  115. end;
  116. end;
  117. reset_reference(p^.location.reference);
  118. p^.location.reference.symbol:=p^.lab_real;
  119. p^.location.loc:=LOC_MEM;
  120. end;
  121. end;
  122. {*****************************************************************************
  123. SecondFixConst
  124. *****************************************************************************}
  125. procedure secondfixconst(var p : ptree);
  126. begin
  127. { an fix comma const. behaves as a memory reference }
  128. p^.location.loc:=LOC_MEM;
  129. p^.location.reference.is_immediate:=true;
  130. p^.location.reference.offset:=p^.value_fix;
  131. end;
  132. {*****************************************************************************
  133. SecondOrdConst
  134. *****************************************************************************}
  135. procedure secondordconst(var p : ptree);
  136. var
  137. l : pasmlabel;
  138. begin
  139. p^.location.loc:=LOC_MEM;
  140. if is_64bitint(p^.resulttype) then
  141. begin
  142. getdatalabel(l);
  143. if (cs_create_smart in aktmoduleswitches) then
  144. consts^.concat(new(pai_cut,init));
  145. consts^.concat(new(pai_label,init(l)));
  146. consts^.concat(new(pai_const,init_32bit(lo(p^.value))));
  147. consts^.concat(new(pai_const,init_32bit(hi(p^.value))));
  148. reset_reference(p^.location.reference);
  149. p^.location.reference.symbol:=l;
  150. end
  151. else
  152. begin
  153. { non int64 const. behaves as a memory reference }
  154. p^.location.reference.is_immediate:=true;
  155. p^.location.reference.offset:=p^.value;
  156. end;
  157. end;
  158. {*****************************************************************************
  159. SecondPointerConst
  160. *****************************************************************************}
  161. procedure secondpointerconst(var p : ptree);
  162. begin
  163. { an integer const. behaves as a memory reference }
  164. p^.location.loc:=LOC_MEM;
  165. p^.location.reference.is_immediate:=true;
  166. p^.location.reference.offset:=p^.value;
  167. end;
  168. {*****************************************************************************
  169. SecondStringConst
  170. *****************************************************************************}
  171. procedure secondstringconst(var p : ptree);
  172. var
  173. hp1 : pai;
  174. l1,l2,
  175. lastlabel : pasmlabel;
  176. pc : pchar;
  177. same_string : boolean;
  178. l,j,
  179. i,mylength : longint;
  180. begin
  181. { for empty ansistrings we could return a constant 0 }
  182. if is_ansistring(p^.resulttype) and
  183. (p^.length=0) then
  184. begin
  185. p^.location.loc:=LOC_MEM;
  186. p^.location.reference.is_immediate:=true;
  187. p^.location.reference.offset:=0;
  188. exit;
  189. end;
  190. { const already used ? }
  191. lastlabel:=nil;
  192. if not assigned(p^.lab_str) then
  193. begin
  194. if is_shortstring(p^.resulttype) then
  195. mylength:=p^.length+2
  196. else
  197. mylength:=p^.length+1;
  198. { tries to found an old entry }
  199. hp1:=pai(consts^.first);
  200. while assigned(hp1) do
  201. begin
  202. if hp1^.typ=ait_label then
  203. lastlabel:=pai_label(hp1)^.l
  204. else
  205. begin
  206. { when changing that code, be careful that }
  207. { you don't use typed consts, which are }
  208. { are also written to consts }
  209. { currently, this is no problem, because }
  210. { typed consts have no leading length or }
  211. { they have no trailing zero }
  212. if (hp1^.typ=ait_string) and (lastlabel<>nil) and
  213. (pai_string(hp1)^.len=mylength) then
  214. begin
  215. same_string:=true;
  216. { if shortstring then check the length byte first and
  217. set the start index to 1 }
  218. if is_shortstring(p^.resulttype) then
  219. begin
  220. if p^.length<>ord(pai_string(hp1)^.str[0]) then
  221. same_string:=false;
  222. j:=1;
  223. end
  224. else
  225. j:=0;
  226. { don't check if the length byte was already wrong }
  227. if same_string then
  228. begin
  229. for i:=0 to p^.length do
  230. begin
  231. if pai_string(hp1)^.str[j]<>p^.value_str[i] then
  232. begin
  233. same_string:=false;
  234. break;
  235. end;
  236. inc(j);
  237. end;
  238. end;
  239. { found ? }
  240. if same_string then
  241. begin
  242. p^.lab_str:=lastlabel;
  243. { create a new entry for ansistrings, but reuse the data }
  244. if (p^.stringtype in [st_ansistring,st_widestring]) then
  245. begin
  246. getdatalabel(l2);
  247. consts^.concat(new(pai_label,init(l2)));
  248. consts^.concat(new(pai_const_symbol,init(p^.lab_str)));
  249. { return the offset of the real string }
  250. p^.lab_str:=l2;
  251. end;
  252. break;
  253. end;
  254. end;
  255. lastlabel:=nil;
  256. end;
  257. hp1:=pai(hp1^.next);
  258. end;
  259. { :-(, we must generate a new entry }
  260. if not assigned(p^.lab_str) then
  261. begin
  262. getdatalabel(lastlabel);
  263. p^.lab_str:=lastlabel;
  264. if (cs_create_smart in aktmoduleswitches) then
  265. consts^.concat(new(pai_cut,init));
  266. consts^.concat(new(pai_label,init(lastlabel)));
  267. { generate an ansi string ? }
  268. case p^.stringtype of
  269. st_ansistring:
  270. begin
  271. { an empty ansi string is nil! }
  272. if p^.length=0 then
  273. consts^.concat(new(pai_const,init_32bit(0)))
  274. else
  275. begin
  276. getdatalabel(l1);
  277. getdatalabel(l2);
  278. consts^.concat(new(pai_label,init(l2)));
  279. consts^.concat(new(pai_const_symbol,init(l1)));
  280. consts^.concat(new(pai_const,init_32bit(p^.length)));
  281. consts^.concat(new(pai_const,init_32bit(p^.length)));
  282. consts^.concat(new(pai_const,init_32bit(-1)));
  283. consts^.concat(new(pai_label,init(l1)));
  284. getmem(pc,p^.length+2);
  285. move(p^.value_str^,pc^,p^.length);
  286. pc[p^.length]:=#0;
  287. { to overcome this problem we set the length explicitly }
  288. { with the ending null char }
  289. consts^.concat(new(pai_string,init_length_pchar(pc,p^.length+1)));
  290. { return the offset of the real string }
  291. p^.lab_str:=l2;
  292. end;
  293. end;
  294. st_shortstring:
  295. begin
  296. { truncate strings larger than 255 chars }
  297. if p^.length>255 then
  298. l:=255
  299. else
  300. l:=p^.length;
  301. { also length and terminating zero }
  302. getmem(pc,l+3);
  303. move(p^.value_str^,pc[1],l+1);
  304. pc[0]:=chr(l);
  305. { to overcome this problem we set the length explicitly }
  306. { with the ending null char }
  307. pc[l+1]:=#0;
  308. consts^.concat(new(pai_string,init_length_pchar(pc,l+2)));
  309. end;
  310. end;
  311. end;
  312. end;
  313. reset_reference(p^.location.reference);
  314. p^.location.reference.symbol:=p^.lab_str;
  315. p^.location.loc:=LOC_MEM;
  316. end;
  317. {*****************************************************************************
  318. SecondSetCons
  319. *****************************************************************************}
  320. procedure secondsetconst(var p : ptree);
  321. var
  322. hp1 : pai;
  323. lastlabel : pasmlabel;
  324. i : longint;
  325. neededtyp : tait;
  326. begin
  327. { small sets are loaded as constants }
  328. if psetdef(p^.resulttype)^.settype=smallset then
  329. begin
  330. p^.location.loc:=LOC_MEM;
  331. p^.location.reference.is_immediate:=true;
  332. p^.location.reference.offset:=plongint(p^.value_set)^;
  333. exit;
  334. end;
  335. if psetdef(p^.resulttype)^.settype=smallset then
  336. neededtyp:=ait_const_32bit
  337. else
  338. neededtyp:=ait_const_8bit;
  339. lastlabel:=nil;
  340. { const already used ? }
  341. if not assigned(p^.lab_set) then
  342. begin
  343. { tries to found an old entry }
  344. hp1:=pai(consts^.first);
  345. while assigned(hp1) do
  346. begin
  347. if hp1^.typ=ait_label then
  348. lastlabel:=pai_label(hp1)^.l
  349. else
  350. begin
  351. if (lastlabel<>nil) and (hp1^.typ=neededtyp) then
  352. begin
  353. if (hp1^.typ=ait_const_8bit) then
  354. begin
  355. { compare normal set }
  356. i:=0;
  357. while assigned(hp1) and (i<32) do
  358. begin
  359. if pai_const(hp1)^.value<>p^.value_set^[i] then
  360. break;
  361. inc(i);
  362. hp1:=pai(hp1^.next);
  363. end;
  364. if i=32 then
  365. begin
  366. { found! }
  367. p^.lab_set:=lastlabel;
  368. break;
  369. end;
  370. { leave when the end of consts is reached, so no
  371. hp1^.next is done }
  372. if not assigned(hp1) then
  373. break;
  374. end
  375. else
  376. begin
  377. { compare small set }
  378. if plongint(p^.value_set)^=pai_const(hp1)^.value then
  379. begin
  380. { found! }
  381. p^.lab_set:=lastlabel;
  382. break;
  383. end;
  384. end;
  385. end;
  386. lastlabel:=nil;
  387. end;
  388. hp1:=pai(hp1^.next);
  389. end;
  390. { :-(, we must generate a new entry }
  391. if not assigned(p^.lab_set) then
  392. begin
  393. getdatalabel(lastlabel);
  394. p^.lab_set:=lastlabel;
  395. if (cs_create_smart in aktmoduleswitches) then
  396. consts^.concat(new(pai_cut,init));
  397. consts^.concat(new(pai_label,init(lastlabel)));
  398. if psetdef(p^.resulttype)^.settype=smallset then
  399. begin
  400. move(p^.value_set^,i,sizeof(longint));
  401. consts^.concat(new(pai_const,init_32bit(i)));
  402. end
  403. else
  404. begin
  405. for i:=0 to 31 do
  406. consts^.concat(new(pai_const,init_8bit(p^.value_set^[i])));
  407. end;
  408. end;
  409. end;
  410. reset_reference(p^.location.reference);
  411. p^.location.reference.symbol:=p^.lab_set;
  412. p^.location.loc:=LOC_MEM;
  413. end;
  414. {*****************************************************************************
  415. SecondNilN
  416. *****************************************************************************}
  417. procedure secondniln(var p : ptree);
  418. begin
  419. p^.location.loc:=LOC_MEM;
  420. p^.location.reference.is_immediate:=true;
  421. p^.location.reference.offset:=0;
  422. end;
  423. end.
  424. {
  425. $Log$
  426. Revision 1.4 2000-09-24 21:19:49 peter
  427. * delphi compile fixes
  428. Revision 1.3 2000/08/16 13:06:06 florian
  429. + support of 64 bit integer constants
  430. Revision 1.2 2000/07/13 11:32:33 michael
  431. + removed logs
  432. }