cg386con.pas 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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 found 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. begin
  136. { an integer const. behaves as a memory reference }
  137. p^.location.loc:=LOC_MEM;
  138. p^.location.reference.is_immediate:=true;
  139. p^.location.reference.offset:=p^.value;
  140. end;
  141. {*****************************************************************************
  142. SecondPointerConst
  143. *****************************************************************************}
  144. procedure secondpointerconst(var p : ptree);
  145. begin
  146. { an integer const. behaves as a memory reference }
  147. p^.location.loc:=LOC_MEM;
  148. p^.location.reference.is_immediate:=true;
  149. p^.location.reference.offset:=p^.value;
  150. end;
  151. {*****************************************************************************
  152. SecondStringConst
  153. *****************************************************************************}
  154. procedure secondstringconst(var p : ptree);
  155. var
  156. hp1 : pai;
  157. l1,l2,
  158. lastlabel : pasmlabel;
  159. pc : pchar;
  160. same_string : boolean;
  161. l,j,
  162. i,mylength : longint;
  163. begin
  164. { for empty ansistrings we could return a constant 0 }
  165. if is_ansistring(p^.resulttype) and
  166. (p^.length=0) then
  167. begin
  168. p^.location.loc:=LOC_MEM;
  169. p^.location.reference.is_immediate:=true;
  170. p^.location.reference.offset:=0;
  171. exit;
  172. end;
  173. { const already used ? }
  174. lastlabel:=nil;
  175. if not assigned(p^.lab_str) then
  176. begin
  177. if is_shortstring(p^.resulttype) then
  178. mylength:=p^.length+2
  179. else
  180. mylength:=p^.length+1;
  181. { tries to found an old entry }
  182. hp1:=pai(consts^.first);
  183. while assigned(hp1) do
  184. begin
  185. if hp1^.typ=ait_label then
  186. lastlabel:=pai_label(hp1)^.l
  187. else
  188. begin
  189. { when changing that code, be careful that }
  190. { you don't use typed consts, which are }
  191. { are also written to consts }
  192. { currently, this is no problem, because }
  193. { typed consts have no leading length or }
  194. { they have no trailing zero }
  195. if (hp1^.typ=ait_string) and (lastlabel<>nil) and
  196. (pai_string(hp1)^.len=mylength) then
  197. begin
  198. same_string:=true;
  199. { if shortstring then check the length byte first and
  200. set the start index to 1 }
  201. if is_shortstring(p^.resulttype) then
  202. begin
  203. if p^.length<>ord(pai_string(hp1)^.str[0]) then
  204. same_string:=false;
  205. j:=1;
  206. end
  207. else
  208. j:=0;
  209. { don't check if the length byte was already wrong }
  210. if same_string then
  211. begin
  212. for i:=0 to p^.length do
  213. begin
  214. if pai_string(hp1)^.str[j]<>p^.value_str[i] then
  215. begin
  216. same_string:=false;
  217. break;
  218. end;
  219. inc(j);
  220. end;
  221. end;
  222. { found ? }
  223. if same_string then
  224. begin
  225. p^.lab_str:=lastlabel;
  226. { create a new entry for ansistrings, but reuse the data }
  227. if (p^.stringtype in [st_ansistring,st_widestring]) then
  228. begin
  229. getdatalabel(l2);
  230. consts^.concat(new(pai_label,init(l2)));
  231. consts^.concat(new(pai_const_symbol,init(p^.lab_str)));
  232. { return the offset of the real string }
  233. p^.lab_str:=l2;
  234. end;
  235. break;
  236. end;
  237. end;
  238. lastlabel:=nil;
  239. end;
  240. hp1:=pai(hp1^.next);
  241. end;
  242. { :-(, we must generate a new entry }
  243. if not assigned(p^.lab_str) then
  244. begin
  245. getdatalabel(lastlabel);
  246. p^.lab_str:=lastlabel;
  247. if (cs_create_smart in aktmoduleswitches) then
  248. consts^.concat(new(pai_cut,init));
  249. consts^.concat(new(pai_label,init(lastlabel)));
  250. { generate an ansi string ? }
  251. case p^.stringtype of
  252. st_ansistring:
  253. begin
  254. { an empty ansi string is nil! }
  255. if p^.length=0 then
  256. consts^.concat(new(pai_const,init_32bit(0)))
  257. else
  258. begin
  259. getdatalabel(l1);
  260. getdatalabel(l2);
  261. consts^.concat(new(pai_label,init(l2)));
  262. consts^.concat(new(pai_const_symbol,init(l1)));
  263. consts^.concat(new(pai_const,init_32bit(p^.length)));
  264. consts^.concat(new(pai_const,init_32bit(p^.length)));
  265. consts^.concat(new(pai_const,init_32bit(-1)));
  266. consts^.concat(new(pai_label,init(l1)));
  267. getmem(pc,p^.length+2);
  268. move(p^.value_str^,pc^,p^.length);
  269. pc[p^.length]:=#0;
  270. { to overcome this problem we set the length explicitly }
  271. { with the ending null char }
  272. consts^.concat(new(pai_string,init_length_pchar(pc,p^.length+1)));
  273. { return the offset of the real string }
  274. p^.lab_str:=l2;
  275. end;
  276. end;
  277. st_shortstring:
  278. begin
  279. { truncate strings larger than 255 chars }
  280. if p^.length>255 then
  281. l:=255
  282. else
  283. l:=p^.length;
  284. { also length and terminating zero }
  285. getmem(pc,l+3);
  286. move(p^.value_str^,pc[1],l+1);
  287. pc[0]:=chr(l);
  288. { to overcome this problem we set the length explicitly }
  289. { with the ending null char }
  290. pc[l+1]:=#0;
  291. consts^.concat(new(pai_string,init_length_pchar(pc,l+2)));
  292. end;
  293. end;
  294. end;
  295. end;
  296. reset_reference(p^.location.reference);
  297. p^.location.reference.symbol:=p^.lab_str;
  298. p^.location.loc:=LOC_MEM;
  299. end;
  300. {*****************************************************************************
  301. SecondSetCons
  302. *****************************************************************************}
  303. procedure secondsetconst(var p : ptree);
  304. var
  305. hp1 : pai;
  306. lastlabel : pasmlabel;
  307. i : longint;
  308. neededtyp : tait;
  309. begin
  310. { small sets are loaded as constants }
  311. if psetdef(p^.resulttype)^.settype=smallset then
  312. begin
  313. p^.location.loc:=LOC_MEM;
  314. p^.location.reference.is_immediate:=true;
  315. p^.location.reference.offset:=plongint(p^.value_set)^;
  316. exit;
  317. end;
  318. if psetdef(p^.resulttype)^.settype=smallset then
  319. neededtyp:=ait_const_32bit
  320. else
  321. neededtyp:=ait_const_8bit;
  322. lastlabel:=nil;
  323. { const already used ? }
  324. if not assigned(p^.lab_set) then
  325. begin
  326. { tries to found an old entry }
  327. hp1:=pai(consts^.first);
  328. while assigned(hp1) do
  329. begin
  330. if hp1^.typ=ait_label then
  331. lastlabel:=pai_label(hp1)^.l
  332. else
  333. begin
  334. if (lastlabel<>nil) and (hp1^.typ=neededtyp) then
  335. begin
  336. if (hp1^.typ=ait_const_8bit) then
  337. begin
  338. { compare normal set }
  339. i:=0;
  340. while assigned(hp1) and (i<32) do
  341. begin
  342. if pai_const(hp1)^.value<>p^.value_set^[i] then
  343. break;
  344. inc(i);
  345. hp1:=pai(hp1^.next);
  346. end;
  347. if i=32 then
  348. begin
  349. { found! }
  350. p^.lab_set:=lastlabel;
  351. break;
  352. end;
  353. { leave when the end of consts is reached, so no
  354. hp1^.next is done }
  355. if not assigned(hp1) then
  356. break;
  357. end
  358. else
  359. begin
  360. { compare small set }
  361. if plongint(p^.value_set)^=pai_const(hp1)^.value then
  362. begin
  363. { found! }
  364. p^.lab_set:=lastlabel;
  365. break;
  366. end;
  367. end;
  368. end;
  369. lastlabel:=nil;
  370. end;
  371. hp1:=pai(hp1^.next);
  372. end;
  373. { :-(, we must generate a new entry }
  374. if not assigned(p^.lab_set) then
  375. begin
  376. getdatalabel(lastlabel);
  377. p^.lab_set:=lastlabel;
  378. if (cs_create_smart in aktmoduleswitches) then
  379. consts^.concat(new(pai_cut,init));
  380. consts^.concat(new(pai_label,init(lastlabel)));
  381. if psetdef(p^.resulttype)^.settype=smallset then
  382. begin
  383. move(p^.value_set^,i,sizeof(longint));
  384. consts^.concat(new(pai_const,init_32bit(i)));
  385. end
  386. else
  387. begin
  388. for i:=0 to 31 do
  389. consts^.concat(new(pai_const,init_8bit(p^.value_set^[i])));
  390. end;
  391. end;
  392. end;
  393. reset_reference(p^.location.reference);
  394. p^.location.reference.symbol:=p^.lab_set;
  395. p^.location.loc:=LOC_MEM;
  396. end;
  397. {*****************************************************************************
  398. SecondNilN
  399. *****************************************************************************}
  400. procedure secondniln(var p : ptree);
  401. begin
  402. p^.location.loc:=LOC_MEM;
  403. p^.location.reference.is_immediate:=true;
  404. p^.location.reference.offset:=0;
  405. end;
  406. end.
  407. {
  408. $Log$
  409. Revision 1.2 2000-07-13 11:32:33 michael
  410. + removed logs
  411. }