ncpuset.pas 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. {
  2. $Id$
  3. Copyright (c) 1998-2004 by Florian Klaempfl
  4. Generate sparc 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 ncpuset;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. globtype,
  23. nset,
  24. ncgset;
  25. type
  26. tcpucasenode = class(tcgcasenode)
  27. protected
  28. procedure optimizevalues(var max_linear_list:aint;var max_dist:aword);override;
  29. function has_jumptable : boolean;override;
  30. procedure genjumptable(hp : pcaserecord;min_,max_ : aint);override;
  31. end;
  32. implementation
  33. uses
  34. globals,
  35. systems,
  36. cpubase,
  37. aasmbase,aasmtai,aasmcpu,
  38. cgbase,cgutils,cgobj,
  39. procinfo;
  40. procedure tcpucasenode.optimizevalues(var max_linear_list:aint;var max_dist:aword);
  41. begin
  42. { give the jump table a higher priority }
  43. max_dist:=(max_dist*3) div 2;
  44. end;
  45. function tcpucasenode.has_jumptable : boolean;
  46. begin
  47. has_jumptable:=true;
  48. end;
  49. procedure tcpucasenode.genjumptable(hp : pcaserecord;min_,max_ : aint);
  50. var
  51. table : tasmlabel;
  52. last : TConstExprInt;
  53. indexreg,jmpreg,basereg : tregister;
  54. href : treference;
  55. jumpsegment : TAAsmOutput;
  56. procedure genitem(t : pcaserecord);
  57. var
  58. i : aint;
  59. begin
  60. if assigned(t^.less) then
  61. genitem(t^.less);
  62. { fill possible hole }
  63. for i:=last+1 to t^._low-1 do
  64. jumpSegment.concat(Tai_const.Create_sym(elselabel));
  65. for i:=t^._low to t^._high do
  66. jumpSegment.concat(Tai_const.Create_sym(t^.statement));
  67. last:=t^._high;
  68. if assigned(t^.greater) then
  69. genitem(t^.greater);
  70. end;
  71. begin
  72. if (cs_create_smart in aktmoduleswitches) or
  73. (af_smartlink_sections in target_asm.flags) then
  74. jumpsegment:=current_procinfo.aktlocaldata
  75. else
  76. jumpsegment:=datasegment;
  77. if not(jumptable_no_range) then
  78. begin
  79. { case expr less than min_ => goto elselabel }
  80. cg.a_cmp_const_reg_label(exprasmlist,opsize,jmp_lt,aint(min_),hregister,elselabel);
  81. { case expr greater than max_ => goto elselabel }
  82. cg.a_cmp_const_reg_label(exprasmlist,opsize,jmp_gt,aint(max_),hregister,elselabel);
  83. end;
  84. objectlibrary.getlabel(table);
  85. indexreg:=cg.getaddressregister(exprasmlist);
  86. cg.a_op_const_reg_reg(exprasmlist,OP_SHL,OS_ADDR,2,hregister,indexreg);
  87. { create reference }
  88. reference_reset_symbol(href,table,0);
  89. href.offset:=(-aint(min_))*4;
  90. basereg:=cg.getaddressregister(exprasmlist);
  91. cg.a_loadaddr_ref_reg(exprasmlist,href,basereg);
  92. jmpreg:=cg.getaddressregister(exprasmlist);
  93. cg.a_op_reg_reg_reg(exprasmlist,OP_ADD,OS_ADDR,indexreg,basereg,jmpreg);
  94. exprasmlist.concat(taicpu.op_reg(A_JMP,jmpreg));
  95. { Delay slot }
  96. exprasmlist.concat(taicpu.op_none(A_NOP));
  97. { generate jump table }
  98. if not(cs_littlesize in aktglobalswitches) then
  99. jumpSegment.concat(Tai_Align.Create_Op(4,0));
  100. jumpSegment.concat(Tai_label.Create(table));
  101. last:=min_;
  102. genitem(hp);
  103. end;
  104. begin
  105. ccasenode:=tcpucasenode;
  106. end.
  107. {
  108. $Log$
  109. Revision 1.2 2004-10-30 22:01:11 florian
  110. * jmp table code generation for case statement on sparc
  111. Revision 1.1 2004/10/30 17:50:53 florian
  112. * initial revision, not yet enabled
  113. }