n386set.pas 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 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 n386set;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. node,nset,pass_1,ncgset;
  23. type
  24. ti386innode = class(tinnode)
  25. procedure pass_2;override;
  26. function pass_1 : tnode;override;
  27. end;
  28. ti386casenode = class(tcgcasenode)
  29. procedure optimizevalues(var max_linear_list:longint;var max_dist:cardinal);override;
  30. function has_jumptable : boolean;override;
  31. procedure genjumptable(hp : pcaserecord;min_,max_ : longint);override;
  32. procedure genlinearlist(hp : pcaserecord);override;
  33. end;
  34. implementation
  35. uses
  36. globtype,systems,
  37. verbose,globals,
  38. symconst,symdef,defutil,
  39. aasmbase,aasmtai,aasmcpu,
  40. cginfo,cgbase,pass_2,
  41. ncon,
  42. cpubase,cpuinfo,
  43. cga,cgx86,cgobj,tgobj,ncgutil,regvars,rgobj;
  44. {*****************************************************************************
  45. TI386INNODE
  46. *****************************************************************************}
  47. function ti386innode.pass_1 : tnode;
  48. begin
  49. result:=nil;
  50. { this is the only difference from the generic version }
  51. location.loc:=LOC_FLAGS;
  52. firstpass(right);
  53. firstpass(left);
  54. if codegenerror then
  55. exit;
  56. left_right_max;
  57. { this is not allways true due to optimization }
  58. { but if we don't set this we get problems with optimizing self code }
  59. if tsetdef(right.resulttype.def).settype<>smallset then
  60. procinfo.flags:=procinfo.flags or pi_do_call
  61. else
  62. begin
  63. { a smallset needs maybe an misc. register }
  64. if (left.nodetype<>ordconstn) and
  65. not(right.location.loc in [LOC_CREGISTER,LOC_REGISTER]) and
  66. (right.registers32<1) then
  67. inc(registers32);
  68. end;
  69. end;
  70. procedure ti386innode.pass_2;
  71. type
  72. Tsetpart=record
  73. range : boolean; {Part is a range.}
  74. start,stop : byte; {Start/stop when range; Stop=element when an element.}
  75. end;
  76. var
  77. genjumps,
  78. use_small,
  79. ranges : boolean;
  80. hr,hr2,
  81. pleftreg : tregister;
  82. href : treference;
  83. opsize : topsize;
  84. setparts : array[1..8] of Tsetpart;
  85. i,numparts : byte;
  86. adjustment : longint;
  87. pushedregs : tmaybesave;
  88. l,l2 : tasmlabel;
  89. r : Tregister;
  90. {$ifdef CORRECT_SET_IN_FPC}
  91. AM : tasmop;
  92. {$endif CORRECT_SET_IN_FPC}
  93. function analizeset(Aset:pconstset;is_small:boolean):boolean;
  94. type
  95. byteset=set of byte;
  96. var
  97. compares,maxcompares:word;
  98. i:byte;
  99. begin
  100. if byteset(Aset^)=[] then
  101. {The expression...
  102. if expr in []
  103. ...is allways false. It should be optimized away in the
  104. resulttype pass, and thus never occur here. Since we
  105. do generate wrong code for it, do internalerror.}
  106. internalerror(2002072301);
  107. analizeset:=false;
  108. ranges:=false;
  109. numparts:=0;
  110. compares:=0;
  111. { Lots of comparisions take a lot of time, so do not allow
  112. too much comparisions. 8 comparisions are, however, still
  113. smalller than emitting the set }
  114. if cs_littlesize in aktglobalswitches then
  115. maxcompares:=8
  116. else
  117. maxcompares:=5;
  118. { when smallset is possible allow only 3 compares the smallset
  119. code is for littlesize also smaller when more compares are used }
  120. if is_small then
  121. maxcompares:=3;
  122. for i:=0 to 255 do
  123. if i in byteset(Aset^) then
  124. begin
  125. if (numparts=0) or (i<>setparts[numparts].stop+1) then
  126. begin
  127. {Set element is a separate element.}
  128. inc(compares);
  129. if compares>maxcompares then
  130. exit;
  131. inc(numparts);
  132. setparts[numparts].range:=false;
  133. setparts[numparts].stop:=i;
  134. end
  135. else
  136. {Set element is part of a range.}
  137. if not setparts[numparts].range then
  138. begin
  139. {Transform an element into a range.}
  140. setparts[numparts].range:=true;
  141. setparts[numparts].start:=setparts[numparts].stop;
  142. setparts[numparts].stop:=i;
  143. ranges := true;
  144. { there's only one compare per range anymore. Only a }
  145. { sub is added, but that's much faster than a }
  146. { cmp/jcc combo so neglect its effect }
  147. { inc(compares);
  148. if compares>maxcompares then
  149. exit; }
  150. end
  151. else
  152. begin
  153. {Extend a range.}
  154. setparts[numparts].stop:=i;
  155. end;
  156. end;
  157. analizeset:=true;
  158. end;
  159. begin
  160. { We check first if we can generate jumps, this can be done
  161. because the resulttype.def is already set in firstpass }
  162. { check if we can use smallset operation using btl which is limited
  163. to 32 bits, the left side may also not contain higher values !! }
  164. use_small:=(tsetdef(right.resulttype.def).settype=smallset) and
  165. ((left.resulttype.def.deftype=orddef) and (torddef(left.resulttype.def).high<=32) or
  166. (left.resulttype.def.deftype=enumdef) and (tenumdef(left.resulttype.def).max<=32));
  167. { Can we generate jumps? Possible for all types of sets }
  168. genjumps:=(right.nodetype=setconstn) and
  169. analizeset(tsetconstnode(right).value_set,use_small);
  170. { calculate both operators }
  171. { the complex one first }
  172. firstcomplex(self);
  173. secondpass(left);
  174. { Only process the right if we are not generating jumps }
  175. if not genjumps then
  176. begin
  177. maybe_save(exprasmlist,right.registers32,left.location,pushedregs);
  178. secondpass(right);
  179. maybe_restore(exprasmlist,left.location,pushedregs);
  180. end;
  181. if codegenerror then
  182. exit;
  183. { ofcourse not commutative }
  184. if nf_swaped in flags then
  185. swapleftright;
  186. if genjumps then
  187. begin
  188. { It gives us advantage to check for the set elements
  189. separately instead of using the SET_IN_BYTE procedure.
  190. To do: Build in support for LOC_JUMP }
  191. opsize := def_opsize(left.resulttype.def);
  192. { If register is used, use only lower 8 bits }
  193. if left.location.loc in [LOC_REGISTER,LOC_CREGISTER] then
  194. begin
  195. { for ranges we always need a 32bit register, because then we }
  196. { use the register as base in a reference (JM) }
  197. if ranges then
  198. begin
  199. pleftreg:=rg.makeregsize(left.location.register,OS_INT);
  200. cg.a_load_reg_reg(exprasmlist,left.location.size,OS_INT,left.location.register,pleftreg);
  201. if opsize <> S_L then
  202. emit_const_reg(A_AND,S_L,255,pleftreg);
  203. opsize := S_L;
  204. end
  205. else
  206. { otherwise simply use the lower 8 bits (no "and" }
  207. { necessary this way) (JM) }
  208. begin
  209. pleftreg:=rg.makeregsize(left.location.register,OS_8);
  210. opsize := S_B;
  211. end;
  212. end
  213. else
  214. begin
  215. { load the value in a register }
  216. pleftreg := rg.getexplicitregisterint(exprasmlist,R_EDI);
  217. opsize := S_L;
  218. emit_ref_reg(A_MOVZX,S_BL,left.location.reference,pleftreg);
  219. end;
  220. { Get a label to jump to the end }
  221. location_reset(location,LOC_FLAGS,OS_NO);
  222. { It's better to use the zero flag when there are
  223. no ranges }
  224. if ranges then
  225. location.resflags:=F_C
  226. else
  227. location.resflags:=F_E;
  228. objectlibrary.getlabel(l);
  229. { how much have we already substracted from the x in the }
  230. { "x in [y..z]" expression }
  231. adjustment := 0;
  232. for i:=1 to numparts do
  233. if setparts[i].range then
  234. { use fact that a <= x <= b <=> cardinal(x-a) <= cardinal(b-a) }
  235. begin
  236. { is the range different from all legal values? }
  237. if (setparts[i].stop-setparts[i].start <> 255) then
  238. begin
  239. { yes, is the lower bound <> 0? }
  240. if (setparts[i].start <> 0) then
  241. { we're going to substract from the left register, }
  242. { so in case of a LOC_CREGISTER first move the value }
  243. { to edi (not done before because now we can do the }
  244. { move and substract in one instruction with LEA) }
  245. if (pleftreg.enum <> R_EDI) and
  246. (left.location.loc = LOC_CREGISTER) then
  247. begin
  248. r.enum:=R_EDI;
  249. rg.ungetregister(exprasmlist,pleftreg);
  250. rg.getexplicitregisterint(exprasmlist,R_EDI);
  251. reference_reset_base(href,pleftreg,-setparts[i].start);
  252. emit_ref_reg(A_LEA,S_L,href,r);
  253. { only now change pleftreg since previous value is }
  254. { still used in previous instruction }
  255. pleftreg := r;
  256. opsize := S_L;
  257. end
  258. else
  259. begin
  260. { otherwise, the value is already in a register }
  261. { that can be modified }
  262. if setparts[i].start-adjustment <> 1 then
  263. emit_const_reg(A_SUB,opsize,
  264. setparts[i].start-adjustment,pleftreg)
  265. else emit_reg(A_DEC,opsize,pleftreg);
  266. end;
  267. { new total value substracted from x: }
  268. { adjustment + (setparts[i].start - adjustment) }
  269. adjustment := setparts[i].start;
  270. { check if result < b-a+1 (not "result <= b-a", since }
  271. { we need a carry in case the element is in the range }
  272. { (this will never overflow since we check at the }
  273. { beginning whether stop-start <> 255) }
  274. emit_const_reg(A_CMP,opsize,
  275. setparts[i].stop-setparts[i].start+1,pleftreg);
  276. { use C_C instead of C_B: the meaning is the same, but }
  277. { then the optimizer can easier trace the jump to its }
  278. { final destination since the resultflag of this node }
  279. { is set to the carryflag }
  280. emitjmp(C_C,l);
  281. end
  282. else
  283. { if setparts[i].start = 0 and setparts[i].stop = 255, }
  284. { it's always true since "in" is only allowed for bytes }
  285. begin
  286. emit_none(A_STC,S_NO);
  287. cg.a_jmp_always(exprasmlist,l);
  288. end;
  289. end
  290. else
  291. begin
  292. { Emit code to check if left is an element }
  293. emit_const_reg(A_CMP,opsize,setparts[i].stop-adjustment,
  294. pleftreg);
  295. { Result should be in carry flag when ranges are used }
  296. if ranges then
  297. emit_none(A_STC,S_NO);
  298. { If found, jump to end }
  299. emitjmp(C_E,l);
  300. end;
  301. if ranges and
  302. { if the last one was a range, the carry flag is already }
  303. { set appropriately }
  304. not(setparts[numparts].range) then
  305. emit_none(A_CLC,S_NO);
  306. { To compensate for not doing a second pass }
  307. right.location.reference.symbol:=nil;
  308. { Now place the end label }
  309. cg.a_label(exprasmlist,l);
  310. case left.location.loc of
  311. LOC_REGISTER,
  312. LOC_CREGISTER :
  313. rg.ungetregister(exprasmlist,pleftreg);
  314. else
  315. begin
  316. reference_release(exprasmlist,left.location.reference);
  317. r.enum:=R_EDI;
  318. rg.ungetregister(exprasmlist,r);
  319. end;
  320. end;
  321. end
  322. else
  323. begin
  324. location_reset(location,LOC_FLAGS,OS_NO);
  325. { We will now generated code to check the set itself, no jmps,
  326. handle smallsets separate, because it allows faster checks }
  327. if use_small then
  328. begin
  329. if left.nodetype=ordconstn then
  330. begin
  331. location.resflags:=F_NE;
  332. case right.location.loc of
  333. LOC_REGISTER,
  334. LOC_CREGISTER:
  335. begin
  336. emit_const_reg(A_TEST,S_L,
  337. 1 shl (tordconstnode(left).value and 31),right.location.register);
  338. end;
  339. LOC_REFERENCE,
  340. LOC_CREFERENCE :
  341. begin
  342. emit_const_ref(A_TEST,S_L,1 shl (tordconstnode(left).value and 31),
  343. right.location.reference);
  344. end;
  345. else
  346. internalerror(200203312);
  347. end;
  348. location_release(exprasmlist,right.location);
  349. end
  350. else
  351. begin
  352. case left.location.loc of
  353. LOC_REGISTER,
  354. LOC_CREGISTER:
  355. begin
  356. hr:=rg.makeregsize(left.location.register,OS_INT);
  357. cg.a_load_reg_reg(exprasmlist,left.location.size,OS_INT,left.location.register,hr);
  358. end;
  359. else
  360. begin
  361. { the set element isn't never samller than a byte }
  362. { and because it's a small set we need only 5 bits }
  363. { but 8 bits are easier to load }
  364. r.enum:=R_EDI;
  365. rg.getexplicitregisterint(exprasmlist,R_EDI);
  366. emit_ref_reg(A_MOVZX,S_BL,left.location.reference,r);
  367. hr:=r;
  368. location_release(exprasmlist,left.location);
  369. end;
  370. end;
  371. case right.location.loc of
  372. LOC_REGISTER,
  373. LOC_CREGISTER :
  374. begin
  375. emit_reg_reg(A_BT,S_L,hr,
  376. right.location.register);
  377. rg.ungetregisterint(exprasmlist,right.location.register);
  378. end;
  379. LOC_CONSTANT :
  380. begin
  381. { We have to load the value into a register because
  382. btl does not accept values only refs or regs (PFV) }
  383. hr2:=rg.getregisterint(exprasmlist);
  384. emit_const_reg(A_MOV,S_L,
  385. right.location.value,hr2);
  386. emit_reg_reg(A_BT,S_L,hr,hr2);
  387. rg.ungetregisterint(exprasmlist,hr2);
  388. end;
  389. LOC_CREFERENCE,
  390. LOC_REFERENCE :
  391. begin
  392. location_release(exprasmlist,right.location);
  393. emit_reg_ref(A_BT,S_L,hr,right.location.reference);
  394. end;
  395. else
  396. internalerror(2002032210);
  397. end;
  398. { simply to indicate EDI is deallocated here too (JM) }
  399. rg.ungetregisterint(exprasmlist,hr);
  400. location.loc:=LOC_FLAGS;
  401. location.resflags:=F_C;
  402. end;
  403. end
  404. else
  405. begin
  406. if right.location.loc=LOC_CONSTANT then
  407. begin
  408. location.resflags:=F_C;
  409. objectlibrary.getlabel(l);
  410. objectlibrary.getlabel(l2);
  411. { Is this treated in firstpass ?? }
  412. if left.nodetype=ordconstn then
  413. begin
  414. hr:=rg.getregisterint(exprasmlist);
  415. left.location.loc:=LOC_REGISTER;
  416. left.location.register:=hr;
  417. emit_const_reg(A_MOV,S_L,
  418. tordconstnode(left).value,hr);
  419. end;
  420. case left.location.loc of
  421. LOC_REGISTER,
  422. LOC_CREGISTER:
  423. begin
  424. hr:=rg.makeregsize(left.location.register,OS_INT);
  425. cg.a_load_reg_reg(exprasmlist,left.location.size,OS_INT,left.location.register,hr);
  426. emit_const_reg(A_CMP,S_L,31,hr);
  427. emitjmp(C_NA,l);
  428. { reset carry flag }
  429. emit_none(A_CLC,S_NO);
  430. cg.a_jmp_always(exprasmlist,l2);
  431. cg.a_label(exprasmlist,l);
  432. { We have to load the value into a register because
  433. btl does not accept values only refs or regs (PFV) }
  434. hr2:=rg.getregisterint(exprasmlist);
  435. emit_const_reg(A_MOV,S_L,right.location.value,hr2);
  436. emit_reg_reg(A_BT,S_L,hr,hr2);
  437. rg.ungetregisterint(exprasmlist,hr2);
  438. end;
  439. else
  440. begin
  441. {$ifdef CORRECT_SET_IN_FPC}
  442. if m_tp in aktmodeswitches then
  443. begin
  444. {***WARNING only correct if
  445. reference is 32 bits (PM) *****}
  446. emit_const_ref(A_CMP,S_L,31,reference_copy(left.location.reference));
  447. end
  448. else
  449. {$endif CORRECT_SET_IN_FPC}
  450. begin
  451. emit_const_ref(A_CMP,S_B,31,left.location.reference);
  452. end;
  453. emitjmp(C_NA,l);
  454. { reset carry flag }
  455. emit_none(A_CLC,S_NO);
  456. cg.a_jmp_always(exprasmlist,l2);
  457. cg.a_label(exprasmlist,l);
  458. location_release(exprasmlist,left.location);
  459. hr:=rg.getregisterint(exprasmlist);
  460. emit_ref_reg(A_MOV,S_L,left.location.reference,hr);
  461. { We have to load the value into a register because
  462. btl does not accept values only refs or regs (PFV) }
  463. hr2:=rg.getregisterint(exprasmlist);
  464. emit_const_reg(A_MOV,S_L,
  465. right.location.value,hr2);
  466. emit_reg_reg(A_BT,S_L,hr,hr2);
  467. rg.ungetregisterint(exprasmlist,hr2);
  468. end;
  469. end;
  470. cg.a_label(exprasmlist,l2);
  471. end { of right.location.loc=LOC_CONSTANT }
  472. { do search in a normal set which could have >32 elementsm
  473. but also used if the left side contains higher values > 32 }
  474. else if left.nodetype=ordconstn then
  475. begin
  476. location.resflags:=F_NE;
  477. inc(right.location.reference.offset,tordconstnode(left).value shr 3);
  478. emit_const_ref(A_TEST,S_B,1 shl (tordconstnode(left).value and 7),right.location.reference);
  479. location_release(exprasmlist,right.location);
  480. end
  481. else
  482. begin
  483. if (left.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  484. pleftreg:=rg.makeregsize(left.location.register,OS_INT)
  485. else
  486. pleftreg:=rg.getexplicitregisterint(exprasmlist,R_EDI);
  487. cg.a_load_loc_reg(exprasmlist,left.location,pleftreg);
  488. location_freetemp(exprasmlist,left.location);
  489. location_release(exprasmlist,left.location);
  490. emit_reg_ref(A_BT,S_L,pleftreg,right.location.reference);
  491. rg.ungetregister(exprasmlist,pleftreg);
  492. location_release(exprasmlist,right.location);
  493. { tg.ungetiftemp(exprasmlist,right.location.reference) happens below }
  494. location.resflags:=F_C;
  495. end;
  496. end;
  497. end;
  498. location_freetemp(exprasmlist,right.location);
  499. end;
  500. {*****************************************************************************
  501. TI386CASENODE
  502. *****************************************************************************}
  503. procedure ti386casenode.optimizevalues(var max_linear_list:longint;var max_dist:cardinal);
  504. begin
  505. { a jump table crashes the pipeline! }
  506. if aktoptprocessor=Class386 then
  507. inc(max_linear_list,3)
  508. else if aktoptprocessor=ClassP5 then
  509. inc(max_linear_list,6)
  510. else if aktoptprocessor>=ClassP6 then
  511. inc(max_linear_list,9);
  512. end;
  513. function ti386casenode.has_jumptable : boolean;
  514. begin
  515. has_jumptable:=true;
  516. end;
  517. procedure ti386casenode.genjumptable(hp : pcaserecord;min_,max_ : longint);
  518. var
  519. table : tasmlabel;
  520. last : TConstExprInt;
  521. indexreg : tregister;
  522. href : treference;
  523. jumpsegment : TAAsmOutput;
  524. procedure genitem(t : pcaserecord);
  525. var
  526. i : longint;
  527. begin
  528. if assigned(t^.less) then
  529. genitem(t^.less);
  530. { fill possible hole }
  531. for i:=last+1 to t^._low-1 do
  532. jumpSegment.concat(Tai_const_symbol.Create(elselabel));
  533. for i:=t^._low to t^._high do
  534. jumpSegment.concat(Tai_const_symbol.Create(t^.statement));
  535. last:=t^._high;
  536. if assigned(t^.greater) then
  537. genitem(t^.greater);
  538. end;
  539. begin
  540. if (cs_create_smart in aktmoduleswitches) then
  541. jumpsegment:=procinfo.aktlocaldata
  542. else
  543. jumpsegment:=datasegment;
  544. if not(jumptable_no_range) then
  545. begin
  546. { case expr less than min_ => goto elselabel }
  547. cg.a_cmp_const_reg_label(exprasmlist,OS_INT,jmp_lt,aword(min_),hregister,elselabel);
  548. { case expr greater than max_ => goto elselabel }
  549. cg.a_cmp_const_reg_label(exprasmlist,OS_INT,jmp_gt,aword(max_),hregister,elselabel);
  550. end;
  551. objectlibrary.getlabel(table);
  552. { make it a 32bit register }
  553. indexreg:=rg.makeregsize(hregister,OS_INT);
  554. cg.a_load_reg_reg(exprasmlist,opsize,OS_INT,hregister,indexreg);
  555. { create reference }
  556. reference_reset_symbol(href,table,0);
  557. href.offset:=(-longint(min_))*4;
  558. href.index:=indexreg;
  559. href.scalefactor:=4;
  560. emit_ref(A_JMP,S_NO,href);
  561. { generate jump table }
  562. if not(cs_littlesize in aktglobalswitches) then
  563. jumpSegment.concat(Tai_Align.Create_Op(4,0));
  564. jumpSegment.concat(Tai_label.Create(table));
  565. last:=min_;
  566. genitem(hp);
  567. end;
  568. procedure ti386casenode.genlinearlist(hp : pcaserecord);
  569. var
  570. first : boolean;
  571. lastrange : boolean;
  572. last : TConstExprInt;
  573. cond_lt,cond_le : tasmcond;
  574. procedure genitem(t : pcaserecord);
  575. begin
  576. if assigned(t^.less) then
  577. genitem(t^.less);
  578. { need we to test the first value }
  579. if first and (t^._low>get_min_value(left.resulttype.def)) then
  580. begin
  581. cg.a_cmp_const_reg_label(exprasmlist,OS_INT,jmp_lt,aword(t^._low),hregister,elselabel);
  582. end;
  583. if t^._low=t^._high then
  584. begin
  585. if t^._low-last=0 then
  586. cg.a_cmp_const_reg_label(exprasmlist, OS_INT, OC_EQ,0,hregister,t^.statement)
  587. else
  588. begin
  589. cg.a_op_const_reg(exprasmlist, OP_SUB, aword(t^._low-last), hregister);
  590. emitjmp(C_Z,t^.statement);
  591. end;
  592. last:=t^._low;
  593. lastrange:=false;
  594. end
  595. else
  596. begin
  597. { it begins with the smallest label, if the value }
  598. { is even smaller then jump immediately to the }
  599. { ELSE-label }
  600. if first then
  601. begin
  602. { have we to ajust the first value ? }
  603. if (t^._low>get_min_value(left.resulttype.def)) then
  604. cg.a_op_const_reg(exprasmlist, OP_SUB, longint(t^._low), hregister);
  605. end
  606. else
  607. begin
  608. { if there is no unused label between the last and the }
  609. { present label then the lower limit can be checked }
  610. { immediately. else check the range in between: }
  611. cg.a_op_const_reg(exprasmlist, OP_SUB, longint(t^._low-last), hregister);
  612. { no jump necessary here if the new range starts at }
  613. { at the value following the previous one }
  614. if ((t^._low-last) <> 1) or
  615. (not lastrange) then
  616. emitjmp(cond_lt,elselabel);
  617. end;
  618. {we need to use A_SUB, because A_DEC does not set the correct flags, therefor
  619. using a_op_const_reg(OP_SUB) is not possible }
  620. emit_const_reg(A_SUB,TCGSize2OpSize[opsize],longint(t^._high-t^._low),hregister);
  621. emitjmp(cond_le,t^.statement);
  622. last:=t^._high;
  623. lastrange:=true;
  624. end;
  625. first:=false;
  626. if assigned(t^.greater) then
  627. genitem(t^.greater);
  628. end;
  629. begin
  630. if with_sign then
  631. begin
  632. cond_lt:=C_L;
  633. cond_le:=C_LE;
  634. end
  635. else
  636. begin
  637. cond_lt:=C_B;
  638. cond_le:=C_BE;
  639. end;
  640. { do we need to generate cmps? }
  641. if (with_sign and (min_label<0)) then
  642. genlinearcmplist(hp)
  643. else
  644. begin
  645. last:=0;
  646. lastrange:=false;
  647. first:=true;
  648. genitem(hp);
  649. cg.a_jmp_always(exprasmlist,elselabel);
  650. end;
  651. end;
  652. begin
  653. {$ifndef TEST_GENERIC}
  654. cinnode:=ti386innode;
  655. {$endif}
  656. ccasenode:=ti386casenode;
  657. end.
  658. {
  659. $Log$
  660. Revision 1.46 2003-01-08 18:43:57 daniel
  661. * Tregister changed into a record
  662. Revision 1.45 2002/11/25 17:43:27 peter
  663. * splitted defbase in defutil,symutil,defcmp
  664. * merged isconvertable and is_equal into compare_defs(_ext)
  665. * made operator search faster by walking the list only once
  666. Revision 1.44 2002/10/03 21:34:45 carl
  667. * range check error fixes
  668. Revision 1.43 2002/09/17 18:54:05 jonas
  669. * a_load_reg_reg() now has two size parameters: source and dest. This
  670. allows some optimizations on architectures that don't encode the
  671. register size in the register name.
  672. Revision 1.42 2002/09/16 18:08:26 peter
  673. * fix last optimization in genlinearlist, detected by bug tw1066
  674. * use generic casenode.pass2 routine and override genlinearlist
  675. * add jumptable support to generic casenode, by default there is
  676. no jumptable support
  677. Revision 1.41 2002/09/09 13:57:45 jonas
  678. * small optimization to case genlist() case statements
  679. Revision 1.40 2002/08/17 09:23:46 florian
  680. * first part of procinfo rewrite
  681. Revision 1.39 2002/08/12 15:08:42 carl
  682. + stab register indexes for powerpc (moved from gdb to cpubase)
  683. + tprocessor enumeration moved to cpuinfo
  684. + linker in target_info is now a class
  685. * many many updates for m68k (will soon start to compile)
  686. - removed some ifdef or correct them for correct cpu
  687. Revision 1.38 2002/08/11 14:32:30 peter
  688. * renamed current_library to objectlibrary
  689. Revision 1.37 2002/08/11 13:24:17 peter
  690. * saving of asmsymbols in ppu supported
  691. * asmsymbollist global is removed and moved into a new class
  692. tasmlibrarydata that will hold the info of a .a file which
  693. corresponds with a single module. Added librarydata to tmodule
  694. to keep the library info stored for the module. In the future the
  695. objectfiles will also be stored to the tasmlibrarydata class
  696. * all getlabel/newasmsymbol and friends are moved to the new class
  697. Revision 1.36 2002/07/23 14:31:00 daniel
  698. * Added internal error when asked to generate code for 'if expr in []'
  699. Revision 1.35 2002/07/20 11:58:04 florian
  700. * types.pas renamed to defbase.pas because D6 contains a types
  701. unit so this would conflicts if D6 programms are compiled
  702. + Willamette/SSE2 instructions to assembler added
  703. Revision 1.34 2002/07/11 14:41:34 florian
  704. * start of the new generic parameter handling
  705. Revision 1.33 2002/07/06 20:27:26 carl
  706. + generic set handling
  707. Revision 1.32 2002/07/01 18:46:33 peter
  708. * internal linker
  709. * reorganized aasm layer
  710. Revision 1.31 2002/05/18 13:34:25 peter
  711. * readded missing revisions
  712. Revision 1.30 2002/05/16 19:46:52 carl
  713. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  714. + try to fix temp allocation (still in ifdef)
  715. + generic constructor calls
  716. + start of tassembler / tmodulebase class cleanup
  717. Revision 1.28 2002/05/13 19:54:38 peter
  718. * removed n386ld and n386util units
  719. * maybe_save/maybe_restore added instead of the old maybe_push
  720. Revision 1.27 2002/05/12 16:53:17 peter
  721. * moved entry and exitcode to ncgutil and cgobj
  722. * foreach gets extra argument for passing local data to the
  723. iterator function
  724. * -CR checks also class typecasts at runtime by changing them
  725. into as
  726. * fixed compiler to cycle with the -CR option
  727. * fixed stabs with elf writer, finally the global variables can
  728. be watched
  729. * removed a lot of routines from cga unit and replaced them by
  730. calls to cgobj
  731. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  732. u32bit then the other is typecasted also to u32bit without giving
  733. a rangecheck warning/error.
  734. * fixed pascal calling method with reversing also the high tree in
  735. the parast, detected by tcalcst3 test
  736. Revision 1.26 2002/04/25 20:16:40 peter
  737. * moved more routines from cga/n386util
  738. Revision 1.25 2002/04/21 19:02:07 peter
  739. * removed newn and disposen nodes, the code is now directly
  740. inlined from pexpr
  741. * -an option that will write the secondpass nodes to the .s file, this
  742. requires EXTDEBUG define to actually write the info
  743. * fixed various internal errors and crashes due recent code changes
  744. Revision 1.24 2002/04/21 15:37:26 carl
  745. * changeregsize -> rg.makeregsize
  746. Revision 1.23 2002/04/19 15:39:35 peter
  747. * removed some more routines from cga
  748. * moved location_force_reg/mem to ncgutil
  749. * moved arrayconstructnode secondpass to ncgld
  750. Revision 1.22 2002/04/15 19:44:21 peter
  751. * fixed stackcheck that would be called recursively when a stack
  752. error was found
  753. * generic changeregsize(reg,size) for i386 register resizing
  754. * removed some more routines from cga unit
  755. * fixed returnvalue handling
  756. * fixed default stacksize of linux and go32v2, 8kb was a bit small :-)
  757. Revision 1.21 2002/04/02 17:11:36 peter
  758. * tlocation,treference update
  759. * LOC_CONSTANT added for better constant handling
  760. * secondadd splitted in multiple routines
  761. * location_force_reg added for loading a location to a register
  762. of a specified size
  763. * secondassignment parses now first the right and then the left node
  764. (this is compatible with Kylix). This saves a lot of push/pop especially
  765. with string operations
  766. * adapted some routines to use the new cg methods
  767. Revision 1.20 2002/03/31 20:26:39 jonas
  768. + a_loadfpu_* and a_loadmm_* methods in tcg
  769. * register allocation is now handled by a class and is mostly processor
  770. independent (+rgobj.pas and i386/rgcpu.pas)
  771. * temp allocation is now handled by a class (+tgobj.pas, -i386\tgcpu.pas)
  772. * some small improvements and fixes to the optimizer
  773. * some register allocation fixes
  774. * some fpuvaroffset fixes in the unary minus node
  775. * push/popusedregisters is now called rg.save/restoreusedregisters and
  776. (for i386) uses temps instead of push/pop's when using -Op3 (that code is
  777. also better optimizable)
  778. * fixed and optimized register saving/restoring for new/dispose nodes
  779. * LOC_FPU locations now also require their "register" field to be set to
  780. R_ST, not R_ST0 (the latter is used for LOC_CFPUREGISTER locations only)
  781. - list field removed of the tnode class because it's not used currently
  782. and can cause hard-to-find bugs
  783. }