ncpumat.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Generate Xtensa assembler 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 ncpumat;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. node,nmat,ncgmat;
  22. type
  23. tcpumoddivnode = class(tmoddivnode)
  24. procedure pass_generate_code;override;
  25. end;
  26. tcpunotnode = class(tcgnotnode)
  27. procedure second_boolean;override;
  28. end;
  29. tcpuunaryminusnode = class(tcgunaryminusnode)
  30. function pass_1: tnode; override;
  31. procedure second_float;override;
  32. end;
  33. tcpushlshrnode = class(tcgshlshrnode)
  34. procedure second_64bit;override;
  35. end;
  36. implementation
  37. uses
  38. globtype,compinnr,
  39. cutils,verbose,globals,constexp,
  40. aasmbase,aasmcpu,aasmtai,aasmdata,
  41. defutil,
  42. symtype,symconst,symtable,
  43. cgbase,cgobj,hlcgobj,cgutils,
  44. pass_2,procinfo,
  45. ncon,ncnv,ncal,ninl,
  46. cpubase,cpuinfo,
  47. ncgutil,
  48. nadd,pass_1,symdef;
  49. {*****************************************************************************
  50. TCPUMODDIVNODE
  51. *****************************************************************************}
  52. procedure tcpumoddivnode.pass_generate_code;
  53. begin
  54. location.loc:=LOC_REGISTER;
  55. end;
  56. {*****************************************************************************
  57. TCPUNOTNODE
  58. *****************************************************************************}
  59. procedure tcpunotnode.second_boolean;
  60. var
  61. tmpreg : TRegister;
  62. begin
  63. secondpass(left);
  64. location:=left.location;
  65. hlcg.location_force_reg(current_asmdata.CurrAsmList,location,resultdef,resultdef,false);
  66. { not supported yet }
  67. if is_64bit(resultdef) then
  68. Internalerror(2020031701);
  69. if is_cbool(resultdef) then
  70. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_NOT,def_cgsize(resultdef), location.register, location.register)
  71. else
  72. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_XOR,def_cgsize(resultdef),1, location.register, location.register)
  73. end;
  74. {*****************************************************************************
  75. TARMUNARYMINUSNODE
  76. *****************************************************************************}
  77. function tcpuunaryminusnode.pass_1: tnode;
  78. var
  79. procname: string[31];
  80. fdef : tdef;
  81. begin
  82. Result:=nil;
  83. if (current_settings.fputype=fpu_soft) and
  84. (left.resultdef.typ=floatdef) then
  85. begin
  86. result:=nil;
  87. firstpass(left);
  88. expectloc:=LOC_REGISTER;
  89. exit;
  90. end;
  91. result:=nil;
  92. firstpass(left);
  93. if codegenerror then
  94. exit;
  95. expectloc:=LOC_REGISTER;
  96. end;
  97. procedure tcpuunaryminusnode.second_float;
  98. begin
  99. secondpass(left);
  100. case current_settings.fputype of
  101. fpu_soft:
  102. begin
  103. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,false);
  104. location:=left.location;
  105. case location.size of
  106. OS_32:
  107. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_XOR,OS_32,tcgint($80000000),location.register);
  108. OS_64:
  109. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_XOR,OS_32,tcgint($80000000),location.registerhi);
  110. else
  111. internalerror(2014033101);
  112. end;
  113. end
  114. else
  115. internalerror(2009112602);
  116. end;
  117. end;
  118. procedure tcpushlshrnode.second_64bit;
  119. var
  120. v : TConstExprInt;
  121. lreg, resreg: TRegister64;
  122. procedure emit_instr(p: tai);
  123. begin
  124. current_asmdata.CurrAsmList.concat(p);
  125. end;
  126. {This code is build like it gets called with sm=SM_LSR all the time, for SM_LSL dst* and src* have to be reversed
  127. This will generate
  128. mov shiftval1, shiftval
  129. cmp shiftval1, #64
  130. movcs shiftval1, #64
  131. rsb shiftval2, shiftval1, #32
  132. mov dstlo, srclo, lsr shiftval1
  133. mov dsthi, srchi, lsr shiftval1
  134. orr dstlo, srchi, lsl shiftval2
  135. subs shiftval2, shiftval1, #32
  136. movpl dstlo, srchi, lsr shiftval2
  137. }
  138. procedure shift_by_variable(srchi, srclo, dsthi, dstlo, shiftval: TRegister);
  139. var
  140. shiftval1,shiftval2:TRegister;
  141. begin
  142. //shifterop_reset(so);
  143. //shiftval1:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  144. //shiftval2:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  145. //
  146. //cg.a_load_reg_reg(current_asmdata.CurrAsmList, OS_INT, OS_INT, shiftval, shiftval1);
  147. //
  148. //{The ARM barrel shifter only considers the lower 8 bits of a register for the shift}
  149. //cg.a_reg_alloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  150. //emit_instr(taicpu.op_reg_const(A_CMP, shiftval1, 64));
  151. //emit_instr(setcondition(taicpu.op_reg_const(A_MOV, shiftval1, 64), C_CS));
  152. //cg.a_reg_dealloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  153. //
  154. //{Calculate how much the upper register needs to be shifted left}
  155. //emit_instr(taicpu.op_reg_reg_const(A_RSB, shiftval2, shiftval1, 32));
  156. //
  157. //so.shiftmode:=sm;
  158. //so.rs:=shiftval1;
  159. //
  160. //{Shift and zerofill the hi+lo register}
  161. //emit_instr(taicpu.op_reg_reg_shifterop(A_MOV, dstlo, srclo, so));
  162. //emit_instr(taicpu.op_reg_reg_shifterop(A_MOV, dsthi, srchi, so));
  163. //
  164. //{Fold in the lower 32-shiftval bits}
  165. //if sm = SM_LSR then so.shiftmode:=SM_LSL else so.shiftmode:=SM_LSR;
  166. //so.rs:=shiftval2;
  167. //emit_instr(taicpu.op_reg_reg_reg_shifterop(A_ORR, dstlo, dstlo, srchi, so));
  168. //
  169. //cg.a_reg_alloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  170. //emit_instr(setoppostfix(taicpu.op_reg_reg_const(A_SUB, shiftval2, shiftval1, 32), PF_S));
  171. //
  172. //so.shiftmode:=sm;
  173. //emit_instr(setcondition(taicpu.op_reg_reg_shifterop(A_MOV, dstlo, srchi, so), C_PL));
  174. //cg.a_reg_dealloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  175. end;
  176. begin
  177. inherited;
  178. //if GenerateThumbCode or GenerateThumb2Code then
  179. //begin
  180. // inherited;
  181. // exit;
  182. //end;
  183. //
  184. //location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  185. //location.register64.reghi:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  186. //location.register64.reglo:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  187. //
  188. //{ load left operator in a register }
  189. //if not(left.location.loc in [LOC_CREGISTER,LOC_REGISTER]) or
  190. // (left.location.size<>OS_64) then
  191. // hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,resultdef,true);
  192. //
  193. //lreg := left.location.register64;
  194. //resreg := location.register64;
  195. //shifterop_reset(so);
  196. //
  197. //{ shifting by a constant directly coded: }
  198. //if (right.nodetype=ordconstn) then
  199. // begin
  200. // v:=Tordconstnode(right).value and 63;
  201. // {Single bit shift}
  202. // if v = 1 then
  203. // if nodetype=shln then
  204. // begin
  205. // {Shift left by one by 2 simple 32bit additions}
  206. // cg.a_reg_alloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  207. // emit_instr(setoppostfix(taicpu.op_reg_reg_reg(A_ADD, resreg.reglo, lreg.reglo, lreg.reglo), PF_S));
  208. // emit_instr(taicpu.op_reg_reg_reg(A_ADC, resreg.reghi, lreg.reghi, lreg.reghi));
  209. // cg.a_reg_dealloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  210. // end
  211. // else
  212. // begin
  213. // {Shift right by first shifting hi by one and then using RRX (rotate right extended), which rotates through the carry}
  214. // shifterop_reset(so); so.shiftmode:=SM_LSR; so.shiftimm:=1;
  215. // cg.a_reg_alloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  216. // emit_instr(setoppostfix(taicpu.op_reg_reg_shifterop(A_MOV, resreg.reghi, lreg.reghi, so), PF_S));
  217. // so.shiftmode:=SM_RRX; so.shiftimm:=0; {RRX does NOT have a shift amount}
  218. // emit_instr(taicpu.op_reg_reg_shifterop(A_MOV, resreg.reglo, lreg.reglo, so));
  219. // cg.a_reg_dealloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  220. // end
  221. // {Clear one register and use the cg to generate a normal 32-bit shift}
  222. // else if v >= 32 then
  223. // if nodetype=shln then
  224. // begin
  225. // emit_instr(taicpu.op_reg_const(A_MOV, resreg.reglo, 0));
  226. // cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SHL,OS_32,v.uvalue-32,lreg.reglo,resreg.reghi);
  227. // end
  228. // else
  229. // begin
  230. // emit_instr(taicpu.op_reg_const(A_MOV, resreg.reghi, 0));
  231. // cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SHR,OS_32,v.uvalue-32,lreg.reghi,resreg.reglo);
  232. // end
  233. // {Shift LESS than 32, thats the tricky one}
  234. // else if (v < 32) and (v > 1) then
  235. // if nodetype=shln then
  236. // shift_less_than_32(lreg.reglo, lreg.reghi, resreg.reglo, resreg.reghi, v.uvalue, SM_LSL)
  237. // else
  238. // shift_less_than_32(lreg.reghi, lreg.reglo, resreg.reghi, resreg.reglo, v.uvalue, SM_LSR);
  239. // end
  240. //else
  241. // begin
  242. // { force right operator into a register }
  243. // if not(right.location.loc in [LOC_CREGISTER,LOC_REGISTER]) or
  244. // (right.location.size<>OS_32) then
  245. // hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,u32inttype,true);
  246. //
  247. // if nodetype = shln then
  248. // shift_by_variable(lreg.reglo, lreg.reghi, resreg.reglo, resreg.reghi, right.location.register, SM_LSL)
  249. // else
  250. // shift_by_variable(lreg.reghi, lreg.reglo, resreg.reghi, resreg.reglo, right.location.register, SM_LSR);
  251. // end;
  252. end;
  253. begin
  254. cmoddivnode:=tcpumoddivnode;
  255. cnotnode:=tcpunotnode;
  256. cunaryminusnode:=tcpuunaryminusnode;
  257. cshlshrnode:=tcpushlshrnode;
  258. end.