cgcpu.pas 91 KB

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