cgcpu.pas 84 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. This unit implements the code generator for the PowerPC
  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,symtype,symdef,
  22. cgbase,cgobj,cgppc,
  23. aasmbase,aasmcpu,aasmtai,aasmdata,
  24. cpubase,cpuinfo,cgutils,cg64f32,rgcpu,
  25. parabase;
  26. type
  27. tcgppc = class(tcgppcgen)
  28. procedure init_register_allocators;override;
  29. procedure done_register_allocators;override;
  30. { passing parameters, per default the parameter is pushed }
  31. { nr gives the number of the parameter (enumerated from }
  32. { left to right), this allows to move the parameter to }
  33. { register, if the cpu supports register calling }
  34. { conventions }
  35. procedure a_param_ref(list : TAsmList;size : tcgsize;const r : treference;const paraloc : tcgpara);override;
  36. procedure a_call_name(list : TAsmList;const s : string);override;
  37. procedure a_call_reg(list : TAsmList;reg: tregister); override;
  38. procedure a_op_const_reg(list : TAsmList; Op: TOpCG; size: TCGSize; a: aint; reg: TRegister); override;
  39. procedure a_op_reg_reg(list : TAsmList; Op: TOpCG; size: TCGSize; src, dst: TRegister); override;
  40. procedure a_op_const_reg_reg(list: TAsmList; op: TOpCg;
  41. size: tcgsize; a: aint; src, dst: tregister); override;
  42. procedure a_op_reg_reg_reg(list: TAsmList; op: TOpCg;
  43. size: tcgsize; src1, src2, dst: tregister); override;
  44. { move instructions }
  45. procedure a_load_const_reg(list : TAsmList; size: tcgsize; a : aint;reg : tregister);override;
  46. procedure a_load_ref_reg(list : TAsmList; fromsize, tosize : tcgsize;const Ref : treference;reg : tregister);override;
  47. procedure a_load_reg_reg(list : TAsmList; fromsize, tosize : tcgsize;reg1,reg2 : tregister);override;
  48. procedure a_load_subsetreg_reg(list : TAsmList; subsetsize: tcgsize;
  49. tosize: tcgsize; const sreg: tsubsetregister; destreg: tregister); override;
  50. procedure a_load_subsetreg_subsetreg(list: TAsmlist; fromsubsetsize, tosubsetsize: tcgsize; const fromsreg, tosreg: tsubsetregister); override;
  51. { comparison operations }
  52. procedure a_cmp_const_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : aint;reg : tregister;
  53. l : tasmlabel);override;
  54. procedure a_cmp_reg_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel); override;
  55. procedure a_jmp_name(list : TAsmList;const s : string); override;
  56. procedure a_jmp_always(list : TAsmList;l: tasmlabel); override;
  57. procedure a_jmp_flags(list : TAsmList;const f : TResFlags;l: tasmlabel); override;
  58. procedure g_flags2reg(list: TAsmList; size: TCgSize; const f: TResFlags; reg: TRegister); override;
  59. procedure g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);override;
  60. procedure g_proc_exit(list : TAsmList;parasize : longint;nostackframe:boolean); override;
  61. procedure g_save_standard_registers(list:TAsmList); override;
  62. procedure g_restore_standard_registers(list:TAsmList); override;
  63. procedure a_loadaddr_ref_reg(list : TAsmList;const ref : treference;r : tregister);override;
  64. procedure g_concatcopy(list : TAsmList;const source,dest : treference;len : aint);override;
  65. procedure g_overflowcheck(list: TAsmList; const l: tlocation; def: tdef); override;
  66. { find out whether a is of the form 11..00..11b or 00..11...00. If }
  67. { that's the case, we can use rlwinm to do an AND operation }
  68. function get_rlwi_const(a: aint; var l1, l2: longint): boolean;
  69. procedure a_jmp_cond(list : TAsmList;cond : TOpCmp;l: tasmlabel);
  70. procedure g_intf_wrapper(list: TAsmList; procdef: tprocdef; const labelname: string; ioffset: longint);override;
  71. protected
  72. procedure a_load_regconst_subsetreg_intern(list : TAsmList; fromsize, subsetsize: tcgsize; fromreg: tregister; const sreg: tsubsetregister; slopt: tsubsetloadopt); override;
  73. private
  74. (* NOT IN USE: *)
  75. procedure g_stackframe_entry_mac(list : TAsmList;localsize : longint);
  76. (* NOT IN USE: *)
  77. procedure g_return_from_proc_mac(list : TAsmList;parasize : aint);
  78. { clear out potential overflow bits from 8 or 16 bit operations }
  79. { the upper 24/16 bits of a register after an operation }
  80. procedure maybeadjustresult(list: TAsmList; op: TOpCg; size: tcgsize; dst: tregister);
  81. { Make sure ref is a valid reference for the PowerPC and sets the }
  82. { base to the value of the index if (base = R_NO). }
  83. { Returns true if the reference contained a base, index and an }
  84. { offset or symbol, in which case the base will have been changed }
  85. { to a tempreg (which has to be freed by the caller) containing }
  86. { the sum of part of the original reference }
  87. function fixref(list: TAsmList; var ref: treference): boolean; override;
  88. { returns whether a reference can be used immediately in a powerpc }
  89. { instruction }
  90. function issimpleref(const ref: treference): boolean;
  91. { contains the common code of a_load_reg_ref and a_load_ref_reg }
  92. procedure a_load_store(list:TAsmList;op: tasmop;reg:tregister;
  93. ref: treference); override;
  94. { creates the correct branch instruction for a given combination }
  95. { of asmcondflags and destination addressing mode }
  96. procedure a_jmp(list: TAsmList; op: tasmop;
  97. c: tasmcondflag; crval: longint; l: tasmlabel);
  98. function save_regs(list : TAsmList):longint;
  99. procedure restore_regs(list : TAsmList);
  100. end;
  101. tcg64fppc = class(tcg64f32)
  102. procedure a_op64_reg_reg(list : TAsmList;op:TOpCG;size : tcgsize;regsrc,regdst : tregister64);override;
  103. procedure a_op64_const_reg(list : TAsmList;op:TOpCG;size : tcgsize;value : int64;reg : tregister64);override;
  104. procedure a_op64_const_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;value : int64;regsrc,regdst : tregister64);override;
  105. procedure a_op64_reg_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64);override;
  106. end;
  107. const
  108. TOpCG2AsmOpConstLo: Array[topcg] of TAsmOp = (A_NONE,A_MR,A_ADDI,A_ANDI_,A_DIVWU,
  109. A_DIVW,A_MULLW, A_MULLW, A_NONE,A_NONE,A_ORI,
  110. A_SRAWI,A_SLWI,A_SRWI,A_SUBI,A_XORI);
  111. TOpCG2AsmOpConstHi: Array[topcg] of TAsmOp = (A_NONE,A_MR,A_ADDIS,A_ANDIS_,
  112. A_DIVWU,A_DIVW, A_MULLW,A_MULLW,A_NONE,A_NONE,
  113. A_ORIS,A_NONE, A_NONE,A_NONE,A_SUBIS,A_XORIS);
  114. TOpCmp2AsmCond: Array[topcmp] of TAsmCondFlag = (C_NONE,C_EQ,C_GT,
  115. C_LT,C_GE,C_LE,C_NE,C_LE,C_LT,C_GE,C_GT);
  116. implementation
  117. uses
  118. globals,verbose,systems,cutils,
  119. symconst,symsym,fmodule,
  120. rgobj,tgobj,cpupi,procinfo,paramgr;
  121. procedure tcgppc.init_register_allocators;
  122. begin
  123. inherited init_register_allocators;
  124. if target_info.system=system_powerpc_darwin then
  125. begin
  126. {
  127. if pi_needs_got in current_procinfo.flags then
  128. begin
  129. current_procinfo.got:=NR_R31;
  130. rg[R_INTREGISTER]:=trgcpu.create(R_INTREGISTER,R_SUBWHOLE,
  131. [RS_R2,RS_R3,RS_R4,RS_R5,RS_R6,RS_R7,RS_R8,
  132. RS_R9,RS_R10,RS_R11,RS_R12,RS_R30,RS_R29,
  133. RS_R28,RS_R27,RS_R26,RS_R25,RS_R24,RS_R23,RS_R22,
  134. RS_R21,RS_R20,RS_R19,RS_R18,RS_R17,RS_R16,RS_R15,
  135. RS_R14,RS_R13],first_int_imreg,[]);
  136. end
  137. else}
  138. rg[R_INTREGISTER]:=trgcpu.create(R_INTREGISTER,R_SUBWHOLE,
  139. [RS_R2,RS_R3,RS_R4,RS_R5,RS_R6,RS_R7,RS_R8,
  140. RS_R9,RS_R10,RS_R11,RS_R12,RS_R31,RS_R30,RS_R29,
  141. RS_R28,RS_R27,RS_R26,RS_R25,RS_R24,RS_R23,RS_R22,
  142. RS_R21,RS_R20,RS_R19,RS_R18,RS_R17,RS_R16,RS_R15,
  143. RS_R14,RS_R13],first_int_imreg,[]);
  144. end
  145. else
  146. rg[R_INTREGISTER]:=trgcpu.create(R_INTREGISTER,R_SUBWHOLE,
  147. [RS_R3,RS_R4,RS_R5,RS_R6,RS_R7,RS_R8,
  148. RS_R9,RS_R10,RS_R11,RS_R12,RS_R31,RS_R30,RS_R29,
  149. RS_R28,RS_R27,RS_R26,RS_R25,RS_R24,RS_R23,RS_R22,
  150. RS_R21,RS_R20,RS_R19,RS_R18,RS_R17,RS_R16,RS_R15,
  151. RS_R14,RS_R13],first_int_imreg,[]);
  152. rg[R_FPUREGISTER]:=trgcpu.create(R_FPUREGISTER,R_SUBNONE,
  153. [RS_F0,RS_F1,RS_F2,RS_F3,RS_F4,RS_F5,RS_F6,RS_F7,RS_F8,RS_F9,
  154. RS_F10,RS_F11,RS_F12,RS_F13,RS_F31,RS_F30,RS_F29,RS_F28,RS_F27,
  155. RS_F26,RS_F25,RS_F24,RS_F23,RS_F22,RS_F21,RS_F20,RS_F19,RS_F18,
  156. RS_F17,RS_F16,RS_F15,RS_F14],first_fpu_imreg,[]);
  157. {$warning FIX ME}
  158. rg[R_MMREGISTER]:=trgcpu.create(R_MMREGISTER,R_SUBNONE,
  159. [RS_M0,RS_M1,RS_M2],first_mm_imreg,[]);
  160. end;
  161. procedure tcgppc.done_register_allocators;
  162. begin
  163. rg[R_INTREGISTER].free;
  164. rg[R_FPUREGISTER].free;
  165. rg[R_MMREGISTER].free;
  166. inherited done_register_allocators;
  167. end;
  168. procedure tcgppc.a_param_ref(list : TAsmList;size : tcgsize;const r : treference;const paraloc : tcgpara);
  169. var
  170. tmpref, ref: treference;
  171. location: pcgparalocation;
  172. sizeleft: aint;
  173. begin
  174. location := paraloc.location;
  175. tmpref := r;
  176. sizeleft := paraloc.intsize;
  177. while assigned(location) do
  178. begin
  179. case location^.loc of
  180. LOC_REGISTER,LOC_CREGISTER:
  181. begin
  182. {$ifndef cpu64bit}
  183. if (sizeleft <> 3) then
  184. begin
  185. a_load_ref_reg(list,location^.size,location^.size,tmpref,location^.register);
  186. end
  187. else
  188. begin
  189. a_load_ref_reg(list,OS_16,OS_16,tmpref,location^.register);
  190. a_reg_alloc(list,NR_R0);
  191. inc(tmpref.offset,2);
  192. a_load_ref_reg(list,OS_8,OS_8,tmpref,newreg(R_INTREGISTER,RS_R0,R_SUBNONE));
  193. a_op_const_reg(list,OP_SHL,OS_INT,16,location^.register);
  194. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWIMI,location^.register,newreg(R_INTREGISTER,RS_R0,R_SUBNONE),8,16,31-8));
  195. a_reg_dealloc(list,NR_R0);
  196. dec(tmpref.offset,2);
  197. end;
  198. {$else not cpu64bit}
  199. {$error add 64 bit support for non power of 2 loads in a_param_ref}
  200. {$endif not cpu64bit}
  201. end;
  202. LOC_REFERENCE:
  203. begin
  204. reference_reset_base(ref,location^.reference.index,location^.reference.offset);
  205. g_concatcopy(list,tmpref,ref,sizeleft);
  206. if assigned(location^.next) then
  207. internalerror(2005010710);
  208. end;
  209. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  210. case location^.size of
  211. OS_F32, OS_F64:
  212. a_loadfpu_ref_reg(list,location^.size,location^.size,tmpref,location^.register);
  213. else
  214. internalerror(2002072801);
  215. end;
  216. LOC_VOID:
  217. begin
  218. // nothing to do
  219. end;
  220. else
  221. internalerror(2002081103);
  222. end;
  223. inc(tmpref.offset,tcgsize2size[location^.size]);
  224. dec(sizeleft,tcgsize2size[location^.size]);
  225. location := location^.next;
  226. end;
  227. end;
  228. { calling a procedure by name }
  229. procedure tcgppc.a_call_name(list : TAsmList;const s : string);
  230. begin
  231. { MacOS: The linker on MacOS (PPCLink) inserts a call to glue code,
  232. if it is a cross-TOC call. If so, it also replaces the NOP
  233. with some restore code.}
  234. if (target_info.system <> system_powerpc_darwin) then
  235. begin
  236. list.concat(taicpu.op_sym(A_BL,current_asmdata.RefAsmSymbol(s)));
  237. if target_info.system=system_powerpc_macos then
  238. list.concat(taicpu.op_none(A_NOP));
  239. end
  240. else
  241. list.concat(taicpu.op_sym(A_BL,get_darwin_call_stub(s)));
  242. {
  243. the compiler does not properly set this flag anymore in pass 1, and
  244. for now we only need it after pass 2 (I hope) (JM)
  245. if not(pi_do_call in current_procinfo.flags) then
  246. internalerror(2003060703);
  247. }
  248. include(current_procinfo.flags,pi_do_call);
  249. end;
  250. { calling a procedure by address }
  251. procedure tcgppc.a_call_reg(list : TAsmList;reg: tregister);
  252. var
  253. tmpreg : tregister;
  254. tmpref : treference;
  255. begin
  256. if target_info.system=system_powerpc_macos then
  257. begin
  258. {Generate instruction to load the procedure address from
  259. the transition vector.}
  260. //TODO: Support cross-TOC calls.
  261. tmpreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  262. reference_reset(tmpref);
  263. tmpref.offset := 0;
  264. //tmpref.symaddr := refs_full;
  265. tmpref.base:= reg;
  266. list.concat(taicpu.op_reg_ref(A_LWZ,tmpreg,tmpref));
  267. end
  268. else
  269. tmpreg:=reg;
  270. inherited a_call_reg(list,tmpreg);
  271. end;
  272. {********************** load instructions ********************}
  273. procedure tcgppc.a_load_const_reg(list : TAsmList; size: TCGSize; a : aint; reg : TRegister);
  274. begin
  275. if not(size in [OS_8,OS_S8,OS_16,OS_S16,OS_32,OS_S32]) then
  276. internalerror(2002090902);
  277. if (a >= low(smallint)) and
  278. (a <= high(smallint)) then
  279. list.concat(taicpu.op_reg_const(A_LI,reg,smallint(a)))
  280. else if ((a and $ffff) <> 0) then
  281. begin
  282. list.concat(taicpu.op_reg_const(A_LI,reg,smallint(a and $ffff)));
  283. if ((a shr 16) <> 0) or
  284. (smallint(a and $ffff) < 0) then
  285. list.concat(taicpu.op_reg_reg_const(A_ADDIS,reg,reg,
  286. smallint((a shr 16)+ord(smallint(a and $ffff) < 0))))
  287. end
  288. else
  289. list.concat(taicpu.op_reg_const(A_LIS,reg,smallint(a shr 16)));
  290. end;
  291. procedure tcgppc.a_load_ref_reg(list : TAsmList; fromsize,tosize : tcgsize;const ref: treference;reg : tregister);
  292. const
  293. LoadInstr: Array[OS_8..OS_S32,boolean, boolean] of TAsmOp =
  294. { indexed? updating?}
  295. (((A_LBZ,A_LBZU),(A_LBZX,A_LBZUX)),
  296. ((A_LHZ,A_LHZU),(A_LHZX,A_LHZUX)),
  297. ((A_LWZ,A_LWZU),(A_LWZX,A_LWZUX)),
  298. { 64bit stuff should be handled separately }
  299. ((A_NONE,A_NONE),(A_NONE,A_NONE)),
  300. { 128bit stuff too }
  301. ((A_NONE,A_NONE),(A_NONE,A_NONE)),
  302. { there's no load-byte-with-sign-extend :( }
  303. ((A_LBZ,A_LBZU),(A_LBZX,A_LBZUX)),
  304. ((A_LHA,A_LHAU),(A_LHAX,A_LHAUX)),
  305. ((A_LWZ,A_LWZU),(A_LWZX,A_LWZUX)));
  306. var
  307. op: tasmop;
  308. ref2: treference;
  309. begin
  310. { TODO: optimize/take into consideration fromsize/tosize. Will }
  311. { probably only matter for OS_S8 loads though }
  312. if not(fromsize in [OS_8,OS_S8,OS_16,OS_S16,OS_32,OS_S32]) then
  313. internalerror(2002090902);
  314. ref2 := ref;
  315. fixref(list,ref2);
  316. { the caller is expected to have adjusted the reference already }
  317. { in this case }
  318. if (TCGSize2Size[fromsize] >= TCGSize2Size[tosize]) then
  319. fromsize := tosize;
  320. op := loadinstr[fromsize,ref2.index<>NR_NO,false];
  321. a_load_store(list,op,reg,ref2);
  322. { sign extend shortint if necessary, since there is no }
  323. { load instruction that does that automatically (JM) }
  324. if fromsize = OS_S8 then
  325. list.concat(taicpu.op_reg_reg(A_EXTSB,reg,reg));
  326. end;
  327. procedure tcgppc.a_load_reg_reg(list : TAsmList;fromsize, tosize : tcgsize;reg1,reg2 : tregister);
  328. var
  329. instr: taicpu;
  330. begin
  331. if (tcgsize2size[fromsize] > tcgsize2size[tosize]) or
  332. ((tcgsize2size[fromsize] = tcgsize2size[tosize]) and
  333. (fromsize <> tosize)) or
  334. { needs to mask out the sign in the top 16 bits }
  335. ((fromsize = OS_S8) and
  336. (tosize = OS_16)) then
  337. case tosize of
  338. OS_8:
  339. instr := taicpu.op_reg_reg_const_const_const(A_RLWINM,
  340. reg2,reg1,0,31-8+1,31);
  341. OS_S8:
  342. instr := taicpu.op_reg_reg(A_EXTSB,reg2,reg1);
  343. OS_16:
  344. instr := taicpu.op_reg_reg_const_const_const(A_RLWINM,
  345. reg2,reg1,0,31-16+1,31);
  346. OS_S16:
  347. instr := taicpu.op_reg_reg(A_EXTSH,reg2,reg1);
  348. OS_32,OS_S32:
  349. instr := taicpu.op_reg_reg(A_MR,reg2,reg1);
  350. else internalerror(2002090901);
  351. end
  352. else
  353. instr := taicpu.op_reg_reg(A_MR,reg2,reg1);
  354. list.concat(instr);
  355. rg[R_INTREGISTER].add_move_instruction(instr);
  356. end;
  357. procedure tcgppc.a_load_subsetreg_reg(list : TAsmList; subsetsize, tosize: tcgsize; const sreg: tsubsetregister; destreg: tregister);
  358. begin
  359. if (sreg.bitlen <> sizeof(aint)*8) then
  360. begin
  361. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWINM,destreg,
  362. sreg.subsetreg,(32-sreg.startbit) and 31,32-sreg.bitlen,31));
  363. { types with a negative lower bound are always a base type (8, 16, 32 bits) }
  364. if ((sreg.bitlen mod 8) = 0) then
  365. begin
  366. a_load_reg_reg(list,tcgsize2unsigned[subsetsize],subsetsize,destreg,destreg);
  367. a_load_reg_reg(list,subsetsize,tosize,destreg,destreg);
  368. end;
  369. end
  370. else
  371. a_load_reg_reg(list,subsetsize,tosize,sreg.subsetreg,destreg);
  372. end;
  373. procedure tcgppc.a_load_regconst_subsetreg_intern(list : TAsmList; fromsize, subsetsize: tcgsize; fromreg: tregister; const sreg: tsubsetregister; slopt: tsubsetloadopt);
  374. begin
  375. if (slopt in [SL_SETZERO,SL_SETMAX]) then
  376. inherited a_load_regconst_subsetreg_intern(list,fromsize,subsetsize,fromreg,sreg,slopt)
  377. else if (sreg.bitlen <> sizeof(aint) * 8) then
  378. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWIMI,sreg.subsetreg,fromreg,
  379. sreg.startbit,32-sreg.startbit-sreg.bitlen,31-sreg.startbit))
  380. else
  381. a_load_reg_reg(list,fromsize,subsetsize,fromreg,sreg.subsetreg);
  382. end;
  383. procedure tcgppc.a_load_subsetreg_subsetreg(list: TAsmlist; fromsubsetsize, tosubsetsize: tcgsize; const fromsreg, tosreg: tsubsetregister);
  384. begin
  385. if (fromsreg.bitlen >= tosreg.bitlen) then
  386. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWIMI,tosreg.subsetreg, fromsreg.subsetreg,
  387. (tosreg.startbit-fromsreg.startbit) and 31,
  388. 32-tosreg.startbit-tosreg.bitlen,31-tosreg.startbit))
  389. else
  390. inherited a_load_subsetreg_subsetreg(list,fromsubsetsize,tosubsetsize,fromsreg,tosreg);
  391. end;
  392. procedure tcgppc.a_op_const_reg(list : TAsmList; Op: TOpCG; size: TCGSize; a: aint; reg: TRegister);
  393. begin
  394. a_op_const_reg_reg(list,op,size,a,reg,reg);
  395. end;
  396. procedure tcgppc.a_op_reg_reg(list : TAsmList; Op: TOpCG; size: TCGSize; src, dst: TRegister);
  397. begin
  398. a_op_reg_reg_reg(list,op,size,src,dst,dst);
  399. end;
  400. procedure tcgppc.maybeadjustresult(list: TAsmList; op: TOpCg; size: tcgsize; dst: tregister);
  401. const
  402. overflowops = [OP_MUL,OP_SHL,OP_ADD,OP_SUB,OP_NOT,OP_NEG];
  403. begin
  404. if (op in overflowops) and
  405. (size in [OS_8,OS_S8,OS_16,OS_S16]) then
  406. a_load_reg_reg(list,OS_32,size,dst,dst);
  407. end;
  408. procedure tcgppc.a_op_const_reg_reg(list: TAsmList; op: TOpCg;
  409. size: tcgsize; a: aint; src, dst: tregister);
  410. var
  411. l1,l2: longint;
  412. oplo, ophi: tasmop;
  413. scratchreg: tregister;
  414. useReg, gotrlwi: boolean;
  415. procedure do_lo_hi;
  416. begin
  417. list.concat(taicpu.op_reg_reg_const(oplo,dst,src,word(a)));
  418. list.concat(taicpu.op_reg_reg_const(ophi,dst,dst,word(a shr 16)));
  419. end;
  420. begin
  421. if (op = OP_MOVE) then
  422. internalerror(2006031401);
  423. if op = OP_SUB then
  424. begin
  425. a_op_const_reg_reg(list,OP_ADD,size,-a,src,dst);
  426. exit;
  427. end;
  428. ophi := TOpCG2AsmOpConstHi[op];
  429. oplo := TOpCG2AsmOpConstLo[op];
  430. gotrlwi := get_rlwi_const(a,l1,l2);
  431. if (op in [OP_AND,OP_OR,OP_XOR]) then
  432. begin
  433. if (a = 0) then
  434. begin
  435. if op = OP_AND then
  436. list.concat(taicpu.op_reg_const(A_LI,dst,0))
  437. else
  438. a_load_reg_reg(list,size,size,src,dst);
  439. exit;
  440. end
  441. else if (a = -1) then
  442. begin
  443. case op of
  444. OP_OR:
  445. case size of
  446. OS_8, OS_S8:
  447. list.concat(taicpu.op_reg_const(A_LI,dst,255));
  448. OS_16, OS_S16:
  449. a_load_const_reg(list,OS_16,65535,dst);
  450. else
  451. list.concat(taicpu.op_reg_const(A_LI,dst,-1));
  452. end;
  453. OP_XOR:
  454. case size of
  455. OS_8, OS_S8:
  456. list.concat(taicpu.op_reg_reg_const(A_XORI,dst,src,255));
  457. OS_16, OS_S16:
  458. list.concat(taicpu.op_reg_reg_const(A_XORI,dst,src,65535));
  459. else
  460. list.concat(taicpu.op_reg_reg(A_NOT,dst,src));
  461. end;
  462. OP_AND:
  463. a_load_reg_reg(list,size,size,src,dst);
  464. end;
  465. exit;
  466. end
  467. else if (aword(a) <= high(word)) and
  468. ((op <> OP_AND) or
  469. not gotrlwi) then
  470. begin
  471. if ((size = OS_8) and
  472. (byte(a) <> a)) or
  473. ((size = OS_S8) and
  474. (shortint(a) <> a)) then
  475. internalerror(200604142);
  476. list.concat(taicpu.op_reg_reg_const(oplo,dst,src,word(a)));
  477. { and/or/xor -> cannot overflow in high 16 bits }
  478. exit;
  479. end;
  480. { all basic constant instructions also have a shifted form that }
  481. { works only on the highest 16bits, so if lo(a) is 0, we can }
  482. { use that one }
  483. if (word(a) = 0) and
  484. (not(op = OP_AND) or
  485. not gotrlwi) then
  486. begin
  487. if (size in [OS_8,OS_S8,OS_16,OS_S16]) then
  488. internalerror(200604141);
  489. list.concat(taicpu.op_reg_reg_const(ophi,dst,src,word(a shr 16)));
  490. exit;
  491. end;
  492. end
  493. else if (op = OP_ADD) then
  494. if a = 0 then
  495. begin
  496. a_load_reg_reg(list,size,size,src,dst);
  497. exit
  498. end
  499. else if (a >= low(smallint)) and
  500. (a <= high(smallint)) then
  501. begin
  502. list.concat(taicpu.op_reg_reg_const(A_ADDI,dst,src,smallint(a)));
  503. maybeadjustresult(list,op,size,dst);
  504. exit;
  505. end;
  506. { otherwise, the instructions we can generate depend on the }
  507. { operation }
  508. useReg := false;
  509. case op of
  510. OP_DIV,OP_IDIV:
  511. if (a = 0) then
  512. internalerror(200208103)
  513. else if (a = 1) then
  514. begin
  515. a_load_reg_reg(list,OS_INT,OS_INT,src,dst);
  516. exit
  517. end
  518. else if ispowerof2(a,l1) then
  519. begin
  520. case op of
  521. OP_DIV:
  522. list.concat(taicpu.op_reg_reg_const(A_SRWI,dst,src,l1));
  523. OP_IDIV:
  524. begin
  525. list.concat(taicpu.op_reg_reg_const(A_SRAWI,dst,src,l1));
  526. list.concat(taicpu.op_reg_reg(A_ADDZE,dst,dst));
  527. end;
  528. end;
  529. exit;
  530. end
  531. else
  532. usereg := true;
  533. OP_IMUL, OP_MUL:
  534. if (a = 0) then
  535. begin
  536. list.concat(taicpu.op_reg_const(A_LI,dst,0));
  537. exit
  538. end
  539. else if (a = 1) then
  540. begin
  541. a_load_reg_reg(list,OS_INT,OS_INT,src,dst);
  542. exit
  543. end
  544. else if ispowerof2(a,l1) then
  545. list.concat(taicpu.op_reg_reg_const(A_SLWI,dst,src,l1))
  546. else if (longint(a) >= low(smallint)) and
  547. (longint(a) <= high(smallint)) then
  548. list.concat(taicpu.op_reg_reg_const(A_MULLI,dst,src,smallint(a)))
  549. else
  550. usereg := true;
  551. OP_ADD:
  552. begin
  553. list.concat(taicpu.op_reg_reg_const(oplo,dst,src,smallint(a)));
  554. list.concat(taicpu.op_reg_reg_const(ophi,dst,dst,
  555. smallint((a shr 16) + ord(smallint(a) < 0))));
  556. end;
  557. OP_OR:
  558. { try to use rlwimi }
  559. if gotrlwi and
  560. (src = dst) then
  561. begin
  562. scratchreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  563. list.concat(taicpu.op_reg_const(A_LI,scratchreg,-1));
  564. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWIMI,dst,
  565. scratchreg,0,l1,l2));
  566. end
  567. else
  568. do_lo_hi;
  569. OP_AND:
  570. { try to use rlwinm }
  571. if gotrlwi then
  572. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWINM,dst,
  573. src,0,l1,l2))
  574. else
  575. useReg := true;
  576. OP_XOR:
  577. do_lo_hi;
  578. OP_SHL,OP_SHR,OP_SAR:
  579. begin
  580. if (a and 31) <> 0 Then
  581. list.concat(taicpu.op_reg_reg_const(
  582. TOpCG2AsmOpConstLo[Op],dst,src,a and 31))
  583. else
  584. a_load_reg_reg(list,size,size,src,dst);
  585. if (a shr 5) <> 0 then
  586. internalError(68991);
  587. end
  588. else
  589. internalerror(200109091);
  590. end;
  591. { if all else failed, load the constant in a register and then }
  592. { perform the operation }
  593. if useReg then
  594. begin
  595. scratchreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  596. a_load_const_reg(list,OS_32,a,scratchreg);
  597. a_op_reg_reg_reg(list,op,OS_32,scratchreg,src,dst);
  598. end;
  599. maybeadjustresult(list,op,size,dst);
  600. end;
  601. procedure tcgppc.a_op_reg_reg_reg(list: TAsmList; op: TOpCg;
  602. size: tcgsize; src1, src2, dst: tregister);
  603. const
  604. op_reg_reg_opcg2asmop: array[TOpCG] of tasmop =
  605. (A_NONE,A_MR,A_ADD,A_AND,A_DIVWU,A_DIVW,A_MULLW,A_MULLW,A_NEG,A_NOT,A_OR,
  606. A_SRAW,A_SLW,A_SRW,A_SUB,A_XOR);
  607. begin
  608. if (op = OP_MOVE) then
  609. internalerror(2006031402);
  610. case op of
  611. OP_NEG,OP_NOT:
  612. begin
  613. list.concat(taicpu.op_reg_reg(op_reg_reg_opcg2asmop[op],dst,src1));
  614. if (op = OP_NOT) and
  615. not(size in [OS_32,OS_S32]) then
  616. { zero/sign extend result again }
  617. a_load_reg_reg(list,OS_32,size,dst,dst);
  618. end;
  619. else
  620. list.concat(taicpu.op_reg_reg_reg(op_reg_reg_opcg2asmop[op],dst,src2,src1));
  621. end;
  622. maybeadjustresult(list,op,size,dst);
  623. end;
  624. {*************** compare instructructions ****************}
  625. procedure tcgppc.a_cmp_const_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : aint;reg : tregister;
  626. l : tasmlabel);
  627. var
  628. scratch_register: TRegister;
  629. signed: boolean;
  630. begin
  631. signed := cmp_op in [OC_GT,OC_LT,OC_GTE,OC_LTE,OC_EQ,OC_NE];
  632. { in the following case, we generate more efficient code when }
  633. { signed is false }
  634. if (cmp_op in [OC_EQ,OC_NE]) and
  635. (aword(a) >= $8000) and
  636. (aword(a) <= $ffff) then
  637. signed := false;
  638. if signed then
  639. if (a >= low(smallint)) and (a <= high(smallint)) Then
  640. list.concat(taicpu.op_reg_reg_const(A_CMPWI,NR_CR0,reg,a))
  641. else
  642. begin
  643. scratch_register := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  644. a_load_const_reg(list,OS_32,a,scratch_register);
  645. list.concat(taicpu.op_reg_reg_reg(A_CMPW,NR_CR0,reg,scratch_register));
  646. end
  647. else
  648. if (aword(a) <= $ffff) then
  649. list.concat(taicpu.op_reg_reg_const(A_CMPLWI,NR_CR0,reg,aword(a)))
  650. else
  651. begin
  652. scratch_register := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  653. a_load_const_reg(list,OS_32,a,scratch_register);
  654. list.concat(taicpu.op_reg_reg_reg(A_CMPLW,NR_CR0,reg,scratch_register));
  655. end;
  656. a_jmp(list,A_BC,TOpCmp2AsmCond[cmp_op],0,l);
  657. end;
  658. procedure tcgppc.a_cmp_reg_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;
  659. reg1,reg2 : tregister;l : tasmlabel);
  660. var
  661. op: tasmop;
  662. begin
  663. if cmp_op in [OC_GT,OC_LT,OC_GTE,OC_LTE] then
  664. op := A_CMPW
  665. else
  666. op := A_CMPLW;
  667. list.concat(taicpu.op_reg_reg_reg(op,NR_CR0,reg2,reg1));
  668. a_jmp(list,A_BC,TOpCmp2AsmCond[cmp_op],0,l);
  669. end;
  670. procedure tcgppc.a_jmp_cond(list : TAsmList;cond : TOpCmp;l: tasmlabel);
  671. begin
  672. a_jmp(list,A_BC,TOpCmp2AsmCond[cond],0,l);
  673. end;
  674. procedure tcgppc.a_jmp_name(list : TAsmList;const s : string);
  675. var
  676. p : taicpu;
  677. begin
  678. if (target_info.system = system_powerpc_darwin) then
  679. p := taicpu.op_sym(A_B,get_darwin_call_stub(s))
  680. else
  681. p := taicpu.op_sym(A_B,current_asmdata.RefAsmSymbol(s));
  682. p.is_jmp := true;
  683. list.concat(p)
  684. end;
  685. procedure tcgppc.a_jmp_always(list : TAsmList;l: tasmlabel);
  686. begin
  687. a_jmp(list,A_B,C_None,0,l);
  688. end;
  689. procedure tcgppc.a_jmp_flags(list : TAsmList;const f : TResFlags;l: tasmlabel);
  690. var
  691. c: tasmcond;
  692. begin
  693. c := flags_to_cond(f);
  694. a_jmp(list,A_BC,c.cond,c.cr-RS_CR0,l);
  695. end;
  696. procedure tcgppc.g_flags2reg(list: TAsmList; size: TCgSize; const f: TResFlags; reg: TRegister);
  697. var
  698. testbit: byte;
  699. bitvalue: boolean;
  700. begin
  701. { get the bit to extract from the conditional register + its }
  702. { requested value (0 or 1) }
  703. testbit := ((f.cr-RS_CR0) * 4);
  704. case f.flag of
  705. F_EQ,F_NE:
  706. begin
  707. inc(testbit,2);
  708. bitvalue := f.flag = F_EQ;
  709. end;
  710. F_LT,F_GE:
  711. begin
  712. bitvalue := f.flag = F_LT;
  713. end;
  714. F_GT,F_LE:
  715. begin
  716. inc(testbit);
  717. bitvalue := f.flag = F_GT;
  718. end;
  719. else
  720. internalerror(200112261);
  721. end;
  722. { load the conditional register in the destination reg }
  723. list.concat(taicpu.op_reg(A_MFCR,reg));
  724. { we will move the bit that has to be tested to bit 0 by rotating }
  725. { left }
  726. testbit := (testbit + 1) and 31;
  727. { extract bit }
  728. list.concat(taicpu.op_reg_reg_const_const_const(
  729. A_RLWINM,reg,reg,testbit,31,31));
  730. { if we need the inverse, xor with 1 }
  731. if not bitvalue then
  732. list.concat(taicpu.op_reg_reg_const(A_XORI,reg,reg,1));
  733. end;
  734. (*
  735. procedure tcgppc.g_cond2reg(list: TAsmList; const f: TAsmCond; reg: TRegister);
  736. var
  737. testbit: byte;
  738. bitvalue: boolean;
  739. begin
  740. { get the bit to extract from the conditional register + its }
  741. { requested value (0 or 1) }
  742. case f.simple of
  743. false:
  744. begin
  745. { we don't generate this in the compiler }
  746. internalerror(200109062);
  747. end;
  748. true:
  749. case f.cond of
  750. C_None:
  751. internalerror(200109063);
  752. C_LT..C_NU:
  753. begin
  754. testbit := (ord(f.cr) - ord(R_CR0))*4;
  755. inc(testbit,AsmCondFlag2BI[f.cond]);
  756. bitvalue := AsmCondFlagTF[f.cond];
  757. end;
  758. C_T,C_F,C_DNZT,C_DNZF,C_DZT,C_DZF:
  759. begin
  760. testbit := f.crbit
  761. bitvalue := AsmCondFlagTF[f.cond];
  762. end;
  763. else
  764. internalerror(200109064);
  765. end;
  766. end;
  767. { load the conditional register in the destination reg }
  768. list.concat(taicpu.op_reg_reg(A_MFCR,reg));
  769. { we will move the bit that has to be tested to bit 31 -> rotate }
  770. { left by bitpos+1 (remember, this is big-endian!) }
  771. if bitpos <> 31 then
  772. inc(bitpos)
  773. else
  774. bitpos := 0;
  775. { extract bit }
  776. list.concat(taicpu.op_reg_reg_const_const_const(
  777. A_RLWINM,reg,reg,bitpos,31,31));
  778. { if we need the inverse, xor with 1 }
  779. if not bitvalue then
  780. list.concat(taicpu.op_reg_reg_const(A_XORI,reg,reg,1));
  781. end;
  782. *)
  783. { *********** entry/exit code and address loading ************ }
  784. procedure tcgppc.g_save_standard_registers(list:TAsmList);
  785. begin
  786. { this work is done in g_proc_entry }
  787. end;
  788. procedure tcgppc.g_restore_standard_registers(list:TAsmList);
  789. begin
  790. { this work is done in g_proc_exit }
  791. end;
  792. procedure tcgppc.g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);
  793. { generated the entry code of a procedure/function. Note: localsize is the }
  794. { sum of the size necessary for local variables and the maximum possible }
  795. { combined size of ALL the parameters of a procedure called by the current }
  796. { one. }
  797. { This procedure may be called before, as well as after g_return_from_proc }
  798. { is called. NOTE registers are not to be allocated through the register }
  799. { allocator here, because the register colouring has already occured !! }
  800. var regcounter,firstregfpu,firstregint: TSuperRegister;
  801. href : treference;
  802. usesfpr,usesgpr,gotgot : boolean;
  803. cond : tasmcond;
  804. instr : taicpu;
  805. begin
  806. { CR and LR only have to be saved in case they are modified by the current }
  807. { procedure, but currently this isn't checked, so save them always }
  808. { following is the entry code as described in "Altivec Programming }
  809. { Interface Manual", bar the saving of AltiVec registers }
  810. a_reg_alloc(list,NR_STACK_POINTER_REG);
  811. usesgpr := false;
  812. usesfpr := false;
  813. if not(po_assembler in current_procinfo.procdef.procoptions) then
  814. begin
  815. { save link register? }
  816. if (pi_do_call in current_procinfo.flags) or
  817. ([cs_lineinfo,cs_debuginfo,cs_profile] * current_settings.moduleswitches <> []) then
  818. begin
  819. a_reg_alloc(list,NR_R0);
  820. { save return address... }
  821. { warning: if this is no longer done via r0, or if r0 is }
  822. { added to the usable registers, adapt tcgppcgen.g_profilecode }
  823. list.concat(taicpu.op_reg(A_MFLR,NR_R0));
  824. { ... in caller's frame }
  825. case target_info.abi of
  826. abi_powerpc_aix:
  827. reference_reset_base(href,NR_STACK_POINTER_REG,LA_LR_AIX);
  828. abi_powerpc_sysv:
  829. reference_reset_base(href,NR_STACK_POINTER_REG,LA_LR_SYSV);
  830. end;
  831. list.concat(taicpu.op_reg_ref(A_STW,NR_R0,href));
  832. if not(cs_profile in current_settings.moduleswitches) then
  833. a_reg_dealloc(list,NR_R0);
  834. end;
  835. (*
  836. { save the CR if necessary in callers frame. }
  837. if target_info.abi = abi_powerpc_aix then
  838. if false then { Not needed at the moment. }
  839. begin
  840. a_reg_alloc(list,NR_R0);
  841. list.concat(taicpu.op_reg_reg(A_MFSPR,NR_R0,NR_CR));
  842. reference_reset_base(href,NR_STACK_POINTER_REG,LA_CR_AIX);
  843. list.concat(taicpu.op_reg_ref(A_STW,NR_R0,href));
  844. a_reg_dealloc(list,NR_R0);
  845. end;
  846. *)
  847. firstregfpu := tppcprocinfo(current_procinfo).get_first_save_fpu_reg;
  848. firstregint := tppcprocinfo(current_procinfo).get_first_save_int_reg;
  849. usesgpr := firstregint <> 32;
  850. usesfpr := firstregfpu <> 32;
  851. if (tppcprocinfo(current_procinfo).needs_frame_pointer) then
  852. begin
  853. a_reg_alloc(list,NR_R12);
  854. list.concat(taicpu.op_reg_reg(A_MR,NR_R12,NR_STACK_POINTER_REG));
  855. end;
  856. end;
  857. { no GOT pointer loaded yet }
  858. gotgot:=false;
  859. if usesfpr then
  860. begin
  861. { save floating-point registers
  862. if (cs_create_pic in current_settings.moduleswitches) and not(usesgpr) then
  863. begin
  864. a_call_name(current_asmdata.RefAsmSymbol('_savefpr_'+tostr(ord(firstregfpu)-ord(R_F14)+14)+'_g'));
  865. gotgot:=true;
  866. end
  867. else
  868. a_call_name(current_asmdata.RefAsmSymbol('_savefpr_'+tostr(ord(firstregfpu)-ord(R_F14)+14)));
  869. }
  870. reference_reset_base(href,NR_R1,-8);
  871. for regcounter:=firstregfpu to RS_F31 do
  872. begin
  873. a_loadfpu_reg_ref(list,OS_F64,OS_F64,newreg(R_FPUREGISTER,regcounter,R_SUBNONE),href);
  874. dec(href.offset,8);
  875. end;
  876. { compute start of gpr save area }
  877. inc(href.offset,4);
  878. end
  879. else
  880. { compute start of gpr save area }
  881. reference_reset_base(href,NR_R1,-4);
  882. { save gprs and fetch GOT pointer }
  883. if usesgpr then
  884. begin
  885. {
  886. if cs_create_pic in current_settings.moduleswitches then
  887. begin
  888. a_call_name(current_asmdata.RefAsmSymbol('_savegpr_'+tostr(ord(firstreggpr)-ord(R_14)+14)+'_g'));
  889. gotgot:=true;
  890. end
  891. else
  892. a_call_name(current_asmdata.RefAsmSymbol('_savegpr_'+tostr(ord(firstreggpr)-ord(R_14)+14)))
  893. }
  894. if (firstregint <= RS_R22) or
  895. ((cs_opt_size in current_settings.optimizerswitches) and
  896. { with RS_R30 it's also already smaller, but too big a speed trade-off to make }
  897. (firstregint <= RS_R29)) then
  898. begin
  899. dec(href.offset,(RS_R31-firstregint)*sizeof(aint));
  900. list.concat(taicpu.op_reg_ref(A_STMW,newreg(R_INTREGISTER,firstregint,R_SUBNONE),href));
  901. end
  902. else
  903. for regcounter:=firstregint to RS_R31 do
  904. begin
  905. a_load_reg_ref(list,OS_INT,OS_INT,newreg(R_INTREGISTER,regcounter,R_SUBNONE),href);
  906. dec(href.offset,4);
  907. end;
  908. end;
  909. { done in ncgutil because it may only be released after the parameters }
  910. { have been moved to their final resting place }
  911. { if (tppcprocinfo(current_procinfo).needs_frame_pointer) then }
  912. { a_reg_dealloc(list,NR_R12); }
  913. { if we didn't get the GOT pointer till now, we've to calculate it now }
  914. (*
  915. if not(gotgot) and (pi_needs_got in current_procinfo.flags) then
  916. case target_info.system of
  917. system_powerpc_darwin:
  918. begin
  919. list.concat(taicpu.op_reg_reg(A_MFSPR,NR_R0,NR_LR));
  920. fillchar(cond,sizeof(cond),0);
  921. cond.simple:=false;
  922. cond.bo:=20;
  923. cond.bi:=31;
  924. instr:=taicpu.op_sym(A_BCL,current_procinfo.CurrGOTLabel);
  925. instr.setcondition(cond);
  926. list.concat(instr);
  927. a_label(list,current_procinfo.CurrGOTLabel);
  928. list.concat(taicpu.op_reg_reg(A_MFSPR,current_procinfo.got,NR_LR));
  929. list.concat(taicpu.op_reg_reg(A_MTSPR,NR_LR,NR_R0));
  930. end;
  931. else
  932. begin
  933. a_reg_alloc(list,NR_R31);
  934. { place GOT ptr in r31 }
  935. list.concat(taicpu.op_reg_reg(A_MFSPR,NR_R31,NR_LR));
  936. end;
  937. end;
  938. *)
  939. if (not nostackframe) and
  940. (localsize <> 0) then
  941. begin
  942. if (localsize <= high(smallint)) then
  943. begin
  944. reference_reset_base(href,NR_STACK_POINTER_REG,-localsize);
  945. a_load_store(list,A_STWU,NR_STACK_POINTER_REG,href);
  946. end
  947. else
  948. begin
  949. reference_reset_base(href,NR_STACK_POINTER_REG,0);
  950. { can't use getregisterint here, the register colouring }
  951. { is already done when we get here }
  952. href.index := NR_R11;
  953. a_reg_alloc(list,href.index);
  954. a_load_const_reg(list,OS_S32,-localsize,href.index);
  955. a_load_store(list,A_STWUX,NR_STACK_POINTER_REG,href);
  956. a_reg_dealloc(list,href.index);
  957. end;
  958. end;
  959. { save the CR if necessary ( !!! never done currently ) }
  960. { still need to find out where this has to be done for SystemV
  961. a_reg_alloc(list,R_0);
  962. list.concat(taicpu.op_reg_reg(A_MFSPR,R_0,R_CR);
  963. list.concat(taicpu.op_reg_ref(A_STW,scratch_register,
  964. new_reference(STACK_POINTER_REG,LA_CR)));
  965. a_reg_dealloc(list,R_0);
  966. }
  967. { now comes the AltiVec context save, not yet implemented !!! }
  968. end;
  969. procedure tcgppc.g_proc_exit(list : TAsmList;parasize : longint;nostackframe:boolean);
  970. { This procedure may be called before, as well as after g_stackframe_entry }
  971. { is called. NOTE registers are not to be allocated through the register }
  972. { allocator here, because the register colouring has already occured !! }
  973. var
  974. regcounter,firstregfpu,firstregint: TsuperRegister;
  975. href : treference;
  976. usesfpr,usesgpr,genret : boolean;
  977. localsize: aint;
  978. begin
  979. { AltiVec context restore, not yet implemented !!! }
  980. usesfpr:=false;
  981. usesgpr:=false;
  982. if not (po_assembler in current_procinfo.procdef.procoptions) then
  983. begin
  984. firstregfpu := tppcprocinfo(current_procinfo).get_first_save_fpu_reg;
  985. firstregint := tppcprocinfo(current_procinfo).get_first_save_int_reg;
  986. usesgpr := firstregint <> 32;
  987. usesfpr := firstregfpu <> 32;
  988. end;
  989. localsize:= tppcprocinfo(current_procinfo).calc_stackframe_size;
  990. { adjust r1 }
  991. { (register allocator is no longer valid at this time and an add of 0 }
  992. { is translated into a move, which is then registered with the register }
  993. { allocator, causing a crash }
  994. if (not nostackframe) and
  995. (localsize <> 0) then
  996. a_op_const_reg(list,OP_ADD,OS_ADDR,localsize,NR_R1);
  997. { no return (blr) generated yet }
  998. genret:=true;
  999. if usesfpr then
  1000. begin
  1001. reference_reset_base(href,NR_R1,-8);
  1002. for regcounter := firstregfpu to RS_F31 do
  1003. begin
  1004. a_loadfpu_ref_reg(list,OS_F64,OS_F64,href,newreg(R_FPUREGISTER,regcounter,R_SUBNONE));
  1005. dec(href.offset,8);
  1006. end;
  1007. inc(href.offset,4);
  1008. end
  1009. else
  1010. reference_reset_base(href,NR_R1,-4);
  1011. if (usesgpr) then
  1012. begin
  1013. if (firstregint <= RS_R22) or
  1014. ((cs_opt_size in current_settings.optimizerswitches) and
  1015. { with RS_R30 it's also already smaller, but too big a speed trade-off to make }
  1016. (firstregint <= RS_R29)) then
  1017. begin
  1018. dec(href.offset,(RS_R31-firstregint)*sizeof(aint));
  1019. list.concat(taicpu.op_reg_ref(A_LMW,newreg(R_INTREGISTER,firstregint,R_SUBNONE),href));
  1020. end
  1021. else
  1022. for regcounter:=firstregint to RS_R31 do
  1023. begin
  1024. a_load_ref_reg(list,OS_INT,OS_INT,href,newreg(R_INTREGISTER,regcounter,R_SUBNONE));
  1025. dec(href.offset,4);
  1026. end;
  1027. end;
  1028. (*
  1029. { restore fprs and return }
  1030. if usesfpr then
  1031. begin
  1032. { address of fpr save area to r11 }
  1033. r:=NR_R12;
  1034. list.concat(taicpu.op_reg_reg_const(A_ADDI,r,r,(ord(R_F31)-ord(firstregfpu.enum)+1)*8));
  1035. {
  1036. if (pi_do_call in current_procinfo.flags) then
  1037. a_call_name(current_asmdata.RefAsmSymbol('_restfpr_'+tostr(ord(firstregfpu)-ord(R_F14)+14)+'_x'))
  1038. else
  1039. { leaf node => lr haven't to be restored }
  1040. a_call_name('_restfpr_'+tostr(ord(firstregfpu.enum)-ord(R_F14)+14)+'_l');
  1041. genret:=false;
  1042. }
  1043. end;
  1044. *)
  1045. { if we didn't generate the return code, we've to do it now }
  1046. if genret then
  1047. begin
  1048. { load link register? }
  1049. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1050. begin
  1051. if (pi_do_call in current_procinfo.flags) then
  1052. begin
  1053. case target_info.abi of
  1054. abi_powerpc_aix:
  1055. reference_reset_base(href,NR_STACK_POINTER_REG,LA_LR_AIX);
  1056. abi_powerpc_sysv:
  1057. reference_reset_base(href,NR_STACK_POINTER_REG,LA_LR_SYSV);
  1058. end;
  1059. list.concat(taicpu.op_reg_ref(A_LWZ,NR_R0,href));
  1060. list.concat(taicpu.op_reg(A_MTLR,NR_R0));
  1061. end;
  1062. (*
  1063. { restore the CR if necessary from callers frame}
  1064. if target_info.abi = abi_powerpc_aix then
  1065. if false then { Not needed at the moment. }
  1066. begin
  1067. reference_reset_base(href,NR_STACK_POINTER_REG,LA_CR_AIX);
  1068. list.concat(taicpu.op_reg_ref(A_LWZ,NR_R0,href));
  1069. list.concat(taicpu.op_reg_reg(A_MTSPR,NR_R0,NR_CR));
  1070. a_reg_dealloc(list,NR_R0);
  1071. end;
  1072. *)
  1073. end;
  1074. list.concat(taicpu.op_none(A_BLR));
  1075. end;
  1076. end;
  1077. function tcgppc.save_regs(list : TAsmList):longint;
  1078. {Generates code which saves used non-volatile registers in
  1079. the save area right below the address the stackpointer point to.
  1080. Returns the actual used save area size.}
  1081. var regcounter,firstregfpu,firstreggpr: TSuperRegister;
  1082. usesfpr,usesgpr: boolean;
  1083. href : treference;
  1084. offset: aint;
  1085. regcounter2, firstfpureg: Tsuperregister;
  1086. begin
  1087. usesfpr:=false;
  1088. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1089. begin
  1090. { FIXME: has to be R_F14 instad of R_F8 for SYSV-64bit }
  1091. case target_info.abi of
  1092. abi_powerpc_aix:
  1093. firstfpureg := RS_F14;
  1094. abi_powerpc_sysv:
  1095. firstfpureg := RS_F9;
  1096. else
  1097. internalerror(2003122903);
  1098. end;
  1099. for regcounter:=firstfpureg to RS_F31 do
  1100. begin
  1101. if regcounter in rg[R_FPUREGISTER].used_in_proc then
  1102. begin
  1103. usesfpr:=true;
  1104. firstregfpu:=regcounter;
  1105. break;
  1106. end;
  1107. end;
  1108. end;
  1109. usesgpr:=false;
  1110. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1111. for regcounter2:=RS_R13 to RS_R31 do
  1112. begin
  1113. if regcounter2 in rg[R_INTREGISTER].used_in_proc then
  1114. begin
  1115. usesgpr:=true;
  1116. firstreggpr:=regcounter2;
  1117. break;
  1118. end;
  1119. end;
  1120. offset:= 0;
  1121. { save floating-point registers }
  1122. if usesfpr then
  1123. for regcounter := firstregfpu to RS_F31 do
  1124. begin
  1125. offset:= offset - 8;
  1126. reference_reset_base(href, NR_STACK_POINTER_REG, offset);
  1127. list.concat(taicpu.op_reg_ref(A_STFD, tregister(regcounter), href));
  1128. end;
  1129. (* Optimiztion in the future: a_call_name(list,'_savefXX'); *)
  1130. { save gprs in gpr save area }
  1131. if usesgpr then
  1132. if firstreggpr < RS_R30 then
  1133. begin
  1134. offset:= offset - 4 * (RS_R31 - firstreggpr + 1);
  1135. reference_reset_base(href,NR_STACK_POINTER_REG,offset);
  1136. list.concat(taicpu.op_reg_ref(A_STMW,tregister(firstreggpr),href));
  1137. {STMW stores multiple registers}
  1138. end
  1139. else
  1140. begin
  1141. for regcounter := firstreggpr to RS_R31 do
  1142. begin
  1143. offset:= offset - 4;
  1144. reference_reset_base(href, NR_STACK_POINTER_REG, offset);
  1145. list.concat(taicpu.op_reg_ref(A_STW, newreg(R_INTREGISTER,regcounter,R_SUBWHOLE), href));
  1146. end;
  1147. end;
  1148. { now comes the AltiVec context save, not yet implemented !!! }
  1149. save_regs:= -offset;
  1150. end;
  1151. procedure tcgppc.restore_regs(list : TAsmList);
  1152. {Generates code which restores used non-volatile registers from
  1153. the save area right below the address the stackpointer point to.}
  1154. var regcounter,firstregfpu,firstreggpr: TSuperRegister;
  1155. usesfpr,usesgpr: boolean;
  1156. href : treference;
  1157. offset: integer;
  1158. regcounter2, firstfpureg: Tsuperregister;
  1159. begin
  1160. usesfpr:=false;
  1161. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1162. begin
  1163. { FIXME: has to be R_F14 instad of R_F8 for SYSV-64bit }
  1164. case target_info.abi of
  1165. abi_powerpc_aix:
  1166. firstfpureg := RS_F14;
  1167. abi_powerpc_sysv:
  1168. firstfpureg := RS_F9;
  1169. else
  1170. internalerror(2003122903);
  1171. end;
  1172. for regcounter:=firstfpureg to RS_F31 do
  1173. begin
  1174. if regcounter in rg[R_FPUREGISTER].used_in_proc then
  1175. begin
  1176. usesfpr:=true;
  1177. firstregfpu:=regcounter;
  1178. break;
  1179. end;
  1180. end;
  1181. end;
  1182. usesgpr:=false;
  1183. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1184. for regcounter2:=RS_R13 to RS_R31 do
  1185. begin
  1186. if regcounter2 in rg[R_INTREGISTER].used_in_proc then
  1187. begin
  1188. usesgpr:=true;
  1189. firstreggpr:=regcounter2;
  1190. break;
  1191. end;
  1192. end;
  1193. offset:= 0;
  1194. { restore fp registers }
  1195. if usesfpr then
  1196. for regcounter := firstregfpu to RS_F31 do
  1197. begin
  1198. offset:= offset - 8;
  1199. reference_reset_base(href, NR_STACK_POINTER_REG, offset);
  1200. list.concat(taicpu.op_reg_ref(A_LFD, newreg(R_FPUREGISTER,regcounter,R_SUBWHOLE), href));
  1201. end;
  1202. (* Optimiztion in the future: a_call_name(list,'_restfXX'); *)
  1203. { restore gprs }
  1204. if usesgpr then
  1205. if firstreggpr < RS_R30 then
  1206. begin
  1207. offset:= offset - 4 * (RS_R31 - firstreggpr + 1);
  1208. reference_reset_base(href,NR_STACK_POINTER_REG,offset); //-220
  1209. list.concat(taicpu.op_reg_ref(A_LMW,tregister(firstreggpr),href));
  1210. {LMW loads multiple registers}
  1211. end
  1212. else
  1213. begin
  1214. for regcounter := firstreggpr to RS_R31 do
  1215. begin
  1216. offset:= offset - 4;
  1217. reference_reset_base(href, NR_STACK_POINTER_REG, offset);
  1218. list.concat(taicpu.op_reg_ref(A_LWZ, newreg(R_INTREGISTER,regcounter,R_SUBWHOLE), href));
  1219. end;
  1220. end;
  1221. { now comes the AltiVec context restore, not yet implemented !!! }
  1222. end;
  1223. procedure tcgppc.g_stackframe_entry_mac(list : TAsmList;localsize : longint);
  1224. (* NOT IN USE *)
  1225. { generated the entry code of a procedure/function. Note: localsize is the }
  1226. { sum of the size necessary for local variables and the maximum possible }
  1227. { combined size of ALL the parameters of a procedure called by the current }
  1228. { one }
  1229. const
  1230. macosLinkageAreaSize = 24;
  1231. var
  1232. href : treference;
  1233. registerSaveAreaSize : longint;
  1234. begin
  1235. if (localsize mod 8) <> 0 then
  1236. internalerror(58991);
  1237. { CR and LR only have to be saved in case they are modified by the current }
  1238. { procedure, but currently this isn't checked, so save them always }
  1239. { following is the entry code as described in "Altivec Programming }
  1240. { Interface Manual", bar the saving of AltiVec registers }
  1241. a_reg_alloc(list,NR_STACK_POINTER_REG);
  1242. a_reg_alloc(list,NR_R0);
  1243. { save return address in callers frame}
  1244. list.concat(taicpu.op_reg_reg(A_MFSPR,NR_R0,NR_LR));
  1245. { ... in caller's frame }
  1246. reference_reset_base(href,NR_STACK_POINTER_REG,8);
  1247. list.concat(taicpu.op_reg_ref(A_STW,NR_R0,href));
  1248. a_reg_dealloc(list,NR_R0);
  1249. { save non-volatile registers in callers frame}
  1250. registerSaveAreaSize:= save_regs(list);
  1251. { save the CR if necessary in callers frame ( !!! always done currently ) }
  1252. a_reg_alloc(list,NR_R0);
  1253. list.concat(taicpu.op_reg_reg(A_MFSPR,NR_R0,NR_CR));
  1254. reference_reset_base(href,NR_STACK_POINTER_REG,LA_CR_AIX);
  1255. list.concat(taicpu.op_reg_ref(A_STW,NR_R0,href));
  1256. a_reg_dealloc(list,NR_R0);
  1257. (*
  1258. { save pointer to incoming arguments }
  1259. list.concat(taicpu.op_reg_reg_const(A_ORI,R_31,STACK_POINTER_REG,0));
  1260. *)
  1261. (*
  1262. a_reg_alloc(list,R_12);
  1263. { 0 or 8 based on SP alignment }
  1264. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWINM,
  1265. R_12,STACK_POINTER_REG,0,28,28));
  1266. { add in stack length }
  1267. list.concat(taicpu.op_reg_reg_const(A_SUBFIC,R_12,R_12,
  1268. -localsize));
  1269. { establish new alignment }
  1270. list.concat(taicpu.op_reg_reg_reg(A_STWUX,STACK_POINTER_REG,STACK_POINTER_REG,R_12));
  1271. a_reg_dealloc(list,R_12);
  1272. *)
  1273. { allocate stack frame }
  1274. localsize:= align(localsize + macosLinkageAreaSize + registerSaveAreaSize, 16);
  1275. inc(localsize,tg.lasttemp);
  1276. localsize:=align(localsize,16);
  1277. //tppcprocinfo(current_procinfo).localsize:=localsize;
  1278. if (localsize <> 0) then
  1279. begin
  1280. if (localsize <= high(smallint)) then
  1281. begin
  1282. reference_reset_base(href,NR_STACK_POINTER_REG,-localsize);
  1283. a_load_store(list,A_STWU,NR_STACK_POINTER_REG,href);
  1284. end
  1285. else
  1286. begin
  1287. reference_reset_base(href,NR_STACK_POINTER_REG,0);
  1288. href.index := NR_R11;
  1289. a_reg_alloc(list,href.index);
  1290. a_load_const_reg(list,OS_S32,-localsize,href.index);
  1291. a_load_store(list,A_STWUX,NR_STACK_POINTER_REG,href);
  1292. a_reg_dealloc(list,href.index);
  1293. end;
  1294. end;
  1295. end;
  1296. procedure tcgppc.g_return_from_proc_mac(list : TAsmList;parasize : aint);
  1297. (* NOT IN USE *)
  1298. var
  1299. href : treference;
  1300. begin
  1301. a_reg_alloc(list,NR_R0);
  1302. { restore stack pointer }
  1303. reference_reset_base(href,NR_STACK_POINTER_REG,LA_SP);
  1304. list.concat(taicpu.op_reg_ref(A_LWZ,NR_STACK_POINTER_REG,href));
  1305. (*
  1306. list.concat(taicpu.op_reg_reg_const(A_ORI,NR_STACK_POINTER_REG,R_31,0));
  1307. *)
  1308. { restore the CR if necessary from callers frame
  1309. ( !!! always done currently ) }
  1310. reference_reset_base(href,NR_STACK_POINTER_REG,LA_CR_AIX);
  1311. list.concat(taicpu.op_reg_ref(A_LWZ,NR_R0,href));
  1312. list.concat(taicpu.op_reg_reg(A_MTSPR,NR_R0,NR_CR));
  1313. a_reg_dealloc(list,NR_R0);
  1314. (*
  1315. { restore return address from callers frame }
  1316. reference_reset_base(href,STACK_POINTER_REG,8);
  1317. list.concat(taicpu.op_reg_ref(A_LWZ,R_0,href));
  1318. *)
  1319. { restore non-volatile registers from callers frame }
  1320. restore_regs(list);
  1321. (*
  1322. { return to caller }
  1323. list.concat(taicpu.op_reg_reg(A_MTSPR,R_0,R_LR));
  1324. list.concat(taicpu.op_none(A_BLR));
  1325. *)
  1326. { restore return address from callers frame }
  1327. reference_reset_base(href,NR_STACK_POINTER_REG,8);
  1328. list.concat(taicpu.op_reg_ref(A_LWZ,NR_R0,href));
  1329. { return to caller }
  1330. list.concat(taicpu.op_reg_reg(A_MTSPR,NR_R0,NR_LR));
  1331. list.concat(taicpu.op_none(A_BLR));
  1332. end;
  1333. procedure tcgppc.a_loadaddr_ref_reg(list : TAsmList;const ref : treference;r : tregister);
  1334. var
  1335. ref2, tmpref: treference;
  1336. begin
  1337. ref2 := ref;
  1338. fixref(list,ref2);
  1339. if assigned(ref2.symbol) then
  1340. begin
  1341. if target_info.system = system_powerpc_macos then
  1342. begin
  1343. if macos_direct_globals then
  1344. begin
  1345. reference_reset(tmpref);
  1346. tmpref.offset := ref2.offset;
  1347. tmpref.symbol := ref2.symbol;
  1348. tmpref.base := NR_NO;
  1349. list.concat(taicpu.op_reg_reg_ref(A_ADDI,r,NR_RTOC,tmpref));
  1350. end
  1351. else
  1352. begin
  1353. reference_reset(tmpref);
  1354. tmpref.symbol := ref2.symbol;
  1355. tmpref.offset := 0;
  1356. tmpref.base := NR_RTOC;
  1357. list.concat(taicpu.op_reg_ref(A_LWZ,r,tmpref));
  1358. if ref2.offset <> 0 then
  1359. begin
  1360. reference_reset(tmpref);
  1361. tmpref.offset := ref2.offset;
  1362. tmpref.base:= r;
  1363. list.concat(taicpu.op_reg_ref(A_LA,r,tmpref));
  1364. end;
  1365. end;
  1366. if ref2.base <> NR_NO then
  1367. list.concat(taicpu.op_reg_reg_reg(A_ADD,r,r,ref2.base));
  1368. //list.concat(tai_comment.create(strpnew('*** a_loadaddr_ref_reg')));
  1369. end
  1370. else
  1371. begin
  1372. { add the symbol's value to the base of the reference, and if the }
  1373. { reference doesn't have a base, create one }
  1374. reference_reset(tmpref);
  1375. tmpref.offset := ref2.offset;
  1376. tmpref.symbol := ref2.symbol;
  1377. tmpref.relsymbol := ref2.relsymbol;
  1378. tmpref.refaddr := addr_hi;
  1379. if ref2.base<> NR_NO then
  1380. begin
  1381. list.concat(taicpu.op_reg_reg_ref(A_ADDIS,r,
  1382. ref2.base,tmpref));
  1383. end
  1384. else
  1385. list.concat(taicpu.op_reg_ref(A_LIS,r,tmpref));
  1386. tmpref.base := NR_NO;
  1387. tmpref.refaddr := addr_lo;
  1388. { can be folded with one of the next instructions by the }
  1389. { optimizer probably }
  1390. list.concat(taicpu.op_reg_reg_ref(A_ADDI,r,r,tmpref));
  1391. end
  1392. end
  1393. else if ref2.offset <> 0 Then
  1394. if ref2.base <> NR_NO then
  1395. a_op_const_reg_reg(list,OP_ADD,OS_32,ref2.offset,ref2.base,r)
  1396. { FixRef makes sure that "(ref.index <> R_NO) and (ref.offset <> 0)" never}
  1397. { occurs, so now only ref.offset has to be loaded }
  1398. else
  1399. a_load_const_reg(list,OS_32,ref2.offset,r)
  1400. else if ref2.index <> NR_NO Then
  1401. list.concat(taicpu.op_reg_reg_reg(A_ADD,r,ref2.base,ref2.index))
  1402. else if (ref2.base <> NR_NO) and
  1403. (r <> ref2.base) then
  1404. a_load_reg_reg(list,OS_ADDR,OS_ADDR,ref2.base,r)
  1405. else
  1406. list.concat(taicpu.op_reg_const(A_LI,r,0));
  1407. end;
  1408. { ************* concatcopy ************ }
  1409. {$ifndef ppc603}
  1410. const
  1411. maxmoveunit = 8;
  1412. {$else ppc603}
  1413. const
  1414. maxmoveunit = 4;
  1415. {$endif ppc603}
  1416. procedure tcgppc.g_concatcopy(list : TAsmList;const source,dest : treference;len : aint);
  1417. var
  1418. countreg: TRegister;
  1419. src, dst: TReference;
  1420. lab: tasmlabel;
  1421. count, count2: aint;
  1422. size: tcgsize;
  1423. copyreg: tregister;
  1424. begin
  1425. {$ifdef extdebug}
  1426. if len > high(longint) then
  1427. internalerror(2002072704);
  1428. {$endif extdebug}
  1429. if (references_equal(source,dest)) then
  1430. exit;
  1431. { make sure short loads are handled as optimally as possible }
  1432. if (len <= maxmoveunit) and
  1433. (byte(len) in [1,2,4,8]) then
  1434. begin
  1435. if len < 8 then
  1436. begin
  1437. size := int_cgsize(len);
  1438. a_load_ref_ref(list,size,size,source,dest);
  1439. end
  1440. else
  1441. begin
  1442. copyreg := getfpuregister(list,OS_F64);
  1443. a_loadfpu_ref_reg(list,OS_F64,OS_F64,source,copyreg);
  1444. a_loadfpu_reg_ref(list,OS_F64,OS_F64,copyreg,dest);
  1445. end;
  1446. exit;
  1447. end;
  1448. count := len div maxmoveunit;
  1449. reference_reset(src);
  1450. reference_reset(dst);
  1451. { load the address of source into src.base }
  1452. if (count > 4) or
  1453. not issimpleref(source) or
  1454. ((source.index <> NR_NO) and
  1455. ((source.offset + longint(len)) > high(smallint))) then
  1456. begin
  1457. src.base := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1458. a_loadaddr_ref_reg(list,source,src.base);
  1459. end
  1460. else
  1461. begin
  1462. src := source;
  1463. end;
  1464. { load the address of dest into dst.base }
  1465. if (count > 4) or
  1466. not issimpleref(dest) or
  1467. ((dest.index <> NR_NO) and
  1468. ((dest.offset + longint(len)) > high(smallint))) then
  1469. begin
  1470. dst.base := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1471. a_loadaddr_ref_reg(list,dest,dst.base);
  1472. end
  1473. else
  1474. begin
  1475. dst := dest;
  1476. end;
  1477. {$ifndef ppc603}
  1478. if count > 4 then
  1479. { generate a loop }
  1480. begin
  1481. { the offsets are zero after the a_loadaddress_ref_reg and just }
  1482. { have to be set to 8. I put an Inc there so debugging may be }
  1483. { easier (should offset be different from zero here, it will be }
  1484. { easy to notice in the generated assembler }
  1485. inc(dst.offset,8);
  1486. inc(src.offset,8);
  1487. list.concat(taicpu.op_reg_reg_const(A_SUBI,src.base,src.base,8));
  1488. list.concat(taicpu.op_reg_reg_const(A_SUBI,dst.base,dst.base,8));
  1489. countreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1490. a_load_const_reg(list,OS_32,count,countreg);
  1491. copyreg := getfpuregister(list,OS_F64);
  1492. a_reg_sync(list,copyreg);
  1493. current_asmdata.getjumplabel(lab);
  1494. a_label(list, lab);
  1495. list.concat(taicpu.op_reg_reg_const(A_SUBIC_,countreg,countreg,1));
  1496. list.concat(taicpu.op_reg_ref(A_LFDU,copyreg,src));
  1497. list.concat(taicpu.op_reg_ref(A_STFDU,copyreg,dst));
  1498. a_jmp(list,A_BC,C_NE,0,lab);
  1499. a_reg_sync(list,copyreg);
  1500. len := len mod 8;
  1501. end;
  1502. count := len div 8;
  1503. if count > 0 then
  1504. { unrolled loop }
  1505. begin
  1506. copyreg := getfpuregister(list,OS_F64);
  1507. for count2 := 1 to count do
  1508. begin
  1509. a_loadfpu_ref_reg(list,OS_F64,OS_F64,src,copyreg);
  1510. a_loadfpu_reg_ref(list,OS_F64,OS_F64,copyreg,dst);
  1511. inc(src.offset,8);
  1512. inc(dst.offset,8);
  1513. end;
  1514. len := len mod 8;
  1515. end;
  1516. if (len and 4) <> 0 then
  1517. begin
  1518. a_reg_alloc(list,NR_R0);
  1519. a_load_ref_reg(list,OS_32,OS_32,src,NR_R0);
  1520. a_load_reg_ref(list,OS_32,OS_32,NR_R0,dst);
  1521. inc(src.offset,4);
  1522. inc(dst.offset,4);
  1523. a_reg_dealloc(list,NR_R0);
  1524. end;
  1525. {$else not ppc603}
  1526. if count > 4 then
  1527. { generate a loop }
  1528. begin
  1529. { the offsets are zero after the a_loadaddress_ref_reg and just }
  1530. { have to be set to 4. I put an Inc there so debugging may be }
  1531. { easier (should offset be different from zero here, it will be }
  1532. { easy to notice in the generated assembler }
  1533. inc(dst.offset,4);
  1534. inc(src.offset,4);
  1535. list.concat(taicpu.op_reg_reg_const(A_SUBI,src.base,src.base,4));
  1536. list.concat(taicpu.op_reg_reg_const(A_SUBI,dst.base,dst.base,4));
  1537. countreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1538. a_load_const_reg(list,OS_32,count,countreg);
  1539. { explicitely allocate R_0 since it can be used safely here }
  1540. { (for holding date that's being copied) }
  1541. a_reg_alloc(list,NR_R0);
  1542. current_asmdata.getjumplabel(lab);
  1543. a_label(list, lab);
  1544. list.concat(taicpu.op_reg_reg_const(A_SUBIC_,countreg,countreg,1));
  1545. list.concat(taicpu.op_reg_ref(A_LWZU,NR_R0,src));
  1546. list.concat(taicpu.op_reg_ref(A_STWU,NR_R0,dst));
  1547. a_jmp(list,A_BC,C_NE,0,lab);
  1548. a_reg_dealloc(list,NR_R0);
  1549. len := len mod 4;
  1550. end;
  1551. count := len div 4;
  1552. if count > 0 then
  1553. { unrolled loop }
  1554. begin
  1555. a_reg_alloc(list,NR_R0);
  1556. for count2 := 1 to count do
  1557. begin
  1558. a_load_ref_reg(list,OS_32,OS_32,src,NR_R0);
  1559. a_load_reg_ref(list,OS_32,OS_32,NR_R0,dst);
  1560. inc(src.offset,4);
  1561. inc(dst.offset,4);
  1562. end;
  1563. a_reg_dealloc(list,NR_R0);
  1564. len := len mod 4;
  1565. end;
  1566. {$endif not ppc603}
  1567. { copy the leftovers }
  1568. if (len and 2) <> 0 then
  1569. begin
  1570. a_reg_alloc(list,NR_R0);
  1571. a_load_ref_reg(list,OS_16,OS_16,src,NR_R0);
  1572. a_load_reg_ref(list,OS_16,OS_16,NR_R0,dst);
  1573. inc(src.offset,2);
  1574. inc(dst.offset,2);
  1575. a_reg_dealloc(list,NR_R0);
  1576. end;
  1577. if (len and 1) <> 0 then
  1578. begin
  1579. a_reg_alloc(list,NR_R0);
  1580. a_load_ref_reg(list,OS_8,OS_8,src,NR_R0);
  1581. a_load_reg_ref(list,OS_8,OS_8,NR_R0,dst);
  1582. a_reg_dealloc(list,NR_R0);
  1583. end;
  1584. end;
  1585. procedure tcgppc.g_overflowcheck(list: TAsmList; const l: tlocation; def: tdef);
  1586. var
  1587. hl : tasmlabel;
  1588. begin
  1589. if not(cs_check_overflow in current_settings.localswitches) then
  1590. exit;
  1591. current_asmdata.getjumplabel(hl);
  1592. if not ((def.typ=pointerdef) or
  1593. ((def.typ=orddef) and
  1594. (torddef(def).ordtype in [u64bit,u16bit,u32bit,u8bit,uchar,
  1595. bool8bit,bool16bit,bool32bit,bool64bit]))) then
  1596. begin
  1597. list.concat(taicpu.op_reg(A_MCRXR,NR_CR7));
  1598. a_jmp(list,A_BC,C_NO,7,hl)
  1599. end
  1600. else
  1601. a_jmp_cond(list,OC_AE,hl);
  1602. a_call_name(list,'FPC_OVERFLOW');
  1603. a_label(list,hl);
  1604. end;
  1605. procedure tcgppc.g_intf_wrapper(list: TAsmList; procdef: tprocdef; const labelname: string; ioffset: longint);
  1606. procedure loadvmttor11;
  1607. var
  1608. href : treference;
  1609. begin
  1610. reference_reset_base(href,NR_R3,0);
  1611. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,NR_R11);
  1612. end;
  1613. procedure op_onr11methodaddr;
  1614. var
  1615. href : treference;
  1616. begin
  1617. if (procdef.extnumber=$ffff) then
  1618. Internalerror(200006139);
  1619. { call/jmp vmtoffs(%eax) ; method offs }
  1620. reference_reset_base(href,NR_R11,procdef._class.vmtmethodoffset(procdef.extnumber));
  1621. if not((longint(href.offset) >= low(smallint)) and
  1622. (longint(href.offset) <= high(smallint))) then
  1623. begin
  1624. list.concat(taicpu.op_reg_reg_const(A_ADDIS,NR_R11,NR_R11,
  1625. smallint((href.offset shr 16)+ord(smallint(href.offset and $ffff) < 0))));
  1626. href.offset := smallint(href.offset and $ffff);
  1627. end;
  1628. list.concat(taicpu.op_reg_ref(A_LWZ,NR_R11,href));
  1629. list.concat(taicpu.op_reg(A_MTCTR,NR_R11));
  1630. list.concat(taicpu.op_none(A_BCTR));
  1631. end;
  1632. var
  1633. make_global : boolean;
  1634. begin
  1635. if not(procdef.proctypeoption in [potype_function,potype_procedure]) then
  1636. Internalerror(200006137);
  1637. if not assigned(procdef._class) or
  1638. (procdef.procoptions*[po_classmethod, po_staticmethod,
  1639. po_methodpointer, po_interrupt, po_iocheck]<>[]) then
  1640. Internalerror(200006138);
  1641. if procdef.owner.symtabletype<>ObjectSymtable then
  1642. Internalerror(200109191);
  1643. make_global:=false;
  1644. if (not current_module.is_unit) or
  1645. (cs_create_smart in current_settings.moduleswitches) or
  1646. (procdef.owner.defowner.owner.symtabletype=globalsymtable) then
  1647. make_global:=true;
  1648. if make_global then
  1649. List.concat(Tai_symbol.Createname_global(labelname,AT_FUNCTION,0))
  1650. else
  1651. List.concat(Tai_symbol.Createname(labelname,AT_FUNCTION,0));
  1652. { set param1 interface to self }
  1653. g_adjust_self_value(list,procdef,ioffset);
  1654. { case 4 }
  1655. if po_virtualmethod in procdef.procoptions then
  1656. begin
  1657. loadvmttor11;
  1658. op_onr11methodaddr;
  1659. end
  1660. { case 0 }
  1661. else
  1662. if not(target_info.system = system_powerpc_darwin) then
  1663. list.concat(taicpu.op_sym(A_B,current_asmdata.RefAsmSymbol(procdef.mangledname)))
  1664. else
  1665. list.concat(taicpu.op_sym(A_B,get_darwin_call_stub(procdef.mangledname)));
  1666. List.concat(Tai_symbol_end.Createname(labelname));
  1667. end;
  1668. {***************** This is private property, keep out! :) *****************}
  1669. function tcgppc.issimpleref(const ref: treference): boolean;
  1670. begin
  1671. if (ref.base = NR_NO) and
  1672. (ref.index <> NR_NO) then
  1673. internalerror(200208101);
  1674. result :=
  1675. not(assigned(ref.symbol)) and
  1676. (((ref.index = NR_NO) and
  1677. (ref.offset >= low(smallint)) and
  1678. (ref.offset <= high(smallint))) or
  1679. ((ref.index <> NR_NO) and
  1680. (ref.offset = 0)));
  1681. end;
  1682. function tcgppc.fixref(list: TAsmList; var ref: treference): boolean;
  1683. var
  1684. tmpreg: tregister;
  1685. begin
  1686. result := false;
  1687. if (target_info.system = system_powerpc_darwin) and
  1688. assigned(ref.symbol) and
  1689. (ref.symbol.bind = AB_EXTERNAL) then
  1690. begin
  1691. tmpreg := g_indirect_sym_load(list,ref.symbol.name);
  1692. if (ref.base = NR_NO) then
  1693. ref.base := tmpreg
  1694. else if (ref.index = NR_NO) then
  1695. ref.index := tmpreg
  1696. else
  1697. begin
  1698. list.concat(taicpu.op_reg_reg_reg(A_ADD,tmpreg,ref.base,tmpreg));
  1699. ref.base := tmpreg;
  1700. end;
  1701. ref.symbol := nil;
  1702. end;
  1703. if (ref.base = NR_NO) then
  1704. begin
  1705. ref.base := ref.index;
  1706. ref.index := NR_NO;
  1707. end;
  1708. if (ref.base <> NR_NO) then
  1709. begin
  1710. if (ref.index <> NR_NO) and
  1711. ((ref.offset <> 0) or assigned(ref.symbol)) then
  1712. begin
  1713. result := true;
  1714. tmpreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1715. list.concat(taicpu.op_reg_reg_reg(
  1716. A_ADD,tmpreg,ref.base,ref.index));
  1717. ref.index := NR_NO;
  1718. ref.base := tmpreg;
  1719. end
  1720. end
  1721. else
  1722. if ref.index <> NR_NO then
  1723. internalerror(200208102);
  1724. end;
  1725. { find out whether a is of the form 11..00..11b or 00..11...00. If }
  1726. { that's the case, we can use rlwinm to do an AND operation }
  1727. function tcgppc.get_rlwi_const(a: aint; var l1, l2: longint): boolean;
  1728. var
  1729. temp : longint;
  1730. testbit : aint;
  1731. compare: boolean;
  1732. begin
  1733. get_rlwi_const := false;
  1734. if (a = 0) or (a = -1) then
  1735. exit;
  1736. { start with the lowest bit }
  1737. testbit := 1;
  1738. { check its value }
  1739. compare := boolean(a and testbit);
  1740. { find out how long the run of bits with this value is }
  1741. { (it's impossible that all bits are 1 or 0, because in that case }
  1742. { this function wouldn't have been called) }
  1743. l1 := 31;
  1744. while (((a and testbit) <> 0) = compare) do
  1745. begin
  1746. testbit := testbit shl 1;
  1747. dec(l1);
  1748. end;
  1749. { check the length of the run of bits that comes next }
  1750. compare := not compare;
  1751. l2 := l1;
  1752. while (((a and testbit) <> 0) = compare) and
  1753. (l2 >= 0) do
  1754. begin
  1755. testbit := testbit shl 1;
  1756. dec(l2);
  1757. end;
  1758. { and finally the check whether the rest of the bits all have the }
  1759. { same value }
  1760. compare := not compare;
  1761. temp := l2;
  1762. if temp >= 0 then
  1763. if (a shr (31-temp)) <> ((-ord(compare)) shr (31-temp)) then
  1764. exit;
  1765. { we have done "not(not(compare))", so compare is back to its }
  1766. { initial value. If the lowest bit was 0, a is of the form }
  1767. { 00..11..00 and we need "rlwinm reg,reg,0,l2+1,l1", (+1 }
  1768. { because l2 now contains the position of the last zero of the }
  1769. { first run instead of that of the first 1) so switch l1 and l2 }
  1770. { in that case (we will generate "rlwinm reg,reg,0,l1,l2") }
  1771. if not compare then
  1772. begin
  1773. temp := l1;
  1774. l1 := l2+1;
  1775. l2 := temp;
  1776. end
  1777. else
  1778. { otherwise, l1 currently contains the position of the last }
  1779. { zero instead of that of the first 1 of the second run -> +1 }
  1780. inc(l1);
  1781. { the following is the same as "if l1 = -1 then l1 := 31;" }
  1782. l1 := l1 and 31;
  1783. l2 := l2 and 31;
  1784. get_rlwi_const := true;
  1785. end;
  1786. procedure tcgppc.a_load_store(list:TAsmList;op: tasmop;reg:tregister;
  1787. ref: treference);
  1788. var
  1789. tmpreg: tregister;
  1790. tmpref: treference;
  1791. largeOffset: Boolean;
  1792. begin
  1793. tmpreg := NR_NO;
  1794. if target_info.system = system_powerpc_macos then
  1795. begin
  1796. largeOffset:= (cardinal(ref.offset-low(smallint)) >
  1797. high(smallint)-low(smallint));
  1798. if assigned(ref.symbol) then
  1799. begin {Load symbol's value}
  1800. tmpreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1801. reference_reset(tmpref);
  1802. tmpref.symbol := ref.symbol;
  1803. tmpref.base := NR_RTOC;
  1804. if macos_direct_globals then
  1805. list.concat(taicpu.op_reg_ref(A_LA,tmpreg,tmpref))
  1806. else
  1807. list.concat(taicpu.op_reg_ref(A_LWZ,tmpreg,tmpref));
  1808. end;
  1809. if largeOffset then
  1810. begin {Add hi part of offset}
  1811. reference_reset(tmpref);
  1812. if Smallint(Lo(ref.offset)) < 0 then
  1813. tmpref.offset := Hi(ref.offset) + 1 {Compensate when lo part is negative}
  1814. else
  1815. tmpref.offset := Hi(ref.offset);
  1816. if (tmpreg <> NR_NO) then
  1817. list.concat(taicpu.op_reg_reg_ref(A_ADDIS,tmpreg, tmpreg,tmpref))
  1818. else
  1819. begin
  1820. tmpreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1821. list.concat(taicpu.op_reg_ref(A_LIS,tmpreg,tmpref));
  1822. end;
  1823. end;
  1824. if (tmpreg <> NR_NO) then
  1825. begin
  1826. {Add content of base register}
  1827. if ref.base <> NR_NO then
  1828. list.concat(taicpu.op_reg_reg_reg(A_ADD,tmpreg,
  1829. ref.base,tmpreg));
  1830. {Make ref ready to be used by op}
  1831. ref.symbol:= nil;
  1832. ref.base:= tmpreg;
  1833. if largeOffset then
  1834. ref.offset := Smallint(Lo(ref.offset));
  1835. list.concat(taicpu.op_reg_ref(op,reg,ref));
  1836. //list.concat(tai_comment.create(strpnew('*** a_load_store indirect global')));
  1837. end
  1838. else
  1839. list.concat(taicpu.op_reg_ref(op,reg,ref));
  1840. end
  1841. else {if target_info.system <> system_powerpc_macos}
  1842. begin
  1843. if assigned(ref.symbol) or
  1844. (cardinal(ref.offset-low(smallint)) >
  1845. high(smallint)-low(smallint)) then
  1846. begin
  1847. tmpreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1848. reference_reset(tmpref);
  1849. tmpref.symbol := ref.symbol;
  1850. tmpref.relsymbol := ref.relsymbol;
  1851. tmpref.offset := ref.offset;
  1852. tmpref.refaddr := addr_hi;
  1853. if ref.base <> NR_NO then
  1854. list.concat(taicpu.op_reg_reg_ref(A_ADDIS,tmpreg,
  1855. ref.base,tmpref))
  1856. else
  1857. list.concat(taicpu.op_reg_ref(A_LIS,tmpreg,tmpref));
  1858. ref.base := tmpreg;
  1859. ref.refaddr := addr_lo;
  1860. list.concat(taicpu.op_reg_ref(op,reg,ref));
  1861. end
  1862. else
  1863. list.concat(taicpu.op_reg_ref(op,reg,ref));
  1864. end;
  1865. end;
  1866. procedure tcgppc.a_jmp(list: TAsmList; op: tasmop; c: tasmcondflag;
  1867. crval: longint; l: tasmlabel);
  1868. var
  1869. p: taicpu;
  1870. begin
  1871. p := taicpu.op_sym(op,l);
  1872. if op <> A_B then
  1873. create_cond_norm(c,crval,p.condition);
  1874. p.is_jmp := true;
  1875. list.concat(p)
  1876. end;
  1877. procedure tcg64fppc.a_op64_reg_reg(list : TAsmList;op:TOpCG;size : tcgsize;regsrc,regdst : tregister64);
  1878. begin
  1879. a_op64_reg_reg_reg(list,op,size,regsrc,regdst,regdst);
  1880. end;
  1881. procedure tcg64fppc.a_op64_const_reg(list : TAsmList;op:TOpCG;size : tcgsize;value : int64;reg : tregister64);
  1882. begin
  1883. a_op64_const_reg_reg(list,op,size,value,reg,reg);
  1884. end;
  1885. procedure tcg64fppc.a_op64_reg_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64);
  1886. begin
  1887. case op of
  1888. OP_AND,OP_OR,OP_XOR:
  1889. begin
  1890. cg.a_op_reg_reg_reg(list,op,OS_32,regsrc1.reglo,regsrc2.reglo,regdst.reglo);
  1891. cg.a_op_reg_reg_reg(list,op,OS_32,regsrc1.reghi,regsrc2.reghi,regdst.reghi);
  1892. end;
  1893. OP_ADD:
  1894. begin
  1895. list.concat(taicpu.op_reg_reg_reg(A_ADDC,regdst.reglo,regsrc1.reglo,regsrc2.reglo));
  1896. list.concat(taicpu.op_reg_reg_reg(A_ADDE,regdst.reghi,regsrc1.reghi,regsrc2.reghi));
  1897. end;
  1898. OP_SUB:
  1899. begin
  1900. list.concat(taicpu.op_reg_reg_reg(A_SUBC,regdst.reglo,regsrc2.reglo,regsrc1.reglo));
  1901. list.concat(taicpu.op_reg_reg_reg(A_SUBFE,regdst.reghi,regsrc1.reghi,regsrc2.reghi));
  1902. end;
  1903. else
  1904. internalerror(2002072801);
  1905. end;
  1906. end;
  1907. procedure tcg64fppc.a_op64_const_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;value : int64;regsrc,regdst : tregister64);
  1908. const
  1909. ops: array[boolean,1..3] of tasmop = ((A_ADDIC,A_ADDC,A_ADDZE),
  1910. (A_SUBIC,A_SUBC,A_ADDME));
  1911. var
  1912. tmpreg: tregister;
  1913. tmpreg64: tregister64;
  1914. issub: boolean;
  1915. begin
  1916. case op of
  1917. OP_AND,OP_OR,OP_XOR:
  1918. begin
  1919. cg.a_op_const_reg_reg(list,op,OS_32,aint(value),regsrc.reglo,regdst.reglo);
  1920. cg.a_op_const_reg_reg(list,op,OS_32,aint(value shr 32),regsrc.reghi,
  1921. regdst.reghi);
  1922. end;
  1923. OP_ADD, OP_SUB:
  1924. begin
  1925. if (value < 0) and
  1926. (value <> low(value)) then
  1927. begin
  1928. if op = OP_ADD then
  1929. op := OP_SUB
  1930. else
  1931. op := OP_ADD;
  1932. value := -value;
  1933. end;
  1934. if (longint(value) <> 0) then
  1935. begin
  1936. issub := op = OP_SUB;
  1937. if (value > 0) and
  1938. (value-ord(issub) <= 32767) then
  1939. begin
  1940. list.concat(taicpu.op_reg_reg_const(ops[issub,1],
  1941. regdst.reglo,regsrc.reglo,longint(value)));
  1942. list.concat(taicpu.op_reg_reg(ops[issub,3],
  1943. regdst.reghi,regsrc.reghi));
  1944. end
  1945. else if ((value shr 32) = 0) then
  1946. begin
  1947. tmpreg := tcgppc(cg).rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1948. cg.a_load_const_reg(list,OS_32,aint(value),tmpreg);
  1949. list.concat(taicpu.op_reg_reg_reg(ops[issub,2],
  1950. regdst.reglo,regsrc.reglo,tmpreg));
  1951. list.concat(taicpu.op_reg_reg(ops[issub,3],
  1952. regdst.reghi,regsrc.reghi));
  1953. end
  1954. else
  1955. begin
  1956. tmpreg64.reglo := tcgppc(cg).rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1957. tmpreg64.reghi := tcgppc(cg).rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1958. a_load64_const_reg(list,value,tmpreg64);
  1959. a_op64_reg_reg_reg(list,op,size,tmpreg64,regsrc,regdst);
  1960. end
  1961. end
  1962. else
  1963. begin
  1964. cg.a_load_reg_reg(list,OS_INT,OS_INT,regsrc.reglo,regdst.reglo);
  1965. cg.a_op_const_reg_reg(list,op,OS_32,aint(value shr 32),regsrc.reghi,
  1966. regdst.reghi);
  1967. end;
  1968. end;
  1969. else
  1970. internalerror(2002072802);
  1971. end;
  1972. end;
  1973. begin
  1974. cg := tcgppc.create;
  1975. cg64 :=tcg64fppc.create;
  1976. end.