cgcpu.pas 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965
  1. {
  2. Copyright (c) 1998-2009 by Florian Klaempfl and David Zhang
  3. This unit implements the code generator for the MIPSEL
  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,
  23. aasmbase, aasmtai, aasmcpu, aasmdata,
  24. cpubase, cpuinfo,
  25. node, symconst, SymType, symdef,
  26. rgcpu;
  27. type
  28. TCgMPSel = 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 make_simple_ref_fpu(list: tasmlist; var ref: treference);
  36. procedure handle_load_store(list: tasmlist; isstore: boolean; op: tasmop; reg: tregister; ref: treference);
  37. procedure handle_load_store_fpu(list: tasmlist; isstore: boolean; op: tasmop; reg: tregister; ref: treference);
  38. procedure handle_reg_const_reg(list: tasmlist; op: Tasmop; src: tregister; a: aint; dst: tregister);
  39. { parameter }
  40. procedure a_param_const(list: tasmlist; size: tcgsize; a: aint; const paraloc: TCGPara); override;
  41. procedure a_param_ref(list: tasmlist; sz: tcgsize; const r: TReference; const paraloc: TCGPara); override;
  42. procedure a_paramaddr_ref(list: tasmlist; const r: TReference; const paraloc: TCGPara); override;
  43. procedure a_paramfpu_reg(list: tasmlist; size: tcgsize; const r: tregister; const paraloc: TCGPara); override;
  44. procedure a_paramfpu_ref(list: tasmlist; size: tcgsize; const ref: treference; const paraloc: TCGPara); override;
  45. procedure a_call_name(list: tasmlist; const s: string; weak : boolean); override;
  46. procedure a_call_reg(list: tasmlist; Reg: TRegister); override;
  47. { General purpose instructions }
  48. procedure a_op_const_reg(list: tasmlist; Op: TOpCG; size: tcgsize; a: aint; reg: TRegister); override;
  49. procedure a_op_reg_reg(list: tasmlist; Op: TOpCG; size: TCGSize; src, dst: TRegister); override;
  50. procedure a_op_const_reg_reg(list: tasmlist; op: TOpCg; size: tcgsize; a: aint; src, dst: tregister); override;
  51. procedure a_op_reg_reg_reg(list: tasmlist; op: TOpCg; size: tcgsize; src1, src2, dst: tregister); override;
  52. procedure a_op_const_reg_reg_checkoverflow(list: tasmlist; op: TOpCg; size: tcgsize; a: aint; src, dst: tregister; setflags: boolean; var ovloc: tlocation); override;
  53. procedure a_op_reg_reg_reg_checkoverflow(list: tasmlist; op: TOpCg; size: tcgsize; src1, src2, dst: tregister; setflags: boolean; var ovloc: tlocation); override;
  54. { move instructions }
  55. procedure a_load_const_reg(list: tasmlist; size: tcgsize; a: aint; reg: tregister); override;
  56. procedure a_load_const_ref(list: tasmlist; size: tcgsize; a: aint; const ref: TReference); override;
  57. procedure a_load_reg_ref(list: tasmlist; FromSize, ToSize: TCgSize; reg: TRegister; const ref: TReference); override;
  58. procedure a_load_ref_reg(list: tasmlist; FromSize, ToSize: TCgSize; const ref: TReference; reg: tregister); override;
  59. procedure a_load_reg_reg(list: tasmlist; FromSize, ToSize: TCgSize; reg1, reg2: tregister); override;
  60. procedure a_loadaddr_ref_reg(list: tasmlist; const ref: TReference; r: tregister); override;
  61. { fpu move instructions }
  62. procedure a_loadfpu_reg_reg(list: tasmlist; fromsize, tosize: tcgsize; reg1, reg2: tregister); override;
  63. procedure a_loadfpu_ref_reg(list: tasmlist; fromsize, tosize: tcgsize; const ref: TReference; reg: tregister); override;
  64. procedure a_loadfpu_reg_ref(list: tasmlist; fromsize, tosize: tcgsize; reg: tregister; const ref: TReference); override;
  65. { comparison operations }
  66. procedure a_cmp_const_reg_label(list: tasmlist; size: tcgsize; cmp_op: topcmp; a: aint; reg: tregister; l: tasmlabel); override;
  67. procedure a_cmp_reg_reg_label(list: tasmlist; size: tcgsize; cmp_op: topcmp; reg1, reg2: tregister; l: tasmlabel); override;
  68. procedure a_jmp_always(List: tasmlist; l: TAsmLabel); override;
  69. procedure a_jmp_name(list: tasmlist; const s: string); override;
  70. procedure a_jmp_cond(list: tasmlist; cond: TOpCmp; l: tasmlabel); { override;}
  71. procedure g_overflowCheck(List: tasmlist; const Loc: TLocation; def: TDef); override;
  72. procedure g_overflowCheck_loc(List: tasmlist; const Loc: TLocation; def: TDef; ovloc: tlocation); override;
  73. procedure g_proc_entry(list: tasmlist; localsize: longint; nostackframe: boolean); override;
  74. procedure g_proc_exit(list: tasmlist; parasize: longint; nostackframe: boolean); override;
  75. procedure g_concatcopy(list: tasmlist; const Source, dest: treference; len: aint); override;
  76. procedure g_concatcopy_unaligned(list: tasmlist; const Source, dest: treference; len: aint); override;
  77. procedure g_concatcopy_move(list: tasmlist; const Source, dest: treference; len: aint);
  78. procedure g_intf_wrapper(list: tasmlist; procdef: tprocdef; const labelname: string; ioffset: longint); override;
  79. end;
  80. TCg64MPSel = class(tcg64f32)
  81. public
  82. procedure a_load64_reg_ref(list: tasmlist; reg: tregister64; const ref: treference); override;
  83. procedure a_load64_ref_reg(list: tasmlist; const ref: treference; reg: tregister64); override;
  84. procedure a_param64_ref(list: tasmlist; const r: treference; const paraloc: tcgpara); override;
  85. procedure a_op64_reg_reg(list: tasmlist; op: TOpCG; size: tcgsize; regsrc, regdst: TRegister64); override;
  86. procedure a_op64_const_reg(list: tasmlist; op: TOpCG; size: tcgsize; Value: int64; regdst: TRegister64); override;
  87. procedure a_op64_const_reg_reg(list: tasmlist; op: TOpCG; size: tcgsize; Value: int64; regsrc, regdst: tregister64); override;
  88. procedure a_op64_reg_reg_reg(list: tasmlist; op: TOpCG; size: tcgsize; regsrc1, regsrc2, regdst: tregister64); override;
  89. procedure a_op64_const_reg_reg_checkoverflow(list: tasmlist; op: TOpCG; size: tcgsize; Value: int64; regsrc, regdst: tregister64; setflags: boolean; var ovloc: tlocation); override;
  90. procedure a_op64_reg_reg_reg_checkoverflow(list: tasmlist; op: TOpCG; size: tcgsize; regsrc1, regsrc2, regdst: tregister64; setflags: boolean; var ovloc: tlocation); override;
  91. end;
  92. procedure create_codegen;
  93. implementation
  94. uses
  95. globals, verbose, systems, cutils,
  96. paramgr, fmodule,
  97. tgobj,
  98. procinfo, cpupi;
  99. var
  100. cgcpu_calc_stackframe_size: aint;
  101. function f_TOpCG2AsmOp(op: TOpCG; size: tcgsize): TAsmOp;
  102. begin
  103. if size = OS_32 then
  104. case op of
  105. OP_ADD: { simple addition }
  106. f_TOpCG2AsmOp := A_ADDU;
  107. OP_AND: { simple logical and }
  108. f_TOpCG2AsmOp := A_AND;
  109. OP_DIV: { simple unsigned division }
  110. f_TOpCG2AsmOp := A_DIVU;
  111. OP_IDIV: { simple signed division }
  112. f_TOpCG2AsmOp := A_DIV;
  113. OP_IMUL: { simple signed multiply }
  114. f_TOpCG2AsmOp := A_MULT;
  115. OP_MUL: { simple unsigned multiply }
  116. f_TOpCG2AsmOp := A_MULTU;
  117. OP_NEG: { simple negate }
  118. f_TOpCG2AsmOp := A_NEGU;
  119. OP_NOT: { simple logical not }
  120. f_TOpCG2AsmOp := A_NOT;
  121. OP_OR: { simple logical or }
  122. f_TOpCG2AsmOp := A_OR;
  123. OP_SAR: { arithmetic shift-right }
  124. f_TOpCG2AsmOp := A_SRA;
  125. OP_SHL: { logical shift left }
  126. f_TOpCG2AsmOp := A_SLL;
  127. OP_SHR: { logical shift right }
  128. f_TOpCG2AsmOp := A_SRL;
  129. OP_SUB: { simple subtraction }
  130. f_TOpCG2AsmOp := A_SUBU;
  131. OP_XOR: { simple exclusive or }
  132. f_TOpCG2AsmOp := A_XOR;
  133. else
  134. InternalError(2007070401);
  135. end{ case }
  136. else
  137. case op of
  138. OP_ADD: { simple addition }
  139. f_TOpCG2AsmOp := A_ADDU;
  140. OP_AND: { simple logical and }
  141. f_TOpCG2AsmOp := A_AND;
  142. OP_DIV: { simple unsigned division }
  143. f_TOpCG2AsmOp := A_DIVU;
  144. OP_IDIV: { simple signed division }
  145. f_TOpCG2AsmOp := A_DIV;
  146. OP_IMUL: { simple signed multiply }
  147. f_TOpCG2AsmOp := A_MULT;
  148. OP_MUL: { simple unsigned multiply }
  149. f_TOpCG2AsmOp := A_MULTU;
  150. OP_NEG: { simple negate }
  151. f_TOpCG2AsmOp := A_NEGU;
  152. OP_NOT: { simple logical not }
  153. f_TOpCG2AsmOp := A_NOT;
  154. OP_OR: { simple logical or }
  155. f_TOpCG2AsmOp := A_OR;
  156. OP_SAR: { arithmetic shift-right }
  157. f_TOpCG2AsmOp := A_SRA;
  158. OP_SHL: { logical shift left }
  159. f_TOpCG2AsmOp := A_SLL;
  160. OP_SHR: { logical shift right }
  161. f_TOpCG2AsmOp := A_SRL;
  162. OP_SUB: { simple subtraction }
  163. f_TOpCG2AsmOp := A_SUBU;
  164. OP_XOR: { simple exclusive or }
  165. f_TOpCG2AsmOp := A_XOR;
  166. else
  167. InternalError(2007010701);
  168. end;{ case }
  169. end;
  170. function f_TOpCG2AsmOp_ovf(op: TOpCG; size: tcgsize): TAsmOp;
  171. begin
  172. if size = OS_32 then
  173. case op of
  174. OP_ADD: { simple addition }
  175. f_TOpCG2AsmOp_ovf := A_ADD;
  176. OP_AND: { simple logical and }
  177. f_TOpCG2AsmOp_ovf := A_AND;
  178. OP_DIV: { simple unsigned division }
  179. f_TOpCG2AsmOp_ovf := A_DIVU;
  180. OP_IDIV: { simple signed division }
  181. f_TOpCG2AsmOp_ovf := A_DIV;
  182. OP_IMUL: { simple signed multiply }
  183. f_TOpCG2AsmOp_ovf := A_MULO;
  184. OP_MUL: { simple unsigned multiply }
  185. f_TOpCG2AsmOp_ovf := A_MULOU;
  186. OP_NEG: { simple negate }
  187. f_TOpCG2AsmOp_ovf := A_NEG;
  188. OP_NOT: { simple logical not }
  189. f_TOpCG2AsmOp_ovf := A_NOT;
  190. OP_OR: { simple logical or }
  191. f_TOpCG2AsmOp_ovf := A_OR;
  192. OP_SAR: { arithmetic shift-right }
  193. f_TOpCG2AsmOp_ovf := A_SRA;
  194. OP_SHL: { logical shift left }
  195. f_TOpCG2AsmOp_ovf := A_SLL;
  196. OP_SHR: { logical shift right }
  197. f_TOpCG2AsmOp_ovf := A_SRL;
  198. OP_SUB: { simple subtraction }
  199. f_TOpCG2AsmOp_ovf := A_SUB;
  200. OP_XOR: { simple exclusive or }
  201. f_TOpCG2AsmOp_ovf := A_XOR;
  202. else
  203. InternalError(2007070403);
  204. end{ case }
  205. else
  206. case op of
  207. OP_ADD: { simple addition }
  208. f_TOpCG2AsmOp_ovf := A_ADD;
  209. OP_AND: { simple logical and }
  210. f_TOpCG2AsmOp_ovf := A_AND;
  211. OP_DIV: { simple unsigned division }
  212. f_TOpCG2AsmOp_ovf := A_DIVU;
  213. OP_IDIV: { simple signed division }
  214. f_TOpCG2AsmOp_ovf := A_DIV;
  215. OP_IMUL: { simple signed multiply }
  216. f_TOpCG2AsmOp_ovf := A_MULO;
  217. OP_MUL: { simple unsigned multiply }
  218. f_TOpCG2AsmOp_ovf := A_MULOU;
  219. OP_NEG: { simple negate }
  220. f_TOpCG2AsmOp_ovf := A_NEG;
  221. OP_NOT: { simple logical not }
  222. f_TOpCG2AsmOp_ovf := A_NOT;
  223. OP_OR: { simple logical or }
  224. f_TOpCG2AsmOp_ovf := A_OR;
  225. OP_SAR: { arithmetic shift-right }
  226. f_TOpCG2AsmOp_ovf := A_SRA;
  227. OP_SHL: { logical shift left }
  228. f_TOpCG2AsmOp_ovf := A_SLL;
  229. OP_SHR: { logical shift right }
  230. f_TOpCG2AsmOp_ovf := A_SRL;
  231. OP_SUB: { simple subtraction }
  232. f_TOpCG2AsmOp_ovf := A_SUB;
  233. OP_XOR: { simple exclusive or }
  234. f_TOpCG2AsmOp_ovf := A_XOR;
  235. else
  236. InternalError(2007010703);
  237. end;{ case }
  238. end;
  239. function f_TOp64CG2AsmOp(op: TOpCG): TAsmOp;
  240. begin
  241. case op of
  242. OP_ADD: { simple addition }
  243. f_TOp64CG2AsmOp := A_DADDU;
  244. OP_AND: { simple logical and }
  245. f_TOp64CG2AsmOp := A_AND;
  246. OP_DIV: { simple unsigned division }
  247. f_TOp64CG2AsmOp := A_DDIVU;
  248. OP_IDIV: { simple signed division }
  249. f_TOp64CG2AsmOp := A_DDIV;
  250. OP_IMUL: { simple signed multiply }
  251. f_TOp64CG2AsmOp := A_DMULO;
  252. OP_MUL: { simple unsigned multiply }
  253. f_TOp64CG2AsmOp := A_DMULOU;
  254. OP_NEG: { simple negate }
  255. f_TOp64CG2AsmOp := A_DNEGU;
  256. OP_NOT: { simple logical not }
  257. f_TOp64CG2AsmOp := A_NOT;
  258. OP_OR: { simple logical or }
  259. f_TOp64CG2AsmOp := A_OR;
  260. OP_SAR: { arithmetic shift-right }
  261. f_TOp64CG2AsmOp := A_DSRA;
  262. OP_SHL: { logical shift left }
  263. f_TOp64CG2AsmOp := A_DSLL;
  264. OP_SHR: { logical shift right }
  265. f_TOp64CG2AsmOp := A_DSRL;
  266. OP_SUB: { simple subtraction }
  267. f_TOp64CG2AsmOp := A_DSUBU;
  268. OP_XOR: { simple exclusive or }
  269. f_TOp64CG2AsmOp := A_XOR;
  270. else
  271. InternalError(2007010702);
  272. end;{ case }
  273. end;
  274. procedure TCgMPSel.make_simple_ref(list: tasmlist; var ref: treference);
  275. var
  276. tmpreg, tmpreg1: tregister;
  277. tmpref: treference;
  278. begin
  279. tmpreg := NR_NO;
  280. { Be sure to have a base register }
  281. if (ref.base = NR_NO) then
  282. begin
  283. ref.base := ref.index;
  284. ref.index := NR_NO;
  285. end;
  286. if (cs_create_pic in current_settings.moduleswitches) and
  287. assigned(ref.symbol) then
  288. begin
  289. tmpreg := GetIntRegister(list, OS_INT);
  290. reference_reset(tmpref,sizeof(aint));
  291. tmpref.symbol := ref.symbol;
  292. tmpref.refaddr := addr_pic;
  293. if not (pi_needs_got in current_procinfo.flags) then
  294. internalerror(200501161);
  295. tmpref.index := current_procinfo.got;
  296. list.concat(taicpu.op_reg_ref(A_LW, tmpreg, tmpref));
  297. ref.symbol := nil;
  298. if (ref.index <> NR_NO) then
  299. begin
  300. list.concat(taicpu.op_reg_reg_reg(A_ADDU, tmpreg, ref.index, tmpreg));
  301. ref.index := tmpreg;
  302. end
  303. else
  304. begin
  305. if ref.base <> NR_NO then
  306. ref.index := tmpreg
  307. else
  308. ref.base := tmpreg;
  309. end;
  310. end;
  311. { When need to use LUI, do it first }
  312. if assigned(ref.symbol) or
  313. (ref.offset < simm16lo) or
  314. (ref.offset > simm16hi) then
  315. begin
  316. tmpreg := GetIntRegister(list, OS_INT);
  317. reference_reset(tmpref,sizeof(aint));
  318. tmpref.symbol := ref.symbol;
  319. tmpref.offset := ref.offset;
  320. tmpref.refaddr := addr_high;
  321. list.concat(taicpu.op_reg_ref(A_LUI, tmpreg, tmpref));
  322. if (ref.offset = 0) and (ref.index = NR_NO) and
  323. (ref.base = NR_NO) then
  324. begin
  325. ref.refaddr := addr_low;
  326. end
  327. else
  328. begin
  329. { Load the low part is left }
  330. tmpref.refaddr := addr_low;
  331. list.concat(taicpu.op_reg_reg_ref(A_ADDIU, tmpreg, tmpreg, tmpref));
  332. ref.offset := 0;
  333. { symbol is loaded }
  334. ref.symbol := nil;
  335. end;
  336. if (ref.index <> NR_NO) then
  337. begin
  338. list.concat(taicpu.op_reg_reg_reg(A_ADDU, tmpreg, ref.index, tmpreg));
  339. ref.index := tmpreg;
  340. end
  341. else
  342. begin
  343. if ref.base <> NR_NO then
  344. ref.index := tmpreg
  345. else
  346. ref.base := tmpreg;
  347. end;
  348. end;
  349. if (ref.base <> NR_NO) then
  350. begin
  351. if (ref.index <> NR_NO) and (ref.offset = 0) then
  352. begin
  353. tmpreg1 := GetIntRegister(list, OS_INT);
  354. list.concat(taicpu.op_reg_reg_reg(A_ADDU, tmpreg1, ref.base, ref.index));
  355. ref.base := tmpreg1;
  356. ref.index := NR_NO;
  357. end
  358. else if (ref.index <> NR_NO) and
  359. ((ref.offset <> 0) or assigned(ref.symbol)) then
  360. begin
  361. if tmpreg = NR_NO then
  362. tmpreg := GetIntRegister(list, OS_INT);
  363. list.concat(taicpu.op_reg_reg_reg(A_ADDU, tmpreg, ref.base, ref.index));
  364. ref.base := tmpreg;
  365. ref.index := NR_NO;
  366. end;
  367. end;
  368. end;
  369. procedure TCgMPSel.make_simple_ref_fpu(list: tasmlist; var ref: treference);
  370. var
  371. tmpreg, tmpreg1: tregister;
  372. tmpref: treference;
  373. begin
  374. tmpreg := NR_NO;
  375. { Be sure to have a base register }
  376. if (ref.base = NR_NO) then
  377. begin
  378. ref.base := ref.index;
  379. ref.index := NR_NO;
  380. end;
  381. if (cs_create_pic in current_settings.moduleswitches) and
  382. assigned(ref.symbol) then
  383. begin
  384. tmpreg := GetIntRegister(list, OS_INT);
  385. reference_reset(tmpref,sizeof(aint));
  386. tmpref.symbol := ref.symbol;
  387. tmpref.refaddr := addr_pic;
  388. if not (pi_needs_got in current_procinfo.flags) then
  389. internalerror(200501161);
  390. tmpref.index := current_procinfo.got;
  391. list.concat(taicpu.op_reg_ref(A_LW, tmpreg, tmpref));
  392. ref.symbol := nil;
  393. if (ref.index <> NR_NO) then
  394. begin
  395. list.concat(taicpu.op_reg_reg_reg(A_ADDU, tmpreg, ref.index, tmpreg));
  396. ref.index := tmpreg;
  397. end
  398. else
  399. begin
  400. if ref.base <> NR_NO then
  401. ref.index := tmpreg
  402. else
  403. ref.base := tmpreg;
  404. end;
  405. end;
  406. { When need to use LUI, do it first }
  407. if (not assigned(ref.symbol)) and (ref.index = NR_NO) and
  408. (ref.offset > simm16lo + 1000) and (ref.offset < simm16hi - 1000)
  409. then
  410. exit;
  411. tmpreg1 := GetIntRegister(list, OS_INT);
  412. if assigned(ref.symbol) then
  413. begin
  414. reference_reset(tmpref,sizeof(aint));
  415. tmpref.symbol := ref.symbol;
  416. tmpref.offset := ref.offset;
  417. tmpref.refaddr := addr_high;
  418. list.concat(taicpu.op_reg_ref(A_LUI, tmpreg1, tmpref));
  419. { Load the low part }
  420. tmpref.refaddr := addr_low;
  421. list.concat(taicpu.op_reg_reg_ref(A_ADDIU, tmpreg1, tmpreg1, tmpref));
  422. { symbol is loaded }
  423. ref.symbol := nil;
  424. end
  425. else
  426. list.concat(taicpu.op_reg_const(A_LI, tmpreg1, ref.offset));
  427. if (ref.index <> NR_NO) then
  428. begin
  429. list.concat(taicpu.op_reg_reg_reg(A_ADDU, tmpreg1, ref.index, tmpreg1));
  430. ref.index := NR_NO
  431. end;
  432. if ref.base <> NR_NO then
  433. list.concat(taicpu.op_reg_reg_reg(A_ADDU, tmpreg1, ref.base, tmpreg1));
  434. ref.base := tmpreg1;
  435. ref.offset := 0;
  436. end;
  437. procedure TCgMPSel.handle_load_store(list: tasmlist; isstore: boolean; op: tasmop; reg: tregister; ref: treference);
  438. begin
  439. make_simple_ref(list, ref);
  440. list.concat(taicpu.op_reg_ref(op, reg, ref));
  441. end;
  442. procedure TCgMPSel.handle_load_store_fpu(list: tasmlist; isstore: boolean; op: tasmop; reg: tregister; ref: treference);
  443. begin
  444. make_simple_ref_fpu(list, ref);
  445. list.concat(taicpu.op_reg_ref(op, reg, ref));
  446. end;
  447. procedure TCgMPSel.handle_reg_const_reg(list: tasmlist; op: Tasmop; src: tregister; a: aint; dst: tregister);
  448. var
  449. tmpreg: tregister;
  450. begin
  451. if (a < simm16lo) or
  452. (a > simm16hi) then
  453. begin
  454. tmpreg := GetIntRegister(list, OS_INT);
  455. a_load_const_reg(list, OS_INT, a, tmpreg);
  456. list.concat(taicpu.op_reg_reg_reg(op, dst, src, tmpreg));
  457. end
  458. else
  459. list.concat(taicpu.op_reg_reg_const(op, dst, src, a));
  460. end;
  461. {****************************************************************************
  462. Assembler code
  463. ****************************************************************************}
  464. procedure TCgMPSel.init_register_allocators;
  465. begin
  466. inherited init_register_allocators;
  467. if (cs_create_pic in current_settings.moduleswitches) and
  468. (pi_needs_got in current_procinfo.flags) then
  469. begin
  470. current_procinfo.got := NR_GP;
  471. rg[R_INTREGISTER] := Trgcpu.Create(R_INTREGISTER, R_SUBD,
  472. [RS_R4, RS_R5, RS_R6, RS_R7, RS_R8, RS_R9, RS_R10, RS_R11,
  473. RS_R12, RS_R13, RS_R14 {, RS_R15 for tmp_const in ncpuadd.pas} {, RS_R24, RS_R25}],
  474. first_int_imreg, []);
  475. end
  476. else
  477. rg[R_INTREGISTER] := Trgcpu.Create(R_INTREGISTER, R_SUBD,
  478. [RS_R4, RS_R5, RS_R6, RS_R7, RS_R8, RS_R9, RS_R10, RS_R11,
  479. RS_R12, RS_R13, RS_R14 {, RS_R15 for tmp_const in ncpuadd.pas} {, RS_R24=VMT, RS_R25=PIC jump}],
  480. first_int_imreg, []);
  481. rg[R_FPUREGISTER] := trgcpu.Create(R_FPUREGISTER, R_SUBFS{R_SUBFD},
  482. [RS_F0, RS_F2, RS_F4, RS_F6,
  483. RS_F8, RS_F10, RS_F12, RS_F14,
  484. RS_F16, RS_F18, RS_F20, RS_F22,
  485. RS_F24, RS_F26, RS_F28, RS_F30],
  486. first_fpu_imreg, []);
  487. end;
  488. procedure TCgMPSel.done_register_allocators;
  489. begin
  490. rg[R_INTREGISTER].Free;
  491. rg[R_FPUREGISTER].Free;
  492. inherited done_register_allocators;
  493. end;
  494. function TCgMPSel.getfpuregister(list: tasmlist; size: Tcgsize): Tregister;
  495. begin
  496. if size = OS_F64 then
  497. Result := rg[R_FPUREGISTER].getregister(list, R_SUBFD)
  498. else
  499. Result := rg[R_FPUREGISTER].getregister(list, R_SUBFS);
  500. end;
  501. procedure TCgMPSel.a_param_const(list: tasmlist; size: tcgsize; a: aint; const paraloc: TCGPara);
  502. var
  503. Ref: TReference;
  504. begin
  505. paraloc.check_simple_location;
  506. case paraloc.location^.loc of
  507. LOC_REGISTER, LOC_CREGISTER:
  508. a_load_const_reg(list, size, a, paraloc.location^.Register);
  509. LOC_REFERENCE:
  510. begin
  511. with paraloc.location^.Reference do
  512. begin
  513. if (Index = NR_SP) and (Offset < Target_info.first_parm_offset) then
  514. InternalError(2002081104);
  515. reference_reset_base(ref, index, offset, sizeof(aint));
  516. end;
  517. a_load_const_ref(list, size, a, ref);
  518. end;
  519. else
  520. InternalError(2002122200);
  521. end;
  522. end;
  523. procedure TCgMPSel.a_param_ref(list: tasmlist; sz: TCgSize; const r: TReference; const paraloc: TCGPara);
  524. var
  525. ref: treference;
  526. tmpreg: TRegister;
  527. begin
  528. paraloc.check_simple_location;
  529. with paraloc.location^ do
  530. begin
  531. case loc of
  532. LOC_REGISTER, LOC_CREGISTER:
  533. a_load_ref_reg(list, sz, sz, r, Register);
  534. LOC_REFERENCE:
  535. begin
  536. with Reference do
  537. begin
  538. if (Index = NR_SP) and (Offset < Target_info.first_parm_offset) then
  539. InternalError(2002081104);
  540. reference_reset_base(ref, index, offset, sizeof(aint));
  541. end;
  542. tmpreg := GetIntRegister(list, OS_INT);
  543. a_load_ref_reg(list, sz, sz, r, tmpreg);
  544. a_load_reg_ref(list, sz, sz, tmpreg, ref);
  545. end;
  546. else
  547. internalerror(2002081103);
  548. end;
  549. end;
  550. end;
  551. procedure TCgMPSel.a_paramaddr_ref(list: tasmlist; const r: TReference; const paraloc: TCGPara);
  552. var
  553. Ref: TReference;
  554. TmpReg: TRegister;
  555. begin
  556. paraloc.check_simple_location;
  557. with paraloc.location^ do
  558. begin
  559. case loc of
  560. LOC_REGISTER, LOC_CREGISTER:
  561. a_loadaddr_ref_reg(list, r, Register);
  562. LOC_REFERENCE:
  563. begin
  564. reference_reset(ref,sizeof(aint));
  565. ref.base := reference.index;
  566. ref.offset := reference.offset;
  567. tmpreg := GetAddressRegister(list);
  568. a_loadaddr_ref_reg(list, r, tmpreg);
  569. a_load_reg_ref(list, OS_ADDR, OS_ADDR, tmpreg, ref);
  570. end;
  571. else
  572. internalerror(2002080701);
  573. end;
  574. end;
  575. end;
  576. procedure TCgMPSel.a_paramfpu_ref(list: tasmlist; size: tcgsize; const ref: treference; const paraloc: TCGPara);
  577. var
  578. href, href2: treference;
  579. hloc: pcgparalocation;
  580. begin
  581. href := ref;
  582. hloc := paraloc.location;
  583. while assigned(hloc) do
  584. begin
  585. case hloc^.loc of
  586. LOC_REGISTER:
  587. a_load_ref_reg(list, hloc^.size, hloc^.size, href, hloc^.Register);
  588. LOC_REFERENCE:
  589. begin
  590. reference_reset_base(href2, hloc^.reference.index, hloc^.reference.offset, sizeof(aint));
  591. a_load_ref_ref(list, hloc^.size, hloc^.size, href, href2);
  592. end;
  593. else
  594. internalerror(200408241);
  595. end;
  596. Inc(href.offset, tcgsize2size[hloc^.size]);
  597. hloc := hloc^.Next;
  598. end;
  599. end;
  600. procedure TCgMPSel.a_paramfpu_reg(list: tasmlist; size: tcgsize; const r: tregister; const paraloc: TCGPara);
  601. var
  602. href: treference;
  603. begin
  604. tg.GetTemp(list, TCGSize2Size[size], sizeof(aint), tt_normal, href);
  605. a_loadfpu_reg_ref(list, size, size, r, href);
  606. a_paramfpu_ref(list, size, href, paraloc);
  607. tg.Ungettemp(list, href);
  608. end;
  609. procedure TCgMPSel.a_call_name(list: tasmlist; const s: string; weak: boolean);
  610. begin
  611. list.concat(taicpu.op_sym(A_JAL,current_asmdata.RefAsmSymbol(s)));
  612. { Delay slot }
  613. list.concat(taicpu.op_none(A_NOP));
  614. end;
  615. procedure TCgMPSel.a_call_reg(list: tasmlist; Reg: TRegister);
  616. begin
  617. list.concat(taicpu.op_reg(A_JALR, reg));
  618. { Delay slot }
  619. list.concat(taicpu.op_none(A_NOP));
  620. end;
  621. {********************** load instructions ********************}
  622. procedure TCgMPSel.a_load_const_reg(list: tasmlist; size: TCGSize; a: aint; reg: TRegister);
  623. begin
  624. if (a = 0) then
  625. list.concat(taicpu.op_reg_reg(A_MOVE, reg, NR_R0))
  626. { LUI allows to set the upper 16 bits, so we'll take full advantage of it }
  627. else if (a and aint($ffff)) = 0 then
  628. list.concat(taicpu.op_reg_const(A_LUI, reg, a shr 16))
  629. else if (a >= simm16lo) and (a <= simm16hi) then
  630. list.concat(taicpu.op_reg_reg_const(A_ADDIU, reg, NR_R0, a))
  631. else if (a>=0) and (a <= 65535) then
  632. list.concat(taicpu.op_reg_reg_const(A_ORI, reg, NR_R0, a))
  633. else
  634. begin
  635. list.concat(taicpu.op_reg_const(A_LI, reg, a ));
  636. end;
  637. end;
  638. procedure TCgMPSel.a_load_const_ref(list: tasmlist; size: tcgsize; a: aint; const ref: TReference);
  639. begin
  640. if a = 0 then
  641. a_load_reg_ref(list, size, size, NR_R0, ref)
  642. else
  643. inherited a_load_const_ref(list, size, a, ref);
  644. end;
  645. procedure TCgMPSel.a_load_reg_ref(list: tasmlist; FromSize, ToSize: TCGSize; reg: tregister; const Ref: TReference);
  646. var
  647. op: tasmop;
  648. begin
  649. if (TCGSize2Size[fromsize] >= TCGSize2Size[tosize]) then
  650. fromsize := tosize;
  651. case fromsize of
  652. { signed integer registers }
  653. OS_8,
  654. OS_S8:
  655. Op := A_SB;
  656. OS_16,
  657. OS_S16:
  658. Op := A_SH;
  659. OS_32,
  660. OS_S32:
  661. Op := A_SW;
  662. else
  663. InternalError(2002122100);
  664. end;
  665. handle_load_store(list, True, op, reg, ref);
  666. end;
  667. procedure TCgMPSel.a_load_ref_reg(list: tasmlist; FromSize, ToSize: TCgSize; const ref: TReference; reg: tregister);
  668. var
  669. op: tasmop;
  670. begin
  671. if (TCGSize2Size[fromsize] >= TCGSize2Size[tosize]) then
  672. fromsize := tosize;
  673. case fromsize of
  674. OS_S8:
  675. Op := A_LB;{Load Signed Byte}
  676. OS_8:
  677. Op := A_LBU;{Load Unsigned Byte}
  678. OS_S16:
  679. Op := A_LH;{Load Signed Halfword}
  680. OS_16:
  681. Op := A_LHU;{Load Unsigned Halfword}
  682. OS_S32:
  683. Op := A_LW;{Load Word}
  684. OS_32:
  685. Op := A_LW;//A_LWU;{Load Unsigned Word}
  686. OS_S64,
  687. OS_64:
  688. Op := A_LD;{Load a Long Word}
  689. else
  690. InternalError(2002122101);
  691. end;
  692. handle_load_store(list, False, op, reg, ref);
  693. end;
  694. procedure TCgMPSel.a_load_reg_reg(list: tasmlist; fromsize, tosize: tcgsize; reg1, reg2: tregister);
  695. var
  696. instr: taicpu;
  697. begin
  698. if (tcgsize2size[tosize] < tcgsize2size[fromsize]) or
  699. (
  700. (tcgsize2size[tosize] = tcgsize2size[fromsize]) and
  701. (tosize <> fromsize) and not (fromsize in [OS_32, OS_S32])
  702. ) then
  703. begin
  704. case tosize of
  705. OS_8:
  706. a_op_const_reg_reg(list, OP_AND, tosize, $ff, reg1, reg2);
  707. OS_16:
  708. a_op_const_reg_reg(list, OP_AND, tosize, $ffff, reg1, reg2);
  709. OS_32,
  710. OS_S32:
  711. begin
  712. instr := taicpu.op_reg_reg(A_MOVE, reg2, reg1);
  713. list.Concat(instr);
  714. { Notify the register allocator that we have written a move instruction so
  715. it can try to eliminate it. }
  716. add_move_instruction(instr);
  717. end;
  718. OS_S8:
  719. begin
  720. list.concat(taicpu.op_reg_reg_const(A_SLL, reg2, reg1, 24));
  721. list.concat(taicpu.op_reg_reg_const(A_SRA, reg2, reg2, 24));
  722. end;
  723. OS_S16:
  724. begin
  725. list.concat(taicpu.op_reg_reg_const(A_SLL, reg2, reg1, 16));
  726. list.concat(taicpu.op_reg_reg_const(A_SRA, reg2, reg2, 16));
  727. end;
  728. else
  729. internalerror(2002090901);
  730. end;
  731. end
  732. else
  733. begin
  734. if reg1 <> reg2 then
  735. begin
  736. { same size, only a register mov required }
  737. instr := taicpu.op_reg_reg(A_MOVE, 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. add_move_instruction(instr);
  742. end;
  743. end;
  744. end;
  745. procedure TCgMPSel.a_loadaddr_ref_reg(list: tasmlist; const ref: TReference; r: tregister);
  746. var
  747. tmpref, href: treference;
  748. hreg, tmpreg: tregister;
  749. r_used: boolean;
  750. begin
  751. r_used := false;
  752. href := ref;
  753. if (href.base = NR_NO) and (href.index <> NR_NO) then
  754. internalerror(200306171);
  755. if (cs_create_pic in current_settings.moduleswitches) and
  756. assigned(href.symbol) then
  757. begin
  758. tmpreg := r; //GetIntRegister(list, OS_ADDR);
  759. r_used := true;
  760. reference_reset(tmpref,sizeof(aint));
  761. tmpref.symbol := href.symbol;
  762. tmpref.refaddr := addr_pic;
  763. if not (pi_needs_got in current_procinfo.flags) then
  764. internalerror(200501161);
  765. tmpref.base := current_procinfo.got;
  766. list.concat(taicpu.op_reg_ref(A_LW, tmpreg, tmpref));
  767. href.symbol := nil;
  768. if (href.index <> NR_NO) then
  769. begin
  770. list.concat(taicpu.op_reg_reg_reg(A_ADDU, tmpreg, href.index, tmpreg));
  771. href.index := tmpreg;
  772. end
  773. else
  774. begin
  775. if href.base <> NR_NO then
  776. href.index := tmpreg
  777. else
  778. href.base := tmpreg;
  779. end;
  780. end;
  781. if assigned(href.symbol) or
  782. (href.offset < simm16lo) or
  783. (href.offset > simm16hi) then
  784. begin
  785. if (href.base = NR_NO) and (href.index = NR_NO) then
  786. hreg := r
  787. else
  788. hreg := GetAddressRegister(list);
  789. reference_reset(tmpref,sizeof(aint));
  790. tmpref.symbol := href.symbol;
  791. tmpref.offset := href.offset;
  792. tmpref.refaddr := addr_high;
  793. list.concat(taicpu.op_reg_ref(A_LUI, hreg, tmpref));
  794. { Only the low part is left }
  795. tmpref.refaddr := addr_low;
  796. list.concat(taicpu.op_reg_reg_ref(A_ADDIU, hreg, hreg, tmpref));
  797. if href.base <> NR_NO then
  798. begin
  799. if href.index <> NR_NO then
  800. begin
  801. list.concat(taicpu.op_reg_reg_reg(A_ADDU, hreg, href.base, hreg));
  802. list.concat(taicpu.op_reg_reg_reg(A_ADDU, r, hreg, href.index));
  803. end
  804. else
  805. list.concat(taicpu.op_reg_reg_reg(A_ADDU, r, hreg, href.base));
  806. end;
  807. end
  808. else
  809. { At least small offset, maybe base and maybe index }
  810. if (href.offset >= simm16lo) and
  811. (href.offset <= simm16hi) then
  812. begin
  813. if href.index <> NR_NO then { Both base and index }
  814. begin
  815. if href.offset = 0 then
  816. begin
  817. list.concat(taicpu.op_reg_reg_reg(A_ADDU, r, href.base, href.index));
  818. end
  819. else
  820. begin
  821. if r_used then
  822. hreg := GetAddressRegister(list)
  823. else
  824. hreg := r;
  825. list.concat(taicpu.op_reg_reg_const(A_ADDIU, hreg, href.base, href.offset));
  826. list.concat(taicpu.op_reg_reg_reg(A_ADDU, r, hreg, href.index));
  827. end
  828. end
  829. else if href.base <> NR_NO then { Only base }
  830. begin
  831. list.concat(taicpu.op_reg_reg_const(A_ADDIU, r, href.base, href.offset));
  832. end
  833. else
  834. { only offset, can be generated by absolute }
  835. a_load_const_reg(list, OS_ADDR, href.offset, r);
  836. end
  837. else
  838. internalerror(200703111);
  839. end;
  840. procedure TCgMPSel.a_loadfpu_reg_reg(list: tasmlist; fromsize, tosize: tcgsize; reg1, reg2: tregister);
  841. const
  842. FpuMovInstr: array[OS_F32..OS_F64] of TAsmOp =
  843. (A_MOV_S, A_MOV_D);
  844. var
  845. instr: taicpu;
  846. begin
  847. if reg1 <> reg2 then
  848. begin
  849. instr := taicpu.op_reg_reg(fpumovinstr[tosize], reg2, reg1);
  850. list.Concat(instr);
  851. { Notify the register allocator that we have written a move instruction so
  852. it can try to eliminate it. }
  853. add_move_instruction(instr);
  854. end;
  855. end;
  856. procedure TCgMPSel.a_loadfpu_ref_reg(list: tasmlist; fromsize, tosize: tcgsize; const ref: TReference; reg: tregister);
  857. var
  858. tmpref: treference;
  859. tmpreg: tregister;
  860. begin
  861. case tosize of
  862. OS_F32:
  863. handle_load_store_fpu(list, False, A_LWC1, reg, ref);
  864. OS_F64:
  865. handle_load_store_fpu(list, False, A_LDC1, reg, ref);
  866. else
  867. InternalError(2007042701);
  868. end;
  869. end;
  870. procedure TCgMPSel.a_loadfpu_reg_ref(list: tasmlist; fromsize, tosize: tcgsize; reg: tregister; const ref: TReference);
  871. var
  872. tmpref: treference;
  873. tmpreg: tregister;
  874. begin
  875. case tosize of
  876. OS_F32:
  877. handle_load_store_fpu(list, True, A_SWC1, reg, ref);
  878. OS_F64:
  879. handle_load_store_fpu(list, True, A_SDC1, reg, ref);
  880. else
  881. InternalError(2007042702);
  882. end;
  883. end;
  884. procedure TCgMPSel.a_op_const_reg(list: tasmlist; Op: TOpCG; size: tcgsize; a: aint; reg: TRegister);
  885. var
  886. power: longint;
  887. tmpreg1: tregister;
  888. begin
  889. if ((op = OP_MUL) or (op = OP_IMUL)) then
  890. begin
  891. if ispowerof2(a, power) then
  892. begin
  893. { can be done with a shift }
  894. if power < 32 then
  895. begin
  896. list.concat(taicpu.op_reg_reg_const(A_SLL, reg, reg, power));
  897. exit;
  898. end;
  899. end;
  900. end;
  901. if ((op = OP_SUB) or (op = OP_ADD)) then
  902. begin
  903. if (a = 0) then
  904. exit;
  905. end;
  906. if Op in [OP_NEG, OP_NOT] then
  907. internalerror(200306011);
  908. if (a = 0) then
  909. begin
  910. if (Op = OP_IMUL) or (Op = OP_MUL) then
  911. list.concat(taicpu.op_reg_reg(A_MOVE, reg, NR_R0))
  912. else
  913. list.concat(taicpu.op_reg_reg_reg(f_TOpCG2AsmOp(op, size), reg, reg, NR_R0))
  914. end
  915. else
  916. begin
  917. if op = OP_IMUL then
  918. begin
  919. tmpreg1 := GetIntRegister(list, OS_INT);
  920. a_load_const_reg(list, OS_INT, a, tmpreg1);
  921. list.concat(taicpu.op_reg_reg(A_MULT, reg, tmpreg1));
  922. list.concat(taicpu.op_reg(A_MFLO, reg));
  923. end
  924. else if op = OP_MUL then
  925. begin
  926. tmpreg1 := GetIntRegister(list, OS_INT);
  927. a_load_const_reg(list, OS_INT, a, tmpreg1);
  928. list.concat(taicpu.op_reg_reg(A_MULTU, reg, tmpreg1));
  929. list.concat(taicpu.op_reg(A_MFLO, reg));
  930. end
  931. else
  932. handle_reg_const_reg(list, f_TOpCG2AsmOp(op, size), reg, a, reg);
  933. end;
  934. end;
  935. procedure TCgMPSel.a_op_reg_reg(list: tasmlist; Op: TOpCG; size: TCGSize; src, dst: TRegister);
  936. var
  937. a: aint;
  938. begin
  939. case Op of
  940. OP_NEG:
  941. list.concat(taicpu.op_reg_reg(A_NEG, dst, src));
  942. OP_NOT:
  943. begin
  944. list.concat(taicpu.op_reg_reg(A_NOT, dst, src));
  945. end;
  946. else
  947. begin
  948. if op = OP_IMUL then
  949. begin
  950. list.concat(taicpu.op_reg_reg(A_MULT, dst, src));
  951. list.concat(taicpu.op_reg(A_MFLO, dst));
  952. end
  953. else if op = OP_MUL then
  954. begin
  955. list.concat(taicpu.op_reg_reg(A_MULTU, dst, src));
  956. list.concat(taicpu.op_reg(A_MFLO, dst));
  957. end
  958. else
  959. begin
  960. list.concat(taicpu.op_reg_reg_reg(f_TOpCG2AsmOp(op, size), dst, dst, src));
  961. end;
  962. end;
  963. end;
  964. end;
  965. procedure TCgMPSel.a_op_const_reg_reg(list: tasmlist; op: TOpCg; size: tcgsize; a: aint; src, dst: tregister);
  966. var
  967. power: longint;
  968. tmpreg1: tregister;
  969. begin
  970. case op of
  971. OP_MUL,
  972. OP_IMUL:
  973. begin
  974. if ispowerof2(a, power) then
  975. begin
  976. { can be done with a shift }
  977. if power < 32 then
  978. list.concat(taicpu.op_reg_reg_const(A_SLL, dst, src, power))
  979. else
  980. inherited a_op_const_reg_reg(list, op, size, a, src, dst);
  981. exit;
  982. end;
  983. end;
  984. OP_SUB,
  985. OP_ADD:
  986. begin
  987. if (a = 0) then
  988. begin
  989. a_load_reg_reg(list, size, size, src, dst);
  990. exit;
  991. end;
  992. end;
  993. end;
  994. if op = OP_IMUL then
  995. begin
  996. tmpreg1 := GetIntRegister(list, OS_INT);
  997. a_load_const_reg(list, OS_INT, a, tmpreg1);
  998. list.concat(taicpu.op_reg_reg(A_MULT, src, tmpreg1));
  999. list.concat(taicpu.op_reg(A_MFLO, dst));
  1000. end
  1001. else if op = OP_MUL then
  1002. begin
  1003. tmpreg1 := GetIntRegister(list, OS_INT);
  1004. a_load_const_reg(list, OS_INT, a, tmpreg1);
  1005. list.concat(taicpu.op_reg_reg(A_MULTU, src, tmpreg1));
  1006. list.concat(taicpu.op_reg(A_MFLO, dst));
  1007. end
  1008. else
  1009. handle_reg_const_reg(list, f_TOpCG2AsmOp(op, size), src, a, dst);
  1010. end;
  1011. procedure TCgMPSel.a_op_reg_reg_reg(list: tasmlist; op: TOpCg; size: tcgsize; src1, src2, dst: tregister);
  1012. begin
  1013. list.concat(taicpu.op_reg_reg_reg(f_TOpCG2AsmOp(op, size), dst, src2, src1));
  1014. end;
  1015. procedure TCgMPSel.a_op_const_reg_reg_checkoverflow(list: tasmlist; op: TOpCg; size: tcgsize; a: aint; src, dst: tregister; setflags: boolean; var ovloc: tlocation);
  1016. var
  1017. tmpreg1: tregister;
  1018. begin
  1019. ovloc.loc := LOC_VOID;
  1020. case op of
  1021. OP_SUB,
  1022. OP_ADD:
  1023. begin
  1024. if (a = 0) then
  1025. begin
  1026. a_load_reg_reg(list, size, size, src, dst);
  1027. exit;
  1028. end;
  1029. end;
  1030. end;{case}
  1031. case op of
  1032. OP_ADD:
  1033. begin
  1034. if setflags then
  1035. handle_reg_const_reg(list, f_TOpCG2AsmOp_ovf(op, size), src, a, dst)
  1036. else
  1037. handle_reg_const_reg(list, f_TOpCG2AsmOp(op, size), src, a, dst);
  1038. end;
  1039. OP_SUB:
  1040. begin
  1041. if setflags then
  1042. handle_reg_const_reg(list, f_TOpCG2AsmOp_ovf(op, size), src, a, dst)
  1043. else
  1044. handle_reg_const_reg(list, f_TOpCG2AsmOp(op, size), src, a, dst);
  1045. end;
  1046. OP_MUL:
  1047. begin
  1048. if setflags then
  1049. handle_reg_const_reg(list, f_TOpCG2AsmOp_ovf(op, size), src, a, dst)
  1050. else
  1051. begin
  1052. tmpreg1 := GetIntRegister(list, OS_INT);
  1053. a_load_const_reg(list, OS_INT, a, tmpreg1);
  1054. list.concat(taicpu.op_reg_reg(f_TOpCG2AsmOp(op, size),src, tmpreg1));
  1055. list.concat(taicpu.op_reg(A_MFLO, dst));
  1056. end;
  1057. end;
  1058. OP_IMUL:
  1059. begin
  1060. if setflags then
  1061. handle_reg_const_reg(list, f_TOpCG2AsmOp_ovf(op, size), src, a, dst)
  1062. else
  1063. begin
  1064. tmpreg1 := GetIntRegister(list, OS_INT);
  1065. a_load_const_reg(list, OS_INT, a, tmpreg1);
  1066. list.concat(taicpu.op_reg_reg(f_TOpCG2AsmOp(op, size),src, tmpreg1));
  1067. list.concat(taicpu.op_reg(A_MFLO, dst));
  1068. end;
  1069. end;
  1070. OP_XOR, OP_OR, OP_AND:
  1071. begin
  1072. handle_reg_const_reg(list, f_TOpCG2AsmOp_ovf(op, size), src, a, dst);
  1073. end;
  1074. else
  1075. internalerror(2007012601);
  1076. end;
  1077. end;
  1078. procedure TCgMPSel.a_op_reg_reg_reg_checkoverflow(list: tasmlist; op: TOpCg; size: tcgsize; src1, src2, dst: tregister; setflags: boolean; var ovloc: tlocation);
  1079. begin
  1080. ovloc.loc := LOC_VOID;
  1081. case op of
  1082. OP_ADD:
  1083. begin
  1084. if setflags then
  1085. list.concat(taicpu.op_reg_reg_reg(f_TOpCG2AsmOp_ovf(op, size), dst, src2, src1))
  1086. else
  1087. list.concat(taicpu.op_reg_reg_reg(f_TOpCG2AsmOp(op, size), dst, src2, src1));
  1088. end;
  1089. OP_SUB:
  1090. begin
  1091. if setflags then
  1092. list.concat(taicpu.op_reg_reg_reg(f_TOpCG2AsmOp_ovf(op, size), dst, src2, src1))
  1093. else
  1094. list.concat(taicpu.op_reg_reg_reg(f_TOpCG2AsmOp(op, size), dst, src2, src1));
  1095. end;
  1096. OP_MUL:
  1097. begin
  1098. if setflags then
  1099. list.concat(taicpu.op_reg_reg_reg(f_TOpCG2AsmOp_ovf(op, size), dst, src2, src1))
  1100. else
  1101. begin
  1102. list.concat(taicpu.op_reg_reg(f_TOpCG2AsmOp(op, size), src2, src1));
  1103. list.concat(taicpu.op_reg(A_MFLO, dst));
  1104. end;
  1105. end;
  1106. OP_IMUL:
  1107. begin
  1108. if setflags then
  1109. list.concat(taicpu.op_reg_reg_reg(f_TOpCG2AsmOp_ovf(op, size), dst, src2, src1))
  1110. else
  1111. begin
  1112. list.concat(taicpu.op_reg_reg(f_TOpCG2AsmOp(op, size), src2, src1));
  1113. list.concat(taicpu.op_reg(A_MFLO, dst));
  1114. end;
  1115. end;
  1116. OP_XOR, OP_OR, OP_AND:
  1117. begin
  1118. list.concat(taicpu.op_reg_reg_reg(f_TOpCG2AsmOp_ovf(op, size), dst, src2, src1));
  1119. end;
  1120. else
  1121. internalerror(2007012602);
  1122. end;
  1123. end;
  1124. {*************** compare instructructions ****************}
  1125. procedure TCgMPSel.a_cmp_const_reg_label(list: tasmlist; size: tcgsize; cmp_op: topcmp; a: aint; reg: tregister; l: tasmlabel);
  1126. var
  1127. tmpreg: tregister;
  1128. begin
  1129. if a = 0 then
  1130. tmpreg := NR_R0
  1131. else
  1132. begin
  1133. tmpreg := GetIntRegister(list, OS_INT);
  1134. list.concat(taicpu.op_reg_const(A_LI, tmpreg, a));
  1135. end;
  1136. case cmp_op of
  1137. OC_EQ: { equality comparison }
  1138. list.concat(taicpu.op_reg_reg_sym(A_BEQ, reg, tmpreg, l));
  1139. OC_GT: { greater than (signed) }
  1140. list.concat(taicpu.op_reg_reg_sym(A_BGT, reg, tmpreg, l));
  1141. OC_LT: { less than (signed) }
  1142. list.concat(taicpu.op_reg_reg_sym(A_BLT, reg, tmpreg, l));
  1143. OC_GTE: { greater or equal than (signed) }
  1144. list.concat(taicpu.op_reg_reg_sym(A_BGE, reg, tmpreg, l));
  1145. OC_LTE: { less or equal than (signed) }
  1146. list.concat(taicpu.op_reg_reg_sym(A_BLE, reg, tmpreg, l));
  1147. OC_NE: { not equal }
  1148. list.concat(taicpu.op_reg_reg_sym(A_BNE, reg, tmpreg, l));
  1149. OC_BE: { less or equal than (unsigned) }
  1150. list.concat(taicpu.op_reg_reg_sym(A_BLEU, reg, tmpreg, l));
  1151. OC_B: { less than (unsigned) }
  1152. list.concat(taicpu.op_reg_reg_sym(A_BLTU, reg, tmpreg, l));
  1153. OC_AE: { greater or equal than (unsigned) }
  1154. list.concat(taicpu.op_reg_reg_sym(A_BGEU, reg, tmpreg, l));
  1155. OC_A: { greater than (unsigned) }
  1156. list.concat(taicpu.op_reg_reg_sym(A_BGTU, reg, tmpreg, l));
  1157. else
  1158. internalerror(200701071);
  1159. end;
  1160. list.Concat(TAiCpu.Op_none(A_NOP));
  1161. end;
  1162. procedure TCgMPSel.a_cmp_reg_reg_label(list: tasmlist; size: tcgsize; cmp_op: topcmp; reg1, reg2: tregister; l: tasmlabel);
  1163. begin
  1164. case cmp_op of
  1165. OC_EQ: { equality comparison }
  1166. list.concat(taicpu.op_reg_reg_sym(A_BEQ, reg2, reg1, l));
  1167. OC_GT: { greater than (signed) }
  1168. list.concat(taicpu.op_reg_reg_sym(A_BGT, reg2, reg1, l));
  1169. OC_LT: { less than (signed) }
  1170. list.concat(taicpu.op_reg_reg_sym(A_BLT, reg2, reg1, l));
  1171. OC_GTE: { greater or equal than (signed) }
  1172. list.concat(taicpu.op_reg_reg_sym(A_BGE, reg2, reg1, l));
  1173. OC_LTE: { less or equal than (signed) }
  1174. list.concat(taicpu.op_reg_reg_sym(A_BLE, reg2, reg1, l));
  1175. OC_NE: { not equal }
  1176. list.concat(taicpu.op_reg_reg_sym(A_BNE, reg2, reg1, l));
  1177. OC_BE: { less or equal than (unsigned) }
  1178. list.concat(taicpu.op_reg_reg_sym(A_BLEU, reg2, reg1, l));
  1179. OC_B: { less than (unsigned) }
  1180. list.concat(taicpu.op_reg_reg_sym(A_BLTU, reg2, reg1, l));
  1181. OC_AE: { greater or equal than (unsigned) }
  1182. list.concat(taicpu.op_reg_reg_sym(A_BGEU, reg2, reg1, l));
  1183. OC_A: { greater than (unsigned) }
  1184. list.concat(taicpu.op_reg_reg_sym(A_BGTU, reg2, reg1, l));
  1185. else
  1186. internalerror(200701072);
  1187. end;{ case }
  1188. list.Concat(TAiCpu.Op_none(A_NOP));
  1189. end;
  1190. procedure TCgMPSel.a_jmp_always(List: tasmlist; l: TAsmLabel);
  1191. begin
  1192. List.Concat(TAiCpu.op_sym(A_J,l));
  1193. { Delay slot }
  1194. list.Concat(TAiCpu.Op_none(A_NOP));
  1195. end;
  1196. procedure TCgMPSel.a_jmp_name(list: tasmlist; const s: string);
  1197. begin
  1198. List.Concat(TAiCpu.op_sym(A_J, current_asmdata.RefAsmSymbol(s)));
  1199. { Delay slot }
  1200. list.Concat(TAiCpu.Op_none(A_NOP));
  1201. end;
  1202. procedure TCgMPSel.a_jmp_cond(list: tasmlist; cond: TOpCmp; l: TAsmLabel);
  1203. begin
  1204. internalerror(200701181);
  1205. end;
  1206. procedure TCgMPSel.g_overflowCheck(List: tasmlist; const Loc: TLocation; def: TDef);
  1207. begin
  1208. // this is an empty procedure
  1209. end;
  1210. procedure TCgMPSel.g_overflowCheck_loc(List: tasmlist; const Loc: TLocation; def: TDef; ovloc: tlocation);
  1211. begin
  1212. // this is an empty procedure
  1213. end;
  1214. { *********** entry/exit code and address loading ************ }
  1215. procedure TCgMPSel.g_proc_entry(list: tasmlist; localsize: longint; nostackframe: boolean);
  1216. var
  1217. regcounter, firstregfpu, firstreggpr: TSuperRegister;
  1218. href: treference;
  1219. usesfpr, usesgpr, gotgot: boolean;
  1220. regcounter2, firstfpureg: Tsuperregister;
  1221. cond: tasmcond;
  1222. instr: taicpu;
  1223. begin
  1224. if STK2_dummy <> 0 then
  1225. begin
  1226. list.concat(Taicpu.Op_reg_reg_const(A_P_STK2, STK2_PTR, STK2_PTR, -STK2_dummy));
  1227. end;
  1228. if nostackframe then
  1229. exit;
  1230. usesfpr := False;
  1231. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1232. case target_info.abi of
  1233. abi_powerpc_aix:
  1234. firstfpureg := RS_F14;
  1235. abi_powerpc_sysv:
  1236. firstfpureg := RS_F14;
  1237. abi_default:
  1238. firstfpureg := RS_F14;
  1239. else
  1240. internalerror(2003122903);
  1241. end;
  1242. for regcounter := firstfpureg to RS_F31 do
  1243. begin
  1244. if regcounter in rg[R_FPUREGISTER].used_in_proc then
  1245. begin
  1246. usesfpr := True;
  1247. firstregfpu := regcounter;
  1248. break;
  1249. end;
  1250. end;
  1251. usesgpr := False;
  1252. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1253. for regcounter2 := RS_R13 to RS_R31 do
  1254. begin
  1255. if regcounter2 in rg[R_INTREGISTER].used_in_proc then
  1256. begin
  1257. usesgpr := True;
  1258. firstreggpr := regcounter2;
  1259. break;
  1260. end;
  1261. end;
  1262. LocalSize := align(LocalSize, 8);
  1263. cgcpu_calc_stackframe_size := LocalSize;
  1264. list.concat(Taicpu.Op_reg_reg_const(A_P_FRAME, NR_FRAME_POINTER_REG, NR_R31, LocalSize));
  1265. list.concat(Taicpu.op_none(A_P_SET_NOREORDER));
  1266. list.concat(Taicpu.op_none(A_P_SET_NOMACRO));
  1267. list.concat(Taicpu.Op_reg_reg_const(A_P_SW, NR_FRAME_POINTER_REG, NR_STACK_POINTER_REG, -LocalSize));
  1268. list.concat(Taicpu.Op_reg_reg_const(A_P_SW, NR_R31, NR_STACK_POINTER_REG, -LocalSize + 4));
  1269. list.concat(Taicpu.op_reg_reg(A_MOVE, NR_FRAME_POINTER_REG, NR_STACK_POINTER_REG));
  1270. list.concat(Taicpu.Op_reg_reg_const(A_ADDIU, NR_STACK_POINTER_REG, NR_STACK_POINTER_REG, -LocalSize));
  1271. if (cs_create_pic in current_settings.moduleswitches) and
  1272. (pi_needs_got in current_procinfo.flags) then
  1273. begin
  1274. current_procinfo.got := NR_GP;
  1275. end;
  1276. end;
  1277. procedure TCgMPSel.g_proc_exit(list: tasmlist; parasize: longint; nostackframe: boolean);
  1278. var
  1279. hr: treference;
  1280. localsize: aint;
  1281. begin
  1282. localsize := cgcpu_calc_stackframe_size;
  1283. if paramanager.ret_in_param(current_procinfo.procdef.returndef, current_procinfo.procdef.proccalloption) then
  1284. begin
  1285. reference_reset(hr,sizeof(aint));
  1286. hr.offset := 12;
  1287. hr.refaddr := addr_full;
  1288. if nostackframe then
  1289. begin
  1290. if STK2_dummy <> 0 then
  1291. list.concat(Taicpu.Op_reg_reg_const(A_P_STK2, STK2_PTR, STK2_PTR, STK2_dummy));
  1292. list.concat(taicpu.op_reg(A_J, NR_R31));
  1293. list.concat(Taicpu.op_none(A_NOP));
  1294. end
  1295. else
  1296. begin
  1297. list.concat(Taicpu.Op_reg_reg_const(A_P_LW, NR_FRAME_POINTER_REG, NR_STACK_POINTER_REG, 0));
  1298. list.concat(Taicpu.Op_reg_reg_const(A_P_LW, NR_R31, NR_STACK_POINTER_REG, 4));
  1299. list.concat(Taicpu.Op_reg_reg_const(A_ADDIU, NR_STACK_POINTER_REG, NR_STACK_POINTER_REG, localsize));
  1300. if STK2_dummy <> 0 then
  1301. list.concat(Taicpu.Op_reg_reg_const(A_P_STK2, STK2_PTR, STK2_PTR, STK2_dummy));
  1302. list.concat(taicpu.op_reg(A_J, NR_R31));
  1303. list.concat(Taicpu.op_none(A_NOP));
  1304. list.concat(Taicpu.op_none(A_P_SET_MACRO));
  1305. list.concat(Taicpu.op_none(A_P_SET_REORDER));
  1306. end;
  1307. end
  1308. else
  1309. begin
  1310. if nostackframe then
  1311. begin
  1312. if STK2_dummy <> 0 then
  1313. list.concat(Taicpu.Op_reg_reg_const(A_P_STK2, STK2_PTR, STK2_PTR, STK2_dummy));
  1314. list.concat(taicpu.op_reg(A_J, NR_R31));
  1315. list.concat(Taicpu.op_none(A_NOP));
  1316. list.concat(Taicpu.op_none(A_P_SET_MACRO));
  1317. list.concat(Taicpu.op_none(A_P_SET_REORDER));
  1318. end
  1319. else
  1320. begin
  1321. list.concat(Taicpu.Op_reg_reg_const(A_P_LW, NR_FRAME_POINTER_REG, NR_STACK_POINTER_REG, 0));
  1322. list.concat(Taicpu.Op_reg_reg_const(A_P_LW, NR_R31, NR_STACK_POINTER_REG, 4));
  1323. list.concat(Taicpu.Op_reg_reg_const(A_ADDIU, NR_STACK_POINTER_REG, NR_STACK_POINTER_REG, localsize));
  1324. if STK2_dummy <> 0 then
  1325. list.concat(Taicpu.Op_reg_reg_const(A_P_STK2, STK2_PTR, STK2_PTR, STK2_dummy));
  1326. list.concat(taicpu.op_reg(A_J, NR_R31));
  1327. list.concat(Taicpu.op_none(A_NOP));
  1328. list.concat(Taicpu.op_none(A_P_SET_MACRO));
  1329. list.concat(Taicpu.op_none(A_P_SET_REORDER));
  1330. end;
  1331. end;
  1332. end;
  1333. { ************* concatcopy ************ }
  1334. procedure TCgMPSel.g_concatcopy_move(list: tasmlist; const Source, dest: treference; len: aint);
  1335. var
  1336. paraloc1, paraloc2, paraloc3: TCGPara;
  1337. begin
  1338. paraloc1.init;
  1339. paraloc2.init;
  1340. paraloc3.init;
  1341. paramanager.getintparaloc(pocall_default, 1, paraloc1);
  1342. paramanager.getintparaloc(pocall_default, 2, paraloc2);
  1343. paramanager.getintparaloc(pocall_default, 3, paraloc3);
  1344. paramanager.allocparaloc(list, paraloc3);
  1345. a_param_const(list, OS_INT, len, paraloc3);
  1346. paramanager.allocparaloc(list, paraloc2);
  1347. a_paramaddr_ref(list, dest, paraloc2);
  1348. paramanager.allocparaloc(list, paraloc2);
  1349. a_paramaddr_ref(list, Source, paraloc1);
  1350. paramanager.freeparaloc(list, paraloc3);
  1351. paramanager.freeparaloc(list, paraloc2);
  1352. paramanager.freeparaloc(list, paraloc1);
  1353. alloccpuregisters(list, R_INTREGISTER, paramanager.get_volatile_registers_int(pocall_default));
  1354. alloccpuregisters(list, R_FPUREGISTER, paramanager.get_volatile_registers_fpu(pocall_default));
  1355. a_call_name(list, 'FPC_MOVE', false);
  1356. dealloccpuregisters(list, R_FPUREGISTER, paramanager.get_volatile_registers_fpu(pocall_default));
  1357. dealloccpuregisters(list, R_INTREGISTER, paramanager.get_volatile_registers_int(pocall_default));
  1358. paraloc3.done;
  1359. paraloc2.done;
  1360. paraloc1.done;
  1361. end;
  1362. procedure TCgMPSel.g_concatcopy(list: tasmlist; const Source, dest: treference; len: aint);
  1363. var
  1364. tmpreg1, hreg, countreg: TRegister;
  1365. src, dst: TReference;
  1366. lab: tasmlabel;
  1367. Count, count2: aint;
  1368. begin
  1369. if len > high(longint) then
  1370. internalerror(2002072704);
  1371. { anybody wants to determine a good value here :)? }
  1372. if len > 100 then
  1373. g_concatcopy_move(list, Source, dest, len)
  1374. else
  1375. begin
  1376. reference_reset(src,sizeof(aint));
  1377. reference_reset(dst,sizeof(aint));
  1378. { load the address of source into src.base }
  1379. src.base := GetAddressRegister(list);
  1380. a_loadaddr_ref_reg(list, Source, src.base);
  1381. { load the address of dest into dst.base }
  1382. dst.base := GetAddressRegister(list);
  1383. a_loadaddr_ref_reg(list, dest, dst.base);
  1384. { generate a loop }
  1385. Count := len div 4;
  1386. if Count > 4 then
  1387. begin
  1388. { the offsets are zero after the a_loadaddress_ref_reg and just }
  1389. { have to be set to 8. I put an Inc there so debugging may be }
  1390. { easier (should offset be different from zero here, it will be }
  1391. { easy to notice in the generated assembler }
  1392. countreg := GetIntRegister(list, OS_INT);
  1393. tmpreg1 := GetIntRegister(list, OS_INT);
  1394. a_load_const_reg(list, OS_INT, Count, countreg);
  1395. { explicitely allocate R_O0 since it can be used safely here }
  1396. { (for holding date that's being copied) }
  1397. current_asmdata.getjumplabel(lab);
  1398. a_label(list, lab);
  1399. list.concat(taicpu.op_reg_ref(A_LW, tmpreg1, src));
  1400. list.concat(taicpu.op_reg_ref(A_SW, tmpreg1, dst));
  1401. list.concat(taicpu.op_reg_reg_const(A_ADDIU, src.base, src.base, 4));
  1402. list.concat(taicpu.op_reg_reg_const(A_ADDIU, dst.base, dst.base, 4));
  1403. list.concat(taicpu.op_reg_reg_const(A_ADDIU, countreg, countreg, -1));
  1404. list.concat(taicpu.op_reg_sym(A_BGTZ, countreg, lab));
  1405. list.concat(taicpu.op_none(A_NOP));
  1406. len := len mod 4;
  1407. end;
  1408. { unrolled loop }
  1409. Count := len div 4;
  1410. if Count > 0 then
  1411. begin
  1412. tmpreg1 := GetIntRegister(list, OS_INT);
  1413. for count2 := 1 to Count do
  1414. begin
  1415. list.concat(taicpu.op_reg_ref(A_LW, tmpreg1, src));
  1416. list.concat(taicpu.op_reg_ref(A_SW, tmpreg1, dst));
  1417. Inc(src.offset, 4);
  1418. Inc(dst.offset, 4);
  1419. end;
  1420. len := len mod 4;
  1421. end;
  1422. if (len and 4) <> 0 then
  1423. begin
  1424. hreg := GetIntRegister(list, OS_INT);
  1425. a_load_ref_reg(list, OS_32, OS_32, src, hreg);
  1426. a_load_reg_ref(list, OS_32, OS_32, hreg, dst);
  1427. Inc(src.offset, 4);
  1428. Inc(dst.offset, 4);
  1429. end;
  1430. { copy the leftovers }
  1431. if (len and 2) <> 0 then
  1432. begin
  1433. hreg := GetIntRegister(list, OS_INT);
  1434. a_load_ref_reg(list, OS_16, OS_16, src, hreg);
  1435. a_load_reg_ref(list, OS_16, OS_16, hreg, dst);
  1436. Inc(src.offset, 2);
  1437. Inc(dst.offset, 2);
  1438. end;
  1439. if (len and 1) <> 0 then
  1440. begin
  1441. hreg := GetIntRegister(list, OS_INT);
  1442. a_load_ref_reg(list, OS_8, OS_8, src, hreg);
  1443. a_load_reg_ref(list, OS_8, OS_8, hreg, dst);
  1444. end;
  1445. end;
  1446. end;
  1447. procedure TCgMPSel.g_concatcopy_unaligned(list: tasmlist; const Source, dest: treference; len: aint);
  1448. var
  1449. src, dst: TReference;
  1450. tmpreg1, countreg: TRegister;
  1451. i: aint;
  1452. lab: tasmlabel;
  1453. begin
  1454. if len > 31 then
  1455. g_concatcopy_move(list, Source, dest, len)
  1456. else
  1457. begin
  1458. reference_reset(src,sizeof(aint));
  1459. reference_reset(dst,sizeof(aint));
  1460. { load the address of source into src.base }
  1461. src.base := GetAddressRegister(list);
  1462. a_loadaddr_ref_reg(list, Source, src.base);
  1463. { load the address of dest into dst.base }
  1464. dst.base := GetAddressRegister(list);
  1465. a_loadaddr_ref_reg(list, dest, dst.base);
  1466. { generate a loop }
  1467. if len > 4 then
  1468. begin
  1469. { the offsets are zero after the a_loadaddress_ref_reg and just }
  1470. { have to be set to 8. I put an Inc there so debugging may be }
  1471. { easier (should offset be different from zero here, it will be }
  1472. { easy to notice in the generated assembler }
  1473. countreg := GetIntRegister(list, OS_INT);
  1474. tmpreg1 := GetIntRegister(list, OS_INT);
  1475. a_load_const_reg(list, OS_INT, len, countreg);
  1476. { explicitely allocate R_O0 since it can be used safely here }
  1477. { (for holding date that's being copied) }
  1478. current_asmdata.getjumplabel(lab);
  1479. a_label(list, lab);
  1480. list.concat(taicpu.op_reg_ref(A_LBU, tmpreg1, src));
  1481. list.concat(taicpu.op_reg_ref(A_SB, tmpreg1, dst));
  1482. list.concat(taicpu.op_reg_reg_const(A_ADDIU, src.base, src.base, 1));
  1483. list.concat(taicpu.op_reg_reg_const(A_ADDIU, dst.base, dst.base, 1));
  1484. list.concat(taicpu.op_reg_reg_const(A_ADDIU, countreg, countreg, -1));
  1485. list.concat(taicpu.op_reg_sym(A_BGTZ, countreg, lab));
  1486. list.concat(taicpu.op_none(A_NOP));
  1487. end
  1488. else
  1489. begin
  1490. { unrolled loop }
  1491. tmpreg1 := GetIntRegister(list, OS_INT);
  1492. for i := 1 to len do
  1493. begin
  1494. list.concat(taicpu.op_reg_ref(A_LBU, tmpreg1, src));
  1495. list.concat(taicpu.op_reg_ref(A_SB, tmpreg1, dst));
  1496. Inc(src.offset);
  1497. Inc(dst.offset);
  1498. end;
  1499. end;
  1500. end;
  1501. end;
  1502. procedure TCgMPSel.g_intf_wrapper(list: tasmlist; procdef: tprocdef; const labelname: string; ioffset: longint);
  1503. procedure loadvmttor24;
  1504. var
  1505. href: treference;
  1506. begin
  1507. reference_reset_base(href, NR_R2, 0, sizeof(aint)); { return value }
  1508. cg.a_load_ref_reg(list, OS_ADDR, OS_ADDR, href, NR_R24);
  1509. end;
  1510. procedure op_onr24methodaddr;
  1511. var
  1512. href : treference;
  1513. begin
  1514. if (procdef.extnumber=$ffff) then
  1515. Internalerror(200006139);
  1516. { call/jmp vmtoffs(%eax) ; method offs }
  1517. reference_reset_base(href, NR_R24, procdef._class.vmtmethodoffset(procdef.extnumber), sizeof(aint));
  1518. cg.a_load_ref_reg(list, OS_ADDR, OS_ADDR, href, NR_R24);
  1519. list.concat(taicpu.op_reg(A_JR, NR_R24));
  1520. end;
  1521. var
  1522. make_global: boolean;
  1523. href: treference;
  1524. begin
  1525. if procdef.proctypeoption <> potype_none then
  1526. Internalerror(200006137);
  1527. if not assigned(procdef._class) or
  1528. (procdef.procoptions * [po_classmethod, po_staticmethod,
  1529. po_methodpointer, po_interrupt, po_iocheck] <> []) then
  1530. Internalerror(200006138);
  1531. if procdef.owner.symtabletype <> objectsymtable then
  1532. Internalerror(200109191);
  1533. make_global := False;
  1534. if (not current_module.is_unit) or
  1535. (procdef.owner.defowner.owner.symtabletype = globalsymtable) then
  1536. make_global := True;
  1537. if make_global then
  1538. List.concat(Tai_symbol.Createname_global(labelname, AT_FUNCTION, 0))
  1539. else
  1540. List.concat(Tai_symbol.Createname(labelname, AT_FUNCTION, 0));
  1541. { set param1 interface to self }
  1542. g_adjust_self_value(list, procdef, ioffset);
  1543. if po_virtualmethod in procdef.procoptions then
  1544. begin
  1545. loadvmttor24;
  1546. op_onr24methodaddr;
  1547. end
  1548. else
  1549. list.concat(taicpu.op_sym(A_B,current_asmdata.RefAsmSymbol(procdef.mangledname)));
  1550. { Delay slot }
  1551. list.Concat(TAiCpu.Op_none(A_NOP));
  1552. List.concat(Tai_symbol_end.Createname(labelname));
  1553. end;
  1554. {****************************************************************************
  1555. TCG64_MIPSel
  1556. ****************************************************************************}
  1557. procedure TCg64MPSel.a_load64_reg_ref(list: tasmlist; reg: tregister64; const ref: treference);
  1558. var
  1559. tmpref: treference;
  1560. begin
  1561. { Override this function to prevent loading the reference twice }
  1562. tmpref := ref;
  1563. cg.a_load_reg_ref(list, OS_S32, OS_S32, reg.reglo, tmpref);
  1564. Inc(tmpref.offset, 4);
  1565. cg.a_load_reg_ref(list, OS_S32, OS_S32, reg.reghi, tmpref);
  1566. end;
  1567. procedure TCg64MPSel.a_load64_ref_reg(list: tasmlist; const ref: treference; reg: tregister64);
  1568. var
  1569. tmpref: treference;
  1570. begin
  1571. { Override this function to prevent loading the reference twice }
  1572. tmpref := ref;
  1573. cg.a_load_ref_reg(list, OS_S32, OS_S32, tmpref, reg.reglo);
  1574. Inc(tmpref.offset, 4);
  1575. cg.a_load_ref_reg(list, OS_S32, OS_S32, tmpref, reg.reghi);
  1576. end;
  1577. procedure TCg64MPSel.a_param64_ref(list: tasmlist; const r: treference; const paraloc: tcgpara);
  1578. var
  1579. hreg64: tregister64;
  1580. begin
  1581. { Override this function to prevent loading the reference twice.
  1582. Use here some extra registers, but those are optimized away by the RA }
  1583. hreg64.reglo := cg.GetIntRegister(list, OS_S32);
  1584. hreg64.reghi := cg.GetIntRegister(list, OS_S32);
  1585. a_load64_ref_reg(list, r, hreg64);
  1586. a_param64_reg(list, hreg64, paraloc);
  1587. end;
  1588. procedure TCg64MPSel.a_op64_reg_reg(list: tasmlist; op: TOpCG; size: tcgsize; regsrc, regdst: TRegister64);
  1589. var
  1590. op1, op2, op_call64: TAsmOp;
  1591. tmpreg1, tmpreg2: TRegister;
  1592. begin
  1593. tmpreg1 := NR_TCR12; //GetIntRegister(list, OS_INT);
  1594. tmpreg2 := NR_TCR13; //GetIntRegister(list, OS_INT);
  1595. case op of
  1596. OP_ADD:
  1597. begin
  1598. list.concat(taicpu.op_reg_reg_reg(A_ADDU, tmpreg1, regsrc.reglo, regdst.reglo));
  1599. list.concat(taicpu.op_reg_reg_reg(A_SLTU, NR_TCR10, tmpreg1, regsrc.reglo));
  1600. list.concat(taicpu.op_reg_reg_reg(A_ADDU, tmpreg2, regsrc.reghi, regdst.reghi));
  1601. list.concat(taicpu.op_reg_reg_reg(A_ADDU, NR_TCR10, NR_TCR10, tmpreg2));
  1602. list.concat(Taicpu.Op_reg_reg(A_MOVE, regdst.reglo, tmpreg1));
  1603. list.concat(Taicpu.Op_reg_reg(A_MOVE, regdst.reghi, NR_TCR10));
  1604. exit;
  1605. end;
  1606. OP_AND:
  1607. begin
  1608. list.concat(taicpu.op_reg_reg_reg(A_AND, regdst.reglo, regsrc.reglo, regdst.reglo));
  1609. list.concat(taicpu.op_reg_reg_reg(A_AND, regdst.reghi, regsrc.reghi, regdst.reghi));
  1610. exit;
  1611. end;
  1612. OP_NEG:
  1613. begin
  1614. list.concat(taicpu.op_reg_reg_reg(A_SUBU, regdst.reglo, NR_R0, regsrc.reglo));
  1615. list.concat(taicpu.op_reg_reg_reg(A_SLTU, NR_TCR10, NR_R0, regdst.reglo));
  1616. list.concat(taicpu.op_reg_reg_reg(A_SUBU, regdst.reghi, NR_R0, regsrc.reghi));
  1617. list.concat(taicpu.op_reg_reg_reg(A_SUBU, NR_TCR10, regdst.reghi, NR_TCR10));
  1618. list.concat(Taicpu.Op_reg_reg(A_MOVE, regdst.reghi, NR_TCR10));
  1619. exit;
  1620. end;
  1621. OP_NOT:
  1622. begin
  1623. list.concat(taicpu.op_reg_reg_reg(A_NOR, regdst.reglo, NR_R0, regsrc.reglo));
  1624. list.concat(taicpu.op_reg_reg_reg(A_NOR, regdst.reghi, NR_R0, regsrc.reghi));
  1625. exit;
  1626. end;
  1627. OP_OR:
  1628. begin
  1629. list.concat(taicpu.op_reg_reg_reg(A_OR, regdst.reglo, regsrc.reglo, regdst.reglo));
  1630. list.concat(taicpu.op_reg_reg_reg(A_OR, regdst.reghi, regsrc.reghi, regdst.reghi));
  1631. exit;
  1632. end;
  1633. OP_SUB:
  1634. begin
  1635. list.concat(taicpu.op_reg_reg_reg(A_SUBU, tmpreg1, regdst.reglo, regsrc.reglo));
  1636. list.concat(taicpu.op_reg_reg_reg(A_SLTU, NR_TCR10, regdst.reglo, tmpreg1));
  1637. list.concat(taicpu.op_reg_reg_reg(A_SUBU, tmpreg2, regdst.reghi, regsrc.reghi));
  1638. list.concat(taicpu.op_reg_reg_reg(A_SUBU, tmpreg2, tmpreg2, NR_TCR10));
  1639. list.concat(Taicpu.Op_reg_reg(A_MOVE, regdst.reglo, tmpreg1));
  1640. list.concat(Taicpu.Op_reg_reg(A_MOVE, regdst.reghi, tmpreg2));
  1641. exit;
  1642. end;
  1643. OP_XOR:
  1644. begin
  1645. list.concat(taicpu.op_reg_reg_reg(A_XOR, regdst.reglo, regdst.reglo, regsrc.reglo));
  1646. list.concat(taicpu.op_reg_reg_reg(A_XOR, regdst.reghi, regsrc.reghi, regdst.reghi));
  1647. exit;
  1648. end;
  1649. else
  1650. internalerror(200306017);
  1651. end; {case}
  1652. end;
  1653. procedure TCg64MPSel.a_op64_const_reg(list: tasmlist; op: TOpCG; size: tcgsize; Value: int64; regdst: TRegister64);
  1654. var
  1655. op1, op2: TAsmOp;
  1656. begin
  1657. case op of
  1658. OP_NEG,
  1659. OP_NOT:
  1660. internalerror(200306017);
  1661. end;
  1662. a_op64_const_reg_reg(list, op, size, value, regdst, regdst);
  1663. end;
  1664. procedure TCg64MPSel.a_op64_const_reg_reg(list: tasmlist; op: TOpCG; size: tcgsize; Value: int64; regsrc, regdst: tregister64);
  1665. var
  1666. l: tlocation;
  1667. begin
  1668. a_op64_const_reg_reg_checkoverflow(list, op, size, Value, regsrc, regdst, False, l);
  1669. end;
  1670. procedure TCg64MPSel.a_op64_reg_reg_reg(list: tasmlist; op: TOpCG; size: tcgsize; regsrc1, regsrc2, regdst: tregister64);
  1671. var
  1672. l: tlocation;
  1673. begin
  1674. a_op64_reg_reg_reg_checkoverflow(list, op, size, regsrc1, regsrc2, regdst, False, l);
  1675. end;
  1676. procedure TCg64MPSel.a_op64_const_reg_reg_checkoverflow(list: tasmlist; op: TOpCG; size: tcgsize; Value: int64; regsrc, regdst: tregister64; setflags: boolean; var ovloc: tlocation);
  1677. var
  1678. op1, op2: TAsmOp;
  1679. tmpreg1: TRegister;
  1680. begin
  1681. tmpreg1 := NR_TCR12;
  1682. case op of
  1683. OP_NEG,
  1684. OP_NOT:
  1685. internalerror(200306017);
  1686. end;
  1687. list.concat(taicpu.op_reg_const(A_LI, NR_TCR10, aint(hi(Value))));
  1688. list.concat(taicpu.op_reg_const(A_LI, NR_TCR11, aint(lo(Value))));
  1689. case op of
  1690. OP_ADD:
  1691. begin
  1692. list.concat(taicpu.op_reg_reg_reg(A_ADDU, regdst.reglo, regsrc.reglo, NR_TCR10));
  1693. list.concat(taicpu.op_reg_reg_reg(A_SLTU, tmpreg1, regdst.reglo, regsrc.reglo));
  1694. list.concat(taicpu.op_reg_reg_reg(A_ADDU, regdst.reghi, regsrc.reghi, NR_TCR11));
  1695. list.concat(taicpu.op_reg_reg_reg(A_ADDU, tmpreg1, tmpreg1, regdst.reghi));
  1696. list.concat(Taicpu.Op_reg_reg(A_MOVE, regdst.reghi, tmpreg1));
  1697. exit;
  1698. end;
  1699. OP_AND:
  1700. begin
  1701. list.concat(taicpu.op_reg_reg_reg(A_AND, regdst.reglo, regsrc.reglo, NR_TCR10));
  1702. list.concat(taicpu.op_reg_reg_reg(A_AND, regdst.reghi, regsrc.reghi, NR_TCR11));
  1703. exit;
  1704. end;
  1705. OP_OR:
  1706. begin
  1707. list.concat(taicpu.op_reg_reg_reg(A_OR, regdst.reglo, regsrc.reglo, NR_TCR10));
  1708. list.concat(taicpu.op_reg_reg_reg(A_OR, regdst.reghi, regsrc.reghi, NR_TCR11));
  1709. exit;
  1710. end;
  1711. OP_SUB:
  1712. begin
  1713. list.concat(taicpu.op_reg_reg_reg(A_SUBU, regdst.reglo, regsrc.reglo, NR_TCR10));
  1714. list.concat(taicpu.op_reg_reg_reg(A_SLTU, tmpreg1, regsrc.reglo, regdst.reglo));
  1715. list.concat(taicpu.op_reg_reg_reg(A_SUBU, regdst.reghi, regsrc.reghi, NR_TCR11));
  1716. list.concat(taicpu.op_reg_reg_reg(A_SUBU, tmpreg1, regdst.reghi, tmpreg1));
  1717. list.concat(Taicpu.Op_reg_reg(A_MOVE, regdst.reghi, tmpreg1));
  1718. exit;
  1719. end;
  1720. OP_XOR:
  1721. begin
  1722. list.concat(taicpu.op_reg_reg_reg(A_XOR, regdst.reglo, regsrc.reglo, NR_TCR10));
  1723. list.concat(taicpu.op_reg_reg_reg(A_XOR, regdst.reghi, regsrc.reghi, NR_TCR11));
  1724. exit;
  1725. end;
  1726. else
  1727. internalerror(200306017);
  1728. end;
  1729. end;
  1730. procedure TCg64MPSel.a_op64_reg_reg_reg_checkoverflow(list: tasmlist; op: TOpCG; size: tcgsize; regsrc1, regsrc2, regdst: tregister64; setflags: boolean; var ovloc: tlocation);
  1731. var
  1732. op1, op2: TAsmOp;
  1733. tmpreg1, tmpreg2: TRegister;
  1734. begin
  1735. tmpreg1 := NR_TCR12;
  1736. tmpreg2 := NR_TCR13;
  1737. case op of
  1738. OP_NEG,
  1739. OP_NOT:
  1740. internalerror(200306017);
  1741. end;
  1742. case op of
  1743. OP_ADD:
  1744. begin
  1745. list.concat(taicpu.op_reg_reg_reg(A_ADDU, tmpreg1, regsrc2.reglo, regsrc1.reglo));
  1746. list.concat(taicpu.op_reg_reg_reg(A_SLTU, NR_TCR10, tmpreg1, regsrc2.reglo));
  1747. list.concat(taicpu.op_reg_reg_reg(A_ADDU, tmpreg2, regsrc2.reghi, regsrc1.reghi));
  1748. list.concat(taicpu.op_reg_reg_reg(A_ADDU, regdst.reghi, NR_TCR10, tmpreg2));
  1749. list.concat(Taicpu.Op_reg_reg(A_MOVE, regdst.reglo, tmpreg1));
  1750. exit;
  1751. end;
  1752. OP_AND:
  1753. begin
  1754. list.concat(taicpu.op_reg_reg_reg(A_AND, regdst.reglo, regsrc2.reglo, regsrc1.reglo));
  1755. list.concat(taicpu.op_reg_reg_reg(A_AND, regdst.reghi, regsrc2.reghi, regsrc1.reghi));
  1756. exit;
  1757. end;
  1758. OP_OR:
  1759. begin
  1760. list.concat(taicpu.op_reg_reg_reg(A_OR, regdst.reglo, regsrc2.reglo, regsrc1.reglo));
  1761. list.concat(taicpu.op_reg_reg_reg(A_OR, regdst.reghi, regsrc2.reghi, regsrc1.reghi));
  1762. exit;
  1763. end;
  1764. OP_SUB:
  1765. begin
  1766. list.concat(taicpu.op_reg_reg_reg(A_SUBU, tmpreg1, regsrc2.reglo, regsrc1.reglo));
  1767. list.concat(taicpu.op_reg_reg_reg(A_SLTU, NR_TCR10, regsrc2.reglo, tmpreg1));
  1768. list.concat(taicpu.op_reg_reg_reg(A_SUBU, tmpreg2, regsrc2.reghi, regsrc1.reghi));
  1769. list.concat(taicpu.op_reg_reg_reg(A_SUBU, regdst.reghi, tmpreg2, NR_TCR10));
  1770. list.concat(Taicpu.Op_reg_reg(A_MOVE, regdst.reglo, tmpreg1));
  1771. exit;
  1772. end;
  1773. OP_XOR:
  1774. begin
  1775. list.concat(taicpu.op_reg_reg_reg(A_XOR, regdst.reglo, regsrc2.reglo, regsrc1.reglo));
  1776. list.concat(taicpu.op_reg_reg_reg(A_XOR, regdst.reghi, regsrc2.reghi, regsrc1.reghi));
  1777. exit;
  1778. end;
  1779. else
  1780. internalerror(200306017);
  1781. end; {case}
  1782. end;
  1783. procedure create_codegen;
  1784. begin
  1785. cg:=TCgMPSel.Create;
  1786. cg64:=TCg64MPSel.Create;
  1787. end;
  1788. end.