ncpuset.pas 3.9 KB

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