navrmat.pas 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. {
  2. Copyright (c) 1998-2008 by Florian Klaempfl
  3. Generates AVR assembler for math 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 navrmat;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. node,nmat,ncgmat;
  22. type
  23. tavrnotnode = class(tcgnotnode)
  24. procedure second_boolean;override;
  25. end;
  26. tavrshlshrnode = class(tcgshlshrnode)
  27. procedure second_integer;override;
  28. end;
  29. implementation
  30. uses
  31. globtype,systems,
  32. cutils,verbose,globals,constexp,
  33. symtype,symdef,
  34. aasmbase,aasmcpu,aasmtai,aasmdata,
  35. defutil,
  36. cgbase,cgobj,hlcgobj,cgutils,
  37. pass_2,procinfo,
  38. ncon,
  39. cpubase,
  40. ncgutil,cgcpu;
  41. {*****************************************************************************
  42. TAVRNOTNODE
  43. *****************************************************************************}
  44. procedure tavrnotnode.second_boolean;
  45. var
  46. tmpreg,lreg : tregister;
  47. i : longint;
  48. begin
  49. if not handle_locjump then
  50. begin
  51. secondpass(left);
  52. case left.location.loc of
  53. LOC_FLAGS :
  54. begin
  55. location_copy(location,left.location);
  56. inverse_flags(location.resflags);
  57. end;
  58. LOC_REGISTER,LOC_CREGISTER,LOC_REFERENCE,LOC_CREFERENCE,
  59. LOC_SUBSETREG,LOC_CSUBSETREG,LOC_SUBSETREF,LOC_CSUBSETREF :
  60. begin
  61. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true);
  62. current_asmdata.CurrAsmList.concat(taicpu.op_reg_const(A_CPI,left.location.register,0));
  63. tmpreg:=left.location.register;
  64. for i:=2 to tcgsize2size[left.location.size] do
  65. begin
  66. if i=5 then
  67. tmpreg:=left.location.registerhi
  68. else
  69. tmpreg:=cg.GetNextReg(tmpreg);
  70. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CPC,NR_R1,tmpreg));
  71. end;
  72. location_reset(location,LOC_FLAGS,OS_NO);
  73. location.resflags:=F_EQ;
  74. end;
  75. else
  76. internalerror(2003042401);
  77. end;
  78. end;
  79. end;
  80. procedure tavrshlshrnode.second_integer;
  81. var
  82. op : topcg;
  83. opdef: tdef;
  84. hcountreg : tregister;
  85. opsize : tcgsize;
  86. shiftval : longint;
  87. begin
  88. { determine operator }
  89. case nodetype of
  90. shln: op:=OP_SHL;
  91. shrn: op:=OP_SHR;
  92. else
  93. internalerror(2013120102);
  94. end;
  95. opsize:=left.location.size;
  96. opdef:=left.resultdef;
  97. if not(left.location.loc in [LOC_CREGISTER,LOC_REGISTER]) or
  98. { location_force_reg can be also used to change the size of a register }
  99. (left.location.size<>opsize) then
  100. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,opdef,true);
  101. location_reset(location,LOC_REGISTER,opsize);
  102. location.register:=hlcg.getintregister(current_asmdata.CurrAsmList,resultdef);
  103. { shifting by a constant directly coded: }
  104. if (right.nodetype=ordconstn) then
  105. begin
  106. { shl/shr must "wrap around", so use ... and 31 }
  107. { In TP, "byte/word shl 16 = 0", so no "and 15" in case of
  108. a 16 bit ALU }
  109. if tcgsize2size[opsize]<=4 then
  110. shiftval:=tordconstnode(right).value.uvalue and 31
  111. else
  112. shiftval:=tordconstnode(right).value.uvalue and 63;
  113. hlcg.a_op_const_reg_reg(current_asmdata.CurrAsmList,op,opdef,
  114. shiftval,left.location.register,location.register);
  115. end
  116. else
  117. begin
  118. { load right operators in a register - this
  119. is done since most target cpu which will use this
  120. node do not support a shift count in a mem. location (cec)
  121. }
  122. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,sinttype,true);
  123. hlcg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,op,opdef,right.location.register,left.location.register,location.register);
  124. end;
  125. { shl/shr nodes return the same type as left, which can be different
  126. from opdef }
  127. if opdef<>resultdef then
  128. begin
  129. hcountreg:=hlcg.getintregister(current_asmdata.CurrAsmList,resultdef);
  130. hlcg.a_load_reg_reg(current_asmdata.CurrAsmList,opdef,resultdef,location.register,hcountreg);
  131. location.register:=hcountreg;
  132. end;
  133. end;
  134. begin
  135. cnotnode:=tavrnotnode;
  136. cshlshrnode:=tavrshlshrnode;
  137. end.