2
0

navrmat.pas 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 : 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. { avr has no cpci, so we use the first register as "zero" register }
  65. for i:=2 to tcgsize2size[left.location.size] do
  66. begin
  67. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CPC,tmpreg,left.location.register));
  68. end;
  69. location_reset(location,LOC_FLAGS,OS_NO);
  70. location.resflags:=F_EQ;
  71. end;
  72. else
  73. internalerror(2003042401);
  74. end;
  75. end;
  76. end;
  77. procedure tavrshlshrnode.second_integer;
  78. var
  79. op : topcg;
  80. opdef: tdef;
  81. hcountreg : tregister;
  82. opsize : tcgsize;
  83. shiftval : longint;
  84. begin
  85. { determine operator }
  86. case nodetype of
  87. shln: op:=OP_SHL;
  88. shrn: op:=OP_SHR;
  89. else
  90. internalerror(2013120102);
  91. end;
  92. opsize:=left.location.size;
  93. opdef:=left.resultdef;
  94. if not(left.location.loc in [LOC_CREGISTER,LOC_REGISTER]) or
  95. { location_force_reg can be also used to change the size of a register }
  96. (left.location.size<>opsize) then
  97. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,opdef,true);
  98. location_reset(location,LOC_REGISTER,opsize);
  99. location.register:=hlcg.getintregister(current_asmdata.CurrAsmList,resultdef);
  100. { shifting by a constant directly coded: }
  101. if (right.nodetype=ordconstn) then
  102. begin
  103. { shl/shr must "wrap around", so use ... and 31 }
  104. { In TP, "byte/word shl 16 = 0", so no "and 15" in case of
  105. a 16 bit ALU }
  106. if tcgsize2size[opsize]<=4 then
  107. shiftval:=tordconstnode(right).value.uvalue and 31
  108. else
  109. shiftval:=tordconstnode(right).value.uvalue and 63;
  110. hlcg.a_op_const_reg_reg(current_asmdata.CurrAsmList,op,opdef,
  111. shiftval,left.location.register,location.register);
  112. end
  113. else
  114. begin
  115. { load right operators in a register - this
  116. is done since most target cpu which will use this
  117. node do not support a shift count in a mem. location (cec)
  118. }
  119. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,sinttype,true);
  120. hlcg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,op,opdef,right.location.register,left.location.register,location.register);
  121. end;
  122. { shl/shr nodes return the same type as left, which can be different
  123. from opdef }
  124. if opdef<>resultdef then
  125. begin
  126. hcountreg:=hlcg.getintregister(current_asmdata.CurrAsmList,resultdef);
  127. hlcg.a_load_reg_reg(current_asmdata.CurrAsmList,opdef,resultdef,location.register,hcountreg);
  128. location.register:=hcountreg;
  129. end;
  130. end;
  131. begin
  132. cnotnode:=tavrnotnode;
  133. cshlshrnode:=tavrshlshrnode;
  134. end.