ncpuset.pas 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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: int64; var max_dist: qword); override;
  28. function has_jumptable: boolean; override;
  29. procedure genjumptable(hp: pcaselabel; min_, max_: int64); 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: int64; var max_dist: qword);
  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_: int64);
  50. var
  51. table: tasmlabel;
  52. last: TConstExprInt;
  53. indexreg, jmpreg: tregister;
  54. href: treference;
  55. jumpsegment: TAsmlist;
  56. opcgsize: tcgsize;
  57. labeltyp: taiconst_type;
  58. procedure genitem(t: pcaselabel);
  59. var
  60. i: TConstExprInt;
  61. begin
  62. if assigned(t^.less) then
  63. genitem(t^.less);
  64. { fill possible hole }
  65. i:=last+1;
  66. while i<=t^._low-1 do
  67. begin
  68. jumpSegment.concat(Tai_const.Create_type_sym(labeltyp,elselabel));
  69. i:=i+1;
  70. end;
  71. i:= t^._low;
  72. while i<=t^._high do
  73. begin
  74. jumpSegment.concat(Tai_const.Create_type_sym(labeltyp,blocklabel(t^.blockid)));
  75. i:=i+1;
  76. end;
  77. last := t^._high;
  78. if assigned(t^.greater) then
  79. genitem(t^.greater);
  80. end;
  81. begin
  82. opcgsize:=def_cgsize(opsize);
  83. last:=min_;
  84. jumpsegment := current_procinfo.aktlocaldata;
  85. if not (jumptable_no_range) then
  86. begin
  87. { a <= x <= b <-> unsigned(x-a) <= (b-a) }
  88. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_SUB,opcgsize,aint(min_),hregister);
  89. { case expr greater than max_ => goto elselabel }
  90. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,opcgsize,OC_A,aint(max_)-aint(min_),hregister,elselabel);
  91. min_:=0;
  92. end;
  93. current_asmdata.getjumplabel(table);
  94. indexreg := cg.getaddressregister(current_asmdata.CurrAsmList);
  95. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, OP_SHL, OS_ADDR, 2, hregister, indexreg);
  96. { create reference }
  97. reference_reset_symbol(href, table, 0, sizeof(aint), []);
  98. href.offset := (-aint(min_)) * 4;
  99. href.base:=indexreg;
  100. jmpreg := cg.getaddressregister(current_asmdata.CurrAsmList);
  101. cg.a_load_ref_reg(current_asmdata.CurrAsmList, OS_ADDR, OS_ADDR, href, jmpreg);
  102. if (cs_create_pic in current_settings.moduleswitches) then
  103. begin
  104. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,OP_ADD,OS_ADDR,NR_GP,jmpreg,jmpreg);
  105. labeltyp:=aitconst_gotoff_symbol;
  106. end
  107. else
  108. labeltyp:=aitconst_ptr;
  109. current_asmdata.CurrAsmList.concat(taicpu.op_reg(A_JR, jmpreg));
  110. { Delay slot }
  111. current_asmdata.CurrAsmList.concat(taicpu.op_none(A_NOP));
  112. { generate jump table }
  113. new_section(jumpSegment,sec_rodata,current_procinfo.procdef.mangledname,sizeof(aint));
  114. jumpSegment.concat(Tai_label.Create(table));
  115. genitem(hp);
  116. end;
  117. begin
  118. ccasenode := tcpucasenode;
  119. end.