cgcpu.pas 91 KB

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