nllvmmat.pas 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. {
  2. Copyright (c) 2014 Jonas Maebe
  3. Generate LLVM IR for math 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 nllvmmat;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. symtype,
  22. node, nmat, ncgmat, ncghlmat, cgbase;
  23. type
  24. tllvmmoddivnode = class(tcgmoddivnode)
  25. procedure pass_generate_code; override;
  26. end;
  27. Tllvmunaryminusnode = class(tcgunaryminusnode)
  28. procedure emit_float_sign_change(r: tregister; _size : tdef);override;
  29. end;
  30. tllvmnotnode = class(tcghlnotnode)
  31. end;
  32. implementation
  33. uses
  34. globtype, systems, constexp,
  35. cutils, verbose, globals,
  36. symconst, symdef,
  37. aasmbase, aasmllvm, aasmtai, aasmdata,
  38. defutil,
  39. procinfo,
  40. hlcgobj, pass_2,
  41. ncon,
  42. llvmbase,
  43. ncgutil, cgutils;
  44. {*****************************************************************************
  45. tllvmmoddivnode
  46. *****************************************************************************}
  47. procedure tllvmmoddivnode.pass_generate_code;
  48. var
  49. op: tllvmop;
  50. hl: tasmlabel;
  51. tmpovreg1,
  52. tmpovreg2: tregister;
  53. ovloc: tlocation;
  54. begin
  55. secondpass(left);
  56. secondpass(right);
  57. if is_signed(left.resultdef) then
  58. if nodetype=divn then
  59. op:=la_sdiv
  60. else
  61. op:=la_srem
  62. else if nodetype=divn then
  63. op:=la_udiv
  64. else
  65. op:=la_urem;
  66. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,resultdef,true);
  67. if right.location.loc<>LOC_CONSTANT then
  68. begin
  69. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,resultdef,true);
  70. { in llvm, div-by-zero is undefined on all platforms -> need explicit
  71. check }
  72. current_asmdata.getjumplabel(hl);
  73. hlcg.a_cmp_const_loc_label(current_asmdata.CurrAsmList,resultdef,OC_NE,0,right.location,hl);
  74. hlcg.g_call_system_proc(current_asmdata.CurrAsmList,'fpc_divbyzero',[],nil).resetiftemp;
  75. hlcg.a_label(current_asmdata.CurrAsmList,hl);
  76. end;
  77. if (cs_check_overflow in current_settings.localswitches) and
  78. is_signed(left.resultdef) and
  79. ((right.nodetype<>ordconstn) or
  80. (tordconstnode(right).value=-1)) then
  81. begin
  82. current_asmdata.getjumplabel(hl);
  83. location_reset(ovloc,LOC_REGISTER,OS_8);
  84. ovloc.register:=hlcg.getintregister(current_asmdata.CurrAsmList,llvmbool1type);
  85. if right.nodetype=ordconstn then
  86. current_asmdata.CurrAsmList.concat(taillvm.op_reg_cond_size_reg_const(la_icmp,ovloc.register,OC_EQ,resultdef,left.location.register,low(int64)))
  87. else
  88. begin
  89. tmpovreg1:=hlcg.getintregister(current_asmdata.CurrAsmList,llvmbool1type);
  90. tmpovreg2:=hlcg.getintregister(current_asmdata.CurrAsmList,llvmbool1type);
  91. current_asmdata.CurrAsmList.concat(taillvm.op_reg_cond_size_reg_const(la_icmp,tmpovreg1,OC_EQ,resultdef,left.location.register,low(int64)));
  92. current_asmdata.CurrAsmList.concat(taillvm.op_reg_cond_size_reg_const(la_icmp,tmpovreg2,OC_EQ,resultdef,right.location.register,-1));
  93. hlcg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,OP_AND,llvmbool1type,tmpovreg1,tmpovreg2,ovloc.register);
  94. end;
  95. hlcg.g_overflowCheck_loc(current_asmdata.CurrAsmList,location,resultdef,ovloc);
  96. end;
  97. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  98. location.register:=hlcg.getintregister(current_asmdata.CurrAsmList,resultdef);
  99. if right.location.loc=LOC_CONSTANT then
  100. current_asmdata.CurrAsmList.concat(taillvm.op_reg_size_reg_const(op,location.register,resultdef,left.location.register,right.location.value))
  101. else
  102. current_asmdata.CurrAsmList.concat(taillvm.op_reg_size_reg_reg(op,location.register,resultdef,left.location.register,right.location.register))
  103. end;
  104. {*****************************************************************************
  105. Tllvmunaryminusnode
  106. *****************************************************************************}
  107. procedure Tllvmunaryminusnode.emit_float_sign_change(r: tregister; _size : tdef);
  108. var
  109. minusonereg: tregister;
  110. begin
  111. { multiply with -1 instead of subtracting from 0, because otherwise we -x
  112. won't turn into -0.0 if x was 0.0 (0.0 - 0.0 = 0.0, but -1.0 * 0.0 = -0.0 }
  113. if _size.typ<>floatdef then
  114. internalerror(2014012212);
  115. minusonereg:=hlcg.getfpuregister(current_asmdata.CurrAsmList,_size);
  116. case tfloatdef(_size).floattype of
  117. s32real,s64real:
  118. current_asmdata.CurrAsmList.concat(taillvm.op_reg_size_fpconst_size(la_bitcast,minusonereg,_size,-1.0,_size));
  119. { comp and currency are handled as int64 at the llvm level }
  120. s64comp,
  121. s64currency:
  122. begin
  123. { sc80floattype instead of _size, see comment in thlcgllvm.a_loadfpu_ref_reg }
  124. _size:=sc80floattype;
  125. current_asmdata.CurrAsmList.concat(taillvm.op_reg_size_const_size(la_sitofp,minusonereg,s64inttype,-1,_size));
  126. end;
  127. {$ifdef cpuextended}
  128. s80real,sc80real:
  129. current_asmdata.CurrAsmList.concat(taillvm.op_reg_size_fpconst80_size(la_bitcast,minusonereg,_size,-1.0,_size));
  130. {$endif cpuextended}
  131. else
  132. internalerror(2016112701);
  133. end;
  134. current_asmdata.CurrAsmList.Concat(taillvm.op_reg_size_reg_reg(la_fmul,r,_size,minusonereg,r));
  135. end;
  136. begin
  137. cmoddivnode := tllvmmoddivnode;
  138. (*
  139. cshlshrnode := tllvmshlshrnode;
  140. *)
  141. cnotnode := tllvmnotnode;
  142. cunaryminusnode := Tllvmunaryminusnode;
  143. end.