2
0

cgcpu.pas 65 KB

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