ncpuset.pas 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. 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. table: tasmlabel;
  52. last: TConstExprInt;
  53. indexreg, jmpreg, basereg: tregister;
  54. href: treference;
  55. jumpsegment: TAsmlist;
  56. opcgsize: tcgsize;
  57. procedure genitem(t: pcaselabel);
  58. var
  59. i: aint;
  60. begin
  61. if assigned(t^.less) then
  62. genitem(t^.less);
  63. { fill possible hole }
  64. for i := last.svalue+1 to t^._low.svalue-1 do
  65. jumpSegment.concat(Tai_const.Create_sym(elselabel));
  66. for i := t^._low.svalue to t^._high.svalue do
  67. jumpSegment.concat(Tai_const.Create_sym(blocklabel(t^.blockid)));
  68. last := t^._high;
  69. if assigned(t^.greater) then
  70. genitem(t^.greater);
  71. end;
  72. begin
  73. opcgsize:=def_cgsize(opsize);
  74. jumpsegment := current_procinfo.aktlocaldata;
  75. if not (jumptable_no_range) then
  76. begin
  77. { case expr less than min_ => goto elselabel }
  78. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, opcgsize, jmp_lt, aint(min_), hregister, elselabel);
  79. { case expr greater than max_ => goto elselabel }
  80. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, opcgsize, jmp_gt, aint(max_), hregister, elselabel);
  81. end;
  82. current_asmdata.getjumplabel(table);
  83. indexreg := cg.getaddressregister(current_asmdata.CurrAsmList);
  84. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, OP_SHL, OS_ADDR, 2, hregister, indexreg);
  85. { create reference }
  86. reference_reset_symbol(href, table, 0, sizeof(aint));
  87. href.offset := (-aint(min_)) * 4;
  88. href.base:=indexreg;
  89. jmpreg := cg.getaddressregister(current_asmdata.CurrAsmList);
  90. cg.a_load_ref_reg(current_asmdata.CurrAsmList, OS_ADDR, OS_ADDR, href, jmpreg);
  91. current_asmdata.CurrAsmList.concat(taicpu.op_reg(A_JR, jmpreg));
  92. { Delay slot }
  93. current_asmdata.CurrAsmList.concat(taicpu.op_none(A_NOP));
  94. { generate jump table }
  95. new_section(jumpSegment,sec_rodata,current_procinfo.procdef.mangledname,sizeof(aint));
  96. jumpSegment.concat(Tai_label.Create(table));
  97. last := min_;
  98. genitem(hp);
  99. end;
  100. begin
  101. ccasenode := tcpucasenode;
  102. end.