ncpumat.pas 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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. location.loc:=LOC_REGISTER;
  64. //{ if the location is LOC_JUMP, we do the secondpass after the
  65. // labels are allocated
  66. //}
  67. //if not handle_locjump then
  68. // begin
  69. // secondpass(left);
  70. // case left.location.loc of
  71. // LOC_FLAGS :
  72. // begin
  73. // location_copy(location,left.location);
  74. // inverse_flags(location.resflags);
  75. // end;
  76. // LOC_REGISTER,LOC_CREGISTER,LOC_REFERENCE,LOC_CREFERENCE,
  77. // LOC_SUBSETREG,LOC_CSUBSETREG,LOC_SUBSETREF,LOC_CSUBSETREF :
  78. // begin
  79. // hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true);
  80. // cg.a_reg_alloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  81. // if is_64bit(resultdef) then
  82. // begin
  83. // tmpreg:=cg.GetIntRegister(current_asmdata.CurrAsmList,OS_INT);
  84. // { OR low and high parts together }
  85. // current_asmdata.CurrAsmList.concat(setoppostfix(taicpu.op_reg_reg_reg(A_ORR,tmpreg,left.location.register64.reglo,left.location.register64.reghi),PF_S));
  86. // end
  87. // else
  88. // current_asmdata.CurrAsmList.concat(taicpu.op_reg_const(A_CMP,left.location.register,0));
  89. // location_reset(location,LOC_FLAGS,OS_NO);
  90. // location.resflags:=F_EQ;
  91. // end;
  92. // else
  93. // internalerror(2003042401);
  94. // end;
  95. // end;
  96. end;
  97. {*****************************************************************************
  98. TARMUNARYMINUSNODE
  99. *****************************************************************************}
  100. function tcpuunaryminusnode.pass_1: tnode;
  101. var
  102. procname: string[31];
  103. fdef : tdef;
  104. begin
  105. Result:=nil;
  106. //if (current_settings.fputype=fpu_soft) and
  107. // (left.resultdef.typ=floatdef) then
  108. // begin
  109. // result:=nil;
  110. // firstpass(left);
  111. // expectloc:=LOC_REGISTER;
  112. // exit;
  113. // end;
  114. //
  115. //if not(FPUARM_HAS_VFP_SINGLE_ONLY in fpu_capabilities[current_settings.fputype]) or
  116. // (tfloatdef(resultdef).floattype=s32real) then
  117. // exit(inherited pass_1);
  118. //
  119. //result:=nil;
  120. //firstpass(left);
  121. //if codegenerror then
  122. // exit;
  123. //
  124. //if (left.resultdef.typ=floatdef) then
  125. // begin
  126. // case tfloatdef(resultdef).floattype of
  127. // s64real:
  128. // begin
  129. // procname:='float64_sub';
  130. // fdef:=search_system_type('FLOAT64').typedef;
  131. // end;
  132. // else
  133. // internalerror(2005082801);
  134. // end;
  135. // result:=ctypeconvnode.create_internal(ccallnode.createintern(procname,ccallparanode.create(
  136. // ctypeconvnode.create_internal(left,fDef),
  137. // ccallparanode.create(ctypeconvnode.create_internal(crealconstnode.create(0,resultdef),fdef),nil))),resultdef);
  138. //
  139. // left:=nil;
  140. // end
  141. //else
  142. // begin
  143. // if (left.resultdef.typ=floatdef) then
  144. // expectloc:=LOC_FPUREGISTER
  145. // else if (left.resultdef.typ=orddef) then
  146. // expectloc:=LOC_REGISTER;
  147. // end;
  148. end;
  149. procedure tcpuunaryminusnode.second_float;
  150. begin
  151. //secondpass(left);
  152. //case current_settings.fputype of
  153. // fpu_fpa,
  154. // fpu_fpa10,
  155. // fpu_fpa11:
  156. // begin
  157. // hlcg.location_force_fpureg(current_asmdata.CurrAsmList,left.location,left.resultdef,false);
  158. // location:=left.location;
  159. // current_asmdata.CurrAsmList.concat(setoppostfix(taicpu.op_reg_reg_const(A_RSF,
  160. // location.register,left.location.register,0),
  161. // cgsize2fpuoppostfix[def_cgsize(resultdef)]));
  162. // end;
  163. // fpu_soft:
  164. // begin
  165. // hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,false);
  166. // location:=left.location;
  167. // case location.size of
  168. // OS_32:
  169. // cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_XOR,OS_32,tcgint($80000000),location.register);
  170. // OS_64:
  171. // cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_XOR,OS_32,tcgint($80000000),location.registerhi);
  172. // else
  173. // internalerror(2014033101);
  174. // end;
  175. // end
  176. // else if FPUARM_HAS_VFP_DOUBLE in fpu_capabilities[init_settings.fputype] then
  177. // begin
  178. // hlcg.location_force_mmregscalar(current_asmdata.CurrAsmList,left.location,left.resultdef,true);
  179. // location:=left.location;
  180. // if (left.location.loc=LOC_CMMREGISTER) then
  181. // location.register:=cg.getmmregister(current_asmdata.CurrAsmList,location.size);
  182. //
  183. // if (tfloatdef(left.resultdef).floattype=s32real) then
  184. // pf:=PF_F32
  185. // else
  186. // pf:=PF_F64;
  187. //
  188. // current_asmdata.CurrAsmList.concat(setoppostfix(taicpu.op_reg_reg(A_VNEG,
  189. // location.register,left.location.register), pf));
  190. // cg.maybe_check_for_fpu_exception(current_asmdata.CurrAsmList);
  191. // end
  192. // else if FPUARM_HAS_VFP_SINGLE_ONLY in fpu_capabilities[init_settings.fputype] then
  193. // begin
  194. // hlcg.location_force_mmregscalar(current_asmdata.CurrAsmList,left.location,left.resultdef,true);
  195. // location:=left.location;
  196. // if (left.location.loc=LOC_CMMREGISTER) then
  197. // location.register:=cg.getmmregister(current_asmdata.CurrAsmList,location.size);
  198. // current_asmdata.CurrAsmList.concat(setoppostfix(taicpu.op_reg_reg(A_VNEG,
  199. // location.register,left.location.register), PF_F32));
  200. // cg.maybe_check_for_fpu_exception(current_asmdata.CurrAsmList);
  201. // end
  202. // else
  203. // internalerror(2009112602);
  204. //end;
  205. end;
  206. procedure tcpushlshrnode.second_64bit;
  207. var
  208. v : TConstExprInt;
  209. so: tshifterop;
  210. lreg, resreg: TRegister64;
  211. procedure emit_instr(p: tai);
  212. begin
  213. current_asmdata.CurrAsmList.concat(p);
  214. end;
  215. {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
  216. This will generate
  217. mov shiftval1, shiftval
  218. cmp shiftval1, #64
  219. movcs shiftval1, #64
  220. rsb shiftval2, shiftval1, #32
  221. mov dstlo, srclo, lsr shiftval1
  222. mov dsthi, srchi, lsr shiftval1
  223. orr dstlo, srchi, lsl shiftval2
  224. subs shiftval2, shiftval1, #32
  225. movpl dstlo, srchi, lsr shiftval2
  226. }
  227. procedure shift_by_variable(srchi, srclo, dsthi, dstlo, shiftval: TRegister; sm: TShiftMode);
  228. var
  229. shiftval1,shiftval2:TRegister;
  230. begin
  231. //shifterop_reset(so);
  232. //shiftval1:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  233. //shiftval2:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  234. //
  235. //cg.a_load_reg_reg(current_asmdata.CurrAsmList, OS_INT, OS_INT, shiftval, shiftval1);
  236. //
  237. //{The ARM barrel shifter only considers the lower 8 bits of a register for the shift}
  238. //cg.a_reg_alloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  239. //emit_instr(taicpu.op_reg_const(A_CMP, shiftval1, 64));
  240. //emit_instr(setcondition(taicpu.op_reg_const(A_MOV, shiftval1, 64), C_CS));
  241. //cg.a_reg_dealloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  242. //
  243. //{Calculate how much the upper register needs to be shifted left}
  244. //emit_instr(taicpu.op_reg_reg_const(A_RSB, shiftval2, shiftval1, 32));
  245. //
  246. //so.shiftmode:=sm;
  247. //so.rs:=shiftval1;
  248. //
  249. //{Shift and zerofill the hi+lo register}
  250. //emit_instr(taicpu.op_reg_reg_shifterop(A_MOV, dstlo, srclo, so));
  251. //emit_instr(taicpu.op_reg_reg_shifterop(A_MOV, dsthi, srchi, so));
  252. //
  253. //{Fold in the lower 32-shiftval bits}
  254. //if sm = SM_LSR then so.shiftmode:=SM_LSL else so.shiftmode:=SM_LSR;
  255. //so.rs:=shiftval2;
  256. //emit_instr(taicpu.op_reg_reg_reg_shifterop(A_ORR, dstlo, dstlo, srchi, so));
  257. //
  258. //cg.a_reg_alloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  259. //emit_instr(setoppostfix(taicpu.op_reg_reg_const(A_SUB, shiftval2, shiftval1, 32), PF_S));
  260. //
  261. //so.shiftmode:=sm;
  262. //emit_instr(setcondition(taicpu.op_reg_reg_shifterop(A_MOV, dstlo, srchi, so), C_PL));
  263. //cg.a_reg_dealloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  264. end;
  265. begin
  266. //if GenerateThumbCode or GenerateThumb2Code then
  267. //begin
  268. // inherited;
  269. // exit;
  270. //end;
  271. //
  272. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  273. //location.register64.reghi:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  274. //location.register64.reglo:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  275. //
  276. //{ load left operator in a register }
  277. //if not(left.location.loc in [LOC_CREGISTER,LOC_REGISTER]) or
  278. // (left.location.size<>OS_64) then
  279. // hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,resultdef,true);
  280. //
  281. //lreg := left.location.register64;
  282. //resreg := location.register64;
  283. //shifterop_reset(so);
  284. //
  285. //{ shifting by a constant directly coded: }
  286. //if (right.nodetype=ordconstn) then
  287. // begin
  288. // v:=Tordconstnode(right).value and 63;
  289. // {Single bit shift}
  290. // if v = 1 then
  291. // if nodetype=shln then
  292. // begin
  293. // {Shift left by one by 2 simple 32bit additions}
  294. // cg.a_reg_alloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  295. // emit_instr(setoppostfix(taicpu.op_reg_reg_reg(A_ADD, resreg.reglo, lreg.reglo, lreg.reglo), PF_S));
  296. // emit_instr(taicpu.op_reg_reg_reg(A_ADC, resreg.reghi, lreg.reghi, lreg.reghi));
  297. // cg.a_reg_dealloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  298. // end
  299. // else
  300. // begin
  301. // {Shift right by first shifting hi by one and then using RRX (rotate right extended), which rotates through the carry}
  302. // shifterop_reset(so); so.shiftmode:=SM_LSR; so.shiftimm:=1;
  303. // cg.a_reg_alloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  304. // emit_instr(setoppostfix(taicpu.op_reg_reg_shifterop(A_MOV, resreg.reghi, lreg.reghi, so), PF_S));
  305. // so.shiftmode:=SM_RRX; so.shiftimm:=0; {RRX does NOT have a shift amount}
  306. // emit_instr(taicpu.op_reg_reg_shifterop(A_MOV, resreg.reglo, lreg.reglo, so));
  307. // cg.a_reg_dealloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  308. // end
  309. // {Clear one register and use the cg to generate a normal 32-bit shift}
  310. // else if v >= 32 then
  311. // if nodetype=shln then
  312. // begin
  313. // emit_instr(taicpu.op_reg_const(A_MOV, resreg.reglo, 0));
  314. // cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SHL,OS_32,v.uvalue-32,lreg.reglo,resreg.reghi);
  315. // end
  316. // else
  317. // begin
  318. // emit_instr(taicpu.op_reg_const(A_MOV, resreg.reghi, 0));
  319. // cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SHR,OS_32,v.uvalue-32,lreg.reghi,resreg.reglo);
  320. // end
  321. // {Shift LESS than 32, thats the tricky one}
  322. // else if (v < 32) and (v > 1) then
  323. // if nodetype=shln then
  324. // shift_less_than_32(lreg.reglo, lreg.reghi, resreg.reglo, resreg.reghi, v.uvalue, SM_LSL)
  325. // else
  326. // shift_less_than_32(lreg.reghi, lreg.reglo, resreg.reghi, resreg.reglo, v.uvalue, SM_LSR);
  327. // end
  328. //else
  329. // begin
  330. // { force right operator into a register }
  331. // if not(right.location.loc in [LOC_CREGISTER,LOC_REGISTER]) or
  332. // (right.location.size<>OS_32) then
  333. // hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,u32inttype,true);
  334. //
  335. // if nodetype = shln then
  336. // shift_by_variable(lreg.reglo, lreg.reghi, resreg.reglo, resreg.reghi, right.location.register, SM_LSL)
  337. // else
  338. // shift_by_variable(lreg.reghi, lreg.reglo, resreg.reghi, resreg.reglo, right.location.register, SM_LSR);
  339. // end;
  340. end;
  341. begin
  342. cmoddivnode:=tcpumoddivnode;
  343. cnotnode:=tcpunotnode;
  344. cunaryminusnode:=tcpuunaryminusnode;
  345. cshlshrnode:=tcpushlshrnode;
  346. end.