cgcpu.pas 92 KB

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