tcset.pas 8.1 KB

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