cgcpu.pas 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131
  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. ai := taicpu.op_sym(A_BA, l);
  1225. list.concat(ai);
  1226. { Delay slot }
  1227. list.Concat(TAiCpu.Op_none(A_NOP));
  1228. end;
  1229. procedure TCGMIPS.a_jmp_name(list: tasmlist; const s: string);
  1230. begin
  1231. List.Concat(TAiCpu.op_sym(A_BA, current_asmdata.RefAsmSymbol(s)));
  1232. { Delay slot }
  1233. list.Concat(TAiCpu.Op_none(A_NOP));
  1234. end;
  1235. procedure TCGMIPS.a_jmp_cond(list: tasmlist; cond: TOpCmp; l: TAsmLabel);
  1236. begin
  1237. internalerror(200701181);
  1238. end;
  1239. procedure TCGMIPS.g_overflowCheck(List: tasmlist; const Loc: TLocation; def: TDef);
  1240. begin
  1241. // this is an empty procedure
  1242. end;
  1243. procedure TCGMIPS.g_overflowCheck_loc(List: tasmlist; const Loc: TLocation; def: TDef; ovloc: tlocation);
  1244. begin
  1245. // this is an empty procedure
  1246. end;
  1247. { *********** entry/exit code and address loading ************ }
  1248. procedure TCGMIPS.g_proc_entry(list: tasmlist; localsize: longint; nostackframe: boolean);
  1249. var
  1250. lastintoffset,lastfpuoffset,
  1251. nextoffset : aint;
  1252. i : longint;
  1253. ra_save,framesave,gp_save : taicpu;
  1254. fmask,mask : dword;
  1255. saveregs : tcpuregisterset;
  1256. StoreOp : TAsmOp;
  1257. href: treference;
  1258. usesfpr, usesgpr, gotgot : boolean;
  1259. reg : Tsuperregister;
  1260. helplist : TAsmList;
  1261. begin
  1262. a_reg_alloc(list,NR_STACK_POINTER_REG);
  1263. if nostackframe then
  1264. exit;
  1265. if (TMIPSProcinfo(current_procinfo).needs_frame_pointer) then
  1266. a_reg_alloc(list,NR_FRAME_POINTER_REG);
  1267. helplist:=TAsmList.Create;
  1268. cgcpu_calc_stackframe_size := LocalSize;
  1269. reference_reset(href,0);
  1270. href.base:=NR_STACK_POINTER_REG;
  1271. usesfpr:=false;
  1272. fmask:=0;
  1273. nextoffset:=TMIPSProcInfo(current_procinfo).floatregstart;
  1274. lastfpuoffset:=LocalSize;
  1275. for reg := RS_F0 to RS_F30 do { to check: what if F30 is double? }
  1276. begin
  1277. if reg in (rg[R_FPUREGISTER].used_in_proc-paramanager.get_volatile_registers_fpu(pocall_stdcall)) then
  1278. begin
  1279. usesfpr:=true;
  1280. fmask:=fmask or (1 shl ord(reg));
  1281. href.offset:=nextoffset;
  1282. lastfpuoffset:=nextoffset;
  1283. if cs_asm_source in current_settings.globalswitches then
  1284. helplist.concat(tai_comment.Create(strpnew(std_regname(newreg(R_FPUREGISTER,reg,R_SUBFS))+' register saved.')));
  1285. helplist.concat(taicpu.op_reg_ref(A_SWC1,newreg(R_FPUREGISTER,reg,R_SUBFS),href));
  1286. inc(nextoffset,4);
  1287. end;
  1288. end;
  1289. usesgpr:=false;
  1290. mask:=0;
  1291. nextoffset:=TMIPSProcInfo(current_procinfo).intregstart;
  1292. saveregs:=rg[R_INTREGISTER].used_in_proc-paramanager.get_volatile_registers_int(pocall_stdcall);
  1293. include(saveregs,RS_R31);
  1294. if (TMIPSProcinfo(current_procinfo).needs_frame_pointer) then
  1295. include(saveregs,RS_FRAME_POINTER_REG);
  1296. if (cs_create_pic in current_settings.moduleswitches) and
  1297. (pi_needs_got in current_procinfo.flags) then
  1298. include(saveregs,RS_GP);
  1299. lastintoffset:=LocalSize;
  1300. framesave:=nil;
  1301. gp_save:=nil;
  1302. for reg:=RS_R1 to RS_R31 do
  1303. begin
  1304. if reg in saveregs then
  1305. begin
  1306. usesgpr:=true;
  1307. mask:=mask or (1 shl ord(reg));
  1308. href.offset:=nextoffset;
  1309. lastintoffset:=nextoffset;
  1310. if (reg=RS_FRAME_POINTER_REG) then
  1311. framesave:=taicpu.op_reg_ref(A_SW,newreg(R_INTREGISTER,reg,R_SUBWHOLE),href)
  1312. else if (reg=RS_R31) then
  1313. ra_save:=taicpu.op_reg_ref(A_SW,newreg(R_INTREGISTER,reg,R_SUBWHOLE),href)
  1314. else if (reg=RS_GP) and
  1315. (cs_create_pic in current_settings.moduleswitches) and
  1316. (pi_needs_got in current_procinfo.flags) then
  1317. gp_save:=taicpu.op_const(A_P_CPRESTORE,nextoffset)
  1318. else
  1319. begin
  1320. if cs_asm_source in current_settings.globalswitches then
  1321. helplist.concat(tai_comment.Create(strpnew(
  1322. std_regname(newreg(R_INTREGISTER,reg,R_SUBWHOLE))+' register saved.')));
  1323. helplist.concat(taicpu.op_reg_ref(A_SW,newreg(R_INTREGISTER,reg,R_SUBWHOLE),href));
  1324. end;
  1325. inc(nextoffset,4);
  1326. end;
  1327. end;
  1328. //list.concat(Taicpu.Op_reg_reg_const(A_ADDIU,NR_FRAME_POINTER_REG,NR_STACK_POINTER_REG,current_procinfo.para_stack_size));
  1329. list.concat(Taicpu.op_none(A_P_SET_NOMIPS16));
  1330. list.concat(Taicpu.op_reg_const_reg(A_P_FRAME,current_procinfo.framepointer,LocalSize,NR_R31));
  1331. list.concat(Taicpu.op_const_const(A_P_MASK,mask,-(LocalSize-lastintoffset)));
  1332. list.concat(Taicpu.op_const_const(A_P_FMASK,Fmask,-(LocalSize-lastfpuoffset)));
  1333. if (cs_create_pic in current_settings.moduleswitches) and
  1334. (pi_needs_got in current_procinfo.flags) then
  1335. begin
  1336. list.concat(Taicpu.op_reg(A_P_CPLOAD,NR_PIC_FUNC));
  1337. end;
  1338. list.concat(Taicpu.op_none(A_P_SET_NOREORDER));
  1339. if (-LocalSize >= simm16lo) and (-LocalSize <= simm16hi) then
  1340. begin
  1341. list.concat(Taicpu.op_none(A_P_SET_NOMACRO));
  1342. if cs_asm_source in current_settings.globalswitches then
  1343. begin
  1344. list.concat(tai_comment.Create(strpnew('Stack register updated substract '+tostr(LocalSize)+' for local size')));
  1345. list.concat(tai_comment.Create(strpnew(' 0-'+
  1346. tostr(TMIPSProcInfo(current_procinfo).maxpushedparasize)+' for called function parameters')));
  1347. list.concat(tai_comment.Create(strpnew('Register save area at '+
  1348. tostr(TMIPSProcInfo(current_procinfo).intregstart))));
  1349. list.concat(tai_comment.Create(strpnew('FPU register save area at '+
  1350. tostr(TMIPSProcInfo(current_procinfo).floatregstart))));
  1351. end;
  1352. list.concat(Taicpu.Op_reg_reg_const(A_ADDIU,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,-LocalSize));
  1353. if cs_asm_source in current_settings.globalswitches then
  1354. list.concat(tai_comment.Create(strpnew('RA register saved.')));
  1355. list.concat(ra_save);
  1356. if assigned(framesave) then
  1357. begin
  1358. if cs_asm_source in current_settings.globalswitches then
  1359. list.concat(tai_comment.Create(strpnew('Frame S8/FP register saved.')));
  1360. list.concat(framesave);
  1361. if cs_asm_source in current_settings.globalswitches then
  1362. list.concat(tai_comment.Create(strpnew('New frame FP register set to $sp+'+ToStr(LocalSize))));
  1363. list.concat(Taicpu.op_reg_reg_const(A_ADDIU,NR_FRAME_POINTER_REG,
  1364. NR_STACK_POINTER_REG,LocalSize));
  1365. end;
  1366. end
  1367. else
  1368. begin
  1369. if cs_asm_source in current_settings.globalswitches then
  1370. list.concat(tai_comment.Create(strpnew('Stack register updated substract '+tostr(LocalSize)+' for local size using R9/t1 register')));
  1371. list.concat(Taicpu.Op_reg_const(A_LI,NR_R9,-LocalSize));
  1372. list.concat(Taicpu.Op_reg_reg_reg(A_ADD,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,NR_R9));
  1373. if cs_asm_source in current_settings.globalswitches then
  1374. list.concat(tai_comment.Create(strpnew('RA register saved.')));
  1375. list.concat(ra_save);
  1376. if assigned(framesave) then
  1377. begin
  1378. if cs_asm_source in current_settings.globalswitches then
  1379. list.concat(tai_comment.Create(strpnew('Frame register saved.')));
  1380. list.concat(framesave);
  1381. if cs_asm_source in current_settings.globalswitches then
  1382. list.concat(tai_comment.Create(strpnew('Frame register updated to $SP+R9 value')));
  1383. list.concat(Taicpu.op_reg_reg_reg(A_SUBU,NR_FRAME_POINTER_REG,
  1384. NR_STACK_POINTER_REG,NR_R9));
  1385. end;
  1386. { The instructions before are macros that can extend to multiple instructions,
  1387. the settings of R9 to -LocalSize surely does,
  1388. but the saving of RA and FP also might, and might
  1389. even use AT register, which is why we use R9 instead of AT here for -LocalSize }
  1390. list.concat(Taicpu.op_none(A_P_SET_NOMACRO));
  1391. end;
  1392. if assigned(gp_save) then
  1393. begin
  1394. if cs_asm_source in current_settings.globalswitches then
  1395. list.concat(tai_comment.Create(strpnew('GOT register saved.')));
  1396. list.concat(Taicpu.op_none(A_P_SET_MACRO));
  1397. list.concat(gp_save);
  1398. list.concat(Taicpu.op_none(A_P_SET_NOMACRO));
  1399. end;
  1400. with TMIPSProcInfo(current_procinfo) do
  1401. begin
  1402. href.offset:=0;
  1403. //if current_procinfo.framepointer<>NR_STACK_POINTER_REG then
  1404. href.base:=NR_FRAME_POINTER_REG;
  1405. for i:=0 to MIPS_MAX_REGISTERS_USED_IN_CALL-1 do
  1406. if (register_used[i]) then
  1407. begin
  1408. reg:=parasupregs[i];
  1409. if register_offset[i]=-1 then
  1410. comment(V_warning,'Register parameter has offset -1 in TCGMIPS.g_proc_entry');
  1411. //if current_procinfo.framepointer=NR_STACK_POINTER_REG then
  1412. // href.offset:=register_offset[i]+Localsize
  1413. //else
  1414. href.offset:=register_offset[i];
  1415. {$ifdef MIPSEL}
  1416. if cs_asm_source in current_settings.globalswitches then
  1417. list.concat(tai_comment.Create(strpnew('Var '+
  1418. register_name[i]+' Register '+std_regname(newreg(R_INTREGISTER,reg,R_SUBWHOLE))
  1419. +' saved to offset '+tostr(href.offset))));
  1420. list.concat(taicpu.op_reg_ref(A_SW, newreg(R_INTREGISTER,reg,R_SUBWHOLE), href));
  1421. {$else not MIPSEL, for big endian, size matters}
  1422. case register_size[i] of
  1423. OS_8,
  1424. OS_S8:
  1425. StoreOp := A_SB;
  1426. OS_16,
  1427. OS_S16:
  1428. StoreOp := A_SH;
  1429. OS_32,
  1430. OS_NO,
  1431. OS_F32,
  1432. OS_S32:
  1433. StoreOp := A_SW;
  1434. OS_F64,
  1435. OS_64,
  1436. OS_S64:
  1437. begin
  1438. {$ifdef cpu64bitalu}
  1439. StoreOp:=A_SD;
  1440. {$else not cpu64bitalu}
  1441. StoreOp:= A_SW;
  1442. {$endif not cpu64bitalu}
  1443. end
  1444. else
  1445. internalerror(2012061801);
  1446. end;
  1447. if cs_asm_source in current_settings.globalswitches then
  1448. list.concat(tai_comment.Create(strpnew('Var '+
  1449. register_name[i]+' Register '+std_regname(newreg(R_INTREGISTER,reg,R_SUBWHOLE))
  1450. +' saved to offset '+tostr(href.offset))));
  1451. list.concat(taicpu.op_reg_ref(StoreOp, newreg(R_INTREGISTER,reg,R_SUBWHOLE), href));
  1452. {$endif}
  1453. end;
  1454. end;
  1455. if (cs_create_pic in current_settings.moduleswitches) and
  1456. (pi_needs_got in current_procinfo.flags) then
  1457. begin
  1458. current_procinfo.got := NR_GP;
  1459. end;
  1460. list.concatList(helplist);
  1461. helplist.Free;
  1462. end;
  1463. procedure TCGMIPS.g_proc_exit(list: tasmlist; parasize: longint; nostackframe: boolean);
  1464. var
  1465. href : treference;
  1466. stacksize : aint;
  1467. saveregs : tcpuregisterset;
  1468. nextoffset : aint;
  1469. reg : Tsuperregister;
  1470. begin
  1471. stacksize:=current_procinfo.calc_stackframe_size;
  1472. if nostackframe then
  1473. begin
  1474. list.concat(taicpu.op_reg(A_JR, NR_R31));
  1475. list.concat(Taicpu.op_none(A_NOP));
  1476. list.concat(Taicpu.op_none(A_P_SET_MACRO));
  1477. list.concat(Taicpu.op_none(A_P_SET_REORDER));
  1478. end
  1479. else
  1480. begin
  1481. reference_reset(href,0);
  1482. href.base:=NR_STACK_POINTER_REG;
  1483. nextoffset:=TMIPSProcInfo(current_procinfo).floatregstart;
  1484. for reg := RS_F0 to RS_F30 do
  1485. begin
  1486. if reg in (rg[R_FPUREGISTER].used_in_proc-paramanager.get_volatile_registers_fpu(pocall_stdcall)) then
  1487. begin
  1488. href.offset:=nextoffset;
  1489. list.concat(taicpu.op_reg_ref(A_LWC1,newreg(R_FPUREGISTER,reg,R_SUBFS),href));
  1490. inc(nextoffset,4);
  1491. end;
  1492. end;
  1493. nextoffset:=TMIPSProcInfo(current_procinfo).intregstart;
  1494. saveregs:=rg[R_INTREGISTER].used_in_proc-paramanager.get_volatile_registers_int(pocall_stdcall);
  1495. include(saveregs,RS_R31);
  1496. if (TMIPSProcinfo(current_procinfo).needs_frame_pointer) then
  1497. include(saveregs,RS_FRAME_POINTER_REG);
  1498. for reg:=RS_R1 to RS_R31 do
  1499. begin
  1500. if reg in saveregs then
  1501. begin
  1502. href.offset:=nextoffset;
  1503. list.concat(taicpu.op_reg_ref(A_LW,newreg(R_INTREGISTER,reg,R_SUBWHOLE),href));
  1504. inc(nextoffset,sizeof(aint));
  1505. end;
  1506. end;
  1507. if (-stacksize >= simm16lo) and (-stacksize <= simm16hi) then
  1508. begin
  1509. list.concat(taicpu.op_reg(A_JR, NR_R31));
  1510. { correct stack pointer in the delay slot }
  1511. list.concat(Taicpu.Op_reg_reg_const(A_ADDIU, NR_STACK_POINTER_REG, NR_STACK_POINTER_REG, stacksize));
  1512. end
  1513. else
  1514. begin
  1515. a_load_const_reg(list,OS_32,stacksize,NR_R1);
  1516. list.concat(taicpu.op_reg(A_JR, NR_R31));
  1517. { correct stack pointer in the delay slot }
  1518. list.concat(taicpu.op_reg_reg_reg(A_ADD,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,NR_R1));
  1519. end;
  1520. list.concat(Taicpu.op_none(A_P_SET_MACRO));
  1521. list.concat(Taicpu.op_none(A_P_SET_REORDER));
  1522. end;
  1523. end;
  1524. { ************* concatcopy ************ }
  1525. procedure TCGMIPS.g_concatcopy_move(list: tasmlist; const Source, dest: treference; len: tcgint);
  1526. var
  1527. paraloc1, paraloc2, paraloc3: TCGPara;
  1528. begin
  1529. paraloc1.init;
  1530. paraloc2.init;
  1531. paraloc3.init;
  1532. paramanager.getintparaloc(pocall_default, 1, voidpointertype, paraloc1);
  1533. paramanager.getintparaloc(pocall_default, 2, voidpointertype, paraloc2);
  1534. paramanager.getintparaloc(pocall_default, 3, ptrsinttype, paraloc3);
  1535. a_load_const_cgpara(list, OS_SINT, len, paraloc3);
  1536. a_loadaddr_ref_cgpara(list, dest, paraloc2);
  1537. a_loadaddr_ref_cgpara(list, Source, paraloc1);
  1538. paramanager.freecgpara(list, paraloc3);
  1539. paramanager.freecgpara(list, paraloc2);
  1540. paramanager.freecgpara(list, paraloc1);
  1541. alloccpuregisters(list, R_INTREGISTER, paramanager.get_volatile_registers_int(pocall_default));
  1542. alloccpuregisters(list, R_FPUREGISTER, paramanager.get_volatile_registers_fpu(pocall_default));
  1543. a_call_name(list, 'FPC_MOVE', false);
  1544. dealloccpuregisters(list, R_FPUREGISTER, paramanager.get_volatile_registers_fpu(pocall_default));
  1545. dealloccpuregisters(list, R_INTREGISTER, paramanager.get_volatile_registers_int(pocall_default));
  1546. paraloc3.done;
  1547. paraloc2.done;
  1548. paraloc1.done;
  1549. end;
  1550. procedure TCGMIPS.g_concatcopy(list: tasmlist; const Source, dest: treference; len: tcgint);
  1551. var
  1552. tmpreg1, hreg, countreg: TRegister;
  1553. src, dst: TReference;
  1554. lab: tasmlabel;
  1555. Count, count2: aint;
  1556. ai : TaiCpu;
  1557. begin
  1558. if len > high(longint) then
  1559. internalerror(2002072704);
  1560. { anybody wants to determine a good value here :)? }
  1561. if len > 100 then
  1562. g_concatcopy_move(list, Source, dest, len)
  1563. else
  1564. begin
  1565. reference_reset(src,sizeof(aint));
  1566. reference_reset(dst,sizeof(aint));
  1567. { load the address of source into src.base }
  1568. src.base := GetAddressRegister(list);
  1569. a_loadaddr_ref_reg(list, Source, src.base);
  1570. { load the address of dest into dst.base }
  1571. dst.base := GetAddressRegister(list);
  1572. a_loadaddr_ref_reg(list, dest, dst.base);
  1573. { generate a loop }
  1574. Count := len div 4;
  1575. if Count > 4 then
  1576. begin
  1577. { the offsets are zero after the a_loadaddress_ref_reg and just }
  1578. { have to be set to 8. I put an Inc there so debugging may be }
  1579. { easier (should offset be different from zero here, it will be }
  1580. { easy to notice in the generated assembler }
  1581. countreg := GetIntRegister(list, OS_INT);
  1582. tmpreg1 := GetIntRegister(list, OS_INT);
  1583. a_load_const_reg(list, OS_INT, Count, countreg);
  1584. { explicitely allocate R_O0 since it can be used safely here }
  1585. { (for holding date that's being copied) }
  1586. current_asmdata.getjumplabel(lab);
  1587. a_label(list, lab);
  1588. list.concat(taicpu.op_reg_ref(A_LW, tmpreg1, src));
  1589. list.concat(taicpu.op_reg_ref(A_SW, tmpreg1, dst));
  1590. list.concat(taicpu.op_reg_reg_const(A_ADDIU, src.base, src.base, 4));
  1591. list.concat(taicpu.op_reg_reg_const(A_ADDIU, dst.base, dst.base, 4));
  1592. list.concat(taicpu.op_reg_reg_const(A_ADDIU, countreg, countreg, -1));
  1593. //list.concat(taicpu.op_reg_sym(A_BGTZ, countreg, lab));
  1594. ai := taicpu.op_reg_reg_sym(A_BC,countreg, NR_R0, lab);
  1595. ai.setcondition(C_GT);
  1596. list.concat(ai);
  1597. list.concat(taicpu.op_none(A_NOP));
  1598. len := len mod 4;
  1599. end;
  1600. { unrolled loop }
  1601. Count := len div 4;
  1602. if Count > 0 then
  1603. begin
  1604. tmpreg1 := GetIntRegister(list, OS_INT);
  1605. for count2 := 1 to Count do
  1606. begin
  1607. list.concat(taicpu.op_reg_ref(A_LW, tmpreg1, src));
  1608. list.concat(taicpu.op_reg_ref(A_SW, tmpreg1, dst));
  1609. Inc(src.offset, 4);
  1610. Inc(dst.offset, 4);
  1611. end;
  1612. len := len mod 4;
  1613. end;
  1614. if (len and 4) <> 0 then
  1615. begin
  1616. hreg := GetIntRegister(list, OS_INT);
  1617. a_load_ref_reg(list, OS_32, OS_32, src, hreg);
  1618. a_load_reg_ref(list, OS_32, OS_32, hreg, dst);
  1619. Inc(src.offset, 4);
  1620. Inc(dst.offset, 4);
  1621. end;
  1622. { copy the leftovers }
  1623. if (len and 2) <> 0 then
  1624. begin
  1625. hreg := GetIntRegister(list, OS_INT);
  1626. a_load_ref_reg(list, OS_16, OS_16, src, hreg);
  1627. a_load_reg_ref(list, OS_16, OS_16, hreg, dst);
  1628. Inc(src.offset, 2);
  1629. Inc(dst.offset, 2);
  1630. end;
  1631. if (len and 1) <> 0 then
  1632. begin
  1633. hreg := GetIntRegister(list, OS_INT);
  1634. a_load_ref_reg(list, OS_8, OS_8, src, hreg);
  1635. a_load_reg_ref(list, OS_8, OS_8, hreg, dst);
  1636. end;
  1637. end;
  1638. end;
  1639. procedure TCGMIPS.g_concatcopy_unaligned(list: tasmlist; const Source, dest: treference; len: tcgint);
  1640. var
  1641. src, dst: TReference;
  1642. tmpreg1, countreg: TRegister;
  1643. i: aint;
  1644. lab: tasmlabel;
  1645. ai : TaiCpu;
  1646. begin
  1647. if len > 31 then
  1648. g_concatcopy_move(list, Source, dest, len)
  1649. else
  1650. begin
  1651. reference_reset(src,sizeof(aint));
  1652. reference_reset(dst,sizeof(aint));
  1653. { load the address of source into src.base }
  1654. src.base := GetAddressRegister(list);
  1655. a_loadaddr_ref_reg(list, Source, src.base);
  1656. { load the address of dest into dst.base }
  1657. dst.base := GetAddressRegister(list);
  1658. a_loadaddr_ref_reg(list, dest, dst.base);
  1659. { generate a loop }
  1660. if len > 4 then
  1661. begin
  1662. { the offsets are zero after the a_loadaddress_ref_reg and just }
  1663. { have to be set to 8. I put an Inc there so debugging may be }
  1664. { easier (should offset be different from zero here, it will be }
  1665. { easy to notice in the generated assembler }
  1666. countreg := cg.GetIntRegister(list, OS_INT);
  1667. tmpreg1 := cg.GetIntRegister(list, OS_INT);
  1668. a_load_const_reg(list, OS_INT, len, countreg);
  1669. { explicitely allocate R_O0 since it can be used safely here }
  1670. { (for holding date that's being copied) }
  1671. current_asmdata.getjumplabel(lab);
  1672. a_label(list, lab);
  1673. list.concat(taicpu.op_reg_ref(A_LBU, tmpreg1, src));
  1674. list.concat(taicpu.op_reg_ref(A_SB, tmpreg1, dst));
  1675. list.concat(taicpu.op_reg_reg_const(A_ADDIU, src.base, src.base, 1));
  1676. list.concat(taicpu.op_reg_reg_const(A_ADDIU, dst.base, dst.base, 1));
  1677. list.concat(taicpu.op_reg_reg_const(A_ADDIU, countreg, countreg, -1));
  1678. //list.concat(taicpu.op_reg_sym(A_BGTZ, countreg, lab));
  1679. ai := taicpu.op_reg_reg_sym(A_BC,countreg, NR_R0, lab);
  1680. ai.setcondition(C_GT);
  1681. list.concat(ai);
  1682. list.concat(taicpu.op_none(A_NOP));
  1683. end
  1684. else
  1685. begin
  1686. { unrolled loop }
  1687. tmpreg1 := cg.GetIntRegister(list, OS_INT);
  1688. for i := 1 to len do
  1689. begin
  1690. list.concat(taicpu.op_reg_ref(A_LBU, tmpreg1, src));
  1691. list.concat(taicpu.op_reg_ref(A_SB, tmpreg1, dst));
  1692. Inc(src.offset);
  1693. Inc(dst.offset);
  1694. end;
  1695. end;
  1696. end;
  1697. end;
  1698. procedure TCGMIPS.g_intf_wrapper(list: tasmlist; procdef: tprocdef; const labelname: string; ioffset: longint);
  1699. procedure loadvmttorvmt;
  1700. var
  1701. href: treference;
  1702. begin
  1703. reference_reset_base(href, NR_R2, 0, sizeof(aint)); { return value }
  1704. cg.a_load_ref_reg(list, OS_ADDR, OS_ADDR, href, NR_VMT);
  1705. end;
  1706. procedure op_onrvmtmethodaddr;
  1707. var
  1708. href : treference;
  1709. reg : tregister;
  1710. begin
  1711. if (procdef.extnumber=$ffff) then
  1712. Internalerror(200006139);
  1713. { call/jmp vmtoffs(%eax) ; method offs }
  1714. reference_reset_base(href, NR_VMT, tobjectdef(procdef.struct).vmtmethodoffset(procdef.extnumber), sizeof(aint));
  1715. if (cs_create_pic in current_settings.moduleswitches) then
  1716. reg:=NR_PIC_FUNC
  1717. else
  1718. reg:=NR_VMT;
  1719. cg.a_load_ref_reg(list, OS_ADDR, OS_ADDR, href, reg);
  1720. list.concat(taicpu.op_reg(A_JR, reg));
  1721. end;
  1722. var
  1723. make_global: boolean;
  1724. href: treference;
  1725. begin
  1726. if not(procdef.proctypeoption in [potype_function,potype_procedure]) then
  1727. Internalerror(200006137);
  1728. if not assigned(procdef.struct) or
  1729. (procdef.procoptions * [po_classmethod, po_staticmethod,
  1730. po_methodpointer, po_interrupt, po_iocheck] <> []) then
  1731. Internalerror(200006138);
  1732. if procdef.owner.symtabletype <> objectsymtable then
  1733. Internalerror(200109191);
  1734. make_global := False;
  1735. if (not current_module.is_unit) or create_smartlink or
  1736. (procdef.owner.defowner.owner.symtabletype = globalsymtable) then
  1737. make_global := True;
  1738. if make_global then
  1739. List.concat(Tai_symbol.Createname_global(labelname, AT_FUNCTION, 0))
  1740. else
  1741. List.concat(Tai_symbol.Createname(labelname, AT_FUNCTION, 0));
  1742. { set param1 interface to self }
  1743. g_adjust_self_value(list, procdef, ioffset);
  1744. if (po_virtualmethod in procdef.procoptions) and
  1745. not is_objectpascal_helper(procdef.struct) then
  1746. begin
  1747. loadvmttorvmt;
  1748. op_onrvmtmethodaddr;
  1749. end
  1750. else
  1751. list.concat(taicpu.op_sym(A_J,current_asmdata.RefAsmSymbol(procdef.mangledname)));
  1752. { Delay slot }
  1753. list.Concat(TAiCpu.Op_none(A_NOP));
  1754. List.concat(Tai_symbol_end.Createname(labelname));
  1755. end;
  1756. procedure TCGMIPS.g_stackpointer_alloc(list : TAsmList;localsize : longint);
  1757. begin
  1758. Comment(V_Error,'TCgMPSel.g_stackpointer_alloc method not implemented');
  1759. end;
  1760. procedure TCGMIPS.a_bit_scan_reg_reg(list: TAsmList; reverse: boolean; size: TCGSize; src, dst: TRegister);
  1761. begin
  1762. Comment(V_Error,'TCgMPSel.a_bit_scan_reg_reg method not implemented');
  1763. end;
  1764. {****************************************************************************
  1765. TCG64_MIPSel
  1766. ****************************************************************************}
  1767. procedure TCg64MPSel.a_load64_reg_ref(list: tasmlist; reg: tregister64; const ref: treference);
  1768. var
  1769. tmpref: treference;
  1770. tmpreg: tregister;
  1771. begin
  1772. { Override this function to prevent loading the reference twice }
  1773. if target_info.endian = endian_big then
  1774. begin
  1775. tmpreg := reg.reglo;
  1776. reg.reglo := reg.reghi;
  1777. reg.reghi := tmpreg;
  1778. end;
  1779. tmpref := ref;
  1780. cg.a_load_reg_ref(list, OS_S32, OS_S32, reg.reglo, tmpref);
  1781. Inc(tmpref.offset, 4);
  1782. cg.a_load_reg_ref(list, OS_S32, OS_S32, reg.reghi, tmpref);
  1783. end;
  1784. procedure TCg64MPSel.a_load64_ref_reg(list: tasmlist; const ref: treference; reg: tregister64);
  1785. var
  1786. tmpref: treference;
  1787. tmpreg: tregister;
  1788. begin
  1789. { Override this function to prevent loading the reference twice }
  1790. if target_info.endian = endian_big then
  1791. begin
  1792. tmpreg := reg.reglo;
  1793. reg.reglo := reg.reghi;
  1794. reg.reghi := tmpreg;
  1795. end;
  1796. tmpref := ref;
  1797. cg.a_load_ref_reg(list, OS_S32, OS_S32, tmpref, reg.reglo);
  1798. Inc(tmpref.offset, 4);
  1799. cg.a_load_ref_reg(list, OS_S32, OS_S32, tmpref, reg.reghi);
  1800. end;
  1801. procedure TCg64MPSel.a_load64_ref_cgpara(list: tasmlist; const r: treference; const paraloc: tcgpara);
  1802. var
  1803. hreg64: tregister64;
  1804. begin
  1805. { Override this function to prevent loading the reference twice.
  1806. Use here some extra registers, but those are optimized away by the RA }
  1807. hreg64.reglo := cg.GetIntRegister(list, OS_S32);
  1808. hreg64.reghi := cg.GetIntRegister(list, OS_S32);
  1809. a_load64_ref_reg(list, r, hreg64);
  1810. a_load64_reg_cgpara(list, hreg64, paraloc);
  1811. end;
  1812. procedure TCg64MPSel.a_op64_reg_reg(list: tasmlist; op: TOpCG; size: tcgsize; regsrc, regdst: TRegister64);
  1813. var
  1814. op1, op2, op_call64: TAsmOp;
  1815. tmpreg1, tmpreg2: TRegister;
  1816. begin
  1817. tmpreg1 := cg.GetIntRegister(list, OS_INT);
  1818. tmpreg2 := cg.GetIntRegister(list, OS_INT);
  1819. case op of
  1820. OP_ADD:
  1821. begin
  1822. list.concat(taicpu.op_reg_reg_reg(A_ADDU, regdst.reglo, regsrc.reglo, regdst.reglo));
  1823. list.concat(taicpu.op_reg_reg_reg(A_SLTU, tmpreg1, regdst.reglo, regsrc.reglo));
  1824. list.concat(taicpu.op_reg_reg_reg(A_ADDU, tmpreg2, regsrc.reghi, regdst.reghi));
  1825. list.concat(taicpu.op_reg_reg_reg(A_ADDU, regdst.reghi, tmpreg1, tmpreg2));
  1826. exit;
  1827. end;
  1828. OP_AND:
  1829. begin
  1830. list.concat(taicpu.op_reg_reg_reg(A_AND, regdst.reglo, regsrc.reglo, regdst.reglo));
  1831. list.concat(taicpu.op_reg_reg_reg(A_AND, regdst.reghi, regsrc.reghi, regdst.reghi));
  1832. exit;
  1833. end;
  1834. OP_NEG:
  1835. begin
  1836. list.concat(taicpu.op_reg_reg_reg(A_SUBU, regdst.reglo, NR_R0, regsrc.reglo));
  1837. list.concat(taicpu.op_reg_reg_reg(A_SLTU, tmpreg1, NR_R0, regdst.reglo));
  1838. list.concat(taicpu.op_reg_reg_reg(A_SUBU, regdst.reghi, NR_R0, regsrc.reghi));
  1839. list.concat(taicpu.op_reg_reg_reg(A_SUBU, tmpreg1, regdst.reghi, tmpreg1));
  1840. list.concat(Taicpu.Op_reg_reg(A_MOVE, regdst.reghi, tmpreg1));
  1841. exit;
  1842. end;
  1843. OP_NOT:
  1844. begin
  1845. list.concat(taicpu.op_reg_reg_reg(A_NOR, regdst.reglo, NR_R0, regsrc.reglo));
  1846. list.concat(taicpu.op_reg_reg_reg(A_NOR, regdst.reghi, NR_R0, regsrc.reghi));
  1847. exit;
  1848. end;
  1849. OP_OR:
  1850. begin
  1851. list.concat(taicpu.op_reg_reg_reg(A_OR, regdst.reglo, regsrc.reglo, regdst.reglo));
  1852. list.concat(taicpu.op_reg_reg_reg(A_OR, regdst.reghi, regsrc.reghi, regdst.reghi));
  1853. exit;
  1854. end;
  1855. OP_SUB:
  1856. begin
  1857. list.concat(taicpu.op_reg_reg_reg(A_SUBU, tmpreg1, regdst.reglo, regsrc.reglo));
  1858. list.concat(taicpu.op_reg_reg_reg(A_SLTU, tmpreg2, regdst.reglo, tmpreg1));
  1859. list.concat(taicpu.op_reg_reg_reg(A_SUBU, regdst.reghi, regdst.reghi, regsrc.reghi));
  1860. list.concat(taicpu.op_reg_reg_reg(A_SUBU, regdst.reghi, regdst.reghi, tmpreg2));
  1861. list.concat(Taicpu.Op_reg_reg(A_MOVE, regdst.reglo, tmpreg1));
  1862. exit;
  1863. end;
  1864. OP_XOR:
  1865. begin
  1866. list.concat(taicpu.op_reg_reg_reg(A_XOR, regdst.reglo, regdst.reglo, regsrc.reglo));
  1867. list.concat(taicpu.op_reg_reg_reg(A_XOR, regdst.reghi, regsrc.reghi, regdst.reghi));
  1868. exit;
  1869. end;
  1870. else
  1871. internalerror(200306017);
  1872. end; {case}
  1873. end;
  1874. procedure TCg64MPSel.a_op64_const_reg(list: tasmlist; op: TOpCG; size: tcgsize; Value: int64; regdst: TRegister64);
  1875. begin
  1876. a_op64_const_reg_reg(list, op, size, value, regdst, regdst);
  1877. end;
  1878. procedure TCg64MPSel.a_op64_const_reg_reg(list: tasmlist; op: TOpCG; size: tcgsize; Value: int64; regsrc, regdst: tregister64);
  1879. var
  1880. l: tlocation;
  1881. begin
  1882. a_op64_const_reg_reg_checkoverflow(list, op, size, Value, regsrc, regdst, False, l);
  1883. end;
  1884. procedure TCg64MPSel.a_op64_reg_reg_reg(list: tasmlist; op: TOpCG; size: tcgsize; regsrc1, regsrc2, regdst: tregister64);
  1885. var
  1886. l: tlocation;
  1887. begin
  1888. a_op64_reg_reg_reg_checkoverflow(list, op, size, regsrc1, regsrc2, regdst, False, l);
  1889. end;
  1890. procedure TCg64MPSel.a_op64_const_reg_reg_checkoverflow(list: tasmlist; op: TOpCG; size: tcgsize; Value: int64; regsrc, regdst: tregister64; setflags: boolean; var ovloc: tlocation);
  1891. var
  1892. tmpreg64: TRegister64;
  1893. begin
  1894. tmpreg64.reglo := cg.GetIntRegister(list, OS_S32);
  1895. tmpreg64.reghi := cg.GetIntRegister(list, OS_S32);
  1896. list.concat(taicpu.op_reg_const(A_LI, tmpreg64.reglo, aint(lo(Value))));
  1897. list.concat(taicpu.op_reg_const(A_LI, tmpreg64.reghi, aint(hi(Value))));
  1898. a_op64_reg_reg_reg_checkoverflow(list, op, size, tmpreg64, regsrc, regdst, False, ovloc);
  1899. end;
  1900. procedure TCg64MPSel.a_op64_reg_reg_reg_checkoverflow(list: tasmlist; op: TOpCG; size: tcgsize; regsrc1, regsrc2, regdst: tregister64; setflags: boolean; var ovloc: tlocation);
  1901. var
  1902. op1, op2: TAsmOp;
  1903. tmpreg1, tmpreg2: TRegister;
  1904. begin
  1905. case op of
  1906. OP_ADD:
  1907. begin
  1908. tmpreg1 := cg.GetIntRegister(list,OS_S32);
  1909. list.concat(taicpu.op_reg_reg_reg(A_ADDU, regdst.reglo, regsrc2.reglo, regsrc1.reglo));
  1910. list.concat(taicpu.op_reg_reg_reg(A_SLTU, tmpreg1, regdst.reglo, regsrc2.reglo));
  1911. list.concat(taicpu.op_reg_reg_reg(A_ADDU, regdst.reghi, regsrc2.reghi, regsrc1.reghi));
  1912. list.concat(taicpu.op_reg_reg_reg(A_ADDU, regdst.reghi, regdst.reghi, tmpreg1));
  1913. exit;
  1914. end;
  1915. OP_AND:
  1916. begin
  1917. list.concat(taicpu.op_reg_reg_reg(A_AND, regdst.reglo, regsrc2.reglo, regsrc1.reglo));
  1918. list.concat(taicpu.op_reg_reg_reg(A_AND, regdst.reghi, regsrc2.reghi, regsrc1.reghi));
  1919. exit;
  1920. end;
  1921. OP_OR:
  1922. begin
  1923. list.concat(taicpu.op_reg_reg_reg(A_OR, regdst.reglo, regsrc2.reglo, regsrc1.reglo));
  1924. list.concat(taicpu.op_reg_reg_reg(A_OR, regdst.reghi, regsrc2.reghi, regsrc1.reghi));
  1925. exit;
  1926. end;
  1927. OP_SUB:
  1928. begin
  1929. tmpreg1 := cg.GetIntRegister(list,OS_S32);
  1930. list.concat(taicpu.op_reg_reg_reg(A_SUBU, regdst.reglo, regsrc2.reglo, regsrc1.reglo));
  1931. list.concat(taicpu.op_reg_reg_reg(A_SLTU, tmpreg1, regsrc2.reglo, regdst.reglo));
  1932. list.concat(taicpu.op_reg_reg_reg(A_SUBU, regdst.reghi, regsrc2.reghi, regsrc1.reghi));
  1933. list.concat(taicpu.op_reg_reg_reg(A_SUBU, regdst.reghi, regdst.reghi, tmpreg1));
  1934. exit;
  1935. end;
  1936. OP_XOR:
  1937. begin
  1938. list.concat(taicpu.op_reg_reg_reg(A_XOR, regdst.reglo, regsrc2.reglo, regsrc1.reglo));
  1939. list.concat(taicpu.op_reg_reg_reg(A_XOR, regdst.reghi, regsrc2.reghi, regsrc1.reghi));
  1940. exit;
  1941. end;
  1942. else
  1943. internalerror(200306017);
  1944. end; {case}
  1945. end;
  1946. procedure create_codegen;
  1947. begin
  1948. cg:=TCGMIPS.Create;
  1949. cg64:=TCg64MPSel.Create;
  1950. end;
  1951. end.