ncpuset.pas 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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:aint;var max_dist:aword);override;
  29. function has_jumptable : boolean;override;
  30. procedure genjumptable(hp : pcaselabel;min_,max_ : aint);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: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 : pcaselabel;min_,max_ : aint);
  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 : aint;
  60. begin
  61. if assigned(t^.less) then
  62. genitem(list,t^.less);
  63. { fill possible hole }
  64. for i:=last.svalue+1 to t^._low.svalue-1 do
  65. list.concat(Tai_const.Create_rel_sym(aitconst_ptr,base,elselabel));
  66. for i:=t^._low.svalue to t^._high.svalue do
  67. list.concat(Tai_const.Create_rel_sym(aitconst_ptr,base,blocklabel(t^.blockid)));
  68. last:=t^._high;
  69. if assigned(t^.greater) then
  70. genitem(list,t^.greater);
  71. end;
  72. begin
  73. opcgsize:=def_cgsize(opsize);
  74. last:=min_;
  75. if not(jumptable_no_range) then
  76. begin
  77. { a <= x <= b <-> unsigned(x-a) <= (b-a) }
  78. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_SUB,opcgsize,aint(min_),hregister);
  79. { case expr greater than max_ => goto elselabel }
  80. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,opcgsize,OC_A,aint(max_)-aint(min_),hregister,elselabel);
  81. min_:=0;
  82. end;
  83. current_asmdata.getjumplabel(table);
  84. indexreg:=cg.getaddressregister(current_asmdata.CurrAsmList);
  85. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SHL,OS_ADDR,2,hregister,indexreg);
  86. { create reference }
  87. current_asmdata.getjumplabel(base);
  88. cg.a_label(current_asmdata.CurrAsmList,base);
  89. reference_reset_symbol(href,table,(-aint(min_))*4,sizeof(pint));
  90. href.relsymbol:=base;
  91. { Generate the following code:
  92. .Lbase:
  93. call .+8 # mov %pc,%o7
  94. sethi %hi(.LTable-.Lbase),%basereg
  95. or %basereg,%lo(.LTable-.Lbase),%basereg
  96. add %indexreg,%basereg%,%basereg
  97. ld [%o7+%basereg],%jmpreg
  98. jmp %o7+%jmpreg }
  99. { CALL overwrites %o7, tell reg.allocator about that }
  100. cg.getcpuregister(current_asmdata.CurrAsmList,NR_O7);
  101. current_asmdata.CurrAsmList.concat(taicpu.op_sym_ofs(A_CALL,base,8));
  102. basereg:=cg.getaddressregister(current_asmdata.CurrAsmList);
  103. { TODO: incorporate handling such references into cg.a_loadaddr_ref_reg? }
  104. href.refaddr:=addr_high;
  105. current_asmdata.CurrAsmList.concat(taicpu.op_ref_reg(A_SETHI,href,basereg));
  106. href.refaddr:=addr_low;
  107. current_asmdata.CurrAsmList.concat(taicpu.op_reg_ref_reg(A_OR,basereg,href,basereg));
  108. { add index }
  109. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,OP_ADD,OS_ADDR,basereg,indexreg,basereg);
  110. jmpreg:=cg.getaddressregister(current_asmdata.CurrAsmList);
  111. reference_reset_base(href,NR_O7,0,sizeof(pint));
  112. href.index:=basereg;
  113. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,href,jmpreg);
  114. href.index:=jmpreg;
  115. href.refaddr:=addr_full;
  116. current_asmdata.CurrAsmList.concat(taicpu.op_ref(A_JMP,href));
  117. { Delay slot }
  118. current_asmdata.CurrAsmList.concat(taicpu.op_none(A_NOP));
  119. cg.ungetcpuregister(current_asmdata.CurrAsmList,NR_O7);
  120. { generate jump table }
  121. cg.a_label(current_asmdata.CurrAsmList,table);
  122. genitem(current_asmdata.CurrAsmList,hp);
  123. end;
  124. begin
  125. ccasenode:=tcpucasenode;
  126. end.