ncpuset.pas 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. public
  32. function pass_1:tnode;override;
  33. end;
  34. implementation
  35. uses
  36. globals,constexp,
  37. systems,
  38. cpubase,
  39. aasmbase,aasmtai,aasmdata,aasmcpu,
  40. cgbase,cgutils,cgobj,
  41. defutil,procinfo;
  42. procedure tcpucasenode.optimizevalues(var max_linear_list:aint;var max_dist:aword);
  43. begin
  44. { give the jump table a higher priority }
  45. max_dist:=(max_dist*3) div 2;
  46. end;
  47. function tcpucasenode.has_jumptable : boolean;
  48. begin
  49. has_jumptable:=true;
  50. end;
  51. function tcpucasenode.pass_1:tnode;
  52. begin
  53. result:=inherited pass_1;
  54. { TODO: ABI-compliant position-independent case code does not involve GOT }
  55. if (cs_create_pic in current_settings.moduleswitches) and
  56. (tf_pic_uses_got in target_info.flags) then
  57. include(current_procinfo.flags,pi_needs_got);
  58. end;
  59. procedure tcpucasenode.genjumptable(hp : pcaselabel;min_,max_ : aint);
  60. var
  61. table : tasmlabel;
  62. last : TConstExprInt;
  63. indexreg,jmpreg,basereg : tregister;
  64. href : treference;
  65. opcgsize : tcgsize;
  66. procedure genitem(list:TAsmList;t : pcaselabel);
  67. var
  68. i : aint;
  69. begin
  70. if assigned(t^.less) then
  71. genitem(list,t^.less);
  72. { fill possible hole }
  73. for i:=last.svalue+1 to t^._low.svalue-1 do
  74. list.concat(Tai_const.Create_sym(elselabel));
  75. for i:=t^._low.svalue to t^._high.svalue do
  76. list.concat(Tai_const.Create_sym(blocklabel(t^.blockid)));
  77. last:=t^._high;
  78. if assigned(t^.greater) then
  79. genitem(list,t^.greater);
  80. end;
  81. begin
  82. opcgsize:=def_cgsize(opsize);
  83. if not(jumptable_no_range) then
  84. begin
  85. { case expr less than min_ => goto elselabel }
  86. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,opcgsize,jmp_lt,aint(min_),hregister,elselabel);
  87. { case expr greater than max_ => goto elselabel }
  88. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,opcgsize,jmp_gt,aint(max_),hregister,elselabel);
  89. end;
  90. current_asmdata.getjumplabel(table);
  91. indexreg:=cg.getaddressregister(current_asmdata.CurrAsmList);
  92. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SHL,OS_ADDR,2,hregister,indexreg);
  93. { create reference }
  94. reference_reset_symbol(href,table,0,sizeof(pint));
  95. href.offset:=(-aint(min_))*4;
  96. basereg:=cg.getaddressregister(current_asmdata.CurrAsmList);
  97. cg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,href,basereg);
  98. jmpreg:=cg.getaddressregister(current_asmdata.CurrAsmList);
  99. reference_reset(href,sizeof(pint));
  100. href.index:=indexreg;
  101. href.base:=basereg;
  102. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,href,jmpreg);
  103. current_asmdata.CurrAsmList.concat(taicpu.op_reg(A_JMP,jmpreg));
  104. { Delay slot }
  105. current_asmdata.CurrAsmList.concat(taicpu.op_none(A_NOP));
  106. { generate jump table }
  107. new_section(current_procinfo.aktlocaldata,sec_data,current_procinfo.procdef.mangledname,sizeof(pint));
  108. current_procinfo.aktlocaldata.concat(Tai_label.Create(table));
  109. last:=min_;
  110. genitem(current_procinfo.aktlocaldata,hp);
  111. end;
  112. begin
  113. ccasenode:=tcpucasenode;
  114. end.