cgcpu.pas 90 KB

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