cgcpu.pas 66 KB

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