ncpuset.pas 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. {
  2. Copyright (c) 1998-2004 by Florian Klaempfl and David Zhang
  3. Generate MIPSEL 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. constexp,
  36. cpubase,
  37. aasmbase, aasmtai, aasmcpu, aasmdata,
  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: pcaselabel; min_, max_: aint);
  50. var
  51. table: tasmlabel;
  52. last: TConstExprInt;
  53. indexreg, jmpreg, basereg: tregister;
  54. href: treference;
  55. jumpsegment: TAsmlist;
  56. procedure genitem(t: pcaselabel);
  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.svalue+1 to t^._low.svalue-1 do
  64. jumpSegment.concat(Tai_const.Create_sym(elselabel));
  65. for i := t^._low.svalue to t^._high.svalue do
  66. jumpSegment.concat(Tai_const.Create_sym(blocklabel(t^.blockid)));
  67. last := t^._high;
  68. if assigned(t^.greater) then
  69. genitem(t^.greater);
  70. end;
  71. begin
  72. jumpsegment := current_procinfo.aktlocaldata;
  73. if not (jumptable_no_range) then
  74. begin
  75. { case expr less than min_ => goto elselabel }
  76. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, opsize, jmp_lt, aint(min_), hregister, elselabel);
  77. { case expr greater than max_ => goto elselabel }
  78. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, opsize, jmp_gt, aint(max_), hregister, elselabel);
  79. end;
  80. current_asmdata.getjumplabel(table);
  81. indexreg := cg.getaddressregister(current_asmdata.CurrAsmList);
  82. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, OP_SHL, OS_ADDR, 2, hregister, indexreg);
  83. { create reference }
  84. reference_reset_symbol(href, table, 0, sizeof(aint));
  85. href.offset := (-aint(min_)) * 4;
  86. basereg := cg.getaddressregister(current_asmdata.CurrAsmList);
  87. cg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList, href, basereg);
  88. jmpreg := cg.getaddressregister(current_asmdata.CurrAsmList);
  89. reference_reset(href, sizeof(aint));
  90. href.index := indexreg;
  91. href.base := basereg;
  92. cg.a_load_ref_reg(current_asmdata.CurrAsmList, OS_ADDR, OS_ADDR, href, jmpreg);
  93. current_asmdata.CurrAsmList.concat(taicpu.op_reg(A_JR, jmpreg));
  94. { Delay slot }
  95. current_asmdata.CurrAsmList.concat(taicpu.op_none(A_NOP));
  96. { generate jump table }
  97. if not(cs_opt_size in current_settings.optimizerswitches) then
  98. jumpSegment.concat(Tai_Align.Create_Op(4, 0));
  99. jumpSegment.concat(Tai_label.Create(table));
  100. last := min_;
  101. genitem(hp);
  102. end;
  103. begin
  104. ccasenode := tcpucasenode;
  105. end.