cgcpu.pas 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974
  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. begin
  974. if a = 0 then
  975. a_cmp_reg_reg_label(list,size,cmp_op,NR_R0,reg,l)
  976. else
  977. begin
  978. tmpreg := GetIntRegister(list,OS_INT);
  979. a_load_const_reg(list,OS_INT,a,tmpreg);
  980. a_cmp_reg_reg_label(list,size,cmp_op,tmpreg,reg,l);
  981. end;
  982. end;
  983. const
  984. TOpCmp2AsmCond_z : array[OC_GT..OC_LTE] of TAsmCond=(
  985. C_GTZ,C_LTZ,C_GEZ,C_LEZ
  986. );
  987. procedure TCGMIPS.a_cmp_reg_reg_label(list: tasmlist; size: tcgsize; cmp_op: topcmp; reg1, reg2: tregister; l: tasmlabel);
  988. var
  989. ai : Taicpu;
  990. begin
  991. if ((reg1=NR_R0) or (reg2=NR_R0)) and (cmp_op in [OC_GT,OC_LT,OC_GTE,OC_LTE]) then
  992. begin
  993. if (reg2=NR_R0) then
  994. begin
  995. ai:=taicpu.op_reg_sym(A_BC,reg1,l);
  996. ai.setcondition(inverse_cond(TOpCmp2AsmCond_z[cmp_op]));
  997. end
  998. else
  999. begin
  1000. ai:=taicpu.op_reg_sym(A_BC,reg2,l);
  1001. ai.setcondition(TOpCmp2AsmCond_z[cmp_op]);
  1002. end;
  1003. end
  1004. else
  1005. begin
  1006. ai:=taicpu.op_reg_reg_sym(A_BC,reg2,reg1,l);
  1007. ai.SetCondition(TOpCmp2AsmCond[cmp_op]);
  1008. end;
  1009. list.concat(ai);
  1010. { Delay slot }
  1011. list.Concat(TAiCpu.Op_none(A_NOP));
  1012. end;
  1013. procedure TCGMIPS.a_jmp_always(List: tasmlist; l: TAsmLabel);
  1014. var
  1015. ai : Taicpu;
  1016. begin
  1017. ai := taicpu.op_sym(A_BA, l);
  1018. list.concat(ai);
  1019. { Delay slot }
  1020. list.Concat(TAiCpu.Op_none(A_NOP));
  1021. end;
  1022. procedure TCGMIPS.a_jmp_name(list: tasmlist; const s: string);
  1023. begin
  1024. List.Concat(TAiCpu.op_sym(A_BA, current_asmdata.RefAsmSymbol(s)));
  1025. { Delay slot }
  1026. list.Concat(TAiCpu.Op_none(A_NOP));
  1027. end;
  1028. procedure TCGMIPS.g_overflowCheck(List: tasmlist; const Loc: TLocation; def: TDef);
  1029. begin
  1030. // this is an empty procedure
  1031. end;
  1032. procedure TCGMIPS.g_overflowCheck_loc(List: tasmlist; const Loc: TLocation; def: TDef; ovloc: tlocation);
  1033. begin
  1034. // this is an empty procedure
  1035. end;
  1036. { *********** entry/exit code and address loading ************ }
  1037. procedure TCGMIPS.g_proc_entry(list: tasmlist; localsize: longint; nostackframe: boolean);
  1038. var
  1039. lastintoffset,lastfpuoffset,
  1040. nextoffset : aint;
  1041. i : longint;
  1042. ra_save,framesave : taicpu;
  1043. fmask,mask : dword;
  1044. saveregs : tcpuregisterset;
  1045. href: treference;
  1046. reg : Tsuperregister;
  1047. helplist : TAsmList;
  1048. begin
  1049. a_reg_alloc(list,NR_STACK_POINTER_REG);
  1050. if nostackframe then
  1051. exit;
  1052. if (pi_needs_stackframe in current_procinfo.flags) then
  1053. a_reg_alloc(list,NR_FRAME_POINTER_REG);
  1054. helplist:=TAsmList.Create;
  1055. reference_reset(href,0);
  1056. href.base:=NR_STACK_POINTER_REG;
  1057. fmask:=0;
  1058. nextoffset:=TMIPSProcInfo(current_procinfo).floatregstart;
  1059. lastfpuoffset:=LocalSize;
  1060. for reg := RS_F0 to RS_F31 do { to check: what if F30 is double? }
  1061. begin
  1062. if reg in (rg[R_FPUREGISTER].used_in_proc-paramanager.get_volatile_registers_fpu(pocall_stdcall)) then
  1063. begin
  1064. fmask:=fmask or (1 shl ord(reg));
  1065. href.offset:=nextoffset;
  1066. lastfpuoffset:=nextoffset;
  1067. helplist.concat(taicpu.op_reg_ref(A_SWC1,newreg(R_FPUREGISTER,reg,R_SUBFS),href));
  1068. inc(nextoffset,4);
  1069. { IEEE Double values are stored in floating point
  1070. register pairs f2X/f2X+1,
  1071. as the f2X+1 register is not correctly marked as used for now,
  1072. we simply assume it is also used if f2X is used
  1073. Should be fixed by a proper inclusion of f2X+1 into used_in_proc }
  1074. if (ord(reg)-ord(RS_F0)) mod 2 = 0 then
  1075. include(rg[R_FPUREGISTER].used_in_proc,succ(reg));
  1076. end;
  1077. end;
  1078. mask:=0;
  1079. nextoffset:=TMIPSProcInfo(current_procinfo).intregstart;
  1080. saveregs:=rg[R_INTREGISTER].used_in_proc-paramanager.get_volatile_registers_int(pocall_stdcall);
  1081. if (current_procinfo.flags*[pi_do_call,pi_is_assembler]<>[]) then
  1082. include(saveregs,RS_R31);
  1083. if (pi_needs_stackframe in current_procinfo.flags) then
  1084. include(saveregs,RS_FRAME_POINTER_REG);
  1085. lastintoffset:=LocalSize;
  1086. framesave:=nil;
  1087. ra_save:=nil;
  1088. for reg:=RS_R1 to RS_R31 do
  1089. begin
  1090. if reg in saveregs then
  1091. begin
  1092. mask:=mask or (1 shl ord(reg));
  1093. href.offset:=nextoffset;
  1094. lastintoffset:=nextoffset;
  1095. if (reg=RS_FRAME_POINTER_REG) then
  1096. framesave:=taicpu.op_reg_ref(A_SW,newreg(R_INTREGISTER,reg,R_SUBWHOLE),href)
  1097. else if (reg=RS_R31) then
  1098. ra_save:=taicpu.op_reg_ref(A_SW,newreg(R_INTREGISTER,reg,R_SUBWHOLE),href)
  1099. else
  1100. helplist.concat(taicpu.op_reg_ref(A_SW,newreg(R_INTREGISTER,reg,R_SUBWHOLE),href));
  1101. inc(nextoffset,4);
  1102. end;
  1103. end;
  1104. //list.concat(Taicpu.Op_reg_reg_const(A_ADDIU,NR_FRAME_POINTER_REG,NR_STACK_POINTER_REG,current_procinfo.para_stack_size));
  1105. list.concat(Taicpu.op_none(A_P_SET_NOMIPS16));
  1106. list.concat(Taicpu.op_reg_const_reg(A_P_FRAME,current_procinfo.framepointer,LocalSize,NR_R31));
  1107. list.concat(Taicpu.op_const_const(A_P_MASK,mask,-(LocalSize-lastintoffset)));
  1108. list.concat(Taicpu.op_const_const(A_P_FMASK,Fmask,-(LocalSize-lastfpuoffset)));
  1109. list.concat(Taicpu.op_none(A_P_SET_NOREORDER));
  1110. if (cs_create_pic in current_settings.moduleswitches) and
  1111. (pi_needs_got in current_procinfo.flags) then
  1112. begin
  1113. list.concat(Taicpu.op_reg(A_P_CPLOAD,NR_PIC_FUNC));
  1114. end;
  1115. if (-LocalSize >= simm16lo) and (-LocalSize <= simm16hi) then
  1116. begin
  1117. list.concat(Taicpu.op_none(A_P_SET_NOMACRO));
  1118. list.concat(Taicpu.Op_reg_reg_const(A_ADDIU,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,-LocalSize));
  1119. if assigned(ra_save) then
  1120. list.concat(ra_save);
  1121. if assigned(framesave) then
  1122. begin
  1123. list.concat(framesave);
  1124. list.concat(Taicpu.op_reg_reg_const(A_ADDIU,NR_FRAME_POINTER_REG,
  1125. NR_STACK_POINTER_REG,LocalSize));
  1126. end;
  1127. end
  1128. else
  1129. begin
  1130. a_load_const_reg(list,OS_32,-LocalSize,NR_R9);
  1131. list.concat(Taicpu.Op_reg_reg_reg(A_ADDU,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,NR_R9));
  1132. if assigned(ra_save) then
  1133. list.concat(ra_save);
  1134. if assigned(framesave) then
  1135. begin
  1136. list.concat(framesave);
  1137. list.concat(Taicpu.op_reg_reg_reg(A_SUBU,NR_FRAME_POINTER_REG,
  1138. NR_STACK_POINTER_REG,NR_R9));
  1139. end;
  1140. { The instructions before are macros that can extend to multiple instructions,
  1141. the settings of R9 to -LocalSize surely does,
  1142. but the saving of RA and FP also might, and might
  1143. even use AT register, which is why we use R9 instead of AT here for -LocalSize }
  1144. list.concat(Taicpu.op_none(A_P_SET_NOMACRO));
  1145. end;
  1146. if (cs_create_pic in current_settings.moduleswitches) and
  1147. (pi_needs_got in current_procinfo.flags) then
  1148. begin
  1149. list.concat(Taicpu.op_none(A_P_SET_MACRO));
  1150. list.concat(Taicpu.op_const(A_P_CPRESTORE,TMIPSProcinfo(current_procinfo).save_gp_ref.offset));
  1151. list.concat(Taicpu.op_none(A_P_SET_NOMACRO));
  1152. end;
  1153. with TMIPSProcInfo(current_procinfo) do
  1154. begin
  1155. href.offset:=0;
  1156. //if current_procinfo.framepointer<>NR_STACK_POINTER_REG then
  1157. href.base:=NR_FRAME_POINTER_REG;
  1158. for i:=0 to MIPS_MAX_REGISTERS_USED_IN_CALL-1 do
  1159. if (register_used[i]) then
  1160. begin
  1161. reg:=parasupregs[i];
  1162. if register_offset[i]=-1 then
  1163. comment(V_warning,'Register parameter has offset -1 in TCGMIPS.g_proc_entry');
  1164. //if current_procinfo.framepointer=NR_STACK_POINTER_REG then
  1165. // href.offset:=register_offset[i]+Localsize
  1166. //else
  1167. href.offset:=register_offset[i];
  1168. list.concat(taicpu.op_reg_ref(A_SW, newreg(R_INTREGISTER,reg,R_SUBWHOLE), href));
  1169. end;
  1170. end;
  1171. list.concatList(helplist);
  1172. helplist.Free;
  1173. end;
  1174. procedure TCGMIPS.g_proc_exit(list: tasmlist; parasize: longint; nostackframe: boolean);
  1175. var
  1176. href : treference;
  1177. stacksize : aint;
  1178. saveregs : tcpuregisterset;
  1179. nextoffset : aint;
  1180. reg : Tsuperregister;
  1181. begin
  1182. stacksize:=current_procinfo.calc_stackframe_size;
  1183. if nostackframe then
  1184. begin
  1185. list.concat(taicpu.op_reg(A_JR, NR_R31));
  1186. list.concat(Taicpu.op_none(A_NOP));
  1187. list.concat(Taicpu.op_none(A_P_SET_MACRO));
  1188. list.concat(Taicpu.op_none(A_P_SET_REORDER));
  1189. end
  1190. else
  1191. begin
  1192. reference_reset(href,0);
  1193. href.base:=NR_STACK_POINTER_REG;
  1194. nextoffset:=TMIPSProcInfo(current_procinfo).floatregstart;
  1195. for reg := RS_F0 to RS_F31 do
  1196. begin
  1197. if reg in (rg[R_FPUREGISTER].used_in_proc-paramanager.get_volatile_registers_fpu(pocall_stdcall)) then
  1198. begin
  1199. href.offset:=nextoffset;
  1200. list.concat(taicpu.op_reg_ref(A_LWC1,newreg(R_FPUREGISTER,reg,R_SUBFS),href));
  1201. inc(nextoffset,4);
  1202. end;
  1203. end;
  1204. nextoffset:=TMIPSProcInfo(current_procinfo).intregstart;
  1205. saveregs:=rg[R_INTREGISTER].used_in_proc-paramanager.get_volatile_registers_int(pocall_stdcall);
  1206. if (current_procinfo.flags*[pi_do_call,pi_is_assembler]<>[]) then
  1207. include(saveregs,RS_R31);
  1208. if (pi_needs_stackframe in current_procinfo.flags) then
  1209. include(saveregs,RS_FRAME_POINTER_REG);
  1210. // GP does not need to be restored on exit
  1211. for reg:=RS_R1 to RS_R31 do
  1212. begin
  1213. if reg in saveregs then
  1214. begin
  1215. href.offset:=nextoffset;
  1216. list.concat(taicpu.op_reg_ref(A_LW,newreg(R_INTREGISTER,reg,R_SUBWHOLE),href));
  1217. inc(nextoffset,sizeof(aint));
  1218. end;
  1219. end;
  1220. if (-stacksize >= simm16lo) and (-stacksize <= simm16hi) then
  1221. begin
  1222. list.concat(taicpu.op_reg(A_JR, NR_R31));
  1223. { correct stack pointer in the delay slot }
  1224. list.concat(Taicpu.Op_reg_reg_const(A_ADDIU, NR_STACK_POINTER_REG, NR_STACK_POINTER_REG, stacksize));
  1225. end
  1226. else
  1227. begin
  1228. a_load_const_reg(list,OS_32,stacksize,NR_R1);
  1229. list.concat(taicpu.op_reg(A_JR, NR_R31));
  1230. { correct stack pointer in the delay slot }
  1231. list.concat(taicpu.op_reg_reg_reg(A_ADD,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,NR_R1));
  1232. end;
  1233. list.concat(Taicpu.op_none(A_P_SET_MACRO));
  1234. list.concat(Taicpu.op_none(A_P_SET_REORDER));
  1235. end;
  1236. end;
  1237. { ************* concatcopy ************ }
  1238. procedure TCGMIPS.g_concatcopy_move(list: tasmlist; const Source, dest: treference; len: tcgint);
  1239. var
  1240. paraloc1, paraloc2, paraloc3: TCGPara;
  1241. pd: tprocdef;
  1242. begin
  1243. pd:=search_system_proc('MOVE');
  1244. paraloc1.init;
  1245. paraloc2.init;
  1246. paraloc3.init;
  1247. paramanager.getintparaloc(pd, 1, paraloc1);
  1248. paramanager.getintparaloc(pd, 2, paraloc2);
  1249. paramanager.getintparaloc(pd, 3, paraloc3);
  1250. a_load_const_cgpara(list, OS_SINT, len, paraloc3);
  1251. a_loadaddr_ref_cgpara(list, dest, paraloc2);
  1252. a_loadaddr_ref_cgpara(list, Source, paraloc1);
  1253. paramanager.freecgpara(list, paraloc3);
  1254. paramanager.freecgpara(list, paraloc2);
  1255. paramanager.freecgpara(list, paraloc1);
  1256. alloccpuregisters(list, R_INTREGISTER, paramanager.get_volatile_registers_int(pocall_default));
  1257. alloccpuregisters(list, R_FPUREGISTER, paramanager.get_volatile_registers_fpu(pocall_default));
  1258. a_call_name(list, 'FPC_MOVE', false);
  1259. dealloccpuregisters(list, R_FPUREGISTER, paramanager.get_volatile_registers_fpu(pocall_default));
  1260. dealloccpuregisters(list, R_INTREGISTER, paramanager.get_volatile_registers_int(pocall_default));
  1261. paraloc3.done;
  1262. paraloc2.done;
  1263. paraloc1.done;
  1264. end;
  1265. procedure TCGMIPS.g_concatcopy(list: tasmlist; const Source, dest: treference; len: tcgint);
  1266. var
  1267. tmpreg1, hreg, countreg: TRegister;
  1268. src, dst: TReference;
  1269. lab: tasmlabel;
  1270. Count, count2: aint;
  1271. function reference_is_reusable(const ref: treference): boolean;
  1272. begin
  1273. result:=(ref.base<>NR_NO) and (ref.index=NR_NO) and
  1274. (ref.symbol=nil) and
  1275. (ref.alignment>=sizeof(aint)) and
  1276. (ref.offset>=simm16lo) and (ref.offset+len<=simm16hi);
  1277. end;
  1278. begin
  1279. if len > high(longint) then
  1280. internalerror(2002072704);
  1281. { A call (to FPC_MOVE) requires the outgoing parameter area to be properly
  1282. allocated on stack. This can only be done before tmipsprocinfo.set_first_temp_offset,
  1283. i.e. before secondpass. Other internal procedures request correct stack frame
  1284. by setting pi_do_call during firstpass, but for this particular one it is impossible.
  1285. Therefore, if the current procedure is a leaf one, we have to leave it that way. }
  1286. { anybody wants to determine a good value here :)? }
  1287. if (len > 100) and
  1288. assigned(current_procinfo) and
  1289. (pi_do_call in current_procinfo.flags) then
  1290. g_concatcopy_move(list, Source, dest, len)
  1291. else
  1292. begin
  1293. Count := len div 4;
  1294. if (count<=4) and reference_is_reusable(source) then
  1295. src:=source
  1296. else
  1297. begin
  1298. reference_reset(src,sizeof(aint));
  1299. { load the address of source into src.base }
  1300. src.base := GetAddressRegister(list);
  1301. a_loadaddr_ref_reg(list, Source, src.base);
  1302. end;
  1303. if (count<=4) and reference_is_reusable(dest) then
  1304. dst:=dest
  1305. else
  1306. begin
  1307. reference_reset(dst,sizeof(aint));
  1308. { load the address of dest into dst.base }
  1309. dst.base := GetAddressRegister(list);
  1310. a_loadaddr_ref_reg(list, dest, dst.base);
  1311. end;
  1312. { generate a loop }
  1313. if Count > 4 then
  1314. begin
  1315. countreg := GetIntRegister(list, OS_INT);
  1316. tmpreg1 := GetIntRegister(list, OS_INT);
  1317. a_load_const_reg(list, OS_INT, Count, countreg);
  1318. current_asmdata.getjumplabel(lab);
  1319. a_label(list, lab);
  1320. list.concat(taicpu.op_reg_ref(A_LW, tmpreg1, src));
  1321. list.concat(taicpu.op_reg_ref(A_SW, tmpreg1, dst));
  1322. list.concat(taicpu.op_reg_reg_const(A_ADDIU, src.base, src.base, 4));
  1323. list.concat(taicpu.op_reg_reg_const(A_ADDIU, dst.base, dst.base, 4));
  1324. list.concat(taicpu.op_reg_reg_const(A_ADDIU, countreg, countreg, -1));
  1325. a_cmp_reg_reg_label(list,OS_INT,OC_GT,NR_R0,countreg,lab);
  1326. len := len mod 4;
  1327. end;
  1328. { unrolled loop }
  1329. Count := len div 4;
  1330. if Count > 0 then
  1331. begin
  1332. tmpreg1 := GetIntRegister(list, OS_INT);
  1333. for count2 := 1 to Count do
  1334. begin
  1335. list.concat(taicpu.op_reg_ref(A_LW, tmpreg1, src));
  1336. list.concat(taicpu.op_reg_ref(A_SW, tmpreg1, dst));
  1337. Inc(src.offset, 4);
  1338. Inc(dst.offset, 4);
  1339. end;
  1340. len := len mod 4;
  1341. end;
  1342. if (len and 4) <> 0 then
  1343. begin
  1344. hreg := GetIntRegister(list, OS_INT);
  1345. a_load_ref_reg(list, OS_32, OS_32, src, hreg);
  1346. a_load_reg_ref(list, OS_32, OS_32, hreg, dst);
  1347. Inc(src.offset, 4);
  1348. Inc(dst.offset, 4);
  1349. end;
  1350. { copy the leftovers }
  1351. if (len and 2) <> 0 then
  1352. begin
  1353. hreg := GetIntRegister(list, OS_INT);
  1354. a_load_ref_reg(list, OS_16, OS_16, src, hreg);
  1355. a_load_reg_ref(list, OS_16, OS_16, hreg, dst);
  1356. Inc(src.offset, 2);
  1357. Inc(dst.offset, 2);
  1358. end;
  1359. if (len and 1) <> 0 then
  1360. begin
  1361. hreg := GetIntRegister(list, OS_INT);
  1362. a_load_ref_reg(list, OS_8, OS_8, src, hreg);
  1363. a_load_reg_ref(list, OS_8, OS_8, hreg, dst);
  1364. end;
  1365. end;
  1366. end;
  1367. procedure TCGMIPS.g_concatcopy_unaligned(list: tasmlist; const Source, dest: treference; len: tcgint);
  1368. var
  1369. src, dst: TReference;
  1370. tmpreg1, countreg: TRegister;
  1371. i: aint;
  1372. lab: tasmlabel;
  1373. begin
  1374. if (len > 31) and
  1375. { see comment in g_concatcopy }
  1376. assigned(current_procinfo) and
  1377. (pi_do_call in current_procinfo.flags) then
  1378. g_concatcopy_move(list, Source, dest, len)
  1379. else
  1380. begin
  1381. reference_reset(src,sizeof(aint));
  1382. reference_reset(dst,sizeof(aint));
  1383. { load the address of source into src.base }
  1384. src.base := GetAddressRegister(list);
  1385. a_loadaddr_ref_reg(list, Source, src.base);
  1386. { load the address of dest into dst.base }
  1387. dst.base := GetAddressRegister(list);
  1388. a_loadaddr_ref_reg(list, dest, dst.base);
  1389. { generate a loop }
  1390. if len > 4 then
  1391. begin
  1392. countreg := cg.GetIntRegister(list, OS_INT);
  1393. tmpreg1 := cg.GetIntRegister(list, OS_INT);
  1394. a_load_const_reg(list, OS_INT, len, countreg);
  1395. current_asmdata.getjumplabel(lab);
  1396. a_label(list, lab);
  1397. list.concat(taicpu.op_reg_ref(A_LBU, tmpreg1, src));
  1398. list.concat(taicpu.op_reg_ref(A_SB, tmpreg1, dst));
  1399. list.concat(taicpu.op_reg_reg_const(A_ADDIU, src.base, src.base, 1));
  1400. list.concat(taicpu.op_reg_reg_const(A_ADDIU, dst.base, dst.base, 1));
  1401. list.concat(taicpu.op_reg_reg_const(A_ADDIU, countreg, countreg, -1));
  1402. a_cmp_reg_reg_label(list,OS_INT,OC_GT,NR_R0,countreg,lab);
  1403. end
  1404. else
  1405. begin
  1406. { unrolled loop }
  1407. tmpreg1 := cg.GetIntRegister(list, OS_INT);
  1408. for i := 1 to len do
  1409. begin
  1410. list.concat(taicpu.op_reg_ref(A_LBU, tmpreg1, src));
  1411. list.concat(taicpu.op_reg_ref(A_SB, tmpreg1, dst));
  1412. Inc(src.offset);
  1413. Inc(dst.offset);
  1414. end;
  1415. end;
  1416. end;
  1417. end;
  1418. procedure TCGMIPS.g_intf_wrapper(list: tasmlist; procdef: tprocdef; const labelname: string; ioffset: longint);
  1419. var
  1420. make_global: boolean;
  1421. hsym: tsym;
  1422. href: treference;
  1423. paraloc: Pcgparalocation;
  1424. IsVirtual: boolean;
  1425. begin
  1426. if not(procdef.proctypeoption in [potype_function,potype_procedure]) then
  1427. Internalerror(200006137);
  1428. if not assigned(procdef.struct) or
  1429. (procdef.procoptions * [po_classmethod, po_staticmethod,
  1430. po_methodpointer, po_interrupt, po_iocheck] <> []) then
  1431. Internalerror(200006138);
  1432. if procdef.owner.symtabletype <> objectsymtable then
  1433. Internalerror(200109191);
  1434. make_global := False;
  1435. if (not current_module.is_unit) or create_smartlink or
  1436. (procdef.owner.defowner.owner.symtabletype = globalsymtable) then
  1437. make_global := True;
  1438. if make_global then
  1439. List.concat(Tai_symbol.Createname_global(labelname, AT_FUNCTION, 0))
  1440. else
  1441. List.concat(Tai_symbol.Createname(labelname, AT_FUNCTION, 0));
  1442. IsVirtual:=(po_virtualmethod in procdef.procoptions) and
  1443. not is_objectpascal_helper(procdef.struct);
  1444. if (cs_create_pic in current_settings.moduleswitches) and
  1445. (not IsVirtual) then
  1446. begin
  1447. list.concat(Taicpu.op_none(A_P_SET_NOREORDER));
  1448. list.concat(Taicpu.op_reg(A_P_CPLOAD,NR_PIC_FUNC));
  1449. list.concat(Taicpu.op_none(A_P_SET_REORDER));
  1450. end;
  1451. { set param1 interface to self }
  1452. procdef.init_paraloc_info(callerside);
  1453. hsym:=tsym(procdef.parast.Find('self'));
  1454. if not(assigned(hsym) and
  1455. (hsym.typ=paravarsym)) then
  1456. internalerror(2010103101);
  1457. paraloc:=tparavarsym(hsym).paraloc[callerside].location;
  1458. if assigned(paraloc^.next) then
  1459. InternalError(2013020101);
  1460. case paraloc^.loc of
  1461. LOC_REGISTER:
  1462. begin
  1463. if ((ioffset>=simm16lo) and (ioffset<=simm16hi)) then
  1464. a_op_const_reg(list,OP_SUB, paraloc^.size,ioffset,paraloc^.register)
  1465. else
  1466. begin
  1467. a_load_const_reg(list, paraloc^.size, ioffset, NR_R1);
  1468. a_op_reg_reg(list, OP_SUB, paraloc^.size, NR_R1, paraloc^.register);
  1469. end;
  1470. end;
  1471. else
  1472. internalerror(2010103102);
  1473. end;
  1474. if IsVirtual then
  1475. begin
  1476. { load VMT pointer }
  1477. reference_reset_base(href,paraloc^.register,0,sizeof(aint));
  1478. list.concat(taicpu.op_reg_ref(A_LW,NR_VMT,href));
  1479. if (procdef.extnumber=$ffff) then
  1480. Internalerror(200006139);
  1481. { TODO: case of large VMT is not handled }
  1482. { We have no reason not to use $t9 even in non-PIC mode. }
  1483. reference_reset_base(href, NR_VMT, tobjectdef(procdef.struct).vmtmethodoffset(procdef.extnumber), sizeof(aint));
  1484. list.concat(taicpu.op_reg_ref(A_LW,NR_PIC_FUNC,href));
  1485. list.concat(taicpu.op_reg(A_JR, NR_PIC_FUNC));
  1486. end
  1487. else if not (cs_create_pic in current_settings.moduleswitches) then
  1488. list.concat(taicpu.op_sym(A_J,current_asmdata.RefAsmSymbol(procdef.mangledname)))
  1489. else
  1490. begin
  1491. { GAS does not expand "J symbol" into PIC sequence }
  1492. reference_reset_symbol(href,current_asmdata.RefAsmSymbol(procdef.mangledname),0,sizeof(pint));
  1493. href.base:=NR_GP;
  1494. href.refaddr:=addr_pic_call16;
  1495. list.concat(taicpu.op_reg_ref(A_LW,NR_PIC_FUNC,href));
  1496. list.concat(taicpu.op_reg(A_JR,NR_PIC_FUNC));
  1497. end;
  1498. { Delay slot }
  1499. list.Concat(TAiCpu.Op_none(A_NOP));
  1500. List.concat(Tai_symbol_end.Createname(labelname));
  1501. end;
  1502. procedure TCGMIPS.g_external_wrapper(list: TAsmList; procdef: tprocdef; const externalname: string);
  1503. var
  1504. href: treference;
  1505. begin
  1506. reference_reset_symbol(href,current_asmdata.RefAsmSymbol(externalname),0,sizeof(aint));
  1507. { Always do indirect jump using $t9, it won't harm in non-PIC mode }
  1508. if (cs_create_pic in current_settings.moduleswitches) then
  1509. begin
  1510. list.concat(taicpu.op_none(A_P_SET_NOREORDER));
  1511. list.concat(taicpu.op_reg(A_P_CPLOAD,NR_PIC_FUNC));
  1512. href.base:=NR_GP;
  1513. href.refaddr:=addr_pic_call16;
  1514. list.concat(taicpu.op_reg_ref(A_LW,NR_PIC_FUNC,href));
  1515. list.concat(taicpu.op_reg(A_JR,NR_PIC_FUNC));
  1516. { Delay slot }
  1517. list.Concat(taicpu.op_none(A_NOP));
  1518. list.Concat(taicpu.op_none(A_P_SET_REORDER));
  1519. end
  1520. else
  1521. begin
  1522. href.refaddr:=addr_high;
  1523. list.concat(taicpu.op_reg_ref(A_LUI,NR_PIC_FUNC,href));
  1524. href.refaddr:=addr_low;
  1525. list.concat(taicpu.op_reg_ref(A_ADDIU,NR_PIC_FUNC,href));
  1526. list.concat(taicpu.op_reg(A_JR,NR_PIC_FUNC));
  1527. { Delay slot }
  1528. list.Concat(taicpu.op_none(A_NOP));
  1529. end;
  1530. end;
  1531. procedure TCGMIPS.g_profilecode(list:TAsmList);
  1532. var
  1533. href: treference;
  1534. begin
  1535. if not (cs_create_pic in current_settings.moduleswitches) then
  1536. begin
  1537. reference_reset_symbol(href,current_asmdata.RefAsmSymbol('_gp'),0,sizeof(pint));
  1538. a_loadaddr_ref_reg(list,href,NR_GP);
  1539. end;
  1540. list.concat(taicpu.op_reg_reg(A_MOVE,NR_R1,NR_RA));
  1541. list.concat(taicpu.op_reg_reg_const(A_ADDIU,NR_SP,NR_SP,-8));
  1542. a_call_sym_pic(list,current_asmdata.RefAsmSymbol('_mcount'));
  1543. end;
  1544. procedure TCGMIPS.g_adjust_self_value(list:TAsmList;procdef: tprocdef;ioffset: tcgint);
  1545. begin
  1546. { This method is integrated into g_intf_wrapper and shouldn't be called separately }
  1547. InternalError(2013020102);
  1548. end;
  1549. procedure TCGMIPS.g_stackpointer_alloc(list : TAsmList;localsize : longint);
  1550. begin
  1551. Comment(V_Error,'TCgMPSel.g_stackpointer_alloc method not implemented');
  1552. end;
  1553. procedure TCGMIPS.a_bit_scan_reg_reg(list: TAsmList; reverse: boolean; size: TCGSize; src, dst: TRegister);
  1554. begin
  1555. Comment(V_Error,'TCgMPSel.a_bit_scan_reg_reg method not implemented');
  1556. end;
  1557. {****************************************************************************
  1558. TCG64_MIPSel
  1559. ****************************************************************************}
  1560. procedure TCg64MPSel.a_load64_reg_ref(list: tasmlist; reg: tregister64; const ref: treference);
  1561. var
  1562. tmpref: treference;
  1563. tmpreg: tregister;
  1564. begin
  1565. { Override this function to prevent loading the reference twice }
  1566. if target_info.endian = endian_big then
  1567. begin
  1568. tmpreg := reg.reglo;
  1569. reg.reglo := reg.reghi;
  1570. reg.reghi := tmpreg;
  1571. end;
  1572. tmpref := ref;
  1573. cg.a_load_reg_ref(list, OS_S32, OS_S32, reg.reglo, tmpref);
  1574. Inc(tmpref.offset, 4);
  1575. cg.a_load_reg_ref(list, OS_S32, OS_S32, reg.reghi, tmpref);
  1576. end;
  1577. procedure TCg64MPSel.a_load64_ref_reg(list: tasmlist; const ref: treference; reg: tregister64);
  1578. var
  1579. tmpref: treference;
  1580. tmpreg: tregister;
  1581. begin
  1582. { Override this function to prevent loading the reference twice }
  1583. if target_info.endian = endian_big then
  1584. begin
  1585. tmpreg := reg.reglo;
  1586. reg.reglo := reg.reghi;
  1587. reg.reghi := tmpreg;
  1588. end;
  1589. tmpref := ref;
  1590. cg.a_load_ref_reg(list, OS_S32, OS_S32, tmpref, reg.reglo);
  1591. Inc(tmpref.offset, 4);
  1592. cg.a_load_ref_reg(list, OS_S32, OS_S32, tmpref, reg.reghi);
  1593. end;
  1594. procedure TCg64MPSel.a_load64_ref_cgpara(list: tasmlist; const r: treference; const paraloc: tcgpara);
  1595. var
  1596. hreg64: tregister64;
  1597. begin
  1598. { Override this function to prevent loading the reference twice.
  1599. Use here some extra registers, but those are optimized away by the RA }
  1600. hreg64.reglo := cg.GetIntRegister(list, OS_S32);
  1601. hreg64.reghi := cg.GetIntRegister(list, OS_S32);
  1602. a_load64_ref_reg(list, r, hreg64);
  1603. a_load64_reg_cgpara(list, hreg64, paraloc);
  1604. end;
  1605. procedure TCg64MPSel.a_op64_reg_reg(list: tasmlist; op: TOpCG; size: tcgsize; regsrc, regdst: TRegister64);
  1606. var
  1607. tmpreg1: TRegister;
  1608. begin
  1609. case op of
  1610. OP_NEG:
  1611. begin
  1612. tmpreg1 := cg.GetIntRegister(list, OS_INT);
  1613. list.concat(taicpu.op_reg_reg_reg(A_SUBU, regdst.reglo, NR_R0, regsrc.reglo));
  1614. list.concat(taicpu.op_reg_reg_reg(A_SLTU, tmpreg1, NR_R0, regdst.reglo));
  1615. list.concat(taicpu.op_reg_reg_reg(A_SUBU, regdst.reghi, NR_R0, regsrc.reghi));
  1616. list.concat(taicpu.op_reg_reg_reg(A_SUBU, regdst.reghi, regdst.reghi, tmpreg1));
  1617. end;
  1618. OP_NOT:
  1619. begin
  1620. list.concat(taicpu.op_reg_reg_reg(A_NOR, regdst.reglo, NR_R0, regsrc.reglo));
  1621. list.concat(taicpu.op_reg_reg_reg(A_NOR, regdst.reghi, NR_R0, regsrc.reghi));
  1622. end;
  1623. else
  1624. a_op64_reg_reg_reg(list,op,size,regsrc,regdst,regdst);
  1625. end;
  1626. end;
  1627. procedure TCg64MPSel.a_op64_const_reg(list: tasmlist; op: TOpCG; size: tcgsize; Value: int64; regdst: TRegister64);
  1628. begin
  1629. a_op64_const_reg_reg(list, op, size, value, regdst, regdst);
  1630. end;
  1631. procedure TCg64MPSel.a_op64_const_reg_reg(list: tasmlist; op: TOpCG; size: tcgsize; Value: int64; regsrc, regdst: tregister64);
  1632. var
  1633. l: tlocation;
  1634. begin
  1635. a_op64_const_reg_reg_checkoverflow(list, op, size, Value, regsrc, regdst, False, l);
  1636. end;
  1637. procedure TCg64MPSel.a_op64_reg_reg_reg(list: tasmlist; op: TOpCG; size: tcgsize; regsrc1, regsrc2, regdst: tregister64);
  1638. var
  1639. l: tlocation;
  1640. begin
  1641. a_op64_reg_reg_reg_checkoverflow(list, op, size, regsrc1, regsrc2, regdst, False, l);
  1642. end;
  1643. procedure TCg64MPSel.a_op64_const_reg_reg_checkoverflow(list: tasmlist; op: TOpCG; size: tcgsize; Value: int64; regsrc, regdst: tregister64; setflags: boolean; var ovloc: tlocation);
  1644. var
  1645. tmplo,carry: TRegister;
  1646. hisize: tcgsize;
  1647. begin
  1648. carry:=NR_NO;
  1649. if (size in [OS_S64]) then
  1650. hisize:=OS_S32
  1651. else
  1652. hisize:=OS_32;
  1653. case op of
  1654. OP_AND,OP_OR,OP_XOR:
  1655. begin
  1656. cg.a_op_const_reg_reg(list,op,OS_32,aint(lo(value)),regsrc.reglo,regdst.reglo);
  1657. cg.a_op_const_reg_reg(list,op,OS_32,aint(hi(value)),regsrc.reghi,regdst.reghi);
  1658. end;
  1659. OP_ADD:
  1660. begin
  1661. if lo(value)<>0 then
  1662. begin
  1663. tmplo:=cg.GetIntRegister(list,OS_32);
  1664. carry:=cg.GetIntRegister(list,OS_32);
  1665. tcgmips(cg).handle_reg_const_reg(list,A_ADDU,regsrc.reglo,aint(lo(value)),tmplo);
  1666. list.concat(taicpu.op_reg_reg_reg(A_SLTU,carry,tmplo,regsrc.reglo));
  1667. cg.a_load_reg_reg(list,OS_32,OS_32,tmplo,regdst.reglo);
  1668. end
  1669. else
  1670. cg.a_load_reg_reg(list,OS_32,OS_32,regsrc.reglo,regdst.reglo);
  1671. { With overflow checking and unsigned args, this generates slighly suboptimal code
  1672. ($80000000 constant loaded twice). Other cases are fine. Getting it perfect does not
  1673. look worth the effort. }
  1674. cg.a_op_const_reg_reg_checkoverflow(list,OP_ADD,hisize,aint(hi(value)),regsrc.reghi,regdst.reghi,setflags,ovloc);
  1675. if carry<>NR_NO then
  1676. cg.a_op_reg_reg_reg_checkoverflow(list,OP_ADD,hisize,carry,regdst.reghi,regdst.reghi,setflags,ovloc);
  1677. end;
  1678. OP_SUB:
  1679. begin
  1680. carry:=NR_NO;
  1681. if lo(value)<>0 then
  1682. begin
  1683. tmplo:=cg.GetIntRegister(list,OS_32);
  1684. carry:=cg.GetIntRegister(list,OS_32);
  1685. tcgmips(cg).handle_reg_const_reg(list,A_SUBU,regsrc.reglo,aint(lo(value)),tmplo);
  1686. list.concat(taicpu.op_reg_reg_reg(A_SLTU,carry,regsrc.reglo,tmplo));
  1687. cg.a_load_reg_reg(list,OS_32,OS_32,tmplo,regdst.reglo);
  1688. end
  1689. else
  1690. cg.a_load_reg_reg(list,OS_32,OS_32,regsrc.reglo,regdst.reglo);
  1691. cg.a_op_const_reg_reg_checkoverflow(list,OP_SUB,hisize,aint(hi(value)),regsrc.reghi,regdst.reghi,setflags,ovloc);
  1692. if carry<>NR_NO then
  1693. cg.a_op_reg_reg_reg_checkoverflow(list,OP_SUB,hisize,carry,regdst.reghi,regdst.reghi,setflags,ovloc);
  1694. end;
  1695. else
  1696. InternalError(2013050301);
  1697. end;
  1698. end;
  1699. procedure TCg64MPSel.a_op64_reg_reg_reg_checkoverflow(list: tasmlist; op: TOpCG; size: tcgsize; regsrc1, regsrc2, regdst: tregister64; setflags: boolean; var ovloc: tlocation);
  1700. var
  1701. tmplo,tmphi,carry,hreg: TRegister;
  1702. signed: boolean;
  1703. begin
  1704. case op of
  1705. OP_ADD:
  1706. begin
  1707. signed:=(size in [OS_S64]);
  1708. tmplo := cg.GetIntRegister(list,OS_S32);
  1709. carry := cg.GetIntRegister(list,OS_S32);
  1710. // destreg.reglo could be regsrc1.reglo or regsrc2.reglo
  1711. list.concat(taicpu.op_reg_reg_reg(A_ADDU, tmplo, regsrc2.reglo, regsrc1.reglo));
  1712. list.concat(taicpu.op_reg_reg_reg(A_SLTU, carry, tmplo, regsrc2.reglo));
  1713. cg.a_load_reg_reg(list,OS_INT,OS_INT,tmplo,regdst.reglo);
  1714. if signed or (not setflags) then
  1715. begin
  1716. list.concat(taicpu.op_reg_reg_reg(ops_add[setflags and signed], regdst.reghi, regsrc2.reghi, regsrc1.reghi));
  1717. list.concat(taicpu.op_reg_reg_reg(ops_add[setflags and signed], regdst.reghi, regdst.reghi, carry));
  1718. end
  1719. else
  1720. begin
  1721. tmphi:=cg.GetIntRegister(list,OS_INT);
  1722. hreg:=cg.GetIntRegister(list,OS_INT);
  1723. cg.a_load_const_reg(list,OS_INT,$80000000,hreg);
  1724. // first add carry to one of the addends
  1725. list.concat(taicpu.op_reg_reg_reg(A_ADDU, tmphi, regsrc2.reghi, carry));
  1726. list.concat(taicpu.op_reg_reg_reg(A_SLTU, carry, tmphi, regsrc2.reghi));
  1727. list.concat(taicpu.op_reg_reg_reg(A_SUB, carry, hreg, carry));
  1728. // then add another addend
  1729. list.concat(taicpu.op_reg_reg_reg(A_ADDU, regdst.reghi, tmphi, regsrc1.reghi));
  1730. list.concat(taicpu.op_reg_reg_reg(A_SLTU, carry, regdst.reghi, tmphi));
  1731. list.concat(taicpu.op_reg_reg_reg(A_SUB, carry, hreg, carry));
  1732. end;
  1733. end;
  1734. OP_SUB:
  1735. begin
  1736. signed:=(size in [OS_S64]);
  1737. tmplo := cg.GetIntRegister(list,OS_S32);
  1738. carry := cg.GetIntRegister(list,OS_S32);
  1739. // destreg.reglo could be regsrc1.reglo or regsrc2.reglo
  1740. list.concat(taicpu.op_reg_reg_reg(A_SUBU, tmplo, regsrc2.reglo, regsrc1.reglo));
  1741. list.concat(taicpu.op_reg_reg_reg(A_SLTU, carry, regsrc2.reglo,tmplo));
  1742. cg.a_load_reg_reg(list,OS_INT,OS_INT,tmplo,regdst.reglo);
  1743. if signed or (not setflags) then
  1744. begin
  1745. list.concat(taicpu.op_reg_reg_reg(ops_sub[setflags and signed], regdst.reghi, regsrc2.reghi, regsrc1.reghi));
  1746. list.concat(taicpu.op_reg_reg_reg(ops_sub[setflags and signed], regdst.reghi, regdst.reghi, carry));
  1747. end
  1748. else
  1749. begin
  1750. tmphi:=cg.GetIntRegister(list,OS_INT);
  1751. hreg:=cg.GetIntRegister(list,OS_INT);
  1752. cg.a_load_const_reg(list,OS_INT,$80000000,hreg);
  1753. // first subtract the carry...
  1754. list.concat(taicpu.op_reg_reg_reg(A_SUBU, tmphi, regsrc2.reghi, carry));
  1755. list.concat(taicpu.op_reg_reg_reg(A_SLTU, carry, regsrc2.reghi, tmphi));
  1756. list.concat(taicpu.op_reg_reg_reg(A_SUB, carry, hreg, carry));
  1757. // ...then the subtrahend
  1758. list.concat(taicpu.op_reg_reg_reg(A_SUBU, regdst.reghi, tmphi, regsrc1.reghi));
  1759. list.concat(taicpu.op_reg_reg_reg(A_SLTU, carry, tmphi, regdst.reghi));
  1760. list.concat(taicpu.op_reg_reg_reg(A_SUB, carry, hreg, carry));
  1761. end;
  1762. end;
  1763. OP_AND,OP_OR,OP_XOR:
  1764. begin
  1765. cg.a_op_reg_reg_reg(list,op,size,regsrc1.reglo,regsrc2.reglo,regdst.reglo);
  1766. cg.a_op_reg_reg_reg(list,op,size,regsrc1.reghi,regsrc2.reghi,regdst.reghi);
  1767. end;
  1768. else
  1769. internalerror(200306017);
  1770. end;
  1771. end;
  1772. procedure create_codegen;
  1773. begin
  1774. cg:=TCGMIPS.Create;
  1775. cg64:=TCg64MPSel.Create;
  1776. end;
  1777. end.