cgcpu.pas 61 KB

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