ncpuset.pas 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. {
  2. Copyright (c) 1998-2004 by Florian Klaempfl
  3. Generate sparc assembler for in set/case nodes
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit ncpuset;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. globtype,
  22. node,
  23. nset,
  24. ncgset;
  25. type
  26. tcpucasenode = class(tcgcasenode)
  27. protected
  28. procedure optimizevalues(var max_linear_list:int64;var max_dist:qword);override;
  29. function has_jumptable : boolean;override;
  30. procedure genjumptable(hp : pcaselabel;min_,max_ : int64);override;
  31. end;
  32. implementation
  33. uses
  34. globals,constexp,
  35. systems,
  36. cpubase,
  37. aasmbase,aasmtai,aasmdata,aasmcpu,
  38. cgbase,cgutils,cgobj,
  39. defutil,procinfo;
  40. procedure tcpucasenode.optimizevalues(var max_linear_list:int64;var max_dist:qword);
  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 : pcaselabel;min_,max_ : int64);
  50. var
  51. base,
  52. table : tasmlabel;
  53. last : TConstExprInt;
  54. indexreg,jmpreg,basereg : tregister;
  55. href : treference;
  56. opcgsize : tcgsize;
  57. procedure genitem(list:TAsmList;t : pcaselabel);
  58. var
  59. i : TConstExprInt;
  60. begin
  61. if assigned(t^.less) then
  62. genitem(list,t^.less);
  63. { fill possible hole }
  64. i:=last+1;
  65. while i<=t^._low-1 do
  66. begin
  67. list.concat(Tai_const.Create_rel_sym(aitconst_ptr,base,elselabel));
  68. i:=i+1;
  69. end;
  70. i:=t^._low;
  71. while i<=t^._high do
  72. begin
  73. list.concat(Tai_const.Create_rel_sym(aitconst_ptr,base,blocklabel(t^.blockid)));
  74. i:=i+1;
  75. end;
  76. last:=t^._high;
  77. if assigned(t^.greater) then
  78. genitem(list,t^.greater);
  79. end;
  80. begin
  81. opcgsize:=def_cgsize(opsize);
  82. last:=min_;
  83. if not(jumptable_no_range) then
  84. begin
  85. { a <= x <= b <-> unsigned(x-a) <= (b-a) }
  86. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_SUB,opcgsize,aint(min_),hregister);
  87. { case expr greater than max_ => goto elselabel }
  88. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,opcgsize,OC_A,aint(max_)-aint(min_),hregister,elselabel);
  89. min_:=0;
  90. end;
  91. current_asmdata.getjumplabel(table);
  92. indexreg:=cg.getaddressregister(current_asmdata.CurrAsmList);
  93. {$ifdef SPARC64}
  94. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SHL,OS_ADDR,3,hregister,indexreg);
  95. {$else SPARC64}
  96. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SHL,OS_ADDR,2,hregister,indexreg);
  97. {$endif SPARC64}
  98. { create reference }
  99. current_asmdata.getjumplabel(base);
  100. cg.a_label(current_asmdata.CurrAsmList,base);
  101. reference_reset_symbol(href,table,(-aint(min_))*sizeof(pint),sizeof(pint),[]);
  102. href.relsymbol:=base;
  103. { Generate the following code:
  104. .Lbase:
  105. call .+8 # mov %pc,%o7
  106. sethi %hi(.LTable-.Lbase),%basereg
  107. or %basereg,%lo(.LTable-.Lbase),%basereg
  108. add %indexreg,%basereg%,%basereg
  109. ld [%o7+%basereg],%jmpreg
  110. jmp %o7+%jmpreg }
  111. { CALL overwrites %o7, tell reg.allocator about that }
  112. cg.getcpuregister(current_asmdata.CurrAsmList,NR_O7);
  113. current_asmdata.CurrAsmList.concat(taicpu.op_sym_ofs(A_CALL,base,8));
  114. basereg:=cg.getaddressregister(current_asmdata.CurrAsmList);
  115. { TODO: incorporate handling such references into cg.a_loadaddr_ref_reg? }
  116. href.refaddr:=addr_high;
  117. current_asmdata.CurrAsmList.concat(taicpu.op_ref_reg(A_SETHI,href,basereg));
  118. href.refaddr:=addr_low;
  119. current_asmdata.CurrAsmList.concat(taicpu.op_reg_ref_reg(A_OR,basereg,href,basereg));
  120. { add index }
  121. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,OP_ADD,OS_ADDR,basereg,indexreg,basereg);
  122. jmpreg:=cg.getaddressregister(current_asmdata.CurrAsmList);
  123. reference_reset_base(href,NR_O7,0,ctempposinvalid,sizeof(pint),[]);
  124. href.index:=basereg;
  125. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,href,jmpreg);
  126. href.index:=jmpreg;
  127. href.refaddr:=addr_full;
  128. current_asmdata.CurrAsmList.concat(taicpu.op_ref(A_JMP,href));
  129. { Delay slot }
  130. current_asmdata.CurrAsmList.concat(taicpu.op_none(A_NOP));
  131. cg.ungetcpuregister(current_asmdata.CurrAsmList,NR_O7);
  132. { generate jump table }
  133. current_asmdata.CurrAsmList.Concat(tai_align.Create(sizeof(pint)));
  134. cg.a_label(current_asmdata.CurrAsmList,table);
  135. genitem(current_asmdata.CurrAsmList,hp);
  136. end;
  137. begin
  138. ccasenode:=tcpucasenode;
  139. end.