nz80add.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. {
  2. Copyright (c) 2008 by Florian Klaempfl
  3. Code generation for add nodes on the AVR
  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 nz80add;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. node,ncgadd, symtype,cpubase;
  22. type
  23. TZ80AddNode = class(tcgaddnode)
  24. private
  25. function GetResFlags(unsigned:Boolean):TResFlags;
  26. protected
  27. function pass_1 : tnode;override;
  28. procedure second_cmpordinal;override;
  29. procedure second_cmpsmallset;override;
  30. procedure second_cmp64bit;override;
  31. procedure second_cmp;
  32. end;
  33. implementation
  34. uses
  35. globtype,systems,
  36. cutils,verbose,globals,
  37. symconst,symdef,paramgr,
  38. aasmbase,aasmtai,aasmdata,aasmcpu,defutil,htypechk,
  39. cgbase,cgutils,cgcpu,
  40. cpuinfo,pass_1,pass_2,procinfo,
  41. cpupara,
  42. ncon,nset,nadd,
  43. ncgutil,tgobj,rgobj,rgcpu,cgobj,cg64f32,
  44. hlcgobj;
  45. {*****************************************************************************
  46. TZ80AddNode
  47. *****************************************************************************}
  48. function TZ80AddNode.GetResFlags(unsigned:Boolean):TResFlags;
  49. begin
  50. case NodeType of
  51. equaln:
  52. GetResFlags:=F_E;
  53. unequaln:
  54. GetResFlags:=F_NE;
  55. else
  56. if not(unsigned) then
  57. begin
  58. { signed }
  59. if nf_swapped in flags then
  60. case NodeType of
  61. ltn:
  62. GetResFlags:=F_NotPossible;
  63. lten:
  64. GetResFlags:=F_P;
  65. gtn:
  66. GetResFlags:=F_M;
  67. gten:
  68. GetResFlags:=F_NotPossible;
  69. else
  70. internalerror(2014082020);
  71. end
  72. else
  73. case NodeType of
  74. ltn:
  75. GetResFlags:=F_M;
  76. lten:
  77. GetResFlags:=F_NotPossible;
  78. gtn:
  79. GetResFlags:=F_NotPossible;
  80. gten:
  81. GetResFlags:=F_P;
  82. else
  83. internalerror(2014082021);
  84. end;
  85. end
  86. else
  87. begin
  88. { unsigned }
  89. if nf_swapped in Flags then
  90. case NodeType of
  91. ltn:
  92. GetResFlags:=F_NotPossible;
  93. lten:
  94. GetResFlags:=F_NC;
  95. gtn:
  96. GetResFlags:=F_C;
  97. gten:
  98. GetResFlags:=F_NotPossible;
  99. else
  100. internalerror(2014082022);
  101. end
  102. else
  103. case NodeType of
  104. ltn:
  105. GetResFlags:=F_C;
  106. lten:
  107. GetResFlags:=F_NotPossible;
  108. gtn:
  109. GetResFlags:=F_NotPossible;
  110. gten:
  111. GetResFlags:=F_NC;
  112. else
  113. internalerror(2014082023);
  114. end;
  115. end;
  116. end;
  117. end;
  118. procedure TZ80AddNode.second_cmpsmallset;
  119. procedure gencmp(tmpreg1,tmpreg2 : tregister);
  120. var
  121. i : byte;
  122. begin
  123. //current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CP,tmpreg1,tmpreg2));
  124. //for i:=2 to tcgsize2size[left.location.size] do
  125. // begin
  126. // tmpreg1:=GetNextReg(tmpreg1);
  127. // tmpreg2:=GetNextReg(tmpreg2);
  128. // current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CPC,tmpreg1,tmpreg2));
  129. // end;
  130. end;
  131. var
  132. tmpreg : tregister;
  133. begin
  134. //pass_left_right;
  135. //location_reset(location,LOC_FLAGS,OS_NO);
  136. //force_reg_left_right(false,false);
  137. //
  138. //case nodetype of
  139. // equaln:
  140. // begin
  141. // gencmp(left.location.register,right.location.register);
  142. // location.resflags:=F_EQ;
  143. // end;
  144. // unequaln:
  145. // begin
  146. // gencmp(left.location.register,right.location.register);
  147. // location.resflags:=F_NE;
  148. // end;
  149. // lten,
  150. // gten:
  151. // begin
  152. // if (not(nf_swapped in flags) and
  153. // (nodetype = lten)) or
  154. // ((nf_swapped in flags) and
  155. // (nodetype = gten)) then
  156. // swapleftright;
  157. // tmpreg:=cg.getintregister(current_asmdata.CurrAsmList,location.size);
  158. // cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,OP_AND,location.size,
  159. // left.location.register,right.location.register,tmpreg);
  160. // gencmp(tmpreg,right.location.register);
  161. // location.resflags:=F_EQ;
  162. // end;
  163. // else
  164. // internalerror(2004012401);
  165. //end;
  166. end;
  167. procedure TZ80AddNode.second_cmp;
  168. var
  169. unsigned : boolean;
  170. tmpreg1,tmpreg2 : tregister;
  171. i : longint;
  172. opdef: tdef;
  173. opsize: TCgSize;
  174. ai: taicpu;
  175. l: TAsmLabel;
  176. begin
  177. unsigned:=not(is_signed(left.resultdef)) or
  178. not(is_signed(right.resultdef));
  179. opdef:=left.resultdef;
  180. opsize:=def_cgsize(opdef);
  181. pass_left_right;
  182. if (opsize=OS_8) or ((opsize=OS_S8) and (NodeType in [equaln,unequaln])) then
  183. begin
  184. if getresflags(unsigned)=F_NotPossible then
  185. swapleftright;
  186. if left.location.loc<>LOC_REGISTER then
  187. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,false);
  188. if right.location.loc in [LOC_REFERENCE,LOC_CREFERENCE] then
  189. begin
  190. if (right.location.reference.base=NR_IX) and (right.location.reference.index=NR_NO) then
  191. begin
  192. cg.getcpuregister(current_asmdata.CurrAsmList,NR_A);
  193. cg.a_load_loc_reg(current_asmdata.CurrAsmList,def_cgsize(left.resultdef),left.location,NR_A);
  194. current_asmdata.CurrAsmList.Concat(taicpu.op_reg_ref(A_CP,NR_A,right.location.reference));
  195. cg.ungetcpuregister(current_asmdata.CurrAsmList,NR_A);
  196. end
  197. else
  198. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,right.resultdef,false);
  199. end;
  200. case right.location.loc of
  201. LOC_CONSTANT:
  202. begin
  203. cg.getcpuregister(current_asmdata.CurrAsmList,NR_A);
  204. cg.a_load_loc_reg(current_asmdata.CurrAsmList,def_cgsize(left.resultdef),left.location,NR_A);
  205. current_asmdata.CurrAsmList.Concat(taicpu.op_reg_const(A_CP,NR_A,right.location.value));
  206. cg.ungetcpuregister(current_asmdata.CurrAsmList,NR_A);
  207. end;
  208. LOC_REGISTER,LOC_CREGISTER:
  209. begin
  210. cg.getcpuregister(current_asmdata.CurrAsmList,NR_A);
  211. cg.a_load_loc_reg(current_asmdata.CurrAsmList,def_cgsize(left.resultdef),left.location,NR_A);
  212. current_asmdata.CurrAsmList.Concat(taicpu.op_reg_reg(A_CP,NR_A,right.location.register));
  213. cg.ungetcpuregister(current_asmdata.CurrAsmList,NR_A);
  214. end;
  215. LOC_REFERENCE,LOC_CREFERENCE:
  216. begin
  217. { Already handled before the case statement. Nothing to do here. }
  218. end;
  219. else
  220. internalerror(2020040402);
  221. end;
  222. location_reset(location,LOC_FLAGS,OS_NO);
  223. location.resflags:=getresflags(unsigned);
  224. end
  225. else if opsize=OS_S8 then
  226. begin
  227. if getresflags(unsigned)=F_NotPossible then
  228. swapleftright;
  229. if left.location.loc<>LOC_REGISTER then
  230. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,false);
  231. if right.location.loc in [LOC_REFERENCE,LOC_CREFERENCE] then
  232. begin
  233. if (right.location.reference.base=NR_IX) and (right.location.reference.index=NR_NO) then
  234. begin
  235. cg.getcpuregister(current_asmdata.CurrAsmList,NR_A);
  236. cg.a_load_loc_reg(current_asmdata.CurrAsmList,def_cgsize(left.resultdef),left.location,NR_A);
  237. current_asmdata.CurrAsmList.Concat(taicpu.op_reg_ref(A_SUB,NR_A,right.location.reference));
  238. end
  239. else
  240. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,right.resultdef,false);
  241. end;
  242. case right.location.loc of
  243. LOC_CONSTANT:
  244. begin
  245. cg.getcpuregister(current_asmdata.CurrAsmList,NR_A);
  246. cg.a_load_loc_reg(current_asmdata.CurrAsmList,def_cgsize(left.resultdef),left.location,NR_A);
  247. current_asmdata.CurrAsmList.Concat(taicpu.op_reg_const(A_SUB,NR_A,right.location.value));
  248. end;
  249. LOC_REGISTER,LOC_CREGISTER:
  250. begin
  251. cg.getcpuregister(current_asmdata.CurrAsmList,NR_A);
  252. cg.a_load_loc_reg(current_asmdata.CurrAsmList,def_cgsize(left.resultdef),left.location,NR_A);
  253. current_asmdata.CurrAsmList.Concat(taicpu.op_reg_reg(A_SUB,NR_A,right.location.register));
  254. end;
  255. LOC_REFERENCE,LOC_CREFERENCE:
  256. begin
  257. { Already handled before the case statement. Nothing to do here. }
  258. end;
  259. else
  260. internalerror(2020040402);
  261. end;
  262. current_asmdata.getjumplabel(l);
  263. ai:=taicpu.op_cond_sym(A_JP,C_PO,l);
  264. ai.is_jmp:=true;
  265. current_asmdata.CurrAsmList.concat(ai);
  266. current_asmdata.CurrAsmList.Concat(taicpu.op_reg_const(A_XOR,NR_A,$80));
  267. cg.a_label(current_asmdata.CurrAsmList,l);
  268. cg.ungetcpuregister(current_asmdata.CurrAsmList,NR_A);
  269. location_reset(location,LOC_FLAGS,OS_NO);
  270. location.resflags:=getresflags(unsigned);
  271. end
  272. else
  273. internalerror(2020040401);
  274. end;
  275. procedure TZ80AddNode.second_cmp64bit;
  276. begin
  277. second_cmp;
  278. end;
  279. function TZ80AddNode.pass_1 : tnode;
  280. begin
  281. result:=inherited pass_1;
  282. {$ifdef dummy}
  283. if not(assigned(result)) then
  284. begin
  285. unsigned:=not(is_signed(left.resultdef)) or
  286. not(is_signed(right.resultdef));
  287. if is_64bit(left.resultdef) and
  288. ((nodetype in [equaln,unequaln]) or
  289. (unsigned and (nodetype in [ltn,lten,gtn,gten]))
  290. ) then
  291. expectloc:=LOC_FLAGS;
  292. end;
  293. { handling boolean expressions }
  294. if not(assigned(result)) and
  295. (
  296. not(is_boolean(left.resultdef)) or
  297. not(is_boolean(right.resultdef)) or
  298. is_dynamic_array(left.resultdef)
  299. ) then
  300. expectloc:=LOC_FLAGS;
  301. {$endif dummy}
  302. end;
  303. procedure TZ80AddNode.second_cmpordinal;
  304. begin
  305. second_cmp;
  306. end;
  307. begin
  308. caddnode:=TZ80AddNode;
  309. end.