n386set.pas 37 KB

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