cgcpu.pas 65 KB

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