tcset.pas 8.9 KB

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