cgcpu.pas 69 KB

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