cgcpu.pas 88 KB

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