n386set.pas 36 KB

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