nllvmadd.pas 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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,llvmbool1type);
  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. tmpreg:=hlcg.getintregister(current_asmdata.CurrAsmList,resultdef);
  126. hlcg.a_load_reg_reg(current_asmdata.CurrAsmList,llvmbool1type,resultdef,location.register,tmpreg);
  127. location.register:=tmpreg;
  128. end;
  129. procedure tllvmaddnode.second_cmpordinal;
  130. var
  131. tmpreg: tregister;
  132. cmpop: topcmp;
  133. unsigned : boolean;
  134. begin
  135. pass_left_right;
  136. force_reg_left_right(true,true);
  137. unsigned:=not(is_signed(left.resultdef)) or
  138. not(is_signed(right.resultdef));
  139. case nodetype of
  140. ltn:
  141. if unsigned then
  142. cmpop:=OC_B
  143. else
  144. cmpop:=OC_LT;
  145. lten:
  146. if unsigned then
  147. cmpop:=OC_BE
  148. else
  149. cmpop:=OC_LTE;
  150. gtn:
  151. if unsigned then
  152. cmpop:=OC_A
  153. else
  154. cmpop:=OC_GT;
  155. gten:
  156. if unsigned then
  157. cmpop:=OC_AE
  158. else
  159. cmpop:=OC_GTE;
  160. equaln:
  161. cmpop:=OC_EQ;
  162. unequaln:
  163. cmpop:=OC_NE;
  164. else
  165. internalerror(2015031505);
  166. end;
  167. if nf_swapped in flags then
  168. cmpop:=swap_opcmp(cmpop);
  169. location_reset(location,LOC_REGISTER,OS_8);
  170. location.register:=hlcg.getintregister(current_asmdata.CurrAsmList,llvmbool1type);
  171. if right.location.loc=LOC_CONSTANT then
  172. current_asmdata.CurrAsmList.concat(taillvm.op_reg_cond_size_reg_const(la_icmp,
  173. location.register,cmpop,left.resultdef,left.location.register,right.location.value64))
  174. else
  175. current_asmdata.CurrAsmList.concat(taillvm.op_reg_cond_size_reg_reg(la_icmp,
  176. location.register,cmpop,left.resultdef,left.location.register,right.location.register));
  177. tmpreg:=hlcg.getintregister(current_asmdata.CurrAsmList,resultdef);
  178. hlcg.a_load_reg_reg(current_asmdata.CurrAsmList,llvmbool1type,resultdef,location.register,tmpreg);
  179. location.register:=tmpreg;
  180. end;
  181. procedure tllvmaddnode.second_add64bit;
  182. begin
  183. second_addordinal;
  184. end;
  185. procedure tllvmaddnode.second_cmp64bit;
  186. begin
  187. second_cmpordinal;
  188. end;
  189. procedure tllvmaddnode.second_addfloat;
  190. var
  191. tmpreg: tregister;
  192. op : tllvmop;
  193. llvmfpcmp : tllvmfpcmp;
  194. size : tdef;
  195. cmpop,
  196. singleprec : boolean;
  197. begin
  198. pass_left_right;
  199. cmpop:=false;
  200. singleprec:=tfloatdef(left.resultdef).floattype=s32real;
  201. { avoid uninitialised warning }
  202. llvmfpcmp:=lfc_invalid;
  203. case nodetype of
  204. addn :
  205. op:=la_fadd;
  206. muln :
  207. op:=la_fmul;
  208. subn :
  209. op:=la_fsub;
  210. slashn :
  211. op:=la_fdiv;
  212. ltn,lten,gtn,gten,
  213. equaln,unequaln :
  214. begin
  215. op:=la_fcmp;
  216. cmpop:=true;
  217. case nodetype of
  218. ltn:
  219. llvmfpcmp:=lfc_olt;
  220. lten:
  221. llvmfpcmp:=lfc_ole;
  222. gtn:
  223. llvmfpcmp:=lfc_ogt;
  224. gten:
  225. llvmfpcmp:=lfc_oge;
  226. equaln:
  227. llvmfpcmp:=lfc_oeq;
  228. unequaln:
  229. llvmfpcmp:=lfc_one;
  230. else
  231. internalerror(2015031506);
  232. end;
  233. end;
  234. else
  235. internalerror(2013102401);
  236. end;
  237. { get the operands in the correct order; there are no special cases here,
  238. everything is register-based }
  239. if nf_swapped in flags then
  240. swapleftright;
  241. { put both operands in a register }
  242. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,right.location,right.resultdef,true);
  243. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,left.location,left.resultdef,true);
  244. { initialize the result location }
  245. if not cmpop then
  246. begin
  247. location_reset(location,LOC_FPUREGISTER,def_cgsize(resultdef));
  248. location.register:=hlcg.getfpuregister(current_asmdata.CurrAsmList,resultdef);
  249. end
  250. else
  251. begin
  252. location_reset(location,LOC_REGISTER,OS_8);
  253. location.register:=hlcg.getintregister(current_asmdata.CurrAsmList,llvmbool1type);
  254. end;
  255. { see comment in thlcgllvm.a_loadfpu_ref_reg }
  256. if tfloatdef(left.resultdef).floattype in [s64comp,s64currency] then
  257. size:=sc80floattype
  258. else
  259. size:=left.resultdef;
  260. { emit the actual operation }
  261. if not cmpop then
  262. begin
  263. current_asmdata.CurrAsmList.concat(taillvm.op_reg_size_reg_reg(op,location.register,size,
  264. left.location.register,right.location.register))
  265. end
  266. else
  267. begin
  268. current_asmdata.CurrAsmList.concat(taillvm.op_reg_fpcond_size_reg_reg(op,
  269. location.register,llvmfpcmp,size,left.location.register,right.location.register));
  270. tmpreg:=hlcg.getintregister(current_asmdata.CurrAsmList,resultdef);
  271. hlcg.a_load_reg_reg(current_asmdata.CurrAsmList,llvmbool1type,resultdef,location.register,tmpreg);
  272. location.register:=tmpreg;
  273. end;
  274. end;
  275. procedure tllvmaddnode.second_cmpfloat;
  276. begin
  277. second_addfloat;
  278. end;
  279. begin
  280. caddnode:=tllvmaddnode;
  281. end.