cg386con.pas 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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. lastlabel:=nil;
  165. { const already used ? }
  166. if not assigned(p^.lab_str) then
  167. begin
  168. if is_shortstring(p^.resulttype) then
  169. mylength:=p^.length+2
  170. else
  171. mylength:=p^.length+1;
  172. { tries to found an old entry }
  173. hp1:=pai(consts^.first);
  174. while assigned(hp1) do
  175. begin
  176. if hp1^.typ=ait_label then
  177. lastlabel:=pai_label(hp1)^.l
  178. else
  179. begin
  180. { when changing that code, be careful that }
  181. { you don't use typed consts, which are }
  182. { are also written to consts }
  183. { currently, this is no problem, because }
  184. { typed consts have no leading length or }
  185. { they have no trailing zero }
  186. if (hp1^.typ=ait_string) and (lastlabel<>nil) and
  187. (pai_string(hp1)^.len=mylength) then
  188. begin
  189. same_string:=true;
  190. { if shortstring then check the length byte first and
  191. set the start index to 1 }
  192. if is_shortstring(p^.resulttype) then
  193. begin
  194. if p^.length<>ord(pai_string(hp1)^.str[0]) then
  195. same_string:=false;
  196. j:=1;
  197. end
  198. else
  199. j:=0;
  200. { don't check if the length byte was already wrong }
  201. if same_string then
  202. begin
  203. for i:=0 to p^.length do
  204. begin
  205. if pai_string(hp1)^.str[j]<>p^.value_str[i] then
  206. begin
  207. same_string:=false;
  208. break;
  209. end;
  210. inc(j);
  211. end;
  212. end;
  213. { found ? }
  214. if same_string then
  215. begin
  216. p^.lab_str:=lastlabel;
  217. { create a new entry for ansistrings, but reuse the data }
  218. if (p^.stringtype in [st_ansistring,st_widestring]) then
  219. begin
  220. getdatalabel(l2);
  221. consts^.concat(new(pai_label,init(l2)));
  222. consts^.concat(new(pai_const_symbol,init(p^.lab_str)));
  223. { return the offset of the real string }
  224. p^.lab_str:=l2;
  225. end;
  226. break;
  227. end;
  228. end;
  229. lastlabel:=nil;
  230. end;
  231. hp1:=pai(hp1^.next);
  232. end;
  233. { :-(, we must generate a new entry }
  234. if not assigned(p^.lab_str) then
  235. begin
  236. getdatalabel(lastlabel);
  237. p^.lab_str:=lastlabel;
  238. if (cs_create_smart in aktmoduleswitches) then
  239. consts^.concat(new(pai_cut,init));
  240. consts^.concat(new(pai_label,init(lastlabel)));
  241. { generate an ansi string ? }
  242. case p^.stringtype of
  243. st_ansistring:
  244. begin
  245. { an empty ansi string is nil! }
  246. if p^.length=0 then
  247. consts^.concat(new(pai_const,init_32bit(0)))
  248. else
  249. begin
  250. getdatalabel(l1);
  251. getdatalabel(l2);
  252. consts^.concat(new(pai_label,init(l2)));
  253. consts^.concat(new(pai_const_symbol,init(l1)));
  254. consts^.concat(new(pai_const,init_32bit(p^.length)));
  255. consts^.concat(new(pai_const,init_32bit(p^.length)));
  256. consts^.concat(new(pai_const,init_32bit(-1)));
  257. consts^.concat(new(pai_label,init(l1)));
  258. getmem(pc,p^.length+2);
  259. move(p^.value_str^,pc^,p^.length);
  260. pc[p^.length]:=#0;
  261. { to overcome this problem we set the length explicitly }
  262. { with the ending null char }
  263. consts^.concat(new(pai_string,init_length_pchar(pc,p^.length+1)));
  264. { return the offset of the real string }
  265. p^.lab_str:=l2;
  266. end;
  267. end;
  268. st_shortstring:
  269. begin
  270. { truncate strings larger than 255 chars }
  271. if p^.length>255 then
  272. l:=255
  273. else
  274. l:=p^.length;
  275. { also length and terminating zero }
  276. getmem(pc,l+3);
  277. move(p^.value_str^,pc[1],l+1);
  278. pc[0]:=chr(l);
  279. { to overcome this problem we set the length explicitly }
  280. { with the ending null char }
  281. pc[l+1]:=#0;
  282. consts^.concat(new(pai_string,init_length_pchar(pc,l+2)));
  283. end;
  284. end;
  285. end;
  286. end;
  287. reset_reference(p^.location.reference);
  288. p^.location.reference.symbol:=p^.lab_str;
  289. p^.location.loc:=LOC_MEM;
  290. end;
  291. {*****************************************************************************
  292. SecondSetCons
  293. *****************************************************************************}
  294. procedure secondsetconst(var p : ptree);
  295. var
  296. hp1 : pai;
  297. lastlabel : pasmlabel;
  298. i : longint;
  299. neededtyp : tait;
  300. begin
  301. { small sets are loaded as constants }
  302. if psetdef(p^.resulttype)^.settype=smallset then
  303. begin
  304. p^.location.loc:=LOC_MEM;
  305. p^.location.reference.is_immediate:=true;
  306. p^.location.reference.offset:=plongint(p^.value_set)^;
  307. exit;
  308. end;
  309. if psetdef(p^.resulttype)^.settype=smallset then
  310. neededtyp:=ait_const_32bit
  311. else
  312. neededtyp:=ait_const_8bit;
  313. lastlabel:=nil;
  314. { const already used ? }
  315. if not assigned(p^.lab_set) then
  316. begin
  317. { tries to found an old entry }
  318. hp1:=pai(consts^.first);
  319. while assigned(hp1) do
  320. begin
  321. if hp1^.typ=ait_label then
  322. lastlabel:=pai_label(hp1)^.l
  323. else
  324. begin
  325. if (lastlabel<>nil) and (hp1^.typ=neededtyp) then
  326. begin
  327. if (hp1^.typ=ait_const_8bit) then
  328. begin
  329. { compare normal set }
  330. i:=0;
  331. while assigned(hp1) and (i<32) do
  332. begin
  333. if pai_const(hp1)^.value<>p^.value_set^[i] then
  334. break;
  335. inc(i);
  336. hp1:=pai(hp1^.next);
  337. end;
  338. if i=32 then
  339. begin
  340. { found! }
  341. p^.lab_set:=lastlabel;
  342. break;
  343. end;
  344. { leave when the end of consts is reached, so no
  345. hp1^.next is done }
  346. if not assigned(hp1) then
  347. break;
  348. end
  349. else
  350. begin
  351. { compare small set }
  352. if plongint(p^.value_set)^=pai_const(hp1)^.value then
  353. begin
  354. { found! }
  355. p^.lab_set:=lastlabel;
  356. break;
  357. end;
  358. end;
  359. end;
  360. lastlabel:=nil;
  361. end;
  362. hp1:=pai(hp1^.next);
  363. end;
  364. { :-(, we must generate a new entry }
  365. if not assigned(p^.lab_set) then
  366. begin
  367. getdatalabel(lastlabel);
  368. p^.lab_set:=lastlabel;
  369. if (cs_create_smart in aktmoduleswitches) then
  370. consts^.concat(new(pai_cut,init));
  371. consts^.concat(new(pai_label,init(lastlabel)));
  372. if psetdef(p^.resulttype)^.settype=smallset then
  373. begin
  374. move(p^.value_set^,i,sizeof(longint));
  375. consts^.concat(new(pai_const,init_32bit(i)));
  376. end
  377. else
  378. begin
  379. for i:=0 to 31 do
  380. consts^.concat(new(pai_const,init_8bit(p^.value_set^[i])));
  381. end;
  382. end;
  383. end;
  384. reset_reference(p^.location.reference);
  385. p^.location.reference.symbol:=p^.lab_set;
  386. p^.location.loc:=LOC_MEM;
  387. end;
  388. {*****************************************************************************
  389. SecondNilN
  390. *****************************************************************************}
  391. procedure secondniln(var p : ptree);
  392. begin
  393. p^.location.loc:=LOC_MEM;
  394. p^.location.reference.is_immediate:=true;
  395. p^.location.reference.offset:=0;
  396. end;
  397. end.
  398. {
  399. $Log$
  400. Revision 1.45 2000-02-09 13:22:46 peter
  401. * log truncated
  402. Revision 1.44 2000/01/07 01:14:20 peter
  403. * updated copyright to 2000
  404. Revision 1.43 1999/11/06 14:34:17 peter
  405. * truncated log to 20 revs
  406. Revision 1.42 1999/09/26 21:30:15 peter
  407. + constant pointer support which can happend with typecasting like
  408. const p=pointer(1)
  409. * better procvar parsing in typed consts
  410. Revision 1.41 1999/09/20 16:38:52 peter
  411. * cs_create_smart instead of cs_smartlink
  412. * -CX is create smartlink
  413. * -CD is create dynamic, but does nothing atm.
  414. Revision 1.40 1999/09/04 20:53:06 florian
  415. * bug 580 fixed
  416. Revision 1.39 1999/08/04 00:22:45 florian
  417. * renamed i386asm and i386base to cpuasm and cpubase
  418. Revision 1.38 1999/08/03 22:02:38 peter
  419. * moved bitmask constants to sets
  420. * some other type/const renamings
  421. }