n386set.pas 35 KB

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