cgcpu.pas 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994
  1. {
  2. Copyright (c) 1998-2012 by Florian Klaempfl and David Zhang
  3. This unit implements the code generator for MIPS
  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. {$ifndef mips64}
  24. cg64f32,
  25. {$endif mips64}
  26. cpupara,
  27. aasmbase, aasmtai, aasmcpu, aasmdata,
  28. cpubase, cpuinfo,
  29. node, symconst, SymType, symdef,
  30. rgcpu;
  31. type
  32. TCGMIPS = class(tcg)
  33. public
  34. procedure init_register_allocators; override;
  35. procedure done_register_allocators; override;
  36. /// { needed by cg64 }
  37. procedure make_simple_ref(list: tasmlist; var ref: treference);
  38. procedure handle_reg_const_reg(list: tasmlist; op: Tasmop; src: tregister; a: tcgint; dst: tregister);
  39. procedure maybeadjustresult(list: TAsmList; op: TOpCg; size: tcgsize; dst: tregister);
  40. procedure overflowcheck_internal(list: TAsmList; arg1, arg2: TRegister);
  41. { parameter }
  42. procedure a_loadfpu_reg_cgpara(list: tasmlist; size: tcgsize; const r: tregister; const paraloc: TCGPara); override;
  43. procedure a_loadfpu_ref_cgpara(list: tasmlist; size: tcgsize; const ref: treference; const paraloc: TCGPara); override;
  44. procedure a_call_name(list: tasmlist; const s: string; weak : boolean); override;
  45. procedure a_call_reg(list: tasmlist; Reg: TRegister); override;
  46. procedure a_call_sym_pic(list: tasmlist; sym: tasmsymbol);
  47. { General purpose instructions }
  48. procedure a_op_const_reg(list: tasmlist; Op: TOpCG; size: tcgsize; a: tcgint; reg: TRegister); override;
  49. procedure a_op_reg_reg(list: tasmlist; Op: TOpCG; size: TCGSize; src, dst: TRegister); override;
  50. procedure a_op_const_reg_reg(list: tasmlist; op: TOpCg; size: tcgsize; a: tcgint; src, dst: tregister); override;
  51. procedure a_op_reg_reg_reg(list: tasmlist; op: TOpCg; size: tcgsize; src1, src2, dst: tregister); override;
  52. procedure a_op_const_reg_reg_checkoverflow(list: tasmlist; op: TOpCg; size: tcgsize; a: tcgint; src, dst: tregister; setflags: boolean; var ovloc: tlocation); override;
  53. procedure a_op_reg_reg_reg_checkoverflow(list: tasmlist; op: TOpCg; size: tcgsize; src1, src2, dst: tregister; setflags: boolean; var ovloc: tlocation); override;
  54. { move instructions }
  55. procedure a_load_const_reg(list: tasmlist; size: tcgsize; a: tcgint; reg: tregister); override;
  56. procedure a_load_const_ref(list: tasmlist; size: tcgsize; a: tcgint; const ref: TReference); override;
  57. procedure a_load_reg_ref(list: tasmlist; FromSize, ToSize: TCgSize; reg: TRegister; const ref: TReference); override;
  58. procedure a_load_ref_reg(list: tasmlist; FromSize, ToSize: TCgSize; const ref: TReference; reg: tregister); override;
  59. procedure a_load_reg_reg(list: tasmlist; FromSize, ToSize: TCgSize; reg1, reg2: tregister); override;
  60. procedure a_loadaddr_ref_reg(list: tasmlist; const ref: TReference; r: tregister); override;
  61. { fpu move instructions }
  62. procedure a_loadfpu_reg_reg(list: tasmlist; fromsize, tosize: tcgsize; reg1, reg2: tregister); override;
  63. procedure a_loadfpu_ref_reg(list: tasmlist; fromsize, tosize: tcgsize; const ref: TReference; reg: tregister); override;
  64. procedure a_loadfpu_reg_ref(list: tasmlist; fromsize, tosize: tcgsize; reg: tregister; const ref: TReference); override;
  65. { comparison operations }
  66. procedure a_cmp_const_reg_label(list: tasmlist; size: tcgsize; cmp_op: topcmp; a: tcgint; reg: tregister; l: tasmlabel); override;
  67. procedure a_cmp_reg_reg_label(list: tasmlist; size: tcgsize; cmp_op: topcmp; reg1, reg2: tregister; l: tasmlabel); override;
  68. procedure a_jmp_flags(list: tasmlist; const f: TResFlags; l: tasmlabel); override;
  69. procedure g_flags2reg(list: tasmlist; size: TCgSize; const f: TResFlags; reg: tregister); override;
  70. procedure a_jmp_always(List: tasmlist; l: TAsmLabel); override;
  71. procedure a_jmp_name(list: tasmlist; const s: string); override;
  72. procedure a_mul_reg_reg_pair(list: tasmlist; size: tcgsize; src1,src2,dstlo,dsthi: tregister); override;
  73. procedure g_overflowCheck(List: tasmlist; const Loc: TLocation; def: TDef); override;
  74. procedure g_overflowCheck_loc(List: tasmlist; const Loc: TLocation; def: TDef; ovloc: tlocation); override;
  75. procedure g_proc_entry(list: tasmlist; localsize: longint; nostackframe: boolean); override;
  76. procedure g_proc_exit(list: tasmlist; parasize: longint; nostackframe: boolean); override;
  77. procedure g_concatcopy(list: tasmlist; const Source, dest: treference; len: tcgint); override;
  78. procedure g_concatcopy_unaligned(list: tasmlist; const Source, dest: treference; len: tcgint); override;
  79. procedure g_concatcopy_move(list: tasmlist; const Source, dest: treference; len: tcgint);
  80. procedure g_adjust_self_value(list:TAsmList;procdef: tprocdef;ioffset: tcgint); override;
  81. procedure g_profilecode(list: TAsmList);override;
  82. end;
  83. {$ifndef mips64}
  84. TCg64MPSel = class(tcg64f32)
  85. public
  86. procedure a_load64_reg_ref(list: tasmlist; reg: tregister64; const ref: treference); override;
  87. procedure a_load64_ref_reg(list: tasmlist; const ref: treference; reg: tregister64); override;
  88. procedure a_load64_ref_cgpara(list: tasmlist; const r: treference; const paraloc: tcgpara); override;
  89. procedure a_op64_reg_reg(list: tasmlist; op: TOpCG; size: tcgsize; regsrc, regdst: TRegister64); override;
  90. procedure a_op64_const_reg(list: tasmlist; op: TOpCG; size: tcgsize; Value: int64; regdst: TRegister64); override;
  91. procedure a_op64_const_reg_reg(list: tasmlist; op: TOpCG; size: tcgsize; Value: int64; regsrc, regdst: tregister64); override;
  92. procedure a_op64_reg_reg_reg(list: tasmlist; op: TOpCG; size: tcgsize; regsrc1, regsrc2, regdst: tregister64); override;
  93. procedure a_op64_const_reg_reg_checkoverflow(list: tasmlist; op: TOpCG; size: tcgsize; Value: int64; regsrc, regdst: tregister64; setflags: boolean; var ovloc: tlocation); override;
  94. procedure a_op64_reg_reg_reg_checkoverflow(list: tasmlist; op: TOpCG; size: tcgsize; regsrc1, regsrc2, regdst: tregister64; setflags: boolean; var ovloc: tlocation); override;
  95. end;
  96. {$endif mips64}
  97. procedure create_codegen;
  98. const
  99. TOpCmp2AsmCond : array[topcmp] of TAsmCond=(C_NONE,
  100. C_EQ,C_GT,C_LT,C_GE,C_LE,C_NE,C_LEU,C_LTU,C_GEU,C_GTU
  101. );
  102. implementation
  103. uses
  104. globals, verbose, systems, cutils,
  105. paramgr, fmodule,
  106. symtable, symsym,
  107. tgobj,
  108. procinfo, cpupi;
  109. const
  110. TOpcg2AsmOp: array[TOpCg] of TAsmOp = (
  111. A_NONE,A_NONE,A_ADDU,A_AND,A_NONE,A_NONE,A_MULT,A_MULTU,A_NONE,A_NONE,
  112. A_OR,A_SRAV,A_SLLV,A_SRLV,A_SUBU,A_XOR,A_NONE,A_NONE
  113. );
  114. procedure TCGMIPS.make_simple_ref(list: tasmlist; var ref: treference);
  115. var
  116. tmpreg, tmpreg1: tregister;
  117. tmpref: treference;
  118. base_replaced: boolean;
  119. begin
  120. { Enforce some discipline for callers:
  121. - gp is always implicit
  122. - reference is processed only once }
  123. if (ref.base=NR_GP) or (ref.index=NR_GP) then
  124. InternalError(2013022801);
  125. if (ref.refaddr<>addr_no) then
  126. InternalError(2013022802);
  127. { fixup base/index, if both are present then add them together }
  128. base_replaced:=false;
  129. tmpreg:=ref.base;
  130. if (tmpreg=NR_NO) then
  131. tmpreg:=ref.index
  132. else if (ref.index<>NR_NO) then
  133. begin
  134. tmpreg:=getintregister(list,OS_ADDR);
  135. list.concat(taicpu.op_reg_reg_reg(A_ADDU,tmpreg,ref.base,ref.index));
  136. base_replaced:=true;
  137. end;
  138. ref.base:=tmpreg;
  139. ref.index:=NR_NO;
  140. if (ref.symbol=nil) and
  141. (ref.offset>=simm16lo) and
  142. (ref.offset<=simm16hi-sizeof(pint)) then
  143. exit;
  144. { Symbol present or offset > 16bits }
  145. if assigned(ref.symbol) then
  146. begin
  147. ref.base:=getintregister(list,OS_ADDR);
  148. reference_reset_symbol(tmpref,ref.symbol,ref.offset,ref.alignment,ref.volatility);
  149. if (cs_create_pic in current_settings.moduleswitches) then
  150. begin
  151. if not (pi_needs_got in current_procinfo.flags) then
  152. InternalError(2013060102);
  153. { For PIC global symbols offset must be handled separately.
  154. Otherwise (non-PIC or local symbols) offset can be encoded
  155. into relocation even if exceeds 16 bits. }
  156. if (ref.symbol.bind<>AB_LOCAL) then
  157. tmpref.offset:=0;
  158. tmpref.refaddr:=addr_pic;
  159. tmpref.base:=NR_GP;
  160. list.concat(taicpu.op_reg_ref(A_LW,ref.base,tmpref));
  161. end
  162. else
  163. begin
  164. tmpref.refaddr:=addr_high;
  165. list.concat(taicpu.op_reg_ref(A_LUI,ref.base,tmpref));
  166. end;
  167. { Add original base/index, if any. }
  168. if (tmpreg<>NR_NO) then
  169. list.concat(taicpu.op_reg_reg_reg(A_ADDU,ref.base,tmpreg,ref.base));
  170. if (ref.symbol.bind=AB_LOCAL) or
  171. not (cs_create_pic in current_settings.moduleswitches) then
  172. begin
  173. ref.refaddr:=addr_low;
  174. exit;
  175. end;
  176. { PIC global symbol }
  177. ref.symbol:=nil;
  178. if (ref.offset>=simm16lo) and
  179. (ref.offset<=simm16hi-sizeof(pint)) then
  180. exit;
  181. { fallthrough to the case of large offset }
  182. end;
  183. tmpreg1:=getintregister(list,OS_INT);
  184. a_load_const_reg(list,OS_INT,ref.offset,tmpreg1);
  185. if (ref.base=NR_NO) then
  186. ref.base:=tmpreg1 { offset alone, weird but possible }
  187. else
  188. begin
  189. tmpreg:=ref.base;
  190. if (not base_replaced) then
  191. ref.base:=getintregister(list,OS_ADDR);
  192. list.concat(taicpu.op_reg_reg_reg(A_ADDU,ref.base,tmpreg,tmpreg1))
  193. end;
  194. ref.offset:=0;
  195. end;
  196. procedure TCGMIPS.handle_reg_const_reg(list: tasmlist; op: Tasmop; src: tregister; a: tcgint; dst: tregister);
  197. var
  198. tmpreg: tregister;
  199. op2: Tasmop;
  200. negate: boolean;
  201. begin
  202. case op of
  203. A_ADD,A_SUB:
  204. op2:=A_ADDI;
  205. A_ADDU,A_SUBU:
  206. op2:=A_ADDIU;
  207. else
  208. InternalError(2013052001);
  209. end;
  210. negate:=op in [A_SUB,A_SUBU];
  211. { subtraction is actually addition of negated value, so possible range is
  212. off by one (-32767..32768) }
  213. if (a < simm16lo+ord(negate)) or
  214. (a > simm16hi+ord(negate)) then
  215. begin
  216. tmpreg := GetIntRegister(list, OS_INT);
  217. a_load_const_reg(list, OS_INT, a, tmpreg);
  218. list.concat(taicpu.op_reg_reg_reg(op, dst, src, tmpreg));
  219. end
  220. else
  221. begin
  222. if negate then
  223. a:=-a;
  224. list.concat(taicpu.op_reg_reg_const(op2, dst, src, a));
  225. end;
  226. end;
  227. {****************************************************************************
  228. Assembler code
  229. ****************************************************************************}
  230. procedure TCGMIPS.init_register_allocators;
  231. begin
  232. inherited init_register_allocators;
  233. { Keep RS_R25, i.e. $t9 for PIC call }
  234. if (cs_create_pic in current_settings.moduleswitches) and assigned(current_procinfo) and
  235. (pi_needs_got in current_procinfo.flags) then
  236. begin
  237. current_procinfo.got := NR_GP;
  238. rg[R_INTREGISTER] := Trgintcpu.Create(R_INTREGISTER, R_SUBD,
  239. [RS_R2,RS_R3,RS_R4,RS_R5,RS_R6,RS_R7,RS_R8,RS_R9,
  240. RS_R10,RS_R11,RS_R12,RS_R13,RS_R14,RS_R15,RS_R16,RS_R17,RS_R18,RS_R19,
  241. RS_R20,RS_R21,RS_R22,RS_R23,RS_R24{,RS_R25}],
  242. first_int_imreg, []);
  243. end
  244. else
  245. rg[R_INTREGISTER] := trgintcpu.Create(R_INTREGISTER, R_SUBD,
  246. [RS_R2,RS_R3,RS_R4,RS_R5,RS_R6,RS_R7,RS_R8,RS_R9,
  247. RS_R10,RS_R11,RS_R12,RS_R13,RS_R14,RS_R15,RS_R16,RS_R17,RS_R18,RS_R19,
  248. RS_R20,RS_R21,RS_R22,RS_R23,RS_R24{,RS_R25}],
  249. first_int_imreg, []);
  250. {
  251. rg[R_FPUREGISTER] := trgcpu.Create(R_FPUREGISTER, R_SUBFS,
  252. [RS_F0,RS_F1,RS_F2,RS_F3,RS_F4,RS_F5,RS_F6,RS_F7,
  253. RS_F8,RS_F9,RS_F10,RS_F11,RS_F12,RS_F13,RS_F14,RS_F15,
  254. RS_F16,RS_F17,RS_F18,RS_F19,RS_F20,RS_F21,RS_F22,RS_F23,
  255. RS_F24,RS_F25,RS_F26,RS_F27,RS_F28,RS_F29,RS_F30,RS_F31],
  256. first_fpu_imreg, []);
  257. }
  258. rg[R_FPUREGISTER] := trgcpu.Create(R_FPUREGISTER, R_SUBFS,
  259. [RS_F0,RS_F2,RS_F4,RS_F6, RS_F8,RS_F10,RS_F12,RS_F14,
  260. RS_F16,RS_F18,RS_F20,RS_F22, RS_F24,RS_F26,RS_F28,RS_F30],
  261. first_fpu_imreg, []);
  262. end;
  263. procedure TCGMIPS.done_register_allocators;
  264. begin
  265. rg[R_INTREGISTER].Free;
  266. rg[R_FPUREGISTER].Free;
  267. inherited done_register_allocators;
  268. end;
  269. procedure TCGMIPS.a_loadfpu_ref_cgpara(list: tasmlist; size: tcgsize; const ref: treference; const paraloc: TCGPara);
  270. var
  271. href, href2: treference;
  272. hloc: pcgparalocation;
  273. begin
  274. { TODO: inherited cannot deal with individual locations for each of OS_32 registers.
  275. Must change parameter management to allocate a single 64-bit register pair,
  276. then this method can be removed. }
  277. href := ref;
  278. hloc := paraloc.location;
  279. while assigned(hloc) do
  280. begin
  281. paramanager.allocparaloc(list,hloc);
  282. case hloc^.loc of
  283. LOC_REGISTER:
  284. a_load_ref_reg(list, hloc^.size, hloc^.size, href, hloc^.Register);
  285. LOC_FPUREGISTER,LOC_CFPUREGISTER :
  286. a_loadfpu_ref_reg(list,hloc^.size,hloc^.size,href,hloc^.register);
  287. LOC_REFERENCE:
  288. begin
  289. paraloc.check_simple_location;
  290. reference_reset_base(href2,paraloc.location^.reference.index,paraloc.location^.reference.offset,ctempposinvalid,paraloc.alignment,[]);
  291. { concatcopy should choose the best way to copy the data }
  292. g_concatcopy(list,ref,href2,tcgsize2size[size]);
  293. end;
  294. else
  295. internalerror(200408241);
  296. end;
  297. Inc(href.offset, tcgsize2size[hloc^.size]);
  298. hloc := hloc^.Next;
  299. end;
  300. end;
  301. procedure TCGMIPS.a_loadfpu_reg_cgpara(list: tasmlist; size: tcgsize; const r: tregister; const paraloc: TCGPara);
  302. var
  303. href: treference;
  304. begin
  305. if paraloc.Location^.next=nil then
  306. begin
  307. inherited a_loadfpu_reg_cgpara(list,size,r,paraloc);
  308. exit;
  309. end;
  310. tg.GetTemp(list, TCGSize2Size[size], TCGSize2Size[size], tt_normal, href);
  311. a_loadfpu_reg_ref(list, size, size, r, href);
  312. a_loadfpu_ref_cgpara(list, size, href, paraloc);
  313. tg.Ungettemp(list, href);
  314. end;
  315. procedure TCGMIPS.a_call_sym_pic(list: tasmlist; sym: tasmsymbol);
  316. var
  317. href: treference;
  318. begin
  319. reference_reset_symbol(href,sym,0,sizeof(aint),[]);
  320. if (sym.bind=AB_LOCAL) then
  321. href.refaddr:=addr_pic
  322. else
  323. href.refaddr:=addr_pic_call16;
  324. href.base:=NR_GP;
  325. list.concat(taicpu.op_reg_ref(A_LW,NR_PIC_FUNC,href));
  326. if (sym.bind=AB_LOCAL) then
  327. begin
  328. href.refaddr:=addr_low;
  329. href.base:=NR_NO;
  330. list.concat(taicpu.op_reg_ref(A_ADDIU,NR_PIC_FUNC,href));
  331. end;
  332. list.concat(taicpu.op_reg(A_JALR,NR_PIC_FUNC));
  333. { Delay slot }
  334. list.concat(taicpu.op_none(A_NOP));
  335. { Restore GP if in PIC mode }
  336. if (cs_create_pic in current_settings.moduleswitches) then
  337. begin
  338. if tcpuprocinfo(current_procinfo).save_gp_ref.offset=0 then
  339. InternalError(2013071001);
  340. list.concat(taicpu.op_reg_ref(A_LW,NR_GP,tcpuprocinfo(current_procinfo).save_gp_ref));
  341. end;
  342. end;
  343. procedure TCGMIPS.a_call_name(list: tasmlist; const s: string; weak: boolean);
  344. var
  345. sym: tasmsymbol;
  346. begin
  347. if assigned(current_procinfo) and
  348. not (pi_do_call in current_procinfo.flags) then
  349. InternalError(2013022101);
  350. if weak then
  351. sym:=current_asmdata.WeakRefAsmSymbol(s,AT_FUNCTION)
  352. else
  353. sym:=current_asmdata.RefAsmSymbol(s,AT_FUNCTION);
  354. if (cs_create_pic in current_settings.moduleswitches) then
  355. a_call_sym_pic(list,sym)
  356. else
  357. begin
  358. list.concat(taicpu.op_sym(A_JAL,sym));
  359. { Delay slot }
  360. list.concat(taicpu.op_none(A_NOP));
  361. end;
  362. end;
  363. procedure TCGMIPS.a_call_reg(list: tasmlist; Reg: TRegister);
  364. begin
  365. if assigned(current_procinfo) and
  366. not (pi_do_call in current_procinfo.flags) then
  367. InternalError(2013022102);
  368. if (Reg <> NR_PIC_FUNC) then
  369. list.concat(taicpu.op_reg_reg(A_MOVE,NR_PIC_FUNC,reg));
  370. list.concat(taicpu.op_reg(A_JALR,NR_PIC_FUNC));
  371. { Delay slot }
  372. list.concat(taicpu.op_none(A_NOP));
  373. { Restore GP if in PIC mode }
  374. if (cs_create_pic in current_settings.moduleswitches) then
  375. begin
  376. if tcpuprocinfo(current_procinfo).save_gp_ref.offset=0 then
  377. InternalError(2013071002);
  378. list.concat(taicpu.op_reg_ref(A_LW,NR_GP,tcpuprocinfo(current_procinfo).save_gp_ref));
  379. end;
  380. end;
  381. {********************** load instructions ********************}
  382. procedure TCGMIPS.a_load_const_reg(list: tasmlist; size: TCGSize; a: tcgint; reg: TRegister);
  383. begin
  384. if (a = 0) then
  385. a_load_reg_reg(list, OS_INT, OS_INT, NR_R0, reg)
  386. else if (a >= simm16lo) and (a <= simm16hi) then
  387. list.concat(taicpu.op_reg_reg_const(A_ADDIU, reg, NR_R0, a))
  388. else if (a>=0) and (a <= 65535) then
  389. list.concat(taicpu.op_reg_reg_const(A_ORI, reg, NR_R0, a))
  390. {$ifdef mips32}
  391. else
  392. {$else}
  393. else if (a>=0) and (a <= high(dword)) then
  394. {$endif}
  395. begin
  396. list.concat(taicpu.op_reg_const(A_LUI, reg, aint(a) shr 16));
  397. if (a and aint($FFFF))<>0 then
  398. list.concat(taicpu.op_reg_reg_const(A_ORI,reg,reg,a and aint($FFFF)));
  399. {$ifdef mips64}
  400. end
  401. else
  402. begin
  403. list.concat(taicpu.op_reg_const(A_LUI, reg, aint(a) shr 48));
  404. if ((a shr 32) and aint($FFFF))<>0 then
  405. list.concat(taicpu.op_reg_reg_const(A_ORI,reg,reg,(a shr 32) and aint($FFFF)));
  406. list.concat(taicpu.op_reg_const(A_SLL, reg, 16));
  407. if ((a shr 16) and aint($FFFF))<>0 then
  408. list.concat(taicpu.op_reg_reg_const(A_ORI,reg,reg,(a shr 16) and aint($FFFF)));
  409. list.concat(taicpu.op_reg_const(A_SLL, reg, 16));
  410. if (a and aint($FFFF))<>0 then
  411. list.concat(taicpu.op_reg_reg_const(A_ORI,reg,reg,a and aint($FFFF)));
  412. {$endif mips64}
  413. end;
  414. end;
  415. procedure TCGMIPS.a_load_const_ref(list: tasmlist; size: tcgsize; a: tcgint; const ref: TReference);
  416. begin
  417. if a = 0 then
  418. a_load_reg_ref(list, size, size, NR_R0, ref)
  419. else
  420. inherited a_load_const_ref(list, size, a, ref);
  421. end;
  422. procedure TCGMIPS.a_load_reg_ref(list: tasmlist; FromSize, ToSize: TCGSize; reg: tregister; const Ref: TReference);
  423. var
  424. op: tasmop;
  425. href: treference;
  426. begin
  427. if (TCGSize2Size[fromsize] < TCGSize2Size[tosize]) then
  428. a_load_reg_reg(list,fromsize,tosize,reg,reg);
  429. if (ref.alignment<>0) and
  430. (ref.alignment<tcgsize2size[tosize]) then
  431. begin
  432. a_load_reg_ref_unaligned(list,FromSize,ToSize,reg,ref);
  433. exit;
  434. end;
  435. case tosize of
  436. OS_8,
  437. OS_S8:
  438. Op := A_SB;
  439. OS_16,
  440. OS_S16:
  441. Op := A_SH;
  442. OS_32,
  443. OS_S32:
  444. Op := A_SW;
  445. OS_64,
  446. OS_S64:
  447. Op := A_SD;
  448. else
  449. InternalError(2002122100);
  450. end;
  451. href:=ref;
  452. make_simple_ref(list,href);
  453. list.concat(taicpu.op_reg_ref(op,reg,href));
  454. end;
  455. procedure TCGMIPS.a_load_ref_reg(list: tasmlist; FromSize, ToSize: TCgSize; const ref: TReference; reg: tregister);
  456. var
  457. op: tasmop;
  458. href: treference;
  459. begin
  460. if (TCGSize2Size[fromsize] >= TCGSize2Size[tosize]) then
  461. fromsize := tosize;
  462. if (ref.alignment<>0) and
  463. (ref.alignment<tcgsize2size[fromsize]) then
  464. begin
  465. a_load_ref_reg_unaligned(list,FromSize,ToSize,ref,reg);
  466. exit;
  467. end;
  468. case fromsize of
  469. OS_S8:
  470. Op := A_LB;{Load Signed Byte}
  471. OS_8:
  472. Op := A_LBU;{Load Unsigned Byte}
  473. OS_S16:
  474. Op := A_LH;{Load Signed Halfword}
  475. OS_16:
  476. Op := A_LHU;{Load Unsigned Halfword}
  477. OS_S32:
  478. Op := A_LW;{Load Word}
  479. OS_32:
  480. Op := A_LW;//A_LWU;{Load Unsigned Word}
  481. OS_S64,
  482. OS_64:
  483. Op := A_LD;{Load a Long Word}
  484. else
  485. InternalError(2002122101);
  486. end;
  487. href:=ref;
  488. make_simple_ref(list,href);
  489. list.concat(taicpu.op_reg_ref(op,reg,href));
  490. if (fromsize=OS_S8) and (tosize=OS_16) then
  491. a_load_reg_reg(list,fromsize,tosize,reg,reg);
  492. end;
  493. procedure TCGMIPS.a_load_reg_reg(list: tasmlist; fromsize, tosize: tcgsize; reg1, reg2: tregister);
  494. var
  495. instr: taicpu;
  496. done: boolean;
  497. begin
  498. if (tcgsize2size[tosize] < tcgsize2size[fromsize]) or
  499. (
  500. (tcgsize2size[tosize] = tcgsize2size[fromsize]) and (tosize <> fromsize)
  501. ) or ((fromsize = OS_S8) and
  502. (tosize = OS_16)) then
  503. begin
  504. done:=true;
  505. case tosize of
  506. OS_8:
  507. list.concat(taicpu.op_reg_reg_const(A_ANDI, reg2, reg1, $ff));
  508. OS_16:
  509. list.concat(taicpu.op_reg_reg_const(A_ANDI, reg2, reg1, $ffff));
  510. {$ifdef cpu64bitalu}
  511. OS_64,
  512. OS_S64,
  513. {$endif cpu64bitalu}
  514. OS_32,
  515. OS_S32:
  516. done:=false;
  517. OS_S8:
  518. begin
  519. if (CPUMIPS_HAS_ISA32R2 in cpu_capabilities[current_settings.cputype]) then
  520. list.concat(taicpu.op_reg_reg(A_SEB,reg2,reg1))
  521. else
  522. begin
  523. list.concat(taicpu.op_reg_reg_const(A_SLL, reg2, reg1, 24));
  524. list.concat(taicpu.op_reg_reg_const(A_SRA, reg2, reg2, 24));
  525. end;
  526. end;
  527. OS_S16:
  528. begin
  529. if (CPUMIPS_HAS_ISA32R2 in cpu_capabilities[current_settings.cputype]) then
  530. list.concat(taicpu.op_reg_reg(A_SEH,reg2,reg1))
  531. else
  532. begin
  533. list.concat(taicpu.op_reg_reg_const(A_SLL, reg2, reg1, 16));
  534. list.concat(taicpu.op_reg_reg_const(A_SRA, reg2, reg2, 16));
  535. end;
  536. end;
  537. else
  538. internalerror(2002090901);
  539. end;
  540. end
  541. else
  542. done:=false;
  543. if (not done) and (reg1 <> reg2) then
  544. begin
  545. { same size, only a register mov required }
  546. instr := taicpu.op_reg_reg(A_MOVE, reg2, reg1);
  547. list.Concat(instr);
  548. { Notify the register allocator that we have written a move instruction so
  549. it can try to eliminate it. }
  550. add_move_instruction(instr);
  551. end;
  552. end;
  553. procedure TCGMIPS.a_loadaddr_ref_reg(list: tasmlist; const ref: TReference; r: tregister);
  554. var
  555. href: treference;
  556. hreg: tregister;
  557. begin
  558. { Enforce some discipline for callers:
  559. - reference must be a "raw" one and not use gp }
  560. if (ref.base=NR_GP) or (ref.index=NR_GP) then
  561. InternalError(2013022803);
  562. if (ref.refaddr<>addr_no) then
  563. InternalError(2013022804);
  564. if (ref.base=NR_NO) and (ref.index<>NR_NO) then
  565. InternalError(200306171);
  566. if (ref.symbol=nil) then
  567. begin
  568. if (ref.base<>NR_NO) then
  569. begin
  570. if (ref.offset<simm16lo) or (ref.offset>simm16hi) then
  571. begin
  572. hreg:=getintregister(list,OS_INT);
  573. a_load_const_reg(list,OS_INT,ref.offset,hreg);
  574. list.concat(taicpu.op_reg_reg_reg(A_ADDU,r,ref.base,hreg));
  575. end
  576. else if (ref.offset<>0) then
  577. list.concat(taicpu.op_reg_reg_const(A_ADDIU,r,ref.base,ref.offset))
  578. else
  579. a_load_reg_reg(list,OS_INT,OS_INT,ref.base,r); { emit optimizable move }
  580. if (ref.index<>NR_NO) then
  581. list.concat(taicpu.op_reg_reg_reg(A_ADDU,r,r,ref.index));
  582. end
  583. else
  584. a_load_const_reg(list,OS_INT,ref.offset,r);
  585. exit;
  586. end;
  587. reference_reset_symbol(href,ref.symbol,ref.offset,ref.alignment,ref.volatility);
  588. if (cs_create_pic in current_settings.moduleswitches) then
  589. begin
  590. if not (pi_needs_got in current_procinfo.flags) then
  591. InternalError(2013060104);
  592. { For PIC global symbols offset must be handled separately.
  593. Otherwise (non-PIC or local symbols) offset can be encoded
  594. into relocation even if exceeds 16 bits. }
  595. if (href.symbol.bind<>AB_LOCAL) then
  596. href.offset:=0;
  597. href.refaddr:=addr_pic;
  598. href.base:=NR_GP;
  599. list.concat(taicpu.op_reg_ref(A_LW,r,href));
  600. end
  601. else
  602. begin
  603. href.refaddr:=addr_high;
  604. list.concat(taicpu.op_reg_ref(A_LUI,r,href));
  605. end;
  606. { Add original base/index, if any. }
  607. if (ref.base<>NR_NO) then
  608. list.concat(taicpu.op_reg_reg_reg(A_ADDU,r,r,ref.base));
  609. if (ref.index<>NR_NO) then
  610. list.concat(taicpu.op_reg_reg_reg(A_ADDU,r,r,ref.index));
  611. { add low part if necessary }
  612. if (ref.symbol.bind=AB_LOCAL) or
  613. not (cs_create_pic in current_settings.moduleswitches) then
  614. begin
  615. href.refaddr:=addr_low;
  616. href.base:=NR_NO;
  617. list.concat(taicpu.op_reg_reg_ref(A_ADDIU,r,r,href));
  618. exit;
  619. end;
  620. if (ref.offset<simm16lo) or (ref.offset>simm16hi) then
  621. begin
  622. hreg:=getintregister(list,OS_INT);
  623. a_load_const_reg(list,OS_INT,ref.offset,hreg);
  624. list.concat(taicpu.op_reg_reg_reg(A_ADDU,r,r,hreg));
  625. end
  626. else if (ref.offset<>0) then
  627. list.concat(taicpu.op_reg_reg_const(A_ADDIU,r,r,ref.offset));
  628. end;
  629. procedure TCGMIPS.a_loadfpu_reg_reg(list: tasmlist; fromsize, tosize: tcgsize; reg1, reg2: tregister);
  630. const
  631. FpuMovInstr: array[OS_F32..OS_F64,OS_F32..OS_F64] of TAsmOp =
  632. ((A_MOV_S, A_CVT_D_S),(A_CVT_S_D,A_MOV_D));
  633. var
  634. instr: taicpu;
  635. begin
  636. if (reg1 <> reg2) or (fromsize<>tosize) then
  637. begin
  638. instr := taicpu.op_reg_reg(fpumovinstr[fromsize,tosize], reg2, reg1);
  639. list.Concat(instr);
  640. { Notify the register allocator that we have written a move instruction so
  641. it can try to eliminate it. }
  642. if (fromsize=tosize) then
  643. add_move_instruction(instr);
  644. end;
  645. end;
  646. procedure TCGMIPS.a_loadfpu_ref_reg(list: tasmlist; fromsize, tosize: tcgsize; const ref: TReference; reg: tregister);
  647. var
  648. href: TReference;
  649. begin
  650. href:=ref;
  651. make_simple_ref(list,href);
  652. case fromsize of
  653. OS_F32:
  654. list.concat(taicpu.op_reg_ref(A_LWC1,reg,href));
  655. OS_F64:
  656. list.concat(taicpu.op_reg_ref(A_LDC1,reg,href));
  657. else
  658. InternalError(2007042701);
  659. end;
  660. if tosize<>fromsize then
  661. a_loadfpu_reg_reg(list,fromsize,tosize,reg,reg);
  662. end;
  663. procedure TCGMIPS.a_loadfpu_reg_ref(list: tasmlist; fromsize, tosize: tcgsize; reg: tregister; const ref: TReference);
  664. var
  665. href: TReference;
  666. begin
  667. if tosize<>fromsize then
  668. a_loadfpu_reg_reg(list,fromsize,tosize,reg,reg);
  669. href:=ref;
  670. make_simple_ref(list,href);
  671. case tosize of
  672. OS_F32:
  673. list.concat(taicpu.op_reg_ref(A_SWC1,reg,href));
  674. OS_F64:
  675. list.concat(taicpu.op_reg_ref(A_SDC1,reg,href));
  676. else
  677. InternalError(2007042702);
  678. end;
  679. end;
  680. procedure TCGMIPS.maybeadjustresult(list: TAsmList; op: TOpCg; size: tcgsize; dst: tregister);
  681. const
  682. overflowops = [OP_MUL,OP_SHL,OP_ADD,OP_SUB,OP_NOT,OP_NEG];
  683. begin
  684. if (op in overflowops) and
  685. (size in [OS_8,OS_S8,OS_16,OS_S16]) then
  686. a_load_reg_reg(list,OS_32,size,dst,dst);
  687. end;
  688. procedure TCGMIPS.overflowcheck_internal(list: tasmlist; arg1, arg2: tregister);
  689. var
  690. carry, hreg: tregister;
  691. begin
  692. if (arg1=arg2) then
  693. InternalError(2013050501);
  694. carry:=GetIntRegister(list,OS_INT);
  695. hreg:=GetIntRegister(list,OS_INT);
  696. list.concat(taicpu.op_reg_reg_reg(A_SLTU,carry,arg1,arg2));
  697. { if carry<>0, this will cause hardware overflow interrupt }
  698. a_load_const_reg(list,OS_INT,$80000000,hreg);
  699. list.concat(taicpu.op_reg_reg_reg(A_SUB,hreg,hreg,carry));
  700. end;
  701. const
  702. ops_add: array[boolean] of TAsmOp = (A_ADDU, A_ADD);
  703. ops_sub: array[boolean] of TAsmOp = (A_SUBU, A_SUB);
  704. ops_slt: array[boolean] of TAsmOp = (A_SLTU, A_SLT);
  705. ops_slti: array[boolean] of TAsmOp = (A_SLTIU, A_SLTI);
  706. ops_and: array[boolean] of TAsmOp = (A_AND, A_ANDI);
  707. ops_or: array[boolean] of TAsmOp = (A_OR, A_ORI);
  708. ops_xor: array[boolean] of TasmOp = (A_XOR, A_XORI);
  709. procedure TCGMIPS.a_op_const_reg(list: tasmlist; Op: TOpCG; size: tcgsize; a: tcgint; reg: TRegister);
  710. begin
  711. optimize_op_const(size,op,a);
  712. case op of
  713. OP_NONE:
  714. exit;
  715. OP_MOVE:
  716. a_load_const_reg(list,size,a,reg);
  717. OP_NEG,OP_NOT:
  718. internalerror(200306011);
  719. else
  720. a_op_const_reg_reg(list,op,size,a,reg,reg);
  721. end;
  722. end;
  723. procedure TCGMIPS.a_op_reg_reg(list: tasmlist; Op: TOpCG; size: TCGSize; src, dst: TRegister);
  724. begin
  725. case Op of
  726. OP_NEG:
  727. list.concat(taicpu.op_reg_reg_reg(A_SUBU, dst, NR_R0, src));
  728. OP_NOT:
  729. list.concat(taicpu.op_reg_reg_reg(A_NOR, dst, NR_R0, src));
  730. OP_IMUL,OP_MUL:
  731. begin
  732. list.concat(taicpu.op_reg_reg(TOpcg2AsmOp[op], dst, src));
  733. list.concat(taicpu.op_reg(A_MFLO, dst));
  734. end;
  735. else
  736. a_op_reg_reg_reg(list, op, size, src, dst, dst);
  737. exit;
  738. end;
  739. maybeadjustresult(list,op,size,dst);
  740. end;
  741. procedure TCGMIPS.a_op_const_reg_reg(list: tasmlist; op: TOpCg; size: tcgsize; a: tcgint; src, dst: tregister);
  742. var
  743. l: TLocation;
  744. begin
  745. a_op_const_reg_reg_checkoverflow(list, op, size, a, src, dst, false, l);
  746. end;
  747. procedure TCGMIPS.a_op_reg_reg_reg(list: tasmlist; op: TOpCg; size: tcgsize; src1, src2, dst: tregister);
  748. begin
  749. if (TOpcg2AsmOp[op]=A_NONE) then
  750. InternalError(2013070305);
  751. if (op=OP_SAR) then
  752. begin
  753. if (size in [OS_S8,OS_S16]) then
  754. begin
  755. { Sign-extend before shiting }
  756. list.concat(taicpu.op_reg_reg_const(A_SLL, dst, src2, 32-(tcgsize2size[size]*8)));
  757. list.concat(taicpu.op_reg_reg_const(A_SRA, dst, dst, 32-(tcgsize2size[size]*8)));
  758. src2:=dst;
  759. end
  760. else if not (size in [OS_32,OS_S32]) then
  761. InternalError(2013070306);
  762. end;
  763. list.concat(taicpu.op_reg_reg_reg(TOpCG2AsmOp[op], dst, src2, src1));
  764. maybeadjustresult(list,op,size,dst);
  765. end;
  766. procedure TCGMIPS.a_op_const_reg_reg_checkoverflow(list: tasmlist; op: TOpCg; size: tcgsize; a: tcgint; src, dst: tregister; setflags: boolean; var ovloc: tlocation);
  767. var
  768. signed,immed: boolean;
  769. hreg: TRegister;
  770. asmop: TAsmOp;
  771. begin
  772. a:=aint(a);
  773. ovloc.loc := LOC_VOID;
  774. optimize_op_const(size,op,a);
  775. signed:=(size in [OS_S8,OS_S16,OS_S32]);
  776. if (setflags and (not signed) and (src=dst) and (op in [OP_ADD,OP_SUB])) then
  777. hreg:=GetIntRegister(list,OS_INT)
  778. else
  779. hreg:=dst;
  780. case op of
  781. OP_NONE:
  782. a_load_reg_reg(list,size,size,src,dst);
  783. OP_MOVE:
  784. a_load_const_reg(list,size,a,dst);
  785. OP_ADD:
  786. begin
  787. handle_reg_const_reg(list,ops_add[setflags and signed],src,a,hreg);
  788. if setflags and (not signed) then
  789. overflowcheck_internal(list,hreg,src);
  790. { does nothing if hreg=dst }
  791. a_load_reg_reg(list,OS_INT,OS_INT,hreg,dst);
  792. end;
  793. OP_SUB:
  794. begin
  795. handle_reg_const_reg(list,ops_sub[setflags and signed],src,a,hreg);
  796. if setflags and (not signed) then
  797. overflowcheck_internal(list,src,hreg);
  798. a_load_reg_reg(list,OS_INT,OS_INT,hreg,dst);
  799. end;
  800. OP_MUL,OP_IMUL:
  801. begin
  802. hreg:=GetIntRegister(list,OS_INT);
  803. a_load_const_reg(list,OS_INT,a,hreg);
  804. a_op_reg_reg_reg_checkoverflow(list,op,size,src,hreg,dst,setflags,ovloc);
  805. exit;
  806. end;
  807. OP_AND,OP_OR,OP_XOR:
  808. begin
  809. { logical operations zero-extend, not sign-extend, the immediate }
  810. immed:=(a>=0) and (a<=65535);
  811. case op of
  812. OP_AND: asmop:=ops_and[immed];
  813. OP_OR: asmop:=ops_or[immed];
  814. OP_XOR: asmop:=ops_xor[immed];
  815. else
  816. InternalError(2013050401);
  817. end;
  818. if immed then
  819. list.concat(taicpu.op_reg_reg_const(asmop,dst,src,a))
  820. else
  821. begin
  822. hreg:=GetIntRegister(list,OS_INT);
  823. a_load_const_reg(list,OS_INT,a,hreg);
  824. list.concat(taicpu.op_reg_reg_reg(asmop,dst,src,hreg));
  825. end;
  826. end;
  827. OP_SHL:
  828. list.concat(taicpu.op_reg_reg_const(A_SLL,dst,src,a));
  829. OP_SHR:
  830. list.concat(taicpu.op_reg_reg_const(A_SRL,dst,src,a));
  831. OP_SAR:
  832. begin
  833. if (size in [OS_S8,OS_S16]) then
  834. begin
  835. list.concat(taicpu.op_reg_reg_const(A_SLL,dst,src,32-(tcgsize2size[size]*8)));
  836. inc(a,32-tcgsize2size[size]*8);
  837. src:=dst;
  838. end
  839. else if not (size in [OS_32,OS_S32]) then
  840. InternalError(2013070303);
  841. list.concat(taicpu.op_reg_reg_const(A_SRA,dst,src,a));
  842. end;
  843. else
  844. internalerror(2007012601);
  845. end;
  846. maybeadjustresult(list,op,size,dst);
  847. end;
  848. procedure TCGMIPS.a_op_reg_reg_reg_checkoverflow(list: tasmlist; op: TOpCg; size: tcgsize; src1, src2, dst: tregister; setflags: boolean; var ovloc: tlocation);
  849. var
  850. signed: boolean;
  851. hreg,hreg2: TRegister;
  852. hl: tasmlabel;
  853. begin
  854. ovloc.loc := LOC_VOID;
  855. signed:=(size in [OS_S8,OS_S16,OS_S32]);
  856. if (setflags and (not signed) and (src2=dst) and (op in [OP_ADD,OP_SUB])) then
  857. hreg:=GetIntRegister(list,OS_INT)
  858. else
  859. hreg:=dst;
  860. case op of
  861. OP_ADD:
  862. begin
  863. list.concat(taicpu.op_reg_reg_reg(ops_add[setflags and signed], hreg, src2, src1));
  864. if setflags and (not signed) then
  865. overflowcheck_internal(list, hreg, src2);
  866. a_load_reg_reg(list, OS_INT, OS_INT, hreg, dst);
  867. end;
  868. OP_SUB:
  869. begin
  870. list.concat(taicpu.op_reg_reg_reg(ops_sub[setflags and signed], hreg, src2, src1));
  871. if setflags and (not signed) then
  872. overflowcheck_internal(list, src2, hreg);
  873. a_load_reg_reg(list, OS_INT, OS_INT, hreg, dst);
  874. end;
  875. OP_MUL,OP_IMUL:
  876. begin
  877. if (CPUMIPS_HAS_ISA32R2 in cpu_capabilities[current_settings.cputype]) and
  878. (not setflags) then
  879. { NOTE: MUL is actually mips32r1 instruction; on older cores it is handled as macro }
  880. list.concat(taicpu.op_reg_reg_reg(A_MUL,dst,src2,src1))
  881. else
  882. begin
  883. list.concat(taicpu.op_reg_reg(TOpCg2AsmOp[op], src2, src1));
  884. list.concat(taicpu.op_reg(A_MFLO, dst));
  885. if setflags then
  886. begin
  887. current_asmdata.getjumplabel(hl);
  888. hreg:=GetIntRegister(list,OS_INT);
  889. list.concat(taicpu.op_reg(A_MFHI,hreg));
  890. if (op=OP_IMUL) then
  891. begin
  892. hreg2:=GetIntRegister(list,OS_INT);
  893. list.concat(taicpu.op_reg_reg_const(A_SRA,hreg2,dst,31));
  894. a_cmp_reg_reg_label(list,OS_INT,OC_EQ,hreg2,hreg,hl);
  895. end
  896. else
  897. a_cmp_reg_reg_label(list,OS_INT,OC_EQ,hreg,NR_R0,hl);
  898. list.concat(taicpu.op_const(A_BREAK,6));
  899. a_label(list,hl);
  900. end;
  901. end;
  902. end;
  903. OP_AND,OP_OR,OP_XOR:
  904. begin
  905. list.concat(taicpu.op_reg_reg_reg(TOpCG2AsmOp[op], dst, src2, src1));
  906. end;
  907. else
  908. internalerror(2007012602);
  909. end;
  910. maybeadjustresult(list,op,size,dst);
  911. end;
  912. {*************** compare instructructions ****************}
  913. procedure TCGMIPS.a_cmp_const_reg_label(list: tasmlist; size: tcgsize; cmp_op: topcmp; a: tcgint; reg: tregister; l: tasmlabel);
  914. var
  915. tmpreg: tregister;
  916. begin
  917. if a = 0 then
  918. a_cmp_reg_reg_label(list,size,cmp_op,NR_R0,reg,l)
  919. else
  920. begin
  921. tmpreg := GetIntRegister(list,OS_INT);
  922. if (a>=simm16lo) and (a<=simm16hi) and
  923. (cmp_op in [OC_LT,OC_B,OC_GTE,OC_AE]) then
  924. begin
  925. list.concat(taicpu.op_reg_reg_const(ops_slti[cmp_op in [OC_LT,OC_GTE]],tmpreg,reg,a));
  926. if cmp_op in [OC_LT,OC_B] then
  927. a_cmp_reg_reg_label(list,size,OC_NE,NR_R0,tmpreg,l)
  928. else
  929. a_cmp_reg_reg_label(list,size,OC_EQ,NR_R0,tmpreg,l);
  930. end
  931. else
  932. begin
  933. a_load_const_reg(list,OS_INT,a,tmpreg);
  934. a_cmp_reg_reg_label(list, size, cmp_op, tmpreg, reg, l);
  935. end;
  936. end;
  937. end;
  938. const
  939. TOpCmp2AsmCond_z : array[OC_GT..OC_LTE] of TAsmCond=(
  940. C_GTZ,C_LTZ,C_GEZ,C_LEZ
  941. );
  942. TOpCmp2AsmCond_eqne: array[topcmp] of TAsmCond = (C_NONE,
  943. { eq gt lt gte lte ne }
  944. C_NONE, C_NE, C_NE, C_EQ, C_EQ, C_NONE,
  945. { be b ae a }
  946. C_EQ, C_NE, C_EQ, C_NE
  947. );
  948. procedure TCGMIPS.a_cmp_reg_reg_label(list: tasmlist; size: tcgsize; cmp_op: topcmp; reg1, reg2: tregister; l: tasmlabel);
  949. var
  950. ai : Taicpu;
  951. op: TAsmOp;
  952. hreg: TRegister;
  953. begin
  954. if not (cmp_op in [OC_EQ,OC_NE]) then
  955. begin
  956. if ((reg1=NR_R0) or (reg2=NR_R0)) and (cmp_op in [OC_GT,OC_LT,OC_GTE,OC_LTE]) then
  957. begin
  958. if (reg2=NR_R0) then
  959. begin
  960. ai:=taicpu.op_reg_sym(A_BC,reg1,l);
  961. ai.setcondition(TOpCmp2AsmCond_z[swap_opcmp(cmp_op)]);
  962. end
  963. else
  964. begin
  965. ai:=taicpu.op_reg_sym(A_BC,reg2,l);
  966. ai.setcondition(TOpCmp2AsmCond_z[cmp_op]);
  967. end;
  968. end
  969. else
  970. begin
  971. hreg:=GetIntRegister(list,OS_INT);
  972. op:=ops_slt[cmp_op in [OC_LT,OC_LTE,OC_GT,OC_GTE]];
  973. if (cmp_op in [OC_LTE,OC_GT,OC_BE,OC_A]) then { swap operands }
  974. list.concat(taicpu.op_reg_reg_reg(op,hreg,reg1,reg2))
  975. else
  976. list.concat(taicpu.op_reg_reg_reg(op,hreg,reg2,reg1));
  977. if (TOpCmp2AsmCond_eqne[cmp_op]=C_NONE) then
  978. InternalError(2013051501);
  979. ai:=taicpu.op_reg_reg_sym(A_BC,hreg,NR_R0,l);
  980. ai.SetCondition(TOpCmp2AsmCond_eqne[cmp_op]);
  981. end;
  982. end
  983. else
  984. begin
  985. ai:=taicpu.op_reg_reg_sym(A_BC,reg2,reg1,l);
  986. ai.SetCondition(TOpCmp2AsmCond[cmp_op]);
  987. end;
  988. list.concat(ai);
  989. { Delay slot }
  990. list.Concat(TAiCpu.Op_none(A_NOP));
  991. end;
  992. procedure TCGMIPS.a_jmp_always(List: tasmlist; l: TAsmLabel);
  993. var
  994. ai : Taicpu;
  995. begin
  996. ai := taicpu.op_sym(A_BA, l);
  997. list.concat(ai);
  998. { Delay slot }
  999. list.Concat(TAiCpu.Op_none(A_NOP));
  1000. end;
  1001. procedure TCGMIPS.a_jmp_name(list: tasmlist; const s: string);
  1002. begin
  1003. List.Concat(TAiCpu.op_sym(A_BA, current_asmdata.RefAsmSymbol(s,AT_FUNCTION)));
  1004. { Delay slot }
  1005. list.Concat(TAiCpu.Op_none(A_NOP));
  1006. end;
  1007. procedure TCGMIPS.a_jmp_flags(list: tasmlist; const f: TResFlags; l: tasmlabel);
  1008. var
  1009. ai: taicpu;
  1010. begin
  1011. case f.reg1 of
  1012. NR_FCC0..NR_FCC7:
  1013. begin
  1014. if (f.reg1=NR_FCC0) then
  1015. ai:=taicpu.op_sym(A_BC,l)
  1016. else
  1017. ai:=taicpu.op_reg_sym(A_BC,f.reg1,l);
  1018. list.concat(ai);
  1019. { delay slot }
  1020. list.concat(taicpu.op_none(A_NOP));
  1021. case f.cond of
  1022. OC_NE: ai.SetCondition(C_COP1TRUE);
  1023. OC_EQ: ai.SetCondition(C_COP1FALSE);
  1024. else
  1025. InternalError(2014082901);
  1026. end;
  1027. exit;
  1028. end;
  1029. else
  1030. ;
  1031. end;
  1032. if f.use_const then
  1033. a_cmp_const_reg_label(list,OS_INT,f.cond,f.value,f.reg1,l)
  1034. else
  1035. a_cmp_reg_reg_label(list,OS_INT,f.cond,f.reg2,f.reg1,l);
  1036. end;
  1037. procedure TCGMIPS.g_flags2reg(list: tasmlist; size: tcgsize; const f: tresflags; reg: tregister);
  1038. var
  1039. left,right: tregister;
  1040. unsigned: boolean;
  1041. hl: tasmlabel;
  1042. begin
  1043. case f.reg1 of
  1044. NR_FCC0..NR_FCC7:
  1045. begin
  1046. if (current_settings.cputype>=cpu_mips4) then
  1047. begin
  1048. a_load_const_reg(list,size,1,reg);
  1049. case f.cond of
  1050. OC_NE: list.concat(taicpu.op_reg_reg_reg(A_MOVF,reg,NR_R0,f.reg1));
  1051. OC_EQ: list.concat(taicpu.op_reg_reg_reg(A_MOVT,reg,NR_R0,f.reg1));
  1052. else
  1053. InternalError(2014082902);
  1054. end;
  1055. end
  1056. else
  1057. begin
  1058. { TODO: still possible to do branchless by extracting appropriate bit from FCSR? }
  1059. current_asmdata.getjumplabel(hl);
  1060. a_load_const_reg(list,size,1,reg);
  1061. a_jmp_flags(list,f,hl);
  1062. a_load_const_reg(list,size,0,reg);
  1063. a_label(list,hl);
  1064. end;
  1065. exit;
  1066. end;
  1067. else
  1068. ;
  1069. end;
  1070. if (f.cond in [OC_EQ,OC_NE]) then
  1071. begin
  1072. left:=reg;
  1073. if f.use_const and (f.value>=0) and (f.value<=65535) then
  1074. begin
  1075. if (f.value<>0) then
  1076. list.concat(taicpu.op_reg_reg_const(A_XORI,reg,f.reg1,f.value))
  1077. else
  1078. left:=f.reg1;
  1079. end
  1080. else
  1081. begin
  1082. if f.use_const then
  1083. begin
  1084. right:=GetIntRegister(list,OS_INT);
  1085. a_load_const_reg(list,OS_INT,f.value,right);
  1086. end
  1087. else
  1088. right:=f.reg2;
  1089. list.concat(taicpu.op_reg_reg_reg(A_XOR,reg,f.reg1,right));
  1090. end;
  1091. if f.cond=OC_EQ then
  1092. list.concat(taicpu.op_reg_reg_const(A_SLTIU,reg,left,1))
  1093. else
  1094. list.concat(taicpu.op_reg_reg_reg(A_SLTU,reg,NR_R0,left));
  1095. end
  1096. else
  1097. begin
  1098. {
  1099. sle x,a,b --> slt x,b,a; xori x,x,1 immediate not possible (or must be at left)
  1100. sgt x,a,b --> slt x,b,a likewise
  1101. sge x,a,b --> slt x,a,b; xori x,x,1
  1102. slt x,a,b --> unchanged
  1103. }
  1104. unsigned:=f.cond in [OC_GT,OC_LT,OC_GTE,OC_LTE];
  1105. if (f.cond in [OC_GTE,OC_LT,OC_B,OC_AE]) and
  1106. f.use_const and
  1107. (f.value>=simm16lo) and
  1108. (f.value<=simm16hi) then
  1109. list.Concat(taicpu.op_reg_reg_const(ops_slti[unsigned],reg,f.reg1,f.value))
  1110. else
  1111. begin
  1112. if f.use_const then
  1113. begin
  1114. if (f.value=0) then
  1115. right:=NR_R0
  1116. else
  1117. begin
  1118. right:=GetIntRegister(list,OS_INT);
  1119. a_load_const_reg(list,OS_INT,f.value,right);
  1120. end;
  1121. end
  1122. else
  1123. right:=f.reg2;
  1124. if (f.cond in [OC_LTE,OC_GT,OC_BE,OC_A]) then
  1125. list.Concat(taicpu.op_reg_reg_reg(ops_slt[unsigned],reg,right,f.reg1))
  1126. else
  1127. list.Concat(taicpu.op_reg_reg_reg(ops_slt[unsigned],reg,f.reg1,right));
  1128. end;
  1129. if (f.cond in [OC_LTE,OC_GTE,OC_BE,OC_AE]) then
  1130. list.Concat(taicpu.op_reg_reg_const(A_XORI,reg,reg,1));
  1131. end;
  1132. end;
  1133. procedure TCGMIPS.a_mul_reg_reg_pair(list: tasmlist; size: tcgsize; src1,src2,dstlo,dsthi: tregister);
  1134. var
  1135. asmop: tasmop;
  1136. begin
  1137. case size of
  1138. OS_32: asmop:=A_MULTU;
  1139. OS_S32: asmop:=A_MULT;
  1140. {$ifdef cpu64bitalu}
  1141. OS_64: asmop:=A_DMULTU;
  1142. OS_S64: asmop:=A_DMULT;
  1143. {$endif cpu64bitalu}
  1144. else
  1145. InternalError(2022020901);
  1146. end;
  1147. list.concat(taicpu.op_reg_reg(asmop,src1,src2));
  1148. if (dstlo<>NR_NO) then
  1149. list.concat(taicpu.op_reg(A_MFLO,dstlo));
  1150. if (dsthi<>NR_NO) then
  1151. list.concat(taicpu.op_reg(A_MFHI,dsthi));
  1152. end;
  1153. procedure TCGMIPS.g_overflowCheck(List: tasmlist; const Loc: TLocation; def: TDef);
  1154. begin
  1155. // this is an empty procedure
  1156. end;
  1157. procedure TCGMIPS.g_overflowCheck_loc(List: tasmlist; const Loc: TLocation; def: TDef; ovloc: tlocation);
  1158. begin
  1159. // this is an empty procedure
  1160. end;
  1161. { *********** entry/exit code and address loading ************ }
  1162. procedure FixupOffsets(p:TObject;arg:pointer);
  1163. var
  1164. sym: tabstractnormalvarsym absolute p;
  1165. begin
  1166. if (tsym(p).typ=paravarsym) and
  1167. (sym.localloc.loc=LOC_REFERENCE) and
  1168. (sym.localloc.reference.base=NR_FRAME_POINTER_REG) then
  1169. begin
  1170. sym.localloc.reference.base:=NR_STACK_POINTER_REG;
  1171. Inc(sym.localloc.reference.offset,PLongint(arg)^);
  1172. end;
  1173. end;
  1174. procedure TCGMIPS.g_proc_entry(list: tasmlist; localsize: longint; nostackframe: boolean);
  1175. var
  1176. lastintoffset,lastfpuoffset,
  1177. nextoffset : aint;
  1178. i : longint;
  1179. ra_save,framesave : taicpu;
  1180. fmask,mask : dword;
  1181. saveregs : tcpuregisterset;
  1182. href: treference;
  1183. reg : Tsuperregister;
  1184. helplist : TAsmList;
  1185. largeoffs : boolean;
  1186. begin
  1187. list.concat(tai_directive.create(asd_ent,current_procinfo.procdef.mangledname));
  1188. if nostackframe then
  1189. begin
  1190. list.concat(taicpu.op_none(A_P_SET_NOMIPS16));
  1191. list.concat(taicpu.op_none(A_P_SET_NOREORDER));
  1192. exit;
  1193. end;
  1194. helplist:=TAsmList.Create;
  1195. reference_reset(href,0,[]);
  1196. href.base:=NR_STACK_POINTER_REG;
  1197. fmask:=0;
  1198. nextoffset:=tcpuprocinfo(current_procinfo).floatregstart;
  1199. lastfpuoffset:=LocalSize;
  1200. for reg := RS_F0 to RS_F31 do { to check: what if F30 is double? }
  1201. begin
  1202. if reg in (rg[R_FPUREGISTER].used_in_proc-paramanager.get_volatile_registers_fpu(pocall_stdcall)) then
  1203. begin
  1204. fmask:=fmask or (longword(1) shl ord(reg));
  1205. href.offset:=nextoffset;
  1206. lastfpuoffset:=nextoffset;
  1207. helplist.concat(taicpu.op_reg_ref(A_SWC1,newreg(R_FPUREGISTER,reg,R_SUBFS),href));
  1208. inc(nextoffset,4);
  1209. { IEEE Double values are stored in floating point
  1210. register pairs f2X/f2X+1,
  1211. as the f2X+1 register is not correctly marked as used for now,
  1212. we simply assume it is also used if f2X is used
  1213. Should be fixed by a proper inclusion of f2X+1 into used_in_proc }
  1214. if (ord(reg)-ord(RS_F0)) mod 2 = 0 then
  1215. include(rg[R_FPUREGISTER].used_in_proc,succ(reg));
  1216. end;
  1217. end;
  1218. mask:=0;
  1219. nextoffset:=tcpuprocinfo(current_procinfo).intregstart;
  1220. saveregs:=rg[R_INTREGISTER].used_in_proc-paramanager.get_volatile_registers_int(pocall_stdcall);
  1221. if (current_procinfo.flags*[pi_do_call,pi_is_assembler]<>[]) then
  1222. include(saveregs,RS_R31);
  1223. if (pi_needs_stackframe in current_procinfo.flags) then
  1224. include(saveregs,RS_FRAME_POINTER_REG);
  1225. lastintoffset:=LocalSize;
  1226. framesave:=nil;
  1227. ra_save:=nil;
  1228. for reg:=RS_R1 to RS_R31 do
  1229. begin
  1230. if reg in saveregs then
  1231. begin
  1232. mask:=mask or (longword(1) shl ord(reg));
  1233. href.offset:=nextoffset;
  1234. lastintoffset:=nextoffset;
  1235. if (reg=RS_FRAME_POINTER_REG) then
  1236. framesave:=taicpu.op_reg_ref(A_SW,newreg(R_INTREGISTER,reg,R_SUBWHOLE),href)
  1237. else if (reg=RS_R31) then
  1238. ra_save:=taicpu.op_reg_ref(A_SW,newreg(R_INTREGISTER,reg,R_SUBWHOLE),href)
  1239. else
  1240. helplist.concat(taicpu.op_reg_ref(A_SW,newreg(R_INTREGISTER,reg,R_SUBWHOLE),href));
  1241. inc(nextoffset,4);
  1242. end;
  1243. end;
  1244. //list.concat(Taicpu.Op_reg_reg_const(A_ADDIU,NR_FRAME_POINTER_REG,NR_STACK_POINTER_REG,current_procinfo.para_stack_size));
  1245. list.concat(Taicpu.op_none(A_P_SET_NOMIPS16));
  1246. list.concat(Taicpu.op_reg_const_reg(A_P_FRAME,current_procinfo.framepointer,LocalSize,NR_R31));
  1247. list.concat(Taicpu.op_const_const(A_P_MASK,aint(mask),-(LocalSize-lastintoffset)));
  1248. list.concat(Taicpu.op_const_const(A_P_FMASK,aint(Fmask),-(LocalSize-lastfpuoffset)));
  1249. list.concat(Taicpu.op_none(A_P_SET_NOREORDER));
  1250. if tcpuprocinfo(current_procinfo).setnoat then
  1251. list.concat(Taicpu.op_none(A_P_SET_NOAT));
  1252. if (cs_create_pic in current_settings.moduleswitches) and
  1253. (pi_needs_got in current_procinfo.flags) then
  1254. begin
  1255. list.concat(Taicpu.op_reg(A_P_CPLOAD,NR_PIC_FUNC));
  1256. end;
  1257. if (-LocalSize >= simm16lo) and (-LocalSize <= simm16hi) then
  1258. begin
  1259. list.concat(Taicpu.op_none(A_P_SET_NOMACRO));
  1260. list.concat(Taicpu.Op_reg_reg_const(A_ADDIU,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,-LocalSize));
  1261. if assigned(ra_save) then
  1262. list.concat(ra_save);
  1263. if assigned(framesave) then
  1264. begin
  1265. list.concat(framesave);
  1266. list.concat(Taicpu.op_reg_reg_const(A_ADDIU,NR_FRAME_POINTER_REG,
  1267. NR_STACK_POINTER_REG,LocalSize));
  1268. end;
  1269. end
  1270. else
  1271. begin
  1272. a_load_const_reg(list,OS_32,-LocalSize,NR_R9);
  1273. list.concat(Taicpu.Op_reg_reg_reg(A_ADDU,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,NR_R9));
  1274. if assigned(ra_save) then
  1275. list.concat(ra_save);
  1276. if assigned(framesave) then
  1277. begin
  1278. list.concat(framesave);
  1279. list.concat(Taicpu.op_reg_reg_reg(A_SUBU,NR_FRAME_POINTER_REG,
  1280. NR_STACK_POINTER_REG,NR_R9));
  1281. end;
  1282. { The instructions before are macros that can extend to multiple instructions,
  1283. the settings of R9 to -LocalSize surely does,
  1284. but the saving of RA and FP also might, and might
  1285. even use AT register, which is why we use R9 instead of AT here for -LocalSize }
  1286. list.concat(Taicpu.op_none(A_P_SET_NOMACRO));
  1287. end;
  1288. if (cs_create_pic in current_settings.moduleswitches) and
  1289. (pi_needs_got in current_procinfo.flags) then
  1290. begin
  1291. largeoffs:=(tcpuprocinfo(current_procinfo).save_gp_ref.offset>simm16hi);
  1292. if largeoffs then
  1293. list.concat(Taicpu.op_none(A_P_SET_MACRO));
  1294. list.concat(Taicpu.op_const(A_P_CPRESTORE,tcpuprocinfo(current_procinfo).save_gp_ref.offset));
  1295. if largeoffs then
  1296. list.concat(Taicpu.op_none(A_P_SET_NOMACRO));
  1297. end;
  1298. href.base:=NR_STACK_POINTER_REG;
  1299. for i:=0 to MIPS_MAX_REGISTERS_USED_IN_CALL-1 do
  1300. if tcpuprocinfo(current_procinfo).register_used[i] then
  1301. begin
  1302. reg:=parasupregs[i];
  1303. href.offset:=i*sizeof(aint)+LocalSize;
  1304. list.concat(taicpu.op_reg_ref(A_SW, newreg(R_INTREGISTER,reg,R_SUBWHOLE), href));
  1305. end;
  1306. list.concatList(helplist);
  1307. helplist.Free;
  1308. if current_procinfo.has_nestedprocs then
  1309. current_procinfo.procdef.parast.SymList.ForEachCall(@FixupOffsets,@LocalSize);
  1310. end;
  1311. procedure TCGMIPS.g_proc_exit(list: tasmlist; parasize: longint; nostackframe: boolean);
  1312. var
  1313. href : treference;
  1314. stacksize : aint;
  1315. saveregs : tcpuregisterset;
  1316. nextoffset : aint;
  1317. reg : Tsuperregister;
  1318. begin
  1319. stacksize:=current_procinfo.calc_stackframe_size;
  1320. if nostackframe then
  1321. begin
  1322. list.concat(taicpu.op_reg(A_JR, NR_R31));
  1323. list.concat(Taicpu.op_none(A_NOP));
  1324. list.concat(Taicpu.op_none(A_P_SET_MACRO));
  1325. list.concat(Taicpu.op_none(A_P_SET_REORDER));
  1326. end
  1327. else
  1328. begin
  1329. if tcpuprocinfo(current_procinfo).save_gp_ref.offset<>0 then
  1330. tg.ungettemp(list,tcpuprocinfo(current_procinfo).save_gp_ref);
  1331. reference_reset(href,0,[]);
  1332. href.base:=NR_STACK_POINTER_REG;
  1333. nextoffset:=tcpuprocinfo(current_procinfo).floatregstart;
  1334. for reg := RS_F0 to RS_F31 do
  1335. begin
  1336. if reg in (rg[R_FPUREGISTER].used_in_proc-paramanager.get_volatile_registers_fpu(pocall_stdcall)) then
  1337. begin
  1338. href.offset:=nextoffset;
  1339. list.concat(taicpu.op_reg_ref(A_LWC1,newreg(R_FPUREGISTER,reg,R_SUBFS),href));
  1340. inc(nextoffset,4);
  1341. end;
  1342. end;
  1343. nextoffset:=tcpuprocinfo(current_procinfo).intregstart;
  1344. saveregs:=rg[R_INTREGISTER].used_in_proc-paramanager.get_volatile_registers_int(pocall_stdcall);
  1345. if (current_procinfo.flags*[pi_do_call,pi_is_assembler]<>[]) then
  1346. include(saveregs,RS_R31);
  1347. if (pi_needs_stackframe in current_procinfo.flags) then
  1348. include(saveregs,RS_FRAME_POINTER_REG);
  1349. // GP does not need to be restored on exit
  1350. for reg:=RS_R1 to RS_R31 do
  1351. begin
  1352. if reg in saveregs then
  1353. begin
  1354. href.offset:=nextoffset;
  1355. list.concat(taicpu.op_reg_ref(A_LW,newreg(R_INTREGISTER,reg,R_SUBWHOLE),href));
  1356. inc(nextoffset,sizeof(aint));
  1357. end;
  1358. end;
  1359. if (-stacksize >= simm16lo) and (-stacksize <= simm16hi) then
  1360. begin
  1361. list.concat(taicpu.op_reg(A_JR, NR_R31));
  1362. { correct stack pointer in the delay slot }
  1363. list.concat(Taicpu.Op_reg_reg_const(A_ADDIU, NR_STACK_POINTER_REG, NR_STACK_POINTER_REG, stacksize));
  1364. end
  1365. else
  1366. begin
  1367. a_load_const_reg(list,OS_32,stacksize,NR_R1);
  1368. list.concat(taicpu.op_reg(A_JR, NR_R31));
  1369. { correct stack pointer in the delay slot }
  1370. list.concat(taicpu.op_reg_reg_reg(A_ADD,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,NR_R1));
  1371. tcpuprocinfo(current_procinfo).setnoat:=true;
  1372. end;
  1373. list.concat(Taicpu.op_none(A_P_SET_MACRO));
  1374. list.concat(Taicpu.op_none(A_P_SET_REORDER));
  1375. end;
  1376. list.concat(tai_directive.create(asd_ent_end,current_procinfo.procdef.mangledname));
  1377. end;
  1378. { ************* concatcopy ************ }
  1379. procedure TCGMIPS.g_concatcopy_move(list: tasmlist; const Source, dest: treference; len: tcgint);
  1380. var
  1381. paraloc1, paraloc2, paraloc3: TCGPara;
  1382. pd: tprocdef;
  1383. begin
  1384. pd:=search_system_proc('MOVE');
  1385. paraloc1.init;
  1386. paraloc2.init;
  1387. paraloc3.init;
  1388. paramanager.getcgtempparaloc(list, pd, 1, paraloc1);
  1389. paramanager.getcgtempparaloc(list, pd, 2, paraloc2);
  1390. paramanager.getcgtempparaloc(list, pd, 3, paraloc3);
  1391. a_load_const_cgpara(list, OS_SINT, len, paraloc3);
  1392. a_loadaddr_ref_cgpara(list, dest, paraloc2);
  1393. a_loadaddr_ref_cgpara(list, Source, paraloc1);
  1394. paramanager.freecgpara(list, paraloc3);
  1395. paramanager.freecgpara(list, paraloc2);
  1396. paramanager.freecgpara(list, paraloc1);
  1397. alloccpuregisters(list, R_INTREGISTER, paramanager.get_volatile_registers_int(pocall_default));
  1398. alloccpuregisters(list, R_FPUREGISTER, paramanager.get_volatile_registers_fpu(pocall_default));
  1399. a_call_name(list, 'FPC_MOVE', false);
  1400. dealloccpuregisters(list, R_FPUREGISTER, paramanager.get_volatile_registers_fpu(pocall_default));
  1401. dealloccpuregisters(list, R_INTREGISTER, paramanager.get_volatile_registers_int(pocall_default));
  1402. paraloc3.done;
  1403. paraloc2.done;
  1404. paraloc1.done;
  1405. end;
  1406. procedure TCGMIPS.g_concatcopy(list: tasmlist; const Source, dest: treference; len: tcgint);
  1407. var
  1408. tmpreg1, hreg, countreg: TRegister;
  1409. src, dst: TReference;
  1410. lab: tasmlabel;
  1411. Count, count2: aint;
  1412. function reference_is_reusable(const ref: treference): boolean;
  1413. begin
  1414. result:=(ref.base<>NR_NO) and (ref.index=NR_NO) and
  1415. (ref.symbol=nil) and
  1416. (ref.offset>=simm16lo) and (ref.offset+len<=simm16hi);
  1417. end;
  1418. begin
  1419. if len > high(longint) then
  1420. internalerror(2002072704);
  1421. { A call (to FPC_MOVE) requires the outgoing parameter area to be properly
  1422. allocated on stack. This can only be done before tcpuprocinfo.set_first_temp_offset,
  1423. i.e. before secondpass. Other internal procedures request correct stack frame
  1424. by setting pi_do_call during firstpass, but for this particular one it is impossible.
  1425. Therefore, if the current procedure is a leaf one, we have to leave it that way. }
  1426. { anybody wants to determine a good value here :)? }
  1427. if (len > 100) and
  1428. assigned(current_procinfo) and
  1429. (pi_do_call in current_procinfo.flags) then
  1430. g_concatcopy_move(list, Source, dest, len)
  1431. else
  1432. begin
  1433. Count := len div 4;
  1434. if (count<=4) and reference_is_reusable(source) then
  1435. src:=source
  1436. else
  1437. begin
  1438. reference_reset(src,sizeof(aint),source.volatility);
  1439. { load the address of source into src.base }
  1440. src.base := GetAddressRegister(list);
  1441. a_loadaddr_ref_reg(list, Source, src.base);
  1442. end;
  1443. if (count<=4) and reference_is_reusable(dest) then
  1444. dst:=dest
  1445. else
  1446. begin
  1447. reference_reset(dst,sizeof(aint),dest.volatility);
  1448. { load the address of dest into dst.base }
  1449. dst.base := GetAddressRegister(list);
  1450. a_loadaddr_ref_reg(list, dest, dst.base);
  1451. end;
  1452. { generate a loop }
  1453. if Count > 4 then
  1454. begin
  1455. countreg := GetIntRegister(list, OS_INT);
  1456. tmpreg1 := GetIntRegister(list, OS_INT);
  1457. a_load_const_reg(list, OS_INT, Count, countreg);
  1458. current_asmdata.getjumplabel(lab);
  1459. a_label(list, lab);
  1460. list.concat(taicpu.op_reg_ref(A_LW, tmpreg1, src));
  1461. list.concat(taicpu.op_reg_ref(A_SW, tmpreg1, dst));
  1462. list.concat(taicpu.op_reg_reg_const(A_ADDIU, src.base, src.base, 4));
  1463. list.concat(taicpu.op_reg_reg_const(A_ADDIU, dst.base, dst.base, 4));
  1464. list.concat(taicpu.op_reg_reg_const(A_ADDIU, countreg, countreg, -1));
  1465. a_cmp_reg_reg_label(list,OS_INT,OC_GT,NR_R0,countreg,lab);
  1466. len := len mod 4;
  1467. end;
  1468. { unrolled loop }
  1469. Count := len div 4;
  1470. if Count > 0 then
  1471. begin
  1472. tmpreg1 := GetIntRegister(list, OS_INT);
  1473. count2:=1;
  1474. while count2 <= Count do
  1475. begin
  1476. list.concat(taicpu.op_reg_ref(A_LW, tmpreg1, src));
  1477. list.concat(taicpu.op_reg_ref(A_SW, tmpreg1, dst));
  1478. Inc(src.offset, 4);
  1479. Inc(dst.offset, 4);
  1480. Inc(count2);
  1481. end;
  1482. len := len mod 4;
  1483. end;
  1484. if (len and 4) <> 0 then
  1485. begin
  1486. hreg := GetIntRegister(list, OS_INT);
  1487. a_load_ref_reg(list, OS_32, OS_32, src, hreg);
  1488. a_load_reg_ref(list, OS_32, OS_32, hreg, dst);
  1489. Inc(src.offset, 4);
  1490. Inc(dst.offset, 4);
  1491. end;
  1492. { copy the leftovers }
  1493. if (len and 2) <> 0 then
  1494. begin
  1495. hreg := GetIntRegister(list, OS_INT);
  1496. a_load_ref_reg(list, OS_16, OS_16, src, hreg);
  1497. a_load_reg_ref(list, OS_16, OS_16, hreg, dst);
  1498. Inc(src.offset, 2);
  1499. Inc(dst.offset, 2);
  1500. end;
  1501. if (len and 1) <> 0 then
  1502. begin
  1503. hreg := GetIntRegister(list, OS_INT);
  1504. a_load_ref_reg(list, OS_8, OS_8, src, hreg);
  1505. a_load_reg_ref(list, OS_8, OS_8, hreg, dst);
  1506. end;
  1507. end;
  1508. end;
  1509. procedure TCGMIPS.g_concatcopy_unaligned(list: tasmlist; const Source, dest: treference; len: tcgint);
  1510. var
  1511. src, dst: TReference;
  1512. tmpreg1, countreg: TRegister;
  1513. i: aint;
  1514. lab: tasmlabel;
  1515. begin
  1516. if (len > 31) and
  1517. { see comment in g_concatcopy }
  1518. assigned(current_procinfo) and
  1519. (pi_do_call in current_procinfo.flags) then
  1520. g_concatcopy_move(list, Source, dest, len)
  1521. else
  1522. begin
  1523. reference_reset(src,sizeof(aint),source.volatility);
  1524. reference_reset(dst,sizeof(aint),dest.volatility);
  1525. { load the address of source into src.base }
  1526. src.base := GetAddressRegister(list);
  1527. a_loadaddr_ref_reg(list, Source, src.base);
  1528. { load the address of dest into dst.base }
  1529. dst.base := GetAddressRegister(list);
  1530. a_loadaddr_ref_reg(list, dest, dst.base);
  1531. { generate a loop }
  1532. if len > 4 then
  1533. begin
  1534. countreg := GetIntRegister(list, OS_INT);
  1535. tmpreg1 := GetIntRegister(list, OS_INT);
  1536. a_load_const_reg(list, OS_INT, len, countreg);
  1537. current_asmdata.getjumplabel(lab);
  1538. a_label(list, lab);
  1539. list.concat(taicpu.op_reg_ref(A_LBU, tmpreg1, src));
  1540. list.concat(taicpu.op_reg_ref(A_SB, tmpreg1, dst));
  1541. list.concat(taicpu.op_reg_reg_const(A_ADDIU, src.base, src.base, 1));
  1542. list.concat(taicpu.op_reg_reg_const(A_ADDIU, dst.base, dst.base, 1));
  1543. list.concat(taicpu.op_reg_reg_const(A_ADDIU, countreg, countreg, -1));
  1544. a_cmp_reg_reg_label(list,OS_INT,OC_GT,NR_R0,countreg,lab);
  1545. end
  1546. else
  1547. begin
  1548. { unrolled loop }
  1549. tmpreg1 := GetIntRegister(list, OS_INT);
  1550. i := 1;
  1551. while i <= len do
  1552. begin
  1553. list.concat(taicpu.op_reg_ref(A_LBU, tmpreg1, src));
  1554. list.concat(taicpu.op_reg_ref(A_SB, tmpreg1, dst));
  1555. Inc(src.offset);
  1556. Inc(dst.offset);
  1557. Inc(i);
  1558. end;
  1559. end;
  1560. end;
  1561. end;
  1562. procedure TCGMIPS.g_profilecode(list:TAsmList);
  1563. var
  1564. href: treference;
  1565. begin
  1566. if not (cs_create_pic in current_settings.moduleswitches) then
  1567. begin
  1568. reference_reset_symbol(href,current_asmdata.RefAsmSymbol('_gp',AT_DATA),0,sizeof(pint),[]);
  1569. a_loadaddr_ref_reg(list,href,NR_GP);
  1570. end;
  1571. list.concat(taicpu.op_reg_reg(A_MOVE,NR_R1,NR_RA));
  1572. list.concat(taicpu.op_reg_reg_const(A_ADDIU,NR_SP,NR_SP,-8));
  1573. a_call_sym_pic(list,current_asmdata.RefAsmSymbol('_mcount',AT_FUNCTION));
  1574. tcpuprocinfo(current_procinfo).setnoat:=true;
  1575. end;
  1576. procedure TCGMIPS.g_adjust_self_value(list:TAsmList;procdef: tprocdef;ioffset: tcgint);
  1577. begin
  1578. { This method is integrated into g_intf_wrapper and shouldn't be called separately }
  1579. InternalError(2013020102);
  1580. end;
  1581. {$ifndef mips64}
  1582. {****************************************************************************
  1583. TCG64_MIPSel
  1584. ****************************************************************************}
  1585. procedure TCg64MPSel.a_load64_reg_ref(list: tasmlist; reg: tregister64; const ref: treference);
  1586. var
  1587. tmpref: treference;
  1588. tmpreg: tregister;
  1589. begin
  1590. if target_info.endian = endian_big then
  1591. begin
  1592. tmpreg := reg.reglo;
  1593. reg.reglo := reg.reghi;
  1594. reg.reghi := tmpreg;
  1595. end;
  1596. tmpref := ref;
  1597. tcgmips(cg).make_simple_ref(list,tmpref);
  1598. list.concat(taicpu.op_reg_ref(A_SW,reg.reglo,tmpref));
  1599. Inc(tmpref.offset, 4);
  1600. list.concat(taicpu.op_reg_ref(A_SW,reg.reghi,tmpref));
  1601. end;
  1602. procedure TCg64MPSel.a_load64_ref_reg(list: tasmlist; const ref: treference; reg: tregister64);
  1603. var
  1604. tmpref: treference;
  1605. tmpreg: tregister;
  1606. begin
  1607. if target_info.endian = endian_big then
  1608. begin
  1609. tmpreg := reg.reglo;
  1610. reg.reglo := reg.reghi;
  1611. reg.reghi := tmpreg;
  1612. end;
  1613. tmpref := ref;
  1614. tcgmips(cg).make_simple_ref(list,tmpref);
  1615. list.concat(taicpu.op_reg_ref(A_LW,reg.reglo,tmpref));
  1616. Inc(tmpref.offset, 4);
  1617. list.concat(taicpu.op_reg_ref(A_LW,reg.reghi,tmpref));
  1618. end;
  1619. procedure TCg64MPSel.a_load64_ref_cgpara(list: tasmlist; const r: treference; const paraloc: tcgpara);
  1620. var
  1621. hreg64: tregister64;
  1622. begin
  1623. { Override this function to prevent loading the reference twice.
  1624. Use here some extra registers, but those are optimized away by the RA }
  1625. hreg64.reglo := cg.GetIntRegister(list, OS_S32);
  1626. hreg64.reghi := cg.GetIntRegister(list, OS_S32);
  1627. a_load64_ref_reg(list, r, hreg64);
  1628. a_load64_reg_cgpara(list, hreg64, paraloc);
  1629. end;
  1630. procedure TCg64MPSel.a_op64_reg_reg(list: tasmlist; op: TOpCG; size: tcgsize; regsrc, regdst: TRegister64);
  1631. var
  1632. tmpreg1: TRegister;
  1633. begin
  1634. case op of
  1635. OP_NEG:
  1636. begin
  1637. tmpreg1 := cg.GetIntRegister(list, OS_INT);
  1638. list.concat(taicpu.op_reg_reg_reg(A_SUBU, regdst.reglo, NR_R0, regsrc.reglo));
  1639. list.concat(taicpu.op_reg_reg_reg(A_SLTU, tmpreg1, NR_R0, regdst.reglo));
  1640. list.concat(taicpu.op_reg_reg_reg(A_SUBU, regdst.reghi, NR_R0, regsrc.reghi));
  1641. list.concat(taicpu.op_reg_reg_reg(A_SUBU, regdst.reghi, regdst.reghi, tmpreg1));
  1642. end;
  1643. OP_NOT:
  1644. begin
  1645. list.concat(taicpu.op_reg_reg_reg(A_NOR, regdst.reglo, NR_R0, regsrc.reglo));
  1646. list.concat(taicpu.op_reg_reg_reg(A_NOR, regdst.reghi, NR_R0, regsrc.reghi));
  1647. end;
  1648. else
  1649. a_op64_reg_reg_reg(list,op,size,regsrc,regdst,regdst);
  1650. end;
  1651. end;
  1652. procedure TCg64MPSel.a_op64_const_reg(list: tasmlist; op: TOpCG; size: tcgsize; Value: int64; regdst: TRegister64);
  1653. begin
  1654. a_op64_const_reg_reg(list, op, size, value, regdst, regdst);
  1655. end;
  1656. procedure TCg64MPSel.a_op64_const_reg_reg(list: tasmlist; op: TOpCG; size: tcgsize; Value: int64; regsrc, regdst: tregister64);
  1657. var
  1658. l: tlocation;
  1659. begin
  1660. a_op64_const_reg_reg_checkoverflow(list, op, size, Value, regsrc, regdst, False, l);
  1661. end;
  1662. procedure TCg64MPSel.a_op64_reg_reg_reg(list: tasmlist; op: TOpCG; size: tcgsize; regsrc1, regsrc2, regdst: tregister64);
  1663. var
  1664. l: tlocation;
  1665. begin
  1666. a_op64_reg_reg_reg_checkoverflow(list, op, size, regsrc1, regsrc2, regdst, False, l);
  1667. end;
  1668. procedure TCg64MPSel.a_op64_const_reg_reg_checkoverflow(list: tasmlist; op: TOpCG; size: tcgsize; Value: int64; regsrc, regdst: tregister64; setflags: boolean; var ovloc: tlocation);
  1669. var
  1670. tmplo,carry: TRegister;
  1671. hisize: tcgsize;
  1672. begin
  1673. carry:=NR_NO;
  1674. if (size in [OS_S64]) then
  1675. hisize:=OS_S32
  1676. else
  1677. hisize:=OS_32;
  1678. case op of
  1679. OP_AND,OP_OR,OP_XOR:
  1680. begin
  1681. cg.a_op_const_reg_reg(list,op,OS_32,aint(lo(value)),regsrc.reglo,regdst.reglo);
  1682. cg.a_op_const_reg_reg(list,op,OS_32,aint(hi(value)),regsrc.reghi,regdst.reghi);
  1683. end;
  1684. OP_ADD:
  1685. begin
  1686. if lo(value)<>0 then
  1687. begin
  1688. tmplo:=cg.GetIntRegister(list,OS_32);
  1689. carry:=cg.GetIntRegister(list,OS_32);
  1690. tcgmips(cg).handle_reg_const_reg(list,A_ADDU,regsrc.reglo,aint(lo(value)),tmplo);
  1691. list.concat(taicpu.op_reg_reg_reg(A_SLTU,carry,tmplo,regsrc.reglo));
  1692. cg.a_load_reg_reg(list,OS_32,OS_32,tmplo,regdst.reglo);
  1693. end
  1694. else
  1695. cg.a_load_reg_reg(list,OS_32,OS_32,regsrc.reglo,regdst.reglo);
  1696. { With overflow checking and unsigned args, this generates slighly suboptimal code
  1697. ($80000000 constant loaded twice). Other cases are fine. Getting it perfect does not
  1698. look worth the effort. }
  1699. cg.a_op_const_reg_reg_checkoverflow(list,OP_ADD,hisize,aint(hi(value)),regsrc.reghi,regdst.reghi,setflags,ovloc);
  1700. if carry<>NR_NO then
  1701. cg.a_op_reg_reg_reg_checkoverflow(list,OP_ADD,hisize,carry,regdst.reghi,regdst.reghi,setflags,ovloc);
  1702. end;
  1703. OP_SUB:
  1704. begin
  1705. carry:=NR_NO;
  1706. if lo(value)<>0 then
  1707. begin
  1708. tmplo:=cg.GetIntRegister(list,OS_32);
  1709. carry:=cg.GetIntRegister(list,OS_32);
  1710. tcgmips(cg).handle_reg_const_reg(list,A_SUBU,regsrc.reglo,aint(lo(value)),tmplo);
  1711. list.concat(taicpu.op_reg_reg_reg(A_SLTU,carry,regsrc.reglo,tmplo));
  1712. cg.a_load_reg_reg(list,OS_32,OS_32,tmplo,regdst.reglo);
  1713. end
  1714. else
  1715. cg.a_load_reg_reg(list,OS_32,OS_32,regsrc.reglo,regdst.reglo);
  1716. cg.a_op_const_reg_reg_checkoverflow(list,OP_SUB,hisize,aint(hi(value)),regsrc.reghi,regdst.reghi,setflags,ovloc);
  1717. if carry<>NR_NO then
  1718. cg.a_op_reg_reg_reg_checkoverflow(list,OP_SUB,hisize,carry,regdst.reghi,regdst.reghi,setflags,ovloc);
  1719. end;
  1720. else
  1721. InternalError(2013050301);
  1722. end;
  1723. end;
  1724. procedure TCg64MPSel.a_op64_reg_reg_reg_checkoverflow(list: tasmlist; op: TOpCG; size: tcgsize; regsrc1, regsrc2, regdst: tregister64; setflags: boolean; var ovloc: tlocation);
  1725. var
  1726. tmplo,tmphi,carry,hreg: TRegister;
  1727. signed: boolean;
  1728. begin
  1729. case op of
  1730. OP_ADD:
  1731. begin
  1732. signed:=(size in [OS_S64]);
  1733. tmplo := cg.GetIntRegister(list,OS_S32);
  1734. carry := cg.GetIntRegister(list,OS_S32);
  1735. // destreg.reglo could be regsrc1.reglo or regsrc2.reglo
  1736. list.concat(taicpu.op_reg_reg_reg(A_ADDU, tmplo, regsrc2.reglo, regsrc1.reglo));
  1737. list.concat(taicpu.op_reg_reg_reg(A_SLTU, carry, tmplo, regsrc2.reglo));
  1738. cg.a_load_reg_reg(list,OS_INT,OS_INT,tmplo,regdst.reglo);
  1739. if signed or (not setflags) then
  1740. begin
  1741. list.concat(taicpu.op_reg_reg_reg(ops_add[setflags and signed], regdst.reghi, regsrc2.reghi, regsrc1.reghi));
  1742. list.concat(taicpu.op_reg_reg_reg(ops_add[setflags and signed], regdst.reghi, regdst.reghi, carry));
  1743. end
  1744. else
  1745. begin
  1746. tmphi:=cg.GetIntRegister(list,OS_INT);
  1747. hreg:=cg.GetIntRegister(list,OS_INT);
  1748. cg.a_load_const_reg(list,OS_INT,$80000000,hreg);
  1749. // first add carry to one of the addends
  1750. list.concat(taicpu.op_reg_reg_reg(A_ADDU, tmphi, regsrc2.reghi, carry));
  1751. list.concat(taicpu.op_reg_reg_reg(A_SLTU, carry, tmphi, regsrc2.reghi));
  1752. list.concat(taicpu.op_reg_reg_reg(A_SUB, carry, hreg, carry));
  1753. // then add another addend
  1754. list.concat(taicpu.op_reg_reg_reg(A_ADDU, regdst.reghi, tmphi, regsrc1.reghi));
  1755. list.concat(taicpu.op_reg_reg_reg(A_SLTU, carry, regdst.reghi, tmphi));
  1756. list.concat(taicpu.op_reg_reg_reg(A_SUB, carry, hreg, carry));
  1757. end;
  1758. end;
  1759. OP_SUB:
  1760. begin
  1761. signed:=(size in [OS_S64]);
  1762. tmplo := cg.GetIntRegister(list,OS_S32);
  1763. carry := cg.GetIntRegister(list,OS_S32);
  1764. // destreg.reglo could be regsrc1.reglo or regsrc2.reglo
  1765. list.concat(taicpu.op_reg_reg_reg(A_SUBU, tmplo, regsrc2.reglo, regsrc1.reglo));
  1766. list.concat(taicpu.op_reg_reg_reg(A_SLTU, carry, regsrc2.reglo,tmplo));
  1767. cg.a_load_reg_reg(list,OS_INT,OS_INT,tmplo,regdst.reglo);
  1768. if signed or (not setflags) then
  1769. begin
  1770. list.concat(taicpu.op_reg_reg_reg(ops_sub[setflags and signed], regdst.reghi, regsrc2.reghi, regsrc1.reghi));
  1771. list.concat(taicpu.op_reg_reg_reg(ops_sub[setflags and signed], regdst.reghi, regdst.reghi, carry));
  1772. end
  1773. else
  1774. begin
  1775. tmphi:=cg.GetIntRegister(list,OS_INT);
  1776. hreg:=cg.GetIntRegister(list,OS_INT);
  1777. cg.a_load_const_reg(list,OS_INT,$80000000,hreg);
  1778. // first subtract the carry...
  1779. list.concat(taicpu.op_reg_reg_reg(A_SUBU, tmphi, regsrc2.reghi, carry));
  1780. list.concat(taicpu.op_reg_reg_reg(A_SLTU, carry, regsrc2.reghi, tmphi));
  1781. list.concat(taicpu.op_reg_reg_reg(A_SUB, carry, hreg, carry));
  1782. // ...then the subtrahend
  1783. list.concat(taicpu.op_reg_reg_reg(A_SUBU, regdst.reghi, tmphi, regsrc1.reghi));
  1784. list.concat(taicpu.op_reg_reg_reg(A_SLTU, carry, tmphi, regdst.reghi));
  1785. list.concat(taicpu.op_reg_reg_reg(A_SUB, carry, hreg, carry));
  1786. end;
  1787. end;
  1788. OP_AND,OP_OR,OP_XOR:
  1789. begin
  1790. cg.a_op_reg_reg_reg(list,op,size,regsrc1.reglo,regsrc2.reglo,regdst.reglo);
  1791. cg.a_op_reg_reg_reg(list,op,size,regsrc1.reghi,regsrc2.reghi,regdst.reghi);
  1792. end;
  1793. else
  1794. internalerror(200306017);
  1795. end;
  1796. end;
  1797. {$endif mips64}
  1798. procedure create_codegen;
  1799. begin
  1800. cg:=TCGMIPS.Create;
  1801. {$ifndef mips64}
  1802. cg64:=TCg64MPSel.Create;
  1803. {$endif mips64}
  1804. end;
  1805. end.