ncpumat.pas 12 KB

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