navrmat.pas 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. hl : tasmlabel;
  47. tmpreg : tregister;
  48. i : longint;
  49. begin
  50. { if the location is LOC_JUMP, we do the secondpass after the
  51. labels are allocated
  52. }
  53. if left.expectloc=LOC_JUMP then
  54. begin
  55. hl:=current_procinfo.CurrTrueLabel;
  56. current_procinfo.CurrTrueLabel:=current_procinfo.CurrFalseLabel;
  57. current_procinfo.CurrFalseLabel:=hl;
  58. secondpass(left);
  59. if left.location.loc<>LOC_JUMP then
  60. internalerror(2012081304);
  61. maketojumpbool(current_asmdata.CurrAsmList,left,lr_load_regvars);
  62. hl:=current_procinfo.CurrTrueLabel;
  63. current_procinfo.CurrTrueLabel:=current_procinfo.CurrFalseLabel;
  64. current_procinfo.CurrFalseLabel:=hl;
  65. location.loc:=LOC_JUMP;
  66. end
  67. else
  68. begin
  69. secondpass(left);
  70. case left.location.loc of
  71. LOC_FLAGS :
  72. begin
  73. location_copy(location,left.location);
  74. inverse_flags(location.resflags);
  75. end;
  76. LOC_REGISTER,LOC_CREGISTER,LOC_REFERENCE,LOC_CREFERENCE,
  77. LOC_SUBSETREG,LOC_CSUBSETREG,LOC_SUBSETREF,LOC_CSUBSETREF :
  78. begin
  79. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true);
  80. current_asmdata.CurrAsmList.concat(taicpu.op_reg_const(A_CPI,left.location.register,0));
  81. tmpreg:=left.location.register;
  82. { avr has no cpci, so we use the first register as "zero" register }
  83. for i:=2 to tcgsize2size[left.location.size] do
  84. begin
  85. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CPC,tmpreg,left.location.register));
  86. end;
  87. location_reset(location,LOC_FLAGS,OS_NO);
  88. location.resflags:=F_EQ;
  89. end;
  90. else
  91. internalerror(2003042401);
  92. end;
  93. end;
  94. end;
  95. procedure tavrshlshrnode.second_integer;
  96. var
  97. op : topcg;
  98. opdef: tdef;
  99. hcountreg : tregister;
  100. opsize : tcgsize;
  101. shiftval : longint;
  102. begin
  103. { determine operator }
  104. case nodetype of
  105. shln: op:=OP_SHL;
  106. shrn: op:=OP_SHR;
  107. else
  108. internalerror(2013120102);
  109. end;
  110. opsize:=left.location.size;
  111. opdef:=left.resultdef;
  112. if not(left.location.loc in [LOC_CREGISTER,LOC_REGISTER]) or
  113. { location_force_reg can be also used to change the size of a register }
  114. (left.location.size<>opsize) then
  115. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,opdef,true);
  116. location_reset(location,LOC_REGISTER,opsize);
  117. location.register:=hlcg.getintregister(current_asmdata.CurrAsmList,resultdef);
  118. { shifting by a constant directly coded: }
  119. if (right.nodetype=ordconstn) then
  120. begin
  121. { shl/shr must "wrap around", so use ... and 31 }
  122. { In TP, "byte/word shl 16 = 0", so no "and 15" in case of
  123. a 16 bit ALU }
  124. if tcgsize2size[opsize]<=4 then
  125. shiftval:=tordconstnode(right).value.uvalue and 31
  126. else
  127. shiftval:=tordconstnode(right).value.uvalue and 63;
  128. hlcg.a_op_const_reg_reg(current_asmdata.CurrAsmList,op,opdef,
  129. shiftval,left.location.register,location.register);
  130. end
  131. else
  132. begin
  133. { load right operators in a register - this
  134. is done since most target cpu which will use this
  135. node do not support a shift count in a mem. location (cec)
  136. }
  137. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,sinttype,true);
  138. hlcg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,op,opdef,right.location.register,left.location.register,location.register);
  139. end;
  140. { shl/shr nodes return the same type as left, which can be different
  141. from opdef }
  142. if opdef<>resultdef then
  143. begin
  144. hcountreg:=hlcg.getintregister(current_asmdata.CurrAsmList,resultdef);
  145. hlcg.a_load_reg_reg(current_asmdata.CurrAsmList,opdef,resultdef,location.register,hcountreg);
  146. location.register:=hcountreg;
  147. end;
  148. end;
  149. begin
  150. cnotnode:=tavrnotnode;
  151. cshlshrnode:=tavrshlshrnode;
  152. end.