n386set.pas 37 KB

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