cgcpu.pas 68 KB

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