nllvmadd.pas 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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 (left.resultdef.typ=pointerdef)<>(right.resultdef.typ=pointerdef) then
  75. begin
  76. { the result is a pointerdef -> typecast both arguments to pointer;
  77. a_op_*_reg will convert them back to integer as needed }
  78. if left.resultdef.typ<>pointerdef then
  79. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,resultdef,true);
  80. if right.resultdef.typ<>pointerdef then
  81. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,resultdef,true);
  82. end;
  83. end;
  84. procedure tllvmaddnode.second_cmpsmallset;
  85. var
  86. tmpreg,
  87. tmpreg2: tregister;
  88. cmpop : topcmp;
  89. begin
  90. pass_left_right;
  91. location_reset(location,LOC_REGISTER,OS_8);
  92. location.register:=hlcg.getintregister(current_asmdata.CurrAsmList,pasbool8type);
  93. force_reg_left_right(false,false);
  94. case nodetype of
  95. equaln,
  96. unequaln:
  97. begin
  98. if nodetype=equaln then
  99. cmpop:=OC_EQ
  100. else
  101. cmpop:=OC_NE;
  102. current_asmdata.CurrAsmList.concat(taillvm.op_reg_cond_size_reg_reg(la_icmp,
  103. location.register,cmpop,left.resultdef,left.location.register,right.location.register));
  104. end;
  105. lten,
  106. gten:
  107. begin
  108. if (not(nf_swapped in flags) and
  109. (nodetype = lten)) or
  110. ((nf_swapped in flags) and
  111. (nodetype = gten)) then
  112. swapleftright;
  113. { set1<=set2 <-> set2 and not(set1) = 0 }
  114. tmpreg:=hlcg.getintregister(current_asmdata.CurrAsmList,left.resultdef);
  115. hlcg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_NOT,left.resultdef,left.location.register,tmpreg);
  116. tmpreg2:=hlcg.getintregister(current_asmdata.CurrAsmList,left.resultdef);
  117. hlcg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,OP_AND,left.resultdef,right.location.register,tmpreg,tmpreg2);
  118. current_asmdata.CurrAsmList.concat(taillvm.op_reg_cond_size_reg_const(la_icmp,
  119. location.register,OC_EQ,left.resultdef,tmpreg2,0));
  120. end;
  121. else
  122. internalerror(2012042701);
  123. end;
  124. end;
  125. procedure tllvmaddnode.second_cmpordinal;
  126. var
  127. cmpop: topcmp;
  128. unsigned : boolean;
  129. begin
  130. pass_left_right;
  131. force_reg_left_right(true,true);
  132. unsigned:=not(is_signed(left.resultdef)) or
  133. not(is_signed(right.resultdef));
  134. case nodetype of
  135. ltn:
  136. if unsigned then
  137. cmpop:=OC_B
  138. else
  139. cmpop:=OC_LT;
  140. lten:
  141. if unsigned then
  142. cmpop:=OC_BE
  143. else
  144. cmpop:=OC_LTE;
  145. gtn:
  146. if unsigned then
  147. cmpop:=OC_A
  148. else
  149. cmpop:=OC_GT;
  150. gten:
  151. if unsigned then
  152. cmpop:=OC_AE
  153. else
  154. cmpop:=OC_GTE;
  155. equaln:
  156. cmpop:=OC_EQ;
  157. unequaln:
  158. cmpop:=OC_NE;
  159. else
  160. internalerror(2015031505);
  161. end;
  162. if nf_swapped in flags then
  163. cmpop:=swap_opcmp(cmpop);
  164. location_reset(location,LOC_REGISTER,OS_8);
  165. location.register:=hlcg.getintregister(current_asmdata.CurrAsmList,resultdef);
  166. if right.location.loc=LOC_CONSTANT then
  167. current_asmdata.CurrAsmList.concat(taillvm.op_reg_cond_size_reg_const(la_icmp,
  168. location.register,cmpop,left.resultdef,left.location.register,right.location.value64))
  169. else
  170. current_asmdata.CurrAsmList.concat(taillvm.op_reg_cond_size_reg_reg(la_icmp,
  171. location.register,cmpop,left.resultdef,left.location.register,right.location.register));
  172. end;
  173. procedure tllvmaddnode.second_add64bit;
  174. begin
  175. second_addordinal;
  176. end;
  177. procedure tllvmaddnode.second_cmp64bit;
  178. begin
  179. second_cmpordinal;
  180. end;
  181. procedure tllvmaddnode.second_addfloat;
  182. var
  183. op : tllvmop;
  184. llvmfpcmp : tllvmfpcmp;
  185. size : tdef;
  186. cmpop,
  187. singleprec : boolean;
  188. begin
  189. pass_left_right;
  190. cmpop:=false;
  191. singleprec:=tfloatdef(left.resultdef).floattype=s32real;
  192. { avoid uninitialised warning }
  193. llvmfpcmp:=lfc_invalid;
  194. case nodetype of
  195. addn :
  196. op:=la_fadd;
  197. muln :
  198. op:=la_fmul;
  199. subn :
  200. op:=la_fsub;
  201. slashn :
  202. op:=la_fdiv;
  203. ltn,lten,gtn,gten,
  204. equaln,unequaln :
  205. begin
  206. op:=la_fcmp;
  207. cmpop:=true;
  208. case nodetype of
  209. ltn:
  210. llvmfpcmp:=lfc_olt;
  211. lten:
  212. llvmfpcmp:=lfc_ole;
  213. gtn:
  214. llvmfpcmp:=lfc_ogt;
  215. gten:
  216. llvmfpcmp:=lfc_oge;
  217. equaln:
  218. llvmfpcmp:=lfc_oeq;
  219. unequaln:
  220. llvmfpcmp:=lfc_one;
  221. else
  222. internalerror(2015031506);
  223. end;
  224. end;
  225. else
  226. internalerror(2013102401);
  227. end;
  228. { get the operands in the correct order; there are no special cases here,
  229. everything is register-based }
  230. if nf_swapped in flags then
  231. swapleftright;
  232. { put both operands in a register }
  233. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,right.location,right.resultdef,true);
  234. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,left.location,left.resultdef,true);
  235. { initialize the result location }
  236. if not cmpop then
  237. begin
  238. location_reset(location,LOC_FPUREGISTER,def_cgsize(resultdef));
  239. location.register:=hlcg.getfpuregister(current_asmdata.CurrAsmList,resultdef);
  240. end
  241. else
  242. begin
  243. location_reset(location,LOC_REGISTER,OS_8);
  244. location.register:=hlcg.getintregister(current_asmdata.CurrAsmList,resultdef);
  245. end;
  246. { see comment in thlcgllvm.a_loadfpu_ref_reg }
  247. if tfloatdef(left.resultdef).floattype in [s64comp,s64currency] then
  248. size:=sc80floattype
  249. else
  250. size:=left.resultdef;
  251. { emit the actual operation }
  252. if not cmpop then
  253. begin
  254. current_asmdata.CurrAsmList.concat(taillvm.op_reg_size_reg_reg(op,location.register,size,
  255. left.location.register,right.location.register))
  256. end
  257. else
  258. begin
  259. current_asmdata.CurrAsmList.concat(taillvm.op_reg_fpcond_size_reg_reg(op,
  260. location.register,llvmfpcmp,size,left.location.register,right.location.register))
  261. end;
  262. end;
  263. procedure tllvmaddnode.second_cmpfloat;
  264. begin
  265. second_addfloat;
  266. end;
  267. begin
  268. caddnode:=tllvmaddnode;
  269. end.