navrinl.pas 8.1 KB

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