n386set.pas 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110
  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;
  23. type
  24. ti386innode = class(tinnode)
  25. procedure pass_2;override;
  26. function pass_1 : tnode;override;
  27. end;
  28. ti386casenode = class(tcasenode)
  29. procedure pass_2;override;
  30. end;
  31. implementation
  32. uses
  33. globtype,systems,
  34. verbose,globals,
  35. symconst,symdef,types,
  36. aasmbase,aasmtai,aasmcpu,
  37. cginfo,cgbase,pass_2,
  38. ncon,
  39. cpubase,
  40. cga,cgobj,tgobj,ncgutil,regvars,rgobj;
  41. const
  42. bytes2Sxx:array[1..8] of Topsize=(S_B,S_W,S_NO,S_L,S_NO,S_NO,S_NO,S_Q);
  43. {*****************************************************************************
  44. TI386INNODE
  45. *****************************************************************************}
  46. function ti386innode.pass_1 : tnode;
  47. begin
  48. result:=nil;
  49. { this is the only difference from the generic version }
  50. location.loc:=LOC_FLAGS;
  51. firstpass(right);
  52. firstpass(left);
  53. if codegenerror then
  54. exit;
  55. left_right_max;
  56. { this is not allways true due to optimization }
  57. { but if we don't set this we get problems with optimizing self code }
  58. if tsetdef(right.resulttype.def).settype<>smallset then
  59. procinfo^.flags:=procinfo^.flags or pi_do_call
  60. else
  61. begin
  62. { a smallset needs maybe an misc. register }
  63. if (left.nodetype<>ordconstn) and
  64. not(right.location.loc in [LOC_CREGISTER,LOC_REGISTER]) and
  65. (right.registers32<1) then
  66. inc(registers32);
  67. end;
  68. end;
  69. procedure ti386innode.pass_2;
  70. type
  71. Tsetpart=record
  72. range : boolean; {Part is a range.}
  73. start,stop : byte; {Start/stop when range; Stop=element when an element.}
  74. end;
  75. var
  76. genjumps,
  77. use_small,
  78. ranges : boolean;
  79. hr,hr2,
  80. pleftreg : tregister;
  81. href : treference;
  82. opsize : topsize;
  83. setparts : array[1..8] of Tsetpart;
  84. i,numparts : byte;
  85. adjustment : longint;
  86. pushedregs : tmaybesave;
  87. l,l2 : tasmlabel;
  88. {$ifdef CORRECT_SET_IN_FPC}
  89. AM : tasmop;
  90. {$endif CORRECT_SET_IN_FPC}
  91. function analizeset(Aset:pconstset;is_small:boolean):boolean;
  92. type
  93. byteset=set of byte;
  94. var
  95. compares,maxcompares:word;
  96. i:byte;
  97. begin
  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 byteset(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. maybe_save(exprasmlist,right.registers32,left.location,pushedregs);
  169. secondpass(right);
  170. maybe_restore(exprasmlist,left.location,pushedregs);
  171. end;
  172. if codegenerror then
  173. exit;
  174. { ofcourse not commutative }
  175. if nf_swaped in flags then
  176. swapleftright;
  177. if genjumps then
  178. begin
  179. { It gives us advantage to check for the set elements
  180. separately instead of using the SET_IN_BYTE procedure.
  181. To do: Build in support for LOC_JUMP }
  182. opsize := def_opsize(left.resulttype.def);
  183. { If register is used, use only lower 8 bits }
  184. if left.location.loc in [LOC_REGISTER,LOC_CREGISTER] then
  185. begin
  186. { for ranges we always need a 32bit register, because then we }
  187. { use the register as base in a reference (JM) }
  188. if ranges then
  189. begin
  190. pleftreg:=rg.makeregsize(left.location.register,OS_INT);
  191. cg.a_load_reg_reg(exprasmlist,left.location.size,left.location.register,pleftreg);
  192. if opsize <> S_L then
  193. emit_const_reg(A_AND,S_L,255,pleftreg);
  194. opsize := S_L;
  195. end
  196. else
  197. { otherwise simply use the lower 8 bits (no "and" }
  198. { necessary this way) (JM) }
  199. begin
  200. pleftreg:=rg.makeregsize(left.location.register,OS_8);
  201. opsize := S_B;
  202. end;
  203. end
  204. else
  205. begin
  206. { load the value in a register }
  207. pleftreg := rg.getexplicitregisterint(exprasmlist,R_EDI);
  208. opsize := S_L;
  209. emit_ref_reg(A_MOVZX,S_BL,left.location.reference,pleftreg);
  210. end;
  211. { Get a label to jump to the end }
  212. location_reset(location,LOC_FLAGS,OS_NO);
  213. { It's better to use the zero flag when there are
  214. no ranges }
  215. if ranges then
  216. location.resflags:=F_C
  217. else
  218. location.resflags:=F_E;
  219. getlabel(l);
  220. { how much have we already substracted from the x in the }
  221. { "x in [y..z]" expression }
  222. adjustment := 0;
  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 (pleftreg <> R_EDI) and
  237. (left.location.loc = LOC_CREGISTER) then
  238. begin
  239. rg.ungetregister(exprasmlist,pleftreg);
  240. rg.getexplicitregisterint(exprasmlist,R_EDI);
  241. reference_reset_base(href,pleftreg,-setparts[i].start);
  242. emit_ref_reg(A_LEA,S_L,href,R_EDI);
  243. { only now change pleftreg since previous value is }
  244. { still used in previous instruction }
  245. pleftreg := R_EDI;
  246. opsize := S_L;
  247. end
  248. else
  249. begin
  250. { otherwise, the value is already in a register }
  251. { that can be modified }
  252. if setparts[i].start-adjustment <> 1 then
  253. emit_const_reg(A_SUB,opsize,
  254. setparts[i].start-adjustment,pleftreg)
  255. else emit_reg(A_DEC,opsize,pleftreg);
  256. end;
  257. { new total value substracted from x: }
  258. { adjustment + (setparts[i].start - adjustment) }
  259. adjustment := setparts[i].start;
  260. { check if result < b-a+1 (not "result <= b-a", since }
  261. { we need a carry in case the element is in the range }
  262. { (this will never overflow since we check at the }
  263. { beginning whether stop-start <> 255) }
  264. emit_const_reg(A_CMP,opsize,
  265. setparts[i].stop-setparts[i].start+1,pleftreg);
  266. { use C_C instead of C_B: the meaning is the same, but }
  267. { then the optimizer can easier trace the jump to its }
  268. { final destination since the resultflag of this node }
  269. { is set to the carryflag }
  270. emitjmp(C_C,l);
  271. end
  272. else
  273. { if setparts[i].start = 0 and setparts[i].stop = 255, }
  274. { it's always true since "in" is only allowed for bytes }
  275. begin
  276. emit_none(A_STC,S_NO);
  277. cg.a_jmp_always(exprasmlist,l);
  278. end;
  279. end
  280. else
  281. begin
  282. { Emit code to check if left is an element }
  283. emit_const_reg(A_CMP,opsize,setparts[i].stop-adjustment,
  284. pleftreg);
  285. { Result should be in carry flag when ranges are used }
  286. if ranges then
  287. emit_none(A_STC,S_NO);
  288. { If found, jump to end }
  289. emitjmp(C_E,l);
  290. end;
  291. if ranges and
  292. { if the last one was a range, the carry flag is already }
  293. { set appropriately }
  294. not(setparts[numparts].range) then
  295. emit_none(A_CLC,S_NO);
  296. { To compensate for not doing a second pass }
  297. right.location.reference.symbol:=nil;
  298. { Now place the end label }
  299. cg.a_label(exprasmlist,l);
  300. case left.location.loc of
  301. LOC_REGISTER,
  302. LOC_CREGISTER :
  303. rg.ungetregister(exprasmlist,pleftreg);
  304. else
  305. begin
  306. reference_release(exprasmlist,left.location.reference);
  307. rg.ungetregister(exprasmlist,R_EDI);
  308. end;
  309. end;
  310. end
  311. else
  312. begin
  313. location_reset(location,LOC_FLAGS,OS_NO);
  314. { We will now generated code to check the set itself, no jmps,
  315. handle smallsets separate, because it allows faster checks }
  316. if use_small then
  317. begin
  318. if left.nodetype=ordconstn then
  319. begin
  320. location.resflags:=F_NE;
  321. case right.location.loc of
  322. LOC_REGISTER,
  323. LOC_CREGISTER:
  324. begin
  325. emit_const_reg(A_TEST,S_L,
  326. 1 shl (tordconstnode(left).value and 31),right.location.register);
  327. end;
  328. LOC_REFERENCE,
  329. LOC_CREFERENCE :
  330. begin
  331. emit_const_ref(A_TEST,S_L,1 shl (tordconstnode(left).value and 31),
  332. right.location.reference);
  333. end;
  334. else
  335. internalerror(200203312);
  336. end;
  337. location_release(exprasmlist,right.location);
  338. end
  339. else
  340. begin
  341. case left.location.loc of
  342. LOC_REGISTER,
  343. LOC_CREGISTER:
  344. begin
  345. hr:=rg.makeregsize(left.location.register,OS_INT);
  346. cg.a_load_reg_reg(exprasmlist,left.location.size,left.location.register,hr);
  347. end;
  348. else
  349. begin
  350. { the set element isn't never samller than a byte }
  351. { and because it's a small set we need only 5 bits }
  352. { but 8 bits are easier to load }
  353. rg.getexplicitregisterint(exprasmlist,R_EDI);
  354. emit_ref_reg(A_MOVZX,S_BL,left.location.reference,R_EDI);
  355. hr:=R_EDI;
  356. location_release(exprasmlist,left.location);
  357. end;
  358. end;
  359. case right.location.loc of
  360. LOC_REGISTER,
  361. LOC_CREGISTER :
  362. begin
  363. emit_reg_reg(A_BT,S_L,hr,
  364. right.location.register);
  365. rg.ungetregisterint(exprasmlist,right.location.register);
  366. end;
  367. LOC_CONSTANT :
  368. begin
  369. { We have to load the value into a register because
  370. btl does not accept values only refs or regs (PFV) }
  371. hr2:=rg.getregisterint(exprasmlist);
  372. emit_const_reg(A_MOV,S_L,
  373. right.location.value,hr2);
  374. emit_reg_reg(A_BT,S_L,hr,hr2);
  375. rg.ungetregisterint(exprasmlist,hr2);
  376. end;
  377. LOC_CREFERENCE,
  378. LOC_REFERENCE :
  379. begin
  380. location_release(exprasmlist,right.location);
  381. emit_reg_ref(A_BT,S_L,hr,right.location.reference);
  382. end;
  383. else
  384. internalerror(2002032210);
  385. end;
  386. { simply to indicate EDI is deallocated here too (JM) }
  387. rg.ungetregisterint(exprasmlist,hr);
  388. location.loc:=LOC_FLAGS;
  389. location.resflags:=F_C;
  390. end;
  391. end
  392. else
  393. begin
  394. if right.location.loc=LOC_CONSTANT then
  395. begin
  396. location.resflags:=F_C;
  397. getlabel(l);
  398. getlabel(l2);
  399. { Is this treated in firstpass ?? }
  400. if left.nodetype=ordconstn then
  401. begin
  402. hr:=rg.getregisterint(exprasmlist);
  403. left.location.loc:=LOC_REGISTER;
  404. left.location.register:=hr;
  405. emit_const_reg(A_MOV,S_L,
  406. tordconstnode(left).value,hr);
  407. end;
  408. case left.location.loc of
  409. LOC_REGISTER,
  410. LOC_CREGISTER:
  411. begin
  412. hr:=rg.makeregsize(left.location.register,OS_INT);
  413. cg.a_load_reg_reg(exprasmlist,left.location.size,left.location.register,hr);
  414. emit_const_reg(A_CMP,S_L,31,hr);
  415. emitjmp(C_NA,l);
  416. { reset carry flag }
  417. emit_none(A_CLC,S_NO);
  418. cg.a_jmp_always(exprasmlist,l2);
  419. cg.a_label(exprasmlist,l);
  420. { We have to load the value into a register because
  421. btl does not accept values only refs or regs (PFV) }
  422. hr2:=rg.getregisterint(exprasmlist);
  423. emit_const_reg(A_MOV,S_L,right.location.value,hr2);
  424. emit_reg_reg(A_BT,S_L,hr,hr2);
  425. rg.ungetregisterint(exprasmlist,hr2);
  426. end;
  427. else
  428. begin
  429. {$ifdef CORRECT_SET_IN_FPC}
  430. if m_tp in aktmodeswitches then
  431. begin
  432. {***WARNING only correct if
  433. reference is 32 bits (PM) *****}
  434. emit_const_ref(A_CMP,S_L,31,reference_copy(left.location.reference));
  435. end
  436. else
  437. {$endif CORRECT_SET_IN_FPC}
  438. begin
  439. emit_const_ref(A_CMP,S_B,31,left.location.reference);
  440. end;
  441. emitjmp(C_NA,l);
  442. { reset carry flag }
  443. emit_none(A_CLC,S_NO);
  444. cg.a_jmp_always(exprasmlist,l2);
  445. cg.a_label(exprasmlist,l);
  446. location_release(exprasmlist,left.location);
  447. hr:=rg.getregisterint(exprasmlist);
  448. emit_ref_reg(A_MOV,S_L,left.location.reference,hr);
  449. { We have to load the value into a register because
  450. btl does not accept values only refs or regs (PFV) }
  451. hr2:=rg.getregisterint(exprasmlist);
  452. emit_const_reg(A_MOV,S_L,
  453. right.location.value,hr2);
  454. emit_reg_reg(A_BT,S_L,hr,hr2);
  455. rg.ungetregisterint(exprasmlist,hr2);
  456. end;
  457. end;
  458. cg.a_label(exprasmlist,l2);
  459. end { of right.location.loc=LOC_CONSTANT }
  460. { do search in a normal set which could have >32 elementsm
  461. but also used if the left side contains higher values > 32 }
  462. else if left.nodetype=ordconstn then
  463. begin
  464. location.resflags:=F_NE;
  465. inc(right.location.reference.offset,tordconstnode(left).value shr 3);
  466. emit_const_ref(A_TEST,S_B,1 shl (tordconstnode(left).value and 7),right.location.reference);
  467. location_release(exprasmlist,right.location);
  468. end
  469. else
  470. begin
  471. if (left.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  472. pleftreg:=rg.makeregsize(left.location.register,OS_INT)
  473. else
  474. pleftreg:=rg.getexplicitregisterint(exprasmlist,R_EDI);
  475. cg.a_load_loc_reg(exprasmlist,left.location,pleftreg);
  476. location_freetemp(exprasmlist,left.location);
  477. location_release(exprasmlist,left.location);
  478. emit_reg_ref(A_BT,S_L,pleftreg,right.location.reference);
  479. rg.ungetregister(exprasmlist,pleftreg);
  480. location_release(exprasmlist,right.location);
  481. { tg.ungetiftemp(exprasmlist,right.location.reference) happens below }
  482. location.resflags:=F_C;
  483. end;
  484. end;
  485. end;
  486. location_freetemp(exprasmlist,right.location);
  487. end;
  488. {*****************************************************************************
  489. TI386CASENODE
  490. *****************************************************************************}
  491. procedure ti386casenode.pass_2;
  492. var
  493. with_sign : boolean;
  494. opsize : topsize;
  495. jmp_gt,jmp_le,jmp_lee : tasmcond;
  496. hp : tnode;
  497. { register with case expression }
  498. hregister,hregister2 : tregister;
  499. endlabel,elselabel : tasmlabel;
  500. { true, if we can omit the range check of the jump table }
  501. jumptable_no_range : boolean;
  502. { where to put the jump table }
  503. jumpsegment : TAAsmoutput;
  504. min_label : TConstExprInt;
  505. procedure gentreejmp(p : pcaserecord);
  506. var
  507. lesslabel,greaterlabel : tasmlabel;
  508. begin
  509. cg.a_label(exprasmlist,p^._at);
  510. { calculate labels for left and right }
  511. if (p^.less=nil) then
  512. lesslabel:=elselabel
  513. else
  514. lesslabel:=p^.less^._at;
  515. if (p^.greater=nil) then
  516. greaterlabel:=elselabel
  517. else
  518. greaterlabel:=p^.greater^._at;
  519. { calculate labels for left and right }
  520. { no range label: }
  521. if p^._low=p^._high then
  522. begin
  523. emit_const_reg(A_CMP,opsize,p^._low,hregister);
  524. if greaterlabel=lesslabel then
  525. emitjmp(C_NE,lesslabel)
  526. else
  527. begin
  528. emitjmp(jmp_le,lesslabel);
  529. emitjmp(jmp_gt,greaterlabel);
  530. end;
  531. cg.a_jmp_always(exprasmlist,p^.statement);
  532. end
  533. else
  534. begin
  535. emit_const_reg(A_CMP,opsize,p^._low,hregister);
  536. emitjmp(jmp_le,lesslabel);
  537. emit_const_reg(A_CMP,opsize,p^._high,hregister);
  538. emitjmp(jmp_gt,greaterlabel);
  539. cg.a_jmp_always(exprasmlist,p^.statement);
  540. end;
  541. if assigned(p^.less) then
  542. gentreejmp(p^.less);
  543. if assigned(p^.greater) then
  544. gentreejmp(p^.greater);
  545. end;
  546. procedure genlinearcmplist(hp : pcaserecord);
  547. var
  548. first : boolean;
  549. last : TConstExprInt;
  550. procedure genitem(t : pcaserecord);
  551. var
  552. l1 : tasmlabel;
  553. begin
  554. if assigned(t^.less) then
  555. genitem(t^.less);
  556. if t^._low=t^._high then
  557. begin
  558. if opsize=S_Q then
  559. begin
  560. getlabel(l1);
  561. emit_const_reg(A_CMP,S_L,longint(hi(int64(t^._low))),hregister2);
  562. emitjmp(C_NZ,l1);
  563. emit_const_reg(A_CMP,S_L,longint(lo(int64(t^._low))),hregister);
  564. emitjmp(C_Z,t^.statement);
  565. cg.a_label(exprasmlist,l1);
  566. end
  567. else
  568. begin
  569. emit_const_reg(A_CMP,opsize,longint(t^._low),hregister);
  570. emitjmp(C_Z,t^.statement);
  571. last:=t^._low;
  572. end;
  573. end
  574. else
  575. begin
  576. { if there is no unused label between the last and the }
  577. { present label then the lower limit can be checked }
  578. { immediately. else check the range in between: }
  579. if first or (t^._low-last>1) then
  580. begin
  581. if opsize=S_Q then
  582. begin
  583. getlabel(l1);
  584. emit_const_reg(A_CMP,S_L,longint(hi(int64(t^._low))),hregister2);
  585. emitjmp(jmp_le,elselabel);
  586. emitjmp(jmp_gt,l1);
  587. emit_const_reg(A_CMP,S_L,longint(lo(int64(t^._low))),hregister);
  588. { the comparisation of the low dword must be always unsigned! }
  589. emitjmp(C_B,elselabel);
  590. cg.a_label(exprasmlist,l1);
  591. end
  592. else
  593. begin
  594. emit_const_reg(A_CMP,opsize,longint(t^._low),hregister);
  595. emitjmp(jmp_le,elselabel);
  596. end;
  597. end;
  598. if opsize=S_Q then
  599. begin
  600. getlabel(l1);
  601. emit_const_reg(A_CMP,S_L,longint(hi(int64(t^._high))),hregister2);
  602. emitjmp(jmp_le,t^.statement);
  603. emitjmp(jmp_gt,l1);
  604. emit_const_reg(A_CMP,S_L,longint(lo(int64(t^._high))),hregister);
  605. { the comparisation of the low dword must be always unsigned! }
  606. emitjmp(C_BE,t^.statement);
  607. cg.a_label(exprasmlist,l1);
  608. end
  609. else
  610. begin
  611. emit_const_reg(A_CMP,opsize,longint(t^._high),hregister);
  612. emitjmp(jmp_lee,t^.statement);
  613. end;
  614. last:=t^._high;
  615. end;
  616. first:=false;
  617. if assigned(t^.greater) then
  618. genitem(t^.greater);
  619. end;
  620. begin
  621. last:=0;
  622. first:=true;
  623. genitem(hp);
  624. cg.a_jmp_always(exprasmlist,elselabel);
  625. end;
  626. procedure genlinearlist(hp : pcaserecord);
  627. var
  628. first : boolean;
  629. last : TConstExprInt;
  630. {helplabel : longint;}
  631. procedure genitem(t : pcaserecord);
  632. procedure gensub(value:longint);
  633. begin
  634. if value=1 then
  635. emit_reg(A_DEC,opsize,hregister)
  636. else
  637. emit_const_reg(A_SUB,opsize,value,hregister);
  638. end;
  639. begin
  640. if assigned(t^.less) then
  641. genitem(t^.less);
  642. { need we to test the first value }
  643. if first and (t^._low>get_min_value(left.resulttype.def)) then
  644. begin
  645. emit_const_reg(A_CMP,opsize,longint(t^._low),hregister);
  646. emitjmp(jmp_le,elselabel);
  647. end;
  648. if t^._low=t^._high then
  649. begin
  650. if t^._low-last=0 then
  651. emit_reg_reg(A_OR,opsize,hregister,hregister)
  652. else
  653. gensub(longint(t^._low-last));
  654. last:=t^._low;
  655. emitjmp(C_Z,t^.statement);
  656. end
  657. else
  658. begin
  659. { it begins with the smallest label, if the value }
  660. { is even smaller then jump immediately to the }
  661. { ELSE-label }
  662. if first then
  663. begin
  664. { have we to ajust the first value ? }
  665. if (t^._low>get_min_value(left.resulttype.def)) then
  666. gensub(longint(t^._low));
  667. end
  668. else
  669. begin
  670. { if there is no unused label between the last and the }
  671. { present label then the lower limit can be checked }
  672. { immediately. else check the range in between: }
  673. { note: you can't use gensub() here because dec doesn't }
  674. { change the carry flag (needed for jmp_lxx) (JM) }
  675. emit_const_reg(A_SUB,opsize,longint(t^._low-last),hregister);
  676. emitjmp(jmp_le,elselabel);
  677. end;
  678. emit_const_reg(A_SUB,opsize,longint(t^._high-t^._low),hregister);
  679. emitjmp(jmp_lee,t^.statement);
  680. last:=t^._high;
  681. end;
  682. first:=false;
  683. if assigned(t^.greater) then
  684. genitem(t^.greater);
  685. end;
  686. begin
  687. { do we need to generate cmps? }
  688. if (with_sign and (min_label<0)) then
  689. genlinearcmplist(hp)
  690. else
  691. begin
  692. last:=0;
  693. first:=true;
  694. genitem(hp);
  695. cg.a_jmp_always(exprasmlist,elselabel);
  696. end;
  697. end;
  698. procedure genjumptable(hp : pcaserecord;min_,max_ : longint);
  699. var
  700. table : tasmlabel;
  701. last : TConstExprInt;
  702. href : treference;
  703. procedure genitem(t : pcaserecord);
  704. var
  705. i : longint;
  706. begin
  707. if assigned(t^.less) then
  708. genitem(t^.less);
  709. { fill possible hole }
  710. for i:=last+1 to t^._low-1 do
  711. jumpSegment.concat(Tai_const_symbol.Create(elselabel));
  712. for i:=t^._low to t^._high do
  713. jumpSegment.concat(Tai_const_symbol.Create(t^.statement));
  714. last:=t^._high;
  715. if assigned(t^.greater) then
  716. genitem(t^.greater);
  717. end;
  718. begin
  719. if not(jumptable_no_range) then
  720. begin
  721. emit_const_reg(A_CMP,opsize,longint(min_),hregister);
  722. { case expr less than min_ => goto elselabel }
  723. emitjmp(jmp_le,elselabel);
  724. emit_const_reg(A_CMP,opsize,longint(max_),hregister);
  725. emitjmp(jmp_gt,elselabel);
  726. end;
  727. getlabel(table);
  728. { extend with sign }
  729. if opsize=S_W then
  730. begin
  731. if with_sign then
  732. emit_reg_reg(A_MOVSX,S_WL,hregister,
  733. rg.makeregsize(hregister,OS_INT))
  734. else
  735. emit_reg_reg(A_MOVZX,S_WL,hregister,
  736. rg.makeregsize(hregister,OS_INT));
  737. hregister:=rg.makeregsize(hregister,OS_INT);
  738. end
  739. else if opsize=S_B then
  740. begin
  741. if with_sign then
  742. emit_reg_reg(A_MOVSX,S_BL,hregister,
  743. rg.makeregsize(hregister,OS_INT))
  744. else
  745. emit_reg_reg(A_MOVZX,S_BL,hregister,
  746. rg.makeregsize(hregister,OS_INT));
  747. hregister:=rg.makeregsize(hregister,OS_INT);
  748. end;
  749. reference_reset_symbol(href,table,0);
  750. href.offset:=(-longint(min_))*4;
  751. href.index:=hregister;
  752. href.scalefactor:=4;
  753. emit_ref(A_JMP,S_NO,href);
  754. { !!!!! generate tables
  755. if not(cs_littlesize in aktlocalswitches) then
  756. jumpSegment.concat(Taicpu.Op_const(A_ALIGN,S_NO,4));
  757. }
  758. jumpSegment.concat(Tai_label.Create(table));
  759. last:=min_;
  760. genitem(hp);
  761. { !!!!!!!
  762. if not(cs_littlesize in aktlocalswitches) then
  763. emit_const(A_ALIGN,S_NO,4);
  764. }
  765. end;
  766. var
  767. lv,hv,
  768. max_label: tconstexprint;
  769. labels : longint;
  770. max_linear_list : longint;
  771. otl, ofl: tasmlabel;
  772. isjump : boolean;
  773. {$ifdef Delphi}
  774. dist : cardinal;
  775. {$else Delphi}
  776. dist : dword;
  777. {$endif Delphi}
  778. begin
  779. getlabel(endlabel);
  780. getlabel(elselabel);
  781. if (cs_create_smart in aktmoduleswitches) then
  782. jumpsegment:=procinfo^.aktlocaldata
  783. else
  784. jumpsegment:=datasegment;
  785. with_sign:=is_signed(left.resulttype.def);
  786. if with_sign then
  787. begin
  788. jmp_gt:=C_G;
  789. jmp_le:=C_L;
  790. jmp_lee:=C_LE;
  791. end
  792. else
  793. begin
  794. jmp_gt:=C_A;
  795. jmp_le:=C_B;
  796. jmp_lee:=C_BE;
  797. end;
  798. rg.cleartempgen;
  799. { save current truelabel and falselabel }
  800. isjump:=false;
  801. if left.location.loc=LOC_JUMP then
  802. begin
  803. otl:=truelabel;
  804. getlabel(truelabel);
  805. ofl:=falselabel;
  806. getlabel(falselabel);
  807. isjump:=true;
  808. end;
  809. secondpass(left);
  810. { determines the size of the operand }
  811. opsize:=bytes2Sxx[left.resulttype.def.size];
  812. { copy the case expression to a register }
  813. location_force_reg(exprasmlist,left.location,def_cgsize(left.resulttype.def),false);
  814. if opsize=S_Q then
  815. begin
  816. hregister:=left.location.registerlow;
  817. hregister2:=left.location.registerhigh;
  818. end
  819. else
  820. hregister:=left.location.register;
  821. if isjump then
  822. begin
  823. truelabel:=otl;
  824. falselabel:=ofl;
  825. end;
  826. { we need the min_label always to choose between }
  827. { cmps and subs/decs }
  828. min_label:=case_get_min(nodes);
  829. load_all_regvars(exprasmlist);
  830. { now generate the jumps }
  831. if opsize=S_Q then
  832. genlinearcmplist(nodes)
  833. else
  834. begin
  835. if cs_optimize in aktglobalswitches then
  836. begin
  837. { procedures are empirically passed on }
  838. { consumption can also be calculated }
  839. { but does it pay on the different }
  840. { processors? }
  841. { moreover can the size only be appro- }
  842. { ximated as it is not known if rel8, }
  843. { rel16 or rel32 jumps are used }
  844. max_label:=case_get_max(nodes);
  845. labels:=case_count_labels(nodes);
  846. { can we omit the range check of the jump table ? }
  847. getrange(left.resulttype.def,lv,hv);
  848. jumptable_no_range:=(lv=min_label) and (hv=max_label);
  849. { hack a little bit, because the range can be greater }
  850. { than the positive range of a longint }
  851. if (min_label<0) and (max_label>0) then
  852. begin
  853. {$ifdef Delphi}
  854. if min_label=longint($80000000) then
  855. dist:=Cardinal(max_label)+Cardinal($80000000)
  856. else
  857. dist:=Cardinal(max_label)+Cardinal(-min_label)
  858. {$else Delphi}
  859. if min_label=$80000000 then
  860. dist:=dword(max_label)+dword($80000000)
  861. else
  862. dist:=dword(max_label)+dword(-min_label)
  863. {$endif Delphi}
  864. end
  865. else
  866. dist:=max_label-min_label;
  867. { optimize for size ? }
  868. if cs_littlesize in aktglobalswitches then
  869. begin
  870. if (labels<=2) or
  871. ((max_label-min_label)<0) or
  872. ((max_label-min_label)>3*labels) then
  873. { a linear list is always smaller than a jump tree }
  874. genlinearlist(nodes)
  875. else
  876. { if the labels less or more a continuum then }
  877. genjumptable(nodes,min_label,max_label);
  878. end
  879. else
  880. begin
  881. if jumptable_no_range then
  882. max_linear_list:=4
  883. else
  884. max_linear_list:=2;
  885. { a jump table crashes the pipeline! }
  886. if aktoptprocessor=Class386 then
  887. inc(max_linear_list,3);
  888. if aktoptprocessor=ClassP5 then
  889. inc(max_linear_list,6);
  890. if aktoptprocessor>=ClassP6 then
  891. inc(max_linear_list,9);
  892. if (labels<=max_linear_list) then
  893. genlinearlist(nodes)
  894. else
  895. begin
  896. if (dist>4*cardinal(labels)) then
  897. begin
  898. if labels>16 then
  899. gentreejmp(nodes)
  900. else
  901. genlinearlist(nodes);
  902. end
  903. else
  904. genjumptable(nodes,min_label,max_label);
  905. end;
  906. end;
  907. end
  908. else
  909. { it's always not bad }
  910. genlinearlist(nodes);
  911. end;
  912. rg.ungetregister(exprasmlist,hregister);
  913. { now generate the instructions }
  914. hp:=right;
  915. while assigned(hp) do
  916. begin
  917. rg.cleartempgen;
  918. secondpass(tbinarynode(hp).right);
  919. { don't come back to case line }
  920. aktfilepos:=exprasmList.getlasttaifilepos^;
  921. load_all_regvars(exprasmlist);
  922. cg.a_jmp_always(exprasmlist,endlabel);
  923. hp:=tbinarynode(hp).left;
  924. end;
  925. cg.a_label(exprasmlist,elselabel);
  926. { ...and the else block }
  927. if assigned(elseblock) then
  928. begin
  929. rg.cleartempgen;
  930. secondpass(elseblock);
  931. load_all_regvars(exprasmlist);
  932. end;
  933. cg.a_label(exprasmlist,endlabel);
  934. end;
  935. begin
  936. {$ifndef TEST_GENERIC}
  937. cinnode:=ti386innode;
  938. {$endif}
  939. ccasenode:=ti386casenode;
  940. end.
  941. {
  942. $Log$
  943. Revision 1.33 2002-07-06 20:27:26 carl
  944. + generic set handling
  945. Revision 1.32 2002/07/01 18:46:33 peter
  946. * internal linker
  947. * reorganized aasm layer
  948. Revision 1.31 2002/05/18 13:34:25 peter
  949. * readded missing revisions
  950. Revision 1.30 2002/05/16 19:46:52 carl
  951. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  952. + try to fix temp allocation (still in ifdef)
  953. + generic constructor calls
  954. + start of tassembler / tmodulebase class cleanup
  955. Revision 1.28 2002/05/13 19:54:38 peter
  956. * removed n386ld and n386util units
  957. * maybe_save/maybe_restore added instead of the old maybe_push
  958. Revision 1.27 2002/05/12 16:53:17 peter
  959. * moved entry and exitcode to ncgutil and cgobj
  960. * foreach gets extra argument for passing local data to the
  961. iterator function
  962. * -CR checks also class typecasts at runtime by changing them
  963. into as
  964. * fixed compiler to cycle with the -CR option
  965. * fixed stabs with elf writer, finally the global variables can
  966. be watched
  967. * removed a lot of routines from cga unit and replaced them by
  968. calls to cgobj
  969. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  970. u32bit then the other is typecasted also to u32bit without giving
  971. a rangecheck warning/error.
  972. * fixed pascal calling method with reversing also the high tree in
  973. the parast, detected by tcalcst3 test
  974. Revision 1.26 2002/04/25 20:16:40 peter
  975. * moved more routines from cga/n386util
  976. Revision 1.25 2002/04/21 19:02:07 peter
  977. * removed newn and disposen nodes, the code is now directly
  978. inlined from pexpr
  979. * -an option that will write the secondpass nodes to the .s file, this
  980. requires EXTDEBUG define to actually write the info
  981. * fixed various internal errors and crashes due recent code changes
  982. Revision 1.24 2002/04/21 15:37:26 carl
  983. * changeregsize -> rg.makeregsize
  984. Revision 1.23 2002/04/19 15:39:35 peter
  985. * removed some more routines from cga
  986. * moved location_force_reg/mem to ncgutil
  987. * moved arrayconstructnode secondpass to ncgld
  988. Revision 1.22 2002/04/15 19:44:21 peter
  989. * fixed stackcheck that would be called recursively when a stack
  990. error was found
  991. * generic changeregsize(reg,size) for i386 register resizing
  992. * removed some more routines from cga unit
  993. * fixed returnvalue handling
  994. * fixed default stacksize of linux and go32v2, 8kb was a bit small :-)
  995. Revision 1.21 2002/04/02 17:11:36 peter
  996. * tlocation,treference update
  997. * LOC_CONSTANT added for better constant handling
  998. * secondadd splitted in multiple routines
  999. * location_force_reg added for loading a location to a register
  1000. of a specified size
  1001. * secondassignment parses now first the right and then the left node
  1002. (this is compatible with Kylix). This saves a lot of push/pop especially
  1003. with string operations
  1004. * adapted some routines to use the new cg methods
  1005. Revision 1.20 2002/03/31 20:26:39 jonas
  1006. + a_loadfpu_* and a_loadmm_* methods in tcg
  1007. * register allocation is now handled by a class and is mostly processor
  1008. independent (+rgobj.pas and i386/rgcpu.pas)
  1009. * temp allocation is now handled by a class (+tgobj.pas, -i386\tgcpu.pas)
  1010. * some small improvements and fixes to the optimizer
  1011. * some register allocation fixes
  1012. * some fpuvaroffset fixes in the unary minus node
  1013. * push/popusedregisters is now called rg.save/restoreusedregisters and
  1014. (for i386) uses temps instead of push/pop's when using -Op3 (that code is
  1015. also better optimizable)
  1016. * fixed and optimized register saving/restoring for new/dispose nodes
  1017. * LOC_FPU locations now also require their "register" field to be set to
  1018. R_ST, not R_ST0 (the latter is used for LOC_CFPUREGISTER locations only)
  1019. - list field removed of the tnode class because it's not used currently
  1020. and can cause hard-to-find bugs
  1021. }