cgcpu.pas 90 KB

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