n386set.pas 38 KB

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