cg386con.pas 18 KB

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