navrinl.pas 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. var
  60. para1,para2,para3,para4: tcallparanode;
  61. begin
  62. Result:=nil;
  63. case inlinenumber of
  64. in_avr_nop,
  65. in_avr_sleep,
  66. in_avr_sei,
  67. in_avr_wdr,
  68. in_avr_cli:
  69. begin
  70. CheckParameters(0);
  71. resultdef:=voidtype;
  72. end;
  73. in_avr_save:
  74. begin
  75. CheckParameters(0);
  76. resultdef:=u8inttype;
  77. end;
  78. in_avr_restore:
  79. begin
  80. CheckParameters(1);
  81. resultdef:=voidtype;
  82. end;
  83. in_avr_des:
  84. begin
  85. CheckParameters(4);
  86. resultdef:=voidtype;
  87. para4:=tcallparanode(left);
  88. para3:=tcallparanode(tcallparanode(para4).nextpara);
  89. para2:=tcallparanode(tcallparanode(para3).nextpara);
  90. para1:=tcallparanode(tcallparanode(para2).nextpara);
  91. if not(is_constintnode(para4.paravalue)) then
  92. MessagePos(para4.paravalue.fileinfo,type_e_constant_expr_expected);
  93. if not(is_constboolnode(para3.paravalue)) then
  94. MessagePos(para3.paravalue.fileinfo,type_e_constant_expr_expected);
  95. if (tordconstnode(para4.paravalue).value<0) or
  96. (tordconstnode(para4.paravalue).value>15) then
  97. MessagePos(para4.paravalue.fileinfo,parser_e_range_check_error);
  98. end;
  99. else
  100. Result:=inherited pass_typecheck_cpu;
  101. end;
  102. end;
  103. function tavrinlinenode.first_cpu : tnode;
  104. begin
  105. Result:=nil;
  106. case inlinenumber of
  107. in_avr_nop,
  108. in_avr_sleep,
  109. in_avr_sei,
  110. in_avr_wdr,
  111. in_avr_cli,
  112. in_avr_restore,
  113. in_avr_des:
  114. begin
  115. expectloc:=LOC_VOID;
  116. resultdef:=voidtype;
  117. end;
  118. in_avr_save:
  119. begin
  120. expectloc:=LOC_REGISTER;
  121. resultdef:=u8inttype;
  122. end;
  123. else
  124. Result:=inherited first_cpu;
  125. end;
  126. end;
  127. procedure tavrinlinenode.pass_generate_code_cpu;
  128. var
  129. para1,para2,para3,para4: tcallparanode;
  130. ref: treference;
  131. r: TRegister;
  132. begin
  133. case inlinenumber of
  134. in_avr_nop:
  135. current_asmdata.CurrAsmList.concat(taicpu.op_none(A_NOP));
  136. in_avr_sleep:
  137. current_asmdata.CurrAsmList.concat(taicpu.op_none(A_SLEEP));
  138. in_avr_sei:
  139. current_asmdata.CurrAsmList.concat(taicpu.op_none(A_SEI));
  140. in_avr_wdr:
  141. current_asmdata.CurrAsmList.concat(taicpu.op_none(A_WDR));
  142. in_avr_cli:
  143. current_asmdata.CurrAsmList.concat(taicpu.op_none(A_CLI));
  144. in_avr_save:
  145. begin
  146. location_reset(location,LOC_CREGISTER,OS_8);
  147. location.register:=cg.getintregister(current_asmdata.CurrAsmList,OS_8);
  148. current_asmdata.CurrAsmList.concat(taicpu.op_reg_const(A_IN, location.register, NIO_SREG));
  149. current_asmdata.CurrAsmList.concat(taicpu.op_none(A_CLI));
  150. end;
  151. in_avr_restore:
  152. begin
  153. secondpass(left);
  154. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,false);
  155. current_asmdata.CurrAsmList.concat(taicpu.op_const_reg(A_OUT, NIO_SREG, left.location.register));
  156. end;
  157. in_avr_des:
  158. begin
  159. para4:=tcallparanode(left);
  160. para3:=tcallparanode(tcallparanode(para4).nextpara);
  161. para2:=tcallparanode(tcallparanode(para3).nextpara);
  162. para1:=tcallparanode(tcallparanode(para2).nextpara);
  163. secondpass(tcallparanode(para1).paravalue);
  164. secondpass(tcallparanode(para2).paravalue);
  165. cg.getcpuregister(current_asmdata.CurrAsmList,NR_R30);
  166. cg.getcpuregister(current_asmdata.CurrAsmList,NR_R31);
  167. cg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,tcallparanode(para2).paravalue.location.reference,NR_R30);
  168. reference_reset(ref,0,[]);
  169. ref.base:=NR_R30;
  170. for r:=NR_R8 to NR_R15 do
  171. begin
  172. cg.getcpuregister(current_asmdata.CurrAsmList,r);
  173. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_8,OS_8,ref,r);
  174. inc(ref.offset);
  175. end;
  176. cg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,tcallparanode(para1).paravalue.location.reference,NR_R30);
  177. reference_reset(ref,0,[]);
  178. ref.base:=NR_R30;
  179. for r:=NR_R0 to NR_R7 do
  180. begin
  181. cg.getcpuregister(current_asmdata.CurrAsmList,r);
  182. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_8,OS_8,ref,r);
  183. inc(ref.offset);
  184. end;
  185. if tordconstnode(para3.paravalue).value=0 then
  186. current_asmdata.CurrAsmList.concat(taicpu.op_none(A_CLH))
  187. else
  188. current_asmdata.CurrAsmList.concat(taicpu.op_none(A_SEH));
  189. current_asmdata.CurrAsmList.concat(taicpu.op_const(A_DES,int64(tordconstnode(para4.paravalue).value)));
  190. for r:=NR_R8 to NR_R15 do
  191. cg.ungetcpuregister(current_asmdata.CurrAsmList,r);
  192. { save data }
  193. ref.offset:=0;
  194. for r:=NR_R0 to NR_R7 do
  195. begin
  196. cg.a_load_reg_ref(current_asmdata.CurrAsmList,OS_8,OS_8,r,ref);
  197. cg.ungetcpuregister(current_asmdata.CurrAsmList,r);
  198. inc(ref.offset);
  199. end;
  200. end
  201. else
  202. inherited pass_generate_code_cpu;
  203. end;
  204. end;
  205. begin
  206. cinlinenode:=tavrinlinenode;
  207. end.