cgcpu.pas 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990
  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_R2,RS_R3,RS_R4,RS_R5,RS_R6,RS_R7,RS_R8,RS_R9,
  476. RS_R10,RS_R11,RS_R12,RS_R13,RS_R14,RS_R15,RS_R16,RS_R17,RS_R18,RS_R19,
  477. RS_R20,RS_R21,RS_R22,RS_R23,RS_R24,RS_R25],
  478. first_int_imreg, []);
  479. end
  480. else
  481. rg[R_INTREGISTER] := trgcpu.Create(R_INTREGISTER, R_SUBD,
  482. [RS_R2,RS_R3,RS_R4,RS_R5,RS_R6,RS_R7,RS_R8,RS_R9,
  483. RS_R10,RS_R11,RS_R12,RS_R13,RS_R14,RS_R15,RS_R16,RS_R17,RS_R18,RS_R19,
  484. RS_R20,RS_R21,RS_R22,RS_R23,RS_R24,RS_R25],
  485. first_int_imreg, []);
  486. rg[R_FPUREGISTER] := trgcpu.Create(R_FPUREGISTER, R_SUBFS,
  487. [RS_F0,RS_F1,RS_F2,RS_F3,RS_F4,RS_F5,RS_F6,RS_F7,
  488. RS_F8,RS_F9,RS_F10,RS_F11,RS_F12,RS_F13,RS_F14,RS_F15,
  489. RS_F16,RS_F17,RS_F18,RS_F19,RS_F20,RS_F21,RS_F22,RS_F23,
  490. RS_F24,RS_F25,RS_F26,RS_F27,RS_F28,RS_F29,RS_F30,RS_F31],
  491. first_fpu_imreg, []);
  492. { needs at least one element for rgobj not to crash }
  493. rg[R_MMREGISTER]:=trgcpu.create(R_MMREGISTER,R_SUBNONE,
  494. [RS_R0],first_mm_imreg,[]);
  495. end;
  496. procedure TCgMPSel.done_register_allocators;
  497. begin
  498. rg[R_INTREGISTER].Free;
  499. rg[R_FPUREGISTER].Free;
  500. rg[R_MMREGISTER].Free;
  501. inherited done_register_allocators;
  502. end;
  503. function TCgMPSel.getfpuregister(list: tasmlist; size: Tcgsize): Tregister;
  504. begin
  505. if size = OS_F64 then
  506. Result := rg[R_FPUREGISTER].getregister(list, R_SUBFS)
  507. else
  508. Result := rg[R_FPUREGISTER].getregister(list, R_SUBFS);
  509. end;
  510. procedure TCgMPSel.a_load_const_cgpara(list: tasmlist; size: tcgsize; a: tcgint; const paraloc: TCGPara);
  511. var
  512. Ref: TReference;
  513. begin
  514. paraloc.check_simple_location;
  515. paramanager.allocparaloc(list,paraloc.location);
  516. case paraloc.location^.loc of
  517. LOC_REGISTER, LOC_CREGISTER:
  518. a_load_const_reg(list, size, a, paraloc.location^.Register);
  519. LOC_REFERENCE:
  520. begin
  521. with paraloc.location^.Reference do
  522. begin
  523. if (Index = NR_SP) and (Offset < 0) then
  524. InternalError(2002081104);
  525. reference_reset_base(ref, index, offset, sizeof(aint));
  526. end;
  527. a_load_const_ref(list, size, a, ref);
  528. end;
  529. else
  530. InternalError(2002122200);
  531. end;
  532. end;
  533. procedure TCgMPSel.a_load_ref_cgpara(list: tasmlist; sz: TCgSize; const r: TReference; const paraloc: TCGPara);
  534. var
  535. ref: treference;
  536. tmpreg: TRegister;
  537. begin
  538. paraloc.check_simple_location;
  539. paramanager.allocparaloc(list,paraloc.location);
  540. with paraloc.location^ do
  541. begin
  542. case loc of
  543. LOC_REGISTER, LOC_CREGISTER:
  544. a_load_ref_reg(list, sz, sz, r, Register);
  545. LOC_REFERENCE:
  546. begin
  547. with Reference do
  548. begin
  549. if (Index = NR_SP) and (Offset < 0) then
  550. InternalError(2002081104);
  551. reference_reset_base(ref, index, offset, sizeof(aint));
  552. end;
  553. tmpreg := GetIntRegister(list, OS_INT);
  554. a_load_ref_reg(list, sz, sz, r, tmpreg);
  555. a_load_reg_ref(list, sz, sz, tmpreg, ref);
  556. end;
  557. else
  558. internalerror(2002081103);
  559. end;
  560. end;
  561. end;
  562. procedure TCgMPSel.a_loadaddr_ref_cgpara(list: tasmlist; const r: TReference; const paraloc: TCGPara);
  563. var
  564. Ref: TReference;
  565. TmpReg: TRegister;
  566. begin
  567. paraloc.check_simple_location;
  568. paramanager.allocparaloc(list,paraloc.location);
  569. with paraloc.location^ do
  570. begin
  571. case loc of
  572. LOC_REGISTER, LOC_CREGISTER:
  573. a_loadaddr_ref_reg(list, r, Register);
  574. LOC_REFERENCE:
  575. begin
  576. reference_reset(ref,sizeof(aint));
  577. ref.base := reference.index;
  578. ref.offset := reference.offset;
  579. tmpreg := GetAddressRegister(list);
  580. a_loadaddr_ref_reg(list, r, tmpreg);
  581. a_load_reg_ref(list, OS_ADDR, OS_ADDR, tmpreg, ref);
  582. end;
  583. else
  584. internalerror(2002080701);
  585. end;
  586. end;
  587. end;
  588. procedure TCgMPSel.a_loadfpu_ref_cgpara(list: tasmlist; size: tcgsize; const ref: treference; const paraloc: TCGPara);
  589. var
  590. href, href2: treference;
  591. hloc: pcgparalocation;
  592. begin
  593. href := ref;
  594. hloc := paraloc.location;
  595. while assigned(hloc) do
  596. begin
  597. paramanager.allocparaloc(list,hloc);
  598. case hloc^.loc of
  599. LOC_REGISTER:
  600. a_load_ref_reg(list, hloc^.size, hloc^.size, href, hloc^.Register);
  601. LOC_FPUREGISTER,LOC_CFPUREGISTER :
  602. a_loadfpu_ref_reg(list,hloc^.size,hloc^.size,href,hloc^.register);
  603. LOC_REFERENCE:
  604. begin
  605. reference_reset_base(href2, hloc^.reference.index, hloc^.reference.offset, sizeof(aint));
  606. a_load_ref_ref(list, hloc^.size, hloc^.size, href, href2);
  607. end;
  608. else
  609. internalerror(200408241);
  610. end;
  611. Inc(href.offset, tcgsize2size[hloc^.size]);
  612. hloc := hloc^.Next;
  613. end;
  614. end;
  615. procedure TCgMPSel.a_loadfpu_reg_cgpara(list: tasmlist; size: tcgsize; const r: tregister; const paraloc: TCGPara);
  616. var
  617. href: treference;
  618. begin
  619. tg.GetTemp(list, TCGSize2Size[size], sizeof(aint), tt_normal, href);
  620. a_loadfpu_reg_ref(list, size, size, r, href);
  621. a_loadfpu_ref_cgpara(list, size, href, paraloc);
  622. tg.Ungettemp(list, href);
  623. end;
  624. procedure TCgMPSel.a_call_name(list: tasmlist; const s: string; weak: boolean);
  625. begin
  626. list.concat(taicpu.op_sym(A_JAL,current_asmdata.RefAsmSymbol(s)));
  627. { Delay slot }
  628. list.concat(taicpu.op_none(A_NOP));
  629. end;
  630. procedure TCgMPSel.a_call_reg(list: tasmlist; Reg: TRegister);
  631. begin
  632. list.concat(taicpu.op_reg(A_JALR, reg));
  633. { Delay slot }
  634. list.concat(taicpu.op_none(A_NOP));
  635. end;
  636. {********************** load instructions ********************}
  637. procedure TCgMPSel.a_load_const_reg(list: tasmlist; size: TCGSize; a: tcgint; reg: TRegister);
  638. begin
  639. if (a = 0) then
  640. list.concat(taicpu.op_reg_reg(A_MOVE, reg, NR_R0))
  641. { LUI allows to set the upper 16 bits, so we'll take full advantage of it }
  642. else if (a and aint($ffff)) = 0 then
  643. list.concat(taicpu.op_reg_const(A_LUI, reg, aint(a) shr 16))
  644. else if (a >= simm16lo) and (a <= simm16hi) then
  645. list.concat(taicpu.op_reg_reg_const(A_ADDIU, reg, NR_R0, a))
  646. else if (a>=0) and (a <= 65535) then
  647. list.concat(taicpu.op_reg_reg_const(A_ORI, reg, NR_R0, a))
  648. else
  649. begin
  650. list.concat(taicpu.op_reg_const(A_LI, reg, aint(a) ));
  651. end;
  652. end;
  653. procedure TCgMPSel.a_load_const_ref(list: tasmlist; size: tcgsize; a: tcgint; const ref: TReference);
  654. begin
  655. if a = 0 then
  656. a_load_reg_ref(list, size, size, NR_R0, ref)
  657. else
  658. inherited a_load_const_ref(list, size, a, ref);
  659. end;
  660. procedure TCgMPSel.a_load_reg_ref(list: tasmlist; FromSize, ToSize: TCGSize; reg: tregister; const Ref: TReference);
  661. var
  662. op: tasmop;
  663. begin
  664. if (TCGSize2Size[fromsize] >= TCGSize2Size[tosize]) then
  665. fromsize := tosize;
  666. case fromsize of
  667. { signed integer registers }
  668. OS_8,
  669. OS_S8:
  670. Op := A_SB;
  671. OS_16,
  672. OS_S16:
  673. Op := A_SH;
  674. OS_32,
  675. OS_S32:
  676. Op := A_SW;
  677. else
  678. InternalError(2002122100);
  679. end;
  680. handle_load_store(list, True, op, reg, ref);
  681. end;
  682. procedure TCgMPSel.a_load_ref_reg(list: tasmlist; FromSize, ToSize: TCgSize; const ref: TReference; reg: tregister);
  683. var
  684. op: tasmop;
  685. begin
  686. if (TCGSize2Size[fromsize] >= TCGSize2Size[tosize]) then
  687. fromsize := tosize;
  688. case fromsize of
  689. OS_S8:
  690. Op := A_LB;{Load Signed Byte}
  691. OS_8:
  692. Op := A_LBU;{Load Unsigned Byte}
  693. OS_S16:
  694. Op := A_LH;{Load Signed Halfword}
  695. OS_16:
  696. Op := A_LHU;{Load Unsigned Halfword}
  697. OS_S32:
  698. Op := A_LW;{Load Word}
  699. OS_32:
  700. Op := A_LW;//A_LWU;{Load Unsigned Word}
  701. OS_S64,
  702. OS_64:
  703. Op := A_LD;{Load a Long Word}
  704. else
  705. InternalError(2002122101);
  706. end;
  707. handle_load_store(list, False, op, reg, ref);
  708. end;
  709. procedure TCgMPSel.a_load_reg_reg(list: tasmlist; fromsize, tosize: tcgsize; reg1, reg2: tregister);
  710. var
  711. instr: taicpu;
  712. begin
  713. if (tcgsize2size[tosize] < tcgsize2size[fromsize]) or
  714. (
  715. (tcgsize2size[tosize] = tcgsize2size[fromsize]) and
  716. (tosize <> fromsize) and not (fromsize in [OS_32, OS_S32])
  717. ) then
  718. begin
  719. case tosize of
  720. OS_8:
  721. a_op_const_reg_reg(list, OP_AND, tosize, $ff, reg1, reg2);
  722. OS_16:
  723. a_op_const_reg_reg(list, OP_AND, tosize, $ffff, reg1, reg2);
  724. OS_32,
  725. OS_S32:
  726. begin
  727. instr := taicpu.op_reg_reg(A_MOVE, reg2, reg1);
  728. list.Concat(instr);
  729. { Notify the register allocator that we have written a move instruction so
  730. it can try to eliminate it. }
  731. add_move_instruction(instr);
  732. end;
  733. OS_S8:
  734. begin
  735. list.concat(taicpu.op_reg_reg_const(A_SLL, reg2, reg1, 24));
  736. list.concat(taicpu.op_reg_reg_const(A_SRA, reg2, reg2, 24));
  737. end;
  738. OS_S16:
  739. begin
  740. list.concat(taicpu.op_reg_reg_const(A_SLL, reg2, reg1, 16));
  741. list.concat(taicpu.op_reg_reg_const(A_SRA, reg2, reg2, 16));
  742. end;
  743. else
  744. internalerror(2002090901);
  745. end;
  746. end
  747. else
  748. begin
  749. if reg1 <> reg2 then
  750. begin
  751. { same size, only a register mov required }
  752. instr := taicpu.op_reg_reg(A_MOVE, reg2, reg1);
  753. list.Concat(instr);
  754. // { Notify the register allocator that we have written a move instruction so
  755. // it can try to eliminate it. }
  756. add_move_instruction(instr);
  757. end;
  758. end;
  759. end;
  760. procedure TCgMPSel.a_loadaddr_ref_reg(list: tasmlist; const ref: TReference; r: tregister);
  761. var
  762. tmpref, href: treference;
  763. hreg, tmpreg: tregister;
  764. r_used: boolean;
  765. begin
  766. r_used := false;
  767. href := ref;
  768. if (href.base = NR_NO) and (href.index <> NR_NO) then
  769. internalerror(200306171);
  770. if (cs_create_pic in current_settings.moduleswitches) and
  771. assigned(href.symbol) then
  772. begin
  773. tmpreg := r; //GetIntRegister(list, OS_ADDR);
  774. r_used := true;
  775. reference_reset(tmpref,sizeof(aint));
  776. tmpref.symbol := href.symbol;
  777. tmpref.refaddr := addr_pic;
  778. if not (pi_needs_got in current_procinfo.flags) then
  779. internalerror(200501161);
  780. tmpref.base := current_procinfo.got;
  781. list.concat(taicpu.op_reg_ref(A_LW, tmpreg, tmpref));
  782. href.symbol := nil;
  783. if (href.index <> NR_NO) then
  784. begin
  785. list.concat(taicpu.op_reg_reg_reg(A_ADDU, tmpreg, href.index, tmpreg));
  786. href.index := tmpreg;
  787. end
  788. else
  789. begin
  790. if href.base <> NR_NO then
  791. href.index := tmpreg
  792. else
  793. href.base := tmpreg;
  794. end;
  795. end;
  796. if assigned(href.symbol) or
  797. (href.offset < simm16lo) or
  798. (href.offset > simm16hi) then
  799. begin
  800. if (href.base = NR_NO) and (href.index = NR_NO) then
  801. hreg := r
  802. else
  803. hreg := GetAddressRegister(list);
  804. reference_reset(tmpref,sizeof(aint));
  805. tmpref.symbol := href.symbol;
  806. tmpref.offset := href.offset;
  807. tmpref.refaddr := addr_high;
  808. list.concat(taicpu.op_reg_ref(A_LUI, hreg, tmpref));
  809. { Only the low part is left }
  810. tmpref.refaddr := addr_low;
  811. list.concat(taicpu.op_reg_reg_ref(A_ADDIU, hreg, hreg, tmpref));
  812. if href.base <> NR_NO then
  813. begin
  814. if href.index <> NR_NO then
  815. begin
  816. list.concat(taicpu.op_reg_reg_reg(A_ADDU, hreg, href.base, hreg));
  817. list.concat(taicpu.op_reg_reg_reg(A_ADDU, r, hreg, href.index));
  818. end
  819. else
  820. list.concat(taicpu.op_reg_reg_reg(A_ADDU, r, hreg, href.base));
  821. end;
  822. end
  823. else
  824. { At least small offset, maybe base and maybe index }
  825. if (href.offset >= simm16lo) and
  826. (href.offset <= simm16hi) then
  827. begin
  828. if href.index <> NR_NO then { Both base and index }
  829. begin
  830. if href.offset = 0 then
  831. begin
  832. list.concat(taicpu.op_reg_reg_reg(A_ADDU, r, href.base, href.index));
  833. end
  834. else
  835. begin
  836. if r_used then
  837. hreg := GetAddressRegister(list)
  838. else
  839. hreg := r;
  840. list.concat(taicpu.op_reg_reg_const(A_ADDIU, hreg, href.base, href.offset));
  841. list.concat(taicpu.op_reg_reg_reg(A_ADDU, r, hreg, href.index));
  842. end
  843. end
  844. else if href.base <> NR_NO then { Only base }
  845. begin
  846. list.concat(taicpu.op_reg_reg_const(A_ADDIU, r, href.base, href.offset));
  847. end
  848. else
  849. { only offset, can be generated by absolute }
  850. a_load_const_reg(list, OS_ADDR, href.offset, r);
  851. end
  852. else
  853. internalerror(200703111);
  854. end;
  855. procedure TCgMPSel.a_loadfpu_reg_reg(list: tasmlist; fromsize, tosize: tcgsize; reg1, reg2: tregister);
  856. const
  857. FpuMovInstr: array[OS_F32..OS_F64] of TAsmOp =
  858. (A_MOV_S, A_MOV_D);
  859. var
  860. instr: taicpu;
  861. begin
  862. if reg1 <> reg2 then
  863. begin
  864. instr := taicpu.op_reg_reg(fpumovinstr[tosize], reg2, reg1);
  865. list.Concat(instr);
  866. { Notify the register allocator that we have written a move instruction so
  867. it can try to eliminate it. }
  868. add_move_instruction(instr);
  869. end;
  870. end;
  871. procedure TCgMPSel.a_loadfpu_ref_reg(list: tasmlist; fromsize, tosize: tcgsize; const ref: TReference; reg: tregister);
  872. var
  873. tmpref: treference;
  874. tmpreg: tregister;
  875. begin
  876. case tosize of
  877. OS_F32:
  878. handle_load_store_fpu(list, False, A_LWC1, reg, ref);
  879. OS_F64:
  880. handle_load_store_fpu(list, False, A_LDC1, reg, ref);
  881. else
  882. InternalError(2007042701);
  883. end;
  884. end;
  885. procedure TCgMPSel.a_loadfpu_reg_ref(list: tasmlist; fromsize, tosize: tcgsize; reg: tregister; const ref: TReference);
  886. var
  887. tmpref: treference;
  888. tmpreg: tregister;
  889. begin
  890. case tosize of
  891. OS_F32:
  892. handle_load_store_fpu(list, True, A_SWC1, reg, ref);
  893. OS_F64:
  894. handle_load_store_fpu(list, True, A_SDC1, reg, ref);
  895. else
  896. InternalError(2007042702);
  897. end;
  898. end;
  899. procedure TCgMPSel.a_op_const_reg(list: tasmlist; Op: TOpCG; size: tcgsize; a: tcgint; reg: TRegister);
  900. var
  901. power: longint;
  902. tmpreg1: tregister;
  903. begin
  904. if ((op = OP_MUL) or (op = OP_IMUL)) then
  905. begin
  906. if ispowerof2(a, power) then
  907. begin
  908. { can be done with a shift }
  909. if power < 32 then
  910. begin
  911. list.concat(taicpu.op_reg_reg_const(A_SLL, reg, reg, power));
  912. exit;
  913. end;
  914. end;
  915. end;
  916. if ((op = OP_SUB) or (op = OP_ADD)) then
  917. begin
  918. if (a = 0) then
  919. exit;
  920. end;
  921. if Op in [OP_NEG, OP_NOT] then
  922. internalerror(200306011);
  923. if (a = 0) then
  924. begin
  925. if (Op = OP_IMUL) or (Op = OP_MUL) then
  926. list.concat(taicpu.op_reg_reg(A_MOVE, reg, NR_R0))
  927. else
  928. list.concat(taicpu.op_reg_reg_reg(f_TOpCG2AsmOp(op, size), reg, reg, NR_R0))
  929. end
  930. else
  931. begin
  932. if op = OP_IMUL then
  933. begin
  934. tmpreg1 := GetIntRegister(list, OS_INT);
  935. a_load_const_reg(list, OS_INT, a, tmpreg1);
  936. list.concat(taicpu.op_reg_reg(A_MULT, reg, tmpreg1));
  937. list.concat(taicpu.op_reg(A_MFLO, reg));
  938. end
  939. else if op = OP_MUL then
  940. begin
  941. tmpreg1 := GetIntRegister(list, OS_INT);
  942. a_load_const_reg(list, OS_INT, a, tmpreg1);
  943. list.concat(taicpu.op_reg_reg(A_MULTU, reg, tmpreg1));
  944. list.concat(taicpu.op_reg(A_MFLO, reg));
  945. end
  946. else
  947. handle_reg_const_reg(list, f_TOpCG2AsmOp(op, size), reg, a, reg);
  948. end;
  949. end;
  950. procedure TCgMPSel.a_op_reg_reg(list: tasmlist; Op: TOpCG; size: TCGSize; src, dst: TRegister);
  951. var
  952. a: aint;
  953. begin
  954. case Op of
  955. OP_NEG:
  956. list.concat(taicpu.op_reg_reg(A_NEG, dst, src));
  957. OP_NOT:
  958. begin
  959. list.concat(taicpu.op_reg_reg(A_NOT, dst, src));
  960. end;
  961. else
  962. begin
  963. if op = OP_IMUL then
  964. begin
  965. list.concat(taicpu.op_reg_reg(A_MULT, dst, src));
  966. list.concat(taicpu.op_reg(A_MFLO, dst));
  967. end
  968. else if op = OP_MUL then
  969. begin
  970. list.concat(taicpu.op_reg_reg(A_MULTU, dst, src));
  971. list.concat(taicpu.op_reg(A_MFLO, dst));
  972. end
  973. else
  974. begin
  975. list.concat(taicpu.op_reg_reg_reg(f_TOpCG2AsmOp(op, size), dst, dst, src));
  976. end;
  977. end;
  978. end;
  979. end;
  980. procedure TCgMPSel.a_op_const_reg_reg(list: tasmlist; op: TOpCg; size: tcgsize; a: tcgint; src, dst: tregister);
  981. var
  982. power: longint;
  983. tmpreg1: tregister;
  984. begin
  985. case op of
  986. OP_MUL,
  987. OP_IMUL:
  988. begin
  989. if ispowerof2(a, power) then
  990. begin
  991. { can be done with a shift }
  992. if power < 32 then
  993. list.concat(taicpu.op_reg_reg_const(A_SLL, dst, src, power))
  994. else
  995. inherited a_op_const_reg_reg(list, op, size, a, src, dst);
  996. exit;
  997. end;
  998. end;
  999. OP_SUB,
  1000. OP_ADD:
  1001. begin
  1002. if (a = 0) then
  1003. begin
  1004. a_load_reg_reg(list, size, size, src, dst);
  1005. exit;
  1006. end;
  1007. end;
  1008. end;
  1009. if op = OP_IMUL then
  1010. begin
  1011. tmpreg1 := GetIntRegister(list, OS_INT);
  1012. a_load_const_reg(list, OS_INT, a, tmpreg1);
  1013. list.concat(taicpu.op_reg_reg(A_MULT, src, tmpreg1));
  1014. list.concat(taicpu.op_reg(A_MFLO, dst));
  1015. end
  1016. else if op = OP_MUL then
  1017. begin
  1018. tmpreg1 := GetIntRegister(list, OS_INT);
  1019. a_load_const_reg(list, OS_INT, a, tmpreg1);
  1020. list.concat(taicpu.op_reg_reg(A_MULTU, src, tmpreg1));
  1021. list.concat(taicpu.op_reg(A_MFLO, dst));
  1022. end
  1023. else
  1024. handle_reg_const_reg(list, f_TOpCG2AsmOp(op, size), src, a, dst);
  1025. end;
  1026. procedure TCgMPSel.a_op_reg_reg_reg(list: tasmlist; op: TOpCg; size: tcgsize; src1, src2, dst: tregister);
  1027. begin
  1028. list.concat(taicpu.op_reg_reg_reg(f_TOpCG2AsmOp(op, size), dst, src2, src1));
  1029. end;
  1030. procedure TCgMPSel.a_op_const_reg_reg_checkoverflow(list: tasmlist; op: TOpCg; size: tcgsize; a: tcgint; src, dst: tregister; setflags: boolean; var ovloc: tlocation);
  1031. var
  1032. tmpreg1: tregister;
  1033. begin
  1034. ovloc.loc := LOC_VOID;
  1035. case op of
  1036. OP_SUB,
  1037. OP_ADD:
  1038. begin
  1039. if (a = 0) then
  1040. begin
  1041. a_load_reg_reg(list, size, size, src, dst);
  1042. exit;
  1043. end;
  1044. end;
  1045. end;{case}
  1046. case op of
  1047. OP_ADD:
  1048. begin
  1049. if setflags then
  1050. handle_reg_const_reg(list, f_TOpCG2AsmOp_ovf(op, size), src, a, dst)
  1051. else
  1052. handle_reg_const_reg(list, f_TOpCG2AsmOp(op, size), src, a, dst);
  1053. end;
  1054. OP_SUB:
  1055. begin
  1056. if setflags then
  1057. handle_reg_const_reg(list, f_TOpCG2AsmOp_ovf(op, size), src, a, dst)
  1058. else
  1059. handle_reg_const_reg(list, f_TOpCG2AsmOp(op, size), src, a, dst);
  1060. end;
  1061. OP_MUL:
  1062. begin
  1063. if setflags then
  1064. handle_reg_const_reg(list, f_TOpCG2AsmOp_ovf(op, size), src, a, dst)
  1065. else
  1066. begin
  1067. tmpreg1 := GetIntRegister(list, OS_INT);
  1068. a_load_const_reg(list, OS_INT, a, tmpreg1);
  1069. list.concat(taicpu.op_reg_reg(f_TOpCG2AsmOp(op, size),src, tmpreg1));
  1070. list.concat(taicpu.op_reg(A_MFLO, dst));
  1071. end;
  1072. end;
  1073. OP_IMUL:
  1074. begin
  1075. if setflags then
  1076. handle_reg_const_reg(list, f_TOpCG2AsmOp_ovf(op, size), src, a, dst)
  1077. else
  1078. begin
  1079. tmpreg1 := GetIntRegister(list, OS_INT);
  1080. a_load_const_reg(list, OS_INT, a, tmpreg1);
  1081. list.concat(taicpu.op_reg_reg(f_TOpCG2AsmOp(op, size),src, tmpreg1));
  1082. list.concat(taicpu.op_reg(A_MFLO, dst));
  1083. end;
  1084. end;
  1085. OP_XOR, OP_OR, OP_AND:
  1086. begin
  1087. handle_reg_const_reg(list, f_TOpCG2AsmOp_ovf(op, size), src, a, dst);
  1088. end;
  1089. else
  1090. internalerror(2007012601);
  1091. end;
  1092. end;
  1093. procedure TCgMPSel.a_op_reg_reg_reg_checkoverflow(list: tasmlist; op: TOpCg; size: tcgsize; src1, src2, dst: tregister; setflags: boolean; var ovloc: tlocation);
  1094. begin
  1095. ovloc.loc := LOC_VOID;
  1096. case op of
  1097. OP_ADD:
  1098. begin
  1099. if setflags then
  1100. list.concat(taicpu.op_reg_reg_reg(f_TOpCG2AsmOp_ovf(op, size), dst, src2, src1))
  1101. else
  1102. list.concat(taicpu.op_reg_reg_reg(f_TOpCG2AsmOp(op, size), dst, src2, src1));
  1103. end;
  1104. OP_SUB:
  1105. begin
  1106. if setflags then
  1107. list.concat(taicpu.op_reg_reg_reg(f_TOpCG2AsmOp_ovf(op, size), dst, src2, src1))
  1108. else
  1109. list.concat(taicpu.op_reg_reg_reg(f_TOpCG2AsmOp(op, size), dst, src2, src1));
  1110. end;
  1111. OP_MUL:
  1112. begin
  1113. if setflags then
  1114. list.concat(taicpu.op_reg_reg_reg(f_TOpCG2AsmOp_ovf(op, size), dst, src2, src1))
  1115. else
  1116. begin
  1117. list.concat(taicpu.op_reg_reg(f_TOpCG2AsmOp(op, size), src2, src1));
  1118. list.concat(taicpu.op_reg(A_MFLO, dst));
  1119. end;
  1120. end;
  1121. OP_IMUL:
  1122. begin
  1123. if setflags then
  1124. list.concat(taicpu.op_reg_reg_reg(f_TOpCG2AsmOp_ovf(op, size), dst, src2, src1))
  1125. else
  1126. begin
  1127. list.concat(taicpu.op_reg_reg(f_TOpCG2AsmOp(op, size), src2, src1));
  1128. list.concat(taicpu.op_reg(A_MFLO, dst));
  1129. end;
  1130. end;
  1131. OP_XOR, OP_OR, OP_AND:
  1132. begin
  1133. list.concat(taicpu.op_reg_reg_reg(f_TOpCG2AsmOp_ovf(op, size), dst, src2, src1));
  1134. end;
  1135. else
  1136. internalerror(2007012602);
  1137. end;
  1138. end;
  1139. {*************** compare instructructions ****************}
  1140. procedure TCgMPSel.a_cmp_const_reg_label(list: tasmlist; size: tcgsize; cmp_op: topcmp; a: tcgint; reg: tregister; l: tasmlabel);
  1141. var
  1142. tmpreg: tregister;
  1143. begin
  1144. if a = 0 then
  1145. tmpreg := NR_R0
  1146. else
  1147. begin
  1148. tmpreg := GetIntRegister(list, OS_INT);
  1149. list.concat(taicpu.op_reg_const(A_LI, tmpreg, a));
  1150. end;
  1151. case cmp_op of
  1152. OC_EQ: { equality comparison }
  1153. list.concat(taicpu.op_reg_reg_sym(A_BEQ, reg, tmpreg, l));
  1154. OC_GT: { greater than (signed) }
  1155. list.concat(taicpu.op_reg_reg_sym(A_BGT, reg, tmpreg, l));
  1156. OC_LT: { less than (signed) }
  1157. list.concat(taicpu.op_reg_reg_sym(A_BLT, reg, tmpreg, l));
  1158. OC_GTE: { greater or equal than (signed) }
  1159. list.concat(taicpu.op_reg_reg_sym(A_BGE, reg, tmpreg, l));
  1160. OC_LTE: { less or equal than (signed) }
  1161. list.concat(taicpu.op_reg_reg_sym(A_BLE, reg, tmpreg, l));
  1162. OC_NE: { not equal }
  1163. list.concat(taicpu.op_reg_reg_sym(A_BNE, reg, tmpreg, l));
  1164. OC_BE: { less or equal than (unsigned) }
  1165. list.concat(taicpu.op_reg_reg_sym(A_BLEU, reg, tmpreg, l));
  1166. OC_B: { less than (unsigned) }
  1167. list.concat(taicpu.op_reg_reg_sym(A_BLTU, reg, tmpreg, l));
  1168. OC_AE: { greater or equal than (unsigned) }
  1169. list.concat(taicpu.op_reg_reg_sym(A_BGEU, reg, tmpreg, l));
  1170. OC_A: { greater than (unsigned) }
  1171. list.concat(taicpu.op_reg_reg_sym(A_BGTU, reg, tmpreg, l));
  1172. else
  1173. internalerror(200701071);
  1174. end;
  1175. list.Concat(TAiCpu.Op_none(A_NOP));
  1176. end;
  1177. procedure TCgMPSel.a_cmp_reg_reg_label(list: tasmlist; size: tcgsize; cmp_op: topcmp; reg1, reg2: tregister; l: tasmlabel);
  1178. begin
  1179. case cmp_op of
  1180. OC_EQ: { equality comparison }
  1181. list.concat(taicpu.op_reg_reg_sym(A_BEQ, reg2, reg1, l));
  1182. OC_GT: { greater than (signed) }
  1183. list.concat(taicpu.op_reg_reg_sym(A_BGT, reg2, reg1, l));
  1184. OC_LT: { less than (signed) }
  1185. list.concat(taicpu.op_reg_reg_sym(A_BLT, reg2, reg1, l));
  1186. OC_GTE: { greater or equal than (signed) }
  1187. list.concat(taicpu.op_reg_reg_sym(A_BGE, reg2, reg1, l));
  1188. OC_LTE: { less or equal than (signed) }
  1189. list.concat(taicpu.op_reg_reg_sym(A_BLE, reg2, reg1, l));
  1190. OC_NE: { not equal }
  1191. list.concat(taicpu.op_reg_reg_sym(A_BNE, reg2, reg1, l));
  1192. OC_BE: { less or equal than (unsigned) }
  1193. list.concat(taicpu.op_reg_reg_sym(A_BLEU, reg2, reg1, l));
  1194. OC_B: { less than (unsigned) }
  1195. list.concat(taicpu.op_reg_reg_sym(A_BLTU, reg2, reg1, l));
  1196. OC_AE: { greater or equal than (unsigned) }
  1197. list.concat(taicpu.op_reg_reg_sym(A_BGEU, reg2, reg1, l));
  1198. OC_A: { greater than (unsigned) }
  1199. list.concat(taicpu.op_reg_reg_sym(A_BGTU, reg2, reg1, l));
  1200. else
  1201. internalerror(200701072);
  1202. end;{ case }
  1203. list.Concat(TAiCpu.Op_none(A_NOP));
  1204. end;
  1205. procedure TCgMPSel.a_jmp_always(List: tasmlist; l: TAsmLabel);
  1206. begin
  1207. List.Concat(TAiCpu.op_sym(A_J,l));
  1208. { Delay slot }
  1209. list.Concat(TAiCpu.Op_none(A_NOP));
  1210. end;
  1211. procedure TCgMPSel.a_jmp_name(list: tasmlist; const s: string);
  1212. begin
  1213. List.Concat(TAiCpu.op_sym(A_J, current_asmdata.RefAsmSymbol(s)));
  1214. { Delay slot }
  1215. list.Concat(TAiCpu.Op_none(A_NOP));
  1216. end;
  1217. procedure TCgMPSel.a_jmp_cond(list: tasmlist; cond: TOpCmp; l: TAsmLabel);
  1218. begin
  1219. internalerror(200701181);
  1220. end;
  1221. procedure TCgMPSel.g_overflowCheck(List: tasmlist; const Loc: TLocation; def: TDef);
  1222. begin
  1223. // this is an empty procedure
  1224. end;
  1225. procedure TCgMPSel.g_overflowCheck_loc(List: tasmlist; const Loc: TLocation; def: TDef; ovloc: tlocation);
  1226. begin
  1227. // this is an empty procedure
  1228. end;
  1229. { *********** entry/exit code and address loading ************ }
  1230. procedure TCgMPSel.g_proc_entry(list: tasmlist; localsize: longint; nostackframe: boolean);
  1231. var
  1232. regcounter, firstregfpu, firstreggpr: TSuperRegister;
  1233. href: treference;
  1234. usesfpr, usesgpr, gotgot: boolean;
  1235. regcounter2, firstfpureg: Tsuperregister;
  1236. cond: tasmcond;
  1237. instr: taicpu;
  1238. begin
  1239. if STK2_dummy <> 0 then
  1240. begin
  1241. list.concat(Taicpu.Op_reg_reg_const(A_P_STK2, STK2_PTR, STK2_PTR, -STK2_dummy));
  1242. end;
  1243. if nostackframe then
  1244. exit;
  1245. usesfpr := False;
  1246. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1247. case target_info.abi of
  1248. abi_powerpc_aix:
  1249. firstfpureg := RS_F14;
  1250. abi_powerpc_sysv:
  1251. firstfpureg := RS_F14;
  1252. abi_default:
  1253. firstfpureg := RS_F14;
  1254. else
  1255. internalerror(2003122903);
  1256. end;
  1257. for regcounter := firstfpureg to RS_F31 do
  1258. begin
  1259. if regcounter in rg[R_FPUREGISTER].used_in_proc then
  1260. begin
  1261. usesfpr := True;
  1262. firstregfpu := regcounter;
  1263. break;
  1264. end;
  1265. end;
  1266. usesgpr := False;
  1267. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1268. for regcounter2 := RS_R13 to RS_R31 do
  1269. begin
  1270. if regcounter2 in rg[R_INTREGISTER].used_in_proc then
  1271. begin
  1272. usesgpr := True;
  1273. firstreggpr := regcounter2;
  1274. break;
  1275. end;
  1276. end;
  1277. LocalSize := align(LocalSize, 8);
  1278. cgcpu_calc_stackframe_size := LocalSize;
  1279. list.concat(Taicpu.Op_reg_reg_const(A_P_FRAME, NR_FRAME_POINTER_REG, NR_R31, LocalSize));
  1280. list.concat(Taicpu.op_none(A_P_SET_NOREORDER));
  1281. list.concat(Taicpu.op_none(A_P_SET_NOMACRO));
  1282. list.concat(Taicpu.Op_reg_reg_const(A_P_SW, NR_FRAME_POINTER_REG, NR_STACK_POINTER_REG, -LocalSize));
  1283. list.concat(Taicpu.Op_reg_reg_const(A_P_SW, NR_R31, NR_STACK_POINTER_REG, -LocalSize + 4));
  1284. list.concat(Taicpu.op_reg_reg(A_MOVE, NR_FRAME_POINTER_REG, NR_STACK_POINTER_REG));
  1285. list.concat(Taicpu.Op_reg_reg_const(A_ADDIU, NR_STACK_POINTER_REG, NR_STACK_POINTER_REG, -LocalSize));
  1286. if (cs_create_pic in current_settings.moduleswitches) and
  1287. (pi_needs_got in current_procinfo.flags) then
  1288. begin
  1289. current_procinfo.got := NR_GP;
  1290. end;
  1291. end;
  1292. procedure TCgMPSel.g_proc_exit(list: tasmlist; parasize: longint; nostackframe: boolean);
  1293. var
  1294. hr: treference;
  1295. localsize: aint;
  1296. begin
  1297. localsize := cgcpu_calc_stackframe_size;
  1298. if paramanager.ret_in_param(current_procinfo.procdef.returndef, current_procinfo.procdef.proccalloption) then
  1299. begin
  1300. reference_reset(hr,sizeof(aint));
  1301. hr.offset := 12;
  1302. hr.refaddr := addr_full;
  1303. if nostackframe then
  1304. begin
  1305. if STK2_dummy <> 0 then
  1306. list.concat(Taicpu.Op_reg_reg_const(A_P_STK2, STK2_PTR, STK2_PTR, STK2_dummy));
  1307. list.concat(taicpu.op_reg(A_J, NR_R31));
  1308. list.concat(Taicpu.op_none(A_NOP));
  1309. end
  1310. else
  1311. begin
  1312. list.concat(Taicpu.Op_reg_reg_const(A_P_LW, NR_FRAME_POINTER_REG, NR_STACK_POINTER_REG, 0));
  1313. list.concat(Taicpu.Op_reg_reg_const(A_P_LW, NR_R31, NR_STACK_POINTER_REG, 4));
  1314. list.concat(Taicpu.Op_reg_reg_const(A_ADDIU, NR_STACK_POINTER_REG, NR_STACK_POINTER_REG, localsize));
  1315. if STK2_dummy <> 0 then
  1316. list.concat(Taicpu.Op_reg_reg_const(A_P_STK2, STK2_PTR, STK2_PTR, STK2_dummy));
  1317. list.concat(taicpu.op_reg(A_J, NR_R31));
  1318. list.concat(Taicpu.op_none(A_NOP));
  1319. list.concat(Taicpu.op_none(A_P_SET_MACRO));
  1320. list.concat(Taicpu.op_none(A_P_SET_REORDER));
  1321. end;
  1322. end
  1323. else
  1324. begin
  1325. if nostackframe then
  1326. begin
  1327. if STK2_dummy <> 0 then
  1328. list.concat(Taicpu.Op_reg_reg_const(A_P_STK2, STK2_PTR, STK2_PTR, STK2_dummy));
  1329. list.concat(taicpu.op_reg(A_J, NR_R31));
  1330. list.concat(Taicpu.op_none(A_NOP));
  1331. list.concat(Taicpu.op_none(A_P_SET_MACRO));
  1332. list.concat(Taicpu.op_none(A_P_SET_REORDER));
  1333. end
  1334. else
  1335. begin
  1336. list.concat(Taicpu.Op_reg_reg_const(A_P_LW, NR_FRAME_POINTER_REG, NR_STACK_POINTER_REG, 0));
  1337. list.concat(Taicpu.Op_reg_reg_const(A_P_LW, NR_R31, NR_STACK_POINTER_REG, 4));
  1338. list.concat(Taicpu.Op_reg_reg_const(A_ADDIU, NR_STACK_POINTER_REG, NR_STACK_POINTER_REG, localsize));
  1339. if STK2_dummy <> 0 then
  1340. list.concat(Taicpu.Op_reg_reg_const(A_P_STK2, STK2_PTR, STK2_PTR, STK2_dummy));
  1341. list.concat(taicpu.op_reg(A_J, NR_R31));
  1342. list.concat(Taicpu.op_none(A_NOP));
  1343. list.concat(Taicpu.op_none(A_P_SET_MACRO));
  1344. list.concat(Taicpu.op_none(A_P_SET_REORDER));
  1345. end;
  1346. end;
  1347. end;
  1348. { ************* concatcopy ************ }
  1349. procedure TCgMPSel.g_concatcopy_move(list: tasmlist; const Source, dest: treference; len: tcgint);
  1350. var
  1351. paraloc1, paraloc2, paraloc3: TCGPara;
  1352. begin
  1353. paraloc1.init;
  1354. paraloc2.init;
  1355. paraloc3.init;
  1356. paramanager.getintparaloc(pocall_default, 1, paraloc1);
  1357. paramanager.getintparaloc(pocall_default, 2, paraloc2);
  1358. paramanager.getintparaloc(pocall_default, 3, paraloc3);
  1359. a_load_const_cgpara(list, OS_INT, len, paraloc3);
  1360. a_loadaddr_ref_cgpara(list, dest, paraloc2);
  1361. a_loadaddr_ref_cgpara(list, Source, paraloc1);
  1362. paramanager.freecgpara(list, paraloc3);
  1363. paramanager.freecgpara(list, paraloc2);
  1364. paramanager.freecgpara(list, paraloc1);
  1365. alloccpuregisters(list, R_INTREGISTER, paramanager.get_volatile_registers_int(pocall_default));
  1366. alloccpuregisters(list, R_FPUREGISTER, paramanager.get_volatile_registers_fpu(pocall_default));
  1367. a_call_name(list, 'FPC_MOVE', false);
  1368. dealloccpuregisters(list, R_FPUREGISTER, paramanager.get_volatile_registers_fpu(pocall_default));
  1369. dealloccpuregisters(list, R_INTREGISTER, paramanager.get_volatile_registers_int(pocall_default));
  1370. paraloc3.done;
  1371. paraloc2.done;
  1372. paraloc1.done;
  1373. end;
  1374. procedure TCgMPSel.g_concatcopy(list: tasmlist; const Source, dest: treference; len: tcgint);
  1375. var
  1376. tmpreg1, hreg, countreg: TRegister;
  1377. src, dst: TReference;
  1378. lab: tasmlabel;
  1379. Count, count2: aint;
  1380. begin
  1381. if len > high(longint) then
  1382. internalerror(2002072704);
  1383. { anybody wants to determine a good value here :)? }
  1384. if len > 100 then
  1385. g_concatcopy_move(list, Source, dest, len)
  1386. else
  1387. begin
  1388. reference_reset(src,sizeof(aint));
  1389. reference_reset(dst,sizeof(aint));
  1390. { load the address of source into src.base }
  1391. src.base := GetAddressRegister(list);
  1392. a_loadaddr_ref_reg(list, Source, src.base);
  1393. { load the address of dest into dst.base }
  1394. dst.base := GetAddressRegister(list);
  1395. a_loadaddr_ref_reg(list, dest, dst.base);
  1396. { generate a loop }
  1397. Count := len div 4;
  1398. if Count > 4 then
  1399. begin
  1400. { the offsets are zero after the a_loadaddress_ref_reg and just }
  1401. { have to be set to 8. I put an Inc there so debugging may be }
  1402. { easier (should offset be different from zero here, it will be }
  1403. { easy to notice in the generated assembler }
  1404. countreg := GetIntRegister(list, OS_INT);
  1405. tmpreg1 := GetIntRegister(list, OS_INT);
  1406. a_load_const_reg(list, OS_INT, Count, countreg);
  1407. { explicitely allocate R_O0 since it can be used safely here }
  1408. { (for holding date that's being copied) }
  1409. current_asmdata.getjumplabel(lab);
  1410. a_label(list, lab);
  1411. list.concat(taicpu.op_reg_ref(A_LW, tmpreg1, src));
  1412. list.concat(taicpu.op_reg_ref(A_SW, tmpreg1, dst));
  1413. list.concat(taicpu.op_reg_reg_const(A_ADDIU, src.base, src.base, 4));
  1414. list.concat(taicpu.op_reg_reg_const(A_ADDIU, dst.base, dst.base, 4));
  1415. list.concat(taicpu.op_reg_reg_const(A_ADDIU, countreg, countreg, -1));
  1416. list.concat(taicpu.op_reg_sym(A_BGTZ, countreg, lab));
  1417. list.concat(taicpu.op_none(A_NOP));
  1418. len := len mod 4;
  1419. end;
  1420. { unrolled loop }
  1421. Count := len div 4;
  1422. if Count > 0 then
  1423. begin
  1424. tmpreg1 := GetIntRegister(list, OS_INT);
  1425. for count2 := 1 to Count do
  1426. begin
  1427. list.concat(taicpu.op_reg_ref(A_LW, tmpreg1, src));
  1428. list.concat(taicpu.op_reg_ref(A_SW, tmpreg1, dst));
  1429. Inc(src.offset, 4);
  1430. Inc(dst.offset, 4);
  1431. end;
  1432. len := len mod 4;
  1433. end;
  1434. if (len and 4) <> 0 then
  1435. begin
  1436. hreg := GetIntRegister(list, OS_INT);
  1437. a_load_ref_reg(list, OS_32, OS_32, src, hreg);
  1438. a_load_reg_ref(list, OS_32, OS_32, hreg, dst);
  1439. Inc(src.offset, 4);
  1440. Inc(dst.offset, 4);
  1441. end;
  1442. { copy the leftovers }
  1443. if (len and 2) <> 0 then
  1444. begin
  1445. hreg := GetIntRegister(list, OS_INT);
  1446. a_load_ref_reg(list, OS_16, OS_16, src, hreg);
  1447. a_load_reg_ref(list, OS_16, OS_16, hreg, dst);
  1448. Inc(src.offset, 2);
  1449. Inc(dst.offset, 2);
  1450. end;
  1451. if (len and 1) <> 0 then
  1452. begin
  1453. hreg := GetIntRegister(list, OS_INT);
  1454. a_load_ref_reg(list, OS_8, OS_8, src, hreg);
  1455. a_load_reg_ref(list, OS_8, OS_8, hreg, dst);
  1456. end;
  1457. end;
  1458. end;
  1459. procedure TCgMPSel.g_concatcopy_unaligned(list: tasmlist; const Source, dest: treference; len: tcgint);
  1460. var
  1461. src, dst: TReference;
  1462. tmpreg1, countreg: TRegister;
  1463. i: aint;
  1464. lab: tasmlabel;
  1465. begin
  1466. if len > 31 then
  1467. g_concatcopy_move(list, Source, dest, len)
  1468. else
  1469. begin
  1470. reference_reset(src,sizeof(aint));
  1471. reference_reset(dst,sizeof(aint));
  1472. { load the address of source into src.base }
  1473. src.base := GetAddressRegister(list);
  1474. a_loadaddr_ref_reg(list, Source, src.base);
  1475. { load the address of dest into dst.base }
  1476. dst.base := GetAddressRegister(list);
  1477. a_loadaddr_ref_reg(list, dest, dst.base);
  1478. { generate a loop }
  1479. if len > 4 then
  1480. begin
  1481. { the offsets are zero after the a_loadaddress_ref_reg and just }
  1482. { have to be set to 8. I put an Inc there so debugging may be }
  1483. { easier (should offset be different from zero here, it will be }
  1484. { easy to notice in the generated assembler }
  1485. countreg := GetIntRegister(list, OS_INT);
  1486. tmpreg1 := GetIntRegister(list, OS_INT);
  1487. a_load_const_reg(list, OS_INT, len, countreg);
  1488. { explicitely allocate R_O0 since it can be used safely here }
  1489. { (for holding date that's being copied) }
  1490. current_asmdata.getjumplabel(lab);
  1491. a_label(list, lab);
  1492. list.concat(taicpu.op_reg_ref(A_LBU, tmpreg1, src));
  1493. list.concat(taicpu.op_reg_ref(A_SB, tmpreg1, dst));
  1494. list.concat(taicpu.op_reg_reg_const(A_ADDIU, src.base, src.base, 1));
  1495. list.concat(taicpu.op_reg_reg_const(A_ADDIU, dst.base, dst.base, 1));
  1496. list.concat(taicpu.op_reg_reg_const(A_ADDIU, countreg, countreg, -1));
  1497. list.concat(taicpu.op_reg_sym(A_BGTZ, countreg, lab));
  1498. list.concat(taicpu.op_none(A_NOP));
  1499. end
  1500. else
  1501. begin
  1502. { unrolled loop }
  1503. tmpreg1 := GetIntRegister(list, OS_INT);
  1504. for i := 1 to len do
  1505. begin
  1506. list.concat(taicpu.op_reg_ref(A_LBU, tmpreg1, src));
  1507. list.concat(taicpu.op_reg_ref(A_SB, tmpreg1, dst));
  1508. Inc(src.offset);
  1509. Inc(dst.offset);
  1510. end;
  1511. end;
  1512. end;
  1513. end;
  1514. procedure TCgMPSel.g_intf_wrapper(list: tasmlist; procdef: tprocdef; const labelname: string; ioffset: longint);
  1515. procedure loadvmttor24;
  1516. var
  1517. href: treference;
  1518. begin
  1519. reference_reset_base(href, NR_R2, 0, sizeof(aint)); { return value }
  1520. cg.a_load_ref_reg(list, OS_ADDR, OS_ADDR, href, NR_R24);
  1521. end;
  1522. procedure op_onr24methodaddr;
  1523. var
  1524. href : treference;
  1525. begin
  1526. if (procdef.extnumber=$ffff) then
  1527. Internalerror(200006139);
  1528. { call/jmp vmtoffs(%eax) ; method offs }
  1529. reference_reset_base(href, NR_R24, tobjectdef(procdef.struct).vmtmethodoffset(procdef.extnumber), sizeof(aint));
  1530. cg.a_load_ref_reg(list, OS_ADDR, OS_ADDR, href, NR_R24);
  1531. list.concat(taicpu.op_reg(A_JR, NR_R24));
  1532. end;
  1533. var
  1534. make_global: boolean;
  1535. href: treference;
  1536. begin
  1537. if not(procdef.proctypeoption in [potype_function,potype_procedure]) then
  1538. Internalerror(200006137);
  1539. if not assigned(procdef.struct) or
  1540. (procdef.procoptions * [po_classmethod, po_staticmethod,
  1541. po_methodpointer, po_interrupt, po_iocheck] <> []) then
  1542. Internalerror(200006138);
  1543. if procdef.owner.symtabletype <> objectsymtable then
  1544. Internalerror(200109191);
  1545. make_global := False;
  1546. if (not current_module.is_unit) or create_smartlink or
  1547. (procdef.owner.defowner.owner.symtabletype = globalsymtable) then
  1548. make_global := True;
  1549. if make_global then
  1550. List.concat(Tai_symbol.Createname_global(labelname, AT_FUNCTION, 0))
  1551. else
  1552. List.concat(Tai_symbol.Createname(labelname, AT_FUNCTION, 0));
  1553. { set param1 interface to self }
  1554. g_adjust_self_value(list, procdef, ioffset);
  1555. if (po_virtualmethod in procdef.procoptions) and
  1556. not is_objectpascal_helper(procdef.struct) then
  1557. begin
  1558. loadvmttor24;
  1559. op_onr24methodaddr;
  1560. end
  1561. else
  1562. list.concat(taicpu.op_sym(A_B,current_asmdata.RefAsmSymbol(procdef.mangledname)));
  1563. { Delay slot }
  1564. list.Concat(TAiCpu.Op_none(A_NOP));
  1565. List.concat(Tai_symbol_end.Createname(labelname));
  1566. end;
  1567. procedure TCgMPSel.g_stackpointer_alloc(list : TAsmList;localsize : longint);
  1568. begin
  1569. Comment(V_Error,'TCgMPSel.g_stackpointer_alloc method not implemented');
  1570. end;
  1571. procedure TCgMPSel.a_bit_scan_reg_reg(list: TAsmList; reverse: boolean; size: TCGSize; src, dst: TRegister);
  1572. begin
  1573. Comment(V_Error,'TCgMPSel.a_bit_scan_reg_reg method not implemented');
  1574. end;
  1575. {****************************************************************************
  1576. TCG64_MIPSel
  1577. ****************************************************************************}
  1578. procedure TCg64MPSel.a_load64_reg_ref(list: tasmlist; reg: tregister64; const ref: treference);
  1579. var
  1580. tmpref: treference;
  1581. begin
  1582. { Override this function to prevent loading the reference twice }
  1583. tmpref := ref;
  1584. cg.a_load_reg_ref(list, OS_S32, OS_S32, reg.reglo, tmpref);
  1585. Inc(tmpref.offset, 4);
  1586. cg.a_load_reg_ref(list, OS_S32, OS_S32, reg.reghi, tmpref);
  1587. end;
  1588. procedure TCg64MPSel.a_load64_ref_reg(list: tasmlist; const ref: treference; reg: tregister64);
  1589. var
  1590. tmpref: treference;
  1591. begin
  1592. { Override this function to prevent loading the reference twice }
  1593. tmpref := ref;
  1594. cg.a_load_ref_reg(list, OS_S32, OS_S32, tmpref, reg.reglo);
  1595. Inc(tmpref.offset, 4);
  1596. cg.a_load_ref_reg(list, OS_S32, OS_S32, tmpref, reg.reghi);
  1597. end;
  1598. procedure TCg64MPSel.a_load64_ref_cgpara(list: tasmlist; const r: treference; const paraloc: tcgpara);
  1599. var
  1600. hreg64: tregister64;
  1601. begin
  1602. { Override this function to prevent loading the reference twice.
  1603. Use here some extra registers, but those are optimized away by the RA }
  1604. hreg64.reglo := cg.GetIntRegister(list, OS_S32);
  1605. hreg64.reghi := cg.GetIntRegister(list, OS_S32);
  1606. a_load64_ref_reg(list, r, hreg64);
  1607. a_load64_reg_cgpara(list, hreg64, paraloc);
  1608. end;
  1609. procedure TCg64MPSel.a_op64_reg_reg(list: tasmlist; op: TOpCG; size: tcgsize; regsrc, regdst: TRegister64);
  1610. var
  1611. op1, op2, op_call64: TAsmOp;
  1612. tmpreg1, tmpreg2: TRegister;
  1613. begin
  1614. tmpreg1 := NR_TCR12; //GetIntRegister(list, OS_INT);
  1615. tmpreg2 := NR_TCR13; //GetIntRegister(list, OS_INT);
  1616. case op of
  1617. OP_ADD:
  1618. begin
  1619. list.concat(taicpu.op_reg_reg_reg(A_ADDU, tmpreg1, regsrc.reglo, regdst.reglo));
  1620. list.concat(taicpu.op_reg_reg_reg(A_SLTU, NR_TCR10, tmpreg1, regsrc.reglo));
  1621. list.concat(taicpu.op_reg_reg_reg(A_ADDU, tmpreg2, regsrc.reghi, regdst.reghi));
  1622. list.concat(taicpu.op_reg_reg_reg(A_ADDU, NR_TCR10, NR_TCR10, tmpreg2));
  1623. list.concat(Taicpu.Op_reg_reg(A_MOVE, regdst.reglo, tmpreg1));
  1624. list.concat(Taicpu.Op_reg_reg(A_MOVE, regdst.reghi, NR_TCR10));
  1625. exit;
  1626. end;
  1627. OP_AND:
  1628. begin
  1629. list.concat(taicpu.op_reg_reg_reg(A_AND, regdst.reglo, regsrc.reglo, regdst.reglo));
  1630. list.concat(taicpu.op_reg_reg_reg(A_AND, regdst.reghi, regsrc.reghi, regdst.reghi));
  1631. exit;
  1632. end;
  1633. OP_NEG:
  1634. begin
  1635. list.concat(taicpu.op_reg_reg_reg(A_SUBU, regdst.reglo, NR_R0, regsrc.reglo));
  1636. list.concat(taicpu.op_reg_reg_reg(A_SLTU, NR_TCR10, NR_R0, regdst.reglo));
  1637. list.concat(taicpu.op_reg_reg_reg(A_SUBU, regdst.reghi, NR_R0, regsrc.reghi));
  1638. list.concat(taicpu.op_reg_reg_reg(A_SUBU, NR_TCR10, regdst.reghi, NR_TCR10));
  1639. list.concat(Taicpu.Op_reg_reg(A_MOVE, regdst.reghi, NR_TCR10));
  1640. exit;
  1641. end;
  1642. OP_NOT:
  1643. begin
  1644. list.concat(taicpu.op_reg_reg_reg(A_NOR, regdst.reglo, NR_R0, regsrc.reglo));
  1645. list.concat(taicpu.op_reg_reg_reg(A_NOR, regdst.reghi, NR_R0, regsrc.reghi));
  1646. exit;
  1647. end;
  1648. OP_OR:
  1649. begin
  1650. list.concat(taicpu.op_reg_reg_reg(A_OR, regdst.reglo, regsrc.reglo, regdst.reglo));
  1651. list.concat(taicpu.op_reg_reg_reg(A_OR, regdst.reghi, regsrc.reghi, regdst.reghi));
  1652. exit;
  1653. end;
  1654. OP_SUB:
  1655. begin
  1656. list.concat(taicpu.op_reg_reg_reg(A_SUBU, tmpreg1, regdst.reglo, regsrc.reglo));
  1657. list.concat(taicpu.op_reg_reg_reg(A_SLTU, NR_TCR10, regdst.reglo, tmpreg1));
  1658. list.concat(taicpu.op_reg_reg_reg(A_SUBU, tmpreg2, regdst.reghi, regsrc.reghi));
  1659. list.concat(taicpu.op_reg_reg_reg(A_SUBU, tmpreg2, tmpreg2, NR_TCR10));
  1660. list.concat(Taicpu.Op_reg_reg(A_MOVE, regdst.reglo, tmpreg1));
  1661. list.concat(Taicpu.Op_reg_reg(A_MOVE, regdst.reghi, tmpreg2));
  1662. exit;
  1663. end;
  1664. OP_XOR:
  1665. begin
  1666. list.concat(taicpu.op_reg_reg_reg(A_XOR, regdst.reglo, regdst.reglo, regsrc.reglo));
  1667. list.concat(taicpu.op_reg_reg_reg(A_XOR, regdst.reghi, regsrc.reghi, regdst.reghi));
  1668. exit;
  1669. end;
  1670. else
  1671. internalerror(200306017);
  1672. end; {case}
  1673. end;
  1674. procedure TCg64MPSel.a_op64_const_reg(list: tasmlist; op: TOpCG; size: tcgsize; Value: int64; regdst: TRegister64);
  1675. var
  1676. op1, op2: TAsmOp;
  1677. begin
  1678. case op of
  1679. OP_NEG,
  1680. OP_NOT:
  1681. internalerror(200306017);
  1682. end;
  1683. a_op64_const_reg_reg(list, op, size, value, regdst, regdst);
  1684. end;
  1685. procedure TCg64MPSel.a_op64_const_reg_reg(list: tasmlist; op: TOpCG; size: tcgsize; Value: int64; regsrc, regdst: tregister64);
  1686. var
  1687. l: tlocation;
  1688. begin
  1689. a_op64_const_reg_reg_checkoverflow(list, op, size, Value, regsrc, regdst, False, l);
  1690. end;
  1691. procedure TCg64MPSel.a_op64_reg_reg_reg(list: tasmlist; op: TOpCG; size: tcgsize; regsrc1, regsrc2, regdst: tregister64);
  1692. var
  1693. l: tlocation;
  1694. begin
  1695. a_op64_reg_reg_reg_checkoverflow(list, op, size, regsrc1, regsrc2, regdst, False, l);
  1696. end;
  1697. procedure TCg64MPSel.a_op64_const_reg_reg_checkoverflow(list: tasmlist; op: TOpCG; size: tcgsize; Value: int64; regsrc, regdst: tregister64; setflags: boolean; var ovloc: tlocation);
  1698. var
  1699. op1, op2: TAsmOp;
  1700. tmpreg1: TRegister;
  1701. begin
  1702. tmpreg1 := NR_TCR12;
  1703. case op of
  1704. OP_NEG,
  1705. OP_NOT:
  1706. internalerror(200306017);
  1707. end;
  1708. list.concat(taicpu.op_reg_const(A_LI, NR_TCR10, aint(hi(Value))));
  1709. list.concat(taicpu.op_reg_const(A_LI, NR_TCR11, aint(lo(Value))));
  1710. case op of
  1711. OP_ADD:
  1712. begin
  1713. list.concat(taicpu.op_reg_reg_reg(A_ADDU, regdst.reglo, regsrc.reglo, NR_TCR10));
  1714. list.concat(taicpu.op_reg_reg_reg(A_SLTU, tmpreg1, regdst.reglo, regsrc.reglo));
  1715. list.concat(taicpu.op_reg_reg_reg(A_ADDU, regdst.reghi, regsrc.reghi, NR_TCR11));
  1716. list.concat(taicpu.op_reg_reg_reg(A_ADDU, tmpreg1, tmpreg1, regdst.reghi));
  1717. list.concat(Taicpu.Op_reg_reg(A_MOVE, regdst.reghi, tmpreg1));
  1718. exit;
  1719. end;
  1720. OP_AND:
  1721. begin
  1722. list.concat(taicpu.op_reg_reg_reg(A_AND, regdst.reglo, regsrc.reglo, NR_TCR10));
  1723. list.concat(taicpu.op_reg_reg_reg(A_AND, regdst.reghi, regsrc.reghi, NR_TCR11));
  1724. exit;
  1725. end;
  1726. OP_OR:
  1727. begin
  1728. list.concat(taicpu.op_reg_reg_reg(A_OR, regdst.reglo, regsrc.reglo, NR_TCR10));
  1729. list.concat(taicpu.op_reg_reg_reg(A_OR, regdst.reghi, regsrc.reghi, NR_TCR11));
  1730. exit;
  1731. end;
  1732. OP_SUB:
  1733. begin
  1734. list.concat(taicpu.op_reg_reg_reg(A_SUBU, regdst.reglo, regsrc.reglo, NR_TCR10));
  1735. list.concat(taicpu.op_reg_reg_reg(A_SLTU, tmpreg1, regsrc.reglo, regdst.reglo));
  1736. list.concat(taicpu.op_reg_reg_reg(A_SUBU, regdst.reghi, regsrc.reghi, NR_TCR11));
  1737. list.concat(taicpu.op_reg_reg_reg(A_SUBU, tmpreg1, regdst.reghi, tmpreg1));
  1738. list.concat(Taicpu.Op_reg_reg(A_MOVE, regdst.reghi, tmpreg1));
  1739. exit;
  1740. end;
  1741. OP_XOR:
  1742. begin
  1743. list.concat(taicpu.op_reg_reg_reg(A_XOR, regdst.reglo, regsrc.reglo, NR_TCR10));
  1744. list.concat(taicpu.op_reg_reg_reg(A_XOR, regdst.reghi, regsrc.reghi, NR_TCR11));
  1745. exit;
  1746. end;
  1747. else
  1748. internalerror(200306017);
  1749. end;
  1750. end;
  1751. procedure TCg64MPSel.a_op64_reg_reg_reg_checkoverflow(list: tasmlist; op: TOpCG; size: tcgsize; regsrc1, regsrc2, regdst: tregister64; setflags: boolean; var ovloc: tlocation);
  1752. var
  1753. op1, op2: TAsmOp;
  1754. tmpreg1, tmpreg2: TRegister;
  1755. begin
  1756. tmpreg1 := NR_TCR12;
  1757. tmpreg2 := NR_TCR13;
  1758. case op of
  1759. OP_NEG,
  1760. OP_NOT:
  1761. internalerror(200306017);
  1762. end;
  1763. case op of
  1764. OP_ADD:
  1765. begin
  1766. list.concat(taicpu.op_reg_reg_reg(A_ADDU, tmpreg1, regsrc2.reglo, regsrc1.reglo));
  1767. list.concat(taicpu.op_reg_reg_reg(A_SLTU, NR_TCR10, tmpreg1, regsrc2.reglo));
  1768. list.concat(taicpu.op_reg_reg_reg(A_ADDU, tmpreg2, regsrc2.reghi, regsrc1.reghi));
  1769. list.concat(taicpu.op_reg_reg_reg(A_ADDU, regdst.reghi, NR_TCR10, tmpreg2));
  1770. list.concat(Taicpu.Op_reg_reg(A_MOVE, regdst.reglo, tmpreg1));
  1771. exit;
  1772. end;
  1773. OP_AND:
  1774. begin
  1775. list.concat(taicpu.op_reg_reg_reg(A_AND, regdst.reglo, regsrc2.reglo, regsrc1.reglo));
  1776. list.concat(taicpu.op_reg_reg_reg(A_AND, regdst.reghi, regsrc2.reghi, regsrc1.reghi));
  1777. exit;
  1778. end;
  1779. OP_OR:
  1780. begin
  1781. list.concat(taicpu.op_reg_reg_reg(A_OR, regdst.reglo, regsrc2.reglo, regsrc1.reglo));
  1782. list.concat(taicpu.op_reg_reg_reg(A_OR, regdst.reghi, regsrc2.reghi, regsrc1.reghi));
  1783. exit;
  1784. end;
  1785. OP_SUB:
  1786. begin
  1787. list.concat(taicpu.op_reg_reg_reg(A_SUBU, tmpreg1, regsrc2.reglo, regsrc1.reglo));
  1788. list.concat(taicpu.op_reg_reg_reg(A_SLTU, NR_TCR10, regsrc2.reglo, tmpreg1));
  1789. list.concat(taicpu.op_reg_reg_reg(A_SUBU, tmpreg2, regsrc2.reghi, regsrc1.reghi));
  1790. list.concat(taicpu.op_reg_reg_reg(A_SUBU, regdst.reghi, tmpreg2, NR_TCR10));
  1791. list.concat(Taicpu.Op_reg_reg(A_MOVE, regdst.reglo, tmpreg1));
  1792. exit;
  1793. end;
  1794. OP_XOR:
  1795. begin
  1796. list.concat(taicpu.op_reg_reg_reg(A_XOR, regdst.reglo, regsrc2.reglo, regsrc1.reglo));
  1797. list.concat(taicpu.op_reg_reg_reg(A_XOR, regdst.reghi, regsrc2.reghi, regsrc1.reghi));
  1798. exit;
  1799. end;
  1800. else
  1801. internalerror(200306017);
  1802. end; {case}
  1803. end;
  1804. procedure create_codegen;
  1805. begin
  1806. cg:=TCgMPSel.Create;
  1807. cg64:=TCg64MPSel.Create;
  1808. end;
  1809. end.