cg386set.pas 31 KB

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