cgcpu.pas 90 KB

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