cg386set.pas 34 KB

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