cg386set.pas 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 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. globtype,systems,
  28. cobjects,verbose,globals,
  29. symconst,symtable,aasm,types,
  30. hcodegen,temp_gen,pass_2,
  31. cpubase,cpuasm,
  32. cgai386,tgeni386;
  33. const
  34. bytes2Sxx:array[1..4] of Topsize=(S_B,S_W,S_NO,S_L);
  35. {*****************************************************************************
  36. SecondSetElement
  37. *****************************************************************************}
  38. procedure secondsetelement(var p : ptree);
  39. begin
  40. { load first value in 32bit register }
  41. secondpass(p^.left);
  42. if p^.left^.location.loc in [LOC_REGISTER,LOC_CREGISTER] then
  43. emit_to_reg32(p^.left^.location.register);
  44. { also a second value ? }
  45. if assigned(p^.right) then
  46. begin
  47. secondpass(p^.right);
  48. if p^.right^.location.loc in [LOC_REGISTER,LOC_CREGISTER] then
  49. emit_to_reg32(p^.right^.location.register);
  50. end;
  51. { we doesn't modify the left side, we check only the type }
  52. set_location(p^.location,p^.left^.location);
  53. end;
  54. {*****************************************************************************
  55. SecondIn
  56. *****************************************************************************}
  57. procedure secondin(var p : ptree);
  58. type
  59. Tsetpart=record
  60. range : boolean; {Part is a range.}
  61. start,stop : byte; {Start/stop when range; Stop=element when an element.}
  62. end;
  63. var
  64. genjumps,
  65. use_small,
  66. pushed,
  67. ranges : boolean;
  68. hr,hr2,
  69. pleftreg : tregister;
  70. opsize : topsize;
  71. setparts : array[1..8] of Tsetpart;
  72. i,numparts : byte;
  73. {href,href2 : Treference;}
  74. l,l2 : pasmlabel;
  75. {$ifdef CORRECT_SET_IN_FPC}
  76. AM : tasmop;
  77. {$endif CORRECT_SET_IN_FPC}
  78. function analizeset(Aset:pconstset;is_small:boolean):boolean;
  79. type
  80. byteset=set of byte;
  81. var
  82. compares,maxcompares:word;
  83. i:byte;
  84. begin
  85. analizeset:=false;
  86. ranges:=false;
  87. numparts:=0;
  88. compares:=0;
  89. { Lots of comparisions take a lot of time, so do not allow
  90. too much comparisions. 8 comparisions are, however, still
  91. smalller than emitting the set }
  92. if cs_littlesize in aktglobalswitches then
  93. maxcompares:=8
  94. else
  95. maxcompares:=5;
  96. { when smallset is possible allow only 3 compares the smallset
  97. code is for littlesize also smaller when more compares are used }
  98. if is_small then
  99. maxcompares:=3;
  100. for i:=0 to 255 do
  101. if i in byteset(Aset^) then
  102. begin
  103. if (numparts=0) or (i<>setparts[numparts].stop+1) then
  104. begin
  105. {Set element is a separate element.}
  106. inc(compares);
  107. if compares>maxcompares then
  108. exit;
  109. inc(numparts);
  110. setparts[numparts].range:=false;
  111. setparts[numparts].stop:=i;
  112. end
  113. else
  114. {Set element is part of a range.}
  115. if not setparts[numparts].range then
  116. begin
  117. {Transform an element into a range.}
  118. setparts[numparts].range:=true;
  119. setparts[numparts].start:=setparts[numparts].stop;
  120. setparts[numparts].stop:=i;
  121. inc(compares);
  122. if compares>maxcompares then
  123. exit;
  124. end
  125. else
  126. begin
  127. {Extend a range.}
  128. setparts[numparts].stop:=i;
  129. {A range of two elements can better
  130. be checked as two separate ones.
  131. When extending a range, our range
  132. becomes larger than two elements.}
  133. ranges:=true;
  134. end;
  135. end;
  136. analizeset:=true;
  137. end;
  138. begin
  139. { We check first if we can generate jumps, this can be done
  140. because the resulttype is already set in firstpass }
  141. { check if we can use smallset operation using btl which is limited
  142. to 32 bits, the left side may also not contain higher values !! }
  143. use_small:=(psetdef(p^.right^.resulttype)^.settype=smallset) and
  144. ((p^.left^.resulttype^.deftype=orddef) and (porddef(p^.left^.resulttype)^.high<=32) or
  145. (p^.left^.resulttype^.deftype=enumdef) and (penumdef(p^.left^.resulttype)^.max<=32));
  146. { Can we generate jumps? Possible for all types of sets }
  147. genjumps:=(p^.right^.treetype=setconstn) and
  148. analizeset(p^.right^.value_set,use_small);
  149. { calculate both operators }
  150. { the complex one first }
  151. firstcomplex(p);
  152. secondpass(p^.left);
  153. { Only process the right if we are not generating jumps }
  154. if not genjumps then
  155. begin
  156. pushed:=maybe_push(p^.right^.registers32,p^.left,false);
  157. secondpass(p^.right);
  158. if pushed then
  159. restore(p^.left,false);
  160. end;
  161. if codegenerror then
  162. exit;
  163. { ofcourse not commutative }
  164. if p^.swaped then
  165. swaptree(p);
  166. if genjumps then
  167. begin
  168. { It gives us advantage to check for the set elements
  169. separately instead of using the SET_IN_BYTE procedure.
  170. To do: Build in support for LOC_JUMP }
  171. { If register is used, use only lower 8 bits }
  172. if p^.left^.location.loc in [LOC_REGISTER,LOC_CREGISTER] then
  173. begin
  174. pleftreg:=p^.left^.location.register;
  175. if pleftreg in [R_AX..R_DX] then
  176. begin
  177. emit_const_reg(A_AND,S_W,255,pleftreg);
  178. opsize:=S_W;
  179. end
  180. else
  181. if pleftreg in [R_EAX..R_EDI] then
  182. begin
  183. emit_const_reg(A_AND,S_L,255,pleftreg);
  184. opsize:=S_L;
  185. end
  186. else
  187. opsize:=S_B;
  188. end;
  189. { Get a label to jump to the end }
  190. p^.location.loc:=LOC_FLAGS;
  191. { It's better to use the zero flag when there are
  192. no ranges }
  193. if ranges then
  194. p^.location.resflags:=F_C
  195. else
  196. p^.location.resflags:=F_E;
  197. getlabel(l);
  198. for i:=1 to numparts do
  199. if setparts[i].range then
  200. begin
  201. { Check if left is in a range }
  202. { Get a label to jump over the check }
  203. getlabel(l2);
  204. if setparts[i].start=setparts[i].stop-1 then
  205. begin
  206. case p^.left^.location.loc of
  207. LOC_REGISTER,
  208. LOC_CREGISTER : emit_const_reg(A_CMP,opsize,
  209. setparts[i].start,pleftreg);
  210. else
  211. emit_const_ref(A_CMP,S_B,
  212. setparts[i].start,newreference(p^.left^.location.reference));
  213. end;
  214. { Result should be in carry flag when ranges are used }
  215. if ranges then
  216. emit_none(A_STC,S_NO);
  217. { If found, jump to end }
  218. emitjmp(C_E,l);
  219. case p^.left^.location.loc of
  220. LOC_REGISTER,
  221. LOC_CREGISTER : emit_const_reg(A_CMP,opsize,
  222. setparts[i].stop,pleftreg);
  223. else
  224. emit_const_ref(A_CMP,S_B,
  225. setparts[i].stop,newreference(p^.left^.location.reference));
  226. end;
  227. { Result should be in carry flag when ranges are used }
  228. if ranges then
  229. emit_none(A_STC,S_NO);
  230. { If found, jump to end }
  231. emitjmp(C_E,l);
  232. end
  233. else
  234. begin
  235. if setparts[i].start<>0 then
  236. begin
  237. { We only check for the lower bound if it is > 0, because
  238. set elements lower than 0 dont exist }
  239. case p^.left^.location.loc of
  240. LOC_REGISTER,
  241. LOC_CREGISTER :
  242. emit_const_reg(A_CMP,opsize,
  243. setparts[i].start,pleftreg);
  244. else
  245. emit_const_ref(A_CMP,S_B,
  246. setparts[i].start,newreference(p^.left^.location.reference));
  247. end;
  248. { If lower, jump to next check }
  249. emitjmp(C_B,l2);
  250. end;
  251. { We only check for the high bound if it is < 255, because
  252. set elements higher than 255 do nt exist, the its always true,
  253. so only a JMP is generated }
  254. if setparts[i].stop<>255 then
  255. begin
  256. case p^.left^.location.loc of
  257. LOC_REGISTER,
  258. LOC_CREGISTER : emit_const_reg(A_CMP,opsize,
  259. setparts[i].stop+1,pleftreg);
  260. else
  261. emit_const_ref(A_CMP,S_B,
  262. setparts[i].stop+1,newreference(p^.left^.location.reference));
  263. end;
  264. { If higher, element is in set }
  265. emitjmp(C_B,l);
  266. end
  267. else
  268. begin
  269. emit_none(A_STC,S_NO);
  270. emitjmp(C_None,l);
  271. end;
  272. end;
  273. { Emit the jump over label }
  274. emitlab(l2);
  275. end
  276. else
  277. begin
  278. { Emit code to check if left is an element }
  279. case p^.left^.location.loc of
  280. LOC_REGISTER,
  281. LOC_CREGISTER : emit_const_reg(A_CMP,opsize,
  282. setparts[i].stop,pleftreg);
  283. else
  284. emit_const_ref(A_CMP,S_B,
  285. setparts[i].stop,newreference(p^.left^.location.reference));
  286. end;
  287. { Result should be in carry flag when ranges are used }
  288. if ranges then
  289. emit_none(A_STC,S_NO);
  290. { If found, jump to end }
  291. emitjmp(C_E,l);
  292. end;
  293. if ranges then
  294. emit_none(A_CLC,S_NO);
  295. { To compensate for not doing a second pass }
  296. p^.right^.location.reference.symbol:=nil;
  297. { Now place the end label }
  298. emitlab(l);
  299. case p^.left^.location.loc of
  300. LOC_REGISTER,
  301. LOC_CREGISTER : ungetregister32(pleftreg);
  302. else
  303. del_reference(p^.left^.location.reference);
  304. end;
  305. end
  306. else
  307. begin
  308. { We will now generated code to check the set itself, no jmps,
  309. handle smallsets separate, because it allows faster checks }
  310. if use_small then
  311. begin
  312. if p^.left^.treetype=ordconstn then
  313. begin
  314. p^.location.resflags:=F_NE;
  315. case p^.right^.location.loc of
  316. LOC_REGISTER,
  317. LOC_CREGISTER:
  318. begin
  319. emit_const_reg(A_TEST,S_L,
  320. 1 shl (p^.left^.value and 31),p^.right^.location.register);
  321. ungetregister32(p^.right^.location.register);
  322. end
  323. else
  324. begin
  325. emit_const_ref(A_TEST,S_L,1 shl (p^.left^.value and 31),
  326. newreference(p^.right^.location.reference));
  327. del_reference(p^.right^.location.reference);
  328. end;
  329. end;
  330. end
  331. else
  332. begin
  333. case p^.left^.location.loc of
  334. LOC_REGISTER,
  335. LOC_CREGISTER:
  336. begin
  337. hr:=p^.left^.location.register;
  338. emit_to_reg32(hr);
  339. end;
  340. else
  341. begin
  342. { the set element isn't never samller than a byte }
  343. { and because it's a small set we need only 5 bits }
  344. { but 8 bits are easier to load }
  345. emit_ref_reg(A_MOVZX,S_BL,
  346. newreference(p^.left^.location.reference),R_EDI);
  347. hr:=R_EDI;
  348. del_reference(p^.left^.location.reference);
  349. end;
  350. end;
  351. case p^.right^.location.loc of
  352. LOC_REGISTER,
  353. LOC_CREGISTER : emit_reg_reg(A_BT,S_L,hr,
  354. p^.right^.location.register);
  355. else
  356. begin
  357. del_reference(p^.right^.location.reference);
  358. if p^.right^.location.reference.is_immediate then
  359. begin
  360. { We have to load the value into a register because
  361. btl does not accept values only refs or regs (PFV) }
  362. hr2:=getregister32;
  363. emit_const_reg(A_MOV,S_L,
  364. p^.right^.location.reference.offset,hr2);
  365. emit_reg_reg(A_BT,S_L,hr,hr2);
  366. ungetregister32(hr2);
  367. end
  368. else
  369. emit_reg_ref(A_BT,S_L,hr,
  370. newreference(p^.right^.location.reference));
  371. end;
  372. end;
  373. ungetregister32(hr);
  374. p^.location.loc:=LOC_FLAGS;
  375. p^.location.resflags:=F_C;
  376. end;
  377. end
  378. else
  379. begin
  380. if p^.right^.location.reference.is_immediate then
  381. begin
  382. p^.location.resflags:=F_C;
  383. getlabel(l);
  384. getlabel(l2);
  385. { Is this treated in firstpass ?? }
  386. if p^.left^.treetype=ordconstn then
  387. begin
  388. hr:=getregister32;
  389. p^.left^.location.loc:=LOC_REGISTER;
  390. p^.left^.location.register:=hr;
  391. emit_const_reg(A_MOV,S_L,
  392. p^.left^.value,hr);
  393. end;
  394. case p^.left^.location.loc of
  395. LOC_REGISTER,
  396. LOC_CREGISTER:
  397. begin
  398. hr:=p^.left^.location.register;
  399. emit_to_reg32(hr);
  400. emit_const_reg(A_CMP,S_L,31,hr);
  401. emitjmp(C_NA,l);
  402. { reset carry flag }
  403. emit_none(A_CLC,S_NO);
  404. emitjmp(C_NONE,l2);
  405. emitlab(l);
  406. { We have to load the value into a register because
  407. btl does not accept values only refs or regs (PFV) }
  408. hr2:=getregister32;
  409. emit_const_reg(A_MOV,S_L,p^.right^.location.reference.offset,hr2);
  410. emit_reg_reg(A_BT,S_L,hr,hr2);
  411. ungetregister32(hr2);
  412. end;
  413. else
  414. begin
  415. {$ifdef CORRECT_SET_IN_FPC}
  416. if m_tp in aktmodeswitches then
  417. begin
  418. {***WARNING only correct if
  419. reference is 32 bits (PM) *****}
  420. emit_const_ref(A_CMP,S_L,
  421. 31,newreference(p^.left^.location.reference));
  422. end
  423. else
  424. {$endif CORRECT_SET_IN_FPC}
  425. begin
  426. emit_const_ref(A_CMP,S_B,
  427. 31,newreference(p^.left^.location.reference));
  428. end;
  429. emitjmp(C_NA,l);
  430. { reset carry flag }
  431. emit_none(A_CLC,S_NO);
  432. emitjmp(C_NONE,l2);
  433. emitlab(l);
  434. del_reference(p^.left^.location.reference);
  435. hr:=getregister32;
  436. emit_ref_reg(A_MOV,S_L,
  437. newreference(p^.left^.location.reference),hr);
  438. { We have to load the value into a register because
  439. btl does not accept values only refs or regs (PFV) }
  440. hr2:=getregister32;
  441. emit_const_reg(A_MOV,S_L,
  442. p^.right^.location.reference.offset,hr2);
  443. emit_reg_reg(A_BT,S_L,hr,hr2);
  444. ungetregister32(hr2);
  445. end;
  446. end;
  447. emitlab(l2);
  448. end { of p^.right^.location.reference.is_immediate }
  449. { do search in a normal set which could have >32 elementsm
  450. but also used if the left side contains higher values > 32 }
  451. else if p^.left^.treetype=ordconstn then
  452. begin
  453. p^.location.resflags:=F_NE;
  454. inc(p^.right^.location.reference.offset,p^.left^.value shr 3);
  455. emit_const_ref(A_TEST,S_B,1 shl (p^.left^.value and 7),
  456. newreference(p^.right^.location.reference));
  457. del_reference(p^.right^.location.reference);
  458. end
  459. else
  460. begin
  461. pushsetelement(p^.left);
  462. emitpushreferenceaddr(p^.right^.location.reference);
  463. del_reference(p^.right^.location.reference);
  464. { registers need not be save. that happens in SET_IN_BYTE }
  465. { (EDI is changed) }
  466. emitcall('FPC_SET_IN_BYTE');
  467. { ungetiftemp(p^.right^.location.reference); }
  468. p^.location.loc:=LOC_FLAGS;
  469. p^.location.resflags:=F_C;
  470. end;
  471. end;
  472. end;
  473. if (p^.right^.location.loc in [LOC_MEM,LOC_REFERENCE]) then
  474. ungetiftemp(p^.right^.location.reference);
  475. end;
  476. {*****************************************************************************
  477. SecondCase
  478. *****************************************************************************}
  479. procedure secondcase(var p : ptree);
  480. var
  481. with_sign : boolean;
  482. opsize : topsize;
  483. jmp_gt,jmp_le,jmp_lee : tasmcond;
  484. hp : ptree;
  485. { register with case expression }
  486. hregister : tregister;
  487. endlabel,elselabel : pasmlabel;
  488. { true, if we can omit the range check of the jump table }
  489. jumptable_no_range : boolean;
  490. { where to put the jump table }
  491. jumpsegment : paasmoutput;
  492. procedure gentreejmp(p : pcaserecord);
  493. var
  494. lesslabel,greaterlabel : pasmlabel;
  495. begin
  496. emitlab(p^._at);
  497. { calculate labels for left and right }
  498. if (p^.less=nil) then
  499. lesslabel:=elselabel
  500. else
  501. lesslabel:=p^.less^._at;
  502. if (p^.greater=nil) then
  503. greaterlabel:=elselabel
  504. else
  505. greaterlabel:=p^.greater^._at;
  506. { calculate labels for left and right }
  507. { no range label: }
  508. if p^._low=p^._high then
  509. begin
  510. emit_const_reg(A_CMP,opsize,p^._low,hregister);
  511. if greaterlabel=lesslabel then
  512. emitjmp(C_NE,lesslabel)
  513. else
  514. begin
  515. emitjmp(jmp_le,lesslabel);
  516. emitjmp(jmp_gt,greaterlabel);
  517. end;
  518. emitjmp(C_None,p^.statement);
  519. end
  520. else
  521. begin
  522. emit_const_reg(A_CMP,opsize,p^._low,hregister);
  523. emitjmp(jmp_le,lesslabel);
  524. emit_const_reg(A_CMP,opsize,p^._high,hregister);
  525. emitjmp(jmp_gt,greaterlabel);
  526. emitjmp(C_None,p^.statement);
  527. end;
  528. if assigned(p^.less) then
  529. gentreejmp(p^.less);
  530. if assigned(p^.greater) then
  531. gentreejmp(p^.greater);
  532. end;
  533. procedure genlinearlist(hp : pcaserecord);
  534. var
  535. first : boolean;
  536. last : longint;
  537. {helplabel : longint;}
  538. procedure genitem(t : pcaserecord);
  539. begin
  540. if assigned(t^.less) then
  541. genitem(t^.less);
  542. { need we to test the first value }
  543. if first and (t^._low>get_min_value(p^.left^.resulttype)) then
  544. begin
  545. emit_const_reg(A_CMP,opsize,t^._low,hregister);
  546. emitjmp(jmp_le,elselabel);
  547. end;
  548. if t^._low=t^._high then
  549. begin
  550. if t^._low-last=1 then
  551. emit_reg(A_DEC,opsize,hregister)
  552. else if t^._low-last=0 then
  553. emit_reg_reg(A_OR,opsize,hregister,hregister)
  554. else
  555. emit_const_reg(A_SUB,opsize,t^._low-last,hregister);
  556. last:=t^._low;
  557. emitjmp(C_Z,t^.statement);
  558. end
  559. else
  560. begin
  561. { it begins with the smallest label, if the value }
  562. { is even smaller then jump immediately to the }
  563. { ELSE-label }
  564. if first then
  565. begin
  566. { have we to ajust the first value ? }
  567. if t^._low>get_min_value(p^.left^.resulttype) then
  568. begin
  569. if t^._low=1 then
  570. emit_reg(A_DEC,opsize,
  571. hregister)
  572. else
  573. emit_const_reg(A_SUB,opsize,
  574. t^._low,hregister);
  575. end;
  576. end
  577. else
  578. { if there is no unused label between the last and the }
  579. { present label then the lower limit can be checked }
  580. { immediately. else check the range in between: }
  581. if (t^._low-last>1) then
  582. begin
  583. emit_const_reg(A_SUB,opsize,t^._low-last,hregister);
  584. emitjmp(jmp_le,elselabel);
  585. end
  586. else
  587. emit_reg(A_DEC,opsize,hregister);
  588. emit_const_reg(A_SUB,opsize,t^._high-t^._low,hregister);
  589. emitjmp(jmp_lee,t^.statement);
  590. last:=t^._high;
  591. end;
  592. first:=false;
  593. if assigned(t^.greater) then
  594. genitem(t^.greater);
  595. end;
  596. begin
  597. last:=0;
  598. first:=true;
  599. genitem(hp);
  600. emitjmp(C_None,elselabel);
  601. end;
  602. procedure genjumptable(hp : pcaserecord;min_,max_ : longint);
  603. var
  604. table : pasmlabel;
  605. last : longint;
  606. hr : preference;
  607. procedure genitem(t : pcaserecord);
  608. var
  609. i : longint;
  610. begin
  611. if assigned(t^.less) then
  612. genitem(t^.less);
  613. { fill possible hole }
  614. for i:=last+1 to t^._low-1 do
  615. jumpsegment^.concat(new(pai_const_symbol,init(elselabel)));
  616. for i:=t^._low to t^._high do
  617. jumpsegment^.concat(new(pai_const_symbol,init(t^.statement)));
  618. last:=t^._high;
  619. if assigned(t^.greater) then
  620. genitem(t^.greater);
  621. end;
  622. begin
  623. if not(jumptable_no_range) then
  624. begin
  625. emit_const_reg(A_CMP,opsize,min_,hregister);
  626. { case expr less than min_ => goto elselabel }
  627. emitjmp(jmp_le,elselabel);
  628. emit_const_reg(A_CMP,opsize,max_,hregister);
  629. emitjmp(jmp_gt,elselabel);
  630. end;
  631. getlabel(table);
  632. { extend with sign }
  633. if opsize=S_W then
  634. begin
  635. if with_sign then
  636. emit_reg_reg(A_MOVSX,S_WL,hregister,
  637. reg16toreg32(hregister))
  638. else
  639. emit_reg_reg(A_MOVZX,S_WL,hregister,
  640. reg16toreg32(hregister));
  641. hregister:=reg16toreg32(hregister);
  642. end
  643. else if opsize=S_B then
  644. begin
  645. if with_sign then
  646. emit_reg_reg(A_MOVSX,S_BL,hregister,
  647. reg8toreg32(hregister))
  648. else
  649. emit_reg_reg(A_MOVZX,S_BL,hregister,
  650. reg8toreg32(hregister));
  651. hregister:=reg8toreg32(hregister);
  652. end;
  653. new(hr);
  654. reset_reference(hr^);
  655. hr^.symbol:=table;
  656. hr^.offset:=(-min_)*4;
  657. hr^.index:=hregister;
  658. hr^.scalefactor:=4;
  659. emit_ref(A_JMP,S_NO,hr);
  660. { !!!!! generate tables
  661. if not(cs_littlesize in aktlocalswitches) then
  662. jumpsegment^.concat(new(paicpu,op_const(A_ALIGN,S_NO,4)));
  663. }
  664. jumpsegment^.concat(new(pai_label,init(table)));
  665. last:=min_;
  666. genitem(hp);
  667. { !!!!!!!
  668. if not(cs_littlesize in aktlocalswitches) then
  669. emit_const(A_ALIGN,S_NO,4);
  670. }
  671. end;
  672. var
  673. lv,hv,min_label,max_label,labels : longint;
  674. max_linear_list : longint;
  675. {$ifdef Delphi}
  676. dist : cardinal;
  677. {$else Delphi}
  678. dist : dword;
  679. {$endif Delphi}
  680. begin
  681. getlabel(endlabel);
  682. getlabel(elselabel);
  683. if (cs_create_smart in aktmoduleswitches) then
  684. jumpsegment:=procinfo^.aktlocaldata
  685. else
  686. jumpsegment:=datasegment;
  687. with_sign:=is_signed(p^.left^.resulttype);
  688. if with_sign then
  689. begin
  690. jmp_gt:=C_G;
  691. jmp_le:=C_L;
  692. jmp_lee:=C_LE;
  693. end
  694. else
  695. begin
  696. jmp_gt:=C_A;
  697. jmp_le:=C_B;
  698. jmp_lee:=C_BE;
  699. end;
  700. cleartempgen;
  701. secondpass(p^.left);
  702. { determines the size of the operand }
  703. opsize:=bytes2Sxx[p^.left^.resulttype^.size];
  704. { copy the case expression to a register }
  705. case p^.left^.location.loc of
  706. LOC_REGISTER:
  707. hregister:=p^.left^.location.register;
  708. LOC_FLAGS :
  709. begin
  710. hregister:=getregister32;
  711. case opsize of
  712. S_B : hregister:=reg32toreg8(hregister);
  713. S_W : hregister:=reg32toreg16(hregister);
  714. end;
  715. emit_flag2reg(p^.left^.location.resflags,hregister);
  716. end;
  717. LOC_CREGISTER:
  718. begin
  719. hregister:=getregister32;
  720. case opsize of
  721. S_B : hregister:=reg32toreg8(hregister);
  722. S_W : hregister:=reg32toreg16(hregister);
  723. end;
  724. emit_reg_reg(A_MOV,opsize,
  725. p^.left^.location.register,hregister);
  726. end;
  727. LOC_MEM,LOC_REFERENCE : begin
  728. del_reference(p^.left^.location.reference);
  729. hregister:=getregister32;
  730. case opsize of
  731. S_B : hregister:=reg32toreg8(hregister);
  732. S_W : hregister:=reg32toreg16(hregister);
  733. end;
  734. emit_ref_reg(A_MOV,opsize,newreference(
  735. p^.left^.location.reference),hregister);
  736. end;
  737. else internalerror(2002);
  738. end;
  739. { now generate the jumps }
  740. if cs_optimize in aktglobalswitches then
  741. begin
  742. { procedures are empirically passed on }
  743. { consumption can also be calculated }
  744. { but does it pay on the different }
  745. { processors? }
  746. { moreover can the size only be appro- }
  747. { ximated as it is not known if rel8, }
  748. { rel16 or rel32 jumps are used }
  749. min_label:=case_get_min(p^.nodes);
  750. max_label:=case_get_max(p^.nodes);
  751. labels:=case_count_labels(p^.nodes);
  752. { can we omit the range check of the jump table ? }
  753. getrange(p^.left^.resulttype,lv,hv);
  754. jumptable_no_range:=(lv=min_label) and (hv=max_label);
  755. { hack a little bit, because the range can be greater }
  756. { than the positive range of a longint }
  757. if (min_label<0) and (max_label>0) then
  758. begin
  759. {$ifdef Delphi}
  760. if min_label=$80000000 then
  761. dist:=Cardinal(max_label)+Cardinal($80000000)
  762. else
  763. dist:=Cardinal(max_label)+Cardinal(-min_label)
  764. {$else Delphi}
  765. if min_label=$80000000 then
  766. dist:=dword(max_label)+dword($80000000)
  767. else
  768. dist:=dword(max_label)+dword(-min_label)
  769. {$endif Delphi}
  770. end
  771. else
  772. dist:=max_label-min_label;
  773. { optimize for size ? }
  774. if cs_littlesize in aktglobalswitches then
  775. begin
  776. if (labels<=2) or
  777. ((max_label-min_label)<0) or
  778. ((max_label-min_label)>3*labels) then
  779. { a linear list is always smaller than a jump tree }
  780. genlinearlist(p^.nodes)
  781. else
  782. { if the labels less or more a continuum then }
  783. genjumptable(p^.nodes,min_label,max_label);
  784. end
  785. else
  786. begin
  787. if jumptable_no_range then
  788. max_linear_list:=4
  789. else
  790. max_linear_list:=2;
  791. { a jump table crashes the pipeline! }
  792. if aktoptprocessor=Class386 then
  793. inc(max_linear_list,3);
  794. if aktoptprocessor=ClassP5 then
  795. inc(max_linear_list,6);
  796. if aktoptprocessor>=ClassP6 then
  797. inc(max_linear_list,9);
  798. if (labels<=max_linear_list) then
  799. genlinearlist(p^.nodes)
  800. else
  801. begin
  802. if (dist>4*labels) then
  803. begin
  804. if labels>16 then
  805. gentreejmp(p^.nodes)
  806. else
  807. genlinearlist(p^.nodes);
  808. end
  809. else
  810. genjumptable(p^.nodes,min_label,max_label);
  811. end;
  812. end;
  813. end
  814. else
  815. { it's always not bad }
  816. genlinearlist(p^.nodes);
  817. {$IfDef regallocfix}
  818. ungetregister(hregister);
  819. {$EndIf regallocfix}
  820. { now generate the instructions }
  821. hp:=p^.right;
  822. while assigned(hp) do
  823. begin
  824. cleartempgen;
  825. secondpass(hp^.right);
  826. { don't come back to case line }
  827. aktfilepos:=exprasmlist^.getlasttaifilepos^;
  828. emitjmp(C_None,endlabel);
  829. hp:=hp^.left;
  830. end;
  831. emitlab(elselabel);
  832. { ...and the else block }
  833. if assigned(p^.elseblock) then
  834. begin
  835. cleartempgen;
  836. secondpass(p^.elseblock);
  837. end;
  838. emitlab(endlabel);
  839. end;
  840. end.
  841. {
  842. $Log$
  843. Revision 1.45 2000-01-07 01:14:21 peter
  844. * updated copyright to 2000
  845. Revision 1.44 1999/12/01 22:45:54 peter
  846. * fixed wrong assembler with in-node
  847. Revision 1.43 1999/11/06 14:34:18 peter
  848. * truncated log to 20 revs
  849. Revision 1.42 1999/09/27 23:44:48 peter
  850. * procinfo is now a pointer
  851. * support for result setting in sub procedure
  852. Revision 1.41 1999/09/20 16:38:52 peter
  853. * cs_create_smart instead of cs_smartlink
  854. * -CX is create smartlink
  855. * -CD is create dynamic, but does nothing atm.
  856. Revision 1.40 1999/08/25 11:59:47 jonas
  857. * changed pai386, paippc and paiapha (same for tai*) to paicpu (taicpu)
  858. Revision 1.39 1999/08/23 23:46:42 pierre
  859. * del_reference moved to respect registers32 in secondin
  860. Revision 1.38 1999/08/19 13:08:53 pierre
  861. * emit_??? used
  862. Revision 1.37 1999/08/04 00:22:54 florian
  863. * renamed i386asm and i386base to cpuasm and cpubase
  864. Revision 1.36 1999/08/03 22:02:48 peter
  865. * moved bitmask constants to sets
  866. * some other type/const renamings
  867. Revision 1.35 1999/07/18 14:01:16 florian
  868. * handling of integer and shortint in case was wrong, if a case
  869. label was negative and a jump table was generated
  870. Revision 1.34 1999/06/08 15:27:24 pierre
  871. * fix for bug0258
  872. Revision 1.33 1999/06/02 10:11:48 florian
  873. * make cycle fixed i.e. compilation with 0.99.10
  874. * some fixes for qword
  875. * start of register calling conventions
  876. Revision 1.32 1999/05/27 19:44:19 peter
  877. * removed oldasm
  878. * plabel -> pasmlabel
  879. * -a switches to source writing automaticly
  880. * assembler readers OOPed
  881. * asmsymbol automaticly external
  882. * jumptables and other label fixes for asm readers
  883. Revision 1.31 1999/05/21 13:54:54 peter
  884. * NEWLAB for label as symbol
  885. Revision 1.30 1999/05/05 08:09:24 michael
  886. * Changed longword to cardinal
  887. Revision 1.29 1999/05/04 21:44:34 florian
  888. * changes to compile it with Delphi 4.0
  889. Revision 1.28 1999/05/01 13:24:15 peter
  890. * merged nasm compiler
  891. * old asm moved to oldasm/
  892. Revision 1.27 1999/04/16 13:42:30 jonas
  893. * more regalloc fixes (still not complete)
  894. Revision 1.26 1999/04/09 08:36:36 peter
  895. * fix also for -Og
  896. Revision 1.25 1999/04/08 20:59:37 florian
  897. * fixed problem with default properties which are a class
  898. * case bug (from the mailing list with -O2) fixed, the
  899. distance of the case labels can be greater than the positive
  900. range of a longint => it is now a dword for fpc
  901. Revision 1.24 1999/03/02 18:21:35 peter
  902. + flags support for add and case
  903. Revision 1.23 1999/02/25 21:02:31 peter
  904. * ag386bin updates
  905. + coff writer
  906. }