navrinl.pas 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. {
  2. Copyright (c) 1998-2017 by Florian Klaempfl
  3. Generates AVR inline 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 navrinl;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. node,ninl,ncginl, aasmbase;
  22. type
  23. tavrinlinenode = class(tcginlinenode)
  24. procedure second_abs_long; override;
  25. function pass_typecheck_cpu:tnode;override;
  26. function first_cpu : tnode;override;
  27. procedure pass_generate_code_cpu;override;
  28. end;
  29. implementation
  30. uses
  31. verbose,
  32. constexp,
  33. compinnr,
  34. aasmdata,
  35. aasmcpu,
  36. symdef,
  37. defutil,
  38. hlcgobj,
  39. pass_2,
  40. cgbase, cgobj, cgutils,
  41. ncon,ncal,
  42. cpubase;
  43. procedure tavrinlinenode.second_abs_long;
  44. var
  45. hl: TAsmLabel;
  46. size: TCgSize;
  47. begin
  48. secondpass(left);
  49. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,false);
  50. location:=left.location;
  51. location.register:=hlcg.getintregister(current_asmdata.CurrAsmList,left.resultdef);
  52. size:=def_cgsize(left.resultdef);
  53. current_asmdata.getjumplabel(hl);
  54. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,size,OC_GTE,0,left.location.register,hl);
  55. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_NEG,size,left.location.register,location.register);
  56. cg.a_label(current_asmdata.CurrAsmList,hl);
  57. end;
  58. function tavrinlinenode.pass_typecheck_cpu : tnode;
  59. begin
  60. Result:=nil;
  61. case inlinenumber of
  62. in_avr_nop,
  63. in_avr_sleep,
  64. in_avr_sei,
  65. in_avr_wdr,
  66. in_avr_cli:
  67. begin
  68. CheckParameters(0);
  69. resultdef:=voidtype;
  70. end;
  71. in_avr_save:
  72. begin
  73. CheckParameters(0);
  74. resultdef:=u8inttype;
  75. end;
  76. in_avr_restore:
  77. begin
  78. CheckParameters(1);
  79. resultdef:=voidtype;
  80. end;
  81. in_avr_des:
  82. begin
  83. CheckParameters(2);
  84. resultdef:=voidtype;
  85. if not(is_constintnode(tcallparanode(left).paravalue)) then
  86. MessagePos(tcallparanode(left).paravalue.fileinfo,type_e_constant_expr_expected);
  87. if not(is_constboolnode(tcallparanode(tcallparanode(left).nextpara).paravalue)) then
  88. MessagePos(tcallparanode(tcallparanode(left).nextpara).paravalue.fileinfo,type_e_constant_expr_expected);
  89. if (tordconstnode(tcallparanode(left).paravalue).value<0) or
  90. (tordconstnode(tcallparanode(left).paravalue).value>15) then
  91. MessagePos(tcallparanode(left).paravalue.fileinfo,parser_e_range_check_error);
  92. end;
  93. else
  94. Result:=inherited pass_typecheck_cpu;
  95. end;
  96. end;
  97. function tavrinlinenode.first_cpu : tnode;
  98. begin
  99. Result:=nil;
  100. case inlinenumber of
  101. in_avr_nop,
  102. in_avr_sleep,
  103. in_avr_sei,
  104. in_avr_wdr,
  105. in_avr_cli,
  106. in_avr_restore,
  107. in_avr_des:
  108. begin
  109. expectloc:=LOC_VOID;
  110. resultdef:=voidtype;
  111. end;
  112. in_avr_save:
  113. begin
  114. expectloc:=LOC_REGISTER;
  115. resultdef:=u8inttype;
  116. end;
  117. else
  118. Result:=inherited first_cpu;
  119. end;
  120. end;
  121. procedure tavrinlinenode.pass_generate_code_cpu;
  122. begin
  123. case inlinenumber of
  124. in_avr_nop:
  125. current_asmdata.CurrAsmList.concat(taicpu.op_none(A_NOP));
  126. in_avr_sleep:
  127. current_asmdata.CurrAsmList.concat(taicpu.op_none(A_SLEEP));
  128. in_avr_sei:
  129. current_asmdata.CurrAsmList.concat(taicpu.op_none(A_SEI));
  130. in_avr_wdr:
  131. current_asmdata.CurrAsmList.concat(taicpu.op_none(A_WDR));
  132. in_avr_cli:
  133. current_asmdata.CurrAsmList.concat(taicpu.op_none(A_CLI));
  134. in_avr_save:
  135. begin
  136. location_reset(location,LOC_CREGISTER,OS_8);
  137. location.register:=cg.getintregister(current_asmdata.CurrAsmList,OS_8);
  138. current_asmdata.CurrAsmList.concat(taicpu.op_reg_const(A_IN, location.register, NIO_SREG));
  139. current_asmdata.CurrAsmList.concat(taicpu.op_none(A_CLI));
  140. end;
  141. in_avr_restore:
  142. begin
  143. secondpass(left);
  144. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,false);
  145. current_asmdata.CurrAsmList.concat(taicpu.op_const_reg(A_OUT, NIO_SREG, left.location.register));
  146. end;
  147. in_avr_des:
  148. begin
  149. if tordconstnode(tcallparanode(tcallparanode(left).nextpara).paravalue).value=0 then
  150. current_asmdata.CurrAsmList.concat(taicpu.op_none(A_CLH))
  151. else
  152. current_asmdata.CurrAsmList.concat(taicpu.op_none(A_SEH));
  153. current_asmdata.CurrAsmList.concat(taicpu.op_const(A_DES,int64(tordconstnode(tcallparanode(left).paravalue).value)));
  154. end;
  155. else
  156. inherited pass_generate_code_cpu;
  157. end;
  158. end;
  159. begin
  160. cinlinenode:=tavrinlinenode;
  161. end.