nllvmadd.pas 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. {
  2. Copyright (c) 2013 by Jonas Maebe
  3. Generate LLVM bytecode for add 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 nllvmadd;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. node,
  22. ncgadd;
  23. type
  24. tllvmaddnode = class(tcgaddnode)
  25. public
  26. function pass_1: tnode; override;
  27. procedure force_reg_left_right(allow_swap, allow_constant: boolean); override;
  28. protected
  29. procedure second_cmpsmallset; override;
  30. procedure second_cmpordinal; override;
  31. procedure second_add64bit; override;
  32. procedure second_cmp64bit; override;
  33. procedure second_addfloat; override;
  34. procedure second_cmpfloat; override;
  35. end;
  36. implementation
  37. uses
  38. verbose,globtype,
  39. aasmdata,
  40. symconst,symtype,symdef,defutil,
  41. llvmbase,aasmllvm,
  42. cgbase,cgutils,
  43. hlcgobj,
  44. nadd
  45. ;
  46. { tllvmaddnode }
  47. function tllvmaddnode.pass_1: tnode;
  48. begin
  49. result:=inherited pass_1;
  50. { there are no flags in LLVM }
  51. if expectloc=LOC_FLAGS then
  52. expectloc:=LOC_REGISTER;
  53. end;
  54. procedure tllvmaddnode.force_reg_left_right(allow_swap, allow_constant: boolean);
  55. begin
  56. { comparison with pointer -> no immediate, as icmp can't handle pointer
  57. immediates (except for nil as "null", but we don't generate that) }
  58. if (nodetype in [equaln,unequaln,gtn,gten,ltn,lten]) and
  59. ((left.nodetype in [pointerconstn,niln]) or
  60. (right.nodetype in [pointerconstn,niln])) then
  61. allow_constant:=false;
  62. inherited;
  63. { pointer - pointer = integer -> make all defs pointer since we can't
  64. subtract pointers }
  65. if (nodetype=subn) and
  66. (left.resultdef.typ=pointerdef) and
  67. (right.resultdef.typ=pointerdef) then
  68. begin
  69. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,resultdef,true);
  70. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,resultdef,true);
  71. end
  72. { pointer +/- integer -> make defs the same since a_op_* only gets a
  73. single type as argument }
  74. else if (nodetype in [addn,subn]) and
  75. ((left.resultdef.typ=pointerdef)<>(right.resultdef.typ=pointerdef)) then
  76. begin
  77. { the result is a pointerdef -> typecast both arguments to pointer;
  78. a_op_*_reg will convert them back to integer as needed }
  79. if left.resultdef.typ<>pointerdef then
  80. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,resultdef,true);
  81. if right.resultdef.typ<>pointerdef then
  82. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,resultdef,true);
  83. end;
  84. end;
  85. procedure tllvmaddnode.second_cmpsmallset;
  86. var
  87. tmpreg,
  88. tmpreg2: tregister;
  89. cmpop : topcmp;
  90. begin
  91. pass_left_right;
  92. location_reset(location,LOC_REGISTER,OS_8);
  93. location.register:=hlcg.getintregister(current_asmdata.CurrAsmList,pasbool8type);
  94. force_reg_left_right(false,false);
  95. case nodetype of
  96. equaln,
  97. unequaln:
  98. begin
  99. if nodetype=equaln then
  100. cmpop:=OC_EQ
  101. else
  102. cmpop:=OC_NE;
  103. current_asmdata.CurrAsmList.concat(taillvm.op_reg_cond_size_reg_reg(la_icmp,
  104. location.register,cmpop,left.resultdef,left.location.register,right.location.register));
  105. end;
  106. lten,
  107. gten:
  108. begin
  109. if (not(nf_swapped in flags) and
  110. (nodetype = lten)) or
  111. ((nf_swapped in flags) and
  112. (nodetype = gten)) then
  113. swapleftright;
  114. { set1<=set2 <-> set2 and not(set1) = 0 }
  115. tmpreg:=hlcg.getintregister(current_asmdata.CurrAsmList,left.resultdef);
  116. hlcg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_NOT,left.resultdef,left.location.register,tmpreg);
  117. tmpreg2:=hlcg.getintregister(current_asmdata.CurrAsmList,left.resultdef);
  118. hlcg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,OP_AND,left.resultdef,right.location.register,tmpreg,tmpreg2);
  119. current_asmdata.CurrAsmList.concat(taillvm.op_reg_cond_size_reg_const(la_icmp,
  120. location.register,OC_EQ,left.resultdef,tmpreg2,0));
  121. end;
  122. else
  123. internalerror(2012042701);
  124. end;
  125. end;
  126. procedure tllvmaddnode.second_cmpordinal;
  127. var
  128. cmpop: topcmp;
  129. unsigned : boolean;
  130. begin
  131. pass_left_right;
  132. force_reg_left_right(true,true);
  133. unsigned:=not(is_signed(left.resultdef)) or
  134. not(is_signed(right.resultdef));
  135. case nodetype of
  136. ltn:
  137. if unsigned then
  138. cmpop:=OC_B
  139. else
  140. cmpop:=OC_LT;
  141. lten:
  142. if unsigned then
  143. cmpop:=OC_BE
  144. else
  145. cmpop:=OC_LTE;
  146. gtn:
  147. if unsigned then
  148. cmpop:=OC_A
  149. else
  150. cmpop:=OC_GT;
  151. gten:
  152. if unsigned then
  153. cmpop:=OC_AE
  154. else
  155. cmpop:=OC_GTE;
  156. equaln:
  157. cmpop:=OC_EQ;
  158. unequaln:
  159. cmpop:=OC_NE;
  160. else
  161. internalerror(2015031505);
  162. end;
  163. if nf_swapped in flags then
  164. cmpop:=swap_opcmp(cmpop);
  165. location_reset(location,LOC_REGISTER,OS_8);
  166. location.register:=hlcg.getintregister(current_asmdata.CurrAsmList,resultdef);
  167. if right.location.loc=LOC_CONSTANT then
  168. current_asmdata.CurrAsmList.concat(taillvm.op_reg_cond_size_reg_const(la_icmp,
  169. location.register,cmpop,left.resultdef,left.location.register,right.location.value64))
  170. else
  171. current_asmdata.CurrAsmList.concat(taillvm.op_reg_cond_size_reg_reg(la_icmp,
  172. location.register,cmpop,left.resultdef,left.location.register,right.location.register));
  173. end;
  174. procedure tllvmaddnode.second_add64bit;
  175. begin
  176. second_addordinal;
  177. end;
  178. procedure tllvmaddnode.second_cmp64bit;
  179. begin
  180. second_cmpordinal;
  181. end;
  182. procedure tllvmaddnode.second_addfloat;
  183. var
  184. op : tllvmop;
  185. llvmfpcmp : tllvmfpcmp;
  186. size : tdef;
  187. cmpop,
  188. singleprec : boolean;
  189. begin
  190. pass_left_right;
  191. cmpop:=false;
  192. singleprec:=tfloatdef(left.resultdef).floattype=s32real;
  193. { avoid uninitialised warning }
  194. llvmfpcmp:=lfc_invalid;
  195. case nodetype of
  196. addn :
  197. op:=la_fadd;
  198. muln :
  199. op:=la_fmul;
  200. subn :
  201. op:=la_fsub;
  202. slashn :
  203. op:=la_fdiv;
  204. ltn,lten,gtn,gten,
  205. equaln,unequaln :
  206. begin
  207. op:=la_fcmp;
  208. cmpop:=true;
  209. case nodetype of
  210. ltn:
  211. llvmfpcmp:=lfc_olt;
  212. lten:
  213. llvmfpcmp:=lfc_ole;
  214. gtn:
  215. llvmfpcmp:=lfc_ogt;
  216. gten:
  217. llvmfpcmp:=lfc_oge;
  218. equaln:
  219. llvmfpcmp:=lfc_oeq;
  220. unequaln:
  221. llvmfpcmp:=lfc_one;
  222. else
  223. internalerror(2015031506);
  224. end;
  225. end;
  226. else
  227. internalerror(2013102401);
  228. end;
  229. { get the operands in the correct order; there are no special cases here,
  230. everything is register-based }
  231. if nf_swapped in flags then
  232. swapleftright;
  233. { put both operands in a register }
  234. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,right.location,right.resultdef,true);
  235. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,left.location,left.resultdef,true);
  236. { initialize the result location }
  237. if not cmpop then
  238. begin
  239. location_reset(location,LOC_FPUREGISTER,def_cgsize(resultdef));
  240. location.register:=hlcg.getfpuregister(current_asmdata.CurrAsmList,resultdef);
  241. end
  242. else
  243. begin
  244. location_reset(location,LOC_REGISTER,OS_8);
  245. location.register:=hlcg.getintregister(current_asmdata.CurrAsmList,resultdef);
  246. end;
  247. { see comment in thlcgllvm.a_loadfpu_ref_reg }
  248. if tfloatdef(left.resultdef).floattype in [s64comp,s64currency] then
  249. size:=sc80floattype
  250. else
  251. size:=left.resultdef;
  252. { emit the actual operation }
  253. if not cmpop then
  254. begin
  255. current_asmdata.CurrAsmList.concat(taillvm.op_reg_size_reg_reg(op,location.register,size,
  256. left.location.register,right.location.register))
  257. end
  258. else
  259. begin
  260. current_asmdata.CurrAsmList.concat(taillvm.op_reg_fpcond_size_reg_reg(op,
  261. location.register,llvmfpcmp,size,left.location.register,right.location.register))
  262. end;
  263. end;
  264. procedure tllvmaddnode.second_cmpfloat;
  265. begin
  266. second_addfloat;
  267. end;
  268. begin
  269. caddnode:=tllvmaddnode;
  270. end.