n386set.pas 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944
  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.registersint<1) then
  62. inc(registersint);
  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=ClassPentium then
  470. inc(max_linear_list,6)
  471. else if aktoptprocessor in [ClassPentium2,ClassPentium3] then
  472. inc(max_linear_list,9)
  473. else if aktoptprocessor=ClassPentium4 then
  474. inc(max_linear_list,14);
  475. end;
  476. function ti386casenode.has_jumptable : boolean;
  477. begin
  478. has_jumptable:=true;
  479. end;
  480. procedure ti386casenode.genjumptable(hp : pcaserecord;min_,max_ : longint);
  481. var
  482. table : tasmlabel;
  483. last : TConstExprInt;
  484. indexreg : tregister;
  485. href : treference;
  486. jumpsegment : TAAsmOutput;
  487. procedure genitem(t : pcaserecord);
  488. var
  489. i : longint;
  490. begin
  491. if assigned(t^.less) then
  492. genitem(t^.less);
  493. { fill possible hole }
  494. for i:=last+1 to t^._low-1 do
  495. jumpSegment.concat(Tai_const_symbol.Create(elselabel));
  496. for i:=t^._low to t^._high do
  497. jumpSegment.concat(Tai_const_symbol.Create(t^.statement));
  498. last:=t^._high;
  499. if assigned(t^.greater) then
  500. genitem(t^.greater);
  501. end;
  502. begin
  503. if (cs_create_smart in aktmoduleswitches) then
  504. jumpsegment:=current_procinfo.aktlocaldata
  505. else
  506. jumpsegment:=datasegment;
  507. if not(jumptable_no_range) then
  508. begin
  509. { case expr less than min_ => goto elselabel }
  510. cg.a_cmp_const_reg_label(exprasmlist,opsize,jmp_lt,aword(min_),hregister,elselabel);
  511. { case expr greater than max_ => goto elselabel }
  512. cg.a_cmp_const_reg_label(exprasmlist,opsize,jmp_gt,aword(max_),hregister,elselabel);
  513. end;
  514. objectlibrary.getlabel(table);
  515. { make it a 32bit register }
  516. indexreg:=cg.makeregsize(hregister,OS_INT);
  517. cg.a_load_reg_reg(exprasmlist,opsize,OS_INT,hregister,indexreg);
  518. { create reference }
  519. reference_reset_symbol(href,table,0);
  520. href.offset:=(-longint(min_))*4;
  521. href.index:=indexreg;
  522. href.scalefactor:=4;
  523. emit_ref(A_JMP,S_NO,href);
  524. { generate jump table }
  525. if not(cs_littlesize in aktglobalswitches) then
  526. jumpSegment.concat(Tai_Align.Create_Op(4,0));
  527. jumpSegment.concat(Tai_label.Create(table));
  528. last:=min_;
  529. genitem(hp);
  530. end;
  531. procedure ti386casenode.genlinearlist(hp : pcaserecord);
  532. var
  533. first : boolean;
  534. lastrange : boolean;
  535. last : TConstExprInt;
  536. cond_lt,cond_le : tresflags;
  537. procedure genitem(t : pcaserecord);
  538. begin
  539. if assigned(t^.less) then
  540. genitem(t^.less);
  541. { need we to test the first value }
  542. if first and (t^._low>get_min_value(left.resulttype.def)) then
  543. begin
  544. cg.a_cmp_const_reg_label(exprasmlist,opsize,jmp_lt,aword(t^._low),hregister,elselabel);
  545. end;
  546. if t^._low=t^._high then
  547. begin
  548. if t^._low-last=0 then
  549. cg.a_cmp_const_reg_label(exprasmlist, opsize, OC_EQ,0,hregister,t^.statement)
  550. else
  551. begin
  552. cg.a_op_const_reg(exprasmlist, OP_SUB, opsize, aword(t^._low-last), hregister);
  553. cg.a_jmp_flags(exprasmlist,F_E,t^.statement);
  554. end;
  555. last:=t^._low;
  556. lastrange:=false;
  557. end
  558. else
  559. begin
  560. { it begins with the smallest label, if the value }
  561. { is even smaller then jump immediately to the }
  562. { ELSE-label }
  563. if first then
  564. begin
  565. { have we to ajust the first value ? }
  566. if (t^._low>get_min_value(left.resulttype.def)) then
  567. cg.a_op_const_reg(exprasmlist, OP_SUB, opsize, longint(t^._low), hregister);
  568. end
  569. else
  570. begin
  571. { if there is no unused label between the last and the }
  572. { present label then the lower limit can be checked }
  573. { immediately. else check the range in between: }
  574. cg.a_op_const_reg(exprasmlist, OP_SUB, opsize, longint(t^._low-last), hregister);
  575. { no jump necessary here if the new range starts at }
  576. { at the value following the previous one }
  577. if ((t^._low-last) <> 1) or
  578. (not lastrange) then
  579. cg.a_jmp_flags(exprasmlist,cond_lt,elselabel);
  580. end;
  581. {we need to use A_SUB, because A_DEC does not set the correct flags, therefor
  582. using a_op_const_reg(OP_SUB) is not possible }
  583. emit_const_reg(A_SUB,TCGSize2OpSize[opsize],longint(t^._high-t^._low),hregister);
  584. cg.a_jmp_flags(exprasmlist,cond_le,t^.statement);
  585. last:=t^._high;
  586. lastrange:=true;
  587. end;
  588. first:=false;
  589. if assigned(t^.greater) then
  590. genitem(t^.greater);
  591. end;
  592. begin
  593. if with_sign then
  594. begin
  595. cond_lt:=F_L;
  596. cond_le:=F_LE;
  597. end
  598. else
  599. begin
  600. cond_lt:=F_B;
  601. cond_le:=F_BE;
  602. end;
  603. { do we need to generate cmps? }
  604. if (with_sign and (min_label<0)) then
  605. genlinearcmplist(hp)
  606. else
  607. begin
  608. last:=0;
  609. lastrange:=false;
  610. first:=true;
  611. genitem(hp);
  612. cg.a_jmp_always(exprasmlist,elselabel);
  613. end;
  614. end;
  615. begin
  616. {$ifndef TEST_GENERIC}
  617. cinnode:=ti386innode;
  618. {$endif}
  619. ccasenode:=ti386casenode;
  620. end.
  621. {
  622. $Log$
  623. Revision 1.71 2004-02-03 22:32:54 peter
  624. * renamed xNNbittype to xNNinttype
  625. * renamed registers32 to registersint
  626. * replace some s32bit,u32bit with torddef([su]inttype).def.typ
  627. Revision 1.70 2003/11/07 15:58:32 florian
  628. * Florian's culmutative nr. 1; contains:
  629. - invalid calling conventions for a certain cpu are rejected
  630. - arm softfloat calling conventions
  631. - -Sp for cpu dependend code generation
  632. - several arm fixes
  633. - remaining code for value open array paras on heap
  634. Revision 1.69 2003/10/10 17:48:14 peter
  635. * old trgobj moved to x86/rgcpu and renamed to trgx86fpu
  636. * tregisteralloctor renamed to trgobj
  637. * removed rgobj from a lot of units
  638. * moved location_* and reference_* to cgobj
  639. * first things for mmx register allocation
  640. Revision 1.68 2003/10/09 21:31:37 daniel
  641. * Register allocator splitted, ans abstract now
  642. Revision 1.67 2003/10/01 20:34:49 peter
  643. * procinfo unit contains tprocinfo
  644. * cginfo renamed to cgbase
  645. * moved cgmessage to verbose
  646. * fixed ppc and sparc compiles
  647. Revision 1.66 2003/09/28 21:48:20 peter
  648. * fix register leaks
  649. Revision 1.65 2003/09/07 22:09:35 peter
  650. * preparations for different default calling conventions
  651. * various RA fixes
  652. Revision 1.64 2003/09/05 11:21:39 marco
  653. * applied Peter's patch. Now cycles.
  654. Revision 1.63 2003/09/03 15:55:01 peter
  655. * NEWRA branch merged
  656. Revision 1.62.2.1 2003/08/29 17:29:00 peter
  657. * next batch of updates
  658. Revision 1.62 2003/06/12 22:10:44 jonas
  659. * t386innode.pass_2 already doesn't call a helper anymore since a long
  660. time
  661. Revision 1.61 2003/06/03 21:11:09 peter
  662. * cg.a_load_* get a from and to size specifier
  663. * makeregsize only accepts newregister
  664. * i386 uses generic tcgnotnode,tcgunaryminus
  665. Revision 1.60 2003/06/01 21:38:06 peter
  666. * getregisterfpu size parameter added
  667. * op_const_reg size parameter added
  668. * sparc updates
  669. Revision 1.59 2003/05/31 15:04:31 peter
  670. * load_loc_reg update
  671. Revision 1.58 2003/05/22 21:32:29 peter
  672. * removed some unit dependencies
  673. Revision 1.57 2003/04/27 11:21:35 peter
  674. * aktprocdef renamed to current_procdef
  675. * procinfo renamed to current_procinfo
  676. * procinfo will now be stored in current_module so it can be
  677. cleaned up properly
  678. * gen_main_procsym changed to create_main_proc and release_main_proc
  679. to also generate a tprocinfo structure
  680. * fixed unit implicit initfinal
  681. Revision 1.56 2003/04/25 08:25:26 daniel
  682. * Ifdefs around a lot of calls to cleartempgen
  683. * Fixed registers that are allocated but not freed in several nodes
  684. * Tweak to register allocator to cause less spills
  685. * 8-bit registers now interfere with esi,edi and ebp
  686. Compiler can now compile rtl successfully when using new register
  687. allocator
  688. Revision 1.55 2003/04/23 09:51:16 daniel
  689. * Removed usage of edi in a lot of places when new register allocator used
  690. + Added newra versions of g_concatcopy and secondadd_float
  691. Revision 1.54 2003/04/22 23:50:23 peter
  692. * firstpass uses expectloc
  693. * checks if there are differences between the expectloc and
  694. location.loc from secondpass in EXTDEBUG
  695. Revision 1.53 2003/04/22 14:33:38 peter
  696. * removed some notes/hints
  697. Revision 1.52 2003/04/22 10:09:35 daniel
  698. + Implemented the actual register allocator
  699. + Scratch registers unavailable when new register allocator used
  700. + maybe_save/maybe_restore unavailable when new register allocator used
  701. Revision 1.51 2003/03/13 19:52:23 jonas
  702. * and more new register allocator fixes (in the i386 code generator this
  703. time). At least now the ppc cross compiler can compile the linux
  704. system unit again, but I haven't tested it.
  705. Revision 1.50 2003/02/26 23:06:13 daniel
  706. * Fixed an illegal use of makeregsize
  707. Revision 1.49 2003/02/19 22:39:56 daniel
  708. * Fixed a few issues
  709. Revision 1.48 2003/02/19 22:00:15 daniel
  710. * Code generator converted to new register notation
  711. - Horribily outdated todo.txt removed
  712. Revision 1.47 2003/01/13 14:54:34 daniel
  713. * Further work to convert codegenerator register convention;
  714. internalerror bug fixed.
  715. Revision 1.46 2003/01/08 18:43:57 daniel
  716. * Tregister changed into a record
  717. Revision 1.45 2002/11/25 17:43:27 peter
  718. * splitted defbase in defutil,symutil,defcmp
  719. * merged isconvertable and is_equal into compare_defs(_ext)
  720. * made operator search faster by walking the list only once
  721. Revision 1.44 2002/10/03 21:34:45 carl
  722. * range check error fixes
  723. Revision 1.43 2002/09/17 18:54:05 jonas
  724. * a_load_reg_reg() now has two size parameters: source and dest. This
  725. allows some optimizations on architectures that don't encode the
  726. register size in the register name.
  727. Revision 1.42 2002/09/16 18:08:26 peter
  728. * fix last optimization in genlinearlist, detected by bug tw1066
  729. * use generic casenode.pass2 routine and override genlinearlist
  730. * add jumptable support to generic casenode, by default there is
  731. no jumptable support
  732. Revision 1.41 2002/09/09 13:57:45 jonas
  733. * small optimization to case genlist() case statements
  734. Revision 1.40 2002/08/17 09:23:46 florian
  735. * first part of procinfo rewrite
  736. Revision 1.39 2002/08/12 15:08:42 carl
  737. + stab register indexes for powerpc (moved from gdb to cpubase)
  738. + tprocessor enumeration moved to cpuinfo
  739. + linker in target_info is now a class
  740. * many many updates for m68k (will soon start to compile)
  741. - removed some ifdef or correct them for correct cpu
  742. Revision 1.38 2002/08/11 14:32:30 peter
  743. * renamed current_library to objectlibrary
  744. Revision 1.37 2002/08/11 13:24:17 peter
  745. * saving of asmsymbols in ppu supported
  746. * asmsymbollist global is removed and moved into a new class
  747. tasmlibrarydata that will hold the info of a .a file which
  748. corresponds with a single module. Added librarydata to tmodule
  749. to keep the library info stored for the module. In the future the
  750. objectfiles will also be stored to the tasmlibrarydata class
  751. * all getlabel/newasmsymbol and friends are moved to the new class
  752. Revision 1.36 2002/07/23 14:31:00 daniel
  753. * Added internal error when asked to generate code for 'if expr in []'
  754. Revision 1.35 2002/07/20 11:58:04 florian
  755. * types.pas renamed to defbase.pas because D6 contains a types
  756. unit so this would conflicts if D6 programms are compiled
  757. + Willamette/SSE2 instructions to assembler added
  758. Revision 1.34 2002/07/11 14:41:34 florian
  759. * start of the new generic parameter handling
  760. Revision 1.33 2002/07/06 20:27:26 carl
  761. + generic set handling
  762. Revision 1.32 2002/07/01 18:46:33 peter
  763. * internal linker
  764. * reorganized aasm layer
  765. Revision 1.31 2002/05/18 13:34:25 peter
  766. * readded missing revisions
  767. Revision 1.30 2002/05/16 19:46:52 carl
  768. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  769. + try to fix temp allocation (still in ifdef)
  770. + generic constructor calls
  771. + start of tassembler / tmodulebase class cleanup
  772. Revision 1.28 2002/05/13 19:54:38 peter
  773. * removed n386ld and n386util units
  774. * maybe_save/maybe_restore added instead of the old maybe_push
  775. Revision 1.27 2002/05/12 16:53:17 peter
  776. * moved entry and exitcode to ncgutil and cgobj
  777. * foreach gets extra argument for passing local data to the
  778. iterator function
  779. * -CR checks also class typecasts at runtime by changing them
  780. into as
  781. * fixed compiler to cycle with the -CR option
  782. * fixed stabs with elf writer, finally the global variables can
  783. be watched
  784. * removed a lot of routines from cga unit and replaced them by
  785. calls to cgobj
  786. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  787. u32bit then the other is typecasted also to u32bit without giving
  788. a rangecheck warning/error.
  789. * fixed pascal calling method with reversing also the high tree in
  790. the parast, detected by tcalcst3 test
  791. Revision 1.26 2002/04/25 20:16:40 peter
  792. * moved more routines from cga/n386util
  793. Revision 1.25 2002/04/21 19:02:07 peter
  794. * removed newn and disposen nodes, the code is now directly
  795. inlined from pexpr
  796. * -an option that will write the secondpass nodes to the .s file, this
  797. requires EXTDEBUG define to actually write the info
  798. * fixed various internal errors and crashes due recent code changes
  799. Revision 1.24 2002/04/21 15:37:26 carl
  800. * changeregsize -> rg.makeregsize
  801. Revision 1.23 2002/04/19 15:39:35 peter
  802. * removed some more routines from cga
  803. * moved location_force_reg/mem to ncgutil
  804. * moved arrayconstructnode secondpass to ncgld
  805. Revision 1.22 2002/04/15 19:44:21 peter
  806. * fixed stackcheck that would be called recursively when a stack
  807. error was found
  808. * generic changeregsize(reg,size) for i386 register resizing
  809. * removed some more routines from cga unit
  810. * fixed returnvalue handling
  811. * fixed default stacksize of linux and go32v2, 8kb was a bit small :-)
  812. Revision 1.21 2002/04/02 17:11:36 peter
  813. * tlocation,treference update
  814. * LOC_CONSTANT added for better constant handling
  815. * secondadd splitted in multiple routines
  816. * location_force_reg added for loading a location to a register
  817. of a specified size
  818. * secondassignment parses now first the right and then the left node
  819. (this is compatible with Kylix). This saves a lot of push/pop especially
  820. with string operations
  821. * adapted some routines to use the new cg methods
  822. Revision 1.20 2002/03/31 20:26:39 jonas
  823. + a_loadfpu_* and a_loadmm_* methods in tcg
  824. * register allocation is now handled by a class and is mostly processor
  825. independent (+rgobj.pas and i386/rgcpu.pas)
  826. * temp allocation is now handled by a class (+tgobj.pas, -i386\tgcpu.pas)
  827. * some small improvements and fixes to the optimizer
  828. * some register allocation fixes
  829. * some fpuvaroffset fixes in the unary minus node
  830. * push/popusedregisters is now called rg.save/restoreusedregisters and
  831. (for i386) uses temps instead of push/pop's when using -Op3 (that code is
  832. also better optimizable)
  833. * fixed and optimized register saving/restoring for new/dispose nodes
  834. * LOC_FPU locations now also require their "register" field to be set to
  835. R_ST, not R_ST0 (the latter is used for LOC_CFPUREGISTER locations only)
  836. - list field removed of the tnode class because it's not used currently
  837. and can cause hard-to-find bugs
  838. }