cg386set.pas 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056
  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. globtype,systems,
  28. cobjects,verbose,globals,
  29. symconst,symtable,aasm,types,
  30. hcodegen,temp_gen,pass_2,
  31. i386base,i386asm,
  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. exprasmlist^.concat(new(pai386,op_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. exprasmlist^.concat(new(pai386,op_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 : exprasmlist^.concat(new(pai386,op_const_reg(A_CMP,opsize,
  209. setparts[i].start,pleftreg)));
  210. else
  211. exprasmlist^.concat(new(pai386,op_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. exprasmlist^.concat(new(pai386,op_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 : exprasmlist^.concat(new(pai386,op_const_reg(A_CMP,opsize,
  222. setparts[i].stop,pleftreg)));
  223. else
  224. exprasmlist^.concat(new(pai386,op_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. exprasmlist^.concat(new(pai386,op_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 : exprasmlist^.concat(new(pai386,op_const_reg(A_CMP,opsize,
  242. setparts[i].start,pleftreg)));
  243. else
  244. exprasmlist^.concat(new(pai386,op_const_ref(A_CMP,S_B,
  245. setparts[i].start,newreference(p^.left^.location.reference))));
  246. end;
  247. { If lower, jump to next check }
  248. emitjmp(C_B,l2);
  249. end;
  250. { We only check for the high bound if it is < 255, because
  251. set elements higher than 255 do nt exist, the its always true,
  252. so only a JMP is generated }
  253. if setparts[i].stop<>255 then
  254. begin
  255. case p^.left^.location.loc of
  256. LOC_REGISTER,
  257. LOC_CREGISTER : exprasmlist^.concat(new(pai386,op_const_reg(A_CMP,opsize,
  258. setparts[i].stop+1,pleftreg)));
  259. else
  260. exprasmlist^.concat(new(pai386,op_const_ref(A_CMP,S_B,
  261. setparts[i].stop+1,newreference(p^.left^.location.reference))));
  262. end;
  263. { If higher, element is in set }
  264. emitjmp(C_B,l);
  265. end
  266. else
  267. begin
  268. exprasmlist^.concat(new(pai386,op_none(A_STC,S_NO)));
  269. emitjmp(C_None,l);
  270. end;
  271. end;
  272. { Emit the jump over label }
  273. emitlab(l2);
  274. end
  275. else
  276. begin
  277. { Emit code to check if left is an element }
  278. case p^.left^.location.loc of
  279. LOC_REGISTER,
  280. LOC_CREGISTER : exprasmlist^.concat(new(pai386,op_const_reg(A_CMP,opsize,
  281. setparts[i].stop,pleftreg)));
  282. else
  283. exprasmlist^.concat(new(pai386,op_const_ref(A_CMP,S_B,
  284. setparts[i].stop,newreference(p^.left^.location.reference))));
  285. end;
  286. { Result should be in carry flag when ranges are used }
  287. if ranges then
  288. exprasmlist^.concat(new(pai386,op_none(A_STC,S_NO)));
  289. { If found, jump to end }
  290. emitjmp(C_E,l);
  291. end;
  292. if ranges then
  293. exprasmlist^.concat(new(pai386,op_none(A_CLC,S_NO)));
  294. { To compensate for not doing a second pass }
  295. p^.right^.location.reference.symbol:=nil;
  296. { Now place the end label }
  297. emitlab(l);
  298. case p^.left^.location.loc of
  299. LOC_REGISTER,
  300. LOC_CREGISTER : ungetregister32(pleftreg);
  301. else
  302. del_reference(p^.left^.location.reference);
  303. end;
  304. end
  305. else
  306. begin
  307. { We will now generated code to check the set itself, no jmps,
  308. handle smallsets separate, because it allows faster checks }
  309. if use_small then
  310. begin
  311. if p^.left^.treetype=ordconstn then
  312. begin
  313. p^.location.resflags:=F_NE;
  314. case p^.right^.location.loc of
  315. LOC_REGISTER,
  316. LOC_CREGISTER:
  317. begin
  318. exprasmlist^.concat(new(pai386,op_const_reg(A_TEST,S_L,
  319. 1 shl (p^.left^.value and 31),p^.right^.location.register)));
  320. ungetregister32(p^.right^.location.register);
  321. end
  322. else
  323. begin
  324. exprasmlist^.concat(new(pai386,op_const_ref(A_TEST,S_L,1 shl (p^.left^.value and 31),
  325. newreference(p^.right^.location.reference))));
  326. del_reference(p^.right^.location.reference);
  327. end;
  328. end;
  329. end
  330. else
  331. begin
  332. case p^.left^.location.loc of
  333. LOC_REGISTER,
  334. LOC_CREGISTER:
  335. begin
  336. hr:=p^.left^.location.register;
  337. emit_to_reg32(hr);
  338. end;
  339. else
  340. begin
  341. { the set element isn't never samller than a byte }
  342. { and because it's a small set we need only 5 bits }
  343. { but 8 bits are easier to load }
  344. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOVZX,S_BL,
  345. newreference(p^.left^.location.reference),R_EDI)));
  346. hr:=R_EDI;
  347. del_reference(p^.left^.location.reference);
  348. end;
  349. end;
  350. case p^.right^.location.loc of
  351. LOC_REGISTER,
  352. LOC_CREGISTER : exprasmlist^.concat(new(pai386,op_reg_reg(A_BT,S_L,hr,
  353. p^.right^.location.register)));
  354. else
  355. begin
  356. del_reference(p^.right^.location.reference);
  357. if p^.right^.location.reference.is_immediate then
  358. begin
  359. { We have to load the value into a register because
  360. btl does not accept values only refs or regs (PFV) }
  361. hr2:=getregister32;
  362. exprasmlist^.concat(new(pai386,op_const_reg(A_MOV,S_L,
  363. p^.right^.location.reference.offset,hr2)));
  364. exprasmlist^.concat(new(pai386,op_reg_reg(A_BT,S_L,hr,hr2)));
  365. ungetregister32(hr2);
  366. end
  367. else
  368. exprasmlist^.concat(new(pai386,op_reg_ref(A_BT,S_L,hr,
  369. newreference(p^.right^.location.reference))));
  370. end;
  371. end;
  372. ungetregister32(hr);
  373. p^.location.loc:=LOC_FLAGS;
  374. p^.location.resflags:=F_C;
  375. end;
  376. end
  377. else
  378. begin
  379. if p^.right^.location.reference.is_immediate then
  380. begin
  381. p^.location.resflags:=F_C;
  382. getlabel(l);
  383. getlabel(l2);
  384. { Is this treated in firstpass ?? }
  385. if p^.left^.treetype=ordconstn then
  386. begin
  387. hr:=getregister32;
  388. p^.left^.location.loc:=LOC_REGISTER;
  389. p^.left^.location.register:=hr;
  390. exprasmlist^.concat(new(pai386,op_const_reg(A_MOV,S_L,
  391. p^.left^.value,hr)));
  392. end;
  393. case p^.left^.location.loc of
  394. LOC_REGISTER,
  395. LOC_CREGISTER:
  396. begin
  397. hr:=regtoreg32(p^.left^.location.register);
  398. {$ifdef CORRECT_SET_IN_FPC}
  399. if m_tp in aktmodeswitches then
  400. begin
  401. if is_signed(p^.left^.resulttype) then
  402. AM:=A_MOVSX
  403. else
  404. AM:=A_MOVZX;
  405. if p^.left^.location.register in [R_AX,R_DI] then
  406. exprasmlist^.concat(new(pai386,op_reg_reg(AM,S_WL,
  407. p^.left^.location.register,hr)))
  408. else if p^.left^.location.register in [R_AL,R_DH] then
  409. exprasmlist^.concat(new(pai386,op_reg_reg(AM,S_BL,
  410. p^.left^.location.register,hr)));
  411. end
  412. else
  413. {$endif CORRECT_SET_IN_FPC}
  414. begin
  415. exprasmlist^.concat(new(pai386,op_const_reg(A_AND,S_L,
  416. 255,hr)));
  417. end;
  418. exprasmlist^.concat(new(pai386,op_const_reg(A_CMP,S_L,
  419. 31,hr)));
  420. emitjmp(C_NA,l);
  421. { reset carry flag }
  422. exprasmlist^.concat(new(pai386,op_none(A_CLC,S_NO)));
  423. emitjmp(C_NONE,l2);
  424. emitlab(l);
  425. { We have to load the value into a register because
  426. btl does not accept values only refs or regs (PFV) }
  427. hr2:=getregister32;
  428. exprasmlist^.concat(new(pai386,op_const_reg(A_MOV,S_L,
  429. p^.right^.location.reference.offset,hr2)));
  430. exprasmlist^.concat(new(pai386,op_reg_reg(A_BT,S_L,hr,hr2)));
  431. ungetregister32(hr2);
  432. end;
  433. else
  434. begin
  435. {$ifdef CORRECT_SET_IN_FPC}
  436. if m_tp in aktmodeswitches then
  437. begin
  438. {***WARNING only correct if
  439. reference is 32 bits (PM) *****}
  440. exprasmlist^.concat(new(pai386,op_const_ref(A_CMP,S_L,
  441. 31,newreference(p^.left^.location.reference))));
  442. end
  443. else
  444. {$endif CORRECT_SET_IN_FPC}
  445. begin
  446. exprasmlist^.concat(new(pai386,op_const_ref(A_CMP,S_B,
  447. 31,newreference(p^.left^.location.reference))));
  448. end;
  449. emitjmp(C_NA,l);
  450. { reset carry flag }
  451. exprasmlist^.concat(new(pai386,op_none(A_CLC,S_NO)));
  452. emitjmp(C_NONE,l2);
  453. emitlab(l);
  454. hr:=getregister32;
  455. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  456. newreference(p^.left^.location.reference),hr)));
  457. { We have to load the value into a register because
  458. btl does not accept values only refs or regs (PFV) }
  459. hr2:=getregister32;
  460. exprasmlist^.concat(new(pai386,op_const_reg(A_MOV,S_L,
  461. p^.right^.location.reference.offset,hr2)));
  462. exprasmlist^.concat(new(pai386,op_reg_reg(A_BT,S_L,hr,hr2)));
  463. ungetregister32(hr2);
  464. del_reference(p^.left^.location.reference);
  465. end;
  466. emitlab(l2);
  467. end;
  468. end { of p^.right^.location.reference.is_immediate }
  469. { do search in a normal set which could have >32 elementsm
  470. but also used if the left side contains higher values > 32 }
  471. else if p^.left^.treetype=ordconstn then
  472. begin
  473. p^.location.resflags:=F_NE;
  474. inc(p^.right^.location.reference.offset,p^.left^.value shr 3);
  475. exprasmlist^.concat(new(pai386,op_const_ref(A_TEST,S_B,1 shl (p^.left^.value and 7),
  476. newreference(p^.right^.location.reference))));
  477. del_reference(p^.right^.location.reference);
  478. end
  479. else
  480. begin
  481. pushsetelement(p^.left);
  482. emitpushreferenceaddr(p^.right^.location.reference);
  483. del_reference(p^.right^.location.reference);
  484. { registers need not be save. that happens in SET_IN_BYTE }
  485. { (EDI is changed) }
  486. emitcall('FPC_SET_IN_BYTE');
  487. { ungetiftemp(p^.right^.location.reference); }
  488. p^.location.loc:=LOC_FLAGS;
  489. p^.location.resflags:=F_C;
  490. end;
  491. end;
  492. end;
  493. if (p^.right^.location.loc in [LOC_MEM,LOC_REFERENCE]) then
  494. ungetiftemp(p^.right^.location.reference);
  495. end;
  496. {*****************************************************************************
  497. SecondCase
  498. *****************************************************************************}
  499. procedure secondcase(var p : ptree);
  500. var
  501. with_sign : boolean;
  502. opsize : topsize;
  503. jmp_gt,jmp_le,jmp_lee : tasmcond;
  504. hp : ptree;
  505. { register with case expression }
  506. hregister : tregister;
  507. endlabel,elselabel : pasmlabel;
  508. { true, if we can omit the range check of the jump table }
  509. jumptable_no_range : boolean;
  510. { where to put the jump table }
  511. jumpsegment : paasmoutput;
  512. procedure gentreejmp(p : pcaserecord);
  513. var
  514. lesslabel,greaterlabel : pasmlabel;
  515. begin
  516. emitlab(p^._at);
  517. { calculate labels for left and right }
  518. if (p^.less=nil) then
  519. lesslabel:=elselabel
  520. else
  521. lesslabel:=p^.less^._at;
  522. if (p^.greater=nil) then
  523. greaterlabel:=elselabel
  524. else
  525. greaterlabel:=p^.greater^._at;
  526. { calculate labels for left and right }
  527. { no range label: }
  528. if p^._low=p^._high then
  529. begin
  530. exprasmlist^.concat(new(pai386,op_const_reg(A_CMP,opsize,p^._low,hregister)));
  531. if greaterlabel=lesslabel then
  532. emitjmp(C_NE,lesslabel)
  533. else
  534. begin
  535. emitjmp(jmp_le,lesslabel);
  536. emitjmp(jmp_gt,greaterlabel);
  537. end;
  538. emitjmp(C_None,p^.statement);
  539. end
  540. else
  541. begin
  542. exprasmlist^.concat(new(pai386,op_const_reg(A_CMP,opsize,p^._low,hregister)));
  543. emitjmp(jmp_le,lesslabel);
  544. exprasmlist^.concat(new(pai386,op_const_reg(A_CMP,opsize,p^._high,hregister)));
  545. emitjmp(jmp_gt,greaterlabel);
  546. emitjmp(C_None,p^.statement);
  547. end;
  548. if assigned(p^.less) then
  549. gentreejmp(p^.less);
  550. if assigned(p^.greater) then
  551. gentreejmp(p^.greater);
  552. end;
  553. procedure genlinearlist(hp : pcaserecord);
  554. var
  555. first : boolean;
  556. last : longint;
  557. {helplabel : longint;}
  558. procedure genitem(t : pcaserecord);
  559. begin
  560. if assigned(t^.less) then
  561. genitem(t^.less);
  562. { need we to test the first value }
  563. if first and (t^._low>get_min_value(p^.left^.resulttype)) then
  564. begin
  565. exprasmlist^.concat(new(pai386,op_const_reg(A_CMP,opsize,t^._low,hregister)));
  566. emitjmp(jmp_le,elselabel);
  567. end;
  568. if t^._low=t^._high then
  569. begin
  570. if t^._low-last=1 then
  571. exprasmlist^.concat(new(pai386,op_reg(A_DEC,opsize,hregister)))
  572. else if t^._low-last=0 then
  573. exprasmlist^.concat(new(pai386,op_reg_reg(A_OR,opsize,hregister,hregister)))
  574. else
  575. exprasmlist^.concat(new(pai386,op_const_reg(A_SUB,opsize,t^._low-last,hregister)));
  576. last:=t^._low;
  577. emitjmp(C_Z,t^.statement);
  578. end
  579. else
  580. begin
  581. { it begins with the smallest label, if the value }
  582. { is even smaller then jump immediately to the }
  583. { ELSE-label }
  584. if first then
  585. begin
  586. { have we to ajust the first value ? }
  587. if t^._low>get_min_value(p^.left^.resulttype) then
  588. begin
  589. if t^._low=1 then
  590. exprasmlist^.concat(new(pai386,op_reg(A_DEC,opsize,
  591. hregister)))
  592. else
  593. exprasmlist^.concat(new(pai386,op_const_reg(A_SUB,opsize,
  594. t^._low,hregister)));
  595. end;
  596. end
  597. else
  598. { if there is no unused label between the last and the }
  599. { present label then the lower limit can be checked }
  600. { immediately. else check the range in between: }
  601. if (t^._low-last>1) then
  602. begin
  603. exprasmlist^.concat(new(pai386,op_const_reg(A_SUB,opsize,t^._low-last,hregister)));
  604. emitjmp(jmp_le,elselabel);
  605. end
  606. else
  607. exprasmlist^.concat(new(pai386,op_reg(A_DEC,opsize,hregister)));
  608. exprasmlist^.concat(new(pai386,op_const_reg(A_SUB,opsize,t^._high-t^._low,hregister)));
  609. emitjmp(jmp_lee,t^.statement);
  610. last:=t^._high;
  611. end;
  612. first:=false;
  613. if assigned(t^.greater) then
  614. genitem(t^.greater);
  615. end;
  616. begin
  617. last:=0;
  618. first:=true;
  619. genitem(hp);
  620. emitjmp(C_None,elselabel);
  621. end;
  622. procedure genjumptable(hp : pcaserecord;min_,max_ : longint);
  623. var
  624. table : pasmlabel;
  625. last : longint;
  626. hr : preference;
  627. procedure genitem(t : pcaserecord);
  628. var
  629. i : longint;
  630. begin
  631. if assigned(t^.less) then
  632. genitem(t^.less);
  633. { fill possible hole }
  634. for i:=last+1 to t^._low-1 do
  635. jumpsegment^.concat(new(pai_const_symbol,init(elselabel)));
  636. for i:=t^._low to t^._high do
  637. jumpsegment^.concat(new(pai_const_symbol,init(t^.statement)));
  638. last:=t^._high;
  639. if assigned(t^.greater) then
  640. genitem(t^.greater);
  641. end;
  642. begin
  643. if not(jumptable_no_range) then
  644. begin
  645. exprasmlist^.concat(new(pai386,op_const_reg(A_CMP,opsize,min_,hregister)));
  646. { case expr less than min_ => goto elselabel }
  647. emitjmp(jmp_le,elselabel);
  648. exprasmlist^.concat(new(pai386,op_const_reg(A_CMP,opsize,max_,hregister)));
  649. emitjmp(jmp_gt,elselabel);
  650. end;
  651. getlabel(table);
  652. { extend with sign }
  653. if opsize=S_W then
  654. begin
  655. if with_sign then
  656. exprasmlist^.concat(new(pai386,op_reg_reg(A_MOVSX,S_WL,hregister,
  657. reg16toreg32(hregister))))
  658. else
  659. exprasmlist^.concat(new(pai386,op_reg_reg(A_MOVZX,S_WL,hregister,
  660. reg16toreg32(hregister))));
  661. hregister:=reg16toreg32(hregister);
  662. end
  663. else if opsize=S_B then
  664. begin
  665. if with_sign then
  666. exprasmlist^.concat(new(pai386,op_reg_reg(A_MOVSX,S_BL,hregister,
  667. reg8toreg32(hregister))))
  668. else
  669. exprasmlist^.concat(new(pai386,op_reg_reg(A_MOVZX,S_BL,hregister,
  670. reg8toreg32(hregister))));
  671. hregister:=reg8toreg32(hregister);
  672. end;
  673. new(hr);
  674. reset_reference(hr^);
  675. hr^.symbol:=table;
  676. hr^.offset:=(-min_)*4;
  677. hr^.index:=hregister;
  678. hr^.scalefactor:=4;
  679. exprasmlist^.concat(new(pai386,op_ref(A_JMP,S_NO,hr)));
  680. { !!!!! generate tables
  681. if not(cs_littlesize in aktlocalswitches) then
  682. jumpsegment^.concat(new(pai386,op_const(A_ALIGN,S_NO,4)));
  683. }
  684. jumpsegment^.concat(new(pai_label,init(table)));
  685. last:=min_;
  686. genitem(hp);
  687. { !!!!!!!
  688. if not(cs_littlesize in aktlocalswitches) then
  689. exprasmlist^.concat(new(pai386,op_const(A_ALIGN,S_NO,4)));
  690. }
  691. end;
  692. var
  693. lv,hv,min_label,max_label,labels : longint;
  694. max_linear_list : longint;
  695. {$ifdef Delphi}
  696. dist : cardinal;
  697. {$else Delphi}
  698. dist : dword;
  699. {$endif Delphi}
  700. begin
  701. getlabel(endlabel);
  702. getlabel(elselabel);
  703. if (cs_smartlink in aktmoduleswitches) then
  704. jumpsegment:=procinfo.aktlocaldata
  705. else
  706. jumpsegment:=datasegment;
  707. with_sign:=is_signed(p^.left^.resulttype);
  708. if with_sign then
  709. begin
  710. jmp_gt:=C_G;
  711. jmp_le:=C_L;
  712. jmp_lee:=C_LE;
  713. end
  714. else
  715. begin
  716. jmp_gt:=C_A;
  717. jmp_le:=C_B;
  718. jmp_lee:=C_BE;
  719. end;
  720. cleartempgen;
  721. secondpass(p^.left);
  722. { determines the size of the operand }
  723. opsize:=bytes2Sxx[p^.left^.resulttype^.size];
  724. { copy the case expression to a register }
  725. case p^.left^.location.loc of
  726. LOC_REGISTER:
  727. hregister:=p^.left^.location.register;
  728. LOC_FLAGS :
  729. begin
  730. hregister:=getregister32;
  731. case opsize of
  732. S_B : hregister:=reg32toreg8(hregister);
  733. S_W : hregister:=reg32toreg16(hregister);
  734. end;
  735. emit_flag2reg(p^.left^.location.resflags,hregister);
  736. end;
  737. LOC_CREGISTER:
  738. begin
  739. hregister:=getregister32;
  740. case opsize of
  741. S_B : hregister:=reg32toreg8(hregister);
  742. S_W : hregister:=reg32toreg16(hregister);
  743. end;
  744. exprasmlist^.concat(new(pai386,op_reg_reg(A_MOV,opsize,
  745. p^.left^.location.register,hregister)));
  746. end;
  747. LOC_MEM,LOC_REFERENCE : begin
  748. del_reference(p^.left^.location.reference);
  749. hregister:=getregister32;
  750. case opsize of
  751. S_B : hregister:=reg32toreg8(hregister);
  752. S_W : hregister:=reg32toreg16(hregister);
  753. end;
  754. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,opsize,newreference(
  755. p^.left^.location.reference),hregister)));
  756. end;
  757. else internalerror(2002);
  758. end;
  759. { now generate the jumps }
  760. if cs_optimize in aktglobalswitches then
  761. begin
  762. { procedures are empirically passed on }
  763. { consumption can also be calculated }
  764. { but does it pay on the different }
  765. { processors? }
  766. { moreover can the size only be appro- }
  767. { ximated as it is not known if rel8, }
  768. { rel16 or rel32 jumps are used }
  769. min_label:=case_get_min(p^.nodes);
  770. max_label:=case_get_max(p^.nodes);
  771. labels:=case_count_labels(p^.nodes);
  772. { can we omit the range check of the jump table ? }
  773. getrange(p^.left^.resulttype,lv,hv);
  774. jumptable_no_range:=(lv=min_label) and (hv=max_label);
  775. { hack a little bit, because the range can be greater }
  776. { than the positive range of a longint }
  777. if (min_label<0) and (max_label>0) then
  778. begin
  779. {$ifdef Delphi}
  780. if min_label=$80000000 then
  781. dist:=Cardinal(max_label)+Cardinal($80000000)
  782. else
  783. dist:=Cardinal(max_label)+Cardinal(-min_label)
  784. {$else Delphi}
  785. if min_label=$80000000 then
  786. dist:=dword(max_label)+dword($80000000)
  787. else
  788. dist:=dword(max_label)+dword(-min_label)
  789. {$endif Delphi}
  790. end
  791. else
  792. dist:=max_label-min_label;
  793. { optimize for size ? }
  794. if cs_littlesize in aktglobalswitches then
  795. begin
  796. if (labels<=2) or
  797. ((max_label-min_label)<0) or
  798. ((max_label-min_label)>3*labels) then
  799. { a linear list is always smaller than a jump tree }
  800. genlinearlist(p^.nodes)
  801. else
  802. { if the labels less or more a continuum then }
  803. genjumptable(p^.nodes,min_label,max_label);
  804. end
  805. else
  806. begin
  807. if jumptable_no_range then
  808. max_linear_list:=4
  809. else
  810. max_linear_list:=2;
  811. { a jump table crashes the pipeline! }
  812. if aktoptprocessor=Class386 then
  813. inc(max_linear_list,3);
  814. if aktoptprocessor=ClassP5 then
  815. inc(max_linear_list,6);
  816. if aktoptprocessor>=ClassP6 then
  817. inc(max_linear_list,9);
  818. if (labels<=max_linear_list) then
  819. genlinearlist(p^.nodes)
  820. else
  821. begin
  822. if (dist>4*labels) then
  823. begin
  824. if labels>16 then
  825. gentreejmp(p^.nodes)
  826. else
  827. genlinearlist(p^.nodes);
  828. end
  829. else
  830. genjumptable(p^.nodes,min_label,max_label);
  831. end;
  832. end;
  833. end
  834. else
  835. { it's always not bad }
  836. genlinearlist(p^.nodes);
  837. {$IfDef regallocfix}
  838. ungetregister(hregister);
  839. {$EndIf regallocfix}
  840. { now generate the instructions }
  841. hp:=p^.right;
  842. while assigned(hp) do
  843. begin
  844. cleartempgen;
  845. secondpass(hp^.right);
  846. { don't come back to case line }
  847. aktfilepos:=exprasmlist^.getlasttaifilepos^;
  848. emitjmp(C_None,endlabel);
  849. hp:=hp^.left;
  850. end;
  851. emitlab(elselabel);
  852. { ...and the else block }
  853. if assigned(p^.elseblock) then
  854. begin
  855. cleartempgen;
  856. secondpass(p^.elseblock);
  857. end;
  858. emitlab(endlabel);
  859. end;
  860. end.
  861. {
  862. $Log$
  863. Revision 1.36 1999-08-03 22:02:48 peter
  864. * moved bitmask constants to sets
  865. * some other type/const renamings
  866. Revision 1.35 1999/07/18 14:01:16 florian
  867. * handling of integer and shortint in case was wrong, if a case
  868. label was negative and a jump table was generated
  869. Revision 1.34 1999/06/08 15:27:24 pierre
  870. * fix for bug0258
  871. Revision 1.33 1999/06/02 10:11:48 florian
  872. * make cycle fixed i.e. compilation with 0.99.10
  873. * some fixes for qword
  874. * start of register calling conventions
  875. Revision 1.32 1999/05/27 19:44:19 peter
  876. * removed oldasm
  877. * plabel -> pasmlabel
  878. * -a switches to source writing automaticly
  879. * assembler readers OOPed
  880. * asmsymbol automaticly external
  881. * jumptables and other label fixes for asm readers
  882. Revision 1.31 1999/05/21 13:54:54 peter
  883. * NEWLAB for label as symbol
  884. Revision 1.30 1999/05/05 08:09:24 michael
  885. * Changed longword to cardinal
  886. Revision 1.29 1999/05/04 21:44:34 florian
  887. * changes to compile it with Delphi 4.0
  888. Revision 1.28 1999/05/01 13:24:15 peter
  889. * merged nasm compiler
  890. * old asm moved to oldasm/
  891. Revision 1.27 1999/04/16 13:42:30 jonas
  892. * more regalloc fixes (still not complete)
  893. Revision 1.26 1999/04/09 08:36:36 peter
  894. * fix also for -Og
  895. Revision 1.25 1999/04/08 20:59:37 florian
  896. * fixed problem with default properties which are a class
  897. * case bug (from the mailing list with -O2) fixed, the
  898. distance of the case labels can be greater than the positive
  899. range of a longint => it is now a dword for fpc
  900. Revision 1.24 1999/03/02 18:21:35 peter
  901. + flags support for add and case
  902. Revision 1.23 1999/02/25 21:02:31 peter
  903. * ag386bin updates
  904. + coff writer
  905. Revision 1.22 1999/02/22 02:15:16 peter
  906. * updates for ag386bin
  907. Revision 1.21 1999/02/17 10:12:59 peter
  908. * removed memory leak when jumps are generated
  909. Revision 1.20 1998/12/11 00:02:56 peter
  910. + globtype,tokens,version unit splitted from globals
  911. Revision 1.19 1998/10/09 08:56:25 pierre
  912. * several memory leaks fixed
  913. Revision 1.18 1998/10/08 17:17:14 pierre
  914. * current_module old scanner tagged as invalid if unit is recompiled
  915. + added ppheap for better info on tracegetmem of heaptrc
  916. (adds line column and file index)
  917. * several memory leaks removed ith help of heaptrc !!
  918. Revision 1.17 1998/09/17 09:42:20 peter
  919. + pass_2 for cg386
  920. * Message() -> CGMessage() for pass_1/pass_2
  921. Revision 1.16 1998/09/14 10:43:53 peter
  922. * all internal RTL functions start with FPC_
  923. Revision 1.15 1998/09/09 17:51:59 florian
  924. * the next try to fix the case problem ...
  925. Revision 1.14 1998/09/09 16:44:21 florian
  926. * I hope, the case bug is fixed now
  927. Revision 1.13 1998/09/07 18:45:54 peter
  928. * update smartlinking, uses getdatalabel
  929. * renamed ptree.value vars to value_str,value_real,value_set
  930. Revision 1.12 1998/09/05 23:51:05 florian
  931. * possible bug with too few registers in first/secondin fixed
  932. Revision 1.11 1998/09/04 08:41:41 peter
  933. * updated some error messages
  934. Revision 1.10 1998/09/03 17:08:40 pierre
  935. * better lines for stabs
  936. (no scroll back to if before else part
  937. no return to case line at jump outside case)
  938. + source lines also if not in order
  939. Revision 1.9 1998/08/28 10:54:19 peter
  940. * fixed smallset generation from elements, it has never worked before!
  941. Revision 1.8 1998/08/25 11:51:46 peter
  942. * fixed -15 seen as byte in case
  943. Revision 1.7 1998/08/19 16:07:38 jonas
  944. * changed optimizer switches + cleanup of DestroyRefs in daopt386.pas
  945. Revision 1.6 1998/08/18 09:24:39 pierre
  946. * small warning position bug fixed
  947. * support_mmx switches splitting was missing
  948. * rhide error and warning output corrected
  949. Revision 1.5 1998/08/14 18:18:40 peter
  950. + dynamic set contruction
  951. * smallsets are now working (always longint size)
  952. Revision 1.4 1998/08/10 14:49:51 peter
  953. + localswitches, moduleswitches, globalswitches splitting
  954. Revision 1.3 1998/06/25 08:48:10 florian
  955. * first version of rtti support
  956. Revision 1.2 1998/06/16 08:56:18 peter
  957. + targetcpu
  958. * cleaner pmodules for newppu
  959. Revision 1.1 1998/06/05 17:44:13 peter
  960. * splitted cgi386
  961. }