n386set.pas 37 KB

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