cgcpu.pas 93 KB

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