hlcgcpu.pas 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. {
  2. Copyright (c) 1998-2010 by Florian Klaempfl and Jonas Maebe
  3. Member of the Free Pascal development team
  4. This unit contains routines to create a pass-through high-level code
  5. generator. This is used by most regular code generators.
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. ****************************************************************************
  18. }
  19. unit hlcgcpu;
  20. {$i fpcdefs.inc}
  21. interface
  22. uses
  23. symtype,
  24. aasmdata,
  25. cgbase,cgutils,
  26. hlcgobj, hlcg2ll;
  27. type
  28. thlcgaarch64 = class(thlcg2ll)
  29. procedure a_load_subsetreg_reg(list: TAsmList; subsetsize, tosize: tdef; const sreg: tsubsetregister; destreg: tregister); override;
  30. procedure a_load_subsetreg_subsetreg(list: TAsmlist; fromsubsetsize, tosubsetsize: tdef; const fromsreg, tosreg: tsubsetregister); override;
  31. protected
  32. procedure a_load_regconst_subsetreg_intern(list: TAsmList; fromsize, subsetsize: tdef; fromreg: tregister; const sreg: tsubsetregister; slopt: tsubsetloadopt); override;
  33. end;
  34. procedure create_hlcodegen;
  35. implementation
  36. uses
  37. defutil,
  38. cpubase,aasmcpu,
  39. cgobj,cgcpu;
  40. procedure thlcgaarch64.a_load_subsetreg_reg(list: TAsmList; subsetsize, tosize: tdef; const sreg: tsubsetregister; destreg: tregister);
  41. var
  42. op: tasmop;
  43. tocgsize: tcgsize;
  44. tmpdestreg: tregister;
  45. begin
  46. tocgsize:=def_cgsize(tosize);
  47. if (sreg.startbit<>0) or
  48. not(sreg.bitlen in [32,64]) then
  49. begin
  50. if is_signed(subsetsize) then
  51. op:=A_SBFX
  52. else
  53. op:=A_UBFX;
  54. { source and destination register of SBFX/UBFX have to be the same size }
  55. if (sreg.subsetregsize in [OS_64,OS_S64]) and
  56. not(tocgsize in [OS_64,OS_S64]) then
  57. tmpdestreg:=cg.getintregister(list,OS_64)
  58. else if not(sreg.subsetregsize in [OS_64,OS_S64]) and
  59. (tocgsize in [OS_64,OS_S64]) then
  60. tmpdestreg:=cg.getintregister(list,OS_32)
  61. else
  62. tmpdestreg:=destreg;
  63. list.concat(taicpu.op_reg_reg_const_const(op,tmpdestreg,sreg.subsetreg,sreg.startbit,sreg.bitlen));
  64. { need to sign extend further or truncate? }
  65. if (sreg.subsetregsize=OS_S64) and
  66. not(tocgsize in [OS_64,OS_S64]) then
  67. cg.a_load_reg_reg(list,OS_S64,tocgsize,tmpdestreg,destreg)
  68. else if is_signed(subsetsize) and
  69. (tocgsize in [OS_8,OS_16]) then
  70. cg.a_load_reg_reg(list,OS_32,tocgsize,tmpdestreg,destreg)
  71. else if tmpdestreg<>destreg then
  72. cg.a_load_reg_reg(list,def_cgsize(subsetsize),tocgsize,tmpdestreg,destreg)
  73. end
  74. else
  75. cg.a_load_reg_reg(list,def_cgsize(subsetsize),tocgsize,sreg.subsetreg,destreg);
  76. end;
  77. procedure makeregssamesize(list: tasmlist; fromsize, tosize: tcgsize; orgfromreg, orgtoreg: tregister; out newfromreg, newtoreg: tregister);
  78. begin
  79. if (fromsize in [OS_S64,OS_64])<>
  80. (tosize in [OS_S64,OS_64]) then
  81. begin
  82. newfromreg:=cg.makeregsize(list,orgfromreg,OS_64);
  83. newtoreg:=cg.makeregsize(list,orgtoreg,OS_64);
  84. end
  85. else
  86. begin
  87. newfromreg:=orgfromreg;
  88. newtoreg:=orgtoreg;
  89. end;
  90. end;
  91. procedure thlcgaarch64.a_load_subsetreg_subsetreg(list: TAsmlist; fromsubsetsize, tosubsetsize: tdef; const fromsreg, tosreg: tsubsetregister);
  92. var
  93. fromreg, toreg: tregister;
  94. begin
  95. { BFM can only insert a bitfield that starts at position 0 in the source
  96. source or destination register }
  97. if (tosreg.startbit=0) and
  98. (fromsreg.bitlen>=tosreg.bitlen) then
  99. begin
  100. makeregssamesize(list,fromsreg.subsetregsize,tosreg.subsetregsize,fromsreg.subsetreg,tosreg.subsetreg,fromreg,toreg);
  101. list.concat(taicpu.op_reg_reg_const_const(A_BFXIL,toreg,fromreg,fromsreg.startbit,tosreg.bitlen))
  102. end
  103. else if (fromsreg.startbit=0) and
  104. (fromsreg.bitlen>=tosreg.bitlen) then
  105. begin
  106. makeregssamesize(list,fromsreg.subsetregsize,tosreg.subsetregsize,fromsreg.subsetreg,tosreg.subsetreg,fromreg,toreg);
  107. list.concat(taicpu.op_reg_reg_const_const(A_BFI,toreg,fromreg,tosreg.startbit,tosreg.bitlen))
  108. end
  109. else
  110. inherited;
  111. end;
  112. procedure thlcgaarch64.a_load_regconst_subsetreg_intern(list: TAsmList; fromsize, subsetsize: tdef; fromreg: tregister; const sreg: tsubsetregister; slopt: tsubsetloadopt);
  113. var
  114. toreg: tregister;
  115. begin
  116. if slopt in [SL_SETZERO,SL_SETMAX] then
  117. inherited
  118. else if not(sreg.bitlen in [32,64]) or
  119. (sreg.startbit<>0) then
  120. begin
  121. makeregssamesize(list,def_cgsize(fromsize),sreg.subsetregsize,fromreg,sreg.subsetreg,fromreg,toreg);
  122. list.concat(taicpu.op_reg_reg_const_const(A_BFI,toreg,fromreg,sreg.startbit,sreg.bitlen))
  123. end
  124. else
  125. a_load_reg_reg(list,fromsize,subsetsize,fromreg,sreg.subsetreg);
  126. end;
  127. procedure create_hlcodegen;
  128. begin
  129. hlcg:=thlcgaarch64.create;
  130. create_codegen;
  131. end;
  132. end.