ncpuset.pas 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl and Carl Eric Codere
  3. Generate Risc-V32/64 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. node,nset,ncgset,cpubase,cgbase,cgobj,aasmbase,aasmtai,aasmdata,globtype;
  22. type
  23. tloongarch64casenode = class(tcgcasenode)
  24. protected
  25. procedure optimizevalues(var max_linear_list : int64; var max_dist : qword);override;
  26. function has_jumptable : boolean;override;
  27. procedure genjumptable(hp : pcaselabel;min_,max_ : int64);override;
  28. end;
  29. implementation
  30. uses
  31. systems,
  32. verbose,globals,constexp,
  33. symconst,symdef,defutil,
  34. paramgr,
  35. cpuinfo,
  36. pass_2,cgcpu,
  37. ncon,
  38. tgobj,ncgutil,rgobj,aasmcpu,
  39. procinfo,
  40. cgutils;
  41. {*****************************************************************************
  42. TCGCASENODE
  43. *****************************************************************************}
  44. procedure tloongarch64casenode.optimizevalues(var max_linear_list : int64; var max_dist : qword);
  45. begin
  46. max_linear_list := 3;
  47. end;
  48. function tloongarch64casenode.has_jumptable : boolean;
  49. begin
  50. has_jumptable:=true;
  51. end;
  52. procedure tloongarch64casenode.genjumptable(hp : pcaselabel;min_,max_ : int64);
  53. var
  54. table : tasmlabel;
  55. last : TConstExprInt;
  56. indexreg : tregister;
  57. href : treference;
  58. procedure genitem(list:TAsmList;t : pcaselabel);
  59. var
  60. i : TConstExprInt;
  61. begin
  62. if assigned(t^.less) then
  63. genitem(list,t^.less);
  64. { fill possible hole }
  65. i:=last+1;
  66. while i<=t^._low-1 do
  67. begin
  68. list.concat(Tai_const.Create_sym_offset(elselabel,0));
  69. i:=i+1;
  70. end;
  71. i:=t^._low;
  72. while i<=t^._high do
  73. begin
  74. list.concat(Tai_const.Create_sym_offset(blocklabel(t^.blockid),0));
  75. i:=i+1;
  76. end;
  77. last:=t^._high;
  78. if assigned(t^.greater) then
  79. genitem(list,t^.greater);
  80. end;
  81. begin
  82. last:=min_;
  83. {
  84. la.pcrel x,tbl
  85. alsl.d y,idx,x,3
  86. ld.d z,y,0
  87. jr z
  88. }
  89. indexreg:= cg.makeregsize(current_asmdata.CurrAsmList, hregister, OS_INT);
  90. { indexreg := hregister; }
  91. cg.a_load_reg_reg(current_asmdata.CurrAsmList, def_cgsize(opsize), OS_INT, hregister, indexreg);
  92. { a <= x <= b <-> unsigned(x-a) <= (b-a) }
  93. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_SUB,OS_INT,aint(min_),indexreg);
  94. if not(jumptable_no_range) then
  95. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_INT,OC_A,aint(max_)-aint(min_),indexreg,elselabel);
  96. current_asmdata.getjumplabel(table);
  97. hregister:=cg.getaddressregister(current_asmdata.CurrAsmList);
  98. { la.pcrel x,tbl }
  99. reference_reset_symbol(href, table, 0, 4,[]);
  100. href.refaddr:=addr_pcrel;
  101. cg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,href,hregister);
  102. { alsl.d y,idx,x,3 }
  103. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg_const(A_ALSL_D,hregister,indexreg,hregister,3));
  104. { ld.d z,y,0 }
  105. reference_reset_base(href,hregister,0,ctempposinvalid,4,[]);
  106. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,href,hregister);
  107. { jr z }
  108. reference_reset_base(href,hregister,0,ctempposinvalid,4,[]);
  109. href.refaddr:=addr_reg;
  110. current_asmdata.CurrAsmList.concat(taicpu.op_ref(A_JR,href));
  111. { generate jump table }
  112. current_asmdata.CurrAsmList.concat(cai_align.Create(8));
  113. current_asmdata.CurrAsmList.concat(Tai_label.Create(table));
  114. genitem(current_asmdata.CurrAsmList,hp);
  115. end;
  116. begin
  117. ccasenode:=tloongarch64casenode;
  118. end.