cgcpu.pas 68 KB

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