cgcpu.pas 66 KB

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