ncpumat.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. if (current_settings.fputype=fpu_soft) or (tfloatdef(left.resultdef).floattype<>s32real) or
  101. not(FPUXTENSA_SINGLE in fpu_capabilities[current_settings.fputype]) then
  102. begin
  103. if not(left.location.loc in [LOC_CREGISTER,LOC_REGISTER]) then
  104. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,false);
  105. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  106. if location.size in [OS_64,OS_S64,OS_F64] then
  107. begin
  108. location.register64.reglo:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  109. location.register64.reghi:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  110. end
  111. else
  112. location.register:=cg.getintregister(current_asmdata.CurrAsmList,location.size);
  113. case location.size of
  114. OS_32:
  115. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_XOR,OS_32,tcgint($80000000),left.location.register,location.register);
  116. OS_64:
  117. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_XOR,OS_32,tcgint($80000000),left.location.registerhi,location.registerhi);
  118. else
  119. internalerror(2014033101);
  120. end;
  121. end
  122. else
  123. begin
  124. if not(left.location.loc in [LOC_CFPUREGISTER,LOC_FPUREGISTER]) then
  125. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,left.location,left.resultdef,false);
  126. location_reset(location,LOC_FPUREGISTER,def_cgsize(resultdef));
  127. location.register:=cg.getfpuregister(current_asmdata.CurrAsmList,location.size);
  128. current_asmdata.CurrAsmList.Concat(taicpu.op_reg_reg(A_NEG_S,location.register,left.location.register));
  129. end;
  130. end;
  131. procedure tcpushlshrnode.second_64bit;
  132. var
  133. v : TConstExprInt;
  134. lreg, resreg: TRegister64;
  135. procedure emit_instr(p: tai);
  136. begin
  137. current_asmdata.CurrAsmList.concat(p);
  138. end;
  139. {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
  140. This will generate
  141. mov shiftval1, shiftval
  142. cmp shiftval1, #64
  143. movcs shiftval1, #64
  144. rsb shiftval2, shiftval1, #32
  145. mov dstlo, srclo, lsr shiftval1
  146. mov dsthi, srchi, lsr shiftval1
  147. orr dstlo, srchi, lsl shiftval2
  148. subs shiftval2, shiftval1, #32
  149. movpl dstlo, srchi, lsr shiftval2
  150. }
  151. procedure shift_by_variable(srchi, srclo, dsthi, dstlo, shiftval: TRegister);
  152. var
  153. shiftval1,shiftval2:TRegister;
  154. begin
  155. //shifterop_reset(so);
  156. //shiftval1:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  157. //shiftval2:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  158. //
  159. //cg.a_load_reg_reg(current_asmdata.CurrAsmList, OS_INT, OS_INT, shiftval, shiftval1);
  160. //
  161. //{The ARM barrel shifter only considers the lower 8 bits of a register for the shift}
  162. //cg.a_reg_alloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  163. //emit_instr(taicpu.op_reg_const(A_CMP, shiftval1, 64));
  164. //emit_instr(setcondition(taicpu.op_reg_const(A_MOV, shiftval1, 64), C_CS));
  165. //cg.a_reg_dealloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  166. //
  167. //{Calculate how much the upper register needs to be shifted left}
  168. //emit_instr(taicpu.op_reg_reg_const(A_RSB, shiftval2, shiftval1, 32));
  169. //
  170. //so.shiftmode:=sm;
  171. //so.rs:=shiftval1;
  172. //
  173. //{Shift and zerofill the hi+lo register}
  174. //emit_instr(taicpu.op_reg_reg_shifterop(A_MOV, dstlo, srclo, so));
  175. //emit_instr(taicpu.op_reg_reg_shifterop(A_MOV, dsthi, srchi, so));
  176. //
  177. //{Fold in the lower 32-shiftval bits}
  178. //if sm = SM_LSR then so.shiftmode:=SM_LSL else so.shiftmode:=SM_LSR;
  179. //so.rs:=shiftval2;
  180. //emit_instr(taicpu.op_reg_reg_reg_shifterop(A_ORR, dstlo, dstlo, srchi, so));
  181. //
  182. //cg.a_reg_alloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  183. //emit_instr(setoppostfix(taicpu.op_reg_reg_const(A_SUB, shiftval2, shiftval1, 32), PF_S));
  184. //
  185. //so.shiftmode:=sm;
  186. //emit_instr(setcondition(taicpu.op_reg_reg_shifterop(A_MOV, dstlo, srchi, so), C_PL));
  187. //cg.a_reg_dealloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  188. end;
  189. begin
  190. inherited;
  191. //if GenerateThumbCode or GenerateThumb2Code then
  192. //begin
  193. // inherited;
  194. // exit;
  195. //end;
  196. //
  197. //location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  198. //location.register64.reghi:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  199. //location.register64.reglo:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  200. //
  201. //{ load left operator in a register }
  202. //if not(left.location.loc in [LOC_CREGISTER,LOC_REGISTER]) or
  203. // (left.location.size<>OS_64) then
  204. // hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,resultdef,true);
  205. //
  206. //lreg := left.location.register64;
  207. //resreg := location.register64;
  208. //shifterop_reset(so);
  209. //
  210. //{ shifting by a constant directly coded: }
  211. //if (right.nodetype=ordconstn) then
  212. // begin
  213. // v:=Tordconstnode(right).value and 63;
  214. // {Single bit shift}
  215. // if v = 1 then
  216. // if nodetype=shln then
  217. // begin
  218. // {Shift left by one by 2 simple 32bit additions}
  219. // cg.a_reg_alloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  220. // emit_instr(setoppostfix(taicpu.op_reg_reg_reg(A_ADD, resreg.reglo, lreg.reglo, lreg.reglo), PF_S));
  221. // emit_instr(taicpu.op_reg_reg_reg(A_ADC, resreg.reghi, lreg.reghi, lreg.reghi));
  222. // cg.a_reg_dealloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  223. // end
  224. // else
  225. // begin
  226. // {Shift right by first shifting hi by one and then using RRX (rotate right extended), which rotates through the carry}
  227. // shifterop_reset(so); so.shiftmode:=SM_LSR; so.shiftimm:=1;
  228. // cg.a_reg_alloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  229. // emit_instr(setoppostfix(taicpu.op_reg_reg_shifterop(A_MOV, resreg.reghi, lreg.reghi, so), PF_S));
  230. // so.shiftmode:=SM_RRX; so.shiftimm:=0; {RRX does NOT have a shift amount}
  231. // emit_instr(taicpu.op_reg_reg_shifterop(A_MOV, resreg.reglo, lreg.reglo, so));
  232. // cg.a_reg_dealloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  233. // end
  234. // {Clear one register and use the cg to generate a normal 32-bit shift}
  235. // else if v >= 32 then
  236. // if nodetype=shln then
  237. // begin
  238. // emit_instr(taicpu.op_reg_const(A_MOV, resreg.reglo, 0));
  239. // cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SHL,OS_32,v.uvalue-32,lreg.reglo,resreg.reghi);
  240. // end
  241. // else
  242. // begin
  243. // emit_instr(taicpu.op_reg_const(A_MOV, resreg.reghi, 0));
  244. // cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SHR,OS_32,v.uvalue-32,lreg.reghi,resreg.reglo);
  245. // end
  246. // {Shift LESS than 32, thats the tricky one}
  247. // else if (v < 32) and (v > 1) then
  248. // if nodetype=shln then
  249. // shift_less_than_32(lreg.reglo, lreg.reghi, resreg.reglo, resreg.reghi, v.uvalue, SM_LSL)
  250. // else
  251. // shift_less_than_32(lreg.reghi, lreg.reglo, resreg.reghi, resreg.reglo, v.uvalue, SM_LSR);
  252. // end
  253. //else
  254. // begin
  255. // { force right operator into a register }
  256. // if not(right.location.loc in [LOC_CREGISTER,LOC_REGISTER]) or
  257. // (right.location.size<>OS_32) then
  258. // hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,u32inttype,true);
  259. //
  260. // if nodetype = shln then
  261. // shift_by_variable(lreg.reglo, lreg.reghi, resreg.reglo, resreg.reghi, right.location.register, SM_LSL)
  262. // else
  263. // shift_by_variable(lreg.reghi, lreg.reglo, resreg.reghi, resreg.reglo, right.location.register, SM_LSR);
  264. // end;
  265. end;
  266. begin
  267. cmoddivnode:=tcpumoddivnode;
  268. cnotnode:=tcpunotnode;
  269. cunaryminusnode:=tcpuunaryminusnode;
  270. cshlshrnode:=tcpushlshrnode;
  271. end.