ncpuset.pas 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. nset,
  23. ncgset;
  24. type
  25. tcpucasenode = class(tcgcasenode)
  26. protected
  27. procedure optimizevalues(var max_linear_list:aint;var max_dist:aword);override;
  28. function has_jumptable : boolean;override;
  29. procedure genjumptable(hp : pcaselabel;min_,max_ : aint);override;
  30. end;
  31. implementation
  32. uses
  33. globals,
  34. systems,
  35. cpubase,
  36. aasmbase,aasmtai,aasmdata,aasmcpu,
  37. cgbase,cgutils,cgobj,
  38. procinfo;
  39. procedure tcpucasenode.optimizevalues(var max_linear_list:aint;var max_dist:aword);
  40. begin
  41. { give the jump table a higher priority }
  42. max_dist:=(max_dist*3) div 2;
  43. end;
  44. function tcpucasenode.has_jumptable : boolean;
  45. begin
  46. has_jumptable:=true;
  47. end;
  48. procedure tcpucasenode.genjumptable(hp : pcaselabel;min_,max_ : aint);
  49. var
  50. table : tasmlabel;
  51. last : TConstExprInt;
  52. indexreg,jmpreg,basereg : tregister;
  53. href : treference;
  54. procedure genitem(list:TAsmList;t : pcaselabel);
  55. var
  56. i : aint;
  57. begin
  58. if assigned(t^.less) then
  59. genitem(list,t^.less);
  60. { fill possible hole }
  61. for i:=last+1 to t^._low-1 do
  62. list.concat(Tai_const.Create_sym(elselabel));
  63. for i:=t^._low to t^._high do
  64. list.concat(Tai_const.Create_sym(blocklabel(t^.blockid)));
  65. last:=t^._high;
  66. if assigned(t^.greater) then
  67. genitem(list,t^.greater);
  68. end;
  69. begin
  70. if not(jumptable_no_range) then
  71. begin
  72. { case expr less than min_ => goto elselabel }
  73. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,opsize,jmp_lt,aint(min_),hregister,elselabel);
  74. { case expr greater than max_ => goto elselabel }
  75. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,opsize,jmp_gt,aint(max_),hregister,elselabel);
  76. end;
  77. current_asmdata.getjumplabel(table);
  78. indexreg:=cg.getaddressregister(current_asmdata.CurrAsmList);
  79. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SHL,OS_ADDR,2,hregister,indexreg);
  80. { create reference }
  81. reference_reset_symbol(href,table,0);
  82. href.offset:=(-aint(min_))*4;
  83. basereg:=cg.getaddressregister(current_asmdata.CurrAsmList);
  84. cg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,href,basereg);
  85. jmpreg:=cg.getaddressregister(current_asmdata.CurrAsmList);
  86. reference_reset(href);
  87. href.index:=indexreg;
  88. href.base:=basereg;
  89. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,href,jmpreg);
  90. current_asmdata.CurrAsmList.concat(taicpu.op_reg(A_JMP,jmpreg));
  91. { Delay slot }
  92. current_asmdata.CurrAsmList.concat(taicpu.op_none(A_NOP));
  93. { generate jump table }
  94. new_section(current_procinfo.aktlocaldata,sec_data,current_procinfo.procdef.mangledname,sizeof(aint));
  95. current_procinfo.aktlocaldata.concat(Tai_label.Create(table));
  96. last:=min_;
  97. genitem(current_procinfo.aktlocaldata,hp);
  98. end;
  99. begin
  100. ccasenode:=tcpucasenode;
  101. end.