cgcpu.pas 66 KB

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