cg386set.pas 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  1. {
  2. $Id$
  3. Copyright (c) 1993-98 by Florian Klaempfl
  4. Generate i386 assembler for in 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 cg386set;
  19. interface
  20. uses
  21. tree;
  22. procedure secondrange(var p : ptree);
  23. procedure secondsetele(var p : ptree);
  24. procedure secondin(var p : ptree);
  25. procedure secondcase(var p : ptree);
  26. implementation
  27. uses
  28. cobjects,verbose,globals,systems,
  29. symtable,aasm,i386,types,
  30. cgi386,cgai386,tgeni386,temp_gen,hcodegen;
  31. const
  32. bytes2Sxx:array[1..4] of Topsize=(S_B,S_W,S_NO,S_L);
  33. {*****************************************************************************
  34. SecondRange
  35. *****************************************************************************}
  36. procedure secondrange(var p : ptree);
  37. begin
  38. secondpass(p^.left);
  39. secondpass(p^.right);
  40. { we doesn't modifiy the left side, we check only the type }
  41. set_location(p^.location,p^.left^.location);
  42. end;
  43. {*****************************************************************************
  44. SecondSetEle
  45. *****************************************************************************}
  46. procedure secondsetele(var p : ptree);
  47. begin
  48. secondpass(p^.left);
  49. { we doesn't modifiy the left side, we check only the type }
  50. set_location(p^.location,p^.left^.location);
  51. { return always in 32bit becuase set operations are done with
  52. S_L opsize }
  53. if p^.location.loc in [LOC_REGISTER,LOC_CREGISTER] then
  54. emit_to_reg32(p^.location.register);
  55. end;
  56. {*****************************************************************************
  57. SecondIn
  58. *****************************************************************************}
  59. procedure secondin(var p : ptree);
  60. type
  61. Tsetpart=record
  62. range : boolean; {Part is a range.}
  63. start,stop : byte; {Start/stop when range; Stop=element when an element.}
  64. end;
  65. var
  66. use_small,
  67. pushed,
  68. ranges : boolean;
  69. hr,hr2,
  70. pleftreg : tregister;
  71. opsize : topsize;
  72. setparts : array[1..8] of Tsetpart;
  73. i,numparts : byte;
  74. href,href2 : Treference;
  75. l,l2 : plabel;
  76. function analizeset(Aset:Pconstset;is_small:boolean):boolean;
  77. type
  78. byteset=set of byte;
  79. var
  80. compares,maxcompares:word;
  81. i:byte;
  82. begin
  83. analizeset:=false;
  84. ranges:=false;
  85. numparts:=0;
  86. compares:=0;
  87. { Lots of comparisions take a lot of time, so do not allow
  88. too much comparisions. 8 comparisions are, however, still
  89. smalller than emitting the set }
  90. if cs_littlesize in aktglobalswitches then
  91. maxcompares:=8
  92. else
  93. maxcompares:=5;
  94. { when smallset is possible allow only 3 compares the smallset
  95. code is for littlesize also smaller when more compares are used }
  96. if is_small then
  97. maxcompares:=3;
  98. for i:=0 to 255 do
  99. if i in byteset(Aset^) then
  100. begin
  101. if (numparts=0) or (i<>setparts[numparts].stop+1) then
  102. begin
  103. {Set element is a separate element.}
  104. inc(compares);
  105. if compares>maxcompares then
  106. exit;
  107. inc(numparts);
  108. setparts[numparts].range:=false;
  109. setparts[numparts].stop:=i;
  110. end
  111. else
  112. {Set element is part of a range.}
  113. if not setparts[numparts].range then
  114. begin
  115. {Transform an element into a range.}
  116. setparts[numparts].range:=true;
  117. setparts[numparts].start:=setparts[numparts].stop;
  118. setparts[numparts].stop:=i;
  119. inc(compares);
  120. if compares>maxcompares then
  121. exit;
  122. end
  123. else
  124. begin
  125. {Extend a range.}
  126. setparts[numparts].stop:=i;
  127. {A range of two elements can better
  128. be checked as two separate ones.
  129. When extending a range, our range
  130. becomes larger than two elements.}
  131. ranges:=true;
  132. end;
  133. end;
  134. analizeset:=true;
  135. end;
  136. begin
  137. { calculate both operators }
  138. { the complex one first }
  139. firstcomplex(p);
  140. secondpass(p^.left);
  141. { are too few registers free? }
  142. pushed:=maybe_push(p^.right^.registers32,p^.left);
  143. secondpass(p^.right);
  144. if pushed then
  145. restore(p^.left);
  146. if codegenerror then
  147. exit;
  148. { ofcourse not commutative }
  149. if p^.swaped then
  150. swaptree(p);
  151. { check if we can use smallset operation using btl which is limited
  152. to 32 bits, the left side may also not contain higher values !! }
  153. use_small:=(psetdef(p^.right^.resulttype)^.settype=smallset) and
  154. ((p^.left^.resulttype^.deftype=orddef) and (porddef(p^.left^.resulttype)^.high<=32) or
  155. (p^.left^.resulttype^.deftype=enumdef) and (penumdef(p^.left^.resulttype)^.max<=32));
  156. { Can we generate jumps? Possible for all types of sets }
  157. if (p^.right^.treetype=setconstrn) and
  158. analizeset(p^.right^.constset,use_small) then
  159. begin
  160. { It gives us advantage to check for the set elements
  161. separately instead of using the SET_IN_BYTE procedure.
  162. To do: Build in support for LOC_JUMP }
  163. { If register is used, use only lower 8 bits }
  164. if p^.left^.location.loc in [LOC_REGISTER,LOC_CREGISTER] then
  165. begin
  166. pleftreg:=p^.left^.location.register;
  167. if pleftreg in [R_AX..R_DX] then
  168. begin
  169. exprasmlist^.concat(new(pai386,op_const_reg(A_AND,S_W,255,pleftreg)));
  170. opsize:=S_W;
  171. end
  172. else
  173. if pleftreg in [R_EAX..R_EDI] then
  174. begin
  175. exprasmlist^.concat(new(pai386,op_const_reg(A_AND,S_L,255,pleftreg)));
  176. opsize:=S_L;
  177. end
  178. else
  179. opsize:=S_B;
  180. end;
  181. { Get a label to jump to the end }
  182. p^.location.loc:=LOC_FLAGS;
  183. { It's better to use the zero flag when there are
  184. no ranges }
  185. if ranges then
  186. p^.location.resflags:=F_C
  187. else
  188. p^.location.resflags:=F_E;
  189. reset_reference(href);
  190. getlabel(l);
  191. href.symbol:=stringdup(lab2str(l));
  192. for i:=1 to numparts do
  193. if setparts[i].range then
  194. begin
  195. { Check if left is in a range }
  196. { Get a label to jump over the check }
  197. reset_reference(href2);
  198. getlabel(l2);
  199. href.symbol:=stringdup(lab2str(l2));
  200. if setparts[i].start=setparts[i].stop-1 then
  201. begin
  202. case p^.left^.location.loc of
  203. LOC_REGISTER,
  204. LOC_CREGISTER : exprasmlist^.concat(new(pai386,op_const_reg(A_CMP,opsize,
  205. setparts[i].start,pleftreg)));
  206. else
  207. exprasmlist^.concat(new(pai386,op_const_ref(A_CMP,S_B,
  208. setparts[i].start,newreference(p^.left^.location.reference))));
  209. end;
  210. { Result should be in carry flag when ranges are used }
  211. if ranges then
  212. exprasmlist^.concat(new(pai386,op_none(A_STC,S_NO)));
  213. { If found, jump to end }
  214. emitl(A_JE,l);
  215. case p^.left^.location.loc of
  216. LOC_REGISTER,
  217. LOC_CREGISTER : exprasmlist^.concat(new(pai386,op_const_reg(A_CMP,opsize,
  218. setparts[i].stop,pleftreg)));
  219. else
  220. exprasmlist^.concat(new(pai386,op_const_ref(A_CMP,S_B,
  221. setparts[i].stop,newreference(p^.left^.location.reference))));
  222. end;
  223. { Result should be in carry flag when ranges are used }
  224. if ranges then
  225. exprasmlist^.concat(new(pai386,op_none(A_STC,S_NO)));
  226. { If found, jump to end }
  227. emitl(A_JE,l);
  228. end
  229. else
  230. begin
  231. if setparts[i].start<>0 then
  232. begin
  233. { We only check for the lower bound if it is > 0, because
  234. set elements lower than 0 dont exist }
  235. case p^.left^.location.loc of
  236. LOC_REGISTER,
  237. LOC_CREGISTER : exprasmlist^.concat(new(pai386,op_const_reg(A_CMP,opsize,
  238. setparts[i].start,pleftreg)));
  239. else
  240. exprasmlist^.concat(new(pai386,op_const_ref(A_CMP,S_B,
  241. setparts[i].start,newreference(p^.left^.location.reference))));
  242. end;
  243. { If lower, jump to next check }
  244. emitl(A_JB,l2);
  245. end;
  246. { We only check for the high bound if it is < 255, because
  247. set elements higher than 255 do nt exist, the its always true,
  248. so only a JMP is generated }
  249. if setparts[i].stop<>255 then
  250. begin
  251. case p^.left^.location.loc of
  252. LOC_REGISTER,
  253. LOC_CREGISTER : exprasmlist^.concat(new(pai386,op_const_reg(A_CMP,opsize,
  254. setparts[i].stop+1,pleftreg)));
  255. else
  256. exprasmlist^.concat(new(pai386,op_const_ref(A_CMP,S_B,
  257. setparts[i].stop+1,newreference(p^.left^.location.reference))));
  258. end;
  259. { If higher, element is in set }
  260. emitl(A_JB,l);
  261. end
  262. else
  263. begin
  264. exprasmlist^.concat(new(pai386,op_none(A_STC,S_NO)));
  265. emitl(A_JMP,l);
  266. end;
  267. end;
  268. { Emit the jump over label }
  269. exprasmlist^.concat(new(pai_label,init(l2)));
  270. end
  271. else
  272. begin
  273. { Emit code to check if left is an element }
  274. case p^.left^.location.loc of
  275. LOC_REGISTER,
  276. LOC_CREGISTER : exprasmlist^.concat(new(pai386,op_const_reg(A_CMP,opsize,
  277. setparts[i].stop,pleftreg)));
  278. else
  279. exprasmlist^.concat(new(pai386,op_const_ref(A_CMP,S_B,
  280. setparts[i].stop,newreference(p^.left^.location.reference))));
  281. end;
  282. { Result should be in carry flag when ranges are used }
  283. if ranges then
  284. exprasmlist^.concat(new(pai386,op_none(A_STC,S_NO)));
  285. { If found, jump to end }
  286. emitl(A_JE,l);
  287. end;
  288. if ranges then
  289. exprasmlist^.concat(new(pai386,op_none(A_CLC,S_NO)));
  290. { To compensate for not doing a second pass }
  291. stringdispose(p^.right^.location.reference.symbol);
  292. { Now place the end label }
  293. exprasmlist^.concat(new(pai_label,init(l)));
  294. case p^.left^.location.loc of
  295. LOC_REGISTER,
  296. LOC_CREGISTER : ungetregister32(pleftreg);
  297. else
  298. del_reference(p^.left^.location.reference);
  299. end;
  300. end
  301. else
  302. begin
  303. { We will now generated code to check the set itself, no jmps,
  304. handle smallsets separate, because it allows faster checks }
  305. if use_small then
  306. begin
  307. if p^.left^.treetype=ordconstn then
  308. begin
  309. p^.location.resflags:=F_NE;
  310. case p^.right^.location.loc of
  311. LOC_REGISTER,
  312. LOC_CREGISTER : begin
  313. exprasmlist^.concat(new(pai386,op_const_reg(A_TEST,S_L,
  314. 1 shl (p^.left^.value and 31),p^.right^.location.register)));
  315. ungetregister32(p^.right^.location.register);
  316. end
  317. else
  318. begin
  319. exprasmlist^.concat(new(pai386,op_const_ref(A_TEST,S_L,1 shl (p^.left^.value and 31),
  320. newreference(p^.right^.location.reference))));
  321. del_reference(p^.right^.location.reference);
  322. end;
  323. end;
  324. end
  325. else
  326. begin
  327. case p^.left^.location.loc of
  328. LOC_REGISTER,
  329. LOC_CREGISTER : begin
  330. hr:=p^.left^.location.register;
  331. emit_to_reg32(hr);
  332. end;
  333. else
  334. begin
  335. { the set element isn't never samller than a byte }
  336. { and because it's a small set we need only 5 bits }
  337. { but 8 bits are easier to load }
  338. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOVZX,S_BL,
  339. newreference(p^.left^.location.reference),R_EDI)));
  340. hr:=R_EDI;
  341. del_reference(p^.left^.location.reference);
  342. end;
  343. end;
  344. case p^.right^.location.loc of
  345. LOC_REGISTER,
  346. LOC_CREGISTER : exprasmlist^.concat(new(pai386,op_reg_reg(A_BT,S_L,hr,
  347. p^.right^.location.register)));
  348. else
  349. begin
  350. if p^.right^.location.reference.isintvalue then
  351. begin
  352. { We have to load the value into a register because
  353. btl does not accept values only refs or regs (PFV) }
  354. hr2:=getregister32;
  355. exprasmlist^.concat(new(pai386,op_const_reg(A_MOV,S_L,
  356. p^.right^.location.reference.offset,hr2)));
  357. exprasmlist^.concat(new(pai386,op_reg_reg(A_BT,S_L,hr,hr2)));
  358. ungetregister32(hr2);
  359. end
  360. else
  361. exprasmlist^.concat(new(pai386,op_reg_ref(A_BT,S_L,hr,
  362. newreference(p^.right^.location.reference))));
  363. del_reference(p^.right^.location.reference);
  364. end;
  365. end;
  366. ungetregister32(hr);
  367. p^.location.loc:=LOC_FLAGS;
  368. p^.location.resflags:=F_C;
  369. end;
  370. end
  371. else
  372. begin
  373. { do search in a normal set which could have >32 elementsm
  374. but also used if the left side contains higher values > 32 }
  375. if p^.left^.treetype=ordconstn then
  376. begin
  377. p^.location.resflags:=F_NE;
  378. inc(p^.right^.location.reference.offset,p^.left^.value shr 3);
  379. exprasmlist^.concat(new(pai386,op_const_ref(A_TEST,S_B,1 shl (p^.left^.value and 7),
  380. newreference(p^.right^.location.reference))));
  381. del_reference(p^.right^.location.reference);
  382. end
  383. else
  384. begin
  385. pushsetelement(p^.left);
  386. emitpushreferenceaddr(exprasmlist,p^.right^.location.reference);
  387. del_reference(p^.right^.location.reference);
  388. { registers need not be save. that happens in SET_IN_BYTE }
  389. { (EDI is changed) }
  390. emitcall('SET_IN_BYTE',true);
  391. { ungetiftemp(p^.right^.location.reference); }
  392. p^.location.loc:=LOC_FLAGS;
  393. p^.location.resflags:=F_C;
  394. end;
  395. end;
  396. end;
  397. end;
  398. {*****************************************************************************
  399. SecondCase
  400. *****************************************************************************}
  401. procedure secondcase(var p : ptree);
  402. var
  403. with_sign : boolean;
  404. opsize : topsize;
  405. jmp_gt,jmp_le,jmp_lee : tasmop;
  406. hp : ptree;
  407. { register with case expression }
  408. hregister : tregister;
  409. endlabel,elselabel : plabel;
  410. { true, if we can omit the range check of the jump table }
  411. jumptable_no_range : boolean;
  412. { where to put the jump table }
  413. jumpsegment : paasmoutput;
  414. procedure gentreejmp(p : pcaserecord);
  415. var
  416. lesslabel,greaterlabel : plabel;
  417. begin
  418. emitl(A_LABEL,p^._at);
  419. { calculate labels for left and right }
  420. if (p^.less=nil) then
  421. lesslabel:=elselabel
  422. else
  423. lesslabel:=p^.less^._at;
  424. if (p^.greater=nil) then
  425. greaterlabel:=elselabel
  426. else
  427. greaterlabel:=p^.greater^._at;
  428. { calculate labels for left and right }
  429. { no range label: }
  430. if p^._low=p^._high then
  431. begin
  432. exprasmlist^.concat(new(pai386,op_const_reg(A_CMP,opsize,p^._low,hregister)));
  433. if greaterlabel=lesslabel then
  434. begin
  435. emitl(A_JNE,lesslabel);
  436. end
  437. else
  438. begin
  439. emitl(jmp_le,lesslabel);
  440. emitl(jmp_gt,greaterlabel);
  441. end;
  442. emitl(A_JMP,p^.statement);
  443. end
  444. else
  445. begin
  446. exprasmlist^.concat(new(pai386,op_const_reg(A_CMP,opsize,p^._low,hregister)));
  447. emitl(jmp_le,lesslabel);
  448. exprasmlist^.concat(new(pai386,op_const_reg(A_CMP,opsize,p^._high,hregister)));
  449. emitl(jmp_gt,greaterlabel);
  450. emitl(A_JMP,p^.statement);
  451. end;
  452. if assigned(p^.less) then
  453. gentreejmp(p^.less);
  454. if assigned(p^.greater) then
  455. gentreejmp(p^.greater);
  456. end;
  457. procedure genlinearlist(hp : pcaserecord);
  458. var
  459. first : boolean;
  460. last : longint;
  461. {helplabel : longint;}
  462. procedure genitem(t : pcaserecord);
  463. begin
  464. if assigned(t^.less) then
  465. genitem(t^.less);
  466. if t^._low=t^._high then
  467. begin
  468. if t^._low-last=1 then
  469. exprasmlist^.concat(new(pai386,op_reg(A_DEC,opsize,hregister)))
  470. else if t^._low-last=0 then
  471. exprasmlist^.concat(new(pai386,op_reg_reg(A_OR,opsize,hregister,hregister)))
  472. else
  473. exprasmlist^.concat(new(pai386,op_const_reg(A_SUB,opsize,t^._low-last,hregister)));
  474. last:=t^._low;
  475. emitl(A_JZ,t^.statement);
  476. end
  477. else
  478. begin
  479. { it begins with the smallest label, if the value }
  480. { is even smaller then jump immediately to the }
  481. { ELSE-label }
  482. if first then
  483. begin
  484. if t^._low-1=1 then
  485. exprasmlist^.concat(new(pai386,op_reg(A_DEC,opsize,
  486. hregister)))
  487. else if t^._low-1=0 then
  488. exprasmlist^.concat(new(pai386,op_reg_reg(A_OR,opsize,
  489. hregister,hregister)))
  490. else
  491. exprasmlist^.concat(new(pai386,op_const_reg(A_SUB,opsize,
  492. t^._low-1,hregister)));
  493. { work around: if the lower range=0 and we
  494. do the subtraction we have to take care
  495. of the sign!
  496. }
  497. if t^._low=0 then
  498. emitl(A_JLE,elselabel)
  499. else
  500. emitl(jmp_lee,elselabel);
  501. end
  502. { if there is no unused label between the last and the }
  503. { present label then the lower limit can be checked }
  504. { immediately. else check the range in between: }
  505. else if (t^._low-last>1)then
  506. begin
  507. if t^._low-last-1=1 then
  508. exprasmlist^.concat(new(pai386,op_reg(A_DEC,opsize,hregister)))
  509. else
  510. exprasmlist^.concat(new(pai386,op_const_reg(A_SUB,opsize,t^._low-last-1,hregister)));
  511. emitl(jmp_lee,elselabel);
  512. end;
  513. exprasmlist^.concat(new(pai386,op_const_reg(A_SUB,opsize,t^._high-t^._low+1,hregister)));
  514. emitl(jmp_lee,t^.statement);
  515. last:=t^._high;
  516. end;
  517. first:=false;
  518. if assigned(t^.greater) then
  519. genitem(t^.greater);
  520. end;
  521. var
  522. hr : tregister;
  523. begin
  524. { case register is modified by the list evalution }
  525. if (p^.left^.location.loc=LOC_CREGISTER) then
  526. begin
  527. hr:=getregister32;
  528. case opsize of
  529. S_B : hregister:=reg32toreg8(hr);
  530. S_W : hregister:=reg32toreg16(hr);
  531. S_L : hregister:=hr;
  532. end;
  533. end;
  534. last:=0;
  535. first:=true;
  536. genitem(hp);
  537. emitl(A_JMP,elselabel);
  538. end;
  539. procedure genjumptable(hp : pcaserecord;min_,max_ : longint);
  540. var
  541. table : plabel;
  542. last : longint;
  543. hr : preference;
  544. procedure genitem(t : pcaserecord);
  545. var
  546. i : longint;
  547. begin
  548. if assigned(t^.less) then
  549. genitem(t^.less);
  550. { fill possible hole }
  551. for i:=last+1 to t^._low-1 do
  552. jumpsegment^.concat(new(pai_const,init_symbol(strpnew(lab2str
  553. (elselabel)))));
  554. for i:=t^._low to t^._high do
  555. jumpsegment^.concat(new(pai_const,init_symbol(strpnew(lab2str
  556. (t^.statement)))));
  557. last:=t^._high;
  558. if assigned(t^.greater) then
  559. genitem(t^.greater);
  560. end;
  561. begin
  562. if not(jumptable_no_range) then
  563. begin
  564. exprasmlist^.concat(new(pai386,op_const_reg(A_CMP,opsize,min_,hregister)));
  565. { case expr less than min_ => goto elselabel }
  566. emitl(jmp_le,elselabel);
  567. exprasmlist^.concat(new(pai386,op_const_reg(A_CMP,opsize,max_,hregister)));
  568. emitl(jmp_gt,elselabel);
  569. end;
  570. getlabel(table);
  571. { extend with sign }
  572. if opsize=S_W then
  573. begin
  574. exprasmlist^.concat(new(pai386,op_reg_reg(A_MOVZX,S_WL,hregister,
  575. reg16toreg32(hregister))));
  576. hregister:=reg16toreg32(hregister);
  577. end
  578. else if opsize=S_B then
  579. begin
  580. exprasmlist^.concat(new(pai386,op_reg_reg(A_MOVZX,S_BL,hregister,
  581. reg8toreg32(hregister))));
  582. hregister:=reg8toreg32(hregister);
  583. end;
  584. new(hr);
  585. reset_reference(hr^);
  586. hr^.symbol:=stringdup(lab2str(table));
  587. hr^.offset:=(-min_)*4;
  588. hr^.index:=hregister;
  589. hr^.scalefactor:=4;
  590. exprasmlist^.concat(new(pai386,op_ref(A_JMP,S_NO,hr)));
  591. { !!!!! generate tables
  592. if not(cs_littlesize in aktlocalswitches) then
  593. jumpsegment^.concat(new(pai386,op_const(A_ALIGN,S_NO,4)));
  594. }
  595. jumpsegment^.concat(new(pai_label,init(table)));
  596. last:=min_;
  597. genitem(hp);
  598. { !!!!!!!
  599. if not(cs_littlesize in aktlocalswitches) then
  600. exprasmlist^.concat(new(pai386,op_const(A_ALIGN,S_NO,4)));
  601. }
  602. end;
  603. var
  604. lv,hv,min_label,max_label,labels : longint;
  605. max_linear_list : longint;
  606. begin
  607. getlabel(endlabel);
  608. getlabel(elselabel);
  609. if (cs_smartlink in aktmoduleswitches) then
  610. jumpsegment:=procinfo.aktlocaldata
  611. else
  612. jumpsegment:=datasegment;
  613. with_sign:=is_signed(p^.left^.resulttype);
  614. if with_sign then
  615. begin
  616. jmp_gt:=A_JG;
  617. jmp_le:=A_JL;
  618. jmp_lee:=A_JLE;
  619. end
  620. else
  621. begin
  622. jmp_gt:=A_JA;
  623. jmp_le:=A_JB;
  624. jmp_lee:=A_JBE;
  625. end;
  626. cleartempgen;
  627. secondpass(p^.left);
  628. { determines the size of the operand }
  629. opsize:=bytes2Sxx[p^.left^.resulttype^.size];
  630. { copy the case expression to a register }
  631. case p^.left^.location.loc of
  632. LOC_REGISTER,
  633. LOC_CREGISTER:
  634. hregister:=p^.left^.location.register;
  635. LOC_MEM,LOC_REFERENCE : begin
  636. del_reference(p^.left^.location.reference);
  637. hregister:=getregister32;
  638. case opsize of
  639. S_B : hregister:=reg32toreg8(hregister);
  640. S_W : hregister:=reg32toreg16(hregister);
  641. end;
  642. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,opsize,newreference(
  643. p^.left^.location.reference),hregister)));
  644. end;
  645. else internalerror(2002);
  646. end;
  647. { now generate the jumps }
  648. if cs_optimize in aktglobalswitches then
  649. begin
  650. { procedures are empirically passed on }
  651. { consumption can also be calculated }
  652. { but does it pay on the different }
  653. { processors? }
  654. { moreover can the size only be appro- }
  655. { ximated as it is not known if rel8, }
  656. { rel16 or rel32 jumps are used }
  657. min_label:=case_get_min(p^.nodes);
  658. max_label:=case_get_max(p^.nodes);
  659. labels:=case_count_labels(p^.nodes);
  660. { can we omit the range check of the jump table }
  661. getrange(p^.left^.resulttype,lv,hv);
  662. jumptable_no_range:=(lv=min_label) and (hv=max_label);
  663. { optimize for size ? }
  664. if cs_littlesize in aktglobalswitches then
  665. begin
  666. if (labels<=2) or ((max_label-min_label)>3*labels) then
  667. { a linear list is always smaller than a jump tree }
  668. genlinearlist(p^.nodes)
  669. else
  670. { if the labels less or more a continuum then }
  671. genjumptable(p^.nodes,min_label,max_label);
  672. end
  673. else
  674. begin
  675. if jumptable_no_range then
  676. max_linear_list:=4
  677. else
  678. max_linear_list:=2;
  679. { a jump table crashes the pipeline! }
  680. if aktoptprocessor=int486 then
  681. inc(max_linear_list,3);
  682. if aktoptprocessor=pentium then
  683. inc(max_linear_list,6);
  684. if aktoptprocessor>=pentiumpro then
  685. inc(max_linear_list,9);
  686. if (labels<=max_linear_list) then
  687. genlinearlist(p^.nodes)
  688. else
  689. begin
  690. if ((max_label-min_label)>4*labels) then
  691. begin
  692. if labels>16 then
  693. gentreejmp(p^.nodes)
  694. else
  695. genlinearlist(p^.nodes);
  696. end
  697. else
  698. genjumptable(p^.nodes,min_label,max_label);
  699. end;
  700. end;
  701. end
  702. else
  703. { it's always not bad }
  704. genlinearlist(p^.nodes);
  705. { now generate the instructions }
  706. hp:=p^.right;
  707. while assigned(hp) do
  708. begin
  709. cleartempgen;
  710. secondpass(hp^.right);
  711. emitl(A_JMP,endlabel);
  712. hp:=hp^.left;
  713. end;
  714. emitl(A_LABEL,elselabel);
  715. { ...and the else block }
  716. if assigned(p^.elseblock) then
  717. begin
  718. cleartempgen;
  719. secondpass(p^.elseblock);
  720. end;
  721. emitl(A_LABEL,endlabel);
  722. end;
  723. end.
  724. {
  725. $Log$
  726. Revision 1.6 1998-08-18 09:24:39 pierre
  727. * small warning position bug fixed
  728. * support_mmx switches splitting was missing
  729. * rhide error and warning output corrected
  730. Revision 1.5 1998/08/14 18:18:40 peter
  731. + dynamic set contruction
  732. * smallsets are now working (always longint size)
  733. Revision 1.4 1998/08/10 14:49:51 peter
  734. + localswitches, moduleswitches, globalswitches splitting
  735. Revision 1.3 1998/06/25 08:48:10 florian
  736. * first version of rtti support
  737. Revision 1.2 1998/06/16 08:56:18 peter
  738. + targetcpu
  739. * cleaner pmodules for newppu
  740. Revision 1.1 1998/06/05 17:44:13 peter
  741. * splitted cgi386
  742. }