cgcpu.pas 68 KB

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