cgcpu.pas 70 KB

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