cg386set.pas 33 KB

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