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