tcset.pas 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. {
  2. $Id$
  3. Copyright (c) 1993-98 by Florian Klaempfl
  4. Type checking and register allocation for set/case nodes
  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 tcset;
  19. interface
  20. uses
  21. tree;
  22. procedure firstsetelement(var p : ptree);
  23. procedure firstin(var p : ptree);
  24. procedure firstrange(var p : ptree);
  25. procedure firstcase(var p : ptree);
  26. implementation
  27. uses
  28. globtype,systems,
  29. cobjects,verbose,globals,
  30. symtable,aasm,types,
  31. hcodegen,htypechk,pass_1
  32. {$ifdef i386}
  33. ,i386,tgeni386
  34. {$endif}
  35. {$ifdef m68k}
  36. ,m68k,tgen68k
  37. {$endif}
  38. ;
  39. {*****************************************************************************
  40. FirstSetElement
  41. *****************************************************************************}
  42. procedure firstsetelement(var p : ptree);
  43. begin
  44. firstpass(p^.left);
  45. if codegenerror then
  46. exit;
  47. if assigned(p^.right) then
  48. begin
  49. firstpass(p^.right);
  50. if codegenerror then
  51. exit;
  52. end;
  53. calcregisters(p,0,0,0);
  54. p^.resulttype:=p^.left^.resulttype;
  55. set_location(p^.location,p^.left^.location);
  56. end;
  57. {*****************************************************************************
  58. FirstIn
  59. *****************************************************************************}
  60. procedure firstin(var p : ptree);
  61. type
  62. byteset = set of byte;
  63. var
  64. t : ptree;
  65. begin
  66. p^.location.loc:=LOC_FLAGS;
  67. p^.resulttype:=booldef;
  68. firstpass(p^.right);
  69. if codegenerror then
  70. exit;
  71. if p^.right^.resulttype^.deftype<>setdef then
  72. CGMessage(sym_e_set_expected);
  73. firstpass(p^.left);
  74. if codegenerror then
  75. exit;
  76. { empty set then return false }
  77. if not assigned(psetdef(p^.right^.resulttype)^.setof) then
  78. begin
  79. t:=genordinalconstnode(0,booldef);
  80. disposetree(p);
  81. firstpass(t);
  82. p:=t;
  83. exit;
  84. end;
  85. { type conversion/check }
  86. p^.left:=gentypeconvnode(p^.left,psetdef(p^.right^.resulttype)^.setof);
  87. firstpass(p^.left);
  88. if codegenerror then
  89. exit;
  90. { constant evaulation }
  91. if (p^.left^.treetype=ordconstn) and (p^.right^.treetype=setconstn) then
  92. begin
  93. t:=genordinalconstnode(byte(p^.left^.value in byteset(p^.right^.value_set^)),booldef);
  94. disposetree(p);
  95. firstpass(t);
  96. p:=t;
  97. exit;
  98. end;
  99. left_right_max(p);
  100. { this is not allways true due to optimization }
  101. { but if we don't set this we get problems with optimizing self code }
  102. if psetdef(p^.right^.resulttype)^.settype<>smallset then
  103. procinfo.flags:=procinfo.flags or pi_do_call
  104. else
  105. begin
  106. { a smallset needs maybe an misc. register }
  107. if (p^.left^.treetype<>ordconstn) and
  108. not(p^.right^.location.loc in [LOC_CREGISTER,LOC_REGISTER]) and
  109. (p^.right^.registers32<1) then
  110. inc(p^.registers32);
  111. end;
  112. end;
  113. {*****************************************************************************
  114. FirstRange
  115. *****************************************************************************}
  116. procedure firstrange(var p : ptree);
  117. var
  118. ct : tconverttype;
  119. begin
  120. firstpass(p^.left);
  121. firstpass(p^.right);
  122. if codegenerror then
  123. exit;
  124. { both types must be compatible }
  125. if not(is_equal(p^.left^.resulttype,p^.right^.resulttype)) and
  126. not(isconvertable(p^.left^.resulttype,p^.right^.resulttype,ct,ordconstn,false)) then
  127. CGMessage(type_e_mismatch);
  128. { Check if only when its a constant set }
  129. if (p^.left^.treetype=ordconstn) and (p^.right^.treetype=ordconstn) then
  130. begin
  131. { upper limit must be greater or equal than lower limit }
  132. { not if u32bit }
  133. if (p^.left^.value>p^.right^.value) and
  134. (( p^.left^.value<0) or (p^.right^.value>=0)) then
  135. CGMessage(cg_e_upper_lower_than_lower);
  136. end;
  137. left_right_max(p);
  138. p^.resulttype:=p^.left^.resulttype;
  139. set_location(p^.location,p^.left^.location);
  140. end;
  141. {*****************************************************************************
  142. FirstCase
  143. *****************************************************************************}
  144. procedure firstcase(var p : ptree);
  145. var
  146. old_t_times : longint;
  147. hp : ptree;
  148. begin
  149. { evalutes the case expression }
  150. cleartempgen;
  151. must_be_valid:=true;
  152. firstpass(p^.left);
  153. if codegenerror then
  154. exit;
  155. p^.registers32:=p^.left^.registers32;
  156. p^.registersfpu:=p^.left^.registersfpu;
  157. {$ifdef SUPPORT_MMX}
  158. p^.registersmmx:=p^.left^.registersmmx;
  159. {$endif SUPPORT_MMX}
  160. { walk through all instructions }
  161. { estimates the repeat of each instruction }
  162. old_t_times:=t_times;
  163. if not(cs_littlesize in aktglobalswitches) then
  164. begin
  165. t_times:=t_times div case_count_labels(p^.nodes);
  166. if t_times<1 then
  167. t_times:=1;
  168. end;
  169. { first case }
  170. hp:=p^.right;
  171. while assigned(hp) do
  172. begin
  173. cleartempgen;
  174. firstpass(hp^.right);
  175. { searchs max registers }
  176. if hp^.right^.registers32>p^.registers32 then
  177. p^.registers32:=hp^.right^.registers32;
  178. if hp^.right^.registersfpu>p^.registersfpu then
  179. p^.registersfpu:=hp^.right^.registersfpu;
  180. {$ifdef SUPPORT_MMX}
  181. if hp^.right^.registersmmx>p^.registersmmx then
  182. p^.registersmmx:=hp^.right^.registersmmx;
  183. {$endif SUPPORT_MMX}
  184. hp:=hp^.left;
  185. end;
  186. { may be handle else tree }
  187. if assigned(p^.elseblock) then
  188. begin
  189. cleartempgen;
  190. firstpass(p^.elseblock);
  191. if codegenerror then
  192. exit;
  193. if p^.registers32<p^.elseblock^.registers32 then
  194. p^.registers32:=p^.elseblock^.registers32;
  195. if p^.registersfpu<p^.elseblock^.registersfpu then
  196. p^.registersfpu:=p^.elseblock^.registersfpu;
  197. {$ifdef SUPPORT_MMX}
  198. if p^.registersmmx<p^.elseblock^.registersmmx then
  199. p^.registersmmx:=p^.elseblock^.registersmmx;
  200. {$endif SUPPORT_MMX}
  201. end;
  202. t_times:=old_t_times;
  203. { there is one register required for the case expression }
  204. if p^.registers32<1 then p^.registers32:=1;
  205. end;
  206. end.
  207. {
  208. $Log$
  209. Revision 1.5 1998-12-18 17:15:40 peter
  210. * added 'in []' support
  211. Revision 1.4 1998/12/11 00:03:58 peter
  212. + globtype,tokens,version unit splitted from globals
  213. Revision 1.3 1998/11/13 10:17:06 peter
  214. + constant eval for in
  215. Revision 1.2 1998/10/06 20:49:13 peter
  216. * m68k compiler compiles again
  217. Revision 1.1 1998/09/23 20:42:24 peter
  218. * splitted pass_1
  219. }