cgcpu.pas 72 KB

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