cgcpu.pas 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565
  1. {
  2. Copyright (c) 2014 by Jonas Maebe
  3. This unit implements the code generator for Xtensa
  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 cgcpu;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. globtype,parabase,
  22. cgbase,cgutils,cgobj,
  23. aasmbase,aasmtai,aasmdata,aasmcpu,
  24. cpubase,cpuinfo,
  25. node,symconst,SymType,symdef,
  26. rgcpu,
  27. cg64f32;
  28. type
  29. tcgcpu=class(tcg)
  30. private
  31. procedure fixref(list : TAsmList; var ref : treference);
  32. procedure g_concatcopy_move(list : tasmlist; const Source,dest : treference; len : tcgint);
  33. public
  34. procedure init_register_allocators;override;
  35. procedure done_register_allocators;override;
  36. { move instructions }
  37. procedure a_load_reg_reg(list: TAsmList; fromsize, tosize: tcgsize; reg1, reg2: tregister);override;
  38. procedure a_load_reg_ref(list: TAsmList; fromsize, tosize: tcgsize; reg: tregister;const ref: TReference);override;
  39. procedure a_load_ref_reg(list: TAsmList; fromsize, tosize: tcgsize; const ref: TReference; reg: tregister);override;
  40. procedure a_load_const_reg(list: TAsmList; size: tcgsize; a: tcgint; reg: tregister);override;
  41. procedure a_loadaddr_ref_reg(list: TAsmList; const ref: TReference; r: tregister);override;
  42. procedure a_op_reg_reg(list: TAsmList; op: topcg; size: tcgsize; src, dst: tregister);override;
  43. procedure a_op_const_reg(list: TAsmList; op: topcg; size: tcgsize; a: tcgint; reg: tregister);override;
  44. procedure a_op_reg_reg_reg(list: TAsmList; op: topcg; size: tcgsize; src1, src2, dst: tregister);override;
  45. procedure a_op_const_reg_reg(list : TAsmList; op : TOpCg; size : tcgsize; a : tcgint; src,dst : tregister);override;
  46. procedure a_call_name(list:TAsmList;const s:string; weak: boolean);override;
  47. procedure a_call_reg(list:TAsmList;Reg:tregister);override;
  48. procedure a_jmp_name(list: TAsmList; const s: string);override;
  49. procedure a_jmp_flags(list: TAsmList; const f: TResFlags; l: tasmlabel);override;
  50. procedure g_proc_entry(list: TAsmList; localsize: longint; nostackframe: boolean);override;
  51. procedure g_proc_exit(list: TAsmList; parasize: longint; nostackframe: boolean);override;
  52. { comparison operations }
  53. procedure a_cmp_const_reg_label(list: TAsmList; size: tcgsize; cmp_op: topcmp; a: tcgint; reg: tregister; l: tasmlabel); override;
  54. procedure a_cmp_reg_reg_label(list: TAsmList; size: tcgsize; cmp_op: topcmp; reg1, reg2: tregister; l: tasmlabel);override;
  55. procedure a_jmp_always(list: TAsmList; l: TAsmLabel);override;
  56. procedure a_bit_scan_reg_reg(list: TAsmList; reverse: boolean; srcsize, dstsize: TCGSize; src, dst: TRegister);override;
  57. procedure g_flags2reg(list: TAsmList; size: TCgSize; const f: tresflags; reg: TRegister);override;
  58. procedure g_concatcopy(list : TAsmList; const source,dest : treference; len : tcgint);override;
  59. procedure a_loadfpu_reg_reg(list: TAsmList; fromsize, tosize: tcgsize; reg1, reg2: tregister);override;
  60. procedure a_loadfpu_ref_reg(list: TAsmList; fromsize, tosize: tcgsize; const ref: treference; reg: tregister);override;
  61. procedure a_loadfpu_reg_ref(list: TAsmList; fromsize, tosize: tcgsize; reg: tregister; const ref: treference);override;
  62. procedure a_loadfpu_intreg_reg(list: TAsmList; fromsize, tosize: tcgsize; intreg, fpureg: tregister);override;
  63. procedure a_loadfpu_reg_intreg(list: TAsmList; fromsize, tosize: tcgsize; fpureg, intreg: tregister);override;
  64. procedure maybeadjustresult(list: TAsmList; op: TOpCg; size: tcgsize; dst: tregister);
  65. procedure g_overflowcheck(list: TAsmList; const Loc:tlocation; def:tdef);override;
  66. function create_data_entry(symbol: TAsmSymbol; offset: asizeint): TAsmLabel;
  67. end;
  68. tcg64fxtensa = class(tcg64f32)
  69. procedure a_op64_reg_reg(list : TAsmList;op:TOpCG;size : tcgsize;regsrc,regdst : tregister64);override;
  70. procedure a_op64_const_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;value : int64;regsrc,regdst : tregister64);override;
  71. procedure a_op64_const_reg(list : TAsmList;op:TOpCG;size : tcgsize;value : int64;reg : tregister64);override;
  72. procedure a_op64_reg_reg_reg(list : TAsmList; op : TOpCG;size : tcgsize; regsrc1,regsrc2,regdst : tregister64);override;
  73. //procedure a_op64_reg_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64);override;
  74. //procedure a_op64_reg_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64);override;
  75. //procedure a_op64_const_reg_reg_checkoverflow(list: TAsmList;op:TOpCG;size : tcgsize;value : int64;regsrc,regdst : tregister64;setflags : boolean;var ovloc : tlocation);override;
  76. //procedure a_op64_reg_reg_reg_checkoverflow(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64;setflags : boolean;var ovloc : tlocation);override;
  77. //procedure a_loadmm_intreg64_reg(list: TAsmList; mmsize: tcgsize; intreg: tregister64; mmreg: tregister);override;
  78. //procedure a_loadmm_reg_intreg64(list: TAsmList; mmsize: tcgsize; mmreg: tregister; intreg: tregister64);override;
  79. end;
  80. procedure create_codegen;
  81. const
  82. TOpCG2AsmOp: array[topcg] of TAsmOp = (
  83. A_NONE,A_MOV,A_ADD,A_AND,A_NONE,A_NONE,A_MULL,A_MULL,A_NEG,A_NONE,A_OR,A_SRA,A_SLL,A_SRL,A_SUB,A_XOR,A_NONE,A_NONE
  84. );
  85. {
  86. );TOpCG2AsmOpReg: array[topcg] of TAsmOp = (
  87. A_NONE,A_MOV,A_ADD,A_AND,A_UDIV,A_SDIV,A_MUL,A_MUL,A_NEG,A_MVN,A_ORR,A_ASRV,A_LSLV,A_LSRV,A_SUB,A_EOR,A_NONE,A_RORV
  88. );
  89. TOpCG2AsmOpImm: array[topcg] of TAsmOp = (
  90. A_NONE,A_MOV,A_ADD,A_AND,A_UDIV,A_SDIV,A_MUL,A_MUL,A_NEG,A_MVN,A_ORR,A_ASR,A_LSL,A_LSR,A_SUB,A_EOR,A_NONE,A_ROR
  91. );
  92. TOpCmp2AsmCond: array[topcmp] of TAsmCond = (C_NONE,C_EQ,C_GT,
  93. C_LT,C_GE,C_LE,C_NE,C_LS,C_CC,C_CS,C_HI
  94. );
  95. }
  96. implementation
  97. uses
  98. globals,verbose,systems,cutils,
  99. paramgr,fmodule,
  100. symtable,symsym,
  101. tgobj,
  102. procinfo,cpupi;
  103. const
  104. TOpCmp2AsmCond: array[TOpCmp] of TAsmCond = (
  105. C_None,
  106. C_EQ,
  107. C_None,
  108. C_LT,
  109. C_GE,
  110. C_None,
  111. C_NE,
  112. C_None,
  113. C_LTU,
  114. C_GEU,
  115. C_None
  116. );
  117. procedure tcgcpu.init_register_allocators;
  118. begin
  119. inherited init_register_allocators;
  120. if target_info.abi = abi_xtensa_call0 then
  121. rg[R_INTREGISTER]:=trgintcpu.create(R_INTREGISTER,R_SUBWHOLE,
  122. [RS_A2,RS_A3,RS_A4,RS_A5,RS_A6,RS_A7,{RS_A8,}RS_A9,
  123. RS_A10,RS_A11,RS_A12,RS_A13,RS_A14{,RS_A15}],first_int_imreg,[])
  124. else
  125. rg[R_INTREGISTER]:=trgintcpu.create(R_INTREGISTER,R_SUBWHOLE,
  126. [RS_A2,RS_A3,RS_A4,RS_A5,RS_A6,RS_A7,RS_A8,RS_A9,
  127. RS_A10,RS_A11,RS_A12,RS_A13,RS_A14,RS_A15],first_int_imreg,[]);
  128. rg[R_FPUREGISTER]:=trgcpu.create(R_FPUREGISTER,R_SUBNONE,
  129. [RS_F0,RS_F1,RS_F2,RS_F3,RS_F4,RS_F5,RS_F6,RS_F7,RS_F8,RS_F9,
  130. RS_F10,RS_F11,RS_F12,RS_F13,RS_F14,RS_F15],first_fpu_imreg,[]);
  131. rg[R_SPECIALREGISTER]:=trgcpu.create(R_SPECIALREGISTER,R_SUBNONE,
  132. [RS_B0,RS_B1,RS_B2,RS_B3,RS_B4,RS_B5,RS_B6,RS_B7,RS_B8,RS_B9,
  133. RS_B10,RS_B11,RS_B12,RS_B13,RS_B14,RS_B15],first_flag_imreg,[]);
  134. end;
  135. procedure tcgcpu.done_register_allocators;
  136. begin
  137. rg[R_INTREGISTER].free;
  138. rg[R_FPUREGISTER].free;
  139. rg[R_SPECIALREGISTER].free;
  140. inherited done_register_allocators;
  141. end;
  142. procedure tcgcpu.a_load_reg_reg(list : TAsmList; fromsize,tosize : tcgsize;
  143. reg1,reg2 : tregister);
  144. var
  145. conv_done : Boolean;
  146. instr : taicpu;
  147. begin
  148. if (tcgsize2size[fromsize]>32) or (tcgsize2size[tosize]>32) or (fromsize=OS_NO) or (tosize=OS_NO) then
  149. internalerror(2020030710);
  150. conv_done:=false;
  151. if tosize<>fromsize then
  152. begin
  153. conv_done:=true;
  154. if tcgsize2size[tosize]<=tcgsize2size[fromsize] then
  155. fromsize:=tosize;
  156. case fromsize of
  157. OS_8:
  158. list.concat(taicpu.op_reg_reg_const_const(A_EXTUI,reg2,reg1,0,8));
  159. OS_S8:
  160. begin
  161. if CPUXTENSA_HAS_SEXT in cpu_capabilities[current_settings.cputype] then
  162. list.concat(taicpu.op_reg_reg_const(A_SEXT,reg2,reg1,7))
  163. else
  164. begin
  165. list.concat(taicpu.op_reg_reg_const(A_SLLI,reg2,reg1,24));
  166. list.concat(taicpu.op_reg_reg_const(A_SRAI,reg2,reg2,24));
  167. end;
  168. if tosize=OS_16 then
  169. list.concat(taicpu.op_reg_reg_const_const(A_EXTUI,reg2,reg2,0,16));
  170. end;
  171. OS_16:
  172. list.concat(taicpu.op_reg_reg_const_const(A_EXTUI,reg2,reg1,0,16));
  173. OS_S16:
  174. if CPUXTENSA_HAS_SEXT in cpu_capabilities[current_settings.cputype] then
  175. list.concat(taicpu.op_reg_reg_const(A_SEXT,reg2,reg1,15))
  176. else
  177. begin
  178. list.concat(taicpu.op_reg_reg_const(A_SLLI,reg2,reg1,16));
  179. list.concat(taicpu.op_reg_reg_const(A_SRAI,reg2,reg2,16));
  180. end;
  181. else
  182. conv_done:=false;
  183. end;
  184. end;
  185. if not conv_done and (reg1<>reg2) then
  186. begin
  187. { same size, only a register mov required }
  188. instr:=taicpu.op_reg_reg(A_MOV,reg2,reg1);
  189. list.Concat(instr);
  190. { Notify the register allocator that we have written a move instruction so
  191. it can try to eliminate it. }
  192. add_move_instruction(instr);
  193. end;
  194. end;
  195. procedure tcgcpu.a_load_reg_ref(list : TAsmList; fromsize,tosize : tcgsize;
  196. reg : tregister; const ref : TReference);
  197. var
  198. op: TAsmOp;
  199. href : treference;
  200. begin
  201. if (TCGSize2Size[FromSize] >= TCGSize2Size[ToSize]) then
  202. FromSize := ToSize;
  203. case tosize of
  204. { signed integer registers }
  205. OS_8,
  206. OS_S8:
  207. op:=A_S8I;
  208. OS_16,
  209. OS_S16:
  210. op:=A_S16I;
  211. OS_32,
  212. OS_S32:
  213. op:=A_S32I;
  214. else
  215. InternalError(2020030804);
  216. end;
  217. href:=ref;
  218. if assigned(href.symbol) or
  219. (href.index<>NR_NO) or
  220. ((op=A_S8I) and ((href.offset<0) or (href.offset>255))) or
  221. ((op=A_S16I) and ((href.offset<0) or (href.offset>510) or (href.offset mod 2<>0))) or
  222. ((op=A_S32I) and ((href.offset<0) or (href.offset>1020) or (href.offset mod 4<>0))) then
  223. fixref(list,href);
  224. list.concat(taicpu.op_reg_ref(op,reg,href));
  225. end;
  226. procedure tcgcpu.a_load_ref_reg(list : TAsmList; fromsize,tosize : tcgsize;
  227. const ref : TReference; reg : tregister);
  228. var
  229. href: treference;
  230. op: TAsmOp;
  231. tmpreg: TRegister;
  232. begin
  233. case fromsize of
  234. OS_8: op:=A_L8UI;
  235. OS_16: op:=A_L16UI;
  236. OS_S8: op:=A_L8UI;
  237. OS_S16: op:=A_L16SI;
  238. OS_64,OS_S64, { This only happens if tosize is smaller than fromsize }
  239. { We can therefore only consider the low 32-bit of the 64bit value }
  240. OS_32,
  241. OS_S32: op:=A_L32I;
  242. else
  243. internalerror(2020030805);
  244. end;
  245. href:=ref;
  246. if assigned(href.symbol) or
  247. (href.index<>NR_NO) or
  248. ((op=A_L8UI) and ((href.offset<0) or (href.offset>255))) or
  249. ((op in [A_L16SI,A_L16UI]) and ((href.offset<0) or (href.offset>510) or (href.offset mod 2<>0))) or
  250. ((op=A_L32I) and ((href.offset<0) or (href.offset>1020) or (href.offset mod 4<>0))) or
  251. ((href.base=NR_NO) and (href.index=NR_NO)) then
  252. fixref(list,href);
  253. list.concat(taicpu.op_reg_ref(op,reg,href));
  254. if (fromsize=OS_S8) and not(tosize in [OS_S8,OS_8]) then
  255. if CPUXTENSA_HAS_SEXT in cpu_capabilities[current_settings.cputype] then
  256. list.concat(taicpu.op_reg_reg_const(A_SEXT,reg,reg,7))
  257. else
  258. begin
  259. list.concat(taicpu.op_reg_reg_const(A_SLLI,reg,reg,24));
  260. list.concat(taicpu.op_reg_reg_const(A_SRAI,reg,reg,24));
  261. end;
  262. if (fromsize<>tosize) and (not (tosize in [OS_SINT,OS_INT])) then
  263. a_load_reg_reg(list,fromsize,tosize,reg,reg);
  264. end;
  265. procedure tcgcpu.a_load_const_reg(list : TAsmList; size : tcgsize;
  266. a : tcgint; reg : tregister);
  267. var
  268. hr : treference;
  269. l : TAsmLabel;
  270. begin
  271. if (a>=-2048) and (a<=2047) then
  272. list.Concat(taicpu.op_reg_const(A_MOVI,reg,a))
  273. else
  274. begin
  275. reference_reset(hr,4,[]);
  276. hr.symbol:=create_data_entry(nil,longint(a));
  277. list.concat(taicpu.op_reg_ref(A_L32R,reg,hr));
  278. end;
  279. end;
  280. procedure tcgcpu.fixref(list : TAsmList;var ref : treference);
  281. var
  282. tmpreg, tmpreg2 : tregister;
  283. tmpref : treference;
  284. l : tasmlabel;
  285. begin
  286. { create consts entry }
  287. if assigned(ref.symbol) or (ref.offset<-2048) or (ref.offset>2047) or
  288. ((ref.base=NR_NO) and (ref.index=NR_NO)) then
  289. begin
  290. reference_reset(tmpref,4,[]);
  291. tmpreg:=NR_NO;
  292. { load consts entry }
  293. tmpreg:=getintregister(list,OS_INT);
  294. if ref.symbol=nil then
  295. a_load_const_reg(list,OS_ADDR,ref.offset,tmpreg)
  296. else
  297. begin
  298. tmpref.symbol:=create_data_entry(ref.symbol,ref.offset);
  299. list.concat(taicpu.op_reg_ref(A_L32R,tmpreg,tmpref));
  300. end;
  301. if ref.base<>NR_NO then
  302. begin
  303. if ref.index<>NR_NO then
  304. begin
  305. list.concat(taicpu.op_reg_reg_reg(A_ADD,tmpreg,ref.base,tmpreg));
  306. ref.base:=tmpreg;
  307. end
  308. else
  309. ref.index:=tmpreg;
  310. end
  311. else
  312. ref.base:=tmpreg;
  313. end
  314. else if ref.offset<>0 then
  315. begin
  316. tmpreg:=getintregister(list,OS_INT);
  317. if (ref.offset>=-128) and (ref.offset<=127) then
  318. begin
  319. list.concat(taicpu.op_reg_reg_const(A_ADDI,tmpreg,ref.base,ref.offset));
  320. ref.base:=tmpreg;
  321. end
  322. else
  323. begin
  324. list.concat(taicpu.op_reg_const(A_MOVI,tmpreg,ref.offset));
  325. if ref.base<>NR_NO then
  326. begin
  327. if ref.index<>NR_NO then
  328. begin
  329. list.concat(taicpu.op_reg_reg_reg(A_ADD,tmpreg,ref.base,tmpreg));
  330. ref.base:=tmpreg;
  331. end
  332. else
  333. ref.index:=tmpreg;
  334. end
  335. else
  336. ref.base:=tmpreg;
  337. end;
  338. end;
  339. if ref.index<>NR_NO then
  340. begin
  341. if ref.base<>NR_NO then
  342. begin
  343. tmpreg:=getintregister(list,OS_INT);
  344. list.concat(taicpu.op_reg_reg_reg(A_ADD,tmpreg,ref.base,ref.index));
  345. ref.base:=tmpreg;
  346. end
  347. else
  348. ref.base:=ref.index;
  349. ref.index:=NR_NO;
  350. end;
  351. ref.offset:=0;
  352. ref.symbol:=nil;
  353. end;
  354. procedure tcgcpu.a_loadaddr_ref_reg(list : TAsmList;
  355. const ref : TReference; r : tregister);
  356. var
  357. b : byte;
  358. tmpref : treference;
  359. instr : taicpu;
  360. begin
  361. tmpref:=ref;
  362. { Be sure to have a base register }
  363. if tmpref.base=NR_NO then
  364. begin
  365. tmpref.base:=tmpref.index;
  366. tmpref.index:=NR_NO;
  367. end;
  368. if assigned(tmpref.symbol) then
  369. fixref(list,tmpref);
  370. { expect a base here if there is an index }
  371. if (tmpref.base=NR_NO) and (tmpref.index<>NR_NO) then
  372. internalerror(200312022);
  373. if tmpref.index<>NR_NO then
  374. begin
  375. a_op_reg_reg_reg(list,OP_ADD,OS_ADDR,tmpref.base,tmpref.index,r);
  376. if tmpref.offset<>0 then
  377. a_op_const_reg_reg(list,OP_ADD,OS_ADDR,tmpref.offset,r,r);
  378. end
  379. else
  380. begin
  381. if tmpref.base=NR_NO then
  382. a_load_const_reg(list,OS_ADDR,tmpref.offset,r)
  383. else
  384. if tmpref.offset<>0 then
  385. a_op_const_reg_reg(list,OP_ADD,OS_ADDR,tmpref.offset,tmpref.base,r)
  386. else
  387. begin
  388. instr:=taicpu.op_reg_reg(A_MOV,r,tmpref.base);
  389. list.concat(instr);
  390. add_move_instruction(instr);
  391. end;
  392. end;
  393. end;
  394. procedure tcgcpu.a_op_reg_reg(list : TAsmList; op : topcg; size : tcgsize; src,dst : tregister);
  395. var
  396. tmpreg : TRegister;
  397. begin
  398. if op = OP_NEG then
  399. begin
  400. list.concat(taicpu.op_reg_reg(A_NEG,dst,src));
  401. maybeadjustresult(list,OP_NEG,size,dst);
  402. end
  403. else if op = OP_NOT then
  404. begin
  405. tmpreg:=getintregister(list,size);
  406. list.concat(taicpu.op_reg_const(A_MOVI,tmpreg,-1));
  407. list.concat(taicpu.op_reg_reg_reg(A_XOR,dst,tmpreg,src));
  408. maybeadjustresult(list,OP_NOT,size,dst);
  409. end
  410. else
  411. a_op_reg_reg_reg(list,op,size,src,dst,dst);
  412. end;
  413. procedure tcgcpu.a_op_const_reg_reg(list: TAsmList; op: TOpCg; size: tcgsize; a: tcgint; src, dst: tregister);
  414. var
  415. l1 : longint;
  416. tmpreg : TRegister;
  417. begin
  418. optimize_op_const(size, op, a);
  419. case op of
  420. OP_NONE:
  421. begin
  422. if src <> dst then
  423. a_load_reg_reg(list, size, size, src, dst);
  424. exit;
  425. end;
  426. OP_MOVE:
  427. begin
  428. a_load_const_reg(list, size, a, dst);
  429. exit;
  430. end;
  431. else
  432. ;
  433. end;
  434. { there could be added some more sophisticated optimizations }
  435. if (op in [OP_IMUL,OP_IDIV]) and (a=-1) then
  436. a_op_reg_reg(list,OP_NEG,size,src,dst)
  437. { we do this here instead in the peephole optimizer because
  438. it saves us a register }
  439. else if (op in [OP_MUL,OP_IMUL]) and ispowerof2(a,l1) then
  440. a_op_const_reg_reg(list,OP_SHL,size,l1,src,dst)
  441. { we cannot make use of SUB(X) here because the inital shift might overflow }
  442. else if (op in [OP_MUL,OP_IMUL]) and (a=3) then
  443. list.concat(taicpu.op_reg_reg_reg(A_ADDX2,dst,src,src))
  444. else if (op in [OP_MUL,OP_IMUL]) and (a=5) then
  445. list.concat(taicpu.op_reg_reg_reg(A_ADDX4,dst,src,src))
  446. else if (op in [OP_MUL,OP_IMUL]) and (a=9) then
  447. list.concat(taicpu.op_reg_reg_reg(A_ADDX8,dst,src,src))
  448. else if (op in [OP_MUL,OP_IMUL]) and (src<>dst) and ispowerof2(a-8,l1) then
  449. begin
  450. a_op_const_reg_reg(list,OP_SHL,size,l1,src,dst);
  451. list.concat(taicpu.op_reg_reg_reg(A_ADDX8,dst,src,dst));
  452. end
  453. else if (op in [OP_MUL,OP_IMUL]) and (src<>dst) and ispowerof2(a-4,l1) then
  454. begin
  455. a_op_const_reg_reg(list,OP_SHL,size,l1,src,dst);
  456. list.concat(taicpu.op_reg_reg_reg(A_ADDX4,dst,src,dst));
  457. end
  458. else if (op in [OP_MUL,OP_IMUL]) and (src<>dst) and ispowerof2(a-2,l1) then
  459. begin
  460. a_op_const_reg_reg(list,OP_SHL,size,l1,src,dst);
  461. list.concat(taicpu.op_reg_reg_reg(A_ADDX2,dst,src,dst));
  462. end
  463. else if (op in [OP_MUL,OP_IMUL]) and (src<>dst) and ispowerof2(a-1,l1) then
  464. begin
  465. a_op_const_reg_reg(list,OP_SHL,size,l1,src,dst);
  466. list.concat(taicpu.op_reg_reg_reg(A_ADD,dst,src,dst));
  467. end
  468. else if (op=OP_ADD) and (a>=-128) and (a<=127) then
  469. list.concat(taicpu.op_reg_reg_const(A_ADDI,dst,src,a))
  470. else if (op=OP_ADD) and (a>=-128-32768) and (a<=127+32512) then
  471. begin
  472. {$ifdef EXTDEBUG}
  473. list.concat(tai_comment.Create(strpnew('Value: '+tostr(a))));
  474. {$endif EXTDEBUG}
  475. list.concat(taicpu.op_reg_reg_const(A_ADDMI,dst,src,Smallint((a+128) and $ff00)));
  476. list.concat(taicpu.op_reg_reg_const(A_ADDI,dst,dst,Shortint(a and $ff)));
  477. end
  478. else if (op=OP_SUB) and (a>=-127) and (a<=128) then
  479. list.concat(taicpu.op_reg_reg_const(A_ADDI,dst,src,-a))
  480. else if (op=OP_SUB) and (a>=-127-32512) and (a<=128+32768) then
  481. begin
  482. {$ifdef EXTDEBUG}
  483. list.concat(tai_comment.Create(strpnew('Value: '+tostr(a))));
  484. {$endif EXTDEBUG}
  485. a:=-a;
  486. list.concat(taicpu.op_reg_reg_const(A_ADDMI,dst,src,Smallint((a+128) and $ff00)));
  487. list.concat(taicpu.op_reg_reg_const(A_ADDI,dst,dst,Shortint(a and $ff)));
  488. end
  489. else if (op=OP_SHL) and (a>=1) and (a<=31) then
  490. list.concat(taicpu.op_reg_reg_const(A_SLLI,dst,src,a))
  491. else if (op=OP_SAR) and (a>=0) and (a<=31) then
  492. list.concat(taicpu.op_reg_reg_const(A_SRAI,dst,src,a))
  493. else if (op=OP_SHR) and (a>=0) and (a<=15) then
  494. list.concat(taicpu.op_reg_reg_const(A_SRLI,dst,src,a))
  495. else if (op=OP_SHR) and (a>15) and (a<=31) then
  496. list.concat(taicpu.op_reg_reg_const_const(A_EXTUI,dst,src,a,32-a))
  497. else if (op=OP_AND) and (63-BsrQWord(qword(a))+PopCnt(QWord(a))=64) and (PopCnt(QWord(a))<=16) then
  498. list.concat(taicpu.op_reg_reg_const_const(A_EXTUI,dst,src,0,PopCnt(QWord(a))))
  499. else
  500. begin
  501. tmpreg:=getintregister(list,size);
  502. a_load_const_reg(list,size,a,tmpreg);
  503. a_op_reg_reg_reg(list,op,size,tmpreg,src,dst);
  504. end;
  505. maybeadjustresult(list,op,size,dst);
  506. end;
  507. procedure tcgcpu.a_op_const_reg(list : TAsmList; op : topcg; size : tcgsize; a : tcgint; reg : tregister);
  508. begin
  509. a_op_const_reg_reg(list,op,size,a,reg,reg);
  510. end;
  511. procedure tcgcpu.a_op_reg_reg_reg(list : TAsmList; op : topcg;
  512. size : tcgsize; src1,src2,dst : tregister);
  513. var
  514. tmpreg : TRegister;
  515. begin
  516. if op=OP_NOT then
  517. begin
  518. tmpreg:=getintregister(list,size);
  519. list.concat(taicpu.op_reg_const(A_MOVI,tmpreg,-1));
  520. maybeadjustresult(list,op,size,dst);
  521. end
  522. else if op=OP_NEG then
  523. begin
  524. list.concat(taicpu.op_reg_reg(A_NEG,dst,src1));
  525. maybeadjustresult(list,op,size,dst);
  526. end
  527. else if op in [OP_SAR,OP_SHL,OP_SHR] then
  528. begin
  529. if op=OP_SHL then
  530. list.concat(taicpu.op_reg(A_SSL,src1))
  531. else
  532. list.concat(taicpu.op_reg(A_SSR,src1));
  533. list.concat(taicpu.op_reg_reg(TOpCG2AsmOp[op],dst,src2));
  534. maybeadjustresult(list,op,size,dst);
  535. end
  536. else
  537. case op of
  538. OP_MOVE:
  539. a_load_reg_reg(list,size,size,src1,dst);
  540. else
  541. begin
  542. list.concat(taicpu.op_reg_reg_reg(TOpCG2AsmOp[op],dst,src2,src1));
  543. maybeadjustresult(list,op,size,dst);
  544. end;
  545. end;
  546. end;
  547. procedure tcgcpu.a_call_name(list : TAsmList; const s : string;
  548. weak : boolean);
  549. begin
  550. if not weak then
  551. list.concat(taicpu.op_sym(txtensaprocinfo(current_procinfo).callins,current_asmdata.RefAsmSymbol(s,AT_FUNCTION)))
  552. else
  553. list.concat(taicpu.op_sym(txtensaprocinfo(current_procinfo).callins,current_asmdata.WeakRefAsmSymbol(s,AT_FUNCTION)));
  554. end;
  555. procedure tcgcpu.a_call_reg(list : TAsmList; Reg : tregister);
  556. begin
  557. list.concat(taicpu.op_reg(txtensaprocinfo(current_procinfo).callxins,reg));
  558. end;
  559. procedure tcgcpu.a_jmp_name(list : TAsmList; const s : string);
  560. var
  561. ai : taicpu;
  562. tmpreg: TRegister;
  563. begin
  564. { for now, we use A15 here, however, this is not save as it might contain an argument }
  565. ai:=TAiCpu.op_sym_reg(A_J,current_asmdata.RefAsmSymbol(s,AT_FUNCTION),NR_A15);
  566. ai.oppostfix := PF_L; // if destination is too far for J then assembler can convert to JX
  567. ai.is_jmp:=true;
  568. list.Concat(ai);
  569. end;
  570. procedure tcgcpu.a_jmp_flags(list: TAsmList; const f: TResFlags; l: tasmlabel);
  571. var
  572. instr: taicpu;
  573. begin
  574. if CPUXTENSA_HAS_BOOLEAN_OPTION in cpu_capabilities[current_settings.cputype] then
  575. begin
  576. instr:=taicpu.op_reg_sym(A_B,f.register,l);
  577. instr.condition:=flags_to_cond(f.flag);
  578. instr.is_jmp:=true;
  579. list.concat(instr);
  580. end
  581. else
  582. Internalerror(2020070401);
  583. end;
  584. procedure tcgcpu.g_proc_entry(list : TAsmList; localsize : longint;
  585. nostackframe : boolean);
  586. var
  587. ref : treference;
  588. r : byte;
  589. regs : tcpuregisterset;
  590. stackmisalignment : pint;
  591. regoffset : LongInt;
  592. stack_parameters : Boolean;
  593. registerarea : PtrInt;
  594. l : TAsmLabel;
  595. begin
  596. LocalSize:=align(LocalSize,4);
  597. stack_parameters:=current_procinfo.procdef.stack_tainting_parameter(calleeside);
  598. { call instruction does not put anything on the stack }
  599. registerarea:=0;
  600. if not(nostackframe) then
  601. begin
  602. regs:=rg[R_INTREGISTER].used_in_proc-paramanager.get_volatile_registers_int(pocall_stdcall);
  603. a_reg_alloc(list,NR_STACK_POINTER_REG);
  604. case target_info.abi of
  605. abi_xtensa_call0:
  606. begin
  607. list.concat(tai_comment.Create(strpnew(' Start of abi_call0 entry localsize='+tostr(localsize))));
  608. if current_procinfo.framepointer<>NR_STACK_POINTER_REG then
  609. Include(regs,RS_A15);
  610. if pi_do_call in current_procinfo.flags then
  611. Include(regs,RS_A0);
  612. if regs<>[] then
  613. begin
  614. for r:=RS_A0 to RS_A15 do
  615. if r in regs then
  616. inc(registerarea,4);
  617. end;
  618. if stack_parameters and (pi_estimatestacksize in current_procinfo.flags) then
  619. begin
  620. list.concat(tai_comment.Create(strpnew('Stackframe size was estimated before code generation due to stack parameters')));
  621. list.concat(tai_comment.Create(strpnew(' Calculated stackframe size: '+tostr(txtensaprocinfo(current_procinfo).stackframesize))));
  622. list.concat(tai_comment.Create(strpnew(' Max. outgoing parameter size: '+tostr(txtensaprocinfo(current_procinfo).maxpushedparasize))));
  623. list.concat(tai_comment.Create(strpnew(' End of last temporary location: '+tostr(tg.lasttemp))));
  624. list.concat(tai_comment.Create(strpnew(' Size of register area: '+tostr(registerarea))));
  625. list.concat(tai_comment.Create(strpnew(' Required size after code generation: '+tostr(localsize))));
  626. if localsize>txtensaprocinfo(current_procinfo).stackframesize then
  627. internalerror(2020091001);
  628. localsize:=txtensaprocinfo(current_procinfo).stackframesize;
  629. end
  630. else
  631. begin
  632. inc(localsize,registerarea);
  633. localsize:=align(localsize,current_settings.alignment.localalignmax);
  634. end;
  635. if LocalSize<>0 then
  636. begin
  637. a_reg_alloc(list,NR_STACK_POINTER_REG);
  638. { not sure if 32512 is the correct value or if it can be larger }
  639. if Localsize>32512 then
  640. begin
  641. reference_reset(ref,4,[]);
  642. ref.symbol:=create_data_entry(nil,-localsize);
  643. list.concat(tai_comment.Create(strpnew(' Decreasing stack pointer by localsize='+tostr(localsize)+' using A8 register')));
  644. list.concat(taicpu.op_reg_ref(A_L32R,NR_A8,ref));
  645. list.concat(taicpu.op_reg_reg_reg(A_ADD,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,NR_A8));
  646. end
  647. else
  648. begin
  649. list.concat(tai_comment.Create(strpnew(' Decreasing stack pointer by localsize='+tostr(localsize)+' using A8 register')));
  650. list.concat(taicpu.op_reg_reg_const(A_ADDI,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,-localsize));
  651. end;
  652. end;
  653. reference_reset(ref,4,[]);
  654. ref.base:=NR_STACK_POINTER_REG;
  655. ref.offset:=localsize;
  656. if localsize>1024 then
  657. begin
  658. list.concat(tai_comment.Create(strpnew(' Special entry code of abi_xtensa_call0 entry localsize='+tostr(localsize))));
  659. if localsize<=1024+32512 then
  660. begin
  661. list.concat(taicpu.op_reg_reg_const(A_ADDI,NR_A8,NR_STACK_POINTER_REG,localsize-registerarea));
  662. reference_reset(ref,4,[]);
  663. ref.base:=NR_A8;
  664. ref.offset:=registerarea;
  665. end
  666. else
  667. begin
  668. reference_reset(ref,4,[]);
  669. ref.symbol:=create_data_entry(nil,localsize-registerarea);
  670. list.concat(taicpu.op_reg_ref(A_L32R,NR_A8,ref));
  671. list.concat(taicpu.op_reg_reg_reg(A_ADD,NR_A8,NR_A8,NR_STACK_POINTER_REG));
  672. reference_reset(ref,4,[]);
  673. ref.base:=NR_A8;
  674. ref.offset:=registerarea;
  675. end;
  676. end;
  677. if current_procinfo.framepointer<>NR_STACK_POINTER_REG then
  678. begin
  679. dec(ref.offset,4);
  680. list.concat(tai_comment.Create(strpnew(' Storing reg A15 at offset='+tostr(ref.offset))));
  681. list.concat(taicpu.op_reg_ref(A_S32I,NR_A15,ref));
  682. a_reg_alloc(list,NR_FRAME_POINTER_REG);
  683. list.concat(taicpu.op_reg_reg(A_MOV,NR_A15,NR_STACK_POINTER_REG));
  684. end;
  685. if regs<>[] then
  686. begin
  687. for r:=RS_A14 downto RS_A0 do
  688. if r in regs then
  689. begin
  690. dec(ref.offset,4);
  691. list.concat(tai_comment.Create(strpnew(' Storing reg '+std_regname(newreg(R_INTREGISTER,r,R_SUBWHOLE))+' at offset='+tostr(ref.offset))));
  692. list.concat(taicpu.op_reg_ref(A_S32I,newreg(R_INTREGISTER,r,R_SUBWHOLE),ref));
  693. end;
  694. end;
  695. end;
  696. abi_xtensa_windowed:
  697. begin
  698. list.concat(tai_comment.Create(strpnew(' Start of abi_windowed entry localsize='+tostr(localsize))));
  699. if stack_parameters and (pi_estimatestacksize in current_procinfo.flags) then
  700. begin
  701. list.concat(tai_comment.Create(strpnew('Stackframe size was estimated before code generation due to stack parameters')));
  702. list.concat(tai_comment.Create(strpnew(' Calculated stackframe size: '+tostr(txtensaprocinfo(current_procinfo).stackframesize))));
  703. list.concat(tai_comment.Create(strpnew(' Max. outgoing parameter size: '+tostr(txtensaprocinfo(current_procinfo).maxpushedparasize))));
  704. list.concat(tai_comment.Create(strpnew(' End of last temporary location: '+tostr(tg.lasttemp))));
  705. list.concat(tai_comment.Create(strpnew(' Max. window rotation in bytes: '+tostr(txtensaprocinfo(current_procinfo).maxcall*4))));
  706. list.concat(tai_comment.Create(strpnew(' Required size after code generation: '+tostr(localsize))));
  707. { should never happen as localsize is derived from
  708. txtensaprocinfo(current_procinfo).stackframesize }
  709. if localsize>txtensaprocinfo(current_procinfo).stackframesize then
  710. internalerror(2020031402);
  711. localsize:=txtensaprocinfo(current_procinfo).stackframesize;
  712. end
  713. else
  714. begin
  715. localsize:=align(localsize,current_settings.alignment.localalignmax);
  716. inc(localsize,4*4);
  717. if pi_do_call in current_procinfo.flags then
  718. inc(localsize,txtensaprocinfo(current_procinfo).maxcall*4);
  719. end;
  720. if localsize<0 then
  721. Internalerror(2020083001);
  722. if localsize>32760 then
  723. begin
  724. list.concat(taicpu.op_reg_const(A_ENTRY,NR_STACK_POINTER_REG,32));
  725. reference_reset(ref,4,[]);
  726. ref.symbol:=create_data_entry(nil,longint(localsize-32));
  727. list.concat(taicpu.op_reg_ref(A_L32R,NR_A8,ref));
  728. list.concat(taicpu.op_reg_reg_reg(A_SUB,NR_A8,NR_STACK_POINTER_REG,NR_A8));
  729. list.concat(taicpu.op_reg_reg(A_MOVSP,NR_STACK_POINTER_REG,NR_A8));
  730. end
  731. else
  732. list.concat(taicpu.op_reg_const(A_ENTRY,NR_STACK_POINTER_REG,localsize));
  733. end;
  734. else
  735. Internalerror(2020031401);
  736. end;
  737. end;
  738. end;
  739. procedure tcgcpu.g_proc_exit(list : TAsmList; parasize : longint;
  740. nostackframe : boolean);
  741. var
  742. ref : treference;
  743. r : byte;
  744. regs : tcpuregisterset;
  745. stackmisalignment : pint;
  746. regoffset : LongInt;
  747. stack_parameters : Boolean;
  748. registerarea : PtrInt;
  749. l : TAsmLabel;
  750. LocalSize: longint;
  751. begin
  752. case target_info.abi of
  753. abi_xtensa_windowed:
  754. list.Concat(taicpu.op_none(A_RETW));
  755. abi_xtensa_call0:
  756. begin
  757. if not(nostackframe) then
  758. begin
  759. LocalSize:=current_procinfo.calc_stackframe_size;
  760. LocalSize:=align(LocalSize,4);
  761. stack_parameters:=current_procinfo.procdef.stack_tainting_parameter(calleeside);
  762. registerarea:=0;
  763. regs:=rg[R_INTREGISTER].used_in_proc-paramanager.get_volatile_registers_int(pocall_stdcall);
  764. if current_procinfo.framepointer<>NR_STACK_POINTER_REG then
  765. Include(regs,RS_A15);
  766. if pi_do_call in current_procinfo.flags then
  767. Include(regs,RS_A0);
  768. if regs<>[] then
  769. begin
  770. for r:=RS_A0 to RS_A15 do
  771. if r in regs then
  772. inc(registerarea,4);
  773. end;
  774. { do we use then estimated stack size? }
  775. if not(stack_parameters and (pi_estimatestacksize in current_procinfo.flags)) then
  776. begin
  777. inc(localsize,registerarea);
  778. localsize:=align(localsize,current_settings.alignment.localalignmax);
  779. end;
  780. if LocalSize<>0 then
  781. begin
  782. // Determine reference mode required to access stack
  783. reference_reset(ref,4,[]);
  784. ref.base:=NR_STACK_POINTER_REG;
  785. ref.offset:=localsize;
  786. if localsize>1024 then
  787. begin
  788. if localsize<=1024+32512 then
  789. begin
  790. list.concat(taicpu.op_reg_reg_const(A_ADDI,NR_A8,NR_STACK_POINTER_REG,ref.offset-registerarea));
  791. ref.offset:=registerarea;
  792. ref.base:=NR_A8;
  793. end
  794. else
  795. begin
  796. reference_reset(ref,4,[]);
  797. ref.symbol:=create_data_entry(nil,ref.offset-registerarea);
  798. list.concat(taicpu.op_reg_ref(A_L32R,NR_A8,ref));
  799. list.concat(taicpu.op_reg_reg_reg(A_ADD,NR_A8,NR_A8,NR_STACK_POINTER_REG));
  800. reference_reset(ref,4,[]);
  801. ref.base:=NR_A8;
  802. ref.offset:=registerarea;
  803. end;
  804. end;
  805. // restore a15 if used
  806. if current_procinfo.framepointer<>NR_STACK_POINTER_REG then
  807. begin
  808. dec(ref.offset,4);
  809. list.concat(tai_comment.Create(strpnew(' Restoring reg A15 from offset='+tostr(ref.offset))));
  810. list.concat(taicpu.op_reg_ref(A_L32I,NR_A15,ref));
  811. a_reg_dealloc(list,NR_FRAME_POINTER_REG);
  812. end;
  813. // restore rest of registers
  814. if regs<>[] then
  815. begin
  816. for r:=RS_A14 downto RS_A0 do
  817. if r in regs then
  818. begin
  819. dec(ref.offset,4);
  820. list.concat(tai_comment.Create(strpnew(' Restoring reg '+std_regname(newreg(R_INTREGISTER,r,R_SUBWHOLE))+' from offset='+tostr(ref.offset))));
  821. list.concat(taicpu.op_reg_ref(A_L32I,newreg(R_INTREGISTER,r,R_SUBWHOLE),ref));
  822. end;
  823. end;
  824. // restore stack pointer
  825. { not sure if 32512 is the correct value or if it can be larger }
  826. list.concat(tai_comment.Create(strpnew(' Restoring stack pointer')));
  827. if Localsize>32512 then
  828. begin
  829. reference_reset(ref,4,[]);
  830. ref.symbol:=create_data_entry(nil,localsize);
  831. list.concat(taicpu.op_reg_ref(A_L32R,NR_A8,ref));
  832. list.concat(taicpu.op_reg_reg_reg(A_ADD,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,NR_A8));
  833. end
  834. else
  835. list.concat(taicpu.op_reg_reg_const(A_ADDI,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,localsize));
  836. a_reg_dealloc(list,NR_STACK_POINTER_REG);
  837. end;
  838. end;
  839. list.Concat(taicpu.op_none(A_RET));
  840. end
  841. else
  842. Internalerror(2020031403);
  843. end;
  844. end;
  845. procedure tcgcpu.a_cmp_const_reg_label(list: TAsmList; size: tcgsize; cmp_op: topcmp; a: tcgint; reg: tregister; l: tasmlabel);
  846. function is_b4const(v: tcgint): boolean;
  847. begin
  848. case v of
  849. -1,1,2,3,4,5,6,7,8,
  850. 10,12,16,32,64,128,256:
  851. result:=true;
  852. else
  853. result:=false;
  854. end;
  855. end;
  856. function is_b4constu(v: tcgint): boolean;
  857. begin
  858. case v of
  859. 32768,65536,
  860. 2,3,4,5,6,7,8,
  861. 10,12,16,32,64,128,256:
  862. result:=true;
  863. else
  864. result:=false;
  865. end;
  866. end;
  867. var
  868. op: TAsmCond;
  869. instr: taicpu;
  870. begin
  871. if (a=0) and (cmp_op in [OC_EQ,OC_NE,OC_LT,OC_GTE]) then
  872. begin
  873. case cmp_op of
  874. OC_EQ: op:=C_EQZ;
  875. OC_NE: op:=C_NEZ;
  876. OC_LT: op:=C_LTZ;
  877. OC_GTE: op:=C_GEZ;
  878. else
  879. Internalerror(2020030806);
  880. end;
  881. instr:=taicpu.op_reg_sym(A_B,reg,l);
  882. instr.condition:=op;
  883. instr.is_jmp:=true;
  884. list.concat(instr);
  885. end
  886. else if is_b4const(a) and
  887. (cmp_op in [OC_EQ,OC_NE,OC_LT,OC_GTE]) then
  888. begin
  889. case cmp_op of
  890. OC_EQ: op:=C_EQI;
  891. OC_NE: op:=C_NEI;
  892. OC_LT: op:=C_LTI;
  893. OC_GTE: op:=C_GEI;
  894. else
  895. Internalerror(2020030807);
  896. end;
  897. instr:=taicpu.op_reg_const_sym(A_B,reg,a,l);
  898. instr.condition:=op;
  899. instr.is_jmp:=true;
  900. list.concat(instr);
  901. end
  902. else if is_b4constu(a) and
  903. (cmp_op in [OC_B,OC_AE]) then
  904. begin
  905. case cmp_op of
  906. OC_B: op:=C_LTUI;
  907. OC_AE: op:=C_GEUI;
  908. else
  909. Internalerror(2020030808);
  910. end;
  911. instr:=taicpu.op_reg_const_sym(A_B,reg,a,l);
  912. instr.condition:=op;
  913. instr.is_jmp:=true;
  914. list.concat(instr);
  915. end
  916. else
  917. inherited a_cmp_const_reg_label(list, size, cmp_op, a, reg, l);
  918. end;
  919. procedure tcgcpu.a_cmp_reg_reg_label(list : TAsmList; size : tcgsize;
  920. cmp_op : topcmp; reg1,reg2 : tregister; l : tasmlabel);
  921. var
  922. tmpreg: TRegister;
  923. instr: taicpu;
  924. begin
  925. if TOpCmp2AsmCond[cmp_op]=C_None then
  926. begin
  927. cmp_op:=swap_opcmp(cmp_op);
  928. tmpreg:=reg1;
  929. reg1:=reg2;
  930. reg2:=tmpreg;
  931. end;
  932. instr:=taicpu.op_reg_reg_sym(A_B,reg2,reg1,l);
  933. instr.condition:=TOpCmp2AsmCond[cmp_op];
  934. instr.is_jmp:=true;
  935. list.concat(instr);
  936. end;
  937. procedure tcgcpu.a_jmp_always(list : TAsmList; l : TAsmLabel);
  938. var
  939. ai : taicpu;
  940. begin
  941. if l.bind in [AB_GLOBAL] then
  942. begin
  943. { for now, we use A15 here, however, this is not save as it might contain an argument, I have not figured out a
  944. solution yet }
  945. ai:=taicpu.op_sym_reg(A_J,l,NR_A15);
  946. ai.oppostfix := PF_L;
  947. end
  948. else
  949. ai:=taicpu.op_sym(A_J,l);
  950. ai.is_jmp:=true;
  951. list.concat(ai);
  952. end;
  953. procedure tcgcpu.g_flags2reg(list: TAsmList; size: TCgSize; const f: tresflags; reg: TRegister);
  954. var
  955. hregister: TRegister;
  956. instr: taicpu;
  957. begin
  958. a_load_const_reg(list,size,0,reg);
  959. hregister:=getintregister(list,size);
  960. a_load_const_reg(list,size,1,hregister);
  961. instr:=taicpu.op_reg_reg_reg(A_MOV,reg,hregister,f.register);
  962. instr.condition:=flags_to_cond(f.flag);
  963. list.concat(instr);
  964. end;
  965. procedure tcgcpu.g_concatcopy_move(list: tasmlist; const Source, dest: treference; len: tcgint);
  966. var
  967. paraloc1, paraloc2, paraloc3: TCGPara;
  968. pd: tprocdef;
  969. begin
  970. pd:=search_system_proc('MOVE');
  971. paraloc1.init;
  972. paraloc2.init;
  973. paraloc3.init;
  974. paramanager.getcgtempparaloc(list, pd, 1, paraloc1);
  975. paramanager.getcgtempparaloc(list, pd, 2, paraloc2);
  976. paramanager.getcgtempparaloc(list, pd, 3, paraloc3);
  977. a_load_const_cgpara(list, OS_SINT, len, paraloc3);
  978. a_loadaddr_ref_cgpara(list, dest, paraloc2);
  979. a_loadaddr_ref_cgpara(list, Source, paraloc1);
  980. paramanager.freecgpara(list, paraloc3);
  981. paramanager.freecgpara(list, paraloc2);
  982. paramanager.freecgpara(list, paraloc1);
  983. alloccpuregisters(list, R_INTREGISTER, paramanager.get_volatile_registers_int(pocall_default));
  984. alloccpuregisters(list, R_FPUREGISTER, paramanager.get_volatile_registers_fpu(pocall_default));
  985. a_call_name(list, 'FPC_MOVE', false);
  986. dealloccpuregisters(list, R_FPUREGISTER, paramanager.get_volatile_registers_fpu(pocall_default));
  987. dealloccpuregisters(list, R_INTREGISTER, paramanager.get_volatile_registers_int(pocall_default));
  988. paraloc3.done;
  989. paraloc2.done;
  990. paraloc1.done;
  991. end;
  992. procedure tcgcpu.g_concatcopy(list : TAsmList;const source,dest : treference;len : tcgint);
  993. var
  994. tmpreg1, hreg, countreg: TRegister;
  995. src, dst, src2, dst2: TReference;
  996. lab: tasmlabel;
  997. Count, count2: aint;
  998. function reference_is_reusable(const ref: treference): boolean;
  999. begin
  1000. result:=(ref.base<>NR_NO) and (ref.index=NR_NO) and
  1001. (ref.symbol=nil);
  1002. end;
  1003. begin
  1004. src2:=source;
  1005. fixref(list,src2);
  1006. dst2:=dest;
  1007. fixref(list,dst2);
  1008. if len > high(longint) then
  1009. internalerror(2002072704);
  1010. { A call (to FPC_MOVE) requires the outgoing parameter area to be properly
  1011. allocated on stack. This can only be done before tmipsprocinfo.set_first_temp_offset,
  1012. i.e. before secondpass. Other internal procedures request correct stack frame
  1013. by setting pi_do_call during firstpass, but for this particular one it is impossible.
  1014. Therefore, if the current procedure is a leaf one, we have to leave it that way. }
  1015. { anybody wants to determine a good value here :)? }
  1016. if (len > 100) and
  1017. assigned(current_procinfo) and
  1018. (pi_do_call in current_procinfo.flags) then
  1019. g_concatcopy_move(list, src2, dst2, len)
  1020. else
  1021. begin
  1022. Count := len div 4;
  1023. if (count<=4) and reference_is_reusable(src2) then
  1024. src:=src2
  1025. else
  1026. begin
  1027. reference_reset(src,sizeof(aint),[]);
  1028. { load the address of src2 into src.base }
  1029. src.base := GetAddressRegister(list);
  1030. a_loadaddr_ref_reg(list, src2, src.base);
  1031. end;
  1032. if (count<=4) and reference_is_reusable(dst2) then
  1033. dst:=dst2
  1034. else
  1035. begin
  1036. reference_reset(dst,sizeof(aint),[]);
  1037. { load the address of dst2 into dst.base }
  1038. dst.base := GetAddressRegister(list);
  1039. a_loadaddr_ref_reg(list, dst2, dst.base);
  1040. end;
  1041. { generate a loop }
  1042. if Count > 4 then
  1043. begin
  1044. countreg := GetIntRegister(list, OS_INT);
  1045. tmpreg1 := GetIntRegister(list, OS_INT);
  1046. a_load_const_reg(list, OS_INT, Count, countreg);
  1047. current_asmdata.getjumplabel(lab);
  1048. if CPUXTENSA_HAS_LOOPS in cpu_capabilities[current_settings.cputype] then
  1049. begin
  1050. list.concat(taicpu.op_reg_sym(A_LOOP, countreg, lab));
  1051. list.concat(taicpu.op_reg_ref(A_L32I, tmpreg1, src));
  1052. list.concat(taicpu.op_reg_ref(A_S32I, tmpreg1, dst));
  1053. list.concat(taicpu.op_reg_reg_const(A_ADDI, src.base, src.base, 4));
  1054. list.concat(taicpu.op_reg_reg_const(A_ADDI, dst.base, dst.base, 4));
  1055. a_label(list, lab);
  1056. end
  1057. else
  1058. begin
  1059. a_label(list, lab);
  1060. list.concat(taicpu.op_reg_ref(A_L32I, tmpreg1, src));
  1061. list.concat(taicpu.op_reg_ref(A_S32I, tmpreg1, dst));
  1062. list.concat(taicpu.op_reg_reg_const(A_ADDI, src.base, src.base, 4));
  1063. list.concat(taicpu.op_reg_reg_const(A_ADDI, dst.base, dst.base, 4));
  1064. list.concat(taicpu.op_reg_reg_const(A_ADDI, countreg, countreg, -1));
  1065. a_cmp_const_reg_label(list,OS_INT,OC_NE,0,countreg,lab);
  1066. { keep the registers alive }
  1067. list.concat(taicpu.op_reg_reg(A_MOV,countreg,countreg));
  1068. end;
  1069. { keep the registers alive }
  1070. list.concat(taicpu.op_reg_reg(A_MOV,src.base,src.base));
  1071. list.concat(taicpu.op_reg_reg(A_MOV,dst.base,dst.base));
  1072. len := len mod 4;
  1073. end;
  1074. { unrolled loop }
  1075. Count := len div 4;
  1076. if Count > 0 then
  1077. begin
  1078. tmpreg1 := GetIntRegister(list, OS_INT);
  1079. for count2 := 1 to Count do
  1080. begin
  1081. list.concat(taicpu.op_reg_ref(A_L32I, tmpreg1, src));
  1082. list.concat(taicpu.op_reg_ref(A_S32I, tmpreg1, dst));
  1083. Inc(src.offset, 4);
  1084. Inc(dst.offset, 4);
  1085. end;
  1086. len := len mod 4;
  1087. end;
  1088. if (len and 4) <> 0 then
  1089. begin
  1090. hreg := GetIntRegister(list, OS_INT);
  1091. a_load_ref_reg(list, OS_32, OS_32, src, hreg);
  1092. a_load_reg_ref(list, OS_32, OS_32, hreg, dst);
  1093. Inc(src.offset, 4);
  1094. Inc(dst.offset, 4);
  1095. end;
  1096. { copy the leftovers }
  1097. if (len and 2) <> 0 then
  1098. begin
  1099. hreg := GetIntRegister(list, OS_INT);
  1100. a_load_ref_reg(list, OS_16, OS_16, src, hreg);
  1101. a_load_reg_ref(list, OS_16, OS_16, hreg, dst);
  1102. Inc(src.offset, 2);
  1103. Inc(dst.offset, 2);
  1104. end;
  1105. if (len and 1) <> 0 then
  1106. begin
  1107. hreg := GetIntRegister(list, OS_INT);
  1108. a_load_ref_reg(list, OS_8, OS_8, src, hreg);
  1109. a_load_reg_ref(list, OS_8, OS_8, hreg, dst);
  1110. end;
  1111. end;
  1112. end;
  1113. procedure tcgcpu.a_loadfpu_reg_reg(list: TAsmList; fromsize,tosize: tcgsize; reg1, reg2: tregister);
  1114. var
  1115. ai: taicpu;
  1116. begin
  1117. if not(fromsize in [OS_32,OS_F32]) then
  1118. InternalError(2020032603);
  1119. ai := taicpu.op_reg_reg(A_MOV,reg2,reg1);
  1120. ai.oppostfix := PF_S;
  1121. list.concat(ai);
  1122. end;
  1123. procedure tcgcpu.a_loadfpu_ref_reg(list: TAsmList; fromsize,tosize: tcgsize; const ref: treference; reg: tregister);
  1124. var
  1125. href: treference;
  1126. begin
  1127. if not(fromsize in [OS_32,OS_F32]) then
  1128. InternalError(2020032602);
  1129. href:=ref;
  1130. if assigned(href.symbol) or
  1131. ((href.index<>NR_NO) and (href.offset<>0)) or
  1132. (((href.offset<0) or (href.offset>1020) or (href.offset mod 4<>0))) then
  1133. fixref(list,href);
  1134. if (href.base<>NR_NO) and (href.index<>NR_NO) then
  1135. list.concat(taicpu.op_reg_ref(A_LSX,reg,href))
  1136. else
  1137. list.concat(taicpu.op_reg_ref(A_LSI,reg,href));
  1138. if fromsize<>tosize then
  1139. a_loadfpu_reg_reg(list,fromsize,tosize,reg,reg);
  1140. end;
  1141. procedure tcgcpu.a_loadfpu_reg_ref(list: TAsmList; fromsize, tosize: tcgsize; reg: tregister; const ref: treference);
  1142. var
  1143. href: treference;
  1144. begin
  1145. if not(fromsize in [OS_32,OS_F32]) then
  1146. InternalError(2020032604);
  1147. href:=ref;
  1148. if assigned(href.symbol) or
  1149. ((href.index<>NR_NO) and (href.offset<>0)) or
  1150. (((href.offset<0) or (href.offset>1020) or (href.offset mod 4<>0))) then
  1151. fixref(list,href);
  1152. if (href.base<>NR_NO) and (href.index<>NR_NO) then
  1153. list.concat(taicpu.op_reg_ref(A_SSX,reg,href))
  1154. else
  1155. list.concat(taicpu.op_reg_ref(A_SSI,reg,href));
  1156. end;
  1157. procedure tcgcpu.a_loadfpu_intreg_reg(list : TAsmList; fromsize,tosize : tcgsize; intreg,fpureg : tregister);
  1158. begin
  1159. if not(tcgsize2size[fromsize]=4) or
  1160. not(tcgsize2size[tosize]=4) then
  1161. internalerror(2020091102);
  1162. list.concat(taicpu.op_reg_reg(A_WFR,fpureg,intreg));
  1163. end;
  1164. procedure tcgcpu.a_loadfpu_reg_intreg(list : TAsmList; fromsize,tosize : tcgsize; fpureg,intreg : tregister);
  1165. begin
  1166. if not(tcgsize2size[fromsize]=4) or
  1167. not(tcgsize2size[tosize]=4) then
  1168. internalerror(2020091202);
  1169. list.concat(taicpu.op_reg_reg(A_RFR,intreg,fpureg));
  1170. end;
  1171. procedure tcgcpu.maybeadjustresult(list : TAsmList; op : TOpCg; size : tcgsize; dst : tregister);
  1172. const
  1173. overflowops = [OP_MUL,OP_SHL,OP_ADD,OP_SUB,OP_NEG];
  1174. begin
  1175. if (op in overflowops) and
  1176. (size in [OS_8,OS_S8,OS_16,OS_S16]) then
  1177. a_load_reg_reg(list,OS_32,size,dst,dst);
  1178. end;
  1179. procedure tcgcpu.g_overflowcheck(list: TAsmList; const Loc: tlocation; def: tdef);
  1180. begin
  1181. { no overflow checking yet }
  1182. end;
  1183. function tcgcpu.create_data_entry(symbol: TAsmSymbol;offset: asizeint): TAsmLabel;
  1184. var
  1185. hp: tai;
  1186. begin
  1187. hp:=tai(current_procinfo.aktlocaldata.first);
  1188. while assigned(hp) do
  1189. begin
  1190. if (hp.typ=ait_label) and assigned(hp.Next) and
  1191. (tai(hp.Next).typ=ait_const) and
  1192. (tai_const(hp.Next).consttype=aitconst_ptr) and
  1193. (tai_const(hp.Next).sym=symbol) and
  1194. (tai_const(hp.Next).endsym=nil) and
  1195. ((assigned(symbol) and (tai_const(hp.Next).symofs=offset)) or
  1196. (not(assigned(symbol)) and (tai_const(hp.Next).value=offset))
  1197. ) then
  1198. begin
  1199. Result:=tai_label(hp).labsym;
  1200. exit;
  1201. end;
  1202. hp:=tai(hp.Next);
  1203. end;
  1204. current_asmdata.getjumplabel(Result);
  1205. cg.a_label(current_procinfo.aktlocaldata,Result);
  1206. if assigned(symbol) then
  1207. current_procinfo.aktlocaldata.concat(tai_const.create_sym_offset(symbol,offset))
  1208. else
  1209. current_procinfo.aktlocaldata.concat(tai_const.Create_32bit(offset));
  1210. end;
  1211. procedure tcgcpu.a_bit_scan_reg_reg(list: TAsmList; reverse: boolean; srcsize, dstsize: TCGSize; src, dst: TRegister);
  1212. var
  1213. ai: taicpu;
  1214. tmpreg: TRegister;
  1215. begin
  1216. if reverse then
  1217. begin
  1218. list.Concat(taicpu.op_reg_reg(A_NSAU,dst,src));
  1219. tmpreg:=getintregister(list,OS_INT);
  1220. a_load_const_reg(list,OS_INT,31,tmpreg);
  1221. a_op_reg_reg_reg(list,OP_SUB,OS_INT,dst,tmpreg,dst);
  1222. tmpreg:=getintregister(list,OS_INT);
  1223. a_load_const_reg(list,OS_INT,255,tmpreg);
  1224. ai:=taicpu.op_reg_reg_reg(A_MOV,dst,tmpreg,src);
  1225. ai.condition:=C_EQZ;
  1226. list.Concat(ai);
  1227. end
  1228. else
  1229. Internalerror(2020092604);
  1230. end;
  1231. procedure tcg64fxtensa.a_op64_reg_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64);
  1232. var
  1233. instr: taicpu;
  1234. no_carry: TAsmLabel;
  1235. tmpreg: TRegister;
  1236. begin
  1237. case op of
  1238. OP_NEG,
  1239. OP_NOT :
  1240. internalerror(2020030810);
  1241. else
  1242. ;
  1243. end;
  1244. case op of
  1245. OP_AND,OP_OR,OP_XOR:
  1246. begin
  1247. cg.a_op_reg_reg_reg(list,op,OS_32,regsrc1.reglo,regsrc2.reglo,regdst.reglo);
  1248. cg.a_op_reg_reg_reg(list,op,OS_32,regsrc1.reghi,regsrc2.reghi,regdst.reghi);
  1249. end;
  1250. OP_ADD:
  1251. begin
  1252. if (regsrc1.reglo=regdst.reglo) or (regsrc1.reghi=regdst.reghi) then
  1253. Internalerror(2020082205);
  1254. list.concat(taicpu.op_reg_reg_reg(A_ADD, regdst.reglo, regsrc2.reglo, regsrc1.reglo));
  1255. list.concat(taicpu.op_reg_reg_reg(A_ADD, regdst.reghi, regsrc2.reghi, regsrc1.reghi));
  1256. current_asmdata.getjumplabel(no_carry);
  1257. cg.a_cmp_reg_reg_label(list,OS_INT,OC_AE, regsrc1.reglo, regdst.reglo, no_carry);
  1258. list.concat(taicpu.op_reg_reg_const(A_ADDI, regdst.reghi, regdst.reghi, 1));
  1259. cg.a_label(list,no_carry);
  1260. end;
  1261. OP_SUB:
  1262. begin
  1263. if (regsrc1.reglo=regdst.reglo) or (regsrc1.reghi=regdst.reghi) then
  1264. Internalerror(2020082206);
  1265. { we need the original src2 value for the comparison, do not overwrite it }
  1266. if regsrc2.reglo=regdst.reglo then
  1267. begin
  1268. tmpreg:=cg.GetIntRegister(list,OS_S32);
  1269. cg.a_load_reg_reg(list,OS_INT,OS_INT,regsrc2.reglo,tmpreg);
  1270. regsrc2.reglo:=tmpreg;
  1271. end;
  1272. list.concat(taicpu.op_reg_reg_reg(A_SUB, regdst.reglo, regsrc2.reglo, regsrc1.reglo));
  1273. list.concat(taicpu.op_reg_reg_reg(A_SUB, regdst.reghi, regsrc2.reghi, regsrc1.reghi));
  1274. current_asmdata.getjumplabel(no_carry);
  1275. cg.a_cmp_reg_reg_label(list,OS_INT,OC_AE, regsrc1.reglo, regsrc2.reglo, no_carry);
  1276. list.concat(taicpu.op_reg_reg_const(A_ADDI, regdst.reghi, regdst.reghi, -1));
  1277. cg.a_label(list,no_carry);
  1278. end;
  1279. else
  1280. internalerror(2020030813);
  1281. end;
  1282. end;
  1283. procedure tcg64fxtensa.a_op64_reg_reg(list : TAsmList; op : TOpCG; size : tcgsize; regsrc,regdst : tregister64);
  1284. var
  1285. tmpreg : TRegister;
  1286. instr : taicpu;
  1287. begin
  1288. case op of
  1289. OP_NEG:
  1290. begin
  1291. tmpreg:=cg.GetIntRegister(list, OS_INT);
  1292. list.concat(taicpu.op_reg_reg(A_NEG,regdst.reglo,regsrc.reglo));
  1293. list.concat(taicpu.op_reg_reg(A_NEG,regdst.reghi,regsrc.reghi));
  1294. list.concat(taicpu.op_reg_reg_const(A_ADDI,tmpreg,regdst.reghi,-1));
  1295. instr:=taicpu.op_reg_reg_reg(A_MOV,regdst.reghi,tmpreg,regdst.reglo);
  1296. instr.condition:=C_NEZ;
  1297. list.concat(instr);
  1298. end;
  1299. OP_NOT:
  1300. begin
  1301. cg.a_op_reg_reg(list,OP_NOT,OS_INT,regsrc.reglo,regdst.reglo);
  1302. cg.a_op_reg_reg(list,OP_NOT,OS_INT,regsrc.reghi,regdst.reghi);
  1303. end;
  1304. else
  1305. a_op64_reg_reg_reg(list,op,size,regsrc,regdst,regdst);
  1306. end;
  1307. end;
  1308. procedure tcg64fxtensa.a_op64_const_reg_reg(list : TAsmList; op : TOpCG; size : tcgsize; value : int64; regsrc,regdst : tregister64);
  1309. var
  1310. tmpreg64 : tregister64;
  1311. no_carry : TAsmLabel;
  1312. tmpreg: tregister;
  1313. begin
  1314. case op of
  1315. OP_NEG,
  1316. OP_NOT :
  1317. internalerror(2020030904);
  1318. else
  1319. ;
  1320. end;
  1321. case op of
  1322. OP_AND,OP_OR,OP_XOR:
  1323. begin
  1324. cg.a_op_const_reg_reg(list,op,OS_32,aint(lo(value)),regsrc.reglo,regdst.reglo);
  1325. cg.a_op_const_reg_reg(list,op,OS_32,aint(hi(value)),regsrc.reghi,regdst.reghi);
  1326. end;
  1327. OP_ADD:
  1328. begin
  1329. { could do better here (hi(value) in 248..2047), for now we support only the simple cases }
  1330. if (value>=-2048) and (value<=2047) then
  1331. begin
  1332. { we need the original src value for the comparison, do not overwrite it }
  1333. if regsrc.reglo=regdst.reglo then
  1334. begin
  1335. tmpreg:=cg.GetIntRegister(list,OS_S32);
  1336. cg.a_load_reg_reg(list,OS_INT,OS_INT,regsrc.reglo,tmpreg);
  1337. regsrc.reglo:=tmpreg;
  1338. end;
  1339. list.concat(taicpu.op_reg_reg_const(A_ADDI, regdst.reglo, regsrc.reglo, value));
  1340. list.concat(taicpu.op_reg_reg(A_MOV, regdst.reghi, regsrc.reghi));
  1341. current_asmdata.getjumplabel(no_carry);
  1342. cg.a_cmp_reg_reg_label(list,OS_INT,OC_AE, regsrc.reglo, regdst.reglo, no_carry);
  1343. list.concat(taicpu.op_reg_reg_const(A_ADDI, regdst.reghi, regdst.reghi, 1));
  1344. cg.a_label(list,no_carry);
  1345. end
  1346. else
  1347. begin
  1348. tmpreg64.reglo := cg.GetIntRegister(list,OS_S32);
  1349. tmpreg64.reghi := cg.GetIntRegister(list,OS_S32);
  1350. a_load64_const_reg(list,value,tmpreg64);
  1351. a_op64_reg_reg_reg(list,op,size,tmpreg64,regsrc,regdst);
  1352. end;
  1353. end;
  1354. OP_SHL:
  1355. begin
  1356. if (value>0) and (value<=16) then
  1357. begin
  1358. tmpreg:=cg.GetIntRegister(list,OS_32);
  1359. list.concat(taicpu.op_reg_reg_const_const(A_EXTUI, tmpreg, regsrc.reglo, 32-value, value));
  1360. list.concat(taicpu.op_reg_reg_const(A_SLLI, regdst.reglo, regsrc.reglo, value));
  1361. list.concat(taicpu.op_reg_reg_const(A_SLLI, regdst.reghi, regsrc.reghi, value));
  1362. list.concat(taicpu.op_reg_reg_reg(A_OR, regdst.reghi, tmpreg, regdst.reghi));
  1363. end
  1364. else if value=32 then
  1365. begin
  1366. cg.a_load_reg_reg(list,OS_INT,OS_INT,regsrc.reglo,regdst.reghi);
  1367. cg.a_load_const_reg(list,OS_INT,0,regdst.reglo);
  1368. end
  1369. else
  1370. Internalerror(2020082209);
  1371. end;
  1372. OP_SHR:
  1373. begin
  1374. if (value>0) and (value<=15) then
  1375. begin
  1376. tmpreg:=cg.GetIntRegister(list,OS_32);
  1377. list.concat(taicpu.op_reg_reg_const(A_SLLI, tmpreg, regsrc.reghi, 32-value));
  1378. list.concat(taicpu.op_reg_reg_const(A_SRLI, regdst.reglo, regsrc.reglo, value));
  1379. list.concat(taicpu.op_reg_reg_reg(A_OR, regdst.reglo, tmpreg, regdst.reglo));
  1380. list.concat(taicpu.op_reg_reg_const(A_SRLI, regdst.reghi, regsrc.reghi, value));
  1381. end
  1382. else if value=32 then
  1383. begin
  1384. cg.a_load_reg_reg(list,OS_INT,OS_INT,regsrc.reghi,regdst.reglo);
  1385. cg.a_load_const_reg(list,OS_INT,0,regdst.reghi);
  1386. end
  1387. else
  1388. Internalerror(2020082210);
  1389. end;
  1390. OP_SUB:
  1391. begin
  1392. { for now, we take the simple approach }
  1393. tmpreg64.reglo := cg.GetIntRegister(list,OS_S32);
  1394. tmpreg64.reghi := cg.GetIntRegister(list,OS_S32);
  1395. a_load64_const_reg(list,value,tmpreg64);
  1396. a_op64_reg_reg_reg(list,op,size,tmpreg64,regsrc,regdst);
  1397. end;
  1398. else
  1399. internalerror(2020030901);
  1400. end;
  1401. end;
  1402. procedure tcg64fxtensa.a_op64_const_reg(list : TAsmList; op : TOpCG; size : tcgsize; value : int64; reg : tregister64);
  1403. begin
  1404. a_op64_const_reg_reg(list,op,size,value,reg,reg);
  1405. end;
  1406. {$warnings off}
  1407. procedure create_codegen;
  1408. begin
  1409. cg:=tcgcpu.Create;
  1410. cg64:=tcg64fxtensa.Create;
  1411. end;
  1412. end.